synta 0.1.10

ASN.1 parser, decoder, and encoder library with DER/BER support and C FFI
Documentation
#!/usr/bin/env python3
"""
Example 4: PyCA interoperability.

Demonstrates: Certificate.to_pyca, Certificate.from_pyca.

Requires the ``cryptography`` package to be installed.  The ImportError path
is demonstrated when the library is absent.
"""

import base64
import synta

_RSA_CERT_B64 = (
    "MIIDiTCCAnGgAwIBAgIUXhaeS3ad5SJp60GRJU73OQaO0xkwDQYJKoZIhvcNAQEL"
    "BQAwVDELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAkNBMRYwFAYDVQQHDA1TYW4gRnJh"
    "bmNpc2NvMQ0wCwYDVQQKDARUZXN0MREwDwYDVQQDDAh0ZXN0LmNvbTAeFw0yNjAy"
    "MjMxMDU0MzRaFw0yNzAyMjMxMDU0MzRaMFQxCzAJBgNVBAYTAlVTMQswCQYDVQQI"
    "DAJDQTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNjbzENMAsGA1UECgwEVGVzdDERMA8G"
    "A1UEAwwIdGVzdC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDP"
    "9oir0NwIXFZ6gOUo//akzjNjvUhA/V1KSUY0L/iXOGWRHFcdcf4gVhoCgR+DgCV6"
    "bJKKrYPIvfEwmd8DdpPj1WU4Dztb4NNLgxquFZym2Swe0xDLQdtWoIQYerF/ER8D"
    "9Pk0qQ5QVaCO+KB3UKyXiJwcTc/LJnDqEX24mrf0ZH/HqB2GsUE3aI9aW5Lgwm9A"
    "7+gV7FrumaT7fQqpfNucWwlXU2SIRm//JKUrT0MGrh99vmmkGRZK+c9wLfIK+pny"
    "UQxSD1E395bpQTqTWIfcMWti6af3ix3GsWeoXwY+GDfZlZ1w22GjLmSgg1RMhhKZ"
    "9l+QFnI/GtmiXX2pCRZfAgMBAAGjUzBRMB0GA1UdDgQWBBRCjPvAUpiRe0Zs6DTL"
    "K+KLoTZfezAfBgNVHSMEGDAWgBRCjPvAUpiRe0Zs6DTLK+KLoTZfezAPBgNVHRMB"
    "Af8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQC+xBpTScGlcZGYvwtWLZuF2qRu"
    "o9xhYzHXgIDJglXZ8i70O+ut2WRyI9RJOSMsa7BI2qmc87Ki9ZMCO2QIMQCQo7cB"
    "kZtQvK8iGGHhSwepMORekzdbfUUs7N4YEM3Xako1+4RzL+T1Z3qzQ6nrnQ+gYyQo"
    "GiIFKbYZONu2OlqXGQe5LPwbsPU52GUbttkxodaHgCdP7yKO/l3sDifXpaFEXJhY"
    "5RZqgSG77jymh8YKcY0X9J53OtP1So/IOS1Za137k+eYci6iCB4w511qEhSRZdCy"
    "+3NEoGrBearMcJHttGbMplR6TU6fDFRIUAdelPdWfpfmtl1iElRDwNdQkBBR"
)


def section(title):
    print(f"\n{'' * 60}\n{title}\n{'' * 60}")


def demo_to_pyca(synta_cert):
    section("Certificate.to_pyca() — synta → cryptography")
    try:
        pyca_cert = synta_cert.to_pyca()
        from cryptography.x509 import Certificate as PyCAX509
        assert isinstance(pyca_cert, PyCAX509)
        print(f"  pyca subject: {pyca_cert.subject.rfc4514_string()}")
        print(f"  pyca serial:  {pyca_cert.serial_number}")
        print(f"  to_pyca(): OK")
        return pyca_cert
    except ImportError as e:
        print(f"  ImportError (cryptography not installed): {e}")
        return None


def demo_from_pyca(pyca_cert):
    section("Certificate.from_pyca() — cryptography → synta")
    if pyca_cert is None:
        print("  Skipped (cryptography not available)")
        return
    synta_cert = synta.Certificate.from_pyca(pyca_cert)
    assert isinstance(synta_cert, synta.Certificate)
    print(f"  synta subject: {synta_cert.subject}")
    print(f"  from_pyca(): OK")

    # Fields must match
    from cryptography.hazmat.primitives.serialization import Encoding
    pyca_der = pyca_cert.public_bytes(Encoding.DER)
    synta_der = synta_cert.to_der()
    assert pyca_der == synta_der
    print("  DER from both libraries is identical: OK")


def demo_import_error_message():
    section("ImportError message when cryptography is absent (simulated)")
    # Simulate the ImportError message the binding would raise
    msg = ("'cryptography' package is required for to_pyca(). "
           "Install it with: pip install cryptography")
    print(f"  Expected ImportError message: {msg!r}")
    print("  (Actual ImportError is raised if cryptography is not installed)")


def main():
    print("=" * 60)
    print("Example 4: PyCA interoperability")
    print("=" * 60)

    der = base64.b64decode(_RSA_CERT_B64)
    cert = synta.Certificate.from_der(der)
    print(f"  Parsed synta cert: {cert.subject}")

    pyca_cert = demo_to_pyca(cert)
    demo_from_pyca(pyca_cert)
    demo_import_error_message()
    print("\nAll PyCA interoperability examples completed.")


if __name__ == "__main__":
    main()