diff --git a/docs/agent-workflow/STATE.md b/docs/agent-workflow/STATE.md index 19241a0..35d1995 100644 --- a/docs/agent-workflow/STATE.md +++ b/docs/agent-workflow/STATE.md @@ -97,9 +97,13 @@ Stage: VERIFIED — Plans 01–19 + Plan 20 Slices 1–2C + Track A + Track B (B - Added `Void::Build::Provenance::load_trusted_keys` / `normalize_public_key` (`include/Void/Build/Provenance/TrustedKeys.hpp`, `src/Provenance/TrustedKeys.cpp`): keys are loaded from explicit `--trusted-key-file` files, an implicit project `vbs.keys` ring, and `VBS_TRUSTED_PUBLIC_KEYS` (comma/whitespace separated), validated as 64-hex, lowercased, and deduplicated. Malformed keys and unreadable files fail explicitly with `file:line` context. - `vbs build|test` combines inline `--trusted-public-key` values with file/env sources; the daemon build request loads `vbs.keys` + the environment variable before resolving dependencies. `SignatureEnforcement` now has 5 cases. +- **Wave 20 (Track D3 Binary-Variant Signing)**: + - Added generic Ed25519 message primitives (`sign_message`/`verify_message`) in `Provenance::Signing`, optional `signature`/`public_key` on `BinaryVariantMeta`, the canonical `vbs-binary-v1` message and `verify_trusted_binary_signature` trust policy, and `select_compatible_signed_binary`: it selects the first ABI-compatible variant and, when trusted keys are supplied, fails closed on unsigned, untrusted, or invalid variants. With no trusted keys it matches `select_compatible_binary`. + - `SignatureEnforcement` now covers binary-variant policy and signed selection (7 cases). Binary-variant distribution/download remains future work; this is the verification layer used by it. + ## Remaining future scope -- **Post-v1 Expansion Roadmap ([21-expansion-roadmap.md](../roadmap/21-expansion-roadmap.md) & [20-future-translation-platforms.md](../roadmap/20-future-translation-platforms.md))**: Live `conan` CLI verification (Track B3), Track C (macOS/Windows ports + dual-profile cross-compilation), Track D3 signing (SBOM, Ed25519 primitives, trusted-key enforcement and distribution, the #52 built-in-backend capability probe, and the VoidLibrary pin bump to `0002e74` done; binary-variant signing remains), Track E3 (daemon core, bounded `describe`, build-serving, and dependency materialization done; external editor plugins remain), and Track E4 (`Void::Net`/`Void::Sqlite` alignment) remain planned scope. +- **Post-v1 Expansion Roadmap ([21-expansion-roadmap.md](../roadmap/21-expansion-roadmap.md) & [20-future-translation-platforms.md](../roadmap/20-future-translation-platforms.md))**: Live `conan` CLI verification (Track B3), Track C (macOS/Windows ports + dual-profile cross-compilation), Track D3 signing (SBOM, Ed25519 primitives, trusted-key enforcement and distribution, binary-variant signing, the #52 built-in-backend capability probe, and the VoidLibrary pin bump to `0002e74` done; binary-variant distribution remains), Track E3 (daemon core, bounded `describe`, build-serving, and dependency materialization done; external editor plugins remain), and Track E4 (`Void::Net`/`Void::Sqlite` alignment) remain planned scope. ## Environment notes & upstream boundaries diff --git a/docs/agent-workflow/TASKS.md b/docs/agent-workflow/TASKS.md index 75bb389..02fb946 100644 --- a/docs/agent-workflow/TASKS.md +++ b/docs/agent-workflow/TASKS.md @@ -26,4 +26,5 @@ Base HEAD: `fe0d1a6` | Wave 17 (VoidLibrary #52 follow-up + pin bump) | `signing_available()` probes the real Ed25519 capability instead of `has_openssl_backend()`; VoidLibrary pin bumped to `0002e74a7da94a756f55a4a5574d20b77736c185` (contains #52) and pin assertions updated | COMPLETE & VERIFIED (GCC 16.2, fresh remote fetch of the pin, no OpenSSL: Signing 4/4 crypto path, SignatureEnforcement 3/3, full CTest 8/8) | `CMakeLists.txt`, `build.cpp`, `src/Provenance/Signing.cpp`, `include/Void/Build/Provenance/Signing.hpp`, `tests/{SigningTests.cpp,ExampleContexts.cpp,DescribeContracts.cmake}`, docs | | Wave 18 (Track E3 daemon dependency materialization) | Extracted `Package::resolve_and_materialize_dependencies` (`Resolver.hpp/.cpp`) from the CLI; daemon `build` now merges TOML deps, resolves/materializes with the pinned lock, imports provider targets, lowers with imported interfaces, and reports `dependencies` | COMPLETE & VERIFIED (GCC 16.2, pin `0002e74`; Daemon 8/8, full CTest 8/8) | `include/Void/Build/Package/Resolver.hpp`, `src/Package/Resolver.cpp`, `src/Main.cpp`, `src/Daemon/Daemon.cpp`, `tests/DaemonTests.cpp`, build lists, docs | | Wave 19 (Track D3 trusted-key distribution) | `Provenance::load_trusted_keys` over `--trusted-key-file`, project `vbs.keys`, and `VBS_TRUSTED_PUBLIC_KEYS`; 64-hex validation/lowercasing/dedup with `file:line` errors; wired into CLI builds and daemon dependency resolution | COMPLETE & VERIFIED (GCC 16.2, pin `0002e74`; SignatureEnforcement 5/5, Daemon 8/8, full CTest 8/8) | `include/Void/Build/Provenance/TrustedKeys.hpp`, `src/Provenance/TrustedKeys.cpp`, `src/Main.cpp`, `src/Daemon/Daemon.cpp`, `tests/SignatureEnforcementTests.cpp`, build lists, docs | -| Future (Plan 20 Slice 3 / Tracks C, D3 binary signing, E3 editor plugins, E4, live `conan`) | Live `conan` CLI verification; macOS/Windows/cross-compile; binary-variant signing and key distribution; daemon-side dependency materialization and editor plugins; `Void::Net`/`Void::Sqlite` alignment | PLANNED (unimplemented) | [21-expansion-roadmap.md](../roadmap/21-expansion-roadmap.md), [20-future-translation-platforms.md](../roadmap/20-future-translation-platforms.md) | +| Wave 20 (Track D3 binary-variant signing) | `sign_message`/`verify_message`, `BinaryVariantMeta` signature fields, canonical `vbs-binary-v1` message + trusted-key policy, `select_compatible_signed_binary` fail-closed selection | COMPLETE & VERIFIED (GCC 16.2, pin `0002e74`; SignatureEnforcement 7/7, full CTest 8/8) | `include/Void/Build/Provenance/{Signing,Verification}.hpp`, `src/Provenance/{Signing,Verification}.cpp`, `include/Void/Build/Package/{Solver,Fetch}.hpp`, `src/Package/Fetch.cpp`, `tests/SignatureEnforcementTests.cpp`, docs | +| Future (Plan 20 Slice 3 / Tracks C, D3 binary distribution, E3 editor plugins, E4, live `conan`) | Live `conan` CLI verification; macOS/Windows/cross-compile; binary-variant signing and key distribution; daemon-side dependency materialization and editor plugins; `Void::Net`/`Void::Sqlite` alignment | PLANNED (unimplemented) | [21-expansion-roadmap.md](../roadmap/21-expansion-roadmap.md), [20-future-translation-platforms.md](../roadmap/20-future-translation-platforms.md) | diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index 33d6327..1eb1bb4 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -387,6 +387,22 @@ dev cpp-gcc -c ctest --test-dir build/pinned --output-on-failure - Full CTest: **8 / 8 passed** in 209.27 s. - `clang-format --dry-run --Werror` passes on all changed/new C++ files. +## Track D3 Binary-Variant Signing (GCC 16.2, pin `0002e74`, built-in Ed25519) + +```sh +dev cpp-gcc -c cmake --build build/pinned --parallel 8 +dev cpp-gcc -c build/pinned/bin/vbs_v1_tests --suite SignatureEnforcement --no-color +dev cpp-gcc -c ctest --test-dir build/pinned --output-on-failure +``` + +- Build: exit 0. `SignatureEnforcement`: **7 / 7 passed** in 158 ms, including + `binary_variant_signature_policy_and_selection` (no trusted keys selects; a trusted key rejects + an unsigned variant; an unrelated key reports `untrusted_key`; a signed variant with its trusted + key is selected; a tampered digest reports `invalid_signature`) and + `canonical_binary_message_is_exact` (`vbs-binary-v1\n\nsha256:\n`). +- Full CTest: **8 / 8 passed** in 195.08 s. +- `clang-format --dry-run --Werror` passes on all changed C++ files. + ## Limits and Unrun Checks 1. **HTTPS git fetch**: the host shell is proxy-restricted, but the Docker container could clone `https://git.faumaray.com/Void/VoidLibrary.git`; the pinned commit `3e254055e081a104f08df68dde39711bc7813518` was fetched directly for the verification above. A separate configure against the local `/home/faumaray/Projects/Library` checkout via `VBS_VOID_SOURCE_DIR` also succeeded (development-only override, checkout unmodified). diff --git a/docs/roadmap/21-expansion-roadmap.md b/docs/roadmap/21-expansion-roadmap.md index 5e61443..42db85f 100644 --- a/docs/roadmap/21-expansion-roadmap.md +++ b/docs/roadmap/21-expansion-roadmap.md @@ -61,7 +61,7 @@ flowchart LR | --- | --- | --- | | **D1 — Shared HTTP Content-Addressed Action Cache (CAS)** *(verified: cross-build-directory 100% cache hit)* | Push/pull action records and output blobs (`PUT/GET /api/v1/cas/blobs/{sha256}`, `/api/v1/cas/actions/{action_key}`) against `vbs-registry` or any compatible HTTP CAS server with bearer auth and digest verification on every read. Action keys are normalized to be build-directory independent; `vbs build/test --remote-cache URL [--remote-cache-token-env VAR]` (or `VBS_REMOTE_CACHE` / `VBS_REMOTE_CACHE_TOKEN`). | Cold CI build populates remote CAS; second clean machine achieves 100% cache hit with verified SHA-256 outputs. | | **D2 — Fast Hashing & Parallel Snapshot Pipeline** *(verified: 5,001-file warm no-op 374 ms)* | Adopt `Void::Crypto` BLAKE2b / hardware-accelerated SHA-256 (when available) + parallel snapshot hashing across `Void::Concurrency::ThreadPool` + optional `Void::Sqlite` indexed state store for >10k-file monorepos. Snapshot capture revalidation now hashes on a bounded thread pool for trees of 32+ files. | Sub-second warm no-op on 5,000-file synthetic benchmark fixture (`vbs_benchmarks`). | -| **D3 — Supply-Chain Provenance & Signed Packages** *(SBOM, Ed25519 signing primitives, and trusted-key enforcement implemented; OpenSSL or the built-in Ed25519 backend)* | Emit SPDX/CycloneDX SBOM JSON from `vbs.lock` + `BuildGraph` (`vbs build/test --sbom FILE --sbom-format spdx\|cyclonedx`, `src/Provenance/Sbom.cpp`); Ed25519 detached-signature primitives in `Void::Build::Provenance::Signing` (`include/Void/Build/Provenance/Signing.hpp`, `src/Provenance/Signing.cpp`): 0600 seed generation, public-key derivation, canonical `vbs-package-v1` package message, and sign/verify; package signature/public-key metadata persisted in `vbs.lock` and registry releases; `vbs publish --signing-key FILE`; `vbs build\|test --trusted-public-key HEX` enforced before extraction via `Provenance::verify_trusted_package_signature` (`include/Void/Build/Provenance/Verification.hpp`); trusted keys can also come from `--trusted-key-file`, a project `vbs.keys` ring, or `VBS_TRUSTED_PUBLIC_KEYS` (`Provenance::load_trusted_keys`). | SBOM is deterministic and valid JSON; signing primitives pass round-trip/tamper/malformed tests (`tests/SigningTests.cpp`); trusted-key policy tests pass with and without OpenSSL (`tests/SignatureEnforcementTests.cpp`); a signed publish -> deps update -> `--locked` trusted build succeeds and an untrusted/unsigned package fails closed before extraction (`RegistryTests.cpp`). | +| **D3 — Supply-Chain Provenance & Signed Packages** *(SBOM, Ed25519 signing primitives, and trusted-key enforcement implemented; OpenSSL or the built-in Ed25519 backend)* | Emit SPDX/CycloneDX SBOM JSON from `vbs.lock` + `BuildGraph` (`vbs build/test --sbom FILE --sbom-format spdx\|cyclonedx`, `src/Provenance/Sbom.cpp`); Ed25519 detached-signature primitives in `Void::Build::Provenance::Signing` (`include/Void/Build/Provenance/Signing.hpp`, `src/Provenance/Signing.cpp`): 0600 seed generation, public-key derivation, canonical `vbs-package-v1` package message, and sign/verify; package signature/public-key metadata persisted in `vbs.lock` and registry releases; `vbs publish --signing-key FILE`; `vbs build\|test --trusted-public-key HEX` enforced before extraction via `Provenance::verify_trusted_package_signature` (`include/Void/Build/Provenance/Verification.hpp`); trusted keys can also come from `--trusted-key-file`, a project `vbs.keys` ring, or `VBS_TRUSTED_PUBLIC_KEYS` (`Provenance::load_trusted_keys`); prebuilt binary variants carry optional signatures verified by `select_compatible_signed_binary` over the canonical `vbs-binary-v1` message. | SBOM is deterministic and valid JSON; signing primitives pass round-trip/tamper/malformed tests (`tests/SigningTests.cpp`); trusted-key policy tests pass with and without OpenSSL (`tests/SignatureEnforcementTests.cpp`); a signed publish -> deps update -> `--locked` trusted build succeeds and an untrusted/unsigned package fails closed before extraction (`RegistryTests.cpp`). | --- diff --git a/include/Void/Build/Package/Fetch.hpp b/include/Void/Build/Package/Fetch.hpp index 475fbc1..98dc500 100644 --- a/include/Void/Build/Package/Fetch.hpp +++ b/include/Void/Build/Package/Fetch.hpp @@ -59,6 +59,14 @@ make_binary_compatibility_key(const ProfileContext &profile, select_compatible_binary(const BinaryCompatibilityKey &key, const std::vector &available); +// Selects the first ABI-compatible binary variant and, when trusted keys are supplied, requires +// a valid Ed25519 signature over the canonical binary message (fail closed on unsigned, untrusted, +// or invalid variants). With no trusted keys this matches select_compatible_binary. +[[nodiscard]] Void::Result::Result +select_compatible_signed_binary(const BinaryCompatibilityKey &key, + const std::vector &available, + const std::vector &trusted_public_keys_hex); + // Acquires and verifies a source dependency (local path, exact pinned Git SHA, or // SHA-256 verified archive) into managed storage without ever modifying external checkouts. [[nodiscard]] Void::Result::Result diff --git a/include/Void/Build/Package/Solver.hpp b/include/Void/Build/Package/Solver.hpp index 754ea39..43b69c5 100644 --- a/include/Void/Build/Package/Solver.hpp +++ b/include/Void/Build/Package/Solver.hpp @@ -44,6 +44,8 @@ struct BinaryVariantMeta { std::string dependency_closure_hash; std::string sha256; std::string download_url; + std::optional signature; // Ed25519 detached signature hex (optional) + std::optional public_key; // Signer public key hex (optional) }; struct ReleaseRecord { diff --git a/include/Void/Build/Provenance/Signing.hpp b/include/Void/Build/Provenance/Signing.hpp index 592f2f1..6eb10eb 100644 --- a/include/Void/Build/Provenance/Signing.hpp +++ b/include/Void/Build/Provenance/Signing.hpp @@ -3,7 +3,9 @@ #include #include +#include #include +#include #include #include @@ -24,6 +26,15 @@ generate_signing_seed(const std::filesystem::path &seed_path); [[nodiscard]] Void::Result::Result public_key_from_seed(const std::filesystem::path &seed_path); +// Signs arbitrary exact message bytes and returns 128 lowercase hex characters. +[[nodiscard]] Void::Result::Result +sign_message(std::span message, const std::filesystem::path &seed_path); + +// Verifies a detached signature over arbitrary exact message bytes. +[[nodiscard]] Void::Result::Result +verify_message(std::span message, std::string_view signature_hex, + std::string_view public_key_hex); + // Canonical package message signed by vbs: // "vbs-package-v1\n@\nsha256:\n" [[nodiscard]] std::string canonical_package_message(std::string_view name, std::string_view version, diff --git a/include/Void/Build/Provenance/Verification.hpp b/include/Void/Build/Provenance/Verification.hpp index e9c0ddd..969f4e2 100644 --- a/include/Void/Build/Provenance/Verification.hpp +++ b/include/Void/Build/Provenance/Verification.hpp @@ -20,6 +20,23 @@ enum class TrustDecision { Verified, Unsigned, UntrustedKey, InvalidSignature }; [[nodiscard]] std::string_view trust_decision_name(TrustDecision decision) noexcept; +// Detached signature metadata attached to a prebuilt binary variant. +struct BinarySignature { + std::string variant_id; + std::string sha256; // Binary artifact digest hex. + std::string signature; // Ed25519 detached signature hex (may be empty). + std::string public_key; // Ed25519 signer public key hex (may be empty). +}; + +// Canonical binary-variant message: "vbs-binary-v1\n\nsha256:\n". +[[nodiscard]] std::string canonical_binary_message(std::string_view variant_id, + std::string_view sha256_hex); + +// Trust policy for binary-variant signatures (same decision semantics as package signatures). +[[nodiscard]] Void::Result::Result +verify_trusted_binary_signature(const BinarySignature &signature, + const std::vector &trusted_public_keys_hex); + // Trust policy for package signatures: // - No trusted keys => Unsigned (the caller keeps legacy unsigned behavior). // - Missing signature or public key => Unsigned (the caller decides whether that is an error). diff --git a/src/Package/Fetch.cpp b/src/Package/Fetch.cpp index 8cad2e6..69d7330 100644 --- a/src/Package/Fetch.cpp +++ b/src/Package/Fetch.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -141,6 +142,36 @@ select_compatible_binary(const BinaryCompatibilityKey &key, return std::nullopt; } +Void::Result::Result +select_compatible_signed_binary(const BinaryCompatibilityKey &key, + const std::vector &available, + const std::vector &trusted_public_keys_hex) { + const auto selected = select_compatible_binary(key, available); + if (!selected) { + return std::unexpected{make_error(Void::System::SystemErrorCode::not_found, + "no ABI-compatible binary variant is available")}; + } + if (trusted_public_keys_hex.empty()) { + return *selected; + } + + auto decision = Provenance::verify_trusted_binary_signature( + Provenance::BinarySignature{selected->variant_id, selected->sha256, + selected->signature.value_or(std::string{}), + selected->public_key.value_or(std::string{})}, + trusted_public_keys_hex); + if (!decision) { + return std::unexpected{decision.error()}; + } + if (*decision != Provenance::TrustDecision::Verified) { + return std::unexpected{make_error( + Void::System::SystemErrorCode::permission_denied, + std::format("binary variant '{}' is not signed by a trusted key ({})", + selected->variant_id, Provenance::trust_decision_name(*decision)))}; + } + return *selected; +} + Void::Result::Result materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &options) { std::error_code ec; diff --git a/src/Provenance/Signing.cpp b/src/Provenance/Signing.cpp index 93972ea..6c0c6b2 100644 --- a/src/Provenance/Signing.cpp +++ b/src/Provenance/Signing.cpp @@ -326,8 +326,7 @@ std::string canonical_package_message(std::string_view name, std::string_view ve return message; } -Result sign_package(std::string_view name, std::string_view version, - std::string_view sha256_hex, +Result sign_message(std::span message, const std::filesystem::path &seed_path) { if (auto backend = require_signing_backend(); !backend) { return std::unexpected{backend.error()}; @@ -341,18 +340,23 @@ Result sign_package(std::string_view name, std::string_view if (!private_key) { return std::unexpected{private_key.error()}; } - - const std::string message = canonical_package_message(name, version, sha256_hex); - auto signature = Void::Crypto::sign_ed25519( - std::as_bytes(std::span{message.data(), message.size()}), *private_key); + auto signature = Void::Crypto::sign_ed25519(message, *private_key); if (!signature) { return std::unexpected{signature.error()}; } return Void::Encoding::Hex::encode(signature->view()); } -Result verify_package(std::string_view name, std::string_view version, - std::string_view sha256_hex, std::string_view signature_hex, +Result sign_package(std::string_view name, std::string_view version, + std::string_view sha256_hex, + const std::filesystem::path &seed_path) { + const std::string message = canonical_package_message(name, version, sha256_hex); + return sign_message(std::as_bytes(std::span{message.data(), message.size()}), + seed_path); +} + +Result verify_message(std::span message, + std::string_view signature_hex, std::string_view public_key_hex) { if (auto backend = require_signing_backend(); !backend) { return std::unexpected{backend.error()}; @@ -379,10 +383,15 @@ Result verify_package(std::string_view name, std::string_view versi return std::unexpected{public_key.error()}; } + return Void::Crypto::verify_ed25519(message, *signature, *public_key); +} + +Result verify_package(std::string_view name, std::string_view version, + std::string_view sha256_hex, std::string_view signature_hex, + std::string_view public_key_hex) { const std::string message = canonical_package_message(name, version, sha256_hex); - return Void::Crypto::verify_ed25519( - std::as_bytes(std::span{message.data(), message.size()}), *signature, - *public_key); + return verify_message(std::as_bytes(std::span{message.data(), message.size()}), + signature_hex, public_key_hex); } } // namespace Void::Build::Provenance diff --git a/src/Provenance/Verification.cpp b/src/Provenance/Verification.cpp index d53517b..f67dcbe 100644 --- a/src/Provenance/Verification.cpp +++ b/src/Provenance/Verification.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -35,6 +36,45 @@ std::string_view trust_decision_name(TrustDecision decision) noexcept { return "unknown"; } +std::string canonical_binary_message(std::string_view variant_id, std::string_view sha256_hex) { + std::string message; + message.reserve(16 + variant_id.size() + 8 + sha256_hex.size() + 1); + message += "vbs-binary-v1\n"; + message += variant_id; + message += "\nsha256:"; + message += sha256_hex; + message.push_back('\n'); + return message; +} + +Void::Result::Result +verify_trusted_binary_signature(const BinarySignature &signature, + const std::vector &trusted_public_keys_hex) { + if (trusted_public_keys_hex.empty()) { + return TrustDecision::Unsigned; + } + if (signature.signature.empty() || signature.public_key.empty()) { + return TrustDecision::Unsigned; + } + + const std::string normalized_key = normalize_hex(signature.public_key); + const bool trusted = std::ranges::any_of(trusted_public_keys_hex, [&](const std::string &key) { + return normalize_hex(key) == normalized_key; + }); + if (!trusted) { + return TrustDecision::UntrustedKey; + } + + const std::string message = canonical_binary_message(signature.variant_id, signature.sha256); + auto verified = + verify_message(std::as_bytes(std::span{message.data(), message.size()}), + signature.signature, signature.public_key); + if (!verified) { + return std::unexpected{verified.error()}; + } + return *verified ? TrustDecision::Verified : TrustDecision::InvalidSignature; +} + Void::Result::Result verify_trusted_package_signature(std::string_view name, std::string_view version, const PackageSignature &signature, diff --git a/tests/SignatureEnforcementTests.cpp b/tests/SignatureEnforcementTests.cpp index 9c3bd17..a024c8d 100644 --- a/tests/SignatureEnforcementTests.cpp +++ b/tests/SignatureEnforcementTests.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,8 +7,10 @@ #include #include +#include #include #include +#include #include #include @@ -176,6 +179,100 @@ VOID_TEST(SignatureEnforcement, normalize_public_key_validates_and_lowercases) { Void::Testing::expect(!Provenance::normalize_public_key(std::string(64, 'z')).has_value()); } +namespace { + +Void::Build::Package::BinaryCompatibilityKey sample_binary_key() { + Void::Build::Package::BinaryCompatibilityKey key; + key.os = "Linux"; + key.architecture = "x86_64"; + key.compiler_family = "gcc"; + key.compiler_abi = "gcc-16"; + key.stdlib_abi = "libstdc++"; + key.linkage = "static"; + key.lto_mode = "off"; + key.dependency_closure_hash = "closure"; + return key; +} + +Void::Build::Package::BinaryVariantMeta sample_binary_variant() { + Void::Build::Package::BinaryVariantMeta variant; + variant.variant_id = "variant-1"; + variant.os = "Linux"; + variant.architecture = "x86_64"; + variant.compiler_family = "gcc"; + variant.compiler_abi = "gcc-16"; + variant.stdlib_abi = "libstdc++"; + variant.linkage = "static"; + variant.lto_mode = "off"; + variant.dependency_closure_hash = "closure"; + variant.sha256 = std::string{kDigest}; + variant.download_url = "http://127.0.0.1:1/binary"; + return variant; +} + +} // namespace + +VOID_TEST(SignatureEnforcement, binary_variant_signature_policy_and_selection) { + namespace Package = Void::Build::Package; + namespace Provenance = Void::Build::Provenance; + const auto key = sample_binary_key(); + auto variant = sample_binary_variant(); + + Void::Testing::expect(Package::select_compatible_signed_binary(key, {variant}, {}).has_value()); + + auto unsigned_result = Package::select_compatible_signed_binary(key, {variant}, {kKeyA}); + Void::Testing::expect(!unsigned_result.has_value()); + if (!unsigned_result) { + Void::Testing::expect(unsigned_result.error().message().find("unsigned") != + std::string::npos); + } + + variant.signature = kSignature; + variant.public_key = kKeyB; + auto untrusted_result = Package::select_compatible_signed_binary(key, {variant}, {kKeyA}); + Void::Testing::expect(!untrusted_result.has_value()); + if (!untrusted_result) { + Void::Testing::expect(untrusted_result.error().message().find("untrusted_key") != + std::string::npos); + } + + if (!signing_available()) { + return; + } + + auto temp = make_temp_dir(); + const auto seed_path = temp.path() / "binary.seed"; + Void::Testing::require(Provenance::generate_signing_seed(seed_path).has_value()); + auto public_key = Provenance::public_key_from_seed(seed_path); + Void::Testing::require(public_key.has_value()); + const std::string message = + Provenance::canonical_binary_message(variant.variant_id, variant.sha256); + auto signature = Provenance::sign_message( + std::as_bytes(std::span{message.data(), message.size()}), seed_path); + Void::Testing::require(signature.has_value()); + variant.signature = *signature; + variant.public_key = *public_key; + + auto verified = Package::select_compatible_signed_binary(key, {variant}, {*public_key}); + Void::Testing::require(verified.has_value()); + Void::Testing::expect(verified->variant_id == variant.variant_id); + + auto tampered = variant; + tampered.sha256.back() = tampered.sha256.back() == '0' ? '1' : '0'; + auto tampered_result = Package::select_compatible_signed_binary(key, {tampered}, {*public_key}); + Void::Testing::expect(!tampered_result.has_value()); + if (!tampered_result) { + Void::Testing::expect(tampered_result.error().message().find("invalid_signature") != + std::string::npos); + } +} + +VOID_TEST(SignatureEnforcement, canonical_binary_message_is_exact) { + namespace Provenance = Void::Build::Provenance; + const std::string expected = "vbs-binary-v1\nvariant-1\nsha256:" + std::string{kDigest} + "\n"; + Void::Testing::expect_eq(Provenance::canonical_binary_message("variant-1", kDigest), expected); +} + VOID_TEST(SignatureEnforcement, trusted_key_accepts_valid_and_rejects_tampered_signature) { using Void::Build::Provenance::PackageSignature; if (!signing_available()) {