Skip to content

Commit 5d7fc31

Browse files
fix: Ranged gets with RSA keys (#288)
* fix: Ranged gets with RSA keys * checkstyle * more tests
1 parent adb6d3b commit 5d7fc31

File tree

3 files changed

+155
-76
lines changed

3 files changed

+155
-76
lines changed

src/main/java/software/amazon/encryption/s3/internal/ContentMetadataStrategy.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ private static ContentMetadata readFromMap(Map<String, String> metadata, GetObje
102102
|| contentEncryptionAlgorithm.equals(AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF.cipherName())) {
103103
algorithmSuite = AlgorithmSuite.ALG_AES_256_CBC_IV16_NO_KDF;
104104
} else if (contentEncryptionAlgorithm.equals(AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF.cipherName())) {
105-
algorithmSuite = (contentRange == null ) ? AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF : AlgorithmSuite.ALG_AES_256_CTR_IV16_TAG16_NO_KDF;
105+
// If contentRange is provided, this is a ranged get.
106+
// ranged gets require legacy unauthenticated modes.
107+
// Change AES-GCM to AES-CTR to disable authentication when reading this message.
108+
algorithmSuite = (contentRange == null)
109+
? AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF
110+
: AlgorithmSuite.ALG_AES_256_CTR_IV16_TAG16_NO_KDF;
106111
} else {
107112
throw new S3EncryptionClientException(
108113
"Unknown content encryption algorithm: " + contentEncryptionAlgorithm);

src/main/java/software/amazon/encryption/s3/materials/RsaKeyring.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package software.amazon.encryption.s3.materials;
44

55
import software.amazon.encryption.s3.S3EncryptionClientException;
6+
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
67
import software.amazon.encryption.s3.internal.CryptoFactory;
78

89
import javax.crypto.Cipher;
@@ -117,8 +118,7 @@ public byte[] encryptDataKey(SecureRandom secureRandom,
117118

118119
// Create a pseudo-data key with the content encryption appended to the data key
119120
byte[] dataKey = materials.plaintextDataKey();
120-
byte[] dataCipherName = materials.algorithmSuite().cipherName().getBytes(
121-
StandardCharsets.UTF_8);
121+
byte[] dataCipherName = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF.cipherName().getBytes(StandardCharsets.UTF_8);
122122
byte[] pseudoDataKey = new byte[1 + dataKey.length + dataCipherName.length];
123123

124124
pseudoDataKey[0] = (byte)dataKey.length;
@@ -146,7 +146,8 @@ private byte[] parsePseudoDataKey(DecryptionMaterials materials, byte[] pseudoDa
146146
throw new S3EncryptionClientException("Invalid key length (" + dataKeyLengthBytes + ") in encrypted data key");
147147
}
148148

149-
int dataCipherNameLength = pseudoDataKey.length - dataKeyLengthBytes - 1;
149+
// int dataCipherNameLength = pseudoDataKey.length - dataKeyLengthBytes - 1;
150+
int dataCipherNameLength = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF.cipherName().getBytes(StandardCharsets.UTF_8).length;
150151
if (dataCipherNameLength <= 0) {
151152
throw new S3EncryptionClientException("Invalid data cipher name length (" + dataCipherNameLength + ") in encrypted data key");
152153
}
@@ -156,7 +157,7 @@ private byte[] parsePseudoDataKey(DecryptionMaterials materials, byte[] pseudoDa
156157
System.arraycopy(pseudoDataKey, 1, dataKey, 0, dataKeyLengthBytes);
157158
System.arraycopy(pseudoDataKey, 1 + dataKeyLengthBytes, dataCipherName, 0, dataCipherNameLength);
158159

159-
byte[] expectedDataCipherName = materials.algorithmSuite().cipherName().getBytes(StandardCharsets.UTF_8);
160+
byte[] expectedDataCipherName = AlgorithmSuite.ALG_AES_256_GCM_IV12_TAG16_NO_KDF.cipherName().getBytes(StandardCharsets.UTF_8);
160161
if (!Arrays.equals(expectedDataCipherName, dataCipherName)) {
161162
throw new S3EncryptionClientException("The data cipher does not match the data cipher used for encryption. The object may be altered or corrupted");
162163
}

0 commit comments

Comments
 (0)