-
Notifications
You must be signed in to change notification settings - Fork 273
feat(tls): server certificate SAN can have a trailing dot #1405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
levkk
wants to merge
1
commit into
main
Choose a base branch
from
levkk-trailing-dot-san
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+119
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -355,10 +355,19 @@ fn build_connector(config_key: &ConnectorConfigKey) -> Result<Arc<ClientConfig>, | |
|
|
||
| config | ||
| } | ||
| TlsVerifyMode::VerifyFull => build_client_config( | ||
| ClientConfig::builder().with_root_certificates(roots), | ||
| client_auth, | ||
| )?, | ||
| TlsVerifyMode::VerifyFull => { | ||
| let verifier = TrailingDotVerifier::new(roots.clone())?; | ||
| let mut config = build_client_config( | ||
| ClientConfig::builder().with_root_certificates(roots), | ||
| client_auth, | ||
| )?; | ||
|
|
||
| config | ||
| .dangerous() | ||
| .set_certificate_verifier(Arc::new(verifier)); | ||
|
|
||
| config | ||
| } | ||
| }; | ||
|
|
||
| increment_connector_build_count(); | ||
|
|
@@ -485,6 +494,109 @@ pub fn connector_with_verify_mode( | |
| Ok(connector) | ||
| } | ||
|
|
||
| /// Certificate verifier that treats one trailing dot in a DNS SAN as optional. | ||
| #[derive(Debug)] | ||
| struct TrailingDotVerifier { | ||
| webpki_verifier: Arc<dyn ServerCertVerifier>, | ||
| } | ||
|
|
||
| impl TrailingDotVerifier { | ||
| fn new(roots: rustls::RootCertStore) -> Result<Self, Error> { | ||
| let webpki_verifier = rustls::client::WebPkiServerVerifier::builder(roots.into()) | ||
| .build() | ||
| .map_err(|e| { | ||
| invalid_data(format!("failed to build server certificate verifier: {e}")) | ||
| })?; | ||
|
|
||
| Ok(Self { webpki_verifier }) | ||
| } | ||
| } | ||
|
|
||
| impl ServerCertVerifier for TrailingDotVerifier { | ||
| fn verify_server_cert( | ||
| &self, | ||
| end_entity: &CertificateDer<'_>, | ||
| intermediates: &[CertificateDer<'_>], | ||
| server_name: &rustls::pki_types::ServerName<'_>, | ||
| ocsp_response: &[u8], | ||
| now: rustls::pki_types::UnixTime, | ||
| ) -> Result<ServerCertVerified, rustls::Error> { | ||
| let result = self.webpki_verifier.verify_server_cert( | ||
| end_entity, | ||
| intermediates, | ||
| server_name, | ||
| ocsp_response, | ||
| now, | ||
| ); | ||
|
|
||
| if matches!( | ||
| &result, | ||
| Err(rustls::Error::InvalidCertificate( | ||
| rustls::CertificateError::NotValidForNameContext { .. } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this make sense as It sounds like rustls considers it okay to change enum variants, so this might help avoid a silent behavior change if I'm reading this right. |
||
| )) | ||
| ) && matches_dns_san_with_trailing_dot(end_entity, server_name) | ||
| { | ||
| debug!("certificate hostname verification succeeded after normalizing a trailing dot"); | ||
| return Ok(ServerCertVerified::assertion()); | ||
| } | ||
|
|
||
| result | ||
| } | ||
|
|
||
| fn verify_tls12_signature( | ||
| &self, | ||
| message: &[u8], | ||
| cert: &CertificateDer<'_>, | ||
| dss: &rustls::DigitallySignedStruct, | ||
| ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> { | ||
| self.webpki_verifier | ||
| .verify_tls12_signature(message, cert, dss) | ||
| } | ||
|
|
||
| fn verify_tls13_signature( | ||
| &self, | ||
| message: &[u8], | ||
| cert: &CertificateDer<'_>, | ||
| dss: &rustls::DigitallySignedStruct, | ||
| ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> { | ||
| self.webpki_verifier | ||
| .verify_tls13_signature(message, cert, dss) | ||
| } | ||
|
|
||
| fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> { | ||
| self.webpki_verifier.supported_verify_schemes() | ||
| } | ||
| } | ||
|
|
||
| fn matches_dns_san_with_trailing_dot( | ||
| end_entity: &CertificateDer<'_>, | ||
| server_name: &rustls::pki_types::ServerName<'_>, | ||
| ) -> bool { | ||
| use x509_parser::{certificate::X509Certificate, extensions::GeneralName}; | ||
|
|
||
| let rustls::pki_types::ServerName::DnsName(expected_name) = server_name else { | ||
| return false; | ||
| }; | ||
| let Ok((_, cert)) = X509Certificate::from_der(end_entity.as_ref()) else { | ||
| return false; | ||
| }; | ||
| let Ok(Some(san)) = cert.subject_alternative_name() else { | ||
| return false; | ||
| }; | ||
|
|
||
| san.value.general_names.iter().any(|name| { | ||
| let GeneralName::DNSName(presented_name) = name else { | ||
| return false; | ||
| }; | ||
| let Some(normalized_name) = presented_name.strip_suffix('.') else { | ||
| return false; | ||
| }; | ||
|
|
||
| !normalized_name.ends_with('.') | ||
| && normalized_name.eq_ignore_ascii_case(expected_name.as_ref()) | ||
| }) | ||
| } | ||
|
|
||
| /// Certificate verifier that validates certificates but skips hostname verification | ||
| #[derive(Debug)] | ||
| struct NoHostnameVerifier { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not familiar with the rustls API but this seems like a lot of code for a trailing dot. (And it wouldn't surprise me at all that the TLS library API actually makes it this hard)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah... I'm not sure if this is how this is supposed to be done. I'm waiting for a review from Ryan.