| 1 |
use std::cell::RefCell; |
| 2 |
use std::collections::BTreeSet; |
| 3 |
use std::sync::Arc; |
| 4 |
|
| 5 |
use sequoia_openpgp as openpgp; |
| 6 |
use openpgp::Result; |
| 7 |
use openpgp::packet::UserID; |
| 8 |
use openpgp::serialize::Serialize; |
| 9 |
|
| 10 |
use sequoia_cert_store as cert_store; |
| 11 |
use cert_store::StoreUpdate; |
| 12 |
|
| 13 |
use crate::Sq; |
| 14 |
use crate::cli::pki::vouch::replay; |
| 15 |
use crate::output::sanitize::Safe; |
| 16 |
use crate::sq::TrustThreshold; |
| 17 |
|
| 18 |
const TRACE: bool = false; |
| 19 |
|
| 20 |
pub fn replay(sq: Sq, c: replay::Command) |
| 21 |
-> Result<()> |
| 22 |
{ |
| 23 |
tracer!(TRACE, "sq::pki::vouch::replay"); |
| 24 |
|
| 25 |
let source = |
| 26 |
sq.resolve_cert(&c.source, TrustThreshold::Full)?.0; |
| 27 |
|
| 28 |
let target = |
| 29 |
sq.resolve_cert(&c.target, TrustThreshold::Full)?.0; |
| 30 |
|
| 31 |
t!("Replaying certifications made by {} with {}", |
| 32 |
source.fingerprint(), target.fingerprint()); |
| 33 |
|
| 34 |
if ! c.allow_dissimilar_userids { |
| 35 |
// Check that the certificates have a self-signed user ID in |
| 36 |
// common. |
| 37 |
let source_vc = source.with_policy(sq.policy, sq.time)?; |
| 38 |
let target_vc = target.with_policy(sq.policy, sq.time)?; |
| 39 |
let source_userids: BTreeSet<&UserID> |
| 40 |
= source_vc.userids().map(|ua| ua.userid()).collect(); |
| 41 |
let target_userids: BTreeSet<&UserID> |
| 42 |
= target_vc.userids().map(|ua| ua.userid()).collect(); |
| 43 |
|
| 44 |
if source_userids.intersection(&target_userids).next().is_none() { |
| 45 |
weprintln!("The source and target certificates don't share a \ |
| 46 |
self-signed user ID. Normally you don't want to \ |
| 47 |
replay certifications made by someone else. If you \ |
| 48 |
are sure, pass --allow-dissimilar-userids to \ |
| 49 |
disable this check."); |
| 50 |
weprintln!("Source certificate's self-signed user IDs:"); |
| 51 |
for userid in source_userids { |
| 52 |
weprintln!(initial_indent = " - ", "{}", Safe(userid)); |
| 53 |
} |
| 54 |
weprintln!("Target certificate's self-signed user IDs:"); |
| 55 |
for userid in target_userids { |
| 56 |
weprintln!(initial_indent = " - ", "{}", Safe(userid)); |
| 57 |
} |
| 58 |
|
| 59 |
return Err(anyhow::anyhow!( |
| 60 |
"Cowardly refusing to replay the certifications. The source \ |
| 61 |
and target certificates appear to be for different entities.")); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
let o = &mut std::io::stderr(); |
| 66 |
|
| 67 |
let source = RefCell::new(source); |
| 68 |
let results = crate::common::pki::replay::replay( |
| 69 |
&sq, o, "", RefCell::clone(&source), &target, None, None)?; |
| 70 |
|
| 71 |
if results.is_empty() { |
| 72 |
return Ok(()); |
| 73 |
} |
| 74 |
|
| 75 |
if let Some(output) = c.output { |
| 76 |
// And export it. |
| 77 |
let path = output.path().map(Clone::clone); |
| 78 |
let mut message = output.create_pgp_safe( |
| 79 |
&sq, |
| 80 |
false, // binary |
| 81 |
sequoia_openpgp::armor::Kind::PublicKey)?; |
| 82 |
for cert in results.into_iter() { |
| 83 |
cert.serialize(&mut message)?; |
| 84 |
} |
| 85 |
message.finalize()?; |
| 86 |
|
| 87 |
if let Some(path) = path { |
| 88 |
sq.hint(format_args!( |
| 89 |
"Updated certificates written to {}. \ |
| 90 |
To make the updates effective, they have to be published \ |
| 91 |
so that others can find them, for example using:", |
| 92 |
path.display())) |
| 93 |
.sq().arg("network").arg("keyserver").arg("publish") |
| 94 |
.arg_value("--cert-file", path.display()) |
| 95 |
.done(); |
| 96 |
} else { |
| 97 |
sq.hint(format_args!( |
| 98 |
"To make the updates effective, they have to be published \ |
| 99 |
so that others can find them.")); |
| 100 |
} |
| 101 |
} else { |
| 102 |
// Import it. |
| 103 |
let cert_store = sq.cert_store_or_else()?; |
| 104 |
|
| 105 |
let mut hint = sq.hint(format_args!( |
| 106 |
"Imported updated certificates into the cert store. \ |
| 107 |
To make the updates effective, they have to be published \ |
| 108 |
so that others can find them, for example using:")) |
| 109 |
.sq().arg("network").arg("keyserver").arg("publish"); |
| 110 |
|
| 111 |
for cert in results.into_iter() { |
| 112 |
let fpr = cert.fingerprint(); |
| 113 |
|
| 114 |
if let Err(err) = cert_store.update(Arc::new(cert.into())) { |
| 115 |
weprintln!("Error importing updated certificate: {}", err); |
| 116 |
return Err(err); |
| 117 |
} |
| 118 |
hint = hint.arg_value("--cert", fpr) |
| 119 |
} |
| 120 |
|
| 121 |
hint.done(); |
| 122 |
} |
| 123 |
|
| 124 |
Ok(()) |
| 125 |
} |