1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
/* MD5 Digest Authentication.
cribbed from LWP::Authen::Digest, the RFC, and auth.c in the jabber
code. Moving out of the static RVP module so that others can use it
if necessary.
*/
/*
* Reverse-engineering reveals that the Microsoft client:
* - supports MD5 and MD5-sess
* - supports opaque
* - may support stale, it at least doesn't abandon auth when it sees it
* - only supports qop=auth (no auth-int, and definitely no missing qop)
*
* Anything it doesn't like causes it to revert to not authing at all.
*/
#include <glib.h>
#include <string.h>
/* Gaim includes */
#include <debug.h>
/* local include */
#include "digest_access_auth.h"
#ifndef NOMD5
/* shims for gcrypt */
#ifdef GCRYPT_MD5
typedef guint8 md5_byte_t;
#define md5_state_t gcry_md_hd_t
#define md5_init( handle ) gcry_md_open( handle, GCRY_MD_MD5, 0 )
static void md5_append( md5_state_t *handle, md5_byte_t *buffer, size_t length ) {
gcry_md_write( *handle, buffer, length );
}
static void md5_finish( md5_state_t *handle, md5_byte_t *buffer ) {
unsigned char *buf = gcry_md_read( *handle, 0 );
bcopy( buf, buffer, 16 ); /* MD5 length == 16 */
gcry_md_close( *handle );
}
#endif
gchar *get_auth_digest( GaimConnection *gc, gchar *method, gchar *uri,
gchar *header, gchar *user, gchar *pass ) {
gchar **auth_param = NULL;
gint i = 0;
gchar *cnonce, *a1 = NULL, *a2, *retval = NULL, hexresp[33];
md5_state_t md5;
md5_byte_t ha2[16], response[16];
GHashTable *params = g_hash_table_new( g_str_hash, g_str_equal );
/* slightly naive, but seems to be acceptable for now. FIXME */
auth_param = g_strsplit( &header[strlen( "Digest ")], ",", 0 );
/* default algorithm */
g_hash_table_replace( params, g_strdup( "algorithm" ), g_strdup( "MD5" ));
/* find the nonce, etc. */
for ( i = 0; auth_param[i]; i++ ) {
gchar **bits;
/* no leading/trailing spaces, thanks. */
g_strstrip( auth_param[i] );
bits = g_strsplit( auth_param[i], "=", 2 );
/* clean up value by unquoting it FIXME */
if ( bits[1][0] == '"' ) {
memcpy( &bits[1][0], &bits[1][1],
strlen( bits[1] ) - 1 );
bits[1][strlen( bits[1] ) - 1] = '\0';
}
if ( bits[1][strlen( bits[1] ) - 1] == '"' ) {
bits[1][strlen( bits[1] ) - 1 ] = '\0';
}
g_hash_table_replace( params, g_strdup( bits[0] ), g_strdup( bits[1] ));
gaim_debug_misc( __FUNCTION__, "Found %s = %s\n", bits[0], bits[1] );
g_strfreev( bits );
}
if ( g_hash_table_lookup( params, "nonce" ) == NULL ||
g_hash_table_lookup( params, "realm" ) == NULL ) {
gaim_connection_error( gc,
"Can't find one of nonce or realm in header" );
return NULL;
}
cnonce = g_strdup_printf( "%x%u%x", g_random_int(), (int)time( NULL ),
g_random_int() );
if ( !strcasecmp( g_hash_table_lookup( params, "algorithm" ), "MD5" )) {
/* A1 = "username : realm : passwd"; */
md5_init( &md5 );
md5_append( &md5, (md5_byte_t *)user, strlen( user ));
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)g_hash_table_lookup( params, "realm" ),
strlen( g_hash_table_lookup( params, "realm" )));
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)pass, strlen( pass ));
a1 = g_malloc0( 16 + 1 );
md5_finish( &md5, (md5_byte_t *)a1 );
a1[16] = '\0';
} else if ( !strcasecmp( g_hash_table_lookup( params, "algorithm" ),
"MD5-sess" )) {
/* A1 = "H( username : realm : passwd ):nonce:cnonce"; */
md5_byte_t a1_b[16];
md5_init( &md5 );
md5_append( &md5, (md5_byte_t *)user, strlen( user ));
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)g_hash_table_lookup( params, "realm" ),
strlen( g_hash_table_lookup( params, "realm" )));
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)pass, strlen( pass ));
md5_finish( &md5, a1_b );
/* hexify */
a1 = g_malloc0( 32 + strlen( g_hash_table_lookup( params, "nonce" )) +
strlen( cnonce ) + 1 );
for ( i = 0; i < 16; i++ ) {
sprintf( &a1[i * 2], "%02x", (guint8)a1_b[i] );
}
/* append nonce & cnonce */
memcpy( &a1[32], g_hash_table_lookup( params, "nonce" ),
strlen( g_hash_table_lookup( params, "nonce" )));
memcpy( &a1[32 + strlen( g_hash_table_lookup( params, "nonce" ) )], cnonce,
strlen( cnonce ));
a1[32 + strlen( g_hash_table_lookup( params, "nonce" ) ) + strlen( cnonce )] = '\0';
} else {
gchar *msg = g_strdup_printf( "Server asked for unknown algorithm '%s'",
(char *)g_hash_table_lookup( params,
"algorithm" ));
gaim_connection_error( gc, msg );
g_free( msg );
return NULL;
}
if ( g_hash_table_lookup( params, "qop" ) != NULL &&
( !strcmp( g_hash_table_lookup( params, "qop" ), "auth" ) ||
!strcmp( g_hash_table_lookup( params, "qop" ), "auth-int" ))) {
if ( !strcmp( g_hash_table_lookup( params, "qop" ), "auth" )) {
/* A2 = method : uri */
a2 = g_strdup_printf( "%s:%s%s", method, uri[0] == '/' ? "" : "/", uri );
} else {
/* we don't support auth-int just now */
/* A2 = method : uri : content */
a2 = g_strdup_printf( "%s:%s%s:%s", method, uri[0] == '/' ? "" : "/",
uri, "HEXME(XXX)" );
}
} else {
/* A2 = method : uri */
a2 = g_strdup_printf( "%s:%s%s", method, uri[0] == '/' ? "" : "/", uri );
}
/* MD5-ify a2 */
md5_init( &md5 );
md5_append( &md5, (md5_byte_t *)a2, strlen( a2 ));
md5_finish( &md5, ha2 );
/* now gob it all together appropriate to the qop parameter */
if ( g_hash_table_lookup( params, "qop" ) != NULL &&
( !strcmp( g_hash_table_lookup( params, "qop" ), "auth" ) ||
!strcmp( g_hash_table_lookup( params, "qop" ), "auth-int" ))) {
/* request-digest = KD(H(A1), nonce : nc : cnonce : qop : H(A2) */
md5_init( &md5 );
/* H(A1) */
for ( i = 0; i < strlen( a1 ); i++ ) {
gchar *hex = g_strdup_printf( "%02x", (guint8)a1[i] );
md5_append( &md5, (md5_byte_t *)hex, 2 );
g_free( hex );
}
/* :nonce */
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)g_hash_table_lookup( params, "nonce" ), strlen( g_hash_table_lookup( params, "nonce" ) ));
/* :nc */
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)"00000001", strlen( "00000001" ));
/* :cnonce */
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)cnonce, strlen( cnonce ));
/* :qop */
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)g_hash_table_lookup( params, "qop" ), strlen( g_hash_table_lookup( params, "qop" ) ));
/* : */
md5_append( &md5, (md5_byte_t *)":", 1 );
/* H(A2) */
for ( i = 0; i < 16; i++ ) {
gchar *hex = g_strdup_printf( "%02x", (guint8)ha2[i] );
md5_append( &md5, (md5_byte_t *)hex, 2 );
g_free( hex );
}
md5_finish( &md5, response );
/* turn response into hex */
for ( i = 0; i < 16; i++ ) {
sprintf( &hexresp[i * 2], "%02x", (guint8)response[i] );
}
hexresp[32] = '\0';
retval = g_strdup_printf( "Digest "
"username=\"%s\", "
"realm=\"%s\", "
"qop=\"%s\", "
"algorithm=\"%s\", "
"uri=\"%s%s\", "
"nonce=\"%s\", "
"nc=00000001, "
"cnonce=\"%s\", "
"response=\"%s\"",
user,
(char *)g_hash_table_lookup( params, "realm" ),
(char *)g_hash_table_lookup( params, "qop" ),
(char *)g_hash_table_lookup( params, "algorithm" ),
uri[0] == '/' ? "" : "/", uri,
(char *)g_hash_table_lookup( params, "nonce" ),
cnonce,
hexresp );
} else {
/* request-digest = KD(H(A1), nonce : H(A2) */
md5_init( &md5 );
/* H(A1) */
for ( i = 0; i < 16; i++ ) {
gchar *hex = g_strdup_printf( "%02x", (guint8)a1[i] );
md5_append( &md5, (md5_byte_t *)hex, 2 );
g_free( hex );
}
/* :nonce: */
md5_append( &md5, (md5_byte_t *)":", 1 );
md5_append( &md5, (md5_byte_t *)g_hash_table_lookup( params, "nonce" ), strlen( g_hash_table_lookup( params, "nonce" ) ));
md5_append( &md5, (md5_byte_t *)":", 1 );
/* H(A2) */
for ( i = 0; i < 16; i++ ) {
gchar *hex = g_strdup_printf( "%02x", (guint8)ha2[i] );
md5_append( &md5, (md5_byte_t *)hex, 2 );
g_free( hex );
}
md5_finish( &md5, response );
/* turn response into hex */
for ( i = 0; i < 16; i++ ) {
sprintf( &hexresp[i * 2], "%02x", (guint8)response[i] );
}
hexresp[32] = '\0';
retval = g_strdup_printf( "Digest "
"username=\"%s\", "
"realm=\"%s\", "
"uri=\"%s%s\", "
"nonce=\"%s\", "
"response=\"%s\"",
user,
(char *)g_hash_table_lookup( params, "realm" ),
uri[0] == '/' ? "" : "/", uri,
(char *)g_hash_table_lookup( params, "nonce" ),
hexresp );
}
if ( auth_param != NULL ) {
g_strfreev( auth_param );
}
if ( a1 != NULL ) { g_free( a1 ); }
return retval;
}
#endif
|