1 /*
2 * purple - Jabber Protocol Plugin
3 *
4 * Purple is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
21 *
22 */
23 #include "internal.h"
24
25 #include "debug.h"
26 #include "cipher.h"
27 #include "util.h"
28 #include "xmlnode.h"
29
30 #include "auth_digest_md5.h"
31 #include "auth.h"
32 #include "jabber.h"
33
34 static JabberSaslState
digest_md5_start(JabberStream * js,xmlnode * packet,xmlnode ** response,char ** error)35 digest_md5_start(JabberStream *js, xmlnode *packet, xmlnode **response,
36 char **error)
37 {
38 xmlnode *auth = xmlnode_new("auth");
39 xmlnode_set_namespace(auth, NS_XMPP_SASL);
40 xmlnode_set_attrib(auth, "mechanism", "DIGEST-MD5");
41
42 *response = auth;
43 return JABBER_SASL_STATE_CONTINUE;
44 }
45
46 /* Parts of this algorithm are inspired by stuff in libgsasl */
jabber_auth_digest_md5_parse(const char * challenge)47 GHashTable* jabber_auth_digest_md5_parse(const char *challenge)
48 {
49 const char *token_start, *val_start, *val_end, *cur;
50 GHashTable *ret = g_hash_table_new_full(g_str_hash, g_str_equal,
51 g_free, g_free);
52
53 cur = challenge;
54 while(*cur != '\0') {
55 /* Find the end of the token */
56 gboolean in_quotes = FALSE;
57 char *name, *value = NULL;
58 token_start = cur;
59 while(*cur != '\0' && (in_quotes || (!in_quotes && *cur != ','))) {
60 if (*cur == '"')
61 in_quotes = !in_quotes;
62 cur++;
63 }
64
65 /* Find start of value. */
66 val_start = strchr(token_start, '=');
67 if (val_start == NULL || val_start > cur)
68 val_start = cur;
69
70 if (token_start != val_start) {
71 name = g_strndup(token_start, val_start - token_start);
72
73 if (val_start != cur) {
74 val_start++;
75 while (val_start != cur && (*val_start == ' ' || *val_start == '\t'
76 || *val_start == '\r' || *val_start == '\n'
77 || *val_start == '"'))
78 val_start++;
79
80 val_end = cur;
81 while (val_end >= val_start && (*val_end == ' ' || *val_end == ',' || *val_end == '\t'
82 || *val_end == '\r' || *val_end == '\n'
83 || *val_end == '"' || *val_end == '\0'))
84 val_end--;
85
86 if (val_end - val_start + 1 >= 0)
87 value = g_strndup(val_start, val_end - val_start + 1);
88 }
89
90 g_hash_table_replace(ret, name, value);
91 }
92
93 /* Find the start of the next token, if there is one */
94 if (*cur != '\0') {
95 cur++;
96 while (*cur == ' ' || *cur == ',' || *cur == '\t'
97 || *cur == '\r' || *cur == '\n')
98 cur++;
99 }
100 }
101
102 return ret;
103 }
104
105 static char *
generate_response_value(JabberID * jid,const char * passwd,const char * nonce,const char * cnonce,const char * a2,const char * realm)106 generate_response_value(JabberID *jid, const char *passwd, const char *nonce,
107 const char *cnonce, const char *a2, const char *realm)
108 {
109 PurpleCipher *cipher;
110 PurpleCipherContext *context;
111 guchar result[16];
112 size_t a1len;
113
114 gchar *a1, *convnode=NULL, *convpasswd = NULL, *ha1, *ha2, *kd, *x, *z;
115
116 if((convnode = g_convert(jid->node, -1, "iso-8859-1", "utf-8",
117 NULL, NULL, NULL)) == NULL) {
118 convnode = g_strdup(jid->node);
119 }
120 if(passwd && ((convpasswd = g_convert(passwd, -1, "iso-8859-1",
121 "utf-8", NULL, NULL, NULL)) == NULL)) {
122 convpasswd = g_strdup(passwd);
123 }
124
125 cipher = purple_ciphers_find_cipher("md5");
126 context = purple_cipher_context_new(cipher, NULL);
127
128 x = g_strdup_printf("%s:%s:%s", convnode, realm, convpasswd ? convpasswd : "");
129 purple_cipher_context_append(context, (const guchar *)x, strlen(x));
130 purple_cipher_context_digest(context, sizeof(result), result, NULL);
131
132 a1 = g_strdup_printf("xxxxxxxxxxxxxxxx:%s:%s", nonce, cnonce);
133 a1len = strlen(a1);
134 memmove(a1, result, 16);
135
136 purple_cipher_context_reset(context, NULL);
137 purple_cipher_context_append(context, (const guchar *)a1, a1len);
138 purple_cipher_context_digest(context, sizeof(result), result, NULL);
139
140 ha1 = purple_base16_encode(result, 16);
141
142 purple_cipher_context_reset(context, NULL);
143 purple_cipher_context_append(context, (const guchar *)a2, strlen(a2));
144 purple_cipher_context_digest(context, sizeof(result), result, NULL);
145
146 ha2 = purple_base16_encode(result, 16);
147
148 kd = g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1, nonce, cnonce, ha2);
149
150 purple_cipher_context_reset(context, NULL);
151 purple_cipher_context_append(context, (const guchar *)kd, strlen(kd));
152 purple_cipher_context_digest(context, sizeof(result), result, NULL);
153 purple_cipher_context_destroy(context);
154
155 z = purple_base16_encode(result, 16);
156
157 g_free(convnode);
158 g_free(convpasswd);
159 g_free(x);
160 g_free(a1);
161 g_free(ha1);
162 g_free(ha2);
163 g_free(kd);
164
165 return z;
166 }
167
168 static JabberSaslState
digest_md5_handle_challenge(JabberStream * js,xmlnode * packet,xmlnode ** response,char ** msg)169 digest_md5_handle_challenge(JabberStream *js, xmlnode *packet,
170 xmlnode **response, char **msg)
171 {
172 xmlnode *reply = NULL;
173 char *enc_in = xmlnode_get_data(packet);
174 char *dec_in;
175 char *enc_out;
176 GHashTable *parts;
177 JabberSaslState state = JABBER_SASL_STATE_CONTINUE;
178
179 if (!enc_in) {
180 *msg = g_strdup(_("Invalid response from server"));
181 return JABBER_SASL_STATE_FAIL;
182 }
183
184 dec_in = (char *)purple_base64_decode(enc_in, NULL);
185 purple_debug_misc("jabber", "decoded challenge (%"
186 G_GSIZE_FORMAT "): %s\n",
187 strlen(dec_in),
188 dec_in);
189
190 parts = jabber_auth_digest_md5_parse(dec_in);
191
192 if (g_hash_table_lookup(parts, "rspauth")) {
193 char *rspauth = g_hash_table_lookup(parts, "rspauth");
194 char *expected_rspauth = js->auth_mech_data;
195
196 if (rspauth && purple_strequal(rspauth, expected_rspauth)) {
197 reply = xmlnode_new("response");
198 xmlnode_set_namespace(reply, NS_XMPP_SASL);
199 } else {
200 *msg = g_strdup(_("Invalid challenge from server"));
201 state = JABBER_SASL_STATE_FAIL;
202 }
203 g_free(js->auth_mech_data);
204 js->auth_mech_data = NULL;
205 } else {
206 /* assemble a response, and send it */
207 /* see RFC 2831 */
208 char *realm;
209 char *nonce;
210
211 /* Make sure the auth string contains everything that should be there.
212 This isn't everything in RFC2831, but it is what we need. */
213
214 nonce = g_hash_table_lookup(parts, "nonce");
215
216 /* we're actually supposed to prompt the user for a realm if
217 * the server doesn't send one, but that really complicates things,
218 * so i'm not gonna worry about it until is poses a problem to
219 * someone, or I get really bored */
220 realm = g_hash_table_lookup(parts, "realm");
221 if(!realm)
222 realm = js->user->domain;
223
224 if (nonce == NULL || realm == NULL) {
225 *msg = g_strdup(_("Invalid challenge from server"));
226 state = JABBER_SASL_STATE_FAIL;
227 } else {
228 GString *response = g_string_new("");
229 char *a2;
230 char *auth_resp;
231 char *cnonce;
232
233 cnonce = g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL),
234 g_random_int());
235
236 a2 = g_strdup_printf("AUTHENTICATE:xmpp/%s", realm);
237 auth_resp = generate_response_value(js->user,
238 purple_connection_get_password(js->gc), nonce, cnonce, a2, realm);
239 g_free(a2);
240
241 a2 = g_strdup_printf(":xmpp/%s", realm);
242 js->auth_mech_data = generate_response_value(js->user,
243 purple_connection_get_password(js->gc), nonce, cnonce, a2, realm);
244 g_free(a2);
245
246 g_string_append_printf(response, "username=\"%s\"", js->user->node);
247 g_string_append_printf(response, ",realm=\"%s\"", realm);
248 g_string_append_printf(response, ",nonce=\"%s\"", nonce);
249 g_string_append_printf(response, ",cnonce=\"%s\"", cnonce);
250 g_string_append_printf(response, ",nc=00000001");
251 g_string_append_printf(response, ",qop=auth");
252 g_string_append_printf(response, ",digest-uri=\"xmpp/%s\"", realm);
253 g_string_append_printf(response, ",response=%s", auth_resp);
254 g_string_append_printf(response, ",charset=utf-8");
255
256 g_free(auth_resp);
257 g_free(cnonce);
258
259 enc_out = purple_base64_encode((guchar *)response->str, response->len);
260
261 purple_debug_misc("jabber", "decoded response (%"
262 G_GSIZE_FORMAT "): %s\n",
263 response->len, response->str);
264
265 reply = xmlnode_new("response");
266 xmlnode_set_namespace(reply, NS_XMPP_SASL);
267 xmlnode_insert_data(reply, enc_out, -1);
268
269 g_free(enc_out);
270
271 g_string_free(response, TRUE);
272 }
273 }
274
275 g_free(enc_in);
276 g_free(dec_in);
277 g_hash_table_destroy(parts);
278
279 *response = reply;
280 return state;
281 }
282
283 static void
digest_md5_dispose(JabberStream * js)284 digest_md5_dispose(JabberStream *js)
285 {
286 g_free(js->auth_mech_data);
287 js->auth_mech_data = NULL;
288 }
289
290 static JabberSaslMech digest_md5_mech = {
291 10, /* priority */
292 "DIGEST-MD5", /* name */
293 digest_md5_start,
294 digest_md5_handle_challenge,
295 NULL, /* handle_success */
296 NULL, /* handle_failure */
297 digest_md5_dispose,
298 };
299
jabber_auth_get_digest_md5_mech(void)300 JabberSaslMech *jabber_auth_get_digest_md5_mech(void)
301 {
302 return &digest_md5_mech;
303 }
304