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 / interop.om

418 lines · 7934 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tests/drv/interop.om: the client against OpenSSL's s_server as a VERIFICATION ORACLE (never linked, never in the
3note product). For each certificate type (RSA-2048 leaf signed by an RSA root: RSA-PSS CertificateVerify and a
4note PKCS#1 v1.5 certificate signature; ECDSA P-256 leaf signed by a P-384 root: ECDSA CertificateVerify and an
5note ecdsa-with-SHA384 certificate signature) and each suite, it starts `s_server -www` on a private loopback port,
6note connects as mail.test, sends an HTTP GET and requires the status page s_server writes, naming TLSv1.3 and the
7note suite. Then two refusals: a TLS 1.2-only s_server, and an s_server whose only group is P-256.
8note usage: build/drv_interop FIXTURE-DIR OPENSSL-PATH
9global io_v ulong 16
10global io_dir uchar 4096
11global io_ossl uchar 4096
12global io_pk uchar 8192
13global io_resp uchar 1048576
14text io_host "mail.test"
15text io_via "127.0.0.1"
16text io_none ""
17text io_roots "/roots-both.pem"
18text io_rsac "/leaf-good.pem"
19text io_rsak "/leaf.key"
20text io_ecc "/ec-leaf.pem"
21text io_eck "/ec-leaf.key"
22text io_a1 "s_server"
23text io_a2 "-accept"
24text io_a3 "-cert"
25text io_a4 "-key"
26text io_a5 "-tls1_3"
27text io_a6 "-ciphersuites"
28text io_a7 "-groups"
29text io_a8 "X25519"
30text io_a9 "-www"
31text io_a10 "-naccept"
32text io_a11 "2"
33text io_a12 "-quiet"
34text io_a13 "-tls1_2"
35text io_a14 "P-256"
36text io_s1 "TLS_AES_128_GCM_SHA256"
37text io_s3 "TLS_CHACHA20_POLY1305_SHA256"
38text io_v13 "TLSv1.3"
39text io_req "GET / HTTP/1.0\n\n"
40text io_devnull "/dev/null"
41text io_pass "PASS "
42text io_fail "FAIL "
43text io_c " cert="
44text io_su " suite="
45text io_res " result="
46text io_al " alert="
47text io_nl "\n"
48text io_rsa "rsa"
49text io_ec "ecdsa"
50text io_t12 "tls12-only-server"
51text io_p256 "p256-only-server"
52text io_final "PASS interop cases="
53text io_finalf "FAIL interop pass="
54text io_ff " fail="
55
56fn io_say t view uchar * to void
57k = write 1 t 0 len t
58end
59fn io_num n ulong to void
60d = new uchar 24 0
61v = n
62i = ulong 24
63while true bound 30
64i = sub i 1
65put d i uchar add 48 rem v 10
66v = div v 10
67if eq v 0
68break
69end
70end
71k = write 1 d i sub 24 i
72end
73note ---- the s_server argument packet in io_pk ----
74fn io_pkstart to void
75put io_v 4 2
76put io_v 5 0
77end
78fn io_pkt t view uchar * to void
79p = get io_v 4
80n = len t
81put io_pk p uchar and n 255
82put io_pk add p 1 uchar shr n 8
83for i ulong n
84put io_pk add add p 2 i get t i
85end
86put io_v 4 add add p 2 n
87put io_v 5 add get io_v 5 1
88end
89note a record holding fixture-dir + name
90fn io_pkf t view uchar * to void
91p = get io_v 4
92d = get io_v 0
93n = add d len t
94put io_pk p uchar and n 255
95put io_pk add p 1 uchar shr n 8
96for i ulong d
97put io_pk add add p 2 i get io_dir i
98end
99for i ulong len t
100put io_pk add add add p 2 d i get t i
101end
102put io_v 4 add add p 2 n
103put io_v 5 add get io_v 5 1
104end
105fn io_pkossl to void
106p = get io_v 4
107n = get io_v 1
108put io_pk p uchar and n 255
109put io_pk add p 1 uchar shr n 8
110for i ulong n
111put io_pk add add p 2 i get io_ossl i
112end
113put io_v 4 add add p 2 n
114put io_v 5 add get io_v 5 1
115end
116fn io_pkdone to void
117c = get io_v 5
118put io_pk 0 uchar and c 255
119put io_pk 1 uchar shr c 8
120end
121note "127.0.0.1:PORT" as one record
122fn io_pkaddr port ulong to void
123a = new uchar 32 0
124for i ulong len io_via
125put a i get io_via i
126end
127put a 9 58
128d = new uchar 8 0
129v = port
130k = ulong 0
131while true bound 10
132put d k uchar add 48 rem v 10
133k = add k 1
134v = div v 10
135if eq v 0
136break
137end
138end
139for i ulong k
140put a add 10 i get d sub sub k 1 i
141end
142io_pkt part a 0 add 10 k
143end
144note ---- one s_server process: kind 0 RSA, 1 ECDSA; suite 1 AES-128-GCM, 3 ChaCha; mode 0 normal, 1 TLS 1.2 only,
145note 2 P-256 only ----
146fn io_spawn port ulong kind ulong suite ulong mode ulong to long
147io_pkstart
148io_pkossl
149io_pkt io_a1
150io_pkt io_a2
151io_pkaddr port
152io_pkt io_a3
153if eq kind 0
154io_pkf io_rsac
155else
156io_pkf io_ecc
157end
158io_pkt io_a4
159if eq kind 0
160io_pkf io_rsak
161else
162io_pkf io_eck
163end
164io_spawn2 suite mode
165io_pkdone
166pid = fork
167if eq pid 0
168io_child
169end
170ret pid
171end
172note the child: standard streams on /dev/null, then the s_server packet
173fn io_child to void
174nf = fopen io_devnull 0 len io_devnull 2 0
175z = dupfd int nf 0 0
176z = dupfd int nf 1 0
177z = dupfd int nf 2 0
178r = execprog io_pk 0 get io_v 4
179exit 127
180end
181fn io_spawn2 suite ulong mode ulong to void
182if eq mode 1
183io_pkt io_a13
184else
185io_pkt io_a5
186io_pkt io_a6
187if eq suite 1
188io_pkt io_s1
189else
190io_pkt io_s3
191end
192end
193io_pkt io_a7
194if eq mode 2
195io_pkt io_a14
196else
197io_pkt io_a8
198end
199io_pkt io_a9
200io_pkt io_a10
201io_pkt io_a11
202io_pkt io_a12
203end
204note start s_server on a fresh random port and wait until it accepts TCP (a probe connection, hence -naccept 2);
205note three tries on three ports. pp[0] = port, pp[1] = pid. false when no s_server came up.
206fn io_up kind ulong suite ulong mode ulong pp edit ulong 2 to bool
207for t ulong 3
208port = io_port
209pid = io_spawn port kind suite mode
210if io_probe port
211put pp 0 port
212put pp 1 ulong pid
213ret true
214end
215z = killpid int pid 9
216io_reap pid
217end
218ret false
219end
220fn io_probe port ulong to bool
221t0 = monons
222while lt sub monons t0 10000000000 bound 100000
223fd = ot_tl_tcp io_via port
224if ge fd 0
225z = fclose int fd
226ret true
227end
228z = sleepns 50000000
229end
230ret false
231end
232note one handshake (the server is already up)
233fn io_try port ulong to int
234ret ot_tl_connect io_host port io_none
235end
236note GET and read the whole status page into io_resp; its length, or a negative error
237fn io_get to long
238if not ot_tl_write io_req
239ret -23
240end
241b = new uchar 65536 0
242n = ulong 0
243while true bound 100000
244r = ot_tl_read b
245if lt r 0
246ret r
247end
248if eq r 0
249break
250end
251for i ulong ulong r
252if lt add n i len io_resp
253put io_resp add n i get b i
254end
255end
256n = add n ulong r
257end
258ot_tl_close
259ret long n
260end
261fn io_has n ulong t view uchar * to bool
262m = len t
263if lt n m
264ret false
265end
266for p ulong add sub n m 1
267hit = true
268for i ulong m
269if ne get io_resp add p i get t i
270hit = false
271break
272end
273end
274if hit
275ret true
276end
277end
278ret false
279end
280fn io_port to ulong
281rnd = new uchar 2 0
282k = random rnd 0 2 0
283ret add 33000 rem or shl ulong get rnd 0 8 ulong get rnd 1 15000
284end
285fn io_case kind ulong suite ulong to bool
286pp = new ulong 2 0
287c = int 1
288pid = long 0
289if io_up kind suite 0 pp
290pid = long get pp 1
291c = io_try get pp 0
292end
293ok = false
294n = long 0
295if eq c 0
296n = io_get
297if gt n 0
298ok = io_has ulong n io_v13
299if eq suite 1
300ok = and ok io_has ulong n io_s1
301else
302ok = and ok io_has ulong n io_s3
303end
304end
305end
306z = killpid int pid 9
307io_reap pid
308if ok
309io_say io_pass
310else
311io_say io_fail
312end
313if eq kind 0
314io_say io_rsa
315else
316io_say io_ec
317end
318io_say io_su
319if eq suite 1
320io_say io_s1
321else
322io_say io_s3
323end
324io_say io_res
325io_num ulong c
326io_say io_nl
327ret ok
328end
329note a refusal case: the client must stop with error 18 (the server's alert) or a defined refusal, never connect
330fn io_refuse mode ulong to bool
331pp = new ulong 2 0
332c = int 1
333pid = long 0
334if io_up 0 1 mode pp
335pid = long get pp 1
336c = io_try get pp 0
337end
338a = ot_tl_alertrecv
339z = killpid int pid 9
340io_reap pid
341ok = and ne c 0 ne c 1
342if ok
343io_say io_pass
344else
345io_say io_fail
346end
347if eq mode 1
348io_say io_t12
349else
350io_say io_p256
351end
352io_say io_res
353io_num ulong c
354io_say io_al
355io_num ulong a
356io_say io_nl
357ret ok
358end
359fn io_reap pid long to void
360b = new uchar 4 0
361w = waitpid int pid b 0 4 0
362end
363fn main to int
364k = argread 1 io_dir 0 4000
365k2 = argread 2 io_ossl 0 4000
366if or le k 0 le k2 0
367ret 2
368end
369put io_v 0 ulong k
370put io_v 1 ulong k2
371p = new uchar 4096 0
372d = get io_v 0
373for i ulong d
374put p i get io_dir i
375end
376for i ulong len io_roots
377put p add d i get io_roots i
378end
379if le ot_tl_roots part p 0 add d len io_roots 0
380ret 2
381end
382ok = ot_tl_via io_via
383ot_tl_deadline 10000000000
384pass = ulong 0
385fail = ulong 0
386for kind ulong 2
387for s ulong 2
388su = ulong 1
389if eq s 1
390su = 3
391end
392if io_case kind su
393pass = add pass 1
394else
395fail = add fail 1
396end
397end
398end
399for m ulong 2
400if io_refuse add m 1
401pass = add pass 1
402else
403fail = add fail 1
404end
405end
406if eq fail 0
407io_say io_final
408io_num pass
409io_say io_nl
410ret 0
411end
412io_say io_finalf
413io_num pass
414io_say io_ff
415io_num fail
416io_say io_nl
417ret 1
418end