diff --git a/scripts/sign-bootstrap-release.sh b/scripts/sign-bootstrap-release.sh new file mode 100755 index 0000000..60e173c --- /dev/null +++ b/scripts/sign-bootstrap-release.sh @@ -0,0 +1,113 @@ +#!/usr/bin/env bash +# Sign the DragonX chain-bootstrap archive for the wallet's in-app bootstrap verifier (F5). +# +# The bootstrap archive (DRAGONX.zip) can be multiple GB, so the wallet does NOT sign the archive bytes. +# Instead it verifies a detached ed25519 signature over the published "DRAGONX.zip.sha256" FILE, against a +# public key pinned in src/util/bootstrap.h (kBootstrapSignaturePublicKeyBase64). The SHA-256 file already +# binds the archive bytes (the wallet computes and matches the archive's hash first), so signing the small +# checksum authenticates the whole download cheaply. +# +# Uses OpenSSL (>= 1.1.1) only. OpenSSL's ed25519 is PureEdDSA (RFC 8032), the same primitive libsodium's +# crypto_sign_verify_detached checks, so the signatures are compatible. +# +# Usage: +# scripts/sign-bootstrap-release.sh keygen [out-prefix] # -> .ed25519.{key,pub.b64} +# scripts/sign-bootstrap-release.sh pubkey # print the base64 public key to pin +# scripts/sign-bootstrap-release.sh sign ... # sign existing files -> .sig +# scripts/sign-bootstrap-release.sh checksum +# # compute .sha256 (sha256sum format) and sign it -> .sha256 + .sha256.sig +# +# Publish BOTH "DRAGONX.zip.sha256" and "DRAGONX.zip.sha256.sig" next to the archive. The wallet verifies +# the signature over the EXACT .sha256 bytes, so upload the same file that was signed. Keep the secret key +# (.ed25519.key) OFFLINE (mode 600). Flip kBootstrapRequireSignature to true in bootstrap.h once every +# served bootstrap publishes a signature, to make a missing signature fail closed. + +set -euo pipefail +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +die() { echo "error: $*" >&2; exit 1; } +command -v openssl >/dev/null || die "openssl not found (need >= 1.1.1 with ed25519)" + +# Raw 32-byte ed25519 public key (base64) from a private key file (DER SPKI = 12-byte prefix + 32-byte key). +pubkey_b64() { openssl pkey -in "$1" -pubout -outform DER | tail -c 32 | openssl base64 -A; } + +sha256_of() { + if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | awk '{print $1}'; + else shasum -a 256 "$1" | awk '{print $1}'; fi +} + +# Detached ed25519 signature over the raw file bytes -> .sig (base64 of the 64-byte sig). +sign_file() { + local key="$1" f="$2" raw + raw="$(mktemp)" + openssl pkeyutl -sign -inkey "$key" -rawin -in "$f" -out "$raw" + openssl base64 -A -in "$raw" > "$f.sig" + printf '\n' >> "$f.sig" + rm -f "$raw" +} + +cmd="${1:-}"; shift || true +case "$cmd" in + keygen) + prefix="${1:-dragonx-bootstrap}" + [ -e "$prefix.ed25519.key" ] && die "$prefix.ed25519.key already exists — refusing to overwrite" + openssl genpkey -algorithm ed25519 -out "$prefix.ed25519.key" + chmod 600 "$prefix.ed25519.key" + pub="$(pubkey_b64 "$prefix.ed25519.key")" + printf '%s\n' "$pub" > "$prefix.ed25519.pub.b64" + echo "secret key : $prefix.ed25519.key (KEEP OFFLINE, mode 600)" + echo "public key : $prefix.ed25519.pub.b64" + echo + echo "Pin this in src/util/bootstrap.h (kBootstrapSignaturePublicKeyBase64):" + echo " $pub" + ;; + + pubkey) + [ $# -ge 1 ] || die "usage: pubkey " + pubkey_b64 "$1" + ;; + + sign) + [ $# -ge 2 ] || die "usage: sign ..." + key="$1"; shift + [ -f "$key" ] || die "no such key: $key" + for f in "$@"; do + [ -f "$f" ] || die "no such file: $f" + sign_file "$key" "$f" + echo "signed: $f -> $f.sig" + done + ;; + + checksum) + [ $# -ge 2 ] || die "usage: checksum " + key="$1"; zip="$2" + [ -f "$key" ] || die "no such key: $key" + [ -f "$zip" ] || die "no such archive: $zip" + + # Warn if this key does not match the public key pinned in the wallet (it would then reject every + # signature made with it — expected only when deliberately rotating the pinned key). + pinned="$(grep -oE '"[A-Za-z0-9+/]{43}="' "$PROJECT_ROOT/src/util/bootstrap.h" 2>/dev/null | head -1 | tr -d '"')" + mine="$(pubkey_b64 "$key")" + if [ -n "$pinned" ] && [ "$pinned" != "$mine" ]; then + echo "WARNING: this key's public key does not match the one pinned in bootstrap.h:" >&2 + echo " signing key -> $mine" >&2 + echo " pinned key -> $pinned" >&2 + echo " The wallet will REJECT these signatures unless you are rotating the pinned key." >&2 + echo >&2 + fi + + sum="$(sha256_of "$zip")" + printf '%s %s\n' "$sum" "$(basename "$zip")" > "$zip.sha256" + sign_file "$key" "$zip.sha256" + echo "wrote : $zip.sha256 (sha256=$sum)" + echo "signed : $zip.sha256.sig" + echo + echo "Publish both files next to the archive:" + echo " $(basename "$zip").sha256" + echo " $(basename "$zip").sha256.sig" + ;; + + *) + die "usage: $0 {keygen [prefix] | pubkey | sign ... | checksum }" + ;; +esac diff --git a/src/util/bootstrap.cpp b/src/util/bootstrap.cpp index bc2b5ca..df08c1a 100644 --- a/src/util/bootstrap.cpp +++ b/src/util/bootstrap.cpp @@ -18,6 +18,7 @@ #include #include "../util/logger.h" #include "http_download.h" +#include "xmrig_updater.h" // util::verifyXmrigSignature (shared ed25519 verify) — F5 namespace fs = std::filesystem; @@ -1153,9 +1154,43 @@ bool Bootstrap::verifyChecksums(const std::string& zipPath, const std::string& b } DEBUG_LOGF("[Bootstrap] SHA-256 verified: %s\n", actual.c_str()); + // F5 — the checksum proves the archive matches a hash the server served, but not that the hash is + // ours. Authenticate it with a detached ed25519 signature over the .sha256 content (pinned key). + if (!verifyArchiveSignature(sha256Content, baseUrl)) return false; // keeps the archive, sets Failed + setProgress(State::Verifying, "Checksums verified \xe2\x9c\x93", 100.0, 100.0); return true; } +bool Bootstrap::verifyArchiveSignature(const std::string& sha256Content, const std::string& baseUrl) { + const std::string pubKey = kBootstrapSignaturePublicKeyBase64 ? kBootstrapSignaturePublicKeyBase64 : ""; + + HttpGetOptions opt; opt.cancel = &cancel_requested_; + const std::string sig = httpGetString(baseUrl + "/" + kZipName + ".sha256.sig", "[bootstrap]", opt); + if (cancel_requested_) return false; + + if (sig.empty() || pubKey.empty()) { + if (kBootstrapRequireSignature) { + DEBUG_LOGF("[Bootstrap] No ed25519 signature published - refusing (signature required)\n"); + setProgress(State::Failed, "Verification failed: the bootstrap is not signed. " + "Refusing to install an unauthenticated archive."); + return false; // fail CLOSED; archive is KEPT + } + DEBUG_LOGF("[Bootstrap] No ed25519 signature published - relying on SHA-256 over TLS only\n"); + return true; // defense-in-depth not yet enforced server-side + } + + // Verify over the EXACT .sha256 bytes the signer signed (not the parsed hash). A present-but-invalid + // signature is always fatal — it means the checksum we just trusted was not issued by us. + if (!util::verifyXmrigSignature(sha256Content, sig, pubKey)) { + DEBUG_LOGF("[Bootstrap] ed25519 signature MISMATCH - refusing\n"); + setProgress(State::Failed, "Verification failed: the bootstrap signature did not match the pinned " + "release key. Refusing to install."); + return false; // fail CLOSED; archive is KEPT + } + DEBUG_LOGF("[Bootstrap] ed25519 signature verified against the pinned key\n"); + return true; +} + } // namespace util } // namespace dragonx diff --git a/src/util/bootstrap.h b/src/util/bootstrap.h index ce697af..bafaddc 100644 --- a/src/util/bootstrap.h +++ b/src/util/bootstrap.h @@ -75,6 +75,19 @@ public: static constexpr const char* kMirrorUrl = "https://bootstrap2.dragonx.is"; static constexpr const char* kZipName = "DRAGONX.zip"; + // F5 — authenticity of the bootstrap archive. A same-origin SHA-256 only proves transport integrity: + // whoever can replace the archive (CDN/server compromise, or a TLS break) can replace its .sha256 too. + // So we additionally verify a detached ed25519 signature, over the published .sha256 file (not the + // multi-GB archive — the checksum already binds the archive bytes), against the key pinned here. A + // signature that is PRESENT but invalid is always fatal. A MISSING signature is fatal only when + // kBootstrapRequireSignature is true; until bootstrap.dragonx.is publishes per-archive + // ".sha256.sig" files, it stays false (SHA-256 over verified TLS still applies) — flip it to true + // once signatures are published to fully close the archive-substitution gap. Sign with + // scripts/sign-bootstrap-release.sh; the secret key stays offline. + static constexpr const char* kBootstrapSignaturePublicKeyBase64 = + "CbyBk7GAw9PFU3OEWoXdm6FfxdpIX3LMy4b4EnurJDM="; + static constexpr bool kBootstrapRequireSignature = false; + /// Start the bootstrap process on a background thread. /// @param dataDir Path to DRAGONX data dir (e.g. ~/.hush/DRAGONX/) /// @param url Bootstrap zip URL @@ -177,6 +190,11 @@ private: /// Verify downloaded zip against remote checksums. Returns true if valid. bool verifyChecksums(const std::string& zipPath, const std::string& baseUrl); + /// F5 — verify the detached ed25519 signature over the published .sha256 content (already matched to + /// the archive) against the pinned key. Returns false (and sets the Failed state) on a present-but- + /// invalid signature, or on a missing signature when kBootstrapRequireSignature is true. + bool verifyArchiveSignature(const std::string& sha256Content, const std::string& baseUrl); + struct RemoteInfo { bool transportOk = false; long httpCode = 0; long long size = -1; }; /// HEAD the remote file. transportOk=false on any curl error; size=-1 when absent. RemoteInfo getRemoteFileSize(const std::string& url); diff --git a/tests/test_phase4.cpp b/tests/test_phase4.cpp index 6168e02..1139172 100644 --- a/tests/test_phase4.cpp +++ b/tests/test_phase4.cpp @@ -14680,6 +14680,47 @@ void testDaemonSignatureInterop() EXPECT_FALSE(verifyXmrigSignature(msg, sigB64, std::string(kXmrigSignaturePublicKeyBase64))); } +void testBootstrapSignatureInterop() +{ + using namespace dragonx::util; + // F5 — known answer from scripts/sign-bootstrap-release.sh (OpenSSL ed25519) over the published + // " DRAGONX.zip\n" checksum file, verified with libsodium against the key pinned in + // bootstrap.h. The wallet signs the small checksum (not the multi-GB archive), so this is the exact + // byte string the signer signs and the verifier checks. Proves the pinned bootstrap key matches the + // signing key and that a tampered checksum / wrong key fails closed. + const std::string content = + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 DRAGONX.zip\n"; + const std::string sigB64 = + "MXcbt9SBbiScAXknsf3vfBDp7pXXC9Ii0Jx3RT03Tssps/LJmxgNyFu2onK7T2VLq8bhaudoyxt0C2cLxyTUAg=="; + const std::string pin = Bootstrap::kBootstrapSignaturePublicKeyBase64; + EXPECT_TRUE(verifyXmrigSignature(content, sigB64, pin)); + EXPECT_TRUE(verifyXmrigSignature(content, " " + sigB64 + "\n", pin)); // trailing newline tolerated + // Fails closed on a tampered checksum, a different checksum, or the wrong pinned key. + EXPECT_FALSE(verifyXmrigSignature(content + "x", sigB64, pin)); + EXPECT_FALSE(verifyXmrigSignature( + "0000000000000000000000000000000000000000000000000000000000000000 DRAGONX.zip\n", sigB64, pin)); + EXPECT_FALSE(verifyXmrigSignature(content, sigB64, std::string(kXmrigSignaturePublicKeyBase64))); + // The pinned key must be valid base64 for a 32-byte ed25519 public key. + EXPECT_TRUE(sodium_init() >= 0); + unsigned char pk[crypto_sign_PUBLICKEYBYTES]; + std::size_t n = 0; const char* end = nullptr; + EXPECT_EQ(sodium_base642bin(pk, sizeof(pk), pin.data(), pin.size(), " \t\r\n", &n, &end, + sodium_base64_VARIANT_ORIGINAL), 0); + EXPECT_EQ(n, static_cast(crypto_sign_PUBLICKEYBYTES)); +} + +// F13 — the migrate-to-seed port picker must hand out an OS-assigned free port via a real bind(), not a +// guessable fixed-range number, so a local co-tenant can't pre-squat the port the isolated node will take. +void testEphemeralPortPick() +{ + using dragonx::daemon::EmbeddedDaemon; + const int a = EmbeddedDaemon::pickEphemeralPort(); + EXPECT_TRUE(a > 1024); // a real ephemeral port, not 0/low/fixed + EXPECT_FALSE(EmbeddedDaemon::tcpPortInUse(a)); // nothing is LISTENing on a port just handed out free + const int b = EmbeddedDaemon::pickEphemeralPort(); + EXPECT_TRUE(b > 1024); +} + void testDaemonReleaseListParsing() { using namespace dragonx::util; @@ -20545,6 +20586,8 @@ int main() testDaemonSignatureAssetSelection(); testDaemonPinnedKeyValidity(); testDaemonSignatureInterop(); + testBootstrapSignatureInterop(); + testEphemeralPortPick(); testDaemonReleaseListParsing(); testLiteServerHostParsing(); testLiteOfficialServerDetection();