@@ -28,7 +28,7 @@ use std::net::{Ipv4Addr, Ipv6Addr};
2828use std:: sync:: Arc ;
2929
3030use rustls:: client:: danger:: ServerCertVerifier ;
31- use rustls:: pki_types;
31+ use rustls:: pki_types:: { self , CertificateDer } ;
3232#[ cfg( not( any( target_vendor = "apple" , windows) ) ) ]
3333use rustls:: pki_types:: { DnsName , ServerName } ;
3434use rustls:: { CertificateError , Error as TlsError , OtherError } ;
@@ -80,13 +80,15 @@ macro_rules! no_error {
8080 } ;
8181}
8282
83- const ROOT1 : pki_types:: CertificateDer < ' static > =
84- pki_types:: CertificateDer :: from_slice ( include_bytes ! ( "root1.crt" ) ) ;
83+ const ROOT1 : CertificateDer = CertificateDer :: from_slice ( include_bytes ! ( "root1.crt" ) ) ;
8584const ROOT1_INT1 : & [ u8 ] = include_bytes ! ( "root1-int1.crt" ) ;
8685const ROOT1_INT1_EXAMPLE_COM_GOOD : & [ u8 ] = include_bytes ! ( "root1-int1-ee_example.com-good.crt" ) ;
8786const ROOT1_INT1_LOCALHOST_IPV4_GOOD : & [ u8 ] = include_bytes ! ( "root1-int1-ee_127.0.0.1-good.crt" ) ;
8887const ROOT1_INT1_LOCALHOST_IPV6_GOOD : & [ u8 ] = include_bytes ! ( "root1-int1-ee_1-good.crt" ) ;
8988
89+ #[ cfg( not( any( target_os = "android" , target_os = "windows" ) ) ) ]
90+ const ROOT2 : CertificateDer = CertificateDer :: from_slice ( include_bytes ! ( "root2.crt" ) ) ;
91+
9092const EXAMPLE_COM : & str = "example.com" ;
9193const LOCALHOST_IPV4 : & str = "127.0.0.1" ;
9294const LOCALHOST_IPV6 : & str = "::1" ;
@@ -111,8 +113,8 @@ pub(super) fn verification_without_mock_root() {
111113 let verifier = Verifier :: new ( crypto_provider) . unwrap ( ) ;
112114
113115 let server_name = pki_types:: ServerName :: try_from ( EXAMPLE_COM ) . unwrap ( ) ;
114- let end_entity = pki_types :: CertificateDer :: from ( ROOT1_INT1_EXAMPLE_COM_GOOD ) ;
115- let intermediates = [ pki_types :: CertificateDer :: from ( ROOT1_INT1 ) ] ;
116+ let end_entity = CertificateDer :: from ( ROOT1_INT1_EXAMPLE_COM_GOOD ) ;
117+ let intermediates = [ CertificateDer :: from ( ROOT1_INT1 ) ] ;
116118
117119 // Fails because the server cert has no trust root in Windows, and can't since it uses a self-signed CA.
118120 // Similarly on UNIX platforms using the Webpki verifier, it can't fetch extra certificates through
@@ -139,6 +141,45 @@ fn test_verification_without_mock_root() {
139141 verification_without_mock_root ( )
140142}
141143
144+ #[ cfg( not( any( target_os = "android" , target_os = "windows" ) ) ) ]
145+ #[ test]
146+ fn test_selfsigned_cert_with_extra_roots ( ) {
147+ let crypto_provider = test_provider ( ) ;
148+
149+ let selfsigned = ROOT2 ;
150+ let roots = vec ! [ selfsigned. clone( ) ] ;
151+ let server_name = pki_types:: ServerName :: try_from ( EXAMPLE_COM ) . unwrap ( ) ;
152+
153+ let verifier = Verifier :: new_with_extra_roots ( roots, crypto_provider) . unwrap ( ) ;
154+
155+ verifier
156+ . verify_server_cert ( & selfsigned, & [ ] , & server_name, & [ ] , verification_time ( ) )
157+ . expect ( "failed to validate singular extra root certificate chain" ) ;
158+ }
159+
160+ #[ cfg( not( target_os = "android" ) ) ]
161+ #[ test]
162+ fn test_chain_signed_with_extra_roots ( ) {
163+ let crypto_provider = test_provider ( ) ;
164+
165+ let server_name = pki_types:: ServerName :: try_from ( EXAMPLE_COM ) . unwrap ( ) ;
166+ let end_entity = CertificateDer :: from ( ROOT1_INT1_EXAMPLE_COM_GOOD ) ;
167+ let intermediates = [ CertificateDer :: from ( ROOT1_INT1 ) ] ;
168+ let roots = vec ! [ ROOT1 ] ;
169+
170+ let verifier = Verifier :: new_with_extra_roots ( roots, crypto_provider) . unwrap ( ) ;
171+
172+ verifier
173+ . verify_server_cert (
174+ & end_entity,
175+ & intermediates,
176+ & server_name,
177+ & [ ] ,
178+ verification_time ( ) ,
179+ )
180+ . expect ( "failed to validate extra root-only certificate chain" ) ;
181+ }
182+
142183// Note: Android does not currently support IP address hosts, so these tests are disabled for
143184// Android.
144185// Verifies that our test trust anchor(s) are not trusted when `Verifier::new()`
@@ -349,10 +390,10 @@ fn test_with_mock_root<E: std::error::Error + PartialEq + 'static>(
349390 let mut chain = test_case
350391 . chain
351392 . iter ( )
352- . map ( |bytes| pki_types :: CertificateDer :: from ( * bytes) ) ;
393+ . map ( |bytes| CertificateDer :: from ( * bytes) ) ;
353394
354395 let end_entity = chain. next ( ) . unwrap ( ) ;
355- let intermediates: Vec < pki_types :: CertificateDer < ' _ > > = chain. collect ( ) ;
396+ let intermediates: Vec < CertificateDer < ' _ > > = chain. collect ( ) ;
356397
357398 let server_name = pki_types:: ServerName :: try_from ( test_case. reference_id ) . unwrap ( ) ;
358399
0 commit comments