| 1 |
//! Deletes secret key material. |
| 2 |
use anyhow::Context; |
| 3 |
|
| 4 |
use sequoia_openpgp as openpgp; |
| 5 |
use openpgp::Cert; |
| 6 |
use openpgp::Packet; |
| 7 |
use openpgp::Result; |
| 8 |
use openpgp::cert::amalgamation::key::PrimaryKey; |
| 9 |
use openpgp::cert::amalgamation::key::ValidErasedKeyAmalgamation; |
| 10 |
use openpgp::packet::key::KeyParts; |
| 11 |
use openpgp::serialize::Serialize; |
| 12 |
|
| 13 |
use sequoia_keystore as keystore; |
| 14 |
|
| 15 |
use crate::Sq; |
| 16 |
use crate::cli::types::FileOrStdout; |
| 17 |
use crate::cli::types::FileStdinOrKeyHandle; |
| 18 |
|
| 19 |
pub fn delete<'a, P>( |
| 20 |
sq: Sq, |
| 21 |
cert: &Cert, |
| 22 |
cert_source: FileStdinOrKeyHandle, |
| 23 |
to_delete: Vec<( |
| 24 |
&'a ValidErasedKeyAmalgamation<'a, P>, |
| 25 |
Option<Vec<keystore::Key>> |
| 26 |
)>, |
| 27 |
output: Option<FileOrStdout>, |
| 28 |
binary: bool) |
| 29 |
-> Result<()> |
| 30 |
where |
| 31 |
P: 'a + KeyParts, |
| 32 |
{ |
| 33 |
let ks = matches!(cert_source, FileStdinOrKeyHandle::KeyHandle(_)); |
| 34 |
if ks { |
| 35 |
// Delete the secret key material from the key store. |
| 36 |
for (ka, remote_keys) in to_delete.into_iter() { |
| 37 |
let remote_keys = remote_keys.expect("have remote keys"); |
| 38 |
assert!(! remote_keys.is_empty()); |
| 39 |
for (i, mut kh) in remote_keys.into_iter().enumerate() { |
| 40 |
if let Err(err) = kh.delete_secret_key_material() { |
| 41 |
if i > 0 { |
| 42 |
// We failed to delete the key. It could be |
| 43 |
// that when we deleted another instance of |
| 44 |
// the key, it deleted this instance. (The |
| 45 |
// softkey backend combines keys, and does |
| 46 |
// this.) |
| 47 |
if let Some(err) |
| 48 |
= err.downcast_ref::<keystore::Error>() |
| 49 |
{ |
| 50 |
if let keystore::Error::EOF = err { |
| 51 |
continue; |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
Err(err).with_context(|| { |
| 56 |
format!("Deleting {}", ka.key().fingerprint()) |
| 57 |
})?; |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
} else { |
| 62 |
// Strip the secret key material from the certificate. |
| 63 |
let mut stripped: Vec<Packet> = Vec::new(); |
| 64 |
|
| 65 |
for (ka, _) in to_delete.into_iter() { |
| 66 |
let key = ka.key().clone().parts_into_secret().expect("have a secret"); |
| 67 |
let pk = key.take_secret().0; |
| 68 |
if ka.primary() { |
| 69 |
stripped.push( |
| 70 |
Packet::PublicKey(pk.role_into_primary())); |
| 71 |
} else { |
| 72 |
stripped.push( |
| 73 |
Packet::PublicSubkey(pk.role_into_subordinate())); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
let cert = cert.clone().insert_packets( |
| 78 |
stripped.into_iter().map(|stripped| Packet::from(stripped)))?.0; |
| 79 |
|
| 80 |
let output = output.unwrap_or_else(|| FileOrStdout::new(None)); |
| 81 |
let mut output = output.for_secrets().create_safe(&sq)?; |
| 82 |
if binary { |
| 83 |
cert.as_tsk().serialize(&mut output)?; |
| 84 |
} else { |
| 85 |
cert.as_tsk().armored().serialize(&mut output)?; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
Ok(()) |
| 90 |
} |