source: protocols/ssl_nss.c @ 881fd4e

Last change on this file since 881fd4e was 6e62132, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-04-17T08:25:16Z

Fixed ssl_nss.c (although it still needs some non-blocking I/O changes).

  • Property mode set to 100644
File size: 4.6 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
54static void ssl_connected( gpointer data, gint source, GaimInputCondition 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
[3d64e5b]93void *ssl_connect( char *host, int port, ssl_input_function func, gpointer data )
[b7d3cc34]94{
95        struct scd *conn = g_new0( struct scd, 1 );
96       
97        conn->fd = proxy_connect( host, port, ssl_connected, conn );
98        conn->func = func;
99        conn->data = data;
100       
101        if( conn->fd < 0 )
102        {
103                g_free( conn );
104                return( NULL );
105        }
106       
107        if( !initialized )
108        {
109                PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
110                NSS_NoDB_Init(NULL);
111                NSS_SetDomesticPolicy();
112        }
113
114       
115        return( conn );
116}
117
118static void ssl_connected( gpointer data, gint source, GaimInputCondition cond )
119{
120        struct scd *conn = data;
121       
122        if( source == -1 )
123                goto ssl_connected_failure;
124       
[6e62132]125        /* Until we find out how to handle non-blocking I/O with NSS... */
126        sock_make_blocking( conn->fd );
127       
[b7d3cc34]128        conn->prfd = SSL_ImportFD(NULL, PR_ImportTCPSocket(source));
129        SSL_OptionSet(conn->prfd, SSL_SECURITY, PR_TRUE);
130        SSL_OptionSet(conn->prfd, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE);
131        SSL_BadCertHook(conn->prfd, (SSLBadCertHandler)nss_bad_cert, NULL);
132        SSL_AuthCertificateHook(conn->prfd, (SSLAuthCertificate)nss_auth_cert, (void *)CERT_GetDefaultCertDB());
133        SSL_ResetHandshake(conn->prfd, PR_FALSE);
134
135        if (SSL_ForceHandshake(conn->prfd)) {
136                goto ssl_connected_failure;
137        }
138       
139       
140        conn->established = TRUE;
141        conn->func( conn->data, conn, cond );
142        return;
143       
144        ssl_connected_failure:
145       
146        conn->func( conn->data, NULL, cond );
147       
148        PR_Close( conn -> prfd );
149        if( source >= 0 ) closesocket( source );
150        g_free( conn );
151}
152
153int ssl_read( void *conn, char *buf, int len )
154{
155        if( !((struct scd*)conn)->established )
156                return( 0 );
157       
158        return( PR_Read( ((struct scd*)conn)->prfd, buf, len ) );
159}
160
161int ssl_write( void *conn, const char *buf, int len )
162{
163        if( !((struct scd*)conn)->established )
164                return( 0 );
165       
166        return( PR_Write ( ((struct scd*)conn)->prfd, buf, len ) );
167}
168
169void ssl_disconnect( void *conn_ )
170{
171        struct scd *conn = conn_;
172       
173        PR_Close( conn->prfd );
174        closesocket( conn->fd );
175       
176        g_free( conn );
177}
178
179int ssl_getfd( void *conn )
180{
181        return( ((struct scd*)conn)->fd );
182}
[6e62132]183
184GaimInputCondition ssl_getdirection( void *conn )
185{
186        /* Just in case someone calls us, let's return the most likely case: */
187        return GAIM_INPUT_READ;
188}
Note: See TracBrowser for help on using the repository browser.