| 1 |
use std::collections::BTreeMap; |
| 2 |
use std::sync::Arc; |
| 3 |
|
| 4 |
use sequoia_openpgp as openpgp; |
| 5 |
use openpgp::Fingerprint; |
| 6 |
use openpgp::KeyHandle; |
| 7 |
|
| 8 |
use sequoia_cert_store as cert_store; |
| 9 |
use cert_store::LazyCert; |
| 10 |
use cert_store::Store; |
| 11 |
|
| 12 |
use crate::Result; |
| 13 |
use crate::Sq; |
| 14 |
use crate::cli::pki::link; |
| 15 |
use crate::cli::types::cert_designator; |
| 16 |
use crate::commands::active_certification; |
| 17 |
use crate::common::pki::list::summarize_certification; |
| 18 |
use crate::common::ui; |
| 19 |
use crate::sq::TrustThreshold; |
| 20 |
|
| 21 |
pub fn list(sq: Sq, c: link::ListCommand) |
| 22 |
-> Result<()> |
| 23 |
{ |
| 24 |
tracer!(TRACE, "sq::pki::link::list"); |
| 25 |
|
| 26 |
let cert_store = sq.cert_store_or_else()?; |
| 27 |
cert_store.prefetch_all(); |
| 28 |
|
| 29 |
let trust_root = sq.local_trust_root()?; |
| 30 |
let trust_root = trust_root.to_cert()?; |
| 31 |
|
| 32 |
let mut certs = c.certs; |
| 33 |
|
| 34 |
if let Some(pattern) = c.pattern { |
| 35 |
let mut d = None; |
| 36 |
if let Ok(kh) = pattern.parse::<KeyHandle>() { |
| 37 |
if matches!(kh, KeyHandle::Fingerprint(Fingerprint::Unknown { .. })) { |
| 38 |
let hex = pattern.chars() |
| 39 |
.map(|c| { |
| 40 |
if c == ' ' { 0 } else { 1 } |
| 41 |
}) |
| 42 |
.sum::<usize>(); |
| 43 |
|
| 44 |
if hex >= 16 { |
| 45 |
weprintln!("Warning: {} looks like a fingerprint or key ID, \ |
| 46 |
but its invalid. Treating it as a text pattern.", |
| 47 |
pattern); |
| 48 |
} |
| 49 |
} else { |
| 50 |
d = Some(cert_designator::CertDesignator::Cert(kh)); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
certs.push(d.unwrap_or_else(|| { |
| 55 |
cert_designator::CertDesignator::Grep(pattern) |
| 56 |
})); |
| 57 |
} |
| 58 |
|
| 59 |
let mut active_certifications_cache: BTreeMap<Fingerprint, _> |
| 60 |
= BTreeMap::new(); |
| 61 |
|
| 62 |
let (certs, errors) = if certs.is_empty() { |
| 63 |
(cert_store.certs(), Vec::new()) |
| 64 |
} else { |
| 65 |
let (c, e) = sq.resolve_certs_filter( |
| 66 |
&certs, TrustThreshold::Full, &mut |_designator, cert| { |
| 67 |
let userids = cert.userids(); |
| 68 |
let cert = cert.to_cert()?; |
| 69 |
|
| 70 |
let active_certifications = active_certification( |
| 71 |
&sq, cert, userids, |
| 72 |
trust_root.primary_key().key().role_as_unspecified()); |
| 73 |
|
| 74 |
if active_certifications.iter().any(|(_userid, certifications)| { |
| 75 |
certifications.is_some() |
| 76 |
}) |
| 77 |
{ |
| 78 |
active_certifications_cache.insert( |
| 79 |
cert.fingerprint(), |
| 80 |
active_certifications); |
| 81 |
Ok(()) |
| 82 |
} else { |
| 83 |
Err(anyhow::anyhow!("{} {} was never linked", |
| 84 |
cert.fingerprint(), |
| 85 |
sq.best_userid(cert, true).display())) |
| 86 |
} |
| 87 |
})?; |
| 88 |
(Box::new(c.into_iter().map(|c| Arc::new(LazyCert::from(c)))) |
| 89 |
as Box<dyn Iterator<Item=Arc<LazyCert<'_>>>>, |
| 90 |
e) |
| 91 |
}; |
| 92 |
|
| 93 |
for error in errors.iter() { |
| 94 |
crate::print_error_chain(error); |
| 95 |
} |
| 96 |
if ! errors.is_empty() { |
| 97 |
return Err(anyhow::anyhow!("Failed to resolve certificates")); |
| 98 |
} |
| 99 |
|
| 100 |
let o = &mut std::io::stdout(); |
| 101 |
let mut dirty = false; |
| 102 |
|
| 103 |
for cert in certs { |
| 104 |
let cert = if let Ok(cert) = cert.to_cert() { |
| 105 |
cert |
| 106 |
} else { |
| 107 |
// Invalid cert. Skip it. |
| 108 |
continue; |
| 109 |
}; |
| 110 |
|
| 111 |
let active_certifications = if let Some(ac) |
| 112 |
= active_certifications_cache.remove(&cert.fingerprint()) |
| 113 |
{ |
| 114 |
ac |
| 115 |
} else { |
| 116 |
let userids = cert.userids().map(|ua| ua.userid().clone()); |
| 117 |
|
| 118 |
active_certification( |
| 119 |
&sq, &cert, userids, |
| 120 |
trust_root.primary_key().key().role_as_unspecified()) |
| 121 |
}; |
| 122 |
|
| 123 |
for (userid, certification) in active_certifications |
| 124 |
.into_iter() |
| 125 |
.filter_map(|(user, certification)| { |
| 126 |
if let Some(certification) = certification { |
| 127 |
Some((user, certification)) |
| 128 |
} else { |
| 129 |
None |
| 130 |
} |
| 131 |
}) |
| 132 |
{ |
| 133 |
let (depth, _amount) = certification.trust_signature() |
| 134 |
.unwrap_or((0, sequoia_wot::FULLY_TRUSTED as u8)); |
| 135 |
|
| 136 |
if c.ca && depth == 0 { |
| 137 |
// Only show CAs. |
| 138 |
continue; |
| 139 |
} |
| 140 |
|
| 141 |
if dirty { |
| 142 |
wwriteln!(stream=o); |
| 143 |
} |
| 144 |
dirty = true; |
| 145 |
|
| 146 |
ui::emit_cert_userid(o, &cert, &userid)?; |
| 147 |
let indent = " "; |
| 148 |
summarize_certification(o, indent, &certification, true)?; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
Ok(()) |
| 153 |
} |