use super::{Hash, hex, unhex};
use crate::error::{NetError, Result};
use crate::member::MemberRevocation;
use crate::user::{decode_public, user_id};
use libp2p::identity::Keypair;
use serde::{Deserialize, Serialize};
const DOMAIN: &str = "weirdnet/content/manifest/1";
const CLOCK_SKEW_MS: i64 = 5 * 60 * 1000;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ManifestEntry {
pub path: String,
pub root: String,
pub size: u64,
#[serde(default, with = "serde_bytes_vec")]
pub meta: Vec<u8>,
}
#[derive(Debug, Clone, Default)]
pub struct ManifestDraft {
pub id: String,
pub version: u64,
pub entries: Vec<ManifestEntry>,
pub revoked: Vec<String>,
pub member_revocations: Vec<MemberRevocation>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Manifest {
pub v: u8,
pub id: String,
pub version: u64,
pub user_id: String,
pub user_key: Vec<u8>,
pub entries: Vec<ManifestEntry>,
pub revoked: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub member_revocations: Vec<MemberRevocation>,
pub issued_at: i64,
pub sig: Vec<u8>,
}
mod serde_bytes_vec {
use serde::{Deserialize, Deserializer, Serializer};
pub fn serialize<S: Serializer>(v: &[u8], s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&super::hex(v))
}
pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
let s = String::deserialize(d)?;
super::unhex(&s).ok_or_else(|| serde::de::Error::custom("not hex"))
}
}
fn check_path(p: &str) -> bool {
!p.is_empty()
&& p.len() <= 4096
&& !p.starts_with('/')
&& !p.contains('\\')
&& p.split('/').all(|c| !c.is_empty() && c != "." && c != ".." && !c.contains('\0'))
}
impl Manifest {
fn body(&self) -> Vec<u8> {
let mut m = self.clone();
m.sig = vec![];
let mut v = DOMAIN.as_bytes().to_vec();
v.push(b'\n');
v.extend(serde_json::to_vec(&m).unwrap_or_default());
v
}
pub fn sign(user: &Keypair, draft: ManifestDraft, now: i64) -> Result<Manifest> {
let ManifestDraft { id, version, mut entries, revoked, member_revocations } = draft;
entries.sort_by(|a, b| a.path.cmp(&b.path));
let mut m = Manifest {
v: 1,
id,
version,
user_id: user_id(&user.public()),
user_key: user.public().encode_protobuf(),
entries,
revoked,
member_revocations,
issued_at: now,
sig: vec![],
};
m.check_fields()?;
m.sig = user.sign(&m.body()).map_err(|e| NetError::Crypto(format!("manifest: {e}")))?;
Ok(m)
}
fn check_fields(&self) -> Result<()> {
let bad = |m: String| NetError::invalid("manifest", m);
if self.v != 1 {
return Err(bad("unknown version".into()));
}
if self.id.is_empty() || self.id.len() > 256 {
return Err(bad("id: 1 to 256 bytes".into()));
}
let mut last: Option<&str> = None;
for e in &self.entries {
if !check_path(&e.path) {
return Err(bad(format!("bad path `{}`", e.path)));
}
if last.is_some_and(|l| l >= e.path.as_str()) {
return Err(bad(format!("`{}` repeated or out of order", e.path)));
}
last = Some(&e.path);
if unhex(&e.root).is_none_or(|r| r.len() != 32) {
return Err(bad(format!("`{}`: bad root", e.path)));
}
}
for r in &self.member_revocations {
if r.issuer_id != self.user_id {
return Err(bad("a member revocation is not the owner's".into()));
}
r.verify()?;
}
if self.revoked.iter().any(|r| unhex(r).is_none_or(|r| r.len() != 32)) {
return Err(bad("bad revoked root".into()));
}
Ok(())
}
pub fn verify(&self, now: i64) -> Result<()> {
self.check_fields()?;
let key = decode_public(&self.user_key)?;
if user_id(&key) != self.user_id {
return Err(NetError::invalid("manifest", "the user id does not match the key"));
}
if self.issued_at > now + CLOCK_SKEW_MS {
return Err(NetError::invalid("manifest", "from the future"));
}
if !key.verify(&self.body(), &self.sig) {
return Err(NetError::invalid("manifest", "bad signature"));
}
Ok(())
}
pub fn supersedes(&self, older: &Manifest) -> bool {
self.id == older.id && self.user_id == older.user_id && self.version > older.version
}
pub fn entry(&self, path: &str) -> Option<&ManifestEntry> {
self.entries.binary_search_by(|e| e.path.as_str().cmp(path)).ok().map(|i| &self.entries[i])
}
pub fn is_revoked(&self, root: &Hash) -> bool {
let h = hex(root);
self.revoked.contains(&h)
}
}