]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Fix-fix for MD5.
authoramosjeffries <>
Thu, 15 Nov 2007 16:18:04 +0000 (16:18 +0000)
committeramosjeffries <>
Thu, 15 Nov 2007 16:18:04 +0000 (16:18 +0000)
Pre-compiler only tested on Linux and FreeBSD.

This update amends the previous to allow OS which provide a partial MD5
implementation but do not supply correct buffer size (MD5_DIGEST_* macro)
to build using the squid bundled code.

To evade symbol-clashes the squid code is also updated to use xMD5Init,
xMD5Update, xMD5Final and the code sorts out which version is to be used
at compile time from configure options and available sources.

For MacOS X and other broken OS the sys/types.h must also be included on
behalf of the sys/*.h which need it.

helpers/basic_auth/NCSA/crypt_md5.c
include/md5.h
lib/md5-test.c
lib/md5.c
lib/rfc2617.c
src/MemObject.cc
src/store_key_md5.cc
src/wccp2.cc

index 8936c84bee69c2d54e8d427b737b105d03da3d3c..e956000b94be138b514e2367169f5034e4d65872 100644 (file)
@@ -77,25 +77,25 @@ char *crypt_md5(const char *pw, const char *salt)
     /* get the length of the true salt */
     sl = ep - sp;
 
-    MD5Init(&ctx);
+    xMD5Init(&ctx);
 
     /* The password first, since that is what is most unknown */
-    MD5Update(&ctx, (unsigned const char *) pw, strlen(pw));
+    xMD5Update(&ctx, (unsigned const char *) pw, strlen(pw));
 
     /* Then our magic string */
-    MD5Update(&ctx, (unsigned const char *) magic, magiclen);
+    xMD5Update(&ctx, (unsigned const char *) magic, magiclen);
 
     /* Then the raw salt */
-    MD5Update(&ctx, (unsigned const char *) sp, sl);
+    xMD5Update(&ctx, (unsigned const char *) sp, sl);
 
     /* Then just as many characters of the MD5(pw,salt,pw) */
-    MD5Init(&ctx1);
-    MD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
-    MD5Update(&ctx1, (unsigned const char *) sp, sl);
-    MD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
-    MD5Final(final, &ctx1);
+    xMD5Init(&ctx1);
+    xMD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
+    xMD5Update(&ctx1, (unsigned const char *) sp, sl);
+    xMD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
+    xMD5Final(final, &ctx1);
     for (pl = strlen(pw); pl > 0; pl -= 16)
-       MD5Update(&ctx, (unsigned const char *) final, pl > 16 ? 16 : pl);
+       xMD5Update(&ctx, (unsigned const char *) final, pl > 16 ? 16 : pl);
 
     /* Don't leave anything around in vm they could use. */
     memset(final, 0, sizeof final);
@@ -103,9 +103,9 @@ char *crypt_md5(const char *pw, const char *salt)
     /* Then something really weird... */
     for (j = 0, i = strlen(pw); i; i >>= 1)
        if (i & 1)
-           MD5Update(&ctx, (unsigned const char *) final + j, 1);
+           xMD5Update(&ctx, (unsigned const char *) final + j, 1);
        else
-           MD5Update(&ctx, (unsigned const char *) pw + j, 1);
+           xMD5Update(&ctx, (unsigned const char *) pw + j, 1);
 
     /* Now make the output string */
     memset(passwd, 0, sizeof(passwd));
@@ -113,7 +113,7 @@ char *crypt_md5(const char *pw, const char *salt)
     strncat(passwd, sp, sl);
     strcat(passwd, "$");
 
