Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #10422 -- don't crash when a PKCS#12 key and cert don't match #10423

Merged
merged 1 commit into from Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/cryptography/hazmat/backends/openssl/backend.py
Expand Up @@ -623,6 +623,15 @@ def serialize_key_and_certificates_to_pkcs12(
mac_iter,
0,
)
if p12 == self._ffi.NULL:
errors = self._consume_errors()
raise ValueError(
(
"Failed to create PKCS12 (does the key match the "
"certificate?)"
),
errors,
)

if (
self._lib.Cryptography_HAS_PKCS12_SET_MAC
Expand Down
18 changes: 18 additions & 0 deletions tests/hazmat/primitives/test_pkcs12.py
Expand Up @@ -660,6 +660,24 @@ def test_key_serialization_encryption_set_mac_unsupported(
b"name", cakey, cacert, [], algorithm
)

@pytest.mark.supported(
only_if=lambda backend: backend._lib.Cryptography_HAS_PKCS12_SET_MAC,
skip_message="Requires OpenSSL with PKCS12_set_mac",
)
def test_set_mac_key_certificate_mismatch(self, backend):
cacert, _ = _load_ca(backend)
key = ec.generate_private_key(ec.SECP256R1())
encryption = (
serialization.PrivateFormat.PKCS12.encryption_builder()
.hmac_hash(hashes.SHA256())
.build(b"password")
)

with pytest.raises(ValueError):
serialize_key_and_certificates(
b"name", key, cacert, [], encryption
)


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