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