/
v4.go
350 lines (303 loc) · 11.1 KB
/
v4.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Minio Cloud Storage, (C) 2015, 2016 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// This file implements helper functions to validate AWS
// Signature Version '4' authorization header.
//
// This package provides comprehensive helpers for following signature
// types.
// - Based on Authorization header.
// - Based on Query parameters.
// - Based on Form POST policy.
package signature
import (
"bytes"
"crypto/subtle"
"encoding/hex"
"net/http"
"sort"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/opensds/multi-cloud/api/pkg/filters/signature/credentials"
"github.com/opensds/multi-cloud/api/pkg/filters/signature/credentials/keystonecredentials"
. "github.com/opensds/multi-cloud/api/pkg/s3/datatype"
. "github.com/opensds/multi-cloud/s3/error"
)
// AWS Signature Version '4' constants.
const (
signV4Algorithm = "AWS4-HMAC-SHA256"
)
// getSignedHeaders generate a string i.e alphabetically sorted,
// semicolon-separated list of lowercase request header names
func getSignedHeaders(signedHeaders http.Header) string {
var headers []string
for k := range signedHeaders {
headers = append(headers, strings.ToLower(k))
}
sort.Strings(headers)
return strings.Join(headers, ";")
}
// getCanonicalRequest generate a canonical request of style
//
// canonicalRequest =
// <HTTPMethod>\n
// <CanonicalURI>\n
// <CanonicalQueryString>\n
// <CanonicalHeaders>\n
// <SignedHeaders>\n
// <HashedPayload>
//
func getCanonicalRequest(extractedSignedHeaders http.Header, payload, queryStr,
urlPath, method string) string {
rawQuery := strings.Replace(queryStr, "+", "%20", -1)
encodedPath := getURLEncodedName(urlPath)
canonicalRequest := strings.Join([]string{
method,
encodedPath,
rawQuery,
getCanonicalHeaders(extractedSignedHeaders),
getSignedHeaders(extractedSignedHeaders),
payload,
}, "\n")
return canonicalRequest
}
// getCanonicalHeaders generate a list of request headers with their values
func getCanonicalHeaders(signedHeaders http.Header) string {
var headers []string
vals := make(http.Header)
for k, vv := range signedHeaders {
headers = append(headers, strings.ToLower(k))
vals[strings.ToLower(k)] = vv
}
sort.Strings(headers)
var buf bytes.Buffer
for _, k := range headers {
buf.WriteString(k)
buf.WriteByte(':')
for idx, v := range vals[k] {
if idx > 0 {
buf.WriteByte(',')
}
buf.WriteString(signV4TrimAll(v))
}
buf.WriteByte('\n')
}
return buf.String()
}
// getScope generate a string of a specific date, an AWS region, and a service.
func getScope(t time.Time, region string) string {
scope := strings.Join([]string{
t.Format(YYYYMMDD),
region,
"s3",
"aws4_request",
}, "/")
return scope
}
// getStringToSign a string based on selected query values.
func getStringToSign(canonicalRequest string, t time.Time, region string) string {
stringToSign := signV4Algorithm + "\n" + t.Format(Iso8601Format) + "\n"
stringToSign = stringToSign + getScope(t, region) + "\n"
canonicalRequestBytes := sum256([]byte(canonicalRequest))
stringToSign = stringToSign + hex.EncodeToString(canonicalRequestBytes[:])
return stringToSign
}
// getSigningKey hmac seed to calculate final signature.
func getSigningKey(secretKey string, t time.Time, region string) []byte {
date := sumHMAC([]byte("AWS4"+secretKey), []byte(t.Format(YYYYMMDD)))
regionBytes := sumHMAC(date, []byte(region))
service := sumHMAC(regionBytes, []byte("s3"))
signingKey := sumHMAC(service, []byte("aws4_request"))
return signingKey
}
// getSignature final signature in hexadecimal form.
func getSignature(signingKey []byte, stringToSign string) string {
return hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
}
// doesPolicySignatureMatch - Verify query headers with post policy
// - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
// returns true if matches, false otherwise. if error is not nil then it is always false
func DoesPolicySignatureMatchV4(formValues map[string]string) (credential credentials.Value, err error) {
// Parse credential tag.
credHeader, err := parseCredential(formValues["X-Amz-Credential"])
if err != nil {
return credential, err
}
// Verify if the region is valid.
region := credHeader.scope.region
if !isValidRegion(region) {
return credential, ErrInvalidRegion
}
// Parse date string.
t, e := time.Parse(Iso8601Format, formValues["X-Amz-Date"])
if e != nil {
return credential, ErrMalformedDate
}
credential, e = keystonecredentials.NewCredentialsClient(credHeader.accessKey).Get()
if e != nil {
return credential, ErrInvalidAccessKeyID
}
// Get signing key.
signingKey := getSigningKey(credential.SecretAccessKey, t, region)
// Get signature.
newSignature := getSignature(signingKey, formValues["Policy"])
// Verify signature.
if newSignature != formValues["X-Amz-Signature"] {
return credential, ErrSignatureDoesNotMatch
}
return credential, nil
}
// doesPresignedSignatureMatch - Verify query headers with presigned signature
// - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
// returns true if matches, false otherwise. if error is not nil then it is always false
func DoesPresignedSignatureMatchV4(r *http.Request,
validateRegion bool) (credential credentials.Value, err error) {
// Parse request query string.
preSignValues, err := parsePreSignV4(r.URL.Query(), r.Header)
if err != nil {
return credential, err
}
credential, e := keystonecredentials.NewCredentialsClient(preSignValues.Credential.accessKey).Get()
if e != nil {
return credential, ErrInvalidAccessKeyID
}
if preSignValues.Expires > PresignedUrlExpireLimit {
return credential, ErrMalformedExpires
}
if time.Now().Sub(preSignValues.Date) > time.Duration(preSignValues.Expires) {
return credential, ErrExpiredPresignRequest
}
// Verify if region is valid.
region := preSignValues.Credential.scope.region
// Should validate region, only if region is set. Some operations
// do not need region validated for example GetBucketLocation.
if validateRegion && !isValidRegion(region) {
return credential, ErrInvalidRegion
}
// Extract all the signed headers along with its values.
extractedSignedHeaders, err := extractSignedHeaders(preSignValues.SignedHeaders, r)
if err != nil {
return
}
/// Verify finally if signature is same.
// Get canonical request.
query := r.URL.Query()
query.Del("X-Amz-Signature")
presignedCanonicalReq := getCanonicalRequest(extractedSignedHeaders, UnsignedPayload,
query.Encode(), r.URL.Path, r.Method)
// Get string to sign from canonical request.
presignedStringToSign := getStringToSign(presignedCanonicalReq, preSignValues.Date, region)
// Get hmac presigned signing key.
presignedSigningKey := getSigningKey(credential.SecretAccessKey, preSignValues.Date, region)
// Get new signature.
newSignature := getSignature(presignedSigningKey, presignedStringToSign)
// Verify signature.
if preSignValues.Signature != newSignature {
return credential, ErrSignatureDoesNotMatch
}
return credential, nil
}
// get credential but not verify it, used only for signed v4 auth
func getCredentialUnverified(r *http.Request) (credential credentials.Value, err error) {
v4Auth := r.Header.Get("Authorization")
signV4Values, err := parseSignV4(v4Auth, r.Header)
if err != nil {
return credential, err
}
credential, e := keystonecredentials.NewCredentialsClient(signV4Values.Credential.accessKey).Get()
if e != nil {
return credential, ErrInvalidAccessKeyID
}
return credential, nil
}
// doesSignatureMatch - Verify authorization header with calculated header in accordance with
// - http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html
// returns true if matches, false otherwise. if error is not nil then it is always false
func DoesSignatureMatchV4(hashedPayload string, r *http.Request,
validateRegion bool) (credential credentials.Value, err error) {
log.Debugln("auth v4")
// Save authorization header.
v4Auth := r.Header.Get("Authorization")
// Parse signature version '4' header.
signV4Values, err := parseSignV4(v4Auth, r.Header)
if err != nil {
return credential, err
}
// http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
// The x-amz-content-sha256 header is required for all AWS Signature Version 4 requests.
// It provides a hash of the request payload. If there is no payload, you must provide
// the hash of an empty string.
if hashedPayload == "" {
log.Errorln("X-Amz-Content-Sha256 is required for Signature Version 4 requests.")
return credential, ErrContentSHA256Mismatch
}
// Extract all the signed headers along with its values.
extractedSignedHeaders, err := extractSignedHeaders(signV4Values.SignedHeaders, r)
if err != nil {
return credential, err
}
// Verify if region is valid.
region := signV4Values.Credential.scope.region
// Should validate region, only if region is set. Some operations
// do not need region validated for example GetBucketLocation.
if validateRegion && !isValidRegion(region) {
return credential, ErrInvalidRegion
}
// Extract date, if not present throw error.
var date string
if date = r.Header.Get("x-amz-date"); date == "" {
if date = r.Header.Get("Date"); date == "" {
return credential, ErrMissingDateHeader
}
}
// Parse date header.
t, err := ParseAmzDate(date)
if err != nil {
return credential, err
}
diff := time.Now().Sub(t)
if diff > 15*time.Minute || diff < -15*time.Minute {
return credential, ErrRequestTimeTooSkewed
}
// Query string.
queryStr := r.URL.Query().Encode()
// Get canonical request.
canonicalRequest := getCanonicalRequest(extractedSignedHeaders, hashedPayload, queryStr,
r.URL.Path, r.Method)
// Get string to sign from canonical request.
stringToSign := getStringToSign(canonicalRequest, t, region)
credential, e := keystonecredentials.NewCredentialsClient(signV4Values.Credential.accessKey).Get()
if e != nil {
return credential, ErrInvalidAccessKeyID
}
// Get hmac signing key.
signingKey := getSigningKey(credential.SecretAccessKey, t, region)
// Calculate signature.
newSignature := getSignature(signingKey, stringToSign)
// Verify if signature match.
if newSignature != signV4Values.Signature {
return credential, ErrSignatureDoesNotMatch
}
return credential, nil
}
// compareSignatureV4 returns true if and only if both signatures
// are equal. The signatures are expected to be HEX encoded strings
// according to the AWS S3 signature V4 spec.
func compareSignatureV4(sig1, sig2 string) bool {
// The CTC using []byte(str) works because the hex encoding
// is unique for a sequence of bytes. See also compareSignatureV2.
return subtle.ConstantTimeCompare([]byte(sig1), []byte(sig2)) == 1
}