-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add option to skip domain name verification of servers
- Loading branch information
1 parent
ee5a170
commit f9d07b2
Showing
7 changed files
with
103 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,8 +12,9 @@ | |
// ZettaScale Zenoh Team, <[email protected]> | ||
// | ||
use crate::{ | ||
config::*, get_tls_addr, get_tls_host, get_tls_server_name, TLS_ACCEPT_THROTTLE_TIME, | ||
TLS_DEFAULT_MTU, TLS_LINGER_TIMEOUT, TLS_LOCATOR_PREFIX, | ||
config::*, get_tls_addr, get_tls_host, get_tls_server_name, | ||
verify::WebPkiVerifierAnyServerName, TLS_ACCEPT_THROTTLE_TIME, TLS_DEFAULT_MTU, | ||
TLS_LINGER_TIMEOUT, TLS_LOCATOR_PREFIX, | ||
}; | ||
use async_rustls::rustls::server::AllowAnyAuthenticatedClient; | ||
use async_rustls::rustls::version::TLS13; | ||
|
@@ -603,6 +604,18 @@ impl TlsClientConfig { | |
client_auth = value.parse()? | ||
} | ||
|
||
let server_name_verification: bool = | ||
if let Some(value) = config.get(TLS_SERVER_NAME_VERIFICATION) { | ||
value | ||
} else { | ||
TLS_SERVER_NAME_VERIFICATION_DEFAULT | ||
} | ||
.parse()?; | ||
|
||
if !server_name_verification { | ||
log::warn!("Skipping name verification of servers"); | ||
} | ||
|
||
// Allows mixed user-generated CA and webPKI CA | ||
log::debug!("Loading default Web PKI certificates."); | ||
let mut root_cert_store: RootCertStore = RootCertStore { | ||
|
@@ -640,19 +653,37 @@ impl TlsClientConfig { | |
bail!("No private key found"); | ||
} | ||
|
||
ClientConfig::builder() | ||
let builder = ClientConfig::builder() | ||
.with_safe_default_cipher_suites() | ||
.with_safe_default_kx_groups() | ||
.with_protocol_versions(&[&TLS13]) | ||
.unwrap() | ||
.with_root_certificates(root_cert_store) | ||
.with_single_cert(certs, keys.remove(0)) | ||
.expect("bad certificate/key") | ||
.expect("Config parameters should be valid"); | ||
|
||
if server_name_verification { | ||
builder | ||
.with_root_certificates(root_cert_store) | ||
.with_client_auth_cert(certs, keys.remove(0)) | ||
} else { | ||
builder | ||
.with_custom_certificate_verifier(Arc::new(WebPkiVerifierAnyServerName::new( | ||
root_cert_store, | ||
))) | ||
.with_client_auth_cert(certs, keys.remove(0)) | ||
} | ||
.expect("bad certificate/key") | ||
} else { | ||
ClientConfig::builder() | ||
.with_safe_defaults() | ||
.with_root_certificates(root_cert_store) | ||
.with_no_client_auth() | ||
let builder = ClientConfig::builder().with_safe_defaults(); | ||
if server_name_verification { | ||
builder | ||
.with_root_certificates(root_cert_store) | ||
.with_no_client_auth() | ||
} else { | ||
builder | ||
.with_custom_certificate_verifier(Arc::new(WebPkiVerifierAnyServerName::new( | ||
root_cert_store, | ||
))) | ||
.with_no_client_auth() | ||
} | ||
}; | ||
Ok(TlsClientConfig { client_config: cc }) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use std::time::SystemTime; | ||
use rustls::client::verify_server_cert_signed_by_trust_anchor; | ||
use rustls::server::ParsedCertificate; | ||
use async_rustls::rustls::{client::{ServerCertVerifier, ServerCertVerified}, Certificate, ServerName, RootCertStore}; | ||
|
||
impl ServerCertVerifier for WebPkiVerifierAnyServerName { | ||
/// Will verify the certificate is valid in the following ways: | ||
/// - Signed by a trusted `RootCertStore` CA | ||
/// - Not Expired | ||
fn verify_server_cert( | ||
&self, | ||
end_entity: &Certificate, | ||
intermediates: &[Certificate], | ||
_server_name: &ServerName, | ||
_scts: &mut dyn Iterator<Item = &[u8]>, | ||
_ocsp_response: &[u8], | ||
now: SystemTime, | ||
) -> Result<ServerCertVerified, async_rustls::rustls::Error> { | ||
let cert = ParsedCertificate::try_from(end_entity)?; | ||
verify_server_cert_signed_by_trust_anchor(&cert, &self.roots, intermediates, now)?; | ||
Ok(ServerCertVerified::assertion()) | ||
} | ||
} | ||
|
||
/// `ServerCertVerifier` that verifies that the server is signed by a trusted root, but allows any serverName | ||
/// see the trait impl for more information. | ||
pub struct WebPkiVerifierAnyServerName { | ||
roots: RootCertStore, | ||
} | ||
|
||
#[allow(unreachable_pub)] | ||
impl WebPkiVerifierAnyServerName { | ||
/// Constructs a new `WebPkiVerifierAnyServerName`. | ||
/// | ||
/// `roots` is the set of trust anchors to trust for issuing server certs. | ||
pub fn new(roots: RootCertStore) -> Self { | ||
Self { roots } | ||
} | ||
} |