//! The user's own trust store, one directory per project: //! //! /projects//project.toml feed, source, threshold, pinned rebuilders //! /projects//keys/.key each rebuilder's key file, as fetched when pinned //! //! ``: `--home`, `$QUORUM_HOME`, or the configuration directory //! (`%APPDATA%\quorum`, `~/.config/quorum`, `~/Library/Application Support/quorum`). //! //! The cached key files are what checks use, never what a feed serves later, //! so a change to a feed's keys can't add or swap a key you trust. `refresh` //! updates them only while every pinned fingerprint is still there. use std::fs; use std::path::{Path, PathBuf}; use anyhow::{Context, Result, bail}; use serde::{Deserialize, Serialize}; use crate::feed::FeedKind; pub const DEFAULT_THRESHOLD: usize = 3; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ProjectFile { pub feed: FeedKind, /// An https:// base URL, or a local directory. pub source: String, /// Infer the release from file names that start with this (`bitcoin-`). #[serde(default, skip_serializing_if = "Option::is_none")] pub release_prefix: Option, #[serde(default, skip_serializing_if = "Option::is_none")] pub threshold: Option, /// The project's TUF repository (https:// or a directory): which release /// is the latest. Its pinned and updated metadata lives in `tuf/`. #[serde(default, skip_serializing_if = "Option::is_none")] pub tuf: Option, /// The transparency log rebuilders' attestations must be in to count. #[serde(default, skip_serializing_if = "Option::is_none")] pub log: Option, #[serde(default, rename = "rebuilder")] pub rebuilders: Vec, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Rebuilder { /// Their name in the feed (a guix.sigs directory, say). pub name: String, /// Fingerprints of the keys that speak for them. pub fingerprints: Vec, } pub struct Project { pub name: String, pub dir: PathBuf, pub file: ProjectFile, } pub fn default_home() -> Result { if let Some(d) = std::env::var_os("QUORUM_HOME") { return Ok(PathBuf::from(d)); } Ok(dirs::config_dir() .context("no configuration directory on this system; pass --home")? .join("quorum")) } /// Names become file names and URL path segments: keep them plain. pub fn check_name(what: &str, name: &str) -> Result<()> { let ok = !name.is_empty() && name.len() <= 64 && name .chars() .all(|c| c.is_ascii_alphanumeric() || "-_.".contains(c)) && !name.starts_with('.'); if !ok { bail!("invalid {what} {name:?}"); } Ok(()) } fn projects_dir(home: &Path) -> PathBuf { home.join("projects") } pub fn list(home: &Path) -> Result> { let mut names = Vec::new(); match fs::read_dir(projects_dir(home)) { Ok(rd) => { for e in rd { let e = e?; if e.path().join("project.toml").is_file() { names.push(e.file_name().to_string_lossy().into_owned()); } } } Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} Err(e) => return Err(e.into()), } names.sort(); Ok(names) } impl Project { pub fn create(home: &Path, name: &str, file: ProjectFile) -> Result { check_name("project name", name)?; let dir = projects_dir(home).join(name); if dir.join("project.toml").exists() { bail!("project {name} already exists"); } let p = Project { name: name.into(), dir, file, }; p.save()?; Ok(p) } pub fn open(home: &Path, name: &str) -> Result { check_name("project name", name)?; let dir = projects_dir(home).join(name); let path = dir.join("project.toml"); let text = match fs::read_to_string(&path) { Ok(t) => t, Err(e) if e.kind() == std::io::ErrorKind::NotFound => { bail!("no project {name} (`quorum project list`)") } Err(e) => return Err(e).with_context(|| format!("reading {}", path.display())), }; let file: ProjectFile = toml::from_str(&text).with_context(|| format!("reading {}", path.display()))?; for r in &file.rebuilders { check_name("rebuilder name", &r.name)?; } Ok(Project { name: name.into(), dir, file, }) } pub fn save(&self) -> Result<()> { fs::create_dir_all(self.dir.join("keys"))?; let path = self.dir.join("project.toml"); let tmp = self.dir.join("project.toml.tmp"); fs::write(&tmp, toml::to_string_pretty(&self.file)?)?; fs::rename(&tmp, &path).with_context(|| format!("writing {}", path.display()))?; Ok(()) } pub fn threshold(&self) -> usize { self.file.threshold.unwrap_or(DEFAULT_THRESHOLD) } /// Trusted TUF metadata: the pinned root, then whatever updates verified. pub fn tuf_dir(&self) -> PathBuf { self.dir.join("tuf") } /// The log head last verified: the next one must extend it. pub fn log_state_path(&self) -> PathBuf { self.dir.join("log-state.json") } pub fn key_path(&self, rebuilder: &str) -> PathBuf { self.dir.join("keys").join(format!("{rebuilder}.key")) } pub fn read_key(&self, rebuilder: &str) -> Result> { let p = self.key_path(rebuilder); fs::read(&p).with_context(|| format!("reading {}", p.display())) } pub fn write_key(&self, rebuilder: &str, data: &[u8]) -> Result<()> { fs::create_dir_all(self.dir.join("keys"))?; fs::write(self.key_path(rebuilder), data)?; Ok(()) } pub fn rebuilder(&self, name: &str) -> Option<&Rebuilder> { self.file.rebuilders.iter().find(|r| r.name == name) } pub fn remove(home: &Path, name: &str) -> Result<()> { check_name("project name", name)?; let dir = projects_dir(home).join(name); if !dir.join("project.toml").exists() { bail!("no project {name}"); } fs::remove_dir_all(&dir)?; Ok(()) } }