-    MD5Final(final, &ctx);
+    xMD5Final(final, &ctx);
 
     /*
      * and now, just to make sure things don't run too fast
@@ -121,23 +121,23 @@ char *crypt_md5(const char *pw, const char *salt)
      * need 30 seconds to build a 1000 entry dictionary...
      */
     for (i = 0; i < 1000; i++) {
-       MD5Init(&ctx1);
+       xMD5Init(&ctx1);
        if (i & 1)
-           MD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
+           xMD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
        else
-           MD5Update(&ctx1, (unsigned const char *) final, 16);
+           xMD5Update(&ctx1, (unsigned const char *) final, 16);
 
        if (i % 3)
-           MD5Update(&ctx1, (unsigned const char *) sp, sl);
+           xMD5Update(&ctx1, (unsigned const char *) sp, sl);
 
        if (i % 7)
-           MD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
+           xMD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
 
        if (i & 1)
-           MD5Update(&ctx1, (unsigned const char *) final, 16);
+           xMD5Update(&ctx1, (unsigned const char *) final, 16);
        else
-           MD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
-       MD5Final(final, &ctx1);
+           xMD5Update(&ctx1, (unsigned const char *) pw, strlen(pw));
+       xMD5Final(final, &ctx1);
     }
 
     p = passwd + strlen(passwd);
