source: protocols/ssl_openssl.c @ bad4a586

Last change on this file since bad4a586 was b7d3cc34, checked in by Wilmer van der Gaast <wilmer@…>, at 2005-11-06T18:23:18Z

Initial repository (0.99 release tree)

  • Property mode set to 100644
File size: 3.5 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 <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
37static gboolean initialized = FALSE;
38
39struct scd
40{
41        SslInputFunction func;
42        gpointer data;
43        int fd;
44        gboolean established;
45       
46        SSL *ssl;
47        SSL_CTX *ssl_ctx;
48};
49
50static void ssl_connected( gpointer data, gint source, GaimInputCondition cond );
51
52
53
54void *ssl_connect( char *host, int port, SslInputFunction func, gpointer data )
55{
56        struct scd *conn = g_new0( struct scd, 1 );
57        SSL_METHOD *meth;
58       
59        conn->fd = proxy_connect( host, port, ssl_connected, conn );
60        conn->func = func;
61        conn->data = data;
62       
63        if( conn->fd < 0 )
64        {
65                g_free( conn );
66                return( NULL );
67        }
68       
69        if( !initialized )
70        {
71                initialized = TRUE;
72                SSLeay_add_ssl_algorithms();
73        }
74       
75        meth = TLSv1_client_method();
76        conn->ssl_ctx = SSL_CTX_new( meth );
77        if( conn->ssl_ctx == NULL )
78        {
79                conn->fd = -1;
80                return( NULL );
81        }
82       
83        conn->ssl = SSL_new( conn->ssl_ctx );
84        if( conn->ssl == NULL )
85        {
86                conn->fd = -1;
87                return( NULL );
88        }
89       
90        return( conn );
91}
92
93static void ssl_connected( gpointer data, gint source, GaimInputCondition cond )
94{
95        struct scd *conn = data;
96       
97        if( source == -1 )
98                goto ssl_connected_failure;
99       
100        SSL_set_fd( conn->ssl, conn->fd );
101       
102        if( SSL_connect( conn->ssl ) < 0 )
103                goto ssl_connected_failure;
104       
105        conn->established = TRUE;
106        conn->func( conn->data, conn, cond );
107        return;
108       
109ssl_connected_failure:
110        conn->func( conn->data, NULL, cond );
111       
112        if( conn->ssl )
113        {
114                SSL_shutdown( conn->ssl );
115                SSL_free( conn->ssl );
116        }
117        if( conn->ssl_ctx )
118        {
119                SSL_CTX_free( conn->ssl_ctx );
120        }
121        if( source >= 0 ) closesocket( source );
122        g_free( conn );
123}
124
125int ssl_read( void *conn, char *buf, int len )
126{
127        if( !((struct scd*)conn)->established )
128                return( 0 );
129       
130        return( SSL_read( ((struct scd*)conn)->ssl, buf, len ) );
131}
132
133int ssl_write( void *conn, const char *buf, int len )
134{
135        if( !((struct scd*)conn)->established )
136                return( 0 );
137       
138        return( SSL_write( ((struct scd*)conn)->ssl, buf, len ) );
139}
140
141void ssl_disconnect( void *conn_ )
142{
143        struct scd *conn = conn_;
144       
145        if( conn->established )
146                SSL_shutdown( conn->ssl );
147       
148        closesocket( conn->fd );
149       
150        SSL_free( conn->ssl );
151        SSL_CTX_free( conn->ssl_ctx );
152        g_free( conn );
153}
154
155int ssl_getfd( void *conn )
156{
157        return( ((struct scd*)conn)->fd );
158}
Note: See TracBrowser for help on using the repository browser.