22
22
*/
23
23
package aws .example .s3 ;
24
24
25
+ // snippet-start:[s3.java1.s3_encrypt.complete]
26
+ // snippet-start:[s3.java1.s3_encrypt.import]
27
+
25
28
import com .amazonaws .regions .Region ;
26
29
import com .amazonaws .regions .Regions ;
27
30
import com .amazonaws .services .s3 .AmazonS3 ;
34
37
import com .amazonaws .services .s3 .model .GetObjectRequest ;
35
38
import com .amazonaws .services .s3 .model .KMSEncryptionMaterialsProvider ;
36
39
import com .amazonaws .services .s3 .model .StaticEncryptionMaterialsProvider ;
40
+
37
41
import java .security .KeyPair ;
38
42
import java .security .KeyPairGenerator ;
39
43
import java .security .NoSuchAlgorithmException ;
40
44
import javax .crypto .KeyGenerator ;
41
45
import javax .crypto .SecretKey ;
46
+ // snippet-end:[s3.java1.s3_encrypt.import]
42
47
43
48
/**
44
49
* Test out various cryptography settings for S3.
45
- *
50
+ * <p>
46
51
* This code expects that you have AWS credentials set up per:
47
52
* http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
48
53
* This code also requires you to install the Unlimited Strength Java(TM) Cryptography Extension Policy Files (JCE)
49
54
* You can install this from the oracle site: http://www.oracle.com
50
55
*/
51
- public class S3Encrypt
52
- {
56
+ public class S3Encrypt {
53
57
public static final String BUCKET_NAME = "s3EncryptTestBucket" ; //add your bucket name
54
58
public static final String ENCRYPTED_KEY = "enc-key" ;
55
59
public static final String NON_ENCRYPTED_KEY = "some-key" ;
56
60
57
- public static void main (String [] args )
58
- {
61
+ public static void main (String [] args ) {
59
62
System .out .println ("calling encryption with customer managed keys" );
60
63
S3Encrypt encrypt = new S3Encrypt ();
61
64
@@ -72,7 +75,9 @@ public static void main(String[] args)
72
75
* encryption requires the bouncy castle provider to be on the classpath. Also, for authenticated encryption the size
73
76
* of the data can be no longer than 64 GB.
74
77
*/
78
+ // snippet-start:[s3.java1.s3_encrypt.authenticated_encryption]
75
79
public void authenticatedEncryption_CustomerManagedKey () throws NoSuchAlgorithmException {
80
+ // snippet-start:[s3.java1.s3_encrypt.authenticated_encryption_build]
76
81
SecretKey secretKey = KeyGenerator .getInstance ("AES" ).generateKey ();
77
82
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
78
83
.standard ()
@@ -82,19 +87,23 @@ public void authenticatedEncryption_CustomerManagedKey() throws NoSuchAlgorithmE
82
87
.build ();
83
88
84
89
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder .defaultClient ();
90
+ // snippet-end:[s3.java1.s3_encrypt.authenticated_encryption_build]
85
91
86
92
s3Encryption .putObject (BUCKET_NAME , ENCRYPTED_KEY , "some contents" );
87
93
s3NonEncrypt .putObject (BUCKET_NAME , NON_ENCRYPTED_KEY , "some other contents" );
88
94
System .out .println (s3Encryption .getObjectAsString (BUCKET_NAME , ENCRYPTED_KEY ));
89
95
System .out .println (s3Encryption .getObjectAsString (BUCKET_NAME , NON_ENCRYPTED_KEY ));
90
96
}
97
+ // snippet-end:[s3.java1.s3_encrypt.authenticated_encryption]
91
98
92
99
/**
93
100
* For ranged GET we do not use authenticated encryption since we aren't reading the entire message and can't produce the
94
101
* MAC. Instead we use AES/CTR, an unauthenticated encryption algorithm. If {@link CryptoMode#StrictAuthenticatedEncryption}
95
102
* is enabled, ranged GETs will not be allowed since they do not use authenticated encryption..
96
103
*/
104
+ // snippet-start:[s3.java1.s3_encrypt.strict_authenticated_encryption]
97
105
public void authenticatedEncryption_RangeGet_CustomerManagedKey () throws NoSuchAlgorithmException {
106
+ // snippet-start:[s3.java1.s3_encrypt.strict_authenticated_encryption_build]
98
107
SecretKey secretKey = KeyGenerator .getInstance ("AES" ).generateKey ();
99
108
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
100
109
.standard ()
@@ -104,12 +113,14 @@ public void authenticatedEncryption_RangeGet_CustomerManagedKey() throws NoSuchA
104
113
.build ();
105
114
106
115
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder .defaultClient ();
116
+ // snippet-end:[s3.java1.s3_encrypt.strict_authenticated_encryption_build]
107
117
108
118
s3Encryption .putObject (BUCKET_NAME , ENCRYPTED_KEY , "some contents" );
109
119
s3NonEncrypt .putObject (BUCKET_NAME , NON_ENCRYPTED_KEY , "some other contents" );
110
120
System .out .println (s3Encryption .getObjectAsString (BUCKET_NAME , ENCRYPTED_KEY ));
111
121
System .out .println (s3Encryption .getObjectAsString (BUCKET_NAME , NON_ENCRYPTED_KEY ));
112
122
}
123
+ // snippet-end:[s3.java1.s3_encrypt.strict_authenticated_encryption]
113
124
114
125
/**
115
126
* Same as {@link #authenticatedEncryption_CustomerManagedKey()} except uses an asymmetric key pair and
@@ -204,6 +215,7 @@ public void encryptionOnly_CustomerManagedKey() throws NoSuchAlgorithmException
204
215
/**
205
216
* Non-authenticated encryption schemes can do range GETs without an issue.
206
217
*/
218
+ // snippet-start:[s3.java1.s3_encrypt.encryption_only]
207
219
public void encryptionOnly_RangeGet_CustomerManagedKey () throws NoSuchAlgorithmException {
208
220
SecretKey secretKey = KeyGenerator .getInstance ("AES" ).generateKey ();
209
221
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
@@ -215,15 +227,18 @@ public void encryptionOnly_RangeGet_CustomerManagedKey() throws NoSuchAlgorithmE
215
227
216
228
s3Encryption .putObject (BUCKET_NAME , ENCRYPTED_KEY , "some contents" );
217
229
System .out .println (s3Encryption .getObject (new GetObjectRequest (BUCKET_NAME , ENCRYPTED_KEY )
218
- .withRange (0 , 2 )));
230
+ .withRange (0 , 2 )));
219
231
}
232
+ // snippet-end:[s3.java1.s3_encrypt.encryption_only]
220
233
221
234
/**
222
235
* Uses an asymmetric key pair instead of a symmetric key. Note this does not change the algorithm used to encrypt
223
236
* the content, that will still be a symmetric key algorithm (AES/CBC in this case) using the derived CEK. It does impact
224
237
* the algorithm used to encrypt the CEK, in this case we use RSA/ECB/OAEPWithSHA-256AndMGF1Padding.
225
238
*/
239
+ // snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key]
226
240
public void encryptionOnly_CustomerManagedAsymetricKey () throws NoSuchAlgorithmException {
241
+ // snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_build]
227
242
KeyPair keyPair = KeyPairGenerator .getInstance ("RSA" ).generateKeyPair ();
228
243
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
229
244
.standard ()
@@ -233,17 +248,25 @@ public void encryptionOnly_CustomerManagedAsymetricKey() throws NoSuchAlgorithmE
233
248
.build ();
234
249
235
250
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder .defaultClient ();
251
+ // snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_build]
236
252
253
+ // snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_put_object]
237
254
s3Encryption .putObject (BUCKET_NAME , ENCRYPTED_KEY , "some contents" );
238
255
s3NonEncrypt .putObject (BUCKET_NAME , NON_ENCRYPTED_KEY , "some other contents" );
256
+ // snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_put_object]
257
+ // snippet-start:[s3.java1.s3_encrypt.encryption_only_asymetric_key_retrieve]
239
258
System .out .println (s3Encryption .getObjectAsString (BUCKET_NAME , ENCRYPTED_KEY ));
240
259
System .out .println (s3Encryption .getObjectAsString (BUCKET_NAME , NON_ENCRYPTED_KEY ));
260
+ // snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key_retrieve]
241
261
}
262
+ // snippet-end:[s3.java1.s3_encrypt.encryption_only_asymetric_key]
242
263
243
264
/**
244
265
* This uses the V2 metadata schema with a key wrap algorithm of 'kms' and a CEK algorithm of AES/CBC/PKCS5Padding.
245
266
*/
267
+ // snippet-start:[s3.java1.s3_encrypt.kms_encryption_only]
246
268
public void encryptionOnly_KmsManagedKey () throws NoSuchAlgorithmException {
269
+ // snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_build]
247
270
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
248
271
.standard ()
249
272
.withRegion (Regions .US_WEST_2 )
@@ -253,12 +276,18 @@ public void encryptionOnly_KmsManagedKey() throws NoSuchAlgorithmException {
253
276
.build ();
254
277
255
278
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder .defaultClient ();
279
+ // snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_build]
256
280
281
+ // snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_put_object]
257
282
s3Encryption .putObject (BUCKET_NAME , ENCRYPTED_KEY , "some contents" );
258
283
s3NonEncrypt .putObject (BUCKET_NAME , NON_ENCRYPTED_KEY , "some other contents" );
284
+ // snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_put_object]
285
+ // snippet-start:[s3.java1.s3_encrypt.kms_encryption_only_retrieve]
259
286
System .out .println (s3Encryption .getObjectAsString (BUCKET_NAME , ENCRYPTED_KEY ));
260
287
System .out .println (s3Encryption .getObjectAsString (BUCKET_NAME , NON_ENCRYPTED_KEY ));
288
+ // snippet-end:[s3.java1.s3_encrypt.kms_encryption_only_retrieve]
261
289
}
290
+ // snippet-end:[s3.java1.s3_encrypt.kms_encryption_only]
262
291
263
292
/**
264
293
* This uses the V2 metadata schema with a key wrap algorithm of 'kms' and a CEK algorithm of AES/GCM/NoPadding.
@@ -284,7 +313,9 @@ public void authenticatedEncryption_KmsManagedKey() throws NoSuchAlgorithmExcept
284
313
* Same as authenticatedEncryption_KmsManagedKey except throws an exception when trying to get objects not encrypted with
285
314
* AES/GCM.
286
315
*/
316
+ // snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption]
287
317
public void strictAuthenticatedEncryption_KmsManagedKey () throws NoSuchAlgorithmException {
318
+ // snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_builder]
288
319
AmazonS3Encryption s3Encryption = AmazonS3EncryptionClientBuilder
289
320
.standard ()
290
321
.withRegion (Regions .US_WEST_2 )
@@ -294,14 +325,23 @@ public void strictAuthenticatedEncryption_KmsManagedKey() throws NoSuchAlgorithm
294
325
.build ();
295
326
296
327
AmazonS3 s3NonEncrypt = AmazonS3ClientBuilder .defaultClient ();
328
+ // snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_builder]
297
329
330
+ // snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_put_object]
298
331
s3Encryption .putObject (BUCKET_NAME , ENCRYPTED_KEY , "some contents" );
299
332
s3NonEncrypt .putObject (BUCKET_NAME , NON_ENCRYPTED_KEY , "some other contents" );
333
+ // snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_put_object]
334
+ // snippet-start:[s3.java1.s3_encrypt.kms_authenticated_encryption_exception]
300
335
try {
301
336
s3Encryption .getObjectAsString (BUCKET_NAME , NON_ENCRYPTED_KEY );
302
337
} catch (SecurityException e ) {
303
338
// Strict authenticated encryption will throw an exception if an object is not encrypted with AES/GCM
304
339
System .err .println (NON_ENCRYPTED_KEY + " was not encrypted with AES/GCM" );
305
340
}
341
+
342
+ // snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption_exception]
306
343
}
344
+ // snippet-end:[s3.java1.s3_encrypt.kms_authenticated_encryption]
345
+
307
346
}
347
+ // snippet-end:[s3.java1.s3_encrypt.complete]
0 commit comments