You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

721 lines
22 KiB

5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
  1. /*
  2. * The following code tries to reverse engineer the Amazon S3 APIs,
  3. * and is mostly copied from minio implementation.
  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
  14. // implied. See the License for the specific language governing
  15. // permissions and limitations under the License.
  16. package s3api
  17. import (
  18. "bytes"
  19. "crypto/hmac"
  20. "crypto/sha256"
  21. "crypto/subtle"
  22. "encoding/hex"
  23. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  24. "net/http"
  25. "net/url"
  26. "regexp"
  27. "sort"
  28. "strconv"
  29. "strings"
  30. "time"
  31. "unicode/utf8"
  32. )
  33. func (iam *IdentityAccessManagement) reqSignatureV4Verify(r *http.Request) (*Identity, s3err.ErrorCode) {
  34. sha256sum := getContentSha256Cksum(r)
  35. switch {
  36. case isRequestSignatureV4(r):
  37. return iam.doesSignatureMatch(sha256sum, r)
  38. case isRequestPresignedSignatureV4(r):
  39. return iam.doesPresignedSignatureMatch(sha256sum, r)
  40. }
  41. return nil, s3err.ErrAccessDenied
  42. }
  43. // Streaming AWS Signature Version '4' constants.
  44. const (
  45. emptySHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  46. streamingContentSHA256 = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
  47. signV4ChunkedAlgorithm = "AWS4-HMAC-SHA256-PAYLOAD"
  48. // http Header "x-amz-content-sha256" == "UNSIGNED-PAYLOAD" indicates that the
  49. // client did not calculate sha256 of the payload.
  50. unsignedPayload = "UNSIGNED-PAYLOAD"
  51. )
  52. // Returns SHA256 for calculating canonical-request.
  53. func getContentSha256Cksum(r *http.Request) string {
  54. var (
  55. defaultSha256Cksum string
  56. v []string
  57. ok bool
  58. )
  59. // For a presigned request we look at the query param for sha256.
  60. if isRequestPresignedSignatureV4(r) {
  61. // X-Amz-Content-Sha256, if not set in presigned requests, checksum
  62. // will default to 'UNSIGNED-PAYLOAD'.
  63. defaultSha256Cksum = unsignedPayload
  64. v, ok = r.URL.Query()["X-Amz-Content-Sha256"]
  65. if !ok {
  66. v, ok = r.Header["X-Amz-Content-Sha256"]
  67. }
  68. } else {
  69. // X-Amz-Content-Sha256, if not set in signed requests, checksum
  70. // will default to sha256([]byte("")).
  71. defaultSha256Cksum = emptySHA256
  72. v, ok = r.Header["X-Amz-Content-Sha256"]
  73. }
  74. // We found 'X-Amz-Content-Sha256' return the captured value.
  75. if ok {
  76. return v[0]
  77. }
  78. // We couldn't find 'X-Amz-Content-Sha256'.
  79. return defaultSha256Cksum
  80. }
  81. // Verify authorization header - http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html
  82. func (iam *IdentityAccessManagement) doesSignatureMatch(hashedPayload string, r *http.Request) (*Identity, s3err.ErrorCode) {
  83. // Copy request.
  84. req := *r
  85. // Save authorization header.
  86. v4Auth := req.Header.Get("Authorization")
  87. // Parse signature version '4' header.
  88. signV4Values, err := parseSignV4(v4Auth)
  89. if err != s3err.ErrNone {
  90. return nil, err
  91. }
  92. // Extract all the signed headers along with its values.
  93. extractedSignedHeaders, errCode := extractSignedHeaders(signV4Values.SignedHeaders, r)
  94. if errCode != s3err.ErrNone {
  95. return nil, errCode
  96. }
  97. // Verify if the access key id matches.
  98. identity, cred, found := iam.lookupByAccessKey(signV4Values.Credential.accessKey)
  99. if !found {
  100. return nil, s3err.ErrInvalidAccessKeyID
  101. }
  102. // Extract date, if not present throw error.
  103. var date string
  104. if date = req.Header.Get(http.CanonicalHeaderKey("X-Amz-Date")); date == "" {
  105. if date = r.Header.Get("Date"); date == "" {
  106. return nil, s3err.ErrMissingDateHeader
  107. }
  108. }
  109. // Parse date header.
  110. t, e := time.Parse(iso8601Format, date)
  111. if e != nil {
  112. return nil, s3err.ErrMalformedDate
  113. }
  114. // Query string.
  115. queryStr := req.URL.Query().Encode()
  116. // Get canonical request.
  117. canonicalRequest := getCanonicalRequest(extractedSignedHeaders, hashedPayload, queryStr, req.URL.Path, req.Method)
  118. // Get string to sign from canonical request.
  119. stringToSign := getStringToSign(canonicalRequest, t, signV4Values.Credential.getScope())
  120. // Get hmac signing key.
  121. signingKey := getSigningKey(cred.SecretKey, signV4Values.Credential.scope.date, signV4Values.Credential.scope.region)
  122. // Calculate signature.
  123. newSignature := getSignature(signingKey, stringToSign)
  124. // Verify if signature match.
  125. if !compareSignatureV4(newSignature, signV4Values.Signature) {
  126. return nil, s3err.ErrSignatureDoesNotMatch
  127. }
  128. // Return error none.
  129. return identity, s3err.ErrNone
  130. }
  131. // credentialHeader data type represents structured form of Credential
  132. // string from authorization header.
  133. type credentialHeader struct {
  134. accessKey string
  135. scope struct {
  136. date time.Time
  137. region string
  138. service string
  139. request string
  140. }
  141. }
  142. // signValues data type represents structured form of AWS Signature V4 header.
  143. type signValues struct {
  144. Credential credentialHeader
  145. SignedHeaders []string
  146. Signature string
  147. }
  148. // Return scope string.
  149. func (c credentialHeader) getScope() string {
  150. return strings.Join([]string{
  151. c.scope.date.Format(yyyymmdd),
  152. c.scope.region,
  153. c.scope.service,
  154. c.scope.request,
  155. }, "/")
  156. }
  157. // Authorization: algorithm Credential=accessKeyID/credScope, \
  158. // SignedHeaders=signedHeaders, Signature=signature
  159. //
  160. func parseSignV4(v4Auth string) (sv signValues, aec s3err.ErrorCode) {
  161. // Replace all spaced strings, some clients can send spaced
  162. // parameters and some won't. So we pro-actively remove any spaces
  163. // to make parsing easier.
  164. v4Auth = strings.Replace(v4Auth, " ", "", -1)
  165. if v4Auth == "" {
  166. return sv, s3err.ErrAuthHeaderEmpty
  167. }
  168. // Verify if the header algorithm is supported or not.
  169. if !strings.HasPrefix(v4Auth, signV4Algorithm) {
  170. return sv, s3err.ErrSignatureVersionNotSupported
  171. }
  172. // Strip off the Algorithm prefix.
  173. v4Auth = strings.TrimPrefix(v4Auth, signV4Algorithm)
  174. authFields := strings.Split(strings.TrimSpace(v4Auth), ",")
  175. if len(authFields) != 3 {
  176. return sv, s3err.ErrMissingFields
  177. }
  178. // Initialize signature version '4' structured header.
  179. signV4Values := signValues{}
  180. var err s3err.ErrorCode
  181. // Save credentail values.
  182. signV4Values.Credential, err = parseCredentialHeader(authFields[0])
  183. if err != s3err.ErrNone {
  184. return sv, err
  185. }
  186. // Save signed headers.
  187. signV4Values.SignedHeaders, err = parseSignedHeader(authFields[1])
  188. if err != s3err.ErrNone {
  189. return sv, err
  190. }
  191. // Save signature.
  192. signV4Values.Signature, err = parseSignature(authFields[2])
  193. if err != s3err.ErrNone {
  194. return sv, err
  195. }
  196. // Return the structure here.
  197. return signV4Values, s3err.ErrNone
  198. }
  199. // parse credentialHeader string into its structured form.
  200. func parseCredentialHeader(credElement string) (ch credentialHeader, aec s3err.ErrorCode) {
  201. creds := strings.Split(strings.TrimSpace(credElement), "=")
  202. if len(creds) != 2 {
  203. return ch, s3err.ErrMissingFields
  204. }
  205. if creds[0] != "Credential" {
  206. return ch, s3err.ErrMissingCredTag
  207. }
  208. credElements := strings.Split(strings.TrimSpace(creds[1]), "/")
  209. if len(credElements) != 5 {
  210. return ch, s3err.ErrCredMalformed
  211. }
  212. // Save access key id.
  213. cred := credentialHeader{
  214. accessKey: credElements[0],
  215. }
  216. var e error
  217. cred.scope.date, e = time.Parse(yyyymmdd, credElements[1])
  218. if e != nil {
  219. return ch, s3err.ErrMalformedCredentialDate
  220. }
  221. cred.scope.region = credElements[2]
  222. cred.scope.service = credElements[3] // "s3"
  223. cred.scope.request = credElements[4] // "aws4_request"
  224. return cred, s3err.ErrNone
  225. }
  226. // Parse slice of signed headers from signed headers tag.
  227. func parseSignedHeader(signedHdrElement string) ([]string, s3err.ErrorCode) {
  228. signedHdrFields := strings.Split(strings.TrimSpace(signedHdrElement), "=")
  229. if len(signedHdrFields) != 2 {
  230. return nil, s3err.ErrMissingFields
  231. }
  232. if signedHdrFields[0] != "SignedHeaders" {
  233. return nil, s3err.ErrMissingSignHeadersTag
  234. }
  235. if signedHdrFields[1] == "" {
  236. return nil, s3err.ErrMissingFields
  237. }
  238. signedHeaders := strings.Split(signedHdrFields[1], ";")
  239. return signedHeaders, s3err.ErrNone
  240. }
  241. // Parse signature from signature tag.
  242. func parseSignature(signElement string) (string, s3err.ErrorCode) {
  243. signFields := strings.Split(strings.TrimSpace(signElement), "=")
  244. if len(signFields) != 2 {
  245. return "", s3err.ErrMissingFields
  246. }
  247. if signFields[0] != "Signature" {
  248. return "", s3err.ErrMissingSignTag
  249. }
  250. if signFields[1] == "" {
  251. return "", s3err.ErrMissingFields
  252. }
  253. signature := signFields[1]
  254. return signature, s3err.ErrNone
  255. }
  256. // check query headers with presigned signature
  257. // - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
  258. func (iam *IdentityAccessManagement) doesPresignedSignatureMatch(hashedPayload string, r *http.Request) (*Identity, s3err.ErrorCode) {
  259. // Copy request
  260. req := *r
  261. // Parse request query string.
  262. pSignValues, err := parsePreSignV4(req.URL.Query())
  263. if err != s3err.ErrNone {
  264. return nil, err
  265. }
  266. // Verify if the access key id matches.
  267. identity, cred, found := iam.lookupByAccessKey(pSignValues.Credential.accessKey)
  268. if !found {
  269. return nil, s3err.ErrInvalidAccessKeyID
  270. }
  271. // Extract all the signed headers along with its values.
  272. extractedSignedHeaders, errCode := extractSignedHeaders(pSignValues.SignedHeaders, r)
  273. if errCode != s3err.ErrNone {
  274. return nil, errCode
  275. }
  276. // Construct new query.
  277. query := make(url.Values)
  278. if req.URL.Query().Get("X-Amz-Content-Sha256") != "" {
  279. query.Set("X-Amz-Content-Sha256", hashedPayload)
  280. }
  281. query.Set("X-Amz-Algorithm", signV4Algorithm)
  282. now := time.Now().UTC()
  283. // If the host which signed the request is slightly ahead in time (by less than globalMaxSkewTime) the
  284. // request should still be allowed.
  285. if pSignValues.Date.After(now.Add(15 * time.Minute)) {
  286. return nil, s3err.ErrRequestNotReadyYet
  287. }
  288. if now.Sub(pSignValues.Date) > pSignValues.Expires {
  289. return nil, s3err.ErrExpiredPresignRequest
  290. }
  291. // Save the date and expires.
  292. t := pSignValues.Date
  293. expireSeconds := int(pSignValues.Expires / time.Second)
  294. // Construct the query.
  295. query.Set("X-Amz-Date", t.Format(iso8601Format))
  296. query.Set("X-Amz-Expires", strconv.Itoa(expireSeconds))
  297. query.Set("X-Amz-SignedHeaders", getSignedHeaders(extractedSignedHeaders))
  298. query.Set("X-Amz-Credential", cred.AccessKey+"/"+getScope(t, pSignValues.Credential.scope.region))
  299. // Save other headers available in the request parameters.
  300. for k, v := range req.URL.Query() {
  301. // Handle the metadata in presigned put query string
  302. if strings.Contains(strings.ToLower(k), "x-amz-meta-") {
  303. query.Set(k, v[0])
  304. }
  305. if strings.HasPrefix(strings.ToLower(k), "x-amz") {
  306. continue
  307. }
  308. query[k] = v
  309. }
  310. // Get the encoded query.
  311. encodedQuery := query.Encode()
  312. // Verify if date query is same.
  313. if req.URL.Query().Get("X-Amz-Date") != query.Get("X-Amz-Date") {
  314. return nil, s3err.ErrSignatureDoesNotMatch
  315. }
  316. // Verify if expires query is same.
  317. if req.URL.Query().Get("X-Amz-Expires") != query.Get("X-Amz-Expires") {
  318. return nil, s3err.ErrSignatureDoesNotMatch
  319. }
  320. // Verify if signed headers query is same.
  321. if req.URL.Query().Get("X-Amz-SignedHeaders") != query.Get("X-Amz-SignedHeaders") {
  322. return nil, s3err.ErrSignatureDoesNotMatch
  323. }
  324. // Verify if credential query is same.
  325. if req.URL.Query().Get("X-Amz-Credential") != query.Get("X-Amz-Credential") {
  326. return nil, s3err.ErrSignatureDoesNotMatch
  327. }
  328. // Verify if sha256 payload query is same.
  329. if req.URL.Query().Get("X-Amz-Content-Sha256") != "" {
  330. if req.URL.Query().Get("X-Amz-Content-Sha256") != query.Get("X-Amz-Content-Sha256") {
  331. return nil, s3err.ErrContentSHA256Mismatch
  332. }
  333. }
  334. /// Verify finally if signature is same.
  335. // Get canonical request.
  336. presignedCanonicalReq := getCanonicalRequest(extractedSignedHeaders, hashedPayload, encodedQuery, req.URL.Path, req.Method)
  337. // Get string to sign from canonical request.
  338. presignedStringToSign := getStringToSign(presignedCanonicalReq, t, pSignValues.Credential.getScope())
  339. // Get hmac presigned signing key.
  340. presignedSigningKey := getSigningKey(cred.SecretKey, pSignValues.Credential.scope.date, pSignValues.Credential.scope.region)
  341. // Get new signature.
  342. newSignature := getSignature(presignedSigningKey, presignedStringToSign)
  343. // Verify signature.
  344. if !compareSignatureV4(req.URL.Query().Get("X-Amz-Signature"), newSignature) {
  345. return nil, s3err.ErrSignatureDoesNotMatch
  346. }
  347. return identity, s3err.ErrNone
  348. }
  349. func contains(list []string, elem string) bool {
  350. for _, t := range list {
  351. if t == elem {
  352. return true
  353. }
  354. }
  355. return false
  356. }
  357. // preSignValues data type represents structued form of AWS Signature V4 query string.
  358. type preSignValues struct {
  359. signValues
  360. Date time.Time
  361. Expires time.Duration
  362. }
  363. // Parses signature version '4' query string of the following form.
  364. //
  365. // querystring = X-Amz-Algorithm=algorithm
  366. // querystring += &X-Amz-Credential= urlencode(accessKey + '/' + credential_scope)
  367. // querystring += &X-Amz-Date=date
  368. // querystring += &X-Amz-Expires=timeout interval
  369. // querystring += &X-Amz-SignedHeaders=signed_headers
  370. // querystring += &X-Amz-Signature=signature
  371. //
  372. // verifies if any of the necessary query params are missing in the presigned request.
  373. func doesV4PresignParamsExist(query url.Values) s3err.ErrorCode {
  374. v4PresignQueryParams := []string{"X-Amz-Algorithm", "X-Amz-Credential", "X-Amz-Signature", "X-Amz-Date", "X-Amz-SignedHeaders", "X-Amz-Expires"}
  375. for _, v4PresignQueryParam := range v4PresignQueryParams {
  376. if _, ok := query[v4PresignQueryParam]; !ok {
  377. return s3err.ErrInvalidQueryParams
  378. }
  379. }
  380. return s3err.ErrNone
  381. }
  382. // Parses all the presigned signature values into separate elements.
  383. func parsePreSignV4(query url.Values) (psv preSignValues, aec s3err.ErrorCode) {
  384. var err s3err.ErrorCode
  385. // verify whether the required query params exist.
  386. err = doesV4PresignParamsExist(query)
  387. if err != s3err.ErrNone {
  388. return psv, err
  389. }
  390. // Verify if the query algorithm is supported or not.
  391. if query.Get("X-Amz-Algorithm") != signV4Algorithm {
  392. return psv, s3err.ErrInvalidQuerySignatureAlgo
  393. }
  394. // Initialize signature version '4' structured header.
  395. preSignV4Values := preSignValues{}
  396. // Save credential.
  397. preSignV4Values.Credential, err = parseCredentialHeader("Credential=" + query.Get("X-Amz-Credential"))
  398. if err != s3err.ErrNone {
  399. return psv, err
  400. }
  401. var e error
  402. // Save date in native time.Time.
  403. preSignV4Values.Date, e = time.Parse(iso8601Format, query.Get("X-Amz-Date"))
  404. if e != nil {
  405. return psv, s3err.ErrMalformedPresignedDate
  406. }
  407. // Save expires in native time.Duration.
  408. preSignV4Values.Expires, e = time.ParseDuration(query.Get("X-Amz-Expires") + "s")
  409. if e != nil {
  410. return psv, s3err.ErrMalformedExpires
  411. }
  412. if preSignV4Values.Expires < 0 {
  413. return psv, s3err.ErrNegativeExpires
  414. }
  415. // Check if Expiry time is less than 7 days (value in seconds).
  416. if preSignV4Values.Expires.Seconds() > 604800 {
  417. return psv, s3err.ErrMaximumExpires
  418. }
  419. // Save signed headers.
  420. preSignV4Values.SignedHeaders, err = parseSignedHeader("SignedHeaders=" + query.Get("X-Amz-SignedHeaders"))
  421. if err != s3err.ErrNone {
  422. return psv, err
  423. }
  424. // Save signature.
  425. preSignV4Values.Signature, err = parseSignature("Signature=" + query.Get("X-Amz-Signature"))
  426. if err != s3err.ErrNone {
  427. return psv, err
  428. }
  429. // Return structed form of signature query string.
  430. return preSignV4Values, s3err.ErrNone
  431. }
  432. // extractSignedHeaders extract signed headers from Authorization header
  433. func extractSignedHeaders(signedHeaders []string, r *http.Request) (http.Header, s3err.ErrorCode) {
  434. reqHeaders := r.Header
  435. // find whether "host" is part of list of signed headers.
  436. // if not return ErrUnsignedHeaders. "host" is mandatory.
  437. if !contains(signedHeaders, "host") {
  438. return nil, s3err.ErrUnsignedHeaders
  439. }
  440. extractedSignedHeaders := make(http.Header)
  441. for _, header := range signedHeaders {
  442. // `host` will not be found in the headers, can be found in r.Host.
  443. // but its alway necessary that the list of signed headers containing host in it.
  444. val, ok := reqHeaders[http.CanonicalHeaderKey(header)]
  445. if ok {
  446. for _, enc := range val {
  447. extractedSignedHeaders.Add(header, enc)
  448. }
  449. continue
  450. }
  451. switch header {
  452. case "expect":
  453. // Golang http server strips off 'Expect' header, if the
  454. // client sent this as part of signed headers we need to
  455. // handle otherwise we would see a signature mismatch.
  456. // `aws-cli` sets this as part of signed headers.
  457. //
  458. // According to
  459. // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.20
  460. // Expect header is always of form:
  461. //
  462. // Expect = "Expect" ":" 1#expectation
  463. // expectation = "100-continue" | expectation-extension
  464. //
  465. // So it safe to assume that '100-continue' is what would
  466. // be sent, for the time being keep this work around.
  467. // Adding a *TODO* to remove this later when Golang server
  468. // doesn't filter out the 'Expect' header.
  469. extractedSignedHeaders.Set(header, "100-continue")
  470. case "host":
  471. // Go http server removes "host" from Request.Header
  472. extractedSignedHeaders.Set(header, r.Host)
  473. case "transfer-encoding":
  474. for _, enc := range r.TransferEncoding {
  475. extractedSignedHeaders.Add(header, enc)
  476. }
  477. case "content-length":
  478. // Signature-V4 spec excludes Content-Length from signed headers list for signature calculation.
  479. // But some clients deviate from this rule. Hence we consider Content-Length for signature
  480. // calculation to be compatible with such clients.
  481. extractedSignedHeaders.Set(header, strconv.FormatInt(r.ContentLength, 10))
  482. default:
  483. return nil, s3err.ErrUnsignedHeaders
  484. }
  485. }
  486. return extractedSignedHeaders, s3err.ErrNone
  487. }
  488. // getSignedHeaders generate a string i.e alphabetically sorted, semicolon-separated list of lowercase request header names
  489. func getSignedHeaders(signedHeaders http.Header) string {
  490. var headers []string
  491. for k := range signedHeaders {
  492. headers = append(headers, strings.ToLower(k))
  493. }
  494. sort.Strings(headers)
  495. return strings.Join(headers, ";")
  496. }
  497. // getScope generate a string of a specific date, an AWS region, and a service.
  498. func getScope(t time.Time, region string) string {
  499. scope := strings.Join([]string{
  500. t.Format(yyyymmdd),
  501. region,
  502. "s3",
  503. "aws4_request",
  504. }, "/")
  505. return scope
  506. }
  507. // getCanonicalRequest generate a canonical request of style
  508. //
  509. // canonicalRequest =
  510. // <HTTPMethod>\n
  511. // <CanonicalURI>\n
  512. // <CanonicalQueryString>\n
  513. // <CanonicalHeaders>\n
  514. // <SignedHeaders>\n
  515. // <HashedPayload>
  516. //
  517. func getCanonicalRequest(extractedSignedHeaders http.Header, payload, queryStr, urlPath, method string) string {
  518. rawQuery := strings.Replace(queryStr, "+", "%20", -1)
  519. encodedPath := encodePath(urlPath)
  520. canonicalRequest := strings.Join([]string{
  521. method,
  522. encodedPath,
  523. rawQuery,
  524. getCanonicalHeaders(extractedSignedHeaders),
  525. getSignedHeaders(extractedSignedHeaders),
  526. payload,
  527. }, "\n")
  528. return canonicalRequest
  529. }
  530. // getStringToSign a string based on selected query values.
  531. func getStringToSign(canonicalRequest string, t time.Time, scope string) string {
  532. stringToSign := signV4Algorithm + "\n" + t.Format(iso8601Format) + "\n"
  533. stringToSign = stringToSign + scope + "\n"
  534. canonicalRequestBytes := sha256.Sum256([]byte(canonicalRequest))
  535. stringToSign = stringToSign + hex.EncodeToString(canonicalRequestBytes[:])
  536. return stringToSign
  537. }
  538. // sumHMAC calculate hmac between two input byte array.
  539. func sumHMAC(key []byte, data []byte) []byte {
  540. hash := hmac.New(sha256.New, key)
  541. hash.Write(data)
  542. return hash.Sum(nil)
  543. }
  544. // getSigningKey hmac seed to calculate final signature.
  545. func getSigningKey(secretKey string, t time.Time, region string) []byte {
  546. date := sumHMAC([]byte("AWS4"+secretKey), []byte(t.Format(yyyymmdd)))
  547. regionBytes := sumHMAC(date, []byte(region))
  548. service := sumHMAC(regionBytes, []byte("s3"))
  549. signingKey := sumHMAC(service, []byte("aws4_request"))
  550. return signingKey
  551. }
  552. // getSignature final signature in hexadecimal form.
  553. func getSignature(signingKey []byte, stringToSign string) string {
  554. return hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
  555. }
  556. // getCanonicalHeaders generate a list of request headers with their values
  557. func getCanonicalHeaders(signedHeaders http.Header) string {
  558. var headers []string
  559. vals := make(http.Header)
  560. for k, vv := range signedHeaders {
  561. headers = append(headers, strings.ToLower(k))
  562. vals[strings.ToLower(k)] = vv
  563. }
  564. sort.Strings(headers)
  565. var buf bytes.Buffer
  566. for _, k := range headers {
  567. buf.WriteString(k)
  568. buf.WriteByte(':')
  569. for idx, v := range vals[k] {
  570. if idx > 0 {
  571. buf.WriteByte(',')
  572. }
  573. buf.WriteString(signV4TrimAll(v))
  574. }
  575. buf.WriteByte('\n')
  576. }
  577. return buf.String()
  578. }
  579. // Trim leading and trailing spaces and replace sequential spaces with one space, following Trimall()
  580. // in http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
  581. func signV4TrimAll(input string) string {
  582. // Compress adjacent spaces (a space is determined by
  583. // unicode.IsSpace() internally here) to one space and return
  584. return strings.Join(strings.Fields(input), " ")
  585. }
  586. // if object matches reserved string, no need to encode them
  587. var reservedObjectNames = regexp.MustCompile("^[a-zA-Z0-9-_.~/]+$")
  588. // EncodePath encode the strings from UTF-8 byte representations to HTML hex escape sequences
  589. //
  590. // This is necessary since regular url.Parse() and url.Encode() functions do not support UTF-8
  591. // non english characters cannot be parsed due to the nature in which url.Encode() is written
  592. //
  593. // This function on the other hand is a direct replacement for url.Encode() technique to support
  594. // pretty much every UTF-8 character.
  595. func encodePath(pathName string) string {
  596. if reservedObjectNames.MatchString(pathName) {
  597. return pathName
  598. }
  599. var encodedPathname string
  600. for _, s := range pathName {
  601. if 'A' <= s && s <= 'Z' || 'a' <= s && s <= 'z' || '0' <= s && s <= '9' { // §2.3 Unreserved characters (mark)
  602. encodedPathname = encodedPathname + string(s)
  603. continue
  604. }
  605. switch s {
  606. case '-', '_', '.', '~', '/': // §2.3 Unreserved characters (mark)
  607. encodedPathname = encodedPathname + string(s)
  608. continue
  609. default:
  610. len := utf8.RuneLen(s)
  611. if len < 0 {
  612. // if utf8 cannot convert return the same string as is
  613. return pathName
  614. }
  615. u := make([]byte, len)
  616. utf8.EncodeRune(u, s)
  617. for _, r := range u {
  618. hex := hex.EncodeToString([]byte{r})
  619. encodedPathname = encodedPathname + "%" + strings.ToUpper(hex)
  620. }
  621. }
  622. }
  623. return encodedPathname
  624. }
  625. // compareSignatureV4 returns true if and only if both signatures
  626. // are equal. The signatures are expected to be HEX encoded strings
  627. // according to the AWS S3 signature V4 spec.
  628. func compareSignatureV4(sig1, sig2 string) bool {
  629. // The CTC using []byte(str) works because the hex encoding
  630. // is unique for a sequence of bytes. See also compareSignatureV2.
  631. return subtle.ConstantTimeCompare([]byte(sig1), []byte(sig2)) == 1
  632. }