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_receipt.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_receipt.c` 711 lines ยท 32428 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_receipt.c -- see omega_receipt.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_receipt.h" 21 22 #include 23 #include 24 #include 25 26 #include "omega_blake3.h" 27 28 #define DOMAIN "AIEN_EVIDENCE_RECEIPT_V1" 29 #define SCHEMA "EvidenceReceiptV1" 30 31 /* ---------------------------------------------------------------- small helpers */ 32 static void seterr(char *err, size_t cap, const char *fmt, const char *a) 33 { 34 if (err && cap) snprintf(err, cap, fmt, a ? a : ""); 35 } 36 37 static int hexval(int c) 38 { 39 if (c >= '0' && c <= '9') return c - '0'; 40 if (c >= 'a' && c <= 'f') return c - 'a' + 10; 41 if (c >= 'A' && c <= 'F') return c - 'A' + 10; 42 return -1; 43 } 44 static int is_hex(const char *s) 45 { 46 if (!*s) return 0; 47 for (; *s; s++) if (hexval((unsigned char)*s) < 0) return 0; 48 return 1; 49 } 50 static int is_lower_hex(const char *s) 51 { 52 if (!*s) return 0; 53 for (; *s; s++) if (!((*s >= '0' && *s <= '9') || (*s >= 'a' && *s <= 'f'))) return 0; 54 return 1; 55 } 56 /* exactly 64 hex digits (any case) to 32 bytes */ 57 static int hex32(const char *s, uint8_t out[32]) 58 { 59 if (strlen(s) != 64) return -1; 60 for (int i = 0; i < 32; i++) { 61 int hi = hexval((unsigned char)s[2 * i]), lo = hexval((unsigned char)s[2 * i + 1]); 62 if (hi < 0 || lo < 0) return -1; 63 out[i] = (uint8_t)(hi << 4 | lo); 64 } 65 return 0; 66 } 67 68 /* ---------------------------------------------------------------- tiers, verdicts, mutations */ 69 static const struct { const char *name; int rank; } TIERS[] = { 70 {"TEST_ONLY_TRUST", 0}, {"HOST_TEST", 1}, {"QEMU", 2}, {"QEMU_SECURITY", 3}, 71 {"MACHINE1_READ_ONLY", 4}, {"MACHINE1_ATTENDED", 5}, {"MACHINE1_MUTATING", 6}, {"PRODUCTION", 7}, 72 }; 73 int omega_tier_rank(const char *tier) 74 { 75 for (size_t i = 0; i < sizeof TIERS / sizeof TIERS[0]; i++) 76 if (strcmp(TIERS[i].name, tier) == 0) return TIERS[i].rank; 77 return -1; 78 } 79 int omega_tier_satisfies(int need, int have) 80 { 81 if (need < 0 || have < 0) return 0; 82 if (have == 0) return need == 0; /* test-only trust satisfies only itself */ 83 if (need >= 4 && have < 4) return 0; /* physical needs physical (rank >= 4) */ 84 return have >= need; 85 } 86 static const char *const VERDICTS[] = { "PASS", "FAIL", "BLOCKED", "INCOMPLETE", "SKIPPED" }; 87 /* name, wire code, severity */ 88 static const struct { const char *name; uint8_t code, sev; } MUTS[] = { 89 {"NONE", 0, 0}, {"VOLATILE_ONLY", 1, 1}, {"REMOVABLE_MEDIA_ONLY", 2, 2}, 90 {"ONE_TIME_BOOT_SELECTION", 3, 3}, {"BOUNDED_TEST_REGION_WRITE", 4, 4}, 91 {"BOOT_CONFIGURATION_CHANGE", 5, 5}, {"TRUST_ROOT_CHANGE", 6, 6}, 92 {"TPM_POLICY_CHANGE", 7, 6}, {"DESTRUCTIVE_STORAGE", 8, 7}, 93 }; 94 #define N_MUTS (sizeof MUTS / sizeof MUTS[0]) 95 96 /* ---------------------------------------------------------------- JSON DOM */ 97 typedef enum { J_NULL, J_BOOL, J_NUM, J_STR, J_ARR, J_OBJ } JK; 98 typedef struct J { 99 JK k; 100 int b; 101 uint64_t u; 102 int is_uint; 103 char *s; /* J_STR, NUL-terminated, no embedded NUL */ 104 struct J **v; /* J_ARR items, J_OBJ values */ 105 char **keys; /* J_OBJ */ 106 size_t n; 107 } J; 108 109 typedef struct { const uint8_t *p; size_t n, i; int depth; char err[160]; } JP; 110 111 static J *jnew(JK k) 112 { 113 J *j = calloc(1, sizeof *j); 114 if (j) j->k = k; 115 return j; 116 } 117 static void jfree(J *j) 118 { 119 if (!j) return; 120 free(j->s); 121 for (size_t i = 0; i < j->n; i++) { jfree(j->v ? j->v[i] : NULL); if (j->keys) free(j->keys[i]); } 122 free(j->v); 123 free(j->keys); 124 free(j); 125 } 126 static J *jfail(JP *p, const char *what) 127 { 128 if (!p->err[0]) snprintf(p->err, sizeof p->err, "receipt JSON: %s at byte %zu", what, p->i); 129 return NULL; 130 } 131 static void jws(JP *p) 132 { 133 while (p->i < p->n && (p->p[p->i] == ' ' || p->p[p->i] == '\t' || p->p[p->i] == '\n' || p->p[p->i] == '\r')) p->i++; 134 } 135 static int utf8_len(const uint8_t *s, size_t avail) 136 { 137 if (s[0] < 0x80) return 1; 138 if (s[0] >= 0xC2 && s[0] <= 0xDF && avail >= 2 && (s[1] & 0xC0) == 0x80) return 2; 139 if (s[0] >= 0xE0 && s[0] <= 0xEF && avail >= 3 && (s[1] & 0xC0) == 0x80 && (s[2] & 0xC0) == 0x80) { 140 uint32_t cp = (uint32_t)(s[0] & 0x0F) << 12 | (uint32_t)(s[1] & 0x3F) << 6 | (s[2] & 0x3F); 141 if (cp >= 0x800 && !(cp >= 0xD800 && cp <= 0xDFFF)) return 3; 142 } 143 if (s[0] >= 0xF0 && s[0] <= 0xF4 && avail >= 4 && (s[1] & 0xC0) == 0x80 && (s[2] & 0xC0) == 0x80 && (s[3] & 0xC0) == 0x80) { 144 uint32_t cp = (uint32_t)(s[0] & 0x07) << 18 | (uint32_t)(s[1] & 0x3F) << 12 | (uint32_t)(s[2] & 0x3F) << 6 | (s[3] & 0x3F); 145 if (cp >= 0x10000 && cp <= 0x10FFFF) return 4; 146 } 147 return 0; 148 } 149 static int put_utf8(char *o, uint32_t cp) 150 { 151 if (cp < 0x80) { o[0] = (char)cp; return 1; } 152 if (cp < 0x800) { o[0] = (char)(0xC0 | cp >> 6); o[1] = (char)(0x80 | (cp & 0x3F)); return 2; } 153 if (cp < 0x10000) { o[0] = (char)(0xE0 | cp >> 12); o[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); o[2] = (char)(0x80 | (cp & 0x3F)); return 3; } 154 o[0] = (char)(0xF0 | cp >> 18); o[1] = (char)(0x80 | ((cp >> 12) & 0x3F)); 155 o[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); o[3] = (char)(0x80 | (cp & 0x3F)); 156 return 4; 157 } 158 static int hex4(JP *p, uint32_t *out) 159 { 160 if (p->i + 4 > p->n) return -1; 161 uint32_t v = 0; 162 for (int k = 0; k < 4; k++) { 163 int h = hexval(p->p[p->i + k]); 164 if (h < 0) return -1; 165 v = v << 4 | (uint32_t)h; 166 } 167 p->i += 4; 168 *out = v; 169 return 0; 170 } 171 /* p->i is just past the opening quote */ 172 static char *jstring(JP *p) 173 { 174 size_t cap = 16, n = 0; 175 char *s = malloc(cap); 176 if (!s) { jfail(p, "out of memory"); return NULL; } 177 for (;;) { 178 if (p->i >= p->n) { free(s); jfail(p, "unterminated string"); return NULL; } 179 uint8_t c = p->p[p->i]; 180 if (n + 8 > cap) { cap *= 2; char *t = realloc(s, cap); if (!t) { free(s); jfail(p, "out of memory"); return NULL; } s = t; } 181 if (c == '"') { p->i++; s[n] = 0; return s; } 182 if (c < 0x20) { free(s); jfail(p, "control character in string"); return NULL; } 183 if (c == '\\') { 184 p->i++; 185 if (p->i >= p->n) { free(s); jfail(p, "bad escape"); return NULL; } 186 uint8_t e = p->p[p->i++]; 187 switch (e) { 188 case '"': s[n++] = '"'; break; 189 case '\\': s[n++] = '\\'; break; 190 case '/': s[n++] = '/'; break; 191 case 'b': s[n++] = '\b'; break; 192 case 'f': s[n++] = '\f'; break; 193 case 'n': s[n++] = '\n'; break; 194 case 'r': s[n++] = '\r'; break; 195 case 't': s[n++] = '\t'; break; 196 case 'u': { 197 uint32_t cp; 198 if (hex4(p, &cp)) { free(s); jfail(p, "bad \\u escape"); return NULL; } 199 if (cp >= 0xD800 && cp <= 0xDBFF) { 200 uint32_t lo; 201 if (p->i + 2 > p->n || p->p[p->i] != '\\' || p->p[p->i + 1] != 'u') { free(s); jfail(p, "lone surrogate"); return NULL; } 202 p->i += 2; 203 if (hex4(p, &lo) || lo < 0xDC00 || lo > 0xDFFF) { free(s); jfail(p, "bad surrogate pair"); return NULL; } 204 cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00); 205 } else if (cp >= 0xDC00 && cp <= 0xDFFF) { free(s); jfail(p, "lone surrogate"); return NULL; } 206 if (cp == 0) { free(s); jfail(p, "NUL in string"); return NULL; } 207 n += (size_t)put_utf8(s + n, cp); 208 break; 209 } 210 default: free(s); jfail(p, "bad escape"); return NULL; 211 } 212 continue; 213 } 214 int l = utf8_len(p->p + p->i, p->n - p->i); 215 if (!l) { free(s); jfail(p, "invalid UTF-8"); return NULL; } 216 memcpy(s + n, p->p + p->i, (size_t)l); 217 n += (size_t)l; 218 p->i += (size_t)l; 219 } 220 } 221 static J *jvalue(JP *p); 222 static J *jcontainer(JP *p, int is_obj) 223 { 224 if (++p->depth > 6) return jfail(p, "nesting too deep"); 225 J *j = jnew(is_obj ? J_OBJ : J_ARR); 226 if (!j) return jfail(p, "out of memory"); 227 size_t cap = 0; 228 jws(p); 229 char close = is_obj ? '}' : ']'; 230 if (p->i < p->n && p->p[p->i] == close) { p->i++; p->depth--; return j; } 231 for (;;) { 232 jws(p); 233 char *key = NULL; 234 if (is_obj) { 235 if (p->i >= p->n || p->p[p->i] != '"') { jfree(j); return jfail(p, "expected object key"); } 236 p->i++; 237 key = jstring(p); 238 if (!key) { jfree(j); return NULL; } 239 for (size_t k = 0; k < j->n; k++) 240 if (strcmp(j->keys[k], key) == 0) { free(key); jfree(j); return jfail(p, "duplicate key"); } /* VC1R:json-dup-key */ 241 jws(p); 242 if (p->i >= p->n || p->p[p->i] != ':') { free(key); jfree(j); return jfail(p, "expected ':'"); } 243 p->i++; 244 } 245 J *v = jvalue(p); 246 if (!v) { free(key); jfree(j); return NULL; } 247 if (j->n == cap) { 248 cap = cap ? cap * 2 : 8; 249 J **nv = realloc(j->v, cap * sizeof *nv); 250 char **nk = is_obj ? realloc(j->keys, cap * sizeof *nk) : NULL; 251 if (nv) j->v = nv; 252 if (nk) j->keys = nk; 253 if (!nv || (is_obj && !nk)) { free(key); jfree(v); jfree(j); return jfail(p, "out of memory"); } 254 } 255 j->v[j->n] = v; 256 if (is_obj) j->keys[j->n] = key; 257 j->n++; 258 jws(p); 259 if (p->i < p->n && p->p[p->i] == ',') { p->i++; continue; } 260 if (p->i < p->n && p->p[p->i] == close) { p->i++; break; } 261 jfree(j); 262 return jfail(p, "expected ',' or closing bracket"); 263 } 264 p->depth--; 265 return j; 266 } 267 static J *jvalue(JP *p) 268 { 269 jws(p); 270 if (p->i >= p->n) return jfail(p, "unexpected end"); 271 uint8_t c = p->p[p->i]; 272 if (c == '{') { p->i++; return jcontainer(p, 1); } 273 if (c == '[') { p->i++; return jcontainer(p, 0); } 274 if (c == '"') { 275 p->i++; 276 char *s = jstring(p); 277 if (!s) return NULL; 278 J *j = jnew(J_STR); 279 if (!j) { free(s); return jfail(p, "out of memory"); } 280 j->s = s; 281 return j; 282 } 283 if (p->n - p->i >= 4 && memcmp(p->p + p->i, "null", 4) == 0) { p->i += 4; return jnew(J_NULL); } 284 if (p->n - p->i >= 4 && memcmp(p->p + p->i, "true", 4) == 0) { p->i += 4; J *j = jnew(J_BOOL); if (j) j->b = 1; return j; } 285 if (p->n - p->i >= 5 && memcmp(p->p + p->i, "false", 5) == 0) { p->i += 5; return jnew(J_BOOL); } 286 if (c == '-' || (c >= '0' && c <= '9')) { 287 size_t s = p->i; 288 if (c == '-') p->i++; 289 size_t d0 = p->i; 290 while (p->i < p->n && p->p[p->i] >= '0' && p->p[p->i] <= '9') p->i++; 291 if (p->i == d0) return jfail(p, "bad number"); 292 if (p->p[d0] == '0' && p->i - d0 > 1) return jfail(p, "leading zero"); 293 int plain = (c != '-'); 294 if (p->i < p->n && (p->p[p->i] == '.' || p->p[p->i] == 'e' || p->p[p->i] == 'E')) { 295 plain = 0; 296 p->i++; 297 while (p->i < p->n && ((p->p[p->i] >= '0' && p->p[p->i] <= '9') || p->p[p->i] == '+' || p->p[p->i] == '-' || 298 p->p[p->i] == 'e' || p->p[p->i] == 'E' || p->p[p->i] == '.')) p->i++; 299 } 300 J *j = jnew(J_NUM); 301 if (!j) return jfail(p, "out of memory"); 302 if (plain) { 303 uint64_t v = 0; 304 int ok = (p->i - s) <= 20; 305 for (size_t k = s; ok && k < p->i; k++) { 306 uint64_t dgt = (uint64_t)(p->p[k] - '0'); 307 if (v > (UINT64_MAX - dgt) / 10) ok = 0; else v = v * 10 + dgt; 308 } 309 j->is_uint = ok; 310 j->u = v; 311 } 312 return j; 313 } 314 return jfail(p, "unexpected character"); 315 } 316 317 static const J *oget(const J *o, const char *key) 318 { 319 for (size_t i = 0; i < o->n; i++) if (strcmp(o->keys[i], key) == 0) return o->v[i]; 320 return NULL; 321 } 322 323 /* ---------------------------------------------------------------- receipt extraction */ 324 typedef struct { char *err; size_t cap; } Er; 325 static int ef(Er *e, const char *fmt, const char *a) { seterr(e->err, e->cap, fmt, a); return -1; } 326 327 static int want_str(Er *e, const J *o, const char *key, int required, char **out) 328 { 329 const J *v = oget(o, key); 330 if (!v) { 331 if (required) return ef(e, "receipt: missing field %s", key); 332 *out = strdup(""); 333 return *out ? 0 : ef(e, "receipt: out of memory%s", ""); 334 } 335 if (v->k != J_STR) return ef(e, "receipt: field %s must be a string", key); 336 *out = strdup(v->s); 337 return *out ? 0 : ef(e, "receipt: out of memory%s", ""); 338 } 339 static int want_strlist(Er *e, const J *o, const char *key, int required, char ***out, size_t *n) 340 { 341 const J *v = oget(o, key); 342 *out = NULL; *n = 0; 343 if (!v) return required ? ef(e, "receipt: missing field %s", key) : 0; 344 if (v->k != J_ARR) return ef(e, "receipt: field %s must be a list", key); 345 if (v->n > 4096) return ef(e, "receipt: field %s is too long", key); 346 if (v->n == 0) return 0; 347 char **a = calloc(v->n, sizeof *a); 348 if (!a) return ef(e, "receipt: out of memory%s", ""); 349 *out = a; *n = v->n; 350 for (size_t i = 0; i < v->n; i++) { 351 if (v->v[i]->k != J_STR) return ef(e, "receipt: list %s must hold strings", key); 352 a[i] = strdup(v->v[i]->s); 353 if (!a[i]) return ef(e, "receipt: out of memory%s", ""); 354 } 355 return 0; 356 } 357 static int want_bool(Er *e, const J *o, const char *key, int *out) 358 { 359 const J *v = oget(o, key); 360 if (!v) return ef(e, "receipt: missing field %s", key); 361 if (v->k != J_BOOL) return ef(e, "receipt: field %s must be true or false", key); 362 *out = v->b; 363 return 0; 364 } 365 static int want_u64(Er *e, const J *o, const char *key, uint64_t *out) 366 { 367 const J *v = oget(o, key); 368 if (!v) return ef(e, "receipt: missing field %s", key); 369 if (v->k != J_NUM || !v->is_uint) return ef(e, "receipt: field %s must be an unsigned integer", key); 370 *out = v->u; 371 return 0; 372 } 373 static int want_mut(Er *e, const J *o, const char *key, uint8_t *out) 374 { 375 char *s = NULL; 376 if (want_str(e, o, key, 1, &s)) return -1; 377 int found = 0; 378 for (size_t i = 0; i < N_MUTS; i++) if (strcmp(MUTS[i].name, s) == 0) { *out = (uint8_t)i; found = 1; } 379 free(s); 380 return found ? 0 : ef(e, "receipt: unknown mutation in %s", key); 381 } 382 383 void omega_receipt_free(OmegaReceipt *r) 384 { 385 if (!r) return; 386 char **sv[] = { &r->kind, &r->tier, &r->repo, &r->commit, &r->toolchain, &r->procedure, &r->machine, 387 &r->env_class, &r->authority, &r->lease_resource }; 388 for (size_t i = 0; i < sizeof sv / sizeof sv[0]; i++) { free(*sv[i]); *sv[i] = NULL; } 389 char ***lv[] = { &r->input_artifacts, &r->output_artifacts, &r->dependencies, &r->external_refs, &r->required_features }; 390 size_t *nv[] = { &r->n_input, &r->n_output, &r->n_dependencies, &r->n_external, &r->n_features }; 391 for (size_t k = 0; k < 5; k++) { 392 if (*lv[k]) for (size_t i = 0; i < *nv[k]; i++) free((*lv[k])[i]); 393 free(*lv[k]); *lv[k] = NULL; *nv[k] = 0; 394 } 395 if (r->assertions) { 396 for (size_t i = 0; i < r->n_assertions; i++) { 397 free(r->assertions[i].id); free(r->assertions[i].expected); free(r->assertions[i].observed); 398 free(r->assertions[i].source); free(r->assertions[i].note); 399 } 400 free(r->assertions); 401 } 402 r->assertions = NULL; r->n_assertions = 0; 403 } 404 405 static int cmp_str(const void *a, const void *b) { return strcmp(*(char *const *)a, *(char *const *)b); } 406 407 static int check_digest_list(Er *e, const char *name, char **items, size_t n) 408 { 409 for (size_t i = 0; i < n; i++) { 410 const char *it = items[i], *colon = strchr(it, ':'); 411 if (colon) { 412 size_t al = (size_t)(colon - it); 413 int ok = (al == 6 && memcmp(it, "blake3", 6) == 0) || (al == 6 && memcmp(it, "sha256", 6) == 0) || 414 (al == 6 && memcmp(it, "sha512", 6) == 0); 415 if (!ok) return ef(e, "receipt: unknown digest algorithm in %s", name); 416 const char *hx = colon + 1; 417 size_t hl = strlen(hx); 418 if (hl < 32 || hl % 2 || !is_hex(hx)) return ef(e, "receipt: bad digest hex in %s", name); 419 } else if (!(strlen(it) == 64 && is_hex(it))) { 420 return ef(e, "receipt: digest must be : or 64 hex digits in %s", name); 421 } 422 } 423 return 0; 424 } 425 426 static int all_known_key(const J *o) 427 { 428 static const char *const keys[] = { "schema", "version", "id", "kind", "tier", "result", "timestamp", "repo", "commit", 429 "dirty", "toolchain", "procedure", "machine", "env_class", "input_artifacts", "output_artifacts", "assertions", 430 "dependencies", "declared_mutation", "observed_mutation", "authority", "output_digest", "external_refs", "ledger", 431 "lease", "required_features", "reserved" }; 432 for (size_t i = 0; i < o->n; i++) { 433 int ok = 0; 434 for (size_t k = 0; k < sizeof keys / sizeof keys[0]; k++) if (strcmp(keys[k], o->keys[i]) == 0) ok = 1; 435 if (!ok) return 0; 436 } 437 return 1; 438 } 439 440 int omega_receipt_parse(const uint8_t *json, size_t len, OmegaReceipt *r, char *err, size_t errcap) 441 { 442 Er e = { err, errcap }; 443 memset(r, 0, sizeof *r); 444 if (!json || len == 0 || len > OMEGA_RECEIPT_MAX_BYTES) return ef(&e, "receipt: empty or oversize input%s", ""); 445 JP p = { json, len, 0, 0, { 0 } }; 446 J *root = jvalue(&p); 447 if (root) { jws(&p); if (p.i != p.n) { jfree(root); root = NULL; jfail(&p, "trailing bytes after the receipt"); } } 448 if (!root) return ef(&e, "%s", p.err); 449 int rc = -1; 450 char *schema = NULL, *idhex = NULL, *resultstr = NULL, *reserved = NULL, *odigest = NULL; 451 if (root->k != J_OBJ) { ef(&e, "receipt: top level must be an object%s", ""); goto done; } 452 if (!all_known_key(root)) { ef(&e, "receipt: unknown field%s", ""); goto done; } /* VC1R:deny-unknown */ 453 uint64_t ver = 0; 454 if (want_str(&e, root, "schema", 1, &schema) || want_u64(&e, root, "version", &ver) || 455 want_str(&e, root, "id", 1, &idhex) || want_str(&e, root, "kind", 1, &r->kind) || 456 want_str(&e, root, "tier", 1, &r->tier) || want_str(&e, root, "result", 1, &resultstr) || 457 want_u64(&e, root, "timestamp", &r->timestamp) || want_str(&e, root, "repo", 1, &r->repo) || 458 want_str(&e, root, "commit", 1, &r->commit) || want_bool(&e, root, "dirty", &r->dirty) || 459 want_str(&e, root, "toolchain", 1, &r->toolchain) || want_str(&e, root, "procedure", 1, &r->procedure) || 460 want_str(&e, root, "machine", 1, &r->machine) || want_str(&e, root, "env_class", 1, &r->env_class) || 461 want_strlist(&e, root, "input_artifacts", 1, &r->input_artifacts, &r->n_input) || 462 want_strlist(&e, root, "output_artifacts", 1, &r->output_artifacts, &r->n_output) || 463 want_strlist(&e, root, "dependencies", 1, &r->dependencies, &r->n_dependencies) || 464 want_mut(&e, root, "declared_mutation", &r->declared_mutation) || 465 want_mut(&e, root, "observed_mutation", &r->observed_mutation) || 466 want_str(&e, root, "authority", 0, &r->authority) || want_str(&e, root, "output_digest", 1, &odigest) || 467 want_strlist(&e, root, "external_refs", 0, &r->external_refs, &r->n_external) || 468 want_strlist(&e, root, "required_features", 0, &r->required_features, &r->n_features) || 469 want_str(&e, root, "reserved", 0, &reserved)) 470 goto done; 471 if (ver > 0xFFFFFFFFu) { ef(&e, "receipt: version out of range%s", ""); goto done; } 472 r->version = (uint32_t)ver; 473 /* ---- the validations of canonical_bytes, in its order ---- */ 474 if (strcmp(schema, SCHEMA) != 0) { ef(&e, "receipt: unknown receipt schema%s", ""); goto done; } /* VC1R:schema */ 475 if (r->version != 1) { ef(&e, "receipt: unsupported receipt version%s", ""); goto done; } /* VC1R:version */ 476 for (size_t i = 0; i < r->n_features; i++) 477 if (strcmp(r->required_features[i], "v1-base") != 0) { ef(&e, "receipt: unknown required receipt feature%s", ""); goto done; } /* VC1R:feature */ 478 if (reserved[0]) { ef(&e, "receipt: reserved field must be empty%s", ""); goto done; } /* VC1R:reserved */ 479 if (!r->kind[0]) { ef(&e, "receipt: kind must not be empty%s", ""); goto done; } 480 if (!r->repo[0]) { ef(&e, "receipt: repo must not be empty%s", ""); goto done; } 481 if (!((strlen(r->commit) == 40 || strlen(r->commit) == 64) && is_lower_hex(r->commit))) { 482 ef(&e, "receipt: commit must be 40 or 64 lowercase hex digits%s", ""); goto done; 483 } 484 if (!r->procedure[0]) { ef(&e, "receipt: procedure identity must not be empty%s", ""); goto done; } 485 if (!r->machine[0]) { ef(&e, "receipt: machine identity must not be empty%s", ""); goto done; } 486 r->tier_rank = omega_tier_rank(r->tier); 487 if (r->tier_rank < 0) { ef(&e, "receipt: unknown tier%s", ""); goto done; } 488 r->result = 255; 489 for (size_t i = 0; i < sizeof VERDICTS / sizeof VERDICTS[0]; i++) if (strcmp(VERDICTS[i], resultstr) == 0) r->result = (uint8_t)i; 490 if (r->result == 255) { ef(&e, "receipt: unknown result%s", ""); goto done; } 491 if (strcmp(r->env_class, r->tier) != 0) { ef(&e, "receipt: env_class contradicts tier%s", ""); goto done; } /* VC1R:env-class */ 492 if (check_digest_list(&e, "input_artifacts", r->input_artifacts, r->n_input) || 493 check_digest_list(&e, "output_artifacts", r->output_artifacts, r->n_output)) goto done; 494 if (hex32(odigest, r->output_digest)) { ef(&e, "receipt: output_digest must be 64 hex digits%s", ""); goto done; } 495 if (hex32(idhex, r->id)) { ef(&e, "receipt: id must be 64 hex digits%s", ""); goto done; } 496 /* assertions */ 497 const J *av = oget(root, "assertions"); 498 if (!av) { ef(&e, "receipt: missing field %s", "assertions"); goto done; } 499 if (av->k != J_ARR || av->n > 4096) { ef(&e, "receipt: assertions must be a list%s", ""); goto done; } 500 if (av->n) { 501 r->assertions = calloc(av->n, sizeof *r->assertions); 502 if (!r->assertions) { ef(&e, "receipt: out of memory%s", ""); goto done; } 503 r->n_assertions = av->n; 504 } 505 for (size_t i = 0; i < av->n; i++) { 506 const J *a = av->v[i]; 507 if (a->k != J_OBJ) { ef(&e, "receipt: an assertion must be an object%s", ""); goto done; } 508 for (size_t k = 0; k < a->n; k++) { 509 static const char *const ak[] = { "id", "expected", "observed", "pass", "source", "note" }; 510 int ok = 0; 511 for (size_t q = 0; q < 6; q++) if (strcmp(ak[q], a->keys[k]) == 0) ok = 1; 512 if (!ok) { ef(&e, "receipt: unknown assertion field%s", ""); goto done; } 513 } 514 OmegaReceiptAssertion *as = &r->assertions[i]; 515 if (want_str(&e, a, "id", 1, &as->id) || want_str(&e, a, "expected", 0, &as->expected) || 516 want_str(&e, a, "observed", 0, &as->observed) || want_bool(&e, a, "pass", &as->pass) || 517 want_str(&e, a, "source", 0, &as->source) || want_str(&e, a, "note", 0, &as->note)) 518 goto done; 519 } 520 if (r->n_assertions == 0 && r->result == OMEGA_RECEIPT_PASS) { ef(&e, "receipt: PASS receipts must carry at least one assertion%s", ""); goto done; } /* VC1R:pass-needs-assertion */ 521 for (size_t i = 0; i < r->n_assertions; i++) { 522 if (!r->assertions[i].id[0]) { ef(&e, "receipt: assertion id must not be empty%s", ""); goto done; } 523 for (size_t k = 0; k < i; k++) 524 if (strcmp(r->assertions[k].id, r->assertions[i].id) == 0) { ef(&e, "receipt: duplicate assertion id%s", ""); goto done; } 525 } 526 for (size_t i = 0; i < r->n_dependencies; i++) { 527 uint8_t tmp[32]; 528 if (hex32(r->dependencies[i], tmp)) { ef(&e, "receipt: dependency is not a 64 digit receipt hash%s", ""); goto done; } 529 } 530 if (MUTS[r->observed_mutation].sev > MUTS[r->declared_mutation].sev) { ef(&e, "receipt: observed mutation exceeds declared mutation%s", ""); goto done; } /* VC1R:mutation-order */ 531 { 532 int max = r->tier_rank == 4 ? 2 : r->tier_rank == 5 ? 5 : (r->tier_rank >= 6) ? 255 : 4; 533 if (MUTS[r->observed_mutation].sev > max) { ef(&e, "receipt: tier cannot claim that observed mutation%s", ""); goto done; } 534 if (r->tier_rank == 4 && r->observed_mutation >= 4) { ef(&e, "receipt: a read-only run cannot claim that mutation%s", ""); goto done; } 535 } 536 if (r->tier_rank == 7) { 537 size_t a = 0, b = strlen(r->authority); 538 while (a < b && (r->authority[a] == ' ' || r->authority[a] == '\t' || r->authority[a] == '\n' || r->authority[a] == '\r')) a++; 539 if (a == b) { ef(&e, "receipt: PRODUCTION receipts require an authority reference%s", ""); goto done; } /* VC1R:prod-authority */ 540 char *low = strdup(r->authority); 541 if (!low) { ef(&e, "receipt: out of memory%s", ""); goto done; } 542 for (char *c = low; *c; c++) if (*c >= 'A' && *c <= 'Z') *c = (char)(*c - 'A' + 'a'); 543 int bad = strstr(low, "test-only") || strstr(low, "test_only") || strstr(low, "testonly"); /* VC1R:prod-spelling */ 544 free(low); 545 if (bad) { ef(&e, "receipt: a TEST_ONLY signer cannot produce a PRODUCTION receipt%s", ""); goto done; } /* VC1R:prod-bad-refused */ 546 } 547 /* ledger and lease: null or an object (deny_unknown_fields) */ 548 const J *lg = oget(root, "ledger"); 549 if (lg && lg->k != J_NULL) { 550 if (lg->k != J_OBJ) { ef(&e, "receipt: ledger must be null or an object%s", ""); goto done; } 551 for (size_t k = 0; k < lg->n; k++) 552 if (strcmp(lg->keys[k], "index") && strcmp(lg->keys[k], "hash")) { ef(&e, "receipt: unknown ledger field%s", ""); goto done; } 553 char *h = NULL; 554 if (want_u64(&e, lg, "index", &r->ledger_index) || want_str(&e, lg, "hash", 1, &h)) goto done; 555 int bad = hex32(h, r->ledger_hash); 556 free(h); 557 if (bad) { ef(&e, "receipt: ledger hash must be 64 hex digits%s", ""); goto done; } 558 r->has_ledger = 1; 559 } 560 const J *ls = oget(root, "lease"); 561 if (ls && ls->k != J_NULL) { 562 if (ls->k != J_OBJ) { ef(&e, "receipt: lease must be null or an object%s", ""); goto done; } 563 for (size_t k = 0; k < ls->n; k++) 564 if (strcmp(ls->keys[k], "hold_id") && strcmp(ls->keys[k], "resource")) { ef(&e, "receipt: unknown lease field%s", ""); goto done; } 565 char *h = NULL; 566 if (want_str(&e, ls, "hold_id", 1, &h) || want_str(&e, ls, "resource", 1, &r->lease_resource)) { free(h); goto done; } 567 int bad = hex32(h, r->lease_hold); 568 free(h); 569 if (bad) { ef(&e, "receipt: lease hold_id must be 64 hex digits%s", ""); goto done; } 570 size_t a = 0, b = strlen(r->lease_resource); 571 while (a < b && (r->lease_resource[a] == ' ' || r->lease_resource[a] == '\t' || r->lease_resource[a] == '\n' || r->lease_resource[a] == '\r')) a++; 572 if (a == b) { ef(&e, "receipt: lease resource must not be empty%s", ""); goto done; } 573 r->has_lease = 1; 574 } 575 rc = 0; 576 done: 577 free(schema); free(idhex); free(resultstr); free(reserved); free(odigest); 578 jfree(root); 579 if (rc) omega_receipt_free(r); 580 return rc; 581 } 582 583 /* ---------------------------------------------------------------- canonical bytes */ 584 typedef struct { uint8_t *p; size_t n, cap; int oom; } Buf; 585 static void bput(Buf *b, const void *d, size_t k) 586 { 587 if (b->oom) return; 588 if (b->n + k > b->cap) { 589 size_t nc = (b->n + k) * 2 + 256; 590 uint8_t *t = realloc(b->p, nc); 591 if (!t) { b->oom = 1; return; } 592 b->p = t; b->cap = nc; 593 } 594 if (k) memcpy(b->p + b->n, d, k); 595 b->n += k; 596 } 597 static void bu8(Buf *b, uint8_t v) { bput(b, &v, 1); } 598 static void bu32(Buf *b, uint32_t v) { uint8_t t[4] = { (uint8_t)(v >> 24), (uint8_t)(v >> 16), (uint8_t)(v >> 8), (uint8_t)v }; bput(b, t, 4); } 599 static void bu64(Buf *b, uint64_t v) { bu32(b, (uint32_t)(v >> 32)); bu32(b, (uint32_t)v); } 600 static void bstr(Buf *b, const char *s) { size_t n = strlen(s); bu64(b, n); bput(b, s, n); } 601 602 /* sorted, de-duplicated copy of the pointers (BTreeSet<&str>: byte order) */ 603 static char **sorted_unique(char **items, size_t n, size_t *out_n) 604 { 605 char **c = malloc((n ? n : 1) * sizeof *c); 606 if (!c) return NULL; 607 if (n) memcpy(c, items, n * sizeof *c); 608 qsort(c, n, sizeof *c, cmp_str); /* VC1R:canon-sort */ 609 size_t m = 0; 610 for (size_t i = 0; i < n; i++) if (m == 0 || strcmp(c[m - 1], c[i]) != 0) c[m++] = c[i]; 611 *out_n = m; 612 return c; 613 } 614 static int cmp_assert(const void *a, const void *b) 615 { 616 return strcmp(((const OmegaReceiptAssertion *)a)->id, ((const OmegaReceiptAssertion *)b)->id); 617 } 618 619 int omega_receipt_canonical(const OmegaReceipt *r, uint8_t **out, size_t *len) 620 { 621 Buf b = { 0 }; 622 bput(&b, DOMAIN, sizeof DOMAIN - 1); 623 bu32(&b, 1); 624 bstr(&b, r->kind); 625 bstr(&b, r->tier); 626 bu8(&b, r->result); 627 bstr(&b, r->repo); 628 bstr(&b, r->commit); 629 bu8(&b, (uint8_t)(r->dirty ? 1 : 0)); 630 bstr(&b, r->toolchain); 631 bstr(&b, r->procedure); 632 bstr(&b, r->machine); 633 bstr(&b, r->env_class); 634 size_t m; 635 char **s = sorted_unique(r->input_artifacts, r->n_input, &m); 636 if (!s) return -1; 637 bu32(&b, (uint32_t)m); 638 for (size_t i = 0; i < m; i++) bstr(&b, s[i]); 639 free(s); 640 s = sorted_unique(r->output_artifacts, r->n_output, &m); 641 if (!s) return -1; 642 bu32(&b, (uint32_t)m); 643 for (size_t i = 0; i < m; i++) bstr(&b, s[i]); 644 free(s); 645 OmegaReceiptAssertion *as = malloc((r->n_assertions ? r->n_assertions : 1) * sizeof *as); 646 if (!as) return -1; 647 if (r->n_assertions) memcpy(as, r->assertions, r->n_assertions * sizeof *as); 648 qsort(as, r->n_assertions, sizeof *as, cmp_assert); 649 bu32(&b, (uint32_t)r->n_assertions); 650 for (size_t i = 0; i < r->n_assertions; i++) { 651 bstr(&b, as[i].id); bstr(&b, as[i].expected); bstr(&b, as[i].observed); 652 bu8(&b, (uint8_t)(as[i].pass ? 1 : 0)); 653 bstr(&b, as[i].source); bstr(&b, as[i].note); 654 } 655 free(as); 656 s = sorted_unique(r->dependencies, r->n_dependencies, &m); 657 if (!s) return -1; 658 bu32(&b, (uint32_t)m); 659 for (size_t i = 0; i < m; i++) { 660 uint8_t raw[32]; 661 if (hex32(s[i], raw)) { free(s); free(b.p); return -1; } 662 bput(&b, raw, 32); 663 } 664 free(s); 665 bu8(&b, MUTS[r->declared_mutation].code); 666 bu8(&b, MUTS[r->observed_mutation].code); 667 bstr(&b, r->authority); 668 bput(&b, r->output_digest, 32); 669 s = sorted_unique(r->external_refs, r->n_external, &m); 670 if (!s) return -1; 671 bu32(&b, (uint32_t)m); 672 for (size_t i = 0; i < m; i++) bstr(&b, s[i]); 673 free(s); 674 if (r->has_ledger) { bu8(&b, 1); bu64(&b, r->ledger_index); bput(&b, r->ledger_hash, 32); } 675 else bu8(&b, 0); 676 if (r->has_lease) { bu8(&b, 1); bput(&b, r->lease_hold, 32); bstr(&b, r->lease_resource); } 677 else bu8(&b, 0); 678 bu64(&b, r->timestamp); 679 s = sorted_unique(r->required_features, r->n_features, &m); 680 if (!s) return -1; 681 bu32(&b, (uint32_t)m); 682 for (size_t i = 0; i < m; i++) bstr(&b, s[i]); 683 free(s); 684 bu32(&b, 0); 685 if (b.oom) { free(b.p); return -1; } 686 *out = b.p; 687 *len = b.n; 688 return 0; 689 } 690 691 int omega_receipt_compute_id(const OmegaReceipt *r, uint8_t out[32]) 692 { 693 uint8_t *c = NULL; 694 size_t n = 0; 695 if (omega_receipt_canonical(r, &c, &n)) return -1; 696 omega_blake3_hash(c, n, out); 697 free(c); 698 return 0; 699 } 700 701 int omega_receipt_verify_bytes(const uint8_t *json, size_t len, OmegaReceipt *r, uint8_t out_id[32], char *err, size_t errcap) 702 { 703 if (omega_receipt_parse(json, len, r, err, errcap)) return -1; 704 if (omega_receipt_compute_id(r, out_id)) { seterr(err, errcap, "receipt: cannot compute canonical identity%s", ""); omega_receipt_free(r); return -1; } 705 if (memcmp(out_id, r->id, 32) != 0) { /* VC1R:id-compare */ 706 seterr(err, errcap, "receipt id mismatch: the file claims one id, its canonical bytes hash to another%s", ""); 707 omega_receipt_free(r); 708 return -1; 709 } 710 return 0; 711 }