source: protocols/jabber/sasl.c @ cc2cb2d

Last change on this file since cc2cb2d was 501b4e0, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-02T16:42:32Z

Added a useful error message for SASL negotiation failures and turned off
the little hack in jabber_write() for now because it breaks error handling.

  • Property mode set to 100644
File size: 9.0 KB
Line 
1/***************************************************************************\
2*                                                                           *
3*  BitlBee - An IRC to IM gateway                                           *
4*  Jabber module - SASL authentication                                      *
5*                                                                           *
6*  Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net>                   *
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 along  *
19*  with this program; if not, write to the Free Software Foundation, Inc.,  *
20*  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.              *
21*                                                                           *
22\***************************************************************************/
23
24#include "jabber.h"
25#include "base64.h"
26
27#define SASL_NS "urn:ietf:params:xml:ns:xmpp-sasl"
28
29xt_status sasl_pkt_mechanisms( struct xt_node *node, gpointer data )
30{
31        struct gaim_connection *gc = data;
32        struct jabber_data *jd = gc->proto_data;
33        struct xt_node *c, *reply;
34        char *s;
35        int sup_plain = 0, sup_digest = 0;
36       
37        if( !sasl_supported( gc ) )
38        {
39                /* Should abort this now, since we should already be doing
40                   IQ authentication. Strange things happen when you try
41                   to do both... */
42                serv_got_crap( gc, "XMPP 1.0 non-compliant server seems to support SASL, please report this as a BitlBee bug!" );
43                return XT_HANDLED;
44        }
45       
46        s = xt_find_attr( node, "xmlns" );
47        if( !s || strcmp( s, SASL_NS ) != 0 )
48        {
49                signoff( gc );
50                return XT_ABORT;
51        }
52       
53        c = node->children;
54        while( ( c = xt_find_node( c, "mechanism" ) ) )
55        {
56                if( c->text && g_strcasecmp( c->text, "PLAIN" ) == 0 )
57                        sup_plain = 1;
58                if( c->text && g_strcasecmp( c->text, "DIGEST-MD5" ) == 0 )
59                        sup_digest = 1;
60               
61                c = c->next;
62        }
63       
64        if( !sup_plain && !sup_digest )
65        {
66                hide_login_progress( gc, "No known SASL authentication schemes supported" );
67                signoff( gc );
68                return XT_ABORT;
69        }
70       
71        reply = xt_new_node( "auth", NULL, NULL );
72        xt_add_attr( reply, "xmlns", SASL_NS );
73       
74        if( sup_digest )
75        {
76                xt_add_attr( reply, "mechanism", "DIGEST-MD5" );
77               
78                /* The rest will be done later, when we receive a <challenge/>. */
79        }
80        else if( sup_plain )
81        {
82                int len;
83               
84                xt_add_attr( reply, "mechanism", "PLAIN" );
85               
86                /* With SASL PLAIN in XMPP, the text should be b64(\0user\0pass) */
87                len = strlen( jd->username ) + strlen( gc->acc->pass ) + 2;
88                s = g_malloc( len + 1 );
89                s[0] = 0;
90                strcpy( s + 1, jd->username );
91                strcpy( s + 2 + strlen( jd->username ), gc->acc->pass );
92                reply->text = base64_encode( s, len );
93                reply->text_len = strlen( reply->text );
94                g_free( s );
95        }
96       
97        if( !jabber_write_packet( gc, reply ) )
98        {
99                xt_free_node( reply );
100                return XT_ABORT;
101        }
102        xt_free_node( reply );
103       
104        /* To prevent classic authentication from happening. */
105        jd->flags |= JFLAG_STREAM_STARTED;
106       
107        return XT_HANDLED;
108}
109
110static char *sasl_get_part( char *data, char *field )
111{
112        int i, len;
113       
114        len = strlen( field );
115       
116        if( g_strncasecmp( data, field, len ) == 0 && data[len] == '=' )
117        {
118                i = strlen( field ) + 1;
119        }
120        else
121        {
122                for( i = 0; data[i]; i ++ )
123                {
124                        /* If we have a ", skip until it's closed again. */
125                        if( data[i] == '"' )
126                        {
127                                i ++;
128                                while( data[i] != '"' || data[i-1] == '\\' )
129                                        i ++;
130                        }
131                       
132                        /* If we got a comma, we got a new field. Check it. */
133                        if( data[i] == ',' &&
134                            g_strncasecmp( data + i + 1, field, len ) == 0 &&
135                            data[i+len+1] == '=' )
136                        {
137                                i += len + 2;
138                                break;
139                        }
140                }
141        }
142       
143        if( data[i] == '"' )
144        {
145                int j;
146                char *ret;
147               
148                i ++;
149                len = 0;
150                while( data[i+len] != '"' || data[i+len-1] == '\\' )
151                        len ++;
152               
153                ret = g_strndup( data + i, len );
154                for( i = j = 0; ret[i]; i ++ )
155                {
156                        if( ret[i] == '\\' )
157                        {
158                                ret[j++] = ret[++i];
159                        }
160                        else
161                        {
162                                ret[j++] = ret[i];
163                        }
164                }
165                ret[j] = 0;
166               
167                return ret;
168        }
169        else if( data[i] )
170        {
171                len = 0;
172                while( data[i+len] && data[i+len] != ',' )
173                        len ++;
174               
175                return g_strndup( data + i, len );
176        }
177        else
178        {
179                return NULL;
180        }
181}
182
183xt_status sasl_pkt_challenge( struct xt_node *node, gpointer data )
184{
185        struct gaim_connection *gc = data;
186        struct jabber_data *jd = gc->proto_data;
187        struct xt_node *reply = NULL;
188        char *nonce = NULL, *realm = NULL, *cnonce = NULL, cnonce_bin[30];
189        char *digest_uri = NULL;
190        char *dec = NULL;
191        char *s = NULL;
192        xt_status ret = XT_ABORT;
193       
194        if( node->text_len == 0 )
195                goto error;
196       
197        dec = frombase64( node->text );
198       
199        if( !( s = sasl_get_part( dec, "rspauth" ) ) )
200        {
201                /* See RFC 2831 for for information. */
202                md5_state_t A1, A2, H;
203                md5_byte_t A1r[16], A2r[16], Hr[16];
204                char A1h[33], A2h[33], Hh[33];
205                int i;
206               
207                nonce = sasl_get_part( dec, "nonce" );
208                realm = sasl_get_part( dec, "realm" );
209               
210                if( !nonce )
211                        goto error;
212               
213                /* Jabber.Org considers the realm part optional and doesn't
214                   specify one. Oh well, actually they're right, but still,
215                   don't know if this is right... */
216                if( !realm )
217                        realm = g_strdup( jd->server );
218               
219                random_bytes( (unsigned char *) cnonce_bin, sizeof( cnonce_bin ) );
220                cnonce = base64_encode( cnonce_bin, sizeof( cnonce_bin ) );
221                digest_uri = g_strdup_printf( "%s/%s", "xmpp", jd->server );
222               
223                /* Generate the MD5 hash of username:realm:password,
224                   I decided to call it H. */
225                md5_init( &H );
226                s = g_strdup_printf( "%s:%s:%s", jd->username, realm, gc->acc->pass );
227                md5_append( &H, (unsigned char *) s, strlen( s ) );
228                g_free( s );
229                md5_finish( &H, Hr );
230               
231                /* Now generate the hex. MD5 hash of H:nonce:cnonce, called A1. */
232                md5_init( &A1 );
233                s = g_strdup_printf( ":%s:%s", nonce, cnonce );
234                md5_append( &A1, Hr, 16 );
235                md5_append( &A1, (unsigned char *) s, strlen( s ) );
236                g_free( s );
237                md5_finish( &A1, A1r );
238                for( i = 0; i < 16; i ++ )
239                        sprintf( A1h + i * 2, "%02x", A1r[i] );
240               
241                /* A2... */
242                md5_init( &A2 );
243                s = g_strdup_printf( "%s:%s", "AUTHENTICATE", digest_uri );
244                md5_append( &A2, (unsigned char *) s, strlen( s ) );
245                g_free( s );
246                md5_finish( &A2, A2r );
247                for( i = 0; i < 16; i ++ )
248                        sprintf( A2h + i * 2, "%02x", A2r[i] );
249               
250                /* Final result: A1:nonce:00000001:cnonce:auth:A2. Let's reuse H for it. */
251                md5_init( &H );
252                s = g_strdup_printf( "%s:%s:%s:%s:%s:%s", A1h, nonce, "00000001", cnonce, "auth", A2h );
253                md5_append( &H, (unsigned char *) s, strlen( s ) );
254                g_free( s );
255                md5_finish( &H, Hr );
256                for( i = 0; i < 16; i ++ )
257                        sprintf( Hh + i * 2, "%02x", Hr[i] );
258               
259                /* Now build the SASL response string: */
260                g_free( dec );
261                dec = g_strdup_printf( "username=\"%s\",realm=\"%s\",nonce=\"%s\",cnonce=\"%s\","
262                                       "nc=%08x,qop=auth,digest-uri=\"%s\",response=%s,charset=%s",
263                                       jd->username, realm, nonce, cnonce, 1, digest_uri, Hh, "utf-8" );
264                s = tobase64( dec );
265        }
266        else
267        {
268                /* We found rspauth, but don't really care... */
269                g_free( s );
270                s = NULL;
271        }
272       
273        reply = xt_new_node( "response", s, NULL );
274        xt_add_attr( reply, "xmlns", SASL_NS );
275       
276        if( !jabber_write_packet( gc, reply ) )
277                goto silent_error;
278       
279        ret = XT_HANDLED;
280        goto silent_error;
281
282error:
283        hide_login_progress( gc, "Incorrect SASL challenge received" );
284        signoff( gc );
285
286silent_error:
287        g_free( digest_uri );
288        g_free( cnonce );
289        g_free( nonce );
290        g_free( realm );
291        g_free( dec );
292        g_free( s );
293        xt_free_node( reply );
294       
295        return ret;
296}
297
298xt_status sasl_pkt_result( struct xt_node *node, gpointer data )
299{
300        struct gaim_connection *gc = data;
301        struct jabber_data *jd = gc->proto_data;
302        char *s;
303       
304        s = xt_find_attr( node, "xmlns" );
305        if( !s || strcmp( s, SASL_NS ) != 0 )
306        {
307                signoff( gc );
308                return XT_ABORT;
309        }
310       
311        if( strcmp( node->name, "success" ) == 0 )
312        {
313                set_login_progress( gc, 1, "Authentication finished" );
314                jd->flags |= JFLAG_AUTHENTICATED | JFLAG_STREAM_RESTART;
315        }
316        else if( strcmp( node->name, "failure" ) == 0 )
317        {
318                hide_login_progress( gc, "Authentication failure" );
319                signoff( gc );
320                return XT_ABORT;
321        }
322       
323        return XT_HANDLED;
324}
325
326/* This one is needed to judge if we'll do authentication using IQ or SASL.
327   It's done by checking if the <stream:stream> from the server has a
328   version attribute. I don't know if this is the right way though... */
329gboolean sasl_supported( struct gaim_connection *gc )
330{
331        struct jabber_data *jd = gc->proto_data;
332       
333        return ( (void*) ( jd->xt && jd->xt->root && xt_find_attr( jd->xt->root, "version" ) ) ) != NULL;
334}
Note: See TracBrowser for help on using the repository browser.