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.
328 lines
10 KiB
328 lines
10 KiB
10 years ago
|
/*
|
||
10 years ago
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
||
10 years ago
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
9 years ago
|
package fs
|
||
10 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/hmac"
|
||
|
"encoding/hex"
|
||
|
"net/http"
|
||
9 years ago
|
"net/url"
|
||
10 years ago
|
"regexp"
|
||
|
"sort"
|
||
9 years ago
|
"strconv"
|
||
10 years ago
|
"strings"
|
||
|
"time"
|
||
|
"unicode/utf8"
|
||
|
|
||
9 years ago
|
"github.com/minio/minio-xl/pkg/crypto/sha256"
|
||
|
"github.com/minio/minio-xl/pkg/probe"
|
||
10 years ago
|
)
|
||
|
|
||
10 years ago
|
// Signature - local variables
|
||
|
type Signature struct {
|
||
|
AccessKeyID string
|
||
|
SecretAccessKey string
|
||
9 years ago
|
Presigned bool
|
||
9 years ago
|
PresignedPolicy string
|
||
9 years ago
|
SignedHeaders []string
|
||
|
Signature string
|
||
10 years ago
|
Request *http.Request
|
||
10 years ago
|
}
|
||
|
|
||
|
const (
|
||
10 years ago
|
authHeaderPrefix = "AWS4-HMAC-SHA256"
|
||
|
iso8601Format = "20060102T150405Z"
|
||
|
yyyymmdd = "20060102"
|
||
10 years ago
|
)
|
||
|
|
||
|
// sumHMAC calculate hmac between two input byte array
|
||
|
func sumHMAC(key []byte, data []byte) []byte {
|
||
|
hash := hmac.New(sha256.New, key)
|
||
|
hash.Write(data)
|
||
|
return hash.Sum(nil)
|
||
|
}
|
||
|
|
||
9 years ago
|
// getURLEncodedName encode the strings from UTF-8 byte representations to HTML hex escape sequences
|
||
10 years ago
|
//
|
||
|
// This is necessary since regular url.Parse() and url.Encode() functions do not support UTF-8
|
||
|
// non english characters cannot be parsed due to the nature in which url.Encode() is written
|
||
|
//
|
||
|
// This function on the other hand is a direct replacement for url.Encode() technique to support
|
||
|
// pretty much every UTF-8 character.
|
||
9 years ago
|
func getURLEncodedName(name string) string {
|
||
10 years ago
|
// if object matches reserved string, no need to encode them
|
||
|
reservedNames := regexp.MustCompile("^[a-zA-Z0-9-_.~/]+$")
|
||
|
if reservedNames.MatchString(name) {
|
||
9 years ago
|
return name
|
||
10 years ago
|
}
|
||
|
var encodedName string
|
||
|
for _, s := range name {
|
||
|
if 'A' <= s && s <= 'Z' || 'a' <= s && s <= 'z' || '0' <= s && s <= '9' { // §2.3 Unreserved characters (mark)
|
||
|
encodedName = encodedName + string(s)
|
||
|
continue
|
||
|
}
|
||
|
switch s {
|
||
|
case '-', '_', '.', '~', '/': // §2.3 Unreserved characters (mark)
|
||
|
encodedName = encodedName + string(s)
|
||
|
continue
|
||
|
default:
|
||
|
len := utf8.RuneLen(s)
|
||
|
if len < 0 {
|
||
9 years ago
|
return name
|
||
10 years ago
|
}
|
||
|
u := make([]byte, len)
|
||
|
utf8.EncodeRune(u, s)
|
||
|
for _, r := range u {
|
||
|
hex := hex.EncodeToString([]byte{r})
|
||
|
encodedName = encodedName + "%" + strings.ToUpper(hex)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
9 years ago
|
return encodedName
|
||
10 years ago
|
}
|
||
|
|
||
|
// getCanonicalHeaders generate a list of request headers with their values
|
||
9 years ago
|
func (r Signature) getCanonicalHeaders(signedHeaders map[string][]string) string {
|
||
10 years ago
|
var headers []string
|
||
|
vals := make(map[string][]string)
|
||
10 years ago
|
for k, vv := range signedHeaders {
|
||
10 years ago
|
headers = append(headers, strings.ToLower(k))
|
||
|
vals[strings.ToLower(k)] = vv
|
||
|
}
|
||
|
headers = append(headers, "host")
|
||
|
sort.Strings(headers)
|
||
|
|
||
|
var buf bytes.Buffer
|
||
|
for _, k := range headers {
|
||
|
buf.WriteString(k)
|
||
|
buf.WriteByte(':')
|
||
|
switch {
|
||
|
case k == "host":
|
||
10 years ago
|
buf.WriteString(r.Request.Host)
|
||
10 years ago
|
fallthrough
|
||
|
default:
|
||
|
for idx, v := range vals[k] {
|
||
|
if idx > 0 {
|
||
|
buf.WriteByte(',')
|
||
|
}
|
||
|
buf.WriteString(v)
|
||
|
}
|
||
|
buf.WriteByte('\n')
|
||
|
}
|
||
|
}
|
||
|
return buf.String()
|
||
|
}
|
||
|
|
||
|
// getSignedHeaders generate a string i.e alphabetically sorted, semicolon-separated list of lowercase request header names
|
||
9 years ago
|
func (r Signature) getSignedHeaders(signedHeaders map[string][]string) string {
|
||
10 years ago
|
var headers []string
|
||
10 years ago
|
for k := range signedHeaders {
|
||
10 years ago
|
headers = append(headers, strings.ToLower(k))
|
||
|
}
|
||
|
headers = append(headers, "host")
|
||
|
sort.Strings(headers)
|
||
|
return strings.Join(headers, ";")
|
||
|
}
|
||
|
|
||
10 years ago
|
// extractSignedHeaders extract signed headers from Authorization header
|
||
9 years ago
|
func (r Signature) extractSignedHeaders() map[string][]string {
|
||
10 years ago
|
extractedSignedHeadersMap := make(map[string][]string)
|
||
9 years ago
|
for _, header := range r.SignedHeaders {
|
||
10 years ago
|
val, ok := r.Request.Header[http.CanonicalHeaderKey(header)]
|
||
|
if !ok {
|
||
|
// if not found continue, we will fail later
|
||
|
continue
|
||
|
}
|
||
|
extractedSignedHeadersMap[header] = val
|
||
|
}
|
||
|
return extractedSignedHeadersMap
|
||
|
}
|
||
|
|
||
10 years ago
|
// getCanonicalRequest generate a canonical request of style
|
||
|
//
|
||
|
// canonicalRequest =
|
||
|
// <HTTPMethod>\n
|
||
|
// <CanonicalURI>\n
|
||
|
// <CanonicalQueryString>\n
|
||
|
// <CanonicalHeaders>\n
|
||
|
// <SignedHeaders>\n
|
||
|
// <HashedPayload>
|
||
|
//
|
||
10 years ago
|
func (r *Signature) getCanonicalRequest() string {
|
||
9 years ago
|
payload := r.Request.Header.Get(http.CanonicalHeaderKey("x-amz-content-sha256"))
|
||
10 years ago
|
r.Request.URL.RawQuery = strings.Replace(r.Request.URL.Query().Encode(), "+", "%20", -1)
|
||
9 years ago
|
encodedPath := getURLEncodedName(r.Request.URL.Path)
|
||
10 years ago
|
// convert any space strings back to "+"
|
||
|
encodedPath = strings.Replace(encodedPath, "+", "%20", -1)
|
||
|
canonicalRequest := strings.Join([]string{
|
||
10 years ago
|
r.Request.Method,
|
||
10 years ago
|
encodedPath,
|
||
10 years ago
|
r.Request.URL.RawQuery,
|
||
10 years ago
|
r.getCanonicalHeaders(r.extractSignedHeaders()),
|
||
|
r.getSignedHeaders(r.extractSignedHeaders()),
|
||
9 years ago
|
payload,
|
||
|
}, "\n")
|
||
|
return canonicalRequest
|
||
|
}
|
||
|
|
||
|
// getCanonicalRequest generate a canonical request of style
|
||
|
//
|
||
|
// canonicalRequest =
|
||
|
// <HTTPMethod>\n
|
||
|
// <CanonicalURI>\n
|
||
|
// <CanonicalQueryString>\n
|
||
|
// <CanonicalHeaders>\n
|
||
|
// <SignedHeaders>\n
|
||
|
// <HashedPayload>
|
||
|
//
|
||
9 years ago
|
func (r Signature) getPresignedCanonicalRequest(presignedQuery string) string {
|
||
9 years ago
|
rawQuery := strings.Replace(presignedQuery, "+", "%20", -1)
|
||
9 years ago
|
encodedPath := getURLEncodedName(r.Request.URL.Path)
|
||
9 years ago
|
// convert any space strings back to "+"
|
||
|
encodedPath = strings.Replace(encodedPath, "+", "%20", -1)
|
||
|
canonicalRequest := strings.Join([]string{
|
||
|
r.Request.Method,
|
||
|
encodedPath,
|
||
|
rawQuery,
|
||
|
r.getCanonicalHeaders(r.extractSignedHeaders()),
|
||
|
r.getSignedHeaders(r.extractSignedHeaders()),
|
||
|
"UNSIGNED-PAYLOAD",
|
||
10 years ago
|
}, "\n")
|
||
|
return canonicalRequest
|
||
|
}
|
||
|
|
||
|
// getScope generate a string of a specific date, an AWS region, and a service
|
||
9 years ago
|
func (r Signature) getScope(t time.Time) string {
|
||
10 years ago
|
scope := strings.Join([]string{
|
||
|
t.Format(yyyymmdd),
|
||
|
"milkyway",
|
||
|
"s3",
|
||
|
"aws4_request",
|
||
|
}, "/")
|
||
|
return scope
|
||
|
}
|
||
|
|
||
|
// getStringToSign a string based on selected query values
|
||
9 years ago
|
func (r Signature) getStringToSign(canonicalRequest string, t time.Time) string {
|
||
10 years ago
|
stringToSign := authHeaderPrefix + "\n" + t.Format(iso8601Format) + "\n"
|
||
10 years ago
|
stringToSign = stringToSign + r.getScope(t) + "\n"
|
||
|
stringToSign = stringToSign + hex.EncodeToString(sha256.Sum256([]byte(canonicalRequest)))
|
||
|
return stringToSign
|
||
|
}
|
||
|
|
||
|
// getSigningKey hmac seed to calculate final signature
|
||
9 years ago
|
func (r Signature) getSigningKey(t time.Time) []byte {
|
||
10 years ago
|
secret := r.SecretAccessKey
|
||
10 years ago
|
date := sumHMAC([]byte("AWS4"+secret), []byte(t.Format(yyyymmdd)))
|
||
|
region := sumHMAC(date, []byte("milkyway"))
|
||
|
service := sumHMAC(region, []byte("s3"))
|
||
|
signingKey := sumHMAC(service, []byte("aws4_request"))
|
||
|
return signingKey
|
||
|
}
|
||
|
|
||
|
// getSignature final signature in hexadecimal form
|
||
9 years ago
|
func (r Signature) getSignature(signingKey []byte, stringToSign string) string {
|
||
10 years ago
|
return hex.EncodeToString(sumHMAC(signingKey, []byte(stringToSign)))
|
||
|
}
|
||
|
|
||
9 years ago
|
// DoesPolicySignatureMatch - Verify query headers with post policy
|
||
|
// - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
|
||
|
// returns true if matches, false otherwise. if error is not nil then it is always false
|
||
9 years ago
|
func (r *Signature) DoesPolicySignatureMatch(date string) (bool, *probe.Error) {
|
||
|
t, err := time.Parse(iso8601Format, date)
|
||
|
if err != nil {
|
||
|
return false, probe.NewError(err)
|
||
|
}
|
||
|
signingKey := r.getSigningKey(t)
|
||
|
stringToSign := string(r.PresignedPolicy)
|
||
|
newSignature := r.getSignature(signingKey, stringToSign)
|
||
|
if newSignature != r.Signature {
|
||
|
return false, nil
|
||
|
}
|
||
9 years ago
|
return true, nil
|
||
|
}
|
||
|
|
||
|
// DoesPresignedSignatureMatch - Verify query headers with presigned signature
|
||
|
// - http://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-query-string-auth.html
|
||
|
// returns true if matches, false otherwise. if error is not nil then it is always false
|
||
|
func (r *Signature) DoesPresignedSignatureMatch() (bool, *probe.Error) {
|
||
|
query := make(url.Values)
|
||
|
query.Set("X-Amz-Algorithm", authHeaderPrefix)
|
||
|
|
||
|
var date string
|
||
|
if date = r.Request.URL.Query().Get("X-Amz-Date"); date == "" {
|
||
|
return false, probe.NewError(MissingDateHeader{})
|
||
|
}
|
||
|
t, err := time.Parse(iso8601Format, date)
|
||
|
if err != nil {
|
||
|
return false, probe.NewError(err)
|
||
|
}
|
||
|
if _, ok := r.Request.URL.Query()["X-Amz-Expires"]; !ok {
|
||
|
return false, probe.NewError(MissingExpiresQuery{})
|
||
|
}
|
||
|
expireSeconds, err := strconv.Atoi(r.Request.URL.Query().Get("X-Amz-Expires"))
|
||
|
if err != nil {
|
||
|
return false, probe.NewError(err)
|
||
|
}
|
||
|
if time.Now().UTC().Sub(t) > time.Duration(expireSeconds)*time.Second {
|
||
|
return false, probe.NewError(ExpiredPresignedRequest{})
|
||
|
}
|
||
|
query.Set("X-Amz-Date", t.Format(iso8601Format))
|
||
|
query.Set("X-Amz-Expires", strconv.Itoa(expireSeconds))
|
||
|
query.Set("X-Amz-SignedHeaders", r.getSignedHeaders(r.extractSignedHeaders()))
|
||
|
query.Set("X-Amz-Credential", r.AccessKeyID+"/"+r.getScope(t))
|
||
|
|
||
|
encodedQuery := query.Encode()
|
||
|
newSignature := r.getSignature(r.getSigningKey(t), r.getStringToSign(r.getPresignedCanonicalRequest(encodedQuery), t))
|
||
|
encodedQuery += "&X-Amz-Signature=" + newSignature
|
||
|
|
||
|
if encodedQuery != r.Request.URL.RawQuery {
|
||
|
return false, nil
|
||
|
}
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
// DoesSignatureMatch - Verify authorization header with calculated header in accordance with
|
||
|
// - http://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html
|
||
|
// returns true if matches, false otherwise. if error is not nil then it is always false
|
||
10 years ago
|
func (r *Signature) DoesSignatureMatch(hashedPayload string) (bool, *probe.Error) {
|
||
10 years ago
|
// set new calulated payload
|
||
10 years ago
|
r.Request.Header.Set("X-Amz-Content-Sha256", hashedPayload)
|
||
10 years ago
|
|
||
9 years ago
|
// Add date if not present throw error
|
||
10 years ago
|
var date string
|
||
10 years ago
|
if date = r.Request.Header.Get(http.CanonicalHeaderKey("x-amz-date")); date == "" {
|
||
10 years ago
|
if date = r.Request.Header.Get("Date"); date == "" {
|
||
10 years ago
|
return false, probe.NewError(MissingDateHeader{})
|
||
10 years ago
|
}
|
||
|
}
|
||
|
t, err := time.Parse(iso8601Format, date)
|
||
|
if err != nil {
|
||
10 years ago
|
return false, probe.NewError(err)
|
||
10 years ago
|
}
|
||
10 years ago
|
canonicalRequest := r.getCanonicalRequest()
|
||
10 years ago
|
stringToSign := r.getStringToSign(canonicalRequest, t)
|
||
|
signingKey := r.getSigningKey(t)
|
||
10 years ago
|
newSignature := r.getSignature(signingKey, stringToSign)
|
||
10 years ago
|
|
||
9 years ago
|
if newSignature != r.Signature {
|
||
10 years ago
|
return false, nil
|
||
|
}
|
||
|
return true, nil
|
||
10 years ago
|
}
|