source: lib/ssl_gnutls.c @ e132b60

Last change on this file since e132b60 was 2fb1262, checked in by Wilmer van der Gaast <wilmer@…>, at 2012-11-11T18:22:39Z

Tiny cleanup. Fixing some memory leaks (why did I not notice so far that
those free()s were commented out?).

  • Property mode set to 100644
File size: 11.1 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[59cd92b]4  * Copyright 2002-2011 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* SSL module - GnuTLS version                                          */
8
9/*
10  This program is free software; you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 2 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License with
21  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23  Suite 330, Boston, MA  02111-1307  USA
24*/
25
26#include <gnutls/gnutls.h>
[486ddb5]27#include <gnutls/x509.h>
[83e47ec]28#include <gcrypt.h>
[701acdd4]29#include <fcntl.h>
30#include <unistd.h>
[b7d3cc34]31#include "proxy.h"
32#include "ssl_client.h"
33#include "sock.h"
34#include "stdlib.h"
[486ddb5]35#include "bitlbee.h"
[b7d3cc34]36
[701acdd4]37int ssl_errno = 0;
38
[b7d3cc34]39static gboolean initialized = FALSE;
[2fb1262]40gnutls_certificate_credentials_t xcred;
[b7d3cc34]41
[56f260a]42#include <limits.h>
43
44#if defined(ULONG_MAX) && ULONG_MAX > 4294967295UL
45#define GNUTLS_STUPID_CAST (long)
46#else
47#define GNUTLS_STUPID_CAST (int)
48#endif
49
[ca974d7]50#define SSLDEBUG 0
51
[b7d3cc34]52struct scd
53{
[3d64e5b]54        ssl_input_function func;
[b7d3cc34]55        gpointer data;
56        int fd;
57        gboolean established;
[701acdd4]58        int inpa;
[486ddb5]59        char *hostname;
60        gboolean verify;
[b7d3cc34]61       
[2fb1262]62        gnutls_session_t session;
[b7d3cc34]63};
64
[2b7d2d1]65static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond );
[c1ed6527]66static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond );
67static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond );
[b7d3cc34]68
[59cd92b]69static void ssl_deinit( void );
[b7d3cc34]70
[632f3d4]71static void ssl_log( int level, const char *line )
72{
73        printf( "%d %s", level, line );
74}
75
[ba5add7]76void ssl_init( void )
77{
[83e47ec]78        if( initialized )
79                return;
80       
[ba5add7]81        gnutls_global_init();
[59cd92b]82        gnutls_certificate_allocate_credentials( &xcred );
83        if( global.conf->cafile )
84        {
85                gnutls_certificate_set_x509_trust_file( xcred, global.conf->cafile, GNUTLS_X509_FMT_PEM );
[59c03bd]86               
87                /* Not needed in GnuTLS 2.11+ but we support older versions for now. */
[59cd92b]88                gnutls_certificate_set_verify_flags( xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT );
89        }
[ba5add7]90        initialized = TRUE;
[59cd92b]91       
[632f3d4]92        gnutls_global_set_log_function( ssl_log );
93        /*
94        gnutls_global_set_log_level( 3 );
95        */
96       
[59cd92b]97        atexit( ssl_deinit );
98}
99
100static void ssl_deinit( void )
101{
102        gnutls_global_deinit();
103        gnutls_certificate_free_credentials( xcred );
[ba5add7]104}
105
[a72dc2b]106void *ssl_connect( char *host, int port, gboolean verify, ssl_input_function func, gpointer data )
[b7d3cc34]107{
108        struct scd *conn = g_new0( struct scd, 1 );
109       
110        conn->fd = proxy_connect( host, port, ssl_connected, conn );
111        conn->func = func;
112        conn->data = data;
[701acdd4]113        conn->inpa = -1;
[a72dc2b]114        conn->hostname = g_strdup( host );
115        conn->verify = verify && global.conf->cafile;
[b7d3cc34]116       
117        if( conn->fd < 0 )
118        {
119                g_free( conn );
[42127dc]120                return NULL;
[b7d3cc34]121        }
122       
[42127dc]123        return conn;
124}
125
[486ddb5]126void *ssl_starttls( int fd, char *hostname, gboolean verify, ssl_input_function func, gpointer data )
[42127dc]127{
128        struct scd *conn = g_new0( struct scd, 1 );
[b7d3cc34]129       
[42127dc]130        conn->fd = fd;
131        conn->func = func;
132        conn->data = data;
133        conn->inpa = -1;
[2fb1262]134        conn->hostname = g_strdup( hostname );
[486ddb5]135       
136        /* For now, SSL verification is globally enabled by setting the cafile
137           setting in bitlbee.conf. Commented out by default because probably
138           not everyone has this file in the same place and plenty of folks
139           may not have the cert of their private Jabber server in it. */
140        conn->verify = verify && global.conf->cafile;
[42127dc]141       
[c1ed6527]142        /* This function should be called via a (short) timeout instead of
143           directly from here, because these SSL calls are *supposed* to be
144           *completely* asynchronous and not ready yet when this function
145           (or *_connect, for examle) returns. Also, errors are reported via
146           the callback function, not via this function's return value.
147           
148           In short, doing things like this makes the rest of the code a lot
149           simpler. */
150       
151        b_timeout_add( 1, ssl_starttls_real, conn );
[b7d3cc34]152       
[42127dc]153        return conn;
[b7d3cc34]154}
155
[c1ed6527]156static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond )
157{
158        struct scd *conn = data;
159       
[e046390]160        return ssl_connected( conn, conn->fd, B_EV_IO_WRITE );
[c1ed6527]161}
[701acdd4]162
[486ddb5]163static int verify_certificate_callback( gnutls_session_t session )
164{
165        unsigned int status;
166        const gnutls_datum_t *cert_list;
167        unsigned int cert_list_size;
168        int gnutlsret;
169        int verifyret = 0;
170        gnutls_x509_crt_t cert;
[2fb1262]171        struct scd *conn;
[486ddb5]172       
[2fb1262]173        conn = gnutls_session_get_ptr( session );
[486ddb5]174
175        gnutlsret = gnutls_certificate_verify_peers2( session, &status );
176        if( gnutlsret < 0 )
177                return VERIFY_CERT_ERROR;
178
179        if( status & GNUTLS_CERT_INVALID )
180                verifyret |= VERIFY_CERT_INVALID;
181
182        if( status & GNUTLS_CERT_REVOKED )
183                verifyret |= VERIFY_CERT_REVOKED;
184
185        if( status & GNUTLS_CERT_SIGNER_NOT_FOUND )
186                verifyret |= VERIFY_CERT_SIGNER_NOT_FOUND;
187
188        if( status & GNUTLS_CERT_SIGNER_NOT_CA )
189                verifyret |= VERIFY_CERT_SIGNER_NOT_CA;
190
191        if( status & GNUTLS_CERT_INSECURE_ALGORITHM )
192                verifyret |= VERIFY_CERT_INSECURE_ALGORITHM;
193
[5513f3e]194#ifdef GNUTLS_CERT_NOT_ACTIVATED
195        /* Amusingly, the GnuTLS function used above didn't check for expiry
196           until GnuTLS 2.8 or so. (See CVE-2009-1417) */
[486ddb5]197        if( status & GNUTLS_CERT_NOT_ACTIVATED )
198                verifyret |= VERIFY_CERT_NOT_ACTIVATED;
199
200        if( status & GNUTLS_CERT_EXPIRED )
201                verifyret |= VERIFY_CERT_EXPIRED;
[5513f3e]202#endif
[486ddb5]203
[59c03bd]204        if( gnutls_certificate_type_get( session ) != GNUTLS_CRT_X509 || gnutls_x509_crt_init( &cert ) < 0 )
[486ddb5]205                return VERIFY_CERT_ERROR;
206
207        cert_list = gnutls_certificate_get_peers( session, &cert_list_size );
208        if( cert_list == NULL || gnutls_x509_crt_import( cert, &cert_list[0], GNUTLS_X509_FMT_DER ) < 0 )
209                return VERIFY_CERT_ERROR;
210
[2fb1262]211        if( !gnutls_x509_crt_check_hostname( cert, conn->hostname ) )
[486ddb5]212        {
213                verifyret |= VERIFY_CERT_INVALID;
214                verifyret |= VERIFY_CERT_WRONG_HOSTNAME;
215        }
216
217        gnutls_x509_crt_deinit( cert );
218
219        return verifyret;
220}
221
[78b8401]222char *ssl_verify_strerror( int code )
223{
224        GString *ret = g_string_new( "" );
225       
226        if( code & VERIFY_CERT_REVOKED )
227                g_string_append( ret, "certificate has been revoked, " );
228        if( code & VERIFY_CERT_SIGNER_NOT_FOUND )
229                g_string_append( ret, "certificate hasn't got a known issuer, " );
230        if( code & VERIFY_CERT_SIGNER_NOT_CA )
231                g_string_append( ret, "certificate's issuer is not a CA, " );
232        if( code & VERIFY_CERT_INSECURE_ALGORITHM )
233                g_string_append( ret, "certificate uses an insecure algorithm, " );
234        if( code & VERIFY_CERT_NOT_ACTIVATED )
235                g_string_append( ret, "certificate has not been activated, " );
236        if( code & VERIFY_CERT_EXPIRED )
237                g_string_append( ret, "certificate has expired, " );
238        if( code & VERIFY_CERT_WRONG_HOSTNAME )
239                g_string_append( ret, "certificate hostname mismatch, " );
240       
241        if( ret->len == 0 )
242        {
243                g_string_free( ret, TRUE );
244                return NULL;
245        }
246        else
247        {
248                g_string_truncate( ret, ret->len - 2 );
249                return g_string_free( ret, FALSE );
250        }
251}
252
[2b7d2d1]253static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond )
[b7d3cc34]254{
255        struct scd *conn = data;
256       
257        if( source == -1 )
[701acdd4]258        {
[486ddb5]259                conn->func( conn->data, 0, NULL, cond );
[701acdd4]260                g_free( conn );
[2b7d2d1]261                return FALSE;
[701acdd4]262        }
[b7d3cc34]263       
[83e47ec]264        ssl_init();
[42127dc]265       
266        gnutls_init( &conn->session, GNUTLS_CLIENT );
[2fb1262]267        gnutls_session_set_ptr( conn->session, (void *) conn );
[80acb6d]268#if GNUTLS_VERSION_NUMBER < 0x020c00
269        gnutls_transport_set_lowat( conn->session, 0 );
270#endif
[42127dc]271        gnutls_set_default_priority( conn->session );
[59cd92b]272        gnutls_credentials_set( conn->session, GNUTLS_CRD_CERTIFICATE, xcred );
[42127dc]273       
[701acdd4]274        sock_make_nonblocking( conn->fd );
[2fb1262]275        gnutls_transport_set_ptr( conn->session, (gnutls_transport_ptr_t) GNUTLS_STUPID_CAST conn->fd );
[b7d3cc34]276       
[2b7d2d1]277        return ssl_handshake( data, source, cond );
[701acdd4]278}
279
[2b7d2d1]280static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond )
[701acdd4]281{
282        struct scd *conn = data;
[486ddb5]283        int st, stver;
[b7d3cc34]284       
[701acdd4]285        if( ( st = gnutls_handshake( conn->session ) ) < 0 )
286        {
287                if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED )
288                {
[2b7d2d1]289                        conn->inpa = b_input_add( conn->fd, ssl_getdirection( conn ),
290                                                  ssl_handshake, data );
[701acdd4]291                }
292                else
293                {
[486ddb5]294                        conn->func( conn->data, 0, NULL, cond );
[701acdd4]295                       
296                        gnutls_deinit( conn->session );
297                        closesocket( conn->fd );
298                       
299                        g_free( conn );
300                }
301        }
302        else
303        {
[486ddb5]304                if( conn->verify && ( stver = verify_certificate_callback( conn->session ) ) != 0 )
305                {
306                        conn->func( conn->data, stver, NULL, cond );
307
308                        gnutls_deinit( conn->session );
309                        closesocket( conn->fd );
310
311                        g_free( conn );
312                }
313                else
314                {
315                        /* For now we can't handle non-blocking perfectly everywhere... */
316                        sock_make_blocking( conn->fd );
[701acdd4]317               
[486ddb5]318                        conn->established = TRUE;
319                        conn->func( conn->data, 0, conn, cond );
320                }
[701acdd4]321        }
[2b7d2d1]322       
323        return FALSE;
[b7d3cc34]324}
325
326int ssl_read( void *conn, char *buf, int len )
327{
[8a9afe4]328        int st;
329       
[b7d3cc34]330        if( !((struct scd*)conn)->established )
[701acdd4]331        {
332                ssl_errno = SSL_NOHANDSHAKE;
[80acb6d]333                return -1;
[701acdd4]334        }
[b7d3cc34]335       
[8a9afe4]336        st = gnutls_record_recv( ((struct scd*)conn)->session, buf, len );
337       
338        ssl_errno = SSL_OK;
339        if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED )
340                ssl_errno = SSL_AGAIN;
[701acdd4]341       
[ca974d7]342        if( SSLDEBUG && getenv( "BITLBEE_DEBUG" ) && st > 0 ) len = write( 2, buf, st );
[50b8978]343       
[8a9afe4]344        return st;
[b7d3cc34]345}
346
347int ssl_write( void *conn, const char *buf, int len )
348{
[8a9afe4]349        int st;
350       
[b7d3cc34]351        if( !((struct scd*)conn)->established )
[701acdd4]352        {
353                ssl_errno = SSL_NOHANDSHAKE;
[80acb6d]354                return -1;
[701acdd4]355        }
[b7d3cc34]356       
[8a9afe4]357        st = gnutls_record_send( ((struct scd*)conn)->session, buf, len );
358       
359        ssl_errno = SSL_OK;
360        if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED )
361                ssl_errno = SSL_AGAIN;
362       
[ca974d7]363        if( SSLDEBUG && getenv( "BITLBEE_DEBUG" ) && st > 0 ) len = write( 2, buf, st );
[50b8978]364       
[8a9afe4]365        return st;
[b7d3cc34]366}
367
[8a2221a7]368int ssl_pending( void *conn )
369{
[80acb6d]370        if( conn == NULL )
371                return 0;
372       
373        if( !((struct scd*)conn)->established )
374        {
375                ssl_errno = SSL_NOHANDSHAKE;
376                return 0;
377        }
[632f3d4]378
379#if GNUTLS_VERSION_NUMBER >= 0x03000d && GNUTLS_VERSION_NUMBER <= 0x030012
380        if( ssl_errno == SSL_AGAIN )
381                return 0;
382#endif
[80acb6d]383       
384        return gnutls_record_check_pending( ((struct scd*)conn)->session ) != 0;
[8a2221a7]385}
386
[b7d3cc34]387void ssl_disconnect( void *conn_ )
388{
389        struct scd *conn = conn_;
390       
[a03a9f3]391        if( conn->inpa != -1 )
[2b7d2d1]392                b_event_remove( conn->inpa );
[a03a9f3]393       
[b7d3cc34]394        if( conn->established )
395                gnutls_bye( conn->session, GNUTLS_SHUT_WR );
396       
397        closesocket( conn->fd );
398       
[3e79889]399        if( conn->session )
400                gnutls_deinit( conn->session );
[2fb1262]401        g_free( conn->hostname );
[b7d3cc34]402        g_free( conn );
403}
404
405int ssl_getfd( void *conn )
406{
407        return( ((struct scd*)conn)->fd );
408}
[8a9afe4]409
[ba9edaa]410b_input_condition ssl_getdirection( void *conn )
[8a9afe4]411{
412        return( gnutls_record_get_direction( ((struct scd*)conn)->session ) ?
[e046390]413                B_EV_IO_WRITE : B_EV_IO_READ );
[8a9afe4]414}
[83e47ec]415
416size_t ssl_des3_encrypt( const unsigned char *key, size_t key_len, const unsigned char *input,
417                         size_t input_len, const unsigned char *iv, unsigned char **res )
418{
419        gcry_cipher_hd_t gcr;
420        gcry_error_t st;
421       
422        ssl_init();
423       
424        *res = g_malloc( input_len  );
425        st = gcry_cipher_open( &gcr, GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC, 0 ) ||
426             gcry_cipher_setkey( gcr, key, key_len ) ||
427             gcry_cipher_setiv( gcr, iv, 8 ) ||
428             gcry_cipher_encrypt( gcr, *res, input_len, input, input_len );
429       
430        gcry_cipher_close( gcr );
431       
432        if( st == 0 )
433                return input_len;
434       
435        g_free( *res );
436        return 0;
437}
Note: See TracBrowser for help on using the repository browser.