interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / tls / rev.om

1768 lines · 41184 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/tls/rev.om (module RV): certificate revocation for the om/tls client. SHA-1 (FIPS 180-4, only
3note for OCSP CertID and KeyHash, never for a signature), stapled OCSP (RFC 6960 BasicOCSPResponse,
4note RFC 8446 4.4.2.1 status_request in the leaf CertificateEntry), the TLS Feature extension
5note (RFC 7633 must-staple) and CRL validation against a caller-supplied CRL (RFC 5280 5, 6.3).
6note Library unit; compose after x509.om (it reuses der_hdr, der_time, x509_parse, chain_sig,
7note chain_link, chain_time, chain_index, x509_errname). No globals.
8note
9note Inputs (rev_check, rev_ocsp, rev_crl): one buffer b and a descriptor p (ulong 16): p[0] unix now,
10note p[1] p[2] leaf DER offset and length in b, p[3] p[4] the leaf issuer's DER (rev_issuer finds it),
11note p[5] p[6] the stapled OCSPResponse (length 0: none), p[7] p[8] a CRL (length 0: none).
12note
13note Reject codes continue the x509.om table (rev_errname gives the names; 1..21 are x509.om's):
14note 32 REVOKED the OCSP response or the CRL lists the leaf as revoked
15note 33 STATUS_UNKNOWN the OCSP response says unknown, or has no SingleResponse for the leaf
16note 34 STALE thisUpdate later than now + RVSKEW, nextUpdate absent, or nextUpdate <= now
17note 35 NO_STAPLE the leaf carries TLS Feature status_request (must-staple) and no staple came
18note 36 RESPONDER the OCSP signer is neither the issuer nor a certificate issued by it, valid now,
19note with extKeyUsage id-kp-OCSPSigning; or the responderID names nobody presented
20note 37 OCSP_STATUS responseStatus is not successful (malformedRequest, tryLater, ...)
21note 38 CRL_SCOPE the CRL does not cover the leaf: issuer name differs, delta CRL, indirect CRL,
22note onlyContainsCACerts / onlySomeReasons / onlyContainsAttributeCerts, a relative
23note distribution point name, or an issuingDistributionPoint none of whose fullName
24note names is in the leaf's cRLDistributionPoints
25note x509.om codes used here: 1 MALFORMED, 6 BAD_SIGNATURE, 9 KEY_USAGE (issuer keyUsage without cRLSign), 11/12 algorithm or key policy
26note (chain_sig), 13 CRITICAL_EXT, 18 ALG_MISMATCH.
27note
28note Policy (rev_check): a staple, when present, must verify and say good; a must-staple leaf without a
29note staple is refused; a CRL, when given, must verify, cover the leaf, be fresh and not list it. With
30note no staple, no must-staple and no CRL the result is 0: revocation was then NOT checked (soft fail
31note open, as every browser does without must-staple), which the caller reports. Nothing here is
32note secret; variable time by design.
33note
34note omc limits shape the code: a call carries at most 6 slots (an unsized array is 2, a sized one 1)
35note and never names one array twice, so the leaf and the issuer are copied into local buffers and
36note intermediate offsets travel in the sized state arrays o (ulong 32), w (ulong 128), ix (uchar 256).
37
38const RVEREV ulong 32
39const RVEUNK ulong 33
40const RVESTALE ulong 34
41const RVENOST ulong 35
42const RVERESP ulong 36
43const RVEOST ulong 37
44const RVESCOPE ulong 38
45note RVSKEW: seconds a thisUpdate may lie in the future (clock skew between responder and client)
46const RVSKEW ulong 300
47note RVANY: rev_exts skips the criticality rule; RVNONE: no extension may be critical / none wanted
48const RVANY ulong 99
49const RVNONE ulong 98
50note RVHIT / RVMISS: "found" (must-staple, serial listed) and "absent / names another certificate"
51note results, distinct from every error code (X5EMAL is 1)
52const RVHIT ulong 97
53const RVMISS ulong 96
54const RVKBUF ulong 16896
55note RVSER: serial number bytes of the leaf live at ix[RVSER ..), at most RVSERX of them
56const RVSER ulong 128
57const RVSERX ulong 128
58
59note RVOIDS: OID content bytes (hex), space separated, 1-based index by rev_oidin:
60note 1 id-pe-tlsfeature 1.3.6.1.5.5.7.1.24 (RFC 7633), 2 cRLDistributionPoints, 3 issuingDistributionPoint,
61note 4 id-kp-OCSPSigning, 5 id-pkix-ocsp-basic, 6 id-sha1 1.3.14.3.2.26, 7 id-sha256, 8 extKeyUsage,
62note 9 cRLNumber, 10 authorityKeyIdentifier, 11 deltaCRLIndicator, 12 freshestCRL, 13 reasonCode,
63note 14 invalidityDate, 15 certificateIssuer, 16 id-pkix-ocsp-nonce, 17 authorityInfoAccess
64text RVOIDS "2b06010505070118 551d1f 551d1c 2b06010505070309 2b0601050507300101 2b0e03021a 608648016503040201 551d25 551d14 551d23 551d1b 551d2e 551d15 551d18 551d1d 2b0601050507300102 2b06010505070101 "
65text RVNAMES "REV_ERR_REVOKED REV_ERR_STATUS_UNKNOWN REV_ERR_STALE REV_ERR_NO_STAPLE REV_ERR_RESPONDER REV_ERR_OCSP_STATUS REV_ERR_CRL_SCOPE "
66
67note ---------------------------------------------------------------- SHA-1 (CertID only)
68
69note sha1_f: round function and constant of SHA-1 step t (FIPS 180-4 4.1.1, 4.2.1), k selects
70fn sha1_f t ulong b uint c uint d uint to uint
71if lt t 20
72ret or and b c and not b d
73end
74if lt t 40
75ret xor xor b c d
76end
77if lt t 60
78ret or or and b c and b d and c d
79end
80ret xor xor b c d
81end
82
83fn sha1_k t ulong to uint
84if lt t 20
85ret 0x5a827999
86end
87if lt t 40
88ret 0x6ed9eba1
89end
90if lt t 60
91ret 0x8f1bbcdc
92end
93ret 0xca62c1d6
94end
95
96note sha1_sched: message schedule w[0 .. 80) of the block m[o .. o+64)
97fn sha1_sched w edit uint 80 m view uchar * o ulong to void
98for t ulong 16
99q = add o mul t 4
100v = or shl uint get m q 24 shl uint get m add q 1 16
101v = or v or shl uint get m add q 2 8 uint get m add q 3
102put w t v
103end
104for t ulong 64
105i = add t 16
106x = xor get w sub i 3 get w sub i 8
107x = xor x get w sub i 14
108x = xor x get w sub i 16
109put w i or shl x 1 shr x 31
110end
111end
112
113note sha1_blk: one SHA-1 compression of m[o .. o+64) into the chaining value h[0 .. 5)
114fn sha1_blk h edit uint 5 m view uchar * o ulong to void
115w = new uint 80 0
116sha1_sched w m o
117a = get h 0
118b = get h 1
119c = get h 2
120d = get h 3
121e = get h 4
122for t ulong 80
123f = sha1_f t b c d
124x = add or shl a 5 shr a 27 f
125x = add x add e sha1_k t
126x = add x get w t
127e = d
128d = c
129c = or shl b 30 shr b 2
130b = a
131a = x
132end
133put h 0 add get h 0 a
134put h 1 add get h 1 b
135put h 2 add get h 2 c
136put h 3 add get h 3 d
137put h 4 add get h 4 e
138end
139
140note sha1_pad: the final block(s) after r tail bytes already in t: 0x80, zeros, the 64-bit bit
141note length of an n-byte message; returns the padded length (64 or 128)
142fn sha1_pad t edit uchar 128 n ulong r ulong to ulong
143put t r 128
144tl = ulong 64
145if ge r 56
146tl = 128
147end
148bits = mul n 8
149for i ulong 8
150q = sub sub tl 1 i
151put t q uchar and shr bits mul i 8 255
152end
153ret tl
154end
155
156note sha1_out: the chaining value h big-endian into o[0 .. 20)
157fn sha1_out h view uint 5 o edit uchar * to void
158for i ulong 5
159v = get h i
160q = mul i 4
161put o q uchar shr v 24
162put o add q 1 uchar and shr v 16 255
163put o add q 2 uchar and shr v 8 255
164put o add q 3 uchar and v 255
165end
166end
167
168note sha1_digest: SHA-1 of m (any length below 2^61 bytes) into o[0 .. 20); returns 20
169fn sha1_digest m view uchar * o edit uchar * to ulong
170h = new uint 5 0
171t = new uchar 128 0
172put h 0 0x67452301
173put h 1 0xefcdab89
174put h 2 0x98badcfe
175put h 3 0x10325476
176put h 4 0xc3d2e1f0
177n = len m
178nb = div n 64
179for i ulong nb
180sha1_blk h m mul i 64
181end
182r = sub n mul nb 64
183for i ulong r
184put t i get m add mul nb 64 i
185end
186tl = sha1_pad t n r
187sha1_blk h t 0
188if eq tl 128
189sha1_blk h t 64
190end
191sha1_out h o
192ret 20
193end
194
195note rev_hash: digest of m with OCSP CertID hash a (6 SHA-1, 7 SHA-256, rev_oidin) into o; length
196fn rev_hash a ulong m view uchar * o edit uchar * to ulong
197if eq a 6
198ret sha1_digest m o
199end
200ret sha2_digest 256 m o
201end
202
203note ---------------------------------------------------------------- DER helpers
204
205note rev_tag: der_hdr of the TLV at d[p] (content by e) whose tag must be g; 0 ok, 1 otherwise
206fn rev_tag d view uchar * p ulong e ulong h edit ulong 3 g ulong to ulong
207if ne der_hdr d p e h 0
208ret 1
209end
210if ne get h 0 g
211ret 1
212end
213ret 0
214end
215
216note rev_oidin: position (1-based) of the OID content d in RVOIDS, 0 when absent
217fn rev_oidin d view uchar * to ulong
218s = ulong 0
219k = ulong 1
220n = len RVOIDS
221while lt s n bound 1000
222j = s
223while lt j n bound 1000
224if eq get RVOIDS j 32
225break
226end
227j = add j 1
228end
229if der_hexeq d part RVOIDS s sub j s
230ret k
231end
232s = add j 1
233k = add k 1
234end
235ret 0
236end
237
238note rev_ext1: one Extension, content c[a .. b) (RFC 5280 4.1): extnID, critical BOOLEAN (0x00 or 0xff,
239note as x509_ext), extnValue. y[0] critical 0/1, y[1] y[2] extnValue content span, y[3] OID index
240note (rev_oidin). 0 ok, 1 malformed.
241fn rev_ext1 c view uchar * a ulong b ulong y edit ulong 4 to ulong
242h = new ulong 3 0
243if ne rev_tag c a b h 6 0
244ret 1
245end
246put y 3 rev_oidin part c get h 1 sub get h 2 get h 1
247put y 0 0
248s = get h 2
249if ne der_hdr c s b h 0
250ret 1
251end
252if eq get h 0 1
253if ne sub get h 2 get h 1 1
254ret 1
255end
256v = ulong get c get h 1
257if and ne v 0 ne v 255
258ret 1
259end
260if eq v 255
261put y 0 1
262end
263s = get h 2
264if ne der_hdr c s b h 0
265ret 1
266end
267end
268if or ne get h 0 4 ne get h 2 b
269ret 1
270end
271put y 1 get h 1
272put y 2 b
273ret 0
274end
275
276note rev_extk: criticality rule for the parsed extension y: x[13] is the one OID index that may be
277note critical (RVNONE: none; RVANY: skip the rule). deltaCRLIndicator and certificateIssuer are
278note RVESCOPE (delta and indirect CRLs are not supported); any other critical extension X5ECRIT.
279fn rev_extk y view ulong 4 x view ulong 16 to ulong
280ok = get x 13
281if or eq ok RVANY eq get y 0 0
282ret 0
283end
284o = get y 3
285if and eq o ok ne o 0
286ret 0
287end
288if or eq o 11 eq o 15
289ret RVESCOPE
290end
291ret X5ECRIT
292end
293
294note rev_exts: Extensions SEQUENCE TLV that is exactly c[a .. b) (1..X5XMAX entries). The extension
295note with OID index x[12] is recorded: x[4] 1, x[5] critical, x[6] x[7] its extnValue content span (a
296note second one is X5EMAL). Criticality per rev_extk.
297fn rev_exts c view uchar * a ulong b ulong x edit ulong 16 to ulong
298h = new ulong 3 0
299y = new ulong 4 0
300put x 4 0
301if ne rev_tag c a b h 0x30 0
302ret X5EMAL
303end
304if or ne get h 2 b eq get h 1 b
305ret X5EMAL
306end
307s = get h 1
308n = ulong 0
309while lt s b bound 100
310n = add n 1
311if gt n X5XMAX
312ret X5EMAL
313end
314if ne rev_tag c s b h 0x30 0
315ret X5EMAL
316end
317s = get h 2
318if ne rev_ext1 c get h 1 s y 0
319ret X5EMAL
320end
321k = rev_extk y x
322if ne k 0
323ret k
324end
325if eq get y 3 get x 12
326if ne get x 4 0
327ret X5EMAL
328end
329put x 4 1
330put x 5 get y 0
331put x 6 get y 1
332put x 7 get y 2
333end
334end
335ret 0
336end
337
338note rev_certx: in the certificate c (record r from x509_parse): its serialNumber INTEGER content span
339note into x[10] x[11], and its Extensions SEQUENCE TLV into x[8] x[9] (both 0 when it has none).
340note 0 ok, X5EMAL.
341fn rev_certx c view uchar * r view ulong * x edit ulong 16 to ulong
342h = new ulong 3 0
343put x 8 0
344put x 9 0
345a = get r X5RTBO
346if ne rev_tag c a len c h 0x30 0
347ret X5EMAL
348end
349b = get h 2
350s = get h 1
351if ne der_hdr c s b h 0
352ret X5EMAL
353end
354if eq get h 0 0xa0
355s = get h 2
356if ne der_hdr c s b h 0
357ret X5EMAL
358end
359end
360if ne get h 0 2
361ret X5EMAL
362end
363put x 10 get h 1
364put x 11 get h 2
365s = get h 2
366while lt s b bound 16
367if ne der_hdr c s b h 0
368ret X5EMAL
369end
370if eq get h 0 0xa3
371s = get h 1
372if ne der_hdr c s get h 2 h 0
373ret X5EMAL
374end
375put x 8 s
376put x 9 get h 2
377ret 0
378end
379s = get h 2
380end
381ret 0
382end
383
384note rev_certext: extension with OID index x[12] of the certificate c (record r): 0 found (x[6] x[7]
385note value span), RVMISS absent, X5EMAL. x509_parse already policed the certificate's criticality.
386fn rev_certext c view uchar * r view ulong * x edit ulong 16 to ulong
387v = rev_certx c r x
388if ne v 0
389ret v
390end
391if eq get x 9 0
392ret RVMISS
393end
394put x 13 RVANY
395v = rev_exts c get x 8 get x 9 x
396if ne v 0
397ret v
398end
399if eq get x 4 0
400ret RVMISS
401end
402ret 0
403end
404
405note ---------------------------------------------------------------- must-staple (RFC 7633)
406
407note rev_mustst: the certificate c: RVHIT when its TLS Feature extension lists status_request (5), 0 when
408note it has none or the list lacks 5, X5EMAL when the certificate or the extension is malformed
409note (Features ::= SEQUENCE OF INTEGER, each 0..65535, at least one)
410fn rev_mustst c view uchar * to ulong
411r = new ulong 32 0
412x = new ulong 16 0
413h = new ulong 3 0
414if ne x509_parse c r 0
415ret X5EMAL
416end
417put x 12 1
418v = rev_certext c r x
419if eq v RVMISS
420ret 0
421end
422if ne v 0
423ret v
424end
425b = get x 7
426if ne rev_tag c get x 6 b h 0x30 0
427ret X5EMAL
428end
429if or ne get h 2 b eq get h 1 b
430ret X5EMAL
431end
432s = get h 1
433m = ulong 0
434while lt s b bound 1000
435if ne rev_tag c s b h 2 0
436ret X5EMAL
437end
438f = der_small part c s sub get h 2 s
439if gt f 65535
440ret X5EMAL
441end
442if eq f 5
443m = RVHIT
444end
445s = get h 2
446end
447ret m
448end
449
450note ---------------------------------------------------------------- shared state and setup
451
452note State o (ulong 32): 0 now (x509 seconds), 1 signed TLV start (ResponseData or tbsCertList), 2 its
453note length, 3 signature algorithm (x509_algid), 4 signature value start, 5 its length. OCSP: 6 7
454note certs content span (0 0 none), 8 responderID tag (0xa1 byName, 0xa2 byKey), 9 10 its content
455note span, 11 12 responses content span, 13 14 responseExtensions span, 16 status (0 good, 1 revoked,
456note 2 unknown), 17 thisUpdate, 18 nextUpdate (X5BIG none), 20 21 BasicOCSPResponse content span, 24
457note 25 26 27 candidate signer subject and key spans, 28 29 CertID content span, 30 31 SingleResponse
458note span. CRL: 11 12 revokedCertificates content span, 13 14 crlExtensions SEQUENCE span, 17 18 as
459note above, 24 25 tbsCertList content span, 26 27 outer signatureAlgorithm TLV start and length, 28 29
460note issuer Name TLV start and length, 30 31 issuingDistributionPoint fullName content span (0 0
461note none). Both: 22 leaf serial length (bytes at ix[RVSER ..)).
462note Workspace w (ulong 128): w[0 .. 32) and w[32 .. 64) are x509 records (leaf or candidate, issuer
463note or signer: the chain_link layout), 64 leaf length, 65 issuer length, 66 now.
464note ix (uchar 256): CertID hashes of the issuer (rev_ids) at 0 .. 104, the leaf serial at RVSER.
465
466note rev_now: unix seconds nw as x509 seconds (x509_chain_verify's scale); X5BIG beyond 9999
467fn rev_now nw ulong to ulong
468if gt nw 253402300799
469ret X5BIG
470end
471ret add nw mul x509_days 1970 1 1 86400
472end
473
474note rev_span: p[i] .. p[i] + p[i+1] lies within b
475fn rev_span b view uchar * p view ulong 16 i ulong to bool
476a = get p i
477if gt a len b
478ret false
479end
480ret le get p add i 1 sub len b a
481end
482
483note rev_load: the certificate p[k] .. p[k] + p[k+1] of b copied into d and parsed into the x509
484note record w[j*32 .. j*32+32) (j = (k-1)/2), its length into w[64+j]. 0 or X5EMAL.
485fn rev_load b view uchar * p view ulong 16 k ulong d edit uchar 16896 w edit ulong 128 to ulong
486if not rev_span b p k
487ret X5EMAL
488end
489l = get p add k 1
490if or eq l 0 gt l RVKBUF
491ret X5EMAL
492end
493a = get p k
494for i ulong l
495put d i get b add a i
496end
497a = mul div sub k 1 2 X5RSZ
498put w add 64 div a X5RSZ l
499if ne x509_parse part d 0 l part w a X5RSZ 0
500ret X5EMAL
501end
502ret 0
503end
504
505note rev_setup: now into w[66]; the leaf and the issuer (p[1 .. 4]) copied into lb and ib and parsed
506note into w[0 .. 32) and w[32 .. 64); their lengths into w[64] w[65]. 0 or X5EMAL.
507fn rev_setup b view uchar * p view ulong 16 lb edit uchar 16896 ib edit uchar 16896 w edit ulong 128 to ulong
508nw = rev_now get p 0
509if eq nw X5BIG
510ret X5EMAL
511end
512put w 66 nw
513if ne rev_load b p 1 lb w 0
514ret X5EMAL
515end
516ret rev_load b p 3 ib w
517end
518
519note rev_serial: the leaf serial (INTEGER content, 1 .. RVSERX bytes) into ix[RVSER ..), length o[22]
520fn rev_serial lb view uchar 16896 w view ulong 128 ix edit uchar 256 o edit ulong 32 to ulong
521x = new ulong 16 0
522v = rev_certx part lb 0 get w 64 part w 0 X5RSZ x
523if ne v 0
524ret v
525end
526a = get x 10
527n = sub get x 11 a
528if or eq n 0 gt n RVSERX
529ret X5EMAL
530end
531for i ulong n
532put ix add RVSER i get lb add a i
533end
534put o 22 n
535ret 0
536end
537
538note rev_sereq: the INTEGER content d[a .. b) is the leaf serial (n bytes at t[RVSER ..))
539fn rev_sereq d view uchar * a ulong b ulong t view uchar 256 n ulong to bool
540if ne sub b a n
541ret false
542end
543for i ulong n
544if ne get d add a i get t add RVSER i
545ret false
546end
547end
548ret true
549end
550
551note rev_sig: the signature over d[o[1] .. +o[2]) (algorithm o[3], value o[4] .. +o[5]) with the key of
552note kd (record w[32 .. 64)); w[0 .. 32) becomes the pseudo-record chain_sig reads. chain_sig applies
553note x509.om's algorithm and key policy. 0, X5ESIG, X5EALG, X5EWEAK.
554fn rev_sig d view uchar * kd view uchar * w edit ulong 128 o view ulong 32 to ulong
555put w X5RALG get o 3
556put w X5RTBO get o 1
557put w X5RTBL get o 2
558put w X5RSGO get o 4
559put w X5RSGL get o 5
560ret chain_sig d kd part w 0 mul X5RSZ 2
561end
562
563note rev_fresh: thisUpdate o[17] <= now + RVSKEW, nextUpdate o[18] present and later than now (RFC 6960
564note 4.2.2.1; RFC 5280 6.3.3 a.1 and a.2); 0 or RVESTALE
565fn rev_fresh o view ulong 32 to ulong
566nw = get o 0
567if gt get o 17 add nw RVSKEW
568ret RVESTALE
569end
570if eq get o 18 X5BIG
571ret RVESTALE
572end
573if le get o 18 nw
574ret RVESTALE
575end
576ret 0
577end
578
579note ---------------------------------------------------------------- OCSP (RFC 6960)
580
581note rev_ids: the CertID hashes of the issuer ib (record w[32 .. 64)) into ix: SHA-1 of its subject
582note Name DER at 0, of its subjectPublicKey bits at 20; SHA-256 of the same at 40 and 72 (RFC 6960 4.1.1)
583fn rev_ids ib view uchar 16896 w view ulong 128 ix edit uchar 256 to void
584d = new uchar 32 0
585for k ulong 2
586a = add ulong 6 k
587n = rev_hash a part ib get w add X5RSZ X5RSBO get w add X5RSZ X5RSBL d
588b = mul k 40
589for i ulong n
590put ix add b i get d i
591end
592n = rev_hash a part ib get w add X5RSZ X5RKO get w add X5RSZ X5RKL d
593b = add b n
594for i ulong n
595put ix add b i get d i
596end
597end
598end
599
600note rev_octeq: the OCTET STRING TLV at d[p] (by e) holds the hash t[q ..) (20 bytes below q = 40,
601note else 32); its end offset when it does, else 0
602fn rev_octeq d view uchar * p ulong e ulong t view uchar 256 q ulong to ulong
603h = new ulong 3 0
604n = ulong 20
605if ge q 40
606n = 32
607end
608if ne rev_tag d p e h 4 0
609ret 0
610end
611if ne sub get h 2 get h 1 n
612ret 0
613end
614s = get h 1
615for i ulong n
616if ne get d add s i get t add q i
617ret 0
618end
619end
620ret get h 2
621end
622
623note rev_hashalg: AlgorithmIdentifier TLV that is exactly rs[a .. s): 6 SHA-1 or 7 SHA-256, parameters
624note absent or NULL; 0 for anything else
625fn rev_hashalg rs view uchar * a ulong s ulong to ulong
626h = new ulong 3 0
627if ne rev_tag rs a s h 0x30 0
628ret 0
629end
630if ne get h 2 s
631ret 0
632end
633if ne rev_tag rs get h 1 s h 6 0
634ret 0
635end
636o = rev_oidin part rs get h 1 sub get h 2 get h 1
637q = get h 2
638if ne q s
639if not der_null rs q s
640ret 0
641end
642end
643if or eq o 6 eq o 7
644ret o
645end
646ret 0
647end
648
649note rev_certid: CertID content rs[o[28] .. o[29]): 0 when it names the leaf (issuer hashes ix, serial
650note ix[RVSER ..)), RVMISS when it names another certificate, X5EMAL
651fn rev_certid rs view uchar * o view ulong 32 ix view uchar 256 to ulong
652h = new ulong 3 0
653a = get o 28
654b = get o 29
655if ne rev_tag rs a b h 0x30 0
656ret X5EMAL
657end
658s = get h 2
659al = rev_hashalg rs a s
660if eq al 0
661ret RVMISS
662end
663q = ulong 0
664r = ulong 20
665if eq al 7
666q = 40
667r = 72
668end
669t = rev_octeq rs s b ix q
670if eq t 0
671ret RVMISS
672end
673t = rev_octeq rs t b ix r
674if eq t 0
675ret RVMISS
676end
677if ne rev_tag rs t b h 2 0
678ret X5EMAL
679end
680if ne get h 2 b
681ret X5EMAL
682end
683if not rev_sereq rs get h 1 b ix get o 22
684ret RVMISS
685end
686ret 0
687end
688
689note rev_rinfo: RevokedInfo content rs[a .. b): revocationTime GeneralizedTime, optional
690note revocationReason [0] EXPLICIT CRLReason (ENUMERATED 0..10, 7 unused). 0 or X5EMAL.
691fn rev_rinfo rs view uchar * a ulong b ulong to ulong
692h = new ulong 3 0
693if ne rev_tag rs a b h 24 0
694ret X5EMAL
695end
696if eq der_time part rs a sub get h 2 a X5BIG
697ret X5EMAL
698end
699s = get h 2
700if eq s b
701ret 0
702end
703if ne rev_tag rs s b h 0xa0 0
704ret X5EMAL
705end
706if ne get h 2 b
707ret X5EMAL
708end
709if ne rev_tag rs get h 1 b h 10 0
710ret X5EMAL
711end
712if or ne get h 2 b ne sub b get h 1 1
713ret X5EMAL
714end
715v = ulong get rs get h 1
716if or gt v 10 eq v 7
717ret X5EMAL
718end
719ret 0
720end
721
722note rev_cstat: certStatus header h of rs: 0 good ([0] IMPLICIT NULL), 1 revoked ([1] RevokedInfo,
723note checked), 2 unknown ([2] IMPLICIT NULL), 3 malformed
724fn rev_cstat rs view uchar * h view ulong 3 to ulong
725g = get h 0
726n = sub get h 2 get h 1
727if eq g 0x80
728if eq n 0
729ret 0
730end
731ret 3
732end
733if eq g 0x82
734if eq n 0
735ret 2
736end
737ret 3
738end
739if eq g 0xa1
740if eq rev_rinfo rs get h 1 get h 2 0
741ret 1
742end
743end
744ret 3
745end
746
747note rev_single: the SingleResponse TLV that is exactly rs[o[30] .. o[31]): certID, certStatus,
748note thisUpdate, optional nextUpdate [0], optional singleExtensions [1] (none critical). RVMISS when
749note its CertID is not the leaf's; 0 when it is, with o[16] status, o[17] thisUpdate, o[18] nextUpdate.
750fn rev_single rs view uchar * ix view uchar 256 o edit ulong 32 to ulong
751h = new ulong 3 0
752x = new ulong 16 0
753b = get o 31
754if ne rev_tag rs get o 30 b h 0x30 0
755ret X5EMAL
756end
757if ne get h 2 b
758ret X5EMAL
759end
760if ne rev_tag rs get h 1 b h 0x30 0
761ret X5EMAL
762end
763put o 28 get h 1
764put o 29 get h 2
765s = get h 2
766v = rev_certid rs o ix
767if ne v 0
768ret v
769end
770if ne der_hdr rs s b h 0
771ret X5EMAL
772end
773v = rev_cstat rs h
774if eq v 3
775ret X5EMAL
776end
777put o 16 v
778s = get h 2
779if ne rev_tag rs s b h 24 0
780ret X5EMAL
781end
782t = der_time part rs s sub get h 2 s
783if eq t X5BIG
784ret X5EMAL
785end
786put o 17 t
787put o 18 X5BIG
788s = get h 2
789if lt s b
790if ne der_hdr rs s b h 0
791ret X5EMAL
792end
793if eq get h 0 0xa0
794e = get h 2
795s = get h 1
796if ne rev_tag rs s e h 24 0
797ret X5EMAL
798end
799if ne get h 2 e
800ret X5EMAL
801end
802t = der_time part rs s sub e s
803if eq t X5BIG
804ret X5EMAL
805end
806put o 18 t
807s = e
808end
809end
810if lt s b
811if ne rev_tag rs s b h 0xa1 0
812ret X5EMAL
813end
814if ne get h 2 b
815ret X5EMAL
816end
817put x 12 RVNONE
818put x 13 RVNONE
819v = rev_exts rs get h 1 b x
820if ne v 0
821ret v
822end
823s = b
824end
825if ne s b
826ret X5EMAL
827end
828ret 0
829end
830
831note rev_resps: the responses o[11] .. o[12]: the first SingleResponse for the leaf (rev_single); 0
832note found, RVMISS none, else a code. At most 256 SingleResponses.
833fn rev_resps rs view uchar * ix view uchar 256 o edit ulong 32 to ulong
834h = new ulong 3 0
835s = get o 11
836e = get o 12
837n = ulong 0
838while lt s e bound 1000
839n = add n 1
840if gt n 256
841ret X5EMAL
842end
843if ne der_hdr rs s e h 0
844ret X5EMAL
845end
846t = get h 2
847put o 30 s
848put o 31 t
849v = rev_single rs ix o
850if ne v RVMISS
851ret v
852end
853s = t
854end
855ret RVMISS
856end
857
858note rev_ocsp1: OCSPResponse that is exactly rs: responseStatus successful (else RVEOST), responseBytes
859note id-pkix-ocsp-basic; the BasicOCSPResponse content span into o[20] o[21]
860fn rev_ocsp1 rs view uchar * o edit ulong 32 to ulong
861h = new ulong 3 0
862e = len rs
863if ne rev_tag rs 0 e h 0x30 0
864ret X5EMAL
865end
866if ne get h 2 e
867ret X5EMAL
868end
869if ne rev_tag rs get h 1 e h 10 0
870ret X5EMAL
871end
872if ne sub get h 2 get h 1 1
873ret X5EMAL
874end
875c = ulong get rs get h 1
876if ne c 0
877if or gt c 6 eq c 4
878ret X5EMAL
879end
880ret RVEOST
881end
882if ne rev_tag rs get h 2 e h 0xa0 0
883ret X5EMAL
884end
885if ne get h 2 e
886ret X5EMAL
887end
888if ne rev_tag rs get h 1 e h 0x30 0
889ret X5EMAL
890end
891if ne get h 2 e
892ret X5EMAL
893end
894if ne rev_tag rs get h 1 e h 6 0
895ret X5EMAL
896end
897if ne rev_oidin part rs get h 1 sub get h 2 get h 1 5
898ret X5EMAL
899end
900if ne rev_tag rs get h 2 e h 4 0
901ret X5EMAL
902end
903if ne get h 2 e
904ret X5EMAL
905end
906if ne rev_tag rs get h 1 e h 0x30 0
907ret X5EMAL
908end
909if ne get h 2 e
910ret X5EMAL
911end
912put o 20 get h 1
913put o 21 e
914ret 0
915end
916
917note rev_basic: BasicOCSPResponse content rs[o[20] .. o[21]): tbsResponseData, signatureAlgorithm,
918note signature BIT STRING (no unused bits), optional certs [0] SEQUENCE OF Certificate
919fn rev_basic rs view uchar * o edit ulong 32 to ulong
920h = new ulong 3 0
921e = get o 21
922s = get o 20
923if ne rev_tag rs s e h 0x30 0
924ret X5EMAL
925end
926put o 1 s
927put o 2 sub get h 2 s
928s = get h 2
929if ne rev_tag rs s e h 0x30 0
930ret X5EMAL
931end
932put o 3 x509_algid part rs s sub get h 2 s
933s = get h 2
934if ne rev_tag rs s e h 3 0
935ret X5EMAL
936end
937q = get h 1
938if or ge q get h 2 ne get rs q 0
939ret X5EMAL
940end
941put o 4 add q 1
942put o 5 sub sub get h 2 q 1
943s = get h 2
944put o 6 0
945put o 7 0
946if eq s e
947ret 0
948end
949if ne rev_tag rs s e h 0xa0 0
950ret X5EMAL
951end
952if ne get h 2 e
953ret X5EMAL
954end
955if ne rev_tag rs get h 1 e h 0x30 0
956ret X5EMAL
957end
958if ne get h 2 e
959ret X5EMAL
960end
961put o 6 get h 1
962put o 7 e
963ret 0
964end
965
966note rev_rdata: ResponseData (RFC 6960 4.2.1) at rs[o[1] .. +o[2]): version absent (v1; an explicit
967note DEFAULT is not DER), responderID [1] or [2], producedAt, responses (at least one), optional
968note responseExtensions [1] with no critical extension
969fn rev_rdata rs view uchar * o edit ulong 32 to ulong
970h = new ulong 3 0
971x = new ulong 16 0
972s = get o 1
973e = add s get o 2
974if ne rev_tag rs s e h 0x30 0
975ret X5EMAL
976end
977s = get h 1
978if ne der_hdr rs s e h 0
979ret X5EMAL
980end
981g = get h 0
982if and ne g 0xa1 ne g 0xa2
983ret X5EMAL
984end
985put o 8 g
986put o 9 get h 1
987put o 10 get h 2
988s = get h 2
989if ne rev_tag rs s e h 24 0
990ret X5EMAL
991end
992if eq der_time part rs s sub get h 2 s X5BIG
993ret X5EMAL
994end
995s = get h 2
996if ne rev_tag rs s e h 0x30 0
997ret X5EMAL
998end
999if eq get h 1 get h 2
1000ret X5EMAL
1001end
1002put o 11 get h 1
1003put o 12 get h 2
1004s = get h 2
1005put o 13 0
1006put o 14 0
1007if eq s e
1008ret 0
1009end
1010if ne rev_tag rs s e h 0xa1 0
1011ret X5EMAL
1012end
1013if ne get h 2 e
1014ret X5EMAL
1015end
1016put o 13 get h 1
1017put o 14 e
1018put x 12 RVNONE
1019put x 13 RVNONE
1020ret rev_exts rs get h 1 e x
1021end
1022
1023note rev_ridm: the responderID (o[8] tag, o[9] .. o[10] content) names the candidate c whose subject
1024note and key spans are o[24] o[25] o[26] o[27]: byName [1] the Name DER equals the subject; byKey [2]
1025note the KeyHash OCTET STRING is the SHA-1 of the subjectPublicKey bits (RFC 6960 4.2.1)
1026fn rev_ridm rs view uchar * o view ulong 32 c view uchar * to bool
1027d = new uchar 256 0
1028a = get o 9
1029b = get o 10
1030if eq get o 8 0xa1
1031ret der_same part rs a sub b a part c get o 24 get o 25
1032end
1033n = sha1_digest part c get o 26 get o 27 d
1034if ne n 20
1035ret false
1036end
1037ret eq rev_octeq rs a b d 0 b
1038end
1039
1040note rev_ocspeku: the certificate c (record r) has extKeyUsage with id-kp-OCSPSigning: 0, else 1
1041fn rev_ocspeku c view uchar * r view ulong * to ulong
1042x = new ulong 16 0
1043h = new ulong 3 0
1044put x 12 8
1045if ne rev_certext c r x 0
1046ret 1
1047end
1048b = get x 7
1049if ne rev_tag c get x 6 b h 0x30 0
1050ret 1
1051end
1052s = get h 1
1053while lt s b bound 10000
1054if ne rev_tag c s b h 6 0
1055ret 1
1056end
1057if eq rev_oidin part c get h 1 sub get h 2 get h 1 4
1058ret 0
1059end
1060s = get h 2
1061end
1062ret 1
1063end
1064
1065note rev_dchk: the candidate c (record in w[0 .. 32)) is an authorized responder for the issuer is
1066note (record in w[32 .. 64)): issued by is (chain_link), valid at o[0], with extKeyUsage
1067note id-kp-OCSPSigning. On success its record moves to w[32 .. 64) as the signer. 0 or RVERESP.
1068fn rev_dchk c view uchar * is view uchar * w edit ulong 128 o view ulong 32 to ulong
1069if ne chain_link c is part w 0 64 0
1070ret RVERESP
1071end
1072if ne chain_time part w 0 X5RSZ get o 0 0
1073ret RVERESP
1074end
1075if ne rev_ocspeku c part w 0 X5RSZ 0
1076ret RVERESP
1077end
1078for i ulong X5RSZ
1079put w add X5RSZ i get w i
1080end
1081ret 0
1082end
1083
1084note rev_cp: s[a .. a+l) into d[0 .. l)
1085fn rev_cp d edit uchar * s view uchar * a ulong l ulong to void
1086for i ulong l
1087put d i get s add a i
1088end
1089end
1090
1091note rev_deleg: the certificate among o[6] .. o[7] (the BasicOCSPResponse certs) that the responderID
1092note names, copied into kb (length o[23]) with its record in w[0 .. 32). 0, RVERESP when none is
1093note named (RFC 6960 4.2.2.2), X5EMAL on bad framing.
1094fn rev_deleg rs view uchar * o edit ulong 32 w edit ulong 128 kb edit uchar 16896 to ulong
1095h = new ulong 3 0
1096s = get o 6
1097while lt s get o 7 bound 64
1098if ne rev_tag rs s get o 7 h 0x30 0
1099ret X5EMAL
1100end
1101l = sub get h 2 s
1102if gt l RVKBUF
1103ret X5EMAL
1104end
1105rev_cp kb rs s l
1106s = get h 2
1107if ne x509_parse part kb 0 l part w 0 X5RSZ 0
1108continue
1109end
1110put o 23 l
1111put o 24 get w X5RSBO
1112put o 25 get w X5RSBL
1113put o 26 get w X5RKO
1114put o 27 get w X5RKL
1115if rev_ridm rs o part kb 0 l
1116ret 0
1117end
1118end
1119ret RVERESP
1120end
1121
1122note rev_auth: the response signer: the issuer is itself when the responderID names it, else the
1123note named delegated responder (rev_deleg) when it is issued by is, valid at o[0] and carries
1124note id-kp-OCSPSigning (rev_dchk); then the response signature with the signer's key. w[32 .. 64)
1125note holds the record of is on entry.
1126fn rev_auth rs view uchar * is view uchar * o edit ulong 32 w edit ulong 128 to ulong
1127put o 24 get w add X5RSZ X5RSBO
1128put o 25 get w add X5RSZ X5RSBL
1129put o 26 get w add X5RSZ X5RKO
1130put o 27 get w add X5RSZ X5RKL
1131if rev_ridm rs o is
1132ret rev_sig rs is w o
1133end
1134kb = new uchar 16896 0
1135v = rev_deleg rs o w kb
1136if ne v 0
1137ret v
1138end
1139if ne rev_dchk part kb 0 get o 23 is w o 0
1140ret RVERESP
1141end
1142ret rev_sig rs part kb 0 get o 23 w o
1143end
1144
1145note rev_ocspr: the OCSPResponse rs: framing and responseStatus, ResponseData, the signer and its
1146note signature, the leaf's SingleResponse, its status (revoked RVEREV, unknown or missing RVEUNK),
1147note then freshness (rev_fresh)
1148fn rev_ocspr rs view uchar * ib view uchar 16896 o edit ulong 32 w edit ulong 128 ix view uchar 256 to ulong
1149v = rev_ocsp1 rs o
1150if eq v 0
1151v = rev_basic rs o
1152end
1153if eq v 0
1154v = rev_rdata rs o
1155end
1156if eq v 0
1157v = rev_auth rs part ib 0 get w 65 o w
1158end
1159if ne v 0
1160ret v
1161end
1162v = rev_resps rs ix o
1163if eq v RVMISS
1164ret RVEUNK
1165end
1166if ne v 0
1167ret v
1168end
1169st = get o 16
1170if eq st 1
1171ret RVEREV
1172end
1173if eq st 2
1174ret RVEUNK
1175end
1176ret rev_fresh o
1177end
1178
1179note rev_ocsp: the stapled OCSPResponse b[p[5] .. +p[6]) for the leaf p[1 .. 2] issued by p[3 .. 4] at
1180note unix time p[0]; 0 = signed by an authorized responder, good and fresh
1181fn rev_ocsp b view uchar * p view ulong 16 to ulong
1182o = new ulong 32 0
1183w = new ulong 128 0
1184ix = new uchar 256 0
1185lb = new uchar 16896 0
1186ib = new uchar 16896 0
1187v = rev_setup b p lb ib w
1188if ne v 0
1189ret v
1190end
1191put o 0 get w 66
1192v = rev_serial lb w ix o
1193if ne v 0
1194ret v
1195end
1196rev_ids ib w ix
1197if or eq get p 6 0 not rev_span b p 5
1198ret X5EMAL
1199end
1200ret rev_ocspr part b get p 5 get p 6 ib o w ix
1201end
1202
1203note ---------------------------------------------------------------- CRL (RFC 5280 5, 6.3)
1204
1205note rev_cl1: CertificateList that is exactly cr: tbsCertList, signatureAlgorithm, signatureValue
1206note (BIT STRING, no unused bits)
1207fn rev_cl1 cr view uchar * o edit ulong 32 to ulong
1208h = new ulong 3 0
1209e = len cr
1210if ne rev_tag cr 0 e h 0x30 0
1211ret X5EMAL
1212end
1213if ne get h 2 e
1214ret X5EMAL
1215end
1216s = get h 1
1217if ne rev_tag cr s e h 0x30 0
1218ret X5EMAL
1219end
1220put o 1 s
1221put o 2 sub get h 2 s
1222put o 24 get h 1
1223put o 25 get h 2
1224s = get h 2
1225if ne rev_tag cr s e h 0x30 0
1226ret X5EMAL
1227end
1228put o 3 x509_algid part cr s sub get h 2 s
1229put o 26 s
1230put o 27 sub get h 2 s
1231s = get h 2
1232if ne rev_tag cr s e h 3 0
1233ret X5EMAL
1234end
1235if ne get h 2 e
1236ret X5EMAL
1237end
1238q = get h 1
1239if or ge q e ne get cr q 0
1240ret X5EMAL
1241end
1242put o 4 add q 1
1243put o 5 sub sub e q 1
1244ret 0
1245end
1246
1247note rev_times: thisUpdate and optional nextUpdate (UTCTime or GeneralizedTime) at cr[s ..) by e into
1248note o[17] o[18] (X5BIG none); the offset after them, 0 when malformed
1249fn rev_times cr view uchar * s ulong e ulong o edit ulong 32 to ulong
1250h = new ulong 3 0
1251if ne der_hdr cr s e h 0
1252ret 0
1253end
1254if and ne get h 0 23 ne get h 0 24
1255ret 0
1256end
1257t = der_time part cr s sub get h 2 s
1258if eq t X5BIG
1259ret 0
1260end
1261put o 17 t
1262put o 18 X5BIG
1263s = get h 2
1264if lt s e
1265if ne der_hdr cr s e h 0
1266ret 0
1267end
1268if or eq get h 0 23 eq get h 0 24
1269t = der_time part cr s sub get h 2 s
1270if eq t X5BIG
1271ret 0
1272end
1273put o 18 t
1274s = get h 2
1275end
1276end
1277ret s
1278end
1279
1280note rev_tbsl: tbsCertList content cr[o[24] .. o[25]): version v2 (INTEGER 1; required with
1281note extensions, RFC 5280 5.1.2.1), signature equal to the outer algorithm (X5EAMIS), issuer (span into
1282note o[28] o[29]), thisUpdate, optional nextUpdate, optional revokedCertificates (non-empty when
1283note present), optional crlExtensions [0]
1284fn rev_tbsl cr view uchar * o edit ulong 32 to ulong
1285h = new ulong 3 0
1286s = get o 24
1287e = get o 25
1288vr = ulong 0
1289if ne der_hdr cr s e h 0
1290ret X5EMAL
1291end
1292if eq get h 0 2
1293if ne der_small part cr s sub get h 2 s 1
1294ret X5EMAL
1295end
1296vr = 1
1297s = get h 2
1298end
1299if ne rev_tag cr s e h 0x30 0
1300ret X5EMAL
1301end
1302if ne sub get h 2 s get o 27
1303ret X5EAMIS
1304end
1305if not der_eqat cr s get o 26 get o 27
1306ret X5EAMIS
1307end
1308s = get h 2
1309if ne rev_tag cr s e h 0x30 0
1310ret X5EMAL
1311end
1312put o 28 s
1313put o 29 sub get h 2 s
1314s = rev_times cr get h 2 e o
1315if eq s 0
1316ret X5EMAL
1317end
1318put o 11 0
1319put o 12 0
1320put o 13 0
1321put o 14 0
1322if lt s e
1323if ne der_hdr cr s e h 0
1324ret X5EMAL
1325end
1326if eq get h 0 0x30
1327if eq get h 1 get h 2
1328ret X5EMAL
1329end
1330put o 11 get h 1
1331put o 12 get h 2
1332s = get h 2
1333end
1334end
1335if lt s e
1336if ne rev_tag cr s e h 0xa0 0
1337ret X5EMAL
1338end
1339if or ne get h 2 e eq vr 0
1340ret X5EMAL
1341end
1342put o 13 get h 1
1343put o 14 e
1344s = e
1345end
1346if ne s e
1347ret X5EMAL
1348end
1349ret 0
1350end
1351
1352note rev_idp: issuingDistributionPoint value (x[6] .. x[7] of cr; RFC 5280 5.2.5). A distributionPoint
1353note must be a fullName [0] (its GeneralNames content span into o[30] o[31]); onlyContainsUserCerts
1354note TRUE is accepted (the leaf is an end entity, chain_leaf); nameRelativeToCRLIssuer,
1355note onlyContainsCACerts, onlySomeReasons, indirectCRL and onlyContainsAttributeCerts are RVESCOPE.
1356fn rev_idp cr view uchar * x view ulong 16 o edit ulong 32 to ulong
1357h = new ulong 3 0
1358a = get x 6
1359b = get x 7
1360if ne rev_tag cr a b h 0x30 0
1361ret X5EMAL
1362end
1363if ne get h 2 b
1364ret X5EMAL
1365end
1366s = get h 1
1367while lt s b bound 8
1368if ne der_hdr cr s b h 0
1369ret X5EMAL
1370end
1371g = get h 0
1372t = get h 2
1373if eq g 0xa0
1374if ne der_hdr cr get h 1 t h 0
1375ret X5EMAL
1376end
1377if or ne get h 0 0xa0 ne get h 2 t
1378ret RVESCOPE
1379end
1380if eq get h 1 t
1381ret X5EMAL
1382end
1383put o 30 get h 1
1384put o 31 t
1385else
1386if ne g 0x81
1387if and ge g 0x82 le g 0x85
1388ret RVESCOPE
1389end
1390ret X5EMAL
1391end
1392g = get h 1
1393if ne sub t g 1
1394ret X5EMAL
1395end
1396if ne get cr g 255
1397ret X5EMAL
1398end
1399end
1400s = t
1401end
1402ret 0
1403end
1404
1405note rev_crlx: crlExtensions o[13] .. o[14]: only issuingDistributionPoint may be critical
1406note (deltaCRLIndicator RVESCOPE, others X5ECRIT, rev_exts); an IDP is checked by rev_idp
1407fn rev_crlx cr view uchar * o edit ulong 32 to ulong
1408x = new ulong 16 0
1409put o 30 0
1410put o 31 0
1411if eq get o 14 0
1412ret 0
1413end
1414put x 12 3
1415put x 13 3
1416v = rev_exts cr get o 13 get o 14 x
1417if ne v 0
1418ret v
1419end
1420if eq get x 4 0
1421ret 0
1422end
1423ret rev_idp cr x o
1424end
1425
1426note rev_crlr: the CRL cr: framing, the issuer's signature (ib, record w[32 .. 64)), the issuer's
1427note keyUsage includes cRLSign when present (X5EKU), the tbsCertList, issuer Name equal to the
1428note issuer's subject DER (RVESCOPE), crlExtensions
1429fn rev_crlr cr view uchar * ib view uchar 16896 o edit ulong 32 w edit ulong 128 to ulong
1430v = rev_cl1 cr o
1431if eq v 0
1432v = rev_sig cr part ib 0 get w 65 w o
1433end
1434if ne v 0
1435ret v
1436end
1437if eq get w add X5RSZ X5RKU 1
1438if eq and get w add X5RSZ X5RKUB 2 0
1439ret X5EKU
1440end
1441end
1442v = rev_tbsl cr o
1443if ne v 0
1444ret v
1445end
1446a = get w add X5RSZ X5RSBO
1447n = get w add X5RSZ X5RSBL
1448if not der_same part cr get o 28 get o 29 part ib a n
1449ret RVESCOPE
1450end
1451ret rev_crlx cr o
1452end
1453
1454note rev_gnin: the GeneralName TLV lf[g[0] .. g[1]) equals one of the GeneralName TLVs in cr[g[2] .. g[3])
1455fn rev_gnin cr view uchar * lf view uchar * g view ulong 8 to bool
1456h = new ulong 3 0
1457s = get g 2
1458b = get g 3
1459while lt s b bound 1000
1460if ne der_hdr cr s b h 0
1461ret false
1462end
1463e = sub get h 2 s
1464a = get g 0
1465if der_same part cr s e part lf a sub get g 1 a
1466ret true
1467end
1468s = get h 2
1469end
1470ret false
1471end
1472
1473note rev_dpone: DistributionPoint content lf[g[4] .. g[5]): its distributionPoint fullName holds a
1474note GeneralName that rev_gnin finds in the IDP fullName
1475fn rev_dpone lf view uchar * cr view uchar * g edit ulong 8 to bool
1476h = new ulong 3 0
1477if ne rev_tag lf get g 4 get g 5 h 0xa0 0
1478ret false
1479end
1480if ne rev_tag lf get h 1 get h 2 h 0xa0 0
1481ret false
1482end
1483s = get h 1
1484e = get h 2
1485while lt s e bound 1000
1486if ne der_hdr lf s e h 0
1487ret false
1488end
1489put g 0 s
1490put g 1 get h 2
1491if rev_gnin cr lf g
1492ret true
1493end
1494s = get h 2
1495end
1496ret false
1497end
1498
1499note rev_dpmatch: the leaf (lb, length w[64]) has a cRLDistributionPoints fullName name equal to one
1500note of the IDP fullName names cr[o[30] .. o[31]) (RFC 5280 6.3.3 b.2.i); 0 yes, RVESCOPE otherwise
1501note (also when the leaf has no cRLDistributionPoints)
1502fn rev_dpmatch cr view uchar * lb view uchar 16896 w edit ulong 128 o view ulong 32 to ulong
1503x = new ulong 16 0
1504g = new ulong 8 0
1505h = new ulong 3 0
1506l = get w 64
1507if ne x509_parse part lb 0 l part w 0 X5RSZ 0
1508ret X5EMAL
1509end
1510put x 12 2
1511if ne rev_certext part lb 0 l part w 0 X5RSZ x 0
1512ret RVESCOPE
1513end
1514put g 2 get o 30
1515put g 3 get o 31
1516b = get x 7
1517if ne rev_tag part lb 0 l get x 6 b h 0x30 0
1518ret RVESCOPE
1519end
1520s = get h 1
1521while lt s b bound 1000
1522if ne rev_tag part lb 0 l s b h 0x30 0
1523ret RVESCOPE
1524end
1525put g 4 get h 1
1526put g 5 get h 2
1527s = get h 2
1528if rev_dpone part lb 0 l cr g
1529ret 0
1530end
1531end
1532ret RVESCOPE
1533end
1534
1535note rev_listed: revokedCertificates o[11] .. o[12]: every entry {userCertificate, revocationDate,
1536note crlEntryExtensions} is framed and its extensions checked (certificateIssuer RVESCOPE: indirect
1537note CRLs are not supported; unknown critical X5ECRIT). RVHIT when an entry has the leaf serial (ix),
1538note 0 when none has, else a code.
1539fn rev_listed cr view uchar * o view ulong 32 ix view uchar 256 to ulong
1540h = new ulong 3 0
1541x = new ulong 16 0
1542t = new ulong 32 0
1543put x 12 RVNONE
1544put x 13 RVNONE
1545s = get o 11
1546e = get o 12
1547n = get o 22
1548hit = ulong 0
1549while lt s e bound 999999
1550if ne rev_tag cr s e h 0x30 0
1551ret X5EMAL
1552end
1553u = get h 2
1554if ne rev_tag cr get h 1 u h 2 0
1555ret X5EMAL
1556end
1557if rev_sereq cr get h 1 get h 2 ix n
1558hit = RVHIT
1559end
1560q = rev_times cr get h 2 u t
1561if eq q 0
1562ret X5EMAL
1563end
1564if ne get t 18 X5BIG
1565ret X5EMAL
1566end
1567if lt q u
1568v = rev_exts cr q u x
1569if ne v 0
1570ret v
1571end
1572end
1573s = u
1574end
1575ret hit
1576end
1577
1578note rev_crl: the CRL b[p[7] .. +p[8]) for the leaf p[1 .. 2] issued by p[3 .. 4] at unix time p[0].
1579note Order: signature and keyUsage, tbsCertList, scope (issuer name, crlExtensions, IDP against the
1580note leaf's cRLDistributionPoints), the entries (RVEREV when listed), then freshness (rev_fresh).
1581fn rev_crl b view uchar * p view ulong 16 to ulong
1582o = new ulong 32 0
1583w = new ulong 128 0
1584ix = new uchar 256 0
1585lb = new uchar 16896 0
1586ib = new uchar 16896 0
1587v = rev_setup b p lb ib w
1588if ne v 0
1589ret v
1590end
1591put o 0 get w 66
1592v = rev_serial lb w ix o
1593if ne v 0
1594ret v
1595end
1596if or eq get p 8 0 not rev_span b p 7
1597ret X5EMAL
1598end
1599v = rev_crlr part b get p 7 get p 8 ib o w
1600if ne v 0
1601ret v
1602end
1603if ne get o 31 0
1604v = rev_dpmatch part b get p 7 get p 8 lb w o
1605if ne v 0
1606ret v
1607end
1608end
1609v = rev_listed part b get p 7 get p 8 o ix
1610if eq v RVHIT
1611ret RVEREV
1612end
1613if ne v 0
1614ret v
1615end
1616ret rev_fresh o
1617end
1618
1619note ---------------------------------------------------------------- issuer lookup and policy
1620
1621note rev_isstry: is c (the exact DER of a candidate) the issuer of the leaf lb (length w[64], record
1622note w[0 .. 32))? chain_link: its subject is the leaf's issuer and the leaf signature verifies with its
1623note key. Copies c into kb and returns its length when it is, else 0.
1624fn rev_isstry lb view uchar 16896 c view uchar * w edit ulong 128 kb edit uchar * to ulong
1625if gt len c len kb
1626ret 0
1627end
1628if ne x509_parse c part w X5RSZ X5RSZ 0
1629ret 0
1630end
1631if ne chain_link part lb 0 get w 64 c w 0
1632ret 0
1633end
1634for i ulong len c
1635put kb i get c i
1636end
1637ret len c
1638end
1639
1640note rev_isstore: rev_isstry over the entries of the store st (uint24 length, DER: x509_store_load)
1641fn rev_isstore lb view uchar 16896 st view uchar * w edit ulong 128 kb edit uchar * to ulong
1642p = ulong 0
1643while lt add p 3 len st bound 100000
1644k = der_u24 st p
1645p = add p 3
1646if or eq k 0 gt k sub len st p
1647ret 0
1648end
1649m = rev_isstry lb part st p k w kb
1650if ne m 0
1651ret m
1652end
1653p = add p k
1654end
1655ret 0
1656end
1657
1658note rev_prs: x509_parse of d[0 .. l) into the record w[j*32 .. j*32+32)
1659fn rev_prs d view uchar 16896 l ulong w edit ulong 128 j ulong to ulong
1660a = mul j X5RSZ
1661ret x509_parse part d 0 l part w a X5RSZ
1662end
1663
1664note rev_isslist: rev_isstry over the presented certificates after the leaf; the chain_index pairs are
1665note in w[96 .. 128), the entry count in w[67]
1666fn rev_isslist lb view uchar 16896 cl view uchar * w edit ulong 128 kb edit uchar * to ulong
1667n = get w 67
1668for j ulong n
1669if eq j 0
1670continue
1671end
1672a = add 96 mul j 2
1673s = get w a
1674l = get w add a 1
1675m = rev_isstry lb part cl s l w kb
1676if ne m 0
1677ret m
1678end
1679end
1680ret 0
1681end
1682
1683note rev_issuer: the issuer of the leaf of the TLS certificate_list cl (chain_index framing), looked
1684note for among the presented certificates and then the store st, copied into kb; its length, 0 when
1685note none. Run after x509_chain_verify accepted cl: any certificate whose subject is the leaf's issuer
1686note and whose key verifies the leaf signature holds the issuing key, which is all OCSP CertID, OCSP
1687note signer authorization and CRL signature checks need.
1688fn rev_issuer cl view uchar * st view uchar * kb edit uchar * to ulong
1689lb = new uchar 16896 0
1690w = new ulong 128 0
1691n = chain_index cl part w 96 32
1692if eq n 0
1693ret 0
1694end
1695put w 67 n
1696l = get w 97
1697if gt l RVKBUF
1698ret 0
1699end
1700rev_cp lb cl get w 96 l
1701put w 64 l
1702if ne rev_prs lb l w 0 0
1703ret 0
1704end
1705n = rev_isslist lb cl w kb
1706if ne n 0
1707ret n
1708end
1709ret rev_isstore lb st w kb
1710end
1711
1712note rev_check: revocation policy for b and p (see the head of this file). A staple must verify and
1713note be good (rev_ocsp); a must-staple leaf without one is RVENOST; a given CRL must verify, cover the
1714note leaf and not list it (rev_crl). 0 otherwise, including when nothing was supplied.
1715fn rev_check b view uchar * p view ulong 16 to ulong
1716if not rev_span b p 1
1717ret X5EMAL
1718end
1719m = rev_mustst part b get p 1 get p 2
1720if and ne m 0 ne m RVHIT
1721ret m
1722end
1723if gt get p 6 0
1724v = rev_ocsp b p
1725if ne v 0
1726ret v
1727end
1728else
1729if eq m RVHIT
1730ret RVENOST
1731end
1732end
1733if gt get p 8 0
1734ret rev_crl b p
1735end
1736ret 0
1737end
1738
1739note rev_errname: name of code into o (REV_ERR_* for 32 .. 38, else x509_errname); its length
1740fn rev_errname code ulong o edit uchar * to ulong
1741if or lt code RVEREV gt code RVESCOPE
1742ret x509_errname code o
1743end
1744k = sub code RVEREV
1745s = ulong 0
1746n = len RVNAMES
1747while lt s n bound 100
1748j = s
1749while lt j n bound 100
1750if eq get RVNAMES j 32
1751break
1752end
1753j = add j 1
1754end
1755if eq k 0
1756m = sub j s
1757for i ulong m
1758if lt i len o
1759put o i get RVNAMES add s i
1760end
1761end
1762ret m
1763end
1764k = sub k 1
1765s = add j 1
1766end
1767ret 0
1768end