Skip to contentSkip to conversation

client: Add boolean config option "relax_sni" under "[authenticate.client]".

PATCH In review Ken Milmore 1 participant 1 message Jun 8, 2025 Handshake upcall for transport layer security

lore
Share & tools
lore
#0 lore raw

PATCH 7/7 client: Add boolean config option "relax_sni" under "[authenticate.client]".

If this option is set to true, then the client will avoid using Server Name Indication (SNI) in cases where a numeric IPv4 or IPv6 address is being used to connect to the server. This prevents failure of certificate exchange with the server in such cases.

---
 src/tlshd/client.c | 76 +++++++++++++++++++++++++++++++++++++++++-----
 src/tlshd/config.c |  3 ++
 src/tlshd/tlshd.h  |  1 +
 3 files changed, 72 insertions(+), 8 deletions(-)
src/tlshd/client.c +68 -8
index 80107b2..90cce6a 100644
@@ -32,6 +32,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <keyutils.h>
+#include <netdb.h>
 
 #include <gnutls/gnutls.h>
 #include <gnutls/abstract.h>
@@ -43,6 +44,62 @@
 #include "tlshd.h"
 #include "netlink.h"
 
+bool tlshd_client_relax_sni = false;
+
+/**
+ * tlshd_is_hostname_numeric - Determine if the given string represents a
+ * valid numerical IPv4 or IPv6 address rather than an actual host name.
+ *
+ * Return values:
+ * %true: @hostname corresponds to a numerical IPv4 or IPv6 address.
+ * %false: @hostname is not a valid numerical address, or an error occurred.
+ */
+static bool tlshd_is_hostname_numeric(const char *hostname)
+{
+	struct addrinfo hints = { 0 };
+	struct addrinfo *res;
+	int err;
+
+	/* An empty host name is not a numeric address. */
+	if (hostname[0] == '\0')
+		return false;
+
+	hints.ai_flags = AI_NUMERICHOST;
+	hints.ai_socktype = SOCK_RAW; /* To avoid duplicate results. */
+
+	/* Try to convert peername into a textualized IP/IPv6 address. */
+	err = getaddrinfo(hostname, NULL, &hints, &res);
+	if (err) {
+		tlshd_log_gai_error(err);
+		return false;
+	}
+
+	freeaddrinfo(res);
+	return true;
+}
+
+/**
+ * tlshd_client_is_sni_required - Determine if we want GnuTLS to send the peer
+ * name to the server for purposes of Server Name Indication (SNI).
+ *
+ * Return values:
+ * %true: Send peer name to server.
+ * %false: Do not send peer name to server.
+ */
+static bool tlshd_client_is_sni_required(struct tlshd_handshake_parms *parms)
+{
+	bool sni_required = true;
+
+	if (tlshd_client_relax_sni && tlshd_is_hostname_numeric(parms->peername)) {
+		tlshd_log_debug("client: Not using SNI for numeric peer name '%s'",
+				parms->peername);
+		sni_required = false;
+	}
+
+	return sni_required;
+}
+
+
 static void tlshd_tls13_client_anon_handshake(struct tlshd_handshake_parms *parms)
 {
 	gnutls_certificate_credentials_t xcred;
@@ -85,8 +142,9 @@ static void tlshd_tls13_client_anon_handshake(struct tlshd_handshake_parms *parm
 	}
 	gnutls_transport_set_int(session, parms->sockfd);
 
-	gnutls_server_name_set(session, GNUTLS_NAME_DNS,
-			       parms->peername, strlen(parms->peername));
+	if (tlshd_client_is_sni_required(parms))
+		gnutls_server_name_set(session, GNUTLS_NAME_DNS,
+				       parms->peername, strlen(parms->peername));
 
 	gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
 
@@ -311,11 +369,13 @@ static void tlshd_tls13_client_x509_handshake(struct tlshd_handshake_parms *parm
 	gnutls_transport_set_int(session, parms->sockfd);
 	gnutls_session_set_ptr(session, parms);
 
-	ret = gnutls_server_name_set(session, GNUTLS_NAME_DNS,
-				     parms->peername, strlen(parms->peername));
-	if (ret != GNUTLS_E_SUCCESS) {
-		tlshd_log_gnutls_error(ret);
-		goto out_free_creds;
+	if (tlshd_client_is_sni_required(parms)) {
+		ret = gnutls_server_name_set(session, GNUTLS_NAME_DNS,
+					     parms->peername, strlen(parms->peername));
+		if (ret != GNUTLS_E_SUCCESS) {
+			tlshd_log_gnutls_error(ret);
+			goto out_free_creds;
+		}
 	}
 	ret = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, xcred);
 	if (ret != GNUTLS_E_SUCCESS) {
@@ -533,7 +593,7 @@ static int tlshd_quic_client_set_x509_session(struct tlshd_quic_conn *conn)
 	ret = gnutls_credentials_set(session, GNUTLS_CRD_CERTIFICATE, cred);
 	if (ret)
 		goto err_session;
-	if (parms->peername) {
+	if (parms->peername && tlshd_client_is_sni_required(parms)) {
 		ret = gnutls_server_name_set(session, GNUTLS_NAME_DNS,
 					     parms->peername, strlen(parms->peername));
 		if (ret)
src/tlshd/config.c +3 -0
index 9599e1b..b912614 100644
@@ -92,6 +92,9 @@ bool tlshd_config_init(const gchar *pathname)
 				     "delay_done", NULL);
 	tlshd_delay_done = tmp > 0 ? (unsigned int)tmp : 0;
 
+	tlshd_client_relax_sni = g_key_file_get_string(tlshd_configuration,
+					"authenticate.client", "relax_sni", NULL);
+
 	tlshd_server_verify_peername = g_key_file_get_string(tlshd_configuration,
 					"authenticate.server", "verify_peername", NULL);
 	tlshd_server_verify_peeraddr = g_key_file_get_string(tlshd_configuration,
src/tlshd/tlshd.h +1 -0
index d0602bf..3ed2567 100644
@@ -24,6 +24,7 @@ extern int tlshd_debug;
 extern int tlshd_tls_debug;
 extern unsigned int tlshd_delay_done;
 extern int tlshd_stderr;
+extern bool tlshd_client_relax_sni;
 extern bool tlshd_server_verify_peername;
 extern bool tlshd_server_verify_peeraddr;
 
Signature · 1 line
2.47.2