Support optional server certificate name checking. Bug 1479
[exim.git] / src / src / tlscert-gnu.c
index 32b1986b8f627c117d6d541bde27a8e8f9adbdd4..73763730257f989e3a03c926f8c7ddbf7b21738f 100644 (file)
@@ -75,11 +75,27 @@ gnutls_global_deinit();
 *  Certificate field extraction routines
 *****************************************************/
 static uschar *
-time_copy(time_t t)
+g_err(const char * tag, const char * from, int gnutls_err)
 {
-uschar * cp = store_get(32);
-struct tm * tp = gmtime(&t);
-size_t len = strftime(CS cp, 32, "%b %e %T %Y %Z", tp);
+expand_string_message = string_sprintf("%s: %s fail: %s\n",
+  from, tag, gnutls_strerror(gnutls_err));
+return NULL;
+}
+
+
+static uschar *
+time_copy(time_t t, uschar * mod)
+{
+uschar * cp;
+struct tm * tp;
+size_t len;
+
+if (mod && Ustrcmp(mod, "int") == 0)
+  return string_sprintf("%u", (unsigned)t);
+
+cp = store_get(32);
+tp = gmtime(&t);
+len = strftime(CS cp, 32, "%b %e %T %Y %Z", tp);
 return len > 0 ? cp : NULL;
 }
 
@@ -88,24 +104,35 @@ return len > 0 ? cp : NULL;
 uschar *
 tls_cert_issuer(void * cert, uschar * mod)
 {
-uschar txt[256];
-size_t sz = sizeof(txt);
-return ( gnutls_x509_crt_get_issuer_dn(cert, CS txt, &sz) == 0 )
-  ? string_copy(txt) : NULL;
+uschar * cp = NULL;
+int ret;
+size_t siz = 0;
+
+if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz))
+    != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  return g_err("gi0", __FUNCTION__, ret);
+
+cp = store_get(siz);
+if ((ret = gnutls_x509_crt_get_issuer_dn(cert, cp, &siz)) < 0)
+  return g_err("gi1", __FUNCTION__, ret);
+
+return mod ? tls_field_from_dn(cp, mod) : cp;
 }
 
 uschar *
 tls_cert_not_after(void * cert, uschar * mod)
 {
 return time_copy(
-  gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t)cert));
+  gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t)cert),
+  mod);
 }
 
 uschar *
 tls_cert_not_before(void * cert, uschar * mod)
 {
 return time_copy(
-  gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t)cert));
+  gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t)cert),
+  mod);
 }
 
 uschar *
@@ -134,20 +161,13 @@ uschar * cp3;
 size_t len = 0;
 int ret;
 
-if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len)) !=
-       GNUTLS_E_SHORT_MEMORY_BUFFER)
-  {
-  fprintf(stderr, "%s: gs0 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
-  return NULL;
-  }
+if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len))
+    != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  return g_err("gs0", __FUNCTION__, ret);
 
 cp1 = store_get(len*4+1);
-
 if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len) != 0)
-  {
-  fprintf(stderr, "%s: gs1 fail\n", __FUNCTION__);
-  return NULL;
-  }
+  return g_err("gs1", __FUNCTION__, ret);
 
 for(cp3 = cp2 = cp1+len; cp1 < cp2; cp3 += 3, cp1++)
   sprintf(cp3, "%.2x ", *cp1);
@@ -167,10 +187,19 @@ return algo < 0 ? NULL : string_copy(gnutls_sign_get_name(algo));
 uschar *
 tls_cert_subject(void * cert, uschar * mod)
 {
-static uschar txt[256];
-size_t sz = sizeof(txt);
-return ( gnutls_x509_crt_get_dn(cert, CS txt, &sz) == 0 )
-  ? string_copy(txt) : NULL;
+uschar * cp = NULL;
+int ret;
+size_t siz = 0;
+
+if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz))
+    != GNUTLS_E_SHORT_MEMORY_BUFFER)
+  return g_err("gs0", __FUNCTION__, ret);
+
+cp = store_get(siz);
+if ((ret = gnutls_x509_crt_get_dn(cert, cp, &siz)) < 0)
+  return g_err("gs1", __FUNCTION__, ret);
+
+return mod ? tls_field_from_dn(cp, mod) : cp;
 }
 
 uschar *
@@ -192,20 +221,14 @@ int ret;
 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
   oid, idx, cp1, &siz, &crit);
 if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
-  {
-  fprintf(stderr, "%s: ge0 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
-  return NULL;
-  }
+  return g_err("ge0", __FUNCTION__, ret);
 
 cp1 = store_get(siz*4 + 1);
 
 ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
   oid, idx, cp1, &siz, &crit);
 if (ret < 0)
