|
| 1 | +# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +""" |
| 4 | +The AWS Encryption SDK supports several different algorithm suites |
| 5 | +that offer different security properties. |
| 6 | +
|
| 7 | +https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/supported-algorithms.html |
| 8 | +
|
| 9 | +By default, the AWS Encryption SDK will let you use any of these, |
| 10 | +but you might want to restrict that further. |
| 11 | +
|
| 12 | +We recommend that you use the default algorithm suite, |
| 13 | +which uses AES-GCM with 256-bit keys, HKDF, and ECDSA message signing. |
| 14 | +If your readers and writers have the same permissions, |
| 15 | +you might want to omit the message signature for faster operation. |
| 16 | +For more information about choosing a signed or unsigned algorithm suite, |
| 17 | +see the AWS Encryption SDK developer guide: |
| 18 | +
|
| 19 | +https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/supported-algorithms.html#other-algorithms |
| 20 | +
|
| 21 | +This example shows how you can make a custom cryptographic materials manager (CMM) |
| 22 | +that only allows encrypt requests that either specify one of these two algorithm suites |
| 23 | +or do not specify an algorithm suite, in which case the default CMM uses the default algorithm suite. |
| 24 | +""" |
| 25 | +import aws_encryption_sdk |
| 26 | +from aws_encryption_sdk.identifiers import AlgorithmSuite |
| 27 | +from aws_encryption_sdk.keyrings.aws_kms import KmsKeyring |
| 28 | +from aws_encryption_sdk.keyrings.base import Keyring |
| 29 | +from aws_encryption_sdk.materials_managers import ( |
| 30 | + DecryptionMaterials, |
| 31 | + DecryptionMaterialsRequest, |
| 32 | + EncryptionMaterials, |
| 33 | + EncryptionMaterialsRequest, |
| 34 | +) |
| 35 | +from aws_encryption_sdk.materials_managers.base import CryptoMaterialsManager |
| 36 | +from aws_encryption_sdk.materials_managers.default import DefaultCryptoMaterialsManager |
| 37 | + |
| 38 | + |
| 39 | +class UnapprovedAlgorithmSuite(Exception): |
| 40 | + """Indicate that an unsupported algorithm suite was requested.""" |
| 41 | + |
| 42 | + |
| 43 | +class RequireApprovedAlgorithmSuitesCryptoMaterialsManager(CryptoMaterialsManager): |
| 44 | + """Only allow encryption requests for approved algorithm suites.""" |
| 45 | + |
| 46 | + def __init__(self, keyring): |
| 47 | + # type: (Keyring) -> None |
| 48 | + """Set up the inner cryptographic materials manager using the provided keyring. |
| 49 | +
|
| 50 | + :param Keyring keyring: Keyring to use in the inner cryptographic materials manager |
| 51 | + """ |
| 52 | + self._allowed_algorithm_suites = { |
| 53 | + None, # no algorithm suite in the request |
| 54 | + AlgorithmSuite.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384, # the default algorithm suite |
| 55 | + AlgorithmSuite.AES_256_GCM_IV12_TAG16_HKDF_SHA256, # the recommended unsigned algorithm suite |
| 56 | + } |
| 57 | + # Wrap the provided keyring in the default cryptographic materials manager (CMM). |
| 58 | + # |
| 59 | + # This is the same thing that the encrypt and decrypt APIs, as well as the caching CMM, |
| 60 | + # do if you provide a keyring instead of a CMM. |
| 61 | + self._cmm = DefaultCryptoMaterialsManager(keyring=keyring) |
| 62 | + |
| 63 | + def get_encryption_materials(self, request): |
| 64 | + # type: (EncryptionMaterialsRequest) -> EncryptionMaterials |
| 65 | + """Block any requests that include an unapproved algorithm suite.""" |
| 66 | + if request.algorithm not in self._allowed_algorithm_suites: |
| 67 | + raise UnapprovedAlgorithmSuite("Unapproved algorithm suite requested!") |
| 68 | + |
| 69 | + return self._cmm.get_encryption_materials(request) |
| 70 | + |
| 71 | + def decrypt_materials(self, request): |
| 72 | + # type: (DecryptionMaterialsRequest) -> DecryptionMaterials |
| 73 | + """Be more permissive on decrypt and just pass through.""" |
| 74 | + return self._cmm.decrypt_materials(request) |
| 75 | + |
| 76 | + |
| 77 | +def run(aws_kms_cmk, source_plaintext): |
| 78 | + # type: (str, bytes) -> None |
| 79 | + """Demonstrate an encrypt/decrypt cycle using a custom cryptographic materials manager that filters requests. |
| 80 | +
|
| 81 | + :param str aws_kms_cmk: The ARN of an AWS KMS CMK that protects data keys |
| 82 | + :param bytes source_plaintext: Plaintext to encrypt |
| 83 | + """ |
| 84 | + # Prepare your encryption context. |
| 85 | + # https://docs.aws.amazon.com/encryption-sdk/latest/developer-guide/concepts.html#encryption-context |
| 86 | + encryption_context = { |
| 87 | + "encryption": "context", |
| 88 | + "is not": "secret", |
| 89 | + "but adds": "useful metadata", |
| 90 | + "that can help you": "be confident that", |
| 91 | + "the data you are handling": "is what you think it is", |
| 92 | + } |
| 93 | + |
| 94 | + # Create the keyring that determines how your data keys are protected. |
| 95 | + keyring = KmsKeyring(generator_key_id=aws_kms_cmk) |
| 96 | + |
| 97 | + # Create the algorithm suite restricting cryptographic materials manager using your keyring. |
| 98 | + cmm = RequireApprovedAlgorithmSuitesCryptoMaterialsManager(keyring=keyring) |
| 99 | + |
| 100 | + # Demonstrate that the algorithm suite restricting CMM will not let you use an unapproved algorithm suite. |
| 101 | + try: |
| 102 | + aws_encryption_sdk.encrypt( |
| 103 | + source=source_plaintext, |
| 104 | + encryption_context=encryption_context, |
| 105 | + materials_manager=cmm, |
| 106 | + algorithm=AlgorithmSuite.AES_256_GCM_IV12_TAG16, |
| 107 | + ) |
| 108 | + except UnapprovedAlgorithmSuite: |
| 109 | + # You asked for an unapproved algorithm suite. |
| 110 | + # Reaching this point means everything is working as expected. |
| 111 | + pass |
| 112 | + else: |
| 113 | + # The algorithm suite restricting CMM keeps this from happening. |
| 114 | + raise AssertionError("The algorithm suite restricting CMM does not let this happen!") |
| 115 | + |
| 116 | + # Encrypt your plaintext data. |
| 117 | + ciphertext, _encrypt_header = aws_encryption_sdk.encrypt( |
| 118 | + source=source_plaintext, encryption_context=encryption_context, materials_manager=cmm |
| 119 | + ) |
| 120 | + |
| 121 | + # Demonstrate that the ciphertext and plaintext are different. |
| 122 | + assert ciphertext != source_plaintext |
| 123 | + |
| 124 | + # Decrypt your encrypted data using the same cryptographic materials manager you used on encrypt. |
| 125 | + # |
| 126 | + # You do not need to specify the encryption context on decrypt |
| 127 | + # because the header of the encrypted message includes the encryption context. |
| 128 | + decrypted, decrypt_header = aws_encryption_sdk.decrypt(source=ciphertext, materials_manager=cmm) |
| 129 | + |
| 130 | + # Demonstrate that the decrypted plaintext is identical to the original plaintext. |
| 131 | + assert decrypted == source_plaintext |
| 132 | + |
| 133 | + # Verify that the encryption context used in the decrypt operation includes |
| 134 | + # the encryption context that you specified when encrypting. |
| 135 | + # The AWS Encryption SDK can add pairs, so don't require an exact match. |
| 136 | + # |
| 137 | + # In production, always use a meaningful encryption context. |
| 138 | + assert set(encryption_context.items()) <= set(decrypt_header.encryption_context.items()) |
0 commit comments