| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) Daniel Stenberg, <[email protected]>, 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.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 |
* SPDX-License-Identifier: curl |
| 22 |
* |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
/* This file is for implementing all "generic" SSL functions that all libcurl |
| 26 |
internals should use. It is then responsible for calling the proper |
| 27 |
"backend" function. |
| 28 |
|
| 29 |
SSL-functions in libcurl should call functions in this source file, and not |
| 30 |
to any specific SSL-layer. |
| 31 |
|
| 32 |
Curl_ssl_ - prefix for generic ones |
| 33 |
|
| 34 |
Note that this source code uses the functions of the configured SSL |
| 35 |
backend via the global Curl_ssl instance. |
| 36 |
|
| 37 |
"SSL/TLS Strong Encryption: An Introduction" |
| 38 |
https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html |
| 39 |
*/ |
| 40 |
|
| 41 |
#include "../curl_setup.h" |
| 42 |
|
| 43 |
#ifdef HAVE_SYS_TYPES_H |
| 44 |
#include <sys/types.h> |
| 45 |
#endif |
| 46 |
#ifdef HAVE_SYS_STAT_H |
| 47 |
#include <sys/stat.h> |
| 48 |
#endif |
| 49 |
#ifdef HAVE_FCNTL_H |
| 50 |
#include <fcntl.h> |
| 51 |
#endif |
| 52 |
|
| 53 |
#include "../urldata.h" |
| 54 |
#include "../cfilters.h" |
| 55 |
|
| 56 |
#include "vtls.h" /* generic SSL protos etc */ |
| 57 |
#include "vtls_int.h" |
| 58 |
#include "vtls_scache.h" |
| 59 |
|
| 60 |
#include "openssl.h" /* OpenSSL versions */ |
| 61 |
#include "gtls.h" /* GnuTLS versions */ |
| 62 |
#include "wolfssl.h" /* wolfSSL versions */ |
| 63 |
#include "schannel.h" /* Schannel SSPI version */ |
| 64 |
#include "sectransp.h" /* Secure Transport (Darwin) version */ |
| 65 |
#include "mbedtls.h" /* mbedTLS versions */ |
| 66 |
#include "bearssl.h" /* BearSSL versions */ |
| 67 |
#include "rustls.h" /* Rustls versions */ |
| 68 |
|
| 69 |
#include "../slist.h" |
| 70 |
#include "../sendf.h" |
| 71 |
#include "../strcase.h" |
| 72 |
#include "../url.h" |
| 73 |
#include "../progress.h" |
| 74 |
#include "../share.h" |
| 75 |
#include "../multiif.h" |
| 76 |
#include "../curlx/timeval.h" |
| 77 |
#include "../curl_md5.h" |
| 78 |
#include "../curl_sha256.h" |
| 79 |
#include "../curlx/warnless.h" |
| 80 |
#include "../curlx/base64.h" |
| 81 |
#include "../curl_printf.h" |
| 82 |
#include "../curlx/inet_pton.h" |
| 83 |
#include "../connect.h" |
| 84 |
#include "../select.h" |
| 85 |
#include "../strdup.h" |
| 86 |
#include "../rand.h" |
| 87 |
|
| 88 |
/* The last #include files should be: */ |
| 89 |
#include "../curl_memory.h" |
| 90 |
#include "../memdebug.h" |
| 91 |
|
| 92 |
|
| 93 |
#define CLONE_STRING(var) \ |
| 94 |
do { \ |
| 95 |
if(source->var) { \ |
| 96 |
dest->var = strdup(source->var); \ |
| 97 |
if(!dest->var) \ |
| 98 |
return FALSE; \ |
| 99 |
} \ |
| 100 |
else \ |
| 101 |
dest->var = NULL; \ |
| 102 |
} while(0) |
| 103 |
|
| 104 |
#define CLONE_BLOB(var) \ |
| 105 |
do { \ |
| 106 |
if(blobdup(&dest->var, source->var)) \ |
| 107 |
return FALSE; \ |
| 108 |
} while(0) |
| 109 |
|
| 110 |
static CURLcode blobdup(struct curl_blob **dest, |
| 111 |
struct curl_blob *src) |
| 112 |
{ |
| 113 |
DEBUGASSERT(dest); |
| 114 |
DEBUGASSERT(!*dest); |
| 115 |
if(src) { |
| 116 |
/* only if there is data to dupe! */ |
| 117 |
struct curl_blob *d; |
| 118 |
d = malloc(sizeof(struct curl_blob) + src->len); |
| 119 |
if(!d) |
| 120 |
return CURLE_OUT_OF_MEMORY; |
| 121 |
d->len = src->len; |
| 122 |
/* Always duplicate because the connection may survive longer than the |
| 123 |
handle that passed in the blob. */ |
| 124 |
d->flags = CURL_BLOB_COPY; |
| 125 |
d->data = (void *)((char *)d + sizeof(struct curl_blob)); |
| 126 |
memcpy(d->data, src->data, src->len); |
| 127 |
*dest = d; |
| 128 |
} |
| 129 |
return CURLE_OK; |
| 130 |
} |
| 131 |
|
| 132 |
/* returns TRUE if the blobs are identical */ |
| 133 |
static bool blobcmp(struct curl_blob *first, struct curl_blob *second) |
| 134 |
{ |
| 135 |
if(!first && !second) /* both are NULL */ |
| 136 |
return TRUE; |
| 137 |
if(!first || !second) /* one is NULL */ |
| 138 |
return FALSE; |
| 139 |
if(first->len != second->len) /* different sizes */ |
| 140 |
return FALSE; |
| 141 |
return !memcmp(first->data, second->data, first->len); /* same data */ |
| 142 |
} |
| 143 |
|
| 144 |
#ifdef USE_SSL |
| 145 |
static const struct alpn_spec ALPN_SPEC_H11 = { |
| 146 |
{ ALPN_HTTP_1_1 }, 1 |
| 147 |
}; |
| 148 |
#ifdef USE_HTTP2 |
| 149 |
static const struct alpn_spec ALPN_SPEC_H2 = { |
| 150 |
{ ALPN_H2 }, 1 |
| 151 |
}; |
| 152 |
static const struct alpn_spec ALPN_SPEC_H2_H11 = { |
| 153 |
{ ALPN_H2, ALPN_HTTP_1_1 }, 2 |
| 154 |
}; |
| 155 |
#endif |
| 156 |
|
| 157 |
static const struct alpn_spec * |
| 158 |
alpn_get_spec(http_majors allowed, bool use_alpn) |
| 159 |
{ |
| 160 |
if(!use_alpn) |
| 161 |
return NULL; |
| 162 |
#ifdef USE_HTTP2 |
| 163 |
if(allowed & CURL_HTTP_V2x) { |
| 164 |
if(allowed & CURL_HTTP_V1x) |
| 165 |
return &ALPN_SPEC_H2_H11; |
| 166 |
return &ALPN_SPEC_H2; |
| 167 |
} |
| 168 |
#else |
| 169 |
(void)allowed; |
| 170 |
#endif |
| 171 |
/* Use the ALPN protocol "http/1.1" for HTTP/1.x. |
| 172 |
Avoid "http/1.0" because some servers do not support it. */ |
| 173 |
return &ALPN_SPEC_H11; |
| 174 |
} |
| 175 |
#endif /* USE_SSL */ |
| 176 |
|
| 177 |
|
| 178 |
void Curl_ssl_easy_config_init(struct Curl_easy *data) |
| 179 |
{ |
| 180 |
/* |
| 181 |
* libcurl 7.10 introduced SSL verification *by default*! This needs to be |
| 182 |
* switched off unless wanted. |
| 183 |
*/ |
| 184 |
data->set.ssl.primary.verifypeer = TRUE; |
| 185 |
data->set.ssl.primary.verifyhost = TRUE; |
| 186 |
data->set.ssl.primary.cache_session = TRUE; /* caching by default */ |
| 187 |
#ifndef CURL_DISABLE_PROXY |
| 188 |
data->set.proxy_ssl = data->set.ssl; |
| 189 |
#endif |
| 190 |
} |
| 191 |
|
| 192 |
static bool |
| 193 |
match_ssl_primary_config(struct Curl_easy *data, |
| 194 |
struct ssl_primary_config *c1, |
| 195 |
struct ssl_primary_config *c2) |
| 196 |
{ |
| 197 |
(void)data; |
| 198 |
if((c1->version == c2->version) && |
| 199 |
(c1->version_max == c2->version_max) && |
| 200 |
(c1->ssl_options == c2->ssl_options) && |
| 201 |
(c1->verifypeer == c2->verifypeer) && |
| 202 |
(c1->verifyhost == c2->verifyhost) && |
| 203 |
(c1->verifystatus == c2->verifystatus) && |
| 204 |
blobcmp(c1->cert_blob, c2->cert_blob) && |
| 205 |
blobcmp(c1->ca_info_blob, c2->ca_info_blob) && |
| 206 |
blobcmp(c1->issuercert_blob, c2->issuercert_blob) && |
| 207 |
Curl_safecmp(c1->CApath, c2->CApath) && |
| 208 |
Curl_safecmp(c1->CAfile, c2->CAfile) && |
| 209 |
Curl_safecmp(c1->issuercert, c2->issuercert) && |
| 210 |
Curl_safecmp(c1->clientcert, c2->clientcert) && |
| 211 |
#ifdef USE_TLS_SRP |
| 212 |
!Curl_timestrcmp(c1->username, c2->username) && |
| 213 |
!Curl_timestrcmp(c1->password, c2->password) && |
| 214 |
#endif |
| 215 |
strcasecompare(c1->cipher_list, c2->cipher_list) && |
| 216 |
strcasecompare(c1->cipher_list13, c2->cipher_list13) && |
| 217 |
strcasecompare(c1->curves, c2->curves) && |
| 218 |
strcasecompare(c1->signature_algorithms, c2->signature_algorithms) && |
| 219 |
strcasecompare(c1->CRLfile, c2->CRLfile) && |
| 220 |
strcasecompare(c1->pinned_key, c2->pinned_key)) |
| 221 |
return TRUE; |
| 222 |
|
| 223 |
return FALSE; |
| 224 |
} |
| 225 |
|
| 226 |
bool Curl_ssl_conn_config_match(struct Curl_easy *data, |
| 227 |
struct connectdata *candidate, |
| 228 |
bool proxy) |
| 229 |
{ |
| 230 |
#ifndef CURL_DISABLE_PROXY |
| 231 |
if(proxy) |
| 232 |
return match_ssl_primary_config(data, &data->set.proxy_ssl.primary, |
| 233 |
&candidate->proxy_ssl_config); |
| 234 |
#else |
| 235 |
(void)proxy; |
| 236 |
#endif |
| 237 |
return match_ssl_primary_config(data, &data->set.ssl.primary, |
| 238 |
&candidate->ssl_config); |
| 239 |
} |
| 240 |
|
| 241 |
static bool clone_ssl_primary_config(struct ssl_primary_config *source, |
| 242 |
struct ssl_primary_config *dest) |
| 243 |
{ |
| 244 |
dest->version = source->version; |
| 245 |
dest->version_max = source->version_max; |
| 246 |
dest->verifypeer = source->verifypeer; |
| 247 |
dest->verifyhost = source->verifyhost; |
| 248 |
dest->verifystatus = source->verifystatus; |
| 249 |
dest->cache_session = source->cache_session; |
| 250 |
dest->ssl_options = source->ssl_options; |
| 251 |
|
| 252 |
CLONE_BLOB(cert_blob); |
| 253 |
CLONE_BLOB(ca_info_blob); |
| 254 |
CLONE_BLOB(issuercert_blob); |
| 255 |
CLONE_STRING(CApath); |
| 256 |
CLONE_STRING(CAfile); |
| 257 |
CLONE_STRING(issuercert); |
| 258 |
CLONE_STRING(clientcert); |
| 259 |
CLONE_STRING(cipher_list); |
| 260 |
CLONE_STRING(cipher_list13); |
| 261 |
CLONE_STRING(pinned_key); |
| 262 |
CLONE_STRING(curves); |
| 263 |
CLONE_STRING(signature_algorithms); |
| 264 |
CLONE_STRING(CRLfile); |
| 265 |
#ifdef USE_TLS_SRP |
| 266 |
CLONE_STRING(username); |
| 267 |
CLONE_STRING(password); |
| 268 |
#endif |
| 269 |
|
| 270 |
return TRUE; |
| 271 |
} |
| 272 |
|
| 273 |
static void free_primary_ssl_config(struct ssl_primary_config *sslc) |
| 274 |
{ |
| 275 |
Curl_safefree(sslc->CApath); |
| 276 |
Curl_safefree(sslc->CAfile); |
| 277 |
Curl_safefree(sslc->issuercert); |
| 278 |
Curl_safefree(sslc->clientcert); |
| 279 |
Curl_safefree(sslc->cipher_list); |
| 280 |
Curl_safefree(sslc->cipher_list13); |
| 281 |
Curl_safefree(sslc->pinned_key); |
| 282 |
Curl_safefree(sslc->cert_blob); |
| 283 |
Curl_safefree(sslc->ca_info_blob); |
| 284 |
Curl_safefree(sslc->issuercert_blob); |
| 285 |
Curl_safefree(sslc->curves); |
| 286 |
Curl_safefree(sslc->signature_algorithms); |
| 287 |
Curl_safefree(sslc->CRLfile); |
| 288 |
#ifdef USE_TLS_SRP |
| 289 |
Curl_safefree(sslc->username); |
| 290 |
Curl_safefree(sslc->password); |
| 291 |
#endif |
| 292 |
} |
| 293 |
|
| 294 |
CURLcode Curl_ssl_easy_config_complete(struct Curl_easy *data) |
| 295 |
{ |
| 296 |
data->set.ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH]; |
| 297 |
data->set.ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE]; |
| 298 |
data->set.ssl.primary.CRLfile = data->set.str[STRING_SSL_CRLFILE]; |
| 299 |
data->set.ssl.primary.issuercert = data->set.str[STRING_SSL_ISSUERCERT]; |
| 300 |
data->set.ssl.primary.issuercert_blob = data->set.blobs[BLOB_SSL_ISSUERCERT]; |
| 301 |
data->set.ssl.primary.cipher_list = |
| 302 |
data->set.str[STRING_SSL_CIPHER_LIST]; |
| 303 |
data->set.ssl.primary.cipher_list13 = |
| 304 |
data->set.str[STRING_SSL_CIPHER13_LIST]; |
| 305 |
data->set.ssl.primary.signature_algorithms = |
| 306 |
data->set.str[STRING_SSL_SIGNATURE_ALGORITHMS]; |
| 307 |
data->set.ssl.primary.pinned_key = |
| 308 |
data->set.str[STRING_SSL_PINNEDPUBLICKEY]; |
| 309 |
data->set.ssl.primary.cert_blob = data->set.blobs[BLOB_CERT]; |
| 310 |
data->set.ssl.primary.ca_info_blob = data->set.blobs[BLOB_CAINFO]; |
| 311 |
data->set.ssl.primary.curves = data->set.str[STRING_SSL_EC_CURVES]; |
| 312 |
#ifdef USE_TLS_SRP |
| 313 |
data->set.ssl.primary.username = data->set.str[STRING_TLSAUTH_USERNAME]; |
| 314 |
data->set.ssl.primary.password = data->set.str[STRING_TLSAUTH_PASSWORD]; |
| 315 |
#endif |
| 316 |
data->set.ssl.cert_type = data->set.str[STRING_CERT_TYPE]; |
| 317 |
data->set.ssl.key = data->set.str[STRING_KEY]; |
| 318 |
data->set.ssl.key_type = data->set.str[STRING_KEY_TYPE]; |
| 319 |
data->set.ssl.key_passwd = data->set.str[STRING_KEY_PASSWD]; |
| 320 |
data->set.ssl.primary.clientcert = data->set.str[STRING_CERT]; |
| 321 |
data->set.ssl.key_blob = data->set.blobs[BLOB_KEY]; |
| 322 |
|
| 323 |
#ifndef CURL_DISABLE_PROXY |
| 324 |
data->set.proxy_ssl.primary.CApath = data->set.str[STRING_SSL_CAPATH_PROXY]; |
| 325 |
data->set.proxy_ssl.primary.CAfile = data->set.str[STRING_SSL_CAFILE_PROXY]; |
| 326 |
data->set.proxy_ssl.primary.cipher_list = |
| 327 |
data->set.str[STRING_SSL_CIPHER_LIST_PROXY]; |
| 328 |
data->set.proxy_ssl.primary.cipher_list13 = |
| 329 |
data->set.str[STRING_SSL_CIPHER13_LIST_PROXY]; |
| 330 |
data->set.proxy_ssl.primary.pinned_key = |
| 331 |
data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]; |
| 332 |
data->set.proxy_ssl.primary.cert_blob = data->set.blobs[BLOB_CERT_PROXY]; |
| 333 |
data->set.proxy_ssl.primary.ca_info_blob = |
| 334 |
data->set.blobs[BLOB_CAINFO_PROXY]; |
| 335 |
data->set.proxy_ssl.primary.issuercert = |
| 336 |
data->set.str[STRING_SSL_ISSUERCERT_PROXY]; |
| 337 |
data->set.proxy_ssl.primary.issuercert_blob = |
| 338 |
data->set.blobs[BLOB_SSL_ISSUERCERT_PROXY]; |
| 339 |
data->set.proxy_ssl.primary.CRLfile = |
| 340 |
data->set.str[STRING_SSL_CRLFILE_PROXY]; |
| 341 |
data->set.proxy_ssl.cert_type = data->set.str[STRING_CERT_TYPE_PROXY]; |
| 342 |
data->set.proxy_ssl.key = data->set.str[STRING_KEY_PROXY]; |
| 343 |
data->set.proxy_ssl.key_type = data->set.str[STRING_KEY_TYPE_PROXY]; |
| 344 |
data->set.proxy_ssl.key_passwd = data->set.str[STRING_KEY_PASSWD_PROXY]; |
| 345 |
data->set.proxy_ssl.primary.clientcert = data->set.str[STRING_CERT_PROXY]; |
| 346 |
data->set.proxy_ssl.key_blob = data->set.blobs[BLOB_KEY_PROXY]; |
| 347 |
#ifdef USE_TLS_SRP |
| 348 |
data->set.proxy_ssl.primary.username = |
| 349 |
data->set.str[STRING_TLSAUTH_USERNAME_PROXY]; |
| 350 |
data->set.proxy_ssl.primary.password = |
| 351 |
data->set.str[STRING_TLSAUTH_PASSWORD_PROXY]; |
| 352 |
#endif |
| 353 |
#endif /* CURL_DISABLE_PROXY */ |
| 354 |
|
| 355 |
return CURLE_OK; |
| 356 |
} |
| 357 |
|
| 358 |
CURLcode Curl_ssl_conn_config_init(struct Curl_easy *data, |
| 359 |
struct connectdata *conn) |
| 360 |
{ |
| 361 |
/* Clone "primary" SSL configurations from the esay handle to |
| 362 |
* the connection. They are used for connection cache matching and |
| 363 |
* probably outlive the easy handle */ |
| 364 |
if(!clone_ssl_primary_config(&data->set.ssl.primary, &conn->ssl_config)) |
| 365 |
return CURLE_OUT_OF_MEMORY; |
| 366 |
#ifndef CURL_DISABLE_PROXY |
| 367 |
if(!clone_ssl_primary_config(&data->set.proxy_ssl.primary, |
| 368 |
&conn->proxy_ssl_config)) |
| 369 |
return CURLE_OUT_OF_MEMORY; |
| 370 |
#endif |
| 371 |
return CURLE_OK; |
| 372 |
} |
| 373 |
|
| 374 |
void Curl_ssl_conn_config_cleanup(struct connectdata *conn) |
| 375 |
{ |
| 376 |
free_primary_ssl_config(&conn->ssl_config); |
| 377 |
#ifndef CURL_DISABLE_PROXY |
| 378 |
free_primary_ssl_config(&conn->proxy_ssl_config); |
| 379 |
#endif |
| 380 |
} |
| 381 |
|
| 382 |
void Curl_ssl_conn_config_update(struct Curl_easy *data, bool for_proxy) |
| 383 |
{ |
| 384 |
/* May be called on an easy that has no connection yet */ |
| 385 |
if(data->conn) { |
| 386 |
struct ssl_primary_config *src, *dest; |
| 387 |
#ifndef CURL_DISABLE_PROXY |
| 388 |
src = for_proxy ? &data->set.proxy_ssl.primary : &data->set.ssl.primary; |
| 389 |
dest = for_proxy ? &data->conn->proxy_ssl_config : &data->conn->ssl_config; |
| 390 |
#else |
| 391 |
(void)for_proxy; |
| 392 |
src = &data->set.ssl.primary; |
| 393 |
dest = &data->conn->ssl_config; |
| 394 |
#endif |
| 395 |
dest->verifyhost = src->verifyhost; |
| 396 |
dest->verifypeer = src->verifypeer; |
| 397 |
dest->verifystatus = src->verifystatus; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
#ifdef USE_SSL |
| 402 |
static int multissl_setup(const struct Curl_ssl *backend); |
| 403 |
#endif |
| 404 |
|
| 405 |
curl_sslbackend Curl_ssl_backend(void) |
| 406 |
{ |
| 407 |
#ifdef USE_SSL |
| 408 |
multissl_setup(NULL); |
| 409 |
return Curl_ssl->info.id; |
| 410 |
#else |
| 411 |
return CURLSSLBACKEND_NONE; |
| 412 |
#endif |
| 413 |
} |
| 414 |
|
| 415 |
#ifdef USE_SSL |
| 416 |
|
| 417 |
/* "global" init done? */ |
| 418 |
static bool init_ssl = FALSE; |
| 419 |
|
| 420 |
/** |
| 421 |
* Global SSL init |
| 422 |
* |
| 423 |
* @retval 0 error initializing SSL |
| 424 |
* @retval 1 SSL initialized successfully |
| 425 |
*/ |
| 426 |
int Curl_ssl_init(void) |
| 427 |
{ |
| 428 |
/* make sure this is only done once */ |
| 429 |
if(init_ssl) |
| 430 |
return 1; |
| 431 |
init_ssl = TRUE; /* never again */ |
| 432 |
|
| 433 |
if(Curl_ssl->init) |
| 434 |
return Curl_ssl->init(); |
| 435 |
return 1; |
| 436 |
} |
| 437 |
|
| 438 |
static bool ssl_prefs_check(struct Curl_easy *data) |
| 439 |
{ |
| 440 |
/* check for CURLOPT_SSLVERSION invalid parameter value */ |
| 441 |
const unsigned char sslver = data->set.ssl.primary.version; |
| 442 |
if(sslver >= CURL_SSLVERSION_LAST) { |
| 443 |
failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION"); |
| 444 |
return FALSE; |
| 445 |
} |
| 446 |
|
| 447 |
switch(data->set.ssl.primary.version_max) { |
| 448 |
case CURL_SSLVERSION_MAX_NONE: |
| 449 |
case CURL_SSLVERSION_MAX_DEFAULT: |
| 450 |
break; |
| 451 |
|
| 452 |
default: |
| 453 |
if((data->set.ssl.primary.version_max >> 16) < sslver) { |
| 454 |
failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION"); |
| 455 |
return FALSE; |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
return TRUE; |
| 460 |
} |
| 461 |
|
| 462 |
static struct ssl_connect_data *cf_ctx_new(struct Curl_easy *data, |
| 463 |
const struct alpn_spec *alpn) |
| 464 |
{ |
| 465 |
struct ssl_connect_data *ctx; |
| 466 |
|
| 467 |
(void)data; |
| 468 |
ctx = calloc(1, sizeof(*ctx)); |
| 469 |
if(!ctx) |
| 470 |
return NULL; |
| 471 |
|
| 472 |
ctx->ssl_impl = Curl_ssl; |
| 473 |
ctx->alpn = alpn; |
| 474 |
Curl_bufq_init2(&ctx->earlydata, CURL_SSL_EARLY_MAX, 1, BUFQ_OPT_NO_SPARES); |
| 475 |
ctx->backend = calloc(1, ctx->ssl_impl->sizeof_ssl_backend_data); |
| 476 |
if(!ctx->backend) { |
| 477 |
free(ctx); |
| 478 |
return NULL; |
| 479 |
} |
| 480 |
return ctx; |
| 481 |
} |
| 482 |
|
| 483 |
static void cf_ctx_free(struct ssl_connect_data *ctx) |
| 484 |
{ |
| 485 |
if(ctx) { |
| 486 |
Curl_safefree(ctx->negotiated.alpn); |
| 487 |
Curl_bufq_free(&ctx->earlydata); |
| 488 |
free(ctx->backend); |
| 489 |
free(ctx); |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
CURLcode Curl_ssl_get_channel_binding(struct Curl_easy *data, int sockindex, |
| 494 |
struct dynbuf *binding) |
| 495 |
{ |
| 496 |
if(Curl_ssl->get_channel_binding) |
| 497 |
return Curl_ssl->get_channel_binding(data, sockindex, binding); |
| 498 |
return CURLE_OK; |
| 499 |
} |
| 500 |
|
| 501 |
void Curl_ssl_close_all(struct Curl_easy *data) |
| 502 |
{ |
| 503 |
if(Curl_ssl->close_all) |
| 504 |
Curl_ssl->close_all(data); |
| 505 |
} |
| 506 |
|
| 507 |
void Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 508 |
struct easy_pollset *ps) |
| 509 |
{ |
| 510 |
struct ssl_connect_data *connssl = cf->ctx; |
| 511 |
|
| 512 |
if(connssl->io_need) { |
| 513 |
curl_socket_t sock = Curl_conn_cf_get_socket(cf->next, data); |
| 514 |
if(sock != CURL_SOCKET_BAD) { |
| 515 |
if(connssl->io_need & CURL_SSL_IO_NEED_SEND) { |
| 516 |
Curl_pollset_set_out_only(data, ps, sock); |
| 517 |
CURL_TRC_CF(data, cf, "adjust_pollset, POLLOUT fd=%" FMT_SOCKET_T, |
| 518 |
sock); |
| 519 |
} |
| 520 |
else { |
| 521 |
Curl_pollset_set_in_only(data, ps, sock); |
| 522 |
CURL_TRC_CF(data, cf, "adjust_pollset, POLLIN fd=%" FMT_SOCKET_T, |
| 523 |
sock); |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
/* Selects an SSL crypto engine |
| 530 |
*/ |
| 531 |
CURLcode Curl_ssl_set_engine(struct Curl_easy *data, const char *engine) |
| 532 |
{ |
| 533 |
if(Curl_ssl->set_engine) |
| 534 |
return Curl_ssl->set_engine(data, engine); |
| 535 |
return CURLE_NOT_BUILT_IN; |
| 536 |
} |
| 537 |
|
| 538 |
/* Selects the default SSL crypto engine |
| 539 |
*/ |
| 540 |
CURLcode Curl_ssl_set_engine_default(struct Curl_easy *data) |
| 541 |
{ |
| 542 |
if(Curl_ssl->set_engine_default) |
| 543 |
return Curl_ssl->set_engine_default(data); |
| 544 |
return CURLE_NOT_BUILT_IN; |
| 545 |
} |
| 546 |
|
| 547 |
/* Return list of OpenSSL crypto engine names. */ |
| 548 |
struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data) |
| 549 |
{ |
| 550 |
if(Curl_ssl->engines_list) |
| 551 |
return Curl_ssl->engines_list(data); |
| 552 |
return NULL; |
| 553 |
} |
| 554 |
|
| 555 |
static size_t multissl_version(char *buffer, size_t size); |
| 556 |
|
| 557 |
void Curl_ssl_version(char *buffer, size_t size) |
| 558 |
{ |
| 559 |
#ifdef CURL_WITH_MULTI_SSL |
| 560 |
(void)multissl_version(buffer, size); |
| 561 |
#else |
| 562 |
(void)Curl_ssl->version(buffer, size); |
| 563 |
#endif |
| 564 |
} |
| 565 |
|
| 566 |
void Curl_ssl_free_certinfo(struct Curl_easy *data) |
| 567 |
{ |
| 568 |
struct curl_certinfo *ci = &data->info.certs; |
| 569 |
|
| 570 |
if(ci->num_of_certs) { |
| 571 |
/* free all individual lists used */ |
| 572 |
int i; |
| 573 |
for(i = 0; i < ci->num_of_certs; i++) { |
| 574 |
curl_slist_free_all(ci->certinfo[i]); |
| 575 |
ci->certinfo[i] = NULL; |
| 576 |
} |
| 577 |
|
| 578 |
free(ci->certinfo); /* free the actual array too */ |
| 579 |
ci->certinfo = NULL; |
| 580 |
ci->num_of_certs = 0; |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num) |
| 585 |
{ |
| 586 |
struct curl_certinfo *ci = &data->info.certs; |
| 587 |
struct curl_slist **table; |
| 588 |
|
| 589 |
/* Free any previous certificate information structures */ |
| 590 |
Curl_ssl_free_certinfo(data); |
| 591 |
|
| 592 |
/* Allocate the required certificate information structures */ |
| 593 |
table = calloc((size_t) num, sizeof(struct curl_slist *)); |
| 594 |
if(!table) |
| 595 |
return CURLE_OUT_OF_MEMORY; |
| 596 |
|
| 597 |
ci->num_of_certs = num; |
| 598 |
ci->certinfo = table; |
| 599 |
|
| 600 |
return CURLE_OK; |
| 601 |
} |
| 602 |
|
| 603 |
/* |
| 604 |
* 'value' is NOT a null-terminated string |
| 605 |
*/ |
| 606 |
CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, |
| 607 |
int certnum, |
| 608 |
const char *label, |
| 609 |
const char *value, |
| 610 |
size_t valuelen) |
| 611 |
{ |
| 612 |
struct curl_certinfo *ci = &data->info.certs; |
| 613 |
struct curl_slist *nl; |
| 614 |
CURLcode result = CURLE_OK; |
| 615 |
struct dynbuf build; |
| 616 |
|
| 617 |
DEBUGASSERT(certnum < ci->num_of_certs); |
| 618 |
|
| 619 |
curlx_dyn_init(&build, CURL_X509_STR_MAX); |
| 620 |
|
| 621 |
if(curlx_dyn_add(&build, label) || |
| 622 |
curlx_dyn_addn(&build, ":", 1) || |
| 623 |
curlx_dyn_addn(&build, value, valuelen)) |
| 624 |
return CURLE_OUT_OF_MEMORY; |
| 625 |
|
| 626 |
nl = Curl_slist_append_nodup(ci->certinfo[certnum], |
| 627 |
curlx_dyn_ptr(&build)); |
| 628 |
if(!nl) { |
| 629 |
curlx_dyn_free(&build); |
| 630 |
curl_slist_free_all(ci->certinfo[certnum]); |
| 631 |
result = CURLE_OUT_OF_MEMORY; |
| 632 |
} |
| 633 |
|
| 634 |
ci->certinfo[certnum] = nl; |
| 635 |
return result; |
| 636 |
} |
| 637 |
|
| 638 |
/* get length bytes of randomness */ |
| 639 |
CURLcode Curl_ssl_random(struct Curl_easy *data, |
| 640 |
unsigned char *entropy, |
| 641 |
size_t length) |
| 642 |
{ |
| 643 |
DEBUGASSERT(length == sizeof(int)); |
| 644 |
if(Curl_ssl->random) |
| 645 |
return Curl_ssl->random(data, entropy, length); |
| 646 |
else |
| 647 |
return CURLE_NOT_BUILT_IN; |
| 648 |
} |
| 649 |
|
| 650 |
/* |
| 651 |
* Public key pem to der conversion |
| 652 |
*/ |
| 653 |
|
| 654 |
static CURLcode pubkey_pem_to_der(const char *pem, |
| 655 |
unsigned char **der, size_t *der_len) |
| 656 |
{ |
| 657 |
char *begin_pos, *end_pos; |
| 658 |
size_t pem_count, pem_len; |
| 659 |
CURLcode result; |
| 660 |
struct dynbuf pbuf; |
| 661 |
|
| 662 |
/* if no pem, exit. */ |
| 663 |
if(!pem) |
| 664 |
return CURLE_BAD_CONTENT_ENCODING; |
| 665 |
|
| 666 |
curlx_dyn_init(&pbuf, MAX_PINNED_PUBKEY_SIZE); |
| 667 |
|
| 668 |
begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----"); |
| 669 |
if(!begin_pos) |
| 670 |
return CURLE_BAD_CONTENT_ENCODING; |
| 671 |
|
| 672 |
pem_count = begin_pos - pem; |
| 673 |
/* Invalid if not at beginning AND not directly following \n */ |
| 674 |
if(0 != pem_count && '\n' != pem[pem_count - 1]) |
| 675 |
return CURLE_BAD_CONTENT_ENCODING; |
| 676 |
|
| 677 |
/* 26 is length of "-----BEGIN PUBLIC KEY-----" */ |
| 678 |
pem_count += 26; |
| 679 |
|
| 680 |
/* Invalid if not directly following \n */ |
| 681 |
end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----"); |
| 682 |
if(!end_pos) |
| 683 |
return CURLE_BAD_CONTENT_ENCODING; |
| 684 |
|
| 685 |
pem_len = end_pos - pem; |
| 686 |
|
| 687 |
/* |
| 688 |
* Here we loop through the pem array one character at a time between the |
| 689 |
* correct indices, and place each character that is not '\n' or '\r' |
| 690 |
* into the stripped_pem array, which should represent the raw base64 string |
| 691 |
*/ |
| 692 |
while(pem_count < pem_len) { |
| 693 |
if('\n' != pem[pem_count] && '\r' != pem[pem_count]) { |
| 694 |
result = curlx_dyn_addn(&pbuf, &pem[pem_count], 1); |
| 695 |
if(result) |
| 696 |
return result; |
| 697 |
} |
| 698 |
++pem_count; |
| 699 |
} |
| 700 |
|
| 701 |
if(curlx_dyn_len(&pbuf)) { |
| 702 |
result = curlx_base64_decode(curlx_dyn_ptr(&pbuf), der, der_len); |
| 703 |
curlx_dyn_free(&pbuf); |
| 704 |
} |
| 705 |
else |
| 706 |
result = CURLE_BAD_CONTENT_ENCODING; |
| 707 |
|
| 708 |
return result; |
| 709 |
} |
| 710 |
|
| 711 |
/* |
| 712 |
* Generic pinned public key check. |
| 713 |
*/ |
| 714 |
|
| 715 |
CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data, |
| 716 |
const char *pinnedpubkey, |
| 717 |
const unsigned char *pubkey, size_t pubkeylen) |
| 718 |
{ |
| 719 |
CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
| 720 |
#ifdef CURL_DISABLE_VERBOSE_STRINGS |
| 721 |
(void)data; |
| 722 |
#endif |
| 723 |
|
| 724 |
/* if a path was not specified, do not pin */ |
| 725 |
if(!pinnedpubkey) |
| 726 |
return CURLE_OK; |
| 727 |
if(!pubkey || !pubkeylen) |
| 728 |
return result; |
| 729 |
|
| 730 |
/* only do this if pinnedpubkey starts with "sha256//", length 8 */ |
| 731 |
if(!strncmp(pinnedpubkey, "sha256//", 8)) { |
| 732 |
CURLcode encode; |
| 733 |
size_t encodedlen = 0; |
| 734 |
char *encoded = NULL, *pinkeycopy, *begin_pos, *end_pos; |
| 735 |
unsigned char *sha256sumdigest; |
| 736 |
|
| 737 |
if(!Curl_ssl->sha256sum) { |
| 738 |
/* without sha256 support, this cannot match */ |
| 739 |
return result; |
| 740 |
} |
| 741 |
|
| 742 |
/* compute sha256sum of public key */ |
| 743 |
sha256sumdigest = malloc(CURL_SHA256_DIGEST_LENGTH); |
| 744 |
if(!sha256sumdigest) |
| 745 |
return CURLE_OUT_OF_MEMORY; |
| 746 |
encode = Curl_ssl->sha256sum(pubkey, pubkeylen, |
| 747 |
sha256sumdigest, CURL_SHA256_DIGEST_LENGTH); |
| 748 |
|
| 749 |
if(!encode) |
| 750 |
encode = curlx_base64_encode((char *)sha256sumdigest, |
| 751 |
CURL_SHA256_DIGEST_LENGTH, &encoded, |
| 752 |
&encodedlen); |
| 753 |
Curl_safefree(sha256sumdigest); |
| 754 |
|
| 755 |
if(encode) |
| 756 |
return encode; |
| 757 |
|
| 758 |
infof(data, " public key hash: sha256//%s", encoded); |
| 759 |
|
| 760 |
/* it starts with sha256//, copy so we can modify it */ |
| 761 |
pinkeycopy = strdup(pinnedpubkey); |
| 762 |
if(!pinkeycopy) { |
| 763 |
Curl_safefree(encoded); |
| 764 |
return CURLE_OUT_OF_MEMORY; |
| 765 |
} |
| 766 |
/* point begin_pos to the copy, and start extracting keys */ |
| 767 |
begin_pos = pinkeycopy; |
| 768 |
do { |
| 769 |
end_pos = strstr(begin_pos, ";sha256//"); |
| 770 |
/* |
| 771 |
* if there is an end_pos, null-terminate, otherwise it will go to the |
| 772 |
* end of the original string |
| 773 |
*/ |
| 774 |
if(end_pos) |
| 775 |
end_pos[0] = '\0'; |
| 776 |
|
| 777 |
/* compare base64 sha256 digests, 8 is the length of "sha256//" */ |
| 778 |
if(encodedlen == strlen(begin_pos + 8) && |
| 779 |
!memcmp(encoded, begin_pos + 8, encodedlen)) { |
| 780 |
result = CURLE_OK; |
| 781 |
break; |
| 782 |
} |
| 783 |
|
| 784 |
/* |
| 785 |
* change back the null-terminator we changed earlier, |
| 786 |
* and look for next begin |
| 787 |
*/ |
| 788 |
if(end_pos) { |
| 789 |
end_pos[0] = ';'; |
| 790 |
begin_pos = strstr(end_pos, "sha256//"); |
| 791 |
} |
| 792 |
} while(end_pos && begin_pos); |
| 793 |
Curl_safefree(encoded); |
| 794 |
Curl_safefree(pinkeycopy); |
| 795 |
} |
| 796 |
else { |
| 797 |
long filesize; |
| 798 |
size_t size, pem_len; |
| 799 |
CURLcode pem_read; |
| 800 |
struct dynbuf buf; |
| 801 |
char unsigned *pem_ptr = NULL; |
| 802 |
size_t left; |
| 803 |
FILE *fp = fopen(pinnedpubkey, "rb"); |
| 804 |
if(!fp) |
| 805 |
return result; |
| 806 |
|
| 807 |
curlx_dyn_init(&buf, MAX_PINNED_PUBKEY_SIZE); |
| 808 |
|
| 809 |
/* Determine the file's size */ |
| 810 |
if(fseek(fp, 0, SEEK_END)) |
| 811 |
goto end; |
| 812 |
filesize = ftell(fp); |
| 813 |
if(fseek(fp, 0, SEEK_SET)) |
| 814 |
goto end; |
| 815 |
if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE) |
| 816 |
goto end; |
| 817 |
|
| 818 |
/* |
| 819 |
* if the size of our certificate is bigger than the file |
| 820 |
* size then it cannot match |
| 821 |
*/ |
| 822 |
size = curlx_sotouz((curl_off_t) filesize); |
| 823 |
if(pubkeylen > size) |
| 824 |
goto end; |
| 825 |
|
| 826 |
/* |
| 827 |
* Read the file into the dynbuf |
| 828 |
*/ |
| 829 |
left = size; |
| 830 |
do { |
| 831 |
char buffer[1024]; |
| 832 |
size_t want = left > sizeof(buffer) ? sizeof(buffer) : left; |
| 833 |
if(want != fread(buffer, 1, want, fp)) |
| 834 |
goto end; |
| 835 |
if(curlx_dyn_addn(&buf, buffer, want)) |
| 836 |
goto end; |
| 837 |
left -= want; |
| 838 |
} while(left); |
| 839 |
|
| 840 |
/* If the sizes are the same, it cannot be base64 encoded, must be der */ |
| 841 |
if(pubkeylen == size) { |
| 842 |
if(!memcmp(pubkey, curlx_dyn_ptr(&buf), pubkeylen)) |
| 843 |
result = CURLE_OK; |
| 844 |
goto end; |
| 845 |
} |
| 846 |
|
| 847 |
/* |
| 848 |
* Otherwise we will assume it is PEM and try to decode it after placing |
| 849 |
* null-terminator |
| 850 |
*/ |
| 851 |
pem_read = pubkey_pem_to_der(curlx_dyn_ptr(&buf), &pem_ptr, &pem_len); |
| 852 |
/* if it was not read successfully, exit */ |
| 853 |
if(pem_read) |
| 854 |
goto end; |
| 855 |
|
| 856 |
/* |
| 857 |
* if the size of our certificate does not match the size of |
| 858 |
* the decoded file, they cannot be the same, otherwise compare |
| 859 |
*/ |
| 860 |
if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen)) |
| 861 |
result = CURLE_OK; |
| 862 |
end: |
| 863 |
curlx_dyn_free(&buf); |
| 864 |
Curl_safefree(pem_ptr); |
| 865 |
fclose(fp); |
| 866 |
} |
| 867 |
|
| 868 |
return result; |
| 869 |
} |
| 870 |
|
| 871 |
/* |
| 872 |
* Check whether the SSL backend supports the status_request extension. |
| 873 |
*/ |
| 874 |
bool Curl_ssl_cert_status_request(void) |
| 875 |
{ |
| 876 |
if(Curl_ssl->cert_status_request) |
| 877 |
return Curl_ssl->cert_status_request(); |
| 878 |
return FALSE; |
| 879 |
} |
| 880 |
|
| 881 |
/* |
| 882 |
* Check whether the SSL backend supports false start. |
| 883 |
*/ |
| 884 |
bool Curl_ssl_false_start(void) |
| 885 |
{ |
| 886 |
if(Curl_ssl->false_start) |
| 887 |
return Curl_ssl->false_start(); |
| 888 |
return FALSE; |
| 889 |
} |
| 890 |
|
| 891 |
static int multissl_init(void) |
| 892 |
{ |
| 893 |
if(multissl_setup(NULL)) |
| 894 |
return 1; |
| 895 |
if(Curl_ssl->init) |
| 896 |
return Curl_ssl->init(); |
| 897 |
return 1; |
| 898 |
} |
| 899 |
|
| 900 |
static CURLcode multissl_connect(struct Curl_cfilter *cf, |
| 901 |
struct Curl_easy *data, bool *done) |
| 902 |
{ |
| 903 |
if(multissl_setup(NULL)) |
| 904 |
return CURLE_FAILED_INIT; |
| 905 |
return Curl_ssl->do_connect(cf, data, done); |
| 906 |
} |
| 907 |
|
| 908 |
static void multissl_adjust_pollset(struct Curl_cfilter *cf, |
| 909 |
struct Curl_easy *data, |
| 910 |
struct easy_pollset *ps) |
| 911 |
{ |
| 912 |
if(multissl_setup(NULL)) |
| 913 |
return; |
| 914 |
Curl_ssl->adjust_pollset(cf, data, ps); |
| 915 |
} |
| 916 |
|
| 917 |
static void *multissl_get_internals(struct ssl_connect_data *connssl, |
| 918 |
CURLINFO info) |
| 919 |
{ |
| 920 |
if(multissl_setup(NULL)) |
| 921 |
return NULL; |
| 922 |
return Curl_ssl->get_internals(connssl, info); |
| 923 |
} |
| 924 |
|
| 925 |
static void multissl_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 926 |
{ |
| 927 |
if(multissl_setup(NULL)) |
| 928 |
return; |
| 929 |
Curl_ssl->close(cf, data); |
| 930 |
} |
| 931 |
|
| 932 |
static ssize_t multissl_recv_plain(struct Curl_cfilter *cf, |
| 933 |
struct Curl_easy *data, |
| 934 |
char *buf, size_t len, CURLcode *code) |
| 935 |
{ |
| 936 |
if(multissl_setup(NULL)) |
| 937 |
return CURLE_FAILED_INIT; |
| 938 |
return Curl_ssl->recv_plain(cf, data, buf, len, code); |
| 939 |
} |
| 940 |
|
| 941 |
static ssize_t multissl_send_plain(struct Curl_cfilter *cf, |
| 942 |
struct Curl_easy *data, |
| 943 |
const void *mem, size_t len, |
| 944 |
CURLcode *code) |
| 945 |
{ |
| 946 |
if(multissl_setup(NULL)) |
| 947 |
return CURLE_FAILED_INIT; |
| 948 |
return Curl_ssl->send_plain(cf, data, mem, len, code); |
| 949 |
} |
| 950 |
|
| 951 |
static const struct Curl_ssl Curl_ssl_multi = { |
| 952 |
{ CURLSSLBACKEND_NONE, "multi" }, /* info */ |
| 953 |
0, /* supports nothing */ |
| 954 |
(size_t)-1, /* something insanely large to be on the safe side */ |
| 955 |
|
| 956 |
multissl_init, /* init */ |
| 957 |
NULL, /* cleanup */ |
| 958 |
multissl_version, /* version */ |
| 959 |
NULL, /* shutdown */ |
| 960 |
NULL, /* data_pending */ |
| 961 |
NULL, /* random */ |
| 962 |
NULL, /* cert_status_request */ |
| 963 |
multissl_connect, /* connect */ |
| 964 |
multissl_adjust_pollset, /* adjust_pollset */ |
| 965 |
multissl_get_internals, /* get_internals */ |
| 966 |
multissl_close, /* close_one */ |
| 967 |
NULL, /* close_all */ |
| 968 |
NULL, /* set_engine */ |
| 969 |
NULL, /* set_engine_default */ |
| 970 |
NULL, /* engines_list */ |
| 971 |
NULL, /* false_start */ |
| 972 |
NULL, /* sha256sum */ |
| 973 |
multissl_recv_plain, /* recv decrypted data */ |
| 974 |
multissl_send_plain, /* send data to encrypt */ |
| 975 |
NULL, /* get_channel_binding */ |
| 976 |
}; |
| 977 |
|
| 978 |
const struct Curl_ssl *Curl_ssl = |
| 979 |
#if defined(CURL_WITH_MULTI_SSL) |
| 980 |
&Curl_ssl_multi; |
| 981 |
#elif defined(USE_WOLFSSL) |
| 982 |
&Curl_ssl_wolfssl; |
| 983 |
#elif defined(USE_GNUTLS) |
| 984 |
&Curl_ssl_gnutls; |
| 985 |
#elif defined(USE_MBEDTLS) |
| 986 |
&Curl_ssl_mbedtls; |
| 987 |
#elif defined(USE_RUSTLS) |
| 988 |
&Curl_ssl_rustls; |
| 989 |
#elif defined(USE_OPENSSL) |
| 990 |
&Curl_ssl_openssl; |
| 991 |
#elif defined(USE_SECTRANSP) |
| 992 |
&Curl_ssl_sectransp; |
| 993 |
#elif defined(USE_SCHANNEL) |
| 994 |
&Curl_ssl_schannel; |
| 995 |
#elif defined(USE_BEARSSL) |
| 996 |
&Curl_ssl_bearssl; |
| 997 |
#else |
| 998 |
#error "Missing struct Curl_ssl for selected SSL backend" |
| 999 |
#endif |
| 1000 |
|
| 1001 |
static const struct Curl_ssl *available_backends[] = { |
| 1002 |
#if defined(USE_WOLFSSL) |
| 1003 |
&Curl_ssl_wolfssl, |
| 1004 |
#endif |
| 1005 |
#if defined(USE_GNUTLS) |
| 1006 |
&Curl_ssl_gnutls, |
| 1007 |
#endif |
| 1008 |
#if defined(USE_MBEDTLS) |
| 1009 |
&Curl_ssl_mbedtls, |
| 1010 |
#endif |
| 1011 |
#if defined(USE_OPENSSL) |
| 1012 |
&Curl_ssl_openssl, |
| 1013 |
#endif |
| 1014 |
#if defined(USE_SECTRANSP) |
| 1015 |
&Curl_ssl_sectransp, |
| 1016 |
#endif |
| 1017 |
#if defined(USE_SCHANNEL) |
| 1018 |
&Curl_ssl_schannel, |
| 1019 |
#endif |
| 1020 |
#if defined(USE_BEARSSL) |
| 1021 |
&Curl_ssl_bearssl, |
| 1022 |
#endif |
| 1023 |
#if defined(USE_RUSTLS) |
| 1024 |
&Curl_ssl_rustls, |
| 1025 |
#endif |
| 1026 |
NULL |
| 1027 |
}; |
| 1028 |
|
| 1029 |
/* Global cleanup */ |
| 1030 |
void Curl_ssl_cleanup(void) |
| 1031 |
{ |
| 1032 |
if(init_ssl) { |
| 1033 |
/* only cleanup if we did a previous init */ |
| 1034 |
if(Curl_ssl->cleanup) |
| 1035 |
Curl_ssl->cleanup(); |
| 1036 |
#if defined(CURL_WITH_MULTI_SSL) |
| 1037 |
Curl_ssl = &Curl_ssl_multi; |
| 1038 |
#endif |
| 1039 |
init_ssl = FALSE; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
static size_t multissl_version(char *buffer, size_t size) |
| 1044 |
{ |
| 1045 |
static const struct Curl_ssl *selected; |
| 1046 |
static char backends[200]; |
| 1047 |
static size_t backends_len; |
| 1048 |
const struct Curl_ssl *current; |
| 1049 |
|
| 1050 |
current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl; |
| 1051 |
|
| 1052 |
if(current != selected) { |
| 1053 |
char *p = backends; |
| 1054 |
char *end = backends + sizeof(backends); |
| 1055 |
int i; |
| 1056 |
|
| 1057 |
selected = current; |
| 1058 |
|
| 1059 |
backends[0] = '\0'; |
| 1060 |
|
| 1061 |
for(i = 0; available_backends[i]; ++i) { |
| 1062 |
char vb[200]; |
| 1063 |
bool paren = (selected != available_backends[i]); |
| 1064 |
|
| 1065 |
if(available_backends[i]->version(vb, sizeof(vb))) { |
| 1066 |
p += msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""), |
| 1067 |
(paren ? "(" : ""), vb, (paren ? ")" : "")); |
| 1068 |
} |
| 1069 |
} |
| 1070 |
|
| 1071 |
backends_len = p - backends; |
| 1072 |
} |
| 1073 |
|
| 1074 |
if(size) { |
| 1075 |
if(backends_len < size) |
| 1076 |
strcpy(buffer, backends); |
| 1077 |
else |
| 1078 |
*buffer = 0; /* did not fit */ |
| 1079 |
} |
| 1080 |
return 0; |
| 1081 |
} |
| 1082 |
|
| 1083 |
static int multissl_setup(const struct Curl_ssl *backend) |
| 1084 |
{ |
| 1085 |
int i; |
| 1086 |
char *env; |
| 1087 |
|
| 1088 |
if(Curl_ssl != &Curl_ssl_multi) |
| 1089 |
return 1; |
| 1090 |
|
| 1091 |
if(backend) { |
| 1092 |
Curl_ssl = backend; |
| 1093 |
return 0; |
| 1094 |
} |
| 1095 |
|
| 1096 |
if(!available_backends[0]) |
| 1097 |
return 1; |
| 1098 |
|
| 1099 |
env = curl_getenv("CURL_SSL_BACKEND"); |
| 1100 |
if(env) { |
| 1101 |
for(i = 0; available_backends[i]; i++) { |
| 1102 |
if(strcasecompare(env, available_backends[i]->info.name)) { |
| 1103 |
Curl_ssl = available_backends[i]; |
| 1104 |
free(env); |
| 1105 |
return 0; |
| 1106 |
} |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
#ifdef CURL_DEFAULT_SSL_BACKEND |
| 1111 |
for(i = 0; available_backends[i]; i++) { |
| 1112 |
if(strcasecompare(CURL_DEFAULT_SSL_BACKEND, |
| 1113 |
available_backends[i]->info.name)) { |
| 1114 |
Curl_ssl = available_backends[i]; |
| 1115 |
free(env); |
| 1116 |
return 0; |
| 1117 |
} |
| 1118 |
} |
| 1119 |
#endif |
| 1120 |
|
| 1121 |
/* Fall back to first available backend */ |
| 1122 |
Curl_ssl = available_backends[0]; |
| 1123 |
free(env); |
| 1124 |
return 0; |
| 1125 |
} |
| 1126 |
|
| 1127 |
/* This function is used to select the SSL backend to use. It is called by |
| 1128 |
curl_global_sslset (easy.c) which uses the global init lock. */ |
| 1129 |
CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
| 1130 |
const curl_ssl_backend ***avail) |
| 1131 |
{ |
| 1132 |
int i; |
| 1133 |
|
| 1134 |
if(avail) |
| 1135 |
*avail = (const curl_ssl_backend **)&available_backends; |
| 1136 |
|
| 1137 |
if(Curl_ssl != &Curl_ssl_multi) |
| 1138 |
return id == Curl_ssl->info.id || |
| 1139 |
(name && strcasecompare(name, Curl_ssl->info.name)) ? |
| 1140 |
CURLSSLSET_OK : |
| 1141 |
#if defined(CURL_WITH_MULTI_SSL) |
| 1142 |
CURLSSLSET_TOO_LATE; |
| 1143 |
#else |
| 1144 |
CURLSSLSET_UNKNOWN_BACKEND; |
| 1145 |
#endif |
| 1146 |
|
| 1147 |
for(i = 0; available_backends[i]; i++) { |
| 1148 |
if(available_backends[i]->info.id == id || |
| 1149 |
(name && strcasecompare(available_backends[i]->info.name, name))) { |
| 1150 |
multissl_setup(available_backends[i]); |
| 1151 |
return CURLSSLSET_OK; |
| 1152 |
} |
| 1153 |
} |
| 1154 |
|
| 1155 |
return CURLSSLSET_UNKNOWN_BACKEND; |
| 1156 |
} |
| 1157 |
|
| 1158 |
#else /* USE_SSL */ |
| 1159 |
CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
| 1160 |
const curl_ssl_backend ***avail) |
| 1161 |
{ |
| 1162 |
(void)id; |
| 1163 |
(void)name; |
| 1164 |
(void)avail; |
| 1165 |
return CURLSSLSET_NO_BACKENDS; |
| 1166 |
} |
| 1167 |
|
| 1168 |
#endif /* !USE_SSL */ |
| 1169 |
|
| 1170 |
#ifdef USE_SSL |
| 1171 |
|
| 1172 |
void Curl_ssl_peer_cleanup(struct ssl_peer *peer) |
| 1173 |
{ |
| 1174 |
Curl_safefree(peer->sni); |
| 1175 |
if(peer->dispname != peer->hostname) |
| 1176 |
free(peer->dispname); |
| 1177 |
peer->dispname = NULL; |
| 1178 |
Curl_safefree(peer->hostname); |
| 1179 |
Curl_safefree(peer->scache_key); |
| 1180 |
peer->type = CURL_SSL_PEER_DNS; |
| 1181 |
} |
| 1182 |
|
| 1183 |
static void cf_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 1184 |
{ |
| 1185 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1186 |
if(connssl) { |
| 1187 |
connssl->ssl_impl->close(cf, data); |
| 1188 |
connssl->state = ssl_connection_none; |
| 1189 |
Curl_ssl_peer_cleanup(&connssl->peer); |
| 1190 |
} |
| 1191 |
cf->connected = FALSE; |
| 1192 |
} |
| 1193 |
|
| 1194 |
static ssl_peer_type get_peer_type(const char *hostname) |
| 1195 |
{ |
| 1196 |
if(hostname && hostname[0]) { |
| 1197 |
#ifdef USE_IPV6 |
| 1198 |
struct in6_addr addr; |
| 1199 |
#else |
| 1200 |
struct in_addr addr; |
| 1201 |
#endif |
| 1202 |
if(curlx_inet_pton(AF_INET, hostname, &addr)) |
| 1203 |
return CURL_SSL_PEER_IPV4; |
| 1204 |
#ifdef USE_IPV6 |
| 1205 |
else if(curlx_inet_pton(AF_INET6, hostname, &addr)) { |
| 1206 |
return CURL_SSL_PEER_IPV6; |
| 1207 |
} |
| 1208 |
#endif |
| 1209 |
} |
| 1210 |
return CURL_SSL_PEER_DNS; |
| 1211 |
} |
| 1212 |
|
| 1213 |
CURLcode Curl_ssl_peer_init(struct ssl_peer *peer, |
| 1214 |
struct Curl_cfilter *cf, |
| 1215 |
const char *tls_id, |
| 1216 |
int transport) |
| 1217 |
{ |
| 1218 |
const char *ehostname, *edispname; |
| 1219 |
CURLcode result = CURLE_OUT_OF_MEMORY; |
| 1220 |
|
| 1221 |
/* We expect a clean struct, e.g. called only ONCE */ |
| 1222 |
DEBUGASSERT(peer); |
| 1223 |
DEBUGASSERT(!peer->hostname); |
| 1224 |
DEBUGASSERT(!peer->dispname); |
| 1225 |
DEBUGASSERT(!peer->sni); |
| 1226 |
/* We need the hostname for SNI negotiation. Once handshaked, this remains |
| 1227 |
* the SNI hostname for the TLS connection. When the connection is reused, |
| 1228 |
* the settings in cf->conn might change. We keep a copy of the hostname we |
| 1229 |
* use for SNI. |
| 1230 |
*/ |
| 1231 |
peer->transport = transport; |
| 1232 |
#ifndef CURL_DISABLE_PROXY |
| 1233 |
if(Curl_ssl_cf_is_proxy(cf)) { |
| 1234 |
ehostname = cf->conn->http_proxy.host.name; |
| 1235 |
edispname = cf->conn->http_proxy.host.dispname; |
| 1236 |
peer->port = cf->conn->http_proxy.port; |
| 1237 |
} |
| 1238 |
else |
| 1239 |
#endif |
| 1240 |
{ |
| 1241 |
ehostname = cf->conn->host.name; |
| 1242 |
edispname = cf->conn->host.dispname; |
| 1243 |
peer->port = cf->conn->remote_port; |
| 1244 |
} |
| 1245 |
|
| 1246 |
/* hostname MUST exist and not be empty */ |
| 1247 |
if(!ehostname || !ehostname[0]) { |
| 1248 |
result = CURLE_FAILED_INIT; |
| 1249 |
goto out; |
| 1250 |
} |
| 1251 |
|
| 1252 |
peer->hostname = strdup(ehostname); |
| 1253 |
if(!peer->hostname) |
| 1254 |
goto out; |
| 1255 |
if(!edispname || !strcmp(ehostname, edispname)) |
| 1256 |
peer->dispname = peer->hostname; |
| 1257 |
else { |
| 1258 |
peer->dispname = strdup(edispname); |
| 1259 |
if(!peer->dispname) |
| 1260 |
goto out; |
| 1261 |
} |
| 1262 |
peer->type = get_peer_type(peer->hostname); |
| 1263 |
if(peer->type == CURL_SSL_PEER_DNS) { |
| 1264 |
/* not an IP address, normalize according to RCC 6066 ch. 3, |
| 1265 |
* max len of SNI is 2^16-1, no trailing dot */ |
| 1266 |
size_t len = strlen(peer->hostname); |
| 1267 |
if(len && (peer->hostname[len-1] == '.')) |
| 1268 |
len--; |
| 1269 |
if(len < USHRT_MAX) { |
| 1270 |
peer->sni = calloc(1, len + 1); |
| 1271 |
if(!peer->sni) |
| 1272 |
goto out; |
| 1273 |
Curl_strntolower(peer->sni, peer->hostname, len); |
| 1274 |
peer->sni[len] = 0; |
| 1275 |
} |
| 1276 |
} |
| 1277 |
|
| 1278 |
result = Curl_ssl_peer_key_make(cf, peer, tls_id, &peer->scache_key); |
| 1279 |
|
| 1280 |
out: |
| 1281 |
if(result) |
| 1282 |
Curl_ssl_peer_cleanup(peer); |
| 1283 |
return result; |
| 1284 |
} |
| 1285 |
|
| 1286 |
static void ssl_cf_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 1287 |
{ |
| 1288 |
struct cf_call_data save; |
| 1289 |
|
| 1290 |
CF_DATA_SAVE(save, cf, data); |
| 1291 |
cf_close(cf, data); |
| 1292 |
CF_DATA_RESTORE(cf, save); |
| 1293 |
cf_ctx_free(cf->ctx); |
| 1294 |
cf->ctx = NULL; |
| 1295 |
} |
| 1296 |
|
| 1297 |
static void ssl_cf_close(struct Curl_cfilter *cf, |
| 1298 |
struct Curl_easy *data) |
| 1299 |
{ |
| 1300 |
struct cf_call_data save; |
| 1301 |
|
| 1302 |
CF_DATA_SAVE(save, cf, data); |
| 1303 |
cf_close(cf, data); |
| 1304 |
if(cf->next) |
| 1305 |
cf->next->cft->do_close(cf->next, data); |
| 1306 |
CF_DATA_RESTORE(cf, save); |
| 1307 |
} |
| 1308 |
|
| 1309 |
static CURLcode ssl_cf_connect(struct Curl_cfilter *cf, |
| 1310 |
struct Curl_easy *data, |
| 1311 |
bool *done) |
| 1312 |
{ |
| 1313 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1314 |
struct cf_call_data save; |
| 1315 |
CURLcode result; |
| 1316 |
|
| 1317 |
if(cf->connected && (connssl->state != ssl_connection_deferred)) { |
| 1318 |
*done = TRUE; |
| 1319 |
return CURLE_OK; |
| 1320 |
} |
| 1321 |
|
| 1322 |
if(!cf->next) { |
| 1323 |
*done = FALSE; |
| 1324 |
return CURLE_FAILED_INIT; |
| 1325 |
} |
| 1326 |
|
| 1327 |
if(!cf->next->connected) { |
| 1328 |
result = cf->next->cft->do_connect(cf->next, data, done); |
| 1329 |
if(result || !*done) |
| 1330 |
return result; |
| 1331 |
} |
| 1332 |
|
| 1333 |
CF_DATA_SAVE(save, cf, data); |
| 1334 |
CURL_TRC_CF(data, cf, "cf_connect()"); |
| 1335 |
DEBUGASSERT(connssl); |
| 1336 |
|
| 1337 |
*done = FALSE; |
| 1338 |
if(!connssl->peer.hostname) { |
| 1339 |
char tls_id[80]; |
| 1340 |
connssl->ssl_impl->version(tls_id, sizeof(tls_id) - 1); |
| 1341 |
result = Curl_ssl_peer_init(&connssl->peer, cf, tls_id, TRNSPRT_TCP); |
| 1342 |
if(result) |
| 1343 |
goto out; |
| 1344 |
} |
| 1345 |
|
| 1346 |
if(!connssl->prefs_checked) { |
| 1347 |
if(!ssl_prefs_check(data)) |
| 1348 |
return CURLE_SSL_CONNECT_ERROR; |
| 1349 |
connssl->prefs_checked = TRUE; |
| 1350 |
} |
| 1351 |
|
| 1352 |
result = connssl->ssl_impl->do_connect(cf, data, done); |
| 1353 |
|
| 1354 |
if(!result && *done) { |
| 1355 |
cf->connected = TRUE; |
| 1356 |
if(connssl->state == ssl_connection_complete) |
| 1357 |
connssl->handshake_done = curlx_now(); |
| 1358 |
/* Connection can be deferred when sending early data */ |
| 1359 |
DEBUGASSERT(connssl->state == ssl_connection_complete || |
| 1360 |
connssl->state == ssl_connection_deferred); |
| 1361 |
DEBUGASSERT(connssl->state != ssl_connection_deferred || |
| 1362 |
connssl->earlydata_state > ssl_earlydata_none); |
| 1363 |
} |
| 1364 |
out: |
| 1365 |
CURL_TRC_CF(data, cf, "cf_connect() -> %d, done=%d", result, *done); |
| 1366 |
CF_DATA_RESTORE(cf, save); |
| 1367 |
return result; |
| 1368 |
} |
| 1369 |
|
| 1370 |
static CURLcode ssl_cf_set_earlydata(struct Curl_cfilter *cf, |
| 1371 |
struct Curl_easy *data, |
| 1372 |
const void *buf, size_t blen) |
| 1373 |
{ |
| 1374 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1375 |
ssize_t nwritten = 0; |
| 1376 |
CURLcode result = CURLE_OK; |
| 1377 |
|
| 1378 |
DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_await); |
| 1379 |
DEBUGASSERT(Curl_bufq_is_empty(&connssl->earlydata)); |
| 1380 |
if(blen) { |
| 1381 |
if(blen > connssl->earlydata_max) |
| 1382 |
blen = connssl->earlydata_max; |
| 1383 |
nwritten = Curl_bufq_write(&connssl->earlydata, buf, blen, &result); |
| 1384 |
CURL_TRC_CF(data, cf, "ssl_cf_set_earlydata(len=%zu) -> %zd", |
| 1385 |
blen, nwritten); |
| 1386 |
if(nwritten < 0) |
| 1387 |
return result; |
| 1388 |
} |
| 1389 |
return CURLE_OK; |
| 1390 |
} |
| 1391 |
|
| 1392 |
static CURLcode ssl_cf_connect_deferred(struct Curl_cfilter *cf, |
| 1393 |
struct Curl_easy *data, |
| 1394 |
const void *buf, size_t blen, |
| 1395 |
bool *done) |
| 1396 |
{ |
| 1397 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1398 |
CURLcode result = CURLE_OK; |
| 1399 |
|
| 1400 |
DEBUGASSERT(connssl->state == ssl_connection_deferred); |
| 1401 |
*done = FALSE; |
| 1402 |
if(connssl->earlydata_state == ssl_earlydata_await) { |
| 1403 |
result = ssl_cf_set_earlydata(cf, data, buf, blen); |
| 1404 |
if(result) |
| 1405 |
return result; |
| 1406 |
/* we buffered any early data we'd like to send. Actually |
| 1407 |
* do the connect now which sends it and performs the handshake. */ |
| 1408 |
connssl->earlydata_state = ssl_earlydata_sending; |
| 1409 |
connssl->earlydata_skip = Curl_bufq_len(&connssl->earlydata); |
| 1410 |
} |
| 1411 |
|
| 1412 |
result = ssl_cf_connect(cf, data, done); |
| 1413 |
|
| 1414 |
if(!result && *done) { |
| 1415 |
Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done); |
| 1416 |
switch(connssl->earlydata_state) { |
| 1417 |
case ssl_earlydata_none: |
| 1418 |
break; |
| 1419 |
case ssl_earlydata_accepted: |
| 1420 |
if(!Curl_ssl_cf_is_proxy(cf)) |
| 1421 |
Curl_pgrsEarlyData(data, (curl_off_t)connssl->earlydata_skip); |
| 1422 |
infof(data, "Server accepted %zu bytes of TLS early data.", |
| 1423 |
connssl->earlydata_skip); |
| 1424 |
break; |
| 1425 |
case ssl_earlydata_rejected: |
| 1426 |
if(!Curl_ssl_cf_is_proxy(cf)) |
| 1427 |
Curl_pgrsEarlyData(data, -(curl_off_t)connssl->earlydata_skip); |
| 1428 |
infof(data, "Server rejected TLS early data."); |
| 1429 |
connssl->earlydata_skip = 0; |
| 1430 |
break; |
| 1431 |
default: |
| 1432 |
/* This should not happen. Either we do not use early data or we |
| 1433 |
* should know if it was accepted or not. */ |
| 1434 |
DEBUGASSERT(NULL); |
| 1435 |
break; |
| 1436 |
} |
| 1437 |
} |
| 1438 |
return result; |
| 1439 |
} |
| 1440 |
|
| 1441 |
static bool ssl_cf_data_pending(struct Curl_cfilter *cf, |
| 1442 |
const struct Curl_easy *data) |
| 1443 |
{ |
| 1444 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1445 |
struct cf_call_data save; |
| 1446 |
bool result; |
| 1447 |
|
| 1448 |
CF_DATA_SAVE(save, cf, data); |
| 1449 |
if(connssl->ssl_impl->data_pending && |
| 1450 |
connssl->ssl_impl->data_pending(cf, data)) |
| 1451 |
result = TRUE; |
| 1452 |
else |
| 1453 |
result = cf->next->cft->has_data_pending(cf->next, data); |
| 1454 |
CF_DATA_RESTORE(cf, save); |
| 1455 |
return result; |
| 1456 |
} |
| 1457 |
|
| 1458 |
static ssize_t ssl_cf_send(struct Curl_cfilter *cf, |
| 1459 |
struct Curl_easy *data, |
| 1460 |
const void *buf, size_t blen, |
| 1461 |
bool eos, CURLcode *err) |
| 1462 |
{ |
| 1463 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1464 |
struct cf_call_data save; |
| 1465 |
ssize_t nwritten = 0, early_written = 0; |
| 1466 |
|
| 1467 |
(void)eos; |
| 1468 |
*err = CURLE_OK; |
| 1469 |
CF_DATA_SAVE(save, cf, data); |
| 1470 |
|
| 1471 |
if(connssl->state == ssl_connection_deferred) { |
| 1472 |
bool done = FALSE; |
| 1473 |
*err = ssl_cf_connect_deferred(cf, data, buf, blen, &done); |
| 1474 |
if(*err) { |
| 1475 |
nwritten = -1; |
| 1476 |
goto out; |
| 1477 |
} |
| 1478 |
else if(!done) { |
| 1479 |
*err = CURLE_AGAIN; |
| 1480 |
nwritten = -1; |
| 1481 |
goto out; |
| 1482 |
} |
| 1483 |
DEBUGASSERT(connssl->state == ssl_connection_complete); |
| 1484 |
} |
| 1485 |
|
| 1486 |
if(connssl->earlydata_skip) { |
| 1487 |
if(connssl->earlydata_skip >= blen) { |
| 1488 |
connssl->earlydata_skip -= blen; |
| 1489 |
*err = CURLE_OK; |
| 1490 |
nwritten = (ssize_t)blen; |
| 1491 |
goto out; |
| 1492 |
} |
| 1493 |
else { |
| 1494 |
early_written = connssl->earlydata_skip; |
| 1495 |
buf = ((const char *)buf) + connssl->earlydata_skip; |
| 1496 |
blen -= connssl->earlydata_skip; |
| 1497 |
connssl->earlydata_skip = 0; |
| 1498 |
} |
| 1499 |
} |
| 1500 |
|
| 1501 |
/* OpenSSL and maybe other TLS libs do not like 0-length writes. Skip. */ |
| 1502 |
if(blen > 0) |
| 1503 |
nwritten = connssl->ssl_impl->send_plain(cf, data, buf, blen, err); |
| 1504 |
|
| 1505 |
if(nwritten >= 0) |
| 1506 |
nwritten += early_written; |
| 1507 |
|
| 1508 |
out: |
| 1509 |
CF_DATA_RESTORE(cf, save); |
| 1510 |
return nwritten; |
| 1511 |
} |
| 1512 |
|
| 1513 |
static ssize_t ssl_cf_recv(struct Curl_cfilter *cf, |
| 1514 |
struct Curl_easy *data, char *buf, size_t len, |
| 1515 |
CURLcode *err) |
| 1516 |
{ |
| 1517 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1518 |
struct cf_call_data save; |
| 1519 |
ssize_t nread; |
| 1520 |
|
| 1521 |
CF_DATA_SAVE(save, cf, data); |
| 1522 |
*err = CURLE_OK; |
| 1523 |
if(connssl->state == ssl_connection_deferred) { |
| 1524 |
bool done = FALSE; |
| 1525 |
*err = ssl_cf_connect_deferred(cf, data, NULL, 0, &done); |
| 1526 |
if(*err) { |
| 1527 |
nread = -1; |
| 1528 |
goto out; |
| 1529 |
} |
| 1530 |
else if(!done) { |
| 1531 |
*err = CURLE_AGAIN; |
| 1532 |
nread = -1; |
| 1533 |
goto out; |
| 1534 |
} |
| 1535 |
DEBUGASSERT(connssl->state == ssl_connection_complete); |
| 1536 |
} |
| 1537 |
|
| 1538 |
nread = connssl->ssl_impl->recv_plain(cf, data, buf, len, err); |
| 1539 |
if(nread > 0) { |
| 1540 |
DEBUGASSERT((size_t)nread <= len); |
| 1541 |
} |
| 1542 |
else if(nread == 0) { |
| 1543 |
/* eof */ |
| 1544 |
*err = CURLE_OK; |
| 1545 |
} |
| 1546 |
|
| 1547 |
out: |
| 1548 |
CURL_TRC_CF(data, cf, "cf_recv(len=%zu) -> %zd, %d", len, |
| 1549 |
nread, *err); |
| 1550 |
CF_DATA_RESTORE(cf, save); |
| 1551 |
return nread; |
| 1552 |
} |
| 1553 |
|
| 1554 |
static CURLcode ssl_cf_shutdown(struct Curl_cfilter *cf, |
| 1555 |
struct Curl_easy *data, |
| 1556 |
bool *done) |
| 1557 |
{ |
| 1558 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1559 |
CURLcode result = CURLE_OK; |
| 1560 |
|
| 1561 |
*done = TRUE; |
| 1562 |
/* If we have done the SSL handshake, shut down the connection cleanly */ |
| 1563 |
if(cf->connected && (connssl->state == ssl_connection_complete) && |
| 1564 |
!cf->shutdown && Curl_ssl->shut_down) { |
| 1565 |
struct cf_call_data save; |
| 1566 |
|
| 1567 |
CF_DATA_SAVE(save, cf, data); |
| 1568 |
result = connssl->ssl_impl->shut_down(cf, data, TRUE, done); |
| 1569 |
CURL_TRC_CF(data, cf, "cf_shutdown -> %d, done=%d", result, *done); |
| 1570 |
CF_DATA_RESTORE(cf, save); |
| 1571 |
cf->shutdown = (result || *done); |
| 1572 |
} |
| 1573 |
return result; |
| 1574 |
} |
| 1575 |
|
| 1576 |
static void ssl_cf_adjust_pollset(struct Curl_cfilter *cf, |
| 1577 |
struct Curl_easy *data, |
| 1578 |
struct easy_pollset *ps) |
| 1579 |
{ |
| 1580 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1581 |
struct cf_call_data save; |
| 1582 |
|
| 1583 |
CF_DATA_SAVE(save, cf, data); |
| 1584 |
connssl->ssl_impl->adjust_pollset(cf, data, ps); |
| 1585 |
CF_DATA_RESTORE(cf, save); |
| 1586 |
} |
| 1587 |
|
| 1588 |
static CURLcode ssl_cf_query(struct Curl_cfilter *cf, |
| 1589 |
struct Curl_easy *data, |
| 1590 |
int query, int *pres1, void *pres2) |
| 1591 |
{ |
| 1592 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1593 |
|
| 1594 |
switch(query) { |
| 1595 |
case CF_QUERY_TIMER_APPCONNECT: { |
| 1596 |
struct curltime *when = pres2; |
| 1597 |
if(cf->connected && !Curl_ssl_cf_is_proxy(cf)) |
| 1598 |
*when = connssl->handshake_done; |
| 1599 |
return CURLE_OK; |
| 1600 |
} |
| 1601 |
default: |
| 1602 |
break; |
| 1603 |
} |
| 1604 |
return cf->next ? |
| 1605 |
cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
| 1606 |
CURLE_UNKNOWN_OPTION; |
| 1607 |
} |
| 1608 |
|
| 1609 |
static bool cf_ssl_is_alive(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 1610 |
bool *input_pending) |
| 1611 |
{ |
| 1612 |
/* |
| 1613 |
* This function tries to determine connection status. |
| 1614 |
*/ |
| 1615 |
return cf->next ? |
| 1616 |
cf->next->cft->is_alive(cf->next, data, input_pending) : |
| 1617 |
FALSE; /* pessimistic in absence of data */ |
| 1618 |
} |
| 1619 |
|
| 1620 |
struct Curl_cftype Curl_cft_ssl = { |
| 1621 |
"SSL", |
| 1622 |
CF_TYPE_SSL, |
| 1623 |
CURL_LOG_LVL_NONE, |
| 1624 |
ssl_cf_destroy, |
| 1625 |
ssl_cf_connect, |
| 1626 |
ssl_cf_close, |
| 1627 |
ssl_cf_shutdown, |
| 1628 |
Curl_cf_def_get_host, |
| 1629 |
ssl_cf_adjust_pollset, |
| 1630 |
ssl_cf_data_pending, |
| 1631 |
ssl_cf_send, |
| 1632 |
ssl_cf_recv, |
| 1633 |
Curl_cf_def_cntrl, |
| 1634 |
cf_ssl_is_alive, |
| 1635 |
Curl_cf_def_conn_keep_alive, |
| 1636 |
ssl_cf_query, |
| 1637 |
}; |
| 1638 |
|
| 1639 |
#ifndef CURL_DISABLE_PROXY |
| 1640 |
|
| 1641 |
struct Curl_cftype Curl_cft_ssl_proxy = { |
| 1642 |
"SSL-PROXY", |
| 1643 |
CF_TYPE_SSL|CF_TYPE_PROXY, |
| 1644 |
CURL_LOG_LVL_NONE, |
| 1645 |
ssl_cf_destroy, |
| 1646 |
ssl_cf_connect, |
| 1647 |
ssl_cf_close, |
| 1648 |
ssl_cf_shutdown, |
| 1649 |
Curl_cf_def_get_host, |
| 1650 |
ssl_cf_adjust_pollset, |
| 1651 |
ssl_cf_data_pending, |
| 1652 |
ssl_cf_send, |
| 1653 |
ssl_cf_recv, |
| 1654 |
Curl_cf_def_cntrl, |
| 1655 |
cf_ssl_is_alive, |
| 1656 |
Curl_cf_def_conn_keep_alive, |
| 1657 |
Curl_cf_def_query, |
| 1658 |
}; |
| 1659 |
|
| 1660 |
#endif /* !CURL_DISABLE_PROXY */ |
| 1661 |
|
| 1662 |
static CURLcode cf_ssl_create(struct Curl_cfilter **pcf, |
| 1663 |
struct Curl_easy *data, |
| 1664 |
struct connectdata *conn) |
| 1665 |
{ |
| 1666 |
struct Curl_cfilter *cf = NULL; |
| 1667 |
struct ssl_connect_data *ctx; |
| 1668 |
CURLcode result; |
| 1669 |
|
| 1670 |
DEBUGASSERT(data->conn); |
| 1671 |
|
| 1672 |
#ifdef CURL_DISABLE_HTTP |
| 1673 |
/* We only support ALPN for HTTP so far. */ |
| 1674 |
DEBUGASSERT(!conn->bits.tls_enable_alpn); |
| 1675 |
ctx = cf_ctx_new(data, NULL); |
| 1676 |
#else |
| 1677 |
ctx = cf_ctx_new(data, alpn_get_spec(data->state.http_neg.wanted, |
| 1678 |
conn->bits.tls_enable_alpn)); |
| 1679 |
#endif |
| 1680 |
if(!ctx) { |
| 1681 |
result = CURLE_OUT_OF_MEMORY; |
| 1682 |
goto out; |
| 1683 |
} |
| 1684 |
|
| 1685 |
result = Curl_cf_create(&cf, &Curl_cft_ssl, ctx); |
| 1686 |
|
| 1687 |
out: |
| 1688 |
if(result) |
| 1689 |
cf_ctx_free(ctx); |
| 1690 |
*pcf = result ? NULL : cf; |
| 1691 |
return result; |
| 1692 |
} |
| 1693 |
|
| 1694 |
CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data, |
| 1695 |
struct connectdata *conn, |
| 1696 |
int sockindex) |
| 1697 |
{ |
| 1698 |
struct Curl_cfilter *cf; |
| 1699 |
CURLcode result; |
| 1700 |
|
| 1701 |
result = cf_ssl_create(&cf, data, conn); |
| 1702 |
if(!result) |
| 1703 |
Curl_conn_cf_add(data, conn, sockindex, cf); |
| 1704 |
return result; |
| 1705 |
} |
| 1706 |
|
| 1707 |
CURLcode Curl_cf_ssl_insert_after(struct Curl_cfilter *cf_at, |
| 1708 |
struct Curl_easy *data) |
| 1709 |
{ |
| 1710 |
struct Curl_cfilter *cf; |
| 1711 |
CURLcode result; |
| 1712 |
|
| 1713 |
result = cf_ssl_create(&cf, data, cf_at->conn); |
| 1714 |
if(!result) |
| 1715 |
Curl_conn_cf_insert_after(cf_at, cf); |
| 1716 |
return result; |
| 1717 |
} |
| 1718 |
|
| 1719 |
#ifndef CURL_DISABLE_PROXY |
| 1720 |
|
| 1721 |
static CURLcode cf_ssl_proxy_create(struct Curl_cfilter **pcf, |
| 1722 |
struct Curl_easy *data, |
| 1723 |
struct connectdata *conn) |
| 1724 |
{ |
| 1725 |
struct Curl_cfilter *cf = NULL; |
| 1726 |
struct ssl_connect_data *ctx; |
| 1727 |
CURLcode result; |
| 1728 |
bool use_alpn = conn->bits.tls_enable_alpn; |
| 1729 |
http_majors allowed = CURL_HTTP_V1x; |
| 1730 |
|
| 1731 |
#ifdef USE_HTTP2 |
| 1732 |
if(conn->http_proxy.proxytype == CURLPROXY_HTTPS2) { |
| 1733 |
use_alpn = TRUE; |
| 1734 |
allowed = (CURL_HTTP_V1x|CURL_HTTP_V2x); |
| 1735 |
} |
| 1736 |
#endif |
| 1737 |
|
| 1738 |
ctx = cf_ctx_new(data, alpn_get_spec(allowed, use_alpn)); |
| 1739 |
if(!ctx) { |
| 1740 |
result = CURLE_OUT_OF_MEMORY; |
| 1741 |
goto out; |
| 1742 |
} |
| 1743 |
result = Curl_cf_create(&cf, &Curl_cft_ssl_proxy, ctx); |
| 1744 |
|
| 1745 |
out: |
| 1746 |
if(result) |
| 1747 |
cf_ctx_free(ctx); |
| 1748 |
*pcf = result ? NULL : cf; |
| 1749 |
return result; |
| 1750 |
} |
| 1751 |
|
| 1752 |
CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at, |
| 1753 |
struct Curl_easy *data) |
| 1754 |
{ |
| 1755 |
struct Curl_cfilter *cf; |
| 1756 |
CURLcode result; |
| 1757 |
|
| 1758 |
result = cf_ssl_proxy_create(&cf, data, cf_at->conn); |
| 1759 |
if(!result) |
| 1760 |
Curl_conn_cf_insert_after(cf_at, cf); |
| 1761 |
return result; |
| 1762 |
} |
| 1763 |
|
| 1764 |
#endif /* !CURL_DISABLE_PROXY */ |
| 1765 |
|
| 1766 |
bool Curl_ssl_supports(struct Curl_easy *data, unsigned int ssl_option) |
| 1767 |
{ |
| 1768 |
(void)data; |
| 1769 |
return (Curl_ssl->supports & ssl_option); |
| 1770 |
} |
| 1771 |
|
| 1772 |
static struct Curl_cfilter *get_ssl_filter(struct Curl_cfilter *cf) |
| 1773 |
{ |
| 1774 |
for(; cf; cf = cf->next) { |
| 1775 |
if(cf->cft == &Curl_cft_ssl) |
| 1776 |
return cf; |
| 1777 |
#ifndef CURL_DISABLE_PROXY |
| 1778 |
if(cf->cft == &Curl_cft_ssl_proxy) |
| 1779 |
return cf; |
| 1780 |
#endif |
| 1781 |
} |
| 1782 |
return NULL; |
| 1783 |
} |
| 1784 |
|
| 1785 |
|
| 1786 |
void *Curl_ssl_get_internals(struct Curl_easy *data, int sockindex, |
| 1787 |
CURLINFO info, int n) |
| 1788 |
{ |
| 1789 |
void *result = NULL; |
| 1790 |
(void)n; |
| 1791 |
if(data->conn) { |
| 1792 |
struct Curl_cfilter *cf; |
| 1793 |
/* get first SSL filter in chain, if any is present */ |
| 1794 |
cf = get_ssl_filter(data->conn->cfilter[sockindex]); |
| 1795 |
if(cf) { |
| 1796 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1797 |
struct cf_call_data save; |
| 1798 |
CF_DATA_SAVE(save, cf, data); |
| 1799 |
result = connssl->ssl_impl->get_internals(cf->ctx, info); |
| 1800 |
CF_DATA_RESTORE(cf, save); |
| 1801 |
} |
| 1802 |
} |
| 1803 |
return result; |
| 1804 |
} |
| 1805 |
|
| 1806 |
static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf, |
| 1807 |
struct Curl_easy *data, |
| 1808 |
bool send_shutdown, bool *done) |
| 1809 |
{ |
| 1810 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1811 |
struct cf_call_data save; |
| 1812 |
CURLcode result = CURLE_OK; |
| 1813 |
timediff_t timeout_ms; |
| 1814 |
int what, loop = 10; |
| 1815 |
|
| 1816 |
if(cf->shutdown) { |
| 1817 |
*done = TRUE; |
| 1818 |
return CURLE_OK; |
| 1819 |
} |
| 1820 |
CF_DATA_SAVE(save, cf, data); |
| 1821 |
|
| 1822 |
*done = FALSE; |
| 1823 |
while(!result && !*done && loop--) { |
| 1824 |
timeout_ms = Curl_shutdown_timeleft(cf->conn, cf->sockindex, NULL); |
| 1825 |
|
| 1826 |
if(timeout_ms < 0) { |
| 1827 |
/* no need to continue if time is already up */ |
| 1828 |
failf(data, "SSL shutdown timeout"); |
| 1829 |
return CURLE_OPERATION_TIMEDOUT; |
| 1830 |
} |
| 1831 |
|
| 1832 |
result = connssl->ssl_impl->shut_down(cf, data, send_shutdown, done); |
| 1833 |
if(result ||*done) |
| 1834 |
goto out; |
| 1835 |
|
| 1836 |
if(connssl->io_need) { |
| 1837 |
what = Curl_conn_cf_poll(cf, data, timeout_ms); |
| 1838 |
if(what < 0) { |
| 1839 |
/* fatal error */ |
| 1840 |
failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO); |
| 1841 |
result = CURLE_RECV_ERROR; |
| 1842 |
goto out; |
| 1843 |
} |
| 1844 |
else if(0 == what) { |
| 1845 |
/* timeout */ |
| 1846 |
failf(data, "SSL shutdown timeout"); |
| 1847 |
result = CURLE_OPERATION_TIMEDOUT; |
| 1848 |
goto out; |
| 1849 |
} |
| 1850 |
/* socket is readable or writable */ |
| 1851 |
} |
| 1852 |
} |
| 1853 |
out: |
| 1854 |
CF_DATA_RESTORE(cf, save); |
| 1855 |
cf->shutdown = (result || *done); |
| 1856 |
return result; |
| 1857 |
} |
| 1858 |
|
| 1859 |
CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data, |
| 1860 |
int sockindex, bool send_shutdown) |
| 1861 |
{ |
| 1862 |
struct Curl_cfilter *cf, *head; |
| 1863 |
CURLcode result = CURLE_OK; |
| 1864 |
|
| 1865 |
head = data->conn ? data->conn->cfilter[sockindex] : NULL; |
| 1866 |
for(cf = head; cf; cf = cf->next) { |
| 1867 |
if(cf->cft == &Curl_cft_ssl) { |
| 1868 |
bool done; |
| 1869 |
CURL_TRC_CF(data, cf, "shutdown and remove SSL, start"); |
| 1870 |
Curl_shutdown_start(data, sockindex, 0, NULL); |
| 1871 |
result = vtls_shutdown_blocking(cf, data, send_shutdown, &done); |
| 1872 |
Curl_shutdown_clear(data, sockindex); |
| 1873 |
if(!result && !done) /* blocking failed? */ |
| 1874 |
result = CURLE_SSL_SHUTDOWN_FAILED; |
| 1875 |
Curl_conn_cf_discard_sub(head, cf, data, FALSE); |
| 1876 |
CURL_TRC_CF(data, cf, "shutdown and remove SSL, done -> %d", result); |
| 1877 |
break; |
| 1878 |
} |
| 1879 |
} |
| 1880 |
return result; |
| 1881 |
} |
| 1882 |
|
| 1883 |
bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf) |
| 1884 |
{ |
| 1885 |
return (cf->cft->flags & CF_TYPE_SSL) && (cf->cft->flags & CF_TYPE_PROXY); |
| 1886 |
} |
| 1887 |
|
| 1888 |
struct ssl_config_data * |
| 1889 |
Curl_ssl_cf_get_config(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 1890 |
{ |
| 1891 |
#ifdef CURL_DISABLE_PROXY |
| 1892 |
(void)cf; |
| 1893 |
return &data->set.ssl; |
| 1894 |
#else |
| 1895 |
return Curl_ssl_cf_is_proxy(cf) ? &data->set.proxy_ssl : &data->set.ssl; |
| 1896 |
#endif |
| 1897 |
} |
| 1898 |
|
| 1899 |
struct ssl_primary_config * |
| 1900 |
Curl_ssl_cf_get_primary_config(struct Curl_cfilter *cf) |
| 1901 |
{ |
| 1902 |
#ifdef CURL_DISABLE_PROXY |
| 1903 |
return &cf->conn->ssl_config; |
| 1904 |
#else |
| 1905 |
return Curl_ssl_cf_is_proxy(cf) ? |
| 1906 |
&cf->conn->proxy_ssl_config : &cf->conn->ssl_config; |
| 1907 |
#endif |
| 1908 |
} |
| 1909 |
|
| 1910 |
CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf, |
| 1911 |
const struct alpn_spec *spec) |
| 1912 |
{ |
| 1913 |
size_t i, len; |
| 1914 |
int off = 0; |
| 1915 |
unsigned char blen; |
| 1916 |
|
| 1917 |
memset(buf, 0, sizeof(*buf)); |
| 1918 |
for(i = 0; spec && i < spec->count; ++i) { |
| 1919 |
len = strlen(spec->entries[i]); |
| 1920 |
if(len >= ALPN_NAME_MAX) |
| 1921 |
return CURLE_FAILED_INIT; |
| 1922 |
blen = (unsigned char)len; |
| 1923 |
if(off + blen + 1 >= (int)sizeof(buf->data)) |
| 1924 |
return CURLE_FAILED_INIT; |
| 1925 |
buf->data[off++] = blen; |
| 1926 |
memcpy(buf->data + off, spec->entries[i], blen); |
| 1927 |
off += blen; |
| 1928 |
} |
| 1929 |
buf->len = off; |
| 1930 |
return CURLE_OK; |
| 1931 |
} |
| 1932 |
|
| 1933 |
CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf, |
| 1934 |
const struct alpn_spec *spec) |
| 1935 |
{ |
| 1936 |
size_t i, len; |
| 1937 |
size_t off = 0; |
| 1938 |
|
| 1939 |
memset(buf, 0, sizeof(*buf)); |
| 1940 |
for(i = 0; spec && i < spec->count; ++i) { |
| 1941 |
len = strlen(spec->entries[i]); |
| 1942 |
if(len >= ALPN_NAME_MAX) |
| 1943 |
return CURLE_FAILED_INIT; |
| 1944 |
if(off + len + 2 >= sizeof(buf->data)) |
| 1945 |
return CURLE_FAILED_INIT; |
| 1946 |
if(off) |
| 1947 |
buf->data[off++] = ','; |
| 1948 |
memcpy(buf->data + off, spec->entries[i], len); |
| 1949 |
off += len; |
| 1950 |
} |
| 1951 |
buf->data[off] = '\0'; |
| 1952 |
buf->len = (int)off; |
| 1953 |
return CURLE_OK; |
| 1954 |
} |
| 1955 |
|
| 1956 |
bool Curl_alpn_contains_proto(const struct alpn_spec *spec, |
| 1957 |
const char *proto) |
| 1958 |
{ |
| 1959 |
size_t i, plen = proto ? strlen(proto) : 0; |
| 1960 |
for(i = 0; spec && plen && i < spec->count; ++i) { |
| 1961 |
size_t slen = strlen(spec->entries[i]); |
| 1962 |
if((slen == plen) && !memcmp(proto, spec->entries[i], plen)) |
| 1963 |
return TRUE; |
| 1964 |
} |
| 1965 |
return FALSE; |
| 1966 |
} |
| 1967 |
|
| 1968 |
void Curl_alpn_restrict_to(struct alpn_spec *spec, const char *proto) |
| 1969 |
{ |
| 1970 |
size_t plen = strlen(proto); |
| 1971 |
DEBUGASSERT(plen < sizeof(spec->entries[0])); |
| 1972 |
if(plen < sizeof(spec->entries[0])) { |
| 1973 |
memcpy(spec->entries[0], proto, plen + 1); |
| 1974 |
spec->count = 1; |
| 1975 |
} |
| 1976 |
} |
| 1977 |
|
| 1978 |
void Curl_alpn_copy(struct alpn_spec *dest, const struct alpn_spec *src) |
| 1979 |
{ |
| 1980 |
if(src) |
| 1981 |
memcpy(dest, src, sizeof(*dest)); |
| 1982 |
else |
| 1983 |
memset(dest, 0, sizeof(*dest)); |
| 1984 |
} |
| 1985 |
|
| 1986 |
CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf, |
| 1987 |
struct Curl_easy *data, |
| 1988 |
struct ssl_connect_data *connssl, |
| 1989 |
const unsigned char *proto, |
| 1990 |
size_t proto_len) |
| 1991 |
{ |
| 1992 |
CURLcode result = CURLE_OK; |
| 1993 |
unsigned char *palpn = |
| 1994 |
#ifndef CURL_DISABLE_PROXY |
| 1995 |
(cf->conn->bits.tunnel_proxy && Curl_ssl_cf_is_proxy(cf)) ? |
| 1996 |
&cf->conn->proxy_alpn : &cf->conn->alpn |
| 1997 |
#else |
| 1998 |
&cf->conn->alpn |
| 1999 |
#endif |
| 2000 |
; |
| 2001 |
|
| 2002 |
if(connssl->negotiated.alpn) { |
| 2003 |
/* When we ask for a specific ALPN protocol, we need the confirmation |
| 2004 |
* of it by the server, as we have installed protocol handler and |
| 2005 |
* connection filter chain for exactly this protocol. */ |
| 2006 |
if(!proto_len) { |
| 2007 |
failf(data, "ALPN: asked for '%s' from previous session, " |
| 2008 |
"but server did not confirm it. Refusing to continue.", |
| 2009 |
connssl->negotiated.alpn); |
| 2010 |
result = CURLE_SSL_CONNECT_ERROR; |
| 2011 |
goto out; |
| 2012 |
} |
| 2013 |
else if((strlen(connssl->negotiated.alpn) != proto_len) || |
| 2014 |
memcmp(connssl->negotiated.alpn, proto, proto_len)) { |
| 2015 |
failf(data, "ALPN: asked for '%s' from previous session, but server " |
| 2016 |
"selected '%.*s'. Refusing to continue.", |
| 2017 |
connssl->negotiated.alpn, (int)proto_len, proto); |
| 2018 |
result = CURLE_SSL_CONNECT_ERROR; |
| 2019 |
goto out; |
| 2020 |
} |
| 2021 |
/* ALPN is exactly what we asked for, done. */ |
| 2022 |
infof(data, "ALPN: server confirmed to use '%s'", |
| 2023 |
connssl->negotiated.alpn); |
| 2024 |
goto out; |
| 2025 |
} |
| 2026 |
|
| 2027 |
if(proto && proto_len) { |
| 2028 |
if(memchr(proto, '\0', proto_len)) { |
| 2029 |
failf(data, "ALPN: server selected protocol contains NUL. " |
| 2030 |
"Refusing to continue."); |
| 2031 |
result = CURLE_SSL_CONNECT_ERROR; |
| 2032 |
goto out; |
| 2033 |
} |
| 2034 |
connssl->negotiated.alpn = malloc(proto_len + 1); |
| 2035 |
if(!connssl->negotiated.alpn) |
| 2036 |
return CURLE_OUT_OF_MEMORY; |
| 2037 |
memcpy(connssl->negotiated.alpn, proto, proto_len); |
| 2038 |
connssl->negotiated.alpn[proto_len] = 0; |
| 2039 |
} |
| 2040 |
|
| 2041 |
if(proto && proto_len) { |
| 2042 |
if(proto_len == ALPN_HTTP_1_1_LENGTH && |
| 2043 |
!memcmp(ALPN_HTTP_1_1, proto, ALPN_HTTP_1_1_LENGTH)) { |
| 2044 |
*palpn = CURL_HTTP_VERSION_1_1; |
| 2045 |
} |
| 2046 |
#ifdef USE_HTTP2 |
| 2047 |
else if(proto_len == ALPN_H2_LENGTH && |
| 2048 |
!memcmp(ALPN_H2, proto, ALPN_H2_LENGTH)) { |
| 2049 |
*palpn = CURL_HTTP_VERSION_2; |
| 2050 |
} |
| 2051 |
#endif |
| 2052 |
#ifdef USE_HTTP3 |
| 2053 |
else if(proto_len == ALPN_H3_LENGTH && |
| 2054 |
!memcmp(ALPN_H3, proto, ALPN_H3_LENGTH)) { |
| 2055 |
*palpn = CURL_HTTP_VERSION_3; |
| 2056 |
} |
| 2057 |
#endif |
| 2058 |
else { |
| 2059 |
*palpn = CURL_HTTP_VERSION_NONE; |
| 2060 |
failf(data, "unsupported ALPN protocol: '%.*s'", (int)proto_len, proto); |
| 2061 |
/* Previous code just ignored it and some vtls backends even ignore the |
| 2062 |
* return code of this function. */ |
| 2063 |
/* return CURLE_NOT_BUILT_IN; */ |
| 2064 |
goto out; |
| 2065 |
} |
| 2066 |
|
| 2067 |
if(connssl->state == ssl_connection_deferred) |
| 2068 |
infof(data, VTLS_INFOF_ALPN_DEFERRED, (int)proto_len, proto); |
| 2069 |
else |
| 2070 |
infof(data, VTLS_INFOF_ALPN_ACCEPTED, (int)proto_len, proto); |
| 2071 |
} |
| 2072 |
else { |
| 2073 |
*palpn = CURL_HTTP_VERSION_NONE; |
| 2074 |
if(connssl->state == ssl_connection_deferred) |
| 2075 |
infof(data, VTLS_INFOF_NO_ALPN_DEFERRED); |
| 2076 |
else |
| 2077 |
infof(data, VTLS_INFOF_NO_ALPN); |
| 2078 |
} |
| 2079 |
|
| 2080 |
out: |
| 2081 |
return result; |
| 2082 |
} |
| 2083 |
|
| 2084 |
#endif /* USE_SSL */ |