]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/tls-gnutls.c
License change: Apache License, Version 2.0.
[thirdparty/cups.git] / cups / tls-gnutls.c
index 0900f4fe3180e6a728306b8d11d6d196f48e36d9..28a471c91b795c3cfb884b513225e95f51ba008e 100644 (file)
@@ -1,18 +1,10 @@
 /*
- * "$Id$"
- *
  * TLS support code for CUPS using GNU TLS.
  *
- * Copyright 2007-2014 by Apple Inc.
+ * Copyright 2007-2017 by Apple Inc.
  * Copyright 1997-2007 by Easy Software Products, all rights reserved.
  *
- * These coded instructions, statements, and computer programs are the
- * property of Apple Inc. and are protected by Federal copyright
- * law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- * which should have been included with this file.  If this file is
- * file is missing or damaged, see the license at "http://www.cups.org/".
- *
- * This file is subject to the Apple OS-Developed Software exception.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more information.
  */
 
 /**** This file is included from tls.c ****/
@@ -32,10 +24,14 @@ static int          tls_auto_create = 0;
                                        /* Auto-create self-signed certs? */
 static char            *tls_common_name = NULL;
                                        /* Default common name */
+static gnutls_x509_crl_t tls_crl = NULL;/* Certificate revocation list */
 static char            *tls_keypath = NULL;
                                        /* Server cert keychain path */
 static _cups_mutex_t   tls_mutex = _CUPS_MUTEX_INITIALIZER;
                                        /* Mutex for keychain/certs */
+static int             tls_options = -1,/* Options for TLS connections */
+                       tls_min_version = _HTTP_TLS_1_0,
+                       tls_max_version = _HTTP_TLS_MAX;
 
 
 /*
@@ -44,6 +40,7 @@ static _cups_mutex_t  tls_mutex = _CUPS_MUTEX_INITIALIZER;
 
 static gnutls_x509_crt_t http_gnutls_create_credential(http_credential_t *credential);
 static const char      *http_gnutls_default_path(char *buffer, size_t bufsize);
+static void            http_gnutls_load_crl(void);
 static const char      *http_gnutls_make_path(char *buffer, size_t bufsize, const char *dirname, const char *filename, const char *ext);
 static ssize_t         http_gnutls_read(gnutls_transport_ptr_t ptr, void *data, size_t length);
 static ssize_t         http_gnutls_write(gnutls_transport_ptr_t ptr, const void *data, size_t length);
@@ -290,7 +287,7 @@ cupsSetServerCredentials(
  * 'httpCopyCredentials()' - Copy the credentials associated with the peer in
  *                           an encrypted connection.
  *
- * @since CUPS 1.5/OS X 10.7@
+ * @since CUPS 1.5/macOS 10.7@
  */
 
 int                                    /* O - Status of call (0 = success) */
@@ -374,6 +371,39 @@ httpCredentialsAreValidForName(
   if (cert)
   {
     result = gnutls_x509_crt_check_hostname(cert, common_name) != 0;
+
+    if (result)
+    {
+      int              i,              /* Looping var */
+                       count;          /* Number of revoked certificates */
+      unsigned char    cserial[1024],  /* Certificate serial number */
+                       rserial[1024];  /* Revoked serial number */
+      size_t           cserial_size,   /* Size of cert serial number */
+                       rserial_size;   /* Size of revoked serial number */
+
+      _cupsMutexLock(&tls_mutex);
+
+      count = gnutls_x509_crl_get_crt_count(tls_crl);
+
+      if (count > 0)
+      {
+        cserial_size = sizeof(cserial);
+        gnutls_x509_crt_get_serial(cert, cserial, &cserial_size);
+
+        for (i = 0; i < count; i ++)
+       {
+         rserial_size = sizeof(rserial);
+          if (!gnutls_x509_crl_get_crt_serial(tls_crl, (unsigned)i, rserial, &rserial_size, NULL) && cserial_size == rserial_size && !memcmp(cserial, rserial, rserial_size))
+         {
+           result = 0;
+           break;
+         }
+       }
+      }
+
+      _cupsMutexUnlock(&tls_mutex);
+    }
+
     gnutls_x509_crt_deinit(cert);
   }
 
@@ -401,10 +431,22 @@ httpCredentialsGetTrust(
 
 
   if (!common_name)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No common name specified."), 1);
     return (HTTP_TRUST_UNKNOWN);
+  }
 
   if ((cert = http_gnutls_create_credential((http_credential_t *)cupsArrayFirst(credentials))) == NULL)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create credentials from array."), 1);
     return (HTTP_TRUST_UNKNOWN);
+  }
+
+  if (cg->any_root < 0)
+  {
+    _cupsSetDefaults();
+    http_gnutls_load_crl();
+  }
 
  /*
   * Look this common name up in the default keychains...
@@ -427,14 +469,34 @@ httpCredentialsGetTrust(
       * credentials and allow if the new ones have a later expiration...
       */
 
