request-signature-v4.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 signer
  18. import (
  19. "bytes"
  20. "encoding/hex"
  21. "net/http"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "time"
  26. "github.com/minio/minio-go/v7/pkg/s3utils"
  27. )
  28. // Signature and API related constants.
  29. const (
  30. signV4Algorithm = "AWS4-HMAC-SHA256"
  31. iso8601DateFormat = "20060102T150405Z"
  32. yyyymmdd = "20060102"
  33. )
  34. // Different service types
  35. const (
  36. ServiceTypeS3 = "s3"
  37. ServiceTypeSTS = "sts"
  38. )
  39. ///
  40. /// Excerpts from @lsegal -
  41. /// https://github.com/aws/aws-sdk-js/issues/659#issuecomment-120477258.
  42. ///
  43. /// User-Agent:
  44. ///
  45. /// This is ignored from signing because signing this causes
  46. /// problems with generating pre-signed URLs (that are executed
  47. /// by other agents) or when customers pass requests through
  48. /// proxies, which may modify the user-agent.
  49. ///
  50. ///
  51. /// Authorization:
  52. ///
  53. /// Is skipped for obvious reasons
  54. ///
  55. var v4IgnoredHeaders = map[string]bool{
  56. "Authorization": true,
  57. "User-Agent": true,
  58. }
  59. // getSigningKey hmac seed to calculate final signature.
  60. func getSigningKey(secret, loc string, t time.Time, serviceType string) []byte {
  61. date := sumHMAC([]byte("AWS4"+secret), []byte(t.Format(yyyymmdd)))
  62. location := sumHMAC(date, []byte(loc))
  63. service := sumHMAC(location, []byte(serviceType))
  64. signingKey := sumHMAC(service, []byte("aws4_request"))
  65. return signingKey
  66. }
  67. // getSignature final signature in hexadecimal form.
  68. func getSignature(signingKey []byte, stringToSign string) string {
  69. return hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
  70. }
  71. // getScope generate a string of a specific date, an AWS region, and a
  72. // service.
  73. func getScope(location string, t time.Time, serviceType string) string {
  74. scope := strings.Join([]string{
  75. t.Format(yyyymmdd),
  76. location,
  77. serviceType,
  78. "aws4_request",
  79. }, "/")
  80. return scope
  81. }
  82. // GetCredential generate a credential string.
  83. func GetCredential(accessKeyID, location string, t time.Time, serviceType string) string {
  84. scope := getScope(location, t, serviceType)
  85. return accessKeyID + "/" + scope
  86. }
  87. // getHashedPayload get the hexadecimal value of the SHA256 hash of
  88. // the request payload.
  89. func getHashedPayload(req http.Request) string {
  90. hashedPayload := req.Header.Get("X-Amz-Content-Sha256")
  91. if hashedPayload == "" {
  92. // Presign does not have a payload, use S3 recommended value.
  93. hashedPayload = unsignedPayload
  94. }
  95. return hashedPayload
  96. }
  97. // getCanonicalHeaders generate a list of request headers for
  98. // signature.
  99. func getCanonicalHeaders(req http.Request, ignoredHeaders map[string]bool) string {
  100. var headers []string
  101. vals := make(map[string][]string)
  102. for k, vv := range req.Header {
  103. if _, ok := ignoredHeaders[http.CanonicalHeaderKey(k)]; ok {
  104. continue // ignored header
  105. }
  106. headers = append(headers, strings.ToLower(k))
  107. vals[strings.ToLower(k)] = vv
  108. }
  109. headers = append(headers, "host")
  110. sort.Strings(headers)
  111. var buf bytes.Buffer
  112. // Save all the headers in canonical form <header>:<value> newline
  113. // separated for each header.
  114. for _, k := range headers {
  115. buf.WriteString(k)
  116. buf.WriteByte(':')
  117. switch {
  118. case k == "host":
  119. buf.WriteString(getHostAddr(&req))
  120. fallthrough
  121. default:
  122. for idx, v := range vals[k] {
  123. if idx > 0 {
  124. buf.WriteByte(',')
  125. }
  126. buf.WriteString(signV4TrimAll(v))
  127. }
  128. buf.WriteByte('\n')
  129. }
  130. }
  131. return buf.String()
  132. }
  133. // getSignedHeaders generate all signed request headers.
  134. // i.e lexically sorted, semicolon-separated list of lowercase
  135. // request header names.
  136. func getSignedHeaders(req http.Request, ignoredHeaders map[string]bool) string {
  137. var headers []string
  138. for k := range req.Header {
  139. if _, ok := ignoredHeaders[http.CanonicalHeaderKey(k)]; ok {
  140. continue // Ignored header found continue.
  141. }
  142. headers = append(headers, strings.ToLower(k))
  143. }
  144. headers = append(headers, "host")
  145. sort.Strings(headers)
  146. return strings.Join(headers, ";")
  147. }
  148. // getCanonicalRequest generate a canonical request of style.
  149. //
  150. // canonicalRequest =
  151. // <HTTPMethod>\n
  152. // <CanonicalURI>\n
  153. // <CanonicalQueryString>\n
  154. // <CanonicalHeaders>\n
  155. // <SignedHeaders>\n
  156. // <HashedPayload>
  157. func getCanonicalRequest(req http.Request, ignoredHeaders map[string]bool, hashedPayload string) string {
  158. req.URL.RawQuery = strings.Replace(req.URL.Query().Encode(), "+", "%20", -1)
  159. canonicalRequest := strings.Join([]string{
  160. req.Method,
  161. s3utils.EncodePath(req.URL.Path),
  162. req.URL.RawQuery,
  163. getCanonicalHeaders(req, ignoredHeaders),
  164. getSignedHeaders(req, ignoredHeaders),
  165. hashedPayload,
  166. }, "\n")
  167. return canonicalRequest
  168. }
  169. // getStringToSign a string based on selected query values.
  170. func getStringToSignV4(t time.Time, location, canonicalRequest, serviceType string) string {
  171. stringToSign := signV4Algorithm + "\n" + t.Format(iso8601DateFormat) + "\n"
  172. stringToSign = stringToSign + getScope(location, t, serviceType) + "\n"
  173. stringToSign = stringToSign + hex.EncodeToString(sum256([]byte(canonicalRequest)))
  174. return stringToSign
  175. }
  176. // PreSignV4 presign the request, in accordance with
  177. // http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html.
  178. func PreSignV4(req http.Request, accessKeyID, secretAccessKey, sessionToken, location string, expires int64) *http.Request {
  179. // Presign is not needed for anonymous credentials.
  180. if accessKeyID == "" || secretAccessKey == "" {
  181. return &req
  182. }
  183. // Initial time.
  184. t := time.Now().UTC()
  185. // Get credential string.
  186. credential := GetCredential(accessKeyID, location, t, ServiceTypeS3)
  187. // Get all signed headers.
  188. signedHeaders := getSignedHeaders(req, v4IgnoredHeaders)
  189. // Set URL query.
  190. query := req.URL.Query()
  191. query.Set("X-Amz-Algorithm", signV4Algorithm)
  192. query.Set("X-Amz-Date", t.Format(iso8601DateFormat))
  193. query.Set("X-Amz-Expires", strconv.FormatInt(expires, 10))
  194. query.Set("X-Amz-SignedHeaders", signedHeaders)
  195. query.Set("X-Amz-Credential", credential)
  196. // Set session token if available.
  197. if sessionToken != "" {
  198. query.Set("X-Amz-Security-Token", sessionToken)
  199. }
  200. req.URL.RawQuery = query.Encode()
  201. // Get canonical request.
  202. canonicalRequest := getCanonicalRequest(req, v4IgnoredHeaders, getHashedPayload(req))
  203. // Get string to sign from canonical request.
  204. stringToSign := getStringToSignV4(t, location, canonicalRequest, ServiceTypeS3)
  205. // Gext hmac signing key.
  206. signingKey := getSigningKey(secretAccessKey, location, t, ServiceTypeS3)
  207. // Calculate signature.
  208. signature := getSignature(signingKey, stringToSign)
  209. // Add signature header to RawQuery.
  210. req.URL.RawQuery += "&X-Amz-Signature=" + signature
  211. return &req
  212. }
  213. // PostPresignSignatureV4 - presigned signature for PostPolicy
  214. // requests.
  215. func PostPresignSignatureV4(policyBase64 string, t time.Time, secretAccessKey, location string) string {
  216. // Get signining key.
  217. signingkey := getSigningKey(secretAccessKey, location, t, ServiceTypeS3)
  218. // Calculate signature.
  219. signature := getSignature(signingkey, policyBase64)
  220. return signature
  221. }
  222. // SignV4STS - signature v4 for STS request.
  223. func SignV4STS(req http.Request, accessKeyID, secretAccessKey, location string) *http.Request {
  224. return signV4(req, accessKeyID, secretAccessKey, "", location, ServiceTypeSTS)
  225. }
  226. // Internal function called for different service types.
  227. func signV4(req http.Request, accessKeyID, secretAccessKey, sessionToken, location, serviceType string) *http.Request {
  228. // Signature calculation is not needed for anonymous credentials.
  229. if accessKeyID == "" || secretAccessKey == "" {
  230. return &req
  231. }
  232. // Initial time.
  233. t := time.Now().UTC()
  234. // Set x-amz-date.
  235. req.Header.Set("X-Amz-Date", t.Format(iso8601DateFormat))
  236. // Set session token if available.
  237. if sessionToken != "" {
  238. req.Header.Set("X-Amz-Security-Token", sessionToken)
  239. }
  240. hashedPayload := getHashedPayload(req)
  241. if serviceType == ServiceTypeSTS {
  242. // Content sha256 header is not sent with the request
  243. // but it is expected to have sha256 of payload for signature
  244. // in STS service type request.
  245. req.Header.Del("X-Amz-Content-Sha256")
  246. }
  247. // Get canonical request.
  248. canonicalRequest := getCanonicalRequest(req, v4IgnoredHeaders, hashedPayload)
  249. // Get string to sign from canonical request.
  250. stringToSign := getStringToSignV4(t, location, canonicalRequest, serviceType)
  251. // Get hmac signing key.
  252. signingKey := getSigningKey(secretAccessKey, location, t, serviceType)
  253. // Get credential string.
  254. credential := GetCredential(accessKeyID, location, t, serviceType)
  255. // Get all signed headers.
  256. signedHeaders := getSignedHeaders(req, v4IgnoredHeaders)
  257. // Calculate signature.
  258. signature := getSignature(signingKey, stringToSign)
  259. // If regular request, construct the final authorization header.
  260. parts := []string{
  261. signV4Algorithm + " Credential=" + credential,
  262. "SignedHeaders=" + signedHeaders,
  263. "Signature=" + signature,
  264. }
  265. // Set authorization header.
  266. auth := strings.Join(parts, ", ")
  267. req.Header.Set("Authorization", auth)
  268. return &req
  269. }
  270. // SignV4 sign the request before Do(), in accordance with
  271. // http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html.
  272. func SignV4(req http.Request, accessKeyID, secretAccessKey, sessionToken, location string) *http.Request {
  273. return signV4(req, accessKeyID, secretAccessKey, sessionToken, location, ServiceTypeS3)
  274. }