| 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 | | |
| 112 | 0× | pub(crate) fn accept_checkpoint( |
| 113 | 0× | &self, |
| 114 | 0× | hub: &str, |
| 115 | 0× | text: &str, |
| 116 | 0× | options: &RemoteOptions, |
| 117 | 0× | ) -> Result<std::result::Result<(Checkpoint, Vec<String>), String>> { |
| 118 | 0× | let unchecked = |why: String| Ok(Err(why)); |
| 119 | 0× | let note = match Note::parse(text) { |
| 120 | 0× | Ok(n) => n, |
| 121 | 0× | Err(e) => return unchecked(e.to_string()), |
| 122 | | }; |
| 123 | 0× | let cp = match Checkpoint::parse(¬e.text) { |
| 124 | 0× | Ok(c) => c, |
| 125 | 0× | Err(e) => return unchecked(e.to_string()), |
| 126 | | }; |
| 127 | 0× | let fetch_key = || { |
| 128 | 0× | http_get(&format!("{hub}/.well-known/oxyhub-key"), options) |
| 129 | 0× | .and_then(|b| Ok(Identity::decode(&b)?)) |
| 130 | 0× | }; |
| 131 | 0× | let mut notes = Vec::new(); |
| 132 | 0× | let mut seen = self.seen_log(hub); |
| 133 | 0× | let mut identity = match &seen { |
| 134 | 0× | Some(s) => s.identity.clone(), |
| 135 | 0× | None => match fetch_key() { |
| 136 | 0× | Ok(i) => i, |
| 137 | 0× | Err(e) => return unchecked(format!("its key could not be fetched ({e})")), |
| 138 | | }, |
| 139 | | }; |
| 140 | 0× | if let Err(e) = note.verify(&cp.origin, &identity) { |
| 141 | | |
| 142 | | |
| 143 | | |
| 144 | | |
| 145 | 0× | let Some(old) = &seen else { |
| 146 | 0× | return unchecked(e.to_string()); |
| 147 | | }; |
| 148 | 0× | match fetch_key() { |
| 149 | 0× | Ok(new) if note.verify(&cp.origin, &new).is_ok() => { |
| 150 | 0× | notes.push(format!( |
| 151 | 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", |
| 152 | 0× | new.hash().short(12), |
| 153 | 0× | old.identity.hash().short(12) |
| 154 | 0× | )); |
| 155 | 0× | identity = new; |
| 156 | 0× | seen = None; |
| 157 | 0× | } |
| 158 | 0× | _ => return unchecked(format!("its checkpoint does not verify ({e})")), |
| 159 | | } |
| 160 | 0× | } |
| 161 | 0× | let two = |why: String| { |
| 162 | 0× | 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 {}.)", |
| 165 | 0× | self.tlog_file(hub).display() |
| 166 | | )) |
| 167 | 0× | }; |
| 168 | 0× | if let Some(s) = &seen { |
| 169 | 0× | let old = &s.checkpoint; |
| 170 | 0× | if old.origin != cp.origin { |
| 171 | 0× | |
| 172 | 0× | |
| 173 | 0× | notes.push(format!( |
| 174 | 0× | "{hub}'s log is now {}, not {}: checking it afresh", |
| 175 | 0× | cp.origin, old.origin |
| 176 | 0× | )); |
| 177 | 0× | } else if cp.size < old.size { |
| 178 | 0× | return two(format!( |
| 179 | 0× | "its log has {} entries, fewer than the {} it showed before", |
| 180 | 0× | cp.size, old.size |
| 181 | 0× | )); |
| 182 | 0× | } else if cp.size == old.size && cp.root != old.root { |
| 183 | 0× | return two(format!("two different logs of {} entries", cp.size)); |
| 184 | 0× | } else if cp.size > old.size && old.size > 0 { |
| 185 | 0× | let tiles = Tiles { |
| 186 | 0× | hub, |
| 187 | 0× | size: cp.size, |
| 188 | 0× | options, |
| 189 | 0× | cache: RefCell::new(HashMap::new()), |
| 190 | 0× | }; |
| 191 | 0× | match tlog::prove_consistency(cp.size, old.size, &tiles).and_then(|p| { |
| 192 | 0× | tlog::check_consistency(&p, cp.size, &cp.root, old.size, &old.root) |
| 193 | 0× | }) { |
| 194 | 0× | Ok(()) => {} |
| 195 | | Err(TlogError::ProofFailed) | Err(TlogError::BadIndex(_)) => { |
| 196 | 0× | return two(format!( |
| 197 | 0× | "its log of {} entries does not extend the one of {} it showed before", |
| 198 | 0× | cp.size, old.size |
| 199 | 0× | )) |
| 200 | | } |
| 201 | | |
| 202 | 0× | Err(e) => return unchecked(format!("its tiles could not be read ({e})")), |
| 203 | | } |
| 204 | 0× | } |
| 205 | 0× | } |
| 206 | 0× | self.remember_log(hub, &identity, ¬e)?; |
| 207 | 0× | Ok(Ok((cp, notes))) |
| 208 | 0× | } |
| 209 | |
|
| 210 | | |
| 211 | | |
| 212 | | |
| 213 | 365× | pub(crate) fn check_log( |
| 214 | 365× | &self, |
| 215 | 365× | url: &str, |
| 216 | 365× | ca: Option<&Path>, |
| 217 | 365× | resp: &RefsResponse, |
| 218 | 365× | ) -> Result<Vec<String>> { |
| 219 | 365× | let (Some(text), Some(hub)) = (&resp.log, hub_of(url)) else { |
| 220 | 365× | return Ok(vec![]); |
| 221 | | }; |
| 222 | 0× | let options = RemoteOptions { |
| 223 | 0× | ca: ca.map(Path::to_path_buf), |
| 224 | 0× | ..RemoteOptions::default() |
| 225 | 0× | }; |
| 226 | 0× | let (cp, mut notes) = match self.accept_checkpoint(&hub, text, &options)? { |
| 227 | 0× | Ok(c) => c, |
| 228 | 0× | Err(why) => { |
| 229 | 0× | return Ok(vec![format!( |
| 230 | 0× | "{hub}'s transparency log was not checked: {why}" |
| 231 | 0× | )]) |
| 232 | | } |
| 233 | | }; |
| 234 | 0× | let mut missing = 0; |
| 235 | 0× | for r in &resp.refs { |
| 236 | 0× | let Some(a) = &r.attestation else { continue }; |
| 237 | 0× | let Some(l) = &r.logged else { |
| 238 | 0× | missing += 1; |
| 239 | 0× | continue; |
| 240 | | }; |
| 241 | 0× | let fits = Entry::decode(&l.entry).is_ok_and(|e| { |
| 242 | 0× | e.kind == "ref" && e.object == a.attest.hash() && e.repo == a.attest.repo |
| 243 | 0× | }); |
| 244 | 0× | let included = fits |
| 245 | 0× | && tlog::check_inclusion( |
| 246 | 0× | &l.proof, |
| 247 | 0× | cp.size, |
| 248 | 0× | &cp.root, |
| 249 | 0× | l.index, |
| 250 | 0× | &tlog::leaf_hash(&l.entry), |
| 251 | 0× | ) |
| 252 | 0× | .is_ok(); |
| 253 | 0× | if !included { |
| 254 | 0× | notes.push(format!( |
| 255 | 0× | "{hub}'s log entry {} for {} does not prove its attestation is in the log", |
| 256 | 0× | l.index, r.name |
| 257 | 0× | )); |
| 258 | 0× | } |
| 259 | | } |
| 260 | 0× | if missing > 0 { |
| 261 | 0× | 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, |
| 264 | 0× | if missing == 1 { "is" } else { "are" } |
| 265 | | )); |
| 266 | 0× | } |
| 267 | 0× | Ok(notes) |
| 268 | 365× | } |
| 269 | | } |
| 270 | |
|
| 271 | |
|
| 272 | | #[derive(Clone, Debug)] |
| 273 | | pub struct LogAudit { |
| 274 | | pub hub: String, |
| 275 | | pub checkpoint: Checkpoint, |
| 276 | | |
| 277 | | pub entries: Vec<Entry>, |
| 278 | | |
| 279 | | pub notes: Vec<String>, |
| 280 | | } |
| 281 | |
|
| 282 | | impl Repo { |
| 283 | | |
| 284 | | |
| 285 | | |
| 286 | 0× | pub fn audit_log(&self, url: &str, ca: Option<&Path>) -> Result<LogAudit> { |
| 287 | 0× | let hub = |
| 288 | 0× | hub_of(url).ok_or_else(|| RepoError::Msg(format!("{url} is not a hub's address")))?; |
| 289 | 0× | let options = RemoteOptions { |
| 290 | 0× | ca: ca.map(Path::to_path_buf), |
| 291 | 0× | ..RemoteOptions::default() |
| 292 | 0× | }; |
| 293 | 0× | let tl = |e: TlogError| RepoError::Msg(e.to_string()); |
| 294 | 0× | let text = String::from_utf8(http_get(&format!("{hub}/log/checkpoint"), &options)?) |
| 295 | 0× | .map_err(|_| RepoError::Msg(format!("{hub}'s checkpoint is not text")))?; |
| 296 | 0× | let (cp, notes) = match self.accept_checkpoint(&hub, &text, &options)? { |
| 297 | 0× | Ok(c) => c, |
| 298 | 0× | Err(why) => return fail(format!("{hub}'s log could not be checked: {why}")), |
| 299 | | }; |
| 300 | 0× | let mut entries = Vec::with_capacity(cp.size as usize); |
| 301 | 0× | let mut stored: Vec<Hash> = Vec::new(); |
| 302 | 0× | let mut n = 0u64; |
| 303 | 0× | while n < cp.size { |
| 304 | 0× | let t = Tile { |
| 305 | 0× | level: 0, |
| 306 | 0× | n: n >> tlog::TILE_HEIGHT, |
| 307 | 0× | width: (cp.size - n).min(tlog::TILE_WIDTH) as u32, |
| 308 | 0× | }; |
| 309 | 0× | let bytes = http_get(&format!("{hub}/log/{}", t.entries_path()), &options)?; |
| 310 | 0× | let bundle = tlog::decode_bundle(&bytes).map_err(tl)?; |
| 311 | 0× | if bundle.len() != t.width as usize { |
| 312 | 0× | return fail(format!( |
| 313 | | "{hub}'s entry bundle {} holds {} entries, not {}", |
| 314 | 0× | t.entries_path(), |
| 315 | 0× | bundle.len(), |
| 316 | | t.width |
| 317 | | )); |
| 318 | 0× | } |
| 319 | 0× | for b in bundle { |
| 320 | 0× | let add = |
| 321 | 0× | tlog::stored_hashes_for_leaf(n, tlog::leaf_hash(&b), &stored).map_err(tl)?; |
| 322 | 0× | stored.extend(add); |
| 323 | 0× | entries.push(Entry::decode(&b).map_err(tl)?); |
| 324 | 0× | n += 1; |
| 325 | | } |
| 326 | | } |
| 327 | 0× | if tlog::tree_hash(cp.size, &stored).map_err(tl)? != cp.root { |
| 328 | 0× | 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 | | )); |
| 332 | 0× | } |
| 333 | 0× | Ok(LogAudit { |
| 334 | 0× | hub, |
| 335 | 0× | checkpoint: cp, |
| 336 | 0× | entries, |
| 337 | 0× | notes, |
| 338 | 0× | }) |
| 339 | 0× | } |
| 340 | | } |