Skip to content

Commit d4e7741

Browse files
committed
more tests for 100% python coverage
1 parent e1c4620 commit d4e7741

File tree

2 files changed

+95
-6
lines changed

2 files changed

+95
-6
lines changed

src/cryptography/hazmat/primitives/serialization/pkcs7.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ def set_recipient(
306306
raise TypeError("certificate must be a x509.Certificate")
307307

308308
if not isinstance(certificate.public_key(), rsa.RSAPublicKey):
309-
raise TypeError("Only RSA keys are supported at this time.")
309+
raise TypeError("Only RSA public keys are supported at this time.")
310310

311311
return PKCS7EnvelopeDecryptor(
312312
_data=self._data,
@@ -320,6 +320,11 @@ def set_private_key(
320320
if self._private_key is not None:
321321
raise ValueError("private key may only be set once")
322322

323+
if not isinstance(private_key, rsa.RSAPrivateKey):
324+
raise TypeError(
325+
"Only RSA private keys are supported at this time."
326+
)
327+
323328
return PKCS7EnvelopeDecryptor(
324329
_data=self._data,
325330
_recipient=self._recipient,

tests/hazmat/primitives/test_pkcs7.py

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import email.parser
77
import os
88
import typing
9+
from email.message import EmailMessage
910

1011
import pytest
1112

@@ -1087,6 +1088,12 @@ def test_set_data_twice(self, backend):
10871088
with pytest.raises(ValueError):
10881089
decryptor.set_data(b"test")
10891090

1091+
def test_set_recipient_twice(self, backend):
1092+
cert, _ = _load_rsa_cert_key()
1093+
decryptor = pkcs7.PKCS7EnvelopeDecryptor().set_recipient(cert)
1094+
with pytest.raises(ValueError):
1095+
decryptor.set_recipient(cert)
1096+
10901097
def test_unsupported_encryption(self, backend):
10911098
cert_non_rsa, _ = _load_cert_key()
10921099
with pytest.raises(TypeError):
@@ -1098,30 +1105,80 @@ def test_not_a_cert(self, backend):
10981105
b"notacert", # type: ignore[arg-type]
10991106
)
11001107

1101-
def test_decrypt_no_recipient(self, backend):
1102-
decryptor = pkcs7.PKCS7EnvelopeDecryptor().set_data(b"test")
1108+
def test_set_private_key_twice(self, backend):
1109+
_, private_key = _load_rsa_cert_key()
1110+
decryptor = pkcs7.PKCS7EnvelopeDecryptor().set_private_key(private_key)
11031111
with pytest.raises(ValueError):
1104-
decryptor.decrypt(serialization.Encoding.SMIME, [])
1112+
decryptor.set_private_key(private_key)
1113+
1114+
def test_not_a_pkey(self, backend):
1115+
with pytest.raises(TypeError):
1116+
pkcs7.PKCS7EnvelopeDecryptor().set_private_key(
1117+
b"notapkey", # type: ignore[arg-type]
1118+
)
11051119

11061120
def test_decrypt_no_data(self, backend):
11071121
cert, _ = _load_rsa_cert_key()
11081122
decryptor = pkcs7.PKCS7EnvelopeDecryptor().set_recipient(cert)
11091123
with pytest.raises(ValueError):
11101124
decryptor.decrypt(serialization.Encoding.SMIME, [])
11111125

1112-
def test_decrypt_invalid_options(self, backend):
1126+
def test_decrypt_no_recipient(self, backend):
1127+
decryptor = pkcs7.PKCS7EnvelopeDecryptor().set_data(b"test")
1128+
with pytest.raises(ValueError):
1129+
decryptor.decrypt(serialization.Encoding.SMIME, [])
1130+
1131+
def test_decrypt_no_private_key(self, backend):
11131132
cert, _ = _load_rsa_cert_key()
11141133
decryptor = (
11151134
pkcs7.PKCS7EnvelopeDecryptor()
11161135
.set_data(b"test")
11171136
.set_recipient(cert)
11181137
)
1138+
with pytest.raises(ValueError):
1139+
decryptor.decrypt(serialization.Encoding.SMIME, [])
1140+
1141+
@pytest.fixture
1142+
def decryptor(self, backend) -> pkcs7.PKCS7EnvelopeDecryptor:
1143+
cert, private_key = _load_rsa_cert_key()
1144+
return (
1145+
pkcs7.PKCS7EnvelopeDecryptor()
1146+
.set_data(b"test")
1147+
.set_recipient(cert)
1148+
.set_private_key(private_key)
1149+
)
1150+
1151+
def test_decrypt_invalid_options(
1152+
self, backend, decryptor: pkcs7.PKCS7EnvelopeDecryptor
1153+
):
11191154
with pytest.raises(ValueError):
11201155
decryptor.decrypt(
11211156
serialization.Encoding.SMIME,
11221157
[b"invalid"], # type: ignore[list-item]
11231158
)
11241159

1160+
def test_decrypt_invalid_encoding(
1161+
self, backend, decryptor: pkcs7.PKCS7EnvelopeDecryptor
1162+
):
1163+
with pytest.raises(ValueError):
1164+
decryptor.decrypt(serialization.Encoding.Raw, [])
1165+
1166+
@pytest.mark.parametrize(
1167+
"invalid_options",
1168+
[
1169+
[pkcs7.PKCS7Options.NoAttributes],
1170+
[pkcs7.PKCS7Options.NoCapabilities],
1171+
[pkcs7.PKCS7Options.NoCerts],
1172+
[pkcs7.PKCS7Options.DetachedSignature],
1173+
[pkcs7.PKCS7Options.Binary, pkcs7.PKCS7Options.Text],
1174+
],
1175+
)
1176+
def test_encrypt_invalid_encryption_options(
1177+
self, backend, invalid_options, decryptor: pkcs7.PKCS7EnvelopeDecryptor
1178+
):
1179+
with pytest.raises(ValueError):
1180+
decryptor.decrypt(serialization.Encoding.DER, invalid_options)
1181+
11251182
@pytest.mark.parametrize(
11261183
("encoding", "options"),
11271184
[
@@ -1134,7 +1191,6 @@ def test_decrypt_invalid_options(self, backend):
11341191
],
11351192
)
11361193
def test_smime_decrypt(self, backend, encoding, options):
1137-
"""Testing the round-trip of encrypting and decrypting data."""
11381194
# Encrypt some data
11391195
plain = b"hello world\n"
11401196
cert, private_key = _load_rsa_cert_key()
@@ -1154,6 +1210,23 @@ def test_smime_decrypt(self, backend, encoding, options):
11541210
decrypted = decryptor.decrypt(encoding, options)
11551211
assert decrypted == plain
11561212

1213+
def test_smime_decrypt_not_encrypted(self, backend):
1214+
# Create a plain email
1215+
email_message = EmailMessage()
1216+
email_message.set_content("hello world\n")
1217+
1218+
# Test decryption failure with plain email
1219+
cert, private_key = _load_rsa_cert_key()
1220+
decryptor = (
1221+
pkcs7.PKCS7EnvelopeDecryptor()
1222+
.set_data(email_message.as_bytes())
1223+
.set_recipient(cert)
1224+
.set_private_key(private_key)
1225+
)
1226+
1227+
with pytest.raises(ValueError):
1228+
decryptor.decrypt(serialization.Encoding.SMIME, [])
1229+
11571230

11581231
@pytest.mark.supported(
11591232
only_if=lambda backend: backend.pkcs7_supported(),
@@ -1253,3 +1326,14 @@ class TestPKCS7EnvelopeBuilderUnsupported:
12531326
def test_envelope_builder_unsupported(self, backend):
12541327
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_PADDING):
12551328
pkcs7.PKCS7EnvelopeBuilder()
1329+
1330+
1331+
@pytest.mark.supported(
1332+
only_if=lambda backend: backend.pkcs7_supported()
1333+
and not backend.rsa_encryption_supported(padding.PKCS1v15()),
1334+
skip_message="Requires OpenSSL with no PKCS1 v1.5 padding support",
1335+
)
1336+
class TestPKCS7EnvelopeDecryptorUnsupported:
1337+
def test_envelope_builder_unsupported(self, backend):
1338+
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_PADDING):
1339+
pkcs7.PKCS7EnvelopeDecryptor()

0 commit comments

Comments
 (0)