Debian Codegraph @

tests/integration/sq_key_rotate.rs

raw
1 use sequoia_openpgp as openpgp;
2 use openpgp::types::ReasonForRevocation;
3 use openpgp::types::KeyFlags;
4
5 use crate::integration::common::STANDARD_POLICY;
6 use crate::integration::common::Sq;
7 use crate::integration::common::check_certifications;
8
9 #[test]
10 fn rotate() {
11 let s = &KeyFlags::signing();
12 let a = &KeyFlags::authentication();
13 let es = &KeyFlags::storage_encryption();
14 let ec = &KeyFlags::transport_encryption();
15
16 for kf in [
17 &(&(s | a) | &(es | ec)),
18 s,
19 &(s | es),
20 &(s | ec),
21 a
22 ] {
23 let mut sq = Sq::new();
24
25 let mut args = Vec::new();
26 if kf.for_signing() {
27 args.push("--can-sign");
28 } else {
29 args.push("--cannot-sign");
30 }
31 if kf.for_authentication() {
32 args.push("--can-authenticate");
33 } else {
34 args.push("--cannot-authenticate");
35 }
36 match (kf.for_storage_encryption(), kf.for_transport_encryption()) {
37 (true, true) => args.push("--can-encrypt=universal"),
38 (true, false) => args.push("--can-encrypt=storage"),
39 (false, true) => args.push("--can-encrypt=transport"),
40 (false, false) => args.push("--cannot-encrypt"),
41 }
42
43 // The original certificate.
44 let alice_email = "[email protected]";
45 let alice_userid = &format!("Alice <{}>", alice_email);
46 let alice_email2 = "[email protected]";
47 let alice_userid2 = &format!("Alice <{}>", alice_email2);
48 let (alice, alice_pgp, _alice_rev)
49 = sq.key_generate(&args, &[ alice_userid, alice_userid2 ]);
50 sq.key_import(&alice_pgp);
51
52 // Link a user ID.
53 sq.pki_link_add(&[], alice.key_handle(), &[ alice_userid ]);
54 assert!(sq.pki_authenticate(
55 &[], &alice.key_handle().to_string(), alice_userid).is_ok());
56 assert!(sq.pki_authenticate(
57 &[], &alice.key_handle().to_string(), alice_userid2).is_err());
58
59 let bob_email = "[email protected]";
60 let bob_userid = &format!("Bob <{}>", bob_email)[..];
61 let bob_email2 = "[email protected]";
62 let bob_userid2 = &format!("Bob <{}>", bob_email2)[..];
63 let (bob, bob_pgp, _bob_rev)
64 = sq.key_generate(&[], &[ bob_userid, bob_userid2 ]);
65 sq.key_import(&bob_pgp);
66
67 // Alice certifies Bob.
68 sq.tick(1);
69 sq.pki_vouch_add(
70 &["--amount", "1"],
71 &alice.key_handle(), &bob.key_handle(), &[bob_userid],
72 None);
73
74 // And do the rotation...
75 sq.tick(1);
76 sq.key_rotate(&[], alice.key_handle().into(), None);
77
78 // Extract the updated certificates.
79 let certs = sq.cert_export_all();
80 assert_eq!(certs.len(), 3);
81 let mut alice2 = None;
82 let mut bob2 = None;
83 let mut alice_new = None;
84 for cert in certs.iter() {
85 if cert.fingerprint() == alice.fingerprint() {
86 alice2 = Some(cert);
87 } else if cert.fingerprint() == bob.fingerprint() {
88 bob2 = Some(cert);
89 } else {
90 alice_new = Some(cert);
91 }
92 }
93
94 drop(alice);
95 drop(bob);
96
97 let alice2 = alice2.unwrap();
98 let alice2_vc = alice2.with_policy(STANDARD_POLICY, sq.now()).unwrap();
99 let bob2 = bob2.unwrap();
100 let alice_new = alice_new.unwrap();
101 let alice_new_vc = alice_new.with_policy(STANDARD_POLICY, sq.now()).unwrap();
102
103 // Check the key structure.
104 let alice2_kf = alice2_vc.keys().fold(KeyFlags::empty(), |kf, ka| {
105 if let Some(subkeys_kf) = ka.key_flags() {
106 &kf | &subkeys_kf
107 } else {
108 kf
109 }
110 });
111 assert!(alice2_kf != KeyFlags::empty());
112 let alice_new_kf = alice_new_vc.keys().fold(KeyFlags::empty(), |kf, ka| {
113 if let Some(subkeys_kf) = ka.key_flags() {
114 &kf | &subkeys_kf
115 } else {
116 kf
117 }
118 });
119 assert_eq!(alice2_kf, alice_new_kf);
120
121 // Check the user IDs.
122 let mut alice2_userids
123 = alice2_vc.userids().map(|ua| ua.userid()).collect::<Vec<_>>();
124 assert!(alice2_userids.len() > 0);
125 alice2_userids.sort();
126
127 let mut alice_new_userids
128 = alice_new_vc.userids().map(|ua| ua.userid()).collect::<Vec<_>>();
129 alice_new_userids.sort();
130
131 assert_eq!(alice2_userids, alice_new_userids);
132
133 // Check the certifications.
134 check_certifications(
135 &certs,
136 &[
137 (alice2.fingerprint(), bob2.fingerprint(), bob_userid, 1, 1),
138
139 // Cross sigs.
140 (alice2.fingerprint(), alice_new.fingerprint(), alice_userid, 120, 1),
141 (alice2.fingerprint(), alice_new.fingerprint(), alice_userid2, 120, 1),
142 (alice_new.fingerprint(), alice2.fingerprint(), alice_userid, 120, 1),
143 (alice_new.fingerprint(), alice2.fingerprint(), alice_userid2, 120, 1),
144
145 // The replayed signature.
146 (alice_new.fingerprint(), bob2.fingerprint(), bob_userid, 1, 1),
147 ][..]);
148
149 // Make sure the links were copied.
150 assert!(sq.pki_authenticate(
151 &[], &alice2.key_handle().to_string(), alice_userid).is_ok());
152 assert!(sq.pki_authenticate(
153 &[], &alice2.key_handle().to_string(), alice_userid2).is_err());
154
155 assert!(sq.pki_authenticate(
156 &[], &alice_new.key_handle().to_string(), alice_userid).is_ok());
157 assert!(sq.pki_authenticate(
158 &[], &alice_new.key_handle().to_string(), alice_userid2).is_err());
159
160 // Alice should have exactly one revocation certificate. It
161 // should say the certificate is retired, and it should contain
162 // the fingerprint of the new certificate.
163 let revs = alice2_vc.primary_key().self_revocations().collect::<Vec<_>>();
164 assert_eq!(revs.len(), 1);
165 let rev = revs.into_iter().next().unwrap();
166 let (reason, message)
167 = rev.reason_for_revocation().expect("have revocation reason");
168 assert_eq!(reason, ReasonForRevocation::KeyRetired);
169 assert!(String::from_utf8_lossy(message)
170 .contains(&alice_new.fingerprint().to_string()));
171 }
172 }