mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 11:07:31 +01:00
* s3api: return the upload's SSE headers from CreateMultipartUpload CreateMultipartUpload now answers with the encryption the upload was initiated with, as AWS does: x-amz-server-side-encryption for SSE-S3 and SSE-KMS, the KMS key id, encryption context and bucket-key flag, and the SSE-C algorithm and key MD5. Bucket-default encryption is reported the same way, from the config stored on the upload entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: validate the SSE-C key and KMS context on CreateMultipartUpload Initiate accepted an SSE-C key whose MD5 did not match, an incomplete SSE-C header set, and a KMS encryption context that was not base64 JSON, then created the upload. Reject them before creating the upload with the same codes PutObject returns, so the headers the response echoes are ones the parts can use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: encrypt multipart parts with the context given at initiate CreateMultipartUpload stores the x-amz-server-side-encryption-context header as received, base64 JSON, but UploadPart and UploadPartCopy parsed it as raw JSON, failed, and fell back to the default context. Every part's data key was wrapped without the context the client asked for. Parse the stored value the way the header is parsed on PutObject. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: reject KMS encryption contexts the key service refuses up front CreateMultipartUpload accepted a context with control characters, a whitespace-only key or too many pairs, so every part then failed; PutObject answered the same input with a 500. Both now build the merged context once and apply the KMS checks, answering InvalidRequest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * s3api: keep the derived aws:s3:arn in a client's KMS encryption context A context header naming aws:s3:arn replaced the object or bucket ARN sent to the key service, on PutObject and, through the context stored at initiate, on every multipart part, so a key policy conditioned on aws:s3:arn could be met with another bucket's ARN. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
109 lines
3.9 KiB
Go
109 lines
3.9 KiB
Go
package s3api
|
|
|
|
import (
|
|
"context"
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/kms"
|
|
"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
|
|
)
|
|
|
|
// KMSDataKeyResult holds the result of data key generation
|
|
type KMSDataKeyResult struct {
|
|
Response *kms.GenerateDataKeyResponse
|
|
Block cipher.Block
|
|
}
|
|
|
|
// validateKMSEncryptionContext rejects contexts that must not reach the KMS service.
|
|
func validateKMSEncryptionContext(encryptionContext map[string]string) error {
|
|
for key, value := range encryptionContext {
|
|
if strings.TrimSpace(key) == "" {
|
|
return fmt.Errorf("invalid encryption context: keys cannot be empty or whitespace-only")
|
|
}
|
|
if strings.ContainsAny(key, "\x00\n\r\t") || strings.ContainsAny(value, "\x00\n\r\t") {
|
|
return fmt.Errorf("invalid encryption context: keys and values cannot contain control characters")
|
|
}
|
|
// AWS KMS has limits on key/value lengths
|
|
if len(key) > 2048 || len(value) > 2048 {
|
|
return fmt.Errorf("invalid encryption context: keys and values must be ≤ 2048 characters (key=%d, value=%d)", len(key), len(value))
|
|
}
|
|
}
|
|
if len(encryptionContext) > s3_constants.MaxKMSEncryptionContextPairs {
|
|
return fmt.Errorf("invalid encryption context: cannot exceed %d key-value pairs, got %d", s3_constants.MaxKMSEncryptionContextPairs, len(encryptionContext))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// generateKMSDataKey generates a new data encryption key using KMS
|
|
// This function encapsulates the common pattern used across all SSE-KMS functions
|
|
func generateKMSDataKey(keyID string, encryptionContext map[string]string) (*KMSDataKeyResult, error) {
|
|
// Validate keyID to prevent injection attacks and malformed requests to KMS service
|
|
if !isValidKMSKeyID(keyID) {
|
|
return nil, fmt.Errorf("invalid KMS key ID format: key ID must be non-empty, without spaces or control characters")
|
|
}
|
|
|
|
if err := validateKMSEncryptionContext(encryptionContext); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Get KMS provider
|
|
kmsProvider := kms.GetGlobalKMS()
|
|
if kmsProvider == nil {
|
|
return nil, fmt.Errorf("KMS is not configured")
|
|
}
|
|
|
|
// Create data key request
|
|
generateDataKeyReq := &kms.GenerateDataKeyRequest{
|
|
KeyID: keyID,
|
|
KeySpec: kms.KeySpecAES256,
|
|
EncryptionContext: encryptionContext,
|
|
}
|
|
|
|
// Generate the data key
|
|
dataKeyResp, err := kmsProvider.GenerateDataKey(context.Background(), generateDataKeyReq)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to generate KMS data key: %w", err)
|
|
}
|
|
|
|
// Create AES cipher with the plaintext data key
|
|
block, err := aes.NewCipher(dataKeyResp.Plaintext)
|
|
if err != nil {
|
|
// Clear sensitive data before returning error
|
|
kms.ClearSensitiveData(dataKeyResp.Plaintext)
|
|
return nil, fmt.Errorf("failed to create AES cipher: %v", err)
|
|
}
|
|
|
|
return &KMSDataKeyResult{
|
|
Response: dataKeyResp,
|
|
Block: block,
|
|
}, nil
|
|
}
|
|
|
|
// clearKMSDataKey safely clears sensitive data from a KMSDataKeyResult
|
|
func clearKMSDataKey(result *KMSDataKeyResult) {
|
|
if result != nil && result.Response != nil {
|
|
kms.ClearSensitiveData(result.Response.Plaintext)
|
|
}
|
|
}
|
|
|
|
// createSSEKMSKey creates an SSEKMSKey struct from data key result and parameters.
|
|
// The HMAC key commitment is computed here (rather than at each call site) so
|
|
// every SSE-KMS encryption path produces metadata that can later be verified
|
|
// against tampering — a missing commitment was an attacker-controlled silent
|
|
// downgrade vector. plaintext must still be live; deferred clearKMSDataKey
|
|
// runs after this function returns.
|
|
func createSSEKMSKey(result *KMSDataKeyResult, encryptionContext map[string]string, bucketKeyEnabled bool, iv []byte, chunkOffset int64) *SSEKMSKey {
|
|
return &SSEKMSKey{
|
|
KeyID: result.Response.KeyID,
|
|
EncryptedDataKey: result.Response.CiphertextBlob,
|
|
EncryptionContext: encryptionContext,
|
|
BucketKeyEnabled: bucketKeyEnabled,
|
|
IV: iv,
|
|
ChunkOffset: chunkOffset,
|
|
KeyCommitment: ComputeKeyCommitment(result.Response.Plaintext, iv, s3_constants.SSEAlgorithmKMS),
|
|
}
|
|
}
|