From adaf70c7ebf239949a4682e8edddaa522984fe19 Mon Sep 17 00:00:00 2001 From: Sveinn Date: Tue, 18 Jun 2024 07:51:58 -0700 Subject: [PATCH] Adding support for Hex keys (#4962) --- cmd/encryption-methods.go | 62 ++++++++++++++++++++-------------- cmd/encryption-methods_test.go | 9 +++++ cmd/flags.go | 2 +- cmd/suite_test.go | 52 +++++++++++++++++++++++++++- cmd/typed-errors.go | 2 +- 5 files changed, 98 insertions(+), 29 deletions(-) diff --git a/cmd/encryption-methods.go b/cmd/encryption-methods.go index f1dba762..da4b41a3 100644 --- a/cmd/encryption-methods.go +++ b/cmd/encryption-methods.go @@ -18,9 +18,11 @@ package cmd import ( + "bytes" "encoding/base64" + "encoding/hex" + "fmt" "sort" - "strconv" "strings" "github.com/minio/cli" @@ -194,41 +196,49 @@ func parseSSEKey(sseKey string, keyType sseKeyType) ( key string, err *probe.Error, ) { - if keyType == sseS3 { - alias, prefix = splitKey(sseKey) - return - } + sseKeyBytes := []byte(sseKey) - var path string - alias, path = splitKey(sseKey) - splitPath := strings.Split(path, "=") - if len(splitPath) == 0 { + separatorIndex := bytes.LastIndex(sseKeyBytes, []byte("=")) + if separatorIndex < 0 { err = errSSEKeyMissing().Trace(sseKey) return } - aliasPlusPrefix := strings.Join(splitPath[:len(splitPath)-1], "=") - prefix = strings.Replace(aliasPlusPrefix, alias+"/", "", 1) - key = splitPath[len(splitPath)-1] + encodedKey := string(sseKeyBytes[separatorIndex+1:]) + if separatorIndex == len(sseKeyBytes)-1 { + err = errSSEKeyMissing().Trace(sseKey) + return + } - if keyType == sseC { - keyB, de := base64.RawStdEncoding.DecodeString(key) - if de != nil { - err = errSSEClientKeyFormat("One of the inserted keys was " + strconv.Itoa(len(key)) + " bytes and did not have valid base64 raw encoding.").Trace(sseKey) - return - } - key = string(keyB) - if len(key) != 32 { - err = errSSEClientKeyFormat("The plain text key was " + strconv.Itoa(len(key)) + " bytes but should be 32 bytes long").Trace(sseKey) - return - } + alias, prefix = splitKey(string(sseKeyBytes[:separatorIndex])) + + if keyType == sseS3 { + return } if keyType == sseKMS { - if !validKMSKeyName(key) { - err = errSSEKMSKeyFormat("One of the inserted keys was " + strconv.Itoa(len(key)) + " bytes and did not have a valid KMS key name.").Trace(sseKey) - return + if !validKMSKeyName(encodedKey) { + err = errSSEKMSKeyFormat(fmt.Sprintf("Key (%s) is badly formatted.", encodedKey)).Trace(sseKey) } + return + } + + var keyB []byte + var decodeError error + if len(encodedKey) == 64 { + keyB, decodeError = hex.DecodeString(encodedKey) + } else { + keyB, decodeError = base64.RawStdEncoding.DecodeString(encodedKey) + } + + if decodeError != nil { + err = errSSEClientKeyFormat(fmt.Sprintf("Key (%s) was neither base64 raw encoded nor hex encoded.", encodedKey)).Trace(sseKey) + } + + key = string(keyB) + if len(key) != 32 { + err = errSSEClientKeyFormat(fmt.Sprintf("Plain text from key (%s) is only %d bytes, but should be 32 bytes.", encodedKey, len(key))).Trace(sseKey) + return } return diff --git a/cmd/encryption-methods_test.go b/cmd/encryption-methods_test.go index fe64b82a..65aa1645 100644 --- a/cmd/encryption-methods_test.go +++ b/cmd/encryption-methods_test.go @@ -28,6 +28,7 @@ func TestParseEncryptionKeys(t *testing.T) { baseObject := "object_name" sseKey := "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDA" sseKeyPlain := "01234567890123456789012345678900" + sseHexKey := "3031323334353637383930313233343536373839303132333435363738393030" // INVALID KEYS sseKeyInvalidShort := "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2" @@ -45,6 +46,14 @@ func TestParseEncryptionKeys(t *testing.T) { sseType sseKeyType success bool }{ + { + encryptionKey: fmt.Sprintf("%s/%s/%s=%s", baseAlias, basePrefix, baseObject, sseHexKey), + keyPlain: sseKeyPlain, + alias: baseAlias, + prefix: basePrefix, + object: baseObject, + success: true, + }, { encryptionKey: fmt.Sprintf("%s/%s/%s=%s", baseAlias, basePrefix, baseObject, sseKey), keyPlain: sseKeyPlain, diff --git a/cmd/flags.go b/cmd/flags.go index 80771ff1..a84228a6 100644 --- a/cmd/flags.go +++ b/cmd/flags.go @@ -97,7 +97,7 @@ var encFlags = []cli.Flag{ var encCFlag = cli.StringSliceFlag{ Name: "enc-c", - Usage: "encrypt/decrypt objects using client provided keys. (multiple keys can be provided) Format: Raw base64 encoding.", + Usage: "encrypt/decrypt objects using client provided keys. (multiple keys can be provided) Formats: RawBase64 or Hex.", } var encKSMFlag = cli.StringSliceFlag{ diff --git a/cmd/suite_test.go b/cmd/suite_test.go index 262bff24..5a96571a 100644 --- a/cmd/suite_test.go +++ b/cmd/suite_test.go @@ -84,6 +84,9 @@ func Test_FullSuite(t *testing.T) { // MC_TEST_ENABLE_HTTPS=true // needs to be set in order to run these tests if protocol == "https://" { + PutObjectWithSSECHexKey(t) + GetObjectWithSSEC(t) + PutObjectWithSSEC(t) PutObjectWithSSECPartialPrefixMatch(t) PutObjectWithSSECMultipart(t) @@ -207,7 +210,8 @@ var ( bucketList = make([]string, 0) userList = make(map[string]TestUser, 0) - // KMS + // ENCRYPTION + sseHexKey = "8fe4d820587c427d5cc207d75cb76f3c6874808174b04050fa209206bfd08ebb" sseBaseEncodedKey = "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5MDA" invalidSSEBaseEncodedKey = "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5" sseBaseEncodedKey2 = "MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTIzNDU2Nzg5YWE" @@ -1292,6 +1296,22 @@ func PutObjectWithSSECPartialPrefixMatch(t *testing.T) { fatalIfErrorWMsg(err, out, t) } +func PutObjectWithSSECHexKey(t *testing.T) { + file := createFile(newTestFile{ + addToGlobalFileMap: false, + tag: "encputhex", + sizeInMBS: 1, + }) + + out, err := RunMC( + "cp", + "--enc-c="+sseTestBucket+"="+sseHexKey, + file.diskFile.Name(), + sseTestBucket+"/"+file.fileNameWithoutPath, + ) + fatalIfErrorWMsg(err, out, t) +} + func PutObjectWithSSEC(t *testing.T) { file := createFile(newTestFile{ addToGlobalFileMap: false, @@ -1340,6 +1360,36 @@ func PutObjectWithSSECInvalidKeys(t *testing.T) { fatalIfNoErrorWMsg(err, out, t) } +func GetObjectWithSSECHexKey(t *testing.T) { + file := createFile(newTestFile{ + addToGlobalFileMap: false, + tag: "encgethex", + sizeInMBS: 1, + }) + + out, err := RunMC( + "cp", + "--enc-c="+sseTestBucket+"="+sseHexKey, + file.diskFile.Name(), + sseTestBucket+"/"+file.fileNameWithoutPath, + ) + fatalIfErrorWMsg(err, out, t) + + out, err = RunMC( + "cp", + "--enc-c="+sseTestBucket+"="+sseHexKey, + sseTestBucket+"/"+file.fileNameWithoutPath, + file.diskFile.Name()+".download", + ) + fatalIfErrorWMsg(err, out, t) + + md5s, err := openFileAndGetMd5Sum(file.diskFile.Name() + ".download") + fatalIfError(err, t) + if md5s != file.md5Sum { + fatalMsgOnly(fmt.Sprintf("expecting md5sum (%s) but got sum (%s)", file.md5Sum, md5s), t) + } +} + func GetObjectWithSSEC(t *testing.T) { file := createFile(newTestFile{ addToGlobalFileMap: false, diff --git a/cmd/typed-errors.go b/cmd/typed-errors.go index 281c6eeb..1440923c 100644 --- a/cmd/typed-errors.go +++ b/cmd/typed-errors.go @@ -187,7 +187,7 @@ var errSSEKMSKeyFormat = func(msg string) *probe.Error { type sseClientKeyFormatErr error var errSSEClientKeyFormat = func(msg string) *probe.Error { - m := "Encryption key should be 44 bytes raw base64 encoded key." + m := "Encryption key should be either raw base64 encoded or hex encoded. " m += msg return probe.NewError(sseClientKeyFormatErr(errors.New(m))).Untrace() }