2 * purple - Jabber Protocol Plugin
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
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.
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.
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
26 #include "ciphers/md5hash.h"
30 #include "auth_digest_md5.h"
34 static JabberSaslState
35 digest_md5_start(JabberStream
*js
, PurpleXmlNode
*packet
, PurpleXmlNode
**response
,
38 PurpleXmlNode
*auth
= purple_xmlnode_new("auth");
39 purple_xmlnode_set_namespace(auth
, NS_XMPP_SASL
);
40 purple_xmlnode_set_attrib(auth
, "mechanism", "DIGEST-MD5");
43 return JABBER_SASL_STATE_CONTINUE
;
46 /* Parts of this algorithm are inspired by stuff in libgsasl */
47 GHashTable
* jabber_auth_digest_md5_parse(const char *challenge
)
49 const char *token_start
, *val_start
, *val_end
, *cur
;
50 GHashTable
*ret
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
55 /* Find the end of the token */
56 gboolean in_quotes
= FALSE
;
57 char *name
, *value
= NULL
;
59 while(*cur
!= '\0' && (in_quotes
|| (!in_quotes
&& *cur
!= ','))) {
61 in_quotes
= !in_quotes
;
65 /* Find start of value. */
66 val_start
= strchr(token_start
, '=');
67 if (val_start
== NULL
|| val_start
> cur
)
70 if (token_start
!= val_start
) {
71 name
= g_strndup(token_start
, val_start
- token_start
);
73 if (val_start
!= cur
) {
75 while (val_start
!= cur
&& (*val_start
== ' ' || *val_start
== '\t'
76 || *val_start
== '\r' || *val_start
== '\n'
77 || *val_start
== '"'))
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'))
86 if (val_end
- val_start
+ 1 >= 0)
87 value
= g_strndup(val_start
, val_end
- val_start
+ 1);
90 g_hash_table_replace(ret
, name
, value
);
93 /* Find the start of the next token, if there is one */
96 while (*cur
== ' ' || *cur
== ',' || *cur
== '\t'
97 || *cur
== '\r' || *cur
== '\n')
106 generate_response_value(JabberID
*jid
, const char *passwd
, const char *nonce
,
107 const char *cnonce
, const char *a2
, const char *realm
)
113 gchar
*a1
, *convnode
=NULL
, *convpasswd
= NULL
, *ha1
, *ha2
, *kd
, *x
, *z
;
115 if((convnode
= g_convert(jid
->node
, -1, "iso-8859-1", "utf-8",
116 NULL
, NULL
, NULL
)) == NULL
) {
117 convnode
= g_strdup(jid
->node
);
119 if(passwd
&& ((convpasswd
= g_convert(passwd
, -1, "iso-8859-1",
120 "utf-8", NULL
, NULL
, NULL
)) == NULL
)) {
121 convpasswd
= g_strdup(passwd
);
124 hash
= purple_md5_hash_new();
126 x
= g_strdup_printf("%s:%s:%s", convnode
, realm
, convpasswd
? convpasswd
: "");
127 purple_hash_append(hash
, (const guchar
*)x
, strlen(x
));
128 purple_hash_digest(hash
, result
, sizeof(result
));
130 a1
= g_strdup_printf("xxxxxxxxxxxxxxxx:%s:%s", nonce
, cnonce
);
132 g_memmove(a1
, result
, 16);
134 purple_hash_reset(hash
);
135 purple_hash_append(hash
, (const guchar
*)a1
, a1len
);
136 purple_hash_digest(hash
, result
, sizeof(result
));
138 ha1
= purple_base16_encode(result
, 16);
140 purple_hash_reset(hash
);
141 purple_hash_append(hash
, (const guchar
*)a2
, strlen(a2
));
142 purple_hash_digest(hash
, result
, sizeof(result
));
144 ha2
= purple_base16_encode(result
, 16);
146 kd
= g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1
, nonce
, cnonce
, ha2
);
148 purple_hash_reset(hash
);
149 purple_hash_append(hash
, (const guchar
*)kd
, strlen(kd
));
150 purple_hash_digest(hash
, result
, sizeof(result
));
151 g_object_unref(hash
);
153 z
= purple_base16_encode(result
, 16);
166 static JabberSaslState
167 digest_md5_handle_challenge(JabberStream
*js
, PurpleXmlNode
*packet
,
168 PurpleXmlNode
**response
, char **msg
)
170 PurpleXmlNode
*reply
= NULL
;
171 char *enc_in
= purple_xmlnode_get_data(packet
);
175 JabberSaslState state
= JABBER_SASL_STATE_CONTINUE
;
178 *msg
= g_strdup(_("Invalid response from server"));
179 return JABBER_SASL_STATE_FAIL
;
182 dec_in
= (char *)purple_base64_decode(enc_in
, NULL
);
183 purple_debug_misc("jabber", "decoded challenge (%"
184 G_GSIZE_FORMAT
"): %s\n",
188 parts
= jabber_auth_digest_md5_parse(dec_in
);
190 if (g_hash_table_lookup(parts
, "rspauth")) {
191 char *rspauth
= g_hash_table_lookup(parts
, "rspauth");
192 char *expected_rspauth
= js
->auth_mech_data
;
194 if (rspauth
&& purple_strequal(rspauth
, expected_rspauth
)) {
195 reply
= purple_xmlnode_new("response");
196 purple_xmlnode_set_namespace(reply
, NS_XMPP_SASL
);
198 *msg
= g_strdup(_("Invalid challenge from server"));
199 state
= JABBER_SASL_STATE_FAIL
;
201 g_free(js
->auth_mech_data
);
202 js
->auth_mech_data
= NULL
;
204 /* assemble a response, and send it */
209 /* Make sure the auth string contains everything that should be there.
210 This isn't everything in RFC2831, but it is what we need. */
212 nonce
= g_hash_table_lookup(parts
, "nonce");
214 /* we're actually supposed to prompt the user for a realm if
215 * the server doesn't send one, but that really complicates things,
216 * so i'm not gonna worry about it until is poses a problem to
217 * someone, or I get really bored */
218 realm
= g_hash_table_lookup(parts
, "realm");
220 realm
= js
->user
->domain
;
222 if (nonce
== NULL
|| realm
== NULL
) {
223 *msg
= g_strdup(_("Invalid challenge from server"));
224 state
= JABBER_SASL_STATE_FAIL
;
226 GString
*response
= g_string_new("");
231 cnonce
= g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL
),
234 a2
= g_strdup_printf("AUTHENTICATE:xmpp/%s", realm
);
235 auth_resp
= generate_response_value(js
->user
,
236 purple_connection_get_password(js
->gc
), nonce
, cnonce
, a2
, realm
);
239 a2
= g_strdup_printf(":xmpp/%s", realm
);
240 js
->auth_mech_data
= generate_response_value(js
->user
,
241 purple_connection_get_password(js
->gc
), nonce
, cnonce
, a2
, realm
);
244 g_string_append_printf(response
, "username=\"%s\"", js
->user
->node
);
245 g_string_append_printf(response
, ",realm=\"%s\"", realm
);
246 g_string_append_printf(response
, ",nonce=\"%s\"", nonce
);
247 g_string_append_printf(response
, ",cnonce=\"%s\"", cnonce
);
248 g_string_append_printf(response
, ",nc=00000001");
249 g_string_append_printf(response
, ",qop=auth");
250 g_string_append_printf(response
, ",digest-uri=\"xmpp/%s\"", realm
);
251 g_string_append_printf(response
, ",response=%s", auth_resp
);
252 g_string_append_printf(response
, ",charset=utf-8");
257 enc_out
= purple_base64_encode((guchar
*)response
->str
, response
->len
);
259 purple_debug_misc("jabber", "decoded response (%"
260 G_GSIZE_FORMAT
"): %s\n",
261 response
->len
, response
->str
);
263 reply
= purple_xmlnode_new("response");
264 purple_xmlnode_set_namespace(reply
, NS_XMPP_SASL
);
265 purple_xmlnode_insert_data(reply
, enc_out
, -1);
269 g_string_free(response
, TRUE
);
275 g_hash_table_destroy(parts
);
282 digest_md5_dispose(JabberStream
*js
)
284 g_free(js
->auth_mech_data
);
285 js
->auth_mech_data
= NULL
;
288 static JabberSaslMech digest_md5_mech
= {
290 "DIGEST-MD5", /* name */
292 digest_md5_handle_challenge
,
293 NULL
, /* handle_success */
294 NULL
, /* handle_failure */
298 JabberSaslMech
*jabber_auth_get_digest_md5_mech(void)
300 return &digest_md5_mech
;