Debian Codegraph @

tests/integration/sq_pki_vouch_add.rs

raw
1 use std::fs::File;
2 use std::time;
3 use std::time::Duration;
4
5 use sequoia_openpgp as openpgp;
6 use openpgp::Result;
7 use openpgp::KeyHandle;
8 use openpgp::Packet;
9 use openpgp::PacketPile;
10 use openpgp::packet::signature::subpacket::NotationData;
11 use openpgp::packet::signature::subpacket::NotationDataFlags;
12 use openpgp::packet::UserID;
13 use openpgp::parse::Parse;
14 use openpgp::policy::StandardPolicy;
15 use openpgp::serialize::{Serialize, SerializeInto};
16
17 use super::common::FileOrKeyHandle;
18 use super::common::NO_USERIDS;
19 use super::common::Sq;
20 use super::common::UserIDArg;
21
22 const P: &StandardPolicy = &StandardPolicy::new();
23
24 #[test]
25 fn sq_pki_vouch_add() -> Result<()> {
26 let mut sq = Sq::new();
27
28 let (alice, alice_pgp, _alice_rev)
29 = sq.key_generate(&[], &["<[email protected]>"]);
30 let (_bob, bob_pgp, _bob_rev)
31 = sq.key_generate(&[], &["<[email protected]>"]);
32
33 for keystore in [false, true] {
34 if keystore {
35 sq.key_import(&alice_pgp);
36 }
37
38 let alice_handle: FileOrKeyHandle = if keystore {
39 alice.key_handle().into()
40 } else {
41 alice_pgp.clone().into()
42 };
43
44 let mut bob_pgp = vec![ bob_pgp.clone() ];
45
46 let mut certification_count = 0;
47
48 // A simple certification.
49 sq.tick(1);
50 let bob_pgp_new = sq.scratch_file("bob");
51 let cert = sq.pki_vouch_add(
52 &[], &alice_handle, bob_pgp.last().unwrap(), &["<[email protected]>"],
53 Some(&*bob_pgp_new));
54 bob_pgp.push(bob_pgp_new);
55 certification_count += 1;
56 assert_eq!(cert.bad_signatures().count(), 0,
57 "Bad signatures in cert\n\n{}",
58 String::from_utf8(cert.armored().to_vec().unwrap()).unwrap());
59
60 let vc = cert.with_policy(P, None).unwrap();
61
62 let mut ok = false;
63 for ua in vc.userids() {
64 if ua.userid().value() == b"<[email protected]>" {
65 let certifications: Vec<_>
66 = ua.certifications().collect();
67 assert_eq!(certifications.len(), 1);
68 let c = certifications[0];
69
70 assert_eq!(c.trust_signature(), None);
71 assert_eq!(c.regular_expressions().count(), 0);
72 assert_eq!(c.revocable().unwrap_or(true), true);
73 assert_eq!(c.exportable_certification().unwrap_or(true), true);
74 // By default, we set a duration.
75 assert!(c.signature_validity_period().is_some());
76
77 ok = true;
78 break;
79 }
80 }
81 assert!(ok, "Didn't find user id");
82
83 // No expiration.
84 sq.tick(1);
85 let bob_pgp_new = sq.scratch_file(None);
86 let cert = sq.pki_vouch_add(
87 &["--expiration", "never"],
88 &alice_handle, bob_pgp.last().unwrap(), &["<[email protected]>"],
89 Some(&*bob_pgp_new));
90 bob_pgp.push(bob_pgp_new);
91 certification_count += 1;
92 assert_eq!(cert.bad_signatures().count(), 0,
93 "Bad signatures in cert\n\n{}",
94 String::from_utf8(cert.armored().to_vec().unwrap()).unwrap());
95
96 let vc = cert.with_policy(P, None).unwrap();
97
98 let mut ok = false;
99 for ua in vc.userids() {
100 if ua.userid().value() == b"<[email protected]>" {
101 let certifications: Vec<_>
102 = ua.certifications().collect();
103 assert_eq!(certifications.len(), certification_count,
104 "Expected exactly one certification");
105 let c = certifications[0];
106
107 assert_eq!(c.trust_signature(), None);
108 assert_eq!(c.regular_expressions().count(), 0);
109 assert_eq!(c.revocable().unwrap_or(true), true);
110 assert_eq!(c.exportable_certification().unwrap_or(true), true);
111 assert!(c.signature_validity_period().is_none());
112
113 ok = true;
114 break;
115 }
116 }
117 assert!(ok, "Didn't find user id");
118
119 // Have alice certify <[email protected]> for 0xB0B.
120 sq.tick(1);
121 let bob_pgp_new = sq.scratch_file(None);
122 let cert = sq.pki_vouch_authorize(
123 &["--depth", "10",
124 "--amount", "5",
125 "--regex", "a",
126 "--regex", "b",
127 "--local",
128 "--non-revocable",
129 "--expiration", "1d",
130 ],
131 &alice_handle, bob_pgp.last().unwrap(), &["<[email protected]>"],
132 Some(&*bob_pgp_new));
133 bob_pgp.push(bob_pgp_new);
134 certification_count += 1;
135 assert_eq!(cert.bad_signatures().count(), 0,
136 "Bad signatures in cert\n\n{}",
137 String::from_utf8(cert.armored().to_vec().unwrap()).unwrap());
138
139 let vc = cert.with_policy(P, None).unwrap();
140
141 let mut ok = false;
142 for ua in vc.userids() {
143 if ua.userid().value() == b"<[email protected]>" {
144 let certifications: Vec<_>
145 = ua.certifications().collect();
146 assert_eq!(certifications.len(), certification_count);
147 let c = certifications[0];
148
149 assert_eq!(c.trust_signature(), Some((10, 5)));
150 assert_eq!(&c.regular_expressions().collect::<Vec<_>>()[..],
151 &[ b"a", b"b" ]);
152 assert_eq!(c.revocable(), Some(false));
153 assert_eq!(c.exportable_certification(), Some(false));
154 assert_eq!(c.signature_validity_period(),
155 Some(Duration::new(24 * 60 * 60, 0)));
156
157 ok = true;
158 break;
159 }
160 }
161 assert!(ok, "Didn't find user id");
162
163 // It should fail if the User ID doesn't exist.
164 assert!(sq.try_pki_vouch_add(
165 &[], &alice_handle, bob_pgp.last().unwrap(), &["bob"],
166 None).is_err());
167
168 // With a notation.
169 sq.tick(1);
170 let bob_pgp_new = sq.scratch_file(None);
171 let cert = sq.pki_vouch_add(
172 &[
173 "--signature-notation", "foo", "bar",
174 "--signature-notation", "!foo", "xyzzy",
175 "--signature-notation", "[email protected]", "1234567890",
176 ],
177 &alice_handle, bob_pgp.last().unwrap(), &["<[email protected]>"],
178 Some(&*bob_pgp_new));
179 bob_pgp.push(bob_pgp_new);
180 certification_count += 1;
181 assert_eq!(cert.bad_signatures().count(), 0,
182 "Bad signatures in cert\n\n{}",
183 String::from_utf8(cert.armored().to_vec().unwrap()).unwrap());
184
185 // The standard policy will reject the
186 // certification, because it has an unknown
187 // critical notation.
188 let vc = cert.with_policy(P, None).unwrap();
189 for ua in vc.userids() {
190 if ua.userid().value() == b"<[email protected]>" {
191 assert_eq!(
192 ua.bundle().certifications().count(),
193 certification_count);
194 let certifications: Vec<_>
195 = ua.certifications().collect();
196 assert_eq!(
197 certifications.len(),
198 // Subtract the bad one.
199 certification_count - 1);
200 }
201 }
202
203 // Accept the critical notation.
204 let p = &mut StandardPolicy::new();
205 p.good_critical_notations(&["foo"]);
206 let vc = cert.with_policy(p, None).unwrap();
207
208 let mut ok = false;
209 for ua in vc.userids() {
210 if ua.userid().value() == b"<[email protected]>" {
211 assert_eq!(ua.bundle().certifications().count(),
212 certification_count);
213
214 let certifications: Vec<_>
215 = ua.certifications().collect();
216 assert_eq!(certifications.len(), certification_count);
217
218 let c = certifications[0];
219
220 assert_eq!(c.trust_signature(), None);
221 assert_eq!(c.regular_expressions().count(), 0);
222 assert_eq!(c.revocable().unwrap_or(true), true);
223 assert_eq!(c.exportable_certification().unwrap_or(true), true);
224 // By default, we set a duration.
225 assert!(c.signature_validity_period().is_some());
226
227 let hr = NotationDataFlags::empty().set_human_readable();
228 let notations = &mut [
229 (NotationData::new("foo", "bar", hr.clone()), false),
230 (NotationData::new("foo", "xyzzy", hr.clone()), false),
231 (NotationData::new("[email protected]", "1234567890", hr), false)
232 ];
233
234 for n in c.notation_data() {
235 if n.name() == "[email protected]" {
236 continue;
237 }
238
239 for (m, found) in notations.iter_mut() {
240 if n == m {
241 assert!(!*found);
242 *found = true;
243 }
244 }
245 }
246 for (n, found) in notations.iter() {
247 assert!(found, "Missing: {:?}", n);
248 }
249
250 ok = true;
251 break;
252 }
253 }
254 assert!(ok, "Didn't find user id");
255 }
256 Ok(())
257 }
258
259 #[test]
260 fn sq_pki_vouch_add_creation_time() -> Result<()>
261 {
262 // $ date +'%Y%m%dT%H%M%S%z'; date +'%s'
263 let t = 1642692756;
264 let t = time::UNIX_EPOCH + time::Duration::new(t, 0);
265
266 let sq = Sq::at(t);
267
268 let alice = "<[email protected]>";
269 let (alice_key, alice_pgp, _alice_rev)
270 = sq.key_generate(&[], &[ alice ]);
271
272 let bob = "<[email protected]>";
273 let (_bob_key, bob_pgp, _bob_rev)
274 = sq.key_generate(&[], &[ bob ]);
275
276
277 for keystore in [false, true] {
278 if keystore {
279 sq.key_import(&alice_pgp);
280 }
281
282 let alice_handle: FileOrKeyHandle = if keystore {
283 alice_key.key_handle().into()
284 } else {
285 alice_pgp.clone().into()
286 };
287
288 // Alice certifies bob's key.
289 let cert = sq.pki_vouch_add(
290 &[], &alice_handle, &bob_pgp, &[bob], None);
291
292 assert_eq!(cert.bad_signatures().count(), 0,
293 "Bad signatures in cert\n\n{}",
294 String::from_utf8(cert.armored().to_vec().unwrap()).unwrap());
295
296 let vc = cert.with_policy(P, t)?;
297
298 assert_eq!(vc.primary_key().key().creation_time(), t);
299
300 let mut userid = None;
301 for u in vc.userids() {
302 if u.userid().value() == bob.as_bytes() {
303 userid = Some(u);
304 break;
305 }
306 }
307
308 if let Some(userid) = userid {
309 let certifications: Vec<_> = userid.certifications().collect();
310 assert_eq!(certifications.len(), 1);
311 let certification = certifications.into_iter().next().unwrap();
312
313 assert_eq!(certification.get_issuers().into_iter().next(),
314 Some(KeyHandle::from(alice_key.fingerprint())));
315
316 assert_eq!(certification.signature_creation_time(), Some(t));
317 } else {
318 panic!("missing user id");
319 }
320 }
321
322 Ok(())
323 }
324
325 #[test]
326 fn sq_pki_vouch_add_with_expired_key() -> Result<()>
327 {
328 let seconds_in_day = 24 * 60 * 60;
329
330 // Alice's certificate expires in 30 days.
331 let validity_seconds = 30 * seconds_in_day;
332 let validity = time::Duration::new(validity_seconds, 0);
333
334 let creation_time = time::SystemTime::now() - 2 * validity;
335 let mut sq = Sq::at(creation_time);
336
337 let alice = "<[email protected]>";
338 let (alice_key, alice_pgp, _) = sq.key_generate(
339 &["--expiration", &format!("{}s", validity_seconds) ],
340 &[ alice ]);
341
342 // Bob's certificate has the same creation time, but it does not
343 // expire.
344 let bob = "<[email protected]>";
345 let (_bob_key, bob_pgp, _) = sq.key_generate(&[], &[ bob ]);
346
347 for keystore in [false, true] {
348 if keystore {
349 sq.key_import(&alice_pgp);
350 }
351
352 let alice_handle: FileOrKeyHandle = if keystore {
353 alice_key.key_handle().into()
354 } else {
355 alice_pgp.clone().into()
356 };
357
358 // Alice's expired key certifies bob's not expired key.
359 sq.tick(validity_seconds + 1);
360
361 // Make sure using an expired key fails by default.
362 assert!(sq.try_pki_vouch_add(
363 &[], &alice_handle, &bob_pgp, &[bob], Some(&*bob_pgp)).is_err());
364 }
365
366 Ok(())
367 }
368
369 #[test]
370 fn sq_pki_vouch_add_with_revoked_key() -> Result<()>
371 {
372 let seconds_in_day = 24 * 60 * 60;
373
374 let delta = seconds_in_day;
375 let creation_time =
376 time::SystemTime::now() - time::Duration::new(delta, 0);
377
378 let mut sq = Sq::at(creation_time);
379
380 // Create a certificate for alice and immediately revoke it.
381 let alice = "<[email protected]>";
382 let (alice_key, alice_pgp, revocation)
383 = sq.key_generate(&[], &[ alice ]);
384
385 let revocation = PacketPile::from_file(revocation)
386 .expect("can parse revocation certificate");
387 let revocation = revocation
388 .descendants()
389 .filter(|p| {
390 if let Packet::Signature(_) = p {
391 true
392 } else {
393 false
394 }
395 })
396 .cloned().collect::<Vec<_>>();
397
398 let alice_key = alice_key.insert_packets(revocation)?.0;
399 {
400 let mut file = File::create(&alice_pgp)?;
401 alice_key.as_tsk().serialize(&mut file)?;
402 }
403 eprintln!("Alice:\n{}", sq.inspect(&alice_pgp));
404
405 let bob = "<[email protected]>";
406 let (_bob_key, bob_pgp, _) = sq.key_generate(&[], &[ bob ]);
407 eprintln!("Bob:\n{}", sq.inspect(&bob_pgp));
408
409 for keystore in [false, true] {
410 if keystore {
411 sq.key_import(&alice_pgp);
412 }
413
414 let alice_handle: FileOrKeyHandle = if keystore {
415 alice_key.key_handle().into()
416 } else {
417 alice_pgp.clone().into()
418 };
419
420 sq.tick(delta);
421
422 // Make sure using an expired key fails by default.
423 assert!(sq.try_pki_vouch_add(
424 &[], &alice_handle, &bob_pgp, &[bob], None).is_err());
425 }
426
427 Ok(())
428 }
429
430 // Certify a certificate in the cert store.
431 #[test]
432 fn sq_pki_vouch_add_using_cert_store() -> Result<()>
433 {
434 let mut sq = Sq::new();
435
436 let (alice_key, alice_pgp, _alice_rev)
437 = sq.key_generate(&[], &["<[email protected]>"]);
438 let (bob_key, bob_pgp, _bob_rev)
439 = sq.key_generate(&[], &["<[email protected]>"]);
440
441 // Import bob's (but not yet alice's!).
442 sq.cert_import(&bob_pgp);
443
444 let mut certification_count = 0;
445 for keystore in [false, true] {
446 if keystore {
447 sq.key_import(&alice_pgp);
448 }
449
450 let alice_handle: FileOrKeyHandle = if keystore {
451 alice_key.key_handle().into()
452 } else {
453 alice_pgp.clone().into()
454 };
455
456 sq.tick(1);
457
458 // Have alice certify bob.
459 let found = sq.pki_vouch_add(
460 &[], &alice_handle,
461 bob_key.key_handle(), &["<[email protected]>"],
462 None);
463 certification_count += 1;
464
465 // Make sure the certificate on stdout is bob and that alice
466 // signed it.
467 assert_eq!(found.fingerprint(), bob_key.fingerprint());
468 assert_eq!(found.userids().count(), 1);
469
470 let ua = found.userids().next().expect("have one");
471 let certifications: Vec<_> = ua.certifications().collect();
472 assert_eq!(certifications.len(), certification_count);
473 let certification = certifications.into_iter().next().unwrap();
474
475 assert_eq!(certification.get_issuers().into_iter().next(),
476 Some(KeyHandle::from(alice_key.fingerprint())));
477 certification.clone().verify_userid_binding(
478 alice_key.primary_key().key(),
479 bob_key.primary_key().key(),
480 &UserID::from("<[email protected]>"))
481 .expect("valid certification");
482 }
483
484 Ok(())
485 }
486
487 // Make sure we reject making self signatures.
488 #[test]
489 fn sq_pki_vouch_add_no_self_signatures() -> Result<()>
490 {
491 let sq = Sq::new();
492
493 let alice_userid = "<[email protected]>";
494 let (alice, alice_pgp, _alice_rev)
495 = sq.key_generate(&[], &[alice_userid]);
496 sq.key_import(&alice_pgp);
497
498 let r = sq.try_pki_vouch_add(
499 &[], &alice.key_handle(),
500 alice.key_handle(), &[alice_userid],
501 None);
502 assert!(r.is_err());
503
504 Ok(())
505 }
506
507 #[test]
508 fn userid_designators() {
509 // Check that the different user ID designators work.
510 for authorize in [false, true] {
511 let mut sq = Sq::new();
512
513 let (ca, ca_path, _rev_path) = sq.key_generate(
514 &[], &["CA <[email protected]>" ]);
515 sq.key_import(ca_path);
516
517 sq.pki_link_authorize(
518 &["--unconstrained", "--all"],
519 ca.key_handle(), NO_USERIDS);
520
521
522 let vouch_maybe = |sq: &mut Sq,
523 kh: KeyHandle, userid_arg: UserIDArg|
524 -> Result<_>
525 {
526 sq.tick(1);
527 if authorize {
528 sq.try_pki_vouch_authorize(
529 &["--unconstrained"], ca.key_handle(),
530 kh, &[ userid_arg ],
531 None)
532 } else {
533 sq.try_pki_vouch_add(
534 &[], ca.key_handle(),
535 kh, &[ userid_arg ],
536 None)
537 }
538 };
539
540 let vouch = |sq: &mut Sq,
541 kh: KeyHandle, userid_arg: UserIDArg|
542 {
543 vouch_maybe(sq, kh, userid_arg)
544 .expect("success")
545 };
546
547 let (cert, cert_path, _rev_path) = sq.key_generate(
548 &[], &[
549 "Alice <[email protected]>",
550 "Alice <[email protected]>",
551 "Alice <[email protected]>",
552 "Alice <[email protected]>",
553 ]);
554 let fpr = cert.fingerprint().to_string();
555 sq.key_import(cert_path);
556
557
558 // 1. Use --userid to certify "Alice <[email protected]>", which is a
559 // self-signed user ID.
560 vouch(&mut sq, cert.key_handle(),
561 UserIDArg::UserID("Alice <[email protected]>"));
562 assert!(sq.pki_authenticate(
563 &[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok());
564
565
566 // 2. Use --add-userid to certify "Alice <[email protected]>",
567 // which is not a self-signed user ID.
568
569 // This fails with --userid, because it expects a self-signed
570 // user ID.
571 assert!(vouch_maybe(
572 &mut sq, cert.key_handle(),
573 UserIDArg::UserID("Alice <[email protected]>")).is_err());
574
575 // But it works with --add-userid.
576 vouch(&mut sq, cert.key_handle(),
577 UserIDArg::AddUserID("Alice <[email protected]>"));
578 assert!(sq.pki_authenticate(
579 &[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok());
580
581
582 // 3. Use --userid-by-email to certify "Alice <[email protected]>",
583 // which is a self-signed user ID.
584 //
585 // --userid-by-email => the email address must be part of a
586 // self-signed user ID.
587 vouch(&mut sq, cert.key_handle(),
588 UserIDArg::ByEmail("[email protected]"));
589
590 assert!(sq.pki_authenticate(
591 &[], &fpr, UserIDArg::UserID("<[email protected]>")).is_err());
592 assert!(sq.pki_authenticate(
593 &[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_ok());
594
595
596 // 4. Use --email to certify "<[email protected]>", which is part
597 // of the self-signed user ID "Alice <[email protected]>".
598 //
599 // --email => there must be a self-signed user ID with the
600 // email address, but a user ID with just the email address is
601 // used.
602 vouch(&mut sq, cert.key_handle(),
603 UserIDArg::Email("[email protected]"));
604
605 assert!(sq.pki_authenticate(
606 &[], &fpr, UserIDArg::UserID("<[email protected]>")).is_ok());
607 if ! authorize {
608 assert!(sq.pki_authenticate(
609 &[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err());
610 }
611
612
613 // 5. Use --add-email to certify "<[email protected]>",
614 // which is not part of a self signed user ID.
615
616 // This fails with --email, because it expects a self-signed
617 // user ID.
618 assert!(vouch_maybe(
619 &mut sq, cert.key_handle(),
620 UserIDArg::Email("[email protected]")).is_err());
621
622 // But it works with --add-email.
623 vouch(&mut sq,
624 cert.key_handle(), UserIDArg::AddEmail("[email protected]"));
625 assert!(sq.pki_authenticate(
626 &[], &fpr, UserIDArg::UserID("<[email protected]>")).is_ok());
627
628 // Use --add-email to link "<[email protected]>", which is
629 // part of the self signed user ID "Alice <[email protected]>".
630 // This should link "<[email protected]>", not the self-signed
631 // user ID.
632 vouch(&mut sq,
633 cert.key_handle(), UserIDArg::AddEmail("[email protected]"));
634 assert!(sq.pki_authenticate(
635 &[], &fpr, UserIDArg::UserID("<[email protected]>")).is_ok());
636 if ! authorize {
637 assert!(sq.pki_authenticate(
638 &[], &fpr, UserIDArg::UserID("Alice <[email protected]>")).is_err());
639 }
640 }
641 }