auth_signature_v4.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  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. "io"
  24. "net/http"
  25. "path"
  26. "regexp"
  27. "sort"
  28. "strconv"
  29. "strings"
  30. "time"
  31. "unicode/utf8"
  32. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  33. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  34. )
  35. func (iam *IdentityAccessManagement) reqSignatureV4Verify(r *http.Request) (*Identity, s3err.ErrorCode) {
  36. sha256sum := getContentSha256Cksum(r)
  37. switch {
  38. case isRequestSignatureV4(r):
  39. return iam.doesSignatureMatch(sha256sum, r)
  40. case isRequestPresignedSignatureV4(r):
  41. return iam.doesPresignedSignatureMatch(sha256sum, r)
  42. }
  43. return nil, s3err.ErrAccessDenied
  44. }
  45. // Constants specific to this file
  46. const (
  47. emptySHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  48. streamingContentSHA256 = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
  49. streamingUnsignedPayload = "STREAMING-UNSIGNED-PAYLOAD-TRAILER"
  50. unsignedPayload = "UNSIGNED-PAYLOAD"
  51. // Limit for IAM/STS request body size to prevent DoS attacks
  52. iamRequestBodyLimit = 10 * (1 << 20) // 10 MiB
  53. )
  54. // streamHashRequestBody computes SHA256 hash incrementally while preserving the body.
  55. func streamHashRequestBody(r *http.Request, sizeLimit int64) (string, error) {
  56. if r.Body == nil {
  57. return emptySHA256, nil
  58. }
  59. limitedReader := io.LimitReader(r.Body, sizeLimit)
  60. hasher := sha256.New()
  61. var bodyBuffer bytes.Buffer
  62. // Use io.Copy with an io.MultiWriter to hash and buffer the body simultaneously.
  63. if _, err := io.Copy(io.MultiWriter(hasher, &bodyBuffer), limitedReader); err != nil {
  64. return "", err
  65. }
  66. r.Body = io.NopCloser(&bodyBuffer)
  67. if bodyBuffer.Len() == 0 {
  68. return emptySHA256, nil
  69. }
  70. return hex.EncodeToString(hasher.Sum(nil)), nil
  71. }
  72. // getContentSha256Cksum retrieves the "x-amz-content-sha256" header value.
  73. func getContentSha256Cksum(r *http.Request) string {
  74. // If the client sends a SHA256 checksum of the object in this header, use it.
  75. if v := r.Header.Get("X-Amz-Content-Sha256"); v != "" {
  76. return v
  77. }
  78. // For a presigned request we look at the query param for sha256.
  79. if isRequestPresignedSignatureV4(r) {
  80. // X-Amz-Content-Sha256 header value is optional for presigned requests.
  81. return unsignedPayload
  82. }
  83. // X-Amz-Content-Sha256 header value is required for all non-presigned requests.
  84. return emptySHA256
  85. }
  86. // signValues data type represents structured form of AWS Signature V4 header.
  87. type signValues struct {
  88. Credential credentialHeader
  89. SignedHeaders []string
  90. Signature string
  91. }
  92. // parseSignV4 parses the authorization header for signature v4.
  93. func parseSignV4(v4Auth string) (sv signValues, aec s3err.ErrorCode) {
  94. // Replace all spaced strings, some clients can send spaced
  95. // parameters and some won't. So we pro-actively remove any spaces
  96. // to make parsing easier.
  97. v4Auth = strings.Replace(v4Auth, " ", "", -1)
  98. if v4Auth == "" {
  99. return sv, s3err.ErrAuthHeaderEmpty
  100. }
  101. // Verify if the header algorithm is supported or not.
  102. if !strings.HasPrefix(v4Auth, signV4Algorithm) {
  103. return sv, s3err.ErrSignatureVersionNotSupported
  104. }
  105. // Strip off the Algorithm prefix.
  106. v4Auth = strings.TrimPrefix(v4Auth, signV4Algorithm)
  107. authFields := strings.Split(strings.TrimSpace(v4Auth), ",")
  108. if len(authFields) != 3 {
  109. return sv, s3err.ErrMissingFields
  110. }
  111. // Initialize signature version '4' structured header.
  112. signV4Values := signValues{}
  113. var err s3err.ErrorCode
  114. // Save credential values.
  115. signV4Values.Credential, err = parseCredentialHeader(authFields[0])
  116. if err != s3err.ErrNone {
  117. return sv, err
  118. }
  119. // Save signed headers.
  120. signV4Values.SignedHeaders, err = parseSignedHeader(authFields[1])
  121. if err != s3err.ErrNone {
  122. return sv, err
  123. }
  124. // Save signature.
  125. signV4Values.Signature, err = parseSignature(authFields[2])
  126. if err != s3err.ErrNone {
  127. return sv, err
  128. }
  129. // Return the structure here.
  130. return signV4Values, s3err.ErrNone
  131. }
  132. // doesSignatureMatch verifies the request signature.
  133. func (iam *IdentityAccessManagement) doesSignatureMatch(hashedPayload string, r *http.Request) (*Identity, s3err.ErrorCode) {
  134. // Copy request
  135. req := *r
  136. // Save authorization header.
  137. v4Auth := req.Header.Get("Authorization")
  138. // Parse signature version '4' header.
  139. signV4Values, errCode := parseSignV4(v4Auth)
  140. if errCode != s3err.ErrNone {
  141. return nil, errCode
  142. }
  143. // Compute payload hash for non-S3 services
  144. if signV4Values.Credential.scope.service != "s3" && hashedPayload == emptySHA256 && r.Body != nil {
  145. var err error
  146. hashedPayload, err = streamHashRequestBody(r, iamRequestBodyLimit)
  147. if err != nil {
  148. return nil, s3err.ErrInternalError
  149. }
  150. }
  151. // Extract all the signed headers along with its values.
  152. extractedSignedHeaders, errCode := extractSignedHeaders(signV4Values.SignedHeaders, r)
  153. if errCode != s3err.ErrNone {
  154. return nil, errCode
  155. }
  156. cred := signV4Values.Credential
  157. identity, foundCred, found := iam.lookupByAccessKey(cred.accessKey)
  158. if !found {
  159. return nil, s3err.ErrInvalidAccessKeyID
  160. }
  161. bucket, object := s3_constants.GetBucketAndObject(r)
  162. canDoResult := identity.canDo(s3_constants.ACTION_WRITE, bucket, object)
  163. if !canDoResult {
  164. return nil, s3err.ErrAccessDenied
  165. }
  166. // Extract date, if not present throw error.
  167. var dateStr string
  168. if dateStr = req.Header.Get("x-amz-date"); dateStr == "" {
  169. if dateStr = r.Header.Get("Date"); dateStr == "" {
  170. return nil, s3err.ErrMissingDateHeader
  171. }
  172. }
  173. // Parse date header.
  174. t, e := time.Parse(iso8601Format, dateStr)
  175. if e != nil {
  176. return nil, s3err.ErrMalformedDate
  177. }
  178. // Query string.
  179. queryStr := req.URL.Query().Encode()
  180. // Check if reverse proxy is forwarding with prefix
  181. if forwardedPrefix := r.Header.Get("X-Forwarded-Prefix"); forwardedPrefix != "" {
  182. // Try signature verification with the forwarded prefix first.
  183. // This handles cases where reverse proxies strip URL prefixes and add the X-Forwarded-Prefix header.
  184. cleanedPath := buildPathWithForwardedPrefix(forwardedPrefix, req.URL.Path)
  185. errCode = iam.verifySignatureWithPath(extractedSignedHeaders, hashedPayload, queryStr, cleanedPath, req.Method, foundCred.SecretKey, t, signV4Values)
  186. if errCode == s3err.ErrNone {
  187. return identity, errCode
  188. }
  189. }
  190. // Try normal signature verification (without prefix)
  191. errCode = iam.verifySignatureWithPath(extractedSignedHeaders, hashedPayload, queryStr, req.URL.Path, req.Method, foundCred.SecretKey, t, signV4Values)
  192. if errCode == s3err.ErrNone {
  193. return identity, errCode
  194. }
  195. return nil, errCode
  196. }
  197. // buildPathWithForwardedPrefix combines forwarded prefix with URL path while preserving trailing slashes.
  198. // This ensures compatibility with S3 SDK signatures that include trailing slashes for directory operations.
  199. func buildPathWithForwardedPrefix(forwardedPrefix, urlPath string) string {
  200. fullPath := forwardedPrefix + urlPath
  201. hasTrailingSlash := strings.HasSuffix(urlPath, "/") && urlPath != "/"
  202. cleanedPath := path.Clean(fullPath)
  203. if hasTrailingSlash && !strings.HasSuffix(cleanedPath, "/") {
  204. cleanedPath += "/"
  205. }
  206. return cleanedPath
  207. }
  208. // verifySignatureWithPath verifies signature with a given path (used for both normal and prefixed paths).
  209. func (iam *IdentityAccessManagement) verifySignatureWithPath(extractedSignedHeaders http.Header, hashedPayload, queryStr, urlPath, method, secretKey string, t time.Time, signV4Values signValues) s3err.ErrorCode {
  210. // Get canonical request.
  211. canonicalRequest := getCanonicalRequest(extractedSignedHeaders, hashedPayload, queryStr, urlPath, method)
  212. // Get string to sign from canonical request.
  213. stringToSign := getStringToSign(canonicalRequest, t, signV4Values.Credential.getScope())
  214. // Get hmac signing key.
  215. signingKey := getSigningKey(secretKey, signV4Values.Credential.scope.date.Format(yyyymmdd), signV4Values.Credential.scope.region, signV4Values.Credential.scope.service)
  216. // Calculate signature.
  217. newSignature := getSignature(signingKey, stringToSign)
  218. // Verify if signature match.
  219. if !compareSignatureV4(newSignature, signV4Values.Signature) {
  220. return s3err.ErrSignatureDoesNotMatch
  221. }
  222. return s3err.ErrNone
  223. }
  224. // verifyPresignedSignatureWithPath verifies presigned signature with a given path (used for both normal and prefixed paths).
  225. func (iam *IdentityAccessManagement) verifyPresignedSignatureWithPath(extractedSignedHeaders http.Header, hashedPayload, queryStr, urlPath, method, secretKey string, t time.Time, credHeader credentialHeader, signature string) s3err.ErrorCode {
  226. // Get canonical request.
  227. canonicalRequest := getCanonicalRequest(extractedSignedHeaders, hashedPayload, queryStr, urlPath, method)
  228. // Get string to sign from canonical request.
  229. stringToSign := getStringToSign(canonicalRequest, t, credHeader.getScope())
  230. // Get hmac signing key.
  231. signingKey := getSigningKey(secretKey, credHeader.scope.date.Format(yyyymmdd), credHeader.scope.region, credHeader.scope.service)
  232. // Calculate expected signature.
  233. expectedSignature := getSignature(signingKey, stringToSign)
  234. // Verify if signature match.
  235. if !compareSignatureV4(expectedSignature, signature) {
  236. return s3err.ErrSignatureDoesNotMatch
  237. }
  238. return s3err.ErrNone
  239. }
  240. // Simple implementation for presigned signature verification
  241. func (iam *IdentityAccessManagement) doesPresignedSignatureMatch(hashedPayload string, r *http.Request) (*Identity, s3err.ErrorCode) {
  242. // Parse presigned signature values from query parameters
  243. query := r.URL.Query()
  244. // Check required parameters
  245. algorithm := query.Get("X-Amz-Algorithm")
  246. if algorithm != signV4Algorithm {
  247. return nil, s3err.ErrSignatureVersionNotSupported
  248. }
  249. credential := query.Get("X-Amz-Credential")
  250. if credential == "" {
  251. return nil, s3err.ErrMissingFields
  252. }
  253. signature := query.Get("X-Amz-Signature")
  254. if signature == "" {
  255. return nil, s3err.ErrMissingFields
  256. }
  257. signedHeadersStr := query.Get("X-Amz-SignedHeaders")
  258. if signedHeadersStr == "" {
  259. return nil, s3err.ErrMissingFields
  260. }
  261. dateStr := query.Get("X-Amz-Date")
  262. if dateStr == "" {
  263. return nil, s3err.ErrMissingDateHeader
  264. }
  265. // Parse credential
  266. credHeader, err := parseCredentialHeader("Credential=" + credential)
  267. if err != s3err.ErrNone {
  268. return nil, err
  269. }
  270. // Look up identity by access key
  271. identity, foundCred, found := iam.lookupByAccessKey(credHeader.accessKey)
  272. if !found {
  273. return nil, s3err.ErrInvalidAccessKeyID
  274. }
  275. // Check permissions
  276. bucket, object := s3_constants.GetBucketAndObject(r)
  277. if !identity.canDo(s3_constants.ACTION_READ, bucket, object) {
  278. return nil, s3err.ErrAccessDenied
  279. }
  280. // Parse date
  281. t, e := time.Parse(iso8601Format, dateStr)
  282. if e != nil {
  283. return nil, s3err.ErrMalformedDate
  284. }
  285. // Check expiration
  286. expiresStr := query.Get("X-Amz-Expires")
  287. if expiresStr != "" {
  288. expires, parseErr := strconv.ParseInt(expiresStr, 10, 64)
  289. if parseErr != nil {
  290. return nil, s3err.ErrMalformedDate
  291. }
  292. // Check if current time is after the expiration time
  293. expirationTime := t.Add(time.Duration(expires) * time.Second)
  294. if time.Now().UTC().After(expirationTime) {
  295. return nil, s3err.ErrExpiredPresignRequest
  296. }
  297. }
  298. // Parse signed headers
  299. signedHeaders := strings.Split(signedHeadersStr, ";")
  300. // Extract signed headers from request
  301. extractedSignedHeaders := make(http.Header)
  302. for _, header := range signedHeaders {
  303. if header == "host" {
  304. extractedSignedHeaders[header] = []string{extractHostHeader(r)}
  305. continue
  306. }
  307. if values := r.Header[http.CanonicalHeaderKey(header)]; len(values) > 0 {
  308. extractedSignedHeaders[http.CanonicalHeaderKey(header)] = values
  309. }
  310. }
  311. // Remove signature from query for canonical request calculation
  312. queryForCanonical := r.URL.Query()
  313. queryForCanonical.Del("X-Amz-Signature")
  314. queryStr := strings.Replace(queryForCanonical.Encode(), "+", "%20", -1)
  315. var errCode s3err.ErrorCode
  316. // Check if reverse proxy is forwarding with prefix for presigned URLs
  317. if forwardedPrefix := r.Header.Get("X-Forwarded-Prefix"); forwardedPrefix != "" {
  318. // Try signature verification with the forwarded prefix first.
  319. // This handles cases where reverse proxies strip URL prefixes and add the X-Forwarded-Prefix header.
  320. cleanedPath := buildPathWithForwardedPrefix(forwardedPrefix, r.URL.Path)
  321. errCode = iam.verifyPresignedSignatureWithPath(extractedSignedHeaders, hashedPayload, queryStr, cleanedPath, r.Method, foundCred.SecretKey, t, credHeader, signature)
  322. if errCode == s3err.ErrNone {
  323. return identity, errCode
  324. }
  325. }
  326. // Try normal signature verification (without prefix)
  327. errCode = iam.verifyPresignedSignatureWithPath(extractedSignedHeaders, hashedPayload, queryStr, r.URL.Path, r.Method, foundCred.SecretKey, t, credHeader, signature)
  328. if errCode == s3err.ErrNone {
  329. return identity, errCode
  330. }
  331. return nil, errCode
  332. }
  333. // credentialHeader data type represents structured form of Credential
  334. // string from authorization header.
  335. type credentialHeader struct {
  336. accessKey string
  337. scope struct {
  338. date time.Time
  339. region string
  340. service string
  341. request string
  342. }
  343. }
  344. func (c credentialHeader) getScope() string {
  345. return strings.Join([]string{
  346. c.scope.date.Format(yyyymmdd),
  347. c.scope.region,
  348. c.scope.service,
  349. c.scope.request,
  350. }, "/")
  351. }
  352. // parse credentialHeader string into its structured form.
  353. func parseCredentialHeader(credElement string) (ch credentialHeader, aec s3err.ErrorCode) {
  354. creds := strings.SplitN(strings.TrimSpace(credElement), "=", 2)
  355. if len(creds) != 2 {
  356. return ch, s3err.ErrMissingFields
  357. }
  358. if creds[0] != "Credential" {
  359. return ch, s3err.ErrMissingCredTag
  360. }
  361. credElements := strings.Split(strings.TrimSpace(creds[1]), "/")
  362. if len(credElements) != 5 {
  363. return ch, s3err.ErrCredMalformed
  364. }
  365. // Save access key id.
  366. cred := credentialHeader{
  367. accessKey: credElements[0],
  368. }
  369. var e error
  370. cred.scope.date, e = time.Parse(yyyymmdd, credElements[1])
  371. if e != nil {
  372. return ch, s3err.ErrMalformedCredentialDate
  373. }
  374. cred.scope.region = credElements[2]
  375. cred.scope.service = credElements[3] // "s3"
  376. cred.scope.request = credElements[4] // "aws4_request"
  377. return cred, s3err.ErrNone
  378. }
  379. // Parse signature from signature tag.
  380. func parseSignature(signElement string) (string, s3err.ErrorCode) {
  381. signFields := strings.Split(strings.TrimSpace(signElement), "=")
  382. if len(signFields) != 2 {
  383. return "", s3err.ErrMissingFields
  384. }
  385. if signFields[0] != "Signature" {
  386. return "", s3err.ErrMissingSignTag
  387. }
  388. if signFields[1] == "" {
  389. return "", s3err.ErrMissingFields
  390. }
  391. signature := signFields[1]
  392. return signature, s3err.ErrNone
  393. }
  394. // Parse slice of signed headers from signed headers tag.
  395. func parseSignedHeader(signedHdrElement string) ([]string, s3err.ErrorCode) {
  396. signedHdrFields := strings.Split(strings.TrimSpace(signedHdrElement), "=")
  397. if len(signedHdrFields) != 2 {
  398. return nil, s3err.ErrMissingFields
  399. }
  400. if signedHdrFields[0] != "SignedHeaders" {
  401. return nil, s3err.ErrMissingSignHeadersTag
  402. }
  403. if signedHdrFields[1] == "" {
  404. return nil, s3err.ErrMissingFields
  405. }
  406. signedHeaders := strings.Split(signedHdrFields[1], ";")
  407. return signedHeaders, s3err.ErrNone
  408. }
  409. func (iam *IdentityAccessManagement) doesPolicySignatureV4Match(formValues http.Header) s3err.ErrorCode {
  410. // Parse credential tag.
  411. credHeader, err := parseCredentialHeader("Credential=" + formValues.Get("X-Amz-Credential"))
  412. if err != s3err.ErrNone {
  413. return err
  414. }
  415. identity, cred, found := iam.lookupByAccessKey(credHeader.accessKey)
  416. if !found {
  417. return s3err.ErrInvalidAccessKeyID
  418. }
  419. bucket := formValues.Get("bucket")
  420. if !identity.canDo(s3_constants.ACTION_WRITE, bucket, "") {
  421. return s3err.ErrAccessDenied
  422. }
  423. // Get signing key.
  424. signingKey := getSigningKey(cred.SecretKey, credHeader.scope.date.Format(yyyymmdd), credHeader.scope.region, credHeader.scope.service)
  425. // Get signature.
  426. newSignature := getSignature(signingKey, formValues.Get("Policy"))
  427. // Verify signature.
  428. if !compareSignatureV4(newSignature, formValues.Get("X-Amz-Signature")) {
  429. return s3err.ErrSignatureDoesNotMatch
  430. }
  431. return s3err.ErrNone
  432. }
  433. // Verify if extracted signed headers are not properly signed.
  434. func extractSignedHeaders(signedHeaders []string, r *http.Request) (http.Header, s3err.ErrorCode) {
  435. reqHeaders := r.Header
  436. // If no signed headers are provided, then return an error.
  437. if len(signedHeaders) == 0 {
  438. return nil, s3err.ErrMissingFields
  439. }
  440. extractedSignedHeaders := make(http.Header)
  441. for _, header := range signedHeaders {
  442. // `host` is not a case-sensitive header, unlike other headers such as `x-amz-date`.
  443. if header == "host" {
  444. // Get host value.
  445. hostHeaderValue := extractHostHeader(r)
  446. extractedSignedHeaders[header] = []string{hostHeaderValue}
  447. continue
  448. }
  449. // For all other headers we need to find them in the HTTP headers and copy them over.
  450. // We skip non-existent headers to be compatible with AWS signatures.
  451. if values, ok := reqHeaders[http.CanonicalHeaderKey(header)]; ok {
  452. extractedSignedHeaders[header] = values
  453. }
  454. }
  455. return extractedSignedHeaders, s3err.ErrNone
  456. }
  457. // extractHostHeader returns the value of host header if available.
  458. func extractHostHeader(r *http.Request) string {
  459. // Check for X-Forwarded-Host header first, which is set by reverse proxies
  460. if forwardedHost := r.Header.Get("X-Forwarded-Host"); forwardedHost != "" {
  461. // Check if reverse proxy also forwarded the port
  462. if forwardedPort := r.Header.Get("X-Forwarded-Port"); forwardedPort != "" {
  463. // Determine the protocol to check for standard ports
  464. proto := r.Header.Get("X-Forwarded-Proto")
  465. // Only add port if it's not the standard port for the protocol
  466. if (proto == "https" && forwardedPort != "443") || (proto != "https" && forwardedPort != "80") {
  467. return forwardedHost + ":" + forwardedPort
  468. }
  469. }
  470. // Using reverse proxy with X-Forwarded-Host (standard port or no port forwarded).
  471. return forwardedHost
  472. }
  473. hostHeaderValue := r.Host
  474. // For standard requests, this should be fine.
  475. if r.Host != "" {
  476. return hostHeaderValue
  477. }
  478. // If no host header is found, then check for host URL value.
  479. if r.URL.Host != "" {
  480. hostHeaderValue = r.URL.Host
  481. }
  482. return hostHeaderValue
  483. }
  484. // getScope generate a string of a specific date, an AWS region, and a service.
  485. func getScope(t time.Time, region string, service string) string {
  486. scope := strings.Join([]string{
  487. t.Format(yyyymmdd),
  488. region,
  489. service,
  490. "aws4_request",
  491. }, "/")
  492. return scope
  493. }
  494. // getCanonicalRequest generate a canonical request of style
  495. //
  496. // canonicalRequest =
  497. //
  498. // <HTTPMethod>\n
  499. // <CanonicalURI>\n
  500. // <CanonicalQueryString>\n
  501. // <CanonicalHeaders>\n
  502. // <SignedHeaders>\n
  503. // <HashedPayload>
  504. func getCanonicalRequest(extractedSignedHeaders http.Header, payload, queryStr, urlPath, method string) string {
  505. rawQuery := strings.Replace(queryStr, "+", "%20", -1)
  506. encodedPath := encodePath(urlPath)
  507. canonicalRequest := strings.Join([]string{
  508. method,
  509. encodedPath,
  510. rawQuery,
  511. getCanonicalHeaders(extractedSignedHeaders),
  512. getSignedHeaders(extractedSignedHeaders),
  513. payload,
  514. }, "\n")
  515. return canonicalRequest
  516. }
  517. // getStringToSign a string based on selected query values.
  518. func getStringToSign(canonicalRequest string, t time.Time, scope string) string {
  519. stringToSign := signV4Algorithm + "\n" + t.Format(iso8601Format) + "\n"
  520. stringToSign = stringToSign + scope + "\n"
  521. stringToSign = stringToSign + getSHA256Hash([]byte(canonicalRequest))
  522. return stringToSign
  523. }
  524. // getSHA256Hash returns hex-encoded SHA256 hash of the input data.
  525. func getSHA256Hash(data []byte) string {
  526. hash := sha256.Sum256(data)
  527. return hex.EncodeToString(hash[:])
  528. }
  529. // sumHMAC calculate hmac between two input byte array.
  530. func sumHMAC(key []byte, data []byte) []byte {
  531. hash := hmac.New(sha256.New, key)
  532. hash.Write(data)
  533. return hash.Sum(nil)
  534. }
  535. // getSigningKey hmac seed to calculate final signature.
  536. func getSigningKey(secretKey string, time string, region string, service string) []byte {
  537. date := sumHMAC([]byte("AWS4"+secretKey), []byte(time))
  538. regionBytes := sumHMAC(date, []byte(region))
  539. serviceBytes := sumHMAC(regionBytes, []byte(service))
  540. signingKey := sumHMAC(serviceBytes, []byte("aws4_request"))
  541. return signingKey
  542. }
  543. // getCanonicalHeaders generate a list of request headers with their values
  544. func getCanonicalHeaders(signedHeaders http.Header) string {
  545. var headers []string
  546. vals := make(http.Header)
  547. for k, vv := range signedHeaders {
  548. vals[strings.ToLower(k)] = vv
  549. }
  550. for k := range vals {
  551. headers = append(headers, k)
  552. }
  553. sort.Strings(headers)
  554. var buf bytes.Buffer
  555. for _, k := range headers {
  556. buf.WriteString(k)
  557. buf.WriteByte(':')
  558. for idx, v := range vals[k] {
  559. if idx > 0 {
  560. buf.WriteByte(',')
  561. }
  562. buf.WriteString(signV4TrimAll(v))
  563. }
  564. buf.WriteByte('\n')
  565. }
  566. return buf.String()
  567. }
  568. // signV4TrimAll trims leading and trailing spaces from each string in the slice, and trims sequential spaces.
  569. func signV4TrimAll(input string) string {
  570. // Compress adjacent spaces (a space is determined by
  571. // unicode.IsSpace() internally here) to a single space and trim
  572. // leading and trailing spaces.
  573. return strings.Join(strings.Fields(input), " ")
  574. }
  575. // getSignedHeaders generate a string i.e alphabetically sorted, semicolon-separated list of lowercase request header names
  576. func getSignedHeaders(signedHeaders http.Header) string {
  577. var headers []string
  578. for k := range signedHeaders {
  579. headers = append(headers, strings.ToLower(k))
  580. }
  581. sort.Strings(headers)
  582. return strings.Join(headers, ";")
  583. }
  584. // if object matches reserved string, no need to encode them
  585. var reservedObjectNames = regexp.MustCompile("^[a-zA-Z0-9-_.~/]+$")
  586. // encodePath encodes the strings from UTF-8 byte representations to HTML hex escape sequences
  587. //
  588. // This is necessary since regular url.Parse() and url.Encode() functions do not support UTF-8
  589. // non english characters cannot be parsed due to the nature in which url.Encode() is written
  590. //
  591. // This function on the other hand is a direct replacement for url.Encode() technique to support
  592. // pretty much every UTF-8 character.
  593. func encodePath(pathName string) string {
  594. if reservedObjectNames.MatchString(pathName) {
  595. return pathName
  596. }
  597. var encodedPathname string
  598. for _, s := range pathName {
  599. if 'A' <= s && s <= 'Z' || 'a' <= s && s <= 'z' || '0' <= s && s <= '9' { // §2.3 Unreserved characters (mark)
  600. encodedPathname = encodedPathname + string(s)
  601. } else {
  602. switch s {
  603. case '-', '_', '.', '~', '/': // §2.3 Unreserved characters (mark)
  604. encodedPathname = encodedPathname + string(s)
  605. default:
  606. runeLen := utf8.RuneLen(s)
  607. if runeLen < 0 {
  608. return pathName
  609. }
  610. u := make([]byte, runeLen)
  611. utf8.EncodeRune(u, s)
  612. for _, r := range u {
  613. hex := hex.EncodeToString([]byte{r})
  614. encodedPathname = encodedPathname + "%" + strings.ToUpper(hex)
  615. }
  616. }
  617. }
  618. }
  619. return encodedPathname
  620. }
  621. // getSignature final signature in hexadecimal form.
  622. func getSignature(signingKey []byte, stringToSign string) string {
  623. return hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
  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 doesn't use
  630. // non-ASCII characters. Otherwise, we'd need to convert the strings to
  631. // a []rune of UTF-8 characters.
  632. return subtle.ConstantTimeCompare([]byte(sig1), []byte(sig2)) == 1
  633. }