Skip to content

Commit 3256822

Browse files
author
Adriano Hernandez
committed
Fixed issue aws#135. Took longer than I expected because was trying to
get the value of the key in the encryption context dictionary to be the same (as well as name of the key), but then learned this wasn't what we wanted. Wrote a test that I confirm works only after the change is added.
1 parent c62ed68 commit 3256822

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/aws_encryption_sdk/materials_managers/default.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,21 @@ def _generate_signing_key_and_update_encryption_context(self, algorithm, encrypt
4949
:param dict encryption_context: Encryption context from request
5050
:returns: Signing key bytes
5151
:rtype: bytes or None
52+
:raises ValueError if a signer key that is already present in the encryption
53+
context is added
5254
"""
5355
_LOGGER.debug("Generating signing key")
5456
if algorithm.signing_algorithm_info is None:
5557
return None
5658

5759
signer = Signer(algorithm=algorithm, key=generate_ecc_signing_key(algorithm=algorithm))
58-
encryption_context[ENCODED_SIGNER_KEY] = to_str(signer.encoded_public_key())
60+
signer_key = signer.encoded_public_key()
61+
# raise error if key already present, even if different value
62+
# we don't care about the value, but we do care about the key (in the DICT)
63+
# remember this is dict(key -> value) = dict(key_name -> key)
64+
if ENCODED_SIGNER_KEY in encryption_context:
65+
raise ValueError("Tried to add key that was already present in Encryption Context")
66+
encryption_context[ENCODED_SIGNER_KEY] = to_str(signer_key)
5967
return signer.key_bytes()
6068

6169
def get_encryption_materials(self, request):
@@ -68,6 +76,8 @@ def get_encryption_materials(self, request):
6876
:raises MasterKeyProviderError: if no master keys are available from the underlying master key provider
6977
:raises MasterKeyProviderError: if the primary master key provided by the underlying master key provider
7078
is not included in the full set of master keys provided by that provider
79+
:raises ValueError if in calling _generate_signing_key_and_update_encryption_context()
80+
a key is attempted to be added to the encryption context, when it already has that key.
7181
"""
7282
algorithm = request.algorithm if request.algorithm is not None else self.algorithm
7383
encryption_context = request.encryption_context.copy()

test/unit/test_deserialize.py

+8
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@
2727

2828
pytestmark = [pytest.mark.unit, pytest.mark.local]
2929

30+
def test_deserialize_malformed_encryption_context():
31+
"""
32+
If the client deserialization receives a malformed ciphertext
33+
that defines the AAD length as 0 and then also defines a AAD fields
34+
as 0, the deserialization logic SHOULD raise an error.
35+
"""
36+
bad_ciphertext = b'AYAAFJwN8IgQ9+0sxyy7+90cCCgAAgAAAAEAE1dFQi1DUllQVE8tUlNBLU9BRVAAKDhDRUQyRkQyMEZDODhBOUMwNkVGREIwNzM3MDdFQjFFRjE2NTU3ODABAFbIi+gmSrvejfOCjbE08rTYHym2uLWsiizQHnTy3z8/VeR+7MKvNv7ZfPf5LX7i9amYwxCMISvY+BCcndLakH/RlDUdgz5/Q0KAxrE5LX7DHxO/wMviJCi+qXWMb+5u0mhwepRihO/dk+3kGqyaLhnGuA6xqYmThUlCZR5BwfyEddSango7umEWw1YQ8vokjqUzCKRyk3VpXwQTXQLLrBz7ZmZ7Anzn0SoaLYk8D0rPWhKHvUXQDJYDYdQ7vpedxpsE5vliLI98CAcIWllkst964DIBwKgAX6Ic8Nj+8T7VurdK2SFuTH4LIvkebmEGCxngdRpfopEU/Rd0LYXZik4CAAAAAAwAAAAGAAAAAAAAAAAAAAAAK9vNRvymDkoxO6dy67pDuf////8AAAABAAAAAAAAAAAAAAABAAAABTAqmilQragTFTYdPz23w1NMR+c8Uw=='
37+
pass
3038

3139
def test_deserialize_non_framed_values():
3240
iv = b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x11"

test/unit/test_material_managers_default.py

+24-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# language governing permissions and limitations under the License.
1313
"""Test suite for aws_encryption_sdk.materials_managers.default"""
1414
import pytest
15+
import os
1516
from mock import MagicMock, sentinel
1617
from pytest_mock import mocker # noqa pylint: disable=unused-import
1718

@@ -20,10 +21,14 @@
2021
from aws_encryption_sdk.identifiers import Algorithm
2122
from aws_encryption_sdk.internal.defaults import ALGORITHM, ENCODED_SIGNER_KEY
2223
from aws_encryption_sdk.key_providers.base import MasterKeyProvider
23-
from aws_encryption_sdk.materials_managers import EncryptionMaterials
24+
from aws_encryption_sdk.materials_managers import EncryptionMaterials, EncryptionMaterialsRequest
2425
from aws_encryption_sdk.materials_managers.default import DefaultCryptoMaterialsManager
2526
from aws_encryption_sdk.structures import DataKey
2627

28+
from aws_encryption_sdk.internal.crypto.authentication import Signer
29+
from aws_encryption_sdk.internal.crypto.elliptic_curve import generate_ecc_signing_key
30+
from aws_encryption_sdk.internal.str_ops import to_str
31+
2732
pytestmark = [pytest.mark.unit, pytest.mark.local]
2833

2934

@@ -234,3 +239,21 @@ def test_decrypt_materials(mocker, patch_for_dcmm_decrypt):
234239
)
235240
assert test.data_key is cmm.master_key_provider.decrypt_data_key_from_list.return_value
236241
assert test.verification_key == patch_for_dcmm_decrypt
242+
243+
# tests that we correctly through an error when we try to add a key
244+
# to the encryption context but it is already present
245+
def test_signer_key_in_encryption_context():
246+
cmm = build_cmm()
247+
algo = ALGORITHM # default
248+
249+
# key value does not matter
250+
key = to_str(Signer(algorithm=algo, key=generate_ecc_signing_key(algorithm=algo)).encoded_public_key())
251+
context = {ENCODED_SIGNER_KEY: key}
252+
request = EncryptionMaterialsRequest(
253+
algorithm=algo,
254+
encryption_context=context,
255+
frame_length=4096
256+
)
257+
with pytest.raises(ValueError) as excinfo:
258+
cmm.get_encryption_materials(request)
259+
excinfo.match(r"Tried to add key that was already present in Encryption Context")

0 commit comments

Comments
 (0)