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 / src / tls.om

2176 lines · 45480 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tls.om: TLS 1.3 client (RFC 8446), client side only, one connection per process (prefix ot_tl_).
3note Profile: TLS_CHACHA20_POLY1305_SHA256 and TLS_AES_128_GCM_SHA256, X25519 key share, certificate authentication of
4note the server (src/x509.om, src/pkv.om), SNI, optional ALPN (one protocol), KeyUpdate, close_notify, an empty client
5note Certificate when the server asks for one. Not in this slice: TLS 1.2 or lower (refused, never downgraded), PSK,
6note resumption, 0-RTT, HelloRetryRequest (refused), other groups and suites, client certificates.
7note Records (RFC 8446 5): plaintext records at most 2^14 bytes, protected records at most 2^14 + 256 bytes, inner
8note plaintext at most 2^14 + 1 bytes; a larger record is refused with record_overflow before it is read. These are
9note the protocol's own wire boundaries. Handshake messages are reassembled into a 16 MiB global (2^24 - 1 bytes is the
10note largest body the 24-bit length field can carry). One certificate entry is copied into a 1 MiB local array to be
11note handed to the X.509 code; a larger certificate is refused with a defined error (the omc boundaries: globals cannot
12note be passed to functions and local arrays are at most 4 MiB; see the README "Limits" table).
13note Secrets: the handshake secrets live in one local array of ot_tl_hand and are overwritten before it returns; the
14note traffic keys live in the globals below and are overwritten by ot_tl_close and on failure; the dead stack below the
15note caller is zeroed with the OM-2 `scrub` word after the handshake and after every record operation.
16note Error codes (ot_tl_err, ot_tl_why): see ot_tl_why below; every refusal is one of them, never a trap.
17
18const OT_TL_FD ulong 0
19const OT_TL_ST ulong 1
20const OT_TL_SUITE ulong 2
21const OT_TL_AEAD ulong 3
22const OT_TL_KLEN ulong 4
23const OT_TL_RSEQ ulong 5
24const OT_TL_WSEQ ulong 6
25const OT_TL_RENC ulong 7
26const OT_TL_WENC ulong 8
27const OT_TL_ERR ulong 9
28const OT_TL_DL ulong 10
29const OT_TL_PS ulong 11
30const OT_TL_PE ulong 12
31const OT_TL_QS ulong 13
32const OT_TL_QE ulong 14
33const OT_TL_HLEN ulong 15
34const OT_TL_HTYP ulong 16
35const OT_TL_ALERT ulong 17
36const OT_TL_SENT ulong 18
37const OT_TL_HOSTN ulong 19
38const OT_TL_ALPNN ulong 20
39const OT_TL_VIAN ulong 21
40const OT_TL_SIDN ulong 22
41const OT_TL_CCS ulong 23
42const OT_TL_RTYPE ulong 24
43const OT_TL_RLEN ulong 25
44const OT_TL_CREQ ulong 26
45const OT_TL_RSL ulong 27
46const OT_TL_FIRST ulong 28
47const OT_TL_SYS ulong 29
48const OT_TL_SNI ulong 30
49const OT_TL_DLSET ulong 31
50const OT_TL_NOFF ulong 32
51const OT_TL_SCHEME ulong 33
52const OT_TL_X5 ulong 34
53const OT_TL_SVS ulong 35
54const OT_TL_KSP ulong 36
55const OT_TL_KSL ulong 37
56const OT_TL_SHX ulong 38
57const OT_TL_OPDL ulong 39
58note states
59const OT_TL_IDLE ulong 0
60const OT_TL_HS ulong 1
61const OT_TL_OPEN ulong 2
62const OT_TL_PEERCLOSED ulong 3
63const OT_TL_FAILED ulong 4
64const OT_TL_CLOSED ulong 5
65note internal results of the record layer (negative)
66const OT_TL_EOF long -1000
67
68global ot_tl_v ulong 64
69global ot_tl_rk uchar 44
70global ot_tl_wk uchar 44
71global ot_tl_rs uchar 64
72global ot_tl_ws uchar 64
73global ot_tl_hq uchar 32800
74global ot_tl_hm uchar 16777215
75global ot_tl_pend uchar 16400
76global ot_tl_host uchar 256
77global ot_tl_alpn uchar 256
78global ot_tl_viab uchar 64
79global ot_tl_sid uchar 32
80global ot_tl_offx uchar 65536
81global ot_tl_offs ulong 64
82global ot_tl_cr uchar 256
83note the transcript hash through the server Finished (public: both peers compute it from the messages on the wire)
84global ot_tl_thg uchar 32
85
86text ot_tl_t_pfx "tls13 "
87text ot_tl_t_key "key"
88text ot_tl_t_iv "iv"
89text ot_tl_t_fin "finished"
90text ot_tl_t_der "derived"
91text ot_tl_t_chs "c hs traffic"
92text ot_tl_t_shs "s hs traffic"
93text ot_tl_t_cap "c ap traffic"
94text ot_tl_t_sap "s ap traffic"
95text ot_tl_t_upd "traffic upd"
96text ot_tl_t_cv "TLS 1.3, server CertificateVerify"
97text ot_tl_t_empty ""
98
99note ======== small helpers ========
100fn ot_tl_get i ulong to ulong
101ret get ot_tl_v i
102end
103fn ot_tl_set i ulong x ulong to void
104put ot_tl_v i x
105end
106fn ot_tl_fd to int
107ret int get ot_tl_v OT_TL_FD
108end
109note the deadline of the current operation: one absolute deadline per handshake, per ot_tl_read and per ot_tl_write
110note (OPDL, set by ot_tl_opstart), so a stream of empty or ignored records cannot keep an operation alive
111fn ot_tl_until to long
112d = get ot_tl_v OT_TL_OPDL
113if gt d 0
114ret long d
115end
116ret add monons long get ot_tl_v OT_TL_DL
117end
118fn ot_tl_opstart to void
119put ot_tl_v OT_TL_OPDL ulong add monons long get ot_tl_v OT_TL_DL
120end
121fn ot_tl_opend to void
122put ot_tl_v OT_TL_OPDL 0
123end
124note big-endian reads of the current handshake body (ot_tl_hm); callers check bounds against HLEN first
125fn ot_tl_h8 i ulong to ulong
126ret ulong get ot_tl_hm i
127end
128fn ot_tl_h16 i ulong to ulong
129ret or shl ulong get ot_tl_hm i 8 ulong get ot_tl_hm add i 1
130end
131fn ot_tl_h24 i ulong to ulong
132ret or shl ulong get ot_tl_hm i 16 ot_tl_h16 add i 1
133end
134fn ot_tl_zero b edit uchar * to void
135for i ulong len b
136put b i 0
137end
138end
139fn ot_tl_zero64 b edit uchar 64 to void
140for i ulong 64
141put b i 0
142end
143end
144fn ot_tl_wipekeys to void
145for i ulong 44
146put ot_tl_rk i 0
147put ot_tl_wk i 0
148end
149for i ulong 64
150put ot_tl_rs i 0
151put ot_tl_ws i 0
152end
153end
154
155note ======== failure: record the first error, send a fatal alert when the connection allows it, close ========
156note alert for each error code (RFC 8446 6.2); 0 = send none
157fn ot_tl_alertfor code ulong to ulong
158a = new uchar 32 0
159put a 4 22
160put a 5 10
161put a 6 50
162put a 7 20
163put a 8 70
164put a 9 47
165put a 10 40
166put a 11 110
167put a 12 42
168put a 13 45
169put a 14 48
170put a 15 42
171put a 16 51
172put a 17 51
173put a 19 80
174put a 20 80
175put a 21 10
176put a 24 43
177put a 25 109
178put a 26 42
179if ge code 32
180ret 80
181end
182ret ulong get a code
183end
184fn ot_tl_fail code ulong to long
185if eq get ot_tl_v OT_TL_ERR 0
186put ot_tl_v OT_TL_ERR code
187end
188st = get ot_tl_v OT_TL_ST
189if or eq st OT_TL_HS eq st OT_TL_OPEN
190a = ot_tl_alertfor code
191if gt a 0
192ok = ot_tl_sendalert a
193end
194end
195ot_tl_shut
196put ot_tl_v OT_TL_ST OT_TL_FAILED
197ret long code
198end
199note close the socket and overwrite the keys; the error code stays readable
200fn ot_tl_shut to void
201st = get ot_tl_v OT_TL_ST
202if or or eq st OT_TL_HS eq st OT_TL_OPEN eq st OT_TL_PEERCLOSED
203z = fclose ot_tl_fd
204end
205ot_tl_wipekeys
206put ot_tl_v OT_TL_PS 0
207put ot_tl_v OT_TL_PE 0
208for i ulong len ot_tl_pend
209put ot_tl_pend i 0
210end
211end
212note level 1 (warning) for close_notify, as RFC 8448 shows and RFC 8446 6.1 implies; 2 (fatal) for every error alert
213fn ot_tl_sendalert desc ulong to bool
214a = new uchar 2 0
215put a 0 2
216if eq desc 0
217put a 0 1
218end
219put a 1 uchar desc
220put ot_tl_v OT_TL_SENT desc
221ret ot_tl_wrec 21 a
222end
223note map an internal negative record-layer result to an error code
224fn ot_tl_code r long to ulong
225if eq r OT_TL_EOF
226ret 3
227end
228if lt r 0
229ret ulong neg r
230end
231ret 20
232end
233
234note ======== socket I/O with the per-operation deadline ========
235fn ot_tl_sendall b view uchar * to bool
236fd = ot_tl_fd
237n = len b
238p = ulong 0
239until = ot_tl_until
240while lt p n bound 9223372036854775807
241left = sub until monons
242if le left 0
243ret false
244end
245z = waitio fd 4 left
246if eq z -4
247continue
248end
249if le z 0
250ret false
251end
252k = send fd b p sub n p 0
253if or eq k -11 eq k -4
254continue
255end
256if le k 0
257put ot_tl_v OT_TL_SYS ulong neg k
258ret false
259end
260p = add p ulong k
261end
262ret true
263end
264note read exactly n bytes into b[off, off+n): n, the count read before end of stream (< n), -2 timeout, -23 I/O error
265fn ot_tl_recvx b edit uchar * off ulong n ulong to long
266fd = ot_tl_fd
267p = ulong 0
268until = ot_tl_until
269while lt p n bound 9223372036854775807
270left = sub until monons
271if le left 0
272ret -2
273end
274z = waitio fd 1 left
275if eq z -4
276continue
277end
278if eq z 0
279ret -2
280end
281if lt z 0
282ret -23
283end
284k = recv fd b add off p sub n p 0
285if or eq k -11 eq k -4
286continue
287end
288if lt k 0
289put ot_tl_v OT_TL_SYS ulong neg k
290ret -23
291end
292if eq k 0
293ret long p
294end
295p = add p ulong k
296end
297ret long n
298end
299
300note ======== record layer ========
301note read one record into rb: returns its length (header in rb[0..5)), OT_TL_EOF at a clean record boundary, or -code
302fn ot_tl_rdrec rb edit uchar 16645 to long
303k = ot_tl_recvx rb 0 5
304if eq k 0
305ret OT_TL_EOF
306end
307if lt k 0
308ret k
309end
310if lt k 5
311ret -3
312end
313t = get rb 0
314if or lt t 20 gt t 23
315ret -5
316end
317n = or shl ulong get rb 3 8 ulong get rb 4
318lim = ulong 16384
319if eq get ot_tl_v OT_TL_RENC 1
320lim = 16640
321end
322if gt n lim
323ret -4
324end
325k = ot_tl_recvx rb 5 n
326if lt k 0
327ret k
328end
329if ne ulong k n
330ret -3
331end
332ret long n
333end
334note the AEAD key and nonce for one record: key, then iv xor the 64-bit sequence number (RFC 8446 5.3)
335fn ot_tl_nonce kn edit uchar 44 key view uchar 44 seq ulong to void
336for i ulong 44
337put kn i get key i
338end
339for i ulong 8
340b = uchar and shr seq mul sub 7 i 8 255
341put kn add 36 i xor get kn add 36 i b
342end
343end
344fn ot_tl_rnonce kn edit uchar 44 to void
345k = new uchar 44 0
346for i ulong 44
347put k i get ot_tl_rk i
348end
349ot_tl_nonce kn k get ot_tl_v OT_TL_RSEQ
350for i ulong 44
351put k i 0
352end
353end
354fn ot_tl_wnonce kn edit uchar 44 to void
355k = new uchar 44 0
356for i ulong 44
357put k i get ot_tl_wk i
358end
359ot_tl_nonce kn k get ot_tl_v OT_TL_WSEQ
360for i ulong 44
361put k i 0
362end
363end
364note decrypt the protected record of length n in rb; inner type, or -code. Plaintext at rb[5, 5+RLEN)
365fn ot_tl_dec rb edit uchar 16645 n ulong to long
366if lt n 17
367ret -7
368end
369kn = new uchar 44 0
370ad = new uchar 5 0
371for i ulong 5
372put ad i get rb i
373end
374ot_tl_rnonce kn
375ok = ot_ae_open get ot_tl_v OT_TL_AEAD kn ad part rb 5 n
376for i ulong 44
377put kn i 0
378end
379if not ok
380ret -7
381end
382s = get ot_tl_v OT_TL_RSEQ
383if eq s 18446744073709551615
384ret -20
385end
386put ot_tl_v OT_TL_RSEQ add s 1
387m = sub n 16
388while gt m 0 bound 16640
389if ne get rb add 4 m 0
390break
391end
392m = sub m 1
393end
394if eq m 0
395ret -5
396end
397t = get rb add 4 m
398m = sub m 1
399if gt m 16384
400ret -4
401end
402put ot_tl_v OT_TL_RTYPE ulong t
403put ot_tl_v OT_TL_RLEN m
404ret long t
405end
406note the next record with its content: content type with plaintext at rb[5, 5+RLEN), or -code.
407note Drops change_cipher_spec while it is allowed (RFC 8446 5); a received alert ends the connection:
408note close_notify gives OT_TL_EOF with the peer-closed state, any other alert -18 with its description kept.
409fn ot_tl_rec rb edit uchar 16645 to long
410while true bound 9223372036854775807
411n = ot_tl_rdrec rb
412if lt n 0
413ret n
414end
415t = get rb 0
416if eq t 20
417note at most one change_cipher_spec (RFC 8446 D.4 sends exactly one), and never inside a handshake message
418if and and eq get ot_tl_v OT_TL_CCS 1 eq n 1 eq get rb 5 1
419if ot_tl_qempty
420put ot_tl_v OT_TL_CCS 2
421continue
422end
423end
424ret -5
425end
426if eq get ot_tl_v OT_TL_RENC 1
427if ne t 23
428ret -5
429end
430r = ot_tl_dec rb ulong n
431if lt r 0
432ret r
433end
434t = uchar r
435else
436if eq t 23
437ret -5
438end
439put ot_tl_v OT_TL_RTYPE ulong t
440put ot_tl_v OT_TL_RLEN ulong n
441end
442if eq t 21
443ret ot_tl_gotalert rb
444end
445ret long t
446end
447ret -20
448end
449fn ot_tl_gotalert rb view uchar 16645 to long
450if ne get ot_tl_v OT_TL_RLEN 2
451ret -6
452end
453d = get rb 6
454if eq d 0
455put ot_tl_v OT_TL_ST OT_TL_PEERCLOSED
456ret OT_TL_EOF
457end
458put ot_tl_v OT_TL_ALERT ulong d
459ret -18
460end
461note write one record of content type ct holding d (at most the record size limit); protected once WENC is set
462fn ot_tl_wrec ct ulong d view uchar * to bool
463n = len d
464if gt n 16384
465ret false
466end
467wb = new uchar 16645 0
468for i ulong n
469put wb add 5 i get d i
470end
471if eq get ot_tl_v OT_TL_WENC 1
472s = get ot_tl_v OT_TL_WSEQ
473if eq s 18446744073709551615
474ret false
475end
476put wb add 5 n uchar ct
477m = add n 17
478put wb 0 23
479put wb 1 3
480put wb 2 3
481put wb 3 uchar shr m 8
482put wb 4 uchar and m 255
483ok = ot_tl_seal wb m
484put ot_tl_v OT_TL_WSEQ add s 1
485if not ok
486ret false
487end
488ret ot_tl_sendall part wb 0 add 5 m
489end
490put wb 0 uchar ct
491put wb 1 3
492put wb 2 3
493if eq get ot_tl_v OT_TL_FIRST 1
494put wb 2 1
495put ot_tl_v OT_TL_FIRST 0
496end
497put wb 3 uchar shr n 8
498put wb 4 uchar and n 255
499ret ot_tl_sendall part wb 0 add 5 n
500end
501fn ot_tl_seal wb edit uchar 16645 m ulong to bool
502kn = new uchar 44 0
503ad = new uchar 5 0
504for i ulong 5
505put ad i get wb i
506end
507ot_tl_wnonce kn
508ot_ae_seal get ot_tl_v OT_TL_AEAD kn ad part wb 5 m
509for i ulong 44
510put kn i 0
511end
512ret true
513end
514
515note ======== handshake message reassembly (RFC 8446 5.1: messages may span records, records may hold several) ========
516fn ot_tl_fill to long
517rb = new uchar 16645 0
518t = ot_tl_rec rb
519if lt t 0
520ret t
521end
522if ne t 22
523ret -5
524end
525m = get ot_tl_v OT_TL_RLEN
526if eq m 0
527ret -5
528end
529qs = get ot_tl_v OT_TL_QS
530qe = get ot_tl_v OT_TL_QE
531for i ulong sub qe qs
532put ot_tl_hq i get ot_tl_hq add qs i
533end
534qe = sub qe qs
535if gt add qe m len ot_tl_hq
536ret -20
537end
538for i ulong m
539put ot_tl_hq add qe i get rb add 5 i
540end
541put ot_tl_v OT_TL_QS 0
542put ot_tl_v OT_TL_QE add qe m
543for i ulong add 5 m
544put rb i 0
545end
546ret 0
547end
548fn ot_tl_q24 i ulong to ulong
549hi = shl ulong get ot_tl_hq i 16
550mi = shl ulong get ot_tl_hq add i 1 8
551ret or or hi mi ulong get ot_tl_hq add i 2
552end
553note the next handshake message: its type, body in ot_tl_hm[0, HLEN); or -code
554fn ot_tl_rdhs to long
555while lt sub get ot_tl_v OT_TL_QE get ot_tl_v OT_TL_QS 4 bound 9223372036854775807
556r = ot_tl_fill
557if lt r 0
558ret r
559end
560end
561qs = get ot_tl_v OT_TL_QS
562t = ulong get ot_tl_hq qs
563l = ot_tl_q24 add qs 1
564put ot_tl_v OT_TL_QS add qs 4
565got = ulong 0
566while lt got l bound 9223372036854775807
567if eq get ot_tl_v OT_TL_QE get ot_tl_v OT_TL_QS
568r = ot_tl_fill
569if lt r 0
570ret r
571end
572end
573qs = get ot_tl_v OT_TL_QS
574m = sub get ot_tl_v OT_TL_QE qs
575if gt m sub l got
576m = sub l got
577end
578for i ulong m
579put ot_tl_hm add got i get ot_tl_hq add qs i
580end
581put ot_tl_v OT_TL_QS add qs m
582got = add got m
583end
584put ot_tl_v OT_TL_HLEN l
585put ot_tl_v OT_TL_HTYP t
586ret long t
587end
588fn ot_tl_qempty to bool
589ret eq get ot_tl_v OT_TL_QS get ot_tl_v OT_TL_QE
590end
591note send one handshake message (type t, body b) as records of at most 16384 bytes
592fn ot_tl_wrhs t ulong b view uchar * to bool
593n = len b
594h = new uchar 16384 0
595put h 0 uchar t
596put h 1 uchar shr n 16
597put h 2 uchar and shr n 8 255
598put h 3 uchar and n 255
599tot = add n 4
600p = ulong 0
601while lt p tot bound 9223372036854775807
602m = sub tot p
603if gt m 16384
604m = 16384
605end
606for i ulong m
607j = add p i
608if lt j 4
609put h i get h j
610else
611put h i get b sub j 4
612end
613end
614if not ot_tl_wrec 22 part h 0 m
615ret false
616end
617p = add p m
618end
619ret true
620end
621
622note ======== transcript hash (SHA-256: both offered suites use it) ========
623fn ot_tl_tradd tr edit shactx to void
624h = new uchar 4 0
625l = get ot_tl_v OT_TL_HLEN
626put h 0 uchar get ot_tl_v OT_TL_HTYP
627put h 1 uchar shr l 16
628put h 2 uchar and shr l 8 255
629put h 3 uchar and l 255
630sha256_update tr h
631b = new uchar 65536 0
632p = ulong 0
633while lt p l bound 9223372036854775807
634m = sub l p
635if gt m 65536
636m = 65536
637end
638for i ulong m
639put b i get ot_tl_hm add p i
640end
641sha256_update tr part b 0 m
642p = add p m
643end
644end
645note the hash so far: a copy of the running context is finalized, so the live one is never used after final
646fn ot_tl_trhash tr edit shactx o edit uchar 64 to void
647d = new uchar 32 0
648cp = new uchar 104 0
649for i ulong 104
650put cp i get tr i
651end
652sha256_final cp d
653for i ulong 104
654put cp i 0
655end
656for i ulong 32
657put o i get d i
658end
659for i ulong 32
660put o add 32 i 0
661end
662end
663
664note ======== key schedule (RFC 8446 7.1) ========
665note HkdfLabel: uint16 length, opaque label<7..255> = "tls13 " + label, opaque context<0..255>
666fn ot_tl_info info edit uchar 330 label view uchar * ctx view uchar 64 cl ulong ol ulong to ulong
667put info 0 uchar shr ol 8
668put info 1 uchar and ol 255
669ll = add len ot_tl_t_pfx len label
670put info 2 uchar ll
671o = ulong 3
672for i ulong len ot_tl_t_pfx
673put info add o i get ot_tl_t_pfx i
674end
675o = add o len ot_tl_t_pfx
676for i ulong len label
677put info add o i get label i
678end
679o = add o len label
680put info o uchar cl
681o = add o 1
682for i ulong cl
683put info add o i get ctx i
684end
685ret add o cl
686end
687note HKDF-Expand-Label(secret, label, "", len dst)
688fn ot_tl_expl sec view uchar 64 label view uchar * dst edit uchar * to bool
689info = new uchar 330 0
690c = new uchar 64 0
691n = ot_tl_info info label c 0 len dst
692ret ot_hk_expand 0 sec part info 0 n dst
693end
694note HKDF-Expand-Label(secret, label, ctx[0..32), len dst)
695fn ot_tl_explc sec view uchar 64 label view uchar * ctx view uchar 64 dst edit uchar * to bool
696info = new uchar 330 0
697n = ot_tl_info info label ctx 32 len dst
698ret ot_hk_expand 0 sec part info 0 n dst
699end
700note Derive-Secret(secret, label, messages) with the transcript hash th: 32 bytes into o[0..32), o[32..64) zero
701fn ot_tl_derive sec view uchar 64 label view uchar * th view uchar 64 o edit uchar 64 to bool
702d = new uchar 32 0
703ok = ot_tl_explc sec label th d
704for i ulong 32
705put o i get d i
706put d i 0
707end
708for i ulong 32
709put o add 32 i 0
710end
711ret ok
712end
713fn ot_tl_emptyhash o edit uchar 64 to void
714e = new uchar 1 0
715d = new uchar 32 0
716sha256_hash part e 0 0 d
717for i ulong 32
718put o i get d i
719end
720end
721note traffic keys from a traffic secret into ot_tl_rk (dir 0) or ot_tl_wk (dir 1), saving the secret for KeyUpdate
722fn ot_tl_keys dir ulong sec view uchar 64 to bool
723k = new uchar 32 0
724ivb = new uchar 12 0
725kl = get ot_tl_v OT_TL_KLEN
726ok = ot_tl_expl sec ot_tl_t_key part k 0 kl
727ok2 = ot_tl_expl sec ot_tl_t_iv ivb
728kn = new uchar 44 0
729for i ulong kl
730put kn i get k i
731end
732for i ulong 12
733put kn add 32 i get ivb i
734end
735for i ulong 44
736if eq dir 0
737put ot_tl_rk i get kn i
738else
739put ot_tl_wk i get kn i
740end
741put kn i 0
742end
743for i ulong 64
744if eq dir 0
745put ot_tl_rs i get sec i
746else
747put ot_tl_ws i get sec i
748end
749end
750for i ulong 32
751put k i 0
752end
753for i ulong 12
754put ivb i 0
755end
756if eq dir 0
757put ot_tl_v OT_TL_RSEQ 0
758put ot_tl_v OT_TL_RENC 1
759else
760put ot_tl_v OT_TL_WSEQ 0
761put ot_tl_v OT_TL_WENC 1
762end
763ret and ok ok2
764end
765note copy secret slot `so` (64 bytes) of the handshake secret array out / in
766fn ot_tl_sk sk view uchar 640 so ulong o edit uchar 64 to void
767for i ulong 64
768put o i get sk add so i
769end
770end
771fn ot_tl_skput sk edit uchar 640 so ulong s view uchar 64 to void
772for i ulong 64
773put sk add so i get s i
774end
775end
776note slots of the handshake secret array sk (uchar 640)
777const OT_TL_K_HS ulong 64
778const OT_TL_K_CHS ulong 128
779const OT_TL_K_SHS ulong 192
780const OT_TL_K_MS ulong 256
781const OT_TL_K_CAP ulong 320
782const OT_TL_K_SAP ulong 384
783const OT_TL_K_PRIV ulong 448
784const OT_TL_K_DH ulong 512
785const OT_TL_K_TH ulong 576
786
787note ======== ClientHello (RFC 8446 4.1.2) ========
788note offered signature schemes for CertificateVerify and certificates (RSA PKCS#1 v1.5 only for certificates)
789fn ot_tl_schemes o edit uchar 32 to ulong
790s = new ulong 11 0
791put s 0 1027
792put s 1 1283
793put s 2 2052
794put s 3 2053
795put s 4 2054
796put s 5 2057
797put s 6 2058
798put s 7 2059
799put s 8 1025
800put s 9 1281
801put s 10 1537
802for i ulong 11
803put o mul i 2 uchar shr get s i 8
804put o add mul i 2 1 uchar and get s i 255
805end
806ret 22
807end
808fn ot_tl_cvscheme x ulong to bool
809ret or or or eq x 1027 eq x 1283 or eq x 2052 eq x 2053 or or eq x 2054 eq x 2057 or eq x 2058 eq x 2059
810end
811note append helpers for building into a local array: position returned
812fn ot_tl_p8 b edit uchar 2048 p ulong x ulong to ulong
813put b p uchar and x 255
814ret add p 1
815end
816fn ot_tl_p16 b edit uchar 2048 p ulong x ulong to ulong
817put b p uchar and shr x 8 255
818put b add p 1 uchar and x 255
819ret add p 2
820end
821fn ot_tl_ext b edit uchar 2048 p ulong t ulong n ulong to ulong
822q = ot_tl_p16 b p t
823ret ot_tl_p16 b q n
824end
825note build the ClientHello body into ch; returns its length. pub = our X25519 public key, rnd = 64 random bytes
826note (32 for client random, 32 for legacy_session_id: middlebox compatibility mode, RFC 8446 D.4)
827fn ot_tl_chbody ch edit uchar 2048 pub view uchar 32 rnd view uchar 64 to ulong
828p = ot_tl_p16 ch 0 771
829for i ulong 64
830put ch add 2 i get rnd i
831end
832put ch 34 32
833for i ulong 32
834put ch add 35 i get rnd add 32 i
835end
836p = 67
837p = ot_tl_p16 ch p 4
838p = ot_tl_p16 ch p 4867
839p = ot_tl_p16 ch p 4865
840p = ot_tl_p8 ch p 1
841p = ot_tl_p8 ch p 0
842el = p
843p = add p 2
844hn = get ot_tl_v OT_TL_HOSTN
845if eq get ot_tl_v OT_TL_SNI 1
846p = ot_tl_ext ch p 0 add hn 5
847p = ot_tl_p16 ch p add hn 3
848p = ot_tl_p8 ch p 0
849p = ot_tl_p16 ch p hn
850for i ulong hn
851put ch add p i get ot_tl_host i
852end
853p = add p hn
854end
855p = ot_tl_ext ch p 10 4
856p = ot_tl_p16 ch p 2
857p = ot_tl_p16 ch p 29
858sc = new uchar 32 0
859sn = ot_tl_schemes sc
860p = ot_tl_ext ch p 13 add sn 2
861p = ot_tl_p16 ch p sn
862for i ulong sn
863put ch add p i get sc i
864end
865p = add p sn
866p = ot_tl_ext ch p 43 3
867p = ot_tl_p8 ch p 2
868p = ot_tl_p16 ch p 772
869p = ot_tl_ext ch p 51 38
870p = ot_tl_p16 ch p 36
871p = ot_tl_p16 ch p 29
872p = ot_tl_p16 ch p 32
873for i ulong 32
874put ch add p i get pub i
875end
876p = add p 32
877an = get ot_tl_v OT_TL_ALPNN
878if gt an 0
879p = ot_tl_ext ch p 16 add an 3
880p = ot_tl_p16 ch p add an 1
881p = ot_tl_p8 ch p an
882for i ulong an
883put ch add p i get ot_tl_alpn i
884end
885p = add p an
886end
887q = ot_tl_p16 ch el sub sub p el 2
888ret p
889end
890fn ot_tl_c16 ch view uchar 2048 p ulong to ulong
891ret or shl ulong get ch p 8 ulong get ch add p 1
892end
893note record what a ClientHello body offers (suites, extension types, session id), for checking the server's answers
894fn ot_tl_chscan ch view uchar 2048 n ulong to bool
895for i ulong 65536
896put ot_tl_offx i 0
897end
898if lt n 38
899ret false
900end
901sl = ulong get ch 34
902if or gt sl 32 gt add 35 sl n
903ret false
904end
905for i ulong sl
906put ot_tl_sid i get ch add 35 i
907end
908put ot_tl_v OT_TL_SIDN sl
909p = add 35 sl
910if gt add p 2 n
911ret false
912end
913cl = ot_tl_c16 ch p
914p = add p 2
915if or gt add p cl n gt div cl 2 64
916ret false
917end
918for i ulong div cl 2
919put ot_tl_offs i ot_tl_c16 ch add p mul i 2
920end
921put ot_tl_v OT_TL_NOFF div cl 2
922p = add p cl
923if gt add p 1 n
924ret false
925end
926p = add add p 1 ulong get ch p
927if gt add p 2 n
928ret false
929end
930e = add add p 2 ot_tl_c16 ch p
931p = add p 2
932if gt e n
933ret false
934end
935while lt p e bound 65536
936if gt add p 4 e
937ret false
938end
939t = ot_tl_c16 ch p
940l = ot_tl_c16 ch add p 2
941put ot_tl_offx t 1
942p = add add p 4 l
943end
944ret eq p e
945end
946fn ot_tl_offered suite ulong to bool
947for i ulong get ot_tl_v OT_TL_NOFF
948if eq get ot_tl_offs i suite
949ret true
950end
951end
952ret false
953end
954fn ot_tl_hello tr edit shactx sk edit uchar 640 to long
955ch = new uchar 2048 0
956rnd = new uchar 64 0
957pub = new uchar 32 0
958priv = new uchar 32 0
959if not ot_tl_hk_random rnd
960ret 19
961end
962if not ot_tl_hk_random priv
963ret 19
964end
965n = ot_tl_hk_ch ch priv
966if eq n 0
967ot_x25_base pub priv
968n = ot_tl_chbody ch pub rnd
969end
970for i ulong 32
971put sk add OT_TL_K_PRIV i get priv i
972put priv i 0
973end
974for i ulong 64
975put rnd i 0
976end
977if not ot_tl_chscan ch n
978ret 20
979end
980put ot_tl_v OT_TL_HTYP 1
981put ot_tl_v OT_TL_HLEN n
982for i ulong n
983put ot_tl_hm i get ch i
984end
985ot_tl_tradd tr
986put ot_tl_v OT_TL_FIRST 1
987if not ot_tl_wrhs 1 part ch 0 n
988ret 23
989end
990ret 0
991end
992
993note ======== ServerHello (RFC 8446 4.1.3) ========
994note the HelloRetryRequest random: SHA-256("HelloRetryRequest") = cf21ad74e59a6111 be1d8c021e65b891 c2a211167abb8c5e
995note 079e09e2c8a8339c (RFC 8446 4.1.3), as four big-endian words.
996note wolfram (WolframLanguageEvaluator, 2026-11-11): Hash["HelloRetryRequest", "SHA256"] in 64-bit words =
997note {14925401357662445841, 13699259582258722961, 14024790977831210078, 548887074219504540}
998fn ot_tl_ishrr to bool
999h = new ulong 4 0
1000put h 0 14925401357662445841
1001put h 1 13699259582258722961
1002put h 2 14024790977831210078
1003put h 3 548887074219504540
1004for i ulong 4
1005w = ulong 0
1006for j ulong 8
1007w = or shl w 8 ulong get ot_tl_hm add add 2 mul i 8 j
1008end
1009if ne w get h i
1010ret false
1011end
1012end
1013ret true
1014end
1015note the TLS 1.2/1.1 downgrade sentinels in the last 8 bytes of ServerHello.random (RFC 8446 4.1.3)
1016fn ot_tl_isdowngrade to bool
1017w = ulong 0
1018for j ulong 8
1019w = or shl w 8 ulong get ot_tl_hm add 26 j
1020end
1021note wolfram: FromDigits["444F574E47524401", 16] = 4922248911454880769 ("DOWNGRD" 01), ...00 = 4922248911454880768
1022ret or eq w 4922248911454880769 eq w 4922248911454880768
1023end
1024fn ot_tl_shello tr edit shactx sk edit uchar 640 to long
1025r = ot_tl_rdhs
1026if lt r 0
1027ret long ot_tl_code r
1028end
1029if ne r 2
1030ret 5
1031end
1032n = get ot_tl_v OT_TL_HLEN
1033if lt n 38
1034ret 6
1035end
1036sv = ot_tl_shexts n
1037if ne sv 0
1038ret long sv
1039end
1040ret ot_tl_shkeys tr sk
1041end
1042note check the ServerHello body; 0 or an error code. Leaves the cipher suite and the server key share position.
1043fn ot_tl_shexts n ulong to ulong
1044sl = ot_tl_h8 34
1045if or gt sl 32 gt add 35 sl n
1046ret 6
1047end
1048p = add 35 sl
1049if gt add p 5 n
1050ret 6
1051end
1052if ne add add p 5 ot_tl_h16 add p 3 n
1053ret 6
1054end
1055c = ot_tl_shloop add p 5 n
1056if ne c 0
1057ret c
1058end
1059ret ot_tl_shcheck sl ot_tl_h16 p ot_tl_h8 add p 2
1060end
1061note the ServerHello extensions: SVS = 1 + selected version (0: absent), KSP/KSL = key share data position and
1062note 1 + length (0: absent), SHX = the error for the first other extension (9 offered but not allowed here, 11 not offered)
1063fn ot_tl_shloop p ulong e ulong to ulong
1064put ot_tl_v OT_TL_SVS 0
1065put ot_tl_v OT_TL_KSP 0
1066put ot_tl_v OT_TL_KSL 0
1067put ot_tl_v OT_TL_SHX 0
1068q = p
1069while lt q e bound 65536
1070if gt add q 4 e
1071ret 6
1072end
1073t = ot_tl_h16 q
1074l = ot_tl_h16 add q 2
1075d = add q 4
1076if gt add d l e
1077ret 6
1078end
1079if eq t 43
1080if gt get ot_tl_v OT_TL_SVS 0
1081ret 9
1082end
1083if ne l 2
1084ret 6
1085end
1086put ot_tl_v OT_TL_SVS add 1 ot_tl_h16 d
1087else
1088if eq t 51
1089if gt get ot_tl_v OT_TL_KSL 0
1090ret 9
1091end
1092put ot_tl_v OT_TL_KSP d
1093put ot_tl_v OT_TL_KSL add l 1
1094else
1095if eq get ot_tl_v OT_TL_SHX 0
1096if eq get ot_tl_offx t 1
1097put ot_tl_v OT_TL_SHX 9
1098else
1099put ot_tl_v OT_TL_SHX 11
1100end
1101end
1102end
1103end
1104q = add d l
1105end
1106ret 0
1107end
1108fn ot_tl_shcheck sl ulong suite ulong comp ulong to ulong
1109note RFC 8446 4.1.3, D.3: without supported_versions = TLS 1.3 this is an older ServerHello: refuse, never downgrade
1110sv = get ot_tl_v OT_TL_SVS
1111if or ne sv 773 ot_tl_isdowngrade
1112ret 8
1113end
1114if ne ot_tl_h16 0 771
1115ret 9
1116end
1117if ot_tl_ishrr
1118ret 10
1119end
1120x = get ot_tl_v OT_TL_SHX
1121if gt x 0
1122ret x
1123end
1124if ne sl get ot_tl_v OT_TL_SIDN
1125ret 9
1126end
1127for i ulong sl
1128if ne get ot_tl_hm add 35 i get ot_tl_sid i
1129ret 9
1130end
1131end
1132if not ot_tl_offered suite
1133ret 9
1134end
1135if and ne suite 4865 ne suite 4867
1136ret 10
1137end
1138if ne comp 0
1139ret 9
1140end
1141if eq get ot_tl_v OT_TL_KSL 0
1142ret 10
1143end
1144ks = get ot_tl_v OT_TL_KSP
1145if or ne get ot_tl_v OT_TL_KSL 37 ne ot_tl_h16 ks 29
1146ret 9
1147end
1148if ne ot_tl_h16 add ks 2 32
1149ret 9
1150end
1151put ot_tl_v OT_TL_SUITE suite
1152if eq suite 4865
1153put ot_tl_v OT_TL_AEAD 1
1154put ot_tl_v OT_TL_KLEN 16
1155else
1156put ot_tl_v OT_TL_AEAD 3
1157put ot_tl_v OT_TL_KLEN 32
1158end
1159put ot_tl_v OT_TL_X5 add ks 4
1160ret 0
1161end
1162note X25519 shared secret and the handshake traffic secrets; switches both directions to handshake keys
1163fn ot_tl_shkeys tr edit shactx sk edit uchar 640 to long
1164ot_tl_tradd tr
1165dh = new uchar 64 0
1166if not ot_tl_dh sk dh
1167ret 9
1168end
1169if not ot_tl_qempty
1170ot_tl_zero64 dh
1171ret 5
1172end
1173c = ot_tl_hsec tr sk dh
1174ot_tl_zero64 dh
1175ret c
1176end
1177note the shared secret into dh[0..32); false when it is all zero (a low-order server point, RFC 7748 6.1)
1178fn ot_tl_dh sk edit uchar 640 dh edit uchar 64 to bool
1179peer = new uchar 32 0
1180priv = new uchar 32 0
1181d = new uchar 32 0
1182o = get ot_tl_v OT_TL_X5
1183for i ulong 32
1184put peer i get ot_tl_hm add o i
1185put priv i get sk add OT_TL_K_PRIV i
1186put sk add OT_TL_K_PRIV i 0
1187end
1188ot_x25_mul d priv peer
1189for i ulong 32
1190put priv i 0
1191put dh i get d i
1192end
1193bad = ot_ct_zero32 d
1194for i ulong 32
1195put d i 0
1196end
1197ret not bad
1198end
1199note Early Secret = HKDF-Extract(0, 0) (no PSK)
1200fn ot_tl_early s edit uchar 64 to void
1201z = new uchar 32 0
1202salt = new uchar 32 0
1203n = ot_hk_extract 0 salt z s
1204end
1205fn ot_tl_hsec tr edit shactx sk edit uchar 640 dh view uchar 64 to long
1206s = new uchar 64 0
1207x = new uchar 64 0
1208h = new uchar 64 0
1209ot_tl_emptyhash h
1210ot_tl_early s
1211ok = ot_tl_derive s ot_tl_t_der h x
1212n = ot_hk_extract 0 part x 0 32 part dh 0 32 s
1213ot_tl_skput sk OT_TL_K_HS s
1214ot_tl_trhash tr h
1215ok = and ok ot_tl_derive s ot_tl_t_chs h x
1216ot_tl_skput sk OT_TL_K_CHS x
1217ot_tl_klog 1 x
1218ok = and ok ot_tl_derive s ot_tl_t_shs h x
1219ot_tl_skput sk OT_TL_K_SHS x
1220ot_tl_klog 2 x
1221ok = and ok ot_tl_keys 0 x
1222ot_tl_sk sk OT_TL_K_CHS x
1223ok = and ok ot_tl_keys 1 x
1224put ot_tl_v OT_TL_WENC 0
1225ot_tl_zero64 x
1226ot_tl_zero64 s
1227put ot_tl_v OT_TL_CCS 1
1228if not ok
1229ret 20
1230end
1231ret 0
1232end
1233
1234note ======== EncryptedExtensions (RFC 8446 4.3.1) ========
1235fn ot_tl_ee tr edit shactx to long
1236r = ot_tl_rdhs
1237if lt r 0
1238ret long ot_tl_code r
1239end
1240if ne r 8
1241ret 5
1242end
1243n = get ot_tl_v OT_TL_HLEN
1244if lt n 2
1245ret 6
1246end
1247if ne add 2 ot_tl_h16 0 n
1248ret 6
1249end
1250p = ulong 2
1251seen = new uchar 65536 0
1252while lt p n bound 65536
1253if gt add p 4 n
1254ret 6
1255end
1256t = ot_tl_h16 p
1257l = ot_tl_h16 add p 2
1258q = add p 4
1259if gt add q l n
1260ret 6
1261end
1262if eq get seen t 1
1263ret 9
1264end
1265put seen t 1
1266if ne get ot_tl_offx t 1
1267ret 11
1268end
1269if or or eq t 43 eq t 51 or eq t 13 eq t 41
1270ret 9
1271end
1272if eq t 16
1273c = ot_tl_alpnok q l
1274if ne c 0
1275ret long c
1276end
1277end
1278if and eq t 0 ne l 0
1279ret 6
1280end
1281if eq t 28
1282if ne l 2
1283ret 6
1284end
1285v = ot_tl_h16 q
1286if lt v 64
1287ret 9
1288end
1289if lt v 16385
1290put ot_tl_v OT_TL_RSL sub v 1
1291end
1292end
1293p = add q l
1294end
1295ot_tl_tradd tr
1296ret 0
1297end
1298note the server's ALPN answer must be exactly one protocol and the one we offered (RFC 7301 3.1)
1299fn ot_tl_alpnok q ulong l ulong to ulong
1300an = get ot_tl_v OT_TL_ALPNN
1301if lt l 3
1302ret 6
1303end
1304if ne add 2 ot_tl_h16 q l
1305ret 6
1306end
1307if ne add 3 ot_tl_h8 add q 2 l
1308ret 6
1309end
1310if ne ot_tl_h8 add q 2 an
1311ret 9
1312end
1313for i ulong an
1314if ne get ot_tl_hm add add q 3 i get ot_tl_alpn i
1315ret 9
1316end
1317end
1318ret 0
1319end
1320
1321note ======== CertificateRequest (optional), Certificate (RFC 8446 4.3.2, 4.4.2) ========
1322fn ot_tl_certs tr edit shactx to long
1323r = ot_tl_rdhs
1324if lt r 0
1325ret long ot_tl_code r
1326end
1327if eq r 13
1328c = ot_tl_creq
1329if ne c 0
1330ret long c
1331end
1332ot_tl_tradd tr
1333r = ot_tl_rdhs
1334if lt r 0
1335ret long ot_tl_code r
1336end
1337end
1338if ne r 11
1339ret 5
1340end
1341c = ot_tl_certmsg
1342if ne c 0
1343ret long c
1344end
1345ot_tl_tradd tr
1346ret 0
1347end
1348fn ot_tl_creq to ulong
1349n = get ot_tl_v OT_TL_HLEN
1350if lt n 3
1351ret 6
1352end
1353cl = ot_tl_h8 0
1354note RFC 8446 4.3.2: the context is empty in a handshake CertificateRequest
1355if ne cl 0
1356ret 9
1357end
1358if gt add add 1 cl 2 n
1359ret 6
1360end
1361if ne add add add 1 cl 2 ot_tl_h16 add 1 cl n
1362ret 6
1363end
1364for i ulong cl
1365put ot_tl_cr i get ot_tl_hm add 1 i
1366end
1367put ot_tl_v OT_TL_CREQ add cl 1
1368ret 0
1369end
1370fn ot_tl_certmsg to ulong
1371n = get ot_tl_v OT_TL_HLEN
1372if lt n 4
1373ret 6
1374end
1375if ne ot_tl_h8 0 0
1376ret 9
1377end
1378ll = ot_tl_h24 1
1379if ne add ll 4 n
1380ret 6
1381end
1382if eq ll 0
1383ret 6
1384end
1385ot_x5_reset
1386p = ulong 4
1387k = ulong 0
1388while lt p n bound 16777216
1389if gt add p 3 n
1390ret 6
1391end
1392cl = ot_tl_h24 p
1393if eq cl 0
1394ret 6
1395end
1396if gt add add p 3 cl n
1397ret 6
1398end
1399c = ot_tl_addcert add p 3 cl k
1400if ne c 0
1401ret c
1402end
1403p = add add p 3 cl
1404if gt add p 2 n
1405ret 6
1406end
1407el = ot_tl_h16 p
1408if gt add add p 2 el n
1409ret 6
1410end
1411if ne el 0
1412ret 11
1413end
1414p = add add p 2 el
1415k = add k 1
1416end
1417c = ot_tl_hk_cert
1418if eq c 255
1419c = ot_tl_x5check
1420end
1421if ne c 0
1422ret c
1423end
1424ret ot_tl_leafkey
1425end
1426note one certificate entry to the X.509 code (copied: globals cannot be passed); the first one also to the test hook
1427fn ot_tl_addcert off ulong n ulong k ulong to ulong
1428if gt n 1048576
1429ret 24
1430end
1431b = new uchar 1048576 0
1432for i ulong n bound 1048576
1433put b i get ot_tl_hm add off i
1434end
1435if eq k 0
1436ot_tl_hk_leaf part b 0 n
1437end
1438r = ot_x5_add part b 0 n
1439if lt r 0
1440ret 15
1441end
1442ret 0
1443end
1444note the X.509 result codes (src/x509.om) as TLS error codes
1445fn ot_tl_x5code c int to ulong
1446if eq c 0
1447ret 0
1448end
1449if eq c 2
1450ret 12
1451end
1452if eq c 3
1453ret 13
1454end
1455if eq c 4
1456ret 14
1457end
1458if or eq c 7 eq c 8
1459ret 24
1460end
1461if eq c 9
1462ret 6
1463end
1464ret 15
1465end
1466note ot_x5_check with the host name copied out of the global (globals cannot be passed) and the current time
1467fn ot_tl_x5check to ulong
1468hv = new uchar 256 0
1469hn = get ot_tl_v OT_TL_HOSTN
1470for i ulong hn
1471put hv i get ot_tl_host i
1472end
1473ret ot_tl_x5code ot_x5_check part hv 0 hn ot_tl_hk_now
1474end
1475fn ot_tl_leafkey to ulong
1476s = new uchar 1048576 0
1477n = ot_x5_leafspki s
1478if le n 0
1479ret 15
1480end
1481if not ot_pk_tlskey part s 0 ulong n
1482ret 24
1483end
1484ret 0
1485end
1486
1487note ======== CertificateVerify (RFC 8446 4.4.3) ========
1488fn ot_tl_cv tr edit shactx to long
1489th = new uchar 64 0
1490ot_tl_trhash tr th
1491r = ot_tl_rdhs
1492if lt r 0
1493ret long ot_tl_code r
1494end
1495if ne r 15
1496ret 5
1497end
1498n = get ot_tl_v OT_TL_HLEN
1499if lt n 4
1500ret 6
1501end
1502sch = ot_tl_h16 0
1503sl = ot_tl_h16 2
1504if ne add 4 sl n
1505ret 6
1506end
1507if not ot_tl_cvscheme sch
1508ret 9
1509end
1510m = new uchar 130 0
1511for i ulong 64
1512put m i 32
1513end
1514for i ulong len ot_tl_t_cv
1515put m add 64 i get ot_tl_t_cv i
1516end
1517put m 97 0
1518for i ulong 32
1519put m add 98 i get th i
1520end
1521sig = new uchar 65536 0
1522for i ulong sl
1523put sig i get ot_tl_hm add 4 i
1524end
1525if not ot_pk_tls sch m part sig 0 sl
1526ret 16
1527end
1528put ot_tl_v OT_TL_SCHEME sch
1529ot_tl_tradd tr
1530ret 0
1531end
1532
1533note ======== Finished (RFC 8446 4.4.4) ========
1534note verify_data = HMAC(finished_key, transcript hash), finished_key = HKDF-Expand-Label(base, "finished", "", 32)
1535fn ot_tl_vdata base view uchar 64 th view uchar 64 o edit uchar 64 to bool
1536fk = new uchar 32 0
1537ok = ot_tl_expl base ot_tl_t_fin fk
1538n = ot_hm_mac 0 fk part th 0 32 o
1539for i ulong 32
1540put fk i 0
1541end
1542ret ok
1543end
1544fn ot_tl_sfin tr edit shactx sk edit uchar 640 to long
1545th = new uchar 64 0
1546ot_tl_trhash tr th
1547r = ot_tl_rdhs
1548if lt r 0
1549ret long ot_tl_code r
1550end
1551if ne r 20
1552ret 5
1553end
1554if ne get ot_tl_v OT_TL_HLEN 32
1555ret 6
1556end
1557b = new uchar 64 0
1558want = new uchar 64 0
1559got = new uchar 32 0
1560ot_tl_sk sk OT_TL_K_SHS b
1561ok = ot_tl_vdata b th want
1562w32 = new uchar 32 0
1563for i ulong 32
1564put got i get ot_tl_hm i
1565put w32 i get want i
1566end
1567same = ot_ct_eq32 got w32
1568ot_tl_zero64 b
1569ot_tl_zero64 want
1570if not ok
1571ret 20
1572end
1573if not same
1574ret 17
1575end
1576ot_tl_tradd tr
1577if not ot_tl_qempty
1578ret 5
1579end
1580ret ot_tl_appkeys tr sk
1581end
1582note master secret and the application traffic secrets; switches reading to the server's application keys
1583fn ot_tl_appkeys tr edit shactx sk edit uchar 640 to long
1584th = new uchar 64 0
1585ot_tl_trhash tr th
1586for i ulong 64
1587put sk add OT_TL_K_TH i get th i
1588if lt i 32
1589put ot_tl_thg i get th i
1590end
1591end
1592eh = new uchar 64 0
1593s = new uchar 64 0
1594x = new uchar 64 0
1595z = new uchar 32 0
1596ot_tl_emptyhash eh
1597ot_tl_sk sk OT_TL_K_HS s
1598ok = ot_tl_derive s ot_tl_t_der eh x
1599n = ot_hk_extract 0 part x 0 32 z s
1600ot_tl_skput sk OT_TL_K_MS s
1601ok = and ok ot_tl_derive s ot_tl_t_cap th x
1602ot_tl_skput sk OT_TL_K_CAP x
1603ot_tl_klog 3 x
1604ok = and ok ot_tl_derive s ot_tl_t_sap th x
1605ot_tl_skput sk OT_TL_K_SAP x
1606ot_tl_klog 4 x
1607ok = and ok ot_tl_keys 0 x
1608put ot_tl_v OT_TL_CCS 0
1609ot_tl_zero64 x
1610ot_tl_zero64 s
1611if not ok
1612ret 20
1613end
1614ret 0
1615end
1616note the compatibility-mode change_cipher_spec record (RFC 8446 D.4): plaintext, before the first protected record
1617fn ot_tl_ccs to bool
1618c = new uchar 1 1
1619put ot_tl_v OT_TL_WENC 0
1620ret ot_tl_wrec 20 c
1621end
1622note the empty client Certificate answering a CertificateRequest (RFC 8446 4.4.2.4 allows it; no client auth here)
1623fn ot_tl_emptycert tr edit shactx to bool
1624cm = new uchar 260 0
1625cl = sub get ot_tl_v OT_TL_CREQ 1
1626put cm 0 uchar cl
1627for i ulong cl
1628put cm add 1 i get ot_tl_cr i
1629end
1630put ot_tl_v OT_TL_HTYP 11
1631put ot_tl_v OT_TL_HLEN add cl 4
1632for i ulong add cl 4
1633put ot_tl_hm i get cm i
1634end
1635ot_tl_tradd tr
1636ret ot_tl_wrhs 11 part cm 0 add cl 4
1637end
1638note client flight: change_cipher_spec (compatibility mode only), an empty Certificate if asked, Finished
1639fn ot_tl_cfin tr edit shactx sk edit uchar 640 to long
1640if gt get ot_tl_v OT_TL_SIDN 0
1641if not ot_tl_ccs
1642ret 23
1643end
1644end
1645x = new uchar 64 0
1646ot_tl_sk sk OT_TL_K_CHS x
1647ok = ot_tl_keys 1 x
1648if gt get ot_tl_v OT_TL_CREQ 0
1649if not ot_tl_emptycert tr
1650ret 23
1651end
1652end
1653th = new uchar 64 0
1654ot_tl_trhash tr th
1655vd = new uchar 64 0
1656ok = and ok ot_tl_vdata x th vd
1657if not ok
1658ot_tl_zero64 x
1659ot_tl_zero64 vd
1660ret 20
1661end
1662if not ot_tl_wrhs 20 part vd 0 32
1663ret 23
1664end
1665ot_tl_sk sk OT_TL_K_CAP x
1666ok2 = ot_tl_keys 1 x
1667ot_tl_zero64 x
1668ot_tl_zero64 vd
1669if not ok2
1670ret 20
1671end
1672ret 0
1673end
1674
1675note ======== the handshake ========
1676fn ot_tl_hand to long
1677sk = new uchar 640 0
1678tr = new uchar 104 0
1679sha256_init tr
1680put ot_tl_v OT_TL_ST OT_TL_HS
1681put ot_tl_v OT_TL_QS 0
1682put ot_tl_v OT_TL_QE 0
1683put ot_tl_v OT_TL_RENC 0
1684put ot_tl_v OT_TL_WENC 0
1685put ot_tl_v OT_TL_RSL 16384
1686put ot_tl_v OT_TL_CREQ 0
1687put ot_tl_v OT_TL_CCS 0
1688ot_tl_opstart
1689c = ot_tl_hello tr sk
1690if eq c 0
1691c = ot_tl_shello tr sk
1692end
1693if eq c 0
1694c = ot_tl_ee tr
1695end
1696if eq c 0
1697c = ot_tl_certs tr
1698end
1699if eq c 0
1700c = ot_tl_cv tr
1701end
1702if eq c 0
1703c = ot_tl_sfin tr sk
1704end
1705if eq c 0
1706c = ot_tl_cfin tr sk
1707end
1708for i ulong 640
1709put sk i 0
1710end
1711for i ulong 104
1712put tr i 0
1713end
1714if ne c 0
1715r = ot_tl_fail ulong c
1716ret c
1717end
1718put ot_tl_v OT_TL_ST OT_TL_OPEN
1719ret 0
1720end
1721
1722note ======== API ========
1723note set the per-operation deadline (default 30 s)
1724fn ot_tl_deadline ns long to void
1725if gt ns 0
1726put ot_tl_v OT_TL_DL ulong ns
1727put ot_tl_v OT_TL_DLSET 1
1728end
1729end
1730fn ot_tl_init to void
1731if eq get ot_tl_v OT_TL_DLSET 0
1732put ot_tl_v OT_TL_DL 30000000000
1733end
1734end
1735note the trust store (a PEM bundle such as /etc/ssl/certs/ca-certificates.crt): certificates loaded or a negative error
1736fn ot_tl_roots path view uchar * to long
1737ret ot_x5_roots path
1738end
1739note connect-address override: an IPv4 literal used for the TCP connection while the certificate is still checked
1740note against the host name; an empty view clears it
1741fn ot_tl_via a view uchar * to bool
1742x = new uchar 4 0
1743if eq len a 0
1744put ot_tl_v OT_TL_VIAN 0
1745ret true
1746end
1747if or gt len a 64 not ot_dn_ipv4 a x
1748ret false
1749end
1750for i ulong len a
1751put ot_tl_viab i get a i
1752end
1753put ot_tl_v OT_TL_VIAN len a
1754ret true
1755end
1756note resolve and connect: fd, or a negative code (see net.om; -110 timeout)
1757fn ot_tl_tcp host view uchar * port ulong to long
1758ot_tl_init
1759ot_tl_opstart
1760a = new uchar 4 0
1761v = new uchar 64 0
1762vn = get ot_tl_v OT_TL_VIAN
1763if gt vn 0
1764for i ulong vn
1765put v i get ot_tl_viab i
1766end
1767if not ot_dn_ipv4 part v 0 vn a
1768ret -1
1769end
1770else
1771r = ot_dn_resolve host a
1772if lt r 0
1773ret r
1774end
1775end
1776if or eq port 0 gt port 65535
1777ret -22
1778end
1779ret ot_io_connect a port ot_tl_until
1780end
1781fn ot_tl_begin fd int host view uchar * alpn view uchar * to long
1782ot_tl_init
1783for i ulong 64
1784if ne i OT_TL_DL
1785if ne i OT_TL_DLSET
1786if ne i OT_TL_VIAN
1787put ot_tl_v i 0
1788end
1789end
1790end
1791end
1792n = len host
1793if gt n 0
1794if eq get host sub n 1 46
1795n = sub n 1
1796end
1797end
1798if or eq n 0 gt n 253
1799z = fclose fd
1800ret 6
1801end
1802for i ulong n
1803c = get host i
1804if or le c 32 ge c 127
1805z = fclose fd
1806ret 6
1807end
1808put ot_tl_host i c
1809end
1810put ot_tl_v OT_TL_HOSTN n
1811ip = new uchar 4 0
1812put ot_tl_v OT_TL_SNI 1
1813note no SNI for an IP literal (RFC 6066 3): dotted IPv4, or any host containing ':' (IPv6)
1814if ot_dn_ipv4 part host 0 n ip
1815put ot_tl_v OT_TL_SNI 0
1816end
1817for i ulong n
1818if eq get host i 58
1819put ot_tl_v OT_TL_SNI 0
1820end
1821end
1822if or gt len alpn 255 eq len alpn 1
1823z = fclose fd
1824ret 6
1825end
1826for i ulong len alpn
1827put ot_tl_alpn i get alpn i
1828end
1829put ot_tl_v OT_TL_ALPNN len alpn
1830put ot_tl_v OT_TL_FD ulong fd
1831note PSTATE.DIT (Arm FEAT_DIT): data-independent timing for the instructions that support it, for the rest of the
1832note process (the GB10 lists dit in /proc/cpuinfo; a core without FEAT_DIT would stop with SIGILL here)
1833od = dit 1
1834c = ot_tl_hand
1835k = scrub 1048576
1836ret c
1837end
1838note resolve, TCP, TLS 1.3 handshake: 0 when the connection is open, else an error code (ot_tl_why)
1839fn ot_tl_connect host view uchar * port ulong alpn view uchar * to int
1840fd = ot_tl_tcp host port
1841if lt fd 0
1842put ot_tl_v OT_TL_ERR 1
1843put ot_tl_v OT_TL_SYS ulong neg fd
1844put ot_tl_v OT_TL_ST OT_TL_FAILED
1845ret 1
1846end
1847ret int ot_tl_begin int fd host alpn
1848end
1849note TLS on an already connected descriptor (STARTTLS); the descriptor is closed on failure
1850fn ot_tl_start fd int host view uchar * alpn view uchar * to int
1851ret int ot_tl_begin fd host alpn
1852end
1853note send all of d as application data; false on any failure (then ot_tl_err says why)
1854fn ot_tl_write d view uchar * to bool
1855if ne get ot_tl_v OT_TL_ST OT_TL_OPEN
1856ret false
1857end
1858ot_tl_opstart
1859n = len d
1860p = ulong 0
1861lim = get ot_tl_v OT_TL_RSL
1862while lt p n bound 9223372036854775807
1863m = sub n p
1864if gt m lim
1865m = lim
1866end
1867if ge get ot_tl_v OT_TL_WSEQ 16777216
1868if not ot_tl_keyupd 0
1869r = ot_tl_fail 23
1870ret false
1871end
1872end
1873if not ot_tl_wrec 23 part d p m
1874r = ot_tl_fail 23
1875ret false
1876end
1877p = add p m
1878end
1879k = scrub 131072
1880ret true
1881end
1882note read application data into b: bytes read (> 0), 0 after the peer's close_notify, or -code on failure
1883note (a stream that ends without close_notify is -3: truncation is an error, never end of data)
1884fn ot_tl_read b edit uchar * to long
1885st = get ot_tl_v OT_TL_ST
1886if eq st OT_TL_PEERCLOSED
1887ret 0
1888end
1889if ne st OT_TL_OPEN
1890ret -22
1891end
1892if eq len b 0
1893ret 0
1894end
1895ot_tl_opstart
1896r = ot_tl_read1 b
1897k = scrub 131072
1898ret r
1899end
1900fn ot_tl_read1 b edit uchar * to long
1901while true bound 9223372036854775807
1902ps = get ot_tl_v OT_TL_PS
1903pe = get ot_tl_v OT_TL_PE
1904if lt ps pe
1905m = sub pe ps
1906if gt m len b
1907m = len b
1908end
1909for i ulong m
1910put b i get ot_tl_pend add ps i
1911put ot_tl_pend add ps i 0
1912end
1913put ot_tl_v OT_TL_PS add ps m
1914ret long m
1915end
1916c = ot_tl_pump
1917if eq c OT_TL_EOF
1918if eq get ot_tl_v OT_TL_ST OT_TL_PEERCLOSED
1919ret 0
1920end
1921r = ot_tl_fail 3
1922ret -3
1923end
1924if lt c 0
1925r = ot_tl_fail ot_tl_code c
1926ret c
1927end
1928end
1929ret -20
1930end
1931note process one record after the handshake: application data goes to ot_tl_pend; handshake messages are
1932note NewSessionTicket (ignored: no resumption) and KeyUpdate; anything else is unexpected (renegotiation does not
1933note exist in TLS 1.3: a ClientHello, ServerHello or HelloRequest here is refused). 0 or a negative result.
1934fn ot_tl_pump to long
1935if ot_tl_qempty
1936r = ot_tl_pump1
1937if le r 0
1938ret r
1939end
1940end
1941h = ot_tl_rdhs
1942if lt h 0
1943ret h
1944end
1945if eq h 4
1946ret 0
1947end
1948if eq h 24
1949ret ot_tl_gotku
1950end
1951ret -21
1952end
1953note one record after the handshake: 0 application data now in ot_tl_pend, 1 handshake bytes now in ot_tl_hq, or -code
1954fn ot_tl_pump1 to long
1955rb = new uchar 16645 0
1956t = ot_tl_rec rb
1957if lt t 0
1958ret t
1959end
1960m = get ot_tl_v OT_TL_RLEN
1961if eq t 23
1962for i ulong m
1963put ot_tl_pend i get rb add 5 i
1964put rb add 5 i 0
1965end
1966put ot_tl_v OT_TL_PS 0
1967put ot_tl_v OT_TL_PE m
1968ret 0
1969end
1970if or ne t 22 eq m 0
1971ret -5
1972end
1973for i ulong m
1974put ot_tl_hq i get rb add 5 i
1975end
1976put ot_tl_v OT_TL_QS 0
1977put ot_tl_v OT_TL_QE m
1978ret 1
1979end
1980note KeyUpdate (RFC 8446 4.6.3): update the read key; when asked, answer with our own KeyUpdate
1981fn ot_tl_gotku to long
1982if ne get ot_tl_v OT_TL_HLEN 1
1983ret -6
1984end
1985req = ot_tl_h8 0
1986if gt req 1
1987ret -9
1988end
1989if not ot_tl_qempty
1990ret -5
1991end
1992if not ot_tl_rotate 0
1993ret -20
1994end
1995if eq req 1
1996if not ot_tl_keyupd 0
1997ret -23
1998end
1999end
2000ret 0
2001end
2002note send KeyUpdate(request) and rotate our write key
2003fn ot_tl_keyupd req ulong to bool
2004b = new uchar 1 0
2005put b 0 uchar req
2006if not ot_tl_wrhs 24 b
2007ret false
2008end
2009ret ot_tl_rotate 1
2010end
2011note application_traffic_secret_N+1 = HKDF-Expand-Label(application_traffic_secret_N, "traffic upd", "", 32)
2012fn ot_tl_rotate dir ulong to bool
2013s = new uchar 64 0
2014for i ulong 64
2015if eq dir 0
2016put s i get ot_tl_rs i
2017else
2018put s i get ot_tl_ws i
2019end
2020end
2021x = new uchar 32 0
2022ok = ot_tl_expl s ot_tl_t_upd x
2023for i ulong 32
2024put s i get x i
2025put x i 0
2026end
2027ok2 = ot_tl_keys dir s
2028ot_tl_zero64 s
2029ret and ok ok2
2030end
2031note send close_notify, close the descriptor, overwrite the keys
2032fn ot_tl_close to void
2033ot_tl_opstart
2034st = get ot_tl_v OT_TL_ST
2035if or eq st OT_TL_OPEN eq st OT_TL_PEERCLOSED
2036ok = ot_tl_sendalert 0
2037end
2038ot_tl_shut
2039put ot_tl_v OT_TL_ST OT_TL_CLOSED
2040k = scrub 131072
2041end
2042fn ot_tl_err to int
2043ret int get ot_tl_v OT_TL_ERR
2044end
2045note the error code of the last failure as text (o gets at most 128 bytes); returns the length
2046text ot_tl_e0 "no error"
2047text ot_tl_e1 "cannot resolve or connect to the server"
2048text ot_tl_e2 "timed out"
2049text ot_tl_e3 "the connection ended early (truncated record or stream without close_notify)"
2050text ot_tl_e4 "record too large (record_overflow)"
2051text ot_tl_e5 "unexpected message"
2052text ot_tl_e6 "malformed handshake message (decode_error)"
2053text ot_tl_e7 "record authentication failed (bad_record_mac)"
2054text ot_tl_e8 "server does not speak TLS 1.3; refusing to downgrade (protocol_version)"
2055text ot_tl_e9 "illegal parameter from the server"
2056text ot_tl_e10 "no common parameters (handshake_failure; HelloRetryRequest is not supported)"
2057text ot_tl_e11 "server sent an extension the client did not offer"
2058text ot_tl_e12 "certificate does not match the host name"
2059text ot_tl_e13 "certificate expired or not yet valid"
2060text ot_tl_e14 "certificate not issued by a trusted root"
2061text ot_tl_e15 "certificate invalid (signature, encoding or constraints)"
2062text ot_tl_e16 "CertificateVerify signature invalid"
2063text ot_tl_e17 "server Finished verification failed"
2064text ot_tl_e18 "the server sent a fatal alert"
2065text ot_tl_e19 "no randomness from the system"
2066text ot_tl_e20 "internal error"
2067text ot_tl_e21 "post-handshake message refused (renegotiation does not exist in TLS 1.3)"
2068text ot_tl_e22 "not connected"
2069text ot_tl_e23 "write failed"
2070text ot_tl_e24 "certificate uses an unsupported algorithm, key or critical extension"
2071fn ot_tl_why o edit uchar 128 to ulong
2072c = get ot_tl_v OT_TL_ERR
2073if eq c 0
2074ret ot_tl_cpy o ot_tl_e0
2075end
2076if eq c 1
2077ret ot_tl_cpy o ot_tl_e1
2078end
2079if eq c 2
2080ret ot_tl_cpy o ot_tl_e2
2081end
2082if eq c 3
2083ret ot_tl_cpy o ot_tl_e3
2084end
2085if eq c 4
2086ret ot_tl_cpy o ot_tl_e4
2087end
2088if eq c 5
2089ret ot_tl_cpy o ot_tl_e5
2090end
2091if eq c 6
2092ret ot_tl_cpy o ot_tl_e6
2093end
2094if eq c 7
2095ret ot_tl_cpy o ot_tl_e7
2096end
2097if eq c 8
2098ret ot_tl_cpy o ot_tl_e8
2099end
2100if eq c 9
2101ret ot_tl_cpy o ot_tl_e9
2102end
2103if eq c 10
2104ret ot_tl_cpy o ot_tl_e10
2105end
2106if eq c 11
2107ret ot_tl_cpy o ot_tl_e11
2108end
2109ret ot_tl_why2 o c
2110end
2111fn ot_tl_why2 o edit uchar 128 c ulong to ulong
2112if eq c 12
2113ret ot_tl_cpy o ot_tl_e12
2114end
2115if eq c 13
2116ret ot_tl_cpy o ot_tl_e13
2117end
2118if eq c 14
2119ret ot_tl_cpy o ot_tl_e14
2120end
2121if eq c 15
2122ret ot_tl_cpy o ot_tl_e15
2123end
2124if eq c 16
2125ret ot_tl_cpy o ot_tl_e16
2126end
2127if eq c 17
2128ret ot_tl_cpy o ot_tl_e17
2129end
2130if eq c 18
2131ret ot_tl_cpy o ot_tl_e18
2132end
2133if eq c 19
2134ret ot_tl_cpy o ot_tl_e19
2135end
2136if eq c 21
2137ret ot_tl_cpy o ot_tl_e21
2138end
2139if eq c 22
2140ret ot_tl_cpy o ot_tl_e22
2141end
2142if eq c 23
2143ret ot_tl_cpy o ot_tl_e23
2144end
2145if eq c 24
2146ret ot_tl_cpy o ot_tl_e24
2147end
2148ret ot_tl_cpy o ot_tl_e20
2149end
2150fn ot_tl_cpy o edit uchar 128 t view uchar * to ulong
2151n = len t
2152if gt n 128
2153n = 128
2154end
2155for i ulong n
2156put o i get t i
2157end
2158ret n
2159end
2160note the alert description received from the server (valid when ot_tl_err is 18)
2161fn ot_tl_alertrecv to int
2162ret int get ot_tl_v OT_TL_ALERT
2163end
2164note the negotiated cipher suite and CertificateVerify scheme (for logs and tests)
2165fn ot_tl_suite to ulong
2166ret get ot_tl_v OT_TL_SUITE
2167end
2168fn ot_tl_scheme to ulong
2169ret get ot_tl_v OT_TL_SCHEME
2170end
2171note the handshake transcript hash (SHA-256 through the server Finished) of the current connection
2172fn ot_tl_thash o edit uchar 32 to void
2173for i ulong 32
2174put o i get ot_tl_thg i
2175end
2176end