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