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 / x509.om

2402 lines · 59294 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/tls/x509.om (module X5): strict DER reader, X.509 v3 certificate parser, PEM trust store,
3note TLS 1.3 server chain validation and the CertificateVerify signature check. Library unit for the
4note OM-2 TLS client; compose after sha2.om, hkdf.om, ec.om, rsa.om. No mutable globals.
5note
6note Reject codes (ulong, 0 = valid; x509_errname gives the ASCII name):
7note 1 MALFORMED DER/structure/framing error (any input that does not parse)
8note 2 EXPIRED now >= notAfter of a non-anchor certificate in the path (at notAfter
9note itself already expired, as OpenSSL; stricter than RFC 5280 4.1.2.5)
10note 3 NOT_YET_VALID now < notBefore of a non-anchor certificate in the path
11note 4 NAME_MISMATCH no SAN dNSName matches the host, or the host is not a valid LDH name
12note 5 UNKNOWN_ISSUER no trust anchor or presented certificate has the issuer's name (only
13note self-issued store entries are trust anchors, see chain_anchor)
14note 6 BAD_SIGNATURE issuer found by name but no candidate key verifies the signature
15note 7 NOT_CA issuer lacks basicConstraints cA TRUE
16note 8 PATH_LENGTH pathLenConstraint of an issuer exceeded
17note 9 KEY_USAGE issuer keyUsage lacks keyCertSign, or leaf keyUsage lacks digitalSignature
18note 10 EKU leaf EKU lacks serverAuth, or issuer EKU lacks serverAuth and anyExtendedKeyUsage
19note 11 UNSUPPORTED_ALG signature algorithm or key type not on the allowed list, or key/alg mismatch
20note 12 WEAK_KEY RSA key out of policy: modulus below 2048 bits (leaf key or any key that
21note verifies a signature); for presented keys (leaf and chain) also a modulus
22note above 4096 bits or a public exponent that is even, below 3 or above 2^32-1
23note 13 CRITICAL_EXT critical extension we do not implement
24note 14 TOO_DEEP more than 8 non-anchor certificates would be needed
25note 15 LEAF_IS_CA leaf has basicConstraints cA TRUE
26note 16 NO_SAN leaf has no subjectAltName (no CN fallback)
27note 17 IP_HOST host is an IP literal (refused)
28note 18 ALG_MISMATCH tbsCertificate.signature differs from signatureAlgorithm
29note 19 SCHEME CertificateVerify scheme refused or does not match the leaf key
30note 20 NAME_CONSTRAINTS a nameConstraints extension (critical or not) on any certificate: name
31note constraints are not implemented, so such a certificate is always refused
32note
33note Constant time: nothing here handles secrets. Certificates, names, signatures and keys are public;
34note every function is variable time by design (branches on public data). Hashing and signature checks
35note are done by sha2.om, ec.om and rsa.om (see their notes).
36
37const X5RSZ ulong 32
38const X5RTBO ulong 0
39const X5RTBL ulong 1
40const X5RALG ulong 2
41const X5RSGO ulong 3
42const X5RSGL ulong 4
43const X5RISO ulong 5
44const X5RISL ulong 6
45const X5RSBO ulong 7
46const X5RSBL ulong 8
47const X5RNB ulong 9
48const X5RNA ulong 10
49const X5RKT ulong 11
50const X5RKO ulong 12
51const X5RKL ulong 13
52const X5RBITS ulong 14
53const X5RBC ulong 15
54const X5RCA ulong 16
55const X5RPL ulong 17
56const X5RKU ulong 18
57const X5RKUB ulong 19
58const X5REKU ulong 20
59const X5RSRV ulong 21
60const X5RANY ulong 22
61const X5RSNO ulong 23
62const X5RSNL ulong 24
63const X5RSAN ulong 25
64const X5RVER ulong 26
65const X5RSEEN ulong 27
66const X5RAO ulong 28
67const X5RAL ulong 29
68const X5RTAO ulong 30
69const X5RTAL ulong 31
70
71const X5WNOW ulong 64
72const X5WN ulong 65
73const X5WUSED ulong 66
74const X5WDEP ulong 67
75const X5WCUR ulong 68
76const X5WSEL ulong 69
77const X5WLNB ulong 70
78const X5WIX ulong 96
79const X5WSZ ulong 128
80const X5MAXC ulong 16
81const X5MAXD ulong 8
82const X5KBUF ulong 16384
83note largest presented leaf (bytes, DER), equal to TLLFSZ in tls.om; a larger one is X5EMAL
84const X5LMAX ulong 16896
85note element cap of the SEQUENCE OF loops (EKU, SAN, Extensions, SAN host walk): X5EMAL beyond it
86const X5NMAX ulong 9999
87const X5BIG ulong 0xffffffffffffffff
88
89const X5EMAL ulong 1
90const X5EEXP ulong 2
91const X5ENYV ulong 3
92const X5ENAME ulong 4
93const X5EUNK ulong 5
94const X5ESIG ulong 6
95const X5ENCA ulong 7
96const X5EPATH ulong 8
97const X5EKU ulong 9
98const X5EEKU ulong 10
99const X5EALG ulong 11
100const X5EWEAK ulong 12
101const X5ECRIT ulong 13
102const X5EDEEP ulong 14
103const X5ELCA ulong 15
104const X5ENSAN ulong 16
105const X5EIP ulong 17
106const X5EAMIS ulong 18
107const X5ESCH ulong 19
108const X5ENC ulong 20
109const X5EEXT ulong 21
110const X5XMAX ulong 64
111
112note OID content bytes (hex), space separated; der_oidix returns the 1-based position.
113note 1-3 sha256/384/512WithRSAEncryption (RFC 4055), 4 RSASSA-PSS, 5-7 ecdsa-with-SHA256/384/512
114note (RFC 5758), 8 rsaEncryption, 9 id-ecPublicKey, 10 prime256v1, 11 secp384r1 (RFC 5480),
115note 12-14 sha256/384/512 (NIST CSOR), 15 id-mgf1, 16 basicConstraints, 17 keyUsage, 18 extKeyUsage,
116note 19 subjectAltName, 20 nameConstraints (RFC 5280 4.2.1), 21 id-kp-serverAuth, 22 anyExtendedKeyUsage,
117note 23 authorityKeyIdentifier, 24 subjectKeyIdentifier
118text X5OIDS "2a864886f70d01010b 2a864886f70d01010c 2a864886f70d01010d 2a864886f70d01010a 2a8648ce3d040302 2a8648ce3d040303 2a8648ce3d040304 2a864886f70d010101 2a8648ce3d0201 2a8648ce3d030107 2b81040022 608648016503040201 608648016503040202 608648016503040203 2a864886f70d010108 551d13 551d0f 551d25 551d11 551d1e 2b06010505070301 551d2500 551d23 551d0e "
119text X5NAMES "X509_OK X509_ERR_MALFORMED X509_ERR_EXPIRED X509_ERR_NOT_YET_VALID X509_ERR_NAME_MISMATCH X509_ERR_UNKNOWN_ISSUER X509_ERR_BAD_SIGNATURE X509_ERR_NOT_CA X509_ERR_PATH_LENGTH X509_ERR_KEY_USAGE X509_ERR_EKU X509_ERR_UNSUPPORTED_ALG X509_ERR_WEAK_KEY X509_ERR_CRITICAL_EXT X509_ERR_TOO_DEEP X509_ERR_LEAF_IS_CA X509_ERR_NO_SAN X509_ERR_IP_HOST X509_ERR_ALG_MISMATCH X509_ERR_SCHEME X509_ERR_NAME_CONSTRAINTS X509_ERR_ENTRY_EXT "
120text X5PATH "/etc/ssl/certs/ca-certificates.crt"
121text X5BEG "-----BEGIN CERTIFICATE-----"
122text X5END "-----END CERTIFICATE-----"
123note X5DIST (M1): Mozilla server-auth distrust, "SHA256(anchor DER) CUTOFF " per 76 characters. A leaf
124note with notBefore > CUTOFF (unix seconds) never chains to that anchor; CUTOFF 0 = not a Mozilla
125note server-auth root. Generated by test/x509/gen_distrust.py from NSS certdata.txt (sha256 3ec2b4d2..,
126note mozilla-firefox main 2026-10-26) and the Debian ca-certificates 20260601 bundle (8 of its 121 roots).
127text X5DIST "02ed0eb28c14da45165c566791700d6451d7fb56f0b2ab1d3b8eb070e56edff5 0000000000 0c2cd63df7806fa399ede809116b575bf87989f06518f9808c860503178baf66 0000000000 125609aa301da0a249b97a8239cb6a34216f44dcac9f3954b14292f2e8c8608f 0000000000 1465fa205397b876faa6f0a9958e5590e40fcc7faa4fb7c2c8677521fb5fb658 0000000000 18f1fc7f205df8adddeb7fe007dd57e3af375a9c4d8d73546bf4f1fed1e18d35 0000000000 1be7abe30686b16348afd1c61b6866a0ea7f4821e67d5e8af937cf8011bc750d 0000000000 1d4ca4a2ab21d0093659804fc0eb2175a617279b56a2475245c9517afeb59153 0000000000 22d9599234d60f1d4bc7c7e96f43fa555b07301fd475175089dafb8c25e477b3 0000000000 2530cc8e98321502bad96f9b1fba1b099e2d299e0f4548bb914f363bc0d4531f 1776297599 261d7114ae5f8ff2d8c7209a9de4289e6afc9d717023d85450909199f1857cfe 0000000000 319af0a7729e6f89269c131ea6a3a16fcd86389fdcab3c47a4a675c161a3f974 0000000000 3629e7188e00a7cb3232c4426bc84912f1218b1a9ae676c0b0abe1dbfe2182b5 0000000000 3682228d7d678b571440cf1cb34e69fb4135fd6c2a1be38e14163b711e02ae01 0000000000 3a0072d49ffc04e996c59aeb75991d3c340f3615d6fd4dce90ac0b3d88ead4f4 0000000000 3ae6df7e0d637a65a8c81612ec6f9a142f85a16834c10280d88e707028518755 0000000000 3c4fb0b95ab8b30032f432b86f535fe172c185d0fd39865837cf36187fa6f428 0000000000 3e9099b5015e8f486c00bcea9d111ee721faba355a89bcf1df69561e3dc6325c 0000000000 3f034bb5704d44b2d08545a02057de93ebf3905fce721acbc730c06ddaee904e 0000000000 41c923866ab4cad6b7ad578081582e020797a6cbdf4fff78ce8396b38937d7f5 0000000000 4348a0e9444c78cb265e058d5e8944b4d84f9662bd26db257f8934a443c70161 0000000000 436472c1009a325c54f1a5bbb5468a7baeeccbe05de5f099cb70d3fe41e13c16 0000000000 43df5774b03e7fef5fe40d931a7bedf1bb2e6b42738c4e6d3841103d3aa7f339 0000000000 48e1cf9e43b688a51044160f46d773b8277fe45beaad0e4df90d1974382fea99 0000000000 4c1ccd24f17e950fc18536b33cafe32293cfc33e8467b41e1c693055d7f513bf 0000000000 5b0c502a7d963ba55217396fda9b3dc78171000aeeff42cecc3a20a7938163e8 0000000000 5cbf6fb81fd417ea4128cd6f8172a3c9402094f74ab2ed3a06b4405d04f30b19 0000000000 62dd0be9b9f50a163ea0f8e75c053b1eca57ea55c8688f647c6881f2c8357b95 0000000000 6dc47172e01cbcb0bf62580d895fe2b8ac9ad4f873801e0c10b9c837d21eb177 0000000000 73c176434f1bc6d5adf45b0e76e727287c8de57616c1e6e6141a2b2cbc7d8e4c 0000000000 7431e5f4c3c1ce4690774f0b61e05440883ba9a01ed00ba6abd7806ed3b118cf 0000000000 78833a783bb2986c254b9370d3c20e5eba8fa7840cbf63fe17297a0b0119685e 0000000000 78a656344f947e9cc0f734d9053d32f6742086b6b9cd2cae4fae1a2e4efde048 0000000000 82bd5d851acf7f6e1ba7bfcbc53030d0e7bc3c21df772d858cab41d199bdf595 0000000000 85a0dd7dd720adb7ff05f83d542b209dc7ff4528f7d677b18389fea5e5c49e86 0000000000 86a1ecba089c4a8d3bbe2734c612ba341d813e043cf9e8a862cd5c57a36bbe6b 0000000000 8d25cd97229dbf70356bda4eb3cc734031e24cf00fafcfd32dc76eb5841c7ea8 0000000000 8dd4b5373cb0de36769c12339280d82746b3aa6cd426e797a31babe4279cf00b 0000000000 90370d3efa88bf58c30105ba25104a358460a7fa52dfc2011df233a0f417912a 0000000000 9a12c392bfe57891a0c545309d4d9fd567e480cb613d6342278b195c79a7931f 0000000000 a1a86d04121eb87f027c66f53303c28e5739f943fc84b38ad6af009035dd9457 0000000000 ad7dd58d03aedb22a30b5084394920ce12230c2d8017ad9b81ab04079bdd026b 0000000000 bc4d809b15189d78db3e1d8cf4f9726a795da1643ca5f1358e1ddb0edc0d7eb3 0000000000 c0a6f4dc63a24bfdcf54ef2a6a082a0a72de35803e2ff5ff527ae5d87206dfd5 0000000000 c3846bf24b9e93ca64274c0ec67c1ecc5e024ffcacd2d74019350e81fe546ae4 0000000000 c7796beb62c101bb143d262a7c96a0c6168183223ef50d699632d86e03b8cc9b 0000000000 d02a0f994a868c66395f2e7a880df509bd0c29c96de16015a0fd501eda4f96a9 0000000000 d5917a7791eb7cf20a2e57eb98284a67b28a57e89182da53d546678c9fde2b4f 0000000000 d7a7a0fb5d7e2731d771e9484ebcdef71d5f0c3e0a2948782bc83ee0ea699ef4 0000000000 d8e0febc1db2e38d00940f37d27d41344d993e734b99d5656d9778d4d8143624 0000000000 d92c171f5cf890ba428019292927fe22f3207fd2b54449cb6f675af4922146e2 0000000000 d9a32485a8cca85539cef12fffff711378a17851d73da2732ab4302d763bd62b 0000000000 db3517d1f6732a2d5ab97c533ec70779ee3270a62fb4ac4238372460e6f01e88 0000000000 dba84dd7ef622d485463a90137ea4d574df8550928f6afa03b4d8b1141e636cc 0000000000 e38655f4b0190c84d3b3893d840a687e190a256d98052f159e6d4a39f589a6eb 0000000000 e3b6a2db2ed7ce48842f7ac53241c7b71d54144bfb40c11f3f1d0b42f5eea12d 0000000000 e8e8176536a60cc2c4e10187c3befca20ef263497018f566d5bea0f94d0c111b 0000000000 ebd41040e4bb3ec742c9e381d31ef2a41a48b6685c96e7cef3c1df6cd4331c99 0000000000 f356bea244b7a91eb35d53ca9ad7864ace018e2d35d5f8f96ddf68a6f41aa474 0000000000 "
128
129note ---------------------------------------------------------------- DER reader
130
131note der_hdr: the TLV header at d[p], whose content must end by e (e <= len d). Single-byte tags
132note only; definite lengths of at most 3 length bytes, minimal (short form below 128, no leading 0).
133note h[0] tag, h[1] content start, h[2] content end. 0 ok, 1 malformed. Never traps for len h >= 3.
134fn der_hdr d view uchar * p ulong e ulong h edit ulong * to ulong
135if gt e len d
136ret 1
137end
138if ge p e
139ret 1
140end
141if lt sub e p 2
142ret 1
143end
144t = ulong get d p
145if eq and t 31 31
146ret 1
147end
148b = ulong get d add p 1
149q = add p 2
150n = b
151if ge b 128
152k = and b 127
153if or eq k 0 gt k 3
154ret 1
155end
156if gt k sub e q
157ret 1
158end
159if eq get d q 0
160ret 1
161end
162n = 0
163for i ulong k
164n = or shl n 8 ulong get d add q i
165end
166q = add q k
167if lt n 128
168ret 1
169end
170end
171if gt n sub e q
172ret 1
173end
174put h 0 t
175put h 1 q
176put h 2 add q n
177ret 0
178end
179
180note der_same: byte equality of two spans (lengths must match)
181fn der_same a view uchar * b view uchar * to bool
182if ne len a len b
183ret false
184end
185for i ulong len a
186if ne get a i get b i
187ret false
188end
189end
190ret true
191end
192
193note der_eqat: d[a .. a+n) equals d[b .. b+n); false when either span leaves d
194fn der_eqat d view uchar * a ulong b ulong n ulong to bool
195if gt n len d
196ret false
197end
198if or gt a sub len d n gt b sub len d n
199ret false
200end
201for i ulong n
202if ne get d add a i get d add b i
203ret false
204end
205end
206ret true
207end
208
209note der_hexv: value of a lowercase hex digit (16 when not one)
210fn der_hexv c ulong to ulong
211if and ge c 48 le c 57
212ret sub c 48
213end
214if and ge c 97 le c 102
215ret sub c 87
216end
217ret 16
218end
219
220note der_hexeq: d equals the bytes written in hex by t (len t = 2 len d)
221fn der_hexeq d view uchar * t view uchar * to bool
222if ne len t mul len d 2
223ret false
224end
225for i ulong len d
226hi = der_hexv ulong get t mul i 2
227lo = der_hexv ulong get t add mul i 2 1
228if ne ulong get d i or shl hi 4 lo
229ret false
230end
231end
232ret true
233end
234
235note der_oidix: position (1-based) of the OID content d in X5OIDS, 0 when absent
236fn der_oidix d view uchar * to ulong
237s = ulong 0
238k = ulong 1
239n = len X5OIDS
240while lt s n bound 1000
241j = s
242while lt j n bound 1000
243if eq get X5OIDS j 32
244break
245end
246j = add j 1
247end
248if der_hexeq d part X5OIDS s sub j s
249ret k
250end
251s = add j 1
252k = add k 1
253end
254ret 0
255end
256
257note der_small: value of the INTEGER TLV that is exactly a; nonnegative, minimal, at most 7 content
258note bytes. X5BIG when it is not one.
259fn der_small a view uchar * to ulong
260h = new ulong 3 0
261if ne der_hdr a 0 len a h 0
262ret X5BIG
263end
264if or ne get h 0 2 ne get h 2 len a
265ret X5BIG
266end
267s = get h 1
268n = sub len a s
269if or eq n 0 gt n 7
270ret X5BIG
271end
272b = ulong get a s
273if ge b 128
274ret X5BIG
275end
276if and gt n 1 eq b 0
277if lt ulong get a add s 1 128
278ret X5BIG
279end
280end
281v = ulong 0
282for i ulong n
283v = or shl v 8 ulong get a add s i
284end
285ret v
286end
287
288note der_null: c[q .. e) is exactly a NULL (05 00)
289fn der_null c view uchar * q ulong e ulong to bool
290if ne sub e q 2
291ret false
292end
293if or ne get c q 5 ne get c add q 1 0
294ret false
295end
296ret true
297end
298
299note der_u24: big-endian 24-bit value at a[p] (caller checks p + 3 <= len a)
300fn der_u24 a view uchar * p ulong to ulong
301v = shl ulong get a p 16
302v = or v shl ulong get a add p 1 8
303ret or v ulong get a add p 2
304end
305
306note der_d2: two decimal digits at t[i], 100 when either is not a digit
307fn der_d2 t view uchar * i ulong to ulong
308a = ulong get t i
309b = ulong get t add i 1
310if or lt a 48 gt a 57
311ret 100
312end
313if or lt b 48 gt b 57
314ret 100
315end
316ret add mul sub a 48 10 sub b 48
317end
318
319note x509_days: days from 0000-03-01 to y-m-d (proleptic Gregorian, H. Hinnant's days_from_civil
320note without the 1970 shift; y >= 1, m 1..12)
321fn x509_days y ulong m ulong d ulong to ulong
322yy = y
323if le m 2
324yy = sub y 1
325end
326era = div yy 400
327yoe = sub yy mul era 400
328mp = add m 9
329if gt m 2
330mp = sub m 3
331end
332doy = div add mul mp 153 2 5
333doy = add doy sub d 1
334doe = add mul yoe 365 div yoe 4
335doe = add sub doe div yoe 100 doy
336ret add mul era 146097 doe
337end
338
339note x509_dim: days in month m of year y
340fn x509_dim y ulong m ulong to ulong
341if eq m 2
342if and eq rem y 4 0 or ne rem y 100 0 eq rem y 400 0
343ret 29
344end
345ret 28
346end
347if or or eq m 4 eq m 6 or eq m 9 eq m 11
348ret 30
349end
350ret 31
351end
352
353note der_time: the Time TLV that is exactly t, as seconds since 0000-03-01T00:00:00Z, X5BIG when bad.
354note UTCTime YYMMDDHHMMSSZ (RFC 5280 4.1.2.5.1: YY >= 50 is 19YY, else 20YY);
355note GeneralizedTime YYYYMMDDHHMMSSZ (4.1.2.5.2). No fractions, no offsets, seconds 00..59.
356fn der_time t view uchar * to ulong
357h = new ulong 3 0
358if ne der_hdr t 0 len t h 0
359ret X5BIG
360end
361g = get h 0
362s = get h 1
363n = sub get h 2 s
364if ne get h 2 len t
365ret X5BIG
366end
367y = ulong 0
368if eq g 23
369if ne n 13
370ret X5BIG
371end
372y = der_d2 t s
373if gt y 99
374ret X5BIG
375end
376y = add y 1900
377if lt y 1950
378y = add y 100
379end
380s = add s 2
381else
382if or ne g 24 ne n 15
383ret X5BIG
384end
385a = der_d2 t s
386b = der_d2 t add s 2
387if or gt a 99 gt b 99
388ret X5BIG
389end
390y = add mul a 100 b
391if eq y 0
392ret X5BIG
393end
394s = add s 4
395end
396if ne get t add s 10 90
397ret X5BIG
398end
399ret x509_hms t s y
400end
401
402note x509_hms: MMDDHHMMSS at t[s] of year y, as seconds (see der_time)
403fn x509_hms t view uchar * s ulong y ulong to ulong
404hh = der_d2 t add s 4
405mi = der_d2 t add s 6
406se = der_d2 t add s 8
407if or gt hh 23 or gt mi 59 gt se 59
408ret X5BIG
409end
410sec = add add mul hh 3600 mul mi 60 se
411mo = der_d2 t s
412dd = der_d2 t add s 2
413if or lt mo 1 gt mo 12
414ret X5BIG
415end
416if or lt dd 1 gt dd x509_dim y mo
417ret X5BIG
418end
419dy = x509_days y mo dd
420ret add mul dy 86400 sec
421end
422
423note ---------------------------------------------------------------- algorithm identifiers
424
425note x509_hashix: the AlgorithmIdentifier TLV that is exactly a, for sha256/384/512 with absent or NULL
426note parameters: 256, 384 or 512; else 0
427fn x509_hashix a view uchar * to ulong
428h = new ulong 3 0
429if ne der_hdr a 0 len a h 0
430ret 0
431end
432if or ne get h 0 0x30 ne get h 2 len a
433ret 0
434end
435e = get h 2
436if ne der_hdr a get h 1 e h 0
437ret 0
438end
439if ne get h 0 6
440ret 0
441end
442o = der_oidix part a get h 1 sub get h 2 get h 1
443q = get h 2
444if ne q e
445if not der_null a q e
446ret 0
447end
448end
449if or lt o 12 gt o 14
450ret 0
451end
452ret add 256 mul sub o 12 128
453end
454
455note x509_mgf: the [1] content of RSASSA-PSS-params: AlgorithmIdentifier {id-mgf1, hash}; the hash
456note (256/384/512) or 0
457fn x509_mgf a view uchar * to ulong
458h = new ulong 3 0
459if ne der_hdr a 0 len a h 0
460ret 0
461end
462if or ne get h 0 0x30 ne get h 2 len a
463ret 0
464end
465e = get h 2
466if ne der_hdr a get h 1 e h 0
467ret 0
468end
469if ne get h 0 6
470ret 0
471end
472if ne der_oidix part a get h 1 sub get h 2 get h 1 15
473ret 0
474end
475q = get h 2
476ret x509_hashix part a q sub e q
477end
478
479note x509_pss: RSASSA-PSS-params TLV that is exactly p (RFC 4055 3.1). Allowed: hashAlgorithm
480note sha256/384/512 present, maskGenAlgorithm MGF1 with the same hash, saltLength = hash length,
481note trailerField absent. Returns sigalg 4/5/6, else 0 (unsupported).
482fn x509_pss p view uchar * to ulong
483h = new ulong 3 0
484if ne der_hdr p 0 len p h 0
485ret 0
486end
487if or ne get h 0 0x30 ne get h 2 len p
488ret 0
489end
490e = get h 2
491if ne der_hdr p get h 1 e h 0
492ret 0
493end
494if ne get h 0 0xa0
495ret 0
496end
497hb = x509_hashix part p get h 1 sub get h 2 get h 1
498if ne der_hdr p get h 2 e h 0
499ret 0
500end
501if ne get h 0 0xa1
502ret 0
503end
504mg = x509_mgf part p get h 1 sub get h 2 get h 1
505if ne der_hdr p get h 2 e h 0
506ret 0
507end
508if ne get h 0 0xa2
509ret 0
510end
511sl = der_small part p get h 1 sub get h 2 get h 1
512if ne get h 2 e
513ret 0
514end
515if or eq hb 0 ne mg hb
516ret 0
517end
518if ne sl div hb 8
519ret 0
520end
521ret add 4 div sub hb 256 128
522end
523
524note x509_algid: signature AlgorithmIdentifier TLV that is exactly a -> sigalg code:
525note 1-3 sha256/384/512WithRSAEncryption (parameters NULL or absent, RFC 4055 5), 4-6 RSASSA-PSS
526note SHA-256/384/512 (x509_pss), 7-9 ecdsa-with-SHA256/384/512 (parameters absent, RFC 5758 3.2);
527note 0 for anything else (SHA-1, MD5, Ed25519, malformed parameters...)
528fn x509_algid a view uchar * to ulong
529h = new ulong 3 0
530if ne der_hdr a 0 len a h 0
531ret 0
532end
533if or ne get h 0 0x30 ne get h 2 len a
534ret 0
535end
536e = get h 2
537if ne der_hdr a get h 1 e h 0
538ret 0
539end
540if ne get h 0 6
541ret 0
542end
543o = der_oidix part a get h 1 sub get h 2 get h 1
544q = get h 2
545if and ge o 1 le o 3
546if eq q e
547ret o
548end
549if der_null a q e
550ret o
551end
552ret 0
553end
554if eq o 4
555if eq q e
556ret 0
557end
558ret x509_pss part a q sub e q
559end
560if and ge o 5 le o 7
561if eq q e
562ret add o 2
563end
564end
565ret 0
566end
567
568note ---------------------------------------------------------------- certificate parser
569
570note x509_rsakey: the RSAPublicKey TLV that is exactly k (RFC 8017 A.1.1) into q (len >= 4): q[0], q[1]
571note the offset and length of the modulus magnitude (sign octet removed), q[2], q[3] the offset and
572note length of the public exponent content. 1, or 0 when malformed (non-minimal or non-positive
573note integers, trailing bytes)
574fn x509_rsakey k view uchar * q edit ulong * to ulong
575h = new ulong 3 0
576if ne der_hdr k 0 len k h 0
577ret 0
578end
579if or ne get h 0 0x30 ne get h 2 len k
580ret 0
581end
582e = get h 2
583if ne der_hdr k get h 1 e h 0
584ret 0
585end
586if ne get h 0 2
587ret 0
588end
589s = get h 1
590n = sub get h 2 s
591if ne x509_posint part k s n 0
592ret 0
593end
594if ne der_hdr k get h 2 e h 0
595ret 0
596end
597if or ne get h 0 2 ne get h 2 e
598ret 0
599end
600if ne x509_posint part k get h 1 sub e get h 1 0
601ret 0
602end
603put q 2 get h 1
604put q 3 sub e get h 1
605if eq get k s 0
606s = add s 1
607n = sub n 1
608end
609put q 0 s
610put q 1 n
611ret 1
612end
613
614note x509_rsabits: the modulus bit count of the RSAPublicKey k (x509_rsakey), 0 when malformed. It is
615note the size only (the record keeps it in X5RBITS); the exponent policy is x509_rsaeok, which parses
616note the key bytes again when needed, so no exponent can change what a minimum-size check (x509_rsaok,
617note chain_sig for anchors and revocation signers) reads.
618fn x509_rsabits k view uchar * to ulong
619q = new ulong 4 0
620if eq x509_rsakey k edit q 0
621ret 0
622end
623b = ulong get k get q 0
624bits = mul sub get q 1 1 8
625while ne b 0 bound 9
626bits = add bits 1
627b = shr b 1
628end
629ret bits
630end
631
632note x509_rsaeok: 1 when the public exponent of the RSAPublicKey k is within x509_rsaexp, 0 when it is
633note not or k is malformed
634fn x509_rsaeok k view uchar * to ulong
635q = new ulong 4 0
636if eq x509_rsakey k edit q 0
637ret 0
638end
639ret x509_rsaexp part k get q 2 get q 3
640end
641
642note x509_rsaexp: 1 when the public exponent content x (minimal, positive) is odd and in 3 .. 2^32-1
643fn x509_rsaexp x view uchar * to ulong
644n = len x
645if eq n 0
646ret 0
647end
648s = ulong 0
649if eq get x 0 0
650s = 1
651end
652if gt sub n s 4
653ret 0
654end
655v = ulong 0
656for i ulong sub n s
657v = or shl v 8 ulong get x add s i
658end
659if or lt v 3 eq and v 1 0
660ret 0
661end
662ret 1
663end
664
665note x509_rsaok: 1 when the RSA modulus bit count b (X5RBITS) is in 2048 .. 4096: the size part of the
666note policy for keys presented by the server (x509_rsapres); anchors only need >= 2048 bits
667note (chain_sig). The modulus size must also be a multiple of 8 bits (Mozilla Root Store Policy 5.1).
668fn x509_rsaok b ulong to ulong
669if or lt b 2048 gt b 4096
670ret 0
671end
672if ne and b 7 0
673ret 0
674end
675ret 1
676end
677
678note x509_rsapres: 1 when the RSA key of the parsed certificate c (record r) is within the policy for
679note keys presented by the server: the size from the record (x509_rsaok) and the exponent parsed again
680note (x509_rsaeok on the key bytes c[r[X5RKO] .. +r[X5RKL]))
681fn x509_rsapres c view uchar * r view ulong * to ulong
682if eq x509_rsaok get r X5RBITS 0
683ret 0
684end
685ret x509_rsaeok part c get r X5RKO get r X5RKL
686end
687
688note x509_posint: INTEGER content c is minimal and positive (0 ok, 1 not)
689fn x509_posint c view uchar * to ulong
690n = len c
691if eq n 0
692ret 1
693end
694b = ulong get c 0
695if ge b 128
696ret 1
697end
698if eq b 0
699if eq n 1
700ret 1
701end
702if lt ulong get c 1 128
703ret 1
704end
705end
706ret 0
707end
708
709note x509_spki: SubjectPublicKeyInfo TLV that is exactly c[a .. b). Key types: 1 rsaEncryption
710note (parameters NULL; key = RSAPublicKey DER, bits recorded), 2 id-ecPublicKey namedCurve
711note prime256v1 (uncompressed point, 65 bytes), 3 secp384r1 (97 bytes); 0 = any other key (refused
712note when used). 0 ok, 1 malformed.
713fn x509_spki c view uchar * a ulong b ulong r edit ulong * to ulong
714h = new ulong 3 0
715if ne der_hdr c a b h 0
716ret 1
717end
718if or ne get h 0 0x30 ne get h 2 b
719ret 1
720end
721if ne der_hdr c get h 1 b h 0
722ret 1
723end
724if ne get h 0 0x30
725ret 1
726end
727s = get h 2
728kt = x509_keyalg part c get h 1 sub get h 2 get h 1
729if eq kt 9
730ret 1
731end
732if ne der_hdr c s b h 0
733ret 1
734end
735if or ne get h 0 3 ne get h 2 b
736ret 1
737end
738q = get h 1
739if or ge q b ne get c q 0
740ret 1
741end
742q = add q 1
743put r X5RKO q
744put r X5RKL sub b q
745put r X5RKT kt
746ret x509_keyset c q b r
747end
748
749note x509_keyalg: SPKI AlgorithmIdentifier content a -> key type 1 (rsaEncryption, NULL), 2 (EC
750note prime256v1), 3 (EC secp384r1), 0 other, 9 malformed
751fn x509_keyalg a view uchar * to ulong
752h = new ulong 3 0
753e = len a
754if ne der_hdr a 0 e h 0
755ret 9
756end
757if ne get h 0 6
758ret 9
759end
760o = der_oidix part a get h 1 sub get h 2 get h 1
761q = get h 2
762if eq o 8
763if der_null a q e
764ret 1
765end
766ret 0
767end
768if ne o 9
769ret 0
770end
771if ne der_hdr a q e h 0
772ret 9
773end
774if or ne get h 0 6 ne get h 2 e
775ret 0
776end
777p = der_oidix part a get h 1 sub get h 2 get h 1
778if eq p 10
779ret 2
780end
781if eq p 11
782ret 3
783end
784ret 0
785end
786
787note x509_keyset: check the key bytes c[q .. b) for the key type in r[X5RKT] (see x509_spki); a
788note bad EC point becomes key type 0; 0 ok, 1 malformed RSAPublicKey
789fn x509_keyset c view uchar * q ulong b ulong r edit ulong * to ulong
790kt = get r X5RKT
791n = sub b q
792if eq kt 1
793bits = x509_rsabits part c q n
794if eq bits 0
795ret 1
796end
797put r X5RBITS bits
798end
799if eq kt 2
800if or ne n 65 ne get c q 4
801put r X5RKT 0
802ret 0
803end
804end
805if eq kt 3
806if or ne n 97 ne get c q 4
807put r X5RKT 0
808ret 0
809end
810end
811put r X5RKT kt
812ret 0
813end
814
815note x509_bc: basicConstraints extnValue content c[a .. b): SEQUENCE {cA BOOLEAN DEFAULT FALSE,
816note pathLenConstraint INTEGER (0..MAX) OPTIONAL}. An explicit FALSE is tolerated (BER default).
817fn x509_bc c view uchar * a ulong b ulong r edit ulong * to ulong
818h = new ulong 3 0
819if ne der_hdr c a b h 0
820ret X5EMAL
821end
822if or ne get h 0 0x30 ne get h 2 b
823ret X5EMAL
824end
825s = get h 1
826put r X5RBC 1
827if lt s b
828if ne der_hdr c s b h 0
829ret X5EMAL
830end
831if eq get h 0 1
832if ne sub get h 2 get h 1 1
833ret X5EMAL
834end
835v = ulong get c get h 1
836if eq v 255
837put r X5RCA 1
838else
839if ne v 0
840ret X5EMAL
841end
842end
843s = get h 2
844end
845end
846if lt s b
847if ne der_hdr c s b h 0
848ret X5EMAL
849end
850pl = der_small part c s sub get h 2 s
851if eq pl X5BIG
852ret X5EMAL
853end
854put r X5RPL pl
855s = get h 2
856end
857if ne s b
858ret X5EMAL
859end
860ret 0
861end
862
863note x509_ku: keyUsage extnValue content c[a .. b): BIT STRING, at least one content byte, unused
864note bits 0..7 and zero. The first content byte is recorded (0x80 digitalSignature, 0x04 keyCertSign).
865fn x509_ku c view uchar * a ulong b ulong r edit ulong * to ulong
866h = new ulong 3 0
867if ne der_hdr c a b h 0
868ret X5EMAL
869end
870if or ne get h 0 3 ne get h 2 b
871ret X5EMAL
872end
873s = get h 1
874if lt sub b s 2
875ret X5EMAL
876end
877note I5c: 9 named bits fit in 2 octets after the unused-bits octet; the second only holds decipherOnly
878if gt sub b s 3
879ret X5EMAL
880end
881if eq sub b s 3
882if ne and get c add s 2 127 0
883ret X5EMAL
884end
885end
886u = ulong get c s
887if gt u 7
888ret X5EMAL
889end
890z = ulong get c sub b 1
891if ne and z sub shl ulong 1 u 1 0
892ret X5EMAL
893end
894put r X5RKU 1
895put r X5RKUB ulong get c add s 1
896ret 0
897end
898
899note x509_eku: extKeyUsage extnValue content c[a .. b): SEQUENCE SIZE (1..MAX) OF OBJECT IDENTIFIER
900fn x509_eku c view uchar * a ulong b ulong r edit ulong * to ulong
901h = new ulong 3 0
902if ne der_hdr c a b h 0
903ret X5EMAL
904end
905if or ne get h 0 0x30 ne get h 2 b
906ret X5EMAL
907end
908s = get h 1
909if eq s b
910ret X5EMAL
911end
912n = ulong 0
913while lt s b bound 10000
914n = add n 1
915if gt n X5NMAX
916ret X5EMAL
917end
918if ne der_hdr c s b h 0
919ret X5EMAL
920end
921if ne get h 0 6
922ret X5EMAL
923end
924o = der_oidix part c get h 1 sub get h 2 get h 1
925if eq o 21
926put r X5RSRV 1
927end
928if eq o 22
929put r X5RANY 1
930end
931s = get h 2
932end
933put r X5REKU 1
934ret 0
935end
936
937note x509_san: subjectAltName extnValue content c[a .. b): SEQUENCE SIZE (1..MAX) OF GeneralName,
938note each a context-specific TLV; the GeneralNames content span is recorded
939fn x509_san c view uchar * a ulong b ulong r edit ulong * to ulong
940h = new ulong 3 0
941if ne der_hdr c a b h 0
942ret X5EMAL
943end
944if or ne get h 0 0x30 ne get h 2 b
945ret X5EMAL
946end
947s = get h 1
948if eq s b
949ret X5EMAL
950end
951put r X5RSNO s
952put r X5RSNL sub b s
953n = ulong 0
954while lt s b bound 10000
955n = add n 1
956if gt n X5NMAX
957ret X5EMAL
958end
959if ne der_hdr c s b h 0
960ret X5EMAL
961end
962if ne and get h 0 0xc0 0x80
963ret X5EMAL
964end
965s = get h 2
966end
967put r X5RSAN 1
968ret 0
969end
970
971note x509_ext: one Extension, content c[a .. b): {extnID, critical BOOLEAN DEFAULT FALSE, extnValue}.
972note Known: basicConstraints, keyUsage, extKeyUsage, subjectAltName, authorityKeyIdentifier,
973note subjectKeyIdentifier (each at most once, RFC 5280 4.2). A nameConstraints extension, critical or
974note not, is X5ENC; any other unknown critical one X5ECRIT (x509_extk).
975fn x509_ext c view uchar * a ulong b ulong r edit ulong * to ulong
976h = new ulong 3 0
977if ne der_hdr c a b h 0
978ret X5EMAL
979end
980if ne get h 0 6
981ret X5EMAL
982end
983o = der_oidix part c get h 1 sub get h 2 get h 1
984if ne der_hdr c get h 2 b h 0
985ret X5EMAL
986end
987cr = ulong 0
988if eq get h 0 1
989if ne sub get h 2 get h 1 1
990ret X5EMAL
991end
992v = ulong get c get h 1
993if eq v 255
994cr = 1
995else
996if ne v 0
997ret X5EMAL
998end
999end
1000if ne der_hdr c get h 2 b h 0
1001ret X5EMAL
1002end
1003end
1004if or ne get h 0 4 ne get h 2 b
1005ret X5EMAL
1006end
1007k = x509_extk o cr r
1008if ne k 0
1009if eq k 1
1010ret 0
1011end
1012ret k
1013end
1014s = get h 1
1015if eq o 16
1016ret x509_bc c s b r
1017end
1018if eq o 17
1019ret x509_ku c s b r
1020end
1021if eq o 18
1022ret x509_eku c s b r
1023end
1024ret x509_san c s b r
1025end
1026
1027note x509_extk: classify extension o (der_oidix) with critical flag cr; 0 when its content must be
1028note parsed (16..19), 1 when done (an unknown non-critical one; AKI/SKI, recorded by presence only),
1029note else a reject code. X5RSEEN bit (o - 16) marks each known extension (a repeat is X5EMAL); bit 9
1030note (512) a critical subjectAltName. AKI/SKI MUST be non-critical (RFC 5280 4.2.1.1, 4.2.1.2).
1031fn x509_extk o ulong cr ulong r edit ulong * to ulong
1032if eq o 20
1033ret X5ENC
1034end
1035kn = ulong 0
1036if and ge o 16 le o 19
1037kn = 1
1038end
1039if or eq o 23 eq o 24
1040kn = 2
1041end
1042if eq kn 0
1043if ne cr 0
1044ret X5ECRIT
1045end
1046ret 1
1047end
1048m = shl ulong 1 sub o 16
1049if ne and get r X5RSEEN m 0
1050ret X5EMAL
1051end
1052put r X5RSEEN or get r X5RSEEN m
1053if and eq o 19 ne cr 0
1054put r X5RSEEN or get r X5RSEEN 512
1055end
1056if eq kn 2
1057if ne cr 0
1058ret X5ECRIT
1059end
1060ret 1
1061end
1062ret 0
1063end
1064
1065note x509_exts: [3] content c[a .. b): Extensions ::= SEQUENCE SIZE (1..X5XMAX) OF Extension, no
1066note extnID twice (RFC 5280 4.2, x509_dupx)
1067fn x509_exts c view uchar * a ulong b ulong r edit ulong * to ulong
1068h = new ulong 3 0
1069if ne der_hdr c a b h 0
1070ret X5EMAL
1071end
1072if or ne get h 0 0x30 ne get h 2 b
1073ret X5EMAL
1074end
1075s = get h 1
1076f = s
1077if eq s b
1078ret X5EMAL
1079end
1080n = ulong 0
1081while lt s b bound 10000
1082n = add n 1
1083if gt n X5XMAX
1084ret X5EMAL
1085end
1086if ne der_hdr c s b h 0
1087ret X5EMAL
1088end
1089if ne get h 0 0x30
1090ret X5EMAL
1091end
1092if ne x509_dupx c f s 0
1093ret X5EMAL
1094end
1095v = x509_ext c get h 1 get h 2 r
1096if ne v 0
1097ret v
1098end
1099s = get h 2
1100end
1101ret 0
1102end
1103
1104note x509_dupx: 1 when the extnID of the Extension TLV at c[s] equals the extnID of an earlier
1105note Extension in c[a .. s) (already framed by x509_exts), else 0; 1 also when the TLV is malformed
1106fn x509_dupx c view uchar * a ulong s ulong to ulong
1107h = new ulong 3 0
1108if ne der_hdr c s len c h 0
1109ret 1
1110end
1111if ne der_hdr c get h 1 get h 2 h 0
1112ret 1
1113end
1114o = get h 1
1115l = sub get h 2 o
1116while lt a s bound 10000
1117if ne der_hdr c a s h 0
1118ret 1
1119end
1120e = get h 2
1121if ne der_hdr c get h 1 e h 0
1122ret 1
1123end
1124if x509_same c get h 1 o l sub get h 2 get h 1
1125ret 1
1126end
1127a = e
1128end
1129ret 0
1130end
1131
1132note x509_valid: Validity TLV that is exactly c[a .. b): notBefore, notAfter
1133fn x509_valid c view uchar * a ulong b ulong r edit ulong * to ulong
1134h = new ulong 3 0
1135if ne der_hdr c a b h 0
1136ret 1
1137end
1138if or ne get h 0 0x30 ne get h 2 b
1139ret 1
1140end
1141s = get h 1
1142if ne der_hdr c s b h 0
1143ret 1
1144end
1145nb = der_time part c s sub get h 2 s
1146s = get h 2
1147if ne der_hdr c s b h 0
1148ret 1
1149end
1150if ne get h 2 b
1151ret 1
1152end
1153na = der_time part c s sub b s
1154if or eq nb X5BIG eq na X5BIG
1155ret 1
1156end
1157put r X5RNB nb
1158put r X5RNA na
1159ret 0
1160end
1161
1162note x509_tbs2: tbsCertificate from validity on, c[s .. b): validity, subject, SPKI, optional unique
1163note IDs (v2, v3: at most one issuerUniqueID [1], then at most one subjectUniqueID [2]) and
1164note extensions (v3 only)
1165fn x509_tbs2 c view uchar * s ulong b ulong r edit ulong * to ulong
1166h = new ulong 3 0
1167if ne der_hdr c s b h 0
1168ret X5EMAL
1169end
1170if ne x509_valid c s get h 2 r 0
1171ret X5EMAL
1172end
1173s = get h 2
1174if ne der_hdr c s b h 0
1175ret X5EMAL
1176end
1177if ne get h 0 0x30
1178ret X5EMAL
1179end
1180put r X5RSBO s
1181put r X5RSBL sub get h 2 s
1182s = get h 2
1183if ne der_hdr c s b h 0
1184ret X5EMAL
1185end
1186if ne x509_spki c s get h 2 r 0
1187ret X5EMAL
1188end
1189s = get h 2
1190v = get r X5RVER
1191k = ulong 0x80
1192while lt s b bound 4
1193if ne der_hdr c s b h 0
1194ret X5EMAL
1195end
1196g = get h 0
1197if or eq g 0x81 eq g 0x82
1198if or eq v 0 le g k
1199ret X5EMAL
1200end
1201k = g
1202s = get h 2
1203continue
1204end
1205if or ne g 0xa3 ne v 2
1206ret X5EMAL
1207end
1208if ne get h 2 b
1209ret X5EMAL
1210end
1211ret x509_exts c get h 1 b r
1212end
1213if ne s b
1214ret X5EMAL
1215end
1216ret 0
1217end
1218
1219note x509_tbs: tbsCertificate content c[a .. b): version (explicit [0], 1 or 2 when present),
1220note serialNumber (1..21 content bytes), signature AlgorithmIdentifier, issuer Name, then x509_tbs2
1221fn x509_tbs c view uchar * a ulong b ulong r edit ulong * to ulong
1222h = new ulong 3 0
1223s = a
1224if ne der_hdr c s b h 0
1225ret X5EMAL
1226end
1227if eq get h 0 0xa0
1228v = der_small part c get h 1 sub get h 2 get h 1
1229if or eq v 0 gt v 2
1230ret X5EMAL
1231end
1232put r X5RVER v
1233s = get h 2
1234if ne der_hdr c s b h 0
1235ret X5EMAL
1236end
1237end
1238n = sub get h 2 get h 1
1239if or ne get h 0 2 or eq n 0 gt n 21
1240ret X5EMAL
1241end
1242s = get h 2
1243if ne der_hdr c s b h 0
1244ret X5EMAL
1245end
1246if ne get h 0 0x30
1247ret X5EMAL
1248end
1249put r X5RTAO s
1250put r X5RTAL sub get h 2 s
1251s = get h 2
1252if ne der_hdr c s b h 0
1253ret X5EMAL
1254end
1255if ne get h 0 0x30
1256ret X5EMAL
1257end
1258put r X5RISO s
1259put r X5RISL sub get h 2 s
1260ret x509_tbs2 c get h 2 b r
1261end
1262
1263note x509_parse: the Certificate that is exactly c (RFC 5280 4.1) into the record r (len r >= X5RSZ,
1264note offsets relative to c). 0 ok, else X5EMAL, X5ECRIT or X5EAMIS. An unsupported signature
1265note algorithm or key type is recorded (sigalg 0, key type 0), not refused here.
1266fn x509_parse c view uchar * r edit ulong * to ulong
1267h = new ulong 3 0
1268for i ulong X5RSZ
1269put r i 0
1270end
1271put r X5RPL X5BIG
1272if ne der_hdr c 0 len c h 0
1273ret X5EMAL
1274end
1275if or ne get h 0 0x30 ne get h 2 len c
1276ret X5EMAL
1277end
1278e = len c
1279s = get h 1
1280if ne der_hdr c s e h 0
1281ret X5EMAL
1282end
1283if ne get h 0 0x30
1284ret X5EMAL
1285end
1286put r X5RTBO s
1287put r X5RTBL sub get h 2 s
1288tc = get h 1
1289s = get h 2
1290if ne der_hdr c s e h 0
1291ret X5EMAL
1292end
1293if ne get h 0 0x30
1294ret X5EMAL
1295end
1296put r X5RAO s
1297put r X5RAL sub get h 2 s
1298put r X5RALG x509_algid part c s sub get h 2 s
1299s = get h 2
1300if ne der_hdr c s e h 0
1301ret X5EMAL
1302end
1303if or ne get h 0 3 ne get h 2 e
1304ret X5EMAL
1305end
1306q = get h 1
1307if or ge q e ne get c q 0
1308ret X5EMAL
1309end
1310put r X5RSGO add q 1
1311put r X5RSGL sub sub e q 1
1312v = x509_tbs c tc add get r X5RTBO get r X5RTBL r
1313if ne v 0
1314ret v
1315end
1316if ne get r X5RAL get r X5RTAL
1317ret X5EAMIS
1318end
1319if not der_eqat c get r X5RAO get r X5RTAO get r X5RAL
1320ret X5EAMIS
1321end
1322ret 0
1323end
1324
1325note ---------------------------------------------------------------- PEM trust store
1326
1327note b64_val: value of a base64 character (RFC 4648 4), 64 for '=', 99 for anything else
1328fn b64_val c ulong to ulong
1329if and ge c 65 le c 90
1330ret sub c 65
1331end
1332if and ge c 97 le c 122
1333ret sub c 71
1334end
1335if and ge c 48 le c 57
1336ret add c 4
1337end
1338if eq c 43
1339ret 62
1340end
1341if eq c 47
1342ret 63
1343end
1344if eq c 61
1345ret 64
1346end
1347ret 99
1348end
1349
1350note b64_push: one base64 character c of a certificate block. s: 0 write position, 3 accumulator,
1351note 4 characters in the quartet, 5 '=' seen, 6 block finished by padding. Strict: no character
1352note after padding, '=' only as the last one or two of a quartet, unused bits zero. 0 or -4098.
1353fn b64_push st edit uchar * c ulong s edit ulong * to long
1354v = b64_val c
1355if or eq v 99 eq get s 6 1
1356ret -4098
1357end
1358acc = get s 3
1359if eq v 64
1360if lt get s 4 2
1361ret -4098
1362end
1363put s 5 add get s 5 1
1364acc = shl acc 6
1365else
1366if gt get s 5 0
1367ret -4098
1368end
1369acc = or shl acc 6 v
1370end
1371put s 3 acc
1372put s 4 add get s 4 1
1373if lt get s 4 4
1374ret 0
1375end
1376pad = get s 5
1377w = get s 0
1378if and eq pad 1 ne and acc 255 0
1379ret -4098
1380end
1381if and eq pad 2 ne and acc 65535 0
1382ret -4098
1383end
1384put st w uchar shr acc 16
1385if lt pad 2
1386put st add w 1 uchar and shr acc 8 255
1387end
1388if lt pad 1
1389put st add w 2 uchar and acc 255
1390end
1391put s 0 add w sub 3 pad
1392put s 3 0
1393put s 4 0
1394if gt pad 0
1395put s 6 1
1396end
1397ret 0
1398end
1399
1400note pem_end: close the block: complete quartets, one SEQUENCE TLV of exactly the decoded length,
1401note then the 3-byte big-endian length is written in front of it
1402fn pem_end st edit uchar * s edit ulong * to long
1403h = new ulong 3 0
1404if ne get s 4 0
1405ret -4098
1406end
1407o = add get s 2 3
1408n = sub get s 0 o
1409if or eq n 0 ge n 16777216
1410ret -4099
1411end
1412if ne der_hdr view part st o n 0 n h 0
1413ret -4099
1414end
1415if or ne get h 0 0x30 ne get h 2 n
1416ret -4099
1417end
1418w = get s 2
1419put st w uchar shr n 16
1420put st add w 1 uchar and shr n 8 255
1421put st add w 2 uchar and n 255
1422put s 1 0
1423ret 0
1424end
1425
1426note pem_line: one line st[a .. b) (no line end). Outside a block only the exact BEGIN CERTIFICATE
1427note line matters (other lines are ignored); inside, the exact END line or base64 characters only.
1428fn pem_line st edit uchar * a ulong b ulong s edit ulong * to long
1429if eq get s 1 0
1430if der_same view part st a sub b a X5BEG
1431put s 1 1
1432put s 2 get s 0
1433put s 0 add get s 0 3
1434put s 3 0
1435put s 4 0
1436put s 5 0
1437put s 6 0
1438end
1439ret 0
1440end
1441if der_same view part st a sub b a X5END
1442ret pem_end st s
1443end
1444for i ulong sub b a
1445r = b64_push st ulong get st add a i s
1446if ne r 0
1447ret r
1448end
1449end
1450ret 0
1451end
1452
1453note pem_decode: decode the PEM text st[0 .. n) in place into the store format (repeated 3-byte length
1454note || certificate DER; a zero length or the array end terminates; 3 zero bytes are written after
1455note the last entry when they fit), return the
1456note bytes used. In place is safe: the write position stays below the read position (each block
1457note consumes its 28-byte BEGIN line before 3 header bytes are written, then 4 characters per <= 3
1458note bytes). Codes: -4098 bad base64 or framing, -4099 a block that is not one DER SEQUENCE, -4100
1459note n > len st. Lines end with LF; one CR before the LF is dropped.
1460fn pem_decode st edit uchar * n ulong to long
1461s = new ulong 8 0
1462if gt n len st
1463ret -4100
1464end
1465a = ulong 0
1466while lt a n bound 999999
1467e = a
1468while lt e n bound 999999
1469if eq get st e 10
1470break
1471end
1472e = add e 1
1473end
1474b = e
1475if gt b a
1476if eq get st sub b 1 13
1477b = sub b 1
1478end
1479end
1480r = pem_line st a b s
1481if ne r 0
1482ret r
1483end
1484a = add e 1
1485end
1486if ne get s 1 0
1487ret -4098
1488end
1489w = get s 0
1490if le add w 3 len st
1491put st w 0
1492put st add w 1 0
1493put st add w 2 0
1494end
1495ret long w
1496end
1497
1498note x509_store_loadf: read the PEM bundle at path into st and decode it (pem_decode). Returns the
1499note bytes used, -errno for open/read errors, -4097 when the file does not fit in st, or a
1500note pem_decode code.
1501fn x509_store_loadf st edit uchar * path view uchar * to long
1502one = new uchar 1 0
1503fd = fopen path 0 len path 524288 0
1504if lt fd 0
1505ret fd
1506end
1507n = ulong 0
1508z = long 0
1509while lt n len st bound 999999
1510r = read int fd st n sub len st n
1511if lt r 0
1512z = fclose int fd
1513ret r
1514end
1515if eq r 0
1516break
1517end
1518n = add n ulong r
1519end
1520if eq n len st
1521r = read int fd one 0 1
1522if ne r 0
1523z = fclose int fd
1524ret -4097
1525end
1526end
1527z = fclose int fd
1528ret pem_decode st n
1529end
1530
1531note x509_store_load: the system bundle /etc/ssl/certs/ca-certificates.crt (see x509_store_loadf)
1532fn x509_store_load st edit uchar * to long
1533ret x509_store_loadf st X5PATH
1534end
1535
1536note x509_errname: ASCII name of a reject code into o (truncated to len o); its length, 0 if unknown
1537fn x509_errname code ulong o edit uchar * to ulong
1538s = ulong 0
1539k = ulong 0
1540n = len X5NAMES
1541while lt s n bound 100
1542j = s
1543while lt j n bound 100
1544if eq get X5NAMES j 32
1545break
1546end
1547j = add j 1
1548end
1549if eq k code
1550m = sub j s
1551for i ulong m
1552if lt i len o
1553put o i get X5NAMES add s i
1554end
1555end
1556ret m
1557end
1558s = add j 1
1559k = add k 1
1560end
1561ret 0
1562end
1563
1564note ---------------------------------------------------------------- chain validation
1565
1566note chain_index: the TLS 1.3 certificate_list contents cl (RFC 8446 4.4.2: uint24 length, DER,
1567note uint16 extensions length, extensions) into ix (offset, length pairs). Entry count, or 0 when the
1568note framing is bad, an entry is empty, or there are more than len ix / 2 entries.
1569fn chain_index cl view uchar * ix edit ulong * to ulong
1570p = ulong 0
1571n = ulong 0
1572e = len cl
1573while lt p e bound 100000
1574if lt sub e p 3
1575ret 0
1576end
1577l = der_u24 cl p
1578p = add p 3
1579if or eq l 0 gt l sub e p
1580ret 0
1581end
1582if ge n div len ix 2
1583ret 0
1584end
1585put ix mul n 2 p
1586put ix add mul n 2 1 l
1587p = add p l
1588if lt sub e p 2
1589ret 0
1590end
1591x = or shl ulong get cl p 8 ulong get cl add p 1
1592p = add p 2
1593if gt x sub e p
1594ret 0
1595end
1596p = add p x
1597n = add n 1
1598end
1599ret n
1600end
1601
1602note chain_hostok: host must be an LDH name: 1..253 bytes, labels 1..63 of letters, digits and '-',
1603note no empty label, no trailing dot. All digits and dots, or any ':', is an IP literal (X5EIP).
1604fn chain_hostok h view uchar * to ulong
1605n = len h
1606if or eq n 0 gt n 253
1607ret X5ENAME
1608end
1609dig = ulong 1
1610lab = ulong 0
1611for i ulong n
1612c = ulong get h i
1613if eq c 58
1614ret X5EIP
1615end
1616if eq c 46
1617if eq lab 0
1618ret X5ENAME
1619end
1620lab = 0
1621continue
1622end
1623if or lt c 48 gt c 57
1624dig = 0
1625lc = chain_low c
1626if and or lt lc 97 gt lc 122 ne c 45
1627ret X5ENAME
1628end
1629end
1630lab = add lab 1
1631if gt lab 63
1632ret X5ENAME
1633end
1634end
1635if eq dig 1
1636ret X5EIP
1637end
1638if eq lab 0
1639ret X5ENAME
1640end
1641ret 0
1642end
1643
1644note chain_low: ASCII lower case
1645fn chain_low c ulong to ulong
1646if and ge c 65 le c 90
1647ret add c 32
1648end
1649ret c
1650end
1651
1652note chain_ieq: equal spans under ASCII case folding
1653fn chain_ieq a view uchar * b view uchar * to bool
1654if ne len a len b
1655ret false
1656end
1657for i ulong len a
1658if ne chain_low ulong get a i chain_low ulong get b i
1659ret false
1660end
1661end
1662ret true
1663end
1664
1665note chain_match: SAN dNSName pattern p against host h (RFC 6125 6.4). Exact match ignoring ASCII
1666note case; or p = "*." rest where rest has >= 2 non-empty labels and no '*': the '*' stands for the
1667note whole leftmost label of h (one non-empty label, never several, never the bare parent).
1668note Partial wildcards ("f*.example.com") never match.
1669fn chain_match p view uchar * h view uchar * to bool
1670np = len p
1671nh = len h
1672if eq np 0
1673ret false
1674end
1675if ne get p 0 42
1676ret chain_ieq p h
1677end
1678if lt np 5
1679ret false
1680end
1681if ne get p 1 46
1682ret false
1683end
1684dots = ulong 0
1685lab = ulong 0
1686for i ulong sub np 2
1687c = get p add i 2
1688if eq c 42
1689ret false
1690end
1691if eq c 46
1692if eq lab 0
1693ret false
1694end
1695dots = add dots 1
1696lab = 0
1697else
1698lab = add lab 1
1699end
1700end
1701if or eq lab 0 eq dots 0
1702ret false
1703end
1704k = ulong 0
1705while lt k nh bound 300
1706if eq get h k 46
1707break
1708end
1709k = add k 1
1710end
1711if or eq k 0 ge k nh
1712ret false
1713end
1714ret chain_ieq part p 1 sub np 1 part h k sub nh k
1715end
1716
1717note chain_host: host h against the SAN dNSNames of the certificate c (record r). No SAN: X5ENSAN
1718note (no subject CN fallback); no matching dNSName: X5ENAME.
1719fn chain_host c view uchar * r view ulong * h view uchar * to ulong
1720hd = new ulong 3 0
1721if eq get r X5RSAN 0
1722ret X5ENSAN
1723end
1724s = get r X5RSNO
1725e = add s get r X5RSNL
1726n = ulong 0
1727while lt s e bound 10000
1728n = add n 1
1729if gt n X5NMAX
1730ret X5EMAL
1731end
1732if ne der_hdr c s e hd 0
1733ret X5EMAL
1734end
1735if eq get hd 0 0x82
1736if chain_match part c get hd 1 sub get hd 2 get hd 1 h
1737ret 0
1738end
1739end
1740s = get hd 2
1741end
1742ret X5ENAME
1743end
1744
1745note chain_time: validity of record r at nw (seconds since 0000-03-01, as der_time): valid iff
1746note notBefore <= nw < notAfter. RFC 5280 4.1.2.5 includes notAfter; OpenSSL does not, and the
1747note stricter bound is kept so that this client never accepts what OpenSSL refuses.
1748fn chain_time r view ulong * nw ulong to ulong
1749if gt get r X5RNB nw
1750ret X5ENYV
1751end
1752if le get r X5RNA nw
1753ret X5EEXP
1754end
1755ret 0
1756end
1757
1758note chain_hash: the hash (256/384/512) of sigalg a (1..9)
1759fn chain_hash a ulong to ulong
1760ret add 256 mul rem sub a 1 3 128
1761end
1762
1763note chain_sig: verify the signature of the certificate cd (record rr[0 .. 32)) with the key of the
1764note certificate kd (record rr[32 .. 64)). Allowed: sigalg 1-6 with an rsaEncryption key of >= 2048
1765note bits, sigalg 7-9 with a P-256 or P-384 key. 0, X5EALG, X5EWEAK or X5ESIG.
1766fn chain_sig cd view uchar * kd view uchar * rr view ulong * to ulong
1767p = new ulong 10 0
1768a = get rr X5RALG
1769kt = get rr add X5RSZ X5RKT
1770if or eq a 0 gt a 9
1771ret X5EALG
1772end
1773if le a 6
1774if ne kt 1
1775ret X5EALG
1776end
1777if lt get rr add X5RSZ X5RBITS 2048
1778ret X5EWEAK
1779end
1780else
1781if or lt kt 2 gt kt 3
1782ret X5EALG
1783end
1784end
1785put p 0 add div sub a 1 3 1
1786put p 1 chain_hash a
1787put p 2 get rr add X5RSZ X5RKO
1788put p 3 get rr add X5RSZ X5RKL
1789put p 4 kt
1790put p 6 get rr X5RTBO
1791put p 7 get rr X5RTBL
1792put p 8 get rr X5RSGO
1793put p 9 get rr X5RSGL
1794ret chain_hsig cd kd edit p
1795end
1796
1797note chain_hsig: digest of cd[p[6] .. +p[7]) with hash p[1], then chain_vfy of digest || signature
1798note cd[p[8] .. +p[9]) (at most 1024 bytes, else X5ESIG) with the key in kd
1799fn chain_hsig cd view uchar * kd view uchar * p edit ulong * to ulong
1800hs = new uchar 1100 0
1801sl = get p 9
1802if or eq sl 0 gt sl 1024
1803ret X5ESIG
1804end
1805put p 5 sha2_digest get p 1 part cd get p 6 get p 7 edit hs
1806dl = get p 5
1807so = get p 8
1808for i ulong sl
1809put hs add dl i get cd add so i
1810end
1811dl = add dl sl
1812so = get p 2
1813sl = get p 3
1814ret chain_vfy part kd so sl view part hs 0 dl view p
1815end
1816
1817note chain_vfy: hs = digest (p[5] bytes) || signature, key ky (RSAPublicKey DER or SEC1 point);
1818note p[0] 1 PKCS#1 v1.5, 2 PSS, 3 ECDSA; p[1] hash, p[4] key type. 0 or X5ESIG.
1819fn chain_vfy ky view uchar * hs view uchar * p view ulong * to ulong
1820k = get p 0
1821hb = get p 1
1822dl = get p 5
1823v = ulong 1
1824if eq k 1
1825v = rsa_verify_pkcs1 hb ky hs dl
1826end
1827if eq k 2
1828v = rsa_verify_pss chain_ctl hb ky hs dl
1829end
1830if eq k 3
1831v = ecdsa_verify chain_crv get p 4 ky hs dl
1832end
1833if ne v 0
1834ret X5ESIG
1835end
1836ret 0
1837end
1838
1839note chain_ctl: rsa.om PSS control word for hash hb: hash | MGF1 hash << 16 | salt (= hash bytes) << 32
1840fn chain_ctl hb ulong to ulong
1841ret or or hb shl hb 16 shl div hb 8 32
1842end
1843
1844note chain_crv: ec.om curve for key type kt (2 P-256, 3 P-384)
1845fn chain_crv kt ulong to ulong
1846if eq kt 2
1847ret 256
1848end
1849ret 384
1850end
1851
1852note chain_link: is kd (record w[32 .. 64), already parsed) the issuer of cd (record w[0 .. 32))?
1853note subject(kd) must equal issuer(cd) byte for byte (DER equality instead of the RFC 5280 7.1
1854note comparison rules), then chain_sig. X5EUNK when the names differ.
1855fn chain_link cd view uchar * kd view uchar * w view ulong * to ulong
1856io = get w X5RISO
1857il = get w X5RISL
1858so = get w add X5RSZ X5RSBO
1859sn = get w add X5RSZ X5RSBL
1860if not der_same part cd io il part kd so sn
1861ret X5EUNK
1862end
1863ret chain_sig cd kd part w 0 mul X5RSZ 2
1864end
1865
1866note chain_self: 1 when the certificate c (record w[32 .. 64)) is self-issued: its issuer and subject
1867note are the same DER bytes; else 0
1868fn chain_self c view uchar * w view ulong * to ulong
1869io = get w add X5RSZ X5RISO
1870il = get w add X5RSZ X5RISL
1871so = get w add X5RSZ X5RSBO
1872if ne il get w add X5RSZ X5RSBL
1873ret 0
1874end
1875for i ulong il
1876if ne get c add io i get c add so i
1877ret 0
1878end
1879end
1880ret 1
1881end
1882
1883note x509_same: c[a .. a+l) and c[b .. b+m) are the same bytes (spans of one array; in bounds)
1884fn x509_same c view uchar * a ulong b ulong l ulong m ulong to bool
1885if ne l m
1886ret false
1887end
1888for i ulong l
1889if ne get c add a i get c add b i
1890ret false
1891end
1892end
1893ret true
1894end
1895
1896note chain_alink: chain_link for a store entry kd, which must be self-issued (chain_self) to be an
1897note anchor; X5EUNK otherwise. An anchor that verifies cd must also pass chain_aok and chain_dist.
1898fn chain_alink cd view uchar * kd view uchar * w view ulong * to ulong
1899if eq chain_self kd w 0
1900ret X5EUNK
1901end
1902v = chain_link cd kd w
1903if ne v 0
1904ret v
1905end
1906v = chain_aok w
1907if ne v 0
1908ret v
1909end
1910ret chain_dist kd w
1911end
1912
1913note chain_dist (M1): X5EUNK when SHA-256(kd) is in X5DIST with a cutoff before the leaf notBefore
1914note w[X5WLNB] (seconds since 0000-03-01 like w[X5WNOW]; cutoff 0: never a Mozilla server-auth
1915note root), else 0
1916fn chain_dist kd view uchar * w view ulong * to ulong
1917hs = new uchar 32 0
1918n = sha2_digest 256 kd edit hs
1919for k ulong div len X5DIST 76
1920s = mul k 76
1921if der_hexeq hs part X5DIST s 64
1922if gt get w X5WLNB chain_dcut add s 65
1923ret X5EUNK
1924end
1925end
1926end
1927ret 0
1928end
1929
1930note chain_dcut: the 10-digit decimal unix-seconds cutoff at X5DIST[o .. o+10), on the internal
1931note time scale of x509_chain_verify (unix + x509_days 1970 1 1 days)
1932fn chain_dcut o ulong to ulong
1933t = ulong 0
1934for i ulong 10
1935c = ulong get X5DIST add o i
1936t = add mul t 10 sub c 48
1937end
1938ret add t mul x509_days 1970 1 1 86400
1939end
1940
1941note chain_aok: the anchor record w[32 .. 64) is within its validity at w[X5WNOW] and has
1942note basicConstraints cA TRUE; 0, X5EEXP, X5ENYV or X5ENCA
1943fn chain_aok w view ulong * to ulong
1944v = chain_time part w X5RSZ X5RSZ get w X5WNOW
1945if ne v 0
1946ret v
1947end
1948if eq get w add X5RSZ X5RCA 0
1949ret X5ENCA
1950end
1951ret 0
1952end
1953
1954note chain_xchk: profile checks on a certificate c presented by the server (record r), beyond
1955note x509_parse, which also reads trust anchors: chain_strict encodings; authorityKeyIdentifier
1956note present unless self-issued (RFC 5280 4.2.1.1 MUST); empty subject only with a critical SAN
1957note (4.2.1.6 MUST). 0 or X5EMAL. keyCertSign on a non-CA is refused by chain_leaf (a presented
1958note intermediate without cA TRUE is already X5ENCA in chain_ca).
1959fn chain_xchk c view uchar * r view ulong * to ulong
1960v = chain_strict c
1961if ne v 0
1962ret v
1963end
1964sn = get r X5RSBL
1965if not x509_same c get r X5RISO get r X5RSBO get r X5RISL sn
1966if eq and get r X5RSEEN 128 0
1967ret X5EMAL
1968end
1969end
1970if and eq sn 2 eq and get r X5RSEEN 512 0
1971ret X5EMAL
1972end
1973ret 0
1974end
1975
1976note chain_strict: DER profile of a presented Certificate c that x509_parse accepted: serialNumber
1977note positive, minimal, at most 20 octets (RFC 5280 4.1.2.2); a GeneralizedTime validity date only
1978note for years >= 2050 (4.1.2.5). Trust anchors are exempt (six roots in the Debian bundle have
1979note non-conforming serials, one a GeneralizedTime before 2050). 0 or X5EMAL.
1980fn chain_strict c view uchar * to ulong
1981h = new ulong 3 0
1982if ne der_hdr c 0 len c h 0
1983ret X5EMAL
1984end
1985e = get h 2
1986if ne der_hdr c get h 1 e h 0
1987ret X5EMAL
1988end
1989e = get h 2
1990if ne der_hdr c get h 1 e h 0
1991ret X5EMAL
1992end
1993if eq get h 0 0xa0
1994if ne der_hdr c get h 2 e h 0
1995ret X5EMAL
1996end
1997end
1998if ne x509_serok part c get h 1 sub get h 2 get h 1 0
1999ret X5EMAL
2000end
2001s = get h 2
2002for i ulong 2
2003if ne der_hdr c s e h 0
2004ret X5EMAL
2005end
2006s = get h 2
2007end
2008if ne der_hdr c s e h 0
2009ret X5EMAL
2010end
2011ret x509_vtimes c get h 1 get h 2
2012end
2013
2014note x509_serok: 0 when the INTEGER content s is a positive, minimally encoded serial of at most
2015note 20 octets (a leading 0x00 sign octet not counted), else 1
2016fn x509_serok s view uchar * to ulong
2017if ne x509_posint s 0
2018ret 1
2019end
2020n = len s
2021if eq get s 0 0
2022n = sub n 1
2023end
2024if gt n 20
2025ret 1
2026end
2027ret 0
2028end
2029
2030note x509_vtimes: Validity content c[a .. b) (two Time TLVs, framed by x509_valid): X5EMAL when a
2031note GeneralizedTime carries a year before 2050, else 0
2032fn x509_vtimes c view uchar * a ulong b ulong to ulong
2033h = new ulong 3 0
2034for i ulong 2
2035if ne der_hdr c a b h 0
2036ret X5EMAL
2037end
2038if eq get h 0 24
2039o = get h 1
2040if lt add mul der_d2 c o 100 der_d2 c add o 2 2050
2041ret X5EMAL
2042end
2043end
2044a = get h 2
2045end
2046ret 0
2047end
2048
2049note chain_entx: 1 when a CertificateEntry of the framed certificate_list cl (chain_index) carries
2050note extensions. This client offers no extension that a CertificateEntry may answer (RFC 8446 4.4.2:
2051note status_request, signed_certificate_timestamp), so any is unsolicited.
2052fn chain_entx cl view uchar * to ulong
2053p = ulong 0
2054while lt add p 5 len cl bound 100000
2055p = add add p 3 der_u24 cl p
2056if gt add p 2 len cl
2057ret 1
2058end
2059x = or shl ulong get cl p 8 ulong get cl add p 1
2060if ne x 0
2061ret 1
2062end
2063p = add p 2
2064end
2065ret 0
2066end
2067
2068note chain_anchor: try every trust anchor in st (store format) whose subject is the issuer of the
2069note current certificate cd. 0 when one verifies it; else the code of the last failed candidate,
2070note X5EUNK when none has the name. Anchors that do not parse are skipped, and so are store entries
2071note that are not self-issued (subject DER == issuer DER, chain_self): an intermediate placed in the
2072note store is not a trust anchor (OpenSSL without -partial_chain; stricter than RFC 5280 6.1.1 d).
2073note An anchor that verifies the certificate must itself be within its validity period and carry
2074note basicConstraints cA TRUE (chain_aok; RFC 5280 6.1 leaves this to the relying party, OpenSSL and
2075note x509-limbo rfc5280::validity::expired-root / root-missing-basic-constraints require it).
2076fn chain_anchor cd view uchar * st view uchar * w edit ulong * to ulong
2077p = ulong 0
2078res = X5EUNK
2079while lt p len st bound 100000
2080if lt sub len st p 3
2081break
2082end
2083l = der_u24 st p
2084p = add p 3
2085if or eq l 0 gt l sub len st p
2086break
2087end
2088if eq x509_parse part st p l part w X5RSZ X5RSZ 0
2089v = chain_alink cd part st p l view w
2090if eq v 0
2091ret 0
2092end
2093if ne v X5EUNK
2094res = v
2095end
2096end
2097p = add p l
2098end
2099ret res
2100end
2101
2102note chain_pres: try the presented certificates of cl not yet in the path as issuer of the current
2103note one (the first that verifies and passes chain_ca is taken; its index goes to w[X5WSEL]). A
2104note candidate that verifies but fails chain_ca is skipped, so a later one can still be taken. 0, or the code of the
2105note first failed candidate, replaced by any later code other than X5EUNK and X5EMAL. A presented
2106note certificate that does not parse (X5EMAL, X5ECRIT) fails the chain when no candidate verifies
2107note (fail closed: its subject cannot be known). X5EUNK when nothing is left to try.
2108fn chain_pres cl view uchar * w edit ulong * to ulong
2109kb = new uchar 16384 0
2110res = X5EUNK
2111for j ulong get w X5WN
2112if ne and get w X5WUSED shl ulong 1 j 0
2113continue
2114end
2115put w X5WSEL j
2116v = chain_try cl w kb
2117if eq v 0
2118ret 0
2119end
2120if or eq res X5EUNK and ne v X5EUNK ne v X5EMAL
2121res = v
2122end
2123end
2124ret res
2125end
2126
2127note chain_try: candidate issuer w[X5WSEL] of cl for the current certificate w[X5WCUR]: parse,
2128note profile, signature (chain_link), then the CA constraints (chain_ca). The candidate is copied
2129note into kb (the two certificates of one call must be distinct arrays)
2130fn chain_try cl view uchar * w edit ulong * kb edit uchar * to ulong
2131j = get w X5WSEL
2132o = get w add X5WIX mul j 2
2133l = get w add X5WIX add mul j 2 1
2134if gt l len kb
2135ret X5EMAL
2136end
2137for i ulong l
2138put kb i get cl add o i
2139end
2140v = x509_parse view part kb 0 l part w X5RSZ X5RSZ
2141if ne v 0
2142ret v
2143end
2144v = chain_xchk view part kb 0 l view part w X5RSZ X5RSZ
2145if ne v 0
2146ret v
2147end
2148if ne chain_rsapol view part kb 0 l part w X5RSZ X5RSZ 0
2149ret X5EWEAK
2150end
2151j = get w X5WCUR
2152o = get w add X5WIX mul j 2
2153i = get w add X5WIX add mul j 2 1
2154v = chain_link part cl o i view part kb 0 l view w
2155if ne v 0
2156ret v
2157end
2158ret chain_ca w
2159end
2160
2161note chain_ca: constraints of the chosen issuer (record w[32 .. 64)), a non-anchor CA certificate:
2162note validity at now, basicConstraints cA TRUE, pathLenConstraint >= the number of intermediates
2163note below it (w[X5WDEP] - 1), keyUsage (if present) keyCertSign, EKU (if present) serverAuth or
2164note anyExtendedKeyUsage
2165fn chain_ca w view ulong * to ulong
2166v = chain_time part w X5RSZ X5RSZ get w X5WNOW
2167if ne v 0
2168ret v
2169end
2170if eq get w add X5RSZ X5RCA 0
2171ret X5ENCA
2172end
2173if eq and get w add X5RSZ X5RSEEN 256 0
2174ret X5EMAL
2175end
2176if gt sub get w X5WDEP 1 get w add X5RSZ X5RPL
2177ret X5EPATH
2178end
2179if eq get w add X5RSZ X5RKU 1
2180if eq and get w add X5RSZ X5RKUB 4 0
2181ret X5EKU
2182end
2183end
2184if eq get w add X5RSZ X5REKU 1
2185if and eq get w add X5RSZ X5RSRV 0 eq get w add X5RSZ X5RANY 0
2186ret X5EEKU
2187end
2188end
2189ret 0
2190end
2191
2192note chain_rsapol: X5EWEAK when the certificate c (record r) has an RSA key outside x509_rsapres,
2193note else 0
2194fn chain_rsapol c view uchar * r view ulong * to ulong
2195if eq get r X5RKT 1
2196if eq x509_rsapres c r 0
2197ret X5EWEAK
2198end
2199end
2200ret 0
2201end
2202
2203note chain_leaf: the leaf (entry 0): parse, validity at now, host name, not a CA, keyUsage (if
2204note present) digitalSignature, EKU (if present) serverAuth, key type supported, RSA key policy
2205note (x509_rsapres: 2048 .. 4096 bits, exponent odd in 3 .. 2^32-1)
2206fn chain_leaf cl view uchar * hq view uchar * w edit ulong * to ulong
2207lo = get w X5WIX
2208ll = get w add X5WIX 1
2209v = x509_parse part cl lo ll part w 0 X5RSZ
2210if ne v 0
2211ret v
2212end
2213put w X5WLNB get w X5RNB
2214v = chain_xchk part cl lo ll view part w 0 X5RSZ
2215if ne v 0
2216ret v
2217end
2218v = chain_time part w 0 X5RSZ get w X5WNOW
2219if ne v 0
2220ret v
2221end
2222v = chain_hostok part hq 8 sub len hq 8
2223if ne v 0
2224ret v
2225end
2226v = chain_host part cl lo ll view part w 0 X5RSZ part hq 8 sub len hq 8
2227if ne v 0
2228ret v
2229end
2230if eq get w X5RCA 1
2231ret X5ELCA
2232end
2233if eq get w X5RKU 1
2234if eq and get w X5RKUB 128 0
2235ret X5EKU
2236end
2237if ne and get w X5RKUB 4 0
2238ret X5EKU
2239end
2240end
2241if eq get w X5REKU 1
2242if eq get w X5RSRV 0
2243ret X5EEKU
2244end
2245end
2246kt = get w X5RKT
2247if or eq kt 0 gt kt 3
2248ret X5EALG
2249end
2250if eq kt 1
2251if eq x509_rsapres part cl lo ll part w 0 X5RSZ 0
2252ret X5EWEAK
2253end
2254end
2255ret 0
2256end
2257
2258note chain_path: build the path upward from the leaf. At each step a trust anchor is preferred
2259note (chain_anchor); else a presented certificate that passes chain_try (signature and chain_ca).
2260note Each presented certificate is used at most once (no loops); at most X5MAXD non-anchor
2261note certificates. Greedy: no backtracking past a chosen issuer.
2262fn chain_path cl view uchar * st view uchar * w edit ulong * to ulong
2263put w X5WUSED 1
2264put w X5WDEP 1
2265put w X5WCUR 0
2266for k ulong X5MAXD
2267cu = get w X5WCUR
2268co = get w add X5WIX mul cu 2
2269cn = get w add X5WIX add mul cu 2 1
2270a = chain_anchor part cl co cn st w
2271if eq a 0
2272ret 0
2273end
2274v = chain_pres cl w
2275if ne v 0
2276if eq v X5EUNK
2277ret a
2278end
2279ret v
2280end
2281if ge get w X5WDEP X5MAXD
2282ret X5EDEEP
2283end
2284chain_up w
2285end
2286ret X5EDEEP
2287end
2288
2289note chain_up: the chosen presented issuer becomes the current certificate
2290fn chain_up w edit ulong * to void
2291for i ulong X5RSZ
2292put w i get w add X5RSZ i
2293end
2294j = get w X5WSEL
2295put w X5WCUR j
2296put w X5WUSED or get w X5WUSED shl ulong 1 j
2297put w X5WDEP add get w X5WDEP 1
2298end
2299
2300note x509_chain_verify: validate the server chain. cl = TLS 1.3 certificate_list contents, leaf
2301note first; st = store from x509_store_load(f); hq = 8-byte big-endian unix time || host name.
2302note 0 valid, else a reject code (see the table at the top). Limits (X5EMAL beyond them): at most
2303note X5MAXC (16) entries in cl, presented issuer certificates at most X5KBUF (16384) bytes, signatures
2304note at most 1024 bytes (X5ESIG), now at most 253402300799 (9999-12-31T23:59:59Z).
2305note Rules, all fail closed: leaf = entry 0 (chain_leaf); issuers by path building (chain_path);
2306note trust anchors are the self-issued store entries, name + key only (no validity, CA or usage
2307note checks on anchors, RFC 5280 6.1.1 d);
2308note names are compared as DER bytes, not with the RFC 5280 7.1 / 4.1.2.4 matching rules (case and
2309note whitespace folding of PrintableString/UTF8String are NOT applied: an issuer whose encoding of
2310note the same name differs from the subject encoding is reported X5EUNK).
2311fn x509_chain_verify cl view uchar * st view uchar * hq view uchar * to ulong
2312w = new ulong 128 0
2313if lt len hq 8
2314ret X5EMAL
2315end
2316nw = ulong 0
2317for i ulong 8
2318nw = or shl nw 8 ulong get hq i
2319end
2320if gt nw 253402300799
2321ret X5EMAL
2322end
2323put w X5WNOW add nw mul x509_days 1970 1 1 86400
2324n = chain_index cl part w X5WIX mul X5MAXC 2
2325if eq n 0
2326ret X5EMAL
2327end
2328if ne chain_entx cl 0
2329ret X5EEXT
2330end
2331if gt get w add X5WIX 1 X5LMAX
2332ret X5EMAL
2333end
2334put w X5WN n
2335v = chain_leaf cl hq w
2336if ne v 0
2337ret v
2338end
2339ret chain_path cl st w
2340end
2341
2342note chain_cvsig: CertificateVerify check with the leaf kd (record r; r[30] hash, r[31] content
2343note length): ms = content || signature; RSA keys use PSS, EC keys ECDSA (chain_hsig)
2344fn chain_cvsig kd view uchar * r view ulong * ms view uchar * to ulong
2345p = new ulong 10 0
2346kt = get r X5RKT
2347put p 0 3
2348if eq kt 1
2349put p 0 2
2350end
2351put p 1 get r 30
2352put p 2 get r X5RKO
2353put p 3 get r X5RKL
2354put p 4 kt
2355put p 7 get r 31
2356put p 8 get r 31
2357put p 9 sub len ms get r 31
2358ret chain_hsig ms kd edit p
2359end
2360
2361note x509_cv_verify: TLS 1.3 CertificateVerify (RFC 8446 4.4.3) against the leaf of cl. ms = signed
2362note content (mlen bytes) || signature. Schemes: 0x0403 ecdsa_secp256r1_sha256 (P-256 key),
2363note 0x0503 ecdsa_secp384r1_sha384 (P-384 key), 0x0804/0x0805/0x0806 rsa_pss_rsae_sha256/384/512
2364note (rsaEncryption key within x509_rsaok, else X5EWEAK). Anything else, rsa_pkcs1_* included, is
2365note X5ESCH.
2366fn x509_cv_verify cl view uchar * scheme ulong ms view uchar * mlen ulong to ulong
2367r = new ulong 32 0
2368if lt len cl 3
2369ret X5EMAL
2370end
2371l = der_u24 cl 0
2372if or eq l 0 gt l sub len cl 3
2373ret X5EMAL
2374end
2375v = x509_parse part cl ulong 3 l edit r
2376if ne v 0
2377ret v
2378end
2379if gt mlen len ms
2380ret X5EMAL
2381end
2382kt = get r X5RKT
2383hb = ulong 0
2384if and eq scheme 0x0403 eq kt 2
2385hb = 256
2386end
2387if and eq scheme 0x0503 eq kt 3
2388hb = 384
2389end
2390if and and ge scheme 0x0804 le scheme 0x0806 eq kt 1
2391hb = add 256 mul sub scheme 0x0804 128
2392if eq x509_rsapres part cl ulong 3 l view r 0
2393ret X5EWEAK
2394end
2395end
2396if eq hb 0
2397ret X5ESCH
2398end
2399put r 30 hb
2400put r 31 mlen
2401ret chain_cvsig part cl ulong 3 l r ms
2402end