Skip to content

Commit 57d95e7

Browse files
committed
fix: raw RSA keyring must raise an error on encrypt if public key is not available
1 parent 4be5c22 commit 57d95e7

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

src/aws_encryption_sdk/keyrings/raw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ def on_encrypt(self, encryption_materials):
380380
new_materials = encryption_materials
381381

382382
if self._public_wrapping_key is None:
383-
return encryption_materials
383+
raise EncryptKeyError("A public key is required to encrypt")
384384

385385
if new_materials.data_encryption_key is None:
386386
new_materials = _generate_data_key(encryption_materials=new_materials, key_provider=self._key_provider)

test/functional/keyrings/raw/test_raw_rsa.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from cryptography.hazmat.primitives import serialization
1818
from cryptography.hazmat.primitives.asymmetric import rsa
1919

20+
from aws_encryption_sdk.exceptions import EncryptKeyError
2021
from aws_encryption_sdk.identifiers import (
2122
Algorithm,
2223
EncryptionKeyType,
@@ -307,7 +308,7 @@ def test_public_key_only_cannot_decrypt():
307308
assert test_materials is initial_decryption_materials
308309

309310

310-
def test_private_key_only_can_decrypt():
311+
def test_private_key_can_decrypt():
311312
complete_keyring = RawRSAKeyring(
312313
key_namespace=_PROVIDER_ID,
313314
key_name=_KEY_ID,
@@ -339,7 +340,7 @@ def test_private_key_only_can_decrypt():
339340
assert test_materials.data_encryption_key is not None
340341

341342

342-
def test_private_key_only_cannot_encrypt():
343+
def test_private_key_cannot_encrypt():
343344
test_keyring = RawRSAKeyring(
344345
key_namespace=_PROVIDER_ID,
345346
key_name=_KEY_ID,
@@ -350,11 +351,10 @@ def test_private_key_only_cannot_encrypt():
350351
algorithm=Algorithm.AES_256_GCM_IV12_TAG16_HKDF_SHA384_ECDSA_P384, encryption_context=_ENCRYPTION_CONTEXT
351352
)
352353

353-
test_materials = test_keyring.on_encrypt(initial_materials)
354+
with pytest.raises(EncryptKeyError) as excinfo:
355+
test_keyring.on_encrypt(initial_materials)
354356

355-
assert test_materials is initial_materials
356-
assert test_materials.data_encryption_key is None
357-
assert not test_materials.encrypted_data_keys
357+
excinfo.match("A public key is required to encrypt")
358358

359359

360360
def test_keypair_must_match():

test/unit/keyrings/raw/test_raw_rsa.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import aws_encryption_sdk.key_providers.raw
2121
import aws_encryption_sdk.keyrings.raw
22+
from aws_encryption_sdk.exceptions import EncryptKeyError
2223
from aws_encryption_sdk.identifiers import KeyringTraceFlag, WrappingAlgorithm
2324
from aws_encryption_sdk.internal.crypto.wrapping_keys import WrappingKey
2425
from aws_encryption_sdk.keyrings.base import Keyring
@@ -148,14 +149,20 @@ def test_on_encrypt_when_data_encryption_key_given(raw_rsa_keyring, patch_genera
148149

149150

150151
def test_on_encrypt_no_public_key(raw_rsa_keyring):
151-
raw_rsa_keyring._public_wrapping_key = None
152+
private_key = raw_rsa_private_key()
153+
test_keyring = RawRSAKeyring(
154+
key_namespace=_PROVIDER_ID,
155+
key_name=_KEY_ID,
156+
wrapping_algorithm=WrappingAlgorithm.RSA_OAEP_SHA256_MGF1,
157+
private_wrapping_key=private_key,
158+
)
152159

153-
intial_materials = get_encryption_materials_without_data_encryption_key()
160+
initial_materials = get_encryption_materials_without_data_encryption_key()
154161

155-
test_materials = raw_rsa_keyring.on_encrypt(encryption_materials=intial_materials)
162+
with pytest.raises(EncryptKeyError) as excinfo:
163+
test_keyring.on_encrypt(encryption_materials=initial_materials)
156164

157-
assert test_materials is intial_materials
158-
assert intial_materials.data_encryption_key is None
165+
excinfo.match("A public key is required to encrypt")
159166

160167

161168
def test_on_encrypt_keyring_trace_when_data_encryption_key_given(raw_rsa_keyring):

0 commit comments

Comments
 (0)