source: protocols/ssl_gnutls.c @ 703f0f7

Last change on this file since 703f0f7 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
Line 
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>
27#include "proxy.h"
28#include "ssl_client.h"
29#include "sock.h"
30#include "stdlib.h"
31
32static gboolean initialized = FALSE;
33
34struct scd
35{
36        SslInputFunction func;
37        gpointer data;
38        int fd;
39        gboolean established;
40       
41        gnutls_session session;
42        gnutls_certificate_credentials xcred;
43};
44
45static void ssl_connected( gpointer data, gint source, GaimInputCondition cond );
46
47
48
49void *ssl_connect( char *host, int port, SslInputFunction func, gpointer data )
50{
51        struct scd *conn = g_new0( struct scd, 1 );
52       
53        conn->fd = proxy_connect( host, port, ssl_connected, conn );
54        conn->func = func;
55        conn->data = data;
56       
57        if( conn->fd < 0 )
58        {
59                g_free( conn );
60                return( NULL );
61        }
62       
63        if( !initialized )
64        {
65                gnutls_global_init();
66                initialized = TRUE;
67                atexit( gnutls_global_deinit );
68        }
69       
70        gnutls_certificate_allocate_credentials( &conn->xcred );
71        gnutls_init( &conn->session, GNUTLS_CLIENT );
72        gnutls_set_default_priority( conn->session );
73        gnutls_credentials_set( conn->session, GNUTLS_CRD_CERTIFICATE, conn->xcred );
74       
75        return( conn );
76}
77
78static void ssl_connected( gpointer data, gint source, GaimInputCondition cond )
79{
80        struct scd *conn = data;
81       
82        if( source == -1 )
83                goto ssl_connected_failure;
84       
85        gnutls_transport_set_ptr( conn->session, (gnutls_transport_ptr) conn->fd );
86       
87        if( gnutls_handshake( conn->session ) < 0 )
88                goto ssl_connected_failure;
89       
90        conn->established = TRUE;
91        conn->func( conn->data, conn, cond );
92        return;
93       
94ssl_connected_failure:
95        conn->func( conn->data, NULL, cond );
96       
97        gnutls_deinit( conn->session );
98        gnutls_certificate_free_credentials( conn->xcred );
99        if( source >= 0 ) closesocket( source );
100        g_free( conn );
101}
102
103int ssl_read( void *conn, char *buf, int len )
104{
105        if( !((struct scd*)conn)->established )
106                return( 0 );
107       
108        return( gnutls_record_recv( ((struct scd*)conn)->session, buf, len ) );
109}
110
111int ssl_write( void *conn, const char *buf, int len )
112{
113        if( !((struct scd*)conn)->established )
114                return( 0 );
115       
116        return( gnutls_record_send( ((struct scd*)conn)->session, buf, len ) );
117}
118
119void ssl_disconnect( void *conn_ )
120{
121        struct scd *conn = conn_;
122       
123        if( conn->established )
124                gnutls_bye( conn->session, GNUTLS_SHUT_WR );
125       
126        closesocket( conn->fd );
127       
128        gnutls_deinit( conn->session );
129        gnutls_certificate_free_credentials( conn->xcred );
130        g_free( conn );
131}
132
133int ssl_getfd( void *conn )
134{
135        return( ((struct scd*)conn)->fd );
136}
Note: See TracBrowser for help on using the repository browser.