forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.go
195 lines (171 loc) · 4.27 KB
/
sign.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
package s3gof3r
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"io"
"io/ioutil"
"net/http"
"sort"
"strings"
"time"
)
const (
prefix = "AWS4-HMAC-SHA256"
isoFormat = "20060102T150405Z"
shortDate = "20060102"
)
var ignoredHeaders = map[string]bool{
"Authorization": true,
"Content-Type": true,
"Content-Length": true,
"User-Agent": true,
}
type signer struct {
Time time.Time
Request *http.Request
Region string
Keys Keys
credentialString string
signedHeaders string
signature string
canonicalHeaders string
canonicalString string
stringToSign string
}
// Sign signs the http.Request
func (b *Bucket) Sign(req *http.Request) {
if req.Header == nil {
req.Header = http.Header{}
}
if b.S3.Keys.SecurityToken != "" {
req.Header.Set("X-Amz-Security-Token", b.S3.Keys.SecurityToken)
}
req.Header.Set("User-Agent", "S3Gof3r")
s := &signer{
Time: time.Now(),
Request: req,
Region: b.S3.Region(),
Keys: b.S3.Keys,
}
s.sign()
}
func (s *signer) sign() {
s.buildTime()
s.buildCredentialString()
s.buildCanonicalHeaders()
s.buildCanonicalString()
s.buildStringToSign()
s.buildSignature()
parts := []string{
prefix + " Credential=" + s.Keys.AccessKey + "/" + s.credentialString,
"SignedHeaders=" + s.signedHeaders,
"Signature=" + s.signature,
}
s.Request.Header.Set("Authorization", strings.Join(parts, ","))
}
func (s *signer) buildTime() {
s.Request.Header.Set("X-Amz-Date", s.Time.UTC().Format(isoFormat))
}
func (s *signer) buildCredentialString() {
s.credentialString = strings.Join([]string{
s.Time.UTC().Format(shortDate),
s.Region,
"s3",
"aws4_request",
}, "/")
}
func (s *signer) buildCanonicalHeaders() {
var headers []string
headers = append(headers, "host")
for k := range s.Request.Header {
if _, ok := ignoredHeaders[http.CanonicalHeaderKey(k)]; ok {
continue
}
headers = append(headers, strings.ToLower(k))
}
sort.Strings(headers)
s.signedHeaders = strings.Join(headers, ";")
headerValues := make([]string, len(headers))
for i, k := range headers {
if k == "host" {
headerValues[i] = "host:" + s.Request.URL.Host
} else {
headerValues[i] = k + ":" +
strings.Join(s.Request.Header[http.CanonicalHeaderKey(k)], ",")
}
}
s.canonicalHeaders = strings.Join(headerValues, "\n")
}
func (s *signer) buildCanonicalString() {
s.Request.URL.RawQuery = strings.Replace(s.Request.URL.Query().Encode(), "+", "%20", -1)
uri := s.Request.URL.Opaque
if uri != "" {
uri = "/" + strings.Join(strings.Split(uri, "/")[3:], "/")
} else {
uri = s.Request.URL.EscapedPath()
}
if uri == "" {
uri = "/"
}
uri = strings.Replace(uri, "@", "%40", -1)
uri = strings.Replace(uri, ":", "%3A", -1)
s.canonicalString = strings.Join([]string{
s.Request.Method,
uri,
s.Request.URL.RawQuery,
s.canonicalHeaders + "\n",
s.signedHeaders,
s.bodyDigest(),
}, "\n")
}
func (s *signer) buildStringToSign() {
s.stringToSign = strings.Join([]string{
prefix,
s.Time.UTC().Format(isoFormat),
s.credentialString,
hex.EncodeToString(sha([]byte(s.canonicalString))),
}, "\n")
}
func (s *signer) buildSignature() {
secret := s.Keys.SecretKey
date := hmacSign([]byte("AWS4"+secret), []byte(s.Time.UTC().Format(shortDate)))
region := hmacSign(date, []byte(s.Region))
service := hmacSign(region, []byte("s3"))
credentials := hmacSign(service, []byte("aws4_request"))
signature := hmacSign(credentials, []byte(s.stringToSign))
s.signature = hex.EncodeToString(signature)
}
func (s *signer) bodyDigest() string {
hash := s.Request.Header.Get("X-Amz-Content-Sha256")
if hash == "" {
if s.Request.Body == nil {
hash = hex.EncodeToString(sha([]byte{}))
} else {
body, _ := ioutil.ReadAll(s.Request.Body)
s.Request.Body = ioutil.NopCloser(bytes.NewReader(body))
hash = hex.EncodeToString(sha(body))
}
s.Request.Header.Add("X-Amz-Content-Sha256", hash)
}
return hash
}
func hmacSign(key []byte, data []byte) []byte {
hash := hmac.New(sha256.New, key)
hash.Write(data)
return hash.Sum(nil)
}
func sha(data []byte) []byte {
hash := sha256.New()
hash.Write(data)
return hash.Sum(nil)
}
func shaReader(r io.ReadSeeker) string {
hash := sha256.New()
start, _ := r.Seek(0, 1)
defer r.Seek(start, 0)
io.Copy(hash, r)
sum := hash.Sum(nil)
return hex.EncodeToString(sum)
}