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 / bogo / shim.om

366 lines · 6906 bytes Raw Blame History
1note shim.om: BoringSSL BoGo test shim for the om/tls client (program mode, composed after every
2note om/tls unit by om/tls/build.sh). Protocol as in BoringSSL ssl/test/bssl_shim.cc and
3note runner/runner.go at the commit in PINS:
4note connect to the runner on 127.0.0.1 or, with -ipv6, ::1 at -port, send -shim-id as 8 bytes
5note little-endian, run a TLS 1.3 client handshake for the name "test" against the PEM root of
6note -trust-cert, then echo: every byte read is written back XORed with 0xff, until the peer's
7note close_notify or a clean TCP end.
8note Flags handled: -port N, -shim-id N, -ipv6, -trust-cert PATH, -shim-writes-first ("hello" before
9note the first read), -shim-shuts-down (send close_notify instead of reading), -check-close-notify
10note (the read loop must end with the peer's close_notify), -max-version N (N >= 772),
11note -min-version N (N <= 772), -expect-version 772, -async and -expect-no-session (no effect: the
12note client is blocking and keeps no session cache). -is-handshaker-supported prints "No".
13note Every other flag, server tests included, exits 89 (the runner's "unimplemented").
14note Exit 0 on success; on a TLS failure print "TLSERR <name> detail <n>" on stderr and exit 1;
15note exit 2 for a bad flag value, 3 when the runner cannot be reached.
16
17text sh_port "-port"
18text sh_id "-shim-id"
19text sh_ipv6 "-ipv6"
20text sh_trust "-trust-cert"
21text sh_wf "-shim-writes-first"
22text sh_sd "-shim-shuts-down"
23text sh_ccn "-check-close-notify"
24text sh_maxv "-max-version"
25text sh_minv "-min-version"
26text sh_expv "-expect-version"
27text sh_async "-async"
28text sh_nos "-expect-no-session"
29text sh_hss "-is-handshaker-supported"
30text sh_no "No\n"
31text sh_name "test"
32text sh_hello "hello"
33text sh_err "TLSERR "
34text sh_det " detail "
35text sh_nl "\n"
36text sh_sp " "
37
38note a[0..n) equals text t
39fn sh_is a view uchar * n ulong t view uchar * to bool
40if ne n len t
41ret false
42end
43for i ulong n
44if ne get a i get t i
45ret false
46end
47end
48ret true
49end
50
51note argument i into a; its length, or 0 when it is empty or does not fit
52fn sh_arg i ulong a edit uchar * to ulong
53n = argread i a 0 len a
54if lt n 0
55ret 0
56end
57ret ulong n
58end
59
60note decimal a[0..n) (1..19 digits) into v[k]; 0, or 2 when it is not a number
61fn sh_num a view uchar * n ulong v edit ulong * k ulong to ulong
62if or eq n 0 gt n 19
63ret 2
64end
65x = ulong 0
66for i ulong n
67d = ulong get a i
68if or lt d 48 gt d 57
69ret 2
70end
71x = add mul x 10 sub d 48
72end
73put v k x
74ret 0
75end
76
77fn sh_dec v ulong o edit uchar * to ulong
78t = new uchar 24 0
79n = ulong 0
80x = v
81while true bound 24
82put t n uchar add 48 rem x 10
83n = add n 1
84x = div x 10
85if eq x 0
86break
87end
88end
89for i ulong n
90put o i get t sub sub n 1 i
91end
92ret n
93end
94
95fn sh_fail c view uchar * e ulong to int
96m = new uchar 256 0
97w = write 2 sh_err 0 len sh_err
98n = tls_errname e m
99w = write 2 m 0 n
100w = write 2 sh_det 0 len sh_det
101d = tls_detail c
102n = sh_dec d m
103w = write 2 m 0 n
104if or eq e 17 eq e 19
105n = x509_errname d m
106w = write 2 sh_sp 0 1
107w = write 2 m 0 n
108end
109w = write 2 sh_nl 0 1
110ret 1
111end
112
113note flags into v: 0 port, 1 shim id, 2 ipv6, 3 trust path length (path in p), 4 writes first,
114note 5 shuts down, 6 check close_notify, 7 scratch number; 0, 2 (bad value) or 89 (unimplemented)
115fn sh_parse a edit uchar * p edit uchar * v edit ulong * to ulong
116na = ulong argcnt
117i = ulong 1
118while lt i na bound 4096
119n = sh_arg i a
120i = add i 1
121if sh_is a n sh_ipv6
122put v 2 1
123continue
124end
125if sh_is a n sh_wf
126put v 4 1
127continue
128end
129if sh_is a n sh_sd
130put v 5 1
131continue
132end
133if sh_is a n sh_ccn
134put v 6 1
135continue
136end
137if or sh_is a n sh_async sh_is a n sh_nos
138continue
139end
140k = ulong 99
141if sh_is a n sh_port
142k = 0
143end
144if sh_is a n sh_id
145k = 1
146end
147if or sh_is a n sh_maxv or sh_is a n sh_minv sh_is a n sh_expv
148k = 7
149end
150if sh_is a n sh_trust
151if ge i na
152ret 2
153end
154m = sh_arg i p
155if eq m 0
156ret 2
157end
158put v 3 m
159i = add i 1
160continue
161end
162if eq k 99
163ret 89
164end
165if ge i na
166ret 2
167end
168m = sh_arg i a
169if ne sh_num part a 0 m m v k 0
170ret 2
171end
172i = add i 1
173if eq k 7
174r = get v 7
175if and sh_is a n sh_maxv lt r 772
176ret 89
177end
178if and sh_is a n sh_minv gt r 772
179ret 89
180end
181if and sh_is a n sh_expv ne r 772
182ret 89
183end
184end
185end
186if or eq get v 0 0 gt get v 0 65535
187ret 2
188end
189if and ne get v 5 0 ne get v 6 0
190ret 89
191end
192ret 0
193end
194
195note connect to 127.0.0.1 or ::1 at port (non-blocking connect, 10 s limit); fd or a negative value
196fn sh_open v6 ulong port ulong to long
197ip = new uchar 4 0
198sa = new uchar 28 0
199if eq v6 0
200put ip 0 127
201put ip 3 1
202ret tcp_open4 ip port
203end
204put sa 0 10
205put sa 2 uchar and shr port 8 255
206put sa 3 uchar and port 255
207put sa 23 1
208fd = socknew 10 526337 0
209if lt fd 0
210ret neg long 1
211end
212f = int fd
213r = connect f sa 0 28
214if eq r -115
215w = waitio f 4 10000000000
216if le w 0
217x = fclose f
218ret neg long 1
219end
220r = connect f sa 0 28
221if eq r -106
222r = 0
223end
224end
225if ne r 0
226x = fclose f
227ret neg long 1
228end
229ret fd
230end
231
232note the 8-byte little-endian shim id, all of it or 3
233fn sh_id8 f int id ulong to int
234b = new uchar 8 0
235for i ulong 8
236put b i uchar and shr id mul i 8 255
237end
238o = ulong 0
239while lt o 8 bound 64
240w = waitio f 4 10000000000
241if le w 0
242ret 3
243end
244r = send f b o sub 8 o 0
245if le r 0
246if eq r -11
247continue
248end
249ret 3
250end
251o = add o ulong r
252end
253ret 0
254end
255
256note echo loop: 0, or a TLS code (TLEEOF when -check-close-notify saw no close_notify)
257fn sh_echo c edit uchar * v view ulong * to ulong
258if ne get v 4 0
259r = tls_write c sh_hello
260if lt r 0
261ret ulong neg r
262end
263end
264if ne get v 5 0
265tls_close c
266ret 0
267end
268b = new uchar 512 0
269while true bound 999999
270r = tls_read c b
271if eq r 0
272tls_close c
273ret 0
274end
275if eq r neg long 4
276if ne get v 6 0
277ret 4
278end
279ret 0
280end
281if lt r 0
282ret ulong neg r
283end
284m = ulong r
285for i ulong m
286put b i uchar xor get b i 255
287end
288w = tls_write c part b 0 ulong r
289if lt w 0
290ret ulong neg w
291end
292end
293ret 7
294end
295
296note after an abort: read and discard what the runner already sent (bounded, 200 ms idle) so the
297note close is a FIN, not a TCP RST; an RST over unread data can drop the alert we just sent
298fn sh_drain f int to void
299b = new uchar 4096 0
300for i ulong 256
301w = waitio f 1 200000000
302if le w 0
303ret
304end
305n = recv f b 0 4096 0
306if le n 0
307ret
308end
309end
310end
311
312note handshake and echo on the runner connection; c holds the TLS state, st the trust anchors
313fn sh_run c edit uchar * st edit uchar * v view ulong * to int
314e = tls_init c sh_name 0
315if ne e 0
316ret sh_fail c e
317end
318fd = sh_open get v 2 get v 0
319if lt fd 0
320ret 3
321end
322f = int fd
323if ne sh_id8 f get v 1 0
324x = fclose f
325ret 3
326end
327e = tls_connect c st f
328if eq e 0
329e = sh_echo c v
330end
331if ne e 0
332sh_drain f
333end
334x = fclose f
335if eq e 0
336ret 0
337end
338ret sh_fail c e
339end
340
341fn main to int
342c = new uchar TLCSZ 0
343st = new uchar TLSTSZ 0
344a = new uchar 4096 0
345p = new uchar 4096 0
346v = new ulong 8 0
347if ge argcnt 2
348n = sh_arg 1 a
349if sh_is a n sh_hss
350w = write 1 sh_no 0 len sh_no
351ret 0
352end
353end
354e = sh_parse a p v
355if ne e 0
356ret int e
357end
358if eq get v 3 0
359ret 89
360end
361e = tls_storef st part p 0 get v 3
362if ne e 0
363ret sh_fail c e
364end
365ret sh_run c st v
366end