OXYGENHUB
# 67a4fe1aa2d5 oxygen / crates / oxy-core / src / repo / transparency.rs
8.6 KiB · Rust · last changed Fetch checks the host's transparency log (spec §14.8) 19 days ago source blame history raw
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<'_> {
31
    fn read_hashes(&self, indexes: &[u64]) -> std::result::Result<Vec<Hash>, TlogError> {
32
        let mut out = Vec::with_capacity(indexes.len());
33
        for &i in indexes {
34
            let (t, ..) = Tile::for_index(i);
35
            let key = (t.level, t.n);
36
            if !self.cache.borrow().contains_key(&key) {
37
                let held =
38
                    (self.size >> (tlog::TILE_HEIGHT * t.level)) - (t.n << tlog::TILE_HEIGHT);
39
                let tile = Tile {
40
                    width: held.min(tlog::TILE_WIDTH) as u32,
41
                    ..t
42
                };
43
                let data = http_get(&format!("{}/log/{}", self.hub, tile.path()), self.options)
44
                    .map_err(|e| TlogError::Read(e.to_string()))?;
45
                self.cache.borrow_mut().insert(key, (tile, data));
46
            }
47
            let cache = self.cache.borrow();
48
            let (tile, data) = &cache[&key];
49
            out.push(tile.hash_at(data, i)?);
50
        }
51
        Ok(out)
52
    }
