| 1 |
use std::{ |
| 2 |
fmt::Write, |
| 3 |
time::SystemTime, |
| 4 |
}; |
| 5 |
|
| 6 |
use anyhow::Error; |
| 7 |
|
| 8 |
use sequoia_openpgp as openpgp; |
| 9 |
use openpgp::Cert; |
| 10 |
use openpgp::Fingerprint; |
| 11 |
use openpgp::KeyHandle; |
| 12 |
use openpgp::Result; |
| 13 |
use openpgp::cert::amalgamation::ValidAmalgamation; |
| 14 |
use openpgp::packet::UserID; |
| 15 |
use openpgp::types::RevocationStatus; |
| 16 |
|
| 17 |
use sequoia_cert_store as cert_store; |
| 18 |
use cert_store::Store; |
| 19 |
|
| 20 |
use sequoia_wot as wot; |
| 21 |
use wot::Path; |
| 22 |
use wot::PathLints; |
| 23 |
use wot::FULLY_TRUSTED; |
| 24 |
use wot::PARTIALLY_TRUSTED; |
| 25 |
|
| 26 |
use crate::Convert; |
| 27 |
use crate::Sq; |
| 28 |
use crate::Time; |
| 29 |
use crate::common::ca_creation_time; |
| 30 |
use crate::common::ui; |
| 31 |
use crate::error_chain; |
| 32 |
use crate::output::wrapping::NBSP; |
| 33 |
use super::OutputType; |
| 34 |
|
| 35 |
/// Prints a Path Error |
| 36 |
pub fn print_path_error(output: &mut dyn std::io::Write, err: Error) { |
| 37 |
wwriteln!(stream=output, initial_indent = "└ ", "Checking path: {}", err); |
| 38 |
} |
| 39 |
|
| 40 |
/// Prints information of a Path for a target UserID associated with a KeyHandle |
| 41 |
pub fn print_path_header( |
| 42 |
output: &mut dyn std::io::Write, |
| 43 |
target_kh: &KeyHandle, |
| 44 |
target_userid: &UserID, |
| 45 |
amount: usize, |
| 46 |
required_amount: usize, |
| 47 |
) { |
| 48 |
wwriteln!( |
| 49 |
stream=output, |
| 50 |
initial_indent="", |
| 51 |
subsequent_indent=" ", |
| 52 |
"[{}] {} {}: {} authenticated ({}%)", |
| 53 |
if amount >= required_amount { |
| 54 |
"✓" |
| 55 |
} else { |
| 56 |
" " |
| 57 |
}, |
| 58 |
target_kh, |
| 59 |
ui::Safe(target_userid), |
| 60 |
if amount >= 2 * FULLY_TRUSTED { |
| 61 |
"doubly" |
| 62 |
} else if amount >= FULLY_TRUSTED { |
| 63 |
"fully" |
| 64 |
} else if amount >= PARTIALLY_TRUSTED { |
| 65 |
"partially" |
| 66 |
} else if amount > 0 { |
| 67 |
"marginally" |
| 68 |
} else { |
| 69 |
"not" |
| 70 |
}, |
| 71 |
(amount * 100) / FULLY_TRUSTED |
| 72 |
); |
| 73 |
} |
| 74 |
|
| 75 |
/// Prints information on a Path for a UserID |
| 76 |
pub fn print_path(output: &mut dyn std::io::Write, |
| 77 |
path: &PathLints, target_userid: &UserID, prefix: &str) |
| 78 |
-> Result<()> |
| 79 |
{ |
| 80 |
let certification_count = path.certifications().count(); |
| 81 |
wwriteln!(stream=output, initial_indent=format!("{}◯─┬ ", prefix), |
| 82 |
subsequent_indent=format!("{}│ │ ", prefix), |
| 83 |
"{}", path.root().key_handle()); |
| 84 |
wwriteln!(stream=output, initial_indent=format!("{}│ └ ", prefix), |
| 85 |
subsequent_indent=format!("{}│ ", prefix), |
| 86 |
"{}", |
| 87 |
if certification_count == 0 { |
| 88 |
format!("{}", ui::Safe(target_userid)) |
| 89 |
} else if let Some(userid) = path.root().primary_userid() { |
| 90 |
format!("({})", ui::Safe(&userid)) |
| 91 |
} else { |
| 92 |
format!("") |
| 93 |
}); |
| 94 |
|
| 95 |
if path.certifications().count() == 0 { |
| 96 |
wwriteln!(stream=output, indent=prefix, "│"); |
| 97 |
wwriteln!(stream=output, initial_indent=format!("{}└── ", prefix), |
| 98 |
subsequent_indent=format!("{} ", prefix), |
| 99 |
"Self-signed user ID."); |
| 100 |
return Ok(()); |
| 101 |
} |
| 102 |
|
| 103 |
for (last, (cert, certification)) in path |
| 104 |
.certs() |
| 105 |
.zip(path.certifications()) |
| 106 |
.enumerate() |
| 107 |
.map(|(j, c)| { |
| 108 |
if j + 1 == certification_count { |
| 109 |
(true, c) |
| 110 |
} else { |
| 111 |
(false, c) |
| 112 |
} |
| 113 |
}) |
| 114 |
{ |
| 115 |
let mut line = String::new(); |
| 116 |
if let Some(certification) = certification.certification() { |
| 117 |
if certification.amount() < FULLY_TRUSTED { |
| 118 |
write!(&mut line, |
| 119 |
"partially certified (amount: {}{}of{}120)", |
| 120 |
certification.amount(), NBSP, NBSP, |
| 121 |
)?; |
| 122 |
} else { |
| 123 |
write!(&mut line, "certified")?; |
| 124 |
} |
| 125 |
|
| 126 |
if last { |
| 127 |
write!(&mut line, " the following binding")?; |
| 128 |
} else { |
| 129 |
write!(&mut line, " the following certificate")?; |
| 130 |
} |
| 131 |
|
| 132 |
if certification.creation_time() != ca_creation_time() { |
| 133 |
write!(&mut line, |
| 134 |
" on {}", |
| 135 |
chrono::DateTime::<chrono::Utc>::from( |
| 136 |
certification.creation_time() |
| 137 |
) |
| 138 |
.format("%Y‑%m‑%d") |
| 139 |
)?; |
| 140 |
} |
| 141 |
|
| 142 |
if let Some(e) = certification.expiration_time() { |
| 143 |
write!(&mut line, |
| 144 |
" (expiry: {})", |
| 145 |
chrono::DateTime::<chrono::Utc>::from(e).format("%Y‑%m‑%d") |
| 146 |
)?; |
| 147 |
} |
| 148 |
if certification.depth() > 0.into() { |
| 149 |
write!(&mut line, " as a")?; |
| 150 |
if certification.depth() == 1.into() { |
| 151 |
write!(&mut line, " introducer (depth: {})", |
| 152 |
certification.depth())?; |
| 153 |
} else { |
| 154 |
write!(&mut line, |
| 155 |
" meta-introducer (depth: {})", |
| 156 |
certification.depth() |
| 157 |
)?; |
| 158 |
} |
| 159 |
} |
| 160 |
} else { |
| 161 |
write!(&mut line, " No adequate certification found.")?; |
| 162 |
} |
| 163 |
|
| 164 |
wwriteln!(stream=output, indent=prefix, "│"); |
| 165 |
wwriteln!(stream=output, indent=format!("{}│ ", prefix), "{}", line); |
| 166 |
wwriteln!(stream=output, indent=prefix, "│"); |
| 167 |
|
| 168 |
for err in cert.errors().iter().chain(cert.lints()) { |
| 169 |
for (i, msg) in error_chain(err).into_iter().enumerate() { |
| 170 |
wwriteln!( |
| 171 |
stream=output, |
| 172 |
indent=format!( |
| 173 |
"{}│ {}", prefix, if i == 0 { "" } else { " " }), |
| 174 |
"{}", msg); |
| 175 |
} |
| 176 |
} |
| 177 |
for err in certification.errors().iter().chain(certification.lints()) { |
| 178 |
for (i, msg) in error_chain(err).into_iter().enumerate() { |
| 179 |
wwriteln!( |
| 180 |
stream=output, |
| 181 |
indent=format!( |
| 182 |
"{}│ {}", prefix, if i == 0 { "" } else { " " }), |
| 183 |
"{}", msg); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
wwriteln!(stream=output, |
| 188 |
initial_indent=format!("{}{}─┬ ", prefix, |
| 189 |
if last { "└" } else { "├" }), |
| 190 |
subsequent_indent=format!("{}{} │ ", prefix, |
| 191 |
if last { " " } else { "│" }), |
| 192 |
"{}", certification.target()); |
| 193 |
wwriteln!(stream=output, |
| 194 |
initial_indent=format!("{}{} └ ", prefix, |
| 195 |
if last { " " } else { "│" }), |
| 196 |
subsequent_indent=format!("{}{} ", prefix, |
| 197 |
if last { " " } else { "│" }), |
| 198 |
"{}", |
| 199 |
if last { |
| 200 |
format!("{}", ui::Safe(target_userid)) |
| 201 |
} else if let Some(userid) = |
| 202 |
certification.target_cert().and_then(|c| c.primary_userid()) |
| 203 |
{ |
| 204 |
format!("({})", ui::Safe(userid.userid())) |
| 205 |
} else { |
| 206 |
"".into() |
| 207 |
}); |
| 208 |
|
| 209 |
if last { |
| 210 |
let target = path.certs().last().expect("have one"); |
| 211 |
for err in target.errors().iter().chain(target.lints()) { |
| 212 |
for (i, msg) in error_chain(err).into_iter().enumerate() { |
| 213 |
wwriteln!( |
| 214 |
stream=output, |
| 215 |
indent=format!( |
| 216 |
"{}│ {}", prefix, if i == 0 { "" } else { " " }), |
| 217 |
"{}", msg); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
wwriteln!(stream=output, ""); |
| 224 |
Ok(()) |
| 225 |
} |
| 226 |
|
| 227 |
/// The concise human-readable specific implementation of an |
| 228 |
/// OutputNetwork |
| 229 |
pub struct ConciseHumanReadableOutputNetwork<'a, 'store, 'rstore> { |
| 230 |
output: &'a mut dyn std::io::Write, |
| 231 |
sq: &'a Sq<'store, 'rstore>, |
| 232 |
paths: bool, |
| 233 |
required_amount: usize, |
| 234 |
current_cert: Option<Cert>, |
| 235 |
bindings_shown: usize, |
| 236 |
} |
| 237 |
|
| 238 |
impl<'a, 'store, 'rstore> ConciseHumanReadableOutputNetwork<'a, 'store, 'rstore> { |
| 239 |
/// Creates a new ConciseHumanReadableOutputNetwork |
| 240 |
pub fn new(output: &'a mut dyn std::io::Write, |
| 241 |
sq: &'a Sq<'store, 'rstore>, |
| 242 |
required_amount: usize, paths: bool) |
| 243 |
-> Self |
| 244 |
{ |
| 245 |
Self { |
| 246 |
output, |
| 247 |
sq, |
| 248 |
paths, |
| 249 |
required_amount, |
| 250 |
current_cert: None, |
| 251 |
bindings_shown: 0, |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
impl OutputType for ConciseHumanReadableOutputNetwork<'_, '_, '_> { |
| 257 |
fn add_cert(&mut self, fingerprint: &Fingerprint) -> Result<()> { |
| 258 |
let first_shown = self.current_cert.is_none(); |
| 259 |
|
| 260 |
let current_fingerprint = |
| 261 |
self.current_cert.as_ref().map(|cert| cert.fingerprint()); |
| 262 |
let show_cert = if current_fingerprint.as_ref() != Some(fingerprint) { |
| 263 |
let cert = if let Ok(store) = self.sq.cert_store_or_else() { |
| 264 |
store.lookup_by_cert_fpr(fingerprint) |
| 265 |
.and_then(|lazy_cert| { |
| 266 |
Ok(lazy_cert.to_cert()?.clone()) |
| 267 |
}) |
| 268 |
.ok() |
| 269 |
} else { |
| 270 |
None |
| 271 |
}; |
| 272 |
self.current_cert = cert; |
| 273 |
true |
| 274 |
} else { |
| 275 |
false |
| 276 |
}; |
| 277 |
|
| 278 |
let vc = self.current_cert.as_ref().map(|cert| { |
| 279 |
cert.with_policy(self.sq.policy, self.sq.time) |
| 280 |
}); |
| 281 |
|
| 282 |
if show_cert { |
| 283 |
let cert = self.current_cert.as_ref() |
| 284 |
.expect("show_cert is true, there is a current cert"); |
| 285 |
|
| 286 |
let mut extra_info = Vec::new(); |
| 287 |
|
| 288 |
// To derive a valid cert, we need a valid binding |
| 289 |
// signature. But even if we don't have that we may still |
| 290 |
// have a valid revocation certificate. So be a bit more |
| 291 |
// careful. |
| 292 |
let rs = if let Some(Ok(ref vc)) = vc { |
| 293 |
vc.revocation_status() |
| 294 |
} else { |
| 295 |
cert.revocation_status(self.sq.policy, self.sq.time) |
| 296 |
}; |
| 297 |
|
| 298 |
if let RevocationStatus::Revoked(sigs) = rs { |
| 299 |
let sig = sigs[0]; |
| 300 |
let mut reason_; |
| 301 |
let reason = if let Some((reason, message)) |
| 302 |
= sig.reason_for_revocation() |
| 303 |
{ |
| 304 |
// Be careful to quote the message it is |
| 305 |
// controlled by the certificate holder. |
| 306 |
reason_ = reason.to_string(); |
| 307 |
if ! message.is_empty() { |
| 308 |
reason_.push_str(": "); |
| 309 |
reason_.push_str(&ui::Safe(message).to_string()); |
| 310 |
} |
| 311 |
&reason_ |
| 312 |
} else { |
| 313 |
"no reason specified" |
| 314 |
}; |
| 315 |
|
| 316 |
extra_info.push(format!( |
| 317 |
"revoked {}, {}", |
| 318 |
sig.signature_creation_time() |
| 319 |
.unwrap_or(std::time::UNIX_EPOCH) |
| 320 |
.convert(), |
| 321 |
reason)) |
| 322 |
} |
| 323 |
|
| 324 |
match &vc { |
| 325 |
Some(Ok(vc)) => { |
| 326 |
if let Some(t) = vc.primary_key().key_expiration_time() { |
| 327 |
if t < SystemTime::now() { |
| 328 |
extra_info.push( |
| 329 |
format!("expired {}", |
| 330 |
Time::try_from(t) |
| 331 |
.expect("is an OpenPGP timestamp"))) |
| 332 |
} else { |
| 333 |
extra_info.push( |
| 334 |
format!("will expire {}", |
| 335 |
Time::try_from(t) |
| 336 |
.expect("is an OpenPGP timestamp"))) |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
Some(Err(err)) => { |
| 341 |
extra_info.push( |
| 342 |
format!("not valid: {}", |
| 343 |
crate::one_line_error_chain(err))); |
| 344 |
} |
| 345 |
None => (), |
| 346 |
} |
| 347 |
|
| 348 |
if ! first_shown { |
| 349 |
wwriteln!(stream=self.output); |
| 350 |
} |
| 351 |
|
| 352 |
wwriteln!(stream=self.output, initial_indent = " - ", |
| 353 |
"{}", fingerprint); |
| 354 |
|
| 355 |
if cert.primary_key().key().creation_time() != ca_creation_time() { |
| 356 |
wwriteln!(stream=self.output, initial_indent = " - ", |
| 357 |
"created {}", |
| 358 |
cert.primary_key().key().creation_time().convert()); |
| 359 |
} |
| 360 |
|
| 361 |
for info in extra_info.into_iter() { |
| 362 |
wwriteln!(stream=self.output, initial_indent = " - ", |
| 363 |
"{}", info); |
| 364 |
} |
| 365 |
wwriteln!(stream=self.output); |
| 366 |
} |
| 367 |
|
| 368 |
Ok(()) |
| 369 |
} |
| 370 |
|
| 371 |
fn add_paths( |
| 372 |
&mut self, |
| 373 |
paths: Vec<(Path, usize)>, |
| 374 |
fingerprint: &Fingerprint, |
| 375 |
userid: &UserID, |
| 376 |
aggregated_amount: usize, |
| 377 |
) -> Result<()> { |
| 378 |
let vc; |
| 379 |
if let Some(cert) = &self.current_cert { |
| 380 |
if cert.fingerprint() != *fingerprint { |
| 381 |
return Err( |
| 382 |
anyhow::anyhow!("missing call to OutputFormat::add_cert")); |
| 383 |
} |
| 384 |
|
| 385 |
vc = cert.with_policy(self.sq.policy, self.sq.time); |
| 386 |
} else { |
| 387 |
return Err( |
| 388 |
anyhow::anyhow!("missing call to OutputFormat::add_cert")); |
| 389 |
}; |
| 390 |
|
| 391 |
let revoked = if let Ok(ref vc) = vc { |
| 392 |
vc.userids() |
| 393 |
.filter_map(|u| { |
| 394 |
if u.userid() != userid { |
| 395 |
return None; |
| 396 |
} |
| 397 |
|
| 398 |
if let RevocationStatus::Revoked(_) = u.revocation_status() { |
| 399 |
Some(()) |
| 400 |
} else { |
| 401 |
None |
| 402 |
} |
| 403 |
}) |
| 404 |
.next() |
| 405 |
} else { |
| 406 |
None |
| 407 |
}; |
| 408 |
|
| 409 |
wwriteln!(stream=self.output, initial_indent = " - ", "[ {} ] {}", |
| 410 |
if revoked.is_some() { |
| 411 |
"revoked".to_string() |
| 412 |
} else if aggregated_amount >= self.required_amount { |
| 413 |
" ✓ ".to_string() |
| 414 |
} else { |
| 415 |
format!("{:3}/120", aggregated_amount) |
| 416 |
}, |
| 417 |
ui::Safe(userid)); |
| 418 |
|
| 419 |
if self.paths { |
| 420 |
wwriteln!(stream=self.output); |
| 421 |
|
| 422 |
for (i, (path, amount)) in paths.iter().enumerate() { |
| 423 |
if paths.len() > 1 { |
| 424 |
wwriteln!( |
| 425 |
stream=self.output, |
| 426 |
initial_indent=" ", |
| 427 |
"Path #{}{}of{}{}, trust amount {}:", |
| 428 |
i + 1, NBSP, NBSP, |
| 429 |
paths.len(), |
| 430 |
amount |
| 431 |
); |
| 432 |
} |
| 433 |
|
| 434 |
print_path(self.output, &path.into(), userid, " ")?; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
self.bindings_shown += 1; |
| 439 |
|
| 440 |
Ok(()) |
| 441 |
} |
| 442 |
|
| 443 |
/// Write the HumanReadableOutputNetwork to output |
| 444 |
/// |
| 445 |
/// This function does in fact nothing as we are printing directly in |
| 446 |
/// add_paths(). |
| 447 |
fn finalize(&mut self) -> Result<()> { |
| 448 |
if self.bindings_shown == 0 { |
| 449 |
return Ok(()); |
| 450 |
} |
| 451 |
|
| 452 |
let cert = |
| 453 |
self.current_cert.as_ref().expect("have at least one"); |
| 454 |
|
| 455 |
if ! self.paths { |
| 456 |
self.sq.hint(format_args!( |
| 457 |
"To view why a user ID is considered valid, pass \ |
| 458 |
`--show-paths`.")); |
| 459 |
} |
| 460 |
|
| 461 |
self.sq.hint(format_args!( |
| 462 |
"To see more details about a certificate, for example {}, run:", |
| 463 |
cert.fingerprint())) |
| 464 |
.sq().arg("inspect") |
| 465 |
.arg_value("--cert", cert.fingerprint()) |
| 466 |
.done(); |
| 467 |
|
| 468 |
Ok(()) |
| 469 |
} |
| 470 |
} |