source: lib/ssl_gnutls.c @ 7732193

Last change on this file since 7732193 was d43d55a, checked in by Wilmer van der Gaast <wilmer@…>, at 2011-04-18T13:47:36Z

Fix GnuTLS >2.12 or so compatibility.
Bug #779 and https://savannah.gnu.org/support/index.php?107660

  • Property mode set to 100644
File size: 6.8 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2004 Wilmer van der Gaast and others                *
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>
[83e47ec]27#include <gcrypt.h>
[701acdd4]28#include <fcntl.h>
29#include <unistd.h>
[b7d3cc34]30#include "proxy.h"
31#include "ssl_client.h"
32#include "sock.h"
33#include "stdlib.h"
34
[701acdd4]35int ssl_errno = 0;
36
[b7d3cc34]37static gboolean initialized = FALSE;
38
[56f260a]39#include <limits.h>
40
41#if defined(ULONG_MAX) && ULONG_MAX > 4294967295UL
42#define GNUTLS_STUPID_CAST (long)
43#else
44#define GNUTLS_STUPID_CAST (int)
45#endif
46
[b7d3cc34]47struct scd
48{
[3d64e5b]49        ssl_input_function func;
[b7d3cc34]50        gpointer data;
51        int fd;
52        gboolean established;
[701acdd4]53        int inpa;
[b7d3cc34]54       
55        gnutls_session session;
56        gnutls_certificate_credentials xcred;
57};
58
[2b7d2d1]59static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond );
[c1ed6527]60static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond );
61static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond );
[b7d3cc34]62
63
[ba5add7]64void ssl_init( void )
65{
[83e47ec]66        if( initialized )
67                return;
68       
[ba5add7]69        gnutls_global_init();
70        initialized = TRUE;
71        atexit( gnutls_global_deinit );
72}
73
[3d64e5b]74void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
[b7d3cc34]75{
76        struct scd *conn = g_new0( struct scd, 1 );
77       
78        conn->fd = proxy_connect( host, port, ssl_connected, conn );
79        conn->func = func;
80        conn->data = data;
[701acdd4]81        conn->inpa = -1;
[b7d3cc34]82       
83        if( conn->fd < 0 )
84        {
85                g_free( conn );
[42127dc]86                return NULL;
[b7d3cc34]87        }
88       
[42127dc]89        return conn;
90}
91
92void *ssl_starttls( int fd, ssl_input_function func, gpointer data )
93{
94        struct scd *conn = g_new0( struct scd, 1 );
[b7d3cc34]95       
[42127dc]96        conn->fd = fd;
97        conn->func = func;
98        conn->data = data;
99        conn->inpa = -1;
100       
[c1ed6527]101        /* This function should be called via a (short) timeout instead of
102           directly from here, because these SSL calls are *supposed* to be
103           *completely* asynchronous and not ready yet when this function
104           (or *_connect, for examle) returns. Also, errors are reported via
105           the callback function, not via this function's return value.
106           
107           In short, doing things like this makes the rest of the code a lot
108           simpler. */
109       
110        b_timeout_add( 1, ssl_starttls_real, conn );
[b7d3cc34]111       
[42127dc]112        return conn;
[b7d3cc34]113}
114
[c1ed6527]115static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond )
116{
117        struct scd *conn = data;
118       
[e046390]119        return ssl_connected( conn, conn->fd, B_EV_IO_WRITE );
[c1ed6527]120}
[701acdd4]121
[2b7d2d1]122static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond )
[b7d3cc34]123{
124        struct scd *conn = data;
125       
126        if( source == -1 )
[701acdd4]127        {
128                conn->func( conn->data, NULL, cond );
129                g_free( conn );
[2b7d2d1]130                return FALSE;
[701acdd4]131        }
[b7d3cc34]132       
[83e47ec]133        ssl_init();
[42127dc]134       
135        gnutls_certificate_allocate_credentials( &conn->xcred );
136        gnutls_init( &conn->session, GNUTLS_CLIENT );
[d43d55a]137        gnutls_transport_set_lowat( conn->session, 1 ); 
[42127dc]138        gnutls_set_default_priority( conn->session );
139        gnutls_credentials_set( conn->session, GNUTLS_CRD_CERTIFICATE, conn->xcred );
140       
[701acdd4]141        sock_make_nonblocking( conn->fd );
[56f260a]142        gnutls_transport_set_ptr( conn->session, (gnutls_transport_ptr) GNUTLS_STUPID_CAST conn->fd );
[b7d3cc34]143       
[2b7d2d1]144        return ssl_handshake( data, source, cond );
[701acdd4]145}
146
[2b7d2d1]147static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond )
[701acdd4]148{
149        struct scd *conn = data;
150        int st;
[b7d3cc34]151       
[701acdd4]152        if( ( st = gnutls_handshake( conn->session ) ) < 0 )
153        {
154                if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED )
155                {
[2b7d2d1]156                        conn->inpa = b_input_add( conn->fd, ssl_getdirection( conn ),
157                                                  ssl_handshake, data );
[701acdd4]158                }
159                else
160                {
161                        conn->func( conn->data, NULL, cond );
162                       
163                        gnutls_deinit( conn->session );
164                        gnutls_certificate_free_credentials( conn->xcred );
165                        closesocket( conn->fd );
166                       
167                        g_free( conn );
168                }
169        }
170        else
171        {
172                /* For now we can't handle non-blocking perfectly everywhere... */
173                sock_make_blocking( conn->fd );
174               
175                conn->established = TRUE;
176                conn->func( conn->data, conn, cond );
177        }
[2b7d2d1]178       
179        return FALSE;
[b7d3cc34]180}
181
182int ssl_read( void *conn, char *buf, int len )
183{
[8a9afe4]184        int st;
185       
[b7d3cc34]186        if( !((struct scd*)conn)->established )
[701acdd4]187        {
188                ssl_errno = SSL_NOHANDSHAKE;
189                return( -1 );
190        }
[b7d3cc34]191       
[8a9afe4]192        st = gnutls_record_recv( ((struct scd*)conn)->session, buf, len );
193       
194        ssl_errno = SSL_OK;
195        if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED )
196                ssl_errno = SSL_AGAIN;
[701acdd4]197       
[afb9ea9]198        if( 0 && getenv( "BITLBEE_DEBUG" ) && st > 0 ) len = write( 1, buf, st );
[50b8978]199       
[8a9afe4]200        return st;
[b7d3cc34]201}
202
203int ssl_write( void *conn, const char *buf, int len )
204{
[8a9afe4]205        int st;
206       
[b7d3cc34]207        if( !((struct scd*)conn)->established )
[701acdd4]208        {
209                ssl_errno = SSL_NOHANDSHAKE;
210                return( -1 );
211        }
[b7d3cc34]212       
[8a9afe4]213        st = gnutls_record_send( ((struct scd*)conn)->session, buf, len );
214       
215        ssl_errno = SSL_OK;
216        if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED )
217                ssl_errno = SSL_AGAIN;
218       
[afb9ea9]219        if( 0 && getenv( "BITLBEE_DEBUG" ) && st > 0 ) len = write( 1, buf, st );
[50b8978]220       
[8a9afe4]221        return st;
[b7d3cc34]222}
223
[8a2221a7]224/* See ssl_openssl.c for an explanation. */
225int ssl_pending( void *conn )
226{
227        return 0;
228}
229
[b7d3cc34]230void ssl_disconnect( void *conn_ )
231{
232        struct scd *conn = conn_;
233       
[a03a9f3]234        if( conn->inpa != -1 )
[2b7d2d1]235                b_event_remove( conn->inpa );
[a03a9f3]236       
[b7d3cc34]237        if( conn->established )
238                gnutls_bye( conn->session, GNUTLS_SHUT_WR );
239       
240        closesocket( conn->fd );
241       
[3e79889]242        if( conn->session )
243                gnutls_deinit( conn->session );
244        if( conn->xcred )
245                gnutls_certificate_free_credentials( conn->xcred );
[b7d3cc34]246        g_free( conn );
247}
248
249int ssl_getfd( void *conn )
250{
251        return( ((struct scd*)conn)->fd );
252}
[8a9afe4]253
[ba9edaa]254b_input_condition ssl_getdirection( void *conn )
[8a9afe4]255{
256        return( gnutls_record_get_direction( ((struct scd*)conn)->session ) ?
[e046390]257                B_EV_IO_WRITE : B_EV_IO_READ );
[8a9afe4]258}
[83e47ec]259
260size_t ssl_des3_encrypt( const unsigned char *key, size_t key_len, const unsigned char *input,
261                         size_t input_len, const unsigned char *iv, unsigned char **res )
262{
263        gcry_cipher_hd_t gcr;
264        gcry_error_t st;
265       
266        ssl_init();
267       
268        *res = g_malloc( input_len  );
269        st = gcry_cipher_open( &gcr, GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC, 0 ) ||
270             gcry_cipher_setkey( gcr, key, key_len ) ||
271             gcry_cipher_setiv( gcr, iv, 8 ) ||
272             gcry_cipher_encrypt( gcr, *res, input_len, input, input_len );
273       
274        gcry_cipher_close( gcr );
275       
276        if( st == 0 )
277                return input_len;
278       
279        g_free( *res );
280        return 0;
281}
Note: See TracBrowser for help on using the repository browser.