use std::collections::BTreeSet;
use openmls::prelude::{Credential, CredentialType};
use sew_proto::e2ee::CallScope;
use sew_proto::e2ee::tls::{
DeviceSigInput, IDENTITY_KIND_MEMBER, IDENTITY_VERSION, SewCallIdentity, TlsCodec,
};
use super::cert::{CertContext, check_device_cert, verify_device_sig};
use crate::config::caps;
use crate::ids::{Id, UnixMs, install_id_bytes};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeafIdentity {
pub member_id: Id,
pub device_id: Id,
pub serial: u64,
pub platform: u8,
pub label: String,
pub cert_not_after_s: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum LeafCheckError {
#[error("credential: {0}")]
Credential(&'static str),
#[error("scope: {0}")]
Scope(&'static str),
#[error("device certificate: {0}")]
Cert(&'static str),
#[error("revoked")]
Revoked,
#[error("device signature")]
DeviceSig,
}
impl LeafCheckError {
pub fn is_scope(&self) -> bool {
matches!(self, LeafCheckError::Scope(_))
}
}
#[derive(Debug, Clone, Copy)]
pub struct LeafContext<'a> {
pub install_id: u64,
pub ca_public: &'a [u8; 32],
pub crl: &'a BTreeSet<u64>,
pub scope: CallScope,
pub now: UnixMs,
pub skew_s: u64,
}
pub fn check_leaf(
credential: &Credential,
leaf_signature_key: &[u8],
ctx: &LeafContext<'_>,
) -> Result<LeafIdentity, LeafCheckError> {
if credential.credential_type() != CredentialType::Basic {
return Err(LeafCheckError::Credential("not_basic"));
}
let raw = credential.serialized_content();
if raw.len() > caps::CALL_IDENTITY {
return Err(LeafCheckError::Credential("size"));
}
let identity =
SewCallIdentity::from_bytes(raw).map_err(|_| LeafCheckError::Credential("decode"))?;
check_identity(&identity, leaf_signature_key, ctx)
}
pub fn check_identity(
identity: &SewCallIdentity,
leaf_signature_key: &[u8],
ctx: &LeafContext<'_>,
) -> Result<LeafIdentity, LeafCheckError> {
if identity.version != IDENTITY_VERSION {
return Err(LeafCheckError::Credential("version"));
}
if identity.kind != IDENTITY_KIND_MEMBER {
return Err(LeafCheckError::Credential("kind"));
}
if identity.scope.is_empty() {
return Err(LeafCheckError::Scope("empty"));
}
let scope = CallScope::parse(&identity.scope).ok_or(LeafCheckError::Scope("malformed"))?;
if scope != ctx.scope {
return Err(LeafCheckError::Scope("mismatch"));
}
if identity.device_cert.len() > caps::DEVICE_CERT {
return Err(LeafCheckError::Cert("size"));
}
let cert = sew_proto::e2ee::tls::SewDeviceCert::from_bytes(&identity.device_cert)
.map_err(|_| LeafCheckError::Cert("decode"))?;
let cctx = CertContext {
install_id: ctx.install_id,
ca_public: ctx.ca_public,
skew_s: ctx.skew_s,
now: ctx.now,
};
let verified = check_device_cert(cert, &cctx).map_err(|e| match e {
crate::error::IdentityError::BadCert(r) => LeafCheckError::Cert(r),
_ => LeafCheckError::Cert("cert"),
})?;
if ctx.crl.contains(&verified.serial()) {
return Err(LeafCheckError::Revoked);
}
let tbs = &verified.cert.tbs;
let input = DeviceSigInput {
install_id: tbs.install_id.clone(),
member_id: tbs.member_id,
device_id: tbs.device_id,
scope: identity.scope.clone(),
leaf_signature_key: leaf_signature_key.to_vec(),
};
if !verify_device_sig(&tbs.device_pubkey, &input, &identity.device_sig) {
return Err(LeafCheckError::DeviceSig);
}
Ok(LeafIdentity {
member_id: verified.member_id,
device_id: verified.device_id,
serial: tbs.serial,
platform: tbs.platform,
label: String::from_utf8_lossy(&tbs.label).into_owned(),
cert_not_after_s: tbs.not_after,
})
}
pub fn device_sig_message(
install_id: u64,
member_id: Id,
device_id: Id,
scope: CallScope,
leaf_signature_key: &[u8],
) -> Vec<u8> {
DeviceSigInput {
install_id: install_id_bytes(install_id),
member_id: member_id.get(),
device_id: device_id.get(),
scope: scope.to_bytes(),
leaf_signature_key: leaf_signature_key.to_vec(),
}
.signed_bytes()
.unwrap_or_default()
}
pub fn identity_bytes(scope: CallScope, device_cert: &[u8], device_sig: &[u8; 64]) -> Vec<u8> {
SewCallIdentity {
version: IDENTITY_VERSION,
kind: IDENTITY_KIND_MEMBER,
scope: scope.to_bytes(),
device_cert: device_cert.to_vec(),
device_sig: device_sig.to_vec(),
}
.to_bytes()
.unwrap_or_default()
}