-
Notifications
You must be signed in to change notification settings - Fork 342
/
v4.go
407 lines (364 loc) · 10.4 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package sig
import (
"bufio"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"sort"
"strconv"
"strings"
"time"
"unicode"
"github.com/treeverse/lakefs/pkg/auth/model"
"github.com/treeverse/lakefs/pkg/gateway/errors"
)
const (
V4authHeaderName = "Authorization"
V4authHeaderPrefix = "AWS4-HMAC-SHA256"
AmzDecodedContentLength = "X-Amz-Decoded-Content-Length"
v4StreamingPayloadHash = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
v4UnsignedPayload = "UNSIGNED-PAYLOAD"
v4authHeaderPayload = "x-amz-content-sha256"
v4scopeTerminator = "aws4_request"
v4timeFormat = "20060102T150405Z"
v4shortTimeFormat = "20060102"
v4SignatureHeader = "X-Amz-Signature"
)
var (
V4AuthHeaderRegexp = regexp.MustCompile(`AWS4-HMAC-SHA256 Credential=(?P<AccessKeyId>[A-Z0-9]{20})/(?P<Date>\d{8})/(?P<Region>[\w\-]+)/(?P<Service>[\w\-]+)/aws4_request,\s*SignedHeaders=(?P<SignatureHeaders>[\w\-\;]+),\s*Signature=(?P<Signature>[abcdef0123456789]{64})`)
V4CredentialScopeRegexp = regexp.MustCompile(`(?P<AccessKeyId>[A-Z0-9]{20})/(?P<Date>\d{8})/(?P<Region>[\w\-]+)/(?P<Service>[\w\-]+)/aws4_request`)
)
type V4Auth struct {
AccessKeyID string
Date string
Region string
Service string
SignedHeaders []string
SignedHeadersString string
Signature string
}
func (a V4Auth) GetAccessKeyID() string {
return a.AccessKeyID
}
func splitHeaders(headers string) []string {
headerValues := strings.Split(headers, ";")
sort.Strings(headerValues)
return headerValues
}
func ParseV4AuthContext(r *http.Request) (V4Auth, error) {
var ctx V4Auth
// start by trying to extract the data from the Authorization header
headerValue := r.Header.Get(V4authHeaderName)
if len(headerValue) > 0 {
match := V4AuthHeaderRegexp.FindStringSubmatch(headerValue)
if len(match) == 0 {
return ctx, ErrHeaderMalformed
}
result := make(map[string]string)
for i, name := range V4AuthHeaderRegexp.SubexpNames() {
if i != 0 && name != "" {
result[name] = match[i]
}
}
headers := splitHeaders(result["SignatureHeaders"])
ctx.AccessKeyID = result["AccessKeyId"]
ctx.Date = result["Date"]
ctx.Region = result["Region"]
ctx.Service = result["Service"]
ctx.Signature = result["Signature"]
ctx.SignedHeaders = headers
ctx.SignedHeadersString = result["SignatureHeaders"]
return ctx, nil
}
// otherwise, see if we have all the required query parameters
query := r.URL.Query()
algorithm := query.Get("X-Amz-Algorithm")
if len(algorithm) == 0 || !strings.EqualFold(algorithm, V4authHeaderPrefix) {
return ctx, errors.ErrInvalidQuerySignatureAlgo
}
credentialScope := query.Get("X-Amz-Credential")
if len(credentialScope) == 0 {
return ctx, errors.ErrMissingCredTag
}
credsMatch := V4CredentialScopeRegexp.FindStringSubmatch(credentialScope)
if len(credsMatch) == 0 {
return ctx, errors.ErrCredMalformed
}
credsResult := make(map[string]string)
for i, name := range V4CredentialScopeRegexp.SubexpNames() {
if i != 0 && name != "" {
credsResult[name] = credsMatch[i]
}
}
ctx.AccessKeyID = credsResult["AccessKeyId"]
ctx.Date = credsResult["Date"]
ctx.Region = credsResult["Region"]
ctx.Service = credsResult["Service"]
ctx.SignedHeadersString = query.Get("X-Amz-SignedHeaders")
headers := splitHeaders(ctx.SignedHeadersString)
ctx.SignedHeaders = headers
ctx.Signature = query.Get(v4SignatureHeader)
return ctx, nil
}
func V4Verify(auth V4Auth, credentials *model.Credential, r *http.Request) error {
ctx := &verificationCtx{
Request: r,
Query: r.URL.Query(),
AuthValue: auth,
}
canonicalRequest := ctx.buildCanonicalRequest()
stringToSign, err := ctx.buildSignedString(canonicalRequest)
if err != nil {
return err
}
// sign
signingKey := createSignature(credentials.SecretAccessKey, auth.Date, auth.Region, auth.Service)
signature := hex.EncodeToString(sign(signingKey, stringToSign))
// compare signatures
if !Equal([]byte(signature), []byte(auth.Signature)) {
return errors.ErrSignatureDoesNotMatch
}
// wrap body with verifier
reader, err := ctx.reader(r.Body, credentials)
if err != nil {
return err
}
r.Body = reader
// update to decoded content length
contentLength, err := ctx.contentLength()
if err != nil {
return err
}
r.ContentLength = contentLength
return nil
}
type verificationCtx struct {
Request *http.Request
Query url.Values
AuthValue V4Auth
}
func (ctx *verificationCtx) queryEscape(str string) string {
return strings.ReplaceAll(url.QueryEscape(str), "+", "%20")
}
func (ctx *verificationCtx) canonicalizeQueryString() string {
queryNames := make([]string, 0, len(ctx.Query))
for k := range ctx.Query {
if k == v4SignatureHeader {
continue
}
queryNames = append(queryNames, k)
}
sort.Strings(queryNames)
buf := make([]string, len(queryNames))
for i, key := range queryNames {
buf[i] = fmt.Sprintf("%s=%s", ctx.queryEscape(key), ctx.queryEscape(ctx.Query.Get(key)))
}
return strings.Join(buf, "&")
}
func (ctx *verificationCtx) canonicalizeHeaders(headers []string) string {
var buf strings.Builder
for _, header := range headers {
var value string
if strings.EqualFold(strings.ToLower(header), "host") {
// in Go, Host is removed from the headers and is promoted to request.Host for some reason
value = ctx.Request.Host
} else {
value = getInsensitiveHeader(ctx.Request, header)
}
buf.WriteString(header)
buf.WriteString(":")
buf.WriteString(ctx.trimAll(value))
buf.WriteString("\n")
}
return buf.String()
}
func (ctx *verificationCtx) trimAll(str string) string {
str = strings.TrimSpace(str)
inSpace := false
var buf strings.Builder
for _, ch := range str {
if unicode.IsSpace(ch) {
if !inSpace {
// first space to appear
buf.WriteRune(ch)
inSpace = true
}
} else {
// not a space
buf.WriteRune(ch)
inSpace = false
}
}
return buf.String()
}
func getInsensitiveHeader(r *http.Request, headerName string) string {
for k, v := range r.Header {
if strings.EqualFold(k, headerName) {
return v[0]
}
}
return ""
}
func (ctx *verificationCtx) payloadHash() string {
payloadHash := getInsensitiveHeader(ctx.Request, v4authHeaderPayload)
if payloadHash == "" {
return v4UnsignedPayload
}
return payloadHash
}
func (ctx *verificationCtx) buildCanonicalRequest() string {
// Step 1: Canonical request
method := ctx.Request.Method
canonicalURI := EncodePath(ctx.Request.URL.Path)
canonicalQueryString := ctx.canonicalizeQueryString()
canonicalHeaders := ctx.canonicalizeHeaders(ctx.AuthValue.SignedHeaders)
signedHeaders := ctx.AuthValue.SignedHeadersString
payloadHash := ctx.payloadHash()
canonicalRequest := strings.Join([]string{
method,
canonicalURI,
canonicalQueryString,
canonicalHeaders,
signedHeaders,
payloadHash,
}, "\n")
return canonicalRequest
}
func (ctx *verificationCtx) getAmzDate() (string, error) {
// https://docs.aws.amazon.com/general/latest/gr/sigv4-date-handling.html
amzDate := ctx.Request.URL.Query().Get("X-Amz-Date")
if len(amzDate) == 0 {
amzDate = ctx.Request.Header.Get("x-amz-date")
if len(amzDate) == 0 {
amzDate = ctx.Request.Header.Get("date")
if len(amzDate) == 0 {
return "", errors.ErrMissingDateHeader
}
}
}
// parse date
ts, err := time.Parse(v4timeFormat, amzDate)
if err != nil {
return "", errors.ErrMalformedDate
}
// parse signature date
sigTS, err := time.Parse(v4shortTimeFormat, ctx.AuthValue.Date)
if err != nil {
return "", errors.ErrMalformedCredentialDate
}
// ensure same date
if sigTS.Year() != ts.Year() || sigTS.Month() != ts.Month() || sigTS.Day() != ts.Day() {
return "", errors.ErrMalformedCredentialDate
}
return amzDate, nil
}
func sign(key []byte, msg string) []byte {
h := hmac.New(sha256.New, key)
_, _ = h.Write([]byte(msg))
return h.Sum(nil)
}
func createSignature(key, dateStamp, region, service string) []byte {
kDate := sign([]byte(fmt.Sprintf("AWS4%s", key)), dateStamp)
kRegion := sign(kDate, region)
kService := sign(kRegion, service)
kSigning := sign(kService, v4scopeTerminator)
return kSigning
}
func (ctx *verificationCtx) buildSignedString(canonicalRequest string) (string, error) {
// Step 2: Create string to sign
algorithm := V4authHeaderPrefix
credentialScope := strings.Join([]string{
ctx.AuthValue.Date,
ctx.AuthValue.Region,
ctx.AuthValue.Service,
v4scopeTerminator,
}, "/")
amzDate, err := ctx.getAmzDate()
if err != nil {
return "", err
}
h := sha256.New()
if _, err := h.Write([]byte(canonicalRequest)); err != nil {
return "", err
}
hashedCanonicalRequest := hex.EncodeToString(h.Sum(nil))
stringToSign := strings.Join([]string{
algorithm,
amzDate,
credentialScope,
hashedCanonicalRequest,
}, "\n")
return stringToSign, nil
}
func (ctx *verificationCtx) isStreaming() bool {
payloadHash := ctx.payloadHash()
return strings.EqualFold(payloadHash, v4StreamingPayloadHash)
}
func (ctx *verificationCtx) isUnsigned() bool {
return strings.EqualFold(ctx.payloadHash(), v4UnsignedPayload)
}
func (ctx *verificationCtx) contentLength() (int64, error) {
size := ctx.Request.ContentLength
if ctx.isStreaming() {
if sizeStr, ok := ctx.Request.Header[AmzDecodedContentLength]; ok {
if sizeStr[0] == "" {
return 0, errors.ErrMissingContentLength
}
var err error
size, err = strconv.ParseInt(sizeStr[0], 10, 64)
if err != nil {
return 0, err
}
}
}
return size, nil
}
func (ctx *verificationCtx) reader(reader io.ReadCloser, creds *model.Credential) (io.ReadCloser, error) {
if ctx.isStreaming() {
amzDate, err := ctx.getAmzDate()
if err != nil {
return nil, err
}
chunkReader, err := newSignV4ChunkedReader(bufio.NewReader(reader), amzDate, ctx.AuthValue, creds)
if err != nil {
return nil, err
}
return chunkReader, nil
}
if ctx.isUnsigned() {
return reader, nil
}
return NewSha265Reader(reader, ctx.payloadHash())
}
type V4Authenticator struct {
request *http.Request
ctx V4Auth
}
func (a *V4Authenticator) Parse() (SigContext, error) {
var ctx V4Auth
var err error
ctx, err = ParseV4AuthContext(a.request)
if err != nil {
return ctx, err
}
a.ctx = ctx
return a.ctx, nil
}
func (a *V4Authenticator) String() string {
return "sigv4"
}
func (a *V4Authenticator) Verify(creds *model.Credential, _ string) error {
err := V4Verify(a.ctx, creds, a.request)
return err
}
func NewV4Authenticator(r *http.Request) SigAuthenticator {
return &V4Authenticator{
request: r,
ctx: V4Auth{},
}
}