PYSEC-2024-225: cryptography NULL pointer deference with pkcs12.serialize_key_and_certificates when called with a non-matching certificate and private key and an hmac_hash override
cryptography is a package designed to expose cryptographic primitives and recipes to Python developers. Starting in version 38.0.0 and prior to version 42.0.4, if pkcs12.serializekeyandcertificates is called with both a certificate whose public key did not match the provided private key and an encryptionalgorithm with hmachash set (via PrivateFormat.PKCS12.encryptionbuilder().hmachash(...), then a NULL pointer dereference would occur, crashing the Python process. This has been resolved in version 42.0.4, the first version in which a ValueError is properly raised.
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
cryptographyto a version that resolves this vulnerability.Fixed in 42.0.4
Event History
Frequently Asked Questions
Which deployments are affected?
Applications using pypi/cryptography versions 38.0.0 through versions before 42.0.4 are affected, but only when they call pkcs12.serialize_key_and_certificates with a certificate and private key that do not match and configure an encryption algorithm with an hmac_hash override.
What does an attacker need to trigger the issue?
The vulnerable call must receive a non-matching certificate and private key, and the caller must use PrivateFormat.PKCS12.encryption_builder().hmac_hash(...) to set hmac_hash. Under those conditions, the call can crash the Python process through a NULL pointer dereference.
Is a default PKCS#12 serialization configuration affected?
The issue requires an hmac_hash override on the encryption algorithm. The provided information does not indicate that serialization without this override is affected.
What is the remediation and expected behavior after updating?
Update cryptography to version 42.0.4 or later. In 42.0.4, this invalid certificate/private-key combination properly raises ValueError rather than dereferencing a NULL pointer and crashing the process.
How can teams identify potentially affected code paths?
Review calls to pkcs12.serialize_key_and_certificates for code that accepts or constructs both a certificate and private key, and look specifically for encryption configuration using PrivateFormat.PKCS12.encryption_builder().hmac_hash(...). Such paths should ensure the certificate public key matches the supplied private key.