| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | /* | 
					
						
							|  |  |  |  * Minio Cloud Storage, (C) 2016 Minio, Inc. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | // Package cmd This file implements helper functions to validate Streaming AWS
 | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | // Signature Version '4' authorization header.
 | 
					
						
							| 
									
										
										
										
											2016-08-19 07:23:42 +08:00
										 |  |  | package cmd | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"bufio" | 
					
						
							|  |  |  | 	"bytes" | 
					
						
							|  |  |  | 	"encoding/hex" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"hash" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | 	humanize "github.com/dustin/go-humanize" | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	"github.com/minio/sha256-simd" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Streaming AWS Signature Version '4' constants.
 | 
					
						
							|  |  |  | const ( | 
					
						
							| 
									
										
										
										
											2017-02-21 04:07:03 +08:00
										 |  |  | 	emptySHA256              = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" | 
					
						
							|  |  |  | 	streamingContentSHA256   = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD" | 
					
						
							|  |  |  | 	signV4ChunkedAlgorithm   = "AWS4-HMAC-SHA256-PAYLOAD" | 
					
						
							|  |  |  | 	streamingContentEncoding = "aws-chunked" | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // getChunkSignature - get chunk signature.
 | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | func getChunkSignature(seedSignature string, region string, date time.Time, hashedChunk string) string { | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	// Access credentials.
 | 
					
						
							|  |  |  | 	cred := serverConfig.GetCredential() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Calculate string to sign.
 | 
					
						
							|  |  |  | 	stringToSign := signV4ChunkedAlgorithm + "\n" + | 
					
						
							|  |  |  | 		date.Format(iso8601Format) + "\n" + | 
					
						
							|  |  |  | 		getScope(date, region) + "\n" + | 
					
						
							|  |  |  | 		seedSignature + "\n" + | 
					
						
							|  |  |  | 		emptySHA256 + "\n" + | 
					
						
							|  |  |  | 		hashedChunk | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Get hmac signing key.
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	signingKey := getSigningKey(cred.SecretKey, date, region) | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Calculate signature.
 | 
					
						
							|  |  |  | 	newSignature := getSignature(signingKey, stringToSign) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return newSignature | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // calculateSeedSignature - Calculate seed signature in accordance with
 | 
					
						
							|  |  |  | //     - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-streaming.html
 | 
					
						
							|  |  |  | // returns signature, error otherwise if the signature mismatches or any other
 | 
					
						
							|  |  |  | // error while parsing and validating.
 | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | func calculateSeedSignature(r *http.Request) (signature string, region string, date time.Time, errCode APIErrorCode) { | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	// Access credentials.
 | 
					
						
							|  |  |  | 	cred := serverConfig.GetCredential() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 	// Configured region.
 | 
					
						
							|  |  |  | 	confRegion := serverConfig.GetRegion() | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Copy request.
 | 
					
						
							|  |  |  | 	req := *r | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Save authorization header.
 | 
					
						
							|  |  |  | 	v4Auth := req.Header.Get("Authorization") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Parse signature version '4' header.
 | 
					
						
							|  |  |  | 	signV4Values, errCode := parseSignV4(v4Auth) | 
					
						
							|  |  |  | 	if errCode != ErrNone { | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 		return "", "", time.Time{}, errCode | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Payload streaming.
 | 
					
						
							|  |  |  | 	payload := streamingContentSHA256 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Payload for STREAMING signature should be 'STREAMING-AWS4-HMAC-SHA256-PAYLOAD'
 | 
					
						
							|  |  |  | 	if payload != req.Header.Get("X-Amz-Content-Sha256") { | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 		return "", "", time.Time{}, ErrContentSHA256Mismatch | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Extract all the signed headers along with its values.
 | 
					
						
							| 
									
										
										
										
											2017-04-06 06:08:33 +08:00
										 |  |  | 	extractedSignedHeaders, errCode := extractSignedHeaders(signV4Values.SignedHeaders, r) | 
					
						
							| 
									
										
										
										
											2016-08-10 00:13:15 +08:00
										 |  |  | 	if errCode != ErrNone { | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 		return "", "", time.Time{}, errCode | 
					
						
							| 
									
										
										
										
											2016-08-10 00:13:15 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	// Verify if the access key id matches.
 | 
					
						
							| 
									
										
										
										
											2016-12-27 02:21:23 +08:00
										 |  |  | 	if signV4Values.Credential.accessKey != cred.AccessKey { | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 		return "", "", time.Time{}, ErrInvalidAccessKeyID | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Verify if region is valid.
 | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 	region = signV4Values.Credential.scope.region | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	// Should validate region, only if region is set. Some operations
 | 
					
						
							|  |  |  | 	// do not need region validated for example GetBucketLocation.
 | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 	if !isValidRegion(region, confRegion) { | 
					
						
							|  |  |  | 		return "", "", time.Time{}, ErrInvalidRegion | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Extract date, if not present throw error.
 | 
					
						
							|  |  |  | 	var dateStr string | 
					
						
							|  |  |  | 	if dateStr = req.Header.Get(http.CanonicalHeaderKey("x-amz-date")); dateStr == "" { | 
					
						
							|  |  |  | 		if dateStr = r.Header.Get("Date"); dateStr == "" { | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 			return "", "", time.Time{}, ErrMissingDateHeader | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Parse date header.
 | 
					
						
							|  |  |  | 	var err error | 
					
						
							|  |  |  | 	date, err = time.Parse(iso8601Format, dateStr) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		errorIf(err, "Unable to parse date", dateStr) | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 		return "", "", time.Time{}, ErrMalformedDate | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Query string.
 | 
					
						
							|  |  |  | 	queryStr := req.URL.Query().Encode() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Get canonical request.
 | 
					
						
							| 
									
										
										
										
											2017-04-06 08:00:24 +08:00
										 |  |  | 	canonicalRequest := getCanonicalRequest(extractedSignedHeaders, payload, queryStr, req.URL.Path, req.Method) | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Get string to sign from canonical request.
 | 
					
						
							| 
									
										
										
										
											2017-02-07 05:09:09 +08:00
										 |  |  | 	stringToSign := getStringToSign(canonicalRequest, date, signV4Values.Credential.getScope()) | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Get hmac signing key.
 | 
					
						
							| 
									
										
										
										
											2017-02-07 05:09:09 +08:00
										 |  |  | 	signingKey := getSigningKey(cred.SecretKey, signV4Values.Credential.scope.date, region) | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Calculate signature.
 | 
					
						
							|  |  |  | 	newSignature := getSignature(signingKey, stringToSign) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Verify if signature match.
 | 
					
						
							|  |  |  | 	if newSignature != signV4Values.Signature { | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 		return "", "", time.Time{}, ErrSignatureDoesNotMatch | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Return caculated signature.
 | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 	return newSignature, region, date, ErrNone | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 10:18:22 +08:00
										 |  |  | const maxLineLength = 4 * humanize.KiByte // assumed <= bufio.defaultBufSize 4KiB
 | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // lineTooLong is generated as chunk header is bigger than 4KiB.
 | 
					
						
							|  |  |  | var errLineTooLong = errors.New("header line too long") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Malformed encoding is generated when chunk header is wrongly formed.
 | 
					
						
							|  |  |  | var errMalformedEncoding = errors.New("malformed chunked encoding") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // newSignV4ChunkedReader returns a new s3ChunkedReader that translates the data read from r
 | 
					
						
							|  |  |  | // out of HTTP "chunked" format before returning it.
 | 
					
						
							|  |  |  | // The s3ChunkedReader returns io.EOF when the final 0-length chunk is read.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // NewChunkedReader is not needed by normal applications. The http package
 | 
					
						
							|  |  |  | // automatically decodes chunking when reading response bodies.
 | 
					
						
							| 
									
										
										
										
											2017-10-22 13:30:34 +08:00
										 |  |  | func newSignV4ChunkedReader(req *http.Request) (io.ReadCloser, APIErrorCode) { | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 	seedSignature, region, seedDate, errCode := calculateSeedSignature(req) | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	if errCode != ErrNone { | 
					
						
							|  |  |  | 		return nil, errCode | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return &s3ChunkedReader{ | 
					
						
							|  |  |  | 		reader:            bufio.NewReader(req.Body), | 
					
						
							|  |  |  | 		seedSignature:     seedSignature, | 
					
						
							|  |  |  | 		seedDate:          seedDate, | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 		region:            region, | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 		chunkSHA256Writer: sha256.New(), | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | 		state:             readChunkHeader, | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	}, ErrNone | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Represents the overall state that is required for decoding a
 | 
					
						
							|  |  |  | // AWS Signature V4 chunked reader.
 | 
					
						
							|  |  |  | type s3ChunkedReader struct { | 
					
						
							|  |  |  | 	reader            *bufio.Reader | 
					
						
							|  |  |  | 	seedSignature     string | 
					
						
							|  |  |  | 	seedDate          time.Time | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 	region            string | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | 	state             chunkState | 
					
						
							|  |  |  | 	lastChunk         bool | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	chunkSignature    string | 
					
						
							|  |  |  | 	chunkSHA256Writer hash.Hash // Calculates sha256 of chunk data.
 | 
					
						
							|  |  |  | 	n                 uint64    // Unread bytes in chunk
 | 
					
						
							|  |  |  | 	err               error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Read chunk reads the chunk token signature portion.
 | 
					
						
							|  |  |  | func (cr *s3ChunkedReader) readS3ChunkHeader() { | 
					
						
							|  |  |  | 	// Read the first chunk line until CRLF.
 | 
					
						
							|  |  |  | 	var hexChunkSize, hexChunkSignature []byte | 
					
						
							|  |  |  | 	hexChunkSize, hexChunkSignature, cr.err = readChunkLine(cr.reader) | 
					
						
							|  |  |  | 	if cr.err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// <hex>;token=value - converts the hex into its uint64 form.
 | 
					
						
							|  |  |  | 	cr.n, cr.err = parseHexUint(hexChunkSize) | 
					
						
							|  |  |  | 	if cr.err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if cr.n == 0 { | 
					
						
							|  |  |  | 		cr.err = io.EOF | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Save the incoming chunk signature.
 | 
					
						
							|  |  |  | 	cr.chunkSignature = string(hexChunkSignature) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | type chunkState int | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	readChunkHeader chunkState = iota | 
					
						
							|  |  |  | 	readChunkTrailer | 
					
						
							|  |  |  | 	readChunk | 
					
						
							|  |  |  | 	verifyChunk | 
					
						
							| 
									
										
										
										
											2017-02-07 06:19:27 +08:00
										 |  |  | 	eofChunk | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (cs chunkState) String() string { | 
					
						
							|  |  |  | 	stateString := "" | 
					
						
							|  |  |  | 	switch cs { | 
					
						
							|  |  |  | 	case readChunkHeader: | 
					
						
							|  |  |  | 		stateString = "readChunkHeader" | 
					
						
							|  |  |  | 	case readChunkTrailer: | 
					
						
							|  |  |  | 		stateString = "readChunkTrailer" | 
					
						
							|  |  |  | 	case readChunk: | 
					
						
							|  |  |  | 		stateString = "readChunk" | 
					
						
							|  |  |  | 	case verifyChunk: | 
					
						
							|  |  |  | 		stateString = "verifyChunk" | 
					
						
							| 
									
										
										
										
											2017-02-07 06:19:27 +08:00
										 |  |  | 	case eofChunk: | 
					
						
							|  |  |  | 		stateString = "eofChunk" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | 	return stateString | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-22 13:30:34 +08:00
										 |  |  | func (cr *s3ChunkedReader) Close() (err error) { | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | // Read - implements `io.Reader`, which transparently decodes
 | 
					
						
							|  |  |  | // the incoming AWS Signature V4 streaming signature.
 | 
					
						
							|  |  |  | func (cr *s3ChunkedReader) Read(buf []byte) (n int, err error) { | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | 	for { | 
					
						
							|  |  |  | 		switch cr.state { | 
					
						
							|  |  |  | 		case readChunkHeader: | 
					
						
							|  |  |  | 			cr.readS3ChunkHeader() | 
					
						
							|  |  |  | 			// If we're at the end of a chunk.
 | 
					
						
							|  |  |  | 			if cr.n == 0 && cr.err == io.EOF { | 
					
						
							|  |  |  | 				cr.state = readChunkTrailer | 
					
						
							|  |  |  | 				cr.lastChunk = true | 
					
						
							|  |  |  | 				continue | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | 			if cr.err != nil { | 
					
						
							|  |  |  | 				return 0, cr.err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			cr.state = readChunk | 
					
						
							|  |  |  | 		case readChunkTrailer: | 
					
						
							|  |  |  | 			cr.err = readCRLF(cr.reader) | 
					
						
							|  |  |  | 			if cr.err != nil { | 
					
						
							|  |  |  | 				return 0, errMalformedEncoding | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			cr.state = verifyChunk | 
					
						
							|  |  |  | 		case readChunk: | 
					
						
							|  |  |  | 			// There is no more space left in the request buffer.
 | 
					
						
							|  |  |  | 			if len(buf) == 0 { | 
					
						
							|  |  |  | 				return n, nil | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			rbuf := buf | 
					
						
							|  |  |  | 			// The request buffer is larger than the current chunk size.
 | 
					
						
							|  |  |  | 			// Read only the current chunk from the underlying reader.
 | 
					
						
							|  |  |  | 			if uint64(len(rbuf)) > cr.n { | 
					
						
							|  |  |  | 				rbuf = rbuf[:cr.n] | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			var n0 int | 
					
						
							|  |  |  | 			n0, cr.err = cr.reader.Read(rbuf) | 
					
						
							|  |  |  | 			if cr.err != nil { | 
					
						
							|  |  |  | 				// We have lesser than chunk size advertised in chunkHeader, this is 'unexpected'.
 | 
					
						
							|  |  |  | 				if cr.err == io.EOF { | 
					
						
							|  |  |  | 					cr.err = io.ErrUnexpectedEOF | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | 				return 0, cr.err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Calculate sha256.
 | 
					
						
							|  |  |  | 			cr.chunkSHA256Writer.Write(rbuf[:n0]) | 
					
						
							|  |  |  | 			// Update the bytes read into request buffer so far.
 | 
					
						
							|  |  |  | 			n += n0 | 
					
						
							|  |  |  | 			buf = buf[n0:] | 
					
						
							|  |  |  | 			// Update bytes to be read of the current chunk before verifying chunk's signature.
 | 
					
						
							|  |  |  | 			cr.n -= uint64(n0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// If we're at the end of a chunk.
 | 
					
						
							|  |  |  | 			if cr.n == 0 { | 
					
						
							|  |  |  | 				cr.state = readChunkTrailer | 
					
						
							|  |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		case verifyChunk: | 
					
						
							|  |  |  | 			// Calculate the hashed chunk.
 | 
					
						
							|  |  |  | 			hashedChunk := hex.EncodeToString(cr.chunkSHA256Writer.Sum(nil)) | 
					
						
							|  |  |  | 			// Calculate the chunk signature.
 | 
					
						
							| 
									
										
										
										
											2017-06-22 02:30:34 +08:00
										 |  |  | 			newSignature := getChunkSignature(cr.seedSignature, cr.region, cr.seedDate, hashedChunk) | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | 			if cr.chunkSignature != newSignature { | 
					
						
							|  |  |  | 				// Chunk signature doesn't match we return signature does not match.
 | 
					
						
							|  |  |  | 				cr.err = errSignatureMismatch | 
					
						
							|  |  |  | 				return 0, cr.err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			// Newly calculated signature becomes the seed for the next chunk
 | 
					
						
							|  |  |  | 			// this follows the chaining.
 | 
					
						
							|  |  |  | 			cr.seedSignature = newSignature | 
					
						
							|  |  |  | 			cr.chunkSHA256Writer.Reset() | 
					
						
							|  |  |  | 			if cr.lastChunk { | 
					
						
							| 
									
										
										
										
											2017-02-07 06:19:27 +08:00
										 |  |  | 				cr.state = eofChunk | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				cr.state = readChunkHeader | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-07 06:19:27 +08:00
										 |  |  | 		case eofChunk: | 
					
						
							|  |  |  | 			return n, io.EOF | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | // readCRLF - check if reader only has '\r\n' CRLF character.
 | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | // returns malformed encoding if it doesn't.
 | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | func readCRLF(reader io.Reader) error { | 
					
						
							|  |  |  | 	buf := make([]byte, 2) | 
					
						
							|  |  |  | 	_, err := io.ReadFull(reader, buf[:2]) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if buf[0] != '\r' || buf[1] != '\n' { | 
					
						
							|  |  |  | 		return errMalformedEncoding | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-10 16:42:32 +08:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-08-09 11:56:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Read a line of bytes (up to \n) from b.
 | 
					
						
							|  |  |  | // Give up if the line exceeds maxLineLength.
 | 
					
						
							|  |  |  | // The returned bytes are owned by the bufio.Reader
 | 
					
						
							|  |  |  | // so they are only valid until the next bufio read.
 | 
					
						
							|  |  |  | func readChunkLine(b *bufio.Reader) ([]byte, []byte, error) { | 
					
						
							|  |  |  | 	buf, err := b.ReadSlice('\n') | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// We always know when EOF is coming.
 | 
					
						
							|  |  |  | 		// If the caller asked for a line, there should be a line.
 | 
					
						
							|  |  |  | 		if err == io.EOF { | 
					
						
							|  |  |  | 			err = io.ErrUnexpectedEOF | 
					
						
							|  |  |  | 		} else if err == bufio.ErrBufferFull { | 
					
						
							|  |  |  | 			err = errLineTooLong | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return nil, nil, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(buf) >= maxLineLength { | 
					
						
							|  |  |  | 		return nil, nil, errLineTooLong | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// Parse s3 specific chunk extension and fetch the values.
 | 
					
						
							|  |  |  | 	hexChunkSize, hexChunkSignature := parseS3ChunkExtension(buf) | 
					
						
							|  |  |  | 	return hexChunkSize, hexChunkSignature, nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // trimTrailingWhitespace - trim trailing white space.
 | 
					
						
							|  |  |  | func trimTrailingWhitespace(b []byte) []byte { | 
					
						
							|  |  |  | 	for len(b) > 0 && isASCIISpace(b[len(b)-1]) { | 
					
						
							|  |  |  | 		b = b[:len(b)-1] | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return b | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // isASCIISpace - is ascii space?
 | 
					
						
							|  |  |  | func isASCIISpace(b byte) bool { | 
					
						
							|  |  |  | 	return b == ' ' || b == '\t' || b == '\n' || b == '\r' | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Constant s3 chunk encoding signature.
 | 
					
						
							|  |  |  | const s3ChunkSignatureStr = ";chunk-signature=" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // parses3ChunkExtension removes any s3 specific chunk-extension from buf.
 | 
					
						
							|  |  |  | // For example,
 | 
					
						
							|  |  |  | //     "10000;chunk-signature=..." => "10000", "chunk-signature=..."
 | 
					
						
							|  |  |  | func parseS3ChunkExtension(buf []byte) ([]byte, []byte) { | 
					
						
							|  |  |  | 	buf = trimTrailingWhitespace(buf) | 
					
						
							|  |  |  | 	semi := bytes.Index(buf, []byte(s3ChunkSignatureStr)) | 
					
						
							|  |  |  | 	// Chunk signature not found, return the whole buffer.
 | 
					
						
							|  |  |  | 	if semi == -1 { | 
					
						
							|  |  |  | 		return buf, nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return buf[:semi], parseChunkSignature(buf[semi:]) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // parseChunkSignature - parse chunk signature.
 | 
					
						
							|  |  |  | func parseChunkSignature(chunk []byte) []byte { | 
					
						
							|  |  |  | 	chunkSplits := bytes.SplitN(chunk, []byte(s3ChunkSignatureStr), 2) | 
					
						
							|  |  |  | 	return chunkSplits[1] | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // parse hex to uint64.
 | 
					
						
							|  |  |  | func parseHexUint(v []byte) (n uint64, err error) { | 
					
						
							|  |  |  | 	for i, b := range v { | 
					
						
							|  |  |  | 		switch { | 
					
						
							|  |  |  | 		case '0' <= b && b <= '9': | 
					
						
							|  |  |  | 			b = b - '0' | 
					
						
							|  |  |  | 		case 'a' <= b && b <= 'f': | 
					
						
							|  |  |  | 			b = b - 'a' + 10 | 
					
						
							|  |  |  | 		case 'A' <= b && b <= 'F': | 
					
						
							|  |  |  | 			b = b - 'A' + 10 | 
					
						
							|  |  |  | 		default: | 
					
						
							|  |  |  | 			return 0, errors.New("invalid byte in chunk length") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if i == 16 { | 
					
						
							|  |  |  | 			return 0, errors.New("http chunk length too large") | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		n <<= 4 | 
					
						
							|  |  |  | 		n |= uint64(b) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							|  |  |  | } |