v4a.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. package v4a
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto"
  6. "crypto/ecdsa"
  7. "crypto/elliptic"
  8. "crypto/rand"
  9. "crypto/sha256"
  10. "encoding/hex"
  11. "fmt"
  12. "hash"
  13. "math/big"
  14. "net/http"
  15. "net/textproto"
  16. "net/url"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. "time"
  21. signerCrypto "github.com/aws/aws-sdk-go-v2/internal/v4a/internal/crypto"
  22. v4Internal "github.com/aws/aws-sdk-go-v2/internal/v4a/internal/v4"
  23. "github.com/aws/smithy-go/encoding/httpbinding"
  24. "github.com/aws/smithy-go/logging"
  25. )
  26. const (
  27. // AmzRegionSetKey represents the region set header used for sigv4a
  28. AmzRegionSetKey = "X-Amz-Region-Set"
  29. amzAlgorithmKey = v4Internal.AmzAlgorithmKey
  30. amzSecurityTokenKey = v4Internal.AmzSecurityTokenKey
  31. amzDateKey = v4Internal.AmzDateKey
  32. amzCredentialKey = v4Internal.AmzCredentialKey
  33. amzSignedHeadersKey = v4Internal.AmzSignedHeadersKey
  34. authorizationHeader = "Authorization"
  35. signingAlgorithm = "AWS4-ECDSA-P256-SHA256"
  36. timeFormat = "20060102T150405Z"
  37. shortTimeFormat = "20060102"
  38. // EmptyStringSHA256 is a hex encoded SHA-256 hash of an empty string
  39. EmptyStringSHA256 = v4Internal.EmptyStringSHA256
  40. // Version of signing v4a
  41. Version = "SigV4A"
  42. )
  43. var (
  44. p256 elliptic.Curve
  45. nMinusTwoP256 *big.Int
  46. one = new(big.Int).SetInt64(1)
  47. )
  48. func init() {
  49. // Ensure the elliptic curve parameters are initialized on package import rather then on first usage
  50. p256 = elliptic.P256()
  51. nMinusTwoP256 = new(big.Int).SetBytes(p256.Params().N.Bytes())
  52. nMinusTwoP256 = nMinusTwoP256.Sub(nMinusTwoP256, new(big.Int).SetInt64(2))
  53. }
  54. // SignerOptions is the SigV4a signing options for constructing a Signer.
  55. type SignerOptions struct {
  56. Logger logging.Logger
  57. LogSigning bool
  58. // Disables the Signer's moving HTTP header key/value pairs from the HTTP
  59. // request header to the request's query string. This is most commonly used
  60. // with pre-signed requests preventing headers from being added to the
  61. // request's query string.
  62. DisableHeaderHoisting bool
  63. // Disables the automatic escaping of the URI path of the request for the
  64. // siganture's canonical string's path. For services that do not need additional
  65. // escaping then use this to disable the signer escaping the path.
  66. //
  67. // S3 is an example of a service that does not need additional escaping.
  68. //
  69. // http://docs.aws.amazon.com/general/latest/gr/sigv4-create-canonical-request.html
  70. DisableURIPathEscaping bool
  71. }
  72. // Signer is a SigV4a HTTP signing implementation
  73. type Signer struct {
  74. options SignerOptions
  75. }
  76. // NewSigner constructs a SigV4a Signer.
  77. func NewSigner(optFns ...func(*SignerOptions)) *Signer {
  78. options := SignerOptions{}
  79. for _, fn := range optFns {
  80. fn(&options)
  81. }
  82. return &Signer{options: options}
  83. }
  84. // deriveKeyFromAccessKeyPair derives a NIST P-256 PrivateKey from the given
  85. // IAM AccessKey and SecretKey pair.
  86. //
  87. // Based on FIPS.186-4 Appendix B.4.2
  88. func deriveKeyFromAccessKeyPair(accessKey, secretKey string) (*ecdsa.PrivateKey, error) {
  89. params := p256.Params()
  90. bitLen := params.BitSize // Testing random candidates does not require an additional 64 bits
  91. counter := 0x01
  92. buffer := make([]byte, 1+len(accessKey)) // 1 byte counter + len(accessKey)
  93. kdfContext := bytes.NewBuffer(buffer)
  94. inputKey := append([]byte("AWS4A"), []byte(secretKey)...)
  95. d := new(big.Int)
  96. for {
  97. kdfContext.Reset()
  98. kdfContext.WriteString(accessKey)
  99. kdfContext.WriteByte(byte(counter))
  100. key, err := signerCrypto.HMACKeyDerivation(sha256.New, bitLen, inputKey, []byte(signingAlgorithm), kdfContext.Bytes())
  101. if err != nil {
  102. return nil, err
  103. }
  104. // Check key first before calling SetBytes if key key is in fact a valid candidate.
  105. // This ensures the byte slice is the correct length (32-bytes) to compare in constant-time
  106. cmp, err := signerCrypto.ConstantTimeByteCompare(key, nMinusTwoP256.Bytes())
  107. if err != nil {
  108. return nil, err
  109. }
  110. if cmp == -1 {
  111. d.SetBytes(key)
  112. break
  113. }
  114. counter++
  115. if counter > 0xFF {
  116. return nil, fmt.Errorf("exhausted single byte external counter")
  117. }
  118. }
  119. d = d.Add(d, one)
  120. priv := new(ecdsa.PrivateKey)
  121. priv.PublicKey.Curve = p256
  122. priv.D = d
  123. priv.PublicKey.X, priv.PublicKey.Y = p256.ScalarBaseMult(d.Bytes())
  124. return priv, nil
  125. }
  126. type httpSigner struct {
  127. Request *http.Request
  128. ServiceName string
  129. RegionSet []string
  130. Time time.Time
  131. Credentials Credentials
  132. IsPreSign bool
  133. Logger logging.Logger
  134. Debug bool
  135. // PayloadHash is the hex encoded SHA-256 hash of the request payload
  136. // If len(PayloadHash) == 0 the signer will attempt to send the request
  137. // as an unsigned payload. Note: Unsigned payloads only work for a subset of services.
  138. PayloadHash string
  139. DisableHeaderHoisting bool
  140. DisableURIPathEscaping bool
  141. }
  142. // SignHTTP takes the provided http.Request, payload hash, service, regionSet, and time and signs using SigV4a.
  143. // The passed in request will be modified in place.
  144. func (s *Signer) SignHTTP(ctx context.Context, credentials Credentials, r *http.Request, payloadHash string, service string, regionSet []string, signingTime time.Time, optFns ...func(*SignerOptions)) error {
  145. options := s.options
  146. for _, fn := range optFns {
  147. fn(&options)
  148. }
  149. signer := &httpSigner{
  150. Request: r,
  151. PayloadHash: payloadHash,
  152. ServiceName: service,
  153. RegionSet: regionSet,
  154. Credentials: credentials,
  155. Time: signingTime.UTC(),
  156. DisableHeaderHoisting: options.DisableHeaderHoisting,
  157. DisableURIPathEscaping: options.DisableURIPathEscaping,
  158. }
  159. signedRequest, err := signer.Build()
  160. if err != nil {
  161. return err
  162. }
  163. logHTTPSigningInfo(ctx, options, signedRequest)
  164. return nil
  165. }
  166. // PresignHTTP takes the provided http.Request, payload hash, service, regionSet, and time and presigns using SigV4a
  167. // Returns the presigned URL along with the headers that were signed with the request.
  168. //
  169. // PresignHTTP will not set the expires time of the presigned request
  170. // automatically. To specify the expire duration for a request add the
  171. // "X-Amz-Expires" query parameter on the request with the value as the
  172. // duration in seconds the presigned URL should be considered valid for. This
  173. // parameter is not used by all AWS services, and is most notable used by
  174. // Amazon S3 APIs.
  175. func (s *Signer) PresignHTTP(ctx context.Context, credentials Credentials, r *http.Request, payloadHash string, service string, regionSet []string, signingTime time.Time, optFns ...func(*SignerOptions)) (signedURI string, signedHeaders http.Header, err error) {
  176. options := s.options
  177. for _, fn := range optFns {
  178. fn(&options)
  179. }
  180. signer := &httpSigner{
  181. Request: r,
  182. PayloadHash: payloadHash,
  183. ServiceName: service,
  184. RegionSet: regionSet,
  185. Credentials: credentials,
  186. Time: signingTime.UTC(),
  187. IsPreSign: true,
  188. DisableHeaderHoisting: options.DisableHeaderHoisting,
  189. DisableURIPathEscaping: options.DisableURIPathEscaping,
  190. }
  191. signedRequest, err := signer.Build()
  192. if err != nil {
  193. return "", nil, err
  194. }
  195. logHTTPSigningInfo(ctx, options, signedRequest)
  196. signedHeaders = make(http.Header)
  197. // For the signed headers we canonicalize the header keys in the returned map.
  198. // This avoids situations where can standard library double headers like host header. For example the standard
  199. // library will set the Host header, even if it is present in lower-case form.
  200. for k, v := range signedRequest.SignedHeaders {
  201. key := textproto.CanonicalMIMEHeaderKey(k)
  202. signedHeaders[key] = append(signedHeaders[key], v...)
  203. }
  204. return signedRequest.Request.URL.String(), signedHeaders, nil
  205. }
  206. func (s *httpSigner) setRequiredSigningFields(headers http.Header, query url.Values) {
  207. amzDate := s.Time.Format(timeFormat)
  208. if s.IsPreSign {
  209. query.Set(AmzRegionSetKey, strings.Join(s.RegionSet, ","))
  210. query.Set(amzDateKey, amzDate)
  211. query.Set(amzAlgorithmKey, signingAlgorithm)
  212. if len(s.Credentials.SessionToken) > 0 {
  213. query.Set(amzSecurityTokenKey, s.Credentials.SessionToken)
  214. }
  215. return
  216. }
  217. headers.Set(AmzRegionSetKey, strings.Join(s.RegionSet, ","))
  218. headers.Set(amzDateKey, amzDate)
  219. if len(s.Credentials.SessionToken) > 0 {
  220. headers.Set(amzSecurityTokenKey, s.Credentials.SessionToken)
  221. }
  222. }
  223. func (s *httpSigner) Build() (signedRequest, error) {
  224. req := s.Request
  225. query := req.URL.Query()
  226. headers := req.Header
  227. s.setRequiredSigningFields(headers, query)
  228. // Sort Each Query Key's Values
  229. for key := range query {
  230. sort.Strings(query[key])
  231. }
  232. v4Internal.SanitizeHostForHeader(req)
  233. credentialScope := s.buildCredentialScope()
  234. credentialStr := s.Credentials.Context + "/" + credentialScope
  235. if s.IsPreSign {
  236. query.Set(amzCredentialKey, credentialStr)
  237. }
  238. unsignedHeaders := headers
  239. if s.IsPreSign && !s.DisableHeaderHoisting {
  240. urlValues := url.Values{}
  241. urlValues, unsignedHeaders = buildQuery(v4Internal.AllowedQueryHoisting, unsignedHeaders)
  242. for k := range urlValues {
  243. query[k] = urlValues[k]
  244. }
  245. }
  246. host := req.URL.Host
  247. if len(req.Host) > 0 {
  248. host = req.Host
  249. }
  250. signedHeaders, signedHeadersStr, canonicalHeaderStr := s.buildCanonicalHeaders(host, v4Internal.IgnoredHeaders, unsignedHeaders, s.Request.ContentLength)
  251. if s.IsPreSign {
  252. query.Set(amzSignedHeadersKey, signedHeadersStr)
  253. }
  254. rawQuery := strings.Replace(query.Encode(), "+", "%20", -1)
  255. canonicalURI := v4Internal.GetURIPath(req.URL)
  256. if !s.DisableURIPathEscaping {
  257. canonicalURI = httpbinding.EscapePath(canonicalURI, false)
  258. }
  259. canonicalString := s.buildCanonicalString(
  260. req.Method,
  261. canonicalURI,
  262. rawQuery,
  263. signedHeadersStr,
  264. canonicalHeaderStr,
  265. )
  266. strToSign := s.buildStringToSign(credentialScope, canonicalString)
  267. signingSignature, err := s.buildSignature(strToSign)
  268. if err != nil {
  269. return signedRequest{}, err
  270. }
  271. if s.IsPreSign {
  272. rawQuery += "&X-Amz-Signature=" + signingSignature
  273. } else {
  274. headers[authorizationHeader] = append(headers[authorizationHeader][:0], buildAuthorizationHeader(credentialStr, signedHeadersStr, signingSignature))
  275. }
  276. req.URL.RawQuery = rawQuery
  277. return signedRequest{
  278. Request: req,
  279. SignedHeaders: signedHeaders,
  280. CanonicalString: canonicalString,
  281. StringToSign: strToSign,
  282. PreSigned: s.IsPreSign,
  283. }, nil
  284. }
  285. func buildAuthorizationHeader(credentialStr, signedHeadersStr, signingSignature string) string {
  286. const credential = "Credential="
  287. const signedHeaders = "SignedHeaders="
  288. const signature = "Signature="
  289. const commaSpace = ", "
  290. var parts strings.Builder
  291. parts.Grow(len(signingAlgorithm) + 1 +
  292. len(credential) + len(credentialStr) + len(commaSpace) +
  293. len(signedHeaders) + len(signedHeadersStr) + len(commaSpace) +
  294. len(signature) + len(signingSignature),
  295. )
  296. parts.WriteString(signingAlgorithm)
  297. parts.WriteRune(' ')
  298. parts.WriteString(credential)
  299. parts.WriteString(credentialStr)
  300. parts.WriteString(commaSpace)
  301. parts.WriteString(signedHeaders)
  302. parts.WriteString(signedHeadersStr)
  303. parts.WriteString(commaSpace)
  304. parts.WriteString(signature)
  305. parts.WriteString(signingSignature)
  306. return parts.String()
  307. }
  308. func (s *httpSigner) buildCredentialScope() string {
  309. return strings.Join([]string{
  310. s.Time.Format(shortTimeFormat),
  311. s.ServiceName,
  312. "aws4_request",
  313. }, "/")
  314. }
  315. func buildQuery(r v4Internal.Rule, header http.Header) (url.Values, http.Header) {
  316. query := url.Values{}
  317. unsignedHeaders := http.Header{}
  318. for k, h := range header {
  319. if r.IsValid(k) {
  320. query[k] = h
  321. } else {
  322. unsignedHeaders[k] = h
  323. }
  324. }
  325. return query, unsignedHeaders
  326. }
  327. func (s *httpSigner) buildCanonicalHeaders(host string, rule v4Internal.Rule, header http.Header, length int64) (signed http.Header, signedHeaders, canonicalHeadersStr string) {
  328. signed = make(http.Header)
  329. var headers []string
  330. const hostHeader = "host"
  331. headers = append(headers, hostHeader)
  332. signed[hostHeader] = append(signed[hostHeader], host)
  333. if length > 0 {
  334. const contentLengthHeader = "content-length"
  335. headers = append(headers, contentLengthHeader)
  336. signed[contentLengthHeader] = append(signed[contentLengthHeader], strconv.FormatInt(length, 10))
  337. }
  338. for k, v := range header {
  339. if !rule.IsValid(k) {
  340. continue // ignored header
  341. }
  342. lowerCaseKey := strings.ToLower(k)
  343. if _, ok := signed[lowerCaseKey]; ok {
  344. // include additional values
  345. signed[lowerCaseKey] = append(signed[lowerCaseKey], v...)
  346. continue
  347. }
  348. headers = append(headers, lowerCaseKey)
  349. signed[lowerCaseKey] = v
  350. }
  351. sort.Strings(headers)
  352. signedHeaders = strings.Join(headers, ";")
  353. var canonicalHeaders strings.Builder
  354. n := len(headers)
  355. const colon = ':'
  356. for i := 0; i < n; i++ {
  357. if headers[i] == hostHeader {
  358. canonicalHeaders.WriteString(hostHeader)
  359. canonicalHeaders.WriteRune(colon)
  360. canonicalHeaders.WriteString(v4Internal.StripExcessSpaces(host))
  361. } else {
  362. canonicalHeaders.WriteString(headers[i])
  363. canonicalHeaders.WriteRune(colon)
  364. // Trim out leading, trailing, and dedup inner spaces from signed header values.
  365. values := signed[headers[i]]
  366. for j, v := range values {
  367. cleanedValue := strings.TrimSpace(v4Internal.StripExcessSpaces(v))
  368. canonicalHeaders.WriteString(cleanedValue)
  369. if j < len(values)-1 {
  370. canonicalHeaders.WriteRune(',')
  371. }
  372. }
  373. }
  374. canonicalHeaders.WriteRune('\n')
  375. }
  376. canonicalHeadersStr = canonicalHeaders.String()
  377. return signed, signedHeaders, canonicalHeadersStr
  378. }
  379. func (s *httpSigner) buildCanonicalString(method, uri, query, signedHeaders, canonicalHeaders string) string {
  380. return strings.Join([]string{
  381. method,
  382. uri,
  383. query,
  384. canonicalHeaders,
  385. signedHeaders,
  386. s.PayloadHash,
  387. }, "\n")
  388. }
  389. func (s *httpSigner) buildStringToSign(credentialScope, canonicalRequestString string) string {
  390. return strings.Join([]string{
  391. signingAlgorithm,
  392. s.Time.Format(timeFormat),
  393. credentialScope,
  394. hex.EncodeToString(makeHash(sha256.New(), []byte(canonicalRequestString))),
  395. }, "\n")
  396. }
  397. func makeHash(hash hash.Hash, b []byte) []byte {
  398. hash.Reset()
  399. hash.Write(b)
  400. return hash.Sum(nil)
  401. }
  402. func (s *httpSigner) buildSignature(strToSign string) (string, error) {
  403. sig, err := s.Credentials.PrivateKey.Sign(rand.Reader, makeHash(sha256.New(), []byte(strToSign)), crypto.SHA256)
  404. if err != nil {
  405. return "", err
  406. }
  407. return hex.EncodeToString(sig), nil
  408. }
  409. const logSignInfoMsg = `Request Signature:
  410. ---[ CANONICAL STRING ]-----------------------------
  411. %s
  412. ---[ STRING TO SIGN ]--------------------------------
  413. %s%s
  414. -----------------------------------------------------`
  415. const logSignedURLMsg = `
  416. ---[ SIGNED URL ]------------------------------------
  417. %s`
  418. func logHTTPSigningInfo(ctx context.Context, options SignerOptions, r signedRequest) {
  419. if !options.LogSigning {
  420. return
  421. }
  422. signedURLMsg := ""
  423. if r.PreSigned {
  424. signedURLMsg = fmt.Sprintf(logSignedURLMsg, r.Request.URL.String())
  425. }
  426. logger := logging.WithContext(ctx, options.Logger)
  427. logger.Logf(logging.Debug, logSignInfoMsg, r.CanonicalString, r.StringToSign, signedURLMsg)
  428. }
  429. type signedRequest struct {
  430. Request *http.Request
  431. SignedHeaders http.Header
  432. CanonicalString string
  433. StringToSign string
  434. PreSigned bool
  435. }