OXYGENHUB

Coverage / crates/oxy-core/src/repo/transparency.rs 3.7%

9 of 241 lines run, 2 of 38 functions · run #345 on proposal/125 · run missed

1
//! Checking a host's transparency log on fetch (spec §14.8, oxygenhub#238).
2
//!
3
//! A host that keeps a log sends its latest checkpoint with the refs, and
4
//! for each bookmark's attestation its entry and an inclusion proof. This
5
//! user remembers, per hub, the log's key and the last checkpoint seen
6
//! (`<oxy home>/tlog/<hub>`): the key on first sight, then never another.
7
//! Each new checkpoint must extend the last one, which oxy proves from the
8
//! hub's public tiles (C2SP tlog-tiles); one that does not means the hub
9
//! showed two histories, and the fetch fails. An attestation that is not
10
//! in the log yet, or whose proof does not verify, is only reported while
11
//! the log is new (oxygenhub docs/transparency.md, decision 3).
12
13
use super::*;
14
use crate::credentials::hub_of;
15
use crate::protocol::http_get;
16
use crate::tlog::{self, Checkpoint, Entry, Hash, HashReader, Note, Tile, TlogError};
17
use std::cell::RefCell;
18
19
/// Tiles fetched, by level and number, with the width fetched.
20
type TileCache = RefCell<HashMap<(u32, u64), (Tile, Vec<u8>)>>;
21
22
/// A hub's log as its tiles serve it, for a tree of `size` entries.
23
struct Tiles<'a> {
24
    hub: &'a str,
25
    size: u64,
26
    options: &'a RemoteOptions,
27
    cache: TileCache,
28
}
29
30
impl HashReader for Tiles<'_> {
310×
    fn read_hashes(&self, indexes: &[u64]) -> std::result::Result<Vec<Hash>, TlogError> {
320×
        let mut out = Vec::with_capacity(indexes.len());
330×
        for &i in indexes {
340×
            let (t, ..) = Tile::for_index(i);
350×
            let key = (t.level, t.n);
360×
            if !self.cache.borrow().contains_key(&key) {
370×
                let held =
380×
                    (self.size >> (tlog::TILE_HEIGHT * t.level)) - (t.n << tlog::TILE_HEIGHT);
390×
                let tile = Tile {
400×
                    width: held.min(tlog::TILE_WIDTH) as u32,
410×
                    ..t
420×
                };
430×
                let data = http_get(&format!("{}/log/{}", self.hub, tile.path()), self.options)
440×
                    .map_err(|e| TlogError::Read(e.to_string()))?;
450×
                self.cache.borrow_mut().insert(key, (tile, data));
460×
            }
470×
            let cache = self.cache.borrow();
480×
            let (tile, data) = &cache[&key];
490×
            out.push(tile.hash_at(data, i)?);
50
        }
510×
        Ok(out)
520×
    }
