interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / net / https.om

212 lines · 6776 bytes Raw Blame History
1note seed/net/https.om -- HTTPS transport for http.om: the TLS 1.3 client of om/tls plugged into
2note the transport boundary (hrecvio, hrecvd, hsendio dispatch here when HXPORT is 1).
3note Library unit: no `om2` line, no `fn main`, no globals. Compose the om/tls units, http.om and
4note this file (in that order) in front of the application:
5note { printf 'om2\n'; for u in sha2 hkdf drbg x25519 sha3 mlkem chacha aesgcm ec ecdh rsa x509 rev fips tls; do cat om/tls/$u.om; done
6note cat seed/net/http.om seed/net/https.om app.om; } | seed/out/omc > app
7note A program without TLS composes seed/net/notls.om instead (same three function names, HENOSUP).
8note
9note The TLS connection state lives in the transport region io[HTB, HTB + TLCSZ) that htlsinit
10note reserves at the end of io; the trust store is a caller-owned array of TLSTSZ bytes filled by
11note tls_store / tls_storef. Because Omega passes an array to one call once, bytes between the
12note TLS state and the read / write regions of the same io cross a 16 KiB bounce array (one copy).
13note
14note Client use (hinit first, no I/O before htlsinit):
15note fd = hdial a tmo TCP
16note r = hinit cs io int fd 0
17note r = htlsinit cs io host lay out the transport region, SNI and certificate name
18note r = htlsconn cs io st handshake (tls_connect) under the wait policy of cs
19note ... hreqst / hhend / hwfin / hrsprd / hbodyrd as over TCP ...
20note htlsend cs io close_notify, wipe the keys, close fd
21note Results: TLS error code e (tls_errname) is returned as HETLS - e (-601 .. -632);
22note TLS_ERR_TIMEOUT is HETIME. A TCP end of stream without close_notify is an error (HETLS - 4,
23note truncation), never a clean end; only close_notify gives 0 from the reads.
24note
25note Timeouts: before the handshake and before each TLS read or write the http.om wait policy
26note (HIDLE single-wait limit and the HDL deadline) and the exchange deadline HXDL (htlsdl) are
27note given to tls_deadline, so one monotonic deadline bounds handshake, write and read together
28note and the bounds of seed/net/README.md hold over TLS. With no limit set at all, tls_connect
29note keeps its own handshake budget.
30note Refusals: a certificate the trust store, the clock or the name does not accept is
31note HETLS - 17 (TLS_ERR_CERT) before any application byte is written; htlsdet gives the x509
32note reason (x509_errname). There is no fallback to plain HTTP on any TLS error.
33
34const HETLS long -600
35const HTLBUF ulong 16384
36
37note htlsinit: reserve io[HTB, HTB + TLCSZ) for the TLS state (io must hold it plus the read and
38note write regions: at least TLCSZ + 512 bytes) and start the client for TLS name host with ALPN
39note http/1.1. HEINVAL when I/O already happened on cs or io is too small.
40fn htlsinit cs edit ulong * io edit uchar * host view uchar * to long
41if or ne get cs HRLEN 0 ne get cs HWLEN 0
42ret HEINVAL
43end
44if or ne get cs HMEM 0 ne get cs HOPEN 1
45ret HEINVAL
46end
47if lt len io add TLCSZ 512
48ret HEINVAL
49end
50n = sub len io TLCSZ
51w = get cs HWCAP
52if gt w sub n 256
53w = sub n 256
54end
55r = hlayout cs n w
56if lt r 0
57ret r
58end
59put cs HTL TLCSZ
60e = tls_init part io n TLCSZ host 1
61if ne e 0
62ret sub HETLS long e
63end
64put cs HXPORT 1
65ret 0
66end
67
68note htlsconn: the TLS handshake on the connection's fd with trust store st (tls_store or
69note tls_storef) under the wait policy of cs (htlsarm); 0, HETIME or HETLS - e
70fn htlsconn cs edit ulong * io edit uchar * st view uchar * to long
71if ne get cs HXPORT 1
72ret HEINVAL
73end
74htlsarm cs io
75b = get cs HTB
76e = tls_connect part io b TLCSZ st int get cs HFD
77ret htlsmap long e
78end
79
80note htlsmap: a TLS code as an http.om result
81fn htlsmap e long to long
82if eq e 0
83ret 0
84end
85if eq e long TLETIME
86ret HETIME
87end
88ret sub HETLS e
89end
90
91note htlsarm: give the TLS state the wait policy of cs (the earliest of now + idle, the deadline HDL
92note and the exchange deadline HXDL), 0 = none
93fn htlsarm cs edit ulong * io edit uchar * to void
94b = get cs HTB
95dl = get cs HDL
96iv = get cs HIDLE
97if ne iv 0
98t = add ulong monons iv
99if or eq dl 0 lt t dl
100dl = t
101end
102end
103x = get cs HXDL
104if and ne x 0 or eq dl 0 lt x dl
105dl = x
106end
107tls_deadline part io b TLCSZ long dl
108end
109
110note htlsdl: one deadline for the whole exchange, iv ns from now (0 or negative = none); call after
111note hinit (which clears it); it bounds the handshake, every TLS write and every TLS read
112fn htlsdl cs edit ulong * iv long to void
113put cs HXDL 0
114if gt iv 0
115put cs HXDL add ulong monons ulong iv
116end
117end
118
119note htlsdet: why the last TLS operation failed; for HETLS - 17 the x509 reject code (x509_errname:
120note 2 expired, 3 not yet valid, 4 name mismatch, 5 unknown issuer, ...); 0 without TLS
121fn htlsdet cs view ulong * io view uchar * to ulong
122if ne get cs HXPORT 1
123ret 0
124end
125ret tls_detail part io get cs HTB TLCSZ
126end
127
128note htlsrecv: at most n bytes into io[off, off+n) through TLS; count, 0 at close_notify, or
129note negative (HETIME, HETLS - e)
130fn htlsrecv cs edit ulong * io edit uchar * off ulong n ulong to long
131bb = new uchar 16384 0
132k = n
133if gt k HTLBUF
134k = HTLBUF
135end
136htlsarm cs io
137r = tls_read part io get cs HTB TLCSZ part bb 0 k
138if lt r 0
139ret htlsmap neg r
140end
141for i ulong ulong r
142put io add off i get bb i
143end
144ret r
145end
146
147note htlsrecvd: at most len d bytes straight into the caller's array d
148fn htlsrecvd cs edit ulong * io edit uchar * d edit uchar * to long
149k = len d
150if gt k HTLBUF
151k = HTLBUF
152end
153htlsarm cs io
154r = tls_read part io get cs HTB TLCSZ part d 0 k
155if lt r 0
156ret htlsmap neg r
157end
158ret r
159end
160
161note htlscp: io[s, s+n) into bb (at most 16 KiB); the count copied
162fn htlscp bb edit uchar 16384 io view uchar * s ulong n ulong to ulong
163k = n
164if gt k HTLBUF
165k = HTLBUF
166end
167for i ulong k
168put bb i get io add s i
169end
170ret k
171end
172
173note htlswr: one TLS write of bb[0, k)
174fn htlswr cs edit ulong * io edit uchar * bb view uchar 16384 k ulong to long
175htlsarm cs io
176r = tls_write part io get cs HTB TLCSZ part bb 0 k
177if lt r 0
178ret htlsmap neg r
179end
180ret 0
181end
182
183note htlssend: all of io[off, off+n) through TLS in records of at most 16 KiB; 0 or negative.
184note HEINVAL (nothing sent) when the range is not inside io. io is a local array of at most 4 MiB
185note (omc error 23 above), so at most 256 records; the loop bound 1024 is not reachable.
186fn htlssend cs edit ulong * io edit uchar * off ulong n ulong to long
187if gt off len io
188ret HEINVAL
189end
190if gt n sub len io off
191ret HEINVAL
192end
193bb = new uchar 16384 0
194p = ulong 0
195while lt p n bound 1024
196k = htlscp bb io add off p sub n p
197r = htlswr cs io bb k
198if lt r 0
199ret r
200end
201p = add p k
202end
203ret 0
204end
205
206note htlsend: close_notify when still connected, wipe the TLS keys and buffers, close the fd
207fn htlsend cs edit ulong * io edit uchar * to long
208if eq get cs HXPORT 1
209tls_close part io get cs HTB TLCSZ
210end
211ret hclose cs
212end