| 1 |
use std::cell::RefCell; |
| 2 |
use std::cmp::Ordering; |
| 3 |
|
| 4 |
use sequoia_openpgp as openpgp; |
| 5 |
use openpgp::KeyHandle; |
| 6 |
use openpgp::Result; |
| 7 |
use openpgp::policy::HashAlgoSecurity; |
| 8 |
use openpgp::policy::Policy; |
| 9 |
|
| 10 |
use crate::Sq; |
| 11 |
use crate::cli::pki::vouch::list; |
| 12 |
use crate::common::ui; |
| 13 |
use crate::sq::TrustThreshold; |
| 14 |
|
| 15 |
const TRACE: bool = false; |
| 16 |
|
| 17 |
pub fn list(sq: Sq, c: list::Command) |
| 18 |
-> Result<()> |
| 19 |
{ |
| 20 |
tracer!(TRACE, "pki::vouch::list"); |
| 21 |
|
| 22 |
// Optional certifier. |
| 23 |
let (certifier, error) = |
| 24 |
sq.resolve_certs(&c.certifier, TrustThreshold::Full)?; |
| 25 |
assert!(certifier.len() <= 1); |
| 26 |
assert!(error.len() <= 1); |
| 27 |
let certifier = certifier.into_iter().next().map(RefCell::new); |
| 28 |
|
| 29 |
if let Some(error) = error.into_iter().next() { |
| 30 |
return Err(error); |
| 31 |
} |
| 32 |
|
| 33 |
// Optional target cert. |
| 34 |
let (target_cert, error) = sq.resolve_certs(&c.cert, TrustThreshold::Full)?; |
| 35 |
assert!(target_cert.len() <= 1); |
| 36 |
assert!(error.len() <= 1); |
| 37 |
if let Some(error) = error.into_iter().next() { |
| 38 |
return Err(error); |
| 39 |
} |
| 40 |
let target_cert = target_cert.into_iter().next().map(RefCell::new); |
| 41 |
|
| 42 |
let mut candidates = crate::common::pki::list::list_certifications( |
| 43 |
&sq, certifier, target_cert)?; |
| 44 |
|
| 45 |
let o = &mut std::io::stdout(); |
| 46 |
|
| 47 |
t!("Have {} possible certifications", candidates.len()); |
| 48 |
candidates.sort_by( |
| 49 |
|(a_certifier, a_cert, a_userid, a_certification), |
| 50 |
(b_certifier, b_cert, b_userid, b_certification)| |
| 51 |
{ |
| 52 |
match (a_certifier.key_handle(), b_certifier.key_handle()) { |
| 53 |
(KeyHandle::Fingerprint(a), KeyHandle::Fingerprint(b)) => { |
| 54 |
a.cmp(&b) |
| 55 |
} |
| 56 |
(KeyHandle::Fingerprint(_), KeyHandle::KeyID(_)) => { |
| 57 |
Ordering::Greater |
| 58 |
} |
| 59 |
(KeyHandle::KeyID(_), KeyHandle::Fingerprint(_)) => { |
| 60 |
Ordering::Less |
| 61 |
} |
| 62 |
(KeyHandle::KeyID(a), KeyHandle::KeyID(b)) => { |
| 63 |
a.cmp(&b) |
| 64 |
} |
| 65 |
} |
| 66 |
.then_with(|| { |
| 67 |
a_cert.borrow().fingerprint() |
| 68 |
.cmp(&b_cert.borrow().fingerprint()) |
| 69 |
}) |
| 70 |
.then_with(|| { |
| 71 |
a_userid.cmp(&b_userid) |
| 72 |
}) |
| 73 |
.then_with(|| { |
| 74 |
a_certification.signature_creation_time().cmp( |
| 75 |
&b_certification.signature_creation_time()) |
| 76 |
// Newest first. |
| 77 |
.reverse() |
| 78 |
}) |
| 79 |
.then_with(|| { |
| 80 |
// Break ties deterministically using the MPIs. |
| 81 |
a_certification.mpis().cmp( |
| 82 |
&b_certification.mpis()) |
| 83 |
}) |
| 84 |
}); |
| 85 |
|
| 86 |
let mut first = true; |
| 87 |
let mut current_certifier = None; |
| 88 |
for (certifier, cert, userid, certification) in candidates.into_iter() { |
| 89 |
let cert = cert.borrow(); |
| 90 |
|
| 91 |
let invalid = if let Some(certifier) = certifier.cert() { |
| 92 |
if let Err(err) = certification.verify_userid_binding( |
| 93 |
certifier.primary_key().key(), |
| 94 |
cert.primary_key().key(), |
| 95 |
&userid) |
| 96 |
{ |
| 97 |
// Invalid signature. |
| 98 |
t!("Certification by {} on {}, {} is invalid: {}", |
| 99 |
certifier.key_handle(), |
| 100 |
cert.fingerprint(), |
| 101 |
String::from_utf8_lossy(userid.value()), |
| 102 |
err); |
| 103 |
|
| 104 |
Some(err) |
| 105 |
} else { |
| 106 |
// Check the policy. |
| 107 |
if let Err(err) = sq.policy.signature( |
| 108 |
&certification, |
| 109 |
HashAlgoSecurity::CollisionResistance) |
| 110 |
{ |
| 111 |
Some(err) |
| 112 |
} else { |
| 113 |
None |
| 114 |
} |
| 115 |
} |
| 116 |
} else { |
| 117 |
Some(anyhow::anyhow!("Cannot check signature: missing certificate")) |
| 118 |
}; |
| 119 |
|
| 120 |
if first { |
| 121 |
first = false; |
| 122 |
} else { |
| 123 |
wwriteln!(o, ""); |
| 124 |
} |
| 125 |
|
| 126 |
if current_certifier != Some(certifier.key_handle()) { |
| 127 |
current_certifier = Some(certifier.key_handle()); |
| 128 |
|
| 129 |
wwriteln!(o, " - Certifier:"); |
| 130 |
if let Some(certifier) = certifier.cert() { |
| 131 |
ui::emit_cert_indent(o, " ", &sq, &certifier)?; |
| 132 |
} else { |
| 133 |
ui::emit_cert_key_handle_userid_str_indent( |
| 134 |
o, " ", |
| 135 |
&certifier.key_handle(), "<unknown: missing certificate>")?; |
| 136 |
} |
| 137 |
wwriteln!(o, ""); |
| 138 |
} |
| 139 |
|
| 140 |
if certifier.cert().is_some() { |
| 141 |
if let Some(ref err) = invalid { |
| 142 |
wwriteln!(o, " - Invalid certification: {}", |
| 143 |
crate::one_line_error_chain(err)); |
| 144 |
} else { |
| 145 |
wwriteln!(o, " - Certified the binding:"); |
| 146 |
} |
| 147 |
} else { |
| 148 |
wwriteln!(o, " - Unchecked and possible invalid certification \ |
| 149 |
of the binding:"); |
| 150 |
} |
| 151 |
ui::emit_cert_userid_indent(o, " ", &cert, &userid)?; |
| 152 |
|
| 153 |
wwriteln!(o, ""); |
| 154 |
let indent = " "; |
| 155 |
crate::common::pki::list::summarize_certification( |
| 156 |
o, indent, &certification, false)?; |
| 157 |
} |
| 158 |
|
| 159 |
Ok(()) |
| 160 |
} |