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 / interop.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tests/drv/interop.om: the client against OpenSSL's s_server as a VERIFICATION ORACLE (never linked, never in the |
| 3 | note product). For each certificate type (RSA-2048 leaf signed by an RSA root: RSA-PSS CertificateVerify and a |
| 4 | note PKCS#1 v1.5 certificate signature; ECDSA P-256 leaf signed by a P-384 root: ECDSA CertificateVerify and an |
| 5 | note ecdsa-with-SHA384 certificate signature) and each suite, it starts `s_server -www` on a private loopback port, |
| 6 | note connects as mail.test, sends an HTTP GET and requires the status page s_server writes, naming TLSv1.3 and the |
| 7 | note suite. Then two refusals: a TLS 1.2-only s_server, and an s_server whose only group is P-256. |
| 8 | note usage: build/drv_interop FIXTURE-DIR OPENSSL-PATH |
| 9 | global io_v ulong 16 |
| 10 | global io_dir uchar 4096 |
| 11 | global io_ossl uchar 4096 |
| 12 | global io_pk uchar 8192 |
| 13 | global io_resp uchar 1048576 |
| 14 | text io_host "mail.test" |
| 15 | text io_via "127.0.0.1" |
| 16 | text io_none "" |
| 17 | text io_roots "/roots-both.pem" |
| 18 | text io_rsac "/leaf-good.pem" |
| 19 | text io_rsak "/leaf.key" |
| 20 | text io_ecc "/ec-leaf.pem" |
| 21 | text io_eck "/ec-leaf.key" |
| 22 | text io_a1 "s_server" |
| 23 | text io_a2 "-accept" |
| 24 | text io_a3 "-cert" |
| 25 | text io_a4 "-key" |
| 26 | text io_a5 "-tls1_3" |
| 27 | text io_a6 "-ciphersuites" |
| 28 | text io_a7 "-groups" |
| 29 | text io_a8 "X25519" |
| 30 | text io_a9 "-www" |
| 31 | text io_a10 "-naccept" |
| 32 | text io_a11 "2" |
| 33 | text io_a12 "-quiet" |
| 34 | text io_a13 "-tls1_2" |
| 35 | text io_a14 "P-256" |
| 36 | text io_s1 "TLS_AES_128_GCM_SHA256" |
| 37 | text io_s3 "TLS_CHACHA20_POLY1305_SHA256" |
| 38 | text io_v13 "TLSv1.3" |
| 39 | text io_req "GET / HTTP/1.0\n\n" |
| 40 | text io_devnull "/dev/null" |
| 41 | text io_pass "PASS " |
| 42 | text io_fail "FAIL " |
| 43 | text io_c " cert=" |
| 44 | text io_su " suite=" |
| 45 | text io_res " result=" |
| 46 | text io_al " alert=" |
| 47 | text io_nl "\n" |
| 48 | text io_rsa "rsa" |
| 49 | text io_ec "ecdsa" |
| 50 | text io_t12 "tls12-only-server" |
| 51 | text io_p256 "p256-only-server" |
| 52 | text io_final "PASS interop cases=" |
| 53 | text io_finalf "FAIL interop pass=" |
| 54 | text io_ff " fail=" |
| 55 | |
| 56 | fn io_say t view uchar * to void |
| 57 | k = write 1 t 0 len t |
| 58 | end |
| 59 | fn io_num n ulong to void |
| 60 | d = new uchar 24 0 |
| 61 | v = n |
| 62 | i = ulong 24 |
| 63 | while true bound 30 |
| 64 | i = sub i 1 |
| 65 | put d i uchar add 48 rem v 10 |
| 66 | v = div v 10 |
| 67 | if eq v 0 |
| 68 | break |
| 69 | end |
| 70 | end |
| 71 | k = write 1 d i sub 24 i |
| 72 | end |
| 73 | note ---- the s_server argument packet in io_pk ---- |
| 74 | fn io_pkstart to void |
| 75 | put io_v 4 2 |
| 76 | put io_v 5 0 |
| 77 | end |
| 78 | fn io_pkt t view uchar * to void |
| 79 | p = get io_v 4 |
| 80 | n = len t |
| 81 | put io_pk p uchar and n 255 |
| 82 | put io_pk add p 1 uchar shr n 8 |
| 83 | for i ulong n |
| 84 | put io_pk add add p 2 i get t i |
| 85 | end |
| 86 | put io_v 4 add add p 2 n |
| 87 | put io_v 5 add get io_v 5 1 |
| 88 | end |
| 89 | note a record holding fixture-dir + name |
| 90 | fn io_pkf t view uchar * to void |
| 91 | p = get io_v 4 |
| 92 | d = get io_v 0 |
| 93 | n = add d len t |
| 94 | put io_pk p uchar and n 255 |
| 95 | put io_pk add p 1 uchar shr n 8 |
| 96 | for i ulong d |
| 97 | put io_pk add add p 2 i get io_dir i |
| 98 | end |
| 99 | for i ulong len t |
| 100 | put io_pk add add add p 2 d i get t i |
| 101 | end |
| 102 | put io_v 4 add add p 2 n |
| 103 | put io_v 5 add get io_v 5 1 |
| 104 | end |
| 105 | fn io_pkossl to void |
| 106 | p = get io_v 4 |
| 107 | n = get io_v 1 |
| 108 | put io_pk p uchar and n 255 |
| 109 | put io_pk add p 1 uchar shr n 8 |
| 110 | for i ulong n |
| 111 | put io_pk add add p 2 i get io_ossl i |
| 112 | end |
| 113 | put io_v 4 add add p 2 n |
| 114 | put io_v 5 add get io_v 5 1 |
| 115 | end |
| 116 | fn io_pkdone to void |
| 117 | c = get io_v 5 |
| 118 | put io_pk 0 uchar and c 255 |
| 119 | put io_pk 1 uchar shr c 8 |
| 120 | end |
| 121 | note "127.0.0.1:PORT" as one record |
| 122 | fn io_pkaddr port ulong to void |
| 123 | a = new uchar 32 0 |
| 124 | for i ulong len io_via |
| 125 | put a i get io_via i |
| 126 | end |
| 127 | put a 9 58 |
| 128 | d = new uchar 8 0 |
| 129 | v = port |
| 130 | k = ulong 0 |
| 131 | while true bound 10 |
| 132 | put d k uchar add 48 rem v 10 |
| 133 | k = add k 1 |
| 134 | v = div v 10 |
| 135 | if eq v 0 |
| 136 | break |
| 137 | end |
| 138 | end |
| 139 | for i ulong k |
| 140 | put a add 10 i get d sub sub k 1 i |
| 141 | end |
| 142 | io_pkt part a 0 add 10 k |
| 143 | end |
| 144 | note ---- one s_server process: kind 0 RSA, 1 ECDSA; suite 1 AES-128-GCM, 3 ChaCha; mode 0 normal, 1 TLS 1.2 only, |
| 145 | note 2 P-256 only ---- |
| 146 | fn io_spawn port ulong kind ulong suite ulong mode ulong to long |
| 147 | io_pkstart |
| 148 | io_pkossl |
| 149 | io_pkt io_a1 |
| 150 | io_pkt io_a2 |
| 151 | io_pkaddr port |
| 152 | io_pkt io_a3 |
| 153 | if eq kind 0 |
| 154 | io_pkf io_rsac |
| 155 | else |
| 156 | io_pkf io_ecc |
| 157 | end |
| 158 | io_pkt io_a4 |
| 159 | if eq kind 0 |
| 160 | io_pkf io_rsak |
| 161 | else |
| 162 | io_pkf io_eck |
| 163 | end |
| 164 | io_spawn2 suite mode |
| 165 | io_pkdone |
| 166 | pid = fork |
| 167 | if eq pid 0 |
| 168 | io_child |
| 169 | end |
| 170 | ret pid |
| 171 | end |
| 172 | note the child: standard streams on /dev/null, then the s_server packet |
| 173 | fn io_child to void |
| 174 | nf = fopen io_devnull 0 len io_devnull 2 0 |
| 175 | z = dupfd int nf 0 0 |
| 176 | z = dupfd int nf 1 0 |
| 177 | z = dupfd int nf 2 0 |
| 178 | r = execprog io_pk 0 get io_v 4 |
| 179 | exit 127 |
| 180 | end |
| 181 | fn io_spawn2 suite ulong mode ulong to void |
| 182 | if eq mode 1 |
| 183 | io_pkt io_a13 |
| 184 | else |
| 185 | io_pkt io_a5 |
| 186 | io_pkt io_a6 |
| 187 | if eq suite 1 |
| 188 | io_pkt io_s1 |
| 189 | else |
| 190 | io_pkt io_s3 |
| 191 | end |
| 192 | end |
| 193 | io_pkt io_a7 |
| 194 | if eq mode 2 |
| 195 | io_pkt io_a14 |
| 196 | else |
| 197 | io_pkt io_a8 |
| 198 | end |
| 199 | io_pkt io_a9 |
| 200 | io_pkt io_a10 |
| 201 | io_pkt io_a11 |
| 202 | io_pkt io_a12 |
| 203 | end |
| 204 | note start s_server on a fresh random port and wait until it accepts TCP (a probe connection, hence -naccept 2); |
| 205 | note three tries on three ports. pp[0] = port, pp[1] = pid. false when no s_server came up. |
| 206 | fn io_up kind ulong suite ulong mode ulong pp edit ulong 2 to bool |
| 207 | for t ulong 3 |
| 208 | port = io_port |
| 209 | pid = io_spawn port kind suite mode |
| 210 | if io_probe port |
| 211 | put pp 0 port |
| 212 | put pp 1 ulong pid |
| 213 | ret true |
| 214 | end |
| 215 | z = killpid int pid 9 |
| 216 | io_reap pid |
| 217 | end |
| 218 | ret false |
| 219 | end |
| 220 | fn io_probe port ulong to bool |
| 221 | t0 = monons |
| 222 | while lt sub monons t0 10000000000 bound 100000 |
| 223 | fd = ot_tl_tcp io_via port |
| 224 | if ge fd 0 |
| 225 | z = fclose int fd |
| 226 | ret true |
| 227 | end |
| 228 | z = sleepns 50000000 |
| 229 | end |
| 230 | ret false |
| 231 | end |
| 232 | note one handshake (the server is already up) |
| 233 | fn io_try port ulong to int |
| 234 | ret ot_tl_connect io_host port io_none |
| 235 | end |
| 236 | note GET and read the whole status page into io_resp; its length, or a negative error |
| 237 | fn io_get to long |
| 238 | if not ot_tl_write io_req |
| 239 | ret -23 |
| 240 | end |
| 241 | b = new uchar 65536 0 |
| 242 | n = ulong 0 |
| 243 | while true bound 100000 |
| 244 | r = ot_tl_read b |
| 245 | if lt r 0 |
| 246 | ret r |
| 247 | end |
| 248 | if eq r 0 |
| 249 | break |
| 250 | end |
| 251 | for i ulong ulong r |
| 252 | if lt add n i len io_resp |
| 253 | put io_resp add n i get b i |
| 254 | end |
| 255 | end |
| 256 | n = add n ulong r |
| 257 | end |
| 258 | ot_tl_close |
| 259 | ret long n |
| 260 | end |
| 261 | fn io_has n ulong t view uchar * to bool |
| 262 | m = len t |
| 263 | if lt n m |
| 264 | ret false |
| 265 | end |
| 266 | for p ulong add sub n m 1 |
| 267 | hit = true |
| 268 | for i ulong m |
| 269 | if ne get io_resp add p i get t i |
| 270 | hit = false |
| 271 | break |
| 272 | end |
| 273 | end |
| 274 | if hit |
| 275 | ret true |
| 276 | end |
| 277 | end |
| 278 | ret false |
| 279 | end |
| 280 | fn io_port to ulong |
| 281 | rnd = new uchar 2 0 |
| 282 | k = random rnd 0 2 0 |
| 283 | ret add 33000 rem or shl ulong get rnd 0 8 ulong get rnd 1 15000 |
| 284 | end |
| 285 | fn io_case kind ulong suite ulong to bool |
| 286 | pp = new ulong 2 0 |
| 287 | c = int 1 |
| 288 | pid = long 0 |
| 289 | if io_up kind suite 0 pp |
| 290 | pid = long get pp 1 |
| 291 | c = io_try get pp 0 |
| 292 | end |
| 293 | ok = false |
| 294 | n = long 0 |
| 295 | if eq c 0 |
| 296 | n = io_get |
| 297 | if gt n 0 |
| 298 | ok = io_has ulong n io_v13 |
| 299 | if eq suite 1 |
| 300 | ok = and ok io_has ulong n io_s1 |
| 301 | else |
| 302 | ok = and ok io_has ulong n io_s3 |
| 303 | end |
| 304 | end |
| 305 | end |
| 306 | z = killpid int pid 9 |
| 307 | io_reap pid |
| 308 | if ok |
| 309 | io_say io_pass |
| 310 | else |
| 311 | io_say io_fail |
| 312 | end |
| 313 | if eq kind 0 |
| 314 | io_say io_rsa |
| 315 | else |
| 316 | io_say io_ec |
| 317 | end |
| 318 | io_say io_su |
| 319 | if eq suite 1 |
| 320 | io_say io_s1 |
| 321 | else |
| 322 | io_say io_s3 |
| 323 | end |
| 324 | io_say io_res |
| 325 | io_num ulong c |
| 326 | io_say io_nl |
| 327 | ret ok |
| 328 | end |
| 329 | note a refusal case: the client must stop with error 18 (the server's alert) or a defined refusal, never connect |
| 330 | fn io_refuse mode ulong to bool |
| 331 | pp = new ulong 2 0 |
| 332 | c = int 1 |
| 333 | pid = long 0 |
| 334 | if io_up 0 1 mode pp |
| 335 | pid = long get pp 1 |
| 336 | c = io_try get pp 0 |
| 337 | end |
| 338 | a = ot_tl_alertrecv |
| 339 | z = killpid int pid 9 |
| 340 | io_reap pid |
| 341 | ok = and ne c 0 ne c 1 |
| 342 | if ok |
| 343 | io_say io_pass |
| 344 | else |
| 345 | io_say io_fail |
| 346 | end |
| 347 | if eq mode 1 |
| 348 | io_say io_t12 |
| 349 | else |
| 350 | io_say io_p256 |
| 351 | end |
| 352 | io_say io_res |
| 353 | io_num ulong c |
| 354 | io_say io_al |
| 355 | io_num ulong a |
| 356 | io_say io_nl |
| 357 | ret ok |
| 358 | end |
| 359 | fn io_reap pid long to void |
| 360 | b = new uchar 4 0 |
| 361 | w = waitpid int pid b 0 4 0 |
| 362 | end |
| 363 | fn main to int |
| 364 | k = argread 1 io_dir 0 4000 |
| 365 | k2 = argread 2 io_ossl 0 4000 |
| 366 | if or le k 0 le k2 0 |
| 367 | ret 2 |
| 368 | end |
| 369 | put io_v 0 ulong k |
| 370 | put io_v 1 ulong k2 |
| 371 | p = new uchar 4096 0 |
| 372 | d = get io_v 0 |
| 373 | for i ulong d |
| 374 | put p i get io_dir i |
| 375 | end |
| 376 | for i ulong len io_roots |
| 377 | put p add d i get io_roots i |
| 378 | end |
| 379 | if le ot_tl_roots part p 0 add d len io_roots 0 |
| 380 | ret 2 |
| 381 | end |
| 382 | ok = ot_tl_via io_via |
| 383 | ot_tl_deadline 10000000000 |
| 384 | pass = ulong 0 |
| 385 | fail = ulong 0 |
| 386 | for kind ulong 2 |
| 387 | for s ulong 2 |
| 388 | su = ulong 1 |
| 389 | if eq s 1 |
| 390 | su = 3 |
| 391 | end |
| 392 | if io_case kind su |
| 393 | pass = add pass 1 |
| 394 | else |
| 395 | fail = add fail 1 |
| 396 | end |
| 397 | end |
| 398 | end |
| 399 | for m ulong 2 |
| 400 | if io_refuse add m 1 |
| 401 | pass = add pass 1 |
| 402 | else |
| 403 | fail = add fail 1 |
| 404 | end |
| 405 | end |
| 406 | if eq fail 0 |
| 407 | io_say io_final |
| 408 | io_num pass |
| 409 | io_say io_nl |
| 410 | ret 0 |
| 411 | end |
| 412 | io_say io_finalf |
| 413 | io_num pass |
| 414 | io_say io_ff |
| 415 | io_num fail |
| 416 | io_say io_nl |
| 417 | ret 1 |
| 418 | end |