request-signature-v4.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2017 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package s3signer
  18. import (
  19. "bytes"
  20. "encoding/hex"
  21. "net/http"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "time"
  26. "github.com/minio/minio-go/v6/pkg/s3utils"
  27. )
  28. // Signature and API related constants.
  29. const (
  30. signV4Algorithm = "AWS4-HMAC-SHA256"
  31. iso8601DateFormat = "20060102T150405Z"
  32. yyyymmdd = "20060102"
  33. )
  34. ///
  35. /// Excerpts from @lsegal -
  36. /// https://github.com/aws/aws-sdk-js/issues/659#issuecomment-120477258.
  37. ///
  38. /// User-Agent:
  39. ///
  40. /// This is ignored from signing because signing this causes
  41. /// problems with generating pre-signed URLs (that are executed
  42. /// by other agents) or when customers pass requests through
  43. /// proxies, which may modify the user-agent.
  44. ///
  45. /// Content-Length:
  46. ///
  47. /// This is ignored from signing because generating a pre-signed
  48. /// URL should not provide a content-length constraint,
  49. /// specifically when vending a S3 pre-signed PUT URL. The
  50. /// corollary to this is that when sending regular requests
  51. /// (non-pre-signed), the signature contains a checksum of the
  52. /// body, which implicitly validates the payload length (since
  53. /// changing the number of bytes would change the checksum)
  54. /// and therefore this header is not valuable in the signature.
  55. ///
  56. /// Content-Type:
  57. ///
  58. /// Signing this header causes quite a number of problems in
  59. /// browser environments, where browsers like to modify and
  60. /// normalize the content-type header in different ways. There is
  61. /// more information on this in https://goo.gl/2E9gyy. Avoiding
  62. /// this field simplifies logic and reduces the possibility of
  63. /// future bugs.
  64. ///
  65. /// Authorization:
  66. ///
  67. /// Is skipped for obvious reasons
  68. ///
  69. var v4IgnoredHeaders = map[string]bool{
  70. "Authorization": true,
  71. "Content-Type": true,
  72. "Content-Length": true,
  73. "User-Agent": true,
  74. }
  75. // getSigningKey hmac seed to calculate final signature.
  76. func getSigningKey(secret, loc string, t time.Time) []byte {
  77. date := sumHMAC([]byte("AWS4"+secret), []byte(t.Format(yyyymmdd)))
  78. location := sumHMAC(date, []byte(loc))
  79. service := sumHMAC(location, []byte("s3"))
  80. signingKey := sumHMAC(service, []byte("aws4_request"))
  81. return signingKey
  82. }
  83. // getSignature final signature in hexadecimal form.
  84. func getSignature(signingKey []byte, stringToSign string) string {
  85. return hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
  86. }
  87. // getScope generate a string of a specific date, an AWS region, and a
  88. // service.
  89. func getScope(location string, t time.Time) string {
  90. scope := strings.Join([]string{
  91. t.Format(yyyymmdd),
  92. location,
  93. "s3",
  94. "aws4_request",
  95. }, "/")
  96. return scope
  97. }
  98. // GetCredential generate a credential string.
  99. func GetCredential(accessKeyID, location string, t time.Time) string {
  100. scope := getScope(location, t)
  101. return accessKeyID + "/" + scope
  102. }
  103. // getHashedPayload get the hexadecimal value of the SHA256 hash of
  104. // the request payload.
  105. func getHashedPayload(req http.Request) string {
  106. hashedPayload := req.Header.Get("X-Amz-Content-Sha256")
  107. if hashedPayload == "" {
  108. // Presign does not have a payload, use S3 recommended value.
  109. hashedPayload = unsignedPayload
  110. }
  111. return hashedPayload
  112. }
  113. // getCanonicalHeaders generate a list of request headers for
  114. // signature.
  115. func getCanonicalHeaders(req http.Request, ignoredHeaders map[string]bool) string {
  116. var headers []string
  117. vals := make(map[string][]string)
  118. for k, vv := range req.Header {
  119. if _, ok := ignoredHeaders[http.CanonicalHeaderKey(k)]; ok {
  120. continue // ignored header
  121. }
  122. headers = append(headers, strings.ToLower(k))
  123. vals[strings.ToLower(k)] = vv
  124. }
  125. headers = append(headers, "host")
  126. sort.Strings(headers)
  127. var buf bytes.Buffer
  128. // Save all the headers in canonical form <header>:<value> newline
  129. // separated for each header.
  130. for _, k := range headers {
  131. buf.WriteString(k)
  132. buf.WriteByte(':')
  133. switch {
  134. case k == "host":
  135. buf.WriteString(getHostAddr(&req))
  136. fallthrough
  137. default:
  138. for idx, v := range vals[k] {
  139. if idx > 0 {
  140. buf.WriteByte(',')
  141. }
  142. buf.WriteString(signV4TrimAll(v))
  143. }
  144. buf.WriteByte('\n')
  145. }
  146. }
  147. return buf.String()
  148. }
  149. // getSignedHeaders generate all signed request headers.
  150. // i.e lexically sorted, semicolon-separated list of lowercase
  151. // request header names.
  152. func getSignedHeaders(req http.Request, ignoredHeaders map[string]bool) string {
  153. var headers []string
  154. for k := range req.Header {
  155. if _, ok := ignoredHeaders[http.CanonicalHeaderKey(k)]; ok {
  156. continue // Ignored header found continue.
  157. }
  158. headers = append(headers, strings.ToLower(k))
  159. }
  160. headers = append(headers, "host")
  161. sort.Strings(headers)
  162. return strings.Join(headers, ";")
  163. }
  164. // getCanonicalRequest generate a canonical request of style.
  165. //
  166. // canonicalRequest =
  167. // <HTTPMethod>\n
  168. // <CanonicalURI>\n
  169. // <CanonicalQueryString>\n
  170. // <CanonicalHeaders>\n
  171. // <SignedHeaders>\n
  172. // <HashedPayload>
  173. func getCanonicalRequest(req http.Request, ignoredHeaders map[string]bool) string {
  174. req.URL.RawQuery = strings.Replace(req.URL.Query().Encode(), "+", "%20", -1)
  175. canonicalRequest := strings.Join([]string{
  176. req.Method,
  177. s3utils.EncodePath(req.URL.Path),
  178. req.URL.RawQuery,
  179. getCanonicalHeaders(req, ignoredHeaders),
  180. getSignedHeaders(req, ignoredHeaders),
  181. getHashedPayload(req),
  182. }, "\n")
  183. return canonicalRequest
  184. }
  185. // getStringToSign a string based on selected query values.
  186. func getStringToSignV4(t time.Time, location, canonicalRequest string) string {
  187. stringToSign := signV4Algorithm + "\n" + t.Format(iso8601DateFormat) + "\n"
  188. stringToSign = stringToSign + getScope(location, t) + "\n"
  189. stringToSign = stringToSign + hex.EncodeToString(sum256([]byte(canonicalRequest)))
  190. return stringToSign
  191. }
  192. // PreSignV4 presign the request, in accordance with
  193. // http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html.
  194. func PreSignV4(req http.Request, accessKeyID, secretAccessKey, sessionToken, location string, expires int64) *http.Request {
  195. // Presign is not needed for anonymous credentials.
  196. if accessKeyID == "" || secretAccessKey == "" {
  197. return &req
  198. }
  199. // Initial time.
  200. t := time.Now().UTC()
  201. // Get credential string.
  202. credential := GetCredential(accessKeyID, location, t)
  203. // Get all signed headers.
  204. signedHeaders := getSignedHeaders(req, v4IgnoredHeaders)
  205. // Set URL query.
  206. query := req.URL.Query()
  207. query.Set("X-Amz-Algorithm", signV4Algorithm)
  208. query.Set("X-Amz-Date", t.Format(iso8601DateFormat))
  209. query.Set("X-Amz-Expires", strconv.FormatInt(expires, 10))
  210. query.Set("X-Amz-SignedHeaders", signedHeaders)
  211. query.Set("X-Amz-Credential", credential)
  212. // Set session token if available.
  213. if sessionToken != "" {
  214. query.Set("X-Amz-Security-Token", sessionToken)
  215. }
  216. req.URL.RawQuery = query.Encode()
  217. // Get canonical request.
  218. canonicalRequest := getCanonicalRequest(req, v4IgnoredHeaders)
  219. // Get string to sign from canonical request.
  220. stringToSign := getStringToSignV4(t, location, canonicalRequest)
  221. // Gext hmac signing key.
  222. signingKey := getSigningKey(secretAccessKey, location, t)
  223. // Calculate signature.
  224. signature := getSignature(signingKey, stringToSign)
  225. // Add signature header to RawQuery.
  226. req.URL.RawQuery += "&X-Amz-Signature=" + signature
  227. return &req
  228. }
  229. // PostPresignSignatureV4 - presigned signature for PostPolicy
  230. // requests.
  231. func PostPresignSignatureV4(policyBase64 string, t time.Time, secretAccessKey, location string) string {
  232. // Get signining key.
  233. signingkey := getSigningKey(secretAccessKey, location, t)
  234. // Calculate signature.
  235. signature := getSignature(signingkey, policyBase64)
  236. return signature
  237. }
  238. // SignV4 sign the request before Do(), in accordance with
  239. // http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html.
  240. func SignV4(req http.Request, accessKeyID, secretAccessKey, sessionToken, location string) *http.Request {
  241. // Signature calculation is not needed for anonymous credentials.
  242. if accessKeyID == "" || secretAccessKey == "" {
  243. return &req
  244. }
  245. // Initial time.
  246. t := time.Now().UTC()
  247. // Set x-amz-date.
  248. req.Header.Set("X-Amz-Date", t.Format(iso8601DateFormat))
  249. // Set session token if available.
  250. if sessionToken != "" {
  251. req.Header.Set("X-Amz-Security-Token", sessionToken)
  252. }
  253. // Get canonical request.
  254. canonicalRequest := getCanonicalRequest(req, v4IgnoredHeaders)
  255. // Get string to sign from canonical request.
  256. stringToSign := getStringToSignV4(t, location, canonicalRequest)
  257. // Get hmac signing key.
  258. signingKey := getSigningKey(secretAccessKey, location, t)
  259. // Get credential string.
  260. credential := GetCredential(accessKeyID, location, t)
  261. // Get all signed headers.
  262. signedHeaders := getSignedHeaders(req, v4IgnoredHeaders)
  263. // Calculate signature.
  264. signature := getSignature(signingKey, stringToSign)
  265. // If regular request, construct the final authorization header.
  266. parts := []string{
  267. signV4Algorithm + " Credential=" + credential,
  268. "SignedHeaders=" + signedHeaders,
  269. "Signature=" + signature,
  270. }
  271. // Set authorization header.
  272. auth := strings.Join(parts, ", ")
  273. req.Header.Set("Authorization", auth)
  274. return &req
  275. }