interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / tls / test / x509 / gen_distrust.py

98 lines · 3630 bytes Raw Blame History
1# SPDX-License-Identifier: Apache-2.0
2"""Mozilla partial-distrust table for om/tls/x509.om (generator, not shipped).
3
4usage: python3 gen_distrust.py CERTDATA_TXT [BUNDLE_PEM]
5Reads Mozilla NSS certdata.txt and prints, for every root with CKA_NSS_SERVER_DISTRUST_AFTER, one
6line "SHA256(root DER) UNIX_SECONDS LABEL": a leaf whose notBefore is later than UNIX_SECONDS must not
7chain to that root (Mozilla root store policy 7.4 / NSS behaviour). The hex pairs go into x509.om
8X5DIST. With BUNDLE_PEM it also checks the bundle against certdata: every bundle root must be a
9CKO_NSS_TRUST object with CKA_TRUST_SERVER_AUTH CKT_NSS_TRUSTED_DELEGATOR, and it reports which
10partially distrusted roots the bundle carries. Exit 1 on a bundle root that is not a trusted
11server-auth delegator.
12"""
13import base64
14import calendar
15import hashlib
16import re
17import sys
18
19
20def octal(lines):
21 out = bytearray()
22 for ln in lines:
23 for m in re.findall(r"\\([0-7]{3})", ln):
24 out.append(int(m, 8))
25 return bytes(out)
26
27
28def objects(path):
29 obj, cur, ml = {}, None, None
30 for ln in open(path, encoding="utf-8"):
31 ln = ln.rstrip("\n")
32 if ml is not None:
33 if ln == "END":
34 obj[cur] = octal(ml)
35 ml = None
36 else:
37 ml.append(ln)
38 continue
39 if ln.startswith("CKA_CLASS"):
40 if obj:
41 yield obj
42 obj = {}
43 if not ln.startswith("CKA_"):
44 continue
45 f = ln.split(None, 2)
46 if len(f) >= 2 and f[1] == "MULTILINE_OCTAL":
47 cur, ml = f[0], []
48 elif len(f) == 3:
49 obj[f[0]] = f[2].strip('"') if f[1] == "UTF8" else f[2]
50 if obj:
51 yield obj
52
53
54def utc(b):
55 s = b.decode()
56 if len(s) == 13: # YYMMDDHHMMSSZ
57 y = int(s[:2])
58 s = ("20" if y < 50 else "19") + s
59 return calendar.timegm((int(s[:4]), int(s[4:6]), int(s[6:8]), int(s[8:10]), int(s[10:12]), int(s[12:14])))
60
61
62def main():
63 certs, trust = {}, {}
64 for o in objects(sys.argv[1]):
65 if o.get("CKA_CLASS") == "CKO_CERTIFICATE" and "CKA_VALUE" in o:
66 certs[hashlib.sha1(o["CKA_VALUE"]).digest()] = o
67 elif o.get("CKA_CLASS") == "CKO_NSS_TRUST" and "CKA_CERT_SHA1_HASH" in o:
68 trust[o["CKA_CERT_SHA1_HASH"]] = o
69 rows = {}
70 for h, o in certs.items():
71 k = hashlib.sha256(o["CKA_VALUE"]).hexdigest()
72 lab = o.get("CKA_LABEL", "?")
73 t = trust.get(h, {}).get("CKA_TRUST_SERVER_AUTH")
74 d = o.get("CKA_NSS_SERVER_DISTRUST_AFTER")
75 if t != "CKT_NSS_TRUSTED_DELEGATOR":
76 rows[k] = (0, "not a Mozilla server-auth root: " + lab)
77 elif isinstance(d, bytes) and d:
78 rows[k] = (utc(d), "distrust after: " + lab)
79 bad = 0
80 if len(sys.argv) >= 3:
81 pem = open(sys.argv[2]).read()
82 ders = [base64.b64decode(b) for b in re.findall(r"-----BEGIN CERTIFICATE-----(.*?)-----END CERTIFICATE-----", pem, re.S)]
83 n = 0
84 for der in ders:
85 k = hashlib.sha256(der).hexdigest()
86 if hashlib.sha1(der).digest() not in certs:
87 rows[k] = (0, "bundle root absent from certdata (removed by Mozilla)")
88 if k in rows:
89 n += 1
90 print("BUNDLE DISTRUSTED %s %d %s" % (k, rows[k][0], rows[k][1]))
91 print("BUNDLE roots=%d distrusted=%d" % (len(ders), n))
92 for k in sorted(rows):
93 print("%s %010d %s" % (k, rows[k][0], rows[k][1]))
94 print('text X5DIST "%s"' % "".join("%s %010d " % (k, rows[k][0]) for k in sorted(rows)))
95 return bad
96
97
98sys.exit(main())