Debian Codegraph @

src/commands/key/delete.rs

raw
1 //! Deletes all of a certificate's secret key material.
2
3 use anyhow::Context;
4
5 use sequoia_openpgp as openpgp;
6 use openpgp::Cert;
7 use openpgp::cert::amalgamation::ValidateAmalgamation;
8 use openpgp::cert::amalgamation::key::PrimaryKey;
9
10 use crate::Result;
11 use crate::Sq;
12 use crate::cli;
13 use crate::common::NULL_POLICY;
14 use crate::common::key::delete;
15 use crate::common::key::get_keys;
16 use crate::sq::TrustThreshold;
17
18 pub fn dispatch(sq: Sq, command: cli::key::delete::Command)
19 -> Result<()>
20 {
21 let (cert, cert_source)
22 = sq.resolve_cert(&command.cert, TrustThreshold::Full)?;
23
24 // Fail if the certificate is not valid under the current policy.
25 Cert::with_policy(&cert, sq.policy, sq.time)
26 .with_context(|| {
27 sq.hint(format_args!(
28 "The certificate {} is not valid under the \
29 current policy. You can still delete individual \
30 keys using `sq key subkey delete`.",
31 cert.fingerprint()));
32
33 format!("The certificate {} is not valid under the \
34 current policy.",
35 cert.fingerprint())
36 })?;
37
38 // We want to delete all secret key material associated with the
39 // certificate, but we don't want to delete secret key material
40 // that we are not confident belongs to the certificate.
41 //
42 // Imagine Alice creates a new certificate. Mallory see this, and
43 // anticipates that she is going to delete the old certificate.
44 // He attaches her new encryption-capable subkey to the old
45 // certificate using some weak cryptography, publishes it, and
46 // then Alice gets the update to her old certificate via
47 // parcimonie. When she deletes the secret key material
48 // associated with the old certificate, she would also delete her
49 // new secret key material. Ouch! Admittedly, this attack is a
50 // bit contrived.
51 //
52 // Alternatively, we could skip subkeys whose bindings rely on
53 // weak cryptography. This behavior would probably surprise most
54 // users. It could have serious consequences as well, since the
55 // user thought they deleted the secret key material, but didn't.
56 //
57 // Instead, we are conservative: if a subkey's binding signature
58 // relies on weak cryptography AND we have secret key material for
59 // it, we abort, and suggest using `sq key subkey delete` instead.
60
61 // Get all keys valid under the NULL policy.
62 let nc = Cert::with_policy(&cert, NULL_POLICY, sq.time)
63 .with_context(|| {
64 format!("The certificate {} is not valid under the \
65 null policy.",
66 cert.fingerprint())
67 })?;
68
69 let kas = nc.keys().collect::<Vec<_>>();
70 let kas = kas.iter().collect::<Vec<_>>();
71 let to_delete = get_keys(&sq, &cert_source, &kas, true)?;
72
73 // Go through the keys with secret key material, and make sure
74 // their binding is valid under the current policy.
75 let mut bad = Vec::new();
76 for (ka, _remote) in to_delete.iter() {
77 if ka.primary() {
78 // We check that the primary key is valid above.
79 continue;
80 }
81 if let Err(err) = ka.with_policy(sq.policy, sq.time) {
82 bad.push((ka.key().fingerprint(), err));
83 }
84 }
85 if ! bad.is_empty() {
86 weprintln!("Some keys are not valid according \
87 to the current policy:");
88 for (fpr, err) in bad.into_iter() {
89 weprintln!(" - {}: {}",
90 fpr,
91 crate::one_line_error_chain(err));
92 }
93 weprintln!("Cowardly refusing to delete all of the secret key \
94 material to avoid accidentally losing data. Use \
95 `sq key subkey delete` to delete the keys individually.");
96
97 return Err(anyhow::anyhow!(
98 "The authenticity of some subkeys is uncertain."));
99 }
100
101 if to_delete.is_empty() {
102 return Err(anyhow::anyhow!(
103 "{} does not contain any secret key material.",
104 cert.fingerprint()));
105 }
106
107 if cert_source.is_key_handle() {
108 let mut die = false;
109
110 // Make sure this is not ambiguous.
111 for (ka, _remote) in to_delete.iter() {
112 if let Ok(certs) = sq.lookup_with_policy(
113 std::iter::once(ka.key().key_handle()),
114 None,
115 true,
116 true,
117 NULL_POLICY,
118 sq.time)
119 {
120 if certs.len() > 1 {
121 die = true;
122 weprintln!("{} is associated with multiple certificates:",
123 ka.key().fingerprint());
124 for cert in certs.iter() {
125 weprintln!(" - {}", cert.fingerprint());
126 }
127 }
128 }
129 }
130
131 if die {
132 weprintln!("Cowardly refusing to delete secret key material to \
133 avoid accidentally losing data. Use \
134 `sq key subkey delete` to delete the keys \
135 individually.");
136
137 return Err(anyhow::anyhow!(
138 "Some keys are associated with multiple certificates."));
139 }
140 }
141
142 delete::delete(sq, &cert, cert_source, to_delete,
143 command.output, false)
144 }