Debian Codegraph @

tests/integration/sq_cert_lint.rs

raw
1 use std::path;
2
3 use predicates::prelude::*;
4
5 use sequoia_openpgp as openpgp;
6
7 use openpgp::Cert;
8 use openpgp::Packet;
9 use openpgp::parse::Parse;
10
11 use super::common::Sq;
12
13 fn dir() -> path::PathBuf {
14 path::Path::new("tests").join("data").join("cert-lint")
15 }
16
17 const FROZEN_TIME: &str = "20220101";
18
19 // passwords: one '-p' option per element.
20 // required_fixes: the number of fixes (= new top-level signatures) needed.
21 // expected_fixes: the number of them that we can create.
22 fn t(base: &str, prv: Option<&str>, passwords: &[&str],
23 required_fixes: usize, expected_fixes: usize)
24 {
25 assert!(required_fixes >= expected_fixes);
26
27 let sq = Sq::new();
28 let dir = dir();
29 let mut suffixes = vec![ "pub" ];
30 if let Some(prv) = prv {
31 suffixes.push(prv);
32 }
33
34 for suffix in suffixes.iter() {
35 for keystore in [false, true] {
36 // Lint it.
37 let filename = &format!("{}-{}.pgp", base, suffix);
38 eprintln!("Linting {}", filename);
39
40 let cert = Cert::from_file(dir.join(filename))
41 .expect(&format!("Can parse {}", filename));
42
43 if keystore {
44 // When using the keystore, we need to import the key.
45 if suffix == &"pub" {
46 eprintln!("Import certificate from {}", filename);
47
48 let mut cmd = sq.command();
49 cmd
50 .current_dir(&dir)
51 .args([
52 "cert",
53 "import",
54 &filename,
55 ]);
56 let output = cmd.output().expect("can sq cert import");
57 if !output.status.success() {
58 panic!(
59 "sq exited with non-zero status code: {}",
60 String::from_utf8_lossy(&output.stderr)
61 );
62 }
63 } else {
64 eprintln!("Import key from {}", filename);
65
66 let mut cmd = sq.command();
67 cmd
68 .current_dir(&dir)
69 .args([
70 "key",
71 "import",
72 &filename,
73 ]);
74 let output = cmd.output().expect("can sq key import");
75 if !output.status.success() {
76 panic!(
77 "sq exited with non-zero status code: {}",
78 String::from_utf8_lossy(&output.stderr)
79 );
80 }
81 }
82 }
83
84 let mut cmd = sq.command();
85 cmd
86 .current_dir(&dir)
87 .arg("cert").arg("lint")
88 .arg("--time").arg(FROZEN_TIME);
89 if keystore {
90 cmd.arg("--cert").arg(&cert.fingerprint().to_string());
91 } else {
92 cmd.arg("--cert-file").arg(filename);
93 }
94
95 cmd
96 .assert()
97 .code(if required_fixes > 0 { 1 } else { 0 });
98
99
100 // Fix it.
101 let filename = &format!("{}-{}.pgp", base, suffix);
102 eprint!("Fixing {}", filename);
103 if passwords.len() > 0 {
104 eprint!(" (passwords: ");
105 for (i, p) in passwords.iter().enumerate() {
106 if i > 0 {
107 eprint!(", ");
108 }
109 eprint!("{:?}", p)
110 }
111 eprint!(")");
112 }
113 eprintln!(".");
114
115 let expected_fixes = if suffix == &"pub" {
116 // We only have public key material: we won't be able
117 // to fix anything.
118 0
119 } else {
120 expected_fixes
121 };
122 eprintln!("{} expected fixes, {} required fixes",
123 expected_fixes, required_fixes);
124
125 let mut cmd = sq.command();
126 let mut cmd = cmd.current_dir(&dir)
127 .args(&[
128 "cert", "lint",
129 "--time", FROZEN_TIME,
130 "--fix",
131 ]);
132 if keystore {
133 cmd.args([
134 "--cert", &cert.fingerprint().to_string(),
135 ]);
136 } else {
137 cmd.args([
138 "--cert-file", &format!("{}-{}.pgp", base, suffix),
139 ]);
140 }
141 for p in passwords.iter() {
142 cmd = cmd.arg("-p").arg(p)
143 }
144 cmd.assert()
145 // If not everything can be fixed, then --fix's exit code is 1.
146 .code(if expected_fixes == required_fixes { 0 } else { 1 })
147 .stdout(predicate::function(|output: &[u8]| -> bool {
148 if expected_fixes == 0 {
149 // If there are no fixes, nothing is printed.
150 output == b""
151 } else {
152 // Pass the result through the linter.
153 let mut cmd = sq.command();
154 cmd
155 .current_dir(&dir)
156 .arg("cert").arg("lint")
157 .arg("--time").arg(FROZEN_TIME);
158 if keystore {
159 cmd.arg("--cert")
160 .arg(&cert.fingerprint().to_string());
161 } else {
162 cmd.arg("--cert-file").arg("-")
163 .write_stdin(output);
164 }
165
166 cmd.assert()
167 .code(
168 if expected_fixes == required_fixes {
169 // Everything should have been fixed.
170 0
171 } else {
172 // There are still issues.
173 1
174 });
175
176 // Check that the number of new signatures equals
177 // the number of expected new signatures.
178 let orig_sigs: isize = cert
179 .clone()
180 .into_packets()
181 .map(|p| {
182 if let Packet::Signature(_) = p {
183 1
184 } else {
185 0
186 }
187 })
188 .sum();
189
190 let updated_cert = if keystore {
191 let mut cmd = sq.command();
192 let cmd = cmd.current_dir(&dir)
193 .args(&[
194 "cert", "export",
195 "--cert", &cert.fingerprint().to_string(),
196 ]);
197 let output = cmd.output()
198 .expect(&format!("Can run sq cert export"));
199 if !output.status.success() {
200 panic!(
201 "sq exited with non-zero status code: {}",
202 String::from_utf8_lossy(&output.stderr)
203 );
204 }
205 Cert::from_bytes(&output.stdout)
206 } else {
207 // When not using the keystore, `sq
208 // cert lint --fix` emits the fixed
209 // certificate on stdout.
210 Cert::from_bytes(output)
211 };
212
213 let fixed_sigs: isize = updated_cert
214 .map(|cert| {
215 cert.into_packets()
216 .map(|p| {
217 match p {
218 Packet::Signature(_) => 1,
219 Packet::SecretKey(_)
220 | Packet::SecretSubkey(_) =>
221 panic!("Secret key material \
222 should not be exported!"),
223 _ => 0,
224 }
225 })
226 .sum()
227 })
228 .map_err(|err| {
229 eprintln!("Parsing fixed certificate: {}", err);
230 0
231 })
232 .unwrap();
233
234 let fixes = fixed_sigs - orig_sigs;
235 if expected_fixes as isize != fixes {
236 eprintln!("Expected {} fixes, \
237 found {} additional signatures",
238 expected_fixes, fixes);
239 false
240 } else {
241 true
242 }
243 }
244 }));
245 }
246 }
247 }
248
249 #[test]
250 fn known_good() {
251 t("gnupg-rsa-normal", Some("priv"), &[], 0, 0);
252 t("gnupg-ecc-normal", Some("priv"), &[], 0, 0);
253 }
254
255 #[test]
256 fn userid_certification() {
257 // User ID: SHA256
258 // User ID: SHA1
259 // Enc Subkey: SHA256
260 t("sha1-userid", Some("priv"), &[], 1, 1);
261 }
262
263 #[test]
264 fn revoked_userid_certification() {
265 // A revoked User ID shouldn't be updated.
266
267 // User ID: SHA256
268 // User ID: SHA1 (revoked)
269 // Enc Subkey: SHA256
270 t("sha1-userid-revoked", Some("priv"), &[], 0, 0);
271 }
272
273 #[test]
274 fn signing_subkey_binding_signature() {
275 // User ID: SHA256
276 // Enc Subkey: SHA256
277 // Sig Subkey: SHA1
278 t("sha1-signing-subkey", Some("priv"), &[], 1, 1);
279 }
280
281 #[test]
282 fn encryption_subkey_binding_signature() {
283 // User ID: SHA256
284 // Enc Subkey: SHA256
285 // Enc Subkey: SHA1
286 t("sha1-encryption-subkey", Some("priv"), &[], 1, 1);
287 }
288
289 #[test]
290 fn subkey_backsig() {
291 // User ID: SHA256
292 // Enc Subkey: SHA256
293 // Sig Subkey: SHA256, backsig: SHA1
294 t("sha1-backsig-signing-subkey", Some("priv"), &[], 1, 1);
295 }
296
297 #[test]
298 fn all_bad() {
299 // User ID: SHA1
300 // Enc Subkey: SHA1
301 t("only-sha1", Some("priv"), &[], 2, 2);
302
303 // We don't fix MD5 signatures.
304 //
305 // User ID: MD5
306 // Enc Subkey: MD5
307 t("only-md5", Some("priv"), &[], 2, 0);
308 }
309
310 /// XXX: Disabled because there is no non-interactive way to feed
311 /// passwords to it.
312 #[allow(dead_code)]
313 fn passwords() {
314 // User ID: SHA1
315 // Enc Subkey: SHA1
316
317 // Wrong password.
318 t("all-sha1-password-Foobar", Some("priv"), &["foobar"], 2, 0);
319 // Right password.
320 t("all-sha1-password-Foobar", Some("priv"), &["Foobar"], 2, 2);
321
322 // Try multiple passwords.
323 t("all-sha1-password-Foobar", Some("priv"), &["Foobar", "bar"], 2, 2);
324 t("all-sha1-password-Foobar", Some("priv"), &["bar", "Foobar"], 2, 2);
325 }
326
327 /// XXX: Disabled because there is no non-interactive way to feed
328 /// passwords to it.
329 #[allow(dead_code)]
330 fn multiple_passwords() {
331 // The primary is encrypted with foo and the signing subkey
332 // with bar. We need to provide both, because the signing
333 // subkey needs its backsig updated.
334
335 // User ID: SHA256
336 // Enc Subkey: SHA256
337 // Enc Subkey: SHA1
338 // Sig Subkey: SHA1
339
340 // We only have the password for the signing subkey: we can't
341 // update anything.
342 t("multiple-passwords", Some("priv"), &["bar", "Foobar"], 2, 0);
343 // We only have the password for the primary key: we can't
344 // update the backsig.
345 t("multiple-passwords", Some("priv"), &["foo", "Foobar"], 2, 1);
346 // We have all passwords: we can fix everything.
347 t("multiple-passwords", Some("priv"), &["bar", "Foobar", "foo"], 2, 2);
348 }
349
350 #[test]
351 fn offline_subkeys() {
352 // The User ID, the encryption subkey, and the signing subkey
353 // all need new signatures. With just the primary key, we are
354 // able to create two of the three required signatures.
355
356 // User ID: SHA1
357 // Enc Subkey: SHA1
358 // Sig Subkey: SHA1
359
360 // We can't update the backsig.
361 t("sha1-offline-subkeys", Some("offline"), &[], 3, 2);
362 // We can fix everything.
363 t("sha1-offline-subkeys", Some("priv"), &[], 3, 3);
364 }
365
366 #[test]
367 fn sha1_authentication_subkey() {
368 // User ID: SHA1
369 // Enc Subkey: SHA1
370 // Auth Subkey: SHA1
371 t("sha1-authentication-subkey", Some("priv"), &[], 3, 3);
372 }
373
374 #[test]
375 fn authentication_subkey() {
376 // An authentication subkey doesn't require a backsig. Make
377 // sure we don't flag a missing backsig as an error.
378
379 // User ID: SHA512
380 // Enc Subkey: SHA512
381 // Auth Subkey: SHA512
382 t("authentication-subkey", Some("priv"), &[], 0, 0);
383 }
384
385 #[test]
386 fn sha1_userid_sha256_subkeys() {
387 // The User ID is protected with a SHA-1 signature, but two
388 // subkeys are protected with SHA256. Make sure the subkeys
389 // don't get new binding signatures.
390
391 // User ID: SHA1
392 // Enc Subkey: SHA1
393 // Sig Subkey: SHA256
394 // Enc Subkey: SHA256
395 t("sha1-userid-sha256-subkeys", Some("priv"), &[], 2, 2);
396 }
397
398 #[test]
399 fn no_backsig() {
400 // If a key doesn't have a backsig and needs one, it won't be
401 // detected as an issue, because it is not valid under
402 // SHA1+SP. That's okay.
403
404 // User ID: SHA512
405 // Sig Subkey: SHA512, no backsig.
406 t("no-backsig", Some("priv"), &[], 0, 0);
407 }
408
409 #[test]
410 fn sha512_self_sig_sha1_revocation() {
411 // Under the standard policy, SHA1 revocations are considered
412 // bad. We assume that SP+SHA-1 is strictly more liberal than
413 // SP (i.e., it accepts at least everything that SP accepts).
414
415 // User ID: SHA512, SHA-1 revocation.
416 t("sha512-self-sig-sha1-revocation", None, &[], 0, 0);
417 }
418
419 #[test]
420 fn revoked_certificate() {
421 // The certificate is only valid under SP+SHA1, and the
422 // revocation certificate uses SHA1. There is no need to
423 // upgrade the certificate or the revocation certificate.
424
425 // User ID: SHA1
426 // Enc Subkey: SHA1
427 // Revocation: SHA1
428 t("sha1-cert-sha1-revocation", Some("priv"), &[], 0, 0);
429
430 // The certificate is only valid under SP+SHA1, and the
431 // revocation certificate uses SHA256. There is no need to
432 // upgrade the certificate or the revocation certificate.
433
434 // User ID: SHA1
435 // Enc Subkey: SHA1
436 // Revocation: SHA256
437 t("sha1-cert-sha256-revocation", Some("priv"), &[], 0, 0);
438
439 // The certificate is valid under SP (the signatures use
440 // SHA512), but there are two revocation certificates that use
441 // SHA1. Make sure we upgrade them.
442
443 // User ID: SHA512
444 // Enc Subkey: SHA512
445 // Revocation: SHA1
446 // Revocation: SHA1
447 t("sha512-cert-sha1-revocation", Some("priv"), &[], 2, 2);
448
449 // The certificate is valid under SP (the signatures use
450 // SHA256), and it is revoked using a SHA256 revocation
451 // certificate, which is also valid under SP. It also has a
452 // SHA-1 protected signing subkey. Because the certificate is
453 // revoked and the revocation certificate uses SHA256, we
454 // don't need to fix the SHA-1 signature. Make sure we don't.
455
456 // User ID: SHA256
457 // Enc Subkey: SHA256
458 // Sig Subkey: SHA1
459 // Revocation: SHA256
460 t("sha256-cert-sha256-revocation", Some("priv"), &[], 0, 0);
461 }
462
463 #[test]
464 fn expired_certificates() {
465 // User ID: SHA256 (expired)
466 // Enc Subkey: SHA256
467 t("sha256-expired", Some("priv"), &[], 0, 0);
468
469 // User ID: SHA1 (expired)
470 // Enc Subkey: SHA1
471 t("sha1-expired", Some("priv"), &[], 0, 0);
472
473 // User ID: SHA256 (old, expired), SHA1 (new, live)
474 // Enc Subkey: SHA256
475 t("sha256-expired-sha1-live", Some("priv"), &[], 1, 1);
476 }
477
478 #[test]
479 fn signature() {
480 let sq = Sq::new();
481 sq.command()
482 .current_dir(&dir())
483 .args(&[
484 "cert", "lint",
485 "--time", FROZEN_TIME,
486 "--cert-file", "msg.sig",
487 ])
488 .assert()
489 // If there are issues, the command fails.
490 .failure();
491 }