Mercurial > pidgin
annotate src/protocols/jabber/auth.c @ 7703:e0e73a26f130
[gaim-migrate @ 8348]
ladies and gentlemen, i give you PLAIN auth for SASL
committer: Tailor Script <tailor@pidgin.im>
author | Nathan Walp <nwalp@pidgin.im> |
---|---|
date | Tue, 02 Dec 2003 21:18:39 +0000 |
parents | 28100686bde9 |
children | 3d79f9cab273 |
rev | line source |
---|---|
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, |
7642 | 49 "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>", -1); |
7157 | 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"); | |
7703 | 78 |
7645 | 79 if(digest_md5) { |
7291 | 80 xmlnode_set_attrib(auth, "mechanism", "DIGEST-MD5"); |
81 js->auth_type = JABBER_AUTH_DIGEST_MD5; | |
82 } else if(plain) { | |
7703 | 83 GString *response = g_string_new(""); |
84 char *enc_out; | |
85 | |
86 response = g_string_append_len(response, "\0", 1); | |
87 response = g_string_append(response, js->user->node); | |
88 response = g_string_append_len(response, "\0", 1); | |
89 response = g_string_append(response, | |
90 gaim_account_get_password(js->gc->account)); | |
91 | |
92 enc_out = gaim_base64_encode(response->str, response->len); | |
93 | |
7642 | 94 xmlnode_set_attrib(auth, "mechanism", "PLAIN"); |
7703 | 95 xmlnode_insert_data(auth, enc_out, -1); |
96 g_free(enc_out); | |
97 | |
7291 | 98 js->auth_type = JABBER_AUTH_PLAIN; |
7014 | 99 } else { |
100 gaim_connection_error(js->gc, | |
101 _("Server does not use any supported authentication method")); | |
7291 | 102 xmlnode_free(auth); |
103 return; | |
7014 | 104 } |
7703 | 105 |
7291 | 106 jabber_send(js, auth); |
107 xmlnode_free(auth); | |
7014 | 108 } |
109 | |
7395 | 110 static void auth_old_result_cb(JabberStream *js, xmlnode *packet, gpointer data) |
7014 | 111 { |
112 const char *type = xmlnode_get_attrib(packet, "type"); | |
113 | |
114 if(type && !strcmp(type, "error")) { | |
115 xmlnode *error = xmlnode_get_child(packet, "error"); | |
116 const char *err_code; | |
117 char *err_text; | |
118 char *buf; | |
119 | |
120 err_code = xmlnode_get_attrib(error, "code"); | |
121 err_text = xmlnode_get_data(error); | |
122 | |
123 if(!err_code) | |
124 err_code = ""; | |
125 if(!err_text) | |
126 err_text = g_strdup(_("Unknown")); | |
127 | |
128 if(!strcmp(err_code, "401")) | |
129 js->gc->wants_to_die = TRUE; | |
130 | |
131 buf = g_strdup_printf("Error %s: %s", | |
132 err_code, err_text); | |
133 | |
134 gaim_connection_error(js->gc, buf); | |
135 g_free(err_text); | |
136 g_free(buf); | |
137 } | |
138 jabber_stream_set_state(js, JABBER_STREAM_CONNECTED); | |
139 } | |
140 | |
7395 | 141 static void auth_old_cb(JabberStream *js, xmlnode *packet, gpointer data) |
7014 | 142 { |
143 JabberIq *iq; | |
144 xmlnode *query, *x; | |
145 gboolean digest = FALSE; | |
7514 | 146 const char *type = xmlnode_get_attrib(packet, "type"); |
7014 | 147 const char *pw = gaim_account_get_password(js->gc->account); |
148 | |
7514 | 149 if(!type) { |
7014 | 150 return; |
7515 | 151 } else if(!strcmp(type, "error")) { |
7644 | 152 /* XXX: still need to handle XMPP-style errors */ |
153 xmlnode *error; | |
154 char *buf, *err_txt = NULL; | |
155 const char *code = NULL; | |
156 if((error = xmlnode_get_child(packet, "error"))) { | |
157 code = xmlnode_get_attrib(error, "code"); | |
158 err_txt = xmlnode_get_data(error); | |
159 } | |
160 buf = g_strdup_printf("%s%s%s", code ? code : "", code ? ": " : "", | |
161 err_txt ? err_txt : _("Unknown Error")); | |
162 gaim_connection_error(js->gc, buf); | |
163 if(err_txt) | |
164 g_free(err_txt); | |
165 g_free(buf); | |
7515 | 166 } else if(!strcmp(type, "result")) { |
7514 | 167 query = xmlnode_get_child(packet, "query"); |
168 if(js->stream_id && xmlnode_get_child(query, "digest")) { | |
169 digest = TRUE; | |
170 } else if(!xmlnode_get_child(query, "password")) { | |
171 gaim_connection_error(js->gc, | |
172 _("Server does not use any supported authentication method")); | |
173 return; | |
174 } | |
7014 | 175 |
7514 | 176 iq = jabber_iq_new_query(js, JABBER_IQ_SET, "jabber:iq:auth"); |
177 query = xmlnode_get_child(iq->node, "query"); | |
178 x = xmlnode_new_child(query, "username"); | |
179 xmlnode_insert_data(x, js->user->node, -1); | |
180 x = xmlnode_new_child(query, "resource"); | |
181 xmlnode_insert_data(x, js->user->resource, -1); | |
7014 | 182 |
7514 | 183 if(digest) { |
184 unsigned char hashval[20]; | |
185 char *s, h[41], *p; | |
186 int i; | |
7014 | 187 |
7514 | 188 x = xmlnode_new_child(query, "digest"); |
189 s = g_strdup_printf("%s%s", js->stream_id, pw); | |
190 shaBlock((unsigned char *)s, strlen(s), hashval); | |
191 p = h; | |
192 for(i=0; i<20; i++, p+=2) | |
193 snprintf(p, 3, "%02x", hashval[i]); | |
194 xmlnode_insert_data(x, h, -1); | |
195 g_free(s); | |
196 } else { | |
197 x = xmlnode_new_child(query, "password"); | |
198 xmlnode_insert_data(x, pw, -1); | |
199 } | |
200 | |
201 jabber_iq_set_callback(iq, auth_old_result_cb, NULL); | |
202 | |
203 jabber_iq_send(iq); | |
7014 | 204 } |
205 } | |
206 | |
207 void jabber_auth_start_old(JabberStream *js) | |
208 { | |
209 JabberIq *iq; | |
210 xmlnode *query, *username; | |
211 | |
212 iq = jabber_iq_new_query(js, JABBER_IQ_GET, "jabber:iq:auth"); | |
213 | |
214 query = xmlnode_get_child(iq->node, "query"); | |
215 username = xmlnode_new_child(query, "username"); | |
216 xmlnode_insert_data(username, js->user->node, -1); | |
217 | |
7395 | 218 jabber_iq_set_callback(iq, auth_old_cb, NULL); |
7014 | 219 |
220 jabber_iq_send(iq); | |
221 } | |
222 | |
223 static GHashTable* parse_challenge(const char *challenge) | |
224 { | |
225 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal, | |
226 g_free, g_free); | |
227 char **pairs; | |
228 int i; | |
229 | |
230 pairs = g_strsplit(challenge, ",", -1); | |
231 | |
232 for(i=0; pairs[i]; i++) { | |
233 char **keyval = g_strsplit(pairs[i], "=", 2); | |
234 if(keyval[0] && keyval[1]) { | |
235 if(keyval[1][0] == '"' && keyval[1][strlen(keyval[1])-1] == '"') | |
236 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strndup(keyval[1]+1, strlen(keyval[1])-2)); | |
237 else | |
238 g_hash_table_replace(ret, g_strdup(keyval[0]), g_strdup(keyval[1])); | |
239 } | |
240 g_strfreev(keyval); | |
241 } | |
242 | |
243 g_strfreev(pairs); | |
244 | |
245 return ret; | |
246 } | |
247 | |
248 static unsigned char* | |
249 generate_response_value(JabberID *jid, const char *passwd, const char *nonce, | |
7267 | 250 const char *cnonce, const char *a2, const char *realm) |
7014 | 251 { |
252 md5_state_t ctx; | |
253 md5_byte_t result[16]; | |
254 | |
255 char *x, *y, *a1, *ha1, *ha2, *kd, *z; | |
256 | |
7267 | 257 x = g_strdup_printf("%s:%s:%s", jid->node, realm, passwd); |
7014 | 258 md5_init(&ctx); |
259 md5_append(&ctx, x, strlen(x)); | |
260 md5_finish(&ctx, result); | |
261 | |
262 y = g_strndup(result, 16); | |
263 | |
7395 | 264 a1 = g_strdup_printf("%s:%s:%s:%s@%s", y, nonce, cnonce, jid->node, |
265 jid->domain); | |
7014 | 266 |
267 md5_init(&ctx); | |
268 md5_append(&ctx, a1, strlen(a1)); | |
269 md5_finish(&ctx, result); | |
270 | |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
271 ha1 = gaim_base16_encode(result, 16); |
7014 | 272 |
273 md5_init(&ctx); | |
274 md5_append(&ctx, a2, strlen(a2)); | |
275 md5_finish(&ctx, result); | |
276 | |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
277 ha2 = gaim_base16_encode(result, 16); |
7014 | 278 |
279 kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2); | |
280 | |
281 md5_init(&ctx); | |
282 md5_append(&ctx, kd, strlen(kd)); | |
283 md5_finish(&ctx, result); | |
284 | |
7106
db6bd3e794d8
[gaim-migrate @ 7671]
Christian Hammond <chipx86@chipx86.com>
parents:
7014
diff
changeset
|
285 z = gaim_base16_encode(result, 16); |
7014 | 286 |
287 g_free(x); | |
288 g_free(y); | |
289 g_free(a1); | |
290 g_free(ha1); | |
291 g_free(ha2); | |
292 g_free(kd); | |
293 | |
294 return z; | |
295 } | |
296 | |
297 void | |
298 jabber_auth_handle_challenge(JabberStream *js, xmlnode *packet) | |
299 { | |
300 | |
7703 | 301 if(js->auth_type == JABBER_AUTH_DIGEST_MD5) { |
7291 | 302 char *enc_in = xmlnode_get_data(packet); |
303 char *dec_in; | |
304 char *enc_out; | |
305 GHashTable *parts; | |
7014 | 306 |
7395 | 307 if(!enc_in) { |
308 gaim_connection_error(js->gc, _("Invalid response from server")); | |
309 return; | |
310 } | |
311 | |
7291 | 312 gaim_base64_decode(enc_in, &dec_in, NULL); |
7395 | 313 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded challenge (%d): %s\n", |
314 strlen(dec_in), dec_in); | |
7291 | 315 |
316 parts = parse_challenge(dec_in); | |
7014 | 317 |
318 | |
7291 | 319 if (g_hash_table_lookup(parts, "rspauth")) { |
320 char *rspauth = g_hash_table_lookup(parts, "rspauth"); | |
7014 | 321 |
322 | |
7291 | 323 if(rspauth && js->expected_rspauth && |
324 !strcmp(rspauth, js->expected_rspauth)) { | |
325 jabber_send_raw(js, | |
7642 | 326 "<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl' />", |
327 -1); | |
7291 | 328 } else { |
329 gaim_connection_error(js->gc, _("Invalid challenge from server")); | |
330 } | |
331 g_free(js->expected_rspauth); | |
332 } else { | |
333 /* assemble a response, and send it */ | |
334 /* see RFC 2831 */ | |
335 GString *response = g_string_new(""); | |
336 char *a2; | |
337 char *auth_resp; | |
338 char *buf; | |
339 char *cnonce; | |
340 char *realm; | |
341 char *nonce; | |
7014 | 342 |
7291 | 343 /* we're actually supposed to prompt the user for a realm if |
344 * the server doesn't send one, but that really complicates things, | |
345 * so i'm not gonna worry about it until is poses a problem to | |
346 * someone, or I get really bored */ | |
347 realm = g_hash_table_lookup(parts, "realm"); | |
348 if(!realm) | |
349 realm = js->user->domain; | |
7014 | 350 |
7291 | 351 cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL), |
352 g_random_int()); | |
353 nonce = g_hash_table_lookup(parts, "nonce"); | |
7014 | 354 |
355 | |
7291 | 356 a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm); |
357 auth_resp = generate_response_value(js->user, | |
358 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
359 g_free(a2); | |
360 | |
361 a2 = g_strdup_printf(":xmpp/%s", realm); | |
362 js->expected_rspauth = generate_response_value(js->user, | |
363 gaim_account_get_password(js->gc->account), nonce, cnonce, a2, realm); | |
364 g_free(a2); | |
365 | |
366 | |
367 g_string_append_printf(response, "username=\"%s\"", js->user->node); | |
368 g_string_append_printf(response, ",realm=\"%s\"", realm); | |
369 g_string_append_printf(response, ",nonce=\"%s\"", nonce); | |
370 g_string_append_printf(response, ",cnonce=\"%s\"", cnonce); | |
371 g_string_append_printf(response, ",nc=00000001"); | |
372 g_string_append_printf(response, ",qop=auth"); | |
373 g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm); | |
374 g_string_append_printf(response, ",response=%s", auth_resp); | |
375 g_string_append_printf(response, ",charset=utf-8"); | |
7395 | 376 g_string_append_printf(response, ",authzid=\"%s@%s\"", |
377 js->user->node, js->user->domain); | |
7291 | 378 |
379 g_free(auth_resp); | |
380 g_free(cnonce); | |
381 | |
382 enc_out = gaim_base64_encode(response->str, response->len); | |
383 | |
384 gaim_debug(GAIM_DEBUG_MISC, "jabber", "decoded response (%d): %s\n", response->len, response->str); | |
385 | |
386 buf = g_strdup_printf("<response xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>%s</response>", enc_out); | |
387 | |
7642 | 388 jabber_send_raw(js, buf, -1); |
7291 | 389 |
390 g_free(buf); | |
391 | |
392 g_free(enc_out); | |
393 | |
394 g_string_free(response, TRUE); | |
7014 | 395 } |
7291 | 396 |
397 g_free(enc_in); | |
398 g_free(dec_in); | |
399 g_hash_table_destroy(parts); | |
7014 | 400 } |
401 } | |
402 | |
403 void jabber_auth_handle_success(JabberStream *js, xmlnode *packet) | |
404 { | |
405 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
406 | |
407 if(!ns || strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
408 gaim_connection_error(js->gc, _("Invalid response from server")); | |
409 return; | |
410 } | |
411 | |
412 jabber_stream_set_state(js, JABBER_STREAM_REINITIALIZING); | |
413 } | |
414 | |
415 void jabber_auth_handle_failure(JabberStream *js, xmlnode *packet) | |
416 { | |
417 const char *ns = xmlnode_get_attrib(packet, "xmlns"); | |
418 | |
419 if(!ns) | |
420 gaim_connection_error(js->gc, _("Invalid response from server")); | |
421 else if(!strcmp(ns, "urn:ietf:params:xml:ns:xmpp-sasl")) { | |
422 if(xmlnode_get_child(packet, "bad-protocol")) { | |
423 gaim_connection_error(js->gc, _("Bad Protocol")); | |
424 } else if(xmlnode_get_child(packet, "encryption-required")) { | |
425 js->gc->wants_to_die = TRUE; | |
426 gaim_connection_error(js->gc, _("Encryption Required")); | |
427 } else if(xmlnode_get_child(packet, "invalid-authzid")) { | |
428 js->gc->wants_to_die = TRUE; | |
429 gaim_connection_error(js->gc, _("Invalid authzid")); | |
430 } else if(xmlnode_get_child(packet, "invalid-mechanism")) { | |
431 js->gc->wants_to_die = TRUE; | |
432 gaim_connection_error(js->gc, _("Invalid Mechanism")); | |
433 } else if(xmlnode_get_child(packet, "invalid-realm")) { | |
434 gaim_connection_error(js->gc, _("Invalid Realm")); | |
435 } else if(xmlnode_get_child(packet, "mechanism-too-weak")) { | |
436 js->gc->wants_to_die = TRUE; | |
437 gaim_connection_error(js->gc, _("Mechanism Too Weak")); | |
438 } else if(xmlnode_get_child(packet, "not-authorized")) { | |
439 js->gc->wants_to_die = TRUE; | |
440 gaim_connection_error(js->gc, _("Not Authorized")); | |
441 } else if(xmlnode_get_child(packet, "temporary-auth-failure")) { | |
442 gaim_connection_error(js->gc, | |
443 _("Temporary Authentication Failure")); | |
444 } else { | |
445 gaim_connection_error(js->gc, _("Authentication Failure")); | |
446 } | |
447 } | |
448 } |