feat(provenance): distribute trusted Ed25519 keys via files, vbs.keys, and environment
This commit is contained in:
1 parent
f5c0c9f09d
commit
ff59f6ff5c
13 files changed
+314
-7
No files matched your search
@@ -0,0 +1,145 @@
|
||||
#include <Void/Build/Provenance/TrustedKeys.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <format>
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Build::Provenance {
|
||||
namespace {
|
||||
|
||||
using Void::Result::Error;
|
||||
using Void::Result::ErrorKind;
|
||||
using Void::Result::Result;
|
||||
|
||||
constexpr std::size_t PublicKeyHexLength = 64;
|
||||
|
||||
Error make_error(ErrorKind kind, std::string message) { return Error{kind, std::move(message)}; }
|
||||
|
||||
std::string_view trim_ascii(std::string_view text) {
|
||||
while (!text.empty() && std::isspace(static_cast<unsigned char>(text.front())) != 0) {
|
||||
text.remove_prefix(1);
|
||||
}
|
||||
while (!text.empty() && std::isspace(static_cast<unsigned char>(text.back())) != 0) {
|
||||
text.remove_suffix(1);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
bool is_hex_digit(char c) noexcept {
|
||||
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
|
||||
}
|
||||
|
||||
Result<void, Error> add_key(std::string_view raw, std::string_view origin,
|
||||
std::vector<std::string> &keys, std::set<std::string> &seen) {
|
||||
const std::string_view trimmed = trim_ascii(raw);
|
||||
if (trimmed.empty()) {
|
||||
return {};
|
||||
}
|
||||
if (trimmed.size() != PublicKeyHexLength || !std::ranges::all_of(trimmed, is_hex_digit)) {
|
||||
return std::unexpected{make_error(
|
||||
ErrorKind::invalid_argument,
|
||||
std::format("{}: trusted public key must be exactly {} hex characters, got '{}'",
|
||||
origin, PublicKeyHexLength, trimmed))};
|
||||
}
|
||||
std::string normalized{trimmed};
|
||||
std::ranges::transform(normalized, normalized.begin(), [](char c) {
|
||||
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
});
|
||||
if (seen.insert(normalized).second) {
|
||||
keys.push_back(std::move(normalized));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void, Error> add_key_file(const std::filesystem::path &path, std::vector<std::string> &keys,
|
||||
std::set<std::string> &seen) {
|
||||
std::ifstream stream{path};
|
||||
if (!stream) {
|
||||
return std::unexpected{make_error(
|
||||
ErrorKind::io, std::format("cannot open trusted key file '{}'", path.string()))};
|
||||
}
|
||||
std::string line;
|
||||
std::size_t line_number = 0;
|
||||
while (std::getline(stream, line)) {
|
||||
++line_number;
|
||||
const auto comment = line.find('#');
|
||||
if (comment != std::string::npos) {
|
||||
line.resize(comment);
|
||||
}
|
||||
if (auto added =
|
||||
add_key(line, std::format("{}:{}", path.string(), line_number), keys, seen);
|
||||
!added) {
|
||||
return added;
|
||||
}
|
||||
}
|
||||
if (!stream.eof()) {
|
||||
return std::unexpected{make_error(
|
||||
ErrorKind::io, std::format("cannot read trusted key file '{}'", path.string()))};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Result<std::string, Error> normalize_public_key(std::string_view hex) {
|
||||
const std::string_view trimmed = trim_ascii(hex);
|
||||
if (trimmed.size() != PublicKeyHexLength || !std::ranges::all_of(trimmed, is_hex_digit)) {
|
||||
return std::unexpected{
|
||||
make_error(ErrorKind::invalid_argument,
|
||||
std::format("trusted public key must be exactly {} hex characters",
|
||||
PublicKeyHexLength))};
|
||||
}
|
||||
std::string normalized{trimmed};
|
||||
std::ranges::transform(normalized, normalized.begin(), [](char c) {
|
||||
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
});
|
||||
return normalized;
|
||||
}
|
||||
|
||||
Result<std::vector<std::string>, Error>
|
||||
load_trusted_keys(std::span<const std::filesystem::path> explicit_files,
|
||||
const std::filesystem::path &project_root,
|
||||
std::optional<std::string_view> environment_value) {
|
||||
std::vector<std::string> keys;
|
||||
std::set<std::string> seen;
|
||||
|
||||
for (const auto &file : explicit_files) {
|
||||
if (auto added = add_key_file(file, keys, seen); !added) {
|
||||
return std::unexpected{added.error()};
|
||||
}
|
||||
}
|
||||
|
||||
const auto project_key_file = project_root / "vbs.keys";
|
||||
std::error_code ec;
|
||||
if (std::filesystem::is_regular_file(project_key_file, ec)) {
|
||||
if (auto added = add_key_file(project_key_file, keys, seen); !added) {
|
||||
return std::unexpected{added.error()};
|
||||
}
|
||||
}
|
||||
|
||||
if (environment_value) {
|
||||
std::string_view text = *environment_value;
|
||||
while (!text.empty()) {
|
||||
const auto separator = text.find_first_of(", \t\r\n");
|
||||
const std::string_view token =
|
||||
separator == std::string_view::npos ? text : text.substr(0, separator);
|
||||
if (auto added = add_key(token, "VBS_TRUSTED_PUBLIC_KEYS", keys, seen); !added) {
|
||||
return std::unexpected{added.error()};
|
||||
}
|
||||
if (separator == std::string_view::npos) {
|
||||
break;
|
||||
}
|
||||
text.remove_prefix(separator + 1);
|
||||
}
|
||||
}
|
||||
|
||||
return keys;
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Provenance
|
||||
Reference in new issue
Block a user