2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
2018-06-28 15:47:42 -04:00
|
|
|
//
|
2021-04-18 15:41:13 -04:00
|
|
|
// This file is part of MinIO Object Storage stack
|
2018-06-28 15:47:42 -04:00
|
|
|
//
|
2021-04-18 15:41:13 -04:00
|
|
|
// 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.
|
2018-06-28 15:47:42 -04:00
|
|
|
//
|
2021-04-18 15:41:13 -04:00
|
|
|
// 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/>.
|
2018-06-28 15:47:42 -04:00
|
|
|
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
2018-07-18 13:49:26 -04:00
|
|
|
"bytes"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/base64"
|
2018-06-28 15:47:42 -04:00
|
|
|
"net/http"
|
2020-08-11 11:29:29 -04:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2018-06-28 15:47:42 -04:00
|
|
|
)
|
|
|
|
|
2018-09-24 11:32:51 -04:00
|
|
|
// RemoveSensitiveHeaders removes confidential encryption
|
|
|
|
// information - e.g. the SSE-C key - from the HTTP headers.
|
|
|
|
// It has the same semantics as RemoveSensitiveEntires.
|
|
|
|
func RemoveSensitiveHeaders(h http.Header) {
|
2020-12-22 12:19:32 -05:00
|
|
|
h.Del(xhttp.AmzServerSideEncryptionCustomerKey)
|
|
|
|
h.Del(xhttp.AmzServerSideEncryptionCopyCustomerKey)
|
2020-08-11 11:29:29 -04:00
|
|
|
h.Del(xhttp.AmzMetaUnencryptedContentLength)
|
|
|
|
h.Del(xhttp.AmzMetaUnencryptedContentMD5)
|
2018-09-24 11:32:51 -04:00
|
|
|
}
|
|
|
|
|
2018-07-18 13:49:26 -04:00
|
|
|
var (
|
|
|
|
// SSECopy represents AWS SSE-C for copy requests. It provides
|
|
|
|
// functionality to handle SSE-C copy requests.
|
|
|
|
SSECopy = ssecCopy{}
|
|
|
|
)
|
|
|
|
|
|
|
|
type ssecCopy struct{}
|
|
|
|
|
|
|
|
// IsRequested returns true if the HTTP headers contains
|
|
|
|
// at least one SSE-C copy header. Regular SSE-C headers
|
|
|
|
// are ignored.
|
|
|
|
func (ssecCopy) IsRequested(h http.Header) bool {
|
2020-12-22 12:19:32 -05:00
|
|
|
if _, ok := h[xhttp.AmzServerSideEncryptionCopyCustomerAlgorithm]; ok {
|
2018-07-18 13:49:26 -04:00
|
|
|
return true
|
|
|
|
}
|
2020-12-22 12:19:32 -05:00
|
|
|
if _, ok := h[xhttp.AmzServerSideEncryptionCopyCustomerKey]; ok {
|
2018-07-18 13:49:26 -04:00
|
|
|
return true
|
|
|
|
}
|
2020-12-22 12:19:32 -05:00
|
|
|
if _, ok := h[xhttp.AmzServerSideEncryptionCopyCustomerKeyMD5]; ok {
|
2018-07-18 13:49:26 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:35:54 -04:00
|
|
|
// ParseHTTP parses the SSE-C copy headers and returns the SSE-C client key
|
2018-07-18 13:49:26 -04:00
|
|
|
// on success. Regular SSE-C headers are ignored.
|
2018-07-25 16:35:54 -04:00
|
|
|
func (ssecCopy) ParseHTTP(h http.Header) (key [32]byte, err error) {
|
2020-12-22 12:19:32 -05:00
|
|
|
if h.Get(xhttp.AmzServerSideEncryptionCopyCustomerAlgorithm) != xhttp.AmzEncryptionAES {
|
2018-07-18 13:49:26 -04:00
|
|
|
return key, ErrInvalidCustomerAlgorithm
|
|
|
|
}
|
2020-12-22 12:19:32 -05:00
|
|
|
if h.Get(xhttp.AmzServerSideEncryptionCopyCustomerKey) == "" {
|
2018-07-18 13:49:26 -04:00
|
|
|
return key, ErrMissingCustomerKey
|
|
|
|
}
|
2020-12-22 12:19:32 -05:00
|
|
|
if h.Get(xhttp.AmzServerSideEncryptionCopyCustomerKeyMD5) == "" {
|
2018-07-18 13:49:26 -04:00
|
|
|
return key, ErrMissingCustomerKeyMD5
|
|
|
|
}
|
|
|
|
|
2020-12-22 12:19:32 -05:00
|
|
|
clientKey, err := base64.StdEncoding.DecodeString(h.Get(xhttp.AmzServerSideEncryptionCopyCustomerKey))
|
2018-07-18 13:49:26 -04:00
|
|
|
if err != nil || len(clientKey) != 32 { // The client key must be 256 bits long
|
|
|
|
return key, ErrInvalidCustomerKey
|
|
|
|
}
|
2020-12-22 12:19:32 -05:00
|
|
|
keyMD5, err := base64.StdEncoding.DecodeString(h.Get(xhttp.AmzServerSideEncryptionCopyCustomerKeyMD5))
|
2018-07-18 13:49:26 -04:00
|
|
|
if md5Sum := md5.Sum(clientKey); err != nil || !bytes.Equal(md5Sum[:], keyMD5) {
|
|
|
|
return key, ErrCustomerKeyMD5Mismatch
|
|
|
|
}
|
|
|
|
copy(key[:], clientKey)
|
|
|
|
return key, nil
|
|
|
|
}
|