interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / src / x509.om

1769 lines · 38066 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note x509.om (ot_x5_): certificate chain validation for the TLS 1.3 client (RFC 5280 section 6.1 subset,
3note RFC 6125 / RFC 9525 DNS-ID matching, RFC 8446 4.4.2). State lives in ot_x5_ globals; one
4note validation at a time per process.
5note Unit order: src/der.om and src/pkv.om must come earlier in the unit (their globals are used here).
6note
7note Rules (each is checked by tests/drv/x509.om; codes are the ot_x5_check results):
8note - Strict DER for every field read (der.om): minimal lengths, no indefinite length, minimal INTEGERs,
9note BOOLEAN 0x00/0xFF; a DEFAULT value present (version v1, critical FALSE, cA FALSE) is not DER and is
10note refused. version is v3 when extensions are present and v2 or v3 when unique IDs are present;
11note serialNumber positive, at most 20 octets (4.1.2.2); tbsCertificate.signature byte-equal to
12note signatureAlgorithm (4.1.1.2); signatureValue and subjectPublicKey with no unused bits; nothing
13note after the last field; no extension twice (4.2: the extension OIDs of a certificate are sorted and
14note neighbours compared, O(S log S) for a certificate of S bytes, no count cap); keyUsage with
15note DER-minimal unused bits (X.690 11.2.2: the lowest used bit of the last byte is set); extension
16note lists, EKU, SAN and name-constraint subtree lists non-empty; Name is SEQUENCE OF non-empty SET OF { OID, one value }; validity
17note times per 4.1.2.5: UTCTime through 2049, GeneralizedTime from 2050 (a GeneralizedTime before 2050
18note is refused), Z only, seconds present. An empty subject needs a critical subjectAltName (4.2.1.6).
19note A presented certificate that breaks any of these is code 1. Two rules are relaxed for trust-store
20note certificates only, because widely used roots break them and neither value is relied on: serial
21note number 0 (Go Daddy and Starfield G2, Hellenic 2015, SECOM RootCA2) and GeneralizedTime before 2050
22note (Certum Trusted Network CA 2). A trust-store certificate that breaks another rule is not loaded
23note (counted in ot_x5_st 5) and so cannot anchor a path.
24note - Codes and precedence (deterministic, not dependent on search order): 9 no certificate presented;
25note 1 any presented certificate fails to parse; 2 the leaf does not match the host; 3 the leaf is
26note outside its validity; 4 no path: no chain of issuer links from the leaf to a trust-store
27note certificate. Otherwise every path is scored by its first failing check in the order 3 (validity of
28note every certificate on it, the trust anchor included), 5 (a signature that does not verify, or an
29note algorithm that does not suit the issuer key), 6 (constraints below), 7 (an unrecognised critical
30note extension on any certificate of the path, or a critical nameConstraints with a form other than a
31note plain dNSName subtree), 8 (a signature algorithm other than those of pkv.om, e.g. SHA-1 or MD5, on
32note any non-root certificate; a key of an unsupported type or curve, or one the primitive refuses, e.g.
33note RSA under ot_rsa_minbits, on any certificate of the path); a path with none of these is 0. The
34note result is 0 when some path scores 0, else the lowest score of all paths.
35note - Path building: from the leaf, candidate issuers are the presented certificates (in the order
36note received) then the trust store (in file order) whose subject Name is byte-equal to the issuer Name
37note (RFC 5280 7.1 normalisation is not done) and, when the child has an authorityKeyIdentifier
38note keyIdentifier and the candidate a subjectKeyIdentifier, with equal key identifiers. Depth-first
39note with backtracking; a certificate is used at most once per path; a path ends at the first
40note trust-store certificate reached. No fixed depth: the search ends because certificates are not
41note reused; a budget of 1000000 candidate examinations bounds pathological cross-certified sets (the
42note search then stops with what it has found). The trust anchor's own signature is not checked; its
43note validity, basicConstraints, keyUsage, nameConstraints, critical extensions and key are.
44note - Constraints (code 6): every issuer on the path, the trust anchor included, has basicConstraints
45note with cA TRUE; its pathLenConstraint, when present, is at least the number of certificates between it
46note and the leaf (self-issued ones counted too: stricter than 6.1.4 (l)); its keyUsage, when present,
47note has keyCertSign; the leaf's keyUsage, when present, has digitalSignature (TLS 1.3 signs with it);
48note extKeyUsage, when present on the leaf or an intermediate, includes serverAuth or
49note anyExtendedKeyUsage; nameConstraints of every issuer: each dNSName SAN of the leaf, and the host
50note when it is a DNS name, matches no excluded dNSName subtree and, when there are permitted dNSName
51note subtrees, matches one of them; a wildcard SAN "*.D" is also refused when an excluded subtree lies
52note strictly below D (the wildcard would cover a name the issuer may not certify). The host check is
53note implied by the SAN checks (the host matched a SAN) and is kept as defence in depth. A
54note subtree "example.com" matches that name and names ending ".example.com"; ".example.com" matches
55note only the latter; an empty one matches all. Other forms are ignored when the extension is not
56note critical (code 7 when it is).
57note - Host name (RFC 6125 6.4, RFC 9525): one trailing dot is removed from the host first; subjectAltName
58note dNSName entries only (no CN fallback: a leaf without a SAN never matches); ASCII case-insensitive; a
59note wildcard only as the whole left-most label "*." followed by at least two labels without "*", and it
60note matches exactly one non-empty host label; "*" alone, "*.com" and partial labels "f*.x.y" never
61note match. A host of digits and dots is an IPv4 literal (four decimal parts 0..255, no leading zeros)
62note and a host with ":" an IPv6 literal (hex groups, at most one "::", no embedded IPv4 or zone); they
63note match iPAddress SANs byte-exactly. An empty host, one longer than 253 bytes, one with an empty
64note label or a "*", or a malformed literal matches nothing.
65note - Time: notBefore <= now <= notAfter (both ends inclusive, RFC 5280 4.1.2.5).
66note - Storage: one 16 MiB store (ot_der_m) less the 64 KiB ot_pk_ scratch at its top holds the trust
67note store and the presented chain (and, while ot_x5_roots runs, the bundle text at the top of that
68note region). That is the only boundary: ot_x5_roots and ot_x5_add refuse with -3 when it is full. The
69note record index has one entry per 64 bytes of store, more than the store can hold certificates
70note (a certificate is longer than 64 bytes: its validity alone takes 32).
71const ot_x5_cap ulong 16711680
72const ot_x5_mr ulong 261120
73const ot_x5_none ulong 18446744073709551615
74note record fields (32 per certificate): 0 start, 1 end, 2 tbs offset, 3 tbs end, 4 signatureAlgorithm
75note offset, 5 its length, 6 signature offset, 7 signature length, 8 issuer offset, 9 its length,
76note 10 subject offset, 11 its length, 14 SPKI offset, 15 its length, 16 SAN contents offset, 17 their
77note length, 18 SKI offset, 19 SKI length, 20 AKI keyIdentifier offset, 21 its length, 22 flags,
78note 23 pathLenConstraint (ot_x5_none: absent), 24 keyUsage bits (first byte << 8 | second),
79note 25 nameConstraints contents offset, 26 their length
80note flags: 1 basicConstraints, 2 cA, 4 keyUsage, 8 extKeyUsage, 16 EKU serverAuth or any,
81note 32 unrecognised critical extension, 64 critical nameConstraints with an unsupported form, 128 SAN,
82note 256 nameConstraints, 512 SAN critical
83global ot_x5_rc ulong 8355840
84note validity of record r: 2r notBefore, 2r + 1 notAfter (Unix seconds)
85global ot_x5_tm long 522240
86note ot_x5_st: 0 trust-store records, 1 store bytes used by them, 2 presented records, 3 presented
87note certificates refused, 4 store write position, 5 bundle certificates not loaded
88global ot_x5_st ulong 8
89global ot_x5_nw long 1
90note ot_x5_ps: 0 the number of extension OIDs listed in ot_x5_eo, 1 the version of the certificate
91note being parsed, 2 1 while the trust store is parsed
92global ot_x5_ps ulong 3
93note ot_x5_eo: the extension OIDs of the certificate being parsed, offset << 24 | length (both below
94note 2^24: the store is 16 MiB); ot_x5_et the merge buffer. An extension takes at least 7 bytes, so
95note 2400000 entries hold every extension a certificate in the store can have.
96const ot_x5_me ulong 2400000
97global ot_x5_eo ulong 2400000
98global ot_x5_et ulong 2400000
99global ot_x5_pth ulong 261120
100global ot_x5_cur ulong 261120
101global ot_x5_on uchar 261120
102note the host: ot_x5_hb its bytes (lower case), ot_x5_hs 0 length, 1 kind (0 DNS, 1 IPv4, 2 IPv6),
103note ot_x5_ia the address bytes
104global ot_x5_hb uchar 256
105global ot_x5_hs ulong 2
106global ot_x5_ia uchar 16
107
108fn ot_x5_g r ulong f ulong to ulong
109ret get ot_x5_rc add mul r 32 f
110end
111fn ot_x5_s r ulong f ulong v ulong to void
112put ot_x5_rc add mul r 32 f v
113end
114fn ot_x5_fl r ulong b ulong to void
115ot_x5_s r 22 or ot_x5_g r 22 b
116end
117fn ot_x5_has r ulong b ulong to bool
118ret ne and ot_x5_g r 22 b 0
119end
120
121note ---- parsing ----
122
123note ot_x5_parse: the certificate exactly [s, e) into record r; false when it breaks a rule
124fn ot_x5_parse s ulong e ulong r ulong to bool
125for f ulong 32
126ot_x5_s r f 0
127end
128ot_x5_s r 23 ot_x5_none
129ot_x5_s r 0 s
130ot_x5_s r 1 e
131if eq ot_der_ex s e 48 false
132ret false
133end
134if ne ot_der_nd e
135ret false
136end
137v = ot_der_vo
138if eq ot_der_ex v e 48 false
139ret false
140end
141ot_x5_s r 2 v
142a = ot_der_nd
143ot_x5_s r 3 a
144if eq ot_der_ex a e 48 false
145ret false
146end
147b = ot_der_nd
148ot_x5_s r 4 a
149ot_x5_s r 5 sub b a
150if eq ot_der_ex b e 3 false
151ret false
152end
153if ne ot_der_nd e
154ret false
155end
156if eq ot_der_okbits ot_der_vo ot_der_vl false
157ret false
158end
159if ne get ot_der_m ot_der_vo 0
160ret false
161end
162ot_x5_s r 6 add ot_der_vo 1
163ot_x5_s r 7 sub ot_der_vl 1
164if eq ot_pk_sigalg a b false
165ret false
166end
167ret ot_x5_tbs r
168end
169
170note ot_x5_ver: the optional [0] EXPLICIT version at i; sets ot_x5_ps 1 to 0 (v1), 1 (v2) or 2 (v3);
171note the offset after it, or 0 when malformed
172fn ot_x5_ver i ulong e ulong to ulong
173put ot_x5_ps 1 0
174if ge i e
175ret 0
176end
177if ne get ot_der_m i 160
178ret i
179end
180if eq ot_der_ex i e 160 false
181ret 0
182end
183j = ot_der_nd
184if eq ot_der_ex ot_der_vo j 2 false
185ret 0
186end
187if ne ot_der_nd j
188ret 0
189end
190v = ot_der_small ot_der_vo ot_der_vl
191if or lt v 1 gt v 2
192ret 0
193end
194put ot_x5_ps 1 v
195ret j
196end
197
198note ot_x5_serial: a positive INTEGER of at most 20 content octets just read
199fn ot_x5_serial to bool
200o = ot_der_vo
201n = ot_der_vl
202if or eq ot_der_okint o n false gt n 20
203ret false
204end
205if ge get ot_der_m o 128
206ret false
207end
208if and eq n 1 eq get ot_der_m o 0
209ret ne get ot_x5_ps 2 0
210end
211ret true
212end
213
214note ot_x5_tbs: tbsCertificate of record r (RFC 5280 4.1)
215fn ot_x5_tbs r ulong to bool
216e = ot_x5_g r 3
217if eq ot_der_ex ot_x5_g r 2 e 48 false
218ret false
219end
220i = ot_x5_ver ot_der_vo e
221if eq i 0
222ret false
223end
224if eq ot_der_ex i e 2 false
225ret false
226end
227if eq ot_x5_serial false
228ret false
229end
230i = ot_der_nd
231if eq ot_der_ex i e 48 false
232ret false
233end
234n = sub ot_der_nd i
235if ne n ot_x5_g r 5
236ret false
237end
238if eq ot_der_eq i ot_x5_g r 4 n false
239ret false
240end
241i = add i n
242j = ot_x5_name i e
243if eq j 0
244ret false
245end
246ot_x5_s r 8 i
247ot_x5_s r 9 sub j i
248i = ot_x5_valid j e r
249if eq i 0
250ret false
251end
252j = ot_x5_name i e
253if eq j 0
254ret false
255end
256ot_x5_s r 10 i
257ot_x5_s r 11 sub j i
258ret ot_x5_tbs2 j e r
259end
260
261note ot_x5_tbs2: subjectPublicKeyInfo, unique IDs and extensions, from i to the tbs end e
262fn ot_x5_tbs2 i ulong e ulong r ulong to bool
263if eq ot_der_ex i e 48 false
264ret false
265end
266j = ot_der_nd
267if eq ot_pk_spki i j false
268ret false
269end
270ot_x5_s r 14 i
271ot_x5_s r 15 sub j i
272v = get ot_x5_ps 1
273j = ot_x5_uid j e 129 v
274j = ot_x5_uid j e 130 v
275if eq j 0
276ret false
277end
278if lt j e
279if ne v 2
280ret false
281end
282if eq ot_der_ex j e 163 false
283ret false
284end
285k = ot_der_nd
286if eq ot_x5_exts ot_der_vo k r false
287ret false
288end
289j = k
290end
291if ne j e
292ret false
293end
294if eq ot_x5_g r 11 2
295if eq ot_x5_has r 512 false
296ret false
297end
298end
299ret true
300end
301
302note ot_x5_uid: the optional unique ID with tag t at i (version v2 or v3 only); next offset, 0 malformed
303fn ot_x5_uid i ulong e ulong t ulong v ulong to ulong
304if or eq i 0 ge i e
305ret i
306end
307if ne ulong get ot_der_m i t
308ret i
309end
310if eq v 0
311ret 0
312end
313if eq ot_der_ex i e t false
314ret 0
315end
316if eq ot_der_okbits ot_der_vo ot_der_vl false
317ret 0
318end
319ret ot_der_nd
320end
321
322note ot_x5_name: a Name at i inside [i, e); its end, or 0 when malformed
323fn ot_x5_name i ulong e ulong to ulong
324if eq ot_der_ex i e 48 false
325ret 0
326end
327j = ot_der_nd
328k = ot_der_vo
329while lt k j bound 16777216
330if eq ot_der_ex k j 49 false
331ret 0
332end
333m = ot_der_nd
334if eq ot_der_vl 0
335ret 0
336end
337if eq ot_x5_rdn ot_der_vo m false
338ret 0
339end
340k = m
341end
342ret j
343end
344
345note ot_x5_rdn: SET OF AttributeTypeAndValue contents [a, m)
346fn ot_x5_rdn a ulong m ulong to bool
347k = a
348while lt k m bound 16777216
349if eq ot_der_ex k m 48 false
350ret false
351end
352b = ot_der_nd
353if eq ot_der_ex ot_der_vo b 6 false
354ret false
355end
356if eq ot_der_okoid ot_der_vo ot_der_vl false
357ret false
358end
359if eq ot_der_rd ot_der_nd b false
360ret false
361end
362if ne ot_der_nd b
363ret false
364end
365k = b
366end
367ret true
368end
369
370note ot_x5_time: the time TLV exactly [a, b): seconds or ot_tm_bad (GeneralizedTime before 2050 refused)
371fn ot_x5_time a ulong b ulong to long
372v = ot_tm_der a b
373if eq v ot_tm_bad
374ret v
375end
376if ne ot_der_nd b
377ret ot_tm_bad
378end
379if and eq ot_der_tg 24 lt v 2524608000
380if eq get ot_x5_ps 2 0
381ret ot_tm_bad
382end
383end
384ret v
385end
386
387note ot_x5_valid: Validity at i; its end, or 0
388fn ot_x5_valid i ulong e ulong r ulong to ulong
389if eq ot_der_ex i e 48 false
390ret 0
391end
392j = ot_der_nd
393a = ot_der_vo
394if eq ot_der_rd a j false
395ret 0
396end
397b = ot_der_nd
398t = ot_x5_time a b
399if eq t ot_tm_bad
400ret 0
401end
402put ot_x5_tm mul r 2 t
403if eq ot_der_rd b j false
404ret 0
405end
406t = ot_x5_time b ot_der_nd
407if eq t ot_tm_bad
408ret 0
409end
410if ne ot_der_nd j
411ret 0
412end
413put ot_x5_tm add mul r 2 1 t
414ret j
415end
416
417note ---- extensions ----
418
419note ot_x5_exts: Extensions SEQUENCE exactly [p, e)
420fn ot_x5_exts p ulong e ulong r ulong to bool
421if eq ot_der_ex p e 48 false
422ret false
423end
424if or ne ot_der_nd e eq ot_der_vl 0
425ret false
426end
427k = ot_der_vo
428put ot_x5_ps 0 0
429while lt k e bound 16777216
430if eq ot_der_ex k e 48 false
431ret false
432end
433m = ot_der_nd
434if eq ot_x5_ext ot_der_vo m r false
435ret false
436end
437k = m
438end
439ret ot_x5_nodup
440end
441
442note ot_x5_eadd: list the extension OID [o, o + l) for the duplicate check
443fn ot_x5_eadd o ulong l ulong to bool
444n = get ot_x5_ps 0
445if ge n ot_x5_me
446ret false
447end
448put ot_x5_eo n or shl o 24 l
449put ot_x5_ps 0 add n 1
450ret true
451end
452
453note ot_x5_ocmp: order of two listed OIDs: 0 a first, 1 equal, 2 b first (length, then bytes)
454fn ot_x5_ocmp a ulong b ulong to ulong
455la = and a 16777215
456lb = and b 16777215
457if lt la lb
458ret 0
459end
460if gt la lb
461ret 2
462end
463oa = shr a 24
464ob = shr b 24
465for i ulong la bound 16777216
466x = get ot_der_m add oa i
467y = get ot_der_m add ob i
468if lt x y
469ret 0
470end
471if gt x y
472ret 2
473end
474end
475ret 1
476end
477
478note ot_x5_mrg: merge the sorted runs ot_x5_eo [lo, md) and [md, hi) into ot_x5_et [lo, hi)
479fn ot_x5_mrg lo ulong md ulong hi ulong to void
480i = lo
481j = md
482k = lo
483while lt k hi bound 16777216
484tk = false
485if lt i md
486if ge j hi
487tk = true
488else
489if ne ot_x5_ocmp get ot_x5_eo i get ot_x5_eo j 2
490tk = true
491end
492end
493end
494if tk
495put ot_x5_et k get ot_x5_eo i
496i = add i 1
497else
498put ot_x5_et k get ot_x5_eo j
499j = add j 1
500end
501k = add k 1
502end
503end
504
505note ot_x5_pass: one bottom-up merge pass with runs of w over n entries, then back into ot_x5_eo
506fn ot_x5_pass w ulong n ulong to void
507lo = ulong 0
508while lt lo n bound 16777216
509md = add lo w
510if gt md n
511md = n
512end
513hi = add md w
514if gt hi n
515hi = n
516end
517ot_x5_mrg lo md hi
518lo = hi
519end
520for i ulong n bound 16777216
521put ot_x5_eo i get ot_x5_et i
522end
523end
524
525note ot_x5_nodup: sort the listed extension OIDs (bottom-up merge sort) and refuse two equal ones
526fn ot_x5_nodup to bool
527n = get ot_x5_ps 0
528w = ulong 1
529while lt w n bound 64
530ot_x5_pass w n
531w = mul w 2
532end
533for i ulong n bound 16777216
534if gt i 0
535if eq ot_x5_ocmp get ot_x5_eo sub i 1 get ot_x5_eo i 1
536ret false
537end
538end
539end
540ret true
541end
542
543note ot_x5_ext: one Extension's contents [v, m)
544fn ot_x5_ext v ulong m ulong r ulong to bool
545if eq ot_der_ex v m 6 false
546ret false
547end
548o = ot_der_vo
549l = ot_der_vl
550i = ot_der_nd
551if eq ot_der_okoid o l false
552ret false
553end
554if eq ot_x5_eadd o l false
555ret false
556end
557c = false
558if lt i m
559if eq get ot_der_m i 1
560if eq ot_der_ex i m 1 false
561ret false
562end
563if or ne ot_der_vl 1 ne get ot_der_m ot_der_vo 255
564ret false
565end
566c = true
567i = ot_der_nd
568end
569end
570if eq ot_der_ex i m 4 false
571ret false
572end
573if ne ot_der_nd m
574ret false
575end
576ret ot_x5_extv o l ot_der_vo r c
577end
578
579note ot_x5_extv: extnValue contents from v (to the end the last header read gave) by OID [o, o+l)
580fn ot_x5_extv o ulong l ulong v ulong r ulong c bool to bool
581e = ot_der_nd
582if ot_der_oid o l 3 0 0x551d13
583ret ot_x5_bc v e r
584end
585if ot_der_oid o l 3 0 0x551d0f
586ret ot_x5_ku v e r
587end
588if ot_der_oid o l 3 0 0x551d25
589ret ot_x5_eku v e r
590end
591if ot_der_oid o l 3 0 0x551d11
592if c
593ot_x5_fl r 512
594end
595ret ot_x5_san v e r
596end
597if ot_der_oid o l 3 0 0x551d1e
598ret ot_x5_nc v e r c
599end
600if ot_der_oid o l 3 0 0x551d0e
601ret ot_x5_ski v e r
602end
603if ot_der_oid o l 3 0 0x551d23
604ret ot_x5_aki v e r
605end
606if c
607ot_x5_fl r 32
608end
609ret true
610end
611
612note ot_x5_bc: BasicConstraints ::= SEQUENCE { cA BOOLEAN DEFAULT FALSE, pathLenConstraint INTEGER (0..MAX) OPTIONAL }
613fn ot_x5_bc v ulong e ulong r ulong to bool
614if eq ot_der_ex v e 48 false
615ret false
616end
617if ne ot_der_nd e
618ret false
619end
620i = ot_der_vo
621ot_x5_fl r 1
622if lt i e
623if eq get ot_der_m i 1
624if eq ot_der_ex i e 1 false
625ret false
626end
627if or ne ot_der_vl 1 ne get ot_der_m ot_der_vo 255
628ret false
629end
630ot_x5_fl r 2
631i = ot_der_nd
632end
633end
634if lt i e
635if eq ot_der_ex i e 2 false
636ret false
637end
638n = ot_der_small ot_der_vo ot_der_vl
639if eq n ot_x5_none
640ret false
641end
642ot_x5_s r 23 n
643i = ot_der_nd
644end
645ret eq i e
646end
647
648note ot_x5_ku: KeyUsage BIT STRING (DER, X.690 11.2.2: trailing zero bits removed, so the lowest used
649note bit of the last byte is set and at least one bit is; only decipherOnly may be set in the second byte)
650fn ot_x5_ku v ulong e ulong r ulong to bool
651if eq ot_der_ex v e 3 false
652ret false
653end
654if ne ot_der_nd e
655ret false
656end
657o = ot_der_vo
658n = ot_der_vl
659if or lt n 2 gt n 3
660ret false
661end
662if eq ot_der_okbits o n false
663ret false
664end
665u = get ot_der_m o
666if eq and shr get ot_der_m add o sub n 1 u 1 0
667ret false
668end
669x = shl ulong get ot_der_m add o 1 8
670if eq n 3
671if ne and get ot_der_m add o 2 127 0
672ret false
673end
674x = or x ulong get ot_der_m add o 2
675end
676ot_x5_fl r 4
677ot_x5_s r 24 x
678ret true
679end
680
681note ot_x5_eku: ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
682fn ot_x5_eku v ulong e ulong r ulong to bool
683if eq ot_der_ex v e 48 false
684ret false
685end
686if or ne ot_der_nd e eq ot_der_vl 0
687ret false
688end
689k = ot_der_vo
690ot_x5_fl r 8
691while lt k e bound 16777216
692if eq ot_der_ex k e 6 false
693ret false
694end
695o = ot_der_vo
696l = ot_der_vl
697k = ot_der_nd
698if eq ot_der_okoid o l false
699ret false
700end
701if or ot_der_oid o l 8 0 0x2b06010505070301 ot_der_oid o l 4 0 0x551d2500
702ot_x5_fl r 16
703end
704end
705ret true
706end
707
708note ot_x5_gn: one GeneralName at k inside [k, e); nc: inside nameConstraints (iPAddress is then an
709note address and a mask, 8 or 32 bytes)
710fn ot_x5_gn k ulong e ulong nc bool to bool
711if eq ot_der_rd k e false
712ret false
713end
714t = ot_der_tg
715if eq t 130
716ret ot_x5_ia5 ot_der_vo ot_der_vl
717end
718if eq t 135
719n = ot_der_vl
720if nc
721ret or eq n 8 eq n 32
722end
723ret or eq n 4 eq n 16
724end
725if or or eq t 160 eq t 129 or eq t 163 eq t 164
726ret true
727end
728ret or or eq t 165 eq t 134 eq t 136
729end
730
731fn ot_x5_ia5 o ulong n ulong to bool
732for i ulong n bound 16777216
733if ge get ot_der_m add o i 128
734ret false
735end
736end
737ret true
738end
739
740note ot_x5_san: GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
741fn ot_x5_san v ulong e ulong r ulong to bool
742if eq ot_der_ex v e 48 false
743ret false
744end
745if or ne ot_der_nd e eq ot_der_vl 0
746ret false
747end
748a = ot_der_vo
749k = a
750while lt k e bound 16777216
751if eq ot_x5_gn k e false false
752ret false
753end
754k = ot_der_nd
755end
756ot_x5_s r 16 a
757ot_x5_s r 17 sub e a
758ot_x5_fl r 128
759ret true
760end
761
762note ot_x5_ski: SubjectKeyIdentifier ::= OCTET STRING (non-empty)
763fn ot_x5_ski v ulong e ulong r ulong to bool
764if eq ot_der_ex v e 4 false
765ret false
766end
767if or ne ot_der_nd e eq ot_der_vl 0
768ret false
769end
770ot_x5_s r 18 ot_der_vo
771ot_x5_s r 19 ot_der_vl
772ret true
773end
774
775note ot_x5_aki: AuthorityKeyIdentifier ::= SEQUENCE { [0] keyIdentifier, [1] authorityCertIssuer,
776note [2] authorityCertSerialNumber } (all optional, in order)
777fn ot_x5_aki v ulong e ulong r ulong to bool
778if eq ot_der_ex v e 48 false
779ret false
780end
781if ne ot_der_nd e
782ret false
783end
784i = ot_der_vo
785if lt i e
786if eq get ot_der_m i 128
787if eq ot_der_ex i e 128 false
788ret false
789end
790if eq ot_der_vl 0
791ret false
792end
793ot_x5_s r 20 ot_der_vo
794ot_x5_s r 21 ot_der_vl
795i = ot_der_nd
796end
797end
798if lt i e
799if eq get ot_der_m i 161
800if eq ot_der_ex i e 161 false
801ret false
802end
803i = ot_der_nd
804end
805end
806if lt i e
807if eq get ot_der_m i 130
808if eq ot_der_ex i e 130 false
809ret false
810end
811i = ot_der_nd
812end
813end
814ret eq i e
815end
816
817note ot_x5_nc: NameConstraints ::= SEQUENCE { permittedSubtrees [0], excludedSubtrees [1] } (not both absent)
818fn ot_x5_nc v ulong e ulong r ulong c bool to bool
819if eq ot_der_ex v e 48 false
820ret false
821end
822if or ne ot_der_nd e eq ot_der_vl 0
823ret false
824end
825i = ot_der_vo
826ot_x5_s r 25 i
827ot_x5_s r 26 sub e i
828ot_x5_fl r 256
829if lt i e
830if eq get ot_der_m i 160
831if eq ot_der_ex i e 160 false
832ret false
833end
834j = ot_der_nd
835if eq ot_x5_gst ot_der_vo j r c false
836ret false
837end
838i = j
839end
840end
841if lt i e
842if eq ot_der_ex i e 161 false
843ret false
844end
845j = ot_der_nd
846if eq ot_x5_gst ot_der_vo j r c false
847ret false
848end
849i = j
850end
851ret eq i e
852end
853
854note ot_x5_gst: GeneralSubtrees contents [a, j): SIZE (1..MAX) OF SEQUENCE { base GeneralName,
855note minimum [0] DEFAULT 0, maximum [1] OPTIONAL }. A subtree other than a dNSName base alone is an
856note unsupported form (flag 64 when the extension is critical).
857fn ot_x5_gst a ulong j ulong r ulong c bool to bool
858if ge a j
859ret false
860end
861k = a
862while lt k j bound 16777216
863if eq ot_der_ex k j 48 false
864ret false
865end
866m = ot_der_nd
867if eq ot_x5_gn ot_der_vo m true false
868ret false
869end
870if or ne ot_der_tg 130 ne ot_der_nd m
871if c
872ot_x5_fl r 64
873end
874if ne ot_der_nd m
875if eq ot_x5_mm ot_der_nd m false
876ret false
877end
878end
879end
880k = m
881end
882ret true
883end
884
885note ot_x5_mm: the minimum [0] and maximum [1] of a GeneralSubtree, exactly [i, m)
886fn ot_x5_mm i ulong m ulong to bool
887k = i
888if lt k m
889if eq get ot_der_m k 128
890if eq ot_der_ex k m 128 false
891ret false
892end
893k = ot_der_nd
894end
895end
896if lt k m
897if eq ot_der_ex k m 129 false
898ret false
899end
900k = ot_der_nd
901end
902ret eq k m
903end
904
905note ---- trust store and presented chain ----
906
907fn ot_x5_clear to void
908for i ulong 8
909put ot_x5_st i 0
910end
911end
912
913note ot_x5_roots: load a PEM bundle as the trust store (replacing any earlier one and the presented
914note chain). The number of certificates loaded, or: -1 the file cannot be opened or read, -2 a malformed
915note PEM block, -3 the file or its certificates do not fit the store (the one storage boundary). A
916note well-formed block whose certificate breaks a parsing rule is not loaded (ot_x5_st 5 counts them).
917export fn ot_x5_roots path view uchar * to long
918sb = new uchar 128 0
919ot_x5_clear
920n = len path
921if or eq n 0 gt n 4096
922ret -1
923end
924fd = fopen path 0 n 0 0
925if lt fd 0
926ret -1
927end
928st = fstat int fd sb 0 128
929if lt st 0
930c = fclose int fd
931ret -1
932end
933z = ulong 0
934for i ulong 8
935z = or z shl ulong get sb add 48 i mul i 8
936end
937if gt z ot_x5_cap
938c = fclose int fd
939ret -3
940end
941b = sub ot_x5_cap z
942t = ulong 0
943while lt t z bound 16777216
944k = read int fd ot_der_m add b t sub z t
945if lt k 0
946c = fclose int fd
947ret -1
948end
949if eq k 0
950break
951end
952t = add t ulong k
953end
954c = fclose int fd
955ret ot_x5_pemld b add b t
956end
957
958note ot_x5_pemld: decode every CERTIFICATE block of store [i, e) to the store start and index them.
959note The write position never passes the read position (four characters give at most three bytes).
960fn ot_x5_pemld i ulong e ulong to long
961w = ulong 0
962p = i
963put ot_x5_ps 2 1
964while true bound 16777216
965r = ot_pem_next p e
966if lt r 0
967ot_x5_clear
968put ot_x5_ps 2 0
969ret -2
970end
971if eq r 0
972break
973end
974p = get ot_pem_r 2
975k = ot_b64_dec get ot_pem_r 0 get ot_pem_r 1 w
976if lt k 0
977ot_x5_clear
978put ot_x5_ps 2 0
979ret -2
980end
981n = get ot_x5_st 0
982if ge n ot_x5_mr
983ot_x5_clear
984put ot_x5_ps 2 0
985ret -3
986end
987if ot_x5_parse w add w ulong k n
988put ot_x5_st 0 add n 1
989w = add w ulong k
990else
991put ot_x5_st 5 add get ot_x5_st 5 1
992end
993end
994put ot_x5_ps 2 0
995put ot_x5_st 1 w
996put ot_x5_st 4 w
997ret long get ot_x5_st 0
998end
999
1000note ot_x5_reset: forget the presented chain (the trust store stays)
1001export fn ot_x5_reset to void
1002put ot_x5_st 2 0
1003put ot_x5_st 3 0
1004put ot_x5_st 4 get ot_x5_st 1
1005end
1006
1007note ot_x5_add: the next certificate of the server's Certificate message (leaf first). The number of
1008note certificates held so far, or -1 when it breaks a parsing rule (ot_x5_check then gives 1), -3 when
1009note the store is full (also then 1).
1010export fn ot_x5_add der view uchar * to long
1011n = len der
1012w = get ot_x5_st 4
1013r = add get ot_x5_st 0 get ot_x5_st 2
1014if or gt n sub ot_x5_cap w ge r ot_x5_mr
1015put ot_x5_st 3 add get ot_x5_st 3 1
1016ret -3
1017end
1018for i ulong n bound 16777216
1019put ot_der_m add w i get der i
1020end
1021if eq ot_x5_parse w add w n r false
1022put ot_x5_st 3 add get ot_x5_st 3 1
1023ret -1
1024end
1025put ot_x5_st 2 add get ot_x5_st 2 1
1026put ot_x5_st 4 add w n
1027ret long get ot_x5_st 2
1028end
1029
1030note ot_x5_leafspki: copy the leaf's SubjectPublicKeyInfo DER into o; its length, or -1 (no leaf, or o
1031note too short)
1032export fn ot_x5_leafspki o edit uchar * to long
1033if eq get ot_x5_st 2 0
1034ret -1
1035end
1036r = get ot_x5_st 0
1037n = ot_x5_g r 15
1038if gt n len o
1039ret -1
1040end
1041p = ot_x5_g r 14
1042for i ulong n bound 16777216
1043put o i get ot_der_m add p i
1044end
1045ret long n
1046end
1047
1048note ---- host name ----
1049
1050fn ot_x5_low c uchar to uchar
1051if and ge c 65 le c 90
1052ret add c 32
1053end
1054ret c
1055end
1056
1057note ot_x5_host: take the host into ot_x5_hb / ot_x5_hs; false when it can match nothing
1058fn ot_x5_host h view uchar * to bool
1059n = len h
1060if gt n 0
1061if eq get h sub n 1 46
1062n = sub n 1
1063end
1064end
1065if or eq n 0 gt n 253
1066ret false
1067end
1068dg = true
1069cl = false
1070for i ulong n bound 16777216
1071c = ot_x5_low get h i
1072put ot_x5_hb i c
1073if and ne c 46 or lt c 48 gt c 57
1074dg = false
1075end
1076if eq c 58
1077cl = true
1078end
1079end
1080put ot_x5_hs 0 n
1081if dg
1082put ot_x5_hs 1 1
1083ret ot_x5_ip4 n
1084end
1085if cl
1086put ot_x5_hs 1 2
1087ret ot_x5_ip6 n
1088end
1089put ot_x5_hs 1 0
1090ret ot_x5_dnsok 0 n
1091end
1092
1093note ot_x5_dnsok: ot_x5_hb [o, o + n) is a name of labels: no empty label, no "*"
1094fn ot_x5_dnsok o ulong n ulong to bool
1095if eq n 0
1096ret false
1097end
1098if or eq get ot_x5_hb o 46 eq get ot_x5_hb add o sub n 1 46
1099ret false
1100end
1101for i ulong n bound 16777216
1102c = get ot_x5_hb add o i
1103if eq c 42
1104ret false
1105end
1106if and eq c 46 gt i 0
1107if eq get ot_x5_hb add o sub i 1 46
1108ret false
1109end
1110end
1111end
1112ret true
1113end
1114
1115note ot_x5_ip4: four decimal parts 0..255 without leading zeros into ot_x5_ia
1116fn ot_x5_ip4 n ulong to bool
1117k = ulong 0
1118v = ulong 0
1119d = ulong 0
1120for i ulong add n 1 bound 16777216
1121c = uchar 46
1122if lt i n
1123c = get ot_x5_hb i
1124end
1125if eq c 46
1126if or or eq d 0 ge k 4 gt v 255
1127ret false
1128end
1129put ot_x5_ia k uchar v
1130k = add k 1
1131v = 0
1132d = 0
1133else
1134if and eq d 1 eq v 0
1135ret false
1136end
1137v = add mul v 10 ulong sub c 48
1138d = add d 1
1139if gt d 3
1140ret false
1141end
1142end
1143end
1144ret eq k 4
1145end
1146
1147note ot_x5_hexv: a lower-case hex digit's value, 16 otherwise
1148fn ot_x5_hexv c uchar to ulong
1149if and ge c 48 le c 57
1150ret ulong sub c 48
1151end
1152if and ge c 97 le c 102
1153ret ulong add sub c 97 10
1154end
1155ret 16
1156end
1157
1158note ot_x5_ip6: RFC 4291 2.2 forms 1 and 2 (no embedded IPv4) into ot_x5_ia
1159fn ot_x5_ip6 n ulong to bool
1160g = new ulong 8 0
1161k = ulong 0
1162dc = ulong 8
1163i = ulong 0
1164if and gt n 1 eq get ot_x5_hb 0 58
1165if ne get ot_x5_hb 1 58
1166ret false
1167end
1168dc = 0
1169i = 2
1170end
1171while lt i n bound 256
1172v = ulong 0
1173d = ulong 0
1174while lt i n bound 256
1175x = ot_x5_hexv get ot_x5_hb i
1176if eq x 16
1177break
1178end
1179v = add mul v 16 x
1180d = add d 1
1181i = add i 1
1182end
1183if or or eq d 0 gt d 4 ge k 8
1184ret false
1185end
1186put g k v
1187k = add k 1
1188if lt i n
1189if ne get ot_x5_hb i 58
1190ret false
1191end
1192i = add i 1
1193if ge i n
1194ret false
1195end
1196if eq get ot_x5_hb i 58
1197if ne dc 8
1198ret false
1199end
1200dc = k
1201i = add i 1
1202end
1203end
1204end
1205ret ot_x5_ip6x g k dc
1206end
1207
1208note ot_x5_ip6x: expand k groups with "::" before group dc (8: none) into ot_x5_ia
1209fn ot_x5_ip6x g view ulong 8 k ulong dc ulong to bool
1210if eq dc 8
1211if ne k 8
1212ret false
1213end
1214else
1215if gt k 7
1216ret false
1217end
1218end
1219z = ulong 0
1220if ne dc 8
1221z = sub 8 k
1222end
1223for j ulong 8
1224v = ulong 0
1225if lt j dc
1226v = get g j
1227else
1228if ge j add dc z
1229v = get g sub j z
1230end
1231end
1232put ot_x5_ia mul j 2 uchar shr v 8
1233put ot_x5_ia add mul j 2 1 uchar and v 255
1234end
1235ret true
1236end
1237
1238note ot_x5_ceq: store [v, v + n) equals ot_x5_hb [o, o + n), ASCII case-insensitive
1239fn ot_x5_ceq v ulong o ulong n ulong to bool
1240for i ulong n bound 16777216
1241if ne ot_x5_low get ot_der_m add v i get ot_x5_hb add o i
1242ret false
1243end
1244end
1245ret true
1246end
1247
1248note ot_x5_lbls: store [v, v + n) is at least two labels, none empty, no "*"
1249fn ot_x5_lbls v ulong n ulong to bool
1250if eq n 0
1251ret false
1252end
1253if or eq get ot_der_m v 46 eq get ot_der_m add v sub n 1 46
1254ret false
1255end
1256dots = ulong 0
1257for i ulong n bound 16777216
1258c = get ot_der_m add v i
1259if eq c 42
1260ret false
1261end
1262if eq c 46
1263dots = add dots 1
1264if eq get ot_der_m add v sub i 1 46
1265ret false
1266end
1267end
1268end
1269ret gt dots 0
1270end
1271
1272note ot_x5_dnsm: the dNSName [v, v + l) matches the host
1273fn ot_x5_dnsm v ulong l ulong to bool
1274hn = get ot_x5_hs 0
1275if eq l 0
1276ret false
1277end
1278if eq get ot_der_m v 42
1279if lt l 3
1280ret false
1281end
1282if ne get ot_der_m add v 1 46
1283ret false
1284end
1285if eq ot_x5_lbls add v 2 sub l 2 false
1286ret false
1287end
1288f = ulong 0
1289while lt f hn bound 256
1290if eq get ot_x5_hb f 46
1291break
1292end
1293f = add f 1
1294end
1295if or eq f 0 ge f hn
1296ret false
1297end
1298if ne sub hn add f 1 sub l 2
1299ret false
1300end
1301ret ot_x5_ceq add v 2 add f 1 sub l 2
1302end
1303if ne l hn
1304ret false
1305end
1306ret ot_x5_ceq v 0 l
1307end
1308
1309note ot_x5_ipm: the iPAddress [v, v + l) equals the host address
1310fn ot_x5_ipm v ulong l ulong to bool
1311n = ulong 4
1312if eq get ot_x5_hs 1 2
1313n = 16
1314end
1315if ne l n
1316ret false
1317end
1318for i ulong n bound 16777216
1319if ne get ot_der_m add v i get ot_x5_ia i
1320ret false
1321end
1322end
1323ret true
1324end
1325
1326note ot_x5_nmok: a SAN entry of record r matches the host
1327fn ot_x5_nmok r ulong to bool
1328if eq ot_x5_has r 128 false
1329ret false
1330end
1331k = ot_x5_g r 16
1332e = add k ot_x5_g r 17
1333w = get ot_x5_hs 1
1334while lt k e bound 16777216
1335if eq ot_der_rd k e false
1336ret false
1337end
1338t = ot_der_tg
1339m = ot_der_nd
1340if and eq w 0 eq t 130
1341if ot_x5_dnsm ot_der_vo ot_der_vl
1342ret true
1343end
1344end
1345if and ne w 0 eq t 135
1346if ot_x5_ipm ot_der_vo ot_der_vl
1347ret true
1348end
1349end
1350k = m
1351end
1352ret false
1353end
1354
1355note ---- path checks ----
1356
1357note ot_x5_tok: record r is valid at now
1358fn ot_x5_tok r ulong to bool
1359t = get ot_x5_nw 0
1360if lt t get ot_x5_tm mul r 2
1361ret false
1362end
1363ret le t get ot_x5_tm add mul r 2 1
1364end
1365
1366note ot_x5_iss: record x can have issued record c
1367fn ot_x5_iss x ulong c ulong to bool
1368n = ot_x5_g c 9
1369if ne n ot_x5_g x 11
1370ret false
1371end
1372if eq ot_der_eq ot_x5_g c 8 ot_x5_g x 10 n false
1373ret false
1374end
1375a = ot_x5_g c 21
1376b = ot_x5_g x 19
1377if or eq a 0 eq b 0
1378ret true
1379end
1380if ne a b
1381ret false
1382end
1383ret ot_der_eq ot_x5_g c 20 ot_x5_g x 18 a
1384end
1385
1386note ot_x5_sig: the signature of record c under the key of record x: 0, 5 or 8 (ot_pk_csig)
1387fn ot_x5_sig c ulong x ulong to ulong
1388put ot_pk_q 0 ot_x5_g c 4
1389put ot_pk_q 1 ot_x5_g c 5
1390put ot_pk_q 2 ot_x5_g c 2
1391put ot_pk_q 3 sub ot_x5_g c 3 ot_x5_g c 2
1392put ot_pk_q 4 ot_x5_g c 6
1393put ot_pk_q 5 ot_x5_g c 7
1394put ot_pk_q 6 ot_x5_g x 14
1395put ot_pk_q 7 ot_x5_g x 15
1396ret ot_pk_csig
1397end
1398
1399note ot_x5_dcm: the dNSName constraint [c, c + cl) matches the name [n, n + nl) (store, case-insensitive)
1400fn ot_x5_dcm c ulong cl ulong n ulong nl ulong to bool
1401if eq cl 0
1402ret true
1403end
1404if lt nl cl
1405ret false
1406end
1407o = add n sub nl cl
1408for i ulong cl bound 16777216
1409if ne ot_x5_low get ot_der_m add c i ot_x5_low get ot_der_m add o i
1410ret false
1411end
1412end
1413if eq get ot_der_m c 46
1414ret gt nl cl
1415end
1416if eq nl cl
1417ret true
1418end
1419ret eq get ot_der_m sub o 1 46
1420end
1421
1422note ot_x5_ncany: the subtree list [a, j) has a plain dNSName subtree; with n, nl: one matching that name
1423fn ot_x5_ncany a ulong j ulong n ulong nl ulong any bool to bool
1424k = a
1425while lt k j bound 16777216
1426if eq ot_der_ex k j 48 false
1427ret false
1428end
1429m = ot_der_nd
1430if ot_der_rd ot_der_vo m
1431if and eq ot_der_tg 130 eq ot_der_nd m
1432if any
1433ret true
1434end
1435if ot_x5_dcm ot_der_vo ot_der_vl n nl
1436ret true
1437end
1438end
1439end
1440k = m
1441end
1442ret false
1443end
1444
1445note ot_x5_ncl: the leaf lf and the host against one subtree list [a, j); ex: excluded, else permitted
1446fn ot_x5_ncl a ulong j ulong lf ulong ex bool to bool
1447if eq ex false
1448if eq ot_x5_ncany a j 0 0 true false
1449ret true
1450end
1451end
1452if eq ot_x5_nchok a j ex false
1453ret false
1454end
1455k = ot_x5_g lf 16
1456e = add k ot_x5_g lf 17
1457while lt k e bound 16777216
1458if eq ot_der_rd k e false
1459ret false
1460end
1461m = ot_der_nd
1462if eq ot_der_tg 130
1463v = ot_der_vo
1464l = ot_der_vl
1465h = ot_x5_ncany a j v l false
1466if eq h ex
1467ret false
1468end
1469if ex
1470if ot_x5_ncwb a j v l
1471ret false
1472end
1473end
1474end
1475k = m
1476end
1477ret true
1478end
1479
1480note ot_x5_dch: the dNSName constraint [c, c + cl) (store) matches the host (ot_x5_hb, lower case)
1481fn ot_x5_dch c ulong cl ulong to bool
1482hn = get ot_x5_hs 0
1483if eq cl 0
1484ret true
1485end
1486if lt hn cl
1487ret false
1488end
1489o = sub hn cl
1490for i ulong cl bound 16777216
1491if ne ot_x5_low get ot_der_m add c i get ot_x5_hb add o i
1492ret false
1493end
1494end
1495if eq get ot_der_m c 46
1496ret gt hn cl
1497end
1498if eq hn cl
1499ret true
1500end
1501ret eq get ot_x5_hb sub o 1 46
1502end
1503
1504note ot_x5_nchst: a plain dNSName subtree of the list [a, j) matches the host
1505fn ot_x5_nchst a ulong j ulong to bool
1506k = a
1507while lt k j bound 16777216
1508if eq ot_der_ex k j 48 false
1509ret false
1510end
1511m = ot_der_nd
1512if ot_der_rd ot_der_vo m
1513if and eq ot_der_tg 130 eq ot_der_nd m
1514if ot_x5_dch ot_der_vo ot_der_vl
1515ret true
1516end
1517end
1518end
1519k = m
1520end
1521ret false
1522end
1523
1524note ot_x5_nchok: a DNS host satisfies the subtree list [a, j) (excluded: matches none; permitted: the
1525note caller has found a dNSName subtree, the host matches one); an IP host is not a dNSName
1526fn ot_x5_nchok a ulong j ulong ex bool to bool
1527if ne get ot_x5_hs 1 0
1528ret true
1529end
1530h = ot_x5_nchst a j
1531if ex
1532ret eq h false
1533end
1534ret h
1535end
1536
1537note ot_x5_below: the constraint [c, c + cl) without a leading dot is a name strictly below the name
1538note [d, d + dl) (store, case-insensitive)
1539fn ot_x5_below c ulong cl ulong d ulong dl ulong to bool
1540x = c
1541n = cl
1542if gt n 0
1543if eq get ot_der_m x 46
1544x = add x 1
1545n = sub n 1
1546end
1547end
1548if le n dl
1549ret false
1550end
1551o = add x sub n dl
1552if ne get ot_der_m sub o 1 46
1553ret false
1554end
1555for i ulong dl bound 16777216
1556if ne ot_x5_low get ot_der_m add o i ot_x5_low get ot_der_m add d i
1557ret false
1558end
1559end
1560ret true
1561end
1562
1563note ot_x5_ncwb: the SAN [v, v + l) is a wildcard "*.D" and an excluded plain dNSName subtree of the
1564note list [a, j) lies strictly below D
1565fn ot_x5_ncwb a ulong j ulong v ulong l ulong to bool
1566if lt l 3
1567ret false
1568end
1569if or ne get ot_der_m v 42 ne get ot_der_m add v 1 46
1570ret false
1571end
1572k = a
1573while lt k j bound 16777216
1574if eq ot_der_ex k j 48 false
1575ret false
1576end
1577m = ot_der_nd
1578if ot_der_rd ot_der_vo m
1579if and eq ot_der_tg 130 eq ot_der_nd m
1580if ot_x5_below ot_der_vo ot_der_vl add v 2 sub l 2
1581ret true
1582end
1583end
1584end
1585k = m
1586end
1587ret false
1588end
1589
1590note ot_x5_ncok: the leaf's dNSName SANs satisfy the nameConstraints of record r
1591fn ot_x5_ncok r ulong lf ulong to bool
1592i = ot_x5_g r 25
1593e = add i ot_x5_g r 26
1594while lt i e bound 16777216
1595if eq ot_der_rd i e false
1596ret false
1597end
1598j = ot_der_nd
1599if eq ot_x5_ncl ot_der_vo j lf eq ot_der_tg 161 false
1600ret false
1601end
1602i = j
1603end
1604ret true
1605end
1606
1607note ot_x5_ca: issuer record r at path position i (1 = issues the leaf) meets its constraints
1608fn ot_x5_ca r ulong i ulong lf ulong to bool
1609if or eq ot_x5_has r 1 false eq ot_x5_has r 2 false
1610ret false
1611end
1612if gt sub i 1 ot_x5_g r 23
1613ret false
1614end
1615if ot_x5_has r 4
1616if eq and ot_x5_g r 24 1024 0
1617ret false
1618end
1619end
1620if ot_x5_has r 256
1621if eq ot_x5_ncok r lf false
1622ret false
1623end
1624end
1625ret true
1626end
1627
1628note ot_x5_cons: constraints (code 6) of the path ot_x5_pth [0 .. m]
1629fn ot_x5_cons m ulong to bool
1630lf = get ot_x5_pth 0
1631if ot_x5_has lf 4
1632if eq and ot_x5_g lf 24 32768 0
1633ret false
1634end
1635end
1636for i ulong m bound 16777216
1637r = get ot_x5_pth i
1638if ot_x5_has r 8
1639if eq ot_x5_has r 16 false
1640ret false
1641end
1642end
1643if eq ot_x5_ca get ot_x5_pth add i 1 add i 1 lf false
1644ret false
1645end
1646end
1647ret true
1648end
1649
1650note ot_x5_pev: the score of the path ot_x5_pth [0 .. m] (m: the trust anchor)
1651fn ot_x5_pev m ulong to ulong
1652for i ulong add m 1 bound 16777216
1653if eq ot_x5_tok get ot_x5_pth i false
1654ret 3
1655end
1656end
1657c = ulong 0
1658for i ulong m bound 16777216
1659s = ot_x5_sig get ot_x5_pth i get ot_x5_pth add i 1
1660if eq s 5
1661ret 5
1662end
1663if ne s 0
1664c = s
1665end
1666end
1667if eq ot_x5_cons m false
1668ret 6
1669end
1670for i ulong add m 1 bound 16777216
1671if ne and ot_x5_g get ot_x5_pth i 22 96 0
1672ret 7
1673end
1674end
1675for i ulong add m 1 bound 16777216
1676r = get ot_x5_pth i
1677if ne ot_pk_kok ot_x5_g r 14 ot_x5_g r 15 0
1678ret 8
1679end
1680end
1681ret c
1682end
1683
1684note ot_x5_cand: the t-th candidate issuer: presented certificates first, then the trust store
1685fn ot_x5_cand t ulong to ulong
1686nr = get ot_x5_st 0
1687np = get ot_x5_st 2
1688if lt t np
1689ret add nr t
1690end
1691ret sub t np
1692end
1693
1694note ot_x5_dfs: depth-first search from the leaf; the best path score, 4 when there is no path
1695fn ot_x5_dfs lf ulong to ulong
1696na = add get ot_x5_st 0 get ot_x5_st 2
1697best = ulong 99
1698d = ulong 0
1699n = ulong 0
1700put ot_x5_pth 0 lf
1701put ot_x5_cur 0 0
1702put ot_x5_on lf 1
1703while true bound 4611686018427387904
1704c = get ot_x5_pth d
1705t = get ot_x5_cur d
1706if ge t na
1707put ot_x5_on c 0
1708if eq d 0
1709break
1710end
1711d = sub d 1
1712else
1713put ot_x5_cur d add t 1
1714n = add n 1
1715if gt n 1000000
1716break
1717end
1718x = ot_x5_cand t
1719if eq get ot_x5_on x 0
1720if ot_x5_iss x c
1721if lt x get ot_x5_st 0
1722put ot_x5_pth add d 1 x
1723s = ot_x5_pev add d 1
1724if lt s best
1725best = s
1726end
1727if eq best 0
1728break
1729end
1730else
1731d = add d 1
1732put ot_x5_pth d x
1733put ot_x5_cur d 0
1734put ot_x5_on x 1
1735end
1736end
1737end
1738end
1739end
1740for i ulong add d 1 bound 16777216
1741put ot_x5_on get ot_x5_pth i 0
1742end
1743if eq best 99
1744ret 4
1745end
1746ret best
1747end
1748
1749note ot_x5_check: validate the presented chain for host at now (Unix seconds); 0 or the code
1750export fn ot_x5_check host view uchar * now long to int
1751if eq add get ot_x5_st 2 get ot_x5_st 3 0
1752ret 9
1753end
1754if gt get ot_x5_st 3 0
1755ret 1
1756end
1757put ot_x5_nw 0 now
1758lf = get ot_x5_st 0
1759if eq ot_x5_host host false
1760ret 2
1761end
1762if eq ot_x5_nmok lf false
1763ret 2
1764end
1765if eq ot_x5_tok lf false
1766ret 3
1767end
1768ret int ot_x5_dfs lf
1769end