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