interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / om / tls / tls.om

3424 lines · 72546 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, ecdh.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, one HelloRetryRequest (RFC 8446 4.1.4) accepted and
9note checked; SNI always; ALPN http/1.1 when flag 1 is set.
10note Approved-only mode (flag 4): TLS_AES_128_GCM_SHA256 only, groups secp256r1 and secp384r1 only
11note (ecdh.om, a share for each), signature schemes ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384
12note and rsa_pss_rsae_* only; a ServerHello choosing ChaCha20-Poly1305 is TLS_ERR_CIPHER_SUITE and
13note one choosing x25519 is TLS_ERR_KEY_SHARE. See README.md "Approved-only mode".
14note Server authentication is mandatory: x509_chain_verify (chain, dates, name, constraints) then
15note x509_cv_verify (CertificateVerify) then the Finished HMAC. Any failure: a fatal alert is sent
16note (best effort) and a named error is returned; the connection is unusable afterwards.
17note
18note Connection state c is one caller-owned uchar array of TLCSZ bytes; the trust store st is a
19note separate array filled once by tls_store (x509_store_load). Integers in c are big-endian.
20
21const TLCSZ ulong 121696
22const TLSTSZ ulong 262144
23const TLPH ulong 0
24const TLSU ulong 1
25const TLFL ulong 2
26const TLHL ulong 3
27const TLRK ulong 5
28const TLWK ulong 6
29const TLCR ulong 7
30const TLFD ulong 8
31const TLDL ulong 16
32const TLRS ulong 24
33const TLWS ulong 32
34const TLWKEY ulong 64
35const TLWIV ulong 96
36const TLRKEY ulong 112
37const TLRIV ulong 144
38const TLCAP ulong 160
39const TLSAP ulong 192
40const TLHOST ulong 224
41const TLPO ulong 480
42const TLPN ulong 484
43const TLHF ulong 488
44const TLDET ulong 492
45note change_cipher_spec state: 0 none allowed yet, 1 one allowed (after ServerHello), 2 no more
46const TLCCS ulong 496
47note record limit override (4 bytes, 0 = TLKUREC) and handshake budget in ns (8 bytes, 0 = TLHSNS)
48const TLKUL ulong 500
49const TLHSL ulong 504
50const TLEPK ulong 512
51const TLEPU ulong 544
52const TLSID ulong 576
53const TLHS ulong 608
54const TLCHS ulong 640
55const TLSHS ulong 672
56const TLMS ulong 704
57note c[736..768) is unused since the shared secret grew to 64 bytes (TLSS below)
58const TLCRX ulong 832
59note approved mode (flag 4), c[1088..1536): secp256r1 private (32) and public (65), secp384r1
60note private (48) and public (97), the server's point (up to 97), the shared secret (up to 48) and
61note the group the server chose
62const TLECD2 ulong 1088
63const TLECD3 ulong 1120
64const TLECQ2 ulong 1168
65const TLECQ3 ulong 1233
66const TLECP ulong 1330
67const TLECZ ulong 1427
68const TLGRP ulong 1475
69const TLRB ulong 2048
70const TLWB ulong 19456
71const TLLF ulong 36864
72const TLLFSZ ulong 16896
73const TLHB ulong 53760
74const TLHBSZ ulong 65536
75note ML-KEM-768 decapsulation key (2400 bytes, secret) of the hybrid share, wiped after use
76const TLKDK ulong 119296
77note handshake state for groups and HelloRetryRequest (RFC 8446 4.1.4). Group bits: TLGHYB =
78note X25519MLKEM768 (0x11EC), TLGX25 = x25519; 4 and 8 are reserved for secp256r1 / secp384r1.
79note c[TLGO] offered groups, c[TLGS] groups with a share in the current ClientHello, c[TLHRS] 1
80note after a HelloRetryRequest, c[TLHSU] its suite (TLSU value), c[TLGSEL] the group bit of the
81note ServerHello share, c[TLKGEN] 1 while an ML-KEM key pair is held at c[TLKDK].
82note c[1088..1792) belongs to the approved-mode ECDH and DRBG state (other units).
83const TLKGEN ulong 4
84const TLGO ulong 12
85const TLGS ulong 13
86const TLHRS ulong 14
87const TLGSEL ulong 15
88const TLGHYB ulong 1
89const TLGX25 ulong 2
90const TLGOFF ulong 3
91note shared secret (EC)DHE input: 32 bytes for x25519, 64 (ML-KEM || x25519) for the hybrid
92const TLSS ulong 1792
93note client random (kept for ClientHello2), server share offset in c[TLHB], cookie place/length
94const TLRND ulong 1856
95const TLKSO ulong 1888
96const TLCKO ulong 1892
97const TLCKL ulong 1896
98const TLHSU ulong 1898
99note RFC 8446 section 5.5: at most 2^24.5 = 23726566 records under one AES-GCM key (applied to both
100note suites); the whole handshake must finish within TLHSNS (30 s) unless the caller set a deadline
101const TLKUREC ulong 23726566
102const TLHSNS ulong 30000000000
103
104note error codes (tls_errname gives the names)
105const TLEARG ulong 1
106const TLEIO ulong 2
107const TLETIME ulong 3
108const TLEEOF ulong 4
109const TLEREC ulong 5
110const TLEMAC ulong 6
111const TLEUNEX ulong 7
112const TLEDEC ulong 8
113const TLEVER ulong 9
114const TLECIPH ulong 10
115const TLEKEYS ulong 11
116const TLEEXT ulong 12
117const TLEHRR ulong 13
118const TLESID ulong 14
119const TLEZERO ulong 15
120const TLEALPN ulong 16
121const TLECERT ulong 17
122const TLESCH ulong 18
123const TLECV ulong 19
124const TLEFIN ulong 20
125const TLEALRT ulong 21
126const TLEBIG ulong 22
127const TLESTAT ulong 23
128const TLERAND ulong 24
129const TLEDNS ulong 25
130const TLECONN ulong 26
131const TLEKUPD ulong 27
132const TLESEQ ulong 28
133const TLESTOR ulong 29
134const TLETKT ulong 30
135const TLEILL ulong 31
136note TLESELF was 30 on #855 (audit/tls-fips-approved); renumbered to 32 at the integration base
137const TLESELF ulong 32
138note approved mode (flag 4): HMAC_DRBG state (fips.om, DRSZ bytes) at c[TLDRBG]
139const TLDRBG ulong 1536
140
141text TLN00 "TLS_OK"
142text TLN01 "TLS_ERR_ARGUMENT"
143text TLN02 "TLS_ERR_IO"
144text TLN03 "TLS_ERR_TIMEOUT"
145text TLN04 "TLS_ERR_EOF"
146text TLN05 "TLS_ERR_RECORD"
147text TLN06 "TLS_ERR_BAD_RECORD_MAC"
148text TLN07 "TLS_ERR_UNEXPECTED_MESSAGE"
149text TLN08 "TLS_ERR_DECODE"
150text TLN09 "TLS_ERR_PROTOCOL_VERSION"
151text TLN10 "TLS_ERR_CIPHER_SUITE"
152text TLN11 "TLS_ERR_KEY_SHARE"
153text TLN12 "TLS_ERR_EXTENSION"
154text TLN13 "TLS_ERR_HELLO_RETRY"
155text TLN14 "TLS_ERR_SESSION_ID_ECHO"
156text TLN15 "TLS_ERR_ZERO_SHARED_SECRET"
157text TLN16 "TLS_ERR_ALPN"
158text TLN17 "TLS_ERR_CERTIFICATE"
159text TLN18 "TLS_ERR_SIGNATURE_SCHEME"
160text TLN19 "TLS_ERR_CERTIFICATE_VERIFY"
161text TLN20 "TLS_ERR_FINISHED"
162text TLN21 "TLS_ERR_ALERT_RECEIVED"
163text TLN22 "TLS_ERR_MESSAGE_TOO_BIG"
164text TLN23 "TLS_ERR_STATE"
165text TLN24 "TLS_ERR_RANDOM"
166text TLN25 "TLS_ERR_DNS"
167text TLN26 "TLS_ERR_CONNECT"
168text TLN27 "TLS_ERR_KEY_UPDATE"
169text TLN28 "TLS_ERR_SEQUENCE_LIMIT"
170text TLN29 "TLS_ERR_TRUST_STORE"
171text TLN30 "TLS_ERR_TICKET"
172text TLN31 "TLS_ERR_ILLEGAL_PARAMETER"
173text TLN32 "TLS_ERR_SELFTEST"
174text TLNUNK "TLS_ERR_UNKNOWN"
175
176note HKDF labels (RFC 8446 section 7.1, 7.2, 7.3); "tls13 " is prepended by tls_info
177text TLL13 "tls13 "
178text TLLKEY "key"
179text TLLIV "iv"
180text TLLFIN "finished"
181text TLLDER "derived"
182text TLLCHS "c hs traffic"
183text TLLSHS "s hs traffic"
184text TLLCAP "c ap traffic"
185text TLLSAP "s ap traffic"
186text TLLUPD "traffic upd"
187text TLCVC "TLS 1.3, server CertificateVerify"
188text TLALPN "http/1.1"
189text TLRESOLV "/etc/resolv.conf"
190text TLAUXV "/proc/self/auxv"
191text TLNS "nameserver"
192
193note ---- byte helpers (big-endian, n <= 8) ----
194fn tls_gn b view uchar * o ulong n ulong to ulong
195v = ulong 0
196for i ulong n
197v = or shl v 8 ulong get b add o i
198end
199ret v
200end
201
202fn tls_pn b edit uchar * o ulong v ulong n ulong to void
203x = v
204for i ulong n
205put b sub add o sub n 1 i uchar and x 255
206x = shr x 8
207end
208end
209
210fn tls_cpy d edit uchar * o ulong s view uchar * to void
211for i ulong len s
212put d add o i get s i
213end
214end
215
216note move n bytes inside one array from s to d (d <= s or non-overlapping)
217fn tls_mov b edit uchar * d ulong s ulong n ulong to void
218for i ulong n
219put b add d i get b add s i
220end
221end
222
223fn tls_zero b edit uchar * o ulong n ulong to void
224for i ulong n
225put b add o i 0
226end
227end
228
229note constant-time equality of a[0..n) and b[o..o+n): 0 when equal, 1 otherwise
230fn tls_cteq a view uchar * b view uchar * o ulong n ulong to ulong
231d = ulong 0
232for i ulong n
233d = or d ulong xor get a i get b add o i
234end
235ret shr add d 255 8
236end
237
238note ---- error bookkeeping ----
239fn tls_fail c edit uchar * e ulong dt ulong to ulong
240put c TLPH 3
241tls_pn c TLDET dt 4
242ret e
243end
244
245fn tls_detail c view uchar * to ulong
246ret tls_gn c TLDET 4
247end
248
249fn tls_errname e ulong o edit uchar * to ulong
250n = ulong 0
251if eq e 0
252n = tls_namecp o TLN00
253end
254if eq e 1
255n = tls_namecp o TLN01
256end
257if eq e 2
258n = tls_namecp o TLN02
259end
260if eq e 3
261n = tls_namecp o TLN03
262end
263if eq e 4
264n = tls_namecp o TLN04
265end
266if eq e 5
267n = tls_namecp o TLN05
268end
269if eq e 6
270n = tls_namecp o TLN06
271end
272if eq e 7
273n = tls_namecp o TLN07
274end
275if eq e 8
276n = tls_namecp o TLN08
277end
278if eq e 9
279n = tls_namecp o TLN09
280end
281if eq e 10
282n = tls_namecp o TLN10
283end
284if eq e 11
285n = tls_namecp o TLN11
286end
287if eq e 12
288n = tls_namecp o TLN12
289end
290if eq e 13
291n = tls_namecp o TLN13
292end
293if eq e 14
294n = tls_namecp o TLN14
295end
296if eq e 15
297n = tls_namecp o TLN15
298end
299ret tls_errnm2 e o n
300end
301
302fn tls_errnm2 e ulong o edit uchar * n0 ulong to ulong
303n = n0
304if eq e 16
305n = tls_namecp o TLN16
306end
307if eq e 17
308n = tls_namecp o TLN17
309end
310if eq e 18
311n = tls_namecp o TLN18
312end
313if eq e 19
314n = tls_namecp o TLN19
315end
316if eq e 20
317n = tls_namecp o TLN20
318end
319if eq e 21
320n = tls_namecp o TLN21
321end
322if eq e 22
323n = tls_namecp o TLN22
324end
325if eq e 23
326n = tls_namecp o TLN23
327end
328if eq e 24
329n = tls_namecp o TLN24
330end
331if eq e 25
332n = tls_namecp o TLN25
333end
334if eq e 26
335n = tls_namecp o TLN26
336end
337if eq e 27
338n = tls_namecp o TLN27
339end
340if eq e 28
341n = tls_namecp o TLN28
342end
343if eq e 29
344n = tls_namecp o TLN29
345end
346if eq e 30
347n = tls_namecp o TLN30
348end
349if eq e 31
350n = tls_namecp o TLN31
351end
352if eq e 32
353n = tls_namecp o TLN32
354end
355if eq n 0
356n = tls_namecp o TLNUNK
357end
358ret n
359end
360
361fn tls_namecp o edit uchar * t view uchar * to ulong
362n = len t
363if gt n len o
364n = len o
365end
366for i ulong n
367put o i get t i
368end
369ret n
370end
371
372note internal: a received close_notify alert (tls_read turns it into a clean end of data)
373const TLCLOSE ulong 100
374
375note ---- socket I/O with deadline (c holds the fd and an absolute monotonic deadline, 0 = none;
376note with no deadline every wait is bounded by 30 s of silence) ----
377fn tls_fd c view uchar * to int
378ret int tls_gn c TLFD 4
379end
380
381fn tls_deadline c edit uchar * dl long to void
382if lt dl 0
383tls_pn c TLDL 0 8
384ret
385end
386tls_pn c TLDL ulong dl 8
387end
388
389note wait for ev (1 readable, 4 writable): 0 ready, else an error code
390fn tls_wait c view uchar * ev int to ulong
391dl = tls_gn c TLDL 8
392iv = long 30000000000
393if ne dl 0
394now = monons
395if ge now long dl
396ret TLETIME
397end
398iv = sub long dl now
399end
400r = waitio tls_fd c ev iv
401if eq r 0
402ret TLETIME
403end
404if lt r 0
405if eq r -4
406ret 0
407end
408ret TLEIO
409end
410ret 0
411end
412
413note read exactly n bytes into c[o..o+n)
414fn tls_rdn c edit uchar * o ulong n ulong to ulong
415fd = tls_fd c
416got = ulong 0
417while lt got n bound 999999
418e = tls_wait c 1
419if ne e 0
420ret e
421end
422r = recv fd c add o got sub n got 0
423if eq r 0
424ret TLEEOF
425end
426if lt r 0
427if and ne r -11 ne r -4
428ret TLEIO
429end
430else
431got = add got ulong r
432end
433end
434ret 0
435end
436
437note send c[o..o+n) completely
438fn tls_sendall c edit uchar * o ulong n ulong to ulong
439fd = tls_fd c
440done = ulong 0
441while lt done n bound 999999
442e = tls_wait c 4
443if ne e 0
444ret e
445end
446r = send fd c add o done sub n done 0
447if lt r 0
448if and ne r -11 ne r -4
449ret TLEIO
450end
451else
452done = add done ulong r
453end
454end
455ret 0
456end
457
458note ---- record protection (RFC 8446 section 5.2, 5.3) ----
459note per-record nonce: the 12-byte iv at c[ivo] XOR the 64-bit sequence number at c[sqo],
460note left-padded; key at c[ko]; written as key || nonce into kn
461fn tls_kn c view uchar * kn edit uchar * sel ulong to ulong
462kl = ulong 16
463if eq get c TLSU 1
464kl = 32
465end
466ko = and sel 65535
467for i ulong kl
468put kn i get c add ko i
469end
470tls_nonce c kn or and shr sel 16 4294967295 shl kl 32
471ret add kl 12
472end
473
474note nonce bytes into kn[kl..kl+12): sel = ivo | sqo << 16 | kl << 32
475fn tls_nonce c view uchar * kn edit uchar * sel ulong to void
476kl = shr sel 32
477ivo = and sel 65535
478for i ulong 12
479put kn add kl i get c add ivo i
480end
481sq = tls_gn c and shr sel 16 65535 8
482for i ulong 8
483p = add kl add 4 i
484s = mul 8 sub 7 i
485b = and shr sq s 255
486put kn p uchar xor ulong get kn p b
487end
488end
489
490note bump the 64-bit sequence number at c[o]; 1 when it would wrap (the key must not be reused)
491fn tls_seqinc c edit uchar * o ulong to ulong
492sq = tls_gn c o 8
493if eq sq 18446744073709551615
494ret 1
495end
496tls_pn c o add sq 1 8
497ret 0
498end
499
500note the record limit in force: TLKUREC or the lower test-only value set by tls_reclimit
501fn tls_reclim c view uchar * to ulong
502v = tls_gn c TLKUL 4
503if eq v 0
504ret TLKUREC
505end
506ret v
507end
508
509note tls_reclimit: lower the per-key record limit before tls_connect (tests); it cannot be raised
510fn tls_reclimit c edit uchar * n ulong to ulong
511if or ne get c TLPH 0 or eq n 0 ge n TLKUREC
512ret TLEARG
513end
514tls_pn c TLKUL n 4
515ret 0
516end
517
518note tls_hslimit: the whole-handshake budget in ns used when no deadline is set (0 = TLHSNS)
519fn tls_hslimit c edit uchar * ns ulong to ulong
520if ne get c TLPH 0
521ret TLEARG
522end
523tls_pn c TLHSL ns 8
524ret 0
525end
526
527note decrypt the record at c[TLRB] whose body is ln bytes; 0, TLEMAC, or TLESEQ when the peer sent
528note more records under this key than the limit allows without a KeyUpdate (fail closed)
529fn tls_open c edit uchar * ln ulong to ulong
530kn = new uchar 44 0
531ad = new uchar 5 0
532if ge tls_gn c TLRS 8 tls_reclim c
533ret TLESEQ
534end
535if eq fips_bad 1
536ret TLESELF
537end
538for i ulong 5
539put ad i get c add TLRB i
540end
541kl = tls_kn c kn or TLRKEY or shl TLRIV 16 shl TLRS 32
542prev = dit 1
543r = ulong 1
544if eq get c TLSU 1
545r = aead_chacha_open kn ad part c add TLRB 5 ln
546else
547r = aead_aes128gcm_open part kn 0 kl ad part c add TLRB 5 ln
548end
549z = scrub 4096
550z = dit int prev
551tls_zero kn 0 44
552if ne r 0
553ret TLEMAC
554end
555if ne tls_seqinc c TLRS 0
556ret TLESEQ
557end
558ret 0
559end
560
561note encrypt c[TLWB+5 .. +n) (inner plaintext incl. type byte), header already at c[TLWB];
562note TLESEQ when the write key reached its record limit (tls_write rekeys before that)
563fn tls_seal c edit uchar * n ulong to ulong
564kn = new uchar 44 0
565ad = new uchar 5 0
566if ge tls_gn c TLWS 8 tls_reclim c
567ret TLESEQ
568end
569if eq fips_bad 1
570ret TLESELF
571end
572for i ulong 5
573put ad i get c add TLWB i
574end
575kl = tls_kn c kn or TLWKEY or shl TLWIV 16 shl TLWS 32
576prev = dit 1
577r = ulong 1
578if eq get c TLSU 1
579r = aead_chacha_seal kn ad part c add TLWB 5 add n 16
580else
581r = aead_aes128gcm_seal part kn 0 kl ad part c add TLWB 5 add n 16
582end
583z = scrub 4096
584z = dit int prev
585tls_zero kn 0 44
586if ne r 0
587ret TLEARG
588end
589if ne tls_seqinc c TLWS 0
590ret TLESEQ
591end
592ret 0
593end
594
595note send one record of content type ty whose n plaintext bytes are at c[TLWB+5]
596fn tls_wrbuf c edit uchar * ty ulong n ulong to ulong
597if gt n 16384
598ret TLEARG
599end
600if eq get c TLWK 0
601put c TLWB uchar ty
602tls_pn c add TLWB 1 771 2
603tls_pn c add TLWB 3 n 2
604ret tls_sendall c TLWB add n 5
605end
606put c add add TLWB 5 n uchar ty
607ln = add n 17
608put c TLWB 23
609tls_pn c add TLWB 1 771 2
610tls_pn c add TLWB 3 ln 2
611e = tls_seal c add n 1
612if ne e 0
613ret e
614end
615ret tls_sendall c TLWB add ln 5
616end
617
618note best-effort fatal alert (RFC 8446 section 6)
619fn tls_alert c edit uchar * desc ulong to void
620put c add TLWB 5 2
621put c add TLWB 6 uchar desc
622e = tls_wrbuf c 21 2
623put c TLPH 3
624end
625
626note read one record into c[TLRB]; on 0 the content type is at c[TLRB] and the plaintext length
627note at c[TLRB+3..+5), plaintext from c[TLRB+5]
628fn tls_rdrec c edit uchar * to ulong
629e = tls_rdn c TLRB 5
630if ne e 0
631ret e
632end
633ty = ulong get c TLRB
634ln = tls_gn c add TLRB 3 2
635if gt ln 16640
636ret TLEREC
637end
638e = tls_rdn c add TLRB 5 ln
639if ne e 0
640ret e
641end
642if eq ty 20
643if or ne ln 1 ne get c add TLRB 5 1
644ret TLEUNEX
645end
646if ne get c TLCCS 1
647ret TLEUNEX
648end
649put c TLCCS 2
650ret 0
651end
652if eq get c TLRK 0
653if and ne ty 22 ne ty 21
654ret TLEUNEX
655end
656if or eq ln 0 gt ln 16384
657ret TLEREC
658end
659ret 0
660end
661if ne ty 23
662ret TLEUNEX
663end
664put c TLCCS 2
665if lt ln 17
666ret TLEMAC
667end
668if gt ln 16401
669ret TLEREC
670end
671e = tls_open c ln
672if ne e 0
673ret e
674end
675n = sub ln 16
676while gt n 0 bound 20000
677if ne get c add add TLRB 4 n 0
678break
679end
680n = sub n 1
681end
682if eq n 0
683ret TLEUNEX
684end
685n = sub n 1
686put c TLRB get c add add TLRB 5 n
687tls_pn c add TLRB 3 n 2
688if eq get c TLRB 20
689ret TLEUNEX
690end
691ret 0
692end
693
694note a received alert record: close_notify gives TLCLOSE, any other alert TLEALRT (detail = desc)
695fn tls_ralert c edit uchar * to ulong
696if ne tls_gn c add TLRB 3 2 2
697ret TLEDEC
698end
699d = ulong get c add TLRB 6
700tls_pn c TLDET d 4
701if eq d 0
702ret TLCLOSE
703end
704ret TLEALRT
705end
706
707note ---- handshake message buffer (c[TLHB], fill count at c[TLHF]) ----
708note make sure one whole handshake message is at c[TLHB]; 0 or an error code. A message spread
709note over more than 4000 records is refused (TLEBIG); close_notify here is TLEALRT, detail 0
710fn tls_hsmsg c edit uchar * to ulong
711k = ulong 0
712while true bound 4096
713k = add k 1
714if gt k 4000
715ret TLEBIG
716end
717f = tls_gn c TLHF 4
718if ge f 4
719ml = add tls_gn c add TLHB 1 3 4
720if gt ml TLHBSZ
721ret TLEBIG
722end
723if ge f ml
724ret 0
725end
726end
727e = tls_rdrec c
728if ne e 0
729ret e
730end
731ty = ulong get c TLRB
732n = tls_gn c add TLRB 3 2
733if eq ty 21
734e = tls_ralert c
735if eq e TLCLOSE
736ret TLEALRT
737end
738ret e
739end
740if ne ty 20
741if ne ty 22
742ret TLEUNEX
743end
744if eq n 0
745ret TLEUNEX
746end
747if gt add f n TLHBSZ
748ret TLEBIG
749end
750tls_mov c add TLHB f add TLRB 5 n
751tls_pn c TLHF add f n 4
752end
753end
754ret TLEUNEX
755end
756
757note drop the message at c[TLHB] (after hashing it when h is 1)
758fn tls_hsdrop c edit uchar * to void
759f = tls_gn c TLHF 4
760ml = add tls_gn c add TLHB 1 3 4
761tls_mov c TLHB add TLHB ml sub f ml
762tls_pn c TLHF sub f ml 4
763end
764
765fn tls_hsdone c edit uchar * th edit ulong * to void
766ml = add tls_gn c add TLHB 1 3 4
767sha256_update th part c TLHB ml
768tls_hsdrop c
769end
770
771note ---- key schedule (RFC 8446 section 7.1), SHA-256 for both suites ----
772const TLSPUB ulong 800
773const TLTMP ulong 768
774note label ids for tls_drv
775const TLIKEY ulong 0
776const TLIIV ulong 1
777const TLIFIN ulong 2
778const TLIDER ulong 3
779const TLICHS ulong 4
780const TLISHS ulong 5
781const TLICAP ulong 6
782const TLISAP ulong 7
783const TLIUPD ulong 8
784
785fn tls_labcp p edit uchar * o ulong lid ulong to ulong
786t = ulong 0
787if eq lid TLIKEY
788tls_cpy p o TLLKEY
789t = len TLLKEY
790end
791if eq lid TLIIV
792tls_cpy p o TLLIV
793t = len TLLIV
794end
795if eq lid TLIFIN
796tls_cpy p o TLLFIN
797t = len TLLFIN
798end
799if eq lid TLIDER
800tls_cpy p o TLLDER
801t = len TLLDER
802end
803if eq lid TLICHS
804tls_cpy p o TLLCHS
805t = len TLLCHS
806end
807if eq lid TLISHS
808tls_cpy p o TLLSHS
809t = len TLLSHS
810end
811if eq lid TLICAP
812tls_cpy p o TLLCAP
813t = len TLLCAP
814end
815if eq lid TLISAP
816tls_cpy p o TLLSAP
817t = len TLLSAP
818end
819if eq lid TLIUPD
820tls_cpy p o TLLUPD
821t = len TLLUPD
822end
823ret t
824end
825
826note HkdfLabel after the 32-byte secret in p: uint16 ln, opaque label<7..255> = "tls13 " + label,
827note opaque context<0..255> = hv. Returns 32 + the HkdfLabel length.
828fn tls_info p edit uchar * lid ulong hv view uchar * ln ulong to ulong
829tls_pn p 32 ln 2
830tls_cpy p 35 TLL13
831t = tls_labcp p 41 lid
832put p 34 uchar add t 6
833o = add 41 t
834put p o uchar len hv
835tls_cpy p add o 1 hv
836ret add add o 1 len hv
837end
838
839note c[dst..dst+ln) = HKDF-Expand-Label(c[src..src+32), label lid, hv, ln);
840note sel = dst | src << 16 | ln << 32 | lid << 48
841fn tls_drv c edit uchar * sel ulong hv view uchar * to ulong
842p = new uchar 160 0
843k = new uchar 32 0
844src = and shr sel 16 65535
845for i ulong 32
846put p i get c add src i
847end
848n = tls_info p shr sel 48 hv and shr sel 32 65535
849r = tls_hexp p n k
850tls_drvout c k sel
851tls_zero p 0 160
852tls_zero k 0 32
853ret r
854end
855
856note HKDF-Expand of p[32..n) under the PRK p[0..32) into all 32 bytes of k; a shorter label
857note length L takes the first L bytes (T(1) does not depend on the output length)
858fn tls_hexp p view uchar * n ulong k edit uchar * to ulong
859ret hkdf_expand 256 part p 0 n 32 k
860end
861
862fn tls_drvout c edit uchar * k view uchar * sel ulong to void
863dst = and sel 65535
864for i ulong and shr sel 32 65535
865put c add dst i get k i
866end
867end
868
869fn tls_sel dst ulong src ulong ln ulong lid ulong to ulong
870ret or or dst shl src 16 or shl ln 32 shl lid 48
871end
872
873note c[dst..+32) = HKDF-Extract(salt, ikm); sel = dst | salt << 16 | ikm << 32 | ikm length << 48
874note (length 0 means 32, at most 64), where offset 0 stands for 32 zero bytes (RFC 8446 "0" input)
875fn tls_xtr c edit uchar * sel ulong to ulong
876s = new uchar 96 0
877k = new uchar 32 0
878so = and shr sel 16 65535
879io = and shr sel 32 65535
880il = shr sel 48
881if eq il 0
882il = 32
883end
884if ne so 0
885for i ulong 32
886put s i get c add so i
887end
888end
889if ne io 0
890for i ulong il
891put s add 32 i get c add io i
892end
893end
894r = hkdf_extract 256 part s 0 add 32 il 32 k
895dst = and sel 65535
896for i ulong 32
897put c add dst i get k i
898end
899tls_zero s 0 96
900tls_zero k 0 32
901if ne r 32
902ret 1
903end
904ret 0
905end
906
907note traffic key and iv from the secret at c[so]; d 0 installs the write side, 1 the read side
908fn tls_keys c edit uchar * so ulong d ulong to ulong
909e = new uchar 1 0
910kl = ulong 16
911if eq get c TLSU 1
912kl = 32
913end
914ko = TLWKEY
915io = TLWIV
916sq = TLWS
917if eq d 1
918ko = TLRKEY
919io = TLRIV
920sq = TLRS
921end
922r = tls_drv c tls_sel ko so kl TLIKEY part e 0 0
923r = or r tls_drv c tls_sel io so 12 TLIIV part e 0 0
924tls_pn c sq 0 8
925ret r
926end
927
928note transcript hash so far into o (32 bytes), leaving th running
929fn tls_th th view ulong * o edit uchar * to void
930t = new ulong 40 0
931sha2_copy t th
932sha256_final t o
933for i ulong 40
934put t i 0
935end
936end
937
938note SHA-256 of the empty string (the Derive-Secret context for "derived")
939fn tls_h0 o edit uchar * to void
940e = new uchar 1 0
941n = sha2_digest 256 part e 0 0 o
942end
943
944note 1 in approved-only mode (tls_init flag 4), else 0
945fn tls_apm c view uchar * to ulong
946if eq and get c TLFL 4 0
947ret 0
948end
949ret 1
950end
951
952note cipher_suites and legacy_compression_methods at c[p]; returns the next offset. Approved
953note mode offers TLS_AES_128_GCM_SHA256 (SP 800-38D, FIPS 197) alone.
954fn tls_suites c edit uchar * p ulong to ulong
955if eq tls_apm c 1
956tls_pn c p 2 2
957tls_pn c add p 2 4865 2
958put c add p 4 1
959put c add p 5 0
960ret add p 6
961end
962tls_pn c p 4 2
963tls_pn c add p 2 4867 2
964tls_pn c add p 4 4865 2
965put c add p 6 1
966put c add p 7 0
967ret add p 8
968end
969
970note approved mode: supported_groups secp256r1, secp384r1 and signature_algorithms
971note ecdsa_secp256r1_sha256, ecdsa_secp384r1_sha384, rsa_pss_rsae_sha256/384/512 at c[q]
972fn tls_apgs c edit uchar * q ulong to ulong
973tls_pn c q 10 2
974tls_pn c add q 2 6 2
975tls_pn c add q 4 4 2
976tls_pn c add q 6 23 2
977tls_pn c add q 8 24 2
978tls_pn c add q 10 13 2
979tls_pn c add q 12 12 2
980tls_pn c add q 14 10 2
981tls_pn c add q 16 1027 2
982tls_pn c add q 18 1283 2
983tls_pn c add q 20 2052 2
984tls_pn c add q 22 2053 2
985tls_pn c add q 24 2054 2
986ret add q 26
987end
988
989note approved mode: key_share with a secp256r1 (65-byte) and a secp384r1 (97-byte) share at c[q]
990fn tls_apks c edit uchar * q ulong to ulong
991tls_pn c q 51 2
992tls_pn c add q 2 172 2
993tls_pn c add q 4 170 2
994tls_pn c add q 6 23 2
995tls_pn c add q 8 65 2
996tls_mov c add q 10 TLECQ2 65
997tls_pn c add q 75 24 2
998tls_pn c add q 77 97 2
999tls_mov c add q 79 TLECQ3 97
1000ret add q 176
1001end
1002
1003note ---- ClientHello (RFC 8446 section 4.1.2) built at c[TLWB+5] ----
1004note server_name, supported_groups (offered bits c[TLGO]), signature_algorithms; returns the next
1005note offset
1006fn tls_chex1 c edit uchar * p ulong to ulong
1007hl = ulong get c TLHL
1008tls_pn c p 0 2
1009tls_pn c add p 2 add hl 5 2
1010tls_pn c add p 4 add hl 3 2
1011put c add p 6 0
1012tls_pn c add p 7 hl 2
1013tls_mov c add p 9 TLHOST hl
1014q = add add p 9 hl
1015if eq tls_apm c 1
1016ret tls_apgs c q
1017end
1018q = tls_grps c q
1019tls_pn c q 13 2
1020tls_pn c add q 2 18 2
1021tls_pn c add q 4 16 2
1022tls_pn c add q 6 1027 2
1023tls_pn c add q 8 1283 2
1024tls_pn c add q 10 2052 2
1025tls_pn c add q 12 2053 2
1026tls_pn c add q 14 2054 2
1027tls_pn c add q 16 1025 2
1028tls_pn c add q 18 1281 2
1029tls_pn c add q 20 1537 2
1030ret add q 22
1031end
1032
1033note supported_versions (TLS 1.3 only), key_share (shares of c[TLGS]), cookie after a
1034note HelloRetryRequest, status_request (ocsp, no responder ids or extensions) when flag 8, ALPN
1035note http/1.1 when flag 1
1036fn tls_chex2 c edit uchar * p ulong to ulong
1037tls_pn c p 43 2
1038tls_pn c add p 2 3 2
1039put c add p 4 2
1040tls_pn c add p 5 772 2
1041q = add p 7
1042if eq tls_apm c 1
1043q = tls_apks c q
1044else
1045q = tls_kslist c q
1046end
1047q = tls_ckext c q
1048if ne and get c TLFL 8 0
1049tls_pn c q 5 2
1050tls_pn c add q 2 5 2
1051put c add q 4 1
1052tls_pn c add q 5 0 4
1053q = add q 9
1054end
1055if eq and get c TLFL 1 0
1056ret q
1057end
1058tls_pn c q 16 2
1059tls_pn c add q 2 11 2
1060tls_pn c add q 4 9 2
1061put c add q 6 8
1062tls_cpy c add q 7 TLALPN
1063ret add q 15
1064end
1065
1066note supported_groups extension at c[q] from the offered bits, in preference order; next offset
1067fn tls_grps c edit uchar * q ulong to ulong
1068go = ulong get c TLGO
1069p = add q 6
1070if ne and go TLGHYB 0
1071tls_pn c p 4588 2
1072p = add p 2
1073end
1074if ne and go TLGX25 0
1075tls_pn c p 29 2
1076p = add p 2
1077end
1078n = sub p add q 6
1079tls_pn c q 10 2
1080tls_pn c add q 2 add n 2 2
1081tls_pn c add q 4 n 2
1082ret p
1083end
1084
1085note group id to its bit; 0 for a group this client never offers
1086fn tls_gbit g ulong to ulong
1087if eq g 4588
1088ret TLGHYB
1089end
1090if eq g 29
1091ret TLGX25
1092end
1093ret 0
1094end
1095
1096note key_share extension at c[q]: one entry per bit of c[TLGS] (none is allowed: the server
1097note then picks a group with a HelloRetryRequest). The ML-KEM-768 encapsulation key is
1098note dk[1152..2336) (FIPS 203 Algorithm 16). Next offset.
1099fn tls_kslist c edit uchar * q ulong to ulong
1100gs = ulong get c TLGS
1101p = add q 6
1102if ne and gs TLGHYB 0
1103tls_pn c p 4588 2
1104tls_pn c add p 2 1216 2
1105tls_mov c add p 4 add TLKDK 1152 1184
1106tls_mov c add p 1188 TLEPU 32
1107p = add p 1220
1108end
1109if ne and gs TLGX25 0
1110tls_pn c p 29 2
1111tls_pn c add p 2 32 2
1112tls_mov c add p 4 TLEPU 32
1113p = add p 36
1114end
1115n = sub p add q 6
1116tls_pn c q 51 2
1117tls_pn c add q 2 add n 2 2
1118tls_pn c add q 4 n 2
1119ret p
1120end
1121
1122note cookie extension (RFC 8446 4.2.2) echoed verbatim from the HelloRetryRequest, which is still
1123note at c[TLHB] while ClientHello2 is built; nothing without a cookie. Next offset.
1124fn tls_ckext c edit uchar * q ulong to ulong
1125cl = tls_gn c TLCKL 2
1126if eq cl 0
1127ret q
1128end
1129tls_pn c q 44 2
1130tls_pn c add q 2 add cl 2 2
1131tls_pn c add q 4 cl 2
1132tls_mov c add q 6 tls_gn c TLCKO 4 cl
1133ret add add q 6 cl
1134end
1135
1136note random bytes into k: getrandom, or in approved mode (flag 4) the HMAC_DRBG at c[TLDRBG]
1137note with prediction resistance (fips.om); 0, TLERAND, or TLESELF (module error state)
1138fn tls_rand c edit uchar * k edit uchar * to ulong
1139if eq and get c TLFL 4 0
1140r = random k 0 len k 0
1141if ne ulong r len k
1142ret TLERAND
1143end
1144ret 0
1145end
1146if ne fips_drbgget part c TLDRBG DRSZ k 0
1147ret TLESELF
1148end
1149ret 0
1150end
1151
1152note 32 random bytes (tls_rand) at c[o]
1153fn tls_randc c edit uchar * o ulong to ulong
1154t = new uchar 32 0
1155r = tls_rand c t
1156for i ulong 32
1157put c add o i get t i
1158end
1159tls_zero t 0 32
1160ret r
1161end
1162
1163note ML-KEM-768 key pair of the hybrid share (FIPS 203 ML-KEM.KeyGen_internal from 64 random
1164note bytes d || z; dk at c[TLKDK], its ek at dk[1152..2336)): generated once while c[TLGS] holds
1165note TLGHYB, wiped as soon as it does not (a HelloRetryRequest for x25519). 0 or an error code
1166fn tls_kemset c edit uchar * to ulong
1167if eq and ulong get c TLGS TLGHYB 0
1168tls_zero c TLKDK 2400
1169put c TLKGEN 0
1170ret 0
1171end
1172if ne get c TLKGEN 0
1173ret 0
1174end
1175s = new uchar 64 0
1176r = tls_rand c s
1177if ne r 0
1178tls_zero s 0 64
1179ret r
1180end
1181ek = new uchar 1184 0
1182k = mlkem_keygen_seed s ek part c TLKDK 2400
1183tls_zero s 0 64
1184if ne k 0
1185tls_zero c TLKDK 2400
1186ret TLEARG
1187end
1188put c TLKGEN 1
1189ret 0
1190end
1191note ephemeral x25519 key pair: private at c[TLEPK], public at c[TLEPU]
1192fn tls_ephem c edit uchar * to ulong
1193k = new uchar 32 0
1194o = new uchar 32 0
1195r = tls_rand c k
1196if ne r 0
1197ret r
1198end
1199e = x25519_base o k
1200p = ulong 0
1201if eq e 0
1202p = fips_pctx k o
1203end
1204for i ulong 32
1205put c add TLEPK i get k i
1206put c add TLEPU i get o i
1207end
1208tls_zero k 0 32
1209if ne p 0
1210ret TLESELF
1211end
1212if ne e 0
1213ret TLERAND
1214end
1215ret 0
1216end
1217
1218note approved mode: an ECDH key pair on the curve with nb-byte scalars (32 secp256r1, 48 secp384r1)
1219note into c[TLECD2]/c[TLECQ2] or c[TLECD3]/c[TLECQ3]. Private key by rejection sampling (FIPS 186-5
1220note A.2.2: draw nb bytes from the approved DRBG (tls_rand), redraw while not in [1, n-1], at most 16
1221note draws), then the pair-wise consistency test of SP 800-56A Rev. 3 5.6.2.1.4 (ecdh_pct). A failed
1222note test or 16 rejected draws latch the module error state (fips_fail 21) and return
1223note TLS_ERR_SELFTEST; a failed DRBG call returns tls_rand's code.
1224fn tls_ecgen c edit uchar * nb ulong to ulong
1225d = new uchar 48 0
1226q = new uchar 97 0
1227ql = add 1 mul nb 2
1228rc = ulong 4
1229for t ulong 16
1230r = tls_rand c part d 0 nb
1231if ne r 0
1232tls_zero d 0 48
1233ret r
1234end
1235rc = ecdh_pub part d 0 nb part q 0 ql
1236if ne rc 4
1237break
1238end
1239end
1240if eq rc 0
1241rc = ecdh_pct part d 0 nb part q 0 ql
1242end
1243do = TLECD2
1244qo = TLECQ2
1245if eq nb 48
1246do = TLECD3
1247qo = TLECQ3
1248end
1249for i ulong nb
1250put c add do i get d i
1251end
1252for i ulong ql
1253put c add qo i get q i
1254end
1255tls_zero d 0 48
1256if ne rc 0
1257tls_zero c TLECD2 145
1258tls_zero c TLECQ3 97
1259r = fips_fail 21
1260ret TLESELF
1261end
1262ret 0
1263end
1264
1265note the ephemeral key pairs for the offered groups: x25519 and the ML-KEM-768 half of the hybrid
1266note (default) or secp256r1 and secp384r1
1267fn tls_ephall c edit uchar * to ulong
1268if eq tls_apm c 0
1269e = tls_ephem c
1270if ne e 0
1271ret e
1272end
1273ret tls_kemset c
1274end
1275e = tls_ecgen c 32
1276if ne e 0
1277ret e
1278end
1279ret tls_ecgen c 48
1280end
1281
1282note writes the ClientHello handshake message at c[TLWB+5]; 0 or an error code
1283fn tls_mkch c edit uchar * to ulong
1284e = tls_ephall c
1285if ne e 0
1286ret e
1287end
1288b = add TLWB 5
1289put c b 1
1290tls_pn c add b 4 771 2
1291r = tls_randc c add b 6
1292if ne r 0
1293ret r
1294end
1295put c add b 38 32
1296r = tls_randc c add b 39
1297if ne r 0
1298ret r
1299end
1300tls_mov c TLSID add b 39 32
1301tls_mov c TLRND add b 6 32
1302ret tls_chtail c
1303end
1304
1305note cipher suites (tls_suites), compression and extensions after the session id; fills in the
1306note lengths
1307fn tls_chtail c edit uchar * to ulong
1308b = add TLWB 5
1309x = tls_suites c add b 71
1310q = tls_chex1 c add x 2
1311q = tls_chex2 c q
1312tls_pn c x sub sub q x 2 2
1313tls_pn c add b 1 sub sub q b 4 3
1314ret 0
1315end
1316
1317note ClientHello2 after a HelloRetryRequest: the same random and session id (RFC 8446 4.1.2)
1318fn tls_ch2 c edit uchar * to ulong
1319e = tls_kemset c
1320if ne e 0
1321ret e
1322end
1323b = add TLWB 5
1324put c b 1
1325tls_pn c add b 4 771 2
1326tls_mov c add b 6 TLRND 32
1327put c add b 38 32
1328tls_mov c add b 39 TLSID 32
1329ret tls_chtail c
1330end
1331
1332note ---- ServerHello (RFC 8446 section 4.1.3) at c[TLHB] ----
1333note HRR random = SHA-256("HelloRetryRequest"), computed rather than tabled
1334text TLHRR "HelloRetryRequest"
1335
1336note 0 when the 32 bytes at c[o] equal our legacy_session_id
1337fn tls_sidok c view uchar * o ulong to ulong
1338s = new uchar 32 0
1339for i ulong 32
1340put s i get c add TLSID i
1341end
1342ret tls_cteq s c o 32
1343end
1344
1345fn tls_ishrr c view uchar * o ulong to ulong
1346h = new uchar 32 0
1347n = sha2_digest 256 TLHRR h
1348ret xor tls_cteq h c o 32 1
1349end
1350
1351note fixed part: version, random, session id echo, suite, compression; 0 or error
1352fn tls_shhead c edit uchar * to ulong
1353if ne get c TLHB 2
1354ret TLEUNEX
1355end
1356ln = tls_gn c add TLHB 1 3
1357if lt ln 76
1358ret TLEDEC
1359end
1360b = add TLHB 4
1361if ne tls_gn c b 2 771
1362ret TLEVER
1363end
1364if ne get c add b 34 32
1365ret TLESID
1366end
1367if ne tls_sidok c add b 35 0
1368ret TLESID
1369end
1370s = tls_gn c add b 67 2
1371put c TLSU 0
1372if eq s 4867
1373put c TLSU 1
1374end
1375if eq s 4865
1376put c TLSU 2
1377end
1378if eq get c TLSU 0
1379ret TLECIPH
1380end
1381if and eq get c TLSU 1 eq tls_apm c 1
1382ret TLECIPH
1383end
1384if ne get c add b 69 0
1385ret TLEDEC
1386end
1387x = tls_gn c add b 70 2
1388if ne add x 72 ln
1389ret TLEDEC
1390end
1391if eq tls_ishrr c add b 2 1
1392ret TLEHRR
1393end
1394ret 0
1395end
1396
1397note 1 when t is an extension type of the RFC 8446 section 4.2 table: one of these in a message
1398note the table does not list it for is illegal_parameter (TLS_ERR_ILLEGAL_PARAMETER); an unknown
1399note type, or a listed one we did not ask for, is unsupported_extension (TLS_ERR_EXTENSION)
1400fn tls_kext t ulong to ulong
1401if or le t 1 eq t 5
1402ret 1
1403end
1404if or eq t 10 and ge t 13 le t 16
1405ret 1
1406end
1407if and ge t 18 le t 21
1408ret 1
1409end
1410if and ge t 41 le t 51
1411if ne t 46
1412ret 1
1413end
1414end
1415ret 0
1416end
1417
1418note 1 when t is in the RFC 8446 4.2 table but the table does not allow it in EncryptedExtensions
1419fn tls_eebad t ulong to ulong
1420if or le t 1 eq t 10
1421ret 0
1422end
1423if and ge t 14 le t 16
1424ret 0
1425end
1426if or or eq t 19 eq t 20 eq t 42
1427ret 0
1428end
1429ret tls_kext t
1430end
1431
1432note ServerHello key_share entry at c[q..q+l): default mode tls_ksh (a group of c[TLGS], x25519
1433note into c[TLSPUB]); approved mode secp256r1 (23, 65 bytes) or secp384r1 (24, 97 bytes) into
1434note c[TLECP], the group into c[TLGRP]. Anything else, x25519 in approved mode included, is
1435note TLS_ERR_KEY_SHARE.
1436fn tls_shks c edit uchar * q ulong l ulong to ulong
1437if eq tls_apm c 0
1438ret tls_ksh c q l
1439end
1440if lt l 4
1441ret TLEKEYS
1442end
1443g = tls_gn c q 2
1444kl = tls_gn c add q 2 2
1445if ne add kl 4 l
1446ret TLEKEYS
1447end
1448ok = ulong 0
1449if and eq g 23 eq kl 65
1450ok = 1
1451end
1452if and eq g 24 eq kl 97
1453ok = 1
1454end
1455if eq ok 0
1456ret TLEKEYS
1457end
1458put c TLGRP uchar g
1459tls_mov c TLECP add q 4 kl
1460ret 0
1461end
1462
1463note approved mode: copy the private key for len d (32 secp256r1 at TLECD2, 48 secp384r1 at TLECD3)
1464note and the server point into d and p, then Z = x(d*Q) straight into c[TLECZ]; returns ecdh_shared's code
1465fn tls_ecs c edit uchar * d edit uchar * p edit uchar * to ulong
1466nb = len d
1467for i ulong nb
1468put d i get c sub add add TLECD2 mul 2 nb i 64
1469end
1470for i ulong len p
1471put p i get c add TLECP i
1472end
1473r = ecdh_shared d p part c TLECZ nb
1474tls_zero d 0 nb
1475ret r
1476end
1477
1478note approved mode: Z = x(d*Q) (ecdh_shared: SP 800-56A Rev. 3 5.7.1.2 after the partial public-key
1479note validation of 5.6.2.3.4) for the server's group into c[TLECZ]; both private keys are wiped.
1480note An invalid server point is TLS_ERR_KEY_SHARE, an infinity result TLS_ERR_ZERO_SECRET.
1481fn tls_ecdhp c edit uchar * to ulong
1482d2 = new uchar 32 0
1483p2 = new uchar 65 0
1484d3 = new uchar 48 0
1485p3 = new uchar 97 0
1486r = ulong 0
1487if eq get c TLGRP 24
1488r = tls_ecs c d3 p3
1489else
1490r = tls_ecs c d2 p2
1491end
1492tls_zero c TLECD2 80
1493if eq r 1
1494ret TLEZERO
1495end
1496if ne r 0
1497ret TLEKEYS
1498end
1499ret 0
1500end
1501
1502note HKDF-Extract selector for the handshake secret: salt c[TLHS], ikm the (EC)DHE shared secret
1503note (64 bytes ML-KEM || x25519 for the hybrid, draft-ietf-tls-ecdhe-mlkem)
1504fn tls_ikm c view uchar * to ulong
1505s = or TLHS shl TLHS 16
1506if eq tls_apm c 0
1507if eq ulong get c TLGSEL TLGHYB
1508ret or s or shl TLSS 32 shl ulong 64 48
1509end
1510ret or s shl TLSS 32
1511end
1512l = ulong 32
1513if eq get c TLGRP 24
1514l = 48
1515end
1516ret or s or shl TLECZ 32 shl l 48
1517end
1518
1519note ServerHello extensions: exactly supported_versions = 0x0304 and a key_share (tls_shks)
1520fn tls_shext c edit uchar * to ulong
1521ln = tls_gn c add TLHB 1 3
1522q = add TLHB 76
1523z = add add TLHB 4 ln
1524seen = ulong 0
1525while lt q z bound 64
1526if gt add q 4 z
1527ret TLEDEC
1528end
1529t = tls_gn c q 2
1530l = tls_gn c add q 2 2
1531q = add q 4
1532if gt add q l z
1533ret TLEDEC
1534end
1535if eq t 43
1536if ne l 2
1537ret TLEDEC
1538end
1539if ne and seen 1 0
1540ret TLEEXT
1541end
1542if ne tls_gn c q 2 772
1543ret TLEILL
1544end
1545seen = or seen 1
1546else
1547if and ne t 51 and ne tls_kext t 0 ne t 41
1548ret TLEILL
1549end
1550if or ne t 51 ne and seen 2 0
1551ret TLEEXT
1552end
1553e = tls_shks c q l
1554if ne e 0
1555ret e
1556end
1557seen = or seen 2
1558end
1559q = add q l
1560end
1561if eq and seen 1 0
1562ret TLEVER
1563end
1564if eq and seen 2 0
1565ret TLEKEYS
1566end
1567ret 0
1568end
1569
1570note ServerHello key_share entry (l bytes at c[q]): a group this ClientHello holds a share for and
1571note exactly its share length; the x25519 public key (the last 32 bytes) goes to c[TLSPUB], the
1572note group bit to c[TLGSEL] and the share offset to c[TLKSO]
1573fn tls_ksh c edit uchar * q ulong l ulong to ulong
1574g = tls_gbit tls_gn c q 2
1575if or eq g 0 eq and ulong get c TLGS g 0
1576ret TLEKEYS
1577end
1578kl = ulong 32
1579if eq g TLGHYB
1580kl = 1120
1581end
1582if or ne l add kl 4 ne tls_gn c add q 2 2 kl
1583ret TLEKEYS
1584end
1585put c TLGSEL uchar g
1586tls_pn c TLKSO add q 4 4
1587tls_mov c TLSPUB add add q 4 sub kl 32 32
1588ret 0
1589end
1590
1591note HelloRetryRequest extension type to its seen bit; 0 = not allowed (unsupported_extension)
1592fn tls_hrrbit t ulong to ulong
1593if eq t 43
1594ret 1
1595end
1596if eq t 51
1597ret 2
1598end
1599if eq t 44
1600ret 4
1601end
1602ret 0
1603end
1604
1605note one HelloRetryRequest extension of type t, l bytes at c[q]. key_share must name a group we
1606note offered without a share (illegal_parameter otherwise); a cookie is <1..2^16-1> and is kept
1607note in place for ClientHello2 (more than 12000 bytes would not fit one record: TLEBIG)
1608fn tls_hrr1 c edit uchar * t ulong q ulong l ulong to ulong
1609if eq t 43
1610if ne l 2
1611ret TLEDEC
1612end
1613if ne tls_gn c q 2 772
1614ret TLEILL
1615end
1616ret 0
1617end
1618if eq t 51
1619if ne l 2
1620ret TLEDEC
1621end
1622g = tls_gbit tls_gn c q 2
1623if or eq g 0 eq and ulong get c TLGO g 0
1624ret TLEKEYS
1625end
1626if ne and ulong get c TLGS g 0
1627ret TLEKEYS
1628end
1629put c TLGS uchar g
1630ret 0
1631end
1632if lt l 3
1633ret TLEDEC
1634end
1635n = tls_gn c q 2
1636if or eq n 0 ne add n 2 l
1637ret TLEDEC
1638end
1639if gt n 12000
1640ret TLEBIG
1641end
1642tls_pn c TLCKO add q 2 4
1643tls_pn c TLCKL n 2
1644ret 0
1645end
1646
1647note HelloRetryRequest extensions (RFC 8446 4.1.4): supported_versions 0x0304 is required, each
1648note extension once, and the request must change the ClientHello (key_share or cookie)
1649fn tls_hrrext c edit uchar * to ulong
1650ln = tls_gn c add TLHB 1 3
1651q = add TLHB 76
1652z = add add TLHB 4 ln
1653seen = ulong 0
1654while lt q z bound 64
1655if gt add q 4 z
1656ret TLEDEC
1657end
1658t = tls_gn c q 2
1659l = tls_gn c add q 2 2
1660q = add q 4
1661if gt add q l z
1662ret TLEDEC
1663end
1664b = tls_hrrbit t
1665if and eq b 0 ne tls_kext t 0
1666ret TLEILL
1667end
1668if or eq b 0 ne and seen b 0
1669ret TLEEXT
1670end
1671e = tls_hrr1 c t q l
1672if ne e 0
1673ret e
1674end
1675seen = or seen b
1676q = add q l
1677end
1678if lt q z
1679ret TLEDEC
1680end
1681if eq and seen 1 0
1682ret TLEVER
1683end
1684if eq and seen 6 0
1685ret TLEHRR
1686end
1687ret 0
1688end
1689
1690note HelloRetryRequest at c[TLHB]: check it, restart the transcript as message_hash(254, 00 00 20,
1691note SHA-256(ClientHello1)) then the HelloRetryRequest (RFC 8446 4.4.1), send ClientHello2.
1692note The server may send its compatibility change_cipher_spec now (RFC 8446 D.4).
1693fn tls_hrr c edit uchar * th edit ulong * to ulong
1694e = tls_hrrext c
1695if ne e 0
1696ret e
1697end
1698put c TLHRS 1
1699put c TLHSU get c TLSU
1700h = new uchar 36 0
1701put h 0 254
1702put h 3 32
1703tls_th th part h 4 32
1704sha256_init th
1705sha256_update th h
1706ml = add tls_gn c add TLHB 1 3 4
1707sha256_update th part c TLHB ml
1708e = tls_ch2 c
1709if ne e 0
1710ret e
1711end
1712tls_hsdrop c
1713if ne tls_gn c TLHF 4 0
1714ret TLEUNEX
1715end
1716n = add tls_gn c add TLWB 6 3 4
1717sha256_update th part c add TLWB 5 n
1718put c TLCCS 1
1719ret tls_wrbuf c 22 n
1720end
1721
1722note after a HelloRetryRequest: ClientHello2, then a ServerHello (a second HelloRetryRequest is
1723note unexpected_message) whose suite is the one the HelloRetryRequest chose
1724fn tls_sh2 c edit uchar * th edit ulong * to ulong
1725e = tls_hrr c th
1726if ne e 0
1727ret e
1728end
1729e = tls_hsmsg c
1730if ne e 0
1731ret e
1732end
1733e = tls_shhead c
1734if eq e TLEHRR
1735ret TLEUNEX
1736end
1737if ne e 0
1738ret e
1739end
1740if ne get c TLSU get c TLHSU
1741ret TLECIPH
1742end
1743ret 0
1744end
1745
1746note hybrid decapsulation: the ML-KEM shared secret into c[TLSS..+32) from the server's ciphertext
1747note (the first 1088 bytes of its share, still at c[TLKSO]); the decapsulation key is wiped either
1748note way. A wrong ciphertext gives the implicit-rejection secret, so the Finished MAC fails. 0 or
1749note TLEKEYS
1750fn tls_kemdec c edit uchar * to ulong
1751r = ulong 0
1752ss = new uchar 32 0
1753ct = new uchar 1088 0
1754o = tls_gn c TLKSO 4
1755if eq ulong get c TLGSEL TLGHYB
1756for i ulong 1088
1757put ct i get c add o i
1758end
1759r = mlkem_decaps part c TLKDK 2400 ct ss
1760for i ulong 32
1761put c add TLSS i get ss i
1762end
1763tls_zero ss 0 32
1764end
1765tls_zero c TLKDK 2400
1766put c TLKGEN 0
1767if ne r 0
1768ret TLEKEYS
1769end
1770ret 0
1771end
1772
1773note shared secret into c[TLSS] (hybrid: ML-KEM secret, then x25519 at c[TLSS+32]); the private
1774note keys are wiped
1775fn tls_ecdh c edit uchar * to ulong
1776if eq tls_apm c 1
1777r = tls_ecdhp c
1778z = scrub 16384
1779ret r
1780end
1781e = tls_kemdec c
1782z = scrub 65536
1783if ne e 0
1784tls_zero c TLEPK 32
1785ret e
1786end
1787x = TLSS
1788if eq ulong get c TLGSEL TLGHYB
1789x = add TLSS 32
1790end
1791k = new uchar 32 0
1792u = new uchar 32 0
1793o = new uchar 32 0
1794for i ulong 32
1795put k i get c add TLEPK i
1796put u i get c add TLSPUB i
1797end
1798r = x25519_scalarmult o k u
1799z = scrub 4096
1800for i ulong 32
1801put c add x i get o i
1802end
1803tls_zero k 0 32
1804tls_zero o 0 32
1805tls_zero c TLEPK 32
1806if ne r 0
1807ret TLEZERO
1808end
1809ret 0
1810end
1811
1812note handshake secrets after ServerHello; installs the handshake write and read keys
1813fn tls_hskeys_core c edit uchar * th view ulong * to ulong
1814h = new uchar 32 0
1815z = new uchar 32 0
1816tls_h0 z
1817r = tls_xtr c TLHS
1818r = or r tls_drv c tls_sel TLHS TLHS 32 TLIDER z
1819r = or r tls_xtr c tls_ikm c
1820tls_th th h
1821r = or r tls_drv c tls_sel TLCHS TLHS 32 TLICHS h
1822r = or r tls_drv c tls_sel TLSHS TLHS 32 TLISHS h
1823r = or r tls_drv c tls_sel TLMS TLHS 32 TLIDER z
1824r = or r tls_xtr c or TLMS shl TLMS 16
1825tls_zero c TLSS 64
1826tls_zero c TLECZ 48
1827r = or r tls_keys c TLSHS 1
1828r = or r tls_keys c TLCHS 0
1829if ne r 0
1830ret TLEARG
1831end
1832put c TLRK 1
1833if eq get c TLCCS 0
1834put c TLCCS 1
1835end
1836ret 0
1837end
1838
1839fn tls_hskeys c edit uchar * th view ulong * to ulong
1840prev = dit 1
1841r = tls_hskeys_core c th
1842z = scrub 4096
1843z = dit int prev
1844ret r
1845end
1846
1847note read and check ServerHello, send the compatibility change_cipher_spec, derive keys
1848fn tls_rdsh c edit uchar * th edit ulong * to ulong
1849e = tls_hsmsg c
1850if eq e 0
1851e = tls_shhead c
1852end
1853if eq e TLEHRR
1854e = tls_sh2 c th
1855end
1856if ne e 0
1857ret e
1858end
1859e = tls_shext c
1860if ne e 0
1861ret e
1862end
1863tls_hsdone c th
1864if ne tls_gn c TLHF 4 0
1865ret TLEUNEX
1866end
1867e = tls_ecdh c
1868z = scrub 65536
1869if ne e 0
1870ret e
1871end
1872put c add TLWB 5 1
1873e = tls_wrbuf c 20 1
1874if ne e 0
1875ret e
1876end
1877e = tls_hskeys c th
1878z = scrub 4096
1879if ne e 0
1880ret e
1881end
1882put c TLWK 1
1883ret 0
1884end
1885
1886note ---- server's encrypted flight (RFC 8446 sections 4.3, 4.4) ----
1887note EncryptedExtensions: only server_name (empty), supported_groups and ALPN (offered, http/1.1)
1888fn tls_ee c edit uchar * to ulong
1889if ne get c TLHB 8
1890ret TLEUNEX
1891end
1892ln = tls_gn c add TLHB 1 3
1893if lt ln 2
1894ret TLEDEC
1895end
1896z = add add TLHB 4 ln
1897if ne add tls_gn c add TLHB 4 2 2 ln
1898ret TLEDEC
1899end
1900q = add TLHB 6
1901seen = ulong 0
1902while lt q z bound 64
1903if gt add q 4 z
1904ret TLEDEC
1905end
1906t = tls_gn c q 2
1907l = tls_gn c add q 2 2
1908q = add q 4
1909if gt add q l z
1910ret TLEDEC
1911end
1912bit = ulong 0
1913if eq t 0
1914if ne l 0
1915ret TLEDEC
1916end
1917bit = 1
1918end
1919if eq t 10
1920bit = 2
1921end
1922if eq t 16
1923e = tls_alpnok c q l
1924if ne e 0
1925ret e
1926end
1927bit = 4
1928end
1929if ne tls_eebad t 0
1930ret TLEILL
1931end
1932if or eq bit 0 ne and seen bit 0
1933ret TLEEXT
1934end
1935seen = or seen bit
1936q = add q l
1937end
1938ret 0
1939end
1940
1941note the server's ALPN choice must be exactly the one protocol we offered
1942fn tls_alpnok c view uchar * q ulong l ulong to ulong
1943if eq and get c TLFL 1 0
1944ret TLEEXT
1945end
1946if or ne l 11 ne tls_gn c q 2 9
1947ret TLEALPN
1948end
1949if ne get c add q 2 8
1950ret TLEALPN
1951end
1952if ne tls_cteq TLALPN c add q 3 8 0
1953ret TLEALPN
1954end
1955ret 0
1956end
1957
1958note CertificateRequest: remember its context (we answer with an empty Certificate)
1959fn tls_cr c edit uchar * to ulong
1960ln = tls_gn c add TLHB 1 3
1961if lt ln 3
1962ret TLEDEC
1963end
1964cl = ulong get c add TLHB 4
1965if gt add cl 3 ln
1966ret TLEDEC
1967end
1968if ne add add cl 3 tls_gn c add add TLHB 5 cl 2 ln
1969ret TLEDEC
1970end
1971put c TLCRX uchar cl
1972tls_mov c add TLCRX 1 add TLHB 5 cl
1973put c TLCR 1
1974ret 0
1975end
1976
1977note Certificate: empty context, then the certificate_list (validated by x509_chain_verify);
1978note the leaf entry is kept at c[TLLF] (extensions stripped) for CertificateVerify
1979fn tls_cert c edit uchar * st view uchar * to ulong
1980if ne get c TLHB 11
1981ret TLEUNEX
1982end
1983ln = tls_gn c add TLHB 1 3
1984if lt ln 4
1985ret TLEDEC
1986end
1987if ne get c add TLHB 4 0
1988ret TLEDEC
1989end
1990cl = tls_gn c add TLHB 5 3
1991if ne add cl 4 ln
1992ret TLEDEC
1993end
1994if lt cl 5
1995ret TLEDEC
1996end
1997ret tls_chain c st cl
1998end
1999
2000note hq = now (8 bytes, unix seconds) || hostname; returns its length
2001fn tls_hq c view uchar * hq edit uchar * to ulong
2002now = div realns 1000000000
2003tls_pn hq 0 ulong now 8
2004hl = ulong get c TLHL
2005for i ulong hl
2006put hq add 8 i get c add TLHOST i
2007end
2008ret add 8 hl
2009end
2010
2011note ---- revocation (flag 8, rev.om): status_request offered in ClientHello; after the chain verified,
2012note the leaf's stapled OCSP response must verify and say good (rev_ocsp) and a must-staple leaf
2013note (RFC 7633) without one is refused; the rev.om code is the detail of TLS_ERR_CERTIFICATE ----
2014note offset of the staple in the rev_check buffer (leaf at 0, issuer at TLLFSZ, staple at TLRVST)
2015const TLRVST ulong 33792
2016const TLRVSZ ulong 50688
2017
2018note tls_stok: the leaf entry's extensions at c[x .. x+2+el) are exactly one status_request (5)
2019note holding a CertificateStatus of type ocsp (1) with an OCSPResponse of 1 .. TLLFSZ bytes: its
2020note length, else 0
2021fn tls_stok c view uchar * x ulong el ulong to ulong
2022if lt el 8
2023ret 0
2024end
2025if or ne tls_gn c add x 2 2 5 ne tls_gn c add x 4 2 sub el 4
2026ret 0
2027end
2028if ne get c add x 6 1
2029ret 0
2030end
2031sl = tls_gn c add x 7 3
2032if or ne add sl 4 sub el 4 or eq sl 0 gt sl TLLFSZ
2033ret 0
2034end
2035ret sl
2036end
2037
2038note tls_stcp: the leaf entry (q[2] = its certificate length, q[3] = its extensions offset in c) with
2039note empty extensions, then the rest of the certificate_list, into cb; q[0] the copy's length
2040fn tls_stcp c view uchar * cl ulong cb edit uchar 65536 q edit ulong 4 to void
2041l1 = get q 2
2042x = get q 3
2043el = tls_gn c x 2
2044rev_cp part cb 0 add l1 3 c add TLHB 8 add l1 3
2045put cb add l1 3 0
2046put cb add l1 4 0
2047r = sub sub cl add l1 5 el
2048rev_cp part cb add l1 5 r c add add x 2 el r
2049put q 0 sub cl el
2050end
2051
2052note tls_stap: copy of the certificate_list c[TLHB+8 .. +cl) into cb; when the leaf entry's
2053note extensions are one stapled OCSP response (tls_stok) they are left out of the copy and the
2054note OCSPResponse goes to b[TLRVST ..). q[0] the copy's length, q[1] the staple's length (0 none).
2055note Any other leaf extension stays in the copy, so x509_chain_verify refuses it (X5EEXT). 0 or TLEDEC.
2056fn tls_stap c view uchar * cl ulong cb edit uchar 65536 b edit uchar 50688 q edit ulong 4 to ulong
2057l1 = tls_gn c add TLHB 8 3
2058if gt add l1 5 cl
2059ret TLEDEC
2060end
2061x = add add TLHB 11 l1
2062el = tls_gn c x 2
2063if gt add add l1 5 el cl
2064ret TLEDEC
2065end
2066sl = tls_stok c x el
2067put q 1 sl
2068if eq sl 0
2069rev_cp part cb 0 cl c add TLHB 8 cl
2070put q 0 cl
2071ret 0
2072end
2073put q 2 l1
2074put q 3 x
2075tls_stcp c cl cb q
2076rev_cp part b TLRVST sl c add x 10 sl
2077ret 0
2078end
2079
2080note tls_rev: rev_check of the leaf of cb (q[0] bytes) with its issuer (the presented certificates,
2081note then the store st) and the staple b[TLRVST .. +q[1]) at the current time; 0 or a rev.om code
2082fn tls_rev cb view uchar 65536 q view ulong 4 st view uchar * b edit uchar 50688 to ulong
2083p = new ulong 16 0
2084l1 = der_u24 cb 0
2085rev_cp part b 0 l1 cb 3 l1
2086il = rev_issuer part cb 0 get q 0 st part b TLLFSZ TLLFSZ
2087if eq il 0
2088ret X5EUNK
2089end
2090now = div realns 1000000000
2091put p 0 ulong now
2092put p 2 l1
2093put p 3 TLLFSZ
2094put p 4 il
2095put p 5 TLRVST
2096put p 6 get q 1
2097ret rev_check b p
2098end
2099
2100note tls_cvfy: x509_chain_verify of the copy cb (q[0] bytes) for the connection's host, now
2101fn tls_cvfy c view uchar * st view uchar * cb view uchar 65536 q view ulong 4 to ulong
2102hq = new uchar 264 0
2103n = tls_hq c hq
2104ret x509_chain_verify part cb 0 get q 0 st part hq 0 n
2105end
2106
2107note tls_chainr: tls_chain with revocation (flag 8)
2108fn tls_chainr c edit uchar * st view uchar * cl ulong to ulong
2109cb = new uchar 65536 0
2110b = new uchar TLRVSZ 0
2111q = new ulong 4 0
2112e = tls_stap c cl cb b q
2113if ne e 0
2114ret e
2115end
2116r = tls_cvfy c st cb q
2117if eq r X5EEXT
2118tls_pn c TLDET r 4
2119ret TLEEXT
2120end
2121if eq r 0
2122r = tls_rev cb q st b
2123end
2124if ne r 0
2125tls_pn c TLDET r 4
2126ret TLECERT
2127end
2128ret tls_leaf c cl
2129end
2130
2131fn tls_chain c edit uchar * st view uchar * cl ulong to ulong
2132if ne and get c TLFL 8 0
2133ret tls_chainr c st cl
2134end
2135hq = new uchar 264 0
2136n = tls_hq c hq
2137r = x509_chain_verify part c add TLHB 8 cl st part hq 0 n
2138if eq r X5EEXT
2139tls_pn c TLDET r 4
2140ret TLEEXT
2141end
2142if ne r 0
2143tls_pn c TLDET r 4
2144ret TLECERT
2145end
2146ret tls_leaf c cl
2147end
2148
2149fn tls_leaf c edit uchar * cl ulong to ulong
2150l1 = tls_gn c add TLHB 8 3
2151if gt add l1 5 cl
2152ret TLEDEC
2153end
2154if gt add l1 5 TLLFSZ
2155ret TLEBIG
2156end
2157tls_mov c TLLF add TLHB 8 add l1 3
2158put c add add TLLF l1 3 0
2159put c add add TLLF l1 4 0
2160ret 0
2161end
2162
2163note CertificateVerify over the transcript hash h (through Certificate)
2164fn tls_cv c edit uchar * h view uchar * to ulong
2165if ne get c TLHB 15
2166ret TLEUNEX
2167end
2168ln = tls_gn c add TLHB 1 3
2169if lt ln 4
2170ret TLEDEC
2171end
2172sch = tls_gn c add TLHB 4 2
2173sl = tls_gn c add TLHB 6 2
2174if ne add sl 4 ln
2175ret TLEDEC
2176end
2177if gt sl 1024
2178ret TLEDEC
2179end
2180if eq tls_schok sch 0
2181ret TLESCH
2182end
2183m = new uchar 1154 0
2184tls_cvmsg c h m
2185r = tls_cvcall c m sch
2186if ne r 0
2187tls_pn c TLDET r 4
2188ret TLECV
2189end
2190ret 0
2191end
2192
2193note m = 64 spaces || context string || 0 || h || signature (from the message at c[TLHB])
2194fn tls_cvmsg c view uchar * h view uchar * m edit uchar * to void
2195for i ulong 64
2196put m i 32
2197end
2198tls_cpy m 64 TLCVC
2199put m 97 0
2200tls_cpy m 98 h
2201sl = tls_gn c add TLHB 6 2
2202for i ulong sl
2203put m add 130 i get c add add TLHB 8 i
2204end
2205end
2206
2207fn tls_cvcall c edit uchar * m view uchar * sch ulong to ulong
2208sl = tls_gn c add TLHB 6 2
2209l1 = add tls_gn c TLLF 3 5
2210ret x509_cv_verify part c TLLF l1 sch part m 0 add 130 sl 130
2211end
2212
2213note 1 for the CertificateVerify schemes we offered (never rsa_pkcs1_*, RFC 8446 section 4.4.3)
2214fn tls_schok s ulong to ulong
2215if or eq s 1027 eq s 1283
2216ret 1
2217end
2218if or eq s 2052 or eq s 2053 eq s 2054
2219ret 1
2220end
2221ret 0
2222end
2223
2224note hv = HMAC(finished_key(secret at c[so]), hv): the transcript hash in, verify_data out
2225note (RFC 8446 section 4.4.4)
2226fn tls_vd c edit uchar * so ulong hv edit uchar * to ulong
2227e = new uchar 1 0
2228k = new uchar 32 0
2229x = new ulong 96 0
2230r = tls_drv c tls_sel TLTMP so 32 TLIFIN part e 0 0
2231for i ulong 32
2232put k i get c add TLTMP i
2233end
2234tls_zero c TLTMP 32
2235hmac_init 256 x k
2236hmac_update x hv
2237n = hmac_final x hv
2238tls_zero k 0 32
2239for i ulong 96
2240put x i 0
2241end
2242ret r
2243end
2244
2245note server Finished
2246fn tls_sfin c edit uchar * h view uchar * to ulong
2247if ne get c TLHB 20
2248ret TLEUNEX
2249end
2250if ne tls_gn c add TLHB 1 3 32
2251ret TLEDEC
2252end
2253v = new uchar 32 0
2254tls_cpy v 0 h
2255r = tls_vd c TLSHS v
2256if ne r 0
2257ret TLEARG
2258end
2259if ne tls_cteq v c add TLHB 4 32 0
2260ret TLEFIN
2261end
2262ret 0
2263end
2264
2265note EE, optional CertificateRequest, Certificate, CertificateVerify, Finished
2266fn tls_rdenc c edit uchar * st view uchar * th edit ulong * to ulong
2267e = tls_hsmsg c
2268if eq e 0
2269e = tls_ee c
2270end
2271if ne e 0
2272ret e
2273end
2274tls_hsdone c th
2275e = tls_hsmsg c
2276if ne e 0
2277ret e
2278end
2279if eq get c TLHB 13
2280e = tls_cr c
2281if ne e 0
2282ret e
2283end
2284tls_hsdone c th
2285e = tls_hsmsg c
2286if ne e 0
2287ret e
2288end
2289end
2290e = tls_cert c st
2291if ne e 0
2292ret e
2293end
2294tls_hsdone c th
2295ret tls_rdfin c th
2296end
2297
2298fn tls_rdfin c edit uchar * th edit ulong * to ulong
2299h = new uchar 32 0
2300tls_th th h
2301e = tls_hsmsg c
2302if eq e 0
2303e = tls_cv c h
2304end
2305if ne e 0
2306ret e
2307end
2308tls_hsdone c th
2309tls_th th h
2310e = tls_hsmsg c
2311if eq e 0
2312e = tls_sfin c h
2313end
2314if ne e 0
2315ret e
2316end
2317tls_hsdone c th
2318if ne tls_gn c TLHF 4 0
2319ret TLEUNEX
2320end
2321ret tls_apkeys c th
2322end
2323
2324note application traffic secrets from the transcript through server Finished; installs the read key
2325fn tls_apkeys c edit uchar * th view ulong * to ulong
2326h = new uchar 32 0
2327tls_th th h
2328r = tls_drv c tls_sel TLCAP TLMS 32 TLICAP h
2329r = or r tls_drv c tls_sel TLSAP TLMS 32 TLISAP h
2330r = or r tls_keys c TLSAP 1
2331if ne r 0
2332ret TLEARG
2333end
2334ret 0
2335end
2336
2337note client flight: [empty Certificate], Finished; then the application write key
2338fn tls_cfin c edit uchar * th edit ulong * to ulong
2339b = add TLWB 5
2340if eq get c TLCR 1
2341cl = ulong get c TLCRX
2342put c b 11
2343tls_pn c add b 1 add cl 4 3
2344put c add b 4 uchar cl
2345tls_mov c add b 5 add TLCRX 1 cl
2346tls_pn c add add b 5 cl 0 3
2347n = add cl 8
2348sha256_update th part c b n
2349e = tls_wrbuf c 22 n
2350if ne e 0
2351ret e
2352end
2353end
2354v = new uchar 32 0
2355tls_th th v
2356r = tls_vd c TLCHS v
2357put c b 20
2358tls_pn c add b 1 32 3
2359for i ulong 32
2360put c add add b 4 i get v i
2361end
2362sha256_update th part c b 36
2363e = tls_wrbuf c 22 36
2364if ne e 0
2365ret e
2366end
2367r = or r tls_keys c TLCAP 0
2368tls_zero c TLHS 128
2369if ne r 0
2370ret TLEARG
2371end
2372ret 0
2373end
2374
2375note alert description for a failure (0: none, the peer is gone or already alerted)
2376fn tls_alertfor e ulong to ulong
2377if or eq e TLEIO or eq e TLETIME or eq e TLEEOF eq e TLEALRT
2378ret 0
2379end
2380if eq e TLEMAC
2381ret 20
2382end
2383if or eq e TLEREC eq e TLEBIG
2384ret 22
2385end
2386if eq e TLEUNEX
2387ret 10
2388end
2389if eq e TLEDEC
2390ret 50
2391end
2392if eq e TLEVER
2393ret 70
2394end
2395if or eq e TLEHRR or eq e TLETKT eq e TLEILL
2396ret 47
2397end
2398if eq e TLEEXT
2399ret 110
2400end
2401if eq e TLECERT
2402ret 42
2403end
2404if or eq e TLECV eq e TLEFIN
2405ret 51
2406end
2407if eq e TLEALPN
2408ret 120
2409end
2410if or eq e TLECIPH or eq e TLEKEYS or eq e TLESID or eq e TLEZERO or eq e TLESCH eq e TLEKUPD
2411ret 47
2412end
2413ret 80
2414end
2415
2416note fail: alert (if any), mark the connection failed, wipe key material, return e
2417fn tls_abort c edit uchar * e ulong to ulong
2418d = tls_alertfor e
2419if ne d 0
2420tls_alert c d
2421end
2422put c TLPH 3
2423tls_wipe c
2424ret e
2425end
2426
2427note ---- public API ----
2428note tls_init: c (at least TLCSZ bytes) for hostname hn; flags: 1 = offer ALPN http/1.1,
2429note 2 = offer x25519 without a share in ClientHello1 (only the X25519MLKEM768 share is sent; a
2430note server without the hybrid must send a HelloRetryRequest for x25519),
2431note 4 = approved-only mode (fips.om: HMAC_DRBG with its self-test and health-tested entropy; see the
2432note header and README.md; x25519 and the hybrid are then neither offered nor accepted),
2433note 8 = revocation (rev.om): offer status_request, check the leaf's stapled OCSP response and refuse a
2434note must-staple leaf without one (tls_chainr).
2435note Hostnames are 1..253 bytes of letters, digits, '-' and '.', and not an IPv4 literal.
2436fn tls_init c edit uchar * hn view uchar * fl ulong to ulong
2437if lt len c TLCSZ
2438ret TLEARG
2439end
2440tls_zero c 0 TLRB
2441hl = len hn
2442if or eq hl 0 gt hl 253
2443ret TLEARG
2444end
2445alpha = ulong 0
2446for i ulong hl
2447k = tls_hch ulong get hn i
2448if eq k 0
2449ret TLEARG
2450end
2451alpha = or alpha and k 1
2452put c add TLHOST i get hn i
2453end
2454if eq alpha 0
2455ret TLEARG
2456end
2457put c TLHL uchar hl
2458put c TLFL uchar and fl 15
2459put c TLGO uchar TLGOFF
2460put c TLGS uchar TLGOFF
2461if ne and fl 2 0
2462put c TLGS uchar and TLGOFF sub 255 TLGX25
2463end
2464if ne and fl 4 0
2465put c TLGO uchar 0
2466put c TLGS uchar 0
2467end
2468ret 0
2469end
2470
2471note hostname byte class: 1 letter, 2 digit, '-' or '.', 0 anything else
2472fn tls_hch ch ulong to ulong
2473if or and ge ch 97 le ch 122 and ge ch 65 le ch 90
2474ret 1
2475end
2476if or and ge ch 48 le ch 57 or eq ch 45 eq ch 46
2477ret 2
2478end
2479ret 0
2480end
2481
2482note load the system trust store /etc/ssl/certs/ca-certificates.crt into st (TLSTSZ bytes)
2483fn tls_store st edit uchar * to ulong
2484r = x509_store_load st
2485if le r 0
2486ret TLESTOR
2487end
2488ret 0
2489end
2490
2491note load trust anchors from the PEM bundle at path instead (tests with a private CA)
2492fn tls_storef st edit uchar * path view uchar * to ulong
2493r = x509_store_loadf st path
2494if le r 0
2495ret TLESTOR
2496end
2497ret 0
2498end
2499
2500note the module's show-version service (fips_version): "Omega TLS module (om/tls) version V integrity
2501note hmac-sha256:HEX state S\n" into o, returns its length (0 when o is too short)
2502fn tls_version o edit uchar * to ulong
2503ret fips_version o
2504end
2505
2506note FIPS 140-3 style self-tests (fips.om): the known-answer tests and then the software integrity
2507note test (HMAC-SHA-256 of the program file, detail 16) run once per process at first
2508note use in every mode; approved mode (flag 4) adds the DRBG self-test and a fresh DRBG at
2509note c[TLDRBG]. 0, or TLESELF with the failing test in tls_detail.
2510fn tls_fips c edit uchar * to ulong
2511ap = ulong and get c TLFL 4
2512d = fips_check ap
2513if ne d 0
2514ret tls_fail c TLESELF d
2515end
2516if eq ap 0
2517ret 0
2518end
2519if ne fips_drbgnew part c TLDRBG DRSZ 0
2520ret tls_fail c TLESELF fips_detail
2521end
2522ret 0
2523end
2524
2525note full handshake on the connected socket fd; 0 or a named error code (tls_errname).
2526note Without a caller deadline (tls_deadline) the whole handshake is bounded by the budget of
2527note tls_hslimit (default TLHSNS, 30 s); that deadline is cleared again once connected.
2528fn tls_connect_hs c edit uchar * st view uchar * to ulong
2529mine = ulong 0
2530if eq tls_gn c TLDL 8 0
2531mine = 1
2532b = tls_gn c TLHSL 8
2533if eq b 0
2534b = TLHSNS
2535end
2536tls_pn c TLDL add ulong monons b 8
2537end
2538th = new ulong 40 0
2539sha256_init th
2540e = tls_mkch c
2541z = scrub 65536
2542if ne e 0
2543ret tls_abort c e
2544end
2545n = add tls_gn c add TLWB 6 3 4
2546sha256_update th part c add TLWB 5 n
2547e = tls_wrbuf c 22 n
2548if eq e 0
2549e = tls_rdsh c th
2550z = scrub 65536
2551end
2552if eq e 0
2553e = tls_rdenc c st th
2554z = scrub 8192
2555end
2556if eq e 0
2557e = tls_cfin c th
2558z = scrub 4096
2559end
2560for i ulong 40
2561put th i 0
2562end
2563if ne e 0
2564ret tls_abort c e
2565end
2566if eq mine 1
2567tls_pn c TLDL 0 8
2568end
2569put c TLPH 1
2570ret 0
2571end
2572
2573fn tls_connect c edit uchar * st view uchar * fd int to ulong
2574if or ne get c TLPH 0 eq get c TLHL 0
2575ret TLESTAT
2576end
2577if lt fd 0
2578ret TLEARG
2579end
2580e = tls_fips c
2581if ne e 0
2582ret e
2583end
2584tls_pn c TLFD ulong fd 4
2585prev = dit 1
2586e = tls_connect_hs c st
2587z = scrub 65536
2588z = dit int prev
2589ret e
2590end
2591
2592note ---- application data ----
2593note KeyUpdate (RFC 8446 section 4.6.3): new read key; answer update_requested once
2594fn tls_kupd c edit uchar * to ulong
2595if ne tls_gn c add TLHB 1 3 1
2596ret TLEDEC
2597end
2598v = ulong get c add TLHB 4
2599if gt v 1
2600ret TLEKUPD
2601end
2602tls_hsdrop c
2603if ne tls_gn c TLHF 4 0
2604ret TLEUNEX
2605end
2606e = new uchar 1 0
2607r = tls_drv c tls_sel TLSAP TLSAP 32 TLIUPD part e 0 0
2608r = or r tls_keys c TLSAP 1
2609if eq v 1
2610x = tls_kusend c
2611if ne x 0
2612ret x
2613end
2614end
2615if ne r 0
2616ret TLEARG
2617end
2618ret 0
2619end
2620
2621note send KeyUpdate(update_not_requested) under the current write key, then switch to the next
2622note write key (RFC 8446 section 4.6.3 and 7.2); 0 or an error code
2623fn tls_kusend c edit uchar * to ulong
2624e = new uchar 1 0
2625b = add TLWB 5
2626put c b 24
2627tls_pn c add b 1 1 3
2628put c add b 4 0
2629x = tls_wrbuf c 22 5
2630if ne x 0
2631ret x
2632end
2633r = tls_drv c tls_sel TLCAP TLCAP 32 TLIUPD part e 0 0
2634r = or r tls_keys c TLCAP 0
2635if ne r 0
2636ret TLEARG
2637end
2638ret 0
2639end
2640
2641note post-handshake handshake records: NewSessionTicket is ignored (no resumption), KeyUpdate
2642note handled, anything else refused
2643fn tls_post c edit uchar * to ulong
2644f = tls_gn c TLHF 4
2645n = tls_gn c add TLRB 3 2
2646if or eq n 0 gt add f n TLHBSZ
2647ret TLEUNEX
2648end
2649tls_mov c add TLHB f add TLRB 5 n
2650f = add f n
2651tls_pn c TLHF f 4
2652while ge f 4 bound 4096
2653ml = add tls_gn c add TLHB 1 3 4
2654if gt ml TLHBSZ
2655ret TLEBIG
2656end
2657if lt f ml
2658ret 0
2659end
2660t = ulong get c TLHB
2661if eq t 4
2662e = tls_nst c
2663if ne e 0
2664ret e
2665end
2666else
2667if ne t 24
2668ret TLEUNEX
2669end
2670e = tls_kupd c
2671if ne e 0
2672ret e
2673end
2674end
2675f = tls_gn c TLHF 4
2676end
2677ret 0
2678end
2679
2680note NewSessionTicket (RFC 8446 4.6.1) at c[TLHB]: lifetime at most 604800 s (illegal_parameter
2681note otherwise), nonce<0..255>, ticket<1..2^16-1>, extensions<0..2^16-2> of well-formed entries,
2682note filling the message exactly (decode_error otherwise); then dropped (no resumption)
2683fn tls_nst c edit uchar * to ulong
2684ln = tls_gn c add TLHB 1 3
2685if lt ln 14
2686ret TLEDEC
2687end
2688if gt tls_gn c add TLHB 4 4 604800
2689ret TLETKT
2690end
2691z = add add TLHB 4 ln
2692p = add add TLHB 13 ulong get c add TLHB 12
2693if gt add p 2 z
2694ret TLEDEC
2695end
2696tl = tls_gn c p 2
2697if eq tl 0
2698ret TLEDEC
2699end
2700p = add add p 2 tl
2701if gt add p 2 z
2702ret TLEDEC
2703end
2704el = tls_gn c p 2
2705p = add p 2
2706if ne add p el z
2707ret TLEDEC
2708end
2709while lt p z bound 16384
2710if gt add p 4 z
2711ret TLEDEC
2712end
2713p = add add p 4 tls_gn c add p 2 2
2714end
2715if ne p z
2716ret TLEDEC
2717end
2718tls_hsdrop c
2719ret 0
2720end
2721
2722note copy pending application bytes into o; returns the count
2723fn tls_take c edit uchar * o edit uchar * to ulong
2724po = tls_gn c TLPO 4
2725pn = tls_gn c TLPN 4
2726n = pn
2727if gt n len o
2728n = len o
2729end
2730for i ulong n
2731put o i get c add add add TLRB 5 po i
2732end
2733tls_pn c TLPO add po n 4
2734tls_pn c TLPN sub pn n 4
2735ret n
2736end
2737
2738note tls_read: decrypted application bytes into o. Returns the count (> 0), 0 at the peer's
2739note close_notify, or minus an error code (a TCP end without close_notify is -TLS_ERR_EOF).
2740note More than 1000 consecutive records without application bytes (empty records, tickets,
2741note KeyUpdates) are refused with -TLS_ERR_UNEXPECTED_MESSAGE.
2742fn tls_read c edit uchar * o edit uchar * to long
2743ph = get c TLPH
2744if eq ph 2
2745ret 0
2746end
2747if ne ph 1
2748ret neg long TLESTAT
2749end
2750if eq len o 0
2751ret neg long TLEARG
2752end
2753k = ulong 0
2754while true bound 999999
2755k = add k 1
2756if gt k 1001
2757ret neg long tls_abort c TLEUNEX
2758end
2759if gt tls_gn c TLPN 4 0
2760ret long tls_take c o
2761end
2762e = tls_rdrec c
2763if eq e 0
2764e = tls_rdapp c
2765end
2766if eq e TLCLOSE
2767put c TLPH 2
2768ret 0
2769end
2770if ne e 0
2771ret neg long tls_abort c e
2772end
2773end
2774ret neg long tls_abort c TLEUNEX
2775end
2776
2777note dispatch one received record after the handshake; application data while a handshake
2778note message is partly received is unexpected_message (RFC 8446 5.1: no interleaving)
2779fn tls_rdapp c edit uchar * to ulong
2780ty = ulong get c TLRB
2781if eq ty 23
2782if ne tls_gn c TLHF 4 0
2783ret TLEUNEX
2784end
2785tls_pn c TLPO 0 4
2786tls_pn c TLPN tls_gn c add TLRB 3 2 4
2787ret 0
2788end
2789if eq ty 21
2790ret tls_ralert c
2791end
2792if eq ty 22
2793ret tls_post c
2794end
2795ret TLEUNEX
2796end
2797
2798note tls_write: send all of m as application data; returns len m or minus an error code.
2799note Before the write key's last allowed record a KeyUpdate is sent and the key rotated
2800note (RFC 8446 section 5.5), so application data never exhausts the record limit.
2801fn tls_write c edit uchar * m view uchar * to long
2802if ne get c TLPH 1
2803ret neg long TLESTAT
2804end
2805off = ulong 0
2806while lt off len m bound 999999
2807if ge tls_gn c TLWS 8 sub tls_reclim c 1
2808e = tls_kusend c
2809if ne e 0
2810ret neg long tls_abort c e
2811end
2812end
2813k = sub len m off
2814if gt k 16384
2815k = 16384
2816end
2817for i ulong k
2818put c add add TLWB 5 i get m add off i
2819end
2820e = tls_wrbuf c 23 k
2821if ne e 0
2822ret neg long tls_abort c e
2823end
2824off = add off k
2825end
2826ret long len m
2827end
2828
2829note tls_close: send close_notify (when connected), wipe the key material and every buffer that
2830note held plaintext, records or certificates. The caller closes fd.
2831note A failed connection stays failed (later calls give TLS_ERR_STATE).
2832fn tls_close c edit uchar * to void
2833if eq get c TLPH 1
2834put c add TLWB 5 1
2835put c add TLWB 6 0
2836e = tls_wrbuf c 21 2
2837end
2838if ne get c TLPH 3
2839put c TLPH 2
2840end
2841tls_wipe c
2842end
2843
2844note zero the secrets (keys, IVs, traffic secrets, ephemeral key, shared secret, transcript
2845note secrets) and the record, write, leaf and handshake buffers; the state words stay
2846fn tls_wipe c edit uchar * to void
2847tls_zero c TLWKEY 96
2848tls_zero c TLCAP 64
2849tls_zero c TLEPK 320
2850tls_zero c TLSS 112
2851tls_zero c TLDRBG DRSZ
2852tls_zero c TLECD2 448
2853tls_zero c TLRB sub TLCSZ TLRB
2854end
2855
2856note ---- data-independent timing (Arm DIT, DDI0487 C5.2.4) ----
2857note tls_hwdit: 1 when the kernel advertises FEAT_DIT in AT_HWCAP (/proc/self/auxv entry 16,
2858note bit 24 = HWCAP_DIT), 0 when it does not, -1 when auxv cannot be read. Whether the bit is
2859note advertised says nothing about PSTATE.DIT, which Linux starts at 0 for every process and
2860note this library cannot set: an `msr DIT, #1` needs a compiler word (see THREAT_MODEL.md).
2861fn tls_hwdit to long
2862b = new uchar 1024 0
2863fd = fopen TLAUXV 0 len TLAUXV 0 0
2864if lt fd 0
2865ret -1
2866end
2867n = long 0
2868r = long 0
2869while lt n 1024 bound 64
2870r = read int fd b ulong n sub 1024 ulong n
2871if le r 0
2872break
2873end
2874n = add n r
2875end
2876r = fclose int fd
2877if lt n 16
2878ret -1
2879end
2880for i ulong div ulong n 16
2881o = mul i 16
2882k = tls_gl b o
2883if eq k 16
2884ret long and shr tls_gl b add o 8 24 1
2885end
2886if eq k 0
2887break
2888end
2889end
2890ret -1
2891end
2892
2893note little-endian 64-bit word at b[o..o+8)
2894fn tls_gl b view uchar * o ulong to ulong
2895v = ulong 0
2896for j ulong 8
2897v = or v shl ulong get b add o j mul j 8
2898end
2899ret v
2900end
2901
2902note ---- DNS A lookup (RFC 1035) over UDP to the first IPv4 nameserver in /etc/resolv.conf ----
2903note Only A records (IPv4). The answer section is walked in order: a CNAME record owned by the
2904note current name (first the question name) makes its target the current name, and the first
2905note IN A record owned by the current name is used; records for any other owner are skipped.
2906note Owner names are compared label by label, ASCII case-insensitive, following compression.
2907note Response checks: id, QR set, opcode 0, TC clear, RCODE 0, one question equal to ours.
2908
2909note parse a dotted IPv4 address (all of b) into ip[0..4); 1 on success
2910fn dns_ip4 b view uchar * ip edit uchar * to ulong
2911k = ulong 0
2912v = ulong 0
2913dg = ulong 0
2914for p ulong len b
2915ch = ulong get b p
2916if and ge ch 48 le ch 57
2917v = add mul v 10 sub ch 48
2918dg = add dg 1
2919if or gt v 255 gt dg 3
2920ret 0
2921end
2922else
2923if or ne ch 46 or eq dg 0 ge k 3
2924ret 0
2925end
2926put ip k uchar v
2927k = add k 1
2928v = 0
2929dg = 0
2930end
2931end
2932if or ne k 3 eq dg 0
2933ret 0
2934end
2935put ip 3 uchar v
2936ret 1
2937end
2938
2939note Return the opened regular file's nonnegative byte size, or -1. Linux AArch64 stat ABI:
2940note asm-generic/stat.h: st_mode at 16 (S_IFMT 61440, S_IFREG 32768), signed st_size at 48.
2941fn dns_fsize fd int to long
2942sb = new uchar 128 0
2943if ne fstat fd sb 0 len sb 0
2944ret -1
2945end
2946if ne and tls_gl sb 16 61440 32768
2947ret -1
2948end
2949ret bitcast long tls_gl sb 48
2950end
2951
2952note Read the initially observed byte extent of an opened regular resolver file, never a stream.
2953note O_NONBLOCK prevents a FIFO open from waiting for a writer; fstat decides type on that same fd.
2954note Appends after fstat do not extend this read. A short EOF, read error or close error refuses,
2955note even after finding an address. This is a bounded extent, not an atomic content snapshot.
2956note The 4096-byte array is an I/O chunk, not a byte cap. Parser state crosses every chunk boundary.
2957fn dns_conf ip edit uchar * to ulong
2958b = new uchar 4096 0
2959a = new uchar 15 0
2960st = new ulong 3 0
2961fd = fopen TLRESOLV 0 len TLRESOLV or int 524288 2048 0
2962if lt fd 0
2963ret TLEDNS
2964end
2965size = dns_fsize int fd
2966if lt size 0
2967x = fclose int fd
2968ret TLEDNS
2969end
2970left = ulong size
2971note Every pass consumes at least one byte of the initial signed-64-bit file extent or refuses.
2972while gt left 0 bound 9223372036854775807
2973ask = left
2974if gt ask len b
2975ask = len b
2976end
2977n = read int fd b 0 ask
2978if le n 0
2979x = fclose int fd
2980ret TLEDNS
2981end
2982left = sub left ulong n
2983for i ulong ulong n bound 4096
2984c = get b i
2985if eq c 10
2986dns_cfgend a st ip
2987else
2988dns_cfgbyte a st c
2989end
2990end
2991end
2992x = fclose int fd
2993if lt x 0
2994ret TLEDNS
2995end
2996dns_cfgend a st ip
2997if eq get st 2 1
2998ret 0
2999end
3000ret TLEDNS
3001end
3002
3003note st: keyword position 0..10, 11 blanks, 12 address, 13 ignored line, 14 address ended;
3004note address length; found-first-address flag. Only one byte of parser state advances per input byte.
3005fn dns_cfgbyte a edit uchar 15 st edit ulong 3 c uchar to void
3006if eq get st 2 1
3007ret
3008end
3009p = get st 0
3010if lt p 10
3011if eq c get TLNS p
3012put st 0 add p 1
3013else
3014put st 0 13
3015end
3016ret
3017end
3018if eq p 10
3019if eq dns_blank ulong c 1
3020put st 0 11
3021else
3022put st 0 13
3023end
3024ret
3025end
3026if eq p 11
3027if eq dns_blank ulong c 1
3028ret
3029end
3030put st 0 12
3031p = 12
3032end
3033if ne p 12
3034ret
3035end
3036if eq dns_blank ulong c 1
3037put st 0 14
3038ret
3039end
3040n = get st 1
3041if eq n len a
3042put st 0 13
3043ret
3044end
3045put a n c
3046put st 1 add n 1
3047end
3048
3049note Finalize at LF or EOF, including a last line without LF. Once found, keep the first address.
3050fn dns_cfgend a view uchar 15 st edit ulong 3 ip edit uchar * to void
3051if eq get st 2 1
3052ret
3053end
3054if or eq get st 0 12 eq get st 0 14
3055if eq dns_ip4 part a 0 get st 1 ip 1
3056put st 2 1
3057ret
3058end
3059end
3060put st 0 0
3061put st 1 0
3062end
3063
3064note b[i] as a ulong, or 256 past the end (never traps)
3065fn tls_at b view uchar * i ulong to ulong
3066if ge i len b
3067ret 256
3068end
3069ret ulong get b i
3070end
3071
3072note 1 for space, tab or CR
3073fn dns_blank ch ulong to ulong
3074if or eq ch 32 or eq ch 9 eq ch 13
3075ret 1
3076end
3077ret 0
3078end
3079
3080note query for hn type A at q[0..); returns its length (0 when hn is not a valid name)
3081fn dns_query q edit uchar * hn view uchar * id ulong to ulong
3082tls_pn q 0 id 2
3083tls_pn q 2 256 2
3084tls_pn q 4 1 2
3085tls_zero q 6 6
3086hl = len hn
3087put q 12 46
3088tls_cpy q 13 hn
3089if eq dns_labels part q 12 add hl 1 0
3090ret 0
3091end
3092p = add 13 hl
3093put q p 0
3094tls_pn q add p 1 1 2
3095tls_pn q add p 3 1 2
3096ret add p 5
3097end
3098
3099note "." + name in b becomes length-prefixed labels, back to front; 1 when every label is 1..63
3100fn dns_labels b edit uchar * to ulong
3101n = ulong 0
3102for i ulong len b
3103k = sub sub len b 1 i
3104if eq get b k 46
3105if or eq n 0 gt n 63
3106ret 0
3107end
3108put b k uchar n
3109n = 0
3110else
3111n = add n 1
3112end
3113end
3114ret 1
3115end
3116
3117note skip a (possibly compressed) name at r[p..z); returns the offset after it, or 0 when it is
3118note malformed or has more than 127 labels (never traps)
3119fn dns_skip r view uchar * p0 ulong z ulong to ulong
3120p = p0
3121k = ulong 0
3122while lt p z bound 128
3123k = add k 1
3124if gt k 127
3125ret 0
3126end
3127l = ulong get r p
3128if eq l 0
3129ret add p 1
3130end
3131if eq and l 192 192
3132if gt add p 2 z
3133ret 0
3134end
3135ret add p 2
3136end
3137if ne and l 192 0
3138ret 0
3139end
3140p = add add p 1 l
3141end
3142ret 0
3143end
3144
3145note answer check and first A record into ip; q is our whole query; 0 or TLEDNS
3146fn dns_parse r view uchar * q view uchar * ip edit uchar * to ulong
3147ql = len q
3148if lt len r ql
3149ret TLEDNS
3150end
3151if ne tls_cteq q r 0 2 0
3152ret TLEDNS
3153end
3154note flags: QR 0x8000 set; opcode 0x7800, TC 0x0200 and RCODE 0x000F clear (mask 0xFA0F)
3155fl = tls_gn r 2 2
3156if ne and fl 64015 32768
3157ret TLEDNS
3158end
3159if ne tls_gn r 4 2 1
3160ret TLEDNS
3161end
3162if ne dns_qeq q r ql 0
3163ret TLEDNS
3164end
3165an = tls_gn r 6 2
3166ret dns_ans r ql an ip
3167end
3168
3169note 0 when the question of r equals ours (ASCII case-insensitive), q[12..ql)
3170fn dns_qeq q view uchar * r view uchar * ql ulong to ulong
3171for i ulong sub ql 12
3172a = ulong get q add 12 i
3173b = ulong get r add 12 i
3174if and ge a 65 le a 90
3175a = add a 32
3176end
3177if and ge b 65 le b 90
3178b = add b 32
3179end
3180if ne a b
3181ret 1
3182end
3183end
3184ret 0
3185end
3186
3187note answer records from r[p0]; the current name starts as the question name at offset 12
3188fn dns_ans r view uchar * p0 ulong an ulong ip edit uchar * to ulong
3189p = p0
3190cur = ulong 12
3191for k ulong an
3192o = p
3193p = dns_rr r o
3194if eq p 0
3195ret TLEDNS
3196end
3197if eq dns_neq r o cur 0
3198t = dns_rrty r o
3199if eq t 1
3200dns_rrip r o ip
3201ret 0
3202end
3203if eq t 5
3204cur = add dns_skip r o len r 10
3205end
3206end
3207end
3208ret TLEDNS
3209end
3210
3211note one resource record at r[p0]: the offset of the next record, or 0 when it is malformed
3212fn dns_rr r view uchar * p0 ulong to ulong
3213z = len r
3214p = dns_skip r p0 z
3215if or eq p 0 gt add p 10 z
3216ret 0
3217end
3218dl = tls_gn r add p 8 2
3219if gt add add p 10 dl z
3220ret 0
3221end
3222ret add add p 10 dl
3223end
3224
3225note copy the 4 data bytes of the (well-formed) A record at r[o] into ip
3226fn dns_rrip r view uchar * o ulong ip edit uchar * to void
3227p = add dns_skip r o len r 10
3228for i ulong 4
3229put ip i get r add p i
3230end
3231end
3232
3233note kind of the (well-formed) record at r[o]: 1 IN A with 4 data bytes, 5 IN CNAME, else 0
3234fn dns_rrty r view uchar * o ulong to ulong
3235p = dns_skip r o len r
3236ty = tls_gn r p 4
3237dl = tls_gn r add p 8 2
3238if and eq ty 65537 eq dl 4
3239ret 1
3240end
3241if eq ty 327681
3242ret 5
3243end
3244ret 0
3245end
3246
3247note the target of a compression pointer at r[a]; a itself when r[a] is no pointer; len r when
3248note the pointer is cut off
3249fn dns_ptr r view uchar * a ulong to ulong
3250x = tls_at r a
3251if ne and x 192 192
3252ret a
3253end
3254if ge add a 1 len r
3255ret len r
3256end
3257ret or shl and x 63 8 ulong get r add a 1
3258end
3259
3260note 0 when the names at r[a0] and r[b0] are equal (ASCII case-insensitive, compression
3261note followed); 1 when they differ or either is malformed (at most 400 steps, never traps)
3262fn dns_neq r view uchar * a0 ulong b0 ulong to ulong
3263a = a0
3264b = b0
3265for k ulong 400
3266na = dns_ptr r a
3267nb = dns_ptr r b
3268if or ne na a ne nb b
3269a = na
3270b = nb
3271else
3272la = tls_at r a
3273if or ne la tls_at r b gt la 63
3274ret 1
3275end
3276if eq la 0
3277ret 0
3278end
3279if ne dns_lbeq r a b la 0
3280ret 1
3281end
3282a = add a add la 1
3283b = add b add la 1
3284end
3285end
3286ret 1
3287end
3288
3289note 0 when the l label bytes after r[a] and r[b] are equal (ASCII case-insensitive, in range)
3290fn dns_lbeq r view uchar * a ulong b ulong l ulong to ulong
3291for i ulong l
3292x = dns_low tls_at r add add a 1 i
3293y = dns_low tls_at r add add b 1 i
3294if or ne x y eq x 256
3295ret 1
3296end
3297end
3298ret 0
3299end
3300
3301fn dns_low x ulong to ulong
3302if and ge x 65 le x 90
3303ret add x 32
3304end
3305ret x
3306end
3307
3308note sockaddr_in for ip:port into sa[0..16)
3309fn tcp_sa sa edit uchar * ip view uchar * port ulong to void
3310tls_zero sa 0 16
3311put sa 0 2
3312tls_pn sa 2 port 2
3313for i ulong 4
3314put sa add 4 i get ip i
3315end
3316end
3317
3318note resolve hn to an IPv4 address in ip[0..4) (two attempts, 3 s each); 0 or TLEDNS. A name
3319note longer than 253 bytes (RFC 1035 2.3.4) is TLEDNS before the 300-byte query is built.
3320fn dns_a4 hn view uchar * ip edit uchar * to ulong
3321if gt len hn 253
3322ret TLEDNS
3323end
3324ns = new uchar 4 0
3325if ne dns_conf ns 0
3326ret TLEDNS
3327end
3328q = new uchar 300 0
3329idb = new uchar 2 0
3330if ne random idb 0 2 0 2
3331ret TLERAND
3332end
3333ql = dns_query q hn tls_gn idb 0 2
3334if eq ql 0
3335ret TLEDNS
3336end
3337for t ulong 2
3338if eq dns_try ns part q 0 ql ip 0
3339ret 0
3340end
3341end
3342ret TLEDNS
3343end
3344
3345fn dns_try ns view uchar * q view uchar * ip edit uchar * to ulong
3346sa = new uchar 16 0
3347tcp_sa sa ns 53
3348fd = socknew 2 524290 0
3349if lt fd 0
3350ret TLEDNS
3351end
3352f = int fd
3353e = ulong TLEDNS
3354if eq connect f sa 0 16 0
3355e = dns_xchg f q ip
3356end
3357x = fclose f
3358ret e
3359end
3360
3361note send the query on the connected UDP socket f and parse one reply (3 s limit)
3362fn dns_xchg f int q view uchar * ip edit uchar * to ulong
3363r = new uchar 4096 0
3364if ne send f q 0 len q 0 long len q
3365ret TLEDNS
3366end
3367w = waitio f 1 3000000000
3368if le w 0
3369ret TLEDNS
3370end
3371n = recv f r 0 4096 0
3372if le n 0
3373ret TLEDNS
3374end
3375ret dns_parse part r 0 ulong n q ip
3376end
3377
3378note TCP connect to ip:port, non-blocking with a 10 s limit; returns fd (>= 0) or minus a code.
3379note TCP_NODELAY is set: TLS hands the kernel whole records, and with Nagle on, the first request
3380note record waits behind the un-acknowledged Finished for the peer's delayed ACK (about 40 ms per
3381note connection on Linux loopback, measured in the TlsProof benchmark). A refusal is ignored: it
3382note only costs latency.
3383fn tcp_open4 ip view uchar * port ulong to long
3384sa = new uchar 16 0
3385tcp_sa sa ip port
3386fd = socknew 2 526337 0
3387if lt fd 0
3388ret neg long TLECONN
3389end
3390f = int fd
3391one = new uchar 4 0
3392put one 0 1
3393x = sockopt f 6 1 one 0 4
3394r = connect f sa 0 16
3395if eq r -115
3396w = waitio f 4 10000000000
3397if le w 0
3398x = fclose f
3399ret neg long TLECONN
3400end
3401r = connect f sa 0 16
3402if eq r -106
3403r = 0
3404end
3405end
3406if ne r 0
3407x = fclose f
3408ret neg long TLECONN
3409end
3410ret fd
3411end
3412
3413note tls_dial: DNS A lookup of hn and a TCP connection to port; fd or minus a code
3414note (a dotted IPv4 literal is used as it is, without DNS; the TLS name is set separately)
3415fn tls_dial hn view uchar * port ulong to long
3416ip = new uchar 4 0
3417if eq dns_ip4 hn ip 0
3418e = dns_a4 hn ip
3419if ne e 0
3420ret neg long e
3421end
3422end
3423ret tcp_open4 ip port
3424end