mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 02:57:37 +01:00
* s3api: return KMS errors on SSE-KMS writes as their S3 error codes An SSE-KMS PutObject, UploadPart, UploadPartCopy or CopyObject naming a key the KMS doesn't have failed with 500 InternalError, and so did a plain upload to a bucket whose default encryption names one. MapKMSErrorToS3Error never saw these errors. The put path returned InternalError for every encryption failure. The copy path compared the mapper's result against InvalidRequest, which it never returns, so it answered InternalError for every error and its SSE-C check after it never ran. And the KMS calls wrapped the provider's error with %v, so its type was gone before any of them looked. Wrap the provider errors with %w, find the KMS error with errors.As, and map encryption failures on the put path, the bucket default put and the inline copy path. A missing key is now 400 KMSKeyNotFoundException, a disabled one 400 KMSKeyDisabledException, and errors that don't come from KMS stay 500. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: map wrapped SSE-C errors to their S3 codes A destination SSE-C parse error reaches the copy error mapper wrapped with %w, so the exact-match switch missed it and the copy answered 500. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3err: answer KMS key errors with the codes AWS S3 uses AWS S3 reports a missing SSE-KMS key as KMS.NotFoundException, a disabled one as KMS.DisabledException, and a denied KMS call as AccessDenied. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
348 lines
11 KiB
Go
348 lines
11 KiB
Go
package s3api
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/md5"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
|
|
)
|
|
|
|
// decryptReaderCloser wraps a cipher.StreamReader with proper Close() support
|
|
// This ensures the underlying io.ReadCloser (like http.Response.Body) is properly closed
|
|
type decryptReaderCloser struct {
|
|
io.Reader
|
|
underlyingCloser io.Closer
|
|
}
|
|
|
|
func (d *decryptReaderCloser) Close() error {
|
|
if d.underlyingCloser != nil {
|
|
return d.underlyingCloser.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SSECCopyStrategy represents different strategies for copying SSE-C objects
|
|
type SSECCopyStrategy int
|
|
|
|
const (
|
|
// SSECCopyStrategyDirect indicates the object can be copied directly without decryption
|
|
SSECCopyStrategyDirect SSECCopyStrategy = iota
|
|
// SSECCopyStrategyDecryptEncrypt indicates the object must be decrypted then re-encrypted
|
|
SSECCopyStrategyDecryptEncrypt
|
|
)
|
|
|
|
const (
|
|
// SSE-C constants
|
|
SSECustomerAlgorithmAES256 = s3_constants.SSEAlgorithmAES256
|
|
SSECustomerKeySize = 32 // 256 bits
|
|
)
|
|
|
|
// SSE-C related errors
|
|
var (
|
|
ErrInvalidRequest = errors.New("invalid request")
|
|
ErrInvalidEncryptionAlgorithm = errors.New("invalid encryption algorithm")
|
|
ErrInvalidEncryptionKey = errors.New("invalid encryption key")
|
|
ErrSSECustomerKeyMD5Mismatch = errors.New("customer key MD5 mismatch")
|
|
ErrSSECustomerKeyMissing = errors.New("customer key missing")
|
|
ErrSSECustomerKeyNotNeeded = errors.New("customer key not needed")
|
|
ErrSSECNotSupportedFIPS140 = errors.New("SSE-C is not supported under fips140=only")
|
|
)
|
|
|
|
// SSECustomerKey represents a customer-provided encryption key for SSE-C
|
|
type SSECustomerKey struct {
|
|
Algorithm string
|
|
Key []byte
|
|
KeyMD5 string
|
|
}
|
|
|
|
// IsSSECRequest checks if the request contains SSE-C headers
|
|
func IsSSECRequest(r *http.Request) bool {
|
|
// If SSE-KMS headers are present, this is not an SSE-C request (they are mutually exclusive)
|
|
sseAlgorithm := r.Header.Get(s3_constants.AmzServerSideEncryption)
|
|
if sseAlgorithm == "aws:kms" || r.Header.Get(s3_constants.AmzServerSideEncryptionAwsKmsKeyId) != "" {
|
|
return false
|
|
}
|
|
|
|
return r.Header.Get(s3_constants.AmzServerSideEncryptionCustomerAlgorithm) != ""
|
|
}
|
|
|
|
// IsSSECEncrypted checks if the metadata indicates SSE-C encryption
|
|
func IsSSECEncrypted(metadata map[string][]byte) bool {
|
|
if metadata == nil {
|
|
return false
|
|
}
|
|
|
|
// Check for SSE-C specific metadata keys
|
|
if _, exists := metadata[s3_constants.AmzServerSideEncryptionCustomerAlgorithm]; exists {
|
|
return true
|
|
}
|
|
if _, exists := metadata[s3_constants.AmzServerSideEncryptionCustomerKeyMD5]; exists {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
// validateAndParseSSECHeaders does the core validation and parsing logic.
|
|
// The key check needs MD5, so SSE-C is refused under fips140=only.
|
|
func validateAndParseSSECHeaders(algorithm, key, keyMD5 string) (*SSECustomerKey, error) {
|
|
if algorithm == "" && key == "" && keyMD5 == "" {
|
|
return nil, nil // No SSE-C headers
|
|
}
|
|
|
|
if fips140Enforced() {
|
|
return nil, ErrSSECNotSupportedFIPS140
|
|
}
|
|
|
|
if algorithm == "" || key == "" || keyMD5 == "" {
|
|
return nil, ErrInvalidRequest
|
|
}
|
|
|
|
if algorithm != SSECustomerAlgorithmAES256 {
|
|
return nil, ErrInvalidEncryptionAlgorithm
|
|
}
|
|
|
|
// Decode and validate key
|
|
keyBytes, err := base64.StdEncoding.DecodeString(key)
|
|
if err != nil {
|
|
return nil, ErrInvalidEncryptionKey
|
|
}
|
|
|
|
if len(keyBytes) != SSECustomerKeySize {
|
|
return nil, ErrInvalidEncryptionKey
|
|
}
|
|
|
|
// Validate key MD5 (base64-encoded MD5 of the raw key bytes; case-sensitive)
|
|
sum := md5.Sum(keyBytes)
|
|
expectedMD5 := base64.StdEncoding.EncodeToString(sum[:])
|
|
|
|
// Debug logging for MD5 validation (never log key material)
|
|
glog.V(4).Infof("SSE-C MD5 validation: provided='%s', expected='%s'", keyMD5, expectedMD5)
|
|
|
|
if keyMD5 != expectedMD5 {
|
|
glog.Errorf("SSE-C MD5 mismatch: provided='%s', expected='%s'", keyMD5, expectedMD5)
|
|
return nil, ErrSSECustomerKeyMD5Mismatch
|
|
}
|
|
|
|
return &SSECustomerKey{
|
|
Algorithm: algorithm,
|
|
Key: keyBytes,
|
|
KeyMD5: keyMD5,
|
|
}, nil
|
|
}
|
|
|
|
// ParseSSECHeaders parses and validates SSE-C headers from the request
|
|
func ParseSSECHeaders(r *http.Request) (*SSECustomerKey, error) {
|
|
algorithm := r.Header.Get(s3_constants.AmzServerSideEncryptionCustomerAlgorithm)
|
|
key := r.Header.Get(s3_constants.AmzServerSideEncryptionCustomerKey)
|
|
keyMD5 := r.Header.Get(s3_constants.AmzServerSideEncryptionCustomerKeyMD5)
|
|
|
|
return validateAndParseSSECHeaders(algorithm, key, keyMD5)
|
|
}
|
|
|
|
// ParseSSECCopySourceHeaders parses and validates SSE-C copy source headers from the request
|
|
func ParseSSECCopySourceHeaders(r *http.Request) (*SSECustomerKey, error) {
|
|
algorithm := r.Header.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerAlgorithm)
|
|
key := r.Header.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerKey)
|
|
keyMD5 := r.Header.Get(s3_constants.AmzCopySourceServerSideEncryptionCustomerKeyMD5)
|
|
|
|
return validateAndParseSSECHeaders(algorithm, key, keyMD5)
|
|
}
|
|
|
|
// CreateSSECEncryptedReader creates a new encrypted reader for SSE-C
|
|
// Returns the encrypted reader and the IV for metadata storage
|
|
func CreateSSECEncryptedReader(r io.Reader, customerKey *SSECustomerKey) (io.Reader, []byte, error) {
|
|
if customerKey == nil {
|
|
return r, nil, nil
|
|
}
|
|
|
|
// Create AES cipher
|
|
block, err := aes.NewCipher(customerKey.Key)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("failed to create AES cipher: %v", err)
|
|
}
|
|
|
|
// Generate random IV
|
|
iv := make([]byte, s3_constants.AESBlockSize)
|
|
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
|
|
return nil, nil, fmt.Errorf("failed to generate IV: %v", err)
|
|
}
|
|
|
|
// Create CTR mode cipher
|
|
stream := cipher.NewCTR(block, iv)
|
|
|
|
// The IV is stored in metadata, so the encrypted stream does not need to prepend the IV
|
|
// This ensures correct Content-Length for clients
|
|
encryptedReader := &cipher.StreamReader{S: stream, R: r}
|
|
|
|
return encryptedReader, iv, nil
|
|
}
|
|
|
|
// CreateSSECDecryptedReader creates a new decrypted reader for SSE-C
|
|
// The IV comes from metadata, not from the encrypted data stream
|
|
func CreateSSECDecryptedReader(r io.Reader, customerKey *SSECustomerKey, iv []byte) (io.Reader, error) {
|
|
if customerKey == nil {
|
|
return r, nil
|
|
}
|
|
|
|
// IV must be provided from metadata
|
|
if err := ValidateIV(iv, "IV"); err != nil {
|
|
return nil, fmt.Errorf("invalid IV from metadata: %w", err)
|
|
}
|
|
|
|
// Create AES cipher
|
|
block, err := aes.NewCipher(customerKey.Key)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create AES cipher: %v", err)
|
|
}
|
|
|
|
// Create CTR mode cipher using the IV from metadata
|
|
stream := cipher.NewCTR(block, iv)
|
|
decryptReader := &cipher.StreamReader{S: stream, R: r}
|
|
|
|
// Wrap with closer if the underlying reader implements io.Closer
|
|
if closer, ok := r.(io.Closer); ok {
|
|
return &decryptReaderCloser{
|
|
Reader: decryptReader,
|
|
underlyingCloser: closer,
|
|
}, nil
|
|
}
|
|
|
|
return decryptReader, nil
|
|
}
|
|
|
|
// CreateSSECEncryptedReaderWithOffset creates an encrypted reader with a specific counter offset
|
|
// This is used for chunk-level encryption where each chunk needs a different counter position
|
|
func CreateSSECEncryptedReaderWithOffset(r io.Reader, customerKey *SSECustomerKey, iv []byte, counterOffset uint64) (io.Reader, error) {
|
|
if customerKey == nil {
|
|
return r, nil
|
|
}
|
|
|
|
// Create AES cipher
|
|
block, err := aes.NewCipher(customerKey.Key)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create AES cipher: %v", err)
|
|
}
|
|
|
|
// Create CTR mode cipher with offset
|
|
stream := createCTRStreamWithOffset(block, iv, counterOffset)
|
|
|
|
return &cipher.StreamReader{S: stream, R: r}, nil
|
|
}
|
|
|
|
// CreateSSECDecryptedReaderWithOffset creates a decrypted reader with a specific counter offset
|
|
func CreateSSECDecryptedReaderWithOffset(r io.Reader, customerKey *SSECustomerKey, iv []byte, counterOffset uint64) (io.Reader, error) {
|
|
if customerKey == nil {
|
|
return r, nil
|
|
}
|
|
|
|
// Create AES cipher
|
|
block, err := aes.NewCipher(customerKey.Key)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create AES cipher: %v", err)
|
|
}
|
|
|
|
// Create CTR mode cipher with offset
|
|
stream := createCTRStreamWithOffset(block, iv, counterOffset)
|
|
|
|
return &cipher.StreamReader{S: stream, R: r}, nil
|
|
}
|
|
|
|
// createCTRStreamWithOffset creates a CTR stream positioned at a specific counter offset
|
|
func createCTRStreamWithOffset(block cipher.Block, iv []byte, counterOffset uint64) cipher.Stream {
|
|
adjustedIV, skip := calculateIVWithOffset(iv, int64(counterOffset))
|
|
stream := cipher.NewCTR(block, adjustedIV)
|
|
if skip > 0 {
|
|
dummy := make([]byte, skip)
|
|
stream.XORKeyStream(dummy, dummy)
|
|
}
|
|
return stream
|
|
}
|
|
|
|
// GetSourceSSECInfo extracts SSE-C information from source object metadata
|
|
func GetSourceSSECInfo(metadata map[string][]byte) (algorithm string, keyMD5 string, isEncrypted bool) {
|
|
if alg, exists := metadata[s3_constants.AmzServerSideEncryptionCustomerAlgorithm]; exists {
|
|
algorithm = string(alg)
|
|
}
|
|
if md5, exists := metadata[s3_constants.AmzServerSideEncryptionCustomerKeyMD5]; exists {
|
|
keyMD5 = string(md5)
|
|
}
|
|
isEncrypted = algorithm != "" && keyMD5 != ""
|
|
return
|
|
}
|
|
|
|
// CanDirectCopySSEC determines if we can directly copy chunks without decrypt/re-encrypt
|
|
func CanDirectCopySSEC(srcMetadata map[string][]byte, copySourceKey *SSECustomerKey, destKey *SSECustomerKey) bool {
|
|
_, srcKeyMD5, srcEncrypted := GetSourceSSECInfo(srcMetadata)
|
|
|
|
// Case 1: Source unencrypted, destination unencrypted -> Direct copy
|
|
if !srcEncrypted && destKey == nil {
|
|
return true
|
|
}
|
|
|
|
// Case 2: Source encrypted, same key for decryption and destination -> Direct copy
|
|
if srcEncrypted && copySourceKey != nil && destKey != nil {
|
|
// Same key if MD5 matches exactly (base64 encoding is case-sensitive)
|
|
return copySourceKey.KeyMD5 == srcKeyMD5 &&
|
|
destKey.KeyMD5 == srcKeyMD5
|
|
}
|
|
|
|
// All other cases require decrypt/re-encrypt
|
|
return false
|
|
}
|
|
|
|
// Note: SSECCopyStrategy is defined above
|
|
|
|
// DetermineSSECCopyStrategy determines the optimal copy strategy
|
|
func DetermineSSECCopyStrategy(srcMetadata map[string][]byte, copySourceKey *SSECustomerKey, destKey *SSECustomerKey) (SSECCopyStrategy, error) {
|
|
_, srcKeyMD5, srcEncrypted := GetSourceSSECInfo(srcMetadata)
|
|
|
|
// Validate source key if source is encrypted
|
|
if srcEncrypted {
|
|
if copySourceKey == nil {
|
|
return SSECCopyStrategyDecryptEncrypt, ErrSSECustomerKeyMissing
|
|
}
|
|
if copySourceKey.KeyMD5 != srcKeyMD5 {
|
|
return SSECCopyStrategyDecryptEncrypt, ErrSSECustomerKeyMD5Mismatch
|
|
}
|
|
} else if copySourceKey != nil {
|
|
// Source not encrypted but copy source key provided
|
|
return SSECCopyStrategyDecryptEncrypt, ErrSSECustomerKeyNotNeeded
|
|
}
|
|
|
|
if CanDirectCopySSEC(srcMetadata, copySourceKey, destKey) {
|
|
return SSECCopyStrategyDirect, nil
|
|
}
|
|
|
|
return SSECCopyStrategyDecryptEncrypt, nil
|
|
}
|
|
|
|
// MapSSECErrorToS3Error maps SSE-C custom errors to S3 API error codes
|
|
func MapSSECErrorToS3Error(err error) s3err.ErrorCode {
|
|
switch {
|
|
case errors.Is(err, ErrInvalidEncryptionAlgorithm):
|
|
return s3err.ErrInvalidEncryptionAlgorithm
|
|
case errors.Is(err, ErrInvalidEncryptionKey):
|
|
return s3err.ErrInvalidEncryptionKey
|
|
case errors.Is(err, ErrSSECustomerKeyMD5Mismatch):
|
|
return s3err.ErrSSECustomerKeyMD5Mismatch
|
|
case errors.Is(err, ErrSSECustomerKeyMissing):
|
|
return s3err.ErrSSECustomerKeyMissing
|
|
case errors.Is(err, ErrSSECustomerKeyNotNeeded):
|
|
return s3err.ErrSSECustomerKeyNotNeeded
|
|
case errors.Is(err, ErrSSECNotSupportedFIPS140):
|
|
return s3err.ErrNotImplemented
|
|
default:
|
|
return s3err.ErrInvalidRequest
|
|
}
|
|
}
|