Tue, 25 Nov 2003 23:21:31 +0000
[gaim-migrate @ 8259]
T.M.Thanh requests these updates
| 7014 | 1 | /* |
| 2 | * gaim - Jabber Protocol Plugin | |
| 3 | * | |
| 4 | * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
| 5 | * | |
| 6 | * This program is free software; you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 10 | * | |
| 11 | * This program is distributed in the hope that it will be useful, | |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 | * | |
| 20 | */ | |
| 21 | #include "internal.h" | |
| 22 | ||
| 23 | #include "jutil.h" | |
| 24 | #include "auth.h" | |
| 25 | #include "xmlnode.h" | |
| 26 | #include "jabber.h" | |
| 27 | #include "iq.h" | |
| 28 | #include "sha.h" | |
| 29 | ||
| 30 | #include "debug.h" | |
| 31 | #include "md5.h" | |
| 32 | #include "util.h" | |
| 33 | #include "sslconn.h" | |
| 34 | ||
| 35 | ||
| 36 | void | |
| 37 | jabber_auth_start(JabberStream *js, xmlnode *packet) | |
| 38 | { | |
| 39 | xmlnode *mechs, *mechnode; | |
| 40 | xmlnode *starttls; | |
| 7291 | 41 | xmlnode *auth; |
| 7014 | 42 | |
| 7291 | 43 | gboolean digest_md5 = FALSE, plain=FALSE; |
| 7014 | 44 | |
| 7157 | 45 | if((starttls = xmlnode_get_child(packet, "starttls"))) { |
| 7630 | 46 | if(gaim_account_get_bool(js->gc->account, "use_tls", TRUE) && |
| 47 | gaim_ssl_is_supported()) { | |
| 7157 | 48 | jabber_send_raw(js, |
| 49 | "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"); | |
| 50 | return; | |
| 51 | } else if(xmlnode_get_child(starttls, "required")) { | |
| 52 | gaim_connection_error(js->gc, _("Server requires SSL for login")); | |
| 53 | return; | |
| 54 | } | |
| 7014 | 55 | } |
| 56 | ||
| 57 | mechs = xmlnode_get_child(packet, "mechanisms"); | |
| 58 | ||
| 59 | if(!mechs) { | |
| 60 | gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 61 | return; | |
| 62 | } | |
| 63 | ||
| 64 | for(mechnode = mechs->child; mechnode; mechnode = mechnode->next) | |
| 65 | { | |
| 66 | if(mechnode->type == NODE_TYPE_TAG) { | |
| 67 | char *mech_name = xmlnode_get_data(mechnode); | |
| 68 | if(mech_name && !strcmp(mech_name, "DIGEST-MD5")) | |
| 69 | digest_md5 = TRUE; | |
| 7291 | 70 | else if(mech_name && !strcmp(mech_name, "PLAIN")) |
| 71 | plain = TRUE; | |
| 7014 | 72 | g_free(mech_name); |
| 73 | } | |
| 74 | } | |
| 75 | ||
| 7291 | 76 | auth = xmlnode_new("auth"); |
| 77 | xmlnode_set_attrib(auth, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl"); | |
| 7014 | 78 | if(digest_md5) { |
| 7291 | 79 | xmlnode_set_attrib(auth, "mechanism", "DIGEST-MD5"); |
| 80 | js->auth_type = JABBER_AUTH_DIGEST_MD5; | |
| 81 | /* | |
| 82 | } else if(plain) { | |
| 83 | js->auth_type = JABBER_AUTH_PLAIN; | |
| 84 | */ | |
| 7014 | 85 | } else { |
| 86 | gaim_connection_error(js->gc, | |
| 87 | _("Server does not use any supported authentication method")); | |
| 7291 | 88 | xmlnode_free(auth); |
| 89 | return; | |
| 7014 | 90 | } |
| 7291 | 91 | jabber_send(js, auth); |
| 92 | xmlnode_free(auth); | |
| 7014 | 93 | } |
| 94 | ||
| 7395 | 95 | static void auth_old_result_cb(JabberStream *js, xmlnode *packet, gpointer data) |
| 7014 | 96 | { |
| 97 | const char *type = xmlnode_get_attrib(packet, "type"); | |
| 98 | ||
| 99 | if(type && !strcmp(type, "error")) { | |
| 100 | xmlnode *error = xmlnode_get_child(packet, "error"); | |
| 101 | const char *err_code; | |
| 102 | char *err_text; | |
| 103 | char *buf; | |
| 104 | ||
| 105 | err_code = xmlnode_get_attrib(error, "code"); | |
| 106 | err_text = xmlnode_get_data(error); | |
| 107 | ||
| 108 | if(!err_code) | |
| 109 | err_code = ""; | |
| 110 | if(!err_text) | |
| 111 | err_text = g_strdup(_("Unknown")); | |
| 112 | ||
| 113 | if(!strcmp(err_code, "401")) | |
| 114 | js->gc->wants_to_die = TRUE; | |
| 115 | ||
| 116 | buf = g_strdup_printf("Error %s: %s", | |
| 117 | err_code, err_text); | |
| 118 | ||
| 119 | gaim_connection_error(js->gc, buf); | |
| 120 | g_free(err_text); | |
| 121 | g_free(buf); | |
| 122 | } | |
| 123 | jabber_stream_set_state(js, JABBER_STREAM_CONNECTED); | |
| 124 | } | |
| 125 | ||
| 7395 | 126 | static void auth_old_cb(JabberStream *js, xmlnode *packet, gpointer data) |
| 7014 | 127 | { |
| 128 | JabberIq *iq; | |
| 129 | xmlnode *query, *x; | |
| 130 | gboolean digest = FALSE; | |
| 7514 | 131 | const char *type = xmlnode_get_attrib(packet, "type"); |
| 7014 | 132 | const char *pw = gaim_account_get_password(js->gc->account); |
| 133 | ||
| 7514 | 134 | if(!type) { |
| 7014 | 135 | return; |
| 7515 | 136 | } else if(!strcmp(type, "error")) { |
| 7514 | 137 | /* XXX: handle error */ |
| 7515 | 138 | } else if(!strcmp(type, "result")) { |
| 7514 | 139 | query = xmlnode_get_child(packet, "query"); |
| 140 | if(js->stream_id && xmlnode_get_child(query, "digest")) { | |
| 141 | digest = TRUE; | |
| 142 | } else if(!xmlnode_get_child(query, "password")) { | |
| 143 | gaim_connection_error(js->gc, | |
| 144 | _("Server does not use any supported authentication method")); | |
| 145 | return; | |
| 146 | } | |
| 7014 | 147 | |
| 7514 | 148 | iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:auth"); |
| 149 | query = xmlnode_get_child(iq->node, "query"); | |
| 150 | x = xmlnode_new_child(query, "username"); | |
| 151 | xmlnode_insert_data(x, js->user->node, -1); | |
| 152 | x = xmlnode_new_child(query, "resource"); | |
| 153 | xmlnode_insert_data(x, js->user->resource, -1); | |
| 7014 | 154 | |
| 7514 | 155 | if(digest) { |
| 156 | unsigned char hashval[20]; | |
| 157 | char *s, h[41], *p; | |
| 158 | int i; | |
| 7014 | 159 | |
| 7514 | 160 | x = xmlnode_new_child(query, "digest"); |
| 161 | s = g_strdup_printf("%s%s", js->stream_id, pw); | |
| 162 | shaBlock((unsigned char *)s, strlen(s), hashval); | |
| 163 | p = h; | |
| 164 | for(i=0; i<20; i++, p+=2) | |
| 165 | snprintf(p, 3, "%02x", hashval[i]); | |
| 166 | xmlnode_insert_data(x, h, -1); | |
| 167 | g_free(s); | |
| 168 | } else { | |
| 169 | x = xmlnode_new_child(query, "password"); | |
| 170 | xmlnode_insert_data(x, pw, -1); | |
| 171 | } | |
| 172 | ||
| 173 | jabber_iq_set_callback(iq, auth_old_result_cb, NULL); | |
| 174 | ||
| 175 | jabber_iq_send(iq); | |
| 7014 | 176 | } |
| 177 | } | |
| 178 | ||
| 179 | void jabber_auth_start_old(JabberStream *js) | |
| 180 | { | |
| 181 | JabberIq *iq; | |
| 182 | xmlnode *query, *username; | |
| 183 | ||
| 184 | iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:auth"); | |
| 185 | ||
| 186 | query = xmlnode_get_child(iq->node, "query"); | |
| 187 | username = xmlnode_new_child(query, "username"); | |
| 188 | xmlnode_insert_data(username, js->user->node, -1); | |
| 189 | ||
| 7395 | 190 | jabber_iq_set_callback(iq, auth_old_cb, NULL); |
| 7014 | 191 | |
| 192 | jabber_iq_send(iq); | |
| 193 | } | |
| 194 | ||
| 195 | static GHashTable* parse_challenge(const char *challenge) | |
| 196 | { | |
| 197 | GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, | |
| 198 | g_free, g_free); | |
| 199 | char **pairs; | |
| 200 | int i; | |
| 201 | ||
| 202 | pairs = g_strsplit(challenge, ",", -1); | |
| 203 | ||
| 204 | for(i=0; pairs[i]; i++) { | |
| 205 | char **keyval = g_strsplit(pairs[i], "=", 2); | |
| 206 | if(keyval[0] && keyval[1]) { | |
| 207 | if(keyval[1][0] == '"' && keyval[1][strlen(keyval[1])-1] == '"') | |
| 208 | g_hash_table_replace(ret, g_strdup(keyval[0]), g_strndup(keyval[1]+1, strlen(keyval[1])-2)); | |
| 209 | else | |
| 210 | g_hash_table_replace(ret, g_strdup(keyval[0]), g_strdup(keyval[1])); | |
| 211 | } | |
| 212 | g_strfreev(keyval); | |
| 213 | } | |
| 214 | ||
| 215 | g_strfreev(pairs); | |
| 216 | ||
| 217 | return ret; | |
| 218 | } | |
| 219 | ||
| 220 | static unsigned char* | |
| 221 | generate_response_value(JabberID *jid, const char *passwd, const char *nonce, | |
| 7267 | 222 | const char *cnonce, const char *a2, const char *realm) |
| 7014 | 223 | { |
| 224 | md5_state_t ctx; | |
| 225 | md5_byte_t result[16]; | |
| 226 | ||
| 227 | char *x, *y, *a1, *ha1, *ha2, *kd, *z; | |
| 228 | ||
| 7267 | 229 | x = g_strdup_printf("%s:%s:%s", jid->node, realm, passwd); |
| 7014 | 230 | md5_init(&ctx); |
| 231 | md5_append(&ctx, x, strlen(x)); | |
| 232 | md5_finish(&ctx, result); | |
| 233 | ||
| 234 | y = g_strndup(result, 16); | |
| 235 | ||
| 7395 | 236 | a1 = g_strdup_printf("%s:%s:%s:%s@%s", y, nonce, cnonce, jid->node, |
| 237 | jid->domain); | |
| 7014 | 238 | |
| 239 | md5_init(&ctx); | |
| 240 | md5_append(&ctx, a1, strlen(a1)); | |
| 241 | md5_finish(&ctx, result); | |
| 242 | ||
|
7106
eaeff5775818
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
243 | ha1 = gaim_base16_encode(result, 16); |
| 7014 | 244 | |
| 245 | md5_init(&ctx); | |
| 246 | md5_append(&ctx, a2, strlen(a2)); | |
| 247 | md5_finish(&ctx, result); | |
| 248 | ||
|
7106
eaeff5775818
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
249 | ha2 = gaim_base16_encode(result, 16); |
| 7014 | 250 | |
| 251 | kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2); | |
| 252 | ||
| 253 | md5_init(&ctx); | |
| 254 | md5_append(&ctx, kd, strlen(kd)); | |
| 255 | md5_finish(&ctx, result); | |
| 256 | ||
|
7106
eaeff5775818
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
257 | z = gaim_base16_encode(result, 16); |
| 7014 | 258 | |
| 259 | g_free(x); | |
| 260 | g_free(y); | |
| 261 | g_free(a1); | |
| 262 | g_free(ha1); | |
| 263 | g_free(ha2); | |
| 264 | g_free(kd); | |
| 265 | ||
| 266 | return z; | |
| 267 | } | |
| 268 | ||
| 269 | void | |
| 270 | jabber_auth_handle_challenge(JabberStream *js, xmlnode *packet) | |
| 271 | { | |
| 272 | ||
| 7291 | 273 | if(js->auth_type == JABBER_AUTH_PLAIN) { |
| 274 | /* XXX: implement me! */ | |
| 275 | } else if(js->auth_type == JABBER_AUTH_DIGEST_MD5) { | |
| 276 | char *enc_in = xmlnode_get_data(packet); | |
| 277 | char *dec_in; | |
| 278 | char *enc_out; | |
| 279 | GHashTable *parts; | |
| 7014 | 280 | |
| 7395 | 281 | if(!enc_in) { |
| 282 | gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 283 | return; | |
| 284 | } | |
| 285 | ||
| 7291 | 286 | gaim_base64_decode(enc_in, &dec_in, NULL); |
| 7395 | 287 | gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded challenge (%d): %s\n", |
| 288 | strlen(dec_in), dec_in); | |
| 7291 | 289 | |
| 290 | parts = parse_challenge(dec_in); | |
| 7014 | 291 | |
| 292 | ||
| 7291 | 293 | if (g_hash_table_lookup(parts, "rspauth")) { |
| 294 | char *rspauth = g_hash_table_lookup(parts, "rspauth"); | |
| 7014 | 295 | |
| 296 | ||
| 7291 | 297 | if(rspauth && js->expected_rspauth && |
| 298 | !strcmp(rspauth, js->expected_rspauth)) { | |
| 299 | jabber_send_raw(js, | |
| 300 | "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />"); | |
| 301 | } else { | |
| 302 | gaim_connection_error(js->gc, _("Invalid challenge from server")); | |
| 303 | } | |
| 304 | g_free(js->expected_rspauth); | |
| 305 | } else { | |
| 306 | /* assemble a response, and send it */ | |
| 307 | /* see RFC 2831 */ | |
| 308 | GString *response = g_string_new(""); | |
| 309 | char *a2; | |
| 310 | char *auth_resp; | |
| 311 | char *buf; | |
| 312 | char *cnonce; | |
| 313 | char *realm; | |
| 314 | char *nonce; | |
| 7014 | 315 | |
| 7291 | 316 | /* we're actually supposed to prompt the user for a realm if |
| 317 | * the server doesn't send one, but that really complicates things, | |
| 318 | * so i'm not gonna worry about it until is poses a problem to | |
| 319 | * someone, or I get really bored */ | |
| 320 | realm = g_hash_table_lookup(parts, "realm"); | |
| 321 | if(!realm) | |
| 322 | realm = js->user->domain; | |
| 7014 | 323 | |
| 7291 | 324 | cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL), |
| 325 | g_random_int()); | |
| 326 | nonce = g_hash_table_lookup(parts, "nonce"); | |
| 7014 | 327 | |
| 328 | ||
| 7291 | 329 | a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm); |
| 330 | auth_resp = generate_response_value(js->user, | |
| 331 | gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
| 332 | g_free(a2); | |
| 333 | ||
| 334 | a2 = g_strdup_printf(":xmpp/%s", realm); | |
| 335 | js->expected_rspauth = generate_response_value(js->user, | |
| 336 | gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
| 337 | g_free(a2); | |
| 338 | ||
| 339 | ||
| 340 | g_string_append_printf(response, "username=\"%s\"", js->user->node); | |
| 341 | g_string_append_printf(response, ",realm=\"%s\"", realm); | |
| 342 | g_string_append_printf(response, ",nonce=\"%s\"", nonce); | |
| 343 | g_string_append_printf(response, ",cnonce=\"%s\"", cnonce); | |
| 344 | g_string_append_printf(response, ",nc=00000001"); | |
| 345 | g_string_append_printf(response, ",qop=auth"); | |
| 346 | g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm); | |
| 347 | g_string_append_printf(response, ",response=%s", auth_resp); | |
| 348 | g_string_append_printf(response, ",charset=utf-8"); | |
| 7395 | 349 | g_string_append_printf(response, ",authzid=\"%s@%s\"", |
| 350 | js->user->node, js->user->domain); | |
| 7291 | 351 | |
| 352 | g_free(auth_resp); | |
| 353 | g_free(cnonce); | |
| 354 | ||
| 355 | enc_out = gaim_base64_encode(response->str, response->len); | |
| 356 | ||
| 357 | gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str); | |
| 358 | ||
| 359 | buf = g_strdup_printf("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", enc_out); | |
| 360 | ||
| 361 | jabber_send_raw(js, buf); | |
| 362 | ||
| 363 | g_free(buf); | |
| 364 | ||
| 365 | g_free(enc_out); | |
| 366 | ||
| 367 | g_string_free(response, TRUE); | |
| 7014 | 368 | } |
| 7291 | 369 | |
| 370 | g_free(enc_in); | |
| 371 | g_free(dec_in); | |
| 372 | g_hash_table_destroy(parts); | |
| 7014 | 373 | } |
| 374 | } | |
| 375 | ||
| 376 | void jabber_auth_handle_success(JabberStream *js, xmlnode *packet) | |
| 377 | { | |
| 378 | const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
| 379 | ||
| 380 | if(!ns || strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
| 381 | gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 382 | return; | |
| 383 | } | |
| 384 | ||
| 385 | jabber_stream_set_state(js, JABBER_STREAM_REINITIALIZING); | |
| 386 | } | |
| 387 | ||
| 388 | void jabber_auth_handle_failure(JabberStream *js, xmlnode *packet) | |
| 389 | { | |
| 390 | const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
| 391 | ||
| 392 | if(!ns) | |
| 393 | gaim_connection_error(js->gc, _("Invalid response from server")); | |
| 394 | else if(!strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
| 395 | if(xmlnode_get_child(packet, "bad-protocol")) { | |
| 396 | gaim_connection_error(js->gc, _("Bad Protocol")); | |
| 397 | } else if(xmlnode_get_child(packet, "encryption-required")) { | |
| 398 | js->gc->wants_to_die = TRUE; | |
| 399 | gaim_connection_error(js->gc, _("Encryption Required")); | |
| 400 | } else if(xmlnode_get_child(packet, "invalid-authzid")) { | |
| 401 | js->gc->wants_to_die = TRUE; | |
| 402 | gaim_connection_error(js->gc, _("Invalid authzid")); | |
| 403 | } else if(xmlnode_get_child(packet, "invalid-mechanism")) { | |
| 404 | js->gc->wants_to_die = TRUE; | |
| 405 | gaim_connection_error(js->gc, _("Invalid Mechanism")); | |
| 406 | } else if(xmlnode_get_child(packet, "invalid-realm")) { | |
| 407 | gaim_connection_error(js->gc, _("Invalid Realm")); | |
| 408 | } else if(xmlnode_get_child(packet, "mechanism-too-weak")) { | |
| 409 | js->gc->wants_to_die = TRUE; | |
| 410 | gaim_connection_error(js->gc, _("Mechanism Too Weak")); | |
| 411 | } else if(xmlnode_get_child(packet, "not-authorized")) { | |
| 412 | js->gc->wants_to_die = TRUE; | |
| 413 | gaim_connection_error(js->gc, _("Not Authorized")); | |
| 414 | } else if(xmlnode_get_child(packet, "temporary-auth-failure")) { | |
| 415 | gaim_connection_error(js->gc, | |
| 416 | _("Temporary Authentication Failure")); | |
| 417 | } else { | |
| 418 | gaim_connection_error(js->gc, _("Authentication Failure")); | |
| 419 | } | |
| 420 | } | |
| 421 | } |