From 7dda64bca70f9c3f942ea0bbfa30d9a7880be413 Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 01:15:22 +0100 Subject: [PATCH] Seed new vpac roots from a pinned packaged public certificate --- tests/FEED.md | 16 ++++++ tools/keyring.py | 98 ++++++++++++++++++++++++++++++++++ vinix-keyring/PKGBUILD.vile.in | 16 ++++++ 3 files changed, 130 insertions(+) create mode 100644 tools/keyring.py create mode 100644 vinix-keyring/PKGBUILD.vile.in diff --git a/tests/FEED.md b/tests/FEED.md index 2043efe..da3f384 100644 --- a/tests/FEED.md +++ b/tests/FEED.md @@ -101,3 +101,19 @@ v -o "$work/boot" tests/busybox/boot.v ``` The x86_64 audit verified 27 signed package records, 9,583 owned files and links, and 110 target ELFs. Inside Vinix, the fixture loaded Zsh modules, edited a file with Vim, ran a Makefile, round-tripped compressed files, resolved pkg-config modules, parsed XML and verified the packaged ISRG root through OpenSSL. The serial log ends with `VPAC USERLAND GUEST PASS`. The worker keeps it under `musl-busybox-CqkBEA/foundation/userland-guest-01/serial.log` (SHA-256 `237592ae4c71ca99438e4fba943472c44c6890f60495a43204202c7ba2eb0837`). Boot harness revision `0a0ab0a` supports this run. + +### Seed a fresh image with a distributor's verification key + +Provide a public OpenPGP certificate and its full fingerprint from the trusted release configuration. Choose a new numeric package version each time the public material changes. Prepare the source port in worker state, build it through Vile and install it into the new image using the builder's existing trusted keyring. + +```sh +python3 tools/keyring.py prepare "$public_certificate" "$trusted_fingerprint" "$version" "$work/vinix-keyring" +vpac --native-build "$work/vinix-keyring/PKGBUILD.vile" "$work/keyring.manifest" +# Include the archive listed in keyring.manifest when publishing the signed feed. +feed install "$builder_keys" "$repo" x86_64 "$work/new-client" --fakeroot musl vinix-keyring +python3 tools/keyring.py seed "$work/new-client/root" "$trusted_fingerprint" +``` + +`vinix-keyring` owns the public certificate and fingerprint in `/usr/share/vpac/keyrings/`. Image assembly initializes the mutable GnuPG keybox and trust database under `/etc/vpac.d/gnupg/`. The seed operation requires a fresh destination. The builder compares the independently supplied fingerprint before packaging and again before seeding. + +The worker used a disposable test signer in `foundation/keyring-03`. It rejected a wrong pin, installed the signed keyring, verified the signed database with the seeded keyring, and used that keyring alone for an offline vpac install of musl into another empty root. A second seed attempt preserved the existing keyring. The checksum manifest has SHA-256 `9d0868ef14da385aeddbca46b97cb154fdfe9ccd8b69a3e90d5a3c5f7f36ed3c`. The target-vpac guest transaction and provisioning the release signer's public certificate remain release checks. diff --git a/tools/keyring.py b/tools/keyring.py new file mode 100644 index 0000000..3acd7eb --- /dev/null +++ b/tools/keyring.py @@ -0,0 +1,98 @@ +"""Package a pinned public certificate or seed a new image's vpac verification keyring.""" +import argparse +import hashlib +import os +import re +import shutil +import subprocess +import tempfile +from pathlib import Path + + +def gpg(home, *arguments, data=None): + return subprocess.run(["gpg", "--homedir", home, "--no-options", "--batch", "--no-tty", + "--no-autostart", *arguments], check=True, input=data, stdout=subprocess.PIPE).stdout + + +def fingerprint(value): + value = value.upper() + if not re.fullmatch(r"(?:[0-9A-F]{40}|[0-9A-F]{64})", value): + raise argparse.ArgumentTypeError("expected the full OpenPGP fingerprint") + return value + + +def import_certificate(home, public_key, expected): + records = gpg(home, "--with-colons", "--import-options", "show-only", "--import", public_key) + types = [line.split(b":", 1)[0] for line in records.splitlines()] + if types.count(b"pub") != 1 or b"sec" in types or b"ssb" in types: + raise ValueError("expected one public OpenPGP certificate") + first = next((line.split(b":")[9].decode() for line in records.splitlines() if line.startswith(b"fpr:")), "") + if first != expected: + raise ValueError("certificate fingerprint differs from the trusted release fingerprint") + gpg(home, "--import", public_key) + if any(line.startswith((b"sec:", b"ssb:")) for line in gpg(home, "--with-colons", "--list-secret-keys").splitlines()): + raise ValueError("secret key found in verification keyring") + + +def prepare(args): + if not re.fullmatch(r"[0-9]+(?:\.[0-9]+)*", args.version): + raise ValueError("expected a numeric keyring package version") + with tempfile.TemporaryDirectory() as directory: + home = Path(directory) + import_certificate(home, args.public_key, args.fingerprint) + public = gpg(home, "--export", args.fingerprint) + if not public: + raise ValueError("could not export public certificate") + pin = (args.fingerprint + "\n").encode() + template = Path(__file__).resolve().parent.parent / "vinix-keyring/PKGBUILD.vile.in" + recipe = (template.read_text().replace("@VERSION@", args.version) + .replace("@PUBLIC_SHA256@", hashlib.sha256(public).hexdigest()) + .replace("@FINGERPRINT_SHA256@", hashlib.sha256(pin).hexdigest())) + args.output.mkdir() + (args.output / "vinix.gpg").write_bytes(public) + (args.output / "vinix.fingerprint").write_bytes(pin) + (args.output / "PKGBUILD.vile").write_text(recipe) + + +def seed(args): + # For freshly assembled roots. Upgrading live trust state requires a key rotation policy. + root = args.root.resolve(strict=True) + if root == Path("/") or not root.is_dir(): + raise ValueError("expected a staged image directory") + certificates = root / "usr/share/vpac/keyrings" + parent = root / "etc/vpac.d" + for path in (parent, certificates / "vinix.gpg", certificates / "vinix.fingerprint"): + if path.resolve() != path: + raise ValueError(f"unexpected symlink in image keyring path: {path}") + if (certificates / "vinix.fingerprint").read_text().strip() != args.fingerprint: + raise ValueError("installed keyring fingerprint differs from the trusted release fingerprint") + parent.mkdir(parents=True, exist_ok=True) + dest = parent / "gnupg" + if os.path.lexists(dest): + raise ValueError(f"verification keyring already exists: {dest}") + # Long image paths can exceed the Unix socket limit used by GnuPG. + with tempfile.TemporaryDirectory() as directory, tempfile.TemporaryDirectory(dir=parent) as staging: + home = Path(directory) + import_certificate(home, certificates / "vinix.gpg", args.fingerprint) + gpg(home, "--import-ownertrust", data=(args.fingerprint + ":6:\n").encode()) + gpg(home, "--check-trustdb") + keybox = "pubring.kbx" if (home / "pubring.kbx").is_file() else "pubring.gpg" + for name in (keybox, "trustdb.gpg"): + shutil.copyfile(home / name, Path(staging) / name) + (Path(staging) / name).chmod(0o600) + Path(staging).rename(dest) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + sub = parser.add_subparsers(dest="command", required=True) + make = sub.add_parser("prepare", help="materialize a reproducible Vile port outside Git") + make.add_argument("public_key", type=Path) + make.add_argument("fingerprint", type=fingerprint) + make.add_argument("version") + make.add_argument("output", type=Path) + image = sub.add_parser("seed", help="initialize trust in a new, signed-package-installed root") + image.add_argument("root", type=Path) + image.add_argument("fingerprint", type=fingerprint) + args = parser.parse_args() + (prepare if args.command == "prepare" else seed)(args) diff --git a/vinix-keyring/PKGBUILD.vile.in b/vinix-keyring/PKGBUILD.vile.in new file mode 100644 index 0000000..8f9224f --- /dev/null +++ b/vinix-keyring/PKGBUILD.vile.in @@ -0,0 +1,16 @@ +; tools/keyring.py prepares this recipe using the distributor's public certificate. +defpkgbuild(vinix-keyring, + #:pkgname, "vinix-keyring", + #:pkgver, "@VERSION@", + #:pkgrel, 1, + #:pkgdesc, "Public certificate used to verify the Vinix package repository", + #:arch, [#:any], + #:url, "https://github.com/vlang/vinix", + #:license, "0BSD", + #:source, ["vinix.gpg", "vinix.fingerprint"], + #:sha256sums, ["@PUBLIC_SHA256@", "@FINGERPRINT_SHA256@"], + #:package, fn() { + for-each(fn(file) { + run("install", "-Dm644", file, string-append(pkgdir(), "/usr/share/vpac/keyrings/", file)) + }, list("vinix.gpg", "vinix.fingerprint")) + }) -- 2.56.1