Implemented gsasl driver for authentication.
[exim.git] / src / src / tls-gnu.c
index ee57659fa7bf174f86d1c0e0c35b21159cc51ac8..2d1a327dea80307939e2fc23b0c9d5714fa2ae5c 100644 (file)
@@ -1,10 +1,8 @@
-/* $Cambridge: exim/src/src/tls-gnu.c,v 1.18 2007/01/18 15:35:42 ph10 Exp $ */
-
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2007 */
+/* Copyright (c) University of Cambridge 1995 - 2009 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 /* This module provides TLS (aka SSL) support for Exim using the GnuTLS
@@ -14,6 +12,13 @@ is based on a patch that was contributed by Nikos Mavroyanopoulos.
 No cryptographic code is included in Exim. All this module does is to call
 functions from the GnuTLS library. */
 
+/* Note: This appears to be using an old API from compat.h; it is likely that
+someone familiary with GnuTLS programming could rework a lot of this to a
+modern API and perhaps remove the explicit knowledge of crypto algorithms from
+Exim.  Such a re-work would be most welcome and we'd sacrifice support for
+older GnuTLS releases without too many qualms -- maturity and experience
+in crypto libraries tends to improve their robustness against attack.
+Frankly, if you maintain it, you decide what's supported and what isn't. */
 
 /* Heading stuff for GnuTLS */
 
@@ -26,14 +31,12 @@ functions from the GnuTLS library. */
 #define PARAM_SIZE 2*1024
 
 
-/* Values for verify_requirment and initialized */
+/* Values for verify_requirment */
 
 enum { VERIFY_NONE, VERIFY_OPTIONAL, VERIFY_REQUIRED };
-enum { INITIALIZED_NOT, INITIALIZED_SERVER, INITIALIZED_CLIENT };
 
 /* Local static variables for GNUTLS */
 
-static BOOL initialized = INITIALIZED_NOT;
 static host_item *client_host;
 
 static gnutls_dh_params dh_params = NULL;
@@ -50,6 +53,13 @@ static int  verify_requirement;
 and space into which it can be copied and altered. */
 
 static const int default_proto_priority[16] = {
+  /* These are gnutls_protocol_t enum values */
+#if GNUTLS_VERSION_MAJOR > 1 || GNUTLS_VERSION_MINOR >= 7
+  GNUTLS_TLS1_2,
+#endif
+#if GNUTLS_VERSION_MAJOR > 1 || GNUTLS_VERSION_MINOR >= 2
+  GNUTLS_TLS1_1,
+#endif
   GNUTLS_TLS1,
   GNUTLS_SSL3,
   0 };
@@ -93,11 +103,27 @@ typedef struct pri_item {
 } pri_item;
 
 
-static int tls1_codes[] = { GNUTLS_TLS1, 0 };
+#if GNUTLS_VERSION_MAJOR > 1 || GNUTLS_VERSION_MINOR >= 7
+static int tls1_2_codes[] = { GNUTLS_TLS1_2, 0 };
+#endif
+#if GNUTLS_VERSION_MAJOR > 1 || GNUTLS_VERSION_MINOR >= 2
+static int tls1_1_codes[] = { GNUTLS_TLS1_1, 0 };
+#endif
+/* more recent libraries define this as an equivalent value to the
+canonical GNUTLS_TLS1_0; since they're the same, we stick to the
+older name. */
+static int tls1_0_codes[] = { GNUTLS_TLS1, 0 };
 static int ssl3_codes[] = { GNUTLS_SSL3, 0 };
 
 static pri_item proto_index[] = {
-  { US"TLS1", tls1_codes },
+#if GNUTLS_VERSION_MAJOR > 1 || GNUTLS_VERSION_MINOR >= 7
+  { US"TLS1.2", tls1_2_codes },
+#endif
+#if GNUTLS_VERSION_MAJOR > 1 || GNUTLS_VERSION_MINOR >= 2
+  { US"TLS1.1", tls1_1_codes },
+#endif
+  { US"TLS1.0", tls1_0_codes },
+  { US"TLS1", tls1_0_codes },
   { US"SSL3", ssl3_codes }
 };
 
