feat(provenance,daemon): add Ed25519 signing primitives and bounded daemon describe op
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 3s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 9s

This commit is contained in:
faumaray committed 2026-12-03 17:30:12 +03:00
1 parent 0bde74f739
commit 21babb8c31
16 files changed
+1232 -20

No files matched your search

+374
View File
@@ -0,0 +1,374 @@
#include <Void/Build/Provenance/Signing.hpp>
#include <Void/Crypto/Crypto.hpp>
#include <Void/System/Types.hpp>
#include <Void/Text/Hex.hpp>
#include <algorithm>
#include <array>
#include <cctype>
#include <cstddef>
#include <format>
#include <fstream>
#include <iterator>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#ifndef _WIN32
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace Void::Build::Provenance {
namespace {
using Void::Result::Error;
using Void::Result::ErrorKind;
using Void::Result::Result;
constexpr std::size_t SeedHexLength = 64;
constexpr std::size_t PublicKeyHexLength = 64;
constexpr std::size_t SignatureHexLength = 128;
constexpr std::size_t MaxSeedFileBytes = 4096;
constexpr std::string_view OpenSslRequirement =
"Ed25519 package signing requires the OpenSSL backend of Void::Crypto";
Error make_error(ErrorKind kind, std::string message) { return Error{kind, std::move(message)}; }
Result<void, Error> require_openssl_backend() {
if (Void::Crypto::has_openssl_backend()) {
return {};
}
return std::unexpected{make_error(ErrorKind::unsupported, std::string{OpenSslRequirement})};
}
bool is_hex_digit(char c) noexcept {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}
// Best-effort secure wipe of a local secret string.
void wipe_string(std::string &secret) noexcept {
Void::Crypto::secure_wipe(
std::span<std::byte>{reinterpret_cast<std::byte *>(secret.data()), secret.size()});
secret.clear();
}
struct SecretWipeGuard {
std::string &secret;
~SecretWipeGuard() { wipe_string(secret); }
};
Result<std::string, Error> decode_hex_exact(std::string_view hex, std::size_t expected_chars,
std::string_view description) {
if (hex.size() != expected_chars) {
return std::unexpected{make_error(
ErrorKind::invalid_argument, std::format("{} must be exactly {} hex characters, got {}",
description, expected_chars, hex.size()))};
}
if (!std::ranges::all_of(hex, is_hex_digit)) {
return std::unexpected{
make_error(ErrorKind::invalid_argument,
std::format("{} contains a non-hexadecimal character", description))};
}
auto decoded = Void::Encoding::Hex::decode(hex);
if (!decoded) {
return std::unexpected{make_error(ErrorKind::invalid_argument,
std::format("{} could not be decoded", description))};
}
return *decoded;
}
// Reads a bounded regular file without following symlinks. The caller owns
// wiping the returned contents when they are secret.
Result<std::string, Error> read_bounded_file(const std::filesystem::path &path) {
#ifndef _WIN32
const int raw_fd = ::open(path.c_str(), O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
if (raw_fd < 0) {
const int error = errno;
return std::unexpected{Void::System::make_system_error(
Void::System::map_errno_to_system_code(error),
std::format("cannot open signing seed '{}'", path.string()), error)};
}
Void::System::OwnedFd fd{raw_fd};
struct stat status{};
if (::fstat(fd.get(), &status) != 0) {
const int error = errno;
return std::unexpected{Void::System::make_system_error(
Void::System::map_errno_to_system_code(error),
std::format("cannot stat signing seed '{}'", path.string()), error)};
}
if (!S_ISREG(status.st_mode)) {
return std::unexpected{
make_error(ErrorKind::invalid_argument,
std::format("signing seed '{}' is not a regular file", path.string()))};
}
std::string contents;
std::array<char, 512> buffer{};
while (true) {
const ssize_t count = ::read(fd.get(), buffer.data(), buffer.size());
if (count < 0) {
if (errno == EINTR) {
continue;
}
const int error = errno;
return std::unexpected{Void::System::make_system_error(
Void::System::map_errno_to_system_code(error),
std::format("cannot read signing seed '{}'", path.string()), error)};
}
if (count == 0) {
break;
}
contents.append(buffer.data(), static_cast<std::size_t>(count));
if (contents.size() > MaxSeedFileBytes) {
return std::unexpected{make_error(ErrorKind::invalid_argument,
std::format("signing seed '{}' exceeds {} bytes",
path.string(), MaxSeedFileBytes))};
}
}
return contents;
#else
std::error_code error;
if (!std::filesystem::is_regular_file(path, error)) {
return std::unexpected{
make_error(ErrorKind::invalid_argument,
std::format("signing seed '{}' is not a regular file", path.string()))};
}
std::ifstream stream{path, std::ios::binary};
if (!stream) {
return std::unexpected{
make_error(ErrorKind::io, std::format("cannot open signing seed '{}'", path.string()))};
}
std::string contents{std::istreambuf_iterator<char>{stream}, std::istreambuf_iterator<char>{}};
if (contents.size() > MaxSeedFileBytes) {
return std::unexpected{make_error(
ErrorKind::invalid_argument,
std::format("signing seed '{}' exceeds {} bytes", path.string(), MaxSeedFileBytes))};
}
return contents;
#endif
}
// Parses and decodes a seed file. The returned Ed25519Seed wipes itself.
Result<Void::Crypto::Ed25519Seed, Error> read_seed(const std::filesystem::path &seed_path) {
auto contents = read_bounded_file(seed_path);
if (!contents) {
return std::unexpected{contents.error()};
}
SecretWipeGuard wipe{*contents};
if (contents->size() < SeedHexLength) {
return std::unexpected{make_error(
ErrorKind::invalid_argument,
"signing seed must contain exactly 64 hex characters followed only by whitespace")};
}
const std::string_view hex{contents->data(), SeedHexLength};
if (!std::ranges::all_of(hex, is_hex_digit)) {
return std::unexpected{make_error(ErrorKind::invalid_argument,
"signing seed contains a non-hexadecimal character")};
}
for (const char trailing : std::string_view{*contents}.substr(SeedHexLength)) {
if (std::isspace(static_cast<unsigned char>(trailing)) == 0) {
return std::unexpected{
make_error(ErrorKind::invalid_argument,
"signing seed has trailing data after the 64 hex characters")};
}
}
auto decoded = Void::Encoding::Hex::decode(hex);
if (!decoded) {
return std::unexpected{
make_error(ErrorKind::invalid_argument, "signing seed could not be decoded")};
}
auto seed = Void::Crypto::Ed25519Seed::from_bytes(
std::as_bytes(std::span{decoded->data(), decoded->size()}));
wipe_string(*decoded);
if (!seed) {
return std::unexpected{seed.error()};
}
return std::move(*seed);
}
// Creates or truncates path with owner-only permissions (0600).
Result<void, Error> write_private_file(const std::filesystem::path &path,
std::string_view contents) {
#ifndef _WIN32
const int raw_fd =
::open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW, 0600);
if (raw_fd < 0) {
const int error = errno;
return std::unexpected{Void::System::make_system_error(
Void::System::map_errno_to_system_code(error),
std::format("cannot create signing seed '{}'", path.string()), error)};
}
Void::System::OwnedFd fd{raw_fd};
// Tighten an existing file even when the umask or its previous mode was wider.
if (::fchmod(fd.get(), 0600) != 0) {
const int error = errno;
return std::unexpected{Void::System::make_system_error(
Void::System::map_errno_to_system_code(error),
std::format("cannot restrict signing seed '{}'", path.string()), error)};
}
std::size_t offset = 0;
while (offset < contents.size()) {
const ssize_t count = ::write(fd.get(), contents.data() + offset, contents.size() - offset);
if (count < 0) {
if (errno == EINTR) {
continue;
}
const int error = errno;
return std::unexpected{Void::System::make_system_error(
Void::System::map_errno_to_system_code(error),
std::format("cannot write signing seed '{}'", path.string()), error)};
}
offset += static_cast<std::size_t>(count);
}
return fd.close();
#else
std::ofstream stream{path, std::ios::binary | std::ios::trunc};
if (!stream) {
return std::unexpected{make_error(
ErrorKind::io, std::format("cannot create signing seed '{}'", path.string()))};
}
stream.write(contents.data(), static_cast<std::streamsize>(contents.size()));
stream.close();
if (!stream) {
return std::unexpected{make_error(
ErrorKind::io, std::format("cannot write signing seed '{}'", path.string()))};
}
std::error_code error;
std::filesystem::permissions(
path, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write,
std::filesystem::perm_options::replace, error);
if (error) {
return std::unexpected{
make_error(ErrorKind::io, std::format("cannot restrict signing seed '{}': {}",
path.string(), error.message()))};
}
return {};
#endif
}
} // namespace
bool signing_available() noexcept { return Void::Crypto::has_openssl_backend(); }
Result<void, Error> generate_signing_seed(const std::filesystem::path &seed_path) {
if (auto backend = require_openssl_backend(); !backend) {
return backend;
}
auto private_key = Void::Crypto::Ed25519PrivateKey::generate();
if (!private_key) {
return std::unexpected{private_key.error()};
}
auto seed = private_key->export_raw_seed();
if (!seed) {
return std::unexpected{seed.error()};
}
std::string hex = Void::Encoding::Hex::encode(seed->view());
hex.push_back('\n');
SecretWipeGuard wipe{hex};
return write_private_file(seed_path, hex);
}
Result<std::string, Error> public_key_from_seed(const std::filesystem::path &seed_path) {
if (auto backend = require_openssl_backend(); !backend) {
return std::unexpected{backend.error()};
}
auto seed = read_seed(seed_path);
if (!seed) {
return std::unexpected{seed.error()};
}
auto private_key = Void::Crypto::Ed25519PrivateKey::from_raw_seed(seed->view());
if (!private_key) {
return std::unexpected{private_key.error()};
}
auto public_key = private_key->public_key();
if (!public_key) {
return std::unexpected{public_key.error()};
}
return Void::Encoding::Hex::encode(public_key->view());
}
std::string canonical_package_message(std::string_view name, std::string_view version,
std::string_view sha256_hex) {
std::string message;
message.reserve(15 + name.size() + 1 + version.size() + 8 + sha256_hex.size() + 1);
message += "vbs-package-v1\n";
message += name;
message.push_back('@');
message += version;
message += "\nsha256:";
message += sha256_hex;
message.push_back('\n');
return message;
}
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) {
if (auto backend = require_openssl_backend(); !backend) {
return std::unexpected{backend.error()};
}
auto seed = read_seed(seed_path);
if (!seed) {
return std::unexpected{seed.error()};
}
auto private_key = Void::Crypto::Ed25519PrivateKey::from_raw_seed(seed->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);
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,
std::string_view public_key_hex) {
if (auto backend = require_openssl_backend(); !backend) {
return std::unexpected{backend.error()};
}
auto signature_bytes = decode_hex_exact(signature_hex, SignatureHexLength, "Ed25519 signature");
if (!signature_bytes) {
return std::unexpected{signature_bytes.error()};
}
auto public_key_bytes =
decode_hex_exact(public_key_hex, PublicKeyHexLength, "Ed25519 public key");
if (!public_key_bytes) {
return std::unexpected{public_key_bytes.error()};
}
auto signature = Void::Crypto::Ed25519Signature::from_bytes(
std::as_bytes(std::span{signature_bytes->data(), signature_bytes->size()}));
if (!signature) {
return std::unexpected{signature.error()};
}
auto public_key = Void::Crypto::Ed25519PublicKey::from_bytes(
std::as_bytes(std::span{public_key_bytes->data(), public_key_bytes->size()}));
if (!public_key) {
return std::unexpected{public_key.error()};
}
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);
}
} // namespace Void::Build::Provenance