Skip to content

Commit b43a06a

Browse files
committed
Use more meaningful type for test roots
1 parent 1a0335f commit b43a06a

File tree

6 files changed

+24
-18
lines changed

6 files changed

+24
-18
lines changed

rustls-platform-verifier/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
#![doc = include_str!("../README.md")]
33
#![warn(missing_docs)]
44

5-
use rustls::{client::WantsClientCert, ClientConfig, ConfigBuilder, WantsVerifier};
65
use std::sync::Arc;
76

7+
#[cfg(feature = "dbg")]
8+
use rustls::pki_types::CertificateDer;
9+
use rustls::{client::WantsClientCert, ClientConfig, ConfigBuilder, WantsVerifier};
10+
811
mod verification;
912
pub use verification::Verifier;
1013

@@ -84,7 +87,9 @@ pub fn tls_config_with_provider(
8487
///
8588
/// This is not intended for production use, you should use [tls_config] instead.
8689
#[cfg(feature = "dbg")]
87-
pub fn verifier_for_dbg(root: &[u8]) -> Arc<dyn rustls::client::danger::ServerCertVerifier> {
90+
pub fn verifier_for_dbg(
91+
root: CertificateDer<'static>,
92+
) -> Arc<dyn rustls::client::danger::ServerCertVerifier> {
8893
Arc::new(Verifier::new_with_fake_root(root))
8994
}
9095

rustls-platform-verifier/src/tests/verification_mock/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ macro_rules! no_error {
8080
};
8181
}
8282

83-
const ROOT1: &[u8] = include_bytes!("root1.crt");
83+
const ROOT1: pki_types::CertificateDer<'static> =
84+
pki_types::CertificateDer::from_slice(include_bytes!("root1.crt"));
8485
const ROOT1_INT1: &[u8] = include_bytes!("root1-int1.crt");
8586
const ROOT1_INT1_EXAMPLE_COM_GOOD: &[u8] = include_bytes!("root1-int1-ee_example.com-good.crt");
8687
const ROOT1_INT1_LOCALHOST_IPV4_GOOD: &[u8] = include_bytes!("root1-int1-ee_127.0.0.1-good.crt");
@@ -340,7 +341,7 @@ fn test_with_mock_root<E: std::error::Error + PartialEq + 'static>(
340341
let verifier = match root_src {
341342
Roots::OnlyExtra => Verifier::new_with_fake_root(ROOT1), // TODO: time
342343
#[cfg(not(target_os = "android"))]
343-
Roots::ExtraAndPlatform => Verifier::new_with_extra_roots([ROOT1.into()]).unwrap(),
344+
Roots::ExtraAndPlatform => Verifier::new_with_extra_roots([ROOT1]).unwrap(),
344345
};
345346
let mut chain = test_case
346347
.chain

rustls-platform-verifier/src/verification/android.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const AUTH_TYPE: &str = "RSA";
4646
pub struct Verifier {
4747
/// Testing only: The root CA certificate to trust.
4848
#[cfg(any(test, feature = "ffi-testing"))]
49-
test_only_root_ca_override: Option<Vec<u8>>,
49+
test_only_root_ca_override: Option<pki_types::CertificateDer<'static>>,
5050
pub(super) crypto_provider: OnceCell<Arc<CryptoProvider>>,
5151
}
5252

@@ -85,9 +85,9 @@ impl Verifier {
8585

8686
/// Creates a test-only TLS certificate verifier which trusts our fake root CA cert.
8787
#[cfg(any(test, feature = "ffi-testing"))]
88-
pub(crate) fn new_with_fake_root(root: &[u8]) -> Self {
88+
pub(crate) fn new_with_fake_root(root: pki_types::CertificateDer<'static>) -> Self {
8989
Self {
90-
test_only_root_ca_override: Some(root.into()),
90+
test_only_root_ca_override: Some(root),
9191
crypto_provider: OnceCell::new(),
9292
}
9393
}

rustls-platform-verifier/src/verification/apple.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use std::sync::Arc;
22

3-
use super::log_server_cert;
4-
use crate::verification::invalid_certificate;
53
use core_foundation::date::CFDate;
64
use core_foundation_sys::date::kCFAbsoluteTimeIntervalSince1970;
75
use once_cell::sync::OnceCell;
@@ -16,6 +14,9 @@ use security_framework::{
1614
trust::SecTrust,
1715
};
1816

17+
use super::log_server_cert;
18+
use crate::verification::invalid_certificate;
19+
1920
mod errors {
2021
pub(super) use security_framework_sys::base::{
2122
errSecCertificateRevoked, errSecCreateChainFailed, errSecHostNameMismatch,
@@ -92,10 +93,10 @@ impl Verifier {
9293

9394
/// Creates a test-only TLS certificate verifier which trusts our fake root CA cert.
9495
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
95-
pub(crate) fn new_with_fake_root(root: &[u8]) -> Self {
96+
pub(crate) fn new_with_fake_root(root: pki_types::CertificateDer<'static>) -> Self {
9697
Self {
9798
extra_roots: Vec::new(),
98-
test_only_root_ca_override: Some(SecCertificate::from_der(root).unwrap()),
99+
test_only_root_ca_override: Some(SecCertificate::from_der(root.as_ref()).unwrap()),
99100
crypto_provider: OnceCell::new(),
100101
}
101102
}

rustls-platform-verifier/src/verification/others.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::log_server_cert;
22
use once_cell::sync::OnceCell;
33
use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier};
44
use rustls::client::WebPkiServerVerifier;
5-
use rustls::pki_types;
65
use rustls::{
76
crypto::CryptoProvider, CertificateError, DigitallySignedStruct, Error as TlsError, OtherError,
87
SignatureScheme,
@@ -28,7 +27,7 @@ pub struct Verifier {
2827

2928
/// Testing only: an additional root CA certificate to trust.
3029
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
31-
test_only_root_ca_override: Option<Vec<u8>>,
30+
test_only_root_ca_override: Option<pki_types::CertificateDer<'static>>,
3231

3332
pub(super) crypto_provider: OnceCell<Arc<CryptoProvider>>,
3433
}
@@ -73,11 +72,11 @@ impl Verifier {
7372

7473
/// Creates a test-only TLS certificate verifier which trusts our fake root CA cert.
7574
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
76-
pub(crate) fn new_with_fake_root(root: &[u8]) -> Self {
75+
pub(crate) fn new_with_fake_root(root: pki_types::CertificateDer<'static>) -> Self {
7776
Self {
7877
inner: OnceCell::new(),
7978
extra_roots: Vec::new().into(),
80-
test_only_root_ca_override: Some(root.into()),
79+
test_only_root_ca_override: Some(root),
8180
crypto_provider: OnceCell::new(),
8281
}
8382
}

rustls-platform-verifier/src/verification/windows.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ fn call_with_last_error<T, F: FnMut() -> Option<T>>(mut call: F) -> Result<T, Tl
486486
pub struct Verifier {
487487
/// Testing only: The root CA certificate to trust.
488488
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
489-
test_only_root_ca_override: Option<Vec<u8>>,
489+
test_only_root_ca_override: Option<pki_types::CertificateDer<'static>>,
490490
pub(super) crypto_provider: OnceCell<Arc<CryptoProvider>>,
491491
/// Extra trust anchors to add to the verifier above and beyond those provided by
492492
/// the system-provided trust stores.
@@ -529,9 +529,9 @@ impl Verifier {
529529

530530
/// Creates a test-only TLS certificate verifier which trusts our fake root CA cert.
531531
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
532-
pub(crate) fn new_with_fake_root(root: &[u8]) -> Self {
532+
pub(crate) fn new_with_fake_root(root: pki_types::CertificateDer<'static>) -> Self {
533533
Self {
534-
test_only_root_ca_override: Some(root.into()),
534+
test_only_root_ca_override: Some(root),
535535
crypto_provider: OnceCell::new(),
536536
extra_roots: None,
537537
}

0 commit comments

Comments
 (0)