diff --git a/CLAUDE.md b/CLAUDE.md index 441eadba..d9011859 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -888,8 +888,8 @@ TABLES_BSS. between each, matrix in `tools/package/_common.sh`: c64-https-ip65-onchip.prg stock C64 + RR-Net, no REU, no turbo - c64-https-uci-onchip.prg turbo, no REU - c64-https-uci-comb.prg turbo + REU, fastest (needs bank 2 + boot precompute) + c64-https-uci-onchip.prg turbo, no REU (TRUST_STORE=1) + c64-https-uci-comb.prg turbo + REU, fastest (needs bank 2 + boot precompute; TRUST_STORE=1) REU-profile images were retired (curation: still fastest below ~18 MHz, one line in `PACKAGE_VARIANTS` to restore). **That line is a diff --git a/Makefile b/Makefile index e87d3187..4a97baab 100644 --- a/Makefile +++ b/Makefile @@ -281,8 +281,20 @@ endif # public key and generation floor come from TRUST_BUNDLE_KEY_INC, a file # `tools/trust_bundle.py inc` writes. The tree's default is the TEST-ONLY # key, whose private half is committed: anyone can sign for it. +# TRUST_RELEASE=1 marks a release build (tools/package/build_prgs.sh passes +# it for every product) and refuses that key three times over: here at parse +# time, by the .inc's 64 `$$XX` tokens (fast, but textual); in trust_bundle.s, +# by its TRUST_BUNDLE_KEY_IS_TEST_ONLY flag; and after the link, by the +# ASSEMBLED bytes (tools/check_release_key.py searches the PRG for the test +# key derived from its PEM, and the PRG is deleted on a hit), which no +# spelling of the .inc gets past. TRUST_BUNDLE ?= TRUST_BUNDLE_KEY_INC ?= tools/trust_bundle_TEST_ONLY_pubkey.inc +TRUST_BUNDLE_TEST_KEY_INC := tools/trust_bundle_TEST_ONLY_pubkey.inc +TRUST_RELEASE ?= +ifneq ($(filter-out 0 1,$(TRUST_RELEASE)),) +$(error TRUST_RELEASE must be 1 (a release build) or 0/unset) +endif # The 64 key bytes of an .inc, as one word: what the guard compares. trust_key_bytes = $(shell sed -n '/^\.macro TRUST_BUNDLE_PUBKEY_BYTES/,/^\.endmacro/p' '$(1)' 2>/dev/null | grep -o '\$$[0-9A-Fa-f][0-9A-Fa-f]' | tr -d '\n$$' | tr a-f A-F) ifeq ($(TRUST_BUNDLE),1) @@ -296,6 +308,12 @@ TRUST_BUNDLE_KEY_HEX := $(call trust_key_bytes,$(TRUST_BUNDLE_KEY_INC)) ifneq ($(words $(TRUST_BUNDLE_KEY_HEX)) $(shell printf '%s' '$(TRUST_BUNDLE_KEY_HEX)' | wc -c | tr -d ' '),1 128) $(error TRUST_BUNDLE_KEY_INC=$(TRUST_BUNDLE_KEY_INC) has no 64-byte TRUST_BUNDLE_PUBKEY_BYTES macro) endif +ifeq ($(TRUST_RELEASE),1) +ifeq ($(TRUST_BUNDLE_KEY_HEX),$(call trust_key_bytes,$(TRUST_BUNDLE_TEST_KEY_INC))) +$(error TRUST_RELEASE=1 with the TEST-ONLY trust-bundle key ($(TRUST_BUNDLE_KEY_INC)): its private half is in the repository, so anyone could sign a bundle this release accepts. Build with TRUST_BUNDLE_KEY_INC=, or without TRUST_BUNDLE) +endif +CA65FLAGS += -D TRUST_RELEASE=1 +endif CA65FLAGS += -D TRUST_BUNDLE=1 UCI_SRCS += src/net/uci/trust_bundle.s else ifneq ($(filter-out 0,$(TRUST_BUNDLE)),) @@ -660,6 +678,9 @@ $(PRG): $(PRG_DEPS) @grep -q '^al C:BC00 \.sqtab_lo' $(LABELS) || \ { echo 'ERROR: sqtab_lo is not at $$BC00 — TABLES_BSS layout drifted; realign LIB_SHARED_SQTAB_BASE in tools/integration/build_x25519.sh and build_nistcurves_p256.sh'; \ grep ' \.sqtab_lo$$' $(LABELS); exit 1; } +ifeq ($(TRUST_RELEASE)$(TRUST_BUNDLE),11) + @python3 tools/check_release_key.py $@ || { rm -f $@; exit 1; } +endif # Phase 5 Fix D: $(LABELS) is normally a side-effect of the $(PRG) # link recipe; we don't add an explicit rule. The overlay-bin rule @@ -1062,6 +1083,12 @@ endif # pair, whichever of them grows a direct dependency next. build/boot.o build/http.o build/cert_pin.o: build/https_host.inc build/net/uci/trust_store.o: build/https_host.inc +# Only reached when the parse-time write above did not run (a -n/-q/-t dry +# run on a tree that has never built): the same write, as a recipe, so a +# dry run of a fresh checkout answers instead of dying "No rule to make". +build/https_host.inc: + @mkdir -p build + $(HTTPS_TARGET_INC_BODY) > $@ # The trust-bundle key (#155 phase 2, L3) gets the same content-compared # copy, build/trust_key.inc: a different key or floor rebuilds exactly diff --git a/src/net/uci/trust_bundle.s b/src/net/uci/trust_bundle.s index abd560e2..e1d876a2 100644 --- a/src/net/uci/trust_bundle.s +++ b/src/net/uci/trust_bundle.s @@ -21,7 +21,8 @@ ; then the record for this attempt's ts_key, if any, goes to tb_spki ; (tb_found = 1): the hook in src/cert_pin.s warns if the server's key ; differs. The public key and the floor come from build/trust_key.inc -; (TRUST_BUNDLE_KEY_INC; the tree's key is TEST-ONLY). +; (TRUST_BUNDLE_KEY_INC; the tree's key is TEST-ONLY, and a release build +; refuses it — see the assert below and the Makefile). ; ; VERIFY ONCE PER BOOT (DECISIONS 11, Q4): the ECDSA verify (16-30 s at ; 48 MHz) runs at the first 'G' that finds the file. Its digest and verdict @@ -69,6 +70,11 @@ TB_V_NONE = 0 ; tb_verdict: nothing judged yet TB_V_GOOD = 1 TB_V_BAD = 2 +; A release (make package) must not carry the key whose private half is in +; the repository. The Makefile refuses it too, by its bytes. +.ifdef TRUST_RELEASE +.assert TRUST_BUNDLE_KEY_IS_TEST_ONLY = 0, error, "TEST-ONLY trust-bundle key in a release build (TRUST_BUNDLE_KEY_INC)" +.endif .assert TB_HEADER_LEN = 8 .and TB_RECORD_LEN = TS_REC_SIZE .and TB_SIG_LEN = 64, error, "bundle format v1" .assert TB_FILE_MAX + 1 <= TCP_RECV_MASK + 1, error, "bundle > TCP ring" .assert TB_MAX_RECORDS < 64, error, "tb_body_len: N * 64 must fit in 12 bits" diff --git a/tools/check_release_key.py b/tools/check_release_key.py new file mode 100644 index 00000000..6047ff4e --- /dev/null +++ b/tools/check_release_key.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +"""check_release_key.py — refuse a linked PRG that carries the TEST-ONLY bundle key. + +#155 phase 2, L3. The Makefile's parse-time guard compares the key .inc's +`$XX` tokens, which is text: an .inc that spells a byte in decimal (`234` +for `$EA`) or hides a `$00` in a comment slips past it while ca65 still +assembles the test key (adv-269 #1). This looks at what was ASSEMBLED: it +derives the test key's 64 bytes (Qx || Qy, the layout trust_bundle.s +embeds) from tools/trust_bundle_TEST_ONLY_signing_key.pem — no .inc parsed — +and searches the whole PRG for them. Anywhere is enough: the key has no +business in a release image, and a whole-file search needs no label math +for the comb cold image (linked to run in cert_buf, carried at $C000). + +The Makefile runs it after the link under TRUST_RELEASE=1 TRUST_BUNDLE=1 +and deletes the PRG when it fails. + + python3 tools/check_release_key.py build/c64-https.prg + +Exit 0 the key is absent; 1 it is present, or the check could not run +(fail closed: this only ever runs for a release). +""" +from __future__ import annotations + +import sys +from pathlib import Path + +HERE = Path(__file__).resolve().parent + + +def test_key_bytes() -> bytes: + sys.path.insert(0, str(HERE)) + import trust_bundle as tb # noqa: PLC0415 — needs `cryptography` + return tb.pubkey_xy(tb.load_private_key(tb.TEST_KEY_PATH).public_key()) + + +def main(argv) -> int: + if len(argv) != 1: + print("usage: check_release_key.py ", file=sys.stderr) + return 1 + try: + key = test_key_bytes() + prg = Path(argv[0]).read_bytes() + except Exception as exc: # noqa: BLE001 — fail closed + print(f"ERROR: release key check could not run ({type(exc).__name__}: {exc})", + file=sys.stderr) + return 1 + assert len(key) == 64 + at = prg.find(key) + if at >= 0: + print(f"ERROR: {argv[0]} carries the TEST-ONLY trust-bundle key (PRG offset {at}): " + "its private half is in the repository. Build the release with " + "TRUST_BUNDLE_KEY_INC=.", file=sys.stderr) + return 1 + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1:])) diff --git a/tools/package/_common.sh b/tools/package/_common.sh index bcc8d3b2..52de71f1 100644 --- a/tools/package/_common.sh +++ b/tools/package/_common.sh @@ -23,13 +23,22 @@ # and every downstream script picks it up with no further edits. PACKAGE_VARIANTS=( "ip65-onchip|c64-https-ip65-onchip.prg|BACKEND=ip65 USE_NISTCURVES_ONCHIP=1|ip65-noreu|ip65|MAXIMUM COMPATIBILITY. Bone-stock C64 + RR-Net / cs8900a cartridge. No REU, no turbo, nothing optional at all — the only image a completely unmodified machine can run end to end. Slowest: ~36 min per handshake at 1 MHz." - "uci-onchip|c64-https-uci-onchip.prg|BACKEND=uci USE_NISTCURVES_ONCHIP=1|uci-noreu|uci|TURBO, NO REU. Ultimate 64 / C64 Ultimate at 32/48/64 MHz with the REU disabled or absent. Boots straight to the menu." - "uci-comb|c64-https-uci-comb.prg|BACKEND=uci USE_NISTCURVES_ONCHIP_COMB=1|uci-comb|uci|TURBO + REU — FASTEST. Ultimate 64 / C64 Ultimate at 32/48/64 MHz with the REU enabled. ~1.7x faster verify than the no-REU turbo image (measured: 16.4 s vs 28.4 s on a U64E at 48 MHz). Costs a one-time table build at every boot: ~34 s at 64 MHz, ~45 s at 48 MHz. Needs REU bank 2." + "uci-onchip|c64-https-uci-onchip.prg|BACKEND=uci USE_NISTCURVES_ONCHIP=1 TRUST_STORE=1|uci-noreu|uci|TURBO, NO REU. Ultimate 64 / C64 Ultimate at 32/48/64 MHz with the REU disabled or absent. Boots straight to the menu. Remembers each server's key on first use (TRUST.A / TRUST.B on /USB1) and refuses a changed one unless you accept it." + "uci-comb|c64-https-uci-comb.prg|BACKEND=uci USE_NISTCURVES_ONCHIP_COMB=1 TRUST_STORE=1|uci-comb|uci|TURBO + REU — FASTEST. Ultimate 64 / C64 Ultimate at 32/48/64 MHz with the REU enabled. ~1.7x faster verify than the no-REU turbo image (measured: 16.4 s vs 28.4 s on a U64E at 48 MHz). Costs a one-time table build at every boot: ~34 s at 64 MHz, ~45 s at 48 MHz. Needs REU bank 2. Remembers each server's key on first use (TRUST.A / TRUST.B on /USB1) and refuses a changed one unless you accept it." ) # Field accessors — `variant_field <1-based index>`. variant_field() { printf '%s' "$1" | cut -d'|' -f"$2"; } +# The make arguments a variant is BUILT with: its matrix args plus +# TRUST_RELEASE=1, which marks a release build. Under TRUST_BUNDLE=1 the +# Makefile then refuses the TEST-ONLY bundle key (#155 phase 2, L3); +# without the bundle it changes no byte. build_prgs.sh records exactly +# this in build-info, so verify_release.py rebuilds with it too. The +# bundle stays out of the matrix until a production key exists +# (DECISIONS 11, Q1). +variant_make_args() { printf '%s TRUST_RELEASE=1' "$(variant_field "$1" 3)"; } + # --- Paths -------------------------------------------------------------------- # PROJECT_ROOT must be set by the caller before sourcing (it knows its own $0). : "${PROJECT_ROOT:?_common.sh: PROJECT_ROOT must be set before sourcing}" diff --git a/tools/package/build_prgs.sh b/tools/package/build_prgs.sh index 3b1c5a7c..4a111be3 100755 --- a/tools/package/build_prgs.sh +++ b/tools/package/build_prgs.sh @@ -5,8 +5,10 @@ # Three variants, one per shipped product (PACKAGE_VARIANTS in _common.sh): # # c64-https-ip65-onchip.prg make BACKEND=ip65 USE_NISTCURVES_ONCHIP=1 -# c64-https-uci-onchip.prg make BACKEND=uci USE_NISTCURVES_ONCHIP=1 -# c64-https-uci-comb.prg make BACKEND=uci USE_NISTCURVES_ONCHIP_COMB=1 +# c64-https-uci-onchip.prg make BACKEND=uci USE_NISTCURVES_ONCHIP=1 TRUST_STORE=1 +# c64-https-uci-comb.prg make BACKEND=uci USE_NISTCURVES_ONCHIP_COMB=1 TRUST_STORE=1 +# +# each with TRUST_RELEASE=1 appended (variant_make_args in _common.sh). # # The matrix itself lives in _common.sh; this script has no per-variant # knowledge and nothing version-specific, so it survives a library bump with @@ -126,7 +128,7 @@ failed=0 for line in "${PACKAGE_VARIANTS[@]}"; do key="$(variant_field "$line" 1)" prg="$(variant_field "$line" 2)" - args="$(variant_field "$line" 3)" + args="$(variant_make_args "$line")" echo "[package] === $key ===" echo "[package] make clean && make $args" diff --git a/tools/package/verify_release.py b/tools/package/verify_release.py index 682849e9..dd37e87f 100755 --- a/tools/package/verify_release.py +++ b/tools/package/verify_release.py @@ -23,7 +23,8 @@ SKIP_REBUILD=1 skip check 1 (it costs four full builds) SKIP_VICE=1 skip the VICE boots (keeps the c1541 byte-compare) SKIP_LISTENER=1 skip check 3 - VICE_BOOT_TIMEOUT seconds to wait for the menu (default 180) + VICE_BOOT_TIMEOUT seconds to wait for the banner (default 600: a loaded + host took 90 s for ip65-onchip and up to 150 s for uci) """ from __future__ import annotations @@ -225,7 +226,7 @@ def check_d64_boots(variants: list[dict]) -> None: except Exception as exc: # noqa: BLE001 record("c64_test_harness importable", False, f"{type(exc).__name__}: {exc}") return - timeout = float(os.environ.get("VICE_BOOT_TIMEOUT", "240")) + timeout = float(os.environ.get("VICE_BOOT_TIMEOUT", "600")) import time expected = expected_d64_images(variants) present = set(d64_images()) @@ -288,6 +289,8 @@ def check_d64_boots(variants: list[dict]) -> None: detail = f"nothing recognisable on screen within {timeout:.0f}s" if foreign: detail += f"; UNEXPECTED foreign banner {foreign}" + if not ok: + detail += f" (boot budget {timeout:.0f}s; VICE_BOOT_TIMEOUT to change)" record(f"{image.name} boots to the banner", ok, detail) diff --git a/tools/package/write_manifest.sh b/tools/package/write_manifest.sh index 6c6af99f..0645dd2b 100755 --- a/tools/package/write_manifest.sh +++ b/tools/package/write_manifest.sh @@ -53,7 +53,7 @@ grep '^failreason=' "$BUILD_INFO" | cut -d= -f2- | while read -r key reason; do [ "$(variant_field "$line" 1)" = "$key" ] || continue prg="$(variant_field "$line" 2)" echo " $prg" - echo " make $(variant_field "$line" 3)" + echo " make $(variant_make_args "$line")" done [ -n "$prg" ] || echo " $key" echo " $reason" diff --git a/tools/test_trust_release_guard.py b/tools/test_trust_release_guard.py new file mode 100644 index 00000000..2cfdcffd --- /dev/null +++ b/tools/test_trust_release_guard.py @@ -0,0 +1,182 @@ +#!/usr/bin/env python3 +"""test_trust_release_guard.py — a release never carries the TEST-ONLY bundle key. + +#155 phase 2, L3 (DECISIONS 10): the tree's trust-bundle key +(tools/trust_bundle_TEST_ONLY_pubkey.inc) has its private half committed, so +anyone can sign a bundle an image built with it accepts. `make package` +must refuse it. Two independent layers, each checked here: + + Makefile TRUST_RELEASE=1 + TRUST_BUNDLE=1 with the test key's 64 `$XX` + tokens is a parse-time $(error) — even if the .inc claims + TRUST_BUNDLE_KEY_IS_TEST_ONLY = 0; + link that check is textual, so the link also runs + tools/check_release_key.py over the ASSEMBLED PRG (the test key + derived from its PEM) — pinned here by the recipe it prints and + by the script's verdict on synthetic images; + source src/net/uci/trust_bundle.s `.assert`s the flag under + -D TRUST_RELEASE (ca65 run directly, the Makefile bypassed). + +and the wiring: every UCI product's args as tools/package/build_prgs.sh +builds them (variant_make_args in _common.sh) carry TRUST_RELEASE=1, so +turning the bundle on in a package line with the test key fails the +build. Controls: a dev build (no TRUST_RELEASE) with the test key, and a +release with a throwaway non-test key, both go through. + +No build and no VICE: `make -n` (which, since #174, evaluates the Makefile +and writes nothing) and one ca65 run. Seconds. + +Exit 0 pass, 1 fail, 2 could not run. +""" +from __future__ import annotations + +import os +import re +import shutil +import subprocess +import sys +import tempfile +from pathlib import Path + +HERE = Path(__file__).resolve().parent +sys.path.insert(0, str(HERE)) +from _skip_policy import cannot_run, verdict # noqa: E402 + +REPO = HERE.parent +TEST_INC = REPO / "tools" / "trust_bundle_TEST_ONLY_pubkey.inc" +CERTIFIES = "the release guard against the TEST-ONLY bundle key (#155, L3)" +UCI_BUNDLE = ["BACKEND=uci", "USE_NISTCURVES_ONCHIP=1", "TRUST_STORE=1", "TRUST_BUNDLE=1"] + +PASSED = 0 +FAILED: list[str] = [] + + +def check(ok, what): + global PASSED + if ok: + PASSED += 1 + else: + FAILED.append(what) + print(f" FAIL: {what}") + + +def make_n(*args): + p = subprocess.run(["make", "-n", *args], cwd=REPO, capture_output=True, text=True) + return p.returncode, p.stdout + p.stderr + + +def package_args(): + """{key: args} exactly as build_prgs.sh builds each product.""" + script = ('PROJECT_ROOT="$1"; . "$1/tools/package/_common.sh"; ' + 'for l in "${PACKAGE_VARIANTS[@]}"; do ' + 'printf "%s|%s\\n" "$(variant_field "$l" 1)" "$(variant_make_args "$l")"; done') + out = subprocess.run(["bash", "-c", script, "_", str(REPO)], capture_output=True, + text=True, check=True).stdout + return dict(line.split("|", 1) for line in out.splitlines() if line) + + +def other_key_inc(tmp: Path) -> Path: + from cryptography.hazmat.primitives import serialization + from cryptography.hazmat.primitives.asymmetric import ec + pem = tmp / "throwaway.pem" + pem.write_bytes(ec.generate_private_key(ec.SECP256R1()).private_bytes( + serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, + serialization.NoEncryption())) + inc = tmp / "throwaway.inc" + subprocess.run([sys.executable, str(HERE / "trust_bundle.py"), "inc", "--key", str(pem), + "--floor", "1", "-o", str(inc)], check=True, capture_output=True) + return inc + + +def main(): + if not shutil.which("make") or not shutil.which("ca65"): + return cannot_run("make / ca65 not on PATH", certifies=CERTIFIES) + tmp = Path(tempfile.mkdtemp(prefix="trust-release-")) + try: + # --- the packaging wiring ------------------------------------------- + prods = package_args() + uci = {k: a for k, a in prods.items() if "BACKEND=uci" in a.split()} + check(len(uci) >= 2, f"expected the UCI products, got {sorted(prods)}") + for key, args in prods.items(): + check("TRUST_RELEASE=1" in args.split(), f"{key}: built without TRUST_RELEASE=1") + check("TRUST_BUNDLE=1" not in args.split(), + f"{key}: the bundle is in a package before a production key exists") + for key in ("uci-onchip", "uci-comb"): + check("TRUST_STORE=1" in prods.get(key, "").split(), + f"{key}: TRUST_STORE=1 is not on (DECISIONS 11, Q1)") + for key, args in uci.items(): # the bundle switched on + rc, out = make_n(*args.split(), "TRUST_BUNDLE=1") + check(rc != 0 and "TEST-ONLY" in out, + f"{key} + TRUST_BUNDLE=1 with the test key: make -n exit {rc}") + + # --- Makefile layer --------------------------------------------------- + rc, out = make_n(*UCI_BUNDLE, "TRUST_RELEASE=1") + check(rc != 0 and "TEST-ONLY" in out, f"release + test key: exit {rc}") + relabelled = tmp / "relabelled.inc" + text = TEST_INC.read_text() + relabelled.write_text(re.sub(r"TRUST_BUNDLE_KEY_IS_TEST_ONLY = 1", + "TRUST_BUNDLE_KEY_IS_TEST_ONLY = 0", text)) + check("IS_TEST_ONLY = 0" in relabelled.read_text(), "could not relabel the test key") + rc, out = make_n(*UCI_BUNDLE, "TRUST_RELEASE=1", f"TRUST_BUNDLE_KEY_INC={relabelled}") + check(rc != 0 and "TEST-ONLY" in out, + f"release + the test key's bytes relabelled not-test-only: exit {rc}") + other = other_key_inc(tmp) + rc, out = make_n(*UCI_BUNDLE, "TRUST_RELEASE=1", f"TRUST_BUNDLE_KEY_INC={other}") + check(rc == 0, f"release + a non-test key was refused (exit {rc}):\n{out[-400:]}") + rc, out = make_n(*UCI_BUNDLE) + check(rc == 0, f"a dev build with the test key was refused (exit {rc})") + rc, out = make_n(*UCI_BUNDLE, "TRUST_RELEASE=2") + check(rc != 0, "TRUST_RELEASE=2 accepted") + + # --- assembled-byte layer (adv-269 #1) -------------------------------- + # The parse-time guard reads `$XX` tokens; an .inc that spells a byte + # in decimal and pads the count with a commented `$00` gets past it. + crafted = tmp / "crafted.inc" + t = TEST_INC.read_text().replace("TRUST_BUNDLE_KEY_IS_TEST_ONLY = 1", + "TRUST_BUNDLE_KEY_IS_TEST_ONLY = 0") + t = t.replace(" .byte $EA, $30,", " .byte 234, $30,", 1) + t = t.replace(" ; Qy, big-endian", " ; Qy, big-endian ; $00") + crafted.write_text(t) + rc, out = make_n(*UCI_BUNDLE, "TRUST_RELEASE=1", f"TRUST_BUNDLE_KEY_INC={crafted}") + check(rc == 0 and "check_release_key.py" in out, + f"crafted .inc: the textual guard passes it (exit {rc}), so the link " + "must run the assembled-byte check -- it is not in the recipe") + rc, out = make_n(*UCI_BUNDLE, f"TRUST_BUNDLE_KEY_INC={other}") + check(rc == 0 and "check_release_key.py" not in out, + "a dev build runs the release key check") + sys.path.insert(0, str(HERE)) + import trust_bundle as tb # noqa: PLC0415 + key = tb.pubkey_xy(tb.load_private_key(tb.TEST_KEY_PATH).public_key()) + for name, body, want in (("test key inside", bytes(5901) + key + bytes(99), 1), + ("no test key", bytes(6000) + key[:63] + bytes(9), 0)): + prg = tmp / f"{name.replace(' ', '_')}.prg" + prg.write_bytes(b"\x01\x08" + body) + p = subprocess.run([sys.executable, str(HERE / "check_release_key.py"), str(prg)], + capture_output=True, text=True) + check(p.returncode == want, f"check_release_key.py, {name}: exit {p.returncode}") + + # --- source layer (Makefile bypassed) -------------------------------- + def assemble(inc: Path, release: bool): + d = tmp / ("inc-" + inc.stem + ("-rel" if release else "")) + d.mkdir() + shutil.copy(inc, d / "trust_key.inc") + cmd = ["ca65", "-I", "src", "-I", "src/net", "-I", "src/net/uci", "-I", str(d), + "-D", "TRUST_STORE=1", "-D", "TRUST_BUNDLE=1", "-D", "BACKEND_UCI=1", + "src/net/uci/trust_bundle.s", "-o", str(d / "tb.o")] + if release: + cmd[1:1] = ["-D", "TRUST_RELEASE=1"] + p = subprocess.run(cmd, cwd=REPO, capture_output=True, text=True) + return p.returncode, p.stdout + p.stderr + rc, out = assemble(TEST_INC, True) + check(rc != 0 and "TEST-ONLY" in out, f"ca65 -D TRUST_RELEASE + test key: exit {rc} {out}") + rc, out = assemble(other, True) + check(rc == 0, f"ca65 -D TRUST_RELEASE + non-test key: exit {rc} {out}") + rc, out = assemble(TEST_INC, False) + check(rc == 0, f"ca65 dev + test key: exit {rc} {out}") + finally: + shutil.rmtree(tmp, ignore_errors=True) + print(f"{PASSED} checks passed, {len(FAILED)} failed") + return verdict(PASSED, len(FAILED), certifies=CERTIFIES) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/trust_bundle.py b/tools/trust_bundle.py index 1eb3c98c..52de4867 100644 --- a/tools/trust_bundle.py +++ b/tools/trust_bundle.py @@ -79,6 +79,25 @@ holding a production key is the maintainer's decision. Signing with the test key prints a warning, and `inc` marks it with TRUST_BUNDLE_KEY_IS_TEST_ONLY. +Production key (the bundle ships only once one exists, DECISIONS 11 Q1). +Keep the private key OUTSIDE the repository; only its public half and the +generation floor enter a build: + + openssl ecparam -name prime256v1 -genkey -noout -out ~/keys/c64-bundle.pem + python3 tools/trust_bundle.py inc --key ~/keys/c64-bundle.pem --floor 1 \\ + -o ~/keys/c64-bundle.inc # public key + floor; safe to share + python3 tools/trust_bundle.py build --key ~/keys/c64-bundle.pem \\ + --generation 1 -o TRUST.P en.wikipedia.org github.com + make BACKEND=uci ... TRUST_STORE=1 TRUST_BUNDLE=1 \\ + TRUST_BUNDLE_KEY_INC=~/keys/c64-bundle.inc + +Copy TRUST.P to TRUST_STORE_DIR (default /USB1). Raise --floor and +--generation together whenever an older bundle must stop being accepted. +A release (`make package`, TRUST_RELEASE=1) refuses the tree's TEST-ONLY key +by its .inc tokens, by its IS_TEST_ONLY flag and by the assembled bytes +(tools/check_release_key.py), so pointing TRUST_BUNDLE_KEY_INC at a real +key is the only way a bundle-enabled release builds. + Exit codes: 0 ok; 1 the bundle is invalid / a fetch failed; 2 usage. """