#include #include #include #include #include #include #include #include #include #include #include #include namespace { constexpr std::string_view kDigest = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; std::string read_text(const std::filesystem::path &path) { std::ifstream stream{path, std::ios::binary}; return std::string{std::istreambuf_iterator{stream}, std::istreambuf_iterator{}}; } void write_text(const std::filesystem::path &path, std::string_view text) { std::ofstream stream{path, std::ios::binary | std::ios::trunc}; stream.write(text.data(), static_cast(text.size())); } bool is_lower_hex(std::string_view text) { return std::ranges::all_of( text, [](char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'); }); } std::string uppercase(std::string value) { std::ranges::transform(value, value.begin(), [](char c) { return static_cast(std::toupper(static_cast(c))); }); return value; } std::string tampered(std::string value) { value.front() = value.front() == '0' ? '1' : '0'; return value; } Void::FileSystem::TempDir make_temp_dir() { auto temp = Void::FileSystem::TempDir::open("vbs_signing_"); Void::Testing::require(temp.has_value()); return std::move(*temp); } } // namespace VOID_TEST(Signing, canonical_package_message_is_exact_and_stable) { using Void::Build::Provenance::canonical_package_message; const std::string expected = "vbs-package-v1\nvoid-core@1.2.3\nsha256:" + std::string{kDigest} + "\n"; Void::Testing::expect_eq(canonical_package_message("void-core", "1.2.3", kDigest), expected); Void::Testing::expect_eq(canonical_package_message("void-core", "1.2.3", kDigest), expected); } VOID_TEST(Signing, seed_roundtrip_sign_verify_and_rejections) { namespace Provenance = Void::Build::Provenance; if (!Provenance::signing_available()) { return; } auto temp = make_temp_dir(); const auto seed_path = temp.path() / "signing.seed"; auto generated = Provenance::generate_signing_seed(seed_path); Void::Testing::require(generated.has_value()); const std::string contents = read_text(seed_path); Void::Testing::expect_eq(contents.size(), std::size_t{65}); if (contents.size() == 65) { Void::Testing::expect(contents.back() == '\n'); Void::Testing::expect(is_lower_hex(std::string_view{contents}.substr(0, 64))); } using Perms = std::filesystem::perms; const auto permissions = std::filesystem::status(seed_path).permissions(); Void::Testing::expect((permissions & Perms::owner_read) != Perms::none); Void::Testing::expect((permissions & Perms::owner_write) != Perms::none); Void::Testing::expect((permissions & (Perms::group_all | Perms::others_all)) == Perms::none); auto public_key = Provenance::public_key_from_seed(seed_path); Void::Testing::require(public_key.has_value()); Void::Testing::expect_eq(public_key->size(), std::size_t{64}); auto signature = Provenance::sign_package("void-core", "1.2.3", kDigest, seed_path); Void::Testing::require(signature.has_value()); Void::Testing::expect_eq(signature->size(), std::size_t{128}); auto verified = Provenance::verify_package("void-core", "1.2.3", kDigest, *signature, *public_key); Void::Testing::require(verified.has_value()); Void::Testing::expect(*verified); // Hex input accepts uppercase as well as lowercase. auto upper_verified = Provenance::verify_package("void-core", "1.2.3", kDigest, uppercase(*signature), uppercase(*public_key)); Void::Testing::require(upper_verified.has_value()); Void::Testing::expect(*upper_verified); std::string other_digest{kDigest}; other_digest.back() = other_digest.back() == '0' ? '1' : '0'; auto digest_result = Provenance::verify_package("void-core", "1.2.3", other_digest, *signature, *public_key); Void::Testing::require(digest_result.has_value()); Void::Testing::expect(!*digest_result); auto signature_result = Provenance::verify_package("void-core", "1.2.3", kDigest, tampered(*signature), *public_key); Void::Testing::require(signature_result.has_value()); Void::Testing::expect(!*signature_result); const auto other_seed_path = temp.path() / "other.seed"; Void::Testing::require(Provenance::generate_signing_seed(other_seed_path).has_value()); auto other_public_key = Provenance::public_key_from_seed(other_seed_path); Void::Testing::require(other_public_key.has_value()); Void::Testing::expect(*other_public_key != *public_key); auto wrong_key_result = Provenance::verify_package("void-core", "1.2.3", kDigest, *signature, *other_public_key); Void::Testing::require(wrong_key_result.has_value()); Void::Testing::expect(!*wrong_key_result); } VOID_TEST(Signing, malformed_hex_and_seed_files_are_rejected) { namespace Provenance = Void::Build::Provenance; if (!Provenance::signing_available()) { return; } auto temp = make_temp_dir(); const auto seed_path = temp.path() / "signing.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()); auto signature = Provenance::sign_package("void-core", "1.2.3", kDigest, seed_path); Void::Testing::require(signature.has_value()); for (const std::string_view bad_signature : {std::string_view{}, std::string_view{"00"}, std::string_view{std::string(128, 'z')}}) { auto result = Provenance::verify_package("void-core", "1.2.3", kDigest, bad_signature, *public_key); Void::Testing::expect(!result.has_value()); } auto bad_key = Provenance::verify_package("void-core", "1.2.3", kDigest, *signature, "not-a-key"); Void::Testing::expect(!bad_key.has_value()); const auto bad_seed_path = temp.path() / "bad.seed"; write_text(bad_seed_path, std::string(63, 'a')); Void::Testing::expect(!Provenance::public_key_from_seed(bad_seed_path).has_value()); write_text(bad_seed_path, std::string(64, 'g')); Void::Testing::expect(!Provenance::public_key_from_seed(bad_seed_path).has_value()); write_text(bad_seed_path, std::string(64, 'a') + "trailing"); Void::Testing::expect(!Provenance::public_key_from_seed(bad_seed_path).has_value()); // Optional trailing whitespace is accepted. write_text(bad_seed_path, read_text(seed_path) + " \t\r\n"); auto whitespace_key = Provenance::public_key_from_seed(bad_seed_path); Void::Testing::require(whitespace_key.has_value()); Void::Testing::expect_eq(*whitespace_key, *public_key); const auto directory_path = temp.path() / "seed-directory"; std::filesystem::create_directory(directory_path); Void::Testing::expect(!Provenance::public_key_from_seed(directory_path).has_value()); Void::Testing::expect( !Provenance::public_key_from_seed(temp.path() / "missing.seed").has_value()); } VOID_TEST(Signing, backend_gating_reports_ed25519_requirement) { namespace Provenance = Void::Build::Provenance; if (Provenance::signing_available()) { return; } auto temp = make_temp_dir(); const auto seed_path = temp.path() / "signing.seed"; auto generated = Provenance::generate_signing_seed(seed_path); Void::Testing::expect(!generated.has_value()); if (!generated) { Void::Testing::expect(generated.error().message().find("Ed25519") != std::string::npos); } auto signed_result = Provenance::sign_package("void-core", "1.2.3", kDigest, seed_path); Void::Testing::expect(!signed_result.has_value()); if (!signed_result) { Void::Testing::expect(signed_result.error().message().find("Ed25519") != std::string::npos); } auto verified_result = Provenance::verify_package("void-core", "1.2.3", kDigest, std::string(128, '0'), std::string(64, '0')); Void::Testing::expect(!verified_result.has_value()); if (!verified_result) { Void::Testing::expect(verified_result.error().message().find("Ed25519") != std::string::npos); } }