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 / protocol / interplane / core / envelope.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/interplane/core/envelope.om: pure-Omega port of interplane-core envelope, |
| 3 | note version, identifier validation, strict untrusted-input parser, and canonical trust digest. |
| 4 | note Invariant: parsing NEVER executes and NEVER grants authorization. |
| 5 | note Compose after si/directives/om2.om and om/tls/sha2.om. |
| 6 | |
| 7 | const IP_OK ulong 0 |
| 8 | const IP_ERR_BAD_LEN ulong 1 |
| 9 | const IP_ERR_BAD_CHAR ulong 2 |
| 10 | const IP_ERR_BAD_VERSION ulong 3 |
| 11 | const IP_ERR_BAD_IDENT ulong 4 |
| 12 | const IP_ERR_BAD_HEX ulong 5 |
| 13 | const IP_ERR_DUP_KEY ulong 6 |
| 14 | const IP_ERR_UNKNOWN_KEY ulong 7 |
| 15 | const IP_ERR_MISSING_FIELD ulong 8 |
| 16 | const IP_ERR_BAD_LINE ulong 9 |
| 17 | const IP_ERR_TOO_MANY_LINES ulong 10 |
| 18 | const IP_ERR_BAD_TRANSPORT ulong 11 |
| 19 | const IP_ERR_BAD_POLICY_HINT ulong 12 |
| 20 | |
| 21 | const IP_MAX_ENV_BYTES ulong 4096 |
| 22 | const IP_MAX_ENV_LINES ulong 32 |
| 23 | |
| 24 | const TRANS_UNTRUSTED ulong 0 |
| 25 | const TRANS_TLS ulong 1 |
| 26 | const TRANS_MTLS ulong 2 |
| 27 | const TRANS_LOOPBACK ulong 3 |
| 28 | |
| 29 | const POL_DENY_ALL ulong 0 |
| 30 | const POL_READ_ONLY ulong 1 |
| 31 | const POL_STANDARD ulong 2 |
| 32 | const POL_ELEVATED ulong 3 |
| 33 | |
| 34 | text ip_hex_digits "0123456789abcdef" |
| 35 | |
| 36 | text ip_k_version "version" |
| 37 | text ip_k_id "envelope_id" |
| 38 | text ip_k_origin "origin_id" |
| 39 | text ip_k_action "action" |
| 40 | text ip_k_payload "payload_sha256" |
| 41 | text ip_k_transport "transport" |
| 42 | text ip_k_claimed "claimed_policy" |
| 43 | |
| 44 | text ip_t_untrusted "untrusted" |
| 45 | text ip_t_tls "tls" |
| 46 | text ip_t_mtls "mtls" |
| 47 | text ip_t_loopback "loopback" |
| 48 | |
| 49 | note --- Basic slice, byte, and I/O helpers --- |
| 50 | |
| 51 | fn ip_slice_eq a view uchar * b view uchar * to bool |
| 52 | na = len a |
| 53 | nb = len b |
| 54 | if ne na nb |
| 55 | ret false |
| 56 | end |
| 57 | if gt na ulong 65536 |
| 58 | ret false |
| 59 | end |
| 60 | i = ulong 0 |
| 61 | while lt i na bound 65536 |
| 62 | if ne get a i get b i |
| 63 | ret false |
| 64 | end |
| 65 | i = add i ulong 1 |
| 66 | end |
| 67 | ret true |
| 68 | end |
| 69 | |
| 70 | fn ip_write_all fd int buf view uchar * to bool |
| 71 | n = len buf |
| 72 | if eq n ulong 0 |
| 73 | ret true |
| 74 | end |
| 75 | if gt n ulong 1048576 |
| 76 | ret false |
| 77 | end |
| 78 | off = ulong 0 |
| 79 | while lt off n bound 1048576 |
| 80 | remn = sub n off |
| 81 | k = write fd buf off remn |
| 82 | if le k long 0 |
| 83 | ret false |
| 84 | end |
| 85 | off = add off ulong k |
| 86 | end |
| 87 | ret true |
| 88 | end |
| 89 | |
| 90 | fn ip_put_u32_le dst edit uchar * pos ulong v uint to ulong |
| 91 | if gt add pos ulong 4 len dst |
| 92 | ret pos |
| 93 | end |
| 94 | put dst pos uchar and v uint 255 |
| 95 | put dst add pos ulong 1 uchar and shr v uint 8 uint 255 |
| 96 | put dst add pos ulong 2 uchar and shr v uint 16 uint 255 |
| 97 | put dst add pos ulong 3 uchar and shr v uint 24 uint 255 |
| 98 | ret add pos ulong 4 |
| 99 | end |
| 100 | |
| 101 | fn ip_put_u64_le dst edit uchar * pos ulong v ulong to ulong |
| 102 | if gt add pos ulong 8 len dst |
| 103 | ret pos |
| 104 | end |
| 105 | for i ulong 8 |
| 106 | sh = mul i ulong 8 |
| 107 | put dst add pos i uchar and shr v sh ulong 255 |
| 108 | end |
| 109 | ret add pos ulong 8 |
| 110 | end |
| 111 | |
| 112 | fn ip_hex_nibble b uchar to int |
| 113 | if and ge b uchar 48 le b uchar 57 |
| 114 | ret sub int b int 48 |
| 115 | end |
| 116 | if and ge b uchar 97 le b uchar 102 |
| 117 | ret add sub int b int 97 int 10 |
| 118 | end |
| 119 | ret int -1 |
| 120 | end |
| 121 | |
| 122 | fn ip_digest_to_hex d view uchar 32 dst edit uchar 64 to void |
| 123 | for i ulong 32 |
| 124 | b = ulong get d i |
| 125 | put dst mul i ulong 2 get ip_hex_digits shr b ulong 4 |
| 126 | put dst add mul i ulong 2 ulong 1 get ip_hex_digits and b ulong 15 |
| 127 | end |
| 128 | end |
| 129 | |
| 130 | fn ip_digest_eq a view uchar 32 b view uchar 32 to bool |
| 131 | for i ulong 32 |
| 132 | if ne get a i get b i |
| 133 | ret false |
| 134 | end |
| 135 | end |
| 136 | ret true |
| 137 | end |
| 138 | |
| 139 | note --- Strict untrusted-input validators --- |
| 140 | |
| 141 | fn ip_is_lower_hex64 s view uchar * to bool |
| 142 | if ne len s ulong 64 |
| 143 | ret false |
| 144 | end |
| 145 | for i ulong 64 |
| 146 | if lt ip_hex_nibble get s i int 0 |
| 147 | ret false |
| 148 | end |
| 149 | end |
| 150 | ret true |
| 151 | end |
| 152 | |
| 153 | note Strict Interplane identifier: 1..64 bytes of [a-z0-9._-], no leading/trailing '.' or '-', no ".." |
| 154 | fn ip_valid_ident s view uchar * to ulong |
| 155 | n = len s |
| 156 | if eq n ulong 0 |
| 157 | ret IP_ERR_BAD_IDENT |
| 158 | end |
| 159 | if gt n ulong 64 |
| 160 | ret IP_ERR_BAD_IDENT |
| 161 | end |
| 162 | first = get s 0 |
| 163 | last = get s sub n ulong 1 |
| 164 | if or eq first uchar 46 eq first uchar 45 |
| 165 | ret IP_ERR_BAD_IDENT |
| 166 | end |
| 167 | if or eq last uchar 46 eq last uchar 45 |
| 168 | ret IP_ERR_BAD_IDENT |
| 169 | end |
| 170 | prev_dot = false |
| 171 | i = ulong 0 |
| 172 | while lt i n bound 64 |
| 173 | b = get s i |
| 174 | ok = false |
| 175 | if and ge b uchar 97 le b uchar 122 |
| 176 | ok = true |
| 177 | end |
| 178 | if and ge b uchar 48 le b uchar 57 |
| 179 | ok = true |
| 180 | end |
| 181 | if or eq b uchar 46 or eq b uchar 95 eq b uchar 45 |
| 182 | ok = true |
| 183 | end |
| 184 | if not ok |
| 185 | ret IP_ERR_BAD_IDENT |
| 186 | end |
| 187 | if eq b uchar 46 |
| 188 | if prev_dot |
| 189 | ret IP_ERR_BAD_IDENT |
| 190 | end |
| 191 | prev_dot = true |
| 192 | else |
| 193 | prev_dot = false |
| 194 | end |
| 195 | i = add i ulong 1 |
| 196 | end |
| 197 | ret IP_OK |
| 198 | end |
| 199 | |
| 200 | note Strict protocol version: "major.minor", digits only, no leading zeros (except "0"), major in 1..1 |
| 201 | fn ip_parse_ver_part s view uchar * to long |
| 202 | n = len s |
| 203 | if eq n ulong 0 |
| 204 | ret long -1 |
| 205 | end |
| 206 | if gt n ulong 5 |
| 207 | ret long -1 |
| 208 | end |
| 209 | if gt n ulong 1 |
| 210 | if eq get s 0 uchar 48 |
| 211 | ret long -1 |
| 212 | end |
| 213 | end |
| 214 | acc = ulong 0 |
| 215 | i = ulong 0 |
| 216 | while lt i n bound 5 |
| 217 | b = get s i |
| 218 | if or lt b uchar 48 gt b uchar 57 |
| 219 | ret long -1 |
| 220 | end |
| 221 | acc = add mul acc ulong 10 sub ulong b ulong 48 |
| 222 | i = add i ulong 1 |
| 223 | end |
| 224 | if gt acc ulong 65535 |
| 225 | ret long -1 |
| 226 | end |
| 227 | ret long acc |
| 228 | end |
| 229 | |
| 230 | fn ip_validate_version s view uchar * to ulong |
| 231 | n = len s |
| 232 | if lt n ulong 3 |
| 233 | ret IP_ERR_BAD_VERSION |
| 234 | end |
| 235 | if gt n ulong 11 |
| 236 | ret IP_ERR_BAD_VERSION |
| 237 | end |
| 238 | dot = ulong 999 |
| 239 | i = ulong 0 |
| 240 | while lt i n bound 11 |
| 241 | if eq get s i uchar 46 |
| 242 | if ne dot ulong 999 |
| 243 | ret IP_ERR_BAD_VERSION |
| 244 | end |
| 245 | dot = i |
| 246 | end |
| 247 | i = add i ulong 1 |
| 248 | end |
| 249 | if eq dot ulong 999 |
| 250 | ret IP_ERR_BAD_VERSION |
| 251 | end |
| 252 | maj = ip_parse_ver_part part s 0 dot |
| 253 | min = ip_parse_ver_part part s add dot ulong 1 sub sub n dot ulong 1 |
| 254 | if or lt maj long 0 lt min long 0 |
| 255 | ret IP_ERR_BAD_VERSION |
| 256 | end |
| 257 | if ne maj long 1 |
| 258 | ret IP_ERR_BAD_VERSION |
| 259 | end |
| 260 | ret IP_OK |
| 261 | end |
| 262 | |
| 263 | fn ip_parse_transport s view uchar * to long |
| 264 | if ip_slice_eq s ip_t_untrusted |
| 265 | ret long TRANS_UNTRUSTED |
| 266 | end |
| 267 | if ip_slice_eq s ip_t_tls |
| 268 | ret long TRANS_TLS |
| 269 | end |
| 270 | if ip_slice_eq s ip_t_mtls |
| 271 | ret long TRANS_MTLS |
| 272 | end |
| 273 | if ip_slice_eq s ip_t_loopback |
| 274 | ret long TRANS_LOOPBACK |
| 275 | end |
| 276 | ret long -1 |
| 277 | end |
| 278 | |
| 279 | fn ip_parse_policy_hint s view uchar * to long |
| 280 | if ne len s ulong 1 |
| 281 | ret long -1 |
| 282 | end |
| 283 | b = get s 0 |
| 284 | if or lt b uchar 48 gt b uchar 51 |
| 285 | ret long -1 |
| 286 | end |
| 287 | ret long sub ulong b ulong 48 |
| 288 | end |
| 289 | |
| 290 | note --- Canonical length-prefixed SHA-256 trust digest helper --- |
| 291 | |
| 292 | fn ip_hash_lp_field ctx edit ulong 40 fld view uchar * to void |
| 293 | lb = new uchar 4 0 |
| 294 | w = ip_put_u32_le lb ulong 0 uint len fld |
| 295 | if ne w ulong 4 |
| 296 | trap 8 |
| 297 | end |
| 298 | sha256_update ctx lb |
| 299 | sha256_update ctx fld |
| 300 | end |
| 301 | |
| 302 | note --- Untrusted Wire Envelope Parser --- |
| 303 | note Parses key=value lines separated by '\n'. Rejects control chars, NULs, duplicate keys, |
| 304 | note unknown keys, and missing required fields. |
| 305 | note meta[0] = seen_mask, meta[1] = transport_class, meta[2] = claimed_policy (hint only!), |
| 306 | note meta[3] = lifecycle_state (always set to 1=PARSED on IP_OK, NEVER authorized/executed). |
| 307 | note Also computes canonical trust digest over the 5 core fields into digest_out[32]. |
| 308 | |
| 309 | fn ip_check_printable_line line view uchar * to bool |
| 310 | n = len line |
| 311 | if gt n ulong 512 |
| 312 | ret false |
| 313 | end |
| 314 | i = ulong 0 |
| 315 | while lt i n bound 512 |
| 316 | b = get line i |
| 317 | if or lt b uchar 32 ge b uchar 127 |
| 318 | ret false |
| 319 | end |
| 320 | i = add i ulong 1 |
| 321 | end |
| 322 | ret true |
| 323 | end |
| 324 | |
| 325 | fn ip_find_eq line view uchar * to ulong |
| 326 | n = len line |
| 327 | if gt n ulong 512 |
| 328 | ret ulong 9999 |
| 329 | end |
| 330 | i = ulong 0 |
| 331 | while lt i n bound 512 |
| 332 | if eq get line i uchar 61 |
| 333 | ret i |
| 334 | end |
| 335 | i = add i ulong 1 |
| 336 | end |
| 337 | ret ulong 9999 |
| 338 | end |
| 339 | |
| 340 | note Field bitmask: 1=version, 2=envelope_id, 4=origin_id, 8=action, 16=payload_sha256, |
| 341 | note 32=transport, 64=claimed_policy. Required mask = 31 (bits 0..4). |
| 342 | fn ip_parse_line line view uchar * meta edit ulong 4 ctx edit ulong 40 to ulong |
| 343 | if not ip_check_printable_line line |
| 344 | ret IP_ERR_BAD_CHAR |
| 345 | end |
| 346 | eqp = ip_find_eq line |
| 347 | if eq eqp ulong 9999 |
| 348 | ret IP_ERR_BAD_LINE |
| 349 | end |
| 350 | if eq eqp ulong 0 |
| 351 | ret IP_ERR_BAD_LINE |
| 352 | end |
| 353 | klen = eqp |
| 354 | voff = add eqp ulong 1 |
| 355 | vlen = sub len line voff |
| 356 | seen = get meta 0 |
| 357 | if ip_slice_eq part line 0 klen ip_k_version |
| 358 | if ne and seen ulong 1 ulong 0 |
| 359 | ret IP_ERR_DUP_KEY |
| 360 | end |
| 361 | rc = ip_validate_version part line voff vlen |
| 362 | if ne rc IP_OK |
| 363 | ret rc |
| 364 | end |
| 365 | put meta 0 or seen ulong 1 |
| 366 | ip_hash_lp_field ctx part line voff vlen |
| 367 | ret IP_OK |
| 368 | end |
| 369 | if ip_slice_eq part line 0 klen ip_k_id |
| 370 | if ne and seen ulong 2 ulong 0 |
| 371 | ret IP_ERR_DUP_KEY |
| 372 | end |
| 373 | rc = ip_valid_ident part line voff vlen |
| 374 | if ne rc IP_OK |
| 375 | ret rc |
| 376 | end |
| 377 | put meta 0 or seen ulong 2 |
| 378 | ip_hash_lp_field ctx part line voff vlen |
| 379 | ret IP_OK |
| 380 | end |
| 381 | if ip_slice_eq part line 0 klen ip_k_origin |
| 382 | if ne and seen ulong 4 ulong 0 |
| 383 | ret IP_ERR_DUP_KEY |
| 384 | end |
| 385 | rc = ip_valid_ident part line voff vlen |
| 386 | if ne rc IP_OK |
| 387 | ret rc |
| 388 | end |
| 389 | put meta 0 or seen ulong 4 |
| 390 | ip_hash_lp_field ctx part line voff vlen |
| 391 | ret IP_OK |
| 392 | end |
| 393 | if ip_slice_eq part line 0 klen ip_k_action |
| 394 | if ne and seen ulong 8 ulong 0 |
| 395 | ret IP_ERR_DUP_KEY |
| 396 | end |
| 397 | rc = ip_valid_ident part line voff vlen |
| 398 | if ne rc IP_OK |
| 399 | ret rc |
| 400 | end |
| 401 | put meta 0 or seen ulong 8 |
| 402 | ip_hash_lp_field ctx part line voff vlen |
| 403 | ret IP_OK |
| 404 | end |
| 405 | if ip_slice_eq part line 0 klen ip_k_payload |
| 406 | if ne and seen ulong 16 ulong 0 |
| 407 | ret IP_ERR_DUP_KEY |
| 408 | end |
| 409 | if not ip_is_lower_hex64 part line voff vlen |
| 410 | ret IP_ERR_BAD_HEX |
| 411 | end |
| 412 | put meta 0 or seen ulong 16 |
| 413 | ip_hash_lp_field ctx part line voff vlen |
| 414 | ret IP_OK |
| 415 | end |
| 416 | if ip_slice_eq part line 0 klen ip_k_transport |
| 417 | if ne and seen ulong 32 ulong 0 |
| 418 | ret IP_ERR_DUP_KEY |
| 419 | end |
| 420 | tr = ip_parse_transport part line voff vlen |
| 421 | if lt tr long 0 |
| 422 | ret IP_ERR_BAD_TRANSPORT |
| 423 | end |
| 424 | put meta 0 or seen ulong 32 |
| 425 | put meta 1 ulong tr |
| 426 | ret IP_OK |
| 427 | end |
| 428 | if ip_slice_eq part line 0 klen ip_k_claimed |
| 429 | if ne and seen ulong 64 ulong 0 |
| 430 | ret IP_ERR_DUP_KEY |
| 431 | end |
| 432 | ph = ip_parse_policy_hint part line voff vlen |
| 433 | if lt ph long 0 |
| 434 | ret IP_ERR_BAD_POLICY_HINT |
| 435 | end |
| 436 | put meta 0 or seen ulong 64 |
| 437 | put meta 2 ulong ph |
| 438 | ret IP_OK |
| 439 | end |
| 440 | ret IP_ERR_UNKNOWN_KEY |
| 441 | end |
| 442 | |
| 443 | fn ip_parse_envelope raw view uchar * meta edit ulong 4 digest_out edit uchar 32 to ulong |
| 444 | put meta 0 ulong 0 |
| 445 | put meta 1 TRANS_UNTRUSTED |
| 446 | put meta 2 POL_DENY_ALL |
| 447 | put meta 3 ulong 0 |
| 448 | n = len raw |
| 449 | if eq n ulong 0 |
| 450 | ret IP_ERR_BAD_LEN |
| 451 | end |
| 452 | if gt n IP_MAX_ENV_BYTES |
| 453 | ret IP_ERR_BAD_LEN |
| 454 | end |
| 455 | ctx = new ulong 40 0 |
| 456 | sha256_init ctx |
| 457 | lines = ulong 0 |
| 458 | start = ulong 0 |
| 459 | i = ulong 0 |
| 460 | while le i n bound 4097 |
| 461 | at_nl = false |
| 462 | if eq i n |
| 463 | at_nl = true |
| 464 | else |
| 465 | if eq get raw i uchar 10 |
| 466 | at_nl = true |
| 467 | end |
| 468 | end |
| 469 | if at_nl |
| 470 | llen = sub i start |
| 471 | if gt llen ulong 0 |
| 472 | lines = add lines ulong 1 |
| 473 | if gt lines IP_MAX_ENV_LINES |
| 474 | ret IP_ERR_TOO_MANY_LINES |
| 475 | end |
| 476 | rc = ip_parse_line part raw start llen meta ctx |
| 477 | if ne rc IP_OK |
| 478 | ret rc |
| 479 | end |
| 480 | end |
| 481 | start = add i ulong 1 |
| 482 | end |
| 483 | i = add i ulong 1 |
| 484 | end |
| 485 | req_mask = ulong 31 |
| 486 | if ne and get meta 0 req_mask req_mask |
| 487 | ret IP_ERR_MISSING_FIELD |
| 488 | end |
| 489 | sha256_final ctx digest_out |
| 490 | note Parsing NEVER authorizes or executes: lifecycle state is strictly 1 (LC_PARSED). |
| 491 | put meta 3 ulong 1 |
| 492 | ret IP_OK |
| 493 | end |