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