]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/auth.c
Fix potential unaligned accesses in the string pool (Issue #5474)
[thirdparty/cups.git] / cups / auth.c
index 7e14939f378fd13be4f19b9f9d70ebd5ef23ccf5..b97a6b61c73978c7566447e40b88ddc897463e4a 100644 (file)
@@ -1,19 +1,13 @@
 /*
  * Authentication functions for CUPS.
  *
- * Copyright 2007-2017 by Apple Inc.
+ * Copyright 2007-2018 by Apple Inc.
  * Copyright 1997-2007 by Easy Software Products.
  *
  * This file contains Kerberos support code, copyright 2006 by
  * Jelmer Vernooij.
  *
- * 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
- * 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.
  */
 
 /*
  */
 
 #include "cups-private.h"
+#include "debug-internal.h"
 #include <fcntl.h>
 #include <sys/stat.h>
-#if defined(WIN32) || defined(__EMX__)
+#if defined(_WIN32) || defined(__EMX__)
 #  include <io.h>
 #else
 #  include <unistd.h>
-#endif /* WIN32 || __EMX__ */
+#endif /* _WIN32 || __EMX__ */
 
 #if HAVE_AUTHORIZATION_H
 #  include <Security/Authorization.h>
@@ -119,9 +114,7 @@ cupsDoAuthentication(
                *www_auth,              /* WWW-Authenticate header */
                *schemedata;            /* Scheme-specific data */
   char         scheme[256],            /* Scheme name */
-               prompt[1024],           /* Prompt for user */
-               realm[HTTP_MAX_VALUE],  /* realm="xyz" string */
-               nonce[HTTP_MAX_VALUE];  /* nonce="xyz" string */
+               prompt[1024];           /* Prompt for user */
   int          localauth;              /* Local authentication result */
   _cups_globals_t *cg;                 /* Global data */
 
@@ -218,7 +211,7 @@ cupsDoAuthentication(
       if (!cg->lang_default)
        cg->lang_default = cupsLangDefault();
 
-      if (cups_auth_param(scheme, "username", default_username, sizeof(default_username)))
+      if (cups_auth_param(schemedata, "username", default_username, sizeof(default_username)))
        cupsSetUser(default_username);
 
       snprintf(prompt, sizeof(prompt), _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), cupsUser(), http->hostname[0] == '/' ? "localhost" : http->hostname);
@@ -259,6 +252,7 @@ cupsDoAuthentication(
 
       httpEncode64_2(encode, sizeof(encode), http->userpass, (int)strlen(http->userpass));
       httpSetAuthString(http, "Basic", encode);
+      break;
     }
     else if (!_cups_strcasecmp(scheme, "Digest"))
     {
@@ -266,124 +260,15 @@ cupsDoAuthentication(
       * Digest authentication...
       */
 
-      int              i;              /* Looping var */
-      char             algorithm[65],  /* Hashing algorithm */
-                       opaque[HTTP_MAX_VALUE],
-                                       /* Opaque data from server */
-                       cnonce[65],     /* cnonce value */
-                       kd[65],         /* Final MD5/SHA-256 digest */
-                       ha1[65],        /* Hash of username:realm:password */
-                       ha2[65],        /* Hash of method:request-uri */
-                       hdata[65],      /* Hash of auth data */
-                       temp[1024],     /* Temporary string */
-                       digest[1024];   /* Digest auth data */
-      unsigned char    hash[32];       /* Hash buffer */
-      const char       *hashalg;       /* Hashing algorithm */
-      size_t           hashsize;       /* Size of hash */
-
-      if (strcmp(nonce, http->nonce))
-      {
-        strlcpy(http->nonce, nonce, sizeof(http->nonce));
-        http->nonce_count = 1;
-      }
-      else
-        http->nonce_count ++;
+      char nonce[HTTP_MAX_VALUE];      /* nonce="xyz" string */
 
-      cups_auth_param(schemedata, "opaque", opaque, sizeof(opaque));
+      cups_auth_param(schemedata, "algorithm", http->algorithm, sizeof(http->algorithm));
+      cups_auth_param(schemedata, "opaque", http->opaque, sizeof(http->opaque));
       cups_auth_param(schemedata, "nonce", nonce, sizeof(nonce));
-      cups_auth_param(schemedata, "realm", realm, sizeof(realm));
-
-      for (i = 0; i < 64; i ++)
-        cnonce[i] = "0123456789ABCDEF"[CUPS_RAND() & 15];
-      cnonce[64] = '\0';
-
-      if (cups_auth_param(schemedata, "algorithm", algorithm, sizeof(algorithm)))
-      {
-       /*
-        * Follow RFC 2617/7616...
-        */
-
-        if (!_cups_strcasecmp(algorithm, "MD5"))
-        {
-         /*
-          * RFC 2617 Digest with MD5
-          */
-
-          hashalg = "md5";
-       }
-       else if (!_cups_strcasecmp(algorithm, "SHA-256"))
-       {
-        /*
-         * RFC 7616 Digest with SHA-256
-         */
+      cups_auth_param(schemedata, "realm", http->realm, sizeof(http->realm));
 
-          hashalg = "sha2-256";
-       }
-       else
-       {
-        /*
-         * Some other algorithm we don't support, skip this one...
-         */
-
-         continue;
-       }
-
-       /*
-        * Calculate digest value...
-        */
-
-        /* H(A1) = H(username:realm:password) */
-        snprintf(temp, sizeof(temp), "%s:%s:%s", cupsUser(), realm, strchr(http->userpass, ':') + 1);
-        hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
-       cupsHashString(hash, hashsize, ha1, sizeof(ha1));
-
-        /* H(A2) = H(method:uri) */
-       snprintf(temp, sizeof(temp), "%s:%s", method, resource);
-        hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
-       cupsHashString(hash, hashsize, ha2, sizeof(ha2));
-
-        /* H(data) = H(nonce:nc:cnonce:qop:H(A2)) */
-       snprintf(temp, sizeof(temp), "%s:%08x:%s:auth:%s", nonce, http->nonce_count, cnonce, ha2);
-        hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
-       cupsHashString(hash, hashsize, hdata, sizeof(hdata));
-
-        /* KD = H(H(A1):H(data)) */
-       snprintf(temp, sizeof(temp), "%s:%s", ha1, hdata);
-        hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
-       cupsHashString(hash, hashsize, kd, sizeof(kd));
-
-        /* Pass the RFC 2617/7616 WWW-Authenticate header */
-        if (opaque[0])
-         snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=auth, opaque=\"%s\", cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, algorithm, opaque, cnonce, http->nonce_count, resource, kd);
-       else
-         snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=auth, cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, algorithm, cnonce, http->nonce_count, resource, kd);
-      }
-      else
-      {
-       /*
-        * Use old RFC 2069 Digest method...
-        */
-
-        /* H(A1) = H(username:realm:password) */
-        snprintf(temp, sizeof(temp), "%s:%s:%s", cupsUser(), realm, strchr(http->userpass, ':') + 1);
-        hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
-       cupsHashString(hash, hashsize, ha1, sizeof(ha1));
-
-        /* H(A2) = H(method:uri) */
-       snprintf(temp, sizeof(temp), "%s:%s", method, resource);
-        hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
-       cupsHashString(hash, hashsize, ha2, sizeof(ha2));
-
-        /* KD = H(H(A1):nonce:H(A2)) */
-       snprintf(temp, sizeof(temp), "%s:%s:%s", ha1, nonce, ha2);
-       hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
-       cupsHashString(hash, hashsize, kd, sizeof(kd));
-
-        /* Pass the RFC 2069 WWW-Authenticate header */
-       snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, resource, kd);
-      }
-
-      httpSetAuthString(http, "Digest", digest);
+      if (_httpSetDigestAuthString(http, nonce, method, resource))
+        break;
     }
   }
 
