feat(provenance): sign and verify prebuilt binary variants with trusted keys
This commit is contained in:
1 parent
ff59f6ff5c
commit
e97f3905e5
12 files changed
+250
-14
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <Void/Build/Platform/FileOps.hpp>
|
||||
#include <Void/Build/Platform/ProcessSupervisor.hpp>
|
||||
#include <Void/Build/Provenance/Verification.hpp>
|
||||
#include <Void/Build/Snapshot/Snapshot.hpp>
|
||||
#include <Void/Build/Util/Sha256.hpp>
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
@@ -141,6 +142,36 @@ select_compatible_binary(const BinaryCompatibilityKey &key,
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
Void::Result::Result<BinaryVariantMeta, Void::Result::Error>
|
||||
select_compatible_signed_binary(const BinaryCompatibilityKey &key,
|
||||
const std::vector<BinaryVariantMeta> &available,
|
||||
const std::vector<std::string> &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<MaterializedSource, Void::Result::Error>
|
||||
materialize_dependency_source(const DependencyRecord &dep, const FetchOptions &options) {
|
||||
std::error_code ec;
|
||||
|
||||
+20
-11
@@ -326,8 +326,7 @@ std::string canonical_package_message(std::string_view name, std::string_view ve
|
||||
return message;
|
||||
}
|
||||
|
||||
Result<std::string, Error> sign_package(std::string_view name, std::string_view version,
|
||||
std::string_view sha256_hex,
|
||||
Result<std::string, Error> sign_message(std::span<const std::byte> message,
|
||||
const std::filesystem::path &seed_path) {
|
||||
if (auto backend = require_signing_backend(); !backend) {
|
||||
return std::unexpected{backend.error()};
|
||||
@@ -341,18 +340,23 @@ Result<std::string, Error> 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<const char>{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<bool, Error> verify_package(std::string_view name, std::string_view version,
|
||||
std::string_view sha256_hex, std::string_view signature_hex,
|
||||
Result<std::string, Error> 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<const char>{message.data(), message.size()}),
|
||||
seed_path);
|
||||
}
|
||||
|
||||
Result<bool, Error> verify_message(std::span<const std::byte> 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<bool, Error> 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<bool, Error> 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<const char>{message.data(), message.size()}), *signature,
|
||||
*public_key);
|
||||
return verify_message(std::as_bytes(std::span<const char>{message.data(), message.size()}),
|
||||
signature_hex, public_key_hex);
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Provenance
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
@@ -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<TrustDecision, Void::Result::Error>
|
||||
verify_trusted_binary_signature(const BinarySignature &signature,
|
||||
const std::vector<std::string> &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<const char>{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<TrustDecision, Void::Result::Error>
|
||||
verify_trusted_package_signature(std::string_view name, std::string_view version,
|
||||
const PackageSignature &signature,
|
||||
|
||||
Reference in new issue
Block a user