use std::collections::BTreeSet;
use std::fmt;
use openmls::prelude::{BasicCredential, Credential, ExternalSender, SignaturePublicKey};
use sew_proto::e2ee::{CallKeysParams, ReadyCallKeys, VerificationCode};
use tls_codec::Serialize as _;
use crate::config::{Params, caps};
use crate::error::TrustError;
use crate::store::{DeviceStore, Pins};
pub fn external_sender_bytes(ds_public: &[u8; 32], install_id: u64) -> Vec<u8> {
let credential: Credential =
BasicCredential::new(format!("sew-call-ds:{install_id}").into_bytes()).into();
ExternalSender::new(SignaturePublicKey::from(ds_public.to_vec()), credential)
.tls_serialize_detached()
.unwrap_or_default()
}
#[derive(Clone)]
pub struct TrustAnchors {
install_id: u64,
ca_public: [u8; 32],
ca_kid: String,
ds_public: [u8; 32],
external_sender: Vec<u8>,
crl: BTreeSet<u64>,
crl_partial: bool,
verification_code: VerificationCode,
params_wire: CallKeysParams,
}
impl fmt::Debug for TrustAnchors {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TrustAnchors")
.field("install_id", &self.install_id)
.field("ca_kid", &self.ca_kid)
.field("crl", &self.crl.len())
.finish_non_exhaustive()
}
}
fn key32(b: &[u8], what: &'static str) -> Result<[u8; 32], TrustError> {
<[u8; 32]>::try_from(b).map_err(|_| TrustError::Malformed(what))
}
impl TrustAnchors {
pub fn from_ready(
r: &ReadyCallKeys,
install_id: u64,
pins: &mut impl DeviceStore,
) -> Result<TrustAnchors, TrustError> {
let ca_public = key32(r.ca.public_key.as_bytes(), "ca.public_key")?;
let ds_public = key32(r.ds_public_key.as_bytes(), "ds_public_key")?;
if r.external_sender.len() > caps::EXTERNAL_SENDER {
return Err(TrustError::Malformed("external_sender"));
}
let external_sender = r.external_sender.as_bytes().to_vec();
if external_sender != external_sender_bytes(&ds_public, install_id) {
return Err(TrustError::Malformed("external_sender"));
}
match pins.load_pins(install_id) {
Some(p) => {
if p.ca_public != ca_public {
return Err(TrustError::ServerIdentityChanged { what: "ca" });
}
if p.ds_public != ds_public || p.external_sender != external_sender {
return Err(TrustError::ServerIdentityChanged {
what: "external_sender",
});
}
}
None => pins
.save_pins(&Pins {
install_id,
ca_public,
ds_public,
external_sender: external_sender.clone(),
})
.map_err(TrustError::Store)?,
}
Ok(TrustAnchors {
install_id,
ca_public,
ca_kid: r.ca.kid.clone(),
ds_public,
external_sender,
crl: r.crl.iter().map(|s| s.get()).collect(),
crl_partial: r.crl_partial,
verification_code: r.verification_code,
params_wire: r.params.clone(),
})
}
pub fn refresh(&mut self, r: &ReadyCallKeys) -> Result<(), TrustError> {
let ca_public = key32(r.ca.public_key.as_bytes(), "ca.public_key")?;
let ds_public = key32(r.ds_public_key.as_bytes(), "ds_public_key")?;
if ca_public != self.ca_public {
return Err(TrustError::ServerIdentityChanged { what: "ca" });
}
if ds_public != self.ds_public || r.external_sender.as_bytes() != self.external_sender {
return Err(TrustError::ServerIdentityChanged {
what: "external_sender",
});
}
self.crl = r.crl.iter().map(|s| s.get()).collect();
self.crl_partial = r.crl_partial;
self.verification_code = r.verification_code;
self.params_wire = r.params.clone();
Ok(())
}
pub fn revoke(&mut self, serials: &[u64]) {
self.crl.extend(serials.iter().copied());
}
pub fn install_id(&self) -> u64 {
self.install_id
}
pub fn ca_public(&self) -> &[u8; 32] {
&self.ca_public
}
pub fn ds_public(&self) -> &[u8; 32] {
&self.ds_public
}
pub fn external_sender(&self) -> &[u8] {
&self.external_sender
}
pub fn crl(&self) -> &BTreeSet<u64> {
&self.crl
}
pub fn crl_partial(&self) -> bool {
self.crl_partial
}
pub fn verification_code(&self) -> VerificationCode {
self.verification_code
}
pub fn params(&self) -> Result<Params, crate::error::SessionError> {
Params::from_wire(&self.params_wire)
}
pub fn params_wire(&self) -> &CallKeysParams {
&self.params_wire
}
}