Skip to content

Commit 682cd13

Browse files
committed
Make logic independant from server certificates
1 parent fcc1e83 commit 682cd13

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

sqlx-core/src/net/tls/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,14 @@ async fn configure_tls_connector(
138138

139139
builder.add_root_certificate(cert);
140140
}
141+
}
141142

142-
// authentication using user's key-file and its associated certificate
143-
if let (Some(cert_path), Some(key_path)) = (client_cert_path, client_key_path) {
144-
let cert_path = cert_path.data().await?;
145-
let key_path = key_path.data().await?;
146-
let identity = Identity::from_pkcs8(&cert_path, &key_path)?;
147-
builder.identity(identity);
148-
}
143+
// authentication using user's key-file and its associated certificate
144+
if let (Some(cert_path), Some(key_path)) = (client_cert_path, client_key_path) {
145+
let cert_path = cert_path.data().await?;
146+
let key_path = key_path.data().await?;
147+
let identity = Identity::from_pkcs8(&cert_path, &key_path)?;
148+
builder.identity(identity);
149149
}
150150

151151
#[cfg(not(feature = "_rt-async-std"))]

sqlx-core/src/net/tls/rustls.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)