@@ -164,27 +190,28 @@ Argument:
   prefix    text to include in the logged error
   host      NULL if setting up a server;
             the connected host if setting up a client
-  err       a GnuTLS error number, or 0 if local error
+  msg       additional error string (may be NULL)
+            usually obtained from gnutls_strerror()
 
 Returns:    OK/DEFER/FAIL
 */
 
 static int
-tls_error(uschar *prefix, host_item *host, int err)
+tls_error(uschar *prefix, host_item *host, const char *msg)
 {
-uschar *errtext = US"";
-if (err != 0) errtext = string_sprintf(": %s", gnutls_strerror(err));
 if (host == NULL)
   {
-  log_write(0, LOG_MAIN, "TLS error on connection from %s (%s)%s",
-    (sender_fullhost != NULL)? sender_fullhost : US "local process",
-    prefix, errtext);
+  uschar *conn_info = smtp_get_connection_info();
+  if (strncmp(conn_info, "SMTP ", 5) == 0)
+    conn_info += 5;
+  log_write(0, LOG_MAIN, "TLS error on %s (%s)%s%s",
+    conn_info, prefix, msg ? ": " : "", msg ? msg : "");
   return DEFER;
   }
 else
   {
-  log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s)%s",
-    host->name, host->address, prefix, errtext);
+  log_write(0, LOG_MAIN, "TLS error on connection to %s [%s] (%s)%s%s",
+    host->name, host->address, prefix, msg ? ": " : "", msg ? msg : "");
   return FAIL;
   }
 }
@@ -206,12 +233,12 @@ Returns:     TRUE/FALSE
 */
 
 static BOOL
-verify_certificate(gnutls_session session, uschar **error)
+verify_certificate(gnutls_session session, const char **error)
 {
-int verify;
+int rc = -1;
 uschar *dn_string = US"";
 const gnutls_datum *cert;
-unsigned int cert_size = 0;
+unsigned int verify, cert_size = 0;
 
 *error = NULL;
 
@@ -235,23 +262,23 @@ if (cert != NULL)
       dn_string = string_copy_malloc(buff);
     }
 
-  verify = gnutls_certificate_verify_peers(session);
+  rc = gnutls_certificate_verify_peers2(session, &verify);
   }
 else
   {
   DEBUG(D_tls) debug_printf("no peer certificate supplied\n");
   verify = GNUTLS_CERT_INVALID;
-  *error = US"not supplied";
+  *error = "not supplied";
   }
 
 /* Handle the result of verification. INVALID seems to be set as well
 as REVOKED, but leave the test for both. */
 
