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

564 lines · 11559 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tests/drv/r8448.om: replay of RFC 8448 section 3 ("Simple 1-RTT Handshake") byte for byte.
3note The client (src/tls.om, with the test hooks of tests/tlshook.om) is given the trace's ClientHello and X25519 private
4note key, and pinned to the trace's server certificate (it has no subjectAltName and is RSA-1024: the replay lowers
5note ot_rsa_minbits to 1024; it does not touch name or path validation, which other tests cover). A forked replay server
6note sends the trace's server records unchanged and compares every byte the client sends with the trace:
7note the ClientHello record, the client Finished record, the client application data record (the trace's 50-byte
8note payload) and the client close_notify record.
9note The client must also decrypt the server's records (EncryptedExtensions..Finished, NewSessionTicket, application
10note data) and return exactly the trace's 50-byte server payload, and its four traffic secrets must equal the trace's.
11note usage: build/drv_r8448 tests/vectors/rfc8448/rfc8448.txt
12global rf_t uchar 1048576
13global rf_v ulong 16
14global rf_rec uchar 65536
15global rf_off ulong 64
16global rf_sec uchar 256
17text rf_s3 "3. Simple 1-RTT Handshake"
18text rf_s4 "4. Resumed 0-RTT Handshake"
19text rf_ckey "{client} create an ephemeral x25519 key pair:"
20text rf_priv "private key ("
21text rf_cch "{client} construct a ClientHello handshake message:"
22text rf_chm "ClientHello ("
23text rf_scert "{server} construct a Certificate handshake message:"
24text rf_certm "Certificate ("
25text rf_chs "{client} send handshake record:"
26text rf_shs "{server} send handshake record:"
27text rf_cap "{client} send application_data record:"
28text rf_sap "{server} send application_data record:"
29text rf_cal "{client} send alert record:"
30text rf_sal "{server} send alert record:"
31text rf_complete "complete record ("
32text rf_payload "payload ("
33text rf_d1 "derive secret \"tls13 c hs traffic\":"
34text rf_d2 "derive secret \"tls13 s hs traffic\":"
35text rf_d3 "derive secret \"tls13 c ap traffic\":"
36text rf_d4 "derive secret \"tls13 s ap traffic\":"
37text rf_exp "expanded ("
38text rf_host "server"
39text rf_via "127.0.0.1"
40text rf_none ""
41text rf_ok "PASS r8448 cases="
42text rf_bad "FAIL r8448 step="
43text rf_nl "\n"
44text rf_s_ch "FAIL ClientHello record differs from the trace\n"
45text rf_s_cf "FAIL client Finished record differs from the trace\n"
46text rf_s_ca "FAIL client application data record differs from the trace\n"
47text rf_s_cl "FAIL client close_notify record differs from the trace\n"
48text rf_s_sec "FAIL a traffic secret differs from the trace: "
49text rf_s_conn "FAIL the client did not complete the handshake: error "
50text rf_s_read "FAIL the client did not return the server's payload\n"
51text rf_s_srv "FAIL replay server status "
52text rf_s_parse "FAIL cannot find a trace item: "
53text rf_colon "):"
54
55fn rf_say t view uchar * to void
56k = write 1 t 0 len t
57end
58fn rf_num n ulong to void
59d = new uchar 24 0
60v = n
61i = ulong 24
62while true bound 30
63i = sub i 1
64put d i uchar add 48 rem v 10
65v = div v 10
66if eq v 0
67break
68end
69end
70k = write 1 d i sub 24 i
71end
72note ---- locating text in the trace ----
73fn rf_at p ulong t view uchar * to bool
74n = get rf_v 0
75if gt add p len t n
76ret false
77end
78for i ulong len t
79if ne get rf_t add p i get t i
80ret false
81end
82end
83ret true
84end
85note position just after the first occurrence of t at or after p, before e; 0 when absent
86fn rf_find p ulong e ulong t view uchar * to ulong
87q = p
88while lt q e bound 9223372036854775807
89if rf_at q t
90ret add q len t
91end
92q = add q 1
93end
94ret 0
95end
96note after label "NAME (N octets):" at p: N hex bytes into rf_rec[o..]; returns N, or 0
97fn rf_hex p ulong o ulong to ulong
98n = get rf_v 0
99q = p
100v = ulong 0
101while lt q n bound 100
102c = get rf_t q
103if or lt c 48 gt c 57
104break
105end
106v = add mul v 10 ulong sub c 48
107q = add q 1
108end
109q = rf_find q n rf_colon
110if eq q 0
111ret 0
112end
113k = ulong 0
114while and lt k v lt add q 1 n bound 9223372036854775807
115x = rf_tok q
116if lt x 256
117put rf_rec add o k uchar x
118k = add k 1
119q = add q 2
120else
121q = add q 1
122end
123end
124if ne k v
125ret 0
126end
127ret v
128end
129note the value of a whitespace-delimited two-hex-digit token at q, or 256 when there is none
130fn rf_tok q ulong to ulong
131n = get rf_v 0
132a = rf_hv get rf_t q
133b = rf_hv get rf_t add q 1
134if or ge a 16 ge b 16
135ret 256
136end
137if not rf_isws get rf_t sub q 1
138ret 256
139end
140if lt add q 2 n
141if not rf_isws get rf_t add q 2
142ret 256
143end
144end
145ret add mul a 16 b
146end
147fn rf_isws c uchar to bool
148ret or eq c 32 eq c 10
149end
150fn rf_hv c uchar to ulong
151if and ge c 48 le c 57
152ret ulong sub c 48
153end
154if and ge c 97 le c 102
155ret add 10 ulong sub c 97
156end
157ret 16
158end
159note item: the k-th (1-based) occurrence of heading h in section 3, then the item label lab after it, its bytes at
160note rf_rec[o..]; returns the length (0: not found)
161fn rf_item h view uchar * k ulong lab view uchar * o ulong to ulong
162s = get rf_v 1
163e = get rf_v 2
164p = s
165for i ulong k
166p = rf_find p e h
167if eq p 0
168ret 0
169end
170end
171q = rf_find p e lab
172if eq q 0
173ret 0
174end
175ret rf_hex q o
176end
177
178note ---- the replay server (child process) ----
179note slots: 0 CH record, 1 server SH record, 2 server flight record, 3 client Finished record, 4 NewSessionTicket record,
180note 5 client app data record, 6 server app data record, 7 client alert record, 8 server alert record;
181note rf_off[2i] = offset in rf_rec, rf_off[2i+1] = length
182fn rf_slot i ulong o ulong n ulong to void
183put rf_off mul i 2 o
184put rf_off add mul i 2 1 n
185end
186fn rf_send fd int i ulong to bool
187o = get rf_off mul i 2
188n = get rf_off add mul i 2 1
189b = new uchar 65536 0
190for j ulong n
191put b j get rf_rec add o j
192end
193p = ulong 0
194while lt p n bound 100000
195k = send fd b p sub n p 0
196if le k 0
197ret false
198end
199p = add p ulong k
200end
201ret true
202end
203fn rf_expect fd int i ulong to bool
204o = get rf_off mul i 2
205n = get rf_off add mul i 2 1
206b = new uchar 65536 0
207p = ulong 0
208t0 = monons
209while lt p n bound 100000
210k = recv fd b p sub n p 0
211if le k 0
212ret false
213end
214p = add p ulong k
215end
216for j ulong n
217if ne get b j get rf_rec add o j
218ret false
219end
220end
221ret true
222end
223fn rf_server lfd int to void
224fd = accept lfd 0
225if lt fd 0
226exit 10
227end
228f = int fd
229if not rf_expect f 0
230exit 11
231end
232if not and rf_send f 1 rf_send f 2
233exit 12
234end
235if not rf_expect f 3
236exit 13
237end
238if not rf_send f 4
239exit 14
240end
241if not rf_expect f 5
242exit 15
243end
244if not rf_send f 6
245exit 16
246end
247if not rf_expect f 7
248exit 17
249end
250ok = rf_send f 8
251exit 0
252end
253
254note ---- trace extraction ----
255fn rf_extract to ulong
256o = ulong 0
257n = rf_item rf_chs 1 rf_complete o
258rf_slot 0 o n
259o = add o n
260if eq n 0
261ret 1
262end
263n = rf_item rf_shs 1 rf_complete o
264rf_slot 1 o n
265o = add o n
266if eq n 0
267ret 2
268end
269n = rf_item rf_shs 2 rf_complete o
270rf_slot 2 o n
271o = add o n
272if eq n 0
273ret 3
274end
275n = rf_item rf_chs 2 rf_complete o
276rf_slot 3 o n
277o = add o n
278if eq n 0
279ret 4
280end
281n = rf_item rf_shs 3 rf_complete o
282rf_slot 4 o n
283o = add o n
284if eq n 0
285ret 5
286end
287ret rf_extract2 o
288end
289fn rf_extract2 p ulong to ulong
290o = p
291n = rf_item rf_cap 1 rf_complete o
292rf_slot 5 o n
293o = add o n
294if eq n 0
295ret 6
296end
297n = rf_item rf_sap 1 rf_complete o
298rf_slot 6 o n
299o = add o n
300if eq n 0
301ret 7
302end
303n = rf_item rf_cal 1 rf_complete o
304rf_slot 7 o n
305o = add o n
306if eq n 0
307ret 8
308end
309n = rf_item rf_sal 1 rf_complete o
310rf_slot 8 o n
311o = add o n
312if eq n 0
313ret 9
314end
315note payloads: 9 client app payload, 10 server app payload; 11 ClientHello message; 12 private key; 13 Certificate
316n = rf_item rf_cap 1 rf_payload o
317rf_slot 9 o n
318o = add o n
319n = rf_item rf_sap 1 rf_payload o
320rf_slot 10 o n
321o = add o n
322n = rf_item rf_cch 1 rf_chm o
323rf_slot 11 o n
324o = add o n
325n = rf_item rf_ckey 1 rf_priv o
326rf_slot 12 o n
327o = add o n
328n = rf_item rf_scert 1 rf_certm o
329rf_slot 13 o n
330o = add o n
331ret rf_extract3 o
332end
333fn rf_extract3 p ulong to ulong
334o = p
335n = rf_item rf_d1 1 rf_exp o
336rf_slot 14 o n
337o = add o n
338n = rf_item rf_d2 1 rf_exp o
339rf_slot 15 o n
340o = add o n
341n = rf_item rf_d3 1 rf_exp o
342rf_slot 16 o n
343o = add o n
344n = rf_item rf_d4 1 rf_exp o
345rf_slot 17 o n
346for i ulong 9
347if eq get rf_off add mul add 9 i 2 1 0
348ret add 10 i
349end
350end
351ret 0
352end
353note copy slot i into b; its length
354fn rf_get i ulong b edit uchar 65536 to ulong
355o = get rf_off mul i 2
356n = get rf_off add mul i 2 1
357for j ulong n
358put b j get rf_rec add o j
359end
360ret n
361end
362
363note ---- the client (parent) ----
364fn rf_client port ulong to ulong
365s = rf_setup
366if ne s 0
367ret s
368end
369ok = ot_tl_via rf_via
370c = ot_tl_connect rf_host port rf_none
371if ne c 0
372rf_say rf_s_conn
373rf_num ulong c
374rf_say rf_nl
375ret 21
376end
377r = rf_secrets
378if ne r 0
379ret r
380end
381r = rf_appdata
382ot_tl_close
383ret r
384end
385note the trace's ClientHello and private key, the pinned certificate, secret recording, RSA-1024 allowed
386fn rf_setup to ulong
387b = new uchar 65536 0
388k = new uchar 65536 0
389n = rf_get 11 b
390m = rf_get 12 k
391if or lt n 4 ne m 32
392ret 20
393end
394pk = new uchar 32 0
395for i ulong 32
396put pk i get k i
397end
398ot_th_setch part b 4 sub n 4 pk
399n = rf_get 13 b
400note Certificate message: header 4, context length 1 (0), list length 3, entry length 3, DER
401cl = or or shl ulong get b 8 16 shl ulong get b 9 8 ulong get b 10
402ot_th_pin part b 11 cl
403ot_th_keylog true
404put ot_rsa_minbits 0 1024
405ret 0
406end
407note send the trace's client payload, then the next read must return exactly the trace's server payload
408fn rf_appdata to ulong
409b = new uchar 65536 0
410k = new uchar 65536 0
411n = rf_get 9 b
412if not ot_tl_write part b 0 n
413ret 22
414end
415x = ot_tl_read k
416m = rf_get 10 b
417if ne x long m
418rf_say rf_s_read
419ret 23
420end
421for i ulong m
422if ne get k i get b i
423rf_say rf_s_read
424ret 23
425end
426end
427ret 0
428end
429note the four traffic secrets recorded by the test hook against the trace
430fn rf_secrets to ulong
431b = new uchar 65536 0
432for w ulong 4
433n = rf_get add 14 w b
434if ne n 32
435ret 24
436end
437for i ulong 32
438if ne get b i get ot_th_sec add mul add w 1 64 i
439rf_say rf_s_sec
440rf_num add w 1
441rf_say rf_nl
442ret 25
443end
444end
445end
446ret 0
447end
448
449fn rf_listen pa edit ulong 1 to long
450sa = new uchar 16 0
451rnd = new uchar 2 0
452for tries ulong 50
453k = random rnd 0 2 0
454pt = add 30000 rem or shl ulong get rnd 0 8 ulong get rnd 1 20000
455put sa 0 2
456put sa 2 uchar shr pt 8
457put sa 3 uchar and pt 255
458put sa 4 127
459put sa 7 1
460fd = socknew 2 524289 0
461b = bind int fd sa 0 16
462if eq b 0
463if eq listen int fd 4 0
464put pa 0 pt
465ret fd
466end
467end
468z = fclose int fd
469end
470ret -1
471end
472fn main to int
473if lt argcnt 2
474ret 2
475end
476x = rf_load
477if ne x 0
478rf_say rf_s_parse
479rf_num x
480rf_say rf_nl
481ret 1
482end
483pa = new ulong 1 0
484lfd = rf_listen pa
485if lt lfd 0
486ret 3
487end
488pid = fork
489if eq pid 0
490rf_server int lfd
491exit 99
492end
493z = fclose int lfd
494ot_tl_deadline 10000000000
495c = rf_client get pa 0
496ret rf_report c rf_reap pid
497end
498note read the trace named by argument 1 and extract every item; 0 or the missing item's number
499fn rf_load to ulong
500pb = new uchar 4096 0
501k = argread 1 pb 0 4096
502if le k 0
503ret 100
504end
505fd = fopen pb 0 ulong k 0 0
506if lt fd 0
507ret 101
508end
509n = ulong 0
510while lt n len rf_t bound 100000
511r = read int fd rf_t n sub len rf_t n
512if le r 0
513break
514end
515n = add n ulong r
516end
517z = fclose int fd
518put rf_v 0 n
519s = rf_find 0 n rf_s3
520s = rf_find s n rf_s3
521e = rf_find s n rf_s4
522if or eq s 0 eq e 0
523ret 102
524end
525put rf_v 1 s
526put rf_v 2 e
527ret rf_extract
528end
529fn rf_reap pid long to ulong
530b = new uchar 4 0
531w = waitpid int pid b 0 4 0
532st = or ulong get b 0 shl ulong get b 1 8
533if ne and st 127 0
534ret add 200 and st 127
535end
536ret and shr st 8 255
537end
538fn rf_report c ulong code ulong to int
539if and eq c 0 eq code 0
540rf_say rf_ok
541rf_num 18
542rf_say rf_nl
543ret 0
544end
545rf_say rf_bad
546rf_num c
547rf_say rf_nl
548rf_say rf_s_srv
549rf_num code
550rf_say rf_nl
551if eq code 11
552rf_say rf_s_ch
553end
554if eq code 13
555rf_say rf_s_cf
556end
557if eq code 15
558rf_say rf_s_ca
559end
560if eq code 17
561rf_say rf_s_cl
562end
563ret 1
564end