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/aienux/blob/main/native/kernel/svc/store_boot.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / svc / store_boot.c` 1057 lines ยท 44855 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 /* store_boot.c -- see store_boot.h. */ 18 #include "store_boot.h" 19 #include "artifact_store.h" 20 #include 21 #include "ck.h" 22 #include "disk_layout.h" 23 #include "m5.h" 24 #include "sha256.h" 25 #include "store_v1.h" 26 #include "store_crash.h" 27 #include "continuity_boot.h" 28 #ifdef CK_PI5_FACT 29 #include "pi5_fact.h" 30 #endif 31 #ifdef CK_PI5_AGENT 32 #include "pi5_agent.h" 33 #endif 34 #ifdef CK_SERVICE 35 #include "continuity_service.h" 36 #endif 37 38 #ifdef CK_HARDWARE_STAGING 39 /* Hardware staging image: owner provisioning generated at build time by 40 * tools/ck_owner_gen.c from CK_OWNER_PUBKEYS= and CK_MACHINE_ID= (public 41 * material only). No TEST label, TEST uuid or TEST key code is compiled. */ 42 #include "ck_owner_prov.h" 43 #if !defined(CK_OWNER_PROVISIONED) || CK_OWNER_PROVISIONED != 1 44 #error "CK_HARDWARE_STAGING needs the generated owner provisioning header (make full CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS= CK_MACHINE_ID=)" 45 #endif 46 #if defined(CK_STORE_TEST_KEYS) 47 #error "CK_STORE_TEST_KEYS (TEST Store keys, TEST uuid) cannot be combined with CK_HARDWARE_STAGING" 48 #endif 49 #if defined(CK_PI5_UNLOCK) 50 #error "CK_PI5_UNLOCK cannot be combined with CK_HARDWARE_STAGING" 51 #endif 52 #define CK_SB_HW 1 53 #elif defined(CK_PI5_UNLOCK) 54 /* Pi unlock image (CK_PI5_UNLOCK): the Store opens only through the Slot 1 recovery-secret path 55 * (ck_store_pi5_unlock_run). No TEST keys, no TEST uuid are compiled, and the policy is the 56 * hardware one: TEST-identity keys are refused before any disk access. It needs no owner 57 * provisioning header (the store uuid is drawn at genesis or read from the manifest). */ 58 #if defined(CK_STORE_TEST_KEYS) || defined(CK_PI5_STORE) || defined(CK_TEST_STORE_CRASH) || (defined(CK_SERVICE) && !defined(CK_PI5_LIFECYCLE)) 59 #error "CK_PI5_UNLOCK cannot be combined with TEST keys, CK_PI5_STORE, CK_TEST_STORE_CRASH or CK_SERVICE (the one exception: the CK_PI5_LIFECYCLE qualification image, whose lifecycle sources are the CK_SERVICE ones)" 60 #endif 61 #if defined(CK_PI5_LIFECYCLE) && (!defined(CK_SERVICE) || !defined(CK_PI5_UNLOCK_CARD) || defined(CK_SERVICE_TEST_KEYS)) 62 #error "CK_PI5_LIFECYCLE needs CK_SERVICE sources, CK_PI5_UNLOCK_CARD (card secret) and no TEST keys" 63 #endif 64 #define CK_SB_HW 1 65 #else 66 #define CK_STORE_TEST_KEYS 1 67 #define CK_SB_HW 0 68 #endif 69 70 /* TEST-ONLY Store crash hook (svc/store_crash.h), only in the image built 71 * with CK_TEST_STORE_CRASH=1; never with CK_HARDWARE_STAGING. */ 72 #ifdef CK_TEST_STORE_CRASH 73 #define CK_SB_HOOK ck_store_crash_hook 74 #else 75 #define CK_SB_HOOK 0 76 #endif 77 78 #ifdef CK_STORE_TEST_KEYS 79 #define LABEL_KVOL "AIENOS-LANE18-TEST-KVOL-NOT-SECRET" 80 81 const uint8_t ck_store_test_uuid[16] = {'A', 'I', 'E', 'N', '-', 'T', 'E', 'S', 82 'T', '-', 'B', 'O', 'O', 'T', '0', '1'}; 83 #endif 84 85 static void bz(void *p, size_t n) 86 { 87 volatile uint8_t *b = p; 88 while (n--) *b++ = 0; 89 } 90 static void bcp(void *d, const void *s, size_t n) 91 { 92 uint8_t *o = d; 93 const uint8_t *i = s; 94 while (n--) *o++ = *i++; 95 } 96 static void put64(uint8_t *p, uint64_t v) 97 { 98 for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); 99 } 100 static void put32(uint8_t *p, uint32_t v) 101 { 102 for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); 103 } 104 static uint64_t get64(const uint8_t *p) 105 { 106 uint64_t v = 0; 107 for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; 108 return v; 109 } 110 static uint32_t get32(const uint8_t *p) 111 { 112 return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; 113 } 114 115 #ifdef CK_STORE_TEST_KEYS 116 void ck_store_test_keys(ss_keys *k) 117 { 118 uint8_t kvol[32]; 119 m5_subkeys sk; 120 sha256_hash((const uint8_t *)LABEL_KVOL, sizeof LABEL_KVOL - 1, kvol); 121 bz(k, sizeof *k); 122 m5_derive_subkeys(kvol, M5_ID_TEST, 0, &sk); 123 k->identity_class = M5_ID_TEST; 124 k->key_generation = 1; 125 bcp(k->k_root_auth, sk.k_root_auth, 32); 126 bcp(k->k_domain, sk.k_artifact, 32); 127 m5_subkeys_wipe(&sk); 128 bz(kvol, sizeof kvol); 129 } 130 #endif 131 132 /* BLOCKED_OPERATOR seam. Production Store keys derive from K_vol (ADR 0017), 133 * which is secret: it cannot come from the public owner material injected at 134 * build time, and no in-kernel unwrap of a K_vol keyslot exists yet. The 135 * operator must provide, through the TRUST-1 key ceremony: Gate 3 Owner Root 136 * (offline), Gate 5/6 TPM PolicyAuthorize key + sealed Slot 0 KEK (K_vol 137 * unwrapped at boot), or the Slot 1 recovery KEK. Until then this refuses and 138 * there is no TEST fallback. */ 139 int ck_store_production_keys(ss_keys *k) 140 { 141 if (k) bz(k, sizeof *k); 142 return CK_SB_E_BLOCKED_OPERATOR; 143 } 144 145 int ck_store_keys_admissible(const ss_keys *k, int hardware_staging) 146 { 147 if (!k) return 0; 148 if (k->identity_class == M5_ID_PRODUCTION) return 1; 149 return k->identity_class == M5_ID_TEST && !hardware_staging; 150 } 151 152 static int fail(ck_store_report *r, const char *proof, const char *step, int rc) 153 { 154 r->verdict = CK_SB_REFUSED; 155 r->proof = proof; 156 r->step = step; 157 r->rc = rc; 158 return rc ? rc : -1; 159 } 160 161 /* Blank = anchor units and the first two Store units read back all zero. */ 162 static int bytes_differ(const uint8_t *a, const uint8_t *b, size_t n) 163 { 164 int diff = 0; 165 for (size_t i = 0; i < n; i++) diff |= a[i] ^ b[i]; 166 return diff != 0; 167 } 168 169 static int is_blank(const disk_dev *d, uint32_t bpu, uint8_t *buf, int *blank) 170 { 171 *blank = 1; 172 for (uint64_t u = 0; u < CK_LAYOUT_ANCHOR_UNITS + 2u; u++) { 173 int rc = disk_read(d, u * bpu, bpu, buf); 174 if (rc) return rc; 175 for (uint32_t i = 0; i < CK_LAYOUT_UNIT; i++) 176 if (buf[i]) { 177 *blank = 0; 178 return 0; 179 } 180 } 181 return 0; 182 } 183 184 static const char *classify(int rc) 185 { 186 switch (rc) { 187 case SS_E_ROLLBACK: 188 case SS_E_INCONSISTENT: 189 case SS_E_ANCHOR: 190 return "rollback"; 191 case SS_E_IO: 192 case ST_M_IO: 193 return "io"; 194 default: 195 return (rc <= -101 && rc >= -107) ? "structural" : "keyed"; 196 } 197 } 198 199 /* Workspace lives across boots of the host test; the kernel uses it once. */ 200 static ss_workspace *g_ws; 201 static ss_store *g_ss; 202 /* The newest SecurityManifest after a committed boot or rotation (P04 cut 203 * 2a/2b): sequence and Store ObjectId the next manifest links to. */ 204 static uint64_t g_secman_seq; 205 static uint8_t g_secman_sid[32]; 206 207 /* SecurityManifest refusal: the Store is not served and the keys are wiped. */ 208 static int secman_refuse(ck_store_report *r, const char *step) 209 { 210 bz(&g_ss->keys, sizeof g_ss->keys); 211 return fail(r, "keyed", step, CK_SB_E_SECMAN); 212 } 213 #ifdef CK_SERVICE 214 static int g_service_open; /* Store open for the service lifetime (core/lifecycle.c) */ 215 static int g_service_locked_rc; /* last production key source refusal */ 216 static const disk_dev *g_disk; /* boot disk kept bound while LOCKED, for a later unlock */ 217 #endif 218 219 int store_boot_run(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, 220 ck_store_report *r) 221 { 222 return store_boot_run_policy(d, keys, uuid, commit, CK_SB_HW, r); 223 } 224 225 /* static: the open Store (g_ss) keeps pointers to these devices and is 226 * used after the boot returns (store_boot_store, the artifact read). */ 227 static st_disk g_sd; 228 static st_dev g_sdev; 229 static ts_device g_tdev; 230 231 /* Geometry + workspace + device binding shared by the boot and the unlock. 232 * Fills the layout fields of r; no disk access. */ 233 static int bind_disk(const disk_dev *d, ck_store_report *r, uint32_t *bpu_out) 234 { 235 if (d->block_size != 512 && d->block_size != 4096) return fail(r, "geometry", "block size", -1); 236 uint32_t bpu = CK_LAYOUT_UNIT / d->block_size; 237 uint64_t units = d->block_count / bpu; 238 if (units < CK_LAYOUT_ANCHOR_UNITS + CK_LAYOUT_MIN_STORE_UNITS + CK_LAYOUT_PROBE_UNITS) 239 return fail(r, "geometry", "disk too small", -1); 240 r->anchor_lba = 0; 241 r->store_base_lba = (uint64_t)CK_LAYOUT_ANCHOR_UNITS * bpu; 242 r->store_units = units - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS; 243 244 if (!g_ws) g_ws = ck_alloc(sizeof *g_ws); 245 if (!g_ss) g_ss = ck_alloc(sizeof *g_ss); 246 if (!g_ws || !g_ss) return fail(r, "io", "allocate workspace", -1); 247 bz(g_ss, sizeof *g_ss); 248 249 int rc = st_disk_bind(&g_sd, d, r->store_base_lba, r->store_units, &g_sdev); 250 if (rc) return fail(r, "geometry", "st_disk_bind", rc); 251 ss_ts_device(d, &g_tdev); 252 *bpu_out = bpu; 253 return 0; 254 } 255 256 /* The boot proper. genesis_obj (optional) is committed beside the boot record 257 * in the first transaction of a disk this run formatted (the KeySlotManifest 258 * of ck_store_unlock_boot); on a non-blank disk it is ignored. */ 259 static int run_policy_ex(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, 260 int hardware_staging, const ss_object *genesis_obj, ck_store_report *r) 261 { 262 bz(r, sizeof *r); 263 bcp(r->prev_commit, "none", 5); 264 if (!d || !keys || !uuid || !commit) return fail(r, "geometry", "arguments", -1); 265 r->identity_class = keys->identity_class; 266 /* Before any disk access: a hardware staging build never opens, and 267 * never formats a blank disk, under TEST-identity keys. */ 268 if (!ck_store_keys_admissible(keys, hardware_staging)) 269 return fail(r, "keyed", "identity class not admissible in this build (TEST keys refused on hardware)", 270 CK_SB_E_TEST_KEYS); 271 uint32_t bpu = 0; 272 int rc = bind_disk(d, r, &bpu); 273 if (rc) return rc; 274 275 int blank = 0; 276 rc = is_blank(d, bpu, g_ws->rd, &blank); 277 if (rc) return fail(r, "io", "blank check read", rc); 278 if (blank) { 279 rc = ss_format(&g_sdev, &g_tdev, r->anchor_lba, uuid, keys, g_ws); 280 if (rc) return fail(r, "commit", "format blank disk", rc); 281 r->formatted = 1; 282 } 283 284 /* Proof 1: structural, unkeyed. */ 285 rc = st_open(&g_ss->st, &g_sdev, &g_ws->st); 286 if (rc) return fail(r, rc == ST_M_IO ? "io" : "structural", "st_open (unkeyed)", rc); 287 /* Proofs 2 and 3: keyed M5 + anti-rollback anchor. */ 288 bz(g_ss, sizeof *g_ss); 289 rc = ss_open(g_ss, &g_sdev, &g_tdev, r->anchor_lba, keys, g_ws); 290 if (rc) return fail(r, classify(rc), "ss_open (keyed + anchor)", rc); 291 r->rb = g_ss->rb; 292 r->gen_open = ss_generation(g_ss); 293 294 /* Latest boot record = highest store generation among boot-kind claims. */ 295 int best = -1; 296 for (uint32_t k = 0; k < g_ss->nclaims; k++) { 297 const ss_claim *c = &g_ws->claims[k]; 298 if (c->c.obj.object_kind != CK_BOOT_KIND) continue; 299 if (best < 0 || c->c.store_generation > g_ws->claims[best].c.store_generation) best = (int)k; 300 } 301 uint8_t rec[CK_BOOT_RECORD_LEN]; 302 if (best >= 0) { 303 uint8_t sid[32]; 304 size_t len = 0; 305 uint16_t kind = 0; 306 bcp(sid, g_ws->claims[best].sid, 32); 307 rc = ss_read(g_ss, sid, rec, sizeof rec, &len, &kind); 308 if (rc) return fail(r, classify(rc), "read boot record", rc); 309 if (len != CK_BOOT_RECORD_LEN || kind != CK_BOOT_KIND || get64(rec) != get64((const uint8_t *)"AIENBOOT") || 310 get32(rec + 8) != CK_BOOT_VERSION) 311 return fail(r, "record", "boot record malformed (authenticated, wrong layout)", -1); 312 r->boot_count_prev = get64(rec + 16); 313 size_t i = 0; 314 for (; i < CK_BOOT_COMMIT_MAX && rec[24 + i]; i++) r->prev_commit[i] = (char)rec[24 + i]; 315 r->prev_commit[i] = 0; 316 } else if (!r->formatted && r->gen_open > 1) { 317 return fail(r, "record", "store has history but no boot record", -1); 318 } 319 320 /* SecurityManifest (ADR 0017 section 2.5, P04 cut 2a): the newest 321 * kind-22 claim must verify under these keys and bind this Store, its 322 * keyslot and its key epoch; its security_sequence equals the number of 323 * manifests on the Store (every boot transaction writes exactly one). */ 324 m5_secman sm; 325 uint8_t sm_prev[32], zero32[32]; 326 uint32_t nsecman = 0; 327 int sm_best = -1; 328 bz(&sm, sizeof sm); 329 bz(sm_prev, sizeof sm_prev); 330 bz(zero32, sizeof zero32); 331 for (uint32_t k = 0; k < g_ss->nclaims; k++) { 332 const ss_claim *c = &g_ws->claims[k]; 333 if (c->c.obj.object_kind != CK_SECMAN_KIND) continue; 334 nsecman++; 335 if (sm_best < 0 || c->c.store_generation > g_ws->claims[sm_best].c.store_generation) sm_best = (int)k; 336 } 337 if (sm_best >= 0) { 338 uint8_t smb[M5_SECMAN_LEN]; 339 size_t len = 0; 340 uint16_t kind = 0; 341 bcp(sm_prev, g_ws->claims[sm_best].sid, 32); 342 rc = ss_read(g_ss, sm_prev, smb, sizeof smb, &len, &kind); 343 if (rc) return fail(r, classify(rc), "read SecurityManifest", rc); 344 if (len != M5_SECMAN_LEN || kind != CK_SECMAN_KIND || m5_secman_open(smb, len, keys->k_root_auth, &sm) != 0) 345 return secman_refuse(r, "SecurityManifest malformed or not under these keys"); 346 if (bytes_differ(sm.store_uuid, g_ss->uuid, 16)) return secman_refuse(r, "SecurityManifest names another Store"); 347 if (bytes_differ(sm.keyslot_manifest_id, g_ss->has_keyslot ? g_ss->keyslot_sid : zero32, 32)) 348 return secman_refuse(r, "SecurityManifest does not bind this Store's KeySlotManifest"); 349 if (sm.key_epoch != keys->key_generation) return secman_refuse(r, "SecurityManifest key_epoch differs from the open keys"); 350 if (sm.generation != g_ws->claims[sm_best].c.store_generation) 351 return secman_refuse(r, "SecurityManifest generation differs from the transaction that committed it"); 352 if (sm.security_sequence != nsecman) return secman_refuse(r, "SecurityManifest security_sequence is not the manifest count"); 353 r->secman_prev = 1; 354 } else if (!r->formatted && r->gen_open > 1) { 355 return secman_refuse(r, "store has history but no SecurityManifest"); 356 } 357 358 r->boot_count_new = r->boot_count_prev + 1; 359 bz(rec, sizeof rec); 360 bcp(rec, "AIENBOOT", 8); 361 put32(rec + 8, CK_BOOT_VERSION); 362 put64(rec + 16, r->boot_count_new); 363 for (size_t i = 0; i < CK_BOOT_COMMIT_MAX && commit[i]; i++) rec[24 + i] = (uint8_t)commit[i]; 364 /* The SecurityManifest of this transaction (sequence previous + 1). */ 365 uint8_t smb_new[M5_SECMAN_LEN]; 366 { 367 m5_secman n; 368 bz(&n, sizeof n); 369 bcp(n.store_uuid, g_ss->uuid, 16); 370 n.generation = r->gen_open + 1; 371 n.security_sequence = sm.security_sequence + 1; 372 n.epoch = n.generation; 373 n.key_epoch = keys->key_generation; 374 bcp(n.previous_security_manifest_id, sm_prev, 32); 375 if (genesis_obj && r->formatted) { 376 if (sv1_object_id(genesis_obj->kind, genesis_obj->version, genesis_obj->bytes, genesis_obj->len, 377 n.keyslot_manifest_id) != 0) 378 return fail(r, "commit", "KeySlotManifest ObjectId", -1); 379 } else if (g_ss->has_keyslot) { 380 bcp(n.keyslot_manifest_id, g_ss->keyslot_sid, 32); 381 } 382 if (m5_secman_seal(&n, keys->k_root_auth, smb_new) != 0) return fail(r, "commit", "seal SecurityManifest", -1); 383 r->secman_seq = n.security_sequence; 384 r->secman_keyslot = bytes_differ(n.keyslot_manifest_id, zero32, 32); 385 } 386 ss_object objs[3] = {{CK_BOOT_KIND, CK_BOOT_VERSION, rec, sizeof rec}, 387 {CK_SECMAN_KIND, 1, smb_new, M5_SECMAN_LEN}, 388 {0, 0, 0, 0}}; 389 size_t nobjs = 2; 390 if (genesis_obj && r->formatted) objs[nobjs++] = *genesis_obj; 391 #ifdef CK_TEST_STORE_CRASH 392 ck_store_crash_opened(r->gen_open); 393 #endif 394 uint8_t ids[3][32]; 395 rc = ss_transact(g_ss, objs, nobjs, CK_SB_HOOK, 0, ids); 396 if (rc) return fail(r, "commit", "ss_transact", rc); 397 rc = disk_flush(d); 398 if (rc) return fail(r, "io", "flush", rc); 399 r->gen_commit = ss_generation(g_ss); 400 if (r->gen_commit != r->gen_open + 1) return fail(r, "commit", "generation did not advance by one", -1); 401 g_secman_seq = r->secman_seq; 402 bcp(g_secman_sid, ids[1], 32); 403 r->verdict = CK_SB_COMMITTED; 404 return 0; 405 } 406 407 int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t uuid[16], const char *commit, 408 int hardware_staging, ck_store_report *r) 409 { 410 return run_policy_ex(d, keys, uuid, commit, hardware_staging, 0, r); 411 } 412 413 /* ---- recovery keyslot unlock (ADR 0017 section 2.4, Slot 1) ---- */ 414 415 /* PRODUCTION-class Store keys from K_vol: owner hierarchy generation is the 416 * manifest's active_key_epoch (1 in cut 1, no rotation yet). */ 417 static int production_keys_from_kvol(const uint8_t k_vol[32], uint64_t epoch, ss_keys *k) 418 { 419 m5_subkeys sk; 420 bz(k, sizeof *k); 421 int rc = m5_derive_subkeys(k_vol, M5_ID_PRODUCTION, epoch, &sk); 422 if (rc) { 423 m5_subkeys_wipe(&sk); 424 return rc; 425 } 426 k->identity_class = M5_ID_PRODUCTION; 427 k->key_generation = epoch; 428 bcp(k->k_root_auth, sk.k_root_auth, 32); 429 bcp(k->k_domain, sk.k_artifact, 32); 430 m5_subkeys_wipe(&sk); 431 return 0; 432 } 433 434 #define CK_RECOVERY_SLOT_ID 1u 435 #define CK_RECOVERY_EPOCH 1u 436 437 static uint8_t *g_ksm_buf; /* manifest bytes (bootstrap read / genesis encode) */ 438 439 int ck_store_unlock_boot(const disk_dev *d, const uint8_t secret[CK_SB_SECRET_LEN], const uint8_t *expect_uuid, 440 const char *commit, int hardware_staging, ck_store_report *r) 441 { 442 ss_keys keys; 443 uint8_t k_vol[32], kek[32], uuid[16]; 444 m5_ksm m; 445 int rc, blank = 0; 446 uint32_t bpu = 0; 447 size_t mlen = 0; 448 449 bz(r, sizeof *r); 450 bcp(r->prev_commit, "none", 5); 451 r->identity_class = M5_ID_PRODUCTION; 452 r->key_source = "recovery-keyslot"; 453 if (!d || !secret || !commit) return fail(r, "geometry", "arguments", -1); 454 if (!g_ksm_buf) g_ksm_buf = ck_alloc(SS_KEYSLOT_MAX); 455 if (!g_ksm_buf) return fail(r, "io", "allocate keyslot buffer", -1); 456 rc = bind_disk(d, r, &bpu); 457 if (rc) return rc; 458 rc = is_blank(d, bpu, g_ws->rd, &blank); 459 if (rc) return fail(r, "io", "blank check read", rc); 460 461 bz(&m, sizeof m); 462 bz(&keys, sizeof keys); 463 if (blank) { 464 /* Genesis: fresh K_vol, salt and nonce from the kernel entropy service; 465 * refuse (no fallback) when RNDR refuses. The manifest goes into the 466 * same first transaction as the boot record. */ 467 uint8_t fresh[32 + 16 + 12 + 16]; 468 if (ck_entropy_fill(fresh, sizeof fresh) != 0) { 469 bz(fresh, sizeof fresh); 470 return fail(r, "keyed", "genesis entropy (K_vol, salt, nonce) refused by the entropy service", 471 CK_SB_E_ENTROPY); 472 } 473 bcp(k_vol, fresh, 32); 474 if (expect_uuid) bcp(uuid, expect_uuid, 16); 475 else bcp(uuid, fresh + 60, 16); 476 bcp(m.store_uuid, uuid, 16); 477 m.manifest_sequence = 1; 478 m.active_key_epoch = CK_RECOVERY_EPOCH; 479 m.slot_count = 2; /* index 0 reserved for the TPM slot (Empty), index 1 recovery */ 480 m5_keyslot *sl = &m.slots[1]; 481 sl->slot_type = M5_SLOT_RECOVERY_RAW_SECRET; 482 sl->wrap_suite = M5_WRAP_SUITE_GCMSIV; 483 sl->kdf_suite = 0x02; /* HKDF-SHA256 */ 484 sl->slot_id = CK_RECOVERY_SLOT_ID; 485 sl->key_epoch = CK_RECOVERY_EPOCH; 486 bcp(sl->salt, fresh + 32, 16); 487 rc = m5_recovery_kek(sl->salt, secret, CK_SB_SECRET_LEN, kek); 488 if (rc == 0) rc = m5_keyslot_wrap(sl, uuid, kek, k_vol, fresh + 48); 489 bz(fresh, sizeof fresh); 490 bz(kek, sizeof kek); 491 if (rc) { 492 bz(k_vol, sizeof k_vol); 493 return fail(r, "keyed", "wrap K_vol into the recovery keyslot", rc); 494 } 495 m5_ksm_encode(&m, g_ksm_buf); 496 mlen = M5_KSM_FIXED_LEN; 497 r->unlocked_genesis = 1; 498 } else { 499 /* Bootstrap: the manifest is read before any key exists, then the 500 * keyed open re-binds the same bytes through the transaction record. */ 501 rc = ss_keyslot_bootstrap(&g_ss->st, &g_sdev, g_ws, g_ksm_buf, SS_KEYSLOT_MAX, &mlen); 502 if (rc) return fail(r, rc == ST_M_IO ? "io" : "keyed", "no KeySlotManifest on a non-blank disk", rc == SS_E_KEYSLOT ? CK_SB_E_NO_KEYSLOT : rc); 503 rc = m5_ksm_decode(g_ksm_buf, mlen, &m); 504 if (rc) return fail(r, "keyed", "KeySlotManifest decode", CK_SB_E_KEYSLOT_BAD); 505 if (expect_uuid) { 506 int diff = 0; 507 for (size_t i = 0; i < 16; i++) diff |= m.store_uuid[i] ^ expect_uuid[i]; 508 if (diff) return fail(r, "keyed", "KeySlotManifest store_uuid is not this machine's", CK_SB_E_KEYSLOT_BAD); 509 } 510 bcp(uuid, m.store_uuid, 16); 511 const m5_keyslot *sl = 0; 512 for (uint32_t i = 0; i < m.slot_count && i < M5_KSM_SLOTS; i++) 513 if (m.slots[i].slot_type == M5_SLOT_RECOVERY_RAW_SECRET && m.slots[i].kdf_suite == 0x02 && 514 m.slots[i].wrap_suite == M5_WRAP_SUITE_GCMSIV) { 515 sl = &m.slots[i]; 516 break; 517 } 518 if (!sl) return fail(r, "keyed", "no Recovery_RawSecret (HKDF) descriptor in the manifest", CK_SB_E_KEYSLOT_BAD); 519 rc = m5_recovery_kek(sl->salt, secret, CK_SB_SECRET_LEN, kek); 520 if (rc == 0) rc = m5_keyslot_unwrap(sl, uuid, kek, k_vol); 521 bz(kek, sizeof kek); 522 if (rc) { 523 bz(k_vol, sizeof k_vol); 524 return fail(r, "keyed", "recovery secret rejected (K_vol unwrap tag failed); disk not opened", 525 CK_SB_E_SECRET_REJECTED); 526 } 527 /* The epoch the descriptor wrapped under is the active one in cut 1. */ 528 if (sl->key_epoch != m.active_key_epoch) { 529 bz(k_vol, sizeof k_vol); 530 return fail(r, "keyed", "descriptor key_epoch differs from active_key_epoch (rotation not supported)", 531 CK_SB_E_KEYSLOT_BAD); 532 } 533 } 534 rc = production_keys_from_kvol(k_vol, m.active_key_epoch, &keys); 535 bz(k_vol, sizeof k_vol); 536 if (rc) { 537 bz(&keys, sizeof keys); 538 return fail(r, "keyed", "derive PRODUCTION subkeys", rc); 539 } 540 { 541 ss_object ks = {M5_KSM_KIND, 1, g_ksm_buf, mlen}; 542 int genesis = r->unlocked_genesis; 543 rc = run_policy_ex(d, &keys, uuid, commit, hardware_staging, blank ? &ks : 0, r); 544 r->key_source = "recovery-keyslot"; 545 r->unlocked_genesis = genesis; 546 r->keyslot_seq = m.manifest_sequence; 547 if (rc == 0 && !blank && !g_ss->has_keyslot) { 548 /* Opened, but the keyed open did not bind the manifest we 549 * bootstrapped from: refuse rather than serve an unbound keyslot. */ 550 bz(&g_ss->keys, sizeof g_ss->keys); 551 rc = fail(r, "keyed", "KeySlotManifest not bound by the sealed transaction records", CK_SB_E_KEYSLOT_BAD); 552 } 553 } 554 bz(&keys, sizeof keys); 555 bz(g_ksm_buf, SS_KEYSLOT_MAX); 556 return rc; 557 } 558 559 /* The Recovery_RawSecret (HKDF, GCM-SIV) descriptor of a decoded manifest. */ 560 static m5_keyslot *recovery_slot(m5_ksm *m) 561 { 562 for (uint32_t i = 0; i < m->slot_count && i < M5_KSM_SLOTS; i++) 563 if (m->slots[i].slot_type == M5_SLOT_RECOVERY_RAW_SECRET && m->slots[i].kdf_suite == 0x02 && 564 m->slots[i].wrap_suite == M5_WRAP_SUITE_GCMSIV) 565 return &m->slots[i]; 566 return 0; 567 } 568 569 int ck_store_rotate_recovery(const disk_dev *d, const uint8_t current[CK_SB_SECRET_LEN], 570 const uint8_t next[CK_SB_SECRET_LEN], ck_store_report *r) 571 { 572 m5_ksm m; 573 m5_keyslot *sl; 574 uint8_t k_vol[32], kek[32], fresh[16 + 12], smb[M5_SECMAN_LEN], ids[2][32]; 575 size_t mlen = 0; 576 uint16_t kind = 0; 577 int rc; 578 579 bz(r, sizeof *r); 580 bcp(r->prev_commit, "none", 5); 581 r->identity_class = M5_ID_PRODUCTION; 582 r->key_source = "recovery-keyslot"; 583 if (!d || !current || !next) return fail(r, "geometry", "arguments", -1); 584 if (!g_ss || !g_ws || !g_ksm_buf || g_ss->keys.identity_class != M5_ID_PRODUCTION || !g_ss->has_keyslot) 585 return fail(r, "keyed", "no open production Store with a KeySlotManifest to rotate", CK_SB_E_NO_KEYSLOT); 586 r->gen_open = ss_generation(g_ss); 587 rc = ss_read(g_ss, g_ss->keyslot_sid, g_ksm_buf, SS_KEYSLOT_MAX, &mlen, &kind); 588 if (rc || kind != M5_KSM_KIND) return fail(r, "keyed", "read the bound KeySlotManifest", rc ? rc : CK_SB_E_KEYSLOT_BAD); 589 bz(&m, sizeof m); 590 if (m5_ksm_decode(g_ksm_buf, mlen, &m) != 0 || m.arena_len != 0 || mlen != M5_KSM_FIXED_LEN) 591 return fail(r, "keyed", "KeySlotManifest decode (raw-secret profile, no arena)", CK_SB_E_KEYSLOT_BAD); 592 if (m.manifest_sequence != g_ss->nkeyslot) 593 return fail(r, "keyed", "bound manifest sequence is not the chain length", CK_SB_E_KEYSLOT_BAD); 594 sl = recovery_slot(&m); 595 if (!sl) return fail(r, "keyed", "no Recovery_RawSecret (HKDF) descriptor in the manifest", CK_SB_E_KEYSLOT_BAD); 596 /* proof of possession: the current secret must unwrap K_vol */ 597 rc = m5_recovery_kek(sl->salt, current, CK_SB_SECRET_LEN, kek); 598 if (rc == 0) rc = m5_keyslot_unwrap(sl, g_ss->uuid, kek, k_vol); 599 bz(kek, sizeof kek); 600 if (rc) { 601 bz(k_vol, sizeof k_vol); 602 return fail(r, "keyed", "current recovery secret rejected (K_vol unwrap tag failed); nothing written", 603 CK_SB_E_SECRET_REJECTED); 604 } 605 if (ck_entropy_fill(fresh, sizeof fresh) != 0) { 606 bz(k_vol, sizeof k_vol); 607 bz(fresh, sizeof fresh); 608 return fail(r, "keyed", "rotation entropy (salt, nonce) refused by the entropy service", CK_SB_E_ENTROPY); 609 } 610 /* next link of the chain: same K_vol and epoch, new salt, K_vol re-wrapped under the new KEK */ 611 m.manifest_sequence += 1; 612 bcp(m.previous_keyslot_manifest_id, g_ss->keyslot_sid, 32); 613 bcp(sl->salt, fresh, 16); 614 rc = m5_recovery_kek(sl->salt, next, CK_SB_SECRET_LEN, kek); 615 if (rc == 0) rc = m5_keyslot_wrap(sl, g_ss->uuid, kek, k_vol, fresh + 16); 616 bz(kek, sizeof kek); 617 bz(k_vol, sizeof k_vol); 618 bz(fresh, sizeof fresh); 619 if (rc) { 620 bz(&m, sizeof m); 621 return fail(r, "keyed", "wrap K_vol under the new recovery secret", rc); 622 } 623 m5_ksm_encode(&m, g_ksm_buf); 624 r->keyslot_seq = m.manifest_sequence; 625 bz(&m, sizeof m); 626 /* the SecurityManifest of the rotation transaction binds the new manifest */ 627 { 628 m5_secman n; 629 bz(&n, sizeof n); 630 bcp(n.store_uuid, g_ss->uuid, 16); 631 n.generation = r->gen_open + 1; 632 n.security_sequence = g_secman_seq + 1; 633 n.epoch = n.generation; 634 n.key_epoch = g_ss->keys.key_generation; 635 bcp(n.previous_security_manifest_id, g_secman_sid, 32); 636 if (sv1_object_id(M5_KSM_KIND, 1, g_ksm_buf, M5_KSM_FIXED_LEN, n.keyslot_manifest_id) != 0) 637 return fail(r, "commit", "KeySlotManifest ObjectId", -1); 638 if (m5_secman_seal(&n, g_ss->keys.k_root_auth, smb) != 0) return fail(r, "commit", "seal SecurityManifest", -1); 639 r->secman_seq = n.security_sequence; 640 r->secman_keyslot = 1; 641 r->secman_prev = 1; 642 } 643 { 644 ss_object objs[2] = {{M5_KSM_KIND, 1, g_ksm_buf, M5_KSM_FIXED_LEN}, {CK_SECMAN_KIND, 1, smb, M5_SECMAN_LEN}}; 645 rc = ss_transact(g_ss, objs, 2, CK_SB_HOOK, 0, ids); 646 } 647 bz(g_ksm_buf, SS_KEYSLOT_MAX); 648 if (rc) return fail(r, "commit", "ss_transact", rc); 649 rc = disk_flush(d); 650 if (rc) return fail(r, "io", "flush", rc); 651 r->gen_commit = ss_generation(g_ss); 652 if (r->gen_commit != r->gen_open + 1) return fail(r, "commit", "generation did not advance by one", -1); 653 g_secman_seq = r->secman_seq; 654 bcp(g_secman_sid, ids[1], 32); 655 r->verdict = CK_SB_COMMITTED; 656 return 0; 657 } 658 659 static const char *rb_name(int rb) 660 { 661 return rb == SS_RB_VALID_RESUME ? "valid-resume" : rb == SS_RB_PREPARED_ADVANCE ? "prepared-advance" : 662 rb == SS_RB_GENESIS ? "genesis" : "?"; 663 } 664 665 static const char *err_name(int rc) 666 { 667 if (rc <= -301 && rc >= -313) return ss_strerror(rc); 668 if (rc <= -101 && rc >= -212) return st_strerror(rc); 669 if (rc == CK_SB_E_TEST_KEYS) return "TestKeysRefused"; 670 if (rc == CK_SB_E_BLOCKED_OPERATOR) return "BlockedOperator"; 671 if (rc == CK_SB_E_NO_KEYSLOT) return "NoKeyslotManifest"; 672 if (rc == CK_SB_E_SECRET_REJECTED) return "RecoverySecretRejected"; 673 if (rc == CK_SB_E_ENTROPY) return "EntropyRefused"; 674 if (rc == CK_SB_E_KEYSLOT_BAD) return "KeyslotManifestUnusable"; 675 if (rc == CK_SB_E_CONTINUITY) return "ContinuityRefused"; 676 if (rc == CK_SB_E_SECMAN) return "SecurityManifestRefused"; 677 return "error"; 678 } 679 680 void store_boot_print(const ck_store_report *r) 681 { 682 const char *fmt_line; 683 #ifdef CK_STORE_TEST_KEYS 684 if (r->identity_class == M5_ID_TEST) { 685 ck_printf("store: TEST identity, TEST keys (public label, not secret, not production); " 686 "anchor lba=%llu store lba=%llu units=%llu\n", 687 (unsigned long long)r->anchor_lba, (unsigned long long)r->store_base_lba, 688 (unsigned long long)r->store_units); 689 fmt_line = "store: blank disk (all-zero anchor + Store head): formatted TEST store\n"; 690 } else 691 #endif 692 { 693 ck_printf("store: identity class=%u (%s); anchor lba=%llu store lba=%llu units=%llu\n", 694 (unsigned)r->identity_class, r->identity_class == M5_ID_PRODUCTION ? "production" : "refused", 695 (unsigned long long)r->anchor_lba, (unsigned long long)r->store_base_lba, 696 (unsigned long long)r->store_units); 697 fmt_line = "store: blank disk (all-zero anchor + Store head): formatted production store\n"; 698 } 699 if (r->formatted) ck_puts(fmt_line); 700 if (r->key_source) 701 ck_printf("store: key source=%s genesis=%d (K_vol source: the Slot 1 recovery descriptor; " 702 "TPM Slot 0 BLOCKED_HARDWARE; no rotation)\n", r->key_source, r->unlocked_genesis); 703 if (r->verdict != CK_SB_COMMITTED) { 704 ck_printf("store: REFUSED proof=%s step=\"%s\" rc=%d (%s); disk left as found, not reformatted\n", 705 r->proof, r->step, r->rc, err_name(r->rc)); 706 return; 707 } 708 ck_printf("store: opened generation=%llu boot_count=%llu prev_commit=%s anchor=%s\n", 709 (unsigned long long)r->gen_open, (unsigned long long)r->boot_count_prev, r->prev_commit, 710 rb_name(r->rb)); 711 ck_printf("store: committed generation=%llu boot_count=%llu\n", (unsigned long long)r->gen_commit, 712 (unsigned long long)r->boot_count_new); 713 ck_printf("store: security manifest sequence=%llu generation=%llu keyslot=%s previous=%s\n", 714 (unsigned long long)r->secman_seq, (unsigned long long)r->gen_commit, 715 r->secman_keyslot ? "bound" : "none", r->secman_prev ? "linked" : "genesis"); 716 ck_printf("store: limit: anchor shares this disk, so a whole-disk rollback is not detectable " 717 "(needs TPM NV or another external counter)\n"); 718 } 719 720 #ifdef CK_SERVICE 721 /* P05 cut 1: continuity (identity + ALLEN subject) runs on every Store open 722 * (svc/continuity_service.c). 0 = REFUSED: the Store keys are erased, the 723 * disk bytes stay as found, the lifecycle stays LOCKED and the NVMe stays 724 * bound for the stop order. */ 725 static int service_continuity(void) 726 { 727 #ifdef CK_HARDWARE_STAGING 728 int o = ck_cont_service_start(g_ss, g_ss->uuid, ck_owner_root_pk, sizeof ck_owner_root_pk, CC_SOURCE_OPERATOR); 729 #else 730 int o = ck_cont_service_start(g_ss, g_ss->uuid, 0, 0, CC_SOURCE_QUALIFICATION); 731 #endif 732 if (o == CK_CONT_REFUSED) { 733 bz(&g_ss->keys, sizeof g_ss->keys); 734 g_service_locked_rc = CK_SB_E_CONTINUITY; 735 ck_puts("store: service lifetime: continuity REFUSED, Store keys erased, lifecycle stays LOCKED " 736 "(disk as found; NVMe stays bound for the stop order)\n"); 737 return 0; 738 } 739 return 1; 740 } 741 #endif 742 743 #ifdef CK_PI5_UNLOCK 744 /* Pi unlock image policy (ck_stage_store calls this, the host test pi5_unlock_test drives it). 745 * secret NULL (no source gave one): refused before the disk is read, nothing written. Otherwise the 746 * Slot 1 recovery path with the hardware policy: a wrong secret fails the K_vol unwrap tag before the 747 * disk is opened; a disk with no KeySlotManifest (for example a TEST-tier Store) is refused; a blank 748 * disk is formatted with a PRODUCTION-class Store under a freshly drawn uuid. */ 749 int ck_store_pi5_unlock_run(const disk_dev *d, const uint8_t *secret, const char *commit, ck_store_report *r) 750 { 751 if (!secret) { 752 bz(r, sizeof *r); 753 bcp(r->prev_commit, "none", 5); 754 r->identity_class = M5_ID_PRODUCTION; 755 r->key_source = "recovery-keyslot"; 756 return fail(r, "keyed", "no recovery secret source in this image; disk not read, not formatted", 757 CK_SB_E_BLOCKED_OPERATOR); 758 } 759 return ck_store_unlock_boot(d, secret, 0, commit ? commit : "unknown", CK_SB_HW, r); 760 } 761 #endif 762 763 #ifdef CK_PI5_FACT 764 #if !defined(CK_PI5_UNLOCK) || !defined(CK_PI5_UNLOCK_CARD) 765 #error "CK_PI5_FACT needs CK_PI5_UNLOCK and CK_PI5_UNLOCK_CARD (production-class Store, card request source)" 766 #endif 767 int ck_store_pi5_fact_stage(const ck_store_report *r) 768 { 769 struct ck_fact_result fr; 770 int rc = ck_pi5_fact_run(r->verdict == CK_SB_COMMITTED ? g_ss : 0, &ck_fact_req_boot, &fr); 771 ck_fact_req_clear(); 772 return rc; 773 } 774 #endif 775 776 #ifdef CK_PI5_AGENT 777 #if !defined(CK_PI5_UNLOCK) || !defined(CK_PI5_UNLOCK_CARD) 778 #error "CK_PI5_AGENT needs CK_PI5_UNLOCK and CK_PI5_UNLOCK_CARD (production-class Store, card request source)" 779 #endif 780 int ck_store_pi5_agent_stage(const ck_store_report *r) 781 { 782 struct ck_agent_result ar; 783 return ck_pi5_agent_run(r->verdict == CK_SB_COMMITTED ? g_ss : 0, &ar); 784 } 785 #endif 786 787 /* Kernel stage. ck_dev_boot_disk comes from dev/devices.c. */ 788 const disk_dev *ck_dev_boot_disk(void); 789 void ck_dev_nvme_release(void); 790 791 int ck_stage_store(void) 792 { 793 const disk_dev *d = ck_dev_boot_disk(); 794 if (!d) { 795 ck_printf("store: REFUSED proof=io step=\"no boot disk\" (NVMe not bound; see nvme/dma_gate lines)\n"); 796 ck_art_stage_load(0, "no boot disk"); 797 return -1; 798 } 799 ss_keys keys; 800 ck_store_report r; 801 const char *commit = ck_commit(); 802 int rc; 803 #ifdef CK_HARDWARE_STAGING 804 { 805 uint8_t fp[32]; 806 sha256_hash(ck_owner_root_pk, sizeof ck_owner_root_pk, fp); 807 ck_printf("owner: provisioned at build time (public only): owner_root_`ed25519` sha256=%02x%02x%02x%02x%02x%02x%02x%02x " 808 "store_uuid=%02x%02x%02x%02x...\n", 809 fp[0], fp[1], fp[2], fp[3], fp[4], fp[5], fp[6], fp[7], ck_owner_store_uuid[0], 810 ck_owner_store_uuid[1], ck_owner_store_uuid[2], ck_owner_store_uuid[3]); 811 } 812 rc = ck_store_production_keys(&keys); 813 if (rc) { 814 bz(&keys, sizeof keys); 815 ck_printf("store: REFUSED proof=keyed step=\"production Store key source\" rc=%d (BlockedOperator: " 816 "K_vol needs the TRUST-1 key ceremony, Gate 6 TPM keyslot or the operator recovery keyslot); " 817 "disk not read, not formatted\n", 818 rc); 819 ck_art_stage_load(0, "production Store key source BLOCKED_OPERATOR"); 820 #ifdef CK_SERVICE 821 g_service_locked_rc = rc; 822 g_disk = d; 823 ck_puts("store: service lifetime: Store LOCKED, NVMe stays bound for `unlock <64-hex recovery secret>` " 824 "or the lifecycle stop order\n"); 825 #else 826 ck_dev_nvme_release(); 827 #endif 828 return rc; 829 } 830 rc = store_boot_run(d, &keys, ck_owner_store_uuid, commit ? commit : "unknown", &r); 831 #else 832 #ifdef CK_TEST_STORE_CRASH 833 d = ck_store_crash_setup(d); 834 #endif 835 #ifdef CK_TEST_CONTINUITY 836 { 837 /* K-7: a modes 5..11 plan runs the continuity / Recovery Core test 838 * boot INSTEAD of the boot-record commit (svc/continuity_boot.h). */ 839 int crc = 0; 840 if (ck_cont_boot_stage(d, &crc)) { 841 ck_art_stage_load(0, "continuity test mode"); 842 ck_dev_nvme_release(); 843 return crc; 844 } 845 } 846 #endif 847 #if defined(CK_SERVICE) && !defined(CK_SERVICE_TEST_KEYS) && !defined(CK_PI5_UNLOCK) 848 /* Production service image without owner provisioning: the production 849 * key source only (it refuses today: no in-kernel K_vol unwrap, P04). No 850 * TEST key is ever used here. */ 851 rc = ck_store_production_keys(&keys); 852 bz(&keys, sizeof keys); 853 if (rc == 0) rc = CK_SB_E_BLOCKED_OPERATOR; /* no Store UUID without CK_HARDWARE_STAGING provisioning */ 854 ck_printf("store: REFUSED proof=keyed step=\"production Store key source\" rc=%d (service image stays LOCKED: " 855 "K_vol needs the TRUST-1 key ceremony, Gate 6 TPM keyslot or the operator recovery keyslot); " 856 "disk not read, not formatted\n", rc); 857 ck_art_stage_load(0, "production Store key source BLOCKED_OPERATOR"); 858 g_service_locked_rc = rc; 859 g_disk = d; 860 ck_puts("store: service lifetime: Store LOCKED, NVMe stays bound for `unlock <64-hex recovery secret>` " 861 "or the lifecycle stop order\n"); 862 (void)commit; 863 return rc; 864 #else 865 #if defined(CK_PI5_UNLOCK) 866 { 867 uint8_t secret[CK_SB_SECRET_LEN]; 868 const char *src = 0; 869 int have = ck_pi5_unlock_secret(secret, &src) == 0; 870 if (have) ck_printf("unlock_source: %s\n", src ? src : "unknown"); 871 else ck_puts("unlock_source: none (no recovery secret given); Store not opened\n"); 872 rc = ck_store_pi5_unlock_run(d, have ? secret : 0, commit, &r); 873 bz(secret, sizeof secret); 874 bz(&keys, sizeof keys); 875 } 876 #else 877 ck_store_test_keys(&keys); 878 rc = store_boot_run(d, &keys, ck_store_test_uuid, commit ? commit : "unknown", &r); 879 #endif 880 #endif 881 #endif 882 bz(&keys, sizeof keys); 883 store_boot_print(&r); 884 /* Signed artifact candidates live in this Store: read them while the disk 885 * is still bound (the loader verifies them later, as untrusted bytes). */ 886 ck_art_stage_load(r.verdict == CK_SB_COMMITTED ? g_ss : 0, r.step); 887 #ifdef CK_PI5_FACT 888 { 889 int frc = ck_store_pi5_fact_stage(&r); 890 if (frc && rc == 0) rc = frc; 891 } 892 #endif 893 #ifdef CK_PI5_AGENT 894 { 895 int arc = ck_store_pi5_agent_stage(&r); 896 if (arc && rc == 0) rc = arc; 897 } 898 #endif 899 #ifdef CK_SERVICE 900 /* Service lifetime (core/lifecycle.c): an open Store keeps the NVMe bound 901 * until the lifecycle stop order releases it, in that order. A refused 902 * Store releases the disk now, exactly as the diagnostic image does. */ 903 if (r.verdict == CK_SB_COMMITTED) { 904 if (!service_continuity()) return CK_SB_E_CONTINUITY; 905 g_service_open = 1; 906 ck_puts("store: service lifetime: Store open, NVMe stays bound until the lifecycle stop order\n"); 907 return rc; 908 } 909 #endif 910 ck_dev_nvme_release(); /* last disk user: revoke NVMe bus mastering */ 911 return rc; 912 } 913 914 #ifdef CK_SERVICE 915 int ck_store_service_ready(void) 916 { 917 return g_service_open && g_ss; 918 } 919 920 /* Stop order step 1: erase the Store keys. Every commit is already durable 921 * (ss_transact returns after its final flush), so nothing is pending. */ 922 int ck_store_service_close(void) 923 { 924 if (!g_service_open) return 0; 925 g_service_open = 0; 926 ck_cont_service_stop(); 927 if (g_ss) bz(&g_ss->keys, sizeof g_ss->keys); 928 return 1; 929 } 930 931 static int hex_nibble(char c) 932 { 933 if (c >= '0' && c <= '9') return c - '0'; 934 if (c >= 'a' && c <= 'f') return c - 'a' + 10; 935 if (c >= 'A' && c <= 'F') return c - 'A' + 10; 936 return -1; 937 } 938 939 /* "unlock" while LOCKED. With no argument the production key source is asked 940 * again and refuses (CK_SB_E_BLOCKED_OPERATOR: no TPM Slot 0 path). With the 941 * operator's 64-hex recovery secret the Slot 1 keyslot path runs on the disk 942 * kept bound at boot (ck_store_unlock_boot). The secret bytes are wiped 943 * before return and never printed. Returns 1 only when the Store is open. */ 944 int ck_store_service_unlock(const char *arg, size_t arg_len) 945 { 946 if (g_service_open) return 1; 947 if (arg_len == 0) { 948 ss_keys keys; 949 int rc = ck_store_production_keys(&keys); 950 bz(&keys, sizeof keys); 951 ck_printf("store: unlock REFUSED proof=keyed step=\"production Store key source\" rc=%d (%s)\n", 952 rc ? rc : g_service_locked_rc, "BlockedOperator: no TPM Slot 0 unwrap; give `unlock <64-hex recovery secret>`"); 953 return 0; 954 } 955 uint8_t secret[CK_SB_SECRET_LEN]; 956 if (arg_len != 2 * CK_SB_SECRET_LEN) { 957 ck_printf("store: unlock REFUSED proof=keyed step=\"recovery secret syntax\" rc=%d (64 hex digits expected, got %u)\n", 958 CK_SB_E_SECRET_REJECTED, (unsigned)arg_len); 959 return 0; 960 } 961 for (size_t i = 0; i < CK_SB_SECRET_LEN; i++) { 962 int hi = hex_nibble(arg[2 * i]), lo = hex_nibble(arg[2 * i + 1]); 963 if (hi < 0 || lo < 0) { 964 bz(secret, sizeof secret); 965 ck_printf("store: unlock REFUSED proof=keyed step=\"recovery secret syntax\" rc=%d (not hex)\n", 966 CK_SB_E_SECRET_REJECTED); 967 return 0; 968 } 969 secret[i] = (uint8_t)((hi << 4) | lo); 970 } 971 if (!g_disk) { 972 bz(secret, sizeof secret); 973 ck_printf("store: unlock REFUSED proof=io step=\"no boot disk bound\" rc=%d\n", g_service_locked_rc); 974 return 0; 975 } 976 ck_store_report r; 977 const char *commit = ck_commit(); 978 #ifdef CK_HARDWARE_STAGING 979 const uint8_t *expect = ck_owner_store_uuid; 980 #else 981 const uint8_t *expect = 0; 982 #endif 983 int rc = ck_store_unlock_boot(g_disk, secret, expect, commit ? commit : "unknown", CK_SB_HW, &r); 984 bz(secret, sizeof secret); 985 store_boot_print(&r); 986 if (rc || r.verdict != CK_SB_COMMITTED) { 987 ck_printf("store: unlock REFUSED proof=%s step=\"%s\" rc=%d (%s); still LOCKED, disk not reformatted\n", 988 r.proof, r.step, r.rc, err_name(r.rc)); 989 g_service_locked_rc = r.rc; 990 return 0; 991 } 992 g_service_open = 1; 993 ck_printf("store: unlock ok source=recovery-keyslot slot=%u genesis=%d generation=%llu boot_count=%llu\n", 994 (unsigned)CK_RECOVERY_SLOT_ID, r.unlocked_genesis, (unsigned long long)r.gen_commit, 995 (unsigned long long)r.boot_count_new); 996 /* Signed artifact candidates live in this Store; read them now that it is open. */ 997 ck_art_stage_load(g_ss, r.step); 998 if (!service_continuity()) { 999 g_service_open = 0; 1000 return 0; 1001 } 1002 return 1; 1003 } 1004 1005 /* 64 hex digits -> 32 secret bytes; 0 ok. out is wiped on refusal. */ 1006 static int parse_secret(const char *a, size_t n, uint8_t out[CK_SB_SECRET_LEN]) 1007 { 1008 if (n != 2 * CK_SB_SECRET_LEN) return -1; 1009 for (size_t i = 0; i < CK_SB_SECRET_LEN; i++) { 1010 int hi = hex_nibble(a[2 * i]), lo = hex_nibble(a[2 * i + 1]); 1011 if (hi < 0 || lo < 0) { 1012 bz(out, CK_SB_SECRET_LEN); 1013 return -1; 1014 } 1015 out[i] = (uint8_t)((hi << 4) | lo); 1016 } 1017 return 0; 1018 } 1019 1020 /* "rotate " while READY (P04 cut 2b). Both 1021 * secrets are wiped before return and never printed. Returns 1 only when 1022 * the rotation transaction committed. */ 1023 int ck_store_service_rotate(const char *arg, size_t arg_len) 1024 { 1025 uint8_t cur[CK_SB_SECRET_LEN], nxt[CK_SB_SECRET_LEN]; 1026 ck_store_report r; 1027 if (!g_service_open || !g_disk) { 1028 ck_printf("store: rotate REFUSED proof=keyed step=\"Store not open\" rc=%d\n", CK_SB_E_NO_KEYSLOT); 1029 return 0; 1030 } 1031 if (arg_len != 4 * CK_SB_SECRET_LEN + 1 || arg[2 * CK_SB_SECRET_LEN] != ' ' || 1032 parse_secret(arg, 2 * CK_SB_SECRET_LEN, cur) != 0 || 1033 parse_secret(arg + 2 * CK_SB_SECRET_LEN + 1, 2 * CK_SB_SECRET_LEN, nxt) != 0) { 1034 bz(cur, sizeof cur); 1035 bz(nxt, sizeof nxt); 1036 ck_printf("store: rotate REFUSED proof=keyed step=\"recovery secret syntax\" rc=%d (two 64-hex secrets expected, got %u bytes)\n", 1037 CK_SB_E_SECRET_REJECTED, (unsigned)arg_len); 1038 return 0; 1039 } 1040 int rc = ck_store_rotate_recovery(g_disk, cur, nxt, &r); 1041 bz(cur, sizeof cur); 1042 bz(nxt, sizeof nxt); 1043 if (rc || r.verdict != CK_SB_COMMITTED) { 1044 ck_printf("store: rotate REFUSED proof=%s step=\"%s\" rc=%d (%s); Store unchanged, still open\n", r.proof, r.step, 1045 r.rc, err_name(r.rc)); 1046 return 0; 1047 } 1048 ck_printf("store: rotated recovery keyslot manifest_sequence=%llu security_sequence=%llu generation=%llu (previous secret retired)\n", 1049 (unsigned long long)r.keyslot_seq, (unsigned long long)r.secman_seq, (unsigned long long)r.gen_commit); 1050 return 1; 1051 } 1052 #endif 1053 1054 ss_store *store_boot_store(void) 1055 { 1056 return g_ss; 1057 }