source: lib/ssl_openssl.c @ 07ff8a2

Last change on this file since 07ff8a2 was f920d9e, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-10-19T07:51:35Z

Added starttls code to ssl_openssl.c so GnuTLS isn't the only supported
SSL module in this branch anymore.

  • Property mode set to 100644
File size: 6.2 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        conn->func = func;
65        conn->data = data;
[f920d9e]66        conn->inpa = -1;
[b7d3cc34]67       
68        if( conn->fd < 0 )
69        {
70                g_free( conn );
[f920d9e]71                return NULL;
[b7d3cc34]72        }
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       
104        return ssl_connected( conn, conn->fd, GAIM_INPUT_WRITE );
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
233void ssl_disconnect( void *conn_ )
234{
235        struct scd *conn = conn_;
236       
[26fdfc5]237        if( conn->inpa != -1 )
[309cb9e]238                b_event_remove( conn->inpa );
[26fdfc5]239       
[b7d3cc34]240        if( conn->established )
241                SSL_shutdown( conn->ssl );
242       
243        closesocket( conn->fd );
244       
245        SSL_free( conn->ssl );
246        SSL_CTX_free( conn->ssl_ctx );
247        g_free( conn );
248}
249
250int ssl_getfd( void *conn )
251{
252        return( ((struct scd*)conn)->fd );
253}
[26fdfc5]254
[ba9edaa]255b_input_condition ssl_getdirection( void *conn )
[26fdfc5]256{
257        return( ((struct scd*)conn)->lasterr == SSL_ERROR_WANT_WRITE ? GAIM_INPUT_WRITE : GAIM_INPUT_READ );
258}
Note: See TracBrowser for help on using the repository browser.