53
}
54
55
/// What this user last saw of a hub's log.
56
struct Seen {
57
    identity: Identity,
58
    checkpoint: Checkpoint,
59
}
60
61
fn file_name(hub: &str) -> String {
62
    hub.chars()
63
        .map(|c| {
64
            if c.is_ascii_alphanumeric() || c == '.' || c == '-' {
65
                c
66
            } else {
67
                '_'
68
            }
69
        })
70
        .collect()
71
}
72
73
impl Repo {
74
    fn tlog_file(&self, hub: &str) -> PathBuf {
75
        self.user_config
76
            .parent()
77
            .map(Path::to_path_buf)
78
            .unwrap_or_else(default_home)
79
            .join("tlog")
80
            .join(file_name(hub))
81
    }
82
83
    fn seen_log(&self, hub: &str) -> Option<Seen> {
84
        let text = fs::read_to_string(self.tlog_file(hub)).ok()?;
85
        let (key, note) = text.split_once('\n')?;
86
        let identity = Identity::decode(&crate::dsse::base64_decode(key)?).ok()?;
87
        let note = Note::parse(note).ok()?;
88
        Some(Seen {
89
            checkpoint: Checkpoint::parse(&note.text).ok()?,
90
            identity,
91
        })
92
    }
93
94
    fn remember_log(&self, hub: &str, identity: &Identity, note: &Note) -> Result<()> {
95
        let path = self.tlog_file(hub);
96
        if let Some(dir) = path.parent() {
97
            fs::create_dir_all(dir)?;
98
        }
99
        let text = format!(
100
            "{}\n{}",
101
            crate::dsse::base64(&identity.encode()),
102
            note.encode()
103
        );
104
        crate::durable::write_atomic(&path, text.as_bytes())?;
105
        Ok(())
106
    }
107
108
    /// Check what a refs answer from `url` says about its host's log.
109
    /// Fails only when the host showed two histories (or a log key other
110
    /// than the one it showed before); everything else is a note.
111
    pub(crate) fn check_log(
112
        &self,
113
        url: &str,
114
        ca: Option<&Path>,
115
        resp: &RefsResponse,
116
    ) -> Result<Vec<String>> {
117
        let (Some(text), Some(hub)) = (&resp.log, hub_of(url)) else {
118
            return Ok(vec![]);
119
        };
120
        let mut notes = Vec::new();
121
        let unchecked = |why: String| {
122
            Ok(vec![format!(
123
                "{hub}'s transparency log was not checked: {why}"
124
            )])
125
        };
126
        let note = match Note::parse(text) {
127
            Ok(n) => n,
128
            Err(e) => return unchecked(e.to_string()),
129
        };
130
        let cp = match Checkpoint::parse(&note.text) {
131
            Ok(c) => c,
132
            Err(e) => return unchecked(e.to_string()),
133
        };
134
        let options = RemoteOptions {
135
            ca: ca.map(Path::to_path_buf),
136
            ..RemoteOptions::default()
137
        };
138
        let seen = self.seen_log(&hub);
139
        let identity = match &seen {
140
            Some(s) => s.identity.clone(),
141
            None => match http_get(&format!("{hub}/.well-known/oxyhub-key"), &options)
142
                .and_then(|b| Ok(Identity::decode(&b)?))
143
            {
144
                Ok(i) => i,
145
                Err(e) => return unchecked(format!("its key could not be fetched ({e})")),
146
            },
147
        };
148
        let two = |why: String| {
149
            fail(format!(
150
                "{hub} showed two histories: {why}. Do not trust what it serves; tell its operator. \
151
                 (What this user saw of its log is in {}.)",
152
                self.tlog_file(&hub).display()
153
            ))
154
        };
155
        if let Err(e) = note.verify(&cp.origin, &identity) {
156
            return match &seen {
157
                Some(_) => two(format!(
158
                    "its checkpoint is not signed by the log key it showed before ({e})"
159
                )),
160
                None => unchecked(e.to_string()),
161
            };
162
        }
163
        if let Some(s) = &seen {
164
            let old = &s.checkpoint;
165
            if old.origin != cp.origin {
166
                return two(format!("its log is now {}, not {}", cp.origin, old.origin));
167
            }
168
            if cp.size < old.size {
169
                return two(format!(
170
                    "its log has {} entries, fewer than the {} it showed before",
171
                    cp.size, old.size
172
                ));
173
            }
174
            if cp.size == old.size && cp.root != old.root {
175
                return two(format!("two different logs of {} entries", cp.size));
176
            }
177
            if cp.size > old.size && old.size > 0 {
178
                let tiles = Tiles {
179
                    hub: &hub,
180
                    size: cp.size,
181
                    options: &options,
182
                    cache: RefCell::new(HashMap::new()),
183
                };
184
                match tlog::prove_consistency(cp.size, old.size, &tiles).and_then(|p| {
185
                    tlog::check_consistency(&p, cp.size, &cp.root, old.size, &old.root)
186
                }) {
187
                    Ok(()) => {}
188
                    Err(TlogError::ProofFailed) | Err(TlogError::BadIndex(_)) => {
189
                        return two(format!(
190
                            "its log of {} entries does not extend the one of {} it showed before",
191
                            cp.size, old.size
192
                        ))
193
                    }
194
                    // Keep the older checkpoint: the next fetch tries again.
195
                    Err(e) => return unchecked(format!("its tiles could not be read ({e})")),
196
                }
197
            }
198
        }
199
        self.remember_log(&hub, &identity, &note)?;
200
        let mut missing = 0;
201
        for r in &resp.refs {
202
            let Some(a) = &r.attestation else { continue };
203
            let Some(l) = &r.logged else {
204
                missing += 1;
205
                continue;
206
            };
207
            let fits = Entry::decode(&l.entry).is_ok_and(|e| {
208
                e.kind == "ref" && e.object == a.attest.hash() && e.repo == a.attest.repo
209
            });
210
            let included = fits
211
                && tlog::check_inclusion(
212
                    &l.proof,
213
                    cp.size,
214
                    &cp.root,
215
                    l.index,
216
                    &tlog::leaf_hash(&l.entry),
217
                )
218
                .is_ok();
219
            if !included {
220
                notes.push(format!(
221
                    "{hub}'s log entry {} for {} does not prove its attestation is in the log",
222
                    l.index, r.name
223
                ));
224
            }
225
        }
226
        if missing > 0 {
227
            notes.push(format!(
228
                "{} of {hub}'s attestations {} not in its transparency log yet (logged within seconds of a push; checked on a later fetch)",
229
                missing,
230
                if missing == 1 { "is" } else { "are" }
231
            ));
232
        }
233
        Ok(notes)
234
    }
235
}