@@ -4,6 +4,7 @@ use error::PemLoadError;
44use pem:: encode as pem_encode;
55use pem:: Pem ;
66use rustls:: RootCertStore ;
7+ use rustls_pki_types:: pem:: PemObject ;
78use rustls_pki_types:: CertificateDer ;
89use rustls_pki_types:: PrivateKeyDer ;
910use rustls_pki_types:: PrivatePkcs8KeyDer ;
@@ -47,15 +48,23 @@ impl Certificate {
4748 error : io_error,
4849 } ) ?;
4950
50- let cert = rustls_pemfile:: certs ( & mut & * file_data)
51- . next ( )
52- . ok_or ( PemLoadError :: NoCertificateSection ) ?
53- . map_err ( |io_error| PemLoadError :: FileError {
54- file : filepath. as_ref ( ) . to_path_buf ( ) ,
55- error : io_error,
56- } ) ?;
51+ let der = CertificateDer :: from_pem_slice ( & file_data) . map_err ( |error| {
52+ if let rustls_pki_types:: pem:: Error :: NoItemsFound = error {
53+ PemLoadError :: NoCertificateSection
54+ } else {
55+ PemLoadError :: FileError {
56+ file : filepath. as_ref ( ) . to_path_buf ( ) ,
57+ error : std:: io:: Error :: other ( error) ,
58+ }
59+ }
60+ } ) ?;
5761
58- Ok ( Self ( cert) )
62+ Self :: from_der ( der. to_vec ( ) ) . map_err ( |invalid_certificate| {
63+ PemLoadError :: InvalidCertificateChain {
64+ index : 0 ,
65+ error : invalid_certificate,
66+ }
67+ } )
5968 }
6069
6170 /// Stores the certificate in PEM format into a file asynchronously.
@@ -137,14 +146,18 @@ impl PrivateKey {
137146 error : io_error,
138147 } ) ?;
139148
140- let private_key = rustls_pemfile:: private_key ( & mut & * file_data)
141- . map_err ( |io_error| PemLoadError :: FileError {
142- file : filepath. as_ref ( ) . to_path_buf ( ) ,
143- error : io_error,
144- } ) ?
145- . map ( Self ) ;
149+ let private_key = PrivateKeyDer :: from_pem_slice ( & file_data) . map_err ( |error| {
150+ if let rustls_pki_types:: pem:: Error :: NoItemsFound = error {
151+ PemLoadError :: NoPrivateKeySection
152+ } else {
153+ PemLoadError :: FileError {
154+ file : filepath. as_ref ( ) . to_path_buf ( ) ,
155+ error : std:: io:: Error :: other ( error) ,
156+ }
157+ }
158+ } ) ?;
146159
147- private_key . ok_or ( PemLoadError :: NoPrivateKeySection )
160+ Ok ( Self ( private_key ) )
148161 }
149162
150163 /// Stores the private key in PEM format into a file asynchronously.
@@ -206,14 +219,18 @@ impl CertificateChain {
206219 error : io_error,
207220 } ) ?;
208221
209- let certificates = rustls_pemfile :: certs ( & mut & * file_data)
222+ let certificates = CertificateDer :: pem_slice_iter ( & file_data)
210223 . enumerate ( )
211- . map ( |( index, maybe_cert) | match maybe_cert {
212- Ok ( cert) => Certificate :: from_der ( cert. to_vec ( ) )
213- . map_err ( |error| PemLoadError :: InvalidCertificateChain { index, error } ) ,
214- Err ( io_error) => Err ( PemLoadError :: FileError {
224+ . map ( |( index, maybe_der) | match maybe_der {
225+ Ok ( der) => Certificate :: from_der ( der. to_vec ( ) ) . map_err ( |invalid_certificate| {
226+ PemLoadError :: InvalidCertificateChain {
227+ index,
228+ error : invalid_certificate,
229+ }
230+ } ) ,
231+ Err ( error) => Err ( PemLoadError :: FileError {
215232 file : filepath. as_ref ( ) . to_path_buf ( ) ,
216- error : io_error ,
233+ error : std :: io :: Error :: other ( error ) ,
217234 } ) ,
218235 } )
219236 . collect :: < Result < Vec < _ > , _ > > ( ) ?;
0 commit comments