[5ebff60] | 1 | /********************************************************************\ |
---|
[b7d3cc34] | 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[def3650] | 4 | * Copyright 2002-2012 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
[26fdfc5] | 7 | /* SSL module - OpenSSL version */ |
---|
[b7d3cc34] | 8 | |
---|
| 9 | /* |
---|
| 10 | This program is free software; you can redistribute it and/or modify |
---|
| 11 | it under the terms of the GNU General Public License as published by |
---|
| 12 | the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | (at your option) any later version. |
---|
| 14 | |
---|
| 15 | This program is distributed in the hope that it will be useful, |
---|
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | GNU General Public License for more details. |
---|
| 19 | |
---|
| 20 | You should have received a copy of the GNU General Public License with |
---|
| 21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
[6f10697] | 22 | if not, write to the Free Software Foundation, Inc., 51 Franklin St., |
---|
| 23 | Fifth Floor, Boston, MA 02110-1301 USA |
---|
[b7d3cc34] | 24 | */ |
---|
| 25 | |
---|
| 26 | #include <openssl/crypto.h> |
---|
| 27 | #include <openssl/rand.h> |
---|
| 28 | #include <openssl/x509.h> |
---|
| 29 | #include <openssl/pem.h> |
---|
| 30 | #include <openssl/ssl.h> |
---|
| 31 | #include <openssl/err.h> |
---|
| 32 | |
---|
[59c03bd] | 33 | #include "bitlbee.h" |
---|
[b7d3cc34] | 34 | #include "proxy.h" |
---|
| 35 | #include "ssl_client.h" |
---|
| 36 | #include "sock.h" |
---|
| 37 | |
---|
[701acdd4] | 38 | int ssl_errno = 0; |
---|
| 39 | |
---|
[b7d3cc34] | 40 | static gboolean initialized = FALSE; |
---|
| 41 | |
---|
[5ebff60] | 42 | struct scd { |
---|
[26fdfc5] | 43 | ssl_input_function func; |
---|
[b7d3cc34] | 44 | gpointer data; |
---|
| 45 | int fd; |
---|
| 46 | gboolean established; |
---|
[486ddb5] | 47 | gboolean verify; |
---|
[3f661849] | 48 | char *hostname; |
---|
[5ebff60] | 49 | |
---|
[26fdfc5] | 50 | int inpa; |
---|
[5ebff60] | 51 | int lasterr; /* Necessary for SSL_get_error */ |
---|
[b7d3cc34] | 52 | SSL *ssl; |
---|
| 53 | }; |
---|
| 54 | |
---|
[def3650] | 55 | static SSL_CTX *ssl_ctx; |
---|
| 56 | |
---|
[5ebff60] | 57 | static void ssl_conn_free(struct scd *conn); |
---|
| 58 | static gboolean ssl_connected(gpointer data, gint source, b_input_condition cond); |
---|
| 59 | static gboolean ssl_starttls_real(gpointer data, gint source, b_input_condition cond); |
---|
| 60 | static gboolean ssl_handshake(gpointer data, gint source, b_input_condition cond); |
---|
[b7d3cc34] | 61 | |
---|
| 62 | |
---|
[5ebff60] | 63 | void ssl_init(void) |
---|
[ba5add7] | 64 | { |
---|
[def3650] | 65 | const SSL_METHOD *meth; |
---|
[5ebff60] | 66 | |
---|
[8e9e2b7] | 67 | SSL_library_init(); |
---|
[5ebff60] | 68 | |
---|
[82e55d2] | 69 | meth = SSLv23_client_method(); |
---|
[5ebff60] | 70 | ssl_ctx = SSL_CTX_new(meth); |
---|
[82e55d2] | 71 | SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3); |
---|
[5ebff60] | 72 | |
---|
[def3650] | 73 | initialized = TRUE; |
---|
[ba5add7] | 74 | } |
---|
| 75 | |
---|
[5ebff60] | 76 | void *ssl_connect(char *host, int port, gboolean verify, ssl_input_function func, gpointer data) |
---|
[b7d3cc34] | 77 | { |
---|
[5ebff60] | 78 | struct scd *conn = g_new0(struct scd, 1); |
---|
| 79 | |
---|
| 80 | conn->fd = proxy_connect(host, port, ssl_connected, conn); |
---|
| 81 | if (conn->fd < 0) { |
---|
| 82 | ssl_conn_free(conn); |
---|
[f920d9e] | 83 | return NULL; |
---|
[b7d3cc34] | 84 | } |
---|
[5ebff60] | 85 | |
---|
[8a2221a7] | 86 | conn->func = func; |
---|
| 87 | conn->data = data; |
---|
| 88 | conn->inpa = -1; |
---|
[5ebff60] | 89 | conn->hostname = g_strdup(host); |
---|
| 90 | |
---|
[f920d9e] | 91 | return conn; |
---|
| 92 | } |
---|
| 93 | |
---|
[5ebff60] | 94 | void *ssl_starttls(int fd, char *hostname, gboolean verify, ssl_input_function func, gpointer data) |
---|
[f920d9e] | 95 | { |
---|
[5ebff60] | 96 | struct scd *conn = g_new0(struct scd, 1); |
---|
| 97 | |
---|
[f920d9e] | 98 | conn->fd = fd; |
---|
| 99 | conn->func = func; |
---|
| 100 | conn->data = data; |
---|
| 101 | conn->inpa = -1; |
---|
[200e151] | 102 | conn->verify = verify && global.conf->cafile; |
---|
[5ebff60] | 103 | conn->hostname = g_strdup(hostname); |
---|
| 104 | |
---|
[f920d9e] | 105 | /* This function should be called via a (short) timeout instead of |
---|
| 106 | directly from here, because these SSL calls are *supposed* to be |
---|
| 107 | *completely* asynchronous and not ready yet when this function |
---|
| 108 | (or *_connect, for examle) returns. Also, errors are reported via |
---|
| 109 | the callback function, not via this function's return value. |
---|
[5ebff60] | 110 | |
---|
[f920d9e] | 111 | In short, doing things like this makes the rest of the code a lot |
---|
| 112 | simpler. */ |
---|
[5ebff60] | 113 | |
---|
| 114 | b_timeout_add(1, ssl_starttls_real, conn); |
---|
| 115 | |
---|
[f920d9e] | 116 | return conn; |
---|
| 117 | } |
---|
| 118 | |
---|
[5ebff60] | 119 | static gboolean ssl_starttls_real(gpointer data, gint source, b_input_condition cond) |
---|
[f920d9e] | 120 | { |
---|
| 121 | struct scd *conn = data; |
---|
[5ebff60] | 122 | |
---|
| 123 | return ssl_connected(conn, conn->fd, B_EV_IO_WRITE); |
---|
[f920d9e] | 124 | } |
---|
| 125 | |
---|
[5ebff60] | 126 | static gboolean ssl_connected(gpointer data, gint source, b_input_condition cond) |
---|
[f920d9e] | 127 | { |
---|
| 128 | struct scd *conn = data; |
---|
[5ebff60] | 129 | |
---|
| 130 | if (conn->verify) { |
---|
[3f661849] | 131 | /* Right now we don't have any verification functionality for OpenSSL. */ |
---|
[5ebff60] | 132 | conn->func(conn->data, 1, NULL, cond); |
---|
| 133 | if (source >= 0) { |
---|
[0db6618] | 134 | proxy_disconnect(source); |
---|
[5ebff60] | 135 | } |
---|
| 136 | ssl_conn_free(conn); |
---|
[486ddb5] | 137 | |
---|
| 138 | return FALSE; |
---|
| 139 | } |
---|
| 140 | |
---|
[5ebff60] | 141 | if (source == -1) { |
---|
[f920d9e] | 142 | goto ssl_connected_failure; |
---|
[5ebff60] | 143 | } |
---|
| 144 | |
---|
| 145 | if (!initialized) { |
---|
[ba5add7] | 146 | ssl_init(); |
---|
[b7d3cc34] | 147 | } |
---|
[5ebff60] | 148 | |
---|
| 149 | |
---|
| 150 | if (ssl_ctx == NULL) { |
---|
[f920d9e] | 151 | goto ssl_connected_failure; |
---|
[5ebff60] | 152 | } |
---|
| 153 | |
---|
| 154 | conn->ssl = SSL_new(ssl_ctx); |
---|
| 155 | if (conn->ssl == NULL) { |
---|
[f920d9e] | 156 | goto ssl_connected_failure; |
---|
[5ebff60] | 157 | } |
---|
| 158 | |
---|
[309cb9e] | 159 | /* We can do at least the handshake with non-blocking I/O */ |
---|
[5ebff60] | 160 | sock_make_nonblocking(conn->fd); |
---|
| 161 | SSL_set_fd(conn->ssl, conn->fd); |
---|
| 162 | |
---|
| 163 | if (conn->hostname && !g_ascii_isdigit(conn->hostname[0])) { |
---|
| 164 | SSL_set_tlsext_host_name(conn->ssl, conn->hostname); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | return ssl_handshake(data, source, cond); |
---|
[f920d9e] | 168 | |
---|
| 169 | ssl_connected_failure: |
---|
[5ebff60] | 170 | conn->func(conn->data, 0, NULL, cond); |
---|
| 171 | ssl_disconnect(conn); |
---|
[f920d9e] | 172 | return FALSE; |
---|
| 173 | |
---|
[5ebff60] | 174 | } |
---|
[26fdfc5] | 175 | |
---|
[5ebff60] | 176 | static gboolean ssl_handshake(gpointer data, gint source, b_input_condition cond) |
---|
[26fdfc5] | 177 | { |
---|
| 178 | struct scd *conn = data; |
---|
| 179 | int st; |
---|
[5ebff60] | 180 | |
---|
| 181 | if ((st = SSL_connect(conn->ssl)) < 0) { |
---|
| 182 | conn->lasterr = SSL_get_error(conn->ssl, st); |
---|
| 183 | if (conn->lasterr != SSL_ERROR_WANT_READ && conn->lasterr != SSL_ERROR_WANT_WRITE) { |
---|
| 184 | conn->func(conn->data, 0, NULL, cond); |
---|
| 185 | ssl_disconnect(conn); |
---|
[f920d9e] | 186 | return FALSE; |
---|
| 187 | } |
---|
[5ebff60] | 188 | |
---|
| 189 | conn->inpa = b_input_add(conn->fd, ssl_getdirection(conn), ssl_handshake, data); |
---|
[309cb9e] | 190 | return FALSE; |
---|
[26fdfc5] | 191 | } |
---|
[5ebff60] | 192 | |
---|
[b7d3cc34] | 193 | conn->established = TRUE; |
---|
[5ebff60] | 194 | sock_make_blocking(conn->fd); /* For now... */ |
---|
| 195 | conn->func(conn->data, 0, conn, cond); |
---|
[309cb9e] | 196 | return FALSE; |
---|
[b7d3cc34] | 197 | } |
---|
| 198 | |
---|
[5ebff60] | 199 | int ssl_read(void *conn, char *buf, int len) |
---|
[b7d3cc34] | 200 | { |
---|
[26fdfc5] | 201 | int st; |
---|
[5ebff60] | 202 | |
---|
| 203 | if (!((struct scd*) conn)->established) { |
---|
[26fdfc5] | 204 | ssl_errno = SSL_NOHANDSHAKE; |
---|
| 205 | return -1; |
---|
| 206 | } |
---|
[5ebff60] | 207 | |
---|
| 208 | st = SSL_read(((struct scd*) conn)->ssl, buf, len); |
---|
| 209 | |
---|
[26fdfc5] | 210 | ssl_errno = SSL_OK; |
---|
[5ebff60] | 211 | if (st <= 0) { |
---|
| 212 | ((struct scd*) conn)->lasterr = SSL_get_error(((struct scd*) conn)->ssl, st); |
---|
| 213 | if (((struct scd*) conn)->lasterr == SSL_ERROR_WANT_READ || ((struct scd*) conn)->lasterr == |
---|
| 214 | SSL_ERROR_WANT_WRITE) { |
---|
[26fdfc5] | 215 | ssl_errno = SSL_AGAIN; |
---|
[5ebff60] | 216 | } |
---|
[26fdfc5] | 217 | } |
---|
[5ebff60] | 218 | |
---|
| 219 | if (0 && getenv("BITLBEE_DEBUG") && st > 0) { |
---|
| 220 | write(1, buf, st); |
---|
| 221 | } |
---|
| 222 | |
---|
[26fdfc5] | 223 | return st; |
---|
[b7d3cc34] | 224 | } |
---|
| 225 | |
---|
[5ebff60] | 226 | int ssl_write(void *conn, const char *buf, int len) |
---|
[b7d3cc34] | 227 | { |
---|
[26fdfc5] | 228 | int st; |
---|
[5ebff60] | 229 | |
---|
| 230 | if (!((struct scd*) conn)->established) { |
---|
[26fdfc5] | 231 | ssl_errno = SSL_NOHANDSHAKE; |
---|
| 232 | return -1; |
---|
| 233 | } |
---|
[5ebff60] | 234 | |
---|
| 235 | st = SSL_write(((struct scd*) conn)->ssl, buf, len); |
---|
| 236 | |
---|
| 237 | if (0 && getenv("BITLBEE_DEBUG") && st > 0) { |
---|
| 238 | write(1, buf, st); |
---|
| 239 | } |
---|
| 240 | |
---|
[26fdfc5] | 241 | ssl_errno = SSL_OK; |
---|
[5ebff60] | 242 | if (st <= 0) { |
---|
| 243 | ((struct scd*) conn)->lasterr = SSL_get_error(((struct scd*) conn)->ssl, st); |
---|
| 244 | if (((struct scd*) conn)->lasterr == SSL_ERROR_WANT_READ || ((struct scd*) conn)->lasterr == |
---|
| 245 | SSL_ERROR_WANT_WRITE) { |
---|
[26fdfc5] | 246 | ssl_errno = SSL_AGAIN; |
---|
[5ebff60] | 247 | } |
---|
[26fdfc5] | 248 | } |
---|
[5ebff60] | 249 | |
---|
[26fdfc5] | 250 | return st; |
---|
[b7d3cc34] | 251 | } |
---|
| 252 | |
---|
[5ebff60] | 253 | int ssl_pending(void *conn) |
---|
[8a2221a7] | 254 | { |
---|
[5ebff60] | 255 | return (((struct scd*) conn) && ((struct scd*) conn)->established) ? |
---|
| 256 | SSL_pending(((struct scd*) conn)->ssl) > 0 : 0; |
---|
[8a2221a7] | 257 | } |
---|
| 258 | |
---|
[5ebff60] | 259 | static void ssl_conn_free(struct scd *conn) |
---|
[3f661849] | 260 | { |
---|
[5ebff60] | 261 | SSL_free(conn->ssl); |
---|
| 262 | g_free(conn->hostname); |
---|
| 263 | g_free(conn); |
---|
| 264 | |
---|
[3f661849] | 265 | } |
---|
| 266 | |
---|
[5ebff60] | 267 | void ssl_disconnect(void *conn_) |
---|
[b7d3cc34] | 268 | { |
---|
| 269 | struct scd *conn = conn_; |
---|
[5ebff60] | 270 | |
---|
| 271 | if (conn->inpa != -1) { |
---|
| 272 | b_event_remove(conn->inpa); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | if (conn->established) { |
---|
| 276 | SSL_shutdown(conn->ssl); |
---|
| 277 | } |
---|
| 278 | |
---|
[0db6618] | 279 | proxy_disconnect(conn->fd); |
---|
[5ebff60] | 280 | |
---|
| 281 | ssl_conn_free(conn); |
---|
[b7d3cc34] | 282 | } |
---|
| 283 | |
---|
[5ebff60] | 284 | int ssl_getfd(void *conn) |
---|
[b7d3cc34] | 285 | { |
---|
[5ebff60] | 286 | return(((struct scd*) conn)->fd); |
---|
[b7d3cc34] | 287 | } |
---|
[26fdfc5] | 288 | |
---|
[5ebff60] | 289 | b_input_condition ssl_getdirection(void *conn) |
---|
[26fdfc5] | 290 | { |
---|
[5ebff60] | 291 | return(((struct scd*) conn)->lasterr == SSL_ERROR_WANT_WRITE ? B_EV_IO_WRITE : B_EV_IO_READ); |
---|
[26fdfc5] | 292 | } |
---|
[523fb23] | 293 | |
---|
[5ebff60] | 294 | char *ssl_verify_strerror(int code) |
---|
[78b8401] | 295 | { |
---|
[5ebff60] | 296 | return g_strdup("SSL certificate verification not supported by BitlBee OpenSSL code."); |
---|
[78b8401] | 297 | } |
---|
| 298 | |
---|
[5ebff60] | 299 | size_t ssl_des3_encrypt(const unsigned char *key, size_t key_len, const unsigned char *input, size_t input_len, |
---|
| 300 | const unsigned char *iv, unsigned char **res) |
---|
[523fb23] | 301 | { |
---|
[5ebff60] | 302 | int output_length = 0; |
---|
[50b8978] | 303 | EVP_CIPHER_CTX ctx; |
---|
[5ebff60] | 304 | |
---|
[523fb23] | 305 | *res = g_new0(unsigned char, 72); |
---|
[5ebff60] | 306 | |
---|
[523fb23] | 307 | /* Don't set key or IV because we will modify the parameters */ |
---|
| 308 | EVP_CIPHER_CTX_init(&ctx); |
---|
| 309 | EVP_CipherInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, NULL, NULL, 1); |
---|
| 310 | EVP_CIPHER_CTX_set_key_length(&ctx, key_len); |
---|
| 311 | EVP_CIPHER_CTX_set_padding(&ctx, 0); |
---|
| 312 | /* We finished modifying parameters so now we can set key and IV */ |
---|
| 313 | EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, 1); |
---|
| 314 | EVP_CipherUpdate(&ctx, *res, &output_length, input, input_len); |
---|
| 315 | EVP_CipherFinal_ex(&ctx, *res, &output_length); |
---|
[5ebff60] | 316 | EVP_CIPHER_CTX_cleanup(&ctx); |
---|
[50b8978] | 317 | //EVP_cleanup(); |
---|
[5ebff60] | 318 | |
---|
[523fb23] | 319 | return output_length; |
---|
| 320 | } |
---|