mod carrier;
#[cfg(test)]
mod tests;
#[cfg(feature = "wire")]
pub mod wire;
pub use carrier::{Carrier, Offered, Topic};
use crate::envelope::{Domain, Signed};
use crate::ids::check_id;
use crate::keys::SigningKey;
use crate::manifest::UpdateManifest;
use crate::root::{Root, RootPin};
use crate::{Error, Hash32, Result, Timestamp};
use serde::{Deserialize, Serialize};
pub const MAX_ANNOUNCEMENT_BYTES: usize = 8 * 1024;
pub const MAX_LIFETIME_SECS: u64 = 30 * 86_400;
pub const MAX_SOURCES: usize = 8;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Announcement {
pub org: String,
pub pin: RootPin,
pub app: String,
pub channel: String,
pub update_id: String,
pub seq: u64,
pub manifest_hash: Hash32,
pub published: Timestamp,
pub expires: Timestamp,
#[serde(default)]
pub security: bool,
#[serde(default)]
pub sources: Vec<String>,
}
impl Announcement {
pub fn for_manifest(org: &str, pin: RootPin, manifest: &Signed, published: Timestamp, lifetime_secs: u64, sources: Vec<String>) -> Result<Self> {
if manifest.domain != Domain::Manifest {
return Err(Error::WrongDomain { expected: Domain::Manifest, found: manifest.domain });
}
let m: UpdateManifest = manifest.decode()?;
let a = Self {
org: org.into(),
pin,
app: m.app.clone(),
channel: m.channel.clone(),
update_id: m.update_id.clone(),
seq: m.seq,
manifest_hash: manifest.payload_hash(),
published,
expires: published.plus(lifetime_secs.min(MAX_LIFETIME_SECS)).min(m.expires),
security: m.labels.security.is_some(),
sources,
};
a.validate()?;
Ok(a)
}
pub fn validate(&self) -> Result<()> {
check_id("org", &self.org)?;
check_id("app", &self.app)?;
check_id("channel", &self.channel)?;
check_id("update id", &self.update_id)?;
if self.expires <= self.published || self.expires.0 - self.published.0 > MAX_LIFETIME_SECS {
return Err(Error::invalid("announcement", format!("must expire within {MAX_LIFETIME_SECS} s of publishing")));
}
if self.sources.len() > MAX_SOURCES {
return Err(Error::invalid("announcement", format!("more than {MAX_SOURCES} sources")));
}
for s in &self.sources {
let ok = (s.starts_with("https://") || s.starts_with("weirdnet:")) && s.len() <= 512 && !s.chars().any(char::is_control);
if !ok {
return Err(Error::invalid("announcement source", "https:// URLs or weirdnet: ids, up to 512 bytes"));
}
}
Ok(())
}
pub fn sign(&self, keys: &[&SigningKey]) -> Result<Signed> {
self.validate()?;
let env = Signed::sign(Domain::Announcement, self, keys)?;
if env.to_bytes()?.len() > MAX_ANNOUNCEMENT_BYTES {
return Err(Error::invalid("announcement", format!("larger than {MAX_ANNOUNCEMENT_BYTES} bytes")));
}
Ok(env)
}
pub fn verify(env: &Signed, pin: RootPin, root: &Root, now: Timestamp) -> Result<Self> {
if env.domain != Domain::Announcement {
return Err(Error::WrongDomain { expected: Domain::Announcement, found: env.domain });
}
if env.payload.len() > MAX_ANNOUNCEMENT_BYTES {
return Err(Error::Decode("announcement too large".into()));
}
let claimed: Announcement = env.decode()?;
let keys = root.release_keys_for(&claimed.app, &claimed.channel, now);
if keys.is_empty() {
return Err(Error::NotDelegated(format!("{}/{}", claimed.app, claimed.channel)));
}
env.verify_threshold(Domain::Announcement, &keys, root.release_threshold.into())?;
claimed.validate()?;
if claimed.pin != pin || claimed.org != root.org {
return Err(Error::PinMismatch);
}
root.check_live(now)?;
if now < claimed.published.minus(300) || now >= claimed.expires {
return Err(Error::Expired(Domain::Announcement));
}
Ok(claimed)
}
pub fn names(&self, manifest: &Signed) -> bool {
manifest.domain == Domain::Manifest && manifest.payload_hash() == self.manifest_hash
}
}