| 1 |
//! Changes a key's password. |
| 2 |
|
| 3 |
use anyhow::Context; |
| 4 |
|
| 5 |
use sequoia_openpgp as openpgp; |
| 6 |
use openpgp::Cert; |
| 7 |
|
| 8 |
use crate::Result; |
| 9 |
use crate::Sq; |
| 10 |
use crate::cli; |
| 11 |
use crate::common::NULL_POLICY; |
| 12 |
use crate::common::key::get_keys; |
| 13 |
use crate::common::key::password; |
| 14 |
use crate::sq::TrustThreshold; |
| 15 |
|
| 16 |
pub fn dispatch(sq: Sq, command: cli::key::password::Command) |
| 17 |
-> Result<()> |
| 18 |
{ |
| 19 |
let (cert, cert_source) |
| 20 |
= sq.resolve_cert(&command.cert, TrustThreshold::Full)?; |
| 21 |
|
| 22 |
// We require the certificate be valid under the standard policy. |
| 23 |
Cert::with_policy(&cert, sq.policy, sq.time) |
| 24 |
.with_context(|| { |
| 25 |
format!("The certificate {} is not valid under the \ |
| 26 |
current policy.", |
| 27 |
cert.fingerprint()) |
| 28 |
})?; |
| 29 |
|
| 30 |
// But we change the password for all keys with plausible |
| 31 |
// bindings. |
| 32 |
let vc = Cert::with_policy(&cert, NULL_POLICY, sq.time) |
| 33 |
.with_context(|| { |
| 34 |
format!("The certificate {} is not valid under the \ |
| 35 |
null policy.", |
| 36 |
cert.fingerprint()) |
| 37 |
})?; |
| 38 |
|
| 39 |
let kas = vc.keys().collect::<Vec<_>>(); |
| 40 |
let kas = kas.iter().collect::<Vec<_>>(); |
| 41 |
|
| 42 |
let to_change = get_keys(&sq, &cert_source, &kas, true)?; |
| 43 |
|
| 44 |
if to_change.is_empty() { |
| 45 |
return Err(anyhow::anyhow!( |
| 46 |
"{} does not contain any secret key material.", |
| 47 |
cert.fingerprint())); |
| 48 |
} |
| 49 |
|
| 50 |
password::password(sq, &cert, cert_source, to_change, |
| 51 |
command.clear_password, |
| 52 |
command.new_password_file.as_deref(), |
| 53 |
command.output, false) |
| 54 |
} |