| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | | use super::*; |
| 20 | | use crate::credentials::hub_of; |
| 21 | | use crate::protocol::http_get; |
| 22 | | use crate::tlog::{self, Checkpoint, Entry, Hash, HashReader, Integrated, Note, Tile, TlogError}; |
| 23 | | use std::cell::RefCell; |
| 24 | |
|
| 25 | |
|
| 26 | | type TileCache = RefCell<HashMap<(u32, u64), (Tile, Vec<u8>)>>; |
| 27 | |
|
| 28 | |
|
| 29 | | struct Tiles<'a> { |
| 30 | | hub: &'a str, |
| 31 | | size: u64, |
| 32 | | options: &'a RemoteOptions, |
| 33 | | cache: TileCache, |
| 34 | | } |
| 35 | |
|
| 36 | | impl HashReader for Tiles<'_> { |
| 37 | 0× | fn read_hashes(&self, indexes: &[u64]) -> std::result::Result<Vec<Hash>, TlogError> { |
| 38 | 0× | let mut out = Vec::with_capacity(indexes.len()); |
| 39 | 0× | for &i in indexes { |
| 40 | 0× | let (t, ..) = Tile::for_index(i); |
| 41 | 0× | let key = (t.level, t.n); |
| 42 | 0× | if !self.cache.borrow().contains_key(&key) { |
| 43 | 0× | let held = |
| 44 | 0× | (self.size >> (tlog::TILE_HEIGHT * t.level)) - (t.n << tlog::TILE_HEIGHT); |
| 45 | 0× | let tile = Tile { |
| 46 | 0× | width: held.min(tlog::TILE_WIDTH) as u32, |
| 47 | 0× | ..t |
| 48 | 0× | }; |
| 49 | 0× | let data = http_get(&format!("{}/log/{}", self.hub, tile.path()), self.options) |
| 50 | 0× | .map_err(|e| TlogError::Read(e.to_string()))?; |
| 51 | 0× | self.cache.borrow_mut().insert(key, (tile, data)); |
| 52 | 0× | } |
| 53 | 0× | let cache = self.cache.borrow(); |
| 54 | 0× | let (tile, data) = &cache[&key]; |
| 55 | 0× | out.push(tile.hash_at(data, i)?); |
| 56 | | } |
| 57 | 0× | Ok(out) |
| 58 | 0× | } |
| 59 | | } |
| 60 | |
|
| 61 | |
|
| 62 | | struct Seen { |
| 63 | | identity: Identity, |
| 64 | | checkpoint: Checkpoint, |
| 65 | | } |
| 66 | |
|
| 67 | 25× | fn file_name(hub: &str) -> String { |
| 68 | 25× | hub.chars() |
| 69 | 475× | .map(|c| { |
| 70 | 475× | if c.is_ascii_alphanumeric() || c == '.' || c == '-' { |
| 71 | 400× | c |
| 72 | | } else { |
| 73 | 75× | '_' |
| 74 | | } |
| 75 | 475× | }) |
| 76 | 25× | .collect() |
| 77 | 25× | } |
| 78 | |
|
| 79 | | impl Repo { |
| 80 | 17× | fn tlog_file(&self, hub: &str) -> PathBuf { |
| 81 | 17× | self.user_config |
| 82 | 17× | .parent() |
| 83 | 17× | .map(Path::to_path_buf) |
| 84 | 17× | .unwrap_or_else(default_home) |
| 85 | 17× | .join("tlog") |
| 86 | 17× | .join(file_name(hub)) |
| 87 | 17× | } |
| 88 | |
|
| 89 | 4× | fn seen_log(&self, hub: &str) -> Option<Seen> { |
| 90 | 4× | let text = fs::read_to_string(self.tlog_file(hub)).ok()?; |
| 91 | 4× | let (key, note) = text.split_once('\n')?; |
| 92 | 4× | let identity = Identity::decode(&crate::dsse::base64_decode(key)?).ok()?; |
| 93 | 4× | let note = Note::parse(note).ok()?; |
| 94 | | Some(Seen { |
| 95 | 4× | checkpoint: Checkpoint::parse(¬e.text).ok()?, |
| 96 | 4× | identity, |
| 97 | | }) |
| 98 | 4× | } |
| 99 | |
|
| 100 | 5× | pub(crate) fn remember_log(&self, hub: &str, identity: &Identity, note: &Note) -> Result<()> { |
| 101 | 5× | let path = self.tlog_file(hub); |
| 102 | 5× | if let Some(dir) = path.parent() { |
| 103 | 5× | fs::create_dir_all(dir)?; |
| 104 | 0× | } |
| 105 | 5× | let text = format!( |
| 106 | | "{}\n{}", |
| 107 | 5× | crate::dsse::base64(&identity.encode()), |
| 108 | 5× | note.encode() |
| 109 | | ); |
| 110 | 5× | crate::durable::write_atomic(&path, text.as_bytes())?; |
| 111 | 5× | Ok(()) |
| 112 | 5× | } |
| 113 | |
|
| 114 | | |
| 115 | | |
| 116 | | |
| 117 | | |
| 118 | 4× | pub(crate) fn accept_checkpoint( |
| 119 | 4× | &self, |
| 120 | 4× | hub: &str, |
| 121 | 4× | text: &str, |
| 122 | 4× | options: &RemoteOptions, |
| 123 | 4× | ) -> Result<std::result::Result<Accepted, String>> { |
| 124 | 4× | let unchecked = |why: String| Ok(Err(why)); |
| 125 | 4× | let note = match Note::parse(text) { |
| 126 | 4× | Ok(n) => n, |
| 127 | 0× | Err(e) => return unchecked(e.to_string()), |
| 128 | | }; |
| 129 | 4× | let cp = match Checkpoint::parse(¬e.text) { |
| 130 | 4× | Ok(c) => c, |
| 131 | 0× | Err(e) => return unchecked(e.to_string()), |
| 132 | | }; |
| 133 | 4× | let fetch_key = || { |
| 134 | 0× | http_get(&format!("{hub}/.well-known/oxyhub-key"), options) |
| 135 | 0× | .and_then(|b| Ok(Identity::decode(&b)?)) |
| 136 | 0× | }; |
| 137 | 4× | let mut notes = Vec::new(); |
| 138 | 4× | let mut seen = self.seen_log(hub); |
| 139 | 4× | let mut identity = match &seen { |
| 140 | 4× | Some(s) => s.identity.clone(), |
| 141 | 0× | None => match fetch_key() { |
| 142 | 0× | Ok(i) => i, |
| 143 | 0× | Err(e) => return unchecked(format!("its key could not be fetched ({e})")), |
| 144 | | }, |
| 145 | | }; |
| 146 | 4× | if let Err(e) = note.verify(&cp.origin, &identity) { |
| 147 | | |
| 148 | | |
| 149 | | |
| 150 | | |
| 151 | 0× | let Some(old) = &seen else { |
| 152 | 0× | return unchecked(e.to_string()); |
| 153 | | }; |
| 154 | 0× | match fetch_key() { |
| 155 | 0× | Ok(new) if note.verify(&cp.origin, &new).is_ok() => { |
| 156 | 0× | notes.push(format!( |
| 157 | 0× | "{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", |
| 158 | 0× | new.hash().short(12), |
| 159 | 0× | old.identity.hash().short(12) |
| 160 | 0× | )); |
| 161 | 0× | identity = new; |
| 162 | 0× | seen = None; |
| 163 | 0× | } |
| 164 | 0× | _ => return unchecked(format!("its checkpoint does not verify ({e})")), |
| 165 | | } |
| 166 | 4× | } |
| 167 | 4× | let two = |why: String| { |
| 168 | 0× | fail(format!( |
| 169 | | "{hub} showed two histories: {why}. Do not trust what it serves; tell its operator. \ |
| 170 | | (What this user saw of its log is in {}.)", |
| 171 | 0× | self.tlog_file(hub).display() |
| 172 | | )) |
| 173 | 0× | }; |
| 174 | 4× | if let Some(s) = &seen { |
| 175 | 4× | let old = &s.checkpoint; |
| 176 | 4× | if old.origin != cp.origin { |
| 177 | 0× | |
| 178 | 0× | |
| 179 | 0× | notes.push(format!( |
| 180 | 0× | "{hub}'s log is now {}, not {}: checking it afresh", |
| 181 | 0× | cp.origin, old.origin |
| 182 | 0× | )); |
| 183 | 4× | } else if cp.size < old.size { |
| 184 | 0× | return two(format!( |
| 185 | 0× | "its log has {} entries, fewer than the {} it showed before", |
| 186 | 0× | cp.size, old.size |
| 187 | 0× | )); |
| 188 | 4× | } else if cp.size == old.size && cp.root != old.root { |
| 189 | 0× | return two(format!("two different logs of {} entries", cp.size)); |
| 190 | 4× | } else if cp.size > old.size && old.size > 0 { |
| 191 | 0× | let tiles = Tiles { |
| 192 | 0× | hub, |
| 193 | 0× | size: cp.size, |
| 194 | 0× | options, |
| 195 | 0× | cache: RefCell::new(HashMap::new()), |
| 196 | 0× | }; |
| 197 | 0× | match tlog::prove_consistency(cp.size, old.size, &tiles).and_then(|p| { |
| 198 | 0× | tlog::check_consistency(&p, cp.size, &cp.root, old.size, &old.root) |
| 199 | 0× | }) { |
| 200 | 0× | Ok(()) => {} |
| 201 | | Err(TlogError::ProofFailed) | Err(TlogError::BadIndex(_)) => { |
| 202 | 0× | return two(format!( |
| 203 | 0× | "its log of {} entries does not extend the one of {} it showed before", |
| 204 | 0× | cp.size, old.size |
| 205 | 0× | )) |
| 206 | | } |
| 207 | | |
| 208 | 0× | Err(e) => return unchecked(format!("its tiles could not be read ({e})")), |
| 209 | | } |
| 210 | 4× | } |
| 211 | 0× | } |
| 212 | 4× | self.remember_log(hub, &identity, ¬e)?; |
| 213 | 4× | Ok(Ok(Accepted { |
| 214 | 4× | checkpoint: cp, |
| 215 | 4× | identity, |
| 216 | 4× | note, |
| 217 | 4× | notes, |
| 218 | 4× | })) |
| 219 | 4× | } |
| 220 | |
|
| 221 | 8× | fn witnesses_file(&self, hub: &str) -> PathBuf { |
| 222 | 8× | let mut p = self.tlog_file(hub); |
| 223 | 8× | p.set_file_name(format!("{}.witnesses", file_name(hub))); |
| 224 | 8× | p |
| 225 | 8× | } |
| 226 | |
|
| 227 | | |
| 228 | | |
| 229 | 4× | pub(crate) fn pinned_witnesses(&self, hub: &str) -> Vec<(String, Identity)> { |
| 230 | 4× | let Ok(text) = fs::read_to_string(self.witnesses_file(hub)) else { |
| 231 | 1× | return vec![]; |
| 232 | | }; |
| 233 | 3× | text.lines() |
| 234 | 3× | .filter_map(|l| { |
| 235 | 3× | let (name, key) = l.split_once(' ')?; |
| 236 | 3× | let id = Identity::decode(&crate::dsse::base64_decode(key)?).ok()?; |
| 237 | 3× | Some((name.to_string(), id)) |
| 238 | 3× | }) |
| 239 | 3× | .collect() |
| 240 | 4× | } |
| 241 | |
|
| 242 | | |
| 243 | | |
| 244 | | |
| 245 | | |
| 246 | 4× | pub(crate) fn pin_witnesses( |
| 247 | 4× | &self, |
| 248 | 4× | hub: &str, |
| 249 | 4× | listed: &[(String, Vec<u8>)], |
| 250 | 4× | notes: &mut Vec<String>, |
| 251 | 4× | ) -> Result<Vec<(String, Identity)>> { |
| 252 | 4× | let path = self.witnesses_file(hub); |
| 253 | 4× | let first = !path.exists(); |
| 254 | 4× | let mut pinned = self.pinned_witnesses(hub); |
| 255 | 4× | let mut changed = false; |
| 256 | 4× | for (name, bytes) in listed { |
| 257 | 3× | if name.is_empty() || name.contains(char::is_whitespace) { |
| 258 | 0× | notes.push(format!( |
| 259 | | "{hub} lists a witness named {name:?}, which cannot name one" |
| 260 | | )); |
| 261 | 0× | continue; |
| 262 | 3× | } |
| 263 | 3× | let Ok(id) = Identity::decode(bytes) else { |
| 264 | 0× | notes.push(format!( |
| 265 | | "{hub} lists witness {name} with a key that does not decode" |
| 266 | | )); |
| 267 | 0× | continue; |
| 268 | | }; |
| 269 | 3× | match pinned.iter_mut().find(|(n, _)| n == name) { |
| 270 | 2× | Some((_, old)) if old.hash() == id.hash() => {} |
| 271 | 2× | Some((_, old)) => { |
| 272 | 2× | notes.push(format!( |
| 273 | 2× | "{hub}'s witness {name} has another key than before ({} now, {} then): if its operator did not announce one, {hub} may be naming a witness of its own", |
| 274 | 2× | id.hash().short(12), |
| 275 | 2× | old.hash().short(12) |
| 276 | 2× | )); |
| 277 | 2× | *old = id; |
| 278 | 2× | changed = true; |
| 279 | 2× | } |
| 280 | | None => { |
| 281 | 1× | if !first { |
| 282 | 0× | notes.push(format!( |
| 283 | 0× | "{hub} is now cosigned by witness {name} (key {}), trusted from now on", |
| 284 | 0× | id.hash().short(12) |
| 285 | 0× | )); |
| 286 | 1× | } |
| 287 | 1× | pinned.push((name.clone(), id)); |
| 288 | 1× | changed = true; |
| 289 | | } |
| 290 | | } |
| 291 | | } |
| 292 | 4× | if changed { |
| 293 | 3× | if let Some(dir) = path.parent() { |
| 294 | 3× | fs::create_dir_all(dir)?; |
| 295 | 0× | } |
| 296 | 3× | let text: String = pinned |
| 297 | 3× | .iter() |
| 298 | 3× | .map(|(n, id)| format!("{n} {}\n", crate::dsse::base64(&id.encode()))) |
| 299 | 3× | .collect(); |
| 300 | 3× | crate::durable::write_atomic(&path, text.as_bytes())?; |
| 301 | 1× | } |
| 302 | 4× | Ok(pinned) |
| 303 | 4× | } |
| 304 | |
|
| 305 | | |
| 306 | | |
| 307 | | |
| 308 | | |
| 309 | 413× | pub(crate) fn check_log( |
| 310 | 413× | &self, |
| 311 | 413× | url: &str, |
| 312 | 413× | ca: Option<&Path>, |
| 313 | 413× | resp: &RefsResponse, |
| 314 | 413× | ) -> Result<LogCheck> { |
| 315 | 413× | let mut out = LogCheck::default(); |
| 316 | 413× | let (Some(text), Some(hub)) = (&resp.log, hub_of(url)) else { |
| 317 | 409× | return Ok(out); |
| 318 | | }; |
| 319 | 4× | out.kept = true; |
| 320 | 4× | let options = RemoteOptions { |
| 321 | 4× | ca: ca.map(Path::to_path_buf), |
| 322 | 4× | ..RemoteOptions::default() |
| 323 | 4× | }; |
| 324 | 4× | let accepted = match self.accept_checkpoint(&hub, text, &options)? { |
| 325 | 4× | Ok(c) => c, |
| 326 | 0× | Err(why) => { |
| 327 | 0× | out.notes |
| 328 | 0× | .push(format!("{hub}'s transparency log was not checked: {why}")); |
| 329 | 0× | out.unchecked = Some(why); |
| 330 | 0× | return Ok(out); |
| 331 | | } |
| 332 | | }; |
| 333 | 4× | let cp = &accepted.checkpoint; |
| 334 | 4× | out.notes.extend(accepted.notes.iter().cloned()); |
| 335 | 4× | let witnesses = self.pin_witnesses(&hub, &resp.witnesses, &mut out.notes)?; |
| 336 | 4× | let mut missing = 0; |
| 337 | | |
| 338 | 4× | let mut place = |name: &str, |
| 339 | | kind: &str, |
| 340 | | a: &SignedAttest, |
| 341 | | logged: Option<&crate::protocol::Logged>, |
| 342 | | integration: &[crate::protocol::Integration]| |
| 343 | 4× | -> std::result::Result<Integrated, String> { |
| 344 | 4× | let Some(l) = logged else { |
| 345 | 0× | missing += 1; |
| 346 | 0× | return Err("it is not in the log yet".into()); |
| 347 | | }; |
| 348 | 4× | let fits = Entry::decode(&l.entry).is_ok_and(|e| { |
| 349 | 4× | e.kind == kind && e.object == a.attest.hash() && e.repo == a.attest.repo |
| 350 | 4× | }); |
| 351 | 4× | let included = fits |
| 352 | 4× | && tlog::check_inclusion( |
| 353 | 4× | &l.proof, |
| 354 | 4× | cp.size, |
| 355 | 4× | &cp.root, |
| 356 | 4× | l.index, |
| 357 | 4× | &tlog::leaf_hash(&l.entry), |
| 358 | 4× | ) |
| 359 | 4× | .is_ok(); |
| 360 | 4× | if !included { |
| 361 | 0× | out.notes.push(format!( |
| 362 | 0× | "{hub}'s log entry {} for {name} does not prove its attestation is in the log", |
| 363 | 0× | l.index |
| 364 | 0× | )); |
| 365 | 4× | } |
| 366 | 4× | if !fits { |
| 367 | 0× | Err("its log entry is not its attestation's".to_string()) |
| 368 | 4× | } else if witnesses.is_empty() { |
| 369 | 0× | Err(format!("{hub} names no witness")) |
| 370 | | } else { |
| 371 | 4× | let cosigned: Vec<(&str, &[Hash])> = integration |
| 372 | 4× | .iter() |
| 373 | 4× | .map(|i| (i.note.as_str(), i.proof.as_slice())) |
| 374 | 4× | .collect(); |
| 375 | 4× | tlog::integrated_time( |
| 376 | 4× | &l.entry, |
| 377 | 4× | l.index, |
| 378 | 4× | &cosigned, |
| 379 | 4× | &cp.origin, |
| 380 | 4× | &accepted.identity, |
| 381 | 4× | &witnesses, |
| 382 | | ) |
| 383 | | } |
| 384 | 4× | }; |
| 385 | 4× | let mut times = HashMap::new(); |
| 386 | 4× | for r in &resp.refs { |
| 387 | 4× | let Some(a) = &r.attestation else { continue }; |
| 388 | 4× | let time = place(&r.name, "ref", a, r.logged.as_ref(), &r.integration); |
| 389 | 4× | times.insert(r.name.clone(), time); |
| 390 | | } |
| 391 | | |
| 392 | | |
| 393 | 4× | let mut tag_times = HashMap::new(); |
| 394 | 4× | for t in &resp.tags { |
| 395 | 0× | let Some(a) = &t.attestation else { continue }; |
| 396 | 0× | let name = format!("tags/{}", t.name); |
| 397 | 0× | let time = place(&name, "tag", a, t.logged.as_ref(), &t.integration); |
| 398 | 0× | tag_times.insert(t.name.clone(), time); |
| 399 | | } |
| 400 | 4× | out.times = times; |
| 401 | 4× | out.tag_times = tag_times; |
| 402 | 4× | if missing > 0 { |
| 403 | 0× | out.notes.push(format!( |
| 404 | | "{} of {hub}'s attestations {} not in its transparency log yet (logged within seconds of a push; checked on a later fetch)", |
| 405 | | missing, |
| 406 | 0× | if missing == 1 { "is" } else { "are" } |
| 407 | | )); |
| 408 | 4× | } |
| 409 | 4× | Ok(out) |
| 410 | 413× | } |
| 411 | | } |
| 412 | |
|
| 413 | |
|
| 414 | |
|
| 415 | | pub(crate) struct Accepted { |
| 416 | | pub checkpoint: Checkpoint, |
| 417 | | pub identity: Identity, |
| 418 | | pub note: Note, |
| 419 | | pub notes: Vec<String>, |
| 420 | | } |
| 421 | |
|
| 422 | |
|
| 423 | | #[derive(Debug, Default)] |
| 424 | | pub(crate) struct LogCheck { |
| 425 | | |
| 426 | | pub kept: bool, |
| 427 | | |
| 428 | | pub unchecked: Option<String>, |
| 429 | | pub notes: Vec<String>, |
| 430 | | |
| 431 | | pub times: HashMap<String, std::result::Result<Integrated, String>>, |
| 432 | | |
| 433 | | pub tag_times: HashMap<String, std::result::Result<Integrated, String>>, |
| 434 | | } |
| 435 | |
|
| 436 | | impl LogCheck { |
| 437 | | |
| 438 | 30× | pub fn time_of(&self, name: &str) -> std::result::Result<&Integrated, String> { |
| 439 | 30× | self.lookup(&self.times, name) |
| 440 | 30× | } |
| 441 | |
|
| 442 | | |
| 443 | 8× | pub fn tag_time_of(&self, name: &str) -> std::result::Result<&Integrated, String> { |
| 444 | 8× | self.lookup(&self.tag_times, name) |
| 445 | 8× | } |
| 446 | |
|
| 447 | 38× | fn lookup<'a>( |
| 448 | 38× | &self, |
| 449 | 38× | times: &'a HashMap<String, std::result::Result<Integrated, String>>, |
| 450 | 38× | name: &str, |
| 451 | 38× | ) -> std::result::Result<&'a Integrated, String> { |
| 452 | 38× | if let Some(why) = &self.unchecked { |
| 453 | 0× | return Err(format!("its host's log was not checked ({why})")); |
| 454 | 38× | } |
| 455 | 38× | if !self.kept { |
| 456 | 29× | return Err("its host keeps no transparency log".into()); |
| 457 | 9× | } |
| 458 | 9× | match times.get(name) { |
| 459 | 6× | Some(Ok(i)) => Ok(i), |
| 460 | 3× | Some(Err(why)) => Err(why.clone()), |
| 461 | 0× | None => Err("it has no log entry".into()), |
| 462 | | } |
| 463 | 38× | } |
| 464 | | } |
| 465 | |
|
| 466 | |
|
| 467 | |
|
| 468 | | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 469 | | pub(crate) enum Timed { |
| 470 | | |
| 471 | | Integrated, |
| 472 | | |
| 473 | | |
| 474 | | Fallback, |
| 475 | | |
| 476 | | |
| 477 | | Disagrees, |
| 478 | | |
| 479 | | Refused, |
| 480 | | } |
| 481 | |
|
| 482 | |
|
| 483 | |
|
| 484 | 33× | pub(crate) fn judge( |
| 485 | 33× | integrated: Option<u64>, |
| 486 | 33× | at: impl Fn(u64) -> bool, |
| 487 | 33× | before: impl FnOnce() -> bool, |
| 488 | 33× | ) -> Timed { |
| 489 | 5× | match integrated { |
| 490 | 5× | Some(t) if at(t) => Timed::Integrated, |
| 491 | | Some(_) => { |
| 492 | 2× | if before() { |
| 493 | 1× | Timed::Disagrees |
| 494 | | } else { |
| 495 | 1× | Timed::Refused |
| 496 | | } |
| 497 | | } |
| 498 | | None => { |
| 499 | 28× | if before() { |
| 500 | 28× | Timed::Fallback |
| 501 | | } else { |
| 502 | 0× | Timed::Refused |
| 503 | | } |
| 504 | | } |
| 505 | | } |
| 506 | 33× | } |
| 507 | |
|
| 508 | |
|
| 509 | |
|
| 510 | 4× | pub(crate) fn gap_note(claimed: u64, integrated: &Integrated) -> Option<String> { |
| 511 | 4× | let t = integrated.ms(); |
| 512 | 4× | let minutes = |d: u64| d.div_ceil(60_000); |
| 513 | 4× | if claimed.saturating_add(tlog::TIME_TOLERANCE_MS) < t { |
| 514 | 1× | Some(format!( |
| 515 | 1× | "claims to be from {} minutes before witness {} first saw it: backdated, held back before it was logged, or signed on a slow clock", |
| 516 | 1× | minutes(t - claimed), |
| 517 | 1× | integrated.witness |
| 518 | 1× | )) |
| 519 | 3× | } else if claimed > t.saturating_add(tlog::TIME_TOLERANCE_MS) { |
| 520 | 1× | Some(format!( |
| 521 | 1× | "claims to be from {} minutes after witness {} saw it: signed on a fast clock", |
| 522 | 1× | minutes(claimed - t), |
| 523 | 1× | integrated.witness |
| 524 | 1× | )) |
| 525 | | } else { |
| 526 | 2× | None |
| 527 | | } |
| 528 | 4× | } |
| 529 | |
|
| 530 | |
|
| 531 | | #[derive(Clone, Debug)] |
| 532 | | pub struct LogAudit { |
| 533 | | pub hub: String, |
| 534 | | pub checkpoint: Checkpoint, |
| 535 | | |
| 536 | | pub entries: Vec<Entry>, |
| 537 | | |
| 538 | | pub notes: Vec<String>, |
| 539 | | |
| 540 | | |
| 541 | | pub witnessed: Vec<(String, u64)>, |
| 542 | | } |
| 543 | |
|
| 544 | | impl Repo { |
| 545 | | |
| 546 | | |
| 547 | | |
| 548 | 0× | pub fn audit_log(&self, url: &str, ca: Option<&Path>) -> Result<LogAudit> { |
| 549 | 0× | let hub = |
| 550 | 0× | hub_of(url).ok_or_else(|| RepoError::Msg(format!("{url} is not a hub's address")))?; |
| 551 | 0× | let options = RemoteOptions { |
| 552 | 0× | ca: ca.map(Path::to_path_buf), |
| 553 | 0× | ..RemoteOptions::default() |
| 554 | 0× | }; |
| 555 | 0× | let tl = |e: TlogError| RepoError::Msg(e.to_string()); |
| 556 | 0× | let text = String::from_utf8(http_get(&format!("{hub}/log/checkpoint"), &options)?) |
| 557 | 0× | .map_err(|_| RepoError::Msg(format!("{hub}'s checkpoint is not text")))?; |
| 558 | 0× | let accepted = match self.accept_checkpoint(&hub, &text, &options)? { |
| 559 | 0× | Ok(c) => c, |
| 560 | 0× | Err(why) => return fail(format!("{hub}'s log could not be checked: {why}")), |
| 561 | | }; |
| 562 | 0× | let mut notes = accepted.notes; |
| 563 | | |
| 564 | | |
| 565 | | |
| 566 | 0× | let listed: Vec<(String, Vec<u8>)> = http_get(&format!("{hub}/log/witnesses"), &options) |
| 567 | 0× | .ok() |
| 568 | 0× | .and_then(|b| String::from_utf8(b).ok()) |
| 569 | 0× | .map(|t| { |
| 570 | 0× | t.lines() |
| 571 | 0× | .filter_map(|l| { |
| 572 | 0× | let (n, k) = l.split_once(' ')?; |
| 573 | 0× | Some((n.to_string(), crate::dsse::base64_decode(k.trim())?)) |
| 574 | 0× | }) |
| 575 | 0× | .collect() |
| 576 | 0× | }) |
| 577 | 0× | .unwrap_or_default(); |
| 578 | 0× | let witnesses = self.pin_witnesses(&hub, &listed, &mut notes)?; |
| 579 | 0× | let mut witnessed = accepted.note.cosignatures(&witnesses); |
| 580 | 0× | witnessed.sort_by_key(|(_, t)| *t); |
| 581 | 0× | let cp = accepted.checkpoint; |
| 582 | 0× | let mut entries = Vec::with_capacity(cp.size as usize); |
| 583 | 0× | let mut stored: Vec<Hash> = Vec::new(); |
| 584 | 0× | let mut n = 0u64; |
| 585 | 0× | while n < cp.size { |
| 586 | 0× | let t = Tile { |
| 587 | 0× | level: 0, |
| 588 | 0× | n: n >> tlog::TILE_HEIGHT, |
| 589 | 0× | width: (cp.size - n).min(tlog::TILE_WIDTH) as u32, |
| 590 | 0× | }; |
| 591 | 0× | let bytes = http_get(&format!("{hub}/log/{}", t.entries_path()), &options)?; |
| 592 | 0× | let bundle = tlog::decode_bundle(&bytes).map_err(tl)?; |
| 593 | 0× | if bundle.len() != t.width as usize { |
| 594 | 0× | return fail(format!( |
| 595 | | "{hub}'s entry bundle {} holds {} entries, not {}", |
| 596 | 0× | t.entries_path(), |
| 597 | 0× | bundle.len(), |
| 598 | | t.width |
| 599 | | )); |
| 600 | 0× | } |
| 601 | 0× | for b in bundle { |
| 602 | 0× | let add = |
| 603 | 0× | tlog::stored_hashes_for_leaf(n, tlog::leaf_hash(&b), &stored).map_err(tl)?; |
| 604 | 0× | stored.extend(add); |
| 605 | 0× | entries.push(Entry::decode(&b).map_err(tl)?); |
| 606 | 0× | n += 1; |
| 607 | | } |
| 608 | | } |
| 609 | 0× | if tlog::tree_hash(cp.size, &stored).map_err(tl)? != cp.root { |
| 610 | 0× | return fail(format!( |
| 611 | | "{hub} serves entries that do not make the root it signed for {} entries: its log is not the one it attests", |
| 612 | | cp.size |
| 613 | | )); |
| 614 | 0× | } |
| 615 | 0× | Ok(LogAudit { |
| 616 | 0× | hub, |
| 617 | 0× | checkpoint: cp, |
| 618 | 0× | entries, |
| 619 | 0× | notes, |
| 620 | 0× | witnessed, |
| 621 | 0× | }) |
| 622 | 0× | } |
| 623 | | } |