Skip to content

Commit f864c1e

Browse files
stormshield-gtcpu
authored andcommitted
Parse the extra roots only once on Apple
1 parent 245f8a6 commit f864c1e

File tree

1 file changed

+26
-22
lines changed
  • rustls-platform-verifier/src/verification

1 file changed

+26
-22
lines changed

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ fn system_time_to_cfdate(time: pki_types::UnixTime) -> Result<CFDate, TlsError>
4545
pub struct Verifier {
4646
/// Extra trust anchors to add to the verifier above and beyond those provided by
4747
/// the system-provided trust stores.
48-
extra_roots: Vec<pki_types::CertificateDer<'static>>,
48+
extra_roots: Vec<SecCertificate>,
4949
/// Testing only: The root CA certificate to trust.
5050
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
51-
test_only_root_ca_override: Option<Vec<u8>>,
51+
test_only_root_ca_override: Option<SecCertificate>,
5252
pub(super) crypto_provider: OnceCell<Arc<CryptoProvider>>,
5353
}
5454

@@ -72,21 +72,30 @@ impl Verifier {
7272
/// facilities with the addition of extra root certificates to trust.
7373
///
7474
/// See [Verifier::new] for the external requirements the verifier needs.
75-
pub fn new_with_extra_roots(roots: Vec<pki_types::CertificateDer<'static>>) -> Self {
76-
Self {
77-
extra_roots: roots,
75+
pub fn new_with_extra_roots(
76+
roots: Vec<pki_types::CertificateDer<'static>>,
77+
) -> Result<Self, TlsError> {
78+
let extra_roots = roots
79+
.into_iter()
80+
.map(|root| {
81+
SecCertificate::from_der(&root)
82+
.map_err(|_| TlsError::InvalidCertificate(CertificateError::BadEncoding))
83+
})
84+
.collect::<Result<Vec<_>, _>>()?;
85+
Ok(Self {
86+
extra_roots,
7887
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
7988
test_only_root_ca_override: None,
8089
crypto_provider: OnceCell::new(),
81-
}
90+
})
8291
}
8392

8493
/// Creates a test-only TLS certificate verifier which trusts our fake root CA cert.
8594
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
8695
pub(crate) fn new_with_fake_root(root: &[u8]) -> Self {
8796
Self {
8897
extra_roots: Vec::new(),
89-
test_only_root_ca_override: Some(root.into()),
98+
test_only_root_ca_override: Some(SecCertificate::from_der(root).unwrap()),
9099
crypto_provider: OnceCell::new(),
91100
}
92101
}
@@ -141,29 +150,24 @@ impl Verifier {
141150
.map_err(|e| invalid_certificate(e.to_string()))?;
142151
}
143152

144-
let raw_extra_roots = self.extra_roots.iter();
153+
#[cfg(not(any(test, feature = "ffi-testing", feature = "dbg")))]
154+
let extra_roots = self.extra_roots.as_slice();
145155

146156
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
147-
let extra_root = self
148-
.test_only_root_ca_override
149-
.as_ref()
150-
.map(|root| pki_types::CertificateDer::from_slice(root));
151-
157+
let extra_roots: Vec<_> = self
158+
.extra_roots
159+
.iter()
160+
.chain(self.test_only_root_ca_override.as_ref())
161+
.cloned()
162+
.collect();
152163
#[cfg(any(test, feature = "ffi-testing", feature = "dbg"))]
153-
let raw_extra_roots = raw_extra_roots.chain(&extra_root).to_owned();
154-
155-
let extra_roots = raw_extra_roots
156-
.map(|root| {
157-
SecCertificate::from_der(root)
158-
.map_err(|_| TlsError::InvalidCertificate(CertificateError::BadEncoding))
159-
})
160-
.collect::<Result<Vec<_>, _>>()?;
164+
let extra_roots = extra_roots.as_slice();
161165

162166
// If any extra roots were provided by the user (or tests), provide them to the trust
163167
// evaluation regardless of their system trust settings or status.
164168
if !extra_roots.is_empty() {
165169
trust_evaluation
166-
.set_anchor_certificates(&extra_roots)
170+
.set_anchor_certificates(extra_roots)
167171
.map_err(|e| TlsError::Other(OtherError(Arc::new(e))))?;
168172

169173
// We want to trust both the system-installed and the extra roots. This must be set

0 commit comments

Comments
 (0)