|
| 1 | +.. Copyright 2010-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +
|
| 3 | + This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 | + International License (the "License"). You may not use this file except in compliance with the |
| 5 | + License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 | +
|
| 7 | + This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 | + either express or implied. See the License for the specific language governing permissions and |
| 9 | + limitations under the License. |
| 10 | +
|
| 11 | +########################################################## |
| 12 | +|S3| Client-Side Encryption with Client Master Keys |
| 13 | +########################################################## |
| 14 | + |
| 15 | +.. meta:: |
| 16 | + :description: How to use the cryptography configuration settings for the AWS SDK for Java |
| 17 | + :keywords: cryptography, encryption, example code |
| 18 | + |
| 19 | +The following examples use the |
| 20 | +:aws-java-class:`AmazonS3EncryptionClientBuilder <services/s3/AmazonS3EncryptionClientBuilder>` |
| 21 | +to create an |S3| client with client-side encryption enabled. Once configured, |
| 22 | +any object you upload to |S3| using this client |
| 23 | +will be encrypted. Any objects you get from |S3| using this client will automatically |
| 24 | +be decrypted. |
| 25 | + |
| 26 | +.. note:: |
| 27 | + The examples here demonstrate using the |S3| client-side |
| 28 | + encryption with customer managed client master keys. To learn how to use encryption |
| 29 | + with |KMS| managed keys, see the :doc:`examples-crypto-kms` topic. |
| 30 | + |
| 31 | +You can choose from three encryption modes when enabling client-side |S3| encryption. |
| 32 | +The sections below show how to enable each type. To learn which algorithms each mode uses, |
| 33 | +see :aws-java-class:`CryptoMode <services/s3/model/CryptoMode>` definition. |
| 34 | + |
| 35 | +Required Imports |
| 36 | +================ |
| 37 | + |
| 38 | +Import the following libraries for the examples on this page. |
| 39 | + |
| 40 | +**Imports** |
| 41 | + |
| 42 | +.. literalinclude:: example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java |
| 43 | + :lines: 16-24,26-31 |
| 44 | + :language: java |
| 45 | + |
| 46 | +.. _encryption-only: |
| 47 | + |
| 48 | +Encryption only Mode |
| 49 | +==================== |
| 50 | + |
| 51 | +This is the default mode, if no :classname:`CryptoMode` is not specified. To use a |
| 52 | +symmetric key for encryption, pass the symmetric key to the |
| 53 | +:aws-java-class:`EncryptionMaterials` constructor. The example below uses |
| 54 | +the Java class :class:`KeyGenerator` to generate a symmetric private key. |
| 55 | + |
| 56 | +**Code** |
| 57 | + |
| 58 | +.. literalinclude:: example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java |
| 59 | + :lines: 198-204 |
| 60 | + :dedent: 8 |
| 61 | + :language: java |
| 62 | + |
| 63 | +To use an asymmetric key, simply pass the key pair to the same class |
| 64 | +:aws-java-class:`EncryptionMaterials`. The example below uses the |
| 65 | +:class:`KeyPairGenerator` class to generate a key pair. |
| 66 | + |
| 67 | +**Code** |
| 68 | + |
| 69 | +.. literalinclude:: example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java |
| 70 | + :lines: 217-223 |
| 71 | + :dedent: 8 |
| 72 | + :language: java |
| 73 | + |
| 74 | +You can retrieve the object with the same client. |
| 75 | + |
| 76 | +**Code** |
| 77 | + |
| 78 | +.. literalinclude:: example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java |
| 79 | + :lines: 229 |
| 80 | + :dedent: 8 |
| 81 | + :language: java |
| 82 | + |
| 83 | +See the :sdk-examples-java-s3:`complete example <S3Encrypt.java>`. |
| 84 | + |
| 85 | +.. _authenticated-encryption: |
| 86 | + |
| 87 | +Authenticated Encryption Mode |
| 88 | +============================= |
| 89 | + |
| 90 | +When :classname:`AuthenticatedEncryption` mode is used, an improved key wrapping algorithm is applied |
| 91 | +applied during encryption. When decrypting in this mode, the algorithm is able to verify the integrity |
| 92 | +of the decrypted object and throw an exception if the check fails. |
| 93 | +To get more details about how authenticated encryption works, see the |
| 94 | +:blog:`Amazon S3 Client-Side Authenticated Encryption <developer/amazon-s3-client-side-authenticated-encryption>` |
| 95 | +blog post. |
| 96 | + |
| 97 | +To use client-side authenticated encryption, two steps are required: |
| 98 | + |
| 99 | + #. Include the latest `Bouncy Castle jar <https://www.bouncycastle.org/latest_releases.html>`_ |
| 100 | + in the classpath. |
| 101 | + #. Explicitly specify the cryptographic mode of authenticated encryption when |
| 102 | + instantiating an S3 encryption client. |
| 103 | + |
| 104 | +Use the :aws-java-class:`CryptoMode <service/s3/model/CryptoMode>` to specify |
| 105 | +:classname:`AuthenticatedEncryption`. |
| 106 | + |
| 107 | +**Code** |
| 108 | + |
| 109 | +.. literalinclude:: example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java |
| 110 | + :lines: 67-72 |
| 111 | + :dedent: 8 |
| 112 | + :language: java |
| 113 | + |
| 114 | +The :classname:`AuthenticatedEncryption` mode can retrieve unencrypted objects as well as |
| 115 | +objects encrypted with :classname:`EncryptionOnly` mode. This example shows the |
| 116 | +|S3| encryption client retrieving an unencrypted object. |
| 117 | + |
| 118 | +**Code** |
| 119 | + |
| 120 | +.. literalinclude:: example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java |
| 121 | + :lines: 66-79 |
| 122 | + :dedent: 8 |
| 123 | + :language: java |
| 124 | + |
| 125 | +See the :sdk-examples-java-s3:`complete example <S3Encrypt.java>`. |
| 126 | + |
| 127 | +.. _strict-authenticated-encryption: |
| 128 | + |
| 129 | +Strict Authenticated Encryption |
| 130 | +=============================== |
| 131 | + |
| 132 | +Use the :aws-java-class:`CryptoMode <service/s3/model/CryptoMode>` to specify |
| 133 | +:classname:`StrictAuthenticatedEncryption`. |
| 134 | + |
| 135 | +**Code** |
| 136 | + |
| 137 | +.. literalinclude:: example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java |
| 138 | + :lines: 132-137 |
| 139 | + :dedent: 8 |
| 140 | + :language: java |
| 141 | + |
| 142 | +In :classname:`StrictAuthenticatedEncryption`, the |S3| client will throw an |
| 143 | +exception when retrieving an object that was not encrypted using an |
| 144 | +authenticated mode. |
| 145 | + |
| 146 | +**Code** |
| 147 | + |
| 148 | +.. literalinclude:: example_code/s3/src/main/java/aws/example/s3/S3Encrypt.java |
| 149 | + :lines: 131-149 |
| 150 | + :dedent: 8 |
| 151 | + :language: java |
| 152 | + |
| 153 | +See the :sdk-examples-java-s3:`complete example <S3Encrypt.java>`. |
0 commit comments