Blame SOURCES/elinks-0.12pre6-ssl-hostname.patch

06c6ff
From 30d96f81dbefffd3f1523256cc5a5328ea1c7ecb Mon Sep 17 00:00:00 2001
06c6ff
From: Kalle Olavi Niemitalo <kon@iki.fi>
06c6ff
Date: Mon, 2 May 2011 14:41:40 +0300
06c6ff
Subject: [PATCH 1/4] 1024: Use RFC 3546 server_name TLS extension
06c6ff
06c6ff
For both GnuTLS and OpenSSL.  Not tested with nss-compat-openssl.
06c6ff
06c6ff
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
06c6ff
---
06c6ff
 src/network/ssl/socket.c | 19 ++++++++++++++++++-
06c6ff
 src/network/ssl/ssl.c    | 29 ++++++++++++++++++++++++-----
06c6ff
 src/network/ssl/ssl.h    | 14 ++++++++++++--
06c6ff
 3 files changed, 54 insertions(+), 8 deletions(-)
06c6ff
06c6ff
diff --git a/src/network/ssl/socket.c b/src/network/ssl/socket.c
06c6ff
index 45b4b4a..dc682d0 100644
06c6ff
--- a/src/network/ssl/socket.c
06c6ff
+++ b/src/network/ssl/socket.c
06c6ff
@@ -22,6 +22,7 @@
06c6ff
 #include "network/socket.h"
06c6ff
 #include "network/ssl/socket.h"
06c6ff
 #include "network/ssl/ssl.h"
06c6ff
+#include "protocol/uri.h"
06c6ff
 #include "util/memory.h"
06c6ff
 
06c6ff
 
06c6ff
@@ -117,12 +118,28 @@ int
06c6ff
 ssl_connect(struct socket *socket)
