Debian Codegraph @

src/common/cert.rs

raw
1 use anyhow::Result;
2
3 use sequoia_openpgp as openpgp;
4 use openpgp::{
5 cert::{
6 Cert,
7 amalgamation::{
8 UserIDAmalgamation,
9 ValidAmalgamation,
10 ValidateAmalgamation,
11 key::{
12 PrimaryKeyAmalgamation,
13 SubordinateKeyAmalgamation,
14 },
15 },
16 },
17 packet::key::{
18 PublicParts,
19 },
20 types::RevocationStatus,
21 };
22
23 use crate::{
24 common::{
25 Validity,
26 ValidityLevel,
27 ModelViewAt,
28 },
29 };
30
31 /// A certificate authenticated using a trust model.
32 ///
33 /// This is a view on an `openpgp::Cert` that is augmented using a
34 /// given trust model. It is similar to how `openpgp::ValidCert` is a
35 /// view on an `openpgp::Cert` augmented using a policy and time.
36 pub struct AuthenticatedCert<'a> {
37 cert: &'a Cert,
38
39 /// Validity of primary key and subkeys.
40 ///
41 /// In GnuPG, this is the maximum of the user id validities.
42 cert_validity: Validity,
43
44 /// User id validities, in the same order as returned by
45 /// `cert.userids()`.
46 uid_validities: Vec<Validity>,
47
48 /// (Sub)key validities, in the same order as returned by
49 /// `cert.keys()`.
50 key_validities: Vec<Validity>,
51 }
52
53 impl<'a> AuthenticatedCert<'a> {
54 /// Authenticates a cert using the given trust model.
55 pub fn new(vtm: &dyn ModelViewAt, cert: &'a Cert) -> Result<Self> {
56 let cert_fp = cert.fingerprint();
57
58 // A cert's validity is either revoked, expired, or the max
59 // over the validity of the user ids. Further, if the cert is
60 // revoked or expired, so are the UserIDs. Hence, we
61 // partially compute cert_validity here.
62 let cert_validity = {
63 if let Ok(vcert) = cert.with_policy(vtm.policy(), vtm.time()) {
64 if let RevocationStatus::Revoked(_) = vcert.revocation_status()
65 {
66 Some(Validity::revoked())
67 } else if vcert.alive().is_err() {
68 Some(Validity::expired())
69 } else {
70 None
71 }
72 } else {
73 Some(Validity::expired()) // All binding signatures expired.
74 }
75 };
76
77 let uid_validities: Vec<_> = cert.userids()
78 .map(|uid| {
79 // If the cert is revoked or expired, so are the UserIDs.
80 if let Some(v) = cert_validity {
81 v
82 } else if let Ok(vuid) = uid.with_policy(vtm.policy(), vtm.time()) {
83 if let RevocationStatus::Revoked(_) = vuid.revocation_status() {
84 Validity::revoked()
85 } else {
86 vtm.validity(vuid.userid(), &cert_fp)
87 .unwrap_or(ValidityLevel::Unknown.into())
88 }
89 } else {
90 Validity::expired()
91 }
92 })
93 .collect();
94
95 // A cert's validity is either revoked, expired, or the max
96 // over the validity of the user ids.
97 let cert_validity = cert_validity.unwrap_or_else(
98 || uid_validities.iter().max().cloned()
99 .unwrap_or(ValidityLevel::Unknown.into()));
100
101 let key_validities: Vec<_> = cert.keys()
102 .map(|skb| {
103 if let Ok(vskb) = skb.with_policy(vtm.policy(), vtm.time()) {
104 if let RevocationStatus::Revoked(_) = vskb.revocation_status() {
105 Validity::revoked()
106 } else if vskb.alive().is_err() {
107 Validity::expired()
108 } else {
109 cert_validity
110 }
111 } else {
112 Validity::expired()
113 }
114 })
115 .collect();
116
117 Ok(AuthenticatedCert {
118 cert,
119 cert_validity,
120 uid_validities,
121 key_validities,
122 })
123 }
124
125 /// Returns the cert's validity.
126 ///
127 /// In GnuPG, this is the maximum of the user id validities.
128 pub fn cert_validity(&self) -> Validity {
129 self.cert_validity
130 }
131
132 /// Returns the user ids with their validities.
133 pub fn userids(&self)
134 -> impl Iterator<Item = (Validity,
135 UserIDAmalgamation<'a>)> + 'a
136 {
137 self.uid_validities.clone().into_iter()
138 .zip(self.cert.userids())
139 }
140
141 /// Returns the primary key and its validity.
142 pub fn primary_key(&self)
143 -> (Validity, PrimaryKeyAmalgamation<'a, PublicParts>)
144 {
145 (self.key_validities[0].clone(), self.cert.primary_key())
146 }
147
148 /// Returns the subkeys with their validities.
149 pub fn subkeys(&self)
150 -> impl Iterator<Item = (Validity,
151 SubordinateKeyAmalgamation<'a, PublicParts>)> + 'a
152 {
153 self.key_validities.clone().into_iter().skip(1)
154 .zip(self.cert.keys().subkeys())
155 }
156 }