use crate::builder::{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, 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 computed_min = bundle
.participants
.iter()
.map(|p| p.tct.expires_at)
.min_by_key(|t| t.0)
.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 {
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 {
if entry.tct.issuer != bundle.coordinator {
return Err(SessionBundleError::CoordinatorIssuerMismatch);
}
if entry.tct.audience != entry.aid {
return Err(SessionBundleError::AudienceMismatch);
}
let tct_ctx = TctVerifyContext {
expected_audience: &entry.aid,
issuer_pubkey: &coord_key,
now: ctx.now,
issuer_manifest_expires_at: None,
revocation_check: None, };
verify_tct(&entry.tct, &tct_ctx).map_err(SessionBundleError::TctVerification)?;
let is_revoked = ctx
.revocation_check
.map(|check| check(&entry.tct.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,
})
}
}