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 / rev / driver.om

580 lines · 12274 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note driver.om: program-mode test driver for om/tls/rev.om (composed after sha2 hkdf ec rsa x509 rev).
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 O NAME EXPECT NOW LEAF ISSUER STAPLE CRL rev_check (STAPLE, CRL a DER file or - for none)
6note U NAME EXPECT CERT rev_mustst (EXPECT 0, 1 or a code name)
7note I NAME WANT STORE CERT... rev_issuer of the certificate_list CERT... with the
8note PEM store STORE; WANT the DER file it must return,
9note or - for none
10note H NAME HEX DIGEST sha1_digest of HEX (- empty) equals DIGEST (hex)
11note A NAME COUNT BYTE DIGEST sha1_digest of COUNT bytes of value BYTE
12note E NAME EXPECT CODE rev_errname(CODE) equals EXPECT (- for empty)
13note F NAME COUNT NOW LEAF ISSUER STAPLE the unmutated staple is accepted and each of COUNT
14note deterministic mutations of it is refused
15note G NAME COUNT NOW LEAF ISSUER CRL the same for a CRL: the unmutated result is kept
16note (revoked) and no mutation returns X509_OK
17note Prints "MISMATCH line L NAME got G want W" per failure, "fuzz NAME n=N rejected=R" per F/G
18note record, then "rev driver: C checks, F failed". Exit 0 when F is 0 and C > 0, 1 otherwise, 2 when
19note input cannot be read.
20
21global inp uchar 1048576
22global ds ulong 16
23note main owns bb (file bytes, uchar 262144) and pp (the rev_check descriptor, ulong 16): a global
24note is never passed as an argument (omc error 19)
25global ob uchar 32
26global nm uchar 64
27global onebyte uchar 1
28text DRSUM "rev driver: "
29text DRCHK " checks, "
30text DRFLD " failed\n"
31text DRMIS "MISMATCH line "
32text DRGOT " got "
33text DRWANT " want "
34text DRFUZ "fuzz "
35text DRFN " n="
36text DRFR " rejected="
37text DRNL "\n"
38text DRSP " "
39text DRBAD "BAD"
40text DRERR "ERR"
41text DRDASH "-"
42text DRYES "1"
43text DRNO "0"
44text DRIO "rev driver: cannot read input\n"
45
46note ds: 0 input length, 1 cursor, 2 line number, 3 checks, 4 failures, 6 name length, 8 bb fill,
47note 10 fuzz region (5 staple, 7 CRL)
48
49fn drv_slurp to int
50t = ulong 0
51while true bound 999999
52room = sub len inp t
53if eq room 0
54ret 2
55end
56r = read 0 inp t room
57if lt r 0
58ret 2
59end
60if eq r 0
61break
62end
63t = add t ulong r
64end
65put ds 0 t
66ret 0
67end
68
69fn drv_peek to ulong
70c = get ds 1
71if ge c get ds 0
72ret 256
73end
74ret ulong get inp c
75end
76
77note next token into tk (skipping one space before it); its length (0 at the line end)
78fn drv_tok tk edit uchar * to ulong
79if eq drv_peek 32
80put ds 1 add get ds 1 1
81end
82n = ulong 0
83while true bound 999999
84c = drv_peek
85if or or eq c 32 eq c 10 eq c 256
86break
87end
88if lt n len tk
89put tk n uchar c
90end
91n = add n 1
92put ds 1 add get ds 1 1
93end
94if gt n len tk
95ret 0
96end
97ret n
98end
99
100fn drv_eol to void
101while true bound 999999
102c = drv_peek
103if eq c 256
104ret
105end
106put ds 1 add get ds 1 1
107if eq c 10
108ret
109end
110end
111end
112
113fn drv_num t view uchar * to ulong
114v = ulong 0
115for i ulong len t
116v = add mul v 10 sub ulong get t i 48
117end
118ret v
119end
120
121fn drv_prn v ulong to void
122x = v
123k = ulong 0
124while true bound 21
125put ob sub 31 k uchar add 48 rem x 10
126k = add k 1
127x = div x 10
128if eq x 0
129break
130end
131end
132r = write 1 ob sub 32 k k
133end
134
135note record a mismatch: got / want as text
136fn drv_mis g view uchar * w view uchar * to void
137put ds 4 add get ds 4 1
138r = write 1 DRMIS 0 len DRMIS
139drv_prn get ds 2
140r = write 1 DRSP 0 1
141r = write 1 nm 0 get ds 6
142r = write 1 DRGOT 0 len DRGOT
143r = write 1 g 0 len g
144r = write 1 DRWANT 0 len DRWANT
145r = write 1 w 0 len w
146r = write 1 DRNL 0 1
147end
148
149note compare code c against the expected name w
150fn drv_code c ulong w view uchar * to void
151b = new uchar 64 0
152put ds 3 add get ds 3 1
153n = rev_errname c b
154if eq n 0
155drv_mis DRBAD w
156ret
157end
158if not der_same part b 0 n w
159drv_mis part b 0 n w
160end
161end
162
163fn drv_name to void
164t = new uchar 64 0
165n = drv_tok t
166for i ulong n
167put nm i get t i
168end
169put ds 6 n
170end
171
172note read the file named p into b; bytes read, or -1 (missing, error, or over len b)
173fn drv_file p view uchar * b edit uchar * to long
174fd = fopen p 0 len p 0 0
175if lt fd 0
176ret -1
177end
178n = ulong 0
179while lt n len b bound 999999
180r = read int fd b n sub len b n
181if le r 0
182break
183end
184n = add n ulong r
185end
186r = read int fd onebyte 0 1
187z = fclose int fd
188if or ne r 0 eq n len b
189ret -1
190end
191ret long n
192end
193
194note the next token, a DER file or -, appended to bb as descriptor slot pp[i], pp[i+1]; 0 ok, 1 failure
195fn drv_add tk edit uchar 4096 bb edit uchar 262144 pp edit ulong 16 i ulong to ulong
196n = drv_tok tk
197put pp i 0
198put pp add i 1 0
199if and eq n 1 eq get tk 0 45
200ret 0
201end
202f = get ds 8
203r = drv_file part tk 0 n part bb f sub len bb f
204if lt r 1
205ret 1
206end
207put pp i f
208put pp add i 1 ulong r
209put ds 8 add f ulong r
210ret 0
211end
212
213note NOW LEAF ISSUER, then the slots 5 and 7 in the order given by k (5: staple only, 7: CRL only,
214note 0: both); 0 ok, 1 a file failed
215fn drv_load tk edit uchar 4096 bb edit uchar 262144 pp edit ulong 16 k ulong to ulong
216put ds 8 0
217for i ulong 16
218put pp i 0
219end
220n = drv_tok tk
221put pp 0 drv_num part tk 0 n
222if ne drv_add tk bb pp 1 0
223ret 1
224end
225if ne drv_add tk bb pp 3 0
226ret 1
227end
228if ne k 7
229if ne drv_add tk bb pp 5 0
230ret 1
231end
232end
233if ne k 5
234if ne drv_add tk bb pp 7 0
235ret 1
236end
237end
238ret 0
239end
240
241note O record
242fn drv_o tk edit uchar 4096 bb edit uchar 262144 pp edit ulong 16 to void
243drv_name
244w = new uchar 64 0
245wn = drv_tok w
246if ne drv_load tk bb pp 0 0
247drv_code 98 part w 0 wn
248ret
249end
250c = rev_check part bb 0 get ds 8 pp
251drv_code c part w 0 wn
252end
253
254note one DER file token into slot 1 (bb restarted); 0 ok
255fn drv_load2 tk edit uchar 4096 bb edit uchar 262144 pp edit ulong 16 to ulong
256put ds 8 0
257ret drv_add tk bb pp 1
258end
259
260note U record: EXPECT 1 means rev_mustst returns RVHIT, 0 means 0, a name means that code
261fn drv_u tk edit uchar 4096 bb edit uchar 262144 pp edit ulong 16 to void
262drv_name
263w = new uchar 64 0
264wn = drv_tok w
265if ne drv_load2 tk bb pp 0
266drv_code 98 part w 0 wn
267ret
268end
269c = rev_mustst part bb get pp 1 get pp 2
270if and gt wn 1 ne get w 0 48
271drv_code c part w 0 wn
272ret
273end
274put ds 3 add get ds 3 1
275e = ulong 0
276if eq get w 0 49
277e = RVHIT
278end
279if ne c e
280if eq c RVHIT
281drv_mis DRYES part w 0 wn
282else
283drv_mis DRNO part w 0 wn
284end
285end
286end
287
288note the certificate_list of the remaining file tokens, built at bb[ds[8] ..); its length, 0 on failure
289fn drv_cl tk edit uchar 4096 bb edit uchar 262144 to ulong
290b0 = get ds 8
291p = b0
292while true bound 64
293n = drv_tok tk
294if eq n 0
295break
296end
297q = drv_file part tk 0 n part bb add p 3 sub sub len bb p 5
298if lt q 1
299ret 0
300end
301l = ulong q
302put bb p uchar shr l 16
303put bb add p 1 uchar and shr l 8 255
304put bb add p 2 uchar and l 255
305put bb add add p 3 l 0
306put bb add add p 4 l 0
307p = add add p 5 l
308end
309ret sub p b0
310end
311
312note I record result: rev_issuer of the list bb[pp[10] .. +m) with the store st[0 .. pp[9]) against
313note the expected DER at bb slot 1 (none when slot 1 is empty)
314fn drv_icmp st view uchar 262144 bb view uchar 262144 pp view ulong 16 m ulong to void
315kb = new uchar 16896 0
316k = rev_issuer part bb get pp 10 m part st 0 get pp 9 kb
317if eq get pp 2 0
318if ne k 0
319drv_mis DRBAD DRDASH
320end
321ret
322end
323if eq k 0
324drv_mis DRDASH DRBAD
325ret
326end
327if not der_same part kb 0 k part bb get pp 1 get pp 2
328drv_mis DRERR DRBAD
329end
330end
331
332note I record: the store, then the certificate_list (uint24 length || DER || 2-byte empty
333note extensions) of the remaining files, built in bb after the expected DER (slot 1)
334fn drv_i tk edit uchar 4096 bb edit uchar 262144 pp edit ulong 16 to void
335st = new uchar 262144 0
336drv_name
337put ds 3 add get ds 3 1
338if ne drv_load2 tk bb pp 0
339drv_mis DRERR DRBAD
340ret
341end
342n = drv_tok tk
343r = x509_store_loadf st part tk 0 n
344if le r 0
345drv_mis DRERR DRBAD
346ret
347end
348put pp 9 ulong r
349put pp 10 get ds 8
350m = drv_cl tk bb
351if eq m 0
352drv_mis DRERR DRBAD
353ret
354end
355drv_icmp st bb pp m
356end
357
358note hex digit value, 99 for a non-hex byte
359fn drv_hx c ulong to ulong
360if and ge c 48 le c 57
361ret sub c 48
362end
363if and ge c 97 le c 102
364ret sub c 87
365end
366ret 99
367end
368
369note hex t (- for empty) into b; byte count
370fn drv_unhex t view uchar * b edit uchar * to ulong
371if and eq len t 1 eq get t 0 45
372ret 0
373end
374n = div len t 2
375for i ulong n
376hi = drv_hx ulong get t mul i 2
377lo = drv_hx ulong get t add mul i 2 1
378put b i uchar add mul hi 16 lo
379end
380ret n
381end
382
383note digest d[0 .. 20) against the hex token t
384fn drv_dig d view uchar 32 t view uchar * to void
385e = new uchar 32 0
386put ds 3 add get ds 3 1
387n = drv_unhex t e
388if ne n 20
389drv_mis DRERR DRBAD
390ret
391end
392for i ulong 20
393if ne get d i get e i
394drv_mis DRERR DRBAD
395ret
396end
397end
398end
399
400note H record
401fn drv_h tk edit uchar 4096 to void
402m = new uchar 4096 0
403d = new uchar 32 0
404drv_name
405n = drv_unhex part tk 0 drv_tok tk m
406k = sha1_digest part m 0 n d
407drv_dig d part tk 0 drv_tok tk
408end
409
410note A record (message built in its own 1 MiB buffer)
411fn drv_a tk edit uchar 4096 to void
412bb = new uchar 1048576 0
413d = new uchar 32 0
414drv_name
415n = drv_num part tk 0 drv_tok tk
416v = drv_num part tk 0 drv_tok tk
417if gt n len bb
418put ds 3 add get ds 3 1
419drv_mis DRERR DRBAD
420ret
421end
422for i ulong n
423put bb i uchar v
424end
425k = sha1_digest part bb 0 n d
426drv_dig d part tk 0 drv_tok tk
427end
428
429note E record
430fn drv_e tk edit uchar 4096 to void
431b = new uchar 64 0
432drv_name
433w = new uchar 64 0
434wn = drv_tok w
435c = drv_num part tk 0 drv_tok tk
436put ds 3 add get ds 3 1
437n = rev_errname c b
438if and eq wn 1 eq get w 0 45
439if ne n 0
440drv_mis part b 0 n DRDASH
441end
442ret
443end
444if not der_same part b 0 n part w 0 wn
445drv_mis part b 0 n part w 0 wn
446end
447end
448
449note mb[q] xor a nonzero value taken from x shifted right by s
450fn drv_flip mb edit uchar 262144 q ulong x ulong s ulong to void
451y = add 1 rem shr x s 255
452v = uchar y
453put mb q xor get mb q v
454end
455
456note mutation k of the region pp[r] .. +pp[r+1] of bb into mb (the whole buffer copied): k mod 3 = 0
457note one byte changed (xor a nonzero value), 1 one byte xor 0x80 in the first 64 bytes of the region
458note (headers), 2 two bytes changed
459fn drv_mut mb edit uchar 262144 bb view uchar 262144 pp view ulong 16 k ulong to void
460for i ulong get ds 8
461put mb i get bb i
462end
463a = get pp get ds 10
464l = get pp add get ds 10 1
465x = add mul k 2654435761 12345
466x = xor x shr x 13
467m = rem k 3
468if eq m 1
469q = add a rem rem x l 64
470put mb q xor get mb q 128
471ret
472end
473q = add a rem x l
474drv_flip mb q x 17
475if eq m 2
476q = add a rem mul add sub q a 1 7 l
477drv_flip mb q x 29
478end
479end
480
481note cnt mutations (drv_mut) through rev_check; how many were refused
482fn drv_fz bb view uchar 262144 pp view ulong 16 cnt ulong to ulong
483mb = new uchar 262144 0
484n = get ds 8
485rj = ulong 0
486for j ulong cnt
487drv_mut mb bb pp j
488if ne rev_check part mb 0 n pp 0
489rj = add rj 1
490end
491end
492ret rj
493end
494
495note F and G records: r = 5 (staple) or 7 (CRL). The unmutated input must give the record's
496note expectation (F: accepted, G: refused); every mutation must be refused (code not 0)
497fn drv_f tk edit uchar 4096 bb edit uchar 262144 pp edit ulong 16 r ulong to void
498drv_name
499cnt = drv_num part tk 0 drv_tok tk
500put ds 3 add get ds 3 1
501if ne drv_load tk bb pp r 0
502drv_mis DRERR DRBAD
503ret
504end
505c0 = rev_check part bb 0 get ds 8 pp
506if and eq r 5 ne c0 0
507drv_mis DRBAD DRNO
508ret
509end
510if and eq r 7 eq c0 0
511drv_mis DRNO DRBAD
512ret
513end
514put ds 10 r
515rj = drv_fz bb pp cnt
516r2 = write 1 DRFUZ 0 len DRFUZ
517r2 = write 1 nm 0 get ds 6
518r2 = write 1 DRFN 0 len DRFN
519drv_prn cnt
520r2 = write 1 DRFR 0 len DRFR
521drv_prn rj
522r2 = write 1 DRNL 0 1
523if ne rj cnt
524drv_mis DRERR DRBAD
525end
526end
527
528fn main to int
529tk = new uchar 4096 0
530bb = new uchar 262144 0
531pp = new ulong 16 0
532if ne drv_slurp 0
533r = write 2 DRIO 0 len DRIO
534ret 2
535end
536while lt get ds 1 get ds 0 bound 999999
537put ds 2 add get ds 2 1
538c = drv_peek
539put ds 1 add get ds 1 1
540if eq c 79
541drv_o tk bb pp
542end
543if eq c 85
544drv_u tk bb pp
545end
546if eq c 73
547drv_i tk bb pp
548end
549if eq c 72
550drv_h tk
551end
552if eq c 65
553drv_a tk
554end
555if eq c 69
556drv_e tk
557end
558if eq c 70
559drv_f tk bb pp 5
560end
561if eq c 71
562drv_f tk bb pp 7
563end
564if ne c 10
565drv_eol
566end
567end
568r = write 1 DRSUM 0 len DRSUM
569drv_prn get ds 3
570r = write 1 DRCHK 0 len DRCHK
571drv_prn get ds 4
572r = write 1 DRFLD 0 len DRFLD
573if ne get ds 4 0
574ret 1
575end
576if eq get ds 3 0
577ret 1
578end
579ret 0
580end