@@ -9,17 +9,12 @@ pub use rustls_tokio_stream::*;
99pub use webpki;
1010pub use webpki_roots;
1111
12- use deno_core:: anyhow:: anyhow;
13- use deno_core:: error:: custom_error;
14- use deno_core:: error:: AnyError ;
15-
1612use rustls:: client:: danger:: HandshakeSignatureValid ;
1713use rustls:: client:: danger:: ServerCertVerified ;
1814use rustls:: client:: danger:: ServerCertVerifier ;
1915use rustls:: client:: WebPkiServerVerifier ;
2016use rustls:: ClientConfig ;
2117use rustls:: DigitallySignedStruct ;
22- use rustls:: Error ;
2318use rustls:: RootCertStore ;
2419use rustls_pemfile:: certs;
2520use rustls_pemfile:: ec_private_keys;
@@ -35,12 +30,30 @@ use std::sync::Arc;
3530mod tls_key;
3631pub use tls_key:: * ;
3732
33+ #[ derive( Debug , thiserror:: Error ) ]
34+ pub enum TlsError {
35+ #[ error( transparent) ]
36+ Rustls ( #[ from] rustls:: Error ) ,
37+ #[ error( "Unable to add pem file to certificate store: {0}" ) ]
38+ UnableAddPemFileToCert ( std:: io:: Error ) ,
39+ #[ error( "Unable to decode certificate" ) ]
40+ CertInvalid ,
41+ #[ error( "No certificates found in certificate data" ) ]
42+ CertsNotFound ,
43+ #[ error( "No keys found in key data" ) ]
44+ KeysNotFound ,
45+ #[ error( "Unable to decode key" ) ]
46+ KeyDecode ,
47+ }
48+
3849/// Lazily resolves the root cert store.
3950///
4051/// This was done because the root cert store is not needed in all cases
4152/// and takes a bit of time to initialize.
4253pub trait RootCertStoreProvider : Send + Sync {
43- fn get_or_try_init ( & self ) -> Result < & RootCertStore , AnyError > ;
54+ fn get_or_try_init (
55+ & self ,
56+ ) -> Result < & RootCertStore , deno_core:: error:: AnyError > ;
4457}
4558
4659// This extension has no runtime apis, it only exports some shared native functions.
@@ -77,7 +90,7 @@ impl ServerCertVerifier for NoCertificateVerification {
7790 server_name : & rustls:: pki_types:: ServerName < ' _ > ,
7891 ocsp_response : & [ u8 ] ,
7992 now : rustls:: pki_types:: UnixTime ,
80- ) -> Result < ServerCertVerified , Error > {
93+ ) -> Result < ServerCertVerified , rustls :: Error > {
8194 if self . ic_allowlist . is_empty ( ) {
8295 return Ok ( ServerCertVerified :: assertion ( ) ) ;
8396 }
@@ -89,7 +102,9 @@ impl ServerCertVerifier for NoCertificateVerification {
89102 _ => {
90103 // NOTE(bartlomieju): `ServerName` is a non-exhaustive enum
91104 // so we have this catch all errors here.
92- return Err ( Error :: General ( "Unknown `ServerName` variant" . to_string ( ) ) ) ;
105+ return Err ( rustls:: Error :: General (
106+ "Unknown `ServerName` variant" . to_string ( ) ,
107+ ) ) ;
93108 }
94109 } ;
95110 if self . ic_allowlist . contains ( & dns_name_or_ip_address) {
@@ -110,7 +125,7 @@ impl ServerCertVerifier for NoCertificateVerification {
110125 message : & [ u8 ] ,
111126 cert : & rustls:: pki_types:: CertificateDer ,
112127 dss : & DigitallySignedStruct ,
113- ) -> Result < HandshakeSignatureValid , Error > {
128+ ) -> Result < HandshakeSignatureValid , rustls :: Error > {
114129 if self . ic_allowlist . is_empty ( ) {
115130 return Ok ( HandshakeSignatureValid :: assertion ( ) ) ;
116131 }
@@ -126,7 +141,7 @@ impl ServerCertVerifier for NoCertificateVerification {
126141 message : & [ u8 ] ,
127142 cert : & rustls:: pki_types:: CertificateDer ,
128143 dss : & DigitallySignedStruct ,
129- ) -> Result < HandshakeSignatureValid , Error > {
144+ ) -> Result < HandshakeSignatureValid , rustls :: Error > {
130145 if self . ic_allowlist . is_empty ( ) {
131146 return Ok ( HandshakeSignatureValid :: assertion ( ) ) ;
132147 }
@@ -178,7 +193,7 @@ pub fn create_client_config(
178193 unsafely_ignore_certificate_errors : Option < Vec < String > > ,
179194 maybe_cert_chain_and_key : TlsKeys ,
180195 socket_use : SocketUse ,
181- ) -> Result < ClientConfig , AnyError > {
196+ ) -> Result < ClientConfig , TlsError > {
182197 if let Some ( ic_allowlist) = unsafely_ignore_certificate_errors {
183198 let client_config = ClientConfig :: builder ( )
184199 . dangerous ( )
@@ -214,10 +229,7 @@ pub fn create_client_config(
214229 root_cert_store. add ( cert) ?;
215230 }
216231 Err ( e) => {
217- return Err ( anyhow ! (
218- "Unable to add pem file to certificate store: {}" ,
219- e
220- ) ) ;
232+ return Err ( TlsError :: UnableAddPemFileToCert ( e) ) ;
221233 }
222234 }
223235 }
@@ -255,74 +267,61 @@ fn add_alpn(client: &mut ClientConfig, socket_use: SocketUse) {
255267
256268pub fn load_certs (
257269 reader : & mut dyn BufRead ,
258- ) -> Result < Vec < CertificateDer < ' static > > , AnyError > {
270+ ) -> Result < Vec < CertificateDer < ' static > > , TlsError > {
259271 let certs: Result < Vec < _ > , _ > = certs ( reader) . collect ( ) ;
260272
261- let certs = certs
262- . map_err ( |_| custom_error ( "InvalidData" , "Unable to decode certificate" ) ) ?;
273+ let certs = certs. map_err ( |_| TlsError :: CertInvalid ) ?;
263274
264275 if certs. is_empty ( ) {
265- return Err ( cert_not_found_err ( ) ) ;
276+ return Err ( TlsError :: CertsNotFound ) ;
266277 }
267278
268279 Ok ( certs)
269280}
270281
271- fn key_decode_err ( ) -> AnyError {
272- custom_error ( "InvalidData" , "Unable to decode key" )
273- }
274-
275- fn key_not_found_err ( ) -> AnyError {
276- custom_error ( "InvalidData" , "No keys found in key data" )
277- }
278-
279- fn cert_not_found_err ( ) -> AnyError {
280- custom_error ( "InvalidData" , "No certificates found in certificate data" )
281- }
282-
283282/// Starts with -----BEGIN RSA PRIVATE KEY-----
284283fn load_rsa_keys (
285284 mut bytes : & [ u8 ] ,
286- ) -> Result < Vec < PrivateKeyDer < ' static > > , AnyError > {
285+ ) -> Result < Vec < PrivateKeyDer < ' static > > , TlsError > {
287286 let keys: Result < Vec < _ > , _ > = rsa_private_keys ( & mut bytes) . collect ( ) ;
288- let keys = keys. map_err ( |_| key_decode_err ( ) ) ?;
287+ let keys = keys. map_err ( |_| TlsError :: KeyDecode ) ?;
289288 Ok ( keys. into_iter ( ) . map ( PrivateKeyDer :: Pkcs1 ) . collect ( ) )
290289}
291290
292291/// Starts with -----BEGIN EC PRIVATE KEY-----
293292fn load_ec_keys (
294293 mut bytes : & [ u8 ] ,
295- ) -> Result < Vec < PrivateKeyDer < ' static > > , AnyError > {
294+ ) -> Result < Vec < PrivateKeyDer < ' static > > , TlsError > {
296295 let keys: Result < Vec < _ > , std:: io:: Error > =
297296 ec_private_keys ( & mut bytes) . collect ( ) ;
298- let keys2 = keys. map_err ( |_| key_decode_err ( ) ) ?;
297+ let keys2 = keys. map_err ( |_| TlsError :: KeyDecode ) ?;
299298 Ok ( keys2. into_iter ( ) . map ( PrivateKeyDer :: Sec1 ) . collect ( ) )
300299}
301300
302301/// Starts with -----BEGIN PRIVATE KEY-----
303302fn load_pkcs8_keys (
304303 mut bytes : & [ u8 ] ,
305- ) -> Result < Vec < PrivateKeyDer < ' static > > , AnyError > {
304+ ) -> Result < Vec < PrivateKeyDer < ' static > > , TlsError > {
306305 let keys: Result < Vec < _ > , std:: io:: Error > =
307306 pkcs8_private_keys ( & mut bytes) . collect ( ) ;
308- let keys2 = keys. map_err ( |_| key_decode_err ( ) ) ?;
307+ let keys2 = keys. map_err ( |_| TlsError :: KeyDecode ) ?;
309308 Ok ( keys2. into_iter ( ) . map ( PrivateKeyDer :: Pkcs8 ) . collect ( ) )
310309}
311310
312311fn filter_invalid_encoding_err (
313- to_be_filtered : Result < HandshakeSignatureValid , Error > ,
314- ) -> Result < HandshakeSignatureValid , Error > {
312+ to_be_filtered : Result < HandshakeSignatureValid , rustls :: Error > ,
313+ ) -> Result < HandshakeSignatureValid , rustls :: Error > {
315314 match to_be_filtered {
316- Err ( Error :: InvalidCertificate ( rustls:: CertificateError :: BadEncoding ) ) => {
317- Ok ( HandshakeSignatureValid :: assertion ( ) )
318- }
315+ Err ( rustls:: Error :: InvalidCertificate (
316+ rustls :: CertificateError :: BadEncoding ,
317+ ) ) => Ok ( HandshakeSignatureValid :: assertion ( ) ) ,
319318 res => res,
320319 }
321320}
322321
323322pub fn load_private_keys (
324323 bytes : & [ u8 ] ,
325- ) -> Result < Vec < PrivateKeyDer < ' static > > , AnyError > {
324+ ) -> Result < Vec < PrivateKeyDer < ' static > > , TlsError > {
326325 let mut keys = load_rsa_keys ( bytes) ?;
327326
328327 if keys. is_empty ( ) {
@@ -334,7 +333,7 @@ pub fn load_private_keys(
334333 }
335334
336335 if keys. is_empty ( ) {
337- return Err ( key_not_found_err ( ) ) ;
336+ return Err ( TlsError :: KeysNotFound ) ;
338337 }
339338
340339 Ok ( keys)
0 commit comments