-  {
-  fprintf(stderr, "%s: ge1 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
-  return NULL;
-  }
+  return g_err("ge1", __FUNCTION__, ret);
 
 /* binary data, DER encoded */
 
@@ -245,7 +268,7 @@ for(index = 0;; index++)
   {
   siz = 0;
   switch(ret = gnutls_x509_crt_get_subject_alt_name(
-    (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL))
+      (gnutls_x509_crt_t)cert, index, NULL, &siz, NULL))
     {
     case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
       return list;     /* no more elements; normal exit */
@@ -254,24 +277,17 @@ for(index = 0;; index++)
       break;
 
     default:
-      expand_string_message = 
-       string_sprintf("%s: gs0 fail: %d %s\n", __FUNCTION__,
-         ret, gnutls_strerror(ret));
-      return NULL;
+      return g_err("gs0", __FUNCTION__, ret);
     }
 
   ele = store_get(siz+1);
   if ((ret = gnutls_x509_crt_get_subject_alt_name(
     (gnutls_x509_crt_t)cert, index, ele, &siz, NULL)) < 0)
-    {
-    expand_string_message = 
-      string_sprintf("%s: gs1 fail: %d %s\n", __FUNCTION__,
-       ret, gnutls_strerror(ret));
-    return NULL;
-    }
+    return g_err("gs1", __FUNCTION__, ret);
   ele[siz] = '\0';
 
-  if (match != -1 && match != ret)
+  if (  match != -1 && match != ret    /* wrong type of SAN */
+     || Ustrlen(ele) != siz)           /* contains a NUL */
     continue;
   switch (ret)
     {
@@ -307,12 +323,7 @@ for(index = 0;; index++)
   if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
     return list;
   if (ret < 0)
-    {
-    expand_string_message = 
-      string_sprintf("%s: gai fail: %d %s\n", __FUNCTION__,
-       ret, gnutls_strerror(ret));
-    return NULL;
-    }
+    return g_err("gai", __FUNCTION__, ret);
 
   list = string_append_listele(list, sep,
            string_copyn(uri.data, uri.size));
@@ -353,21 +364,14 @@ for(index = 0;; index++)
     case GNUTLS_E_SHORT_MEMORY_BUFFER:
       break;
     default:
-      expand_string_message = 
-       string_sprintf("%s: gc0 fail: %d %s\n", __FUNCTION__,
-         ret, gnutls_strerror(ret));
-      return NULL;
+      return g_err("gc0", __FUNCTION__, ret);
     }
 
   ele = store_get(siz+1);
   if ((ret = gnutls_x509_crt_get_crl_dist_points(
-    (gnutls_x509_crt_t)cert, index, ele, &siz, NULL, NULL)) < 0)
-    {
-    expand_string_message = 
-      string_sprintf("%s: gc1 fail: %d %s\n", __FUNCTION__,
-       ret, gnutls_strerror(ret));
-    return NULL;
-    }
+      (gnutls_x509_crt_t)cert, index, ele, &siz, NULL, NULL)) < 0)
+    return g_err("gc1", __FUNCTION__, ret);
+
   ele[siz] = '\0';
   list = string_append_listele(list, sep, ele);
   }
@@ -389,20 +393,12 @@ uschar * cp3;
 
 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, NULL, &siz))
     != GNUTLS_E_SHORT_MEMORY_BUFFER)
-  {
-  expand_string_message = 
-    string_sprintf("%s: gf0 fail: %d %s\n", __FUNCTION__,
-      ret, gnutls_strerror(ret));
-  return NULL;
-  }
+  return g_err("gf0", __FUNCTION__, ret);
+
 cp = store_get(siz*3+1);
 if ((ret = gnutls_x509_crt_get_fingerprint(cert, algo, cp, &siz)) < 0)
-  {
-  expand_string_message = 
-    string_sprintf("%s: gf1 fail: %d %s\n", __FUNCTION__,
-      ret, gnutls_strerror(ret));
-  return NULL;
-  }
+  return g_err("gf1", __FUNCTION__, ret);
+
 for (cp3 = cp2 = cp+siz; cp < cp2; cp++, cp3+=2)
   sprintf(cp3, "%02X",*cp);
 return cp2;
@@ -421,6 +417,12 @@ tls_cert_fprt_sha1(void * cert)
 return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA1);
 }
 
+uschar *
+tls_cert_fprt_sha256(void * cert)
+{
+return fingerprint((gnutls_x509_crt_t)cert, GNUTLS_DIG_SHA256);
+}
+
 
 /* vi: aw ai sw=2
 */