Debian Codegraph @

tests/integration/sq_pki_link_authorize.rs

raw
1 use super::common::NO_USERIDS;
2 use super::common::Sq;
3 use super::common::UserIDArg;
4
5 #[test]
6 fn sq_pki_link_authorize_then_authenticate() {
7 let ca_example_org = "<[email protected]>";
8
9 for (all, userids) in &[
10 (false, &[UserIDArg::UserID(ca_example_org)][..]),
11 // Implicitly use all self-signed user IDs.
12 (true, NO_USERIDS),
13 // Use a non-self signed user ID.
14 (false, &[UserIDArg::AddUserID("frank")]),
15 ] {
16 let mut sq = Sq::new();
17
18 let (ca, ca_pgp, _ca_rev)
19 = sq.key_generate(&[], &[ca_example_org]);
20 sq.key_import(&ca_pgp);
21
22 let alice_example_org = "<[email protected]>";
23 let (alice, alice_pgp, _alice_rev)
24 = sq.key_generate(&[], &[alice_example_org]);
25 sq.key_import(&alice_pgp);
26
27 let bob_example_org = "<[email protected]>";
28 let bob_other_org = "<[email protected]>";
29 let (bob, bob_pgp, _bob_rev)
30 = sq.key_generate(&[], &[bob_example_org, bob_other_org]);
31 sq.key_import(&bob_pgp);
32
33 sq.tick(1);
34
35 // The ca certifies alice's and bob's certificates for each of
36 // their user IDs.
37 let certification = sq.scratch_file(None);
38 sq.pki_vouch_add(
39 &[],
40 ca.key_handle(), alice.key_handle(),
41 &[ alice_example_org ],
42 certification.as_path());
43 sq.cert_import(&certification);
44
45 let certification = sq.scratch_file(None);
46 sq.pki_vouch_add(
47 &[],
48 ca.key_handle(), bob.key_handle(),
49 &[ bob_example_org, bob_other_org ],
50 certification.as_path());
51 sq.cert_import(certification);
52
53 // Check whether we can authenticate alice's and bob's
54 // certificates for their user ID.
55 let check = |sq: &Sq,
56 can_authenticate_alice,
57 can_authenticate_bob1,
58 can_authenticate_bob2|
59 {
60 for (cert, userid, can_authenticate) in &[
61 (&alice, alice_example_org, can_authenticate_alice),
62 (&bob, bob_example_org, can_authenticate_bob1),
63 (&bob, bob_other_org, can_authenticate_bob2),
64 ]
65 {
66 let r = sq.pki_authenticate(
67 &[],
68 &cert.fingerprint().to_string(),
69 userid);
70
71 match (can_authenticate, r.is_ok()) {
72 (true, false) => {
73 panic!("Expected to authenticated {}, but didn't.",
74 userid);
75 }
76 (false, true) => {
77 panic!("Expected to NOT authenticated {}, but did.",
78 userid);
79 }
80 _ => (),
81 }
82 }
83 };
84
85 let maybe_all = |args: &[&'static str]| {
86 let mut args = args.to_vec();
87 if *all {
88 args.push("--all");
89 }
90 args
91 };
92
93 // No delegation yet.
94 println!("CA: not authorized");
95 check(
96 &sq,
97 false, // Alice.
98 false, // [email protected]
99 false);// [email protected]
100
101 // The user completely authorizes the CA.
102 sq.tick(1);
103 sq.pki_link_authorize(&maybe_all(&["--unconstrained"]),
104 ca.key_handle(),
105 userids);
106
107 println!("CA: authorized, and unconstrained");
108 check(
109 &sq,
110 true, // [email protected]
111 true, // [email protected]
112 true);// [email protected]
113
114 // The user authorizes the CA with the regex contraint "example".
115 sq.tick(1);
116 sq.pki_link_authorize(&maybe_all(&["--regex", "example"]),
117 ca.key_handle(),
118 userids);
119
120 println!("CA: authorized for \"example\"");
121 check(
122 &sq,
123 true, // [email protected]
124 true, // [email protected]
125 false);// [email protected]
126
127 // The user authorizes the CA with the domain contraint
128 // "example.org".
129 sq.tick(1);
130 sq.pki_link_authorize(&maybe_all(&["--domain", "example.org"]),
131 ca.key_handle(),
132 userids);
133
134 println!("CA: authorized for \"example.org\"");
135 check(
136 &sq,
137 true, // [email protected]
138 true, // [email protected]
139 false);// [email protected]
140
141 // The user authorizes the CA with the regex contraint "other".
142 sq.tick(1);
143 sq.pki_link_authorize(&maybe_all(&["--regex", "other"]),
144 ca.key_handle(),
145 userids);
146
147 println!("CA: authorized for \"other\"");
148 check(
149 &sq,
150 false, // [email protected]
151 false, // [email protected]
152 true); // [email protected]
153
154 // The user authorizes the CA with the regex contraint "bob".
155 sq.tick(1);
156 sq.pki_link_authorize(&maybe_all(&["--regex", "bob"]),
157 ca.key_handle(),
158 userids);
159
160 println!("CA: authorized for \"bob\"");
161 check(
162 &sq,
163 false, // [email protected]
164 true, // [email protected]
165 true); // [email protected]
166
167 // The user authorizes the CA with the regex contraint "bob" or
168 // "alice".
169 sq.tick(1);
170 sq.pki_link_authorize(&maybe_all(&["--regex", "bob",
171 "--regex", "alice"]),
172 ca.key_handle(),
173 userids);
174
175 println!("CA: authorized for \"bob\" or \"alice\"");
176 check(
177 &sq,
178 true, // [email protected]
179 true, // [email protected]
180 true);// [email protected]
181
182
183 // The user authorizes the CA for the domains example.org and
184 // other.org.
185 sq.tick(1);
186 sq.pki_link_authorize(&maybe_all(&["--domain", "example.org",
187 "--domain", "other.org"]),
188 ca.key_handle(),
189 userids);
190
191 println!("CA: authorized for the domains example.org and other.org");
192 check(
193 &sq,
194 true, // [email protected]
195 true, // [email protected]
196 true);// [email protected]
197
198
199 // The user authorizes the CA for the domain example.com and the
200 // regex alice.
201 sq.tick(1);
202 sq.pki_link_authorize(&maybe_all(&["--domain", "other.org",
203 "--regex", "alice"]),
204 ca.key_handle(),
205 userids);
206
207 println!("CA: authorized for the domains example.org and other.org");
208 check(
209 &sq,
210 true, // [email protected]
211 false, // [email protected]
212 true); // [email protected]
213
214
215 // The user authorizes the CA with the regex contraint "zoo".
216 sq.tick(1);
217 sq.pki_link_authorize(&maybe_all(&["--regex", "zoo"]),
218 ca.key_handle(),
219 userids);
220
221 println!("CA: authorized for \"zoo\"");
222 check(
223 &sq,
224 false, // [email protected]
225 false, // [email protected]
226 false);// [email protected]
227
228 // The user authorizes the CA with the domain contraint "example".
229 sq.tick(1);
230 sq.pki_link_authorize(&maybe_all(&["--domain", "example"]),
231 ca.key_handle(),
232 userids);
233
234 println!("CA: authorized for \"zoo\"");
235 check(
236 &sq,
237 false, // [email protected]
238 false, // [email protected]
239 false);// [email protected]
240 }
241 }
242
243 #[test]
244 fn retract_explicit() {
245 // Authorize a CA via multiple user IDs. Retract one
246 // authorization at a time. Make sure the CA remains a trusted
247 // introducer until all authorizations are retracted.
248
249 let ca_example_org = "<[email protected]>";
250 let ca_example_com = "<[email protected]>";
251 let self_signed_userids = &[ca_example_org, ca_example_com][..];
252
253 // When authorizing the CA we can either authorize implicitly
254 // (i.e., all self-signed user IDs) or explicitly. Try both.
255 for explicit_all in [true, false] {
256 eprintln!("explicit all = {}", explicit_all);
257
258 let mut sq = Sq::new();
259 let (ca, ca_pgp, _ca_rev)
260 = sq.key_generate(&[], self_signed_userids);
261 sq.key_import(&ca_pgp);
262
263 let alice_example_org = "<[email protected]>";
264 let (alice, alice_pgp, _alice_rev)
265 = sq.key_generate(&[], &[alice_example_org]);
266 sq.key_import(&alice_pgp);
267
268 sq.tick(1);
269
270 // The ca certifies alice's certificate
271 let certification = sq.scratch_file(None);
272 sq.pki_vouch_add(&[],
273 ca.key_handle(), alice.key_handle(),
274 &[ alice_example_org ],
275 certification.as_path());
276 sq.cert_import(&certification);
277
278 let check = |sq: &Sq, can_authenticate: bool| {
279 let r = sq.pki_authenticate(
280 &[],
281 &alice.fingerprint().to_string(),
282 alice_example_org);
283
284 match (can_authenticate, r.is_ok()) {
285 (true, false) => {
286 panic!("Expected to authenticated {}, but didn't.",
287 alice_example_org);
288 }
289 (false, true) => {
290 panic!("Expected to NOT authenticated {}, but did.",
291 alice_example_org);
292 }
293 _ => (),
294 }
295 };
296
297 // The ca is not yet authorized so we shouldn't be able to
298 // authenticate alice.
299 check(&sq, false);
300
301 // Authorize via all user IDs.
302 sq.tick(1);
303 let userids = self_signed_userids;
304 sq.pki_link_authorize(if explicit_all {
305 &["--unconstrained", "--all"]
306 } else {
307 &["--unconstrained"]
308 },
309 ca.key_handle(),
310 if explicit_all {
311 &[]
312 } else {
313 userids
314 });
315 check(&sq, true);
316
317 for (i, userid) in userids.iter().enumerate() {
318 // Retract the authorization of one user ID. It should
319 // still be a trusted introducer.
320 sq.tick(1);
321 sq.pki_link_retract(&[], ca.key_handle(), &[userid]);
322
323 if i == userids.len() - 1 {
324 // We've retracted all authorizations. This should
325 // now fail.
326 check(&sq, false);
327 } else {
328 check(&sq, true);
329 }
330 }
331 }
332 }
333
334 #[test]
335 fn retract_non_self_signed() {
336 // Make sure we can retract non-self-signed user IDs.
337
338 let self_signed = "<[email protected]>";
339 let non_self_signed = "<[email protected]>";
340
341 let mut sq = Sq::new();
342 let (ca, ca_pgp, _ca_rev)
343 = sq.key_generate(&[], &[self_signed]);
344 sq.key_import(&ca_pgp);
345
346 let alice_example_org = "<[email protected]>";
347 let (alice, alice_pgp, _alice_rev)
348 = sq.key_generate(&[], &[alice_example_org]);
349 sq.key_import(&alice_pgp);
350
351 sq.tick(1);
352
353 // The ca certifies alice's certificate
354 let certification = sq.scratch_file(None);
355 sq.pki_vouch_add(&[],
356 ca.key_handle(), alice.key_handle(),
357 &[ alice_example_org ],
358 certification.as_path());
359 sq.cert_import(&certification);
360
361 let check = |sq: &Sq, can_authenticate: bool| {
362 let r = sq.pki_authenticate(
363 &[],
364 &alice.fingerprint().to_string(),
365 alice_example_org);
366
367 match (can_authenticate, r.is_ok()) {
368 (true, false) => {
369 panic!("Expected to authenticated {}, but didn't.",
370 alice_example_org);
371 }
372 (false, true) => {
373 panic!("Expected to NOT authenticated {}, but did.",
374 alice_example_org);
375 }
376 _ => (),
377 }
378 };
379
380 // The ca is not yet authorized so we shouldn't be able to
381 // authenticate alice.
382 check(&sq, false);
383
384 // Authorize the CA via a non-self-signed user ID. Now we can
385 // authenticate alice.
386 sq.tick(1);
387 sq.pki_link_authorize(&["--unconstrained"],
388 ca.key_handle(),
389 &[UserIDArg::AddUserID(non_self_signed)]);
390 check(&sq, true);
391
392 // Retract the authorization. It should no longer be considered a
393 // trusted introducer.
394 sq.tick(1);
395 sq.pki_link_retract(&[], ca.key_handle(), &[non_self_signed]);
396 check(&sq, false);
397 }
398
399 #[test]
400 fn retract_all() {
401 // Link all self-signed user IDs and a non-self-signed user ID.
402 // When we retract all, make sure they are all retracted.
403
404 let self_signed = "<[email protected]>";
405 let non_self_signed = "<[email protected]>";
406
407 let mut sq = Sq::new();
408 let (ca, ca_pgp, _ca_rev)
409 = sq.key_generate(&[], &[self_signed]);
410 sq.key_import(&ca_pgp);
411
412 let alice_example_org = "<[email protected]>";
413 let (alice, alice_pgp, _alice_rev)
414 = sq.key_generate(&[], &[alice_example_org]);
415 sq.key_import(&alice_pgp);
416
417 sq.tick(1);
418
419 // The ca certifies alice's certificate
420 let certification = sq.scratch_file(None);
421 sq.pki_vouch_add(&[],
422 ca.key_handle(), alice.key_handle(),
423 &[ alice_example_org ],
424 certification.as_path());
425 sq.cert_import(&certification);
426
427 let check = |sq: &Sq, can_authenticate: bool| {
428 let r = sq.pki_authenticate(
429 &[],
430 &alice.fingerprint().to_string(),
431 alice_example_org);
432
433 match (can_authenticate, r.is_ok()) {
434 (true, false) => {
435 panic!("Expected to authenticated {}, but didn't.",
436 alice_example_org);
437 }
438 (false, true) => {
439 panic!("Expected to NOT authenticated {}, but did.",
440 alice_example_org);
441 }
442 _ => (),
443 }
444 };
445
446 // The ca is not yet authorized so we shouldn't be able to
447 // authenticate alice.
448 check(&sq, false);
449
450 // Authorize the CA via a non-self-signed user ID. Now we can
451 // authenticate alice.
452 sq.tick(1);
453 sq.pki_link_authorize(&["--unconstrained"],
454 ca.key_handle(),
455 &[UserIDArg::UserID(self_signed),
456 UserIDArg::AddUserID(non_self_signed)]);
457 check(&sq, true);
458
459 // Retract all authorizations. It should no longer be considered
460 // a trusted introducer.
461 sq.tick(1);
462 sq.pki_link_retract(&["--all"], ca.key_handle(), NO_USERIDS);
463 check(&sq, false);
464 }
465
466 #[test]
467 fn sq_pki_link_all_revoked() {
468 // When we don't provide any user IDs, `sq pki link authorize`
469 // certifies all of the self signed user IDs. Make sure this
470 // works in the presence of a revoked user ID, which should be
471 // ignored.
472
473 let mut sq = Sq::new();
474
475 let ca_example_org = "<[email protected]>";
476 let ca_example_com = "<[email protected]>";
477 let (ca, ca_pgp, _ca_rev)
478 = sq.key_generate(&[], &[ca_example_org, ca_example_com]);
479 sq.key_import(&ca_pgp);
480
481 // Revoke [email protected].
482 sq.tick(1);
483 let revocation = sq.scratch_file("revocation");
484 sq.key_userid_revoke(&[], ca.fingerprint(), ca_example_com,
485 "retired", "bye", Some(revocation.as_path()));
486 sq.cert_import(&revocation);
487
488 let alice_example_org = "<[email protected]>";
489 let (alice, alice_pgp, _alice_rev)
490 = sq.key_generate(&[], &[alice_example_org]);
491 sq.key_import(&alice_pgp);
492
493 sq.tick(1);
494
495 // The ca certifies alice's certificate.
496 let certification = sq.scratch_file(None);
497 sq.pki_vouch_add(
498 &[],
499 ca.key_handle(), alice.key_handle(),
500 &[ alice_example_org ],
501 certification.as_path());
502 sq.cert_import(&certification);
503
504 // Check whether we can authenticate alice's and bob's
505 // certificates for their user ID using otto as the trust root.
506 let check = |sq: &Sq, can_authenticate|
507 {
508 let r = sq.pki_authenticate(
509 &[],
510 &alice.fingerprint().to_string(),
511 alice_example_org);
512
513 match (can_authenticate, r.is_ok()) {
514 (true, false) => {
515 panic!("Expected to authenticated {}, but didn't.",
516 alice_example_org);
517 }
518 (false, true) => {
519 panic!("Expected to NOT authenticated {}, but did.",
520 alice_example_org);
521 }
522 _ => (),
523 }
524 };
525
526 // No delegation yet.
527 println!("CA: not authorized");
528 check(&sq, false);
529
530 // The user completely authorizes the CA. Note: we don't specify
531 // any user IDs so only valid self-signed user IDs should be used.
532 // That means the revoked user ID should be skipped.
533 sq.tick(1);
534 sq.pki_link_authorize(&["--unconstrained", "--all"],
535 ca.key_handle(), NO_USERIDS);
536
537 println!("CA: authorized, and unconstrained");
538 check(&sq, true);
539 }