chunked_reader_v4.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. package s3api
  2. // the related code is copied and modified from minio source code
  3. /*
  4. * Minio Cloud Storage, (C) 2016 Minio, Inc.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. import (
  19. "bufio"
  20. "bytes"
  21. "crypto/sha256"
  22. "encoding/hex"
  23. "errors"
  24. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  25. "hash"
  26. "io"
  27. "net/http"
  28. "time"
  29. "github.com/dustin/go-humanize"
  30. )
  31. // getChunkSignature - get chunk signature.
  32. func getChunkSignature(secretKey string, seedSignature string, region string, date time.Time, hashedChunk string) string {
  33. // Calculate string to sign.
  34. stringToSign := signV4ChunkedAlgorithm + "\n" +
  35. date.Format(iso8601Format) + "\n" +
  36. getScope(date, region) + "\n" +
  37. seedSignature + "\n" +
  38. emptySHA256 + "\n" +
  39. hashedChunk
  40. // Get hmac signing key.
  41. signingKey := getSigningKey(secretKey, date, region)
  42. // Calculate signature.
  43. newSignature := getSignature(signingKey, stringToSign)
  44. return newSignature
  45. }
  46. // calculateSeedSignature - Calculate seed signature in accordance with
  47. // - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html
  48. // returns signature, error otherwise if the signature mismatches or any other
  49. // error while parsing and validating.
  50. func (iam *IdentityAccessManagement) calculateSeedSignature(r *http.Request) (cred *Credential, signature string, region string, date time.Time, errCode s3err.ErrorCode) {
  51. // Copy request.
  52. req := *r
  53. // Save authorization header.
  54. v4Auth := req.Header.Get("Authorization")
  55. // Parse signature version '4' header.
  56. signV4Values, errCode := parseSignV4(v4Auth)
  57. if errCode != s3err.ErrNone {
  58. return nil, "", "", time.Time{}, errCode
  59. }
  60. // Payload streaming.
  61. payload := streamingContentSHA256
  62. // Payload for STREAMING signature should be 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD'
  63. if payload != req.Header.Get("X-Amz-Content-Sha256") {
  64. return nil, "", "", time.Time{}, s3err.ErrContentSHA256Mismatch
  65. }
  66. // Extract all the signed headers along with its values.
  67. extractedSignedHeaders, errCode := extractSignedHeaders(signV4Values.SignedHeaders, r)
  68. if errCode != s3err.ErrNone {
  69. return nil, "", "", time.Time{}, errCode
  70. }
  71. // Verify if the access key id matches.
  72. _, cred, found := iam.lookupByAccessKey(signV4Values.Credential.accessKey)
  73. if !found {
  74. return nil, "", "", time.Time{}, s3err.ErrInvalidAccessKeyID
  75. }
  76. // Verify if region is valid.
  77. region = signV4Values.Credential.scope.region
  78. // Extract date, if not present throw error.
  79. var dateStr string
  80. if dateStr = req.Header.Get(http.CanonicalHeaderKey("x-amz-date")); dateStr == "" {
  81. if dateStr = r.Header.Get("Date"); dateStr == "" {
  82. return nil, "", "", time.Time{}, s3err.ErrMissingDateHeader
  83. }
  84. }
  85. // Parse date header.
  86. var err error
  87. date, err = time.Parse(iso8601Format, dateStr)
  88. if err != nil {
  89. return nil, "", "", time.Time{}, s3err.ErrMalformedDate
  90. }
  91. // Query string.
  92. queryStr := req.URL.Query().Encode()
  93. // Get canonical request.
  94. canonicalRequest := getCanonicalRequest(extractedSignedHeaders, payload, queryStr, req.URL.Path, req.Method)
  95. // Get string to sign from canonical request.
  96. stringToSign := getStringToSign(canonicalRequest, date, signV4Values.Credential.getScope())
  97. // Get hmac signing key.
  98. signingKey := getSigningKey(cred.SecretKey, signV4Values.Credential.scope.date, region)
  99. // Calculate signature.
  100. newSignature := getSignature(signingKey, stringToSign)
  101. // Verify if signature match.
  102. if !compareSignatureV4(newSignature, signV4Values.Signature) {
  103. return nil, "", "", time.Time{}, s3err.ErrSignatureDoesNotMatch
  104. }
  105. // Return caculated signature.
  106. return cred, newSignature, region, date, s3err.ErrNone
  107. }
  108. const maxLineLength = 4 * humanize.KiByte // assumed <= bufio.defaultBufSize 4KiB
  109. // lineTooLong is generated as chunk header is bigger than 4KiB.
  110. var errLineTooLong = errors.New("header line too long")
  111. // Malformed encoding is generated when chunk header is wrongly formed.
  112. var errMalformedEncoding = errors.New("malformed chunked encoding")
  113. // newSignV4ChunkedReader returns a new s3ChunkedReader that translates the data read from r
  114. // out of HTTP "chunked" format before returning it.
  115. // The s3ChunkedReader returns io.EOF when the final 0-length chunk is read.
  116. func (iam *IdentityAccessManagement) newSignV4ChunkedReader(req *http.Request) (io.ReadCloser, s3err.ErrorCode) {
  117. ident, seedSignature, region, seedDate, errCode := iam.calculateSeedSignature(req)
  118. if errCode != s3err.ErrNone {
  119. return nil, errCode
  120. }
  121. return &s3ChunkedReader{
  122. cred: ident,
  123. reader: bufio.NewReader(req.Body),
  124. seedSignature: seedSignature,
  125. seedDate: seedDate,
  126. region: region,
  127. chunkSHA256Writer: sha256.New(),
  128. state: readChunkHeader,
  129. }, s3err.ErrNone
  130. }
  131. // Represents the overall state that is required for decoding a
  132. // AWS Signature V4 chunked reader.
  133. type s3ChunkedReader struct {
  134. cred *Credential
  135. reader *bufio.Reader
  136. seedSignature string
  137. seedDate time.Time
  138. region string
  139. state chunkState
  140. lastChunk bool
  141. chunkSignature string
  142. chunkSHA256Writer hash.Hash // Calculates sha256 of chunk data.
  143. n uint64 // Unread bytes in chunk
  144. err error
  145. }
  146. // Read chunk reads the chunk token signature portion.
  147. func (cr *s3ChunkedReader) readS3ChunkHeader() {
  148. // Read the first chunk line until CRLF.
  149. var hexChunkSize, hexChunkSignature []byte
  150. hexChunkSize, hexChunkSignature, cr.err = readChunkLine(cr.reader)
  151. if cr.err != nil {
  152. return
  153. }
  154. // <hex>;token=value - converts the hex into its uint64 form.
  155. cr.n, cr.err = parseHexUint(hexChunkSize)
  156. if cr.err != nil {
  157. return
  158. }
  159. if cr.n == 0 {
  160. cr.err = io.EOF
  161. }
  162. // Save the incoming chunk signature.
  163. cr.chunkSignature = string(hexChunkSignature)
  164. }
  165. type chunkState int
  166. const (
  167. readChunkHeader chunkState = iota
  168. readChunkTrailer
  169. readChunk
  170. verifyChunk
  171. eofChunk
  172. )
  173. func (cs chunkState) String() string {
  174. stateString := ""
  175. switch cs {
  176. case readChunkHeader:
  177. stateString = "readChunkHeader"
  178. case readChunkTrailer:
  179. stateString = "readChunkTrailer"
  180. case readChunk:
  181. stateString = "readChunk"
  182. case verifyChunk:
  183. stateString = "verifyChunk"
  184. case eofChunk:
  185. stateString = "eofChunk"
  186. }
  187. return stateString
  188. }
  189. func (cr *s3ChunkedReader) Close() (err error) {
  190. return nil
  191. }
  192. // Read - implements `io.Reader`, which transparently decodes
  193. // the incoming AWS Signature V4 streaming signature.
  194. func (cr *s3ChunkedReader) Read(buf []byte) (n int, err error) {
  195. for {
  196. switch cr.state {
  197. case readChunkHeader:
  198. cr.readS3ChunkHeader()
  199. // If we're at the end of a chunk.
  200. if cr.n == 0 && cr.err == io.EOF {
  201. cr.state = readChunkTrailer
  202. cr.lastChunk = true
  203. continue
  204. }
  205. if cr.err != nil {
  206. return 0, cr.err
  207. }
  208. cr.state = readChunk
  209. case readChunkTrailer:
  210. cr.err = readCRLF(cr.reader)
  211. if cr.err != nil {
  212. return 0, errMalformedEncoding
  213. }
  214. cr.state = verifyChunk
  215. case readChunk:
  216. // There is no more space left in the request buffer.
  217. if len(buf) == 0 {
  218. return n, nil
  219. }
  220. rbuf := buf
  221. // The request buffer is larger than the current chunk size.
  222. // Read only the current chunk from the underlying reader.
  223. if uint64(len(rbuf)) > cr.n {
  224. rbuf = rbuf[:cr.n]
  225. }
  226. var n0 int
  227. n0, cr.err = cr.reader.Read(rbuf)
  228. if cr.err != nil {
  229. // We have lesser than chunk size advertised in chunkHeader, this is 'unexpected'.
  230. if cr.err == io.EOF {
  231. cr.err = io.ErrUnexpectedEOF
  232. }
  233. return 0, cr.err
  234. }
  235. // Calculate sha256.
  236. cr.chunkSHA256Writer.Write(rbuf[:n0])
  237. // Update the bytes read into request buffer so far.
  238. n += n0
  239. buf = buf[n0:]
  240. // Update bytes to be read of the current chunk before verifying chunk's signature.
  241. cr.n -= uint64(n0)
  242. // If we're at the end of a chunk.
  243. if cr.n == 0 {
  244. cr.state = readChunkTrailer
  245. continue
  246. }
  247. case verifyChunk:
  248. // Calculate the hashed chunk.
  249. hashedChunk := hex.EncodeToString(cr.chunkSHA256Writer.Sum(nil))
  250. // Calculate the chunk signature.
  251. newSignature := getChunkSignature(cr.cred.SecretKey, cr.seedSignature, cr.region, cr.seedDate, hashedChunk)
  252. if !compareSignatureV4(cr.chunkSignature, newSignature) {
  253. // Chunk signature doesn't match we return signature does not match.
  254. cr.err = errors.New("chunk signature does not match")
  255. return 0, cr.err
  256. }
  257. // Newly calculated signature becomes the seed for the next chunk
  258. // this follows the chaining.
  259. cr.seedSignature = newSignature
  260. cr.chunkSHA256Writer.Reset()
  261. if cr.lastChunk {
  262. cr.state = eofChunk
  263. } else {
  264. cr.state = readChunkHeader
  265. }
  266. case eofChunk:
  267. return n, io.EOF
  268. }
  269. }
  270. }
  271. // readCRLF - check if reader only has '\r\n' CRLF character.
  272. // returns malformed encoding if it doesn't.
  273. func readCRLF(reader io.Reader) error {
  274. buf := make([]byte, 2)
  275. _, err := io.ReadFull(reader, buf[:2])
  276. if err != nil {
  277. return err
  278. }
  279. if buf[0] != '\r' || buf[1] != '\n' {
  280. return errMalformedEncoding
  281. }
  282. return nil
  283. }
  284. // Read a line of bytes (up to \n) from b.
  285. // Give up if the line exceeds maxLineLength.
  286. // The returned bytes are owned by the bufio.Reader
  287. // so they are only valid until the next bufio read.
  288. func readChunkLine(b *bufio.Reader) ([]byte, []byte, error) {
  289. buf, err := b.ReadSlice('\n')
  290. if err != nil {
  291. // We always know when EOF is coming.
  292. // If the caller asked for a line, there should be a line.
  293. if err == io.EOF {
  294. err = io.ErrUnexpectedEOF
  295. } else if err == bufio.ErrBufferFull {
  296. err = errLineTooLong
  297. }
  298. return nil, nil, err
  299. }
  300. if len(buf) >= maxLineLength {
  301. return nil, nil, errLineTooLong
  302. }
  303. // Parse s3 specific chunk extension and fetch the values.
  304. hexChunkSize, hexChunkSignature := parseS3ChunkExtension(buf)
  305. return hexChunkSize, hexChunkSignature, nil
  306. }
  307. // trimTrailingWhitespace - trim trailing white space.
  308. func trimTrailingWhitespace(b []byte) []byte {
  309. for len(b) > 0 && isASCIISpace(b[len(b)-1]) {
  310. b = b[:len(b)-1]
  311. }
  312. return b
  313. }
  314. // isASCIISpace - is ascii space?
  315. func isASCIISpace(b byte) bool {
  316. return b == ' ' || b == '\t' || b == '\n' || b == '\r'
  317. }
  318. // Constant s3 chunk encoding signature.
  319. const s3ChunkSignatureStr = ";chunk-signature="
  320. // parses3ChunkExtension removes any s3 specific chunk-extension from buf.
  321. // For example,
  322. // "10000;chunk-signature=..." => "10000", "chunk-signature=..."
  323. func parseS3ChunkExtension(buf []byte) ([]byte, []byte) {
  324. buf = trimTrailingWhitespace(buf)
  325. semi := bytes.Index(buf, []byte(s3ChunkSignatureStr))
  326. // Chunk signature not found, return the whole buffer.
  327. if semi == -1 {
  328. return buf, nil
  329. }
  330. return buf[:semi], parseChunkSignature(buf[semi:])
  331. }
  332. // parseChunkSignature - parse chunk signature.
  333. func parseChunkSignature(chunk []byte) []byte {
  334. chunkSplits := bytes.SplitN(chunk, []byte(s3ChunkSignatureStr), 2)
  335. return chunkSplits[1]
  336. }
  337. // parse hex to uint64.
  338. func parseHexUint(v []byte) (n uint64, err error) {
  339. for i, b := range v {
  340. switch {
  341. case '0' <= b && b <= '9':
  342. b = b - '0'
  343. case 'a' <= b && b <= 'f':
  344. b = b - 'a' + 10
  345. case 'A' <= b && b <= 'F':
  346. b = b - 'A' + 10
  347. default:
  348. return 0, errors.New("invalid byte in chunk length")
  349. }
  350. if i == 16 {
  351. return 0, errors.New("http chunk length too large")
  352. }
  353. n <<= 4
  354. n |= uint64(b)
  355. }
  356. return
  357. }