18 #include "3rdparty/monocypher/monocypher.h"
19 #include "3rdparty/monocypher/monocypher-ed25519.h"
20 #include "3rdparty/nlohmann/json.hpp"
27 { 0xed, 0x5d, 0x57, 0x47, 0x21, 0x99, 0x8b, 0x02, 0xdf, 0x6e, 0x3d, 0x69, 0xe1, 0x87, 0xca, 0xd0, 0x0e, 0x88, 0xc3, 0xe2, 0xb2, 0xa6, 0x7b, 0xc0, 0x42, 0xc8, 0xd6, 0x4b, 0x65, 0xe6, 0x48, 0xf7 },
43 std::array<uint8_t, 32> digest;
44 crypto_blake2b_ctx ctx;
45 crypto_blake2b_init(&ctx, digest.size());
48 std::array<uint8_t, 1024> buf;
49 size_t len = fread(buf.data(), 1, buf.size(), f);
51 crypto_blake2b_update(&ctx, buf.data(), len);
55 crypto_blake2b_final(&ctx, digest.data());
69 auto pos = checksum.find(
'$');
70 assert(pos != std::string::npos);
71 const std::string version = checksum.substr(0, pos);
72 const std::string hash = checksum.substr(pos + 1);
75 std::string calculated_hash;
79 Debug(misc, 0,
"Failed to validate signature: unknown checksum version: {}", filename);
84 if (calculated_hash.empty()) {
85 Debug(misc, 0,
"Failed to validate signature: couldn't calculate checksum for: {}", filename);
88 if (calculated_hash != hash) {
89 Debug(misc, 0,
"Failed to validate signature: checksum mismatch for: {}", filename);
104 static bool ValidateSignature(
const std::string &signature,
const nlohmann::json &files,
const std::string &filename)
107 auto pos = signature.find(
'$');
108 assert(pos != std::string::npos);
109 const std::string version = signature.substr(0, pos);
110 const std::string sig_value = signature.substr(pos + 1);
113 std::string message = files.dump(-1);
116 if (version ==
"1") {
117 std::array<uint8_t, 64> sig;
119 Debug(misc, 0,
"Failed to validate signature: invalid signature: {}", filename);
125 auto res = crypto_ed25519_check(sig.data(), pk_value.data(),
reinterpret_cast<uint8_t *
>(message.data()), message.size());
131 Debug(misc, 0,
"Failed to validate signature: signature validation failed: {}", filename);
134 Debug(misc, 0,
"Failed to validate signature: unknown signature version: {}", filename);
148 static bool ValidateSchema(
const nlohmann::json &signatures,
const std::string &filename)
150 if (signatures[
"files"].is_null()) {
151 Debug(misc, 0,
"Failed to validate signature: no files found: {}", filename);
155 if (signatures[
"signature"].is_null()) {
156 Debug(misc, 0,
"Failed to validate signature: no signature found: {}", filename);
160 for (
auto &signature : signatures[
"files"]) {
161 if (signature[
"filename"].is_null() || signature[
"checksum"].is_null()) {
162 Debug(misc, 0,
"Failed to validate signature: invalid entry in files: {}", filename);
166 const std::string sig_filename = signature[
"filename"];
167 const std::string sig_checksum = signature[
"checksum"];
169 if (sig_filename.empty() || sig_checksum.empty()) {
170 Debug(misc, 0,
"Failed to validate signature: invalid entry in files: {}", filename);
174 auto pos = sig_checksum.find(
'$');
175 if (pos == std::string::npos) {
176 Debug(misc, 0,
"Failed to validate signature: invalid checksum format: {}", filename);
181 const std::string signature = signatures[
"signature"];
182 auto pos = signature.find(
'$');
183 if (pos == std::string::npos) {
184 Debug(misc, 0,
"Failed to validate signature: invalid signature format: {}", filename);
202 Debug(misc, 0,
"Failed to validate signature: file not found: {}", filename);
206 std::string text(filesize,
'\0');
207 size_t len = fread(text.data(), filesize, 1, f);
209 Debug(misc, 0,
"Failed to validate signature: failed to read file: {}", filename);
213 nlohmann::json signatures;
215 signatures = nlohmann::json::parse(text);
216 }
catch (nlohmann::json::exception &) {
217 Debug(misc, 0,
"Failed to validate signature: not a valid JSON file: {}", filename);
243 if (!
ValidateSignature(signatures[
"signature"], signatures[
"files"], filename)) {
247 std::string dirname = std::filesystem::path(filename).parent_path().string();
249 for (
auto &signature : signatures[
"files"]) {
250 const std::string sig_filename = dirname + PATHSEPCHAR + signature[
"filename"].get<std::string>();
251 const std::string sig_checksum = signature[
"checksum"];
273 #if defined(ALLOW_INVALID_SIGNATURE)