@@ -179,9 +179,9 @@ char *md5sum(const char *s){
 
    memset(digest,0,16);
 
-   MD5Init(&ctx);
-   MD5Update(&ctx,(const unsigned char *)s,strlen(s));
-   MD5Final(digest,&ctx);
+   xMD5Init(&ctx);
+   xMD5Update(&ctx,(const unsigned char *)s,strlen(s));
+   xMD5Final(digest,&ctx);
 
    for(idx=0;idx<16;idx++)
        sprintf(&sum[idx*2],"%02x",digest[idx]);
index 3dee87fa65c5770ef914e9b9a0f542a77d42eb2a..be35d1f653e00acb87d26d10848357b32b4f8e6e 100644 (file)
@@ -1,21 +1,17 @@
 #ifndef SQUID_MD5_H
 #define SQUID_MD5_H
 
+#if USE_OPENSSL && HAVE_OPENSSL_MD5_H
+
 /*
  * If Squid is compiled with OpenSSL then we use the MD5 routines
  * from there via some wrapper macros, and the rest of this file is ignored..
  */
-#define USE_SQUID_MD5 0
-
-#if USE_OPENSSL && HAVE_OPENSSL_MD5_H
 #include <openssl/md5.h>
 
-/* Hack to adopt Squid to the OpenSSL syntax */
-#define MD5_DIGEST_CHARS MD5_DIGEST_LENGTH
-
-#define MD5Init MD5_Init
-#define MD5Update MD5_Update
-#define MD5Final MD5_Final
+#define xMD5Init MD5_Init
+#define xMD5Update MD5_Update
+#define xMD5Final MD5_Final
 
 #elif USE_OPENSSL && !HAVE_OPENSSL_MD5_H
 #error Cannot find OpenSSL MD5 headers
 #elif HAVE_SYS_MD5_H
 /*
  * Solaris 10 provides MD5 as part of the system.
+ * So do other OS - but without MD5_DIGEST_LENGTH defined
+ * for them we need to still use the bunded version
  */
+#if HAVE_SYS_TYPES_H
+#include <sys/types.h>
+#endif
+
 #include <sys/md5.h>
 
-/*
- * They also define MD5_CTX with different field names
- * fortunately we do not access it directly in the squid code.
- */
+#endif
+
+/* according to CacheDigest.cc squid REQUIRES 16-byte here for hash keys */
+#if MD5_DIGEST_LENGTH == 16
+
+  /* We found a nice usable version. No need for ours */
+#define USE_SQUID_MD5 0
 
-/* Hack to adopt Squid to the OpenSSL syntax */
+  /* adopt the supplied version we are able to use. */
+#define xMD5Init MD5Init
+#define xMD5Update MD5Update
+#define xMD5Final MD5Final
 #define MD5_DIGEST_CHARS MD5_DIGEST_LENGTH
 
-#else /* NEED_OWN_MD5 */
+#else /* NEED squid bundled version */
 
- /* Turn on internal MD5 code */
-#undef  USE_SQUID_MD5
+  /* Turn on internal MD5 code */
 #define USE_SQUID_MD5 1
 
+  /* remove MD5_CTX which may have been defined. */
+#undef MD5_CTX
+
 /*
  * This is the header file for the MD5 message-digest algorithm.
  * The algorithm is due to Ron Rivest.  This code was
  * minor cleanup. - Henrik Nordstrom <henrik@henriknordstrom.net>.
  * Still in the public domain.
  *
+ * Changed function names to xMD5* to prevent symbol-clashes when
+ * external library code actually used.
+ * - Amos Jeffries <squid3@treenet.co.nz>
+ *
  */
 
 #include "squid_types.h"
@@ -75,12 +89,12 @@ typedef struct MD5Context {
     uint32_t in[16];
 } MD5_CTX;
 
-SQUIDCEXTERN void MD5Init(struct MD5Context *context);
-SQUIDCEXTERN void MD5Update(struct MD5Context *context, const void *buf, unsigned len);
-SQUIDCEXTERN void MD5Final(uint8_t digest[16], struct MD5Context *context);
-SQUIDCEXTERN void MD5Transform(uint32_t buf[4], uint32_t const in[16]);
+SQUIDCEXTERN void xMD5Init(struct MD5Context *context);
+SQUIDCEXTERN void xMD5Update(struct MD5Context *context, const void *buf, unsigned len);
+SQUIDCEXTERN void xMD5Final(uint8_t digest[16], struct MD5Context *context);
+SQUIDCEXTERN void xMD5Transform(uint32_t buf[4], uint32_t const in[16]);
+
+#endif /* MD5_DIGEST_CHARS != 16 */
 
-#define MD5_DIGEST_CHARS         16
 
-#endif /* USE_OPENSSL */
 #endif /* SQUID_MD5_H */
index 1ba2bdac29dfb6956ad2610174521a28ee802644..5c4523246650450f1256db107c08668291c41bec 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: md5-test.c,v 1.3 2003/01/23 00:37:01 robertc Exp $
+ * $Id: md5-test.c,v 1.4 2007/11/15 09:18:05 amosjeffries Exp $
  */
 
 /*
@@ -20,9 +20,9 @@ MDString(char *string)
     MD5_CTX context;
     unsigned char digest[16];
     unsigned int len = strlen(string);
-    MD5Init(&context);
-    MD5Update(&context, string, len);
-    MD5Final(digest, &context);
+    xMD5Init(&context);
+    xMD5Update(&context, string, len);
+    xMD5Final(digest, &context);
     printf("MD5 (\"%s\") = ", string);
     MDPrint(digest);
     printf("\n");
index 0b477028e6d361b0b0334e49f58ba6904981c17e..257dc436bc18fe62bb2b630c72afa818df7befe7 100644 (file)
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -75,7 +75,7 @@ byteSwap(uint32_t * buf, unsigned words)
  * initialization constants.
  */
 void
-MD5Init(struct MD5Context *ctx)
+xMD5Init(struct MD5Context *ctx)
 {
     ctx->buf[0] = 0x67452301;
     ctx->buf[1] = 0xefcdab89;
@@ -91,7 +91,7 @@ MD5Init(struct MD5Context *ctx)
  * of bytes.
  */
 void
-MD5Update(struct MD5Context *ctx, const void *_buf, unsigned len)
+xMD5Update(struct MD5Context *ctx, const void *_buf, unsigned len)
 {
     uint8_t const *buf = _buf;
     uint32_t t;
@@ -132,7 +132,7 @@ MD5Update(struct MD5Context *ctx, const void *_buf, unsigned len)
  * 1 0* (64-bit count of bits processed, MSB-first)
  */
 void
-MD5Final(unsigned char digest[16], struct MD5Context *ctx)
+xMD5Final(unsigned char digest[16], struct MD5Context *ctx)
 {
     int count = ctx->bytes[0] & 0x3f;  /* Number of bytes in ctx->in */
     uint8_t *p = (uint8_t *) ctx->in + count;
@@ -183,7 +183,7 @@ MD5Final(unsigned char digest[16], struct MD5Context *ctx)
  * the data and converts bytes into longwords for this routine.
  */
 void
-MD5Transform(uint32_t buf[4], uint32_t const in[16])
+xMD5Transform(uint32_t buf[4], uint32_t const in[16])
 {
     register uint32_t a, b, c, d;
 
index 5c14e86a2b35522b91061528886c3e2847d6ec80..8986817f053f204b42d5b3dcedf0a5790ea57ccf 100644 (file)
@@ -13,7 +13,7 @@
 
 
 /*
- * $Id: rfc2617.c,v 1.10 2007/01/13 16:08:19 hno Exp $
+ * $Id: rfc2617.c,v 1.11 2007/11/15 09:18:05 amosjeffries Exp $
  *
  * DEBUG:
  * AUTHOR: RFC 2617 & Robert Collins
@@ -116,24 +116,24 @@ DigestCalcHA1(
     MD5_CTX Md5Ctx;
 
     if (pszUserName) {
-       MD5Init(&Md5Ctx);
-       MD5Update(&Md5Ctx, pszUserName, strlen(pszUserName));
-       MD5Update(&Md5Ctx, ":", 1);
-       MD5Update(&Md5Ctx, pszRealm, strlen(pszRealm));
-       MD5Update(&Md5Ctx, ":", 1);
-       MD5Update(&Md5Ctx, pszPassword, strlen(pszPassword));
-       MD5Final((unsigned char *) HA1, &Md5Ctx);
+       xMD5Init(&Md5Ctx);
+       xMD5Update(&Md5Ctx, pszUserName, strlen(pszUserName));
+       xMD5Update(&Md5Ctx, ":", 1);
+       xMD5Update(&Md5Ctx, pszRealm, strlen(pszRealm));
+       xMD5Update(&Md5Ctx, ":", 1);
+       xMD5Update(&Md5Ctx, pszPassword, strlen(pszPassword));
+       xMD5Final((unsigned char *) HA1, &Md5Ctx);
     }
     if (strcasecmp(pszAlg, "md5-sess") == 0) {
        HASHHEX HA1Hex;
        CvtHex(HA1, HA1Hex);    /* RFC2617 errata */
-       MD5Init(&Md5Ctx);
-       MD5Update(&Md5Ctx, HA1Hex, HASHHEXLEN);
-       MD5Update(&Md5Ctx, ":", 1);
-       MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
-       MD5Update(&Md5Ctx, ":", 1);
-       MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
-       MD5Final((unsigned char *) HA1, &Md5Ctx);
+       xMD5Init(&Md5Ctx);
+       xMD5Update(&Md5Ctx, HA1Hex, HASHHEXLEN);
+       xMD5Update(&Md5Ctx, ":", 1);
+       xMD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
+       xMD5Update(&Md5Ctx, ":", 1);
+       xMD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
+       xMD5Final((unsigned char *) HA1, &Md5Ctx);
     }
     CvtHex(HA1, SessionKey);
 }
@@ -159,33 +159,33 @@ DigestCalcResponse(
 
     /*  calculate H(A2)
      */
-    MD5Init(&Md5Ctx);
-    MD5Update(&Md5Ctx, pszMethod, strlen(pszMethod));
-    MD5Update(&Md5Ctx, ":", 1);
-    MD5Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
+    xMD5Init(&Md5Ctx);
+    xMD5Update(&Md5Ctx, pszMethod, strlen(pszMethod));
+    xMD5Update(&Md5Ctx, ":", 1);
+    xMD5Update(&Md5Ctx, pszDigestUri, strlen(pszDigestUri));
     if (strcasecmp(pszQop, "auth-int") == 0) {
-       MD5Update(&Md5Ctx, ":", 1);
-       MD5Update(&Md5Ctx, HEntity, HASHHEXLEN);
+       xMD5Update(&Md5Ctx, ":", 1);
+       xMD5Update(&Md5Ctx, HEntity, HASHHEXLEN);
     }
-    MD5Final((unsigned char *) HA2, &Md5Ctx);
+    xMD5Final((unsigned char *) HA2, &Md5Ctx);
     CvtHex(HA2, HA2Hex);
 
     /* calculate response
      */
-    MD5Init(&Md5Ctx);
-    MD5Update(&Md5Ctx, HA1, HASHHEXLEN);
-    MD5Update(&Md5Ctx, ":", 1);
-    MD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
-    MD5Update(&Md5Ctx, ":", 1);
+    xMD5Init(&Md5Ctx);
+    xMD5Update(&Md5Ctx, HA1, HASHHEXLEN);
+    xMD5Update(&Md5Ctx, ":", 1);
+    xMD5Update(&Md5Ctx, pszNonce, strlen(pszNonce));
+    xMD5Update(&Md5Ctx, ":", 1);
     if (*pszQop) {
-       MD5Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
-       MD5Update(&Md5Ctx, ":", 1);
-       MD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
-       MD5Update(&Md5Ctx, ":", 1);
-       MD5Update(&Md5Ctx, pszQop, strlen(pszQop));
-       MD5Update(&Md5Ctx, ":", 1);
+       xMD5Update(&Md5Ctx, pszNonceCount, strlen(pszNonceCount));
+       xMD5Update(&Md5Ctx, ":", 1);
+       xMD5Update(&Md5Ctx, pszCNonce, strlen(pszCNonce));
+       xMD5Update(&Md5Ctx, ":", 1);
+       xMD5Update(&Md5Ctx, pszQop, strlen(pszQop));
+       xMD5Update(&Md5Ctx, ":", 1);
     }
-    MD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
-    MD5Final((unsigned char *) RespHash, &Md5Ctx);
+    xMD5Update(&Md5Ctx, HA2Hex, HASHHEXLEN);
+    xMD5Final((unsigned char *) RespHash, &Md5Ctx);
     CvtHex(RespHash, Response);
 }
index 5a1a4d447c5e48157d5fd7672de56bdda69cd1db..b2c4582b390fae0cac51ad0db8a8cc95c2faebd4 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: MemObject.cc,v 1.30 2007/08/13 18:25:14 hno Exp $
+ * $Id: MemObject.cc,v 1.31 2007/11/15 09:18:12 amosjeffries Exp $
  *
  * DEBUG: section 19    Store Memory Primitives
  * AUTHOR: Robert Collins
@@ -54,9 +54,9 @@ url_checksum(const char *url)
     unsigned int ck;
     MD5_CTX M;
     static unsigned char digest[16];
-    MD5Init(&M);
-    MD5Update(&M, (unsigned char *) url, strlen(url));
-    MD5Final(digest, &M);
+    xMD5Init(&M);
+    xMD5Update(&M, (unsigned char *) url, strlen(url));
+    xMD5Final(digest, &M);
     xmemcpy(&ck, digest, sizeof(ck));
     return ck;
 }
index 74ca0df1b4a65f545b43248b34ce91478f38e47c..6487980deacc7b6d69e915ea43f9df6fd9701687 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: store_key_md5.cc,v 1.34 2007/08/14 22:30:35 rousskov Exp $
+ * $Id: store_key_md5.cc,v 1.35 2007/11/15 09:18:12 amosjeffries Exp $
  *
  * DEBUG: section 20    Storage Manager MD5 Cache Keys
  * AUTHOR: Duane Wessels
@@ -105,11 +105,11 @@ storeKeyPrivate(const char *url, method_t method, int id)
     MD5_CTX M;
     assert(id > 0);
     debugs(20, 3, "storeKeyPrivate: " << RequestMethodStr[method] << " " << url);
-    MD5Init(&M);
-    MD5Update(&M, (unsigned char *) &id, sizeof(id));
-    MD5Update(&M, (unsigned char *) &method, sizeof(method));
-    MD5Update(&M, (unsigned char *) url, strlen(url));
-    MD5Final(digest, &M);
+    xMD5Init(&M);
+    xMD5Update(&M, (unsigned char *) &id, sizeof(id));
+    xMD5Update(&M, (unsigned char *) &method, sizeof(method));
+    xMD5Update(&M, (unsigned char *) url, strlen(url));
+    xMD5Final(digest, &M);
     return digest;
 }
 
@@ -119,10 +119,10 @@ storeKeyPublic(const char *url, const method_t method)
     static cache_key digest[MD5_DIGEST_CHARS];
     unsigned char m = (unsigned char) method;
     MD5_CTX M;
-    MD5Init(&M);
-    MD5Update(&M, &m, sizeof(m));
-    MD5Update(&M, (unsigned char *) url, strlen(url));
-    MD5Final(digest, &M);
+    xMD5Init(&M);
+    xMD5Update(&M, &m, sizeof(m));
+    xMD5Update(&M, (unsigned char *) url, strlen(url));
+    xMD5Final(digest, &M);
     return digest;
 }
 
