[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
| 4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
| 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* SSL module - NSS version */ |
---|
| 8 | |
---|
| 9 | /* Copyright 2004 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 | |
---|
| 41 | static gboolean initialized = FALSE; |
---|
| 42 | |
---|
| 43 | struct scd |
---|
| 44 | { |
---|
| 45 | SslInputFunction func; |
---|
| 46 | gpointer data; |
---|
| 47 | int fd; |
---|
| 48 | PRFileDesc *prfd; |
---|
| 49 | gboolean established; |
---|
| 50 | }; |
---|
| 51 | |
---|
| 52 | static void ssl_connected( gpointer data, gint source, GaimInputCondition cond ); |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | static SECStatus nss_auth_cert (void *arg, PRFileDesc *socket, PRBool checksig, PRBool isserver) |
---|
| 56 | { |
---|
| 57 | return SECSuccess; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | static SECStatus nss_bad_cert (void *arg, PRFileDesc *socket) |
---|
| 61 | { |
---|
| 62 | PRErrorCode err; |
---|
| 63 | |
---|
| 64 | if(!arg) return SECFailure; |
---|
| 65 | |
---|
| 66 | *(PRErrorCode *)arg = err = PORT_GetError(); |
---|
| 67 | |
---|
| 68 | switch(err) { |
---|
| 69 | case SEC_ERROR_INVALID_AVA: |
---|
| 70 | case SEC_ERROR_INVALID_TIME: |
---|
| 71 | case SEC_ERROR_BAD_SIGNATURE: |
---|
| 72 | case SEC_ERROR_EXPIRED_CERTIFICATE: |
---|
| 73 | case SEC_ERROR_UNKNOWN_ISSUER: |
---|
| 74 | case SEC_ERROR_UNTRUSTED_CERT: |
---|
| 75 | case SEC_ERROR_CERT_VALID: |
---|
| 76 | case SEC_ERROR_EXPIRED_ISSUER_CERTIFICATE: |
---|
| 77 | case SEC_ERROR_CRL_EXPIRED: |
---|
| 78 | case SEC_ERROR_CRL_BAD_SIGNATURE: |
---|
| 79 | case SEC_ERROR_EXTENSION_VALUE_INVALID: |
---|
| 80 | case SEC_ERROR_CA_CERT_INVALID: |
---|
| 81 | case SEC_ERROR_CERT_USAGES_INVALID: |
---|
| 82 | case SEC_ERROR_UNKNOWN_CRITICAL_EXTENSION: |
---|
| 83 | return SECSuccess; |
---|
| 84 | |
---|
| 85 | default: |
---|
| 86 | return SECFailure; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | void *ssl_connect( char *host, int port, SslInputFunction func, gpointer data ) |
---|
| 92 | { |
---|
| 93 | struct scd *conn = g_new0( struct scd, 1 ); |
---|
| 94 | |
---|
| 95 | conn->fd = proxy_connect( host, port, ssl_connected, conn ); |
---|
| 96 | conn->func = func; |
---|
| 97 | conn->data = data; |
---|
| 98 | |
---|
| 99 | if( conn->fd < 0 ) |
---|
| 100 | { |
---|
| 101 | g_free( conn ); |
---|
| 102 | return( NULL ); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | if( !initialized ) |
---|
| 106 | { |
---|
| 107 | PR_Init( PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1); |
---|
| 108 | NSS_NoDB_Init(NULL); |
---|
| 109 | NSS_SetDomesticPolicy(); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | return( conn ); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | static void ssl_connected( gpointer data, gint source, GaimInputCondition cond ) |
---|
| 117 | { |
---|
| 118 | struct scd *conn = data; |
---|
| 119 | |
---|
| 120 | if( source == -1 ) |
---|
| 121 | goto ssl_connected_failure; |
---|
| 122 | |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | |
---|
| 126 | conn->prfd = SSL_ImportFD(NULL, PR_ImportTCPSocket(source)); |
---|
| 127 | SSL_OptionSet(conn->prfd, SSL_SECURITY, PR_TRUE); |
---|
| 128 | SSL_OptionSet(conn->prfd, SSL_HANDSHAKE_AS_CLIENT, PR_TRUE); |
---|
| 129 | SSL_BadCertHook(conn->prfd, (SSLBadCertHandler)nss_bad_cert, NULL); |
---|
| 130 | SSL_AuthCertificateHook(conn->prfd, (SSLAuthCertificate)nss_auth_cert, (void *)CERT_GetDefaultCertDB()); |
---|
| 131 | SSL_ResetHandshake(conn->prfd, PR_FALSE); |
---|
| 132 | |
---|
| 133 | if (SSL_ForceHandshake(conn->prfd)) { |
---|
| 134 | goto ssl_connected_failure; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | |
---|
| 138 | conn->established = TRUE; |
---|
| 139 | conn->func( conn->data, conn, cond ); |
---|
| 140 | return; |
---|
| 141 | |
---|
| 142 | ssl_connected_failure: |
---|
| 143 | |
---|
| 144 | conn->func( conn->data, NULL, cond ); |
---|
| 145 | |
---|
| 146 | PR_Close( conn -> prfd ); |
---|
| 147 | if( source >= 0 ) closesocket( source ); |
---|
| 148 | g_free( conn ); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | int ssl_read( void *conn, char *buf, int len ) |
---|
| 152 | { |
---|
| 153 | if( !((struct scd*)conn)->established ) |
---|
| 154 | return( 0 ); |
---|
| 155 | |
---|
| 156 | return( PR_Read( ((struct scd*)conn)->prfd, buf, len ) ); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | int ssl_write( void *conn, const char *buf, int len ) |
---|
| 160 | { |
---|
| 161 | if( !((struct scd*)conn)->established ) |
---|
| 162 | return( 0 ); |
---|
| 163 | |
---|
| 164 | return( PR_Write ( ((struct scd*)conn)->prfd, buf, len ) ); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | void ssl_disconnect( void *conn_ ) |
---|
| 168 | { |
---|
| 169 | struct scd *conn = conn_; |
---|
| 170 | |
---|
| 171 | PR_Close( conn->prfd ); |
---|
| 172 | closesocket( conn->fd ); |
---|
| 173 | |
---|
| 174 | g_free( conn ); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | int ssl_getfd( void *conn ) |
---|
| 178 | { |
---|
| 179 | return( ((struct scd*)conn)->fd ); |
---|
| 180 | } |
---|