From 92d1b21caf5ddac5c085ad5819f2aa26405dec82 Mon Sep 17 00:00:00 2001 From: nworbnhoj Date: Sun, 25 Feb 2024 15:49:36 +1000 Subject: [PATCH] Add a single bogus TrustAnchor to danger::StifledCertificateVerification root_cert_store rustls::ServerCertVerifierBuilder::build() returns a `CertVerifierBuilderError` if no trust anchors have been provided. --- libs/tls/src/danger.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libs/tls/src/danger.rs b/libs/tls/src/danger.rs index ec4cd1605..f7f6df4c1 100644 --- a/libs/tls/src/danger.rs +++ b/libs/tls/src/danger.rs @@ -3,7 +3,7 @@ use modals::Modals; use rustls::client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}; use rustls::client::WebPkiServerVerifier; use rustls::crypto::{verify_tls12_signature, verify_tls13_signature, WebPkiSupportedAlgorithms}; -use rustls::pki_types::{CertificateDer, ServerName, UnixTime}; +use rustls::pki_types::{CertificateDer, Der, ServerName, TrustAnchor, UnixTime}; use rustls::{CertificateError, DigitallySignedStruct, Error, RootCertStore, SignatureScheme}; use std::sync::Arc; use webpki::ring as webpki_algs; @@ -17,7 +17,16 @@ pub struct StifledCertificateVerification { impl StifledCertificateVerification { pub fn new() -> Self { - Self { roots: rustls::RootCertStore::empty(), supported: SUPPORTED_SIG_ALGS } + let mut root_cert_store = rustls::RootCertStore::empty(); + // rustls::ServerCertVerifierBuilder::build() returns a + // `CertVerifierBuilderError` if no trust anchors have been provided. + let single_bogus_ta_to_avoid_error_on_empty_roots = TrustAnchor { + subject: Der::from_slice(b"bogus subject"), + subject_public_key_info: Der::from_slice(b"bogus subject_public_key_info"), + name_constraints: None, + }; + root_cert_store.roots.push(single_bogus_ta_to_avoid_error_on_empty_roots); + Self { roots: root_cert_store, supported: SUPPORTED_SIG_ALGS } } }