aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tests / test_osc_receipt.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 | /* test_osc_receipt.c -- host test for artifact/osc_receipt.c (Omega realization receipt v1, aienos#383) under |
| 18 | * the kernel's own policy (core/osc_admit.c ck_osc_policy, linked in). |
| 19 | * |
| 20 | * Built twice: with CK_SEED0B_TEST_ANCHOR=1 (the qualification policy: checker2's TEST key anchored) and without |
| 21 | * it (the release policy: release mode, the owner root as the only receipt anchor). Checks: |
| 22 | * - every line of tests/fixtures/osc_receipt/expected.txt: the receipt, minted by omega checker2, verified |
| 23 | * against the admitted unit gives the stated kernel verdict (checker2's own verdict for the same policy is |
| 24 | * recorded beside it by make-receipts.sh); in the release build every one is refused; |
| 25 | * - the trust inputs: the TEST key header against the fixtures, the pinned checker identity, the target hash, |
| 26 | * the revoked fixture id, the owner root as the one OWNER receipt anchor (aienos#387), never a TEST anchor; |
| 27 | * - each structure reason, the order of the checks, anchor rules, epoch window edges, every single-bit flip; |
| 28 | * - the owner-signed receipt (fixtures/osc_receipt_owner, aienos#387): VALID under the release policy, every |
| 29 | * single-bit flip and each binding, epoch and revocation control refused, TEST receipts still refused; |
| 30 | * - the Store receipt table of osc_admit.c (kept by name, oversize, full). |
| 31 | * The mutation run (Makefile osc-receipt-mutants) requires this test to FAIL with any one MUT guard removed. */ |
| 32 | #include <stdarg.h> |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include "ck_test.h" |
| 37 | #include "osc_admit.h" |
| 38 | #include "osc_receipt.h" |
| 39 | #include "osc_receipt_anchor.h" |
| 40 | #include "osc_receipt_test_anchor.h" |
| 41 | #include "osc_unit.h" |
| 42 | #include "osc_unit_test_anchor.h" |
| 43 | #include "sha256.h" |
| 44 | #include "aienos_sig.h" |
| 45 | |
| 46 | #ifndef OSC_FIXTURE_DIR |
| 47 | #define OSC_FIXTURE_DIR "native/kernel/tests/fixtures" |
| 48 | #endif |
| 49 | |
| 50 | static char out[8192]; |
| 51 | static size_t outn; |
| 52 | void ck_printf(const char *fmt, ...) |
| 53 | { |
| 54 | va_list ap; |
| 55 | va_start(ap, fmt); |
| 56 | int n = vsnprintf(out + outn, sizeof out - outn, fmt, ap); |
| 57 | va_end(ap); |
| 58 | if (n > 0 && outn + (size_t)n < sizeof out) outn += (size_t)n; |
| 59 | } |
| 60 | |
| 61 | static size_t slurp(const char *rel, uint8_t *buf, size_t cap) |
| 62 | { |
| 63 | char path[512]; |
| 64 | snprintf(path, sizeof path, "%s/%s", OSC_FIXTURE_DIR, rel); |
| 65 | FILE *f = fopen(path, "rb"); |
| 66 | if (!f) { printf(" cannot open %s\n", path); return 0; } |
| 67 | size_t n = fread(buf, 1, cap, f); |
| 68 | fclose(f); |
| 69 | return n; |
| 70 | } |
| 71 | |
| 72 | static const char *unit_path(const char *u) |
| 73 | { |
| 74 | if (!strcmp(u, "a01")) return "osc_unit/vectors/a01_valid_min.unit"; |
| 75 | if (!strcmp(u, "l01")) return "osc_unit/launch/l01_launch_fns.unit"; |
| 76 | if (!strcmp(u, "osh_lex")) return "osh/osh_lex.unit"; |
| 77 | if (!strcmp(u, "osh_parse")) return "osh/osh_parse.unit"; |
| 78 | if (!strcmp(u, "osh_expand")) return "osh/osh_expand.unit"; |
| 79 | return "?"; |
| 80 | } |
| 81 | |
| 82 | /* the unit admitted with the container TEST key (admission is not what is tested here) */ |
| 83 | static int admit(const char *u, struct osc_accept *a) |
| 84 | { |
| 85 | static uint8_t buf[OSC_UNIT_MAX_BYTES]; |
| 86 | size_t n = slurp(unit_path(u), buf, sizeof buf); |
| 87 | struct osc_policy p; |
| 88 | memset(&p, 0, sizeof p); |
| 89 | static const uint8_t (*const ta)[32] = &osc_unit_test1_pk; |
| 90 | p.test_anchors = ta; |
| 91 | p.n_test = 1; |
| 92 | p.unit_formats = OSC_PROFILE_UNIT_FORMATS; |
| 93 | p.abi_versions = OSC_PROFILE_ABI; |
| 94 | p.cap_domains = 1u << 1; |
| 95 | return n && osc_unit_admit(buf, n, &p, a) == OSC_OK; |
| 96 | } |
| 97 | |
| 98 | static size_t rcpt(const char *name, uint8_t *b) |
| 99 | { |
| 100 | char rel[128]; |
| 101 | snprintf(rel, sizeof rel, "osc_receipt/%s", name); |
| 102 | return slurp(rel, b, 1024); |
| 103 | } |
| 104 | |
| 105 | /* "VALID" -> 0, else the kernel code whose name is RECEIPT_<name>; ~0u if unknown */ |
| 106 | static unsigned code_of(const char *name) |
| 107 | { |
| 108 | if (!strcmp(name, "VALID")) return 0; |
| 109 | for (unsigned c = 64; c < 96; c++) { |
| 110 | const char *k = osc_code_name(c); |
| 111 | if (!strncmp(k, "RECEIPT_", 8) && !strcmp(k + 8, name)) return c; |
| 112 | } |
| 113 | return ~0u; |
| 114 | } |
| 115 | |
| 116 | static void hex(const uint8_t *p, size_t n, char *o) |
| 117 | { |
| 118 | for (size_t i = 0; i < n; i++) sprintf(o + 2 * i, "%02x", p[i]); |
| 119 | } |
| 120 | |
| 121 | static uint8_t test_fp[1][32], producer[32]; |
| 122 | |
| 123 | static void apply_variant(struct osc_policy *p, const char *v) |
| 124 | { |
| 125 | if (!strcmp(v, "release")) p->release = 1; |
| 126 | if (!strcmp(v, "revoke_key")) { p->rcpt_revoked_keys = (const uint8_t (*)[32])test_fp; p->n_rcpt_revoked_keys = 1; } |
| 127 | if (!strcmp(v, "producer")) p->rcpt_producer = producer; |
| 128 | } |
| 129 | |
| 130 | static void expected_table(void) |
| 131 | { |
| 132 | char path[512], line[512]; |
| 133 | snprintf(path, sizeof path, "%s/osc_receipt/expected.txt", OSC_FIXTURE_DIR); |
| 134 | FILE *f = fopen(path, "r"); |
| 135 | CHECK(f != NULL); |
| 136 | if (!f) return; |
| 137 | int n = 0, refused = 0; |
| 138 | while (fgets(line, sizeof line, f)) { |
| 139 | if (line[0] == '#' || line[0] == '\n') continue; |
| 140 | char name[64], unit[64], variant[32], c2[64], kv[64], idhex[80]; |
| 141 | if (sscanf(line, "%63s unit=%63s variant=%31s checker2=%63s kernel=%63s id=%79s", name, unit, variant, c2, kv, |
| 142 | idhex) != 6) { CHECK(0); continue; } |
| 143 | uint8_t r[1024]; |
| 144 | size_t len = rcpt(name, r); |
| 145 | struct osc_accept a; |
| 146 | CHECK(admit(unit, &a)); |
| 147 | struct osc_policy p; |
| 148 | ck_osc_policy(&p); |
| 149 | apply_variant(&p, variant); |
| 150 | struct osc_receipt_info info; |
| 151 | unsigned got = osc_receipt_verify(r, len, &a, &p, &info), want = code_of(kv); |
| 152 | CHECK(want != ~0u); |
| 153 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 154 | if (got != want) printf(" %s unit=%s variant=%s: got %u %s, want %s\n", name, unit, variant, got, osc_code_name(got), kv); |
| 155 | CHECK(got == want); |
| 156 | /* where the kernel checks what checker2 checks, the verdicts agree (the oracle) */ |
| 157 | CHECK(!strcmp(c2, kv)); |
| 158 | #else |
| 159 | /* release policy: TEST receipts are refused; only a structure fault is reported before that */ |
| 160 | unsigned rel = want == OSC_RCPT_SIZE ? OSC_RCPT_SIZE : OSC_RCPT_TEST_SIGNER_IN_PRODUCTION; |
| 161 | if (got != rel) printf(" release %s: got %u %s\n", name, got, osc_code_name(got)); |
| 162 | CHECK(got == rel); |
| 163 | #endif |
| 164 | if (got) refused++; |
| 165 | if (len == OSC_RCPT_BYTES) { |
| 166 | char h[65]; |
| 167 | hex(info.id, 32, h); |
| 168 | CHECK(info.have_id && !strcmp(h, idhex)); |
| 169 | } |
| 170 | n++; |
| 171 | } |
| 172 | fclose(f); |
| 173 | CHECK(n == 22); |
| 174 | printf(" expected.txt: %d cases, %d refused\n", n, refused); |
| 175 | } |
| 176 | |
| 177 | static void trust_inputs(void) |
| 178 | { |
| 179 | uint8_t r[1024], h[32]; |
| 180 | CHECK(rcpt("a01.rcpt", r) == OSC_RCPT_BYTES); |
| 181 | /* the TEST key header is the key whose fingerprint checker2 wrote, and it verifies (a01 is VALID above) */ |
| 182 | sha256_hash(osc_rcpt_test_pk, 32, h); |
| 183 | CHECK(memcmp(h, r + 320, 32) == 0); |
| 184 | CHECK(memcmp(osc_rcpt_test_pk, osc_unit_test1_pk, 32) != 0); /* not the container TEST key */ |
| 185 | CHECK(memcmp(osc_rcpt_checker_ids[0], r + 144, 32) == 0); /* pinned checker identity = the minting checker2 */ |
| 186 | osc_receipt_target_hash(OSC_RCPT_TARGET_NAME, strlen(OSC_RCPT_TARGET_NAME), h); |
| 187 | CHECK(memcmp(h, r + 208, 32) == 0); /* target constant = what issue --target wrote */ |
| 188 | uint8_t rv[1024]; |
| 189 | size_t rl = rcpt("n_revoked_id.rcpt", rv); |
| 190 | sha256_hash(rv, rl, h); |
| 191 | CHECK(rl == OSC_RCPT_BYTES && memcmp(h, osc_rcpt_qual_revoked_id, 32) == 0); |
| 192 | struct osc_policy p; |
| 193 | ck_osc_policy(&p); |
| 194 | /* aienos#387: the owner root is the one OWNER receipt anchor in both builds, and it is not a TEST anchor */ |
| 195 | CHECK(p.n_rcpt_owner == 1 && p.rcpt_owner_anchors != NULL && |
| 196 | memcmp(p.rcpt_owner_anchors[0], osc_rcpt_owner_root_pk, 32) == 0); |
| 197 | CHECK(p.n_rcpt_checkers == 1 && p.rcpt_target != NULL && p.rcpt_producer == NULL && p.rcpt_firmware == NULL); |
| 198 | CHECK(p.rcpt_epoch_floor == 10 && p.rcpt_epoch_now == 20 && p.n_rcpt_revoked_keys == 0); |
| 199 | for (unsigned i = 0; i < p.n_rcpt_test; i++) CHECK(memcmp(p.rcpt_test_anchors[i], osc_rcpt_owner_root_pk, 32) != 0); |
| 200 | for (unsigned i = 0; i < p.n_rcpt_owner; i++) CHECK(memcmp(p.rcpt_owner_anchors[i], osc_rcpt_test_pk, 32) != 0); |
| 201 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 202 | CHECK(!p.release && p.n_rcpt_test == 1 && memcmp(p.rcpt_test_anchors[0], osc_rcpt_test_pk, 32) == 0); |
| 203 | CHECK(p.n_rcpt_revoked_ids == 1); |
| 204 | #else |
| 205 | CHECK(p.release && p.n_rcpt_test == 0 && p.n_rcpt_revoked_ids == 0); |
| 206 | #endif |
| 207 | /* the owner root constant is the ceremony key of docs/OWNER_KEY_CEREMONY.md (aienos#380) */ |
| 208 | static const char owner_hex[] = "578ada3cb0c2d154623ac089191023c8ad4bc5e63b8ccd1a38db2d679b8c9e76"; |
| 209 | char oh[65]; |
| 210 | hex(osc_rcpt_owner_root_pk, 32, oh); |
| 211 | CHECK(!strcmp(oh, owner_hex)); |
| 212 | } |
| 213 | |
| 214 | /* the qualification policy, with the TEST anchor whatever this binary's build is */ |
| 215 | static struct osc_policy qual(void) |
| 216 | { |
| 217 | struct osc_policy p; |
| 218 | ck_osc_policy(&p); |
| 219 | static const uint8_t (*const rta)[32] = &osc_rcpt_test_pk; |
| 220 | static const uint8_t (*const rid)[32] = &osc_rcpt_qual_revoked_id; |
| 221 | p.release = 0; |
| 222 | p.rcpt_test_anchors = rta; |
| 223 | p.n_rcpt_test = 1; |
| 224 | p.rcpt_revoked_ids = rid; |
| 225 | p.n_rcpt_revoked_ids = 1; |
| 226 | return p; |
| 227 | } |
| 228 | |
| 229 | static void put16(uint8_t *p, unsigned v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } |
| 230 | static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } |
| 231 | |
| 232 | static unsigned v(const uint8_t *r, size_t len, const struct osc_accept *a, const struct osc_policy *p) |
| 233 | { |
| 234 | return osc_receipt_verify(r, len, a, p, NULL); |
| 235 | } |
| 236 | |
| 237 | static void structure_and_order(void) |
| 238 | { |
| 239 | uint8_t g[1024], r[1024]; |
| 240 | CHECK(rcpt("a01.rcpt", g) == OSC_RCPT_BYTES); |
| 241 | struct osc_accept a; |
| 242 | CHECK(admit("a01", &a)); |
| 243 | struct osc_policy p = qual(); |
| 244 | CHECK(v(g, 416, &a, &p) == 0); |
| 245 | CHECK(v(NULL, 416, &a, &p) == OSC_RCPT_MISSING); |
| 246 | CHECK(v(g, 0, &a, &p) == OSC_RCPT_MISSING); |
| 247 | CHECK(v(g, 416, NULL, &p) == OSC_RCPT_ARGUMENT); |
| 248 | CHECK(v(g, 416, &a, NULL) == OSC_RCPT_ARGUMENT); |
| 249 | CHECK(v(g, 415, &a, &p) == OSC_RCPT_SIZE); |
| 250 | CHECK(v(g, 417, &a, &p) == OSC_RCPT_SIZE); |
| 251 | #define MUTATE(stmt, want) do { memcpy(r, g, 416); stmt; unsigned got_ = v(r, 416, &a, &p); \ |
| 252 | if (got_ != (want)) { printf(" %s: got %u %s\n", #stmt, got_, osc_code_name(got_)); } \ |
| 253 | CHECK(got_ == (want)); } while (0) |
| 254 | MUTATE(r[0] ^= 1, OSC_RCPT_MAGIC); |
| 255 | MUTATE(r[7] = 1, OSC_RCPT_MAGIC); |
| 256 | MUTATE(put16(r + 8, 2), OSC_RCPT_VERSION); |
| 257 | MUTATE(put16(r + 8, 0), OSC_RCPT_VERSION); |
| 258 | MUTATE(put16(r + 12, 2), OSC_RCPT_SIGNATURE_ALGORITHM); |
| 259 | MUTATE(put16(r + 10, 3), OSC_RCPT_SIGNER_CLASS); |
| 260 | MUTATE(put16(r + 10, 0), OSC_RCPT_SIGNER_CLASS); |
| 261 | MUTATE(put16(r + 14, 3), OSC_RCPT_EVIDENCE_CLASS); |
| 262 | MUTATE(put16(r + 14, 0), OSC_RCPT_EVIDENCE_CLASS); |
| 263 | MUTATE(put16(r + 14, 2), OSC_RCPT_BAD_SIGNATURE); /* class 2 is well formed; the signature covers the change */ |
| 264 | MUTATE(put64(r + 312, 9), OSC_RCPT_EPOCH_ORDER); /* expiry 9 < policy epoch 10 */ |
| 265 | MUTATE(put64(r + 312, 10), OSC_RCPT_BAD_SIGNATURE); /* expiry == epoch is in order */ |
| 266 | /* order: the first failing step wins */ |
| 267 | MUTATE((r[0] ^= 1, put16(r + 8, 2)), OSC_RCPT_MAGIC); |
| 268 | MUTATE((put16(r + 8, 2), put16(r + 12, 2)), OSC_RCPT_VERSION); |
| 269 | MUTATE((put16(r + 12, 2), put16(r + 10, 3)), OSC_RCPT_SIGNATURE_ALGORITHM); |
| 270 | MUTATE((put16(r + 10, 3), put16(r + 14, 3)), OSC_RCPT_SIGNER_CLASS); |
| 271 | MUTATE((put16(r + 14, 3), put64(r + 312, 9)), OSC_RCPT_EVIDENCE_CLASS); |
| 272 | MUTATE((put64(r + 312, 9), r[400] ^= 1), OSC_RCPT_EPOCH_ORDER); |
| 273 | MUTATE(r[400] ^= 1, OSC_RCPT_BAD_SIGNATURE); |
| 274 | MUTATE(r[303] ^= 1, OSC_RCPT_BAD_SIGNATURE); /* last byte of the input digest, signed */ |
| 275 | MUTATE(r[351] ^= 1, OSC_RCPT_UNTRUSTED_SIGNER); /* last fingerprint byte */ |
| 276 | MUTATE(r[320] ^= 1, OSC_RCPT_UNTRUSTED_SIGNER); /* fingerprint names no anchor */ |
| 277 | MUTATE(put16(r + 10, 2), OSC_RCPT_UNTRUSTED_SIGNER); /* OWNER class: the TEST fingerprint names no owner anchor */ |
| 278 | p.release = 1; |
| 279 | MUTATE((void)0, OSC_RCPT_TEST_SIGNER_IN_PRODUCTION); |
| 280 | MUTATE(put64(r + 312, 9), OSC_RCPT_EPOCH_ORDER); /* structure before class policy */ |
| 281 | MUTATE(put16(r + 10, 2), OSC_RCPT_UNTRUSTED_SIGNER); |
| 282 | /* release: an owner anchor other than the listed owner root is not honoured (here the TEST key as "owner") */ |
| 283 | static const uint8_t (*const tk)[32] = &osc_rcpt_test_pk; |
| 284 | p.rcpt_owner_anchors = tk; |
| 285 | p.n_rcpt_owner = 1; |
| 286 | p.n_rcpt_test = 0; /* so the both-classes rule (guard 11) cannot answer for the owner-root rule (guard 10) */ |
| 287 | MUTATE(put16(r + 10, 2), OSC_RCPT_UNTRUSTED_SIGNER); |
| 288 | p.release = 0; |
| 289 | p.n_rcpt_test = 0; |
| 290 | MUTATE(put16(r + 10, 2), OSC_RCPT_BAD_SIGNATURE); /* qualification honours it, then the class byte breaks the sig */ |
| 291 | /* a key anchored in both classes is refused */ |
| 292 | p = qual(); |
| 293 | p.rcpt_owner_anchors = tk; |
| 294 | p.n_rcpt_owner = 1; |
| 295 | MUTATE((void)0, OSC_RCPT_UNTRUSTED_SIGNER); |
| 296 | p = qual(); |
| 297 | static const uint8_t (*const ok)[32] = &osc_rcpt_owner_root_pk; |
| 298 | p.rcpt_test_anchors = ok; /* wrong key in the TEST set */ |
| 299 | MUTATE((void)0, OSC_RCPT_UNTRUSTED_SIGNER); |
| 300 | p = qual(); |
| 301 | p.n_rcpt_test = 0; |
| 302 | MUTATE((void)0, OSC_RCPT_UNTRUSTED_SIGNER); |
| 303 | #undef MUTATE |
| 304 | } |
| 305 | |
| 306 | static void policy_edges(void) |
| 307 | { |
| 308 | uint8_t g[1024], rv[1024], fw[1024], np[1024]; |
| 309 | CHECK(rcpt("a01.rcpt", g) == OSC_RCPT_BYTES); |
| 310 | CHECK(rcpt("n_revoked_id.rcpt", rv) == OSC_RCPT_BYTES); |
| 311 | CHECK(rcpt("n_firmware.rcpt", fw) == OSC_RCPT_BYTES); |
| 312 | CHECK(rcpt("n_producer.rcpt", np) == OSC_RCPT_BYTES); |
| 313 | struct osc_accept a; |
| 314 | CHECK(admit("a01", &a)); |
| 315 | struct osc_policy p = qual(); |
| 316 | struct osc_receipt_info info; |
| 317 | /* revocation: the id is checked before the key */ |
| 318 | p.rcpt_revoked_keys = (const uint8_t (*)[32])test_fp; |
| 319 | p.n_rcpt_revoked_keys = 1; |
| 320 | CHECK(osc_receipt_verify(rv, 416, &a, &p, &info) == OSC_RCPT_REVOKED && info.revoked == OSC_RCPT_REVOKED_ID); |
| 321 | CHECK(osc_receipt_verify(g, 416, &a, &p, &info) == OSC_RCPT_REVOKED && info.revoked == OSC_RCPT_REVOKED_KEY); |
| 322 | p = qual(); |
| 323 | p.n_rcpt_revoked_ids = 0; |
| 324 | CHECK(v(rv, 416, &a, &p) == 0); |
| 325 | /* revocation before freshness: a revoked receipt that is also stale says REVOKED */ |
| 326 | p = qual(); |
| 327 | p.rcpt_epoch_floor = 12; |
| 328 | CHECK(v(rv, 416, &a, &p) == OSC_RCPT_REVOKED); |
| 329 | /* epoch window, inclusive at both ends (a01: policy epoch 10, expiry 1000) */ |
| 330 | p = qual(); |
| 331 | p.rcpt_epoch_now = 1000; |
| 332 | CHECK(v(g, 416, &a, &p) == 0); |
| 333 | p.rcpt_epoch_now = 1001; |
| 334 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_EXPIRED); |
| 335 | p.rcpt_epoch_now = 10; |
| 336 | CHECK(v(g, 416, &a, &p) == 0); |
| 337 | p.rcpt_epoch_now = 9; |
| 338 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_NOT_YET_VALID); |
| 339 | p.rcpt_epoch_now = 20; |
| 340 | p.rcpt_epoch_floor = 11; |
| 341 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_STALE_EPOCH); |
| 342 | p.rcpt_epoch_now = 2000; /* stale wins over expired */ |
| 343 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_STALE_EPOCH); |
| 344 | p = qual(); |
| 345 | /* checker list required; empty refuses every receipt */ |
| 346 | p.n_rcpt_checkers = 0; |
| 347 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_UNTRUSTED_CHECKER); |
| 348 | p = qual(); |
| 349 | p.rcpt_epoch_now = 2000; /* freshness before checker identity */ |
| 350 | p.n_rcpt_checkers = 0; |
| 351 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_EXPIRED); |
| 352 | /* binding: target required */ |
| 353 | p = qual(); |
| 354 | p.rcpt_target = NULL; |
| 355 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_WRONG_TARGET); |
| 356 | p = qual(); |
| 357 | p.n_rcpt_checkers = 0; /* checker before binding */ |
| 358 | p.rcpt_target = NULL; |
| 359 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_UNTRUSTED_CHECKER); |
| 360 | /* firmware: a policy that names one accepts exactly that one */ |
| 361 | p = qual(); |
| 362 | p.rcpt_firmware = fw + 240; |
| 363 | CHECK(v(fw, 416, &a, &p) == 0); |
| 364 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_WRONG_FIRMWARE); |
| 365 | /* producer: NULL means none (all zero); a policy that names one accepts exactly that one */ |
| 366 | p = qual(); |
| 367 | CHECK(v(np, 416, &a, &p) == OSC_RCPT_WRONG_PRODUCER); |
| 368 | p.rcpt_producer = np + 176; |
| 369 | CHECK(v(np, 416, &a, &p) == 0); |
| 370 | CHECK(v(g, 416, &a, &p) == OSC_RCPT_WRONG_PRODUCER); |
| 371 | /* the binding reads the admitted unit: a different code hash in the verdict is EXECUTABLE_MISMATCH, a different |
| 372 | * IR hash PROGRAM_MISMATCH (program before executable) */ |
| 373 | p = qual(); |
| 374 | struct osc_accept b = a; |
| 375 | b.code_sha256[31] ^= 1; |
| 376 | CHECK(v(g, 416, &b, &p) == OSC_RCPT_EXECUTABLE_MISMATCH); |
| 377 | b.ir_sha256[0] ^= 1; |
| 378 | CHECK(v(g, 416, &b, &p) == OSC_RCPT_PROGRAM_MISMATCH); |
| 379 | /* every single-bit flip of a valid receipt is refused */ |
| 380 | uint8_t r[416]; |
| 381 | int flips = 0, accepted = 0; |
| 382 | for (unsigned i = 0; i < 416; i++) |
| 383 | for (unsigned bit = 0; bit < 8; bit++) { |
| 384 | memcpy(r, g, 416); |
| 385 | r[i] ^= (uint8_t)(1u << bit); |
| 386 | if (v(r, 416, &a, &p) == 0) accepted++; |
| 387 | flips++; |
| 388 | } |
| 389 | CHECK(flips == 3328 && accepted == 0); |
| 390 | printf(" single-bit flips of a01.rcpt: %d refused of %d\n", flips - accepted, flips); |
| 391 | /* the verifier is pure: the same inputs give the same verdict and the inputs are not written */ |
| 392 | uint8_t before[416]; |
| 393 | memcpy(before, g, 416); |
| 394 | CHECK(v(g, 416, &a, &p) == 0 && v(g, 416, &a, &p) == 0 && memcmp(before, g, 416) == 0); |
| 395 | } |
| 396 | |
| 397 | /* Re-sign a receipt in memory with checker2's published TEST key (spec section 3: seed = SHA-256 of the stated |
| 398 | * string; the key is a TEST key by definition, never a release signer). Written from spec section 3; it must |
| 399 | * reproduce checker2's own signature on an unchanged receipt (Ed25519 is deterministic), which cross-checks the |
| 400 | * signed-message construction of the verifier from a second direction. */ |
| 401 | static void test_seed(uint8_t sk[32]) |
| 402 | { |
| 403 | static const char s[] = "OMEGA-CHECKER2-TEST-KEY-v1 NOT FOR PRODUCTION"; |
| 404 | sha256_hash((const uint8_t *)s, sizeof s - 1, sk); |
| 405 | } |
| 406 | |
| 407 | static void resign(uint8_t *r) |
| 408 | { |
| 409 | static const char body_dom[] = "OMEGA-REALIZATION-RECEIPT-V1", sig_dom[] = "OMEGA-REALIZATION-RECEIPT-SIG-V1"; |
| 410 | uint8_t msg[sizeof sig_dom + 32], sk[32]; |
| 411 | sha256_ctx c; |
| 412 | memcpy(msg, sig_dom, sizeof sig_dom); |
| 413 | sha256_init(&c); |
| 414 | sha256_update(&c, (const uint8_t *)body_dom, sizeof body_dom); |
| 415 | sha256_update(&c, r, OSC_RCPT_SIGNED_BYTES); |
| 416 | sha256_final(&c, msg + sizeof sig_dom); |
| 417 | test_seed(sk); |
| 418 | CHECK(aienos_ed25519_sign(r + OSC_RCPT_SIGNED_BYTES, msg, sizeof msg, sk) == AIENOS_SIG_OK); |
| 419 | memset(sk, 0, sizeof sk); |
| 420 | } |
| 421 | |
| 422 | static void resigned(void) |
| 423 | { |
| 424 | uint8_t g[1024], r[416], sk[32], pk[32]; |
| 425 | CHECK(rcpt("a01.rcpt", g) == OSC_RCPT_BYTES); |
| 426 | test_seed(sk); |
| 427 | CHECK(aienos_ed25519_public_key(pk, sk) == AIENOS_SIG_OK && memcmp(pk, osc_rcpt_test_pk, 32) == 0); |
| 428 | memcpy(r, g, 416); |
| 429 | memset(r + 352, 0, 64); |
| 430 | resign(r); |
| 431 | CHECK(memcmp(r, g, 416) == 0); /* same bytes as checker2 wrote */ |
| 432 | struct osc_accept a; |
| 433 | CHECK(admit("a01", &a)); |
| 434 | struct osc_policy p = qual(); |
| 435 | /* evidence class 2, correctly signed: refused LRAT_PROOFS_REQUIRED, and only after every section 6 step */ |
| 436 | memcpy(r, g, 416); |
| 437 | put16(r + 14, 2); |
| 438 | resign(r); |
| 439 | CHECK(v(r, 416, &a, &p) == OSC_RCPT_LRAT_PROOFS_REQUIRED); |
| 440 | memcpy(r, g, 416); |
| 441 | put16(r + 14, 2); |
| 442 | r[208] ^= 1; |
| 443 | resign(r); |
| 444 | CHECK(v(r, 416, &a, &p) == OSC_RCPT_WRONG_TARGET); |
| 445 | /* a signed class 1 receipt with a changed field reaches the step that reads that field */ |
| 446 | memcpy(r, g, 416); |
| 447 | r[272] ^= 1; /* input digest: NOT CHECKED, still valid */ |
| 448 | r[16] ^= 1; /* requirements digest: NOT CHECKED for class 1, still valid */ |
| 449 | resign(r); |
| 450 | CHECK(v(r, 416, &a, &p) == 0); |
| 451 | memcpy(r, g, 416); |
| 452 | put64(r + 304, 15); /* policy epoch 15, now 20: valid; now 14: NOT_YET_VALID */ |
| 453 | resign(r); |
| 454 | CHECK(v(r, 416, &a, &p) == 0); |
| 455 | p.rcpt_epoch_now = 14; |
| 456 | CHECK(v(r, 416, &a, &p) == OSC_RCPT_NOT_YET_VALID); |
| 457 | } |
| 458 | |
| 459 | /* aienos#387: the owner-signed receipt. fixtures/osc_receipt_owner/a01.owner.rcpt is omega |
| 460 | * tests/checker2/fixtures/owner-receipt/a01.owner.rcpt (omega#455, 710d438c) byte for byte, signed on the owner |
| 461 | * YubiKey (provenance in that folder). It is the one receipt here no TEST key made, so it is pinned by hash. */ |
| 462 | static const char owner_rcpt_sha256[] = "1348e78b0f88a5054f2b78b0a8d7c998ba70d166c711a2341528824685776034"; |
| 463 | |
| 464 | /* the release policy: what ck_osc_policy gives in a release build, also when this binary is the qualification |
| 465 | * build (there the TEST anchor and the fixture revocation are dropped and release mode is set) */ |
| 466 | static struct osc_policy rel(void) |
| 467 | { |
| 468 | struct osc_policy p; |
| 469 | ck_osc_policy(&p); |
| 470 | p.release = 1; |
| 471 | p.rcpt_test_anchors = NULL; |
| 472 | p.n_rcpt_test = 0; |
| 473 | p.rcpt_revoked_ids = NULL; |
| 474 | p.n_rcpt_revoked_ids = 0; |
| 475 | return p; |
| 476 | } |
| 477 | |
| 478 | static void owner_receipt(void) |
| 479 | { |
| 480 | uint8_t o[1024], t[1024], r[416], h[32], ofp[1][32], oid[1][32], other[32]; |
| 481 | char hs[65]; |
| 482 | size_t n = slurp("osc_receipt_owner/a01.owner.rcpt", o, sizeof o); |
| 483 | CHECK(n == OSC_RCPT_BYTES); |
| 484 | sha256_hash(o, n, h); |
| 485 | hex(h, 32, hs); |
| 486 | CHECK(!strcmp(hs, owner_rcpt_sha256)); /* the committed bytes, nothing else */ |
| 487 | memcpy(oid[0], h, 32); |
| 488 | sha256_hash(osc_rcpt_owner_root_pk, 32, ofp[0]); |
| 489 | CHECK(o[10] == OSC_SIGNER_OWNER && o[11] == 0 && memcmp(o + 320, ofp[0], 32) == 0); /* OWNER, owner root */ |
| 490 | CHECK(rcpt("a01.rcpt", t) == OSC_RCPT_BYTES); |
| 491 | struct osc_accept a, l, x; |
| 492 | CHECK(admit("a01", &a) && admit("l01", &l) && admit("osh_lex", &x)); |
| 493 | struct osc_receipt_info info; |
| 494 | |
| 495 | /* VALID under the kernel's own policy, in release mode, and under the release policy */ |
| 496 | struct osc_policy p; |
| 497 | ck_osc_policy(&p); |
| 498 | CHECK(v(o, 416, &a, &p) == 0); |
| 499 | p.release = 1; |
| 500 | memset(&info, 0, sizeof info); |
| 501 | CHECK(osc_receipt_verify(o, 416, &a, &p, &info) == 0); |
| 502 | p = rel(); |
| 503 | memset(&info, 0, sizeof info); |
| 504 | unsigned got = osc_receipt_verify(o, 416, &a, &p, &info); |
| 505 | if (got) printf(" owner receipt under the release policy: got %u %s\n", got, osc_code_name(got)); |
| 506 | CHECK(got == 0 && info.have_id && memcmp(info.id, oid[0], 32) == 0); |
| 507 | /* the TEST checker2 key stays out of the release policy: a TEST receipt is refused, and a TEST receipt relabelled |
| 508 | * OWNER names no owner anchor */ |
| 509 | CHECK(v(t, 416, &a, &p) == OSC_RCPT_TEST_SIGNER_IN_PRODUCTION); |
| 510 | memcpy(r, t, 416); |
| 511 | put16(r + 10, OSC_SIGNER_OWNER); |
| 512 | CHECK(v(r, 416, &a, &p) == OSC_RCPT_UNTRUSTED_SIGNER); |
| 513 | /* negative control: the release policy as shipped before aienos#387 (no owner anchor) refuses it */ |
| 514 | p.rcpt_owner_anchors = NULL; |
| 515 | p.n_rcpt_owner = 0; |
| 516 | CHECK(v(o, 416, &a, &p) == OSC_RCPT_UNTRUSTED_SIGNER); |
| 517 | /* the owner root anchored in both classes is refused */ |
| 518 | p = rel(); |
| 519 | static const uint8_t (*const ok)[32] = &osc_rcpt_owner_root_pk; |
| 520 | p.rcpt_test_anchors = ok; |
| 521 | p.n_rcpt_test = 1; |
| 522 | CHECK(v(o, 416, &a, &p) == OSC_RCPT_UNTRUSTED_SIGNER); |
| 523 | /* bound to a01 on this target and epoch window: wrong unit, wrong target, epoch edges, unlisted checker */ |
| 524 | p = rel(); |
| 525 | CHECK(v(o, 416, &l, &p) == OSC_RCPT_EXECUTABLE_MISMATCH); /* l01: a01's program, other code */ |
| 526 | CHECK(v(o, 416, &x, &p) == OSC_RCPT_PROGRAM_MISMATCH); /* osh_lex: another program */ |
| 527 | osc_receipt_target_hash("aienos-ck-x86_64-el0", 20, other); |
| 528 | p.rcpt_target = other; |
| 529 | CHECK(v(o, 416, &a, &p) == OSC_RCPT_WRONG_TARGET); |
| 530 | p = rel(); |
| 531 | p.rcpt_epoch_now = 100; /* expiry 100, inclusive */ |
| 532 | CHECK(v(o, 416, &a, &p) == 0); |
| 533 | p.rcpt_epoch_now = 101; |
| 534 | CHECK(v(o, 416, &a, &p) == OSC_RCPT_EXPIRED); |
| 535 | p.rcpt_epoch_now = 19; /* issued at epoch 20 */ |
| 536 | CHECK(v(o, 416, &a, &p) == OSC_RCPT_NOT_YET_VALID); |
| 537 | p = rel(); |
| 538 | p.rcpt_epoch_floor = 21; |
| 539 | CHECK(v(o, 416, &a, &p) == OSC_RCPT_STALE_EPOCH); |
| 540 | p = rel(); |
| 541 | p.n_rcpt_checkers = 0; |
| 542 | CHECK(v(o, 416, &a, &p) == OSC_RCPT_UNTRUSTED_CHECKER); |
| 543 | /* revocation: the receipt id, then the owner key fingerprint */ |
| 544 | p = rel(); |
| 545 | p.rcpt_revoked_ids = (const uint8_t (*)[32])oid; |
| 546 | p.n_rcpt_revoked_ids = 1; |
| 547 | CHECK(osc_receipt_verify(o, 416, &a, &p, &info) == OSC_RCPT_REVOKED && info.revoked == OSC_RCPT_REVOKED_ID); |
| 548 | p = rel(); |
| 549 | p.rcpt_revoked_keys = (const uint8_t (*)[32])ofp; |
| 550 | p.n_rcpt_revoked_keys = 1; |
| 551 | CHECK(osc_receipt_verify(o, 416, &a, &p, &info) == OSC_RCPT_REVOKED && info.revoked == OSC_RCPT_REVOKED_KEY); |
| 552 | /* every single-bit flip of the owner receipt is refused under the release policy */ |
| 553 | p = rel(); |
| 554 | int flips = 0, accepted = 0; |
| 555 | for (unsigned i = 0; i < 416; i++) |
| 556 | for (unsigned bit = 0; bit < 8; bit++) { |
| 557 | memcpy(r, o, 416); |
| 558 | r[i] ^= (uint8_t)(1u << bit); |
| 559 | if (v(r, 416, &a, &p) == 0) accepted++; |
| 560 | flips++; |
| 561 | } |
| 562 | CHECK(flips == 3328 && accepted == 0); |
| 563 | got = v(o, 416, &a, &p); |
| 564 | CHECK(got == 0); /* the input was not written */ |
| 565 | printf(" owner receipt a01.owner.rcpt under the release policy: %s; single-bit flips refused %d of %d\n", |
| 566 | got ? osc_code_name(got) : "VALID", flips - accepted, flips); |
| 567 | } |
| 568 | |
| 569 | static void names(void) |
| 570 | { |
| 571 | CHECK(!strcmp(osc_code_name(OSC_RCPT_MISSING), "RECEIPT_MISSING")); |
| 572 | CHECK(!strcmp(osc_code_name(OSC_RCPT_REVOKED), "RECEIPT_REVOKED")); |
| 573 | CHECK(!strcmp(osc_code_name(OSC_RCPT_DEPENDENCIES_MISMATCH), "RECEIPT_DEPENDENCIES_MISMATCH")); |
| 574 | CHECK(!strcmp(osc_code_name(OSC_RCPT_LRAT_PROOFS_REQUIRED), "RECEIPT_LRAT_PROOFS_REQUIRED") && !strcmp(osc_code_name(95), "?")); |
| 575 | CHECK(!strcmp(osc_code_name(72), "?") && !strcmp(osc_code_name(85), "?") && !strcmp(osc_code_name(89), "?")); |
| 576 | CHECK(!strcmp(osc_code_name(OSC_CODE_HASH_MISMATCH), "CODE_HASH_MISMATCH")); /* unit codes unchanged */ |
| 577 | /* spec reason n is kernel code 64 + n */ |
| 578 | CHECK(OSC_RCPT_SIZE == OSC_RCPT_BASE + 1 && OSC_RCPT_UNTRUSTED_CHECKER == OSC_RCPT_BASE + 22); |
| 579 | } |
| 580 | |
| 581 | static void store_table(void) |
| 582 | { |
| 583 | uint8_t g[1024], big[600]; |
| 584 | CHECK(rcpt("a01.rcpt", g) == OSC_RCPT_BYTES); |
| 585 | CHECK(ck_osc_is_receipt(g, 416) && !ck_osc_is_receipt(g, 7) && !ck_osc_is_receipt(NULL, 416)); |
| 586 | CHECK(!ck_osc_is_receipt((const uint8_t *)"OSCUNIT\0", 8)); |
| 587 | size_t len = 99; |
| 588 | CHECK(ck_osc_receipt_find("a01.rcpt", &len) == NULL && len == 0); |
| 589 | outn = 0; |
| 590 | ck_osc_receipt_candidate("a01.rcpt", g, 416); |
| 591 | CHECK(strstr(out, "osc_receipt: a01.rcpt STORED bytes=416 id=613e1cece39535842e2b3e406147b837b4bb1ed91628caf54735c7c09cb70996 ") != NULL); |
| 592 | const uint8_t *k = ck_osc_receipt_find("a01.rcpt", &len); |
| 593 | CHECK(k != NULL && len == 416 && memcmp(k, g, 416) == 0); |
| 594 | CHECK(ck_osc_receipt_find("a01.rcp", &len) == NULL && ck_osc_receipt_find("a01.rcpt2", &len) == NULL); |
| 595 | memcpy(big, g, 416); |
| 596 | memset(big + 416, 0, sizeof big - 416); |
| 597 | ck_osc_receipt_candidate("big.rcpt", big, sizeof big); |
| 598 | k = ck_osc_receipt_find("big.rcpt", &len); |
| 599 | struct osc_accept a; |
| 600 | CHECK(admit("a01", &a)); |
| 601 | struct osc_policy p = qual(); |
| 602 | CHECK(k != NULL && len == 512 && v(k, len, &a, &p) == OSC_RCPT_SIZE); |
| 603 | char nm[16]; |
| 604 | for (unsigned i = 0; i < 30; i++) { |
| 605 | snprintf(nm, sizeof nm, "f%u.rcpt", i); |
| 606 | ck_osc_receipt_candidate(nm, g, 416); |
| 607 | } |
| 608 | CHECK(strstr(out, "osc_receipt: f21.rcpt STORED") != NULL && strstr(out, "osc_receipt: f22.rcpt DROPPED") != NULL); |
| 609 | CHECK(ck_osc_receipt_find("f22.rcpt", &len) == NULL); |
| 610 | outn = 0; |
| 611 | ck_osc_summary(); |
| 612 | CHECK(strstr(out, "osc_receipts: seen=32 stored=24 dropped=8 ") != NULL); |
| 613 | } |
| 614 | |
| 615 | int main(void) |
| 616 | { |
| 617 | sha256_hash(osc_rcpt_test_pk, 32, test_fp[0]); |
| 618 | { |
| 619 | uint8_t np[1024]; |
| 620 | CHECK(rcpt("n_producer.rcpt", np) == OSC_RCPT_BYTES); |
| 621 | memcpy(producer, np + 176, 32); |
| 622 | } |
| 623 | #ifdef CK_SEED0B_TEST_ANCHOR |
| 624 | printf(" policy: qualification (CK_SEED0B_TEST_ANCHOR)\n"); |
| 625 | #else |
| 626 | printf(" policy: release\n"); |
| 627 | #endif |
| 628 | expected_table(); |
| 629 | trust_inputs(); |
| 630 | structure_and_order(); |
| 631 | policy_edges(); |
| 632 | names(); |
| 633 | resigned(); |
| 634 | owner_receipt(); |
| 635 | store_table(); |
| 636 | return ck_t_verdict("test_osc_receipt"); |
| 637 | } |