11#[ cfg( feature = "use-rustls-no-provider" ) ]
2- use rustls_pemfile:: Item ;
3- #[ cfg( feature = "use-rustls-no-provider" ) ]
42use tokio_rustls:: rustls:: {
53 self ,
6- pki_types:: { InvalidDnsNameError , ServerName } ,
4+ pki_types:: {
5+ pem:: Error as PemError , pem:: PemObject , CertificateDer , InvalidDnsNameError , PrivateKeyDer ,
6+ ServerName ,
7+ } ,
78 ClientConfig , RootCertStore ,
89} ;
910#[ cfg( feature = "use-rustls-no-provider" ) ]
@@ -12,8 +13,6 @@ use tokio_rustls::TlsConnector as RustlsConnector;
1213#[ cfg( feature = "use-rustls-no-provider" ) ]
1314use std:: convert:: TryFrom ;
1415#[ cfg( feature = "use-rustls-no-provider" ) ]
15- use std:: io:: { BufReader , Cursor } ;
16- #[ cfg( feature = "use-rustls-no-provider" ) ]
1716use std:: sync:: Arc ;
1817
1918use crate :: framed:: AsyncReadWrite ;
@@ -60,6 +59,10 @@ pub enum Error {
6059 /// No valid key found
6160 #[ error( "No valid key in chain" ) ]
6261 NoValidKeyInChain ,
62+ #[ cfg( feature = "use-rustls-no-provider" ) ]
63+ /// PEM parsing error
64+ #[ error( "PEM parsing error: {0}" ) ]
65+ Pem ( #[ from] PemError ) ,
6366 #[ cfg( feature = "use-native-tls" ) ]
6467 #[ error( "Native TLS error {0}" ) ]
6568 NativeTls ( #[ from] NativeTlsError ) ,
@@ -75,8 +78,8 @@ pub async fn rustls_connector(tls_config: &TlsConfiguration) -> Result<RustlsCon
7578 } => {
7679 // Add ca to root store if the connection is TLS
7780 let mut root_cert_store = RootCertStore :: empty ( ) ;
78- let certs = rustls_pemfile :: certs ( & mut BufReader :: new ( Cursor :: new ( ca ) ) )
79- . collect :: < Result < Vec < _ > , _ > > ( ) ?;
81+ let certs: Vec < CertificateDer > =
82+ CertificateDer :: pem_slice_iter ( ca ) . collect :: < Result < Vec < _ > , _ > > ( ) ?;
8083
8184 root_cert_store. add_parsable_certificates ( certs) ;
8285
@@ -88,34 +91,14 @@ pub async fn rustls_connector(tls_config: &TlsConfiguration) -> Result<RustlsCon
8891
8992 // Add der encoded client cert and key
9093 let mut config = if let Some ( client) = client_auth. as_ref ( ) {
91- let certs =
92- rustls_pemfile :: certs ( & mut BufReader :: new ( Cursor :: new ( client. 0 . clone ( ) ) ) )
93- . collect :: < Result < Vec < _ > , _ > > ( ) ? ;
94+ let certs: Vec < CertificateDer > =
95+ CertificateDer :: pem_slice_iter ( & client. 0 ) . collect :: < Result < Vec < _ > , _ > > ( ) ? ;
96+
9497 if certs. is_empty ( ) {
9598 return Err ( Error :: NoValidClientCertInChain ) ;
9699 }
97100
98- // Create buffer for key file
99- let mut key_buffer = BufReader :: new ( Cursor :: new ( client. 1 . clone ( ) ) ) ;
100-
101- // Read PEM items until we find a valid key.
102- let key = loop {
103- let item = rustls_pemfile:: read_one ( & mut key_buffer) ?;
104- match item {
105- Some ( Item :: Sec1Key ( key) ) => {
106- break key. into ( ) ;
107- }
108- Some ( Item :: Pkcs1Key ( key) ) => {
109- break key. into ( ) ;
110- }
111- Some ( Item :: Pkcs8Key ( key) ) => {
112- break key. into ( ) ;
113- }
114- None => return Err ( Error :: NoValidKeyInChain ) ,
115- _ => { }
116- }
117- } ;
118-
101+ let key = PrivateKeyDer :: from_pem_slice ( & client. 1 ) ?;
119102 config. with_client_auth_cert ( certs, key) ?
120103 } else {
121104 config. with_no_client_auth ( )
0 commit comments