-      if (httpCredentialsGetExpiration(credentials) <= httpCredentialsGetExpiration(tcreds) ||
-          !httpCredentialsAreValidForName(credentials, common_name))
+      if (!cg->trust_first)
       {
        /*
-        * Either the new credentials are not newly issued, or the common name
-       * does not match the issued certificate...
+        * Do not trust certificates on first use...
        */
 
+        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Trust on first use is disabled."), 1);
+
+        trust = HTTP_TRUST_INVALID;
+      }
+      else if (httpCredentialsGetExpiration(credentials) <= httpCredentialsGetExpiration(tcreds))
+      {
+       /*
+        * The new credentials are not newly issued...
+       */
+
+        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("New credentials are older than stored credentials."), 1);
+
+        trust = HTTP_TRUST_INVALID;
+      }
+      else if (!httpCredentialsAreValidForName(credentials, common_name))
+      {
+       /*
+        * The common name does not match the issued certificate...
+       */
+
+        _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("New credentials are not valid for name."), 1);
+
         trust = HTTP_TRUST_INVALID;
       }
       else if (httpCredentialsGetExpiration(tcreds) < time(NULL))
@@ -452,7 +514,53 @@ httpCredentialsGetTrust(
     httpFreeCredentials(tcreds);
   }
   else if (cg->validate_certs && !httpCredentialsAreValidForName(credentials, common_name))
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No stored credentials, not valid for name."), 1);
     trust = HTTP_TRUST_INVALID;
