2015-10-19 08:25:34 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 - 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
|
2020-01-16 02:17:04 -05:00
|
|
|
* Copyright (C) 2012 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2015-10-19 08:25:34 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2015-10-19 08:25:34 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
2016-04-25 18:28:40 -04:00
|
|
|
* Source file for all mbedTLS-specific code for the TLS/SSL layer. No code
|
2015-10-19 08:25:34 -04:00
|
|
|
* but vtls.c should ever call or use these functions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
|
|
|
#ifdef USE_MBEDTLS
|
|
|
|
|
2016-11-04 06:13:16 -04:00
|
|
|
#include <mbedtls/version.h>
|
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02040000
|
2016-10-26 17:38:04 -04:00
|
|
|
#include <mbedtls/net_sockets.h>
|
2016-11-04 06:13:16 -04:00
|
|
|
#else
|
|
|
|
#include <mbedtls/net.h>
|
|
|
|
#endif
|
2015-10-19 08:25:34 -04:00
|
|
|
#include <mbedtls/ssl.h>
|
|
|
|
#include <mbedtls/certs.h>
|
|
|
|
#include <mbedtls/x509.h>
|
|
|
|
|
|
|
|
#include <mbedtls/error.h>
|
|
|
|
#include <mbedtls/entropy.h>
|
|
|
|
#include <mbedtls/ctr_drbg.h>
|
2016-03-19 17:37:21 -04:00
|
|
|
#include <mbedtls/sha256.h>
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "mbedtls.h"
|
|
|
|
#include "vtls.h"
|
|
|
|
#include "parsedate.h"
|
|
|
|
#include "connect.h" /* for the connect timeout */
|
|
|
|
#include "select.h"
|
2019-04-30 05:14:38 -04:00
|
|
|
#include "multiif.h"
|
2020-01-16 02:17:04 -05:00
|
|
|
#include "mbedtls_threadlock.h"
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-05-24 06:14:18 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
2015-10-19 08:25:34 -04:00
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
vtls: encapsulate SSL backend-specific data
So far, all of the SSL backends' private data has been declared as
part of the ssl_connect_data struct, in one big #if .. #elif .. #endif
block.
This can only work as long as the SSL backend is a compile-time option,
something we want to change in the next commits.
Therefore, let's encapsulate the exact data needed by each SSL backend
into a private struct, and let's avoid bleeding any SSL backend-specific
information into urldata.h. This is also necessary to allow multiple SSL
backends to be compiled in at the same time, as e.g. OpenSSL's and
CyaSSL's headers cannot be included in the same .c file.
To avoid too many malloc() calls, we simply append the private structs
to the connectdata struct in allocate_conn().
This requires us to take extra care of alignment issues: struct fields
often need to be aligned on certain boundaries e.g. 32-bit values need to
be stored at addresses that divide evenly by 4 (= 32 bit / 8
bit-per-byte).
We do that by assuming that no SSL backend's private data contains any
fields that need to be aligned on boundaries larger than `long long`
(typically 64-bit) would need. Under this assumption, we simply add a
dummy field of type `long long` to the `struct connectdata` struct. This
field will never be accessed but acts as a placeholder for the four
instances of ssl_backend_data instead. the size of each ssl_backend_data
struct is stored in the SSL backend-specific metadata, to allow
allocate_conn() to know how much extra space to allocate, and how to
initialize the ssl[sockindex]->backend and proxy_ssl[sockindex]->backend
pointers.
This would appear to be a little complicated at first, but is really
necessary to encapsulate the private data of each SSL backend correctly.
And we need to encapsulate thusly if we ever want to allow selecting
CyaSSL and OpenSSL at runtime, as their headers cannot be included within
the same .c file (there are just too many conflicting definitions and
declarations for that).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2017-07-28 16:09:35 -04:00
|
|
|
struct ssl_backend_data {
|
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_ssl_context ssl;
|
|
|
|
int server_fd;
|
|
|
|
mbedtls_x509_crt cacert;
|
|
|
|
mbedtls_x509_crt clicert;
|
|
|
|
mbedtls_x509_crl crl;
|
|
|
|
mbedtls_pk_context pk;
|
|
|
|
mbedtls_ssl_config config;
|
|
|
|
const char *protocols[3];
|
|
|
|
};
|
|
|
|
|
2015-10-19 08:25:34 -04:00
|
|
|
/* apply threading? */
|
|
|
|
#if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
|
|
|
|
#define THREADING_SUPPORT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(THREADING_SUPPORT)
|
2017-05-29 17:38:33 -04:00
|
|
|
static mbedtls_entropy_context ts_entropy;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-04-07 09:16:01 -04:00
|
|
|
static int entropy_init_initialized = 0;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
/* start of entropy_init_mutex() */
|
|
|
|
static void entropy_init_mutex(mbedtls_entropy_context *ctx)
|
|
|
|
{
|
|
|
|
/* lock 0 = entropy_init_mutex() */
|
2020-01-16 02:17:04 -05:00
|
|
|
Curl_mbedtlsthreadlock_lock_function(0);
|
2015-10-19 08:25:34 -04:00
|
|
|
if(entropy_init_initialized == 0) {
|
|
|
|
mbedtls_entropy_init(ctx);
|
|
|
|
entropy_init_initialized = 1;
|
|
|
|
}
|
2020-01-16 02:17:04 -05:00
|
|
|
Curl_mbedtlsthreadlock_unlock_function(0);
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
/* end of entropy_init_mutex() */
|
|
|
|
|
|
|
|
/* start of entropy_func_mutex() */
|
|
|
|
static int entropy_func_mutex(void *data, unsigned char *output, size_t len)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
/* lock 1 = entropy_func_mutex() */
|
2020-01-16 02:17:04 -05:00
|
|
|
Curl_mbedtlsthreadlock_lock_function(1);
|
2015-10-19 08:25:34 -04:00
|
|
|
ret = mbedtls_entropy_func(data, output, len);
|
2020-01-16 02:17:04 -05:00
|
|
|
Curl_mbedtlsthreadlock_unlock_function(1);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/* end of entropy_func_mutex() */
|
|
|
|
|
|
|
|
#endif /* THREADING_SUPPORT */
|
|
|
|
|
|
|
|
/* Define this to enable lots of debugging for mbedTLS */
|
|
|
|
#undef MBEDTLS_DEBUG
|
|
|
|
|
|
|
|
#ifdef MBEDTLS_DEBUG
|
2016-04-07 09:58:11 -04:00
|
|
|
static void mbed_debug(void *context, int level, const char *f_name,
|
|
|
|
int line_nb, const char *line)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = NULL;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
if(!context)
|
|
|
|
return;
|
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
data = (struct Curl_easy *)context;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
infof(data, "%s", line);
|
|
|
|
(void) level;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ALPN for http2? */
|
|
|
|
#ifdef USE_NGHTTP2
|
|
|
|
# undef HAS_ALPN
|
|
|
|
# ifdef MBEDTLS_SSL_ALPN
|
|
|
|
# define HAS_ALPN
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-10-21 03:30:31 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* profile
|
|
|
|
*/
|
2017-06-02 16:05:01 -04:00
|
|
|
static const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_fr =
|
2015-10-21 03:30:31 -04:00
|
|
|
{
|
2016-02-10 04:46:57 -05:00
|
|
|
/* Hashes from SHA-1 and above */
|
2016-04-03 14:28:34 -04:00
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
|
|
|
|
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
|
2016-02-10 04:46:57 -05:00
|
|
|
0xFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFF, /* Any curve */
|
|
|
|
1024, /* RSA min key len */
|
2015-10-21 03:30:31 -04:00
|
|
|
};
|
|
|
|
|
2016-01-06 01:00:11 -05:00
|
|
|
/* See https://tls.mbed.org/discussions/generic/
|
|
|
|
howto-determine-exact-buffer-len-for-mbedtls_pk_write_pubkey_der
|
|
|
|
*/
|
|
|
|
#define RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE)
|
|
|
|
#define ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES)
|
|
|
|
|
|
|
|
#define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
|
2016-01-18 03:48:10 -05:00
|
|
|
RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES)
|
2016-01-06 01:00:11 -05:00
|
|
|
|
2016-04-07 09:16:01 -04:00
|
|
|
static Curl_recv mbed_recv;
|
|
|
|
static Curl_send mbed_send;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-12-13 15:10:00 -05:00
|
|
|
static CURLcode mbedtls_version_from_curl(int *mbedver, long version)
|
|
|
|
{
|
2017-03-11 16:09:38 -05:00
|
|
|
switch(version) {
|
2016-12-13 15:10:00 -05:00
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
|
|
|
*mbedver = MBEDTLS_SSL_MINOR_VERSION_1;
|
|
|
|
return CURLE_OK;
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
|
|
|
*mbedver = MBEDTLS_SSL_MINOR_VERSION_2;
|
|
|
|
return CURLE_OK;
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
|
|
|
*mbedver = MBEDTLS_SSL_MINOR_VERSION_3;
|
|
|
|
return CURLE_OK;
|
|
|
|
case CURL_SSLVERSION_TLSv1_3:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
|
|
|
set_ssl_version_min_max(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
|
|
|
struct Curl_easy *data = conn->data;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2016-12-13 15:10:00 -05:00
|
|
|
int mbedtls_ver_min = MBEDTLS_SSL_MINOR_VERSION_1;
|
|
|
|
int mbedtls_ver_max = MBEDTLS_SSL_MINOR_VERSION_1;
|
|
|
|
long ssl_version = SSL_CONN_CONFIG(version);
|
|
|
|
long ssl_version_max = SSL_CONN_CONFIG(version_max);
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
switch(ssl_version) {
|
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
|
|
|
ssl_version = CURL_SSLVERSION_TLSv1_0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(ssl_version_max) {
|
|
|
|
case CURL_SSLVERSION_MAX_NONE:
|
|
|
|
case CURL_SSLVERSION_MAX_DEFAULT:
|
|
|
|
ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = mbedtls_version_from_curl(&mbedtls_ver_min, ssl_version);
|
|
|
|
if(result) {
|
|
|
|
failf(data, "unsupported min version passed via CURLOPT_SSLVERSION");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result = mbedtls_version_from_curl(&mbedtls_ver_max, ssl_version_max >> 16);
|
|
|
|
if(result) {
|
|
|
|
failf(data, "unsupported max version passed via CURLOPT_SSLVERSION");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
2016-12-13 15:10:00 -05:00
|
|
|
mbedtls_ver_min);
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_max_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
2016-12-13 15:10:00 -05:00
|
|
|
mbedtls_ver_max);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-19 08:25:34 -04:00
|
|
|
static CURLcode
|
2016-04-07 09:16:01 -04:00
|
|
|
mbed_connect_step1(struct connectdata *conn,
|
|
|
|
int sockindex)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2020-05-13 06:56:53 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2016-11-21 05:01:25 -05:00
|
|
|
const char * const ssl_cafile = SSL_CONN_CONFIG(CAfile);
|
|
|
|
const bool verifypeer = SSL_CONN_CONFIG(verifypeer);
|
|
|
|
const char * const ssl_capath = SSL_CONN_CONFIG(CApath);
|
|
|
|
char * const ssl_cert = SSL_SET_OPTION(cert);
|
|
|
|
const char * const ssl_crlfile = SSL_SET_OPTION(CRLfile);
|
|
|
|
const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
|
|
|
|
conn->host.name;
|
|
|
|
const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port;
|
2015-10-19 08:25:34 -04:00
|
|
|
int ret = -1;
|
|
|
|
char errorbuf[128];
|
2017-09-09 17:09:06 -04:00
|
|
|
errorbuf[0] = 0;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
/* mbedTLS only supports SSLv3 and TLSv1 */
|
2016-11-21 05:01:25 -05:00
|
|
|
if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2) {
|
2015-10-19 08:25:34 -04:00
|
|
|
failf(data, "mbedTLS does not support SSLv2");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef THREADING_SUPPORT
|
2017-05-29 17:38:33 -04:00
|
|
|
entropy_init_mutex(&ts_entropy);
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ctr_drbg_init(&backend->ctr_drbg);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, entropy_func_mutex,
|
2017-05-29 17:38:33 -04:00
|
|
|
&ts_entropy, NULL, 0);
|
2015-10-23 18:46:03 -04:00
|
|
|
if(ret) {
|
2015-10-19 08:25:34 -04:00
|
|
|
#ifdef MBEDTLS_ERROR_C
|
2015-10-23 18:46:03 -04:00
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
2015-10-19 08:25:34 -04:00
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
2015-10-23 18:46:03 -04:00
|
|
|
failf(data, "Failed - mbedTLS: ctr_drbg_init returned (-0x%04X) %s\n",
|
|
|
|
-ret, errorbuf);
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
#else
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_entropy_init(&backend->entropy);
|
|
|
|
mbedtls_ctr_drbg_init(&backend->ctr_drbg);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, mbedtls_entropy_func,
|
|
|
|
&backend->entropy, NULL, 0);
|
2015-10-19 08:25:34 -04:00
|
|
|
if(ret) {
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
|
|
|
failf(data, "Failed - mbedTLS: ctr_drbg_init returned (-0x%04X) %s\n",
|
|
|
|
-ret, errorbuf);
|
|
|
|
}
|
|
|
|
#endif /* THREADING_SUPPORT */
|
|
|
|
|
|
|
|
/* Load the trusted CA */
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_x509_crt_init(&backend->cacert);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(ssl_cafile) {
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_x509_crt_parse_file(&backend->cacert, ssl_cafile);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
if(ret<0) {
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
|
|
|
failf(data, "Error reading ca cert file %s - mbedTLS: (-0x%04X) %s",
|
2016-11-21 05:01:25 -05:00
|
|
|
ssl_cafile, -ret, errorbuf);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(verifypeer)
|
2015-10-19 08:25:34 -04:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(ssl_capath) {
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_x509_crt_parse_path(&backend->cacert, ssl_capath);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
if(ret<0) {
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
|
|
|
failf(data, "Error reading ca cert path %s - mbedTLS: (-0x%04X) %s",
|
2016-11-21 05:01:25 -05:00
|
|
|
ssl_capath, -ret, errorbuf);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(verifypeer)
|
2015-10-19 08:25:34 -04:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the client certificate */
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_x509_crt_init(&backend->clicert);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(ssl_cert) {
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_x509_crt_parse_file(&backend->clicert, ssl_cert);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
|
|
|
failf(data, "Error reading client cert file %s - mbedTLS: (-0x%04X) %s",
|
2016-11-21 05:01:25 -05:00
|
|
|
ssl_cert, -ret, errorbuf);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
return CURLE_SSL_CERTPROBLEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the client private key */
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_pk_init(&backend->pk);
|
2016-02-01 17:13:10 -05:00
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(SSL_SET_OPTION(key)) {
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_pk_parse_keyfile(&backend->pk, SSL_SET_OPTION(key),
|
2016-11-21 05:01:25 -05:00
|
|
|
SSL_SET_OPTION(key_passwd));
|
2020-03-18 18:22:01 -04:00
|
|
|
if(ret == 0 && !(mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_RSA) ||
|
|
|
|
mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_ECKEY)))
|
2015-10-19 08:25:34 -04:00
|
|
|
ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
|
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
|
|
|
failf(data, "Error reading private key %s - mbedTLS: (-0x%04X) %s",
|
2016-11-21 05:01:25 -05:00
|
|
|
SSL_SET_OPTION(key), -ret, errorbuf);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
return CURLE_SSL_CERTPROBLEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the CRL */
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_x509_crl_init(&backend->crl);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(ssl_crlfile) {
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_x509_crl_parse_file(&backend->crl, ssl_crlfile);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
|
|
|
failf(data, "Error reading CRL file %s - mbedTLS: (-0x%04X) %s",
|
2016-11-21 05:01:25 -05:00
|
|
|
ssl_crlfile, -ret, errorbuf);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
return CURLE_SSL_CRL_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-16 16:04:49 -04:00
|
|
|
infof(data, "mbedTLS: Connecting to %s:%ld\n", hostname, port);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_config_init(&backend->config);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_init(&backend->ssl);
|
|
|
|
if(mbedtls_ssl_setup(&backend->ssl, &backend->config)) {
|
2015-10-19 08:25:34 -04:00
|
|
|
failf(data, "mbedTLS: ssl_init failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ssl_config_defaults(&backend->config,
|
2015-10-19 08:25:34 -04:00
|
|
|
MBEDTLS_SSL_IS_CLIENT,
|
|
|
|
MBEDTLS_SSL_TRANSPORT_STREAM,
|
|
|
|
MBEDTLS_SSL_PRESET_DEFAULT);
|
|
|
|
if(ret) {
|
|
|
|
failf(data, "mbedTLS: ssl_config failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-10-21 03:30:31 -04:00
|
|
|
/* new profile with RSA min key len = 1024 ... */
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_cert_profile(&backend->config,
|
2016-04-03 14:28:34 -04:00
|
|
|
&mbedtls_x509_crt_profile_fr);
|
2015-10-21 03:30:31 -04:00
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
switch(SSL_CONN_CONFIG(version)) {
|
2016-03-05 21:35:16 -05:00
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
2016-03-05 21:35:16 -05:00
|
|
|
MBEDTLS_SSL_MINOR_VERSION_1);
|
|
|
|
infof(data, "mbedTLS: Set min SSL version to TLS 1.0\n");
|
|
|
|
break;
|
2015-10-19 08:25:34 -04:00
|
|
|
case CURL_SSLVERSION_SSLv3:
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
2015-10-19 08:25:34 -04:00
|
|
|
MBEDTLS_SSL_MINOR_VERSION_0);
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_max_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
2016-03-05 21:35:16 -05:00
|
|
|
MBEDTLS_SSL_MINOR_VERSION_0);
|
|
|
|
infof(data, "mbedTLS: Set SSL version to SSLv3\n");
|
2015-10-19 08:25:34 -04:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
2016-11-07 21:51:27 -05:00
|
|
|
case CURL_SSLVERSION_TLSv1_3:
|
2016-12-13 15:10:00 -05:00
|
|
|
{
|
|
|
|
CURLcode result = set_ssl_version_min_max(conn, sockindex);
|
|
|
|
if(result != CURLE_OK)
|
|
|
|
return result;
|
|
|
|
break;
|
|
|
|
}
|
2016-03-05 21:35:16 -05:00
|
|
|
default:
|
2016-11-07 21:51:27 -05:00
|
|
|
failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
|
2016-03-05 21:35:16 -05:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_authmode(&backend->config, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_rng(&backend->config, mbedtls_ctr_drbg_random,
|
|
|
|
&backend->ctr_drbg);
|
|
|
|
mbedtls_ssl_set_bio(&backend->ssl, &conn->sock[sockindex],
|
2016-02-10 04:46:57 -05:00
|
|
|
mbedtls_net_send,
|
|
|
|
mbedtls_net_recv,
|
|
|
|
NULL /* rev_timeout() */);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_ciphersuites(&backend->config,
|
2015-10-19 08:25:34 -04:00
|
|
|
mbedtls_ssl_list_ciphersuites());
|
2016-06-12 23:47:12 -04:00
|
|
|
|
2017-05-09 09:57:19 -04:00
|
|
|
#if defined(MBEDTLS_SSL_RENEGOTIATION)
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_renegotiation(&backend->config,
|
2017-05-09 09:57:19 -04:00
|
|
|
MBEDTLS_SSL_RENEGOTIATION_ENABLED);
|
|
|
|
#endif
|
|
|
|
|
2017-01-28 14:14:54 -05:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_session_tickets(&backend->config,
|
2017-01-28 14:14:54 -05:00
|
|
|
MBEDTLS_SSL_SESSION_TICKETS_DISABLED);
|
|
|
|
#endif
|
|
|
|
|
2016-06-12 23:47:12 -04:00
|
|
|
/* Check if there's a cached ID we can/should use here! */
|
2017-03-22 01:59:49 -04:00
|
|
|
if(SSL_SET_OPTION(primary.sessionid)) {
|
2016-06-12 23:47:12 -04:00
|
|
|
void *old_session = NULL;
|
|
|
|
|
|
|
|
Curl_ssl_sessionid_lock(conn);
|
2016-11-16 12:49:15 -05:00
|
|
|
if(!Curl_ssl_getsessionid(conn, &old_session, NULL, sockindex)) {
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ssl_set_session(&backend->ssl, old_session);
|
2016-06-12 23:47:12 -04:00
|
|
|
if(ret) {
|
|
|
|
Curl_ssl_sessionid_unlock(conn);
|
|
|
|
failf(data, "mbedtls_ssl_set_session returned -0x%x", -ret);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
infof(data, "mbedTLS re-using session\n");
|
2016-04-28 02:57:12 -04:00
|
|
|
}
|
2016-06-12 23:47:12 -04:00
|
|
|
Curl_ssl_sessionid_unlock(conn);
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_ca_chain(&backend->config,
|
|
|
|
&backend->cacert,
|
|
|
|
&backend->crl);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(SSL_SET_OPTION(key)) {
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_own_cert(&backend->config,
|
|
|
|
&backend->clicert, &backend->pk);
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
2020-03-18 18:22:01 -04:00
|
|
|
if(mbedtls_ssl_set_hostname(&backend->ssl, hostname)) {
|
2016-04-24 11:52:18 -04:00
|
|
|
/* mbedtls_ssl_set_hostname() sets the name to use in CN/SAN checks *and*
|
|
|
|
the name to set in the SNI extension. So even if curl connects to a
|
|
|
|
host specified as an IP address, this function must be used. */
|
|
|
|
failf(data, "couldn't set hostname in mbedTLS");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_ALPN
|
2016-05-09 10:50:11 -04:00
|
|
|
if(conn->bits.tls_enable_alpn) {
|
2020-03-18 18:22:01 -04:00
|
|
|
const char **p = &backend->protocols[0];
|
2016-01-07 01:49:31 -05:00
|
|
|
#ifdef USE_NGHTTP2
|
|
|
|
if(data->set.httpversion >= CURL_HTTP_VERSION_2)
|
|
|
|
*p++ = NGHTTP2_PROTO_VERSION_ID;
|
|
|
|
#endif
|
|
|
|
*p++ = ALPN_HTTP_1_1;
|
|
|
|
*p = NULL;
|
2016-02-09 17:37:14 -05:00
|
|
|
/* this function doesn't clone the protocols array, which is why we need
|
|
|
|
to keep it around */
|
2020-03-18 18:22:01 -04:00
|
|
|
if(mbedtls_ssl_conf_alpn_protocols(&backend->config,
|
|
|
|
&backend->protocols[0])) {
|
2016-01-07 01:49:31 -05:00
|
|
|
failf(data, "Failed setting ALPN protocols");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
2020-03-18 18:22:01 -04:00
|
|
|
for(p = &backend->protocols[0]; *p; ++p)
|
2016-01-07 01:49:31 -05:00
|
|
|
infof(data, "ALPN, offering %s\n", *p);
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MBEDTLS_DEBUG
|
2016-08-03 02:20:16 -04:00
|
|
|
/* In order to make that work in mbedtls MBEDTLS_DEBUG_C must be defined. */
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_conf_dbg(&backend->config, mbed_debug, data);
|
2016-08-03 02:20:16 -04:00
|
|
|
/* - 0 No debug
|
|
|
|
* - 1 Error
|
|
|
|
* - 2 State change
|
|
|
|
* - 3 Informational
|
|
|
|
* - 4 Verbose
|
|
|
|
*/
|
|
|
|
mbedtls_debug_set_threshold(4);
|
2015-10-19 08:25:34 -04:00
|
|
|
#endif
|
|
|
|
|
2017-02-21 07:15:56 -05:00
|
|
|
/* give application a chance to interfere with mbedTLS set up. */
|
|
|
|
if(data->set.ssl.fsslctx) {
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = (*data->set.ssl.fsslctx)(data, &backend->config,
|
2017-02-21 07:15:56 -05:00
|
|
|
data->set.ssl.fsslctxp);
|
|
|
|
if(ret) {
|
|
|
|
failf(data, "error signaled by ssl ctx callback");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 08:25:34 -04:00
|
|
|
connssl->connecting_state = ssl_connect_2;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
2016-04-07 09:16:01 -04:00
|
|
|
mbed_connect_step2(struct connectdata *conn,
|
|
|
|
int sockindex)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
|
|
|
int ret;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2020-05-13 06:56:53 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2016-01-18 03:48:10 -05:00
|
|
|
const mbedtls_x509_crt *peercert;
|
2016-11-25 04:47:25 -05:00
|
|
|
const char * const pinnedpubkey = SSL_IS_PROXY() ?
|
2020-03-18 18:22:01 -04:00
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-04-07 09:16:01 -04:00
|
|
|
conn->recv[sockindex] = mbed_recv;
|
|
|
|
conn->send[sockindex] = mbed_send;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ssl_handshake(&backend->ssl);
|
2015-10-29 17:59:27 -04:00
|
|
|
|
|
|
|
if(ret == MBEDTLS_ERR_SSL_WANT_READ) {
|
|
|
|
connssl->connecting_state = ssl_connect_2_reading;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
else if(ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
|
|
|
|
connssl->connecting_state = ssl_connect_2_writing;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
else if(ret) {
|
2019-05-11 15:42:48 -04:00
|
|
|
char errorbuf[128];
|
|
|
|
errorbuf[0] = 0;
|
2015-10-19 08:25:34 -04:00
|
|
|
#ifdef MBEDTLS_ERROR_C
|
2015-10-29 17:59:27 -04:00
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
2015-10-19 08:25:34 -04:00
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
2015-10-29 17:59:27 -04:00
|
|
|
failf(data, "ssl_handshake returned - mbedTLS: (-0x%04X) %s",
|
|
|
|
-ret, errorbuf);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
infof(data, "mbedTLS: Handshake complete, cipher is %s\n",
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_ssl_get_ciphersuite(&backend->ssl)
|
2015-10-19 08:25:34 -04:00
|
|
|
);
|
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ssl_get_verify_result(&backend->ssl);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2018-12-19 02:46:39 -05:00
|
|
|
if(!SSL_CONN_CONFIG(verifyhost))
|
|
|
|
/* Ignore hostname errors if verifyhost is disabled */
|
|
|
|
ret &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
|
|
|
|
|
2016-11-21 05:01:25 -05:00
|
|
|
if(ret && SSL_CONN_CONFIG(verifypeer)) {
|
2015-10-19 08:25:34 -04:00
|
|
|
if(ret & MBEDTLS_X509_BADCERT_EXPIRED)
|
|
|
|
failf(data, "Cert verify failed: BADCERT_EXPIRED");
|
|
|
|
|
2018-12-19 02:46:39 -05:00
|
|
|
else if(ret & MBEDTLS_X509_BADCERT_REVOKED)
|
2015-10-19 08:25:34 -04:00
|
|
|
failf(data, "Cert verify failed: BADCERT_REVOKED");
|
|
|
|
|
2018-12-19 02:46:39 -05:00
|
|
|
else if(ret & MBEDTLS_X509_BADCERT_CN_MISMATCH)
|
|
|
|
failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
|
|
|
|
|
|
|
|
else if(ret & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
|
2015-10-19 08:25:34 -04:00
|
|
|
failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
|
|
|
|
|
2019-11-02 09:15:18 -04:00
|
|
|
else if(ret & MBEDTLS_X509_BADCERT_FUTURE)
|
|
|
|
failf(data, "Cert verify failed: BADCERT_FUTURE");
|
|
|
|
|
2015-10-19 08:25:34 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
peercert = mbedtls_ssl_get_peer_cert(&backend->ssl);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-01-18 03:48:10 -05:00
|
|
|
if(peercert && data->set.verbose) {
|
|
|
|
const size_t bufsize = 16384;
|
|
|
|
char *buffer = malloc(bufsize);
|
|
|
|
|
|
|
|
if(!buffer)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
if(mbedtls_x509_crt_info(buffer, bufsize, "* ", peercert) > 0)
|
2015-10-19 08:25:34 -04:00
|
|
|
infof(data, "Dumping cert info:\n%s\n", buffer);
|
2016-01-18 03:48:10 -05:00
|
|
|
else
|
|
|
|
infof(data, "Unable to dump certificate information.\n");
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
2016-11-25 04:47:25 -05:00
|
|
|
if(pinnedpubkey) {
|
2016-01-18 03:48:10 -05:00
|
|
|
int size;
|
|
|
|
CURLcode result;
|
|
|
|
mbedtls_x509_crt *p;
|
|
|
|
unsigned char pubkey[PUB_DER_MAX_BYTES];
|
|
|
|
|
|
|
|
if(!peercert || !peercert->raw.p || !peercert->raw.len) {
|
|
|
|
failf(data, "Failed due to missing peer certificate");
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = calloc(1, sizeof(*p));
|
|
|
|
|
|
|
|
if(!p)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
mbedtls_x509_crt_init(p);
|
|
|
|
|
|
|
|
/* Make a copy of our const peercert because mbedtls_pk_write_pubkey_der
|
|
|
|
needs a non-const key, for now.
|
|
|
|
https://github.com/ARMmbed/mbedtls/issues/396 */
|
|
|
|
if(mbedtls_x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) {
|
|
|
|
failf(data, "Failed copying peer certificate");
|
|
|
|
mbedtls_x509_crt_free(p);
|
|
|
|
free(p);
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = mbedtls_pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES);
|
|
|
|
|
|
|
|
if(size <= 0) {
|
|
|
|
failf(data, "Failed copying public key from peer certificate");
|
|
|
|
mbedtls_x509_crt_free(p);
|
|
|
|
free(p);
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mbedtls_pk_write_pubkey_der writes data at the end of the buffer. */
|
|
|
|
result = Curl_pin_peer_pubkey(data,
|
2016-11-25 04:47:25 -05:00
|
|
|
pinnedpubkey,
|
2016-01-18 03:48:10 -05:00
|
|
|
&pubkey[PUB_DER_MAX_BYTES - size], size);
|
|
|
|
if(result) {
|
|
|
|
mbedtls_x509_crt_free(p);
|
|
|
|
free(p);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_x509_crt_free(p);
|
|
|
|
free(p);
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_ALPN
|
2016-05-09 10:50:11 -04:00
|
|
|
if(conn->bits.tls_enable_alpn) {
|
2020-03-18 18:22:01 -04:00
|
|
|
const char *next_protocol = mbedtls_ssl_get_alpn_protocol(&backend->ssl);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-01-07 01:49:31 -05:00
|
|
|
if(next_protocol) {
|
2015-10-19 08:25:34 -04:00
|
|
|
infof(data, "ALPN, server accepted to use %s\n", next_protocol);
|
2016-01-07 01:49:31 -05:00
|
|
|
#ifdef USE_NGHTTP2
|
|
|
|
if(!strncmp(next_protocol, NGHTTP2_PROTO_VERSION_ID,
|
|
|
|
NGHTTP2_PROTO_VERSION_ID_LEN) &&
|
|
|
|
!next_protocol[NGHTTP2_PROTO_VERSION_ID_LEN]) {
|
2015-12-13 03:23:36 -05:00
|
|
|
conn->negnpn = CURL_HTTP_VERSION_2;
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
2016-01-07 01:49:31 -05:00
|
|
|
else
|
|
|
|
#endif
|
2016-02-10 04:46:57 -05:00
|
|
|
if(!strncmp(next_protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH) &&
|
|
|
|
!next_protocol[ALPN_HTTP_1_1_LENGTH]) {
|
|
|
|
conn->negnpn = CURL_HTTP_VERSION_1_1;
|
|
|
|
}
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
infof(data, "ALPN, server did not agree to a protocol\n");
|
|
|
|
}
|
2019-04-30 05:14:38 -04:00
|
|
|
Curl_multiuse_state(conn, conn->negnpn == CURL_HTTP_VERSION_2 ?
|
|
|
|
BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_3;
|
|
|
|
infof(data, "SSL connected\n");
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
2016-04-07 09:16:01 -04:00
|
|
|
mbed_connect_step3(struct connectdata *conn,
|
|
|
|
int sockindex)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
|
|
|
CURLcode retcode = CURLE_OK;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
|
|
|
|
|
2017-03-22 01:59:49 -04:00
|
|
|
if(SSL_SET_OPTION(primary.sessionid)) {
|
2016-06-12 23:47:12 -04:00
|
|
|
int ret;
|
|
|
|
mbedtls_ssl_session *our_ssl_sessionid;
|
|
|
|
void *old_ssl_sessionid = NULL;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-06-12 23:47:12 -04:00
|
|
|
our_ssl_sessionid = malloc(sizeof(mbedtls_ssl_session));
|
|
|
|
if(!our_ssl_sessionid)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2016-06-12 23:47:12 -04:00
|
|
|
mbedtls_ssl_session_init(our_ssl_sessionid);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ssl_get_session(&backend->ssl, our_ssl_sessionid);
|
2016-06-12 23:47:12 -04:00
|
|
|
if(ret) {
|
2019-02-16 16:30:31 -05:00
|
|
|
if(ret != MBEDTLS_ERR_SSL_ALLOC_FAILED)
|
|
|
|
mbedtls_ssl_session_free(our_ssl_sessionid);
|
2017-04-13 10:10:42 -04:00
|
|
|
free(our_ssl_sessionid);
|
2016-06-12 23:47:12 -04:00
|
|
|
failf(data, "mbedtls_ssl_get_session returned -0x%x", -ret);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If there's already a matching session in the cache, delete it */
|
|
|
|
Curl_ssl_sessionid_lock(conn);
|
2016-11-16 12:49:15 -05:00
|
|
|
if(!Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL, sockindex))
|
2016-06-12 23:47:12 -04:00
|
|
|
Curl_ssl_delsessionid(conn, old_ssl_sessionid);
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
retcode = Curl_ssl_addsessionid(conn, our_ssl_sessionid, 0, sockindex);
|
2016-06-12 23:47:12 -04:00
|
|
|
Curl_ssl_sessionid_unlock(conn);
|
|
|
|
if(retcode) {
|
2019-02-16 16:30:31 -05:00
|
|
|
mbedtls_ssl_session_free(our_ssl_sessionid);
|
2016-06-12 23:47:12 -04:00
|
|
|
free(our_ssl_sessionid);
|
|
|
|
failf(data, "failed to store ssl session");
|
|
|
|
return retcode;
|
|
|
|
}
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_done;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-07 09:16:01 -04:00
|
|
|
static ssize_t mbed_send(struct connectdata *conn, int sockindex,
|
|
|
|
const void *mem, size_t len,
|
|
|
|
CURLcode *curlcode)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2015-10-19 08:25:34 -04:00
|
|
|
int ret = -1;
|
|
|
|
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ssl_write(&backend->ssl,
|
2016-02-10 04:46:57 -05:00
|
|
|
(unsigned char *)mem, len);
|
2015-10-19 08:25:34 -04:00
|
|
|
|
|
|
|
if(ret < 0) {
|
|
|
|
*curlcode = (ret == MBEDTLS_ERR_SSL_WANT_WRITE) ?
|
|
|
|
CURLE_AGAIN : CURLE_SEND_ERROR;
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_mbedtls_close_all(struct Curl_easy *data)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_mbedtls_close(struct connectdata *conn, int sockindex)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
|
|
|
mbedtls_pk_free(&backend->pk);
|
|
|
|
mbedtls_x509_crt_free(&backend->clicert);
|
|
|
|
mbedtls_x509_crt_free(&backend->cacert);
|
|
|
|
mbedtls_x509_crl_free(&backend->crl);
|
|
|
|
mbedtls_ssl_config_free(&backend->config);
|
|
|
|
mbedtls_ssl_free(&backend->ssl);
|
|
|
|
mbedtls_ctr_drbg_free(&backend->ctr_drbg);
|
2016-02-01 17:13:10 -05:00
|
|
|
#ifndef THREADING_SUPPORT
|
2020-03-18 18:22:01 -04:00
|
|
|
mbedtls_entropy_free(&backend->entropy);
|
2016-02-01 17:13:10 -05:00
|
|
|
#endif /* THREADING_SUPPORT */
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
2016-04-07 09:16:01 -04:00
|
|
|
static ssize_t mbed_recv(struct connectdata *conn, int num,
|
|
|
|
char *buf, size_t buffersize,
|
|
|
|
CURLcode *curlcode)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[num];
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2015-10-19 08:25:34 -04:00
|
|
|
int ret = -1;
|
|
|
|
ssize_t len = -1;
|
|
|
|
|
|
|
|
memset(buf, 0, buffersize);
|
2020-03-18 18:22:01 -04:00
|
|
|
ret = mbedtls_ssl_read(&backend->ssl, (unsigned char *)buf,
|
2015-10-19 08:25:34 -04:00
|
|
|
buffersize);
|
|
|
|
|
|
|
|
if(ret <= 0) {
|
|
|
|
if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*curlcode = (ret == MBEDTLS_ERR_SSL_WANT_READ) ?
|
|
|
|
CURLE_AGAIN : CURLE_RECV_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ret;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_mbedtls_session_free(void *ptr)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2016-04-28 02:57:12 -04:00
|
|
|
mbedtls_ssl_session_free(ptr);
|
2015-10-19 08:25:34 -04:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static size_t Curl_mbedtls_version(char *buffer, size_t size)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2019-02-11 08:30:23 -05:00
|
|
|
#ifdef MBEDTLS_VERSION_C
|
|
|
|
/* if mbedtls_version_get_number() is available it is better */
|
2015-10-19 08:25:34 -04:00
|
|
|
unsigned int version = mbedtls_version_get_number();
|
2018-11-22 03:01:24 -05:00
|
|
|
return msnprintf(buffer, size, "mbedTLS/%u.%u.%u", version>>24,
|
|
|
|
(version>>16)&0xff, (version>>8)&0xff);
|
2019-02-11 08:30:23 -05:00
|
|
|
#else
|
|
|
|
return msnprintf(buffer, size, "mbedTLS/%s", MBEDTLS_VERSION_STRING);
|
|
|
|
#endif
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static CURLcode Curl_mbedtls_random(struct Curl_easy *data,
|
|
|
|
unsigned char *entropy, size_t length)
|
2017-01-27 02:39:28 -05:00
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
|
|
|
int ret = -1;
|
|
|
|
char errorbuf[128];
|
|
|
|
mbedtls_entropy_context ctr_entropy;
|
|
|
|
mbedtls_ctr_drbg_context ctr_drbg;
|
|
|
|
mbedtls_entropy_init(&ctr_entropy);
|
|
|
|
mbedtls_ctr_drbg_init(&ctr_drbg);
|
2017-09-09 17:09:06 -04:00
|
|
|
errorbuf[0] = 0;
|
2017-01-27 02:39:28 -05:00
|
|
|
|
|
|
|
ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
|
|
|
|
&ctr_entropy, NULL, 0);
|
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
|
|
|
failf(data, "Failed - mbedTLS: ctr_drbg_seed returned (-0x%04X) %s\n",
|
|
|
|
-ret, errorbuf);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ret = mbedtls_ctr_drbg_random(&ctr_drbg, entropy, length);
|
|
|
|
|
|
|
|
if(ret) {
|
|
|
|
#ifdef MBEDTLS_ERROR_C
|
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
|
|
|
failf(data, "mbedTLS: ctr_drbg_init returned (-0x%04X) %s\n",
|
|
|
|
-ret, errorbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ctr_drbg_free(&ctr_drbg);
|
|
|
|
mbedtls_entropy_free(&ctr_entropy);
|
|
|
|
|
|
|
|
return ret == 0 ? CURLE_OK : CURLE_FAILED_INIT;
|
|
|
|
#elif defined(MBEDTLS_HAVEGE_C)
|
|
|
|
mbedtls_havege_state hs;
|
|
|
|
mbedtls_havege_init(&hs);
|
|
|
|
mbedtls_havege_random(&hs, entropy, length);
|
|
|
|
mbedtls_havege_free(&hs);
|
|
|
|
return CURLE_OK;
|
|
|
|
#else
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-10-19 08:25:34 -04:00
|
|
|
static CURLcode
|
2016-04-07 09:16:01 -04:00
|
|
|
mbed_connect_common(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
bool nonblocking,
|
|
|
|
bool *done)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
|
|
|
CURLcode retcode;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-10-19 08:25:34 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
2019-09-22 13:04:18 -04:00
|
|
|
timediff_t timeout_ms;
|
2015-10-19 08:25:34 -04:00
|
|
|
int what;
|
|
|
|
|
|
|
|
/* check if the connection has already been established */
|
|
|
|
if(ssl_connection_complete == connssl->state) {
|
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
if(ssl_connect_1 == connssl->connecting_state) {
|
2015-10-19 08:25:34 -04:00
|
|
|
/* Find out how much more time we're allowed */
|
|
|
|
timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* no need to continue if time already is up */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
2016-04-07 09:16:01 -04:00
|
|
|
retcode = mbed_connect_step1(conn, sockindex);
|
2015-10-19 08:25:34 -04:00
|
|
|
if(retcode)
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(ssl_connect_2 == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_reading == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_writing == connssl->connecting_state) {
|
|
|
|
|
|
|
|
/* check allowed time left */
|
|
|
|
timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* no need to continue if time already is up */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if ssl is expecting something, check if it's available. */
|
|
|
|
if(connssl->connecting_state == ssl_connect_2_reading
|
|
|
|
|| connssl->connecting_state == ssl_connect_2_writing) {
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
curl_socket_t writefd = ssl_connect_2_writing ==
|
2015-10-19 08:25:34 -04:00
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
2017-09-09 17:09:06 -04:00
|
|
|
curl_socket_t readfd = ssl_connect_2_reading ==
|
2015-10-19 08:25:34 -04:00
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
|
|
|
|
2016-10-18 04:58:58 -04:00
|
|
|
what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
|
2019-09-22 13:04:18 -04:00
|
|
|
nonblocking ? 0 : (time_t)timeout_ms);
|
2015-10-19 08:25:34 -04:00
|
|
|
if(what < 0) {
|
|
|
|
/* fatal error */
|
|
|
|
failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
else if(0 == what) {
|
|
|
|
if(nonblocking) {
|
|
|
|
*done = FALSE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* timeout */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* socket is readable or writable */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run transaction, and return to the caller if it failed or if
|
|
|
|
* this connection is part of a multi handle and this loop would
|
|
|
|
* execute again. This permits the owner of a multi handle to
|
|
|
|
* abort a connection attempt before step2 has completed while
|
|
|
|
* ensuring that a client using select() or epoll() will always
|
|
|
|
* have a valid fdset to wait on.
|
|
|
|
*/
|
2016-04-07 09:16:01 -04:00
|
|
|
retcode = mbed_connect_step2(conn, sockindex);
|
2015-10-19 08:25:34 -04:00
|
|
|
if(retcode || (nonblocking &&
|
|
|
|
(ssl_connect_2 == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_reading == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_writing == connssl->connecting_state)))
|
|
|
|
return retcode;
|
|
|
|
|
|
|
|
} /* repeat step2 until all transactions are done. */
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
if(ssl_connect_3 == connssl->connecting_state) {
|
2016-04-07 09:16:01 -04:00
|
|
|
retcode = mbed_connect_step3(conn, sockindex);
|
2015-10-19 08:25:34 -04:00
|
|
|
if(retcode)
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
if(ssl_connect_done == connssl->connecting_state) {
|
2015-10-19 08:25:34 -04:00
|
|
|
connssl->state = ssl_connection_complete;
|
2016-04-07 09:16:01 -04:00
|
|
|
conn->recv[sockindex] = mbed_recv;
|
|
|
|
conn->send[sockindex] = mbed_send;
|
2015-10-19 08:25:34 -04:00
|
|
|
*done = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*done = FALSE;
|
|
|
|
|
|
|
|
/* Reset our connect state machine */
|
|
|
|
connssl->connecting_state = ssl_connect_1;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static CURLcode Curl_mbedtls_connect_nonblocking(struct connectdata *conn,
|
|
|
|
int sockindex, bool *done)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2016-04-07 09:16:01 -04:00
|
|
|
return mbed_connect_common(conn, sockindex, TRUE, done);
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static CURLcode Curl_mbedtls_connect(struct connectdata *conn, int sockindex)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
|
|
|
CURLcode retcode;
|
|
|
|
bool done = FALSE;
|
|
|
|
|
2016-04-07 09:16:01 -04:00
|
|
|
retcode = mbed_connect_common(conn, sockindex, FALSE, &done);
|
2015-10-19 08:25:34 -04:00
|
|
|
if(retcode)
|
|
|
|
return retcode;
|
|
|
|
|
|
|
|
DEBUGASSERT(done);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return 0 error initializing SSL
|
|
|
|
* return 1 SSL initialized successfully
|
|
|
|
*/
|
2017-06-23 07:19:00 -04:00
|
|
|
static int Curl_mbedtls_init(void)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2020-01-16 02:17:04 -05:00
|
|
|
return Curl_mbedtlsthreadlock_thread_setup();
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_mbedtls_cleanup(void)
|
2015-10-19 08:25:34 -04:00
|
|
|
{
|
2020-01-16 02:17:04 -05:00
|
|
|
(void)Curl_mbedtlsthreadlock_thread_cleanup();
|
2015-10-19 08:25:34 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static bool Curl_mbedtls_data_pending(const struct connectdata *conn,
|
|
|
|
int sockindex)
|
2016-04-07 10:10:10 -04:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
const struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
|
|
|
return mbedtls_ssl_get_bytes_avail(&backend->ssl) != 0;
|
2016-04-07 10:10:10 -04:00
|
|
|
}
|
2016-04-07 09:19:35 -04:00
|
|
|
|
2018-04-02 13:33:00 -04:00
|
|
|
static CURLcode Curl_mbedtls_sha256sum(const unsigned char *input,
|
2017-06-22 19:04:56 -04:00
|
|
|
size_t inputlen,
|
|
|
|
unsigned char *sha256sum,
|
|
|
|
size_t sha256len UNUSED_PARAM)
|
|
|
|
{
|
2017-09-06 15:11:55 -04:00
|
|
|
(void)sha256len;
|
2018-04-02 13:33:00 -04:00
|
|
|
#if MBEDTLS_VERSION_NUMBER < 0x02070000
|
2017-06-22 19:04:56 -04:00
|
|
|
mbedtls_sha256(input, inputlen, sha256sum, 0);
|
2018-04-02 13:33:00 -04:00
|
|
|
#else
|
|
|
|
/* returns 0 on success, otherwise failure */
|
|
|
|
if(mbedtls_sha256_ret(input, inputlen, sha256sum, 0) != 0)
|
|
|
|
return CURLE_BAD_FUNCTION_ARGUMENT;
|
|
|
|
#endif
|
|
|
|
return CURLE_OK;
|
2017-06-22 19:04:56 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 10:05:26 -04:00
|
|
|
static void *Curl_mbedtls_get_internals(struct ssl_connect_data *connssl,
|
|
|
|
CURLINFO info UNUSED_PARAM)
|
|
|
|
{
|
2020-03-18 18:22:01 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2017-06-23 10:05:26 -04:00
|
|
|
(void)info;
|
2020-03-18 18:22:01 -04:00
|
|
|
return &backend->ssl;
|
2017-06-23 10:05:26 -04:00
|
|
|
}
|
|
|
|
|
2017-06-22 10:45:34 -04:00
|
|
|
const struct Curl_ssl Curl_ssl_mbedtls = {
|
2017-07-15 07:49:30 -04:00
|
|
|
{ CURLSSLBACKEND_MBEDTLS, "mbedtls" }, /* info */
|
2017-06-22 10:45:34 -04:00
|
|
|
|
2018-05-04 06:10:39 -04:00
|
|
|
SSLSUPP_CA_PATH |
|
|
|
|
SSLSUPP_PINNEDPUBKEY |
|
|
|
|
SSLSUPP_SSL_CTX,
|
2017-06-20 05:32:53 -04:00
|
|
|
|
vtls: encapsulate SSL backend-specific data
So far, all of the SSL backends' private data has been declared as
part of the ssl_connect_data struct, in one big #if .. #elif .. #endif
block.
This can only work as long as the SSL backend is a compile-time option,
something we want to change in the next commits.
Therefore, let's encapsulate the exact data needed by each SSL backend
into a private struct, and let's avoid bleeding any SSL backend-specific
information into urldata.h. This is also necessary to allow multiple SSL
backends to be compiled in at the same time, as e.g. OpenSSL's and
CyaSSL's headers cannot be included in the same .c file.
To avoid too many malloc() calls, we simply append the private structs
to the connectdata struct in allocate_conn().
This requires us to take extra care of alignment issues: struct fields
often need to be aligned on certain boundaries e.g. 32-bit values need to
be stored at addresses that divide evenly by 4 (= 32 bit / 8
bit-per-byte).
We do that by assuming that no SSL backend's private data contains any
fields that need to be aligned on boundaries larger than `long long`
(typically 64-bit) would need. Under this assumption, we simply add a
dummy field of type `long long` to the `struct connectdata` struct. This
field will never be accessed but acts as a placeholder for the four
instances of ssl_backend_data instead. the size of each ssl_backend_data
struct is stored in the SSL backend-specific metadata, to allow
allocate_conn() to know how much extra space to allocate, and how to
initialize the ssl[sockindex]->backend and proxy_ssl[sockindex]->backend
pointers.
This would appear to be a little complicated at first, but is really
necessary to encapsulate the private data of each SSL backend correctly.
And we need to encapsulate thusly if we ever want to allow selecting
CyaSSL and OpenSSL at runtime, as their headers cannot be included within
the same .c file (there are just too many conflicting definitions and
declarations for that).
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2017-07-28 16:09:35 -04:00
|
|
|
sizeof(struct ssl_backend_data),
|
|
|
|
|
2017-06-22 10:45:34 -04:00
|
|
|
Curl_mbedtls_init, /* init */
|
|
|
|
Curl_mbedtls_cleanup, /* cleanup */
|
|
|
|
Curl_mbedtls_version, /* version */
|
|
|
|
Curl_none_check_cxn, /* check_cxn */
|
|
|
|
Curl_none_shutdown, /* shutdown */
|
|
|
|
Curl_mbedtls_data_pending, /* data_pending */
|
|
|
|
Curl_mbedtls_random, /* random */
|
|
|
|
Curl_none_cert_status_request, /* cert_status_request */
|
|
|
|
Curl_mbedtls_connect, /* connect */
|
|
|
|
Curl_mbedtls_connect_nonblocking, /* connect_nonblocking */
|
2017-06-23 10:05:26 -04:00
|
|
|
Curl_mbedtls_get_internals, /* get_internals */
|
2017-10-19 14:55:17 -04:00
|
|
|
Curl_mbedtls_close, /* close_one */
|
2017-06-22 10:45:34 -04:00
|
|
|
Curl_mbedtls_close_all, /* close_all */
|
|
|
|
Curl_mbedtls_session_free, /* session_free */
|
|
|
|
Curl_none_set_engine, /* set_engine */
|
|
|
|
Curl_none_set_engine_default, /* set_engine_default */
|
|
|
|
Curl_none_engines_list, /* engines_list */
|
2017-06-22 19:04:56 -04:00
|
|
|
Curl_none_false_start, /* false_start */
|
2017-06-22 19:04:56 -04:00
|
|
|
Curl_none_md5sum, /* md5sum */
|
|
|
|
Curl_mbedtls_sha256sum /* sha256sum */
|
2017-06-22 10:45:34 -04:00
|
|
|
};
|
|
|
|
|
2015-10-19 08:25:34 -04:00
|
|
|
#endif /* USE_MBEDTLS */
|