source: lib/ssl_nss.c @ d150a9d

Last change on this file since d150a9d was be999a5, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-23T23:12:24Z

First step in this merge. Mostly a bzr merge and then a cleanup of conflicts
and parts I want to/have to redo (because of ui-fix).

  • Property mode set to 100644
File size: 4.8 KB
RevLine 
[b7d3cc34]1  /********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
[c92e6801]4  * Copyright 2002-2005 Wilmer van der Gaast and others                *
[b7d3cc34]5  \********************************************************************/
6
7/* SSL module - NSS version                                             */
8
[c92e6801]9/* Copyright 2005 Jelmer Vernooij                                       */
[b7d3cc34]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
[701acdd4]41int ssl_errno = 0;
42
[b7d3cc34]43static gboolean initialized = FALSE;
44
45struct scd
46{
[3d64e5b]47        ssl_input_function func;
[b7d3cc34]48        gpointer data;
49        int fd;
50        PRFileDesc *prfd;
51        gboolean established;
52};
53
[ba9edaa]54static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond );
[b7d3cc34]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
[ba5add7]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
[3d64e5b]101void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
[b7d3cc34]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        {
[ba5add7]117                ssl_init();
[b7d3cc34]118        }
119
120       
121        return( conn );
122}
123
[ba9edaa]124static gboolean ssl_connected( gpointer data, gint source, b_input_condition cond )
[b7d3cc34]125{
126        struct scd *conn = data;
127       
128        if( source == -1 )
129                goto ssl_connected_failure;
130       
[6e62132]131        /* Until we find out how to handle non-blocking I/O with NSS... */
132        sock_make_blocking( conn->fd );
133       
[b7d3cc34]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 );
[ba9edaa]148        return FALSE;
[b7d3cc34]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 );
[ba9edaa]157       
158        return FALSE;
[b7d3cc34]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
[8a2221a7]177/* See ssl_openssl.c for an explanation. */
178int ssl_pending( void *conn )
179{
180        return 0;
181}
182
[b7d3cc34]183void ssl_disconnect( void *conn_ )
184{
185        struct scd *conn = conn_;
186       
187        PR_Close( conn->prfd );
188        closesocket( conn->fd );
189       
190        g_free( conn );
191}
192
193int ssl_getfd( void *conn )
194{
195        return( ((struct scd*)conn)->fd );
196}
[6e62132]197
[ba9edaa]198b_input_condition ssl_getdirection( void *conn )
[6e62132]199{
200        /* Just in case someone calls us, let's return the most likely case: */
[e046390]201        return B_EV_IO_READ;
[6e62132]202}
Note: See TracBrowser for help on using the repository browser.