The previous versions were comparing a punycode hostname against a
utf8 common name.  Convert the common name to punycode too, via
idna_to_ascii_8z(), and compare that with the punycode hostname.

This is done for SAN already (DNS entries), and the comparison is made
that way because punycode normalizes utf-8 before converting it.  So
it makes sense to do the same for CN.

A check for common_name_buf length > 0 is added, to protect against a
segv.  safe_strdup() converts 0-length strings to NULL and
hostname_match() is not checking its arguments for NULL.
---
 mutt_ssl.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/mutt_ssl.c b/mutt_ssl.c
index 1ff46d94..fe8e9a29 100644
--- a/mutt_ssl.c
+++ b/mutt_ssl.c
@@ -991,7 +991,7 @@ static int check_host(X509 *x509cert, const char *hostname, 
char *err, size_t er
 {
   int i, rc = 0;
   /* hostname in ASCII format: */
-  char *hostname_ascii = NULL;
+  char *hostname_ascii = NULL, *common_name_ascii = NULL;
   /* needed to get the common name: */
   const X509_NAME *x509_subject;
   BUFFER *common_name_buf = NULL;
@@ -1067,8 +1067,18 @@ static int check_host(X509 *x509cert, const char 
*hostname, char *err, size_t er
     }
 
     /* This checks for embedded nuls */
-    if (mutt_buffer_len(common_name_buf) == 
mutt_strlen(mutt_b2s(common_name_buf)))
-      match_found = hostname_match(hostname_ascii, mutt_b2s(common_name_buf));
+    if (mutt_buffer_len(common_name_buf) > 0 &&
+        mutt_buffer_len(common_name_buf) == 
mutt_strlen(mutt_b2s(common_name_buf)))
+    {
+#if defined(HAVE_LIBIDN) || defined(HAVE_LIBIDN2)
+      if (idna_to_ascii_8z(mutt_b2s(common_name_buf), &common_name_ascii, 0) 
!= IDNA_SUCCESS)
+        common_name_ascii = safe_strdup(mutt_b2s(common_name_buf));
+#else
+      common_name_ascii = safe_strdup(mutt_b2s(common_name_buf));
+#endif
+      match_found = hostname_match(hostname_ascii, common_name_ascii);
+      FREE(&common_name_ascii);
+    }
   }
 
   if (!match_found)
-- 
2.56.1

Reply via email to