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