Skip to content

Commit 7a4d012

Browse files
authored
Fixes #10422 -- don't crash when a PKCS#12 key and cert don't match (#10423) (#10425)
1 parent df314bb commit 7a4d012

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/cryptography/hazmat/backends/openssl/backend.py

+9
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,15 @@ def serialize_key_and_certificates_to_pkcs12(
826826
mac_iter,
827827
0,
828828
)
829+
if p12 == self._ffi.NULL:
830+
errors = self._consume_errors()
831+
raise ValueError(
832+
(
833+
"Failed to create PKCS12 (does the key match the "
834+
"certificate?)"
835+
),
836+
errors,
837+
)
829838

830839
if (
831840
self._lib.Cryptography_HAS_PKCS12_SET_MAC

tests/hazmat/primitives/test_pkcs12.py

+18
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,24 @@ def test_key_serialization_encryption_set_mac_unsupported(
657657
b"name", cakey, cacert, [], algorithm
658658
)
659659

660+
@pytest.mark.supported(
661+
only_if=lambda backend: backend._lib.Cryptography_HAS_PKCS12_SET_MAC,
662+
skip_message="Requires OpenSSL with PKCS12_set_mac",
663+
)
664+
def test_set_mac_key_certificate_mismatch(self, backend):
665+
cacert, _ = _load_ca(backend)
666+
key = ec.generate_private_key(ec.SECP256R1())
667+
encryption = (
668+
serialization.PrivateFormat.PKCS12.encryption_builder()
669+
.hmac_hash(hashes.SHA256())
670+
.build(b"password")
671+
)
672+
673+
with pytest.raises(ValueError):
674+
serialize_key_and_certificates(
675+
b"name", key, cacert, [], encryption
676+
)
677+
660678

661679
@pytest.mark.skip_fips(
662680
reason="PKCS12 unsupported in FIPS mode. So much bad crypto in it."

0 commit comments

Comments
 (0)