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 / hostile.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tests/drv/hostile.om: the client against the hostile test server (tests/srv/tlssrv.om), one forked server per |
| 3 | note case on a random private loopback port. Each case states the client's defined result; the server then reads |
| 4 | note everything the client sends until it closes and counts records that are not alerts: that count must be 0 in every |
| 5 | note refusal case (no application data, so no plaintext, leaves the client after a refusal). A trap or signal in either |
| 6 | note process fails the case. |
| 7 | note usage: build/drv_hostile FIXTURE-DIR (tests/fixtures/srv) |
| 8 | global hd_v ulong 16 |
| 9 | global hd_dir uchar 4096 |
| 10 | text hd_host "mail.test" |
| 11 | text hd_via "127.0.0.1" |
| 12 | text hd_alpn "imap" |
| 13 | text hd_none "" |
| 14 | text hd_ping "ping: the client's application data" |
| 15 | text hd_root "/root.pem" |
| 16 | text hd_key "/leaf-key.txt" |
| 17 | text hd_good "/leaf-good.der" |
| 18 | text hd_other "/leaf-other.der" |
| 19 | text hd_expired "/leaf-expired.der" |
| 20 | text hd_untrusted "/leaf-untrusted.der" |
| 21 | text hd_pass "PASS " |
| 22 | text hd_fail "FAIL " |
| 23 | text hd_got " client=" |
| 24 | text hd_want " want=" |
| 25 | text hd_srv " server-extra-records=" |
| 26 | text hd_st " server-status=" |
| 27 | text hd_nl "\n" |
| 28 | text hd_final "PASS hostile cases=" |
| 29 | text hd_finalf "FAIL hostile pass=" |
| 30 | text hd_ff " fail=" |
| 31 | text hd_n0 "ok" |
| 32 | text hd_n1 "bad-finished" |
| 33 | text hd_n2 "bad-certificate-verify" |
| 34 | text hd_n3 "downgrade-tls12" |
| 35 | text hd_n4 "hello-retry-request" |
| 36 | text hd_n5 "low-order-key-share" |
| 37 | text hd_n6 "unsolicited-extension" |
| 38 | text hd_n7 "plaintext-handshake-after-keys" |
| 39 | text hd_n8 "truncated-record" |
| 40 | text hd_n9 "oversized-record" |
| 41 | text hd_n10 "alert-flood" |
| 42 | text hd_n11 "renegotiation-attempt" |
| 43 | text hd_n12 "bad-record-mac" |
| 44 | text hd_n13 "key-update-both-ways" |
| 45 | text hd_n14 "truncated-handshake-record" |
| 46 | text hd_n15 "oversized-handshake-record" |
| 47 | text hd_n16 "eof-after-server-hello" |
| 48 | text hd_n17 "wrong-certificate-name" |
| 49 | text hd_n18 "expired-certificate" |
| 50 | text hd_n19 "untrusted-root" |
| 51 | text hd_n20 "ok-alpn-aes128" |
| 52 | text hd_n21 "fragmented-flight" |
| 53 | text hd_n22 "eof-without-close-notify" |
| 54 | text hd_n23 "wrong-session-id-echo" |
| 55 | text hd_n24 "pkcs1-certificate-verify" |
| 56 | text hd_n25 "tls13-serverhello-with-downgrade-sentinel" |
| 57 | text hd_n26 "certificate-entry-of-1000001-bytes" |
| 58 | |
| 59 | fn hd_say t view uchar * to void |
| 60 | k = write 1 t 0 len t |
| 61 | end |
| 62 | fn hd_num n long to void |
| 63 | d = new uchar 24 0 |
| 64 | v = n |
| 65 | ng = lt v 0 |
| 66 | if ng |
| 67 | v = neg v |
| 68 | end |
| 69 | i = ulong 24 |
| 70 | while true bound 30 |
| 71 | i = sub i 1 |
| 72 | put d i uchar add 48 rem v 10 |
| 73 | v = div v 10 |
| 74 | if eq v 0 |
| 75 | break |
| 76 | end |
| 77 | end |
| 78 | if ng |
| 79 | i = sub i 1 |
| 80 | put d i 45 |
| 81 | end |
| 82 | k = write 1 d i sub 24 i |
| 83 | end |
| 84 | fn hd_name c ulong to void |
| 85 | if eq c 0 |
| 86 | hd_say hd_n0 |
| 87 | end |
| 88 | if eq c 1 |
| 89 | hd_say hd_n1 |
| 90 | end |
| 91 | if eq c 2 |
| 92 | hd_say hd_n2 |
| 93 | end |
| 94 | if eq c 3 |
| 95 | hd_say hd_n3 |
| 96 | end |
| 97 | if eq c 4 |
| 98 | hd_say hd_n4 |
| 99 | end |
| 100 | if eq c 5 |
| 101 | hd_say hd_n5 |
| 102 | end |
| 103 | if eq c 6 |
| 104 | hd_say hd_n6 |
| 105 | end |
| 106 | if eq c 7 |
| 107 | hd_say hd_n7 |
| 108 | end |
| 109 | if eq c 8 |
| 110 | hd_say hd_n8 |
| 111 | end |
| 112 | if eq c 9 |
| 113 | hd_say hd_n9 |
| 114 | end |
| 115 | if eq c 10 |
| 116 | hd_say hd_n10 |
| 117 | end |
| 118 | hd_name2 c |
| 119 | end |
| 120 | fn hd_name2 c ulong to void |
| 121 | if eq c 11 |
| 122 | hd_say hd_n11 |
| 123 | end |
| 124 | if eq c 12 |
| 125 | hd_say hd_n12 |
| 126 | end |
| 127 | if eq c 13 |
| 128 | hd_say hd_n13 |
| 129 | end |
| 130 | if eq c 14 |
| 131 | hd_say hd_n14 |
| 132 | end |
| 133 | if eq c 15 |
| 134 | hd_say hd_n15 |
| 135 | end |
| 136 | if eq c 16 |
| 137 | hd_say hd_n16 |
| 138 | end |
| 139 | if eq c 17 |
| 140 | hd_say hd_n17 |
| 141 | end |
| 142 | if eq c 18 |
| 143 | hd_say hd_n18 |
| 144 | end |
| 145 | if eq c 19 |
| 146 | hd_say hd_n19 |
| 147 | end |
| 148 | if eq c 20 |
| 149 | hd_say hd_n20 |
| 150 | end |
| 151 | if eq c 21 |
| 152 | hd_say hd_n21 |
| 153 | end |
| 154 | if eq c 22 |
| 155 | hd_say hd_n22 |
| 156 | end |
| 157 | if eq c 23 |
| 158 | hd_say hd_n23 |
| 159 | end |
| 160 | if eq c 24 |
| 161 | hd_say hd_n24 |
| 162 | end |
| 163 | if eq c 25 |
| 164 | hd_say hd_n25 |
| 165 | end |
| 166 | if eq c 26 |
| 167 | hd_say hd_n26 |
| 168 | end |
| 169 | end |
| 170 | note path = fixture dir + name, into p; length |
| 171 | fn hd_path p edit uchar 4096 t view uchar * to ulong |
| 172 | n = get hd_v 0 |
| 173 | for i ulong n |
| 174 | put p i get hd_dir i |
| 175 | end |
| 176 | for i ulong len t |
| 177 | put p add n i get t i |
| 178 | end |
| 179 | ret add n len t |
| 180 | end |
| 181 | |
| 182 | note case table: server mode, leaf file (0 good, 1 other, 2 expired, 3 untrusted), forced suite, fragment size, |
| 183 | note expected connect result, expected read result (0: the echo must work; checked only when connect gives 0) |
| 184 | fn hd_case c ulong t edit long 6 to void |
| 185 | for i ulong 6 |
| 186 | put t i 0 |
| 187 | end |
| 188 | put t 0 long c |
| 189 | put t 3 16384 |
| 190 | if le c 16 |
| 191 | put t 0 long c |
| 192 | end |
| 193 | if eq c 1 |
| 194 | put t 4 17 |
| 195 | end |
| 196 | if eq c 2 |
| 197 | put t 4 16 |
| 198 | end |
| 199 | if eq c 3 |
| 200 | put t 4 8 |
| 201 | end |
| 202 | if eq c 4 |
| 203 | put t 4 10 |
| 204 | end |
| 205 | if eq c 5 |
| 206 | put t 4 9 |
| 207 | end |
| 208 | if eq c 6 |
| 209 | put t 4 11 |
| 210 | end |
| 211 | if eq c 7 |
| 212 | put t 4 5 |
| 213 | end |
| 214 | if eq c 8 |
| 215 | put t 5 -3 |
| 216 | end |
| 217 | if eq c 9 |
| 218 | put t 5 -4 |
| 219 | end |
| 220 | if eq c 10 |
| 221 | put t 5 -18 |
| 222 | end |
| 223 | hd_case2 c t |
| 224 | end |
| 225 | fn hd_case2 c ulong t edit long 6 to void |
| 226 | if eq c 11 |
| 227 | put t 5 -21 |
| 228 | end |
| 229 | if eq c 12 |
| 230 | put t 5 -7 |
| 231 | end |
| 232 | if eq c 14 |
| 233 | put t 4 3 |
| 234 | end |
| 235 | if eq c 15 |
| 236 | put t 4 4 |
| 237 | end |
| 238 | if eq c 16 |
| 239 | put t 4 3 |
| 240 | end |
| 241 | if eq c 17 |
| 242 | put t 0 0 |
| 243 | put t 1 1 |
| 244 | put t 4 12 |
| 245 | end |
| 246 | if eq c 18 |
| 247 | put t 0 0 |
| 248 | put t 1 2 |
| 249 | put t 4 13 |
| 250 | end |
| 251 | if eq c 19 |
| 252 | put t 0 0 |
| 253 | put t 1 3 |
| 254 | put t 4 14 |
| 255 | end |
| 256 | if eq c 20 |
| 257 | put t 0 0 |
| 258 | put t 2 4865 |
| 259 | end |
| 260 | if eq c 21 |
| 261 | put t 0 0 |
| 262 | put t 3 7 |
| 263 | end |
| 264 | if eq c 22 |
| 265 | put t 0 17 |
| 266 | put t 5 -3 |
| 267 | end |
| 268 | if eq c 23 |
| 269 | put t 0 18 |
| 270 | put t 4 9 |
| 271 | end |
| 272 | if eq c 24 |
| 273 | put t 0 19 |
| 274 | put t 4 9 |
| 275 | end |
| 276 | if eq c 25 |
| 277 | put t 0 20 |
| 278 | put t 4 8 |
| 279 | end |
| 280 | if eq c 26 |
| 281 | put t 0 21 |
| 282 | put t 4 15 |
| 283 | end |
| 284 | end |
| 285 | |
| 286 | note the server process: load, accept one connection, run the mode, drain, exit with the extra-record count |
| 287 | fn hd_server lfd int t view long 6 to void |
| 288 | p = new uchar 4096 0 |
| 289 | q = new uchar 4096 0 |
| 290 | lf = get t 1 |
| 291 | n = ulong 0 |
| 292 | if eq lf 0 |
| 293 | n = hd_path p hd_good |
| 294 | end |
| 295 | if eq lf 1 |
| 296 | n = hd_path p hd_other |
| 297 | end |
| 298 | if eq lf 2 |
| 299 | n = hd_path p hd_expired |
| 300 | end |
| 301 | if eq lf 3 |
| 302 | n = hd_path p hd_untrusted |
| 303 | end |
| 304 | m = hd_path q hd_key |
| 305 | if not ot_ts_load part p 0 n part q 0 m |
| 306 | exit 90 |
| 307 | end |
| 308 | ot_ts_mode ulong get t 0 ulong get t 2 ulong get t 3 |
| 309 | fd = accept lfd 0 |
| 310 | if lt fd 0 |
| 311 | exit 91 |
| 312 | end |
| 313 | z = fclose lfd |
| 314 | c = ot_ts_accept int fd |
| 315 | md = get ot_ts_v 0 |
| 316 | early = or or eq md OT_TS_DOWNGRADE eq md OT_TS_HRR eq md OT_TS_EARLYEOF |
| 317 | if eq md OT_TS_EARLYEOF |
| 318 | z = shutdown int fd 1 |
| 319 | end |
| 320 | if and eq c 0 not early |
| 321 | hd_serve |
| 322 | end |
| 323 | bad = ot_ts_drain |
| 324 | z = fclose int fd |
| 325 | if gt bad 80 |
| 326 | exit 80 |
| 327 | end |
| 328 | exit int bad |
| 329 | end |
| 330 | fn hd_serve to void |
| 331 | m = get ot_ts_v 0 |
| 332 | if eq m OT_TS_KEYUPD |
| 333 | if not ot_tl_keyupd 1 |
| 334 | exit 92 |
| 335 | end |
| 336 | end |
| 337 | if or or or eq m OT_TS_TRUNC eq m OT_TS_OVERSIZE or eq m OT_TS_ALERTS eq m OT_TS_RENEG eq m OT_TS_BADMAC |
| 338 | ot_ts_hostile |
| 339 | ret |
| 340 | end |
| 341 | if eq m OT_TS_NOCLOSE |
| 342 | note end the stream without close_notify: the client must report truncation, not end of data |
| 343 | put ot_ts_v 8 1 |
| 344 | z = shutdown ot_tl_fd 1 |
| 345 | ret |
| 346 | end |
| 347 | b = new uchar 4096 0 |
| 348 | while true bound 1000 |
| 349 | r = ot_tl_read b |
| 350 | if le r 0 |
| 351 | break |
| 352 | end |
| 353 | if not ot_tl_write part b 0 ulong r |
| 354 | break |
| 355 | end |
| 356 | end |
| 357 | ot_tl_close |
| 358 | end |
| 359 | |
| 360 | note the client side of one case: connect result, then (when it connected) the read result |
| 361 | fn hd_client port ulong t view long 6 r edit long 2 to void |
| 362 | p = new uchar 4096 0 |
| 363 | n = hd_path p hd_root |
| 364 | k = ot_tl_roots part p 0 n |
| 365 | if le k 0 |
| 366 | put r 0 999 |
| 367 | ret |
| 368 | end |
| 369 | ok = ot_tl_via hd_via |
| 370 | c = long 0 |
| 371 | if eq get t 2 4865 |
| 372 | c = long ot_tl_connect hd_host port hd_alpn |
| 373 | else |
| 374 | c = long ot_tl_connect hd_host port hd_none |
| 375 | end |
| 376 | put r 0 c |
| 377 | put r 1 0 |
| 378 | if ne c 0 |
| 379 | ret |
| 380 | end |
| 381 | b = new uchar 4096 0 |
| 382 | m = get t 0 |
| 383 | if or eq m 0 eq m 13 |
| 384 | if not ot_tl_write hd_ping |
| 385 | put r 1 -23 |
| 386 | ret |
| 387 | end |
| 388 | end |
| 389 | x = ot_tl_read b |
| 390 | if gt x 0 |
| 391 | note an echo must return the same bytes |
| 392 | if ne ulong x len hd_ping |
| 393 | put r 1 -100 |
| 394 | end |
| 395 | for i ulong len hd_ping |
| 396 | if ne get b i get hd_ping i |
| 397 | put r 1 -101 |
| 398 | end |
| 399 | end |
| 400 | ot_tl_close |
| 401 | note after close, read and write give defined refusals (-22 / false), never a trap |
| 402 | if ne ot_tl_read b -22 |
| 403 | put r 1 -103 |
| 404 | end |
| 405 | if ot_tl_write hd_ping |
| 406 | put r 1 -104 |
| 407 | end |
| 408 | ret |
| 409 | end |
| 410 | if lt x 0 |
| 411 | put r 1 x |
| 412 | ret |
| 413 | end |
| 414 | put r 1 -102 |
| 415 | end |
| 416 | |
| 417 | fn hd_listen port edit ulong 1 to long |
| 418 | sa = new uchar 16 0 |
| 419 | rnd = new uchar 2 0 |
| 420 | one = new uchar 4 0 |
| 421 | put one 0 1 |
| 422 | for tries ulong 50 |
| 423 | k = random rnd 0 2 0 |
| 424 | pt = add 20000 rem or shl ulong get rnd 0 8 ulong get rnd 1 20000 |
| 425 | put sa 0 2 |
| 426 | put sa 2 uchar shr pt 8 |
| 427 | put sa 3 uchar and pt 255 |
| 428 | put sa 4 127 |
| 429 | put sa 7 1 |
| 430 | fd = socknew 2 524289 0 |
| 431 | if lt fd 0 |
| 432 | ret -1 |
| 433 | end |
| 434 | note SO_REUSEADDR |
| 435 | z = sockopt int fd 1 2 one 0 4 |
| 436 | b = bind int fd sa 0 16 |
| 437 | if eq b 0 |
| 438 | l = listen int fd 4 |
| 439 | if eq l 0 |
| 440 | put port 0 pt |
| 441 | ret fd |
| 442 | end |
| 443 | end |
| 444 | z = fclose int fd |
| 445 | end |
| 446 | ret -1 |
| 447 | end |
| 448 | fn hd_run c ulong to bool |
| 449 | t = new long 6 0 |
| 450 | hd_case c t |
| 451 | pa = new ulong 1 0 |
| 452 | lfd = hd_listen pa |
| 453 | if lt lfd 0 |
| 454 | ret false |
| 455 | end |
| 456 | pid = fork |
| 457 | if eq pid 0 |
| 458 | hd_server int lfd t |
| 459 | exit 99 |
| 460 | end |
| 461 | z = fclose int lfd |
| 462 | r = new long 2 0 |
| 463 | hd_client get pa 0 t r |
| 464 | st = hd_wait pid |
| 465 | want0 = get t 4 |
| 466 | want1 = get t 5 |
| 467 | ok = and eq get r 0 want0 eq st 0 |
| 468 | if and ok eq want0 0 |
| 469 | ok = eq get r 1 want1 |
| 470 | end |
| 471 | if ok |
| 472 | hd_say hd_pass |
| 473 | else |
| 474 | hd_say hd_fail |
| 475 | end |
| 476 | hd_name c |
| 477 | hd_say hd_got |
| 478 | hd_num get r 0 |
| 479 | hd_say hd_want |
| 480 | hd_num want0 |
| 481 | if eq want0 0 |
| 482 | hd_say hd_got |
| 483 | hd_num get r 1 |
| 484 | hd_say hd_want |
| 485 | hd_num want1 |
| 486 | end |
| 487 | hd_say hd_st |
| 488 | hd_num long st |
| 489 | hd_say hd_nl |
| 490 | ret ok |
| 491 | end |
| 492 | note the server's exit status (its extra-record count), or 200+ for a signal |
| 493 | fn hd_wait pid long to ulong |
| 494 | b = new uchar 4 0 |
| 495 | t0 = monons |
| 496 | while true bound 9223372036854775807 |
| 497 | w = waitpid int pid b 0 4 1 |
| 498 | if eq w pid |
| 499 | st = or ulong get b 0 shl ulong get b 1 8 |
| 500 | if eq and st 127 0 |
| 501 | ret and shr st 8 255 |
| 502 | end |
| 503 | ret add 200 and st 127 |
| 504 | end |
| 505 | if gt sub monons t0 60000000000 |
| 506 | z = killpid int pid 9 |
| 507 | w = waitpid int pid b 0 4 0 |
| 508 | ret 255 |
| 509 | end |
| 510 | z = sleepns 1000000 |
| 511 | end |
| 512 | ret 255 |
| 513 | end |
| 514 | fn main to int |
| 515 | if lt argcnt 2 |
| 516 | ret 2 |
| 517 | end |
| 518 | k = argread 1 hd_dir 0 4000 |
| 519 | if le k 0 |
| 520 | ret 2 |
| 521 | end |
| 522 | put hd_v 0 ulong k |
| 523 | ot_tl_deadline 10000000000 |
| 524 | pass = ulong 0 |
| 525 | fail = ulong 0 |
| 526 | for c ulong 27 |
| 527 | if hd_run c |
| 528 | pass = add pass 1 |
| 529 | else |
| 530 | fail = add fail 1 |
| 531 | end |
| 532 | end |
| 533 | if eq fail 0 |
| 534 | hd_say hd_final |
| 535 | hd_num long pass |
| 536 | hd_say hd_nl |
| 537 | ret 0 |
| 538 | end |
| 539 | hd_say hd_finalf |
| 540 | hd_num long pass |
| 541 | hd_say hd_ff |
| 542 | hd_num long fail |
| 543 | hd_say hd_nl |
| 544 | ret 1 |
| 545 | end |