Adding support for Hex keys (#4962)

This commit is contained in:
Sveinn
2024-06-18 07:51:58 -07:00
committed by GitHub
parent e7c9a733c6
commit adaf70c7eb
5 changed files with 98 additions and 29 deletions
+36 -26
View File
@@ -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
+9
View File
@@ -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,
+1 -1
View File
@@ -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{
+51 -1
View File
@@ -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,
+1 -1
View File
@@ -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()
}