source: lib/ssl_nss.c @ 59ab2af

Last change on this file since 59ab2af was ba5add7, checked in by Sven Moritz Hallberg <sm@…>, at 2008-02-17T01:39:39Z

explicitly initialize ssl in order to avoid gnutls and libotr fighting over the global state of libgcrypt

  • Property mode set to 100644
File size: 4.7 KB
Line 
1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2005 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* SSL module - NSS version                                             */
8
9/* Copyright 2005 Jelmer Vernooij                                       */
10
11/*
12  This program is free software; you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  GNU General Public License for more details.
21
22  You should have received a copy of the GNU General Public License with
23  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
24  if not, write to the Free Software Foundation, Inc., 59 Temple Place,
25  Suite 330, Boston, MA  02111-1307  USA
26*/
27
28#include "bitlbee.h"
29#include "proxy.h"
30#include "ssl_client.h"
31#include "sock.h"
32#include <nspr.h>
33#include <prio.h>
34#include <sslproto.h>
35#include <nss.h>
36#include <private/pprio.h>
37#include <ssl.h>
38#include <secerr.h>
39#include <sslerr.h>
40
41int ssl_errno = 0;
42
43static gboolean initialized = FALSE;
44
45struct scd
46{
47        ssl_input_function func;
48        gpointer data;
49        int fd;
50        PRFileDesc *prfd;
51        gboolean established;
52};
53
54static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond );
55
56
57static SECStatus nss_auth_cert (void *arg, PRFileDesc *socket, PRBool checksig, PRBool isserver)
58{
59        return SECSuccess;
60}
61
62static SECStatus nss_bad_cert (void *arg, PRFileDesc *socket) 
63{
64        PRErrorCode err;
65
66        if(!arg) return SECFailure;
67
68        *(PRErrorCode *)arg = err = PORT_GetError();
69
70        switch(err) {
71        case SEC_ERROR_INVALID_AVA:
72        case SEC_ERROR_INVALID_TIME:
73        case SEC_ERROR_BAD_SIGNATURE:
74        case SEC_ERROR_EXPIRED_CERTIFICATE:
75        case SEC_ERROR_UNKNOWN_ISSUER:
76        case SEC_ERROR_UNTRUSTED_CERT:
77        case SEC_ERROR_CERT_VALID:
78        case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE:
79        case SEC_ERROR_CRL_EXPIRED:
80        case SEC_ERROR_CRL_BAD_SIGNATURE:
81        case SEC_ERROR_EXTENSION_VALUE_INVALID:
82        case SEC_ERROR_CA_CERT_INVALID:
83        case SEC_ERROR_CERT_USAGES_INVALID:
84        case SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION:
85                return SECSuccess;
86
87        default:
88                return SECFailure;
89        }
90}
91
92
93void ssl_init( void )
94{
95        PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
96        NSS_NoDB_Init(NULL);
97        NSS_SetDomesticPolicy();
98        initialized = TRUE;
99}
100
101void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
102{
103        struct scd *conn = g_new0( struct scd, 1 );
104       
105        conn->fd = proxy_connect( host, port, ssl_connected, conn );
106        conn->func = func;
107        conn->data = data;
108       
109        if( conn->fd < 0 )
110        {
111                g_free( conn );
112                return( NULL );
113        }
114       
115        if( !initialized )
116        {
117                ssl_init();
118        }
119
120       
121        return( conn );
122}
123
124static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond )
125{
126        struct scd *conn = data;
127       
128        if( source == -1 )
129                goto ssl_connected_failure;
130       
131        /* Until we find out how to handle non-blocking I/O with NSS... */
132        sock_make_blocking( conn->fd );
133       
134        conn->prfd = SSL_ImportFD(NULL, PR_ImportTCPSocket(source));
135        SSL_OptionSet(conn->prfd, SSL_SECURITY, PR_TRUE);
136        SSL_OptionSet(conn->prfd, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE);
137        SSL_BadCertHook(conn->prfd, (SSLBadCertHandler)nss_bad_cert, NULL);
138        SSL_AuthCertificateHook(conn->prfd, (SSLAuthCertificate)nss_auth_cert, (void *)CERT_GetDefaultCertDB());
139        SSL_ResetHandshake(conn->prfd, PR_FALSE);
140
141        if (SSL_ForceHandshake(conn->prfd)) {
142                goto ssl_connected_failure;
143        }
144       
145       
146        conn->established = TRUE;
147        conn->func( conn->data, conn, cond );
148        return FALSE;
149       
150        ssl_connected_failure:
151       
152        conn->func( conn->data, NULL, cond );
153       
154        PR_Close( conn -> prfd );
155        if( source >= 0 ) closesocket( source );
156        g_free( conn );
157       
158        return FALSE;
159}
160
161int ssl_read( void *conn, char *buf, int len )
162{
163        if( !((struct scd*)conn)->established )
164                return( 0 );
165       
166        return( PR_Read( ((struct scd*)conn)->prfd, buf, len ) );
167}
168
169int ssl_write( void *conn, const char *buf, int len )
170{
171        if( !((struct scd*)conn)->established )
172                return( 0 );
173       
174        return( PR_Write ( ((struct scd*)conn)->prfd, buf, len ) );
175}
176
177void ssl_disconnect( void *conn_ )
178{
179        struct scd *conn = conn_;
180       
181        PR_Close( conn->prfd );
182        closesocket( conn->fd );
183       
184        g_free( conn );
185}
186
187int ssl_getfd( void *conn )
188{
189        return( ((struct scd*)conn)->fd );
190}
191
192b_input_condition ssl_getdirection( void *conn )
193{
194        /* Just in case someone calls us, let's return the most likely case: */
195        return GAIM_INPUT_READ;
196}
Note: See TracBrowser for help on using the repository browser.