| 1 |
use anyhow::Context; |
| 2 |
|
| 3 |
use sequoia_openpgp as openpgp; |
| 4 |
use openpgp::Cert; |
| 5 |
use openpgp::Packet; |
| 6 |
use openpgp::cert::amalgamation::key::PrimaryKey; |
| 7 |
use openpgp::serialize::Serialize; |
| 8 |
|
| 9 |
use crate::Result; |
| 10 |
use crate::Sq; |
| 11 |
use crate::cli; |
| 12 |
use crate::sq::TrustThreshold; |
| 13 |
|
| 14 |
pub fn dispatch(sq: Sq, command: cli::key::export::Command) |
| 15 |
-> Result<()> |
| 16 |
{ |
| 17 |
let ks = sq.key_store_or_else()?; |
| 18 |
let mut ks = ks.lock().unwrap(); |
| 19 |
|
| 20 |
let certs = sq.resolve_certs_or_fail( |
| 21 |
&command.certs, TrustThreshold::Full)?; |
| 22 |
|
| 23 |
// Note: Sq::resolve_certs already deduped the certificates. |
| 24 |
let mut results = Vec::new(); |
| 25 |
|
| 26 |
for cert in certs.into_iter() { |
| 27 |
let vc = Cert::with_policy(&cert, sq.policy, sq.time) |
| 28 |
.with_context(|| { |
| 29 |
format!("The certificate {} is not valid under the \ |
| 30 |
current policy. Use sq key subkey export --key \ |
| 31 |
to export specific keys.", |
| 32 |
cert.fingerprint()) |
| 33 |
})?; |
| 34 |
|
| 35 |
let mut secret_keys: Vec<Packet> = Vec::new(); |
| 36 |
let mut errs = Vec::new(); |
| 37 |
|
| 38 |
for ka in vc.keys().into_iter().collect::<Vec<_>>() { |
| 39 |
if ka.key().has_secret() { |
| 40 |
// We already have the secret key material. |
| 41 |
continue; |
| 42 |
} |
| 43 |
|
| 44 |
let key_handle = ka.key().key_handle(); |
| 45 |
|
| 46 |
for mut remote in ks.find_key(key_handle)? { |
| 47 |
match remote.export() { |
| 48 |
Ok(secret_key) => { |
| 49 |
if ka.primary() { |
| 50 |
secret_keys.push( |
| 51 |
secret_key.role_into_primary().into()); |
| 52 |
} else { |
| 53 |
secret_keys.push( |
| 54 |
secret_key.role_into_subordinate().into()); |
| 55 |
} |
| 56 |
break; |
| 57 |
} |
| 58 |
Err(err) => { |
| 59 |
errs.push((ka.key().fingerprint(), err)); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
if secret_keys.is_empty() { |
| 66 |
for (fpr, err) in errs.into_iter() { |
| 67 |
weprintln!("Exporting {}: {}", fpr, err); |
| 68 |
} |
| 69 |
return Err(anyhow::anyhow!( |
| 70 |
"Failed to export {}: no secret key material is available", |
| 71 |
cert.fingerprint())); |
| 72 |
} |
| 73 |
|
| 74 |
let cert = cert.insert_packets(secret_keys)?.0; |
| 75 |
results.push(cert); |
| 76 |
} |
| 77 |
|
| 78 |
let mut output = command.output.for_secrets().create_safe(&sq)?; |
| 79 |
|
| 80 |
if false { |
| 81 |
for cert in results.into_iter() { |
| 82 |
cert.as_tsk().export(&mut output) |
| 83 |
.with_context(|| { |
| 84 |
format!("Serializing {}", cert.fingerprint()) |
| 85 |
})?; |
| 86 |
} |
| 87 |
} else { |
| 88 |
let mut output = openpgp::armor::Writer::new( |
| 89 |
output, |
| 90 |
openpgp::armor::Kind::SecretKey)?; |
| 91 |
|
| 92 |
for cert in results.into_iter() { |
| 93 |
cert.as_tsk().export(&mut output) |
| 94 |
.with_context(|| { |
| 95 |
format!("Serializing {}", cert.fingerprint()) |
| 96 |
})?; |
| 97 |
} |
| 98 |
|
| 99 |
output.finalize()?; |
| 100 |
} |
| 101 |
|
| 102 |
Ok(()) |
| 103 |
} |