Skip to main content

load_pkcs12

Function load_pkcs12 

Source
pub fn load_pkcs12<'py>(
    py: Python<'py>,
    data: &[u8],
    password: Option<&[u8]>,
) -> PyResult<Bound<'py, PyTuple>>
Expand description

Extract both certificates and private keys from a PKCS#12 archive.

Returns a 2-tuple (certs, keys) where:

  • certs is a :class:list of :class:Certificate objects.
  • keys is a :class:list of :class:bytes, each a DER-encoded PKCS#8 OneAsymmetricKey structure.

Encrypted bags are decrypted with password when the openssl Cargo feature is enabled; without it, encrypted bags are silently skipped and a :exc:ValueError is raised only on structural errors. Pass b"" or omit password for password-less archives.

Use :func:load_pkcs12_certificates if you only need certificates, or :func:load_pkcs12_keys if you only need keys.

data = open("archive.p12", "rb").read()
certs, keys = synta.load_pkcs12(data, b"s3cr3t")
for cert in certs:
    print(cert.subject)
for key_der in keys:
    from cryptography.hazmat.primitives.serialization import load_der_private_key
    key = load_der_private_key(key_der, None)