From 5243530aa5c13a4d248b9b3615e7d41bc27c79c1 Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 21 Nov 2024 20:27:31 +1100 Subject: [PATCH] Fix tests and add test that proves multiple DC can be set --- rcgen/tests/generic.rs | 5 +-- rcgen/tests/openssl.rs | 70 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/rcgen/tests/generic.rs b/rcgen/tests/generic.rs index 73324a42..4e704243 100644 --- a/rcgen/tests/generic.rs +++ b/rcgen/tests/generic.rs @@ -374,11 +374,12 @@ mod test_parse_ia5string_subject { let params_from_cert = CertificateParams::from_ca_cert_der(cert_der).unwrap(); // We should find the expected distinguished name in the reconstituted params. - let expected_names = &[(&email_address_dn_type, &email_address_dn_value)]; + let expected_names = &[(email_address_dn_type, email_address_dn_value)]; let names = params_from_cert .distinguished_name - .iter() + .into_iter() .collect::>(); + assert_eq!(names, expected_names); } } diff --git a/rcgen/tests/openssl.rs b/rcgen/tests/openssl.rs index ae0b9bbf..5fddcefa 100644 --- a/rcgen/tests/openssl.rs +++ b/rcgen/tests/openssl.rs @@ -3,7 +3,7 @@ use std::cell::RefCell; use std::io::{Error, ErrorKind, Read, Result as ioResult, Write}; use std::rc::Rc; - +use std::str::FromStr; use openssl::asn1::{Asn1Integer, Asn1Time}; use openssl::bn::BigNum; use openssl::pkey::PKey; @@ -12,10 +12,7 @@ use openssl::stack::Stack; use openssl::x509::store::{X509Store, X509StoreBuilder}; use openssl::x509::{CrlStatus, X509Crl, X509Req, X509StoreContext, X509}; -use rcgen::{ - BasicConstraints, Certificate, CertificateParams, DnType, DnValue, GeneralSubtree, IsCa, - KeyPair, NameConstraints, -}; +use rcgen::{BasicConstraints, Certificate, CertificateParams, DnType, DnValue, GeneralSubtree, Ia5String, IsCa, KeyPair, NameConstraints}; mod util; @@ -540,3 +537,66 @@ fn test_openssl_pkcs1_and_sec1_keys() { let pkcs8_ec_key_der = PrivateKeyDer::try_from(ec_key.private_key_to_pkcs8().unwrap()).unwrap(); KeyPair::try_from(&pkcs8_ec_key_der).unwrap(); } + + +/// Command used to generate: +/// `openssl req -x509 -newkey rsa:4096 -nodes -out mycert.pem -keyout mykey.pem -days 365 -subj "/C=US/ST=California/L=San Francisco/O=Example Company/OU=IT Department/CN=www.example.com/DC=example/DC=com"` +/// Contains two distinct "DC" entries. +const CERT_WITH_MULTI_DC: &str = r#"-----BEGIN CERTIFICATE----- +MIIGSzCCBDOgAwIBAgIUECjoFzATY6rTCtu7HKjBtfXnB/owDQYJKoZIhvcNAQEL +BQAwgbQxCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQH +DA1TYW4gRnJhbmNpc2NvMRgwFgYDVQQKDA9FeGFtcGxlIENvbXBhbnkxFjAUBgNV +BAsMDUlUIERlcGFydG1lbnQxGDAWBgNVBAMMD3d3dy5leGFtcGxlLmNvbTEXMBUG +CgmSJomT8ixkARkWB2V4YW1wbGUxEzARBgoJkiaJk/IsZAEZFgNjb20wHhcNMjQx +MTIxMDkxNTE2WhcNMjUxMTIxMDkxNTE2WjCBtDELMAkGA1UEBhMCVVMxEzARBgNV +BAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDVNhbiBGcmFuY2lzY28xGDAWBgNVBAoM +D0V4YW1wbGUgQ29tcGFueTEWMBQGA1UECwwNSVQgRGVwYXJ0bWVudDEYMBYGA1UE +AwwPd3d3LmV4YW1wbGUuY29tMRcwFQYKCZImiZPyLGQBGRYHZXhhbXBsZTETMBEG +CgmSJomT8ixkARkWA2NvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIB +ANla4cBCTS+6JdEw6kVQHskanjtHbw7F25TZ2tZWC1f/UJuQnpF/JJqADdV6R3ta +xjcGj2ubJnKS1npcdiVN6A95jYggbQqjfZV+Z0cxjL8L4dQ+UPDsNyP8W0+S6UnK ++W813DG/QGXxEFrT8nZIfhyD4qZEtOSFGgp/ZA2f687Sv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TmBX/CDUFYJl4yqavEGfvw40p77gaqIOShEBB54ASKDaSyuLSeYbi +3TQsa+JyWmJ5iNmqVsAy8YfioKveNmyl023hRTjtqJgKQY1UzY6M0bnHa0IlgZq/ +l4A7hDDsvi3rDFiqvKg/WTEZd5G87E9hwIcHF/bJPc+0+MjelRoxFTSty2bpbniR +p3mmtsYxi+XCHdwUwRLhbBrdu93z5Iy3AWIb7vGeTKznnnDweJzYpfHCXuWZdr/d +z6cbmudPzN1l99Op5eH9i1JikA+DQ8BQv1OgkNBw2A== +-----END CERTIFICATE----- +"#; + + + + +#[test] +#[cfg(feature = "x509-parser")] +fn test_parse_certificate_with_multiple_domain_components() { + let param = CertificateParams::from_ca_cert_pem(CERT_WITH_MULTI_DC).unwrap(); + + let domain_component_values = param.distinguished_name.get(&DnType::CustomDnType(vec![ + 0, 9, 2342, 19200300, 100, 1, 25, + ])); + + assert_eq!( + domain_component_values, + vec![ + &DnValue::Ia5String(Ia5String::from_str("com").unwrap()), + &DnValue::Ia5String(Ia5String::from_str("example").unwrap()), + ] + ) +} \ No newline at end of file