[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> |
---|
[701acdd4] | 27 | #include <fcntl.h> |
---|
| 28 | #include <unistd.h> |
---|
[b7d3cc34] | 29 | #include "proxy.h" |
---|
| 30 | #include "ssl_client.h" |
---|
| 31 | #include "sock.h" |
---|
| 32 | #include "stdlib.h" |
---|
| 33 | |
---|
[701acdd4] | 34 | int ssl_errno = 0; |
---|
| 35 | |
---|
[b7d3cc34] | 36 | static gboolean initialized = FALSE; |
---|
| 37 | |
---|
| 38 | struct scd |
---|
| 39 | { |
---|
[3d64e5b] | 40 | ssl_input_function func; |
---|
[b7d3cc34] | 41 | gpointer data; |
---|
| 42 | int fd; |
---|
| 43 | gboolean established; |
---|
[701acdd4] | 44 | int inpa; |
---|
[b7d3cc34] | 45 | |
---|
| 46 | gnutls_session session; |
---|
| 47 | gnutls_certificate_credentials xcred; |
---|
| 48 | }; |
---|
| 49 | |
---|
[ba9edaa] | 50 | static void ssl_connected( gpointer data, gint source, b_input_condition cond ); |
---|
[b7d3cc34] | 51 | |
---|
| 52 | |
---|
[3d64e5b] | 53 | void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data ) |
---|
[b7d3cc34] | 54 | { |
---|
| 55 | struct scd *conn = g_new0( struct scd, 1 ); |
---|
| 56 | |
---|
| 57 | conn->fd = proxy_connect( host, port, ssl_connected, conn ); |
---|
| 58 | conn->func = func; |
---|
| 59 | conn->data = data; |
---|
[701acdd4] | 60 | conn->inpa = -1; |
---|
[b7d3cc34] | 61 | |
---|
| 62 | if( conn->fd < 0 ) |
---|
| 63 | { |
---|
| 64 | g_free( conn ); |
---|
| 65 | return( NULL ); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | if( !initialized ) |
---|
| 69 | { |
---|
| 70 | gnutls_global_init(); |
---|
| 71 | initialized = TRUE; |
---|
| 72 | atexit( gnutls_global_deinit ); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | gnutls_certificate_allocate_credentials( &conn->xcred ); |
---|
| 76 | gnutls_init( &conn->session, GNUTLS_CLIENT ); |
---|
| 77 | gnutls_set_default_priority( conn->session ); |
---|
| 78 | gnutls_credentials_set( conn->session, GNUTLS_CRD_CERTIFICATE, conn->xcred ); |
---|
| 79 | |
---|
| 80 | return( conn ); |
---|
| 81 | } |
---|
| 82 | |
---|
[ba9edaa] | 83 | static void ssl_handshake( gpointer data, gint source, b_input_condition cond ); |
---|
[701acdd4] | 84 | |
---|
[ba9edaa] | 85 | static void ssl_connected( gpointer data, gint source, b_input_condition cond ) |
---|
[b7d3cc34] | 86 | { |
---|
| 87 | struct scd *conn = data; |
---|
| 88 | |
---|
| 89 | if( source == -1 ) |
---|
[701acdd4] | 90 | { |
---|
| 91 | conn->func( conn->data, NULL, cond ); |
---|
| 92 | |
---|
| 93 | gnutls_deinit( conn->session ); |
---|
| 94 | gnutls_certificate_free_credentials( conn->xcred ); |
---|
| 95 | |
---|
| 96 | g_free( conn ); |
---|
| 97 | |
---|
| 98 | return; |
---|
| 99 | } |
---|
[b7d3cc34] | 100 | |
---|
[701acdd4] | 101 | sock_make_nonblocking( conn->fd ); |
---|
[b7d3cc34] | 102 | gnutls_transport_set_ptr( conn->session, (gnutls_transport_ptr) conn->fd ); |
---|
| 103 | |
---|
[701acdd4] | 104 | ssl_handshake( data, source, cond ); |
---|
| 105 | } |
---|
| 106 | |
---|
[ba9edaa] | 107 | static void ssl_handshake( gpointer data, gint source, b_input_condition cond ) |
---|
[701acdd4] | 108 | { |
---|
| 109 | struct scd *conn = data; |
---|
| 110 | int st; |
---|
[b7d3cc34] | 111 | |
---|
[701acdd4] | 112 | if( conn->inpa != -1 ) |
---|
[a03a9f3] | 113 | { |
---|
[701acdd4] | 114 | gaim_input_remove( conn->inpa ); |
---|
[a03a9f3] | 115 | conn->inpa = -1; |
---|
| 116 | } |
---|
[b7d3cc34] | 117 | |
---|
[701acdd4] | 118 | if( ( st = gnutls_handshake( conn->session ) ) < 0 ) |
---|
| 119 | { |
---|
| 120 | if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED ) |
---|
| 121 | { |
---|
[8a9afe4] | 122 | conn->inpa = gaim_input_add( conn->fd, ssl_getdirection( conn ), |
---|
[701acdd4] | 123 | ssl_handshake, data ); |
---|
| 124 | } |
---|
| 125 | else |
---|
| 126 | { |
---|
| 127 | conn->func( conn->data, NULL, cond ); |
---|
| 128 | |
---|
| 129 | gnutls_deinit( conn->session ); |
---|
| 130 | gnutls_certificate_free_credentials( conn->xcred ); |
---|
| 131 | closesocket( conn->fd ); |
---|
| 132 | |
---|
| 133 | g_free( conn ); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | else |
---|
| 137 | { |
---|
| 138 | /* For now we can't handle non-blocking perfectly everywhere... */ |
---|
| 139 | sock_make_blocking( conn->fd ); |
---|
| 140 | |
---|
| 141 | conn->established = TRUE; |
---|
| 142 | conn->func( conn->data, conn, cond ); |
---|
| 143 | } |
---|
[b7d3cc34] | 144 | } |
---|
| 145 | |
---|
| 146 | int ssl_read( void *conn, char *buf, int len ) |
---|
| 147 | { |
---|
[8a9afe4] | 148 | int st; |
---|
| 149 | |
---|
[b7d3cc34] | 150 | if( !((struct scd*)conn)->established ) |
---|
[701acdd4] | 151 | { |
---|
| 152 | ssl_errno = SSL_NOHANDSHAKE; |
---|
| 153 | return( -1 ); |
---|
| 154 | } |
---|
[b7d3cc34] | 155 | |
---|
[8a9afe4] | 156 | st = gnutls_record_recv( ((struct scd*)conn)->session, buf, len ); |
---|
| 157 | |
---|
| 158 | ssl_errno = SSL_OK; |
---|
| 159 | if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED ) |
---|
| 160 | ssl_errno = SSL_AGAIN; |
---|
[701acdd4] | 161 | |
---|
[8a9afe4] | 162 | return st; |
---|
[b7d3cc34] | 163 | } |
---|
| 164 | |
---|
| 165 | int ssl_write( void *conn, const char *buf, int len ) |
---|
| 166 | { |
---|
[8a9afe4] | 167 | int st; |
---|
| 168 | |
---|
[b7d3cc34] | 169 | if( !((struct scd*)conn)->established ) |
---|
[701acdd4] | 170 | { |
---|
| 171 | ssl_errno = SSL_NOHANDSHAKE; |
---|
| 172 | return( -1 ); |
---|
| 173 | } |
---|
[b7d3cc34] | 174 | |
---|
[8a9afe4] | 175 | st = gnutls_record_send( ((struct scd*)conn)->session, buf, len ); |
---|
| 176 | |
---|
| 177 | ssl_errno = SSL_OK; |
---|
| 178 | if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED ) |
---|
| 179 | ssl_errno = SSL_AGAIN; |
---|
| 180 | |
---|
| 181 | return st; |
---|
[b7d3cc34] | 182 | } |
---|
| 183 | |
---|
| 184 | void ssl_disconnect( void *conn_ ) |
---|
| 185 | { |
---|
| 186 | struct scd *conn = conn_; |
---|
| 187 | |
---|
[a03a9f3] | 188 | if( conn->inpa != -1 ) |
---|
| 189 | gaim_input_remove( conn->inpa ); |
---|
| 190 | |
---|
[b7d3cc34] | 191 | if( conn->established ) |
---|
| 192 | gnutls_bye( conn->session, GNUTLS_SHUT_WR ); |
---|
| 193 | |
---|
| 194 | closesocket( conn->fd ); |
---|
| 195 | |
---|
| 196 | gnutls_deinit( conn->session ); |
---|
| 197 | gnutls_certificate_free_credentials( conn->xcred ); |
---|
| 198 | g_free( conn ); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | int ssl_getfd( void *conn ) |
---|
| 202 | { |
---|
| 203 | return( ((struct scd*)conn)->fd ); |
---|
| 204 | } |
---|
[8a9afe4] | 205 | |
---|
[ba9edaa] | 206 | b_input_condition ssl_getdirection( void *conn ) |
---|
[8a9afe4] | 207 | { |
---|
| 208 | return( gnutls_record_get_direction( ((struct scd*)conn)->session ) ? |
---|
| 209 | GAIM_INPUT_WRITE : GAIM_INPUT_READ ); |
---|
| 210 | } |
---|