06c6ff
 {
06c6ff
 	int ret;
06c6ff
+	unsigned char *server_name;
06c6ff
+	struct connection *conn = socket->conn;
06c6ff
 
06c6ff
-	if (init_ssl_connection(socket) == S_SSL_ERROR) {
06c6ff
+	/* TODO: Recode server_name to UTF-8.  */
06c6ff
+	server_name = get_uri_string(conn->proxied_uri, URI_HOST);
06c6ff
+	if (!server_name) {
06c6ff
+		socket->ops->done(socket, connection_state(S_OUT_OF_MEM));
06c6ff
+		return -1;
06c6ff
+	}
06c6ff
+
06c6ff
+	/* RFC 3546 says literal IPv4 and IPv6 addresses are not allowed.  */
06c6ff
+	if (is_ip_address(server_name, strlen(server_name)))
06c6ff
+		mem_free_set(&server_name, NULL);
06c6ff
+
06c6ff
+	if (init_ssl_connection(socket, server_name) == S_SSL_ERROR) {
06c6ff
+		mem_free_if(server_name);
06c6ff
 		socket->ops->done(socket, connection_state(S_SSL_ERROR));
06c6ff
 		return -1;
06c6ff
 	}
06c6ff
 
06c6ff
+	mem_free_if(server_name);
06c6ff
+
06c6ff
 	if (socket->no_tls)
06c6ff
 		ssl_set_no_tls(socket);
06c6ff
 
06c6ff
diff --git a/src/network/ssl/ssl.c b/src/network/ssl/ssl.c
06c6ff
index 685c31e..7767a71 100644
06c6ff
--- a/src/network/ssl/ssl.c
06c6ff
+++ b/src/network/ssl/ssl.c
06c6ff
@@ -212,13 +212,26 @@ struct module ssl_module = struct_module(
06c6ff
 );
06c6ff
 
06c6ff
 int
06c6ff
-init_ssl_connection(struct socket *socket)
06c6ff
+init_ssl_connection(struct socket *socket,
06c6ff
+		    const unsigned char *server_name)
06c6ff
 {
06c6ff
 #ifdef CONFIG_OPENSSL
06c6ff
 	socket->ssl = SSL_new(context);
06c6ff
 	if (!socket->ssl) return S_SSL_ERROR;
06c6ff
+
06c6ff
+	/* If the server name is known, pass it to OpenSSL.
06c6ff
+	 *
06c6ff
+	 * The return value of SSL_set_tlsext_host_name is not
06c6ff
+	 * documented.  The source shows that it returns 1 if
06c6ff
+	 * successful; on error, it calls SSLerr and returns 0.  */
06c6ff
+	if (server_name
06c6ff
+	    && !SSL_set_tlsext_host_name(socket->ssl, server_name)) {
06c6ff
+		SSL_free(socket->ssl);
06c6ff
+		socket->ssl = NULL;
06c6ff
+		return S_SSL_ERROR;
06c6ff
+	}
06c6ff
+
06c6ff
 #elif defined(CONFIG_GNUTLS)
06c6ff
-	/* const unsigned char server_name[] = "localhost"; */
06c6ff
 	ssl_t *state = mem_alloc(sizeof(ssl_t));
06c6ff
 
06c6ff
 	if (!state) return S_SSL_ERROR;
06c6ff
@@ -255,9 +268,15 @@ init_ssl_connection(struct socket *socket)
06c6ff
 	/* gnutls_handshake_set_private_extensions(*state, 1); */
06c6ff
 	gnutls_cipher_set_priority(*state, cipher_priority);
06c6ff
 	gnutls_kx_set_priority(*state, kx_priority);
06c6ff
-	/* gnutls_certificate_type_set_priority(*state, cert_type_priority);
06c6ff
-	gnutls_server_name_set(*state, GNUTLS_NAME_DNS, server_name,
06c6ff
-			       sizeof(server_name) - 1); */
06c6ff
+	/* gnutls_certificate_type_set_priority(*state, cert_type_priority); */
06c6ff
+
06c6ff
+	if (server_name
06c6ff
+	    && gnutls_server_name_set(*state, GNUTLS_NAME_DNS, server_name,
06c6ff
+				      strlen(server_name))) {
06c6ff
+		gnutls_deinit(*state);
06c6ff
+		mem_free(state);
06c6ff
+		return S_SSL_ERROR;
06c6ff
+	}
06c6ff
 
06c6ff
 	socket->ssl = state;
06c6ff
 #endif
06c6ff
diff --git a/src/network/ssl/ssl.h b/src/network/ssl/ssl.h
06c6ff
index 7c54a7a..bfd94e1 100644
06c6ff
--- a/src/network/ssl/ssl.h
06c6ff
+++ b/src/network/ssl/ssl.h
06c6ff
@@ -11,8 +11,18 @@ struct socket;
06c6ff
 extern struct module ssl_module;
06c6ff
 
06c6ff
 /* Initializes the SSL connection data. Returns S_OK on success and S_SSL_ERROR
06c6ff
- * on failure. */
06c6ff
-int init_ssl_connection(struct socket *socket);
06c6ff
+ * on failure.
06c6ff
+ *
06c6ff
+ * server_name is the DNS name of the server (in UTF-8), or NULL if
06c6ff
+ * ELinks knows only the IP address.  ELinks reports that name to the
06c6ff
+ * server so that the server can choose the correct certificate if it
06c6ff
+ * has multiple virtual hosts on the same IP address.  See RFC 3546
06c6ff
+ * section 3.1.
06c6ff
+ *
06c6ff
+ * server_name does not affect how ELinks verifies the certificate
06c6ff
+ * after the server has returned it.  */
06c6ff
+int init_ssl_connection(struct socket *socket,
06c6ff
+			const unsigned char *server_name);
06c6ff
 
06c6ff
 /* Releases the SSL connection data */
06c6ff
 void done_ssl_connection(struct socket *socket);
06c6ff
-- 
06c6ff
2.1.0
06c6ff
06c6ff
06c6ff
From e7484a980572b665747c28aa1376e29a12fb4b19 Mon Sep 17 00:00:00 2001
06c6ff
From: Kalle Olavi Niemitalo <kon@iki.fi>
06c6ff
Date: Tue, 3 May 2011 03:52:21 +0300
06c6ff
Subject: [PATCH 2/4] 1024: Verify server certificate hostname with OpenSSL
06c6ff
06c6ff
Not tested with nss-compat-ossl.
06c6ff
06c6ff
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
06c6ff
---
06c6ff
 src/network/ssl/Makefile                   |   7 +-
06c6ff
 src/network/ssl/match-hostname.c           | 125 +++++++++++++++++
06c6ff
 src/network/ssl/match-hostname.h           |  10 ++
06c6ff
 src/network/ssl/socket.c                   | 211 ++++++++++++++++++++++++++++-
06c6ff
 src/network/ssl/ssl.c                      |  41 +++++-
06c6ff
 src/network/ssl/ssl.h                      |   3 +
06c6ff
 src/network/ssl/test/Makefile              |   9 ++
06c6ff
 src/network/ssl/test/match-hostname-test.c | 123 +++++++++++++++++
06c6ff
 src/network/ssl/test/test-match-hostname   |   3 +
06c6ff
 9 files changed, 529 insertions(+), 3 deletions(-)
06c6ff
 create mode 100644 src/network/ssl/match-hostname.c
06c6ff
 create mode 100644 src/network/ssl/match-hostname.h
06c6ff
 create mode 100644 src/network/ssl/test/Makefile
06c6ff
 create mode 100644 src/network/ssl/test/match-hostname-test.c
06c6ff
 create mode 100755 src/network/ssl/test/test-match-hostname
06c6ff
06c6ff
diff --git a/src/network/ssl/Makefile b/src/network/ssl/Makefile
06c6ff
index 26f02c2..6f265da 100644
06c6ff
--- a/src/network/ssl/Makefile
06c6ff
+++ b/src/network/ssl/Makefile
06c6ff
@@ -3,6 +3,11 @@ include $(top_builddir)/Makefile.config
06c6ff
 
06c6ff
 INCLUDES += $(GNUTLS_CFLAGS) $(OPENSSL_CFLAGS)
06c6ff
 
06c6ff
-OBJS = ssl.o socket.o
06c6ff
+SUBDIRS = test
06c6ff
+
06c6ff
+# ELinks uses match-hostname.o only if CONFIG_OPENSSL.
06c6ff
+# However, match-hostname.o has test cases that always need it.
06c6ff
+# The test framework doesn't seem to support conditional tests.
06c6ff
+OBJS = match-hostname.o ssl.o socket.o
06c6ff
 
06c6ff
 include $(top_srcdir)/Makefile.lib
06c6ff
diff --git a/src/network/ssl/match-hostname.c b/src/network/ssl/match-hostname.c
06c6ff
new file mode 100644
06c6ff
index 0000000..9a64bb4
06c6ff
--- /dev/null
06c6ff
+++ b/src/network/ssl/match-hostname.c
06c6ff
@@ -0,0 +1,125 @@
06c6ff
+/* Matching a host name to wildcards in SSL certificates */
06c6ff
+
06c6ff
+#ifdef HAVE_CONFIG_H
06c6ff
+#include "config.h"
06c6ff
+#endif
06c6ff
+
06c6ff
+#include "elinks.h"
06c6ff
+
06c6ff
+#include "intl/charsets.h"
06c6ff
+#include "network/ssl/match-hostname.h"
06c6ff
+#include "util/conv.h"
06c6ff
+#include "util/error.h"
06c6ff
+#include "util/string.h"
06c6ff
+
06c6ff
+/** Checks whether a host name matches a pattern that may contain
06c6ff
+ * wildcards.
06c6ff
+ *
06c6ff
+ * @param[in] hostname
06c6ff
+ *   The host name to which the user wanted to connect.
06c6ff
+ *   Should be in UTF-8 and need not be null-terminated.
06c6ff
+ * @param[in] hostname_length
06c6ff
+ *   The length of @a hostname, in bytes.
06c6ff
+ * @param[in] pattern
06c6ff
+ *   A pattern that the host name might match.
06c6ff
+ *   Should be in UTF-8 and need not be null-terminated.
06c6ff
+ *   The pattern may contain wildcards, as specified in
06c6ff
+ *   RFC 2818 section 3.1.
06c6ff
+ * @param[in] pattern_length
06c6ff
+ *   The length of @a pattern, in bytes.
06c6ff
+ *
06c6ff
+ * @return
06c6ff
+ *   Nonzero if the host name matches.  Zero if it doesn't.
06c6ff
+ *
06c6ff
+ * According to RFC 2818 section 3.1, '*' matches any number of
06c6ff
+ * characters except '.'.  For example, "*r*.example.org" matches
06c6ff
+ * "random.example.org" or "history.example.org" but not
06c6ff
+ * "frozen.fruit.example.org".
06c6ff
+ *
06c6ff
+ * This function does not allocate memory, and consumes at most
06c6ff
+ * O(@a hostname_length * @a pattern_length) time.  */
06c6ff
+int
06c6ff
+match_hostname_pattern(const unsigned char *hostname,
06c6ff
+		       size_t hostname_length,
06c6ff
+		       const unsigned char *pattern,
06c6ff
+		       size_t pattern_length)
06c6ff
+{
06c6ff
+	const unsigned char *const hostname_end = hostname + hostname_length;
06c6ff
+	const unsigned char *const pattern_end = pattern + pattern_length;
06c6ff
+
06c6ff
+	assert(hostname <= hostname_end);
06c6ff
+	assert(pattern <= pattern_end);
06c6ff
+	if_assert_failed return 0;
06c6ff
+
06c6ff
+	while (pattern < pattern_end) {
06c6ff
+		if (*pattern == '*') {
06c6ff
+			const unsigned char *next_wildcard;
06c6ff
+			size_t literal_length;
06c6ff
+
06c6ff
+			++pattern;
06c6ff
+			next_wildcard = memchr(pattern, '*',
06c6ff
+					       pattern_end - pattern);
06c6ff
+			if (next_wildcard == NULL)
06c6ff
+				literal_length = pattern_end - pattern;
06c6ff
+			else
06c6ff
+				literal_length = next_wildcard - pattern;
06c6ff
+
06c6ff
+			for (;;) {
06c6ff
+				size_t hostname_left = hostname_end - hostname;
06c6ff
+				unicode_val_T uni;
06c6ff
+
06c6ff
+				if (hostname_left < literal_length)
06c6ff
+					return 0;
06c6ff
+
06c6ff
+				/* If next_wildcard == NULL, then the
06c6ff
+				 * literal string is at the end of the
06c6ff
+				 * pattern, so anchor the match to the
06c6ff
+				 * end of the hostname.  The end of
06c6ff
+				 * this function can then easily
06c6ff
+				 * verify that the whole hostname was
06c6ff
+				 * matched.
06c6ff
+				 *
06c6ff
+				 * But do not jump directly there;
06c6ff
+				 * first verify that there are no '.'
06c6ff
+				 * characters in between.  */
06c6ff
+				if ((next_wildcard != NULL
06c6ff
+				     || hostname_left == literal_length)
06c6ff
+				    && !c_strlcasecmp(pattern, literal_length,
06c6ff
+						      hostname, literal_length))
06c6ff
+					break;
06c6ff
+
06c6ff
+				/* The literal string doesn't match here.
06c6ff
+				 * Skip one character of the hostname and
06c6ff
+				 * retry.  If the skipped character is '.'
06c6ff
+				 * or one of the equivalent characters
06c6ff
+				 * listed in RFC 3490 section 3.1
06c6ff
+				 * requirement 1, then return 0, because
06c6ff
+				 * '*' must not match such characters.
06c6ff
+				 * Do the same if invalid UTF-8 is found.
06c6ff
+				 * Cast away const.  */
06c6ff
+				uni = utf8_to_unicode((unsigned char **) hostname,
06c6ff
+						      hostname_end);
06c6ff
+				if (uni == 0x002E
06c6ff
+				    || uni == 0x3002
06c6ff
+				    || uni == 0xFF0E
06c6ff
+				    || uni == 0xFF61
06c6ff
+				    || uni == UCS_NO_CHAR)
06c6ff
+					return 0;
06c6ff
+			}
06c6ff
+
06c6ff
+			pattern += literal_length;
06c6ff
+			hostname += literal_length;
06c6ff
+		} else {
06c6ff
+			if (hostname == hostname_end)
06c6ff
+				return 0;
06c6ff
+
06c6ff
+			if (c_toupper(*pattern) != c_toupper(*hostname))
06c6ff
+				return 0;
06c6ff
+
06c6ff
+			++pattern;
06c6ff
+			++hostname;
06c6ff
+		}
06c6ff
+	}
06c6ff
+
06c6ff
+	return hostname == hostname_end;
06c6ff
+}
06c6ff
diff --git a/src/network/ssl/match-hostname.h b/src/network/ssl/match-hostname.h
06c6ff
new file mode 100644
06c6ff
index 0000000..60d32b2
06c6ff
--- /dev/null
06c6ff
+++ b/src/network/ssl/match-hostname.h
06c6ff
@@ -0,0 +1,10 @@
06c6ff
+
06c6ff
+#ifndef EL__NETWORK_SSL_MATCH_HOSTNAME_H
06c6ff
+#define EL__NETWORK_SSL_MATCH_HOSTNAME_H
06c6ff
+
06c6ff
+int match_hostname_pattern(const unsigned char *hostname,
06c6ff
+			   size_t hostname_length,
06c6ff
+			   const unsigned char *pattern,
06c6ff
+			   size_t pattern_length);
06c6ff
+
06c6ff
+#endif
06c6ff
diff --git a/src/network/ssl/socket.c b/src/network/ssl/socket.c
06c6ff
index dc682d0..a67bbde 100644
06c6ff
--- a/src/network/ssl/socket.c
06c6ff
+++ b/src/network/ssl/socket.c
06c6ff
@@ -6,13 +6,24 @@
06c6ff
 
06c6ff
 #ifdef CONFIG_OPENSSL
06c6ff
 #include <openssl/ssl.h>
06c6ff
+#include <openssl/x509v3.h>
06c6ff
+#define USE_OPENSSL
06c6ff
+#elif defined(CONFIG_NSS_COMPAT_OSSL)
06c6ff
+#include <nss_compat_ossl/nss_compat_ossl.h>
06c6ff
+#define USE_OPENSSL
06c6ff
 #elif defined(CONFIG_GNUTLS)
06c6ff
 #include <gnutls/gnutls.h>
06c6ff
 #else
06c6ff
 #error "Huh?! You have SSL enabled, but not OPENSSL nor GNUTLS!! And then you want exactly *what* from me?"
06c6ff
 #endif
06c6ff
 
06c6ff
+#ifdef HAVE_ARPA_INET_H
06c6ff
+#include <arpa/inet.h>
06c6ff
+#endif
06c6ff
 #include <errno.h>
06c6ff
+#ifdef HAVE_NETINET_IN_H
06c6ff
+#include <netinet/in.h>
06c6ff
+#endif
06c6ff
 
06c6ff
 #include "elinks.h"
06c6ff
 
06c6ff
@@ -20,6 +31,7 @@
06c6ff
 #include "main/select.h"
06c6ff
 #include "network/connection.h"
06c6ff
 #include "network/socket.h"
06c6ff
+#include "network/ssl/match-hostname.h"
06c6ff
 #include "network/ssl/socket.h"
06c6ff
 #include "network/ssl/ssl.h"
06c6ff
 #include "protocol/uri.h"
06c6ff
@@ -83,6 +95,203 @@ ssl_set_no_tls(struct socket *socket)
06c6ff
 #endif
06c6ff
 }
06c6ff
 
06c6ff
+#ifdef USE_OPENSSL
06c6ff
+
06c6ff
+/** Checks whether the host component of a URI matches a host name in
06c6ff
+ * the server certificate.
06c6ff
+ *
06c6ff
+ * @param[in] uri_host
06c6ff
+ *   The host name (or IP address) to which the user wanted to connect.
06c6ff
+ *   Should be in UTF-8.
06c6ff
+ * @param[in] cert_host_asn1
06c6ff
+ *   A host name found in the server certificate: either as commonName
06c6ff
+ *   in the subject field, or as a dNSName in the subjectAltName
06c6ff
+ *   extension.  This may contain wildcards, as specified in RFC 2818
06c6ff
+ *   section 3.1.
06c6ff
+ *
06c6ff
+ * @return
06c6ff
+ *   Nonzero if the host matches.  Zero if it doesn't, or on error.
06c6ff
+ *
06c6ff
+ * If @a uri_host is an IP address literal rather than a host name,
06c6ff
+ * then this function returns 0, meaning that the host name does not match.
06c6ff
+ * According to RFC 2818, if the certificate is intended to match an
06c6ff
+ * IP address, then it must have that IP address as an iPAddress
06c6ff
+ * SubjectAltName, rather than in commonName.  For comparing those,
06c6ff
+ * match_uri_host_ip() must be used instead of this function.  */
06c6ff
+static int
06c6ff
+match_uri_host_name(const unsigned char *uri_host,
06c6ff
+		    ASN1_STRING *cert_host_asn1)
06c6ff
+{
06c6ff
+	const size_t uri_host_len = strlen(uri_host);
06c6ff
+	unsigned char *cert_host = NULL;
06c6ff
+	int cert_host_len;
06c6ff
+	int matched = 0;
06c6ff
+
06c6ff
+	if (is_ip_address(uri_host, uri_host_len))
06c6ff
+		goto mismatch;
06c6ff
+
06c6ff
+	/* This function is used for both dNSName and commonName.
06c6ff
+	 * Although dNSName is always an IA5 string, commonName allows
06c6ff
+	 * many other encodings too.  Normalize all to UTF-8.  */
06c6ff
+	cert_host_len = ASN1_STRING_to_UTF8(&cert_host,
06c6ff
+					    cert_host_asn1);
06c6ff
+	if (cert_host_len < 0)
06c6ff
+		goto mismatch;
06c6ff
+
06c6ff
+	matched = match_hostname_pattern(uri_host, uri_host_len,
06c6ff
+					 cert_host, cert_host_len);
06c6ff
+
06c6ff
+mismatch:
06c6ff
+	if (cert_host)
06c6ff
+		OPENSSL_free(cert_host);
06c6ff
+	return matched;
06c6ff
+}
06c6ff
+
06c6ff
+/** Checks whether the host component of a URI matches an IP address
06c6ff
+ * in the server certificate.
06c6ff
+ *
06c6ff
+ * @param[in] uri_host
06c6ff
+ *   The IP address (or host name) to which the user wanted to connect.
06c6ff
+ *   Should be in UTF-8.
06c6ff
+ * @param[in] cert_host_asn1
06c6ff
+ *   An IP address found as iPAddress in the subjectAltName extension
06c6ff
+ *   of the server certificate.  According to RFC 5280 section 4.2.1.6,
06c6ff
+ *   that is an octet string in network byte order.  According to
06c6ff
+ *   RFC 2818 section 3.1, wildcards are not allowed.
06c6ff
+ *
06c6ff
+ * @return
06c6ff
+ *   Nonzero if the host matches.  Zero if it doesn't, or on error.
06c6ff
+ *
06c6ff
+ * If @a uri_host is a host name rather than an IP address literal,
06c6ff
+ * then this function returns 0, meaning that the address does not match.
06c6ff
+ * This function does not try to resolve the host name to an IP address
06c6ff
+ * and compare that to @a cert_host_asn1, because such an approach would
06c6ff
+ * be vulnerable to DNS spoofing.
06c6ff
+ *
06c6ff
+ * This function does not support the address-and-netmask format used
06c6ff
+ * in the name constraints extension of a CA certificate (RFC 5280
06c6ff
+ * section 4.2.1.10).  */
06c6ff
+static int
06c6ff
+match_uri_host_ip(const unsigned char *uri_host,
06c6ff
+		  ASN1_OCTET_STRING *cert_host_asn1)
06c6ff
+{
06c6ff
+	const unsigned char *cert_host_addr = ASN1_STRING_data(cert_host_asn1);
06c6ff
+	struct in_addr uri_host_in;
06c6ff
+#ifdef CONFIG_IPV6
06c6ff
+	struct in6_addr uri_host_in6;
06c6ff
+#endif
06c6ff
+
06c6ff
+	/* RFC 5280 defines the iPAddress alternative of GeneralName
06c6ff
+	 * as an OCTET STRING.  Verify that the type is indeed that.
06c6ff
+	 * This is an assertion because, if someone puts the wrong
06c6ff
+	 * type of data there, then it will not even be recognized as
06c6ff
+	 * an iPAddress, and this function will not be called.
06c6ff
+	 *
06c6ff
+	 * (Because GeneralName is defined in an implicitly tagged
06c6ff
+	 * ASN.1 module, the OCTET STRING tag is not part of the DER
06c6ff
+	 * encoding.  BER also allows a constructed encoding where
06c6ff
+	 * each substring begins with the OCTET STRING tag; but ITU-T
06c6ff
+	 * Rec. X.690 (07/2002) subclause 8.21 says those would be
06c6ff
+	 * OCTET STRING even if the outer string were of some other
06c6ff
+	 * type.  "A Layman's Guide to a Subset of ASN.1, BER, and
06c6ff
+	 * DER" (Kaliski, 1993) claims otherwise, though.)  */
06c6ff
+	assert(ASN1_STRING_type(cert_host_asn1) == V_ASN1_OCTET_STRING);
06c6ff
+	if_assert_failed return 0;
06c6ff
+
06c6ff
+	/* cert_host_addr, url_host_in, and url_host_in6 are all in
06c6ff
+	 * network byte order.  */
06c6ff
+	switch (ASN1_STRING_length(cert_host_asn1)) {
06c6ff
+	case 4:
06c6ff
+		return inet_aton(uri_host, &uri_host_in) != 0
06c6ff
+		    && memcmp(cert_host_addr, &uri_host_in.s_addr, 4) == 0;
06c6ff
+
06c6ff
+#ifdef CONFIG_IPV6
06c6ff
+	case 16:
06c6ff
+		return inet_pton(AF_INET6, uri_host, &uri_host_in6) == 1
06c6ff
+		    && memcmp(cert_host_addr, &uri_host_in6.s6_addr, 16) == 0;
06c6ff
+#endif
06c6ff
+
06c6ff
+	default:
06c6ff
+		return 0;
06c6ff
+	}
06c6ff
+}
06c6ff
+
06c6ff
+/** Verify one certificate in the server certificate chain.
06c6ff
+ * This callback is documented in SSL_set_verify(3).  */
06c6ff
+static int
06c6ff
+verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
06c6ff
+{
06c6ff
+	X509 *cert;
06c6ff
+	SSL *ssl;
06c6ff
+	struct socket *socket;
06c6ff
+	struct connection *conn;
06c6ff
+	unsigned char *host_in_uri;
06c6ff
+	GENERAL_NAMES *alts;
06c6ff
+	int saw_dns_name = 0;
06c6ff
+	int matched = 0;
06c6ff
+
06c6ff
+	/* If OpenSSL already found a problem, keep that.  */
06c6ff
+	if (!preverify_ok)
06c6ff
+		return 0;
06c6ff
+
06c6ff
+	/* Examine only the server certificate, not CA certificates.  */
06c6ff
+	if (X509_STORE_CTX_get_error_depth(ctx) != 0)
06c6ff
+		return preverify_ok;
06c6ff
+
06c6ff
+	cert = X509_STORE_CTX_get_current_cert(ctx);
06c6ff
+	ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
06c6ff
+	socket = SSL_get_ex_data(ssl, socket_SSL_ex_data_idx);
06c6ff
+	conn = socket->conn;
06c6ff
+	host_in_uri = get_uri_string(conn->uri, URI_HOST | URI_IDN);
06c6ff
+	if (!host_in_uri)
06c6ff
+		return 0;
06c6ff
+
06c6ff
+	/* RFC 5280 section 4.2.1.6 describes the subjectAltName extension.
06c6ff
+	 * RFC 2818 section 3.1 says Common Name must not be used
06c6ff
+	 * if dNSName is present.  */
06c6ff
+	alts = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL);
06c6ff
+	if (alts != NULL) {
06c6ff
+		int alt_count;
06c6ff
+		int alt_pos;
06c6ff
+		GENERAL_NAME *alt;
06c6ff
+
06c6ff
+		alt_count = sk_GENERAL_NAME_num(alts);
06c6ff
+		for (alt_pos = 0; !matched && alt_pos < alt_count; ++alt_pos) {
06c6ff
+			alt = sk_GENERAL_NAME_value(alts, alt_pos);
06c6ff
+			if (alt->type == GEN_DNS) {
06c6ff
+				saw_dns_name = 1;
06c6ff
+				matched = match_uri_host_name(host_in_uri,
06c6ff
+							      alt->d.dNSName);
06c6ff
+			} else if (alt->type == GEN_IPADD) {
06c6ff
+				matched = match_uri_host_ip(host_in_uri,
06c6ff
+							    alt->d.iPAddress);
06c6ff
+			}
06c6ff
+		}
06c6ff
+
06c6ff
+		/* Free the GENERAL_NAMES list and each element.  */
06c6ff
+		sk_GENERAL_NAME_pop_free(alts, GENERAL_NAME_free);
06c6ff
+	}
06c6ff
+
06c6ff
+	if (!matched && !saw_dns_name) {
06c6ff
+		X509_NAME *name;
06c6ff
+		int cn_index;
06c6ff
+		X509_NAME_ENTRY *entry = NULL;
06c6ff
+
06c6ff
+		name = X509_get_subject_name(cert);
06c6ff
+		cn_index = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
06c6ff
+		if (cn_index >= 0)
06c6ff
+			entry = X509_NAME_get_entry(name, cn_index);
06c6ff
+		if (entry != NULL)
06c6ff
+			matched = match_uri_host_name(host_in_uri,
06c6ff
+						      X509_NAME_ENTRY_get_data(entry));
06c6ff
+	}
06c6ff
+
06c6ff
+	mem_free(host_in_uri);
06c6ff
+	return matched;
06c6ff
+}
06c6ff
+
06c6ff
+#endif	/* USE_OPENSSL */
06c6ff
+
06c6ff
 static void
06c6ff
 ssl_want_read(struct socket *socket)
06c6ff
 {
06c6ff
@@ -149,7 +358,7 @@ ssl_connect(struct socket *socket)
06c6ff
 	if (get_opt_bool("connection.ssl.cert_verify"))
06c6ff
 		SSL_set_verify(socket->ssl, SSL_VERIFY_PEER
06c6ff
 					  | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
06c6ff
-				NULL);
06c6ff
+			       verify_callback);
06c6ff
 
06c6ff
 	if (get_opt_bool("connection.ssl.client_cert.enable")) {
06c6ff
 		unsigned char *client_cert;
06c6ff
diff --git a/src/network/ssl/ssl.c b/src/network/ssl/ssl.c
06c6ff
index 7767a71..d1881c8 100644
06c6ff
--- a/src/network/ssl/ssl.c
06c6ff
+++ b/src/network/ssl/ssl.c
06c6ff
@@ -39,7 +39,35 @@
06c6ff
 #define	PATH_MAX	256 /* according to my /usr/include/bits/posix1_lim.h */
06c6ff
 #endif
06c6ff
 
06c6ff
-SSL_CTX *context = NULL;
06c6ff
+static SSL_CTX *context = NULL;
06c6ff
+int socket_SSL_ex_data_idx = -1;
06c6ff
+
06c6ff
+/** Prevent SSL_dup() if the SSL is associated with struct socket.
06c6ff
+ * We cannot copy struct socket and it doesn't have a reference count
06c6ff
+ * either.  */
06c6ff
+static int
06c6ff
+socket_SSL_ex_data_dup(CRYPTO_EX_DATA *to, CRYPTO_EX_DATA *from,
06c6ff
+		       void *from_d, int idx, long argl, void *argp)
06c6ff
+{
06c6ff
+	/* The documentation of from_d in RSA_get_ex_new_index(3)
06c6ff
+	 * is a bit unclear.  The caller does something like:
06c6ff
+	 *
06c6ff
+	 * void *data = CRYPTO_get_ex_data(from, idx);
06c6ff
+	 * socket_SSL_dup(to, from, &data, idx, argl, argp);
06c6ff
+	 * CRYPTO_set_ex_data(to, idx, data);
06c6ff
+	 *
06c6ff
+	 * i.e., from_d always points to a pointer, even though
06c6ff
+	 * it is just a void * in the prototype.  */
06c6ff
+	struct socket *socket = *(void **) from_d;
06c6ff
+
06c6ff
+	assert(idx == socket_SSL_ex_data_idx);
06c6ff
+	if_assert_failed return 0;
06c6ff
+
06c6ff
+	if (socket)
06c6ff
+		return 0;	/* prevent SSL_dup() */
06c6ff
+	else
06c6ff
+		return 1;	/* allow SSL_dup() */
06c6ff
+}
06c6ff
 
06c6ff
 static void
06c6ff
 init_openssl(struct module *module)
06c6ff
@@ -48,12 +76,17 @@ init_openssl(struct module *module)
06c6ff
 	context = SSL_CTX_new(SSLv23_client_method());
06c6ff
 	SSL_CTX_set_options(context, SSL_OP_ALL);
06c6ff
 	SSL_CTX_set_default_verify_paths(context);
06c6ff
+	socket_SSL_ex_data_idx = SSL_get_ex_new_index(0, NULL,
06c6ff
+						      NULL,
06c6ff
+						      socket_SSL_ex_data_dup,
06c6ff
+						      NULL);
06c6ff
 }
06c6ff
 
06c6ff
 static void
06c6ff
 done_openssl(struct module *module)
06c6ff
 {
06c6ff
 	if (context) SSL_CTX_free(context);
06c6ff
+	/* There is no function that undoes SSL_get_ex_new_index.  */
06c6ff
 }
06c6ff
 
06c6ff
 static union option_info openssl_options[] = {
06c6ff
@@ -219,6 +252,12 @@ init_ssl_connection(struct socket *socket,
06c6ff
 	socket->ssl = SSL_new(context);
06c6ff
 	if (!socket->ssl) return S_SSL_ERROR;
06c6ff
 
06c6ff
+	if (!SSL_set_ex_data(socket->ssl, socket_SSL_ex_data_idx, socket)) {
06c6ff
+		SSL_free(socket->ssl);
06c6ff
+		socket->ssl = NULL;
06c6ff
+		return S_SSL_ERROR;
06c6ff
+	}
06c6ff
+
06c6ff
 	/* If the server name is known, pass it to OpenSSL.
06c6ff
 	 *
06c6ff
 	 * The return value of SSL_set_tlsext_host_name is not
06c6ff
diff --git a/src/network/ssl/ssl.h b/src/network/ssl/ssl.h
06c6ff
index bfd94e1..480b4db 100644
06c6ff
--- a/src/network/ssl/ssl.h
06c6ff
+++ b/src/network/ssl/ssl.h
06c6ff
@@ -29,6 +29,9 @@ void done_ssl_connection(struct socket *socket);
06c6ff
 
06c6ff
 unsigned char *get_ssl_connection_cipher(struct socket *socket);
06c6ff
 
06c6ff
+#if defined(CONFIG_OPENSSL) || defined(CONFIG_NSS_COMPAT_OSSL)
06c6ff
+extern int socket_SSL_ex_data_idx;
06c6ff
+#endif
06c6ff
 
06c6ff
 /* Internal type used in ssl module. */
06c6ff
 
06c6ff
diff --git a/src/network/ssl/test/Makefile b/src/network/ssl/test/Makefile
06c6ff
new file mode 100644
06c6ff
index 0000000..f2196eb
06c6ff
--- /dev/null
06c6ff
+++ b/src/network/ssl/test/Makefile
06c6ff
@@ -0,0 +1,9 @@
06c6ff
+top_builddir=../../../..
06c6ff
+include $(top_builddir)/Makefile.config
06c6ff
+
06c6ff
+SUBDIRS =
06c6ff
+TEST_PROGS = match-hostname-test
06c6ff
+TESTDEPS += \
06c6ff
+ $(top_builddir)/src/network/ssl/match-hostname.o
06c6ff
+
06c6ff
+include $(top_srcdir)/Makefile.lib
06c6ff
diff --git a/src/network/ssl/test/match-hostname-test.c b/src/network/ssl/test/match-hostname-test.c
06c6ff
new file mode 100644
06c6ff
index 0000000..fbdf6fa
06c6ff
--- /dev/null
06c6ff
+++ b/src/network/ssl/test/match-hostname-test.c
06c6ff
@@ -0,0 +1,123 @@
06c6ff
+/* Test match_hostname_pattern() */
06c6ff
+
06c6ff
+#ifdef HAVE_CONFIG_H
06c6ff
+#include "config.h"
06c6ff
+#endif
06c6ff
+
06c6ff
+#include <stdio.h>
06c6ff
+#include <stdlib.h>
06c6ff
+
06c6ff
+#include "elinks.h"
06c6ff
+
06c6ff
+#include "network/ssl/match-hostname.h"
06c6ff
+#include "util/string.h"
06c6ff
+
06c6ff
+struct match_hostname_pattern_test_case
06c6ff
+{
06c6ff
+	const unsigned char *pattern;
06c6ff
+	const unsigned char *hostname;
06c6ff
+	int match;
06c6ff
+};
06c6ff
+
06c6ff
+static const struct match_hostname_pattern_test_case match_hostname_pattern_test_cases[] = {
06c6ff
+	{ "*r*.example.org", "random.example.org", 1 },
06c6ff
+	{ "*r*.example.org", "history.example.org", 1 },
06c6ff
+	{ "*r*.example.org", "frozen.fruit.example.org", 0 },
06c6ff
+	{ "*r*.example.org", "steamed.fruit.example.org", 0 },
06c6ff
+
06c6ff
+	{ "ABC.def.Ghi", "abc.DEF.gHI", 1 },
06c6ff
+
06c6ff
+	{ "*", "localhost", 1 },
06c6ff
+	{ "*", "example.org", 0 },
06c6ff
+	{ "*.*", "example.org", 1 },
06c6ff
+	{ "*.*.*", "www.example.org", 1 },
06c6ff
+	{ "*.*.*", "example.org", 0 },
06c6ff
+
06c6ff
+	{ "assign", "assignee", 0 },
06c6ff
+	{ "*peg", "arpeggiator", 0 },
06c6ff
+	{ "*peg*", "arpeggiator", 1 },
06c6ff
+	{ "*r*gi*", "arpeggiator", 1 },
06c6ff
+	{ "*r*git*", "arpeggiator", 0 },
06c6ff
+
06c6ff
+	{ NULL, NULL, 0 }
06c6ff
+};
06c6ff
+
06c6ff
+int
06c6ff
+main(void)
06c6ff
+{
06c6ff
+	const struct match_hostname_pattern_test_case *test;
06c6ff
+	int count_ok = 0;
06c6ff
+	int count_fail = 0;
06c6ff
+	struct string hostname_str = NULL_STRING;
06c6ff
+	struct string pattern_str = NULL_STRING;
06c6ff
+
06c6ff
+	if (!init_string(&hostname_str) || !init_string(&pattern_str)) {
06c6ff
+		fputs("Out of memory.\n", stderr);
06c6ff
+		done_string(&hostname_str);
06c6ff
+		done_string(&pattern_str);
06c6ff
+		return EXIT_FAILURE;
06c6ff
+	}
06c6ff
+
06c6ff
+	for (test = match_hostname_pattern_test_cases; test->pattern; test++) {
06c6ff
+		int match;
06c6ff
+
06c6ff
+		match = match_hostname_pattern(
06c6ff
+			test->hostname,
06c6ff
+			strlen(test->hostname),
06c6ff
+			test->pattern,
06c6ff
+			strlen(test->pattern));
06c6ff
+		if (!match == !test->match) {
06c6ff
+			/* Test OK */
06c6ff
+			count_ok++;
06c6ff
+		} else {
06c6ff
+			fprintf(stderr, "match_hostname_pattern() test failed\n"
06c6ff
+				"\tHostname: %s\n"
06c6ff
+				"\tPattern: %s\n"
06c6ff
+				"\tActual result: %d\n"
06c6ff
+				"\tCorrect result: %d\n",
06c6ff
+				test->hostname,
06c6ff
+				test->pattern,
06c6ff
+				match,
06c6ff
+				test->match);
06c6ff
+			count_fail++;
06c6ff
+		}
06c6ff
+
06c6ff
+		/* Try with strings that are not null-terminated.  */
06c6ff
+		hostname_str.length = 0;
06c6ff
+		add_to_string(&hostname_str, test->hostname);
06c6ff
+		add_to_string(&hostname_str, "ZZZ");
06c6ff
+		pattern_str.length = 0;
06c6ff
+		add_to_string(&pattern_str, test->pattern);
06c6ff
+		add_to_string(&hostname_str, "______");
06c6ff
+
06c6ff
+		match = match_hostname_pattern(
06c6ff
+			hostname_str.source,
06c6ff
+			strlen(test->hostname),
06c6ff
+			pattern_str.source,
06c6ff
+			strlen(test->pattern));
06c6ff
+		if (!match == !test->match) {
06c6ff
+			/* Test OK */
06c6ff
+			count_ok++;
06c6ff
+		} else {
06c6ff
+			fprintf(stderr, "match_hostname_pattern() test failed\n"
06c6ff
+				"\tVariant: Strings were not null-terminated.\n"
06c6ff
+				"\tHostname: %s\n"
06c6ff
+				"\tPattern: %s\n"
06c6ff
+				"\tActual result: %d\n"
06c6ff
+				"\tCorrect result: %d\n",
06c6ff
+				test->hostname,
06c6ff
+				test->pattern,
06c6ff
+				match,
06c6ff
+				test->match);
06c6ff
+			count_fail++;
06c6ff
+		}
06c6ff
+	}
06c6ff
+
06c6ff
+	printf("Summary of match_hostname_pattern() tests: %d OK, %d failed.\n",
06c6ff
+	       count_ok, count_fail);
06c6ff
+
06c6ff
+	done_string(&hostname_str);
06c6ff
+	done_string(&pattern_str);
06c6ff
+	return count_fail ? EXIT_FAILURE : EXIT_SUCCESS;
06c6ff
+
06c6ff
+}
06c6ff
diff --git a/src/network/ssl/test/test-match-hostname b/src/network/ssl/test/test-match-hostname
06c6ff
new file mode 100755
06c6ff
index 0000000..01d7173
06c6ff
--- /dev/null
06c6ff
+++ b/src/network/ssl/test/test-match-hostname
06c6ff
@@ -0,0 +1,3 @@
06c6ff
+#! /bin/sh -e
06c6ff
+
06c6ff
+./match-hostname-test
06c6ff
-- 
06c6ff
2.1.0
06c6ff
06c6ff
06c6ff
From 0cb6967bb9ccabc583bbdc6ee76baf4fdf0f90cc Mon Sep 17 00:00:00 2001
06c6ff
From: mancha <mancha@mac.hush.com>
06c6ff
Date: Sun, 15 Jul 2012 23:27:53 +0200
06c6ff
Subject: [PATCH 3/4] Fix hostname verification code.
06c6ff
06c6ff
[ From bug 1123 attachment 569.  --KON ]
06c6ff
06c6ff
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
06c6ff
---
06c6ff
 src/network/ssl/match-hostname.c | 2 +-
06c6ff
 1 file changed, 1 insertion(+), 1 deletion(-)
06c6ff
06c6ff
diff --git a/src/network/ssl/match-hostname.c b/src/network/ssl/match-hostname.c
06c6ff
index 9a64bb4..80d93b0 100644
06c6ff
--- a/src/network/ssl/match-hostname.c
06c6ff
+++ b/src/network/ssl/match-hostname.c
06c6ff
@@ -97,7 +97,7 @@ match_hostname_pattern(const unsigned char *hostname,
06c6ff
 				 * '*' must not match such characters.
06c6ff
 				 * Do the same if invalid UTF-8 is found.
06c6ff
 				 * Cast away const.  */
06c6ff
-				uni = utf8_to_unicode((unsigned char **) hostname,
06c6ff
+				uni = utf8_to_unicode((unsigned char **) &hostname,
06c6ff
 						      hostname_end);
06c6ff
 				if (uni == 0x002E
06c6ff
 				    || uni == 0x3002
06c6ff
-- 
06c6ff
2.1.0
06c6ff
06c6ff
06c6ff
From cf8586b0389911d944d767646d5a91c2e1bae86c Mon Sep 17 00:00:00 2001
06c6ff
From: Kamil Dudka <kdudka@redhat.com>
06c6ff
Date: Fri, 5 Jun 2015 17:08:46 +0200
06c6ff
Subject: [PATCH 4/4] ssl: use the OpenSSL-provided host name check
06c6ff
06c6ff
... if built against a new enough version of OpenSSL
06c6ff
06c6ff
Suggested-by: Christian Heimes
06c6ff
---
06c6ff
 configure.in             |  3 +++
06c6ff
 src/network/ssl/socket.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++-
06c6ff
 2 files changed, 52 insertions(+), 1 deletion(-)
06c6ff
06c6ff
diff --git a/configure.in b/configure.in
06c6ff
index 91d0257..1d858bd 100644
06c6ff
--- a/configure.in
06c6ff
+++ b/configure.in
06c6ff
@@ -1044,6 +1044,9 @@ else
06c6ff
 fi
06c6ff
 
06c6ff
 AC_MSG_RESULT($cf_result)
06c6ff
+if test "$cf_result" = yes; then
06c6ff
+	AC_CHECK_FUNCS(X509_VERIFY_PARAM_set1_host)
06c6ff
+fi
06c6ff
 
06c6ff
 # ---- GNU TLS
06c6ff
 
06c6ff
diff --git a/src/network/ssl/socket.c b/src/network/ssl/socket.c
06c6ff
index a67bbde..c9e2be4 100644
06c6ff
--- a/src/network/ssl/socket.c
06c6ff
+++ b/src/network/ssl/socket.c
06c6ff
@@ -7,6 +7,9 @@
06c6ff
 #ifdef CONFIG_OPENSSL
06c6ff
 #include <openssl/ssl.h>
06c6ff
 #include <openssl/x509v3.h>
06c6ff
+#ifdef HAVE_X509_VERIFY_PARAM_SET1_HOST
06c6ff
+#include <openssl/x509_vfy.h>
06c6ff
+#endif
06c6ff
 #define USE_OPENSSL
06c6ff
 #elif defined(CONFIG_NSS_COMPAT_OSSL)
06c6ff
 #include <nss_compat_ossl/nss_compat_ossl.h>
06c6ff
@@ -97,6 +100,30 @@ ssl_set_no_tls(struct socket *socket)
06c6ff
 
06c6ff
 #ifdef USE_OPENSSL
06c6ff
 
06c6ff
+#ifdef HAVE_X509_VERIFY_PARAM_SET1_HOST
06c6ff
+/* activate the OpenSSL-provided host name check */
06c6ff
+static int
06c6ff
+ossl_set_hostname(void *ssl, unsigned char *server_name)
06c6ff
+{
06c6ff
+	int ret = -1;
06c6ff
+
06c6ff
+	X509_VERIFY_PARAM *vpm = X509_VERIFY_PARAM_new();
06c6ff
+	if (vpm) {
06c6ff
+		if (X509_VERIFY_PARAM_set1_host(vpm, (char *) server_name, 0)
06c6ff
+				&& SSL_set1_param(ssl, vpm))
06c6ff
+		{
06c6ff
+			/* successfully activated the OpenSSL host name check */
06c6ff
+			ret = 0;
06c6ff
+		}
06c6ff
+
06c6ff
+		X509_VERIFY_PARAM_free(vpm);
06c6ff
+	}
06c6ff
+
06c6ff
+	return ret;
06c6ff
+}
06c6ff
+
06c6ff
+#else /* HAVE_X509_VERIFY_PARAM_SET1_HOST */
06c6ff
+
06c6ff
 /** Checks whether the host component of a URI matches a host name in
06c6ff
  * the server certificate.
06c6ff
  *
06c6ff
@@ -289,6 +316,7 @@ verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
06c6ff
 	mem_free(host_in_uri);
06c6ff
 	return matched;
06c6ff
 }
06c6ff
+#endif	/* HAVE_X509_VERIFY_PARAM_SET1_HOST */
06c6ff
 
06c6ff
 #endif	/* USE_OPENSSL */
06c6ff
 
06c6ff
@@ -329,6 +357,9 @@ ssl_connect(struct socket *socket)
06c6ff
 	int ret;
06c6ff
 	unsigned char *server_name;
06c6ff
 	struct connection *conn = socket->conn;
06c6ff
+#ifdef USE_OPENSSL
06c6ff
+	int (*verify_callback_ptr)(int, X509_STORE_CTX *);
06c6ff
+#endif	/* USE_OPENSSL */
06c6ff
 
06c6ff
 	/* TODO: Recode server_name to UTF-8.  */
06c6ff
 	server_name = get_uri_string(conn->proxied_uri, URI_HOST);
06c6ff
@@ -347,6 +378,23 @@ ssl_connect(struct socket *socket)
06c6ff
 		return -1;
06c6ff
 	}
06c6ff
 
06c6ff
+#ifdef USE_OPENSSL
06c6ff
+#ifdef HAVE_X509_VERIFY_PARAM_SET1_HOST
06c6ff
+	/* activate the OpenSSL-provided host name check */
06c6ff
+	if (ossl_set_hostname(socket->ssl, server_name)) {
06c6ff
+		mem_free_if(server_name);
06c6ff
+		socket->ops->done(socket, connection_state(S_SSL_ERROR));
06c6ff
+		return -1;
06c6ff
+	}
06c6ff
+
06c6ff
+	/* verify_callback() is not needed with X509_VERIFY_PARAM_set1_host() */
06c6ff
+	verify_callback_ptr = NULL;
06c6ff
+#else
06c6ff
+	/* use our own callback implementing the host name check */
06c6ff
+	verify_callback_ptr = verify_callback;
06c6ff
+#endif
06c6ff
+#endif	/* USE_OPENSSL */
06c6ff
+
06c6ff
 	mem_free_if(server_name);
06c6ff
 
06c6ff
 	if (socket->no_tls)
06c6ff
@@ -358,7 +406,7 @@ ssl_connect(struct socket *socket)
06c6ff
 	if (get_opt_bool("connection.ssl.cert_verify"))
06c6ff
 		SSL_set_verify(socket->ssl, SSL_VERIFY_PEER
06c6ff
 					  | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
06c6ff
-			       verify_callback);
06c6ff
+			       verify_callback_ptr);
06c6ff
 
06c6ff
 	if (get_opt_bool("connection.ssl.client_cert.enable")) {
06c6ff
 		unsigned char *client_cert;
06c6ff
-- 
06c6ff
2.4.3
06c6ff