Skip to content

Commit c5b8e41

Browse files
author
Anirav Kareddy
committed
added public getter function to retrieve whether enableLegacyWrappingAlgorithm was enabled or not for keyring. From there, in the S3EncryptionClient Builder, I checked whether enableLegacyWrappingAlgorithm was set, but was not set for the keyring that was passed; if not, an exception is thrown. The test cases I wrote illustrates this
1 parent aff10d9 commit c5b8e41

File tree

3 files changed

+58
-229
lines changed

3 files changed

+58
-229
lines changed

src/main/java/software/amazon/encryption/s3/S3EncryptionClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import software.amazon.encryption.s3.materials.MultipartConfiguration;
6262
import software.amazon.encryption.s3.materials.PartialRsaKeyPair;
6363
import software.amazon.encryption.s3.materials.RsaKeyring;
64+
import software.amazon.encryption.s3.materials.S3Keyring;
6465

6566
import javax.crypto.SecretKey;
6667
import java.io.IOException;
@@ -1067,6 +1068,12 @@ public S3EncryptionClient build() {
10671068
if (!onlyOneNonNull(_cryptoMaterialsManager, _keyring, _aesKey, _rsaKeyPair, _kmsKeyId)) {
10681069
throw new S3EncryptionClientException("Exactly one must be set of: crypto materials manager, keyring, AES key, RSA key pair, KMS key id");
10691070
}
1071+
if (_enableLegacyWrappingAlgorithms && _keyring !=null && _keyring instanceof S3Keyring) {
1072+
S3Keyring keyring = (S3Keyring) _keyring;
1073+
if (!keyring.enableLegacyWrappingAlgorithms()) {
1074+
throw new S3EncryptionClientException("Legacy wrapping algorithms are not enabled for this keyring");
1075+
}
1076+
}
10701077

10711078
if (_bufferSize >= 0) {
10721079
if (_enableDelayedAuthenticationMode) {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ protected S3Keyring(Builder<?, ?> builder) {
3333
_dataKeyGenerator = builder._dataKeyGenerator;
3434
}
3535

36+
/**
37+
* @return true if legacy wrapping algorithms are enabled, false otherwise
38+
*/
39+
public boolean enableLegacyWrappingAlgorithms() {
40+
return _enableLegacyWrappingAlgorithms;
41+
}
42+
3643
/**
3744
* Generates a data key using the provided EncryptionMaterials and the configured DataKeyGenerator.
3845
* <p>

src/test/java/software/amazon/encryption/s3/S3EncryptionClientCompatibilityTest.java

Lines changed: 44 additions & 229 deletions
Original file line numberDiff line numberDiff line change
@@ -971,251 +971,66 @@ public void nullMaterialDescriptionV3() {
971971

972972
@Test
973973
public void validateAgainstSettingLegacyWrappingOnClientWithAesKeyringPassedV1toV3() {
974-
final String objectKey = appendTestSuffix("validate-against-setting-legacy-wrapping-on-client-with-aes-keyring-v1-to-v3");
975-
final String input = "Validate Against Setting Legacy Wrapping On Client With AES Keyring V1 to V3";
976-
977-
EncryptionMaterialsProvider materialsProvider =
978-
new StaticEncryptionMaterialsProvider(new EncryptionMaterials(AES_KEY));
979-
CryptoConfiguration v1CryptoConfig =
980-
new CryptoConfiguration(CryptoMode.AuthenticatedEncryption);
981-
AmazonS3Encryption v1Client = AmazonS3EncryptionClient.encryptionBuilder()
982-
.withCryptoConfiguration(v1CryptoConfig)
983-
.withEncryptionMaterials(materialsProvider)
984-
.build();
985-
986-
v1Client.putObject(BUCKET, objectKey, input);
987-
988-
AesKeyring aesKeyring = AesKeyring.builder()
989-
.wrappingKey(AES_KEY)
990-
.build();
991-
992-
S3Client wrappedClient = S3Client.create();
993-
S3Client v3Client = S3EncryptionClient.builder()
994-
.keyring(aesKeyring)
995-
.wrappedClient(wrappedClient)
996-
.enableLegacyUnauthenticatedModes(true)
997-
.enableLegacyWrappingAlgorithms(true)
998-
.build();
999-
1000974
try {
1001-
ResponseBytes<GetObjectResponse> output = v3Client.getObjectAsBytes(builder -> builder
1002-
.bucket(BUCKET)
1003-
.key(objectKey));
975+
AesKeyring aesKeyring = AesKeyring.builder()
976+
.wrappingKey(AES_KEY)
977+
.build();
978+
979+
S3Client wrappedClient = S3Client.create();
980+
S3Client v3Client = S3EncryptionClient.builder()
981+
.keyring(aesKeyring)
982+
.wrappedClient(wrappedClient)
983+
.enableLegacyWrappingAlgorithms(true)
984+
.enableLegacyUnauthenticatedModes(true)
985+
.build();
1004986
throw new RuntimeException("Expected failure");
1005-
} catch (Exception e) {
1006-
assertTrue(e.getMessage().contains("Enable legacy wrapping algorithms to use legacy key wrapping algorithm: AESWrap"));
987+
} catch (S3EncryptionClientException e) {
988+
assertTrue(e.getMessage().contains("Legacy wrapping algorithms are not enabled for this keyring"));
1007989
}
1008990

1009-
deleteObject(BUCKET, objectKey, v3Client);
1010-
v3Client.close();
1011-
}
1012-
1013-
@Test
1014-
public void validateAgainstSettingLegacyWrappingOnClientWithAesKeyringPassedV1toV3EncryptionOnly() {
1015-
final String objectKey = appendTestSuffix("validate-against-setting-legacy-wrapping-on-client-with-aes-keyring-v1-to-v3-encryption-only");
1016-
final String input = "Validate Against Setting Legacy Wrapping On Client With AES Keyring V1 to V3";
1017-
1018-
EncryptionMaterialsProvider materialsProvider =
1019-
new StaticEncryptionMaterialsProvider(new EncryptionMaterials(AES_KEY));
1020-
CryptoConfiguration v1CryptoConfig =
1021-
new CryptoConfiguration(CryptoMode.EncryptionOnly);
1022-
AmazonS3Encryption v1Client = AmazonS3EncryptionClient.encryptionBuilder()
1023-
.withCryptoConfiguration(v1CryptoConfig)
1024-
.withEncryptionMaterials(materialsProvider)
1025-
.build();
1026-
1027-
v1Client.putObject(BUCKET, objectKey, input);
1028-
1029-
AesKeyring aesKeyring = AesKeyring.builder()
1030-
.wrappingKey(AES_KEY)
1031-
.build();
1032-
1033-
S3Client wrappedClient = S3Client.create();
1034-
S3Client v3Client = S3EncryptionClient.builder()
1035-
.keyring(aesKeyring)
1036-
.wrappedClient(wrappedClient)
1037-
.enableLegacyUnauthenticatedModes(true)
1038-
.enableLegacyWrappingAlgorithms(true)
1039-
.build();
1040-
1041-
try {
1042-
ResponseBytes<GetObjectResponse> output = v3Client.getObjectAsBytes(builder -> builder
1043-
.bucket(BUCKET)
1044-
.key(objectKey));
1045-
throw new RuntimeException("Expected failure");
1046-
} catch (Exception e) {
1047-
assertTrue(e.getMessage().contains("Enable legacy wrapping algorithms to use legacy key wrapping algorithm: AES"));
1048-
}
1049-
1050-
deleteObject(BUCKET, objectKey, v3Client);
1051-
v3Client.close();
1052991
}
1053992

1054993
@Test
1055994
public void validateAgainstSettingLegacyWrappingOnClientWithRsaKeyringPassedV1toV3() {
1056-
final String objectKey = appendTestSuffix("validate-against-setting-legacy-wrapping-on-client-with-rsa-keyring-v1-to-v3");
1057-
final String input = "Validate Against Setting Legacy Wrapping On Client With RSA Keyring V1 to V3";
1058-
1059-
EncryptionMaterialsProvider materialsProvider =
1060-
new StaticEncryptionMaterialsProvider(new EncryptionMaterials(RSA_KEY_PAIR));
1061-
CryptoConfiguration v1CryptoConfig =
1062-
new CryptoConfiguration(CryptoMode.StrictAuthenticatedEncryption);
1063-
AmazonS3Encryption v1Client = AmazonS3EncryptionClient.encryptionBuilder()
1064-
.withCryptoConfiguration(v1CryptoConfig)
1065-
.withEncryptionMaterials(materialsProvider)
1066-
.build();
1067-
1068-
v1Client.putObject(BUCKET, objectKey, input);
1069-
1070-
PartialRsaKeyPair partialRsaKeyPair = PartialRsaKeyPair.builder()
1071-
.publicKey(RSA_KEY_PAIR.getPublic())
1072-
.privateKey(RSA_KEY_PAIR.getPrivate())
1073-
.build();
1074-
1075-
RsaKeyring rsaKeyring = RsaKeyring.builder()
1076-
.wrappingKeyPair(partialRsaKeyPair)
1077-
.build();
1078-
1079-
S3Client wrappedClient = S3Client.create();
1080-
S3Client v3Client = S3EncryptionClient.builder()
1081-
.keyring(rsaKeyring)
1082-
.wrappedClient(wrappedClient)
1083-
.enableLegacyUnauthenticatedModes(true)
1084-
.enableLegacyWrappingAlgorithms(true)
1085-
.build();
1086-
1087995
try {
1088-
ResponseBytes<GetObjectResponse> output = v3Client.getObjectAsBytes(builder -> builder
1089-
.bucket(BUCKET)
1090-
.key(objectKey));
996+
PartialRsaKeyPair partialRsaKeyPair = PartialRsaKeyPair.builder()
997+
.publicKey(RSA_KEY_PAIR.getPublic())
998+
.privateKey(RSA_KEY_PAIR.getPrivate())
999+
.build();
1000+
1001+
RsaKeyring rsaKeyring = RsaKeyring.builder()
1002+
.wrappingKeyPair(partialRsaKeyPair)
1003+
.build();
1004+
1005+
S3Client wrappedClient = S3Client.create();
1006+
S3Client v3Client = S3EncryptionClient.builder()
1007+
.keyring(rsaKeyring)
1008+
.wrappedClient(wrappedClient)
1009+
.enableLegacyWrappingAlgorithms(true)
1010+
.enableLegacyUnauthenticatedModes(true)
1011+
.build();
10911012
throw new RuntimeException("Expected failure");
1092-
} catch (Exception e) {
1093-
assertTrue(e.getMessage().contains("Enable legacy wrapping algorithms to use legacy key wrapping algorithm: RSA/ECB/OAEPWithSHA-256AndMGF1Padding"));
1094-
}
1095-
1096-
deleteObject(BUCKET, objectKey, v3Client);
1097-
v3Client.close();
1098-
}
1099-
1100-
@Test
1101-
public void validateAgainstSettingLegacyWrappingOnClientWithRsaKeyringPassedV1toV3EncryptionOnly() {
1102-
final String objectKey = appendTestSuffix("validate-against-setting-legacy-wrapping-on-client-with-rsa-keyring-v1-to-v3-encryption-only");
1103-
final String input = "Validate Against Setting Legacy Wrapping On Client With RSA Keyring V1 to V3";
1104-
1105-
EncryptionMaterialsProvider materialsProvider =
1106-
new StaticEncryptionMaterialsProvider(new EncryptionMaterials(RSA_KEY_PAIR));
1107-
CryptoConfiguration v1CryptoConfig =
1108-
new CryptoConfiguration(CryptoMode.EncryptionOnly);
1109-
AmazonS3Encryption v1Client = AmazonS3EncryptionClient.encryptionBuilder()
1110-
.withCryptoConfiguration(v1CryptoConfig)
1111-
.withEncryptionMaterials(materialsProvider)
1112-
.build();
1113-
1114-
v1Client.putObject(BUCKET, objectKey, input);
1115-
1116-
PartialRsaKeyPair partialRsaKeyPair = PartialRsaKeyPair.builder()
1117-
.publicKey(RSA_KEY_PAIR.getPublic())
1118-
.privateKey(RSA_KEY_PAIR.getPrivate())
1119-
.build();
1120-
1121-
RsaKeyring rsaKeyring = RsaKeyring.builder()
1122-
.wrappingKeyPair(partialRsaKeyPair)
1123-
.build();
1124-
1125-
S3Client wrappedClient = S3Client.create();
1126-
S3Client v3Client = S3EncryptionClient.builder()
1127-
.keyring(rsaKeyring)
1128-
.wrappedClient(wrappedClient)
1129-
.enableLegacyUnauthenticatedModes(true)
1130-
.enableLegacyWrappingAlgorithms(true)
1131-
.build();
1132-
1133-
try {
1134-
ResponseBytes<GetObjectResponse> output = v3Client.getObjectAsBytes(builder -> builder
1135-
.bucket(BUCKET)
1136-
.key(objectKey));
1137-
throw new RuntimeException("Expected failure");
1138-
} catch (Exception e) {
1139-
assertTrue(e.getMessage().contains("Enable legacy wrapping algorithms to use legacy key wrapping algorithm: RSA"));
1140-
}
1141-
1142-
deleteObject(BUCKET, objectKey, v3Client);
1143-
v3Client.close();
1144-
}
1145-
1146-
@Test
1147-
public void validateAgainstSettingLegacyWrappingOnClientWithKmsKeyringPassedV1toV3EncryptionOnly() {
1148-
final String objectKey = appendTestSuffix("validate-against-setting-legacy-wrapping-on-client-with-kms-keyring-v1-to-v3-encryption-only");
1149-
final String input = "Validate Against Setting Legacy Wrapping On Client With KMS Keyring V1 to V3";
1150-
1151-
KMSEncryptionMaterials kmsMaterials = new KMSEncryptionMaterials(KMS_KEY_ID);
1152-
kmsMaterials.addDescription("user-metadata-key", "user-metadata-value-v1-to-v3");
1153-
EncryptionMaterialsProvider materialsProvider = new KMSEncryptionMaterialsProvider(kmsMaterials);
1154-
1155-
CryptoConfiguration v1CryptoConfig =
1156-
new CryptoConfiguration(CryptoMode.EncryptionOnly);
1157-
1158-
AmazonS3Encryption v1Client = AmazonS3EncryptionClient.encryptionBuilder()
1159-
.withCryptoConfiguration(v1CryptoConfig)
1160-
.withEncryptionMaterials(materialsProvider)
1161-
.build();
1162-
1163-
v1Client.putObject(BUCKET, objectKey, input);
1164-
S3Client wrappedClient = S3Client.create();
1165-
S3Client v3Client = S3EncryptionClient.builder()
1166-
.keyring(KmsKeyring.builder()
1167-
.wrappingKeyId(KMS_KEY_ID)
1168-
.build())
1169-
.wrappedClient(wrappedClient)
1170-
.enableLegacyUnauthenticatedModes(true)
1171-
.enableLegacyWrappingAlgorithms(true)
1172-
.build();
1173-
1174-
try {
1175-
ResponseBytes<GetObjectResponse> output = v3Client.getObjectAsBytes(builder -> builder
1176-
.bucket(BUCKET)
1177-
.key(objectKey));
1178-
throw new RuntimeException("Expected failure");
1179-
} catch (Exception e) {
1180-
assertTrue(e.getMessage().contains("Enable legacy wrapping algorithms to use legacy key wrapping algorithm: kms"));
1013+
} catch (S3EncryptionClientException e) {
1014+
assertTrue(e.getMessage().contains("Legacy wrapping algorithms are not enabled for this keyring"));
11811015
}
11821016
}
11831017

11841018
@Test
11851019
public void validateAgainstSettingLegacyWrappingOnClientWithKmsKeyringPassedV1toV3() {
1186-
final String objectKey = appendTestSuffix("validate-against-setting-legacy-wrapping-on-client-with-kms-keyring-v1-to-v3");
1187-
final String input = "Validate Against Setting Legacy Wrapping On Client With KMS Keyring V1 to V3";
1188-
1189-
KMSEncryptionMaterials kmsMaterials = new KMSEncryptionMaterials(KMS_KEY_ID);
1190-
kmsMaterials.addDescription("user-metadata-key", "user-metadata-value-v1-to-v3");
1191-
EncryptionMaterialsProvider materialsProvider = new KMSEncryptionMaterialsProvider(kmsMaterials);
1192-
1193-
CryptoConfiguration v1CryptoConfig =
1194-
new CryptoConfiguration(CryptoMode.AuthenticatedEncryption);
1195-
1196-
AmazonS3Encryption v1Client = AmazonS3EncryptionClient.encryptionBuilder()
1197-
.withCryptoConfiguration(v1CryptoConfig)
1198-
.withEncryptionMaterials(materialsProvider)
1199-
.build();
1200-
1201-
v1Client.putObject(BUCKET, objectKey, input);
1202-
S3Client wrappedClient = S3Client.create();
1203-
S3Client v3Client = S3EncryptionClient.builder()
1204-
.keyring(KmsKeyring.builder()
1205-
.wrappingKeyId(KMS_KEY_ID)
1206-
.build())
1207-
.wrappedClient(wrappedClient)
1208-
.enableLegacyUnauthenticatedModes(true)
1209-
.enableLegacyWrappingAlgorithms(true)
1210-
.build();
1211-
12121020
try {
1213-
ResponseBytes<GetObjectResponse> output = v3Client.getObjectAsBytes(builder -> builder
1214-
.bucket(BUCKET)
1215-
.key(objectKey));
1216-
throw new RuntimeException("Expected failure");
1217-
} catch (Exception e) {
1218-
assertTrue(e.getMessage().contains("Enable legacy wrapping algorithms to use legacy key wrapping algorithm: kms"));
1021+
KmsKeyring kmsKeyring = KmsKeyring.builder()
1022+
.wrappingKeyId(KMS_KEY_ID)
1023+
.build();
1024+
1025+
S3Client wrappedClient = S3Client.create();
1026+
S3Client v3Client = S3EncryptionClient.builder()
1027+
.keyring(kmsKeyring)
1028+
.wrappedClient(wrappedClient)
1029+
.enableLegacyWrappingAlgorithms(true)
1030+
.enableLegacyUnauthenticatedModes(true)
1031+
.build();
1032+
} catch (S3EncryptionClientException e) {
1033+
assertTrue(e.getMessage().contains("Legacy wrapping algorithms are not enabled for this keyring"));
12191034
}
12201035
}
12211036

0 commit comments

Comments
 (0)