Fix hostname validation in the command-line tool to honour negative return values.
authorEmilia Kasper <emilia@openssl.org>
Thu, 5 Feb 2015 15:38:54 +0000 (16:38 +0100)
committerEmilia Kasper <emilia@openssl.org>
Tue, 10 Feb 2015 14:35:20 +0000 (15:35 +0100)
Specifically, an ASN.1 NumericString in the certificate CN will fail UTF-8 conversion
and result in a negative return value, which the "x509 -checkhost" command-line option
incorrectly interpreted as success.

Also update X509_check_host docs to reflect reality.

Thanks to Sean Burford (Google) for reporting this issue.

Reviewed-by: Richard Levitte <levitte@openssl.org>
apps/apps.c
crypto/x509v3/v3_utl.c
doc/crypto/X509_check_host.pod

index 97f0c0e182b36959f1c0df8a56b1d8e8d42fd991..bf044d40f85ae2456f7d4ffe60217424eb637e54 100644 (file)
@@ -2780,7 +2780,7 @@ void print_cert_checks(BIO *bio, X509 *x,
         return;
     if (checkhost) {
         BIO_printf(bio, "Hostname %s does%s match certificate\n",
-                   checkhost, X509_check_host(x, checkhost, 0, 0, NULL)
+                   checkhost, X509_check_host(x, checkhost, 0, 0, NULL) == 1
                    ? "" : " NOT");
     }
 
index 1ad3999c92e5fe53674c9fd082843ba3580f8d59..cd7a9809ca31e8eb835c841682b183bb911c84b3 100644 (file)
@@ -889,8 +889,13 @@ static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
         int astrlen;
         unsigned char *astr;
         astrlen = ASN1_STRING_to_UTF8(&astr, a);
-        if (astrlen < 0)
+        if (astrlen < 0) {
+            /*
+             * -1 could be an internal malloc failure or a decoding error from
+             * malformed input; we can't distinguish.
+             */
             return -1;
+        }
         rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
         if (rv > 0 && peername)
             *peername = BUF_strndup((char *)astr, astrlen);
index f8b530df9b5615a3e63f7ca1858580089e462d55..0def17aac1c54a49f734c7ccc03f3b8977d568e6 100644 (file)
@@ -109,9 +109,12 @@ but would not match a peer certificate with a DNS name of
 =head1 RETURN VALUES
 
 The functions return 1 for a successful match, 0 for a failed match
-and -1 for an internal error: typically a memory allocation failure.
+and -1 for an internal error: typically a memory allocation failure
+or an ASN.1 decoding error.
 
-X509_check_ip_asc() can also return -2 if the IP address string is malformed.
+All functions can also return -2 if the input is malformed. For example,
+X509_check_host() returns -2 if the provided B<name> contains embedded
+NULs.
 
 =head1 NOTES