-if ((verify & (GNUTLS_CERT_INVALID|GNUTLS_CERT_REVOKED)) != 0)
+if ((rc < 0) || (verify & (GNUTLS_CERT_INVALID|GNUTLS_CERT_REVOKED)) != 0)
   {
   tls_certificate_verified = FALSE;
   if (*error == NULL) *error = ((verify & GNUTLS_CERT_REVOKED) != 0)?
-    US"revoked" : US"invalid";
+    "revoked" : "invalid";
   if (verify_requirement == VERIFY_REQUIRED)
     {
     DEBUG(D_tls) debug_printf("TLS certificate verification failed (%s): "
@@ -306,13 +333,13 @@ uschar filename[200];
 /* Initialize the data structures for holding the parameters */
 
 ret = gnutls_dh_params_init(&dh_params);
-if (ret < 0) return tls_error(US"init dh_params", host, ret);
+if (ret < 0) return tls_error(US"init dh_params", host, gnutls_strerror(ret));
 
 /* Set up the name of the cache file */
 
 if (!string_format(filename, sizeof(filename), "%s/gnutls-params",
       spool_directory))
-  return tls_error(US"overlong filename", host, 0);
+  return tls_error(US"overlong filename", host, NULL);
 
 /* Open the cache file for reading and if successful, read it and set up the
 parameters. */
@@ -324,19 +351,21 @@ if (fd >= 0)
   if (fstat(fd, &statbuf) < 0)
     {
     (void)close(fd);
-    return tls_error(US"TLS cache stat failed", host, 0);
+    return tls_error(US"TLS cache stat failed", host, strerror(errno));
     }
 
   m.size = statbuf.st_size;
   m.data = malloc(m.size);
   if (m.data == NULL)
-    return tls_error(US"memory allocation failed", host, 0);
+    return tls_error(US"memory allocation failed", host, strerror(errno));
+  errno = 0;
   if (read(fd, m.data, m.size) != m.size)
-    return tls_error(US"TLS cache read failed", host, 0);
+    return tls_error(US"TLS cache read failed", host, strerror(errno));
   (void)close(fd);
 
   ret = gnutls_dh_params_import_pkcs3(dh_params, &m, GNUTLS_X509_FMT_PEM);
-  if (ret < 0) return tls_error(US"DH params import", host, ret);
+  if (ret < 0)
+    return tls_error(US"DH params import", host, gnutls_strerror(ret));
   DEBUG(D_tls) debug_printf("read D-H parameters from file\n");
 
   free(m.data);
@@ -353,7 +382,7 @@ else if (errno == ENOENT)
   }
 else
   return tls_error(string_open_failed(errno, "%s for reading", filename),
-    host, 0);
+    host, NULL);
 
 /* If ret < 0, either the cache file does not exist, or the data it contains
 is not useful. One particular case of this is when upgrading from an older
@@ -368,7 +397,7 @@ if (ret < 0)
   DEBUG(D_tls) debug_printf("generating %d bit Diffie-Hellman key...\n",
     DH_BITS);
   ret = gnutls_dh_params_generate2(dh_params, DH_BITS);
-  if (ret < 0) return tls_error(US"D-H key generation", host, ret);
+  if (ret < 0) return tls_error(US"D-H key generation", host, gnutls_strerror(ret));
 
   /* Write the parameters to a file in the spool directory so that we
   can use them from other Exim processes. */
@@ -377,7 +406,7 @@ if (ret < 0)
   fd = Uopen(tempfilename, O_WRONLY|O_CREAT, 0400);
   if (fd < 0)
     return tls_error(string_open_failed(errno, "%s for writing", filename),
-      host, 0);
+      host, NULL);
   (void)fchown(fd, exim_uid, exim_gid);   /* Probably not necessary */
 
   /* export the parameters in a format that can be generated using GNUTLS'
@@ -390,23 +419,25 @@ if (ret < 0)
   m.size = PARAM_SIZE;
   m.data = malloc(m.size);
   if (m.data == NULL)
-    return tls_error(US"memory allocation failed", host, 0);
+    return tls_error(US"memory allocation failed", host, strerror(errno));
 
   m.size = PARAM_SIZE;
   ret = gnutls_dh_params_export_pkcs3(dh_params, GNUTLS_X509_FMT_PEM, m.data,
     &m.size);
-  if (ret < 0) return tls_error(US"DH params export", host, ret);
+  if (ret < 0)
+    return tls_error(US"DH params export", host, gnutls_strerror(ret));
 
   m.size = Ustrlen(m.data);
+  errno = 0;
   if (write(fd, m.data, m.size) != m.size || write(fd, "\n", 1) != 1)
-    return tls_error(US"TLS cache write failed", host, 0);
+    return tls_error(US"TLS cache write failed", host, strerror(errno));
 
   free(m.data);
   (void)close(fd);
 
   if (rename(CS tempfilename, CS filename) < 0)
-    return tls_error(string_sprintf("failed to rename %s as %s: %s",
-      tempfilename, filename, strerror(errno)), host, 0);
+    return tls_error(string_sprintf("failed to rename %s as %s",
+      tempfilename, filename), host, strerror(errno));
 
   DEBUG(D_tls) debug_printf("wrote D-H parameters to file %s\n", filename);
   }
@@ -442,10 +473,10 @@ tls_init(host_item *host, uschar *certificate, uschar *privatekey, uschar *cas,
 int rc;
 uschar *cert_expanded, *key_expanded, *cas_expanded, *crl_expanded;
 
-initialized = (host == NULL)? INITIALIZED_SERVER : INITIALIZED_CLIENT;
+client_host = host;
 
 rc = gnutls_global_init();
-if (rc < 0) return tls_error(US"tls-init", host, rc);
+if (rc < 0) return tls_error(US"tls-init", host, gnutls_strerror(rc));
 
 /* Create D-H parameters, or read them from the cache file. This function does
 its own SMTP error messaging. */
