Make SSL_set1_host() and SSL_add1_host() take IP addresses
authorDavid Woodhouse <dwmw2@infradead.org>
Mon, 14 Oct 2019 09:46:07 +0000 (10:46 +0100)
committerTomas Mraz <tmraz@fedoraproject.org>
Mon, 3 Aug 2020 15:15:35 +0000 (17:15 +0200)
There is a slight mismatch here because X509_VERIFY_PARAM copes only
with a single IP address, and doesn't let it be cleared once it's set.
But this fixes up the major use case, making things easier for users to
get it right.

The sconnect demo now works for Legacy IP literals; for IPv6 it needs to
fix up the way it tries to split the host:port string, which will happen
in a subsequent patch.

Reviewed-by: Viktor Dukhovni <viktor@openssl.org>
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/9201)

ssl/ssl_lib.c

index 871606cfc1e07b53d0f596c83b8b58c72cb3c045..a31d2dd2ff4c7426d7106381c1b2823359659b17 100644 (file)
@@ -955,11 +955,21 @@ int SSL_set_trust(SSL *s, int trust)
 
 int SSL_set1_host(SSL *s, const char *hostname)
 {
+    /* If a hostname is provided and parses as an IP address,
+     * treat it as such. */
+    if (hostname && X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname) == 1)
+        return 1;
+
     return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
 }
 
 int SSL_add1_host(SSL *s, const char *hostname)
 {
+    /* If a hostname is provided and parses as an IP address,
+     * treat it as such. */
+    if (hostname && X509_VERIFY_PARAM_set1_ip_asc(s->param, hostname) == 1)
+        return 1;
+
     return X509_VERIFY_PARAM_add1_host(s->param, hostname, 0);
 }