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