@@ -456,7 +487,9 @@ if (rc != OK) return rc;
 /* Create the credentials structure */
 
 rc = gnutls_certificate_allocate_credentials(&x509_cred);
-if (rc < 0) return tls_error(US"certificate_allocate_credentials", host, rc);
+if (rc < 0)
+  return tls_error(US"certificate_allocate_credentials",
+    host, gnutls_strerror(rc));
 
 /* This stuff must be done for each session, because different certificates
 may be required for different sessions. */
@@ -490,7 +523,7 @@ if (cert_expanded != NULL)
     {
     uschar *msg = string_sprintf("cert/key setup: cert=%s key=%s",
       cert_expanded, key_expanded);
-    return tls_error(msg, host, rc);
+    return tls_error(msg, host, gnutls_strerror(rc));
     }
   }
 
@@ -499,7 +532,7 @@ if (cert_expanded != NULL)
 else
   {
   if (host == NULL)
-    return tls_error(US"no TLS server certificate is specified", host, 0);
+    return tls_error(US"no TLS server certificate is specified", NULL, NULL);
   DEBUG(D_tls) debug_printf("no TLS client certificate is specified\n");
   }
 
@@ -532,7 +565,7 @@ if (cas != NULL)
     {
     rc = gnutls_certificate_set_x509_trust_file(x509_cred, CS cas_expanded,
       GNUTLS_X509_FMT_PEM);
-    if (rc < 0) return tls_error(US"setup_certs", host, rc);
+    if (rc < 0) return tls_error(US"setup_certs", host, gnutls_strerror(rc));
 
     if (crl != NULL && *crl != 0)
       {
@@ -541,7 +574,7 @@ if (cas != NULL)
       DEBUG(D_tls) debug_printf("loading CRL file = %s\n", crl_expanded);
       rc = gnutls_certificate_set_x509_crl_file(x509_cred, CS crl_expanded,
         GNUTLS_X509_FMT_PEM);
-      if (rc < 0) return tls_error(US"CRL setup", host, rc);
+      if (rc < 0) return tls_error(US"CRL setup", host, gnutls_strerror(rc));
       }
     }
   }
@@ -787,6 +820,18 @@ if (verify_requirement != VERIFY_NONE)
 
 gnutls_db_set_cache_expiration(session, ssl_session_timeout);
 
+/* Reduce security in favour of increased compatibility, if the admin
+decides to make that trade-off. */
+if (gnutls_compat_mode)
+  {
+#if LIBGNUTLS_VERSION_NUMBER >= 0x020104
+  DEBUG(D_tls) debug_printf("lowering GnuTLS security, compatibility mode\n");
+  gnutls_session_enable_compatibility_mode(session);
+#else
+  DEBUG(D_tls) debug_printf("Unable to set gnutls_compat_mode - GnuTLS version too old\n");
+#endif
+  }
+
 DEBUG(D_tls) debug_printf("initialized GnuTLS session\n");
 return session;
 }
