chunked_reader_v4.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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, "s3")
  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. identity, cred, found := iam.lookupByAccessKey(signV4Values.Credential.accessKey)
  73. if !found {
  74. return nil, "", "", time.Time{}, s3err.ErrInvalidAccessKeyID
  75. }
  76. bucket, _ := getBucketAndObject(r)
  77. if !identity.canDo("Write", bucket) {
  78. errCode = s3err.ErrAccessDenied
  79. return
  80. }
  81. // Verify if region is valid.
  82. region = signV4Values.Credential.scope.region
  83. // Extract date, if not present throw error.
  84. var dateStr string
  85. if dateStr = req.Header.Get(http.CanonicalHeaderKey("x-amz-date")); dateStr == "" {
  86. if dateStr = r.Header.Get("Date"); dateStr == "" {
  87. return nil, "", "", time.Time{}, s3err.ErrMissingDateHeader
  88. }
  89. }
  90. // Parse date header.
  91. var err error
  92. date, err = time.Parse(iso8601Format, dateStr)
  93. if err != nil {
  94. return nil, "", "", time.Time{}, s3err.ErrMalformedDate
  95. }
  96. // Query string.
  97. queryStr := req.URL.Query().Encode()
  98. // Get canonical request.
  99. canonicalRequest := getCanonicalRequest(extractedSignedHeaders, payload, queryStr, req.URL.Path, req.Method)
  100. // Get string to sign from canonical request.
  101. stringToSign := getStringToSign(canonicalRequest, date, signV4Values.Credential.getScope())
  102. // Get hmac signing key.
  103. signingKey := getSigningKey(cred.SecretKey, signV4Values.Credential.scope.date, region, "s3")
  104. // Calculate signature.
  105. newSignature := getSignature(signingKey, stringToSign)
  106. // Verify if signature match.
  107. if !compareSignatureV4(newSignature, signV4Values.Signature) {
  108. return nil, "", "", time.Time{}, s3err.ErrSignatureDoesNotMatch
  109. }
  110. // Return caculated signature.
  111. return cred, newSignature, region, date, s3err.ErrNone
  112. }
  113. const maxLineLength = 4 * humanize.KiByte // assumed <= bufio.defaultBufSize 4KiB
  114. // lineTooLong is generated as chunk header is bigger than 4KiB.
  115. var errLineTooLong = errors.New("header line too long")
  116. // Malformed encoding is generated when chunk header is wrongly formed.
  117. var errMalformedEncoding = errors.New("malformed chunked encoding")
  118. // newSignV4ChunkedReader returns a new s3ChunkedReader that translates the data read from r
  119. // out of HTTP "chunked" format before returning it.
  120. // The s3ChunkedReader returns io.EOF when the final 0-length chunk is read.
  121. func (iam *IdentityAccessManagement) newSignV4ChunkedReader(req *http.Request) (io.ReadCloser, s3err.ErrorCode) {
  122. ident, seedSignature, region, seedDate, errCode := iam.calculateSeedSignature(req)
  123. if errCode != s3err.ErrNone {
  124. return nil, errCode
  125. }
  126. return &s3ChunkedReader{
  127. cred: ident,
  128. reader: bufio.NewReader(req.Body),
  129. seedSignature: seedSignature,
  130. seedDate: seedDate,
  131. region: region,
  132. chunkSHA256Writer: sha256.New(),
  133. state: readChunkHeader,
  134. }, s3err.ErrNone
  135. }
  136. // Represents the overall state that is required for decoding a
  137. // AWS Signature V4 chunked reader.
  138. type s3ChunkedReader struct {
  139. cred *Credential
  140. reader *bufio.Reader
  141. seedSignature string
  142. seedDate time.Time
  143. region string
  144. state chunkState
  145. lastChunk bool
  146. chunkSignature string
  147. chunkSHA256Writer hash.Hash // Calculates sha256 of chunk data.
  148. n uint64 // Unread bytes in chunk
  149. err error
  150. }
  151. // Read chunk reads the chunk token signature portion.
  152. func (cr *s3ChunkedReader) readS3ChunkHeader() {
  153. // Read the first chunk line until CRLF.
  154. var hexChunkSize, hexChunkSignature []byte
  155. hexChunkSize, hexChunkSignature, cr.err = readChunkLine(cr.reader)
  156. if cr.err != nil {
  157. return
  158. }
  159. // <hex>;token=value - converts the hex into its uint64 form.
  160. cr.n, cr.err = parseHexUint(hexChunkSize)
  161. if cr.err != nil {
  162. return
  163. }
  164. if cr.n == 0 {
  165. cr.err = io.EOF
  166. }
  167. // Save the incoming chunk signature.
  168. cr.chunkSignature = string(hexChunkSignature)
  169. }
  170. type chunkState int
  171. const (
  172. readChunkHeader chunkState = iota
  173. readChunkTrailer
  174. readChunk
  175. verifyChunk
  176. eofChunk
  177. )
  178. func (cs chunkState) String() string {
  179. stateString := ""
  180. switch cs {
  181. case readChunkHeader:
  182. stateString = "readChunkHeader"
  183. case readChunkTrailer:
  184. stateString = "readChunkTrailer"
  185. case readChunk:
  186. stateString = "readChunk"
  187. case verifyChunk:
  188. stateString = "verifyChunk"
  189. case eofChunk:
  190. stateString = "eofChunk"
  191. }
  192. return stateString
  193. }
  194. func (cr *s3ChunkedReader) Close() (err error) {
  195. return nil
  196. }
  197. // Read - implements `io.Reader`, which transparently decodes
  198. // the incoming AWS Signature V4 streaming signature.
  199. func (cr *s3ChunkedReader) Read(buf []byte) (n int, err error) {
  200. for {
  201. switch cr.state {
  202. case readChunkHeader:
  203. cr.readS3ChunkHeader()
  204. // If we're at the end of a chunk.
  205. if cr.n == 0 && cr.err == io.EOF {
  206. cr.state = readChunkTrailer
  207. cr.lastChunk = true
  208. continue
  209. }
  210. if cr.err != nil {
  211. return 0, cr.err
  212. }
  213. cr.state = readChunk
  214. case readChunkTrailer:
  215. cr.err = readCRLF(cr.reader)
  216. if cr.err != nil {
  217. return 0, errMalformedEncoding
  218. }
  219. cr.state = verifyChunk
  220. case readChunk:
  221. // There is no more space left in the request buffer.
  222. if len(buf) == 0 {
  223. return n, nil
  224. }
  225. rbuf := buf
  226. // The request buffer is larger than the current chunk size.
  227. // Read only the current chunk from the underlying reader.
  228. if uint64(len(rbuf)) > cr.n {
  229. rbuf = rbuf[:cr.n]
  230. }
  231. var n0 int
  232. n0, cr.err = cr.reader.Read(rbuf)
  233. if cr.err != nil {
  234. // We have lesser than chunk size advertised in chunkHeader, this is 'unexpected'.
  235. if cr.err == io.EOF {
  236. cr.err = io.ErrUnexpectedEOF
  237. }
  238. return 0, cr.err
  239. }
  240. // Calculate sha256.
  241. cr.chunkSHA256Writer.Write(rbuf[:n0])
  242. // Update the bytes read into request buffer so far.
  243. n += n0
  244. buf = buf[n0:]
  245. // Update bytes to be read of the current chunk before verifying chunk's signature.
  246. cr.n -= uint64(n0)
  247. // If we're at the end of a chunk.
  248. if cr.n == 0 {
  249. cr.state = readChunkTrailer
  250. continue
  251. }
  252. case verifyChunk:
  253. // Calculate the hashed chunk.
  254. hashedChunk := hex.EncodeToString(cr.chunkSHA256Writer.Sum(nil))
  255. // Calculate the chunk signature.
  256. newSignature := getChunkSignature(cr.cred.SecretKey, cr.seedSignature, cr.region, cr.seedDate, hashedChunk)
  257. if !compareSignatureV4(cr.chunkSignature, newSignature) {
  258. // Chunk signature doesn't match we return signature does not match.
  259. cr.err = errors.New("chunk signature does not match")
  260. return 0, cr.err
  261. }
  262. // Newly calculated signature becomes the seed for the next chunk
  263. // this follows the chaining.
  264. cr.seedSignature = newSignature
  265. cr.chunkSHA256Writer.Reset()
  266. if cr.lastChunk {
  267. cr.state = eofChunk
  268. } else {
  269. cr.state = readChunkHeader
  270. }
  271. case eofChunk:
  272. return n, io.EOF
  273. }
  274. }
  275. }
  276. // readCRLF - check if reader only has '\r\n' CRLF character.
  277. // returns malformed encoding if it doesn't.
  278. func readCRLF(reader io.Reader) error {
  279. buf := make([]byte, 2)
  280. _, err := io.ReadFull(reader, buf[:2])
  281. if err != nil {
  282. return err
  283. }
  284. if buf[0] != '\r' || buf[1] != '\n' {
  285. return errMalformedEncoding
  286. }
  287. return nil
  288. }
  289. // Read a line of bytes (up to \n) from b.
  290. // Give up if the line exceeds maxLineLength.
  291. // The returned bytes are owned by the bufio.Reader
  292. // so they are only valid until the next bufio read.
  293. func readChunkLine(b *bufio.Reader) ([]byte, []byte, error) {
  294. buf, err := b.ReadSlice('\n')
  295. if err != nil {
  296. // We always know when EOF is coming.
  297. // If the caller asked for a line, there should be a line.
  298. if err == io.EOF {
  299. err = io.ErrUnexpectedEOF
  300. } else if err == bufio.ErrBufferFull {
  301. err = errLineTooLong
  302. }
  303. return nil, nil, err
  304. }
  305. if len(buf) >= maxLineLength {
  306. return nil, nil, errLineTooLong
  307. }
  308. // Parse s3 specific chunk extension and fetch the values.
  309. hexChunkSize, hexChunkSignature := parseS3ChunkExtension(buf)
  310. return hexChunkSize, hexChunkSignature, nil
  311. }
  312. // trimTrailingWhitespace - trim trailing white space.
  313. func trimTrailingWhitespace(b []byte) []byte {
  314. for len(b) > 0 && isASCIISpace(b[len(b)-1]) {
  315. b = b[:len(b)-1]
  316. }
  317. return b
  318. }
  319. // isASCIISpace - is ascii space?
  320. func isASCIISpace(b byte) bool {
  321. return b == ' ' || b == '\t' || b == '\n' || b == '\r'
  322. }
  323. // Constant s3 chunk encoding signature.
  324. const s3ChunkSignatureStr = ";chunk-signature="
  325. // parses3ChunkExtension removes any s3 specific chunk-extension from buf.
  326. // For example,
  327. // "10000;chunk-signature=..." => "10000", "chunk-signature=..."
  328. func parseS3ChunkExtension(buf []byte) ([]byte, []byte) {
  329. buf = trimTrailingWhitespace(buf)
  330. semi := bytes.Index(buf, []byte(s3ChunkSignatureStr))
  331. // Chunk signature not found, return the whole buffer.
  332. if semi == -1 {
  333. return buf, nil
  334. }
  335. return buf[:semi], parseChunkSignature(buf[semi:])
  336. }
  337. // parseChunkSignature - parse chunk signature.
  338. func parseChunkSignature(chunk []byte) []byte {
  339. chunkSplits := bytes.SplitN(chunk, []byte(s3ChunkSignatureStr), 2)
  340. return chunkSplits[1]
  341. }
  342. // parse hex to uint64.
  343. func parseHexUint(v []byte) (n uint64, err error) {
  344. for i, b := range v {
  345. switch {
  346. case '0' <= b && b <= '9':
  347. b = b - '0'
  348. case 'a' <= b && b <= 'f':
  349. b = b - 'a' + 10
  350. case 'A' <= b && b <= 'F':
  351. b = b - 'A' + 10
  352. default:
  353. return 0, errors.New("invalid byte in chunk length")
  354. }
  355. if i == 16 {
  356. return 0, errors.New("http chunk length too large")
  357. }
  358. n <<= 4
  359. n |= uint64(b)
  360. }
  361. return
  362. }