| 1 |
use std::cell::Ref; |
| 2 |
use std::cell::RefCell; |
| 3 |
|
| 4 |
use sequoia_openpgp as openpgp; |
| 5 |
use openpgp::Cert; |
| 6 |
use openpgp::KeyHandle; |
| 7 |
use openpgp::Packet; |
| 8 |
use openpgp::packet::Signature; |
| 9 |
use openpgp::packet::Tag; |
| 10 |
use openpgp::packet::UserID; |
| 11 |
use openpgp::types::KeyFlags; |
| 12 |
use openpgp::types::SignatureType; |
| 13 |
|
| 14 |
use sequoia_cert_store as cert_store; |
| 15 |
use cert_store::Store; |
| 16 |
|
| 17 |
use crate::Result; |
| 18 |
use crate::Sq; |
| 19 |
use crate::common::key_handle_dealias; |
| 20 |
use crate::common::ui; |
| 21 |
|
| 22 |
const TRACE: bool = false; |
| 23 |
|
| 24 |
pub enum CertOrKeyHandle { |
| 25 |
Cert(RefCell<Cert>), |
| 26 |
KeyHandle(KeyHandle), |
| 27 |
} |
| 28 |
|
| 29 |
impl CertOrKeyHandle { |
| 30 |
pub fn key_handle(&self) -> KeyHandle { |
| 31 |
match self { |
| 32 |
CertOrKeyHandle::Cert(cert) => cert.borrow().key_handle(), |
| 33 |
CertOrKeyHandle::KeyHandle(kh) => kh.clone(), |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
pub fn cert(&self) -> Option<Ref<Cert>> { |
| 38 |
match self { |
| 39 |
CertOrKeyHandle::Cert(cert) => Some(cert.borrow()), |
| 40 |
CertOrKeyHandle::KeyHandle(_) => None, |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl From<RefCell<Cert>> for CertOrKeyHandle { |
| 46 |
fn from(cert: RefCell<Cert>) -> CertOrKeyHandle { |
| 47 |
CertOrKeyHandle::Cert(cert) |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
impl From<&KeyHandle> for CertOrKeyHandle { |
| 52 |
fn from(kh: &KeyHandle) -> CertOrKeyHandle { |
| 53 |
CertOrKeyHandle::KeyHandle(kh.clone()) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
impl From<KeyHandle> for CertOrKeyHandle { |
| 58 |
fn from(kh: KeyHandle) -> CertOrKeyHandle { |
| 59 |
CertOrKeyHandle::KeyHandle(kh) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/// Returns the certifications made by `certifier` if not `None` and |
| 64 |
/// the certifications of `target_cert`, if not `None`. |
| 65 |
/// |
| 66 |
/// If both `certifier` and `target_cert` are provided, returns the |
| 67 |
/// intersection. |
| 68 |
/// |
| 69 |
/// At least one of `certifier` and `target_cert` must be provided. |
| 70 |
/// |
| 71 |
/// This returns all possible certificates. It does not check their |
| 72 |
/// legitimacy. |
| 73 |
pub fn list_certifications(sq: &Sq, |
| 74 |
certifier: Option<RefCell<Cert>>, |
| 75 |
target_cert: Option<RefCell<Cert>>) |
| 76 |
-> Result<Vec<(CertOrKeyHandle, RefCell<Cert>, UserID, Signature)>> |
| 77 |
{ |
| 78 |
tracer!(TRACE, "list_certifications"); |
| 79 |
|
| 80 |
let cert_store = sq.cert_store_or_else()?; |
| 81 |
|
| 82 |
// Certifier, target cert, target user ID, certification. |
| 83 |
let mut candidates: Vec<(CertOrKeyHandle, RefCell<Cert>, UserID, Signature)> |
| 84 |
= Vec::new(); |
| 85 |
|
| 86 |
if let Some(target_cert) = target_cert { |
| 87 |
// The user wants to know who certified a particular certificate. |
| 88 |
|
| 89 |
let target_cert_fpr = target_cert.borrow().fingerprint(); |
| 90 |
|
| 91 |
// Get all of the certifications made on the target cert. |
| 92 |
for ua in target_cert.borrow().userids() { |
| 93 |
for certification in ua.certifications() { |
| 94 |
let issuers = certification.get_issuers(); |
| 95 |
|
| 96 |
if let Some(ref certifier) = certifier { |
| 97 |
// The user wants to know about a specific certifier. |
| 98 |
|
| 99 |
let certifier_kh = certifier.borrow().key_handle(); |
| 100 |
|
| 101 |
if issuers.into_iter().any(|kh| kh.aliases(&certifier_kh)) { |
| 102 |
candidates.push( |
| 103 |
(RefCell::clone(certifier).into(), |
| 104 |
RefCell::clone(&target_cert).into(), |
| 105 |
ua.userid().clone(), |
| 106 |
certification.clone())); |
| 107 |
} |
| 108 |
} else { |
| 109 |
// The user wants to know about any certifier. |
| 110 |
|
| 111 |
for kh in key_handle_dealias(&issuers) { |
| 112 |
if let Ok(certifiers) |
| 113 |
= sq.lookup(std::iter::once(&kh), |
| 114 |
Some(KeyFlags::certification()), |
| 115 |
false, true) |
| 116 |
{ |
| 117 |
for certifier in certifiers.into_iter() { |
| 118 |
t!(" Possible certification of {}, {:?} by {}, {}", |
| 119 |
target_cert_fpr, |
| 120 |
String::from_utf8_lossy(ua.userid().value()), |
| 121 |
certifier.fingerprint(), |
| 122 |
sq.best_userid(&certifier, true).display()); |
| 123 |
candidates.push( |
| 124 |
(RefCell::new(certifier).into(), |
| 125 |
RefCell::clone(&target_cert), |
| 126 |
ua.userid().clone(), |
| 127 |
certification.clone())); |
| 128 |
} |
| 129 |
} else { |
| 130 |
t!(" Possible certification by {}, but we can't \ |
| 131 |
check it as we don't have the certificate", |
| 132 |
kh); |
| 133 |
candidates.push( |
| 134 |
(kh.into(), |
| 135 |
RefCell::clone(&target_cert), |
| 136 |
ua.userid().clone(), |
| 137 |
certification.clone())); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
} else if let Some(certifier) = certifier { |
| 144 |
let certifier_fpr = certifier.borrow().fingerprint(); |
| 145 |
let certifier_kh = KeyHandle::from(&certifier_fpr); |
| 146 |
|
| 147 |
// The user wants to know what certificates `certifier` |
| 148 |
// certified. Since certifications are stored alongside the |
| 149 |
// certified certificate, we have to iterate over all of the |
| 150 |
// certificates in the store. |
| 151 |
// |
| 152 |
// To avoid fully parsing certificates, which is expensive, we |
| 153 |
// first do a lightweight check with the `RawCert`: if the |
| 154 |
// `RawCert` doesn't contain a signature packet with |
| 155 |
// `certifier` as the issuer, then we can skip it. |
| 156 |
|
| 157 |
// XXX: We're iterating over all certificates in the |
| 158 |
// certificate store. Consider parallelizing this. |
| 159 |
for cert in cert_store.certs() { |
| 160 |
t!("Considering {}, {}", |
| 161 |
cert.fingerprint(), |
| 162 |
cert.userids().next() |
| 163 |
.map(|userid| { |
| 164 |
String::from_utf8_lossy(userid.value()).to_string() |
| 165 |
}) |
| 166 |
.unwrap_or_else(|| "<no userids>".to_string())); |
| 167 |
if cert.userids().next().is_none() { |
| 168 |
// The certificate has no user IDs. Thus, it can't |
| 169 |
// have been certified. |
| 170 |
t!(" Skipping: no user IDs."); |
| 171 |
continue; |
| 172 |
} |
| 173 |
if cert.fingerprint() == certifier_fpr { |
| 174 |
// We can't make a third-party certification on |
| 175 |
// ourself; that's a first-party certification! |
| 176 |
t!(" Skipping: is certifier."); |
| 177 |
continue; |
| 178 |
} |
| 179 |
|
| 180 |
if let Some(cert) = cert.raw_cert() { |
| 181 |
let mut possible_certification = false; |
| 182 |
for raw_packet in cert.packets() { |
| 183 |
let sig = if raw_packet.tag() == Tag::Signature { |
| 184 |
match Packet::try_from(raw_packet) { |
| 185 |
Ok(packet) => { |
| 186 |
if let Packet::Signature(sig) = packet { |
| 187 |
sig |
| 188 |
} else { |
| 189 |
unreachable!(); |
| 190 |
} |
| 191 |
} |
| 192 |
Err(err) => { |
| 193 |
// Invalid packet. |
| 194 |
t!(" Invalid signature: {}.", err); |
| 195 |
continue; |
| 196 |
} |
| 197 |
} |
| 198 |
} else { |
| 199 |
// Not a signature packet. |
| 200 |
continue; |
| 201 |
}; |
| 202 |
|
| 203 |
if ! matches!( |
| 204 |
sig.typ(), |
| 205 |
SignatureType::GenericCertification |
| 206 |
| SignatureType::PersonaCertification |
| 207 |
| SignatureType::CasualCertification |
| 208 |
| SignatureType::PositiveCertification) |
| 209 |
{ |
| 210 |
// Not a certification. |
| 211 |
continue; |
| 212 |
} |
| 213 |
|
| 214 |
// Check that an issuer packet names the |
| 215 |
// certifier. |
| 216 |
let issuers = sig.get_issuers(); |
| 217 |
if issuers.into_iter().any(|issuer| { |
| 218 |
issuer.aliases( |
| 219 |
KeyHandle::from(cert.fingerprint())) |
| 220 |
}) |
| 221 |
{ |
| 222 |
t!(" Have a possible certification"); |
| 223 |
possible_certification = true; |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
if ! possible_certification { |
| 229 |
t!(" Skipping: No certifications found."); |
| 230 |
continue; |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
// Add any certifications to the candidates list. |
| 235 |
match cert.to_cert() { |
| 236 |
Ok(cert) => { |
| 237 |
let cert_cell: RefCell<Cert> = RefCell::new(cert.clone()); |
| 238 |
let cert = cert_cell.borrow(); |
| 239 |
for ua in cert.userids() { |
| 240 |
for certification in ua.certifications() { |
| 241 |
let issuers = certification.get_issuers(); |
| 242 |
if issuers.into_iter().any(|issuer| { |
| 243 |
issuer.aliases(&certifier_kh) |
| 244 |
}) |
| 245 |
{ |
| 246 |
t!(" Possible certification of {}, {:?}", |
| 247 |
cert.fingerprint(), |
| 248 |
String::from_utf8_lossy(ua.userid().value())); |
| 249 |
candidates.push( |
| 250 |
(RefCell::clone(&certifier).into(), |
| 251 |
RefCell::clone(&cert_cell).into(), |
| 252 |
ua.userid().clone(), |
| 253 |
certification.clone())); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
Err(err) => { |
| 259 |
// Silently ignore the parse error. |
| 260 |
t!("Error parsing {}: {}", cert.fingerprint(), err); |
| 261 |
continue; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
} else { |
| 266 |
// The user must supply either --certifier or --cert (or |
| 267 |
// both). |
| 268 |
return Err(clap::Error::raw( |
| 269 |
clap::error::ErrorKind::MissingRequiredArgument, |
| 270 |
"Either a certifier (using, e.g., --certifier) \ |
| 271 |
or a target certificate (using, e.g., --cert) \ |
| 272 |
must be supplied").into()); |
| 273 |
} |
| 274 |
|
| 275 |
Ok(candidates) |
| 276 |
} |
| 277 |
|
| 278 |
/// `link` is whether "link" should be used to talk about the |
| 279 |
/// certification or "certification". |
| 280 |
pub fn summarize_certification(o: &mut dyn std::io::Write, |
| 281 |
indent: &str, |
| 282 |
certification: &Signature, |
| 283 |
link: bool) |
| 284 |
-> Result<()> |
| 285 |
{ |
| 286 |
let (link, linked) = if link { |
| 287 |
("link", "linked") |
| 288 |
} else { |
| 289 |
("certification", "certified") |
| 290 |
}; |
| 291 |
|
| 292 |
let indent = &format!("{} - ", indent)[..]; |
| 293 |
|
| 294 |
if let Some(t) = certification.signature_creation_time() { |
| 295 |
wwriteln!(stream=o, initial_indent=indent, |
| 296 |
"created at {}", |
| 297 |
chrono::DateTime::<chrono::Utc>::from(t) |
| 298 |
.format("%Y‑%m‑%d %H:%M:%S")); |
| 299 |
} else { |
| 300 |
wwriteln!(stream=o, initial_indent=indent, |
| 301 |
"creation time missing"); |
| 302 |
} |
| 303 |
|
| 304 |
let (depth, amount) = certification.trust_signature() |
| 305 |
.unwrap_or((0, sequoia_wot::FULLY_TRUSTED as u8)); |
| 306 |
|
| 307 |
if amount == 0 { |
| 308 |
wwriteln!(stream=o, initial_indent=indent, |
| 309 |
"{} was retracted", link); |
| 310 |
} else { |
| 311 |
let mut regex: Vec<_> = certification.regular_expressions() |
| 312 |
.map(|re| ui::Safe(re).to_string()) |
| 313 |
.collect(); |
| 314 |
regex.sort(); |
| 315 |
regex.dedup(); |
| 316 |
|
| 317 |
if depth > 0 { |
| 318 |
if amount == sequoia_wot::FULLY_TRUSTED as u8 |
| 319 |
&& regex.is_empty() |
| 320 |
{ |
| 321 |
wwriteln!(stream=o, initial_indent=indent, |
| 322 |
"{} as a fully trusted CA", linked); |
| 323 |
} else { |
| 324 |
wwriteln!(stream=o, initial_indent=indent, |
| 325 |
"{} as a partially trusted CA", linked); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
if let Some(e) = certification.signature_expiration_time() { |
| 330 |
wwriteln!(stream=o, initial_indent=indent, |
| 331 |
"expiration: {}", |
| 332 |
chrono::DateTime::<chrono::Utc>::from(e) |
| 333 |
.format("%Y‑%m‑%d")); |
| 334 |
} |
| 335 |
|
| 336 |
if depth != 0 && depth != 255 { |
| 337 |
wwriteln!(stream=o, initial_indent=indent, |
| 338 |
"trust depth: {}", depth); |
| 339 |
} |
| 340 |
|
| 341 |
if amount != sequoia_wot::FULLY_TRUSTED as u8 { |
| 342 |
wwriteln!(stream=o, initial_indent=indent, |
| 343 |
"trust amount: {}", amount); |
| 344 |
} |
| 345 |
|
| 346 |
if ! regex.is_empty() { |
| 347 |
wwriteln!(stream=o, initial_indent=indent, |
| 348 |
"regular expressions: {}", regex.join("; ")); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
Ok(()) |
| 353 |
} |