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
omtls / tests / drv / r8448.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tests/drv/r8448.om: replay of RFC 8448 section 3 ("Simple 1-RTT Handshake") byte for byte. |
| 3 | note The client (src/tls.om, with the test hooks of tests/tlshook.om) is given the trace's ClientHello and X25519 private |
| 4 | note key, and pinned to the trace's server certificate (it has no subjectAltName and is RSA-1024: the replay lowers |
| 5 | note ot_rsa_minbits to 1024; it does not touch name or path validation, which other tests cover). A forked replay server |
| 6 | note sends the trace's server records unchanged and compares every byte the client sends with the trace: |
| 7 | note the ClientHello record, the client Finished record, the client application data record (the trace's 50-byte |
| 8 | note payload) and the client close_notify record. |
| 9 | note The client must also decrypt the server's records (EncryptedExtensions..Finished, NewSessionTicket, application |
| 10 | note data) and return exactly the trace's 50-byte server payload, and its four traffic secrets must equal the trace's. |
| 11 | note usage: build/drv_r8448 tests/vectors/rfc8448/rfc8448.txt |
| 12 | global rf_t uchar 1048576 |
| 13 | global rf_v ulong 16 |
| 14 | global rf_rec uchar 65536 |
| 15 | global rf_off ulong 64 |
| 16 | global rf_sec uchar 256 |
| 17 | text rf_s3 "3. Simple 1-RTT Handshake" |
| 18 | text rf_s4 "4. Resumed 0-RTT Handshake" |
| 19 | text rf_ckey "{client} create an ephemeral x25519 key pair:" |
| 20 | text rf_priv "private key (" |
| 21 | text rf_cch "{client} construct a ClientHello handshake message:" |
| 22 | text rf_chm "ClientHello (" |
| 23 | text rf_scert "{server} construct a Certificate handshake message:" |
| 24 | text rf_certm "Certificate (" |
| 25 | text rf_chs "{client} send handshake record:" |
| 26 | text rf_shs "{server} send handshake record:" |
| 27 | text rf_cap "{client} send application_data record:" |
| 28 | text rf_sap "{server} send application_data record:" |
| 29 | text rf_cal "{client} send alert record:" |
| 30 | text rf_sal "{server} send alert record:" |
| 31 | text rf_complete "complete record (" |
| 32 | text rf_payload "payload (" |
| 33 | text rf_d1 "derive secret \"tls13 c hs traffic\":" |
| 34 | text rf_d2 "derive secret \"tls13 s hs traffic\":" |
| 35 | text rf_d3 "derive secret \"tls13 c ap traffic\":" |
| 36 | text rf_d4 "derive secret \"tls13 s ap traffic\":" |
| 37 | text rf_exp "expanded (" |
| 38 | text rf_host "server" |
| 39 | text rf_via "127.0.0.1" |
| 40 | text rf_none "" |
| 41 | text rf_ok "PASS r8448 cases=" |
| 42 | text rf_bad "FAIL r8448 step=" |
| 43 | text rf_nl "\n" |
| 44 | text rf_s_ch "FAIL ClientHello record differs from the trace\n" |
| 45 | text rf_s_cf "FAIL client Finished record differs from the trace\n" |
| 46 | text rf_s_ca "FAIL client application data record differs from the trace\n" |
| 47 | text rf_s_cl "FAIL client close_notify record differs from the trace\n" |
| 48 | text rf_s_sec "FAIL a traffic secret differs from the trace: " |
| 49 | text rf_s_conn "FAIL the client did not complete the handshake: error " |
| 50 | text rf_s_read "FAIL the client did not return the server's payload\n" |
| 51 | text rf_s_srv "FAIL replay server status " |
| 52 | text rf_s_parse "FAIL cannot find a trace item: " |
| 53 | text rf_colon "):" |
| 54 | |
| 55 | fn rf_say t view uchar * to void |
| 56 | k = write 1 t 0 len t |
| 57 | end |
| 58 | fn rf_num n ulong to void |
| 59 | d = new uchar 24 0 |
| 60 | v = n |
| 61 | i = ulong 24 |
| 62 | while true bound 30 |
| 63 | i = sub i 1 |
| 64 | put d i uchar add 48 rem v 10 |
| 65 | v = div v 10 |
| 66 | if eq v 0 |
| 67 | break |
| 68 | end |
| 69 | end |
| 70 | k = write 1 d i sub 24 i |
| 71 | end |
| 72 | note ---- locating text in the trace ---- |
| 73 | fn rf_at p ulong t view uchar * to bool |
| 74 | n = get rf_v 0 |
| 75 | if gt add p len t n |
| 76 | ret false |
| 77 | end |
| 78 | for i ulong len t |
| 79 | if ne get rf_t add p i get t i |
| 80 | ret false |
| 81 | end |
| 82 | end |
| 83 | ret true |
| 84 | end |
| 85 | note position just after the first occurrence of t at or after p, before e; 0 when absent |
| 86 | fn rf_find p ulong e ulong t view uchar * to ulong |
| 87 | q = p |
| 88 | while lt q e bound 9223372036854775807 |
| 89 | if rf_at q t |
| 90 | ret add q len t |
| 91 | end |
| 92 | q = add q 1 |
| 93 | end |
| 94 | ret 0 |
| 95 | end |
| 96 | note after label "NAME (N octets):" at p: N hex bytes into rf_rec[o..]; returns N, or 0 |
| 97 | fn rf_hex p ulong o ulong to ulong |
| 98 | n = get rf_v 0 |
| 99 | q = p |
| 100 | v = ulong 0 |
| 101 | while lt q n bound 100 |
| 102 | c = get rf_t q |
| 103 | if or lt c 48 gt c 57 |
| 104 | break |
| 105 | end |
| 106 | v = add mul v 10 ulong sub c 48 |
| 107 | q = add q 1 |
| 108 | end |
| 109 | q = rf_find q n rf_colon |
| 110 | if eq q 0 |
| 111 | ret 0 |
| 112 | end |
| 113 | k = ulong 0 |
| 114 | while and lt k v lt add q 1 n bound 9223372036854775807 |
| 115 | x = rf_tok q |
| 116 | if lt x 256 |
| 117 | put rf_rec add o k uchar x |
| 118 | k = add k 1 |
| 119 | q = add q 2 |
| 120 | else |
| 121 | q = add q 1 |
| 122 | end |
| 123 | end |
| 124 | if ne k v |
| 125 | ret 0 |
| 126 | end |
| 127 | ret v |
| 128 | end |
| 129 | note the value of a whitespace-delimited two-hex-digit token at q, or 256 when there is none |
| 130 | fn rf_tok q ulong to ulong |
| 131 | n = get rf_v 0 |
| 132 | a = rf_hv get rf_t q |
| 133 | b = rf_hv get rf_t add q 1 |
| 134 | if or ge a 16 ge b 16 |
| 135 | ret 256 |
| 136 | end |
| 137 | if not rf_isws get rf_t sub q 1 |
| 138 | ret 256 |
| 139 | end |
| 140 | if lt add q 2 n |
| 141 | if not rf_isws get rf_t add q 2 |
| 142 | ret 256 |
| 143 | end |
| 144 | end |
| 145 | ret add mul a 16 b |
| 146 | end |
| 147 | fn rf_isws c uchar to bool |
| 148 | ret or eq c 32 eq c 10 |
| 149 | end |
| 150 | fn rf_hv c uchar to ulong |
| 151 | if and ge c 48 le c 57 |
| 152 | ret ulong sub c 48 |
| 153 | end |
| 154 | if and ge c 97 le c 102 |
| 155 | ret add 10 ulong sub c 97 |
| 156 | end |
| 157 | ret 16 |
| 158 | end |
| 159 | note item: the k-th (1-based) occurrence of heading h in section 3, then the item label lab after it, its bytes at |
| 160 | note rf_rec[o..]; returns the length (0: not found) |
| 161 | fn rf_item h view uchar * k ulong lab view uchar * o ulong to ulong |
| 162 | s = get rf_v 1 |
| 163 | e = get rf_v 2 |
| 164 | p = s |
| 165 | for i ulong k |
| 166 | p = rf_find p e h |
| 167 | if eq p 0 |
| 168 | ret 0 |
| 169 | end |
| 170 | end |
| 171 | q = rf_find p e lab |
| 172 | if eq q 0 |
| 173 | ret 0 |
| 174 | end |
| 175 | ret rf_hex q o |
| 176 | end |
| 177 | |
| 178 | note ---- the replay server (child process) ---- |
| 179 | note slots: 0 CH record, 1 server SH record, 2 server flight record, 3 client Finished record, 4 NewSessionTicket record, |
| 180 | note 5 client app data record, 6 server app data record, 7 client alert record, 8 server alert record; |
| 181 | note rf_off[2i] = offset in rf_rec, rf_off[2i+1] = length |
| 182 | fn rf_slot i ulong o ulong n ulong to void |
| 183 | put rf_off mul i 2 o |
| 184 | put rf_off add mul i 2 1 n |
| 185 | end |
| 186 | fn rf_send fd int i ulong to bool |
| 187 | o = get rf_off mul i 2 |
| 188 | n = get rf_off add mul i 2 1 |
| 189 | b = new uchar 65536 0 |
| 190 | for j ulong n |
| 191 | put b j get rf_rec add o j |
| 192 | end |
| 193 | p = ulong 0 |
| 194 | while lt p n bound 100000 |
| 195 | k = send fd b p sub n p 0 |
| 196 | if le k 0 |
| 197 | ret false |
| 198 | end |
| 199 | p = add p ulong k |
| 200 | end |
| 201 | ret true |
| 202 | end |
| 203 | fn rf_expect fd int i ulong to bool |
| 204 | o = get rf_off mul i 2 |
| 205 | n = get rf_off add mul i 2 1 |
| 206 | b = new uchar 65536 0 |
| 207 | p = ulong 0 |
| 208 | t0 = monons |
| 209 | while lt p n bound 100000 |
| 210 | k = recv fd b p sub n p 0 |
| 211 | if le k 0 |
| 212 | ret false |
| 213 | end |
| 214 | p = add p ulong k |
| 215 | end |
| 216 | for j ulong n |
| 217 | if ne get b j get rf_rec add o j |
| 218 | ret false |
| 219 | end |
| 220 | end |
| 221 | ret true |
| 222 | end |
| 223 | fn rf_server lfd int to void |
| 224 | fd = accept lfd 0 |
| 225 | if lt fd 0 |
| 226 | exit 10 |
| 227 | end |
| 228 | f = int fd |
| 229 | if not rf_expect f 0 |
| 230 | exit 11 |
| 231 | end |
| 232 | if not and rf_send f 1 rf_send f 2 |
| 233 | exit 12 |
| 234 | end |
| 235 | if not rf_expect f 3 |
| 236 | exit 13 |
| 237 | end |
| 238 | if not rf_send f 4 |
| 239 | exit 14 |
| 240 | end |
| 241 | if not rf_expect f 5 |
| 242 | exit 15 |
| 243 | end |
| 244 | if not rf_send f 6 |
| 245 | exit 16 |
| 246 | end |
| 247 | if not rf_expect f 7 |
| 248 | exit 17 |
| 249 | end |
| 250 | ok = rf_send f 8 |
| 251 | exit 0 |
| 252 | end |
| 253 | |
| 254 | note ---- trace extraction ---- |
| 255 | fn rf_extract to ulong |
| 256 | o = ulong 0 |
| 257 | n = rf_item rf_chs 1 rf_complete o |
| 258 | rf_slot 0 o n |
| 259 | o = add o n |
| 260 | if eq n 0 |
| 261 | ret 1 |
| 262 | end |
| 263 | n = rf_item rf_shs 1 rf_complete o |
| 264 | rf_slot 1 o n |
| 265 | o = add o n |
| 266 | if eq n 0 |
| 267 | ret 2 |
| 268 | end |
| 269 | n = rf_item rf_shs 2 rf_complete o |
| 270 | rf_slot 2 o n |
| 271 | o = add o n |
| 272 | if eq n 0 |
| 273 | ret 3 |
| 274 | end |
| 275 | n = rf_item rf_chs 2 rf_complete o |
| 276 | rf_slot 3 o n |
| 277 | o = add o n |
| 278 | if eq n 0 |
| 279 | ret 4 |
| 280 | end |
| 281 | n = rf_item rf_shs 3 rf_complete o |
| 282 | rf_slot 4 o n |
| 283 | o = add o n |
| 284 | if eq n 0 |
| 285 | ret 5 |
| 286 | end |
| 287 | ret rf_extract2 o |
| 288 | end |
| 289 | fn rf_extract2 p ulong to ulong |
| 290 | o = p |
| 291 | n = rf_item rf_cap 1 rf_complete o |
| 292 | rf_slot 5 o n |
| 293 | o = add o n |
| 294 | if eq n 0 |
| 295 | ret 6 |
| 296 | end |
| 297 | n = rf_item rf_sap 1 rf_complete o |
| 298 | rf_slot 6 o n |
| 299 | o = add o n |
| 300 | if eq n 0 |
| 301 | ret 7 |
| 302 | end |
| 303 | n = rf_item rf_cal 1 rf_complete o |
| 304 | rf_slot 7 o n |
| 305 | o = add o n |
| 306 | if eq n 0 |
| 307 | ret 8 |
| 308 | end |
| 309 | n = rf_item rf_sal 1 rf_complete o |
| 310 | rf_slot 8 o n |
| 311 | o = add o n |
| 312 | if eq n 0 |
| 313 | ret 9 |
| 314 | end |
| 315 | note payloads: 9 client app payload, 10 server app payload; 11 ClientHello message; 12 private key; 13 Certificate |
| 316 | n = rf_item rf_cap 1 rf_payload o |
| 317 | rf_slot 9 o n |
| 318 | o = add o n |
| 319 | n = rf_item rf_sap 1 rf_payload o |
| 320 | rf_slot 10 o n |
| 321 | o = add o n |
| 322 | n = rf_item rf_cch 1 rf_chm o |
| 323 | rf_slot 11 o n |
| 324 | o = add o n |
| 325 | n = rf_item rf_ckey 1 rf_priv o |
| 326 | rf_slot 12 o n |
| 327 | o = add o n |
| 328 | n = rf_item rf_scert 1 rf_certm o |
| 329 | rf_slot 13 o n |
| 330 | o = add o n |
| 331 | ret rf_extract3 o |
| 332 | end |
| 333 | fn rf_extract3 p ulong to ulong |
| 334 | o = p |
| 335 | n = rf_item rf_d1 1 rf_exp o |
| 336 | rf_slot 14 o n |
| 337 | o = add o n |
| 338 | n = rf_item rf_d2 1 rf_exp o |
| 339 | rf_slot 15 o n |
| 340 | o = add o n |
| 341 | n = rf_item rf_d3 1 rf_exp o |
| 342 | rf_slot 16 o n |
| 343 | o = add o n |
| 344 | n = rf_item rf_d4 1 rf_exp o |
| 345 | rf_slot 17 o n |
| 346 | for i ulong 9 |
| 347 | if eq get rf_off add mul add 9 i 2 1 0 |
| 348 | ret add 10 i |
| 349 | end |
| 350 | end |
| 351 | ret 0 |
| 352 | end |
| 353 | note copy slot i into b; its length |
| 354 | fn rf_get i ulong b edit uchar 65536 to ulong |
| 355 | o = get rf_off mul i 2 |
| 356 | n = get rf_off add mul i 2 1 |
| 357 | for j ulong n |
| 358 | put b j get rf_rec add o j |
| 359 | end |
| 360 | ret n |
| 361 | end |
| 362 | |
| 363 | note ---- the client (parent) ---- |
| 364 | fn rf_client port ulong to ulong |
| 365 | s = rf_setup |
| 366 | if ne s 0 |
| 367 | ret s |
| 368 | end |
| 369 | ok = ot_tl_via rf_via |
| 370 | c = ot_tl_connect rf_host port rf_none |
| 371 | if ne c 0 |
| 372 | rf_say rf_s_conn |
| 373 | rf_num ulong c |
| 374 | rf_say rf_nl |
| 375 | ret 21 |
| 376 | end |
| 377 | r = rf_secrets |
| 378 | if ne r 0 |
| 379 | ret r |
| 380 | end |
| 381 | r = rf_appdata |
| 382 | ot_tl_close |
| 383 | ret r |
| 384 | end |
| 385 | note the trace's ClientHello and private key, the pinned certificate, secret recording, RSA-1024 allowed |
| 386 | fn rf_setup to ulong |
| 387 | b = new uchar 65536 0 |
| 388 | k = new uchar 65536 0 |
| 389 | n = rf_get 11 b |
| 390 | m = rf_get 12 k |
| 391 | if or lt n 4 ne m 32 |
| 392 | ret 20 |
| 393 | end |
| 394 | pk = new uchar 32 0 |
| 395 | for i ulong 32 |
| 396 | put pk i get k i |
| 397 | end |
| 398 | ot_th_setch part b 4 sub n 4 pk |
| 399 | n = rf_get 13 b |
| 400 | note Certificate message: header 4, context length 1 (0), list length 3, entry length 3, DER |
| 401 | cl = or or shl ulong get b 8 16 shl ulong get b 9 8 ulong get b 10 |
| 402 | ot_th_pin part b 11 cl |
| 403 | ot_th_keylog true |
| 404 | put ot_rsa_minbits 0 1024 |
| 405 | ret 0 |
| 406 | end |
| 407 | note send the trace's client payload, then the next read must return exactly the trace's server payload |
| 408 | fn rf_appdata to ulong |
| 409 | b = new uchar 65536 0 |
| 410 | k = new uchar 65536 0 |
| 411 | n = rf_get 9 b |
| 412 | if not ot_tl_write part b 0 n |
| 413 | ret 22 |
| 414 | end |
| 415 | x = ot_tl_read k |
| 416 | m = rf_get 10 b |
| 417 | if ne x long m |
| 418 | rf_say rf_s_read |
| 419 | ret 23 |
| 420 | end |
| 421 | for i ulong m |
| 422 | if ne get k i get b i |
| 423 | rf_say rf_s_read |
| 424 | ret 23 |
| 425 | end |
| 426 | end |
| 427 | ret 0 |
| 428 | end |
| 429 | note the four traffic secrets recorded by the test hook against the trace |
| 430 | fn rf_secrets to ulong |
| 431 | b = new uchar 65536 0 |
| 432 | for w ulong 4 |
| 433 | n = rf_get add 14 w b |
| 434 | if ne n 32 |
| 435 | ret 24 |
| 436 | end |
| 437 | for i ulong 32 |
| 438 | if ne get b i get ot_th_sec add mul add w 1 64 i |
| 439 | rf_say rf_s_sec |
| 440 | rf_num add w 1 |
| 441 | rf_say rf_nl |
| 442 | ret 25 |
| 443 | end |
| 444 | end |
| 445 | end |
| 446 | ret 0 |
| 447 | end |
| 448 | |
| 449 | fn rf_listen pa edit ulong 1 to long |
| 450 | sa = new uchar 16 0 |
| 451 | rnd = new uchar 2 0 |
| 452 | for tries ulong 50 |
| 453 | k = random rnd 0 2 0 |
| 454 | pt = add 30000 rem or shl ulong get rnd 0 8 ulong get rnd 1 20000 |
| 455 | put sa 0 2 |
| 456 | put sa 2 uchar shr pt 8 |
| 457 | put sa 3 uchar and pt 255 |
| 458 | put sa 4 127 |
| 459 | put sa 7 1 |
| 460 | fd = socknew 2 524289 0 |
| 461 | b = bind int fd sa 0 16 |
| 462 | if eq b 0 |
| 463 | if eq listen int fd 4 0 |
| 464 | put pa 0 pt |
| 465 | ret fd |
| 466 | end |
| 467 | end |
| 468 | z = fclose int fd |
| 469 | end |
| 470 | ret -1 |
| 471 | end |
| 472 | fn main to int |
| 473 | if lt argcnt 2 |
| 474 | ret 2 |
| 475 | end |
| 476 | x = rf_load |
| 477 | if ne x 0 |
| 478 | rf_say rf_s_parse |
| 479 | rf_num x |
| 480 | rf_say rf_nl |
| 481 | ret 1 |
| 482 | end |
| 483 | pa = new ulong 1 0 |
| 484 | lfd = rf_listen pa |
| 485 | if lt lfd 0 |
| 486 | ret 3 |
| 487 | end |
| 488 | pid = fork |
| 489 | if eq pid 0 |
| 490 | rf_server int lfd |
| 491 | exit 99 |
| 492 | end |
| 493 | z = fclose int lfd |
| 494 | ot_tl_deadline 10000000000 |
| 495 | c = rf_client get pa 0 |
| 496 | ret rf_report c rf_reap pid |
| 497 | end |
| 498 | note read the trace named by argument 1 and extract every item; 0 or the missing item's number |
| 499 | fn rf_load to ulong |
| 500 | pb = new uchar 4096 0 |
| 501 | k = argread 1 pb 0 4096 |
| 502 | if le k 0 |
| 503 | ret 100 |
| 504 | end |
| 505 | fd = fopen pb 0 ulong k 0 0 |
| 506 | if lt fd 0 |
| 507 | ret 101 |
| 508 | end |
| 509 | n = ulong 0 |
| 510 | while lt n len rf_t bound 100000 |
| 511 | r = read int fd rf_t n sub len rf_t n |
| 512 | if le r 0 |
| 513 | break |
| 514 | end |
| 515 | n = add n ulong r |
| 516 | end |
| 517 | z = fclose int fd |
| 518 | put rf_v 0 n |
| 519 | s = rf_find 0 n rf_s3 |
| 520 | s = rf_find s n rf_s3 |
| 521 | e = rf_find s n rf_s4 |
| 522 | if or eq s 0 eq e 0 |
| 523 | ret 102 |
| 524 | end |
| 525 | put rf_v 1 s |
| 526 | put rf_v 2 e |
| 527 | ret rf_extract |
| 528 | end |
| 529 | fn rf_reap pid long to ulong |
| 530 | b = new uchar 4 0 |
| 531 | w = waitpid int pid b 0 4 0 |
| 532 | st = or ulong get b 0 shl ulong get b 1 8 |
| 533 | if ne and st 127 0 |
| 534 | ret add 200 and st 127 |
| 535 | end |
| 536 | ret and shr st 8 255 |
| 537 | end |
| 538 | fn rf_report c ulong code ulong to int |
| 539 | if and eq c 0 eq code 0 |
| 540 | rf_say rf_ok |
| 541 | rf_num 18 |
| 542 | rf_say rf_nl |
| 543 | ret 0 |
| 544 | end |
| 545 | rf_say rf_bad |
| 546 | rf_num c |
| 547 | rf_say rf_nl |
| 548 | rf_say rf_s_srv |
| 549 | rf_num code |
| 550 | rf_say rf_nl |
| 551 | if eq code 11 |
| 552 | rf_say rf_s_ch |
| 553 | end |
| 554 | if eq code 13 |
| 555 | rf_say rf_s_cf |
| 556 | end |
| 557 | if eq code 15 |
| 558 | rf_say rf_s_ca |
| 559 | end |
| 560 | if eq code 17 |
| 561 | rf_say rf_s_cl |
| 562 | end |
| 563 | ret 1 |
| 564 | end |