53
}
54
55
/// What this user last saw of a hub's log.
56
struct Seen {
57
    identity: Identity,
58
    checkpoint: Checkpoint,
59
}
60
610×
fn file_name(hub: &str) -> String {
620×
    hub.chars()
630×
        .map(|c| {
640×
            if c.is_ascii_alphanumeric() || c == '.' || c == '-' {
650×
                c
66
            } else {
670×
                '_'
68
            }
690×
        })
700×
        .collect()
710×
}
72
73
impl Repo {
740×
    fn tlog_file(&self, hub: &str) -> PathBuf {
750×
        self.user_config
760×
            .parent()
770×
            .map(Path::to_path_buf)
780×
            .unwrap_or_else(default_home)
790×
            .join("tlog")
800×
            .join(file_name(hub))
810×
    }
82
830×
    fn seen_log(&self, hub: &str) -> Option<Seen> {
840×
        let text = fs::read_to_string(self.tlog_file(hub)).ok()?;
850×
        let (key, note) = text.split_once('\n')?;
860×
        let identity = Identity::decode(&crate::dsse::base64_decode(key)?).ok()?;
870×
        let note = Note::parse(note).ok()?;
88
        Some(Seen {
890×
            checkpoint: Checkpoint::parse(&note.text).ok()?,
900×
            identity,
91
        })
920×
    }
93
940×
    fn remember_log(&self, hub: &str, identity: &Identity, note: &Note) -> Result<()> {
950×
        let path = self.tlog_file(hub);
960×
        if let Some(dir) = path.parent() {
970×
            fs::create_dir_all(dir)?;
980×
        }
990×
        let text = format!(
100
            "{}\n{}",
1010×
            crate::dsse::base64(&identity.encode()),
1020×
            note.encode()
103
        );
1040×
        crate::durable::write_atomic(&path, text.as_bytes())?;
1050×
        Ok(())
1060×
    }
107
108
    /// Take a checkpoint note `text` from `hub` as the newest seen, once it
109
    /// verifies with the hub's pinned log key and extends the last one
110
    /// seen. `Err` when the hub showed two histories; `Ok(Err(why))` when
111
    /// it could not be checked (the last one seen is kept).
1120×
    pub(crate) fn accept_checkpoint(
1130×
        &self,
1140×
        hub: &str,
1150×
        text: &str,
1160×
        options: &RemoteOptions,
1170×
    ) -> Result<std::result::Result<(Checkpoint, Vec<String>), String>> {
1180×
        let unchecked = |why: String| Ok(Err(why));
1190×
        let note = match Note::parse(text) {
1200×
            Ok(n) => n,
1210×
            Err(e) => return unchecked(e.to_string()),
122
        };
1230×
        let cp = match Checkpoint::parse(&note.text) {
1240×
            Ok(c) => c,
1250×
            Err(e) => return unchecked(e.to_string()),
126
        };
1270×
        let fetch_key = || {
1280×
            http_get(&format!("{hub}/.well-known/oxyhub-key"), options)
1290×
                .and_then(|b| Ok(Identity::decode(&b)?))
1300×
        };
1310×
        let mut notes = Vec::new();
1320×
        let mut seen = self.seen_log(hub);
1330×
        let mut identity = match &seen {
1340×
            Some(s) => s.identity.clone(),
1350×
            None => match fetch_key() {
1360×
                Ok(i) => i,
1370×
                Err(e) => return unchecked(format!("its key could not be fetched ({e})")),
138
            },
139
        };
1400×
        if let Err(e) = note.verify(&cp.origin, &identity) {
141
            // Signed by another key than the one pinned: a hub given a new
142
            // key, or one starting another log. Not proof of a fork, so a
143
            // loud note while logs are new (decision 3), and the new key is
144
            // pinned; a fork under one key is what fails.
1450×
            let Some(old) = &seen else {
1460×
                return unchecked(e.to_string());
147
            };
1480×
            match fetch_key() {
1490×
                Ok(new) if note.verify(&cp.origin, &new).is_ok() => {
1500×
                    notes.push(format!(
1510×
                        "{hub}'s log is signed by another key than before ({} now, {} then): if its operator did not announce a new key, it may be showing others a different log",
1520×
                        new.hash().short(12),
1530×
                        old.identity.hash().short(12)
1540×
                    ));
1550×
                    identity = new;
1560×
                    seen = None;
1570×
                }
1580×
                _ => return unchecked(format!("its checkpoint does not verify ({e})")),
159
            }
1600×
        }
1610×
        let two = |why: String| {
1620×
            fail(format!(
163
                "{hub} showed two histories: {why}. Do not trust what it serves; tell its operator. \
164
                 (What this user saw of its log is in {}.)",
1650×
                self.tlog_file(hub).display()
166
            ))
1670×
        };
1680×
        if let Some(s) = &seen {
1690×
            let old = &s.checkpoint;
1700×
            if old.origin != cp.origin {
1710×
                // A hub moved to another name keeps its key: a new log as
1720×
                // far as this user can tell, said so.
1730×
                notes.push(format!(
1740×
                    "{hub}'s log is now {}, not {}: checking it afresh",
1750×
                    cp.origin, old.origin
1760×
                ));
1770×
            } else if cp.size < old.size {
1780×
                return two(format!(
1790×
                    "its log has {} entries, fewer than the {} it showed before",
1800×
                    cp.size, old.size
1810×
                ));
1820×
            } else if cp.size == old.size && cp.root != old.root {
1830×
                return two(format!("two different logs of {} entries", cp.size));
1840×
            } else if cp.size > old.size && old.size > 0 {
1850×
                let tiles = Tiles {
1860×
                    hub,
1870×
                    size: cp.size,
1880×
                    options,
1890×
                    cache: RefCell::new(HashMap::new()),
1900×
                };
1910×
                match tlog::prove_consistency(cp.size, old.size, &tiles).and_then(|p| {
1920×
                    tlog::check_consistency(&p, cp.size, &cp.root, old.size, &old.root)
1930×
                }) {
1940×
                    Ok(()) => {}
195
                    Err(TlogError::ProofFailed) | Err(TlogError::BadIndex(_)) => {
1960×
                        return two(format!(
1970×
                            "its log of {} entries does not extend the one of {} it showed before",
1980×
                            cp.size, old.size
1990×
                        ))
200
                    }
201
                    // Keep the older checkpoint: the next fetch tries again.
2020×
                    Err(e) => return unchecked(format!("its tiles could not be read ({e})")),
203
                }
2040×
            }
2050×
        }
2060×
        self.remember_log(hub, &identity, &note)?;
2070×
        Ok(Ok((cp, notes)))
2080×
    }
209
210
    /// Check what a refs answer from `url` says about its host's log.
211
    /// Fails only when the host showed two histories under one key;
212
    /// everything else is a note.
213377×
    pub(crate) fn check_log(
214377×
        &self,
215377×
        url: &str,
216377×
        ca: Option<&Path>,
217377×
        resp: &RefsResponse,
218377×
    ) -> Result<Vec<String>> {
219377×
        let (Some(text), Some(hub)) = (&resp.log, hub_of(url)) else {
220377×
            return Ok(vec![]);
221
        };
2220×
        let options = RemoteOptions {
2230×
            ca: ca.map(Path::to_path_buf),
2240×
            ..RemoteOptions::default()
2250×
        };
2260×
        let (cp, mut notes) = match self.accept_checkpoint(&hub, text, &options)? {
2270×
            Ok(c) => c,
2280×
            Err(why) => {
2290×
                return Ok(vec![format!(
2300×
                    "{hub}'s transparency log was not checked: {why}"
2310×
                )])
232
            }
233
        };
2340×
        let mut missing = 0;
2350×
        for r in &resp.refs {
2360×
            let Some(a) = &r.attestation else { continue };
2370×
            let Some(l) = &r.logged else {
2380×
                missing += 1;
2390×
                continue;
240
            };
2410×
            let fits = Entry::decode(&l.entry).is_ok_and(|e| {
2420×
                e.kind == "ref" && e.object == a.attest.hash() && e.repo == a.attest.repo
2430×
            });
2440×
            let included = fits
2450×
                && tlog::check_inclusion(
2460×
                    &l.proof,
2470×
                    cp.size,
2480×
                    &cp.root,
2490×
                    l.index,
2500×
                    &tlog::leaf_hash(&l.entry),
2510×
                )
2520×
                .is_ok();
2530×
            if !included {
2540×
                notes.push(format!(
2550×
                    "{hub}'s log entry {} for {} does not prove its attestation is in the log",
2560×
                    l.index, r.name
2570×
                ));
2580×
            }
259
        }
2600×
        if missing > 0 {
2610×
            notes.push(format!(
262
                "{} of {hub}'s attestations {} not in its transparency log yet (logged within seconds of a push; checked on a later fetch)",
263
                missing,
2640×
                if missing == 1 { "is" } else { "are" }
265
            ));
2660×
        }
2670×
        Ok(notes)
268377×
    }
269
}
270
271
/// A hub's whole log, checked (`oxy tlog verify`).
272
#[derive(Clone, Debug)]
273
pub struct LogAudit {
274
    pub hub: String,
275
    pub checkpoint: Checkpoint,
276
    /// Every entry, numbered from 0.
277
    pub entries: Vec<Entry>,
278
    /// What accepting the checkpoint noted (a new log key, a new name).
279
    pub notes: Vec<String>,
280
}
281
282
impl Repo {
283
    /// Fetch the log of `url`'s hub whole: its checkpoint, accepted as a
284
    /// fetch accepts one, and every entry bundle, whose entries must make
285
    /// the root the checkpoint signs.
2860×
    pub fn audit_log(&self, url: &str, ca: Option<&Path>) -> Result<LogAudit> {
2870×
        let hub =
2880×
            hub_of(url).ok_or_else(|| RepoError::Msg(format!("{url} is not a hub's address")))?;
2890×
        let options = RemoteOptions {
2900×
            ca: ca.map(Path::to_path_buf),
2910×
            ..RemoteOptions::default()
2920×
        };
2930×
        let tl = |e: TlogError| RepoError::Msg(e.to_string());
2940×
        let text = String::from_utf8(http_get(&format!("{hub}/log/checkpoint"), &options)?)
2950×
            .map_err(|_| RepoError::Msg(format!("{hub}'s checkpoint is not text")))?;
2960×
        let (cp, notes) = match self.accept_checkpoint(&hub, &text, &options)? {
2970×
            Ok(c) => c,
2980×
            Err(why) => return fail(format!("{hub}'s log could not be checked: {why}")),
299
        };
3000×
        let mut entries = Vec::with_capacity(cp.size as usize);
3010×
        let mut stored: Vec<Hash> = Vec::new();
3020×
        let mut n = 0u64;
3030×
        while n < cp.size {
3040×
            let t = Tile {
3050×
                level: 0,
3060×
                n: n >> tlog::TILE_HEIGHT,
3070×
                width: (cp.size - n).min(tlog::TILE_WIDTH) as u32,
3080×
            };
3090×
            let bytes = http_get(&format!("{hub}/log/{}", t.entries_path()), &options)?;
3100×
            let bundle = tlog::decode_bundle(&bytes).map_err(tl)?;
3110×
            if bundle.len() != t.width as usize {
3120×
                return fail(format!(
313
                    "{hub}'s entry bundle {} holds {} entries, not {}",
3140×
                    t.entries_path(),
3150×
                    bundle.len(),
316
                    t.width
317
                ));
3180×
            }
3190×
            for b in bundle {
3200×
                let add =
3210×
                    tlog::stored_hashes_for_leaf(n, tlog::leaf_hash(&b), &stored).map_err(tl)?;
3220×
                stored.extend(add);
3230×
                entries.push(Entry::decode(&b).map_err(tl)?);
3240×
                n += 1;
325
            }
326
        }
3270×
        if tlog::tree_hash(cp.size, &stored).map_err(tl)? != cp.root {
3280×
            return fail(format!(
329
                "{hub} serves entries that do not make the root it signed for {} entries: its log is not the one it attests",
330
                cp.size
331
            ));
3320×
        }
3330×
        Ok(LogAudit {
3340×
            hub,
3350×
            checkpoint: cp,
3360×
            entries,
3370×
            notes,
3380×
        })
3390×
    }
340
}