interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omtls / src / cli.om

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