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 / net.om

475 lines · 9279 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note net.om: name resolution and TCP for omtls (prefix ot_dn_ and ot_io_).
3note Resolution order: a dotted IPv4 literal; then /etc/hosts (IPv4 lines); then one DNS A query over UDP to the
4note first IPv4 "nameserver" of /etc/resolv.conf (RFC 1035 4.1; recursion desired). The answer is not authenticated:
5note TLS certificate validation of the host name is what protects the connection, not DNS. IPv6 is not supported in
6note this slice (stated in the README). All failures are defined refusals (negative results), never traps.
7note Results: the resolved address is written to the caller's 4-byte array; return 0, or
8note -1 not a resolvable name, -2 no nameserver, -3 DNS I/O or timeout, -4 malformed or failed DNS answer.
9global ot_dn_buf uchar 1048576
10global ot_dn_v ulong 8
11text ot_dn_t_hosts "/etc/hosts"
12text ot_dn_t_resolv "/etc/resolv.conf"
13text ot_dn_t_ns "nameserver"
14const OT_DN_NS long 5000000000
15
16note ---- dotted IPv4 literal: true and the 4 bytes when s is exactly a.b.c.d with each part 0..255 ----
17fn ot_dn_ipv4 s view uchar * a edit uchar 4 to bool
18n = len s
19pi = ulong 0
20v = ulong 0
21digits = ulong 0
22for i ulong n
23c = get s i
24if eq c 46
25if or eq digits 0 ge pi 3
26ret false
27end
28put a pi uchar v
29pi = add pi 1
30v = 0
31digits = 0
32continue
33end
34if or lt c 48 gt c 57
35ret false
36end
37v = add mul v 10 ulong sub c 48
38digits = add digits 1
39if or gt v 255 gt digits 3
40ret false
41end
42end
43if or ne pi 3 eq digits 0
44ret false
45end
46put a 3 uchar v
47ret true
48end
49
50note ---- read a small system file into ot_dn_buf; length or -1 ----
51fn ot_dn_load path view uchar * to long
52fd = fopen path 0 len path 0 0
53if lt fd 0
54ret -1
55end
56n = ulong 0
57cap = len ot_dn_buf
58while lt n cap bound 9223372036854775807
59k = read int fd ot_dn_buf n sub cap n
60if eq k -4
61continue
62end
63if le k 0
64break
65end
66n = add n ulong k
67end
68z = fclose int fd
69ret long n
70end
71
72note ASCII case-insensitive comparison of ot_dn_buf[p, p+n) with s
73fn ot_dn_bufeq p ulong n ulong s view uchar * to bool
74if ne n len s
75ret false
76end
77for i ulong n
78x = get ot_dn_buf add p i
79y = get s i
80if and ge x 65 le x 90
81x = add x 32
82end
83if and ge y 65 le y 90
84y = add y 32
85end
86if ne x y
87ret false
88end
89end
90ret true
91end
92fn ot_dn_isspace c uchar to bool
93ret or or eq c 32 eq c 9 eq c 13
94end
95
96note ---- /etc/hosts: first IPv4 line naming host ----
97fn ot_dn_hosts host view uchar * a edit uchar 4 to bool
98n = ot_dn_load ot_dn_t_hosts
99if lt n 0
100ret false
101end
102e = ulong n
103p = ulong 0
104while lt p e bound 9223372036854775807
105q = p
106while lt q e bound 9223372036854775807
107if eq get ot_dn_buf q 10
108break
109end
110q = add q 1
111end
112if ot_dn_hostline p q host a
113ret true
114end
115p = add q 1
116end
117ret false
118end
119note the address token ot_dn_buf[i, j) as IPv4 into a
120fn ot_dn_tokip i ulong j ulong a edit uchar 4 to bool
121if gt sub j i 64
122ret false
123end
124t = new uchar 64 0
125for k ulong sub j i
126put t k get ot_dn_buf add i k
127end
128ret ot_dn_ipv4 part t 0 sub j i a
129end
130note the end of the token starting at i (space, tab, CR or '#' ends it)
131fn ot_dn_tokend i ulong q ulong to ulong
132j = i
133while lt j q bound 9223372036854775807
134c = get ot_dn_buf j
135if or ot_dn_isspace c eq c 35
136break
137end
138j = add j 1
139end
140ret j
141end
142fn ot_dn_hostline p ulong q ulong host view uchar * a edit uchar 4 to bool
143note tokens of one line; the first is the address, the others names; '#' starts a comment
144i = p
145first = true
146while lt i q bound 9223372036854775807
147while lt i q bound 9223372036854775807
148if not ot_dn_isspace get ot_dn_buf i
149break
150end
151i = add i 1
152end
153if ge i q
154break
155end
156if eq get ot_dn_buf i 35
157break
158end
159j = ot_dn_tokend i q
160if first
161first = false
162if not ot_dn_tokip i j a
163ret false
164end
165else
166if ot_dn_bufeq i sub j i host
167ret true
168end
169end
170i = j
171end
172ret false
173end
174
175note ---- the first IPv4 nameserver of /etc/resolv.conf ----
176fn ot_dn_nameserver a edit uchar 4 to bool
177n = ot_dn_load ot_dn_t_resolv
178if lt n 0
179ret false
180end
181e = ulong n
182p = ulong 0
183note (the nameserver line: "nameserver ADDRESS")
184while lt p e bound 9223372036854775807
185q = p
186while lt q e bound 9223372036854775807
187if eq get ot_dn_buf q 10
188break
189end
190q = add q 1
191end
192m = len ot_dn_t_ns
193if gt sub q p m
194if ot_dn_bufeq p m ot_dn_t_ns
195i = add p m
196while lt i q bound 9223372036854775807
197if not ot_dn_isspace get ot_dn_buf i
198break
199end
200i = add i 1
201end
202j = ot_dn_tokend i q
203if gt j i
204if ot_dn_tokip i j a
205ret true
206end
207end
208end
209end
210p = add q 1
211end
212ret false
213end
214
215note ---- DNS query for the A record of host ----
216note encode QNAME into b from offset 12; returns the end offset or 0 when host is not a valid name
217fn ot_dn_qname host view uchar * b edit uchar 512 to ulong
218n = len host
219if gt n 0
220if eq get host sub n 1 46
221n = sub n 1
222end
223end
224if or eq n 0 gt n 253
225ret 0
226end
227o = ulong 12
228s = ulong 0
229for i ulong add n 1
230brk = true
231if lt i n
232brk = eq get host i 46
233end
234if brk
235o = ot_dn_label host s sub i s b o
236if eq o 0
237ret 0
238end
239s = add i 1
240end
241end
242put b o 0
243ret add o 1
244end
245note one label host[s, s+l) at b[o]: length byte then the bytes; the next offset, or 0 when the label is invalid
246fn ot_dn_label host view uchar * s ulong l ulong b edit uchar 512 o ulong to ulong
247if or eq l 0 gt l 63
248ret 0
249end
250put b o uchar l
251for k ulong l
252c = get host add s k
253if or or le c 32 ge c 127 eq c 46
254ret 0
255end
256put b add add o 1 k c
257end
258ret add add o 1 l
259end
260note skip a (possibly compressed) name in the answer at offset o; returns the offset after it, or 0 when malformed
261fn ot_dn_skip r view uchar 1500 n ulong o ulong to ulong
262p = o
263note every pass consumes at least 2 bytes of a 1500-byte answer, so 1500 passes always suffice
264while true bound 1500
265if ge p n
266ret 0
267end
268c = get r p
269if eq c 0
270ret add p 1
271end
272if eq and c 192 192
273if ge add p 1 n
274ret 0
275end
276ret add p 2
277end
278if ne and c 192 0
279ret 0
280end
281p = add add p 1 ulong c
282end
283ret 0
284end
285fn ot_dn_query host view uchar * ns view uchar 4 a edit uchar 4 to long
286q = new uchar 512 0
287r = new uchar 1500 0
288k = random q 0 2 0
289if ne k 2
290ret -3
291end
292put q 2 1
293put q 5 1
294e = ot_dn_qname host q
295if eq e 0
296ret -1
297end
298put q add e 1 1
299put q add e 3 1
300ql = add e 4
301put ot_dn_v 0 ql
302got = ot_dn_xfer ns q ql r
303if lt got 12
304ret -3
305end
306ret ot_dn_answer r ulong got q a
307end
308note send the query q[0, ql) to ns:53 over UDP; the answer with the same id in r: its length, or -3
309fn ot_dn_xfer ns view uchar 4 q view uchar 512 ql ulong r edit uchar 1500 to long
310sa = new uchar 16 0
311put sa 0 2
312put sa 3 53
313for i ulong 4
314put sa add 4 i get ns i
315end
316fd = socknew 2 526338 0
317if lt fd 0
318ret -3
319end
320c = connect int fd sa 0 16
321w = long -1
322if eq c 0
323w = send int fd q 0 ql 0
324end
325if ne w long ql
326z = fclose int fd
327ret -3
328end
329got = ot_dn_wait int fd q r
330z = fclose int fd
331ret got
332end
333fn ot_dn_wait fd int q view uchar 512 r edit uchar 1500 to long
334until = add monons OT_DN_NS
335note junk datagrams are bounded by the 5 s deadline, not by a count
336while true bound 9223372036854775807
337left = sub until monons
338if le left 0
339ret -3
340end
341z = waitio fd 1 left
342if eq z -4
343continue
344end
345if le z 0
346ret -3
347end
348got = recv fd r 0 1500 0
349if ge got 12
350if and eq get r 0 get q 0 eq get r 1 get q 1
351ret got
352end
353end
354end
355ret -3
356end
357fn ot_dn_u16 r view uchar 1500 p ulong to ulong
358ret or shl ulong get r p 8 ulong get r add p 1
359end
360note the answer must repeat our question (bytes 12 .. ql of the query, names compared ASCII case-insensitively)
361fn ot_dn_answer r view uchar 1500 n ulong q view uchar 512 a edit uchar 4 to long
362ql = get ot_dn_v 0
363note header: QR set, opcode 0, RCODE 0, one question
364if or ne and get r 2 248 128 ne and get r 3 15 0
365ret -4
366end
367if or ne get r 4 0 ne get r 5 1
368ret -4
369end
370if gt ql n
371ret -4
372end
373if not ot_dn_qsame r q ql
374ret -4
375end
376ret ot_dn_rrs r n ql a
377end
378fn ot_dn_qsame r view uchar 1500 q view uchar 512 ql ulong to bool
379for i ulong sub ql 12
380x = get r add 12 i
381y = get q add 12 i
382if and ge x 65 le x 90
383x = add x 32
384end
385if and ge y 65 le y 90
386y = add y 32
387end
388if ne x y
389ret false
390end
391end
392ret true
393end
394note the answer records after the question at ql: the first IN A record with 4 bytes of data
395fn ot_dn_rrs r view uchar 1500 n ulong ql ulong a edit uchar 4 to long
396an = ot_dn_u16 r 6
397p = ql
398for i ulong an
399p = ot_dn_skip r n p
400if or eq p 0 gt add p 10 n
401ret -4
402end
403ty = ot_dn_u16 r p
404cl = ot_dn_u16 r add p 2
405rl = ot_dn_u16 r add p 8
406p = add p 10
407if gt add p rl n
408ret -4
409end
410if and and eq ty 1 eq cl 1 eq rl 4
411for j ulong 4
412put a j get r add p j
413end
414ret 0
415end
416p = add p rl
417end
418ret -4
419end
420
421fn ot_dn_resolve host view uchar * a edit uchar 4 to long
422if ot_dn_ipv4 host a
423ret 0
424end
425if ot_dn_hosts host a
426ret 0
427end
428ns = new uchar 4 0
429if not ot_dn_nameserver ns
430ret -2
431end
432ret ot_dn_query host ns a
433end
434
435note ---- TCP connect to a.b.c.d:port, nonblocking; fd or a negative errno ----
436fn ot_io_connect a view uchar 4 port ulong until long to long
437sa = new uchar 16 0
438pn = new uchar 32 0
439put sa 0 2
440put sa 2 uchar shr port 8
441put sa 3 uchar and port 255
442for i ulong 4
443put sa add 4 i get a i
444end
445fd = socknew 2 526337 0
446if lt fd 0
447ret fd
448end
449r = connect int fd sa 0 16
450if eq r -115
451left = sub until monons
452z = long 0
453if gt left 0
454z = waitio int fd 4 left
455end
456if le z 0
457c = fclose int fd
458ret -110
459end
460note writable is also how a refused connection reports itself: only a peer address proves it connected
461if lt peername int fd pn 0 32 0
462c = fclose int fd
463ret -111
464end
465r = 0
466end
467if ne r 0
468c = fclose int fd
469ret r
470end
471one = new uchar 4 0
472put one 0 1
473s = sockopt int fd 6 1 one 0 4
474ret fd
475end