import pathlib
import synta
VECTORS = pathlib.Path(__file__).parent.parent / "tests" / "vectors"
CRYPTO_PKCS12 = (
VECTORS
/ "cryptography"
/ "vectors"
/ "cryptography_vectors"
/ "pkcs12"
)
def section(title):
print(f"\n{'─' * 60}\n{title}\n{'─' * 60}")
def demo_no_password():
section("load_pkcs12_certificates — no password")
path = VECTORS / "test_bundle_nopass.p12"
if not path.exists():
print(f" Skipped: {path} not found")
return
data = path.read_bytes()
certs = synta.load_pkcs12_certificates(data, None)
print(f" Loaded {len(certs)} certificate(s) from no-password archive")
for i, cert in enumerate(certs):
print(f" [{i}] subject={cert.subject} sig_alg={cert.signature_algorithm}")
def demo_two_certs():
section("load_pkcs12_certificates — two certificates, no password")
path = VECTORS / "test_bundle_2certs.p12"
if not path.exists():
print(f" Skipped: {path} not found")
return
data = path.read_bytes()
certs = synta.load_pkcs12_certificates(data, None)
print(f" Loaded {len(certs)} certificate(s)")
for i, cert in enumerate(certs):
print(f" [{i}] subject={cert.subject}")
def demo_with_password():
section("load_pkcs12_certificates — AES-256-CBC encrypted, with password")
path = VECTORS / "test_bundle_pass.p12"
if not path.exists():
print(f" Skipped: {path} not found")
return
data = path.read_bytes()
try:
certs = synta.load_pkcs12_certificates(data, b"synta")
print(f" Loaded {len(certs)} certificate(s) with correct password")
for i, cert in enumerate(certs):
print(f" [{i}] subject={cert.subject}")
except ValueError as e:
print(f" ValueError: {e}")
print(" (Encrypted PKCS#12 requires the 'openssl' feature to be enabled)")
def demo_wrong_password():
section("ValueError for wrong password")
path = VECTORS / "test_bundle_pass.p12"
if not path.exists():
print(f" Skipped: {path} not found")
return
data = path.read_bytes()
try:
synta.load_pkcs12_certificates(data, b"wrong_password")
print(" No error raised (unexpected — perhaps openssl feature not enabled)")
except ValueError as e:
print(f" ValueError (wrong password): {e}")
def demo_keys_unencrypted():
section("load_pkcs12_keys — unencrypted keyBag (cert-none-key-none.p12)")
path = CRYPTO_PKCS12 / "cert-none-key-none.p12"
if not path.exists():
print(f" Skipped: {path} not found")
return
data = path.read_bytes()
keys = synta.load_pkcs12_keys(data)
print(f" Loaded {len(keys)} key(s)")
for i, key_der in enumerate(keys):
print(f" [{i}] PKCS#8 DER: {len(key_der)} bytes, tag=0x{key_der[0]:02x}")
def demo_both_unencrypted():
section("load_pkcs12 — cert + unencrypted key in one call")
path = CRYPTO_PKCS12 / "cert-none-key-none.p12"
if not path.exists():
print(f" Skipped: {path} not found")
return
data = path.read_bytes()
certs, keys = synta.load_pkcs12(data)
print(f" Certificates: {len(certs)}, Keys: {len(keys)}")
for i, cert in enumerate(certs):
print(f" cert[{i}] subject={cert.subject}")
for i, key_der in enumerate(keys):
print(f" key[{i}] PKCS#8 DER: {len(key_der)} bytes")
def demo_keys_encrypted():
section("load_pkcs12_keys — AES-256-CBC shrouded keyBag (cert-key-aes256cbc.p12)")
path = CRYPTO_PKCS12 / "cert-key-aes256cbc.p12"
if not path.exists():
print(f" Skipped: {path} not found")
return
data = path.read_bytes()
try:
keys = synta.load_pkcs12_keys(data, b"cryptography")
print(f" Loaded {len(keys)} decrypted key(s)")
for i, key_der in enumerate(keys):
print(f" [{i}] PKCS#8 DER: {len(key_der)} bytes, tag=0x{key_der[0]:02x}")
except ValueError as e:
print(f" ValueError: {e}")
print(" (Encrypted keys require the 'openssl' feature to be enabled)")
def main():
print("=" * 60)
print("Example 9: PKCS#12 archive parsing")
print("=" * 60)
demo_no_password()
demo_two_certs()
demo_with_password()
demo_wrong_password()
demo_keys_unencrypted()
demo_both_unencrypted()
demo_keys_encrypted()
print("\nAll PKCS#12 examples completed.")
if __name__ == "__main__":
main()