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.
/* 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);
/* 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));
strncat(passwd, sp, sl);
strcat(passwd, "$");
- MD5Final(final, &ctx);
+ xMD5Final(final, &ctx);
/*
* and now, just to make sure things don't run too fast
* 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);
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]);
#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"
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 */
/*
- * $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 $
*/
/*
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");
* initialization constants.
*/
void
-MD5Init(struct MD5Context *ctx)
+xMD5Init(struct MD5Context *ctx)
{
ctx->buf[0] = 0x67452301;
ctx->buf[1] = 0xefcdab89;
* 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;
* 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;
* 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;
/*
- * $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
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);
}
/* 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);
}
/*
- * $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
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;
}
/*
- * $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
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;
}
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;
}
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;
}
/*
- * $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
/* 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));
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);
}