//! The canonical sign.request, match codes, approval statements and the policy proof (docs/TEAM.md). use custos_team::policy::Policy; use custos_team::proof::ProofBody; use custos_team::statement::{approve, verified_members}; use custos_team::*; const NOW: u64 = 1_794_000_000; pub fn request(team: &str, env: &str, author: &str, public: [u8; 32]) -> SignRequest { SignRequest { team: team.into(), id: "aa".repeat(16), project: "mnovak/Arca".into(), release: "0.7.0".into(), env: env.into(), author: author.into(), format: Format::Minisign, artifact: custos_team::request::Artifact { name: "Arca_0.7.0_x64-setup.exe".into(), size: 25_145_977, sha256: "c4".repeat(32), }, artifact_digest: vec![9; 64], image: String::new(), public, expires: NOW + 3600, } } #[test] fn a_request_is_read_only_in_canonical_form() { let fabrica = custos_seal::Recipient::generate(); let r = request(&"01".repeat(16), "production", "mnovak", fabrica.public()); let text = r.canonical(); assert!(text.starts_with("{\"v\":1,\"op\":\"sign.request\",\"team\":\"0101")); assert_eq!(SignRequest::check(&text, NOW).unwrap(), r); assert_eq!(r.match_code(), fabrica.match_code()); for bad in [ text.replace(",\"id\"", ", \"id\""), text.replace("\"op\":\"sign.request\"", "\"op\":\"sign.reply\""), text.replace("\"format\":\"minisign\"", "\"format\":\"gpg\""), text.replace("mnovak/Arca", "mnovak/Arca\\u0041"), text.replace("Arca_0.7.0_x64-setup.exe", "../Arca.exe"), text.replace("\"env\":\"production\"", "\"env\":\"Production\""), format!("{text}\n"), text.replace("\"image\":\"\"", "\"image\":\"arca\""), ] { assert!(SignRequest::check(&bad, NOW).is_err(), "{bad}"); } assert_eq!(SignRequest::check(&text, r.expires), Err(TeamError::Expired)); let mut far = r.clone(); far.expires = NOW + 25 * 3600 + 1; assert!(SignRequest::check(&far.canonical(), NOW).is_err()); // Digest lengths per format. let mut apk = r.clone(); apk.format = Format::Apk; apk.artifact_digest = vec![1; 128]; assert!(SignRequest::check(&apk.canonical(), NOW).is_ok()); apk.artifact_digest = vec![1; 64 * 5]; assert!(SignRequest::check(&apk.canonical(), NOW).is_err()); let mut cosign = r.clone(); cosign.format = Format::Cosign; cosign.artifact_digest = vec![1; 32]; assert!(SignRequest::check(&cosign.canonical(), NOW).is_err(), "cosign needs an image"); cosign.image = "registry.example/team/arca".into(); assert!(SignRequest::check(&cosign.canonical(), NOW).is_ok()); } fn roster_of(keys: &[&MemberKey]) -> Vec { keys.iter() .enumerate() .map(|(i, k)| RosterMember { id: k.id(), name: format!("M{i}"), handles: if i == 0 { vec!["mnovak".into()] } else { vec![] }, ed25519: k.public().ed25519, }) .collect() } #[test] fn a_wrong_match_code_is_refused_and_never_counts() { let (a, b) = (MemberKey::generate(), MemberKey::generate()); let roster = roster_of(&[&a, &b]); let fabrica = custos_seal::Recipient::generate(); let req = request(&"01".repeat(16), "production", "mnovak", fabrica.public()); // B's card showed another code: B approves nothing. let wrong = if req.match_code() == "000000" { "000001" } else { "000000" }; assert_eq!(approve(&b, &req, wrong, NOW), Err(TeamError::WrongMatch)); // A request whose pub was swapped after B's card was drawn: the code B saw is the old request's. let shown = req.match_code(); let mut swapped = req.clone(); swapped.public = custos_seal::Recipient::generate().public(); assert_eq!(approve(&b, &swapped, &shown, NOW), Err(TeamError::WrongMatch)); // A statement made for one request does not count for the swapped one. let s = approve(&b, &req, &shown, NOW).unwrap(); s.verify(&req, &roster).unwrap(); assert_eq!(s.verify(&swapped, &roster), Err(TeamError::WrongMatch)); assert!(verified_members(&[s.clone()], &swapped, &roster).is_empty()); // A forged code inside a statement fails its signature or its code. let mut forged = s.clone(); forged.code = wrong.into(); assert_eq!(forged.verify(&req, &roster), Err(TeamError::WrongMatch)); let mut other_member = s.clone(); other_member.member = a.id(); assert_eq!(other_member.verify(&req, &roster), Err(TeamError::BadSignature)); // Not on the roster. let c = MemberKey::generate(); let sc = approve(&c, &req, &shown, NOW).unwrap(); assert_eq!(sc.verify(&req, &roster), Err(TeamError::NotAMember)); assert_eq!(verified_members(&[s.clone(), s.clone(), sc], &req, &roster), vec![b.id()]); } #[test] fn member_keys_seal_the_team_key_to_one_member_team_and_epoch() { let (a, b) = (MemberKey::generate(), MemberKey::generate()); let team = "02".repeat(16); let key = [7u8; 32]; let sealed = custos_team::member::seal_team_key(&key, &a.public(), &team, 3).unwrap(); assert_eq!(sealed.len(), custos_team::member::SEALED_KEY_LEN); assert_eq!(*a.open_team_key(&sealed, &team, 3).unwrap(), key); assert_eq!(a.open_team_key(&sealed, &team, 4), Err(TeamError::NotAMember)); assert_eq!(a.open_team_key(&sealed, &"03".repeat(16), 3), Err(TeamError::NotAMember)); assert_eq!(b.open_team_key(&sealed, &team, 3), Err(TeamError::NotAMember)); // The key round-trips through its secrets; ids and codes are stable. let again = MemberKey::from_secrets(a.x25519_secret(), a.ed25519_secret()).unwrap(); assert_eq!(again.public(), a.public()); assert_eq!(a.public().join_code(), custos_seal::match_code(&a.public().x25519)); assert_eq!(a.id().len(), 32); assert_ne!(a.id(), b.id()); assert_eq!(format!("{a:?}"), "[redacted]"); a.public().check().unwrap(); } #[test] fn a_proof_verifies_only_for_its_request_signature_policy_and_identity() { let identity = TeamIdentity::generate(); let team = "04".repeat(16); let (a, b) = (MemberKey::generate(), MemberKey::generate()); let roster = roster_of(&[&a, &b]); let policy = Policy::default_for(&team); let policy_text = policy.canonical(); let fabrica = custos_seal::Recipient::generate(); let req = request(&team, "production", "mnovak", fabrica.public()); let req_text = req.canonical(); let s = approve(&b, &req, &req.match_code(), NOW).unwrap(); let envelope = b"untrusted comment: ...".to_vec(); let body = |approvals: Vec, signer: String, role: KeyRole| ProofBody { team: team.clone(), request: req.hash_hex(), env: req.env.clone(), author: req.author.clone(), policy: policy_text.clone(), policy_sig: identity.sign_policy(&policy_text), roster: roster.clone(), approvals, signer, role, signature: custos_sign::sha256_hex(&envelope), at: NOW, }; let good = body(vec![s.clone()], b.id(), KeyRole::Release).sign(&identity); assert_eq!(ProofBody::parse(&good.text).unwrap(), body(vec![s.clone()], b.id(), KeyRole::Release)); let v = verify_proof(&good, &identity.public(), &req_text, &envelope).unwrap(); assert_eq!((v.role, v.signer.as_str(), v.approvers.clone()), (KeyRole::Release, b.id().as_str(), vec![b.id()])); // Another identity, another envelope, another request. assert_eq!( verify_proof(&good, &TeamIdentity::generate().public(), &req_text, &envelope), Err(TeamError::BadSignature) ); assert!(verify_proof(&good, &identity.public(), &req_text, b"another signature").is_err()); let mut other = req.clone(); other.release = "0.7.1".into(); assert!(verify_proof(&good, &identity.public(), &other.canonical(), &envelope).is_err()); // The author's own approval does not make a production proof. let sa = approve(&a, &req, &req.match_code(), NOW).unwrap(); let selfish = body(vec![sa], a.id(), KeyRole::Release).sign(&identity); assert_eq!(verify_proof(&selfish, &identity.public(), &req_text, &envelope), Err(TeamError::NeedMore(1))); // A proof claiming the test role for production, or signed by someone who did not approve. let wrong_role = body(vec![s.clone()], b.id(), KeyRole::Test).sign(&identity); assert!(verify_proof(&wrong_role, &identity.public(), &req_text, &envelope).is_err()); let not_approver = body(vec![s.clone()], a.id(), KeyRole::Release).sign(&identity); assert!(verify_proof(¬_approver, &identity.public(), &req_text, &envelope).is_err()); // An edited text breaks the identity's signature. let mut edited = good.clone(); edited.text = edited.text.replace("\"production\"", "\"staging\""); assert_eq!(verify_proof(&edited, &identity.public(), &req_text, &envelope), Err(TeamError::BadSignature)); }