| 1 |
use sequoia_openpgp as openpgp; |
| 2 |
use openpgp::Cert; |
| 3 |
use openpgp::Result; |
| 4 |
use openpgp::cert::amalgamation::key::ValidErasedKeyAmalgamation; |
| 5 |
use openpgp::packet::key::KeyParts; |
| 6 |
|
| 7 |
use sequoia_keystore as keystore; |
| 8 |
|
| 9 |
use crate::Sq; |
| 10 |
use crate::cli::types::FileStdinOrKeyHandle; |
| 11 |
|
| 12 |
|
| 13 |
mod expire; |
| 14 |
pub use expire::expire; |
| 15 |
|
| 16 |
pub mod delete; |
| 17 |
pub use delete::delete; |
| 18 |
|
| 19 |
pub mod password; |
| 20 |
pub use password::password; |
| 21 |
|
| 22 |
/// Looks up the certificate and returns the specified keys. |
| 23 |
/// |
| 24 |
/// If `keys` is empty, all keys are returned. |
| 25 |
/// |
| 26 |
/// If the keys are loaded from the certificate store, then a handle |
| 27 |
/// to the key is returned for each key. Otherwise, the secret key is |
| 28 |
/// returned. |
| 29 |
/// |
| 30 |
/// An error is returned if a key is not found, or there is no known |
| 31 |
/// secret key material. |
| 32 |
/// |
| 33 |
/// The returned keys are not unlocked. |
| 34 |
pub fn get_keys<'a, P>( |
| 35 |
sq: &Sq, |
| 36 |
cert_source: &FileStdinOrKeyHandle, |
| 37 |
kas: &[&'a ValidErasedKeyAmalgamation<'a, P>], |
| 38 |
ignore_missing: bool) |
| 39 |
-> Result<Vec<(&'a ValidErasedKeyAmalgamation<'a, P>, |
| 40 |
Option<Vec<keystore::Key>>)>> |
| 41 |
where |
| 42 |
P: 'a + KeyParts, |
| 43 |
{ |
| 44 |
let mut list: Vec<(&ValidErasedKeyAmalgamation<'a, P>, Option<_>)> |
| 45 |
= Vec::new(); |
| 46 |
|
| 47 |
let mut no_secret_key_material_count = 0; |
| 48 |
|
| 49 |
let warning = if ignore_missing { |
| 50 |
"Warning: " |
| 51 |
} else { |
| 52 |
"" |
| 53 |
}; |
| 54 |
|
| 55 |
match cert_source { |
| 56 |
FileStdinOrKeyHandle::FileOrStdin(ref _file) => { |
| 57 |
// If it is not a TSK, there is nothing to do. |
| 58 |
for ka in kas.into_iter() { |
| 59 |
let no_secret_key_material = ! ka.key().has_secret(); |
| 60 |
if no_secret_key_material { |
| 61 |
weprintln!("{}{} does not contain any secret key material", |
| 62 |
warning, ka.key().fingerprint()); |
| 63 |
no_secret_key_material_count += 1; |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
list.push((ka, None)); |
| 68 |
} |
| 69 |
} |
| 70 |
FileStdinOrKeyHandle::KeyHandle(ref _kh) => { |
| 71 |
let ks = sq.key_store_or_else()?; |
| 72 |
let mut ks = ks.lock().unwrap(); |
| 73 |
|
| 74 |
for ka in kas.into_iter() { |
| 75 |
let remote_keys = ks.find_key(ka.key().key_handle())?; |
| 76 |
let no_secret_key_material = remote_keys.is_empty(); |
| 77 |
|
| 78 |
if no_secret_key_material { |
| 79 |
weprintln!("{}{} does not contain any secret key material", |
| 80 |
warning, ka.key().fingerprint()); |
| 81 |
no_secret_key_material_count += 1; |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
list.push((ka, Some(remote_keys))); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if ! ignore_missing { |
| 91 |
if no_secret_key_material_count > 1 { |
| 92 |
// Plural. |
| 93 |
return Err(anyhow::anyhow!( |
| 94 |
"{} of the specified keys don't have secret key material", |
| 95 |
no_secret_key_material_count)); |
| 96 |
} else if no_secret_key_material_count > 0 { |
| 97 |
// Singular. |
| 98 |
return Err(anyhow::anyhow!( |
| 99 |
"{} of the specified keys doesn't have secret key material", |
| 100 |
no_secret_key_material_count)); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
Ok(list) |
| 105 |
} |
| 106 |
|
| 107 |
/// Certifies the newly created key with a per-host shadow CA that |
| 108 |
/// marks the origin. |
| 109 |
/// |
| 110 |
/// This also has the benefit that newly created keys also show up in |
| 111 |
/// the cert listing. |
| 112 |
pub fn certify_generated<'store, 'rstore>(sq: &mut Sq<'store, 'rstore>, |
| 113 |
cert: &Cert) |
| 114 |
-> Result<Cert> |
| 115 |
{ |
| 116 |
use crate::commands::network::certify_downloads; |
| 117 |
|
| 118 |
let hostname = |
| 119 |
gethostname::gethostname().to_string_lossy().to_string(); |
| 120 |
let certd = sq.certd_or_else()?; |
| 121 |
|
| 122 |
let (ca, _created) = certd.shadow_ca( |
| 123 |
&format!("generated_on_{}", hostname), |
| 124 |
true, |
| 125 |
format!("Generated on {}", hostname), |
| 126 |
1, |
| 127 |
&[])?; |
| 128 |
|
| 129 |
Ok(certify_downloads(sq, false, ca, vec![cert.clone()], None) |
| 130 |
.into_iter().next().expect("exactly one")) |
| 131 |
} |