use crate::builder::{peek_tct_claims, BundleSigningView, DEFAULT_BUNDLE_VERSION};
use crate::error::SessionBundleError;
use crate::types::SessionTrustBundle;
use aitp_core::{jcs, Aid, Timestamp};
use aitp_crypto::{AitpVerifyingKey, Signature};
use aitp_tct::{verify_tct, TctError, TctVerifyContext};
use sha2::{Digest, Sha256};
use uuid::Uuid;
pub struct VerifySessionBundleContext<'a> {
pub verifier_aid: &'a Aid,
pub now: Timestamp,
pub revocation_check: Option<&'a dyn Fn(&Uuid) -> bool>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BundleOutcome {
Clear {
active_aids: Vec<Aid>,
},
DegradedSubset {
active_aids: Vec<Aid>,
dropped_aids: Vec<Aid>,
},
}
pub fn verify_session_bundle(
bundle: &SessionTrustBundle,
ctx: &VerifySessionBundleContext<'_>,
) -> Result<BundleOutcome, SessionBundleError> {
if bundle.version != DEFAULT_BUNDLE_VERSION {
return Err(SessionBundleError::VersionMismatch);
}
if bundle.expires_at.is_in_the_past(ctx.now) {
return Err(SessionBundleError::Expired);
}
if bundle.participants.is_empty() {
return Err(SessionBundleError::EmptyParticipants);
}
let mut computed_min: Option<Timestamp> = None;
for p in &bundle.participants {
let exp = peek_tct_claims(&p.tct)?.exp;
computed_min = Some(match computed_min {
Some(m) if m.0 <= exp.0 => m,
_ => exp,
});
}
let computed_min = computed_min.ok_or(SessionBundleError::EmptyParticipants)?;
if computed_min.0 != bundle.expires_at.0 {
return Err(SessionBundleError::ExpiryWindowInvariant);
}
let verifier_present = bundle
.participants
.iter()
.any(|p| &p.aid == ctx.verifier_aid);
if !verifier_present {
return Err(SessionBundleError::NotMember);
}
let coord_key = AitpVerifyingKey::from_aid(&bundle.coordinator)?;
let view = BundleSigningView {
session_bundle: crate::builder::BundleSigningBody {
version: &bundle.version,
session_id: &bundle.session_id,
coordinator: &bundle.coordinator,
issued_at: &bundle.issued_at,
expires_at: &bundle.expires_at,
participants: &bundle.participants,
},
};
let canonical = jcs::canonicalize_serializable(&view)
.map_err(|e| SessionBundleError::Canonicalization(e.to_string()))?;
let digest = Sha256::digest(&canonical);
let outer_sig =
Signature::parse(&bundle.signature).map_err(|_| SessionBundleError::InvalidSignature)?;
coord_key
.verify(&digest, &outer_sig)
.map_err(|_| SessionBundleError::InvalidSignature)?;
let mut active = Vec::with_capacity(bundle.participants.len());
let mut dropped = Vec::new();
for entry in &bundle.participants {
let tct_ctx = TctVerifyContext::builder(&entry.aid, &bundle.coordinator, ctx.now)
.accept_unchecked_revocation_dangerous()
.skip_manifest_expiry_cap_dangerous()
.build()
.expect("both verify decisions are made above");
let verified = match verify_tct(&entry.tct, &tct_ctx) {
Ok(v) => v,
Err(TctError::IssuerMismatch) => {
return Err(SessionBundleError::CoordinatorIssuerMismatch)
}
Err(TctError::AudienceMismatch) => return Err(SessionBundleError::AudienceMismatch),
Err(e) => return Err(SessionBundleError::TctVerification(e)),
};
let is_revoked = ctx
.revocation_check
.map(|check| check(&verified.claims.jti))
.unwrap_or(false);
if is_revoked {
dropped.push(entry.aid.clone());
} else {
active.push(entry.aid.clone());
}
}
if dropped.is_empty() {
Ok(BundleOutcome::Clear {
active_aids: active,
})
} else {
Ok(BundleOutcome::DegradedSubset {
active_aids: active,
dropped_aids: dropped,
})
}
}