228 lines
8.0 KiB
Rust
228 lines
8.0 KiB
Rust
//! The policy proof (docs/TEAM.md, "The policy proof"): signed by the team identity, it says which request was
|
|
//! signed under which policy, with whose approvals, by whom, and over which envelope. Anyone who pinned the team
|
|
//! identity checks it with [`verify_proof`], which accepts only what the signing Custos itself would have signed.
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::member::{TeamIdentity, verify_policy_sig, verify_proof_sig};
|
|
use crate::policy::{KeyRole, Policy, RosterMember, evaluate};
|
|
use crate::request::SignRequest;
|
|
use crate::statement::{Statement, verified_members};
|
|
use crate::{TeamError, b64, canon, hex, sha256, unb64, unhex};
|
|
|
|
/// The proof as it travels: its canonical text and the team identity's signature over it (base64).
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
pub struct Proof {
|
|
pub text: String,
|
|
pub sig: String,
|
|
}
|
|
|
|
/// A roster member as a proof lists them.
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct RosterJson {
|
|
id: String,
|
|
name: String,
|
|
handles: Vec<String>,
|
|
ed25519: String,
|
|
}
|
|
|
|
#[derive(Deserialize)]
|
|
#[serde(deny_unknown_fields)]
|
|
struct BodyJson {
|
|
v: u32,
|
|
team: String,
|
|
request: String,
|
|
env: String,
|
|
author: String,
|
|
policy: String,
|
|
policy_sig: String,
|
|
roster: Vec<RosterJson>,
|
|
approvals: Vec<Statement>,
|
|
signer: String,
|
|
role: KeyRole,
|
|
signature: String,
|
|
at: u64,
|
|
}
|
|
|
|
/// What a proof says, as the team's Custos builds it.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct ProofBody {
|
|
pub team: String,
|
|
/// The request hash (hex).
|
|
pub request: String,
|
|
pub env: String,
|
|
pub author: String,
|
|
pub policy: String,
|
|
pub policy_sig: [u8; 64],
|
|
pub roster: Vec<RosterMember>,
|
|
pub approvals: Vec<Statement>,
|
|
pub signer: String,
|
|
pub role: KeyRole,
|
|
/// SHA-256 (hex) of the envelope that was returned.
|
|
pub signature: String,
|
|
pub at: u64,
|
|
}
|
|
|
|
impl ProofBody {
|
|
pub fn canonical(&self) -> String {
|
|
let mut o = String::with_capacity(1024);
|
|
o.push_str("{\"v\":1,\"team\":");
|
|
canon::string(&mut o, &self.team);
|
|
o.push_str(",\"request\":");
|
|
canon::string(&mut o, &self.request);
|
|
o.push_str(",\"env\":");
|
|
canon::string(&mut o, &self.env);
|
|
o.push_str(",\"author\":");
|
|
canon::string(&mut o, &self.author);
|
|
o.push_str(",\"policy\":");
|
|
canon::string(&mut o, &self.policy);
|
|
o.push_str(",\"policy_sig\":");
|
|
canon::string(&mut o, &b64(&self.policy_sig));
|
|
o.push_str(",\"roster\":[");
|
|
for (i, m) in self.roster.iter().enumerate() {
|
|
if i > 0 {
|
|
o.push(',');
|
|
}
|
|
o.push_str("{\"id\":");
|
|
canon::string(&mut o, &m.id);
|
|
o.push_str(",\"name\":");
|
|
canon::string(&mut o, &m.name);
|
|
o.push_str(",\"handles\":");
|
|
canon::strings(&mut o, &m.handles);
|
|
o.push_str(",\"ed25519\":");
|
|
canon::string(&mut o, &hex(&m.ed25519));
|
|
o.push('}');
|
|
}
|
|
o.push_str("],\"approvals\":[");
|
|
for (i, s) in self.approvals.iter().enumerate() {
|
|
if i > 0 {
|
|
o.push(',');
|
|
}
|
|
o.push_str("{\"member\":");
|
|
canon::string(&mut o, &s.member);
|
|
o.push_str(",\"code\":");
|
|
canon::string(&mut o, &s.code);
|
|
o.push_str(",\"at\":");
|
|
o.push_str(&s.at.to_string());
|
|
o.push_str(",\"sig\":");
|
|
canon::string(&mut o, &s.sig);
|
|
o.push('}');
|
|
}
|
|
o.push_str("],\"signer\":");
|
|
canon::string(&mut o, &self.signer);
|
|
o.push_str(",\"role\":");
|
|
canon::string(&mut o, self.role.word());
|
|
o.push_str(",\"signature\":");
|
|
canon::string(&mut o, &self.signature);
|
|
o.push_str(",\"at\":");
|
|
o.push_str(&self.at.to_string());
|
|
o.push('}');
|
|
o
|
|
}
|
|
|
|
/// Signed by the team identity.
|
|
pub fn sign(&self, identity: &TeamIdentity) -> Proof {
|
|
let text = self.canonical();
|
|
let sig = identity.sign_proof(&text);
|
|
Proof { text, sig: b64(&sig) }
|
|
}
|
|
|
|
pub fn parse(text: &str) -> Result<ProofBody, TeamError> {
|
|
if text.len() > 64 * 1024 {
|
|
return Err(TeamError::NotCanonical("a proof is at most 64 KiB"));
|
|
}
|
|
let j: BodyJson = serde_json::from_str(text).map_err(|_| TeamError::NotCanonical("not a proof"))?;
|
|
if j.v != 1 {
|
|
return Err(TeamError::NotCanonical("v is not 1"));
|
|
}
|
|
let mut roster = Vec::new();
|
|
for m in j.roster {
|
|
let ed25519 = unhex::<32>(&m.ed25519).ok_or(TeamError::Invalid("a roster key is not 64 hex"))?;
|
|
roster.push(RosterMember { id: m.id, name: m.name, handles: m.handles, ed25519 });
|
|
}
|
|
let body = ProofBody {
|
|
team: j.team,
|
|
request: j.request,
|
|
env: j.env,
|
|
author: j.author,
|
|
policy: j.policy,
|
|
policy_sig: unb64(&j.policy_sig)
|
|
.and_then(|s| s.try_into().ok())
|
|
.ok_or(TeamError::Invalid("policy_sig is not base64 of 64 bytes"))?,
|
|
roster,
|
|
approvals: j.approvals,
|
|
signer: j.signer,
|
|
role: j.role,
|
|
signature: j.signature,
|
|
at: j.at,
|
|
};
|
|
if body.canonical() != text {
|
|
return Err(TeamError::NotCanonical("key order, spaces or escaping"));
|
|
}
|
|
Ok(body)
|
|
}
|
|
}
|
|
|
|
/// What a proof established.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct Verified {
|
|
pub role: KeyRole,
|
|
pub env: String,
|
|
pub signer: String,
|
|
/// The approvals that counted under the policy.
|
|
pub approvers: Vec<String>,
|
|
pub policy_rev: u32,
|
|
}
|
|
|
|
/// Checks a proof against the pinned team identity, the request it was made for and the envelope that came back
|
|
/// with it: both signatures, every approval (signature, roster, the request's own match code), the request and
|
|
/// envelope hashes, and the policy evaluated again. The clock is not consulted: a proof stays checkable after its
|
|
/// request expired.
|
|
pub fn verify_proof(
|
|
proof: &Proof,
|
|
identity: &[u8; 32],
|
|
request_text: &str,
|
|
envelope: &[u8],
|
|
) -> Result<Verified, TeamError> {
|
|
let sig: [u8; 64] = unb64(&proof.sig).and_then(|s| s.try_into().ok()).ok_or(TeamError::BadSignature)?;
|
|
if !verify_proof_sig(identity, &proof.text, &sig) {
|
|
return Err(TeamError::BadSignature);
|
|
}
|
|
let body = ProofBody::parse(&proof.text)?;
|
|
let req = SignRequest::parse(request_text)?;
|
|
if body.request != req.hash_hex() {
|
|
return Err(TeamError::Invalid("the proof is for another request"));
|
|
}
|
|
if body.team != req.team || body.env != req.env || body.author != req.author {
|
|
return Err(TeamError::Invalid("the proof does not match the request's team, env or author"));
|
|
}
|
|
if body.signature != hex(&sha256(envelope)) {
|
|
return Err(TeamError::Invalid("the proof is for another signature"));
|
|
}
|
|
if !verify_policy_sig(identity, &body.policy, &body.policy_sig) {
|
|
return Err(TeamError::BadSignature);
|
|
}
|
|
let policy = Policy::parse(&body.policy)?;
|
|
if policy.team != body.team {
|
|
return Err(TeamError::WrongTeam);
|
|
}
|
|
if !body.roster.iter().any(|m| m.id == body.signer) {
|
|
return Err(TeamError::NotAMember);
|
|
}
|
|
for s in &body.approvals {
|
|
s.verify(&req, &body.roster)?;
|
|
}
|
|
let approvers = verified_members(&body.approvals, &req, &body.roster);
|
|
let eval = evaluate(&policy, &body.roster, &req.env, &req.author, &approvers);
|
|
let role = eval.clone().into_result()?;
|
|
if role != body.role {
|
|
return Err(TeamError::Invalid("the proof's key role is not what the policy gives"));
|
|
}
|
|
if role == KeyRole::Release && !eval.counted.contains(&body.signer) {
|
|
return Err(TeamError::Invalid("the signer's own approval is not among those that counted"));
|
|
}
|
|
Ok(Verified { role, env: body.env, signer: body.signer, approvers: eval.counted, policy_rev: policy.rev })
|
|
}
|