source: lib/ssl_openssl.c @ c8eeadd

Last change on this file since c8eeadd was e046390, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-10-10T23:25:54Z

Make purple use BitlBee's event handling API. Since the APIs never really
diverged too much this is fairly transparent. I did rename and redefine
GAIM_INPUT_* variables to really make it work without adding another stupid
layer in between.

One problem left, the new libpurple input API doesn't care about return
values. Fixing that in the next CL.

  • 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
[26fdfc5]7/* SSL module - OpenSSL version                                         */
[b7d3cc34]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 <openssl/crypto.h>
27#include <openssl/rand.h>
28#include <openssl/x509.h>
29#include <openssl/pem.h>
30#include <openssl/ssl.h>
31#include <openssl/err.h>
32
33#include "proxy.h"
34#include "ssl_client.h"
35#include "sock.h"
36
[701acdd4]37int ssl_errno = 0;
38
[b7d3cc34]39static gboolean initialized = FALSE;
40
41struct scd
42{
[26fdfc5]43        ssl_input_function func;
[b7d3cc34]44        gpointer data;
45        int fd;
46        gboolean established;
47       
[26fdfc5]48        int inpa;
49        int lasterr;            /* Necessary for SSL_get_error */
[b7d3cc34]50        SSL *ssl;
51        SSL_CTX *ssl_ctx;
52};
53
[309cb9e]54static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond );
[f920d9e]55static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond );
56static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond );
[b7d3cc34]57
58
[26fdfc5]59void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
[b7d3cc34]60{
61        struct scd *conn = g_new0( struct scd, 1 );
62       
63        conn->fd = proxy_connect( host, port, ssl_connected, conn );
64        if( conn->fd < 0 )
65        {
66                g_free( conn );
[f920d9e]67                return NULL;
[b7d3cc34]68        }
69       
[8a2221a7]70        conn->func = func;
71        conn->data = data;
72        conn->inpa = -1;
73       
[f920d9e]74        return conn;
75}
76
77void *ssl_starttls( int fd, ssl_input_function func, gpointer data )
78{
79        struct scd *conn = g_new0( struct scd, 1 );
80       
81        conn->fd = fd;
82        conn->func = func;
83        conn->data = data;
84        conn->inpa = -1;
85       
86        /* This function should be called via a (short) timeout instead of
87           directly from here, because these SSL calls are *supposed* to be
88           *completely* asynchronous and not ready yet when this function
89           (or *_connect, for examle) returns. Also, errors are reported via
90           the callback function, not via this function's return value.
91           
92           In short, doing things like this makes the rest of the code a lot
93           simpler. */
94       
95        b_timeout_add( 1, ssl_starttls_real, conn );
96       
97        return conn;
98}
99
100static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond )
101{
102        struct scd *conn = data;
103       
[e046390]104        return ssl_connected( conn, conn->fd, B_EV_IO_WRITE );
[f920d9e]105}
106
107static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond )
108{
109        struct scd *conn = data;
110        SSL_METHOD *meth;
111       
112        if( source == -1 )
113                goto ssl_connected_failure;
114       
[b7d3cc34]115        if( !initialized )
116        {
117                initialized = TRUE;
118                SSLeay_add_ssl_algorithms();
119        }
120       
121        meth = TLSv1_client_method();
122        conn->ssl_ctx = SSL_CTX_new( meth );
123        if( conn->ssl_ctx == NULL )
[f920d9e]124                goto ssl_connected_failure;
[b7d3cc34]125       
126        conn->ssl = SSL_new( conn->ssl_ctx );
127        if( conn->ssl == NULL )
[f920d9e]128                goto ssl_connected_failure;
[b7d3cc34]129       
[309cb9e]130        /* We can do at least the handshake with non-blocking I/O */
[26fdfc5]131        sock_make_nonblocking( conn->fd );
[b7d3cc34]132        SSL_set_fd( conn->ssl, conn->fd );
133       
[26fdfc5]134        return ssl_handshake( data, source, cond );
[f920d9e]135
136ssl_connected_failure:
137        conn->func( conn->data, NULL, cond );
138       
139        if( conn->ssl )
140        {
141                SSL_shutdown( conn->ssl );
142                SSL_free( conn->ssl );
143        }
144        if( conn->ssl_ctx )
145        {
146                SSL_CTX_free( conn->ssl_ctx );
147        }
148        if( source >= 0 ) closesocket( source );
149        g_free( conn );
150       
151        return FALSE;
152
[26fdfc5]153}       
154
[309cb9e]155static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond )
[26fdfc5]156{
157        struct scd *conn = data;
158        int st;
159       
160        if( ( st = SSL_connect( conn->ssl ) ) < 0 )
161        {
162                conn->lasterr = SSL_get_error( conn->ssl, st );
163                if( conn->lasterr != SSL_ERROR_WANT_READ && conn->lasterr != SSL_ERROR_WANT_WRITE )
[f920d9e]164                {
165                        conn->func( conn->data, NULL, cond );
166                       
167                        SSL_shutdown( conn->ssl );
168                        SSL_free( conn->ssl );
169                        SSL_CTX_free( conn->ssl_ctx );
170                       
171                        if( source >= 0 ) closesocket( source );
172                        g_free( conn );
173                       
174                        return FALSE;
175                }
[26fdfc5]176               
[309cb9e]177                conn->inpa = b_input_add( conn->fd, ssl_getdirection( conn ), ssl_handshake, data );
178                return FALSE;
[26fdfc5]179        }
[b7d3cc34]180       
181        conn->established = TRUE;
[26fdfc5]182        sock_make_blocking( conn->fd );         /* For now... */
[b7d3cc34]183        conn->func( conn->data, conn, cond );
[309cb9e]184        return FALSE;
[b7d3cc34]185}
186
187int ssl_read( void *conn, char *buf, int len )
188{
[26fdfc5]189        int st;
190       
[b7d3cc34]191        if( !((struct scd*)conn)->established )
[26fdfc5]192        {
193                ssl_errno = SSL_NOHANDSHAKE;
194                return -1;
195        }
196       
197        st = SSL_read( ((struct scd*)conn)->ssl, buf, len );
[b7d3cc34]198       
[26fdfc5]199        ssl_errno = SSL_OK;
200        if( st <= 0 )
201        {
202                ((struct scd*)conn)->lasterr = SSL_get_error( ((struct scd*)conn)->ssl, st );
203                if( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_READ || ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE )
204                        ssl_errno = SSL_AGAIN;
205        }
206       
207        return st;
[b7d3cc34]208}
209
210int ssl_write( void *conn, const char *buf, int len )
211{
[26fdfc5]212        int st;
213       
[b7d3cc34]214        if( !((struct scd*)conn)->established )
[26fdfc5]215        {
216                ssl_errno = SSL_NOHANDSHAKE;
217                return -1;
218        }
219       
220        st = SSL_write( ((struct scd*)conn)->ssl, buf, len );
[b7d3cc34]221       
[26fdfc5]222        ssl_errno = SSL_OK;
223        if( st <= 0 )
224        {
225                ((struct scd*)conn)->lasterr = SSL_get_error( ((struct scd*)conn)->ssl, st );
226                if( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_READ || ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE )
227                        ssl_errno = SSL_AGAIN;
228        }
229       
230        return st;
[b7d3cc34]231}
232
[8a2221a7]233/* Only OpenSSL *really* needs this (and well, maybe NSS). See for more info:
234   http://www.gnu.org/software/gnutls/manual/gnutls.html#index-gnutls_005frecord_005fcheck_005fpending-209
235   http://www.openssl.org/docs/ssl/SSL_pending.html
236   
237   Required because OpenSSL empties the TCP buffer completely but doesn't
238   necessarily give us all the unencrypted data.
239   
240   Returns 0 if there's nothing left or if we don't have to care (GnuTLS),
241   1 if there's more data. */
242int ssl_pending( void *conn )
243{
244        return ( ((struct scd*)conn) && ((struct scd*)conn)->established ) ?
245               SSL_pending( ((struct scd*)conn)->ssl ) > 0 : 0;
246}
247
[b7d3cc34]248void ssl_disconnect( void *conn_ )
249{
250        struct scd *conn = conn_;
251       
[26fdfc5]252        if( conn->inpa != -1 )
[309cb9e]253                b_event_remove( conn->inpa );
[26fdfc5]254       
[b7d3cc34]255        if( conn->established )
256                SSL_shutdown( conn->ssl );
257       
258        closesocket( conn->fd );
259       
260        SSL_free( conn->ssl );
261        SSL_CTX_free( conn->ssl_ctx );
262        g_free( conn );
263}
264
265int ssl_getfd( void *conn )
266{
267        return( ((struct scd*)conn)->fd );
268}
[26fdfc5]269
[ba9edaa]270b_input_condition ssl_getdirection( void *conn )
[26fdfc5]271{
[e046390]272        return( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ? B_EV_IO_WRITE : B_EV_IO_READ );
[26fdfc5]273}
Note: See TracBrowser for help on using the repository browser.