Debian Codegraph @

tests/integration/sq_key_subkey_expire.rs

raw
1 use std::time::Duration;
2
3 use sequoia_openpgp as openpgp;
4 use openpgp::Cert;
5 use openpgp::cert::amalgamation::ValidAmalgamation;
6 use openpgp::KeyHandle;
7 use openpgp::Result;
8 use openpgp::parse::Parse;
9 use openpgp::types::RevocationStatus;
10
11 use super::common::STANDARD_POLICY;
12 use super::common::Sq;
13 use super::common::power_set;
14 use super::common::time_as_string;
15
16 #[test]
17 fn sq_key_subkey_expire() -> Result<()> {
18 let mut sq = Sq::new();
19
20 let (cert, cert_path, _rev_path)
21 = sq.key_generate(&[], &["alice <[email protected]>"]);
22 let fpr = cert.fingerprint().to_string();
23
24 let updated_path = sq.scratch_file("updated.pgp");
25 let updated2_path = sq.scratch_file("updated2.pgp");
26
27 let keys = cert.keys().map(|k| k.key().fingerprint()).collect::<Vec<_>>();
28
29 // Two days go by.
30 sq.tick(2 * 24 * 60 * 60);
31
32 for expiring in power_set(&keys) {
33 for keystore in [false, true] {
34 let cert_expiring = expiring.contains(&cert.fingerprint());
35
36 for (i, fpr) in keys.iter().enumerate() {
37 eprintln!(" {}. {}: {}expiring",
38 i, fpr,
39 if expiring.contains(&fpr) {
40 ""
41 } else {
42 "NOT "
43 });
44 }
45
46 if keystore {
47 sq.key_import(&cert_path);
48 }
49
50 // Change the key to expire in one day.
51 let mut cmd = sq.command();
52 cmd.args([
53 "key", "subkey", "expire", "--expiration", "1d",
54 ]);
55 if keystore {
56 cmd.args(["--cert", &fpr ]);
57 } else {
58 cmd
59 .arg("--overwrite")
60 .arg("--cert-file").arg(&cert_path)
61 .arg("--output").arg(&updated_path);
62 }
63 for k in expiring.iter() {
64 cmd.args(["--key", &k.to_string()]);
65 }
66 sq.run(cmd, true);
67
68 eprintln!("Updated keys at {} to expire in one day:\n{}",
69 sq.now_as_string(),
70 sq.inspect(&updated_path));
71
72 let updated = if keystore {
73 eprintln!("Updated certificate to expire in one day:\n{}",
74 sq.inspect(cert.key_handle()));
75
76 sq.cert_export(cert.key_handle())
77 } else {
78 eprintln!("Updated certificate to expire in one day:\n{}",
79 sq.inspect(&updated_path));
80
81 Cert::from_file(&updated_path).expect("valid cert")
82 };
83
84 // It should be alive now.
85 let vc = updated.with_policy(STANDARD_POLICY, sq.now()).expect("valid");
86 for k in vc.keys() {
87 assert!(k.alive().is_ok());
88 }
89
90 // It should be alive in 1 day minus 1 second.
91 let t = sq.now() + Duration::new(24 * 60 * 60 - 1, 0);
92 eprintln!("Checking expiration status at {}", time_as_string(t.into()));
93 let vc = updated.with_policy(STANDARD_POLICY, t).expect("valid");
94 for k in vc.keys() {
95 assert!(k.alive().is_ok());
96 }
97
98 // But in exactly one day, it should be expired.
99 let t = sq.now() + Duration::new(24 * 60 * 60, 0);
100 eprintln!("Checking expiration status at {}", time_as_string(t.into()));
101 let vc = updated.with_policy(STANDARD_POLICY, t).expect("valid");
102 for k in vc.keys() {
103 assert_eq!(
104 cert_expiring || expiring.contains(&k.key().fingerprint()),
105 k.alive().is_err(),
106 "{} is {}alive",
107 k.key().fingerprint(),
108 if k.alive().is_ok() { "" } else { "NOT "});
109 }
110
111 // 12 hours go by. Clear the expiration time.
112 sq.tick(12 * 60 * 60);
113
114 let mut cmd = sq.command();
115 cmd.args([ "key", "subkey", "expire", "--expiration", "never" ]);
116 if keystore {
117 cmd.args([ "--cert", &fpr ]);
118 } else {
119 cmd
120 .arg("--overwrite")
121 .arg("--cert-file").arg(&updated_path)
122 .arg("--output").arg(&updated2_path);
123 }
124 for k in expiring.iter() {
125 cmd.args(["--key", &k.to_string()]);
126 }
127 sq.run(cmd, true);
128
129 let updated = if keystore {
130 eprintln!("Updated certificate at {} to never expire:\n{}",
131 sq.now_as_string(),
132 sq.inspect(cert.key_handle()));
133
134 sq.cert_export(cert.key_handle())
135 } else {
136 eprintln!("Updated certificate at {} to never expire:\n{}",
137 sq.now_as_string(),
138 sq.inspect(&updated2_path));
139
140 Cert::from_file(&updated2_path).expect("valid cert")
141 };
142
143 // It should be alive now.
144 let vc = updated.with_policy(STANDARD_POLICY, sq.now())
145 .expect("valid");
146 for k in vc.keys() {
147 assert!(k.alive().is_ok());
148 }
149
150 // It should be alive in 1 day minus 1 second.
151 let t = sq.now() + Duration::new(24 * 60 * 60 - 1, 0);
152 eprintln!("Checking expiration status at {}", time_as_string(t.into()));
153 let vc = updated.with_policy(STANDARD_POLICY, t).expect("valid");
154 for k in vc.keys() {
155 eprintln!(" {} expires at {}",
156 k.key().fingerprint(),
157 if let Some(t) = k.key_expiration_time() {
158 time_as_string(t.into())
159 } else {
160 "never".to_string()
161 });
162 if let Err(err) = k.alive() {
163 panic!("{} should be alive, but it's not: {}",
164 k.key().fingerprint(), err);
165 }
166 }
167
168 // And in exactly one day...
169 let t = sq.now() + Duration::new(24 * 60 * 60, 0);
170 eprintln!("Checking expiration status at {}", time_as_string(t.into()));
171 let vc = updated.with_policy(STANDARD_POLICY, t).expect("valid");
172 for k in vc.keys() {
173 eprintln!(" {} expires at {}",
174 k.key().fingerprint(),
175 if let Some(t) = k.key_expiration_time() {
176 time_as_string(t.into())
177 } else {
178 "never".to_string()
179 });
180 if let Err(err) = k.alive() {
181 panic!("{} should be alive, but it's not: {}",
182 k.key().fingerprint(), err);
183 }
184 }
185 }
186 }
187
188 Ok(())
189 }
190
191 #[test]
192 fn unbound_subkey() {
193 // Make sure we can't extend the expiration time of an unbound
194 // subkey.
195
196 let sq = Sq::new();
197
198 let cert_path = sq.test_data()
199 .join("keys")
200 .join("unbound-subkey.pgp");
201
202 let cert = Cert::from_file(&cert_path).expect("can read");
203 let vc = cert.with_policy(STANDARD_POLICY, sq.now())
204 .expect("valid cert");
205
206 // One subkey should be considered invalid.
207 assert!(vc.keys().count() < cert.keys().count());
208
209 let unbound = "E992BF8BA7A27BB4FBB71D973857E47B14874045"
210 .parse::<KeyHandle>().expect("valid");
211
212 // Set it to expire in a day.
213 let updated_path = sq.scratch_file("updated");
214 let updated = sq.key_subkey_expire(cert_path,
215 &[ unbound ],
216 "1d",
217 None,
218 updated_path.as_path(),
219 false);
220 assert!(updated.is_err());
221 }
222
223 #[test]
224 fn soft_revoked_subkey() {
225 // Make sure we can't extend the expiration time of a soft revoked
226 // subkey.
227
228 let sq = Sq::new();
229
230 let cert_path = sq.test_data()
231 .join("keys")
232 .join("soft-revoked-subkey.pgp");
233
234 let cert = Cert::from_file(&cert_path).expect("can read");
235 let vc = cert.with_policy(STANDARD_POLICY, sq.now())
236 .expect("valid cert");
237
238 // Make sure the revoked key is there and is really revoked.
239 let mut revoked = None;
240 for k in vc.keys().subkeys() {
241 if let RevocationStatus::Revoked(_) = k.revocation_status() {
242 assert!(revoked.is_none(),
243 "Only expected a single revoked subkey");
244 revoked = Some(k.key().fingerprint());
245 }
246 }
247 let revoked = if let Some(revoked) = revoked {
248 revoked
249 } else {
250 panic!("Expected a revoked subkey, but didn't fine one");
251 };
252
253 // Set it to expire in a day.
254 let updated_path = sq.scratch_file("updated");
255 let updated = sq.key_subkey_expire(cert_path,
256 &[ revoked.clone().into() ],
257 "1d",
258 None,
259 updated_path.as_path(),
260 true)
261 .expect("sq key expire should succeed");
262
263 let vc = updated.with_policy(STANDARD_POLICY, sq.now())
264 .expect("valid cert");
265 let mut good = false;
266 for k in vc.keys() {
267 if k.key().fingerprint() == revoked {
268 if let RevocationStatus::Revoked(_) = k.revocation_status() {
269 panic!("{} shouldn't be revoked, but is.",
270 revoked);
271 }
272
273 let expiration = k.key_expiration_time();
274 assert_eq!(expiration,
275 Some(sq.now()
276 + std::time::Duration::new(24 * 60 * 60, 0)));
277 good = true;
278 break;
279 }
280 }
281 assert!(good);
282 }
283
284 #[test]
285 fn hard_revoked_subkey() {
286 // Make sure we can't extend the expiration time of a hard revoked
287 // subkey.
288
289 let sq = Sq::new();
290
291 let cert_path = sq.test_data()
292 .join("keys")
293 .join("hard-revoked-subkey.pgp");
294
295 let cert = Cert::from_file(&cert_path).expect("can read");
296 let vc = cert.with_policy(STANDARD_POLICY, sq.now())
297 .expect("valid cert");
298
299 // Make sure the revoked key is there and is really revoked.
300 let mut revoked = None;
301 for k in vc.keys().subkeys() {
302 if let RevocationStatus::Revoked(_) = k.revocation_status() {
303 assert!(revoked.is_none(),
304 "Only expected a single revoked subkey");
305 revoked = Some(k.key().fingerprint());
306 }
307 }
308 let revoked = if let Some(revoked) = revoked {
309 revoked
310 } else {
311 panic!("Expected a revoked subkey, but didn't fine one");
312 };
313
314 // Set it to expire in a day.
315 let updated_path = sq.scratch_file("updated");
316 let result = sq.key_subkey_expire(cert_path,
317 &[ revoked.clone().into() ],
318 "1d",
319 None,
320 updated_path.as_path(),
321 false);
322 if result.is_ok() {
323 panic!("Updated expiration of hard revoked subkey, but shouldn't have.");
324 }
325 }
326
327 #[test]
328 fn sha1_subkey() {
329 // Make sure we can't extend the expiration time of a subkey that
330 // is bound using SHA-1.
331
332 let sq = Sq::new();
333
334 let cert_path = sq.test_data()
335 .join("keys")
336 .join("sha1-subkey-priv.pgp");
337
338 let cert = Cert::from_file(&cert_path).expect("can read");
339 let vc = cert.with_policy(STANDARD_POLICY, sq.now())
340 .expect("valid cert");
341
342 // Make sure the subkey key is there and really uses SHA-1.
343 let valid_subkeys: Vec<_> = vc.keys().subkeys()
344 .map(|ka| ka.key().fingerprint())
345 .collect();
346 let all_subkeys: Vec<_> = cert.keys().subkeys()
347 .map(|ka| ka.key().fingerprint())
348 .collect();
349
350 assert_eq!(valid_subkeys.len(), 0);
351 assert_eq!(all_subkeys.len(), 1);
352
353 let subkey = all_subkeys[0].clone();
354
355 // Set it to expire in a day.
356 let updated_path = sq.scratch_file("updated");
357 let result = sq.key_subkey_expire(cert_path,
358 &[ subkey.clone().into() ],
359 "1d",
360 None,
361 updated_path.as_path(),
362 false);
363 assert!(result.is_err());
364 }