+  }
+  else if (!cg->trust_first)
+  {
+   /*
+    * See if we have a site CA certificate we can compare...
+    */
+
+    if (!httpLoadCredentials(NULL, &tcreds, "site"))
+    {
+      if (cupsArrayCount(credentials) != (cupsArrayCount(tcreds) + 1))
+      {
+       /*
+        * Certificate isn't directly generated from the CA cert...
+       */
+
+        trust = HTTP_TRUST_INVALID;
+      }
+      else
+      {
+       /*
+        * Do a tail comparison of the two certificates...
+       */
+
+        http_credential_t      *a, *b;         /* Certificates */
+
+        for (a = (http_credential_t *)cupsArrayFirst(tcreds), b = (http_credential_t *)cupsArrayIndex(credentials, 1);
+            a && b;
+            a = (http_credential_t *)cupsArrayNext(tcreds), b = (http_credential_t *)cupsArrayNext(credentials))
+         if (a->datalen != b->datalen || memcmp(a->data, b->data, a->datalen))
+           break;
+
+        if (a || b)
+         trust = HTTP_TRUST_INVALID;
+      }
+
+      if (trust != HTTP_TRUST_OK)
+       _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Credentials do not validate against site CA certificate."), 1);
+    }
+    else
+    {
+      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Trust on first use is disabled."), 1);
+      trust = HTTP_TRUST_INVALID;
+    }
+  }
 
   if (trust == HTTP_TRUST_OK && !cg->expired_certs)
   {
@@ -461,11 +569,17 @@ httpCredentialsGetTrust(
     time(&curtime);
     if (curtime < gnutls_x509_crt_get_activation_time(cert) ||
         curtime > gnutls_x509_crt_get_expiration_time(cert))
+    {
+      _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Credentials have expired."), 1);
       trust = HTTP_TRUST_EXPIRED;
+    }
   }
 
   if (trust == HTTP_TRUST_OK && !cg->any_root && cupsArrayCount(credentials) == 1)
+  {
+    _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Self-signed credentials are blocked."), 1);
     trust = HTTP_TRUST_INVALID;
+  }
 
   gnutls_x509_crt_deinit(cert);
 
@@ -528,7 +642,6 @@ httpCredentialsString(
     char               name[256];      /* Common name associated with cert */
     size_t             namelen;        /* Length of name */
     time_t             expiration;     /* Expiration date of cert */
-    _cups_md5_state_t  md5_state;      /* MD5 state */
     unsigned char      md5_digest[16]; /* MD5 result */
 
     namelen = sizeof(name) - 1;
@@ -539,9 +652,7 @@ httpCredentialsString(
 
     expiration = gnutls_x509_crt_get_expiration_time(cert);
 
-    _cupsMD5Init(&md5_state);
-    _cupsMD5Append(&md5_state, first->data, (int)first->datalen);
-    _cupsMD5Finish(&md5_state, md5_digest);
+    cupsHashData("md5", first->data, first->datalen, md5_digest, sizeof(md5_digest));
 
     snprintf(buffer, bufsize, "%s / %s / %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", name, httpGetDateString(expiration), md5_digest[0], md5_digest[1], md5_digest[2], md5_digest[3], md5_digest[4], md5_digest[5], md5_digest[6], md5_digest[7], md5_digest[8], md5_digest[9], md5_digest[10], md5_digest[11], md5_digest[12], md5_digest[13], md5_digest[14], md5_digest[15]);
 
@@ -574,6 +685,8 @@ httpLoadCredentials(
   size_t               alloc_data = 0, /* Bytes allocated */
                        num_data = 0;   /* Bytes used */
   int                  decoded;        /* Bytes decoded */
+  int                  in_certificate = 0;
+                                       /* In a certificate? */
 
 
   if (!credentials || !common_name)
@@ -593,7 +706,7 @@ httpLoadCredentials(
   {
     if (!strcmp(line, "-----BEGIN CERTIFICATE-----"))
     {
-      if (num_data)
+      if (in_certificate)
       {
        /*
        * Missing END CERTIFICATE...
@@ -603,10 +716,12 @@ httpLoadCredentials(
        *credentials = NULL;
         break;
       }
+
+      in_certificate = 1;
     }
     else if (!strcmp(line, "-----END CERTIFICATE-----"))
     {
-      if (!num_data)
+      if (!in_certificate || !num_data)
       {
        /*
        * Missing data...
@@ -627,9 +742,10 @@ httpLoadCredentials(
         break;
       }
 
-      num_data = 0;
+      num_data       = 0;
+      in_certificate = 0;
     }
-    else
+    else if (in_certificate)
     {
       if (alloc_data == 0)
       {
@@ -663,7 +779,7 @@ httpLoadCredentials(
 
   cupsFileClose(fp);
 
-  if (num_data)
+  if (in_certificate)
   {
    /*
     * Missing END CERTIFICATE...
@@ -820,6 +936,101 @@ http_gnutls_default_path(char   *buffer,/* I - Path buffer */
 }
 
 
+/*
+ * 'http_gnutls_load_crl()' - Load the certificate revocation list, if any.
+ */
+
+static void
+http_gnutls_load_crl(void)
+{
+  _cupsMutexLock(&tls_mutex);
+
+  if (!gnutls_x509_crl_init(&tls_crl))
+  {
+    cups_file_t                *fp;            /* CRL file */
+    char               filename[1024], /* site.crl */
+                       line[256];      /* Base64-encoded line */
+    unsigned char      *data = NULL;   /* Buffer for cert data */
+    size_t             alloc_data = 0, /* Bytes allocated */
+                       num_data = 0;   /* Bytes used */
+    int                        decoded;        /* Bytes decoded */
+    gnutls_datum_t     datum;          /* Data record */
+
+
+    http_gnutls_make_path(filename, sizeof(filename), CUPS_SERVERROOT, "site", "crl");
+
+    if ((fp = cupsFileOpen(filename, "r")) != NULL)
+    {
+      while (cupsFileGets(fp, line, sizeof(line)))
+      {
+       if (!strcmp(line, "-----BEGIN X509 CRL-----"))
+       {
+         if (num_data)
+         {
+          /*
+           * Missing END X509 CRL...
+           */
+
+           break;
+         }
+       }
+       else if (!strcmp(line, "-----END X509 CRL-----"))
+       {
+         if (!num_data)
+         {
+          /*
+           * Missing data...
+           */
+
+           break;
+         }
+
+          datum.data = data;
+         datum.size = num_data;
+
+         gnutls_x509_crl_import(tls_crl, &datum, GNUTLS_X509_FMT_PEM);
+
+         num_data = 0;
+       }
+       else
+       {
+         if (alloc_data == 0)
+         {
+           data       = malloc(2048);
+           alloc_data = 2048;
+
+           if (!data)
+             break;
+         }
+         else if ((num_data + strlen(line)) >= alloc_data)
+         {
+           unsigned char *tdata = realloc(data, alloc_data + 1024);
+                                           /* Expanded buffer */
+
+           if (!tdata)
+             break;
+
+           data       = tdata;
+           alloc_data += 1024;
+         }
+
+         decoded = alloc_data - num_data;
+         httpDecode64_2((char *)data + num_data, &decoded, line);
+         num_data += (size_t)decoded;
+       }
+      }
+
+      cupsFileClose(fp);
+
+      if (data)
+       free(data);
+    }
+  }
+
+  _cupsMutexUnlock(&tls_mutex);
+}
+
+
 /*
  * 'http_gnutls_make_path()' - Format a filename for a certificate or key file.
  */
@@ -1001,6 +1212,22 @@ _httpTLSSetCredentials(http_t *http)     /* I - Connection to server */
 }
 
 
+/*
+ * '_httpTLSSetOptions()' - Set TLS protocol and cipher suite options.
+ */
+
+void
+_httpTLSSetOptions(int options)                /* I - Options */
+{
+  if (!(options & _HTTP_TLS_SET_DEFAULT) || tls_options < 0)
+  {
+    tls_options     = options;
+    tls_min_version = min_version;
+    tls_max_version = max_version;
+  }
+}
+
+
 /*
  * '_httpTLSStart()' - Set up SSL/TLS support on a connection.
  */
@@ -1013,9 +1240,28 @@ _httpTLSStart(http_t *http)              /* I - Connection to server */
   int                  status;         /* Status of handshake */
   gnutls_certificate_credentials_t *credentials;
                                        /* TLS credentials */
+  char                 priority_string[2048];
+                                       /* Priority string */
+  int                  version;        /* Current version */
+  static const char * const versions[] =/* SSL/TLS versions */
+  {
+    "VERS-SSL3.0",
+    "VERS-TLS1.0",
+    "VERS-TLS1.1",
+    "VERS-TLS1.2",
+    "VERS-TLS1.3",
+    "VERS-TLS-ALL"
+  };
 
 
-  DEBUG_printf(("7_httpTLSStart(http=%p)", http));
+  DEBUG_printf(("3_httpTLSStart(http=%p)", http));
+
+  if (tls_options < 0)
+  {
+    DEBUG_puts("4_httpTLSStart: Setting defaults.");
+    _cupsSetDefaults();
+    DEBUG_printf(("4_httpTLSStart: tls_options=%x", tls_options));
+  }
 
   if (http->mode == _HTTP_MODE_SERVER && !tls_keypath)
   {
@@ -1095,7 +1341,6 @@ _httpTLSStart(http_t *http)               /* I - Connection to server */
                keyfile[1024];          /* Private key file */
     int                have_creds = 0;         /* Have credentials? */
 
-
     if (http->fields[HTTP_FIELD_HOST][0])
     {
      /*
@@ -1133,17 +1378,97 @@ _httpTLSStart(http_t *http)             /* I - Connection to server */
 
     if (hostname[0])
     {
+     /*
+      * First look in the CUPS keystore...
+      */
+
       http_gnutls_make_path(crtfile, sizeof(crtfile), tls_keypath, hostname, "crt");
       http_gnutls_make_path(keyfile, sizeof(keyfile), tls_keypath, hostname, "key");
 
-      have_creds = !access(crtfile, 0) && !access(keyfile, 0);
+      if (access(crtfile, R_OK) || access(keyfile, R_OK))
+      {
+       /*
+        * No CUPS-managed certs, look for CA certs...
+        */
+
+        char cacrtfile[1024], cakeyfile[1024]; /* CA cert files */
+
+        snprintf(cacrtfile, sizeof(cacrtfile), "/etc/letsencrypt/live/%s/fullchain.pem", hostname);
+        snprintf(cakeyfile, sizeof(cakeyfile), "/etc/letsencrypt/live/%s/privkey.pem", hostname);
+
+        if ((access(cacrtfile, R_OK) || access(cakeyfile, R_OK)) && (hostptr = strchr(hostname, '.')) != NULL)
+        {
+         /*
+          * Try just domain name...
+          */
+
+          hostptr ++;
+          if (strchr(hostptr, '.'))
+          {
+            snprintf(cacrtfile, sizeof(cacrtfile), "/etc/letsencrypt/live/%s/fullchain.pem", hostptr);
+            snprintf(cakeyfile, sizeof(cakeyfile), "/etc/letsencrypt/live/%s/privkey.pem", hostptr);
+          }
+        }
+
+        if (!access(cacrtfile, R_OK) && !access(cakeyfile, R_OK))
+        {
+         /*
+          * Use the CA certs...
+          */
+
+          strlcpy(crtfile, cacrtfile, sizeof(crtfile));
+          strlcpy(keyfile, cakeyfile, sizeof(keyfile));
+        }
+      }
+
+      have_creds = !access(crtfile, R_OK) && !access(keyfile, R_OK);
     }
     else if (tls_common_name)
     {
+     /*
+      * First look in the CUPS keystore...
+      */
+
       http_gnutls_make_path(crtfile, sizeof(crtfile), tls_keypath, tls_common_name, "crt");
       http_gnutls_make_path(keyfile, sizeof(keyfile), tls_keypath, tls_common_name, "key");
 
-      have_creds = !access(crtfile, 0) && !access(keyfile, 0);
+      if (access(crtfile, R_OK) || access(keyfile, R_OK))
+      {
+       /*
+        * No CUPS-managed certs, look for CA certs...
+        */
+
+        char cacrtfile[1024], cakeyfile[1024]; /* CA cert files */
+
+        snprintf(cacrtfile, sizeof(cacrtfile), "/etc/letsencrypt/live/%s/fullchain.pem", tls_common_name);
+        snprintf(cakeyfile, sizeof(cakeyfile), "/etc/letsencrypt/live/%s/privkey.pem", tls_common_name);
+
+        if ((access(cacrtfile, R_OK) || access(cakeyfile, R_OK)) && (hostptr = strchr(tls_common_name, '.')) != NULL)
+        {
+         /*
+          * Try just domain name...
+          */
+
+          hostptr ++;
+          if (strchr(hostptr, '.'))
+          {
+            snprintf(cacrtfile, sizeof(cacrtfile), "/etc/letsencrypt/live/%s/fullchain.pem", hostptr);
+            snprintf(cakeyfile, sizeof(cakeyfile), "/etc/letsencrypt/live/%s/privkey.pem", hostptr);
+          }
+        }
+
+        if (!access(cacrtfile, R_OK) && !access(cakeyfile, R_OK))
+        {
+         /*
+          * Use the CA certs...
+          */
+
+          strlcpy(crtfile, cacrtfile, sizeof(crtfile));
+          strlcpy(keyfile, cakeyfile, sizeof(keyfile));
+        }
+      }
+
+      have_creds = !access(crtfile, R_OK) && !access(keyfile, R_OK);
     }
 
     if (!have_creds && tls_auto_create && (hostname[0] || tls_common_name))
@@ -1163,7 +1488,8 @@ _httpTLSStart(http_t *http)               /* I - Connection to server */
 
     DEBUG_printf(("4_httpTLSStart: Using certificate \"%s\" and private key \"%s\".", crtfile, keyfile));
 
-    status = gnutls_certificate_set_x509_key_file(*credentials, crtfile, keyfile, GNUTLS_X509_FMT_PEM);
+    if (!status)
+      status = gnutls_certificate_set_x509_key_file(*credentials, crtfile, keyfile, GNUTLS_X509_FMT_PEM);
   }
 
   if (!status)
@@ -1185,6 +1511,64 @@ _httpTLSStart(http_t *http)              /* I - Connection to server */
     return (-1);
   }
 
+  strlcpy(priority_string, "NORMAL", sizeof(priority_string));
+
+  if (tls_max_version < _HTTP_TLS_MAX)
+  {
+   /*
+    * Require specific TLS versions...
+    */
+
+    strlcat(priority_string, ":-VERS-TLS-ALL", sizeof(priority_string));
+    for (version = tls_min_version; version <= tls_max_version; version ++)
+    {
+      strlcat(priority_string, ":+", sizeof(priority_string));
+      strlcat(priority_string, versions[version], sizeof(priority_string));
+    }
+  }
+  else if (tls_min_version == _HTTP_TLS_SSL3)
+  {
+   /*
+    * Allow all versions of TLS and SSL/3.0...
+    */
+
+    strlcat(priority_string, ":+VERS-TLS-ALL:+VERS-SSL3.0", sizeof(priority_string));
+  }
+  else
+  {
+   /*
+    * Require a minimum version...
+    */
+
+    strlcat(priority_string, ":+VERS-TLS-ALL", sizeof(priority_string));
+    for (version = 0; version < tls_min_version; version ++)
+    {
+      strlcat(priority_string, ":-", sizeof(priority_string));
+      strlcat(priority_string, versions[version], sizeof(priority_string));
+    }
+  }
+
+  if (tls_options & _HTTP_TLS_ALLOW_RC4)
+    strlcat(priority_string, ":+ARCFOUR-128", sizeof(priority_string));
+  else
+    strlcat(priority_string, ":!ARCFOUR-128", sizeof(priority_string));
+
+  strlcat(priority_string, ":!ANON-DH", sizeof(priority_string));
+
+  if (tls_options & _HTTP_TLS_DENY_CBC)
+    strlcat(priority_string, ":!AES-128-CBC:!AES-256-CBC:!CAMELLIA-128-CBC:!CAMELLIA-256-CBC:!3DES-CBC", sizeof(priority_string));
+
+#ifdef HAVE_GNUTLS_PRIORITY_SET_DIRECT
+  gnutls_priority_set_direct(http->tls, priority_string, NULL);
+
+#else
+  gnutls_priority_t priority;          /* Priority */
+
+  gnutls_priority_init(&priority, priority_string, NULL);
+  gnutls_priority_set(http->tls, priority);
+  gnutls_priority_deinit(priority);
+#endif /* HAVE_GNUTLS_PRIORITY_SET_DIRECT */
+
   gnutls_transport_set_ptr(http->tls, (gnutls_transport_ptr_t)http);
   gnutls_transport_set_pull_function(http->tls, http_gnutls_read);
 #ifdef HAVE_GNUTLS_TRANSPORT_SET_PULL_TIMEOUT_FUNCTION
@@ -1289,8 +1673,3 @@ _httpTLSWrite(http_t     *http,           /* I - Connection to server */
 
   return ((int)result);
 }
-
-
-/*
- * End of "$Id$".
- */