interplane.Get Omega
Read in your language

omtls

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

Brought by drakestapleton

branch main Branches Tags History

omtls / tests / drv / hostile.om

545 lines · 9033 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tests/drv/hostile.om: the client against the hostile test server (tests/srv/tlssrv.om), one forked server per
3note case on a random private loopback port. Each case states the client's defined result; the server then reads
4note everything the client sends until it closes and counts records that are not alerts: that count must be 0 in every
5note refusal case (no application data, so no plaintext, leaves the client after a refusal). A trap or signal in either
6note process fails the case.
7note usage: build/drv_hostile FIXTURE-DIR (tests/fixtures/srv)
8global hd_v ulong 16
9global hd_dir uchar 4096
10text hd_host "mail.test"
11text hd_via "127.0.0.1"
12text hd_alpn "imap"
13text hd_none ""
14text hd_ping "ping: the client's application data"
15text hd_root "/root.pem"
16text hd_key "/leaf-key.txt"
17text hd_good "/leaf-good.der"
18text hd_other "/leaf-other.der"
19text hd_expired "/leaf-expired.der"
20text hd_untrusted "/leaf-untrusted.der"
21text hd_pass "PASS "
22text hd_fail "FAIL "
23text hd_got " client="
24text hd_want " want="
25text hd_srv " server-extra-records="
26text hd_st " server-status="
27text hd_nl "\n"
28text hd_final "PASS hostile cases="
29text hd_finalf "FAIL hostile pass="
30text hd_ff " fail="
31text hd_n0 "ok"
32text hd_n1 "bad-finished"
33text hd_n2 "bad-certificate-verify"
34text hd_n3 "downgrade-tls12"
35text hd_n4 "hello-retry-request"
36text hd_n5 "low-order-key-share"
37text hd_n6 "unsolicited-extension"
38text hd_n7 "plaintext-handshake-after-keys"
39text hd_n8 "truncated-record"
40text hd_n9 "oversized-record"
41text hd_n10 "alert-flood"
42text hd_n11 "renegotiation-attempt"
43text hd_n12 "bad-record-mac"
44text hd_n13 "key-update-both-ways"
45text hd_n14 "truncated-handshake-record"
46text hd_n15 "oversized-handshake-record"
47text hd_n16 "eof-after-server-hello"
48text hd_n17 "wrong-certificate-name"
49text hd_n18 "expired-certificate"
50text hd_n19 "untrusted-root"
51text hd_n20 "ok-alpn-aes128"
52text hd_n21 "fragmented-flight"
53text hd_n22 "eof-without-close-notify"
54text hd_n23 "wrong-session-id-echo"
55text hd_n24 "pkcs1-certificate-verify"
56text hd_n25 "tls13-serverhello-with-downgrade-sentinel"
57text hd_n26 "certificate-entry-of-1000001-bytes"
58
59fn hd_say t view uchar * to void
60k = write 1 t 0 len t
61end
62fn hd_num n long to void
63d = new uchar 24 0
64v = n
65ng = lt v 0
66if ng
67v = neg v
68end
69i = ulong 24
70while true bound 30
71i = sub i 1
72put d i uchar add 48 rem v 10
73v = div v 10
74if eq v 0
75break
76end
77end
78if ng
79i = sub i 1
80put d i 45
81end
82k = write 1 d i sub 24 i
83end
84fn hd_name c ulong to void
85if eq c 0
86hd_say hd_n0
87end
88if eq c 1
89hd_say hd_n1
90end
91if eq c 2
92hd_say hd_n2
93end
94if eq c 3
95hd_say hd_n3
96end
97if eq c 4
98hd_say hd_n4
99end
100if eq c 5
101hd_say hd_n5
102end
103if eq c 6
104hd_say hd_n6
105end
106if eq c 7
107hd_say hd_n7
108end
109if eq c 8
110hd_say hd_n8
111end
112if eq c 9
113hd_say hd_n9
114end
115if eq c 10
116hd_say hd_n10
117end
118hd_name2 c
119end
120fn hd_name2 c ulong to void
121if eq c 11
122hd_say hd_n11
123end
124if eq c 12
125hd_say hd_n12
126end
127if eq c 13
128hd_say hd_n13
129end
130if eq c 14
131hd_say hd_n14
132end
133if eq c 15
134hd_say hd_n15
135end
136if eq c 16
137hd_say hd_n16
138end
139if eq c 17
140hd_say hd_n17
141end
142if eq c 18
143hd_say hd_n18
144end
145if eq c 19
146hd_say hd_n19
147end
148if eq c 20
149hd_say hd_n20
150end
151if eq c 21
152hd_say hd_n21
153end
154if eq c 22
155hd_say hd_n22
156end
157if eq c 23
158hd_say hd_n23
159end
160if eq c 24
161hd_say hd_n24
162end
163if eq c 25
164hd_say hd_n25
165end
166if eq c 26
167hd_say hd_n26
168end
169end
170note path = fixture dir + name, into p; length
171fn hd_path p edit uchar 4096 t view uchar * to ulong
172n = get hd_v 0
173for i ulong n
174put p i get hd_dir i
175end
176for i ulong len t
177put p add n i get t i
178end
179ret add n len t
180end
181
182note case table: server mode, leaf file (0 good, 1 other, 2 expired, 3 untrusted), forced suite, fragment size,
183note expected connect result, expected read result (0: the echo must work; checked only when connect gives 0)
184fn hd_case c ulong t edit long 6 to void
185for i ulong 6
186put t i 0
187end
188put t 0 long c
189put t 3 16384
190if le c 16
191put t 0 long c
192end
193if eq c 1
194put t 4 17
195end
196if eq c 2
197put t 4 16
198end
199if eq c 3
200put t 4 8
201end
202if eq c 4
203put t 4 10
204end
205if eq c 5
206put t 4 9
207end
208if eq c 6
209put t 4 11
210end
211if eq c 7
212put t 4 5
213end
214if eq c 8
215put t 5 -3
216end
217if eq c 9
218put t 5 -4
219end
220if eq c 10
221put t 5 -18
222end
223hd_case2 c t
224end
225fn hd_case2 c ulong t edit long 6 to void
226if eq c 11
227put t 5 -21
228end
229if eq c 12
230put t 5 -7
231end
232if eq c 14
233put t 4 3
234end
235if eq c 15
236put t 4 4
237end
238if eq c 16
239put t 4 3
240end
241if eq c 17
242put t 0 0
243put t 1 1
244put t 4 12
245end
246if eq c 18
247put t 0 0
248put t 1 2
249put t 4 13
250end
251if eq c 19
252put t 0 0
253put t 1 3
254put t 4 14
255end
256if eq c 20
257put t 0 0
258put t 2 4865
259end
260if eq c 21
261put t 0 0
262put t 3 7
263end
264if eq c 22
265put t 0 17
266put t 5 -3
267end
268if eq c 23
269put t 0 18
270put t 4 9
271end
272if eq c 24
273put t 0 19
274put t 4 9
275end
276if eq c 25
277put t 0 20
278put t 4 8
279end
280if eq c 26
281put t 0 21
282put t 4 15
283end
284end
285
286note the server process: load, accept one connection, run the mode, drain, exit with the extra-record count
287fn hd_server lfd int t view long 6 to void
288p = new uchar 4096 0
289q = new uchar 4096 0
290lf = get t 1
291n = ulong 0
292if eq lf 0
293n = hd_path p hd_good
294end
295if eq lf 1
296n = hd_path p hd_other
297end
298if eq lf 2
299n = hd_path p hd_expired
300end
301if eq lf 3
302n = hd_path p hd_untrusted
303end
304m = hd_path q hd_key
305if not ot_ts_load part p 0 n part q 0 m
306exit 90
307end
308ot_ts_mode ulong get t 0 ulong get t 2 ulong get t 3
309fd = accept lfd 0
310if lt fd 0
311exit 91
312end
313z = fclose lfd
314c = ot_ts_accept int fd
315md = get ot_ts_v 0
316early = or or eq md OT_TS_DOWNGRADE eq md OT_TS_HRR eq md OT_TS_EARLYEOF
317if eq md OT_TS_EARLYEOF
318z = shutdown int fd 1
319end
320if and eq c 0 not early
321hd_serve
322end
323bad = ot_ts_drain
324z = fclose int fd
325if gt bad 80
326exit 80
327end
328exit int bad
329end
330fn hd_serve to void
331m = get ot_ts_v 0
332if eq m OT_TS_KEYUPD
333if not ot_tl_keyupd 1
334exit 92
335end
336end
337if or or or eq m OT_TS_TRUNC eq m OT_TS_OVERSIZE or eq m OT_TS_ALERTS eq m OT_TS_RENEG eq m OT_TS_BADMAC
338ot_ts_hostile
339ret
340end
341if eq m OT_TS_NOCLOSE
342note end the stream without close_notify: the client must report truncation, not end of data
343put ot_ts_v 8 1
344z = shutdown ot_tl_fd 1
345ret
346end
347b = new uchar 4096 0
348while true bound 1000
349r = ot_tl_read b
350if le r 0
351break
352end
353if not ot_tl_write part b 0 ulong r
354break
355end
356end
357ot_tl_close
358end
359
360note the client side of one case: connect result, then (when it connected) the read result
361fn hd_client port ulong t view long 6 r edit long 2 to void
362p = new uchar 4096 0
363n = hd_path p hd_root
364k = ot_tl_roots part p 0 n
365if le k 0
366put r 0 999
367ret
368end
369ok = ot_tl_via hd_via
370c = long 0
371if eq get t 2 4865
372c = long ot_tl_connect hd_host port hd_alpn
373else
374c = long ot_tl_connect hd_host port hd_none
375end
376put r 0 c
377put r 1 0
378if ne c 0
379ret
380end
381b = new uchar 4096 0
382m = get t 0
383if or eq m 0 eq m 13
384if not ot_tl_write hd_ping
385put r 1 -23
386ret
387end
388end
389x = ot_tl_read b
390if gt x 0
391note an echo must return the same bytes
392if ne ulong x len hd_ping
393put r 1 -100
394end
395for i ulong len hd_ping
396if ne get b i get hd_ping i
397put r 1 -101
398end
399end
400ot_tl_close
401note after close, read and write give defined refusals (-22 / false), never a trap
402if ne ot_tl_read b -22
403put r 1 -103
404end
405if ot_tl_write hd_ping
406put r 1 -104
407end
408ret
409end
410if lt x 0
411put r 1 x
412ret
413end
414put r 1 -102
415end
416
417fn hd_listen port edit ulong 1 to long
418sa = new uchar 16 0
419rnd = new uchar 2 0
420one = new uchar 4 0
421put one 0 1
422for tries ulong 50
423k = random rnd 0 2 0
424pt = add 20000 rem or shl ulong get rnd 0 8 ulong get rnd 1 20000
425put sa 0 2
426put sa 2 uchar shr pt 8
427put sa 3 uchar and pt 255
428put sa 4 127
429put sa 7 1
430fd = socknew 2 524289 0
431if lt fd 0
432ret -1
433end
434note SO_REUSEADDR
435z = sockopt int fd 1 2 one 0 4
436b = bind int fd sa 0 16
437if eq b 0
438l = listen int fd 4
439if eq l 0
440put port 0 pt
441ret fd
442end
443end
444z = fclose int fd
445end
446ret -1
447end
448fn hd_run c ulong to bool
449t = new long 6 0
450hd_case c t
451pa = new ulong 1 0
452lfd = hd_listen pa
453if lt lfd 0
454ret false
455end
456pid = fork
457if eq pid 0
458hd_server int lfd t
459exit 99
460end
461z = fclose int lfd
462r = new long 2 0
463hd_client get pa 0 t r
464st = hd_wait pid
465want0 = get t 4
466want1 = get t 5
467ok = and eq get r 0 want0 eq st 0
468if and ok eq want0 0
469ok = eq get r 1 want1
470end
471if ok
472hd_say hd_pass
473else
474hd_say hd_fail
475end
476hd_name c
477hd_say hd_got
478hd_num get r 0
479hd_say hd_want
480hd_num want0
481if eq want0 0
482hd_say hd_got
483hd_num get r 1
484hd_say hd_want
485hd_num want1
486end
487hd_say hd_st
488hd_num long st
489hd_say hd_nl
490ret ok
491end
492note the server's exit status (its extra-record count), or 200+ for a signal
493fn hd_wait pid long to ulong
494b = new uchar 4 0
495t0 = monons
496while true bound 9223372036854775807
497w = waitpid int pid b 0 4 1
498if eq w pid
499st = or ulong get b 0 shl ulong get b 1 8
500if eq and st 127 0
501ret and shr st 8 255
502end
503ret add 200 and st 127
504end
505if gt sub monons t0 60000000000
506z = killpid int pid 9
507w = waitpid int pid b 0 4 0
508ret 255
509end
510z = sleepns 1000000
511end
512ret 255
513end
514fn main to int
515if lt argcnt 2
516ret 2
517end
518k = argread 1 hd_dir 0 4000
519if le k 0
520ret 2
521end
522put hd_v 0 ulong k
523ot_tl_deadline 10000000000
524pass = ulong 0
525fail = ulong 0
526for c ulong 27
527if hd_run c
528pass = add pass 1
529else
530fail = add fail 1
531end
532end
533if eq fail 0
534hd_say hd_final
535hd_num long pass
536hd_say hd_nl
537ret 0
538end
539hd_say hd_finalf
540hd_num long pass
541hd_say hd_ff
542hd_num long fail
543hd_say hd_nl
544ret 1
545end