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_limbo.py
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | """Convert C2SP x509-limbo (limbo.json) into driver.om R records (test generator, not shipped). |
| 3 | |
| 4 | usage: python3 gen_limbo.py LIMBO_JSON OUTDIR |
| 5 | Writes OUTDIR/cases.txt (R records), OUTDIR/meta.tsv (name, id, expected, host kind, flags) and, per |
| 6 | case, OUTDIR/c/NAME.store.pem plus OUTDIR/c/NAME.N.der (leaf first, then the untrusted |
| 7 | intermediates in limbo order). PEM bodies are base64-decoded directly so malformed certificates |
| 8 | reach the validator unchanged. Cases the driver cannot express are written to meta.tsv with a |
| 9 | reason instead of an R record: |
| 10 | CLIENT validation_kind CLIENT (om/tls validates server chains only) |
| 11 | NOHOST no usable peer name and no DNS SAN in the leaf to stand in for one |
| 12 | The validator always checks a DNS host name, so for a case with no expected_peer_name the |
| 13 | first DNS SAN of the leaf (a wildcard's '*' replaced by 'x') stands in (flag SANHOST). |
| 14 | """ |
| 15 | import base64 |
| 16 | import datetime |
| 17 | import json |
| 18 | import os |
| 19 | import re |
| 20 | import sys |
| 21 | |
| 22 | PEM = re.compile(rb"-----BEGIN ([A-Z0-9 ]+)-----(.*?)-----END \1-----", re.S) |
| 23 | |
| 24 | |
| 25 | def ders(text): |
| 26 | out = [] |
| 27 | for kind, body in PEM.findall(text.encode()): |
| 28 | if kind != b"CERTIFICATE": |
| 29 | continue |
| 30 | out.append(base64.b64decode(b"".join(body.split()), validate=False)) |
| 31 | return out |
| 32 | |
| 33 | |
| 34 | def san_host(der): |
| 35 | try: |
| 36 | from cryptography import x509 |
| 37 | cert = x509.load_der_x509_certificate(der) |
| 38 | san = cert.extensions.get_extension_for_class(x509.SubjectAlternativeName).value |
| 39 | for name in san.get_values_for_type(x509.DNSName): |
| 40 | if name and " " not in name: |
| 41 | return name.replace("*", "x", 1) |
| 42 | except Exception: |
| 43 | return None |
| 44 | return None |
| 45 | |
| 46 | |
| 47 | def main(): |
| 48 | src, out = sys.argv[1], sys.argv[2] |
| 49 | cases = json.load(open(src))["testcases"] |
| 50 | os.makedirs(os.path.join(out, "c"), exist_ok=True) |
| 51 | now = int(datetime.datetime.now(datetime.timezone.utc).timestamp()) |
| 52 | rec = open(os.path.join(out, "cases.txt"), "w") |
| 53 | meta = open(os.path.join(out, "meta.tsv"), "w") |
| 54 | for i, t in enumerate(cases): |
| 55 | name = "L%05d" % i |
| 56 | flags = [] |
| 57 | if t["validation_kind"] != "SERVER": |
| 58 | meta.write("%s\t%s\t%s\t-\tSKIP:CLIENT\n" % (name, t["id"], t["expected_result"])) |
| 59 | continue |
| 60 | leaf = ders(t["peer_certificate"]) |
| 61 | inter = [d for p in t["untrusted_intermediates"] for d in ders(p)] |
| 62 | peer = t["expected_peer_name"] |
| 63 | if peer is None: |
| 64 | host = san_host(leaf[0]) if leaf else None |
| 65 | kind = "SAN" |
| 66 | flags.append("SANHOST") |
| 67 | else: |
| 68 | host = peer["value"] |
| 69 | kind = peer["kind"] |
| 70 | if not host or re.search(r"\s", host) or len(host) > 290: |
| 71 | meta.write("%s\t%s\t%s\t%s\tSKIP:NOHOST\n" % (name, t["id"], t["expected_result"], kind)) |
| 72 | continue |
| 73 | if t["validation_time"]: |
| 74 | ts = int(datetime.datetime.fromisoformat(t["validation_time"]).timestamp()) |
| 75 | else: |
| 76 | ts = now |
| 77 | flags.append("NOW") |
| 78 | if t.get("crls"): |
| 79 | flags.append("CRL") |
| 80 | if t["max_chain_depth"] is not None: |
| 81 | flags.append("DEPTH%d" % t["max_chain_depth"]) |
| 82 | if t["extended_key_usage"]: |
| 83 | flags.append("EKU:" + "+".join(t["extended_key_usage"])) |
| 84 | files = [] |
| 85 | for j, der in enumerate(leaf[:1] + inter): |
| 86 | f = "c/%s.%d.der" % (name, j) |
| 87 | open(os.path.join(out, f), "wb").write(der) |
| 88 | files.append(f) |
| 89 | store = "c/%s.store.pem" % name |
| 90 | open(os.path.join(out, store), "w").write("".join(t["trusted_certs"])) |
| 91 | if not files: |
| 92 | files = ["c/%s.missing.der" % name] |
| 93 | rec.write("R %s %d %s %s %s\n" % (name, max(ts, 0), host, store, " ".join(files))) |
| 94 | meta.write("%s\t%s\t%s\t%s\t%s\n" % (name, t["id"], t["expected_result"], kind, |
| 95 | ",".join(flags + t["features"]) or "-")) |
| 96 | rec.close() |
| 97 | meta.close() |
| 98 | |
| 99 | |
| 100 | if __name__ == "__main__": |
| 101 | main() |