use crate::keys::{PublicKey, Signature, SigningKey};
use crate::{Error, Hash32, Result, cbor};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use std::collections::BTreeSet;
use std::fmt;
pub const MAX_ENVELOPE_BYTES: usize = 1024 * 1024;
pub const MAX_SIGNATURES: usize = 64;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Domain {
Root,
Manifest,
Freshness,
Announcement,
}
impl Domain {
pub fn context(self) -> &'static str {
match self {
Self::Root => "weirdnet-update/v1/root",
Self::Manifest => "weirdnet-update/v1/manifest",
Self::Freshness => "weirdnet-update/v1/freshness",
Self::Announcement => "weirdnet-update/v1/announcement",
}
}
}
impl fmt::Display for Domain {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Root => "root",
Self::Manifest => "manifest",
Self::Freshness => "freshness",
Self::Announcement => "announcement",
})
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SignatureEntry {
pub key: PublicKey,
pub sig: Signature,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Signed {
pub domain: Domain,
#[serde(with = "serde_bytes")]
pub payload: Vec<u8>,
pub signatures: Vec<SignatureEntry>,
}
fn message(domain: Domain, payload: &[u8]) -> Vec<u8> {
let ctx = domain.context().as_bytes();
let mut msg = Vec::with_capacity(ctx.len() + 1 + payload.len());
msg.extend_from_slice(ctx);
msg.push(0);
msg.extend_from_slice(payload);
msg
}
impl Signed {
pub fn sign<T: Serialize>(domain: Domain, value: &T, keys: &[&SigningKey]) -> Result<Self> {
let mut env = Self { domain, payload: cbor::to_vec(value)?, signatures: Vec::new() };
for key in keys {
env.add_signature(key);
}
Ok(env)
}
pub fn add_signature(&mut self, key: &SigningKey) {
let public = key.public();
let sig = key.sign_raw(&message(self.domain, &self.payload));
self.signatures.retain(|e| e.key != public);
self.signatures.push(SignatureEntry { key: public, sig });
}
pub fn verify_threshold(&self, domain: Domain, trusted: &[PublicKey], threshold: usize) -> Result<()> {
if self.domain != domain {
return Err(Error::WrongDomain { expected: domain, found: self.domain });
}
if threshold == 0 {
return Err(Error::invalid("threshold", "must be at least 1"));
}
if self.signatures.len() > MAX_SIGNATURES {
return Err(Error::invalid("envelope", format!("more than {MAX_SIGNATURES} signatures")));
}
let msg = message(domain, &self.payload);
let mut good = BTreeSet::new();
for entry in &self.signatures {
if trusted.contains(&entry.key) && !good.contains(&entry.key) && entry.key.verify(&msg, &entry.sig) {
good.insert(entry.key);
}
}
if good.len() < threshold {
return Err(Error::Threshold { domain, found: good.len(), needed: threshold });
}
Ok(())
}
pub fn decode<T: DeserializeOwned>(&self) -> Result<T> {
cbor::from_slice(&self.payload)
}
pub fn payload_hash(&self) -> Hash32 {
Hash32::of(&self.payload)
}
pub fn to_bytes(&self) -> Result<Vec<u8>> {
cbor::to_vec(self)
}
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
if bytes.len() > MAX_ENVELOPE_BYTES {
return Err(Error::Decode(format!("envelope larger than {MAX_ENVELOPE_BYTES} bytes")));
}
let env: Self = cbor::from_slice(bytes)?;
if env.signatures.len() > MAX_SIGNATURES {
return Err(Error::Decode(format!("more than {MAX_SIGNATURES} signatures")));
}
Ok(env)
}
}