Debian Codegraph @

tests/integration/sq_key_userid_revoke.rs

raw
1 use std::collections::BTreeSet;
2
3 use sequoia_openpgp as openpgp;
4 use openpgp::Cert;
5 use openpgp::KeyHandle;
6 use openpgp::Result;
7 use openpgp::cert::amalgamation::ValidAmalgamation;
8 use openpgp::parse::Parse;
9 use openpgp::types::RevocationStatus;
10
11 use super::common::NULL_POLICY;
12 use super::common::STANDARD_POLICY;
13 use super::common::Sq;
14 use super::common::UserIDArg;
15
16 #[test]
17 fn sha1_userid() {
18 // Make sure we can revoke a user ID that is bound using SHA-1.
19
20 let sq = Sq::new();
21
22 let cert_path = sq.test_data()
23 .join("keys")
24 .join("sha1-userid-priv.pgp");
25
26 let cert = Cert::from_file(&cert_path).expect("can read");
27
28 // Make sure the user ID is there and really uses SHA-1.
29 let vc = cert.with_policy(STANDARD_POLICY, sq.now())
30 .expect("valid cert");
31 let valid_userids: BTreeSet<_> = vc.userids()
32 .map(|ua| ua.userid())
33 .collect();
34 let all_userids: BTreeSet<_> = cert.userids()
35 .map(|ua| ua.userid())
36 .collect();
37
38 assert_eq!(valid_userids.len(), 1);
39 assert_eq!(all_userids.len(), 2);
40
41 let weak_userids: Vec<_>
42 = all_userids.difference(&valid_userids).collect();
43 let weak_userid
44 = String::from_utf8_lossy(weak_userids[0].value()).to_string();
45
46 let updated_path = sq.scratch_file("updated");
47 assert!(
48 sq.key_userid_revoke_maybe(&[],
49 &cert_path,
50 &weak_userid,
51 "retired",
52 "bye, bye",
53 updated_path.as_path())
54 .is_err());
55
56 sq.key_userid_revoke(&[],
57 &cert_path,
58 UserIDArg::AddUserID(&weak_userid),
59 "retired",
60 "bye, bye",
61 updated_path.as_path());
62 }
63
64 #[test]
65 fn unbound_userid() {
66 // Make sure we can't revoke a user ID that is unbound using
67 // --userid.
68
69 let sq = Sq::new();
70
71 let cert_path = sq.test_data()
72 .join("keys")
73 .join("unbound-userid.pgp");
74
75 let cert = Cert::from_file(&cert_path).expect("can read");
76
77 // Make sure the user ID is there and is unbound.
78 let vc = cert.with_policy(NULL_POLICY, sq.now())
79 .expect("valid cert");
80 let valid_userids: BTreeSet<_> = vc.userids()
81 .map(|ua| ua.userid())
82 .collect();
83 let all_userids: BTreeSet<_> = cert.userids()
84 .map(|ua| ua.userid())
85 .collect();
86
87 assert_eq!(valid_userids.len(), 0);
88 assert_eq!(all_userids.len(), 1);
89
90 let unbound_userids: Vec<_>
91 = all_userids.difference(&valid_userids).collect();
92 let unbound_userid
93 = String::from_utf8_lossy(unbound_userids[0].value()).to_string();
94
95 let updated_path = sq.scratch_file("updated");
96 assert!(
97 sq.key_userid_revoke_maybe(
98 &[],
99 &cert_path,
100 &unbound_userid,
101 "retired",
102 "bye, bye",
103 updated_path.as_path())
104 .is_err());
105
106 // But it should work with --add-userid.
107 sq.key_userid_revoke(
108 &[],
109 &cert_path,
110 UserIDArg::AddUserID(&unbound_userid),
111 "retired",
112 "bye, bye",
113 updated_path.as_path());
114 }
115
116 #[test]
117 fn revoked_userid() {
118 // Make sure we can revoke a user ID a second time.
119
120 let sq = Sq::new();
121
122 let cert_path = sq.test_data()
123 .join("keys")
124 .join("retired-userid.pgp");
125
126 let cert = Cert::from_file(&cert_path).expect("can read");
127
128 // Make sure the user ID is there and is revoked.
129 let vc = cert.with_policy(NULL_POLICY, sq.now())
130 .expect("valid cert");
131 let mut revoked = None;
132 for ua in vc.userids() {
133 if let RevocationStatus::Revoked(_) = ua.revocation_status() {
134 assert!(revoked.is_none(),
135 "Only expected a single revoked user ID");
136 revoked = Some(ua.userid());
137 }
138 }
139 let revoked = if let Some(revoked) = revoked {
140 String::from_utf8(revoked.value().to_vec()).unwrap()
141 } else {
142 panic!("Expected a revoked user ID, but didn't fine one");
143 };
144
145 let updated_path = sq.scratch_file("updated");
146 sq.key_userid_revoke(
147 &[],
148 &cert_path,
149 UserIDArg::UserID(&revoked),
150 "retired",
151 "bye, bye",
152 updated_path.as_path());
153 }
154
155 #[test]
156 fn allow_non_canonical_userid() {
157 // Make sure we can revoke a user ID that is bound using SHA-1.
158
159 let sq = Sq::new();
160
161 let userid = "<[email protected]> <[email protected]>";
162
163 let (_cert, cert_path, _rev_path) = sq.key_generate(
164 &["--allow-non-canonical-userids"],
165 &[userid]);
166
167 // We should be able to revoke the non-canonical user ID, because
168 // it is a self-signed user ID.
169 let updated_path = sq.scratch_file("updated");
170 sq.key_userid_revoke(&[],
171 &cert_path,
172 UserIDArg::AddUserID(userid),
173 "retired",
174 "bye, bye",
175 updated_path.as_path());
176
177 // But we can't use a non-canonical user ID that is not
178 // self-signed.
179 let updated_path = sq.scratch_file("updated");
180 assert!(
181 sq.key_userid_revoke_maybe(
182 &[],
183 &cert_path,
184 UserIDArg::AddUserID("<[email protected]> <[email protected]>"),
185 "retired",
186 "bye, bye",
187 updated_path.as_path())
188 .is_err());
189
190 // Unless we include --allow
191 let updated_path = sq.scratch_file("updated");
192 sq.key_userid_revoke(
193 &["--allow-non-canonical-userids"],
194 &cert_path,
195 UserIDArg::AddUserID("<[email protected]> <[email protected]>"),
196 "retired",
197 "bye, bye",
198 updated_path.as_path());
199 }
200
201 #[test]
202 fn userid_designators() {
203 let self_signed_email = "[email protected]";
204 let self_signed_userid
205 = &format!("Alice <{}>", self_signed_email);
206
207 let other_email = "[email protected]";
208 let other_userid = &format!("Alice <{}>", other_email);
209
210 let setup = || {
211 let mut sq = Sq::new();
212
213 let (cert, cert_path, _rev_path)
214 = sq.key_generate(&[], &[ self_signed_userid ]);
215 sq.key_import(cert_path);
216
217 // Link the self-signed user ID.
218 sq.pki_link_add(&[], cert.key_handle(),
219 &[ self_signed_userid ]);
220
221 // Link a non-self-signed user ID.
222 sq.pki_link_add(&[], cert.key_handle(),
223 &[ UserIDArg::AddUserID(other_userid) ]);
224
225 sq.tick(1);
226
227 let fpr = cert.fingerprint().to_string();
228
229 (cert, fpr, sq)
230 };
231
232 let revoke = |sq: &Sq, kh: KeyHandle, userid: UserIDArg|
233 -> Result<()>
234 {
235 let rev_path = sq.scratch_file("revocation");
236 sq.key_userid_revoke_maybe(
237 &[], kh, userid,
238 "retired", "xxx", rev_path.as_path())?;
239 sq.cert_import(rev_path);
240 Ok(())
241 };
242
243 // Currently, sq doesn't support first-party revocations on user
244 // ID that are not self signed. See:
245 //
246 // https://gitlab.com/sequoia-pgp/sequoia-sq/-/issues/499
247 //
248 // As such, we check by hand if there is a first-party revocation
249 // for the specified user ID.
250 let revocations = |sq: &Sq, kh: KeyHandle, userid: &str, count: usize|
251 {
252 let cert = sq.cert_export(&kh);
253 for ua in cert.userids() {
254 if &String::from_utf8_lossy(ua.userid().value()) == userid {
255 assert_eq!(ua.self_revocations().count(), count);
256 return;
257 }
258 }
259
260 if count > 0 {
261 panic!("{} does not contain {:?}", kh, userid);
262 }
263 };
264
265 // 1. --user: use the specified self-signed user ID.
266 let (cert, fpr, sq) = setup();
267
268 // Self-signed and authenticated.
269 assert!(sq.pki_authenticate(
270 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_ok());
271 assert!(revoke(&sq, cert.key_handle(),
272 UserIDArg::UserID(self_signed_userid)).is_ok());
273 revocations(&sq, cert.key_handle(), self_signed_userid, 1);
274 assert!(sq.pki_authenticate(
275 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_err());
276
277 // Authenticated, but not self-signed.
278 assert!(revoke(&sq, cert.key_handle(),
279 UserIDArg::UserID(other_userid)).is_err());
280 revocations(&sq, cert.key_handle(), other_userid, 0);
281 assert!(sq.pki_authenticate(
282 &[], &fpr, UserIDArg::UserID(other_userid)).is_ok());
283
284 // 2. --add-userid: use the specified user ID.
285 let (cert, fpr, sq) = setup();
286
287 // Self-signed and authenticated.
288 assert!(sq.pki_authenticate(
289 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_ok());
290 assert!(revoke(&sq, cert.key_handle(),
291 UserIDArg::AddUserID(self_signed_userid)).is_ok());
292 revocations(&sq, cert.key_handle(), self_signed_userid, 1);
293 assert!(sq.pki_authenticate(
294 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_err());
295
296 // Authenticated, but not self-signed.
297 assert!(sq.pki_authenticate(
298 &[], &fpr, UserIDArg::UserID(other_userid)).is_ok());
299 assert!(revoke(&sq, cert.key_handle(),
300 UserIDArg::AddUserID(other_userid)).is_ok());
301 revocations(&sq, cert.key_handle(), other_userid, 1);
302
303 // 3. --userid-by-email: use the self-signed user ID with the
304 // specified email address.
305 let (cert, fpr, sq) = setup();
306
307 // Self-signed and authenticated.
308 assert!(sq.pki_authenticate(
309 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_ok());
310 assert!(revoke(&sq, cert.key_handle(),
311 UserIDArg::ByEmail(self_signed_email)).is_ok());
312 revocations(&sq, cert.key_handle(), self_signed_userid, 1);
313 assert!(sq.pki_authenticate(
314 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_err());
315
316 // Authenticated, but not self-signed.
317 assert!(sq.pki_authenticate(
318 &[], &fpr, UserIDArg::UserID(other_userid)).is_ok());
319 assert!(revoke(&sq, cert.key_handle(),
320 UserIDArg::ByEmail(other_email)).is_err());
321 revocations(&sq, cert.key_handle(), other_userid, 0);
322
323 // 4. --email: use a user ID with just the email address if there
324 // is a self-signed user ID with the specified email address.
325 let (cert, fpr, sq) = setup();
326
327 // Self-signed and authenticated.
328 assert!(sq.pki_authenticate(
329 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_ok());
330 assert!(revoke(&sq, cert.key_handle(),
331 UserIDArg::Email(self_signed_email)).is_ok());
332 revocations(&sq, cert.key_handle(), self_signed_userid, 0);
333 revocations(&sq, cert.key_handle(), &format!("<{}>", self_signed_email), 1);
334 assert!(sq.pki_authenticate(
335 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_ok());
336
337 // Authenticated, but not self-signed.
338 assert!(sq.pki_authenticate(
339 &[], &fpr, UserIDArg::UserID(other_userid)).is_ok());
340 assert!(revoke(&sq, cert.key_handle(),
341 UserIDArg::Email(other_email)).is_err());
342 revocations(&sq, cert.key_handle(), other_userid, 0);
343 revocations(&sq, cert.key_handle(), &format!("<{}>", other_email), 0);
344
345 // 5. --add-email: use a user ID with the email address.
346 let (cert, fpr, sq) = setup();
347
348 // Self-signed and authenticated.
349 assert!(sq.pki_authenticate(
350 &[], &fpr, UserIDArg::UserID(self_signed_userid)).is_ok());
351 assert!(revoke(&sq, cert.key_handle(),
352 UserIDArg::AddEmail(self_signed_email)).is_ok());
353 revocations(&sq, cert.key_handle(), self_signed_userid, 0);
354 revocations(&sq, cert.key_handle(), &format!("<{}>", self_signed_email), 1);
355 assert!(sq.pki_authenticate(
356 &[], &fpr, UserIDArg::UserID(&format!("<{}>", self_signed_email))).is_err());
357
358 // Authenticated, but not self-signed.
359 assert!(sq.pki_authenticate(
360 &[], &fpr, UserIDArg::UserID(other_userid)).is_ok());
361 assert!(revoke(&sq, cert.key_handle(),
362 UserIDArg::AddEmail(other_email)).is_ok());
363 revocations(&sq, cert.key_handle(), other_userid, 0);
364 revocations(&sq, cert.key_handle(), &format!("<{}>", other_email), 1);
365 }