Changeset 486ddb5 for lib


Ignore:
Timestamp:
2011-12-19T14:50:58Z (13 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
78b8401
Parents:
5a48afd
Message:

Initial merge of tls_verify patch from AopicieR.

Location:
lib
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • lib/http_client.c

    r5a48afd r486ddb5  
    3333
    3434static gboolean http_connected( gpointer data, int source, b_input_condition cond );
    35 static gboolean http_ssl_connected( gpointer data, void *source, b_input_condition cond );
     35static gboolean http_ssl_connected( gpointer data, int returncode, void *source, b_input_condition cond );
    3636static gboolean http_incoming_data( gpointer data, int source, b_input_condition cond );
    3737static void http_free( struct http_request *req );
     
    170170}
    171171
    172 static gboolean http_ssl_connected( gpointer data, void *source, b_input_condition cond )
    173 {
     172static gboolean http_ssl_connected( gpointer data, int returncode, void *source, b_input_condition cond )
     173{
     174        //The returncode is not used at the moment.
    174175        struct http_request *req = data;
    175176       
  • lib/ssl_bogus.c

    r5a48afd r486ddb5  
    5656}
    5757
    58 void *ssl_starttls( int fd, ssl_input_function func, gpointer data )
     58void *ssl_starttls( int fd, char *hostname, gboolean verify, ssl_input_function func, gpointer data )
    5959{
    6060        return NULL;
  • lib/ssl_client.h

    r5a48afd r486ddb5  
    3737/* Some generic error codes. Especially SSL_AGAIN is important if you
    3838   want to do asynchronous I/O. */
     39#define NSS_VERIFY_ERROR -2
     40#define OPENSSL_VERIFY_ERROR -1
    3941#define SSL_OK            0
    4042#define SSL_NOHANDSHAKE   1
    4143#define SSL_AGAIN         2
     44#define VERIFY_CERT_ERROR 2
     45#define VERIFY_CERT_INVALID 4
     46#define VERIFY_CERT_REVOKED 8
     47#define VERIFY_CERT_SIGNER_NOT_FOUND 16
     48#define VERIFY_CERT_SIGNER_NOT_CA 32
     49#define VERIFY_CERT_INSECURE_ALGORITHM 64
     50#define VERIFY_CERT_NOT_ACTIVATED 128
     51#define VERIFY_CERT_EXPIRED 256
     52#define VERIFY_CERT_WRONG_HOSTNAME 512
    4253
    4354extern int ssl_errno;
    4455
    4556/* This is what your callback function should look like. */
    46 typedef gboolean (*ssl_input_function)(gpointer, void*, b_input_condition);
     57typedef gboolean (*ssl_input_function)(gpointer, int, void*, b_input_condition);
    4758
    4859
     
    5768/* Start an SSL session on an existing fd. Useful for STARTTLS functionality,
    5869   for example in Jabber. */
    59 G_MODULE_EXPORT void *ssl_starttls( int fd, ssl_input_function func, gpointer data );
     70G_MODULE_EXPORT void *ssl_starttls( int fd, char *hostname, gboolean verify, ssl_input_function func, gpointer data );
    6071
    6172/* Obviously you need special read/write functions to read data. */
  • lib/ssl_gnutls.c

    r5a48afd r486ddb5  
    2525
    2626#include <gnutls/gnutls.h>
     27#include <gnutls/x509.h>
    2728#include <gcrypt.h>
    2829#include <fcntl.h>
     
    3233#include "sock.h"
    3334#include "stdlib.h"
     35#include "bitlbee.h"
    3436
    3537int ssl_errno = 0;
     
    5456        gboolean established;
    5557        int inpa;
     58        char *hostname;
     59        gboolean verify;
    5660       
    5761        gnutls_session session;
     
    9296}
    9397
    94 void *ssl_starttls( int fd, ssl_input_function func, gpointer data )
     98void *ssl_starttls( int fd, char *hostname, gboolean verify, ssl_input_function func, gpointer data )
    9599{
    96100        struct scd *conn = g_new0( struct scd, 1 );
     
    100104        conn->data = data;
    101105        conn->inpa = -1;
     106        conn->hostname = hostname;
     107       
     108        /* For now, SSL verification is globally enabled by setting the cafile
     109           setting in bitlbee.conf. Commented out by default because probably
     110           not everyone has this file in the same place and plenty of folks
     111           may not have the cert of their private Jabber server in it. */
     112        conn->verify = verify && global.conf->cafile;
    102113       
    103114        /* This function should be called via a (short) timeout instead of
     
    122133}
    123134
     135static int verify_certificate_callback( gnutls_session_t session )
     136{
     137        unsigned int status;
     138        const gnutls_datum_t *cert_list;
     139        unsigned int cert_list_size;
     140        int gnutlsret;
     141        int verifyret = 0;
     142        gnutls_x509_crt_t cert;
     143        const char *hostname;
     144       
     145        hostname = gnutls_session_get_ptr(session );
     146
     147        gnutlsret = gnutls_certificate_verify_peers2( session, &status );
     148        if( gnutlsret < 0 )
     149                return VERIFY_CERT_ERROR;
     150
     151        if( status & GNUTLS_CERT_INVALID )
     152                verifyret |= VERIFY_CERT_INVALID;
     153
     154        if( status & GNUTLS_CERT_REVOKED )
     155                verifyret |= VERIFY_CERT_REVOKED;
     156
     157        if( status & GNUTLS_CERT_SIGNER_NOT_FOUND )
     158                verifyret |= VERIFY_CERT_SIGNER_NOT_FOUND;
     159
     160        if( status & GNUTLS_CERT_SIGNER_NOT_CA )
     161                verifyret |= VERIFY_CERT_SIGNER_NOT_CA;
     162
     163        if( status & GNUTLS_CERT_INSECURE_ALGORITHM )
     164                verifyret |= VERIFY_CERT_INSECURE_ALGORITHM;
     165
     166        if( status & GNUTLS_CERT_NOT_ACTIVATED )
     167                verifyret |= VERIFY_CERT_NOT_ACTIVATED;
     168
     169        if( status & GNUTLS_CERT_EXPIRED )
     170                verifyret |= VERIFY_CERT_EXPIRED;
     171
     172        /* The following check is already performed inside
     173         * gnutls_certificate_verify_peers2, so we don't need it.
     174
     175         * if( gnutls_certificate_type_get( session ) != GNUTLS_CRT_X509 )
     176         * return GNUTLS_E_CERTIFICATE_ERROR;
     177         */
     178
     179        if( gnutls_x509_crt_init( &cert ) < 0 )
     180                return VERIFY_CERT_ERROR;
     181
     182        cert_list = gnutls_certificate_get_peers( session, &cert_list_size );
     183        if( cert_list == NULL || gnutls_x509_crt_import( cert, &cert_list[0], GNUTLS_X509_FMT_DER ) < 0 )
     184                return VERIFY_CERT_ERROR;
     185
     186        if( !gnutls_x509_crt_check_hostname( cert, hostname ) )
     187        {
     188                verifyret |= VERIFY_CERT_INVALID;
     189                verifyret |= VERIFY_CERT_WRONG_HOSTNAME;
     190        }
     191
     192        gnutls_x509_crt_deinit( cert );
     193
     194        return verifyret;
     195}
     196
    124197static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond )
    125198{
     
    128201        if( source == -1 )
    129202        {
    130                 conn->func( conn->data, NULL, cond );
     203                conn->func( conn->data, 0, NULL, cond );
    131204                g_free( conn );
    132205                return FALSE;
     
    136209       
    137210        gnutls_certificate_allocate_credentials( &conn->xcred );
     211        if( conn->verify && global.conf->cafile )
     212        {
     213                gnutls_certificate_set_x509_trust_file( conn->xcred, global.conf->cafile, GNUTLS_X509_FMT_PEM );
     214                gnutls_certificate_set_verify_flags( conn->xcred, GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT );
     215        }
     216
    138217        gnutls_init( &conn->session, GNUTLS_CLIENT );
     218        if( conn->verify )
     219                gnutls_session_set_ptr( conn->session, (void *) conn->hostname );
    139220#if GNUTLS_VERSION_NUMBER < 0x020c00
    140221        gnutls_transport_set_lowat( conn->session, 0 );
     
    152233{
    153234        struct scd *conn = data;
    154         int st;
     235        int st, stver;
    155236       
    156237        if( ( st = gnutls_handshake( conn->session ) ) < 0 )
     
    163244                else
    164245                {
    165                         conn->func( conn->data, NULL, cond );
     246                        conn->func( conn->data, 0, NULL, cond );
    166247                       
    167248                        gnutls_deinit( conn->session );
     
    174255        else
    175256        {
    176                 /* For now we can't handle non-blocking perfectly everywhere... */
    177                 sock_make_blocking( conn->fd );
     257                if( conn->verify && ( stver = verify_certificate_callback( conn->session ) ) != 0 )
     258                {
     259                        conn->func( conn->data, stver, NULL, cond );
     260
     261                        gnutls_deinit( conn->session );
     262                        gnutls_certificate_free_credentials( conn->xcred );
     263                        closesocket( conn->fd );
     264
     265                        g_free( conn );
     266                }
     267                else
     268                {
     269                        /* For now we can't handle non-blocking perfectly everywhere... */
     270                        sock_make_blocking( conn->fd );
    178271               
    179                 conn->established = TRUE;
    180                 conn->func( conn->data, conn, cond );
     272                        conn->established = TRUE;
     273                        conn->func( conn->data, 0, conn, cond );
     274                }
    181275        }
    182276       
  • lib/ssl_nss.c

    r5a48afd r486ddb5  
    5252        PRFileDesc *prfd;
    5353        gboolean established;
     54        gboolean verify;
    5455};
    5556
     
    132133}
    133134
    134 void *ssl_starttls( int fd, ssl_input_function func, gpointer data )
     135void *ssl_starttls( int fd, char *hostname, gboolean verify, ssl_input_function func, gpointer data )
    135136{
    136137        struct scd *conn = g_new0( struct scd, 1 );
     
    139140        conn->func = func;
    140141        conn->data = data;
     142        conn->verify = verify;
    141143
    142144        /* This function should be called via a (short) timeout instead of
     
    157159{
    158160        struct scd *conn = data;
     161       
     162        /* Right now we don't have any verification functionality for nss so we
     163           fail in case verification has been requested by the user. */
     164
     165        if( conn->verify )
     166        {
     167                conn->func( conn->data, NSS_VERIFY_ERROR, NULL, cond );
     168                if( source >= 0 ) closesocket( source );
     169                g_free( conn );
     170
     171                return FALSE;
     172        }
    159173       
    160174        if( source == -1 )
     
    177191       
    178192        conn->established = TRUE;
    179         conn->func( conn->data, conn, cond );
     193        conn->func( conn->data, 0, conn, cond );
    180194        return FALSE;
    181195       
    182196        ssl_connected_failure:
    183197       
    184         conn->func( conn->data, NULL, cond );
     198        conn->func( conn->data, 0, NULL, cond );
    185199       
    186200        PR_Close( conn -> prfd );
  • lib/ssl_openssl.c

    r5a48afd r486ddb5  
    4545        int fd;
    4646        gboolean established;
     47        gboolean verify;
    4748       
    4849        int inpa;
     
    8283}
    8384
    84 void *ssl_starttls( int fd, ssl_input_function func, gpointer data )
     85void *ssl_starttls( int fd, char *hostname, gboolean verify, ssl_input_function func, gpointer data )
    8586{
    8687        struct scd *conn = g_new0( struct scd, 1 );
     
    9091        conn->data = data;
    9192        conn->inpa = -1;
     93        conn->verify = verify;
    9294       
    9395        /* This function should be called via a (short) timeout instead of
     
    117119        SSL_METHOD *meth;
    118120       
     121        /* Right now we don't have any verification functionality for openssl so we
     122           fail in case verification has been requested by the user. */
     123
     124        if( conn->verify )
     125        {
     126                conn->func( conn->data, OPENSSL_VERIFY_ERROR, NULL, cond );
     127                if( source >= 0 ) closesocket( source );
     128                g_free( conn );
     129
     130                return FALSE;
     131        }
     132
    119133        if( source == -1 )
    120134                goto ssl_connected_failure;
     
    141155
    142156ssl_connected_failure:
    143         conn->func( conn->data, NULL, cond );
     157        conn->func( conn->data, 0, NULL, cond );
    144158       
    145159        if( conn->ssl )
     
    169183                if( conn->lasterr != SSL_ERROR_WANT_READ && conn->lasterr != SSL_ERROR_WANT_WRITE )
    170184                {
    171                         conn->func( conn->data, NULL, cond );
     185                        conn->func( conn->data, 0, NULL, cond );
    172186                       
    173187                        SSL_shutdown( conn->ssl );
     
    187201        conn->established = TRUE;
    188202        sock_make_blocking( conn->fd );         /* For now... */
    189         conn->func( conn->data, conn, cond );
     203        conn->func( conn->data, 0, conn, cond );
    190204        return FALSE;
    191205}
Note: See TracChangeset for help on using the changeset viewer.