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 / om / tls / test / bogo / shim.om
| 1 | note shim.om: BoringSSL BoGo test shim for the om/tls client (program mode, composed after every |
| 2 | note om/tls unit by om/tls/build.sh). Protocol as in BoringSSL ssl/test/bssl_shim.cc and |
| 3 | note runner/runner.go at the commit in PINS: |
| 4 | note connect to the runner on 127.0.0.1 or, with -ipv6, ::1 at -port, send -shim-id as 8 bytes |
| 5 | note little-endian, run a TLS 1.3 client handshake for the name "test" against the PEM root of |
| 6 | note -trust-cert, then echo: every byte read is written back XORed with 0xff, until the peer's |
| 7 | note close_notify or a clean TCP end. |
| 8 | note Flags handled: -port N, -shim-id N, -ipv6, -trust-cert PATH, -shim-writes-first ("hello" before |
| 9 | note the first read), -shim-shuts-down (send close_notify instead of reading), -check-close-notify |
| 10 | note (the read loop must end with the peer's close_notify), -max-version N (N >= 772), |
| 11 | note -min-version N (N <= 772), -expect-version 772, -async and -expect-no-session (no effect: the |
| 12 | note client is blocking and keeps no session cache). -is-handshaker-supported prints "No". |
| 13 | note Every other flag, server tests included, exits 89 (the runner's "unimplemented"). |
| 14 | note Exit 0 on success; on a TLS failure print "TLSERR <name> detail <n>" on stderr and exit 1; |
| 15 | note exit 2 for a bad flag value, 3 when the runner cannot be reached. |
| 16 | |
| 17 | text sh_port "-port" |
| 18 | text sh_id "-shim-id" |
| 19 | text sh_ipv6 "-ipv6" |
| 20 | text sh_trust "-trust-cert" |
| 21 | text sh_wf "-shim-writes-first" |
| 22 | text sh_sd "-shim-shuts-down" |
| 23 | text sh_ccn "-check-close-notify" |
| 24 | text sh_maxv "-max-version" |
| 25 | text sh_minv "-min-version" |
| 26 | text sh_expv "-expect-version" |
| 27 | text sh_async "-async" |
| 28 | text sh_nos "-expect-no-session" |
| 29 | text sh_hss "-is-handshaker-supported" |
| 30 | text sh_no "No\n" |
| 31 | text sh_name "test" |
| 32 | text sh_hello "hello" |
| 33 | text sh_err "TLSERR " |
| 34 | text sh_det " detail " |
| 35 | text sh_nl "\n" |
| 36 | text sh_sp " " |
| 37 | |
| 38 | note a[0..n) equals text t |
| 39 | fn sh_is a view uchar * n ulong t view uchar * to bool |
| 40 | if ne n len t |
| 41 | ret false |
| 42 | end |
| 43 | for i ulong n |
| 44 | if ne get a i get t i |
| 45 | ret false |
| 46 | end |
| 47 | end |
| 48 | ret true |
| 49 | end |
| 50 | |
| 51 | note argument i into a; its length, or 0 when it is empty or does not fit |
| 52 | fn sh_arg i ulong a edit uchar * to ulong |
| 53 | n = argread i a 0 len a |
| 54 | if lt n 0 |
| 55 | ret 0 |
| 56 | end |
| 57 | ret ulong n |
| 58 | end |
| 59 | |
| 60 | note decimal a[0..n) (1..19 digits) into v[k]; 0, or 2 when it is not a number |
| 61 | fn sh_num a view uchar * n ulong v edit ulong * k ulong to ulong |
| 62 | if or eq n 0 gt n 19 |
| 63 | ret 2 |
| 64 | end |
| 65 | x = ulong 0 |
| 66 | for i ulong n |
| 67 | d = ulong get a i |
| 68 | if or lt d 48 gt d 57 |
| 69 | ret 2 |
| 70 | end |
| 71 | x = add mul x 10 sub d 48 |
| 72 | end |
| 73 | put v k x |
| 74 | ret 0 |
| 75 | end |
| 76 | |
| 77 | fn sh_dec v ulong o edit uchar * to ulong |
| 78 | t = new uchar 24 0 |
| 79 | n = ulong 0 |
| 80 | x = v |
| 81 | while true bound 24 |
| 82 | put t n uchar add 48 rem x 10 |
| 83 | n = add n 1 |
| 84 | x = div x 10 |
| 85 | if eq x 0 |
| 86 | break |
| 87 | end |
| 88 | end |
| 89 | for i ulong n |
| 90 | put o i get t sub sub n 1 i |
| 91 | end |
| 92 | ret n |
| 93 | end |
| 94 | |
| 95 | fn sh_fail c view uchar * e ulong to int |
| 96 | m = new uchar 256 0 |
| 97 | w = write 2 sh_err 0 len sh_err |
| 98 | n = tls_errname e m |
| 99 | w = write 2 m 0 n |
| 100 | w = write 2 sh_det 0 len sh_det |
| 101 | d = tls_detail c |
| 102 | n = sh_dec d m |
| 103 | w = write 2 m 0 n |
| 104 | if or eq e 17 eq e 19 |
| 105 | n = x509_errname d m |
| 106 | w = write 2 sh_sp 0 1 |
| 107 | w = write 2 m 0 n |
| 108 | end |
| 109 | w = write 2 sh_nl 0 1 |
| 110 | ret 1 |
| 111 | end |
| 112 | |
| 113 | note flags into v: 0 port, 1 shim id, 2 ipv6, 3 trust path length (path in p), 4 writes first, |
| 114 | note 5 shuts down, 6 check close_notify, 7 scratch number; 0, 2 (bad value) or 89 (unimplemented) |
| 115 | fn sh_parse a edit uchar * p edit uchar * v edit ulong * to ulong |
| 116 | na = ulong argcnt |
| 117 | i = ulong 1 |
| 118 | while lt i na bound 4096 |
| 119 | n = sh_arg i a |
| 120 | i = add i 1 |
| 121 | if sh_is a n sh_ipv6 |
| 122 | put v 2 1 |
| 123 | continue |
| 124 | end |
| 125 | if sh_is a n sh_wf |
| 126 | put v 4 1 |
| 127 | continue |
| 128 | end |
| 129 | if sh_is a n sh_sd |
| 130 | put v 5 1 |
| 131 | continue |
| 132 | end |
| 133 | if sh_is a n sh_ccn |
| 134 | put v 6 1 |
| 135 | continue |
| 136 | end |
| 137 | if or sh_is a n sh_async sh_is a n sh_nos |
| 138 | continue |
| 139 | end |
| 140 | k = ulong 99 |
| 141 | if sh_is a n sh_port |
| 142 | k = 0 |
| 143 | end |
| 144 | if sh_is a n sh_id |
| 145 | k = 1 |
| 146 | end |
| 147 | if or sh_is a n sh_maxv or sh_is a n sh_minv sh_is a n sh_expv |
| 148 | k = 7 |
| 149 | end |
| 150 | if sh_is a n sh_trust |
| 151 | if ge i na |
| 152 | ret 2 |
| 153 | end |
| 154 | m = sh_arg i p |
| 155 | if eq m 0 |
| 156 | ret 2 |
| 157 | end |
| 158 | put v 3 m |
| 159 | i = add i 1 |
| 160 | continue |
| 161 | end |
| 162 | if eq k 99 |
| 163 | ret 89 |
| 164 | end |
| 165 | if ge i na |
| 166 | ret 2 |
| 167 | end |
| 168 | m = sh_arg i a |
| 169 | if ne sh_num part a 0 m m v k 0 |
| 170 | ret 2 |
| 171 | end |
| 172 | i = add i 1 |
| 173 | if eq k 7 |
| 174 | r = get v 7 |
| 175 | if and sh_is a n sh_maxv lt r 772 |
| 176 | ret 89 |
| 177 | end |
| 178 | if and sh_is a n sh_minv gt r 772 |
| 179 | ret 89 |
| 180 | end |
| 181 | if and sh_is a n sh_expv ne r 772 |
| 182 | ret 89 |
| 183 | end |
| 184 | end |
| 185 | end |
| 186 | if or eq get v 0 0 gt get v 0 65535 |
| 187 | ret 2 |
| 188 | end |
| 189 | if and ne get v 5 0 ne get v 6 0 |
| 190 | ret 89 |
| 191 | end |
| 192 | ret 0 |
| 193 | end |
| 194 | |
| 195 | note connect to 127.0.0.1 or ::1 at port (non-blocking connect, 10 s limit); fd or a negative value |
| 196 | fn sh_open v6 ulong port ulong to long |
| 197 | ip = new uchar 4 0 |
| 198 | sa = new uchar 28 0 |
| 199 | if eq v6 0 |
| 200 | put ip 0 127 |
| 201 | put ip 3 1 |
| 202 | ret tcp_open4 ip port |
| 203 | end |
| 204 | put sa 0 10 |
| 205 | put sa 2 uchar and shr port 8 255 |
| 206 | put sa 3 uchar and port 255 |
| 207 | put sa 23 1 |
| 208 | fd = socknew 10 526337 0 |
| 209 | if lt fd 0 |
| 210 | ret neg long 1 |
| 211 | end |
| 212 | f = int fd |
| 213 | r = connect f sa 0 28 |
| 214 | if eq r -115 |
| 215 | w = waitio f 4 10000000000 |
| 216 | if le w 0 |
| 217 | x = fclose f |
| 218 | ret neg long 1 |
| 219 | end |
| 220 | r = connect f sa 0 28 |
| 221 | if eq r -106 |
| 222 | r = 0 |
| 223 | end |
| 224 | end |
| 225 | if ne r 0 |
| 226 | x = fclose f |
| 227 | ret neg long 1 |
| 228 | end |
| 229 | ret fd |
| 230 | end |
| 231 | |
| 232 | note the 8-byte little-endian shim id, all of it or 3 |
| 233 | fn sh_id8 f int id ulong to int |
| 234 | b = new uchar 8 0 |
| 235 | for i ulong 8 |
| 236 | put b i uchar and shr id mul i 8 255 |
| 237 | end |
| 238 | o = ulong 0 |
| 239 | while lt o 8 bound 64 |
| 240 | w = waitio f 4 10000000000 |
| 241 | if le w 0 |
| 242 | ret 3 |
| 243 | end |
| 244 | r = send f b o sub 8 o 0 |
| 245 | if le r 0 |
| 246 | if eq r -11 |
| 247 | continue |
| 248 | end |
| 249 | ret 3 |
| 250 | end |
| 251 | o = add o ulong r |
| 252 | end |
| 253 | ret 0 |
| 254 | end |
| 255 | |
| 256 | note echo loop: 0, or a TLS code (TLEEOF when -check-close-notify saw no close_notify) |
| 257 | fn sh_echo c edit uchar * v view ulong * to ulong |
| 258 | if ne get v 4 0 |
| 259 | r = tls_write c sh_hello |
| 260 | if lt r 0 |
| 261 | ret ulong neg r |
| 262 | end |
| 263 | end |
| 264 | if ne get v 5 0 |
| 265 | tls_close c |
| 266 | ret 0 |
| 267 | end |
| 268 | b = new uchar 512 0 |
| 269 | while true bound 999999 |
| 270 | r = tls_read c b |
| 271 | if eq r 0 |
| 272 | tls_close c |
| 273 | ret 0 |
| 274 | end |
| 275 | if eq r neg long 4 |
| 276 | if ne get v 6 0 |
| 277 | ret 4 |
| 278 | end |
| 279 | ret 0 |
| 280 | end |
| 281 | if lt r 0 |
| 282 | ret ulong neg r |
| 283 | end |
| 284 | m = ulong r |
| 285 | for i ulong m |
| 286 | put b i uchar xor get b i 255 |
| 287 | end |
| 288 | w = tls_write c part b 0 ulong r |
| 289 | if lt w 0 |
| 290 | ret ulong neg w |
| 291 | end |
| 292 | end |
| 293 | ret 7 |
| 294 | end |
| 295 | |
| 296 | note after an abort: read and discard what the runner already sent (bounded, 200 ms idle) so the |
| 297 | note close is a FIN, not a TCP RST; an RST over unread data can drop the alert we just sent |
| 298 | fn sh_drain f int to void |
| 299 | b = new uchar 4096 0 |
| 300 | for i ulong 256 |
| 301 | w = waitio f 1 200000000 |
| 302 | if le w 0 |
| 303 | ret |
| 304 | end |
| 305 | n = recv f b 0 4096 0 |
| 306 | if le n 0 |
| 307 | ret |
| 308 | end |
| 309 | end |
| 310 | end |
| 311 | |
| 312 | note handshake and echo on the runner connection; c holds the TLS state, st the trust anchors |
| 313 | fn sh_run c edit uchar * st edit uchar * v view ulong * to int |
| 314 | e = tls_init c sh_name 0 |
| 315 | if ne e 0 |
| 316 | ret sh_fail c e |
| 317 | end |
| 318 | fd = sh_open get v 2 get v 0 |
| 319 | if lt fd 0 |
| 320 | ret 3 |
| 321 | end |
| 322 | f = int fd |
| 323 | if ne sh_id8 f get v 1 0 |
| 324 | x = fclose f |
| 325 | ret 3 |
| 326 | end |
| 327 | e = tls_connect c st f |
| 328 | if eq e 0 |
| 329 | e = sh_echo c v |
| 330 | end |
| 331 | if ne e 0 |
| 332 | sh_drain f |
| 333 | end |
| 334 | x = fclose f |
| 335 | if eq e 0 |
| 336 | ret 0 |
| 337 | end |
| 338 | ret sh_fail c e |
| 339 | end |
| 340 | |
| 341 | fn main to int |
| 342 | c = new uchar TLCSZ 0 |
| 343 | st = new uchar TLSTSZ 0 |
| 344 | a = new uchar 4096 0 |
| 345 | p = new uchar 4096 0 |
| 346 | v = new ulong 8 0 |
| 347 | if ge argcnt 2 |
| 348 | n = sh_arg 1 a |
| 349 | if sh_is a n sh_hss |
| 350 | w = write 1 sh_no 0 len sh_no |
| 351 | ret 0 |
| 352 | end |
| 353 | end |
| 354 | e = sh_parse a p v |
| 355 | if ne e 0 |
| 356 | ret int e |
| 357 | end |
| 358 | if eq get v 3 0 |
| 359 | ret 89 |
| 360 | end |
| 361 | e = tls_storef st part p 0 get v 3 |
| 362 | if ne e 0 |
| 363 | ret sh_fail c e |
| 364 | end |
| 365 | ret sh_run c st v |
| 366 | end |