Debian Codegraph @

tests/integration/sq_key_revoke.rs

raw
1 use std::time::Duration;
2
3 use openpgp::parse::Parse;
4 use openpgp::types::ReasonForRevocation;
5 use openpgp::types::RevocationStatus;
6 use openpgp::types::SignatureType;
7 use openpgp::Cert;
8 use openpgp::Result;
9 use sequoia_openpgp as openpgp;
10
11 use super::common::artifact;
12 use super::common::compare_notations;
13 use super::common::FileOrKeyHandle;
14 use super::common::Sq;
15 use super::common::NULL_POLICY;
16 use super::common::STANDARD_POLICY;
17
18 #[test]
19 fn sq_key_revoke() -> Result<()> {
20 let sq = Sq::new();
21
22 let time = sq.now();
23
24 let (_cert, cert_path, _cert_rev)
25 = sq.key_generate(&[], &["alice"]);
26
27 let message = "message";
28
29 // revoke for various reasons, with or without notations added, or
30 // with a revocation whose reference time is one hour after the
31 // creation of the certificate
32 for ((reason, reason_str, notations, revocation_time), cert_path) in [
33 (
34 ReasonForRevocation::KeyCompromised,
35 "compromised",
36 &[][..],
37 None,
38 ),
39 (
40 ReasonForRevocation::KeyCompromised,
41 "compromised",
42 &[][..],
43 Some(time + Duration::new(60 * 60, 0)),
44 ),
45 (
46 ReasonForRevocation::KeyCompromised,
47 "compromised",
48 &[("foo", "bar"), ("[email protected]", "VALUE")][..],
49 None,
50 ),
51 (ReasonForRevocation::KeyRetired, "retired", &[][..], None),
52 (
53 ReasonForRevocation::KeyRetired,
54 "retired",
55 &[("foo", "bar"), ("[email protected]", "VALUE")][..],
56 None,
57 ),
58 (ReasonForRevocation::KeySuperseded, "superseded", &[][..], None),
59 (
60 ReasonForRevocation::KeySuperseded,
61 "superseded",
62 &[("foo", "bar"), ("[email protected]", "VALUE")][..],
63 None,
64 ),
65 (ReasonForRevocation::Unspecified, "unspecified", &[][..], None),
66 (
67 ReasonForRevocation::Unspecified,
68 "unspecified",
69 &[][..],
70 Some(time + Duration::new(60 * 60, 0)),
71 ),
72 (
73 ReasonForRevocation::Unspecified,
74 "unspecified",
75 &[("foo", "bar"), ("[email protected]", "VALUE")][..],
76 None,
77 ),
78 ].into_iter().flat_map(|test| {
79 [
80 // A normal key.
81 (test, cert_path.clone()),
82 // A key that uses SHA-1.
83 (test, artifact("keys/only-sha1-priv.pgp")),
84 ]
85 })
86 {
87 eprintln!("==========================");
88 eprintln!("reason: {}, message: {}, notations: {:?}, time: {:?}",
89 reason, reason_str, notations, revocation_time);
90
91 let cert = Cert::from_file(&cert_path).expect("valid cert");
92
93 for keystore in [false, true].into_iter() {
94 eprintln!("--------------------------");
95 eprintln!("keystore: {}", keystore);
96
97 let revocation = sq.scratch_file(Some(&format!(
98 "revocation_{}_{}_{}.rev",
99 reason_str,
100 if notations.is_empty() {
101 "no_notations"
102 } else {
103 "notations"
104 },
105 if revocation_time.is_some() {
106 "time"
107 } else {
108 "no_time"
109 }
110 )[..]));
111
112 if keystore {
113 // When using the keystore, we need to import the key.
114 sq.key_import(&cert_path);
115 }
116
117 let updated = sq.key_revoke(
118 if keystore {
119 FileOrKeyHandle::from(cert.key_handle())
120 } else {
121 FileOrKeyHandle::from(&cert_path)
122 },
123 None,
124 reason_str,
125 message,
126 None,
127 notations,
128 Some(revocation.as_path()));
129
130 if let RevocationStatus::Revoked(sigs)
131 = updated.revocation_status(STANDARD_POLICY, None)
132 {
133 assert_eq!(sigs.len(), 1);
134 let sig = sigs.into_iter().next().unwrap();
135
136 // the issuer is the certificate owner
137 assert_eq!(
138 sig.get_issuers().into_iter().next(),
139 Some(cert.key_handle())
140 );
141
142 let revoked_cert =
143 cert.clone().insert_packets(sig.clone()).unwrap().0;
144 let status = revoked_cert
145 .revocation_status(
146 STANDARD_POLICY,
147 revocation_time.map(Into::into));
148
149 println!("{:?}", sig);
150 println!("{:?}", status);
151 // Verify the revocation.
152 assert!(matches!(status, RevocationStatus::Revoked(_)));
153
154 // it is a key revocation
155 assert_eq!(sig.typ(), SignatureType::KeyRevocation);
156
157 // our reason for revocation and message matches
158 assert_eq!(
159 sig.reason_for_revocation(),
160 Some((reason, message.as_bytes()))
161 );
162
163 // the notations of the revocation match the ones
164 // we passed in
165 compare_notations(sig, notations)?;
166 } else {
167 panic!("Not revoked");
168 }
169 }
170 }
171
172 Ok(())
173 }
174
175 #[test]
176 fn sq_key_revoke_thirdparty() -> Result<()> {
177 let sq = Sq::new();
178
179 let time = sq.now();
180
181 let (_cert, cert_path, _cert_rev)
182 = sq.key_generate(&[], &["alice"]);
183
184 let (_thirdparty_cert, thirdparty_path, _cert_rev)
185 = sq.key_generate(&[], &["bob <[email protected]>"]);
186
187 let message = "message";
188
189 // revoke for various reasons, with or without notations added, or with
190 // a revocation whose reference time is one hour after the creation of the
191 // certificate
192 for ((reason, reason_str, notations, revocation_time), cert_path, thirdparty_path) in [
193 (
194 ReasonForRevocation::KeyCompromised,
195 "compromised",
196 &[][..],
197 None,
198 ),
199 (
200 ReasonForRevocation::KeyCompromised,
201 "compromised",
202 &[][..],
203 Some(time + Duration::new(60 * 60, 0)),
204 ),
205 (
206 ReasonForRevocation::KeyCompromised,
207 "compromised",
208 &[("foo", "bar"), ("[email protected]", "VALUE")][..],
209 None,
210 ),
211 (ReasonForRevocation::KeyRetired, "retired", &[][..], None),
212 (
213 ReasonForRevocation::KeyRetired,
214 "retired",
215 &[][..],
216 Some(time + Duration::new(60 * 60, 0)),
217 ),
218 (
219 ReasonForRevocation::KeyRetired,
220 "retired",
221 &[("foo", "bar"), ("[email protected]", "VALUE")][..],
222 None,
223 ),
224 (ReasonForRevocation::KeySuperseded, "superseded", &[][..], None),
225 (
226 ReasonForRevocation::KeySuperseded,
227 "superseded",
228 &[][..],
229 Some(time + Duration::new(60 * 60, 0)),
230 ),
231 (
232 ReasonForRevocation::KeySuperseded,
233 "superseded",
234 &[("foo", "bar"), ("[email protected]", "VALUE")][..],
235 None,
236 ),
237 (ReasonForRevocation::Unspecified, "unspecified", &[][..], None),
238 (
239 ReasonForRevocation::Unspecified,
240 "unspecified",
241 &[][..],
242 Some(time + Duration::new(60 * 60, 0)),
243 ),
244 (
245 ReasonForRevocation::Unspecified,
246 "unspecified",
247 &[("foo", "bar"), ("[email protected]", "VALUE")][..],
248 None,
249 ),
250 ].into_iter().flat_map(|test| {
251 [
252 // Two valid keys.
253 (test,
254 cert_path.clone(),
255 thirdparty_path.clone()),
256 // The revokee is invalid (SHA-1).
257 (test,
258 artifact("keys/only-sha1-priv.pgp"),
259 thirdparty_path.clone()),
260 // The revoker is invalid (SHA-1).
261 (test,
262 cert_path.clone(),
263 artifact("keys/only-sha1-priv.pgp")),
264 ]
265 }) {
266 let cert = Cert::from_file(&cert_path).expect("valid cert");
267 let thirdparty_cert
268 = Cert::from_file(&thirdparty_path).expect("valid cert");
269
270 let thirdparty_valid_cert = thirdparty_cert
271 .with_policy(NULL_POLICY, Some(time.into()))?;
272 let thirdparty_fingerprint
273 = &thirdparty_valid_cert.clone().fingerprint();
274
275 for keystore in [false, true].into_iter() {
276 let revocation = sq.scratch_file(Some(&format!(
277 "revocation_{}_{}_{}.rev",
278 reason_str,
279 if ! notations.is_empty() {
280 "no_notations"
281 } else {
282 "notations"
283 },
284 if revocation_time.is_some() {
285 "time"
286 } else {
287 "no_time"
288 }
289 )[..]));
290
291 if keystore {
292 // When using the keystore, we need to import the key.
293
294 sq.cert_import(&cert_path);
295 sq.key_import(&thirdparty_path);
296 }
297
298 let revocation_cert = sq.key_revoke(
299 if keystore {
300 FileOrKeyHandle::from(cert.key_handle())
301 } else {
302 FileOrKeyHandle::from(&cert_path)
303 },
304 if keystore {
305 FileOrKeyHandle::from(thirdparty_cert.key_handle())
306 } else {
307 FileOrKeyHandle::from(&thirdparty_path)
308 },
309 reason_str,
310 message,
311 None,
312 notations,
313 Some(revocation.as_path()));
314
315 assert!(! revocation_cert.is_tsk());
316
317 // evaluate revocation status
318 let status = revocation_cert.revocation_status(
319 STANDARD_POLICY, revocation_time.map(Into::into));
320 if let RevocationStatus::CouldBe(sigs) = status {
321 // there is only one signature packet
322 assert_eq!(sigs.len(), 1);
323 let sig = sigs.into_iter().next().unwrap();
324
325 // it is a key revocation
326 assert_eq!(sig.typ(), SignatureType::KeyRevocation);
327
328 // the issuer is a thirdparty revoker
329 assert_eq!(
330 sig.get_issuers().into_iter().next().as_ref(),
331 Some(&thirdparty_fingerprint.clone().into())
332 );
333
334 // the revocation can be verified
335 if sig
336 .clone()
337 .verify_primary_key_revocation(
338 thirdparty_cert.primary_key().key(),
339 cert.primary_key().key(),
340 )
341 .is_err()
342 {
343 panic!("revocation is not valid")
344 }
345
346 // our reason for revocation and message matches
347 assert_eq!(
348 sig.reason_for_revocation(),
349 Some((reason, message.as_bytes()))
350 );
351
352 // the notations of the revocation match the ones
353 // we passed in
354 compare_notations(sig, notations)?;
355 } else {
356 panic!("there are no signatures in {:?}", status);
357 }
358 }
359 }
360
361 Ok(())
362 }