A patch to simplify some digest-auth code
Wim Lewis <[email protected]> Mon, 1 Aug 2005 00:45:06 -0700
| Newsgroups | gmane.comp.gnome.ximian.soup |
|---|---|
| Message-ID | <[email protected]> |
--jRHKVT23PllUwdXP Content-Type: text/plain; charset=us-ascii Content-Disposition: inline For the ChangeLog: Factored out code to produce hexadecimal MD5 digests into one function (soup_md5_final_hex); eliminated use of sprintf(). Some more explanation: My linker complains bitterly when I link something that references sprintf() (because it's easy to write a buffer overflow --- the linker suggests that I use snprintf() instead). Libsoup's use of sprintf() appears safe, but while looking at it I decided to merge the two identical copies of digest_hex() into one function in the md5 utilities file. I think this makes the code slightly more maintainable. It definitely reduces the warning-message clutter when linking on OpenBSD. -- Wim Lewis <[email protected]>, Seattle, WA, USA. PGP keyID 27F772C1 --jRHKVT23PllUwdXP Content-Type: text/plain; charset=us-ascii Content-Description: A patch for libsoup Content-Disposition: attachment; filename=dipsy02 Index: soup-server-auth.c =================================================================== RCS file: /cvs/gnome/libsoup/libsoup/soup-server-auth.c,v retrieving revision 1.9 diff -u -r1.9 soup-server-auth.c --- soup-server-auth.c 10 Sep 2003 18:14:18 -0000 1.9 +++ soup-server-auth.c 1 Aug 2005 07:18:45 -0000 @@ -70,16 +70,6 @@ return scheme->type; } -static void -digest_hex (guchar *digest, guchar hex[33]) -{ - guchar *s, *p; - - /* lowercase hexify that bad-boy... */ - for (s = digest, p = hex; p < hex + 32; s++, p += 2) - sprintf (p, "%.2x", *s); -} - static gboolean check_digest_passwd (SoupServerAuthDigest *digest, gchar *passwd) @@ -111,8 +101,7 @@ } /* hexify A1 */ - soup_md5_final (&ctx, d); - digest_hex (d, hex_a1); + soup_md5_final_hex (&ctx, hex_a1); /* compute A2 */ soup_md5_init (&ctx); @@ -129,8 +118,7 @@ } /* hexify A2 */ - soup_md5_final (&ctx, d); - digest_hex (d, hex_a2); + soup_md5_final_hex (&ctx, hex_a2); /* compute KD */ soup_md5_init (&ctx); @@ -156,9 +144,7 @@ soup_md5_update (&ctx, ":", 1); soup_md5_update (&ctx, hex_a2, 32); - soup_md5_final (&ctx, d); - - digest_hex (d, o); + soup_md5_final_hex (&ctx, o); return strcmp (o, digest->digest_response) == 0; } Index: soup-auth-digest.c =================================================================== RCS file: /cvs/gnome/libsoup/libsoup/soup-auth-digest.c,v retrieving revision 1.8 diff -u -r1.8 soup-auth-digest.c --- soup-auth-digest.c 19 Apr 2005 06:21:31 -0000 1.8 +++ soup-auth-digest.c 1 Aug 2005 07:18:45 -0000 @@ -243,16 +243,6 @@ } static void -digest_hex (guchar *digest, guchar hex[33]) -{ - guchar *s, *p; - - /* lowercase hexify that bad-boy... */ - for (s = digest, p = hex; p < hex + 32; s++, p += 2) - sprintf (p, "%.2x", *s); -} - -static void authenticate (SoupAuth *auth, const char *username, const char *password) { SoupAuthDigestPrivate *priv = SOUP_AUTH_DIGEST_GET_PRIVATE (auth); @@ -300,8 +290,7 @@ } /* hexify A1 */ - soup_md5_final (&ctx, d); - digest_hex (d, priv->hex_a1); + soup_md5_final_hex (&ctx, priv->hex_a1); } static gboolean @@ -314,7 +303,6 @@ compute_response (SoupAuthDigestPrivate *priv, SoupMessage *msg) { guchar hex_a2[33], o[33]; - guchar d[16]; SoupMD5Context md5; char *url; const SoupUri *uri; @@ -338,8 +326,7 @@ } /* now hexify A2 */ - soup_md5_final (&md5, d); - digest_hex (d, hex_a2); + soup_md5_final_hex (&md5, hex_a2); /* compute KD */ soup_md5_init (&md5); @@ -373,9 +360,7 @@ } soup_md5_update (&md5, hex_a2, 32); - soup_md5_final (&md5, d); - - digest_hex (d, o); + soup_md5_final_hex (&md5, o); return g_strdup (o); } Index: soup-md5-utils.c =================================================================== RCS file: /cvs/gnome/libsoup/libsoup/soup-md5-utils.c,v retrieving revision 1.1 diff -u -r1.1 soup-md5-utils.c --- soup-md5-utils.c 10 Sep 2003 18:14:18 -0000 1.1 +++ soup-md5-utils.c 1 Aug 2005 07:18:46 -0000 @@ -20,6 +20,7 @@ * Written March 1993 by Branko Lankester * Modified June 1993 by Colin Plumb for altered md5.c. * Modified October 1995 by Erik Troan for RPM + * Modified July 2005 by Wim Lewis for libsoup */ @@ -131,7 +132,8 @@ * @digest: 16 bytes buffer * @ctx: context containing the calculated md5 * - * copy the final md5 hash to a bufer + * Performs the final md5 transformation on the context, and + * then copies the resulting md5 hash to a buffer **/ void soup_md5_final (SoupMD5Context *ctx, guchar digest[16]) @@ -179,6 +181,34 @@ +/** + * soup_md5_final_hex: copy the final md5 hash to a bufer + * @digest: 33 bytes buffer (32 hex digits plus NUL) + * @ctx: context containing the calculated md5 + * + * As soup_md5_final(), but copies the final md5 hash + * to a buffer as a NUL-terminated hexadecimal string + **/ +void +soup_md5_final_hex (SoupMD5Context *ctx, guchar hex_digest[33]) +{ + static const guchar hexdigits[16] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' + }; + int p; + + soup_md5_final (ctx, hex_digest); + + /* lowercase hexify that bad-boy... */ + hex_digest[32] = 0; + for (p = 15; p >= 0; p --) { + guchar b = hex_digest[p]; + hex_digest[p*2+1] = hexdigits[ (b & 0x0F ) ]; + hex_digest[p*2 ] = hexdigits[ (b & 0xF0 ) >> 4 ]; + } +} + /* The four core functions - F1 is optimized somewhat */ @@ -280,3 +310,4 @@ buf[2] += c; buf[3] += d; } + Index: soup-md5-utils.h =================================================================== RCS file: /cvs/gnome/libsoup/libsoup/soup-md5-utils.h,v retrieving revision 1.2 diff -u -r1.2 soup-md5-utils.h --- soup-md5-utils.h 14 Jun 2005 15:34:21 -0000 1.2 +++ soup-md5-utils.h 1 Aug 2005 07:18:46 -0000 @@ -20,6 +20,7 @@ * Written March 1993 by Branko Lankester * Modified June 1993 by Colin Plumb for altered md5.c. * Modified October 1995 by Erik Troan for RPM + * Modified July 2005 by Wim Lewis for libsoup */ @@ -42,6 +43,8 @@ guint32 len); void soup_md5_final (SoupMD5Context *ctx, guchar digest[16]); +void soup_md5_final_hex (SoupMD5Context *ctx, + guchar digest[33]); #endif /* SOUP_MD5_UTILS_H */ --jRHKVT23PllUwdXP Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Soup-list maillist - [email protected] http://lists.ximian.com/mailman/listinfo/soup-list --jRHKVT23PllUwdXP--