2008-04-01 16:52:26 +00:00
|
|
|
/*
|
|
|
|
* libratbox: a library used by ircd-ratbox and other things
|
2016-09-15 14:08:19 +00:00
|
|
|
* openssl.c: OpenSSL backend
|
2008-04-01 16:52:26 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2008 ircd-ratbox development team
|
|
|
|
* Copyright (C) 2007-2008 Aaron Sethman <androsyn@ratbox.org>
|
2016-09-15 14:08:19 +00:00
|
|
|
* Copyright (C) 2015-2016 Aaron Jones <aaronmdjones@gmail.com>
|
2008-04-01 16:52:26 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-03-03 04:25:47 +00:00
|
|
|
*
|
2008-04-01 16:52:26 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
* USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libratbox_config.h>
|
|
|
|
#include <ratbox_lib.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_OPENSSL
|
|
|
|
|
|
|
|
#include <commio-int.h>
|
|
|
|
#include <commio-ssl.h>
|
2015-10-23 16:05:33 +00:00
|
|
|
|
2016-09-15 14:08:19 +00:00
|
|
|
#include "openssl_ratbox.h"
|
2016-06-01 17:45:36 +00:00
|
|
|
|
2016-09-15 17:06:05 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
RB_FD_TLS_DIRECTION_IN = 0,
|
|
|
|
RB_FD_TLS_DIRECTION_OUT = 1
|
|
|
|
} rb_fd_tls_direction;
|
|
|
|
|
|
|
|
#define SSL_P(x) ((SSL *)((x)->ssl))
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-15 15:30:12 +00:00
|
|
|
static SSL_CTX *ssl_ctx = NULL;
|
2008-04-01 16:52:26 +00:00
|
|
|
|
2016-09-15 15:51:33 +00:00
|
|
|
struct ssl_connect
|
|
|
|
{
|
|
|
|
CNCB *callback;
|
|
|
|
void *data;
|
|
|
|
int timeout;
|
|
|
|
};
|
|
|
|
|
2016-09-15 17:13:27 +00:00
|
|
|
static const char *rb_ssl_strerror(unsigned long);
|
2016-09-15 19:12:13 +00:00
|
|
|
static void rb_ssl_connect_realcb(rb_fde_t *, int, struct ssl_connect *);
|
2016-09-15 16:26:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal OpenSSL-specific code
|
|
|
|
*/
|
2016-09-15 15:51:33 +00:00
|
|
|
|
2008-12-02 23:49:39 +00:00
|
|
|
static unsigned long
|
2016-09-15 17:13:27 +00:00
|
|
|
rb_ssl_last_err(void)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:54:06 +00:00
|
|
|
unsigned long err_saved, err = 0;
|
2008-12-02 23:49:39 +00:00
|
|
|
|
2016-09-15 16:54:06 +00:00
|
|
|
while((err_saved = ERR_get_error()) != 0)
|
|
|
|
err = err_saved;
|
2008-04-01 16:52:26 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2016-09-15 17:06:05 +00:00
|
|
|
static void
|
|
|
|
rb_ssl_init_fd(rb_fde_t *const F, const rb_fd_tls_direction dir)
|
|
|
|
{
|
2016-09-15 17:13:27 +00:00
|
|
|
(void) rb_ssl_last_err();
|
2016-09-15 17:06:05 +00:00
|
|
|
|
|
|
|
F->ssl = SSL_new(ssl_ctx);
|
|
|
|
|
|
|
|
if(F->ssl == NULL)
|
|
|
|
{
|
2016-09-15 17:13:27 +00:00
|
|
|
rb_lib_log("%s: SSL_new: %s", __func__, rb_ssl_strerror(rb_ssl_last_err()));
|
2016-09-15 17:06:05 +00:00
|
|
|
rb_close(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(dir)
|
|
|
|
{
|
|
|
|
case RB_FD_TLS_DIRECTION_IN:
|
|
|
|
SSL_set_accept_state(SSL_P(F));
|
|
|
|
break;
|
|
|
|
case RB_FD_TLS_DIRECTION_OUT:
|
|
|
|
SSL_set_connect_state(SSL_P(F));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_set_fd(SSL_P(F), rb_get_fd(F));
|
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
static void
|
2016-09-15 19:12:13 +00:00
|
|
|
rb_ssl_accept_common(rb_fde_t *const F, void *const data)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 19:12:13 +00:00
|
|
|
lrb_assert(F != NULL);
|
|
|
|
lrb_assert(F->accept != NULL);
|
|
|
|
lrb_assert(F->accept->callback != NULL);
|
|
|
|
lrb_assert(F->ssl != NULL);
|
|
|
|
|
2016-09-15 17:13:27 +00:00
|
|
|
(void) rb_ssl_last_err();
|
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
int ret = SSL_do_handshake(SSL_P(F));
|
|
|
|
int err = SSL_get_error(SSL_P(F), ret);
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
if(ret == 1)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 19:12:13 +00:00
|
|
|
F->handshake_count++;
|
|
|
|
|
|
|
|
rb_settimeout(F, 0, NULL, NULL);
|
|
|
|
rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);
|
|
|
|
|
|
|
|
struct acceptdata *const ad = F->accept;
|
|
|
|
F->accept = NULL;
|
|
|
|
ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
|
|
|
|
rb_free(ad);
|
|
|
|
|
|
|
|
return;
|
2016-09-15 16:26:01 +00:00
|
|
|
}
|
2016-09-15 19:12:13 +00:00
|
|
|
if(ret == -1 && err == SSL_ERROR_WANT_READ)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
2016-09-15 19:12:13 +00:00
|
|
|
rb_setselect(F, RB_SELECT_READ, rb_ssl_accept_common, NULL);
|
|
|
|
return;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
2016-09-15 19:12:13 +00:00
|
|
|
if(ret == -1 && err == SSL_ERROR_WANT_WRITE)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 19:12:13 +00:00
|
|
|
rb_setselect(F, RB_SELECT_WRITE, rb_ssl_accept_common, NULL);
|
|
|
|
return;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
errno = EIO;
|
|
|
|
F->ssl_errno = (unsigned long) err;
|
|
|
|
F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data);
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
static void
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_tryconn_cb(rb_fde_t *const F, void *const data)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 17:06:05 +00:00
|
|
|
lrb_assert(F != NULL);
|
2016-09-15 19:12:13 +00:00
|
|
|
lrb_assert(F->ssl != NULL);
|
2016-09-15 17:06:05 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
(void) rb_ssl_last_err();
|
2016-09-15 16:26:01 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
int ret = SSL_do_handshake(SSL_P(F));
|
|
|
|
int err = SSL_get_error(SSL_P(F), ret);
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
if(ret == 1)
|
|
|
|
{
|
|
|
|
F->handshake_count++;
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
rb_settimeout(F, 0, NULL, NULL);
|
|
|
|
rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);
|
2016-09-15 17:13:27 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
rb_ssl_connect_realcb(F, RB_OK, data);
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(ret == -1 && err == SSL_ERROR_WANT_READ)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
2016-09-15 19:12:13 +00:00
|
|
|
rb_setselect(F, RB_SELECT_READ, rb_ssl_tryconn_cb, data);
|
|
|
|
return;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
2016-09-15 19:12:13 +00:00
|
|
|
if(ret == -1 && err == SSL_ERROR_WANT_WRITE)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 19:12:13 +00:00
|
|
|
rb_setselect(F, RB_SELECT_WRITE, rb_ssl_tryconn_cb, data);
|
|
|
|
return;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
2016-09-15 19:12:13 +00:00
|
|
|
|
|
|
|
errno = EIO;
|
|
|
|
F->ssl_errno = (unsigned long) err;
|
|
|
|
rb_ssl_connect_realcb(F, RB_ERROR_SSL, data);
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
static const char *
|
2016-09-15 17:13:27 +00:00
|
|
|
rb_ssl_strerror(unsigned long err)
|
2008-06-25 05:28:30 +00:00
|
|
|
{
|
2016-09-15 19:22:45 +00:00
|
|
|
static char errbuf[512];
|
2008-06-25 05:28:30 +00:00
|
|
|
|
2016-09-15 19:22:45 +00:00
|
|
|
ERR_error_string_n(err, errbuf, sizeof errbuf);
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2016-09-15 19:22:45 +00:00
|
|
|
return errbuf;
|
2008-06-25 05:28:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
static int
|
2016-09-15 16:35:47 +00:00
|
|
|
verify_accept_all_cb(const int preverify_ok, X509_STORE_CTX *const x509_ctx)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
return 1;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_read_or_write(const int r_or_w, rb_fde_t *const F, void *const rbuf, const void *const wbuf, const size_t count)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
|
|
|
ssize_t ret;
|
|
|
|
unsigned long err;
|
|
|
|
|
2016-09-15 17:13:27 +00:00
|
|
|
(void) rb_ssl_last_err();
|
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
if(r_or_w == 0)
|
2016-09-15 17:06:05 +00:00
|
|
|
ret = (ssize_t) SSL_read(SSL_P(F), rbuf, (int)count);
|
2008-04-01 16:52:26 +00:00
|
|
|
else
|
2016-09-15 17:06:05 +00:00
|
|
|
ret = (ssize_t) SSL_write(SSL_P(F), wbuf, (int)count);
|
2008-04-01 16:52:26 +00:00
|
|
|
|
|
|
|
if(ret < 0)
|
|
|
|
{
|
2016-09-15 17:06:05 +00:00
|
|
|
switch(SSL_get_error(SSL_P(F), ret))
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
errno = EAGAIN;
|
|
|
|
return RB_RW_SSL_NEED_READ;
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
errno = EAGAIN;
|
|
|
|
return RB_RW_SSL_NEED_WRITE;
|
|
|
|
case SSL_ERROR_ZERO_RETURN:
|
|
|
|
return 0;
|
|
|
|
case SSL_ERROR_SYSCALL:
|
2016-09-15 17:13:27 +00:00
|
|
|
err = rb_ssl_last_err();
|
2008-04-01 16:52:26 +00:00
|
|
|
if(err == 0)
|
|
|
|
{
|
|
|
|
F->ssl_errno = 0;
|
|
|
|
return RB_RW_IO_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2016-09-15 17:13:27 +00:00
|
|
|
err = rb_ssl_last_err();
|
2008-04-01 16:52:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
F->ssl_errno = err;
|
|
|
|
if(err > 0)
|
|
|
|
{
|
|
|
|
errno = EIO; /* not great but... */
|
|
|
|
return RB_RW_SSL_ERROR;
|
|
|
|
}
|
|
|
|
return RB_RW_IO_ERROR;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
/*
|
|
|
|
* External OpenSSL-specific code
|
|
|
|
*/
|
2010-01-31 18:04:20 +00:00
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
void
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_shutdown(rb_fde_t *const F)
|
2010-05-08 22:30:51 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
if(F == NULL || F->ssl == NULL)
|
|
|
|
return;
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2016-09-15 17:13:27 +00:00
|
|
|
(void) rb_ssl_last_err();
|
|
|
|
|
2016-09-15 16:54:06 +00:00
|
|
|
for(int i = 0; i < 4; i++)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
2016-09-15 19:14:41 +00:00
|
|
|
int ret = SSL_shutdown(SSL_P(F));
|
|
|
|
int err = SSL_get_error(SSL_P(F), ret);
|
|
|
|
|
|
|
|
if(ret >= 0 || (err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE))
|
2016-09-15 16:26:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-09-15 16:54:06 +00:00
|
|
|
|
2016-09-15 17:06:05 +00:00
|
|
|
SSL_free(SSL_P(F));
|
|
|
|
F->ssl = NULL;
|
2010-05-08 22:30:51 +00:00
|
|
|
}
|
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
int
|
|
|
|
rb_init_ssl(void)
|
|
|
|
{
|
2016-09-15 17:16:40 +00:00
|
|
|
#ifndef LRB_SSL_NO_EXPLICIT_INIT
|
|
|
|
(void) SSL_library_init();
|
2016-04-30 21:34:42 +00:00
|
|
|
SSL_load_error_strings();
|
2016-09-15 17:16:40 +00:00
|
|
|
#endif
|
2016-04-30 21:34:42 +00:00
|
|
|
|
2016-09-15 17:16:40 +00:00
|
|
|
rb_lib_log("%s: OpenSSL backend initialised", __func__);
|
2016-04-30 21:34:42 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-05-20 02:27:59 +00:00
|
|
|
|
2016-04-30 21:34:42 +00:00
|
|
|
int
|
2016-09-15 17:34:14 +00:00
|
|
|
rb_setup_ssl_server(const char *const certfile, const char *keyfile,
|
|
|
|
const char *const dhfile, const char *cipherlist)
|
2016-04-30 21:34:42 +00:00
|
|
|
{
|
2016-09-15 17:34:14 +00:00
|
|
|
if(certfile == NULL)
|
2016-04-30 21:34:42 +00:00
|
|
|
{
|
2016-09-15 17:20:14 +00:00
|
|
|
rb_lib_log("%s: no certificate file specified", __func__);
|
2016-04-30 21:34:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-20 02:27:59 +00:00
|
|
|
|
2016-04-30 21:34:42 +00:00
|
|
|
if(keyfile == NULL)
|
2016-09-15 17:34:14 +00:00
|
|
|
keyfile = certfile;
|
2015-05-20 02:27:59 +00:00
|
|
|
|
2016-09-15 17:34:14 +00:00
|
|
|
if(cipherlist == NULL)
|
|
|
|
cipherlist = rb_default_ciphers;
|
2015-05-20 02:27:59 +00:00
|
|
|
|
2016-09-15 17:13:27 +00:00
|
|
|
|
|
|
|
(void) rb_ssl_last_err();
|
|
|
|
|
2016-04-30 21:34:42 +00:00
|
|
|
#ifdef LRB_HAVE_TLS_METHOD_API
|
2016-09-15 15:30:12 +00:00
|
|
|
SSL_CTX *const ssl_ctx_new = SSL_CTX_new(TLS_method());
|
2016-04-30 21:34:42 +00:00
|
|
|
#else
|
2016-09-15 15:30:12 +00:00
|
|
|
SSL_CTX *const ssl_ctx_new = SSL_CTX_new(SSLv23_method());
|
2013-09-06 18:05:49 +00:00
|
|
|
#endif
|
2008-04-01 16:52:26 +00:00
|
|
|
|
2016-09-15 15:30:12 +00:00
|
|
|
if(ssl_ctx_new == NULL)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 19:21:38 +00:00
|
|
|
rb_lib_log("%s: SSL_CTX_new: %s", __func__, rb_ssl_strerror(rb_ssl_last_err()));
|
2016-04-30 21:34:42 +00:00
|
|
|
return 0;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
2015-02-09 20:18:32 +00:00
|
|
|
|
2016-09-15 17:34:14 +00:00
|
|
|
if(SSL_CTX_use_certificate_chain_file(ssl_ctx_new, certfile) != 1)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 19:21:38 +00:00
|
|
|
rb_lib_log("%s: SSL_CTX_use_certificate_chain_file ('%s'): %s", __func__, certfile,
|
2016-09-15 19:24:40 +00:00
|
|
|
rb_ssl_strerror(rb_ssl_last_err()));
|
2016-08-12 13:15:56 +00:00
|
|
|
|
2016-09-15 15:30:12 +00:00
|
|
|
SSL_CTX_free(ssl_ctx_new);
|
2008-04-01 16:52:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-15 17:34:14 +00:00
|
|
|
if(SSL_CTX_use_PrivateKey_file(ssl_ctx_new, keyfile, SSL_FILETYPE_PEM) != 1)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 19:21:38 +00:00
|
|
|
rb_lib_log("%s: SSL_CTX_use_PrivateKey_file ('%s'): %s", __func__, keyfile,
|
2016-09-15 19:24:40 +00:00
|
|
|
rb_ssl_strerror(rb_ssl_last_err()));
|
2016-08-12 13:15:56 +00:00
|
|
|
|
2016-09-15 15:30:12 +00:00
|
|
|
SSL_CTX_free(ssl_ctx_new);
|
2008-04-01 16:52:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-15 17:34:14 +00:00
|
|
|
if(dhfile == NULL)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 17:34:14 +00:00
|
|
|
rb_lib_log("%s: no DH parameters file specified", __func__);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FILE *const dhf = fopen(dhfile, "r");
|
|
|
|
DH *dhp = NULL;
|
2016-05-25 21:53:09 +00:00
|
|
|
|
2016-09-15 17:34:14 +00:00
|
|
|
if(dhf == NULL)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 19:21:38 +00:00
|
|
|
rb_lib_log("%s: fopen ('%s'): %s", __func__, dhfile, strerror(errno));
|
2008-12-02 23:49:39 +00:00
|
|
|
}
|
2016-09-15 17:34:14 +00:00
|
|
|
else if(PEM_read_DHparams(dhf, &dhp, NULL, NULL) == NULL)
|
2008-12-02 23:49:39 +00:00
|
|
|
{
|
2016-09-15 19:21:38 +00:00
|
|
|
rb_lib_log("%s: PEM_read_DHparams ('%s'): %s", __func__, dhfile,
|
2016-09-15 17:34:14 +00:00
|
|
|
rb_ssl_strerror(rb_ssl_last_err()));
|
|
|
|
fclose(dhf);
|
2016-05-25 21:53:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-15 17:34:14 +00:00
|
|
|
SSL_CTX_set_tmp_dh(ssl_ctx_new, dhp);
|
|
|
|
DH_free(dhp);
|
|
|
|
fclose(dhf);
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-12 13:50:48 +00:00
|
|
|
|
2016-09-15 19:25:50 +00:00
|
|
|
if(SSL_CTX_set_cipher_list(ssl_ctx_new, cipherlist) != 1)
|
2016-09-15 17:34:14 +00:00
|
|
|
{
|
|
|
|
rb_lib_log("%s: SSL_CTX_set_cipher_list: could not configure any ciphers", __func__);
|
|
|
|
SSL_CTX_free(ssl_ctx_new);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX_set_session_cache_mode(ssl_ctx_new, SSL_SESS_CACHE_OFF);
|
|
|
|
SSL_CTX_set_verify(ssl_ctx_new, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, verify_accept_all_cb);
|
|
|
|
|
|
|
|
#ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
|
|
|
|
(void) SSL_CTX_clear_options(ssl_ctx_new, SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef LRB_HAVE_TLS_METHOD_API
|
|
|
|
(void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SSL_OP_NO_TICKET
|
|
|
|
(void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_NO_TICKET);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SSL_OP_CIPHER_SERVER_PREFERENCE
|
|
|
|
(void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_CIPHER_SERVER_PREFERENCE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SSL_OP_SINGLE_DH_USE
|
|
|
|
(void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_DH_USE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SSL_OP_SINGLE_ECDH_USE
|
|
|
|
(void) SSL_CTX_set_options(ssl_ctx_new, SSL_OP_SINGLE_ECDH_USE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LRB_HAVE_TLS_ECDH_AUTO
|
|
|
|
(void) SSL_CTX_set_ecdh_auto(ssl_ctx_new, 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef LRB_HAVE_TLS_SET_CURVES
|
|
|
|
(void) SSL_CTX_set1_curves_list(ssl_ctx_new, rb_default_curves);
|
|
|
|
#else
|
|
|
|
# if (OPENSSL_VERSION_NUMBER >= 0x10000000L) && !defined(OPENSSL_NO_ECDH) && defined(NID_secp384r1)
|
|
|
|
EC_KEY *const ec_key = EC_KEY_new_by_curve_name(NID_secp384r1);
|
|
|
|
if(ec_key != NULL)
|
|
|
|
{
|
|
|
|
SSL_CTX_set_tmp_ecdh(ssl_ctx_new, ec_key);
|
|
|
|
EC_KEY_free(ec_key);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rb_lib_log("%s: EC_KEY_new_by_curve_name failed; will not enable ECDHE- ciphers", __func__);
|
|
|
|
# else
|
|
|
|
rb_lib_log("%s: OpenSSL built without ECDH support; will not enable ECDHE- ciphers", __func__);
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-09-15 15:30:12 +00:00
|
|
|
if(ssl_ctx)
|
|
|
|
SSL_CTX_free(ssl_ctx);
|
2016-08-12 13:15:56 +00:00
|
|
|
|
2016-09-15 15:30:12 +00:00
|
|
|
ssl_ctx = ssl_ctx_new;
|
2016-08-12 13:15:56 +00:00
|
|
|
|
2016-09-15 17:34:14 +00:00
|
|
|
|
|
|
|
rb_lib_log("%s: TLS configuration successful", __func__);
|
2008-04-01 16:52:26 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_init_prng(const char *const path, prng_seed_t seed_type)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
if(seed_type == RB_PRNG_DEFAULT)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
RAND_screen();
|
|
|
|
#endif
|
|
|
|
return RAND_status();
|
|
|
|
}
|
|
|
|
if(path == NULL)
|
|
|
|
return RAND_status();
|
2012-03-17 14:48:25 +00:00
|
|
|
|
2016-09-15 16:54:06 +00:00
|
|
|
switch(seed_type)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
|
|
|
case RB_PRNG_FILE:
|
|
|
|
if(RAND_load_file(path, -1) == -1)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
#ifdef _WIN32
|
|
|
|
case RB_PRNGWIN32:
|
|
|
|
RAND_screen();
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
2012-03-17 14:48:25 +00:00
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
return RAND_status();
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
int
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_get_random(void *const buf, const size_t length)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if((ret = RAND_bytes(buf, length)) == 0)
|
|
|
|
{
|
|
|
|
/* remove the error from the queue */
|
2016-09-15 17:13:27 +00:00
|
|
|
rb_ssl_last_err();
|
2016-09-15 16:26:01 +00:00
|
|
|
}
|
|
|
|
return ret;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
const char *
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_get_ssl_strerror(rb_fde_t *const F)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 17:13:27 +00:00
|
|
|
return rb_ssl_strerror(F->ssl_errno);
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
int
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
if(F->ssl == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2016-09-15 16:54:06 +00:00
|
|
|
const EVP_MD *evp;
|
|
|
|
unsigned int len;
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
switch(method)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
case RB_SSL_CERTFP_METH_SHA1:
|
|
|
|
evp = EVP_sha1();
|
|
|
|
len = RB_SSL_CERTFP_LEN_SHA1;
|
|
|
|
break;
|
|
|
|
case RB_SSL_CERTFP_METH_SHA256:
|
|
|
|
evp = EVP_sha256();
|
|
|
|
len = RB_SSL_CERTFP_LEN_SHA256;
|
|
|
|
break;
|
|
|
|
case RB_SSL_CERTFP_METH_SHA512:
|
|
|
|
evp = EVP_sha512();
|
|
|
|
len = RB_SSL_CERTFP_LEN_SHA512;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 17:06:05 +00:00
|
|
|
X509 *const cert = SSL_get_peer_certificate(SSL_P(F));
|
2016-09-15 16:26:01 +00:00
|
|
|
if(cert == NULL)
|
|
|
|
return 0;
|
|
|
|
|
2016-09-15 17:06:05 +00:00
|
|
|
int res = SSL_get_verify_result(SSL_P(F));
|
2016-09-15 16:26:01 +00:00
|
|
|
switch(res)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
case X509_V_OK:
|
|
|
|
case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
|
|
|
|
case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
|
|
|
|
case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
|
|
|
|
case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
|
|
|
|
case X509_V_ERR_CERT_UNTRUSTED:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
X509_free(cert);
|
|
|
|
return 0;
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
2016-09-15 16:26:01 +00:00
|
|
|
|
|
|
|
X509_digest(cert, evp, certfp, &len);
|
|
|
|
X509_free(cert);
|
|
|
|
|
|
|
|
return (int) len;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_get_ssl_info(char *const buf, const size_t len)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
|
|
|
#ifdef LRB_SSL_FULL_VERSION_INFO
|
2016-09-15 16:54:06 +00:00
|
|
|
if(LRB_SSL_VNUM_RUNTIME == LRB_SSL_VNUM_COMPILETIME)
|
|
|
|
(void) rb_snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s",
|
|
|
|
LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME);
|
2008-04-01 16:52:26 +00:00
|
|
|
else
|
2016-09-15 16:54:06 +00:00
|
|
|
(void) rb_snprintf(buf, len, "OpenSSL: compiled (0x%lx, %s), library (0x%lx, %s)",
|
|
|
|
LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_COMPILETIME,
|
|
|
|
LRB_SSL_VNUM_RUNTIME, LRB_SSL_VTEXT_RUNTIME);
|
2016-09-15 16:26:01 +00:00
|
|
|
#else
|
2016-09-15 16:54:06 +00:00
|
|
|
(void) rb_snprintf(buf, len, "OpenSSL: compiled 0x%lx, library %s",
|
|
|
|
LRB_SSL_VNUM_COMPILETIME, LRB_SSL_VTEXT_RUNTIME);
|
2016-09-15 16:26:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_get_cipher(rb_fde_t *const F)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
|
|
|
if(F == NULL || F->ssl == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
static char buf[512];
|
|
|
|
|
2016-09-15 17:06:05 +00:00
|
|
|
const char *const version = SSL_get_version(SSL_P(F));
|
|
|
|
const char *const cipher = SSL_get_cipher_name(SSL_P(F));
|
2016-09-15 16:26:01 +00:00
|
|
|
|
|
|
|
(void) rb_snprintf(buf, sizeof buf, "%s, %s", version, cipher);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
|
|
|
return rb_ssl_read_or_write(0, F, buf, NULL, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_write(rb_fde_t *const F, const void *const buf, const size_t count)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
|
|
|
return rb_ssl_read_or_write(1, F, NULL, buf, count);
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
/*
|
|
|
|
* Internal library-agnostic code
|
|
|
|
*/
|
2008-04-01 16:52:26 +00:00
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
static void
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
F->connect->callback = sconn->callback;
|
|
|
|
F->connect->data = sconn->data;
|
|
|
|
rb_free(sconn);
|
|
|
|
rb_connect_callback(F, status);
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
static void
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_timeout(rb_fde_t *const F, void *const notused)
|
2008-04-01 16:52:26 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
lrb_assert(F->accept != NULL);
|
|
|
|
F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data);
|
2008-04-01 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
static void
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data)
|
2010-01-31 18:04:20 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data);
|
|
|
|
}
|
2015-05-27 21:46:46 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
static void
|
|
|
|
rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data)
|
|
|
|
{
|
|
|
|
lrb_assert(F != NULL);
|
|
|
|
|
|
|
|
struct ssl_connect *const sconn = data;
|
|
|
|
|
|
|
|
if(status != RB_OK)
|
|
|
|
{
|
|
|
|
rb_ssl_connect_realcb(F, status, sconn);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
F->type |= RB_FD_SSL;
|
|
|
|
|
|
|
|
rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
|
|
|
|
rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT);
|
|
|
|
rb_ssl_tryconn_cb(F, sconn);
|
|
|
|
}
|
|
|
|
|
2010-01-31 18:04:20 +00:00
|
|
|
|
2016-04-30 21:34:42 +00:00
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
/*
|
|
|
|
* External library-agnostic code
|
|
|
|
*/
|
2010-01-31 18:04:20 +00:00
|
|
|
|
2008-04-01 16:52:26 +00:00
|
|
|
int
|
|
|
|
rb_supports_ssl(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
unsigned int
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_handshake_count(rb_fde_t *const F)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
|
|
|
return F->handshake_count;
|
|
|
|
}
|
|
|
|
|
2008-12-22 09:49:01 +00:00
|
|
|
void
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_clear_handshake_count(rb_fde_t *const F)
|
2008-12-22 09:49:01 +00:00
|
|
|
{
|
2016-09-15 16:26:01 +00:00
|
|
|
F->handshake_count = 0;
|
2008-12-22 09:49:01 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
void
|
|
|
|
rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout)
|
|
|
|
{
|
|
|
|
F->type |= RB_FD_SSL;
|
|
|
|
|
|
|
|
F->accept = rb_malloc(sizeof(struct acceptdata));
|
|
|
|
F->accept->callback = cb;
|
|
|
|
F->accept->data = data;
|
|
|
|
F->accept->addrlen = 0;
|
|
|
|
(void) memset(&F->accept->S, 0x00, sizeof F->accept->S);
|
|
|
|
|
|
|
|
rb_settimeout(F, timeout, rb_ssl_timeout, NULL);
|
|
|
|
rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_IN);
|
|
|
|
rb_ssl_accept_common(F, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_ssl_accept_setup(rb_fde_t *const srv_F, rb_fde_t *const cli_F, struct sockaddr *const st, const int addrlen)
|
|
|
|
{
|
|
|
|
cli_F->type |= RB_FD_SSL;
|
|
|
|
|
|
|
|
cli_F->accept = rb_malloc(sizeof(struct acceptdata));
|
|
|
|
cli_F->accept->callback = srv_F->accept->callback;
|
|
|
|
cli_F->accept->data = srv_F->accept->data;
|
|
|
|
cli_F->accept->addrlen = addrlen;
|
|
|
|
(void) memset(&cli_F->accept->S, 0x00, sizeof cli_F->accept->S);
|
|
|
|
(void) memcpy(&cli_F->accept->S, st, addrlen);
|
|
|
|
|
|
|
|
rb_settimeout(cli_F, 10, rb_ssl_timeout, NULL);
|
|
|
|
rb_ssl_init_fd(cli_F, RB_FD_TLS_DIRECTION_IN);
|
|
|
|
rb_ssl_accept_common(cli_F, NULL);
|
|
|
|
}
|
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
int
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept)
|
2015-12-11 14:32:02 +00:00
|
|
|
{
|
2016-09-15 16:54:06 +00:00
|
|
|
int result = rb_listen(F, backlog, defer_accept);
|
2015-12-11 14:32:02 +00:00
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL;
|
2015-12-11 14:32:02 +00:00
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
return result;
|
|
|
|
}
|
2016-08-20 03:08:30 +00:00
|
|
|
|
2016-09-15 16:26:01 +00:00
|
|
|
void
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_connect_tcp_ssl(rb_fde_t *const F, struct sockaddr *const dest, struct sockaddr *const clocal,
|
|
|
|
const int socklen, CNCB *const callback, void *const data, const int timeout)
|
2016-09-15 16:26:01 +00:00
|
|
|
{
|
|
|
|
if(F == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-09-15 16:35:47 +00:00
|
|
|
struct ssl_connect *const sconn = rb_malloc(sizeof *sconn);
|
2016-09-15 16:26:01 +00:00
|
|
|
sconn->data = data;
|
|
|
|
sconn->callback = callback;
|
|
|
|
sconn->timeout = timeout;
|
2016-08-20 03:08:30 +00:00
|
|
|
|
2016-09-15 16:35:47 +00:00
|
|
|
rb_connect_tcp(F, dest, clocal, socklen, rb_ssl_tryconn, sconn, timeout);
|
2015-12-11 14:32:02 +00:00
|
|
|
}
|
2008-12-22 09:49:01 +00:00
|
|
|
|
2016-09-15 19:12:13 +00:00
|
|
|
void
|
|
|
|
rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout)
|
|
|
|
{
|
|
|
|
if(F == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct ssl_connect *const sconn = rb_malloc(sizeof *sconn);
|
|
|
|
sconn->data = data;
|
|
|
|
sconn->callback = callback;
|
|
|
|
sconn->timeout = timeout;
|
|
|
|
|
|
|
|
F->connect = rb_malloc(sizeof(struct conndata));
|
|
|
|
F->connect->callback = callback;
|
|
|
|
F->connect->data = data;
|
|
|
|
F->type |= RB_FD_SSL;
|
|
|
|
|
|
|
|
rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
|
|
|
|
rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT);
|
|
|
|
rb_ssl_tryconn_cb(F, sconn);
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:27:15 +00:00
|
|
|
#endif /* HAVE_OPENSSL */
|