From 428f958795cf6b182cfc0167ff4aa89120404995 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Thu, 19 Nov 2026 18:13:21 -0700 Subject: [PATCH] x509-cert: use the `zlint` crate It was extracted into its own crate in RustCrypto/utils#1613 --- Cargo.lock | 13 +- Cargo.toml | 2 + x509-cert/Cargo.toml | 1 + x509-cert/test-support/Cargo.lock | 273 ---------------------------- x509-cert/test-support/Cargo.toml | 6 +- x509-cert/test-support/src/lib.rs | 1 - x509-cert/test-support/src/zlint.rs | 215 ---------------------- x509-cert/tests/builder.rs | 2 +- 8 files changed, 16 insertions(+), 497 deletions(-) delete mode 100644 x509-cert/test-support/Cargo.lock delete mode 100644 x509-cert/test-support/src/zlint.rs diff --git a/Cargo.lock b/Cargo.lock index 356c0fe6f..171b56cf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2007,14 +2007,13 @@ dependencies = [ "tls_codec 0.4.2", "tokio", "x509-cert-test-support", + "zlint", ] [[package]] name = "x509-cert-test-support" version = "0.1.0" dependencies = [ - "serde", - "serde_json", "tempfile", ] @@ -2087,6 +2086,16 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "zlint" +version = "0.0.0" +source = "git+https://github.com/RustCrypto/utils#167a48b5f0c05557e6f6cda837677c239759da31" +dependencies = [ + "serde", + "serde_json", + "tempfile", +] + [[package]] name = "zmij" version = "1.0.23" diff --git a/Cargo.toml b/Cargo.toml index 91df57938..c9a3d4a58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,6 +61,8 @@ x509-tsp = { path = "./x509-tsp" } x509-cert = { path = "./x509-cert" } x509-ocsp = { path = "./x509-ocsp" } +zlint = { git = "https://github.com/RustCrypto/utils" } + [workspace.lints.clippy] borrow_as_ptr = "warn" cast_lossless = "warn" diff --git a/x509-cert/Cargo.toml b/x509-cert/Cargo.toml index f8f0c708a..b32c3ec9d 100644 --- a/x509-cert/Cargo.toml +++ b/x509-cert/Cargo.toml @@ -38,6 +38,7 @@ sha2 = { version = "0.11", features = ["oid"] } tempfile = "3.5" tokio = { version = "1.45", features = ["macros", "rt"] } x509-cert-test-support = { path = "./test-support" } +zlint = "0" [features] default = ["pem", "std"] diff --git a/x509-cert/test-support/Cargo.lock b/x509-cert/test-support/Cargo.lock deleted file mode 100644 index 4e3a98571..000000000 --- a/x509-cert/test-support/Cargo.lock +++ /dev/null @@ -1,273 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 4 - -[[package]] -name = "bitflags" -version = "2.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "errno" -version = "0.3.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" -dependencies = [ - "libc", - "windows-sys", -] - -[[package]] -name = "fastrand" -version = "2.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" - -[[package]] -name = "getrandom" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" -dependencies = [ - "cfg-if", - "libc", - "wasi", - "windows-targets", -] - -[[package]] -name = "itoa" -version = "1.0.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" - -[[package]] -name = "libc" -version = "0.2.170" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" - -[[package]] -name = "linux-raw-sys" -version = "0.4.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" - -[[package]] -name = "memchr" -version = "2.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" - -[[package]] -name = "once_cell" -version = "1.20.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" - -[[package]] -name = "proc-macro2" -version = "1.0.93" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" -dependencies = [ - "unicode-ident", -] - -[[package]] -name = "quote" -version = "1.0.38" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" -dependencies = [ - "proc-macro2", -] - -[[package]] -name = "rustix" -version = "0.38.44" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" -dependencies = [ - "bitflags", - "errno", - "libc", - "linux-raw-sys", - "windows-sys", -] - -[[package]] -name = "ryu" -version = "1.0.19" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" - -[[package]] -name = "serde" -version = "1.0.218" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" -dependencies = [ - "serde_derive", -] - -[[package]] -name = "serde_derive" -version = "1.0.218" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "serde_json" -version = "1.0.139" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6" -dependencies = [ - "itoa", - "memchr", - "ryu", - "serde", -] - -[[package]] -name = "syn" -version = "2.0.98" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" -dependencies = [ - "proc-macro2", - "quote", - "unicode-ident", -] - -[[package]] -name = "tempfile" -version = "3.17.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230" -dependencies = [ - "cfg-if", - "fastrand", - "getrandom", - "once_cell", - "rustix", - "windows-sys", -] - -[[package]] -name = "unicode-ident" -version = "1.0.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" - -[[package]] -name = "wasi" -version = "0.13.3+wasi-0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" -dependencies = [ - "wit-bindgen-rt", -] - -[[package]] -name = "windows-sys" -version = "0.59.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-targets" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" -dependencies = [ - "windows_aarch64_gnullvm", - "windows_aarch64_msvc", - "windows_i686_gnu", - "windows_i686_gnullvm", - "windows_i686_msvc", - "windows_x86_64_gnu", - "windows_x86_64_gnullvm", - "windows_x86_64_msvc", -] - -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" - -[[package]] -name = "windows_aarch64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" - -[[package]] -name = "windows_i686_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" - -[[package]] -name = "windows_i686_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" - -[[package]] -name = "windows_i686_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" - -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" - -[[package]] -name = "windows_x86_64_msvc" -version = "0.52.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" - -[[package]] -name = "wit-bindgen-rt" -version = "0.33.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" -dependencies = [ - "bitflags", -] - -[[package]] -name = "x509-cert-test-support" -version = "0.1.0" -dependencies = [ - "serde", - "serde_json", - "tempfile", -] diff --git a/x509-cert/test-support/Cargo.toml b/x509-cert/test-support/Cargo.toml index 56ad6e734..7ff53aabd 100644 --- a/x509-cert/test-support/Cargo.toml +++ b/x509-cert/test-support/Cargo.toml @@ -1,8 +1,6 @@ [package] name = "x509-cert-test-support" -description = """ -Set of utils to run certificates through zlint or openssl. -""" +description = "OpenSSL helpers for the x509-cert test suite" version = "0.1.0" edition = "2021" authors = ["RustCrypto Developers"] @@ -13,6 +11,4 @@ categories = ["cryptography"] readme = "README.md" [dependencies] -serde = { version = "1.0.184", features = ["derive"] } -serde_json = "1" tempfile = "3" diff --git a/x509-cert/test-support/src/lib.rs b/x509-cert/test-support/src/lib.rs index da52a50e2..051897dbe 100644 --- a/x509-cert/test-support/src/lib.rs +++ b/x509-cert/test-support/src/lib.rs @@ -1,2 +1 @@ pub mod openssl; -pub mod zlint; diff --git a/x509-cert/test-support/src/zlint.rs b/x509-cert/test-support/src/zlint.rs deleted file mode 100644 index bcec2dfb6..000000000 --- a/x509-cert/test-support/src/zlint.rs +++ /dev/null @@ -1,215 +0,0 @@ -use serde::{ - de::{Error, MapAccess, Visitor}, - Deserialize, Deserializer, -}; -use std::{ - collections::HashMap, - fmt, - fs::File, - io::{self, Read, Write}, - process::{Command, Stdio}, -}; -use tempfile::tempdir; - -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum Status { - NotApplicable, - NotEffective, - Pass, - Notice, - Info, - Warn, - Error, - Fatal, -} - -impl Status { - pub fn is_successful(&self) -> bool { - !matches!(self, Status::Warn | Status::Error | Status::Fatal) - } -} - -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct LintStatus { - pub status: Status, - pub details: Option, -} - -impl LintStatus { - pub fn is_successful(&self) -> bool { - self.status.is_successful() - } -} - -impl<'de> Deserialize<'de> for LintStatus { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - struct StatusVisitor; - - impl<'de> Visitor<'de> for StatusVisitor { - type Value = LintStatus; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("an integer between -2^31 and 2^31") - } - - fn visit_map(self, mut access: M) -> Result - where - M: MapAccess<'de>, - { - let mut status_output = None; - let mut details = None; - - while let Some((key, value)) = access.next_entry::<&str, &str>()? { - if key == "result" { - status_output = Some(match value { - "NA" => Status::NotApplicable, - "NE" => Status::NotEffective, - "pass" => Status::Pass, - "notice" => Status::Notice, - "fatal" => Status::Fatal, - "error" => Status::Error, - "warn" => Status::Warn, - "info" => Status::Info, - other => { - return Err(M::Error::custom( - format!("unsupported value: {other}",), - )) - } - }); - } - if key == "details" { - details = Some(value.to_string()); - } - } - - if let Some(status) = status_output { - Ok(LintStatus { status, details }) - } else { - Err(M::Error::custom("no 'result' field found")) - } - } - } - - deserializer.deserialize_map(StatusVisitor) - } -} - -#[derive(Debug, Deserialize)] -pub struct LintResult(pub HashMap); - -impl LintResult { - pub fn check_lints(&self, ignored: &[&str]) -> bool { - let mut failed = HashMap::::new(); - - for (key, value) in &self.0 { - if !value.is_successful() && !ignored.contains(&key.as_str()) { - failed.insert(String::from(key), value.clone()); - } - } - - eprintln!("failed lints: {failed:?}"); - - failed.is_empty() - } -} - -const ZLINT_CONFIG: &str = " -[AppleRootStorePolicyConfig] - -[CABFBaselineRequirementsConfig] - -[CABFEVGuidelinesConfig] - -[CommunityConfig] - -[MozillaRootStorePolicyConfig] - -[RFC5280Config] - -[RFC5480Config] - -[RFC5891Config] - -[e_rsa_fermat_factorization] -Rounds = 100 -"; - -pub fn check_certificate(pem: &[u8], ignored: &[&str]) { - let tmp_dir = tempdir().expect("create tempdir"); - let config_path = tmp_dir.path().join("zlint_config.toml"); - let cert_path = tmp_dir.path().join("zlint_cert.pem"); - - { - let mut config_file = File::create(&config_path).expect("create config file"); - config_file - .write_all(ZLINT_CONFIG.as_bytes()) - .expect("Create config file"); - } - { - let mut cert_file = File::create(&cert_path).expect("create pem file"); - cert_file.write_all(pem).expect("Create pem file"); - } - - let mut child = Command::new("zlint") - .arg("-pretty") - .arg("-config") - .arg(&config_path) - .arg(&cert_path) - .stderr(Stdio::inherit()) - .stdout(Stdio::piped()) - .spawn() - .unwrap_or_else(|e| match e.kind() { - io::ErrorKind::NotFound => { - panic!("error running 'zlint': command not found. Is it installed?") - } - _ => panic!("error running 'zlint': {e:?}"), - }); - - let mut stdout = child.stdout.take().unwrap(); - - let output_buf = { - let mut buf = Vec::new(); - stdout.read_to_end(&mut buf).expect("read zlint output"); - buf - }; - - let exit_status = child.wait().expect("get zlint status"); - - assert!(exit_status.success(), "zlint failed"); - - let output: LintResult = serde_json::from_slice(&output_buf).expect("parse zlint output"); - - assert!(output.check_lints(ignored)); -} - -#[test] -fn parse_zlint_output() { - let demo_output = br#" - { - "e_algorithm_identifier_improper_encoding": {"result": "pass"}, - "e_basic_constraints_not_critical": {"result": "NA", "details": "foo"} - } - "#; - - let output: LintResult = serde_json::from_slice(demo_output).expect("parse output"); - - assert_eq!( - output.0.get("e_algorithm_identifier_improper_encoding"), - Some(&LintStatus { - status: Status::Pass, - details: None - }) - ); -} - -#[test] -fn fatal_lint_fails_unless_ignored() { - let output: LintResult = - serde_json::from_str(r#"{"example_lint":{"result":"fatal"}}"#).unwrap(); - - assert!(!output.check_lints(&[])); - assert!(output.check_lints(&["example_lint"])); -} diff --git a/x509-cert/tests/builder.rs b/x509-cert/tests/builder.rs index 189998013..01c60a933 100644 --- a/x509-cert/tests/builder.rs +++ b/x509-cert/tests/builder.rs @@ -22,7 +22,7 @@ use x509_cert::{ serial_number::SerialNumber, time::Validity, }; -use x509_cert_test_support::{openssl, zlint}; +use x509_cert_test_support::openssl; #[cfg(feature = "hazmat")] use x509_cert::builder::profile::cabf::tls::Tls12Options;