This vulnerability allows attackers to impersonate a trusted host.
Why is this an issue?
Transport Layer Security (TLS) provides secure communication between systems over the internet by encrypting the data sent between them. In this
process, the role of hostname validation, combined with certificate validation, is to ensure that a system is indeed the one it claims to be, adding
an extra layer of trust and security.
When hostname validation is disabled, the client skips this critical check. This creates an opportunity for attackers to pose as a trusted entity
and intercept, manipulate, or steal the data being transmitted.
To do so, an attacker would obtain a valid certificate authenticating example.com
, serve it using a different hostname, and the
application code would still accept it.
What is the potential impact?
Establishing trust in a secure way is a non-trivial task. When you disable hostname validation, you are removing a key mechanism designed to build
this trust in internet communication, opening your system up to a number of potential threats.
Identity spoofing
If a system does not validate hostnames, it cannot confirm the identity of the other party involved in the communication. An attacker can exploit
this by creating a fake server and masquerading it as a legitimate one. For example, they might set up a server that looks like your bank’s server,
tricking your system into thinking it is communicating with the bank. This scenario, called identity spoofing, allows the attacker to collect any data
your system sends to them, potentially leading to significant data breaches.
How to fix it in Botan
Code examples
The following code contains examples of disabled hostname validation.
The hostname validation gets disabled by overriding tls_verify_cert_chain
with an empty implementation. It is highly recommended to
use the original implementation.
Noncompliant code example
#include <botan/tls_client.h>
#include <botan/tls_callbacks.h>
#include <botan/tls_session_manager.h>
#include <botan/tls_policy.h>
#include <botan/auto_rng.h>
#include <botan/certstor.h>
#include <botan/certstor_system.h>
class Callbacks : public Botan::TLS::Callbacks
{
virtual void tls_verify_cert_chain(
const std::vector<Botan::X509_Certificate> &cert_chain,
const std::vector<std::shared_ptr<const Botan::OCSP::Response>> &ocsp_responses,
const std::vector<Botan::Certificate_Store *> &trusted_roots,
Botan::Usage_Type usage,
const std::string &hostname,
const Botan::TLS::Policy &policy)
override { }
};
class Client_Credentials : public Botan::Credentials_Manager { };
void connect() {
Callbacks callbacks;
Botan::AutoSeeded_RNG rng;
Botan::TLS::Session_Manager_In_Memory session_mgr(rng);
Client_Credentials creds;
Botan::TLS::Strict_Policy policy;
Botan::TLS::Client client(callbacks, session_mgr, creds, policy, rng,
Botan::TLS::Server_Information("example.com", 443),
Botan::TLS::Protocol_Version::TLS_V12); // Noncompliant
}
Compliant solution
#include <botan/tls_client.h>
#include <botan/tls_callbacks.h>
#include <botan/tls_session_manager.h>
#include <botan/tls_policy.h>
#include <botan/auto_rng.h>
#include <botan/certstor.h>
#include <botan/certstor_system.h>
class Callbacks : public Botan::TLS::Callbacks { };
class Client_Credentials : public Botan::Credentials_Manager { };
void connect() {
Callbacks callbacks;
Botan::AutoSeeded_RNG rng;
Botan::TLS::Session_Manager_In_Memory session_mgr(rng);
Client_Credentials creds;
Botan::TLS::Strict_Policy policy;
Botan::TLS::Client client(callbacks, session_mgr, creds, policy, rng,
Botan::TLS::Server_Information("example.com", 443),
Botan::TLS::Protocol_Version::TLS_V12);
}
How does this work?
To fix the vulnerability of disabled hostname validation, it is strongly recommended to first re-enable the default validation and fix the root
cause: the validity of the certificate.
Use valid certificates
If a hostname validation failure prevents connecting to the target server, keep in mind that one system’s code should not work around
another system’s problems, as this creates unnecessary dependencies and can lead to reliability issues.
Therefore, the first solution is to change the remote host’s certificate to match its identity. If the remote host is not under your control,
consider replicating its service to a server whose certificate you can change yourself.
In case the contacted host is located on a development machine, and if there is no other choice, try following this solution:
- Create a self-signed certificate for that machine.
- Add this self-signed certificate to the system’s trust store.
- If the hostname is not
localhost
, add the hostname in the /etc/hosts
file.
How to fix it in cURL
Code examples
The following code contains examples of disabled hostname validation.
The hostname validation gets disabled by setting CURLOPT_SSL_VERIFYHOST
to 0L
. To enable validation set the value to
1L
or do not set CURLOPT_SSL_VERIFYHOST
at all to use the secure default value.
Noncompliant code example
#include <curl/curl.h>
void connect() {
CURL *curl;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // Noncompliant
curl_easy_perform(curl);
}
Compliant solution
#include <curl/curl.h>
void connect() {
CURL *curl;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_perform(curl);
}
How does this work?
To fix the vulnerability of disabled hostname validation, it is strongly recommended to first re-enable the default validation and fix the root
cause: the validity of the certificate.
Use valid certificates
If a hostname validation failure prevents connecting to the target server, keep in mind that one system’s code should not work around
another system’s problems, as this creates unnecessary dependencies and can lead to reliability issues.
Therefore, the first solution is to change the remote host’s certificate to match its identity. If the remote host is not under your control,
consider replicating its service to a server whose certificate you can change yourself.
In case the contacted host is located on a development machine, and if there is no other choice, try following this solution:
- Create a self-signed certificate for that machine.
- Add this self-signed certificate to the system’s trust store.
- If the hostname is not
localhost
, add the hostname in the /etc/hosts
file.
How to fix it in OpenSSL
Code examples
The following code contains examples of disabled hostname validation.
The hostname validation gets disabled because SSL_set1_host
is omitted. To enable validation, set it to the name of the expected
host
.
In case the hostnames are expected not to be prealably known, it is important to still check the hostnames depending on what is presented in their
certificates.
Noncompliant code example
#include <openssl/ssl.h>
void connect() {
const SSL_METHOD *method = TLS_method();
SSL_CTX *ctx = SSL_CTX_new(method);
SSL *ssl = SSL_new(ctx);
SSL_connect(ssl); // Noncompliant
}
Compliant solution
#include <openssl/ssl.h>
void connect() {
const SSL_METHOD *method = TLS_method();
SSL_CTX *ctx = SSL_CTX_new(method);
SSL *ssl = SSL_new(ctx);
SSL_set1_host(ssl, "smtp.example.com");
SSL_connect(ssl);
}
How does this work?
To fix the vulnerability of disabled hostname validation, it is strongly recommended to first re-enable the default validation and fix the root
cause: the validity of the certificate.
Use valid certificates
If a hostname validation failure prevents connecting to the target server, keep in mind that one system’s code should not work around
another system’s problems, as this creates unnecessary dependencies and can lead to reliability issues.
Therefore, the first solution is to change the remote host’s certificate to match its identity. If the remote host is not under your control,
consider replicating its service to a server whose certificate you can change yourself.
In case the contacted host is located on a development machine, and if there is no other choice, try following this solution:
- Create a self-signed certificate for that machine.
- Add this self-signed certificate to the system’s trust store.
- If the hostname is not
localhost
, add the hostname in the /etc/hosts
file.
Resources
Documentation
Standards