@@ -16,12 +16,34 @@ pub async fn configure_tls_connector(
1616 client_cert_path : Option < & CertificateInput > ,
1717 client_key_path : Option < & CertificateInput > ,
1818) -> Result < sqlx_rt:: TlsConnector , Error > {
19- let mut config = ClientConfig :: builder ( ) . with_safe_defaults ( ) ;
19+ let config = ClientConfig :: builder ( ) . with_safe_defaults ( ) ;
20+
21+ // authentication using user's key and its associated certificate
22+ let user_auth = match ( client_cert_path, client_key_path) {
23+ ( Some ( cert_path) , Some ( key_path) ) => {
24+ let cert_chain = certs_from_pem ( cert_path. data ( ) . await ?) ?;
25+ let key_der = private_key_from_pem ( key_path. data ( ) . await ?) ?;
26+ Some ( ( cert_chain, key_der) )
27+ }
28+ ( None , None ) => None ,
29+ ( _, _) => {
30+ return Err ( Error :: Configuration (
31+ "user auth key and certs must be given together" . into ( ) ,
32+ ) )
33+ }
34+ } ;
2035
2136 let config = if accept_invalid_certs {
22- config
23- . with_custom_certificate_verifier ( Arc :: new ( DummyTlsVerifier ) )
24- . with_no_client_auth ( )
37+ if let Some ( user_auth) = user_auth {
38+ config
39+ . with_custom_certificate_verifier ( Arc :: new ( DummyTlsVerifier ) )
40+ . with_single_cert ( user_auth. 0 , user_auth. 1 )
41+ . map_err ( |err| Error :: Tls ( err. into ( ) ) ) ?
42+ } else {
43+ config
44+ . with_custom_certificate_verifier ( Arc :: new ( DummyTlsVerifier ) )
45+ . with_no_client_auth ( )
46+ }
2547 } else {
2648 let mut cert_store = RootCertStore :: empty ( ) ;
2749 cert_store. add_server_trust_anchors ( webpki_roots:: TLS_SERVER_ROOTS . 0 . iter ( ) . map ( |ta| {
@@ -45,21 +67,6 @@ pub async fn configure_tls_connector(
4567 }
4668 }
4769
48- // authentication using user's key and its associated certificate
49- let user_auth = match ( client_cert_path, client_key_path) {
50- ( Some ( cert_path) , Some ( key_path) ) => {
51- let cert_chain = certs_from_pem ( cert_path. data ( ) . await ?) ?;
52- let key_der = private_key_from_pem ( key_path. data ( ) . await ?) ?;
53- Some ( ( cert_chain, key_der) )
54- }
55- ( None , None ) => None ,
56- ( _, _) => {
57- return Err ( Error :: Configuration (
58- "user auth key and certs must be given together" . into ( ) ,
59- ) )
60- }
61- } ;
62-
6370 if accept_invalid_hostnames {
6471 let verifier = WebPkiVerifier :: new ( cert_store, None ) ;
6572
0 commit comments