106 lines
3.5 KiB
C++
106 lines
3.5 KiB
C++
#include <Void/Build/Provenance/Verification.hpp>
|
|
|
|
#include <Void/Build/Provenance/Signing.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <span>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
namespace Void::Build::Provenance {
|
|
namespace {
|
|
|
|
std::string normalize_hex(std::string_view hex) {
|
|
std::string normalized{hex};
|
|
std::ranges::transform(normalized, normalized.begin(), [](char c) {
|
|
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
|
});
|
|
return normalized;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
std::string_view trust_decision_name(TrustDecision decision) noexcept {
|
|
switch (decision) {
|
|
case TrustDecision::Verified:
|
|
return "verified";
|
|
case TrustDecision::Unsigned:
|
|
return "unsigned";
|
|
case TrustDecision::UntrustedKey:
|
|
return "untrusted_key";
|
|
case TrustDecision::InvalidSignature:
|
|
return "invalid_signature";
|
|
}
|
|
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,
|
|
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;
|
|
}
|
|
|
|
auto verified =
|
|
verify_package(name, version, signature.sha256, signature.signature, signature.public_key);
|
|
if (!verified) {
|
|
return std::unexpected{verified.error()};
|
|
}
|
|
return *verified ? TrustDecision::Verified : TrustDecision::InvalidSignature;
|
|
}
|
|
|
|
} // namespace Void::Build::Provenance
|