| 1 |
use std::borrow::Cow; |
| 2 |
use std::cell::RefCell; |
| 3 |
use std::collections::BTreeMap; |
| 4 |
use std::collections::btree_map::Entry; |
| 5 |
|
| 6 |
use anyhow::Context; |
| 7 |
|
| 8 |
use sequoia_openpgp as openpgp; |
| 9 |
use openpgp::Cert; |
| 10 |
use openpgp::Fingerprint; |
| 11 |
use openpgp::Packet; |
| 12 |
use openpgp::Result; |
| 13 |
use openpgp::crypto; |
| 14 |
use openpgp::packet::signature::SignatureBuilder; |
| 15 |
use openpgp::policy::HashAlgoSecurity; |
| 16 |
use openpgp::policy::Policy; |
| 17 |
use openpgp::types::RevocationStatus; |
| 18 |
|
| 19 |
use crate::Sq; |
| 20 |
use crate::common::pki::certify::diff_certification; |
| 21 |
use crate::common::pki::list::summarize_certification; |
| 22 |
use crate::common::ui::emit_cert_userid_indent; |
| 23 |
use crate::common::ui; |
| 24 |
|
| 25 |
const TRACE: bool = false; |
| 26 |
|
| 27 |
// Concatenate two strings. |
| 28 |
fn strcat<'a>(a: &'a str, b: &'a str) -> Cow<'a, str> { |
| 29 |
if a.is_empty() { |
| 30 |
Cow::Borrowed(b) |
| 31 |
} else if b.is_empty() { |
| 32 |
Cow::Borrowed(a) |
| 33 |
} else { |
| 34 |
Cow::Owned(format!("{}{}", a, b)) |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/// Replays the active certifications made by source using target. |
| 39 |
/// |
| 40 |
/// If target already certified a binding, and the active |
| 41 |
/// certification has the same parameters as the certification being |
| 42 |
/// replayed, no new certification is created. |
| 43 |
/// |
| 44 |
/// Certifications are also not replayed if a binding is invalid (the |
| 45 |
/// certificate is not valid according to the policy, is revoked, or |
| 46 |
/// not live, or the user ID is revoked). |
| 47 |
/// |
| 48 |
/// If `certs` is provided, only certifications of the specified |
| 49 |
/// certificates are replyed. Otherwise, all certificates are |
| 50 |
/// considered. |
| 51 |
pub fn replay(sq: &Sq, o: &mut dyn std::io::Write, indent: &str, |
| 52 |
source: RefCell<Cert>, |
| 53 |
target: &Cert, |
| 54 |
target_signer: Option<&mut Box<dyn crypto::Signer + Send + Sync>>, |
| 55 |
certs: Option<&[RefCell<Cert>]>) |
| 56 |
-> Result<Vec<Cert>> |
| 57 |
{ |
| 58 |
tracer!(TRACE, "pki::replay"); |
| 59 |
|
| 60 |
let source_kh = source.borrow().key_handle(); |
| 61 |
let source_pk = source.borrow().primary_key().key().clone(); |
| 62 |
|
| 63 |
let target_pk = target.primary_key().key().clone(); |
| 64 |
|
| 65 |
// Get the signer to certify with (and fail early if its not |
| 66 |
// available). |
| 67 |
let mut signer_; |
| 68 |
let signer = if let Some(signer) = target_signer { |
| 69 |
signer |
| 70 |
} else { |
| 71 |
signer_ = sq.get_certification_key(target, None)?; |
| 72 |
&mut signer_ |
| 73 |
}; |
| 74 |
|
| 75 |
// Get all of the certifications that the source made. |
| 76 |
let certifications = if let Some(certs) = certs { |
| 77 |
let mut results = Vec::new(); |
| 78 |
for cert in certs { |
| 79 |
results.append( |
| 80 |
&mut crate::common::pki::list::list_certifications( |
| 81 |
&sq, Some(RefCell::clone(&source)), |
| 82 |
Some(RefCell::clone(cert)))?); |
| 83 |
} |
| 84 |
results |
| 85 |
} else { |
| 86 |
crate::common::pki::list::list_certifications( |
| 87 |
&sq, Some(RefCell::clone(&source)), None)? |
| 88 |
}; |
| 89 |
|
| 90 |
// Drop the certifier (its always source) and convert the |
| 91 |
// certifications to a vector so that we can merge them. |
| 92 |
let mut certifications = certifications |
| 93 |
.into_iter() |
| 94 |
.map(|(certifier, cert, userid, certification)| { |
| 95 |
assert_eq!(certifier.key_handle(), source_kh); |
| 96 |
|
| 97 |
(cert, userid, vec![ certification ]) |
| 98 |
}) |
| 99 |
.collect::<Vec<_>>(); |
| 100 |
|
| 101 |
// Sort by certificate and user ID, then merge. |
| 102 |
certifications.sort_by( |
| 103 |
|(a_cert, a_userid, _a_certifications), |
| 104 |
(b_cert, b_userid, _b_certifications)| |
| 105 |
{ |
| 106 |
a_cert.borrow().fingerprint() |
| 107 |
.cmp(&b_cert.borrow().fingerprint()) |
| 108 |
.then_with(|| { |
| 109 |
a_userid.cmp(&b_userid) |
| 110 |
}) |
| 111 |
}); |
| 112 |
certifications.dedup_by( |
| 113 |
|(a_cert, a_userid, a_certifications), |
| 114 |
(b_cert, b_userid, b_certifications)| { |
| 115 |
if a_cert.borrow().fingerprint() == b_cert.borrow().fingerprint() |
| 116 |
&& a_userid == b_userid |
| 117 |
{ |
| 118 |
b_certifications.append(a_certifications); |
| 119 |
true |
| 120 |
} else { |
| 121 |
false |
| 122 |
} |
| 123 |
}); |
| 124 |
|
| 125 |
// The signed certificates. |
| 126 |
let mut results: BTreeMap<Fingerprint, Cert> = BTreeMap::new(); |
| 127 |
|
| 128 |
'next_binding: for (cert, userid, mut certifications) |
| 129 |
in certifications.into_iter() |
| 130 |
{ |
| 131 |
let cert = cert.borrow(); |
| 132 |
|
| 133 |
if cert.fingerprint() == target.fingerprint() { |
| 134 |
// `source` certified `target`. `target` does not need to |
| 135 |
// certify itself. |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
t!("{}: {}, {} certifications", |
| 140 |
cert.fingerprint(), |
| 141 |
String::from_utf8_lossy(userid.value()), |
| 142 |
certifications.len()); |
| 143 |
|
| 144 |
wwriteln!(stream = o, indent = indent, |
| 145 |
"Considering the source certificate's certification \ |
| 146 |
of the binding:"); |
| 147 |
emit_cert_userid_indent(o, indent, &cert, &userid)?; |
| 148 |
|
| 149 |
// Skip if cert is invalid, expired, or revoked, or |
| 150 |
// user ID is revoked. |
| 151 |
let vc = match cert.with_policy(sq.policy, sq.time) { |
| 152 |
Ok(vc) => vc, |
| 153 |
Err(err) => { |
| 154 |
wwriteln!(stream = o, |
| 155 |
indent = strcat(indent, " "), |
| 156 |
"Certificate is not valid according to the \ |
| 157 |
policy: {}", |
| 158 |
crate::one_line_error_chain(err)); |
| 159 |
continue; |
| 160 |
} |
| 161 |
}; |
| 162 |
|
| 163 |
if let RevocationStatus::Revoked(sigs) = vc.revocation_status() { |
| 164 |
let sig = sigs.into_iter().next().expect("have one"); |
| 165 |
if let Some((reason, message)) = sig.reason_for_revocation() |
| 166 |
{ |
| 167 |
wwriteln!(stream = o, |
| 168 |
initial_indent = strcat(indent, " "), |
| 169 |
"Certificate was revoked: {}, {}", |
| 170 |
reason, ui::Safe(message)); |
| 171 |
} else { |
| 172 |
wwriteln!(stream = o, |
| 173 |
initial_indent = strcat(indent, " - "), |
| 174 |
"Certificate was revoked"); |
| 175 |
} |
| 176 |
|
| 177 |
continue; |
| 178 |
} |
| 179 |
|
| 180 |
if let Err(err) = vc.alive() { |
| 181 |
wwriteln!(stream = o, |
| 182 |
initial_indent = strcat(indent, " "), |
| 183 |
"Certificate is not live: {}", err); |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
let ua = cert.userids().find(|u| u.userid() == &userid); |
| 188 |
if let Some(ua) = ua.as_ref() { |
| 189 |
if let RevocationStatus::Revoked(sigs) |
| 190 |
= ua.revocation_status(sq.policy, sq.time) |
| 191 |
{ |
| 192 |
let sig = sigs.into_iter().next().expect("have one"); |
| 193 |
if let Some((reason, message)) = sig.reason_for_revocation() |
| 194 |
{ |
| 195 |
wwriteln!(stream = o, |
| 196 |
initial_indent = strcat(indent, " "), |
| 197 |
"User ID was revoked: {}, {}", |
| 198 |
reason, ui::Safe(message)); |
| 199 |
} else { |
| 200 |
wwriteln!(stream = o, |
| 201 |
initial_indent = strcat(indent, " - "), |
| 202 |
"User ID was revoked"); |
| 203 |
} |
| 204 |
|
| 205 |
continue; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
certifications.sort_by(|a, b| { |
| 210 |
a.signature_creation_time().cmp(&b.signature_creation_time()) |
| 211 |
// Newest first. |
| 212 |
.reverse() |
| 213 |
.then_with(|| { |
| 214 |
// Break ties deterministically using the MPIs. |
| 215 |
a.mpis().cmp(&b.mpis()) |
| 216 |
}) |
| 217 |
}); |
| 218 |
|
| 219 |
let mut reasons = Vec::new(); |
| 220 |
let mut good = false; |
| 221 |
|
| 222 |
// Iterate over the certifications created by the source. |
| 223 |
for certification in certifications.into_iter() { |
| 224 |
let ct = if let Some(ct) = certification.signature_creation_time() { |
| 225 |
ct |
| 226 |
} else { |
| 227 |
// No creation time => invalid signature. |
| 228 |
t!("Ignoring invalid certification: \ |
| 229 |
no signature creation time."); |
| 230 |
continue; |
| 231 |
}; |
| 232 |
let ct_str = chrono::DateTime::<chrono::Utc>::from(ct) |
| 233 |
.format("%Y‑%m‑%d %H:%M:%S") |
| 234 |
.to_string(); |
| 235 |
|
| 236 |
if let Err(err) = certification.signature_alive(sq.time, None) { |
| 237 |
t!("Not considering certification made at {}: {}", |
| 238 |
ct_str, |
| 239 |
err); |
| 240 |
reasons.push((ct_str, err)); |
| 241 |
} else if let Err(err) = sq.policy.signature( |
| 242 |
&certification, |
| 243 |
HashAlgoSecurity::CollisionResistance) |
| 244 |
{ |
| 245 |
// Policy violation. |
| 246 |
t!("Certification made at {} by {} on {}, {} is invalid: {}", |
| 247 |
ct_str, |
| 248 |
source_kh, |
| 249 |
cert.fingerprint(), |
| 250 |
String::from_utf8_lossy(userid.value()), |
| 251 |
err); |
| 252 |
|
| 253 |
reasons.push((ct_str, err)); |
| 254 |
} else if let Err(err) = certification.verify_userid_binding( |
| 255 |
&source_pk, cert.primary_key().key(), &userid) |
| 256 |
{ |
| 257 |
// Invalid signature. |
| 258 |
t!("Certification made at {} by {} on {}, {} is invalid: {}", |
| 259 |
ct_str, |
| 260 |
source_pk, |
| 261 |
cert.fingerprint(), |
| 262 |
String::from_utf8_lossy(userid.value()), |
| 263 |
err); |
| 264 |
|
| 265 |
reasons.push((ct_str, err)); |
| 266 |
} else { |
| 267 |
wwriteln!(stream=o, |
| 268 |
indent=strcat(indent, " "), |
| 269 |
"Source certificate's active certification:"); |
| 270 |
summarize_certification(o, &strcat(indent, " "), |
| 271 |
&certification, false)?; |
| 272 |
|
| 273 |
// Check that the target hasn't already certified the |
| 274 |
// target with the same parameters. |
| 275 |
let builder: SignatureBuilder = certification.into(); |
| 276 |
|
| 277 |
if let Some(ua) = ua.as_ref() { |
| 278 |
for preexisting in ua.active_certifications_by_key( |
| 279 |
sq.policy, sq.time, &target_pk) |
| 280 |
{ |
| 281 |
let preexisting_ct = if let Some(ct) |
| 282 |
= preexisting.signature_creation_time() |
| 283 |
{ |
| 284 |
ct |
| 285 |
} else { |
| 286 |
// No signature creation time: invalid. |
| 287 |
continue; |
| 288 |
}; |
| 289 |
|
| 290 |
// When checking for differences, use the |
| 291 |
// preexisting creation time and not `sq.time` |
| 292 |
// as we don't want the creation time or the |
| 293 |
// expiration time to flag a difference. |
| 294 |
let mut trace: Vec<u8> = Vec::new(); |
| 295 |
let mut sink = std::io::sink(); |
| 296 |
if ! diff_certification( |
| 297 |
if TRACE { &mut trace } else { &mut sink }, |
| 298 |
preexisting, &builder, preexisting_ct) |
| 299 |
{ |
| 300 |
let p_ct = preexisting.signature_creation_time() |
| 301 |
.expect("valid signature"); |
| 302 |
let p_ct_str = chrono::DateTime::<chrono::Utc>::from(p_ct) |
| 303 |
.format("%Y‑%m‑%d %H:%M:%S") |
| 304 |
.to_string(); |
| 305 |
wwriteln!(stream = o, |
| 306 |
initial_indent = strcat(indent, " "), |
| 307 |
"Skipping: the target already \ |
| 308 |
certified the binding with the same \ |
| 309 |
parameters at {}.", |
| 310 |
p_ct_str); |
| 311 |
t!("Differences: {}", |
| 312 |
String::from_utf8_lossy(&trace)); |
| 313 |
continue 'next_binding; |
| 314 |
} |
| 315 |
t!("Differences: {}", |
| 316 |
String::from_utf8_lossy(&trace)); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
// The expiration is not an absolute time, but a |
| 321 |
// period that is relative to the creation time. This |
| 322 |
// means that if the new certification's creation time |
| 323 |
// is a year later, and we leave the expiration period |
| 324 |
// as is, the new certification will expire a year |
| 325 |
// later. |
| 326 |
// |
| 327 |
// Should we adjust the expiration period? By using |
| 328 |
// this command, the user is explicitly opting in to |
| 329 |
// refreshing the certification. So, we leave it as |
| 330 |
// is. |
| 331 |
|
| 332 |
let builder = builder.set_signature_creation_time(sq.time)?; |
| 333 |
|
| 334 |
let sig = builder.sign_userid_binding( |
| 335 |
signer, |
| 336 |
cert.primary_key().key(), |
| 337 |
&userid) |
| 338 |
.with_context(|| { |
| 339 |
format!("Creating certification for {}, {}", |
| 340 |
cert.fingerprint(), |
| 341 |
ui::Safe(String::from_utf8_lossy(userid.value()))) |
| 342 |
})?; |
| 343 |
|
| 344 |
let packets = [ |
| 345 |
Packet::from(userid.clone()), |
| 346 |
sig.into(), |
| 347 |
]; |
| 348 |
|
| 349 |
match results.entry(cert.fingerprint()) { |
| 350 |
Entry::Occupied(oe) => { |
| 351 |
let c = oe.into_mut(); |
| 352 |
*c = c.clone().insert_packets(packets.into_iter())?.0; |
| 353 |
} |
| 354 |
Entry::Vacant(vc) => { |
| 355 |
vc.insert(cert.clone() |
| 356 |
.insert_packets(packets.into_iter())?.0); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
wwriteln!(stream = o, indent = strcat(indent, " "), |
| 361 |
"Replayed certification."); |
| 362 |
|
| 363 |
good = true; |
| 364 |
break; |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
if ! good { |
| 369 |
wwriteln!(stream = o, indent = strcat(indent, " "), |
| 370 |
"Warning: {} certified {}, {} but none of the \ |
| 371 |
certifications are usable:", |
| 372 |
source_kh, |
| 373 |
cert.fingerprint(), |
| 374 |
String::from_utf8_lossy(userid.value())); |
| 375 |
for (ct, err) in reasons.into_iter() { |
| 376 |
wwriteln!(stream = o, indent = strcat(indent, " "), |
| 377 |
"Certification made at {} is invalid: {}", |
| 378 |
ct, crate::one_line_error_chain(err)); |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
if results.is_empty() { |
| 384 |
wwriteln!(stream=o, indent = indent, "Nothing to replay"); |
| 385 |
} |
| 386 |
|
| 387 |
return Ok(results.into_values().collect()); |
| 388 |
} |