//! End-to-end, offline: the quorum binary against //! - guix.sigs: real Bitcoin Core 32.1 attestations of achow101 and fanquake //! (tests/fixtures). The release files are too big to keep, so a stand-in is //! checked under a release file's name: MISMATCH (rather than BAD SIGNATURE) //! shows that the signature verified. //! - an in-toto feed built here: rebuilders signing `quorum statement` with //! cosign (Sigstore bundles), and one publishing a bare DSSE envelope. mod common; use std::path::{Path, PathBuf}; use std::process::{Command, Output}; const SETUP: &str = "bitcoin-32.1-win64-setup.exe"; const SETUP_HASH: &str = "d4cc64d500fa7713b5e2f3b9d46a215def3c4ed91aa46bf13e4de1a042bf9aa2"; const ACHOW: &str = "152812300785C96444D3334D17565732E08E5E41"; const FANQUAKE: &str = "E777299FC265DD04793070EB944D35F9AC3DB76A"; struct Env { dir: PathBuf, } impl Env { fn new(name: &str) -> Self { let dir = std::env::temp_dir().join(format!("quorum-test-{name}-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); std::fs::create_dir_all(&dir).unwrap(); Env { dir } } fn q(&self, args: &[&str]) -> Output { Command::new(env!("CARGO_BIN_EXE_quorum")) .arg("--home") .arg(self.dir.join("home")) .args(args) .current_dir(&self.dir) .output() .unwrap() } fn ok(&self, args: &[&str]) -> String { let o = self.q(args); assert!( o.status.success(), "{args:?}: {}{}", String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr) ); String::from_utf8_lossy(&o.stdout).into_owned() } fn path(&self, rel: &str) -> String { self.dir.join(rel).to_string_lossy().into_owned() } fn write(&self, rel: &str, data: &[u8]) -> String { let p = self.dir.join(rel); std::fs::create_dir_all(p.parent().unwrap()).unwrap(); std::fs::write(&p, data).unwrap(); p.to_string_lossy().into_owned() } } impl Drop for Env { fn drop(&mut self) { let _ = std::fs::remove_dir_all(&self.dir); } } fn out(o: &Output) -> String { String::from_utf8_lossy(&o.stdout).into_owned() } fn line<'a>(out: &'a str, who: &str) -> &'a str { out.lines() .find(|l| l.trim_start().starts_with(who)) .unwrap_or_else(|| panic!("no line for {who} in:\n{out}")) } fn copy_dir(from: &Path, to: &Path) { std::fs::create_dir_all(to).unwrap(); for e in std::fs::read_dir(from).unwrap() { let e = e.unwrap(); let dest = to.join(e.file_name()); if e.file_type().unwrap().is_dir() { copy_dir(&e.path(), &dest); } else { std::fs::copy(e.path(), dest).unwrap(); } } } fn sha256_hex(data: &[u8]) -> String { use sha2::Digest; hex::encode(sha2::Sha256::digest(data)) } // ── guix-sigs ─────────────────────────────────────────────────────────────── fn guix_env(name: &str) -> Env { let env = Env::new(name); let sigs = env.dir.join("sigs"); copy_dir( &Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/guix.sigs"), &sigs, ); env.ok(&[ "project", "add", "btc", "--feed", "guix-sigs", "--source", sigs.to_str().unwrap(), "--release-prefix", "bitcoin-", ]); for (n, f) in [("achow101", ACHOW), ("fanquake", FANQUAKE)] { env.ok(&["trust", "add", n, "--fingerprint", f, "--yes"]); } env.ok(&["trust", "threshold", "2"]); env.write("standin.exe", b"not bitcoin"); env } #[test] fn guix_genuine_signatures_verify_and_wrong_file_fails() { let env = guix_env("g-genuine"); let o = env.q(&["check", "--as", SETUP, &env.path("standin.exe")]); let s = out(&o); assert_eq!(o.status.code(), Some(1), "{s}"); for b in ["achow101", "fanquake"] { assert!( line(&s, b).contains("MISMATCH") && line(&s, b).contains(SETUP_HASH), "{s}" ); } } #[test] fn guix_edited_sums_are_bad_signatures() { let env = guix_env("g-forged"); let standin_hash = sha256_hex(b"not bitcoin"); let p = env.dir.join("sigs/32.1/achow101/all.SHA256SUMS"); let forged = std::fs::read_to_string(&p) .unwrap() .replace(SETUP_HASH, &standin_hash); std::fs::write(&p, forged).unwrap(); let s = out(&env.q(&["check", "--as", SETUP, &env.path("standin.exe")])); assert!(line(&s, "achow101").contains("BAD SIGNATURE"), "{s}"); assert!(line(&s, "fanquake").contains("MISMATCH"), "{s}"); } #[test] fn guix_someone_elses_signature_is_named() { let env = guix_env("g-swap"); for f in ["all.SHA256SUMS", "all.SHA256SUMS.asc"] { std::fs::copy( env.dir.join("sigs/32.1/fanquake").join(f), env.dir.join("sigs/32.1/achow101").join(f), ) .unwrap(); } let s = out(&env.q(&["check", "--as", SETUP, &env.path("standin.exe")])); assert!(line(&s, "achow101").contains("BAD SIGNATURE"), "{s}"); assert!(line(&s, "achow101").contains("fanquake's key"), "{s}"); } #[test] fn guix_pinning_refuses_wrong_fingerprint_and_reused_keys() { let env = guix_env("g-pin"); let o = env.q(&["trust", "add", "someone", "--fingerprint", ACHOW, "--yes"]); assert_eq!(o.status.code(), Some(2)); // no builder-keys/someone.gpg std::fs::copy( env.dir.join("sigs/builder-keys/achow101.gpg"), env.dir.join("sigs/builder-keys/achow-again.gpg"), ) .unwrap(); let o = env.q(&["trust", "add", "achow-again", "--yes"]); assert!(String::from_utf8_lossy(&o.stderr).contains("already pinned as achow101")); let o = env.q(&[ "trust", "add", "achow-again", "--fingerprint", FANQUAKE, "--yes", ]); assert!(String::from_utf8_lossy(&o.stderr).contains("none of these keys has fingerprint")); } #[test] fn guix_unlisted_and_missing() { let env = guix_env("g-missing"); let standin = env.path("standin.exe"); let s = out(&env.q(&["check", "--as", "bitcoin-32.1-win64-evil.exe", &standin])); assert!(line(&s, "achow101").contains("NOT LISTED"), "{s}"); let s = out(&env.q(&["check", "--as", "bitcoin-99.0-win64-setup.exe", &standin])); assert!(line(&s, "achow101").contains("NO ATTESTATION"), "{s}"); } // ── in-toto ───────────────────────────────────────────────────────────────── /// A feed for project "hello": rebuilders alice and bob (cosign keys), /// both attesting hello-1.0.exe for release 1.0. fn intoto_env(name: &str) -> Env { let env = Env::new(name); let artifact = env.write("dl/hello-1.0.exe", b"MZ hello, reproducibly\n"); for who in ["alice", "bob"] { common::rebuilder_key(&env.dir, who); common::attest(&env.dir, who, "1.0", &[&artifact]); } env.ok(&[ "project", "add", "hello", "--feed", "in-toto", "--source", &env.path("feed"), "--release-prefix", "hello-", ]); for who in ["alice", "bob"] { env.ok(&["trust", "-p", "hello", "add", who, "--yes"]); } env.ok(&["trust", "-p", "hello", "threshold", "2"]); env } #[test] fn intoto_agreeing_rebuilders_pass() { let env = intoto_env("i-pass"); let s = env.ok(&["check", "-p", "hello", &env.path("dl/hello-1.0.exe")]); assert!( line(&s, "alice").contains("OK") && line(&s, "bob").contains("OK"), "{s}" ); assert!(s.contains("PASS: 2 pinned rebuilder(s)"), "{s}"); } #[test] fn intoto_changed_file_fails() { let env = intoto_env("i-changed"); let evil = env.write("evil/hello-1.0.exe", b"MZ hello, with extras\n"); let o = env.q(&["check", "-p", "hello", &evil]); let s = out(&o); assert_eq!(o.status.code(), Some(1), "{s}"); assert!(line(&s, "alice").contains("MISMATCH"), "{s}"); } #[test] fn intoto_tampered_or_foreign_envelopes_dont_count() { let env = intoto_env("i-tamper"); // bob's bundle, presented as alice's: names bob's key. std::fs::copy( env.dir.join("feed/1.0/bob/attestation.sigstore.json"), env.dir.join("feed/1.0/alice/attestation.sigstore.json"), ) .unwrap(); // bob's payload edited (a subject's name changed): the signature breaks. let p = env.dir.join("feed/1.0/bob/attestation.sigstore.json"); let mut v: serde_json::Value = serde_json::from_slice(&std::fs::read(&p).unwrap()).unwrap(); use base64::Engine; let b64 = base64::engine::general_purpose::STANDARD; let payload = String::from_utf8( b64.decode(v["dsseEnvelope"]["payload"].as_str().unwrap()) .unwrap(), ) .unwrap(); v["dsseEnvelope"]["payload"] = b64 .encode(payload.replace("hello-1.0.exe", "hello-1.0.EXE")) .into(); std::fs::write(&p, serde_json::to_vec(&v).unwrap()).unwrap(); let s = out(&env.q(&["check", "-p", "hello", &env.path("dl/hello-1.0.exe")])); assert!( line(&s, "alice").contains("BAD SIGNATURE") && line(&s, "alice").contains("bob's key"), "{s}" ); assert!(line(&s, "bob").contains("BAD SIGNATURE"), "{s}"); assert!(s.contains("FAIL: only 0"), "{s}"); } #[test] fn intoto_an_attestation_moved_to_another_release_is_refused() { let env = intoto_env("i-release"); // The same bytes, offered as release 2.0 with alice's 1.0 attestation. let renamed = env.write("dl2/hello-2.0.exe", b"MZ hello, reproducibly\n"); let att = env.dir.join("feed/1.0/alice/attestation.sigstore.json"); std::fs::create_dir_all(env.dir.join("feed/2.0/alice")).unwrap(); std::fs::copy( &att, env.dir.join("feed/2.0/alice/attestation.sigstore.json"), ) .unwrap(); let s = out(&env.q(&["check", "-p", "hello", &renamed])); assert!(line(&s, "alice").contains("WRONG RELEASE"), "{s}"); assert!(line(&s, "bob").contains("NO ATTESTATION"), "{s}"); } #[test] fn intoto_threshold_counts_only_good_ones() { let env = intoto_env("i-threshold"); std::fs::remove_file(env.dir.join("feed/1.0/bob/attestation.sigstore.json")).unwrap(); let o = env.q(&["check", "-p", "hello", &env.path("dl/hello-1.0.exe")]); assert_eq!(o.status.code(), Some(1)); assert!(out(&o).contains("FAIL: only 1 pinned rebuilder(s) vouch")); } /// A rebuilder publishing a bare DSSE envelope with another tool's Statement /// (SLSA provenance, no quorum predicate), ECDSA P-256, DER signature. #[test] fn intoto_p256_rebuilder_counts() { use base64::Engine; use p256::ecdsa::signature::Signer; use p256::pkcs8::{EncodePublicKey, LineEnding}; let env = intoto_env("i-p256"); let sk = p256::ecdsa::SigningKey::from_slice(&[0x42; 32]).unwrap(); let pem = sk .verifying_key() .to_public_key_pem(LineEnding::LF) .unwrap(); env.write("feed/builder-keys/carol.pem", pem.as_bytes()); let hash = sha256_hex(b"MZ hello, reproducibly\n"); // A generic statement, as other tools write it (no quorum predicate). let statement = serde_json::json!({ "_type": "https://in-toto.io/Statement/v1", "subject": [{"name": "hello-1.0.exe", "digest": {"sha256": hash}}], "predicateType": "https://slsa.dev/provenance/v1", "predicate": {}, }); let payload = serde_json::to_vec(&statement).unwrap(); let ptype = "application/vnd.in-toto+json"; let mut pae = format!("DSSEv1 {} {} {} ", ptype.len(), ptype, payload.len()).into_bytes(); pae.extend_from_slice(&payload); let sig: p256::ecdsa::Signature = sk.sign(&pae); let b64 = base64::engine::general_purpose::STANDARD; let envelope = serde_json::json!({ "payloadType": ptype, "payload": b64.encode(&payload), "signatures": [{"keyid": "", "sig": b64.encode(sig.to_der().as_bytes())}], }); env.write( "feed/1.0/carol/attestation.dsse.json", envelope.to_string().as_bytes(), ); env.ok(&["trust", "-p", "hello", "add", "carol", "--yes"]); env.ok(&["trust", "-p", "hello", "threshold", "3"]); let s = env.ok(&["check", "-p", "hello", &env.path("dl/hello-1.0.exe")]); assert!(line(&s, "carol").contains("OK"), "{s}"); assert!(s.contains("PASS: 3 pinned rebuilder(s)"), "{s}"); } #[test] fn projects_are_separate() { let env = intoto_env("projects"); let sigs = env.dir.join("sigs"); copy_dir( &Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/guix.sigs"), &sigs, ); env.ok(&[ "project", "add", "btc", "--feed", "guix-sigs", "--source", sigs.to_str().unwrap(), ]); // With two projects, a command must say which. let o = env.q(&["check", &env.path("dl/hello-1.0.exe")]); assert!(String::from_utf8_lossy(&o.stderr).contains("several projects")); // hello's rebuilders aren't btc's. let s = env.ok(&["trust", "-p", "btc", "list"]); assert!(s.contains("No pinned rebuilders yet"), "{s}"); let s = env.ok(&["project", "list"]); assert!(s.contains("btc") && s.contains("hello"), "{s}"); }