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
@@ -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