2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-02-21 20:57:05 -05:00
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-02-15 20:42:39 -05:00
|
|
|
|
|
|
|
import (
|
2018-10-09 17:00:01 -04:00
|
|
|
"net/http"
|
2016-02-15 20:42:39 -05:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2017-10-31 14:54:32 -04:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/auth"
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2016-02-15 20:42:39 -05:00
|
|
|
)
|
|
|
|
|
2016-03-12 19:08:15 -05:00
|
|
|
// credentialHeader data type represents structured form of Credential
|
2016-02-21 20:57:05 -05:00
|
|
|
// string from authorization header.
|
2016-03-12 19:08:15 -05:00
|
|
|
type credentialHeader struct {
|
|
|
|
accessKey string
|
|
|
|
scope struct {
|
2016-02-15 20:42:39 -05:00
|
|
|
date time.Time
|
|
|
|
region string
|
|
|
|
service string
|
|
|
|
request string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 16:09:09 -05:00
|
|
|
// Return scope string.
|
|
|
|
func (c credentialHeader) getScope() string {
|
|
|
|
return strings.Join([]string{
|
|
|
|
c.scope.date.Format(yyyymmdd),
|
|
|
|
c.scope.region,
|
|
|
|
c.scope.service,
|
|
|
|
c.scope.request,
|
2019-08-06 15:08:58 -04:00
|
|
|
}, SlashSeparator)
|
2017-02-06 16:09:09 -05:00
|
|
|
}
|
|
|
|
|
2019-02-27 20:46:55 -05:00
|
|
|
func getReqAccessKeyV4(r *http.Request, region string, stype serviceType) (auth.Credentials, bool, APIErrorCode) {
|
2021-08-08 01:43:01 -04:00
|
|
|
ch, s3Err := parseCredentialHeader("Credential="+r.Form.Get(xhttp.AmzCredential), region, stype)
|
2020-08-24 01:06:22 -04:00
|
|
|
if s3Err != ErrNone {
|
2018-10-09 17:00:01 -04:00
|
|
|
// Strip off the Algorithm prefix.
|
|
|
|
v4Auth := strings.TrimPrefix(r.Header.Get("Authorization"), signV4Algorithm)
|
|
|
|
authFields := strings.Split(strings.TrimSpace(v4Auth), ",")
|
|
|
|
if len(authFields) != 3 {
|
2018-11-07 09:40:03 -05:00
|
|
|
return auth.Credentials{}, false, ErrMissingFields
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
2020-08-24 01:06:22 -04:00
|
|
|
ch, s3Err = parseCredentialHeader(authFields[0], region, stype)
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
return auth.Credentials{}, false, s3Err
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
}
|
2021-08-12 21:07:08 -04:00
|
|
|
return checkKeyValid(r, ch.accessKey)
|
2018-10-09 17:00:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-12 19:08:15 -05:00
|
|
|
// parse credentialHeader string into its structured form.
|
2019-02-27 20:46:55 -05:00
|
|
|
func parseCredentialHeader(credElement string, region string, stype serviceType) (ch credentialHeader, aec APIErrorCode) {
|
2019-03-17 01:45:42 -04:00
|
|
|
creds := strings.SplitN(strings.TrimSpace(credElement), "=", 2)
|
2016-02-15 20:42:39 -05:00
|
|
|
if len(creds) != 2 {
|
2017-06-21 22:53:09 -04:00
|
|
|
return ch, ErrMissingFields
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
if creds[0] != "Credential" {
|
2017-06-21 22:53:09 -04:00
|
|
|
return ch, ErrMissingCredTag
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2019-08-06 15:08:58 -04:00
|
|
|
credElements := strings.Split(strings.TrimSpace(creds[1]), SlashSeparator)
|
2018-09-11 14:17:23 -04:00
|
|
|
if len(credElements) < 5 {
|
2017-06-21 22:53:09 -04:00
|
|
|
return ch, ErrCredMalformed
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2019-08-06 15:08:58 -04:00
|
|
|
accessKey := strings.Join(credElements[:len(credElements)-4], SlashSeparator) // The access key may contain one or more `/`
|
2018-09-11 14:17:23 -04:00
|
|
|
if !auth.IsAccessKeyValid(accessKey) {
|
2017-06-21 22:53:09 -04:00
|
|
|
return ch, ErrInvalidAccessKeyID
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2016-03-12 19:08:15 -05:00
|
|
|
// Save access key id.
|
|
|
|
cred := credentialHeader{
|
2018-09-11 14:17:23 -04:00
|
|
|
accessKey: accessKey,
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2018-09-11 14:17:23 -04:00
|
|
|
credElements = credElements[len(credElements)-4:]
|
2016-02-15 20:42:39 -05:00
|
|
|
var e error
|
2018-09-11 14:17:23 -04:00
|
|
|
cred.scope.date, e = time.Parse(yyyymmdd, credElements[0])
|
2016-02-15 20:42:39 -05:00
|
|
|
if e != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return ch, ErrMalformedCredentialDate
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2018-03-09 21:18:57 -05:00
|
|
|
|
2018-09-11 14:17:23 -04:00
|
|
|
cred.scope.region = credElements[1]
|
2018-03-09 21:18:57 -05:00
|
|
|
// Verify if region is valid.
|
|
|
|
sRegion := cred.scope.region
|
|
|
|
// Region is set to be empty, we use whatever was sent by the
|
|
|
|
// request and proceed further. This is a work-around to address
|
|
|
|
// an important problem for ListBuckets() getting signed with
|
|
|
|
// different regions.
|
|
|
|
if region == "" {
|
|
|
|
region = sRegion
|
|
|
|
}
|
|
|
|
// Should validate region, only if region is set.
|
|
|
|
if !isValidRegion(sRegion, region) {
|
|
|
|
return ch, ErrAuthorizationHeaderMalformed
|
|
|
|
}
|
2019-02-27 20:46:55 -05:00
|
|
|
if credElements[2] != string(stype) {
|
2020-03-18 20:25:45 -04:00
|
|
|
switch stype {
|
|
|
|
case serviceSTS:
|
|
|
|
return ch, ErrInvalidServiceSTS
|
|
|
|
}
|
|
|
|
return ch, ErrInvalidServiceS3
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2018-09-11 14:17:23 -04:00
|
|
|
cred.scope.service = credElements[2]
|
|
|
|
if credElements[3] != "aws4_request" {
|
2017-06-21 22:53:09 -04:00
|
|
|
return ch, ErrInvalidRequestVersion
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2018-09-11 14:17:23 -04:00
|
|
|
cred.scope.request = credElements[3]
|
2016-03-30 23:04:51 -04:00
|
|
|
return cred, ErrNone
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
2016-11-06 14:47:16 -05:00
|
|
|
// Parse signature from signature tag.
|
2016-03-30 23:04:51 -04:00
|
|
|
func parseSignature(signElement string) (string, APIErrorCode) {
|
2016-02-15 20:42:39 -05:00
|
|
|
signFields := strings.Split(strings.TrimSpace(signElement), "=")
|
|
|
|
if len(signFields) != 2 {
|
2016-03-30 23:04:51 -04:00
|
|
|
return "", ErrMissingFields
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
if signFields[0] != "Signature" {
|
2016-03-30 23:04:51 -04:00
|
|
|
return "", ErrMissingSignTag
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2016-11-06 14:47:16 -05:00
|
|
|
if signFields[1] == "" {
|
|
|
|
return "", ErrMissingFields
|
|
|
|
}
|
2016-02-15 20:42:39 -05:00
|
|
|
signature := signFields[1]
|
2016-03-30 23:04:51 -04:00
|
|
|
return signature, ErrNone
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
2016-11-06 14:47:16 -05:00
|
|
|
// Parse slice of signed headers from signed headers tag.
|
|
|
|
func parseSignedHeader(signedHdrElement string) ([]string, APIErrorCode) {
|
2016-02-15 20:42:39 -05:00
|
|
|
signedHdrFields := strings.Split(strings.TrimSpace(signedHdrElement), "=")
|
|
|
|
if len(signedHdrFields) != 2 {
|
2016-03-30 23:04:51 -04:00
|
|
|
return nil, ErrMissingFields
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
if signedHdrFields[0] != "SignedHeaders" {
|
2016-03-30 23:04:51 -04:00
|
|
|
return nil, ErrMissingSignHeadersTag
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
2016-11-06 14:47:16 -05:00
|
|
|
if signedHdrFields[1] == "" {
|
|
|
|
return nil, ErrMissingFields
|
|
|
|
}
|
2016-02-15 20:42:39 -05:00
|
|
|
signedHeaders := strings.Split(signedHdrFields[1], ";")
|
2016-03-30 23:04:51 -04:00
|
|
|
return signedHeaders, ErrNone
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
2016-02-21 20:57:05 -05:00
|
|
|
// signValues data type represents structured form of AWS Signature V4 header.
|
2016-02-15 20:42:39 -05:00
|
|
|
type signValues struct {
|
2016-03-12 19:08:15 -05:00
|
|
|
Credential credentialHeader
|
2016-02-15 20:42:39 -05:00
|
|
|
SignedHeaders []string
|
|
|
|
Signature string
|
|
|
|
}
|
|
|
|
|
2016-02-21 20:57:05 -05:00
|
|
|
// preSignValues data type represents structued form of AWS Signature V4 query string.
|
2016-02-15 20:42:39 -05:00
|
|
|
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
|
2016-03-12 19:08:15 -05:00
|
|
|
// querystring += &X-Amz-Credential= urlencode(accessKey + '/' + credential_scope)
|
2016-02-15 20:42:39 -05:00
|
|
|
// querystring += &X-Amz-Date=date
|
|
|
|
// querystring += &X-Amz-Expires=timeout interval
|
|
|
|
// querystring += &X-Amz-SignedHeaders=signed_headers
|
|
|
|
// querystring += &X-Amz-Signature=signature
|
2016-11-06 14:47:16 -05:00
|
|
|
//
|
2016-08-09 12:13:15 -04:00
|
|
|
// verifies if any of the necessary query params are missing in the presigned request.
|
|
|
|
func doesV4PresignParamsExist(query url.Values) APIErrorCode {
|
2020-08-19 19:43:52 -04:00
|
|
|
v4PresignQueryParams := []string{xhttp.AmzAlgorithm, xhttp.AmzCredential, xhttp.AmzSignature, xhttp.AmzDate, xhttp.AmzSignedHeaders, xhttp.AmzExpires}
|
2016-08-09 12:13:15 -04:00
|
|
|
for _, v4PresignQueryParam := range v4PresignQueryParams {
|
|
|
|
if _, ok := query[v4PresignQueryParam]; !ok {
|
|
|
|
return ErrInvalidQueryParams
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ErrNone
|
|
|
|
}
|
|
|
|
|
2016-11-06 14:47:16 -05:00
|
|
|
// Parses all the presigned signature values into separate elements.
|
2019-02-27 20:46:55 -05:00
|
|
|
func parsePreSignV4(query url.Values, region string, stype serviceType) (psv preSignValues, aec APIErrorCode) {
|
2016-08-09 12:13:15 -04:00
|
|
|
// verify whether the required query params exist.
|
2020-08-24 01:06:22 -04:00
|
|
|
aec = doesV4PresignParamsExist(query)
|
|
|
|
if aec != ErrNone {
|
|
|
|
return psv, aec
|
2016-08-09 12:13:15 -04:00
|
|
|
}
|
|
|
|
|
2016-02-15 20:42:39 -05:00
|
|
|
// Verify if the query algorithm is supported or not.
|
2020-08-19 19:43:52 -04:00
|
|
|
if query.Get(xhttp.AmzAlgorithm) != signV4Algorithm {
|
2017-06-21 22:53:09 -04:00
|
|
|
return psv, ErrInvalidQuerySignatureAlgo
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize signature version '4' structured header.
|
|
|
|
preSignV4Values := preSignValues{}
|
|
|
|
|
2016-02-21 20:57:05 -05:00
|
|
|
// Save credential.
|
2020-08-24 01:06:22 -04:00
|
|
|
preSignV4Values.Credential, aec = parseCredentialHeader("Credential="+query.Get(xhttp.AmzCredential), region, stype)
|
|
|
|
if aec != ErrNone {
|
|
|
|
return psv, aec
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
var e error
|
|
|
|
// Save date in native time.Time.
|
2020-08-19 19:43:52 -04:00
|
|
|
preSignV4Values.Date, e = time.Parse(iso8601Format, query.Get(xhttp.AmzDate))
|
2016-02-15 20:42:39 -05:00
|
|
|
if e != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return psv, ErrMalformedPresignedDate
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save expires in native time.Duration.
|
2020-08-19 19:43:52 -04:00
|
|
|
preSignV4Values.Expires, e = time.ParseDuration(query.Get(xhttp.AmzExpires) + "s")
|
2016-02-15 20:42:39 -05:00
|
|
|
if e != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return psv, ErrMalformedExpires
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
2016-08-09 12:13:15 -04:00
|
|
|
if preSignV4Values.Expires < 0 {
|
2017-06-21 22:53:09 -04:00
|
|
|
return psv, ErrNegativeExpires
|
2016-08-09 12:13:15 -04:00
|
|
|
}
|
2017-11-13 15:54:03 -05:00
|
|
|
|
|
|
|
// Check if Expiry time is less than 7 days (value in seconds).
|
|
|
|
if preSignV4Values.Expires.Seconds() > 604800 {
|
|
|
|
return psv, ErrMaximumExpires
|
|
|
|
}
|
2018-03-09 21:18:57 -05:00
|
|
|
|
2016-02-15 20:42:39 -05:00
|
|
|
// Save signed headers.
|
2020-08-24 01:06:22 -04:00
|
|
|
preSignV4Values.SignedHeaders, aec = parseSignedHeader("SignedHeaders=" + query.Get(xhttp.AmzSignedHeaders))
|
|
|
|
if aec != ErrNone {
|
|
|
|
return psv, aec
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save signature.
|
2020-08-24 01:06:22 -04:00
|
|
|
preSignV4Values.Signature, aec = parseSignature("Signature=" + query.Get(xhttp.AmzSignature))
|
|
|
|
if aec != ErrNone {
|
|
|
|
return psv, aec
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return structed form of signature query string.
|
2016-03-30 23:04:51 -04:00
|
|
|
return preSignV4Values, ErrNone
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parses signature version '4' header of the following form.
|
|
|
|
//
|
2016-03-12 19:08:15 -05:00
|
|
|
// Authorization: algorithm Credential=accessKeyID/credScope, \
|
|
|
|
// SignedHeaders=signedHeaders, Signature=signature
|
2016-02-15 20:42:39 -05:00
|
|
|
//
|
2019-02-27 20:46:55 -05:00
|
|
|
func parseSignV4(v4Auth string, region string, stype serviceType) (sv signValues, aec APIErrorCode) {
|
2019-09-30 16:55:37 -04:00
|
|
|
// credElement is fetched first to skip replacing the space in access key.
|
|
|
|
credElement := strings.TrimPrefix(strings.Split(strings.TrimSpace(v4Auth), ",")[0], signV4Algorithm)
|
2016-02-15 20:42:39 -05:00
|
|
|
// 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.
|
2021-11-16 12:28:29 -05:00
|
|
|
v4Auth = strings.ReplaceAll(v4Auth, " ", "")
|
2016-02-15 20:42:39 -05:00
|
|
|
if v4Auth == "" {
|
2017-06-21 22:53:09 -04:00
|
|
|
return sv, ErrAuthHeaderEmpty
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify if the header algorithm is supported or not.
|
|
|
|
if !strings.HasPrefix(v4Auth, signV4Algorithm) {
|
2017-06-21 22:53:09 -04:00
|
|
|
return sv, ErrSignatureVersionNotSupported
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Strip off the Algorithm prefix.
|
|
|
|
v4Auth = strings.TrimPrefix(v4Auth, signV4Algorithm)
|
|
|
|
authFields := strings.Split(strings.TrimSpace(v4Auth), ",")
|
|
|
|
if len(authFields) != 3 {
|
2017-06-21 22:53:09 -04:00
|
|
|
return sv, ErrMissingFields
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize signature version '4' structured header.
|
|
|
|
signV4Values := signValues{}
|
|
|
|
|
2020-08-24 01:06:22 -04:00
|
|
|
var s3Err APIErrorCode
|
2016-02-15 20:42:39 -05:00
|
|
|
// Save credentail values.
|
2020-08-24 01:06:22 -04:00
|
|
|
signV4Values.Credential, s3Err = parseCredentialHeader(strings.TrimSpace(credElement), region, stype)
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
return sv, s3Err
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save signed headers.
|
2020-08-24 01:06:22 -04:00
|
|
|
signV4Values.SignedHeaders, s3Err = parseSignedHeader(authFields[1])
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
return sv, s3Err
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save signature.
|
2020-08-24 01:06:22 -04:00
|
|
|
signV4Values.Signature, s3Err = parseSignature(authFields[2])
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
return sv, s3Err
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the structure here.
|
2016-03-30 23:04:51 -04:00
|
|
|
return signV4Values, ErrNone
|
2016-02-15 20:42:39 -05:00
|
|
|
}
|