Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/aws_encryption_sdk/materials_managers/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import attr

from ..exceptions import MasterKeyProviderError, SerializationError
from ..exceptions import ActionNotAllowedError, MasterKeyProviderError, SerializationError
from ..identifiers import CommitmentPolicy
from ..internal.crypto.authentication import Signer, Verifier
from ..internal.crypto.elliptic_curve import generate_ecc_signing_key
Expand Down Expand Up @@ -40,11 +40,19 @@ def _generate_signing_key_and_update_encryption_context(self, algorithm, encrypt
:param dict encryption_context: Encryption context from request
:returns: Signing key bytes
:rtype: bytes or None
:raises ActionNotAllowedError: if the algorithm suite is signed and the encryption context
already contains the reserved signer key
"""
_LOGGER.debug("Generating signing key")
if algorithm.signing_algorithm_info is None:
return None

if ENCODED_SIGNER_KEY in encryption_context:
raise ActionNotAllowedError(
"Encryption context contains reserved key {!r}, which is used for the message signature "
"verification key".format(ENCODED_SIGNER_KEY)
)

signer = Signer(algorithm=algorithm, key=generate_ecc_signing_key(algorithm=algorithm))
encryption_context[ENCODED_SIGNER_KEY] = to_str(signer.encoded_public_key())
return signer.key_bytes()
Expand All @@ -56,6 +64,8 @@ def get_encryption_materials(self, request):
:type request: aws_encryption_sdk.materials_managers.EncryptionMaterialsRequest
:returns: encryption materials
:rtype: aws_encryption_sdk.materials_managers.EncryptionMaterials
:raises ActionNotAllowedError: if the algorithm suite is signed and the request's encryption context
already contains the reserved signer key
:raises MasterKeyProviderError: if no master keys are available from the underlying master key provider
:raises MasterKeyProviderError: if the primary master key provided by the underlying master key provider
is not included in the full set of master keys provided by that provider
Expand Down
28 changes: 28 additions & 0 deletions test/unit/test_material_managers_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ def test_generate_signing_key_and_update_encryption_context_no_signer():
assert test is None


def test_generate_signing_key_and_update_encryption_context_reserved_key_signed_suite(mocker):
mock_signer_cls = mocker.patch.object(aws_encryption_sdk.materials_managers.default, "Signer")
mocker.patch.object(aws_encryption_sdk.materials_managers.default, "generate_ecc_signing_key")
cmm = build_cmm()
encryption_context = {ENCODED_SIGNER_KEY: "supplied by the caller"}

with pytest.raises(ActionNotAllowedError) as excinfo:
cmm._generate_signing_key_and_update_encryption_context(
algorithm=MagicMock(signing_algorithm_info=sentinel.eccurve), encryption_context=encryption_context
)

excinfo.match(ENCODED_SIGNER_KEY)
mock_signer_cls.assert_not_called()
assert encryption_context == {ENCODED_SIGNER_KEY: "supplied by the caller"}


def test_generate_signing_key_and_update_encryption_context_reserved_key_unsigned_suite():
cmm = build_cmm()
encryption_context = {ENCODED_SIGNER_KEY: "left alone"}

test = cmm._generate_signing_key_and_update_encryption_context(
algorithm=MagicMock(signing_algorithm_info=None), encryption_context=encryption_context
)

assert test is None
assert encryption_context == {ENCODED_SIGNER_KEY: "left alone"}


def test_generate_signing_key_and_update_encryption_context(mocker):
mocker.patch.object(aws_encryption_sdk.materials_managers.default, "Signer")
mock_signer = MagicMock()
Expand Down