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 / open-humanity / lib.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/open-humanity/lib.om: pure-Omega opt-in, privacy-first peer assistance protocol. |
| 3 | note Derived from open-humanity (beacon-core wire/schema/firewall + beacon relay queue/audit). |
| 4 | note Enforces explicit opt-in consent, strict zero-authority boundary (never grants local exec, |
| 5 | note secret access, identity mutation, or admin rights), deterministic Personal Data Firewall |
| 6 | note with obfuscation decoding and path scrubbing, TTL/replay guards, bounded peer assistance |
| 7 | note queue, and append-only resolution audit trail. Library unit: no om2 directive, no fn main. |
| 8 | |
| 9 | const OH_MAGIC_0 ulong 0x4f |
| 10 | const OH_MAGIC_1 ulong 0x48 |
| 11 | const OH_VERSION ulong 1 |
| 12 | const OH_HDR_LEN ulong 48 |
| 13 | const OH_MAX_PAYLOAD ulong 512 |
| 14 | const OH_MAX_WIRE ulong 560 |
| 15 | |
| 16 | const OH_CAT_RUST ulong 0 |
| 17 | const OH_CAT_SIMD ulong 1 |
| 18 | const OH_CAT_SERVING ulong 2 |
| 19 | const OH_CAT_HW ulong 3 |
| 20 | const OH_CAT_SEC ulong 4 |
| 21 | const OH_CAT_PROTO ulong 5 |
| 22 | const OH_CAT_AGENT ulong 6 |
| 23 | const OH_CAT_MAX ulong 6 |
| 24 | |
| 25 | const OH_MAX_TTL_MS ulong 604800000 |
| 26 | const OH_MAX_FUTURE_DRIFT_MS ulong 300000 |
| 27 | |
| 28 | const OH_CAP_NONE ulong 0 |
| 29 | const OH_CAP_LOCAL_EXEC ulong 1 |
| 30 | const OH_CAP_SECRET_ACCESS ulong 2 |
| 31 | const OH_CAP_IDENTITY_MUTATE ulong 4 |
| 32 | const OH_CAP_ADMIN_RIGHTS ulong 8 |
| 33 | |
| 34 | const OH_OK ulong 0 |
| 35 | const OH_ERR_BAD_MAGIC ulong 1 |
| 36 | const OH_ERR_BAD_VERSION ulong 2 |
| 37 | const OH_ERR_TRUNCATED ulong 3 |
| 38 | const OH_ERR_BAD_DIGEST ulong 4 |
| 39 | const OH_ERR_BAD_PEER ulong 5 |
| 40 | const OH_ERR_BAD_CATEGORY ulong 6 |
| 41 | const OH_ERR_NODE_OPT_OUT ulong 10 |
| 42 | const OH_ERR_NO_CONSENT ulong 11 |
| 43 | const OH_ERR_TTL_INVALID ulong 20 |
| 44 | const OH_ERR_TTL_EXPIRED ulong 21 |
| 45 | const OH_ERR_FUTURE_DRIFT ulong 22 |
| 46 | const OH_ERR_AUTH_EXEC ulong 30 |
| 47 | const OH_ERR_AUTH_SECRET ulong 31 |
| 48 | const OH_ERR_AUTH_IDENTITY ulong 32 |
| 49 | const OH_ERR_AUTH_ADMIN ulong 33 |
| 50 | const OH_ERR_AUTH_UNKNOWN ulong 34 |
| 51 | const OH_ERR_AUTH_PAYLOAD_ESC ulong 35 |
| 52 | const OH_ERR_PRIV_AWS_KEY ulong 40 |
| 53 | const OH_ERR_PRIV_GITHUB_TOKEN ulong 41 |
| 54 | const OH_ERR_PRIV_ANTHROPIC_KEY ulong 42 |
| 55 | const OH_ERR_PRIV_OPENAI_KEY ulong 43 |
| 56 | const OH_ERR_PRIV_GOOGLE_KEY ulong 44 |
| 57 | const OH_ERR_PRIV_STRIPE_KEY ulong 45 |
| 58 | const OH_ERR_PRIV_HF_TOKEN ulong 46 |
| 59 | const OH_ERR_PRIV_PEM_KEY ulong 47 |
| 60 | const OH_ERR_PRIV_GENERIC_SECRET ulong 48 |
| 61 | const OH_ERR_PRIV_USER_PATH ulong 49 |
| 62 | const OH_ERR_PRIV_TRAVERSAL ulong 50 |
| 63 | const OH_ERR_REPLAY ulong 60 |
| 64 | const OH_ERR_QUEUE_FULL ulong 61 |
| 65 | const OH_ERR_REQ_NOT_FOUND ulong 62 |
| 66 | const OH_ERR_REQ_ALREADY_RESOLVED ulong 63 |
| 67 | const OH_ERR_SELF_RESOLVE ulong 64 |
| 68 | const OH_ERR_EMPTY_PAYLOAD ulong 65 |
| 69 | |
| 70 | const OH_QUEUE_CAP ulong 32 |
| 71 | const OH_AUDIT_CAP ulong 64 |
| 72 | const OH_SEEN_CAP ulong 64 |
| 73 | |
| 74 | const OH_Q_EMPTY ulong 0 |
| 75 | const OH_Q_PENDING ulong 1 |
| 76 | const OH_Q_RESOLVED ulong 2 |
| 77 | const OH_Q_EXPIRED ulong 3 |
| 78 | |
| 79 | const OH_AUD_ENQUEUED ulong 1 |
| 80 | const OH_AUD_RESOLVED ulong 2 |
| 81 | const OH_AUD_REFUSED ulong 3 |
| 82 | const OH_AUD_EXPIRED ulong 4 |
| 83 | |
| 84 | struct OhEnv version ulong peer_id ulong consent ulong category ulong created_ms ulong ttl_ms ulong cap_flags ulong digest ulong payload_len ulong end |
| 85 | struct OhPolicy opt_in ulong now_ms ulong next_req_id ulong audit_seq ulong seen_head ulong end |
| 86 | |
| 87 | global oh_q_status ulong 32 |
| 88 | global oh_q_req_id ulong 32 |
| 89 | global oh_q_peer_id ulong 32 |
| 90 | global oh_q_cat ulong 32 |
| 91 | global oh_q_created ulong 32 |
| 92 | global oh_q_ttl ulong 32 |
| 93 | global oh_q_digest ulong 32 |
| 94 | global oh_q_helper ulong 32 |
| 95 | global oh_q_plen ulong 32 |
| 96 | global oh_q_payload uchar 16384 |
| 97 | |
| 98 | global oh_seen_peer ulong 64 |
| 99 | global oh_seen_dig ulong 64 |
| 100 | |
| 101 | global oh_aud_seq ulong 64 |
| 102 | global oh_aud_kind ulong 64 |
| 103 | global oh_aud_peer ulong 64 |
| 104 | global oh_aud_req ulong 64 |
| 105 | global oh_aud_cat ulong 64 |
| 106 | global oh_aud_code ulong 64 |
| 107 | global oh_aud_ts ulong 64 |
| 108 | global oh_aud_dig ulong 64 |
| 109 | global oh_aud_cnt ulong 1 |
| 110 | |
| 111 | text oh_hex_chars "0123456789abcdef" |
| 112 | text oh_t_pem_beg "-----BEGIN " |
| 113 | text oh_t_pem_end "PRIVATE KEY-----" |
| 114 | text oh_t_akia "AKIA" |
| 115 | text oh_t_sk_ant "sk-ant-" |
| 116 | text oh_t_sk "sk-" |
| 117 | text oh_t_aiza "AIza" |
| 118 | text oh_t_hf "hf_" |
| 119 | text oh_t_live "live_" |
| 120 | text oh_t_test "test_" |
| 121 | text oh_t_home "/home/" |
| 122 | text oh_t_users "/Users/" |
| 123 | text oh_t_wusers ":\\Users\\" |
| 124 | text oh_t_wusers2 ":/Users/" |
| 125 | text oh_t_ak_api "api_key" |
| 126 | text oh_t_ak_apikey "apikey" |
| 127 | text oh_t_ak_sec "secret_key" |
| 128 | text oh_t_ak_priv "private_key" |
| 129 | text oh_t_ak_auth "auth_token" |
| 130 | text oh_t_ak_bear "bearer" |
| 131 | text oh_t_esc_exec "exec:" |
| 132 | text oh_t_esc_sudo "sudo " |
| 133 | text oh_t_esc_chmod "chmod " |
| 134 | text oh_t_esc_chown "chown " |
| 135 | text oh_t_esc_ident "open_humanity_identity" |
| 136 | text oh_t_esc_seed "identity_seed=" |
| 137 | text oh_t_esc_admin "grant_admin" |
| 138 | |
| 139 | text oh_r_ok "ok" |
| 140 | text oh_r_magic "bad_magic" |
| 141 | text oh_r_ver "bad_version" |
| 142 | text oh_r_trunc "truncated" |
| 143 | text oh_r_dig "bad_digest" |
| 144 | text oh_r_peer "bad_peer_id" |
| 145 | text oh_r_cat "bad_category" |
| 146 | text oh_r_optout "node_opt_out" |
| 147 | text oh_r_noconsent "missing_consent" |
| 148 | text oh_r_ttlinv "ttl_invalid" |
| 149 | text oh_r_ttlexp "ttl_expired" |
| 150 | text oh_r_drift "future_timestamp_drift" |
| 151 | text oh_r_aexec "authority_refused_local_exec" |
| 152 | text oh_r_asec "authority_refused_secret_access" |
| 153 | text oh_r_aid "authority_refused_identity_mutate" |
| 154 | text oh_r_aadm "authority_refused_admin_rights" |
| 155 | text oh_r_aunk "authority_refused_unknown_cap" |
| 156 | text oh_r_aesc "authority_refused_payload_escalation" |
| 157 | text oh_r_paws "privacy_refused_aws_key" |
| 158 | text oh_r_pgh "privacy_refused_github_token" |
| 159 | text oh_r_pant "privacy_refused_anthropic_key" |
| 160 | text oh_r_poai "privacy_refused_openai_key" |
| 161 | text oh_r_pgoo "privacy_refused_google_key" |
| 162 | text oh_r_pstr "privacy_refused_stripe_key" |
| 163 | text oh_r_phf "privacy_refused_hf_token" |
| 164 | text oh_r_ppem "privacy_refused_private_key_block" |
| 165 | text oh_r_pgen "privacy_refused_generic_secret" |
| 166 | text oh_r_ppath "privacy_refused_private_user_path" |
| 167 | text oh_r_ptrav "privacy_refused_path_traversal" |
| 168 | text oh_r_rep "replay_detected" |
| 169 | text oh_r_qfull "queue_full" |
| 170 | text oh_r_nf "request_not_found" |
| 171 | text oh_r_alr "request_already_resolved" |
| 172 | text oh_r_self "self_resolution_refused" |
| 173 | text oh_r_empty "empty_payload" |
| 174 | text oh_r_unk "unknown_error" |
| 175 | |
| 176 | fn oh_write_all fd int buf view uchar * to bool |
| 177 | n = len buf |
| 178 | pos = ulong 0 |
| 179 | while lt pos n bound 65536 |
| 180 | k = write fd buf pos sub n pos |
| 181 | if le k 0 |
| 182 | ret false |
| 183 | end |
| 184 | pos = add pos ulong k |
| 185 | end |
| 186 | ret true |
| 187 | end |
| 188 | |
| 189 | fn oh_write_u64 fd int v ulong to void |
| 190 | digits = new uchar 24 0 |
| 191 | if eq v 0 |
| 192 | put digits 0 uchar 48 |
| 193 | ok0 = oh_write_all fd part digits 0 1 |
| 194 | if ok0 |
| 195 | ret |
| 196 | end |
| 197 | ret |
| 198 | end |
| 199 | tmp = new uchar 24 0 |
| 200 | cnt = ulong 0 |
| 201 | x = v |
| 202 | while gt x 0 bound 24 |
| 203 | d = rem x 10 |
| 204 | put tmp cnt uchar add 48 d |
| 205 | cnt = add cnt 1 |
| 206 | x = div x 10 |
| 207 | end |
| 208 | i = ulong 0 |
| 209 | while lt i cnt bound 24 |
| 210 | put digits i get tmp sub sub cnt 1 i |
| 211 | i = add i 1 |
| 212 | end |
| 213 | ok1 = oh_write_all fd part digits 0 cnt |
| 214 | if ok1 |
| 215 | ret |
| 216 | end |
| 217 | end |
| 218 | |
| 219 | fn oh_write_hex64 fd int v ulong to void |
| 220 | buf = new uchar 16 48 |
| 221 | i = ulong 0 |
| 222 | while lt i 16 bound 16 |
| 223 | sh = mul sub 15 i 4 |
| 224 | nib = and shr v sh 0xf |
| 225 | put buf i get oh_hex_chars nib |
| 226 | i = add i 1 |
| 227 | end |
| 228 | ok = oh_write_all fd buf |
| 229 | if ok |
| 230 | ret |
| 231 | end |
| 232 | end |
| 233 | |
| 234 | fn oh_write_reason fd int code ulong to void |
| 235 | ok = false |
| 236 | if eq code OH_OK |
| 237 | ok = oh_write_all fd oh_r_ok |
| 238 | else |
| 239 | if eq code OH_ERR_BAD_MAGIC |
| 240 | ok = oh_write_all fd oh_r_magic |
| 241 | else |
| 242 | if eq code OH_ERR_BAD_VERSION |
| 243 | ok = oh_write_all fd oh_r_ver |
| 244 | else |
| 245 | if eq code OH_ERR_TRUNCATED |
| 246 | ok = oh_write_all fd oh_r_trunc |
| 247 | else |
| 248 | if eq code OH_ERR_BAD_DIGEST |
| 249 | ok = oh_write_all fd oh_r_dig |
| 250 | else |
| 251 | if eq code OH_ERR_BAD_PEER |
| 252 | ok = oh_write_all fd oh_r_peer |
| 253 | else |
| 254 | if eq code OH_ERR_BAD_CATEGORY |
| 255 | ok = oh_write_all fd oh_r_cat |
| 256 | else |
| 257 | if eq code OH_ERR_NODE_OPT_OUT |
| 258 | ok = oh_write_all fd oh_r_optout |
| 259 | else |
| 260 | if eq code OH_ERR_NO_CONSENT |
| 261 | ok = oh_write_all fd oh_r_noconsent |
| 262 | else |
| 263 | if eq code OH_ERR_TTL_INVALID |
| 264 | ok = oh_write_all fd oh_r_ttlinv |
| 265 | else |
| 266 | if eq code OH_ERR_TTL_EXPIRED |
| 267 | ok = oh_write_all fd oh_r_ttlexp |
| 268 | else |
| 269 | if eq code OH_ERR_FUTURE_DRIFT |
| 270 | ok = oh_write_all fd oh_r_drift |
| 271 | else |
| 272 | ok = oh_write_reason_hi fd code |
| 273 | end |
| 274 | end |
| 275 | end |
| 276 | end |
| 277 | end |
| 278 | end |
| 279 | end |
| 280 | end |
| 281 | end |
| 282 | end |
| 283 | end |
| 284 | end |
| 285 | if ok |
| 286 | ret |
| 287 | end |
| 288 | end |
| 289 | |
| 290 | fn oh_write_reason_hi fd int code ulong to bool |
| 291 | if eq code OH_ERR_AUTH_EXEC |
| 292 | ret oh_write_all fd oh_r_aexec |
| 293 | end |
| 294 | if eq code OH_ERR_AUTH_SECRET |
| 295 | ret oh_write_all fd oh_r_asec |
| 296 | end |
| 297 | if eq code OH_ERR_AUTH_IDENTITY |
| 298 | ret oh_write_all fd oh_r_aid |
| 299 | end |
| 300 | if eq code OH_ERR_AUTH_ADMIN |
| 301 | ret oh_write_all fd oh_r_aadm |
| 302 | end |
| 303 | if eq code OH_ERR_AUTH_UNKNOWN |
| 304 | ret oh_write_all fd oh_r_aunk |
| 305 | end |
| 306 | if eq code OH_ERR_AUTH_PAYLOAD_ESC |
| 307 | ret oh_write_all fd oh_r_aesc |
| 308 | end |
| 309 | if eq code OH_ERR_PRIV_AWS_KEY |
| 310 | ret oh_write_all fd oh_r_paws |
| 311 | end |
| 312 | if eq code OH_ERR_PRIV_GITHUB_TOKEN |
| 313 | ret oh_write_all fd oh_r_pgh |
| 314 | end |
| 315 | if eq code OH_ERR_PRIV_ANTHROPIC_KEY |
| 316 | ret oh_write_all fd oh_r_pant |
| 317 | end |
| 318 | if eq code OH_ERR_PRIV_OPENAI_KEY |
| 319 | ret oh_write_all fd oh_r_poai |
| 320 | end |
| 321 | if eq code OH_ERR_PRIV_GOOGLE_KEY |
| 322 | ret oh_write_all fd oh_r_pgoo |
| 323 | end |
| 324 | if eq code OH_ERR_PRIV_STRIPE_KEY |
| 325 | ret oh_write_all fd oh_r_pstr |
| 326 | end |
| 327 | if eq code OH_ERR_PRIV_HF_TOKEN |
| 328 | ret oh_write_all fd oh_r_phf |
| 329 | end |
| 330 | if eq code OH_ERR_PRIV_PEM_KEY |
| 331 | ret oh_write_all fd oh_r_ppem |
| 332 | end |
| 333 | if eq code OH_ERR_PRIV_GENERIC_SECRET |
| 334 | ret oh_write_all fd oh_r_pgen |
| 335 | end |
| 336 | if eq code OH_ERR_PRIV_USER_PATH |
| 337 | ret oh_write_all fd oh_r_ppath |
| 338 | end |
| 339 | if eq code OH_ERR_PRIV_TRAVERSAL |
| 340 | ret oh_write_all fd oh_r_ptrav |
| 341 | end |
| 342 | if eq code OH_ERR_REPLAY |
| 343 | ret oh_write_all fd oh_r_rep |
| 344 | end |
| 345 | if eq code OH_ERR_QUEUE_FULL |
| 346 | ret oh_write_all fd oh_r_qfull |
| 347 | end |
| 348 | if eq code OH_ERR_REQ_NOT_FOUND |
| 349 | ret oh_write_all fd oh_r_nf |
| 350 | end |
| 351 | if eq code OH_ERR_REQ_ALREADY_RESOLVED |
| 352 | ret oh_write_all fd oh_r_alr |
| 353 | end |
| 354 | if eq code OH_ERR_SELF_RESOLVE |
| 355 | ret oh_write_all fd oh_r_self |
| 356 | end |
| 357 | if eq code OH_ERR_EMPTY_PAYLOAD |
| 358 | ret oh_write_all fd oh_r_empty |
| 359 | end |
| 360 | ret oh_write_all fd oh_r_unk |
| 361 | end |
| 362 | |
| 363 | fn oh_reset_state pol edit OhPolicy to void |
| 364 | put pol opt_in ulong 0 |
| 365 | put pol now_ms ulong 0 |
| 366 | put pol next_req_id ulong 1 |
| 367 | put pol audit_seq ulong 0 |
| 368 | put pol seen_head ulong 0 |
| 369 | i = ulong 0 |
| 370 | while lt i OH_QUEUE_CAP bound 64 |
| 371 | put oh_q_status i OH_Q_EMPTY |
| 372 | put oh_q_req_id i ulong 0 |
| 373 | put oh_q_peer_id i ulong 0 |
| 374 | put oh_q_cat i ulong 0 |
| 375 | put oh_q_created i ulong 0 |
| 376 | put oh_q_ttl i ulong 0 |
| 377 | put oh_q_digest i ulong 0 |
| 378 | put oh_q_helper i ulong 0 |
| 379 | put oh_q_plen i ulong 0 |
| 380 | i = add i 1 |
| 381 | end |
| 382 | j = ulong 0 |
| 383 | while lt j OH_SEEN_CAP bound 64 |
| 384 | put oh_seen_peer j ulong 0 |
| 385 | put oh_seen_dig j ulong 0 |
| 386 | j = add j 1 |
| 387 | end |
| 388 | k = ulong 0 |
| 389 | while lt k OH_AUDIT_CAP bound 64 |
| 390 | put oh_aud_seq k ulong 0 |
| 391 | put oh_aud_kind k ulong 0 |
| 392 | put oh_aud_peer k ulong 0 |
| 393 | put oh_aud_req k ulong 0 |
| 394 | put oh_aud_cat k ulong 0 |
| 395 | put oh_aud_code k ulong 0 |
| 396 | put oh_aud_ts k ulong 0 |
| 397 | put oh_aud_dig k ulong 0 |
| 398 | k = add k 1 |
| 399 | end |
| 400 | put oh_aud_cnt 0 ulong 0 |
| 401 | end |
| 402 | |
| 403 | fn oh_fnv1a_byte h ulong b uchar to ulong |
| 404 | x = xor h ulong b |
| 405 | ret mul x ulong 0x100000001b3 |
| 406 | end |
| 407 | |
| 408 | fn oh_fnv1a_u64 h ulong v ulong to ulong |
| 409 | acc = h |
| 410 | i = ulong 0 |
| 411 | while lt i 8 bound 8 |
| 412 | b = uchar and shr v mul i 8 0xff |
| 413 | acc = oh_fnv1a_byte acc b |
| 414 | i = add i 1 |
| 415 | end |
| 416 | ret acc |
| 417 | end |
| 418 | |
| 419 | fn oh_compute_digest env view OhEnv payload view uchar * to ulong |
| 420 | h = ulong 0xcbf29ce484222325 |
| 421 | h = oh_fnv1a_byte h uchar OH_MAGIC_0 |
| 422 | h = oh_fnv1a_byte h uchar OH_MAGIC_1 |
| 423 | h = oh_fnv1a_byte h uchar and get env version 0xff |
| 424 | h = oh_fnv1a_byte h uchar and get env consent 0xff |
| 425 | h = oh_fnv1a_byte h uchar and get env category 0xff |
| 426 | h = oh_fnv1a_byte h uchar 0 |
| 427 | h = oh_fnv1a_u64 h get env peer_id |
| 428 | h = oh_fnv1a_u64 h get env created_ms |
| 429 | h = oh_fnv1a_u64 h get env ttl_ms |
| 430 | h = oh_fnv1a_u64 h get env cap_flags |
| 431 | n = len payload |
| 432 | h = oh_fnv1a_u64 h n |
| 433 | i = ulong 0 |
| 434 | while lt i n bound 1024 |
| 435 | h = oh_fnv1a_byte h get payload i |
| 436 | i = add i 1 |
| 437 | end |
| 438 | ret h |
| 439 | end |
| 440 | |
| 441 | fn oh_put_u64_le dst edit uchar * off ulong v ulong to void |
| 442 | i = ulong 0 |
| 443 | while lt i 8 bound 8 |
| 444 | put dst add off i uchar and shr v mul i 8 0xff |
| 445 | i = add i 1 |
| 446 | end |
| 447 | end |
| 448 | |
| 449 | fn oh_get_u64_le src view uchar * off ulong to ulong |
| 450 | acc = ulong 0 |
| 451 | i = ulong 0 |
| 452 | while lt i 8 bound 8 |
| 453 | b = ulong get src add off i |
| 454 | acc = or acc shl b mul i 8 |
| 455 | i = add i 1 |
| 456 | end |
| 457 | ret acc |
| 458 | end |
| 459 | |
| 460 | fn oh_serialize_envelope env view OhEnv payload view uchar * dst edit uchar * to ulong |
| 461 | plen = len payload |
| 462 | if gt plen OH_MAX_PAYLOAD |
| 463 | ret ulong 0 |
| 464 | end |
| 465 | total = add OH_HDR_LEN plen |
| 466 | if gt total len dst |
| 467 | ret ulong 0 |
| 468 | end |
| 469 | dig = oh_compute_digest env payload |
| 470 | put dst 0 uchar OH_MAGIC_0 |
| 471 | put dst 1 uchar OH_MAGIC_1 |
| 472 | put dst 2 uchar and get env version 0xff |
| 473 | put dst 3 uchar and get env consent 0xff |
| 474 | put dst 4 uchar and get env category 0xff |
| 475 | put dst 5 uchar 0 |
| 476 | oh_put_u64_le dst 6 get env peer_id |
| 477 | oh_put_u64_le dst 14 get env created_ms |
| 478 | oh_put_u64_le dst 22 get env ttl_ms |
| 479 | oh_put_u64_le dst 30 get env cap_flags |
| 480 | oh_put_u64_le dst 38 dig |
| 481 | put dst 46 uchar and plen 0xff |
| 482 | put dst 47 uchar and shr plen 8 0xff |
| 483 | i = ulong 0 |
| 484 | while lt i plen bound 512 |
| 485 | put dst add OH_HDR_LEN i get payload i |
| 486 | i = add i 1 |
| 487 | end |
| 488 | ret total |
| 489 | end |
| 490 | |
| 491 | fn oh_parse_envelope wire view uchar * env edit OhEnv dst_payload edit uchar * to ulong |
| 492 | n = len wire |
| 493 | if lt n OH_HDR_LEN |
| 494 | ret OH_ERR_TRUNCATED |
| 495 | end |
| 496 | if or ne ulong get wire 0 OH_MAGIC_0 ne ulong get wire 1 OH_MAGIC_1 |
| 497 | ret OH_ERR_BAD_MAGIC |
| 498 | end |
| 499 | ver = ulong get wire 2 |
| 500 | if ne ver OH_VERSION |
| 501 | ret OH_ERR_BAD_VERSION |
| 502 | end |
| 503 | plen = or ulong get wire 46 shl ulong get wire 47 8 |
| 504 | if gt plen OH_MAX_PAYLOAD |
| 505 | ret OH_ERR_TRUNCATED |
| 506 | end |
| 507 | if ne add OH_HDR_LEN plen n |
| 508 | ret OH_ERR_TRUNCATED |
| 509 | end |
| 510 | if gt plen len dst_payload |
| 511 | ret OH_ERR_TRUNCATED |
| 512 | end |
| 513 | put env version ver |
| 514 | put env consent ulong get wire 3 |
| 515 | put env category ulong get wire 4 |
| 516 | put env peer_id oh_get_u64_le wire 6 |
| 517 | put env created_ms oh_get_u64_le wire 14 |
| 518 | put env ttl_ms oh_get_u64_le wire 22 |
| 519 | put env cap_flags oh_get_u64_le wire 30 |
| 520 | wire_dig = oh_get_u64_le wire 38 |
| 521 | put env digest wire_dig |
| 522 | put env payload_len plen |
| 523 | i = ulong 0 |
| 524 | while lt i plen bound 512 |
| 525 | put dst_payload i get wire add OH_HDR_LEN i |
| 526 | i = add i 1 |
| 527 | end |
| 528 | want_dig = oh_compute_digest env part dst_payload 0 plen |
| 529 | if ne wire_dig want_dig |
| 530 | ret OH_ERR_BAD_DIGEST |
| 531 | end |
| 532 | ret OH_OK |
| 533 | end |
| 534 | |
| 535 | note --- Character & token classification helpers for Personal Data Firewall --- |
| 536 | |
| 537 | fn oh_is_digit c uchar to bool |
| 538 | ret and ge c 48 le c 57 |
| 539 | end |
| 540 | |
| 541 | fn oh_is_upper c uchar to bool |
| 542 | ret and ge c 65 le c 90 |
| 543 | end |
| 544 | |
| 545 | fn oh_is_lower c uchar to bool |
| 546 | ret and ge c 97 le c 122 |
| 547 | end |
| 548 | |
| 549 | fn oh_is_alpha c uchar to bool |
| 550 | ret or oh_is_upper c oh_is_lower c |
| 551 | end |
| 552 | |
| 553 | fn oh_is_alnum c uchar to bool |
| 554 | ret or oh_is_alpha c oh_is_digit c |
| 555 | end |
| 556 | |
| 557 | fn oh_is_tok_char c uchar to bool |
| 558 | ret or oh_is_alnum c or eq c 95 eq c 45 |
| 559 | end |
| 560 | |
| 561 | fn oh_is_path_char c uchar to bool |
| 562 | ret or oh_is_alnum c or eq c 46 or eq c 95 eq c 45 |
| 563 | end |
| 564 | |
| 565 | fn oh_to_lower c uchar to uchar |
| 566 | if oh_is_upper c |
| 567 | ret add c 32 |
| 568 | end |
| 569 | ret c |
| 570 | end |
| 571 | |
| 572 | fn oh_hex_val c uchar to ulong |
| 573 | if and ge c 48 le c 57 |
| 574 | ret ulong sub c 48 |
| 575 | end |
| 576 | if and ge c 65 le c 70 |
| 577 | ret add ulong sub c 65 10 |
| 578 | end |
| 579 | if and ge c 97 le c 102 |
| 580 | ret add ulong sub c 97 10 |
| 581 | end |
| 582 | ret ulong 255 |
| 583 | end |
| 584 | |
| 585 | fn oh_match_at buf view uchar * pos ulong pat view uchar * to bool |
| 586 | pn = len pat |
| 587 | if gt add pos pn len buf |
| 588 | ret false |
| 589 | end |
| 590 | i = ulong 0 |
| 591 | while lt i pn bound 64 |
| 592 | if ne get buf add pos i get pat i |
| 593 | ret false |
| 594 | end |
| 595 | i = add i 1 |
| 596 | end |
| 597 | ret true |
| 598 | end |
| 599 | |
| 600 | fn oh_match_ci_at buf view uchar * pos ulong pat view uchar * to bool |
| 601 | pn = len pat |
| 602 | if gt add pos pn len buf |
| 603 | ret false |
| 604 | end |
| 605 | i = ulong 0 |
| 606 | while lt i pn bound 64 |
| 607 | if ne oh_to_lower get buf add pos i oh_to_lower get pat i |
| 608 | ret false |
| 609 | end |
| 610 | i = add i 1 |
| 611 | end |
| 612 | ret true |
| 613 | end |
| 614 | |
| 615 | fn oh_count_upper_alnum buf view uchar * pos ulong to ulong |
| 616 | n = len buf |
| 617 | i = pos |
| 618 | cnt = ulong 0 |
| 619 | while lt i n bound 512 |
| 620 | c = get buf i |
| 621 | if or oh_is_digit c oh_is_upper c |
| 622 | cnt = add cnt 1 |
| 623 | i = add i 1 |
| 624 | else |
| 625 | break |
| 626 | end |
| 627 | end |
| 628 | ret cnt |
| 629 | end |
| 630 | |
| 631 | fn oh_count_alnum_u buf view uchar * pos ulong to ulong |
| 632 | n = len buf |
| 633 | i = pos |
| 634 | cnt = ulong 0 |
| 635 | while lt i n bound 512 |
| 636 | c = get buf i |
| 637 | if or oh_is_alnum c eq c 95 |
| 638 | cnt = add cnt 1 |
| 639 | i = add i 1 |
| 640 | else |
| 641 | break |
| 642 | end |
| 643 | end |
| 644 | ret cnt |
| 645 | end |
| 646 | |
| 647 | fn oh_count_tok buf view uchar * pos ulong to ulong |
| 648 | n = len buf |
| 649 | i = pos |
| 650 | cnt = ulong 0 |
| 651 | while lt i n bound 512 |
| 652 | if oh_is_tok_char get buf i |
| 653 | cnt = add cnt 1 |
| 654 | i = add i 1 |
| 655 | else |
| 656 | break |
| 657 | end |
| 658 | end |
| 659 | ret cnt |
| 660 | end |
| 661 | |
| 662 | fn oh_count_alnum buf view uchar * pos ulong to ulong |
| 663 | n = len buf |
| 664 | i = pos |
| 665 | cnt = ulong 0 |
| 666 | while lt i n bound 512 |
| 667 | if oh_is_alnum get buf i |
| 668 | cnt = add cnt 1 |
| 669 | i = add i 1 |
| 670 | else |
| 671 | break |
| 672 | end |
| 673 | end |
| 674 | ret cnt |
| 675 | end |
| 676 | |
| 677 | fn oh_is_zw_at buf view uchar * pos ulong to ulong |
| 678 | n = len buf |
| 679 | b0 = get buf pos |
| 680 | if and eq b0 0xc2 lt add pos 1 n |
| 681 | if eq get buf add pos 1 0xad |
| 682 | ret ulong 2 |
| 683 | end |
| 684 | end |
| 685 | if lt add pos 2 n |
| 686 | b1 = get buf add pos 1 |
| 687 | b2 = get buf add pos 2 |
| 688 | if and eq b0 0xe2 eq b1 0x80 |
| 689 | if or eq b2 0x8b or eq b2 0x8c eq b2 0x8d |
| 690 | ret ulong 3 |
| 691 | end |
| 692 | end |
| 693 | if and eq b0 0xe2 and eq b1 0x81 eq b2 0xa0 |
| 694 | ret ulong 3 |
| 695 | end |
| 696 | if and eq b0 0xef and eq b1 0xbb eq b2 0xbf |
| 697 | ret ulong 3 |
| 698 | end |
| 699 | end |
| 700 | ret ulong 0 |
| 701 | end |
| 702 | |
| 703 | fn oh_normalize_obfuscation src view uchar * dst edit uchar * to ulong |
| 704 | n = len src |
| 705 | cap = len dst |
| 706 | i = ulong 0 |
| 707 | out_n = ulong 0 |
| 708 | while lt i n bound 1024 |
| 709 | zw = oh_is_zw_at src i |
| 710 | if gt zw 0 |
| 711 | i = add i zw |
| 712 | continue |
| 713 | end |
| 714 | b = get src i |
| 715 | if and eq b 37 lt add i 2 n |
| 716 | hi = oh_hex_val get src add i 1 |
| 717 | lo = oh_hex_val get src add i 2 |
| 718 | if and lt hi 16 lt lo 16 |
| 719 | if lt out_n cap |
| 720 | put dst out_n uchar or shl hi 4 lo |
| 721 | out_n = add out_n 1 |
| 722 | end |
| 723 | i = add i 3 |
| 724 | continue |
| 725 | end |
| 726 | end |
| 727 | if lt out_n cap |
| 728 | put dst out_n b |
| 729 | out_n = add out_n 1 |
| 730 | end |
| 731 | i = add i 1 |
| 732 | end |
| 733 | ret out_n |
| 734 | end |
| 735 | |
| 736 | fn oh_leet_char c uchar to uchar |
| 737 | if eq c 48 |
| 738 | ret uchar 111 |
| 739 | end |
| 740 | if or eq c 49 eq c 33 |
| 741 | ret uchar 105 |
| 742 | end |
| 743 | if eq c 51 |
| 744 | ret uchar 101 |
| 745 | end |
| 746 | if or eq c 52 eq c 64 |
| 747 | ret uchar 97 |
| 748 | end |
| 749 | if or eq c 53 eq c 36 |
| 750 | ret uchar 115 |
| 751 | end |
| 752 | if eq c 55 |
| 753 | ret uchar 116 |
| 754 | end |
| 755 | ret c |
| 756 | end |
| 757 | |
| 758 | fn oh_normalize_leet src view uchar * dst edit uchar * to ulong |
| 759 | n = len src |
| 760 | cap = len dst |
| 761 | if gt n cap |
| 762 | n = cap |
| 763 | end |
| 764 | i = ulong 0 |
| 765 | while lt i n bound 1024 |
| 766 | put dst i oh_leet_char get src i |
| 767 | i = add i 1 |
| 768 | end |
| 769 | ret n |
| 770 | end |
| 771 | |
| 772 | fn oh_check_gh_at buf view uchar * pos ulong to bool |
| 773 | if gt add pos 40 len buf |
| 774 | ret false |
| 775 | end |
| 776 | if ne get buf pos 103 |
| 777 | ret false |
| 778 | end |
| 779 | if ne get buf add pos 1 104 |
| 780 | ret false |
| 781 | end |
| 782 | c2 = get buf add pos 2 |
| 783 | ok2 = or eq c2 112 or eq c2 111 or eq c2 117 or eq c2 115 eq c2 114 |
| 784 | if not ok2 |
| 785 | ret false |
| 786 | end |
| 787 | if ne get buf add pos 3 95 |
| 788 | ret false |
| 789 | end |
| 790 | ret ge oh_count_alnum_u buf add pos 4 36 |
| 791 | end |
| 792 | |
| 793 | fn oh_check_stripe_at buf view uchar * pos ulong to bool |
| 794 | if gt add pos 32 len buf |
| 795 | ret false |
| 796 | end |
| 797 | c0 = get buf pos |
| 798 | if not or eq c0 115 eq c0 114 |
| 799 | ret false |
| 800 | end |
| 801 | if ne get buf add pos 1 107 |
| 802 | ret false |
| 803 | end |
| 804 | if ne get buf add pos 2 95 |
| 805 | ret false |
| 806 | end |
| 807 | ok_kind = oh_match_at buf add pos 3 oh_t_live |
| 808 | if not ok_kind |
| 809 | ok_kind = oh_match_at buf add pos 3 oh_t_test |
| 810 | end |
| 811 | if not ok_kind |
| 812 | ret false |
| 813 | end |
| 814 | ret ge oh_count_alnum buf add pos 8 24 |
| 815 | end |
| 816 | |
| 817 | fn oh_is_b64_tok c uchar to bool |
| 818 | if oh_is_tok_char c |
| 819 | ret true |
| 820 | end |
| 821 | if eq c 43 |
| 822 | ret true |
| 823 | end |
| 824 | if eq c 61 |
| 825 | ret true |
| 826 | end |
| 827 | ret eq c 47 |
| 828 | end |
| 829 | |
| 830 | fn oh_skip_hws buf view uchar * pos ulong to ulong |
| 831 | n = len buf |
| 832 | p = pos |
| 833 | while lt p n bound 64 |
| 834 | c = get buf p |
| 835 | if or eq c 32 eq c 9 |
| 836 | p = add p 1 |
| 837 | else |
| 838 | break |
| 839 | end |
| 840 | end |
| 841 | ret p |
| 842 | end |
| 843 | |
| 844 | fn oh_check_generic_at buf view uchar * pos ulong keylen ulong to bool |
| 845 | n = len buf |
| 846 | p = oh_skip_hws buf add pos keylen |
| 847 | if ge p n |
| 848 | ret false |
| 849 | end |
| 850 | sep = get buf p |
| 851 | if not or eq sep 61 eq sep 58 |
| 852 | ret false |
| 853 | end |
| 854 | p = oh_skip_hws buf add p 1 |
| 855 | if ge p n |
| 856 | ret false |
| 857 | end |
| 858 | q = get buf p |
| 859 | if not or eq q 34 eq q 39 |
| 860 | ret false |
| 861 | end |
| 862 | p = add p 1 |
| 863 | cnt = ulong 0 |
| 864 | while lt p n bound 256 |
| 865 | c = get buf p |
| 866 | if eq c q |
| 867 | ret ge cnt 16 |
| 868 | end |
| 869 | if not oh_is_b64_tok c |
| 870 | ret false |
| 871 | end |
| 872 | cnt = add cnt 1 |
| 873 | p = add p 1 |
| 874 | end |
| 875 | ret false |
| 876 | end |
| 877 | |
| 878 | fn oh_scan_generic_secret buf view uchar * to bool |
| 879 | n = len buf |
| 880 | i = ulong 0 |
| 881 | while lt i n bound 1024 |
| 882 | if oh_match_ci_at buf i oh_t_ak_api |
| 883 | if oh_check_generic_at buf i len oh_t_ak_api |
| 884 | ret true |
| 885 | end |
| 886 | end |
| 887 | if oh_match_ci_at buf i oh_t_ak_apikey |
| 888 | if oh_check_generic_at buf i len oh_t_ak_apikey |
| 889 | ret true |
| 890 | end |
| 891 | end |
| 892 | if oh_match_ci_at buf i oh_t_ak_sec |
| 893 | if oh_check_generic_at buf i len oh_t_ak_sec |
| 894 | ret true |
| 895 | end |
| 896 | end |
| 897 | if oh_match_ci_at buf i oh_t_ak_priv |
| 898 | if oh_check_generic_at buf i len oh_t_ak_priv |
| 899 | ret true |
| 900 | end |
| 901 | end |
| 902 | if oh_match_ci_at buf i oh_t_ak_auth |
| 903 | if oh_check_generic_at buf i len oh_t_ak_auth |
| 904 | ret true |
| 905 | end |
| 906 | end |
| 907 | if oh_match_ci_at buf i oh_t_ak_bear |
| 908 | if oh_check_generic_at buf i len oh_t_ak_bear |
| 909 | ret true |
| 910 | end |
| 911 | end |
| 912 | i = add i 1 |
| 913 | end |
| 914 | ret false |
| 915 | end |
| 916 | |
| 917 | fn oh_user_path_len buf view uchar * pos ulong to ulong |
| 918 | n = len buf |
| 919 | plen = ulong 0 |
| 920 | sep = uchar 47 |
| 921 | if oh_match_at buf pos oh_t_home |
| 922 | plen = len oh_t_home |
| 923 | else |
| 924 | if oh_match_at buf pos oh_t_users |
| 925 | plen = len oh_t_users |
| 926 | else |
| 927 | if and oh_is_alpha get buf pos lt add pos 9 n |
| 928 | if oh_match_ci_at buf add pos 1 oh_t_wusers |
| 929 | plen = ulong 9 |
| 930 | sep = uchar 92 |
| 931 | else |
| 932 | if oh_match_ci_at buf add pos 1 oh_t_wusers2 |
| 933 | plen = ulong 9 |
| 934 | sep = uchar 47 |
| 935 | end |
| 936 | end |
| 937 | end |
| 938 | end |
| 939 | end |
| 940 | if eq plen 0 |
| 941 | ret ulong 0 |
| 942 | end |
| 943 | p = add pos plen |
| 944 | ulen = ulong 0 |
| 945 | while lt p n bound 128 |
| 946 | c = get buf p |
| 947 | if oh_is_path_char c |
| 948 | ulen = add ulen 1 |
| 949 | p = add p 1 |
| 950 | else |
| 951 | break |
| 952 | end |
| 953 | end |
| 954 | if and gt ulen 0 lt p n |
| 955 | if eq get buf p sep |
| 956 | ret sub add p 1 pos |
| 957 | end |
| 958 | end |
| 959 | ret ulong 0 |
| 960 | end |
| 961 | |
| 962 | fn oh_scan_secrets_raw buf view uchar * to ulong |
| 963 | n = len buf |
| 964 | i = ulong 0 |
| 965 | while lt i n bound 1024 |
| 966 | c = get buf i |
| 967 | note AWS Access Key: AKIA[0-9A-Z]{16} |
| 968 | if eq c 65 |
| 969 | if oh_match_at buf i oh_t_akia |
| 970 | if ge oh_count_upper_alnum buf add i 4 16 |
| 971 | ret OH_ERR_PRIV_AWS_KEY |
| 972 | end |
| 973 | end |
| 974 | if oh_match_at buf i oh_t_aiza |
| 975 | if ge oh_count_tok buf add i 4 30 |
| 976 | ret OH_ERR_PRIV_GOOGLE_KEY |
| 977 | end |
| 978 | end |
| 979 | end |
| 980 | note GitHub PAT |
| 981 | if eq c 103 |
| 982 | if oh_check_gh_at buf i |
| 983 | ret OH_ERR_PRIV_GITHUB_TOKEN |
| 984 | end |
| 985 | end |
| 986 | note Anthropic / OpenAI / Stripe keys |
| 987 | if or eq c 115 eq c 114 |
| 988 | if oh_check_stripe_at buf i |
| 989 | ret OH_ERR_PRIV_STRIPE_KEY |
| 990 | end |
| 991 | if oh_match_at buf i oh_t_sk_ant |
| 992 | if ge oh_count_tok buf add i 7 20 |
| 993 | ret OH_ERR_PRIV_ANTHROPIC_KEY |
| 994 | end |
| 995 | end |
| 996 | if oh_match_at buf i oh_t_sk |
| 997 | if ge oh_count_tok buf add i 3 20 |
| 998 | ret OH_ERR_PRIV_OPENAI_KEY |
| 999 | end |
| 1000 | end |
| 1001 | end |
| 1002 | note HuggingFace token: hf_[A-Za-z0-9]{34,} |
| 1003 | if eq c 104 |
| 1004 | if oh_match_at buf i oh_t_hf |
| 1005 | if ge oh_count_alnum buf add i 3 34 |
| 1006 | ret OH_ERR_PRIV_HF_TOKEN |
| 1007 | end |
| 1008 | end |
| 1009 | end |
| 1010 | note PEM private key block |
| 1011 | if eq c 45 |
| 1012 | if oh_match_at buf i oh_t_pem_beg |
| 1013 | j = add i len oh_t_pem_beg |
| 1014 | while lt j n bound 128 |
| 1015 | if oh_match_at buf j oh_t_pem_end |
| 1016 | ret OH_ERR_PRIV_PEM_KEY |
| 1017 | end |
| 1018 | j = add j 1 |
| 1019 | end |
| 1020 | end |
| 1021 | end |
| 1022 | i = add i 1 |
| 1023 | end |
| 1024 | if oh_scan_generic_secret buf |
| 1025 | ret OH_ERR_PRIV_GENERIC_SECRET |
| 1026 | end |
| 1027 | ret OH_OK |
| 1028 | end |
| 1029 | |
| 1030 | fn oh_scan_paths_raw buf view uchar * to ulong |
| 1031 | n = len buf |
| 1032 | i = ulong 0 |
| 1033 | while lt i n bound 1024 |
| 1034 | if gt oh_user_path_len buf i 0 |
| 1035 | ret OH_ERR_PRIV_USER_PATH |
| 1036 | end |
| 1037 | if and eq get buf i 46 lt add i 2 n |
| 1038 | if eq get buf add i 1 46 |
| 1039 | c2 = get buf add i 2 |
| 1040 | if or eq c2 47 eq c2 92 |
| 1041 | ret OH_ERR_PRIV_TRAVERSAL |
| 1042 | end |
| 1043 | end |
| 1044 | end |
| 1045 | i = add i 1 |
| 1046 | end |
| 1047 | ret OH_OK |
| 1048 | end |
| 1049 | |
| 1050 | fn oh_privacy_check payload view uchar * to ulong |
| 1051 | if eq len payload 0 |
| 1052 | ret OH_ERR_EMPTY_PAYLOAD |
| 1053 | end |
| 1054 | rc = oh_scan_secrets_raw payload |
| 1055 | if ne rc OH_OK |
| 1056 | ret rc |
| 1057 | end |
| 1058 | rc = oh_scan_paths_raw payload |
| 1059 | if ne rc OH_OK |
| 1060 | ret rc |
| 1061 | end |
| 1062 | norm = new uchar 512 0 |
| 1063 | nn = oh_normalize_obfuscation payload norm |
| 1064 | rc = oh_scan_secrets_raw part norm 0 nn |
| 1065 | if ne rc OH_OK |
| 1066 | ret rc |
| 1067 | end |
| 1068 | rc = oh_scan_paths_raw part norm 0 nn |
| 1069 | if ne rc OH_OK |
| 1070 | ret rc |
| 1071 | end |
| 1072 | leet = new uchar 512 0 |
| 1073 | ln = oh_normalize_leet part norm 0 nn leet |
| 1074 | if oh_scan_generic_secret part leet 0 ln |
| 1075 | ret OH_ERR_PRIV_GENERIC_SECRET |
| 1076 | end |
| 1077 | ret OH_OK |
| 1078 | end |
| 1079 | |
| 1080 | fn oh_scrub_paths src view uchar * dst edit uchar * to ulong |
| 1081 | n = len src |
| 1082 | cap = len dst |
| 1083 | i = ulong 0 |
| 1084 | out_n = ulong 0 |
| 1085 | while lt i n bound 1024 |
| 1086 | plen = oh_user_path_len src i |
| 1087 | if gt plen 0 |
| 1088 | if lt add out_n 1 cap |
| 1089 | put dst out_n uchar 126 |
| 1090 | put dst add out_n 1 uchar 47 |
| 1091 | out_n = add out_n 2 |
| 1092 | end |
| 1093 | i = add i plen |
| 1094 | continue |
| 1095 | end |
| 1096 | if lt out_n cap |
| 1097 | put dst out_n get src i |
| 1098 | out_n = add out_n 1 |
| 1099 | end |
| 1100 | i = add i 1 |
| 1101 | end |
| 1102 | ret out_n |
| 1103 | end |
| 1104 | |
| 1105 | note --- Strict Authority Boundary Enforcement --- |
| 1106 | |
| 1107 | fn oh_scan_escalation_raw buf view uchar * to bool |
| 1108 | n = len buf |
| 1109 | i = ulong 0 |
| 1110 | while lt i n bound 1024 |
| 1111 | if oh_match_ci_at buf i oh_t_esc_exec |
| 1112 | ret true |
| 1113 | end |
| 1114 | if oh_match_ci_at buf i oh_t_esc_sudo |
| 1115 | ret true |
| 1116 | end |
| 1117 | if oh_match_ci_at buf i oh_t_esc_chmod |
| 1118 | ret true |
| 1119 | end |
| 1120 | if oh_match_ci_at buf i oh_t_esc_chown |
| 1121 | ret true |
| 1122 | end |
| 1123 | if oh_match_ci_at buf i oh_t_esc_ident |
| 1124 | ret true |
| 1125 | end |
| 1126 | if oh_match_ci_at buf i oh_t_esc_seed |
| 1127 | ret true |
| 1128 | end |
| 1129 | if oh_match_ci_at buf i oh_t_esc_admin |
| 1130 | ret true |
| 1131 | end |
| 1132 | i = add i 1 |
| 1133 | end |
| 1134 | ret false |
| 1135 | end |
| 1136 | |
| 1137 | fn oh_check_authority cap_flags ulong payload view uchar * to ulong |
| 1138 | if ne and cap_flags OH_CAP_LOCAL_EXEC 0 |
| 1139 | ret OH_ERR_AUTH_EXEC |
| 1140 | end |
| 1141 | if ne and cap_flags OH_CAP_SECRET_ACCESS 0 |
| 1142 | ret OH_ERR_AUTH_SECRET |
| 1143 | end |
| 1144 | if ne and cap_flags OH_CAP_IDENTITY_MUTATE 0 |
| 1145 | ret OH_ERR_AUTH_IDENTITY |
| 1146 | end |
| 1147 | if ne and cap_flags OH_CAP_ADMIN_RIGHTS 0 |
| 1148 | ret OH_ERR_AUTH_ADMIN |
| 1149 | end |
| 1150 | if ne cap_flags 0 |
| 1151 | ret OH_ERR_AUTH_UNKNOWN |
| 1152 | end |
| 1153 | if oh_scan_escalation_raw payload |
| 1154 | ret OH_ERR_AUTH_PAYLOAD_ESC |
| 1155 | end |
| 1156 | norm = new uchar 512 0 |
| 1157 | nn = oh_normalize_obfuscation payload norm |
| 1158 | if oh_scan_escalation_raw part norm 0 nn |
| 1159 | ret OH_ERR_AUTH_PAYLOAD_ESC |
| 1160 | end |
| 1161 | ret OH_OK |
| 1162 | end |
| 1163 | |
| 1164 | note --- Consent, TTL, Replay, Audit Trail, and Bounded Peer Assistance Queue --- |
| 1165 | |
| 1166 | fn oh_check_consent pol view OhPolicy env_consent ulong to ulong |
| 1167 | if eq get pol opt_in 0 |
| 1168 | ret OH_ERR_NODE_OPT_OUT |
| 1169 | end |
| 1170 | if ne env_consent 1 |
| 1171 | ret OH_ERR_NO_CONSENT |
| 1172 | end |
| 1173 | ret OH_OK |
| 1174 | end |
| 1175 | |
| 1176 | fn oh_check_ttl now_ms ulong created_ms ulong ttl_ms ulong to ulong |
| 1177 | if or eq ttl_ms 0 gt ttl_ms OH_MAX_TTL_MS |
| 1178 | ret OH_ERR_TTL_INVALID |
| 1179 | end |
| 1180 | if gt created_ms add now_ms OH_MAX_FUTURE_DRIFT_MS |
| 1181 | ret OH_ERR_FUTURE_DRIFT |
| 1182 | end |
| 1183 | if ge now_ms add created_ms ttl_ms |
| 1184 | ret OH_ERR_TTL_EXPIRED |
| 1185 | end |
| 1186 | ret OH_OK |
| 1187 | end |
| 1188 | |
| 1189 | fn oh_audit_record pol edit OhPolicy kind ulong peer_id ulong req_id ulong cat ulong code ulong to void |
| 1190 | seq = add get pol audit_seq 1 |
| 1191 | put pol audit_seq seq |
| 1192 | idx = rem sub seq 1 OH_AUDIT_CAP |
| 1193 | put oh_aud_seq idx seq |
| 1194 | put oh_aud_kind idx kind |
| 1195 | put oh_aud_peer idx peer_id |
| 1196 | put oh_aud_req idx req_id |
| 1197 | put oh_aud_cat idx cat |
| 1198 | put oh_aud_code idx code |
| 1199 | put oh_aud_ts idx get pol now_ms |
| 1200 | put oh_aud_dig idx ulong 0 |
| 1201 | cnt = get oh_aud_cnt 0 |
| 1202 | if lt cnt OH_AUDIT_CAP |
| 1203 | put oh_aud_cnt 0 add cnt 1 |
| 1204 | end |
| 1205 | end |
| 1206 | |
| 1207 | fn oh_audit_set_last_digest pol view OhPolicy dig ulong to void |
| 1208 | seq = get pol audit_seq |
| 1209 | if eq seq 0 |
| 1210 | ret |
| 1211 | end |
| 1212 | idx = rem sub seq 1 OH_AUDIT_CAP |
| 1213 | put oh_aud_dig idx dig |
| 1214 | end |
| 1215 | |
| 1216 | fn oh_audit_count to ulong |
| 1217 | ret get oh_aud_cnt 0 |
| 1218 | end |
| 1219 | |
| 1220 | fn oh_seen_check_and_record pol edit OhPolicy peer_id ulong dig ulong to bool |
| 1221 | i = ulong 0 |
| 1222 | while lt i OH_SEEN_CAP bound 64 |
| 1223 | if and eq get oh_seen_peer i peer_id eq get oh_seen_dig i dig |
| 1224 | ret false |
| 1225 | end |
| 1226 | i = add i 1 |
| 1227 | end |
| 1228 | head = get pol seen_head |
| 1229 | put oh_seen_peer head peer_id |
| 1230 | put oh_seen_dig head dig |
| 1231 | put pol seen_head rem add head 1 OH_SEEN_CAP |
| 1232 | ret true |
| 1233 | end |
| 1234 | |
| 1235 | fn oh_queue_sweep_expired pol edit OhPolicy to ulong |
| 1236 | now = get pol now_ms |
| 1237 | swept = ulong 0 |
| 1238 | i = ulong 0 |
| 1239 | while lt i OH_QUEUE_CAP bound 64 |
| 1240 | if eq get oh_q_status i OH_Q_PENDING |
| 1241 | exp = add get oh_q_created i get oh_q_ttl i |
| 1242 | if ge now exp |
| 1243 | put oh_q_status i OH_Q_EXPIRED |
| 1244 | oh_audit_record pol OH_AUD_EXPIRED get oh_q_peer_id i get oh_q_req_id i get oh_q_cat i OH_ERR_TTL_EXPIRED |
| 1245 | oh_audit_set_last_digest pol get oh_q_digest i |
| 1246 | swept = add swept 1 |
| 1247 | end |
| 1248 | end |
| 1249 | i = add i 1 |
| 1250 | end |
| 1251 | ret swept |
| 1252 | end |
| 1253 | |
| 1254 | fn oh_queue_pending_count to ulong |
| 1255 | cnt = ulong 0 |
| 1256 | i = ulong 0 |
| 1257 | while lt i OH_QUEUE_CAP bound 64 |
| 1258 | if eq get oh_q_status i OH_Q_PENDING |
| 1259 | cnt = add cnt 1 |
| 1260 | end |
| 1261 | i = add i 1 |
| 1262 | end |
| 1263 | ret cnt |
| 1264 | end |
| 1265 | |
| 1266 | fn oh_queue_find_free to ulong |
| 1267 | i = ulong 0 |
| 1268 | while lt i OH_QUEUE_CAP bound 64 |
| 1269 | st = get oh_q_status i |
| 1270 | if or eq st OH_Q_EMPTY eq st OH_Q_EXPIRED |
| 1271 | ret i |
| 1272 | end |
| 1273 | i = add i 1 |
| 1274 | end |
| 1275 | ret OH_QUEUE_CAP |
| 1276 | end |
| 1277 | |
| 1278 | fn oh_queue_store_slot slot ulong rid ulong env view OhEnv payload view uchar * dig ulong to void |
| 1279 | put oh_q_status slot OH_Q_PENDING |
| 1280 | put oh_q_req_id slot rid |
| 1281 | put oh_q_peer_id slot get env peer_id |
| 1282 | put oh_q_cat slot get env category |
| 1283 | put oh_q_created slot get env created_ms |
| 1284 | put oh_q_ttl slot get env ttl_ms |
| 1285 | put oh_q_digest slot dig |
| 1286 | put oh_q_helper slot ulong 0 |
| 1287 | plen = len payload |
| 1288 | put oh_q_plen slot plen |
| 1289 | base = mul slot OH_MAX_PAYLOAD |
| 1290 | i = ulong 0 |
| 1291 | while lt i plen bound 512 |
| 1292 | put oh_q_payload add base i get payload i |
| 1293 | i = add i 1 |
| 1294 | end |
| 1295 | end |
| 1296 | |
| 1297 | fn oh_validate_distress pol edit OhPolicy env view OhEnv payload view uchar * to ulong |
| 1298 | rc = oh_check_consent pol get env consent |
| 1299 | if ne rc OH_OK |
| 1300 | ret rc |
| 1301 | end |
| 1302 | if ne get env version OH_VERSION |
| 1303 | ret OH_ERR_BAD_VERSION |
| 1304 | end |
| 1305 | if eq get env peer_id 0 |
| 1306 | ret OH_ERR_BAD_PEER |
| 1307 | end |
| 1308 | if gt get env category OH_CAT_MAX |
| 1309 | ret OH_ERR_BAD_CATEGORY |
| 1310 | end |
| 1311 | rc = oh_check_authority get env cap_flags payload |
| 1312 | if ne rc OH_OK |
| 1313 | ret rc |
| 1314 | end |
| 1315 | rc = oh_check_ttl get pol now_ms get env created_ms get env ttl_ms |
| 1316 | if ne rc OH_OK |
| 1317 | ret rc |
| 1318 | end |
| 1319 | ret oh_privacy_check payload |
| 1320 | end |
| 1321 | |
| 1322 | fn oh_submit_distress pol edit OhPolicy env view OhEnv payload view uchar * out_req edit ulong 1 to ulong |
| 1323 | put out_req 0 ulong 0 |
| 1324 | swept = oh_queue_sweep_expired pol |
| 1325 | if gt swept OH_QUEUE_CAP |
| 1326 | ret OH_ERR_QUEUE_FULL |
| 1327 | end |
| 1328 | rc = oh_validate_distress pol env payload |
| 1329 | if ne rc OH_OK |
| 1330 | oh_audit_record pol OH_AUD_REFUSED get env peer_id 0 get env category rc |
| 1331 | oh_audit_set_last_digest pol get env digest |
| 1332 | ret rc |
| 1333 | end |
| 1334 | dig = oh_compute_digest env payload |
| 1335 | if not oh_seen_check_and_record pol get env peer_id dig |
| 1336 | oh_audit_record pol OH_AUD_REFUSED get env peer_id 0 get env category OH_ERR_REPLAY |
| 1337 | oh_audit_set_last_digest pol dig |
| 1338 | ret OH_ERR_REPLAY |
| 1339 | end |
| 1340 | slot = oh_queue_find_free |
| 1341 | if ge slot OH_QUEUE_CAP |
| 1342 | oh_audit_record pol OH_AUD_REFUSED get env peer_id 0 get env category OH_ERR_QUEUE_FULL |
| 1343 | oh_audit_set_last_digest pol dig |
| 1344 | ret OH_ERR_QUEUE_FULL |
| 1345 | end |
| 1346 | rid = get pol next_req_id |
| 1347 | put pol next_req_id add rid 1 |
| 1348 | oh_queue_store_slot slot rid env payload dig |
| 1349 | put out_req 0 rid |
| 1350 | oh_audit_record pol OH_AUD_ENQUEUED get env peer_id rid get env category OH_OK |
| 1351 | oh_audit_set_last_digest pol dig |
| 1352 | ret OH_OK |
| 1353 | end |
| 1354 | |
| 1355 | fn oh_find_req_slot req_id ulong to ulong |
| 1356 | if eq req_id 0 |
| 1357 | ret OH_QUEUE_CAP |
| 1358 | end |
| 1359 | i = ulong 0 |
| 1360 | while lt i OH_QUEUE_CAP bound 64 |
| 1361 | if and ne get oh_q_status i OH_Q_EMPTY eq get oh_q_req_id i req_id |
| 1362 | ret i |
| 1363 | end |
| 1364 | i = add i 1 |
| 1365 | end |
| 1366 | ret OH_QUEUE_CAP |
| 1367 | end |
| 1368 | |
| 1369 | fn oh_resolve_distress pol edit OhPolicy req_id ulong helper view OhEnv solution view uchar * to ulong |
| 1370 | swept = oh_queue_sweep_expired pol |
| 1371 | if gt swept OH_QUEUE_CAP |
| 1372 | ret OH_ERR_QUEUE_FULL |
| 1373 | end |
| 1374 | rc = oh_check_consent pol get helper consent |
| 1375 | if ne rc OH_OK |
| 1376 | oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id 0 rc |
| 1377 | ret rc |
| 1378 | end |
| 1379 | if eq get helper peer_id 0 |
| 1380 | oh_audit_record pol OH_AUD_REFUSED 0 req_id 0 OH_ERR_BAD_PEER |
| 1381 | ret OH_ERR_BAD_PEER |
| 1382 | end |
| 1383 | rc = oh_check_authority get helper cap_flags solution |
| 1384 | if ne rc OH_OK |
| 1385 | oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id 0 rc |
| 1386 | ret rc |
| 1387 | end |
| 1388 | rc = oh_privacy_check solution |
| 1389 | if ne rc OH_OK |
| 1390 | oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id 0 rc |
| 1391 | ret rc |
| 1392 | end |
| 1393 | slot = oh_find_req_slot req_id |
| 1394 | if ge slot OH_QUEUE_CAP |
| 1395 | oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id 0 OH_ERR_REQ_NOT_FOUND |
| 1396 | ret OH_ERR_REQ_NOT_FOUND |
| 1397 | end |
| 1398 | st = get oh_q_status slot |
| 1399 | if eq st OH_Q_EXPIRED |
| 1400 | oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id get oh_q_cat slot OH_ERR_TTL_EXPIRED |
| 1401 | ret OH_ERR_TTL_EXPIRED |
| 1402 | end |
| 1403 | if eq st OH_Q_RESOLVED |
| 1404 | oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id get oh_q_cat slot OH_ERR_REQ_ALREADY_RESOLVED |
| 1405 | ret OH_ERR_REQ_ALREADY_RESOLVED |
| 1406 | end |
| 1407 | if eq get oh_q_peer_id slot get helper peer_id |
| 1408 | oh_audit_record pol OH_AUD_REFUSED get helper peer_id req_id get oh_q_cat slot OH_ERR_SELF_RESOLVE |
| 1409 | ret OH_ERR_SELF_RESOLVE |
| 1410 | end |
| 1411 | put oh_q_status slot OH_Q_RESOLVED |
| 1412 | put oh_q_helper slot get helper peer_id |
| 1413 | oh_audit_record pol OH_AUD_RESOLVED get helper peer_id req_id get oh_q_cat slot OH_OK |
| 1414 | oh_audit_set_last_digest pol get oh_q_digest slot |
| 1415 | ret OH_OK |
| 1416 | end |