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 / src / visor / visor_effect_request.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * visor_effect_request.c -- Omega Visor V1, lane 7. See visor_effect_request.h. |
| 19 | * |
| 20 | * THE VISOR CAN ASK. THE VISOR CANNOT GRANT. |
| 21 | * |
| 22 | * Pure over OmegaGraph. References no runtime (rx_*), AIENOS (aienos_cap_*) |
| 23 | * or publish-gate (rc_*) symbol; tests/visor/check_authority_link.sh enforces |
| 24 | * that on the object file. The only write to `authorized` in this file is the |
| 25 | * zero-initialisation in request_reset(); the field is never set true. |
| 26 | */ |
| 27 | #include "visor_effect_request.h" |
| 28 | |
| 29 | #include <inttypes.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | |
| 33 | #include "omega_canonical.h" |
| 34 | #include "omega_core.h" |
| 35 | #include "omega_validate.h" |
| 36 | #include "sha256.h" |
| 37 | |
| 38 | static void request_reset(VisorEffectRequest *out) { |
| 39 | memset(out, 0, sizeof(*out)); |
| 40 | out->authorized = false; |
| 41 | snprintf(out->status, sizeof out->status, "%s", VISOR_EFFECT_STATUS_UNAUTHORIZED); |
| 42 | snprintf(out->route, sizeof out->route, "%s", VISOR_EFFECT_ROUTE); |
| 43 | } |
| 44 | |
| 45 | int visor_effect_request_build(const OmegaGraph *g, const SemanticId *effect_object_id, |
| 46 | VisorEffectRequest *out) { |
| 47 | if (!out) return -1; |
| 48 | request_reset(out); |
| 49 | if (!g || !effect_object_id) return -1; |
| 50 | |
| 51 | const OmegaObject *obj = omega_graph_find_object_const(g, effect_object_id); |
| 52 | if (!obj || !obj->has_id || obj->kind != KIND_EFFECT) return -1; |
| 53 | |
| 54 | /* Existing structural validation first (strict v2 effect decode). */ |
| 55 | char err[160]; |
| 56 | if (omega_validate_object(g, obj, err, sizeof err) != 0) return -1; |
| 57 | if (obj->payload_len != OMEGA_EFFECT_PAYLOAD_LEN) return -1; |
| 58 | |
| 59 | EffectPayload eff; |
| 60 | if (omega_effect_read(obj, &eff) != OMEGA_EFFECT_OK) return -1; |
| 61 | if (eff.param_len > VISOR_EFFECT_PARAM_MAX) return -1; |
| 62 | |
| 63 | /* Digest = sha256(canonical encoding). Computed into a local buffer so the |
| 64 | * graph is never written. It must equal the stored SemanticId: an object |
| 65 | * edited after it was named (resource, operation, capability fields or |
| 66 | * parameters changed) no longer matches and is refused. */ |
| 67 | uint8_t canon[4096]; |
| 68 | size_t canon_len = 0; |
| 69 | if (omega_canonical_encode(obj, canon, sizeof canon, &canon_len) != 0) return -1; |
| 70 | uint8_t digest[32]; |
| 71 | sha256_hash(canon, canon_len, digest); |
| 72 | if (memcmp(digest, obj->id.bytes, sizeof digest) != 0) return -1; |
| 73 | |
| 74 | out->effect_id = obj->id; |
| 75 | out->resource_class = eff.resource_class; |
| 76 | out->operation_code = eff.operation_code; |
| 77 | out->capability_slot = eff.capability_slot; |
| 78 | out->capability_generation = eff.capability_generation; |
| 79 | out->capability_ref = eff.capability_ref; |
| 80 | out->param_len = eff.param_len; |
| 81 | memcpy(out->param_bytes, eff.param_bytes, eff.param_len); |
| 82 | memcpy(out->request_digest, digest, sizeof digest); |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* ---- classification ---- */ |
| 87 | |
| 88 | static bool id_is_zero(const SemanticId *id) { |
| 89 | for (size_t i = 0; i < OMEGA_ID_BYTES; i++) |
| 90 | if (id->bytes[i]) return false; |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | static int index_of(const OmegaGraph *g, const SemanticId *id) { |
| 95 | const OmegaObject *o = omega_graph_find_object_const(g, id); |
| 96 | if (!o) return -1; |
| 97 | return (int)(o - g->objects); |
| 98 | } |
| 99 | |
| 100 | typedef struct { |
| 101 | const OmegaGraph *g; |
| 102 | uint16_t stack[OMEGA_MAX_GRAPH_OBJECTS]; |
| 103 | uint16_t depth; |
| 104 | uint8_t seen[OMEGA_MAX_GRAPH_OBJECTS]; |
| 105 | bool missing; |
| 106 | } Walk; |
| 107 | |
| 108 | static void push_ref(Walk *w, const SemanticId *id) { |
| 109 | if (id_is_zero(id)) return; /* zero id = field unused */ |
| 110 | int i = index_of(w->g, id); |
| 111 | if (i < 0) { w->missing = true; return; } |
| 112 | if (w->seen[i]) return; |
| 113 | w->seen[i] = 1; |
| 114 | w->stack[w->depth++] = (uint16_t)i; |
| 115 | } |
| 116 | |
| 117 | static bool object_needs_authority(const OmegaObject *o) { |
| 118 | if (o->kind == KIND_EFFECT) return true; |
| 119 | if (o->kind == KIND_TYPE && o->payload_len >= sizeof(TypePayload)) { |
| 120 | TypePayload tp; |
| 121 | memcpy(&tp, o->payload, sizeof tp); |
| 122 | if (tp.tag == TYPE_CAPABILITY_REF || tp.tag == TYPE_EFFECT_INTENT_REF || |
| 123 | tp.tag == TYPE_EFFECT_RECEIPT_REF) |
| 124 | return true; |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | static void push_payload_refs(Walk *w, const OmegaObject *o) { |
| 130 | switch (o->kind) { |
| 131 | case KIND_TYPE: |
| 132 | if (o->payload_len >= sizeof(TypePayload)) { |
| 133 | TypePayload tp; |
| 134 | memcpy(&tp, o->payload, sizeof tp); |
| 135 | push_ref(w, &tp.elem_type); |
| 136 | } |
| 137 | break; |
| 138 | case KIND_VALUE: |
| 139 | if (o->payload_len >= sizeof(ValuePayload)) { |
| 140 | ValuePayload vp; |
| 141 | memcpy(&vp, o->payload, sizeof vp); |
| 142 | push_ref(w, &vp.type_id); |
| 143 | } |
| 144 | break; |
| 145 | case KIND_OPERATION: |
| 146 | /* Same discrimination as omega_validate_object. */ |
| 147 | if (o->payload_len >= sizeof(OperationPayload)) { |
| 148 | OperationPayload op; |
| 149 | memcpy(&op, o->payload, sizeof op); |
| 150 | push_ref(w, &op.type_id); |
| 151 | for (int i = 0; i < 4; i++) push_ref(w, &op.input_types[i]); |
| 152 | push_ref(w, &op.output_type); |
| 153 | } else if (o->payload_len >= sizeof(ApplyPayload)) { |
| 154 | ApplyPayload ap; |
| 155 | memcpy(&ap, o->payload, sizeof ap); |
| 156 | push_ref(w, &ap.op_id); |
| 157 | uint8_t n = ap.operand_count > 4 ? 4 : ap.operand_count; |
| 158 | for (uint8_t i = 0; i < n; i++) push_ref(w, &ap.operands[i]); |
| 159 | } |
| 160 | break; |
| 161 | case KIND_EFFECT: { |
| 162 | EffectPayload ep; |
| 163 | if (omega_effect_read(o, &ep) == OMEGA_EFFECT_OK) push_ref(w, &ep.capability_ref); |
| 164 | break; |
| 165 | } |
| 166 | default: |
| 167 | break; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | int visor_effect_request_classify(const OmegaGraph *g, const SemanticId *id, |
| 172 | bool *requires_authority) { |
| 173 | if (!requires_authority) return -1; |
| 174 | *requires_authority = true; /* fail closed until proven otherwise */ |
| 175 | if (!g || !id) return -1; |
| 176 | if (g->object_count > OMEGA_MAX_GRAPH_OBJECTS) return -1; |
| 177 | |
| 178 | Walk w; |
| 179 | memset(&w, 0, sizeof w); |
| 180 | w.g = g; |
| 181 | int root = index_of(g, id); |
| 182 | if (root < 0) return -1; |
| 183 | w.seen[root] = 1; |
| 184 | w.stack[w.depth++] = (uint16_t)root; |
| 185 | |
| 186 | bool needs = false; |
| 187 | while (w.depth > 0) { |
| 188 | const OmegaObject *o = &g->objects[w.stack[--w.depth]]; |
| 189 | if (object_needs_authority(o)) needs = true; |
| 190 | uint16_t nrel = o->rel_count > OMEGA_MAX_RELATIONS ? OMEGA_MAX_RELATIONS : o->rel_count; |
| 191 | for (uint16_t r = 0; r < nrel; r++) push_ref(&w, &o->relations[r].target_id); |
| 192 | push_payload_refs(&w, o); |
| 193 | } |
| 194 | if (w.missing) return -1; |
| 195 | *requires_authority = needs; |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | /* ---- formatting ---- */ |
| 200 | |
| 201 | static int hex_bytes(const uint8_t *b, size_t n, char *out, size_t cap) { |
| 202 | static const char digits[] = "0123456789abcdef"; |
| 203 | if (cap < n * 2 + 1) return -1; |
| 204 | for (size_t i = 0; i < n; i++) { |
| 205 | out[2 * i] = digits[b[i] >> 4]; |
| 206 | out[2 * i + 1] = digits[b[i] & 15]; |
| 207 | } |
| 208 | out[n * 2] = '\0'; |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | typedef struct { |
| 213 | char effect[65], cap[65], digest[65], params[VISOR_EFFECT_PARAM_MAX * 2 + 1]; |
| 214 | } Hexes; |
| 215 | |
| 216 | static int make_hexes(const VisorEffectRequest *r, Hexes *h) { |
| 217 | if (r->param_len > VISOR_EFFECT_PARAM_MAX) return -1; |
| 218 | if (hex_bytes(r->effect_id.bytes, 32, h->effect, sizeof h->effect)) return -1; |
| 219 | if (hex_bytes(r->capability_ref.bytes, 32, h->cap, sizeof h->cap)) return -1; |
| 220 | if (hex_bytes(r->request_digest, 32, h->digest, sizeof h->digest)) return -1; |
| 221 | if (hex_bytes(r->param_bytes, r->param_len, h->params, sizeof h->params)) return -1; |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* The printed authorization state is a constant. Whatever the struct holds, |
| 226 | * the Visor never renders a request as authorized. */ |
| 227 | int visor_effect_request_format_text(const VisorEffectRequest *r, char *out, size_t n) { |
| 228 | if (!r || !out || n == 0) return -1; |
| 229 | Hexes h; |
| 230 | if (make_hexes(r, &h)) return -1; |
| 231 | int k = snprintf(out, n, |
| 232 | "effect request (UNAUTHORIZED_REQUEST)\n" |
| 233 | " effect: sha256:%s\n" |
| 234 | " resource: class %u\n" |
| 235 | " operation: code %u\n" |
| 236 | " capability: slot %u generation %" PRIu64 " ref sha256:%s (not validated by the Visor)\n" |
| 237 | " params: %u bytes %s\n" |
| 238 | " digest: sha256:%s\n" |
| 239 | " authorized: false\n" |
| 240 | " route: %s\n", |
| 241 | h.effect, (unsigned)r->resource_class, (unsigned)r->operation_code, |
| 242 | (unsigned)r->capability_slot, r->capability_generation, h.cap, |
| 243 | (unsigned)r->param_len, h.params, h.digest, VISOR_EFFECT_ROUTE); |
| 244 | if (k < 0 || (size_t)k >= n) return -1; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int visor_effect_request_format_json(const VisorEffectRequest *r, char *out, size_t n) { |
| 249 | if (!r || !out || n == 0) return -1; |
| 250 | Hexes h; |
| 251 | if (make_hexes(r, &h)) return -1; |
| 252 | int k = snprintf(out, n, |
| 253 | "{\"effect_id\":\"sha256:%s\",\"resource_class\":%u,\"operation_code\":%u," |
| 254 | "\"capability_slot\":%u,\"capability_generation\":%" PRIu64 "," |
| 255 | "\"capability_ref\":\"sha256:%s\",\"param_len\":%u,\"param_bytes\":\"%s\"," |
| 256 | "\"request_digest\":\"sha256:%s\",\"authorized\":false," |
| 257 | "\"status\":\"%s\",\"route\":\"%s\"}", |
| 258 | h.effect, (unsigned)r->resource_class, (unsigned)r->operation_code, |
| 259 | (unsigned)r->capability_slot, r->capability_generation, h.cap, |
| 260 | (unsigned)r->param_len, h.params, h.digest, |
| 261 | VISOR_EFFECT_STATUS_UNAUTHORIZED, VISOR_EFFECT_ROUTE); |
| 262 | if (k < 0 || (size_t)k >= n) return -1; |
| 263 | return 0; |
| 264 | } |