synta 0.1.2

ASN.1 parser, decoder, and encoder library with DER/BER support and C FFI
Documentation
#!/usr/bin/env python3
"""
Example 9: PKCS#12 archive parsing.

Demonstrates: load_pkcs12_certificates, load_pkcs12_keys, load_pkcs12.

Test vectors:
  tests/vectors/test_bundle_nopass.p12  (no password, 1+ certs)
  tests/vectors/test_bundle_pass.p12    (password-protected)
  tests/vectors/test_bundle_2certs.p12  (2 certs, no password)
  tests/vectors/cryptography/…/pkcs12/cert-none-key-none.p12
                                        (1 cert + 1 unencrypted key, no password)
  tests/vectors/cryptography/…/pkcs12/cert-key-aes256cbc.p12
                                        (1 cert + 1 AES-256-CBC encrypted key,
                                         password = b"cryptography")
"""

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:
        # May fail if the openssl feature is not compiled in
        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):
        # Each blob is a DER-encoded OneAsymmetricKey (PKCS#8) SEQUENCE (tag 0x30)
        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()