2015-10-19 20:25:34 +08:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 - 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
|
2017-01-27 15:39:28 +08:00
|
|
|
* Copyright (C) 2012 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2015-10-19 20:25:34 +08: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-03 07:19:02 +08:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2015-10-19 20:25:34 +08: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-26 06:28:40 +08:00
|
|
|
* Source file for all mbedTLS-specific code for the TLS/SSL layer. No code
|
2015-10-19 20:25:34 +08:00
|
|
|
* but vtls.c should ever call or use these functions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
|
|
|
|
|
|
|
#ifdef USE_MBEDTLS
|
|
|
|
|
2016-11-04 18:13:16 +08:00
|
|
|
#include <mbedtls/version.h>
|
|
|
|
#if MBEDTLS_VERSION_NUMBER >= 0x02040000
|
2016-10-27 05:38:04 +08:00
|
|
|
#include <mbedtls/net_sockets.h>
|
2016-11-04 18:13:16 +08:00
|
|
|
#else
|
|
|
|
#include <mbedtls/net.h>
|
|
|
|
#endif
|
2015-10-19 20:25:34 +08: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-20 05:37:21 +08:00
|
|
|
#include <mbedtls/sha256.h>
|
2015-10-19 20:25:34 +08: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"
|
|
|
|
#include "polarssl_threadlock.h"
|
|
|
|
|
2016-05-24 18:14:18 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
2015-10-19 20:25:34 +08:00
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
/* apply threading? */
|
|
|
|
#if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
|
|
|
|
#define THREADING_SUPPORT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(THREADING_SUPPORT)
|
|
|
|
static mbedtls_entropy_context entropy;
|
|
|
|
|
2016-04-07 21:16:01 +08:00
|
|
|
static int entropy_init_initialized = 0;
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
/* start of entropy_init_mutex() */
|
|
|
|
static void entropy_init_mutex(mbedtls_entropy_context *ctx)
|
|
|
|
{
|
|
|
|
/* lock 0 = entropy_init_mutex() */
|
2015-10-23 22:14:29 +08:00
|
|
|
Curl_polarsslthreadlock_lock_function(0);
|
2015-10-19 20:25:34 +08:00
|
|
|
if(entropy_init_initialized == 0) {
|
|
|
|
mbedtls_entropy_init(ctx);
|
|
|
|
entropy_init_initialized = 1;
|
|
|
|
}
|
2015-10-23 22:14:29 +08:00
|
|
|
Curl_polarsslthreadlock_unlock_function(0);
|
2015-10-19 20:25:34 +08: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() */
|
2015-10-23 22:14:29 +08:00
|
|
|
Curl_polarsslthreadlock_lock_function(1);
|
2015-10-19 20:25:34 +08:00
|
|
|
ret = mbedtls_entropy_func(data, output, len);
|
2015-10-23 22:14:29 +08:00
|
|
|
Curl_polarsslthreadlock_unlock_function(1);
|
2015-10-19 20:25:34 +08: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 21:58:11 +08:00
|
|
|
static void mbed_debug(void *context, int level, const char *f_name,
|
|
|
|
int line_nb, const char *line)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
2016-06-21 21:47:12 +08:00
|
|
|
struct Curl_easy *data = NULL;
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
if(!context)
|
|
|
|
return;
|
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
data = (struct Curl_easy *)context;
|
2015-10-19 20:25:34 +08: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 15:30:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* profile
|
|
|
|
*/
|
|
|
|
const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_fr =
|
|
|
|
{
|
2016-02-10 17:46:57 +08:00
|
|
|
/* Hashes from SHA-1 and above */
|
2016-04-04 02:28:34 +08: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 17:46:57 +08:00
|
|
|
0xFFFFFFF, /* Any PK alg */
|
|
|
|
0xFFFFFFF, /* Any curve */
|
|
|
|
1024, /* RSA min key len */
|
2015-10-21 15:30:31 +08:00
|
|
|
};
|
|
|
|
|
2016-01-06 14:00:11 +08: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 16:48:10 +08:00
|
|
|
RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES)
|
2016-01-06 14:00:11 +08:00
|
|
|
|
2016-04-07 21:16:01 +08:00
|
|
|
static Curl_recv mbed_recv;
|
|
|
|
static Curl_send mbed_send;
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
static CURLcode
|
2016-04-07 21:16:01 +08:00
|
|
|
mbed_connect_step1(struct connectdata *conn,
|
|
|
|
int sockindex)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
2016-06-21 21:47:12 +08:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-10-19 20:25:34 +08:00
|
|
|
struct ssl_connect_data* connssl = &conn->ssl[sockindex];
|
2016-11-21 18:01:25 +08: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 20:25:34 +08:00
|
|
|
int ret = -1;
|
|
|
|
char errorbuf[128];
|
|
|
|
errorbuf[0]=0;
|
|
|
|
|
|
|
|
/* mbedTLS only supports SSLv3 and TLSv1 */
|
2016-11-21 18:01:25 +08:00
|
|
|
if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2) {
|
2015-10-19 20:25:34 +08:00
|
|
|
failf(data, "mbedTLS does not support SSLv2");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef THREADING_SUPPORT
|
|
|
|
entropy_init_mutex(&entropy);
|
2015-10-24 06:46:03 +08:00
|
|
|
mbedtls_ctr_drbg_init(&connssl->ctr_drbg);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2015-10-24 06:46:03 +08:00
|
|
|
ret = mbedtls_ctr_drbg_seed(&connssl->ctr_drbg, entropy_func_mutex,
|
2016-04-28 14:57:12 +08:00
|
|
|
&entropy, NULL, 0);
|
2015-10-24 06:46:03 +08:00
|
|
|
if(ret) {
|
2015-10-19 20:25:34 +08:00
|
|
|
#ifdef MBEDTLS_ERROR_C
|
2015-10-24 06:46:03 +08:00
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
2015-10-19 20:25:34 +08:00
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
2015-10-24 06:46:03 +08:00
|
|
|
failf(data, "Failed - mbedTLS: ctr_drbg_init returned (-0x%04X) %s\n",
|
|
|
|
-ret, errorbuf);
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
mbedtls_entropy_init(&connssl->entropy);
|
|
|
|
mbedtls_ctr_drbg_init(&connssl->ctr_drbg);
|
|
|
|
|
|
|
|
ret = mbedtls_ctr_drbg_seed(&connssl->ctr_drbg, mbedtls_entropy_func,
|
2016-04-28 14:57:12 +08:00
|
|
|
&connssl->entropy, NULL, 0);
|
2015-10-19 20:25:34 +08: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 */
|
2016-02-02 06:13:10 +08:00
|
|
|
mbedtls_x509_crt_init(&connssl->cacert);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(ssl_cafile) {
|
|
|
|
ret = mbedtls_x509_crt_parse_file(&connssl->cacert, ssl_cafile);
|
2015-10-19 20:25:34 +08: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 18:01:25 +08:00
|
|
|
ssl_cafile, -ret, errorbuf);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(verifypeer)
|
2015-10-19 20:25:34 +08:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(ssl_capath) {
|
|
|
|
ret = mbedtls_x509_crt_parse_path(&connssl->cacert, ssl_capath);
|
2015-10-19 20:25:34 +08: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 18:01:25 +08:00
|
|
|
ssl_capath, -ret, errorbuf);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(verifypeer)
|
2015-10-19 20:25:34 +08:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the client certificate */
|
2016-02-02 06:13:10 +08:00
|
|
|
mbedtls_x509_crt_init(&connssl->clicert);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(ssl_cert) {
|
|
|
|
ret = mbedtls_x509_crt_parse_file(&connssl->clicert, ssl_cert);
|
2015-10-19 20:25:34 +08: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 18:01:25 +08:00
|
|
|
ssl_cert, -ret, errorbuf);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
return CURLE_SSL_CERTPROBLEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the client private key */
|
2016-02-02 06:13:10 +08:00
|
|
|
mbedtls_pk_init(&connssl->pk);
|
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(SSL_SET_OPTION(key)) {
|
|
|
|
ret = mbedtls_pk_parse_keyfile(&connssl->pk, SSL_SET_OPTION(key),
|
|
|
|
SSL_SET_OPTION(key_passwd));
|
2015-10-19 20:25:34 +08:00
|
|
|
if(ret == 0 && !mbedtls_pk_can_do(&connssl->pk, MBEDTLS_PK_RSA))
|
|
|
|
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 18:01:25 +08:00
|
|
|
SSL_SET_OPTION(key), -ret, errorbuf);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
return CURLE_SSL_CERTPROBLEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the CRL */
|
2016-02-02 06:13:10 +08:00
|
|
|
mbedtls_x509_crl_init(&connssl->crl);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(ssl_crlfile) {
|
|
|
|
ret = mbedtls_x509_crl_parse_file(&connssl->crl, ssl_crlfile);
|
2015-10-19 20:25:34 +08: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 18:01:25 +08:00
|
|
|
ssl_crlfile, -ret, errorbuf);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
return CURLE_SSL_CRL_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
infof(data, "mbedTLS: Connecting to %s:%d\n", hostname, port);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
mbedtls_ssl_config_init(&connssl->config);
|
|
|
|
|
|
|
|
mbedtls_ssl_init(&connssl->ssl);
|
|
|
|
if(mbedtls_ssl_setup(&connssl->ssl, &connssl->config)) {
|
|
|
|
failf(data, "mbedTLS: ssl_init failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
ret = mbedtls_ssl_config_defaults(&connssl->config,
|
|
|
|
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 15:30:31 +08:00
|
|
|
/* new profile with RSA min key len = 1024 ... */
|
2016-04-04 02:28:34 +08:00
|
|
|
mbedtls_ssl_conf_cert_profile(&connssl->config,
|
|
|
|
&mbedtls_x509_crt_profile_fr);
|
2015-10-21 15:30:31 +08:00
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
switch(SSL_CONN_CONFIG(version)) {
|
2016-03-06 10:35:16 +08:00
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
|
|
|
mbedtls_ssl_conf_min_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_1);
|
|
|
|
infof(data, "mbedTLS: Set min SSL version to TLS 1.0\n");
|
|
|
|
break;
|
2015-10-19 20:25:34 +08:00
|
|
|
case CURL_SSLVERSION_SSLv3:
|
|
|
|
mbedtls_ssl_conf_min_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_0);
|
2016-03-06 10:35:16 +08:00
|
|
|
mbedtls_ssl_conf_max_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_0);
|
|
|
|
infof(data, "mbedTLS: Set SSL version to SSLv3\n");
|
2015-10-19 20:25:34 +08:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
|
|
|
mbedtls_ssl_conf_min_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_1);
|
2016-03-06 10:35:16 +08:00
|
|
|
mbedtls_ssl_conf_max_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_1);
|
|
|
|
infof(data, "mbedTLS: Set SSL version to TLS 1.0\n");
|
2015-10-19 20:25:34 +08:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
|
|
|
mbedtls_ssl_conf_min_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_2);
|
2016-03-06 10:35:16 +08:00
|
|
|
mbedtls_ssl_conf_max_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_2);
|
|
|
|
infof(data, "mbedTLS: Set SSL version to TLS 1.1\n");
|
2015-10-19 20:25:34 +08:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
|
|
|
mbedtls_ssl_conf_min_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_3);
|
2016-03-06 10:35:16 +08:00
|
|
|
mbedtls_ssl_conf_max_version(&connssl->config, MBEDTLS_SSL_MAJOR_VERSION_3,
|
|
|
|
MBEDTLS_SSL_MINOR_VERSION_3);
|
|
|
|
infof(data, "mbedTLS: Set SSL version to TLS 1.2\n");
|
2015-10-19 20:25:34 +08:00
|
|
|
break;
|
2016-11-08 10:51:27 +08:00
|
|
|
case CURL_SSLVERSION_TLSv1_3:
|
|
|
|
failf(data, "mbedTLS: TLS 1.3 is not yet supported");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2016-03-06 10:35:16 +08:00
|
|
|
default:
|
2016-11-08 10:51:27 +08:00
|
|
|
failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
|
2016-03-06 10:35:16 +08:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_authmode(&connssl->config, MBEDTLS_SSL_VERIFY_OPTIONAL);
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_rng(&connssl->config, mbedtls_ctr_drbg_random,
|
2016-02-10 17:46:57 +08:00
|
|
|
&connssl->ctr_drbg);
|
2015-10-19 20:25:34 +08:00
|
|
|
mbedtls_ssl_set_bio(&connssl->ssl, &conn->sock[sockindex],
|
2016-02-10 17:46:57 +08:00
|
|
|
mbedtls_net_send,
|
|
|
|
mbedtls_net_recv,
|
|
|
|
NULL /* rev_timeout() */);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
mbedtls_ssl_conf_ciphersuites(&connssl->config,
|
|
|
|
mbedtls_ssl_list_ciphersuites());
|
2016-06-13 11:47:12 +08:00
|
|
|
|
2017-01-29 03:14:54 +08:00
|
|
|
#if defined(MBEDTLS_SSL_SESSION_TICKETS)
|
|
|
|
mbedtls_ssl_conf_session_tickets(&connssl->config,
|
|
|
|
MBEDTLS_SSL_SESSION_TICKETS_DISABLED);
|
|
|
|
#endif
|
|
|
|
|
2016-06-13 11:47:12 +08:00
|
|
|
/* Check if there's a cached ID we can/should use here! */
|
2016-11-17 01:49:15 +08:00
|
|
|
if(data->set.general_ssl.sessionid) {
|
2016-06-13 11:47:12 +08:00
|
|
|
void *old_session = NULL;
|
|
|
|
|
|
|
|
Curl_ssl_sessionid_lock(conn);
|
2016-11-17 01:49:15 +08:00
|
|
|
if(!Curl_ssl_getsessionid(conn, &old_session, NULL, sockindex)) {
|
2016-06-13 11:47:12 +08:00
|
|
|
ret = mbedtls_ssl_set_session(&connssl->ssl, old_session);
|
|
|
|
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 14:57:12 +08:00
|
|
|
}
|
2016-06-13 11:47:12 +08:00
|
|
|
Curl_ssl_sessionid_unlock(conn);
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_ssl_conf_ca_chain(&connssl->config,
|
2016-02-10 17:46:57 +08:00
|
|
|
&connssl->cacert,
|
|
|
|
&connssl->crl);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(SSL_SET_OPTION(key)) {
|
2015-10-19 20:25:34 +08:00
|
|
|
mbedtls_ssl_conf_own_cert(&connssl->config,
|
2016-02-10 17:46:57 +08:00
|
|
|
&connssl->clicert, &connssl->pk);
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
2016-11-21 18:01:25 +08:00
|
|
|
if(mbedtls_ssl_set_hostname(&connssl->ssl, hostname)) {
|
2016-04-24 23:52:18 +08: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 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_ALPN
|
2016-05-09 22:50:11 +08:00
|
|
|
if(conn->bits.tls_enable_alpn) {
|
2016-02-10 06:37:14 +08:00
|
|
|
const char **p = &connssl->protocols[0];
|
2016-01-07 14:49:31 +08: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-10 06:37:14 +08:00
|
|
|
/* this function doesn't clone the protocols array, which is why we need
|
|
|
|
to keep it around */
|
|
|
|
if(mbedtls_ssl_conf_alpn_protocols(&connssl->config,
|
|
|
|
&connssl->protocols[0])) {
|
2016-01-07 14:49:31 +08:00
|
|
|
failf(data, "Failed setting ALPN protocols");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
2016-02-10 06:37:14 +08:00
|
|
|
for(p = &connssl->protocols[0]; *p; ++p)
|
2016-01-07 14:49:31 +08:00
|
|
|
infof(data, "ALPN, offering %s\n", *p);
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef MBEDTLS_DEBUG
|
2016-08-03 14:20:16 +08:00
|
|
|
/* In order to make that work in mbedtls MBEDTLS_DEBUG_C must be defined. */
|
2016-08-02 01:16:42 +08:00
|
|
|
mbedtls_ssl_conf_dbg(&connssl->config, mbed_debug, data);
|
2016-08-03 14:20:16 +08:00
|
|
|
/* - 0 No debug
|
|
|
|
* - 1 Error
|
|
|
|
* - 2 State change
|
|
|
|
* - 3 Informational
|
|
|
|
* - 4 Verbose
|
|
|
|
*/
|
|
|
|
mbedtls_debug_set_threshold(4);
|
2015-10-19 20:25:34 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_2;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
2016-04-07 21:16:01 +08:00
|
|
|
mbed_connect_step2(struct connectdata *conn,
|
|
|
|
int sockindex)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
|
|
|
int ret;
|
2016-06-21 21:47:12 +08:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-10-19 20:25:34 +08:00
|
|
|
struct ssl_connect_data* connssl = &conn->ssl[sockindex];
|
2016-01-18 16:48:10 +08:00
|
|
|
const mbedtls_x509_crt *peercert;
|
2016-11-25 17:47:25 +08:00
|
|
|
const char * const pinnedpubkey = SSL_IS_PROXY() ?
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
#ifdef HAS_ALPN
|
2016-11-23 14:53:24 +08:00
|
|
|
const char *next_protocol;
|
2015-10-19 20:25:34 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
char errorbuf[128];
|
|
|
|
errorbuf[0] = 0;
|
|
|
|
|
2016-04-07 21:16:01 +08:00
|
|
|
conn->recv[sockindex] = mbed_recv;
|
|
|
|
conn->send[sockindex] = mbed_send;
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2015-10-30 05:59:27 +08:00
|
|
|
ret = mbedtls_ssl_handshake(&connssl->ssl);
|
|
|
|
|
|
|
|
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) {
|
2015-10-19 20:25:34 +08:00
|
|
|
#ifdef MBEDTLS_ERROR_C
|
2015-10-30 05:59:27 +08:00
|
|
|
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
|
2015-10-19 20:25:34 +08:00
|
|
|
#endif /* MBEDTLS_ERROR_C */
|
2015-10-30 05:59:27 +08:00
|
|
|
failf(data, "ssl_handshake returned - mbedTLS: (-0x%04X) %s",
|
|
|
|
-ret, errorbuf);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
infof(data, "mbedTLS: Handshake complete, cipher is %s\n",
|
|
|
|
mbedtls_ssl_get_ciphersuite(&conn->ssl[sockindex].ssl)
|
|
|
|
);
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_get_verify_result(&conn->ssl[sockindex].ssl);
|
|
|
|
|
2016-11-21 18:01:25 +08:00
|
|
|
if(ret && SSL_CONN_CONFIG(verifypeer)) {
|
2015-10-19 20:25:34 +08:00
|
|
|
if(ret & MBEDTLS_X509_BADCERT_EXPIRED)
|
|
|
|
failf(data, "Cert verify failed: BADCERT_EXPIRED");
|
|
|
|
|
|
|
|
if(ret & MBEDTLS_X509_BADCERT_REVOKED) {
|
|
|
|
failf(data, "Cert verify failed: BADCERT_REVOKED");
|
|
|
|
return CURLE_SSL_CACERT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ret & MBEDTLS_X509_BADCERT_CN_MISMATCH)
|
|
|
|
failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
|
|
|
|
|
|
|
|
if(ret & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
|
|
|
|
failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
|
|
|
|
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
|
2016-01-18 16:48:10 +08:00
|
|
|
peercert = mbedtls_ssl_get_peer_cert(&connssl->ssl);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-01-18 16:48:10 +08: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 20:25:34 +08:00
|
|
|
infof(data, "Dumping cert info:\n%s\n", buffer);
|
2016-01-18 16:48:10 +08:00
|
|
|
else
|
|
|
|
infof(data, "Unable to dump certificate information.\n");
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
2016-11-25 17:47:25 +08:00
|
|
|
if(pinnedpubkey) {
|
2016-01-18 16:48:10 +08: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 17:47:25 +08:00
|
|
|
pinnedpubkey,
|
2016-01-18 16:48:10 +08: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 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAS_ALPN
|
2016-05-09 22:50:11 +08:00
|
|
|
if(conn->bits.tls_enable_alpn) {
|
2015-10-19 20:25:34 +08:00
|
|
|
next_protocol = mbedtls_ssl_get_alpn_protocol(&connssl->ssl);
|
|
|
|
|
2016-01-07 14:49:31 +08:00
|
|
|
if(next_protocol) {
|
2015-10-19 20:25:34 +08:00
|
|
|
infof(data, "ALPN, server accepted to use %s\n", next_protocol);
|
2016-01-07 14:49:31 +08: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 16:23:36 +08:00
|
|
|
conn->negnpn = CURL_HTTP_VERSION_2;
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
2016-01-07 14:49:31 +08:00
|
|
|
else
|
|
|
|
#endif
|
2016-02-10 17:46:57 +08: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 20:25:34 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
infof(data, "ALPN, server did not agree to a protocol\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_3;
|
|
|
|
infof(data, "SSL connected\n");
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
2016-04-07 21:16:01 +08:00
|
|
|
mbed_connect_step3(struct connectdata *conn,
|
|
|
|
int sockindex)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
|
|
|
CURLcode retcode = CURLE_OK;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2016-06-21 21:47:12 +08:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
|
|
|
|
|
2016-11-17 01:49:15 +08:00
|
|
|
if(data->set.general_ssl.sessionid) {
|
2016-06-13 11:47:12 +08:00
|
|
|
int ret;
|
|
|
|
mbedtls_ssl_session *our_ssl_sessionid;
|
|
|
|
void *old_ssl_sessionid = NULL;
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-06-13 11:47:12 +08:00
|
|
|
our_ssl_sessionid = malloc(sizeof(mbedtls_ssl_session));
|
|
|
|
if(!our_ssl_sessionid)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-06-13 11:47:12 +08:00
|
|
|
mbedtls_ssl_session_init(our_ssl_sessionid);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
2016-06-13 11:47:12 +08:00
|
|
|
ret = mbedtls_ssl_get_session(&connssl->ssl, our_ssl_sessionid);
|
|
|
|
if(ret) {
|
|
|
|
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-17 01:49:15 +08:00
|
|
|
if(!Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL, sockindex))
|
2016-06-13 11:47:12 +08:00
|
|
|
Curl_ssl_delsessionid(conn, old_ssl_sessionid);
|
|
|
|
|
2016-11-17 01:49:15 +08:00
|
|
|
retcode = Curl_ssl_addsessionid(conn, our_ssl_sessionid, 0, sockindex);
|
2016-06-13 11:47:12 +08:00
|
|
|
Curl_ssl_sessionid_unlock(conn);
|
|
|
|
if(retcode) {
|
|
|
|
free(our_ssl_sessionid);
|
|
|
|
failf(data, "failed to store ssl session");
|
|
|
|
return retcode;
|
|
|
|
}
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_done;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-07 21:16:01 +08:00
|
|
|
static ssize_t mbed_send(struct connectdata *conn, int sockindex,
|
|
|
|
const void *mem, size_t len,
|
|
|
|
CURLcode *curlcode)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
ret = mbedtls_ssl_write(&conn->ssl[sockindex].ssl,
|
2016-02-10 17:46:57 +08:00
|
|
|
(unsigned char *)mem, len);
|
2015-10-19 20:25:34 +08:00
|
|
|
|
|
|
|
if(ret < 0) {
|
|
|
|
*curlcode = (ret == MBEDTLS_ERR_SSL_WANT_WRITE) ?
|
|
|
|
CURLE_AGAIN : CURLE_SEND_ERROR;
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-21 21:47:12 +08:00
|
|
|
void Curl_mbedtls_close_all(struct Curl_easy *data)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_mbedtls_close(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2016-02-02 06:13:10 +08:00
|
|
|
mbedtls_pk_free(&conn->ssl[sockindex].pk);
|
2015-10-19 20:25:34 +08:00
|
|
|
mbedtls_x509_crt_free(&conn->ssl[sockindex].clicert);
|
|
|
|
mbedtls_x509_crt_free(&conn->ssl[sockindex].cacert);
|
|
|
|
mbedtls_x509_crl_free(&conn->ssl[sockindex].crl);
|
2016-02-02 06:13:10 +08:00
|
|
|
mbedtls_ssl_config_free(&conn->ssl[sockindex].config);
|
2015-10-19 20:25:34 +08:00
|
|
|
mbedtls_ssl_free(&conn->ssl[sockindex].ssl);
|
2016-02-02 06:13:10 +08:00
|
|
|
mbedtls_ctr_drbg_free(&conn->ssl[sockindex].ctr_drbg);
|
|
|
|
#ifndef THREADING_SUPPORT
|
|
|
|
mbedtls_entropy_free(&conn->ssl[sockindex].entropy);
|
|
|
|
#endif /* THREADING_SUPPORT */
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:16:01 +08:00
|
|
|
static ssize_t mbed_recv(struct connectdata *conn, int num,
|
|
|
|
char *buf, size_t buffersize,
|
|
|
|
CURLcode *curlcode)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
ssize_t len = -1;
|
|
|
|
|
|
|
|
memset(buf, 0, buffersize);
|
|
|
|
ret = mbedtls_ssl_read(&conn->ssl[num].ssl, (unsigned char *)buf,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_mbedtls_session_free(void *ptr)
|
|
|
|
{
|
2016-04-28 14:57:12 +08:00
|
|
|
mbedtls_ssl_session_free(ptr);
|
2015-10-19 20:25:34 +08:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t Curl_mbedtls_version(char *buffer, size_t size)
|
|
|
|
{
|
|
|
|
unsigned int version = mbedtls_version_get_number();
|
|
|
|
return snprintf(buffer, size, "mbedTLS/%d.%d.%d", version>>24,
|
|
|
|
(version>>16)&0xff, (version>>8)&0xff);
|
|
|
|
}
|
|
|
|
|
2017-01-27 15:39:28 +08:00
|
|
|
CURLcode Curl_mbedtls_random(struct Curl_easy *data, unsigned char *entropy,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
#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);
|
|
|
|
errorbuf[0]=0;
|
|
|
|
|
|
|
|
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 20:25:34 +08:00
|
|
|
static CURLcode
|
2016-04-07 21:16:01 +08:00
|
|
|
mbed_connect_common(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
bool nonblocking,
|
|
|
|
bool *done)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
|
|
|
CURLcode retcode;
|
2016-06-21 21:47:12 +08:00
|
|
|
struct Curl_easy *data = conn->data;
|
2015-10-19 20:25:34 +08:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
|
|
|
long timeout_ms;
|
|
|
|
int what;
|
|
|
|
|
|
|
|
/* check if the connection has already been established */
|
|
|
|
if(ssl_connection_complete == connssl->state) {
|
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ssl_connect_1==connssl->connecting_state) {
|
|
|
|
/* 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 21:16:01 +08:00
|
|
|
retcode = mbed_connect_step1(conn, sockindex);
|
2015-10-19 20:25:34 +08: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) {
|
|
|
|
|
|
|
|
curl_socket_t writefd = ssl_connect_2_writing==
|
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
|
|
|
curl_socket_t readfd = ssl_connect_2_reading==
|
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
|
|
|
|
2016-10-18 16:58:58 +08:00
|
|
|
what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
|
|
|
|
nonblocking ? 0 : timeout_ms);
|
2015-10-19 20:25:34 +08: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 21:16:01 +08:00
|
|
|
retcode = mbed_connect_step2(conn, sockindex);
|
2015-10-19 20:25:34 +08: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. */
|
|
|
|
|
|
|
|
if(ssl_connect_3==connssl->connecting_state) {
|
2016-04-07 21:16:01 +08:00
|
|
|
retcode = mbed_connect_step3(conn, sockindex);
|
2015-10-19 20:25:34 +08:00
|
|
|
if(retcode)
|
|
|
|
return retcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ssl_connect_done==connssl->connecting_state) {
|
|
|
|
connssl->state = ssl_connection_complete;
|
2016-04-07 21:16:01 +08:00
|
|
|
conn->recv[sockindex] = mbed_recv;
|
|
|
|
conn->send[sockindex] = mbed_send;
|
2015-10-19 20:25:34 +08:00
|
|
|
*done = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*done = FALSE;
|
|
|
|
|
|
|
|
/* Reset our connect state machine */
|
|
|
|
connssl->connecting_state = ssl_connect_1;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode
|
|
|
|
Curl_mbedtls_connect_nonblocking(struct connectdata *conn,
|
2016-02-10 17:46:57 +08:00
|
|
|
int sockindex,
|
|
|
|
bool *done)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
2016-04-07 21:16:01 +08:00
|
|
|
return mbed_connect_common(conn, sockindex, TRUE, done);
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CURLcode
|
|
|
|
Curl_mbedtls_connect(struct connectdata *conn,
|
2016-02-10 17:46:57 +08:00
|
|
|
int sockindex)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
|
|
|
CURLcode retcode;
|
|
|
|
bool done = FALSE;
|
|
|
|
|
2016-04-07 21:16:01 +08:00
|
|
|
retcode = mbed_connect_common(conn, sockindex, FALSE, &done);
|
2015-10-19 20:25:34 +08:00
|
|
|
if(retcode)
|
|
|
|
return retcode;
|
|
|
|
|
|
|
|
DEBUGASSERT(done);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return 0 error initializing SSL
|
|
|
|
* return 1 SSL initialized successfully
|
|
|
|
*/
|
2016-04-07 21:19:35 +08:00
|
|
|
int Curl_mbedtls_init(void)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
2015-10-23 22:14:29 +08:00
|
|
|
return Curl_polarsslthreadlock_thread_setup();
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 21:19:35 +08:00
|
|
|
void Curl_mbedtls_cleanup(void)
|
2015-10-19 20:25:34 +08:00
|
|
|
{
|
2015-10-23 22:14:29 +08:00
|
|
|
(void)Curl_polarsslthreadlock_thread_cleanup();
|
2015-10-19 20:25:34 +08:00
|
|
|
}
|
|
|
|
|
2016-04-07 22:10:10 +08:00
|
|
|
int Curl_mbedtls_data_pending(const struct connectdata *conn, int sockindex)
|
|
|
|
{
|
2017-02-09 05:09:31 +08:00
|
|
|
return mbedtls_ssl_get_bytes_avail(&conn->ssl[sockindex].ssl) != 0;
|
2016-04-07 22:10:10 +08:00
|
|
|
}
|
2016-04-07 21:19:35 +08:00
|
|
|
|
2015-10-19 20:25:34 +08:00
|
|
|
#endif /* USE_MBEDTLS */
|