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 / rev / gen_revweak.py
| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # TEST INSTRUMENT ONLY (fixture builder; never on the product path): revocation signers with weak or |
| 4 | # over-ceiling RSA keys (review omega-mv-tlscap-35d44b5a finding 1, revocation path: rev_sig -> |
| 5 | # chain_sig reads the RSA key size). Writes vectors/revweak/*.der with python cryptography (>= 41). |
| 6 | # Fresh random keys each run; private keys are never written; cases.txt does not depend on key bytes. |
| 7 | # Also writes vectors/revweak/cases.txt (driver.om O records, appended to vectors/cases.txt by run.sh): |
| 8 | # a 1024-bit signer is X509_ERR_WEAK_KEY whatever its exponent; a 2048-bit one is accepted, also with |
| 9 | # e = 2^32+1 (revocation signers, like anchors, need >= 2048 bits; no exponent ceiling applies to them). |
| 10 | # ocsp: an EC P-256 issuer iss, a leaf by it, and per (bits, e) a delegated OCSP responder |
| 11 | # resp<bits>e<e> (id-kp-OCSPSigning, issued by iss) with that RSA key, signing a "good" response |
| 12 | # o_resp<bits>e<e>.der that carries the responder certificate |
| 13 | # crl: per (bits, e) an RSA issuer crliss<bits>e<e> (keyCertSign, cRLSign), a leaf lcrl<bits>e<e> by |
| 14 | # it with a CRL distribution point, and a clean CRL c_<bits>e<e>.der signed by that issuer |
| 15 | # Usage: python3 gen_revweak.py |
| 16 | import calendar |
| 17 | import datetime as dt |
| 18 | import math |
| 19 | import os |
| 20 | import sys |
| 21 | |
| 22 | from cryptography import x509 |
| 23 | from cryptography.hazmat.primitives import hashes, serialization |
| 24 | from cryptography.hazmat.primitives.asymmetric import ec, rsa |
| 25 | from cryptography.x509 import ocsp |
| 26 | from cryptography.x509.oid import ExtendedKeyUsageOID, NameOID |
| 27 | |
| 28 | OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "vectors", "revweak") |
| 29 | os.makedirs(OUT, exist_ok=True) |
| 30 | for f in os.listdir(OUT): |
| 31 | os.remove(os.path.join(OUT, f)) |
| 32 | NOW = dt.datetime(2026, 10, 27) |
| 33 | H = dt.timedelta(hours=1) |
| 34 | D = dt.timedelta(days=1) |
| 35 | NB, NA = dt.datetime(2026, 1, 1), dt.datetime(2027, 1, 1) |
| 36 | DP = "http://crl.example.test/weak.crl" |
| 37 | KEYS = [(1024, 4294967297), (1024, 65537), (2048, 65537), (2048, 4294967297)] |
| 38 | |
| 39 | |
| 40 | def die(msg): |
| 41 | sys.exit("gen_revweak.py: " + msg) |
| 42 | |
| 43 | |
| 44 | def name(cn): |
| 45 | return x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, cn)]) |
| 46 | |
| 47 | |
| 48 | def rsa_with_e(bits, e): |
| 49 | """an RSA key of exactly `bits` bits with public exponent e (odd, coprime to lambda)""" |
| 50 | while True: |
| 51 | k = rsa.generate_private_key(public_exponent=65537, key_size=bits) |
| 52 | p, q = k.private_numbers().p, k.private_numbers().q |
| 53 | lam = (p - 1) * (q - 1) // math.gcd(p - 1, q - 1) |
| 54 | if math.gcd(e, lam) == 1: |
| 55 | d = pow(e, -1, lam) |
| 56 | k = rsa.RSAPrivateNumbers(p, q, d, rsa.rsa_crt_dmp1(d, p), rsa.rsa_crt_dmq1(d, q), |
| 57 | rsa.rsa_crt_iqmp(p, q), rsa.RSAPublicNumbers(e, p * q)).private_key() |
| 58 | if k.public_key().key_size != bits or k.public_key().public_numbers().e != e: |
| 59 | die("key %d/%d came out %d bits" % (bits, e, k.public_key().key_size)) |
| 60 | return k |
| 61 | |
| 62 | |
| 63 | def cert(cn, pub, icn, ik, serial, ca=False, kus=None, eku=None, dp=False, san=False): |
| 64 | b = (x509.CertificateBuilder().subject_name(name(cn)).issuer_name(name(icn)).public_key(pub) |
| 65 | .serial_number(serial).not_valid_before(NB).not_valid_after(NA) |
| 66 | .add_extension(x509.BasicConstraints(ca=ca, path_length=None), critical=True)) |
| 67 | if kus is not None: |
| 68 | b = b.add_extension(kus, critical=True) |
| 69 | if eku is not None: |
| 70 | b = b.add_extension(x509.ExtendedKeyUsage(eku), critical=False) |
| 71 | if dp: |
| 72 | b = b.add_extension(x509.CRLDistributionPoints([x509.DistributionPoint( |
| 73 | [x509.UniformResourceIdentifier(DP)], None, None, None)]), critical=False) |
| 74 | if san: |
| 75 | b = b.add_extension(x509.SubjectAlternativeName([x509.DNSName("www.example.test")]), critical=False) |
| 76 | return b.sign(ik, hashes.SHA256()) |
| 77 | |
| 78 | |
| 79 | def ku(cert_sign=False, crl_sign=False, ds=True): |
| 80 | return x509.KeyUsage(ds, False, False, False, False, cert_sign, crl_sign, False, False) |
| 81 | |
| 82 | |
| 83 | def save(fn, data): |
| 84 | with open(os.path.join(OUT, fn), "wb") as f: |
| 85 | f.write(data) |
| 86 | |
| 87 | |
| 88 | def der(c): |
| 89 | return c.public_bytes(serialization.Encoding.DER) |
| 90 | |
| 91 | |
| 92 | ki = ec.generate_private_key(ec.SECP256R1()) |
| 93 | iss = cert("RW Issuer", ki.public_key(), "RW Issuer", ki, 1, ca=True, kus=ku(True, True, True)) |
| 94 | save("iss.der", der(iss)) |
| 95 | kl = ec.generate_private_key(ec.SECP256R1()) |
| 96 | leaf = cert("www.example.test", kl.public_key(), "RW Issuer", ki, 0x7001, kus=ku(), |
| 97 | eku=[ExtendedKeyUsageOID.SERVER_AUTH], san=True) |
| 98 | save("leaf.der", der(leaf)) |
| 99 | NOWU = calendar.timegm(NOW.timetuple()) |
| 100 | lines = ["# generated by ../../gen_revweak.py, do not edit; NOW = %d = 2026-10-27T00:00:00Z" % NOWU] |
| 101 | for bits, e in KEYS: |
| 102 | tag = "%de%d" % (bits, e) |
| 103 | # OCSP: delegated responder with the RSA key, issued by the EC issuer |
| 104 | rk = rsa_with_e(bits, e) |
| 105 | resp = cert("RW OCSP Responder " + tag, rk.public_key(), "RW Issuer", ki, 0x7100 + bits, |
| 106 | kus=ku(), eku=[ExtendedKeyUsageOID.OCSP_SIGNING]) |
| 107 | save("resp%s.der" % tag, der(resp)) |
| 108 | r = (ocsp.OCSPResponseBuilder().add_response( |
| 109 | cert=leaf, issuer=iss, algorithm=hashes.SHA1(), cert_status=ocsp.OCSPCertStatus.GOOD, |
| 110 | this_update=NOW - H, next_update=NOW + 3 * D, revocation_time=None, revocation_reason=None) |
| 111 | .responder_id(ocsp.OCSPResponderEncoding.NAME, resp).certificates([resp]).sign(rk, hashes.SHA256())) |
| 112 | if ocsp.load_der_ocsp_response(r.public_bytes(serialization.Encoding.DER)).certificates[0] != resp: |
| 113 | die("response %s does not carry its responder" % tag) |
| 114 | save("o_resp%s.der" % tag, r.public_bytes(serialization.Encoding.DER)) |
| 115 | # CRL: an RSA issuer with the key signs the leaf and a clean CRL |
| 116 | ck = rsa_with_e(bits, e) |
| 117 | icn = "RW CRL Issuer " + tag |
| 118 | ci = cert(icn, ck.public_key(), icn, ck, 0x7200 + bits, ca=True, kus=ku(True, True, True)) |
| 119 | save("crliss%s.der" % tag, der(ci)) |
| 120 | lc = cert("www.example.test", kl.public_key(), icn, ck, 0x7300 + bits, kus=ku(), |
| 121 | eku=[ExtendedKeyUsageOID.SERVER_AUTH], dp=True, san=True) |
| 122 | lc.verify_directly_issued_by(ci) |
| 123 | save("lcrl%s.der" % tag, der(lc)) |
| 124 | c = (x509.CertificateRevocationListBuilder().issuer_name(name(icn)).last_update(NOW - H) |
| 125 | .next_update(NOW + 7 * D).add_extension(x509.CRLNumber(7), critical=False) |
| 126 | .add_revoked_certificate(x509.RevokedCertificateBuilder().serial_number(0x7fff) |
| 127 | .revocation_date(NOW - 2 * D).build()) |
| 128 | .sign(ck, hashes.SHA256())) |
| 129 | if not c.is_signature_valid(ck.public_key()): |
| 130 | die("crl %s does not verify" % tag) |
| 131 | save("c_%s.der" % tag, c.public_bytes(serialization.Encoding.DER)) |
| 132 | want = "X509_ERR_WEAK_KEY" if bits < 2048 else "X509_OK" |
| 133 | en = "2_32_plus_1" if e == 4294967297 else str(e) |
| 134 | lines.append("O ocsp_responder_rsa%d_e%s %s %d revweak/leaf.der revweak/iss.der revweak/o_resp%s.der -" |
| 135 | % (bits, en, want, NOWU, tag)) |
| 136 | lines.append("O crl_issuer_rsa%d_e%s %s %d revweak/lcrl%s.der revweak/crliss%s.der - revweak/c_%s.der" |
| 137 | % (bits, en, want, NOWU, tag, tag, tag)) |
| 138 | with open(os.path.join(OUT, "cases.txt"), "w") as f: |
| 139 | f.write("\n".join(lines) + "\n") |
| 140 | print("revweak fixtures:", len(os.listdir(OUT))) |