Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/curl/lib/vtls/vtls.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 /* This file is for implementing all "generic" SSL functions that all libcurl | |
| 24 internals should use. It is then responsible for calling the proper | |
| 25 "backend" function. | |
| 26 | |
| 27 SSL-functions in libcurl should call functions in this source file, and not | |
| 28 to any specific SSL-layer. | |
| 29 | |
| 30 Curl_ssl_ - prefix for generic ones | |
| 31 | |
| 32 Note that this source code uses the functions of the configured SSL | |
| 33 backend via the global Curl_ssl instance. | |
| 34 | |
| 35 "SSL/TLS Strong Encryption: An Introduction" | |
| 36 https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html | |
| 37 */ | |
| 38 | |
| 39 #include "curl_setup.h" | |
| 40 | |
| 41 #ifdef HAVE_SYS_TYPES_H | |
| 42 #include <sys/types.h> | |
| 43 #endif | |
| 44 #ifdef HAVE_SYS_STAT_H | |
| 45 #include <sys/stat.h> | |
| 46 #endif | |
| 47 #ifdef HAVE_FCNTL_H | |
| 48 #include <fcntl.h> | |
| 49 #endif | |
| 50 | |
| 51 #include "urldata.h" | |
| 52 | |
| 53 #include "vtls.h" /* generic SSL protos etc */ | |
| 54 #include "slist.h" | |
| 55 #include "sendf.h" | |
| 56 #include "strcase.h" | |
| 57 #include "url.h" | |
| 58 #include "progress.h" | |
| 59 #include "share.h" | |
| 60 #include "multiif.h" | |
| 61 #include "timeval.h" | |
| 62 #include "curl_md5.h" | |
| 63 #include "warnless.h" | |
| 64 #include "curl_base64.h" | |
| 65 #include "curl_printf.h" | |
| 66 | |
| 67 /* The last #include files should be: */ | |
| 68 #include "curl_memory.h" | |
| 69 #include "memdebug.h" | |
| 70 | |
| 71 /* convenience macro to check if this handle is using a shared SSL session */ | |
| 72 #define SSLSESSION_SHARED(data) (data->share && \ | |
| 73 (data->share->specifier & \ | |
| 74 (1<<CURL_LOCK_DATA_SSL_SESSION))) | |
| 75 | |
| 76 #define CLONE_STRING(var) \ | |
| 77 if(source->var) { \ | |
| 78 dest->var = strdup(source->var); \ | |
| 79 if(!dest->var) \ | |
| 80 return FALSE; \ | |
| 81 } \ | |
| 82 else \ | |
| 83 dest->var = NULL; | |
| 84 | |
| 85 bool | |
| 86 Curl_ssl_config_matches(struct ssl_primary_config* data, | |
| 87 struct ssl_primary_config* needle) | |
| 88 { | |
| 89 if((data->version == needle->version) && | |
| 90 (data->version_max == needle->version_max) && | |
| 91 (data->verifypeer == needle->verifypeer) && | |
| 92 (data->verifyhost == needle->verifyhost) && | |
| 93 (data->verifystatus == needle->verifystatus) && | |
| 94 Curl_safe_strcasecompare(data->CApath, needle->CApath) && | |
| 95 Curl_safe_strcasecompare(data->CAfile, needle->CAfile) && | |
| 96 Curl_safe_strcasecompare(data->clientcert, needle->clientcert) && | |
| 97 Curl_safe_strcasecompare(data->random_file, needle->random_file) && | |
| 98 Curl_safe_strcasecompare(data->egdsocket, needle->egdsocket) && | |
| 99 Curl_safe_strcasecompare(data->cipher_list, needle->cipher_list) && | |
| 100 Curl_safe_strcasecompare(data->cipher_list13, needle->cipher_list13)) | |
| 101 return TRUE; | |
| 102 | |
| 103 return FALSE; | |
| 104 } | |
| 105 | |
| 106 bool | |
| 107 Curl_clone_primary_ssl_config(struct ssl_primary_config *source, | |
| 108 struct ssl_primary_config *dest) | |
| 109 { | |
| 110 dest->version = source->version; | |
| 111 dest->version_max = source->version_max; | |
| 112 dest->verifypeer = source->verifypeer; | |
| 113 dest->verifyhost = source->verifyhost; | |
| 114 dest->verifystatus = source->verifystatus; | |
| 115 dest->sessionid = source->sessionid; | |
| 116 | |
| 117 CLONE_STRING(CApath); | |
| 118 CLONE_STRING(CAfile); | |
| 119 CLONE_STRING(clientcert); | |
| 120 CLONE_STRING(random_file); | |
| 121 CLONE_STRING(egdsocket); | |
| 122 CLONE_STRING(cipher_list); | |
| 123 CLONE_STRING(cipher_list13); | |
| 124 | |
| 125 return TRUE; | |
| 126 } | |
| 127 | |
| 128 void Curl_free_primary_ssl_config(struct ssl_primary_config* sslc) | |
| 129 { | |
| 130 Curl_safefree(sslc->CApath); | |
| 131 Curl_safefree(sslc->CAfile); | |
| 132 Curl_safefree(sslc->clientcert); | |
| 133 Curl_safefree(sslc->random_file); | |
| 134 Curl_safefree(sslc->egdsocket); | |
| 135 Curl_safefree(sslc->cipher_list); | |
| 136 Curl_safefree(sslc->cipher_list13); | |
| 137 } | |
| 138 | |
| 139 #ifdef USE_SSL | |
| 140 static int multissl_init(const struct Curl_ssl *backend); | |
| 141 #endif | |
| 142 | |
| 143 int Curl_ssl_backend(void) | |
| 144 { | |
| 145 #ifdef USE_SSL | |
| 146 multissl_init(NULL); | |
| 147 return Curl_ssl->info.id; | |
| 148 #else | |
| 149 return (int)CURLSSLBACKEND_NONE; | |
| 150 #endif | |
| 151 } | |
| 152 | |
| 153 #ifdef USE_SSL | |
| 154 | |
| 155 /* "global" init done? */ | |
| 156 static bool init_ssl = FALSE; | |
| 157 | |
| 158 /** | |
| 159 * Global SSL init | |
| 160 * | |
| 161 * @retval 0 error initializing SSL | |
| 162 * @retval 1 SSL initialized successfully | |
| 163 */ | |
| 164 int Curl_ssl_init(void) | |
| 165 { | |
| 166 /* make sure this is only done once */ | |
| 167 if(init_ssl) | |
| 168 return 1; | |
| 169 init_ssl = TRUE; /* never again */ | |
| 170 | |
| 171 return Curl_ssl->init(); | |
| 172 } | |
| 173 | |
| 174 | |
| 175 /* Global cleanup */ | |
| 176 void Curl_ssl_cleanup(void) | |
| 177 { | |
| 178 if(init_ssl) { | |
| 179 /* only cleanup if we did a previous init */ | |
| 180 Curl_ssl->cleanup(); | |
| 181 init_ssl = FALSE; | |
| 182 } | |
| 183 } | |
| 184 | |
| 185 static bool ssl_prefs_check(struct Curl_easy *data) | |
| 186 { | |
| 187 /* check for CURLOPT_SSLVERSION invalid parameter value */ | |
| 188 const long sslver = data->set.ssl.primary.version; | |
| 189 if((sslver < 0) || (sslver >= CURL_SSLVERSION_LAST)) { | |
| 190 failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION"); | |
| 191 return FALSE; | |
| 192 } | |
| 193 | |
| 194 switch(data->set.ssl.primary.version_max) { | |
| 195 case CURL_SSLVERSION_MAX_NONE: | |
| 196 case CURL_SSLVERSION_MAX_DEFAULT: | |
| 197 break; | |
| 198 | |
| 199 default: | |
| 200 if((data->set.ssl.primary.version_max >> 16) < sslver) { | |
| 201 failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION"); | |
| 202 return FALSE; | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 return TRUE; | |
| 207 } | |
| 208 | |
| 209 static CURLcode | |
| 210 ssl_connect_init_proxy(struct connectdata *conn, int sockindex) | |
| 211 { | |
| 212 DEBUGASSERT(conn->bits.proxy_ssl_connected[sockindex]); | |
| 213 if(ssl_connection_complete == conn->ssl[sockindex].state && | |
| 214 !conn->proxy_ssl[sockindex].use) { | |
| 215 struct ssl_backend_data *pbdata; | |
| 216 | |
| 217 if(!(Curl_ssl->supports & SSLSUPP_HTTPS_PROXY)) | |
| 218 return CURLE_NOT_BUILT_IN; | |
| 219 | |
| 220 /* The pointers to the ssl backend data, which is opaque here, are swapped | |
| 221 rather than move the contents. */ | |
| 222 pbdata = conn->proxy_ssl[sockindex].backend; | |
| 223 conn->proxy_ssl[sockindex] = conn->ssl[sockindex]; | |
| 224 | |
| 225 memset(&conn->ssl[sockindex], 0, sizeof(conn->ssl[sockindex])); | |
| 226 memset(pbdata, 0, Curl_ssl->sizeof_ssl_backend_data); | |
| 227 | |
| 228 conn->ssl[sockindex].backend = pbdata; | |
| 229 } | |
| 230 return CURLE_OK; | |
| 231 } | |
| 232 | |
| 233 CURLcode | |
| 234 Curl_ssl_connect(struct connectdata *conn, int sockindex) | |
| 235 { | |
| 236 CURLcode result; | |
| 237 | |
| 238 if(conn->bits.proxy_ssl_connected[sockindex]) { | |
| 239 result = ssl_connect_init_proxy(conn, sockindex); | |
| 240 if(result) | |
| 241 return result; | |
| 242 } | |
| 243 | |
| 244 if(!ssl_prefs_check(conn->data)) | |
| 245 return CURLE_SSL_CONNECT_ERROR; | |
| 246 | |
| 247 /* mark this is being ssl-enabled from here on. */ | |
| 248 conn->ssl[sockindex].use = TRUE; | |
| 249 conn->ssl[sockindex].state = ssl_connection_negotiating; | |
| 250 | |
| 251 result = Curl_ssl->connect_blocking(conn, sockindex); | |
| 252 | |
| 253 if(!result) | |
| 254 Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */ | |
| 255 | |
| 256 return result; | |
| 257 } | |
| 258 | |
| 259 CURLcode | |
| 260 Curl_ssl_connect_nonblocking(struct connectdata *conn, int sockindex, | |
| 261 bool *done) | |
| 262 { | |
| 263 CURLcode result; | |
| 264 if(conn->bits.proxy_ssl_connected[sockindex]) { | |
| 265 result = ssl_connect_init_proxy(conn, sockindex); | |
| 266 if(result) | |
| 267 return result; | |
| 268 } | |
| 269 | |
| 270 if(!ssl_prefs_check(conn->data)) | |
| 271 return CURLE_SSL_CONNECT_ERROR; | |
| 272 | |
| 273 /* mark this is being ssl requested from here on. */ | |
| 274 conn->ssl[sockindex].use = TRUE; | |
| 275 result = Curl_ssl->connect_nonblocking(conn, sockindex, done); | |
| 276 if(!result && *done) | |
| 277 Curl_pgrsTime(conn->data, TIMER_APPCONNECT); /* SSL is connected */ | |
| 278 return result; | |
| 279 } | |
| 280 | |
| 281 /* | |
| 282 * Lock shared SSL session data | |
| 283 */ | |
| 284 void Curl_ssl_sessionid_lock(struct connectdata *conn) | |
| 285 { | |
| 286 if(SSLSESSION_SHARED(conn->data)) | |
| 287 Curl_share_lock(conn->data, | |
| 288 CURL_LOCK_DATA_SSL_SESSION, CURL_LOCK_ACCESS_SINGLE); | |
| 289 } | |
| 290 | |
| 291 /* | |
| 292 * Unlock shared SSL session data | |
| 293 */ | |
| 294 void Curl_ssl_sessionid_unlock(struct connectdata *conn) | |
| 295 { | |
| 296 if(SSLSESSION_SHARED(conn->data)) | |
| 297 Curl_share_unlock(conn->data, CURL_LOCK_DATA_SSL_SESSION); | |
| 298 } | |
| 299 | |
| 300 /* | |
| 301 * Check if there's a session ID for the given connection in the cache, and if | |
| 302 * there's one suitable, it is provided. Returns TRUE when no entry matched. | |
| 303 */ | |
| 304 bool Curl_ssl_getsessionid(struct connectdata *conn, | |
| 305 void **ssl_sessionid, | |
| 306 size_t *idsize, /* set 0 if unknown */ | |
| 307 int sockindex) | |
| 308 { | |
| 309 struct curl_ssl_session *check; | |
| 310 struct Curl_easy *data = conn->data; | |
| 311 size_t i; | |
| 312 long *general_age; | |
| 313 bool no_match = TRUE; | |
| 314 | |
| 315 const bool isProxy = CONNECT_PROXY_SSL(); | |
| 316 struct ssl_primary_config * const ssl_config = isProxy ? | |
| 317 &conn->proxy_ssl_config : | |
| 318 &conn->ssl_config; | |
| 319 const char * const name = isProxy ? conn->http_proxy.host.name : | |
| 320 conn->host.name; | |
| 321 int port = isProxy ? (int)conn->port : conn->remote_port; | |
| 322 *ssl_sessionid = NULL; | |
| 323 | |
| 324 DEBUGASSERT(SSL_SET_OPTION(primary.sessionid)); | |
| 325 | |
| 326 if(!SSL_SET_OPTION(primary.sessionid)) | |
| 327 /* session ID re-use is disabled */ | |
| 328 return TRUE; | |
| 329 | |
| 330 /* Lock if shared */ | |
| 331 if(SSLSESSION_SHARED(data)) | |
| 332 general_age = &data->share->sessionage; | |
| 333 else | |
| 334 general_age = &data->state.sessionage; | |
| 335 | |
| 336 for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) { | |
| 337 check = &data->state.session[i]; | |
| 338 if(!check->sessionid) | |
| 339 /* not session ID means blank entry */ | |
| 340 continue; | |
| 341 if(strcasecompare(name, check->name) && | |
| 342 ((!conn->bits.conn_to_host && !check->conn_to_host) || | |
| 343 (conn->bits.conn_to_host && check->conn_to_host && | |
| 344 strcasecompare(conn->conn_to_host.name, check->conn_to_host))) && | |
| 345 ((!conn->bits.conn_to_port && check->conn_to_port == -1) || | |
| 346 (conn->bits.conn_to_port && check->conn_to_port != -1 && | |
| 347 conn->conn_to_port == check->conn_to_port)) && | |
| 348 (port == check->remote_port) && | |
| 349 strcasecompare(conn->handler->scheme, check->scheme) && | |
| 350 Curl_ssl_config_matches(ssl_config, &check->ssl_config)) { | |
| 351 /* yes, we have a session ID! */ | |
| 352 (*general_age)++; /* increase general age */ | |
| 353 check->age = *general_age; /* set this as used in this age */ | |
| 354 *ssl_sessionid = check->sessionid; | |
| 355 if(idsize) | |
| 356 *idsize = check->idsize; | |
| 357 no_match = FALSE; | |
| 358 break; | |
| 359 } | |
| 360 } | |
| 361 | |
| 362 return no_match; | |
| 363 } | |
| 364 | |
| 365 /* | |
| 366 * Kill a single session ID entry in the cache. | |
| 367 */ | |
| 368 void Curl_ssl_kill_session(struct curl_ssl_session *session) | |
| 369 { | |
| 370 if(session->sessionid) { | |
| 371 /* defensive check */ | |
| 372 | |
| 373 /* free the ID the SSL-layer specific way */ | |
| 374 Curl_ssl->session_free(session->sessionid); | |
| 375 | |
| 376 session->sessionid = NULL; | |
| 377 session->age = 0; /* fresh */ | |
| 378 | |
| 379 Curl_free_primary_ssl_config(&session->ssl_config); | |
| 380 | |
| 381 Curl_safefree(session->name); | |
| 382 Curl_safefree(session->conn_to_host); | |
| 383 } | |
| 384 } | |
| 385 | |
| 386 /* | |
| 387 * Delete the given session ID from the cache. | |
| 388 */ | |
| 389 void Curl_ssl_delsessionid(struct connectdata *conn, void *ssl_sessionid) | |
| 390 { | |
| 391 size_t i; | |
| 392 struct Curl_easy *data = conn->data; | |
| 393 | |
| 394 for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) { | |
| 395 struct curl_ssl_session *check = &data->state.session[i]; | |
| 396 | |
| 397 if(check->sessionid == ssl_sessionid) { | |
| 398 Curl_ssl_kill_session(check); | |
| 399 break; | |
| 400 } | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 /* | |
| 405 * Store session id in the session cache. The ID passed on to this function | |
| 406 * must already have been extracted and allocated the proper way for the SSL | |
| 407 * layer. Curl_XXXX_session_free() will be called to free/kill the session ID | |
| 408 * later on. | |
| 409 */ | |
| 410 CURLcode Curl_ssl_addsessionid(struct connectdata *conn, | |
| 411 void *ssl_sessionid, | |
| 412 size_t idsize, | |
| 413 int sockindex) | |
| 414 { | |
| 415 size_t i; | |
| 416 struct Curl_easy *data = conn->data; /* the mother of all structs */ | |
| 417 struct curl_ssl_session *store = &data->state.session[0]; | |
| 418 long oldest_age = data->state.session[0].age; /* zero if unused */ | |
| 419 char *clone_host; | |
| 420 char *clone_conn_to_host; | |
| 421 int conn_to_port; | |
| 422 long *general_age; | |
| 423 const bool isProxy = CONNECT_PROXY_SSL(); | |
| 424 struct ssl_primary_config * const ssl_config = isProxy ? | |
| 425 &conn->proxy_ssl_config : | |
| 426 &conn->ssl_config; | |
| 427 | |
| 428 DEBUGASSERT(SSL_SET_OPTION(primary.sessionid)); | |
| 429 | |
| 430 clone_host = strdup(isProxy ? conn->http_proxy.host.name : conn->host.name); | |
| 431 if(!clone_host) | |
| 432 return CURLE_OUT_OF_MEMORY; /* bail out */ | |
| 433 | |
| 434 if(conn->bits.conn_to_host) { | |
| 435 clone_conn_to_host = strdup(conn->conn_to_host.name); | |
| 436 if(!clone_conn_to_host) { | |
| 437 free(clone_host); | |
| 438 return CURLE_OUT_OF_MEMORY; /* bail out */ | |
| 439 } | |
| 440 } | |
| 441 else | |
| 442 clone_conn_to_host = NULL; | |
| 443 | |
| 444 if(conn->bits.conn_to_port) | |
| 445 conn_to_port = conn->conn_to_port; | |
| 446 else | |
| 447 conn_to_port = -1; | |
| 448 | |
| 449 /* Now we should add the session ID and the host name to the cache, (remove | |
| 450 the oldest if necessary) */ | |
| 451 | |
| 452 /* If using shared SSL session, lock! */ | |
| 453 if(SSLSESSION_SHARED(data)) { | |
| 454 general_age = &data->share->sessionage; | |
| 455 } | |
| 456 else { | |
| 457 general_age = &data->state.sessionage; | |
| 458 } | |
| 459 | |
| 460 /* find an empty slot for us, or find the oldest */ | |
| 461 for(i = 1; (i < data->set.general_ssl.max_ssl_sessions) && | |
| 462 data->state.session[i].sessionid; i++) { | |
| 463 if(data->state.session[i].age < oldest_age) { | |
| 464 oldest_age = data->state.session[i].age; | |
| 465 store = &data->state.session[i]; | |
| 466 } | |
| 467 } | |
| 468 if(i == data->set.general_ssl.max_ssl_sessions) | |
| 469 /* cache is full, we must "kill" the oldest entry! */ | |
| 470 Curl_ssl_kill_session(store); | |
| 471 else | |
| 472 store = &data->state.session[i]; /* use this slot */ | |
| 473 | |
| 474 /* now init the session struct wisely */ | |
| 475 store->sessionid = ssl_sessionid; | |
| 476 store->idsize = idsize; | |
| 477 store->age = *general_age; /* set current age */ | |
| 478 /* free it if there's one already present */ | |
| 479 free(store->name); | |
| 480 free(store->conn_to_host); | |
| 481 store->name = clone_host; /* clone host name */ | |
| 482 store->conn_to_host = clone_conn_to_host; /* clone connect to host name */ | |
| 483 store->conn_to_port = conn_to_port; /* connect to port number */ | |
| 484 /* port number */ | |
| 485 store->remote_port = isProxy ? (int)conn->port : conn->remote_port; | |
| 486 store->scheme = conn->handler->scheme; | |
| 487 | |
| 488 if(!Curl_clone_primary_ssl_config(ssl_config, &store->ssl_config)) { | |
| 489 store->sessionid = NULL; /* let caller free sessionid */ | |
| 490 free(clone_host); | |
| 491 free(clone_conn_to_host); | |
| 492 return CURLE_OUT_OF_MEMORY; | |
| 493 } | |
| 494 | |
| 495 return CURLE_OK; | |
| 496 } | |
| 497 | |
| 498 | |
| 499 void Curl_ssl_close_all(struct Curl_easy *data) | |
| 500 { | |
| 501 /* kill the session ID cache if not shared */ | |
| 502 if(data->state.session && !SSLSESSION_SHARED(data)) { | |
| 503 size_t i; | |
| 504 for(i = 0; i < data->set.general_ssl.max_ssl_sessions; i++) | |
| 505 /* the single-killer function handles empty table slots */ | |
| 506 Curl_ssl_kill_session(&data->state.session[i]); | |
| 507 | |
| 508 /* free the cache data */ | |
| 509 Curl_safefree(data->state.session); | |
| 510 } | |
| 511 | |
| 512 Curl_ssl->close_all(data); | |
| 513 } | |
| 514 | |
| 515 #if defined(USE_OPENSSL) || defined(USE_GNUTLS) || defined(USE_SCHANNEL) || \ | |
| 516 defined(USE_SECTRANSP) || defined(USE_POLARSSL) || defined(USE_NSS) || \ | |
| 517 defined(USE_MBEDTLS) || defined(USE_WOLFSSL) | |
| 518 int Curl_ssl_getsock(struct connectdata *conn, curl_socket_t *socks) | |
| 519 { | |
| 520 struct ssl_connect_data *connssl = &conn->ssl[FIRSTSOCKET]; | |
| 521 | |
| 522 if(connssl->connecting_state == ssl_connect_2_writing) { | |
| 523 /* write mode */ | |
| 524 socks[0] = conn->sock[FIRSTSOCKET]; | |
| 525 return GETSOCK_WRITESOCK(0); | |
| 526 } | |
| 527 if(connssl->connecting_state == ssl_connect_2_reading) { | |
| 528 /* read mode */ | |
| 529 socks[0] = conn->sock[FIRSTSOCKET]; | |
| 530 return GETSOCK_READSOCK(0); | |
| 531 } | |
| 532 | |
| 533 return GETSOCK_BLANK; | |
| 534 } | |
| 535 #else | |
| 536 int Curl_ssl_getsock(struct connectdata *conn, | |
| 537 curl_socket_t *socks) | |
| 538 { | |
| 539 (void)conn; | |
| 540 (void)socks; | |
| 541 return GETSOCK_BLANK; | |
| 542 } | |
| 543 /* USE_OPENSSL || USE_GNUTLS || USE_SCHANNEL || USE_SECTRANSP || USE_NSS */ | |
| 544 #endif | |
| 545 | |
| 546 void Curl_ssl_close(struct connectdata *conn, int sockindex) | |
| 547 { | |
| 548 DEBUGASSERT((sockindex <= 1) && (sockindex >= -1)); | |
| 549 Curl_ssl->close_one(conn, sockindex); | |
| 550 } | |
| 551 | |
| 552 CURLcode Curl_ssl_shutdown(struct connectdata *conn, int sockindex) | |
| 553 { | |
| 554 if(Curl_ssl->shut_down(conn, sockindex)) | |
| 555 return CURLE_SSL_SHUTDOWN_FAILED; | |
| 556 | |
| 557 conn->ssl[sockindex].use = FALSE; /* get back to ordinary socket usage */ | |
| 558 conn->ssl[sockindex].state = ssl_connection_none; | |
| 559 | |
| 560 conn->recv[sockindex] = Curl_recv_plain; | |
| 561 conn->send[sockindex] = Curl_send_plain; | |
| 562 | |
| 563 return CURLE_OK; | |
| 564 } | |
| 565 | |
| 566 /* Selects an SSL crypto engine | |
| 567 */ | |
| 568 CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine) | |
| 569 { | |
| 570 return Curl_ssl->set_engine(data, engine); | |
| 571 } | |
| 572 | |
| 573 /* Selects the default SSL crypto engine | |
| 574 */ | |
| 575 CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data) | |
| 576 { | |
| 577 return Curl_ssl->set_engine_default(data); | |
| 578 } | |
| 579 | |
| 580 /* Return list of OpenSSL crypto engine names. */ | |
| 581 struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data) | |
| 582 { | |
| 583 return Curl_ssl->engines_list(data); | |
| 584 } | |
| 585 | |
| 586 /* | |
| 587 * This sets up a session ID cache to the specified size. Make sure this code | |
| 588 * is agnostic to what underlying SSL technology we use. | |
| 589 */ | |
| 590 CURLcode Curl_ssl_initsessions(struct Curl_easy *data, size_t amount) | |
| 591 { | |
| 592 struct curl_ssl_session *session; | |
| 593 | |
| 594 if(data->state.session) | |
| 595 /* this is just a precaution to prevent multiple inits */ | |
| 596 return CURLE_OK; | |
| 597 | |
| 598 session = calloc(amount, sizeof(struct curl_ssl_session)); | |
| 599 if(!session) | |
| 600 return CURLE_OUT_OF_MEMORY; | |
| 601 | |
| 602 /* store the info in the SSL section */ | |
| 603 data->set.general_ssl.max_ssl_sessions = amount; | |
| 604 data->state.session = session; | |
| 605 data->state.sessionage = 1; /* this is brand new */ | |
| 606 return CURLE_OK; | |
| 607 } | |
| 608 | |
| 609 static size_t Curl_multissl_version(char *buffer, size_t size); | |
| 610 | |
| 611 size_t Curl_ssl_version(char *buffer, size_t size) | |
| 612 { | |
| 613 #ifdef CURL_WITH_MULTI_SSL | |
| 614 return Curl_multissl_version(buffer, size); | |
| 615 #else | |
| 616 return Curl_ssl->version(buffer, size); | |
| 617 #endif | |
| 618 } | |
| 619 | |
| 620 /* | |
| 621 * This function tries to determine connection status. | |
| 622 * | |
| 623 * Return codes: | |
| 624 * 1 means the connection is still in place | |
| 625 * 0 means the connection has been closed | |
| 626 * -1 means the connection status is unknown | |
| 627 */ | |
| 628 int Curl_ssl_check_cxn(struct connectdata *conn) | |
| 629 { | |
| 630 return Curl_ssl->check_cxn(conn); | |
| 631 } | |
| 632 | |
| 633 bool Curl_ssl_data_pending(const struct connectdata *conn, | |
| 634 int connindex) | |
| 635 { | |
| 636 return Curl_ssl->data_pending(conn, connindex); | |
| 637 } | |
| 638 | |
| 639 void Curl_ssl_free_certinfo(struct Curl_easy *data) | |
| 640 { | |
| 641 struct curl_certinfo *ci = &data->info.certs; | |
| 642 | |
| 643 if(ci->num_of_certs) { | |
| 644 /* free all individual lists used */ | |
| 645 int i; | |
| 646 for(i = 0; i<ci->num_of_certs; i++) { | |
| 647 curl_slist_free_all(ci->certinfo[i]); | |
| 648 ci->certinfo[i] = NULL; | |
| 649 } | |
| 650 | |
| 651 free(ci->certinfo); /* free the actual array too */ | |
| 652 ci->certinfo = NULL; | |
| 653 ci->num_of_certs = 0; | |
| 654 } | |
| 655 } | |
| 656 | |
| 657 CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num) | |
| 658 { | |
| 659 struct curl_certinfo *ci = &data->info.certs; | |
| 660 struct curl_slist **table; | |
| 661 | |
| 662 /* Free any previous certificate information structures */ | |
| 663 Curl_ssl_free_certinfo(data); | |
| 664 | |
| 665 /* Allocate the required certificate information structures */ | |
| 666 table = calloc((size_t) num, sizeof(struct curl_slist *)); | |
| 667 if(!table) | |
| 668 return CURLE_OUT_OF_MEMORY; | |
| 669 | |
| 670 ci->num_of_certs = num; | |
| 671 ci->certinfo = table; | |
| 672 | |
| 673 return CURLE_OK; | |
| 674 } | |
| 675 | |
| 676 /* | |
| 677 * 'value' is NOT a zero terminated string | |
| 678 */ | |
| 679 CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, | |
| 680 int certnum, | |
| 681 const char *label, | |
| 682 const char *value, | |
| 683 size_t valuelen) | |
| 684 { | |
| 685 struct curl_certinfo *ci = &data->info.certs; | |
| 686 char *output; | |
| 687 struct curl_slist *nl; | |
| 688 CURLcode result = CURLE_OK; | |
| 689 size_t labellen = strlen(label); | |
| 690 size_t outlen = labellen + 1 + valuelen + 1; /* label:value\0 */ | |
| 691 | |
| 692 output = malloc(outlen); | |
| 693 if(!output) | |
| 694 return CURLE_OUT_OF_MEMORY; | |
| 695 | |
| 696 /* sprintf the label and colon */ | |
| 697 msnprintf(output, outlen, "%s:", label); | |
| 698 | |
| 699 /* memcpy the value (it might not be zero terminated) */ | |
| 700 memcpy(&output[labellen + 1], value, valuelen); | |
| 701 | |
| 702 /* zero terminate the output */ | |
| 703 output[labellen + 1 + valuelen] = 0; | |
| 704 | |
| 705 nl = Curl_slist_append_nodup(ci->certinfo[certnum], output); | |
| 706 if(!nl) { | |
| 707 free(output); | |
| 708 curl_slist_free_all(ci->certinfo[certnum]); | |
| 709 result = CURLE_OUT_OF_MEMORY; | |
| 710 } | |
| 711 | |
| 712 ci->certinfo[certnum] = nl; | |
| 713 return result; | |
| 714 } | |
| 715 | |
| 716 /* | |
| 717 * This is a convenience function for push_certinfo_len that takes a zero | |
| 718 * terminated value. | |
| 719 */ | |
| 720 CURLcode Curl_ssl_push_certinfo(struct Curl_easy *data, | |
| 721 int certnum, | |
| 722 const char *label, | |
| 723 const char *value) | |
| 724 { | |
| 725 size_t valuelen = strlen(value); | |
| 726 | |
| 727 return Curl_ssl_push_certinfo_len(data, certnum, label, value, valuelen); | |
| 728 } | |
| 729 | |
| 730 CURLcode Curl_ssl_random(struct Curl_easy *data, | |
| 731 unsigned char *entropy, | |
| 732 size_t length) | |
| 733 { | |
| 734 return Curl_ssl->random(data, entropy, length); | |
| 735 } | |
| 736 | |
| 737 /* | |
| 738 * Public key pem to der conversion | |
| 739 */ | |
| 740 | |
| 741 static CURLcode pubkey_pem_to_der(const char *pem, | |
| 742 unsigned char **der, size_t *der_len) | |
| 743 { | |
| 744 char *stripped_pem, *begin_pos, *end_pos; | |
| 745 size_t pem_count, stripped_pem_count = 0, pem_len; | |
| 746 CURLcode result; | |
| 747 | |
| 748 /* if no pem, exit. */ | |
| 749 if(!pem) | |
| 750 return CURLE_BAD_CONTENT_ENCODING; | |
| 751 | |
| 752 begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----"); | |
| 753 if(!begin_pos) | |
| 754 return CURLE_BAD_CONTENT_ENCODING; | |
| 755 | |
| 756 pem_count = begin_pos - pem; | |
| 757 /* Invalid if not at beginning AND not directly following \n */ | |
| 758 if(0 != pem_count && '\n' != pem[pem_count - 1]) | |
| 759 return CURLE_BAD_CONTENT_ENCODING; | |
| 760 | |
| 761 /* 26 is length of "-----BEGIN PUBLIC KEY-----" */ | |
| 762 pem_count += 26; | |
| 763 | |
| 764 /* Invalid if not directly following \n */ | |
| 765 end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----"); | |
| 766 if(!end_pos) | |
| 767 return CURLE_BAD_CONTENT_ENCODING; | |
| 768 | |
| 769 pem_len = end_pos - pem; | |
| 770 | |
| 771 stripped_pem = malloc(pem_len - pem_count + 1); | |
| 772 if(!stripped_pem) | |
| 773 return CURLE_OUT_OF_MEMORY; | |
| 774 | |
| 775 /* | |
| 776 * Here we loop through the pem array one character at a time between the | |
| 777 * correct indices, and place each character that is not '\n' or '\r' | |
| 778 * into the stripped_pem array, which should represent the raw base64 string | |
| 779 */ | |
| 780 while(pem_count < pem_len) { | |
| 781 if('\n' != pem[pem_count] && '\r' != pem[pem_count]) | |
| 782 stripped_pem[stripped_pem_count++] = pem[pem_count]; | |
| 783 ++pem_count; | |
| 784 } | |
| 785 /* Place the null terminator in the correct place */ | |
| 786 stripped_pem[stripped_pem_count] = '\0'; | |
| 787 | |
| 788 result = Curl_base64_decode(stripped_pem, der, der_len); | |
| 789 | |
| 790 Curl_safefree(stripped_pem); | |
| 791 | |
| 792 return result; | |
| 793 } | |
| 794 | |
| 795 /* | |
| 796 * Generic pinned public key check. | |
| 797 */ | |
| 798 | |
| 799 CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data, | |
| 800 const char *pinnedpubkey, | |
| 801 const unsigned char *pubkey, size_t pubkeylen) | |
| 802 { | |
| 803 FILE *fp; | |
| 804 unsigned char *buf = NULL, *pem_ptr = NULL; | |
| 805 CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; | |
| 806 | |
| 807 /* if a path wasn't specified, don't pin */ | |
| 808 if(!pinnedpubkey) | |
| 809 return CURLE_OK; | |
| 810 if(!pubkey || !pubkeylen) | |
| 811 return result; | |
| 812 | |
| 813 /* only do this if pinnedpubkey starts with "sha256//", length 8 */ | |
| 814 if(strncmp(pinnedpubkey, "sha256//", 8) == 0) { | |
| 815 CURLcode encode; | |
| 816 size_t encodedlen, pinkeylen; | |
| 817 char *encoded, *pinkeycopy, *begin_pos, *end_pos; | |
| 818 unsigned char *sha256sumdigest; | |
| 819 | |
| 820 if(!Curl_ssl->sha256sum) { | |
| 821 /* without sha256 support, this cannot match */ | |
| 822 return result; | |
| 823 } | |
| 824 | |
| 825 /* compute sha256sum of public key */ | |
| 826 sha256sumdigest = malloc(CURL_SHA256_DIGEST_LENGTH); | |
| 827 if(!sha256sumdigest) | |
| 828 return CURLE_OUT_OF_MEMORY; | |
| 829 encode = Curl_ssl->sha256sum(pubkey, pubkeylen, | |
| 830 sha256sumdigest, CURL_SHA256_DIGEST_LENGTH); | |
| 831 | |
| 832 if(encode != CURLE_OK) | |
| 833 return encode; | |
| 834 | |
| 835 encode = Curl_base64_encode(data, (char *)sha256sumdigest, | |
| 836 CURL_SHA256_DIGEST_LENGTH, &encoded, | |
| 837 &encodedlen); | |
| 838 Curl_safefree(sha256sumdigest); | |
| 839 | |
| 840 if(encode) | |
| 841 return encode; | |
| 842 | |
| 843 infof(data, "\t public key hash: sha256//%s\n", encoded); | |
| 844 | |
| 845 /* it starts with sha256//, copy so we can modify it */ | |
| 846 pinkeylen = strlen(pinnedpubkey) + 1; | |
| 847 pinkeycopy = malloc(pinkeylen); | |
| 848 if(!pinkeycopy) { | |
| 849 Curl_safefree(encoded); | |
| 850 return CURLE_OUT_OF_MEMORY; | |
| 851 } | |
| 852 memcpy(pinkeycopy, pinnedpubkey, pinkeylen); | |
| 853 /* point begin_pos to the copy, and start extracting keys */ | |
| 854 begin_pos = pinkeycopy; | |
| 855 do { | |
| 856 end_pos = strstr(begin_pos, ";sha256//"); | |
| 857 /* | |
| 858 * if there is an end_pos, null terminate, | |
| 859 * otherwise it'll go to the end of the original string | |
| 860 */ | |
| 861 if(end_pos) | |
| 862 end_pos[0] = '\0'; | |
| 863 | |
| 864 /* compare base64 sha256 digests, 8 is the length of "sha256//" */ | |
| 865 if(encodedlen == strlen(begin_pos + 8) && | |
| 866 !memcmp(encoded, begin_pos + 8, encodedlen)) { | |
| 867 result = CURLE_OK; | |
| 868 break; | |
| 869 } | |
| 870 | |
| 871 /* | |
| 872 * change back the null-terminator we changed earlier, | |
| 873 * and look for next begin | |
| 874 */ | |
| 875 if(end_pos) { | |
| 876 end_pos[0] = ';'; | |
| 877 begin_pos = strstr(end_pos, "sha256//"); | |
| 878 } | |
| 879 } while(end_pos && begin_pos); | |
| 880 Curl_safefree(encoded); | |
| 881 Curl_safefree(pinkeycopy); | |
| 882 return result; | |
| 883 } | |
| 884 | |
| 885 fp = fopen(pinnedpubkey, "rb"); | |
| 886 if(!fp) | |
| 887 return result; | |
| 888 | |
| 889 do { | |
| 890 long filesize; | |
| 891 size_t size, pem_len; | |
| 892 CURLcode pem_read; | |
| 893 | |
| 894 /* Determine the file's size */ | |
| 895 if(fseek(fp, 0, SEEK_END)) | |
| 896 break; | |
| 897 filesize = ftell(fp); | |
| 898 if(fseek(fp, 0, SEEK_SET)) | |
| 899 break; | |
| 900 if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE) | |
| 901 break; | |
| 902 | |
| 903 /* | |
| 904 * if the size of our certificate is bigger than the file | |
| 905 * size then it can't match | |
| 906 */ | |
| 907 size = curlx_sotouz((curl_off_t) filesize); | |
| 908 if(pubkeylen > size) | |
| 909 break; | |
| 910 | |
| 911 /* | |
| 912 * Allocate buffer for the pinned key | |
| 913 * With 1 additional byte for null terminator in case of PEM key | |
| 914 */ | |
| 915 buf = malloc(size + 1); | |
| 916 if(!buf) | |
| 917 break; | |
| 918 | |
| 919 /* Returns number of elements read, which should be 1 */ | |
| 920 if((int) fread(buf, size, 1, fp) != 1) | |
| 921 break; | |
| 922 | |
| 923 /* If the sizes are the same, it can't be base64 encoded, must be der */ | |
| 924 if(pubkeylen == size) { | |
| 925 if(!memcmp(pubkey, buf, pubkeylen)) | |
| 926 result = CURLE_OK; | |
| 927 break; | |
| 928 } | |
| 929 | |
| 930 /* | |
| 931 * Otherwise we will assume it's PEM and try to decode it | |
| 932 * after placing null terminator | |
| 933 */ | |
| 934 buf[size] = '\0'; | |
| 935 pem_read = pubkey_pem_to_der((const char *)buf, &pem_ptr, &pem_len); | |
| 936 /* if it wasn't read successfully, exit */ | |
| 937 if(pem_read) | |
| 938 break; | |
| 939 | |
| 940 /* | |
| 941 * if the size of our certificate doesn't match the size of | |
| 942 * the decoded file, they can't be the same, otherwise compare | |
| 943 */ | |
| 944 if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen)) | |
| 945 result = CURLE_OK; | |
| 946 } while(0); | |
| 947 | |
| 948 Curl_safefree(buf); | |
| 949 Curl_safefree(pem_ptr); | |
| 950 fclose(fp); | |
| 951 | |
| 952 return result; | |
| 953 } | |
| 954 | |
| 955 #ifndef CURL_DISABLE_CRYPTO_AUTH | |
| 956 CURLcode Curl_ssl_md5sum(unsigned char *tmp, /* input */ | |
| 957 size_t tmplen, | |
| 958 unsigned char *md5sum, /* output */ | |
| 959 size_t md5len) | |
| 960 { | |
| 961 return Curl_ssl->md5sum(tmp, tmplen, md5sum, md5len); | |
| 962 } | |
| 963 #endif | |
| 964 | |
| 965 /* | |
| 966 * Check whether the SSL backend supports the status_request extension. | |
| 967 */ | |
| 968 bool Curl_ssl_cert_status_request(void) | |
| 969 { | |
| 970 return Curl_ssl->cert_status_request(); | |
| 971 } | |
| 972 | |
| 973 /* | |
| 974 * Check whether the SSL backend supports false start. | |
| 975 */ | |
| 976 bool Curl_ssl_false_start(void) | |
| 977 { | |
| 978 return Curl_ssl->false_start(); | |
| 979 } | |
| 980 | |
| 981 /* | |
| 982 * Check whether the SSL backend supports setting TLS 1.3 cipher suites | |
| 983 */ | |
| 984 bool Curl_ssl_tls13_ciphersuites(void) | |
| 985 { | |
| 986 return Curl_ssl->supports & SSLSUPP_TLS13_CIPHERSUITES; | |
| 987 } | |
| 988 | |
| 989 /* | |
| 990 * Default implementations for unsupported functions. | |
| 991 */ | |
| 992 | |
| 993 int Curl_none_init(void) | |
| 994 { | |
| 995 return 1; | |
| 996 } | |
| 997 | |
| 998 void Curl_none_cleanup(void) | |
| 999 { } | |
| 1000 | |
| 1001 int Curl_none_shutdown(struct connectdata *conn UNUSED_PARAM, | |
| 1002 int sockindex UNUSED_PARAM) | |
| 1003 { | |
| 1004 (void)conn; | |
| 1005 (void)sockindex; | |
| 1006 return 0; | |
| 1007 } | |
| 1008 | |
| 1009 int Curl_none_check_cxn(struct connectdata *conn UNUSED_PARAM) | |
| 1010 { | |
| 1011 (void)conn; | |
| 1012 return -1; | |
| 1013 } | |
| 1014 | |
| 1015 CURLcode Curl_none_random(struct Curl_easy *data UNUSED_PARAM, | |
| 1016 unsigned char *entropy UNUSED_PARAM, | |
| 1017 size_t length UNUSED_PARAM) | |
| 1018 { | |
| 1019 (void)data; | |
| 1020 (void)entropy; | |
| 1021 (void)length; | |
| 1022 return CURLE_NOT_BUILT_IN; | |
| 1023 } | |
| 1024 | |
| 1025 void Curl_none_close_all(struct Curl_easy *data UNUSED_PARAM) | |
| 1026 { | |
| 1027 (void)data; | |
| 1028 } | |
| 1029 | |
| 1030 void Curl_none_session_free(void *ptr UNUSED_PARAM) | |
| 1031 { | |
| 1032 (void)ptr; | |
| 1033 } | |
| 1034 | |
| 1035 bool Curl_none_data_pending(const struct connectdata *conn UNUSED_PARAM, | |
| 1036 int connindex UNUSED_PARAM) | |
| 1037 { | |
| 1038 (void)conn; | |
| 1039 (void)connindex; | |
| 1040 return 0; | |
| 1041 } | |
| 1042 | |
| 1043 bool Curl_none_cert_status_request(void) | |
| 1044 { | |
| 1045 return FALSE; | |
| 1046 } | |
| 1047 | |
| 1048 CURLcode Curl_none_set_engine(struct Curl_easy *data UNUSED_PARAM, | |
| 1049 const char *engine UNUSED_PARAM) | |
| 1050 { | |
| 1051 (void)data; | |
| 1052 (void)engine; | |
| 1053 return CURLE_NOT_BUILT_IN; | |
| 1054 } | |
| 1055 | |
| 1056 CURLcode Curl_none_set_engine_default(struct Curl_easy *data UNUSED_PARAM) | |
| 1057 { | |
| 1058 (void)data; | |
| 1059 return CURLE_NOT_BUILT_IN; | |
| 1060 } | |
| 1061 | |
| 1062 struct curl_slist *Curl_none_engines_list(struct Curl_easy *data UNUSED_PARAM) | |
| 1063 { | |
| 1064 (void)data; | |
| 1065 return (struct curl_slist *)NULL; | |
| 1066 } | |
| 1067 | |
| 1068 bool Curl_none_false_start(void) | |
| 1069 { | |
| 1070 return FALSE; | |
| 1071 } | |
| 1072 | |
| 1073 #ifndef CURL_DISABLE_CRYPTO_AUTH | |
| 1074 CURLcode Curl_none_md5sum(unsigned char *input, size_t inputlen, | |
| 1075 unsigned char *md5sum, size_t md5len UNUSED_PARAM) | |
| 1076 { | |
| 1077 MD5_context *MD5pw; | |
| 1078 | |
| 1079 (void)md5len; | |
| 1080 | |
| 1081 MD5pw = Curl_MD5_init(Curl_DIGEST_MD5); | |
| 1082 if(!MD5pw) | |
| 1083 return CURLE_OUT_OF_MEMORY; | |
| 1084 Curl_MD5_update(MD5pw, input, curlx_uztoui(inputlen)); | |
| 1085 Curl_MD5_final(MD5pw, md5sum); | |
| 1086 return CURLE_OK; | |
| 1087 } | |
| 1088 #else | |
| 1089 CURLcode Curl_none_md5sum(unsigned char *input UNUSED_PARAM, | |
| 1090 size_t inputlen UNUSED_PARAM, | |
| 1091 unsigned char *md5sum UNUSED_PARAM, | |
| 1092 size_t md5len UNUSED_PARAM) | |
| 1093 { | |
| 1094 (void)input; | |
| 1095 (void)inputlen; | |
| 1096 (void)md5sum; | |
| 1097 (void)md5len; | |
| 1098 return CURLE_NOT_BUILT_IN; | |
| 1099 } | |
| 1100 #endif | |
| 1101 | |
| 1102 static int Curl_multissl_init(void) | |
| 1103 { | |
| 1104 if(multissl_init(NULL)) | |
| 1105 return 1; | |
| 1106 return Curl_ssl->init(); | |
| 1107 } | |
| 1108 | |
| 1109 static CURLcode Curl_multissl_connect(struct connectdata *conn, int sockindex) | |
| 1110 { | |
| 1111 if(multissl_init(NULL)) | |
| 1112 return CURLE_FAILED_INIT; | |
| 1113 return Curl_ssl->connect_blocking(conn, sockindex); | |
| 1114 } | |
| 1115 | |
| 1116 static CURLcode Curl_multissl_connect_nonblocking(struct connectdata *conn, | |
| 1117 int sockindex, bool *done) | |
| 1118 { | |
| 1119 if(multissl_init(NULL)) | |
| 1120 return CURLE_FAILED_INIT; | |
| 1121 return Curl_ssl->connect_nonblocking(conn, sockindex, done); | |
| 1122 } | |
| 1123 | |
| 1124 static void *Curl_multissl_get_internals(struct ssl_connect_data *connssl, | |
| 1125 CURLINFO info) | |
| 1126 { | |
| 1127 if(multissl_init(NULL)) | |
| 1128 return NULL; | |
| 1129 return Curl_ssl->get_internals(connssl, info); | |
| 1130 } | |
| 1131 | |
| 1132 static void Curl_multissl_close(struct connectdata *conn, int sockindex) | |
| 1133 { | |
| 1134 if(multissl_init(NULL)) | |
| 1135 return; | |
| 1136 Curl_ssl->close_one(conn, sockindex); | |
| 1137 } | |
| 1138 | |
| 1139 static const struct Curl_ssl Curl_ssl_multi = { | |
| 1140 { CURLSSLBACKEND_NONE, "multi" }, /* info */ | |
| 1141 0, /* supports nothing */ | |
| 1142 (size_t)-1, /* something insanely large to be on the safe side */ | |
| 1143 | |
| 1144 Curl_multissl_init, /* init */ | |
| 1145 Curl_none_cleanup, /* cleanup */ | |
| 1146 Curl_multissl_version, /* version */ | |
| 1147 Curl_none_check_cxn, /* check_cxn */ | |
| 1148 Curl_none_shutdown, /* shutdown */ | |
| 1149 Curl_none_data_pending, /* data_pending */ | |
| 1150 Curl_none_random, /* random */ | |
| 1151 Curl_none_cert_status_request, /* cert_status_request */ | |
| 1152 Curl_multissl_connect, /* connect */ | |
| 1153 Curl_multissl_connect_nonblocking, /* connect_nonblocking */ | |
| 1154 Curl_multissl_get_internals, /* get_internals */ | |
| 1155 Curl_multissl_close, /* close_one */ | |
| 1156 Curl_none_close_all, /* close_all */ | |
| 1157 Curl_none_session_free, /* session_free */ | |
| 1158 Curl_none_set_engine, /* set_engine */ | |
| 1159 Curl_none_set_engine_default, /* set_engine_default */ | |
| 1160 Curl_none_engines_list, /* engines_list */ | |
| 1161 Curl_none_false_start, /* false_start */ | |
| 1162 Curl_none_md5sum, /* md5sum */ | |
| 1163 NULL /* sha256sum */ | |
| 1164 }; | |
| 1165 | |
| 1166 const struct Curl_ssl *Curl_ssl = | |
| 1167 #if defined(CURL_WITH_MULTI_SSL) | |
| 1168 &Curl_ssl_multi; | |
| 1169 #elif defined(USE_WOLFSSL) | |
| 1170 &Curl_ssl_wolfssl; | |
| 1171 #elif defined(USE_SECTRANSP) | |
| 1172 &Curl_ssl_sectransp; | |
| 1173 #elif defined(USE_GNUTLS) | |
| 1174 &Curl_ssl_gnutls; | |
| 1175 #elif defined(USE_GSKIT) | |
| 1176 &Curl_ssl_gskit; | |
| 1177 #elif defined(USE_MBEDTLS) | |
| 1178 &Curl_ssl_mbedtls; | |
| 1179 #elif defined(USE_NSS) | |
| 1180 &Curl_ssl_nss; | |
| 1181 #elif defined(USE_OPENSSL) | |
| 1182 &Curl_ssl_openssl; | |
| 1183 #elif defined(USE_POLARSSL) | |
| 1184 &Curl_ssl_polarssl; | |
| 1185 #elif defined(USE_SCHANNEL) | |
| 1186 &Curl_ssl_schannel; | |
| 1187 #elif defined(USE_MESALINK) | |
| 1188 &Curl_ssl_mesalink; | |
| 1189 #else | |
| 1190 #error "Missing struct Curl_ssl for selected SSL backend" | |
| 1191 #endif | |
| 1192 | |
| 1193 static const struct Curl_ssl *available_backends[] = { | |
| 1194 #if defined(USE_WOLFSSL) | |
| 1195 &Curl_ssl_wolfssl, | |
| 1196 #endif | |
| 1197 #if defined(USE_SECTRANSP) | |
| 1198 &Curl_ssl_sectransp, | |
| 1199 #endif | |
| 1200 #if defined(USE_GNUTLS) | |
| 1201 &Curl_ssl_gnutls, | |
| 1202 #endif | |
| 1203 #if defined(USE_GSKIT) | |
| 1204 &Curl_ssl_gskit, | |
| 1205 #endif | |
| 1206 #if defined(USE_MBEDTLS) | |
| 1207 &Curl_ssl_mbedtls, | |
| 1208 #endif | |
| 1209 #if defined(USE_NSS) | |
| 1210 &Curl_ssl_nss, | |
| 1211 #endif | |
| 1212 #if defined(USE_OPENSSL) | |
| 1213 &Curl_ssl_openssl, | |
| 1214 #endif | |
| 1215 #if defined(USE_POLARSSL) | |
| 1216 &Curl_ssl_polarssl, | |
| 1217 #endif | |
| 1218 #if defined(USE_SCHANNEL) | |
| 1219 &Curl_ssl_schannel, | |
| 1220 #endif | |
| 1221 #if defined(USE_MESALINK) | |
| 1222 &Curl_ssl_mesalink, | |
| 1223 #endif | |
| 1224 NULL | |
| 1225 }; | |
| 1226 | |
| 1227 static size_t Curl_multissl_version(char *buffer, size_t size) | |
| 1228 { | |
| 1229 static const struct Curl_ssl *selected; | |
| 1230 static char backends[200]; | |
| 1231 static size_t total; | |
| 1232 const struct Curl_ssl *current; | |
| 1233 | |
| 1234 current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl; | |
| 1235 | |
| 1236 if(current != selected) { | |
| 1237 char *p = backends; | |
| 1238 char *end = backends + sizeof(backends); | |
| 1239 int i; | |
| 1240 | |
| 1241 selected = current; | |
| 1242 | |
| 1243 for(i = 0; available_backends[i] && p < (end - 4); i++) { | |
| 1244 if(i) | |
| 1245 *(p++) = ' '; | |
| 1246 if(selected != available_backends[i]) | |
| 1247 *(p++) = '('; | |
| 1248 p += available_backends[i]->version(p, end - p - 2); | |
| 1249 if(selected != available_backends[i]) | |
| 1250 *(p++) = ')'; | |
| 1251 } | |
| 1252 *p = '\0'; | |
| 1253 total = p - backends; | |
| 1254 } | |
| 1255 | |
| 1256 if(size > total) | |
| 1257 memcpy(buffer, backends, total + 1); | |
| 1258 else { | |
| 1259 memcpy(buffer, backends, size - 1); | |
| 1260 buffer[size - 1] = '\0'; | |
| 1261 } | |
| 1262 | |
| 1263 return CURLMIN(size - 1, total); | |
| 1264 } | |
| 1265 | |
| 1266 static int multissl_init(const struct Curl_ssl *backend) | |
| 1267 { | |
| 1268 const char *env; | |
| 1269 char *env_tmp; | |
| 1270 | |
| 1271 if(Curl_ssl != &Curl_ssl_multi) | |
| 1272 return 1; | |
| 1273 | |
| 1274 if(backend) { | |
| 1275 Curl_ssl = backend; | |
| 1276 return 0; | |
| 1277 } | |
| 1278 | |
| 1279 if(!available_backends[0]) | |
| 1280 return 1; | |
| 1281 | |
| 1282 env = env_tmp = curl_getenv("CURL_SSL_BACKEND"); | |
| 1283 #ifdef CURL_DEFAULT_SSL_BACKEND | |
| 1284 if(!env) | |
| 1285 env = CURL_DEFAULT_SSL_BACKEND; | |
| 1286 #endif | |
| 1287 if(env) { | |
| 1288 int i; | |
| 1289 for(i = 0; available_backends[i]; i++) { | |
| 1290 if(strcasecompare(env, available_backends[i]->info.name)) { | |
| 1291 Curl_ssl = available_backends[i]; | |
| 1292 curl_free(env_tmp); | |
| 1293 return 0; | |
| 1294 } | |
| 1295 } | |
| 1296 } | |
| 1297 | |
| 1298 /* Fall back to first available backend */ | |
| 1299 Curl_ssl = available_backends[0]; | |
| 1300 curl_free(env_tmp); | |
| 1301 return 0; | |
| 1302 } | |
| 1303 | |
| 1304 CURLsslset curl_global_sslset(curl_sslbackend id, const char *name, | |
| 1305 const curl_ssl_backend ***avail) | |
| 1306 { | |
| 1307 int i; | |
| 1308 | |
| 1309 if(avail) | |
| 1310 *avail = (const curl_ssl_backend **)&available_backends; | |
| 1311 | |
| 1312 if(Curl_ssl != &Curl_ssl_multi) | |
| 1313 return id == Curl_ssl->info.id || | |
| 1314 (name && strcasecompare(name, Curl_ssl->info.name)) ? | |
| 1315 CURLSSLSET_OK : | |
| 1316 #if defined(CURL_WITH_MULTI_SSL) | |
| 1317 CURLSSLSET_TOO_LATE; | |
| 1318 #else | |
| 1319 CURLSSLSET_UNKNOWN_BACKEND; | |
| 1320 #endif | |
| 1321 | |
| 1322 for(i = 0; available_backends[i]; i++) { | |
| 1323 if(available_backends[i]->info.id == id || | |
| 1324 (name && strcasecompare(available_backends[i]->info.name, name))) { | |
| 1325 multissl_init(available_backends[i]); | |
| 1326 return CURLSSLSET_OK; | |
| 1327 } | |
| 1328 } | |
| 1329 | |
| 1330 return CURLSSLSET_UNKNOWN_BACKEND; | |
| 1331 } | |
| 1332 | |
| 1333 #else /* USE_SSL */ | |
| 1334 CURLsslset curl_global_sslset(curl_sslbackend id, const char *name, | |
| 1335 const curl_ssl_backend ***avail) | |
| 1336 { | |
| 1337 (void)id; | |
| 1338 (void)name; | |
| 1339 (void)avail; | |
| 1340 return CURLSSLSET_NO_BACKENDS; | |
| 1341 } | |
| 1342 | |
| 1343 #endif /* !USE_SSL */ |
