Debian Codegraph @

tests/integration/sq_key_subkey_bind.rs

raw
1 use super::common::FileOrKeyHandle;
2 use super::common::{Sq, artifact};
3
4 use std::path;
5
6 use sequoia_openpgp as openpgp;
7
8 use openpgp::Fingerprint;
9 use openpgp::KeyHandle;
10 use openpgp::Result;
11 use openpgp::cert::prelude::*;
12 use openpgp::parse::Parse;
13 use openpgp::policy::StandardPolicy;
14 use openpgp::types::KeyFlags;
15
16 fn alice() -> path::PathBuf {
17 // Fingerprint: 5CCB BA06 74EA 5162 615E 36E9 80E5 ADE9 43CA 0DC3
18 // Public-key algo: EdDSA Edwards-curve Digital Signature Algorithm
19 // Public-key size: 256 bits
20 // Secret key: Unencrypted
21 // Creation time: 2020-12-21 23:00:49 UTC
22 // Key flags: certification
23 //
24 // Subkey: 6A3B 1EC7 6233 62BC 066E 75AB DC42 7976 95D6 24E5
25 // Public-key algo: EdDSA Edwards-curve Digital Signature Algorithm
26 // Public-key size: 256 bits
27 // Secret key: Unencrypted
28 // Creation time: 2020-12-21 23:00:49 UTC
29 // Key flags: signing
30 //
31 // Subkey: 827E 4397 F330 7EDA 6ABD 2A6E AD9C 461D 6D2F 0982
32 // Public-key algo: ECDH public key algorithm
33 // Public-key size: 256 bits
34 // Secret key: Unencrypted
35 // Creation time: 2020-12-21 23:00:49 UTC
36 // Key flags: transport encryption, data-at-rest encryption
37 //
38 // UserID: Alice Lovelace <[email protected]>
39 artifact("keys").join("alice-lovelace-encryption-subkey-signing-subkey-priv.pgp")
40 }
41 fn alice_primary() -> (Fingerprint, KeyFlags) {
42 ("5CCB BA06 74EA 5162 615E 36E9 80E5 ADE9 43CA 0DC3".parse().unwrap(),
43 KeyFlags::empty().set_certification())
44 }
45 fn alice_signing() -> (Fingerprint, KeyFlags) {
46 ("6A3B 1EC7 6233 62BC 066E 75AB DC42 7976 95D6 24E5".parse().unwrap(),
47 KeyFlags::empty().set_signing())
48 }
49 fn alice_encryption() -> (Fingerprint, KeyFlags) {
50 ("827E 4397 F330 7EDA 6ABD 2A6E AD9C 461D 6D2F 0982".parse().unwrap(),
51 KeyFlags::empty().set_transport_encryption().set_storage_encryption())
52 }
53 fn bob() -> path::PathBuf {
54 // Fingerprint: C1CF 22F6 C838 07CE 3901 6CDE 8463 B196 87EE 13BB
55 // Public-key algo: EdDSA Edwards-curve Digital Signature Algorithm
56 // Public-key size: 256 bits
57 // Secret key: Unencrypted
58 // Creation time: 2020-12-21 23:02:23 UTC
59 // Key flags: certification
60 //
61 // UserID: Bob Babbage <[email protected]>
62 artifact("keys").join("bob-babbage-cert-only-priv.pgp")
63 }
64 fn bob_primary() -> (Fingerprint, KeyFlags) {
65 ("C1CF 22F6 C838 07CE 3901 6CDE 8463 B196 87EE 13BB".parse().unwrap(),
66 KeyFlags::empty().set_certification())
67 }
68
69 fn carol() -> path::PathBuf {
70 // Fingerprint: 0B17 34A8 2726 A5D1 D5AC 1568 1EC1 4781 FD88 09B4
71 // Public-key algo: EdDSA Edwards-curve Digital Signature Algorithm
72 // Public-key size: 256 bits
73 // Secret key: Unencrypted
74 // Creation time: 2020-12-22 00:02:24 UTC
75 // Key flags: certification
76 //
77 // Subkey: 3D56 A424 3D5C C345 638D FB19 05D8 B9EA DB92 A8C1
78 // Public-key algo: EdDSA Edwards-curve Digital Signature Algorithm
79 // Public-key size: 256 bits
80 // Secret key: Unencrypted
81 // Creation time: 2020-12-22 00:02:24 UTC
82 // Key flags: signing
83 //
84 // Subkey: 1F47 6866 1260 CFFA D3DE B630 5652 476A 8B74 5CE5
85 // Public-key algo: ECDH public key algorithm
86 // Public-key size: 256 bits
87 // Secret key: Unencrypted
88 // Creation time: 2020-12-22 00:02:24 UTC
89 // Key flags: transport encryption, data-at-rest encryption
90 //
91 // UserID: Carol <[email protected]>
92 artifact("keys").join("carol-encryption-subkey-signing-subkey-priv.pgp")
93 }
94 fn carol_primary() -> (Fingerprint, KeyFlags) {
95 ("0B17 34A8 2726 A5D1 D5AC 1568 1EC1 4781 FD88 09B4".parse().unwrap(),
96 KeyFlags::empty().set_certification())
97 }
98 fn carol_signing() -> (Fingerprint, KeyFlags) {
99 ("3D56 A424 3D5C C345 638D FB19 05D8 B9EA DB92 A8C1".parse().unwrap(),
100 KeyFlags::empty().set_signing())
101 }
102 fn carol_encryption() -> (Fingerprint, KeyFlags) {
103 ("1F47 6866 1260 CFFA D3DE B630 5652 476A 8B74 5CE5".parse().unwrap(),
104 KeyFlags::empty().set_transport_encryption().set_storage_encryption())
105 }
106
107 fn bare() -> path::PathBuf {
108 // $ sq inspect bare.pgp
109 // bare.pgp: Revocation Certificate.
110 //
111 // Fingerprint: B321BA8F650CB16443E06826DBFA98A78CF6562F
112 // Invalid: No binding signature at time 2024-09-26T08:21:08Z
113 // Public-key algo: RSA
114 // Public-key size: 2048 bits
115 // Creation time: 1970-01-01 00:00:00 UTC
116 artifact("keys").join("bare.pgp")
117 }
118 fn bare_signing() -> (Fingerprint, KeyFlags) {
119 ("B321BA8F650CB16443E06826DBFA98A78CF6562F".parse().unwrap(),
120 KeyFlags::empty().set_signing())
121 }
122
123 fn check(cert: &Cert,
124 key_count: usize,
125 keys: ((Fingerprint, KeyFlags), &[(Fingerprint, KeyFlags)]))
126 -> Result<()>
127 {
128 let p = &StandardPolicy::new();
129
130 let vc = cert.with_policy(p, None).unwrap();
131
132 assert_eq!(key_count, vc.keys().count());
133
134 assert_eq!(vc.primary_key().key().fingerprint(), keys.0.0);
135 assert_eq!(vc.primary_key().key_flags(), Some(keys.0.1));
136
137 for (subkey, keyflags) in keys.1 {
138 let mut found = false;
139 for k in vc.keys().subkeys() {
140 if k.key().fingerprint() == *subkey {
141 assert_eq!(k.key_flags().as_ref(), Some(keyflags));
142 found = true;
143 break;
144 }
145 }
146 assert!(found);
147 }
148
149 Ok(())
150 }
151
152 #[test]
153 fn bind_encryption() -> Result<()> {
154 for (keyrings, key_imports, handle) in [
155 (
156 // Keyrings
157 &[ alice() ][..],
158 // Key store imports.
159 &[][..],
160 // Handle
161 FileOrKeyHandle::from(bob()),
162 ),
163 (
164 // Keyrings
165 &[][..],
166 // Key store imports.
167 &[ alice() ],
168 // Handle
169 bob().into()
170 ),
171 (
172 // Keyrings
173 &[][..],
174 // Key store imports.
175 &[ alice(), bob() ],
176 // Handle
177 bob_primary().0.into()
178 ),
179 ] {
180 let sq = Sq::new();
181
182 for file in key_imports {
183 sq.key_import(file);
184 }
185
186 // Have Bob bind alice's encryption subkey.
187 let cert = sq.key_subkey_bind(
188 &[],
189 keyrings.to_vec(),
190 handle,
191 [ alice_encryption().0.clone() ].to_vec(),
192 "-");
193
194 assert!(
195 check(&cert, 2, (bob_primary(), &[alice_encryption()])).is_ok());
196 }
197
198 Ok(())
199 }
200
201 #[test]
202 fn bind_signing() -> Result<()> {
203 for (keyrings, key_imports, handle) in [
204 (
205 // Keyrings
206 &[ alice() ][..],
207 // Key store imports.
208 &[][..],
209 // Handle
210 FileOrKeyHandle::from(bob()),
211 ),
212 (
213 // Keyrings
214 &[][..],
215 // Key store imports.
216 &[ alice() ],
217 // Handle
218 bob().into()
219 ),
220 (
221 // Keyrings
222 &[][..],
223 // Key store imports.
224 &[ alice(), bob() ],
225 // Handle
226 bob_primary().0.into()
227 ),
228 ] {
229 let sq = Sq::new();
230
231 for file in key_imports {
232 sq.key_import(file);
233 }
234
235 // Bind a signing subkey (subkey has secret key material).
236 let cert = sq.key_subkey_bind(
237 &[],
238 keyrings.to_vec(),
239 handle,
240 [ alice_signing().0.clone() ].to_vec(),
241 "-");
242
243 assert!(
244 check(&cert, 2, (bob_primary(), &[alice_signing()])).is_ok());
245 }
246
247 Ok(())
248 }
249
250 #[test]
251 fn bind_certification() -> Result<()> {
252 for (keyrings, key_imports, handle) in [
253 (
254 // Keyrings
255 &[ alice() ][..],
256 // Key store imports.
257 &[][..],
258 // Handle
259 FileOrKeyHandle::from(carol()),
260 ),
261 (
262 // Keyrings
263 &[][..],
264 // Key store imports.
265 &[ alice() ],
266 // Handle
267 carol().into()
268 ),
269 (
270 // Keyrings
271 &[][..],
272 // Key store imports.
273 &[ alice(), carol() ],
274 // Handle
275 carol_primary().0.into()
276 ),
277 ] {
278 let sq = Sq::new();
279
280 for file in key_imports {
281 sq.key_import(file);
282 }
283
284 // Bind a certification subkey (subkey has secret key
285 // material).
286 let cert = sq.key_subkey_bind(
287 &[],
288 keyrings.to_vec(),
289 handle,
290 [ alice_primary().0.clone() ].to_vec(),
291 "-");
292
293 assert!(check(&cert, 4, (carol_primary(), &[alice_primary()])).is_ok());
294 }
295
296 Ok(())
297 }
298
299 #[test]
300 fn bind_encryption_and_signing() -> Result<()> {
301 for (keyrings, key_imports, handle) in [
302 (
303 // Keyrings
304 &[ alice() ][..],
305 // Key store imports.
306 &[][..],
307 // Handle
308 FileOrKeyHandle::from(bob()),
309 ),
310 (
311 // Keyrings
312 &[][..],
313 // Key store imports.
314 &[ alice() ],
315 // Handle
316 bob().into()
317 ),
318 (
319 // Keyrings
320 &[][..],
321 // Key store imports.
322 &[ alice(), bob() ],
323 // Handle
324 bob_primary().0.into()
325 ),
326 ] {
327 let sq = Sq::new();
328
329 for file in key_imports {
330 sq.key_import(file);
331 }
332
333 let cert = sq.key_subkey_bind(
334 &[],
335 keyrings.to_vec(),
336 handle,
337 [
338 alice_signing().0.clone(),
339 alice_encryption().0.clone(),
340 ].to_vec(),
341 "-");
342
343 assert!(
344 check(&cert, 3,
345 (bob_primary(),
346 &[alice_signing(), alice_encryption()]))
347 .is_ok());
348 }
349
350 Ok(())
351 }
352
353 #[test]
354 fn bind_twice() -> Result<()> {
355 for (keyrings, key_imports, handle) in [
356 (
357 // Keyrings
358 &[ alice() ][..],
359 // Key store imports.
360 &[][..],
361 // Handle
362 FileOrKeyHandle::from(bob()),
363 ),
364 (
365 // Keyrings
366 &[][..],
367 // Key store imports.
368 &[ alice() ],
369 // Handle
370 bob().into()
371 ),
372 (
373 // Keyrings
374 &[][..],
375 // Key store imports.
376 &[ alice(), bob() ],
377 // Handle
378 bob_primary().0.into()
379 ),
380 ] {
381 let sq = Sq::new();
382
383 for file in key_imports {
384 sq.key_import(file);
385 }
386
387 // Bind the same an encryption subkey twice.
388 let cert = sq.key_subkey_bind(
389 &[],
390 keyrings.to_vec(),
391 handle,
392 [
393 alice_encryption().0.clone(),
394 alice_encryption().0.clone(),
395 ].to_vec(),
396 "-");
397
398 assert!(
399 check(&cert, 2, (bob_primary(), &[alice_encryption()])).is_ok());
400 }
401
402 Ok(())
403 }
404
405 #[test]
406 fn bind_key_appears_twice() -> Result<()> {
407 let sq = Sq::new();
408
409 // Bind an encryption subkey that appears twice.
410 let cert = sq.key_subkey_bind(
411 &[],
412 [ alice(), alice(), ].to_vec(),
413 bob(),
414 [
415 alice_encryption().0.clone(),
416 ].to_vec(),
417 "-");
418
419 assert!(
420 check(&cert, 2, (bob_primary(), &[alice_encryption()])).is_ok());
421
422 Ok(())
423 }
424
425 #[test]
426 fn bind_own_encryption() -> Result<()> {
427 for (keyrings, key_imports, handle) in [
428 (
429 // Keyrings
430 &[ alice() ][..],
431 // Key store imports.
432 &[][..],
433 // Handle
434 FileOrKeyHandle::from(alice()),
435 ),
436 (
437 // Keyrings
438 &[][..],
439 // Key store imports.
440 &[ alice() ],
441 // Handle
442 alice().into()
443 ),
444 (
445 // Keyrings
446 &[][..],
447 // Key store imports.
448 &[ alice(), bob() ],
449 // Handle
450 alice_primary().0.into()
451 ),
452 ] {
453 let sq = Sq::new();
454
455 for file in key_imports {
456 sq.key_import(file);
457 }
458
459 // Bind its own encryption subkey. This should be a noop.
460 let cert = sq.key_subkey_bind(
461 &[],
462 keyrings.to_vec(),
463 handle,
464 [
465 alice_encryption().0.clone(),
466 ].to_vec(),
467 "-");
468
469 assert!(
470 check(&cert, 3, (alice_primary(), &[alice_encryption()])).is_ok());
471 }
472
473 Ok(())
474 }
475
476 #[test]
477 fn bind_own_primary() -> Result<()> {
478 for (keyrings, key_imports, handle) in [
479 (
480 // Keyrings
481 &[ bob() ][..],
482 // Key store imports.
483 &[][..],
484 // Handle
485 FileOrKeyHandle::from(bob()),
486 ),
487 (
488 // Keyrings
489 &[][..],
490 // Key store imports.
491 &[ bob() ],
492 // Handle
493 bob().into()
494 ),
495 (
496 // Keyrings
497 &[][..],
498 // Key store imports.
499 &[ bob() ],
500 // Handle
501 bob_primary().0.into()
502 ),
503 ] {
504 let sq = Sq::new();
505
506 for file in key_imports {
507 sq.key_import(file);
508 }
509
510 // Bind own primary key.
511 let cert = sq.key_subkey_bind(
512 &[],
513 keyrings.to_vec(),
514 handle,
515 [
516 bob_primary().0.clone(),
517 ].to_vec(),
518 "-");
519
520 assert!(
521 check(&cert, 2, (bob_primary(), &[bob_primary()])).is_ok());
522 }
523
524 Ok(())
525 }
526
527 #[test]
528 fn bind_missing() -> Result<()> {
529 for (keyrings, key_imports, handle) in [
530 (
531 // Keyrings
532 &[ alice(), bob() ][..],
533 // Key store imports.
534 &[][..],
535 // Handle
536 FileOrKeyHandle::from(bob()),
537 ),
538 (
539 // Keyrings
540 &[][..],
541 // Key store imports.
542 &[][..],
543 // Handle
544 FileOrKeyHandle::from(bob()),
545 ),
546 (
547 // Keyrings
548 &[][..],
549 // Key store imports.
550 &[ bob() ],
551 // Handle
552 bob().into()
553 ),
554 (
555 // Keyrings
556 &[][..],
557 // Key store imports.
558 &[ bob() ],
559 // Handle
560 bob_primary().0.into()
561 ),
562 ] {
563 let sq = Sq::new();
564
565 for file in key_imports {
566 sq.key_import(file);
567 }
568
569 // Bind a key that is not present.
570 let r = sq.key_subkey_bind_maybe(
571 &[],
572 keyrings.to_vec(),
573 handle,
574 [
575 "1234 5678 90AB CDEF 1234 5678 90AB CDEF"
576 .parse::<KeyHandle>()
577 .expect("valid fingerprint")
578 ].to_vec(),
579 "-");
580
581 assert!(r.is_err());
582 }
583
584 Ok(())
585 }
586
587 #[test]
588 fn bind_from_multiple() -> Result<()> {
589 for (keyrings, key_imports, handle) in [
590 (
591 // Keyrings
592 &[ alice(), carol() ][..],
593 // Key store imports.
594 &[][..],
595 // Handle
596 FileOrKeyHandle::from(bob()),
597 ),
598 (
599 // Keyrings
600 &[][..],
601 // Key store imports.
602 &[ alice(), carol() ],
603 // Handle
604 bob().into()
605 ),
606 (
607 // Keyrings
608 &[][..],
609 // Key store imports.
610 &[ alice(), bob(), carol(), ],
611 // Handle
612 bob_primary().0.into()
613 ),
614 ] {
615 let sq = Sq::new();
616
617 for file in key_imports {
618 sq.key_import(file);
619 }
620
621 // Bind own primary key.
622 let cert = sq.key_subkey_bind(
623 &[],
624 keyrings.to_vec(),
625 handle,
626 [
627 alice_signing().0.clone(),
628 alice_encryption().0.clone(),
629 carol_signing().0.clone(),
630 carol_encryption().0.clone(),
631 ].to_vec(),
632 "-");
633
634 assert!(
635 check(&cert, 5,
636 (bob_primary(),
637 &[
638 alice_signing(), alice_encryption(),
639 carol_signing(), carol_encryption()
640 ]))
641 .is_ok());
642 }
643
644 Ok(())
645 }
646
647 // A bare certificate is a certificate that consists of just a primary
648 // key (no subkeys, no user IDs, and no signatures). Make sure we are
649 // able to bind one.
650 #[test]
651 fn bind_bare() -> Result<()> {
652 let sq = Sq::new();
653
654 sq.key_import(alice());
655
656 let alice2_pgp = sq.scratch_file("alice2.pgp");
657
658 let to_bind = bare_signing().0;
659
660 let bare_file = bare();
661 let bare = Cert::from_file(&bare_file).expect("can read file");
662
663 // First, a bare certificate doesn't have any key flags set. Make
664 // sure `sq key bind` complains, if we don't specify any (e.g.,
665 // `--can-encrypt`).
666 let r = sq.key_subkey_bind_maybe(
667 &[],
668 vec![ bare_file.clone() ],
669 alice_primary().0,
670 vec![ to_bind.clone() ],
671 &alice2_pgp);
672 if r.is_ok() {
673 panic!("sq key bind succeeded, but should have complained about \
674 missing key flags");
675 }
676
677 let cert = sq.key_subkey_bind(
678 &["--can-encrypt", "universal"],
679 vec![ bare_file.clone() ],
680 alice_primary().0,
681 vec![ to_bind.clone() ],
682 &alice2_pgp);
683
684 let mut found = false;
685 for k in cert.keys() {
686 let was_bound = k.key().mpis() == bare.primary_key().key().mpis();
687
688 eprintln!("{}{}", k.key().fingerprint(),
689 if was_bound {
690 " (bound)"
691 } else {
692 ""
693 });
694 if was_bound {
695 found = true;
696 }
697 }
698 if ! found {
699 panic!("{} was not bound", to_bind);
700 }
701
702 Ok(())
703 }
704
705 // Check that we can set the key creation time.
706 #[test]
707 fn key_creation_time() -> Result<()> {
708 let sq = Sq::new();
709
710 sq.key_import(alice());
711
712 let alice2_pgp = sq.scratch_file("alice2.pgp");
713
714 let to_bind = bare_signing().0;
715
716 // $ date --iso-8601=seconds --utc --date='@1577483647'
717 // 2019-12-27T21:54:07+00:00
718 let time = 1577483647;
719 let time = std::time::UNIX_EPOCH + std::time::Duration::new(time, 0);
720 let time_str = "2019-12-27T21:54:07+00:00";
721
722 let bare_file = bare();
723 let bare = Cert::from_file(&bare_file).expect("can read file");
724
725 let cert = sq.key_subkey_bind(
726 &["--can-encrypt", "universal", "--creation-time", time_str ],
727 vec![ bare_file ],
728 alice_primary().0,
729 vec![ to_bind.clone() ],
730 &alice2_pgp);
731
732 let mut found = false;
733 for k in cert.keys() {
734 let was_bound = k.key().mpis() == bare.primary_key().key().mpis();
735
736 eprintln!("{}: {:?}{}",
737 k.key().fingerprint(),
738 k.key().creation_time(),
739 if was_bound {
740 " (bound)"
741 } else {
742 ""
743 });
744 if was_bound {
745 assert_eq!(k.key().creation_time(), time);
746 found = true;
747 }
748 }
749 if ! found {
750 panic!("{} was not bound", to_bind);
751 }
752
753 Ok(())
754 }