2019-04-09 14:39:42 -04:00
|
|
|
// MinIO Cloud Storage, (C) 2015, 2016, 2017, 2018 MinIO, Inc.
|
2018-06-28 15:47:42 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
xhttp "github.com/minio/minio/cmd/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
|
|
|
|
}
|