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 / apps / aienos-site / om / agora / tls / x509.om

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