Debian Codegraph @

tests/integration/sq_encrypt.rs

raw
1 use std::collections::HashSet;
2
3 use sequoia_openpgp as openpgp;
4 use openpgp::Cert;
5 use openpgp::KeyHandle;
6 use openpgp::KeyID;
7 use openpgp::Fingerprint;
8 use openpgp::Packet;
9 use openpgp::parse::PacketParser;
10 use openpgp::parse::PacketParserResult;
11 use openpgp::parse::Parse;
12 use openpgp::Result;
13
14 use crate::integration::common::power_set;
15 use crate::integration::common::Sq;
16 use crate::integration::common::STANDARD_POLICY;
17
18 // Encrypts a message to the specified recipients. If
19 // `decryption_keys` is empty, asserts that encrypt failed. If
20 // `decryption_keys` is not empty, asserts that the recipients of the
21 // PKESKs match `decryption_keys`.
22 fn try_encrypt(sq: &Sq, extra_args: &[&str],
23 decryption_keys: &[&Fingerprint],
24 recipient_certs: &[&Fingerprint],
25 recipient_userids: &[&str],
26 recipient_emails: &[&str])
27 {
28 let mut args: Vec<&str> = extra_args.to_vec();
29
30 let recipient_certs_ = recipient_certs
31 .iter()
32 .map(|fpr| fpr.to_string())
33 .collect::<Vec<_>>();
34 for recipient_cert in recipient_certs_.iter() {
35 args.push("--for");
36 args.push(&recipient_cert);
37 }
38
39 for recipient_userid in recipient_userids.iter() {
40 args.push("--for-userid");
41 args.push(&recipient_userid);
42 }
43
44 for recipient_email in recipient_emails.iter() {
45 args.push("--for-email");
46 args.push(&recipient_email);
47 }
48
49 eprintln!("sq encrypt {:?}", args);
50
51 let message = format!("{:?}", args);
52
53 let result = sq.encrypt_maybe(&args, message.as_bytes());
54
55 match result {
56 Err(err) => {
57 assert!(decryption_keys.is_empty(),
58 "Error encrypting message: {}", err);
59 }
60 Ok(ciphertext) => {
61 // Make sure we can decrypt the message using the keys
62 // on the key store.
63 let plaintext = sq.decrypt(&[], &ciphertext);
64 assert_eq!(message.as_bytes(), plaintext);
65
66 let mut die = false;
67
68 let mut actual_recipients = Vec::new();
69 let mut ppr = PacketParser::from_bytes(&ciphertext)
70 .expect("valid ciphertet");
71 while let PacketParserResult::Some(pp) = ppr {
72 let (packet, next_ppr) = pp.next().expect("valid message");
73 ppr = next_ppr;
74
75 // Process the packet.
76 if let Packet::PKESK(pkesk) = packet {
77 eprintln!(" PKESK for {:?}", pkesk.recipient());
78 actual_recipients.push(pkesk.recipient().map(KeyID::from));
79 }
80 }
81 actual_recipients.sort();
82
83 // Make sure the recipients are deduped.
84 for rs in actual_recipients.windows(2) {
85 let r1 = &rs[0];
86 let r2 = &rs[1];
87
88 if r1 == r2 {
89 eprintln!("Multiple PKESKs for the same recipient ({:?}).",
90 r1);
91 die = true;
92 }
93 }
94
95 let actual_recipients: HashSet<KeyID>
96 = actual_recipients.into_iter().filter_map(|x| x).collect();
97
98 let mut decryption_keyids = decryption_keys
99 .iter()
100 .map(|fpr| KeyID::from(*fpr))
101 .collect::<Vec<KeyID>>();
102 decryption_keyids.sort();
103 let decryption_keyids
104 = HashSet::from_iter(decryption_keyids.into_iter());
105
106 for missing in decryption_keyids.difference(&actual_recipients) {
107 eprintln!("Message should have been encrypted to {}, \
108 but wasn't",
109 missing);
110 die = true;
111 }
112
113 for extra in actual_recipients.difference(&decryption_keyids) {
114 eprintln!("Message was encrypted to {}, \
115 but shouldn't have been",
116 extra);
117 die = true;
118 }
119
120 if die {
121 eprintln!("Actual recipients: {:?}", actual_recipients);
122 eprintln!("Expected: {:?}", decryption_keyids);
123 panic!("Something went wrong");
124 }
125 }
126 }
127 }
128
129 #[test]
130 fn sq_encrypt_using_cert_store() -> Result<()>
131 {
132 let sq = Sq::new();
133
134 // Generate a key.
135 let (cert, key_pgp, _key_rev) = sq.key_generate(
136 &["--expiration", "never"],
137 &["<[email protected]>"]);
138 let key_pgp = key_pgp.to_str().expect("valid UTF-8");
139
140 // Try to encrypt a message. This should fail, because we
141 // haven't imported the key.
142 for kh in [KeyHandle::from(cert.fingerprint()),
143 KeyHandle::from(cert.keyid())]
144 {
145 assert!(
146 sq.encrypt_maybe(&["--for", &kh.to_string()], &b""[..])
147 .is_err());
148 }
149
150 // Import the certificate.
151 sq.cert_import(key_pgp);
152
153 const MESSAGE: &[u8] = b"\na secret message\n\nor two\n";
154
155 // Now we should be able to encrypt a message to it, and
156 // decrypt it.
157 for kh in [KeyHandle::from(cert.fingerprint()),
158 KeyHandle::from(cert.keyid())]
159 {
160 let ciphertext = sq.encrypt(
161 &["--for", &kh.to_string()], MESSAGE);
162
163 let plaintext = sq.decrypt(
164 &["--recipient-file", &key_pgp], ciphertext);
165
166 assert_eq!(MESSAGE, plaintext);
167 }
168
169 Ok(())
170 }
171
172 #[test]
173 fn sq_encrypt_recipient_userid() -> Result<()>
174 {
175 let sq = Sq::new();
176
177 // Generate the keys.
178 let (alice, alice_pgp, _alice_rev) = sq.key_generate(
179 &["--expiration", "never"],
180 &["<[email protected]>"]);
181 let alice_pgp = alice_pgp.to_str().expect("valid UTF-8");
182
183 let bob_userids = &[
184 "<[email protected]>",
185 "Bob <[email protected]>",
186 "<[email protected]>",
187 ];
188 let bob_emails = &[
189 "[email protected]",
190 "[email protected]",
191 ];
192
193 let bob_certified_userids = &[
194 "Bob <[email protected]>",
195 ];
196 let bob_certified_emails = &[
197 "[email protected]",
198 ];
199
200 let (bob, bob_pgp, _bob_rev) = sq.key_generate(
201 &["--expiration", "never"],
202 bob_userids);
203 let bob_pgp = bob_pgp.to_str().expect("valid UTF-8");
204
205 // Import the certificates.
206 sq.cert_import(alice_pgp);
207 sq.cert_import(bob_pgp);
208
209 const MESSAGE: &[u8] = &[0x42; 24 * 1024 + 23];
210 let encrypt = |trust_roots: &[&str],
211 recipients: &[(&str, &str)],
212 decryption_keys: &[&str]|
213 {
214 let mut args = Vec::new();
215
216 for trust_root in trust_roots {
217 args.push("--trust-root");
218 args.push(trust_root);
219 }
220
221 for (option, recipient) in recipients.iter() {
222 args.push(option);
223 args.push(recipient);
224 }
225
226 let result = sq.encrypt_maybe(&args, MESSAGE);
227
228 if decryption_keys.is_empty() {
229 assert!(result.is_err(), "should have failed");
230 } else {
231 let ciphertext = result.expect("should have succeeded");
232
233 let args = decryption_keys.iter()
234 .flat_map(|k| vec![ "--recipient-file", k ])
235 .collect::<Vec<&str>>();
236 let plaintext = sq.decrypt(&args, ciphertext);
237
238 assert_eq!(MESSAGE, plaintext);
239 }
240 };
241
242 // Encryption by fingerprint should work.
243 encrypt(&[],
244 &[("--for", &bob.fingerprint().to_string())],
245 &[&bob_pgp]);
246
247 // Encryption by email address and user id should fail if the
248 // binding can't be authenticated.
249 for email in bob_emails.iter() {
250 encrypt(&[],
251 &[("--for-email", email)],
252 &[]);
253 }
254 for userid in bob_userids.iter() {
255 encrypt(&[],
256 &[("--for-userid", userid)],
257 &[]);
258 }
259
260 // Alice certifies Bob's certificate.
261 for userid in bob_certified_userids {
262 let certification
263 = sq.scratch_file(Some(&format!("alice-certifies-{}", userid)[..]));
264 sq.pki_vouch_add(&[], alice_pgp, bob_pgp, &[userid],
265 Some(certification.as_path()));
266 sq.cert_import(certification);
267 }
268
269 // Still don't use a trust root. This should still fail.
270 for email in bob_emails.iter() {
271 encrypt(&[],
272 &[("--for-email", email)],
273 &[]);
274 }
275 for userid in bob_userids.iter() {
276 encrypt(&[],
277 &[("--for-userid", userid)],
278 &[]);
279 }
280
281 // Make Alice the trust root. This should succeed.
282 for email in bob_emails.iter() {
283 if bob_certified_emails.contains(email) {
284 encrypt(&[&alice.fingerprint().to_string()],
285 &[("--for-email", email)],
286 &[ &bob_pgp ]);
287 } else {
288 encrypt(&[&alice.fingerprint().to_string()],
289 &[("--for-email", email)],
290 &[]);
291 }
292 }
293 for userid in bob_userids.iter() {
294 if bob_certified_userids.contains(userid) {
295 encrypt(&[&alice.fingerprint().to_string()],
296 &[("--for-userid", userid)],
297 &[ &bob_pgp ]);
298 } else {
299 encrypt(&[&alice.fingerprint().to_string()],
300 &[("--for-userid", userid)],
301 &[]);
302 }
303 }
304
305 // Make Bob a trust root. This should succeed for all
306 // self-signed user ids.
307 for email in bob_emails.iter() {
308 encrypt(&[&bob.fingerprint().to_string()],
309 &[("--for-email", email)],
310 &[&bob_pgp]);
311 }
312 for userid in bob_userids.iter() {
313 encrypt(&[&bob.fingerprint().to_string()],
314 &[("--for-userid", userid)],
315 &[&bob_pgp]);
316 }
317
318 Ok(())
319 }
320
321 // Encrypt a message to two recipients: one whose certificate is
322 // in the certificate store, and one whose certificated is in a
323 // keyring.
324 #[test]
325 fn sq_encrypt_keyring() -> Result<()>
326 {
327 let sq = Sq::new();
328
329 // Generate the keys.
330 let (alice, alice_pgp, _alice_rev) = sq.key_generate(
331 &["--expiration", "never"],
332 &["<[email protected]>"]);
333 let alice_pgp = alice_pgp.to_str().expect("valid UTF-8");
334 let alice_fpr = alice.fingerprint().to_string();
335
336 let (bob, bob_pgp, _bob_rev) = sq.key_generate(
337 &["--expiration", "never"],
338 &["<[email protected]>"]);
339 let bob_pgp = bob_pgp.to_str().expect("valid UTF-8");
340 let bob_fpr = bob.fingerprint().to_string();
341
342 const MESSAGE: &[u8] = &[0x42; 24 * 1024 + 23];
343 let encrypt = |keyrings: &[&str],
344 recipients: &[&str],
345 decryption_keys: &[&str]|
346 {
347 let mut args = Vec::new();
348
349 for keyring in keyrings.iter() {
350 args.push("--keyring");
351 args.push(keyring);
352 }
353
354 for recipient in recipients.iter() {
355 args.push("--for");
356 args.push(recipient);
357 }
358
359 let result = sq.encrypt_maybe(&args, MESSAGE);
360
361 match result {
362 Err(err) => {
363 assert!(decryption_keys.is_empty(),
364 "Error encrypting message: {}", err);
365 }
366 Ok(ciphertext) => {
367 for key in decryption_keys.iter() {
368 let plaintext
369 = sq.decrypt(&["--recipient-file", &key], &ciphertext);
370 assert_eq!(MESSAGE, plaintext);
371 }
372 }
373 }
374 };
375
376 encrypt(&[&alice_pgp, &bob_pgp],
377 &[&alice_fpr, &bob_fpr],
378 &[&alice_pgp, &bob_pgp]);
379
380 // Import Alice's certificate.
381 sq.cert_import(&alice_pgp);
382
383 encrypt(&[&alice_pgp, &bob_pgp],
384 &[&alice_fpr, &bob_fpr],
385 &[&alice_pgp, &bob_pgp]);
386
387 encrypt(&[&bob_pgp],
388 &[&alice_fpr, &bob_fpr],
389 &[&alice_pgp, &bob_pgp]);
390
391
392 Ok(())
393 }
394
395 #[test]
396 fn sq_encrypt_with_password() -> Result<()>
397 {
398 let sq = Sq::new();
399
400 let password = "hunter2";
401 let password_file = sq.base().join("password");
402 std::fs::write(&password_file, password)?;
403
404 const MESSAGE: &str = "\na secret message\n\nor two\n";
405
406 let plain_file = sq.base().join("plaintext");
407 std::fs::write(&plain_file, MESSAGE)?;
408
409 let cipher_file = sq.base().join("ciphertext");
410 let mut cmd = sq.command();
411 cmd.args(["encrypt",
412 "--with-password-file", &password_file.display().to_string(),
413 "--without-signature",
414 "--output", &cipher_file.display().to_string(),
415 &plain_file.display().to_string()]);
416 sq.run(cmd, Some(true));
417
418 let mut cmd = sq.command();
419 cmd.args(["decrypt",
420 "--password-file", &password_file.display().to_string(),
421 &cipher_file.display().to_string()]);
422 let output = sq.run(cmd, Some(true));
423 let stdout = String::from_utf8_lossy(&output.stdout);
424 assert_eq!(stdout, MESSAGE);
425
426 Ok(())
427 }
428
429 // Exercise various ways to encrypt a message to a recipient
430 // (--for, --for-userid, and --for-email).
431 // When designating a certificate by name, make sure only
432 // authenticated certificates are used.
433 #[test]
434 fn sq_encrypt_cert_designators() -> Result<()>
435 {
436 let sq = Sq::new();
437
438 // Generate and import the keys.
439 let gen_key = |userids: &[&str]| {
440 let (cert, cert_pgp, _cert_rev)
441 = sq.key_generate(&[], userids);
442 let cert_vc = cert.with_policy(STANDARD_POLICY, None).expect("valid cert");
443 let cert_enc = cert_vc.keys().for_transport_encryption()
444 .map(|ka| ka.key().fingerprint())
445 .collect::<Vec<_>>();
446 assert_eq!(cert_enc.len(), 1);
447 let cert_enc = cert_enc.into_iter().next().unwrap();
448
449 eprintln!("{} {:?} => {}",
450 cert.fingerprint(), userids, cert_enc);
451
452 sq.key_import(&cert_pgp);
453
454 (cert, cert_enc)
455 };
456
457 let alice_email = "[email protected]";
458 let alice_userid = format!("Alice <{}>", alice_email);
459 let (alice, alice_enc) = gen_key(&[&alice_userid]);
460 let alice_fpr = alice.fingerprint();
461
462 sq.pki_link_add(&[], alice.key_handle(), &[&alice_userid]);
463
464 let bob_email1 = "[email protected]";
465 let bob_userid1 = format!("Bob <{}>", bob_email1);
466 let bob_email2 = "[email protected]";
467 let bob_userid2 = format!("Bob <{}>", bob_email2);
468 let (bob, bob_enc) = gen_key(&[&bob_userid1, &bob_userid2]);
469 let bob_fpr = bob.fingerprint();
470
471 sq.pki_link_add(&[], bob.key_handle(), &[&bob_userid1]);
472 sq.pki_link_add(&[], bob.key_handle(), &[&bob_userid2]);
473
474 // Mallory's certificate includes Alice's and Bob's user IDs.
475 // Since Mallory's certificate is not authenticated, it shouldn't
476 // be used when addressing certificates by user ID or email
477 // address.
478 let mallory_email = "[email protected]";
479 let mallory_userid = format!("Mallory <{}>", mallory_email);
480 let (mallory, mallory_enc)
481 = gen_key(&[&mallory_userid, &alice_userid, &bob_userid1, &bob_userid2]);
482 let mallory_fpr = mallory.fingerprint();
483
484 // Check that different subsets of fingerprints, user ids, and
485 // email addresses can be used to select recipients. Check that
486 // we encrypt to the relevant certificates, and only the relevant
487 // certificates. This checks that we only consider authenticated
488 // user IDs: mallory's certificate is completely unauthenticated,
489 // but contains Alice's and Bob's user IDs.
490 let power_set_of = |recipients: &[(&Fingerprint, // Recipient
491 Option<&Fingerprint>, // Fingerprint
492 Option<&str>, // User ID
493 Option<&str>)]| // Email
494 {
495 for recipients in power_set(recipients) {
496 let intended_recipients = recipients.iter()
497 .map(|(fpr, _, _, _)| *fpr)
498 .collect::<Vec<_>>();
499
500 let recipient_fprs = recipients.iter()
501 .filter_map(|(_, fpr, _, _)| fpr.as_ref())
502 .map(|fpr| *fpr)
503 .collect::<Vec<&Fingerprint>>();
504 let recipient_userids = recipients.iter()
505 .filter_map(|(_, _, userid, _)| userid.as_ref())
506 .map(|userid| &userid[..])
507 .collect::<Vec<&str>>();
508 let recipient_emails = recipients.iter()
509 .filter_map(|(_, _, _, email)| email.as_ref())
510 .map(|email| &email[..])
511 .collect::<Vec<&str>>();
512
513 try_encrypt(&sq, &[],
514 &intended_recipients,
515 &recipient_fprs[..],
516 &recipient_userids,
517 &recipient_emails);
518 }
519 };
520
521 // Because a power set results in a combinatorial explosion, we
522 // check the power set of a couple of different smaller sets
523 // instead of one large set.
524
525 let all = &[
526 // Primary fingerprint.
527 (&[-1, 0, 2][..], (&alice_enc, Some(&alice_fpr), None, None)),
528 (&[-1, 2], (&bob_enc, Some(&bob_fpr), None, None)),
529 (&[-1, 0, 1, 4], (&mallory_enc, Some(&mallory_fpr), None, None)),
530 // User ID.
531 (&[-3, 0, 3], (&alice_enc, None, Some(&alice_userid[..]), None)),
532 (&[-3, 2, 3], (&bob_enc, None, Some(&bob_userid1[..]), None)),
533 (&[-3, 1, 3], (&bob_enc, None, Some(&bob_userid2[..]), None)),
534 // Email
535 (&[-4, 1, 4], (&alice_enc, None, None, Some(alice_email))),
536 (&[-4, 0, 4], (&bob_enc, None, None, Some(bob_email1))),
537 (&[-4, 2, 4], (&bob_enc, None, None, Some(bob_email2))),
538 ];
539
540 let mut runs: Vec<isize> = all.iter()
541 .flat_map(|(runs, _)| runs.iter().cloned())
542 .collect();
543 runs.sort();
544 runs.dedup();
545
546 for run in runs.into_iter() {
547 let recipients = all
548 .iter()
549 .filter_map(|(runs, recipient)| {
550 if runs.contains(&run) {
551 Some(recipient.clone())
552 } else {
553 None
554 }
555 })
556 .collect::<Vec<_>>();
557
558 power_set_of(&recipients[..]);
559 }
560
561
562 // Make sure we can't encrypt to mallory's certificate using his
563 // primary user ID (it's not authenticated).
564 try_encrypt(&sq, &[], &[], &[], &[], &[&mallory_email]);
565 try_encrypt(&sq, &[], &[], &[], &[&mallory_userid], &[]);
566
567 // Make sure we don't encrypt succeed even if we have a valid designator.
568 try_encrypt(&sq, &[], &[], &[&alice_fpr], &[], &[&mallory_email]);
569 try_encrypt(&sq, &[], &[], &[&alice_fpr], &[&mallory_userid], &[]);
570 try_encrypt(&sq, &[], &[], &[&mallory_fpr], &[&mallory_userid], &[]);
571
572 Ok(())
573 }
574
575 // Try encrypting to a certificate that does not have an encryption
576 // capable subkey. Make sure it fails.
577 #[test]
578 fn sq_encrypt_not_encryption_capable() -> Result<()>
579 {
580 let sq = Sq::new();
581
582 // Generate the keys. Alice has an encryption capable subkey, but
583 // Bob doesn't.
584 let (alice, alice_pgp, _alice_rev) = sq.key_generate(
585 &[],
586 &["<[email protected]>"]);
587 sq.key_import(alice_pgp);
588
589 let alice_enc = alice.keys().with_policy(STANDARD_POLICY, sq.now())
590 .for_storage_encryption()
591 .next()
592 .expect("have a storage encryption-capable subkey")
593 .key()
594 .fingerprint();
595
596 let (bob, bob_pgp, _bob_rev) = sq.key_generate(
597 &["--cannot-encrypt"],
598 &["<[email protected]>"]);
599 sq.key_import(bob_pgp);
600
601 for (i, (recipients, result)) in [
602 (&[ &alice ][..], &[ &alice_enc ][..]),
603 (&[ &bob ], &[]),
604 (&[ &alice, &bob ], &[]),
605 ].into_iter().enumerate() {
606 eprintln!("Test #{}", i + 1);
607
608 let recipients = recipients.iter().map(|r| r.fingerprint())
609 .collect::<Vec<Fingerprint>>();
610 let recipients = recipients.iter()
611 .collect::<Vec<&Fingerprint>>();
612
613 try_encrypt(&sq, &[], result, &recipients, &[], &[]);
614 }
615
616 Ok(())
617 }
618
619 // Try encrypting to a certificate with an expired subkey. Make sure
620 // it fails, unless '--use-expired-subkey' is provided.
621 #[test]
622 fn sq_encrypt_expired() -> Result<()>
623 {
624 let mut sq = Sq::new();
625
626 // Generate the keys. Alice has an encryption capable subkey, but
627 // Bob doesn't.
628 let (alice, alice_pgp, _alice_rev) = sq.key_generate(
629 &[],
630 &["<[email protected]>"]);
631 sq.key_import(alice_pgp);
632
633 let alice_enc = alice.keys().with_policy(STANDARD_POLICY, sq.now())
634 .for_storage_encryption()
635 .next()
636 .expect("have a storage encryption-capable subkey")
637 .key()
638 .fingerprint();
639
640 let (bob, bob_pgp, _bob_rev) = sq.key_generate(
641 &[],
642 &["<[email protected]>"]);
643 sq.key_import(&bob_pgp);
644
645 let bob_enc = bob.keys().with_policy(STANDARD_POLICY, sq.now())
646 .for_storage_encryption()
647 .next()
648 .expect("have a storage encryption-capable subkey")
649 .key();
650
651 sq.tick(1);
652
653 // Expire in a day.
654 let bob = sq.key_subkey_expire(
655 bob.key_handle(), &[bob_enc.key_handle()], "1d",
656 None, None, true)
657 .expect("can set expiration");
658 sq.cert_import(&bob_pgp);
659
660 let bob_enc = bob_enc.fingerprint();
661
662 // Two days pass...
663 sq.tick(2 * 24 * 60 * 60);
664
665 for (i, (args, recipients, result)) in [
666 (&[][..], &[ &alice ][..], &[ &alice_enc ][..]),
667 (&[], &[ &bob ], &[]),
668 (&[], &[ &alice, &bob ], &[]),
669 (&["--use-expired-subkey"], &[ &alice ], &[ &alice_enc ]),
670 (&["--use-expired-subkey"], &[ &bob ], &[ &bob_enc ]),
671 (&["--use-expired-subkey"], &[ &alice, &bob ], &[ &alice_enc, &bob_enc ]),
672 ].into_iter().enumerate() {
673 eprintln!("Test #{}", i + 1);
674
675 let recipients = recipients.iter().map(|r| r.fingerprint())
676 .collect::<Vec<Fingerprint>>();
677 let recipients = recipients.iter()
678 .collect::<Vec<&Fingerprint>>();
679
680 try_encrypt(&sq, args, result, &recipients, &[], &[]);
681 }
682
683 Ok(())
684 }
685
686 // Try encrypting to a certificate with a revoked subkey. Make sure
687 // it fails.
688 #[test]
689 fn sq_encrypt_revoked_subkey() -> Result<()>
690 {
691 let mut sq = Sq::new();
692
693 // Generate the keys. Alice has an encryption capable subkey, but
694 // Bob doesn't.
695 let (alice, alice_pgp, _alice_rev) = sq.key_generate(
696 &[],
697 &["<[email protected]>"]);
698 sq.key_import(alice_pgp);
699
700 let alice_enc = alice.keys().with_policy(STANDARD_POLICY, sq.now())
701 .for_storage_encryption()
702 .next()
703 .expect("have a storage encryption-capable subkey")
704 .key()
705 .fingerprint();
706
707 let (bob, bob_pgp, _bob_rev) = sq.key_generate(
708 &[],
709 &["<[email protected]>"]);
710 sq.key_import(&bob_pgp);
711
712 let bob_enc = bob.keys().with_policy(STANDARD_POLICY, sq.now())
713 .for_storage_encryption()
714 .next()
715 .expect("have a storage encryption-capable subkey")
716 .key();
717
718 sq.tick(1);
719
720 // Revoke.
721 let bob = sq.key_subkey_revoke(
722 bob.key_handle(), &[bob_enc.key_handle()], None,
723 "retired", "retired this key", None, &[], None);
724
725 // Two days pass...
726 sq.tick(2 * 24 * 60 * 60);
727
728 for (i, (recipients, result)) in [
729 (&[ &alice ][..], &[ &alice_enc ][..]),
730 (&[ &bob ], &[]),
731 (&[ &alice, &bob ], &[]),
732 ].into_iter().enumerate() {
733 eprintln!("Test #{}", i + 1);
734
735 let recipients = recipients.iter().map(|r| r.fingerprint())
736 .collect::<Vec<Fingerprint>>();
737 let recipients = recipients.iter()
738 .collect::<Vec<&Fingerprint>>();
739
740 try_encrypt(&sq, &[], result, &recipients, &[], &[]);
741 }
742
743 Ok(())
744 }
745
746 // Try encrypting to a revoked certificate. Make sure it fails.
747 #[test]
748 fn sq_encrypt_revoked() -> Result<()>
749 {
750 let mut sq = Sq::new();
751
752 // Generate the keys. Alice has an encryption capable subkey, but
753 // Bob doesn't.
754 let (alice, alice_pgp, _alice_rev) = sq.key_generate(
755 &[],
756 &["<[email protected]>"]);
757 sq.key_import(alice_pgp);
758
759 let alice_enc = alice.keys().with_policy(STANDARD_POLICY, sq.now())
760 .for_storage_encryption()
761 .next()
762 .expect("have a storage encryption-capable subkey")
763 .key()
764 .fingerprint();
765
766 let (bob, bob_pgp, _bob_rev) = sq.key_generate(
767 &[],
768 &["<[email protected]>"]);
769 sq.key_import(&bob_pgp);
770
771 sq.tick(1);
772
773 // Revoke.
774 let bob = sq.key_revoke(
775 bob.key_handle(), None,
776 "retired", "retired this key", None, &[], None);
777
778 // Two days pass...
779 sq.tick(2 * 24 * 60 * 60);
780
781 for (i, (recipients, result)) in [
782 (&[ &alice ][..], &[ &alice_enc ][..]),
783 (&[ &bob ], &[]),
784 (&[ &alice, &bob ], &[]),
785 ].into_iter().enumerate() {
786 eprintln!("Test #{}", i + 1);
787
788 let recipients = recipients.iter().map(|r| r.fingerprint())
789 .collect::<Vec<Fingerprint>>();
790 let recipients = recipients.iter()
791 .collect::<Vec<&Fingerprint>>();
792
793 try_encrypt(&sq, &[], result, &recipients, &[], &[]);
794 }
795
796 Ok(())
797 }
798
799 // Try encrypting to a certificate with a weakly bound key. Make sure
800 // it fails.
801 #[test]
802 fn sq_encrypt_weak() -> Result<()>
803 {
804 let mut sq = Sq::new();
805
806 let cert_path = sq.test_data()
807 .join("keys")
808 .join("sha1-subkey-priv.pgp");
809 sq.key_import(&cert_path);
810
811 let cert = Cert::from_file(&cert_path).expect("can read");
812
813 sq.tick(1);
814
815 // Two days pass...
816 sq.tick(2 * 24 * 60 * 60);
817
818 try_encrypt(&sq, &[], &[], &[ &cert.fingerprint() ], &[], &[]);
819
820 Ok(())
821 }