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/omega-mv/blob/main/om/tls/test/x509/driver.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / tls / test / x509 / driver.om` 676 lines ยท 14127 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note driver.om: program-mode test driver for om/tls/x509.om (composed after sha2 hkdf ec rsa x509). 3 note Reads case records from stdin, one per line, tokens separated by single spaces; paths are 4 note relative to the working directory (run.sh runs it in vectors/). Records: 5 note C NAME EXPECT NOW HOST STORE CERT... x509_chain_verify of the certificate_list built from the 6 note DER files CERT... (leaf first); STORE a PEM bundle, or SYS 7 note for x509_store_load; NOW unix seconds 8 note V NAME EXPECT SCHEME CERT MSG SIG x509_cv_verify (SCHEME decimal; MSG, SIG raw files) 9 note M NAME EXPECT PATTERN HOST chain_match, EXPECT 1 match / 0 no match 10 note H NAME EXPECT HOST chain_hostok code name 11 note T NAME EXPECT HEX der_time of the Time TLV in HEX; EXPECT unix seconds 12 note (decimal, may be negative) or BAD 13 note S NAME EXPECT STORE store entries loaded from STORE (or SYS) 14 note E NAME EXPECT CODE x509_errname(CODE) equals EXPECT (- for empty) 15 note F NAME COUNT NOW HOST STORE CERT... COUNT deterministic mutations of the certificate_list; 16 note each must return (any code); counts rejections 17 note P NAME EXPECT CERT x509_parse of the single DER file CERT (any size up to 18 note the 262144-byte list buffer; no chain, no policy) 19 note D NAME EXPECT ANCHOR NOTBEFORE chain_dist (Mozilla distrust, M1) of the DER anchor 20 note ANCHOR for a leaf notBefore NOTBEFORE (unix seconds) 21 note EXPECT for C/V/H is a code name (X509_OK, X509_ERR_...). Prints "MISMATCH line L NAME got G want 22 note W" per failure, "fuzz NAME n=N rejected=R" per F record, then "x509 driver: C checks, F failed". 23 note Exit 0 when F is 0 and C > 0, 1 otherwise, 2 when input cannot be read. 24 25 global inp uchar `4194304` 26 global ds ulong 16 27 text DR0 "0" 28 text DR1 "1" 29 global ob uchar 32 30 global sname uchar 256 31 global nm uchar 64 32 global onebyte uchar 1 33 text DRSUM "x509 driver: " 34 text DRCHK " checks, " 35 text DRFLD " failed\n" 36 text DRMIS "MISMATCH line " 37 text DRGOT " got " 38 text DRWANT " want " 39 text DRFUZ "fuzz " 40 text DRFN " n=" 41 text DRFR " rejected=" 42 text DRNL "\n" 43 text DRSP " " 44 text DRSYS "SYS" 45 text DRBAD "BAD" 46 text DRERR "ERR" 47 text DRIO "x509 driver: cannot read input\n" 48 text DRRES "RESULT " 49 text DRSTF "STORE\n" 50 text DRLSF "LIST\n" 51 52 note ds: 0 input length, 1 cursor, 2 line number, 3 checks, 4 failures, 5 store bytes (0 = none), 53 note 6 name length, 7 store name length 54 55 fn drv_slurp to int 56 t = ulong 0 57 while true bound 999999 58 room = sub len inp t 59 if eq room 0 60 ret 2 61 end 62 r = read 0 inp t room 63 if lt r 0 64 ret 2 65 end 66 if eq r 0 67 break 68 end 69 t = add t ulong r 70 end 71 put ds 0 t 72 ret 0 73 end 74 75 fn drv_peek to ulong 76 c = get ds 1 77 if ge c get ds 0 78 ret 256 79 end 80 ret ulong get inp c 81 end 82 83 note next token into tk (skipping one space before it); its length (0 at the line end) 84 fn drv_tok tk edit uchar * to ulong 85 if eq drv_peek 32 86 put ds 1 add get ds 1 1 87 end 88 n = ulong 0 89 while true bound 999999 90 c = drv_peek 91 if or or eq c 32 eq c 10 eq c 256 92 break 93 end 94 if lt n len tk 95 put tk n uchar c 96 end 97 n = add n 1 98 put ds 1 add get ds 1 1 99 end 100 if gt n len tk 101 ret 0 102 end 103 ret n 104 end 105 106 fn drv_eol to void 107 while true bound 999999 108 c = drv_peek 109 if eq c 256 110 ret 111 end 112 put ds 1 add get ds 1 1 113 if eq c 10 114 ret 115 end 116 end 117 end 118 119 fn drv_num t view uchar * to ulong 120 v = ulong 0 121 for i ulong len t 122 v = add mul v 10 sub ulong get t i 48 123 end 124 ret v 125 end 126 127 fn drv_prn v ulong to void 128 x = v 129 k = ulong 0 130 while true bound 21 131 put ob sub 31 k uchar add 48 rem x 10 132 k = add k 1 133 x = div x 10 134 if eq x 0 135 break 136 end 137 end 138 r = write 1 ob sub 32 k k 139 end 140 141 fn drv_out t view uchar * to void 142 for i ulong len t 143 put ob 0 get t i 144 r = write 1 ob 0 1 145 end 146 end 147 148 note record a mismatch: got / want as text 149 fn drv_mis g view uchar * w view uchar * to void 150 put ds 4 add get ds 4 1 151 r = write 1 DRMIS 0 len DRMIS 152 drv_prn get ds 2 153 r = write 1 DRSP 0 1 154 r = write 1 nm 0 get ds 6 155 r = write 1 DRGOT 0 len DRGOT 156 drv_out g 157 r = write 1 DRWANT 0 len DRWANT 158 drv_out w 159 r = write 1 DRNL 0 1 160 end 161 162 note compare code c against the expected name w 163 fn drv_code c ulong w view uchar * to void 164 b = new uchar 64 0 165 put ds 3 add get ds 3 1 166 n = x509_errname c edit b 167 if eq n 0 168 drv_mis DRBAD w 169 ret 170 end 171 if not der_same view part b 0 n w 172 drv_mis view part b 0 n w 173 end 174 end 175 176 fn drv_name to void 177 t = new uchar 64 0 178 n = drv_tok edit t 179 for i ulong n 180 put nm i get t i 181 end 182 put ds 6 n 183 end 184 185 note read the file named p into b; bytes read, or -1 (missing, error, or over len b) 186 fn drv_file p view uchar * b edit uchar * to long 187 fd = fopen p 0 len p 0 0 188 if lt fd 0 189 ret -1 190 end 191 n = ulong 0 192 while lt n len b bound 999999 193 r = read int fd b n sub len b n 194 if le r 0 195 break 196 end 197 n = add n ulong r 198 end 199 r = read int fd onebyte 0 1 200 z = fclose int fd 201 if or ne r 0 eq n len b 202 ret -1 203 end 204 ret long n 205 end 206 207 note load store tk (SYS or a path) into st unless it is the current one; 0 ok, 1 failure 208 fn drv_store st edit uchar * tk view uchar * to ulong 209 if eq len tk get ds 7 210 d = ulong 0 211 for i ulong len tk 212 if ne get tk i get sname i 213 d = 1 214 end 215 end 216 if and eq d 0 gt get ds 5 0 217 ret 0 218 end 219 end 220 for i ulong len tk 221 put sname i get tk i 222 end 223 put ds 7 len tk 224 put ds 5 0 225 r = long 0 226 if der_same tk DRSYS 227 r = x509_store_load st 228 else 229 r = x509_store_loadf st tk 230 end 231 if le r 0 232 ret 1 233 end 234 put ds 5 ulong r 235 ret 0 236 end 237 238 note certificate_list from the remaining DER file tokens of the line into cl; its length (0 bad) 239 fn drv_list cl edit uchar * tk edit uchar * to ulong 240 p = ulong 0 241 while true bound 4096 242 n = drv_tok tk 243 if eq n 0 244 break 245 end 246 r = drv_file view part tk 0 n part cl add p 3 sub len cl add p 5 247 if lt r 1 248 ret 0 249 end 250 l = ulong r 251 put cl p uchar shr l 16 252 put cl add p 1 uchar and shr l 8 255 253 put cl add p 2 uchar and l 255 254 put cl add add p 3 l 0 255 put cl add add p 4 l 0 256 p = add add p 5 l 257 end 258 ret p 259 end 260 261 note x509_chain_verify of cl[0 .. ds 12) with hq[0 .. ds 11) 262 fn drv_crun cl view uchar * st view uchar * hq view uchar 300 to ulong 263 k = get ds 12 264 hn = get ds 11 265 ret x509_chain_verify part cl 0 k st part hq 0 hn 266 end 267 268 note C record 269 note NOW and HOST tokens into hq (8-byte big-endian time || host); total length 270 fn drv_now hq edit uchar 300 tk edit uchar * to ulong 271 n = drv_tok tk 272 nw = drv_num view part tk 0 n 273 for i ulong 8 274 put hq i uchar and shr nw mul sub 7 i 8 255 275 end 276 ret add 8 drv_tok part hq 8 sub len hq 8 277 end 278 279 fn drv_c st edit uchar * cl edit uchar * tk edit uchar * to void 280 hq = new uchar 300 0 281 drv_name 282 w = new uchar 64 0 283 wn = drv_tok edit w 284 hn = drv_now hq tk 285 n = drv_tok tk 286 if ne drv_store st view part tk 0 n 0 287 drv_code 99 view part w 0 wn 288 ret 289 end 290 k = drv_list cl tk 291 if eq k 0 292 drv_code 98 view part w 0 wn 293 ret 294 end 295 put ds 11 hn 296 put ds 12 k 297 c = drv_crun cl st hq 298 drv_code c view part w 0 wn 299 end 300 301 note R record (x509-limbo): R NAME NOW HOST STORE CERT... runs x509_chain_verify like C and prints 302 note "RESULT NAME CODE" (CODE the x509_errname, STORE when the store does not load, LIST when a 303 note certificate file is missing); the expectation is judged outside (test/x509/limbo.py) 304 fn drv_r st edit uchar * cl edit uchar * tk edit uchar * to void 305 hq = new uchar 300 0 306 drv_name 307 hn = drv_now hq tk 308 n = drv_tok tk 309 put ds 3 add get ds 3 1 310 r = write 1 DRRES 0 len DRRES 311 r = write 1 nm 0 get ds 6 312 r = write 1 DRSP 0 1 313 if ne drv_store st view part tk 0 n 0 314 r = write 1 DRSTF 0 len DRSTF 315 ret 316 end 317 k = drv_list cl tk 318 if eq k 0 319 r = write 1 DRLSF 0 len DRLSF 320 ret 321 end 322 put ds 11 hn 323 put ds 12 k 324 drv_rout drv_crun cl st hq 325 end 326 327 note R record result: the code name and a line end 328 fn drv_rout c ulong to void 329 b = new uchar 64 0 330 m = x509_errname c b 331 r = write 1 b 0 m 332 r = write 1 DRNL 0 1 333 end 334 335 note P record 336 fn drv_p cl edit uchar * tk edit uchar * to void 337 r = new ulong 32 0 338 drv_name 339 w = new uchar 64 0 340 wn = drv_tok edit w 341 n = drv_tok tk 342 k = drv_file view part tk 0 n cl 343 if lt k 1 344 drv_code 98 view part w 0 wn 345 ret 346 end 347 c = x509_parse part cl 0 ulong k edit r 348 drv_code c view part w 0 wn 349 end 350 351 note D record 352 fn drv_d cl edit uchar * tk edit uchar * to void 353 w = new ulong X5WSZ 0 354 drv_name 355 e = new uchar 64 0 356 en = drv_tok edit e 357 n = drv_tok tk 358 k = drv_file view part tk 0 n cl 359 if lt k 1 360 drv_code 98 view part e 0 en 361 ret 362 end 363 n = drv_tok tk 364 t = drv_num view part tk 0 n 365 put w X5WLNB add t mul x509_days 1970 1 1 86400 366 c = chain_dist view part cl 0 ulong k view w 367 drv_code c view part e 0 en 368 end 369 370 note V record 371 fn drv_v cl edit uchar * ms edit uchar * tk edit uchar * to void 372 drv_name 373 w = new uchar 64 0 374 wn = drv_tok edit w 375 n = drv_tok tk 376 sc = drv_num view part tk 0 n 377 k = drv_list cl tk 378 if eq k 0 379 drv_code 98 view part w 0 wn 380 ret 381 end 382 put ds 8 sc 383 drv_vrun cl ms view part w 0 wn 384 end 385 386 note V record, second half: the leaf is the only entry; MSG and SIG were listed after it, so 387 note drv_list put them in cl as entries 2 and 3; move them into ms 388 fn drv_vrun cl edit uchar * ms edit uchar * w view uchar * to void 389 m = drv_vcopy cl ms 390 a = get ds 13 391 b = get ds 14 392 c = x509_cv_verify view part cl 0 a get ds 8 view part ms 0 b m 393 drv_code c w 394 end 395 396 note move MSG and SIG (entries 2, 3 of cl) into ms; MSG length; ds 13 leaf list length, ds 14 total 397 fn drv_vcopy cl edit uchar * ms edit uchar * to ulong 398 l1 = der_u24 cl 0 399 p = add l1 5 400 l2 = der_u24 cl p 401 p = add p 3 402 for i ulong l2 403 put ms i get cl add p i 404 end 405 p = add add p l2 2 406 l3 = der_u24 cl p 407 p = add p 3 408 for i ulong l3 409 put ms add l2 i get cl add p i 410 end 411 put cl add l1 3 0 412 put cl add l1 4 0 413 put ds 13 add l1 5 414 put ds 14 add l2 l3 415 ret l2 416 end 417 418 note M record 419 fn drv_m tk edit uchar * hb edit uchar * to void 420 drv_name 421 e = drv_num view part tk 0 drv_tok tk 422 n = drv_tok tk 423 h = drv_tok hb 424 g = ulong 0 425 if chain_match view part tk 0 n view part hb 0 h 426 g = 1 427 end 428 put ds 3 add get ds 3 1 429 if ne g e 430 if eq g 1 431 drv_mis DR1 DR0 432 else 433 drv_mis DR0 DR1 434 end 435 end 436 end 437 438 note H record 439 fn drv_h tk edit uchar * hb edit uchar * to void 440 drv_name 441 n = drv_tok tk 442 h = drv_tok hb 443 c = chain_hostok view part hb 0 h 444 drv_code c view part tk 0 n 445 end 446 447 note T record 448 fn drv_t tk edit uchar * hb edit uchar * to void 449 drv_name 450 n = drv_tok tk 451 h = drv_tok hb 452 k = div h 2 453 for i ulong k 454 put hb i uchar or shl der_hexv ulong get hb mul i 2 4 der_hexv ulong get hb add mul i 2 1 455 end 456 v = der_time view part hb 0 k 457 put ds 3 add get ds 3 1 458 if eq v X5BIG 459 if not der_same view part tk 0 n DRBAD 460 drv_mis DRBAD view part tk 0 n 461 end 462 ret 463 end 464 ep = mul x509_days 1970 1 1 86400 465 if gt get tk 0 47 466 if ge v ep 467 if ne sub v ep drv_num view part tk 0 n 468 drv_mis DRBAD view part tk 0 n 469 end 470 ret 471 end 472 end 473 if eq get tk 0 45 474 if lt v ep 475 if eq sub ep v drv_num view part tk 1 sub n 1 476 ret 477 end 478 end 479 end 480 drv_mis DRBAD view part tk 0 n 481 end 482 483 note S record 484 fn drv_s st edit uchar * tk edit uchar * to void 485 drv_name 486 e = drv_num view part tk 0 drv_tok tk 487 n = drv_tok tk 488 put ds 5 0 489 put ds 7 0 490 put ds 3 add get ds 3 1 491 if ne drv_store st view part tk 0 n 0 492 if ne e 0 493 drv_mis DRERR DRBAD 494 end 495 ret 496 end 497 k = ulong 0 498 p = ulong 0 499 while lt add p 3 len st bound 100000 500 l = der_u24 st p 501 if eq l 0 502 break 503 end 504 p = add add p 3 l 505 k = add k 1 506 end 507 if ne k e 508 drv_prn k 509 drv_mis DRERR DRBAD 510 end 511 end 512 513 note E record 514 fn drv_e tk edit uchar * hb edit uchar * to void 515 drv_name 516 n = drv_tok tk 517 c = drv_num view part hb 0 drv_tok hb 518 b = new uchar 64 0 519 m = x509_errname c edit b 520 put ds 3 add get ds 3 1 521 if eq get tk 0 45 522 if ne m 0 523 drv_mis view part b 0 m view part tk 0 n 524 end 525 ret 526 end 527 if not der_same view part b 0 m view part tk 0 n 528 drv_mis view part b 0 m view part tk 0 n 529 end 530 end 531 532 note F record: mutation k of the certificate_list cl[0 .. n) into mu: k mod 4 = 0 one byte set to a 533 note pseudo-random value, 1 one byte xor 0x80 at a pseudo-random header-ish position, 2 truncate the 534 note leaf length field to a smaller value, 3 two bytes overwritten 535 fn drv_mut cl view uchar * mu edit uchar * k ulong to ulong 536 n = len cl 537 for i ulong n 538 put mu i get cl i 539 end 540 x = add mul k `2654435761` 12345 541 x = xor x shr x 13 542 p = rem x n 543 m = rem k 4 544 if eq m 1 545 p = rem p 64 546 put mu p xor get mu p 128 547 ret n 548 end 549 if eq m 2 550 p = rem x der_u24 cl 0 551 put mu 0 0 552 put mu 1 uchar and shr p 8 255 553 put mu 2 uchar and p 255 554 ret n 555 end 556 put mu p uchar and shr x 17 255 557 if eq m 3 558 p = rem mul p 7 n 559 put mu p uchar and shr x 29 255 560 end 561 ret n 562 end 563 564 note F record 565 fn drv_f st edit uchar * cl edit uchar * tk edit uchar * to void 566 hq = new uchar 300 0 567 drv_name 568 cnt = drv_num view part tk 0 drv_tok tk 569 hn = drv_now hq tk 570 n = drv_tok tk 571 put ds 3 add get ds 3 1 572 if ne drv_store st view part tk 0 n 0 573 drv_mis DRERR DRBAD 574 ret 575 end 576 k = drv_list cl tk 577 if eq k 0 578 drv_mis DRERR DRBAD 579 ret 580 end 581 put ds 9 k 582 put ds 10 cnt 583 drv_fz st cl view part hq 0 hn 584 end 585 586 note one mutation (index ds 15) of cl checked by x509_cv_verify and x509_chain_verify; the latter's code 587 fn drv_fz1 st view uchar * cl view uchar * hq view uchar * to ulong 588 mu = new uchar 65536 0 589 k = get ds 9 590 c = drv_mut part cl 0 k part mu 0 k get ds 15 591 c = x509_cv_verify view part mu 0 k 0x0804 cl 16 592 ret x509_chain_verify view part mu 0 k st hq 593 end 594 595 fn drv_fz st view uchar * cl view uchar * hq view uchar * to void 596 cnt = get ds 10 597 rj = ulong 0 598 for j ulong cnt 599 put ds 15 j 600 if ne drv_fz1 st cl hq 0 601 rj = add rj 1 602 end 603 end 604 r = write 1 DRFUZ 0 len DRFUZ 605 r = write 1 nm 0 get ds 6 606 r = write 1 DRFN 0 len DRFN 607 drv_prn cnt 608 r = write 1 DRFR 0 len DRFR 609 drv_prn rj 610 r = write 1 DRNL 0 1 611 end 612 613 fn main to int 614 st = new uchar `1048576` 0 615 cl = new uchar 262144 0 616 ms = new uchar 65536 0 617 tk = new uchar 4096 0 618 hb = new uchar 4096 0 619 if ne drv_slurp 0 620 r = write 2 DRIO 0 len DRIO 621 ret 2 622 end 623 while lt get ds 1 get ds 0 bound 999999 624 put ds 2 add get ds 2 1 625 c = drv_peek 626 put ds 1 add get ds 1 1 627 if eq c 67 628 drv_c st cl tk 629 end 630 if eq c 86 631 drv_v cl ms tk 632 end 633 if eq c 82 634 drv_r st cl tk 635 end 636 if eq c 77 637 drv_m tk hb 638 end 639 if eq c 72 640 drv_h tk hb 641 end 642 if eq c 84 643 drv_t tk hb 644 end 645 if eq c 83 646 drv_s st tk 647 end 648 if eq c 69 649 drv_e tk hb 650 end 651 if eq c 70 652 drv_f st cl tk 653 end 654 if eq c 80 655 drv_p cl tk 656 end 657 if eq c 68 658 drv_d cl tk 659 end 660 if ne c 10 661 drv_eol 662 end 663 end 664 r = write 1 DRSUM 0 len DRSUM 665 drv_prn get ds 3 666 r = write 1 DRCHK 0 len DRCHK 667 drv_prn get ds 4 668 r = write 1 DRFLD 0 len DRFLD 669 if ne get ds 4 0 670 ret 1 671 end 672 if eq get ds 3 0 673 ret 1 674 end 675 ret 0 676 end