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 / srv / tlssrv.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tests/srv/tlssrv.om: a TLS 1.3 SERVER for tests only (prefix ot_ts_). Never part of a product build. |
| 3 | note It reuses the client's record layer and key schedule (src/tls.om: the same functions serve both directions |
| 4 | note because a process holds one connection) and signs CertificateVerify with RSA-PSS-SHA256 using a test key |
| 5 | note (tests/fixtures/srv/leaf-key.txt) through ot_bn_modexp. A mode makes it hostile (see ot_ts_modes below). |
| 6 | note Uses: tests/drv/hostile.om (the refusal cases), the interop and speed drivers, and mailhand's fake IMAP/SMTP |
| 7 | note servers (ot_ts_load, ot_ts_accept, then ot_tl_read / ot_tl_write / ot_tl_close as for a client). |
| 8 | global ot_ts_v ulong 32 |
| 9 | global ot_ts_chain uchar 1048576 |
| 10 | global ot_ts_cl ulong 16 |
| 11 | global ot_ts_n uchar 1024 |
| 12 | global ot_ts_d uchar 1024 |
| 13 | global ot_ts_ob uchar 4194304 |
| 14 | global ot_ts_cpub uchar 32 |
| 15 | global ot_ts_buf uchar 1048576 |
| 16 | text ot_ts_t_mod "modulus:" |
| 17 | text ot_ts_t_pexp "privateExponent:" |
| 18 | text ot_ts_t_cv "TLS 1.3, server CertificateVerify" |
| 19 | text ot_ts_t_hrr "HelloRetryRequest" |
| 20 | note ot_ts_v: 0 mode, 1 forced suite (0 = server preference), 2 certificates, 3 modulus bytes, 4 output length, |
| 21 | note 5 fragment size, 6 client offered x25519 share at (position), 7 suite chosen, 8 hostile action done, |
| 22 | note 9 records seen after the hostile action that were not alerts (plaintext evidence), 10 ALPN position+1, 11 ALPN len |
| 23 | const OT_TS_OK ulong 0 |
| 24 | const OT_TS_BADFIN ulong 1 |
| 25 | const OT_TS_BADCV ulong 2 |
| 26 | const OT_TS_DOWNGRADE ulong 3 |
| 27 | const OT_TS_HRR ulong 4 |
| 28 | const OT_TS_ZEROKEY ulong 5 |
| 29 | const OT_TS_UNSOLEXT ulong 6 |
| 30 | const OT_TS_PLAINHS ulong 7 |
| 31 | const OT_TS_TRUNC ulong 8 |
| 32 | const OT_TS_OVERSIZE ulong 9 |
| 33 | const OT_TS_ALERTS ulong 10 |
| 34 | const OT_TS_RENEG ulong 11 |
| 35 | const OT_TS_BADMAC ulong 12 |
| 36 | const OT_TS_KEYUPD ulong 13 |
| 37 | const OT_TS_HSTRUNC ulong 14 |
| 38 | const OT_TS_HSOVER ulong 15 |
| 39 | const OT_TS_EARLYEOF ulong 16 |
| 40 | const OT_TS_NOCLOSE ulong 17 |
| 41 | const OT_TS_BADSID ulong 18 |
| 42 | const OT_TS_PKCS1CV ulong 19 |
| 43 | const OT_TS_SENT13 ulong 20 |
| 44 | const OT_TS_BIGCERT ulong 21 |
| 45 | |
| 46 | note ======== loading the certificate chain (DER files) and the RSA key (openssl text dump) ======== |
| 47 | fn ot_ts_readfile path view uchar * to long |
| 48 | fd = fopen path 0 len path 0 0 |
| 49 | if lt fd 0 |
| 50 | ret -1 |
| 51 | end |
| 52 | n = ulong 0 |
| 53 | while lt n len ot_ts_buf bound 9223372036854775807 |
| 54 | k = read int fd ot_ts_buf n sub len ot_ts_buf n |
| 55 | if eq k -4 |
| 56 | continue |
| 57 | end |
| 58 | if le k 0 |
| 59 | break |
| 60 | end |
| 61 | n = add n ulong k |
| 62 | end |
| 63 | z = fclose int fd |
| 64 | ret long n |
| 65 | end |
| 66 | fn ot_ts_addcert path view uchar * to bool |
| 67 | n = ot_ts_readfile path |
| 68 | if le n 0 |
| 69 | ret false |
| 70 | end |
| 71 | c = get ot_ts_v 2 |
| 72 | o = ulong 0 |
| 73 | for i ulong c |
| 74 | o = add o get ot_ts_cl i |
| 75 | end |
| 76 | for i ulong ulong n |
| 77 | put ot_ts_chain add o i get ot_ts_buf i |
| 78 | end |
| 79 | put ot_ts_cl c ulong n |
| 80 | put ot_ts_v 2 add c 1 |
| 81 | ret true |
| 82 | end |
| 83 | fn ot_ts_hexv c uchar to ulong |
| 84 | if and ge c 48 le c 57 |
| 85 | ret ulong sub c 48 |
| 86 | end |
| 87 | if and ge c 97 le c 102 |
| 88 | ret add 10 ulong sub c 97 |
| 89 | end |
| 90 | ret 16 |
| 91 | end |
| 92 | note the hex block after label (lines of "xx:xx:..."), leading zero bytes dropped, into ot_ts_n (which 0) or ot_ts_d (1) |
| 93 | fn ot_ts_block n ulong label view uchar * which ulong to ulong |
| 94 | p = ot_ts_findlab n label |
| 95 | if eq p 0 |
| 96 | ret 0 |
| 97 | end |
| 98 | put ot_ts_v 12 0 |
| 99 | while lt add p 1 n bound 9223372036854775807 |
| 100 | if eq get ot_ts_buf p 10 |
| 101 | if ne get ot_ts_buf add p 1 32 |
| 102 | break |
| 103 | end |
| 104 | end |
| 105 | if ot_ts_pair p which |
| 106 | p = add p 2 |
| 107 | else |
| 108 | p = add p 1 |
| 109 | end |
| 110 | end |
| 111 | ret get ot_ts_v 12 |
| 112 | end |
| 113 | note the position after the first occurrence of label in ot_ts_buf[0, n), or 0 |
| 114 | fn ot_ts_findlab n ulong label view uchar * to ulong |
| 115 | m = len label |
| 116 | p = ulong 0 |
| 117 | while lt add p m n bound 9223372036854775807 |
| 118 | hit = true |
| 119 | for i ulong m |
| 120 | if ne get ot_ts_buf add p i get label i |
| 121 | hit = false |
| 122 | break |
| 123 | end |
| 124 | end |
| 125 | if hit |
| 126 | ret add p m |
| 127 | end |
| 128 | p = add p 1 |
| 129 | end |
| 130 | ret 0 |
| 131 | end |
| 132 | note one hex pair at p into the next byte (leading zero bytes dropped); false when p is not a hex pair |
| 133 | fn ot_ts_pair p ulong which ulong to bool |
| 134 | hi = ot_ts_hexv get ot_ts_buf p |
| 135 | lo = ot_ts_hexv get ot_ts_buf add p 1 |
| 136 | if or ge hi 16 ge lo 16 |
| 137 | ret false |
| 138 | end |
| 139 | b = add mul hi 16 lo |
| 140 | k = get ot_ts_v 12 |
| 141 | if and eq k 0 eq b 0 |
| 142 | ret true |
| 143 | end |
| 144 | ot_ts_put which k uchar b |
| 145 | put ot_ts_v 12 add k 1 |
| 146 | ret true |
| 147 | end |
| 148 | fn ot_ts_put which ulong k ulong b uchar to void |
| 149 | if eq which 0 |
| 150 | put ot_ts_n k b |
| 151 | else |
| 152 | put ot_ts_d k b |
| 153 | end |
| 154 | end |
| 155 | note load: chain = DER file paths separated by ',' (leaf first), key = the openssl rsa -text dump |
| 156 | fn ot_ts_load chain view uchar * key view uchar * to bool |
| 157 | put ot_ts_v 2 0 |
| 158 | s = ulong 0 |
| 159 | for i ulong add len chain 1 |
| 160 | brk = true |
| 161 | if lt i len chain |
| 162 | brk = eq get chain i 44 |
| 163 | end |
| 164 | if brk |
| 165 | if not ot_ts_addpart chain s sub i s |
| 166 | ret false |
| 167 | end |
| 168 | s = add i 1 |
| 169 | end |
| 170 | end |
| 171 | ret ot_ts_loadkey key |
| 172 | end |
| 173 | fn ot_ts_addpart chain view uchar * s ulong l ulong to bool |
| 174 | if or eq l 0 gt l 4096 |
| 175 | ret false |
| 176 | end |
| 177 | pb = new uchar 4096 0 |
| 178 | for j ulong l |
| 179 | put pb j get chain add s j |
| 180 | end |
| 181 | ret ot_ts_addcert part pb 0 l |
| 182 | end |
| 183 | fn ot_ts_loadkey key view uchar * to bool |
| 184 | n = ot_ts_readfile key |
| 185 | if le n 0 |
| 186 | ret false |
| 187 | end |
| 188 | nb = ot_ts_block ulong n ot_ts_t_mod 0 |
| 189 | db = ot_ts_block ulong n ot_ts_t_pexp 1 |
| 190 | if or eq nb 0 gt db nb |
| 191 | ret false |
| 192 | end |
| 193 | note right-align d to the modulus length |
| 194 | if lt db nb |
| 195 | sh = sub nb db |
| 196 | for i ulong db |
| 197 | j = sub sub db 1 i |
| 198 | put ot_ts_d add j sh get ot_ts_d j |
| 199 | end |
| 200 | for i ulong sh |
| 201 | put ot_ts_d i 0 |
| 202 | end |
| 203 | end |
| 204 | put ot_ts_v 3 nb |
| 205 | put ot_ts_v 5 16384 |
| 206 | ret true |
| 207 | end |
| 208 | fn ot_ts_mode m ulong suite ulong frag ulong to void |
| 209 | put ot_ts_v 0 m |
| 210 | put ot_ts_v 1 suite |
| 211 | if gt frag 0 |
| 212 | put ot_ts_v 5 frag |
| 213 | end |
| 214 | end |
| 215 | |
| 216 | note ======== output buffering of handshake messages (sent as records of the fragment size) ======== |
| 217 | fn ot_ts_msg tr edit shactx t ulong b view uchar * to void |
| 218 | n = len b |
| 219 | put ot_tl_v OT_TL_HTYP t |
| 220 | put ot_tl_v OT_TL_HLEN n |
| 221 | for i ulong n bound 16777215 |
| 222 | put ot_tl_hm i get b i |
| 223 | end |
| 224 | ot_tl_tradd tr |
| 225 | o = get ot_ts_v 4 |
| 226 | put ot_ts_ob o uchar t |
| 227 | put ot_ts_ob add o 1 uchar shr n 16 |
| 228 | put ot_ts_ob add o 2 uchar and shr n 8 255 |
| 229 | put ot_ts_ob add o 3 uchar and n 255 |
| 230 | for i ulong n bound 16777215 |
| 231 | put ot_ts_ob add add o 4 i get b i |
| 232 | end |
| 233 | put ot_ts_v 4 add add o 4 n |
| 234 | end |
| 235 | fn ot_ts_flush to bool |
| 236 | n = get ot_ts_v 4 |
| 237 | f = get ot_ts_v 5 |
| 238 | c = new uchar 16384 0 |
| 239 | p = ulong 0 |
| 240 | while lt p n bound 9223372036854775807 |
| 241 | m = sub n p |
| 242 | if gt m f |
| 243 | m = f |
| 244 | end |
| 245 | for i ulong m |
| 246 | put c i get ot_ts_ob add p i |
| 247 | end |
| 248 | if not ot_tl_wrec 22 part c 0 m |
| 249 | ret false |
| 250 | end |
| 251 | p = add p m |
| 252 | end |
| 253 | put ot_ts_v 4 0 |
| 254 | ret true |
| 255 | end |
| 256 | |
| 257 | note ======== ClientHello ======== |
| 258 | note find the x25519 key share and ALPN; choose the suite; 0 or a nonzero failure |
| 259 | fn ot_ts_ch to ulong |
| 260 | n = get ot_tl_v OT_TL_HLEN |
| 261 | if lt n 39 |
| 262 | ret 1 |
| 263 | end |
| 264 | sl = ot_tl_h8 34 |
| 265 | for i ulong sl |
| 266 | put ot_tl_sid i get ot_tl_hm add 35 i |
| 267 | end |
| 268 | put ot_tl_v OT_TL_SIDN sl |
| 269 | p = add 35 sl |
| 270 | cl = ot_tl_h16 p |
| 271 | c = ot_ts_suite add p 2 cl |
| 272 | if eq c 0 |
| 273 | ret 2 |
| 274 | end |
| 275 | put ot_ts_v 7 c |
| 276 | p = add add p 2 cl |
| 277 | p = add add p 1 ot_tl_h8 p |
| 278 | e = add add p 2 ot_tl_h16 p |
| 279 | p = add p 2 |
| 280 | put ot_ts_v 6 0 |
| 281 | put ot_ts_v 10 0 |
| 282 | while lt p e bound 65536 |
| 283 | t = ot_tl_h16 p |
| 284 | l = ot_tl_h16 add p 2 |
| 285 | if eq t 51 |
| 286 | ot_ts_keyshare add p 4 l |
| 287 | end |
| 288 | if eq t 16 |
| 289 | put ot_ts_v 10 add add p 7 1 |
| 290 | put ot_ts_v 11 ot_tl_h8 add p 6 |
| 291 | end |
| 292 | p = add add p 4 l |
| 293 | end |
| 294 | if eq get ot_ts_v 6 0 |
| 295 | ret 3 |
| 296 | end |
| 297 | ret 0 |
| 298 | end |
| 299 | fn ot_ts_keyshare p ulong l ulong to void |
| 300 | q = add p 2 |
| 301 | e = add p l |
| 302 | while lt add q 4 e bound 65536 |
| 303 | g = ot_tl_h16 q |
| 304 | kl = ot_tl_h16 add q 2 |
| 305 | if and eq g 29 eq kl 32 |
| 306 | put ot_ts_v 6 add q 4 |
| 307 | end |
| 308 | q = add add q 4 kl |
| 309 | end |
| 310 | end |
| 311 | fn ot_ts_suite p ulong n ulong to ulong |
| 312 | f = get ot_ts_v 1 |
| 313 | for k ulong 2 |
| 314 | want = ulong 4867 |
| 315 | if eq k 1 |
| 316 | want = 4865 |
| 317 | end |
| 318 | if gt f 0 |
| 319 | want = f |
| 320 | end |
| 321 | for i ulong div n 2 |
| 322 | if eq ot_tl_h16 add p mul i 2 want |
| 323 | ret want |
| 324 | end |
| 325 | end |
| 326 | end |
| 327 | ret 0 |
| 328 | end |
| 329 | |
| 330 | note ======== ServerHello ======== |
| 331 | fn ot_ts_sh tr edit shactx pub view uchar 32 to bool |
| 332 | b = new uchar 256 0 |
| 333 | m = get ot_ts_v 0 |
| 334 | put b 0 3 |
| 335 | put b 1 3 |
| 336 | r = new uchar 32 0 |
| 337 | ok = ot_tl_hk_random r |
| 338 | for i ulong 32 |
| 339 | put b add 2 i get r i |
| 340 | end |
| 341 | if eq m OT_TS_HRR |
| 342 | ot_ts_hrrrand b |
| 343 | end |
| 344 | if or eq m OT_TS_DOWNGRADE eq m OT_TS_SENT13 |
| 345 | put b 26 68 |
| 346 | put b 27 79 |
| 347 | put b 28 87 |
| 348 | put b 29 78 |
| 349 | put b 30 71 |
| 350 | put b 31 82 |
| 351 | put b 32 68 |
| 352 | put b 33 1 |
| 353 | end |
| 354 | sl = get ot_tl_v OT_TL_SIDN |
| 355 | put b 34 uchar sl |
| 356 | for i ulong sl |
| 357 | put b add 35 i get ot_tl_sid i |
| 358 | end |
| 359 | if and eq m OT_TS_BADSID gt sl 0 |
| 360 | put b 35 xor get b 35 1 |
| 361 | put ot_ts_v 8 1 |
| 362 | end |
| 363 | p = add 35 sl |
| 364 | s = get ot_ts_v 7 |
| 365 | if eq m OT_TS_DOWNGRADE |
| 366 | s = 49199 |
| 367 | end |
| 368 | put b p uchar shr s 8 |
| 369 | put b add p 1 uchar and s 255 |
| 370 | put b add p 2 0 |
| 371 | p = add p 3 |
| 372 | if eq m OT_TS_DOWNGRADE |
| 373 | put b p 0 |
| 374 | put b add p 1 0 |
| 375 | p = add p 2 |
| 376 | else |
| 377 | p = ot_ts_shext b p pub |
| 378 | end |
| 379 | ot_ts_msg tr 2 part b 0 p |
| 380 | ret ot_ts_flush |
| 381 | end |
| 382 | fn ot_ts_shext b edit uchar 256 p ulong pub view uchar 32 to ulong |
| 383 | put b p 0 |
| 384 | put b add p 1 46 |
| 385 | q = add p 2 |
| 386 | put b q 0 |
| 387 | put b add q 1 43 |
| 388 | put b add q 2 0 |
| 389 | put b add q 3 2 |
| 390 | put b add q 4 3 |
| 391 | put b add q 5 4 |
| 392 | q = add q 6 |
| 393 | put b q 0 |
| 394 | put b add q 1 51 |
| 395 | put b add q 2 0 |
| 396 | put b add q 3 36 |
| 397 | put b add q 4 0 |
| 398 | put b add q 5 29 |
| 399 | put b add q 6 0 |
| 400 | put b add q 7 32 |
| 401 | for i ulong 32 |
| 402 | put b add add q 8 i get pub i |
| 403 | end |
| 404 | ret add q 40 |
| 405 | end |
| 406 | note derived here independently of the client's constants: SHA-256 of the ASCII text |
| 407 | fn ot_ts_hrrrand b edit uchar 256 to void |
| 408 | d = new uchar 32 0 |
| 409 | sha256_hash ot_ts_t_hrr d |
| 410 | for i ulong 32 |
| 411 | put b add 2 i get d i |
| 412 | end |
| 413 | end |
| 414 | |
| 415 | note ======== the server handshake ======== |
| 416 | fn ot_ts_accept fd int to int |
| 417 | ot_tl_init |
| 418 | for i ulong 64 |
| 419 | if and ne i OT_TL_DL ne i OT_TL_DLSET |
| 420 | put ot_tl_v i 0 |
| 421 | end |
| 422 | end |
| 423 | put ot_tl_v OT_TL_FD ulong fd |
| 424 | put ot_tl_v OT_TL_ST OT_TL_HS |
| 425 | put ot_tl_v OT_TL_RSL 16384 |
| 426 | put ot_ts_v 4 0 |
| 427 | put ot_ts_v 8 0 |
| 428 | c = ot_ts_hand |
| 429 | k = scrub 1048576 |
| 430 | if ne c 0 |
| 431 | note tests drain the socket after a refusal, so the descriptor stays open here (the driver closes it) |
| 432 | put ot_tl_v OT_TL_ERR c |
| 433 | put ot_tl_v OT_TL_ST OT_TL_FAILED |
| 434 | ret int c |
| 435 | end |
| 436 | put ot_tl_v OT_TL_ST OT_TL_OPEN |
| 437 | ret 0 |
| 438 | end |
| 439 | fn ot_ts_hand to ulong |
| 440 | sk = new uchar 640 0 |
| 441 | tr = new uchar 104 0 |
| 442 | sha256_init tr |
| 443 | r = ot_tl_rdhs |
| 444 | if ne r 1 |
| 445 | ret 5 |
| 446 | end |
| 447 | ot_tl_tradd tr |
| 448 | c = ot_ts_ch |
| 449 | if ne c 0 |
| 450 | ret 10 |
| 451 | end |
| 452 | c = ot_ts_keys tr sk |
| 453 | if ne c 0 |
| 454 | ret c |
| 455 | end |
| 456 | m = get ot_ts_v 0 |
| 457 | if or or eq m OT_TS_DOWNGRADE eq m OT_TS_HRR eq m OT_TS_EARLYEOF |
| 458 | put ot_ts_v 8 1 |
| 459 | ret 0 |
| 460 | end |
| 461 | c = ot_ts_flight tr sk |
| 462 | if ne c 0 |
| 463 | ret c |
| 464 | end |
| 465 | ret ot_ts_cfin tr sk |
| 466 | end |
| 467 | note key exchange and ServerHello; handshake keys in both directions |
| 468 | fn ot_ts_keys tr edit shactx sk edit uchar 640 to ulong |
| 469 | priv = new uchar 32 0 |
| 470 | pub = new uchar 32 0 |
| 471 | cp = new uchar 32 0 |
| 472 | dh = new uchar 64 0 |
| 473 | if not ot_tl_hk_random priv |
| 474 | ret 19 |
| 475 | end |
| 476 | ot_x25_base pub priv |
| 477 | o = get ot_ts_v 6 |
| 478 | for i ulong 32 |
| 479 | put cp i get ot_tl_hm add o i |
| 480 | end |
| 481 | d = new uchar 32 0 |
| 482 | ot_x25_mul d priv cp |
| 483 | for i ulong 32 |
| 484 | put dh i get d i |
| 485 | put d i 0 |
| 486 | put priv i 0 |
| 487 | end |
| 488 | if eq get ot_ts_v 0 OT_TS_ZEROKEY |
| 489 | for i ulong 32 |
| 490 | put pub i 0 |
| 491 | end |
| 492 | end |
| 493 | if not ot_ts_sh tr pub |
| 494 | ret 23 |
| 495 | end |
| 496 | if eq get ot_ts_v 0 OT_TS_EARLYEOF |
| 497 | ret 0 |
| 498 | end |
| 499 | if gt get ot_tl_v OT_TL_SIDN 0 |
| 500 | ok = ot_tl_ccs |
| 501 | end |
| 502 | s = get ot_ts_v 7 |
| 503 | put ot_tl_v OT_TL_SUITE s |
| 504 | put ot_tl_v OT_TL_AEAD 3 |
| 505 | put ot_tl_v OT_TL_KLEN 32 |
| 506 | if eq s 4865 |
| 507 | put ot_tl_v OT_TL_AEAD 1 |
| 508 | put ot_tl_v OT_TL_KLEN 16 |
| 509 | end |
| 510 | c2 = ot_tl_hsec tr sk dh |
| 511 | ot_tl_zero64 dh |
| 512 | if ne c2 0 |
| 513 | ret ulong c2 |
| 514 | end |
| 515 | note ot_tl_hsec set read = server hs (client view); swap for the server |
| 516 | x = new uchar 64 0 |
| 517 | ot_tl_sk sk OT_TL_K_CHS x |
| 518 | ok = ot_tl_keys 0 x |
| 519 | ot_tl_sk sk OT_TL_K_SHS x |
| 520 | ok = ot_tl_keys 1 x |
| 521 | ot_tl_zero64 x |
| 522 | put ot_tl_v OT_TL_CCS 1 |
| 523 | ret 0 |
| 524 | end |
| 525 | note EncryptedExtensions, Certificate, CertificateVerify, Finished (with the hostile variants) |
| 526 | fn ot_ts_flight tr edit shactx sk edit uchar 640 to ulong |
| 527 | m = get ot_ts_v 0 |
| 528 | if or eq m OT_TS_HSTRUNC eq m OT_TS_HSOVER |
| 529 | ot_ts_hostile |
| 530 | ret 0 |
| 531 | end |
| 532 | if eq m OT_TS_PLAINHS |
| 533 | put ot_tl_v OT_TL_WENC 0 |
| 534 | put ot_ts_v 8 1 |
| 535 | end |
| 536 | ot_ts_ee tr |
| 537 | if eq get ot_ts_v 0 OT_TS_BIGCERT |
| 538 | ot_ts_bigcert tr |
| 539 | else |
| 540 | if not ot_ts_cert tr |
| 541 | ret 20 |
| 542 | end |
| 543 | end |
| 544 | if not ot_ts_cv tr |
| 545 | ret 20 |
| 546 | end |
| 547 | ot_ts_fin tr sk |
| 548 | if not ot_ts_flush |
| 549 | ret 23 |
| 550 | end |
| 551 | ot_ts_appsw tr sk |
| 552 | ret 0 |
| 553 | end |
| 554 | fn ot_ts_ee tr edit shactx to void |
| 555 | b = new uchar 300 0 |
| 556 | p = ulong 2 |
| 557 | an = get ot_ts_v 11 |
| 558 | if gt get ot_ts_v 10 0 |
| 559 | put b 2 0 |
| 560 | put b 3 16 |
| 561 | put b 4 0 |
| 562 | put b 5 uchar add an 3 |
| 563 | put b 6 0 |
| 564 | put b 7 uchar add an 1 |
| 565 | put b 8 uchar an |
| 566 | a = sub get ot_ts_v 10 1 |
| 567 | for i ulong an |
| 568 | put b add 9 i get ot_tl_hm add a i |
| 569 | end |
| 570 | p = add 9 an |
| 571 | end |
| 572 | if eq get ot_ts_v 0 OT_TS_UNSOLEXT |
| 573 | put b p 255 |
| 574 | put b add p 1 0 |
| 575 | put b add p 2 0 |
| 576 | put b add p 3 0 |
| 577 | p = add p 4 |
| 578 | put ot_ts_v 8 1 |
| 579 | end |
| 580 | put b 0 uchar shr sub p 2 8 |
| 581 | put b 1 uchar and sub p 2 255 |
| 582 | ot_ts_msg tr 8 part b 0 p |
| 583 | end |
| 584 | note one certificate entry of 1000001 zero bytes (a length past a for-loop's default pass guard, below the 1 MiB |
| 585 | note entry limit): the client must refuse it with a defined error, not stop with a trap |
| 586 | fn ot_ts_bigcert tr edit shactx to void |
| 587 | b = new uchar 1000016 0 |
| 588 | l = ulong 1000001 |
| 589 | t = add l 5 |
| 590 | put b 1 uchar shr t 16 |
| 591 | put b 2 uchar and shr t 8 255 |
| 592 | put b 3 uchar and t 255 |
| 593 | put b 4 uchar shr l 16 |
| 594 | put b 5 uchar and shr l 8 255 |
| 595 | put b 6 uchar and l 255 |
| 596 | put ot_ts_v 8 1 |
| 597 | ot_ts_msg tr 11 part b 0 add 4 t |
| 598 | end |
| 599 | fn ot_ts_cert tr edit shactx to bool |
| 600 | c = get ot_ts_v 2 |
| 601 | tot = ulong 0 |
| 602 | for i ulong c |
| 603 | tot = add add tot get ot_ts_cl i 5 |
| 604 | end |
| 605 | b = new uchar 65536 0 |
| 606 | if gt add tot 4 65536 |
| 607 | ret false |
| 608 | end |
| 609 | put b 0 0 |
| 610 | put b 1 uchar shr tot 16 |
| 611 | put b 2 uchar and shr tot 8 255 |
| 612 | put b 3 uchar and tot 255 |
| 613 | p = ulong 4 |
| 614 | o = ulong 0 |
| 615 | for i ulong c |
| 616 | l = get ot_ts_cl i |
| 617 | p = ot_ts_entry b p o l |
| 618 | o = add o l |
| 619 | end |
| 620 | ot_ts_msg tr 11 part b 0 p |
| 621 | ret true |
| 622 | end |
| 623 | note one CertificateEntry: 24-bit length, the DER at ot_ts_chain[o, o+l), no extensions; the next position |
| 624 | fn ot_ts_entry b edit uchar 65536 p ulong o ulong l ulong to ulong |
| 625 | put b p uchar shr l 16 |
| 626 | put b add p 1 uchar and shr l 8 255 |
| 627 | put b add p 2 uchar and l 255 |
| 628 | for j ulong l |
| 629 | put b add add p 3 j get ot_ts_chain add o j |
| 630 | end |
| 631 | q = add add p 3 l |
| 632 | put b q 0 |
| 633 | put b add q 1 0 |
| 634 | ret add q 2 |
| 635 | end |
| 636 | note CertificateVerify, rsa_pss_rsae_sha256 (RFC 8446 4.4.3, RFC 8017 9.1.1 EMSA-PSS-ENCODE, salt = 32 random bytes) |
| 637 | fn ot_ts_cv tr edit shactx to bool |
| 638 | th = new uchar 64 0 |
| 639 | ot_tl_trhash tr th |
| 640 | msg = new uchar 130 0 |
| 641 | for i ulong 64 |
| 642 | put msg i 32 |
| 643 | end |
| 644 | for i ulong len ot_ts_t_cv |
| 645 | put msg add 64 i get ot_ts_t_cv i |
| 646 | end |
| 647 | for i ulong 32 |
| 648 | put msg add 98 i get th i |
| 649 | end |
| 650 | mh = new uchar 32 0 |
| 651 | sha256_hash msg mh |
| 652 | nb = get ot_ts_v 3 |
| 653 | em = new uchar 1024 0 |
| 654 | pk1 = eq get ot_ts_v 0 OT_TS_PKCS1CV |
| 655 | if pk1 |
| 656 | ot_ts_pkcs1 mh part em 0 nb |
| 657 | put ot_ts_v 8 1 |
| 658 | else |
| 659 | if not ot_ts_pss mh part em 0 nb |
| 660 | ret false |
| 661 | end |
| 662 | end |
| 663 | if not ot_ts_rsa part em 0 nb |
| 664 | ret false |
| 665 | end |
| 666 | b = new uchar 1100 0 |
| 667 | put b 0 8 |
| 668 | put b 1 4 |
| 669 | if pk1 |
| 670 | put b 0 4 |
| 671 | put b 1 1 |
| 672 | end |
| 673 | put b 2 uchar shr nb 8 |
| 674 | put b 3 uchar and nb 255 |
| 675 | for i ulong nb |
| 676 | put b add 4 i get em i |
| 677 | end |
| 678 | if eq get ot_ts_v 0 OT_TS_BADCV |
| 679 | put b 40 xor get b 40 1 |
| 680 | put ot_ts_v 8 1 |
| 681 | end |
| 682 | ot_ts_msg tr 15 part b 0 add 4 nb |
| 683 | ret true |
| 684 | end |
| 685 | note EMSA-PKCS1-v1_5 with SHA-256 (RFC 8017 9.2): 00 01 FF.. 00 DigestInfo hash; for the scheme-refusal case only |
| 686 | fn ot_ts_pkcs1 mh view uchar 32 em edit uchar * to void |
| 687 | di = new uchar 19 0 |
| 688 | put di 0 48 |
| 689 | put di 1 49 |
| 690 | put di 2 48 |
| 691 | put di 3 13 |
| 692 | put di 4 6 |
| 693 | put di 5 9 |
| 694 | put di 6 96 |
| 695 | put di 7 134 |
| 696 | put di 8 72 |
| 697 | put di 9 1 |
| 698 | put di 10 101 |
| 699 | put di 11 3 |
| 700 | put di 12 4 |
| 701 | put di 13 2 |
| 702 | put di 14 1 |
| 703 | put di 15 5 |
| 704 | put di 16 0 |
| 705 | put di 17 4 |
| 706 | put di 18 32 |
| 707 | n = len em |
| 708 | put em 0 0 |
| 709 | put em 1 1 |
| 710 | ps = sub sub n 51 3 |
| 711 | for i ulong ps |
| 712 | put em add 2 i 255 |
| 713 | end |
| 714 | put em add 2 ps 0 |
| 715 | for i ulong 19 |
| 716 | put em add add 3 ps i get di i |
| 717 | end |
| 718 | for i ulong 32 |
| 719 | put em add add 22 ps i get mh i |
| 720 | end |
| 721 | end |
| 722 | fn ot_ts_pss mh view uchar 32 em edit uchar * to bool |
| 723 | n = len em |
| 724 | if lt n 66 |
| 725 | ret false |
| 726 | end |
| 727 | salt = new uchar 32 0 |
| 728 | ok = ot_tl_hk_random salt |
| 729 | mp = new uchar 72 0 |
| 730 | for i ulong 32 |
| 731 | put mp add 8 i get mh i |
| 732 | put mp add 40 i get salt i |
| 733 | end |
| 734 | h = new uchar 32 0 |
| 735 | sha256_hash mp h |
| 736 | dl = sub sub n 32 1 |
| 737 | db = new uchar 1024 0 |
| 738 | put db sub sub dl 32 1 1 |
| 739 | for i ulong 32 |
| 740 | put db add sub dl 32 i get salt i |
| 741 | end |
| 742 | mask = new uchar 1024 0 |
| 743 | ot_rsa_mgf1 0 h part mask 0 dl |
| 744 | for i ulong dl |
| 745 | put em i xor get db i get mask i |
| 746 | end |
| 747 | put em 0 and get em 0 127 |
| 748 | for i ulong 32 |
| 749 | put em add dl i get h i |
| 750 | end |
| 751 | put em sub n 1 188 |
| 752 | ret true |
| 753 | end |
| 754 | fn ot_ts_rsa em edit uchar * to bool |
| 755 | nb = get ot_ts_v 3 |
| 756 | n = new uchar 1024 0 |
| 757 | d = new uchar 1024 0 |
| 758 | for i ulong nb |
| 759 | put n i get ot_ts_n i |
| 760 | put d i get ot_ts_d i |
| 761 | end |
| 762 | ret ot_bn_modexp part n 0 nb part d 0 nb em |
| 763 | end |
| 764 | fn ot_ts_fin tr edit shactx sk edit uchar 640 to void |
| 765 | th = new uchar 64 0 |
| 766 | ot_tl_trhash tr th |
| 767 | b = new uchar 64 0 |
| 768 | vd = new uchar 64 0 |
| 769 | ot_tl_sk sk OT_TL_K_SHS b |
| 770 | ok = ot_tl_vdata b th vd |
| 771 | if eq get ot_ts_v 0 OT_TS_BADFIN |
| 772 | put vd 3 xor get vd 3 128 |
| 773 | put ot_ts_v 8 1 |
| 774 | end |
| 775 | ot_ts_msg tr 20 part vd 0 32 |
| 776 | end |
| 777 | note after the flight: application secrets; the server writes with the server application keys at once and reads |
| 778 | note the client's handshake keys until the client's Finished (ot_tl_appkeys sets the client's view, swapped here) |
| 779 | fn ot_ts_appsw tr edit shactx sk edit uchar 640 to void |
| 780 | c = ot_tl_appkeys tr sk |
| 781 | x = new uchar 64 0 |
| 782 | ot_tl_sk sk OT_TL_K_SAP x |
| 783 | ok = ot_tl_keys 1 x |
| 784 | ot_tl_sk sk OT_TL_K_CHS x |
| 785 | ok = ot_tl_keys 0 x |
| 786 | ot_tl_zero64 x |
| 787 | end |
| 788 | fn ot_ts_cfin tr edit shactx sk edit uchar 640 to ulong |
| 789 | put ot_tl_v OT_TL_CCS 1 |
| 790 | r = ot_tl_rdhs |
| 791 | if ne r 20 |
| 792 | ret 5 |
| 793 | end |
| 794 | th = new uchar 64 0 |
| 795 | for i ulong 64 |
| 796 | put th i get sk add OT_TL_K_TH i |
| 797 | end |
| 798 | note the client's Finished covers the transcript through the server Finished (no client Certificate here) |
| 799 | b = new uchar 64 0 |
| 800 | vd = new uchar 64 0 |
| 801 | ot_tl_sk sk OT_TL_K_CHS b |
| 802 | ok = ot_tl_vdata b th vd |
| 803 | for i ulong 32 |
| 804 | if ne get vd i get ot_tl_hm i |
| 805 | ret 17 |
| 806 | end |
| 807 | end |
| 808 | ot_tl_tradd tr |
| 809 | x = new uchar 64 0 |
| 810 | ot_tl_sk sk OT_TL_K_CAP x |
| 811 | ok = ot_tl_keys 0 x |
| 812 | ot_tl_zero64 x |
| 813 | put ot_tl_v OT_TL_CCS 0 |
| 814 | ret 0 |
| 815 | end |
| 816 | |
| 817 | note ======== after the handshake: hostile actions ======== |
| 818 | fn ot_ts_raw b view uchar * to bool |
| 819 | ret ot_tl_sendall b |
| 820 | end |
| 821 | fn ot_ts_hostile to void |
| 822 | m = get ot_ts_v 0 |
| 823 | put ot_ts_v 8 1 |
| 824 | b = new uchar 16700 0 |
| 825 | a = new uchar 2 0 |
| 826 | h = new uchar 8 0 |
| 827 | if or eq m OT_TS_TRUNC eq m OT_TS_HSTRUNC |
| 828 | put b 0 23 |
| 829 | put b 1 3 |
| 830 | put b 2 3 |
| 831 | put b 3 0 |
| 832 | put b 4 100 |
| 833 | ok = ot_ts_raw part b 0 55 |
| 834 | z = shutdown ot_tl_fd 1 |
| 835 | end |
| 836 | if or eq m OT_TS_OVERSIZE eq m OT_TS_HSOVER |
| 837 | put b 0 23 |
| 838 | put b 1 3 |
| 839 | put b 2 3 |
| 840 | put b 3 65 |
| 841 | put b 4 1 |
| 842 | ok = ot_ts_raw part b 0 16646 |
| 843 | end |
| 844 | if eq m OT_TS_ALERTS |
| 845 | note warning-level user_canceled alerts |
| 846 | put a 0 1 |
| 847 | put a 1 90 |
| 848 | for i ulong 1000 |
| 849 | ok = ot_tl_wrec 21 a |
| 850 | end |
| 851 | end |
| 852 | if eq m OT_TS_RENEG |
| 853 | note a HelloRequest (type 0, empty): renegotiation does not exist in TLS 1.3 |
| 854 | put h 0 0 |
| 855 | ok = ot_tl_wrec 22 part h 0 4 |
| 856 | end |
| 857 | if eq m OT_TS_BADMAC |
| 858 | ot_ts_badmac |
| 859 | end |
| 860 | end |
| 861 | fn ot_ts_badmac to void |
| 862 | d = new uchar 32 65 |
| 863 | ok = ot_tl_wrec23x d |
| 864 | end |
| 865 | note an application_data record with one ciphertext bit flipped |
| 866 | fn ot_tl_wrec23x d view uchar 32 to bool |
| 867 | wb = new uchar 16645 0 |
| 868 | for i ulong 32 |
| 869 | put wb add 5 i get d i |
| 870 | end |
| 871 | put wb 37 23 |
| 872 | put wb 0 23 |
| 873 | put wb 1 3 |
| 874 | put wb 2 3 |
| 875 | put wb 3 0 |
| 876 | put wb 4 49 |
| 877 | ok = ot_tl_seal wb 49 |
| 878 | put ot_tl_v OT_TL_WSEQ add get ot_tl_v OT_TL_WSEQ 1 |
| 879 | put wb 10 xor get wb 10 4 |
| 880 | ret ot_tl_sendall part wb 0 54 |
| 881 | end |
| 882 | note read what the client sends after a hostile action until it closes: count records that are not alerts |
| 883 | note (a plaintext alert is 2 bytes, a protected alert 19); any other record is evidence of data sent after a refusal |
| 884 | fn ot_ts_drain to ulong |
| 885 | rb = new uchar 16645 0 |
| 886 | bad = ulong 0 |
| 887 | while true bound 100000 |
| 888 | n = ot_tl_rdrec rb |
| 889 | if lt n 0 |
| 890 | break |
| 891 | end |
| 892 | t = get rb 0 |
| 893 | if not or and eq t 21 eq n 2 and eq t 23 eq n 19 |
| 894 | bad = add bad 1 |
| 895 | end |
| 896 | end |
| 897 | put ot_ts_v 9 bad |
| 898 | ret bad |
| 899 | end |