use crate::error::{NetError, Result};
use crate::profile::Profile;
use crate::user::{Card, decode_public, user_id};
use libp2p::PeerId;
use libp2p::identity::{Keypair, PublicKey};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
const MEMBERSHIP_DOMAIN: &str = "weirdnet/member/membership/1";
const REVOCATION_DOMAIN: &str = "weirdnet/member/revocation/1";
pub const CLOCK_SKEW_MS: i64 = 5 * 60 * 1000;
pub const MAX_FRAME: usize = 256 * 1024;
pub(crate) fn signed_bytes<T: Serialize>(domain: &str, body: &T) -> Vec<u8> {
let mut v = domain.as_bytes().to_vec();
v.push(b'\n');
v.extend(serde_json::to_vec(body).unwrap_or_default());
v
}
pub(crate) fn sign_with(kp: &Keypair, domain: &str, body: &impl Serialize) -> Result<Vec<u8>> {
kp.sign(&signed_bytes(domain, body)).map_err(|e| NetError::Crypto(format!("{domain}: {e}")))
}
pub(crate) fn issuer(key: &[u8], id: &str) -> Result<PublicKey> {
let k = decode_public(key)?;
if user_id(&k) != id {
return Err(NetError::Crypto("the issuer id does not match its key".into()));
}
Ok(k)
}
fn check_text(what: &'static str, s: &str, max: usize) -> Result<()> {
if s.is_empty() || s.len() > max || s.chars().any(char::is_control) {
return Err(NetError::invalid(what, format!("1 to {max} bytes, no control characters")));
}
Ok(())
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Membership {
pub v: u8,
pub resource: String,
pub user_id: String,
pub role: String,
pub issued_at: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires_at: Option<i64>,
pub issuer_id: String,
pub issuer_key: Vec<u8>,
pub sig: Vec<u8>,
}
impl Membership {
pub fn issue(owner: &Keypair, resource: &str, member: &str, role: &str, now: i64, expires_at: Option<i64>) -> Result<Membership> {
check_text("resource", resource, 256)?;
check_text("role", role, 64)?;
check_text("user id", member, 64)?;
let mut m = Membership {
v: 1,
resource: resource.into(),
user_id: member.into(),
role: role.into(),
issued_at: now,
expires_at,
issuer_id: user_id(&owner.public()),
issuer_key: owner.public().encode_protobuf(),
sig: vec![],
};
m.sig = sign_with(owner, MEMBERSHIP_DOMAIN, &m)?;
Ok(m)
}
pub fn verify(&self, now: i64) -> Result<()> {
if self.v != 1 {
return Err(NetError::invalid("membership", "unknown version"));
}
let k = issuer(&self.issuer_key, &self.issuer_id)?;
let mut body = self.clone();
body.sig = vec![];
if !k.verify(&signed_bytes(MEMBERSHIP_DOMAIN, &body), &self.sig) {
return Err(NetError::Crypto("membership: bad signature".into()));
}
if self.issued_at > now + CLOCK_SKEW_MS {
return Err(NetError::invalid("membership", "issued in the future"));
}
if self.expires_at.is_some_and(|e| e <= now) {
return Err(NetError::invalid("membership", "expired"));
}
Ok(())
}
pub fn verify_for(&self, owner: &str, resource: &str, now: i64) -> Result<()> {
if self.issuer_id != owner || self.resource != resource {
return Err(NetError::invalid("membership", "not for this resource"));
}
self.verify(now)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MemberRevocation {
pub v: u8,
pub resource: String,
pub user_id: String,
pub before: i64,
pub at: i64,
pub issuer_id: String,
pub issuer_key: Vec<u8>,
pub sig: Vec<u8>,
}
impl MemberRevocation {
pub fn revoke(owner: &Keypair, resource: &str, member: &str, before: i64, now: i64) -> Result<MemberRevocation> {
check_text("resource", resource, 256)?;
check_text("user id", member, 64)?;
let mut r = MemberRevocation {
v: 1,
resource: resource.into(),
user_id: member.into(),
before,
at: now,
issuer_id: user_id(&owner.public()),
issuer_key: owner.public().encode_protobuf(),
sig: vec![],
};
r.sig = sign_with(owner, REVOCATION_DOMAIN, &r)?;
Ok(r)
}
pub fn verify(&self) -> Result<()> {
if self.v != 1 {
return Err(NetError::invalid("revocation", "unknown version"));
}
let k = issuer(&self.issuer_key, &self.issuer_id)?;
let mut body = self.clone();
body.sig = vec![];
if !k.verify(&signed_bytes(REVOCATION_DOMAIN, &body), &self.sig) {
return Err(NetError::Crypto("revocation: bad signature".into()));
}
Ok(())
}
pub fn voids(&self, m: &Membership) -> bool {
self.issuer_id == m.issuer_id && self.resource == m.resource && self.user_id == m.user_id && m.issued_at <= self.before
}
}
#[derive(Debug, Clone, Default)]
pub struct Revocations {
owner: String,
list: Vec<MemberRevocation>,
}
impl Revocations {
pub fn new(owner: &str) -> Self {
Revocations { owner: owner.into(), list: vec![] }
}
pub fn add(&mut self, r: MemberRevocation) -> Result<()> {
if r.issuer_id != self.owner {
return Err(NetError::invalid("revocation", "not the owner's"));
}
r.verify()?;
if let Some(old) = self.list.iter_mut().find(|o| o.resource == r.resource && o.user_id == r.user_id) {
if r.before > old.before {
*old = r;
}
} else {
self.list.push(r);
}
Ok(())
}
pub fn extend(&mut self, rs: impl IntoIterator<Item = MemberRevocation>) -> Result<()> {
rs.into_iter().try_for_each(|r| self.add(r))
}
pub fn revokes(&self, m: &Membership) -> bool {
self.list.iter().any(|r| r.voids(m))
}
pub fn list(&self) -> &[MemberRevocation] {
&self.list
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Presented {
pub membership: Membership,
pub card: Card,
}
pub fn check_presented(
profile: &Profile,
p: &Presented,
peer: &PeerId,
owner: &str,
resource: &str,
revoked: &Revocations,
now: i64,
) -> Result<Membership> {
p.membership.verify_for(owner, resource, now)?;
if p.card.id != p.membership.user_id {
return Err(NetError::invalid("membership", "the card is someone else's"));
}
profile.check_card(&peer.to_string(), &p.card)?;
if revoked.revokes(&p.membership) {
return Err(NetError::invalid("membership", "revoked"));
}
Ok(p.membership.clone())
}
pub fn write_frame<W: std::io::Write>(w: &mut W, v: &impl Serialize) -> Result<()> {
let b = serde_json::to_vec(v).map_err(NetError::net)?;
w.write_all(&(b.len() as u32).to_be_bytes()).and_then(|_| w.write_all(&b)).and_then(|_| w.flush()).map_err(NetError::net)
}
pub fn read_frame<T: DeserializeOwned, R: std::io::Read>(r: &mut R) -> Result<T> {
let mut l = [0u8; 4];
r.read_exact(&mut l).map_err(NetError::net)?;
let l = u32::from_be_bytes(l) as usize;
if l > MAX_FRAME {
return Err(NetError::invalid("frame", "too large"));
}
let mut b = vec![0u8; l];
r.read_exact(&mut b).map_err(NetError::net)?;
serde_json::from_slice(&b).map_err(|e| NetError::invalid("frame", e.to_string()))
}
pub async fn write_frame_async<W: futures::AsyncWrite + Unpin>(w: &mut W, v: &impl Serialize) -> Result<()> {
use futures::AsyncWriteExt;
let b = serde_json::to_vec(v).map_err(NetError::net)?;
let mut f = (b.len() as u32).to_be_bytes().to_vec();
f.extend(b);
w.write_all(&f).await.map_err(NetError::net)?;
w.flush().await.map_err(NetError::net)
}
pub async fn read_frame_async<T: DeserializeOwned, R: futures::AsyncRead + Unpin>(r: &mut R) -> Result<T> {
use futures::AsyncReadExt;
let mut l = [0u8; 4];
r.read_exact(&mut l).await.map_err(NetError::net)?;
let l = u32::from_be_bytes(l) as usize;
if l > MAX_FRAME {
return Err(NetError::invalid("frame", "too large"));
}
let mut b = vec![0u8; l];
r.read_exact(&mut b).await.map_err(NetError::net)?;
serde_json::from_slice(&b).map_err(|e| NetError::invalid("frame", e.to_string()))
}
pub fn present<S: std::io::Write>(s: &mut S, p: &Presented) -> Result<()> {
write_frame(s, p)
}
pub fn read_presented<S: std::io::Read>(
s: &mut S,
profile: &Profile,
peer: &PeerId,
owner: &str,
resource: &str,
revoked: &Revocations,
now: i64,
) -> Result<Membership> {
let p: Presented = read_frame(s)?;
check_presented(profile, &p, peer, owner, resource, revoked, now)
}