66import com .amazonaws .services .s3 .AmazonS3EncryptionClient ;
77import com .amazonaws .services .s3 .AmazonS3EncryptionClientV2 ;
88import com .amazonaws .services .s3 .AmazonS3EncryptionV2 ;
9- import com .amazonaws .services .s3 .model .CryptoConfiguration ;
10- import com .amazonaws .services .s3 .model .CryptoConfigurationV2 ;
11- import com .amazonaws .services .s3 .model .CryptoMode ;
12- import com .amazonaws .services .s3 .model .EncryptedPutObjectRequest ;
13- import com .amazonaws .services .s3 .model .EncryptionMaterials ;
14- import com .amazonaws .services .s3 .model .EncryptionMaterialsProvider ;
15- import com .amazonaws .services .s3 .model .KMSEncryptionMaterialsProvider ;
16- import com .amazonaws .services .s3 .model .ObjectMetadata ;
17- import com .amazonaws .services .s3 .model .StaticEncryptionMaterialsProvider ;
9+ import com .amazonaws .services .s3 .model .*;
10+
1811import java .io .ByteArrayInputStream ;
1912import java .nio .charset .StandardCharsets ;
2013import java .security .KeyPair ;
3528public class S3EncryptionClientTest {
3629
3730 // TODO: make these dynamic
38- private static final String BUCKET = "845853869857-s3-research " ;
31+ private static final String BUCKET = "s3encryptionclient " ;
3932
40- private static final String KMS_MASTER_KEY = "e45015eb-1643-448f-9145-8ed4679138e4 " ;
33+ private static final String KMS_MASTER_KEY = "6c7db579-a16c-48c0-adea-604f6b449758 " ;
4134
42- private static final Region KMS_REGION = Region .getRegion (Regions .US_EAST_2 );
35+ private static final Region KMS_REGION = Region .getRegion (Regions .US_WEST_2 );
4336
4437 private static SecretKey AES_KEY ;
4538 private static KeyPair RSA_KEY_PAIR ;
@@ -117,6 +110,38 @@ public void AesWrapV1toV3() {
117110 assertEquals (input , output );
118111 }
119112
113+ @ Test
114+ public void AesWrapV1toV3WithInstructionMode () {
115+ final String BUCKET_KEY = "aes-wrap-v1-to-v3-with-instruction-storage-mode" ;
116+
117+ // V1 Client
118+ EncryptionMaterialsProvider materialsProvider =
119+ new StaticEncryptionMaterialsProvider (new EncryptionMaterials (AES_KEY ));
120+ CryptoConfiguration v1CryptoConfig =
121+ new CryptoConfiguration (CryptoMode .AuthenticatedEncryption )
122+ .withStorageMode (CryptoStorageMode .InstructionFile );
123+ AmazonS3Encryption v1Client = AmazonS3EncryptionClient .encryptionBuilder ()
124+ .withCryptoConfiguration (v1CryptoConfig )
125+ .withEncryptionMaterials (materialsProvider )
126+ .build ();
127+
128+ // V3 Client
129+ S3Client v3Client = S3EncryptionClient .builder ()
130+ .aesKey (AES_KEY )
131+ .enableLegacyModes (true )
132+ .build ();
133+
134+ // Asserts
135+ final String input = "AesGcmV1toV3" ;
136+ v1Client .putObject (BUCKET , BUCKET_KEY , input );
137+
138+ ResponseBytes <GetObjectResponse > objectResponse = v3Client .getObjectAsBytes (GetObjectRequest .builder ()
139+ .bucket (BUCKET )
140+ .key (BUCKET_KEY ).build ());
141+ String output = objectResponse .asUtf8String ();
142+ assertEquals (input , output );
143+ }
144+
120145 @ Test
121146 public void AesGcmV2toV3 () {
122147 final String BUCKET_KEY = "aes-gcm-v3-to-v2" ;
@@ -257,6 +282,38 @@ public void RsaEcbV1toV3() {
257282 assertEquals (input , output );
258283 }
259284
285+ @ Test
286+ public void RsaEcbV1toV3WithInstructionMode () {
287+ final String BUCKET_KEY = "rsa-ecb-v1-to-v3-with-instruction-mode" ;
288+
289+ // V1 Client
290+ EncryptionMaterialsProvider materialsProvider =
291+ new StaticEncryptionMaterialsProvider (new EncryptionMaterials (RSA_KEY_PAIR ));
292+ CryptoConfiguration v1CryptoConfig =
293+ new CryptoConfiguration (CryptoMode .AuthenticatedEncryption )
294+ .withStorageMode (CryptoStorageMode .InstructionFile );;
295+ AmazonS3Encryption v1Client = AmazonS3EncryptionClient .encryptionBuilder ()
296+ .withCryptoConfiguration (v1CryptoConfig )
297+ .withEncryptionMaterials (materialsProvider )
298+ .build ();
299+
300+ // V3 Client
301+ S3Client v3Client = S3EncryptionClient .builder ()
302+ .rsaKeyPair (RSA_KEY_PAIR )
303+ .enableLegacyModes (true )
304+ .build ();
305+
306+ // Asserts
307+ final String input = "RsaEcbV1toV3" ;
308+ v1Client .putObject (BUCKET , BUCKET_KEY , input );
309+
310+ ResponseBytes <GetObjectResponse > objectResponse = v3Client .getObjectAsBytes (GetObjectRequest .builder ()
311+ .bucket (BUCKET )
312+ .key (BUCKET_KEY ).build ());
313+ String output = objectResponse .asUtf8String ();
314+ assertEquals (input , output );
315+ }
316+
260317 @ Test
261318 public void RsaOaepV2toV3 () {
262319 final String BUCKET_KEY = "rsa-oaep-v2-to-v3" ;
@@ -265,7 +322,40 @@ public void RsaOaepV2toV3() {
265322 EncryptionMaterialsProvider materialsProvider =
266323 new StaticEncryptionMaterialsProvider (new EncryptionMaterials (RSA_KEY_PAIR ));
267324 CryptoConfigurationV2 cryptoConfig =
268- new CryptoConfigurationV2 (CryptoMode .StrictAuthenticatedEncryption );
325+ new CryptoConfigurationV2 (CryptoMode .StrictAuthenticatedEncryption )
326+ .withStorageMode (CryptoStorageMode .InstructionFile );
327+ AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2 .encryptionBuilder ()
328+ .withCryptoConfiguration (cryptoConfig )
329+ .withEncryptionMaterialsProvider (materialsProvider )
330+ .build ();
331+
332+ // V3 Client
333+ S3Client v3Client = S3EncryptionClient .builder ()
334+ .rsaKeyPair (RSA_KEY_PAIR )
335+ .build ();
336+
337+ // Asserts
338+ final String input = "RsaOaepV2toV3" ;
339+ v2Client .putObject (BUCKET , BUCKET_KEY , input );
340+
341+ ResponseBytes <GetObjectResponse > objectResponse = v3Client .getObjectAsBytes (
342+ GetObjectRequest .builder ()
343+ .bucket (BUCKET )
344+ .key (BUCKET_KEY ).build ());
345+ String output = objectResponse .asUtf8String ();
346+ assertEquals (input , output );
347+ }
348+
349+ @ Test
350+ public void RsaOaepV2toV3WithInstructionMode () {
351+ final String BUCKET_KEY = "rsa-oaep-v2-to-v3-with-instruction-mode" ;
352+
353+ // V2 Client
354+ EncryptionMaterialsProvider materialsProvider =
355+ new StaticEncryptionMaterialsProvider (new EncryptionMaterials (RSA_KEY_PAIR ));
356+ CryptoConfigurationV2 cryptoConfig =
357+ new CryptoConfigurationV2 (CryptoMode .StrictAuthenticatedEncryption )
358+ .withStorageMode (CryptoStorageMode .InstructionFile );
269359 AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2 .encryptionBuilder ()
270360 .withCryptoConfiguration (cryptoConfig )
271361 .withEncryptionMaterialsProvider (materialsProvider )
@@ -402,6 +492,40 @@ public void KmsV1toV3() {
402492 assertEquals (input , output );
403493 }
404494
495+ @ Test
496+ public void KmsV1toV3WithInstructionMode () {
497+ final String BUCKET_KEY = "kms-v1-to-v3-with-instruction-mode" ;
498+
499+ // V1 Client
500+ EncryptionMaterialsProvider materialsProvider = new KMSEncryptionMaterialsProvider (KMS_MASTER_KEY );
501+
502+ CryptoConfiguration v1Config =
503+ new CryptoConfiguration (CryptoMode .AuthenticatedEncryption )
504+ .withStorageMode (CryptoStorageMode .InstructionFile )
505+ .withAwsKmsRegion (KMS_REGION );
506+
507+ AmazonS3Encryption v1Client = AmazonS3EncryptionClient .encryptionBuilder ()
508+ .withCryptoConfiguration (v1Config )
509+ .withEncryptionMaterials (materialsProvider )
510+ .build ();
511+
512+ // V3 Client
513+ S3Client v3Client = S3EncryptionClient .builder ()
514+ .kmsKeyId (KMS_MASTER_KEY )
515+ .enableLegacyModes (true )
516+ .build ();
517+
518+ // Asserts
519+ final String input = "KmsV1toV3" ;
520+ v1Client .putObject (BUCKET , BUCKET_KEY , input );
521+
522+ ResponseBytes <GetObjectResponse > objectResponse = v3Client .getObjectAsBytes (GetObjectRequest .builder ()
523+ .bucket (BUCKET )
524+ .key (BUCKET_KEY ).build ());
525+ String output = objectResponse .asUtf8String ();
526+ assertEquals (input , output );
527+ }
528+
405529 @ Test
406530 public void KmsContextV2toV3 () {
407531 final String BUCKET_KEY = "kms-context-v2-to-v3" ;
0 commit comments