interplane.Get Omega
Read in your language

omega-mv

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

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / tls / test / x509 / driver.om

676 lines · 14127 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: program-mode test driver for om/tls/x509.om (composed after sha2 hkdf ec rsa x509).
3note Reads case records from stdin, one per line, tokens separated by single spaces; paths are
4note relative to the working directory (run.sh runs it in vectors/). Records:
5note C NAME EXPECT NOW HOST STORE CERT... x509_chain_verify of the certificate_list built from the
6note DER files CERT... (leaf first); STORE a PEM bundle, or SYS
7note for x509_store_load; NOW unix seconds
8note V NAME EXPECT SCHEME CERT MSG SIG x509_cv_verify (SCHEME decimal; MSG, SIG raw files)
9note M NAME EXPECT PATTERN HOST chain_match, EXPECT 1 match / 0 no match
10note H NAME EXPECT HOST chain_hostok code name
11note T NAME EXPECT HEX der_time of the Time TLV in HEX; EXPECT unix seconds
12note (decimal, may be negative) or BAD
13note S NAME EXPECT STORE store entries loaded from STORE (or SYS)
14note E NAME EXPECT CODE x509_errname(CODE) equals EXPECT (- for empty)
15note F NAME COUNT NOW HOST STORE CERT... COUNT deterministic mutations of the certificate_list;
16note each must return (any code); counts rejections
17note P NAME EXPECT CERT x509_parse of the single DER file CERT (any size up to
18note the 262144-byte list buffer; no chain, no policy)
19note D NAME EXPECT ANCHOR NOTBEFORE chain_dist (Mozilla distrust, M1) of the DER anchor
20note ANCHOR for a leaf notBefore NOTBEFORE (unix seconds)
21note EXPECT for C/V/H is a code name (X509_OK, X509_ERR_...). Prints "MISMATCH line L NAME got G want
22note W" per failure, "fuzz NAME n=N rejected=R" per F record, then "x509 driver: C checks, F failed".
23note Exit 0 when F is 0 and C > 0, 1 otherwise, 2 when input cannot be read.
24
25global inp uchar 4194304
26global ds ulong 16
27text DR0 "0"
28text DR1 "1"
29global ob uchar 32
30global sname uchar 256
31global nm uchar 64
32global onebyte uchar 1
33text DRSUM "x509 driver: "
34text DRCHK " checks, "
35text DRFLD " failed\n"
36text DRMIS "MISMATCH line "
37text DRGOT " got "
38text DRWANT " want "
39text DRFUZ "fuzz "
40text DRFN " n="
41text DRFR " rejected="
42text DRNL "\n"
43text DRSP " "
44text DRSYS "SYS"
45text DRBAD "BAD"
46text DRERR "ERR"
47text DRIO "x509 driver: cannot read input\n"
48text DRRES "RESULT "
49text DRSTF "STORE\n"
50text DRLSF "LIST\n"
51
52note ds: 0 input length, 1 cursor, 2 line number, 3 checks, 4 failures, 5 store bytes (0 = none),
53note 6 name length, 7 store name length
54
55fn drv_slurp to int
56t = ulong 0
57while true bound 999999
58room = sub len inp t
59if eq room 0
60ret 2
61end
62r = read 0 inp t room
63if lt r 0
64ret 2
65end
66if eq r 0
67break
68end
69t = add t ulong r
70end
71put ds 0 t
72ret 0
73end
74
75fn drv_peek to ulong
76c = get ds 1
77if ge c get ds 0
78ret 256
79end
80ret ulong get inp c
81end
82
83note next token into tk (skipping one space before it); its length (0 at the line end)
84fn drv_tok tk edit uchar * to ulong
85if eq drv_peek 32
86put ds 1 add get ds 1 1
87end
88n = ulong 0
89while true bound 999999
90c = drv_peek
91if or or eq c 32 eq c 10 eq c 256
92break
93end
94if lt n len tk
95put tk n uchar c
96end
97n = add n 1
98put ds 1 add get ds 1 1
99end
100if gt n len tk
101ret 0
102end
103ret n
104end
105
106fn drv_eol to void
107while true bound 999999
108c = drv_peek
109if eq c 256
110ret
111end
112put ds 1 add get ds 1 1
113if eq c 10
114ret
115end
116end
117end
118
119fn drv_num t view uchar * to ulong
120v = ulong 0
121for i ulong len t
122v = add mul v 10 sub ulong get t i 48
123end
124ret v
125end
126
127fn drv_prn v ulong to void
128x = v
129k = ulong 0
130while true bound 21
131put ob sub 31 k uchar add 48 rem x 10
132k = add k 1
133x = div x 10
134if eq x 0
135break
136end
137end
138r = write 1 ob sub 32 k k
139end
140
141fn drv_out t view uchar * to void
142for i ulong len t
143put ob 0 get t i
144r = write 1 ob 0 1
145end
146end
147
148note record a mismatch: got / want as text
149fn drv_mis g view uchar * w view uchar * to void
150put ds 4 add get ds 4 1
151r = write 1 DRMIS 0 len DRMIS
152drv_prn get ds 2
153r = write 1 DRSP 0 1
154r = write 1 nm 0 get ds 6
155r = write 1 DRGOT 0 len DRGOT
156drv_out g
157r = write 1 DRWANT 0 len DRWANT
158drv_out w
159r = write 1 DRNL 0 1
160end
161
162note compare code c against the expected name w
163fn drv_code c ulong w view uchar * to void
164b = new uchar 64 0
165put ds 3 add get ds 3 1
166n = x509_errname c edit b
167if eq n 0
168drv_mis DRBAD w
169ret
170end
171if not der_same view part b 0 n w
172drv_mis view part b 0 n w
173end
174end
175
176fn drv_name to void
177t = new uchar 64 0
178n = drv_tok edit t
179for i ulong n
180put nm i get t i
181end
182put ds 6 n
183end
184
185note read the file named p into b; bytes read, or -1 (missing, error, or over len b)
186fn drv_file p view uchar * b edit uchar * to long
187fd = fopen p 0 len p 0 0
188if lt fd 0
189ret -1
190end
191n = ulong 0
192while lt n len b bound 999999
193r = read int fd b n sub len b n
194if le r 0
195break
196end
197n = add n ulong r
198end
199r = read int fd onebyte 0 1
200z = fclose int fd
201if or ne r 0 eq n len b
202ret -1
203end
204ret long n
205end
206
207note load store tk (SYS or a path) into st unless it is the current one; 0 ok, 1 failure
208fn drv_store st edit uchar * tk view uchar * to ulong
209if eq len tk get ds 7
210d = ulong 0
211for i ulong len tk
212if ne get tk i get sname i
213d = 1
214end
215end
216if and eq d 0 gt get ds 5 0
217ret 0
218end
219end
220for i ulong len tk
221put sname i get tk i
222end
223put ds 7 len tk
224put ds 5 0
225r = long 0
226if der_same tk DRSYS
227r = x509_store_load st
228else
229r = x509_store_loadf st tk
230end
231if le r 0
232ret 1
233end
234put ds 5 ulong r
235ret 0
236end
237
238note certificate_list from the remaining DER file tokens of the line into cl; its length (0 bad)
239fn drv_list cl edit uchar * tk edit uchar * to ulong
240p = ulong 0
241while true bound 4096
242n = drv_tok tk
243if eq n 0
244break
245end
246r = drv_file view part tk 0 n part cl add p 3 sub len cl add p 5
247if lt r 1
248ret 0
249end
250l = ulong r
251put cl p uchar shr l 16
252put cl add p 1 uchar and shr l 8 255
253put cl add p 2 uchar and l 255
254put cl add add p 3 l 0
255put cl add add p 4 l 0
256p = add add p 5 l
257end
258ret p
259end
260
261note x509_chain_verify of cl[0 .. ds 12) with hq[0 .. ds 11)
262fn drv_crun cl view uchar * st view uchar * hq view uchar 300 to ulong
263k = get ds 12
264hn = get ds 11
265ret x509_chain_verify part cl 0 k st part hq 0 hn
266end
267
268note C record
269note NOW and HOST tokens into hq (8-byte big-endian time || host); total length
270fn drv_now hq edit uchar 300 tk edit uchar * to ulong
271n = drv_tok tk
272nw = drv_num view part tk 0 n
273for i ulong 8
274put hq i uchar and shr nw mul sub 7 i 8 255
275end
276ret add 8 drv_tok part hq 8 sub len hq 8
277end
278
279fn drv_c st edit uchar * cl edit uchar * tk edit uchar * to void
280hq = new uchar 300 0
281drv_name
282w = new uchar 64 0
283wn = drv_tok edit w
284hn = drv_now hq tk
285n = drv_tok tk
286if ne drv_store st view part tk 0 n 0
287drv_code 99 view part w 0 wn
288ret
289end
290k = drv_list cl tk
291if eq k 0
292drv_code 98 view part w 0 wn
293ret
294end
295put ds 11 hn
296put ds 12 k
297c = drv_crun cl st hq
298drv_code c view part w 0 wn
299end
300
301note R record (x509-limbo): R NAME NOW HOST STORE CERT... runs x509_chain_verify like C and prints
302note "RESULT NAME CODE" (CODE the x509_errname, STORE when the store does not load, LIST when a
303note certificate file is missing); the expectation is judged outside (test/x509/limbo.py)
304fn drv_r st edit uchar * cl edit uchar * tk edit uchar * to void
305hq = new uchar 300 0
306drv_name
307hn = drv_now hq tk
308n = drv_tok tk
309put ds 3 add get ds 3 1
310r = write 1 DRRES 0 len DRRES
311r = write 1 nm 0 get ds 6
312r = write 1 DRSP 0 1
313if ne drv_store st view part tk 0 n 0
314r = write 1 DRSTF 0 len DRSTF
315ret
316end
317k = drv_list cl tk
318if eq k 0
319r = write 1 DRLSF 0 len DRLSF
320ret
321end
322put ds 11 hn
323put ds 12 k
324drv_rout drv_crun cl st hq
325end
326
327note R record result: the code name and a line end
328fn drv_rout c ulong to void
329b = new uchar 64 0
330m = x509_errname c b
331r = write 1 b 0 m
332r = write 1 DRNL 0 1
333end
334
335note P record
336fn drv_p cl edit uchar * tk edit uchar * to void
337r = new ulong 32 0
338drv_name
339w = new uchar 64 0
340wn = drv_tok edit w
341n = drv_tok tk
342k = drv_file view part tk 0 n cl
343if lt k 1
344drv_code 98 view part w 0 wn
345ret
346end
347c = x509_parse part cl 0 ulong k edit r
348drv_code c view part w 0 wn
349end
350
351note D record
352fn drv_d cl edit uchar * tk edit uchar * to void
353w = new ulong X5WSZ 0
354drv_name
355e = new uchar 64 0
356en = drv_tok edit e
357n = drv_tok tk
358k = drv_file view part tk 0 n cl
359if lt k 1
360drv_code 98 view part e 0 en
361ret
362end
363n = drv_tok tk
364t = drv_num view part tk 0 n
365put w X5WLNB add t mul x509_days 1970 1 1 86400
366c = chain_dist view part cl 0 ulong k view w
367drv_code c view part e 0 en
368end
369
370note V record
371fn drv_v cl edit uchar * ms edit uchar * tk edit uchar * to void
372drv_name
373w = new uchar 64 0
374wn = drv_tok edit w
375n = drv_tok tk
376sc = drv_num view part tk 0 n
377k = drv_list cl tk
378if eq k 0
379drv_code 98 view part w 0 wn
380ret
381end
382put ds 8 sc
383drv_vrun cl ms view part w 0 wn
384end
385
386note V record, second half: the leaf is the only entry; MSG and SIG were listed after it, so
387note drv_list put them in cl as entries 2 and 3; move them into ms
388fn drv_vrun cl edit uchar * ms edit uchar * w view uchar * to void
389m = drv_vcopy cl ms
390a = get ds 13
391b = get ds 14
392c = x509_cv_verify view part cl 0 a get ds 8 view part ms 0 b m
393drv_code c w
394end
395
396note move MSG and SIG (entries 2, 3 of cl) into ms; MSG length; ds 13 leaf list length, ds 14 total
397fn drv_vcopy cl edit uchar * ms edit uchar * to ulong
398l1 = der_u24 cl 0
399p = add l1 5
400l2 = der_u24 cl p
401p = add p 3
402for i ulong l2
403put ms i get cl add p i
404end
405p = add add p l2 2
406l3 = der_u24 cl p
407p = add p 3
408for i ulong l3
409put ms add l2 i get cl add p i
410end
411put cl add l1 3 0
412put cl add l1 4 0
413put ds 13 add l1 5
414put ds 14 add l2 l3
415ret l2
416end
417
418note M record
419fn drv_m tk edit uchar * hb edit uchar * to void
420drv_name
421e = drv_num view part tk 0 drv_tok tk
422n = drv_tok tk
423h = drv_tok hb
424g = ulong 0
425if chain_match view part tk 0 n view part hb 0 h
426g = 1
427end
428put ds 3 add get ds 3 1
429if ne g e
430if eq g 1
431drv_mis DR1 DR0
432else
433drv_mis DR0 DR1
434end
435end
436end
437
438note H record
439fn drv_h tk edit uchar * hb edit uchar * to void
440drv_name
441n = drv_tok tk
442h = drv_tok hb
443c = chain_hostok view part hb 0 h
444drv_code c view part tk 0 n
445end
446
447note T record
448fn drv_t tk edit uchar * hb edit uchar * to void
449drv_name
450n = drv_tok tk
451h = drv_tok hb
452k = div h 2
453for i ulong k
454put hb i uchar or shl der_hexv ulong get hb mul i 2 4 der_hexv ulong get hb add mul i 2 1
455end
456v = der_time view part hb 0 k
457put ds 3 add get ds 3 1
458if eq v X5BIG
459if not der_same view part tk 0 n DRBAD
460drv_mis DRBAD view part tk 0 n
461end
462ret
463end
464ep = mul x509_days 1970 1 1 86400
465if gt get tk 0 47
466if ge v ep
467if ne sub v ep drv_num view part tk 0 n
468drv_mis DRBAD view part tk 0 n
469end
470ret
471end
472end
473if eq get tk 0 45
474if lt v ep
475if eq sub ep v drv_num view part tk 1 sub n 1
476ret
477end
478end
479end
480drv_mis DRBAD view part tk 0 n
481end
482
483note S record
484fn drv_s st edit uchar * tk edit uchar * to void
485drv_name
486e = drv_num view part tk 0 drv_tok tk
487n = drv_tok tk
488put ds 5 0
489put ds 7 0
490put ds 3 add get ds 3 1
491if ne drv_store st view part tk 0 n 0
492if ne e 0
493drv_mis DRERR DRBAD
494end
495ret
496end
497k = ulong 0
498p = ulong 0
499while lt add p 3 len st bound 100000
500l = der_u24 st p
501if eq l 0
502break
503end
504p = add add p 3 l
505k = add k 1
506end
507if ne k e
508drv_prn k
509drv_mis DRERR DRBAD
510end
511end
512
513note E record
514fn drv_e tk edit uchar * hb edit uchar * to void
515drv_name
516n = drv_tok tk
517c = drv_num view part hb 0 drv_tok hb
518b = new uchar 64 0
519m = x509_errname c edit b
520put ds 3 add get ds 3 1
521if eq get tk 0 45
522if ne m 0
523drv_mis view part b 0 m view part tk 0 n
524end
525ret
526end
527if not der_same view part b 0 m view part tk 0 n
528drv_mis view part b 0 m view part tk 0 n
529end
530end
531
532note F record: mutation k of the certificate_list cl[0 .. n) into mu: k mod 4 = 0 one byte set to a
533note pseudo-random value, 1 one byte xor 0x80 at a pseudo-random header-ish position, 2 truncate the
534note leaf length field to a smaller value, 3 two bytes overwritten
535fn drv_mut cl view uchar * mu edit uchar * k ulong to ulong
536n = len cl
537for i ulong n
538put mu i get cl i
539end
540x = add mul k 2654435761 12345
541x = xor x shr x 13
542p = rem x n
543m = rem k 4
544if eq m 1
545p = rem p 64
546put mu p xor get mu p 128
547ret n
548end
549if eq m 2
550p = rem x der_u24 cl 0
551put mu 0 0
552put mu 1 uchar and shr p 8 255
553put mu 2 uchar and p 255
554ret n
555end
556put mu p uchar and shr x 17 255
557if eq m 3
558p = rem mul p 7 n
559put mu p uchar and shr x 29 255
560end
561ret n
562end
563
564note F record
565fn drv_f st edit uchar * cl edit uchar * tk edit uchar * to void
566hq = new uchar 300 0
567drv_name
568cnt = drv_num view part tk 0 drv_tok tk
569hn = drv_now hq tk
570n = drv_tok tk
571put ds 3 add get ds 3 1
572if ne drv_store st view part tk 0 n 0
573drv_mis DRERR DRBAD
574ret
575end
576k = drv_list cl tk
577if eq k 0
578drv_mis DRERR DRBAD
579ret
580end
581put ds 9 k
582put ds 10 cnt
583drv_fz st cl view part hq 0 hn
584end
585
586note one mutation (index ds 15) of cl checked by x509_cv_verify and x509_chain_verify; the latter's code
587fn drv_fz1 st view uchar * cl view uchar * hq view uchar * to ulong
588mu = new uchar 65536 0
589k = get ds 9
590c = drv_mut part cl 0 k part mu 0 k get ds 15
591c = x509_cv_verify view part mu 0 k 0x0804 cl 16
592ret x509_chain_verify view part mu 0 k st hq
593end
594
595fn drv_fz st view uchar * cl view uchar * hq view uchar * to void
596cnt = get ds 10
597rj = ulong 0
598for j ulong cnt
599put ds 15 j
600if ne drv_fz1 st cl hq 0
601rj = add rj 1
602end
603end
604r = write 1 DRFUZ 0 len DRFUZ
605r = write 1 nm 0 get ds 6
606r = write 1 DRFN 0 len DRFN
607drv_prn cnt
608r = write 1 DRFR 0 len DRFR
609drv_prn rj
610r = write 1 DRNL 0 1
611end
612
613fn main to int
614st = new uchar 1048576 0
615cl = new uchar 262144 0
616ms = new uchar 65536 0
617tk = new uchar 4096 0
618hb = new uchar 4096 0
619if ne drv_slurp 0
620r = write 2 DRIO 0 len DRIO
621ret 2
622end
623while lt get ds 1 get ds 0 bound 999999
624put ds 2 add get ds 2 1
625c = drv_peek
626put ds 1 add get ds 1 1
627if eq c 67
628drv_c st cl tk
629end
630if eq c 86
631drv_v cl ms tk
632end
633if eq c 82
634drv_r st cl tk
635end
636if eq c 77
637drv_m tk hb
638end
639if eq c 72
640drv_h tk hb
641end
642if eq c 84
643drv_t tk hb
644end
645if eq c 83
646drv_s st tk
647end
648if eq c 69
649drv_e tk hb
650end
651if eq c 70
652drv_f st cl tk
653end
654if eq c 80
655drv_p cl tk
656end
657if eq c 68
658drv_d cl tk
659end
660if ne c 10
661drv_eol
662end
663end
664r = write 1 DRSUM 0 len DRSUM
665drv_prn get ds 3
666r = write 1 DRCHK 0 len DRCHK
667drv_prn get ds 4
668r = write 1 DRFLD 0 len DRFLD
669if ne get ds 4 0
670ret 1
671end
672if eq get ds 3 0
673ret 1
674end
675ret 0
676end