chunked_reader_v4.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. "hash"
  25. "io"
  26. "net/http"
  27. "time"
  28. "github.com/seaweedfs/seaweedfs/weed/glog"
  29. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  30. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  31. "github.com/dustin/go-humanize"
  32. )
  33. // calculateSeedSignature - Calculate seed signature in accordance with
  34. // - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html
  35. //
  36. // returns signature, error otherwise if the signature mismatches or any other
  37. // error while parsing and validating.
  38. func (iam *IdentityAccessManagement) calculateSeedSignature(r *http.Request) (cred *Credential, signature string, region string, date time.Time, errCode s3err.ErrorCode) {
  39. // Copy request.
  40. req := *r
  41. // Save authorization header.
  42. v4Auth := req.Header.Get("Authorization")
  43. // Parse signature version '4' header.
  44. signV4Values, errCode := parseSignV4(v4Auth)
  45. if errCode != s3err.ErrNone {
  46. return nil, "", "", time.Time{}, errCode
  47. }
  48. contentSha256Header := req.Header.Get("X-Amz-Content-Sha256")
  49. switch contentSha256Header {
  50. // Payload for STREAMING signature should be 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD'
  51. case streamingContentSHA256:
  52. glog.V(3).Infof("streaming content sha256")
  53. case streamingUnsignedPayload:
  54. glog.V(3).Infof("streaming unsigned payload")
  55. default:
  56. return nil, "", "", time.Time{}, s3err.ErrContentSHA256Mismatch
  57. }
  58. // Payload streaming.
  59. payload := contentSha256Header
  60. // Extract all the signed headers along with its values.
  61. extractedSignedHeaders, errCode := extractSignedHeaders(signV4Values.SignedHeaders, r)
  62. if errCode != s3err.ErrNone {
  63. return nil, "", "", time.Time{}, errCode
  64. }
  65. // Verify if the access key id matches.
  66. identity, cred, found := iam.lookupByAccessKey(signV4Values.Credential.accessKey)
  67. if !found {
  68. return nil, "", "", time.Time{}, s3err.ErrInvalidAccessKeyID
  69. }
  70. bucket, object := s3_constants.GetBucketAndObject(r)
  71. if !identity.canDo(s3_constants.ACTION_WRITE, bucket, object) {
  72. errCode = s3err.ErrAccessDenied
  73. return
  74. }
  75. // Verify if region is valid.
  76. region = signV4Values.Credential.scope.region
  77. // Extract date, if not present throw error.
  78. var dateStr string
  79. if dateStr = req.Header.Get(http.CanonicalHeaderKey("x-amz-date")); dateStr == "" {
  80. if dateStr = r.Header.Get("Date"); dateStr == "" {
  81. return nil, "", "", time.Time{}, s3err.ErrMissingDateHeader
  82. }
  83. }
  84. // Parse date header.
  85. var err error
  86. date, err = time.Parse(iso8601Format, dateStr)
  87. if err != nil {
  88. return nil, "", "", time.Time{}, s3err.ErrMalformedDate
  89. }
  90. // Query string.
  91. queryStr := req.URL.Query().Encode()
  92. // Get canonical request.
  93. canonicalRequest := getCanonicalRequest(extractedSignedHeaders, payload, queryStr, req.URL.Path, req.Method)
  94. // Get string to sign from canonical request.
  95. stringToSign := getStringToSign(canonicalRequest, date, signV4Values.Credential.getScope())
  96. // Calculate signature.
  97. newSignature := iam.getSignature(
  98. cred.SecretKey,
  99. signV4Values.Credential.scope.date,
  100. region,
  101. "s3",
  102. stringToSign,
  103. )
  104. // Verify if signature match.
  105. if !compareSignatureV4(newSignature, signV4Values.Signature) {
  106. return nil, "", "", time.Time{}, s3err.ErrSignatureDoesNotMatch
  107. }
  108. // Return calculated signature.
  109. return cred, newSignature, region, date, s3err.ErrNone
  110. }
  111. const maxLineLength = 4 * humanize.KiByte // assumed <= bufio.defaultBufSize 4KiB
  112. // lineTooLong is generated as chunk header is bigger than 4KiB.
  113. var errLineTooLong = errors.New("header line too long")
  114. // Malformed encoding is generated when chunk header is wrongly formed.
  115. var errMalformedEncoding = errors.New("malformed chunked encoding")
  116. // newSignV4ChunkedReader returns a new s3ChunkedReader that translates the data read from r
  117. // out of HTTP "chunked" format before returning it.
  118. // The s3ChunkedReader returns io.EOF when the final 0-length chunk is read.
  119. func (iam *IdentityAccessManagement) newSignV4ChunkedReader(req *http.Request) (io.ReadCloser, s3err.ErrorCode) {
  120. ident, seedSignature, region, seedDate, errCode := iam.calculateSeedSignature(req)
  121. if errCode != s3err.ErrNone {
  122. return nil, errCode
  123. }
  124. return &s3ChunkedReader{
  125. cred: ident,
  126. reader: bufio.NewReader(req.Body),
  127. seedSignature: seedSignature,
  128. seedDate: seedDate,
  129. region: region,
  130. chunkSHA256Writer: sha256.New(),
  131. state: readChunkHeader,
  132. iam: iam,
  133. }, s3err.ErrNone
  134. }
  135. // Represents the overall state that is required for decoding a
  136. // AWS Signature V4 chunked reader.
  137. type s3ChunkedReader struct {
  138. cred *Credential
  139. reader *bufio.Reader
  140. seedSignature string
  141. seedDate time.Time
  142. region string
  143. state chunkState
  144. lastChunk bool
  145. chunkSignature string
  146. chunkSHA256Writer hash.Hash // Calculates sha256 of chunk data.
  147. n uint64 // Unread bytes in chunk
  148. err error
  149. iam *IdentityAccessManagement
  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 := cr.getChunkSignature(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. // getChunkSignature - get chunk signature.
  277. func (cr *s3ChunkedReader) getChunkSignature(hashedChunk string) string {
  278. // Calculate string to sign.
  279. stringToSign := signV4ChunkedAlgorithm + "\n" +
  280. cr.seedDate.Format(iso8601Format) + "\n" +
  281. getScope(cr.seedDate, cr.region) + "\n" +
  282. cr.seedSignature + "\n" +
  283. emptySHA256 + "\n" +
  284. hashedChunk
  285. // Calculate signature.
  286. return cr.iam.getSignature(
  287. cr.cred.SecretKey,
  288. cr.seedDate,
  289. cr.region,
  290. "s3",
  291. stringToSign,
  292. )
  293. }
  294. // readCRLF - check if reader only has '\r\n' CRLF character.
  295. // returns malformed encoding if it doesn't.
  296. func readCRLF(reader io.Reader) error {
  297. buf := make([]byte, 2)
  298. _, err := io.ReadFull(reader, buf[:2])
  299. if err != nil {
  300. return err
  301. }
  302. if buf[0] != '\r' || buf[1] != '\n' {
  303. return errMalformedEncoding
  304. }
  305. return nil
  306. }
  307. // Read a line of bytes (up to \n) from b.
  308. // Give up if the line exceeds maxLineLength.
  309. // The returned bytes are owned by the bufio.Reader
  310. // so they are only valid until the next bufio read.
  311. func readChunkLine(b *bufio.Reader) ([]byte, []byte, error) {
  312. buf, err := b.ReadSlice('\n')
  313. if err != nil {
  314. // We always know when EOF is coming.
  315. // If the caller asked for a line, there should be a line.
  316. if err == io.EOF {
  317. err = io.ErrUnexpectedEOF
  318. } else if err == bufio.ErrBufferFull {
  319. err = errLineTooLong
  320. }
  321. return nil, nil, err
  322. }
  323. if len(buf) >= maxLineLength {
  324. return nil, nil, errLineTooLong
  325. }
  326. // Parse s3 specific chunk extension and fetch the values.
  327. hexChunkSize, hexChunkSignature := parseS3ChunkExtension(buf)
  328. return hexChunkSize, hexChunkSignature, nil
  329. }
  330. // trimTrailingWhitespace - trim trailing white space.
  331. func trimTrailingWhitespace(b []byte) []byte {
  332. for len(b) > 0 && isASCIISpace(b[len(b)-1]) {
  333. b = b[:len(b)-1]
  334. }
  335. return b
  336. }
  337. // isASCIISpace - is ascii space?
  338. func isASCIISpace(b byte) bool {
  339. return b == ' ' || b == '\t' || b == '\n' || b == '\r'
  340. }
  341. // Constant s3 chunk encoding signature.
  342. const s3ChunkSignatureStr = ";chunk-signature="
  343. // parses3ChunkExtension removes any s3 specific chunk-extension from buf.
  344. // For example,
  345. //
  346. // "10000;chunk-signature=..." => "10000", "chunk-signature=..."
  347. func parseS3ChunkExtension(buf []byte) ([]byte, []byte) {
  348. buf = trimTrailingWhitespace(buf)
  349. semi := bytes.Index(buf, []byte(s3ChunkSignatureStr))
  350. // Chunk signature not found, return the whole buffer.
  351. if semi == -1 {
  352. return buf, nil
  353. }
  354. return buf[:semi], parseChunkSignature(buf[semi:])
  355. }
  356. // parseChunkSignature - parse chunk signature.
  357. func parseChunkSignature(chunk []byte) []byte {
  358. chunkSplits := bytes.SplitN(chunk, []byte(s3ChunkSignatureStr), 2)
  359. return chunkSplits[1]
  360. }
  361. // parse hex to uint64.
  362. func parseHexUint(v []byte) (n uint64, err error) {
  363. for i, b := range v {
  364. switch {
  365. case '0' <= b && b <= '9':
  366. b = b - '0'
  367. case 'a' <= b && b <= 'f':
  368. b = b - 'a' + 10
  369. case 'A' <= b && b <= 'F':
  370. b = b - 'A' + 10
  371. default:
  372. return 0, errors.New("invalid byte in chunk length")
  373. }
  374. if i == 16 {
  375. return 0, errors.New("http chunk length too large")
  376. }
  377. n <<= 4
  378. n |= uint64(b)
  379. }
  380. return
  381. }