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 / srv / tlssrv.om

899 lines · 18540 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tests/srv/tlssrv.om: a TLS 1.3 SERVER for tests only (prefix ot_ts_). Never part of a product build.
3note It reuses the client's record layer and key schedule (src/tls.om: the same functions serve both directions
4note because a process holds one connection) and signs CertificateVerify with RSA-PSS-SHA256 using a test key
5note (tests/fixtures/srv/leaf-key.txt) through ot_bn_modexp. A mode makes it hostile (see ot_ts_modes below).
6note Uses: tests/drv/hostile.om (the refusal cases), the interop and speed drivers, and mailhand's fake IMAP/SMTP
7note servers (ot_ts_load, ot_ts_accept, then ot_tl_read / ot_tl_write / ot_tl_close as for a client).
8global ot_ts_v ulong 32
9global ot_ts_chain uchar 1048576
10global ot_ts_cl ulong 16
11global ot_ts_n uchar 1024
12global ot_ts_d uchar 1024
13global ot_ts_ob uchar 4194304
14global ot_ts_cpub uchar 32
15global ot_ts_buf uchar 1048576
16text ot_ts_t_mod "modulus:"
17text ot_ts_t_pexp "privateExponent:"
18text ot_ts_t_cv "TLS 1.3, server CertificateVerify"
19text ot_ts_t_hrr "HelloRetryRequest"
20note ot_ts_v: 0 mode, 1 forced suite (0 = server preference), 2 certificates, 3 modulus bytes, 4 output length,
21note 5 fragment size, 6 client offered x25519 share at (position), 7 suite chosen, 8 hostile action done,
22note 9 records seen after the hostile action that were not alerts (plaintext evidence), 10 ALPN position+1, 11 ALPN len
23const OT_TS_OK ulong 0
24const OT_TS_BADFIN ulong 1
25const OT_TS_BADCV ulong 2
26const OT_TS_DOWNGRADE ulong 3
27const OT_TS_HRR ulong 4
28const OT_TS_ZEROKEY ulong 5
29const OT_TS_UNSOLEXT ulong 6
30const OT_TS_PLAINHS ulong 7
31const OT_TS_TRUNC ulong 8
32const OT_TS_OVERSIZE ulong 9
33const OT_TS_ALERTS ulong 10
34const OT_TS_RENEG ulong 11
35const OT_TS_BADMAC ulong 12
36const OT_TS_KEYUPD ulong 13
37const OT_TS_HSTRUNC ulong 14
38const OT_TS_HSOVER ulong 15
39const OT_TS_EARLYEOF ulong 16
40const OT_TS_NOCLOSE ulong 17
41const OT_TS_BADSID ulong 18
42const OT_TS_PKCS1CV ulong 19
43const OT_TS_SENT13 ulong 20
44const OT_TS_BIGCERT ulong 21
45
46note ======== loading the certificate chain (DER files) and the RSA key (openssl text dump) ========
47fn ot_ts_readfile path view uchar * to long
48fd = fopen path 0 len path 0 0
49if lt fd 0
50ret -1
51end
52n = ulong 0
53while lt n len ot_ts_buf bound 9223372036854775807
54k = read int fd ot_ts_buf n sub len ot_ts_buf n
55if eq k -4
56continue
57end
58if le k 0
59break
60end
61n = add n ulong k
62end
63z = fclose int fd
64ret long n
65end
66fn ot_ts_addcert path view uchar * to bool
67n = ot_ts_readfile path
68if le n 0
69ret false
70end
71c = get ot_ts_v 2
72o = ulong 0
73for i ulong c
74o = add o get ot_ts_cl i
75end
76for i ulong ulong n
77put ot_ts_chain add o i get ot_ts_buf i
78end
79put ot_ts_cl c ulong n
80put ot_ts_v 2 add c 1
81ret true
82end
83fn ot_ts_hexv c uchar to ulong
84if and ge c 48 le c 57
85ret ulong sub c 48
86end
87if and ge c 97 le c 102
88ret add 10 ulong sub c 97
89end
90ret 16
91end
92note the hex block after label (lines of "xx:xx:..."), leading zero bytes dropped, into ot_ts_n (which 0) or ot_ts_d (1)
93fn ot_ts_block n ulong label view uchar * which ulong to ulong
94p = ot_ts_findlab n label
95if eq p 0
96ret 0
97end
98put ot_ts_v 12 0
99while lt add p 1 n bound 9223372036854775807
100if eq get ot_ts_buf p 10
101if ne get ot_ts_buf add p 1 32
102break
103end
104end
105if ot_ts_pair p which
106p = add p 2
107else
108p = add p 1
109end
110end
111ret get ot_ts_v 12
112end
113note the position after the first occurrence of label in ot_ts_buf[0, n), or 0
114fn ot_ts_findlab n ulong label view uchar * to ulong
115m = len label
116p = ulong 0
117while lt add p m n bound 9223372036854775807
118hit = true
119for i ulong m
120if ne get ot_ts_buf add p i get label i
121hit = false
122break
123end
124end
125if hit
126ret add p m
127end
128p = add p 1
129end
130ret 0
131end
132note one hex pair at p into the next byte (leading zero bytes dropped); false when p is not a hex pair
133fn ot_ts_pair p ulong which ulong to bool
134hi = ot_ts_hexv get ot_ts_buf p
135lo = ot_ts_hexv get ot_ts_buf add p 1
136if or ge hi 16 ge lo 16
137ret false
138end
139b = add mul hi 16 lo
140k = get ot_ts_v 12
141if and eq k 0 eq b 0
142ret true
143end
144ot_ts_put which k uchar b
145put ot_ts_v 12 add k 1
146ret true
147end
148fn ot_ts_put which ulong k ulong b uchar to void
149if eq which 0
150put ot_ts_n k b
151else
152put ot_ts_d k b
153end
154end
155note load: chain = DER file paths separated by ',' (leaf first), key = the openssl rsa -text dump
156fn ot_ts_load chain view uchar * key view uchar * to bool
157put ot_ts_v 2 0
158s = ulong 0
159for i ulong add len chain 1
160brk = true
161if lt i len chain
162brk = eq get chain i 44
163end
164if brk
165if not ot_ts_addpart chain s sub i s
166ret false
167end
168s = add i 1
169end
170end
171ret ot_ts_loadkey key
172end
173fn ot_ts_addpart chain view uchar * s ulong l ulong to bool
174if or eq l 0 gt l 4096
175ret false
176end
177pb = new uchar 4096 0
178for j ulong l
179put pb j get chain add s j
180end
181ret ot_ts_addcert part pb 0 l
182end
183fn ot_ts_loadkey key view uchar * to bool
184n = ot_ts_readfile key
185if le n 0
186ret false
187end
188nb = ot_ts_block ulong n ot_ts_t_mod 0
189db = ot_ts_block ulong n ot_ts_t_pexp 1
190if or eq nb 0 gt db nb
191ret false
192end
193note right-align d to the modulus length
194if lt db nb
195sh = sub nb db
196for i ulong db
197j = sub sub db 1 i
198put ot_ts_d add j sh get ot_ts_d j
199end
200for i ulong sh
201put ot_ts_d i 0
202end
203end
204put ot_ts_v 3 nb
205put ot_ts_v 5 16384
206ret true
207end
208fn ot_ts_mode m ulong suite ulong frag ulong to void
209put ot_ts_v 0 m
210put ot_ts_v 1 suite
211if gt frag 0
212put ot_ts_v 5 frag
213end
214end
215
216note ======== output buffering of handshake messages (sent as records of the fragment size) ========
217fn ot_ts_msg tr edit shactx t ulong b view uchar * to void
218n = len b
219put ot_tl_v OT_TL_HTYP t
220put ot_tl_v OT_TL_HLEN n
221for i ulong n bound 16777215
222put ot_tl_hm i get b i
223end
224ot_tl_tradd tr
225o = get ot_ts_v 4
226put ot_ts_ob o uchar t
227put ot_ts_ob add o 1 uchar shr n 16
228put ot_ts_ob add o 2 uchar and shr n 8 255
229put ot_ts_ob add o 3 uchar and n 255
230for i ulong n bound 16777215
231put ot_ts_ob add add o 4 i get b i
232end
233put ot_ts_v 4 add add o 4 n
234end
235fn ot_ts_flush to bool
236n = get ot_ts_v 4
237f = get ot_ts_v 5
238c = new uchar 16384 0
239p = ulong 0
240while lt p n bound 9223372036854775807
241m = sub n p
242if gt m f
243m = f
244end
245for i ulong m
246put c i get ot_ts_ob add p i
247end
248if not ot_tl_wrec 22 part c 0 m
249ret false
250end
251p = add p m
252end
253put ot_ts_v 4 0
254ret true
255end
256
257note ======== ClientHello ========
258note find the x25519 key share and ALPN; choose the suite; 0 or a nonzero failure
259fn ot_ts_ch to ulong
260n = get ot_tl_v OT_TL_HLEN
261if lt n 39
262ret 1
263end
264sl = ot_tl_h8 34
265for i ulong sl
266put ot_tl_sid i get ot_tl_hm add 35 i
267end
268put ot_tl_v OT_TL_SIDN sl
269p = add 35 sl
270cl = ot_tl_h16 p
271c = ot_ts_suite add p 2 cl
272if eq c 0
273ret 2
274end
275put ot_ts_v 7 c
276p = add add p 2 cl
277p = add add p 1 ot_tl_h8 p
278e = add add p 2 ot_tl_h16 p
279p = add p 2
280put ot_ts_v 6 0
281put ot_ts_v 10 0
282while lt p e bound 65536
283t = ot_tl_h16 p
284l = ot_tl_h16 add p 2
285if eq t 51
286ot_ts_keyshare add p 4 l
287end
288if eq t 16
289put ot_ts_v 10 add add p 7 1
290put ot_ts_v 11 ot_tl_h8 add p 6
291end
292p = add add p 4 l
293end
294if eq get ot_ts_v 6 0
295ret 3
296end
297ret 0
298end
299fn ot_ts_keyshare p ulong l ulong to void
300q = add p 2
301e = add p l
302while lt add q 4 e bound 65536
303g = ot_tl_h16 q
304kl = ot_tl_h16 add q 2
305if and eq g 29 eq kl 32
306put ot_ts_v 6 add q 4
307end
308q = add add q 4 kl
309end
310end
311fn ot_ts_suite p ulong n ulong to ulong
312f = get ot_ts_v 1
313for k ulong 2
314want = ulong 4867
315if eq k 1
316want = 4865
317end
318if gt f 0
319want = f
320end
321for i ulong div n 2
322if eq ot_tl_h16 add p mul i 2 want
323ret want
324end
325end
326end
327ret 0
328end
329
330note ======== ServerHello ========
331fn ot_ts_sh tr edit shactx pub view uchar 32 to bool
332b = new uchar 256 0
333m = get ot_ts_v 0
334put b 0 3
335put b 1 3
336r = new uchar 32 0
337ok = ot_tl_hk_random r
338for i ulong 32
339put b add 2 i get r i
340end
341if eq m OT_TS_HRR
342ot_ts_hrrrand b
343end
344if or eq m OT_TS_DOWNGRADE eq m OT_TS_SENT13
345put b 26 68
346put b 27 79
347put b 28 87
348put b 29 78
349put b 30 71
350put b 31 82
351put b 32 68
352put b 33 1
353end
354sl = get ot_tl_v OT_TL_SIDN
355put b 34 uchar sl
356for i ulong sl
357put b add 35 i get ot_tl_sid i
358end
359if and eq m OT_TS_BADSID gt sl 0
360put b 35 xor get b 35 1
361put ot_ts_v 8 1
362end
363p = add 35 sl
364s = get ot_ts_v 7
365if eq m OT_TS_DOWNGRADE
366s = 49199
367end
368put b p uchar shr s 8
369put b add p 1 uchar and s 255
370put b add p 2 0
371p = add p 3
372if eq m OT_TS_DOWNGRADE
373put b p 0
374put b add p 1 0
375p = add p 2
376else
377p = ot_ts_shext b p pub
378end
379ot_ts_msg tr 2 part b 0 p
380ret ot_ts_flush
381end
382fn ot_ts_shext b edit uchar 256 p ulong pub view uchar 32 to ulong
383put b p 0
384put b add p 1 46
385q = add p 2
386put b q 0
387put b add q 1 43
388put b add q 2 0
389put b add q 3 2
390put b add q 4 3
391put b add q 5 4
392q = add q 6
393put b q 0
394put b add q 1 51
395put b add q 2 0
396put b add q 3 36
397put b add q 4 0
398put b add q 5 29
399put b add q 6 0
400put b add q 7 32
401for i ulong 32
402put b add add q 8 i get pub i
403end
404ret add q 40
405end
406note derived here independently of the client's constants: SHA-256 of the ASCII text
407fn ot_ts_hrrrand b edit uchar 256 to void
408d = new uchar 32 0
409sha256_hash ot_ts_t_hrr d
410for i ulong 32
411put b add 2 i get d i
412end
413end
414
415note ======== the server handshake ========
416fn ot_ts_accept fd int to int
417ot_tl_init
418for i ulong 64
419if and ne i OT_TL_DL ne i OT_TL_DLSET
420put ot_tl_v i 0
421end
422end
423put ot_tl_v OT_TL_FD ulong fd
424put ot_tl_v OT_TL_ST OT_TL_HS
425put ot_tl_v OT_TL_RSL 16384
426put ot_ts_v 4 0
427put ot_ts_v 8 0
428c = ot_ts_hand
429k = scrub 1048576
430if ne c 0
431note tests drain the socket after a refusal, so the descriptor stays open here (the driver closes it)
432put ot_tl_v OT_TL_ERR c
433put ot_tl_v OT_TL_ST OT_TL_FAILED
434ret int c
435end
436put ot_tl_v OT_TL_ST OT_TL_OPEN
437ret 0
438end
439fn ot_ts_hand to ulong
440sk = new uchar 640 0
441tr = new uchar 104 0
442sha256_init tr
443r = ot_tl_rdhs
444if ne r 1
445ret 5
446end
447ot_tl_tradd tr
448c = ot_ts_ch
449if ne c 0
450ret 10
451end
452c = ot_ts_keys tr sk
453if ne c 0
454ret c
455end
456m = get ot_ts_v 0
457if or or eq m OT_TS_DOWNGRADE eq m OT_TS_HRR eq m OT_TS_EARLYEOF
458put ot_ts_v 8 1
459ret 0
460end
461c = ot_ts_flight tr sk
462if ne c 0
463ret c
464end
465ret ot_ts_cfin tr sk
466end
467note key exchange and ServerHello; handshake keys in both directions
468fn ot_ts_keys tr edit shactx sk edit uchar 640 to ulong
469priv = new uchar 32 0
470pub = new uchar 32 0
471cp = new uchar 32 0
472dh = new uchar 64 0
473if not ot_tl_hk_random priv
474ret 19
475end
476ot_x25_base pub priv
477o = get ot_ts_v 6
478for i ulong 32
479put cp i get ot_tl_hm add o i
480end
481d = new uchar 32 0
482ot_x25_mul d priv cp
483for i ulong 32
484put dh i get d i
485put d i 0
486put priv i 0
487end
488if eq get ot_ts_v 0 OT_TS_ZEROKEY
489for i ulong 32
490put pub i 0
491end
492end
493if not ot_ts_sh tr pub
494ret 23
495end
496if eq get ot_ts_v 0 OT_TS_EARLYEOF
497ret 0
498end
499if gt get ot_tl_v OT_TL_SIDN 0
500ok = ot_tl_ccs
501end
502s = get ot_ts_v 7
503put ot_tl_v OT_TL_SUITE s
504put ot_tl_v OT_TL_AEAD 3
505put ot_tl_v OT_TL_KLEN 32
506if eq s 4865
507put ot_tl_v OT_TL_AEAD 1
508put ot_tl_v OT_TL_KLEN 16
509end
510c2 = ot_tl_hsec tr sk dh
511ot_tl_zero64 dh
512if ne c2 0
513ret ulong c2
514end
515note ot_tl_hsec set read = server hs (client view); swap for the server
516x = new uchar 64 0
517ot_tl_sk sk OT_TL_K_CHS x
518ok = ot_tl_keys 0 x
519ot_tl_sk sk OT_TL_K_SHS x
520ok = ot_tl_keys 1 x
521ot_tl_zero64 x
522put ot_tl_v OT_TL_CCS 1
523ret 0
524end
525note EncryptedExtensions, Certificate, CertificateVerify, Finished (with the hostile variants)
526fn ot_ts_flight tr edit shactx sk edit uchar 640 to ulong
527m = get ot_ts_v 0
528if or eq m OT_TS_HSTRUNC eq m OT_TS_HSOVER
529ot_ts_hostile
530ret 0
531end
532if eq m OT_TS_PLAINHS
533put ot_tl_v OT_TL_WENC 0
534put ot_ts_v 8 1
535end
536ot_ts_ee tr
537if eq get ot_ts_v 0 OT_TS_BIGCERT
538ot_ts_bigcert tr
539else
540if not ot_ts_cert tr
541ret 20
542end
543end
544if not ot_ts_cv tr
545ret 20
546end
547ot_ts_fin tr sk
548if not ot_ts_flush
549ret 23
550end
551ot_ts_appsw tr sk
552ret 0
553end
554fn ot_ts_ee tr edit shactx to void
555b = new uchar 300 0
556p = ulong 2
557an = get ot_ts_v 11
558if gt get ot_ts_v 10 0
559put b 2 0
560put b 3 16
561put b 4 0
562put b 5 uchar add an 3
563put b 6 0
564put b 7 uchar add an 1
565put b 8 uchar an
566a = sub get ot_ts_v 10 1
567for i ulong an
568put b add 9 i get ot_tl_hm add a i
569end
570p = add 9 an
571end
572if eq get ot_ts_v 0 OT_TS_UNSOLEXT
573put b p 255
574put b add p 1 0
575put b add p 2 0
576put b add p 3 0
577p = add p 4
578put ot_ts_v 8 1
579end
580put b 0 uchar shr sub p 2 8
581put b 1 uchar and sub p 2 255
582ot_ts_msg tr 8 part b 0 p
583end
584note one certificate entry of 1000001 zero bytes (a length past a for-loop's default pass guard, below the 1 MiB
585note entry limit): the client must refuse it with a defined error, not stop with a trap
586fn ot_ts_bigcert tr edit shactx to void
587b = new uchar 1000016 0
588l = ulong 1000001
589t = add l 5
590put b 1 uchar shr t 16
591put b 2 uchar and shr t 8 255
592put b 3 uchar and t 255
593put b 4 uchar shr l 16
594put b 5 uchar and shr l 8 255
595put b 6 uchar and l 255
596put ot_ts_v 8 1
597ot_ts_msg tr 11 part b 0 add 4 t
598end
599fn ot_ts_cert tr edit shactx to bool
600c = get ot_ts_v 2
601tot = ulong 0
602for i ulong c
603tot = add add tot get ot_ts_cl i 5
604end
605b = new uchar 65536 0
606if gt add tot 4 65536
607ret false
608end
609put b 0 0
610put b 1 uchar shr tot 16
611put b 2 uchar and shr tot 8 255
612put b 3 uchar and tot 255
613p = ulong 4
614o = ulong 0
615for i ulong c
616l = get ot_ts_cl i
617p = ot_ts_entry b p o l
618o = add o l
619end
620ot_ts_msg tr 11 part b 0 p
621ret true
622end
623note one CertificateEntry: 24-bit length, the DER at ot_ts_chain[o, o+l), no extensions; the next position
624fn ot_ts_entry b edit uchar 65536 p ulong o ulong l ulong to ulong
625put b p uchar shr l 16
626put b add p 1 uchar and shr l 8 255
627put b add p 2 uchar and l 255
628for j ulong l
629put b add add p 3 j get ot_ts_chain add o j
630end
631q = add add p 3 l
632put b q 0
633put b add q 1 0
634ret add q 2
635end
636note CertificateVerify, rsa_pss_rsae_sha256 (RFC 8446 4.4.3, RFC 8017 9.1.1 EMSA-PSS-ENCODE, salt = 32 random bytes)
637fn ot_ts_cv tr edit shactx to bool
638th = new uchar 64 0
639ot_tl_trhash tr th
640msg = new uchar 130 0
641for i ulong 64
642put msg i 32
643end
644for i ulong len ot_ts_t_cv
645put msg add 64 i get ot_ts_t_cv i
646end
647for i ulong 32
648put msg add 98 i get th i
649end
650mh = new uchar 32 0
651sha256_hash msg mh
652nb = get ot_ts_v 3
653em = new uchar 1024 0
654pk1 = eq get ot_ts_v 0 OT_TS_PKCS1CV
655if pk1
656ot_ts_pkcs1 mh part em 0 nb
657put ot_ts_v 8 1
658else
659if not ot_ts_pss mh part em 0 nb
660ret false
661end
662end
663if not ot_ts_rsa part em 0 nb
664ret false
665end
666b = new uchar 1100 0
667put b 0 8
668put b 1 4
669if pk1
670put b 0 4
671put b 1 1
672end
673put b 2 uchar shr nb 8
674put b 3 uchar and nb 255
675for i ulong nb
676put b add 4 i get em i
677end
678if eq get ot_ts_v 0 OT_TS_BADCV
679put b 40 xor get b 40 1
680put ot_ts_v 8 1
681end
682ot_ts_msg tr 15 part b 0 add 4 nb
683ret true
684end
685note EMSA-PKCS1-v1_5 with SHA-256 (RFC 8017 9.2): 00 01 FF.. 00 DigestInfo hash; for the scheme-refusal case only
686fn ot_ts_pkcs1 mh view uchar 32 em edit uchar * to void
687di = new uchar 19 0
688put di 0 48
689put di 1 49
690put di 2 48
691put di 3 13
692put di 4 6
693put di 5 9
694put di 6 96
695put di 7 134
696put di 8 72
697put di 9 1
698put di 10 101
699put di 11 3
700put di 12 4
701put di 13 2
702put di 14 1
703put di 15 5
704put di 16 0
705put di 17 4
706put di 18 32
707n = len em
708put em 0 0
709put em 1 1
710ps = sub sub n 51 3
711for i ulong ps
712put em add 2 i 255
713end
714put em add 2 ps 0
715for i ulong 19
716put em add add 3 ps i get di i
717end
718for i ulong 32
719put em add add 22 ps i get mh i
720end
721end
722fn ot_ts_pss mh view uchar 32 em edit uchar * to bool
723n = len em
724if lt n 66
725ret false
726end
727salt = new uchar 32 0
728ok = ot_tl_hk_random salt
729mp = new uchar 72 0
730for i ulong 32
731put mp add 8 i get mh i
732put mp add 40 i get salt i
733end
734h = new uchar 32 0
735sha256_hash mp h
736dl = sub sub n 32 1
737db = new uchar 1024 0
738put db sub sub dl 32 1 1
739for i ulong 32
740put db add sub dl 32 i get salt i
741end
742mask = new uchar 1024 0
743ot_rsa_mgf1 0 h part mask 0 dl
744for i ulong dl
745put em i xor get db i get mask i
746end
747put em 0 and get em 0 127
748for i ulong 32
749put em add dl i get h i
750end
751put em sub n 1 188
752ret true
753end
754fn ot_ts_rsa em edit uchar * to bool
755nb = get ot_ts_v 3
756n = new uchar 1024 0
757d = new uchar 1024 0
758for i ulong nb
759put n i get ot_ts_n i
760put d i get ot_ts_d i
761end
762ret ot_bn_modexp part n 0 nb part d 0 nb em
763end
764fn ot_ts_fin tr edit shactx sk edit uchar 640 to void
765th = new uchar 64 0
766ot_tl_trhash tr th
767b = new uchar 64 0
768vd = new uchar 64 0
769ot_tl_sk sk OT_TL_K_SHS b
770ok = ot_tl_vdata b th vd
771if eq get ot_ts_v 0 OT_TS_BADFIN
772put vd 3 xor get vd 3 128
773put ot_ts_v 8 1
774end
775ot_ts_msg tr 20 part vd 0 32
776end
777note after the flight: application secrets; the server writes with the server application keys at once and reads
778note the client's handshake keys until the client's Finished (ot_tl_appkeys sets the client's view, swapped here)
779fn ot_ts_appsw tr edit shactx sk edit uchar 640 to void
780c = ot_tl_appkeys tr sk
781x = new uchar 64 0
782ot_tl_sk sk OT_TL_K_SAP x
783ok = ot_tl_keys 1 x
784ot_tl_sk sk OT_TL_K_CHS x
785ok = ot_tl_keys 0 x
786ot_tl_zero64 x
787end
788fn ot_ts_cfin tr edit shactx sk edit uchar 640 to ulong
789put ot_tl_v OT_TL_CCS 1
790r = ot_tl_rdhs
791if ne r 20
792ret 5
793end
794th = new uchar 64 0
795for i ulong 64
796put th i get sk add OT_TL_K_TH i
797end
798note the client's Finished covers the transcript through the server Finished (no client Certificate here)
799b = new uchar 64 0
800vd = new uchar 64 0
801ot_tl_sk sk OT_TL_K_CHS b
802ok = ot_tl_vdata b th vd
803for i ulong 32
804if ne get vd i get ot_tl_hm i
805ret 17
806end
807end
808ot_tl_tradd tr
809x = new uchar 64 0
810ot_tl_sk sk OT_TL_K_CAP x
811ok = ot_tl_keys 0 x
812ot_tl_zero64 x
813put ot_tl_v OT_TL_CCS 0
814ret 0
815end
816
817note ======== after the handshake: hostile actions ========
818fn ot_ts_raw b view uchar * to bool
819ret ot_tl_sendall b
820end
821fn ot_ts_hostile to void
822m = get ot_ts_v 0
823put ot_ts_v 8 1
824b = new uchar 16700 0
825a = new uchar 2 0
826h = new uchar 8 0
827if or eq m OT_TS_TRUNC eq m OT_TS_HSTRUNC
828put b 0 23
829put b 1 3
830put b 2 3
831put b 3 0
832put b 4 100
833ok = ot_ts_raw part b 0 55
834z = shutdown ot_tl_fd 1
835end
836if or eq m OT_TS_OVERSIZE eq m OT_TS_HSOVER
837put b 0 23
838put b 1 3
839put b 2 3
840put b 3 65
841put b 4 1
842ok = ot_ts_raw part b 0 16646
843end
844if eq m OT_TS_ALERTS
845note warning-level user_canceled alerts
846put a 0 1
847put a 1 90
848for i ulong 1000
849ok = ot_tl_wrec 21 a
850end
851end
852if eq m OT_TS_RENEG
853note a HelloRequest (type 0, empty): renegotiation does not exist in TLS 1.3
854put h 0 0
855ok = ot_tl_wrec 22 part h 0 4
856end
857if eq m OT_TS_BADMAC
858ot_ts_badmac
859end
860end
861fn ot_ts_badmac to void
862d = new uchar 32 65
863ok = ot_tl_wrec23x d
864end
865note an application_data record with one ciphertext bit flipped
866fn ot_tl_wrec23x d view uchar 32 to bool
867wb = new uchar 16645 0
868for i ulong 32
869put wb add 5 i get d i
870end
871put wb 37 23
872put wb 0 23
873put wb 1 3
874put wb 2 3
875put wb 3 0
876put wb 4 49
877ok = ot_tl_seal wb 49
878put ot_tl_v OT_TL_WSEQ add get ot_tl_v OT_TL_WSEQ 1
879put wb 10 xor get wb 10 4
880ret ot_tl_sendall part wb 0 54
881end
882note read what the client sends after a hostile action until it closes: count records that are not alerts
883note (a plaintext alert is 2 bytes, a protected alert 19); any other record is evidence of data sent after a refusal
884fn ot_ts_drain to ulong
885rb = new uchar 16645 0
886bad = ulong 0
887while true bound 100000
888n = ot_tl_rdrec rb
889if lt n 0
890break
891end
892t = get rb 0
893if not or and eq t 21 eq n 2 and eq t 23 eq n 19
894bad = add bad 1
895end
896end
897put ot_ts_v 9 bad
898ret bad
899end