@@ -809,7 +854,10 @@ construct_cipher_name(gnutls_session session)
 {
 static uschar cipherbuf[256];
 uschar *ver;
-int bits, c, kx, mac;
+int bits, c, kx, mac, rc;
+#ifdef GNUTLS_CB_TLS_UNIQUE
+gnutls_datum_t channel;
+#endif
 
 ver = string_copy(
   US gnutls_protocol_get_name(gnutls_protocol_get_version(session)));
@@ -826,6 +874,21 @@ string_format(cipherbuf, sizeof(cipherbuf), "%s:%s:%u", ver,
 tls_cipher = cipherbuf;
 
 DEBUG(D_tls) debug_printf("cipher: %s\n", cipherbuf);
+
+if (tls_channelbinding_b64)
+  free(tls_channelbinding_b64);
+tls_channelbinding_b64 = NULL;
+
+#ifdef GNUTLS_CB_TLS_UNIQUE
+channel = { NULL, 0 };
+rc = gnutls_session_channel_binding(session, GNUTLS_CB_TLS_UNIQUE, &channel);
+if (rc) {
+  DEBUG(D_tls) debug_printf("Channel binding error: %s\n", gnutls_strerror(rc));
+} else {
+  tls_channelbinding_b64 = auth_b64encode(channel.data, (int)channel.size);
+  DEBUG(D_tls) debug_printf("Have channel bindings cached for possible auth usage.\n");
+}
+#endif
 }
 
 
@@ -855,7 +918,7 @@ tls_server_start(uschar *require_ciphers, uschar *require_mac,
   uschar *require_kx, uschar *require_proto)
 {
 int rc;
-uschar *error;
+const char *error;
 uschar *expciphers = NULL;
 uschar *expmac = NULL;
 uschar *expkx = NULL;
@@ -865,9 +928,7 @@ uschar *expproto = NULL;
 
 if (tls_active >= 0)
   {
-  log_write(0, LOG_MAIN, "STARTTLS received in already encrypted "
-    "connection from %s",
-    (sender_fullhost != NULL)? sender_fullhost : US"local process");
+  tls_error("STARTTLS received after TLS started", NULL, "");
   smtp_printf("554 Already in TLS\r\n");
   return FAIL;
   }
@@ -903,7 +964,8 @@ else if (verify_check_host(&tls_try_verify_hosts) == OK)
 tls_session = tls_session_init(GNUTLS_SERVER, expciphers, expmac, expkx,
   expproto);
 if (tls_session == NULL)
-  return tls_error(US"tls_session_init", NULL, GNUTLS_E_MEMORY_ERROR);
+  return tls_error(US"tls_session_init", NULL,
+    gnutls_strerror(GNUTLS_E_MEMORY_ERROR));
 
 /* Set context and tell client to go ahead, except in the case of TLS startup
 on connection, where outputting anything now upsets the clients and tends to
@@ -930,14 +992,8 @@ alarm(0);
 
 if (rc < 0)
   {
-  if (sigalrm_seen)
-    Ustrcpy(ssl_errstring, "timed out");
-  else
-    Ustrcpy(ssl_errstring, gnutls_strerror(rc));
-  log_write(0, LOG_MAIN,
-    "TLS error on connection from %s (gnutls_handshake): %s",
-    (sender_fullhost != NULL)? sender_fullhost : US"local process",
-    ssl_errstring);
+  tls_error(US"gnutls_handshake", NULL,
+    sigalrm_seen ? "timed out" : gnutls_strerror(rc));
 
   /* It seems that, except in the case of a timeout, we have to close the
   connection right here; otherwise if the other end is running OpenSSL it hangs
@@ -957,9 +1013,7 @@ DEBUG(D_tls) debug_printf("gnutls_handshake was successful\n");
 if (verify_requirement != VERIFY_NONE &&
      !verify_certificate(tls_session, &error))
   {
-  log_write(0, LOG_MAIN,
-    "TLS error on connection from %s: certificate verification failed (%s)",
-    (sender_fullhost != NULL)? sender_fullhost : US"local process", error);
+  tls_error(US"certificate verification failed", NULL, error);
   return FAIL;
   }
 
@@ -976,6 +1030,7 @@ receive_getc = tls_getc;
 receive_ungetc = tls_ungetc;
 receive_feof = tls_feof;
 receive_ferror = tls_ferror;
+receive_smtp_buffered = tls_smtp_buffered;
 
 tls_active = fileno(smtp_out);
 
@@ -1021,13 +1076,12 @@ uschar *expciphers = NULL;
 uschar *expmac = NULL;
 uschar *expkx = NULL;
 uschar *expproto = NULL;
-uschar *error;
+const char *error;
 unsigned int server_certs_size;
 int rc;
 
 DEBUG(D_tls) debug_printf("initializing GnuTLS as a client\n");
 
-client_host = host;
 verify_requirement = (verify_certs == NULL)? VERIFY_NONE : VERIFY_REQUIRED;
 rc = tls_init(host, certificate, privatekey, verify_certs, verify_crl);
 if (rc != OK) return rc;
@@ -1042,7 +1096,8 @@ tls_session = tls_session_init(GNUTLS_CLIENT, expciphers, expmac, expkx,
   expproto);
 
 if (tls_session == NULL)
-  return tls_error(US "tls_session_init", host, GNUTLS_E_MEMORY_ERROR);
+  return tls_error(US "tls_session_init", host,
+    gnutls_strerror(GNUTLS_E_MEMORY_ERROR));
 
 gnutls_transport_set_ptr(tls_session, (gnutls_transport_ptr)fd);
 
@@ -1054,15 +1109,8 @@ rc = gnutls_handshake(tls_session);
 alarm(0);
 
 if (rc < 0)
-  {
-  if (sigalrm_seen)
-    {
-    log_write(0, LOG_MAIN, "TLS error on connection to %s [%s]: "
-      "gnutls_handshake timed out", host->name, host->address);
-    return FAIL;
-    }
-  else return tls_error(US "gnutls_handshake", host, rc);
-  }
+  return tls_error(US "gnutls_handshake", host,
+    sigalrm_seen ? "timed out" : gnutls_strerror(rc));
 
 server_certs = gnutls_certificate_get_peers(tls_session, &server_certs_size);
 
@@ -1086,12 +1134,7 @@ if (server_certs != NULL)
 
 if (verify_requirement != VERIFY_NONE &&
       !verify_certificate(tls_session, &error))
-  {
-  log_write(0, LOG_MAIN,
-    "TLS error on connection to %s [%s]: certificate verification failed (%s)",
-    host->name, host->address, error);
-  return FAIL;
-  }
+  return tls_error(US"certificate verification failed", host, error);
 
 construct_cipher_name(tls_session);    /* Sets tls_cipher */
 tls_active = fd;
@@ -1117,21 +1160,15 @@ Returns:   nothing
 static void
 record_io_error(int ec, uschar *when, uschar *text)
 {
-uschar *additional = US"";
+const char *msg;
 
 if (ec == GNUTLS_E_FATAL_ALERT_RECEIVED)
-  additional = string_sprintf(": %s",
+  msg = string_sprintf("%s: %s", gnutls_strerror(ec),
     gnutls_alert_get_name(gnutls_alert_get(tls_session)));
-
-if (initialized == INITIALIZED_SERVER)
-  log_write(0, LOG_MAIN, "TLS %s error on connection from %s: %s%s", when,
-    (sender_fullhost != NULL)? sender_fullhost : US "local process",
-    (ec == 0)? text : US gnutls_strerror(ec), additional);
-
 else
-  log_write(0, LOG_MAIN, "TLS %s error on connection to %s [%s]: %s%s", when,
-    client_host->name, client_host->address,
-    (ec == 0)? text : US gnutls_strerror(ec), additional);
+  msg = gnutls_strerror(ec);
+
+tls_error(when, client_host, msg);
 }
 
 
@@ -1174,6 +1211,7 @@ if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm)
     receive_ungetc = smtp_ungetc;
     receive_feof = smtp_feof;
     receive_ferror = smtp_ferror;
+    receive_smtp_buffered = smtp_buffered;
 
     gnutls_deinit(tls_session);
     tls_session = NULL;
@@ -1192,7 +1230,9 @@ if (ssl_xfer_buffer_lwm >= ssl_xfer_buffer_hwm)
     ssl_xfer_error = 1;
     return EOF;
     }
-
+#ifndef DISABLE_DKIM
+  dkim_exim_verify_feed(ssl_xfer_buffer, inbytes);
+#endif
   ssl_xfer_buffer_hwm = inbytes;
   ssl_xfer_buffer_lwm = 0;
   }
@@ -1316,4 +1356,26 @@ gnutls_global_deinit();
 tls_active = -1;
 }
 
+
+
+
+/*************************************************
+*         Report the library versions.           *
+*************************************************/
+
+/* See a description in tls-openssl.c for an explanation of why this exists.
+
+Arguments:   a FILE* to print the results to
+Returns:     nothing
+*/
+
+void
+tls_version_report(FILE *f)
+{
+fprintf(f, "Library version: GnuTLS: Compile: %s\n"
+           "                         Runtime: %s\n",
+           LIBGNUTLS_VERSION,
+           gnutls_check_version(NULL));
+}
+
 /* End of tls-gnu.c */