| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) Daniel Stenberg, <[email protected]>, et al. |
| 9 |
* Copyright (C) Hoi-Ho Chan, <[email protected]> |
| 10 |
* |
| 11 |
* This software is licensed as described in the file COPYING, which |
| 12 |
* you should have received as part of this distribution. The terms |
| 13 |
* are also available at https://curl.se/docs/copyright.html. |
| 14 |
* |
| 15 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 16 |
* copies of the Software, and permit persons to whom the Software is |
| 17 |
* furnished to do so, under the terms of the COPYING file. |
| 18 |
* |
| 19 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 20 |
* KIND, either express or implied. |
| 21 |
* |
| 22 |
* SPDX-License-Identifier: curl |
| 23 |
* |
| 24 |
***************************************************************************/ |
| 25 |
|
| 26 |
/* |
| 27 |
* Source file for all mbedTLS-specific code for the TLS/SSL layer. No code |
| 28 |
* but vtls.c should ever call or use these functions. |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
#include "../curl_setup.h" |
| 33 |
|
| 34 |
#ifdef USE_MBEDTLS |
| 35 |
|
| 36 |
/* Define this to enable lots of debugging for mbedTLS */ |
| 37 |
/* #define MBEDTLS_DEBUG */ |
| 38 |
|
| 39 |
#include <mbedtls/version.h> |
| 40 |
#if MBEDTLS_VERSION_NUMBER >= 0x02040000 |
| 41 |
#include <mbedtls/net_sockets.h> |
| 42 |
#else |
| 43 |
#include <mbedtls/net.h> |
| 44 |
#endif |
| 45 |
#include <mbedtls/ssl.h> |
| 46 |
#include <mbedtls/x509.h> |
| 47 |
|
| 48 |
#include <mbedtls/error.h> |
| 49 |
#include <mbedtls/entropy.h> |
| 50 |
#include <mbedtls/ctr_drbg.h> |
| 51 |
#include <mbedtls/sha256.h> |
| 52 |
|
| 53 |
#if MBEDTLS_VERSION_MAJOR >= 2 |
| 54 |
# ifdef MBEDTLS_DEBUG |
| 55 |
# include <mbedtls/debug.h> |
| 56 |
# endif |
| 57 |
#endif /* MBEDTLS_VERSION_MAJOR >= 2 */ |
| 58 |
|
| 59 |
#include "cipher_suite.h" |
| 60 |
#include "../strcase.h" |
| 61 |
#include "../urldata.h" |
| 62 |
#include "../sendf.h" |
| 63 |
#include "../curlx/inet_pton.h" |
| 64 |
#include "mbedtls.h" |
| 65 |
#include "vtls.h" |
| 66 |
#include "vtls_int.h" |
| 67 |
#include "vtls_scache.h" |
| 68 |
#include "x509asn1.h" |
| 69 |
#include "../parsedate.h" |
| 70 |
#include "../connect.h" /* for the connect timeout */ |
| 71 |
#include "../select.h" |
| 72 |
#include "../multiif.h" |
| 73 |
#include "mbedtls_threadlock.h" |
| 74 |
#include "../strdup.h" |
| 75 |
|
| 76 |
/* The last 3 #include files should be in this order */ |
| 77 |
#include "../curl_printf.h" |
| 78 |
#include "../curl_memory.h" |
| 79 |
#include "../memdebug.h" |
| 80 |
|
| 81 |
/* ALPN for http2 */ |
| 82 |
#if defined(USE_HTTP2) && defined(MBEDTLS_SSL_ALPN) |
| 83 |
# define HAS_ALPN_MBEDTLS |
| 84 |
#endif |
| 85 |
|
| 86 |
struct mbed_ssl_backend_data { |
| 87 |
mbedtls_ctr_drbg_context ctr_drbg; |
| 88 |
mbedtls_entropy_context entropy; |
| 89 |
mbedtls_ssl_context ssl; |
| 90 |
mbedtls_x509_crt cacert; |
| 91 |
mbedtls_x509_crt clicert; |
| 92 |
#ifdef MBEDTLS_X509_CRL_PARSE_C |
| 93 |
mbedtls_x509_crl crl; |
| 94 |
#endif |
| 95 |
mbedtls_pk_context pk; |
| 96 |
mbedtls_ssl_config config; |
| 97 |
#ifdef HAS_ALPN_MBEDTLS |
| 98 |
const char *protocols[3]; |
| 99 |
#endif |
| 100 |
int *ciphersuites; |
| 101 |
size_t send_blocked_len; |
| 102 |
BIT(initialized); /* mbedtls_ssl_context is initialized */ |
| 103 |
BIT(sent_shutdown); |
| 104 |
BIT(send_blocked); |
| 105 |
}; |
| 106 |
|
| 107 |
/* apply threading? */ |
| 108 |
#if (defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)) || \ |
| 109 |
defined(_WIN32) |
| 110 |
#define HAS_THREADING_SUPPORT |
| 111 |
#endif |
| 112 |
|
| 113 |
#ifndef MBEDTLS_ERROR_C |
| 114 |
#define mbedtls_strerror(a,b,c) b[0] = 0 |
| 115 |
#endif |
| 116 |
|
| 117 |
/* PSA can be used independently of TLS 1.3 */ |
| 118 |
#if defined(MBEDTLS_USE_PSA_CRYPTO) && MBEDTLS_VERSION_NUMBER >= 0x03060000 |
| 119 |
#define HAS_PSA_SUPPORT |
| 120 |
#endif |
| 121 |
|
| 122 |
#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && MBEDTLS_VERSION_NUMBER >= 0x03060000 |
| 123 |
#define HAS_TLS13_SUPPORT |
| 124 |
#endif |
| 125 |
|
| 126 |
#if defined(HAS_TLS13_SUPPORT) && defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 127 |
#define HAS_SESSION_TICKETS |
| 128 |
#endif |
| 129 |
|
| 130 |
#ifdef HAS_THREADING_SUPPORT |
| 131 |
static mbedtls_entropy_context ts_entropy; |
| 132 |
|
| 133 |
static int entropy_init_initialized = 0; |
| 134 |
|
| 135 |
static void entropy_init_mutex(mbedtls_entropy_context *ctx) |
| 136 |
{ |
| 137 |
/* lock 0 = entropy_init_mutex() */ |
| 138 |
Curl_mbedtlsthreadlock_lock_function(0); |
| 139 |
if(entropy_init_initialized == 0) { |
| 140 |
mbedtls_entropy_init(ctx); |
| 141 |
entropy_init_initialized = 1; |
| 142 |
} |
| 143 |
Curl_mbedtlsthreadlock_unlock_function(0); |
| 144 |
} |
| 145 |
|
| 146 |
static void entropy_cleanup_mutex(mbedtls_entropy_context *ctx) |
| 147 |
{ |
| 148 |
/* lock 0 = use same lock as init */ |
| 149 |
Curl_mbedtlsthreadlock_lock_function(0); |
| 150 |
if(entropy_init_initialized == 1) { |
| 151 |
mbedtls_entropy_free(ctx); |
| 152 |
entropy_init_initialized = 0; |
| 153 |
} |
| 154 |
Curl_mbedtlsthreadlock_unlock_function(0); |
| 155 |
} |
| 156 |
|
| 157 |
static int entropy_func_mutex(void *data, unsigned char *output, size_t len) |
| 158 |
{ |
| 159 |
int ret; |
| 160 |
/* lock 1 = entropy_func_mutex() */ |
| 161 |
Curl_mbedtlsthreadlock_lock_function(1); |
| 162 |
ret = mbedtls_entropy_func(data, output, len); |
| 163 |
Curl_mbedtlsthreadlock_unlock_function(1); |
| 164 |
|
| 165 |
return ret; |
| 166 |
} |
| 167 |
|
| 168 |
#endif /* HAS_THREADING_SUPPORT */ |
| 169 |
|
| 170 |
#ifdef MBEDTLS_DEBUG |
| 171 |
static void mbed_debug(void *context, int level, const char *f_name, |
| 172 |
int line_nb, const char *line) |
| 173 |
{ |
| 174 |
struct Curl_easy *data = (struct Curl_easy *)context; |
| 175 |
(void) level; |
| 176 |
(void) line_nb; |
| 177 |
(void) f_name; |
| 178 |
|
| 179 |
if(data) { |
| 180 |
size_t len = strlen(line); |
| 181 |
if(len && (line[len - 1] == '\n')) |
| 182 |
/* discount any trailing newline */ |
| 183 |
len--; |
| 184 |
infof(data, "%.*s", (int)len, line); |
| 185 |
} |
| 186 |
} |
| 187 |
#endif |
| 188 |
|
| 189 |
static int mbedtls_bio_cf_write(void *bio, |
| 190 |
const unsigned char *buf, size_t blen) |
| 191 |
{ |
| 192 |
struct Curl_cfilter *cf = bio; |
| 193 |
struct Curl_easy *data = CF_DATA_CURRENT(cf); |
| 194 |
ssize_t nwritten; |
| 195 |
CURLcode result; |
| 196 |
|
| 197 |
DEBUGASSERT(data); |
| 198 |
if(!data) |
| 199 |
return 0; |
| 200 |
|
| 201 |
nwritten = Curl_conn_cf_send(cf->next, data, (const char *)buf, blen, FALSE, |
| 202 |
&result); |
| 203 |
CURL_TRC_CF(data, cf, "mbedtls_bio_cf_out_write(len=%zu) -> %zd, err=%d", |
| 204 |
blen, nwritten, result); |
| 205 |
if(nwritten < 0 && CURLE_AGAIN == result) { |
| 206 |
nwritten = MBEDTLS_ERR_SSL_WANT_WRITE; |
| 207 |
} |
| 208 |
return (int)nwritten; |
| 209 |
} |
| 210 |
|
| 211 |
static int mbedtls_bio_cf_read(void *bio, unsigned char *buf, size_t blen) |
| 212 |
{ |
| 213 |
struct Curl_cfilter *cf = bio; |
| 214 |
struct Curl_easy *data = CF_DATA_CURRENT(cf); |
| 215 |
ssize_t nread; |
| 216 |
CURLcode result; |
| 217 |
|
| 218 |
DEBUGASSERT(data); |
| 219 |
if(!data) |
| 220 |
return 0; |
| 221 |
/* OpenSSL catches this case, so should we. */ |
| 222 |
if(!buf) |
| 223 |
return 0; |
| 224 |
|
| 225 |
nread = Curl_conn_cf_recv(cf->next, data, (char *)buf, blen, &result); |
| 226 |
CURL_TRC_CF(data, cf, "mbedtls_bio_cf_in_read(len=%zu) -> %zd, err=%d", |
| 227 |
blen, nread, result); |
| 228 |
if(nread < 0 && CURLE_AGAIN == result) { |
| 229 |
nread = MBEDTLS_ERR_SSL_WANT_READ; |
| 230 |
} |
| 231 |
return (int)nread; |
| 232 |
} |
| 233 |
|
| 234 |
/* |
| 235 |
* profile |
| 236 |
*/ |
| 237 |
static const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_fr = |
| 238 |
{ |
| 239 |
/* Hashes from SHA-1 and above */ |
| 240 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) | |
| 241 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) | |
| 242 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) | |
| 243 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) | |
| 244 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | |
| 245 |
MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512), |
| 246 |
0xFFFFFFF, /* Any PK alg */ |
| 247 |
0xFFFFFFF, /* Any curve */ |
| 248 |
1024, /* RSA min key len */ |
| 249 |
}; |
| 250 |
|
| 251 |
/* See https://web.archive.org/web/20200921194007/tls.mbed.org/discussions/ |
| 252 |
generic/howto-determine-exact-buffer-len-for-mbedtls_pk_write_pubkey_der |
| 253 |
*/ |
| 254 |
#define RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE) |
| 255 |
#define ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES) |
| 256 |
|
| 257 |
#define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \ |
| 258 |
RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES) |
| 259 |
|
| 260 |
static CURLcode |
| 261 |
mbed_set_ssl_version_min_max(struct Curl_easy *data, |
| 262 |
struct mbed_ssl_backend_data *backend, |
| 263 |
struct ssl_primary_config *conn_config) |
| 264 |
{ |
| 265 |
/* TLS 1.0 and TLS 1.1 were dropped with mbedTLS 3.0.0 (2021). So, since |
| 266 |
* then, and before the introduction of TLS 1.3 in 3.6.0 (2024), this |
| 267 |
* function basically always sets TLS 1.2 as min/max, unless given |
| 268 |
* unsupported option values. */ |
| 269 |
|
| 270 |
#if MBEDTLS_VERSION_NUMBER < 0x03020000 |
| 271 |
int ver_min = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 272 |
int ver_max = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 273 |
#else |
| 274 |
/* mbedTLS 3.2.0 (2022) introduced new methods for setting TLS version */ |
| 275 |
mbedtls_ssl_protocol_version ver_min = MBEDTLS_SSL_VERSION_TLS1_2; |
| 276 |
mbedtls_ssl_protocol_version ver_max = |
| 277 |
#ifdef HAS_TLS13_SUPPORT |
| 278 |
MBEDTLS_SSL_VERSION_TLS1_3 |
| 279 |
#else |
| 280 |
MBEDTLS_SSL_VERSION_TLS1_2 |
| 281 |
#endif |
| 282 |
; |
| 283 |
#endif |
| 284 |
|
| 285 |
switch(conn_config->version) { |
| 286 |
case CURL_SSLVERSION_DEFAULT: |
| 287 |
#if MBEDTLS_VERSION_NUMBER < 0x03000000 |
| 288 |
case CURL_SSLVERSION_TLSv1: |
| 289 |
case CURL_SSLVERSION_TLSv1_0: |
| 290 |
ver_min = MBEDTLS_SSL_MINOR_VERSION_1; |
| 291 |
break; |
| 292 |
case CURL_SSLVERSION_TLSv1_1: |
| 293 |
ver_min = MBEDTLS_SSL_MINOR_VERSION_2; |
| 294 |
break; |
| 295 |
#else |
| 296 |
case CURL_SSLVERSION_TLSv1: |
| 297 |
case CURL_SSLVERSION_TLSv1_0: |
| 298 |
case CURL_SSLVERSION_TLSv1_1: |
| 299 |
#endif |
| 300 |
case CURL_SSLVERSION_TLSv1_2: |
| 301 |
#if MBEDTLS_VERSION_NUMBER < 0x03020000 |
| 302 |
ver_min = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 303 |
#else |
| 304 |
ver_min = MBEDTLS_SSL_VERSION_TLS1_2; |
| 305 |
#endif |
| 306 |
break; |
| 307 |
case CURL_SSLVERSION_TLSv1_3: |
| 308 |
#ifdef HAS_TLS13_SUPPORT |
| 309 |
ver_min = MBEDTLS_SSL_VERSION_TLS1_3; |
| 310 |
break; |
| 311 |
#endif |
| 312 |
default: |
| 313 |
failf(data, "mbedTLS: unsupported minimum TLS version value: %x", |
| 314 |
conn_config->version); |
| 315 |
return CURLE_SSL_CONNECT_ERROR; |
| 316 |
} |
| 317 |
|
| 318 |
switch(conn_config->version_max) { |
| 319 |
case CURL_SSLVERSION_MAX_DEFAULT: |
| 320 |
case CURL_SSLVERSION_MAX_NONE: |
| 321 |
case CURL_SSLVERSION_MAX_TLSv1_3: |
| 322 |
#ifdef HAS_TLS13_SUPPORT |
| 323 |
ver_max = MBEDTLS_SSL_VERSION_TLS1_3; |
| 324 |
break; |
| 325 |
#endif |
| 326 |
case CURL_SSLVERSION_MAX_TLSv1_2: |
| 327 |
#if MBEDTLS_VERSION_NUMBER < 0x03020000 |
| 328 |
ver_max = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */ |
| 329 |
#else |
| 330 |
ver_max = MBEDTLS_SSL_VERSION_TLS1_2; |
| 331 |
#endif |
| 332 |
break; |
| 333 |
#if MBEDTLS_VERSION_NUMBER < 0x03000000 |
| 334 |
case CURL_SSLVERSION_MAX_TLSv1_1: |
| 335 |
ver_max = MBEDTLS_SSL_MINOR_VERSION_2; |
| 336 |
break; |
| 337 |
case CURL_SSLVERSION_MAX_TLSv1_0: |
| 338 |
ver_max = MBEDTLS_SSL_MINOR_VERSION_1; |
| 339 |
break; |
| 340 |
#else |
| 341 |
case CURL_SSLVERSION_MAX_TLSv1_1: |
| 342 |
case CURL_SSLVERSION_MAX_TLSv1_0: |
| 343 |
#endif |
| 344 |
default: |
| 345 |
failf(data, "mbedTLS: unsupported maximum TLS version value"); |
| 346 |
return CURLE_SSL_CONNECT_ERROR; |
| 347 |
} |
| 348 |
|
| 349 |
#if MBEDTLS_VERSION_NUMBER < 0x03020000 |
| 350 |
mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 351 |
ver_min); |
| 352 |
mbedtls_ssl_conf_max_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 353 |
ver_max); |
| 354 |
#else |
| 355 |
mbedtls_ssl_conf_min_tls_version(&backend->config, ver_min); |
| 356 |
mbedtls_ssl_conf_max_tls_version(&backend->config, ver_max); |
| 357 |
#endif |
| 358 |
|
| 359 |
return CURLE_OK; |
| 360 |
} |
| 361 |
|
| 362 |
/* TLS_ECJPAKE_WITH_AES_128_CCM_8 (0xC0FF) is marked experimental |
| 363 |
in mbedTLS. The number is not reserved by IANA nor is the |
| 364 |
cipher suite present in other SSL implementations. Provide |
| 365 |
provisional support for specifying the cipher suite here. */ |
| 366 |
#ifdef MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 |
| 367 |
#if MBEDTLS_VERSION_NUMBER >= 0x03020000 |
| 368 |
static int |
| 369 |
mbed_cipher_suite_get_str(uint16_t id, char *buf, size_t buf_size, |
| 370 |
bool prefer_rfc) |
| 371 |
{ |
| 372 |
if(id == MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8) |
| 373 |
msnprintf(buf, buf_size, "%s", "TLS_ECJPAKE_WITH_AES_128_CCM_8"); |
| 374 |
else |
| 375 |
return Curl_cipher_suite_get_str(id, buf, buf_size, prefer_rfc); |
| 376 |
return 0; |
| 377 |
} |
| 378 |
#endif |
| 379 |
|
| 380 |
static uint16_t |
| 381 |
mbed_cipher_suite_walk_str(const char **str, const char **end) |
| 382 |
{ |
| 383 |
uint16_t id = Curl_cipher_suite_walk_str(str, end); |
| 384 |
size_t len = *end - *str; |
| 385 |
|
| 386 |
if(!id) { |
| 387 |
if(strncasecompare("TLS_ECJPAKE_WITH_AES_128_CCM_8", *str, len)) |
| 388 |
id = MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8; |
| 389 |
} |
| 390 |
return id; |
| 391 |
} |
| 392 |
#else |
| 393 |
#define mbed_cipher_suite_get_str Curl_cipher_suite_get_str |
| 394 |
#define mbed_cipher_suite_walk_str Curl_cipher_suite_walk_str |
| 395 |
#endif |
| 396 |
|
| 397 |
static CURLcode |
| 398 |
mbed_set_selected_ciphers(struct Curl_easy *data, |
| 399 |
struct mbed_ssl_backend_data *backend, |
| 400 |
const char *ciphers12, |
| 401 |
const char *ciphers13) |
| 402 |
{ |
| 403 |
const char *ciphers = ciphers12; |
| 404 |
const int *supported; |
| 405 |
int *selected; |
| 406 |
size_t supported_len, count = 0, default13_count = 0, i, j; |
| 407 |
const char *ptr, *end; |
| 408 |
|
| 409 |
supported = mbedtls_ssl_list_ciphersuites(); |
| 410 |
for(i = 0; supported[i] != 0; i++); |
| 411 |
supported_len = i; |
| 412 |
|
| 413 |
selected = malloc(sizeof(int) * (supported_len + 1)); |
| 414 |
if(!selected) |
| 415 |
return CURLE_OUT_OF_MEMORY; |
| 416 |
|
| 417 |
#ifndef HAS_TLS13_SUPPORT |
| 418 |
(void) ciphers13, (void) j; |
| 419 |
#else |
| 420 |
if(!ciphers13) { |
| 421 |
/* Add default TLSv1.3 ciphers to selection */ |
| 422 |
for(j = 0; j < supported_len; j++) { |
| 423 |
uint16_t id = (uint16_t) supported[j]; |
| 424 |
if(strncmp(mbedtls_ssl_get_ciphersuite_name(id), "TLS1-3", 6) != 0) |
| 425 |
continue; |
| 426 |
|
| 427 |
selected[count++] = id; |
| 428 |
} |
| 429 |
|
| 430 |
default13_count = count; |
| 431 |
} |
| 432 |
else |
| 433 |
ciphers = ciphers13; |
| 434 |
|
| 435 |
add_ciphers: |
| 436 |
#endif |
| 437 |
for(ptr = ciphers; ptr[0] != '\0' && count < supported_len; ptr = end) { |
| 438 |
uint16_t id = mbed_cipher_suite_walk_str(&ptr, &end); |
| 439 |
|
| 440 |
/* Check if cipher is supported */ |
| 441 |
if(id) { |
| 442 |
for(i = 0; i < supported_len && supported[i] != id; i++); |
| 443 |
if(i == supported_len) |
| 444 |
id = 0; |
| 445 |
} |
| 446 |
if(!id) { |
| 447 |
if(ptr[0] != '\0') |
| 448 |
infof(data, "mbedTLS: unknown cipher in list: \"%.*s\"", |
| 449 |
(int) (end - ptr), ptr); |
| 450 |
continue; |
| 451 |
} |
| 452 |
|
| 453 |
/* No duplicates allowed (so selected cannot overflow) */ |
| 454 |
for(i = 0; i < count && selected[i] != id; i++); |
| 455 |
if(i < count) { |
| 456 |
if(i >= default13_count) |
| 457 |
infof(data, "mbedTLS: duplicate cipher in list: \"%.*s\"", |
| 458 |
(int) (end - ptr), ptr); |
| 459 |
continue; |
| 460 |
} |
| 461 |
|
| 462 |
selected[count++] = id; |
| 463 |
} |
| 464 |
|
| 465 |
#ifdef HAS_TLS13_SUPPORT |
| 466 |
if(ciphers == ciphers13 && ciphers12) { |
| 467 |
ciphers = ciphers12; |
| 468 |
goto add_ciphers; |
| 469 |
} |
| 470 |
|
| 471 |
if(!ciphers12) { |
| 472 |
/* Add default TLSv1.2 ciphers to selection */ |
| 473 |
for(j = 0; j < supported_len; j++) { |
| 474 |
uint16_t id = (uint16_t) supported[j]; |
| 475 |
if(strncmp(mbedtls_ssl_get_ciphersuite_name(id), "TLS1-3", 6) == 0) |
| 476 |
continue; |
| 477 |
|
| 478 |
/* No duplicates allowed (so selected cannot overflow) */ |
| 479 |
for(i = 0; i < count && selected[i] != id; i++); |
| 480 |
if(i < count) |
| 481 |
continue; |
| 482 |
|
| 483 |
selected[count++] = id; |
| 484 |
} |
| 485 |
} |
| 486 |
#endif |
| 487 |
|
| 488 |
selected[count] = 0; |
| 489 |
|
| 490 |
if(count == 0) { |
| 491 |
free(selected); |
| 492 |
failf(data, "mbedTLS: no supported cipher in list"); |
| 493 |
return CURLE_SSL_CIPHER; |
| 494 |
} |
| 495 |
|
| 496 |
/* mbedtls_ssl_conf_ciphersuites(): The ciphersuites array is not copied. |
| 497 |
It must remain valid for the lifetime of the SSL configuration */ |
| 498 |
backend->ciphersuites = selected; |
| 499 |
mbedtls_ssl_conf_ciphersuites(&backend->config, backend->ciphersuites); |
| 500 |
return CURLE_OK; |
| 501 |
} |
| 502 |
|
| 503 |
static void |
| 504 |
mbed_dump_cert_info(struct Curl_easy *data, const mbedtls_x509_crt *crt) |
| 505 |
{ |
| 506 |
#if defined(CURL_DISABLE_VERBOSE_STRINGS) || \ |
| 507 |
(MBEDTLS_VERSION_NUMBER >= 0x03000000 && defined(MBEDTLS_X509_REMOVE_INFO)) |
| 508 |
(void) data, (void) crt; |
| 509 |
#else |
| 510 |
const size_t bufsize = 16384; |
| 511 |
char *p, *buffer = malloc(bufsize); |
| 512 |
|
| 513 |
if(buffer && mbedtls_x509_crt_info(buffer, bufsize, " ", crt) > 0) { |
| 514 |
infof(data, "Server certificate:"); |
| 515 |
for(p = buffer; *p; p += *p != '\0') { |
| 516 |
size_t s = strcspn(p, "\n"); |
| 517 |
infof(data, "%.*s", (int) s, p); |
| 518 |
p += s; |
| 519 |
} |
| 520 |
} |
| 521 |
else |
| 522 |
infof(data, "Unable to dump certificate information"); |
| 523 |
|
| 524 |
free(buffer); |
| 525 |
#endif |
| 526 |
} |
| 527 |
|
| 528 |
static void |
| 529 |
mbed_extract_certinfo(struct Curl_easy *data, const mbedtls_x509_crt *crt) |
| 530 |
{ |
| 531 |
CURLcode result; |
| 532 |
const mbedtls_x509_crt *cur; |
| 533 |
int i; |
| 534 |
|
| 535 |
for(i = 0, cur = crt; cur; ++i, cur = cur->next); |
| 536 |
result = Curl_ssl_init_certinfo(data, i); |
| 537 |
|
| 538 |
for(i = 0, cur = crt; result == CURLE_OK && cur; ++i, cur = cur->next) { |
| 539 |
const char *beg = (const char *) cur->raw.p; |
| 540 |
const char *end = beg + cur->raw.len; |
| 541 |
result = Curl_extract_certinfo(data, i, beg, end); |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
static int mbed_verify_cb(void *ptr, mbedtls_x509_crt *crt, |
| 546 |
int depth, uint32_t *flags) |
| 547 |
{ |
| 548 |
struct Curl_cfilter *cf = (struct Curl_cfilter *) ptr; |
| 549 |
struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf); |
| 550 |
struct Curl_easy *data = CF_DATA_CURRENT(cf); |
| 551 |
|
| 552 |
if(depth == 0) { |
| 553 |
if(data->set.verbose) |
| 554 |
mbed_dump_cert_info(data, crt); |
| 555 |
if(data->set.ssl.certinfo) |
| 556 |
mbed_extract_certinfo(data, crt); |
| 557 |
} |
| 558 |
|
| 559 |
if(!conn_config->verifypeer) |
| 560 |
*flags = 0; |
| 561 |
else if(!conn_config->verifyhost) |
| 562 |
*flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH; |
| 563 |
|
| 564 |
if(*flags) { |
| 565 |
#if MBEDTLS_VERSION_NUMBER < 0x03000000 || !defined(MBEDTLS_X509_REMOVE_INFO) |
| 566 |
char buf[128]; |
| 567 |
mbedtls_x509_crt_verify_info(buf, sizeof(buf), "", *flags); |
| 568 |
failf(data, "mbedTLS: %s", buf); |
| 569 |
#else |
| 570 |
failf(data, "mbedTLS: certificate verification error 0x%08x", *flags); |
| 571 |
#endif |
| 572 |
} |
| 573 |
|
| 574 |
return 0; |
| 575 |
} |
| 576 |
|
| 577 |
static CURLcode |
| 578 |
mbed_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 579 |
{ |
| 580 |
struct ssl_connect_data *connssl = cf->ctx; |
| 581 |
struct mbed_ssl_backend_data *backend = |
| 582 |
(struct mbed_ssl_backend_data *)connssl->backend; |
| 583 |
struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf); |
| 584 |
const struct curl_blob *ca_info_blob = conn_config->ca_info_blob; |
| 585 |
struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data); |
| 586 |
const char * const ssl_cafile = |
| 587 |
/* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */ |
| 588 |
(ca_info_blob ? NULL : conn_config->CAfile); |
| 589 |
const bool verifypeer = conn_config->verifypeer; |
| 590 |
const char * const ssl_capath = conn_config->CApath; |
| 591 |
char * const ssl_cert = ssl_config->primary.clientcert; |
| 592 |
const struct curl_blob *ssl_cert_blob = ssl_config->primary.cert_blob; |
| 593 |
const char * const ssl_crlfile = ssl_config->primary.CRLfile; |
| 594 |
const char *hostname = connssl->peer.hostname; |
| 595 |
int ret = -1; |
| 596 |
char errorbuf[128]; |
| 597 |
|
| 598 |
DEBUGASSERT(backend); |
| 599 |
DEBUGASSERT(!backend->initialized); |
| 600 |
|
| 601 |
if((conn_config->version == CURL_SSLVERSION_SSLv2) || |
| 602 |
(conn_config->version == CURL_SSLVERSION_SSLv3)) { |
| 603 |
failf(data, "Not supported SSL version"); |
| 604 |
return CURLE_NOT_BUILT_IN; |
| 605 |
} |
| 606 |
|
| 607 |
#ifdef HAS_THREADING_SUPPORT |
| 608 |
mbedtls_ctr_drbg_init(&backend->ctr_drbg); |
| 609 |
|
| 610 |
ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, entropy_func_mutex, |
| 611 |
&ts_entropy, NULL, 0); |
| 612 |
if(ret) { |
| 613 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 614 |
failf(data, "mbedtls_ctr_drbg_seed returned (-0x%04X) %s", |
| 615 |
-ret, errorbuf); |
| 616 |
return CURLE_FAILED_INIT; |
| 617 |
} |
| 618 |
#else |
| 619 |
mbedtls_entropy_init(&backend->entropy); |
| 620 |
mbedtls_ctr_drbg_init(&backend->ctr_drbg); |
| 621 |
|
| 622 |
ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, mbedtls_entropy_func, |
| 623 |
&backend->entropy, NULL, 0); |
| 624 |
if(ret) { |
| 625 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 626 |
failf(data, "mbedtls_ctr_drbg_seed returned (-0x%04X) %s", |
| 627 |
-ret, errorbuf); |
| 628 |
return CURLE_FAILED_INIT; |
| 629 |
} |
| 630 |
#endif /* HAS_THREADING_SUPPORT */ |
| 631 |
|
| 632 |
/* Load the trusted CA */ |
| 633 |
mbedtls_x509_crt_init(&backend->cacert); |
| 634 |
|
| 635 |
if(ca_info_blob && verifypeer) { |
| 636 |
/* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null |
| 637 |
terminated even when provided the exact length, forcing us to waste |
| 638 |
extra memory here. */ |
| 639 |
unsigned char *newblob = Curl_memdup0(ca_info_blob->data, |
| 640 |
ca_info_blob->len); |
| 641 |
if(!newblob) |
| 642 |
return CURLE_OUT_OF_MEMORY; |
| 643 |
ret = mbedtls_x509_crt_parse(&backend->cacert, newblob, |
| 644 |
ca_info_blob->len + 1); |
| 645 |
free(newblob); |
| 646 |
if(ret < 0) { |
| 647 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 648 |
failf(data, "Error importing ca cert blob - mbedTLS: (-0x%04X) %s", |
| 649 |
-ret, errorbuf); |
| 650 |
return CURLE_SSL_CERTPROBLEM; |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
if(ssl_cafile && verifypeer) { |
| 655 |
#ifdef MBEDTLS_FS_IO |
| 656 |
ret = mbedtls_x509_crt_parse_file(&backend->cacert, ssl_cafile); |
| 657 |
|
| 658 |
if(ret < 0) { |
| 659 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 660 |
failf(data, "Error reading ca cert file %s - mbedTLS: (-0x%04X) %s", |
| 661 |
ssl_cafile, -ret, errorbuf); |
| 662 |
return CURLE_SSL_CACERT_BADFILE; |
| 663 |
} |
| 664 |
#else |
| 665 |
failf(data, "mbedtls: functions that use the filesystem not built in"); |
| 666 |
return CURLE_NOT_BUILT_IN; |
| 667 |
#endif |
| 668 |
} |
| 669 |
|
| 670 |
if(ssl_capath) { |
| 671 |
#ifdef MBEDTLS_FS_IO |
| 672 |
ret = mbedtls_x509_crt_parse_path(&backend->cacert, ssl_capath); |
| 673 |
|
| 674 |
if(ret < 0) { |
| 675 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 676 |
failf(data, "Error reading ca cert path %s - mbedTLS: (-0x%04X) %s", |
| 677 |
ssl_capath, -ret, errorbuf); |
| 678 |
|
| 679 |
if(verifypeer) |
| 680 |
return CURLE_SSL_CACERT_BADFILE; |
| 681 |
} |
| 682 |
#else |
| 683 |
failf(data, "mbedtls: functions that use the filesystem not built in"); |
| 684 |
return CURLE_NOT_BUILT_IN; |
| 685 |
#endif |
| 686 |
} |
| 687 |
|
| 688 |
/* Load the client certificate */ |
| 689 |
mbedtls_x509_crt_init(&backend->clicert); |
| 690 |
|
| 691 |
if(ssl_cert) { |
| 692 |
#ifdef MBEDTLS_FS_IO |
| 693 |
ret = mbedtls_x509_crt_parse_file(&backend->clicert, ssl_cert); |
| 694 |
|
| 695 |
if(ret) { |
| 696 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 697 |
failf(data, "Error reading client cert file %s - mbedTLS: (-0x%04X) %s", |
| 698 |
ssl_cert, -ret, errorbuf); |
| 699 |
|
| 700 |
return CURLE_SSL_CERTPROBLEM; |
| 701 |
} |
| 702 |
#else |
| 703 |
failf(data, "mbedtls: functions that use the filesystem not built in"); |
| 704 |
return CURLE_NOT_BUILT_IN; |
| 705 |
#endif |
| 706 |
} |
| 707 |
|
| 708 |
if(ssl_cert_blob) { |
| 709 |
/* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null |
| 710 |
terminated even when provided the exact length, forcing us to waste |
| 711 |
extra memory here. */ |
| 712 |
unsigned char *newblob = Curl_memdup0(ssl_cert_blob->data, |
| 713 |
ssl_cert_blob->len); |
| 714 |
if(!newblob) |
| 715 |
return CURLE_OUT_OF_MEMORY; |
| 716 |
ret = mbedtls_x509_crt_parse(&backend->clicert, newblob, |
| 717 |
ssl_cert_blob->len + 1); |
| 718 |
free(newblob); |
| 719 |
|
| 720 |
if(ret) { |
| 721 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 722 |
failf(data, "Error reading client cert data %s - mbedTLS: (-0x%04X) %s", |
| 723 |
ssl_config->key, -ret, errorbuf); |
| 724 |
return CURLE_SSL_CERTPROBLEM; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
/* Load the client private key */ |
| 729 |
mbedtls_pk_init(&backend->pk); |
| 730 |
|
| 731 |
if(ssl_config->key || ssl_config->key_blob) { |
| 732 |
if(ssl_config->key) { |
| 733 |
#ifdef MBEDTLS_FS_IO |
| 734 |
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 735 |
ret = mbedtls_pk_parse_keyfile(&backend->pk, ssl_config->key, |
| 736 |
ssl_config->key_passwd, |
| 737 |
mbedtls_ctr_drbg_random, |
| 738 |
&backend->ctr_drbg); |
| 739 |
#else |
| 740 |
ret = mbedtls_pk_parse_keyfile(&backend->pk, ssl_config->key, |
| 741 |
ssl_config->key_passwd); |
| 742 |
#endif |
| 743 |
if(ret == 0 && !(mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_RSA) || |
| 744 |
mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_ECKEY))) |
| 745 |
ret = MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 746 |
|
| 747 |
if(ret) { |
| 748 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 749 |
failf(data, "Error reading private key %s - mbedTLS: (-0x%04X) %s", |
| 750 |
ssl_config->key, -ret, errorbuf); |
| 751 |
return CURLE_SSL_CERTPROBLEM; |
| 752 |
} |
| 753 |
#else |
| 754 |
failf(data, "mbedtls: functions that use the filesystem not built in"); |
| 755 |
return CURLE_NOT_BUILT_IN; |
| 756 |
#endif |
| 757 |
} |
| 758 |
else { |
| 759 |
const struct curl_blob *ssl_key_blob = ssl_config->key_blob; |
| 760 |
const unsigned char *key_data = |
| 761 |
(const unsigned char *)ssl_key_blob->data; |
| 762 |
const char *passwd = ssl_config->key_passwd; |
| 763 |
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 764 |
ret = mbedtls_pk_parse_key(&backend->pk, key_data, ssl_key_blob->len, |
| 765 |
(const unsigned char *)passwd, |
| 766 |
passwd ? strlen(passwd) : 0, |
| 767 |
mbedtls_ctr_drbg_random, |
| 768 |
&backend->ctr_drbg); |
| 769 |
#else |
| 770 |
ret = mbedtls_pk_parse_key(&backend->pk, key_data, ssl_key_blob->len, |
| 771 |
(const unsigned char *)passwd, |
| 772 |
passwd ? strlen(passwd) : 0); |
| 773 |
#endif |
| 774 |
if(ret == 0 && !(mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_RSA) || |
| 775 |
mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_ECKEY))) |
| 776 |
ret = MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 777 |
|
| 778 |
if(ret) { |
| 779 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 780 |
failf(data, "Error parsing private key - mbedTLS: (-0x%04X) %s", |
| 781 |
-ret, errorbuf); |
| 782 |
return CURLE_SSL_CERTPROBLEM; |
| 783 |
} |
| 784 |
} |
| 785 |
} |
| 786 |
|
| 787 |
/* Load the CRL */ |
| 788 |
#ifdef MBEDTLS_X509_CRL_PARSE_C |
| 789 |
mbedtls_x509_crl_init(&backend->crl); |
| 790 |
|
| 791 |
if(ssl_crlfile) { |
| 792 |
#ifdef MBEDTLS_FS_IO |
| 793 |
ret = mbedtls_x509_crl_parse_file(&backend->crl, ssl_crlfile); |
| 794 |
|
| 795 |
if(ret) { |
| 796 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 797 |
failf(data, "Error reading CRL file %s - mbedTLS: (-0x%04X) %s", |
| 798 |
ssl_crlfile, -ret, errorbuf); |
| 799 |
|
| 800 |
return CURLE_SSL_CRL_BADFILE; |
| 801 |
} |
| 802 |
#else |
| 803 |
failf(data, "mbedtls: functions that use the filesystem not built in"); |
| 804 |
return CURLE_NOT_BUILT_IN; |
| 805 |
#endif |
| 806 |
} |
| 807 |
#else |
| 808 |
if(ssl_crlfile) { |
| 809 |
failf(data, "mbedtls: crl support not built in"); |
| 810 |
return CURLE_NOT_BUILT_IN; |
| 811 |
} |
| 812 |
#endif |
| 813 |
|
| 814 |
infof(data, "mbedTLS: Connecting to %s:%d", hostname, connssl->peer.port); |
| 815 |
|
| 816 |
mbedtls_ssl_config_init(&backend->config); |
| 817 |
ret = mbedtls_ssl_config_defaults(&backend->config, |
| 818 |
MBEDTLS_SSL_IS_CLIENT, |
| 819 |
MBEDTLS_SSL_TRANSPORT_STREAM, |
| 820 |
MBEDTLS_SSL_PRESET_DEFAULT); |
| 821 |
if(ret) { |
| 822 |
failf(data, "mbedTLS: ssl_config failed"); |
| 823 |
return CURLE_SSL_CONNECT_ERROR; |
| 824 |
} |
| 825 |
|
| 826 |
#if defined(HAS_SESSION_TICKETS) && MBEDTLS_VERSION_NUMBER >= 0x03060100 |
| 827 |
/* New in mbedTLS 3.6.1, need to enable, default is now disabled */ |
| 828 |
mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(&backend->config, |
| 829 |
MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_ENABLED); |
| 830 |
#endif |
| 831 |
|
| 832 |
/* Always let mbedTLS verify certificates, if verifypeer or verifyhost are |
| 833 |
* disabled we clear the corresponding error flags in the verify callback |
| 834 |
* function. That is also where we log verification errors. */ |
| 835 |
mbedtls_ssl_conf_verify(&backend->config, mbed_verify_cb, cf); |
| 836 |
mbedtls_ssl_conf_authmode(&backend->config, MBEDTLS_SSL_VERIFY_REQUIRED); |
| 837 |
|
| 838 |
mbedtls_ssl_init(&backend->ssl); |
| 839 |
backend->initialized = TRUE; |
| 840 |
|
| 841 |
/* new profile with RSA min key len = 1024 ... */ |
| 842 |
mbedtls_ssl_conf_cert_profile(&backend->config, |
| 843 |
&mbedtls_x509_crt_profile_fr); |
| 844 |
|
| 845 |
ret = mbed_set_ssl_version_min_max(data, backend, conn_config); |
| 846 |
if(ret != CURLE_OK) |
| 847 |
return ret; |
| 848 |
|
| 849 |
mbedtls_ssl_conf_rng(&backend->config, mbedtls_ctr_drbg_random, |
| 850 |
&backend->ctr_drbg); |
| 851 |
|
| 852 |
ret = mbedtls_ssl_setup(&backend->ssl, &backend->config); |
| 853 |
if(ret) { |
| 854 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 855 |
failf(data, "ssl_setup failed - mbedTLS: (-0x%04X) %s", |
| 856 |
-ret, errorbuf); |
| 857 |
return CURLE_SSL_CONNECT_ERROR; |
| 858 |
} |
| 859 |
|
| 860 |
mbedtls_ssl_set_bio(&backend->ssl, cf, |
| 861 |
mbedtls_bio_cf_write, |
| 862 |
mbedtls_bio_cf_read, |
| 863 |
NULL /* rev_timeout() */); |
| 864 |
|
| 865 |
#ifndef HAS_TLS13_SUPPORT |
| 866 |
if(conn_config->cipher_list) { |
| 867 |
CURLcode result = mbed_set_selected_ciphers(data, backend, |
| 868 |
conn_config->cipher_list, |
| 869 |
NULL); |
| 870 |
#else |
| 871 |
if(conn_config->cipher_list || conn_config->cipher_list13) { |
| 872 |
CURLcode result = mbed_set_selected_ciphers(data, backend, |
| 873 |
conn_config->cipher_list, |
| 874 |
conn_config->cipher_list13); |
| 875 |
#endif |
| 876 |
if(result != CURLE_OK) { |
| 877 |
failf(data, "mbedTLS: failed to set cipher suites"); |
| 878 |
return result; |
| 879 |
} |
| 880 |
} |
| 881 |
else { |
| 882 |
mbedtls_ssl_conf_ciphersuites(&backend->config, |
| 883 |
mbedtls_ssl_list_ciphersuites()); |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
#if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 888 |
mbedtls_ssl_conf_renegotiation(&backend->config, |
| 889 |
MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 890 |
#endif |
| 891 |
|
| 892 |
#if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 893 |
mbedtls_ssl_conf_session_tickets(&backend->config, |
| 894 |
MBEDTLS_SSL_SESSION_TICKETS_DISABLED); |
| 895 |
#endif |
| 896 |
|
| 897 |
/* Check if there is a cached ID we can/should use here! */ |
| 898 |
if(ssl_config->primary.cache_session) { |
| 899 |
struct Curl_ssl_session *sc_session = NULL; |
| 900 |
CURLcode result; |
| 901 |
|
| 902 |
result = Curl_ssl_scache_take(cf, data, connssl->peer.scache_key, |
| 903 |
&sc_session); |
| 904 |
if(!result && sc_session && sc_session->sdata && sc_session->sdata_len) { |
| 905 |
mbedtls_ssl_session session; |
| 906 |
|
| 907 |
mbedtls_ssl_session_init(&session); |
| 908 |
ret = mbedtls_ssl_session_load(&session, sc_session->sdata, |
| 909 |
sc_session->sdata_len); |
| 910 |
if(ret) { |
| 911 |
failf(data, "SSL session error loading: -0x%x", -ret); |
| 912 |
} |
| 913 |
else { |
| 914 |
ret = mbedtls_ssl_set_session(&backend->ssl, &session); |
| 915 |
if(ret) |
| 916 |
failf(data, "SSL session error setting: -0x%x", -ret); |
| 917 |
else |
| 918 |
infof(data, "SSL reusing session ID"); |
| 919 |
} |
| 920 |
mbedtls_ssl_session_free(&session); |
| 921 |
} |
| 922 |
Curl_ssl_scache_return(cf, data, connssl->peer.scache_key, sc_session); |
| 923 |
} |
| 924 |
|
| 925 |
mbedtls_ssl_conf_ca_chain(&backend->config, |
| 926 |
&backend->cacert, |
| 927 |
#ifdef MBEDTLS_X509_CRL_PARSE_C |
| 928 |
&backend->crl); |
| 929 |
#else |
| 930 |
NULL); |
| 931 |
#endif |
| 932 |
|
| 933 |
if(ssl_config->key || ssl_config->key_blob) { |
| 934 |
mbedtls_ssl_conf_own_cert(&backend->config, |
| 935 |
&backend->clicert, &backend->pk); |
| 936 |
} |
| 937 |
|
| 938 |
if(mbedtls_ssl_set_hostname(&backend->ssl, connssl->peer.sni ? |
| 939 |
connssl->peer.sni : connssl->peer.hostname)) { |
| 940 |
/* mbedtls_ssl_set_hostname() sets the name to use in CN/SAN checks and |
| 941 |
the name to set in the SNI extension. So even if curl connects to a |
| 942 |
host specified as an IP address, this function must be used. */ |
| 943 |
failf(data, "Failed to set SNI"); |
| 944 |
return CURLE_SSL_CONNECT_ERROR; |
| 945 |
} |
| 946 |
|
| 947 |
#ifdef HAS_ALPN_MBEDTLS |
| 948 |
if(connssl->alpn) { |
| 949 |
struct alpn_proto_buf proto; |
| 950 |
size_t i; |
| 951 |
|
| 952 |
for(i = 0; i < connssl->alpn->count; ++i) { |
| 953 |
backend->protocols[i] = connssl->alpn->entries[i]; |
| 954 |
} |
| 955 |
/* this function does not clone the protocols array, which is why we need |
| 956 |
to keep it around */ |
| 957 |
if(mbedtls_ssl_conf_alpn_protocols(&backend->config, |
| 958 |
&backend->protocols[0])) { |
| 959 |
failf(data, "Failed setting ALPN protocols"); |
| 960 |
return CURLE_SSL_CONNECT_ERROR; |
| 961 |
} |
| 962 |
Curl_alpn_to_proto_str(&proto, connssl->alpn); |
| 963 |
infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data); |
| 964 |
} |
| 965 |
#endif |
| 966 |
|
| 967 |
#ifdef MBEDTLS_DEBUG |
| 968 |
/* In order to make that work in mbedtls MBEDTLS_DEBUG_C must be defined. */ |
| 969 |
mbedtls_ssl_conf_dbg(&backend->config, mbed_debug, data); |
| 970 |
/* - 0 No debug |
| 971 |
* - 1 Error |
| 972 |
* - 2 State change |
| 973 |
* - 3 Informational |
| 974 |
* - 4 Verbose |
| 975 |
*/ |
| 976 |
mbedtls_debug_set_threshold(4); |
| 977 |
#endif |
| 978 |
|
| 979 |
/* give application a chance to interfere with mbedTLS set up. */ |
| 980 |
if(data->set.ssl.fsslctx) { |
| 981 |
CURLcode result = (*data->set.ssl.fsslctx)(data, &backend->config, |
| 982 |
data->set.ssl.fsslctxp); |
| 983 |
if(result != CURLE_OK) { |
| 984 |
failf(data, "error signaled by ssl ctx callback"); |
| 985 |
return result; |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
connssl->connecting_state = ssl_connect_2; |
| 990 |
|
| 991 |
return CURLE_OK; |
| 992 |
} |
| 993 |
|
| 994 |
static CURLcode |
| 995 |
mbed_connect_step2(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 996 |
{ |
| 997 |
int ret; |
| 998 |
struct ssl_connect_data *connssl = cf->ctx; |
| 999 |
struct mbed_ssl_backend_data *backend = |
| 1000 |
(struct mbed_ssl_backend_data *)connssl->backend; |
| 1001 |
#ifndef CURL_DISABLE_PROXY |
| 1002 |
const char * const pinnedpubkey = Curl_ssl_cf_is_proxy(cf) ? |
| 1003 |
data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] : |
| 1004 |
data->set.str[STRING_SSL_PINNEDPUBLICKEY]; |
| 1005 |
#else |
| 1006 |
const char * const pinnedpubkey = data->set.str[STRING_SSL_PINNEDPUBLICKEY]; |
| 1007 |
#endif |
| 1008 |
|
| 1009 |
DEBUGASSERT(backend); |
| 1010 |
|
| 1011 |
ret = mbedtls_ssl_handshake(&backend->ssl); |
| 1012 |
|
| 1013 |
if(ret == MBEDTLS_ERR_SSL_WANT_READ) { |
| 1014 |
connssl->io_need = CURL_SSL_IO_NEED_RECV; |
| 1015 |
return CURLE_OK; |
| 1016 |
} |
| 1017 |
else if(ret == MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 1018 |
connssl->io_need = CURL_SSL_IO_NEED_SEND; |
| 1019 |
return CURLE_OK; |
| 1020 |
} |
| 1021 |
else if(ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) { |
| 1022 |
failf(data, "peer certificate could not be verified"); |
| 1023 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 1024 |
} |
| 1025 |
else if(ret) { |
| 1026 |
char errorbuf[128]; |
| 1027 |
#if MBEDTLS_VERSION_NUMBER >= 0x03020000 |
| 1028 |
CURL_TRC_CF(data, cf, "TLS version %04X", |
| 1029 |
mbedtls_ssl_get_version_number(&backend->ssl)); |
| 1030 |
#endif |
| 1031 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 1032 |
failf(data, "ssl_handshake returned: (-0x%04X) %s", |
| 1033 |
-ret, errorbuf); |
| 1034 |
return CURLE_SSL_CONNECT_ERROR; |
| 1035 |
} |
| 1036 |
|
| 1037 |
#if MBEDTLS_VERSION_NUMBER >= 0x03020000 |
| 1038 |
{ |
| 1039 |
char cipher_str[64]; |
| 1040 |
uint16_t cipher_id; |
| 1041 |
cipher_id = (uint16_t) |
| 1042 |
mbedtls_ssl_get_ciphersuite_id_from_ssl(&backend->ssl); |
| 1043 |
mbed_cipher_suite_get_str(cipher_id, cipher_str, sizeof(cipher_str), TRUE); |
| 1044 |
infof(data, "mbedTLS: %s Handshake complete, cipher is %s", |
| 1045 |
mbedtls_ssl_get_version(&backend->ssl), cipher_str); |
| 1046 |
} |
| 1047 |
#else |
| 1048 |
infof(data, "mbedTLS: %s Handshake complete", |
| 1049 |
mbedtls_ssl_get_version(&backend->ssl)); |
| 1050 |
#endif |
| 1051 |
|
| 1052 |
if(pinnedpubkey) { |
| 1053 |
int size; |
| 1054 |
CURLcode result; |
| 1055 |
const mbedtls_x509_crt *peercert; |
| 1056 |
mbedtls_x509_crt *p = NULL; |
| 1057 |
unsigned char *pubkey = NULL; |
| 1058 |
|
| 1059 |
peercert = mbedtls_ssl_get_peer_cert(&backend->ssl); |
| 1060 |
#if MBEDTLS_VERSION_NUMBER == 0x03000000 |
| 1061 |
if(!peercert || !peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(p) || |
| 1062 |
!peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(len)) { |
| 1063 |
#else |
| 1064 |
if(!peercert || !peercert->raw.p || !peercert->raw.len) { |
| 1065 |
#endif |
| 1066 |
failf(data, "Failed due to missing peer certificate"); |
| 1067 |
return CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 1068 |
} |
| 1069 |
|
| 1070 |
p = calloc(1, sizeof(*p)); |
| 1071 |
|
| 1072 |
if(!p) |
| 1073 |
return CURLE_OUT_OF_MEMORY; |
| 1074 |
|
| 1075 |
pubkey = malloc(PUB_DER_MAX_BYTES); |
| 1076 |
|
| 1077 |
if(!pubkey) { |
| 1078 |
result = CURLE_OUT_OF_MEMORY; |
| 1079 |
goto pinnedpubkey_error; |
| 1080 |
} |
| 1081 |
|
| 1082 |
mbedtls_x509_crt_init(p); |
| 1083 |
|
| 1084 |
/* Make a copy of our const peercert because mbedtls_pk_write_pubkey_der |
| 1085 |
needs a non-const key, for now. |
| 1086 |
https://github.com/Mbed-TLS/mbedtls/issues/396 */ |
| 1087 |
#if MBEDTLS_VERSION_NUMBER == 0x03000000 |
| 1088 |
if(mbedtls_x509_crt_parse_der(p, |
| 1089 |
peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(p), |
| 1090 |
peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(len))) { |
| 1091 |
#else |
| 1092 |
if(mbedtls_x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) { |
| 1093 |
#endif |
| 1094 |
failf(data, "Failed copying peer certificate"); |
| 1095 |
result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 1096 |
goto pinnedpubkey_error; |
| 1097 |
} |
| 1098 |
|
| 1099 |
#if MBEDTLS_VERSION_NUMBER == 0x03000000 |
| 1100 |
size = mbedtls_pk_write_pubkey_der(&p->MBEDTLS_PRIVATE(pk), pubkey, |
| 1101 |
PUB_DER_MAX_BYTES); |
| 1102 |
#else |
| 1103 |
size = mbedtls_pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES); |
| 1104 |
#endif |
| 1105 |
|
| 1106 |
if(size <= 0) { |
| 1107 |
failf(data, "Failed copying public key from peer certificate"); |
| 1108 |
result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 1109 |
goto pinnedpubkey_error; |
| 1110 |
} |
| 1111 |
|
| 1112 |
/* mbedtls_pk_write_pubkey_der writes data at the end of the buffer. */ |
| 1113 |
result = Curl_pin_peer_pubkey(data, |
| 1114 |
pinnedpubkey, |
| 1115 |
&pubkey[PUB_DER_MAX_BYTES - size], size); |
| 1116 |
pinnedpubkey_error: |
| 1117 |
mbedtls_x509_crt_free(p); |
| 1118 |
free(p); |
| 1119 |
free(pubkey); |
| 1120 |
if(result) { |
| 1121 |
return result; |
| 1122 |
} |
| 1123 |
} |
| 1124 |
|
| 1125 |
#ifdef HAS_ALPN_MBEDTLS |
| 1126 |
if(connssl->alpn) { |
| 1127 |
const char *proto = mbedtls_ssl_get_alpn_protocol(&backend->ssl); |
| 1128 |
|
| 1129 |
Curl_alpn_set_negotiated(cf, data, connssl, (const unsigned char *)proto, |
| 1130 |
proto ? strlen(proto) : 0); |
| 1131 |
} |
| 1132 |
#endif |
| 1133 |
|
| 1134 |
connssl->connecting_state = ssl_connect_3; |
| 1135 |
infof(data, "SSL connected"); |
| 1136 |
|
| 1137 |
return CURLE_OK; |
| 1138 |
} |
| 1139 |
|
| 1140 |
static CURLcode |
| 1141 |
mbed_new_session(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 1142 |
{ |
| 1143 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1144 |
struct mbed_ssl_backend_data *backend = |
| 1145 |
(struct mbed_ssl_backend_data *)connssl->backend; |
| 1146 |
struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data); |
| 1147 |
mbedtls_ssl_session session; |
| 1148 |
bool msession_alloced = FALSE; |
| 1149 |
struct Curl_ssl_session *sc_session = NULL; |
| 1150 |
unsigned char *sdata = NULL; |
| 1151 |
size_t slen = 0; |
| 1152 |
int ietf_tls_id; |
| 1153 |
CURLcode result = CURLE_OK; |
| 1154 |
int ret; |
| 1155 |
|
| 1156 |
DEBUGASSERT(backend); |
| 1157 |
if(!ssl_config->primary.cache_session) |
| 1158 |
return CURLE_OK; |
| 1159 |
|
| 1160 |
mbedtls_ssl_session_init(&session); |
| 1161 |
ret = mbedtls_ssl_get_session(&backend->ssl, &session); |
| 1162 |
msession_alloced = (ret != MBEDTLS_ERR_SSL_ALLOC_FAILED); |
| 1163 |
if(ret) { |
| 1164 |
failf(data, "mbedtls_ssl_get_session returned -0x%x", -ret); |
| 1165 |
result = CURLE_SSL_CONNECT_ERROR; |
| 1166 |
goto out; |
| 1167 |
} |
| 1168 |
|
| 1169 |
mbedtls_ssl_session_save(&session, NULL, 0, &slen); |
| 1170 |
if(!slen) { |
| 1171 |
failf(data, "failed to serialize session: length is 0"); |
| 1172 |
goto out; |
| 1173 |
} |
| 1174 |
|
| 1175 |
sdata = malloc(slen); |
| 1176 |
if(!sdata) { |
| 1177 |
result = CURLE_OUT_OF_MEMORY; |
| 1178 |
goto out; |
| 1179 |
} |
| 1180 |
|
| 1181 |
ret = mbedtls_ssl_session_save(&session, sdata, slen, &slen); |
| 1182 |
if(ret) { |
| 1183 |
failf(data, "failed to serialize session: -0x%x", -ret); |
| 1184 |
goto out; |
| 1185 |
} |
| 1186 |
|
| 1187 |
#if MBEDTLS_VERSION_NUMBER >= 0x03020000 |
| 1188 |
ietf_tls_id = mbedtls_ssl_get_version_number(&backend->ssl); |
| 1189 |
#else |
| 1190 |
ietf_tls_id = CURL_IETF_PROTO_UNKNOWN; |
| 1191 |
#endif |
| 1192 |
result = Curl_ssl_session_create(sdata, slen, |
| 1193 |
ietf_tls_id, |
| 1194 |
connssl->negotiated.alpn, 0, 0, |
| 1195 |
&sc_session); |
| 1196 |
sdata = NULL; /* call took ownership */ |
| 1197 |
if(!result) |
| 1198 |
result = Curl_ssl_scache_put(cf, data, connssl->peer.scache_key, |
| 1199 |
sc_session); |
| 1200 |
|
| 1201 |
out: |
| 1202 |
if(msession_alloced) |
| 1203 |
mbedtls_ssl_session_free(&session); |
| 1204 |
free(sdata); |
| 1205 |
return result; |
| 1206 |
} |
| 1207 |
|
| 1208 |
static ssize_t mbed_send(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 1209 |
const void *mem, size_t len, |
| 1210 |
CURLcode *curlcode) |
| 1211 |
{ |
| 1212 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1213 |
struct mbed_ssl_backend_data *backend = |
| 1214 |
(struct mbed_ssl_backend_data *)connssl->backend; |
| 1215 |
int ret = -1; |
| 1216 |
|
| 1217 |
(void)data; |
| 1218 |
DEBUGASSERT(backend); |
| 1219 |
/* mbedtls is picky when a mbedtls_ssl_write) was previously blocked. |
| 1220 |
* It requires to be called with the same amount of bytes again, or it |
| 1221 |
* will lose bytes, e.g. reporting all was sent but they were not. |
| 1222 |
* Remember the blocked length and use that when set. */ |
| 1223 |
if(backend->send_blocked) { |
| 1224 |
DEBUGASSERT(backend->send_blocked_len <= len); |
| 1225 |
CURL_TRC_CF(data, cf, "mbedtls_ssl_write(len=%zu) -> previously blocked " |
| 1226 |
"on %zu bytes", len, backend->send_blocked_len); |
| 1227 |
len = backend->send_blocked_len; |
| 1228 |
} |
| 1229 |
|
| 1230 |
ret = mbedtls_ssl_write(&backend->ssl, (const unsigned char *)mem, len); |
| 1231 |
|
| 1232 |
if(ret < 0) { |
| 1233 |
CURL_TRC_CF(data, cf, "mbedtls_ssl_write(len=%zu) -> -0x%04X", |
| 1234 |
len, -ret); |
| 1235 |
*curlcode = ((ret == MBEDTLS_ERR_SSL_WANT_WRITE) |
| 1236 |
#ifdef HAS_TLS13_SUPPORT |
| 1237 |
|| (ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) |
| 1238 |
#endif |
| 1239 |
) ? CURLE_AGAIN : CURLE_SEND_ERROR; |
| 1240 |
ret = -1; |
| 1241 |
if((*curlcode == CURLE_AGAIN) && !backend->send_blocked) { |
| 1242 |
backend->send_blocked = TRUE; |
| 1243 |
backend->send_blocked_len = len; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
else { |
| 1247 |
CURL_TRC_CF(data, cf, "mbedtls_ssl_write(len=%zu) -> %d", len, ret); |
| 1248 |
backend->send_blocked = FALSE; |
| 1249 |
} |
| 1250 |
|
| 1251 |
return ret; |
| 1252 |
} |
| 1253 |
|
| 1254 |
static CURLcode mbedtls_shutdown(struct Curl_cfilter *cf, |
| 1255 |
struct Curl_easy *data, |
| 1256 |
bool send_shutdown, bool *done) |
| 1257 |
{ |
| 1258 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1259 |
struct mbed_ssl_backend_data *backend = |
| 1260 |
(struct mbed_ssl_backend_data *)connssl->backend; |
| 1261 |
unsigned char buf[1024]; |
| 1262 |
CURLcode result = CURLE_OK; |
| 1263 |
int ret; |
| 1264 |
size_t i; |
| 1265 |
|
| 1266 |
DEBUGASSERT(backend); |
| 1267 |
|
| 1268 |
if(!backend->initialized || cf->shutdown) { |
| 1269 |
*done = TRUE; |
| 1270 |
return CURLE_OK; |
| 1271 |
} |
| 1272 |
|
| 1273 |
connssl->io_need = CURL_SSL_IO_NEED_NONE; |
| 1274 |
*done = FALSE; |
| 1275 |
|
| 1276 |
if(!backend->sent_shutdown) { |
| 1277 |
/* do this only once */ |
| 1278 |
backend->sent_shutdown = TRUE; |
| 1279 |
if(send_shutdown) { |
| 1280 |
ret = mbedtls_ssl_close_notify(&backend->ssl); |
| 1281 |
switch(ret) { |
| 1282 |
case 0: /* we sent it, receive from the server */ |
| 1283 |
break; |
| 1284 |
case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: /* server also closed */ |
| 1285 |
*done = TRUE; |
| 1286 |
goto out; |
| 1287 |
case MBEDTLS_ERR_SSL_WANT_READ: |
| 1288 |
connssl->io_need = CURL_SSL_IO_NEED_RECV; |
| 1289 |
goto out; |
| 1290 |
case MBEDTLS_ERR_SSL_WANT_WRITE: |
| 1291 |
connssl->io_need = CURL_SSL_IO_NEED_SEND; |
| 1292 |
goto out; |
| 1293 |
default: |
| 1294 |
CURL_TRC_CF(data, cf, "mbedtls_shutdown error -0x%04X", -ret); |
| 1295 |
result = CURLE_RECV_ERROR; |
| 1296 |
goto out; |
| 1297 |
} |
| 1298 |
} |
| 1299 |
} |
| 1300 |
|
| 1301 |
/* SSL should now have started the shutdown from our side. Since it |
| 1302 |
* was not complete, we are lacking the close notify from the server. */ |
| 1303 |
for(i = 0; i < 10; ++i) { |
| 1304 |
ret = mbedtls_ssl_read(&backend->ssl, buf, sizeof(buf)); |
| 1305 |
/* This seems to be a bug in mbedTLS TLSv1.3 where it reports |
| 1306 |
* WANT_READ, but has not encountered an EAGAIN. */ |
| 1307 |
if(ret == MBEDTLS_ERR_SSL_WANT_READ) |
| 1308 |
ret = mbedtls_ssl_read(&backend->ssl, buf, sizeof(buf)); |
| 1309 |
#ifdef HAS_TLS13_SUPPORT |
| 1310 |
if(ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET) |
| 1311 |
continue; |
| 1312 |
#endif |
| 1313 |
if(ret <= 0) |
| 1314 |
break; |
| 1315 |
} |
| 1316 |
|
| 1317 |
if(ret > 0) { |
| 1318 |
/* still data coming in? */ |
| 1319 |
CURL_TRC_CF(data, cf, "mbedtls_shutdown, still getting data"); |
| 1320 |
} |
| 1321 |
else if(ret == 0 || (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)) { |
| 1322 |
/* We got the close notify alert and are done. */ |
| 1323 |
CURL_TRC_CF(data, cf, "mbedtls_shutdown done"); |
| 1324 |
*done = TRUE; |
| 1325 |
} |
| 1326 |
else if(ret == MBEDTLS_ERR_SSL_WANT_READ) { |
| 1327 |
CURL_TRC_CF(data, cf, "mbedtls_shutdown, need RECV"); |
| 1328 |
connssl->io_need = CURL_SSL_IO_NEED_RECV; |
| 1329 |
} |
| 1330 |
else if(ret == MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 1331 |
CURL_TRC_CF(data, cf, "mbedtls_shutdown, need SEND"); |
| 1332 |
connssl->io_need = CURL_SSL_IO_NEED_SEND; |
| 1333 |
} |
| 1334 |
else { |
| 1335 |
CURL_TRC_CF(data, cf, "mbedtls_shutdown error -0x%04X", -ret); |
| 1336 |
result = CURLE_RECV_ERROR; |
| 1337 |
} |
| 1338 |
|
| 1339 |
out: |
| 1340 |
cf->shutdown = (result || *done); |
| 1341 |
return result; |
| 1342 |
} |
| 1343 |
|
| 1344 |
static void mbedtls_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 1345 |
{ |
| 1346 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1347 |
struct mbed_ssl_backend_data *backend = |
| 1348 |
(struct mbed_ssl_backend_data *)connssl->backend; |
| 1349 |
|
| 1350 |
(void)data; |
| 1351 |
DEBUGASSERT(backend); |
| 1352 |
if(backend->initialized) { |
| 1353 |
mbedtls_pk_free(&backend->pk); |
| 1354 |
mbedtls_x509_crt_free(&backend->clicert); |
| 1355 |
mbedtls_x509_crt_free(&backend->cacert); |
| 1356 |
#ifdef MBEDTLS_X509_CRL_PARSE_C |
| 1357 |
mbedtls_x509_crl_free(&backend->crl); |
| 1358 |
#endif |
| 1359 |
Curl_safefree(backend->ciphersuites); |
| 1360 |
mbedtls_ssl_config_free(&backend->config); |
| 1361 |
mbedtls_ssl_free(&backend->ssl); |
| 1362 |
mbedtls_ctr_drbg_free(&backend->ctr_drbg); |
| 1363 |
#ifndef HAS_THREADING_SUPPORT |
| 1364 |
mbedtls_entropy_free(&backend->entropy); |
| 1365 |
#endif /* HAS_THREADING_SUPPORT */ |
| 1366 |
backend->initialized = FALSE; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
static ssize_t mbed_recv(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 1371 |
char *buf, size_t buffersize, |
| 1372 |
CURLcode *curlcode) |
| 1373 |
{ |
| 1374 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1375 |
struct mbed_ssl_backend_data *backend = |
| 1376 |
(struct mbed_ssl_backend_data *)connssl->backend; |
| 1377 |
int ret = -1; |
| 1378 |
|
| 1379 |
(void)data; |
| 1380 |
DEBUGASSERT(backend); |
| 1381 |
|
| 1382 |
ret = mbedtls_ssl_read(&backend->ssl, (unsigned char *)buf, |
| 1383 |
buffersize); |
| 1384 |
if(ret <= 0) { |
| 1385 |
CURL_TRC_CF(data, cf, "mbedtls_ssl_read(len=%zu) -> -0x%04X", |
| 1386 |
buffersize, -ret); |
| 1387 |
switch(ret) { |
| 1388 |
#ifdef HAS_SESSION_TICKETS |
| 1389 |
case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET: |
| 1390 |
mbed_new_session(cf, data); |
| 1391 |
FALLTHROUGH(); |
| 1392 |
#endif |
| 1393 |
case MBEDTLS_ERR_SSL_WANT_READ: |
| 1394 |
*curlcode = CURLE_AGAIN; |
| 1395 |
ret = -1; |
| 1396 |
break; |
| 1397 |
case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: |
| 1398 |
*curlcode = CURLE_OK; |
| 1399 |
ret = 0; |
| 1400 |
break; |
| 1401 |
default: { |
| 1402 |
char errorbuf[128]; |
| 1403 |
mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 1404 |
failf(data, "ssl_read returned: (-0x%04X) %s", -ret, errorbuf); |
| 1405 |
*curlcode = CURLE_RECV_ERROR; |
| 1406 |
ret = -1; |
| 1407 |
break; |
| 1408 |
} |
| 1409 |
} |
| 1410 |
} |
| 1411 |
return (ssize_t)ret; |
| 1412 |
} |
| 1413 |
|
| 1414 |
static size_t mbedtls_version(char *buffer, size_t size) |
| 1415 |
{ |
| 1416 |
#ifdef MBEDTLS_VERSION_C |
| 1417 |
/* if mbedtls_version_get_number() is available it is better */ |
| 1418 |
unsigned int version = mbedtls_version_get_number(); |
| 1419 |
return msnprintf(buffer, size, "mbedTLS/%u.%u.%u", version >> 24, |
| 1420 |
(version >> 16) & 0xff, (version >> 8) & 0xff); |
| 1421 |
#else |
| 1422 |
return msnprintf(buffer, size, "mbedTLS/%s", MBEDTLS_VERSION_STRING); |
| 1423 |
#endif |
| 1424 |
} |
| 1425 |
|
| 1426 |
/* 'data' might be NULL */ |
| 1427 |
static CURLcode mbedtls_random(struct Curl_easy *data, |
| 1428 |
unsigned char *entropy, size_t length) |
| 1429 |
{ |
| 1430 |
#if defined(MBEDTLS_CTR_DRBG_C) |
| 1431 |
int ret; |
| 1432 |
mbedtls_entropy_context ctr_entropy; |
| 1433 |
mbedtls_ctr_drbg_context ctr_drbg; |
| 1434 |
mbedtls_entropy_init(&ctr_entropy); |
| 1435 |
mbedtls_ctr_drbg_init(&ctr_drbg); |
| 1436 |
(void)data; |
| 1437 |
|
| 1438 |
ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, |
| 1439 |
&ctr_entropy, NULL, 0); |
| 1440 |
|
| 1441 |
if(!ret) |
| 1442 |
ret = mbedtls_ctr_drbg_random(&ctr_drbg, entropy, length); |
| 1443 |
|
| 1444 |
mbedtls_ctr_drbg_free(&ctr_drbg); |
| 1445 |
mbedtls_entropy_free(&ctr_entropy); |
| 1446 |
|
| 1447 |
return ret == 0 ? CURLE_OK : CURLE_FAILED_INIT; |
| 1448 |
#elif defined(MBEDTLS_HAVEGE_C) |
| 1449 |
mbedtls_havege_state hs; |
| 1450 |
mbedtls_havege_init(&hs); |
| 1451 |
mbedtls_havege_random(&hs, entropy, length); |
| 1452 |
mbedtls_havege_free(&hs); |
| 1453 |
return CURLE_OK; |
| 1454 |
#else |
| 1455 |
return CURLE_NOT_BUILT_IN; |
| 1456 |
#endif |
| 1457 |
} |
| 1458 |
|
| 1459 |
static CURLcode mbedtls_connect(struct Curl_cfilter *cf, |
| 1460 |
struct Curl_easy *data, |
| 1461 |
bool *done) |
| 1462 |
{ |
| 1463 |
CURLcode retcode; |
| 1464 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1465 |
|
| 1466 |
/* check if the connection has already been established */ |
| 1467 |
if(ssl_connection_complete == connssl->state) { |
| 1468 |
*done = TRUE; |
| 1469 |
return CURLE_OK; |
| 1470 |
} |
| 1471 |
|
| 1472 |
*done = FALSE; |
| 1473 |
connssl->io_need = CURL_SSL_IO_NEED_NONE; |
| 1474 |
|
| 1475 |
if(ssl_connect_1 == connssl->connecting_state) { |
| 1476 |
retcode = mbed_connect_step1(cf, data); |
| 1477 |
if(retcode) |
| 1478 |
return retcode; |
| 1479 |
} |
| 1480 |
|
| 1481 |
if(ssl_connect_2 == connssl->connecting_state) { |
| 1482 |
retcode = mbed_connect_step2(cf, data); |
| 1483 |
if(retcode) |
| 1484 |
return retcode; |
| 1485 |
} |
| 1486 |
|
| 1487 |
if(ssl_connect_3 == connssl->connecting_state) { |
| 1488 |
/* For tls1.3 we get notified about new sessions */ |
| 1489 |
#if MBEDTLS_VERSION_NUMBER >= 0x03020000 |
| 1490 |
struct ssl_connect_data *ctx = cf->ctx; |
| 1491 |
struct mbed_ssl_backend_data *backend = |
| 1492 |
(struct mbed_ssl_backend_data *)ctx->backend; |
| 1493 |
|
| 1494 |
if(mbedtls_ssl_get_version_number(&backend->ssl) <= |
| 1495 |
MBEDTLS_SSL_VERSION_TLS1_2) { |
| 1496 |
#else |
| 1497 |
{ /* no TLSv1.3 supported here */ |
| 1498 |
#endif |
| 1499 |
retcode = mbed_new_session(cf, data); |
| 1500 |
if(retcode) |
| 1501 |
return retcode; |
| 1502 |
} |
| 1503 |
connssl->connecting_state = ssl_connect_done; |
| 1504 |
} |
| 1505 |
|
| 1506 |
if(ssl_connect_done == connssl->connecting_state) { |
| 1507 |
connssl->state = ssl_connection_complete; |
| 1508 |
*done = TRUE; |
| 1509 |
} |
| 1510 |
|
| 1511 |
return CURLE_OK; |
| 1512 |
} |
| 1513 |
|
| 1514 |
/* |
| 1515 |
* return 0 error initializing SSL |
| 1516 |
* return 1 SSL initialized successfully |
| 1517 |
*/ |
| 1518 |
static int mbedtls_init(void) |
| 1519 |
{ |
| 1520 |
if(!Curl_mbedtlsthreadlock_thread_setup()) |
| 1521 |
return 0; |
| 1522 |
#ifdef HAS_THREADING_SUPPORT |
| 1523 |
entropy_init_mutex(&ts_entropy); |
| 1524 |
#endif |
| 1525 |
#ifdef HAS_PSA_SUPPORT |
| 1526 |
{ |
| 1527 |
int ret; |
| 1528 |
#ifdef HAS_THREADING_SUPPORT |
| 1529 |
Curl_mbedtlsthreadlock_lock_function(0); |
| 1530 |
#endif |
| 1531 |
ret = psa_crypto_init(); |
| 1532 |
#ifdef HAS_THREADING_SUPPORT |
| 1533 |
Curl_mbedtlsthreadlock_unlock_function(0); |
| 1534 |
#endif |
| 1535 |
if(ret != PSA_SUCCESS) |
| 1536 |
return 0; |
| 1537 |
} |
| 1538 |
#endif /* HAS_PSA_SUPPORT */ |
| 1539 |
return 1; |
| 1540 |
} |
| 1541 |
|
| 1542 |
static void mbedtls_cleanup(void) |
| 1543 |
{ |
| 1544 |
#ifdef HAS_THREADING_SUPPORT |
| 1545 |
entropy_cleanup_mutex(&ts_entropy); |
| 1546 |
#endif |
| 1547 |
(void)Curl_mbedtlsthreadlock_thread_cleanup(); |
| 1548 |
} |
| 1549 |
|
| 1550 |
static bool mbedtls_data_pending(struct Curl_cfilter *cf, |
| 1551 |
const struct Curl_easy *data) |
| 1552 |
{ |
| 1553 |
struct ssl_connect_data *ctx = cf->ctx; |
| 1554 |
struct mbed_ssl_backend_data *backend; |
| 1555 |
|
| 1556 |
(void)data; |
| 1557 |
DEBUGASSERT(ctx && ctx->backend); |
| 1558 |
backend = (struct mbed_ssl_backend_data *)ctx->backend; |
| 1559 |
return mbedtls_ssl_get_bytes_avail(&backend->ssl) != 0; |
| 1560 |
} |
| 1561 |
|
| 1562 |
static CURLcode mbedtls_sha256sum(const unsigned char *input, |
| 1563 |
size_t inputlen, |
| 1564 |
unsigned char *sha256sum, |
| 1565 |
size_t sha256len UNUSED_PARAM) |
| 1566 |
{ |
| 1567 |
(void)sha256len; |
| 1568 |
#if MBEDTLS_VERSION_NUMBER < 0x02070000 |
| 1569 |
mbedtls_sha256(input, inputlen, sha256sum, 0); |
| 1570 |
#else |
| 1571 |
/* returns 0 on success, otherwise failure */ |
| 1572 |
#if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 1573 |
if(mbedtls_sha256(input, inputlen, sha256sum, 0) != 0) |
| 1574 |
#else |
| 1575 |
if(mbedtls_sha256_ret(input, inputlen, sha256sum, 0) != 0) |
| 1576 |
#endif |
| 1577 |
return CURLE_BAD_FUNCTION_ARGUMENT; |
| 1578 |
#endif |
| 1579 |
return CURLE_OK; |
| 1580 |
} |
| 1581 |
|
| 1582 |
static void *mbedtls_get_internals(struct ssl_connect_data *connssl, |
| 1583 |
CURLINFO info UNUSED_PARAM) |
| 1584 |
{ |
| 1585 |
struct mbed_ssl_backend_data *backend = |
| 1586 |
(struct mbed_ssl_backend_data *)connssl->backend; |
| 1587 |
(void)info; |
| 1588 |
DEBUGASSERT(backend); |
| 1589 |
return &backend->ssl; |
| 1590 |
} |
| 1591 |
|
| 1592 |
const struct Curl_ssl Curl_ssl_mbedtls = { |
| 1593 |
{ CURLSSLBACKEND_MBEDTLS, "mbedtls" }, /* info */ |
| 1594 |
|
| 1595 |
SSLSUPP_CA_PATH | |
| 1596 |
SSLSUPP_CAINFO_BLOB | |
| 1597 |
SSLSUPP_CERTINFO | |
| 1598 |
SSLSUPP_PINNEDPUBKEY | |
| 1599 |
SSLSUPP_SSL_CTX | |
| 1600 |
#ifdef HAS_TLS13_SUPPORT |
| 1601 |
SSLSUPP_TLS13_CIPHERSUITES | |
| 1602 |
#endif |
| 1603 |
SSLSUPP_HTTPS_PROXY | |
| 1604 |
SSLSUPP_CIPHER_LIST, |
| 1605 |
|
| 1606 |
sizeof(struct mbed_ssl_backend_data), |
| 1607 |
|
| 1608 |
mbedtls_init, /* init */ |
| 1609 |
mbedtls_cleanup, /* cleanup */ |
| 1610 |
mbedtls_version, /* version */ |
| 1611 |
mbedtls_shutdown, /* shutdown */ |
| 1612 |
mbedtls_data_pending, /* data_pending */ |
| 1613 |
mbedtls_random, /* random */ |
| 1614 |
NULL, /* cert_status_request */ |
| 1615 |
mbedtls_connect, /* connect */ |
| 1616 |
Curl_ssl_adjust_pollset, /* adjust_pollset */ |
| 1617 |
mbedtls_get_internals, /* get_internals */ |
| 1618 |
mbedtls_close, /* close_one */ |
| 1619 |
NULL, /* close_all */ |
| 1620 |
NULL, /* set_engine */ |
| 1621 |
NULL, /* set_engine_default */ |
| 1622 |
NULL, /* engines_list */ |
| 1623 |
NULL, /* false_start */ |
| 1624 |
mbedtls_sha256sum, /* sha256sum */ |
| 1625 |
mbed_recv, /* recv decrypted data */ |
| 1626 |
mbed_send, /* send data to encrypt */ |
| 1627 |
NULL, /* get_channel_binding */ |
| 1628 |
}; |
| 1629 |
|
| 1630 |
#endif /* USE_MBEDTLS */ |