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
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note tests/tlshook.om: the TEST build's side of the TLS hooks (product builds link src/tlsprod.om instead). |
| 3 | note A test driver may fix the ClientHello body and the X25519 private key (RFC 8448 replay), fix the clock, pin the |
| 4 | note expected leaf certificate instead of validating a chain (the RFC 8448 server certificate has no SAN and expired in |
| 5 | note 2026), and record the four traffic secrets (ot_th_sec, slot 64 * which) and, when asked, print them to stderr as |
| 6 | note "LABEL secret-hex" lines. With nothing set, the hooks behave exactly as the product's. |
| 7 | global ot_th_v ulong 16 |
| 8 | global ot_th_ch uchar 2048 |
| 9 | global ot_th_priv uchar 32 |
| 10 | global ot_th_leaf uchar 65536 |
| 11 | global ot_th_rnd uchar 4096 |
| 12 | global ot_th_sec uchar 512 |
| 13 | text ot_th_l1 "CLIENT_HANDSHAKE_TRAFFIC_SECRET " |
| 14 | text ot_th_l2 "SERVER_HANDSHAKE_TRAFFIC_SECRET " |
| 15 | text ot_th_l3 "CLIENT_TRAFFIC_SECRET_0 " |
| 16 | text ot_th_l4 "SERVER_TRAFFIC_SECRET_0 " |
| 17 | text ot_th_nl "\n" |
| 18 | text ot_th_hex "0123456789abcdef" |
| 19 | note ot_th_v: 0 CH length (0 none), 1 now override (0 none), 2 leaf length (0 none), 3 keylog on, 4 fixed random |
| 20 | note length (0 none), 5 fixed random read position |
| 21 | fn ot_th_setch b view uchar * priv view uchar 32 to void |
| 22 | for i ulong len b |
| 23 | put ot_th_ch i get b i |
| 24 | end |
| 25 | for i ulong 32 |
| 26 | put ot_th_priv i get priv i |
| 27 | end |
| 28 | put ot_th_v 0 len b |
| 29 | end |
| 30 | fn ot_th_setnow t ulong to void |
| 31 | put ot_th_v 1 t |
| 32 | end |
| 33 | fn ot_th_pin d view uchar * to void |
| 34 | for i ulong len d |
| 35 | put ot_th_leaf i get d i |
| 36 | end |
| 37 | put ot_th_v 2 len d |
| 38 | end |
| 39 | fn ot_th_keylog on bool to void |
| 40 | put ot_th_v 3 0 |
| 41 | if on |
| 42 | put ot_th_v 3 1 |
| 43 | end |
| 44 | end |
| 45 | fn ot_th_fixrandom b view uchar * to void |
| 46 | for i ulong len b |
| 47 | put ot_th_rnd i get b i |
| 48 | end |
| 49 | put ot_th_v 4 len b |
| 50 | put ot_th_v 5 0 |
| 51 | end |
| 52 | |
| 53 | fn ot_tl_hk_random b edit uchar * to bool |
| 54 | n = get ot_th_v 4 |
| 55 | if gt n 0 |
| 56 | p = get ot_th_v 5 |
| 57 | for i ulong len b |
| 58 | put b i get ot_th_rnd rem add p i n |
| 59 | end |
| 60 | put ot_th_v 5 rem add p len b n |
| 61 | ret true |
| 62 | end |
| 63 | m = len b |
| 64 | q = ulong 0 |
| 65 | while lt q m bound 1000000 |
| 66 | k = random b q sub m q 0 |
| 67 | if eq k -4 |
| 68 | continue |
| 69 | end |
| 70 | if le k 0 |
| 71 | ret false |
| 72 | end |
| 73 | q = add q ulong k |
| 74 | end |
| 75 | ret true |
| 76 | end |
| 77 | fn ot_tl_hk_ch ch edit uchar 2048 priv edit uchar 32 to ulong |
| 78 | n = get ot_th_v 0 |
| 79 | if eq n 0 |
| 80 | ret 0 |
| 81 | end |
| 82 | for i ulong n |
| 83 | put ch i get ot_th_ch i |
| 84 | end |
| 85 | for i ulong 32 |
| 86 | put priv i get ot_th_priv i |
| 87 | end |
| 88 | ret n |
| 89 | end |
| 90 | note remember whether the first certificate equals the pinned one |
| 91 | fn ot_tl_hk_leaf d view uchar * to void |
| 92 | put ot_th_v 6 0 |
| 93 | n = get ot_th_v 2 |
| 94 | if or eq n 0 ne len d n |
| 95 | ret |
| 96 | end |
| 97 | for i ulong n |
| 98 | if ne get d i get ot_th_leaf i |
| 99 | ret |
| 100 | end |
| 101 | end |
| 102 | put ot_th_v 6 1 |
| 103 | end |
| 104 | note with a pinned leaf: 0 when the first certificate was it, else 14 (untrusted); 255 means validate normally |
| 105 | fn ot_tl_hk_cert to ulong |
| 106 | if eq get ot_th_v 2 0 |
| 107 | ret 255 |
| 108 | end |
| 109 | if eq get ot_th_v 6 1 |
| 110 | ret 0 |
| 111 | end |
| 112 | ret 14 |
| 113 | end |
| 114 | fn ot_tl_hk_now to long |
| 115 | t = get ot_th_v 1 |
| 116 | if gt t 0 |
| 117 | ret long t |
| 118 | end |
| 119 | ret div realns 1000000000 |
| 120 | end |
| 121 | fn ot_tl_klog which ulong s view uchar 64 to void |
| 122 | for i ulong 64 |
| 123 | put ot_th_sec add mul which 64 i get s i |
| 124 | end |
| 125 | if eq get ot_th_v 3 0 |
| 126 | ret |
| 127 | end |
| 128 | if eq which 1 |
| 129 | k = write 2 ot_th_l1 0 len ot_th_l1 |
| 130 | end |
| 131 | if eq which 2 |
| 132 | k = write 2 ot_th_l2 0 len ot_th_l2 |
| 133 | end |
| 134 | if eq which 3 |
| 135 | k = write 2 ot_th_l3 0 len ot_th_l3 |
| 136 | end |
| 137 | if eq which 4 |
| 138 | k = write 2 ot_th_l4 0 len ot_th_l4 |
| 139 | end |
| 140 | h = new uchar 64 0 |
| 141 | for i ulong 32 |
| 142 | put h mul i 2 get ot_th_hex ulong shr get s i 4 |
| 143 | put h add mul i 2 1 get ot_th_hex ulong and get s i 15 |
| 144 | end |
| 145 | k = write 2 h 0 64 |
| 146 | k = write 2 ot_th_nl 0 1 |
| 147 | end |