| 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
| 8 | * Copyright (C) 2012 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al. |
| 9 | * Copyright (C) 2010 - 2011, Hoi-Ho Chan, <hoiho.chan@gmail.com> |
| 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 | ***************************************************************************/ |
| 23 | |
| 24 | /* |
| 25 | * Source file for all mbedTLS-specific code for the TLS/SSL layer. No code |
| 26 | * but vtls.c should ever call or use these functions. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include "curl_setup.h" |
| 31 | |
| 32 | #ifdef USE_MBEDTLS |
| 33 | |
| 34 | /* Define this to enable lots of debugging for mbedTLS */ |
| 35 | /* #define MBEDTLS_DEBUG */ |
| 36 | |
| 37 | #include <mbedtls/version.h> |
| 38 | #if MBEDTLS_VERSION_NUMBER >= 0x02040000 |
| 39 | #include <mbedtls/net_sockets.h> |
| 40 | #else |
| 41 | #include <mbedtls/net.h> |
| 42 | #endif |
| 43 | #include <mbedtls/ssl.h> |
| 44 | #if MBEDTLS_VERSION_NUMBER < 0x03000000 |
| 45 | #include <mbedtls/certs.h> |
| 46 | #endif |
| 47 | #include <mbedtls/x509.h> |
| 48 | |
| 49 | #include <mbedtls/error.h> |
| 50 | #include <mbedtls/entropy.h> |
| 51 | #include <mbedtls/ctr_drbg.h> |
| 52 | #include <mbedtls/sha256.h> |
| 53 | |
| 54 | #if MBEDTLS_VERSION_MAJOR >= 2 |
| 55 | # ifdef MBEDTLS_DEBUG |
| 56 | # include <mbedtls/debug.h> |
| 57 | # endif |
| 58 | #endif |
| 59 | |
| 60 | #include "urldata.h" |
| 61 | #include "sendf.h" |
| 62 | #include "inet_pton.h" |
| 63 | #include "mbedtls.h" |
| 64 | #include "vtls.h" |
| 65 | #include "parsedate.h" |
| 66 | #include "connect.h" /* for the connect timeout */ |
| 67 | #include "select.h" |
| 68 | #include "multiif.h" |
| 69 | #include "mbedtls_threadlock.h" |
| 70 | |
| 71 | /* The last 3 #include files should be in this order */ |
| 72 | #include "curl_printf.h" |
| 73 | #include "curl_memory.h" |
| 74 | #include "memdebug.h" |
| 75 | |
| 76 | struct ssl_backend_data { |
| 77 | mbedtls_ctr_drbg_context ctr_drbg; |
| 78 | mbedtls_entropy_context entropy; |
| 79 | mbedtls_ssl_context ssl; |
| 80 | int server_fd; |
| 81 | mbedtls_x509_crt cacert; |
| 82 | mbedtls_x509_crt clicert; |
| 83 | mbedtls_x509_crl crl; |
| 84 | mbedtls_pk_context pk; |
| 85 | mbedtls_ssl_config config; |
| 86 | const char *protocols[3]; |
| 87 | }; |
| 88 | |
| 89 | /* apply threading? */ |
| 90 | #if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32) |
| 91 | #define THREADING_SUPPORT |
| 92 | #endif |
| 93 | |
| 94 | #ifndef MBEDTLS_ERROR_C |
| 95 | #define mbedtls_strerror(a,b,c) b[0] = 0 |
| 96 | #endif |
| 97 | |
| 98 | #if defined(THREADING_SUPPORT) |
| 99 | static mbedtls_entropy_context ts_entropy; |
| 100 | |
| 101 | static int entropy_init_initialized = 0; |
| 102 | |
| 103 | /* start of entropy_init_mutex() */ |
| 104 | static void entropy_init_mutex(mbedtls_entropy_context *ctx) |
| 105 | { |
| 106 | /* lock 0 = entropy_init_mutex() */ |
| 107 | Curl_mbedtlsthreadlock_lock_function(0); |
| 108 | if(entropy_init_initialized == 0) { |
| 109 | mbedtls_entropy_init(ctx); |
| 110 | entropy_init_initialized = 1; |
| 111 | } |
| 112 | Curl_mbedtlsthreadlock_unlock_function(0); |
| 113 | } |
| 114 | /* end of entropy_init_mutex() */ |
| 115 | |
| 116 | /* start of entropy_func_mutex() */ |
| 117 | static int entropy_func_mutex(void *data, unsigned char *output, size_t len) |
| 118 | { |
| 119 | int ret; |
| 120 | /* lock 1 = entropy_func_mutex() */ |
| 121 | Curl_mbedtlsthreadlock_lock_function(1); |
| 122 | ret = mbedtls_entropy_func(data, output, len); |
| 123 | Curl_mbedtlsthreadlock_unlock_function(1); |
| 124 | |
| 125 | return ret; |
| 126 | } |
| 127 | /* end of entropy_func_mutex() */ |
| 128 | |
| 129 | #endif /* THREADING_SUPPORT */ |
| 130 | |
| 131 | #ifdef MBEDTLS_DEBUG |
| 132 | static void mbed_debug(void *context, int level, const char *f_name, |
| 133 | int line_nb, const char *line) |
| 134 | { |
| 135 | struct Curl_easy *data = NULL; |
| 136 | |
| 137 | if(!context) |
| 138 | return; |
| 139 | |
| 140 | data = (struct Curl_easy *)context; |
| 141 | |
| 142 | infof(data, "%s" , line); |
| 143 | (void) level; |
| 144 | } |
| 145 | #else |
| 146 | #endif |
| 147 | |
| 148 | /* ALPN for http2? */ |
| 149 | #ifdef USE_NGHTTP2 |
| 150 | # undef HAS_ALPN |
| 151 | # ifdef MBEDTLS_SSL_ALPN |
| 152 | # define HAS_ALPN |
| 153 | # endif |
| 154 | #endif |
| 155 | |
| 156 | |
| 157 | /* |
| 158 | * profile |
| 159 | */ |
| 160 | static const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_fr = |
| 161 | { |
| 162 | /* Hashes from SHA-1 and above */ |
| 163 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) | |
| 164 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) | |
| 165 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) | |
| 166 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) | |
| 167 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) | |
| 168 | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512), |
| 169 | 0xFFFFFFF, /* Any PK alg */ |
| 170 | 0xFFFFFFF, /* Any curve */ |
| 171 | 1024, /* RSA min key len */ |
| 172 | }; |
| 173 | |
| 174 | /* See https://tls.mbed.org/discussions/generic/ |
| 175 | howto-determine-exact-buffer-len-for-mbedtls_pk_write_pubkey_der |
| 176 | */ |
| 177 | #define RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE) |
| 178 | #define ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES) |
| 179 | |
| 180 | #define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \ |
| 181 | RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES) |
| 182 | |
| 183 | static Curl_recv mbed_recv; |
| 184 | static Curl_send mbed_send; |
| 185 | |
| 186 | static CURLcode mbedtls_version_from_curl(int *mbedver, long version) |
| 187 | { |
| 188 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 189 | switch(version) { |
| 190 | case CURL_SSLVERSION_TLSv1_0: |
| 191 | case CURL_SSLVERSION_TLSv1_1: |
| 192 | case CURL_SSLVERSION_TLSv1_2: |
| 193 | *mbedver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 194 | return CURLE_OK; |
| 195 | case CURL_SSLVERSION_TLSv1_3: |
| 196 | break; |
| 197 | } |
| 198 | #else |
| 199 | switch(version) { |
| 200 | case CURL_SSLVERSION_TLSv1_0: |
| 201 | *mbedver = MBEDTLS_SSL_MINOR_VERSION_1; |
| 202 | return CURLE_OK; |
| 203 | case CURL_SSLVERSION_TLSv1_1: |
| 204 | *mbedver = MBEDTLS_SSL_MINOR_VERSION_2; |
| 205 | return CURLE_OK; |
| 206 | case CURL_SSLVERSION_TLSv1_2: |
| 207 | *mbedver = MBEDTLS_SSL_MINOR_VERSION_3; |
| 208 | return CURLE_OK; |
| 209 | case CURL_SSLVERSION_TLSv1_3: |
| 210 | break; |
| 211 | } |
| 212 | #endif |
| 213 | |
| 214 | return CURLE_SSL_CONNECT_ERROR; |
| 215 | } |
| 216 | |
| 217 | static CURLcode |
| 218 | set_ssl_version_min_max(struct Curl_easy *data, struct connectdata *conn, |
| 219 | int sockindex) |
| 220 | { |
| 221 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 222 | struct ssl_backend_data *backend = connssl->backend; |
| 223 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 224 | int mbedtls_ver_min = MBEDTLS_SSL_MINOR_VERSION_3; |
| 225 | int mbedtls_ver_max = MBEDTLS_SSL_MINOR_VERSION_3; |
| 226 | #else |
| 227 | int mbedtls_ver_min = MBEDTLS_SSL_MINOR_VERSION_1; |
| 228 | int mbedtls_ver_max = MBEDTLS_SSL_MINOR_VERSION_1; |
| 229 | #endif |
| 230 | long ssl_version = SSL_CONN_CONFIG(version); |
| 231 | long ssl_version_max = SSL_CONN_CONFIG(version_max); |
| 232 | CURLcode result = CURLE_OK; |
| 233 | |
| 234 | switch(ssl_version) { |
| 235 | case CURL_SSLVERSION_DEFAULT: |
| 236 | case CURL_SSLVERSION_TLSv1: |
| 237 | ssl_version = CURL_SSLVERSION_TLSv1_0; |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | switch(ssl_version_max) { |
| 242 | case CURL_SSLVERSION_MAX_NONE: |
| 243 | case CURL_SSLVERSION_MAX_DEFAULT: |
| 244 | ssl_version_max = CURL_SSLVERSION_MAX_TLSv1_2; |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | result = mbedtls_version_from_curl(&mbedtls_ver_min, ssl_version); |
| 249 | if(result) { |
| 250 | failf(data, "unsupported min version passed via CURLOPT_SSLVERSION" ); |
| 251 | return result; |
| 252 | } |
| 253 | result = mbedtls_version_from_curl(&mbedtls_ver_max, ssl_version_max >> 16); |
| 254 | if(result) { |
| 255 | failf(data, "unsupported max version passed via CURLOPT_SSLVERSION" ); |
| 256 | return result; |
| 257 | } |
| 258 | |
| 259 | mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 260 | mbedtls_ver_min); |
| 261 | mbedtls_ssl_conf_max_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 262 | mbedtls_ver_max); |
| 263 | |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | static CURLcode |
| 268 | mbed_connect_step1(struct Curl_easy *data, struct connectdata *conn, |
| 269 | int sockindex) |
| 270 | { |
| 271 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 272 | struct ssl_backend_data *backend = connssl->backend; |
| 273 | const char * const ssl_cafile = SSL_CONN_CONFIG(CAfile); |
| 274 | const bool verifypeer = SSL_CONN_CONFIG(verifypeer); |
| 275 | const char * const ssl_capath = SSL_CONN_CONFIG(CApath); |
| 276 | char * const ssl_cert = SSL_SET_OPTION(primary.clientcert); |
| 277 | const struct curl_blob *ssl_cert_blob = SSL_SET_OPTION(primary.cert_blob); |
| 278 | const char * const ssl_crlfile = SSL_SET_OPTION(CRLfile); |
| 279 | const char * const hostname = SSL_HOST_NAME(); |
| 280 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
| 281 | const long int port = SSL_HOST_PORT(); |
| 282 | #endif |
| 283 | int ret = -1; |
| 284 | char errorbuf[128]; |
| 285 | |
| 286 | if((SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2) || |
| 287 | (SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv3)) { |
| 288 | failf(data, "Not supported SSL version" ); |
| 289 | return CURLE_NOT_BUILT_IN; |
| 290 | } |
| 291 | |
| 292 | #ifdef THREADING_SUPPORT |
| 293 | entropy_init_mutex(&ts_entropy); |
| 294 | mbedtls_ctr_drbg_init(&backend->ctr_drbg); |
| 295 | |
| 296 | ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, entropy_func_mutex, |
| 297 | &ts_entropy, NULL, 0); |
| 298 | if(ret) { |
| 299 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 300 | failf(data, "Failed - mbedTLS: ctr_drbg_init returned (-0x%04X) %s" , |
| 301 | -ret, errorbuf); |
| 302 | } |
| 303 | #else |
| 304 | mbedtls_entropy_init(&backend->entropy); |
| 305 | mbedtls_ctr_drbg_init(&backend->ctr_drbg); |
| 306 | |
| 307 | ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, mbedtls_entropy_func, |
| 308 | &backend->entropy, NULL, 0); |
| 309 | if(ret) { |
| 310 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 311 | failf(data, "Failed - mbedTLS: ctr_drbg_init returned (-0x%04X) %s" , |
| 312 | -ret, errorbuf); |
| 313 | } |
| 314 | #endif /* THREADING_SUPPORT */ |
| 315 | |
| 316 | /* Load the trusted CA */ |
| 317 | mbedtls_x509_crt_init(&backend->cacert); |
| 318 | |
| 319 | if(ssl_cafile) { |
| 320 | ret = mbedtls_x509_crt_parse_file(&backend->cacert, ssl_cafile); |
| 321 | |
| 322 | if(ret<0) { |
| 323 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 324 | failf(data, "Error reading ca cert file %s - mbedTLS: (-0x%04X) %s" , |
| 325 | ssl_cafile, -ret, errorbuf); |
| 326 | |
| 327 | if(verifypeer) |
| 328 | return CURLE_SSL_CACERT_BADFILE; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if(ssl_capath) { |
| 333 | ret = mbedtls_x509_crt_parse_path(&backend->cacert, ssl_capath); |
| 334 | |
| 335 | if(ret<0) { |
| 336 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 337 | failf(data, "Error reading ca cert path %s - mbedTLS: (-0x%04X) %s" , |
| 338 | ssl_capath, -ret, errorbuf); |
| 339 | |
| 340 | if(verifypeer) |
| 341 | return CURLE_SSL_CACERT_BADFILE; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | /* Load the client certificate */ |
| 346 | mbedtls_x509_crt_init(&backend->clicert); |
| 347 | |
| 348 | if(ssl_cert) { |
| 349 | ret = mbedtls_x509_crt_parse_file(&backend->clicert, ssl_cert); |
| 350 | |
| 351 | if(ret) { |
| 352 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 353 | failf(data, "Error reading client cert file %s - mbedTLS: (-0x%04X) %s" , |
| 354 | ssl_cert, -ret, errorbuf); |
| 355 | |
| 356 | return CURLE_SSL_CERTPROBLEM; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | if(ssl_cert_blob) { |
| 361 | const unsigned char *blob_data = |
| 362 | (const unsigned char *)ssl_cert_blob->data; |
| 363 | ret = mbedtls_x509_crt_parse(&backend->clicert, blob_data, |
| 364 | ssl_cert_blob->len); |
| 365 | |
| 366 | if(ret) { |
| 367 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 368 | failf(data, "Error reading private key %s - mbedTLS: (-0x%04X) %s" , |
| 369 | SSL_SET_OPTION(key), -ret, errorbuf); |
| 370 | return CURLE_SSL_CERTPROBLEM; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /* Load the client private key */ |
| 375 | mbedtls_pk_init(&backend->pk); |
| 376 | |
| 377 | if(SSL_SET_OPTION(key) || SSL_SET_OPTION(key_blob)) { |
| 378 | if(SSL_SET_OPTION(key)) { |
| 379 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 380 | ret = mbedtls_pk_parse_keyfile(&backend->pk, SSL_SET_OPTION(key), |
| 381 | SSL_SET_OPTION(key_passwd), |
| 382 | mbedtls_ctr_drbg_random, |
| 383 | &backend->ctr_drbg); |
| 384 | #else |
| 385 | ret = mbedtls_pk_parse_keyfile(&backend->pk, SSL_SET_OPTION(key), |
| 386 | SSL_SET_OPTION(key_passwd)); |
| 387 | #endif |
| 388 | |
| 389 | if(ret) { |
| 390 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 391 | failf(data, "Error reading private key %s - mbedTLS: (-0x%04X) %s" , |
| 392 | SSL_SET_OPTION(key), -ret, errorbuf); |
| 393 | return CURLE_SSL_CERTPROBLEM; |
| 394 | } |
| 395 | } |
| 396 | else { |
| 397 | const struct curl_blob *ssl_key_blob = SSL_SET_OPTION(key_blob); |
| 398 | const unsigned char *key_data = |
| 399 | (const unsigned char *)ssl_key_blob->data; |
| 400 | const char *passwd = SSL_SET_OPTION(key_passwd); |
| 401 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 402 | ret = mbedtls_pk_parse_key(&backend->pk, key_data, ssl_key_blob->len, |
| 403 | (const unsigned char *)passwd, |
| 404 | passwd ? strlen(passwd) : 0, |
| 405 | mbedtls_ctr_drbg_random, |
| 406 | &backend->ctr_drbg); |
| 407 | #else |
| 408 | ret = mbedtls_pk_parse_key(&backend->pk, key_data, ssl_key_blob->len, |
| 409 | (const unsigned char *)passwd, |
| 410 | passwd ? strlen(passwd) : 0); |
| 411 | #endif |
| 412 | |
| 413 | if(ret) { |
| 414 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 415 | failf(data, "Error parsing private key - mbedTLS: (-0x%04X) %s" , |
| 416 | -ret, errorbuf); |
| 417 | return CURLE_SSL_CERTPROBLEM; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | if(ret == 0 && !(mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_RSA) || |
| 422 | mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_ECKEY))) |
| 423 | ret = MBEDTLS_ERR_PK_TYPE_MISMATCH; |
| 424 | } |
| 425 | |
| 426 | /* Load the CRL */ |
| 427 | mbedtls_x509_crl_init(&backend->crl); |
| 428 | |
| 429 | if(ssl_crlfile) { |
| 430 | ret = mbedtls_x509_crl_parse_file(&backend->crl, ssl_crlfile); |
| 431 | |
| 432 | if(ret) { |
| 433 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 434 | failf(data, "Error reading CRL file %s - mbedTLS: (-0x%04X) %s" , |
| 435 | ssl_crlfile, -ret, errorbuf); |
| 436 | |
| 437 | return CURLE_SSL_CRL_BADFILE; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | infof(data, "mbedTLS: Connecting to %s:%ld" , hostname, port); |
| 442 | |
| 443 | mbedtls_ssl_config_init(&backend->config); |
| 444 | |
| 445 | mbedtls_ssl_init(&backend->ssl); |
| 446 | if(mbedtls_ssl_setup(&backend->ssl, &backend->config)) { |
| 447 | failf(data, "mbedTLS: ssl_init failed" ); |
| 448 | return CURLE_SSL_CONNECT_ERROR; |
| 449 | } |
| 450 | ret = mbedtls_ssl_config_defaults(&backend->config, |
| 451 | MBEDTLS_SSL_IS_CLIENT, |
| 452 | MBEDTLS_SSL_TRANSPORT_STREAM, |
| 453 | MBEDTLS_SSL_PRESET_DEFAULT); |
| 454 | if(ret) { |
| 455 | failf(data, "mbedTLS: ssl_config failed" ); |
| 456 | return CURLE_SSL_CONNECT_ERROR; |
| 457 | } |
| 458 | |
| 459 | /* new profile with RSA min key len = 1024 ... */ |
| 460 | mbedtls_ssl_conf_cert_profile(&backend->config, |
| 461 | &mbedtls_x509_crt_profile_fr); |
| 462 | |
| 463 | switch(SSL_CONN_CONFIG(version)) { |
| 464 | case CURL_SSLVERSION_DEFAULT: |
| 465 | case CURL_SSLVERSION_TLSv1: |
| 466 | #if MBEDTLS_VERSION_NUMBER < 0x03000000 |
| 467 | mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3, |
| 468 | MBEDTLS_SSL_MINOR_VERSION_1); |
| 469 | infof(data, "mbedTLS: Set min SSL version to TLS 1.0" ); |
| 470 | break; |
| 471 | #endif |
| 472 | case CURL_SSLVERSION_TLSv1_0: |
| 473 | case CURL_SSLVERSION_TLSv1_1: |
| 474 | case CURL_SSLVERSION_TLSv1_2: |
| 475 | case CURL_SSLVERSION_TLSv1_3: |
| 476 | { |
| 477 | CURLcode result = set_ssl_version_min_max(data, conn, sockindex); |
| 478 | if(result != CURLE_OK) |
| 479 | return result; |
| 480 | break; |
| 481 | } |
| 482 | default: |
| 483 | failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION" ); |
| 484 | return CURLE_SSL_CONNECT_ERROR; |
| 485 | } |
| 486 | |
| 487 | mbedtls_ssl_conf_authmode(&backend->config, MBEDTLS_SSL_VERIFY_OPTIONAL); |
| 488 | |
| 489 | mbedtls_ssl_conf_rng(&backend->config, mbedtls_ctr_drbg_random, |
| 490 | &backend->ctr_drbg); |
| 491 | mbedtls_ssl_set_bio(&backend->ssl, &conn->sock[sockindex], |
| 492 | mbedtls_net_send, |
| 493 | mbedtls_net_recv, |
| 494 | NULL /* rev_timeout() */); |
| 495 | |
| 496 | mbedtls_ssl_conf_ciphersuites(&backend->config, |
| 497 | mbedtls_ssl_list_ciphersuites()); |
| 498 | |
| 499 | #if defined(MBEDTLS_SSL_RENEGOTIATION) |
| 500 | mbedtls_ssl_conf_renegotiation(&backend->config, |
| 501 | MBEDTLS_SSL_RENEGOTIATION_ENABLED); |
| 502 | #endif |
| 503 | |
| 504 | #if defined(MBEDTLS_SSL_SESSION_TICKETS) |
| 505 | mbedtls_ssl_conf_session_tickets(&backend->config, |
| 506 | MBEDTLS_SSL_SESSION_TICKETS_DISABLED); |
| 507 | #endif |
| 508 | |
| 509 | /* Check if there's a cached ID we can/should use here! */ |
| 510 | if(SSL_SET_OPTION(primary.sessionid)) { |
| 511 | void *old_session = NULL; |
| 512 | |
| 513 | Curl_ssl_sessionid_lock(data); |
| 514 | if(!Curl_ssl_getsessionid(data, conn, |
| 515 | SSL_IS_PROXY() ? TRUE : FALSE, |
| 516 | &old_session, NULL, sockindex)) { |
| 517 | ret = mbedtls_ssl_set_session(&backend->ssl, old_session); |
| 518 | if(ret) { |
| 519 | Curl_ssl_sessionid_unlock(data); |
| 520 | failf(data, "mbedtls_ssl_set_session returned -0x%x" , -ret); |
| 521 | return CURLE_SSL_CONNECT_ERROR; |
| 522 | } |
| 523 | infof(data, "mbedTLS re-using session" ); |
| 524 | } |
| 525 | Curl_ssl_sessionid_unlock(data); |
| 526 | } |
| 527 | |
| 528 | mbedtls_ssl_conf_ca_chain(&backend->config, |
| 529 | &backend->cacert, |
| 530 | &backend->crl); |
| 531 | |
| 532 | if(SSL_SET_OPTION(key) || SSL_SET_OPTION(key_blob)) { |
| 533 | mbedtls_ssl_conf_own_cert(&backend->config, |
| 534 | &backend->clicert, &backend->pk); |
| 535 | } |
| 536 | if(mbedtls_ssl_set_hostname(&backend->ssl, hostname)) { |
| 537 | /* mbedtls_ssl_set_hostname() sets the name to use in CN/SAN checks *and* |
| 538 | the name to set in the SNI extension. So even if curl connects to a |
| 539 | host specified as an IP address, this function must be used. */ |
| 540 | failf(data, "couldn't set hostname in mbedTLS" ); |
| 541 | return CURLE_SSL_CONNECT_ERROR; |
| 542 | } |
| 543 | |
| 544 | #ifdef HAS_ALPN |
| 545 | if(conn->bits.tls_enable_alpn) { |
| 546 | const char **p = &backend->protocols[0]; |
| 547 | #ifdef USE_NGHTTP2 |
| 548 | if(data->state.httpwant >= CURL_HTTP_VERSION_2) |
| 549 | *p++ = NGHTTP2_PROTO_VERSION_ID; |
| 550 | #endif |
| 551 | *p++ = ALPN_HTTP_1_1; |
| 552 | *p = NULL; |
| 553 | /* this function doesn't clone the protocols array, which is why we need |
| 554 | to keep it around */ |
| 555 | if(mbedtls_ssl_conf_alpn_protocols(&backend->config, |
| 556 | &backend->protocols[0])) { |
| 557 | failf(data, "Failed setting ALPN protocols" ); |
| 558 | return CURLE_SSL_CONNECT_ERROR; |
| 559 | } |
| 560 | for(p = &backend->protocols[0]; *p; ++p) |
| 561 | infof(data, "ALPN, offering %s" , *p); |
| 562 | } |
| 563 | #endif |
| 564 | |
| 565 | #ifdef MBEDTLS_DEBUG |
| 566 | /* In order to make that work in mbedtls MBEDTLS_DEBUG_C must be defined. */ |
| 567 | mbedtls_ssl_conf_dbg(&backend->config, mbed_debug, data); |
| 568 | /* - 0 No debug |
| 569 | * - 1 Error |
| 570 | * - 2 State change |
| 571 | * - 3 Informational |
| 572 | * - 4 Verbose |
| 573 | */ |
| 574 | mbedtls_debug_set_threshold(4); |
| 575 | #endif |
| 576 | |
| 577 | /* give application a chance to interfere with mbedTLS set up. */ |
| 578 | if(data->set.ssl.fsslctx) { |
| 579 | ret = (*data->set.ssl.fsslctx)(data, &backend->config, |
| 580 | data->set.ssl.fsslctxp); |
| 581 | if(ret) { |
| 582 | failf(data, "error signaled by ssl ctx callback" ); |
| 583 | return ret; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | connssl->connecting_state = ssl_connect_2; |
| 588 | |
| 589 | return CURLE_OK; |
| 590 | } |
| 591 | |
| 592 | static CURLcode |
| 593 | mbed_connect_step2(struct Curl_easy *data, struct connectdata *conn, |
| 594 | int sockindex) |
| 595 | { |
| 596 | int ret; |
| 597 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 598 | struct ssl_backend_data *backend = connssl->backend; |
| 599 | const mbedtls_x509_crt *peercert; |
| 600 | const char * const pinnedpubkey = SSL_PINNED_PUB_KEY(); |
| 601 | |
| 602 | conn->recv[sockindex] = mbed_recv; |
| 603 | conn->send[sockindex] = mbed_send; |
| 604 | |
| 605 | ret = mbedtls_ssl_handshake(&backend->ssl); |
| 606 | |
| 607 | if(ret == MBEDTLS_ERR_SSL_WANT_READ) { |
| 608 | connssl->connecting_state = ssl_connect_2_reading; |
| 609 | return CURLE_OK; |
| 610 | } |
| 611 | else if(ret == MBEDTLS_ERR_SSL_WANT_WRITE) { |
| 612 | connssl->connecting_state = ssl_connect_2_writing; |
| 613 | return CURLE_OK; |
| 614 | } |
| 615 | else if(ret) { |
| 616 | char errorbuf[128]; |
| 617 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 618 | failf(data, "ssl_handshake returned - mbedTLS: (-0x%04X) %s" , |
| 619 | -ret, errorbuf); |
| 620 | return CURLE_SSL_CONNECT_ERROR; |
| 621 | } |
| 622 | |
| 623 | infof(data, "mbedTLS: Handshake complete, cipher is %s" , |
| 624 | mbedtls_ssl_get_ciphersuite(&backend->ssl)); |
| 625 | |
| 626 | ret = mbedtls_ssl_get_verify_result(&backend->ssl); |
| 627 | |
| 628 | if(!SSL_CONN_CONFIG(verifyhost)) |
| 629 | /* Ignore hostname errors if verifyhost is disabled */ |
| 630 | ret &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH; |
| 631 | |
| 632 | if(ret && SSL_CONN_CONFIG(verifypeer)) { |
| 633 | if(ret & MBEDTLS_X509_BADCERT_EXPIRED) |
| 634 | failf(data, "Cert verify failed: BADCERT_EXPIRED" ); |
| 635 | |
| 636 | else if(ret & MBEDTLS_X509_BADCERT_REVOKED) |
| 637 | failf(data, "Cert verify failed: BADCERT_REVOKED" ); |
| 638 | |
| 639 | else if(ret & MBEDTLS_X509_BADCERT_CN_MISMATCH) |
| 640 | failf(data, "Cert verify failed: BADCERT_CN_MISMATCH" ); |
| 641 | |
| 642 | else if(ret & MBEDTLS_X509_BADCERT_NOT_TRUSTED) |
| 643 | failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED" ); |
| 644 | |
| 645 | else if(ret & MBEDTLS_X509_BADCERT_FUTURE) |
| 646 | failf(data, "Cert verify failed: BADCERT_FUTURE" ); |
| 647 | |
| 648 | return CURLE_PEER_FAILED_VERIFICATION; |
| 649 | } |
| 650 | |
| 651 | peercert = mbedtls_ssl_get_peer_cert(&backend->ssl); |
| 652 | |
| 653 | if(peercert && data->set.verbose) { |
| 654 | const size_t bufsize = 16384; |
| 655 | char *buffer = malloc(bufsize); |
| 656 | |
| 657 | if(!buffer) |
| 658 | return CURLE_OUT_OF_MEMORY; |
| 659 | |
| 660 | if(mbedtls_x509_crt_info(buffer, bufsize, "* " , peercert) > 0) |
| 661 | infof(data, "Dumping cert info: %s" , buffer); |
| 662 | else |
| 663 | infof(data, "Unable to dump certificate information" ); |
| 664 | |
| 665 | free(buffer); |
| 666 | } |
| 667 | |
| 668 | if(pinnedpubkey) { |
| 669 | int size; |
| 670 | CURLcode result; |
| 671 | mbedtls_x509_crt *p = NULL; |
| 672 | unsigned char *pubkey = NULL; |
| 673 | |
| 674 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 675 | if(!peercert || !peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(p) || |
| 676 | !peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(len)) { |
| 677 | #else |
| 678 | if(!peercert || !peercert->raw.p || !peercert->raw.len) { |
| 679 | #endif |
| 680 | failf(data, "Failed due to missing peer certificate" ); |
| 681 | return CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 682 | } |
| 683 | |
| 684 | p = calloc(1, sizeof(*p)); |
| 685 | |
| 686 | if(!p) |
| 687 | return CURLE_OUT_OF_MEMORY; |
| 688 | |
| 689 | pubkey = malloc(PUB_DER_MAX_BYTES); |
| 690 | |
| 691 | if(!pubkey) { |
| 692 | result = CURLE_OUT_OF_MEMORY; |
| 693 | goto pinnedpubkey_error; |
| 694 | } |
| 695 | |
| 696 | mbedtls_x509_crt_init(p); |
| 697 | |
| 698 | /* Make a copy of our const peercert because mbedtls_pk_write_pubkey_der |
| 699 | needs a non-const key, for now. |
| 700 | https://github.com/ARMmbed/mbedtls/issues/396 */ |
| 701 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 702 | if(mbedtls_x509_crt_parse_der(p, |
| 703 | peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(p), |
| 704 | peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(len))) { |
| 705 | #else |
| 706 | if(mbedtls_x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) { |
| 707 | #endif |
| 708 | failf(data, "Failed copying peer certificate" ); |
| 709 | result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 710 | goto pinnedpubkey_error; |
| 711 | } |
| 712 | |
| 713 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 714 | size = mbedtls_pk_write_pubkey_der(&p->MBEDTLS_PRIVATE(pk), pubkey, |
| 715 | PUB_DER_MAX_BYTES); |
| 716 | #else |
| 717 | size = mbedtls_pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES); |
| 718 | #endif |
| 719 | |
| 720 | if(size <= 0) { |
| 721 | failf(data, "Failed copying public key from peer certificate" ); |
| 722 | result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 723 | goto pinnedpubkey_error; |
| 724 | } |
| 725 | |
| 726 | /* mbedtls_pk_write_pubkey_der writes data at the end of the buffer. */ |
| 727 | result = Curl_pin_peer_pubkey(data, |
| 728 | pinnedpubkey, |
| 729 | &pubkey[PUB_DER_MAX_BYTES - size], size); |
| 730 | pinnedpubkey_error: |
| 731 | mbedtls_x509_crt_free(p); |
| 732 | free(p); |
| 733 | free(pubkey); |
| 734 | if(result) { |
| 735 | return result; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | #ifdef HAS_ALPN |
| 740 | if(conn->bits.tls_enable_alpn) { |
| 741 | const char *next_protocol = mbedtls_ssl_get_alpn_protocol(&backend->ssl); |
| 742 | |
| 743 | if(next_protocol) { |
| 744 | infof(data, "ALPN, server accepted to use %s" , next_protocol); |
| 745 | #ifdef USE_NGHTTP2 |
| 746 | if(!strncmp(next_protocol, NGHTTP2_PROTO_VERSION_ID, |
| 747 | NGHTTP2_PROTO_VERSION_ID_LEN) && |
| 748 | !next_protocol[NGHTTP2_PROTO_VERSION_ID_LEN]) { |
| 749 | conn->negnpn = CURL_HTTP_VERSION_2; |
| 750 | } |
| 751 | else |
| 752 | #endif |
| 753 | if(!strncmp(next_protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH) && |
| 754 | !next_protocol[ALPN_HTTP_1_1_LENGTH]) { |
| 755 | conn->negnpn = CURL_HTTP_VERSION_1_1; |
| 756 | } |
| 757 | } |
| 758 | else { |
| 759 | infof(data, "ALPN, server did not agree to a protocol" ); |
| 760 | } |
| 761 | Curl_multiuse_state(data, conn->negnpn == CURL_HTTP_VERSION_2 ? |
| 762 | BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE); |
| 763 | } |
| 764 | #endif |
| 765 | |
| 766 | connssl->connecting_state = ssl_connect_3; |
| 767 | infof(data, "SSL connected" ); |
| 768 | |
| 769 | return CURLE_OK; |
| 770 | } |
| 771 | |
| 772 | static CURLcode |
| 773 | mbed_connect_step3(struct Curl_easy *data, struct connectdata *conn, |
| 774 | int sockindex) |
| 775 | { |
| 776 | CURLcode retcode = CURLE_OK; |
| 777 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 778 | struct ssl_backend_data *backend = connssl->backend; |
| 779 | |
| 780 | DEBUGASSERT(ssl_connect_3 == connssl->connecting_state); |
| 781 | |
| 782 | if(SSL_SET_OPTION(primary.sessionid)) { |
| 783 | int ret; |
| 784 | mbedtls_ssl_session *our_ssl_sessionid; |
| 785 | void *old_ssl_sessionid = NULL; |
| 786 | bool isproxy = SSL_IS_PROXY() ? TRUE : FALSE; |
| 787 | |
| 788 | our_ssl_sessionid = malloc(sizeof(mbedtls_ssl_session)); |
| 789 | if(!our_ssl_sessionid) |
| 790 | return CURLE_OUT_OF_MEMORY; |
| 791 | |
| 792 | mbedtls_ssl_session_init(our_ssl_sessionid); |
| 793 | |
| 794 | ret = mbedtls_ssl_get_session(&backend->ssl, our_ssl_sessionid); |
| 795 | if(ret) { |
| 796 | if(ret != MBEDTLS_ERR_SSL_ALLOC_FAILED) |
| 797 | mbedtls_ssl_session_free(our_ssl_sessionid); |
| 798 | free(our_ssl_sessionid); |
| 799 | failf(data, "mbedtls_ssl_get_session returned -0x%x" , -ret); |
| 800 | return CURLE_SSL_CONNECT_ERROR; |
| 801 | } |
| 802 | |
| 803 | /* If there's already a matching session in the cache, delete it */ |
| 804 | Curl_ssl_sessionid_lock(data); |
| 805 | if(!Curl_ssl_getsessionid(data, conn, isproxy, &old_ssl_sessionid, NULL, |
| 806 | sockindex)) |
| 807 | Curl_ssl_delsessionid(data, old_ssl_sessionid); |
| 808 | |
| 809 | retcode = Curl_ssl_addsessionid(data, conn, isproxy, our_ssl_sessionid, |
| 810 | 0, sockindex); |
| 811 | Curl_ssl_sessionid_unlock(data); |
| 812 | if(retcode) { |
| 813 | mbedtls_ssl_session_free(our_ssl_sessionid); |
| 814 | free(our_ssl_sessionid); |
| 815 | failf(data, "failed to store ssl session" ); |
| 816 | return retcode; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | connssl->connecting_state = ssl_connect_done; |
| 821 | |
| 822 | return CURLE_OK; |
| 823 | } |
| 824 | |
| 825 | static ssize_t mbed_send(struct Curl_easy *data, int sockindex, |
| 826 | const void *mem, size_t len, |
| 827 | CURLcode *curlcode) |
| 828 | { |
| 829 | struct connectdata *conn = data->conn; |
| 830 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 831 | struct ssl_backend_data *backend = connssl->backend; |
| 832 | int ret = -1; |
| 833 | |
| 834 | ret = mbedtls_ssl_write(&backend->ssl, (unsigned char *)mem, len); |
| 835 | |
| 836 | if(ret < 0) { |
| 837 | *curlcode = (ret == MBEDTLS_ERR_SSL_WANT_WRITE) ? |
| 838 | CURLE_AGAIN : CURLE_SEND_ERROR; |
| 839 | ret = -1; |
| 840 | } |
| 841 | |
| 842 | return ret; |
| 843 | } |
| 844 | |
| 845 | static void mbedtls_close_all(struct Curl_easy *data) |
| 846 | { |
| 847 | (void)data; |
| 848 | } |
| 849 | |
| 850 | static void mbedtls_close(struct Curl_easy *data, |
| 851 | struct connectdata *conn, int sockindex) |
| 852 | { |
| 853 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 854 | struct ssl_backend_data *backend = connssl->backend; |
| 855 | char buf[32]; |
| 856 | (void) data; |
| 857 | |
| 858 | /* Maybe the server has already sent a close notify alert. |
| 859 | Read it to avoid an RST on the TCP connection. */ |
| 860 | (void)mbedtls_ssl_read(&backend->ssl, (unsigned char *)buf, sizeof(buf)); |
| 861 | |
| 862 | mbedtls_pk_free(&backend->pk); |
| 863 | mbedtls_x509_crt_free(&backend->clicert); |
| 864 | mbedtls_x509_crt_free(&backend->cacert); |
| 865 | mbedtls_x509_crl_free(&backend->crl); |
| 866 | mbedtls_ssl_config_free(&backend->config); |
| 867 | mbedtls_ssl_free(&backend->ssl); |
| 868 | mbedtls_ctr_drbg_free(&backend->ctr_drbg); |
| 869 | #ifndef THREADING_SUPPORT |
| 870 | mbedtls_entropy_free(&backend->entropy); |
| 871 | #endif /* THREADING_SUPPORT */ |
| 872 | } |
| 873 | |
| 874 | static ssize_t mbed_recv(struct Curl_easy *data, int num, |
| 875 | char *buf, size_t buffersize, |
| 876 | CURLcode *curlcode) |
| 877 | { |
| 878 | struct connectdata *conn = data->conn; |
| 879 | struct ssl_connect_data *connssl = &conn->ssl[num]; |
| 880 | struct ssl_backend_data *backend = connssl->backend; |
| 881 | int ret = -1; |
| 882 | ssize_t len = -1; |
| 883 | |
| 884 | ret = mbedtls_ssl_read(&backend->ssl, (unsigned char *)buf, |
| 885 | buffersize); |
| 886 | |
| 887 | if(ret <= 0) { |
| 888 | if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) |
| 889 | return 0; |
| 890 | |
| 891 | *curlcode = (ret == MBEDTLS_ERR_SSL_WANT_READ) ? |
| 892 | CURLE_AGAIN : CURLE_RECV_ERROR; |
| 893 | return -1; |
| 894 | } |
| 895 | |
| 896 | len = ret; |
| 897 | |
| 898 | return len; |
| 899 | } |
| 900 | |
| 901 | static void mbedtls_session_free(void *ptr) |
| 902 | { |
| 903 | mbedtls_ssl_session_free(ptr); |
| 904 | free(ptr); |
| 905 | } |
| 906 | |
| 907 | static size_t mbedtls_version(char *buffer, size_t size) |
| 908 | { |
| 909 | #ifdef MBEDTLS_VERSION_C |
| 910 | /* if mbedtls_version_get_number() is available it is better */ |
| 911 | unsigned int version = mbedtls_version_get_number(); |
| 912 | return msnprintf(buffer, size, "mbedTLS/%u.%u.%u" , version>>24, |
| 913 | (version>>16)&0xff, (version>>8)&0xff); |
| 914 | #else |
| 915 | return msnprintf(buffer, size, "mbedTLS/%s" , MBEDTLS_VERSION_STRING); |
| 916 | #endif |
| 917 | } |
| 918 | |
| 919 | static CURLcode mbedtls_random(struct Curl_easy *data, |
| 920 | unsigned char *entropy, size_t length) |
| 921 | { |
| 922 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 923 | int ret = -1; |
| 924 | char errorbuf[128]; |
| 925 | mbedtls_entropy_context ctr_entropy; |
| 926 | mbedtls_ctr_drbg_context ctr_drbg; |
| 927 | mbedtls_entropy_init(&ctr_entropy); |
| 928 | mbedtls_ctr_drbg_init(&ctr_drbg); |
| 929 | |
| 930 | ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, |
| 931 | &ctr_entropy, NULL, 0); |
| 932 | |
| 933 | if(ret) { |
| 934 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 935 | failf(data, "Failed - mbedTLS: ctr_drbg_seed returned (-0x%04X) %s" , |
| 936 | -ret, errorbuf); |
| 937 | } |
| 938 | else { |
| 939 | ret = mbedtls_ctr_drbg_random(&ctr_drbg, entropy, length); |
| 940 | |
| 941 | if(ret) { |
| 942 | mbedtls_strerror(ret, errorbuf, sizeof(errorbuf)); |
| 943 | failf(data, "mbedTLS: ctr_drbg_init returned (-0x%04X) %s" , |
| 944 | -ret, errorbuf); |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | mbedtls_ctr_drbg_free(&ctr_drbg); |
| 949 | mbedtls_entropy_free(&ctr_entropy); |
| 950 | |
| 951 | return ret == 0 ? CURLE_OK : CURLE_FAILED_INIT; |
| 952 | #elif defined(MBEDTLS_HAVEGE_C) |
| 953 | mbedtls_havege_state hs; |
| 954 | mbedtls_havege_init(&hs); |
| 955 | mbedtls_havege_random(&hs, entropy, length); |
| 956 | mbedtls_havege_free(&hs); |
| 957 | return CURLE_OK; |
| 958 | #else |
| 959 | return CURLE_NOT_BUILT_IN; |
| 960 | #endif |
| 961 | } |
| 962 | |
| 963 | static CURLcode |
| 964 | mbed_connect_common(struct Curl_easy *data, |
| 965 | struct connectdata *conn, |
| 966 | int sockindex, |
| 967 | bool nonblocking, |
| 968 | bool *done) |
| 969 | { |
| 970 | CURLcode retcode; |
| 971 | struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 972 | curl_socket_t sockfd = conn->sock[sockindex]; |
| 973 | timediff_t timeout_ms; |
| 974 | int what; |
| 975 | |
| 976 | /* check if the connection has already been established */ |
| 977 | if(ssl_connection_complete == connssl->state) { |
| 978 | *done = TRUE; |
| 979 | return CURLE_OK; |
| 980 | } |
| 981 | |
| 982 | if(ssl_connect_1 == connssl->connecting_state) { |
| 983 | /* Find out how much more time we're allowed */ |
| 984 | timeout_ms = Curl_timeleft(data, NULL, TRUE); |
| 985 | |
| 986 | if(timeout_ms < 0) { |
| 987 | /* no need to continue if time already is up */ |
| 988 | failf(data, "SSL connection timeout" ); |
| 989 | return CURLE_OPERATION_TIMEDOUT; |
| 990 | } |
| 991 | retcode = mbed_connect_step1(data, conn, sockindex); |
| 992 | if(retcode) |
| 993 | return retcode; |
| 994 | } |
| 995 | |
| 996 | while(ssl_connect_2 == connssl->connecting_state || |
| 997 | ssl_connect_2_reading == connssl->connecting_state || |
| 998 | ssl_connect_2_writing == connssl->connecting_state) { |
| 999 | |
| 1000 | /* check allowed time left */ |
| 1001 | timeout_ms = Curl_timeleft(data, NULL, TRUE); |
| 1002 | |
| 1003 | if(timeout_ms < 0) { |
| 1004 | /* no need to continue if time already is up */ |
| 1005 | failf(data, "SSL connection timeout" ); |
| 1006 | return CURLE_OPERATION_TIMEDOUT; |
| 1007 | } |
| 1008 | |
| 1009 | /* if ssl is expecting something, check if it's available. */ |
| 1010 | if(connssl->connecting_state == ssl_connect_2_reading |
| 1011 | || connssl->connecting_state == ssl_connect_2_writing) { |
| 1012 | |
| 1013 | curl_socket_t writefd = ssl_connect_2_writing == |
| 1014 | connssl->connecting_state?sockfd:CURL_SOCKET_BAD; |
| 1015 | curl_socket_t readfd = ssl_connect_2_reading == |
| 1016 | connssl->connecting_state?sockfd:CURL_SOCKET_BAD; |
| 1017 | |
| 1018 | what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd, |
| 1019 | nonblocking ? 0 : timeout_ms); |
| 1020 | if(what < 0) { |
| 1021 | /* fatal error */ |
| 1022 | failf(data, "select/poll on SSL socket, errno: %d" , SOCKERRNO); |
| 1023 | return CURLE_SSL_CONNECT_ERROR; |
| 1024 | } |
| 1025 | else if(0 == what) { |
| 1026 | if(nonblocking) { |
| 1027 | *done = FALSE; |
| 1028 | return CURLE_OK; |
| 1029 | } |
| 1030 | else { |
| 1031 | /* timeout */ |
| 1032 | failf(data, "SSL connection timeout" ); |
| 1033 | return CURLE_OPERATION_TIMEDOUT; |
| 1034 | } |
| 1035 | } |
| 1036 | /* socket is readable or writable */ |
| 1037 | } |
| 1038 | |
| 1039 | /* Run transaction, and return to the caller if it failed or if |
| 1040 | * this connection is part of a multi handle and this loop would |
| 1041 | * execute again. This permits the owner of a multi handle to |
| 1042 | * abort a connection attempt before step2 has completed while |
| 1043 | * ensuring that a client using select() or epoll() will always |
| 1044 | * have a valid fdset to wait on. |
| 1045 | */ |
| 1046 | retcode = mbed_connect_step2(data, conn, sockindex); |
| 1047 | if(retcode || (nonblocking && |
| 1048 | (ssl_connect_2 == connssl->connecting_state || |
| 1049 | ssl_connect_2_reading == connssl->connecting_state || |
| 1050 | ssl_connect_2_writing == connssl->connecting_state))) |
| 1051 | return retcode; |
| 1052 | |
| 1053 | } /* repeat step2 until all transactions are done. */ |
| 1054 | |
| 1055 | if(ssl_connect_3 == connssl->connecting_state) { |
| 1056 | retcode = mbed_connect_step3(data, conn, sockindex); |
| 1057 | if(retcode) |
| 1058 | return retcode; |
| 1059 | } |
| 1060 | |
| 1061 | if(ssl_connect_done == connssl->connecting_state) { |
| 1062 | connssl->state = ssl_connection_complete; |
| 1063 | conn->recv[sockindex] = mbed_recv; |
| 1064 | conn->send[sockindex] = mbed_send; |
| 1065 | *done = TRUE; |
| 1066 | } |
| 1067 | else |
| 1068 | *done = FALSE; |
| 1069 | |
| 1070 | /* Reset our connect state machine */ |
| 1071 | connssl->connecting_state = ssl_connect_1; |
| 1072 | |
| 1073 | return CURLE_OK; |
| 1074 | } |
| 1075 | |
| 1076 | static CURLcode mbedtls_connect_nonblocking(struct Curl_easy *data, |
| 1077 | struct connectdata *conn, |
| 1078 | int sockindex, bool *done) |
| 1079 | { |
| 1080 | return mbed_connect_common(data, conn, sockindex, TRUE, done); |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | static CURLcode mbedtls_connect(struct Curl_easy *data, |
| 1085 | struct connectdata *conn, int sockindex) |
| 1086 | { |
| 1087 | CURLcode retcode; |
| 1088 | bool done = FALSE; |
| 1089 | |
| 1090 | retcode = mbed_connect_common(data, conn, sockindex, FALSE, &done); |
| 1091 | if(retcode) |
| 1092 | return retcode; |
| 1093 | |
| 1094 | DEBUGASSERT(done); |
| 1095 | |
| 1096 | return CURLE_OK; |
| 1097 | } |
| 1098 | |
| 1099 | /* |
| 1100 | * return 0 error initializing SSL |
| 1101 | * return 1 SSL initialized successfully |
| 1102 | */ |
| 1103 | static int mbedtls_init(void) |
| 1104 | { |
| 1105 | return Curl_mbedtlsthreadlock_thread_setup(); |
| 1106 | } |
| 1107 | |
| 1108 | static void mbedtls_cleanup(void) |
| 1109 | { |
| 1110 | (void)Curl_mbedtlsthreadlock_thread_cleanup(); |
| 1111 | } |
| 1112 | |
| 1113 | static bool mbedtls_data_pending(const struct connectdata *conn, |
| 1114 | int sockindex) |
| 1115 | { |
| 1116 | const struct ssl_connect_data *connssl = &conn->ssl[sockindex]; |
| 1117 | struct ssl_backend_data *backend = connssl->backend; |
| 1118 | return mbedtls_ssl_get_bytes_avail(&backend->ssl) != 0; |
| 1119 | } |
| 1120 | |
| 1121 | static CURLcode mbedtls_sha256sum(const unsigned char *input, |
| 1122 | size_t inputlen, |
| 1123 | unsigned char *sha256sum, |
| 1124 | size_t sha256len UNUSED_PARAM) |
| 1125 | { |
| 1126 | /* TODO: explain this for different mbedtls 2.x vs 3 version */ |
| 1127 | (void)sha256len; |
| 1128 | #if MBEDTLS_VERSION_NUMBER < 0x02070000 |
| 1129 | mbedtls_sha256(input, inputlen, sha256sum, 0); |
| 1130 | #else |
| 1131 | /* returns 0 on success, otherwise failure */ |
| 1132 | #if MBEDTLS_VERSION_NUMBER >= 0x03000000 |
| 1133 | if(mbedtls_sha256(input, inputlen, sha256sum, 0) != 0) |
| 1134 | #else |
| 1135 | if(mbedtls_sha256_ret(input, inputlen, sha256sum, 0) != 0) |
| 1136 | #endif |
| 1137 | return CURLE_BAD_FUNCTION_ARGUMENT; |
| 1138 | #endif |
| 1139 | return CURLE_OK; |
| 1140 | } |
| 1141 | |
| 1142 | static void *mbedtls_get_internals(struct ssl_connect_data *connssl, |
| 1143 | CURLINFO info UNUSED_PARAM) |
| 1144 | { |
| 1145 | struct ssl_backend_data *backend = connssl->backend; |
| 1146 | (void)info; |
| 1147 | return &backend->ssl; |
| 1148 | } |
| 1149 | |
| 1150 | const struct Curl_ssl Curl_ssl_mbedtls = { |
| 1151 | { CURLSSLBACKEND_MBEDTLS, "mbedtls" }, /* info */ |
| 1152 | |
| 1153 | SSLSUPP_CA_PATH | |
| 1154 | SSLSUPP_PINNEDPUBKEY | |
| 1155 | SSLSUPP_SSL_CTX, |
| 1156 | |
| 1157 | sizeof(struct ssl_backend_data), |
| 1158 | |
| 1159 | mbedtls_init, /* init */ |
| 1160 | mbedtls_cleanup, /* cleanup */ |
| 1161 | mbedtls_version, /* version */ |
| 1162 | Curl_none_check_cxn, /* check_cxn */ |
| 1163 | Curl_none_shutdown, /* shutdown */ |
| 1164 | mbedtls_data_pending, /* data_pending */ |
| 1165 | mbedtls_random, /* random */ |
| 1166 | Curl_none_cert_status_request, /* cert_status_request */ |
| 1167 | mbedtls_connect, /* connect */ |
| 1168 | mbedtls_connect_nonblocking, /* connect_nonblocking */ |
| 1169 | Curl_ssl_getsock, /* getsock */ |
| 1170 | mbedtls_get_internals, /* get_internals */ |
| 1171 | mbedtls_close, /* close_one */ |
| 1172 | mbedtls_close_all, /* close_all */ |
| 1173 | mbedtls_session_free, /* session_free */ |
| 1174 | Curl_none_set_engine, /* set_engine */ |
| 1175 | Curl_none_set_engine_default, /* set_engine_default */ |
| 1176 | Curl_none_engines_list, /* engines_list */ |
| 1177 | Curl_none_false_start, /* false_start */ |
| 1178 | mbedtls_sha256sum, /* sha256sum */ |
| 1179 | NULL, /* associate_connection */ |
| 1180 | NULL /* disassociate_connection */ |
| 1181 | }; |
| 1182 | |
| 1183 | #endif /* USE_MBEDTLS */ |
| 1184 | |