request-signature-streaming.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 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. "fmt"
  22. "io"
  23. "io/ioutil"
  24. "net/http"
  25. "strconv"
  26. "strings"
  27. "time"
  28. )
  29. // Reference for constants used below -
  30. // http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html#example-signature-calculations-streaming
  31. const (
  32. streamingSignAlgorithm = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
  33. streamingPayloadHdr = "AWS4-HMAC-SHA256-PAYLOAD"
  34. emptySHA256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
  35. payloadChunkSize = 64 * 1024
  36. chunkSigConstLen = 17 // ";chunk-signature="
  37. signatureStrLen = 64 // e.g. "f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2"
  38. crlfLen = 2 // CRLF
  39. )
  40. // Request headers to be ignored while calculating seed signature for
  41. // a request.
  42. var ignoredStreamingHeaders = map[string]bool{
  43. "Authorization": true,
  44. "User-Agent": true,
  45. "Content-Type": true,
  46. }
  47. // getSignedChunkLength - calculates the length of chunk metadata
  48. func getSignedChunkLength(chunkDataSize int64) int64 {
  49. return int64(len(fmt.Sprintf("%x", chunkDataSize))) +
  50. chunkSigConstLen +
  51. signatureStrLen +
  52. crlfLen +
  53. chunkDataSize +
  54. crlfLen
  55. }
  56. // getStreamLength - calculates the length of the overall stream (data + metadata)
  57. func getStreamLength(dataLen, chunkSize int64) int64 {
  58. if dataLen <= 0 {
  59. return 0
  60. }
  61. chunksCount := int64(dataLen / chunkSize)
  62. remainingBytes := int64(dataLen % chunkSize)
  63. streamLen := int64(0)
  64. streamLen += chunksCount * getSignedChunkLength(chunkSize)
  65. if remainingBytes > 0 {
  66. streamLen += getSignedChunkLength(remainingBytes)
  67. }
  68. streamLen += getSignedChunkLength(0)
  69. return streamLen
  70. }
  71. // buildChunkStringToSign - returns the string to sign given chunk data
  72. // and previous signature.
  73. func buildChunkStringToSign(t time.Time, region, previousSig string, chunkData []byte) string {
  74. stringToSignParts := []string{
  75. streamingPayloadHdr,
  76. t.Format(iso8601DateFormat),
  77. getScope(region, t, ServiceTypeS3),
  78. previousSig,
  79. emptySHA256,
  80. hex.EncodeToString(sum256(chunkData)),
  81. }
  82. return strings.Join(stringToSignParts, "\n")
  83. }
  84. // prepareStreamingRequest - prepares a request with appropriate
  85. // headers before computing the seed signature.
  86. func prepareStreamingRequest(req *http.Request, sessionToken string, dataLen int64, timestamp time.Time) {
  87. // Set x-amz-content-sha256 header.
  88. req.Header.Set("X-Amz-Content-Sha256", streamingSignAlgorithm)
  89. if sessionToken != "" {
  90. req.Header.Set("X-Amz-Security-Token", sessionToken)
  91. }
  92. req.Header.Set("X-Amz-Date", timestamp.Format(iso8601DateFormat))
  93. // Set content length with streaming signature for each chunk included.
  94. req.ContentLength = getStreamLength(dataLen, int64(payloadChunkSize))
  95. req.Header.Set("x-amz-decoded-content-length", strconv.FormatInt(dataLen, 10))
  96. }
  97. // buildChunkHeader - returns the chunk header.
  98. // e.g string(IntHexBase(chunk-size)) + ";chunk-signature=" + signature + \r\n + chunk-data + \r\n
  99. func buildChunkHeader(chunkLen int64, signature string) []byte {
  100. return []byte(strconv.FormatInt(chunkLen, 16) + ";chunk-signature=" + signature + "\r\n")
  101. }
  102. // buildChunkSignature - returns chunk signature for a given chunk and previous signature.
  103. func buildChunkSignature(chunkData []byte, reqTime time.Time, region,
  104. previousSignature, secretAccessKey string) string {
  105. chunkStringToSign := buildChunkStringToSign(reqTime, region,
  106. previousSignature, chunkData)
  107. signingKey := getSigningKey(secretAccessKey, region, reqTime, ServiceTypeS3)
  108. return getSignature(signingKey, chunkStringToSign)
  109. }
  110. // getSeedSignature - returns the seed signature for a given request.
  111. func (s *StreamingReader) setSeedSignature(req *http.Request) {
  112. // Get canonical request
  113. canonicalRequest := getCanonicalRequest(*req, ignoredStreamingHeaders, getHashedPayload(*req))
  114. // Get string to sign from canonical request.
  115. stringToSign := getStringToSignV4(s.reqTime, s.region, canonicalRequest, ServiceTypeS3)
  116. signingKey := getSigningKey(s.secretAccessKey, s.region, s.reqTime, ServiceTypeS3)
  117. // Calculate signature.
  118. s.seedSignature = getSignature(signingKey, stringToSign)
  119. }
  120. // StreamingReader implements chunked upload signature as a reader on
  121. // top of req.Body's ReaderCloser chunk header;data;... repeat
  122. type StreamingReader struct {
  123. accessKeyID string
  124. secretAccessKey string
  125. sessionToken string
  126. region string
  127. prevSignature string
  128. seedSignature string
  129. contentLen int64 // Content-Length from req header
  130. baseReadCloser io.ReadCloser // underlying io.Reader
  131. bytesRead int64 // bytes read from underlying io.Reader
  132. buf bytes.Buffer // holds signed chunk
  133. chunkBuf []byte // holds raw data read from req Body
  134. chunkBufLen int // no. of bytes read so far into chunkBuf
  135. done bool // done reading the underlying reader to EOF
  136. reqTime time.Time
  137. chunkNum int
  138. totalChunks int
  139. lastChunkSize int
  140. }
  141. // signChunk - signs a chunk read from s.baseReader of chunkLen size.
  142. func (s *StreamingReader) signChunk(chunkLen int) {
  143. // Compute chunk signature for next header
  144. signature := buildChunkSignature(s.chunkBuf[:chunkLen], s.reqTime,
  145. s.region, s.prevSignature, s.secretAccessKey)
  146. // For next chunk signature computation
  147. s.prevSignature = signature
  148. // Write chunk header into streaming buffer
  149. chunkHdr := buildChunkHeader(int64(chunkLen), signature)
  150. s.buf.Write(chunkHdr)
  151. // Write chunk data into streaming buffer
  152. s.buf.Write(s.chunkBuf[:chunkLen])
  153. // Write the chunk trailer.
  154. s.buf.Write([]byte("\r\n"))
  155. // Reset chunkBufLen for next chunk read.
  156. s.chunkBufLen = 0
  157. s.chunkNum++
  158. }
  159. // setStreamingAuthHeader - builds and sets authorization header value
  160. // for streaming signature.
  161. func (s *StreamingReader) setStreamingAuthHeader(req *http.Request) {
  162. credential := GetCredential(s.accessKeyID, s.region, s.reqTime, ServiceTypeS3)
  163. authParts := []string{
  164. signV4Algorithm + " Credential=" + credential,
  165. "SignedHeaders=" + getSignedHeaders(*req, ignoredStreamingHeaders),
  166. "Signature=" + s.seedSignature,
  167. }
  168. // Set authorization header.
  169. auth := strings.Join(authParts, ",")
  170. req.Header.Set("Authorization", auth)
  171. }
  172. // StreamingSignV4 - provides chunked upload signatureV4 support by
  173. // implementing io.Reader.
  174. func StreamingSignV4(req *http.Request, accessKeyID, secretAccessKey, sessionToken,
  175. region string, dataLen int64, reqTime time.Time) *http.Request {
  176. // Set headers needed for streaming signature.
  177. prepareStreamingRequest(req, sessionToken, dataLen, reqTime)
  178. if req.Body == nil {
  179. req.Body = ioutil.NopCloser(bytes.NewReader([]byte("")))
  180. }
  181. stReader := &StreamingReader{
  182. baseReadCloser: req.Body,
  183. accessKeyID: accessKeyID,
  184. secretAccessKey: secretAccessKey,
  185. sessionToken: sessionToken,
  186. region: region,
  187. reqTime: reqTime,
  188. chunkBuf: make([]byte, payloadChunkSize),
  189. contentLen: dataLen,
  190. chunkNum: 1,
  191. totalChunks: int((dataLen+payloadChunkSize-1)/payloadChunkSize) + 1,
  192. lastChunkSize: int(dataLen % payloadChunkSize),
  193. }
  194. // Add the request headers required for chunk upload signing.
  195. // Compute the seed signature.
  196. stReader.setSeedSignature(req)
  197. // Set the authorization header with the seed signature.
  198. stReader.setStreamingAuthHeader(req)
  199. // Set seed signature as prevSignature for subsequent
  200. // streaming signing process.
  201. stReader.prevSignature = stReader.seedSignature
  202. req.Body = stReader
  203. return req
  204. }
  205. // Read - this method performs chunk upload signature providing a
  206. // io.Reader interface.
  207. func (s *StreamingReader) Read(buf []byte) (int, error) {
  208. switch {
  209. // After the last chunk is read from underlying reader, we
  210. // never re-fill s.buf.
  211. case s.done:
  212. // s.buf will be (re-)filled with next chunk when has lesser
  213. // bytes than asked for.
  214. case s.buf.Len() < len(buf):
  215. s.chunkBufLen = 0
  216. for {
  217. n1, err := s.baseReadCloser.Read(s.chunkBuf[s.chunkBufLen:])
  218. // Usually we validate `err` first, but in this case
  219. // we are validating n > 0 for the following reasons.
  220. //
  221. // 1. n > 0, err is one of io.EOF, nil (near end of stream)
  222. // A Reader returning a non-zero number of bytes at the end
  223. // of the input stream may return either err == EOF or err == nil
  224. //
  225. // 2. n == 0, err is io.EOF (actual end of stream)
  226. //
  227. // Callers should always process the n > 0 bytes returned
  228. // before considering the error err.
  229. if n1 > 0 {
  230. s.chunkBufLen += n1
  231. s.bytesRead += int64(n1)
  232. if s.chunkBufLen == payloadChunkSize ||
  233. (s.chunkNum == s.totalChunks-1 &&
  234. s.chunkBufLen == s.lastChunkSize) {
  235. // Sign the chunk and write it to s.buf.
  236. s.signChunk(s.chunkBufLen)
  237. break
  238. }
  239. }
  240. if err != nil {
  241. if err == io.EOF {
  242. // No more data left in baseReader - last chunk.
  243. // Done reading the last chunk from baseReader.
  244. s.done = true
  245. // bytes read from baseReader different than
  246. // content length provided.
  247. if s.bytesRead != s.contentLen {
  248. return 0, fmt.Errorf("http: ContentLength=%d with Body length %d", s.contentLen, s.bytesRead)
  249. }
  250. // Sign the chunk and write it to s.buf.
  251. s.signChunk(0)
  252. break
  253. }
  254. return 0, err
  255. }
  256. }
  257. }
  258. return s.buf.Read(buf)
  259. }
  260. // Close - this method makes underlying io.ReadCloser's Close method available.
  261. func (s *StreamingReader) Close() error {
  262. return s.baseReadCloser.Close()
  263. }