interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / agora / tls / tls.om

2261 lines · 43852 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note tls.om: TLS 1.3 client (RFC 8446), DNS A lookup and TCP connect. Library unit: functions,
3note consts and texts only; no main, no mutable globals. Needs sha2.om, hkdf.om, x25519.om,
4note chacha.om, aesgcm.om, ec.om, rsa.om and x509.om composed before it (om/tls/build.sh).
5note
6note TLS 1.3 only: supported_versions offers 0x0304 alone and a ServerHello without it is refused
7note (no TLS 1.2 fallback, no insecure switch). Suites TLS_CHACHA20_POLY1305_SHA256 (preferred)
8note and TLS_AES_128_GCM_SHA256; group x25519 only; SNI always; ALPN http/1.1 when flag 1 is set.
9note Server authentication is mandatory: x509_chain_verify (chain, dates, name, constraints) then
10note x509_cv_verify (CertificateVerify) then the Finished HMAC. Any failure: a fatal alert is sent
11note (best effort) and a named error is returned; the connection is unusable afterwards.
12note
13note Connection state c is one caller-owned uchar array of TLCSZ bytes; the trust store st is a
14note separate array filled once by tls_store (x509_store_load). Integers in c are big-endian.
15
16const TLCSZ ulong 119296
17const TLSTSZ ulong 262144
18const TLPH ulong 0
19const TLSU ulong 1
20const TLFL ulong 2
21const TLHL ulong 3
22const TLRK ulong 5
23const TLWK ulong 6
24const TLCR ulong 7
25const TLFD ulong 8
26const TLDL ulong 16
27const TLRS ulong 24
28const TLWS ulong 32
29const TLWKEY ulong 64
30const TLWIV ulong 96
31const TLRKEY ulong 112
32const TLRIV ulong 144
33const TLCAP ulong 160
34const TLSAP ulong 192
35const TLHOST ulong 224
36const TLPO ulong 480
37const TLPN ulong 484
38const TLHF ulong 488
39const TLDET ulong 492
40note change_cipher_spec state: 0 none allowed yet, 1 one allowed (after ServerHello), 2 no more
41const TLCCS ulong 496
42const TLEPK ulong 512
43const TLEPU ulong 544
44const TLSID ulong 576
45const TLHS ulong 608
46const TLCHS ulong 640
47const TLSHS ulong 672
48const TLMS ulong 704
49const TLSS ulong 736
50const TLCRX ulong 832
51const TLRB ulong 2048
52const TLWB ulong 19456
53const TLLF ulong 36864
54const TLLFSZ ulong 16896
55const TLHB ulong 53760
56const TLHBSZ ulong 65536
57
58note error codes (tls_errname gives the names)
59const TLEARG ulong 1
60const TLEIO ulong 2
61const TLETIME ulong 3
62const TLEEOF ulong 4
63const TLEREC ulong 5
64const TLEMAC ulong 6
65const TLEUNEX ulong 7
66const TLEDEC ulong 8
67const TLEVER ulong 9
68const TLECIPH ulong 10
69const TLEKEYS ulong 11
70const TLEEXT ulong 12
71const TLEHRR ulong 13
72const TLESID ulong 14
73const TLEZERO ulong 15
74const TLEALPN ulong 16
75const TLECERT ulong 17
76const TLESCH ulong 18
77const TLECV ulong 19
78const TLEFIN ulong 20
79const TLEALRT ulong 21
80const TLEBIG ulong 22
81const TLESTAT ulong 23
82const TLERAND ulong 24
83const TLEDNS ulong 25
84const TLECONN ulong 26
85const TLEKUPD ulong 27
86const TLESEQ ulong 28
87const TLESTOR ulong 29
88
89text TLN00 "TLS_OK"
90text TLN01 "TLS_ERR_ARGUMENT"
91text TLN02 "TLS_ERR_IO"
92text TLN03 "TLS_ERR_TIMEOUT"
93text TLN04 "TLS_ERR_EOF"
94text TLN05 "TLS_ERR_RECORD"
95text TLN06 "TLS_ERR_BAD_RECORD_MAC"
96text TLN07 "TLS_ERR_UNEXPECTED_MESSAGE"
97text TLN08 "TLS_ERR_DECODE"
98text TLN09 "TLS_ERR_PROTOCOL_VERSION"
99text TLN10 "TLS_ERR_CIPHER_SUITE"
100text TLN11 "TLS_ERR_KEY_SHARE"
101text TLN12 "TLS_ERR_EXTENSION"
102text TLN13 "TLS_ERR_HELLO_RETRY_UNSUPPORTED"
103text TLN14 "TLS_ERR_SESSION_ID_ECHO"
104text TLN15 "TLS_ERR_ZERO_SHARED_SECRET"
105text TLN16 "TLS_ERR_ALPN"
106text TLN17 "TLS_ERR_CERTIFICATE"
107text TLN18 "TLS_ERR_SIGNATURE_SCHEME"
108text TLN19 "TLS_ERR_CERTIFICATE_VERIFY"
109text TLN20 "TLS_ERR_FINISHED"
110text TLN21 "TLS_ERR_ALERT_RECEIVED"
111text TLN22 "TLS_ERR_MESSAGE_TOO_BIG"
112text TLN23 "TLS_ERR_STATE"
113text TLN24 "TLS_ERR_RANDOM"
114text TLN25 "TLS_ERR_DNS"
115text TLN26 "TLS_ERR_CONNECT"
116text TLN27 "TLS_ERR_KEY_UPDATE"
117text TLN28 "TLS_ERR_SEQUENCE_LIMIT"
118text TLN29 "TLS_ERR_TRUST_STORE"
119text TLNUNK "TLS_ERR_UNKNOWN"
120
121note HKDF labels (RFC 8446 section 7.1, 7.2, 7.3); "tls13 " is prepended by tls_info
122text TLL13 "tls13 "
123text TLLKEY "key"
124text TLLIV "iv"
125text TLLFIN "finished"
126text TLLDER "derived"
127text TLLCHS "c hs traffic"
128text TLLSHS "s hs traffic"
129text TLLCAP "c ap traffic"
130text TLLSAP "s ap traffic"
131text TLLUPD "traffic upd"
132text TLCVC "TLS 1.3, server CertificateVerify"
133text TLALPN "http/1.1"
134text TLRESOLV "/etc/resolv.conf"
135text TLNS "nameserver"
136
137note ---- byte helpers (big-endian, n <= 8) ----
138fn tls_gn b view uchar * o ulong n ulong to ulong
139v = ulong 0
140for i ulong n
141v = or shl v 8 ulong get b add o i
142end
143ret v
144end
145
146fn tls_pn b edit uchar * o ulong v ulong n ulong to void
147x = v
148for i ulong n
149put b sub add o sub n 1 i uchar and x 255
150x = shr x 8
151end
152end
153
154fn tls_cpy d edit uchar * o ulong s view uchar * to void
155for i ulong len s
156put d add o i get s i
157end
158end
159
160note move n bytes inside one array from s to d (d <= s or non-overlapping)
161fn tls_mov b edit uchar * d ulong s ulong n ulong to void
162for i ulong n
163put b add d i get b add s i
164end
165end
166
167fn tls_zero b edit uchar * o ulong n ulong to void
168for i ulong n
169put b add o i 0
170end
171end
172
173note constant-time equality of a[0..n) and b[o..o+n): 0 when equal, 1 otherwise
174fn tls_cteq a view uchar * b view uchar * o ulong n ulong to ulong
175d = ulong 0
176for i ulong n
177d = or d ulong xor get a i get b add o i
178end
179ret shr add d 255 8
180end
181
182note ---- error bookkeeping ----
183fn tls_fail c edit uchar * e ulong dt ulong to ulong
184put c TLPH 3
185tls_pn c TLDET dt 4
186ret e
187end
188
189fn tls_detail c view uchar * to ulong
190ret tls_gn c TLDET 4
191end
192
193fn tls_errname e ulong o edit uchar * to ulong
194n = ulong 0
195if eq e 0
196n = tls_namecp o TLN00
197end
198if eq e 1
199n = tls_namecp o TLN01
200end
201if eq e 2
202n = tls_namecp o TLN02
203end
204if eq e 3
205n = tls_namecp o TLN03
206end
207if eq e 4
208n = tls_namecp o TLN04
209end
210if eq e 5
211n = tls_namecp o TLN05
212end
213if eq e 6
214n = tls_namecp o TLN06
215end
216if eq e 7
217n = tls_namecp o TLN07
218end
219if eq e 8
220n = tls_namecp o TLN08
221end
222if eq e 9
223n = tls_namecp o TLN09
224end
225if eq e 10
226n = tls_namecp o TLN10
227end
228if eq e 11
229n = tls_namecp o TLN11
230end
231if eq e 12
232n = tls_namecp o TLN12
233end
234if eq e 13
235n = tls_namecp o TLN13
236end
237if eq e 14
238n = tls_namecp o TLN14
239end
240if eq e 15
241n = tls_namecp o TLN15
242end
243ret tls_errnm2 e o n
244end
245
246fn tls_errnm2 e ulong o edit uchar * n0 ulong to ulong
247n = n0
248if eq e 16
249n = tls_namecp o TLN16
250end
251if eq e 17
252n = tls_namecp o TLN17
253end
254if eq e 18
255n = tls_namecp o TLN18
256end
257if eq e 19
258n = tls_namecp o TLN19
259end
260if eq e 20
261n = tls_namecp o TLN20
262end
263if eq e 21
264n = tls_namecp o TLN21
265end
266if eq e 22
267n = tls_namecp o TLN22
268end
269if eq e 23
270n = tls_namecp o TLN23
271end
272if eq e 24
273n = tls_namecp o TLN24
274end
275if eq e 25
276n = tls_namecp o TLN25
277end
278if eq e 26
279n = tls_namecp o TLN26
280end
281if eq e 27
282n = tls_namecp o TLN27
283end
284if eq e 28
285n = tls_namecp o TLN28
286end
287if eq e 29
288n = tls_namecp o TLN29
289end
290if eq n 0
291n = tls_namecp o TLNUNK
292end
293ret n
294end
295
296fn tls_namecp o edit uchar * t view uchar * to ulong
297n = len t
298if gt n len o
299n = len o
300end
301for i ulong n
302put o i get t i
303end
304ret n
305end
306
307note internal: a received close_notify alert (tls_read turns it into a clean end of data)
308const TLCLOSE ulong 100
309
310note ---- socket I/O with deadline (c holds the fd and an absolute monotonic deadline, 0 = none;
311note with no deadline every wait is bounded by 30 s of silence) ----
312fn tls_fd c view uchar * to int
313ret int tls_gn c TLFD 4
314end
315
316fn tls_deadline c edit uchar * dl long to void
317if lt dl 0
318tls_pn c TLDL 0 8
319ret
320end
321tls_pn c TLDL ulong dl 8
322end
323
324note wait for ev (1 readable, 4 writable): 0 ready, else an error code
325fn tls_wait c view uchar * ev int to ulong
326dl = tls_gn c TLDL 8
327iv = long 30000000000
328if ne dl 0
329now = monons
330if ge now long dl
331ret TLETIME
332end
333iv = sub long dl now
334end
335r = waitio tls_fd c ev iv
336if eq r 0
337ret TLETIME
338end
339if lt r 0
340if eq r -4
341ret 0
342end
343ret TLEIO
344end
345ret 0
346end
347
348note read exactly n bytes into c[o..o+n)
349fn tls_rdn c edit uchar * o ulong n ulong to ulong
350fd = tls_fd c
351got = ulong 0
352while lt got n bound 999999
353e = tls_wait c 1
354if ne e 0
355ret e
356end
357r = recv fd c add o got sub n got 0
358if eq r 0
359ret TLEEOF
360end
361if lt r 0
362if and ne r -11 ne r -4
363ret TLEIO
364end
365else
366got = add got ulong r
367end
368end
369ret 0
370end
371
372note send c[o..o+n) completely
373fn tls_sendall c edit uchar * o ulong n ulong to ulong
374fd = tls_fd c
375done = ulong 0
376while lt done n bound 999999
377e = tls_wait c 4
378if ne e 0
379ret e
380end
381r = send fd c add o done sub n done 0
382if lt r 0
383if and ne r -11 ne r -4
384ret TLEIO
385end
386else
387done = add done ulong r
388end
389end
390ret 0
391end
392
393note ---- record protection (RFC 8446 section 5.2, 5.3) ----
394note per-record nonce: the 12-byte iv at c[ivo] XOR the 64-bit sequence number at c[sqo],
395note left-padded; key at c[ko]; written as key || nonce into kn
396fn tls_kn c view uchar * kn edit uchar * sel ulong to ulong
397kl = ulong 16
398if eq get c TLSU 1
399kl = 32
400end
401ko = and sel 65535
402for i ulong kl
403put kn i get c add ko i
404end
405tls_nonce c kn or and shr sel 16 4294967295 shl kl 32
406ret add kl 12
407end
408
409note nonce bytes into kn[kl..kl+12): sel = ivo | sqo << 16 | kl << 32
410fn tls_nonce c view uchar * kn edit uchar * sel ulong to void
411kl = shr sel 32
412ivo = and sel 65535
413for i ulong 12
414put kn add kl i get c add ivo i
415end
416sq = tls_gn c and shr sel 16 65535 8
417for i ulong 8
418p = add kl add 4 i
419s = mul 8 sub 7 i
420b = and shr sq s 255
421put kn p uchar xor ulong get kn p b
422end
423end
424
425note bump the 64-bit sequence number at c[o]; 1 when it would wrap (the key must not be reused)
426fn tls_seqinc c edit uchar * o ulong to ulong
427sq = tls_gn c o 8
428if eq sq 18446744073709551615
429ret 1
430end
431tls_pn c o add sq 1 8
432ret 0
433end
434
435note decrypt the record at c[TLRB] whose body is ln bytes; 0 or TLEMAC
436fn tls_open c edit uchar * ln ulong to ulong
437kn = new uchar 44 0
438ad = new uchar 5 0
439for i ulong 5
440put ad i get c add TLRB i
441end
442kl = tls_kn c kn or TLRKEY or shl TLRIV 16 shl TLRS 32
443r = ulong 1
444if eq get c TLSU 1
445r = aead_chacha_open kn ad part c add TLRB 5 ln
446else
447r = aead_aes128gcm_open part kn 0 kl ad part c add TLRB 5 ln
448end
449tls_zero kn 0 44
450if ne r 0
451ret TLEMAC
452end
453if ne tls_seqinc c TLRS 0
454ret TLESEQ
455end
456ret 0
457end
458
459note encrypt c[TLWB+5 .. +n) (inner plaintext incl. type byte), header already at c[TLWB]
460fn tls_seal c edit uchar * n ulong to ulong
461kn = new uchar 44 0
462ad = new uchar 5 0
463for i ulong 5
464put ad i get c add TLWB i
465end
466kl = tls_kn c kn or TLWKEY or shl TLWIV 16 shl TLWS 32
467r = ulong 1
468if eq get c TLSU 1
469r = aead_chacha_seal kn ad part c add TLWB 5 add n 16
470else
471r = aead_aes128gcm_seal part kn 0 kl ad part c add TLWB 5 add n 16
472end
473tls_zero kn 0 44
474if ne r 0
475ret TLEARG
476end
477if ne tls_seqinc c TLWS 0
478ret TLESEQ
479end
480ret 0
481end
482
483note send one record of content type ty whose n plaintext bytes are at c[TLWB+5]
484fn tls_wrbuf c edit uchar * ty ulong n ulong to ulong
485if gt n 16384
486ret TLEARG
487end
488if eq get c TLWK 0
489put c TLWB uchar ty
490tls_pn c add TLWB 1 771 2
491tls_pn c add TLWB 3 n 2
492ret tls_sendall c TLWB add n 5
493end
494put c add add TLWB 5 n uchar ty
495ln = add n 17
496put c TLWB 23
497tls_pn c add TLWB 1 771 2
498tls_pn c add TLWB 3 ln 2
499e = tls_seal c add n 1
500if ne e 0
501ret e
502end
503ret tls_sendall c TLWB add ln 5
504end
505
506note best-effort fatal alert (RFC 8446 section 6)
507fn tls_alert c edit uchar * desc ulong to void
508put c add TLWB 5 2
509put c add TLWB 6 uchar desc
510e = tls_wrbuf c 21 2
511put c TLPH 3
512end
513
514note read one record into c[TLRB]; on 0 the content type is at c[TLRB] and the plaintext length
515note at c[TLRB+3..+5), plaintext from c[TLRB+5]
516fn tls_rdrec c edit uchar * to ulong
517e = tls_rdn c TLRB 5
518if ne e 0
519ret e
520end
521ty = ulong get c TLRB
522ln = tls_gn c add TLRB 3 2
523if gt ln 16640
524ret TLEREC
525end
526e = tls_rdn c add TLRB 5 ln
527if ne e 0
528ret e
529end
530if eq ty 20
531if or ne ln 1 ne get c add TLRB 5 1
532ret TLEUNEX
533end
534if ne get c TLCCS 1
535ret TLEUNEX
536end
537put c TLCCS 2
538ret 0
539end
540if eq get c TLRK 0
541if and ne ty 22 ne ty 21
542ret TLEUNEX
543end
544if or eq ln 0 gt ln 16384
545ret TLEREC
546end
547ret 0
548end
549if ne ty 23
550ret TLEUNEX
551end
552put c TLCCS 2
553if lt ln 17
554ret TLEREC
555end
556e = tls_open c ln
557if ne e 0
558ret e
559end
560n = sub ln 16
561while gt n 0 bound 20000
562if ne get c add add TLRB 4 n 0
563break
564end
565n = sub n 1
566end
567if eq n 0
568ret TLEUNEX
569end
570n = sub n 1
571if gt n 16384
572ret TLEREC
573end
574put c TLRB get c add add TLRB 5 n
575tls_pn c add TLRB 3 n 2
576if eq get c TLRB 20
577ret TLEUNEX
578end
579ret 0
580end
581
582note a received alert record: close_notify gives TLCLOSE, any other alert TLEALRT (detail = desc)
583fn tls_ralert c edit uchar * to ulong
584if ne tls_gn c add TLRB 3 2 2
585ret TLEDEC
586end
587d = ulong get c add TLRB 6
588tls_pn c TLDET d 4
589if eq d 0
590ret TLCLOSE
591end
592ret TLEALRT
593end
594
595note ---- handshake message buffer (c[TLHB], fill count at c[TLHF]) ----
596note make sure one whole handshake message is at c[TLHB]; 0 or an error code. A message spread
597note over more than 4000 records is refused (TLEBIG); close_notify here is TLEALRT, detail 0
598fn tls_hsmsg c edit uchar * to ulong
599k = ulong 0
600while true bound 4096
601k = add k 1
602if gt k 4000
603ret TLEBIG
604end
605f = tls_gn c TLHF 4
606if ge f 4
607ml = add tls_gn c add TLHB 1 3 4
608if gt ml TLHBSZ
609ret TLEBIG
610end
611if ge f ml
612ret 0
613end
614end
615e = tls_rdrec c
616if ne e 0
617ret e
618end
619ty = ulong get c TLRB
620n = tls_gn c add TLRB 3 2
621if eq ty 21
622e = tls_ralert c
623if eq e TLCLOSE
624ret TLEALRT
625end
626ret e
627end
628if ne ty 20
629if ne ty 22
630ret TLEUNEX
631end
632if eq n 0
633ret TLEUNEX
634end
635if gt add f n TLHBSZ
636ret TLEBIG
637end
638tls_mov c add TLHB f add TLRB 5 n
639tls_pn c TLHF add f n 4
640end
641end
642ret TLEUNEX
643end
644
645note drop the message at c[TLHB] (after hashing it when h is 1)
646fn tls_hsdrop c edit uchar * to void
647f = tls_gn c TLHF 4
648ml = add tls_gn c add TLHB 1 3 4
649tls_mov c TLHB add TLHB ml sub f ml
650tls_pn c TLHF sub f ml 4
651end
652
653fn tls_hsdone c edit uchar * th edit ulong * to void
654ml = add tls_gn c add TLHB 1 3 4
655sha256_update th part c TLHB ml
656tls_hsdrop c
657end
658
659note ---- key schedule (RFC 8446 section 7.1), SHA-256 for both suites ----
660const TLSPUB ulong 800
661const TLTMP ulong 768
662note label ids for tls_drv
663const TLIKEY ulong 0
664const TLIIV ulong 1
665const TLIFIN ulong 2
666const TLIDER ulong 3
667const TLICHS ulong 4
668const TLISHS ulong 5
669const TLICAP ulong 6
670const TLISAP ulong 7
671const TLIUPD ulong 8
672
673fn tls_labcp p edit uchar * o ulong lid ulong to ulong
674t = ulong 0
675if eq lid TLIKEY
676tls_cpy p o TLLKEY
677t = len TLLKEY
678end
679if eq lid TLIIV
680tls_cpy p o TLLIV
681t = len TLLIV
682end
683if eq lid TLIFIN
684tls_cpy p o TLLFIN
685t = len TLLFIN
686end
687if eq lid TLIDER
688tls_cpy p o TLLDER
689t = len TLLDER
690end
691if eq lid TLICHS
692tls_cpy p o TLLCHS
693t = len TLLCHS
694end
695if eq lid TLISHS
696tls_cpy p o TLLSHS
697t = len TLLSHS
698end
699if eq lid TLICAP
700tls_cpy p o TLLCAP
701t = len TLLCAP
702end
703if eq lid TLISAP
704tls_cpy p o TLLSAP
705t = len TLLSAP
706end
707if eq lid TLIUPD
708tls_cpy p o TLLUPD
709t = len TLLUPD
710end
711ret t
712end
713
714note HkdfLabel after the 32-byte secret in p: uint16 ln, opaque label<7..255> = "tls13 " + label,
715note opaque context<0..255> = hv. Returns 32 + the HkdfLabel length.
716fn tls_info p edit uchar * lid ulong hv view uchar * ln ulong to ulong
717tls_pn p 32 ln 2
718tls_cpy p 35 TLL13
719t = tls_labcp p 41 lid
720put p 34 uchar add t 6
721o = add 41 t
722put p o uchar len hv
723tls_cpy p add o 1 hv
724ret add add o 1 len hv
725end
726
727note c[dst..dst+ln) = HKDF-Expand-Label(c[src..src+32), label lid, hv, ln);
728note sel = dst | src << 16 | ln << 32 | lid << 48
729fn tls_drv c edit uchar * sel ulong hv view uchar * to ulong
730p = new uchar 160 0
731k = new uchar 32 0
732src = and shr sel 16 65535
733for i ulong 32
734put p i get c add src i
735end
736n = tls_info p shr sel 48 hv and shr sel 32 65535
737r = tls_hexp p n k
738tls_drvout c k sel
739tls_zero p 0 160
740tls_zero k 0 32
741ret r
742end
743
744note HKDF-Expand of p[32..n) under the PRK p[0..32) into all 32 bytes of k; a shorter label
745note length L takes the first L bytes (T(1) does not depend on the output length)
746fn tls_hexp p view uchar * n ulong k edit uchar * to ulong
747ret hkdf_expand 256 part p 0 n 32 k
748end
749
750fn tls_drvout c edit uchar * k view uchar * sel ulong to void
751dst = and sel 65535
752for i ulong and shr sel 32 65535
753put c add dst i get k i
754end
755end
756
757fn tls_sel dst ulong src ulong ln ulong lid ulong to ulong
758ret or or dst shl src 16 or shl ln 32 shl lid 48
759end
760
761note c[dst..+32) = HKDF-Extract(salt, ikm); sel = dst | salt << 16 | ikm << 32, where offset 0
762note stands for 32 zero bytes (RFC 8446 "0" input)
763fn tls_xtr c edit uchar * sel ulong to ulong
764s = new uchar 64 0
765k = new uchar 32 0
766so = and shr sel 16 65535
767io = shr sel 32
768if ne so 0
769for i ulong 32
770put s i get c add so i
771end
772end
773if ne io 0
774for i ulong 32
775put s add 32 i get c add io i
776end
777end
778r = hkdf_extract 256 s 32 k
779dst = and sel 65535
780for i ulong 32
781put c add dst i get k i
782end
783tls_zero s 0 64
784tls_zero k 0 32
785if ne r 32
786ret 1
787end
788ret 0
789end
790
791note traffic key and iv from the secret at c[so]; d 0 installs the write side, 1 the read side
792fn tls_keys c edit uchar * so ulong d ulong to ulong
793e = new uchar 1 0
794kl = ulong 16
795if eq get c TLSU 1
796kl = 32
797end
798ko = TLWKEY
799io = TLWIV
800sq = TLWS
801if eq d 1
802ko = TLRKEY
803io = TLRIV
804sq = TLRS
805end
806r = tls_drv c tls_sel ko so kl TLIKEY part e 0 0
807r = or r tls_drv c tls_sel io so 12 TLIIV part e 0 0
808tls_pn c sq 0 8
809ret r
810end
811
812note transcript hash so far into o (32 bytes), leaving th running
813fn tls_th th view ulong * o edit uchar * to void
814t = new ulong 40 0
815sha2_copy t th
816sha256_final t o
817for i ulong 40
818put t i 0
819end
820end
821
822note SHA-256 of the empty string (the Derive-Secret context for "derived")
823fn tls_h0 o edit uchar * to void
824e = new uchar 1 0
825n = sha2_digest 256 part e 0 0 o
826end
827
828note ---- ClientHello (RFC 8446 section 4.1.2) built at c[TLWB+5] ----
829note server_name, supported_groups (x25519), signature_algorithms; returns the next offset
830fn tls_chex1 c edit uchar * p ulong to ulong
831hl = ulong get c TLHL
832tls_pn c p 0 2
833tls_pn c add p 2 add hl 5 2
834tls_pn c add p 4 add hl 3 2
835put c add p 6 0
836tls_pn c add p 7 hl 2
837tls_mov c add p 9 TLHOST hl
838q = add add p 9 hl
839tls_pn c q 10 2
840tls_pn c add q 2 4 2
841tls_pn c add q 4 2 2
842tls_pn c add q 6 29 2
843q = add q 8
844tls_pn c q 13 2
845tls_pn c add q 2 18 2
846tls_pn c add q 4 16 2
847tls_pn c add q 6 1027 2
848tls_pn c add q 8 1283 2
849tls_pn c add q 10 2052 2
850tls_pn c add q 12 2053 2
851tls_pn c add q 14 2054 2
852tls_pn c add q 16 1025 2
853tls_pn c add q 18 1281 2
854tls_pn c add q 20 1537 2
855ret add q 22
856end
857
858note supported_versions (TLS 1.3 only), key_share (x25519), ALPN http/1.1 when flag 1
859fn tls_chex2 c edit uchar * p ulong to ulong
860tls_pn c p 43 2
861tls_pn c add p 2 3 2
862put c add p 4 2
863tls_pn c add p 5 772 2
864q = add p 7
865tls_pn c q 51 2
866tls_pn c add q 2 38 2
867tls_pn c add q 4 36 2
868tls_pn c add q 6 29 2
869tls_pn c add q 8 32 2
870tls_mov c add q 10 TLEPU 32
871q = add q 42
872if eq and get c TLFL 1 0
873ret q
874end
875tls_pn c q 16 2
876tls_pn c add q 2 11 2
877tls_pn c add q 4 9 2
878put c add q 6 8
879tls_cpy c add q 7 TLALPN
880ret add q 15
881end
882
883note ephemeral x25519 key pair: private at c[TLEPK], public at c[TLEPU]
884fn tls_ephem c edit uchar * to ulong
885k = new uchar 32 0
886o = new uchar 32 0
887r = random k 0 32 0
888if ne r 32
889ret TLERAND
890end
891e = x25519_base o k
892for i ulong 32
893put c add TLEPK i get k i
894put c add TLEPU i get o i
895end
896tls_zero k 0 32
897if ne e 0
898ret TLERAND
899end
900ret 0
901end
902
903note writes the ClientHello handshake message at c[TLWB+5]; 0 or an error code
904fn tls_mkch c edit uchar * to ulong
905e = tls_ephem c
906if ne e 0
907ret e
908end
909b = add TLWB 5
910put c b 1
911tls_pn c add b 4 771 2
912r = random c add b 6 32 0
913if ne r 32
914ret TLERAND
915end
916put c add b 38 32
917r = random c add b 39 32 0
918if ne r 32
919ret TLERAND
920end
921tls_mov c TLSID add b 39 32
922p = add b 71
923tls_pn c p 4 2
924tls_pn c add p 2 4867 2
925tls_pn c add p 4 4865 2
926put c add p 6 1
927put c add p 7 0
928x = add p 8
929q = tls_chex1 c add x 2
930q = tls_chex2 c q
931tls_pn c x sub sub q x 2 2
932tls_pn c add b 1 sub sub q b 4 3
933ret 0
934end
935
936note ---- ServerHello (RFC 8446 section 4.1.3) at c[TLHB] ----
937note HRR random = SHA-256("HelloRetryRequest"), computed rather than tabled
938text TLHRR "HelloRetryRequest"
939
940note 0 when the 32 bytes at c[o] equal our legacy_session_id
941fn tls_sidok c view uchar * o ulong to ulong
942s = new uchar 32 0
943for i ulong 32
944put s i get c add TLSID i
945end
946ret tls_cteq s c o 32
947end
948
949fn tls_ishrr c view uchar * o ulong to ulong
950h = new uchar 32 0
951n = sha2_digest 256 TLHRR h
952ret xor tls_cteq h c o 32 1
953end
954
955note fixed part: version, random, session id echo, suite, compression; 0 or error
956fn tls_shhead c edit uchar * to ulong
957if ne get c TLHB 2
958ret TLEUNEX
959end
960ln = tls_gn c add TLHB 1 3
961if lt ln 76
962ret TLEDEC
963end
964b = add TLHB 4
965if ne tls_gn c b 2 771
966ret TLEVER
967end
968if eq tls_ishrr c add b 2 1
969ret TLEHRR
970end
971if ne get c add b 34 32
972ret TLESID
973end
974if ne tls_sidok c add b 35 0
975ret TLESID
976end
977s = tls_gn c add b 67 2
978put c TLSU 0
979if eq s 4867
980put c TLSU 1
981end
982if eq s 4865
983put c TLSU 2
984end
985if eq get c TLSU 0
986ret TLECIPH
987end
988if ne get c add b 69 0
989ret TLEDEC
990end
991x = tls_gn c add b 70 2
992if ne add x 72 ln
993ret TLEDEC
994end
995ret 0
996end
997
998note ServerHello extensions: exactly supported_versions = 0x0304 and an x25519 key_share
999fn tls_shext c edit uchar * to ulong
1000ln = tls_gn c add TLHB 1 3
1001q = add TLHB 76
1002z = add add TLHB 4 ln
1003seen = ulong 0
1004while lt q z bound 64
1005if gt add q 4 z
1006ret TLEDEC
1007end
1008t = tls_gn c q 2
1009l = tls_gn c add q 2 2
1010q = add q 4
1011if gt add q l z
1012ret TLEDEC
1013end
1014if eq t 43
1015if or ne l 2 ne and seen 1 0
1016ret TLEEXT
1017end
1018if ne tls_gn c q 2 772
1019ret TLEVER
1020end
1021seen = or seen 1
1022else
1023if or ne t 51 ne and seen 2 0
1024ret TLEEXT
1025end
1026if ne l 36
1027ret TLEKEYS
1028end
1029if or ne tls_gn c q 2 29 ne tls_gn c add q 2 2 32
1030ret TLEKEYS
1031end
1032tls_mov c TLSPUB add q 4 32
1033seen = or seen 2
1034end
1035q = add q l
1036end
1037if eq and seen 1 0
1038ret TLEVER
1039end
1040if eq and seen 2 0
1041ret TLEKEYS
1042end
1043ret 0
1044end
1045
1046note shared secret into c[TLSS]; the private key is wiped
1047fn tls_ecdh c edit uchar * to ulong
1048k = new uchar 32 0
1049u = new uchar 32 0
1050o = new uchar 32 0
1051for i ulong 32
1052put k i get c add TLEPK i
1053put u i get c add TLSPUB i
1054end
1055r = x25519_scalarmult o k u
1056for i ulong 32
1057put c add TLSS i get o i
1058end
1059tls_zero k 0 32
1060tls_zero o 0 32
1061tls_zero c TLEPK 32
1062if ne r 0
1063ret TLEZERO
1064end
1065ret 0
1066end
1067
1068note handshake secrets after ServerHello; installs the handshake write and read keys
1069fn tls_hskeys c edit uchar * th view ulong * to ulong
1070h = new uchar 32 0
1071z = new uchar 32 0
1072tls_h0 z
1073r = tls_xtr c TLHS
1074r = or r tls_drv c tls_sel TLHS TLHS 32 TLIDER z
1075r = or r tls_xtr c or TLHS or shl TLHS 16 shl TLSS 32
1076tls_th th h
1077r = or r tls_drv c tls_sel TLCHS TLHS 32 TLICHS h
1078r = or r tls_drv c tls_sel TLSHS TLHS 32 TLISHS h
1079r = or r tls_drv c tls_sel TLMS TLHS 32 TLIDER z
1080r = or r tls_xtr c or TLMS shl TLMS 16
1081tls_zero c TLSS 32
1082r = or r tls_keys c TLSHS 1
1083r = or r tls_keys c TLCHS 0
1084if ne r 0
1085ret TLEARG
1086end
1087put c TLRK 1
1088put c TLCCS 1
1089ret 0
1090end
1091
1092note read and check ServerHello, send the compatibility change_cipher_spec, derive keys
1093fn tls_rdsh c edit uchar * th edit ulong * to ulong
1094e = tls_hsmsg c
1095if ne e 0
1096ret e
1097end
1098e = tls_shhead c
1099if ne e 0
1100ret e
1101end
1102e = tls_shext c
1103if ne e 0
1104ret e
1105end
1106tls_hsdone c th
1107if ne tls_gn c TLHF 4 0
1108ret TLEUNEX
1109end
1110e = tls_ecdh c
1111if ne e 0
1112ret e
1113end
1114put c add TLWB 5 1
1115e = tls_wrbuf c 20 1
1116if ne e 0
1117ret e
1118end
1119e = tls_hskeys c th
1120if ne e 0
1121ret e
1122end
1123put c TLWK 1
1124ret 0
1125end
1126
1127note ---- server's encrypted flight (RFC 8446 sections 4.3, 4.4) ----
1128note EncryptedExtensions: only server_name (empty), supported_groups and ALPN (offered, http/1.1)
1129fn tls_ee c edit uchar * to ulong
1130if ne get c TLHB 8
1131ret TLEUNEX
1132end
1133ln = tls_gn c add TLHB 1 3
1134if lt ln 2
1135ret TLEDEC
1136end
1137z = add add TLHB 4 ln
1138if ne add tls_gn c add TLHB 4 2 2 ln
1139ret TLEDEC
1140end
1141q = add TLHB 6
1142seen = ulong 0
1143while lt q z bound 64
1144if gt add q 4 z
1145ret TLEDEC
1146end
1147t = tls_gn c q 2
1148l = tls_gn c add q 2 2
1149q = add q 4
1150if gt add q l z
1151ret TLEDEC
1152end
1153bit = ulong 0
1154if eq t 0
1155if ne l 0
1156ret TLEDEC
1157end
1158bit = 1
1159end
1160if eq t 10
1161bit = 2
1162end
1163if eq t 16
1164e = tls_alpnok c q l
1165if ne e 0
1166ret e
1167end
1168bit = 4
1169end
1170if or eq bit 0 ne and seen bit 0
1171ret TLEEXT
1172end
1173seen = or seen bit
1174q = add q l
1175end
1176ret 0
1177end
1178
1179note the server's ALPN choice must be exactly the one protocol we offered
1180fn tls_alpnok c view uchar * q ulong l ulong to ulong
1181if eq and get c TLFL 1 0
1182ret TLEEXT
1183end
1184if or ne l 11 ne tls_gn c q 2 9
1185ret TLEALPN
1186end
1187if ne get c add q 2 8
1188ret TLEALPN
1189end
1190if ne tls_cteq TLALPN c add q 3 8 0
1191ret TLEALPN
1192end
1193ret 0
1194end
1195
1196note CertificateRequest: remember its context (we answer with an empty Certificate)
1197fn tls_cr c edit uchar * to ulong
1198ln = tls_gn c add TLHB 1 3
1199if lt ln 3
1200ret TLEDEC
1201end
1202cl = ulong get c add TLHB 4
1203if gt add cl 3 ln
1204ret TLEDEC
1205end
1206if ne add add cl 3 tls_gn c add add TLHB 5 cl 2 ln
1207ret TLEDEC
1208end
1209put c TLCRX uchar cl
1210tls_mov c add TLCRX 1 add TLHB 5 cl
1211put c TLCR 1
1212ret 0
1213end
1214
1215note Certificate: empty context, then the certificate_list (validated by x509_chain_verify);
1216note the leaf entry is kept at c[TLLF] (extensions stripped) for CertificateVerify
1217fn tls_cert c edit uchar * st view uchar * to ulong
1218if ne get c TLHB 11
1219ret TLEUNEX
1220end
1221ln = tls_gn c add TLHB 1 3
1222if lt ln 4
1223ret TLEDEC
1224end
1225if ne get c add TLHB 4 0
1226ret TLEDEC
1227end
1228cl = tls_gn c add TLHB 5 3
1229if ne add cl 4 ln
1230ret TLEDEC
1231end
1232if lt cl 5
1233ret TLECERT
1234end
1235ret tls_chain c st cl
1236end
1237
1238note hq = now (8 bytes, unix seconds) || hostname; returns its length
1239fn tls_hq c view uchar * hq edit uchar * to ulong
1240now = div realns 1000000000
1241tls_pn hq 0 ulong now 8
1242hl = ulong get c TLHL
1243for i ulong hl
1244put hq add 8 i get c add TLHOST i
1245end
1246ret add 8 hl
1247end
1248
1249fn tls_chain c edit uchar * st view uchar * cl ulong to ulong
1250hq = new uchar 264 0
1251n = tls_hq c hq
1252r = x509_chain_verify part c add TLHB 8 cl st part hq 0 n
1253if ne r 0
1254tls_pn c TLDET r 4
1255ret TLECERT
1256end
1257ret tls_leaf c cl
1258end
1259
1260fn tls_leaf c edit uchar * cl ulong to ulong
1261l1 = tls_gn c add TLHB 8 3
1262if gt add l1 5 cl
1263ret TLEDEC
1264end
1265if gt add l1 5 TLLFSZ
1266ret TLEBIG
1267end
1268tls_mov c TLLF add TLHB 8 add l1 3
1269put c add add TLLF l1 3 0
1270put c add add TLLF l1 4 0
1271ret 0
1272end
1273
1274note CertificateVerify over the transcript hash h (through Certificate)
1275fn tls_cv c edit uchar * h view uchar * to ulong
1276if ne get c TLHB 15
1277ret TLEUNEX
1278end
1279ln = tls_gn c add TLHB 1 3
1280if lt ln 4
1281ret TLEDEC
1282end
1283sch = tls_gn c add TLHB 4 2
1284sl = tls_gn c add TLHB 6 2
1285if ne add sl 4 ln
1286ret TLEDEC
1287end
1288if gt sl 1024
1289ret TLEDEC
1290end
1291if eq tls_schok sch 0
1292ret TLESCH
1293end
1294m = new uchar 1154 0
1295tls_cvmsg c h m
1296r = tls_cvcall c m sch
1297if ne r 0
1298tls_pn c TLDET r 4
1299ret TLECV
1300end
1301ret 0
1302end
1303
1304note m = 64 spaces || context string || 0 || h || signature (from the message at c[TLHB])
1305fn tls_cvmsg c view uchar * h view uchar * m edit uchar * to void
1306for i ulong 64
1307put m i 32
1308end
1309tls_cpy m 64 TLCVC
1310put m 97 0
1311tls_cpy m 98 h
1312sl = tls_gn c add TLHB 6 2
1313for i ulong sl
1314put m add 130 i get c add add TLHB 8 i
1315end
1316end
1317
1318fn tls_cvcall c edit uchar * m view uchar * sch ulong to ulong
1319sl = tls_gn c add TLHB 6 2
1320l1 = add tls_gn c TLLF 3 5
1321ret x509_cv_verify part c TLLF l1 sch part m 0 add 130 sl 130
1322end
1323
1324note 1 for the CertificateVerify schemes we offered (never rsa_pkcs1_*, RFC 8446 section 4.4.3)
1325fn tls_schok s ulong to ulong
1326if or eq s 1027 eq s 1283
1327ret 1
1328end
1329if or eq s 2052 or eq s 2053 eq s 2054
1330ret 1
1331end
1332ret 0
1333end
1334
1335note hv = HMAC(finished_key(secret at c[so]), hv): the transcript hash in, verify_data out
1336note (RFC 8446 section 4.4.4)
1337fn tls_vd c edit uchar * so ulong hv edit uchar * to ulong
1338e = new uchar 1 0
1339k = new uchar 32 0
1340x = new ulong 96 0
1341r = tls_drv c tls_sel TLTMP so 32 TLIFIN part e 0 0
1342for i ulong 32
1343put k i get c add TLTMP i
1344end
1345tls_zero c TLTMP 32
1346hmac_init 256 x k
1347hmac_update x hv
1348n = hmac_final x hv
1349tls_zero k 0 32
1350for i ulong 96
1351put x i 0
1352end
1353ret r
1354end
1355
1356note server Finished
1357fn tls_sfin c edit uchar * h view uchar * to ulong
1358if ne get c TLHB 20
1359ret TLEUNEX
1360end
1361if ne tls_gn c add TLHB 1 3 32
1362ret TLEDEC
1363end
1364v = new uchar 32 0
1365tls_cpy v 0 h
1366r = tls_vd c TLSHS v
1367if ne r 0
1368ret TLEARG
1369end
1370if ne tls_cteq v c add TLHB 4 32 0
1371ret TLEFIN
1372end
1373ret 0
1374end
1375
1376note EE, optional CertificateRequest, Certificate, CertificateVerify, Finished
1377fn tls_rdenc c edit uchar * st view uchar * th edit ulong * to ulong
1378e = tls_hsmsg c
1379if eq e 0
1380e = tls_ee c
1381end
1382if ne e 0
1383ret e
1384end
1385tls_hsdone c th
1386e = tls_hsmsg c
1387if ne e 0
1388ret e
1389end
1390if eq get c TLHB 13
1391e = tls_cr c
1392if ne e 0
1393ret e
1394end
1395tls_hsdone c th
1396e = tls_hsmsg c
1397if ne e 0
1398ret e
1399end
1400end
1401e = tls_cert c st
1402if ne e 0
1403ret e
1404end
1405tls_hsdone c th
1406ret tls_rdfin c th
1407end
1408
1409fn tls_rdfin c edit uchar * th edit ulong * to ulong
1410h = new uchar 32 0
1411tls_th th h
1412e = tls_hsmsg c
1413if eq e 0
1414e = tls_cv c h
1415end
1416if ne e 0
1417ret e
1418end
1419tls_hsdone c th
1420tls_th th h
1421e = tls_hsmsg c
1422if eq e 0
1423e = tls_sfin c h
1424end
1425if ne e 0
1426ret e
1427end
1428tls_hsdone c th
1429if ne tls_gn c TLHF 4 0
1430ret TLEUNEX
1431end
1432ret tls_apkeys c th
1433end
1434
1435note application traffic secrets from the transcript through server Finished; installs the read key
1436fn tls_apkeys c edit uchar * th view ulong * to ulong
1437h = new uchar 32 0
1438tls_th th h
1439r = tls_drv c tls_sel TLCAP TLMS 32 TLICAP h
1440r = or r tls_drv c tls_sel TLSAP TLMS 32 TLISAP h
1441r = or r tls_keys c TLSAP 1
1442if ne r 0
1443ret TLEARG
1444end
1445ret 0
1446end
1447
1448note client flight: [empty Certificate], Finished; then the application write key
1449fn tls_cfin c edit uchar * th edit ulong * to ulong
1450b = add TLWB 5
1451if eq get c TLCR 1
1452cl = ulong get c TLCRX
1453put c b 11
1454tls_pn c add b 1 add cl 4 3
1455put c add b 4 uchar cl
1456tls_mov c add b 5 add TLCRX 1 cl
1457tls_pn c add add b 5 cl 0 3
1458n = add cl 8
1459sha256_update th part c b n
1460e = tls_wrbuf c 22 n
1461if ne e 0
1462ret e
1463end
1464end
1465v = new uchar 32 0
1466tls_th th v
1467r = tls_vd c TLCHS v
1468put c b 20
1469tls_pn c add b 1 32 3
1470for i ulong 32
1471put c add add b 4 i get v i
1472end
1473sha256_update th part c b 36
1474e = tls_wrbuf c 22 36
1475if ne e 0
1476ret e
1477end
1478r = or r tls_keys c TLCAP 0
1479tls_zero c TLHS 128
1480if ne r 0
1481ret TLEARG
1482end
1483ret 0
1484end
1485
1486note alert description for a failure (0: none, the peer is gone or already alerted)
1487fn tls_alertfor e ulong to ulong
1488if or eq e TLEIO or eq e TLETIME or eq e TLEEOF eq e TLEALRT
1489ret 0
1490end
1491if eq e TLEMAC
1492ret 20
1493end
1494if or eq e TLEREC eq e TLEBIG
1495ret 22
1496end
1497if eq e TLEUNEX
1498ret 10
1499end
1500if eq e TLEDEC
1501ret 50
1502end
1503if eq e TLEVER
1504ret 70
1505end
1506if eq e TLEHRR
1507ret 40
1508end
1509if eq e TLEEXT
1510ret 110
1511end
1512if eq e TLECERT
1513ret 42
1514end
1515if or eq e TLECV eq e TLEFIN
1516ret 51
1517end
1518if eq e TLEALPN
1519ret 120
1520end
1521if or eq e TLECIPH or eq e TLEKEYS or eq e TLESID or eq e TLEZERO or eq e TLESCH eq e TLEKUPD
1522ret 47
1523end
1524ret 80
1525end
1526
1527note fail: alert (if any), mark the connection failed, wipe key material, return e
1528fn tls_abort c edit uchar * e ulong to ulong
1529d = tls_alertfor e
1530if ne d 0
1531tls_alert c d
1532end
1533put c TLPH 3
1534tls_zero c TLWKEY 96
1535tls_zero c TLCAP 64
1536tls_zero c TLEPK 320
1537ret e
1538end
1539
1540note ---- public API ----
1541note tls_init: c (at least TLCSZ bytes) for hostname hn; flags: 1 = offer ALPN http/1.1.
1542note Hostnames are 1..253 bytes of letters, digits, '-' and '.', and not an IPv4 literal.
1543fn tls_init c edit uchar * hn view uchar * fl ulong to ulong
1544if lt len c TLCSZ
1545ret TLEARG
1546end
1547tls_zero c 0 TLRB
1548hl = len hn
1549if or eq hl 0 gt hl 253
1550ret TLEARG
1551end
1552alpha = ulong 0
1553for i ulong hl
1554k = tls_hch ulong get hn i
1555if eq k 0
1556ret TLEARG
1557end
1558alpha = or alpha and k 1
1559put c add TLHOST i get hn i
1560end
1561if eq alpha 0
1562ret TLEARG
1563end
1564put c TLHL uchar hl
1565put c TLFL uchar and fl 1
1566ret 0
1567end
1568
1569note hostname byte class: 1 letter, 2 digit, '-' or '.', 0 anything else
1570fn tls_hch ch ulong to ulong
1571if or and ge ch 97 le ch 122 and ge ch 65 le ch 90
1572ret 1
1573end
1574if or and ge ch 48 le ch 57 or eq ch 45 eq ch 46
1575ret 2
1576end
1577ret 0
1578end
1579
1580note load the system trust store /etc/ssl/certs/ca-certificates.crt into st (TLSTSZ bytes)
1581fn tls_store st edit uchar * to ulong
1582r = x509_store_load st
1583if le r 0
1584ret TLESTOR
1585end
1586ret 0
1587end
1588
1589note load trust anchors from the PEM bundle at path instead (tests with a private CA)
1590fn tls_storef st edit uchar * path view uchar * to ulong
1591r = x509_store_loadf st path
1592if le r 0
1593ret TLESTOR
1594end
1595ret 0
1596end
1597
1598note full handshake on the connected socket fd; 0 or a named error code (tls_errname)
1599fn tls_connect c edit uchar * st view uchar * fd int to ulong
1600if or ne get c TLPH 0 eq get c TLHL 0
1601ret TLESTAT
1602end
1603if lt fd 0
1604ret TLEARG
1605end
1606tls_pn c TLFD ulong fd 4
1607th = new ulong 40 0
1608sha256_init th
1609e = tls_mkch c
1610if ne e 0
1611ret tls_abort c e
1612end
1613n = add tls_gn c add TLWB 6 3 4
1614sha256_update th part c add TLWB 5 n
1615e = tls_wrbuf c 22 n
1616if eq e 0
1617e = tls_rdsh c th
1618end
1619if eq e 0
1620e = tls_rdenc c st th
1621end
1622if eq e 0
1623e = tls_cfin c th
1624end
1625for i ulong 40
1626put th i 0
1627end
1628if ne e 0
1629ret tls_abort c e
1630end
1631put c TLPH 1
1632ret 0
1633end
1634
1635note ---- application data ----
1636note KeyUpdate (RFC 8446 section 4.6.3): new read key; answer update_requested once
1637fn tls_kupd c edit uchar * to ulong
1638if ne tls_gn c add TLHB 1 3 1
1639ret TLEDEC
1640end
1641v = ulong get c add TLHB 4
1642if gt v 1
1643ret TLEKUPD
1644end
1645tls_hsdrop c
1646if ne tls_gn c TLHF 4 0
1647ret TLEUNEX
1648end
1649e = new uchar 1 0
1650r = tls_drv c tls_sel TLSAP TLSAP 32 TLIUPD part e 0 0
1651r = or r tls_keys c TLSAP 1
1652if eq v 1
1653b = add TLWB 5
1654put c b 24
1655tls_pn c add b 1 1 3
1656put c add b 4 0
1657x = tls_wrbuf c 22 5
1658if ne x 0
1659ret x
1660end
1661r = or r tls_drv c tls_sel TLCAP TLCAP 32 TLIUPD part e 0 0
1662r = or r tls_keys c TLCAP 0
1663end
1664if ne r 0
1665ret TLEARG
1666end
1667ret 0
1668end
1669
1670note post-handshake handshake records: NewSessionTicket is ignored (no resumption), KeyUpdate
1671note handled, anything else refused
1672fn tls_post c edit uchar * to ulong
1673f = tls_gn c TLHF 4
1674n = tls_gn c add TLRB 3 2
1675if or eq n 0 gt add f n TLHBSZ
1676ret TLEUNEX
1677end
1678tls_mov c add TLHB f add TLRB 5 n
1679f = add f n
1680tls_pn c TLHF f 4
1681while ge f 4 bound 4096
1682ml = add tls_gn c add TLHB 1 3 4
1683if gt ml TLHBSZ
1684ret TLEBIG
1685end
1686if lt f ml
1687ret 0
1688end
1689t = ulong get c TLHB
1690if eq t 4
1691tls_hsdrop c
1692else
1693if ne t 24
1694ret TLEUNEX
1695end
1696e = tls_kupd c
1697if ne e 0
1698ret e
1699end
1700end
1701f = tls_gn c TLHF 4
1702end
1703ret 0
1704end
1705
1706note copy pending application bytes into o; returns the count
1707fn tls_take c edit uchar * o edit uchar * to ulong
1708po = tls_gn c TLPO 4
1709pn = tls_gn c TLPN 4
1710n = pn
1711if gt n len o
1712n = len o
1713end
1714for i ulong n
1715put o i get c add add add TLRB 5 po i
1716end
1717tls_pn c TLPO add po n 4
1718tls_pn c TLPN sub pn n 4
1719ret n
1720end
1721
1722note tls_read: decrypted application bytes into o. Returns the count (> 0), 0 at the peer's
1723note close_notify, or minus an error code (a TCP end without close_notify is -TLS_ERR_EOF).
1724note More than 1000 consecutive records without application bytes (empty records, tickets,
1725note KeyUpdates) are refused with -TLS_ERR_UNEXPECTED_MESSAGE.
1726fn tls_read c edit uchar * o edit uchar * to long
1727ph = get c TLPH
1728if eq ph 2
1729ret 0
1730end
1731if ne ph 1
1732ret neg long TLESTAT
1733end
1734if eq len o 0
1735ret 0
1736end
1737k = ulong 0
1738while true bound 999999
1739k = add k 1
1740if gt k 1001
1741ret neg long tls_abort c TLEUNEX
1742end
1743if gt tls_gn c TLPN 4 0
1744ret long tls_take c o
1745end
1746e = tls_rdrec c
1747if eq e 0
1748e = tls_rdapp c
1749end
1750if eq e TLCLOSE
1751put c TLPH 2
1752ret 0
1753end
1754if ne e 0
1755ret neg long tls_abort c e
1756end
1757end
1758ret neg long tls_abort c TLEUNEX
1759end
1760
1761note dispatch one received record after the handshake
1762fn tls_rdapp c edit uchar * to ulong
1763ty = ulong get c TLRB
1764if eq ty 23
1765tls_pn c TLPO 0 4
1766tls_pn c TLPN tls_gn c add TLRB 3 2 4
1767ret 0
1768end
1769if eq ty 21
1770ret tls_ralert c
1771end
1772if eq ty 22
1773ret tls_post c
1774end
1775ret TLEUNEX
1776end
1777
1778note tls_write: send all of m as application data; returns len m or minus an error code
1779fn tls_write c edit uchar * m view uchar * to long
1780if ne get c TLPH 1
1781ret neg long TLESTAT
1782end
1783off = ulong 0
1784while lt off len m bound 999999
1785k = sub len m off
1786if gt k 16384
1787k = 16384
1788end
1789for i ulong k
1790put c add add TLWB 5 i get m add off i
1791end
1792e = tls_wrbuf c 23 k
1793if ne e 0
1794ret neg long tls_abort c e
1795end
1796off = add off k
1797end
1798ret long len m
1799end
1800
1801note tls_close: send close_notify (when connected) and wipe the key material. The caller closes fd.
1802note A failed connection stays failed (later calls give TLS_ERR_STATE).
1803fn tls_close c edit uchar * to void
1804if eq get c TLPH 1
1805put c add TLWB 5 1
1806put c add TLWB 6 0
1807e = tls_wrbuf c 21 2
1808end
1809if ne get c TLPH 3
1810put c TLPH 2
1811end
1812tls_zero c TLWKEY 96
1813tls_zero c TLCAP 64
1814tls_zero c TLEPK 320
1815end
1816
1817note ---- DNS A lookup (RFC 1035) over UDP to the first IPv4 nameserver in /etc/resolv.conf ----
1818note Only A records (IPv4). The answer section is walked in order: a CNAME record owned by the
1819note current name (first the question name) makes its target the current name, and the first
1820note IN A record owned by the current name is used; records for any other owner are skipped.
1821note Owner names are compared label by label, ASCII case-insensitive, following compression.
1822note Response checks: id, QR set, opcode 0, TC clear, RCODE 0, one question equal to ours.
1823
1824note parse a dotted IPv4 address (all of b) into ip[0..4); 1 on success
1825fn dns_ip4 b view uchar * ip edit uchar * to ulong
1826k = ulong 0
1827v = ulong 0
1828dg = ulong 0
1829for p ulong len b
1830ch = ulong get b p
1831if and ge ch 48 le ch 57
1832v = add mul v 10 sub ch 48
1833dg = add dg 1
1834if or gt v 255 gt dg 3
1835ret 0
1836end
1837else
1838if or ne ch 46 or eq dg 0 ge k 3
1839ret 0
1840end
1841put ip k uchar v
1842k = add k 1
1843v = 0
1844dg = 0
1845end
1846end
1847if or ne k 3 eq dg 0
1848ret 0
1849end
1850put ip 3 uchar v
1851ret 1
1852end
1853
1854note first "nameserver a.b.c.d" line of /etc/resolv.conf into ip; 0 or TLEDNS
1855fn dns_conf ip edit uchar * to ulong
1856b = new uchar 4096 0
1857fd = fopen TLRESOLV 0 len TLRESOLV 524288 0
1858if lt fd 0
1859ret TLEDNS
1860end
1861n = read int fd b 0 4096
1862x = fclose int fd
1863if le n 0
1864ret TLEDNS
1865end
1866ret dns_lines part b 0 ulong n ip
1867end
1868
1869note try each line of b (split at LF) as a nameserver line
1870fn dns_lines b view uchar * ip edit uchar * to ulong
1871s = ulong 0
1872for e ulong add len b 1
1873if or eq e len b eq tls_at b e 10
1874if eq dns_nsline part b s sub e s ip 1
1875ret 0
1876end
1877s = add e 1
1878end
1879end
1880ret TLEDNS
1881end
1882
1883note b[i] as a ulong, or 256 past the end (never traps)
1884fn tls_at b view uchar * i ulong to ulong
1885if ge i len b
1886ret 256
1887end
1888ret ulong get b i
1889end
1890
1891note 1 when the line l is "nameserver", blanks, an IPv4 address, optional trailing blanks
1892fn dns_nsline l view uchar * ip edit uchar * to ulong
1893if lt len l 12
1894ret 0
1895end
1896if ne tls_cteq TLNS l 0 10 0
1897ret 0
1898end
1899p = ulong 10
1900if eq dns_blank tls_at l p 0
1901ret 0
1902end
1903while eq dns_blank tls_at l p 1 bound 4096
1904p = add p 1
1905end
1906q = p
1907while lt tls_at l q 256 bound 4096
1908if eq dns_blank tls_at l q 1
1909break
1910end
1911q = add q 1
1912end
1913ret dns_ip4 part l p sub q p ip
1914end
1915
1916note 1 for space, tab or CR
1917fn dns_blank ch ulong to ulong
1918if or eq ch 32 or eq ch 9 eq ch 13
1919ret 1
1920end
1921ret 0
1922end
1923
1924note query for hn type A at q[0..); returns its length (0 when hn is not a valid name)
1925fn dns_query q edit uchar * hn view uchar * id ulong to ulong
1926tls_pn q 0 id 2
1927tls_pn q 2 256 2
1928tls_pn q 4 1 2
1929tls_zero q 6 6
1930hl = len hn
1931put q 12 46
1932tls_cpy q 13 hn
1933if eq dns_labels part q 12 add hl 1 0
1934ret 0
1935end
1936p = add 13 hl
1937put q p 0
1938tls_pn q add p 1 1 2
1939tls_pn q add p 3 1 2
1940ret add p 5
1941end
1942
1943note "." + name in b becomes length-prefixed labels, back to front; 1 when every label is 1..63
1944fn dns_labels b edit uchar * to ulong
1945n = ulong 0
1946for i ulong len b
1947k = sub sub len b 1 i
1948if eq get b k 46
1949if or eq n 0 gt n 63
1950ret 0
1951end
1952put b k uchar n
1953n = 0
1954else
1955n = add n 1
1956end
1957end
1958ret 1
1959end
1960
1961note skip a (possibly compressed) name at r[p..z); returns the offset after it, or 0 when it is
1962note malformed or has more than 127 labels (never traps)
1963fn dns_skip r view uchar * p0 ulong z ulong to ulong
1964p = p0
1965k = ulong 0
1966while lt p z bound 128
1967k = add k 1
1968if gt k 127
1969ret 0
1970end
1971l = ulong get r p
1972if eq l 0
1973ret add p 1
1974end
1975if eq and l 192 192
1976if gt add p 2 z
1977ret 0
1978end
1979ret add p 2
1980end
1981if ne and l 192 0
1982ret 0
1983end
1984p = add add p 1 l
1985end
1986ret 0
1987end
1988
1989note answer check and first A record into ip; q is our whole query; 0 or TLEDNS
1990fn dns_parse r view uchar * q view uchar * ip edit uchar * to ulong
1991ql = len q
1992if lt len r ql
1993ret TLEDNS
1994end
1995if ne tls_cteq q r 0 2 0
1996ret TLEDNS
1997end
1998note flags: QR 0x8000 set; opcode 0x7800, TC 0x0200 and RCODE 0x000F clear (mask 0xFA0F)
1999fl = tls_gn r 2 2
2000if ne and fl 64015 32768
2001ret TLEDNS
2002end
2003if ne tls_gn r 4 2 1
2004ret TLEDNS
2005end
2006if ne dns_qeq q r ql 0
2007ret TLEDNS
2008end
2009an = tls_gn r 6 2
2010ret dns_ans r ql an ip
2011end
2012
2013note 0 when the question of r equals ours (ASCII case-insensitive), q[12..ql)
2014fn dns_qeq q view uchar * r view uchar * ql ulong to ulong
2015for i ulong sub ql 12
2016a = ulong get q add 12 i
2017b = ulong get r add 12 i
2018if and ge a 65 le a 90
2019a = add a 32
2020end
2021if and ge b 65 le b 90
2022b = add b 32
2023end
2024if ne a b
2025ret 1
2026end
2027end
2028ret 0
2029end
2030
2031note answer records from r[p0]; the current name starts as the question name at offset 12
2032fn dns_ans r view uchar * p0 ulong an ulong ip edit uchar * to ulong
2033p = p0
2034cur = ulong 12
2035for k ulong an
2036o = p
2037p = dns_rr r o
2038if eq p 0
2039ret TLEDNS
2040end
2041if eq dns_neq r o cur 0
2042t = dns_rrty r o
2043if eq t 1
2044dns_rrip r o ip
2045ret 0
2046end
2047if eq t 5
2048cur = add dns_skip r o len r 10
2049end
2050end
2051end
2052ret TLEDNS
2053end
2054
2055note one resource record at r[p0]: the offset of the next record, or 0 when it is malformed
2056fn dns_rr r view uchar * p0 ulong to ulong
2057z = len r
2058p = dns_skip r p0 z
2059if or eq p 0 gt add p 10 z
2060ret 0
2061end
2062dl = tls_gn r add p 8 2
2063if gt add add p 10 dl z
2064ret 0
2065end
2066ret add add p 10 dl
2067end
2068
2069note copy the 4 data bytes of the (well-formed) A record at r[o] into ip
2070fn dns_rrip r view uchar * o ulong ip edit uchar * to void
2071p = add dns_skip r o len r 10
2072for i ulong 4
2073put ip i get r add p i
2074end
2075end
2076
2077note kind of the (well-formed) record at r[o]: 1 IN A with 4 data bytes, 5 IN CNAME, else 0
2078fn dns_rrty r view uchar * o ulong to ulong
2079p = dns_skip r o len r
2080ty = tls_gn r p 4
2081dl = tls_gn r add p 8 2
2082if and eq ty 65537 eq dl 4
2083ret 1
2084end
2085if eq ty 327681
2086ret 5
2087end
2088ret 0
2089end
2090
2091note the target of a compression pointer at r[a]; a itself when r[a] is no pointer; len r when
2092note the pointer is cut off
2093fn dns_ptr r view uchar * a ulong to ulong
2094x = tls_at r a
2095if ne and x 192 192
2096ret a
2097end
2098if ge add a 1 len r
2099ret len r
2100end
2101ret or shl and x 63 8 ulong get r add a 1
2102end
2103
2104note 0 when the names at r[a0] and r[b0] are equal (ASCII case-insensitive, compression
2105note followed); 1 when they differ or either is malformed (at most 400 steps, never traps)
2106fn dns_neq r view uchar * a0 ulong b0 ulong to ulong
2107a = a0
2108b = b0
2109for k ulong 400
2110na = dns_ptr r a
2111nb = dns_ptr r b
2112if or ne na a ne nb b
2113a = na
2114b = nb
2115else
2116la = tls_at r a
2117if or ne la tls_at r b gt la 63
2118ret 1
2119end
2120if eq la 0
2121ret 0
2122end
2123if ne dns_lbeq r a b la 0
2124ret 1
2125end
2126a = add a add la 1
2127b = add b add la 1
2128end
2129end
2130ret 1
2131end
2132
2133note 0 when the l label bytes after r[a] and r[b] are equal (ASCII case-insensitive, in range)
2134fn dns_lbeq r view uchar * a ulong b ulong l ulong to ulong
2135for i ulong l
2136x = dns_low tls_at r add add a 1 i
2137y = dns_low tls_at r add add b 1 i
2138if or ne x y eq x 256
2139ret 1
2140end
2141end
2142ret 0
2143end
2144
2145fn dns_low x ulong to ulong
2146if and ge x 65 le x 90
2147ret add x 32
2148end
2149ret x
2150end
2151
2152note sockaddr_in for ip:port into sa[0..16)
2153fn tcp_sa sa edit uchar * ip view uchar * port ulong to void
2154tls_zero sa 0 16
2155put sa 0 2
2156tls_pn sa 2 port 2
2157for i ulong 4
2158put sa add 4 i get ip i
2159end
2160end
2161
2162note resolve hn to an IPv4 address in ip[0..4) (two attempts, 3 s each); 0 or TLEDNS. A name
2163note longer than 253 bytes (RFC 1035 2.3.4) is TLEDNS before the 300-byte query is built.
2164fn dns_a4 hn view uchar * ip edit uchar * to ulong
2165if gt len hn 253
2166ret TLEDNS
2167end
2168ns = new uchar 4 0
2169if ne dns_conf ns 0
2170ret TLEDNS
2171end
2172q = new uchar 300 0
2173idb = new uchar 2 0
2174if ne random idb 0 2 0 2
2175ret TLERAND
2176end
2177ql = dns_query q hn tls_gn idb 0 2
2178if eq ql 0
2179ret TLEDNS
2180end
2181for t ulong 2
2182if eq dns_try ns part q 0 ql ip 0
2183ret 0
2184end
2185end
2186ret TLEDNS
2187end
2188
2189fn dns_try ns view uchar * q view uchar * ip edit uchar * to ulong
2190sa = new uchar 16 0
2191tcp_sa sa ns 53
2192fd = socknew 2 524290 0
2193if lt fd 0
2194ret TLEDNS
2195end
2196f = int fd
2197e = ulong TLEDNS
2198if eq connect f sa 0 16 0
2199e = dns_xchg f q ip
2200end
2201x = fclose f
2202ret e
2203end
2204
2205note send the query on the connected UDP socket f and parse one reply (3 s limit)
2206fn dns_xchg f int q view uchar * ip edit uchar * to ulong
2207r = new uchar 4096 0
2208if ne send f q 0 len q 0 long len q
2209ret TLEDNS
2210end
2211w = waitio f 1 3000000000
2212if le w 0
2213ret TLEDNS
2214end
2215n = recv f r 0 4096 0
2216if le n 0
2217ret TLEDNS
2218end
2219ret dns_parse part r 0 ulong n q ip
2220end
2221
2222note TCP connect to ip:port, non-blocking with a 10 s limit; returns fd (>= 0) or minus a code
2223fn tcp_open4 ip view uchar * port ulong to long
2224sa = new uchar 16 0
2225tcp_sa sa ip port
2226fd = socknew 2 526337 0
2227if lt fd 0
2228ret neg long TLECONN
2229end
2230f = int fd
2231r = connect f sa 0 16
2232if eq r -115
2233w = waitio f 4 10000000000
2234if le w 0
2235x = fclose f
2236ret neg long TLECONN
2237end
2238r = connect f sa 0 16
2239if eq r -106
2240r = 0
2241end
2242end
2243if ne r 0
2244x = fclose f
2245ret neg long TLECONN
2246end
2247ret fd
2248end
2249
2250note tls_dial: DNS A lookup of hn and a TCP connection to port; fd or minus a code
2251note (a dotted IPv4 literal is used as it is, without DNS; the TLS name is set separately)
2252fn tls_dial hn view uchar * port ulong to long
2253ip = new uchar 4 0
2254if eq dns_ip4 hn ip 0
2255e = dns_a4 hn ip
2256if ne e 0
2257ret neg long e
2258end
2259end
2260ret tcp_open4 ip port
2261end