|
14 | 14 | import hypothesis
|
15 | 15 | import pytest
|
16 | 16 |
|
| 17 | +from dynamodb_encryption_sdk.delegated_keys.jce import JceNameLocalDelegatedKey |
17 | 18 | from dynamodb_encryption_sdk.encrypted import CryptoConfig
|
18 | 19 | from dynamodb_encryption_sdk.encrypted.item import decrypt_python_item, encrypt_python_item
|
19 | 20 | from dynamodb_encryption_sdk.exceptions import DecryptionError, EncryptionError
|
20 |
| -from dynamodb_encryption_sdk.internal.identifiers import ReservedAttributes |
| 21 | +from dynamodb_encryption_sdk.identifiers import CryptoAction |
| 22 | +from dynamodb_encryption_sdk.internal.identifiers import MaterialDescriptionKeys, ReservedAttributes |
| 23 | +from dynamodb_encryption_sdk.material_providers.static import StaticCryptographicMaterialsProvider |
| 24 | +from dynamodb_encryption_sdk.materials.raw import RawDecryptionMaterials, RawEncryptionMaterials |
21 | 25 | from dynamodb_encryption_sdk.structures import AttributeActions, EncryptionContext
|
22 | 26 | from ..functional_test_utils import (
|
23 | 27 | build_static_jce_cmp, cycle_item_check, set_parametrized_actions, set_parametrized_cmp, set_parametrized_item
|
@@ -62,6 +66,83 @@ def test_reserved_attributes_on_encrypt(static_cmp_crypto_config, item):
|
62 | 66 | exc_info.match(r'Reserved attribute name *')
|
63 | 67 |
|
64 | 68 |
|
| 69 | +def test_only_sign_item(parametrized_item): |
| 70 | + signing_key = JceNameLocalDelegatedKey.generate('HmacSHA256', 256) |
| 71 | + cmp = StaticCryptographicMaterialsProvider( |
| 72 | + encryption_materials=RawEncryptionMaterials(signing_key=signing_key), |
| 73 | + decryption_materials=RawDecryptionMaterials(verification_key=signing_key) |
| 74 | + ) |
| 75 | + actions = AttributeActions(default_action=CryptoAction.SIGN_ONLY) |
| 76 | + crypto_config = CryptoConfig( |
| 77 | + materials_provider=cmp, |
| 78 | + encryption_context=EncryptionContext(), |
| 79 | + attribute_actions=actions |
| 80 | + ) |
| 81 | + |
| 82 | + signed_item = encrypt_python_item(parametrized_item, crypto_config) |
| 83 | + material_description = signed_item[ReservedAttributes.MATERIAL_DESCRIPTION.value].value |
| 84 | + assert MaterialDescriptionKeys.ATTRIBUTE_ENCRYPTION_MODE.value.encode('utf-8') not in material_description |
| 85 | + |
| 86 | + decrypt_python_item(signed_item, crypto_config) |
| 87 | + |
| 88 | + |
| 89 | +@pytest.mark.parametrize('actions', ( |
| 90 | + AttributeActions(default_action=CryptoAction.ENCRYPT_AND_SIGN), |
| 91 | + AttributeActions(default_action=CryptoAction.SIGN_ONLY, attribute_actions={'test': CryptoAction.ENCRYPT_AND_SIGN}), |
| 92 | +)) |
| 93 | +def test_no_encryption_key_but_encryption_requested(actions, parametrized_item): |
| 94 | + signing_key = JceNameLocalDelegatedKey.generate('HmacSHA256', 256) |
| 95 | + cmp = StaticCryptographicMaterialsProvider( |
| 96 | + encryption_materials=RawEncryptionMaterials(signing_key=signing_key) |
| 97 | + ) |
| 98 | + crypto_config = CryptoConfig( |
| 99 | + materials_provider=cmp, |
| 100 | + encryption_context=EncryptionContext(), |
| 101 | + attribute_actions=actions |
| 102 | + ) |
| 103 | + |
| 104 | + with pytest.raises(EncryptionError) as excinfo: |
| 105 | + encrypt_python_item(parametrized_item, crypto_config) |
| 106 | + |
| 107 | + excinfo.match('Attribute actions ask for some attributes to be encrypted but no encryption key is available') |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize('actions', ( |
| 111 | + AttributeActions(default_action=CryptoAction.ENCRYPT_AND_SIGN), |
| 112 | + AttributeActions(default_action=CryptoAction.SIGN_ONLY, attribute_actions={'test': CryptoAction.ENCRYPT_AND_SIGN}), |
| 113 | +)) |
| 114 | +def test_no_decryption_key_but_decryption_requested(actions, parametrized_item): |
| 115 | + encryption_key = JceNameLocalDelegatedKey.generate('AES', 256) |
| 116 | + signing_key = JceNameLocalDelegatedKey.generate('HmacSHA256', 256) |
| 117 | + encrypting_cmp = StaticCryptographicMaterialsProvider( |
| 118 | + encryption_materials=RawEncryptionMaterials(encryption_key=encryption_key, signing_key=signing_key) |
| 119 | + ) |
| 120 | + decrypting_cmp = StaticCryptographicMaterialsProvider( |
| 121 | + decryption_materials=RawDecryptionMaterials(verification_key=signing_key) |
| 122 | + ) |
| 123 | + |
| 124 | + encrypted_item = encrypt_python_item( |
| 125 | + parametrized_item, |
| 126 | + CryptoConfig( |
| 127 | + materials_provider=encrypting_cmp, |
| 128 | + encryption_context=EncryptionContext(), |
| 129 | + attribute_actions=actions |
| 130 | + ) |
| 131 | + ) |
| 132 | + |
| 133 | + with pytest.raises(DecryptionError) as excinfo: |
| 134 | + decrypt_python_item( |
| 135 | + encrypted_item, |
| 136 | + CryptoConfig( |
| 137 | + materials_provider=decrypting_cmp, |
| 138 | + encryption_context=EncryptionContext(), |
| 139 | + attribute_actions=actions |
| 140 | + ) |
| 141 | + ) |
| 142 | + |
| 143 | + excinfo.match('Attribute actions ask for some attributes to be decrypted but no decryption key is available') |
| 144 | + |
| 145 | + |
65 | 146 | def _item_cycle_check(materials_provider, attribute_actions, item):
|
66 | 147 | crypto_config = CryptoConfig(
|
67 | 148 | materials_provider=materials_provider,
|
|
0 commit comments