@@ -220,7 +220,11 @@ impl Certificate {
220220
221221 fn read_pem_certs ( reader : & mut impl BufRead ) -> crate :: Result < Vec < Vec < u8 > > > {
222222 rustls_pemfile:: certs ( reader)
223- . map_err ( |_| crate :: error:: builder ( "invalid certificate encoding" ) )
223+ . map ( |result| match result {
224+ Ok ( cert) => Ok ( cert. as_ref ( ) . to_vec ( ) ) ,
225+ Err ( _) => Err ( crate :: error:: builder ( "invalid certificate encoding" ) ) ,
226+ } )
227+ . collect ( )
224228 }
225229}
226230
@@ -326,34 +330,30 @@ impl Identity {
326330 /// This requires the `rustls-tls(-...)` Cargo feature enabled.
327331 #[ cfg( feature = "__rustls" ) ]
328332 pub fn from_pem ( buf : & [ u8 ] ) -> crate :: Result < Identity > {
333+ use rustls_pemfile:: Item ;
329334 use std:: io:: Cursor ;
330335
331336 let ( key, certs) = {
332337 let mut pem = Cursor :: new ( buf) ;
333338 let mut sk = Vec :: < rustls_pki_types:: PrivateKeyDer > :: new ( ) ;
334339 let mut certs = Vec :: < rustls_pki_types:: CertificateDer > :: new ( ) ;
335340
336- for item in std:: iter:: from_fn ( || rustls_pemfile:: read_one ( & mut pem) . transpose ( ) ) {
337- match item. map_err ( |_| {
338- crate :: error:: builder ( TLSError :: General ( String :: from (
339- "Invalid identity PEM file" ,
340- ) ) )
341- } ) ? {
342- rustls_pemfile:: Item :: X509Certificate ( cert) => certs. push ( cert. into ( ) ) ,
343- rustls_pemfile:: Item :: PKCS8Key ( key) => {
344- sk. push ( rustls_pki_types:: PrivateKeyDer :: Pkcs8 ( key. into ( ) ) )
345- }
346- rustls_pemfile:: Item :: RSAKey ( key) => {
347- sk. push ( rustls_pki_types:: PrivateKeyDer :: Pkcs1 ( key. into ( ) ) )
348- }
349- rustls_pemfile:: Item :: ECKey ( key) => {
350- sk. push ( rustls_pki_types:: PrivateKeyDer :: Sec1 ( key. into ( ) ) )
351- }
352- _ => {
341+ for result in rustls_pemfile:: read_all ( & mut pem) {
342+ match result {
343+ Ok ( Item :: X509Certificate ( cert) ) => certs. push ( cert) ,
344+ Ok ( Item :: Pkcs1Key ( key) ) => sk. push ( key. into ( ) ) ,
345+ Ok ( Item :: Pkcs8Key ( key) ) => sk. push ( key. into ( ) ) ,
346+ Ok ( Item :: Sec1Key ( key) ) => sk. push ( key. into ( ) ) ,
347+ Ok ( _) => {
353348 return Err ( crate :: error:: builder ( TLSError :: General ( String :: from (
354349 "No valid certificate was found" ,
355350 ) ) ) )
356351 }
352+ Err ( _) => {
353+ return Err ( crate :: error:: builder ( TLSError :: General ( String :: from (
354+ "Invalid identity PEM file" ,
355+ ) ) ) )
356+ }
357357 }
358358 }
359359
0 commit comments