@@ -457,7 +342,7 @@ _cupsSetNegotiateAuthString(
                                      GSS_C_NO_BUFFER, &http->gssmech,
                                      &output_token, NULL, NULL);
 
-#ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
+#  ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
   if (major_status == GSS_S_NO_CRED)
   {
    /*
@@ -533,7 +418,7 @@ _cupsSetNegotiateAuthString(
       }
     }
   }
-#endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */
+#  endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */
 
   if (GSS_ERROR(major_status))
   {
@@ -543,11 +428,11 @@ _cupsSetNegotiateAuthString(
     return (-1);
   }
 
-#ifdef DEBUG
+#  ifdef DEBUG
   else if (major_status == GSS_S_CONTINUE_NEEDED)
     cups_gss_printf(major_status, minor_status,
                    "_cupsSetNegotiateAuthString: Continuation needed!");
-#endif /* DEBUG */
+#  endif /* DEBUG */
 
   if (output_token.length > 0 && output_token.length <= 65536)
   {
@@ -557,7 +442,7 @@ _cupsSetNegotiateAuthString(
     */
 
     int authsize = 10 +                        /* "Negotiate " */
-                  (int)output_token.length * 4 / 3 + 1 + 1;
+                  (((int)output_token.length * 4 / 3 + 3) & ~3) + 1;
                                        /* Base64 + nul */
 
     httpSetAuthString(http, NULL, NULL);
@@ -585,6 +470,7 @@ _cupsSetNegotiateAuthString(
 
   return (0);
 }
+#endif /* HAVE_GSSAPI */
 
 
 /*
@@ -602,25 +488,31 @@ cups_auth_find(const char *www_authenticate,      /* I - Pointer into WWW-Authenticat
   size_t       schemelen = strlen(scheme);     /* Length of scheme */
 
 
+  DEBUG_printf(("8cups_auth_find(www_authenticate=\"%s\", scheme=\"%s\"(%d))", www_authenticate, scheme, (int)schemelen));
+
   while (*www_authenticate)
   {
    /*
     * Skip leading whitespace and commas...
     */
 
+    DEBUG_printf(("9cups_auth_find: Before whitespace: \"%s\"", www_authenticate));
     while (isspace(*www_authenticate & 255) || *www_authenticate == ',')
       www_authenticate ++;
+    DEBUG_printf(("9cups_auth_find: After whitespace: \"%s\"", www_authenticate));
 
    /*
     * See if this is "Scheme" followed by whitespace or the end of the string.
     */
 
-    if (!strncmp(www_authenticate, scheme, schemelen) && (isspace(www_authenticate[schemelen] & 255) || !www_authenticate[schemelen]))
+    if (!strncmp(www_authenticate, scheme, schemelen) && (isspace(www_authenticate[schemelen] & 255) || www_authenticate[schemelen] == ',' || !www_authenticate[schemelen]))
     {
      /*
       * Yes, this is the start of the scheme-specific information...
       */
 
+      DEBUG_printf(("9cups_auth_find: Returning \"%s\".", www_authenticate));
+
       return (www_authenticate);
     }
 
@@ -636,14 +528,21 @@ cups_auth_find(const char *www_authenticate,      /* I - Pointer into WWW-Authenticat
         * Skip quoted value...
         */
 
-        while (www_authenticate[1] && www_authenticate[1] != '\"')
+        www_authenticate ++;
+        while (*www_authenticate && *www_authenticate != '\"')
           www_authenticate ++;
+
+        DEBUG_printf(("9cups_auth_find: After quoted: \"%s\"", www_authenticate));
       }
 
       www_authenticate ++;
     }
+
+    DEBUG_printf(("9cups_auth_find: After skip: \"%s\"", www_authenticate));
   }
 
+  DEBUG_puts("9cups_auth_find: Returning NULL.");
+
   return (NULL);
 }
 
@@ -665,6 +564,8 @@ cups_auth_param(const char *scheme,         /* I - Pointer to auth data */
   int          param;                          /* Is this a parameter? */
 
 
+  DEBUG_printf(("8cups_auth_param(scheme=\"%s\", name=\"%s\", value=%p, valsize=%d)", scheme, name, (void *)value, (int)valsize));
+
   while (!isspace(*scheme & 255) && *scheme)
     scheme ++;
 
@@ -705,6 +606,8 @@ cups_auth_param(const char *scheme,         /* I - Pointer to auth data */
 
       *valptr = '\0';
 
+      DEBUG_printf(("9cups_auth_param: Returning \"%s\".", value));
+
       return (value);
     }
 
@@ -724,7 +627,8 @@ cups_auth_param(const char *scheme,         /* I - Pointer to auth data */
         * Skip quoted value...
         */
 
-        while (scheme[1] && scheme[1] != '\"')
+        scheme ++;
+        while (*scheme && *scheme != '\"')
           scheme ++;
       }
 
@@ -742,6 +646,8 @@ cups_auth_param(const char *scheme,         /* I - Pointer to auth data */
 
   *value = '\0';
 
+  DEBUG_puts("9cups_auth_param: Returning NULL.");
+
   return (NULL);
 }
 
@@ -765,6 +671,8 @@ cups_auth_scheme(const char *www_authenticate,      /* I - Pointer into WWW-Authentic
   int          param;                          /* Is this a parameter? */
 
 
+  DEBUG_printf(("8cups_auth_scheme(www_authenticate=\"%s\", scheme=%p, schemesize=%d)", www_authenticate, (void *)scheme, (int)schemesize));
+
   while (*www_authenticate)
   {
    /*
@@ -778,7 +686,7 @@ cups_auth_scheme(const char *www_authenticate,      /* I - Pointer into WWW-Authentic
     * Parse the scheme name or param="value" string...
     */
 
-    for (sptr = scheme, start = www_authenticate, param = 0; *www_authenticate && !isspace(*www_authenticate & 255); www_authenticate ++)
+    for (sptr = scheme, start = www_authenticate, param = 0; *www_authenticate && *www_authenticate != ',' && !isspace(*www_authenticate & 255); www_authenticate ++)
     {
       if (*www_authenticate == '=')
         param = 1;
@@ -790,7 +698,8 @@ cups_auth_scheme(const char *www_authenticate,      /* I - Pointer into WWW-Authentic
         * Skip quoted value...
         */
 
-        while (www_authenticate[1] && www_authenticate[1] != '\"')
+        www_authenticate ++;
+        while (*www_authenticate && *www_authenticate != '\"')
           www_authenticate ++;
       }
     }
@@ -798,16 +707,22 @@ cups_auth_scheme(const char *www_authenticate,    /* I - Pointer into WWW-Authentic
     if (sptr > scheme && !param)
     {
       *sptr = '\0';
+
+      DEBUG_printf(("9cups_auth_scheme: Returning \"%s\".", start));
+
       return (start);
     }
   }
 
   *scheme = '\0';
 
+  DEBUG_puts("9cups_auth_scheme: Returning NULL.");
+
   return (NULL);
 }
 
 
+#ifdef HAVE_GSSAPI
 #  ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
 /*
  * 'cups_gss_acquire()' - Kerberos credentials callback.
@@ -981,9 +896,9 @@ static int                          /* O - 0 if available */
                                        /*    -1 error */
 cups_local_auth(http_t *http)          /* I - HTTP connection to server */
 {
-#if defined(WIN32) || defined(__EMX__)
+#if defined(_WIN32) || defined(__EMX__)
  /*
-  * Currently WIN32 and OS-2 do not support the CUPS server...
+  * Currently _WIN32 and OS-2 do not support the CUPS server...
   */
 
   return (1);
@@ -1018,6 +933,8 @@ cups_local_auth(http_t *http)              /* I - HTTP connection to server */
     return (1);
   }
 
+  www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
+
 #  if defined(HAVE_AUTHORIZATION_H)
  /*
   * Delete any previous authorization reference...
@@ -1029,9 +946,7 @@ cups_local_auth(http_t *http)              /* I - HTTP connection to server */
     http->auth_ref = NULL;
   }
 
-  www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
-
-  if (!getenv("GATEWAY_INTERFACE") && (schemedata = cups_auth_find(www_auth, "AuthKey")) != NULL && cups_auth_param(schemedata, "key", auth_key, sizeof(auth_key)))
+  if (!getenv("GATEWAY_INTERFACE") && (schemedata = cups_auth_find(www_auth, "AuthRef")) != NULL && cups_auth_param(schemedata, "key", auth_key, sizeof(auth_key)))
   {
     status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &http->auth_ref);
     if (status != errAuthorizationSuccess)
@@ -1091,10 +1006,6 @@ cups_local_auth(http_t *http)            /* I - HTTP connection to server */
   if (cups_auth_find(www_auth, "Negotiate"))
     return (1);
 #  endif /* HAVE_GSSAPI */
-#  ifdef HAVE_AUTHORIZATION_H
-  if (cups_auth_find(www_auth, "AuthKey"))
-    return (1);
-#  endif /* HAVE_AUTHORIZATION_H */
 
 #  if defined(SO_PEERCRED) && defined(AF_LOCAL)
  /*
@@ -1144,7 +1055,7 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
     * No certificate for this PID; see if we can get the root certificate...
     */
 
-    DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s", filename, strerror(errno)));
+    DEBUG_printf(("9cups_local_auth: Unable to open file \"%s\": %s", filename, strerror(errno)));
 
     if (!cups_auth_param(schemedata, "trc", trc, sizeof(trc)))
     {
@@ -1156,7 +1067,8 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
     }
 
     snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir);
-    fp = fopen(filename, "r");
+    if ((fp = fopen(filename, "r")) == NULL)
+      DEBUG_printf(("9cups_local_auth: Unable to open file \"%s\": %s", filename, strerror(errno)));
   }
 
   if (fp)
@@ -1187,5 +1099,5 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
   }
 
   return (1);
-#endif /* WIN32 || __EMX__ */
+#endif /* _WIN32 || __EMX__ */
 }