|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +""" |
| 4 | +Demonstrate an encrypt/decrypt cycle using a Required Encryption Context CMM. |
| 5 | +A required encryption context CMM asks for required keys in the encryption context field |
| 6 | +on encrypt such that they will not be stored on the message, but WILL be included in the header signature. |
| 7 | +On decrypt, the client MUST supply the key/value pair(s) that were not stored to successfully decrypt the message. |
| 8 | +""" |
| 9 | +import sys |
| 10 | + |
| 11 | +import boto3 |
| 12 | +# Ignore missing MPL for pylint, but the MPL is required for this example |
| 13 | +# noqa pylint: disable=import-error |
| 14 | +from aws_cryptographic_materialproviders.mpl import AwsCryptographicMaterialProviders |
| 15 | +from aws_cryptographic_materialproviders.mpl.config import MaterialProvidersConfig |
| 16 | +from aws_cryptographic_materialproviders.mpl.models import ( |
| 17 | + CreateAwsKmsKeyringInput, |
| 18 | + CreateDefaultCryptographicMaterialsManagerInput, |
| 19 | + CreateRequiredEncryptionContextCMMInput, |
| 20 | +) |
| 21 | +from aws_cryptographic_materialproviders.mpl.references import ICryptographicMaterialsManager, IKeyring |
| 22 | +from typing import Dict, List |
| 23 | + |
| 24 | +import aws_encryption_sdk |
| 25 | +from aws_encryption_sdk import CommitmentPolicy |
| 26 | +from aws_encryption_sdk.exceptions import AWSEncryptionSDKClientError |
| 27 | + |
| 28 | +# TODO-MPL: Remove this as part of removing PYTHONPATH hacks |
| 29 | +module_root_dir = '/'.join(__file__.split("/")[:-1]) |
| 30 | + |
| 31 | +sys.path.append(module_root_dir) |
| 32 | + |
| 33 | +EXAMPLE_DATA: bytes = b"Hello World" |
| 34 | + |
| 35 | + |
| 36 | +def encrypt_and_decrypt_with_keyring( |
| 37 | + kms_key_id: str |
| 38 | +): |
| 39 | + """Creates a hierarchical keyring using the provided resources, then encrypts and decrypts a string with it.""" |
| 40 | + # 1. Instantiate the encryption SDK client. |
| 41 | + # This builds the client with the REQUIRE_ENCRYPT_REQUIRE_DECRYPT commitment policy, |
| 42 | + # which enforces that this client only encrypts using committing algorithm suites and enforces |
| 43 | + # that this client will only decrypt encrypted messages that were created with a committing |
| 44 | + # algorithm suite. |
| 45 | + # This is the default commitment policy if you were to build the client as |
| 46 | + # `client = aws_encryption_sdk.EncryptionSDKClient()`. |
| 47 | + |
| 48 | + client = aws_encryption_sdk.EncryptionSDKClient( |
| 49 | + commitment_policy=CommitmentPolicy.REQUIRE_ENCRYPT_REQUIRE_DECRYPT |
| 50 | + ) |
| 51 | + |
| 52 | + # 2. Create an encryption context. |
| 53 | + # Most encrypted data should have an associated encryption context |
| 54 | + # to protect integrity. This sample uses placeholder values. |
| 55 | + # For more information see: |
| 56 | + # blogs.aws.amazon.com/security/post/Tx2LZ6WBJJANTNW/How-to-Protect-the-Integrity-of-Your-Encrypted-Data-by-Using-AWS-Key-Management # noqa: E501 |
| 57 | + encryption_context: Dict[str, str] = { |
| 58 | + "key1": "value1", |
| 59 | + "key2": "value2", |
| 60 | + "requiredKey1": "requiredValue1", |
| 61 | + "requiredKey2": "requiredValue2", |
| 62 | + } |
| 63 | + |
| 64 | + # 3. Create list of required encryption context keys. |
| 65 | + # This is a list of keys that must be present in the encryption context. |
| 66 | + required_encryption_context_keys: List[str] = ["requiredKey1", "requiredKey2"] |
| 67 | + |
| 68 | + # 4. Create the AWS KMS keyring. |
| 69 | + mpl: AwsCryptographicMaterialProviders = AwsCryptographicMaterialProviders( |
| 70 | + config=MaterialProvidersConfig() |
| 71 | + ) |
| 72 | + keyring_input: CreateAwsKmsKeyringInput = CreateAwsKmsKeyringInput( |
| 73 | + kms_key_id=kms_key_id, |
| 74 | + kms_client=boto3.client('kms', region_name="us-west-2") |
| 75 | + ) |
| 76 | + kms_keyring: IKeyring = mpl.create_aws_kms_keyring(keyring_input) |
| 77 | + |
| 78 | + # 5. Create the required encryption context CMM. |
| 79 | + underlying_cmm: ICryptographicMaterialsManager = \ |
| 80 | + mpl.create_default_cryptographic_materials_manager( |
| 81 | + CreateDefaultCryptographicMaterialsManagerInput( |
| 82 | + keyring=kms_keyring |
| 83 | + ) |
| 84 | + ) |
| 85 | + |
| 86 | + required_ec_cmm: ICryptographicMaterialsManager = \ |
| 87 | + mpl.create_required_encryption_context_cmm( |
| 88 | + CreateRequiredEncryptionContextCMMInput( |
| 89 | + required_encryption_context_keys=required_encryption_context_keys, |
| 90 | + underlying_cmm=underlying_cmm, |
| 91 | + ) |
| 92 | + ) |
| 93 | + |
| 94 | + # 6. Encrypt the data |
| 95 | + ciphertext, _ = client.encrypt( |
| 96 | + source=EXAMPLE_DATA, |
| 97 | + materials_manager=required_ec_cmm, |
| 98 | + encryption_context=encryption_context |
| 99 | + ) |
| 100 | + |
| 101 | + # 7. Reproduce the encryption context. |
| 102 | + # The reproduced encryption context MUST contain a value for |
| 103 | + # every key in the configured required encryption context keys during encryption with |
| 104 | + # Required Encryption Context CMM. |
| 105 | + reproduced_encryption_context: Dict[str, str] = { |
| 106 | + "requiredKey1": "requiredValue1", |
| 107 | + "requiredKey2": "requiredValue2", |
| 108 | + } |
| 109 | + |
| 110 | + # 8. Decrypt the data |
| 111 | + plaintext_bytes_A, _ = client.decrypt( |
| 112 | + source=ciphertext, |
| 113 | + materials_manager=required_ec_cmm, |
| 114 | + encryption_context=reproduced_encryption_context |
| 115 | + ) |
| 116 | + assert plaintext_bytes_A == EXAMPLE_DATA |
| 117 | + |
| 118 | + # We can also decrypt using the underlying CMM, |
| 119 | + # but must also provide the reproduced encryption context |
| 120 | + plaintext_bytes_A, _ = client.decrypt( |
| 121 | + source=ciphertext, |
| 122 | + materials_manager=underlying_cmm, |
| 123 | + encryption_context=reproduced_encryption_context |
| 124 | + ) |
| 125 | + assert plaintext_bytes_A == EXAMPLE_DATA |
| 126 | + |
| 127 | + # 9. Extra: Demonstrate that if we don't provide the reproduced encryption context, |
| 128 | + # decryption will fail. |
| 129 | + try: |
| 130 | + plaintext_bytes_A, _ = client.decrypt( |
| 131 | + source=ciphertext, |
| 132 | + materials_manager=required_ec_cmm, |
| 133 | + # No reproduced encryption context for required EC CMM-produced message makes decryption fail. |
| 134 | + ) |
| 135 | + raise Exception("If this exception is raised, decryption somehow succeeded!") |
| 136 | + except AWSEncryptionSDKClientError: |
| 137 | + # Swallow specific expected exception. |
| 138 | + # We expect decryption to fail with an AWSEncryptionSDKClientError |
| 139 | + # since we did not provide reproduced encryption context when decrypting |
| 140 | + # a message encrypted with the requried encryption context CMM. |
| 141 | + pass |
| 142 | + |
| 143 | + # Same for the default CMM; |
| 144 | + # If we don't provide the reproduced encryption context, decryption will fail. |
| 145 | + try: |
| 146 | + plaintext_bytes_A, _ = client.decrypt( |
| 147 | + source=ciphertext, |
| 148 | + materials_manager=required_ec_cmm, |
| 149 | + # No reproduced encryption context for required EC CMM-produced message makes decryption fail. |
| 150 | + ) |
| 151 | + raise Exception("If this exception is raised, decryption somehow succeeded!") |
| 152 | + except AWSEncryptionSDKClientError: |
| 153 | + # Swallow specific expected exception. |
| 154 | + # We expect decryption to fail with an AWSEncryptionSDKClientError |
| 155 | + # since we did not provide reproduced encryption context when decrypting |
| 156 | + # a message encrypted with the requried encryption context CMM, |
| 157 | + # even though we are using a default CMM on decrypt. |
| 158 | + pass |
0 commit comments