/
digest.go
322 lines (274 loc) · 7.38 KB
/
digest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package structures
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"hash"
"strconv"
"strings"
"time"
)
type (
// QOP is a custom type for Authorization header "qop" field
QOP uint
// Algorithm is a custom type for Authorization header "algorithm" field
Algorithm uint
digest struct {
Username string
URI string
QOP QOP
Algorithm Algorithm
Cnonce []byte
Response string
Body []byte
}
// RequestDigest is a functional structure for request Authorization header value creation
RequestDigest struct {
digest
Secret string
}
// ResponseDigest is a functional structure for response Authorization header value validation
ResponseDigest struct {
digest
Timestamp int
Snonce []byte
OriginalURI string
OriginalCnonce []byte
}
)
// Authorization header's "qop" values
const (
QopUnknown QOP = iota
QopAuth
QopAuthInt
)
var qop2string = map[QOP]string{
QopAuth: "auth",
QopAuthInt: "auth-int",
}
var string2qop = map[string]QOP{
"auth": QopAuth,
"auth-int": QopAuthInt,
}
// NewQOP creates QOP value from raw string or returns an error if input is unknown
func NewQOP(in string) (QOP, error) {
if result, ok := string2qop[in]; ok {
return result, nil
}
return QopUnknown, fmt.Errorf("unknown QOP value %s", in)
}
func (o QOP) String() string {
if result, ok := qop2string[o]; ok {
return result
}
return "unknown"
}
// Authorization header's "algorithm" values
const (
AlgorithmUnknown Algorithm = iota
AlgorithmSHA256
)
var algorithm2string = map[Algorithm]string{
AlgorithmSHA256: "SHA-256",
}
var string2algorithm = map[string]Algorithm{
"SHA-256": AlgorithmSHA256,
}
// NewAlgorithm creates Algorithm value from raw string or returns an error if input is unknown
func NewAlgorithm(in string) (Algorithm, error) {
if result, ok := string2algorithm[in]; ok {
return result, nil
}
return AlgorithmUnknown, fmt.Errorf("unknown algorithm %s", in)
}
func (o Algorithm) String() string {
if result, ok := algorithm2string[o]; ok {
return result
}
return "unknown"
}
// Hash returns corresponding hash.Hash creator function
func (o Algorithm) Hash() (hashCreator func() hash.Hash, err error) {
switch o {
case AlgorithmSHA256:
hashCreator = sha256.New
default:
err = fmt.Errorf("unsupported algorithm %s", o)
}
return
}
func calcNonce() (nonce []byte, err error) {
nonceRand := make([]byte, 32)
if _, err := rand.Read(nonceRand); err != nil {
return nil, fmt.Errorf("cannot create cnonce: %s", err)
}
buf := bytes.NewBuffer([]byte(fmt.Sprintf("%d:", time.Now().UTC().Unix())))
buf.Write(nonceRand)
nonce = buf.Bytes()
return
}
// NewRequestDigest creates new RequestDigest structure
func NewRequestDigest(ObjectGUID, secret, uri string, body []byte) (result *RequestDigest, err error) {
var cnonce []byte
if cnonce, err = calcNonce(); err != nil {
return
}
result = &RequestDigest{
digest: digest{
Username: ObjectGUID,
Algorithm: AlgorithmSHA256,
QOP: QopAuthInt,
URI: uri,
Cnonce: cnonce,
Body: body,
},
Secret: secret,
}
return
}
// CreateHeader creates a string for Authorization header for a Gateway request
func (o *RequestDigest) CreateHeader() (digest string, err error) {
var hashFunc func() hash.Hash
if hashFunc, err = o.Algorithm.Hash(); err != nil {
return
}
mac := hmac.New(hashFunc, []byte(o.Secret))
mac.Write([]byte(o.Username))
mac.Write(o.Cnonce)
mac.Write([]byte(o.QOP.String()))
mac.Write([]byte(o.URI))
if o.QOP == QopAuthInt {
mac.Write(o.Body)
}
o.Response = hex.EncodeToString(mac.Sum(nil))
digest = fmt.Sprintf(
"Digest username=%s, uri=\"%s\", algorithm=%s, cnonce=\"%s\", qop=%s, response=\"%s\"",
o.Username,
o.URI,
o.Algorithm,
base64.StdEncoding.EncodeToString(o.Cnonce),
o.QOP,
o.Response,
)
return
}
// NewResponseDigest parse Authorization header's content and fill ResponseDigest structure
func NewResponseDigest(authorizationHeader string) (result *ResponseDigest, err error) {
if len(authorizationHeader) == 0 {
err = errors.New("authorization header is missing")
return
}
values := map[string]string{
"username": "",
"uri": "",
"response": "",
"algorithm": "",
"qop": "",
"cnonce": "",
"snonce": "",
}
elements := strings.Split(
strings.TrimSpace(
strings.ReplaceAll(
strings.ReplaceAll(authorizationHeader, "\r", ""),
"\n", "",
),
),
",",
)
for i := range elements {
keyValue := strings.TrimSpace(elements[i])
delimiterPos := strings.Index(keyValue, "=")
if delimiterPos != -1 {
key := keyValue[:delimiterPos]
if i == 0 {
key = strings.Replace(strings.ToLower(key), "digest ", "", 1)
}
if _, ok := values[key]; ok {
value := keyValue[delimiterPos+1:]
values[key] = strings.TrimSpace(strings.Trim(value, "\""))
}
}
}
result = &ResponseDigest{}
for key, value := range values {
if len(value) == 0 {
err = fmt.Errorf("digest mismatch: empty value for %s", key)
return
}
switch key {
case "username":
result.Username = value
case "uri":
result.URI = value
case "response":
result.Response = value
case "algorithm":
if result.Algorithm, err = NewAlgorithm(value); err != nil {
err = fmt.Errorf("digest mismatch: format error: %s", err)
return
}
case "qop":
if result.QOP, err = NewQOP(value); err != nil {
err = fmt.Errorf("digest mismatch: format error: %s", err)
return
}
case "cnonce":
if result.Cnonce, err = base64.StdEncoding.DecodeString(value); err != nil {
err = fmt.Errorf("digest mismatch: corrupted value for cnonce (%s)", err)
return
}
case "snonce":
if result.Snonce, err = base64.StdEncoding.DecodeString(value); err != nil {
err = fmt.Errorf("digest mismatch: corrupted value for snonce (%s)", err)
return
}
timestampDelimiterPos := bytes.Index(result.Snonce, []byte(":"))
if timestampDelimiterPos == -1 {
err = errors.New("digest mismatch: corrupted value for snonce (missing timestamp)")
return
}
if result.Timestamp, err = strconv.Atoi(string(result.Snonce[:timestampDelimiterPos])); err != nil {
err = fmt.Errorf("digest mismatch: corrupted value for snonce (unexpected timestamp value: %s)", err)
return
}
}
}
return
}
// Verify verifies that parsed response digest was made using given GUID/secret pair.
// In addition, if set, original request's GUID, URI and cnonce will be compared to parsed values.
func (o *ResponseDigest) Verify(objectGUID, secret string) (err error) {
if strings.ToLower(objectGUID) != strings.ToLower(o.Username) {
return errors.New("digest mismatch: username mismatch")
}
if len(o.OriginalURI) > 0 && o.OriginalURI != o.URI {
return errors.New("digest mismatch: uri mismatch")
}
if len(o.OriginalCnonce) > 0 && !bytes.Equal(o.OriginalCnonce, o.Cnonce) {
return errors.New("digest mismatch: cnonce mismatch")
}
var hashFunc func() hash.Hash
if hashFunc, err = o.Algorithm.Hash(); err != nil {
return
}
mac := hmac.New(hashFunc, []byte(secret))
mac.Write([]byte(o.Username))
mac.Write(o.Cnonce)
mac.Write(o.Snonce)
mac.Write([]byte(o.QOP.String()))
mac.Write([]byte(o.URI))
if o.QOP == QopAuthInt {
mac.Write(o.Body)
}
expectedDigest := hex.EncodeToString(mac.Sum(nil))
if !hmac.Equal([]byte(o.Response), []byte(expectedDigest)) {
return errors.New("digest mismatch")
}
return nil
}