omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / tls / test / x509 / gen_anchor.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # TEST INSTRUMENT ONLY (Python as a fixture builder; never on the product path): write the trust-anchor |
| 3 | # key-policy fixtures of review omega-mv-tlscap-35d44b5a finding 1 into vectors/anchor/ with Python |
| 4 | # cryptography (>= 41). Each root is a valid self-signed CA (operator-installed: it is only in its own |
| 5 | # store file), each leaf is a valid EC P-256 server certificate signed by that root with |
| 6 | # sha256WithRSAEncryption. Fresh random keys each run; private keys are never written; cases.txt does |
| 7 | # not depend on key bytes. |
| 8 | # rootA<bits>e<e> storeA<bits>e<e>.pem lA<bits>e<e>.der for (bits, e) in ROOTS below |
| 9 | # 1024/2^32+1 the finding: a weak modulus whose exponent is over the presented-key exponent policy |
| 10 | # (3 .. 2^32-1), which used to be added into the bit count the anchor check reads |
| 11 | # 1024/65537 control: a weak modulus with the usual exponent |
| 12 | # 2048/65537 control: an adequate root |
| 13 | # 2048/2^32+1 control: an adequate modulus with an exponent over 32 bits (anchors need only >= 2048 |
| 14 | # bits; the presented-key exponent policy does not apply to them) |
| 15 | # Usage: python3 gen_anchor.py |
| 16 | import datetime |
| 17 | import math |
| 18 | import os |
| 19 | import sys |
| 20 | |
| 21 | from cryptography import x509 |
| 22 | from cryptography.hazmat.primitives import hashes, serialization |
| 23 | from cryptography.hazmat.primitives.asymmetric import ec, rsa |
| 24 | from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID |
| 25 | |
| 26 | here = os.path.dirname(os.path.abspath(__file__)) |
| 27 | out = os.path.join(here, "vectors", "anchor") |
| 28 | os.makedirs(out, exist_ok=True) |
| 29 | for f in os.listdir(out): |
| 30 | os.remove(os.path.join(out, f)) |
| 31 | T0, T1 = datetime.datetime(2026, 1, 1), datetime.datetime(2027, 1, 1) |
| 32 | R0, R1 = datetime.datetime(2020, 1, 1), datetime.datetime(2051, 1, 1) |
| 33 | ROOTS = [(1024, 4294967297), (1024, 65537), (2048, 65537), (2048, 4294967297)] |
| 34 | |
| 35 | |
| 36 | def die(msg): |
| 37 | sys.exit("gen_anchor.py: " + msg) |
| 38 | |
| 39 | |
| 40 | def name(cn): |
| 41 | return x509.Name([x509.NameAttribute(NameOID.ORGANIZATION_NAME, "omtls test"), |
| 42 | x509.NameAttribute(NameOID.COMMON_NAME, cn)]) |
| 43 | |
| 44 | |
| 45 | def rsa_with_e(bits, e): |
| 46 | """an RSA key of exactly `bits` bits with public exponent e (odd, coprime to lambda)""" |
| 47 | while True: |
| 48 | k = rsa.generate_private_key(public_exponent=65537, key_size=bits) |
| 49 | p, q = k.private_numbers().p, k.private_numbers().q |
| 50 | lam = (p - 1) * (q - 1) // math.gcd(p - 1, q - 1) |
| 51 | if math.gcd(e, lam) == 1: |
| 52 | d = pow(e, -1, lam) |
| 53 | pub = rsa.RSAPublicNumbers(e, p * q) |
| 54 | return rsa.RSAPrivateNumbers(p, q, d, rsa.rsa_crt_dmp1(d, p), rsa.rsa_crt_dmq1(d, q), |
| 55 | rsa.rsa_crt_iqmp(p, q), pub).private_key() |
| 56 | |
| 57 | |
| 58 | for bits, e in ROOTS: |
| 59 | tag = "%de%d" % (bits, e) |
| 60 | rk = rsa_with_e(bits, e) |
| 61 | if rk.public_key().key_size != bits or rk.public_key().public_numbers().e != e: |
| 62 | die("root %s: got %d bits e=%d" % (tag, rk.public_key().key_size, rk.public_key().public_numbers().e)) |
| 63 | rn = name("rootA" + tag) |
| 64 | root = (x509.CertificateBuilder().subject_name(rn).issuer_name(rn).public_key(rk.public_key()) |
| 65 | .serial_number(x509.random_serial_number()).not_valid_before(R0).not_valid_after(R1) |
| 66 | .add_extension(x509.SubjectKeyIdentifier.from_public_key(rk.public_key()), critical=False) |
| 67 | .add_extension(x509.BasicConstraints(ca=True, path_length=None), critical=True) |
| 68 | .add_extension(x509.KeyUsage(False, False, False, False, False, True, True, False, False), critical=True) |
| 69 | .sign(rk, hashes.SHA256())) |
| 70 | lk = ec.generate_private_key(ec.SECP256R1()) |
| 71 | leaf = (x509.CertificateBuilder().subject_name(name("lA" + tag)).issuer_name(rn).public_key(lk.public_key()) |
| 72 | .serial_number(x509.random_serial_number()).not_valid_before(T0).not_valid_after(T1) |
| 73 | .add_extension(x509.SubjectKeyIdentifier.from_public_key(lk.public_key()), critical=False) |
| 74 | .add_extension(x509.AuthorityKeyIdentifier.from_issuer_public_key(rk.public_key()), critical=False) |
| 75 | .add_extension(x509.BasicConstraints(ca=False, path_length=None), critical=True) |
| 76 | .add_extension(x509.KeyUsage(True, False, False, False, False, False, False, False, False), critical=True) |
| 77 | .add_extension(x509.ExtendedKeyUsage([ExtendedKeyUsageOID.SERVER_AUTH]), critical=False) |
| 78 | .add_extension(x509.SubjectAlternativeName([x509.DNSName("www.example.test"), |
| 79 | x509.DNSName("example.test")]), critical=False) |
| 80 | .sign(rk, hashes.SHA256())) |
| 81 | # the leaf really is signed by the root (a failure here is a generator defect, not a test result) |
| 82 | try: |
| 83 | leaf.verify_directly_issued_by(root) |
| 84 | except Exception as x: # noqa: BLE001 - any verification error is a generator defect |
| 85 | die("leaf %s does not verify under its root: %s" % (tag, x)) |
| 86 | with open(os.path.join(out, "rootA%s.der" % tag), "wb") as f: |
| 87 | f.write(root.public_bytes(serialization.Encoding.DER)) |
| 88 | with open(os.path.join(out, "storeA%s.pem" % tag), "wb") as f: |
| 89 | f.write(root.public_bytes(serialization.Encoding.PEM)) |
| 90 | with open(os.path.join(out, "lA%s.der" % tag), "wb") as f: |
| 91 | f.write(leaf.public_bytes(serialization.Encoding.DER)) |
| 92 | print("anchor fixtures:", len(os.listdir(out))) |