Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/curl/lib/vtls/openssl.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) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al. | |
| 9 * | |
| 10 * This software is licensed as described in the file COPYING, which | |
| 11 * you should have received as part of this distribution. The terms | |
| 12 * are also available at https://curl.haxx.se/docs/copyright.html. | |
| 13 * | |
| 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell | |
| 15 * copies of the Software, and permit persons to whom the Software is | |
| 16 * furnished to do so, under the terms of the COPYING file. | |
| 17 * | |
| 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | |
| 19 * KIND, either express or implied. | |
| 20 * | |
| 21 ***************************************************************************/ | |
| 22 | |
| 23 /* | |
| 24 * Source file for all OpenSSL-specific code for the TLS/SSL layer. No code | |
| 25 * but vtls.c should ever call or use these functions. | |
| 26 */ | |
| 27 | |
| 28 #include "curl_setup.h" | |
| 29 | |
| 30 #ifdef USE_OPENSSL | |
| 31 | |
| 32 #include <limits.h> | |
| 33 | |
| 34 #include "urldata.h" | |
| 35 #include "sendf.h" | |
| 36 #include "formdata.h" /* for the boundary function */ | |
| 37 #include "url.h" /* for the ssl config check function */ | |
| 38 #include "inet_pton.h" | |
| 39 #include "openssl.h" | |
| 40 #include "connect.h" | |
| 41 #include "slist.h" | |
| 42 #include "select.h" | |
| 43 #include "vtls.h" | |
| 44 #include "strcase.h" | |
| 45 #include "hostcheck.h" | |
| 46 #include "multiif.h" | |
| 47 #include "curl_printf.h" | |
| 48 #include <openssl/ssl.h> | |
| 49 #include <openssl/rand.h> | |
| 50 #include <openssl/x509v3.h> | |
| 51 #ifndef OPENSSL_NO_DSA | |
| 52 #include <openssl/dsa.h> | |
| 53 #endif | |
| 54 #include <openssl/dh.h> | |
| 55 #include <openssl/err.h> | |
| 56 #include <openssl/md5.h> | |
| 57 #include <openssl/conf.h> | |
| 58 #include <openssl/bn.h> | |
| 59 #include <openssl/rsa.h> | |
| 60 #include <openssl/bio.h> | |
| 61 #include <openssl/buffer.h> | |
| 62 #include <openssl/pkcs12.h> | |
| 63 | |
| 64 #ifdef USE_AMISSL | |
| 65 #include "amigaos.h" | |
| 66 #endif | |
| 67 | |
| 68 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_OCSP) | |
| 69 #include <openssl/ocsp.h> | |
| 70 #endif | |
| 71 | |
| 72 #if (OPENSSL_VERSION_NUMBER >= 0x0090700fL) && /* 0.9.7 or later */ \ | |
| 73 !defined(OPENSSL_NO_ENGINE) && !defined(OPENSSL_NO_UI_CONSOLE) | |
| 74 #define USE_OPENSSL_ENGINE | |
| 75 #include <openssl/engine.h> | |
| 76 #endif | |
| 77 | |
| 78 #include "warnless.h" | |
| 79 #include "non-ascii.h" /* for Curl_convert_from_utf8 prototype */ | |
| 80 | |
| 81 /* The last #include files should be: */ | |
| 82 #include "curl_memory.h" | |
| 83 #include "memdebug.h" | |
| 84 | |
| 85 /* Uncomment the ALLOW_RENEG line to a real #define if you want to allow TLS | |
| 86 renegotiations when built with BoringSSL. Renegotiating is non-compliant | |
| 87 with HTTP/2 and "an extremely dangerous protocol feature". Beware. | |
| 88 | |
| 89 #define ALLOW_RENEG 1 | |
| 90 */ | |
| 91 | |
| 92 #ifndef OPENSSL_VERSION_NUMBER | |
| 93 #error "OPENSSL_VERSION_NUMBER not defined" | |
| 94 #endif | |
| 95 | |
| 96 #ifdef USE_OPENSSL_ENGINE | |
| 97 #include <openssl/ui.h> | |
| 98 #endif | |
| 99 | |
| 100 #if OPENSSL_VERSION_NUMBER >= 0x00909000L | |
| 101 #define SSL_METHOD_QUAL const | |
| 102 #else | |
| 103 #define SSL_METHOD_QUAL | |
| 104 #endif | |
| 105 | |
| 106 #if (OPENSSL_VERSION_NUMBER >= 0x10000000L) | |
| 107 #define HAVE_ERR_REMOVE_THREAD_STATE 1 | |
| 108 #endif | |
| 109 | |
| 110 #if !defined(HAVE_SSLV2_CLIENT_METHOD) || \ | |
| 111 OPENSSL_VERSION_NUMBER >= 0x10100000L /* 1.1.0+ has no SSLv2 */ | |
| 112 #undef OPENSSL_NO_SSL2 /* undef first to avoid compiler warnings */ | |
| 113 #define OPENSSL_NO_SSL2 | |
| 114 #endif | |
| 115 | |
| 116 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && /* OpenSSL 1.1.0+ */ \ | |
| 117 !(defined(LIBRESSL_VERSION_NUMBER) && \ | |
| 118 LIBRESSL_VERSION_NUMBER < 0x20700000L) | |
| 119 #define SSLEAY_VERSION_NUMBER OPENSSL_VERSION_NUMBER | |
| 120 #define HAVE_X509_GET0_EXTENSIONS 1 /* added in 1.1.0 -pre1 */ | |
| 121 #define HAVE_OPAQUE_EVP_PKEY 1 /* since 1.1.0 -pre3 */ | |
| 122 #define HAVE_OPAQUE_RSA_DSA_DH 1 /* since 1.1.0 -pre5 */ | |
| 123 #define CONST_EXTS const | |
| 124 #define HAVE_ERR_REMOVE_THREAD_STATE_DEPRECATED 1 | |
| 125 | |
| 126 /* funny typecast define due to difference in API */ | |
| 127 #ifdef LIBRESSL_VERSION_NUMBER | |
| 128 #define ARG2_X509_signature_print (X509_ALGOR *) | |
| 129 #else | |
| 130 #define ARG2_X509_signature_print | |
| 131 #endif | |
| 132 | |
| 133 #else | |
| 134 /* For OpenSSL before 1.1.0 */ | |
| 135 #define ASN1_STRING_get0_data(x) ASN1_STRING_data(x) | |
| 136 #define X509_get0_notBefore(x) X509_get_notBefore(x) | |
| 137 #define X509_get0_notAfter(x) X509_get_notAfter(x) | |
| 138 #define CONST_EXTS /* nope */ | |
| 139 #ifndef LIBRESSL_VERSION_NUMBER | |
| 140 #define OpenSSL_version_num() SSLeay() | |
| 141 #endif | |
| 142 #endif | |
| 143 | |
| 144 #ifdef LIBRESSL_VERSION_NUMBER | |
| 145 #define OpenSSL_version_num() LIBRESSL_VERSION_NUMBER | |
| 146 #endif | |
| 147 | |
| 148 #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) && /* 1.0.2 or later */ \ | |
| 149 !(defined(LIBRESSL_VERSION_NUMBER) && \ | |
| 150 LIBRESSL_VERSION_NUMBER < 0x20700000L) | |
| 151 #define HAVE_X509_GET0_SIGNATURE 1 | |
| 152 #endif | |
| 153 | |
| 154 #if (OPENSSL_VERSION_NUMBER >= 0x1000200fL) /* 1.0.2 or later */ | |
| 155 #define HAVE_SSL_GET_SHUTDOWN 1 | |
| 156 #endif | |
| 157 | |
| 158 #if OPENSSL_VERSION_NUMBER >= 0x10002003L && \ | |
| 159 OPENSSL_VERSION_NUMBER <= 0x10002FFFL && \ | |
| 160 !defined(OPENSSL_NO_COMP) | |
| 161 #define HAVE_SSL_COMP_FREE_COMPRESSION_METHODS 1 | |
| 162 #endif | |
| 163 | |
| 164 #if (OPENSSL_VERSION_NUMBER < 0x0090808fL) | |
| 165 /* not present in older OpenSSL */ | |
| 166 #define OPENSSL_load_builtin_modules(x) | |
| 167 #endif | |
| 168 | |
| 169 /* | |
| 170 * Whether SSL_CTX_set_keylog_callback is available. | |
| 171 * OpenSSL: supported since 1.1.1 https://github.com/openssl/openssl/pull/2287 | |
| 172 * BoringSSL: supported since d28f59c27bac (committed 2015-11-19) | |
| 173 * LibreSSL: unsupported in at least 2.7.2 (explicitly check for it since it | |
| 174 * lies and pretends to be OpenSSL 2.0.0). | |
| 175 */ | |
| 176 #if (OPENSSL_VERSION_NUMBER >= 0x10101000L && \ | |
| 177 !defined(LIBRESSL_VERSION_NUMBER)) || \ | |
| 178 defined(OPENSSL_IS_BORINGSSL) | |
| 179 #define HAVE_KEYLOG_CALLBACK | |
| 180 #endif | |
| 181 | |
| 182 /* Whether SSL_CTX_set_ciphersuites is available. | |
| 183 * OpenSSL: supported since 1.1.1 (commit a53b5be6a05) | |
| 184 * BoringSSL: no | |
| 185 * LibreSSL: no | |
| 186 */ | |
| 187 #if ((OPENSSL_VERSION_NUMBER >= 0x10101000L) && \ | |
| 188 !defined(LIBRESSL_VERSION_NUMBER) && \ | |
| 189 !defined(OPENSSL_IS_BORINGSSL)) | |
| 190 #define HAVE_SSL_CTX_SET_CIPHERSUITES | |
| 191 #define HAVE_SSL_CTX_SET_POST_HANDSHAKE_AUTH | |
| 192 #endif | |
| 193 | |
| 194 #if defined(LIBRESSL_VERSION_NUMBER) | |
| 195 #define OSSL_PACKAGE "LibreSSL" | |
| 196 #elif defined(OPENSSL_IS_BORINGSSL) | |
| 197 #define OSSL_PACKAGE "BoringSSL" | |
| 198 #else | |
| 199 #define OSSL_PACKAGE "OpenSSL" | |
| 200 #endif | |
| 201 | |
| 202 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) | |
| 203 /* up2date versions of OpenSSL maintain the default reasonably secure without | |
| 204 * breaking compatibility, so it is better not to override the default by curl | |
| 205 */ | |
| 206 #define DEFAULT_CIPHER_SELECTION NULL | |
| 207 #else | |
| 208 /* ... but it is not the case with old versions of OpenSSL */ | |
| 209 #define DEFAULT_CIPHER_SELECTION \ | |
| 210 "ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH" | |
| 211 #endif | |
| 212 | |
| 213 #define ENABLE_SSLKEYLOGFILE | |
| 214 | |
| 215 #ifdef ENABLE_SSLKEYLOGFILE | |
| 216 typedef struct ssl_tap_state { | |
| 217 int master_key_length; | |
| 218 unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH]; | |
| 219 unsigned char client_random[SSL3_RANDOM_SIZE]; | |
| 220 } ssl_tap_state_t; | |
| 221 #endif /* ENABLE_SSLKEYLOGFILE */ | |
| 222 | |
| 223 struct ssl_backend_data { | |
| 224 /* these ones requires specific SSL-types */ | |
| 225 SSL_CTX* ctx; | |
| 226 SSL* handle; | |
| 227 X509* server_cert; | |
| 228 #ifdef ENABLE_SSLKEYLOGFILE | |
| 229 /* tap_state holds the last seen master key if we're logging them */ | |
| 230 ssl_tap_state_t tap_state; | |
| 231 #endif | |
| 232 }; | |
| 233 | |
| 234 #define BACKEND connssl->backend | |
| 235 | |
| 236 /* | |
| 237 * Number of bytes to read from the random number seed file. This must be | |
| 238 * a finite value (because some entropy "files" like /dev/urandom have | |
| 239 * an infinite length), but must be large enough to provide enough | |
| 240 * entropy to properly seed OpenSSL's PRNG. | |
| 241 */ | |
| 242 #define RAND_LOAD_LENGTH 1024 | |
| 243 | |
| 244 #ifdef ENABLE_SSLKEYLOGFILE | |
| 245 /* The fp for the open SSLKEYLOGFILE, or NULL if not open */ | |
| 246 static FILE *keylog_file_fp; | |
| 247 | |
| 248 #ifdef HAVE_KEYLOG_CALLBACK | |
| 249 static void ossl_keylog_callback(const SSL *ssl, const char *line) | |
| 250 { | |
| 251 (void)ssl; | |
| 252 | |
| 253 /* Using fputs here instead of fprintf since libcurl's fprintf replacement | |
| 254 may not be thread-safe. */ | |
| 255 if(keylog_file_fp && line && *line) { | |
| 256 char stackbuf[256]; | |
| 257 char *buf; | |
| 258 size_t linelen = strlen(line); | |
| 259 | |
| 260 if(linelen <= sizeof(stackbuf) - 2) | |
| 261 buf = stackbuf; | |
| 262 else { | |
| 263 buf = malloc(linelen + 2); | |
| 264 if(!buf) | |
| 265 return; | |
| 266 } | |
| 267 memcpy(buf, line, linelen); | |
| 268 buf[linelen] = '\n'; | |
| 269 buf[linelen + 1] = '\0'; | |
| 270 | |
| 271 fputs(buf, keylog_file_fp); | |
| 272 if(buf != stackbuf) | |
| 273 free(buf); | |
| 274 } | |
| 275 } | |
| 276 #else | |
| 277 #define KEYLOG_PREFIX "CLIENT_RANDOM " | |
| 278 #define KEYLOG_PREFIX_LEN (sizeof(KEYLOG_PREFIX) - 1) | |
| 279 /* | |
| 280 * tap_ssl_key is called by libcurl to make the CLIENT_RANDOMs if the OpenSSL | |
| 281 * being used doesn't have native support for doing that. | |
| 282 */ | |
| 283 static void tap_ssl_key(const SSL *ssl, ssl_tap_state_t *state) | |
| 284 { | |
| 285 const char *hex = "0123456789ABCDEF"; | |
| 286 int pos, i; | |
| 287 char line[KEYLOG_PREFIX_LEN + 2 * SSL3_RANDOM_SIZE + 1 + | |
| 288 2 * SSL_MAX_MASTER_KEY_LENGTH + 1 + 1]; | |
| 289 const SSL_SESSION *session = SSL_get_session(ssl); | |
| 290 unsigned char client_random[SSL3_RANDOM_SIZE]; | |
| 291 unsigned char master_key[SSL_MAX_MASTER_KEY_LENGTH]; | |
| 292 int master_key_length = 0; | |
| 293 | |
| 294 if(!session || !keylog_file_fp) | |
| 295 return; | |
| 296 | |
| 297 #if OPENSSL_VERSION_NUMBER >= 0x10100000L && \ | |
| 298 !(defined(LIBRESSL_VERSION_NUMBER) && \ | |
| 299 LIBRESSL_VERSION_NUMBER < 0x20700000L) | |
| 300 /* ssl->s3 is not checked in openssl 1.1.0-pre6, but let's assume that | |
| 301 * we have a valid SSL context if we have a non-NULL session. */ | |
| 302 SSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE); | |
| 303 master_key_length = (int) | |
| 304 SSL_SESSION_get_master_key(session, master_key, SSL_MAX_MASTER_KEY_LENGTH); | |
| 305 #else | |
| 306 if(ssl->s3 && session->master_key_length > 0) { | |
| 307 master_key_length = session->master_key_length; | |
| 308 memcpy(master_key, session->master_key, session->master_key_length); | |
| 309 memcpy(client_random, ssl->s3->client_random, SSL3_RANDOM_SIZE); | |
| 310 } | |
| 311 #endif | |
| 312 | |
| 313 if(master_key_length <= 0) | |
| 314 return; | |
| 315 | |
| 316 /* Skip writing keys if there is no key or it did not change. */ | |
| 317 if(state->master_key_length == master_key_length && | |
| 318 !memcmp(state->master_key, master_key, master_key_length) && | |
| 319 !memcmp(state->client_random, client_random, SSL3_RANDOM_SIZE)) { | |
| 320 return; | |
| 321 } | |
| 322 | |
| 323 state->master_key_length = master_key_length; | |
| 324 memcpy(state->master_key, master_key, master_key_length); | |
| 325 memcpy(state->client_random, client_random, SSL3_RANDOM_SIZE); | |
| 326 | |
| 327 memcpy(line, KEYLOG_PREFIX, KEYLOG_PREFIX_LEN); | |
| 328 pos = KEYLOG_PREFIX_LEN; | |
| 329 | |
| 330 /* Client Random for SSLv3/TLS */ | |
| 331 for(i = 0; i < SSL3_RANDOM_SIZE; i++) { | |
| 332 line[pos++] = hex[client_random[i] >> 4]; | |
| 333 line[pos++] = hex[client_random[i] & 0xF]; | |
| 334 } | |
| 335 line[pos++] = ' '; | |
| 336 | |
| 337 /* Master Secret (size is at most SSL_MAX_MASTER_KEY_LENGTH) */ | |
| 338 for(i = 0; i < master_key_length; i++) { | |
| 339 line[pos++] = hex[master_key[i] >> 4]; | |
| 340 line[pos++] = hex[master_key[i] & 0xF]; | |
| 341 } | |
| 342 line[pos++] = '\n'; | |
| 343 line[pos] = '\0'; | |
| 344 | |
| 345 /* Using fputs here instead of fprintf since libcurl's fprintf replacement | |
| 346 may not be thread-safe. */ | |
| 347 fputs(line, keylog_file_fp); | |
| 348 } | |
| 349 #endif /* !HAVE_KEYLOG_CALLBACK */ | |
| 350 #endif /* ENABLE_SSLKEYLOGFILE */ | |
| 351 | |
| 352 static const char *SSL_ERROR_to_str(int err) | |
| 353 { | |
| 354 switch(err) { | |
| 355 case SSL_ERROR_NONE: | |
| 356 return "SSL_ERROR_NONE"; | |
| 357 case SSL_ERROR_SSL: | |
| 358 return "SSL_ERROR_SSL"; | |
| 359 case SSL_ERROR_WANT_READ: | |
| 360 return "SSL_ERROR_WANT_READ"; | |
| 361 case SSL_ERROR_WANT_WRITE: | |
| 362 return "SSL_ERROR_WANT_WRITE"; | |
| 363 case SSL_ERROR_WANT_X509_LOOKUP: | |
| 364 return "SSL_ERROR_WANT_X509_LOOKUP"; | |
| 365 case SSL_ERROR_SYSCALL: | |
| 366 return "SSL_ERROR_SYSCALL"; | |
| 367 case SSL_ERROR_ZERO_RETURN: | |
| 368 return "SSL_ERROR_ZERO_RETURN"; | |
| 369 case SSL_ERROR_WANT_CONNECT: | |
| 370 return "SSL_ERROR_WANT_CONNECT"; | |
| 371 case SSL_ERROR_WANT_ACCEPT: | |
| 372 return "SSL_ERROR_WANT_ACCEPT"; | |
| 373 #if defined(SSL_ERROR_WANT_ASYNC) | |
| 374 case SSL_ERROR_WANT_ASYNC: | |
| 375 return "SSL_ERROR_WANT_ASYNC"; | |
| 376 #endif | |
| 377 #if defined(SSL_ERROR_WANT_ASYNC_JOB) | |
| 378 case SSL_ERROR_WANT_ASYNC_JOB: | |
| 379 return "SSL_ERROR_WANT_ASYNC_JOB"; | |
| 380 #endif | |
| 381 #if defined(SSL_ERROR_WANT_EARLY) | |
| 382 case SSL_ERROR_WANT_EARLY: | |
| 383 return "SSL_ERROR_WANT_EARLY"; | |
| 384 #endif | |
| 385 default: | |
| 386 return "SSL_ERROR unknown"; | |
| 387 } | |
| 388 } | |
| 389 | |
| 390 /* Return error string for last OpenSSL error | |
| 391 */ | |
| 392 static char *ossl_strerror(unsigned long error, char *buf, size_t size) | |
| 393 { | |
| 394 #ifdef OPENSSL_IS_BORINGSSL | |
| 395 ERR_error_string_n((uint32_t)error, buf, size); | |
| 396 #else | |
| 397 ERR_error_string_n(error, buf, size); | |
| 398 #endif | |
| 399 return buf; | |
| 400 } | |
| 401 | |
| 402 /* Return an extra data index for the connection data. | |
| 403 * This index can be used with SSL_get_ex_data() and SSL_set_ex_data(). | |
| 404 */ | |
| 405 static int ossl_get_ssl_conn_index(void) | |
| 406 { | |
| 407 static int ssl_ex_data_conn_index = -1; | |
| 408 if(ssl_ex_data_conn_index < 0) { | |
| 409 ssl_ex_data_conn_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); | |
| 410 } | |
| 411 return ssl_ex_data_conn_index; | |
| 412 } | |
| 413 | |
| 414 /* Return an extra data index for the sockindex. | |
| 415 * This index can be used with SSL_get_ex_data() and SSL_set_ex_data(). | |
| 416 */ | |
| 417 static int ossl_get_ssl_sockindex_index(void) | |
| 418 { | |
| 419 static int ssl_ex_data_sockindex_index = -1; | |
| 420 if(ssl_ex_data_sockindex_index < 0) { | |
| 421 ssl_ex_data_sockindex_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, | |
| 422 NULL); | |
| 423 } | |
| 424 return ssl_ex_data_sockindex_index; | |
| 425 } | |
| 426 | |
| 427 static int passwd_callback(char *buf, int num, int encrypting, | |
| 428 void *global_passwd) | |
| 429 { | |
| 430 DEBUGASSERT(0 == encrypting); | |
| 431 | |
| 432 if(!encrypting) { | |
| 433 int klen = curlx_uztosi(strlen((char *)global_passwd)); | |
| 434 if(num > klen) { | |
| 435 memcpy(buf, global_passwd, klen + 1); | |
| 436 return klen; | |
| 437 } | |
| 438 } | |
| 439 return 0; | |
| 440 } | |
| 441 | |
| 442 /* | |
| 443 * rand_enough() returns TRUE if we have seeded the random engine properly. | |
| 444 */ | |
| 445 static bool rand_enough(void) | |
| 446 { | |
| 447 return (0 != RAND_status()) ? TRUE : FALSE; | |
| 448 } | |
| 449 | |
| 450 static CURLcode Curl_ossl_seed(struct Curl_easy *data) | |
| 451 { | |
| 452 /* we have the "SSL is seeded" boolean static to prevent multiple | |
| 453 time-consuming seedings in vain */ | |
| 454 static bool ssl_seeded = FALSE; | |
| 455 char fname[256]; | |
| 456 | |
| 457 if(ssl_seeded) | |
| 458 return CURLE_OK; | |
| 459 | |
| 460 if(rand_enough()) { | |
| 461 /* OpenSSL 1.1.0+ will return here */ | |
| 462 ssl_seeded = TRUE; | |
| 463 return CURLE_OK; | |
| 464 } | |
| 465 | |
| 466 #ifndef RANDOM_FILE | |
| 467 /* if RANDOM_FILE isn't defined, we only perform this if an option tells | |
| 468 us to! */ | |
| 469 if(data->set.str[STRING_SSL_RANDOM_FILE]) | |
| 470 #define RANDOM_FILE "" /* doesn't matter won't be used */ | |
| 471 #endif | |
| 472 { | |
| 473 /* let the option override the define */ | |
| 474 RAND_load_file((data->set.str[STRING_SSL_RANDOM_FILE]? | |
| 475 data->set.str[STRING_SSL_RANDOM_FILE]: | |
| 476 RANDOM_FILE), | |
| 477 RAND_LOAD_LENGTH); | |
| 478 if(rand_enough()) | |
| 479 return CURLE_OK; | |
| 480 } | |
| 481 | |
| 482 #if defined(HAVE_RAND_EGD) | |
| 483 /* only available in OpenSSL 0.9.5 and later */ | |
| 484 /* EGD_SOCKET is set at configure time or not at all */ | |
| 485 #ifndef EGD_SOCKET | |
| 486 /* If we don't have the define set, we only do this if the egd-option | |
| 487 is set */ | |
| 488 if(data->set.str[STRING_SSL_EGDSOCKET]) | |
| 489 #define EGD_SOCKET "" /* doesn't matter won't be used */ | |
| 490 #endif | |
| 491 { | |
| 492 /* If there's an option and a define, the option overrides the | |
| 493 define */ | |
| 494 int ret = RAND_egd(data->set.str[STRING_SSL_EGDSOCKET]? | |
| 495 data->set.str[STRING_SSL_EGDSOCKET]:EGD_SOCKET); | |
| 496 if(-1 != ret) { | |
| 497 if(rand_enough()) | |
| 498 return CURLE_OK; | |
| 499 } | |
| 500 } | |
| 501 #endif | |
| 502 | |
| 503 /* fallback to a custom seeding of the PRNG using a hash based on a current | |
| 504 time */ | |
| 505 do { | |
| 506 unsigned char randb[64]; | |
| 507 size_t len = sizeof(randb); | |
| 508 size_t i, i_max; | |
| 509 for(i = 0, i_max = len / sizeof(struct curltime); i < i_max; ++i) { | |
| 510 struct curltime tv = Curl_now(); | |
| 511 Curl_wait_ms(1); | |
| 512 tv.tv_sec *= i + 1; | |
| 513 tv.tv_usec *= (unsigned int)i + 2; | |
| 514 tv.tv_sec ^= ((Curl_now().tv_sec + Curl_now().tv_usec) * | |
| 515 (i + 3)) << 8; | |
| 516 tv.tv_usec ^= (unsigned int) ((Curl_now().tv_sec + | |
| 517 Curl_now().tv_usec) * | |
| 518 (i + 4)) << 16; | |
| 519 memcpy(&randb[i * sizeof(struct curltime)], &tv, | |
| 520 sizeof(struct curltime)); | |
| 521 } | |
| 522 RAND_add(randb, (int)len, (double)len/2); | |
| 523 } while(!rand_enough()); | |
| 524 | |
| 525 /* generates a default path for the random seed file */ | |
| 526 fname[0] = 0; /* blank it first */ | |
| 527 RAND_file_name(fname, sizeof(fname)); | |
| 528 if(fname[0]) { | |
| 529 /* we got a file name to try */ | |
| 530 RAND_load_file(fname, RAND_LOAD_LENGTH); | |
| 531 if(rand_enough()) | |
| 532 return CURLE_OK; | |
| 533 } | |
| 534 | |
| 535 infof(data, "libcurl is now using a weak random seed!\n"); | |
| 536 return (rand_enough() ? CURLE_OK : | |
| 537 CURLE_SSL_CONNECT_ERROR /* confusing error code */); | |
| 538 } | |
| 539 | |
| 540 #ifndef SSL_FILETYPE_ENGINE | |
| 541 #define SSL_FILETYPE_ENGINE 42 | |
| 542 #endif | |
| 543 #ifndef SSL_FILETYPE_PKCS12 | |
| 544 #define SSL_FILETYPE_PKCS12 43 | |
| 545 #endif | |
| 546 static int do_file_type(const char *type) | |
| 547 { | |
| 548 if(!type || !type[0]) | |
| 549 return SSL_FILETYPE_PEM; | |
| 550 if(strcasecompare(type, "PEM")) | |
| 551 return SSL_FILETYPE_PEM; | |
| 552 if(strcasecompare(type, "DER")) | |
| 553 return SSL_FILETYPE_ASN1; | |
| 554 if(strcasecompare(type, "ENG")) | |
| 555 return SSL_FILETYPE_ENGINE; | |
| 556 if(strcasecompare(type, "P12")) | |
| 557 return SSL_FILETYPE_PKCS12; | |
| 558 return -1; | |
| 559 } | |
| 560 | |
| 561 #ifdef USE_OPENSSL_ENGINE | |
| 562 /* | |
| 563 * Supply default password to the engine user interface conversation. | |
| 564 * The password is passed by OpenSSL engine from ENGINE_load_private_key() | |
| 565 * last argument to the ui and can be obtained by UI_get0_user_data(ui) here. | |
| 566 */ | |
| 567 static int ssl_ui_reader(UI *ui, UI_STRING *uis) | |
| 568 { | |
| 569 const char *password; | |
| 570 switch(UI_get_string_type(uis)) { | |
| 571 case UIT_PROMPT: | |
| 572 case UIT_VERIFY: | |
| 573 password = (const char *)UI_get0_user_data(ui); | |
| 574 if(password && (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD)) { | |
| 575 UI_set_result(ui, uis, password); | |
| 576 return 1; | |
| 577 } | |
| 578 default: | |
| 579 break; | |
| 580 } | |
| 581 return (UI_method_get_reader(UI_OpenSSL()))(ui, uis); | |
| 582 } | |
| 583 | |
| 584 /* | |
| 585 * Suppress interactive request for a default password if available. | |
| 586 */ | |
| 587 static int ssl_ui_writer(UI *ui, UI_STRING *uis) | |
| 588 { | |
| 589 switch(UI_get_string_type(uis)) { | |
| 590 case UIT_PROMPT: | |
| 591 case UIT_VERIFY: | |
| 592 if(UI_get0_user_data(ui) && | |
| 593 (UI_get_input_flags(uis) & UI_INPUT_FLAG_DEFAULT_PWD)) { | |
| 594 return 1; | |
| 595 } | |
| 596 default: | |
| 597 break; | |
| 598 } | |
| 599 return (UI_method_get_writer(UI_OpenSSL()))(ui, uis); | |
| 600 } | |
| 601 | |
| 602 /* | |
| 603 * Check if a given string is a PKCS#11 URI | |
| 604 */ | |
| 605 static bool is_pkcs11_uri(const char *string) | |
| 606 { | |
| 607 return (string && strncasecompare(string, "pkcs11:", 7)); | |
| 608 } | |
| 609 | |
| 610 #endif | |
| 611 | |
| 612 static CURLcode Curl_ossl_set_engine(struct Curl_easy *data, | |
| 613 const char *engine); | |
| 614 | |
| 615 static | |
| 616 int cert_stuff(struct connectdata *conn, | |
| 617 SSL_CTX* ctx, | |
| 618 char *cert_file, | |
| 619 const char *cert_type, | |
| 620 char *key_file, | |
| 621 const char *key_type, | |
| 622 char *key_passwd) | |
| 623 { | |
| 624 struct Curl_easy *data = conn->data; | |
| 625 char error_buffer[256]; | |
| 626 bool check_privkey = TRUE; | |
| 627 | |
| 628 int file_type = do_file_type(cert_type); | |
| 629 | |
| 630 if(cert_file || (file_type == SSL_FILETYPE_ENGINE)) { | |
| 631 SSL *ssl; | |
| 632 X509 *x509; | |
| 633 int cert_done = 0; | |
| 634 | |
| 635 if(key_passwd) { | |
| 636 /* set the password in the callback userdata */ | |
| 637 SSL_CTX_set_default_passwd_cb_userdata(ctx, key_passwd); | |
| 638 /* Set passwd callback: */ | |
| 639 SSL_CTX_set_default_passwd_cb(ctx, passwd_callback); | |
| 640 } | |
| 641 | |
| 642 | |
| 643 switch(file_type) { | |
| 644 case SSL_FILETYPE_PEM: | |
| 645 /* SSL_CTX_use_certificate_chain_file() only works on PEM files */ | |
| 646 if(SSL_CTX_use_certificate_chain_file(ctx, | |
| 647 cert_file) != 1) { | |
| 648 failf(data, | |
| 649 "could not load PEM client certificate, " OSSL_PACKAGE | |
| 650 " error %s, " | |
| 651 "(no key found, wrong pass phrase, or wrong file format?)", | |
| 652 ossl_strerror(ERR_get_error(), error_buffer, | |
| 653 sizeof(error_buffer)) ); | |
| 654 return 0; | |
| 655 } | |
| 656 break; | |
| 657 | |
| 658 case SSL_FILETYPE_ASN1: | |
| 659 /* SSL_CTX_use_certificate_file() works with either PEM or ASN1, but | |
| 660 we use the case above for PEM so this can only be performed with | |
| 661 ASN1 files. */ | |
| 662 if(SSL_CTX_use_certificate_file(ctx, | |
| 663 cert_file, | |
| 664 file_type) != 1) { | |
| 665 failf(data, | |
| 666 "could not load ASN1 client certificate, " OSSL_PACKAGE | |
| 667 " error %s, " | |
| 668 "(no key found, wrong pass phrase, or wrong file format?)", | |
| 669 ossl_strerror(ERR_get_error(), error_buffer, | |
| 670 sizeof(error_buffer)) ); | |
| 671 return 0; | |
| 672 } | |
| 673 break; | |
| 674 case SSL_FILETYPE_ENGINE: | |
| 675 #if defined(USE_OPENSSL_ENGINE) && defined(ENGINE_CTRL_GET_CMD_FROM_NAME) | |
| 676 { | |
| 677 /* Implicitly use pkcs11 engine if none was provided and the | |
| 678 * cert_file is a PKCS#11 URI */ | |
| 679 if(!data->state.engine) { | |
| 680 if(is_pkcs11_uri(cert_file)) { | |
| 681 if(Curl_ossl_set_engine(data, "pkcs11") != CURLE_OK) { | |
| 682 return 0; | |
| 683 } | |
| 684 } | |
| 685 } | |
| 686 | |
| 687 if(data->state.engine) { | |
| 688 const char *cmd_name = "LOAD_CERT_CTRL"; | |
| 689 struct { | |
| 690 const char *cert_id; | |
| 691 X509 *cert; | |
| 692 } params; | |
| 693 | |
| 694 params.cert_id = cert_file; | |
| 695 params.cert = NULL; | |
| 696 | |
| 697 /* Does the engine supports LOAD_CERT_CTRL ? */ | |
| 698 if(!ENGINE_ctrl(data->state.engine, ENGINE_CTRL_GET_CMD_FROM_NAME, | |
| 699 0, (void *)cmd_name, NULL)) { | |
| 700 failf(data, "ssl engine does not support loading certificates"); | |
| 701 return 0; | |
| 702 } | |
| 703 | |
| 704 /* Load the certificate from the engine */ | |
| 705 if(!ENGINE_ctrl_cmd(data->state.engine, cmd_name, | |
| 706 0, ¶ms, NULL, 1)) { | |
| 707 failf(data, "ssl engine cannot load client cert with id" | |
| 708 " '%s' [%s]", cert_file, | |
| 709 ossl_strerror(ERR_get_error(), error_buffer, | |
| 710 sizeof(error_buffer))); | |
| 711 return 0; | |
| 712 } | |
| 713 | |
| 714 if(!params.cert) { | |
| 715 failf(data, "ssl engine didn't initialized the certificate " | |
| 716 "properly."); | |
| 717 return 0; | |
| 718 } | |
| 719 | |
| 720 if(SSL_CTX_use_certificate(ctx, params.cert) != 1) { | |
| 721 failf(data, "unable to set client certificate"); | |
| 722 X509_free(params.cert); | |
| 723 return 0; | |
| 724 } | |
| 725 X509_free(params.cert); /* we don't need the handle any more... */ | |
| 726 } | |
| 727 else { | |
| 728 failf(data, "crypto engine not set, can't load certificate"); | |
| 729 return 0; | |
| 730 } | |
| 731 } | |
| 732 break; | |
| 733 #else | |
| 734 failf(data, "file type ENG for certificate not implemented"); | |
| 735 return 0; | |
| 736 #endif | |
| 737 | |
| 738 case SSL_FILETYPE_PKCS12: | |
| 739 { | |
| 740 BIO *fp = NULL; | |
| 741 PKCS12 *p12 = NULL; | |
| 742 EVP_PKEY *pri; | |
| 743 STACK_OF(X509) *ca = NULL; | |
| 744 | |
| 745 fp = BIO_new(BIO_s_file()); | |
| 746 if(fp == NULL) { | |
| 747 failf(data, | |
| 748 "BIO_new return NULL, " OSSL_PACKAGE | |
| 749 " error %s", | |
| 750 ossl_strerror(ERR_get_error(), error_buffer, | |
| 751 sizeof(error_buffer)) ); | |
| 752 return 0; | |
| 753 } | |
| 754 | |
| 755 if(BIO_read_filename(fp, cert_file) <= 0) { | |
| 756 failf(data, "could not open PKCS12 file '%s'", cert_file); | |
| 757 BIO_free(fp); | |
| 758 return 0; | |
| 759 } | |
| 760 p12 = d2i_PKCS12_bio(fp, NULL); | |
| 761 BIO_free(fp); | |
| 762 | |
| 763 if(!p12) { | |
| 764 failf(data, "error reading PKCS12 file '%s'", cert_file); | |
| 765 return 0; | |
| 766 } | |
| 767 | |
| 768 PKCS12_PBE_add(); | |
| 769 | |
| 770 if(!PKCS12_parse(p12, key_passwd, &pri, &x509, | |
| 771 &ca)) { | |
| 772 failf(data, | |
| 773 "could not parse PKCS12 file, check password, " OSSL_PACKAGE | |
| 774 " error %s", | |
| 775 ossl_strerror(ERR_get_error(), error_buffer, | |
| 776 sizeof(error_buffer)) ); | |
| 777 PKCS12_free(p12); | |
| 778 return 0; | |
| 779 } | |
| 780 | |
| 781 PKCS12_free(p12); | |
| 782 | |
| 783 if(SSL_CTX_use_certificate(ctx, x509) != 1) { | |
| 784 failf(data, | |
| 785 "could not load PKCS12 client certificate, " OSSL_PACKAGE | |
| 786 " error %s", | |
| 787 ossl_strerror(ERR_get_error(), error_buffer, | |
| 788 sizeof(error_buffer)) ); | |
| 789 goto fail; | |
| 790 } | |
| 791 | |
| 792 if(SSL_CTX_use_PrivateKey(ctx, pri) != 1) { | |
| 793 failf(data, "unable to use private key from PKCS12 file '%s'", | |
| 794 cert_file); | |
| 795 goto fail; | |
| 796 } | |
| 797 | |
| 798 if(!SSL_CTX_check_private_key (ctx)) { | |
| 799 failf(data, "private key from PKCS12 file '%s' " | |
| 800 "does not match certificate in same file", cert_file); | |
| 801 goto fail; | |
| 802 } | |
| 803 /* Set Certificate Verification chain */ | |
| 804 if(ca) { | |
| 805 while(sk_X509_num(ca)) { | |
| 806 /* | |
| 807 * Note that sk_X509_pop() is used below to make sure the cert is | |
| 808 * removed from the stack properly before getting passed to | |
| 809 * SSL_CTX_add_extra_chain_cert(), which takes ownership. Previously | |
| 810 * we used sk_X509_value() instead, but then we'd clean it in the | |
| 811 * subsequent sk_X509_pop_free() call. | |
| 812 */ | |
| 813 X509 *x = sk_X509_pop(ca); | |
| 814 if(!SSL_CTX_add_client_CA(ctx, x)) { | |
| 815 X509_free(x); | |
| 816 failf(data, "cannot add certificate to client CA list"); | |
| 817 goto fail; | |
| 818 } | |
| 819 if(!SSL_CTX_add_extra_chain_cert(ctx, x)) { | |
| 820 X509_free(x); | |
| 821 failf(data, "cannot add certificate to certificate chain"); | |
| 822 goto fail; | |
| 823 } | |
| 824 } | |
| 825 } | |
| 826 | |
| 827 cert_done = 1; | |
| 828 fail: | |
| 829 EVP_PKEY_free(pri); | |
| 830 X509_free(x509); | |
| 831 #ifdef USE_AMISSL | |
| 832 sk_X509_pop_free(ca, Curl_amiga_X509_free); | |
| 833 #else | |
| 834 sk_X509_pop_free(ca, X509_free); | |
| 835 #endif | |
| 836 if(!cert_done) | |
| 837 return 0; /* failure! */ | |
| 838 break; | |
| 839 } | |
| 840 default: | |
| 841 failf(data, "not supported file type '%s' for certificate", cert_type); | |
| 842 return 0; | |
| 843 } | |
| 844 | |
| 845 if(!key_file) | |
| 846 key_file = cert_file; | |
| 847 else | |
| 848 file_type = do_file_type(key_type); | |
| 849 | |
| 850 switch(file_type) { | |
| 851 case SSL_FILETYPE_PEM: | |
| 852 if(cert_done) | |
| 853 break; | |
| 854 /* FALLTHROUGH */ | |
| 855 case SSL_FILETYPE_ASN1: | |
| 856 if(SSL_CTX_use_PrivateKey_file(ctx, key_file, file_type) != 1) { | |
| 857 failf(data, "unable to set private key file: '%s' type %s", | |
| 858 key_file, key_type?key_type:"PEM"); | |
| 859 return 0; | |
| 860 } | |
| 861 break; | |
| 862 case SSL_FILETYPE_ENGINE: | |
| 863 #ifdef USE_OPENSSL_ENGINE | |
| 864 { /* XXXX still needs some work */ | |
| 865 EVP_PKEY *priv_key = NULL; | |
| 866 | |
| 867 /* Implicitly use pkcs11 engine if none was provided and the | |
| 868 * key_file is a PKCS#11 URI */ | |
| 869 if(!data->state.engine) { | |
| 870 if(is_pkcs11_uri(key_file)) { | |
| 871 if(Curl_ossl_set_engine(data, "pkcs11") != CURLE_OK) { | |
| 872 return 0; | |
| 873 } | |
| 874 } | |
| 875 } | |
| 876 | |
| 877 if(data->state.engine) { | |
| 878 UI_METHOD *ui_method = | |
| 879 UI_create_method((char *)"curl user interface"); | |
| 880 if(!ui_method) { | |
| 881 failf(data, "unable do create " OSSL_PACKAGE | |
| 882 " user-interface method"); | |
| 883 return 0; | |
| 884 } | |
| 885 UI_method_set_opener(ui_method, UI_method_get_opener(UI_OpenSSL())); | |
| 886 UI_method_set_closer(ui_method, UI_method_get_closer(UI_OpenSSL())); | |
| 887 UI_method_set_reader(ui_method, ssl_ui_reader); | |
| 888 UI_method_set_writer(ui_method, ssl_ui_writer); | |
| 889 /* the typecast below was added to please mingw32 */ | |
| 890 priv_key = (EVP_PKEY *) | |
| 891 ENGINE_load_private_key(data->state.engine, key_file, | |
| 892 ui_method, | |
| 893 key_passwd); | |
| 894 UI_destroy_method(ui_method); | |
| 895 if(!priv_key) { | |
| 896 failf(data, "failed to load private key from crypto engine"); | |
| 897 return 0; | |
| 898 } | |
| 899 if(SSL_CTX_use_PrivateKey(ctx, priv_key) != 1) { | |
| 900 failf(data, "unable to set private key"); | |
| 901 EVP_PKEY_free(priv_key); | |
| 902 return 0; | |
| 903 } | |
| 904 EVP_PKEY_free(priv_key); /* we don't need the handle any more... */ | |
| 905 } | |
| 906 else { | |
| 907 failf(data, "crypto engine not set, can't load private key"); | |
| 908 return 0; | |
| 909 } | |
| 910 } | |
| 911 break; | |
| 912 #else | |
| 913 failf(data, "file type ENG for private key not supported"); | |
| 914 return 0; | |
| 915 #endif | |
| 916 case SSL_FILETYPE_PKCS12: | |
| 917 if(!cert_done) { | |
| 918 failf(data, "file type P12 for private key not supported"); | |
| 919 return 0; | |
| 920 } | |
| 921 break; | |
| 922 default: | |
| 923 failf(data, "not supported file type for private key"); | |
| 924 return 0; | |
| 925 } | |
| 926 | |
| 927 ssl = SSL_new(ctx); | |
| 928 if(!ssl) { | |
| 929 failf(data, "unable to create an SSL structure"); | |
| 930 return 0; | |
| 931 } | |
| 932 | |
| 933 x509 = SSL_get_certificate(ssl); | |
| 934 | |
| 935 /* This version was provided by Evan Jordan and is supposed to not | |
| 936 leak memory as the previous version: */ | |
| 937 if(x509) { | |
| 938 EVP_PKEY *pktmp = X509_get_pubkey(x509); | |
| 939 EVP_PKEY_copy_parameters(pktmp, SSL_get_privatekey(ssl)); | |
| 940 EVP_PKEY_free(pktmp); | |
| 941 } | |
| 942 | |
| 943 #if !defined(OPENSSL_NO_RSA) && !defined(OPENSSL_IS_BORINGSSL) | |
| 944 { | |
| 945 /* If RSA is used, don't check the private key if its flags indicate | |
| 946 * it doesn't support it. */ | |
| 947 EVP_PKEY *priv_key = SSL_get_privatekey(ssl); | |
| 948 int pktype; | |
| 949 #ifdef HAVE_OPAQUE_EVP_PKEY | |
| 950 pktype = EVP_PKEY_id(priv_key); | |
| 951 #else | |
| 952 pktype = priv_key->type; | |
| 953 #endif | |
| 954 if(pktype == EVP_PKEY_RSA) { | |
| 955 RSA *rsa = EVP_PKEY_get1_RSA(priv_key); | |
| 956 if(RSA_flags(rsa) & RSA_METHOD_FLAG_NO_CHECK) | |
| 957 check_privkey = FALSE; | |
| 958 RSA_free(rsa); /* Decrement reference count */ | |
| 959 } | |
| 960 } | |
| 961 #endif | |
| 962 | |
| 963 SSL_free(ssl); | |
| 964 | |
| 965 /* If we are using DSA, we can copy the parameters from | |
| 966 * the private key */ | |
| 967 | |
| 968 if(check_privkey == TRUE) { | |
| 969 /* Now we know that a key and cert have been set against | |
| 970 * the SSL context */ | |
| 971 if(!SSL_CTX_check_private_key(ctx)) { | |
| 972 failf(data, "Private key does not match the certificate public key"); | |
| 973 return 0; | |
| 974 } | |
| 975 } | |
| 976 } | |
| 977 return 1; | |
| 978 } | |
| 979 | |
| 980 /* returns non-zero on failure */ | |
| 981 static int x509_name_oneline(X509_NAME *a, char *buf, size_t size) | |
| 982 { | |
| 983 #if 0 | |
| 984 return X509_NAME_oneline(a, buf, size); | |
| 985 #else | |
| 986 BIO *bio_out = BIO_new(BIO_s_mem()); | |
| 987 BUF_MEM *biomem; | |
| 988 int rc; | |
| 989 | |
| 990 if(!bio_out) | |
| 991 return 1; /* alloc failed! */ | |
| 992 | |
| 993 rc = X509_NAME_print_ex(bio_out, a, 0, XN_FLAG_SEP_SPLUS_SPC); | |
| 994 BIO_get_mem_ptr(bio_out, &biomem); | |
| 995 | |
| 996 if((size_t)biomem->length < size) | |
| 997 size = biomem->length; | |
| 998 else | |
| 999 size--; /* don't overwrite the buffer end */ | |
| 1000 | |
| 1001 memcpy(buf, biomem->data, size); | |
| 1002 buf[size] = 0; | |
| 1003 | |
| 1004 BIO_free(bio_out); | |
| 1005 | |
| 1006 return !rc; | |
| 1007 #endif | |
| 1008 } | |
| 1009 | |
| 1010 /** | |
| 1011 * Global SSL init | |
| 1012 * | |
| 1013 * @retval 0 error initializing SSL | |
| 1014 * @retval 1 SSL initialized successfully | |
| 1015 */ | |
| 1016 static int Curl_ossl_init(void) | |
| 1017 { | |
| 1018 #ifdef ENABLE_SSLKEYLOGFILE | |
| 1019 char *keylog_file_name; | |
| 1020 #endif | |
| 1021 | |
| 1022 OPENSSL_load_builtin_modules(); | |
| 1023 | |
| 1024 #ifdef USE_OPENSSL_ENGINE | |
| 1025 ENGINE_load_builtin_engines(); | |
| 1026 #endif | |
| 1027 | |
| 1028 /* CONF_MFLAGS_DEFAULT_SECTION was introduced some time between 0.9.8b and | |
| 1029 0.9.8e */ | |
| 1030 #ifndef CONF_MFLAGS_DEFAULT_SECTION | |
| 1031 #define CONF_MFLAGS_DEFAULT_SECTION 0x0 | |
| 1032 #endif | |
| 1033 | |
| 1034 #ifndef CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG | |
| 1035 CONF_modules_load_file(NULL, NULL, | |
| 1036 CONF_MFLAGS_DEFAULT_SECTION| | |
| 1037 CONF_MFLAGS_IGNORE_MISSING_FILE); | |
| 1038 #endif | |
| 1039 | |
| 1040 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \ | |
| 1041 !defined(LIBRESSL_VERSION_NUMBER) | |
| 1042 /* OpenSSL 1.1.0+ takes care of initialization itself */ | |
| 1043 #else | |
| 1044 /* Lets get nice error messages */ | |
| 1045 SSL_load_error_strings(); | |
| 1046 | |
| 1047 /* Init the global ciphers and digests */ | |
| 1048 if(!SSLeay_add_ssl_algorithms()) | |
| 1049 return 0; | |
| 1050 | |
| 1051 OpenSSL_add_all_algorithms(); | |
| 1052 #endif | |
| 1053 | |
| 1054 #ifdef ENABLE_SSLKEYLOGFILE | |
| 1055 if(!keylog_file_fp) { | |
| 1056 keylog_file_name = curl_getenv("SSLKEYLOGFILE"); | |
| 1057 if(keylog_file_name) { | |
| 1058 keylog_file_fp = fopen(keylog_file_name, FOPEN_APPENDTEXT); | |
| 1059 if(keylog_file_fp) { | |
| 1060 #ifdef WIN32 | |
| 1061 if(setvbuf(keylog_file_fp, NULL, _IONBF, 0)) | |
| 1062 #else | |
| 1063 if(setvbuf(keylog_file_fp, NULL, _IOLBF, 4096)) | |
| 1064 #endif | |
| 1065 { | |
| 1066 fclose(keylog_file_fp); | |
| 1067 keylog_file_fp = NULL; | |
| 1068 } | |
| 1069 } | |
| 1070 Curl_safefree(keylog_file_name); | |
| 1071 } | |
| 1072 } | |
| 1073 #endif | |
| 1074 | |
| 1075 /* Initialize the extra data indexes */ | |
| 1076 if(ossl_get_ssl_conn_index() < 0 || ossl_get_ssl_sockindex_index() < 0) | |
| 1077 return 0; | |
| 1078 | |
| 1079 return 1; | |
| 1080 } | |
| 1081 | |
| 1082 /* Global cleanup */ | |
| 1083 static void Curl_ossl_cleanup(void) | |
| 1084 { | |
| 1085 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && \ | |
| 1086 !defined(LIBRESSL_VERSION_NUMBER) | |
| 1087 /* OpenSSL 1.1 deprecates all these cleanup functions and | |
| 1088 turns them into no-ops in OpenSSL 1.0 compatibility mode */ | |
| 1089 #else | |
| 1090 /* Free ciphers and digests lists */ | |
| 1091 EVP_cleanup(); | |
| 1092 | |
| 1093 #ifdef USE_OPENSSL_ENGINE | |
| 1094 /* Free engine list */ | |
| 1095 ENGINE_cleanup(); | |
| 1096 #endif | |
| 1097 | |
| 1098 /* Free OpenSSL error strings */ | |
| 1099 ERR_free_strings(); | |
| 1100 | |
| 1101 /* Free thread local error state, destroying hash upon zero refcount */ | |
| 1102 #ifdef HAVE_ERR_REMOVE_THREAD_STATE | |
| 1103 ERR_remove_thread_state(NULL); | |
| 1104 #else | |
| 1105 ERR_remove_state(0); | |
| 1106 #endif | |
| 1107 | |
| 1108 /* Free all memory allocated by all configuration modules */ | |
| 1109 CONF_modules_free(); | |
| 1110 | |
| 1111 #ifdef HAVE_SSL_COMP_FREE_COMPRESSION_METHODS | |
| 1112 SSL_COMP_free_compression_methods(); | |
| 1113 #endif | |
| 1114 #endif | |
| 1115 | |
| 1116 #ifdef ENABLE_SSLKEYLOGFILE | |
| 1117 if(keylog_file_fp) { | |
| 1118 fclose(keylog_file_fp); | |
| 1119 keylog_file_fp = NULL; | |
| 1120 } | |
| 1121 #endif | |
| 1122 } | |
| 1123 | |
| 1124 /* | |
| 1125 * This function is used to determine connection status. | |
| 1126 * | |
| 1127 * Return codes: | |
| 1128 * 1 means the connection is still in place | |
| 1129 * 0 means the connection has been closed | |
| 1130 * -1 means the connection status is unknown | |
| 1131 */ | |
| 1132 static int Curl_ossl_check_cxn(struct connectdata *conn) | |
| 1133 { | |
| 1134 /* SSL_peek takes data out of the raw recv buffer without peeking so we use | |
| 1135 recv MSG_PEEK instead. Bug #795 */ | |
| 1136 #ifdef MSG_PEEK | |
| 1137 char buf; | |
| 1138 ssize_t nread; | |
| 1139 nread = recv((RECV_TYPE_ARG1)conn->sock[FIRSTSOCKET], (RECV_TYPE_ARG2)&buf, | |
| 1140 (RECV_TYPE_ARG3)1, (RECV_TYPE_ARG4)MSG_PEEK); | |
| 1141 if(nread == 0) | |
| 1142 return 0; /* connection has been closed */ | |
| 1143 if(nread == 1) | |
| 1144 return 1; /* connection still in place */ | |
| 1145 else if(nread == -1) { | |
| 1146 int err = SOCKERRNO; | |
| 1147 if(err == EINPROGRESS || | |
| 1148 #if defined(EAGAIN) && (EAGAIN != EWOULDBLOCK) | |
| 1149 err == EAGAIN || | |
| 1150 #endif | |
| 1151 err == EWOULDBLOCK) | |
| 1152 return 1; /* connection still in place */ | |
| 1153 if(err == ECONNRESET || | |
| 1154 #ifdef ECONNABORTED | |
| 1155 err == ECONNABORTED || | |
| 1156 #endif | |
| 1157 #ifdef ENETDOWN | |
| 1158 err == ENETDOWN || | |
| 1159 #endif | |
| 1160 #ifdef ENETRESET | |
| 1161 err == ENETRESET || | |
| 1162 #endif | |
| 1163 #ifdef ESHUTDOWN | |
| 1164 err == ESHUTDOWN || | |
| 1165 #endif | |
| 1166 #ifdef ETIMEDOUT | |
| 1167 err == ETIMEDOUT || | |
| 1168 #endif | |
| 1169 err == ENOTCONN) | |
| 1170 return 0; /* connection has been closed */ | |
| 1171 } | |
| 1172 #endif | |
| 1173 return -1; /* connection status unknown */ | |
| 1174 } | |
| 1175 | |
| 1176 /* Selects an OpenSSL crypto engine | |
| 1177 */ | |
| 1178 static CURLcode Curl_ossl_set_engine(struct Curl_easy *data, | |
| 1179 const char *engine) | |
| 1180 { | |
| 1181 #ifdef USE_OPENSSL_ENGINE | |
| 1182 ENGINE *e; | |
| 1183 | |
| 1184 #if OPENSSL_VERSION_NUMBER >= 0x00909000L | |
| 1185 e = ENGINE_by_id(engine); | |
| 1186 #else | |
| 1187 /* avoid memory leak */ | |
| 1188 for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) { | |
| 1189 const char *e_id = ENGINE_get_id(e); | |
| 1190 if(!strcmp(engine, e_id)) | |
| 1191 break; | |
| 1192 } | |
| 1193 #endif | |
| 1194 | |
| 1195 if(!e) { | |
| 1196 failf(data, "SSL Engine '%s' not found", engine); | |
| 1197 return CURLE_SSL_ENGINE_NOTFOUND; | |
| 1198 } | |
| 1199 | |
| 1200 if(data->state.engine) { | |
| 1201 ENGINE_finish(data->state.engine); | |
| 1202 ENGINE_free(data->state.engine); | |
| 1203 data->state.engine = NULL; | |
| 1204 } | |
| 1205 if(!ENGINE_init(e)) { | |
| 1206 char buf[256]; | |
| 1207 | |
| 1208 ENGINE_free(e); | |
| 1209 failf(data, "Failed to initialise SSL Engine '%s':\n%s", | |
| 1210 engine, ossl_strerror(ERR_get_error(), buf, sizeof(buf))); | |
| 1211 return CURLE_SSL_ENGINE_INITFAILED; | |
| 1212 } | |
| 1213 data->state.engine = e; | |
| 1214 return CURLE_OK; | |
| 1215 #else | |
| 1216 (void)engine; | |
| 1217 failf(data, "SSL Engine not supported"); | |
| 1218 return CURLE_SSL_ENGINE_NOTFOUND; | |
| 1219 #endif | |
| 1220 } | |
| 1221 | |
| 1222 /* Sets engine as default for all SSL operations | |
| 1223 */ | |
| 1224 static CURLcode Curl_ossl_set_engine_default(struct Curl_easy *data) | |
| 1225 { | |
| 1226 #ifdef USE_OPENSSL_ENGINE | |
| 1227 if(data->state.engine) { | |
| 1228 if(ENGINE_set_default(data->state.engine, ENGINE_METHOD_ALL) > 0) { | |
| 1229 infof(data, "set default crypto engine '%s'\n", | |
| 1230 ENGINE_get_id(data->state.engine)); | |
| 1231 } | |
| 1232 else { | |
| 1233 failf(data, "set default crypto engine '%s' failed", | |
| 1234 ENGINE_get_id(data->state.engine)); | |
| 1235 return CURLE_SSL_ENGINE_SETFAILED; | |
| 1236 } | |
| 1237 } | |
| 1238 #else | |
| 1239 (void) data; | |
| 1240 #endif | |
| 1241 return CURLE_OK; | |
| 1242 } | |
| 1243 | |
| 1244 /* Return list of OpenSSL crypto engine names. | |
| 1245 */ | |
| 1246 static struct curl_slist *Curl_ossl_engines_list(struct Curl_easy *data) | |
| 1247 { | |
| 1248 struct curl_slist *list = NULL; | |
| 1249 #ifdef USE_OPENSSL_ENGINE | |
| 1250 struct curl_slist *beg; | |
| 1251 ENGINE *e; | |
| 1252 | |
| 1253 for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) { | |
| 1254 beg = curl_slist_append(list, ENGINE_get_id(e)); | |
| 1255 if(!beg) { | |
| 1256 curl_slist_free_all(list); | |
| 1257 return NULL; | |
| 1258 } | |
| 1259 list = beg; | |
| 1260 } | |
| 1261 #endif | |
| 1262 (void) data; | |
| 1263 return list; | |
| 1264 } | |
| 1265 | |
| 1266 | |
| 1267 static void ossl_close(struct ssl_connect_data *connssl) | |
| 1268 { | |
| 1269 if(BACKEND->handle) { | |
| 1270 (void)SSL_shutdown(BACKEND->handle); | |
| 1271 SSL_set_connect_state(BACKEND->handle); | |
| 1272 | |
| 1273 SSL_free(BACKEND->handle); | |
| 1274 BACKEND->handle = NULL; | |
| 1275 } | |
| 1276 if(BACKEND->ctx) { | |
| 1277 SSL_CTX_free(BACKEND->ctx); | |
| 1278 BACKEND->ctx = NULL; | |
| 1279 } | |
| 1280 } | |
| 1281 | |
| 1282 /* | |
| 1283 * This function is called when an SSL connection is closed. | |
| 1284 */ | |
| 1285 static void Curl_ossl_close(struct connectdata *conn, int sockindex) | |
| 1286 { | |
| 1287 ossl_close(&conn->ssl[sockindex]); | |
| 1288 ossl_close(&conn->proxy_ssl[sockindex]); | |
| 1289 } | |
| 1290 | |
| 1291 /* | |
| 1292 * This function is called to shut down the SSL layer but keep the | |
| 1293 * socket open (CCC - Clear Command Channel) | |
| 1294 */ | |
| 1295 static int Curl_ossl_shutdown(struct connectdata *conn, int sockindex) | |
| 1296 { | |
| 1297 int retval = 0; | |
| 1298 struct ssl_connect_data *connssl = &conn->ssl[sockindex]; | |
| 1299 struct Curl_easy *data = conn->data; | |
| 1300 char buf[256]; /* We will use this for the OpenSSL error buffer, so it has | |
| 1301 to be at least 256 bytes long. */ | |
| 1302 unsigned long sslerror; | |
| 1303 ssize_t nread; | |
| 1304 int buffsize; | |
| 1305 int err; | |
| 1306 bool done = FALSE; | |
| 1307 | |
| 1308 #ifndef CURL_DISABLE_FTP | |
| 1309 /* This has only been tested on the proftpd server, and the mod_tls code | |
| 1310 sends a close notify alert without waiting for a close notify alert in | |
| 1311 response. Thus we wait for a close notify alert from the server, but | |
| 1312 we do not send one. Let's hope other servers do the same... */ | |
| 1313 | |
| 1314 if(data->set.ftp_ccc == CURLFTPSSL_CCC_ACTIVE) | |
| 1315 (void)SSL_shutdown(BACKEND->handle); | |
| 1316 #endif | |
| 1317 | |
| 1318 if(BACKEND->handle) { | |
| 1319 buffsize = (int)sizeof(buf); | |
| 1320 while(!done) { | |
| 1321 int what = SOCKET_READABLE(conn->sock[sockindex], | |
| 1322 SSL_SHUTDOWN_TIMEOUT); | |
| 1323 if(what > 0) { | |
| 1324 ERR_clear_error(); | |
| 1325 | |
| 1326 /* Something to read, let's do it and hope that it is the close | |
| 1327 notify alert from the server */ | |
| 1328 nread = (ssize_t)SSL_read(BACKEND->handle, buf, buffsize); | |
| 1329 err = SSL_get_error(BACKEND->handle, (int)nread); | |
| 1330 | |
| 1331 switch(err) { | |
| 1332 case SSL_ERROR_NONE: /* this is not an error */ | |
| 1333 case SSL_ERROR_ZERO_RETURN: /* no more data */ | |
| 1334 /* This is the expected response. There was no data but only | |
| 1335 the close notify alert */ | |
| 1336 done = TRUE; | |
| 1337 break; | |
| 1338 case SSL_ERROR_WANT_READ: | |
| 1339 /* there's data pending, re-invoke SSL_read() */ | |
| 1340 infof(data, "SSL_ERROR_WANT_READ\n"); | |
| 1341 break; | |
| 1342 case SSL_ERROR_WANT_WRITE: | |
| 1343 /* SSL wants a write. Really odd. Let's bail out. */ | |
| 1344 infof(data, "SSL_ERROR_WANT_WRITE\n"); | |
| 1345 done = TRUE; | |
| 1346 break; | |
| 1347 default: | |
| 1348 /* openssl/ssl.h says "look at error stack/return value/errno" */ | |
| 1349 sslerror = ERR_get_error(); | |
| 1350 failf(conn->data, OSSL_PACKAGE " SSL_read on shutdown: %s, errno %d", | |
| 1351 (sslerror ? | |
| 1352 ossl_strerror(sslerror, buf, sizeof(buf)) : | |
| 1353 SSL_ERROR_to_str(err)), | |
| 1354 SOCKERRNO); | |
| 1355 done = TRUE; | |
| 1356 break; | |
| 1357 } | |
| 1358 } | |
| 1359 else if(0 == what) { | |
| 1360 /* timeout */ | |
| 1361 failf(data, "SSL shutdown timeout"); | |
| 1362 done = TRUE; | |
| 1363 } | |
| 1364 else { | |
| 1365 /* anything that gets here is fatally bad */ | |
| 1366 failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO); | |
| 1367 retval = -1; | |
| 1368 done = TRUE; | |
| 1369 } | |
| 1370 } /* while()-loop for the select() */ | |
| 1371 | |
| 1372 if(data->set.verbose) { | |
| 1373 #ifdef HAVE_SSL_GET_SHUTDOWN | |
| 1374 switch(SSL_get_shutdown(BACKEND->handle)) { | |
| 1375 case SSL_SENT_SHUTDOWN: | |
| 1376 infof(data, "SSL_get_shutdown() returned SSL_SENT_SHUTDOWN\n"); | |
| 1377 break; | |
| 1378 case SSL_RECEIVED_SHUTDOWN: | |
| 1379 infof(data, "SSL_get_shutdown() returned SSL_RECEIVED_SHUTDOWN\n"); | |
| 1380 break; | |
| 1381 case SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN: | |
| 1382 infof(data, "SSL_get_shutdown() returned SSL_SENT_SHUTDOWN|" | |
| 1383 "SSL_RECEIVED__SHUTDOWN\n"); | |
| 1384 break; | |
| 1385 } | |
| 1386 #endif | |
| 1387 } | |
| 1388 | |
| 1389 SSL_free(BACKEND->handle); | |
| 1390 BACKEND->handle = NULL; | |
| 1391 } | |
| 1392 return retval; | |
| 1393 } | |
| 1394 | |
| 1395 static void Curl_ossl_session_free(void *ptr) | |
| 1396 { | |
| 1397 /* free the ID */ | |
| 1398 SSL_SESSION_free(ptr); | |
| 1399 } | |
| 1400 | |
| 1401 /* | |
| 1402 * This function is called when the 'data' struct is going away. Close | |
| 1403 * down everything and free all resources! | |
| 1404 */ | |
| 1405 static void Curl_ossl_close_all(struct Curl_easy *data) | |
| 1406 { | |
| 1407 #ifdef USE_OPENSSL_ENGINE | |
| 1408 if(data->state.engine) { | |
| 1409 ENGINE_finish(data->state.engine); | |
| 1410 ENGINE_free(data->state.engine); | |
| 1411 data->state.engine = NULL; | |
| 1412 } | |
| 1413 #else | |
| 1414 (void)data; | |
| 1415 #endif | |
| 1416 #if !defined(HAVE_ERR_REMOVE_THREAD_STATE_DEPRECATED) && \ | |
| 1417 defined(HAVE_ERR_REMOVE_THREAD_STATE) | |
| 1418 /* OpenSSL 1.0.1 and 1.0.2 build an error queue that is stored per-thread | |
| 1419 so we need to clean it here in case the thread will be killed. All OpenSSL | |
| 1420 code should extract the error in association with the error so clearing | |
| 1421 this queue here should be harmless at worst. */ | |
| 1422 ERR_remove_thread_state(NULL); | |
| 1423 #endif | |
| 1424 } | |
| 1425 | |
| 1426 /* ====================================================== */ | |
| 1427 | |
| 1428 /* | |
| 1429 * Match subjectAltName against the host name. This requires a conversion | |
| 1430 * in CURL_DOES_CONVERSIONS builds. | |
| 1431 */ | |
| 1432 static bool subj_alt_hostcheck(struct Curl_easy *data, | |
| 1433 const char *match_pattern, const char *hostname, | |
| 1434 const char *dispname) | |
| 1435 #ifdef CURL_DOES_CONVERSIONS | |
| 1436 { | |
| 1437 bool res = FALSE; | |
| 1438 | |
| 1439 /* Curl_cert_hostcheck uses host encoding, but we get ASCII from | |
| 1440 OpenSSl. | |
| 1441 */ | |
| 1442 char *match_pattern2 = strdup(match_pattern); | |
| 1443 | |
| 1444 if(match_pattern2) { | |
| 1445 if(Curl_convert_from_network(data, match_pattern2, | |
| 1446 strlen(match_pattern2)) == CURLE_OK) { | |
| 1447 if(Curl_cert_hostcheck(match_pattern2, hostname)) { | |
| 1448 res = TRUE; | |
| 1449 infof(data, | |
| 1450 " subjectAltName: host \"%s\" matched cert's \"%s\"\n", | |
| 1451 dispname, match_pattern2); | |
| 1452 } | |
| 1453 } | |
| 1454 free(match_pattern2); | |
| 1455 } | |
| 1456 else { | |
| 1457 failf(data, | |
| 1458 "SSL: out of memory when allocating temporary for subjectAltName"); | |
| 1459 } | |
| 1460 return res; | |
| 1461 } | |
| 1462 #else | |
| 1463 { | |
| 1464 #ifdef CURL_DISABLE_VERBOSE_STRINGS | |
| 1465 (void)dispname; | |
| 1466 (void)data; | |
| 1467 #endif | |
| 1468 if(Curl_cert_hostcheck(match_pattern, hostname)) { | |
| 1469 infof(data, " subjectAltName: host \"%s\" matched cert's \"%s\"\n", | |
| 1470 dispname, match_pattern); | |
| 1471 return TRUE; | |
| 1472 } | |
| 1473 return FALSE; | |
| 1474 } | |
| 1475 #endif | |
| 1476 | |
| 1477 | |
| 1478 /* Quote from RFC2818 section 3.1 "Server Identity" | |
| 1479 | |
| 1480 If a subjectAltName extension of type dNSName is present, that MUST | |
| 1481 be used as the identity. Otherwise, the (most specific) Common Name | |
| 1482 field in the Subject field of the certificate MUST be used. Although | |
| 1483 the use of the Common Name is existing practice, it is deprecated and | |
| 1484 Certification Authorities are encouraged to use the dNSName instead. | |
| 1485 | |
| 1486 Matching is performed using the matching rules specified by | |
| 1487 [RFC2459]. If more than one identity of a given type is present in | |
| 1488 the certificate (e.g., more than one dNSName name, a match in any one | |
| 1489 of the set is considered acceptable.) Names may contain the wildcard | |
| 1490 character * which is considered to match any single domain name | |
| 1491 component or component fragment. E.g., *.a.com matches foo.a.com but | |
| 1492 not bar.foo.a.com. f*.com matches foo.com but not bar.com. | |
| 1493 | |
| 1494 In some cases, the URI is specified as an IP address rather than a | |
| 1495 hostname. In this case, the iPAddress subjectAltName must be present | |
| 1496 in the certificate and must exactly match the IP in the URI. | |
| 1497 | |
| 1498 */ | |
| 1499 static CURLcode verifyhost(struct connectdata *conn, X509 *server_cert) | |
| 1500 { | |
| 1501 bool matched = FALSE; | |
| 1502 int target = GEN_DNS; /* target type, GEN_DNS or GEN_IPADD */ | |
| 1503 size_t addrlen = 0; | |
| 1504 struct Curl_easy *data = conn->data; | |
| 1505 STACK_OF(GENERAL_NAME) *altnames; | |
| 1506 #ifdef ENABLE_IPV6 | |
| 1507 struct in6_addr addr; | |
| 1508 #else | |
| 1509 struct in_addr addr; | |
| 1510 #endif | |
| 1511 CURLcode result = CURLE_OK; | |
| 1512 bool dNSName = FALSE; /* if a dNSName field exists in the cert */ | |
| 1513 bool iPAddress = FALSE; /* if a iPAddress field exists in the cert */ | |
| 1514 const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name : | |
| 1515 conn->host.name; | |
| 1516 const char * const dispname = SSL_IS_PROXY() ? | |
| 1517 conn->http_proxy.host.dispname : conn->host.dispname; | |
| 1518 | |
| 1519 #ifdef ENABLE_IPV6 | |
| 1520 if(conn->bits.ipv6_ip && | |
| 1521 Curl_inet_pton(AF_INET6, hostname, &addr)) { | |
| 1522 target = GEN_IPADD; | |
| 1523 addrlen = sizeof(struct in6_addr); | |
| 1524 } | |
| 1525 else | |
| 1526 #endif | |
| 1527 if(Curl_inet_pton(AF_INET, hostname, &addr)) { | |
| 1528 target = GEN_IPADD; | |
| 1529 addrlen = sizeof(struct in_addr); | |
| 1530 } | |
| 1531 | |
| 1532 /* get a "list" of alternative names */ | |
| 1533 altnames = X509_get_ext_d2i(server_cert, NID_subject_alt_name, NULL, NULL); | |
| 1534 | |
| 1535 if(altnames) { | |
| 1536 #ifdef OPENSSL_IS_BORINGSSL | |
| 1537 size_t numalts; | |
| 1538 size_t i; | |
| 1539 #else | |
| 1540 int numalts; | |
| 1541 int i; | |
| 1542 #endif | |
| 1543 bool dnsmatched = FALSE; | |
| 1544 bool ipmatched = FALSE; | |
| 1545 | |
| 1546 /* get amount of alternatives, RFC2459 claims there MUST be at least | |
| 1547 one, but we don't depend on it... */ | |
| 1548 numalts = sk_GENERAL_NAME_num(altnames); | |
| 1549 | |
| 1550 /* loop through all alternatives - until a dnsmatch */ | |
| 1551 for(i = 0; (i < numalts) && !dnsmatched; i++) { | |
| 1552 /* get a handle to alternative name number i */ | |
| 1553 const GENERAL_NAME *check = sk_GENERAL_NAME_value(altnames, i); | |
| 1554 | |
| 1555 if(check->type == GEN_DNS) | |
| 1556 dNSName = TRUE; | |
| 1557 else if(check->type == GEN_IPADD) | |
| 1558 iPAddress = TRUE; | |
| 1559 | |
| 1560 /* only check alternatives of the same type the target is */ | |
| 1561 if(check->type == target) { | |
| 1562 /* get data and length */ | |
| 1563 const char *altptr = (char *)ASN1_STRING_get0_data(check->d.ia5); | |
| 1564 size_t altlen = (size_t) ASN1_STRING_length(check->d.ia5); | |
| 1565 | |
| 1566 switch(target) { | |
| 1567 case GEN_DNS: /* name/pattern comparison */ | |
| 1568 /* The OpenSSL man page explicitly says: "In general it cannot be | |
| 1569 assumed that the data returned by ASN1_STRING_data() is null | |
| 1570 terminated or does not contain embedded nulls." But also that | |
| 1571 "The actual format of the data will depend on the actual string | |
| 1572 type itself: for example for an IA5String the data will be ASCII" | |
| 1573 | |
| 1574 It has been however verified that in 0.9.6 and 0.9.7, IA5String | |
| 1575 is always zero-terminated. | |
| 1576 */ | |
| 1577 if((altlen == strlen(altptr)) && | |
| 1578 /* if this isn't true, there was an embedded zero in the name | |
| 1579 string and we cannot match it. */ | |
| 1580 subj_alt_hostcheck(data, altptr, hostname, dispname)) { | |
| 1581 dnsmatched = TRUE; | |
| 1582 } | |
| 1583 break; | |
| 1584 | |
| 1585 case GEN_IPADD: /* IP address comparison */ | |
| 1586 /* compare alternative IP address if the data chunk is the same size | |
| 1587 our server IP address is */ | |
| 1588 if((altlen == addrlen) && !memcmp(altptr, &addr, altlen)) { | |
| 1589 ipmatched = TRUE; | |
| 1590 infof(data, | |
| 1591 " subjectAltName: host \"%s\" matched cert's IP address!\n", | |
| 1592 dispname); | |
| 1593 } | |
| 1594 break; | |
| 1595 } | |
| 1596 } | |
| 1597 } | |
| 1598 GENERAL_NAMES_free(altnames); | |
| 1599 | |
| 1600 if(dnsmatched || ipmatched) | |
| 1601 matched = TRUE; | |
| 1602 } | |
| 1603 | |
| 1604 if(matched) | |
| 1605 /* an alternative name matched */ | |
| 1606 ; | |
| 1607 else if(dNSName || iPAddress) { | |
| 1608 infof(data, " subjectAltName does not match %s\n", dispname); | |
| 1609 failf(data, "SSL: no alternative certificate subject name matches " | |
| 1610 "target host name '%s'", dispname); | |
| 1611 result = CURLE_PEER_FAILED_VERIFICATION; | |
| 1612 } | |
| 1613 else { | |
| 1614 /* we have to look to the last occurrence of a commonName in the | |
| 1615 distinguished one to get the most significant one. */ | |
| 1616 int j, i = -1; | |
| 1617 | |
| 1618 /* The following is done because of a bug in 0.9.6b */ | |
| 1619 | |
| 1620 unsigned char *nulstr = (unsigned char *)""; | |
| 1621 unsigned char *peer_CN = nulstr; | |
| 1622 | |
| 1623 X509_NAME *name = X509_get_subject_name(server_cert); | |
| 1624 if(name) | |
| 1625 while((j = X509_NAME_get_index_by_NID(name, NID_commonName, i)) >= 0) | |
| 1626 i = j; | |
| 1627 | |
| 1628 /* we have the name entry and we will now convert this to a string | |
| 1629 that we can use for comparison. Doing this we support BMPstring, | |
| 1630 UTF8 etc. */ | |
| 1631 | |
| 1632 if(i >= 0) { | |
| 1633 ASN1_STRING *tmp = | |
| 1634 X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name, i)); | |
| 1635 | |
| 1636 /* In OpenSSL 0.9.7d and earlier, ASN1_STRING_to_UTF8 fails if the input | |
| 1637 is already UTF-8 encoded. We check for this case and copy the raw | |
| 1638 string manually to avoid the problem. This code can be made | |
| 1639 conditional in the future when OpenSSL has been fixed. */ | |
| 1640 if(tmp) { | |
| 1641 if(ASN1_STRING_type(tmp) == V_ASN1_UTF8STRING) { | |
| 1642 j = ASN1_STRING_length(tmp); | |
| 1643 if(j >= 0) { | |
| 1644 peer_CN = OPENSSL_malloc(j + 1); | |
| 1645 if(peer_CN) { | |
| 1646 memcpy(peer_CN, ASN1_STRING_get0_data(tmp), j); | |
| 1647 peer_CN[j] = '\0'; | |
| 1648 } | |
| 1649 } | |
| 1650 } | |
| 1651 else /* not a UTF8 name */ | |
| 1652 j = ASN1_STRING_to_UTF8(&peer_CN, tmp); | |
| 1653 | |
| 1654 if(peer_CN && (curlx_uztosi(strlen((char *)peer_CN)) != j)) { | |
| 1655 /* there was a terminating zero before the end of string, this | |
| 1656 cannot match and we return failure! */ | |
| 1657 failf(data, "SSL: illegal cert name field"); | |
| 1658 result = CURLE_PEER_FAILED_VERIFICATION; | |
| 1659 } | |
| 1660 } | |
| 1661 } | |
| 1662 | |
| 1663 if(peer_CN == nulstr) | |
| 1664 peer_CN = NULL; | |
| 1665 else { | |
| 1666 /* convert peer_CN from UTF8 */ | |
| 1667 CURLcode rc = Curl_convert_from_utf8(data, (char *)peer_CN, | |
| 1668 strlen((char *)peer_CN)); | |
| 1669 /* Curl_convert_from_utf8 calls failf if unsuccessful */ | |
| 1670 if(rc) { | |
| 1671 OPENSSL_free(peer_CN); | |
| 1672 return rc; | |
| 1673 } | |
| 1674 } | |
| 1675 | |
| 1676 if(result) | |
| 1677 /* error already detected, pass through */ | |
| 1678 ; | |
| 1679 else if(!peer_CN) { | |
| 1680 failf(data, | |
| 1681 "SSL: unable to obtain common name from peer certificate"); | |
| 1682 result = CURLE_PEER_FAILED_VERIFICATION; | |
| 1683 } | |
| 1684 else if(!Curl_cert_hostcheck((const char *)peer_CN, hostname)) { | |
| 1685 failf(data, "SSL: certificate subject name '%s' does not match " | |
| 1686 "target host name '%s'", peer_CN, dispname); | |
| 1687 result = CURLE_PEER_FAILED_VERIFICATION; | |
| 1688 } | |
| 1689 else { | |
| 1690 infof(data, " common name: %s (matched)\n", peer_CN); | |
| 1691 } | |
| 1692 if(peer_CN) | |
| 1693 OPENSSL_free(peer_CN); | |
| 1694 } | |
| 1695 | |
| 1696 return result; | |
| 1697 } | |
| 1698 | |
| 1699 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \ | |
| 1700 !defined(OPENSSL_NO_OCSP) | |
| 1701 static CURLcode verifystatus(struct connectdata *conn, | |
| 1702 struct ssl_connect_data *connssl) | |
| 1703 { | |
| 1704 int i, ocsp_status; | |
| 1705 unsigned char *status; | |
| 1706 const unsigned char *p; | |
| 1707 CURLcode result = CURLE_OK; | |
| 1708 struct Curl_easy *data = conn->data; | |
| 1709 | |
| 1710 OCSP_RESPONSE *rsp = NULL; | |
| 1711 OCSP_BASICRESP *br = NULL; | |
| 1712 X509_STORE *st = NULL; | |
| 1713 STACK_OF(X509) *ch = NULL; | |
| 1714 | |
| 1715 long len = SSL_get_tlsext_status_ocsp_resp(BACKEND->handle, &status); | |
| 1716 | |
| 1717 if(!status) { | |
| 1718 failf(data, "No OCSP response received"); | |
| 1719 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1720 goto end; | |
| 1721 } | |
| 1722 p = status; | |
| 1723 rsp = d2i_OCSP_RESPONSE(NULL, &p, len); | |
| 1724 if(!rsp) { | |
| 1725 failf(data, "Invalid OCSP response"); | |
| 1726 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1727 goto end; | |
| 1728 } | |
| 1729 | |
| 1730 ocsp_status = OCSP_response_status(rsp); | |
| 1731 if(ocsp_status != OCSP_RESPONSE_STATUS_SUCCESSFUL) { | |
| 1732 failf(data, "Invalid OCSP response status: %s (%d)", | |
| 1733 OCSP_response_status_str(ocsp_status), ocsp_status); | |
| 1734 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1735 goto end; | |
| 1736 } | |
| 1737 | |
| 1738 br = OCSP_response_get1_basic(rsp); | |
| 1739 if(!br) { | |
| 1740 failf(data, "Invalid OCSP response"); | |
| 1741 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1742 goto end; | |
| 1743 } | |
| 1744 | |
| 1745 ch = SSL_get_peer_cert_chain(BACKEND->handle); | |
| 1746 st = SSL_CTX_get_cert_store(BACKEND->ctx); | |
| 1747 | |
| 1748 #if ((OPENSSL_VERSION_NUMBER <= 0x1000201fL) /* Fixed after 1.0.2a */ || \ | |
| 1749 (defined(LIBRESSL_VERSION_NUMBER) && \ | |
| 1750 LIBRESSL_VERSION_NUMBER <= 0x2040200fL)) | |
| 1751 /* The authorized responder cert in the OCSP response MUST be signed by the | |
| 1752 peer cert's issuer (see RFC6960 section 4.2.2.2). If that's a root cert, | |
| 1753 no problem, but if it's an intermediate cert OpenSSL has a bug where it | |
| 1754 expects this issuer to be present in the chain embedded in the OCSP | |
| 1755 response. So we add it if necessary. */ | |
| 1756 | |
| 1757 /* First make sure the peer cert chain includes both a peer and an issuer, | |
| 1758 and the OCSP response contains a responder cert. */ | |
| 1759 if(sk_X509_num(ch) >= 2 && sk_X509_num(br->certs) >= 1) { | |
| 1760 X509 *responder = sk_X509_value(br->certs, sk_X509_num(br->certs) - 1); | |
| 1761 | |
| 1762 /* Find issuer of responder cert and add it to the OCSP response chain */ | |
| 1763 for(i = 0; i < sk_X509_num(ch); i++) { | |
| 1764 X509 *issuer = sk_X509_value(ch, i); | |
| 1765 if(X509_check_issued(issuer, responder) == X509_V_OK) { | |
| 1766 if(!OCSP_basic_add1_cert(br, issuer)) { | |
| 1767 failf(data, "Could not add issuer cert to OCSP response"); | |
| 1768 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1769 goto end; | |
| 1770 } | |
| 1771 } | |
| 1772 } | |
| 1773 } | |
| 1774 #endif | |
| 1775 | |
| 1776 if(OCSP_basic_verify(br, ch, st, 0) <= 0) { | |
| 1777 failf(data, "OCSP response verification failed"); | |
| 1778 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1779 goto end; | |
| 1780 } | |
| 1781 | |
| 1782 for(i = 0; i < OCSP_resp_count(br); i++) { | |
| 1783 int cert_status, crl_reason; | |
| 1784 OCSP_SINGLERESP *single = NULL; | |
| 1785 | |
| 1786 ASN1_GENERALIZEDTIME *rev, *thisupd, *nextupd; | |
| 1787 | |
| 1788 single = OCSP_resp_get0(br, i); | |
| 1789 if(!single) | |
| 1790 continue; | |
| 1791 | |
| 1792 cert_status = OCSP_single_get0_status(single, &crl_reason, &rev, | |
| 1793 &thisupd, &nextupd); | |
| 1794 | |
| 1795 if(!OCSP_check_validity(thisupd, nextupd, 300L, -1L)) { | |
| 1796 failf(data, "OCSP response has expired"); | |
| 1797 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1798 goto end; | |
| 1799 } | |
| 1800 | |
| 1801 infof(data, "SSL certificate status: %s (%d)\n", | |
| 1802 OCSP_cert_status_str(cert_status), cert_status); | |
| 1803 | |
| 1804 switch(cert_status) { | |
| 1805 case V_OCSP_CERTSTATUS_GOOD: | |
| 1806 break; | |
| 1807 | |
| 1808 case V_OCSP_CERTSTATUS_REVOKED: | |
| 1809 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1810 | |
| 1811 failf(data, "SSL certificate revocation reason: %s (%d)", | |
| 1812 OCSP_crl_reason_str(crl_reason), crl_reason); | |
| 1813 goto end; | |
| 1814 | |
| 1815 case V_OCSP_CERTSTATUS_UNKNOWN: | |
| 1816 result = CURLE_SSL_INVALIDCERTSTATUS; | |
| 1817 goto end; | |
| 1818 } | |
| 1819 } | |
| 1820 | |
| 1821 end: | |
| 1822 if(br) OCSP_BASICRESP_free(br); | |
| 1823 OCSP_RESPONSE_free(rsp); | |
| 1824 | |
| 1825 return result; | |
| 1826 } | |
| 1827 #endif | |
| 1828 | |
| 1829 #endif /* USE_OPENSSL */ | |
| 1830 | |
| 1831 /* The SSL_CTRL_SET_MSG_CALLBACK doesn't exist in ancient OpenSSL versions | |
| 1832 and thus this cannot be done there. */ | |
| 1833 #ifdef SSL_CTRL_SET_MSG_CALLBACK | |
| 1834 | |
| 1835 static const char *ssl_msg_type(int ssl_ver, int msg) | |
| 1836 { | |
| 1837 #ifdef SSL2_VERSION_MAJOR | |
| 1838 if(ssl_ver == SSL2_VERSION_MAJOR) { | |
| 1839 switch(msg) { | |
| 1840 case SSL2_MT_ERROR: | |
| 1841 return "Error"; | |
| 1842 case SSL2_MT_CLIENT_HELLO: | |
| 1843 return "Client hello"; | |
| 1844 case SSL2_MT_CLIENT_MASTER_KEY: | |
| 1845 return "Client key"; | |
| 1846 case SSL2_MT_CLIENT_FINISHED: | |
| 1847 return "Client finished"; | |
| 1848 case SSL2_MT_SERVER_HELLO: | |
| 1849 return "Server hello"; | |
| 1850 case SSL2_MT_SERVER_VERIFY: | |
| 1851 return "Server verify"; | |
| 1852 case SSL2_MT_SERVER_FINISHED: | |
| 1853 return "Server finished"; | |
| 1854 case SSL2_MT_REQUEST_CERTIFICATE: | |
| 1855 return "Request CERT"; | |
| 1856 case SSL2_MT_CLIENT_CERTIFICATE: | |
| 1857 return "Client CERT"; | |
| 1858 } | |
| 1859 } | |
| 1860 else | |
| 1861 #endif | |
| 1862 if(ssl_ver == SSL3_VERSION_MAJOR) { | |
| 1863 switch(msg) { | |
| 1864 case SSL3_MT_HELLO_REQUEST: | |
| 1865 return "Hello request"; | |
| 1866 case SSL3_MT_CLIENT_HELLO: | |
| 1867 return "Client hello"; | |
| 1868 case SSL3_MT_SERVER_HELLO: | |
| 1869 return "Server hello"; | |
| 1870 #ifdef SSL3_MT_NEWSESSION_TICKET | |
| 1871 case SSL3_MT_NEWSESSION_TICKET: | |
| 1872 return "Newsession Ticket"; | |
| 1873 #endif | |
| 1874 case SSL3_MT_CERTIFICATE: | |
| 1875 return "Certificate"; | |
| 1876 case SSL3_MT_SERVER_KEY_EXCHANGE: | |
| 1877 return "Server key exchange"; | |
| 1878 case SSL3_MT_CLIENT_KEY_EXCHANGE: | |
| 1879 return "Client key exchange"; | |
| 1880 case SSL3_MT_CERTIFICATE_REQUEST: | |
| 1881 return "Request CERT"; | |
| 1882 case SSL3_MT_SERVER_DONE: | |
| 1883 return "Server finished"; | |
| 1884 case SSL3_MT_CERTIFICATE_VERIFY: | |
| 1885 return "CERT verify"; | |
| 1886 case SSL3_MT_FINISHED: | |
| 1887 return "Finished"; | |
| 1888 #ifdef SSL3_MT_CERTIFICATE_STATUS | |
| 1889 case SSL3_MT_CERTIFICATE_STATUS: | |
| 1890 return "Certificate Status"; | |
| 1891 #endif | |
| 1892 #ifdef SSL3_MT_ENCRYPTED_EXTENSIONS | |
| 1893 case SSL3_MT_ENCRYPTED_EXTENSIONS: | |
| 1894 return "Encrypted Extensions"; | |
| 1895 #endif | |
| 1896 #ifdef SSL3_MT_END_OF_EARLY_DATA | |
| 1897 case SSL3_MT_END_OF_EARLY_DATA: | |
| 1898 return "End of early data"; | |
| 1899 #endif | |
| 1900 #ifdef SSL3_MT_KEY_UPDATE | |
| 1901 case SSL3_MT_KEY_UPDATE: | |
| 1902 return "Key update"; | |
| 1903 #endif | |
| 1904 #ifdef SSL3_MT_NEXT_PROTO | |
| 1905 case SSL3_MT_NEXT_PROTO: | |
| 1906 return "Next protocol"; | |
| 1907 #endif | |
| 1908 #ifdef SSL3_MT_MESSAGE_HASH | |
| 1909 case SSL3_MT_MESSAGE_HASH: | |
| 1910 return "Message hash"; | |
| 1911 #endif | |
| 1912 } | |
| 1913 } | |
| 1914 return "Unknown"; | |
| 1915 } | |
| 1916 | |
| 1917 static const char *tls_rt_type(int type) | |
| 1918 { | |
| 1919 switch(type) { | |
| 1920 #ifdef SSL3_RT_HEADER | |
| 1921 case SSL3_RT_HEADER: | |
| 1922 return "TLS header"; | |
| 1923 #endif | |
| 1924 case SSL3_RT_CHANGE_CIPHER_SPEC: | |
| 1925 return "TLS change cipher"; | |
| 1926 case SSL3_RT_ALERT: | |
| 1927 return "TLS alert"; | |
| 1928 case SSL3_RT_HANDSHAKE: | |
| 1929 return "TLS handshake"; | |
| 1930 case SSL3_RT_APPLICATION_DATA: | |
| 1931 return "TLS app data"; | |
| 1932 default: | |
| 1933 return "TLS Unknown"; | |
| 1934 } | |
| 1935 } | |
| 1936 | |
| 1937 | |
| 1938 /* | |
| 1939 * Our callback from the SSL/TLS layers. | |
| 1940 */ | |
| 1941 static void ssl_tls_trace(int direction, int ssl_ver, int content_type, | |
| 1942 const void *buf, size_t len, SSL *ssl, | |
| 1943 void *userp) | |
| 1944 { | |
| 1945 struct Curl_easy *data; | |
| 1946 char unknown[32]; | |
| 1947 const char *verstr = NULL; | |
| 1948 struct connectdata *conn = userp; | |
| 1949 | |
| 1950 if(!conn || !conn->data || !conn->data->set.fdebug || | |
| 1951 (direction != 0 && direction != 1)) | |
| 1952 return; | |
| 1953 | |
| 1954 data = conn->data; | |
| 1955 | |
| 1956 switch(ssl_ver) { | |
| 1957 #ifdef SSL2_VERSION /* removed in recent versions */ | |
| 1958 case SSL2_VERSION: | |
| 1959 verstr = "SSLv2"; | |
| 1960 break; | |
| 1961 #endif | |
| 1962 #ifdef SSL3_VERSION | |
| 1963 case SSL3_VERSION: | |
| 1964 verstr = "SSLv3"; | |
| 1965 break; | |
| 1966 #endif | |
| 1967 case TLS1_VERSION: | |
| 1968 verstr = "TLSv1.0"; | |
| 1969 break; | |
| 1970 #ifdef TLS1_1_VERSION | |
| 1971 case TLS1_1_VERSION: | |
| 1972 verstr = "TLSv1.1"; | |
| 1973 break; | |
| 1974 #endif | |
| 1975 #ifdef TLS1_2_VERSION | |
| 1976 case TLS1_2_VERSION: | |
| 1977 verstr = "TLSv1.2"; | |
| 1978 break; | |
| 1979 #endif | |
| 1980 #ifdef TLS1_3_VERSION | |
| 1981 case TLS1_3_VERSION: | |
| 1982 verstr = "TLSv1.3"; | |
| 1983 break; | |
| 1984 #endif | |
| 1985 case 0: | |
| 1986 break; | |
| 1987 default: | |
| 1988 msnprintf(unknown, sizeof(unknown), "(%x)", ssl_ver); | |
| 1989 verstr = unknown; | |
| 1990 break; | |
| 1991 } | |
| 1992 | |
| 1993 /* Log progress for interesting records only (like Handshake or Alert), skip | |
| 1994 * all raw record headers (content_type == SSL3_RT_HEADER or ssl_ver == 0). | |
| 1995 * For TLS 1.3, skip notification of the decrypted inner Content Type. | |
| 1996 */ | |
| 1997 if(ssl_ver | |
| 1998 #ifdef SSL3_RT_INNER_CONTENT_TYPE | |
| 1999 && content_type != SSL3_RT_INNER_CONTENT_TYPE | |
| 2000 #endif | |
| 2001 ) { | |
| 2002 const char *msg_name, *tls_rt_name; | |
| 2003 char ssl_buf[1024]; | |
| 2004 int msg_type, txt_len; | |
| 2005 | |
| 2006 /* the info given when the version is zero is not that useful for us */ | |
| 2007 | |
| 2008 ssl_ver >>= 8; /* check the upper 8 bits only below */ | |
| 2009 | |
| 2010 /* SSLv2 doesn't seem to have TLS record-type headers, so OpenSSL | |
| 2011 * always pass-up content-type as 0. But the interesting message-type | |
| 2012 * is at 'buf[0]'. | |
| 2013 */ | |
| 2014 if(ssl_ver == SSL3_VERSION_MAJOR && content_type) | |
| 2015 tls_rt_name = tls_rt_type(content_type); | |
| 2016 else | |
| 2017 tls_rt_name = ""; | |
| 2018 | |
| 2019 if(content_type == SSL3_RT_CHANGE_CIPHER_SPEC) { | |
| 2020 msg_type = *(char *)buf; | |
| 2021 msg_name = "Change cipher spec"; | |
| 2022 } | |
| 2023 else if(content_type == SSL3_RT_ALERT) { | |
| 2024 msg_type = (((char *)buf)[0] << 8) + ((char *)buf)[1]; | |
| 2025 msg_name = SSL_alert_desc_string_long(msg_type); | |
| 2026 } | |
| 2027 else { | |
| 2028 msg_type = *(char *)buf; | |
| 2029 msg_name = ssl_msg_type(ssl_ver, msg_type); | |
| 2030 } | |
| 2031 | |
| 2032 txt_len = msnprintf(ssl_buf, sizeof(ssl_buf), "%s (%s), %s, %s (%d):\n", | |
| 2033 verstr, direction?"OUT":"IN", | |
| 2034 tls_rt_name, msg_name, msg_type); | |
| 2035 if(0 <= txt_len && (unsigned)txt_len < sizeof(ssl_buf)) { | |
| 2036 Curl_debug(data, CURLINFO_TEXT, ssl_buf, (size_t)txt_len); | |
| 2037 } | |
| 2038 } | |
| 2039 | |
| 2040 Curl_debug(data, (direction == 1) ? CURLINFO_SSL_DATA_OUT : | |
| 2041 CURLINFO_SSL_DATA_IN, (char *)buf, len); | |
| 2042 (void) ssl; | |
| 2043 } | |
| 2044 #endif | |
| 2045 | |
| 2046 #ifdef USE_OPENSSL | |
| 2047 /* ====================================================== */ | |
| 2048 | |
| 2049 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME | |
| 2050 # define use_sni(x) sni = (x) | |
| 2051 #else | |
| 2052 # define use_sni(x) Curl_nop_stmt | |
| 2053 #endif | |
| 2054 | |
| 2055 /* Check for OpenSSL 1.0.2 which has ALPN support. */ | |
| 2056 #undef HAS_ALPN | |
| 2057 #if OPENSSL_VERSION_NUMBER >= 0x10002000L \ | |
| 2058 && !defined(OPENSSL_NO_TLSEXT) | |
| 2059 # define HAS_ALPN 1 | |
| 2060 #endif | |
| 2061 | |
| 2062 /* Check for OpenSSL 1.0.1 which has NPN support. */ | |
| 2063 #undef HAS_NPN | |
| 2064 #if OPENSSL_VERSION_NUMBER >= 0x10001000L \ | |
| 2065 && !defined(OPENSSL_NO_TLSEXT) \ | |
| 2066 && !defined(OPENSSL_NO_NEXTPROTONEG) | |
| 2067 # define HAS_NPN 1 | |
| 2068 #endif | |
| 2069 | |
| 2070 #ifdef HAS_NPN | |
| 2071 | |
| 2072 /* | |
| 2073 * in is a list of length prefixed strings. this function has to select | |
| 2074 * the protocol we want to use from the list and write its string into out. | |
| 2075 */ | |
| 2076 | |
| 2077 static int | |
| 2078 select_next_protocol(unsigned char **out, unsigned char *outlen, | |
| 2079 const unsigned char *in, unsigned int inlen, | |
| 2080 const char *key, unsigned int keylen) | |
| 2081 { | |
| 2082 unsigned int i; | |
| 2083 for(i = 0; i + keylen <= inlen; i += in[i] + 1) { | |
| 2084 if(memcmp(&in[i + 1], key, keylen) == 0) { | |
| 2085 *out = (unsigned char *) &in[i + 1]; | |
| 2086 *outlen = in[i]; | |
| 2087 return 0; | |
| 2088 } | |
| 2089 } | |
| 2090 return -1; | |
| 2091 } | |
| 2092 | |
| 2093 static int | |
| 2094 select_next_proto_cb(SSL *ssl, | |
| 2095 unsigned char **out, unsigned char *outlen, | |
| 2096 const unsigned char *in, unsigned int inlen, | |
| 2097 void *arg) | |
| 2098 { | |
| 2099 struct connectdata *conn = (struct connectdata*) arg; | |
| 2100 | |
| 2101 (void)ssl; | |
| 2102 | |
| 2103 #ifdef USE_NGHTTP2 | |
| 2104 if(conn->data->set.httpversion >= CURL_HTTP_VERSION_2 && | |
| 2105 !select_next_protocol(out, outlen, in, inlen, NGHTTP2_PROTO_VERSION_ID, | |
| 2106 NGHTTP2_PROTO_VERSION_ID_LEN)) { | |
| 2107 infof(conn->data, "NPN, negotiated HTTP2 (%s)\n", | |
| 2108 NGHTTP2_PROTO_VERSION_ID); | |
| 2109 conn->negnpn = CURL_HTTP_VERSION_2; | |
| 2110 return SSL_TLSEXT_ERR_OK; | |
| 2111 } | |
| 2112 #endif | |
| 2113 | |
| 2114 if(!select_next_protocol(out, outlen, in, inlen, ALPN_HTTP_1_1, | |
| 2115 ALPN_HTTP_1_1_LENGTH)) { | |
| 2116 infof(conn->data, "NPN, negotiated HTTP1.1\n"); | |
| 2117 conn->negnpn = CURL_HTTP_VERSION_1_1; | |
| 2118 return SSL_TLSEXT_ERR_OK; | |
| 2119 } | |
| 2120 | |
| 2121 infof(conn->data, "NPN, no overlap, use HTTP1.1\n"); | |
| 2122 *out = (unsigned char *)ALPN_HTTP_1_1; | |
| 2123 *outlen = ALPN_HTTP_1_1_LENGTH; | |
| 2124 conn->negnpn = CURL_HTTP_VERSION_1_1; | |
| 2125 | |
| 2126 return SSL_TLSEXT_ERR_OK; | |
| 2127 } | |
| 2128 #endif /* HAS_NPN */ | |
| 2129 | |
| 2130 #ifndef CURL_DISABLE_VERBOSE_STRINGS | |
| 2131 static const char * | |
| 2132 get_ssl_version_txt(SSL *ssl) | |
| 2133 { | |
| 2134 if(!ssl) | |
| 2135 return ""; | |
| 2136 | |
| 2137 switch(SSL_version(ssl)) { | |
| 2138 #ifdef TLS1_3_VERSION | |
| 2139 case TLS1_3_VERSION: | |
| 2140 return "TLSv1.3"; | |
| 2141 #endif | |
| 2142 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL | |
| 2143 case TLS1_2_VERSION: | |
| 2144 return "TLSv1.2"; | |
| 2145 case TLS1_1_VERSION: | |
| 2146 return "TLSv1.1"; | |
| 2147 #endif | |
| 2148 case TLS1_VERSION: | |
| 2149 return "TLSv1.0"; | |
| 2150 case SSL3_VERSION: | |
| 2151 return "SSLv3"; | |
| 2152 case SSL2_VERSION: | |
| 2153 return "SSLv2"; | |
| 2154 } | |
| 2155 return "unknown"; | |
| 2156 } | |
| 2157 #endif | |
| 2158 | |
| 2159 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) /* 1.1.0 */ | |
| 2160 static CURLcode | |
| 2161 set_ssl_version_min_max(SSL_CTX *ctx, struct connectdata *conn) | |
| 2162 { | |
| 2163 /* first, TLS min version... */ | |
| 2164 long curl_ssl_version_min = SSL_CONN_CONFIG(version); | |
| 2165 long curl_ssl_version_max; | |
| 2166 | |
| 2167 /* convert cURL min SSL version option to OpenSSL constant */ | |
| 2168 long ossl_ssl_version_min = 0; | |
| 2169 long ossl_ssl_version_max = 0; | |
| 2170 switch(curl_ssl_version_min) { | |
| 2171 case CURL_SSLVERSION_TLSv1: /* TLS 1.x */ | |
| 2172 case CURL_SSLVERSION_TLSv1_0: | |
| 2173 ossl_ssl_version_min = TLS1_VERSION; | |
| 2174 break; | |
| 2175 case CURL_SSLVERSION_TLSv1_1: | |
| 2176 ossl_ssl_version_min = TLS1_1_VERSION; | |
| 2177 break; | |
| 2178 case CURL_SSLVERSION_TLSv1_2: | |
| 2179 ossl_ssl_version_min = TLS1_2_VERSION; | |
| 2180 break; | |
| 2181 #ifdef TLS1_3_VERSION | |
| 2182 case CURL_SSLVERSION_TLSv1_3: | |
| 2183 ossl_ssl_version_min = TLS1_3_VERSION; | |
| 2184 break; | |
| 2185 #endif | |
| 2186 } | |
| 2187 | |
| 2188 /* CURL_SSLVERSION_DEFAULT means that no option was selected. | |
| 2189 We don't want to pass 0 to SSL_CTX_set_min_proto_version as | |
| 2190 it would enable all versions down to the lowest supported by | |
| 2191 the library. | |
| 2192 So we skip this, and stay with the OS default | |
| 2193 */ | |
| 2194 if(curl_ssl_version_min != CURL_SSLVERSION_DEFAULT) { | |
| 2195 if(!SSL_CTX_set_min_proto_version(ctx, ossl_ssl_version_min)) { | |
| 2196 return CURLE_SSL_CONNECT_ERROR; | |
| 2197 } | |
| 2198 } | |
| 2199 | |
| 2200 /* ... then, TLS max version */ | |
| 2201 curl_ssl_version_max = SSL_CONN_CONFIG(version_max); | |
| 2202 | |
| 2203 /* convert cURL max SSL version option to OpenSSL constant */ | |
| 2204 ossl_ssl_version_max = 0; | |
| 2205 switch(curl_ssl_version_max) { | |
| 2206 case CURL_SSLVERSION_MAX_TLSv1_0: | |
| 2207 ossl_ssl_version_max = TLS1_VERSION; | |
| 2208 break; | |
| 2209 case CURL_SSLVERSION_MAX_TLSv1_1: | |
| 2210 ossl_ssl_version_max = TLS1_1_VERSION; | |
| 2211 break; | |
| 2212 case CURL_SSLVERSION_MAX_TLSv1_2: | |
| 2213 ossl_ssl_version_max = TLS1_2_VERSION; | |
| 2214 break; | |
| 2215 #ifdef TLS1_3_VERSION | |
| 2216 case CURL_SSLVERSION_MAX_TLSv1_3: | |
| 2217 ossl_ssl_version_max = TLS1_3_VERSION; | |
| 2218 break; | |
| 2219 #endif | |
| 2220 case CURL_SSLVERSION_MAX_NONE: /* none selected */ | |
| 2221 case CURL_SSLVERSION_MAX_DEFAULT: /* max selected */ | |
| 2222 default: | |
| 2223 /* SSL_CTX_set_max_proto_version states that: | |
| 2224 setting the maximum to 0 will enable | |
| 2225 protocol versions up to the highest version | |
| 2226 supported by the library */ | |
| 2227 ossl_ssl_version_max = 0; | |
| 2228 break; | |
| 2229 } | |
| 2230 | |
| 2231 if(!SSL_CTX_set_max_proto_version(ctx, ossl_ssl_version_max)) { | |
| 2232 return CURLE_SSL_CONNECT_ERROR; | |
| 2233 } | |
| 2234 | |
| 2235 return CURLE_OK; | |
| 2236 } | |
| 2237 #endif | |
| 2238 | |
| 2239 #ifdef OPENSSL_IS_BORINGSSL | |
| 2240 typedef uint32_t ctx_option_t; | |
| 2241 #else | |
| 2242 typedef long ctx_option_t; | |
| 2243 #endif | |
| 2244 | |
| 2245 #if (OPENSSL_VERSION_NUMBER < 0x10100000L) /* 1.1.0 */ | |
| 2246 static CURLcode | |
| 2247 set_ssl_version_min_max_legacy(ctx_option_t *ctx_options, | |
| 2248 struct connectdata *conn, int sockindex) | |
| 2249 { | |
| 2250 #if (OPENSSL_VERSION_NUMBER < 0x1000100FL) || !defined(TLS1_3_VERSION) | |
| 2251 /* convoluted #if condition just to avoid compiler warnings on unused | |
| 2252 variable */ | |
| 2253 struct Curl_easy *data = conn->data; | |
| 2254 #endif | |
| 2255 long ssl_version = SSL_CONN_CONFIG(version); | |
| 2256 long ssl_version_max = SSL_CONN_CONFIG(version_max); | |
| 2257 | |
| 2258 switch(ssl_version) { | |
| 2259 case CURL_SSLVERSION_TLSv1_3: | |
| 2260 #ifdef TLS1_3_VERSION | |
| 2261 { | |
| 2262 struct ssl_connect_data *connssl = &conn->ssl[sockindex]; | |
| 2263 SSL_CTX_set_max_proto_version(BACKEND->ctx, TLS1_3_VERSION); | |
| 2264 *ctx_options |= SSL_OP_NO_TLSv1_2; | |
| 2265 } | |
| 2266 #else | |
| 2267 (void)sockindex; | |
| 2268 (void)ctx_options; | |
| 2269 failf(data, OSSL_PACKAGE " was built without TLS 1.3 support"); | |
| 2270 return CURLE_NOT_BUILT_IN; | |
| 2271 #endif | |
| 2272 /* FALLTHROUGH */ | |
| 2273 case CURL_SSLVERSION_TLSv1_2: | |
| 2274 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL | |
| 2275 *ctx_options |= SSL_OP_NO_TLSv1_1; | |
| 2276 #else | |
| 2277 failf(data, OSSL_PACKAGE " was built without TLS 1.2 support"); | |
| 2278 return CURLE_NOT_BUILT_IN; | |
| 2279 #endif | |
| 2280 /* FALLTHROUGH */ | |
| 2281 case CURL_SSLVERSION_TLSv1_1: | |
| 2282 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL | |
| 2283 *ctx_options |= SSL_OP_NO_TLSv1; | |
| 2284 #else | |
| 2285 failf(data, OSSL_PACKAGE " was built without TLS 1.1 support"); | |
| 2286 return CURLE_NOT_BUILT_IN; | |
| 2287 #endif | |
| 2288 /* FALLTHROUGH */ | |
| 2289 case CURL_SSLVERSION_TLSv1_0: | |
| 2290 case CURL_SSLVERSION_TLSv1: | |
| 2291 break; | |
| 2292 } | |
| 2293 | |
| 2294 switch(ssl_version_max) { | |
| 2295 case CURL_SSLVERSION_MAX_TLSv1_0: | |
| 2296 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL | |
| 2297 *ctx_options |= SSL_OP_NO_TLSv1_1; | |
| 2298 #endif | |
| 2299 /* FALLTHROUGH */ | |
| 2300 case CURL_SSLVERSION_MAX_TLSv1_1: | |
| 2301 #if OPENSSL_VERSION_NUMBER >= 0x1000100FL | |
| 2302 *ctx_options |= SSL_OP_NO_TLSv1_2; | |
| 2303 #endif | |
| 2304 /* FALLTHROUGH */ | |
| 2305 case CURL_SSLVERSION_MAX_TLSv1_2: | |
| 2306 #ifdef TLS1_3_VERSION | |
| 2307 *ctx_options |= SSL_OP_NO_TLSv1_3; | |
| 2308 #endif | |
| 2309 break; | |
| 2310 case CURL_SSLVERSION_MAX_TLSv1_3: | |
| 2311 #ifdef TLS1_3_VERSION | |
| 2312 break; | |
| 2313 #else | |
| 2314 failf(data, OSSL_PACKAGE " was built without TLS 1.3 support"); | |
| 2315 return CURLE_NOT_BUILT_IN; | |
| 2316 #endif | |
| 2317 } | |
| 2318 return CURLE_OK; | |
| 2319 } | |
| 2320 #endif | |
| 2321 | |
| 2322 /* The "new session" callback must return zero if the session can be removed | |
| 2323 * or non-zero if the session has been put into the session cache. | |
| 2324 */ | |
| 2325 static int ossl_new_session_cb(SSL *ssl, SSL_SESSION *ssl_sessionid) | |
| 2326 { | |
| 2327 int res = 0; | |
| 2328 struct connectdata *conn; | |
| 2329 struct Curl_easy *data; | |
| 2330 int sockindex; | |
| 2331 curl_socket_t *sockindex_ptr; | |
| 2332 int connectdata_idx = ossl_get_ssl_conn_index(); | |
| 2333 int sockindex_idx = ossl_get_ssl_sockindex_index(); | |
| 2334 | |
| 2335 if(connectdata_idx < 0 || sockindex_idx < 0) | |
| 2336 return 0; | |
| 2337 | |
| 2338 conn = (struct connectdata*) SSL_get_ex_data(ssl, connectdata_idx); | |
| 2339 if(!conn) | |
| 2340 return 0; | |
| 2341 | |
| 2342 data = conn->data; | |
| 2343 | |
| 2344 /* The sockindex has been stored as a pointer to an array element */ | |
| 2345 sockindex_ptr = (curl_socket_t*) SSL_get_ex_data(ssl, sockindex_idx); | |
| 2346 sockindex = (int)(sockindex_ptr - conn->sock); | |
| 2347 | |
| 2348 if(SSL_SET_OPTION(primary.sessionid)) { | |
| 2349 bool incache; | |
| 2350 void *old_ssl_sessionid = NULL; | |
| 2351 | |
| 2352 Curl_ssl_sessionid_lock(conn); | |
| 2353 incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL, | |
| 2354 sockindex)); | |
| 2355 if(incache) { | |
| 2356 if(old_ssl_sessionid != ssl_sessionid) { | |
| 2357 infof(data, "old SSL session ID is stale, removing\n"); | |
| 2358 Curl_ssl_delsessionid(conn, old_ssl_sessionid); | |
| 2359 incache = FALSE; | |
| 2360 } | |
| 2361 } | |
| 2362 | |
| 2363 if(!incache) { | |
| 2364 if(!Curl_ssl_addsessionid(conn, ssl_sessionid, | |
| 2365 0 /* unknown size */, sockindex)) { | |
| 2366 /* the session has been put into the session cache */ | |
| 2367 res = 1; | |
| 2368 } | |
| 2369 else | |
| 2370 failf(data, "failed to store ssl session"); | |
| 2371 } | |
| 2372 Curl_ssl_sessionid_unlock(conn); | |
| 2373 } | |
| 2374 | |
| 2375 return res; | |
| 2376 } | |
| 2377 | |
| 2378 static CURLcode ossl_connect_step1(struct connectdata *conn, int sockindex) | |
| 2379 { | |
| 2380 CURLcode result = CURLE_OK; | |
| 2381 char *ciphers; | |
| 2382 struct Curl_easy *data = conn->data; | |
| 2383 SSL_METHOD_QUAL SSL_METHOD *req_method = NULL; | |
| 2384 X509_LOOKUP *lookup = NULL; | |
| 2385 curl_socket_t sockfd = conn->sock[sockindex]; | |
| 2386 struct ssl_connect_data *connssl = &conn->ssl[sockindex]; | |
| 2387 ctx_option_t ctx_options = 0; | |
| 2388 | |
| 2389 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME | |
| 2390 bool sni; | |
| 2391 const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name : | |
| 2392 conn->host.name; | |
| 2393 #ifdef ENABLE_IPV6 | |
| 2394 struct in6_addr addr; | |
| 2395 #else | |
| 2396 struct in_addr addr; | |
| 2397 #endif | |
| 2398 #endif | |
| 2399 long * const certverifyresult = SSL_IS_PROXY() ? | |
| 2400 &data->set.proxy_ssl.certverifyresult : &data->set.ssl.certverifyresult; | |
| 2401 const long int ssl_version = SSL_CONN_CONFIG(version); | |
| 2402 #ifdef USE_TLS_SRP | |
| 2403 const enum CURL_TLSAUTH ssl_authtype = SSL_SET_OPTION(authtype); | |
| 2404 #endif | |
| 2405 char * const ssl_cert = SSL_SET_OPTION(cert); | |
| 2406 const char * const ssl_cert_type = SSL_SET_OPTION(cert_type); | |
| 2407 const char * const ssl_cafile = SSL_CONN_CONFIG(CAfile); | |
| 2408 const char * const ssl_capath = SSL_CONN_CONFIG(CApath); | |
| 2409 const bool verifypeer = SSL_CONN_CONFIG(verifypeer); | |
| 2410 const char * const ssl_crlfile = SSL_SET_OPTION(CRLfile); | |
| 2411 char error_buffer[256]; | |
| 2412 | |
| 2413 DEBUGASSERT(ssl_connect_1 == connssl->connecting_state); | |
| 2414 | |
| 2415 /* Make funny stuff to get random input */ | |
| 2416 result = Curl_ossl_seed(data); | |
| 2417 if(result) | |
| 2418 return result; | |
| 2419 | |
| 2420 *certverifyresult = !X509_V_OK; | |
| 2421 | |
| 2422 /* check to see if we've been told to use an explicit SSL/TLS version */ | |
| 2423 | |
| 2424 switch(ssl_version) { | |
| 2425 case CURL_SSLVERSION_DEFAULT: | |
| 2426 case CURL_SSLVERSION_TLSv1: | |
| 2427 case CURL_SSLVERSION_TLSv1_0: | |
| 2428 case CURL_SSLVERSION_TLSv1_1: | |
| 2429 case CURL_SSLVERSION_TLSv1_2: | |
| 2430 case CURL_SSLVERSION_TLSv1_3: | |
| 2431 /* it will be handled later with the context options */ | |
| 2432 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) | |
| 2433 req_method = TLS_client_method(); | |
| 2434 #else | |
| 2435 req_method = SSLv23_client_method(); | |
| 2436 #endif | |
| 2437 use_sni(TRUE); | |
| 2438 break; | |
| 2439 case CURL_SSLVERSION_SSLv2: | |
| 2440 #ifdef OPENSSL_NO_SSL2 | |
| 2441 failf(data, OSSL_PACKAGE " was built without SSLv2 support"); | |
| 2442 return CURLE_NOT_BUILT_IN; | |
| 2443 #else | |
| 2444 #ifdef USE_TLS_SRP | |
| 2445 if(ssl_authtype == CURL_TLSAUTH_SRP) | |
| 2446 return CURLE_SSL_CONNECT_ERROR; | |
| 2447 #endif | |
| 2448 req_method = SSLv2_client_method(); | |
| 2449 use_sni(FALSE); | |
| 2450 break; | |
| 2451 #endif | |
| 2452 case CURL_SSLVERSION_SSLv3: | |
| 2453 #ifdef OPENSSL_NO_SSL3_METHOD | |
| 2454 failf(data, OSSL_PACKAGE " was built without SSLv3 support"); | |
| 2455 return CURLE_NOT_BUILT_IN; | |
| 2456 #else | |
| 2457 #ifdef USE_TLS_SRP | |
| 2458 if(ssl_authtype == CURL_TLSAUTH_SRP) | |
| 2459 return CURLE_SSL_CONNECT_ERROR; | |
| 2460 #endif | |
| 2461 req_method = SSLv3_client_method(); | |
| 2462 use_sni(FALSE); | |
| 2463 break; | |
| 2464 #endif | |
| 2465 default: | |
| 2466 failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION"); | |
| 2467 return CURLE_SSL_CONNECT_ERROR; | |
| 2468 } | |
| 2469 | |
| 2470 if(BACKEND->ctx) | |
| 2471 SSL_CTX_free(BACKEND->ctx); | |
| 2472 BACKEND->ctx = SSL_CTX_new(req_method); | |
| 2473 | |
| 2474 if(!BACKEND->ctx) { | |
| 2475 failf(data, "SSL: couldn't create a context: %s", | |
| 2476 ossl_strerror(ERR_peek_error(), error_buffer, sizeof(error_buffer))); | |
| 2477 return CURLE_OUT_OF_MEMORY; | |
| 2478 } | |
| 2479 | |
| 2480 #ifdef SSL_MODE_RELEASE_BUFFERS | |
| 2481 SSL_CTX_set_mode(BACKEND->ctx, SSL_MODE_RELEASE_BUFFERS); | |
| 2482 #endif | |
| 2483 | |
| 2484 #ifdef SSL_CTRL_SET_MSG_CALLBACK | |
| 2485 if(data->set.fdebug && data->set.verbose) { | |
| 2486 /* the SSL trace callback is only used for verbose logging */ | |
| 2487 SSL_CTX_set_msg_callback(BACKEND->ctx, ssl_tls_trace); | |
| 2488 SSL_CTX_set_msg_callback_arg(BACKEND->ctx, conn); | |
| 2489 } | |
| 2490 #endif | |
| 2491 | |
| 2492 /* OpenSSL contains code to work-around lots of bugs and flaws in various | |
| 2493 SSL-implementations. SSL_CTX_set_options() is used to enabled those | |
| 2494 work-arounds. The man page for this option states that SSL_OP_ALL enables | |
| 2495 all the work-arounds and that "It is usually safe to use SSL_OP_ALL to | |
| 2496 enable the bug workaround options if compatibility with somewhat broken | |
| 2497 implementations is desired." | |
| 2498 | |
| 2499 The "-no_ticket" option was introduced in Openssl0.9.8j. It's a flag to | |
| 2500 disable "rfc4507bis session ticket support". rfc4507bis was later turned | |
| 2501 into the proper RFC5077 it seems: https://tools.ietf.org/html/rfc5077 | |
| 2502 | |
| 2503 The enabled extension concerns the session management. I wonder how often | |
| 2504 libcurl stops a connection and then resumes a TLS session. also, sending | |
| 2505 the session data is some overhead. .I suggest that you just use your | |
| 2506 proposed patch (which explicitly disables TICKET). | |
| 2507 | |
| 2508 If someone writes an application with libcurl and openssl who wants to | |
| 2509 enable the feature, one can do this in the SSL callback. | |
| 2510 | |
| 2511 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG option enabling allowed proper | |
| 2512 interoperability with web server Netscape Enterprise Server 2.0.1 which | |
| 2513 was released back in 1996. | |
| 2514 | |
| 2515 Due to CVE-2010-4180, option SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG has | |
| 2516 become ineffective as of OpenSSL 0.9.8q and 1.0.0c. In order to mitigate | |
| 2517 CVE-2010-4180 when using previous OpenSSL versions we no longer enable | |
| 2518 this option regardless of OpenSSL version and SSL_OP_ALL definition. | |
| 2519 | |
| 2520 OpenSSL added a work-around for a SSL 3.0/TLS 1.0 CBC vulnerability | |
| 2521 (https://www.openssl.org/~bodo/tls-cbc.txt). In 0.9.6e they added a bit to | |
| 2522 SSL_OP_ALL that _disables_ that work-around despite the fact that | |
| 2523 SSL_OP_ALL is documented to do "rather harmless" workarounds. In order to | |
| 2524 keep the secure work-around, the SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS bit | |
| 2525 must not be set. | |
| 2526 */ | |
| 2527 | |
| 2528 ctx_options = SSL_OP_ALL; | |
| 2529 | |
| 2530 #ifdef SSL_OP_NO_TICKET | |
| 2531 ctx_options |= SSL_OP_NO_TICKET; | |
| 2532 #endif | |
| 2533 | |
| 2534 #ifdef SSL_OP_NO_COMPRESSION | |
| 2535 ctx_options |= SSL_OP_NO_COMPRESSION; | |
| 2536 #endif | |
| 2537 | |
| 2538 #ifdef SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG | |
| 2539 /* mitigate CVE-2010-4180 */ | |
| 2540 ctx_options &= ~SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG; | |
| 2541 #endif | |
| 2542 | |
| 2543 #ifdef SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS | |
| 2544 /* unless the user explicitly ask to allow the protocol vulnerability we | |
| 2545 use the work-around */ | |
| 2546 if(!SSL_SET_OPTION(enable_beast)) | |
| 2547 ctx_options &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; | |
| 2548 #endif | |
| 2549 | |
| 2550 switch(ssl_version) { | |
| 2551 /* "--sslv2" option means SSLv2 only, disable all others */ | |
| 2552 case CURL_SSLVERSION_SSLv2: | |
| 2553 #if OPENSSL_VERSION_NUMBER >= 0x10100000L /* 1.1.0 */ | |
| 2554 SSL_CTX_set_min_proto_version(BACKEND->ctx, SSL2_VERSION); | |
| 2555 SSL_CTX_set_max_proto_version(BACKEND->ctx, SSL2_VERSION); | |
| 2556 #else | |
| 2557 ctx_options |= SSL_OP_NO_SSLv3; | |
| 2558 ctx_options |= SSL_OP_NO_TLSv1; | |
| 2559 # if OPENSSL_VERSION_NUMBER >= 0x1000100FL | |
| 2560 ctx_options |= SSL_OP_NO_TLSv1_1; | |
| 2561 ctx_options |= SSL_OP_NO_TLSv1_2; | |
| 2562 # ifdef TLS1_3_VERSION | |
| 2563 ctx_options |= SSL_OP_NO_TLSv1_3; | |
| 2564 # endif | |
| 2565 # endif | |
| 2566 #endif | |
| 2567 break; | |
| 2568 | |
| 2569 /* "--sslv3" option means SSLv3 only, disable all others */ | |
| 2570 case CURL_SSLVERSION_SSLv3: | |
| 2571 #if OPENSSL_VERSION_NUMBER >= 0x10100000L /* 1.1.0 */ | |
| 2572 SSL_CTX_set_min_proto_version(BACKEND->ctx, SSL3_VERSION); | |
| 2573 SSL_CTX_set_max_proto_version(BACKEND->ctx, SSL3_VERSION); | |
| 2574 #else | |
| 2575 ctx_options |= SSL_OP_NO_SSLv2; | |
| 2576 ctx_options |= SSL_OP_NO_TLSv1; | |
| 2577 # if OPENSSL_VERSION_NUMBER >= 0x1000100FL | |
| 2578 ctx_options |= SSL_OP_NO_TLSv1_1; | |
| 2579 ctx_options |= SSL_OP_NO_TLSv1_2; | |
| 2580 # ifdef TLS1_3_VERSION | |
| 2581 ctx_options |= SSL_OP_NO_TLSv1_3; | |
| 2582 # endif | |
| 2583 # endif | |
| 2584 #endif | |
| 2585 break; | |
| 2586 | |
| 2587 /* "--tlsv<x.y>" options mean TLS >= version <x.y> */ | |
| 2588 case CURL_SSLVERSION_DEFAULT: | |
| 2589 case CURL_SSLVERSION_TLSv1: /* TLS >= version 1.0 */ | |
| 2590 case CURL_SSLVERSION_TLSv1_0: /* TLS >= version 1.0 */ | |
| 2591 case CURL_SSLVERSION_TLSv1_1: /* TLS >= version 1.1 */ | |
| 2592 case CURL_SSLVERSION_TLSv1_2: /* TLS >= version 1.2 */ | |
| 2593 case CURL_SSLVERSION_TLSv1_3: /* TLS >= version 1.3 */ | |
| 2594 /* asking for any TLS version as the minimum, means no SSL versions | |
| 2595 allowed */ | |
| 2596 ctx_options |= SSL_OP_NO_SSLv2; | |
| 2597 ctx_options |= SSL_OP_NO_SSLv3; | |
| 2598 | |
| 2599 #if (OPENSSL_VERSION_NUMBER >= 0x10100000L) /* 1.1.0 */ | |
| 2600 result = set_ssl_version_min_max(BACKEND->ctx, conn); | |
| 2601 #else | |
| 2602 result = set_ssl_version_min_max_legacy(&ctx_options, conn, sockindex); | |
| 2603 #endif | |
| 2604 if(result != CURLE_OK) | |
| 2605 return result; | |
| 2606 break; | |
| 2607 | |
| 2608 default: | |
| 2609 failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION"); | |
| 2610 return CURLE_SSL_CONNECT_ERROR; | |
| 2611 } | |
| 2612 | |
| 2613 SSL_CTX_set_options(BACKEND->ctx, ctx_options); | |
| 2614 | |
| 2615 #ifdef HAS_NPN | |
| 2616 if(conn->bits.tls_enable_npn) | |
| 2617 SSL_CTX_set_next_proto_select_cb(BACKEND->ctx, select_next_proto_cb, conn); | |
| 2618 #endif | |
| 2619 | |
| 2620 #ifdef HAS_ALPN | |
| 2621 if(conn->bits.tls_enable_alpn) { | |
| 2622 int cur = 0; | |
| 2623 unsigned char protocols[128]; | |
| 2624 | |
| 2625 #ifdef USE_NGHTTP2 | |
| 2626 if(data->set.httpversion >= CURL_HTTP_VERSION_2 && | |
| 2627 (!SSL_IS_PROXY() || !conn->bits.tunnel_proxy)) { | |
| 2628 protocols[cur++] = NGHTTP2_PROTO_VERSION_ID_LEN; | |
| 2629 | |
| 2630 memcpy(&protocols[cur], NGHTTP2_PROTO_VERSION_ID, | |
| 2631 NGHTTP2_PROTO_VERSION_ID_LEN); | |
| 2632 cur += NGHTTP2_PROTO_VERSION_ID_LEN; | |
| 2633 infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID); | |
| 2634 } | |
| 2635 #endif | |
| 2636 | |
| 2637 protocols[cur++] = ALPN_HTTP_1_1_LENGTH; | |
| 2638 memcpy(&protocols[cur], ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH); | |
| 2639 cur += ALPN_HTTP_1_1_LENGTH; | |
| 2640 infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1); | |
| 2641 | |
| 2642 /* expects length prefixed preference ordered list of protocols in wire | |
| 2643 * format | |
| 2644 */ | |
| 2645 SSL_CTX_set_alpn_protos(BACKEND->ctx, protocols, cur); | |
| 2646 } | |
| 2647 #endif | |
| 2648 | |
| 2649 if(ssl_cert || ssl_cert_type) { | |
| 2650 if(!cert_stuff(conn, BACKEND->ctx, ssl_cert, ssl_cert_type, | |
| 2651 SSL_SET_OPTION(key), SSL_SET_OPTION(key_type), | |
| 2652 SSL_SET_OPTION(key_passwd))) { | |
| 2653 /* failf() is already done in cert_stuff() */ | |
| 2654 return CURLE_SSL_CERTPROBLEM; | |
| 2655 } | |
| 2656 } | |
| 2657 | |
| 2658 ciphers = SSL_CONN_CONFIG(cipher_list); | |
| 2659 if(!ciphers) | |
| 2660 ciphers = (char *)DEFAULT_CIPHER_SELECTION; | |
| 2661 if(ciphers) { | |
| 2662 if(!SSL_CTX_set_cipher_list(BACKEND->ctx, ciphers)) { | |
| 2663 failf(data, "failed setting cipher list: %s", ciphers); | |
| 2664 return CURLE_SSL_CIPHER; | |
| 2665 } | |
| 2666 infof(data, "Cipher selection: %s\n", ciphers); | |
| 2667 } | |
| 2668 | |
| 2669 #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES | |
| 2670 { | |
| 2671 char *ciphers13 = SSL_CONN_CONFIG(cipher_list13); | |
| 2672 if(ciphers13) { | |
| 2673 if(!SSL_CTX_set_ciphersuites(BACKEND->ctx, ciphers13)) { | |
| 2674 failf(data, "failed setting TLS 1.3 cipher suite: %s", ciphers13); | |
| 2675 return CURLE_SSL_CIPHER; | |
| 2676 } | |
| 2677 infof(data, "TLS 1.3 cipher selection: %s\n", ciphers13); | |
| 2678 } | |
| 2679 } | |
| 2680 #endif | |
| 2681 | |
| 2682 #ifdef HAVE_SSL_CTX_SET_POST_HANDSHAKE_AUTH | |
| 2683 /* OpenSSL 1.1.1 requires clients to opt-in for PHA */ | |
| 2684 SSL_CTX_set_post_handshake_auth(BACKEND->ctx, 1); | |
| 2685 #endif | |
| 2686 | |
| 2687 #ifdef USE_TLS_SRP | |
| 2688 if(ssl_authtype == CURL_TLSAUTH_SRP) { | |
| 2689 char * const ssl_username = SSL_SET_OPTION(username); | |
| 2690 | |
| 2691 infof(data, "Using TLS-SRP username: %s\n", ssl_username); | |
| 2692 | |
| 2693 if(!SSL_CTX_set_srp_username(BACKEND->ctx, ssl_username)) { | |
| 2694 failf(data, "Unable to set SRP user name"); | |
| 2695 return CURLE_BAD_FUNCTION_ARGUMENT; | |
| 2696 } | |
| 2697 if(!SSL_CTX_set_srp_password(BACKEND->ctx, SSL_SET_OPTION(password))) { | |
| 2698 failf(data, "failed setting SRP password"); | |
| 2699 return CURLE_BAD_FUNCTION_ARGUMENT; | |
| 2700 } | |
| 2701 if(!SSL_CONN_CONFIG(cipher_list)) { | |
| 2702 infof(data, "Setting cipher list SRP\n"); | |
| 2703 | |
| 2704 if(!SSL_CTX_set_cipher_list(BACKEND->ctx, "SRP")) { | |
| 2705 failf(data, "failed setting SRP cipher list"); | |
| 2706 return CURLE_SSL_CIPHER; | |
| 2707 } | |
| 2708 } | |
| 2709 } | |
| 2710 #endif | |
| 2711 | |
| 2712 if(ssl_cafile || ssl_capath) { | |
| 2713 /* tell SSL where to find CA certificates that are used to verify | |
| 2714 the servers certificate. */ | |
| 2715 if(!SSL_CTX_load_verify_locations(BACKEND->ctx, ssl_cafile, ssl_capath)) { | |
| 2716 if(verifypeer) { | |
| 2717 /* Fail if we insist on successfully verifying the server. */ | |
| 2718 failf(data, "error setting certificate verify locations:\n" | |
| 2719 " CAfile: %s\n CApath: %s", | |
| 2720 ssl_cafile ? ssl_cafile : "none", | |
| 2721 ssl_capath ? ssl_capath : "none"); | |
| 2722 return CURLE_SSL_CACERT_BADFILE; | |
| 2723 } | |
| 2724 /* Just continue with a warning if no strict certificate verification | |
| 2725 is required. */ | |
| 2726 infof(data, "error setting certificate verify locations," | |
| 2727 " continuing anyway:\n"); | |
| 2728 } | |
| 2729 else { | |
| 2730 /* Everything is fine. */ | |
| 2731 infof(data, "successfully set certificate verify locations:\n"); | |
| 2732 } | |
| 2733 infof(data, | |
| 2734 " CAfile: %s\n" | |
| 2735 " CApath: %s\n", | |
| 2736 ssl_cafile ? ssl_cafile : "none", | |
| 2737 ssl_capath ? ssl_capath : "none"); | |
| 2738 } | |
| 2739 #ifdef CURL_CA_FALLBACK | |
| 2740 else if(verifypeer) { | |
| 2741 /* verifying the peer without any CA certificates won't | |
| 2742 work so use openssl's built in default as fallback */ | |
| 2743 SSL_CTX_set_default_verify_paths(BACKEND->ctx); | |
| 2744 } | |
| 2745 #endif | |
| 2746 | |
| 2747 if(ssl_crlfile) { | |
| 2748 /* tell SSL where to find CRL file that is used to check certificate | |
| 2749 * revocation */ | |
| 2750 lookup = X509_STORE_add_lookup(SSL_CTX_get_cert_store(BACKEND->ctx), | |
| 2751 X509_LOOKUP_file()); | |
| 2752 if(!lookup || | |
| 2753 (!X509_load_crl_file(lookup, ssl_crlfile, X509_FILETYPE_PEM)) ) { | |
| 2754 failf(data, "error loading CRL file: %s", ssl_crlfile); | |
| 2755 return CURLE_SSL_CRL_BADFILE; | |
| 2756 } | |
| 2757 /* Everything is fine. */ | |
| 2758 infof(data, "successfully load CRL file:\n"); | |
| 2759 X509_STORE_set_flags(SSL_CTX_get_cert_store(BACKEND->ctx), | |
| 2760 X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); | |
| 2761 | |
| 2762 infof(data, " CRLfile: %s\n", ssl_crlfile); | |
| 2763 } | |
| 2764 | |
| 2765 /* Try building a chain using issuers in the trusted store first to avoid | |
| 2766 problems with server-sent legacy intermediates. Newer versions of | |
| 2767 OpenSSL do alternate chain checking by default which gives us the same | |
| 2768 fix without as much of a performance hit (slight), so we prefer that if | |
| 2769 available. | |
| 2770 https://rt.openssl.org/Ticket/Display.html?id=3621&user=guest&pass=guest | |
| 2771 */ | |
| 2772 #if defined(X509_V_FLAG_TRUSTED_FIRST) && !defined(X509_V_FLAG_NO_ALT_CHAINS) | |
| 2773 if(verifypeer) { | |
| 2774 X509_STORE_set_flags(SSL_CTX_get_cert_store(BACKEND->ctx), | |
| 2775 X509_V_FLAG_TRUSTED_FIRST); | |
| 2776 } | |
| 2777 #endif | |
| 2778 | |
| 2779 /* SSL always tries to verify the peer, this only says whether it should | |
| 2780 * fail to connect if the verification fails, or if it should continue | |
| 2781 * anyway. In the latter case the result of the verification is checked with | |
| 2782 * SSL_get_verify_result() below. */ | |
| 2783 SSL_CTX_set_verify(BACKEND->ctx, | |
| 2784 verifypeer ? SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL); | |
| 2785 | |
| 2786 /* Enable logging of secrets to the file specified in env SSLKEYLOGFILE. */ | |
| 2787 #if defined(ENABLE_SSLKEYLOGFILE) && defined(HAVE_KEYLOG_CALLBACK) | |
| 2788 if(keylog_file_fp) { | |
| 2789 SSL_CTX_set_keylog_callback(BACKEND->ctx, ossl_keylog_callback); | |
| 2790 } | |
| 2791 #endif | |
| 2792 | |
| 2793 /* Enable the session cache because it's a prerequisite for the "new session" | |
| 2794 * callback. Use the "external storage" mode to avoid that OpenSSL creates | |
| 2795 * an internal session cache. | |
| 2796 */ | |
| 2797 SSL_CTX_set_session_cache_mode(BACKEND->ctx, | |
| 2798 SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL); | |
| 2799 SSL_CTX_sess_set_new_cb(BACKEND->ctx, ossl_new_session_cb); | |
| 2800 | |
| 2801 /* give application a chance to interfere with SSL set up. */ | |
| 2802 if(data->set.ssl.fsslctx) { | |
| 2803 result = (*data->set.ssl.fsslctx)(data, BACKEND->ctx, | |
| 2804 data->set.ssl.fsslctxp); | |
| 2805 if(result) { | |
| 2806 failf(data, "error signaled by ssl ctx callback"); | |
| 2807 return result; | |
| 2808 } | |
| 2809 } | |
| 2810 | |
| 2811 /* Lets make an SSL structure */ | |
| 2812 if(BACKEND->handle) | |
| 2813 SSL_free(BACKEND->handle); | |
| 2814 BACKEND->handle = SSL_new(BACKEND->ctx); | |
| 2815 if(!BACKEND->handle) { | |
| 2816 failf(data, "SSL: couldn't create a context (handle)!"); | |
| 2817 return CURLE_OUT_OF_MEMORY; | |
| 2818 } | |
| 2819 | |
| 2820 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \ | |
| 2821 !defined(OPENSSL_NO_OCSP) | |
| 2822 if(SSL_CONN_CONFIG(verifystatus)) | |
| 2823 SSL_set_tlsext_status_type(BACKEND->handle, TLSEXT_STATUSTYPE_ocsp); | |
| 2824 #endif | |
| 2825 | |
| 2826 #if defined(OPENSSL_IS_BORINGSSL) && defined(ALLOW_RENEG) | |
| 2827 SSL_set_renegotiate_mode(BACKEND->handle, ssl_renegotiate_freely); | |
| 2828 #endif | |
| 2829 | |
| 2830 SSL_set_connect_state(BACKEND->handle); | |
| 2831 | |
| 2832 BACKEND->server_cert = 0x0; | |
| 2833 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME | |
| 2834 if((0 == Curl_inet_pton(AF_INET, hostname, &addr)) && | |
| 2835 #ifdef ENABLE_IPV6 | |
| 2836 (0 == Curl_inet_pton(AF_INET6, hostname, &addr)) && | |
| 2837 #endif | |
| 2838 sni && | |
| 2839 !SSL_set_tlsext_host_name(BACKEND->handle, hostname)) | |
| 2840 infof(data, "WARNING: failed to configure server name indication (SNI) " | |
| 2841 "TLS extension\n"); | |
| 2842 #endif | |
| 2843 | |
| 2844 /* Check if there's a cached ID we can/should use here! */ | |
| 2845 if(SSL_SET_OPTION(primary.sessionid)) { | |
| 2846 void *ssl_sessionid = NULL; | |
| 2847 int connectdata_idx = ossl_get_ssl_conn_index(); | |
| 2848 int sockindex_idx = ossl_get_ssl_sockindex_index(); | |
| 2849 | |
| 2850 if(connectdata_idx >= 0 && sockindex_idx >= 0) { | |
| 2851 /* Store the data needed for the "new session" callback. | |
| 2852 * The sockindex is stored as a pointer to an array element. */ | |
| 2853 SSL_set_ex_data(BACKEND->handle, connectdata_idx, conn); | |
| 2854 SSL_set_ex_data(BACKEND->handle, sockindex_idx, conn->sock + sockindex); | |
| 2855 } | |
| 2856 | |
| 2857 Curl_ssl_sessionid_lock(conn); | |
| 2858 if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL, sockindex)) { | |
| 2859 /* we got a session id, use it! */ | |
| 2860 if(!SSL_set_session(BACKEND->handle, ssl_sessionid)) { | |
| 2861 Curl_ssl_sessionid_unlock(conn); | |
| 2862 failf(data, "SSL: SSL_set_session failed: %s", | |
| 2863 ossl_strerror(ERR_get_error(), error_buffer, | |
| 2864 sizeof(error_buffer))); | |
| 2865 return CURLE_SSL_CONNECT_ERROR; | |
| 2866 } | |
| 2867 /* Informational message */ | |
| 2868 infof(data, "SSL re-using session ID\n"); | |
| 2869 } | |
| 2870 Curl_ssl_sessionid_unlock(conn); | |
| 2871 } | |
| 2872 | |
| 2873 if(conn->proxy_ssl[sockindex].use) { | |
| 2874 BIO *const bio = BIO_new(BIO_f_ssl()); | |
| 2875 SSL *handle = conn->proxy_ssl[sockindex].backend->handle; | |
| 2876 DEBUGASSERT(ssl_connection_complete == conn->proxy_ssl[sockindex].state); | |
| 2877 DEBUGASSERT(handle != NULL); | |
| 2878 DEBUGASSERT(bio != NULL); | |
| 2879 BIO_set_ssl(bio, handle, FALSE); | |
| 2880 SSL_set_bio(BACKEND->handle, bio, bio); | |
| 2881 } | |
| 2882 else if(!SSL_set_fd(BACKEND->handle, (int)sockfd)) { | |
| 2883 /* pass the raw socket into the SSL layers */ | |
| 2884 failf(data, "SSL: SSL_set_fd failed: %s", | |
| 2885 ossl_strerror(ERR_get_error(), error_buffer, sizeof(error_buffer))); | |
| 2886 return CURLE_SSL_CONNECT_ERROR; | |
| 2887 } | |
| 2888 | |
| 2889 connssl->connecting_state = ssl_connect_2; | |
| 2890 | |
| 2891 return CURLE_OK; | |
| 2892 } | |
| 2893 | |
| 2894 static CURLcode ossl_connect_step2(struct connectdata *conn, int sockindex) | |
| 2895 { | |
| 2896 struct Curl_easy *data = conn->data; | |
| 2897 int err; | |
| 2898 struct ssl_connect_data *connssl = &conn->ssl[sockindex]; | |
| 2899 long * const certverifyresult = SSL_IS_PROXY() ? | |
| 2900 &data->set.proxy_ssl.certverifyresult : &data->set.ssl.certverifyresult; | |
| 2901 DEBUGASSERT(ssl_connect_2 == connssl->connecting_state | |
| 2902 || ssl_connect_2_reading == connssl->connecting_state | |
| 2903 || ssl_connect_2_writing == connssl->connecting_state); | |
| 2904 | |
| 2905 ERR_clear_error(); | |
| 2906 | |
| 2907 err = SSL_connect(BACKEND->handle); | |
| 2908 /* If keylogging is enabled but the keylog callback is not supported then log | |
| 2909 secrets here, immediately after SSL_connect by using tap_ssl_key. */ | |
| 2910 #if defined(ENABLE_SSLKEYLOGFILE) && !defined(HAVE_KEYLOG_CALLBACK) | |
| 2911 tap_ssl_key(BACKEND->handle, &BACKEND->tap_state); | |
| 2912 #endif | |
| 2913 | |
| 2914 /* 1 is fine | |
| 2915 0 is "not successful but was shut down controlled" | |
| 2916 <0 is "handshake was not successful, because a fatal error occurred" */ | |
| 2917 if(1 != err) { | |
| 2918 int detail = SSL_get_error(BACKEND->handle, err); | |
| 2919 | |
| 2920 if(SSL_ERROR_WANT_READ == detail) { | |
| 2921 connssl->connecting_state = ssl_connect_2_reading; | |
| 2922 return CURLE_OK; | |
| 2923 } | |
| 2924 if(SSL_ERROR_WANT_WRITE == detail) { | |
| 2925 connssl->connecting_state = ssl_connect_2_writing; | |
| 2926 return CURLE_OK; | |
| 2927 } | |
| 2928 #ifdef SSL_ERROR_WANT_ASYNC | |
| 2929 if(SSL_ERROR_WANT_ASYNC == detail) { | |
| 2930 connssl->connecting_state = ssl_connect_2; | |
| 2931 return CURLE_OK; | |
| 2932 } | |
| 2933 #endif | |
| 2934 else { | |
| 2935 /* untreated error */ | |
| 2936 unsigned long errdetail; | |
| 2937 char error_buffer[256]=""; | |
| 2938 CURLcode result; | |
| 2939 long lerr; | |
| 2940 int lib; | |
| 2941 int reason; | |
| 2942 | |
| 2943 /* the connection failed, we're not waiting for anything else. */ | |
| 2944 connssl->connecting_state = ssl_connect_2; | |
| 2945 | |
| 2946 /* Get the earliest error code from the thread's error queue and removes | |
| 2947 the entry. */ | |
| 2948 errdetail = ERR_get_error(); | |
| 2949 | |
| 2950 /* Extract which lib and reason */ | |
| 2951 lib = ERR_GET_LIB(errdetail); | |
| 2952 reason = ERR_GET_REASON(errdetail); | |
| 2953 | |
| 2954 if((lib == ERR_LIB_SSL) && | |
| 2955 (reason == SSL_R_CERTIFICATE_VERIFY_FAILED)) { | |
| 2956 result = CURLE_PEER_FAILED_VERIFICATION; | |
| 2957 | |
| 2958 lerr = SSL_get_verify_result(BACKEND->handle); | |
| 2959 if(lerr != X509_V_OK) { | |
| 2960 *certverifyresult = lerr; | |
| 2961 msnprintf(error_buffer, sizeof(error_buffer), | |
| 2962 "SSL certificate problem: %s", | |
| 2963 X509_verify_cert_error_string(lerr)); | |
| 2964 } | |
| 2965 else | |
| 2966 /* strcpy() is fine here as long as the string fits within | |
| 2967 error_buffer */ | |
| 2968 strcpy(error_buffer, "SSL certificate verification failed"); | |
| 2969 } | |
| 2970 else { | |
| 2971 result = CURLE_SSL_CONNECT_ERROR; | |
| 2972 ossl_strerror(errdetail, error_buffer, sizeof(error_buffer)); | |
| 2973 } | |
| 2974 | |
| 2975 /* detail is already set to the SSL error above */ | |
| 2976 | |
| 2977 /* If we e.g. use SSLv2 request-method and the server doesn't like us | |
| 2978 * (RST connection etc.), OpenSSL gives no explanation whatsoever and | |
| 2979 * the SO_ERROR is also lost. | |
| 2980 */ | |
| 2981 if(CURLE_SSL_CONNECT_ERROR == result && errdetail == 0) { | |
| 2982 const char * const hostname = SSL_IS_PROXY() ? | |
| 2983 conn->http_proxy.host.name : conn->host.name; | |
| 2984 const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port; | |
| 2985 failf(data, OSSL_PACKAGE " SSL_connect: %s in connection to %s:%ld ", | |
| 2986 SSL_ERROR_to_str(detail), hostname, port); | |
| 2987 return result; | |
| 2988 } | |
| 2989 | |
| 2990 /* Could be a CERT problem */ | |
| 2991 failf(data, "%s", error_buffer); | |
| 2992 | |
| 2993 return result; | |
| 2994 } | |
| 2995 } | |
| 2996 else { | |
| 2997 /* we have been connected fine, we're not waiting for anything else. */ | |
| 2998 connssl->connecting_state = ssl_connect_3; | |
| 2999 | |
| 3000 /* Informational message */ | |
| 3001 infof(data, "SSL connection using %s / %s\n", | |
| 3002 get_ssl_version_txt(BACKEND->handle), | |
| 3003 SSL_get_cipher(BACKEND->handle)); | |
| 3004 | |
| 3005 #ifdef HAS_ALPN | |
| 3006 /* Sets data and len to negotiated protocol, len is 0 if no protocol was | |
| 3007 * negotiated | |
| 3008 */ | |
| 3009 if(conn->bits.tls_enable_alpn) { | |
| 3010 const unsigned char *neg_protocol; | |
| 3011 unsigned int len; | |
| 3012 SSL_get0_alpn_selected(BACKEND->handle, &neg_protocol, &len); | |
| 3013 if(len != 0) { | |
| 3014 infof(data, "ALPN, server accepted to use %.*s\n", len, neg_protocol); | |
| 3015 | |
| 3016 #ifdef USE_NGHTTP2 | |
| 3017 if(len == NGHTTP2_PROTO_VERSION_ID_LEN && | |
| 3018 !memcmp(NGHTTP2_PROTO_VERSION_ID, neg_protocol, len)) { | |
| 3019 conn->negnpn = CURL_HTTP_VERSION_2; | |
| 3020 } | |
| 3021 else | |
| 3022 #endif | |
| 3023 if(len == ALPN_HTTP_1_1_LENGTH && | |
| 3024 !memcmp(ALPN_HTTP_1_1, neg_protocol, ALPN_HTTP_1_1_LENGTH)) { | |
| 3025 conn->negnpn = CURL_HTTP_VERSION_1_1; | |
| 3026 } | |
| 3027 } | |
| 3028 else | |
| 3029 infof(data, "ALPN, server did not agree to a protocol\n"); | |
| 3030 | |
| 3031 Curl_multiuse_state(conn, conn->negnpn == CURL_HTTP_VERSION_2 ? | |
| 3032 BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE); | |
| 3033 } | |
| 3034 #endif | |
| 3035 | |
| 3036 return CURLE_OK; | |
| 3037 } | |
| 3038 } | |
| 3039 | |
| 3040 static int asn1_object_dump(ASN1_OBJECT *a, char *buf, size_t len) | |
| 3041 { | |
| 3042 int i, ilen; | |
| 3043 | |
| 3044 ilen = (int)len; | |
| 3045 if(ilen < 0) | |
| 3046 return 1; /* buffer too big */ | |
| 3047 | |
| 3048 i = i2t_ASN1_OBJECT(buf, ilen, a); | |
| 3049 | |
| 3050 if(i >= ilen) | |
| 3051 return 1; /* buffer too small */ | |
| 3052 | |
| 3053 return 0; | |
| 3054 } | |
| 3055 | |
| 3056 #define push_certinfo(_label, _num) \ | |
| 3057 do { \ | |
| 3058 long info_len = BIO_get_mem_data(mem, &ptr); \ | |
| 3059 Curl_ssl_push_certinfo_len(data, _num, _label, ptr, info_len); \ | |
| 3060 if(1 != BIO_reset(mem)) \ | |
| 3061 break; \ | |
| 3062 } WHILE_FALSE | |
| 3063 | |
| 3064 static void pubkey_show(struct Curl_easy *data, | |
| 3065 BIO *mem, | |
| 3066 int num, | |
| 3067 const char *type, | |
| 3068 const char *name, | |
| 3069 #ifdef HAVE_OPAQUE_RSA_DSA_DH | |
| 3070 const | |
| 3071 #endif | |
| 3072 BIGNUM *bn) | |
| 3073 { | |
| 3074 char *ptr; | |
| 3075 char namebuf[32]; | |
| 3076 | |
| 3077 msnprintf(namebuf, sizeof(namebuf), "%s(%s)", type, name); | |
| 3078 | |
| 3079 if(bn) | |
| 3080 BN_print(mem, bn); | |
| 3081 push_certinfo(namebuf, num); | |
| 3082 } | |
| 3083 | |
| 3084 #ifdef HAVE_OPAQUE_RSA_DSA_DH | |
| 3085 #define print_pubkey_BN(_type, _name, _num) \ | |
| 3086 pubkey_show(data, mem, _num, #_type, #_name, _name) | |
| 3087 | |
| 3088 #else | |
| 3089 #define print_pubkey_BN(_type, _name, _num) \ | |
| 3090 do { \ | |
| 3091 if(_type->_name) { \ | |
| 3092 pubkey_show(data, mem, _num, #_type, #_name, _type->_name); \ | |
| 3093 } \ | |
| 3094 } WHILE_FALSE | |
| 3095 #endif | |
| 3096 | |
| 3097 static int X509V3_ext(struct Curl_easy *data, | |
| 3098 int certnum, | |
| 3099 CONST_EXTS STACK_OF(X509_EXTENSION) *exts) | |
| 3100 { | |
| 3101 int i; | |
| 3102 size_t j; | |
| 3103 | |
| 3104 if((int)sk_X509_EXTENSION_num(exts) <= 0) | |
| 3105 /* no extensions, bail out */ | |
| 3106 return 1; | |
| 3107 | |
| 3108 for(i = 0; i < (int)sk_X509_EXTENSION_num(exts); i++) { | |
| 3109 ASN1_OBJECT *obj; | |
| 3110 X509_EXTENSION *ext = sk_X509_EXTENSION_value(exts, i); | |
| 3111 BUF_MEM *biomem; | |
| 3112 char buf[512]; | |
| 3113 char *ptr = buf; | |
| 3114 char namebuf[128]; | |
| 3115 BIO *bio_out = BIO_new(BIO_s_mem()); | |
| 3116 | |
| 3117 if(!bio_out) | |
| 3118 return 1; | |
| 3119 | |
| 3120 obj = X509_EXTENSION_get_object(ext); | |
| 3121 | |
| 3122 asn1_object_dump(obj, namebuf, sizeof(namebuf)); | |
| 3123 | |
| 3124 if(!X509V3_EXT_print(bio_out, ext, 0, 0)) | |
| 3125 ASN1_STRING_print(bio_out, (ASN1_STRING *)X509_EXTENSION_get_data(ext)); | |
| 3126 | |
| 3127 BIO_get_mem_ptr(bio_out, &biomem); | |
| 3128 | |
| 3129 for(j = 0; j < (size_t)biomem->length; j++) { | |
| 3130 const char *sep = ""; | |
| 3131 if(biomem->data[j] == '\n') { | |
| 3132 sep = ", "; | |
| 3133 j++; /* skip the newline */ | |
| 3134 }; | |
| 3135 while((j<(size_t)biomem->length) && (biomem->data[j] == ' ')) | |
| 3136 j++; | |
| 3137 if(j<(size_t)biomem->length) | |
| 3138 ptr += msnprintf(ptr, sizeof(buf)-(ptr-buf), "%s%c", sep, | |
| 3139 biomem->data[j]); | |
| 3140 } | |
| 3141 | |
| 3142 Curl_ssl_push_certinfo(data, certnum, namebuf, buf); | |
| 3143 | |
| 3144 BIO_free(bio_out); | |
| 3145 | |
| 3146 } | |
| 3147 return 0; /* all is fine */ | |
| 3148 } | |
| 3149 | |
| 3150 #ifdef OPENSSL_IS_BORINGSSL | |
| 3151 typedef size_t numcert_t; | |
| 3152 #else | |
| 3153 typedef int numcert_t; | |
| 3154 #endif | |
| 3155 | |
| 3156 static CURLcode get_cert_chain(struct connectdata *conn, | |
| 3157 struct ssl_connect_data *connssl) | |
| 3158 | |
| 3159 { | |
| 3160 CURLcode result; | |
| 3161 STACK_OF(X509) *sk; | |
| 3162 int i; | |
| 3163 struct Curl_easy *data = conn->data; | |
| 3164 numcert_t numcerts; | |
| 3165 BIO *mem; | |
| 3166 | |
| 3167 sk = SSL_get_peer_cert_chain(BACKEND->handle); | |
| 3168 if(!sk) { | |
| 3169 return CURLE_OUT_OF_MEMORY; | |
| 3170 } | |
| 3171 | |
| 3172 numcerts = sk_X509_num(sk); | |
| 3173 | |
| 3174 result = Curl_ssl_init_certinfo(data, (int)numcerts); | |
| 3175 if(result) { | |
| 3176 return result; | |
| 3177 } | |
| 3178 | |
| 3179 mem = BIO_new(BIO_s_mem()); | |
| 3180 | |
| 3181 for(i = 0; i < (int)numcerts; i++) { | |
| 3182 ASN1_INTEGER *num; | |
| 3183 X509 *x = sk_X509_value(sk, i); | |
| 3184 EVP_PKEY *pubkey = NULL; | |
| 3185 int j; | |
| 3186 char *ptr; | |
| 3187 const ASN1_BIT_STRING *psig = NULL; | |
| 3188 | |
| 3189 X509_NAME_print_ex(mem, X509_get_subject_name(x), 0, XN_FLAG_ONELINE); | |
| 3190 push_certinfo("Subject", i); | |
| 3191 | |
| 3192 X509_NAME_print_ex(mem, X509_get_issuer_name(x), 0, XN_FLAG_ONELINE); | |
| 3193 push_certinfo("Issuer", i); | |
| 3194 | |
| 3195 BIO_printf(mem, "%lx", X509_get_version(x)); | |
| 3196 push_certinfo("Version", i); | |
| 3197 | |
| 3198 num = X509_get_serialNumber(x); | |
| 3199 if(num->type == V_ASN1_NEG_INTEGER) | |
| 3200 BIO_puts(mem, "-"); | |
| 3201 for(j = 0; j < num->length; j++) | |
| 3202 BIO_printf(mem, "%02x", num->data[j]); | |
| 3203 push_certinfo("Serial Number", i); | |
| 3204 | |
| 3205 #if defined(HAVE_X509_GET0_SIGNATURE) && defined(HAVE_X509_GET0_EXTENSIONS) | |
| 3206 { | |
| 3207 const X509_ALGOR *sigalg = NULL; | |
| 3208 X509_PUBKEY *xpubkey = NULL; | |
| 3209 ASN1_OBJECT *pubkeyoid = NULL; | |
| 3210 | |
| 3211 X509_get0_signature(&psig, &sigalg, x); | |
| 3212 if(sigalg) { | |
| 3213 i2a_ASN1_OBJECT(mem, sigalg->algorithm); | |
| 3214 push_certinfo("Signature Algorithm", i); | |
| 3215 } | |
| 3216 | |
| 3217 xpubkey = X509_get_X509_PUBKEY(x); | |
| 3218 if(xpubkey) { | |
| 3219 X509_PUBKEY_get0_param(&pubkeyoid, NULL, NULL, NULL, xpubkey); | |
| 3220 if(pubkeyoid) { | |
| 3221 i2a_ASN1_OBJECT(mem, pubkeyoid); | |
| 3222 push_certinfo("Public Key Algorithm", i); | |
| 3223 } | |
| 3224 } | |
| 3225 | |
| 3226 X509V3_ext(data, i, X509_get0_extensions(x)); | |
| 3227 } | |
| 3228 #else | |
| 3229 { | |
| 3230 /* before OpenSSL 1.0.2 */ | |
| 3231 X509_CINF *cinf = x->cert_info; | |
| 3232 | |
| 3233 i2a_ASN1_OBJECT(mem, cinf->signature->algorithm); | |
| 3234 push_certinfo("Signature Algorithm", i); | |
| 3235 | |
| 3236 i2a_ASN1_OBJECT(mem, cinf->key->algor->algorithm); | |
| 3237 push_certinfo("Public Key Algorithm", i); | |
| 3238 | |
| 3239 X509V3_ext(data, i, cinf->extensions); | |
| 3240 | |
| 3241 psig = x->signature; | |
| 3242 } | |
| 3243 #endif | |
| 3244 | |
| 3245 ASN1_TIME_print(mem, X509_get0_notBefore(x)); | |
| 3246 push_certinfo("Start date", i); | |
| 3247 | |
| 3248 ASN1_TIME_print(mem, X509_get0_notAfter(x)); | |
| 3249 push_certinfo("Expire date", i); | |
| 3250 | |
| 3251 pubkey = X509_get_pubkey(x); | |
| 3252 if(!pubkey) | |
| 3253 infof(data, " Unable to load public key\n"); | |
| 3254 else { | |
| 3255 int pktype; | |
| 3256 #ifdef HAVE_OPAQUE_EVP_PKEY | |
| 3257 pktype = EVP_PKEY_id(pubkey); | |
| 3258 #else | |
| 3259 pktype = pubkey->type; | |
| 3260 #endif | |
| 3261 switch(pktype) { | |
| 3262 case EVP_PKEY_RSA: | |
| 3263 { | |
| 3264 RSA *rsa; | |
| 3265 #ifdef HAVE_OPAQUE_EVP_PKEY | |
| 3266 rsa = EVP_PKEY_get0_RSA(pubkey); | |
| 3267 #else | |
| 3268 rsa = pubkey->pkey.rsa; | |
| 3269 #endif | |
| 3270 | |
| 3271 #ifdef HAVE_OPAQUE_RSA_DSA_DH | |
| 3272 { | |
| 3273 const BIGNUM *n; | |
| 3274 const BIGNUM *e; | |
| 3275 | |
| 3276 RSA_get0_key(rsa, &n, &e, NULL); | |
| 3277 BIO_printf(mem, "%d", BN_num_bits(n)); | |
| 3278 push_certinfo("RSA Public Key", i); | |
| 3279 print_pubkey_BN(rsa, n, i); | |
| 3280 print_pubkey_BN(rsa, e, i); | |
| 3281 } | |
| 3282 #else | |
| 3283 BIO_printf(mem, "%d", BN_num_bits(rsa->n)); | |
| 3284 push_certinfo("RSA Public Key", i); | |
| 3285 print_pubkey_BN(rsa, n, i); | |
| 3286 print_pubkey_BN(rsa, e, i); | |
| 3287 #endif | |
| 3288 | |
| 3289 break; | |
| 3290 } | |
| 3291 case EVP_PKEY_DSA: | |
| 3292 { | |
| 3293 #ifndef OPENSSL_NO_DSA | |
| 3294 DSA *dsa; | |
| 3295 #ifdef HAVE_OPAQUE_EVP_PKEY | |
| 3296 dsa = EVP_PKEY_get0_DSA(pubkey); | |
| 3297 #else | |
| 3298 dsa = pubkey->pkey.dsa; | |
| 3299 #endif | |
| 3300 #ifdef HAVE_OPAQUE_RSA_DSA_DH | |
| 3301 { | |
| 3302 const BIGNUM *p; | |
| 3303 const BIGNUM *q; | |
| 3304 const BIGNUM *g; | |
| 3305 const BIGNUM *pub_key; | |
| 3306 | |
| 3307 DSA_get0_pqg(dsa, &p, &q, &g); | |
| 3308 DSA_get0_key(dsa, &pub_key, NULL); | |
| 3309 | |
| 3310 print_pubkey_BN(dsa, p, i); | |
| 3311 print_pubkey_BN(dsa, q, i); | |
| 3312 print_pubkey_BN(dsa, g, i); | |
| 3313 print_pubkey_BN(dsa, pub_key, i); | |
| 3314 } | |
| 3315 #else | |
| 3316 print_pubkey_BN(dsa, p, i); | |
| 3317 print_pubkey_BN(dsa, q, i); | |
| 3318 print_pubkey_BN(dsa, g, i); | |
| 3319 print_pubkey_BN(dsa, pub_key, i); | |
| 3320 #endif | |
| 3321 #endif /* !OPENSSL_NO_DSA */ | |
| 3322 break; | |
| 3323 } | |
| 3324 case EVP_PKEY_DH: | |
| 3325 { | |
| 3326 DH *dh; | |
| 3327 #ifdef HAVE_OPAQUE_EVP_PKEY | |
| 3328 dh = EVP_PKEY_get0_DH(pubkey); | |
| 3329 #else | |
| 3330 dh = pubkey->pkey.dh; | |
| 3331 #endif | |
| 3332 #ifdef HAVE_OPAQUE_RSA_DSA_DH | |
| 3333 { | |
| 3334 const BIGNUM *p; | |
| 3335 const BIGNUM *q; | |
| 3336 const BIGNUM *g; | |
| 3337 const BIGNUM *pub_key; | |
| 3338 DH_get0_pqg(dh, &p, &q, &g); | |
| 3339 DH_get0_key(dh, &pub_key, NULL); | |
| 3340 print_pubkey_BN(dh, p, i); | |
| 3341 print_pubkey_BN(dh, q, i); | |
| 3342 print_pubkey_BN(dh, g, i); | |
| 3343 print_pubkey_BN(dh, pub_key, i); | |
| 3344 } | |
| 3345 #else | |
| 3346 print_pubkey_BN(dh, p, i); | |
| 3347 print_pubkey_BN(dh, g, i); | |
| 3348 print_pubkey_BN(dh, pub_key, i); | |
| 3349 #endif | |
| 3350 break; | |
| 3351 } | |
| 3352 } | |
| 3353 EVP_PKEY_free(pubkey); | |
| 3354 } | |
| 3355 | |
| 3356 if(psig) { | |
| 3357 for(j = 0; j < psig->length; j++) | |
| 3358 BIO_printf(mem, "%02x:", psig->data[j]); | |
| 3359 push_certinfo("Signature", i); | |
| 3360 } | |
| 3361 | |
| 3362 PEM_write_bio_X509(mem, x); | |
| 3363 push_certinfo("Cert", i); | |
| 3364 } | |
| 3365 | |
| 3366 BIO_free(mem); | |
| 3367 | |
| 3368 return CURLE_OK; | |
| 3369 } | |
| 3370 | |
| 3371 /* | |
| 3372 * Heavily modified from: | |
| 3373 * https://www.owasp.org/index.php/Certificate_and_Public_Key_Pinning#OpenSSL | |
| 3374 */ | |
| 3375 static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data, X509* cert, | |
| 3376 const char *pinnedpubkey) | |
| 3377 { | |
| 3378 /* Scratch */ | |
| 3379 int len1 = 0, len2 = 0; | |
| 3380 unsigned char *buff1 = NULL, *temp = NULL; | |
| 3381 | |
| 3382 /* Result is returned to caller */ | |
| 3383 CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; | |
| 3384 | |
| 3385 /* if a path wasn't specified, don't pin */ | |
| 3386 if(!pinnedpubkey) | |
| 3387 return CURLE_OK; | |
| 3388 | |
| 3389 if(!cert) | |
| 3390 return result; | |
| 3391 | |
| 3392 do { | |
| 3393 /* Begin Gyrations to get the subjectPublicKeyInfo */ | |
| 3394 /* Thanks to Viktor Dukhovni on the OpenSSL mailing list */ | |
| 3395 | |
| 3396 /* https://groups.google.com/group/mailing.openssl.users/browse_thread | |
| 3397 /thread/d61858dae102c6c7 */ | |
| 3398 len1 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL); | |
| 3399 if(len1 < 1) | |
| 3400 break; /* failed */ | |
| 3401 | |
| 3402 buff1 = temp = malloc(len1); | |
| 3403 if(!buff1) | |
| 3404 break; /* failed */ | |
| 3405 | |
| 3406 /* https://www.openssl.org/docs/crypto/d2i_X509.html */ | |
| 3407 len2 = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &temp); | |
| 3408 | |
| 3409 /* | |
| 3410 * These checks are verifying we got back the same values as when we | |
| 3411 * sized the buffer. It's pretty weak since they should always be the | |
| 3412 * same. But it gives us something to test. | |
| 3413 */ | |
| 3414 if((len1 != len2) || !temp || ((temp - buff1) != len1)) | |
| 3415 break; /* failed */ | |
| 3416 | |
| 3417 /* End Gyrations */ | |
| 3418 | |
| 3419 /* The one good exit point */ | |
| 3420 result = Curl_pin_peer_pubkey(data, pinnedpubkey, buff1, len1); | |
| 3421 } while(0); | |
| 3422 | |
| 3423 if(buff1) | |
| 3424 free(buff1); | |
| 3425 | |
| 3426 return result; | |
| 3427 } | |
| 3428 | |
| 3429 /* | |
| 3430 * Get the server cert, verify it and show it etc, only call failf() if the | |
| 3431 * 'strict' argument is TRUE as otherwise all this is for informational | |
| 3432 * purposes only! | |
| 3433 * | |
| 3434 * We check certificates to authenticate the server; otherwise we risk | |
| 3435 * man-in-the-middle attack. | |
| 3436 */ | |
| 3437 static CURLcode servercert(struct connectdata *conn, | |
| 3438 struct ssl_connect_data *connssl, | |
| 3439 bool strict) | |
| 3440 { | |
| 3441 CURLcode result = CURLE_OK; | |
| 3442 int rc; | |
| 3443 long lerr; | |
| 3444 struct Curl_easy *data = conn->data; | |
| 3445 X509 *issuer; | |
| 3446 BIO *fp = NULL; | |
| 3447 char error_buffer[256]=""; | |
| 3448 char buffer[2048]; | |
| 3449 const char *ptr; | |
| 3450 long * const certverifyresult = SSL_IS_PROXY() ? | |
| 3451 &data->set.proxy_ssl.certverifyresult : &data->set.ssl.certverifyresult; | |
| 3452 BIO *mem = BIO_new(BIO_s_mem()); | |
| 3453 | |
| 3454 if(data->set.ssl.certinfo) | |
| 3455 /* we've been asked to gather certificate info! */ | |
| 3456 (void)get_cert_chain(conn, connssl); | |
| 3457 | |
| 3458 BACKEND->server_cert = SSL_get_peer_certificate(BACKEND->handle); | |
| 3459 if(!BACKEND->server_cert) { | |
| 3460 BIO_free(mem); | |
| 3461 if(!strict) | |
| 3462 return CURLE_OK; | |
| 3463 | |
| 3464 failf(data, "SSL: couldn't get peer certificate!"); | |
| 3465 return CURLE_PEER_FAILED_VERIFICATION; | |
| 3466 } | |
| 3467 | |
| 3468 infof(data, "%s certificate:\n", SSL_IS_PROXY() ? "Proxy" : "Server"); | |
| 3469 | |
| 3470 rc = x509_name_oneline(X509_get_subject_name(BACKEND->server_cert), | |
| 3471 buffer, sizeof(buffer)); | |
| 3472 infof(data, " subject: %s\n", rc?"[NONE]":buffer); | |
| 3473 | |
| 3474 #ifndef CURL_DISABLE_VERBOSE_STRINGS | |
| 3475 { | |
| 3476 long len; | |
| 3477 ASN1_TIME_print(mem, X509_get0_notBefore(BACKEND->server_cert)); | |
| 3478 len = BIO_get_mem_data(mem, (char **) &ptr); | |
| 3479 infof(data, " start date: %.*s\n", len, ptr); | |
| 3480 (void)BIO_reset(mem); | |
| 3481 | |
| 3482 ASN1_TIME_print(mem, X509_get0_notAfter(BACKEND->server_cert)); | |
| 3483 len = BIO_get_mem_data(mem, (char **) &ptr); | |
| 3484 infof(data, " expire date: %.*s\n", len, ptr); | |
| 3485 (void)BIO_reset(mem); | |
| 3486 } | |
| 3487 #endif | |
| 3488 | |
| 3489 BIO_free(mem); | |
| 3490 | |
| 3491 if(SSL_CONN_CONFIG(verifyhost)) { | |
| 3492 result = verifyhost(conn, BACKEND->server_cert); | |
| 3493 if(result) { | |
| 3494 X509_free(BACKEND->server_cert); | |
| 3495 BACKEND->server_cert = NULL; | |
| 3496 return result; | |
| 3497 } | |
| 3498 } | |
| 3499 | |
| 3500 rc = x509_name_oneline(X509_get_issuer_name(BACKEND->server_cert), | |
| 3501 buffer, sizeof(buffer)); | |
| 3502 if(rc) { | |
| 3503 if(strict) | |
| 3504 failf(data, "SSL: couldn't get X509-issuer name!"); | |
| 3505 result = CURLE_PEER_FAILED_VERIFICATION; | |
| 3506 } | |
| 3507 else { | |
| 3508 infof(data, " issuer: %s\n", buffer); | |
| 3509 | |
| 3510 /* We could do all sorts of certificate verification stuff here before | |
| 3511 deallocating the certificate. */ | |
| 3512 | |
| 3513 /* e.g. match issuer name with provided issuer certificate */ | |
| 3514 if(SSL_SET_OPTION(issuercert)) { | |
| 3515 fp = BIO_new(BIO_s_file()); | |
| 3516 if(fp == NULL) { | |
| 3517 failf(data, | |
| 3518 "BIO_new return NULL, " OSSL_PACKAGE | |
| 3519 " error %s", | |
| 3520 ossl_strerror(ERR_get_error(), error_buffer, | |
| 3521 sizeof(error_buffer)) ); | |
| 3522 X509_free(BACKEND->server_cert); | |
| 3523 BACKEND->server_cert = NULL; | |
| 3524 return CURLE_OUT_OF_MEMORY; | |
| 3525 } | |
| 3526 | |
| 3527 if(BIO_read_filename(fp, SSL_SET_OPTION(issuercert)) <= 0) { | |
| 3528 if(strict) | |
| 3529 failf(data, "SSL: Unable to open issuer cert (%s)", | |
| 3530 SSL_SET_OPTION(issuercert)); | |
| 3531 BIO_free(fp); | |
| 3532 X509_free(BACKEND->server_cert); | |
| 3533 BACKEND->server_cert = NULL; | |
| 3534 return CURLE_SSL_ISSUER_ERROR; | |
| 3535 } | |
| 3536 | |
| 3537 issuer = PEM_read_bio_X509(fp, NULL, ZERO_NULL, NULL); | |
| 3538 if(!issuer) { | |
| 3539 if(strict) | |
| 3540 failf(data, "SSL: Unable to read issuer cert (%s)", | |
| 3541 SSL_SET_OPTION(issuercert)); | |
| 3542 BIO_free(fp); | |
| 3543 X509_free(issuer); | |
| 3544 X509_free(BACKEND->server_cert); | |
| 3545 BACKEND->server_cert = NULL; | |
| 3546 return CURLE_SSL_ISSUER_ERROR; | |
| 3547 } | |
| 3548 | |
| 3549 if(X509_check_issued(issuer, BACKEND->server_cert) != X509_V_OK) { | |
| 3550 if(strict) | |
| 3551 failf(data, "SSL: Certificate issuer check failed (%s)", | |
| 3552 SSL_SET_OPTION(issuercert)); | |
| 3553 BIO_free(fp); | |
| 3554 X509_free(issuer); | |
| 3555 X509_free(BACKEND->server_cert); | |
| 3556 BACKEND->server_cert = NULL; | |
| 3557 return CURLE_SSL_ISSUER_ERROR; | |
| 3558 } | |
| 3559 | |
| 3560 infof(data, " SSL certificate issuer check ok (%s)\n", | |
| 3561 SSL_SET_OPTION(issuercert)); | |
| 3562 BIO_free(fp); | |
| 3563 X509_free(issuer); | |
| 3564 } | |
| 3565 | |
| 3566 lerr = *certverifyresult = SSL_get_verify_result(BACKEND->handle); | |
| 3567 | |
| 3568 if(*certverifyresult != X509_V_OK) { | |
| 3569 if(SSL_CONN_CONFIG(verifypeer)) { | |
| 3570 /* We probably never reach this, because SSL_connect() will fail | |
| 3571 and we return earlier if verifypeer is set? */ | |
| 3572 if(strict) | |
| 3573 failf(data, "SSL certificate verify result: %s (%ld)", | |
| 3574 X509_verify_cert_error_string(lerr), lerr); | |
| 3575 result = CURLE_PEER_FAILED_VERIFICATION; | |
| 3576 } | |
| 3577 else | |
| 3578 infof(data, " SSL certificate verify result: %s (%ld)," | |
| 3579 " continuing anyway.\n", | |
| 3580 X509_verify_cert_error_string(lerr), lerr); | |
| 3581 } | |
| 3582 else | |
| 3583 infof(data, " SSL certificate verify ok.\n"); | |
| 3584 } | |
| 3585 | |
| 3586 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \ | |
| 3587 !defined(OPENSSL_NO_OCSP) | |
| 3588 if(SSL_CONN_CONFIG(verifystatus)) { | |
| 3589 result = verifystatus(conn, connssl); | |
| 3590 if(result) { | |
| 3591 X509_free(BACKEND->server_cert); | |
| 3592 BACKEND->server_cert = NULL; | |
| 3593 return result; | |
| 3594 } | |
| 3595 } | |
| 3596 #endif | |
| 3597 | |
| 3598 if(!strict) | |
| 3599 /* when not strict, we don't bother about the verify cert problems */ | |
| 3600 result = CURLE_OK; | |
| 3601 | |
| 3602 ptr = SSL_IS_PROXY() ? data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] : | |
| 3603 data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG]; | |
| 3604 if(!result && ptr) { | |
| 3605 result = pkp_pin_peer_pubkey(data, BACKEND->server_cert, ptr); | |
| 3606 if(result) | |
| 3607 failf(data, "SSL: public key does not match pinned public key!"); | |
| 3608 } | |
| 3609 | |
| 3610 X509_free(BACKEND->server_cert); | |
| 3611 BACKEND->server_cert = NULL; | |
| 3612 connssl->connecting_state = ssl_connect_done; | |
| 3613 | |
| 3614 return result; | |
| 3615 } | |
| 3616 | |
| 3617 static CURLcode ossl_connect_step3(struct connectdata *conn, int sockindex) | |
| 3618 { | |
| 3619 CURLcode result = CURLE_OK; | |
| 3620 struct ssl_connect_data *connssl = &conn->ssl[sockindex]; | |
| 3621 | |
| 3622 DEBUGASSERT(ssl_connect_3 == connssl->connecting_state); | |
| 3623 | |
| 3624 /* | |
| 3625 * We check certificates to authenticate the server; otherwise we risk | |
| 3626 * man-in-the-middle attack; NEVERTHELESS, if we're told explicitly not to | |
| 3627 * verify the peer ignore faults and failures from the server cert | |
| 3628 * operations. | |
| 3629 */ | |
| 3630 | |
| 3631 result = servercert(conn, connssl, (SSL_CONN_CONFIG(verifypeer) || | |
| 3632 SSL_CONN_CONFIG(verifyhost))); | |
| 3633 | |
| 3634 if(!result) | |
| 3635 connssl->connecting_state = ssl_connect_done; | |
| 3636 | |
| 3637 return result; | |
| 3638 } | |
| 3639 | |
| 3640 static Curl_recv ossl_recv; | |
| 3641 static Curl_send ossl_send; | |
| 3642 | |
| 3643 static CURLcode ossl_connect_common(struct connectdata *conn, | |
| 3644 int sockindex, | |
| 3645 bool nonblocking, | |
| 3646 bool *done) | |
| 3647 { | |
| 3648 CURLcode result; | |
| 3649 struct Curl_easy *data = conn->data; | |
| 3650 struct ssl_connect_data *connssl = &conn->ssl[sockindex]; | |
| 3651 curl_socket_t sockfd = conn->sock[sockindex]; | |
| 3652 time_t timeout_ms; | |
| 3653 int what; | |
| 3654 | |
| 3655 /* check if the connection has already been established */ | |
| 3656 if(ssl_connection_complete == connssl->state) { | |
| 3657 *done = TRUE; | |
| 3658 return CURLE_OK; | |
| 3659 } | |
| 3660 | |
| 3661 if(ssl_connect_1 == connssl->connecting_state) { | |
| 3662 /* Find out how much more time we're allowed */ | |
| 3663 timeout_ms = Curl_timeleft(data, NULL, TRUE); | |
| 3664 | |
| 3665 if(timeout_ms < 0) { | |
| 3666 /* no need to continue if time already is up */ | |
| 3667 failf(data, "SSL connection timeout"); | |
| 3668 return CURLE_OPERATION_TIMEDOUT; | |
| 3669 } | |
| 3670 | |
| 3671 result = ossl_connect_step1(conn, sockindex); | |
| 3672 if(result) | |
| 3673 return result; | |
| 3674 } | |
| 3675 | |
| 3676 while(ssl_connect_2 == connssl->connecting_state || | |
| 3677 ssl_connect_2_reading == connssl->connecting_state || | |
| 3678 ssl_connect_2_writing == connssl->connecting_state) { | |
| 3679 | |
| 3680 /* check allowed time left */ | |
| 3681 timeout_ms = Curl_timeleft(data, NULL, TRUE); | |
| 3682 | |
| 3683 if(timeout_ms < 0) { | |
| 3684 /* no need to continue if time already is up */ | |
| 3685 failf(data, "SSL connection timeout"); | |
| 3686 return CURLE_OPERATION_TIMEDOUT; | |
| 3687 } | |
| 3688 | |
| 3689 /* if ssl is expecting something, check if it's available. */ | |
| 3690 if(connssl->connecting_state == ssl_connect_2_reading || | |
| 3691 connssl->connecting_state == ssl_connect_2_writing) { | |
| 3692 | |
| 3693 curl_socket_t writefd = ssl_connect_2_writing == | |
| 3694 connssl->connecting_state?sockfd:CURL_SOCKET_BAD; | |
| 3695 curl_socket_t readfd = ssl_connect_2_reading == | |
| 3696 connssl->connecting_state?sockfd:CURL_SOCKET_BAD; | |
| 3697 | |
| 3698 what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd, | |
| 3699 nonblocking?0:timeout_ms); | |
| 3700 if(what < 0) { | |
| 3701 /* fatal error */ | |
| 3702 failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO); | |
| 3703 return CURLE_SSL_CONNECT_ERROR; | |
| 3704 } | |
| 3705 if(0 == what) { | |
| 3706 if(nonblocking) { | |
| 3707 *done = FALSE; | |
| 3708 return CURLE_OK; | |
| 3709 } | |
| 3710 /* timeout */ | |
| 3711 failf(data, "SSL connection timeout"); | |
| 3712 return CURLE_OPERATION_TIMEDOUT; | |
| 3713 } | |
| 3714 /* socket is readable or writable */ | |
| 3715 } | |
| 3716 | |
| 3717 /* Run transaction, and return to the caller if it failed or if this | |
| 3718 * connection is done nonblocking and this loop would execute again. This | |
| 3719 * permits the owner of a multi handle to abort a connection attempt | |
| 3720 * before step2 has completed while ensuring that a client using select() | |
| 3721 * or epoll() will always have a valid fdset to wait on. | |
| 3722 */ | |
| 3723 result = ossl_connect_step2(conn, sockindex); | |
| 3724 if(result || (nonblocking && | |
| 3725 (ssl_connect_2 == connssl->connecting_state || | |
| 3726 ssl_connect_2_reading == connssl->connecting_state || | |
| 3727 ssl_connect_2_writing == connssl->connecting_state))) | |
| 3728 return result; | |
| 3729 | |
| 3730 } /* repeat step2 until all transactions are done. */ | |
| 3731 | |
| 3732 if(ssl_connect_3 == connssl->connecting_state) { | |
| 3733 result = ossl_connect_step3(conn, sockindex); | |
| 3734 if(result) | |
| 3735 return result; | |
| 3736 } | |
| 3737 | |
| 3738 if(ssl_connect_done == connssl->connecting_state) { | |
| 3739 connssl->state = ssl_connection_complete; | |
| 3740 conn->recv[sockindex] = ossl_recv; | |
| 3741 conn->send[sockindex] = ossl_send; | |
| 3742 *done = TRUE; | |
| 3743 } | |
| 3744 else | |
| 3745 *done = FALSE; | |
| 3746 | |
| 3747 /* Reset our connect state machine */ | |
| 3748 connssl->connecting_state = ssl_connect_1; | |
| 3749 | |
| 3750 return CURLE_OK; | |
| 3751 } | |
| 3752 | |
| 3753 static CURLcode Curl_ossl_connect_nonblocking(struct connectdata *conn, | |
| 3754 int sockindex, | |
| 3755 bool *done) | |
| 3756 { | |
| 3757 return ossl_connect_common(conn, sockindex, TRUE, done); | |
| 3758 } | |
| 3759 | |
| 3760 static CURLcode Curl_ossl_connect(struct connectdata *conn, int sockindex) | |
| 3761 { | |
| 3762 CURLcode result; | |
| 3763 bool done = FALSE; | |
| 3764 | |
| 3765 result = ossl_connect_common(conn, sockindex, FALSE, &done); | |
| 3766 if(result) | |
| 3767 return result; | |
| 3768 | |
| 3769 DEBUGASSERT(done); | |
| 3770 | |
| 3771 return CURLE_OK; | |
| 3772 } | |
| 3773 | |
| 3774 static bool Curl_ossl_data_pending(const struct connectdata *conn, | |
| 3775 int connindex) | |
| 3776 { | |
| 3777 const struct ssl_connect_data *connssl = &conn->ssl[connindex]; | |
| 3778 const struct ssl_connect_data *proxyssl = &conn->proxy_ssl[connindex]; | |
| 3779 | |
| 3780 if(connssl->backend->handle && SSL_pending(connssl->backend->handle)) | |
| 3781 return TRUE; | |
| 3782 | |
| 3783 if(proxyssl->backend->handle && SSL_pending(proxyssl->backend->handle)) | |
| 3784 return TRUE; | |
| 3785 | |
| 3786 return FALSE; | |
| 3787 } | |
| 3788 | |
| 3789 static size_t Curl_ossl_version(char *buffer, size_t size); | |
| 3790 | |
| 3791 static ssize_t ossl_send(struct connectdata *conn, | |
| 3792 int sockindex, | |
| 3793 const void *mem, | |
| 3794 size_t len, | |
| 3795 CURLcode *curlcode) | |
| 3796 { | |
| 3797 /* SSL_write() is said to return 'int' while write() and send() returns | |
| 3798 'size_t' */ | |
| 3799 int err; | |
| 3800 char error_buffer[256]; | |
| 3801 unsigned long sslerror; | |
| 3802 int memlen; | |
| 3803 int rc; | |
| 3804 struct ssl_connect_data *connssl = &conn->ssl[sockindex]; | |
| 3805 | |
| 3806 ERR_clear_error(); | |
| 3807 | |
| 3808 memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len; | |
| 3809 rc = SSL_write(BACKEND->handle, mem, memlen); | |
| 3810 | |
| 3811 if(rc <= 0) { | |
| 3812 err = SSL_get_error(BACKEND->handle, rc); | |
| 3813 | |
| 3814 switch(err) { | |
| 3815 case SSL_ERROR_WANT_READ: | |
| 3816 case SSL_ERROR_WANT_WRITE: | |
| 3817 /* The operation did not complete; the same TLS/SSL I/O function | |
| 3818 should be called again later. This is basically an EWOULDBLOCK | |
| 3819 equivalent. */ | |
| 3820 *curlcode = CURLE_AGAIN; | |
| 3821 return -1; | |
| 3822 case SSL_ERROR_SYSCALL: | |
| 3823 failf(conn->data, "SSL_write() returned SYSCALL, errno = %d", | |
| 3824 SOCKERRNO); | |
| 3825 *curlcode = CURLE_SEND_ERROR; | |
| 3826 return -1; | |
| 3827 case SSL_ERROR_SSL: | |
| 3828 /* A failure in the SSL library occurred, usually a protocol error. | |
| 3829 The OpenSSL error queue contains more information on the error. */ | |
| 3830 sslerror = ERR_get_error(); | |
| 3831 if(ERR_GET_LIB(sslerror) == ERR_LIB_SSL && | |
| 3832 ERR_GET_REASON(sslerror) == SSL_R_BIO_NOT_SET && | |
| 3833 conn->ssl[sockindex].state == ssl_connection_complete && | |
| 3834 conn->proxy_ssl[sockindex].state == ssl_connection_complete) { | |
| 3835 char ver[120]; | |
| 3836 Curl_ossl_version(ver, 120); | |
| 3837 failf(conn->data, "Error: %s does not support double SSL tunneling.", | |
| 3838 ver); | |
| 3839 } | |
| 3840 else | |
| 3841 failf(conn->data, "SSL_write() error: %s", | |
| 3842 ossl_strerror(sslerror, error_buffer, sizeof(error_buffer))); | |
| 3843 *curlcode = CURLE_SEND_ERROR; | |
| 3844 return -1; | |
| 3845 } | |
| 3846 /* a true error */ | |
| 3847 failf(conn->data, OSSL_PACKAGE " SSL_write: %s, errno %d", | |
| 3848 SSL_ERROR_to_str(err), SOCKERRNO); | |
| 3849 *curlcode = CURLE_SEND_ERROR; | |
| 3850 return -1; | |
| 3851 } | |
| 3852 *curlcode = CURLE_OK; | |
| 3853 return (ssize_t)rc; /* number of bytes */ | |
| 3854 } | |
| 3855 | |
| 3856 static ssize_t ossl_recv(struct connectdata *conn, /* connection data */ | |
| 3857 int num, /* socketindex */ | |
| 3858 char *buf, /* store read data here */ | |
| 3859 size_t buffersize, /* max amount to read */ | |
| 3860 CURLcode *curlcode) | |
| 3861 { | |
| 3862 char error_buffer[256]; | |
| 3863 unsigned long sslerror; | |
| 3864 ssize_t nread; | |
| 3865 int buffsize; | |
| 3866 struct ssl_connect_data *connssl = &conn->ssl[num]; | |
| 3867 | |
| 3868 ERR_clear_error(); | |
| 3869 | |
| 3870 buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize; | |
| 3871 nread = (ssize_t)SSL_read(BACKEND->handle, buf, buffsize); | |
| 3872 if(nread <= 0) { | |
| 3873 /* failed SSL_read */ | |
| 3874 int err = SSL_get_error(BACKEND->handle, (int)nread); | |
| 3875 | |
| 3876 switch(err) { | |
| 3877 case SSL_ERROR_NONE: /* this is not an error */ | |
| 3878 break; | |
| 3879 case SSL_ERROR_ZERO_RETURN: /* no more data */ | |
| 3880 /* close_notify alert */ | |
| 3881 connclose(conn, "TLS close_notify"); | |
| 3882 break; | |
| 3883 case SSL_ERROR_WANT_READ: | |
| 3884 case SSL_ERROR_WANT_WRITE: | |
| 3885 /* there's data pending, re-invoke SSL_read() */ | |
| 3886 *curlcode = CURLE_AGAIN; | |
| 3887 return -1; | |
| 3888 default: | |
| 3889 /* openssl/ssl.h for SSL_ERROR_SYSCALL says "look at error stack/return | |
| 3890 value/errno" */ | |
| 3891 /* https://www.openssl.org/docs/crypto/ERR_get_error.html */ | |
| 3892 sslerror = ERR_get_error(); | |
| 3893 if((nread < 0) || sslerror) { | |
| 3894 /* If the return code was negative or there actually is an error in the | |
| 3895 queue */ | |
| 3896 failf(conn->data, OSSL_PACKAGE " SSL_read: %s, errno %d", | |
| 3897 (sslerror ? | |
| 3898 ossl_strerror(sslerror, error_buffer, sizeof(error_buffer)) : | |
| 3899 SSL_ERROR_to_str(err)), | |
| 3900 SOCKERRNO); | |
| 3901 *curlcode = CURLE_RECV_ERROR; | |
| 3902 return -1; | |
| 3903 } | |
| 3904 } | |
| 3905 } | |
| 3906 return nread; | |
| 3907 } | |
| 3908 | |
| 3909 static size_t Curl_ossl_version(char *buffer, size_t size) | |
| 3910 { | |
| 3911 #ifdef OPENSSL_IS_BORINGSSL | |
| 3912 return msnprintf(buffer, size, OSSL_PACKAGE); | |
| 3913 #elif defined(HAVE_OPENSSL_VERSION) && defined(OPENSSL_VERSION_STRING) | |
| 3914 return msnprintf(buffer, size, "%s/%s", | |
| 3915 OSSL_PACKAGE, OpenSSL_version(OPENSSL_VERSION_STRING)); | |
| 3916 #else | |
| 3917 /* not BoringSSL and not using OpenSSL_version */ | |
| 3918 | |
| 3919 char sub[3]; | |
| 3920 unsigned long ssleay_value; | |
| 3921 sub[2]='\0'; | |
| 3922 sub[1]='\0'; | |
| 3923 ssleay_value = OpenSSL_version_num(); | |
| 3924 if(ssleay_value < 0x906000) { | |
| 3925 ssleay_value = SSLEAY_VERSION_NUMBER; | |
| 3926 sub[0]='\0'; | |
| 3927 } | |
| 3928 else { | |
| 3929 if(ssleay_value&0xff0) { | |
| 3930 int minor_ver = (ssleay_value >> 4) & 0xff; | |
| 3931 if(minor_ver > 26) { | |
| 3932 /* handle extended version introduced for 0.9.8za */ | |
| 3933 sub[1] = (char) ((minor_ver - 1) % 26 + 'a' + 1); | |
| 3934 sub[0] = 'z'; | |
| 3935 } | |
| 3936 else { | |
| 3937 sub[0] = (char) (minor_ver + 'a' - 1); | |
| 3938 } | |
| 3939 } | |
| 3940 else | |
| 3941 sub[0]='\0'; | |
| 3942 } | |
| 3943 | |
| 3944 return msnprintf(buffer, size, "%s/%lx.%lx.%lx%s" | |
| 3945 #ifdef OPENSSL_FIPS | |
| 3946 "-fips" | |
| 3947 #endif | |
| 3948 , | |
| 3949 OSSL_PACKAGE, | |
| 3950 (ssleay_value>>28)&0xf, | |
| 3951 (ssleay_value>>20)&0xff, | |
| 3952 (ssleay_value>>12)&0xff, | |
| 3953 sub); | |
| 3954 #endif /* OPENSSL_IS_BORINGSSL */ | |
| 3955 } | |
| 3956 | |
| 3957 /* can be called with data == NULL */ | |
| 3958 static CURLcode Curl_ossl_random(struct Curl_easy *data, | |
| 3959 unsigned char *entropy, size_t length) | |
| 3960 { | |
| 3961 int rc; | |
| 3962 if(data) { | |
| 3963 if(Curl_ossl_seed(data)) /* Initiate the seed if not already done */ | |
| 3964 return CURLE_FAILED_INIT; /* couldn't seed for some reason */ | |
| 3965 } | |
| 3966 else { | |
| 3967 if(!rand_enough()) | |
| 3968 return CURLE_FAILED_INIT; | |
| 3969 } | |
| 3970 /* RAND_bytes() returns 1 on success, 0 otherwise. */ | |
| 3971 rc = RAND_bytes(entropy, curlx_uztosi(length)); | |
| 3972 return (rc == 1 ? CURLE_OK : CURLE_FAILED_INIT); | |
| 3973 } | |
| 3974 | |
| 3975 static CURLcode Curl_ossl_md5sum(unsigned char *tmp, /* input */ | |
| 3976 size_t tmplen, | |
| 3977 unsigned char *md5sum /* output */, | |
| 3978 size_t unused) | |
| 3979 { | |
| 3980 EVP_MD_CTX *mdctx; | |
| 3981 unsigned int len = 0; | |
| 3982 (void) unused; | |
| 3983 | |
| 3984 mdctx = EVP_MD_CTX_create(); | |
| 3985 EVP_DigestInit_ex(mdctx, EVP_md5(), NULL); | |
| 3986 EVP_DigestUpdate(mdctx, tmp, tmplen); | |
| 3987 EVP_DigestFinal_ex(mdctx, md5sum, &len); | |
| 3988 EVP_MD_CTX_destroy(mdctx); | |
| 3989 return CURLE_OK; | |
| 3990 } | |
| 3991 | |
| 3992 #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256) | |
| 3993 static CURLcode Curl_ossl_sha256sum(const unsigned char *tmp, /* input */ | |
| 3994 size_t tmplen, | |
| 3995 unsigned char *sha256sum /* output */, | |
| 3996 size_t unused) | |
| 3997 { | |
| 3998 EVP_MD_CTX *mdctx; | |
| 3999 unsigned int len = 0; | |
| 4000 (void) unused; | |
| 4001 | |
| 4002 mdctx = EVP_MD_CTX_create(); | |
| 4003 EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL); | |
| 4004 EVP_DigestUpdate(mdctx, tmp, tmplen); | |
| 4005 EVP_DigestFinal_ex(mdctx, sha256sum, &len); | |
| 4006 EVP_MD_CTX_destroy(mdctx); | |
| 4007 return CURLE_OK; | |
| 4008 } | |
| 4009 #endif | |
| 4010 | |
| 4011 static bool Curl_ossl_cert_status_request(void) | |
| 4012 { | |
| 4013 #if (OPENSSL_VERSION_NUMBER >= 0x0090808fL) && !defined(OPENSSL_NO_TLSEXT) && \ | |
| 4014 !defined(OPENSSL_NO_OCSP) | |
| 4015 return TRUE; | |
| 4016 #else | |
| 4017 return FALSE; | |
| 4018 #endif | |
| 4019 } | |
| 4020 | |
| 4021 static void *Curl_ossl_get_internals(struct ssl_connect_data *connssl, | |
| 4022 CURLINFO info) | |
| 4023 { | |
| 4024 /* Legacy: CURLINFO_TLS_SESSION must return an SSL_CTX pointer. */ | |
| 4025 return info == CURLINFO_TLS_SESSION ? | |
| 4026 (void *)BACKEND->ctx : (void *)BACKEND->handle; | |
| 4027 } | |
| 4028 | |
| 4029 const struct Curl_ssl Curl_ssl_openssl = { | |
| 4030 { CURLSSLBACKEND_OPENSSL, "openssl" }, /* info */ | |
| 4031 | |
| 4032 SSLSUPP_CA_PATH | | |
| 4033 SSLSUPP_CERTINFO | | |
| 4034 SSLSUPP_PINNEDPUBKEY | | |
| 4035 SSLSUPP_SSL_CTX | | |
| 4036 #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES | |
| 4037 SSLSUPP_TLS13_CIPHERSUITES | | |
| 4038 #endif | |
| 4039 SSLSUPP_HTTPS_PROXY, | |
| 4040 | |
| 4041 sizeof(struct ssl_backend_data), | |
| 4042 | |
| 4043 Curl_ossl_init, /* init */ | |
| 4044 Curl_ossl_cleanup, /* cleanup */ | |
| 4045 Curl_ossl_version, /* version */ | |
| 4046 Curl_ossl_check_cxn, /* check_cxn */ | |
| 4047 Curl_ossl_shutdown, /* shutdown */ | |
| 4048 Curl_ossl_data_pending, /* data_pending */ | |
| 4049 Curl_ossl_random, /* random */ | |
| 4050 Curl_ossl_cert_status_request, /* cert_status_request */ | |
| 4051 Curl_ossl_connect, /* connect */ | |
| 4052 Curl_ossl_connect_nonblocking, /* connect_nonblocking */ | |
| 4053 Curl_ossl_get_internals, /* get_internals */ | |
| 4054 Curl_ossl_close, /* close_one */ | |
| 4055 Curl_ossl_close_all, /* close_all */ | |
| 4056 Curl_ossl_session_free, /* session_free */ | |
| 4057 Curl_ossl_set_engine, /* set_engine */ | |
| 4058 Curl_ossl_set_engine_default, /* set_engine_default */ | |
| 4059 Curl_ossl_engines_list, /* engines_list */ | |
| 4060 Curl_none_false_start, /* false_start */ | |
| 4061 Curl_ossl_md5sum, /* md5sum */ | |
| 4062 #if (OPENSSL_VERSION_NUMBER >= 0x0090800fL) && !defined(OPENSSL_NO_SHA256) | |
| 4063 Curl_ossl_sha256sum /* sha256sum */ | |
| 4064 #else | |
| 4065 NULL /* sha256sum */ | |
| 4066 #endif | |
| 4067 }; | |
| 4068 | |
| 4069 #endif /* USE_OPENSSL */ |
