|
| 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.masterkeyprovider.awskms; |
| 5 | + |
| 6 | +import com.amazonaws.encryptionsdk.AwsCrypto; |
| 7 | +import com.amazonaws.encryptionsdk.CryptoResult; |
| 8 | +import com.amazonaws.encryptionsdk.kms.AwsKmsCmkId; |
| 9 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKey; |
| 10 | +import com.amazonaws.encryptionsdk.kms.KmsMasterKeyProvider; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | + |
| 18 | +import static java.util.stream.Collectors.toList; |
| 19 | + |
| 20 | +/** |
| 21 | + * This example is intended to serve as reference material for users migrating away from master key providers. |
| 22 | + * We recommend using keyrings rather than master key providers. |
| 23 | + * For examples using keyrings, see the 'examples/keyring' directory. |
| 24 | + * <p> |
| 25 | + * This example shows how to configure and use a KMS master key provider with with CMKs in multiple regions. |
| 26 | + * <p> |
| 27 | + * https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#master-key-provider |
| 28 | + * <p> |
| 29 | + * For an example of how to use the KMS master key with a single CMK, |
| 30 | + * see the {@link SingleCmk} example. |
| 31 | + * <p> |
| 32 | + * For an example of how to use the KMS master key provider in discovery mode on decrypt, |
| 33 | + * see the {@link DiscoveryDecrypt} example. |
| 34 | + */ |
| 35 | +public class MultipleRegions { |
| 36 | + |
| 37 | + /** |
| 38 | + * Demonstrate an encrypt/decrypt cycle using a KMS master key provider with CMKs in multiple regions. |
| 39 | + * |
| 40 | + * @param awsKmsGeneratorCmk The ARN of an AWS KMS CMK that protects data keys |
| 41 | + * @param awsKmsAdditionalCmks Additional ARNs of secondary KMS CMKs |
| 42 | + * @param sourcePlaintext Plaintext to encrypt |
| 43 | + */ |
| 44 | + public static void run(final AwsKmsCmkId awsKmsGeneratorCmk, final List<AwsKmsCmkId> awsKmsAdditionalCmks, final byte[] sourcePlaintext) { |
| 45 | + // Instantiate the AWS Encryption SDK. |
| 46 | + final AwsCrypto awsEncryptionSdk = new AwsCrypto(); |
| 47 | + |
| 48 | + // Prepare your encryption context. |
| 49 | + // https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context |
| 50 | + final Map<String, String> encryptionContext = new HashMap<>(); |
| 51 | + encryptionContext.put("encryption", "context"); |
| 52 | + encryptionContext.put("is not", "secret"); |
| 53 | + encryptionContext.put("but adds", "useful metadata"); |
| 54 | + encryptionContext.put("that can help you", "be confident that"); |
| 55 | + encryptionContext.put("the data you are handling", "is what you think it is"); |
| 56 | + |
| 57 | + // Create the master key provider that will encrypt your data keys under all requested CMKs. |
| 58 | + // |
| 59 | + // The KMS master key provider generates the data key using the first key ID in the list. |
| 60 | + final List<String> awsKmsCmks = new ArrayList<>(); |
| 61 | + awsKmsCmks.add(awsKmsGeneratorCmk.toString()); |
| 62 | + awsKmsCmks.addAll(awsKmsAdditionalCmks.stream().map(AwsKmsCmkId::toString).collect(toList())); |
| 63 | + final KmsMasterKeyProvider masterKeyProvider = KmsMasterKeyProvider.builder() |
| 64 | + .withKeysForEncryption(awsKmsCmks).build(); |
| 65 | + |
| 66 | + // Create master key providers that each only use one of the CMKs. |
| 67 | + // We will use these later to demonstrate that any of the CMKs can be used to decrypt the message. |
| 68 | + final KmsMasterKeyProvider singleCmkMasterKeyThatGenerated = KmsMasterKeyProvider.builder() |
| 69 | + .withKeysForEncryption(awsKmsGeneratorCmk.toString()).build(); |
| 70 | + final KmsMasterKeyProvider singleCmkMasterKeyThatEncrypted = KmsMasterKeyProvider.builder() |
| 71 | + .withKeysForEncryption(awsKmsAdditionalCmks.get(0).toString()).build(); |
| 72 | + |
| 73 | + // Encrypt your plaintext data using the master key provider that uses all requests CMKs. |
| 74 | + final CryptoResult<byte[], KmsMasterKey> encryptResult = awsEncryptionSdk.encryptData( |
| 75 | + masterKeyProvider, |
| 76 | + sourcePlaintext, |
| 77 | + encryptionContext); |
| 78 | + final byte[] ciphertext = encryptResult.getResult(); |
| 79 | + |
| 80 | + // Verify that the header contains the expected number of encrypted data keys (EDKs). |
| 81 | + // It should contain one EDK for each CMK. |
| 82 | + assert encryptResult.getHeaders().getEncryptedKeyBlobCount() == awsKmsAdditionalCmks.size() + 1; |
| 83 | + |
| 84 | + // Demonstrate that the ciphertext and plaintext are different. |
| 85 | + assert !Arrays.equals(ciphertext, sourcePlaintext); |
| 86 | + |
| 87 | + // Decrypt your encrypted data separately using the single-CMK master keys. |
| 88 | + // |
| 89 | + // You do not need to specify the encryption context on decrypt because |
| 90 | + // the header of the encrypted message includes the encryption context. |
| 91 | + final CryptoResult<byte[], KmsMasterKey> decryptResult1 = awsEncryptionSdk.decryptData( |
| 92 | + singleCmkMasterKeyThatGenerated, |
| 93 | + ciphertext); |
| 94 | + final byte[] decrypted1 = decryptResult1.getResult(); |
| 95 | + final CryptoResult<byte[], KmsMasterKey> decryptResult2 = awsEncryptionSdk.decryptData( |
| 96 | + singleCmkMasterKeyThatEncrypted, |
| 97 | + ciphertext); |
| 98 | + final byte[] decrypted2 = decryptResult2.getResult(); |
| 99 | + |
| 100 | + // Demonstrate that the decrypted plaintext is identical to the original plaintext. |
| 101 | + assert Arrays.equals(decrypted1, sourcePlaintext); |
| 102 | + assert Arrays.equals(decrypted2, sourcePlaintext); |
| 103 | + |
| 104 | + // Verify that the encryption context used in the decrypt operation includes |
| 105 | + // the encryption context that you specified when encrypting. |
| 106 | + // The AWS Encryption SDK can add pairs, so don't require an exact match. |
| 107 | + // |
| 108 | + // In production, always use a meaningful encryption context. |
| 109 | + encryptionContext.forEach((k, v) -> { |
| 110 | + assert v.equals(decryptResult1.getEncryptionContext().get(k)); |
| 111 | + assert v.equals(decryptResult2.getEncryptionContext().get(k)); |
| 112 | + }); |
| 113 | + } |
| 114 | +} |
0 commit comments