2015-12-04 04:32:33 +00:00
|
|
|
/*
|
2016-03-06 09:49:27 +00:00
|
|
|
* librb: a library used by ircd-ratbox and other things
|
2016-09-04 03:50:29 +00:00
|
|
|
* mbedtls.c: ARM MbedTLS backend
|
2015-12-04 04:32:33 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2007-2008 ircd-ratbox development team
|
|
|
|
* Copyright (C) 2007-2008 Aaron Sethman <androsyn@ratbox.org>
|
|
|
|
* Copyright (C) 2015 William Pitcock <nenolod@dereferenced.org>
|
2016-05-05 03:31:32 +00:00
|
|
|
* Copyright (C) 2016 Aaron Jones <aaronmdjones@gmail.com>
|
2015-12-04 04:32:33 +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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-03-06 09:49:27 +00:00
|
|
|
#include <librb_config.h>
|
|
|
|
#include <rb_lib.h>
|
2015-12-04 04:32:33 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MBEDTLS
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
#include <commio-int.h>
|
|
|
|
#include <commio-ssl.h>
|
|
|
|
#include <stdbool.h>
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
#include "mbedtls_ratbox.h"
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
RB_FD_TLS_DIRECTION_IN = 0,
|
|
|
|
RB_FD_TLS_DIRECTION_OUT = 1
|
|
|
|
} rb_fd_tls_direction;
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
#define RB_MAX_CIPHERSUITES 512
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
mbedtls_x509_crt crt;
|
|
|
|
mbedtls_pk_context key;
|
|
|
|
mbedtls_dhm_context dhp;
|
|
|
|
mbedtls_ssl_config server_cfg;
|
|
|
|
mbedtls_ssl_config client_cfg;
|
2016-09-04 03:50:29 +00:00
|
|
|
int suites[RB_MAX_CIPHERSUITES + 1];
|
2016-05-05 03:31:32 +00:00
|
|
|
size_t refcount;
|
|
|
|
} rb_mbedtls_cfg_context;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
rb_mbedtls_cfg_context *cfg;
|
|
|
|
mbedtls_ssl_context ssl;
|
|
|
|
} rb_mbedtls_ssl_context;
|
|
|
|
|
|
|
|
#define SSL_C(x) ((rb_mbedtls_ssl_context *) (x)->ssl)->cfg
|
|
|
|
#define SSL_P(x) &((rb_mbedtls_ssl_context *) (x)->ssl)->ssl
|
|
|
|
|
|
|
|
static mbedtls_ctr_drbg_context ctr_drbg_ctx;
|
|
|
|
static mbedtls_entropy_context entropy_ctx;
|
|
|
|
|
|
|
|
static mbedtls_x509_crt dummy_ca_ctx;
|
|
|
|
static rb_mbedtls_cfg_context *rb_mbedtls_cfg = NULL;
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
struct ssl_connect
|
|
|
|
{
|
|
|
|
CNCB *callback;
|
|
|
|
void *data;
|
|
|
|
int timeout;
|
|
|
|
};
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
static const char *rb_ssl_strerror(int);
|
2016-09-04 03:50:29 +00:00
|
|
|
static void rb_ssl_connect_realcb(rb_fde_t *, int, struct ssl_connect *);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Internal MbedTLS-specific code
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_mbedtls_cfg_incref(rb_mbedtls_cfg_context *const cfg)
|
2016-05-05 03:31:32 +00:00
|
|
|
{
|
|
|
|
lrb_assert(cfg->refcount > 0);
|
|
|
|
|
|
|
|
cfg->refcount++;
|
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
static void
|
|
|
|
rb_mbedtls_cfg_decref(rb_mbedtls_cfg_context *const cfg)
|
2016-05-05 03:31:32 +00:00
|
|
|
{
|
|
|
|
if(cfg == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
lrb_assert(cfg->refcount > 0);
|
|
|
|
|
|
|
|
if((--cfg->refcount) > 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mbedtls_ssl_config_free(&cfg->client_cfg);
|
|
|
|
mbedtls_ssl_config_free(&cfg->server_cfg);
|
|
|
|
mbedtls_dhm_free(&cfg->dhp);
|
|
|
|
mbedtls_pk_free(&cfg->key);
|
|
|
|
mbedtls_x509_crt_free(&cfg->crt);
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_free(cfg);
|
|
|
|
}
|
|
|
|
|
2016-11-15 12:16:03 +00:00
|
|
|
static int
|
|
|
|
rb_sock_net_recv(void *const context_ptr, unsigned char *const buf, const size_t count)
|
|
|
|
{
|
|
|
|
const int fd = rb_get_fd((rb_fde_t *)context_ptr);
|
|
|
|
|
|
|
|
const int ret = (int) read(fd, buf, count);
|
|
|
|
|
|
|
|
if(ret < 0 && rb_ignore_errno(errno))
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_READ;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
rb_sock_net_xmit(void *const context_ptr, const unsigned char *const buf, const size_t count)
|
|
|
|
{
|
|
|
|
const int fd = rb_get_fd((rb_fde_t *)context_ptr);
|
|
|
|
|
|
|
|
const int ret = (int) write(fd, buf, count);
|
|
|
|
|
|
|
|
if(ret < 0 && rb_ignore_errno(errno))
|
|
|
|
return MBEDTLS_ERR_SSL_WANT_WRITE;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
static void
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_init_fd(rb_fde_t *const F, const rb_fd_tls_direction dir)
|
2016-09-10 08:51:09 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_mbedtls_ssl_context *const mbed_ssl_ctx = rb_malloc(sizeof *mbed_ssl_ctx);
|
|
|
|
|
|
|
|
if(mbed_ssl_ctx == NULL)
|
|
|
|
{
|
|
|
|
rb_lib_log("%s: rb_malloc: allocation failure", __func__);
|
|
|
|
rb_close(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
mbedtls_ssl_config *mbed_config;
|
|
|
|
|
|
|
|
switch(dir)
|
|
|
|
{
|
|
|
|
case RB_FD_TLS_DIRECTION_IN:
|
|
|
|
mbed_config = &rb_mbedtls_cfg->server_cfg;
|
|
|
|
break;
|
|
|
|
case RB_FD_TLS_DIRECTION_OUT:
|
|
|
|
mbed_config = &rb_mbedtls_cfg->client_cfg;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_init(&mbed_ssl_ctx->ssl);
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if((ret = mbedtls_ssl_setup(&mbed_ssl_ctx->ssl, mbed_config)) != 0)
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: ssl_setup: %s", __func__, rb_ssl_strerror(ret));
|
2016-09-10 08:51:09 +00:00
|
|
|
mbedtls_ssl_free(&mbed_ssl_ctx->ssl);
|
|
|
|
rb_free(mbed_ssl_ctx);
|
|
|
|
rb_close(F);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
mbedtls_ssl_set_bio(&mbed_ssl_ctx->ssl, F, rb_sock_net_xmit, rb_sock_net_recv, NULL);
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_mbedtls_cfg_incref(rb_mbedtls_cfg);
|
|
|
|
mbed_ssl_ctx->cfg = rb_mbedtls_cfg;
|
|
|
|
|
|
|
|
F->ssl = mbed_ssl_ctx;
|
2016-05-05 03:31:32 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
static rb_mbedtls_cfg_context *
|
|
|
|
rb_mbedtls_cfg_new(void)
|
2016-05-05 03:31:32 +00:00
|
|
|
{
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_mbedtls_cfg_context *const cfg = rb_malloc(sizeof *cfg);
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
if(cfg == NULL)
|
2016-05-05 03:31:32 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mbedtls_x509_crt_init(&cfg->crt);
|
|
|
|
mbedtls_pk_init(&cfg->key);
|
|
|
|
mbedtls_dhm_init(&cfg->dhp);
|
|
|
|
mbedtls_ssl_config_init(&cfg->server_cfg);
|
|
|
|
mbedtls_ssl_config_init(&cfg->client_cfg);
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
(void) memset(cfg->suites, 0x00, sizeof cfg->suites);
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
cfg->refcount = 1;
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
if((ret = mbedtls_ssl_config_defaults(&cfg->server_cfg,
|
|
|
|
MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: ssl_config_defaults (server): %s", __func__, rb_ssl_strerror(ret));
|
2016-05-05 03:31:32 +00:00
|
|
|
rb_mbedtls_cfg_decref(cfg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((ret = mbedtls_ssl_config_defaults(&cfg->client_cfg,
|
|
|
|
MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: ssl_config_defaults (client): %s", __func__, rb_ssl_strerror(ret));
|
2016-05-05 03:31:32 +00:00
|
|
|
rb_mbedtls_cfg_decref(cfg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_rng(&cfg->server_cfg, mbedtls_ctr_drbg_random, &ctr_drbg_ctx);
|
|
|
|
mbedtls_ssl_conf_rng(&cfg->client_cfg, mbedtls_ctr_drbg_random, &ctr_drbg_ctx);
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_ca_chain(&cfg->server_cfg, &dummy_ca_ctx, NULL);
|
|
|
|
mbedtls_ssl_conf_ca_chain(&cfg->client_cfg, &dummy_ca_ctx, NULL);
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_authmode(&cfg->server_cfg, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
|
|
|
mbedtls_ssl_conf_authmode(&cfg->client_cfg, MBEDTLS_SSL_VERIFY_NONE);
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
#ifdef MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE
|
|
|
|
mbedtls_ssl_conf_legacy_renegotiation(&cfg->client_cfg, MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MBEDTLS_SSL_SESSION_TICKETS_DISABLED
|
|
|
|
mbedtls_ssl_conf_session_tickets(&cfg->client_cfg, MBEDTLS_SSL_SESSION_TICKETS_DISABLED);
|
|
|
|
#endif
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
return cfg;
|
|
|
|
}
|
2015-12-04 04:32:33 +00:00
|
|
|
|
|
|
|
static void
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_ssl_accept_common(rb_fde_t *const F, void *const data)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
lrb_assert(F != NULL);
|
2015-12-04 04:32:33 +00:00
|
|
|
lrb_assert(F->accept != NULL);
|
2016-09-04 03:50:29 +00:00
|
|
|
lrb_assert(F->accept->callback != NULL);
|
|
|
|
lrb_assert(F->ssl != NULL);
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
const int ret = mbedtls_ssl_handshake(SSL_P(F));
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
switch(ret)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-10 08:51:09 +00:00
|
|
|
case 0:
|
|
|
|
F->handshake_count++;
|
|
|
|
break;
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_READ:
|
|
|
|
rb_setselect(F, RB_SELECT_READ, rb_ssl_accept_common, NULL);
|
|
|
|
return;
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_WRITE:
|
|
|
|
rb_setselect(F, RB_SELECT_WRITE, rb_ssl_accept_common, NULL);
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
errno = EIO;
|
2016-09-17 17:19:31 +00:00
|
|
|
F->ssl_errno = (unsigned long) -ret;
|
2016-09-10 08:51:09 +00:00
|
|
|
F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data);
|
|
|
|
return;
|
2016-05-05 03:31:32 +00:00
|
|
|
}
|
2015-12-04 06:01:40 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_settimeout(F, 0, NULL, NULL);
|
|
|
|
rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);
|
2015-12-04 06:01:40 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
struct acceptdata *const ad = F->accept;
|
|
|
|
F->accept = NULL;
|
|
|
|
ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_free(ad);
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_connect_common(rb_fde_t *const F, void *const data)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
lrb_assert(F != NULL);
|
|
|
|
lrb_assert(F->ssl != NULL);
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
const int ret = mbedtls_ssl_handshake(SSL_P(F));
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
switch(ret)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
2016-09-10 08:51:09 +00:00
|
|
|
case 0:
|
|
|
|
F->handshake_count++;
|
|
|
|
break;
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_READ:
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_setselect(F, RB_SELECT_READ, rb_ssl_connect_common, data);
|
2016-09-10 08:51:09 +00:00
|
|
|
return;
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_WRITE:
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_setselect(F, RB_SELECT_WRITE, rb_ssl_connect_common, data);
|
2016-09-10 08:51:09 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
errno = EIO;
|
2016-09-17 17:19:31 +00:00
|
|
|
F->ssl_errno = (unsigned long) -ret;
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_ssl_connect_realcb(F, RB_ERROR_SSL, data);
|
|
|
|
return;
|
2016-09-04 03:50:29 +00:00
|
|
|
}
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_ssl_connect_realcb(F, RB_OK, data);
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
static const char *
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_strerror(const int err)
|
2016-09-10 08:51:09 +00:00
|
|
|
{
|
|
|
|
static char errbuf[512];
|
|
|
|
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
char mbed_errbuf[512];
|
|
|
|
mbedtls_strerror(err, mbed_errbuf, sizeof mbed_errbuf);
|
2016-09-17 17:19:31 +00:00
|
|
|
(void) snprintf(errbuf, sizeof errbuf, "-0x%x: %s", -err, mbed_errbuf);
|
2016-09-10 08:51:09 +00:00
|
|
|
#else
|
|
|
|
(void) snprintf(errbuf, sizeof errbuf, "-0x%x", -err);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return errbuf;
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
static int
|
|
|
|
rb_make_certfp(const mbedtls_x509_crt *const peer_cert, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
size_t hashlen = 0;
|
2016-09-17 17:19:31 +00:00
|
|
|
mbedtls_md_type_t md_type;
|
2016-09-04 03:50:29 +00:00
|
|
|
bool spki = false;
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
switch(method)
|
|
|
|
{
|
|
|
|
case RB_SSL_CERTFP_METH_CERT_SHA1:
|
|
|
|
md_type = MBEDTLS_MD_SHA1;
|
|
|
|
hashlen = RB_SSL_CERTFP_LEN_SHA1;
|
|
|
|
break;
|
|
|
|
case RB_SSL_CERTFP_METH_SPKI_SHA256:
|
|
|
|
spki = true;
|
|
|
|
case RB_SSL_CERTFP_METH_CERT_SHA256:
|
|
|
|
md_type = MBEDTLS_MD_SHA256;
|
|
|
|
hashlen = RB_SSL_CERTFP_LEN_SHA256;
|
|
|
|
break;
|
|
|
|
case RB_SSL_CERTFP_METH_SPKI_SHA512:
|
|
|
|
spki = true;
|
|
|
|
case RB_SSL_CERTFP_METH_CERT_SHA512:
|
|
|
|
md_type = MBEDTLS_MD_SHA512;
|
|
|
|
hashlen = RB_SSL_CERTFP_LEN_SHA512;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const mbedtls_md_info_t *const md_info = mbedtls_md_info_from_type(md_type);
|
|
|
|
if(md_info == NULL)
|
|
|
|
return 0;
|
2015-12-04 04:32:33 +00:00
|
|
|
|
|
|
|
int ret;
|
2016-09-04 03:50:29 +00:00
|
|
|
void* data = peer_cert->raw.p;
|
|
|
|
size_t datalen = peer_cert->raw.len;
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
if(spki)
|
2016-05-05 03:31:32 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
unsigned char der_pubkey[8192];
|
|
|
|
if((ret = mbedtls_pk_write_pubkey_der((mbedtls_pk_context *)&peer_cert->pk,
|
|
|
|
der_pubkey, sizeof der_pubkey)) < 0)
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("rb_get_ssl_certfp: pk_write_pubkey_der: %s", rb_ssl_strerror(ret));
|
2016-09-04 03:50:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
data = der_pubkey + (sizeof(der_pubkey) - (size_t)ret);
|
|
|
|
datalen = (size_t)ret;
|
2016-05-05 03:31:32 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
if((ret = mbedtls_md(md_info, data, datalen, certfp)) != 0)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("rb_get_ssl_certfp: mbedtls_md: %s", rb_ssl_strerror(ret));
|
2016-09-04 03:50:29 +00:00
|
|
|
return 0;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
return (int) hashlen;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
/*
|
|
|
|
* External MbedTLS-specific code
|
|
|
|
*/
|
2015-12-04 04:32:33 +00:00
|
|
|
|
|
|
|
void
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_ssl_shutdown(rb_fde_t *const F)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
if(F == NULL || F->ssl == NULL)
|
|
|
|
return;
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
for(int i = 0; i < 4; i++)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-10 08:51:09 +00:00
|
|
|
int ret = mbedtls_ssl_close_notify(SSL_P(F));
|
2016-09-04 03:50:29 +00:00
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
if(ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE)
|
|
|
|
break;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
mbedtls_ssl_free(SSL_P(F));
|
|
|
|
rb_mbedtls_cfg_decref(SSL_C(F));
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_free(F->ssl);
|
2016-09-04 03:50:29 +00:00
|
|
|
F->ssl = NULL;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
rb_init_ssl(void)
|
|
|
|
{
|
2016-05-05 03:31:32 +00:00
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg_ctx);
|
|
|
|
mbedtls_entropy_init(&entropy_ctx);
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
if((ret = mbedtls_ctr_drbg_seed(&ctr_drbg_ctx, mbedtls_entropy_func, &entropy_ctx,
|
2016-09-04 03:50:29 +00:00
|
|
|
(const unsigned char *)rb_mbedtls_personal_str, sizeof(rb_mbedtls_personal_str))) != 0)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: ctr_drbg_seed: %s", __func__, rb_ssl_strerror(ret));
|
2015-12-04 04:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
if((ret = mbedtls_x509_crt_parse_der(&dummy_ca_ctx, rb_mbedtls_dummy_ca_certificate,
|
|
|
|
sizeof(rb_mbedtls_dummy_ca_certificate))) != 0)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: x509_crt_parse_der (Dummy CA): %s", __func__, rb_ssl_strerror(ret));
|
2015-12-04 04:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: MbedTLS backend initialised", __func__);
|
2015-12-04 04:32:33 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_setup_ssl_server(const char *const certfile, const char *keyfile,
|
|
|
|
const char *const dhfile, const char *const cipherlist)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-05-05 03:31:32 +00:00
|
|
|
if(certfile == NULL)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: no certificate file specified", __func__);
|
2015-12-04 04:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
if(keyfile == NULL)
|
|
|
|
keyfile = certfile;
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_mbedtls_cfg_context *const newcfg = rb_mbedtls_cfg_new();
|
|
|
|
|
|
|
|
if(newcfg == NULL)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: rb_mbedtls_cfg_new: allocation failed", __func__);
|
2015-12-04 04:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
int ret;
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
if((ret = mbedtls_x509_crt_parse_file(&newcfg->crt, certfile)) != 0)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: x509_crt_parse_file ('%s'): %s", __func__, certfile, rb_ssl_strerror(ret));
|
2016-05-05 03:31:32 +00:00
|
|
|
rb_mbedtls_cfg_decref(newcfg);
|
2015-12-04 04:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-05-05 03:31:32 +00:00
|
|
|
if((ret = mbedtls_pk_parse_keyfile(&newcfg->key, keyfile, NULL)) != 0)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: pk_parse_keyfile ('%s'): %s", __func__, keyfile, rb_ssl_strerror(ret));
|
|
|
|
rb_mbedtls_cfg_decref(newcfg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if((ret = mbedtls_ssl_conf_own_cert(&newcfg->server_cfg, &newcfg->crt, &newcfg->key)) != 0)
|
|
|
|
{
|
|
|
|
rb_lib_log("%s: ssl_conf_own_cert (server): %s", __func__, rb_ssl_strerror(ret));
|
|
|
|
rb_mbedtls_cfg_decref(newcfg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if((ret = mbedtls_ssl_conf_own_cert(&newcfg->client_cfg, &newcfg->crt, &newcfg->key)) != 0)
|
|
|
|
{
|
|
|
|
rb_lib_log("%s: ssl_conf_own_cert (client): %s", __func__, rb_ssl_strerror(ret));
|
2016-05-05 03:31:32 +00:00
|
|
|
rb_mbedtls_cfg_decref(newcfg);
|
2015-12-04 04:32:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
/* Absense of DH parameters does not matter with mbedTLS, as it comes with its own defaults
|
|
|
|
Thus, clients can still use DHE- ciphersuites, just over a weaker, common DH group
|
|
|
|
So, we do not consider failure to parse DH parameters as fatal */
|
|
|
|
if(dhfile == NULL)
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: no DH parameters file specified", __func__);
|
2016-05-05 03:31:32 +00:00
|
|
|
}
|
|
|
|
else
|
2015-12-05 04:42:10 +00:00
|
|
|
{
|
2016-05-05 03:31:32 +00:00
|
|
|
if((ret = mbedtls_dhm_parse_dhmfile(&newcfg->dhp, dhfile)) != 0)
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: dhm_parse_dhmfile ('%s'): %s", __func__, dhfile, rb_ssl_strerror(ret));
|
2016-05-05 03:31:32 +00:00
|
|
|
}
|
|
|
|
else if((ret = mbedtls_ssl_conf_dh_param_ctx(&newcfg->server_cfg, &newcfg->dhp)) != 0)
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: ssl_conf_dh_param_ctx: %s", __func__, rb_ssl_strerror(ret));
|
2016-05-05 03:31:32 +00:00
|
|
|
}
|
2015-12-05 04:42:10 +00:00
|
|
|
}
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
const int *rb_ciphersuites = newcfg->suites;
|
|
|
|
size_t suites_count = 0;
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
if(cipherlist != NULL)
|
|
|
|
{
|
|
|
|
// The cipherlist is (const char *) -- we should not modify it
|
|
|
|
char *const cipherlist_dup = strdup(cipherlist);
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
if(cipherlist_dup != NULL)
|
|
|
|
{
|
|
|
|
char *cipher_str = cipherlist_dup;
|
|
|
|
char *cipher_idx;
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
// Arbitrary, but the same separator as OpenSSL uses
|
|
|
|
cipher_idx = strchr(cipher_str, ':');
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
// This could legitimately be NULL (last ciphersuite in the list)
|
|
|
|
if(cipher_idx != NULL)
|
|
|
|
*cipher_idx = '\0';
|
|
|
|
|
|
|
|
size_t cipher_len = strlen(cipher_str);
|
|
|
|
int cipher_idn = 0;
|
|
|
|
|
|
|
|
// All MbedTLS ciphersuite names begin with these 4 characters
|
|
|
|
if(cipher_len > 4 && strncmp(cipher_str, "TLS-", 4) == 0)
|
|
|
|
cipher_idn = mbedtls_ssl_get_ciphersuite_id(cipher_str);
|
|
|
|
|
|
|
|
// Prevent the same ciphersuite being added multiple times
|
|
|
|
for(size_t x = 0; cipher_idn != 0 && newcfg->suites[x] != 0; x++)
|
|
|
|
if(newcfg->suites[x] == cipher_idn)
|
|
|
|
cipher_idn = 0;
|
|
|
|
|
|
|
|
// Add the suite to the list
|
|
|
|
if(cipher_idn != 0)
|
|
|
|
newcfg->suites[suites_count++] = cipher_idn;
|
|
|
|
|
|
|
|
// Advance the string to the next entry
|
|
|
|
if(cipher_idx)
|
|
|
|
cipher_str = cipher_idx + 1;
|
|
|
|
|
|
|
|
} while(cipher_idx && suites_count < RB_MAX_CIPHERSUITES);
|
|
|
|
|
|
|
|
if(suites_count == 0)
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: Ciphersuites provided, but could not parse any", __func__);
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
free(cipherlist_dup);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: strdup: %s", __func__, strerror(errno));
|
2016-09-04 03:50:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: No ciphersuite list provided", __func__);
|
2016-09-04 03:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(suites_count == 0)
|
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: Using default ciphersuites", __func__);
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
rb_ciphersuites = rb_mbedtls_ciphersuites;
|
|
|
|
suites_count = (sizeof(rb_mbedtls_ciphersuites) / sizeof(rb_mbedtls_ciphersuites[0])) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_ciphersuites(&newcfg->server_cfg, rb_ciphersuites);
|
|
|
|
mbedtls_ssl_conf_ciphersuites(&newcfg->client_cfg, rb_ciphersuites);
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: Configured %zu ciphersuites", __func__, suites_count);
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
rb_mbedtls_cfg_decref(rb_mbedtls_cfg);
|
|
|
|
rb_mbedtls_cfg = newcfg;
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: TLS configuration successful", __func__);
|
2016-09-04 03:50:29 +00:00
|
|
|
return 1;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
int
|
|
|
|
rb_init_prng(const char *const path, prng_seed_t seed_type)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: Skipping PRNG initialisation; not required by MbedTLS backend", __func__);
|
2016-09-04 03:50:29 +00:00
|
|
|
return 1;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
int
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_get_random(void *const buf, const size_t length)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
int ret;
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
if((ret = mbedtls_ctr_drbg_random(&ctr_drbg_ctx, buf, length)) != 0)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_lib_log("%s: ctr_drbg_random: %s", __func__, rb_ssl_strerror(ret));
|
2016-09-04 03:50:29 +00:00
|
|
|
return 0;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
return 1;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
const char *
|
|
|
|
rb_get_ssl_strerror(rb_fde_t *const F)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
const int err = (int) F->ssl_errno;
|
|
|
|
return rb_ssl_strerror(-err);
|
2016-09-04 03:50:29 +00:00
|
|
|
}
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
int
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_get_ssl_certfp(rb_fde_t *const F, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
|
|
|
const mbedtls_x509_crt *const peer_cert = mbedtls_ssl_get_peer_cert(SSL_P(F));
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
if(peer_cert == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return rb_make_certfp(peer_cert, certfp, method);
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
int
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_get_ssl_certfp_file(const char *const filename, uint8_t certfp[const RB_SSL_CERTFP_LEN], const int method)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
mbedtls_x509_crt cert;
|
2016-09-17 17:19:31 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
mbedtls_x509_crt_init(&cert);
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
const int ret = mbedtls_x509_crt_parse_file(&cert, filename);
|
|
|
|
|
|
|
|
if(ret != 0)
|
2016-09-04 03:50:29 +00:00
|
|
|
return -1;
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
const int len = rb_make_certfp(&cert, certfp, method);
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
mbedtls_x509_crt_free(&cert);
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
return len;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
void
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_get_ssl_info(char *const buf, const size_t len)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
char version_str[512];
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
mbedtls_version_get_string(version_str);
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
(void) snprintf(buf, len, "ARM mbedTLS: compiled (v%s), library (v%s)",
|
|
|
|
MBEDTLS_VERSION_STRING, version_str);
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
const char *
|
|
|
|
rb_ssl_get_cipher(rb_fde_t *const F)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
if(F == NULL || F->ssl == NULL)
|
2016-09-04 03:50:29 +00:00
|
|
|
return NULL;
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
static char buf[512];
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
const char *const version = mbedtls_ssl_get_version(SSL_P(F));
|
|
|
|
const char *const cipher = mbedtls_ssl_get_ciphersuite(SSL_P(F));
|
|
|
|
|
|
|
|
(void) snprintf(buf, sizeof buf, "%s, %s", version, cipher);
|
|
|
|
|
|
|
|
return buf;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
ssize_t
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_read(rb_fde_t *const F, void *const buf, const size_t count)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
lrb_assert(F != NULL);
|
|
|
|
lrb_assert(F->ssl != NULL);
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
const int ret = mbedtls_ssl_read(SSL_P(F), buf, count);
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-09 01:46:20 +00:00
|
|
|
if(ret >= 0)
|
2016-09-17 17:19:31 +00:00
|
|
|
return (ssize_t) ret;
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
switch(ret)
|
2015-05-27 21:46:46 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
case MBEDTLS_ERR_SSL_WANT_READ:
|
|
|
|
errno = EAGAIN;
|
|
|
|
return RB_RW_SSL_NEED_READ;
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_WRITE:
|
|
|
|
errno = EAGAIN;
|
|
|
|
return RB_RW_SSL_NEED_WRITE;
|
|
|
|
default:
|
|
|
|
errno = EIO;
|
2016-09-17 17:19:31 +00:00
|
|
|
F->ssl_errno = (unsigned long) -ret;
|
2016-09-04 03:50:29 +00:00
|
|
|
return RB_RW_SSL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
ssize_t
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_write(rb_fde_t *const F, const void *const buf, const size_t count)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
|
|
|
lrb_assert(F != NULL);
|
|
|
|
lrb_assert(F->ssl != NULL);
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
const int ret = mbedtls_ssl_write(SSL_P(F), buf, count);
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-09 01:46:20 +00:00
|
|
|
if(ret >= 0)
|
2016-09-17 17:19:31 +00:00
|
|
|
return (ssize_t) ret;
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
switch(ret)
|
|
|
|
{
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_READ:
|
|
|
|
errno = EAGAIN;
|
|
|
|
return RB_RW_SSL_NEED_READ;
|
|
|
|
case MBEDTLS_ERR_SSL_WANT_WRITE:
|
|
|
|
errno = EAGAIN;
|
|
|
|
return RB_RW_SSL_NEED_WRITE;
|
2015-05-27 21:46:46 +00:00
|
|
|
default:
|
2016-09-04 03:50:29 +00:00
|
|
|
errno = EIO;
|
2016-09-17 17:19:31 +00:00
|
|
|
F->ssl_errno = (unsigned long) -ret;
|
2016-09-04 03:50:29 +00:00
|
|
|
return RB_RW_SSL_ERROR;
|
2015-05-27 21:46:46 +00:00
|
|
|
}
|
2016-09-04 03:50:29 +00:00
|
|
|
}
|
2015-05-27 21:46:46 +00:00
|
|
|
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-04-23 21:51:05 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
/*
|
|
|
|
* Internal library-agnostic code
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_connect_realcb(rb_fde_t *const F, const int status, struct ssl_connect *const sconn)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
lrb_assert(F != NULL);
|
2016-09-04 03:50:29 +00:00
|
|
|
lrb_assert(F->connect != NULL);
|
|
|
|
|
|
|
|
F->connect->callback = sconn->callback;
|
|
|
|
F->connect->data = sconn->data;
|
2016-09-17 17:19:31 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_connect_callback(F, status);
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_free(sconn);
|
2016-09-04 03:50:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_ssl_timeout_cb(rb_fde_t *const F, void *const data)
|
|
|
|
{
|
|
|
|
lrb_assert(F->accept != NULL);
|
|
|
|
lrb_assert(F->accept->callback != NULL);
|
|
|
|
|
|
|
|
F->accept->callback(F, RB_ERR_TIMEOUT, NULL, 0, F->accept->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_ssl_tryconn_timeout_cb(rb_fde_t *const F, void *const data)
|
|
|
|
{
|
|
|
|
rb_ssl_connect_realcb(F, RB_ERR_TIMEOUT, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_tryconn(rb_fde_t *const F, const int status, void *const data)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
|
|
|
lrb_assert(F != NULL);
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
struct ssl_connect *const sconn = data;
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
if(status != RB_OK)
|
2016-05-05 03:31:32 +00:00
|
|
|
{
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_connect_realcb(F, status, sconn);
|
2016-09-04 03:50:29 +00:00
|
|
|
return;
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
F->type |= RB_FD_SSL;
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
|
|
|
|
rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT);
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_connect_common(F, sconn);
|
2016-04-26 19:21:23 +00:00
|
|
|
}
|
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* External library-agnostic code
|
|
|
|
*/
|
2015-12-04 04:32:33 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
rb_supports_ssl(void)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
unsigned int
|
|
|
|
rb_ssl_handshake_count(rb_fde_t *const F)
|
|
|
|
{
|
|
|
|
return F->handshake_count;
|
|
|
|
}
|
|
|
|
|
2015-12-04 04:32:33 +00:00
|
|
|
void
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_ssl_clear_handshake_count(rb_fde_t *const F)
|
2015-12-04 04:32:33 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
F->handshake_count = 0;
|
|
|
|
}
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
void
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_start_accepted(rb_fde_t *const F, ACCB *const cb, void *const data, const int timeout)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
|
|
|
F->type |= RB_FD_SSL;
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
F->accept = rb_malloc(sizeof(struct acceptdata));
|
2016-09-04 03:50:29 +00:00
|
|
|
F->accept->callback = cb;
|
|
|
|
F->accept->data = data;
|
|
|
|
F->accept->addrlen = 0;
|
|
|
|
(void) memset(&F->accept->S, 0x00, sizeof F->accept->S);
|
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_settimeout(F, timeout, rb_ssl_timeout_cb, NULL);
|
|
|
|
rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_IN);
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_ssl_accept_common(F, NULL);
|
2015-12-04 04:32:33 +00:00
|
|
|
}
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
void
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_accept_setup(rb_fde_t *const srv_F, rb_fde_t *const cli_F, struct sockaddr *const st, const int addrlen)
|
2015-12-11 14:32:02 +00:00
|
|
|
{
|
2016-09-04 03:50:29 +00:00
|
|
|
cli_F->type |= RB_FD_SSL;
|
2016-08-20 03:16:33 +00:00
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
cli_F->accept = rb_malloc(sizeof(struct acceptdata));
|
2016-09-04 03:50:29 +00:00
|
|
|
cli_F->accept->callback = srv_F->accept->callback;
|
|
|
|
cli_F->accept->data = srv_F->accept->data;
|
2016-09-17 17:19:31 +00:00
|
|
|
cli_F->accept->addrlen = (rb_socklen_t) addrlen;
|
2016-09-04 03:50:29 +00:00
|
|
|
(void) memset(&cli_F->accept->S, 0x00, sizeof cli_F->accept->S);
|
2016-09-17 17:19:31 +00:00
|
|
|
(void) memcpy(&cli_F->accept->S, st, (size_t) addrlen);
|
2016-08-20 03:16:33 +00:00
|
|
|
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_settimeout(cli_F, 10, rb_ssl_timeout_cb, NULL);
|
|
|
|
rb_ssl_init_fd(cli_F, RB_FD_TLS_DIRECTION_IN);
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_ssl_accept_common(cli_F, NULL);
|
|
|
|
}
|
2016-08-20 03:16:33 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
int
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_listen(rb_fde_t *const F, const int backlog, const int defer_accept)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
|
|
|
int result = rb_listen(F, backlog, defer_accept);
|
2016-09-17 17:19:31 +00:00
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
F->type = RB_FD_SOCKET | RB_FD_LISTEN | RB_FD_SSL;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rb_connect_tcp_ssl(rb_fde_t *const F, struct sockaddr *const dest, struct sockaddr *const clocal,
|
2016-09-17 17:19:31 +00:00
|
|
|
CNCB *const callback, void *const data, const int timeout)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
|
|
|
if(F == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct ssl_connect *const sconn = rb_malloc(sizeof *sconn);
|
|
|
|
sconn->data = data;
|
|
|
|
sconn->callback = callback;
|
|
|
|
sconn->timeout = timeout;
|
|
|
|
|
|
|
|
rb_connect_tcp(F, dest, clocal, rb_ssl_tryconn, sconn, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_start_connected(rb_fde_t *const F, CNCB *const callback, void *const data, const int timeout)
|
2016-09-04 03:50:29 +00:00
|
|
|
{
|
|
|
|
if(F == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct ssl_connect *const sconn = rb_malloc(sizeof *sconn);
|
|
|
|
sconn->data = data;
|
|
|
|
sconn->callback = callback;
|
|
|
|
sconn->timeout = timeout;
|
|
|
|
|
2016-09-17 17:19:31 +00:00
|
|
|
F->connect = rb_malloc(sizeof(struct conndata));
|
|
|
|
F->connect->callback = callback;
|
|
|
|
F->connect->data = data;
|
|
|
|
|
|
|
|
F->type |= RB_FD_SSL;
|
|
|
|
|
2016-09-04 03:50:29 +00:00
|
|
|
rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
|
2016-09-10 08:51:09 +00:00
|
|
|
rb_ssl_init_fd(F, RB_FD_TLS_DIRECTION_OUT);
|
2016-09-17 17:19:31 +00:00
|
|
|
rb_ssl_connect_common(F, sconn);
|
2015-12-11 14:32:02 +00:00
|
|
|
}
|
2015-12-04 04:32:33 +00:00
|
|
|
|
2016-05-05 03:31:32 +00:00
|
|
|
#endif /* HAVE_MBEDTLS */
|