You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
248 lines
7.3 KiB
248 lines
7.3 KiB
9 years ago
|
/*
|
||
|
* Minio Cloud Storage, (C) 2015 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.
|
||
|
*/
|
||
|
|
||
8 years ago
|
package cmd
|
||
9 years ago
|
|
||
|
import (
|
||
|
"net/url"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
9 years ago
|
// credentialHeader data type represents structured form of Credential
|
||
9 years ago
|
// string from authorization header.
|
||
9 years ago
|
type credentialHeader struct {
|
||
|
accessKey string
|
||
|
scope struct {
|
||
9 years ago
|
date time.Time
|
||
|
region string
|
||
|
service string
|
||
|
request string
|
||
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
// parse credentialHeader string into its structured form.
|
||
9 years ago
|
func parseCredentialHeader(credElement string) (credentialHeader, APIErrorCode) {
|
||
9 years ago
|
creds := strings.Split(strings.TrimSpace(credElement), "=")
|
||
|
if len(creds) != 2 {
|
||
9 years ago
|
return credentialHeader{}, ErrMissingFields
|
||
9 years ago
|
}
|
||
|
if creds[0] != "Credential" {
|
||
9 years ago
|
return credentialHeader{}, ErrMissingCredTag
|
||
9 years ago
|
}
|
||
|
credElements := strings.Split(strings.TrimSpace(creds[1]), "/")
|
||
|
if len(credElements) != 5 {
|
||
9 years ago
|
return credentialHeader{}, ErrCredMalformed
|
||
9 years ago
|
}
|
||
|
if !isValidAccessKey.MatchString(credElements[0]) {
|
||
9 years ago
|
return credentialHeader{}, ErrInvalidAccessKeyID
|
||
9 years ago
|
}
|
||
9 years ago
|
// Save access key id.
|
||
|
cred := credentialHeader{
|
||
|
accessKey: credElements[0],
|
||
9 years ago
|
}
|
||
|
var e error
|
||
|
cred.scope.date, e = time.Parse(yyyymmdd, credElements[1])
|
||
|
if e != nil {
|
||
9 years ago
|
return credentialHeader{}, ErrMalformedCredentialDate
|
||
9 years ago
|
}
|
||
|
if credElements[2] == "" {
|
||
9 years ago
|
return credentialHeader{}, ErrMalformedCredentialRegion
|
||
9 years ago
|
}
|
||
|
cred.scope.region = credElements[2]
|
||
|
if credElements[3] != "s3" {
|
||
9 years ago
|
return credentialHeader{}, ErrInvalidService
|
||
9 years ago
|
}
|
||
|
cred.scope.service = credElements[3]
|
||
|
if credElements[4] != "aws4_request" {
|
||
9 years ago
|
return credentialHeader{}, ErrInvalidRequestVersion
|
||
9 years ago
|
}
|
||
|
cred.scope.request = credElements[4]
|
||
9 years ago
|
return cred, ErrNone
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Parse signature string.
|
||
9 years ago
|
func parseSignature(signElement string) (string, APIErrorCode) {
|
||
9 years ago
|
signFields := strings.Split(strings.TrimSpace(signElement), "=")
|
||
|
if len(signFields) != 2 {
|
||
9 years ago
|
return "", ErrMissingFields
|
||
9 years ago
|
}
|
||
|
if signFields[0] != "Signature" {
|
||
9 years ago
|
return "", ErrMissingSignTag
|
||
9 years ago
|
}
|
||
|
signature := signFields[1]
|
||
9 years ago
|
return signature, ErrNone
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// Parse signed headers string.
|
||
9 years ago
|
func parseSignedHeaders(signedHdrElement string) ([]string, APIErrorCode) {
|
||
9 years ago
|
signedHdrFields := strings.Split(strings.TrimSpace(signedHdrElement), "=")
|
||
|
if len(signedHdrFields) != 2 {
|
||
9 years ago
|
return nil, ErrMissingFields
|
||
9 years ago
|
}
|
||
|
if signedHdrFields[0] != "SignedHeaders" {
|
||
9 years ago
|
return nil, ErrMissingSignHeadersTag
|
||
9 years ago
|
}
|
||
|
signedHeaders := strings.Split(signedHdrFields[1], ";")
|
||
9 years ago
|
return signedHeaders, ErrNone
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
// signValues data type represents structured form of AWS Signature V4 header.
|
||
9 years ago
|
type signValues struct {
|
||
9 years ago
|
Credential credentialHeader
|
||
9 years ago
|
SignedHeaders []string
|
||
|
Signature string
|
||
|
}
|
||
|
|
||
9 years ago
|
// preSignValues data type represents structued form of AWS Signature V4 query string.
|
||
9 years ago
|
type preSignValues struct {
|
||
|
signValues
|
||
|
Date time.Time
|
||
|
Expires time.Duration
|
||
|
}
|
||
|
|
||
|
// Parses signature version '4' query string of the following form.
|
||
|
//
|
||
|
// querystring = X-Amz-Algorithm=algorithm
|
||
9 years ago
|
// querystring += &X-Amz-Credential= urlencode(accessKey + '/' + credential_scope)
|
||
9 years ago
|
// querystring += &X-Amz-Date=date
|
||
|
// querystring += &X-Amz-Expires=timeout interval
|
||
|
// querystring += &X-Amz-SignedHeaders=signed_headers
|
||
|
// querystring += &X-Amz-Signature=signature
|
||
9 years ago
|
//{
|
||
|
|
||
|
// verifies if any of the necessary query params are missing in the presigned request.
|
||
|
func doesV4PresignParamsExist(query url.Values) APIErrorCode {
|
||
|
v4PresignQueryParams := []string{"X-Amz-Algorithm", "X-Amz-Credential", "X-Amz-Signature", "X-Amz-Date", "X-Amz-SignedHeaders", "X-Amz-Expires"}
|
||
|
for _, v4PresignQueryParam := range v4PresignQueryParams {
|
||
|
if _, ok := query[v4PresignQueryParam]; !ok {
|
||
|
return ErrInvalidQueryParams
|
||
|
}
|
||
|
}
|
||
|
return ErrNone
|
||
|
}
|
||
|
|
||
9 years ago
|
func parsePreSignV4(query url.Values) (preSignValues, APIErrorCode) {
|
||
9 years ago
|
var err APIErrorCode
|
||
|
// verify whether the required query params exist.
|
||
|
err = doesV4PresignParamsExist(query)
|
||
|
if err != ErrNone {
|
||
|
return preSignValues{}, err
|
||
|
}
|
||
|
|
||
9 years ago
|
// Verify if the query algorithm is supported or not.
|
||
|
if query.Get("X-Amz-Algorithm") != signV4Algorithm {
|
||
9 years ago
|
return preSignValues{}, ErrInvalidQuerySignatureAlgo
|
||
9 years ago
|
}
|
||
|
|
||
|
// Initialize signature version '4' structured header.
|
||
|
preSignV4Values := preSignValues{}
|
||
|
|
||
9 years ago
|
// Save credential.
|
||
9 years ago
|
preSignV4Values.Credential, err = parseCredentialHeader("Credential=" + query.Get("X-Amz-Credential"))
|
||
9 years ago
|
if err != ErrNone {
|
||
|
return preSignValues{}, err
|
||
9 years ago
|
}
|
||
|
|
||
|
var e error
|
||
|
// Save date in native time.Time.
|
||
|
preSignV4Values.Date, e = time.Parse(iso8601Format, query.Get("X-Amz-Date"))
|
||
|
if e != nil {
|
||
9 years ago
|
return preSignValues{}, ErrMalformedPresignedDate
|
||
9 years ago
|
}
|
||
|
|
||
|
// Save expires in native time.Duration.
|
||
|
preSignV4Values.Expires, e = time.ParseDuration(query.Get("X-Amz-Expires") + "s")
|
||
|
if e != nil {
|
||
9 years ago
|
return preSignValues{}, ErrMalformedExpires
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
if preSignV4Values.Expires < 0 {
|
||
|
return preSignValues{}, ErrNegativeExpires
|
||
|
}
|
||
9 years ago
|
// Save signed headers.
|
||
9 years ago
|
preSignV4Values.SignedHeaders, err = parseSignedHeaders("SignedHeaders=" + query.Get("X-Amz-SignedHeaders"))
|
||
9 years ago
|
if err != ErrNone {
|
||
|
return preSignValues{}, err
|
||
9 years ago
|
}
|
||
9 years ago
|
// `host` is the only header used during the presigned request.
|
||
|
// Malformed signed headers has be caught here, otherwise it'll lead to signature mismatch.
|
||
|
if preSignV4Values.SignedHeaders[0] != "host" {
|
||
|
return preSignValues{}, ErrUnsignedHeaders
|
||
|
}
|
||
9 years ago
|
|
||
|
// Save signature.
|
||
9 years ago
|
preSignV4Values.Signature, err = parseSignature("Signature=" + query.Get("X-Amz-Signature"))
|
||
9 years ago
|
if err != ErrNone {
|
||
|
return preSignValues{}, err
|
||
9 years ago
|
}
|
||
|
|
||
|
// Return structed form of signature query string.
|
||
9 years ago
|
return preSignV4Values, ErrNone
|
||
9 years ago
|
}
|
||
|
|
||
|
// Parses signature version '4' header of the following form.
|
||
|
//
|
||
9 years ago
|
// Authorization: algorithm Credential=accessKeyID/credScope, \
|
||
|
// SignedHeaders=signedHeaders, Signature=signature
|
||
9 years ago
|
//
|
||
9 years ago
|
func parseSignV4(v4Auth string) (signValues, APIErrorCode) {
|
||
9 years ago
|
// Replace all spaced strings, some clients can send spaced
|
||
|
// parameters and some won't. So we pro-actively remove any spaces
|
||
|
// to make parsing easier.
|
||
|
v4Auth = strings.Replace(v4Auth, " ", "", -1)
|
||
|
if v4Auth == "" {
|
||
9 years ago
|
return signValues{}, ErrAuthHeaderEmpty
|
||
9 years ago
|
}
|
||
|
|
||
|
// Verify if the header algorithm is supported or not.
|
||
|
if !strings.HasPrefix(v4Auth, signV4Algorithm) {
|
||
9 years ago
|
return signValues{}, ErrSignatureVersionNotSupported
|
||
9 years ago
|
}
|
||
|
|
||
|
// Strip off the Algorithm prefix.
|
||
|
v4Auth = strings.TrimPrefix(v4Auth, signV4Algorithm)
|
||
|
authFields := strings.Split(strings.TrimSpace(v4Auth), ",")
|
||
|
if len(authFields) != 3 {
|
||
9 years ago
|
return signValues{}, ErrMissingFields
|
||
9 years ago
|
}
|
||
|
|
||
|
// Initialize signature version '4' structured header.
|
||
|
signV4Values := signValues{}
|
||
|
|
||
9 years ago
|
var err APIErrorCode
|
||
9 years ago
|
// Save credentail values.
|
||
9 years ago
|
signV4Values.Credential, err = parseCredentialHeader(authFields[0])
|
||
9 years ago
|
if err != ErrNone {
|
||
|
return signValues{}, err
|
||
9 years ago
|
}
|
||
|
|
||
|
// Save signed headers.
|
||
|
signV4Values.SignedHeaders, err = parseSignedHeaders(authFields[1])
|
||
9 years ago
|
if err != ErrNone {
|
||
|
return signValues{}, err
|
||
9 years ago
|
}
|
||
|
|
||
|
// Save signature.
|
||
|
signV4Values.Signature, err = parseSignature(authFields[2])
|
||
9 years ago
|
if err != ErrNone {
|
||
|
return signValues{}, err
|
||
9 years ago
|
}
|
||
|
|
||
|
// Return the structure here.
|
||
9 years ago
|
return signV4Values, ErrNone
|
||
9 years ago
|
}
|