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