153 lines
6.3 KiB
C++
153 lines
6.3 KiB
C++
#include <Void/Build/Package/Lockfile.hpp>
|
|
#include <Void/Build/Provenance/Signing.hpp>
|
|
#include <Void/Build/Provenance/Verification.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace {
|
|
|
|
using Void::Build::Provenance::signing_available;
|
|
using Void::Build::Provenance::TrustDecision;
|
|
using Void::Build::Provenance::verify_trusted_package_signature;
|
|
|
|
constexpr std::string_view kDigest =
|
|
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
|
const std::string kKeyA(64, 'a');
|
|
const std::string kKeyB(64, 'b');
|
|
const std::string kSignature(128, 'c');
|
|
|
|
Void::FileSystem::TempDir make_temp_dir() {
|
|
auto temp = Void::FileSystem::TempDir::open("vbs_signature_enforcement_");
|
|
Void::Testing::require(temp.has_value());
|
|
return std::move(*temp);
|
|
}
|
|
|
|
Void::Build::Package::LockfileDocument sample_document(bool signed_entry) {
|
|
Void::Build::Package::LockContextRecord context;
|
|
context.context_key = "debug:gcc:Linux:x86_64:target";
|
|
context.profile_name = "debug";
|
|
context.compiler = "gcc";
|
|
context.os = "Linux";
|
|
context.architecture = "x86_64";
|
|
context.machine_role = "target";
|
|
context.declaration_fingerprint = "fingerprint";
|
|
|
|
Void::Build::Package::LockedSourceEntry entry;
|
|
entry.alias = "demo";
|
|
entry.source_kind = "package";
|
|
entry.registry_authority = "http://127.0.0.1:1";
|
|
entry.package_name = "demo";
|
|
entry.version = "1.0.0";
|
|
entry.sha256 = std::string{kDigest};
|
|
entry.builder = "native";
|
|
if (signed_entry) {
|
|
entry.signature = kSignature;
|
|
entry.public_key = kKeyA;
|
|
}
|
|
context.entries.push_back(std::move(entry));
|
|
|
|
Void::Build::Package::LockfileDocument document;
|
|
document.contexts[context.context_key] = std::move(context);
|
|
return document;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
VOID_TEST(SignatureEnforcement, lockfile_signature_fields_roundtrip_and_omit_when_absent) {
|
|
using namespace Void::Build::Package;
|
|
|
|
const auto signed_doc = sample_document(true);
|
|
const std::string signed_json = serialize_lockfile(signed_doc);
|
|
Void::Testing::expect(signed_json.find("\"signature\"") != std::string::npos);
|
|
Void::Testing::expect(signed_json.find("\"public_key\"") != std::string::npos);
|
|
auto parsed_signed = parse_lockfile(signed_json);
|
|
Void::Testing::require(parsed_signed.has_value());
|
|
const auto &signed_entry =
|
|
parsed_signed->contexts.at("debug:gcc:Linux:x86_64:target").entries.at(0);
|
|
Void::Testing::require(signed_entry.signature.has_value());
|
|
Void::Testing::require(signed_entry.public_key.has_value());
|
|
Void::Testing::expect_eq(*signed_entry.signature, kSignature);
|
|
Void::Testing::expect_eq(*signed_entry.public_key, kKeyA);
|
|
Void::Testing::expect_eq(serialize_lockfile(*parsed_signed), signed_json);
|
|
|
|
const auto unsigned_doc = sample_document(false);
|
|
const std::string unsigned_json = serialize_lockfile(unsigned_doc);
|
|
Void::Testing::expect(unsigned_json.find("\"signature\"") == std::string::npos);
|
|
Void::Testing::expect(unsigned_json.find("\"public_key\"") == std::string::npos);
|
|
auto parsed_unsigned = parse_lockfile(unsigned_json);
|
|
Void::Testing::require(parsed_unsigned.has_value());
|
|
const auto &unsigned_entry =
|
|
parsed_unsigned->contexts.at("debug:gcc:Linux:x86_64:target").entries.at(0);
|
|
Void::Testing::expect(!unsigned_entry.signature.has_value());
|
|
Void::Testing::expect(!unsigned_entry.public_key.has_value());
|
|
}
|
|
|
|
VOID_TEST(SignatureEnforcement, trust_policy_decisions_without_crypto) {
|
|
using Void::Build::Provenance::PackageSignature;
|
|
|
|
const PackageSignature signed_pkg{std::string{kDigest}, kSignature, kKeyA};
|
|
|
|
auto no_keys = verify_trusted_package_signature("demo", "1.0.0", signed_pkg, {});
|
|
Void::Testing::require(no_keys.has_value());
|
|
Void::Testing::expect(*no_keys == TrustDecision::Unsigned);
|
|
|
|
auto missing_signature = verify_trusted_package_signature(
|
|
"demo", "1.0.0", PackageSignature{std::string{kDigest}, {}, kKeyA}, {kKeyA});
|
|
Void::Testing::require(missing_signature.has_value());
|
|
Void::Testing::expect(*missing_signature == TrustDecision::Unsigned);
|
|
|
|
auto untrusted = verify_trusted_package_signature("demo", "1.0.0", signed_pkg, {kKeyB});
|
|
Void::Testing::require(untrusted.has_value());
|
|
Void::Testing::expect(*untrusted == TrustDecision::UntrustedKey);
|
|
|
|
// Case-insensitive trusted-key matching.
|
|
std::string upper_key = kKeyA;
|
|
for (char &c : upper_key) {
|
|
if (c >= 'a' && c <= 'f') {
|
|
c = static_cast<char>(c - 'a' + 'A');
|
|
}
|
|
}
|
|
if (!signing_available()) {
|
|
auto unavailable =
|
|
verify_trusted_package_signature("demo", "1.0.0", signed_pkg, {upper_key});
|
|
Void::Testing::expect(!unavailable.has_value());
|
|
}
|
|
}
|
|
|
|
VOID_TEST(SignatureEnforcement, trusted_key_accepts_valid_and_rejects_tampered_signature) {
|
|
using Void::Build::Provenance::PackageSignature;
|
|
if (!signing_available()) {
|
|
return;
|
|
}
|
|
|
|
auto temp = make_temp_dir();
|
|
const auto seed_path = temp.path() / "signing.seed";
|
|
Void::Testing::require(Void::Build::Provenance::generate_signing_seed(seed_path).has_value());
|
|
auto public_key = Void::Build::Provenance::public_key_from_seed(seed_path);
|
|
Void::Testing::require(public_key.has_value());
|
|
auto signature = Void::Build::Provenance::sign_package("demo", "1.0.0", kDigest, seed_path);
|
|
Void::Testing::require(signature.has_value());
|
|
|
|
const PackageSignature valid{std::string{kDigest}, *signature, *public_key};
|
|
auto verified = verify_trusted_package_signature("demo", "1.0.0", valid, {*public_key});
|
|
Void::Testing::require(verified.has_value());
|
|
Void::Testing::expect(*verified == TrustDecision::Verified);
|
|
|
|
std::string tampered_digest{kDigest};
|
|
tampered_digest.back() = tampered_digest.back() == '0' ? '1' : '0';
|
|
auto invalid = verify_trusted_package_signature(
|
|
"demo", "1.0.0", PackageSignature{tampered_digest, *signature, *public_key}, {*public_key});
|
|
Void::Testing::require(invalid.has_value());
|
|
Void::Testing::expect(*invalid == TrustDecision::InvalidSignature);
|
|
|
|
auto malformed = verify_trusted_package_signature(
|
|
"demo", "1.0.0", PackageSignature{std::string{kDigest}, "zz", *public_key}, {*public_key});
|
|
Void::Testing::expect(!malformed.has_value());
|
|
}
|