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 / aienux / aegis / aegis.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/aegis/aegis.om: Pure-Omega AIENOS AEGIS Capability Graph, Effect Intent, |
| 3 | note Operator Grant, J-Space World, Policy Evaluator, and Effect Broker. |
| 4 | note Converted from aienux crates/aienos-aegis/src/{capability,effect,world,evaluator,broker}.rs. |
| 5 | note Compose with: si/directives/om2.om om/tls/sha2.om aienux/aegis/aegis.om ... |
| 6 | |
| 7 | const AEGIS_MAX_CAPS ulong 32 |
| 8 | const AEGIS_MAX_DELEG_DEPTH uint 4 |
| 9 | const AEGIS_MAX_STR ulong 64 |
| 10 | const AEGIS_MAX_WORLDS ulong 8 |
| 11 | const AEGIS_MAX_AUDIT ulong 64 |
| 12 | |
| 13 | const AEGIS_SCOPE_SYSADMIN uchar 1 |
| 14 | const AEGIS_SCOPE_FS uchar 2 |
| 15 | const AEGIS_SCOPE_NET uchar 3 |
| 16 | const AEGIS_SCOPE_DEVICE uchar 4 |
| 17 | const AEGIS_SCOPE_WORLD uchar 5 |
| 18 | const AEGIS_SCOPE_STANDING_FETCH uchar 6 |
| 19 | const AEGIS_SCOPE_CUSTOM uchar 7 |
| 20 | |
| 21 | const AEGIS_DEC_APPROVED uchar 1 |
| 22 | const AEGIS_DEC_REQUIRES_OP uchar 2 |
| 23 | const AEGIS_DEC_REJECTED uchar 3 |
| 24 | |
| 25 | const AEGIS_OK int 0 |
| 26 | const AEGIS_ERR_NOT_FOUND int -1 |
| 27 | const AEGIS_ERR_BAD_SIG int -2 |
| 28 | const AEGIS_ERR_REVOKED int -3 |
| 29 | const AEGIS_ERR_EXPIRED int -4 |
| 30 | const AEGIS_ERR_HOLDER int -5 |
| 31 | const AEGIS_ERR_SCOPE int -6 |
| 32 | const AEGIS_ERR_DEPTH int -7 |
| 33 | const AEGIS_ERR_MISSING_GRANT int -8 |
| 34 | const AEGIS_ERR_GRANT_INTENT int -9 |
| 35 | const AEGIS_ERR_GRANT_SIG int -10 |
| 36 | const AEGIS_ERR_WORLD_STATE int -11 |
| 37 | const AEGIS_ERR_CAPACITY int -12 |
| 38 | |
| 39 | const AEGIS_WORLD_NONE uchar 0 |
| 40 | const AEGIS_WORLD_ACTIVE uchar 1 |
| 41 | const AEGIS_WORLD_COMMITTED uchar 2 |
| 42 | const AEGIS_WORLD_ROLLEDBACK uchar 3 |
| 43 | |
| 44 | text aegis_dom_cap "AIENOS_CAP_V1" |
| 45 | text aegis_dom_intent "AIENOS_INTENT_V1" |
| 46 | text aegis_dom_grant "AIENOS_GRANT_V1" |
| 47 | text aegis_act_fs_read "fs.read" |
| 48 | text aegis_act_fs_write "fs.write" |
| 49 | text aegis_act_fs_delete "fs.delete" |
| 50 | text aegis_act_net_fetch "net.fetch" |
| 51 | text aegis_act_net_read "net.read" |
| 52 | text aegis_act_net_connect "net.connect" |
| 53 | text aegis_act_net_send "net.send" |
| 54 | text aegis_act_dev_read "device.read" |
| 55 | text aegis_act_dev_write "device.write" |
| 56 | text aegis_act_hw_inspect "hardware.inspect" |
| 57 | text aegis_act_disk_format "disk.format" |
| 58 | text aegis_act_git_pub "git.publish" |
| 59 | text aegis_act_code_pub "code.publish" |
| 60 | text aegis_act_kern_rep "kernel.replace" |
| 61 | text aegis_act_sys_inst "system.install" |
| 62 | text aegis_act_cred_mod "credential.modify" |
| 63 | text aegis_act_fin_spend "finance.spend" |
| 64 | text aegis_pfx_device "device." |
| 65 | text aegis_pfx_world "world." |
| 66 | |
| 67 | global aegis_op_secret uchar 32 |
| 68 | global aegis_cap_secret uchar 32 |
| 69 | |
| 70 | global aegis_cap_used uchar 32 |
| 71 | global aegis_cap_revoked uchar 32 |
| 72 | global aegis_cap_id uchar 512 |
| 73 | global aegis_cap_parent int 32 |
| 74 | global aegis_cap_depth uint 32 |
| 75 | global aegis_cap_holder ulong 32 |
| 76 | global aegis_cap_scope uchar 32 |
| 77 | global aegis_cap_ro uchar 32 |
| 78 | global aegis_cap_port uint 32 |
| 79 | global aegis_cap_exp ulong 32 |
| 80 | global aegis_cap_str uchar 2048 |
| 81 | global aegis_cap_slen ulong 32 |
| 82 | global aegis_cap_str2 uchar 2048 |
| 83 | global aegis_cap_s2len ulong 32 |
| 84 | global aegis_cap_sig uchar 1024 |
| 85 | |
| 86 | global aegis_w_status uchar 8 |
| 87 | global aegis_w_id ulong 8 |
| 88 | global aegis_w_deltas uint 8 |
| 89 | global aegis_w_writes uint 8 |
| 90 | global aegis_w_deletes uint 8 |
| 91 | |
| 92 | global aegis_audit_cnt ulong 1 |
| 93 | global aegis_audit_dec uchar 64 |
| 94 | global aegis_audit_rc int 64 |
| 95 | global aegis_audit_exec uchar 64 |
| 96 | global aegis_audit_ts ulong 64 |
| 97 | global aegis_audit_iid uchar 1024 |
| 98 | |
| 99 | global aegis_cur_iid uchar 16 |
| 100 | global aegis_cur_grant_iid uchar 16 |
| 101 | global aegis_cur_grant_sig uchar 32 |
| 102 | note Grant meta: 0=present, 1=granted_at_utc, 2=op_id_len, 3=mutant_skip_grant |
| 103 | global aegis_cur_grant_meta ulong 4 |
| 104 | global aegis_cur_grant_op uchar 64 |
| 105 | |
| 106 | note Last eval result: 0=decision, 1=reason_code (cast via long) |
| 107 | global aegis_last_dec uchar 1 |
| 108 | global aegis_last_rc int 1 |
| 109 | |
| 110 | note Dispatch context: 0=world_id, 1=is_reversible, 2=now_utc, 3=net_port |
| 111 | global aegis_dctx ulong 4 |
| 112 | |
| 113 | note Scratch buffers for hashing (never passed as globals) |
| 114 | global aegis_tmp_s1 uchar 64 |
| 115 | global aegis_tmp_s2 uchar 64 |
| 116 | global aegis_tmp_lens ulong 2 |
| 117 | |
| 118 | fn aegis_dispatch_ctx world_id ulong is_rev ulong now_utc ulong port ulong to void |
| 119 | put aegis_dctx 0 world_id |
| 120 | put aegis_dctx 1 is_rev |
| 121 | put aegis_dctx 2 now_utc |
| 122 | put aegis_dctx 3 port |
| 123 | end |
| 124 | |
| 125 | fn aegis_reset op_sec view uchar 32 cap_sec view uchar 32 to void |
| 126 | for i ulong 32 |
| 127 | put aegis_op_secret i get op_sec i |
| 128 | put aegis_cap_secret i get cap_sec i |
| 129 | put aegis_cap_used i 0 |
| 130 | put aegis_cap_revoked i 0 |
| 131 | put aegis_cap_parent i -1 |
| 132 | put aegis_cap_depth i 0 |
| 133 | put aegis_cap_holder i 0 |
| 134 | put aegis_cap_scope i 0 |
| 135 | put aegis_cap_ro i 0 |
| 136 | put aegis_cap_port i 0 |
| 137 | put aegis_cap_exp i 0 |
| 138 | put aegis_cap_slen i 0 |
| 139 | put aegis_cap_s2len i 0 |
| 140 | end |
| 141 | for w ulong 8 |
| 142 | put aegis_w_status w AEGIS_WORLD_NONE |
| 143 | put aegis_w_id w 0 |
| 144 | put aegis_w_deltas w 0 |
| 145 | put aegis_w_writes w 0 |
| 146 | put aegis_w_deletes w 0 |
| 147 | end |
| 148 | put aegis_audit_cnt 0 0 |
| 149 | put aegis_last_dec 0 0 |
| 150 | put aegis_last_rc 0 0 |
| 151 | for g ulong 4 |
| 152 | put aegis_cur_grant_meta g 0 |
| 153 | put aegis_dctx g 0 |
| 154 | end |
| 155 | end |
| 156 | |
| 157 | fn aegis_set_mutant_skip_grant v ulong to void |
| 158 | put aegis_cur_grant_meta 3 v |
| 159 | end |
| 160 | |
| 161 | note ---- Byte/String & Lexical Path Helpers ---- |
| 162 | |
| 163 | fn aegis_str_eq a view uchar * b view uchar * to bool |
| 164 | if ne len a len b |
| 165 | ret false |
| 166 | end |
| 167 | n = len a |
| 168 | i = ulong 0 |
| 169 | while lt i n bound 256 |
| 170 | if ne get a i get b i |
| 171 | ret false |
| 172 | end |
| 173 | i = add i 1 |
| 174 | end |
| 175 | ret true |
| 176 | end |
| 177 | |
| 178 | fn aegis_str_starts a view uchar * pfx view uchar * to bool |
| 179 | if lt len a len pfx |
| 180 | ret false |
| 181 | end |
| 182 | n = len pfx |
| 183 | i = ulong 0 |
| 184 | while lt i n bound 256 |
| 185 | if ne get a i get pfx i |
| 186 | ret false |
| 187 | end |
| 188 | i = add i 1 |
| 189 | end |
| 190 | ret true |
| 191 | end |
| 192 | |
| 193 | fn aegis_has_dotdot p view uchar * to bool |
| 194 | n = len p |
| 195 | i = ulong 0 |
| 196 | while lt i n bound 256 |
| 197 | if eq get p i 46 |
| 198 | if lt add i 1 n |
| 199 | if eq get p add i 1 46 |
| 200 | prev_ok = eq i 0 |
| 201 | if gt i 0 |
| 202 | if eq get p sub i 1 47 |
| 203 | prev_ok = true |
| 204 | end |
| 205 | end |
| 206 | next_ok = eq add i 2 n |
| 207 | if lt add i 2 n |
| 208 | if eq get p add i 2 47 |
| 209 | next_ok = true |
| 210 | end |
| 211 | end |
| 212 | if and prev_ok next_ok |
| 213 | ret true |
| 214 | end |
| 215 | end |
| 216 | end |
| 217 | end |
| 218 | i = add i 1 |
| 219 | end |
| 220 | ret false |
| 221 | end |
| 222 | |
| 223 | note Lexical absolute path confinement (mirrors capability.rs path_within). |
| 224 | note Both path and prefix must start with '/', contain no '..' component, |
| 225 | note and path must equal prefix or be inside prefix/ (prefix "/" matches any absolute path). |
| 226 | fn aegis_path_within path view uchar * prefix view uchar * to bool |
| 227 | plen = len path |
| 228 | xlen = len prefix |
| 229 | if or eq plen 0 eq xlen 0 |
| 230 | ret false |
| 231 | end |
| 232 | if or ne get path 0 47 ne get prefix 0 47 |
| 233 | ret false |
| 234 | end |
| 235 | if or aegis_has_dotdot path aegis_has_dotdot prefix |
| 236 | ret false |
| 237 | end |
| 238 | if and eq xlen 1 eq get prefix 0 47 |
| 239 | ret true |
| 240 | end |
| 241 | note Trim trailing '/' on prefix for comparison |
| 242 | ex = xlen |
| 243 | while and gt ex 1 eq get prefix sub ex 1 47 bound 64 |
| 244 | ex = sub ex 1 |
| 245 | end |
| 246 | if lt plen ex |
| 247 | ret false |
| 248 | end |
| 249 | i = ulong 0 |
| 250 | while lt i ex bound 256 |
| 251 | if ne get path i get prefix i |
| 252 | ret false |
| 253 | end |
| 254 | i = add i 1 |
| 255 | end |
| 256 | if eq plen ex |
| 257 | ret true |
| 258 | end |
| 259 | ret eq get path ex 47 |
| 260 | end |
| 261 | |
| 262 | note ---- SHA-256 Encoding Helpers ---- |
| 263 | |
| 264 | fn aegis_put_be64 buf edit uchar 8 v ulong to void |
| 265 | put buf 0 uchar and shr v 56 0xff |
| 266 | put buf 1 uchar and shr v 48 0xff |
| 267 | put buf 2 uchar and shr v 40 0xff |
| 268 | put buf 3 uchar and shr v 32 0xff |
| 269 | put buf 4 uchar and shr v 24 0xff |
| 270 | put buf 5 uchar and shr v 16 0xff |
| 271 | put buf 6 uchar and shr v 8 0xff |
| 272 | put buf 7 uchar and v 0xff |
| 273 | end |
| 274 | |
| 275 | fn aegis_sha_u64 c edit ulong 40 v ulong to void |
| 276 | b = new uchar 8 0 |
| 277 | aegis_put_be64 b v |
| 278 | sha256_update c b |
| 279 | end |
| 280 | |
| 281 | fn aegis_sha_field c edit ulong 40 f view uchar * to void |
| 282 | aegis_sha_u64 c len f |
| 283 | if gt len f 0 |
| 284 | sha256_update c f |
| 285 | end |
| 286 | end |
| 287 | |
| 288 | fn aegis_copy_cap_s1 slot ulong dst edit uchar 64 to ulong |
| 289 | n = get aegis_cap_slen slot |
| 290 | base = mul slot AEGIS_MAX_STR |
| 291 | i = ulong 0 |
| 292 | while lt i n bound 64 |
| 293 | put dst i get aegis_cap_str add base i |
| 294 | i = add i 1 |
| 295 | end |
| 296 | ret n |
| 297 | end |
| 298 | |
| 299 | fn aegis_copy_cap_s2 slot ulong dst edit uchar 64 to ulong |
| 300 | n = get aegis_cap_s2len slot |
| 301 | base = mul slot AEGIS_MAX_STR |
| 302 | i = ulong 0 |
| 303 | while lt i n bound 64 |
| 304 | put dst i get aegis_cap_str2 add base i |
| 305 | i = add i 1 |
| 306 | end |
| 307 | ret n |
| 308 | end |
| 309 | |
| 310 | note Compute 32-byte SHA-256 signature for capability at slot. |
| 311 | fn aegis_cap_compute_sig slot ulong out_sig edit uchar 32 to void |
| 312 | c = new ulong 40 0 |
| 313 | sec = new uchar 32 0 |
| 314 | s1 = new uchar 64 0 |
| 315 | s2 = new uchar 64 0 |
| 316 | for i ulong 32 |
| 317 | put sec i get aegis_cap_secret i |
| 318 | end |
| 319 | sha256_init c |
| 320 | sha256_update c aegis_dom_cap |
| 321 | sha256_update c sec |
| 322 | aegis_sha_u64 c get aegis_cap_holder slot |
| 323 | aegis_sha_u64 c ulong get aegis_cap_scope slot |
| 324 | aegis_sha_u64 c ulong get aegis_cap_ro slot |
| 325 | aegis_sha_u64 c ulong get aegis_cap_port slot |
| 326 | aegis_sha_u64 c get aegis_cap_exp slot |
| 327 | aegis_sha_u64 c ulong get aegis_cap_depth slot |
| 328 | n1 = aegis_copy_cap_s1 slot s1 |
| 329 | aegis_sha_field c part s1 0 n1 |
| 330 | n2 = aegis_copy_cap_s2 slot s2 |
| 331 | aegis_sha_field c part s2 0 n2 |
| 332 | sha256_final c out_sig |
| 333 | end |
| 334 | |
| 335 | fn aegis_eq32 a view uchar 32 b view uchar 32 to bool |
| 336 | diff = uchar 0 |
| 337 | for i ulong 32 |
| 338 | diff = or diff xor get a i get b i |
| 339 | end |
| 340 | ret eq diff 0 |
| 341 | end |
| 342 | |
| 343 | fn aegis_eq16 a view uchar 16 b view uchar 16 to bool |
| 344 | diff = uchar 0 |
| 345 | for i ulong 16 |
| 346 | diff = or diff xor get a i get b i |
| 347 | end |
| 348 | ret eq diff 0 |
| 349 | end |
| 350 | |
| 351 | note ---- Scope Attenuation & Action Authorization ---- |
| 352 | |
| 353 | fn aegis_scope_contains parent_slot ulong child_scope uchar child_ro uchar child_port uint c1 view uchar * to bool |
| 354 | ps = get aegis_cap_scope parent_slot |
| 355 | if eq ps AEGIS_SCOPE_SYSADMIN |
| 356 | ret true |
| 357 | end |
| 358 | if ne ps child_scope |
| 359 | ret false |
| 360 | end |
| 361 | p1 = new uchar 64 0 |
| 362 | pn1 = aegis_copy_cap_s1 parent_slot p1 |
| 363 | pro = get aegis_cap_ro parent_slot |
| 364 | pport = get aegis_cap_port parent_slot |
| 365 | if eq ps AEGIS_SCOPE_FS |
| 366 | if not aegis_path_within c1 part p1 0 pn1 |
| 367 | ret false |
| 368 | end |
| 369 | if and ne pro 0 eq child_ro 0 |
| 370 | ret false |
| 371 | end |
| 372 | ret true |
| 373 | end |
| 374 | if eq ps AEGIS_SCOPE_STANDING_FETCH |
| 375 | if and eq pn1 1 eq get p1 0 42 |
| 376 | ret true |
| 377 | end |
| 378 | ret aegis_str_eq part p1 0 pn1 c1 |
| 379 | end |
| 380 | if eq ps AEGIS_SCOPE_NET |
| 381 | host_ok = false |
| 382 | if and eq pn1 1 eq get p1 0 42 |
| 383 | host_ok = true |
| 384 | else |
| 385 | host_ok = aegis_str_eq part p1 0 pn1 c1 |
| 386 | end |
| 387 | port_ok = or eq pport 0 eq pport child_port |
| 388 | ret and host_ok port_ok |
| 389 | end |
| 390 | if eq ps AEGIS_SCOPE_DEVICE |
| 391 | if not aegis_str_eq part p1 0 pn1 c1 |
| 392 | ret false |
| 393 | end |
| 394 | if and ne pro 0 eq child_ro 0 |
| 395 | ret false |
| 396 | end |
| 397 | ret true |
| 398 | end |
| 399 | if eq ps AEGIS_SCOPE_WORLD |
| 400 | ret le child_port pport |
| 401 | end |
| 402 | ret false |
| 403 | end |
| 404 | |
| 405 | note ---- Capability Graph Operations ---- |
| 406 | |
| 407 | fn aegis_find_free_cap to long |
| 408 | i = ulong 0 |
| 409 | while lt i AEGIS_MAX_CAPS bound 32 |
| 410 | if eq get aegis_cap_used i 0 |
| 411 | ret long i |
| 412 | end |
| 413 | i = add i 1 |
| 414 | end |
| 415 | ret -1 |
| 416 | end |
| 417 | |
| 418 | fn aegis_store_cap_fields slot ulong holder ulong scope uchar ro uchar port uint exp ulong to void |
| 419 | put aegis_cap_used slot 1 |
| 420 | put aegis_cap_revoked slot 0 |
| 421 | put aegis_cap_holder slot holder |
| 422 | put aegis_cap_scope slot scope |
| 423 | put aegis_cap_ro slot ro |
| 424 | put aegis_cap_port slot port |
| 425 | put aegis_cap_exp slot exp |
| 426 | end |
| 427 | |
| 428 | fn aegis_store_cap_strs slot ulong s1 view uchar * s2 view uchar * to void |
| 429 | n1 = len s1 |
| 430 | if gt n1 AEGIS_MAX_STR |
| 431 | n1 = AEGIS_MAX_STR |
| 432 | end |
| 433 | put aegis_cap_slen slot n1 |
| 434 | b1 = mul slot AEGIS_MAX_STR |
| 435 | i = ulong 0 |
| 436 | while lt i n1 bound 64 |
| 437 | put aegis_cap_str add b1 i get s1 i |
| 438 | i = add i 1 |
| 439 | end |
| 440 | n2 = len s2 |
| 441 | if gt n2 AEGIS_MAX_STR |
| 442 | n2 = AEGIS_MAX_STR |
| 443 | end |
| 444 | put aegis_cap_s2len slot n2 |
| 445 | b2 = mul slot AEGIS_MAX_STR |
| 446 | j = ulong 0 |
| 447 | while lt j n2 bound 64 |
| 448 | put aegis_cap_str2 add b2 j get s2 j |
| 449 | j = add j 1 |
| 450 | end |
| 451 | end |
| 452 | |
| 453 | fn aegis_finalize_cap slot ulong to void |
| 454 | sig = new uchar 32 0 |
| 455 | aegis_cap_compute_sig slot sig |
| 456 | sb = mul slot 32 |
| 457 | ib = mul slot 16 |
| 458 | for i ulong 32 |
| 459 | put aegis_cap_sig add sb i get sig i |
| 460 | end |
| 461 | for k ulong 16 |
| 462 | put aegis_cap_id add ib k get sig k |
| 463 | end |
| 464 | end |
| 465 | |
| 466 | note Issue a root capability token. Returns slot index (0..31) or negative AEGIS_ERR_*. |
| 467 | fn aegis_cap_issue holder ulong scope uchar ro uchar port uint exp ulong to int |
| 468 | s = aegis_find_free_cap |
| 469 | if lt s 0 |
| 470 | ret AEGIS_ERR_CAPACITY |
| 471 | end |
| 472 | slot = ulong s |
| 473 | put aegis_cap_parent slot -1 |
| 474 | put aegis_cap_depth slot 0 |
| 475 | aegis_store_cap_fields slot holder scope ro port exp |
| 476 | put aegis_cap_slen slot 0 |
| 477 | put aegis_cap_s2len slot 0 |
| 478 | aegis_finalize_cap slot |
| 479 | ret int slot |
| 480 | end |
| 481 | |
| 482 | fn aegis_cap_set_strings slot ulong s1 view uchar * s2 view uchar * to void |
| 483 | aegis_store_cap_strs slot s1 s2 |
| 484 | aegis_finalize_cap slot |
| 485 | end |
| 486 | |
| 487 | fn aegis_cap_validate slot ulong now_utc ulong to int |
| 488 | if ge slot AEGIS_MAX_CAPS |
| 489 | ret AEGIS_ERR_NOT_FOUND |
| 490 | end |
| 491 | if eq get aegis_cap_used slot 0 |
| 492 | ret AEGIS_ERR_NOT_FOUND |
| 493 | end |
| 494 | if ne get aegis_cap_revoked slot 0 |
| 495 | ret AEGIS_ERR_REVOKED |
| 496 | end |
| 497 | exp = get aegis_cap_exp slot |
| 498 | if and ne exp 0 gt now_utc exp |
| 499 | ret AEGIS_ERR_EXPIRED |
| 500 | end |
| 501 | want = new uchar 32 0 |
| 502 | have = new uchar 32 0 |
| 503 | aegis_cap_compute_sig slot want |
| 504 | sb = mul slot 32 |
| 505 | for i ulong 32 |
| 506 | put have i get aegis_cap_sig add sb i |
| 507 | end |
| 508 | if not aegis_eq32 want have |
| 509 | ret AEGIS_ERR_BAD_SIG |
| 510 | end |
| 511 | ret AEGIS_OK |
| 512 | end |
| 513 | |
| 514 | note Delegate an attenuated capability from parent_slot to new_holder. |
| 515 | note Call aegis_set_tmp_strs(c1, c2) before calling, or use aegis_cap_delegate_strs. |
| 516 | fn aegis_set_tmp_strs s1 view uchar * s2 view uchar * to void |
| 517 | for z ulong 64 |
| 518 | put aegis_tmp_s1 z 0 |
| 519 | put aegis_tmp_s2 z 0 |
| 520 | end |
| 521 | n1 = len s1 |
| 522 | if gt n1 64 |
| 523 | n1 = ulong 64 |
| 524 | end |
| 525 | i = ulong 0 |
| 526 | while lt i n1 bound 64 |
| 527 | put aegis_tmp_s1 i get s1 i |
| 528 | i = add i 1 |
| 529 | end |
| 530 | n2 = len s2 |
| 531 | if gt n2 64 |
| 532 | n2 = ulong 64 |
| 533 | end |
| 534 | j = ulong 0 |
| 535 | while lt j n2 bound 64 |
| 536 | put aegis_tmp_s2 j get s2 j |
| 537 | j = add j 1 |
| 538 | end |
| 539 | end |
| 540 | |
| 541 | fn aegis_cap_delegate parent_slot ulong new_holder ulong scope uchar ro uchar port uint exp ulong to int |
| 542 | vrc = aegis_cap_validate parent_slot 0 |
| 543 | if ne vrc AEGIS_OK |
| 544 | ret vrc |
| 545 | end |
| 546 | pdepth = get aegis_cap_depth parent_slot |
| 547 | if ge pdepth AEGIS_MAX_DELEG_DEPTH |
| 548 | ret AEGIS_ERR_DEPTH |
| 549 | end |
| 550 | note Expiry cannot exceed parent expiry when parent has one |
| 551 | pexp = get aegis_cap_exp parent_slot |
| 552 | if ne pexp 0 |
| 553 | if or eq exp 0 gt exp pexp |
| 554 | ret AEGIS_ERR_SCOPE |
| 555 | end |
| 556 | end |
| 557 | s = aegis_find_free_cap |
| 558 | if lt s 0 |
| 559 | ret AEGIS_ERR_CAPACITY |
| 560 | end |
| 561 | slot = ulong s |
| 562 | put aegis_cap_parent slot int parent_slot |
| 563 | put aegis_cap_depth slot add pdepth 1 |
| 564 | aegis_store_cap_fields slot new_holder scope ro port exp |
| 565 | put aegis_cap_slen slot 0 |
| 566 | put aegis_cap_s2len slot 0 |
| 567 | aegis_finalize_cap slot |
| 568 | ret int slot |
| 569 | end |
| 570 | |
| 571 | note Delegate with string scope (FS path, host, device) and strict attenuation check. |
| 572 | fn aegis_cap_delegate_scoped parent_slot ulong new_holder ulong scope uchar ro uchar c1 view uchar * to int |
| 573 | vrc = aegis_cap_validate parent_slot 0 |
| 574 | if ne vrc AEGIS_OK |
| 575 | ret vrc |
| 576 | end |
| 577 | pdepth = get aegis_cap_depth parent_slot |
| 578 | if ge pdepth AEGIS_MAX_DELEG_DEPTH |
| 579 | ret AEGIS_ERR_DEPTH |
| 580 | end |
| 581 | if not aegis_scope_contains parent_slot scope ro 0 c1 |
| 582 | ret AEGIS_ERR_SCOPE |
| 583 | end |
| 584 | s = aegis_find_free_cap |
| 585 | if lt s 0 |
| 586 | ret AEGIS_ERR_CAPACITY |
| 587 | end |
| 588 | slot = ulong s |
| 589 | put aegis_cap_parent slot int parent_slot |
| 590 | put aegis_cap_depth slot add pdepth 1 |
| 591 | aegis_store_cap_fields slot new_holder scope ro 0 get aegis_cap_exp parent_slot |
| 592 | empty = new uchar 1 0 |
| 593 | aegis_store_cap_strs slot c1 part empty 0 0 |
| 594 | aegis_finalize_cap slot |
| 595 | ret int slot |
| 596 | end |
| 597 | |
| 598 | note Cascading subtree revocation: revoking slot revokes all descendants. |
| 599 | fn aegis_cap_revoke slot ulong to int |
| 600 | if ge slot AEGIS_MAX_CAPS |
| 601 | ret AEGIS_ERR_NOT_FOUND |
| 602 | end |
| 603 | if eq get aegis_cap_used slot 0 |
| 604 | ret AEGIS_ERR_NOT_FOUND |
| 605 | end |
| 606 | put aegis_cap_revoked slot 1 |
| 607 | note Fixed-point pass over max 32 tokens (depth <= 4, so 5 passes always converge) |
| 608 | pass = ulong 0 |
| 609 | while lt pass 6 bound 6 |
| 610 | i = ulong 0 |
| 611 | while lt i AEGIS_MAX_CAPS bound 32 |
| 612 | if and ne get aegis_cap_used i 0 eq get aegis_cap_revoked i 0 |
| 613 | p = get aegis_cap_parent i |
| 614 | if ge p 0 |
| 615 | if ne get aegis_cap_revoked ulong p 0 |
| 616 | put aegis_cap_revoked i 1 |
| 617 | end |
| 618 | end |
| 619 | end |
| 620 | i = add i 1 |
| 621 | end |
| 622 | pass = add pass 1 |
| 623 | end |
| 624 | ret AEGIS_OK |
| 625 | end |
| 626 | |
| 627 | note ---- Effect Intent ID & Operator Grant Cryptography ---- |
| 628 | |
| 629 | note Compute deterministic 16-byte EffectIntent ID with length-prefixed parameter boundaries. |
| 630 | fn aegis_compute_intent_id agent_id ulong cap_slot ulong act view uchar * arg view uchar * to void |
| 631 | c = new ulong 40 0 |
| 632 | dig = new uchar 32 0 |
| 633 | cid = new uchar 16 0 |
| 634 | if lt cap_slot AEGIS_MAX_CAPS |
| 635 | cb = mul cap_slot 16 |
| 636 | for i ulong 16 |
| 637 | put cid i get aegis_cap_id add cb i |
| 638 | end |
| 639 | end |
| 640 | sha256_init c |
| 641 | sha256_update c aegis_dom_intent |
| 642 | aegis_sha_u64 c agent_id |
| 643 | aegis_sha_field c act |
| 644 | sha256_update c cid |
| 645 | aegis_sha_field c arg |
| 646 | sha256_final c dig |
| 647 | for k ulong 16 |
| 648 | put aegis_cur_iid k get dig k |
| 649 | end |
| 650 | end |
| 651 | |
| 652 | note Compute 16-byte intent ID for an explicit (key, value) pair to prove boundary separation. |
| 653 | fn aegis_intent_hash_kv agent_id ulong pkey view uchar * pval view uchar * to void |
| 654 | c = new ulong 40 0 |
| 655 | dig = new uchar 32 0 |
| 656 | sha256_init c |
| 657 | sha256_update c aegis_dom_intent |
| 658 | aegis_sha_u64 c agent_id |
| 659 | aegis_sha_field c pkey |
| 660 | aegis_sha_field c pval |
| 661 | sha256_final c dig |
| 662 | for k ulong 16 |
| 663 | put aegis_cur_iid k get dig k |
| 664 | end |
| 665 | end |
| 666 | |
| 667 | fn aegis_grant_compute_sig iid view uchar 16 op_id view uchar * ts ulong sec view uchar 32 out_sig edit uchar 32 to void |
| 668 | c = new ulong 40 0 |
| 669 | sha256_init c |
| 670 | sha256_update c aegis_dom_grant |
| 671 | sha256_update c sec |
| 672 | sha256_update c iid |
| 673 | aegis_sha_field c op_id |
| 674 | aegis_sha_u64 c ts |
| 675 | sha256_final c out_sig |
| 676 | end |
| 677 | |
| 678 | fn aegis_grant_clear to void |
| 679 | put aegis_cur_grant_meta 0 0 |
| 680 | end |
| 681 | |
| 682 | note Issue an operator grant for the current intent ID (aegis_cur_iid) using the given secret. |
| 683 | fn aegis_grant_issue_with_secret op_id view uchar * ts ulong sec view uchar 32 to void |
| 684 | iid = new uchar 16 0 |
| 685 | sig = new uchar 32 0 |
| 686 | for i ulong 16 |
| 687 | put iid i get aegis_cur_iid i |
| 688 | put aegis_cur_grant_iid i get aegis_cur_iid i |
| 689 | end |
| 690 | aegis_grant_compute_sig iid op_id ts sec sig |
| 691 | for j ulong 32 |
| 692 | put aegis_cur_grant_sig j get sig j |
| 693 | end |
| 694 | n = len op_id |
| 695 | if gt n 64 |
| 696 | n = ulong 64 |
| 697 | end |
| 698 | k = ulong 0 |
| 699 | while lt k n bound 64 |
| 700 | put aegis_cur_grant_op k get op_id k |
| 701 | k = add k 1 |
| 702 | end |
| 703 | put aegis_cur_grant_meta 0 1 |
| 704 | put aegis_cur_grant_meta 1 ts |
| 705 | put aegis_cur_grant_meta 2 n |
| 706 | end |
| 707 | |
| 708 | fn aegis_grant_verify_current to int |
| 709 | if eq get aegis_cur_grant_meta 0 0 |
| 710 | ret AEGIS_ERR_MISSING_GRANT |
| 711 | end |
| 712 | iid = new uchar 16 0 |
| 713 | giid = new uchar 16 0 |
| 714 | for i ulong 16 |
| 715 | put iid i get aegis_cur_iid i |
| 716 | put giid i get aegis_cur_grant_iid i |
| 717 | end |
| 718 | if not aegis_eq16 iid giid |
| 719 | ret AEGIS_ERR_GRANT_INTENT |
| 720 | end |
| 721 | op_buf = new uchar 64 0 |
| 722 | oplen = get aegis_cur_grant_meta 2 |
| 723 | k = ulong 0 |
| 724 | while lt k oplen bound 64 |
| 725 | put op_buf k get aegis_cur_grant_op k |
| 726 | k = add k 1 |
| 727 | end |
| 728 | sec = new uchar 32 0 |
| 729 | have = new uchar 32 0 |
| 730 | want = new uchar 32 0 |
| 731 | for s ulong 32 |
| 732 | put sec s get aegis_op_secret s |
| 733 | put have s get aegis_cur_grant_sig s |
| 734 | end |
| 735 | aegis_grant_compute_sig giid part op_buf 0 oplen get aegis_cur_grant_meta 1 sec want |
| 736 | if not aegis_eq32 want have |
| 737 | ret AEGIS_ERR_GRANT_SIG |
| 738 | end |
| 739 | ret AEGIS_OK |
| 740 | end |
| 741 | |
| 742 | note ---- J-Space Copy-on-Write Worlds ---- |
| 743 | |
| 744 | fn aegis_world_create wid ulong to int |
| 745 | i = ulong 0 |
| 746 | while lt i AEGIS_MAX_WORLDS bound 8 |
| 747 | if eq get aegis_w_status i AEGIS_WORLD_NONE |
| 748 | put aegis_w_status i AEGIS_WORLD_ACTIVE |
| 749 | put aegis_w_id i wid |
| 750 | put aegis_w_deltas i 0 |
| 751 | put aegis_w_writes i 0 |
| 752 | put aegis_w_deletes i 0 |
| 753 | ret int i |
| 754 | end |
| 755 | i = add i 1 |
| 756 | end |
| 757 | ret AEGIS_ERR_CAPACITY |
| 758 | end |
| 759 | |
| 760 | fn aegis_world_find wid ulong to int |
| 761 | i = ulong 0 |
| 762 | while lt i AEGIS_MAX_WORLDS bound 8 |
| 763 | if and ne get aegis_w_status i AEGIS_WORLD_NONE eq get aegis_w_id i wid |
| 764 | ret int i |
| 765 | end |
| 766 | i = add i 1 |
| 767 | end |
| 768 | ret AEGIS_ERR_NOT_FOUND |
| 769 | end |
| 770 | |
| 771 | fn aegis_world_rollback wid ulong to int |
| 772 | idx = aegis_world_find wid |
| 773 | if lt idx 0 |
| 774 | ret AEGIS_ERR_NOT_FOUND |
| 775 | end |
| 776 | u = ulong idx |
| 777 | put aegis_w_deltas u 0 |
| 778 | put aegis_w_writes u 0 |
| 779 | put aegis_w_deletes u 0 |
| 780 | put aegis_w_status u AEGIS_WORLD_ROLLEDBACK |
| 781 | ret AEGIS_OK |
| 782 | end |
| 783 | |
| 784 | fn aegis_world_commit wid ulong to int |
| 785 | idx = aegis_world_find wid |
| 786 | if lt idx 0 |
| 787 | ret AEGIS_ERR_NOT_FOUND |
| 788 | end |
| 789 | u = ulong idx |
| 790 | if ne get aegis_w_status u AEGIS_WORLD_ACTIVE |
| 791 | ret AEGIS_ERR_WORLD_STATE |
| 792 | end |
| 793 | put aegis_w_status u AEGIS_WORLD_COMMITTED |
| 794 | ret AEGIS_OK |
| 795 | end |
| 796 | |
| 797 | note ---- Policy Evaluator & Effect Broker ---- |
| 798 | |
| 799 | fn aegis_is_high_impact act view uchar * to bool |
| 800 | if or aegis_str_eq act aegis_act_disk_format aegis_str_eq act aegis_act_fs_delete |
| 801 | ret true |
| 802 | end |
| 803 | if or aegis_str_eq act aegis_act_fs_write aegis_str_eq act aegis_act_git_pub |
| 804 | ret true |
| 805 | end |
| 806 | if or aegis_str_eq act aegis_act_code_pub aegis_str_eq act aegis_act_kern_rep |
| 807 | ret true |
| 808 | end |
| 809 | if or aegis_str_eq act aegis_act_sys_inst aegis_str_eq act aegis_act_cred_mod |
| 810 | ret true |
| 811 | end |
| 812 | ret aegis_str_eq act aegis_act_fin_spend |
| 813 | end |
| 814 | |
| 815 | fn aegis_scope_authorizes slot ulong act view uchar * arg view uchar * port uint to bool |
| 816 | sc = get aegis_cap_scope slot |
| 817 | if eq sc AEGIS_SCOPE_SYSADMIN |
| 818 | ret true |
| 819 | end |
| 820 | p1 = new uchar 64 0 |
| 821 | pn1 = aegis_copy_cap_s1 slot p1 |
| 822 | ro = get aegis_cap_ro slot |
| 823 | if eq sc AEGIS_SCOPE_FS |
| 824 | if aegis_str_eq act aegis_act_fs_read |
| 825 | ret aegis_path_within arg part p1 0 pn1 |
| 826 | end |
| 827 | if or aegis_str_eq act aegis_act_fs_write aegis_str_eq act aegis_act_fs_delete |
| 828 | if ne ro 0 |
| 829 | ret false |
| 830 | end |
| 831 | ret aegis_path_within arg part p1 0 pn1 |
| 832 | end |
| 833 | ret false |
| 834 | end |
| 835 | if eq sc AEGIS_SCOPE_STANDING_FETCH |
| 836 | if or aegis_str_eq act aegis_act_net_fetch aegis_str_eq act aegis_act_net_read |
| 837 | if and eq pn1 1 eq get p1 0 42 |
| 838 | ret true |
| 839 | end |
| 840 | ret aegis_str_eq part p1 0 pn1 arg |
| 841 | end |
| 842 | ret false |
| 843 | end |
| 844 | if eq sc AEGIS_SCOPE_NET |
| 845 | if or aegis_str_eq act aegis_act_net_connect aegis_str_eq act aegis_act_net_send |
| 846 | host_ok = false |
| 847 | if and eq pn1 1 eq get p1 0 42 |
| 848 | host_ok = true |
| 849 | else |
| 850 | host_ok = aegis_str_eq part p1 0 pn1 arg |
| 851 | end |
| 852 | pport = get aegis_cap_port slot |
| 853 | port_ok = or eq pport 0 eq pport port |
| 854 | ret and host_ok port_ok |
| 855 | end |
| 856 | ret false |
| 857 | end |
| 858 | if eq sc AEGIS_SCOPE_DEVICE |
| 859 | if aegis_str_starts act aegis_pfx_device |
| 860 | if not aegis_str_eq part p1 0 pn1 arg |
| 861 | ret false |
| 862 | end |
| 863 | if and aegis_str_eq act aegis_act_dev_write ne ro 0 |
| 864 | ret false |
| 865 | end |
| 866 | ret true |
| 867 | end |
| 868 | ret false |
| 869 | end |
| 870 | if eq sc AEGIS_SCOPE_WORLD |
| 871 | ret aegis_str_starts act aegis_pfx_world |
| 872 | end |
| 873 | ret false |
| 874 | end |
| 875 | |
| 876 | fn aegis_record_audit dec uchar rc int executed uchar ts ulong to void |
| 877 | cnt = get aegis_audit_cnt 0 |
| 878 | if lt cnt AEGIS_MAX_AUDIT |
| 879 | put aegis_audit_dec cnt dec |
| 880 | put aegis_audit_rc cnt rc |
| 881 | put aegis_audit_exec cnt executed |
| 882 | put aegis_audit_ts cnt ts |
| 883 | ib = mul cnt 16 |
| 884 | for k ulong 16 |
| 885 | put aegis_audit_iid add ib k get aegis_cur_iid k |
| 886 | end |
| 887 | put aegis_audit_cnt 0 add cnt 1 |
| 888 | end |
| 889 | put aegis_last_dec 0 dec |
| 890 | put aegis_last_rc 0 rc |
| 891 | end |
| 892 | |
| 893 | fn aegis_try_world_delta act view uchar * to bool |
| 894 | world_id = get aegis_dctx 0 |
| 895 | if or eq world_id 0 eq get aegis_dctx 1 0 |
| 896 | ret false |
| 897 | end |
| 898 | is_wfs = or aegis_str_eq act aegis_act_fs_write aegis_str_eq act aegis_act_fs_delete |
| 899 | if not is_wfs |
| 900 | ret false |
| 901 | end |
| 902 | widx = aegis_world_find world_id |
| 903 | if lt widx 0 |
| 904 | ret false |
| 905 | end |
| 906 | wu = ulong widx |
| 907 | if ne get aegis_w_status wu AEGIS_WORLD_ACTIVE |
| 908 | ret false |
| 909 | end |
| 910 | put aegis_w_deltas wu add get aegis_w_deltas wu 1 |
| 911 | if aegis_str_eq act aegis_act_fs_write |
| 912 | put aegis_w_writes wu add get aegis_w_writes wu 1 |
| 913 | else |
| 914 | put aegis_w_deletes wu add get aegis_w_deletes wu 1 |
| 915 | end |
| 916 | ret true |
| 917 | end |
| 918 | |
| 919 | note Evaluate and dispatch an EffectIntent through the AEGIS Broker. |
| 920 | note Uses aegis_dctx: [0]=world_id, [1]=is_reversible, [2]=now_utc, [3]=net_port. |
| 921 | note Returns AEGIS_OK if approved & executed, else negative AEGIS_ERR_*. |
| 922 | fn aegis_broker_dispatch agent_id ulong cap_slot ulong act view uchar * arg view uchar * to int |
| 923 | now_utc = get aegis_dctx 2 |
| 924 | aegis_compute_intent_id agent_id cap_slot act arg |
| 925 | note Step 1: Validate capability token |
| 926 | vrc = aegis_cap_validate cap_slot now_utc |
| 927 | if ne vrc AEGIS_OK |
| 928 | aegis_record_audit AEGIS_DEC_REJECTED vrc 0 now_utc |
| 929 | ret vrc |
| 930 | end |
| 931 | note Step 2: Validate holder == proposing agent |
| 932 | if ne get aegis_cap_holder cap_slot agent_id |
| 933 | aegis_record_audit AEGIS_DEC_REJECTED AEGIS_ERR_HOLDER 0 now_utc |
| 934 | ret AEGIS_ERR_HOLDER |
| 935 | end |
| 936 | note Step 3: Scope authorizes action + target arg |
| 937 | if not aegis_scope_authorizes cap_slot act arg uint get aegis_dctx 3 |
| 938 | aegis_record_audit AEGIS_DEC_REJECTED AEGIS_ERR_SCOPE 0 now_utc |
| 939 | ret AEGIS_ERR_SCOPE |
| 940 | end |
| 941 | note Step 4: Active broker-owned World delta fast-path for reversible fs.write/fs.delete |
| 942 | if aegis_try_world_delta act |
| 943 | aegis_record_audit AEGIS_DEC_APPROVED AEGIS_OK 1 now_utc |
| 944 | ret AEGIS_OK |
| 945 | end |
| 946 | note Step 5: StandingFetch / read-only autonomous path when not high-impact |
| 947 | is_stand = eq get aegis_cap_scope cap_slot AEGIS_SCOPE_STANDING_FETCH |
| 948 | is_ro_act = or aegis_str_eq act aegis_act_fs_read aegis_str_eq act aegis_act_hw_inspect |
| 949 | if and or is_stand is_ro_act not aegis_is_high_impact act |
| 950 | aegis_record_audit AEGIS_DEC_APPROVED AEGIS_OK 1 now_utc |
| 951 | ret AEGIS_OK |
| 952 | end |
| 953 | note Step 6: Irreversible / high-impact boundary requires verified OperatorGrant |
| 954 | if ne get aegis_cur_grant_meta 3 0 |
| 955 | note Mutant: skip operator grant verification on high-impact boundary |
| 956 | aegis_record_audit AEGIS_DEC_APPROVED AEGIS_OK 1 now_utc |
| 957 | ret AEGIS_OK |
| 958 | end |
| 959 | grc = aegis_grant_verify_current |
| 960 | if eq grc AEGIS_ERR_MISSING_GRANT |
| 961 | aegis_record_audit AEGIS_DEC_REQUIRES_OP AEGIS_ERR_MISSING_GRANT 0 now_utc |
| 962 | ret AEGIS_ERR_MISSING_GRANT |
| 963 | end |
| 964 | if ne grc AEGIS_OK |
| 965 | aegis_record_audit AEGIS_DEC_REJECTED grc 0 now_utc |
| 966 | ret grc |
| 967 | end |
| 968 | aegis_record_audit AEGIS_DEC_APPROVED AEGIS_OK 1 now_utc |
| 969 | ret AEGIS_OK |
| 970 | end |