Debian Codegraph @

src/commands/key/revoke.rs

raw
1 use sequoia_openpgp as openpgp;
2 use openpgp::cert::CertRevocationBuilder;
3 use openpgp::packet::signature::subpacket::NotationData;
4 use openpgp::types::ReasonForRevocation;
5 use openpgp::Cert;
6 use openpgp::Packet;
7 use openpgp::Result;
8
9 use crate::Sq;
10 use crate::cli::key::revoke::Command;
11 use crate::common::get_secret_signer;
12 use crate::common::RevocationOutput;
13 use crate::sq::TrustThreshold;
14
15 /// Handle the revocation of a certificate
16 struct CertificateRevocation {
17 cert: Cert,
18 revoker: Cert,
19 revocation_packet: Packet,
20 }
21
22 impl CertificateRevocation {
23 /// Create a new CertificateRevocation
24 pub fn new(
25 sq: &Sq,
26 cert: Cert,
27 revoker: Option<Cert>,
28 reason: ReasonForRevocation,
29 message: &str,
30 notations: &[(bool, NotationData)],
31 ) -> Result<Self> {
32 let (revoker, mut signer) = get_secret_signer(
33 sq,
34 &cert,
35 revoker.as_ref(),
36 )?;
37
38 let revocation_packet = {
39 // Create a revocation for the certificate.
40 let mut rev = CertRevocationBuilder::new()
41 .set_reason_for_revocation(reason, message.as_bytes())?;
42 rev = rev.set_signature_creation_time(sq.time)?;
43 for (critical, notation) in notations {
44 rev = rev.add_notation(
45 notation.name(),
46 notation.value(),
47 Some(notation.flags().clone()),
48 *critical,
49 )?;
50 }
51 let rev = rev.build(&mut signer, &cert, None)?;
52 Packet::Signature(rev)
53 };
54
55 Ok(CertificateRevocation {
56 cert,
57 revoker,
58 revocation_packet,
59 })
60 }
61 }
62
63 impl RevocationOutput for CertificateRevocation
64 {
65 fn cert(&self) -> Result<Cert> {
66 let cert = Cert::from_packets(vec![
67 self.cert.primary_key().key().clone().into(),
68 self.revocation_packet.clone(),
69 ].into_iter())?;
70
71 Ok(cert)
72 }
73
74 fn comment(&self) -> String {
75 format!("This is a revocation certificate for the cert {}.",
76 self.cert.fingerprint())
77 }
78
79 fn revoker(&self) -> &Cert {
80 &self.revoker
81 }
82 }
83
84 /// Revoke a certificate
85 pub fn certificate_revoke(
86 sq: Sq,
87 command: Command,
88 ) -> Result<()> {
89 let cert =
90 sq.resolve_cert(&command.cert, TrustThreshold::Full)?.0;
91
92 let revoker = if command.revoker.is_empty() {
93 None
94 } else {
95 Some(sq.resolve_cert(&command.revoker, TrustThreshold::Full)?.0)
96 };
97
98 let notations = command.signature_notations.parse()?;
99
100 let revocation = CertificateRevocation::new(
101 &sq,
102 cert,
103 revoker,
104 command.reason.into(),
105 &command.message,
106 &notations,
107 )?;
108 revocation.write(&sq, command.output, false)?;
109
110 Ok(())
111 }