Debian Codegraph @

src/commands/key/subkey/revoke.rs

raw
1 use std::collections::BTreeSet;
2
3 use anyhow::Context;
4
5 use sequoia_openpgp as openpgp;
6 use openpgp::Cert;
7 use openpgp::Packet;
8 use openpgp::cert::SubkeyRevocationBuilder;
9 use openpgp::cert::amalgamation::key::ValidErasedKeyAmalgamation;
10 use openpgp::packet::signature::subpacket::NotationData;
11 use openpgp::packet::{Key, Signature, key};
12 use openpgp::types::ReasonForRevocation;
13
14 use crate::Result;
15 use crate::Sq;
16 use crate::common::NULL_POLICY;
17 use crate::common::RevocationOutput;
18 use crate::common::get_secret_signer;
19 use crate::sq::TrustThreshold;
20
21 /// Handle the revocation of a subkey
22 struct SubkeyRevocation {
23 cert: Cert,
24 revoker: Cert,
25 revocations: Vec<(Key<key::PublicParts, key::SubordinateRole>, Signature)>,
26 }
27
28 impl SubkeyRevocation {
29 /// Create a new SubkeyRevocation
30 pub fn new(
31 sq: &Sq,
32 kas: &[ValidErasedKeyAmalgamation<key::PublicParts>],
33 cert: &Cert,
34 revoker: Option<Cert>,
35 reason: ReasonForRevocation,
36 message: &str,
37 notations: &[(bool, NotationData)],
38 ) -> Result<Self> {
39 let (revoker, mut signer)
40 = get_secret_signer(sq, &cert, revoker.as_ref())?;
41
42 let mut revocations = Vec::new();
43 let mut revoked = BTreeSet::new();
44 for ka in kas {
45 // Did we already revoke it?
46 if revoked.contains(&ka.key().fingerprint()) {
47 continue;
48 }
49 revoked.insert(ka.key().fingerprint());
50
51 let mut rev = SubkeyRevocationBuilder::new()
52 .set_reason_for_revocation(reason, message.as_bytes())?;
53 rev = rev.set_signature_creation_time(sq.time)?;
54 for (critical, notation) in notations {
55 rev = rev.add_notation(
56 notation.name(),
57 notation.value(),
58 Some(notation.flags().clone()),
59 *critical,
60 )?;
61 }
62 let key = ka.key().clone().role_into_subordinate();
63 let rev = rev.build(&mut signer, &cert, &key, None)?;
64 revocations.push((key, rev));
65 }
66
67 Ok(SubkeyRevocation {
68 cert: cert.clone(),
69 revoker,
70 revocations,
71 })
72 }
73 }
74
75 impl RevocationOutput for SubkeyRevocation {
76 fn cert(&self) -> Result<Cert> {
77 Cert::from_packets(
78 std::iter::once(
79 Packet::from(self.cert.primary_key().key().clone()))
80 .chain(self.revocations.iter().flat_map(
81 |(k, s)| [k.clone().into(), s.clone().into()].into_iter()))
82 )
83 }
84
85 fn comment(&self) -> String {
86 if self.revocations.len() == 1 {
87 format!("This is a revocation certificate for \
88 the subkey {} of cert {}.",
89 self.revocations[0].0.fingerprint(),
90 self.cert.fingerprint())
91 } else {
92 let fingerprints: Vec<_> = self.revocations.iter()
93 .map(|k| k.0.fingerprint().to_string()).collect();
94 format!("This is a revocation certificate for \
95 the subkeys {} of cert {}.",
96 fingerprints.join(", "),
97 self.cert.fingerprint())
98 }
99 }
100
101 fn revoker(&self) -> &Cert {
102 &self.revoker
103 }
104 }
105
106 /// Revoke a Subkey of an existing primary key
107 ///
108 /// ## Errors
109 ///
110 /// Returns an error if parsing of the [`KeyHandle`] fails, if reading of the
111 /// [`Cert`] fails, if retrieval of [`NotationData`] fails or if the eventual
112 /// revocation fails.
113 pub fn dispatch(sq: Sq, command: crate::cli::key::subkey::revoke::Command)
114 -> Result<()>
115 {
116 let (cert, cert_source) =
117 sq.resolve_cert(&command.cert, TrustThreshold::Full)?;
118
119 let vc = Cert::with_policy(&cert, NULL_POLICY, sq.time)
120 .with_context(|| {
121 format!("The certificate {} is not valid under the \
122 null policy.",
123 cert.fingerprint())
124 })?;
125
126 let keys = sq.resolve_keys(&vc, &cert_source, &command.keys, true)?;
127
128 let revoker = if command.revoker.is_empty() {
129 None
130 } else {
131 Some(sq.resolve_cert(&command.revoker, TrustThreshold::Full)?.0)
132 };
133
134 let notations = command.signature_notations.parse()?;
135
136 let revocation = SubkeyRevocation::new(
137 &sq,
138 &keys[..],
139 &cert,
140 revoker,
141 command.reason.into(),
142 &command.message,
143 &notations,
144 )?;
145 revocation.write(&sq, command.output, false)?;
146
147 Ok(())
148 }