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