2011-02-28 21:02:47 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2021-01-05 03:39:59 -05:00
|
|
|
* Copyright (C) 1998 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-02-28 21:02:47 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2011-02-28 21:02:47 -05: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.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
2019-06-02 10:55:05 -04:00
|
|
|
* Source file for all wolfSSL 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.
|
2011-02-28 21:02:47 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2011-07-26 11:23:27 -04:00
|
|
|
|
2019-06-02 10:55:05 -04:00
|
|
|
#ifdef USE_WOLFSSL
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2015-04-17 15:22:48 -04:00
|
|
|
#define WOLFSSL_OPTIONS_IGNORE_SYS
|
2019-06-02 10:55:05 -04:00
|
|
|
#include <wolfssl/version.h>
|
|
|
|
#include <wolfssl/options.h>
|
2015-04-17 15:22:48 -04:00
|
|
|
|
2017-06-02 17:02:54 -04:00
|
|
|
/* To determine what functions are available we rely on one or both of:
|
2019-06-02 10:55:05 -04:00
|
|
|
- the user's options.h generated by wolfSSL
|
2017-06-02 17:02:54 -04:00
|
|
|
- the symbols detected by curl's configure
|
|
|
|
Since they are markedly different from one another, and one or the other may
|
|
|
|
not be available, we do some checking below to bring things in sync. */
|
|
|
|
|
|
|
|
/* HAVE_ALPN is wolfSSL's build time symbol for enabling ALPN in options.h. */
|
|
|
|
#ifndef HAVE_ALPN
|
|
|
|
#ifdef HAVE_WOLFSSL_USEALPN
|
|
|
|
#define HAVE_ALPN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* WOLFSSL_ALLOW_SSLV3 is wolfSSL's build time symbol for enabling SSLv3 in
|
|
|
|
options.h, but is only seen in >= 3.6.6 since that's when they started
|
|
|
|
disabling SSLv3 by default. */
|
|
|
|
#ifndef WOLFSSL_ALLOW_SSLV3
|
2019-06-02 10:55:05 -04:00
|
|
|
#if (LIBWOLFSSL_VERSION_HEX < 0x03006006) || \
|
|
|
|
defined(HAVE_WOLFSSLV3_CLIENT_METHOD)
|
2017-06-02 17:02:54 -04:00
|
|
|
#define WOLFSSL_ALLOW_SSLV3
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-08-24 08:00:42 -04:00
|
|
|
#include <limits.h>
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "inet_pton.h"
|
2013-12-25 05:20:39 -05:00
|
|
|
#include "vtls.h"
|
2020-05-03 18:16:40 -04:00
|
|
|
#include "keylog.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"
|
2015-04-05 01:48:16 -04:00
|
|
|
#include "x509asn1.h"
|
2015-03-03 17:17:43 -05:00
|
|
|
#include "curl_printf.h"
|
2019-04-30 05:14:38 -04:00
|
|
|
#include "multiif.h"
|
2014-04-23 05:01:30 -04:00
|
|
|
|
2019-06-02 10:55:05 -04:00
|
|
|
#include <wolfssl/openssl/ssl.h>
|
|
|
|
#include <wolfssl/ssl.h>
|
|
|
|
#include <wolfssl/error-ssl.h>
|
|
|
|
#include "wolfssl.h"
|
2016-05-01 11:05:38 -04:00
|
|
|
|
2015-03-24 18:12:03 -04:00
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
2014-04-23 05:01:30 -04:00
|
|
|
#include "memdebug.h"
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2017-06-20 05:32:53 -04:00
|
|
|
/* KEEP_PEER_CERT is a product of the presence of build time symbol
|
|
|
|
OPENSSL_EXTRA without NO_CERTS, depending on the version. KEEP_PEER_CERT is
|
|
|
|
in wolfSSL's settings.h, and the latter two are build time symbols in
|
|
|
|
options.h. */
|
|
|
|
#ifndef KEEP_PEER_CERT
|
2019-06-02 10:55:05 -04:00
|
|
|
#if defined(HAVE_WOLFSSL_GET_PEER_CERTIFICATE) || \
|
2017-06-20 05:32:53 -04:00
|
|
|
(defined(OPENSSL_EXTRA) && !defined(NO_CERTS))
|
|
|
|
#define KEEP_PEER_CERT
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
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 {
|
|
|
|
SSL_CTX* ctx;
|
|
|
|
SSL* handle;
|
|
|
|
};
|
|
|
|
|
2019-06-02 10:55:05 -04:00
|
|
|
static Curl_recv wolfssl_recv;
|
|
|
|
static Curl_send wolfssl_send;
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2020-05-03 18:16:40 -04:00
|
|
|
#ifdef OPENSSL_EXTRA
|
|
|
|
/*
|
|
|
|
* Availability note:
|
|
|
|
* The TLS 1.3 secret callback (wolfSSL_set_tls13_secret_cb) was added in
|
|
|
|
* WolfSSL 4.4.0, but requires the -DHAVE_SECRET_CALLBACK build option. If that
|
|
|
|
* option is not set, then TLS 1.3 will not be logged.
|
|
|
|
* For TLS 1.2 and before, we use wolfSSL_get_keys().
|
|
|
|
* SSL_get_client_random and wolfSSL_get_keys require OPENSSL_EXTRA
|
|
|
|
* (--enable-opensslextra or --enable-all).
|
|
|
|
*/
|
|
|
|
#if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
|
|
|
|
static int
|
|
|
|
wolfssl_tls13_secret_callback(SSL *ssl, int id, const unsigned char *secret,
|
|
|
|
int secretSz, void *ctx)
|
|
|
|
{
|
|
|
|
const char *label;
|
|
|
|
unsigned char client_random[SSL3_RANDOM_SIZE];
|
|
|
|
(void)ctx;
|
|
|
|
|
|
|
|
if(!ssl || !Curl_tls_keylog_enabled()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(id) {
|
|
|
|
case CLIENT_EARLY_TRAFFIC_SECRET:
|
|
|
|
label = "CLIENT_EARLY_TRAFFIC_SECRET";
|
|
|
|
break;
|
|
|
|
case CLIENT_HANDSHAKE_TRAFFIC_SECRET:
|
|
|
|
label = "CLIENT_HANDSHAKE_TRAFFIC_SECRET";
|
|
|
|
break;
|
|
|
|
case SERVER_HANDSHAKE_TRAFFIC_SECRET:
|
|
|
|
label = "SERVER_HANDSHAKE_TRAFFIC_SECRET";
|
|
|
|
break;
|
|
|
|
case CLIENT_TRAFFIC_SECRET:
|
|
|
|
label = "CLIENT_TRAFFIC_SECRET_0";
|
|
|
|
break;
|
|
|
|
case SERVER_TRAFFIC_SECRET:
|
|
|
|
label = "SERVER_TRAFFIC_SECRET_0";
|
|
|
|
break;
|
|
|
|
case EARLY_EXPORTER_SECRET:
|
|
|
|
label = "EARLY_EXPORTER_SECRET";
|
|
|
|
break;
|
|
|
|
case EXPORTER_SECRET:
|
|
|
|
label = "EXPORTER_SECRET";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(SSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE) == 0) {
|
|
|
|
/* Should never happen as wolfSSL_KeepArrays() was called before. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Curl_tls_keylog_write(label, client_random, secret, secretSz);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13) */
|
|
|
|
|
|
|
|
static void
|
|
|
|
wolfssl_log_tls12_secret(SSL *ssl)
|
|
|
|
{
|
|
|
|
unsigned char *ms, *sr, *cr;
|
|
|
|
unsigned int msLen, srLen, crLen, i, x = 0;
|
|
|
|
|
|
|
|
#if LIBWOLFSSL_VERSION_HEX >= 0x0300d000 /* >= 3.13.0 */
|
|
|
|
/* wolfSSL_GetVersion is available since 3.13, we use it instead of
|
|
|
|
* SSL_version since the latter relies on OPENSSL_ALL (--enable-opensslall or
|
|
|
|
* --enable-all). Failing to perform this check could result in an unusable
|
|
|
|
* key log line when TLS 1.3 is actually negotiated. */
|
|
|
|
switch(wolfSSL_GetVersion(ssl)) {
|
|
|
|
case WOLFSSL_SSLV3:
|
|
|
|
case WOLFSSL_TLSV1:
|
|
|
|
case WOLFSSL_TLSV1_1:
|
|
|
|
case WOLFSSL_TLSV1_2:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* TLS 1.3 does not use this mechanism, the "master secret" returned below
|
|
|
|
* is not directly usable. */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if(SSL_get_keys(ssl, &ms, &msLen, &sr, &srLen, &cr, &crLen) != SSL_SUCCESS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a missing master secret and skip logging. That can happen if
|
|
|
|
* curl rejects the server certificate and aborts the handshake.
|
|
|
|
*/
|
|
|
|
for(i = 0; i < msLen; i++) {
|
|
|
|
x |= ms[i];
|
|
|
|
}
|
|
|
|
if(x == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Curl_tls_keylog_write("CLIENT_RANDOM", cr, ms, msLen);
|
|
|
|
}
|
|
|
|
#endif /* OPENSSL_EXTRA */
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
static int do_file_type(const char *type)
|
|
|
|
{
|
|
|
|
if(!type || !type[0])
|
|
|
|
return SSL_FILETYPE_PEM;
|
2016-09-30 12:54:02 -04:00
|
|
|
if(strcasecompare(type, "PEM"))
|
2011-02-28 21:02:47 -05:00
|
|
|
return SSL_FILETYPE_PEM;
|
2016-09-30 12:54:02 -04:00
|
|
|
if(strcasecompare(type, "DER"))
|
2011-02-28 21:02:47 -05:00
|
|
|
return SSL_FILETYPE_ASN1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function loads all the client/CA certificates and CRLs. Setup the TLS
|
|
|
|
* layer and do all necessary magic.
|
|
|
|
*/
|
|
|
|
static CURLcode
|
2021-01-13 11:01:31 -05:00
|
|
|
wolfssl_connect_step1(struct Curl_easy *data, struct connectdata *conn,
|
2011-02-28 21:02:47 -05:00
|
|
|
int sockindex)
|
|
|
|
{
|
2017-01-06 17:00:45 -05:00
|
|
|
char *ciphers;
|
2020-05-13 06:56:53 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2011-02-28 21:02:47 -05:00
|
|
|
SSL_METHOD* req_method = NULL;
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
2015-04-13 01:07:28 -04:00
|
|
|
#ifdef HAVE_SNI
|
|
|
|
bool sni = FALSE;
|
|
|
|
#define use_sni(x) sni = (x)
|
|
|
|
#else
|
|
|
|
#define use_sni(x) Curl_nop_stmt
|
|
|
|
#endif
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
if(connssl->state == ssl_connection_complete)
|
2011-02-28 21:02:47 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
|
2016-12-13 15:10:00 -05:00
|
|
|
if(SSL_CONN_CONFIG(version_max) != CURL_SSLVERSION_MAX_NONE) {
|
2019-06-02 10:55:05 -04:00
|
|
|
failf(data, "wolfSSL does not support to set maximum SSL/TLS version");
|
2016-12-13 15:10:00 -05:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
/* check to see if we've been told to use an explicit SSL/TLS version */
|
2016-11-16 12:49:15 -05:00
|
|
|
switch(SSL_CONN_CONFIG(version)) {
|
2011-02-28 21:02:47 -05:00
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
2019-06-02 10:55:05 -04:00
|
|
|
#if LIBWOLFSSL_VERSION_HEX >= 0x03003000 /* >= 3.3.0 */
|
2015-04-03 02:11:35 -04:00
|
|
|
/* minimum protocol version is set later after the CTX object is created */
|
2015-03-25 02:40:00 -04:00
|
|
|
req_method = SSLv23_client_method();
|
|
|
|
#else
|
2019-06-02 10:55:05 -04:00
|
|
|
infof(data, "wolfSSL <3.3.0 cannot be configured to use TLS 1.0-1.2, "
|
2013-09-19 09:17:13 -04:00
|
|
|
"TLS 1.0 is used exclusively\n");
|
2011-02-28 21:02:47 -05:00
|
|
|
req_method = TLSv1_client_method();
|
2015-03-25 02:40:00 -04:00
|
|
|
#endif
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(TRUE);
|
2011-02-28 21:02:47 -05:00
|
|
|
break;
|
2013-09-19 09:17:13 -04:00
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
2020-12-29 15:46:42 -05:00
|
|
|
#if defined(WOLFSSL_ALLOW_TLSV10) && !defined(NO_OLD_TLS)
|
2013-09-19 09:17:13 -04:00
|
|
|
req_method = TLSv1_client_method();
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(TRUE);
|
2018-04-25 05:45:35 -04:00
|
|
|
#else
|
2019-06-02 10:55:05 -04:00
|
|
|
failf(data, "wolfSSL does not support TLS 1.0");
|
2018-04-25 05:45:35 -04:00
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
2013-09-19 09:17:13 -04:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
2020-12-29 15:46:42 -05:00
|
|
|
#ifndef NO_OLD_TLS
|
2013-09-19 09:17:13 -04:00
|
|
|
req_method = TLSv1_1_client_method();
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(TRUE);
|
2020-12-29 15:46:42 -05:00
|
|
|
#else
|
|
|
|
failf(data, "wolfSSL does not support TLS 1.1");
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
2013-09-19 09:17:13 -04:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
|
|
|
req_method = TLSv1_2_client_method();
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(TRUE);
|
2013-09-19 09:17:13 -04:00
|
|
|
break;
|
2016-11-07 21:51:27 -05:00
|
|
|
case CURL_SSLVERSION_TLSv1_3:
|
2018-03-01 13:10:36 -05:00
|
|
|
#ifdef WOLFSSL_TLS13
|
|
|
|
req_method = wolfTLSv1_3_client_method();
|
|
|
|
use_sni(TRUE);
|
|
|
|
break;
|
|
|
|
#else
|
2019-06-02 10:55:05 -04:00
|
|
|
failf(data, "wolfSSL: TLS 1.3 is not yet supported");
|
2016-11-07 21:51:27 -05:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2018-03-01 13:10:36 -05:00
|
|
|
#endif
|
2011-02-28 21:02:47 -05:00
|
|
|
case CURL_SSLVERSION_SSLv3:
|
2016-03-28 18:18:09 -04:00
|
|
|
#ifdef WOLFSSL_ALLOW_SSLV3
|
2011-02-28 21:02:47 -05:00
|
|
|
req_method = SSLv3_client_method();
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(FALSE);
|
2015-12-16 04:06:09 -05:00
|
|
|
#else
|
2019-06-02 10:55:05 -04:00
|
|
|
failf(data, "wolfSSL does not support SSLv3");
|
2015-12-16 04:06:09 -05:00
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
2011-02-28 21:02:47 -05:00
|
|
|
break;
|
2015-03-25 02:40:00 -04:00
|
|
|
case CURL_SSLVERSION_SSLv2:
|
2019-06-02 10:55:05 -04:00
|
|
|
failf(data, "wolfSSL does not support SSLv2");
|
2015-03-25 02:40:00 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
default:
|
|
|
|
failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!req_method) {
|
|
|
|
failf(data, "SSL: couldn't create a method!");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
if(backend->ctx)
|
|
|
|
SSL_CTX_free(backend->ctx);
|
|
|
|
backend->ctx = SSL_CTX_new(req_method);
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
if(!backend->ctx) {
|
2011-02-28 21:02:47 -05:00
|
|
|
failf(data, "SSL: couldn't create a context!");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
switch(SSL_CONN_CONFIG(version)) {
|
2015-04-03 02:11:35 -04:00
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
2019-06-02 10:55:05 -04:00
|
|
|
#if LIBWOLFSSL_VERSION_HEX > 0x03004006 /* > 3.4.6 */
|
|
|
|
/* Versions 3.3.0 to 3.4.6 we know the minimum protocol version is
|
|
|
|
* whatever minimum version of TLS was built in and at least TLS 1.0. For
|
|
|
|
* later library versions that could change (eg TLS 1.0 built in but
|
|
|
|
* defaults to TLS 1.1) so we have this short circuit evaluation to find
|
|
|
|
* the minimum supported TLS version.
|
2015-04-03 02:11:35 -04:00
|
|
|
*/
|
2020-03-18 18:16:02 -04:00
|
|
|
if((wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1) != 1) &&
|
|
|
|
(wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_1) != 1) &&
|
|
|
|
(wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_2) != 1)
|
2018-03-01 13:10:36 -05:00
|
|
|
#ifdef WOLFSSL_TLS13
|
2020-03-18 18:16:02 -04:00
|
|
|
&& (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_3) != 1)
|
2018-03-01 13:10:36 -05:00
|
|
|
#endif
|
|
|
|
) {
|
2015-04-03 02:11:35 -04:00
|
|
|
failf(data, "SSL: couldn't set the minimum protocol version");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-06 17:00:45 -05:00
|
|
|
ciphers = SSL_CONN_CONFIG(cipher_list);
|
|
|
|
if(ciphers) {
|
2020-03-18 18:16:02 -04:00
|
|
|
if(!SSL_CTX_set_cipher_list(backend->ctx, ciphers)) {
|
2017-01-06 17:00:45 -05:00
|
|
|
failf(data, "failed setting cipher list: %s", ciphers);
|
|
|
|
return CURLE_SSL_CIPHER;
|
|
|
|
}
|
|
|
|
infof(data, "Cipher selection: %s\n", ciphers);
|
|
|
|
}
|
|
|
|
|
2011-05-20 17:40:59 -04:00
|
|
|
#ifndef NO_FILESYSTEM
|
2011-02-28 21:02:47 -05:00
|
|
|
/* load trusted cacert */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(CAfile)) {
|
2020-03-18 18:16:02 -04:00
|
|
|
if(1 != SSL_CTX_load_verify_locations(backend->ctx,
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_CONN_CONFIG(CAfile),
|
|
|
|
SSL_CONN_CONFIG(CApath))) {
|
|
|
|
if(SSL_CONN_CONFIG(verifypeer)) {
|
2014-07-22 18:43:47 -04:00
|
|
|
/* Fail if we insist on successfully verifying the server. */
|
2020-08-17 18:22:34 -04:00
|
|
|
failf(data, "error setting certificate verify locations:"
|
|
|
|
" CAfile: %s CApath: %s",
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_CONN_CONFIG(CAfile)?
|
|
|
|
SSL_CONN_CONFIG(CAfile): "none",
|
|
|
|
SSL_CONN_CONFIG(CApath)?
|
|
|
|
SSL_CONN_CONFIG(CApath) : "none");
|
2011-02-28 21:02:47 -05:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
else {
|
2014-07-22 18:43:47 -04:00
|
|
|
/* Just continue with a warning if no strict certificate
|
2011-02-28 21:02:47 -05:00
|
|
|
verification is required. */
|
|
|
|
infof(data, "error setting certificate verify locations,"
|
|
|
|
" continuing anyway:\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Everything is fine. */
|
|
|
|
infof(data, "successfully set certificate verify locations:\n");
|
|
|
|
}
|
2020-08-17 18:22:34 -04:00
|
|
|
infof(data, " CAfile: %s\n",
|
|
|
|
SSL_CONN_CONFIG(CAfile) ? SSL_CONN_CONFIG(CAfile) : "none");
|
|
|
|
infof(data, " CApath: %s\n",
|
|
|
|
SSL_CONN_CONFIG(CApath) ? SSL_CONN_CONFIG(CApath) : "none");
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the client certificate, and private key */
|
2020-06-29 14:07:37 -04:00
|
|
|
if(SSL_SET_OPTION(primary.clientcert) && SSL_SET_OPTION(key)) {
|
2016-11-16 12:49:15 -05:00
|
|
|
int file_type = do_file_type(SSL_SET_OPTION(cert_type));
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2020-06-29 14:07:37 -04:00
|
|
|
if(SSL_CTX_use_certificate_file(backend->ctx,
|
|
|
|
SSL_SET_OPTION(primary.clientcert),
|
|
|
|
file_type) != 1) {
|
2011-02-28 21:02:47 -05:00
|
|
|
failf(data, "unable to use client certificate (no key or wrong pass"
|
|
|
|
" phrase?)");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:49:15 -05:00
|
|
|
file_type = do_file_type(SSL_SET_OPTION(key_type));
|
2020-03-18 18:16:02 -04:00
|
|
|
if(SSL_CTX_use_PrivateKey_file(backend->ctx, SSL_SET_OPTION(key),
|
2011-02-28 21:02:47 -05:00
|
|
|
file_type) != 1) {
|
|
|
|
failf(data, "unable to set private key");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2015-03-28 00:16:08 -04:00
|
|
|
#endif /* !NO_FILESYSTEM */
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
/* SSL always tries to verify the peer, this only says whether it should
|
|
|
|
* fail to connect if the verification fails, or if it should continue
|
|
|
|
* anyway. In the latter case the result of the verification is checked with
|
|
|
|
* SSL_get_verify_result() below. */
|
2020-03-18 18:16:02 -04:00
|
|
|
SSL_CTX_set_verify(backend->ctx,
|
2016-11-16 12:49:15 -05:00
|
|
|
SSL_CONN_CONFIG(verifypeer)?SSL_VERIFY_PEER:
|
|
|
|
SSL_VERIFY_NONE,
|
2011-02-28 21:02:47 -05:00
|
|
|
NULL);
|
|
|
|
|
2015-04-13 01:07:28 -04:00
|
|
|
#ifdef HAVE_SNI
|
|
|
|
if(sni) {
|
|
|
|
struct in_addr addr4;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct in6_addr addr6;
|
|
|
|
#endif
|
2020-05-27 05:51:34 -04:00
|
|
|
#ifndef CURL_DISABLE_PROXY
|
2016-11-16 12:49:15 -05:00
|
|
|
const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
|
|
|
|
conn->host.name;
|
2020-05-27 05:51:34 -04:00
|
|
|
#else
|
|
|
|
const char * const hostname = conn->host.name;
|
|
|
|
#endif
|
2016-11-16 12:49:15 -05:00
|
|
|
size_t hostname_len = strlen(hostname);
|
2015-04-13 01:07:28 -04:00
|
|
|
if((hostname_len < USHRT_MAX) &&
|
2016-11-16 12:49:15 -05:00
|
|
|
(0 == Curl_inet_pton(AF_INET, hostname, &addr4)) &&
|
2015-04-13 01:07:28 -04:00
|
|
|
#ifdef ENABLE_IPV6
|
2016-11-16 12:49:15 -05:00
|
|
|
(0 == Curl_inet_pton(AF_INET6, hostname, &addr6)) &&
|
2015-04-13 01:07:28 -04:00
|
|
|
#endif
|
2020-03-18 18:16:02 -04:00
|
|
|
(wolfSSL_CTX_UseSNI(backend->ctx, WOLFSSL_SNI_HOST_NAME, hostname,
|
2015-04-13 01:07:28 -04:00
|
|
|
(unsigned short)hostname_len) != 1)) {
|
|
|
|
infof(data, "WARNING: failed to configure server name indication (SNI) "
|
|
|
|
"TLS extension\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-27 07:22:32 -04:00
|
|
|
/* give application a chance to interfere with SSL set up. */
|
|
|
|
if(data->set.ssl.fsslctx) {
|
2020-03-18 18:16:02 -04:00
|
|
|
CURLcode result = (*data->set.ssl.fsslctx)(data, backend->ctx,
|
2019-05-25 04:06:08 -04:00
|
|
|
data->set.ssl.fsslctxp);
|
2015-03-27 07:22:32 -04:00
|
|
|
if(result) {
|
|
|
|
failf(data, "error signaled by ssl ctx callback");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef NO_FILESYSTEM
|
2016-11-16 12:49:15 -05:00
|
|
|
else if(SSL_CONN_CONFIG(verifypeer)) {
|
2019-06-02 10:55:05 -04:00
|
|
|
failf(data, "SSL: Certificates can't be loaded because wolfSSL was built"
|
2015-03-28 00:16:08 -04:00
|
|
|
" with \"no filesystem\". Either disable peer verification"
|
|
|
|
" (insecure) or if you are building an application with libcurl you"
|
|
|
|
" can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
|
2015-03-27 07:22:32 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
/* Let's make an SSL structure */
|
2020-03-18 18:16:02 -04:00
|
|
|
if(backend->handle)
|
|
|
|
SSL_free(backend->handle);
|
|
|
|
backend->handle = SSL_new(backend->ctx);
|
|
|
|
if(!backend->handle) {
|
2011-02-28 21:02:47 -05:00
|
|
|
failf(data, "SSL: couldn't create a context (handle)!");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2016-03-28 18:18:09 -04:00
|
|
|
#ifdef HAVE_ALPN
|
2016-05-09 10:50:11 -04:00
|
|
|
if(conn->bits.tls_enable_alpn) {
|
2016-03-28 18:18:09 -04:00
|
|
|
char protocols[128];
|
|
|
|
*protocols = '\0';
|
|
|
|
|
|
|
|
/* wolfSSL's ALPN protocol name list format is a comma separated string of
|
|
|
|
protocols in descending order of preference, eg: "h2,http/1.1" */
|
|
|
|
|
|
|
|
#ifdef USE_NGHTTP2
|
|
|
|
if(data->set.httpversion >= CURL_HTTP_VERSION_2) {
|
|
|
|
strcpy(protocols + strlen(protocols), NGHTTP2_PROTO_VERSION_ID ",");
|
|
|
|
infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
strcpy(protocols + strlen(protocols), ALPN_HTTP_1_1);
|
|
|
|
infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1);
|
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
if(wolfSSL_UseALPN(backend->handle, protocols,
|
2016-03-28 18:18:09 -04:00
|
|
|
(unsigned)strlen(protocols),
|
|
|
|
WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) != SSL_SUCCESS) {
|
|
|
|
failf(data, "SSL: failed setting ALPN protocols");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_ALPN */
|
|
|
|
|
2020-05-03 18:16:40 -04:00
|
|
|
#ifdef OPENSSL_EXTRA
|
|
|
|
if(Curl_tls_keylog_enabled()) {
|
|
|
|
/* Ensure the Client Random is preserved. */
|
|
|
|
wolfSSL_KeepArrays(backend->handle);
|
|
|
|
#if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
|
|
|
|
wolfSSL_set_tls13_secret_cb(backend->handle,
|
|
|
|
wolfssl_tls13_secret_callback, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* OPENSSL_EXTRA */
|
|
|
|
|
2020-08-31 15:35:35 -04:00
|
|
|
#ifdef HAVE_SECURE_RENEGOTIATION
|
|
|
|
if(wolfSSL_UseSecureRenegotiation(backend->handle) != SSL_SUCCESS) {
|
|
|
|
failf(data, "SSL: failed setting secure renegotiation");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_SECURE_RENEGOTIATION */
|
|
|
|
|
2011-02-28 21:02:47 -05: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 *ssl_sessionid = NULL;
|
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
Curl_ssl_sessionid_lock(data);
|
|
|
|
if(!Curl_ssl_getsessionid(data, conn, &ssl_sessionid, NULL, sockindex)) {
|
2016-06-12 23:47:12 -04:00
|
|
|
/* we got a session id, use it! */
|
2020-03-18 18:16:02 -04:00
|
|
|
if(!SSL_set_session(backend->handle, ssl_sessionid)) {
|
2019-06-02 10:55:05 -04:00
|
|
|
char error_buffer[WOLFSSL_MAX_ERROR_SZ];
|
2021-01-13 11:01:31 -05:00
|
|
|
Curl_ssl_sessionid_unlock(data);
|
2016-06-12 23:47:12 -04:00
|
|
|
failf(data, "SSL: SSL_set_session failed: %s",
|
2020-03-18 18:16:02 -04:00
|
|
|
ERR_error_string(SSL_get_error(backend->handle, 0),
|
2019-06-02 10:55:05 -04:00
|
|
|
error_buffer));
|
2016-06-12 23:47:12 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
/* Informational message */
|
2016-12-13 17:34:59 -05:00
|
|
|
infof(data, "SSL re-using session ID\n");
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
2021-01-13 11:01:31 -05:00
|
|
|
Curl_ssl_sessionid_unlock(data);
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pass the raw socket into the SSL layer */
|
2020-03-18 18:16:02 -04:00
|
|
|
if(!SSL_set_fd(backend->handle, (int)sockfd)) {
|
2011-02-28 21:02:47 -05:00
|
|
|
failf(data, "SSL: SSL_set_fd failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
connssl->connecting_state = ssl_connect_2;
|
2011-02-28 21:02:47 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CURLcode
|
2021-01-13 11:01:31 -05:00
|
|
|
wolfssl_connect_step2(struct Curl_easy *data, struct connectdata *conn,
|
2011-02-28 21:02:47 -05:00
|
|
|
int sockindex)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
2020-05-13 06:56:53 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2020-05-27 05:51:34 -04:00
|
|
|
#ifndef CURL_DISABLE_PROXY
|
2016-11-16 12:49:15 -05:00
|
|
|
const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
|
|
|
|
conn->host.name;
|
|
|
|
const char * const dispname = SSL_IS_PROXY() ?
|
|
|
|
conn->http_proxy.host.dispname : conn->host.dispname;
|
2016-11-25 04:47:25 -05:00
|
|
|
const char * const pinnedpubkey = SSL_IS_PROXY() ?
|
2020-05-27 05:51:34 -04:00
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
|
|
|
|
#else
|
|
|
|
const char * const hostname = conn->host.name;
|
|
|
|
const char * const dispname = conn->host.dispname;
|
|
|
|
const char * const pinnedpubkey =
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
|
|
|
|
#endif
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2019-06-02 10:55:05 -04:00
|
|
|
conn->recv[sockindex] = wolfssl_recv;
|
|
|
|
conn->send[sockindex] = wolfssl_send;
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2012-11-02 21:06:51 -04:00
|
|
|
/* Enable RFC2818 checks */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(verifyhost)) {
|
2020-03-18 18:16:02 -04:00
|
|
|
ret = wolfSSL_check_domain_name(backend->handle, hostname);
|
2012-11-02 21:06:51 -04:00
|
|
|
if(ret == SSL_FAILURE)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
ret = SSL_connect(backend->handle);
|
2020-05-03 18:16:40 -04:00
|
|
|
|
|
|
|
#ifdef OPENSSL_EXTRA
|
|
|
|
if(Curl_tls_keylog_enabled()) {
|
|
|
|
/* If key logging is enabled, wait for the handshake to complete and then
|
|
|
|
* proceed with logging secrets (for TLS 1.2 or older).
|
|
|
|
*
|
|
|
|
* During the handshake (ret==-1), wolfSSL_want_read() is true as it waits
|
|
|
|
* for the server response. At that point the master secret is not yet
|
|
|
|
* available, so we must not try to read it.
|
|
|
|
* To log the secret on completion with a handshake failure, detect
|
|
|
|
* completion via the observation that there is nothing to read or write.
|
|
|
|
* Note that OpenSSL SSL_want_read() is always true here. If wolfSSL ever
|
|
|
|
* changes, the worst case is that no key is logged on error.
|
|
|
|
*/
|
|
|
|
if(ret == SSL_SUCCESS ||
|
|
|
|
(!wolfSSL_want_read(backend->handle) &&
|
|
|
|
!wolfSSL_want_write(backend->handle))) {
|
|
|
|
wolfssl_log_tls12_secret(backend->handle);
|
|
|
|
/* Client Random and master secrets are no longer needed, erase these.
|
|
|
|
* Ignored while the handshake is still in progress. */
|
|
|
|
wolfSSL_FreeArrays(backend->handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* OPENSSL_EXTRA */
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(ret != 1) {
|
2019-06-02 10:55:05 -04:00
|
|
|
char error_buffer[WOLFSSL_MAX_ERROR_SZ];
|
2020-03-18 18:16:02 -04:00
|
|
|
int detail = SSL_get_error(backend->handle, ret);
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(SSL_ERROR_WANT_READ == detail) {
|
2017-06-21 06:41:18 -04:00
|
|
|
connssl->connecting_state = ssl_connect_2_reading;
|
2011-02-28 21:02:47 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2012-11-02 21:06:51 -04:00
|
|
|
else if(SSL_ERROR_WANT_WRITE == detail) {
|
2017-06-21 06:41:18 -04:00
|
|
|
connssl->connecting_state = ssl_connect_2_writing;
|
2011-02-28 21:02:47 -05:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2012-11-02 21:06:51 -04:00
|
|
|
/* There is no easy way to override only the CN matching.
|
|
|
|
* This will enable the override of both mismatching SubjectAltNames
|
|
|
|
* as also mismatching CN fields */
|
|
|
|
else if(DOMAIN_NAME_MISMATCH == detail) {
|
|
|
|
#if 1
|
2020-12-23 17:41:13 -05:00
|
|
|
failf(data, "\tsubject alt name(s) or common name do not match \"%s\"",
|
2016-11-16 12:49:15 -05:00
|
|
|
dispname);
|
2012-11-02 21:06:51 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
#else
|
2019-06-02 10:55:05 -04:00
|
|
|
/* When the wolfssl_check_domain_name() is used and you desire to
|
|
|
|
* continue on a DOMAIN_NAME_MISMATCH, i.e. 'conn->ssl_config.verifyhost
|
|
|
|
* == 0', CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA
|
|
|
|
* error. The only way to do this is currently to switch the
|
|
|
|
* Wolfssl_check_domain_name() in and out based on the
|
|
|
|
* 'conn->ssl_config.verifyhost' value. */
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(verifyhost)) {
|
2012-11-02 21:06:51 -04:00
|
|
|
failf(data,
|
|
|
|
"\tsubject alt name(s) or common name do not match \"%s\"\n",
|
2016-11-16 12:49:15 -05:00
|
|
|
dispname);
|
2012-11-02 21:06:51 -04:00
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
infof(data,
|
|
|
|
"\tsubject alt name(s) and/or common name do not match \"%s\"\n",
|
2016-11-16 12:49:15 -05:00
|
|
|
dispname);
|
2012-11-02 21:06:51 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2019-06-02 10:55:05 -04:00
|
|
|
#if LIBWOLFSSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
|
2014-07-22 18:43:47 -04:00
|
|
|
else if(ASN_NO_SIGNER_E == detail) {
|
2016-11-16 12:49:15 -05:00
|
|
|
if(SSL_CONN_CONFIG(verifypeer)) {
|
2020-12-23 17:41:13 -05:00
|
|
|
failf(data, "\tCA signer not available for verification");
|
2014-07-22 18:43:47 -04:00
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Just continue with a warning if no strict certificate
|
|
|
|
verification is required. */
|
|
|
|
infof(data, "CA signer not available for verification, "
|
|
|
|
"continuing anyway\n");
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 04:07:42 -04:00
|
|
|
#endif
|
2012-11-02 21:06:51 -04:00
|
|
|
else {
|
|
|
|
failf(data, "SSL_connect failed with error %d: %s", detail,
|
2011-02-28 21:02:47 -05:00
|
|
|
ERR_error_string(detail, error_buffer));
|
2012-11-02 21:06:51 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
2016-11-25 04:47:25 -05:00
|
|
|
if(pinnedpubkey) {
|
2016-03-28 18:18:09 -04:00
|
|
|
#ifdef KEEP_PEER_CERT
|
2015-04-05 01:48:16 -04:00
|
|
|
X509 *x509;
|
|
|
|
const char *x509_der;
|
|
|
|
int x509_der_len;
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_X509certificate x509_parsed;
|
|
|
|
struct Curl_asn1Element *pubkey;
|
2015-04-05 01:48:16 -04:00
|
|
|
CURLcode result;
|
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
x509 = SSL_get_peer_certificate(backend->handle);
|
2015-04-05 01:48:16 -04:00
|
|
|
if(!x509) {
|
|
|
|
failf(data, "SSL: failed retrieving server certificate");
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
2019-06-11 01:45:12 -04:00
|
|
|
x509_der = (const char *)wolfSSL_X509_get_der(x509, &x509_der_len);
|
2015-04-05 01:48:16 -04:00
|
|
|
if(!x509_der) {
|
|
|
|
failf(data, "SSL: failed retrieving ASN.1 server certificate");
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
2018-05-11 17:40:58 -04:00
|
|
|
memset(&x509_parsed, 0, sizeof(x509_parsed));
|
2016-11-24 08:28:39 -05:00
|
|
|
if(Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len))
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
2015-04-05 01:48:16 -04:00
|
|
|
|
|
|
|
pubkey = &x509_parsed.subjectPublicKeyInfo;
|
|
|
|
if(!pubkey->header || pubkey->end <= pubkey->header) {
|
|
|
|
failf(data, "SSL: failed retrieving public key from server certificate");
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
2015-09-12 17:35:12 -04:00
|
|
|
result = Curl_pin_peer_pubkey(data,
|
2016-11-25 04:47:25 -05:00
|
|
|
pinnedpubkey,
|
2015-04-05 01:48:16 -04:00
|
|
|
(const unsigned char *)pubkey->header,
|
|
|
|
(size_t)(pubkey->end - pubkey->header));
|
|
|
|
if(result) {
|
|
|
|
failf(data, "SSL: public key does not match pinned public key!");
|
|
|
|
return result;
|
|
|
|
}
|
2015-12-16 04:25:31 -05:00
|
|
|
#else
|
|
|
|
failf(data, "Library lacks pinning support built-in");
|
|
|
|
return CURLE_NOT_BUILT_IN;
|
|
|
|
#endif
|
2015-04-05 01:48:16 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 18:18:09 -04:00
|
|
|
#ifdef HAVE_ALPN
|
2016-05-09 10:50:11 -04:00
|
|
|
if(conn->bits.tls_enable_alpn) {
|
2016-03-28 18:18:09 -04:00
|
|
|
int rc;
|
|
|
|
char *protocol = NULL;
|
|
|
|
unsigned short protocol_len = 0;
|
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
rc = wolfSSL_ALPN_GetProtocol(backend->handle, &protocol, &protocol_len);
|
2016-03-28 18:18:09 -04:00
|
|
|
|
|
|
|
if(rc == SSL_SUCCESS) {
|
|
|
|
infof(data, "ALPN, server accepted to use %.*s\n", protocol_len,
|
|
|
|
protocol);
|
|
|
|
|
|
|
|
if(protocol_len == ALPN_HTTP_1_1_LENGTH &&
|
|
|
|
!memcmp(protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH))
|
|
|
|
conn->negnpn = CURL_HTTP_VERSION_1_1;
|
|
|
|
#ifdef USE_NGHTTP2
|
|
|
|
else if(data->set.httpversion >= CURL_HTTP_VERSION_2 &&
|
|
|
|
protocol_len == NGHTTP2_PROTO_VERSION_ID_LEN &&
|
|
|
|
!memcmp(protocol, NGHTTP2_PROTO_VERSION_ID,
|
|
|
|
NGHTTP2_PROTO_VERSION_ID_LEN))
|
|
|
|
conn->negnpn = CURL_HTTP_VERSION_2;
|
|
|
|
#endif
|
|
|
|
else
|
|
|
|
infof(data, "ALPN, unrecognized protocol %.*s\n", protocol_len,
|
|
|
|
protocol);
|
2021-01-08 11:58:15 -05:00
|
|
|
Curl_multiuse_state(data, conn->negnpn == CURL_HTTP_VERSION_2 ?
|
2019-04-30 05:14:38 -04:00
|
|
|
BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
|
2016-03-28 18:18:09 -04:00
|
|
|
}
|
|
|
|
else if(rc == SSL_ALPN_NOT_FOUND)
|
|
|
|
infof(data, "ALPN, server did not agree to a protocol\n");
|
|
|
|
else {
|
|
|
|
failf(data, "ALPN, failure getting protocol, error %d", rc);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_ALPN */
|
|
|
|
|
2017-06-21 06:41:18 -04:00
|
|
|
connssl->connecting_state = ssl_connect_3;
|
2019-06-02 10:55:05 -04:00
|
|
|
#if (LIBWOLFSSL_VERSION_HEX >= 0x03009010)
|
2017-01-06 18:09:10 -05:00
|
|
|
infof(data, "SSL connection using %s / %s\n",
|
2020-03-18 18:16:02 -04:00
|
|
|
wolfSSL_get_version(backend->handle),
|
|
|
|
wolfSSL_get_cipher_name(backend->handle));
|
2017-01-06 18:09:10 -05:00
|
|
|
#else
|
2012-01-16 15:14:05 -05:00
|
|
|
infof(data, "SSL connected\n");
|
2017-01-06 18:09:10 -05:00
|
|
|
#endif
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CURLcode
|
2021-01-13 11:01:31 -05:00
|
|
|
wolfssl_connect_step3(struct Curl_easy *data, struct connectdata *conn,
|
2011-02-28 21:02:47 -05:00
|
|
|
int sockindex)
|
|
|
|
{
|
2014-12-25 06:39:47 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2011-02-28 21:02:47 -05:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2011-02-28 21:02:47 -05: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
|
|
|
bool incache;
|
|
|
|
SSL_SESSION *our_ssl_sessionid;
|
|
|
|
void *old_ssl_sessionid = NULL;
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
our_ssl_sessionid = SSL_get_session(backend->handle);
|
2016-06-12 23:47:12 -04:00
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
Curl_ssl_sessionid_lock(data);
|
|
|
|
incache = !(Curl_ssl_getsessionid(data, conn, &old_ssl_sessionid, NULL,
|
2016-11-16 12:49:15 -05:00
|
|
|
sockindex));
|
2016-06-12 23:47:12 -04:00
|
|
|
if(incache) {
|
|
|
|
if(old_ssl_sessionid != our_ssl_sessionid) {
|
|
|
|
infof(data, "old SSL session ID is stale, removing\n");
|
2021-01-13 11:01:31 -05:00
|
|
|
Curl_ssl_delsessionid(data, old_ssl_sessionid);
|
2016-06-12 23:47:12 -04:00
|
|
|
incache = FALSE;
|
|
|
|
}
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
2014-12-25 12:15:15 -05:00
|
|
|
|
2016-06-12 23:47:12 -04:00
|
|
|
if(!incache) {
|
2021-01-13 11:01:31 -05:00
|
|
|
result = Curl_ssl_addsessionid(data, conn, our_ssl_sessionid,
|
2016-11-16 12:49:15 -05:00
|
|
|
0 /* unknown size */, sockindex);
|
2016-06-12 23:47:12 -04:00
|
|
|
if(result) {
|
2021-01-13 11:01:31 -05:00
|
|
|
Curl_ssl_sessionid_unlock(data);
|
2016-06-12 23:47:12 -04:00
|
|
|
failf(data, "failed to store ssl session");
|
|
|
|
return result;
|
|
|
|
}
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
2021-01-13 11:01:31 -05:00
|
|
|
Curl_ssl_sessionid_unlock(data);
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_done;
|
|
|
|
|
2014-12-25 06:39:47 -05:00
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static ssize_t wolfssl_send(struct Curl_easy *data,
|
|
|
|
int sockindex,
|
|
|
|
const void *mem,
|
|
|
|
size_t len,
|
|
|
|
CURLcode *curlcode)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2019-06-02 10:55:05 -04:00
|
|
|
char error_buffer[WOLFSSL_MAX_ERROR_SZ];
|
2020-03-18 18:16:02 -04:00
|
|
|
int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
|
|
|
|
int rc = SSL_write(backend->handle, mem, memlen);
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc < 0) {
|
2020-03-18 18:16:02 -04:00
|
|
|
int err = SSL_get_error(backend->handle, rc);
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
switch(err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
/* there's data pending, re-invoke SSL_write() */
|
|
|
|
*curlcode = CURLE_AGAIN;
|
|
|
|
return -1;
|
|
|
|
default:
|
2021-01-08 11:58:15 -05:00
|
|
|
failf(data, "SSL write: %s, errno %d",
|
2011-02-28 21:02:47 -05:00
|
|
|
ERR_error_string(err, error_buffer),
|
|
|
|
SOCKERRNO);
|
|
|
|
*curlcode = CURLE_SEND_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
static void wolfssl_close(struct Curl_easy *data, struct connectdata *conn,
|
|
|
|
int sockindex)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
(void) data;
|
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
if(backend->handle) {
|
|
|
|
(void)SSL_shutdown(backend->handle);
|
|
|
|
SSL_free(backend->handle);
|
|
|
|
backend->handle = NULL;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
2020-03-18 18:16:02 -04:00
|
|
|
if(backend->ctx) {
|
|
|
|
SSL_CTX_free(backend->ctx);
|
|
|
|
backend->ctx = NULL;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 11:58:15 -05:00
|
|
|
static ssize_t wolfssl_recv(struct Curl_easy *data,
|
2020-03-18 18:16:02 -04:00
|
|
|
int num,
|
|
|
|
char *buf,
|
|
|
|
size_t buffersize,
|
|
|
|
CURLcode *curlcode)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2021-01-08 11:58:15 -05:00
|
|
|
struct connectdata *conn = data->conn;
|
2017-06-21 06:41:18 -04:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[num];
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2019-06-02 10:55:05 -04:00
|
|
|
char error_buffer[WOLFSSL_MAX_ERROR_SZ];
|
2020-03-18 18:16:02 -04:00
|
|
|
int buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
|
|
|
|
int nread = SSL_read(backend->handle, buf, buffsize);
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(nread < 0) {
|
2020-03-18 18:16:02 -04:00
|
|
|
int err = SSL_get_error(backend->handle, nread);
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
switch(err) {
|
|
|
|
case SSL_ERROR_ZERO_RETURN: /* no more data */
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
/* there's data pending, re-invoke SSL_read() */
|
|
|
|
*curlcode = CURLE_AGAIN;
|
|
|
|
return -1;
|
|
|
|
default:
|
2021-01-08 11:58:15 -05:00
|
|
|
failf(data, "SSL read: %s, errno %d",
|
|
|
|
ERR_error_string(err, error_buffer), SOCKERRNO);
|
2011-02-28 21:02:47 -05:00
|
|
|
*curlcode = CURLE_RECV_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
static void wolfssl_session_free(void *ptr)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2011-03-08 08:09:20 -05:00
|
|
|
(void)ptr;
|
2019-06-02 10:55:05 -04:00
|
|
|
/* wolfSSL reuses sessions on own, no free */
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
static size_t wolfssl_version(char *buffer, size_t size)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2019-06-02 10:55:05 -04:00
|
|
|
#if LIBWOLFSSL_VERSION_HEX >= 0x03006000
|
2018-11-22 03:01:24 -05:00
|
|
|
return msnprintf(buffer, size, "wolfSSL/%s", wolfSSL_lib_version());
|
2017-02-26 18:49:39 -05:00
|
|
|
#elif defined(WOLFSSL_VERSION)
|
2018-11-22 03:01:24 -05:00
|
|
|
return msnprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
|
2011-02-28 21:02:47 -05:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
static int wolfssl_init(void)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2020-05-03 18:16:40 -04:00
|
|
|
#ifdef OPENSSL_EXTRA
|
|
|
|
Curl_tls_keylog_open();
|
|
|
|
#endif
|
2019-06-02 10:55:05 -04:00
|
|
|
return (wolfSSL_Init() == SSL_SUCCESS);
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
static void wolfssl_cleanup(void)
|
2018-12-19 11:30:44 -05:00
|
|
|
{
|
2019-06-02 10:55:05 -04:00
|
|
|
wolfSSL_Cleanup();
|
2020-05-03 18:16:40 -04:00
|
|
|
#ifdef OPENSSL_EXTRA
|
|
|
|
Curl_tls_keylog_close();
|
|
|
|
#endif
|
2018-12-19 11:30:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
static bool wolfssl_data_pending(const struct connectdata *conn,
|
|
|
|
int connindex)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2017-06-21 06:41:18 -04:00
|
|
|
const struct ssl_connect_data *connssl = &conn->ssl[connindex];
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
|
|
|
if(backend->handle) /* SSL is in use */
|
|
|
|
return (0 != SSL_pending(backend->handle)) ? TRUE : FALSE;
|
2011-02-28 21:02:47 -05:00
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called to shut down the SSL layer but keep the
|
|
|
|
* socket open (CCC - Clear Command Channel)
|
|
|
|
*/
|
2021-01-13 11:01:31 -05:00
|
|
|
static int wolfssl_shutdown(struct Curl_easy *data, struct connectdata *conn,
|
|
|
|
int sockindex)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
(void) data;
|
|
|
|
|
2020-03-18 18:16:02 -04:00
|
|
|
if(backend->handle) {
|
|
|
|
SSL_free(backend->handle);
|
|
|
|
backend->handle = NULL;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CURLcode
|
2021-01-13 11:01:31 -05:00
|
|
|
wolfssl_connect_common(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
2011-02-28 21:02:47 -05:00
|
|
|
int sockindex,
|
|
|
|
bool nonblocking,
|
|
|
|
bool *done)
|
|
|
|
{
|
2014-12-25 06:39:47 -05:00
|
|
|
CURLcode result;
|
2011-02-28 21:02:47 -05:00
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
|
|
|
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) {
|
2011-02-28 21:02:47 -05:00
|
|
|
/* Find out how much more time we're allowed */
|
2020-05-28 18:08:03 -04:00
|
|
|
const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* no need to continue if time already is up */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
2014-12-25 06:39:47 -05:00
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
result = wolfssl_connect_step1(data, conn, sockindex);
|
2014-12-25 06:39:47 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2011-02-28 21:02:47 -05: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 */
|
2020-05-28 18:08:03 -04:00
|
|
|
const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
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 ==
|
2011-02-28 21:02:47 -05:00
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
2017-09-09 17:09:06 -04:00
|
|
|
curl_socket_t readfd = ssl_connect_2_reading ==
|
2011-02-28 21:02:47 -05: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);
|
2011-02-28 21:02:47 -05: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.
|
|
|
|
*/
|
2021-01-13 11:01:31 -05:00
|
|
|
result = wolfssl_connect_step2(data, conn, sockindex);
|
2014-12-25 06:39:47 -05:00
|
|
|
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;
|
2011-02-28 21:02:47 -05:00
|
|
|
} /* repeat step2 until all transactions are done. */
|
|
|
|
|
2014-12-25 06:39:47 -05:00
|
|
|
if(ssl_connect_3 == connssl->connecting_state) {
|
2021-01-13 11:01:31 -05:00
|
|
|
result = wolfssl_connect_step3(data, conn, sockindex);
|
2014-12-25 06:39:47 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
2014-12-25 06:39:47 -05:00
|
|
|
if(ssl_connect_done == connssl->connecting_state) {
|
2011-02-28 21:02:47 -05:00
|
|
|
connssl->state = ssl_connection_complete;
|
2019-06-02 10:55:05 -04:00
|
|
|
conn->recv[sockindex] = wolfssl_recv;
|
|
|
|
conn->send[sockindex] = wolfssl_send;
|
2011-02-28 21:02:47 -05:00
|
|
|
*done = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*done = FALSE;
|
|
|
|
|
|
|
|
/* Reset our connect state machine */
|
|
|
|
connssl->connecting_state = ssl_connect_1;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
static CURLcode wolfssl_connect_nonblocking(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn,
|
2021-01-13 02:42:36 -05:00
|
|
|
int sockindex, bool *done)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2021-01-13 11:01:31 -05:00
|
|
|
return wolfssl_connect_common(data, conn, sockindex, TRUE, done);
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
static CURLcode wolfssl_connect(struct Curl_easy *data,
|
|
|
|
struct connectdata *conn, int sockindex)
|
2011-02-28 21:02:47 -05:00
|
|
|
{
|
2014-12-25 06:39:47 -05:00
|
|
|
CURLcode result;
|
2011-02-28 21:02:47 -05:00
|
|
|
bool done = FALSE;
|
|
|
|
|
2021-01-13 11:01:31 -05:00
|
|
|
result = wolfssl_connect_common(data, conn, sockindex, FALSE, &done);
|
2014-12-25 06:39:47 -05:00
|
|
|
if(result)
|
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
DEBUGASSERT(done);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
static CURLcode wolfssl_random(struct Curl_easy *data,
|
|
|
|
unsigned char *entropy, size_t length)
|
2014-07-30 18:09:13 -04:00
|
|
|
{
|
2020-01-16 09:59:56 -05:00
|
|
|
WC_RNG rng;
|
2014-07-30 18:09:13 -04:00
|
|
|
(void)data;
|
2019-06-02 10:55:05 -04:00
|
|
|
if(wc_InitRng(&rng))
|
2017-01-12 11:41:26 -05:00
|
|
|
return CURLE_FAILED_INIT;
|
2015-03-25 02:37:20 -04:00
|
|
|
if(length > UINT_MAX)
|
2017-01-12 11:41:26 -05:00
|
|
|
return CURLE_FAILED_INIT;
|
2019-06-02 10:55:05 -04:00
|
|
|
if(wc_RNG_GenerateBlock(&rng, entropy, (unsigned)length))
|
2017-01-12 11:41:26 -05:00
|
|
|
return CURLE_FAILED_INIT;
|
2019-06-02 10:55:05 -04:00
|
|
|
if(wc_FreeRng(&rng))
|
2018-07-20 16:00:09 -04:00
|
|
|
return CURLE_FAILED_INIT;
|
2017-01-12 11:41:26 -05:00
|
|
|
return CURLE_OK;
|
2014-07-30 18:09:13 -04:00
|
|
|
}
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
static CURLcode wolfssl_sha256sum(const unsigned char *tmp, /* input */
|
|
|
|
size_t tmplen,
|
|
|
|
unsigned char *sha256sum /* output */,
|
|
|
|
size_t unused)
|
2015-06-30 20:23:54 -04:00
|
|
|
{
|
2020-01-16 09:59:56 -05:00
|
|
|
wc_Sha256 SHA256pw;
|
2015-06-30 20:23:54 -04:00
|
|
|
(void)unused;
|
2019-06-02 10:55:05 -04:00
|
|
|
wc_InitSha256(&SHA256pw);
|
|
|
|
wc_Sha256Update(&SHA256pw, tmp, (word32)tmplen);
|
|
|
|
wc_Sha256Final(&SHA256pw, sha256sum);
|
2018-04-02 13:33:00 -04:00
|
|
|
return CURLE_OK;
|
2015-06-30 20:23:54 -04:00
|
|
|
}
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
static void *wolfssl_get_internals(struct ssl_connect_data *connssl,
|
2020-03-18 18:16:02 -04:00
|
|
|
CURLINFO info UNUSED_PARAM)
|
2017-06-23 10:05:26 -04:00
|
|
|
{
|
2020-03-18 18:16:02 -04:00
|
|
|
struct ssl_backend_data *backend = connssl->backend;
|
2017-06-23 10:05:26 -04:00
|
|
|
(void)info;
|
2020-03-18 18:16:02 -04:00
|
|
|
return backend->handle;
|
2017-06-23 10:05:26 -04:00
|
|
|
}
|
|
|
|
|
2019-06-02 10:55:05 -04:00
|
|
|
const struct Curl_ssl Curl_ssl_wolfssl = {
|
2017-08-30 06:48:53 -04:00
|
|
|
{ CURLSSLBACKEND_WOLFSSL, "WolfSSL" }, /* info */
|
2017-06-22 10:45:34 -04:00
|
|
|
|
2017-06-20 05:32:53 -04:00
|
|
|
#ifdef KEEP_PEER_CERT
|
2018-05-04 06:10:39 -04:00
|
|
|
SSLSUPP_PINNEDPUBKEY |
|
2017-06-20 05:32:53 -04:00
|
|
|
#endif
|
2018-05-04 17:02:36 -04:00
|
|
|
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),
|
|
|
|
|
2021-01-13 02:42:36 -05:00
|
|
|
wolfssl_init, /* init */
|
|
|
|
wolfssl_cleanup, /* cleanup */
|
|
|
|
wolfssl_version, /* version */
|
2017-06-22 10:45:34 -04:00
|
|
|
Curl_none_check_cxn, /* check_cxn */
|
2021-01-13 02:42:36 -05:00
|
|
|
wolfssl_shutdown, /* shutdown */
|
|
|
|
wolfssl_data_pending, /* data_pending */
|
|
|
|
wolfssl_random, /* random */
|
2017-06-22 10:45:34 -04:00
|
|
|
Curl_none_cert_status_request, /* cert_status_request */
|
2021-01-13 02:42:36 -05:00
|
|
|
wolfssl_connect, /* connect */
|
|
|
|
wolfssl_connect_nonblocking, /* connect_nonblocking */
|
2021-02-02 20:54:52 -05:00
|
|
|
Curl_ssl_getsock, /* getsock */
|
2021-01-13 02:42:36 -05:00
|
|
|
wolfssl_get_internals, /* get_internals */
|
|
|
|
wolfssl_close, /* close_one */
|
2017-06-22 10:45:34 -04:00
|
|
|
Curl_none_close_all, /* close_all */
|
2021-01-13 02:42:36 -05:00
|
|
|
wolfssl_session_free, /* session_free */
|
2017-06-22 10:45:34 -04:00
|
|
|
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 */
|
2021-01-13 02:42:36 -05:00
|
|
|
wolfssl_sha256sum /* sha256sum */
|
2017-06-22 10:45:34 -04:00
|
|
|
};
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
#endif
|