|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package com.amazonaws.crypto.examples; |
| 5 | + |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import com.amazonaws.encryptionsdk.AwsCrypto; |
| 12 | +import com.amazonaws.encryptionsdk.CryptoResult; |
| 13 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKey; |
| 14 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider; |
| 15 | +import com.amazonaws.encryptionsdk.CommitmentPolicy; |
| 16 | +import com.amazonaws.encryptionsdk.kms.DiscoveryFilter; |
| 17 | + |
| 18 | +/** |
| 19 | + * <p> |
| 20 | + * Encrypts and then decrypts data using an AWS KMS customer master key in discovery mode. |
| 21 | + * Discovery mode is useful when you use an alias to identify a CMK when encrypting and the |
| 22 | + * underlying key ARN might vary in each AWS Region. |
| 23 | + * <p> |
| 24 | + * Arguments: |
| 25 | + * <ol> |
| 26 | + * <li>Key Name: An identifier for the AWS KMS customer master key (CMK) to use. For example, |
| 27 | + * a key ARN or a key alias. |
| 28 | + * For help finding the Amazon Resource Name (ARN) of your AWS KMS customer master |
| 29 | + * key (CMK), see 'Viewing Keys' at http://docs.aws.amazon.com/kms/latest/developerguide/viewing-keys.html |
| 30 | + * <li>Partition: The partition of the AWS KMS customer master key, which is usually "aws." |
| 31 | + * A partition is a group of regions. The partition is the second element in the key ARN, e.g. "arn" in "aws:aws: ..." |
| 32 | + * <li>Account ID: The identifier for the account of the AWS KMS customer master key. |
| 33 | + * </ol> |
| 34 | + */ |
| 35 | +public class DiscoveryDecryptionExample { |
| 36 | + |
| 37 | + private static final byte[] EXAMPLE_DATA = "Hello World".getBytes(StandardCharsets.UTF_8); |
| 38 | + |
| 39 | + public static void main(final String[] args) { |
| 40 | + final String keyName = args[0]; |
| 41 | + final String partition = args[1]; |
| 42 | + final String accountId = args[2]; |
| 43 | + |
| 44 | + encryptAndDecrypt(keyName, partition, accountId); |
| 45 | + } |
| 46 | + |
| 47 | + static void encryptAndDecrypt(final String keyName, final String partition, final String accountId) { |
| 48 | + // 1. Instantiate the SDK with a specific commitment policy. |
| 49 | + // ForbidEncryptAllowDecrypt is the only available policy in 1.7.0. |
| 50 | + final AwsCrypto crypto = AwsCrypto.builder().withCommitmentPolicy(CommitmentPolicy.ForbidEncryptAllowDecrypt).build(); |
| 51 | + |
| 52 | + // 2. Instantiate an AWS KMS master key provider for encryption. |
| 53 | + // |
| 54 | + // In strict mode (`buildStrict`), the AWS KMS master key provider encrypts and decrypts only by using the key |
| 55 | + // indicated by keyName. |
| 56 | + final KmsMasterKeyProvider encryptingKeyProvider = KmsMasterKeyProvider.builder().buildStrict(keyName); |
| 57 | + |
| 58 | + // 3. Create an encryption context |
| 59 | + // |
| 60 | + // Most encrypted data should have an associated encryption context |
| 61 | + // to protect integrity. This sample uses placeholder values. |
| 62 | + // |
| 63 | + // For more information see: |
| 64 | + // blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management |
| 65 | + final Map<String, String> encryptionContext = Collections.singletonMap("ExampleContextKey", "ExampleContextValue"); |
| 66 | + |
| 67 | + // 4. Encrypt the data |
| 68 | + final CryptoResult<byte[], KmsMasterKey> encryptResult = crypto.encryptData(encryptingKeyProvider, EXAMPLE_DATA, encryptionContext); |
| 69 | + final byte[] ciphertext = encryptResult.getResult(); |
| 70 | + |
| 71 | + // 5. Instantiate a discovery filter for decrypting. This filter restricts what AWS KMS CMKs the |
| 72 | + // AWS KMS master key provider can use to those in a particular AWS partition and account. |
| 73 | + // You can create a similar filter with one partition and multiple AWS accounts. |
| 74 | + // This example only configures the filter with one account, but more may be specified |
| 75 | + // as long as they exist within the same partition. |
| 76 | + // This filter is not required for Discovery mode, but is a best practice. |
| 77 | + final DiscoveryFilter discoveryFilter = new DiscoveryFilter(partition, accountId); |
| 78 | + |
| 79 | + // 6. Instantiate an AWS KMS master key provider for decryption in discovery mode (`buildDiscovery`) with a |
| 80 | + // Discovery Filter. |
| 81 | + // |
| 82 | + // In discovery mode, the AWS KMS master key provider attempts to decrypt only by using AWS KMS ARNs |
| 83 | + // indicated in the encrypted message. |
| 84 | + // By configuring the master key provider with a Discovery Filter, this master key provider |
| 85 | + // attempts to decrypt AWS KMS CMKs only in the configured partition and accounts. |
| 86 | + final KmsMasterKeyProvider decryptingKeyProvider = KmsMasterKeyProvider.builder().buildDiscovery(discoveryFilter); |
| 87 | + |
| 88 | + // 7. Decrypt the data |
| 89 | + final CryptoResult<byte[], KmsMasterKey> decryptResult = crypto.decryptData(decryptingKeyProvider, ciphertext); |
| 90 | + |
| 91 | + // 8. Verify that the encryption context in the result contains the |
| 92 | + // encryption context supplied to the encryptData method. Because the |
| 93 | + // SDK can add values to the encryption context, don't require that |
| 94 | + // the entire context matches. |
| 95 | + if (!encryptionContext.entrySet().stream() |
| 96 | + .allMatch(e -> e.getValue().equals(decryptResult.getEncryptionContext().get(e.getKey())))) { |
| 97 | + throw new IllegalStateException("Wrong Encryption Context!"); |
| 98 | + } |
| 99 | + |
| 100 | + // 9. Verify that the decrypted plaintext matches the original plaintext |
| 101 | + assert Arrays.equals(decryptResult.getResult(), EXAMPLE_DATA); |
| 102 | + } |
| 103 | +} |
0 commit comments