@@ -139,14 +139,14 @@ storeKeyPublicByRequestMethod(HttpRequest * request, const method_t method)
     unsigned char m = (unsigned char) method;
     const char *url = urlCanonical(request);
     MD5_CTX M;
-    MD5Init(&M);
-    MD5Update(&M, &m, sizeof(m));
-    MD5Update(&M, (unsigned char *) url, strlen(url));
+    xMD5Init(&M);
+    xMD5Update(&M, &m, sizeof(m));
+    xMD5Update(&M, (unsigned char *) url, strlen(url));
 
     if (request->vary_headers)
-        MD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
+        xMD5Update(&M, (unsigned char *) request->vary_headers, strlen(request->vary_headers));
 
-    MD5Final(digest, &M);
+    xMD5Final(digest, &M);
 
     return digest;
 }
index f2529ca3d3b17d303707c420bd9257fd5197cf5a..fc4db161694a3cb3c370261553868517e7ece62a 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: wccp2.cc,v 1.17 2007/08/08 14:47:41 rousskov Exp $
+ * $Id: wccp2.cc,v 1.18 2007/11/15 09:18:12 amosjeffries Exp $
  *
  * DEBUG: section 80    WCCP Support
  * AUTHOR: Steven Wilton
@@ -564,13 +564,13 @@ wccp2_update_md5_security(char *password, char *ptr, char *packet, int len)
     /* XXX eventually we should be able to kill md5_digest and blit it directly in */
     memset(ws->security_implementation, 0, sizeof(ws->security_implementation));
 
-    MD5Init(&M);
+    xMD5Init(&M);
 
-    MD5Update(&M, pwd, 8);
+    xMD5Update(&M, pwd, 8);
 
-    MD5Update(&M, packet, len);
+    xMD5Update(&M, packet, len);
 
-    MD5Final(md5_digest, &M);
+    xMD5Final(md5_digest, &M);
 
     memcpy(ws->security_implementation, md5_digest, sizeof(md5_digest));
 
@@ -623,13 +623,13 @@ wccp2_check_security(struct wccp2_service_list_t *srv, char *security, char *pac
 
     memset(ws->security_implementation, 0, sizeof(ws->security_implementation));
 
-    MD5Init(&M);
+    xMD5Init(&M);
 
-    MD5Update(&M, pwd, 8);
+    xMD5Update(&M, pwd, 8);
 
-    MD5Update(&M, packet, len);
+    xMD5Update(&M, packet, len);
 
-    MD5Final(md5_digest, &M);
+    xMD5Final(md5_digest, &M);
 
     return (memcmp(md5_digest, md5_challenge, 16) == 0);
 }