[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[9b67285] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 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> |
---|
[486ddb5] | 27 | #include <gnutls/x509.h> |
---|
[83e47ec] | 28 | #include <gcrypt.h> |
---|
[701acdd4] | 29 | #include <fcntl.h> |
---|
| 30 | #include <unistd.h> |
---|
[b7d3cc34] | 31 | #include "proxy.h" |
---|
| 32 | #include "ssl_client.h" |
---|
| 33 | #include "sock.h" |
---|
| 34 | #include "stdlib.h" |
---|
[486ddb5] | 35 | #include "bitlbee.h" |
---|
[b7d3cc34] | 36 | |
---|
[701acdd4] | 37 | int ssl_errno = 0; |
---|
| 38 | |
---|
[b7d3cc34] | 39 | static gboolean initialized = FALSE; |
---|
[2fb1262] | 40 | gnutls_certificate_credentials_t xcred; |
---|
[b7d3cc34] | 41 | |
---|
[56f260a] | 42 | #include <limits.h> |
---|
| 43 | |
---|
| 44 | #if defined(ULONG_MAX) && ULONG_MAX > 4294967295UL |
---|
| 45 | #define GNUTLS_STUPID_CAST (long) |
---|
| 46 | #else |
---|
| 47 | #define GNUTLS_STUPID_CAST (int) |
---|
| 48 | #endif |
---|
| 49 | |
---|
[ca974d7] | 50 | #define SSLDEBUG 0 |
---|
| 51 | |
---|
[b7d3cc34] | 52 | struct scd |
---|
| 53 | { |
---|
[3d64e5b] | 54 | ssl_input_function func; |
---|
[b7d3cc34] | 55 | gpointer data; |
---|
| 56 | int fd; |
---|
| 57 | gboolean established; |
---|
[701acdd4] | 58 | int inpa; |
---|
[486ddb5] | 59 | char *hostname; |
---|
| 60 | gboolean verify; |
---|
[b7d3cc34] | 61 | |
---|
[2fb1262] | 62 | gnutls_session_t session; |
---|
[b7d3cc34] | 63 | }; |
---|
| 64 | |
---|
[9b67285] | 65 | static GHashTable *session_cache; |
---|
| 66 | |
---|
[2b7d2d1] | 67 | static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond ); |
---|
[c1ed6527] | 68 | static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond ); |
---|
| 69 | static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond ); |
---|
[b7d3cc34] | 70 | |
---|
[59cd92b] | 71 | static void ssl_deinit( void ); |
---|
[b7d3cc34] | 72 | |
---|
[632f3d4] | 73 | static void ssl_log( int level, const char *line ) |
---|
| 74 | { |
---|
| 75 | printf( "%d %s", level, line ); |
---|
| 76 | } |
---|
| 77 | |
---|
[ba5add7] | 78 | void ssl_init( void ) |
---|
| 79 | { |
---|
[83e47ec] | 80 | if( initialized ) |
---|
| 81 | return; |
---|
| 82 | |
---|
[ba5add7] | 83 | gnutls_global_init(); |
---|
[59cd92b] | 84 | gnutls_certificate_allocate_credentials( &xcred ); |
---|
| 85 | if( global.conf->cafile ) |
---|
| 86 | { |
---|
| 87 | gnutls_certificate_set_x509_trust_file( xcred, global.conf->cafile, GNUTLS_X509_FMT_PEM ); |
---|
[59c03bd] | 88 | |
---|
[8f976e6] | 89 | /* Not needed in GnuTLS 2.11+ (enabled by default there) so |
---|
| 90 | don't do it (resets possible other defaults). */ |
---|
| 91 | if( !gnutls_check_version( "2.11" ) ) |
---|
| 92 | gnutls_certificate_set_verify_flags( xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT ); |
---|
[59cd92b] | 93 | } |
---|
[ba5add7] | 94 | initialized = TRUE; |
---|
[59cd92b] | 95 | |
---|
[632f3d4] | 96 | gnutls_global_set_log_function( ssl_log ); |
---|
| 97 | /* |
---|
| 98 | gnutls_global_set_log_level( 3 ); |
---|
| 99 | */ |
---|
| 100 | |
---|
[9b67285] | 101 | session_cache = g_hash_table_new_full( g_str_hash, g_str_equal, g_free, g_free ); |
---|
| 102 | |
---|
[59cd92b] | 103 | atexit( ssl_deinit ); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | static void ssl_deinit( void ) |
---|
| 107 | { |
---|
| 108 | gnutls_global_deinit(); |
---|
| 109 | gnutls_certificate_free_credentials( xcred ); |
---|
[9b67285] | 110 | g_hash_table_destroy( session_cache ); |
---|
| 111 | session_cache = NULL; |
---|
[ba5add7] | 112 | } |
---|
| 113 | |
---|
[a72dc2b] | 114 | void *ssl_connect( char *host, int port, gboolean verify, ssl_input_function func, gpointer data ) |
---|
[b7d3cc34] | 115 | { |
---|
| 116 | struct scd *conn = g_new0( struct scd, 1 ); |
---|
| 117 | |
---|
| 118 | conn->func = func; |
---|
| 119 | conn->data = data; |
---|
[701acdd4] | 120 | conn->inpa = -1; |
---|
[a72dc2b] | 121 | conn->hostname = g_strdup( host ); |
---|
| 122 | conn->verify = verify && global.conf->cafile; |
---|
[8f976e6] | 123 | conn->fd = proxy_connect( host, port, ssl_connected, conn ); |
---|
[b7d3cc34] | 124 | |
---|
| 125 | if( conn->fd < 0 ) |
---|
| 126 | { |
---|
| 127 | g_free( conn ); |
---|
[42127dc] | 128 | return NULL; |
---|
[b7d3cc34] | 129 | } |
---|
| 130 | |
---|
[42127dc] | 131 | return conn; |
---|
| 132 | } |
---|
| 133 | |
---|
[486ddb5] | 134 | void *ssl_starttls( int fd, char *hostname, gboolean verify, ssl_input_function func, gpointer data ) |
---|
[42127dc] | 135 | { |
---|
| 136 | struct scd *conn = g_new0( struct scd, 1 ); |
---|
[b7d3cc34] | 137 | |
---|
[42127dc] | 138 | conn->fd = fd; |
---|
| 139 | conn->func = func; |
---|
| 140 | conn->data = data; |
---|
| 141 | conn->inpa = -1; |
---|
[2fb1262] | 142 | conn->hostname = g_strdup( hostname ); |
---|
[486ddb5] | 143 | |
---|
| 144 | /* For now, SSL verification is globally enabled by setting the cafile |
---|
| 145 | setting in bitlbee.conf. Commented out by default because probably |
---|
| 146 | not everyone has this file in the same place and plenty of folks |
---|
| 147 | may not have the cert of their private Jabber server in it. */ |
---|
| 148 | conn->verify = verify && global.conf->cafile; |
---|
[42127dc] | 149 | |
---|
[c1ed6527] | 150 | /* This function should be called via a (short) timeout instead of |
---|
| 151 | directly from here, because these SSL calls are *supposed* to be |
---|
| 152 | *completely* asynchronous and not ready yet when this function |
---|
| 153 | (or *_connect, for examle) returns. Also, errors are reported via |
---|
| 154 | the callback function, not via this function's return value. |
---|
| 155 | |
---|
| 156 | In short, doing things like this makes the rest of the code a lot |
---|
| 157 | simpler. */ |
---|
| 158 | |
---|
| 159 | b_timeout_add( 1, ssl_starttls_real, conn ); |
---|
[b7d3cc34] | 160 | |
---|
[42127dc] | 161 | return conn; |
---|
[b7d3cc34] | 162 | } |
---|
| 163 | |
---|
[c1ed6527] | 164 | static gboolean ssl_starttls_real( gpointer data, gint source, b_input_condition cond ) |
---|
| 165 | { |
---|
| 166 | struct scd *conn = data; |
---|
| 167 | |
---|
[e046390] | 168 | return ssl_connected( conn, conn->fd, B_EV_IO_WRITE ); |
---|
[c1ed6527] | 169 | } |
---|
[701acdd4] | 170 | |
---|
[486ddb5] | 171 | static int verify_certificate_callback( gnutls_session_t session ) |
---|
| 172 | { |
---|
| 173 | unsigned int status; |
---|
| 174 | const gnutls_datum_t *cert_list; |
---|
| 175 | unsigned int cert_list_size; |
---|
| 176 | int gnutlsret; |
---|
| 177 | int verifyret = 0; |
---|
| 178 | gnutls_x509_crt_t cert; |
---|
[2fb1262] | 179 | struct scd *conn; |
---|
[486ddb5] | 180 | |
---|
[2fb1262] | 181 | conn = gnutls_session_get_ptr( session ); |
---|
[486ddb5] | 182 | |
---|
| 183 | gnutlsret = gnutls_certificate_verify_peers2( session, &status ); |
---|
| 184 | if( gnutlsret < 0 ) |
---|
| 185 | return VERIFY_CERT_ERROR; |
---|
| 186 | |
---|
| 187 | if( status & GNUTLS_CERT_INVALID ) |
---|
| 188 | verifyret |= VERIFY_CERT_INVALID; |
---|
| 189 | |
---|
| 190 | if( status & GNUTLS_CERT_REVOKED ) |
---|
| 191 | verifyret |= VERIFY_CERT_REVOKED; |
---|
| 192 | |
---|
| 193 | if( status & GNUTLS_CERT_SIGNER_NOT_FOUND ) |
---|
| 194 | verifyret |= VERIFY_CERT_SIGNER_NOT_FOUND; |
---|
| 195 | |
---|
| 196 | if( status & GNUTLS_CERT_SIGNER_NOT_CA ) |
---|
| 197 | verifyret |= VERIFY_CERT_SIGNER_NOT_CA; |
---|
| 198 | |
---|
| 199 | if( status & GNUTLS_CERT_INSECURE_ALGORITHM ) |
---|
| 200 | verifyret |= VERIFY_CERT_INSECURE_ALGORITHM; |
---|
| 201 | |
---|
[5513f3e] | 202 | #ifdef GNUTLS_CERT_NOT_ACTIVATED |
---|
| 203 | /* Amusingly, the GnuTLS function used above didn't check for expiry |
---|
| 204 | until GnuTLS 2.8 or so. (See CVE-2009-1417) */ |
---|
[486ddb5] | 205 | if( status & GNUTLS_CERT_NOT_ACTIVATED ) |
---|
| 206 | verifyret |= VERIFY_CERT_NOT_ACTIVATED; |
---|
| 207 | |
---|
| 208 | if( status & GNUTLS_CERT_EXPIRED ) |
---|
| 209 | verifyret |= VERIFY_CERT_EXPIRED; |
---|
[5513f3e] | 210 | #endif |
---|
[486ddb5] | 211 | |
---|
[59c03bd] | 212 | if( gnutls_certificate_type_get( session ) != GNUTLS_CRT_X509 || gnutls_x509_crt_init( &cert ) < 0 ) |
---|
[486ddb5] | 213 | return VERIFY_CERT_ERROR; |
---|
| 214 | |
---|
| 215 | cert_list = gnutls_certificate_get_peers( session, &cert_list_size ); |
---|
| 216 | if( cert_list == NULL || gnutls_x509_crt_import( cert, &cert_list[0], GNUTLS_X509_FMT_DER ) < 0 ) |
---|
| 217 | return VERIFY_CERT_ERROR; |
---|
| 218 | |
---|
[2fb1262] | 219 | if( !gnutls_x509_crt_check_hostname( cert, conn->hostname ) ) |
---|
[486ddb5] | 220 | { |
---|
| 221 | verifyret |= VERIFY_CERT_INVALID; |
---|
| 222 | verifyret |= VERIFY_CERT_WRONG_HOSTNAME; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | gnutls_x509_crt_deinit( cert ); |
---|
| 226 | |
---|
| 227 | return verifyret; |
---|
| 228 | } |
---|
| 229 | |
---|
[9b67285] | 230 | struct ssl_session |
---|
| 231 | { |
---|
| 232 | size_t size; |
---|
| 233 | char data[]; |
---|
| 234 | }; |
---|
| 235 | |
---|
| 236 | static void ssl_cache_add( struct scd *conn ) |
---|
| 237 | { |
---|
| 238 | size_t data_size; |
---|
| 239 | struct ssl_session *data; |
---|
| 240 | char *hostname; |
---|
| 241 | |
---|
| 242 | if( !conn->hostname || |
---|
| 243 | gnutls_session_get_data( conn->session, NULL, &data_size ) != 0 ) |
---|
| 244 | return; |
---|
| 245 | |
---|
| 246 | data = g_malloc( sizeof( struct ssl_session ) + data_size ); |
---|
| 247 | if( gnutls_session_get_data( conn->session, data->data, &data->size ) != 0 ) |
---|
| 248 | { |
---|
| 249 | g_free( data ); |
---|
| 250 | return; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | hostname = g_strdup( conn->hostname ); |
---|
| 254 | g_hash_table_insert( session_cache, hostname, data ); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | static void ssl_cache_resume( struct scd *conn ) |
---|
| 258 | { |
---|
| 259 | struct ssl_session *data; |
---|
| 260 | |
---|
| 261 | if( conn->hostname && |
---|
| 262 | ( data = g_hash_table_lookup( session_cache, conn->hostname ) ) ) |
---|
| 263 | { |
---|
| 264 | gnutls_session_set_data( conn->session, data->data, data->size ); |
---|
| 265 | g_hash_table_remove( session_cache, conn->hostname ); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | |
---|
[78b8401] | 269 | char *ssl_verify_strerror( int code ) |
---|
| 270 | { |
---|
| 271 | GString *ret = g_string_new( "" ); |
---|
| 272 | |
---|
| 273 | if( code & VERIFY_CERT_REVOKED ) |
---|
| 274 | g_string_append( ret, "certificate has been revoked, " ); |
---|
| 275 | if( code & VERIFY_CERT_SIGNER_NOT_FOUND ) |
---|
| 276 | g_string_append( ret, "certificate hasn't got a known issuer, " ); |
---|
| 277 | if( code & VERIFY_CERT_SIGNER_NOT_CA ) |
---|
| 278 | g_string_append( ret, "certificate's issuer is not a CA, " ); |
---|
| 279 | if( code & VERIFY_CERT_INSECURE_ALGORITHM ) |
---|
| 280 | g_string_append( ret, "certificate uses an insecure algorithm, " ); |
---|
| 281 | if( code & VERIFY_CERT_NOT_ACTIVATED ) |
---|
| 282 | g_string_append( ret, "certificate has not been activated, " ); |
---|
| 283 | if( code & VERIFY_CERT_EXPIRED ) |
---|
| 284 | g_string_append( ret, "certificate has expired, " ); |
---|
| 285 | if( code & VERIFY_CERT_WRONG_HOSTNAME ) |
---|
| 286 | g_string_append( ret, "certificate hostname mismatch, " ); |
---|
| 287 | |
---|
| 288 | if( ret->len == 0 ) |
---|
| 289 | { |
---|
| 290 | g_string_free( ret, TRUE ); |
---|
| 291 | return NULL; |
---|
| 292 | } |
---|
| 293 | else |
---|
| 294 | { |
---|
| 295 | g_string_truncate( ret, ret->len - 2 ); |
---|
| 296 | return g_string_free( ret, FALSE ); |
---|
| 297 | } |
---|
| 298 | } |
---|
| 299 | |
---|
[2b7d2d1] | 300 | static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond ) |
---|
[b7d3cc34] | 301 | { |
---|
| 302 | struct scd *conn = data; |
---|
| 303 | |
---|
| 304 | if( source == -1 ) |
---|
[701acdd4] | 305 | { |
---|
[486ddb5] | 306 | conn->func( conn->data, 0, NULL, cond ); |
---|
[701acdd4] | 307 | g_free( conn ); |
---|
[2b7d2d1] | 308 | return FALSE; |
---|
[701acdd4] | 309 | } |
---|
[b7d3cc34] | 310 | |
---|
[83e47ec] | 311 | ssl_init(); |
---|
[42127dc] | 312 | |
---|
| 313 | gnutls_init( &conn->session, GNUTLS_CLIENT ); |
---|
[2fb1262] | 314 | gnutls_session_set_ptr( conn->session, (void *) conn ); |
---|
[80acb6d] | 315 | #if GNUTLS_VERSION_NUMBER < 0x020c00 |
---|
| 316 | gnutls_transport_set_lowat( conn->session, 0 ); |
---|
| 317 | #endif |
---|
[42127dc] | 318 | gnutls_set_default_priority( conn->session ); |
---|
[59cd92b] | 319 | gnutls_credentials_set( conn->session, GNUTLS_CRD_CERTIFICATE, xcred ); |
---|
[3f661849] | 320 | if( conn->hostname && !isdigit( conn->hostname[0] ) ) |
---|
| 321 | gnutls_server_name_set( conn->session, GNUTLS_NAME_DNS, |
---|
| 322 | conn->hostname, strlen( conn->hostname ) ); |
---|
[42127dc] | 323 | |
---|
[701acdd4] | 324 | sock_make_nonblocking( conn->fd ); |
---|
[2fb1262] | 325 | gnutls_transport_set_ptr( conn->session, (gnutls_transport_ptr_t) GNUTLS_STUPID_CAST conn->fd ); |
---|
[b7d3cc34] | 326 | |
---|
[9b67285] | 327 | ssl_cache_resume( conn ); |
---|
| 328 | |
---|
[2b7d2d1] | 329 | return ssl_handshake( data, source, cond ); |
---|
[701acdd4] | 330 | } |
---|
| 331 | |
---|
[2b7d2d1] | 332 | static gboolean ssl_handshake( gpointer data, gint source, b_input_condition cond ) |
---|
[701acdd4] | 333 | { |
---|
| 334 | struct scd *conn = data; |
---|
[486ddb5] | 335 | int st, stver; |
---|
[b7d3cc34] | 336 | |
---|
[701acdd4] | 337 | if( ( st = gnutls_handshake( conn->session ) ) < 0 ) |
---|
| 338 | { |
---|
| 339 | if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED ) |
---|
| 340 | { |
---|
[2b7d2d1] | 341 | conn->inpa = b_input_add( conn->fd, ssl_getdirection( conn ), |
---|
| 342 | ssl_handshake, data ); |
---|
[701acdd4] | 343 | } |
---|
| 344 | else |
---|
| 345 | { |
---|
[486ddb5] | 346 | conn->func( conn->data, 0, NULL, cond ); |
---|
[701acdd4] | 347 | |
---|
| 348 | gnutls_deinit( conn->session ); |
---|
| 349 | closesocket( conn->fd ); |
---|
| 350 | |
---|
| 351 | g_free( conn ); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | else |
---|
| 355 | { |
---|
[486ddb5] | 356 | if( conn->verify && ( stver = verify_certificate_callback( conn->session ) ) != 0 ) |
---|
| 357 | { |
---|
| 358 | conn->func( conn->data, stver, NULL, cond ); |
---|
| 359 | |
---|
| 360 | gnutls_deinit( conn->session ); |
---|
| 361 | closesocket( conn->fd ); |
---|
| 362 | |
---|
| 363 | g_free( conn ); |
---|
| 364 | } |
---|
| 365 | else |
---|
| 366 | { |
---|
| 367 | /* For now we can't handle non-blocking perfectly everywhere... */ |
---|
| 368 | sock_make_blocking( conn->fd ); |
---|
[9b67285] | 369 | |
---|
| 370 | ssl_cache_add( conn ); |
---|
[486ddb5] | 371 | conn->established = TRUE; |
---|
| 372 | conn->func( conn->data, 0, conn, cond ); |
---|
| 373 | } |
---|
[701acdd4] | 374 | } |
---|
[2b7d2d1] | 375 | |
---|
| 376 | return FALSE; |
---|
[b7d3cc34] | 377 | } |
---|
| 378 | |
---|
| 379 | int ssl_read( void *conn, char *buf, int len ) |
---|
| 380 | { |
---|
[8a9afe4] | 381 | int st; |
---|
| 382 | |
---|
[b7d3cc34] | 383 | if( !((struct scd*)conn)->established ) |
---|
[701acdd4] | 384 | { |
---|
| 385 | ssl_errno = SSL_NOHANDSHAKE; |
---|
[80acb6d] | 386 | return -1; |
---|
[701acdd4] | 387 | } |
---|
[b7d3cc34] | 388 | |
---|
[8a9afe4] | 389 | st = gnutls_record_recv( ((struct scd*)conn)->session, buf, len ); |
---|
| 390 | |
---|
| 391 | ssl_errno = SSL_OK; |
---|
| 392 | if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED ) |
---|
| 393 | ssl_errno = SSL_AGAIN; |
---|
[701acdd4] | 394 | |
---|
[ca974d7] | 395 | if( SSLDEBUG && getenv( "BITLBEE_DEBUG" ) && st > 0 ) len = write( 2, buf, st ); |
---|
[50b8978] | 396 | |
---|
[8a9afe4] | 397 | return st; |
---|
[b7d3cc34] | 398 | } |
---|
| 399 | |
---|
| 400 | int ssl_write( void *conn, const char *buf, int len ) |
---|
| 401 | { |
---|
[8a9afe4] | 402 | int st; |
---|
| 403 | |
---|
[b7d3cc34] | 404 | if( !((struct scd*)conn)->established ) |
---|
[701acdd4] | 405 | { |
---|
| 406 | ssl_errno = SSL_NOHANDSHAKE; |
---|
[80acb6d] | 407 | return -1; |
---|
[701acdd4] | 408 | } |
---|
[b7d3cc34] | 409 | |
---|
[8a9afe4] | 410 | st = gnutls_record_send( ((struct scd*)conn)->session, buf, len ); |
---|
| 411 | |
---|
| 412 | ssl_errno = SSL_OK; |
---|
| 413 | if( st == GNUTLS_E_AGAIN || st == GNUTLS_E_INTERRUPTED ) |
---|
| 414 | ssl_errno = SSL_AGAIN; |
---|
| 415 | |
---|
[ca974d7] | 416 | if( SSLDEBUG && getenv( "BITLBEE_DEBUG" ) && st > 0 ) len = write( 2, buf, st ); |
---|
[50b8978] | 417 | |
---|
[8a9afe4] | 418 | return st; |
---|
[b7d3cc34] | 419 | } |
---|
| 420 | |
---|
[8a2221a7] | 421 | int ssl_pending( void *conn ) |
---|
| 422 | { |
---|
[80acb6d] | 423 | if( conn == NULL ) |
---|
| 424 | return 0; |
---|
| 425 | |
---|
| 426 | if( !((struct scd*)conn)->established ) |
---|
| 427 | { |
---|
| 428 | ssl_errno = SSL_NOHANDSHAKE; |
---|
| 429 | return 0; |
---|
| 430 | } |
---|
[632f3d4] | 431 | |
---|
| 432 | #if GNUTLS_VERSION_NUMBER >= 0x03000d && GNUTLS_VERSION_NUMBER <= 0x030012 |
---|
| 433 | if( ssl_errno == SSL_AGAIN ) |
---|
| 434 | return 0; |
---|
| 435 | #endif |
---|
[80acb6d] | 436 | |
---|
| 437 | return gnutls_record_check_pending( ((struct scd*)conn)->session ) != 0; |
---|
[8a2221a7] | 438 | } |
---|
| 439 | |
---|
[b7d3cc34] | 440 | void ssl_disconnect( void *conn_ ) |
---|
| 441 | { |
---|
| 442 | struct scd *conn = conn_; |
---|
| 443 | |
---|
[a03a9f3] | 444 | if( conn->inpa != -1 ) |
---|
[2b7d2d1] | 445 | b_event_remove( conn->inpa ); |
---|
[a03a9f3] | 446 | |
---|
[b7d3cc34] | 447 | if( conn->established ) |
---|
| 448 | gnutls_bye( conn->session, GNUTLS_SHUT_WR ); |
---|
| 449 | |
---|
| 450 | closesocket( conn->fd ); |
---|
| 451 | |
---|
[3e79889] | 452 | if( conn->session ) |
---|
| 453 | gnutls_deinit( conn->session ); |
---|
[2fb1262] | 454 | g_free( conn->hostname ); |
---|
[b7d3cc34] | 455 | g_free( conn ); |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | int ssl_getfd( void *conn ) |
---|
| 459 | { |
---|
| 460 | return( ((struct scd*)conn)->fd ); |
---|
| 461 | } |
---|
[8a9afe4] | 462 | |
---|
[ba9edaa] | 463 | b_input_condition ssl_getdirection( void *conn ) |
---|
[8a9afe4] | 464 | { |
---|
| 465 | return( gnutls_record_get_direction( ((struct scd*)conn)->session ) ? |
---|
[e046390] | 466 | B_EV_IO_WRITE : B_EV_IO_READ ); |
---|
[8a9afe4] | 467 | } |
---|
[83e47ec] | 468 | |
---|
| 469 | size_t ssl_des3_encrypt( const unsigned char *key, size_t key_len, const unsigned char *input, |
---|
| 470 | size_t input_len, const unsigned char *iv, unsigned char **res ) |
---|
| 471 | { |
---|
| 472 | gcry_cipher_hd_t gcr; |
---|
| 473 | gcry_error_t st; |
---|
| 474 | |
---|
| 475 | ssl_init(); |
---|
| 476 | |
---|
| 477 | *res = g_malloc( input_len ); |
---|
| 478 | st = gcry_cipher_open( &gcr, GCRY_CIPHER_3DES, GCRY_CIPHER_MODE_CBC, 0 ) || |
---|
| 479 | gcry_cipher_setkey( gcr, key, key_len ) || |
---|
| 480 | gcry_cipher_setiv( gcr, iv, 8 ) || |
---|
| 481 | gcry_cipher_encrypt( gcr, *res, input_len, input, input_len ); |
---|
| 482 | |
---|
| 483 | gcry_cipher_close( gcr ); |
---|
| 484 | |
---|
| 485 | if( st == 0 ) |
---|
| 486 | return input_len; |
---|
| 487 | |
---|
| 488 | g_free( *res ); |
---|
| 489 | return 0; |
---|
| 490 | } |
---|