Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/omega_resolve.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / omega_resolve.c` 767 lines ยท 38055 bytes Raw Blame History 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 /* omega_resolve.c -- see omega_resolve.h. 18 * Lines tagged VC1R: are the ones `make test-resolve` deletes or weakens, one at a time 19 * (the mutation proof). */ 20 #include "omega_resolve.h" 21 22 #include 23 #include 24 #include 25 #include 26 #include 27 28 #include "omega_genesis.h" 29 #include "omega_program_ir.h" 30 #include "omega_receipt.h" 31 #include "omega_vcstore_priv.h" 32 #include "sha256.h" 33 34 /* the shared codes must keep the store's numbers */ 35 #define SAME_CODE(n) ((int)OMEGA_RES_##n == (int)OMEGA_VCS_##n) 36 _Static_assert(SAME_CODE(UNVERIFIED_DEPENDENCY) && SAME_CODE(MISSING_RECEIPT) && SAME_CODE(RECEIPT_HASH_MISMATCH) && 37 SAME_CODE(DEPENDENCY_NOT_PINNED) && SAME_CODE(DEPENDENCY_CYCLE) && SAME_CODE(STALE_RECEIPT) && 38 SAME_CODE(UNDECLARED_IMPORT) && SAME_CODE(TAINTED_ARTIFACT) && SAME_CODE(UNKNOWN_VERIFIER_PROFILE), 39 "resolve codes must match the store reserved codes"); 40 41 #define MAX_DEPTH 128 42 #define MAX_FILE (4u * 1024u * 1024u) 43 44 const char *omega_resolve_code_name(int code) 45 { 46 switch (code) { 47 case OMEGA_RES_OK: return "OK"; 48 case OMEGA_RES_UNVERIFIED_DEPENDENCY: return "UNVERIFIED_DEPENDENCY"; 49 case OMEGA_RES_MISSING_RECEIPT: return "MISSING_RECEIPT"; 50 case OMEGA_RES_RECEIPT_HASH_MISMATCH: return "RECEIPT_HASH_MISMATCH"; 51 case OMEGA_RES_DEPENDENCY_NOT_PINNED: return "DEPENDENCY_NOT_PINNED"; 52 case OMEGA_RES_DEPENDENCY_CYCLE: return "DEPENDENCY_CYCLE"; 53 case OMEGA_RES_STALE_RECEIPT: return "STALE_RECEIPT"; 54 case OMEGA_RES_UNDECLARED_IMPORT: return "UNDECLARED_IMPORT"; 55 case OMEGA_RES_TAINTED_ARTIFACT: return "TAINTED_ARTIFACT"; 56 case OMEGA_RES_UNKNOWN_VERIFIER_PROFILE: return "UNKNOWN_VERIFIER_PROFILE"; 57 case OMEGA_RES_VERIFIER_TOO_OLD: return "VERIFIER_TOO_OLD"; 58 case OMEGA_RES_BAD_ARGUMENT: return "BAD_ARGUMENT"; 59 case OMEGA_RES_NOMEM: return "NOMEM"; 60 default: return "?"; 61 } 62 } 63 const char *omega_domain_name(OmegaDomain d) { return d == OMEGA_DOMAIN_DEV ? "dev" : d == OMEGA_DOMAIN_BUILD ? "build" : "?"; } 64 65 static void hex(const uint8_t *in, size_t n, char *out) 66 { 67 static const char h[] = "`0123456789abcdef`"; 68 for (size_t i = 0; i < n; i++) { out[2 * i] = h[in[i] >> 4]; out[2 * i + 1] = h[in[i] & 15]; } 69 out[2 * n] = 0; 70 } 71 static int unhex32(const char *s, uint8_t out[32]) 72 { 73 for (int i = 0; i < 64; i++) if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) return -1; 74 for (int i = 0; i < 32; i++) { 75 int hi = s[2 * i] <= '9' ? s[2 * i] - '0' : s[2 * i] - 'a' + 10; 76 int lo = s[2 * i + 1] <= '9' ? s[2 * i + 1] - '0' : s[2 * i + 1] - 'a' + 10; 77 out[i] = (uint8_t)(hi << 4 | lo); 78 } 79 return 0; 80 } 81 static int is_zero32(const uint8_t *p) { static const uint8_t z[32] = { 0 }; return memcmp(p, z, 32) == 0; } 82 83 static int fail(OmegaResolveError *e, int code, int store_code, const char *subject, const char *fmt, ...) 84 __attribute__((format(printf, 5, 6))); 85 static int fail(OmegaResolveError *e, int code, int store_code, const char *subject, const char *fmt, ...) 86 { 87 if (e) { 88 memset(e, 0, sizeof *e); 89 e->code = code; 90 e->store_code = store_code; 91 if (subject) snprintf(e->subject, sizeof e->subject, "%s", subject); 92 va_list ap; 93 va_start(ap, fmt); 94 vsnprintf(e->message, sizeof e->message, fmt, ap); 95 va_end(ap); 96 } 97 return code; 98 } 99 100 /* ---------------------------------------------------------------- profiles */ 101 static const OmegaVerifierProfile DEFAULT_PROFILES[] = { 102 { "host-v1", "1.0.0", "HOST_TEST" }, 103 { "qemu-v1", "1.0.0", "QEMU" }, 104 { "production-v1", "1.0.0", "PRODUCTION" }, 105 }; 106 const OmegaVerifierProfile *omega_resolve_default_profiles(size_t *n) 107 { 108 if (n) *n = sizeof DEFAULT_PROFILES / sizeof DEFAULT_PROFILES[0]; 109 return DEFAULT_PROFILES; 110 } 111 112 #define MAX_VER_PARTS 8 113 static int parse_version(const char *s, uint32_t out[MAX_VER_PARTS], int *n) 114 { 115 *n = 0; 116 if (!*s) return -1; 117 for (;;) { 118 if (*n >= MAX_VER_PARTS) return -1; 119 uint32_t v = 0; 120 int digits = 0; 121 while (*s >= '0' && *s <= '9') { if (++digits > 9) return -1; v = v * 10 + (uint32_t)(*s - '0'); s++; } 122 if (!digits) return -1; 123 out[(*n)++] = v; 124 if (*s == '.') { s++; continue; } 125 if (*s == 0) return 0; 126 return -1; 127 } 128 } 129 130 static const OmegaVerifierProfile *find_profile(const OmegaVerifierProfile *t, size_t n, const char *name) 131 { 132 for (size_t i = 0; i < n; i++) if (strcmp(t[i].name, name) == 0) return &t[i]; 133 return NULL; 134 } 135 136 int omega_resolve_check_profile(const OmegaVerifierProfile *table, size_t n, const char *profile, 137 const char *version, OmegaResolveError *err) 138 { 139 if (!table && n == 0) table = omega_resolve_default_profiles(&n); 140 const OmegaVerifierProfile *p = table ? find_profile(table, n, profile) : NULL; 141 if (!p) return fail(err, OMEGA_RES_UNKNOWN_VERIFIER_PROFILE, 0, profile, "verifier profile '%s' is not in the profile table", profile); /* VC1R:profile-known */ 142 uint32_t have[MAX_VER_PARTS], need[MAX_VER_PARTS]; 143 int nh, nn; 144 if (parse_version(version, have, &nh) || parse_version(p->min_version, need, &nn)) 145 return fail(err, OMEGA_RES_VERIFIER_TOO_OLD, 0, profile, "verifier version '%s' is not a dotted number, so it cannot be shown to meet %s", version, p->min_version); 146 int cmp = 0; 147 for (int i = 0; i < MAX_VER_PARTS && cmp == 0; i++) { 148 uint32_t a = i < nh ? have[i] : 0, b = i < nn ? need[i] : 0; 149 cmp = a < b ? -1 : a > b ? 1 : 0; 150 } 151 if (cmp < 0) /* VC1R:profile-version */ 152 return fail(err, OMEGA_RES_VERIFIER_TOO_OLD, 0, profile, "verifier version %s is older than the minimum %s for profile %s", version, p->min_version, profile); 153 return 0; 154 } 155 156 /* ---------------------------------------------------------------- lock */ 157 void omega_lock_free(OmegaLock *l) { if (l) { free(l->e); l->e = NULL; l->n = 0; } } 158 const OmegaLockEntry *omega_lock_find(const OmegaLock *l, const char *name) 159 { 160 if (!l) return NULL; 161 for (size_t i = 0; i < l->n; i++) if (strcmp(l->e[i].name, name) == 0) return &l->e[i]; 162 return NULL; 163 } 164 165 static int lock_bad(OmegaResolveError *e, size_t line, const char *why) 166 { 167 char subj[32]; 168 snprintf(subj, sizeof subj, "omega.lock:%zu", line); 169 return fail(e, OMEGA_RES_DEPENDENCY_NOT_PINNED, 0, subj, "malformed omega.lock, line %zu: %s", line, why); 170 } 171 172 int omega_lock_parse(const char *text, size_t len, OmegaLock *out, OmegaResolveError *err) 173 { 174 out->e = NULL; out->n = 0; 175 if (!text || len == 0 || len > (1u << 20)) return lock_bad(err, 1, "empty or oversize file"); 176 for (size_t i = 0; i < len; i++) 177 if (text[i] == '\t' || text[i] == '\r' || text[i] == 0) return lock_bad(err, 1, "tab, CR or NUL byte (LF line endings only)"); /* VC1R:lock-bytes */ 178 if (text[len - 1] != '\n') return lock_bad(err, 1, "the last line must end with a line feed"); 179 size_t cap = 0, line = 0, pos = 0; 180 OmegaLockEntry *ents = NULL; 181 size_t n = 0; 182 int header = 0; 183 while (pos < len) { 184 size_t end = pos; 185 while (text[end] != '\n') end++; 186 const char *ln = text + pos; 187 size_t ll = end - pos; 188 pos = end + 1; 189 line++; 190 if (!header) { 191 if (ll != 13 || memcmp(ln, "omega.lock v1", 13) != 0) { free(ents); return lock_bad(err, line, "line 1 must be exactly 'omega.lock v1'"); } /* VC1R:lock-header */ 192 header = 1; 193 continue; 194 } 195 if (ll == 0 || ln[0] == '#') continue; 196 /* NAME */ 197 size_t k = 0; 198 if (!((ln[0] >= 'A' && ln[0] <= 'Z') || (ln[0] >= 'a' && ln[0] <= 'z') || ln[0] == '_')) { free(ents); return lock_bad(err, line, "unknown line (expected NAME = semantic HEX64 receipt HEX64)"); } /* VC1R:lock-unknown-line */ 199 while (k < ll && ((ln[k] >= 'A' && ln[k] <= 'Z') || (ln[k] >= 'a' && ln[k] <= 'z') || (ln[k] >= '0' && ln[k] <= '9') || 200 ln[k] == '_' || ln[k] == '.' || ln[k] == '-')) k++; 201 if (k > 128) { free(ents); return lock_bad(err, line, "name longer than 128 characters"); } 202 static const char mid[] = " = semantic ", mid2[] = " receipt "; 203 size_t need = k + (sizeof mid - 1) + 64 + (sizeof mid2 - 1) + 64; 204 if (ll != need || memcmp(ln + k, mid, sizeof mid - 1) != 0 || 205 memcmp(ln + k + sizeof mid - 1 + 64, mid2, sizeof mid2 - 1) != 0) { free(ents); return lock_bad(err, line, "unknown line (expected NAME = semantic HEX64 receipt HEX64)"); } 206 char h1[65], h2[65]; 207 memcpy(h1, ln + k + sizeof mid - 1, 64); h1[64] = 0; 208 memcpy(h2, ln + k + sizeof mid - 1 + 64 + sizeof mid2 - 1, 64); h2[64] = 0; 209 OmegaLockEntry en; 210 memset(&en, 0, sizeof en); 211 memcpy(en.name, ln, k); 212 if (unhex32(h1, en.semantic) || unhex32(h2, en.receipt)) { free(ents); return lock_bad(err, line, "digest must be 64 lowercase hex digits"); } /* VC1R:lock-hex */ 213 if (is_zero32(en.semantic) || is_zero32(en.receipt)) { free(ents); return lock_bad(err, line, "an all-zero digest pins nothing"); } 214 if (n > 0 && strcmp(ents[n - 1].name, en.name) >= 0) { /* VC1R:lock-order */ 215 int dup = strcmp(ents[n - 1].name, en.name) == 0; 216 free(ents); 217 return lock_bad(err, line, dup ? "duplicate name" : "names must be sorted ascending"); 218 } 219 if (n >= OMEGA_LOCK_MAX_ENTRIES) { free(ents); return lock_bad(err, line, "too many entries"); } 220 if (n == cap) { 221 cap = cap ? cap * 2 : 16; 222 OmegaLockEntry *t = realloc(ents, cap * sizeof *t); 223 if (!t) { free(ents); return fail(err, OMEGA_RES_NOMEM, 0, "omega.lock", "out of memory"); } 224 ents = t; 225 } 226 ents[n++] = en; 227 } 228 if (!header) return lock_bad(err, 1, "missing header"); 229 out->e = ents; 230 out->n = n; 231 return 0; 232 } 233 234 /* ---------------------------------------------------------------- file sources */ 235 static int read_file(const char *path, size_t maxn, uint8_t **bytes, size_t *len) 236 { 237 FILE *f = fopen(path, "rb"); 238 if (!f) return 1; 239 size_t cap = 4096, n = 0; 240 uint8_t *b = malloc(cap); 241 if (!b) { fclose(f); return -1; } 242 for (;;) { 243 size_t r = fread(b + n, 1, cap - n, f); 244 n += r; 245 if (n < cap) break; 246 if (cap >= maxn) { free(b); fclose(f); return -1; } 247 cap *= 2; 248 uint8_t *t = realloc(b, cap); 249 if (!t) { free(b); fclose(f); return -1; } 250 b = t; 251 } 252 int bad = ferror(f); 253 fclose(f); 254 if (bad || n > maxn) { free(b); return -1; } 255 *bytes = b; 256 *len = n; 257 return 0; 258 } 259 static int dir_fetch(void *dir, const uint8_t id[32], const char *ext, size_t maxn, uint8_t **bytes, size_t *len) 260 { 261 if (!dir) return -1; 262 char hx[65], path[4096]; 263 hex(id, 32, hx); 264 if (snprintf(path, sizeof path, "%s/%s.%s", (const char *)dir, hx, ext) >= (int)sizeof path) return -1; 265 return read_file(path, maxn, bytes, len); 266 } 267 int omega_receipt_dir_fetch(void *dir, const uint8_t id[32], uint8_t **bytes, size_t *len) 268 { 269 return dir_fetch(dir, id, "json", MAX_FILE, bytes, len); 270 } 271 int omega_blob_dir_fetch(void *dir, const uint8_t digest[32], uint8_t **bytes, size_t *len) 272 { 273 return dir_fetch(dir, digest, "blob", 16u << 20, bytes, len); 274 } 275 276 /* ---------------------------------------------------------------- receipt check (SPEC 5.1) */ 277 static int cmp32(const void *a, const void *b) { return memcmp(a, b, 32); } 278 279 static int has_artifact(const OmegaReceipt *rc, const char *want) 280 { 281 for (size_t i = 0; i < rc->n_input; i++) if (strcasecmp(rc->input_artifacts[i], want) == 0) return 1; 282 return 0; 283 } 284 285 int omega_resolve_check_record_receipt(const OmegaResolver *r, const OmegaVcView *vc, OmegaResolveError *err) 286 { 287 char sid[65], rid[65]; 288 hex(vc->semantic_id, 32, sid); 289 hex(vc->receipt_id, 32, rid); 290 char prof[OMEGA_VC_MAX_STRING + 1], ver[OMEGA_VC_MAX_STRING + 1]; 291 memcpy(prof, vc->verifier_profile.p, vc->verifier_profile.len); prof[vc->verifier_profile.len] = 0; 292 memcpy(ver, vc->verifier_version.p, vc->verifier_version.len); ver[vc->verifier_version.len] = 0; 293 /* 5: profile */ 294 int rc = omega_resolve_check_profile(r->profiles, r->n_profiles, prof, ver, err); 295 if (rc && err) snprintf(err->subject, sizeof err->subject, "%s", sid); 296 if (rc) return rc; 297 size_t nprof = r->n_profiles; 298 const OmegaVerifierProfile *tab = r->profiles ? r->profiles : omega_resolve_default_profiles(&nprof); 299 const OmegaVerifierProfile *pf = find_profile(tab, nprof, prof); 300 if (!pf) return fail(err, OMEGA_RES_UNKNOWN_VERIFIER_PROFILE, 0, sid, "verifier profile '%s' is not in the profile table", prof); 301 int need_rank = pf ? omega_tier_rank(pf->min_tier) : -1; 302 /* 6: the receipt itself */ 303 uint8_t *bytes = NULL; 304 size_t blen = 0; 305 int fr = r->fetch_receipt ? r->fetch_receipt(r->fetch_ctx, vc->receipt_id, &bytes, &blen) : 1; 306 if (fr != 0) return fail(err, OMEGA_RES_MISSING_RECEIPT, 0, sid, "receipt %s is %s", rid, fr == 1 ? "not in the receipt store" : "unreadable"); /* VC1R:missing-receipt */ 307 OmegaReceipt rcpt; 308 uint8_t got_id[32]; 309 char why[200]; 310 if (omega_receipt_verify_bytes(bytes, blen, &rcpt, got_id, why, sizeof why)) { 311 free(bytes); 312 return fail(err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, sid, "receipt %s: %s", rid, why); 313 } 314 free(bytes); 315 rc = 0; 316 char want[80]; 317 uint8_t (*deps)[32] = NULL, (*rdeps)[32] = NULL; 318 size_t nd = 0, nrd = 0; 319 if (memcmp(got_id, vc->receipt_id, 32) != 0) { /* VC1R:rule1-name */ 320 rc = fail(err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, sid, "the file stored under receipt %s holds another receipt", rid); 321 goto done; 322 } 323 /* A digest authenticates receipt bytes, not a clean source checkout. 324 * DEV may inspect this evidence, but its output remains tainted. */ 325 if (r->domain == OMEGA_DOMAIN_BUILD && rcpt.dirty) { /* VC1R:rule-clean */ 326 rc = fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "receipt %s was produced from a dirty checkout", rid); 327 goto done; 328 } 329 /* rule 4: the receipt names this program (semantic id), then this source (digest) */ 330 snprintf(want, sizeof want, "sha256:%s", sid); 331 if (!has_artifact(&rcpt, want)) { /* VC1R:rule4-semantic */ 332 rc = fail(err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, sid, "receipt %s does not name program %s", rid, sid); 333 goto done; 334 } 335 { 336 char dh[65]; 337 hex(vc->source_or_ir_digest, 32, dh); 338 snprintf(want, sizeof want, "sha256:%s", dh); 339 if (!has_artifact(&rcpt, want)) { /* VC1R:rule4-source */ 340 rc = fail(err, OMEGA_RES_STALE_RECEIPT, 0, sid, "receipt %s names the program but not its current source digest %s: it covers an older source", rid, dh); 341 goto done; 342 } 343 } 344 /* rule 3: output digest is the evidence root */ 345 if (memcmp(rcpt.output_digest, vc->evidence_root, 32) != 0) { /* VC1R:rule3 */ 346 rc = fail(err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, sid, "receipt %s output_digest is not the record's evidence_root", rid); 347 goto done; 348 } 349 /* rule 5: receipt kind is the verifier profile */ 350 if (strcmp(rcpt.kind, prof) != 0) { /* VC1R:rule5 */ 351 rc = fail(err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, sid, "receipt %s kind '%s' is not the verifier profile '%s'", rid, rcpt.kind, prof); 352 goto done; 353 } 354 /* rule 6: receipt dependencies == the receipt ids of the record's dependencies (as sets) */ 355 nd = vc->n_dependencies; 356 deps = malloc((nd ? nd : 1) * 32); 357 rdeps = malloc((rcpt.n_dependencies ? rcpt.n_dependencies : 1) * 32); 358 if (!deps || !rdeps) { rc = fail(err, OMEGA_RES_NOMEM, 0, sid, "out of memory"); goto done; } 359 for (size_t i = 0; i < nd; i++) { 360 if (omega_vcstore_receipt_of(r->store, vc->dependencies + 64 * i, deps[i]) != 0) { 361 rc = fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "a dependency of %s is not in the store", sid); 362 goto done; 363 } 364 } 365 for (size_t i = 0; i < rcpt.n_dependencies; i++) { 366 char *s = rcpt.dependencies[i]; 367 char low[65]; 368 size_t k = 0; 369 for (; s[k] && k < 64; k++) low[k] = (char)((s[k] >= 'A' && s[k] <= 'F') ? s[k] + 32 : s[k]); 370 low[k] = 0; 371 if (unhex32(low, rdeps[i])) { rc = fail(err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, sid, "receipt %s has a malformed dependency", rid); goto done; } 372 } 373 qsort(deps, nd, 32, cmp32); 374 qsort(rdeps, rcpt.n_dependencies, 32, cmp32); 375 { 376 size_t a = 0, b = 0; 377 for (size_t i = 0; i < nd; i++) if (a == 0 || memcmp(deps[a - 1], deps[i], 32) != 0) memcpy(deps[a++], deps[i], 32); 378 for (size_t i = 0; i < rcpt.n_dependencies; i++) if (b == 0 || memcmp(rdeps[b - 1], rdeps[i], 32) != 0) memcpy(rdeps[b++], rdeps[i], 32); 379 nrd = b; 380 if (a != nrd || (a && memcmp(deps, rdeps, a * 32) != 0)) { /* VC1R:rule6 */ 381 rc = fail(err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, sid, "receipt %s dependencies are not the receipts of the record's dependencies", rid); 382 goto done; 383 } 384 } 385 /* rule 2: a passing receipt of enough tier */ 386 if (rcpt.result != OMEGA_RECEIPT_PASS) { /* VC1R:rule2-pass */ 387 rc = fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "receipt %s does not record PASS", rid); 388 goto done; 389 } 390 for (size_t i = 0; i < rcpt.n_assertions; i++) 391 if (!rcpt.assertions[i].pass) { /* VC1R:rule2-assert */ 392 rc = fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "receipt %s says PASS but an assertion failed", rid); 393 goto done; 394 } 395 if (rcpt.tier_rank >= 4 && rcpt.tier_rank <= 6 && !rcpt.has_lease) { 396 rc = fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "receipt %s is a hardware receipt with no lease binding", rid); 397 goto done; 398 } 399 if (rcpt.tier_rank == 0) { /* VC1R:rule2-testonly */ 400 rc = fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "receipt %s is TEST_ONLY_TRUST, which satisfies no profile", rid); 401 goto done; 402 } 403 if (!omega_tier_satisfies(need_rank, rcpt.tier_rank)) { /* VC1R:rule2-tier */ 404 rc = fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "receipt %s tier %s is below the minimum %s of profile %s", rid, rcpt.tier, pf->min_tier, prof); 405 goto done; 406 } 407 done: 408 free(deps); 409 free(rdeps); 410 omega_receipt_free(&rcpt); 411 return rc; 412 } 413 414 /* ---------------------------------------------------------------- closure */ 415 void omega_closure_free(OmegaClosure *c) { if (c) { free(c->entries); c->entries = NULL; c->n = 0; memset(c->digest, 0, 32); } } 416 417 void omega_closure_digest(const OmegaClosureEntry *e, size_t n, uint8_t out[32]) 418 { 419 sha256_ctx c; 420 sha256_init(&c); 421 static const uint8_t dom[] = "OMEGA.CLOSURE.V1"; 422 sha256_update(&c, dom, sizeof dom); /* includes the NUL after the tag */ 423 uint8_t cnt[4] = { (uint8_t)(n >> 24), (uint8_t)(n >> 16), (uint8_t)(n >> 8), (uint8_t)n }; 424 sha256_update(&c, cnt, 4); 425 for (size_t i = 0; i < n; i++) { 426 sha256_update(&c, e[i].semantic_id, 32); 427 sha256_update(&c, e[i].receipt_id, 32); /* VC1R:closure-receipt */ 428 sha256_update(&c, &e[i].admission_kind, 1); 429 } 430 sha256_final(&c, out); 431 } 432 433 typedef struct { uint8_t id[32], receipt[32], contract[32]; uint8_t kind; int state; } WNode; 434 typedef struct { 435 const OmegaResolver *r; 436 WNode *nodes; 437 size_t n, cap; 438 OmegaResolveError *err; 439 } Walk; 440 441 static long wfind(const Walk *w, const uint8_t id[32]) 442 { 443 for (size_t i = 0; i < w->n; i++) if (memcmp(w->nodes[i].id, id, 32) == 0) return (long)i; 444 return -1; 445 } 446 447 static int lists_selfminted_cap(const OmegaVcView *v) 448 { 449 for (uint32_t i = 0; i < v->n_capabilities; i++) 450 if (v->capabilities[i].len == sizeof OMEGA_BRIDGE_SELFMINTED_CAPABILITY - 1 && 451 memcmp(v->capabilities[i].p, OMEGA_BRIDGE_SELFMINTED_CAPABILITY, sizeof OMEGA_BRIDGE_SELFMINTED_CAPABILITY - 1) == 0) return 1; 452 return 0; 453 } 454 455 static int resolve_node(Walk *w, const uint8_t id[32], const uint8_t *lock_receipt, const char *name, size_t depth) 456 { 457 const OmegaResolver *r = w->r; 458 char sid[65]; 459 hex(id, 32, sid); 460 const char *subj = name ? name : sid; 461 long at = wfind(w, id); 462 if (at >= 0) { 463 if (w->nodes[at].state == 1) return fail(w->err, OMEGA_RES_DEPENDENCY_CYCLE, 0, sid, "dependency cycle through %s", sid); /* VC1R:cycle */ 464 if (lock_receipt && memcmp(lock_receipt, w->nodes[at].receipt, 32) != 0) /* VC1R:visited-lock-receipt */ 465 return fail(w->err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, subj, "the lock pins another receipt than the record carries for %s", sid); 466 return 0; 467 } 468 if (depth >= MAX_DEPTH) return fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "dependency chain deeper than %d", MAX_DEPTH); /* VC1R:max-depth */ 469 OmegaVcRecord *rec = malloc(sizeof *rec); 470 if (!rec) return fail(w->err, OMEGA_RES_NOMEM, 0, sid, "out of memory"); 471 int rc = omega_vcstore_get(r->store, id, rec); 472 if (rc) { 473 int out = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, rc, subj, "%s is not an intact record in the Verified Crumb Store (store says %s)", sid, omega_vcstore_code_name(rc)); 474 free(rec); 475 return out; 476 } 477 const OmegaVcView *vc = &rec->vc; 478 rc = 0; 479 if (memcmp(vc->semantic_id, id, 32) != 0) { 480 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the record fetched for %s names a different semantic id", sid); 481 goto out; 482 } 483 if (!r->fetch_blob && r->domain == OMEGA_DOMAIN_BUILD) { /* VC1R:build-needs-blobs */ 484 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the build domain needs the source or IR store to recompute %s (SPEC 6 step 3), and none was given", sid); 485 goto out; 486 } 487 if (r->fetch_blob) { 488 uint8_t *bb = NULL; 489 size_t bl = 0; 490 uint8_t dg[32]; 491 int fr = r->fetch_blob(r->blob_ctx, vc->source_or_ir_digest, &bb, &bl); 492 if (fr != 0) { rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the source or IR named by %s is not available to recompute its digest", sid); goto out; } 493 sha256_hash(bb, bl, dg); 494 if (memcmp(dg, vc->source_or_ir_digest, 32) != 0) { /* VC1R:blob-digest */ 495 free(bb); 496 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the stored source or IR of %s does not hash to its source_or_ir_digest", sid); 497 goto out; 498 } 499 if (vc->digest_kind == OMEGA_VC_DIGEST_IR) { 500 uint8_t pid[32]; 501 if (omega_program_ir_recompute_id(bb, bl, pid) != 0 || memcmp(pid, id, 32) != 0) { /* VC1R:program-id */ 502 free(bb); 503 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "the stored IR of %s does not recompute to its semantic id", sid); 504 goto out; 505 } 506 } else if (r->domain == OMEGA_DOMAIN_BUILD) { /* VC1R:ir-kind */ 507 free(bb); 508 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "%s was recorded from OSC source, and only an IR digest can be recomputed to its program id in the build domain", sid); 509 goto out; 510 } 511 free(bb); 512 } 513 if (lock_receipt && memcmp(lock_receipt, vc->receipt_id, 32) != 0) { /* VC1R:lock-receipt */ 514 rc = fail(w->err, OMEGA_RES_RECEIPT_HASH_MISMATCH, 0, subj, "the lock pins another receipt than the record carries for %s", sid); 515 goto out; 516 } 517 if (rec->admission_kind != OMEGA_VCS_ADMISSION_VERIFIED && rec->admission_kind != OMEGA_VCS_ADMISSION_BOOTSTRAP) { 518 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "%s has an unknown admission kind", sid); 519 goto out; 520 } 521 for (uint32_t i = 0; i < vc->n_capabilities; i++) 522 if (vc->capabilities[i].len == sizeof OMEGA_TAINT_CAPABILITY - 1 && 523 memcmp(vc->capabilities[i].p, OMEGA_TAINT_CAPABILITY, sizeof OMEGA_TAINT_CAPABILITY - 1) == 0) { /* VC1R:taint-cap */ 524 rc = fail(w->err, OMEGA_RES_TAINTED_ARTIFACT, 0, subj, "%s lists the capability %s: an omega-dev output can never satisfy an import", sid, OMEGA_TAINT_CAPABILITY); 525 goto out; 526 } 527 if (r->domain == OMEGA_DOMAIN_BUILD && lists_selfminted_cap(vc)) { rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, subj, "%s was minted by the in-process bridge (capability %s) with no independent qualification run, so it cannot satisfy a build import", sid, OMEGA_BRIDGE_SELFMINTED_CAPABILITY); goto out; } /* VC1B:build-refuses-selfminted */ 528 if (rec->admission_kind == OMEGA_VCS_ADMISSION_VERIFIED) { /* VC1R:verified-needs-receipt */ 529 rc = omega_resolve_check_record_receipt(r, vc, w->err); 530 if (rc) { if (name && w->err) snprintf(w->err->subject, sizeof w->err->subject, "%s", name); goto out; } 531 } 532 if (w->n == w->cap) { 533 size_t nc = w->cap ? w->cap * 2 : 16; 534 WNode *t = realloc(w->nodes, nc * sizeof *t); 535 if (!t) { rc = fail(w->err, OMEGA_RES_NOMEM, 0, sid, "out of memory"); goto out; } 536 w->nodes = t; 537 w->cap = nc; 538 } 539 size_t me = w->n++; 540 memcpy(w->nodes[me].id, id, 32); 541 memcpy(w->nodes[me].receipt, vc->receipt_id, 32); 542 memcpy(w->nodes[me].contract, vc->contract_id, 32); 543 w->nodes[me].kind = rec->admission_kind; 544 w->nodes[me].state = 1; 545 for (uint32_t i = 0; i < vc->n_dependencies; i++) { 546 const uint8_t *dep = vc->dependencies + 64 * (size_t)i, *req = dep + 32; 547 rc = resolve_node(w, dep, NULL, NULL, depth + 1); 548 if (rc) goto out; 549 long di = wfind(w, dep); 550 if (di < 0) { rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "internal: dependency vanished"); goto out; } 551 if (memcmp(w->nodes[di].contract, req, 32) != 0) { /* VC1R:edge-contract */ 552 char dh[65]; 553 hex(dep, 32, dh); 554 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "dependency %s of %s has a different contract than required", dh, sid); 555 goto out; 556 } 557 if (w->nodes[me].kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && w->nodes[di].kind != OMEGA_VCS_ADMISSION_BOOTSTRAP) { /* VC1R:boot-dep-verified */ 558 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "genesis record %s depends on a non-genesis record: the audited base must be closed over itself", sid); 559 goto out; 560 } 561 } 562 /* THE genesis gate: reached only after the whole subtree resolved, and refusing any BOOTSTRAP record whose id the pinned 563 * VC-GENESIS-1 does not list. Nothing but list membership can satisfy it (no flag, no variable, nothing from the store). */ 564 if (rec->admission_kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && !omega_genesis_contains(vc->semantic_id)) { /* VC1R:genesis-gate */ 565 rc = fail(w->err, OMEGA_RES_UNVERIFIED_DEPENDENCY, OMEGA_VCS_GENESIS_NOT_LISTED, subj, "%s is a genesis (bootstrap) record whose id is not in the pinned set %s", sid, OMEGA_GENESIS_SET_NAME); 566 goto out; 567 } 568 w->nodes[me].state = 2; 569 out: 570 free(rec); 571 return rc; 572 } 573 574 static int cmp_entry(const void *a, const void *b) { return memcmp(a, b, 32); } 575 576 int omega_resolve_imports(const OmegaResolver *r, const OmegaLock *lock, const char *const *names, 577 size_t n_names, OmegaClosure *out, OmegaResolveError *err) 578 { 579 out->entries = NULL; out->n = 0; 580 memset(out->digest, 0, 32); 581 if (!r || !r->store || (n_names && !names)) return fail(err, OMEGA_RES_BAD_ARGUMENT, 0, "resolver", "bad argument"); 582 OmegaLock empty = { NULL, 0 }; 583 if (!lock) lock = ∅ 584 for (size_t i = 0; i < n_names; i++) 585 for (size_t k = 0; k < i; k++) 586 if (strcmp(names[i], names[k]) == 0) return fail(err, OMEGA_RES_BAD_ARGUMENT, 0, names[i], "import '%s' is listed twice", names[i]); /* VC1R:dup-import */ 587 /* declaration check: every import is pinned, every pin is imported */ 588 for (size_t i = 0; i < n_names; i++) 589 if (!omega_lock_find(lock, names[i])) /* VC1R:not-pinned */ 590 return fail(err, OMEGA_RES_DEPENDENCY_NOT_PINNED, 0, names[i], "import '%s' has no line in omega.lock", names[i]); 591 for (size_t i = 0; i < lock->n; i++) { 592 int used = 0; 593 for (size_t k = 0; k < n_names; k++) if (strcmp(names[k], lock->e[i].name) == 0) used = 1; 594 if (!used) /* VC1R:undeclared */ 595 return fail(err, OMEGA_RES_UNDECLARED_IMPORT, 0, lock->e[i].name, "omega.lock pins '%s' but the source never imports it", lock->e[i].name); 596 } 597 Walk w = { r, NULL, 0, 0, err }; 598 int rc = 0; 599 for (size_t i = 0; i < n_names && !rc; i++) { 600 const OmegaLockEntry *le = omega_lock_find(lock, names[i]); 601 if (!le) { rc = fail(err, OMEGA_RES_BAD_ARGUMENT, 0, names[i], "internal error: the pin for %s vanished after the declaration check", names[i]); break; } 602 rc = resolve_node(&w, le->semantic, le->receipt, names[i], 0); 603 } 604 if (rc) { free(w.nodes); return rc; } 605 OmegaClosureEntry *ents = malloc((w.n ? w.n : 1) * sizeof *ents); 606 if (!ents) { free(w.nodes); return fail(err, OMEGA_RES_NOMEM, 0, "closure", "out of memory"); } 607 for (size_t i = 0; i < w.n; i++) { 608 memcpy(ents[i].semantic_id, w.nodes[i].id, 32); 609 memcpy(ents[i].receipt_id, w.nodes[i].receipt, 32); 610 ents[i].admission_kind = w.nodes[i].kind; 611 } 612 free(w.nodes); 613 _Static_assert(offsetof(OmegaClosureEntry, semantic_id) == 0, "entries sort by their first 32 bytes"); 614 qsort(ents, w.n, sizeof *ents, cmp_entry); /* VC1R:closure-sort */ 615 out->entries = ents; 616 out->n = w.n; 617 omega_closure_digest(ents, w.n, out->digest); 618 return 0; 619 } 620 621 /* ---------------------------------------------------------------- build identity and the taint mark */ 622 void omega_build_id(const uint8_t ir_digest[32], OmegaDomain d, const uint8_t closure_digest[32], uint8_t out[32]) 623 { 624 sha256_ctx c; 625 sha256_init(&c); 626 static const uint8_t dom[] = "OSC1.BUILD.V1"; 627 sha256_update(&c, dom, sizeof dom); 628 sha256_update(&c, ir_digest, 32); 629 uint8_t db = (uint8_t)d; /* VC1R:build-domain */ 630 sha256_update(&c, &db, 1); 631 sha256_update(&c, closure_digest, 32); /* VC1R:build-closure */ 632 sha256_final(&c, out); 633 } 634 635 void omega_artifact_meta_make(OmegaArtifactMeta *m, OmegaDomain d, const uint8_t ir_digest[32], const uint8_t closure_digest[32]) 636 { 637 memset(m, 0, sizeof *m); 638 m->domain = d; 639 m->tainted = (uint8_t)(d == OMEGA_DOMAIN_DEV); /* VC1R:taint-mark */ 640 memcpy(m->ir_digest, ir_digest, 32); 641 memcpy(m->closure_digest, closure_digest, 32); 642 omega_build_id(ir_digest, d, closure_digest, m->build_id); 643 } 644 645 int omega_artifact_meta_text(const OmegaArtifactMeta *m, char *buf, size_t cap) 646 { 647 char a[65], b[65], c[65]; 648 hex(m->ir_digest, 32, a); hex(m->closure_digest, 32, b); hex(m->build_id, 32, c); 649 int n = snprintf(buf, cap, "OMEGA-ARTIFACT v1\ndomain %s\ntainted %d\nir_sha256 %s\nclosure_sha256 %s\nbuild_id %s\n", 650 omega_domain_name(m->domain), m->tainted ? 1 : 0, a, b, c); 651 return (n < 0 || (size_t)n >= cap) ? -1 : n; 652 } 653 654 int omega_artifact_meta_parse(const char *text, size_t len, OmegaArtifactMeta *m, OmegaResolveError *err) 655 { 656 memset(m, 0, sizeof *m); 657 if (!text || len == 0 || len > 4096 || text[len - 1] != '\n') return fail(err, OMEGA_RES_BAD_ARGUMENT, 0, "artifact", "malformed artifact header"); 658 char *t = malloc(len + 1); 659 if (!t) return fail(err, OMEGA_RES_NOMEM, 0, "artifact", "out of memory"); 660 memcpy(t, text, len); 661 t[len] = 0; 662 static const char *const keys[] = { "OMEGA-ARTIFACT v1", "domain ", "tainted ", "ir_sha256 ", "closure_sha256 ", "build_id " }; 663 char *line = t, *vals[6] = { 0 }; 664 int ok = 1; 665 for (int i = 0; i < 6 && ok; i++) { 666 char *nl = strchr(line, '\n'); 667 if (!nl) { ok = 0; break; } 668 *nl = 0; 669 size_t kl = strlen(keys[i]); 670 if (strncmp(line, keys[i], kl) != 0) { ok = 0; break; } 671 vals[i] = line + kl; 672 if (i == 0 && vals[i][0]) ok = 0; 673 line = nl + 1; 674 } 675 if (ok && *line) ok = 0; 676 uint8_t build[32]; 677 int domain = 0, tainted = -1; 678 if (ok) { 679 if (strcmp(vals[1], "dev") == 0) domain = OMEGA_DOMAIN_DEV; else if (strcmp(vals[1], "build") == 0) domain = OMEGA_DOMAIN_BUILD; else ok = 0; 680 if (strcmp(vals[2], "1") == 0) tainted = 1; else if (strcmp(vals[2], "0") == 0) tainted = 0; else ok = 0; 681 if (unhex32(vals[3], m->ir_digest) || unhex32(vals[4], m->closure_digest) || unhex32(vals[5], build)) ok = 0; 682 } 683 free(t); 684 if (!ok) return fail(err, OMEGA_RES_BAD_ARGUMENT, 0, "artifact", "malformed artifact header"); 685 m->domain = (OmegaDomain)domain; 686 m->tainted = (uint8_t)tainted; 687 if (tainted != (domain == OMEGA_DOMAIN_DEV)) /* VC1R:meta-taint-consistent */ 688 return fail(err, OMEGA_RES_TAINTED_ARTIFACT, 0, "artifact", "the taint mark does not match the domain: an omega-dev artifact is always tainted"); 689 omega_build_id(m->ir_digest, m->domain, m->closure_digest, m->build_id); 690 if (memcmp(m->build_id, build, 32) != 0) /* VC1R:meta-build-id */ 691 return fail(err, OMEGA_RES_TAINTED_ARTIFACT, 0, "artifact", "the build id does not match the header: the artifact was edited"); 692 return 0; 693 } 694 695 /* ---------------------------------------------------------------- the one door into the store */ 696 static int map_store(OmegaResolveError *err, int vcs, const char *subject) 697 { 698 int code = (vcs >= OMEGA_VCS_UNVERIFIED_DEPENDENCY && vcs <= OMEGA_VCS_UNKNOWN_VERIFIER_PROFILE) ? vcs : OMEGA_RES_UNVERIFIED_DEPENDENCY; 699 return fail(err, code, vcs, subject, "the store refused the record: %s", omega_vcstore_code_name(vcs)); 700 } 701 702 static int has_taint_cap(const OmegaVcView *v) 703 { 704 for (uint32_t i = 0; i < v->n_capabilities; i++) 705 if (v->capabilities[i].len == sizeof OMEGA_TAINT_CAPABILITY - 1 && 706 memcmp(v->capabilities[i].p, OMEGA_TAINT_CAPABILITY, sizeof OMEGA_TAINT_CAPABILITY - 1) == 0) return 1; 707 return 0; 708 } 709 710 int omega_resolve_admit(const OmegaResolver *r, OmegaVcStore *s, const uint8_t *canonical, size_t len, 711 const uint8_t claimed_vc_id[32], const OmegaArtifactMeta *origin, OmegaResolveError *err) 712 { 713 if (!r || !s || !canonical || !claimed_vc_id) return fail(err, OMEGA_RES_BAD_ARGUMENT, 0, "admit", "bad argument"); 714 if (origin && (origin->tainted || origin->domain == OMEGA_DOMAIN_DEV)) /* VC1R:admit-origin */ 715 return fail(err, OMEGA_RES_TAINTED_ARTIFACT, 0, "admit", "an omega-dev artifact is tainted and can never enter the store"); 716 OmegaVcView *view = malloc(sizeof *view); 717 if (!view) return fail(err, OMEGA_RES_NOMEM, 0, "admit", "out of memory"); 718 int rc = omega_vc_decode(canonical, len, view); 719 if (rc) { free(view); return map_store(err, rc, "admit"); } 720 char sid[65]; 721 hex(view->semantic_id, 32, sid); 722 if (has_taint_cap(view)) { /* VC1R:admit-cap */ 723 free(view); 724 return fail(err, OMEGA_RES_TAINTED_ARTIFACT, 0, sid, "the record lists %s and can never enter the store", OMEGA_TAINT_CAPABILITY); 725 } 726 OmegaResolver rr = *r; 727 rr.store = s; 728 rc = omega_resolve_check_record_receipt(&rr, view, err); /* VC1R:admit-receipt */ 729 free(view); 730 if (rc) return rc; 731 rc = omega_vcstore_insert(s, canonical, len, claimed_vc_id); 732 if (rc) return map_store(err, rc, sid); 733 return 0; 734 } 735 736 int omega_resolve_admit_genesis(OmegaVcStore *s, const uint8_t *canonical, size_t len, 737 const uint8_t claimed_vc_id[32], const OmegaArtifactMeta *origin, OmegaResolveError *err) 738 { 739 if (!s || !canonical || !claimed_vc_id) return fail(err, OMEGA_RES_BAD_ARGUMENT, 0, "genesis", "bad argument"); 740 if (origin && (origin->tainted || origin->domain == OMEGA_DOMAIN_DEV)) 741 return fail(err, OMEGA_RES_TAINTED_ARTIFACT, 0, "genesis", "an omega-dev artifact is tainted and can never enter the store"); 742 OmegaVcView *view = malloc(sizeof *view); 743 if (!view) return fail(err, OMEGA_RES_NOMEM, 0, "genesis", "out of memory"); 744 int rc = omega_vc_decode(canonical, len, view); 745 if (rc) { free(view); return map_store(err, rc, "genesis"); } 746 char sid[65]; 747 hex(view->semantic_id, 32, sid); 748 if (has_taint_cap(view)) { 749 free(view); 750 return fail(err, OMEGA_RES_TAINTED_ARTIFACT, 0, sid, "the record lists %s and can never enter the store", OMEGA_TAINT_CAPABILITY); 751 } 752 for (uint32_t i = 0; i < view->n_dependencies; i++) { 753 uint8_t k = 0; 754 if (omega_vcstore_admission_kind(s, view->dependencies + 64 * (size_t)i, &k) != 0 || k != OMEGA_VCS_ADMISSION_BOOTSTRAP) { /* VC1R:genesis-dep */ 755 free(view); 756 return fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, 0, sid, "a genesis record may depend only on genesis records already in the store"); 757 } 758 } 759 if (!omega_genesis_contains(view->semantic_id)) { /* VC1R:admit-genesis-list */ 760 free(view); 761 return fail(err, OMEGA_RES_UNVERIFIED_DEPENDENCY, OMEGA_VCS_GENESIS_NOT_LISTED, sid, "%s is not a member of the pinned genesis set %s", sid, OMEGA_GENESIS_SET_NAME); 762 } 763 free(view); 764 rc = omega_vcstore_insert_bootstrap(s, canonical, len, claimed_vc_id); 765 if (rc) return map_store(err, rc, sid); 766 return 0; 767 }