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 cli.om: the omtls command. One TLS 1.3 connection: handshake, then send standard input, then copy what the |
| 3 | note server sends to standard output until close_notify (or a defined error). |
| 4 | note usage: omtls HOST PORT [ROOTS [ALPN [CONNECT-ADDRESS]]] |
| 5 | note ROOTS default /etc/ssl/certs/ca-certificates.crt; ALPN "" for none; CONNECT-ADDRESS an IPv4 literal that |
| 6 | note replaces name resolution for the TCP connection (the certificate is still checked against HOST). |
| 7 | note stderr gets one line "omtls: connected suite=0x1303 scheme=0x0804 transcript=<64 hex>" or |
| 8 | note "omtls: error N: <reason>". Exit 0 when the connection ended with close_notify, 1 on a TLS error, 2 on usage. |
| 9 | global cl_a uchar 65536 |
| 10 | global cl_v ulong 8 |
| 11 | text cl_defroots "/etc/ssl/certs/ca-certificates.crt" |
| 12 | text cl_usage "usage: omtls HOST PORT [ROOTS [ALPN [CONNECT-ADDRESS]]]\n" |
| 13 | text cl_conn "omtls: connected suite=0x" |
| 14 | text cl_sch " scheme=0x" |
| 15 | text cl_tr " transcript=" |
| 16 | text cl_err "omtls: error " |
| 17 | text cl_colon ": " |
| 18 | text cl_nl "\n" |
| 19 | text cl_noroots "omtls: cannot load the trust store\n" |
| 20 | text cl_hex "0123456789abcdef" |
| 21 | |
| 22 | fn cl_say t view uchar * to void |
| 23 | k = write 2 t 0 len t |
| 24 | end |
| 25 | fn cl_hex16 x ulong to void |
| 26 | d = new uchar 4 0 |
| 27 | for i ulong 4 |
| 28 | put d i get cl_hex and shr x mul sub 3 i 4 15 |
| 29 | end |
| 30 | k = write 2 d 0 4 |
| 31 | end |
| 32 | fn cl_dec n ulong to void |
| 33 | d = new uchar 24 0 |
| 34 | v = n |
| 35 | i = ulong 24 |
| 36 | while true bound 30 |
| 37 | i = sub i 1 |
| 38 | put d i uchar add 48 rem v 10 |
| 39 | v = div v 10 |
| 40 | if eq v 0 |
| 41 | break |
| 42 | end |
| 43 | end |
| 44 | k = write 2 d i sub 24 i |
| 45 | end |
| 46 | note argument i into b; its length (0 when absent) |
| 47 | fn cl_arg i ulong b edit uchar 4096 to ulong |
| 48 | if ge i ulong argcnt |
| 49 | ret 0 |
| 50 | end |
| 51 | k = argread i b 0 4096 |
| 52 | if lt k 0 |
| 53 | ret 0 |
| 54 | end |
| 55 | ret ulong k |
| 56 | end |
| 57 | fn cl_port b view uchar * to ulong |
| 58 | v = ulong 0 |
| 59 | if or eq len b 0 gt len b 5 |
| 60 | ret 0 |
| 61 | end |
| 62 | for i ulong len b |
| 63 | c = get b i |
| 64 | if or lt c 48 gt c 57 |
| 65 | ret 0 |
| 66 | end |
| 67 | v = add mul v 10 ulong sub c 48 |
| 68 | end |
| 69 | ret v |
| 70 | end |
| 71 | fn cl_fail to int |
| 72 | w = new uchar 128 0 |
| 73 | cl_say cl_err |
| 74 | cl_dec ulong ot_tl_err |
| 75 | cl_say cl_colon |
| 76 | n = ot_tl_why w |
| 77 | k = write 2 w 0 n |
| 78 | cl_say cl_nl |
| 79 | ret 1 |
| 80 | end |
| 81 | fn cl_connected to void |
| 82 | cl_say cl_conn |
| 83 | cl_hex16 ot_tl_suite |
| 84 | cl_say cl_sch |
| 85 | cl_hex16 ot_tl_scheme |
| 86 | cl_say cl_tr |
| 87 | t = new uchar 32 0 |
| 88 | ot_tl_thash t |
| 89 | h = new uchar 64 0 |
| 90 | for i ulong 32 |
| 91 | put h mul i 2 get cl_hex ulong shr get t i 4 |
| 92 | put h add mul i 2 1 get cl_hex ulong and get t i 15 |
| 93 | end |
| 94 | k = write 2 h 0 64 |
| 95 | cl_say cl_nl |
| 96 | end |
| 97 | note standard input to the connection (in pieces of 64 KiB), then the server's data to standard output |
| 98 | fn cl_pump to int |
| 99 | b = new uchar 65536 0 |
| 100 | while true bound 9223372036854775807 |
| 101 | k = read 0 b 0 65536 |
| 102 | if eq k -4 |
| 103 | continue |
| 104 | end |
| 105 | if le k 0 |
| 106 | break |
| 107 | end |
| 108 | if not ot_tl_write part b 0 ulong k |
| 109 | ret cl_fail |
| 110 | end |
| 111 | end |
| 112 | while true bound 9223372036854775807 |
| 113 | r = ot_tl_read b |
| 114 | if eq r 0 |
| 115 | ot_tl_close |
| 116 | ret 0 |
| 117 | end |
| 118 | if lt r 0 |
| 119 | ret cl_fail |
| 120 | end |
| 121 | p = ulong 0 |
| 122 | while lt p ulong r bound 9223372036854775807 |
| 123 | w = write 1 b p sub ulong r p |
| 124 | if le w 0 |
| 125 | ot_tl_close |
| 126 | ret 1 |
| 127 | end |
| 128 | p = add p ulong w |
| 129 | end |
| 130 | end |
| 131 | ret 1 |
| 132 | end |
| 133 | fn main to int |
| 134 | host = new uchar 4096 0 |
| 135 | ab = new uchar 4096 0 |
| 136 | hn = cl_arg 1 host |
| 137 | port = cl_portarg |
| 138 | if or eq hn 0 eq port 0 |
| 139 | cl_say cl_usage |
| 140 | ret 2 |
| 141 | end |
| 142 | s = cl_setup |
| 143 | if ne s 0 |
| 144 | ret s |
| 145 | end |
| 146 | an = cl_arg 4 ab |
| 147 | c = ot_tl_connect part host 0 hn port part ab 0 an |
| 148 | if ne c 0 |
| 149 | ret cl_fail |
| 150 | end |
| 151 | cl_connected |
| 152 | ret cl_pump |
| 153 | end |
| 154 | fn cl_portarg to ulong |
| 155 | pb = new uchar 4096 0 |
| 156 | pn = cl_arg 2 pb |
| 157 | if eq pn 0 |
| 158 | ret 0 |
| 159 | end |
| 160 | ret cl_port part pb 0 pn |
| 161 | end |
| 162 | note the trust store (argument 3 or the system bundle) and the connect-address override (argument 5) |
| 163 | fn cl_setup to int |
| 164 | rb = new uchar 4096 0 |
| 165 | vb = new uchar 4096 0 |
| 166 | rn = cl_arg 3 rb |
| 167 | vn = cl_arg 5 vb |
| 168 | r = long 0 |
| 169 | if gt rn 0 |
| 170 | r = ot_tl_roots part rb 0 rn |
| 171 | else |
| 172 | r = ot_tl_roots cl_defroots |
| 173 | end |
| 174 | if le r 0 |
| 175 | cl_say cl_noroots |
| 176 | ret 1 |
| 177 | end |
| 178 | if gt vn 0 |
| 179 | if not ot_tl_via part vb 0 vn |
| 180 | cl_say cl_usage |
| 181 | ret 2 |
| 182 | end |
| 183 | end |
| 184 | ret 0 |
| 185 | end |