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
29 #include "auth_digest_md5.h"
33 static JabberSaslState
34 digest_md5_start(JabberStream
*js
, PurpleXmlNode
*packet
, PurpleXmlNode
**response
,
37 PurpleXmlNode
*auth
= purple_xmlnode_new("auth");
38 purple_xmlnode_set_namespace(auth
, NS_XMPP_SASL
);
39 purple_xmlnode_set_attrib(auth
, "mechanism", "DIGEST-MD5");
42 return JABBER_SASL_STATE_CONTINUE
;
45 /* Parts of this algorithm are inspired by stuff in libgsasl */
46 GHashTable
* jabber_auth_digest_md5_parse(const char *challenge
)
48 const char *token_start
, *val_start
, *val_end
, *cur
;
49 GHashTable
*ret
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
54 /* Find the end of the token */
55 gboolean in_quotes
= FALSE
;
56 char *name
, *value
= NULL
;
58 while (*cur
!= '\0' && (in_quotes
|| *cur
!= ',')) {
60 in_quotes
= !in_quotes
;
64 /* Find start of value. */
65 val_start
= strchr(token_start
, '=');
66 if (val_start
== NULL
|| val_start
> cur
)
69 if (token_start
!= val_start
) {
70 name
= g_strndup(token_start
, val_start
- token_start
);
72 if (val_start
!= cur
) {
74 while (val_start
!= cur
&& (*val_start
== ' ' || *val_start
== '\t'
75 || *val_start
== '\r' || *val_start
== '\n'
76 || *val_start
== '"'))
80 while (val_end
>= val_start
&& (*val_end
== ' ' || *val_end
== ',' || *val_end
== '\t'
81 || *val_end
== '\r' || *val_end
== '\n'
82 || *val_end
== '"' || *val_end
== '\0'))
85 if (val_end
- val_start
+ 1 >= 0)
86 value
= g_strndup(val_start
, val_end
- val_start
+ 1);
89 g_hash_table_replace(ret
, name
, value
);
92 /* Find the start of the next token, if there is one */
95 while (*cur
== ' ' || *cur
== ',' || *cur
== '\t'
96 || *cur
== '\r' || *cur
== '\n')
105 generate_response_value(JabberID
*jid
, const char *passwd
, const char *nonce
,
106 const char *cnonce
, const char *a2
, const char *realm
)
109 gsize digest_len
= 16;
111 gchar
*a1
, *convnode
=NULL
, *convpasswd
= NULL
, *ha1
, *ha2
, *kd
, *x
, *z
;
113 if((convnode
= g_convert(jid
->node
, -1, "iso-8859-1", "utf-8",
114 NULL
, NULL
, NULL
)) == NULL
) {
115 convnode
= g_strdup(jid
->node
);
117 if(passwd
&& ((convpasswd
= g_convert(passwd
, -1, "iso-8859-1",
118 "utf-8", NULL
, NULL
, NULL
)) == NULL
)) {
119 convpasswd
= g_strdup(passwd
);
122 hash
= g_checksum_new(G_CHECKSUM_MD5
);
124 x
= g_strdup_printf("%s:%s:%s", convnode
, realm
, convpasswd
? convpasswd
: "");
125 g_checksum_update(hash
, (const guchar
*)x
, -1);
127 a1
= g_strdup_printf("xxxxxxxxxxxxxxxx:%s:%s", nonce
, cnonce
);
129 g_checksum_get_digest(hash
, (guint8
*)a1
, &digest_len
);
130 g_checksum_free(hash
);
132 ha1
= g_compute_checksum_for_string(G_CHECKSUM_MD5
, a1
, -1);
133 ha2
= g_compute_checksum_for_string(G_CHECKSUM_MD5
, a2
, -1);
135 kd
= g_strdup_printf("%s:%s:00000001:%s:auth:%s", ha1
, nonce
, cnonce
, ha2
);
137 z
= g_compute_checksum_for_string(G_CHECKSUM_MD5
, kd
, -1);
150 static JabberSaslState
151 digest_md5_handle_challenge(JabberStream
*js
, PurpleXmlNode
*packet
,
152 PurpleXmlNode
**response
, char **msg
)
154 PurpleXmlNode
*reply
= NULL
;
155 char *enc_in
= purple_xmlnode_get_data(packet
);
160 JabberSaslState state
= JABBER_SASL_STATE_CONTINUE
;
163 *msg
= g_strdup(_("Invalid response from server"));
164 return JABBER_SASL_STATE_FAIL
;
167 dec_in
= (char *)g_base64_decode(enc_in
, &size
);
168 purple_debug_misc("jabber", "decoded challenge (%"
169 G_GSIZE_FORMAT
"): %s\n",
173 parts
= jabber_auth_digest_md5_parse(dec_in
);
175 if (g_hash_table_lookup(parts
, "rspauth")) {
176 char *rspauth
= g_hash_table_lookup(parts
, "rspauth");
177 char *expected_rspauth
= js
->auth_mech_data
;
179 if (rspauth
&& purple_strequal(rspauth
, expected_rspauth
)) {
180 reply
= purple_xmlnode_new("response");
181 purple_xmlnode_set_namespace(reply
, NS_XMPP_SASL
);
183 *msg
= g_strdup(_("Invalid challenge from server"));
184 state
= JABBER_SASL_STATE_FAIL
;
186 g_free(js
->auth_mech_data
);
187 js
->auth_mech_data
= NULL
;
189 /* assemble a response, and send it */
194 /* Make sure the auth string contains everything that should be there.
195 This isn't everything in RFC2831, but it is what we need. */
197 nonce
= g_hash_table_lookup(parts
, "nonce");
199 /* we're actually supposed to prompt the user for a realm if
200 * the server doesn't send one, but that really complicates things,
201 * so i'm not gonna worry about it until is poses a problem to
202 * someone, or I get really bored */
203 realm
= g_hash_table_lookup(parts
, "realm");
205 realm
= js
->user
->domain
;
207 if (nonce
== NULL
|| realm
== NULL
) {
208 *msg
= g_strdup(_("Invalid challenge from server"));
209 state
= JABBER_SASL_STATE_FAIL
;
211 GString
*response
= g_string_new("");
216 cnonce
= g_strdup_printf("%x%u%x", g_random_int(), (int)time(NULL
),
219 a2
= g_strdup_printf("AUTHENTICATE:xmpp/%s", realm
);
220 auth_resp
= generate_response_value(js
->user
,
221 purple_connection_get_password(js
->gc
), nonce
, cnonce
, a2
, realm
);
224 a2
= g_strdup_printf(":xmpp/%s", realm
);
225 js
->auth_mech_data
= generate_response_value(js
->user
,
226 purple_connection_get_password(js
->gc
), nonce
, cnonce
, a2
, realm
);
229 g_string_append_printf(response
, "username=\"%s\"", js
->user
->node
);
230 g_string_append_printf(response
, ",realm=\"%s\"", realm
);
231 g_string_append_printf(response
, ",nonce=\"%s\"", nonce
);
232 g_string_append_printf(response
, ",cnonce=\"%s\"", cnonce
);
233 g_string_append_printf(response
, ",nc=00000001");
234 g_string_append_printf(response
, ",qop=auth");
235 g_string_append_printf(response
, ",digest-uri=\"xmpp/%s\"", realm
);
236 g_string_append_printf(response
, ",response=%s", auth_resp
);
237 g_string_append_printf(response
, ",charset=utf-8");
242 enc_out
= g_base64_encode((guchar
*)response
->str
, response
->len
);
244 purple_debug_misc("jabber", "decoded response (%"
245 G_GSIZE_FORMAT
"): %s\n",
246 response
->len
, response
->str
);
248 reply
= purple_xmlnode_new("response");
249 purple_xmlnode_set_namespace(reply
, NS_XMPP_SASL
);
250 purple_xmlnode_insert_data(reply
, enc_out
, -1);
254 g_string_free(response
, TRUE
);
260 g_hash_table_destroy(parts
);
267 digest_md5_dispose(JabberStream
*js
)
269 g_free(js
->auth_mech_data
);
270 js
->auth_mech_data
= NULL
;
273 static JabberSaslMech digest_md5_mech
= {
275 "DIGEST-MD5", /* name */
277 digest_md5_handle_challenge
,
278 NULL
, /* handle_success */
279 NULL
, /* handle_failure */
283 JabberSaslMech
*jabber_auth_get_digest_md5_mech(void)
285 return &digest_md5_mech
;