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