2010-05-05 16:30:46 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2018-05-04 06:10:39 -04:00
|
|
|
* Copyright (C) 2012 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2015-10-23 10:14:29 -04:00
|
|
|
* Copyright (C) 2010 - 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com>
|
2010-05-05 16:30:46 -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.
|
2010-05-05 16:30:46 -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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source file for all PolarSSL-specific code for the TLS/SSL layer. No code
|
2013-12-25 05:30:51 -05:00
|
|
|
* but vtls.c should ever call or use these functions.
|
2010-05-05 16:30:46 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2011-07-26 11:23:27 -04:00
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
#ifdef USE_POLARSSL
|
|
|
|
#include <polarssl/net.h>
|
|
|
|
#include <polarssl/ssl.h>
|
|
|
|
#include <polarssl/certs.h>
|
|
|
|
#include <polarssl/x509.h>
|
2011-09-13 12:44:41 -04:00
|
|
|
#include <polarssl/version.h>
|
2016-04-21 21:21:45 -04:00
|
|
|
#include <polarssl/sha256.h>
|
2011-09-13 12:44:41 -04:00
|
|
|
|
2014-03-17 15:48:06 -04:00
|
|
|
#if POLARSSL_VERSION_NUMBER < 0x01030000
|
|
|
|
#error too old PolarSSL
|
|
|
|
#endif
|
2013-01-28 09:46:07 -05:00
|
|
|
|
2014-03-17 15:48:06 -04:00
|
|
|
#include <polarssl/error.h>
|
2012-03-27 08:10:32 -04:00
|
|
|
#include <polarssl/entropy.h>
|
|
|
|
#include <polarssl/ctr_drbg.h>
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "polarssl.h"
|
2013-12-25 05:20:39 -05:00
|
|
|
#include "vtls.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "parsedate.h"
|
|
|
|
#include "connect.h" /* for the connect timeout */
|
|
|
|
#include "select.h"
|
2016-10-31 07:51:45 -04:00
|
|
|
#include "strcase.h"
|
2013-03-08 08:02:01 -05:00
|
|
|
#include "polarssl_threadlock.h"
|
2015-03-03 17:17:43 -05:00
|
|
|
#include "curl_printf.h"
|
2010-05-05 16:30:46 -04:00
|
|
|
#include "curl_memory.h"
|
|
|
|
/* The last #include file should be: */
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h"
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2016-04-21 21:21:45 -04: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 * POLARSSL_MPI_MAX_SIZE)
|
|
|
|
#define ECP_PUB_DER_MAX_BYTES (30 + 2 * POLARSSL_ECP_MAX_BYTES)
|
|
|
|
|
|
|
|
#define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
|
|
|
|
RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES)
|
|
|
|
|
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 {
|
|
|
|
ctr_drbg_context ctr_drbg;
|
|
|
|
entropy_context entropy;
|
|
|
|
ssl_context ssl;
|
|
|
|
int server_fd;
|
|
|
|
x509_crt cacert;
|
|
|
|
x509_crt clicert;
|
|
|
|
x509_crl crl;
|
|
|
|
rsa_context rsa;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define BACKEND connssl->backend
|
2017-06-21 06:41:18 -04:00
|
|
|
|
2013-02-19 07:11:55 -05:00
|
|
|
/* apply threading? */
|
|
|
|
#if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
|
|
|
|
#define THREADING_SUPPORT
|
|
|
|
#endif
|
|
|
|
|
2016-10-18 07:17:05 -04:00
|
|
|
#ifndef POLARSSL_ERROR_C
|
|
|
|
#define error_strerror(x,y,z)
|
|
|
|
#endif /* POLARSSL_ERROR_C */
|
|
|
|
|
|
|
|
|
2014-03-17 15:48:06 -04:00
|
|
|
#if defined(THREADING_SUPPORT)
|
2013-01-28 12:31:22 -05:00
|
|
|
static entropy_context entropy;
|
|
|
|
|
|
|
|
static int entropy_init_initialized = 0;
|
|
|
|
|
|
|
|
/* start of entropy_init_mutex() */
|
|
|
|
static void entropy_init_mutex(entropy_context *ctx)
|
|
|
|
{
|
2013-02-13 05:26:16 -05:00
|
|
|
/* lock 0 = entropy_init_mutex() */
|
2015-10-23 10:14:29 -04:00
|
|
|
Curl_polarsslthreadlock_lock_function(0);
|
2013-02-13 05:26:16 -05:00
|
|
|
if(entropy_init_initialized == 0) {
|
|
|
|
entropy_init(ctx);
|
|
|
|
entropy_init_initialized = 1;
|
|
|
|
}
|
2015-10-23 10:14:29 -04:00
|
|
|
Curl_polarsslthreadlock_unlock_function(0);
|
2013-01-28 12:31:22 -05:00
|
|
|
}
|
|
|
|
/* end of entropy_init_mutex() */
|
|
|
|
|
|
|
|
/* start of entropy_func_mutex() */
|
|
|
|
static int entropy_func_mutex(void *data, unsigned char *output, size_t len)
|
|
|
|
{
|
2016-10-18 07:27:37 -04:00
|
|
|
int ret;
|
|
|
|
/* lock 1 = entropy_func_mutex() */
|
|
|
|
Curl_polarsslthreadlock_lock_function(1);
|
|
|
|
ret = entropy_func(data, output, len);
|
|
|
|
Curl_polarsslthreadlock_unlock_function(1);
|
2013-02-13 05:26:16 -05:00
|
|
|
|
2016-10-18 07:27:37 -04:00
|
|
|
return ret;
|
2013-01-28 12:31:22 -05:00
|
|
|
}
|
|
|
|
/* end of entropy_func_mutex() */
|
|
|
|
|
2014-03-18 03:03:45 -04:00
|
|
|
#endif /* THREADING_SUPPORT */
|
2012-01-18 17:17:54 -05:00
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
/* Define this to enable lots of debugging for PolarSSL */
|
|
|
|
#undef POLARSSL_DEBUG
|
|
|
|
|
|
|
|
#ifdef POLARSSL_DEBUG
|
2014-03-22 11:52:48 -04:00
|
|
|
static void polarssl_debug(void *context, int level, const char *line)
|
2010-05-05 16:30:46 -04:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = NULL;
|
2010-05-05 16:30:46 -04:00
|
|
|
|
|
|
|
if(!context)
|
|
|
|
return;
|
|
|
|
|
2016-06-21 09:47:12 -04:00
|
|
|
data = (struct Curl_easy *)context;
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2014-03-22 11:52:48 -04:00
|
|
|
infof(data, "%s", line);
|
|
|
|
(void) level;
|
2010-05-05 16:30:46 -04:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
|
2014-05-19 05:12:11 -04:00
|
|
|
/* ALPN for http2? */
|
2015-02-19 13:41:48 -05:00
|
|
|
#ifdef POLARSSL_SSL_ALPN
|
|
|
|
# define HAS_ALPN
|
2014-05-19 05:12:11 -04:00
|
|
|
#endif
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static Curl_recv polarssl_recv;
|
|
|
|
static Curl_send polarssl_send;
|
|
|
|
|
2017-03-09 03:06:13 -05:00
|
|
|
static CURLcode polarssl_version_from_curl(int *polarver, long ssl_version)
|
2016-12-13 15:10:00 -05:00
|
|
|
{
|
|
|
|
switch(ssl_version) {
|
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
|
|
|
*polarver = SSL_MINOR_VERSION_1;
|
|
|
|
return CURLE_OK;
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
|
|
|
*polarver = SSL_MINOR_VERSION_2;
|
|
|
|
return CURLE_OK;
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
|
|
|
*polarver = SSL_MINOR_VERSION_3;
|
|
|
|
return CURLE_OK;
|
|
|
|
case CURL_SSLVERSION_TLSv1_3:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
2017-03-09 03:06:13 -05:00
|
|
|
set_ssl_version_min_max(struct connectdata *conn, int sockindex)
|
2016-12-13 15:10:00 -05:00
|
|
|
{
|
|
|
|
struct Curl_easy *data = conn->data;
|
|
|
|
struct ssl_connect_data* connssl = &conn->ssl[sockindex];
|
|
|
|
long ssl_version = SSL_CONN_CONFIG(version);
|
|
|
|
long ssl_version_max = SSL_CONN_CONFIG(version_max);
|
|
|
|
int ssl_min_ver = SSL_MINOR_VERSION_1;
|
|
|
|
int ssl_max_ver = SSL_MINOR_VERSION_1;
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
|
|
|
|
switch(ssl_version) {
|
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
|
|
|
ssl_version = CURL_SSLVERSION_TLSv1_0;
|
|
|
|
ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(ssl_version_max) {
|
|
|
|
case CURL_SSLVERSION_MAX_NONE:
|
|
|
|
ssl_version_max = ssl_version << 16;
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_MAX_DEFAULT:
|
|
|
|
ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = polarssl_version_from_curl(&ssl_min_ver, ssl_version);
|
|
|
|
if(result) {
|
|
|
|
failf(data, "unsupported min version passed via CURLOPT_SSLVERSION");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
result = polarssl_version_from_curl(&ssl_max_ver, ssl_version_max >> 16);
|
|
|
|
if(result) {
|
|
|
|
failf(data, "unsupported max version passed via CURLOPT_SSLVERSION");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, ssl_min_ver);
|
|
|
|
ssl_set_max_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3, ssl_max_ver);
|
2016-12-13 15:10:00 -05:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2012-03-27 08:10:32 -04:00
|
|
|
|
|
|
|
static CURLcode
|
|
|
|
polarssl_connect_step1(struct connectdata *conn,
|
2016-10-18 07:27:37 -04:00
|
|
|
int sockindex)
|
2010-05-05 16:30:46 -04:00
|
|
|
{
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2012-03-27 08:10:32 -04:00
|
|
|
struct ssl_connect_data* connssl = &conn->ssl[sockindex];
|
2016-11-16 12:49:15 -05:00
|
|
|
const char *capath = SSL_CONN_CONFIG(CApath);
|
|
|
|
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;
|
2010-05-05 16:30:46 -04:00
|
|
|
int ret = -1;
|
2013-01-28 09:46:07 -05:00
|
|
|
char errorbuf[128];
|
2017-09-09 17:09:06 -04:00
|
|
|
errorbuf[0] = 0;
|
2013-01-28 09:46:07 -05:00
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
/* PolarSSL only supports SSLv3 and TLSv1 */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2) {
|
2010-05-05 16:30:46 -04:00
|
|
|
failf(data, "PolarSSL does not support SSLv2");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-01-28 12:31:22 -05:00
|
|
|
#ifdef THREADING_SUPPORT
|
|
|
|
entropy_init_mutex(&entropy);
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if((ret = ctr_drbg_init(&BACKEND->ctr_drbg, entropy_func_mutex, &entropy,
|
2016-04-28 02:57:12 -04:00
|
|
|
NULL, 0)) != 0) {
|
2016-10-18 07:27:37 -04:00
|
|
|
error_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
|
|
|
|
-ret, errorbuf);
|
2013-01-28 12:31:22 -05:00
|
|
|
}
|
2013-01-28 10:03:40 -05:00
|
|
|
#else
|
2017-06-21 06:41:18 -04:00
|
|
|
entropy_init(&BACKEND->entropy);
|
2013-01-28 10:03:40 -05:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if((ret = ctr_drbg_init(&BACKEND->ctr_drbg, entropy_func, &BACKEND->entropy,
|
2016-04-28 02:57:12 -04:00
|
|
|
NULL, 0)) != 0) {
|
2016-10-18 07:27:37 -04:00
|
|
|
error_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
|
|
|
|
-ret, errorbuf);
|
2013-01-28 10:03:40 -05:00
|
|
|
}
|
2013-01-28 12:31:22 -05:00
|
|
|
#endif /* THREADING_SUPPORT */
|
2010-05-05 16:30:46 -04:00
|
|
|
|
|
|
|
/* Load the trusted CA */
|
2017-06-21 06:41:18 -04:00
|
|
|
memset(&BACKEND->cacert, 0, sizeof(x509_crt));
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(CAfile)) {
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = x509_crt_parse_file(&BACKEND->cacert,
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_CONN_CONFIG(CAfile));
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2012-03-27 08:10:32 -04:00
|
|
|
if(ret<0) {
|
2013-01-28 09:46:07 -05:00
|
|
|
error_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
failf(data, "Error reading ca cert file %s - PolarSSL: (-0x%04X) %s",
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_CONN_CONFIG(CAfile), -ret, errorbuf);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(verifypeer))
|
2010-05-05 16:30:46 -04:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(capath) {
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = x509_crt_parse_path(&BACKEND->cacert, capath);
|
2014-09-06 21:31:01 -04:00
|
|
|
|
|
|
|
if(ret<0) {
|
|
|
|
error_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
failf(data, "Error reading ca cert path %s - PolarSSL: (-0x%04X) %s",
|
2016-11-16 12:49:15 -05:00
|
|
|
capath, -ret, errorbuf);
|
2014-09-06 21:31:01 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(verifypeer))
|
2014-09-06 21:31:01 -04:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
/* Load the client certificate */
|
2017-06-21 06:41:18 -04:00
|
|
|
memset(&BACKEND->clicert, 0, sizeof(x509_crt));
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_SET_OPTION(cert)) {
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = x509_crt_parse_file(&BACKEND->clicert,
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_SET_OPTION(cert));
|
2010-05-05 16:30:46 -04:00
|
|
|
|
|
|
|
if(ret) {
|
2013-01-28 09:46:07 -05:00
|
|
|
error_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
failf(data, "Error reading client cert file %s - PolarSSL: (-0x%04X) %s",
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_SET_OPTION(cert), -ret, errorbuf);
|
2013-01-28 09:46:07 -05:00
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
return CURLE_SSL_CERTPROBLEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the client private key */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_SET_OPTION(key)) {
|
2014-03-18 14:18:23 -04:00
|
|
|
pk_context pk;
|
|
|
|
pk_init(&pk);
|
2016-11-16 12:49:15 -05:00
|
|
|
ret = pk_parse_keyfile(&pk, SSL_SET_OPTION(key),
|
|
|
|
SSL_SET_OPTION(key_passwd));
|
2014-03-18 14:18:23 -04:00
|
|
|
if(ret == 0 && !pk_can_do(&pk, POLARSSL_PK_RSA))
|
|
|
|
ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
|
|
|
|
if(ret == 0)
|
2017-06-21 06:41:18 -04:00
|
|
|
rsa_copy(&BACKEND->rsa, pk_rsa(pk));
|
2014-03-18 14:18:23 -04:00
|
|
|
else
|
2017-06-21 06:41:18 -04:00
|
|
|
rsa_free(&BACKEND->rsa);
|
2014-03-18 14:18:23 -04:00
|
|
|
pk_free(&pk);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
|
|
|
if(ret) {
|
2013-01-28 09:46:07 -05:00
|
|
|
error_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
failf(data, "Error reading private key %s - PolarSSL: (-0x%04X) %s",
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_SET_OPTION(key), -ret, errorbuf);
|
2013-01-28 09:46:07 -05:00
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
return CURLE_SSL_CERTPROBLEM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the CRL */
|
2017-06-21 06:41:18 -04:00
|
|
|
memset(&BACKEND->crl, 0, sizeof(x509_crl));
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_SET_OPTION(CRLfile)) {
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = x509_crl_parse_file(&BACKEND->crl,
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_SET_OPTION(CRLfile));
|
2010-05-05 16:30:46 -04:00
|
|
|
|
|
|
|
if(ret) {
|
2013-01-28 09:46:07 -05:00
|
|
|
error_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
failf(data, "Error reading CRL file %s - PolarSSL: (-0x%04X) %s",
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_SET_OPTION(CRLfile), -ret, errorbuf);
|
2013-01-28 09:46:07 -05:00
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
return CURLE_SSL_CRL_BADFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
infof(data, "PolarSSL: Connecting to %s:%d\n", hostname, port);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(ssl_init(&BACKEND->ssl)) {
|
2010-05-05 16:30:46 -04:00
|
|
|
failf(data, "PolarSSL: ssl_init failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
switch(SSL_CONN_CONFIG(version)) {
|
2014-10-24 14:26:57 -04:00
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
2015-01-02 12:33:41 -05:00
|
|
|
case CURL_SSLVERSION_TLSv1:
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3,
|
2014-10-24 14:26:57 -04:00
|
|
|
SSL_MINOR_VERSION_1);
|
|
|
|
break;
|
2014-09-01 10:06:03 -04:00
|
|
|
case CURL_SSLVERSION_SSLv3:
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_min_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3,
|
2014-09-01 10:06:03 -04:00
|
|
|
SSL_MINOR_VERSION_0);
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_max_version(&BACKEND->ssl, SSL_MAJOR_VERSION_3,
|
2015-01-02 12:33:41 -05:00
|
|
|
SSL_MINOR_VERSION_0);
|
2014-09-01 10:06:03 -04:00
|
|
|
infof(data, "PolarSSL: Forced min. SSL Version to be SSLv3\n");
|
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
2016-10-27 08:57:11 -04: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-11-07 21:51:27 -05:00
|
|
|
default:
|
|
|
|
failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2014-09-01 10:06:03 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_endpoint(&BACKEND->ssl, SSL_IS_CLIENT);
|
|
|
|
ssl_set_authmode(&BACKEND->ssl, SSL_VERIFY_OPTIONAL);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_rng(&BACKEND->ssl, ctr_drbg_random,
|
|
|
|
&BACKEND->ctr_drbg);
|
|
|
|
ssl_set_bio(&BACKEND->ssl,
|
2010-05-05 16:30:46 -04:00
|
|
|
net_recv, &conn->sock[sockindex],
|
|
|
|
net_send, &conn->sock[sockindex]);
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_ciphersuites(&BACKEND->ssl, ssl_list_ciphersuites());
|
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)) {
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = ssl_set_session(&BACKEND->ssl, old_session);
|
2016-06-12 23:47:12 -04:00
|
|
|
if(ret) {
|
2016-10-18 07:11:55 -04:00
|
|
|
Curl_ssl_sessionid_unlock(conn);
|
2016-06-12 23:47:12 -04:00
|
|
|
failf(data, "ssl_set_session returned -0x%x", -ret);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
infof(data, "PolarSSL re-using session\n");
|
2016-04-28 02:57:12 -04:00
|
|
|
}
|
2016-10-18 07:11:55 -04:00
|
|
|
Curl_ssl_sessionid_unlock(conn);
|
2010-05-05 16:30:46 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_ca_chain(&BACKEND->ssl,
|
|
|
|
&BACKEND->cacert,
|
|
|
|
&BACKEND->crl,
|
2016-11-16 12:49:15 -05:00
|
|
|
hostname);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_own_cert_rsa(&BACKEND->ssl,
|
|
|
|
&BACKEND->clicert, &BACKEND->rsa);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(ssl_set_hostname(&BACKEND->ssl, hostname)) {
|
2016-04-24 11:52:18 -04:00
|
|
|
/* 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 PolarSSL");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2010-05-05 16:30:46 -04:00
|
|
|
}
|
|
|
|
|
2014-05-19 05:12:11 -04:00
|
|
|
#ifdef HAS_ALPN
|
2016-05-09 10:50:11 -04:00
|
|
|
if(conn->bits.tls_enable_alpn) {
|
2016-11-23 01:53:24 -05:00
|
|
|
static const char *protocols[3];
|
2015-02-19 13:41:48 -05:00
|
|
|
int cur = 0;
|
|
|
|
|
|
|
|
#ifdef USE_NGHTTP2
|
2015-12-13 03:23:36 -05:00
|
|
|
if(data->set.httpversion >= CURL_HTTP_VERSION_2) {
|
2015-02-19 13:41:48 -05:00
|
|
|
protocols[cur++] = NGHTTP2_PROTO_VERSION_ID;
|
|
|
|
infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID);
|
2014-05-19 05:12:11 -04:00
|
|
|
}
|
2015-02-19 13:41:48 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
protocols[cur++] = ALPN_HTTP_1_1;
|
|
|
|
infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1);
|
|
|
|
|
|
|
|
protocols[cur] = NULL;
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_alpn_protocols(&BACKEND->ssl, protocols);
|
2014-05-19 05:12:11 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
#ifdef POLARSSL_DEBUG
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_set_dbg(&BACKEND->ssl, polarssl_debug, data);
|
2010-05-05 16:30:46 -04:00
|
|
|
#endif
|
|
|
|
|
2012-03-27 08:10:32 -04:00
|
|
|
connssl->connecting_state = ssl_connect_2;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
|
|
|
polarssl_connect_step2(struct connectdata *conn,
|
2016-10-18 07:27:37 -04:00
|
|
|
int sockindex)
|
2012-03-27 08:10:32 -04:00
|
|
|
{
|
|
|
|
int ret;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2017-08-30 04:26:37 -04:00
|
|
|
struct ssl_connect_data* connssl = &conn->ssl[sockindex];
|
2012-03-27 08:10:32 -04:00
|
|
|
char buffer[1024];
|
2016-11-25 04:47:25 -05:00
|
|
|
const char * const pinnedpubkey = SSL_IS_PROXY() ?
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
|
|
|
|
|
2012-03-27 08:10:32 -04:00
|
|
|
|
2013-01-28 09:46:07 -05:00
|
|
|
char errorbuf[128];
|
2014-09-08 04:11:34 -04:00
|
|
|
errorbuf[0] = 0;
|
2013-01-28 09:46:07 -05:00
|
|
|
|
2012-03-27 08:10:32 -04:00
|
|
|
conn->recv[sockindex] = polarssl_recv;
|
|
|
|
conn->send[sockindex] = polarssl_send;
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = ssl_handshake(&BACKEND->ssl);
|
2015-03-25 04:01:11 -04:00
|
|
|
|
|
|
|
switch(ret) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case POLARSSL_ERR_NET_WANT_READ:
|
|
|
|
connssl->connecting_state = ssl_connect_2_reading;
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
case POLARSSL_ERR_NET_WANT_WRITE:
|
|
|
|
connssl->connecting_state = ssl_connect_2_writing;
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
default:
|
2015-03-25 03:49:34 -04:00
|
|
|
error_strerror(ret, errorbuf, sizeof(errorbuf));
|
|
|
|
failf(data, "ssl_handshake returned - PolarSSL: (-0x%04X) %s",
|
|
|
|
-ret, errorbuf);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2010-11-07 22:03:11 -05:00
|
|
|
}
|
2010-05-05 16:30:46 -04:00
|
|
|
|
|
|
|
infof(data, "PolarSSL: Handshake complete, cipher is %s\n",
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_get_ciphersuite(&BACKEND->ssl) );
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = ssl_get_verify_result(&BACKEND->ssl);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
if(ret && SSL_CONN_CONFIG(verifypeer)) {
|
2010-05-05 16:30:46 -04:00
|
|
|
if(ret & BADCERT_EXPIRED)
|
2012-06-14 07:32:05 -04:00
|
|
|
failf(data, "Cert verify failed: BADCERT_EXPIRED");
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2012-04-06 09:10:59 -04:00
|
|
|
if(ret & BADCERT_REVOKED) {
|
2010-05-05 16:30:46 -04:00
|
|
|
failf(data, "Cert verify failed: BADCERT_REVOKED");
|
2012-04-06 09:10:59 -04:00
|
|
|
return CURLE_SSL_CACERT;
|
|
|
|
}
|
2010-05-05 16:30:46 -04:00
|
|
|
|
|
|
|
if(ret & BADCERT_CN_MISMATCH)
|
|
|
|
failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
|
|
|
|
|
|
|
|
if(ret & BADCERT_NOT_TRUSTED)
|
|
|
|
failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
|
|
|
|
|
2012-03-27 08:10:32 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
2010-05-05 16:30:46 -04:00
|
|
|
}
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(ssl_get_peer_cert(&(BACKEND->ssl))) {
|
2010-05-05 16:30:46 -04:00
|
|
|
/* If the session was resumed, there will be no peer certs */
|
|
|
|
memset(buffer, 0, sizeof(buffer));
|
|
|
|
|
2014-03-18 14:18:23 -04:00
|
|
|
if(x509_crt_info(buffer, sizeof(buffer), (char *)"* ",
|
2017-06-21 06:41:18 -04:00
|
|
|
ssl_get_peer_cert(&(BACKEND->ssl))) != -1)
|
2010-05-05 16:30:46 -04:00
|
|
|
infof(data, "Dumping cert info:\n%s\n", buffer);
|
|
|
|
}
|
|
|
|
|
2016-04-21 21:21:45 -04:00
|
|
|
/* adapted from mbedtls.c */
|
2016-11-25 04:47:25 -05:00
|
|
|
if(pinnedpubkey) {
|
2016-04-21 21:21:45 -04:00
|
|
|
int size;
|
|
|
|
CURLcode result;
|
|
|
|
x509_crt *p;
|
|
|
|
unsigned char pubkey[PUB_DER_MAX_BYTES];
|
|
|
|
const x509_crt *peercert;
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
peercert = ssl_get_peer_cert(&BACKEND->ssl);
|
2016-04-21 21:21:45 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
x509_crt_init(p);
|
|
|
|
|
|
|
|
/* Make a copy of our const peercert because pk_write_pubkey_der
|
|
|
|
needs a non-const key, for now.
|
|
|
|
https://github.com/ARMmbed/mbedtls/issues/396 */
|
|
|
|
if(x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) {
|
|
|
|
failf(data, "Failed copying peer certificate");
|
|
|
|
x509_crt_free(p);
|
|
|
|
free(p);
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES);
|
|
|
|
|
|
|
|
if(size <= 0) {
|
|
|
|
failf(data, "Failed copying public key from peer certificate");
|
|
|
|
x509_crt_free(p);
|
|
|
|
free(p);
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-04-21 21:21:45 -04:00
|
|
|
&pubkey[PUB_DER_MAX_BYTES - size], size);
|
|
|
|
if(result) {
|
|
|
|
x509_crt_free(p);
|
|
|
|
free(p);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
x509_crt_free(p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
2014-05-19 05:12:11 -04:00
|
|
|
#ifdef HAS_ALPN
|
2016-05-09 10:50:11 -04:00
|
|
|
if(conn->bits.tls_enable_alpn) {
|
2017-06-21 06:41:18 -04:00
|
|
|
const char *next_protocol = ssl_get_alpn_protocol(&BACKEND->ssl);
|
2014-05-19 05:12:11 -04:00
|
|
|
|
|
|
|
if(next_protocol != NULL) {
|
|
|
|
infof(data, "ALPN, server accepted to use %s\n", next_protocol);
|
|
|
|
|
2015-02-19 13:41:48 -05:00
|
|
|
#ifdef USE_NGHTTP2
|
2015-02-19 13:55:59 -05:00
|
|
|
if(!strncmp(next_protocol, NGHTTP2_PROTO_VERSION_ID,
|
2014-05-19 05:12:11 -04:00
|
|
|
NGHTTP2_PROTO_VERSION_ID_LEN)) {
|
2015-12-13 03:23:36 -05:00
|
|
|
conn->negnpn = CURL_HTTP_VERSION_2;
|
2014-05-19 05:12:11 -04:00
|
|
|
}
|
2015-02-19 13:41:48 -05:00
|
|
|
else
|
|
|
|
#endif
|
2016-10-18 07:27:37 -04:00
|
|
|
if(!strncmp(next_protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH)) {
|
|
|
|
conn->negnpn = CURL_HTTP_VERSION_1_1;
|
|
|
|
}
|
2014-05-19 05:12:11 -04:00
|
|
|
}
|
2015-02-19 13:41:48 -05:00
|
|
|
else
|
2014-05-19 05:12:11 -04:00
|
|
|
infof(data, "ALPN, server did not agree to a protocol\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-03-27 08:10:32 -04:00
|
|
|
connssl->connecting_state = ssl_connect_3;
|
|
|
|
infof(data, "SSL connected\n");
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CURLcode
|
|
|
|
polarssl_connect_step3(struct connectdata *conn,
|
2016-10-18 07:27:37 -04:00
|
|
|
int sockindex)
|
2012-03-27 08:10:32 -04:00
|
|
|
{
|
2016-04-28 02:57:12 -04:00
|
|
|
CURLcode retcode = CURLE_OK;
|
2012-03-27 08:10:32 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2012-03-27 08:10:32 -04:00
|
|
|
|
|
|
|
DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2017-03-22 01:59:49 -04:00
|
|
|
if(SSL_SET_OPTION(primary.sessionid)) {
|
2016-06-12 23:47:12 -04:00
|
|
|
int ret;
|
|
|
|
ssl_session *our_ssl_sessionid;
|
|
|
|
void *old_ssl_sessionid = NULL;
|
2014-12-25 12:15:15 -05:00
|
|
|
|
2018-04-14 16:42:04 -04:00
|
|
|
our_ssl_sessionid = calloc(1, sizeof(ssl_session));
|
2016-06-12 23:47:12 -04:00
|
|
|
if(!our_ssl_sessionid)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2010-05-05 16:30:46 -04:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = ssl_get_session(&BACKEND->ssl, our_ssl_sessionid);
|
2016-06-12 23:47:12 -04:00
|
|
|
if(ret) {
|
|
|
|
failf(data, "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);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
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) {
|
|
|
|
free(our_ssl_sessionid);
|
|
|
|
failf(data, "failed to store ssl session");
|
|
|
|
return retcode;
|
|
|
|
}
|
2010-05-05 16:30:46 -04:00
|
|
|
}
|
|
|
|
|
2012-03-27 08:10:32 -04:00
|
|
|
connssl->connecting_state = ssl_connect_done;
|
|
|
|
|
2010-05-05 16:30:46 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static ssize_t polarssl_send(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
const void *mem,
|
|
|
|
size_t len,
|
|
|
|
CURLcode *curlcode)
|
2010-05-05 16:30:46 -04:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2010-05-05 16:30:46 -04:00
|
|
|
int ret = -1;
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = ssl_write(&BACKEND->ssl,
|
2010-05-05 16:30:46 -04:00
|
|
|
(unsigned char *)mem, len);
|
|
|
|
|
|
|
|
if(ret < 0) {
|
2011-09-13 12:44:41 -04:00
|
|
|
*curlcode = (ret == POLARSSL_ERR_NET_WANT_WRITE) ?
|
2010-05-07 09:05:34 -04:00
|
|
|
CURLE_AGAIN : CURLE_SEND_ERROR;
|
2010-05-05 16:30:46 -04:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_polarssl_close(struct connectdata *conn, int sockindex)
|
2010-05-05 16:30:46 -04:00
|
|
|
{
|
2017-08-30 04:26:37 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2017-06-21 06:41:18 -04:00
|
|
|
rsa_free(&BACKEND->rsa);
|
|
|
|
x509_crt_free(&BACKEND->clicert);
|
|
|
|
x509_crt_free(&BACKEND->cacert);
|
|
|
|
x509_crl_free(&BACKEND->crl);
|
|
|
|
ssl_free(&BACKEND->ssl);
|
2010-05-05 16:30:46 -04:00
|
|
|
}
|
|
|
|
|
2010-05-07 09:05:34 -04:00
|
|
|
static ssize_t polarssl_recv(struct connectdata *conn,
|
|
|
|
int num,
|
|
|
|
char *buf,
|
|
|
|
size_t buffersize,
|
|
|
|
CURLcode *curlcode)
|
2010-05-05 16:30:46 -04:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[num];
|
2010-05-05 16:30:46 -04:00
|
|
|
int ret = -1;
|
|
|
|
ssize_t len = -1;
|
|
|
|
|
|
|
|
memset(buf, 0, buffersize);
|
2017-06-21 06:41:18 -04:00
|
|
|
ret = ssl_read(&BACKEND->ssl, (unsigned char *)buf, buffersize);
|
2010-05-05 16:30:46 -04:00
|
|
|
|
|
|
|
if(ret <= 0) {
|
2011-02-18 00:46:59 -05:00
|
|
|
if(ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
|
|
|
|
return 0;
|
|
|
|
|
2011-09-13 12:44:41 -04:00
|
|
|
*curlcode = (ret == POLARSSL_ERR_NET_WANT_READ) ?
|
2010-05-07 09:05:34 -04:00
|
|
|
CURLE_AGAIN : CURLE_RECV_ERROR;
|
2010-05-05 16:30:46 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ret;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_polarssl_session_free(void *ptr)
|
2010-05-05 16:30:46 -04:00
|
|
|
{
|
2016-04-28 02:57:12 -04:00
|
|
|
ssl_session_free(ptr);
|
2010-05-05 16:30:46 -04:00
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
2015-03-25 04:19:57 -04:00
|
|
|
/* 1.3.10 was the first rebranded version. All new releases (in 1.3 branch and
|
|
|
|
higher) will be mbed TLS branded.. */
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static size_t Curl_polarssl_version(char *buffer, size_t size)
|
2010-05-05 16:30:46 -04:00
|
|
|
{
|
2012-04-06 08:44:36 -04:00
|
|
|
unsigned int version = version_get_number();
|
2015-03-25 04:19:57 -04:00
|
|
|
return snprintf(buffer, size, "%s/%d.%d.%d",
|
|
|
|
version >= 0x01030A00?"mbedTLS":"PolarSSL",
|
|
|
|
version>>24, (version>>16)&0xff, (version>>8)&0xff);
|
2010-05-05 16:30:46 -04:00
|
|
|
}
|
|
|
|
|
2012-03-27 08:10:32 -04:00
|
|
|
static CURLcode
|
|
|
|
polarssl_connect_common(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
bool nonblocking,
|
|
|
|
bool *done)
|
|
|
|
{
|
2014-12-27 12:20:38 -05:00
|
|
|
CURLcode result;
|
2016-06-21 09:47:12 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2012-03-27 08:10:32 -04: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;
|
|
|
|
}
|
|
|
|
|
2014-12-27 12:20:38 -05:00
|
|
|
if(ssl_connect_1 == connssl->connecting_state) {
|
2012-03-27 08:10:32 -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;
|
|
|
|
}
|
2014-12-27 12:20:38 -05:00
|
|
|
|
|
|
|
result = polarssl_connect_step1(conn, sockindex);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2012-03-27 08:10:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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. */
|
2014-12-27 12:20:38 -05:00
|
|
|
if(connssl->connecting_state == ssl_connect_2_reading ||
|
|
|
|
connssl->connecting_state == ssl_connect_2_writing) {
|
2012-03-27 08:10:32 -04:00
|
|
|
|
2017-09-09 17:09:06 -04:00
|
|
|
curl_socket_t writefd = ssl_connect_2_writing ==
|
2012-03-27 08:10:32 -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 ==
|
2012-03-27 08:10:32 -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,
|
|
|
|
nonblocking?0:timeout_ms);
|
2012-03-27 08:10:32 -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.
|
|
|
|
*/
|
2014-12-27 12:20:38 -05:00
|
|
|
result = polarssl_connect_step2(conn, sockindex);
|
|
|
|
if(result || (nonblocking &&
|
|
|
|
(ssl_connect_2 == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_reading == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_writing == connssl->connecting_state)))
|
|
|
|
return result;
|
2012-03-27 08:10:32 -04:00
|
|
|
|
|
|
|
} /* repeat step2 until all transactions are done. */
|
|
|
|
|
2014-12-27 12:20:38 -05:00
|
|
|
if(ssl_connect_3 == connssl->connecting_state) {
|
|
|
|
result = polarssl_connect_step3(conn, sockindex);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2012-03-27 08:10:32 -04:00
|
|
|
}
|
|
|
|
|
2014-12-27 12:20:38 -05:00
|
|
|
if(ssl_connect_done == connssl->connecting_state) {
|
2012-03-27 08:10:32 -04:00
|
|
|
connssl->state = ssl_connection_complete;
|
|
|
|
conn->recv[sockindex] = polarssl_recv;
|
|
|
|
conn->send[sockindex] = polarssl_send;
|
|
|
|
*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_polarssl_connect_nonblocking(struct connectdata *conn,
|
|
|
|
int sockindex, bool *done)
|
2012-03-27 08:10:32 -04:00
|
|
|
{
|
|
|
|
return polarssl_connect_common(conn, sockindex, TRUE, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static CURLcode Curl_polarssl_connect(struct connectdata *conn, int sockindex)
|
2012-03-27 08:10:32 -04:00
|
|
|
{
|
2014-12-27 12:20:38 -05:00
|
|
|
CURLcode result;
|
2012-03-27 08:10:32 -04:00
|
|
|
bool done = FALSE;
|
|
|
|
|
2014-12-27 12:20:38 -05:00
|
|
|
result = polarssl_connect_common(conn, sockindex, FALSE, &done);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2012-03-27 08:10:32 -04:00
|
|
|
|
|
|
|
DEBUGASSERT(done);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-28 12:31:22 -05:00
|
|
|
/*
|
|
|
|
* return 0 error initializing SSL
|
|
|
|
* return 1 SSL initialized successfully
|
|
|
|
*/
|
2017-06-23 07:19:00 -04:00
|
|
|
static int Curl_polarssl_init(void)
|
2013-01-28 12:31:22 -05:00
|
|
|
{
|
2015-10-23 10:14:29 -04:00
|
|
|
return Curl_polarsslthreadlock_thread_setup();
|
2013-01-28 12:31:22 -05:00
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static void Curl_polarssl_cleanup(void)
|
2013-01-28 12:31:22 -05:00
|
|
|
{
|
2015-10-23 10:14:29 -04:00
|
|
|
(void)Curl_polarsslthreadlock_thread_cleanup();
|
2013-01-28 12:31:22 -05:00
|
|
|
}
|
|
|
|
|
2017-06-23 07:19:00 -04:00
|
|
|
static bool Curl_polarssl_data_pending(const struct connectdata *conn,
|
|
|
|
int sockindex)
|
2017-01-28 13:45:52 -05:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
const struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
return ssl_get_bytes_avail(&BACKEND->ssl) != 0;
|
2017-01-28 13:45:52 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 13:33:00 -04:00
|
|
|
static CURLcode Curl_polarssl_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;
|
2017-06-22 19:04:56 -04:00
|
|
|
sha256(input, inputlen, sha256sum, 0);
|
2018-04-02 13:33:00 -04:00
|
|
|
return CURLE_OK;
|
2017-06-22 19:04:56 -04:00
|
|
|
}
|
|
|
|
|
2017-06-23 10:05:26 -04:00
|
|
|
static void *Curl_polarssl_get_internals(struct ssl_connect_data *connssl,
|
|
|
|
CURLINFO info UNUSED_PARAM)
|
|
|
|
{
|
|
|
|
(void)info;
|
2017-06-21 06:41:18 -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_polarssl = {
|
2017-07-15 07:49:30 -04:00
|
|
|
{ CURLSSLBACKEND_POLARSSL, "polarssl" }, /* info */
|
2017-06-22 10:45:34 -04:00
|
|
|
|
2018-05-04 06:10:39 -04:00
|
|
|
SSLSUPP_CA_PATH |
|
|
|
|
SSLSUPP_PINNEDPUBKEY,
|
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_polarssl_init, /* init */
|
|
|
|
Curl_polarssl_cleanup, /* cleanup */
|
|
|
|
Curl_polarssl_version, /* version */
|
|
|
|
Curl_none_check_cxn, /* check_cxn */
|
|
|
|
Curl_none_shutdown, /* shutdown */
|
|
|
|
Curl_polarssl_data_pending, /* data_pending */
|
|
|
|
/* This might cause libcurl to use a weeker random!
|
|
|
|
* TODO: use Polarssl's CTR-DRBG or HMAC-DRBG
|
|
|
|
*/
|
|
|
|
Curl_none_random, /* random */
|
|
|
|
Curl_none_cert_status_request, /* cert_status_request */
|
|
|
|
Curl_polarssl_connect, /* connect */
|
|
|
|
Curl_polarssl_connect_nonblocking, /* connect_nonblocking */
|
2017-06-23 10:05:26 -04:00
|
|
|
Curl_polarssl_get_internals, /* get_internals */
|
2017-10-19 14:55:17 -04:00
|
|
|
Curl_polarssl_close, /* close_one */
|
2017-06-22 10:45:34 -04:00
|
|
|
Curl_none_close_all, /* close_all */
|
|
|
|
Curl_polarssl_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_polarssl_sha256sum /* sha256sum */
|
2017-06-22 10:45:34 -04:00
|
|
|
};
|
|
|
|
|
2013-01-28 12:31:22 -05:00
|
|
|
#endif /* USE_POLARSSL */
|