This is deprecated, and needs to be obtained via a different chain of
calls: X509_NAME_get_index_by_NID(), X509_NAME_get_entry(),
X509_NAME_ENTRY_get_data(), and then ASN1_STRING_to_UTF8() to convert
it to utf8 for comparsion and printing.

Move those calls into x509_get_part(), and change it to store the
output in a BUFFER.

Change check_host() to use x509_get_part() too.

Since x509_get_part() is a general function, move the "Unknown" output
used by interactive_check_cert() into that function instead.
---
 mutt_ssl.c | 78 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 46 insertions(+), 32 deletions(-)

diff --git a/mutt_ssl.c b/mutt_ssl.c
index 9af5af9b..e12b7efc 100644
--- a/mutt_ssl.c
+++ b/mutt_ssl.c
@@ -749,15 +749,36 @@ static void ssl_dprint_err_stack(void)
 }
 
 
-static char *x509_get_part(const X509_NAME *name, int nid)
+static int x509_get_part(BUFFER *output, const X509_NAME *name, int nid)
 {
-  static char ret[SHORT_STRING];
+  int index;
+  const X509_NAME_ENTRY *entry;
+  const ASN1_STRING *asn1_str;
+  unsigned char *utf8_str;
+  int utf8_len;
 
-  if (!name ||
-      X509_NAME_get_text_by_NID(name, nid, ret, sizeof(ret)) < 0)
-    strfcpy(ret, _("Unknown"), sizeof(ret));
+  mutt_buffer_clear(output);
 
-  return ret;
+  index = X509_NAME_get_index_by_NID(name, nid, -1);
+  if (index < 0)
+    return -1;
+
+  entry = X509_NAME_get_entry(name, index);
+  if (!entry)
+    return -1;
+
+  asn1_str = X509_NAME_ENTRY_get_data(entry);
+  if (!asn1_str)
+    return -1;
+
+  utf8_len = ASN1_STRING_to_UTF8(&utf8_str, asn1_str);
+  if (utf8_len < 0)
+    return -1;
+
+  mutt_buffer_addstr_n(output, (const char *)utf8_str, utf8_len);
+
+  OPENSSL_free(utf8_str);
+  return 0;
 }
 
 static void x509_fingerprint(char *s, int l, X509 * cert, const EVP_MD 
*(*hashfunc)(void))
@@ -970,8 +991,7 @@ static int check_host(X509 *x509cert, const char *hostname, 
char *err, size_t er
   char *hostname_ascii = NULL;
   /* needed to get the common name: */
   const X509_NAME *x509_subject;
-  char *buf = NULL;
-  int bufsize;
+  BUFFER *common_name_buf = NULL;
   /* needed to get the DNS subjectAltNames: */
   STACK_OF(GENERAL_NAME) *subj_alt_names;
   int subj_alt_names_count;
@@ -1035,31 +1055,18 @@ static int check_host(X509 *x509cert, const char 
*hostname, char *err, size_t er
       goto out;
     }
 
-    /* first get the space requirements */
-    bufsize = X509_NAME_get_text_by_NID(x509_subject, NID_commonName,
-                                        NULL, 0);
-    if (bufsize == -1)
+    common_name_buf = mutt_buffer_pool_get();
+
+    if (x509_get_part(common_name_buf, x509_subject, NID_commonName) < 0)
     {
       if (err && errlen)
         strfcpy(err, _("cannot get certificate common name"), errlen);
       goto out;
     }
-    bufsize++; /* space for the terminal nul char */
-    buf = safe_malloc((size_t)bufsize);
-    if (X509_NAME_get_text_by_NID(x509_subject, NID_commonName,
-                                  buf, bufsize) == -1)
-    {
-      if (err && errlen)
-        strfcpy(err, _("cannot get certificate common name"), errlen);
-      goto out;
-    }
-    /* cast is safe since bufsize is incremented above, so bufsize-1 is always
-     * zero or greater.
-     */
-    if (mutt_strlen(buf) == (size_t)bufsize - 1)
-    {
-      match_found = hostname_match(hostname_ascii, buf);
-    }
+
+    /* 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 (!match_found)
@@ -1073,7 +1080,7 @@ static int check_host(X509 *x509cert, const char 
*hostname, char *err, size_t er
   rc = 1;
 
 out:
-  FREE(&buf);
+  mutt_buffer_pool_release(&common_name_buf);
   FREE(&hostname_ascii);
 
   return rc;
@@ -1223,7 +1230,7 @@ static int interactive_check_cert(X509 *cert, int idx, 
int len, SSL *ssl, int al
   char title[STRING];
   MUTTMENU *menu;
   int done;
-  BUFFER *drow = NULL;
+  BUFFER *drow = NULL, *x509_part_buf = NULL;
   unsigned u;
   FILE *fp;
   int allow_skip = 0, reset_ignoremacro = 0;
@@ -1239,12 +1246,15 @@ static int interactive_check_cert(X509 *cert, int idx, 
int len, SSL *ssl, int al
   mutt_push_current_menu(menu);
 
   drow = mutt_buffer_pool_get();
+  x509_part_buf = mutt_buffer_pool_get();
 
   mutt_menu_add_dialog_row(menu, _("This certificate belongs to:"));
   x509_subject = X509_get_subject_name(cert);
   for (u = 0; u < mutt_array_size(part); u++)
   {
-    mutt_buffer_printf(drow, "   %s", x509_get_part(x509_subject, part[u]));
+    mutt_buffer_printf(drow, "   %s",
+                       !x509_get_part(x509_part_buf, x509_subject, part[u]) ?
+                       mutt_b2s(x509_part_buf) : _("Unknown"));
     mutt_menu_add_dialog_row(menu, mutt_b2s(drow));
   }
 
@@ -1253,10 +1263,14 @@ static int interactive_check_cert(X509 *cert, int idx, 
int len, SSL *ssl, int al
   x509_issuer = X509_get_issuer_name(cert);
   for (u = 0; u < mutt_array_size(part); u++)
   {
-    mutt_buffer_printf(drow, "   %s", x509_get_part(x509_issuer, part[u]));
+    mutt_buffer_printf(drow, "   %s",
+                       !x509_get_part(x509_part_buf, x509_issuer, part[u]) ?
+                       mutt_b2s(x509_part_buf) : _("Unknown"));
     mutt_menu_add_dialog_row(menu, mutt_b2s(drow));
   }
 
+  mutt_buffer_pool_release(&x509_part_buf);
+
   mutt_menu_add_dialog_row(menu, "");
   mutt_menu_add_dialog_row(menu, _("This certificate is valid"));
   mutt_buffer_printf(drow, _("   from %s"),
-- 
2.56.1

Reply via email to