/src/curl/lib/vtls/vtls.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) 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.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 | | #include "urldata.h" |
44 | | #include "cfilters.h" |
45 | | |
46 | | #include "vtls/vtls.h" /* generic SSL protos etc */ |
47 | | #include "vtls/vtls_int.h" |
48 | | #include "vtls/vtls_scache.h" |
49 | | #include "vtls/keylog.h" |
50 | | |
51 | | #include "vtls/openssl.h" /* OpenSSL versions */ |
52 | | #include "vtls/gtls.h" /* GnuTLS versions */ |
53 | | #include "vtls/wolfssl.h" /* wolfSSL versions */ |
54 | | #include "vtls/schannel.h" /* Schannel SSPI version */ |
55 | | #include "vtls/mbedtls.h" /* mbedTLS versions */ |
56 | | #include "vtls/rustls.h" /* Rustls versions */ |
57 | | |
58 | | #include "slist.h" |
59 | | #include "curl_trc.h" |
60 | | #include "strcase.h" |
61 | | #include "url.h" |
62 | | #include "progress.h" |
63 | | #include "curlx/win32-fopen.h" |
64 | | #include "curl_sha256.h" |
65 | | #include "curlx/base64.h" |
66 | | #include "curlx/inet_pton.h" |
67 | | #include "connect.h" |
68 | | #include "select.h" |
69 | | #include "setopt.h" |
70 | | #include "vdns/cf-dns.h" |
71 | | #include "curlx/strdup.h" |
72 | | #include "curlx/strcopy.h" |
73 | | |
74 | | #ifdef USE_APPLE_SECTRUST |
75 | | #include <Security/Security.h> |
76 | | #endif |
77 | | |
78 | | #ifdef USE_SSL |
79 | | #if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_PROXY) |
80 | | static const struct alpn_spec ALPN_SPEC_H11 = { |
81 | | { ALPN_HTTP_1_1 }, 1 |
82 | | }; |
83 | | static const struct alpn_spec ALPN_SPEC_H10_H11 = { |
84 | | { ALPN_HTTP_1_0, ALPN_HTTP_1_1 }, 2 |
85 | | }; |
86 | | #ifdef USE_HTTP2 |
87 | | static const struct alpn_spec ALPN_SPEC_H2 = { |
88 | | { ALPN_H2 }, 1 |
89 | | }; |
90 | | static const struct alpn_spec ALPN_SPEC_H2_H11 = { |
91 | | { ALPN_H2, ALPN_HTTP_1_1 }, 2 |
92 | | }; |
93 | | static const struct alpn_spec ALPN_SPEC_H11_H2 = { |
94 | | { ALPN_HTTP_1_1, ALPN_H2 }, 2 |
95 | | }; |
96 | | #endif /* USE_HTTP2 */ |
97 | | |
98 | | static const struct alpn_spec *alpn_get_spec(http_majors wanted, |
99 | | http_majors preferred, |
100 | | bool only_http_10, |
101 | | bool use_alpn) |
102 | 0 | { |
103 | 0 | if(!use_alpn) |
104 | 0 | return NULL; |
105 | | /* If HTTP/1.0 is the wanted protocol then use ALPN http/1.0 and http/1.1. |
106 | | This is for compatibility reasons since some HTTP/1.0 servers with old |
107 | | ALPN implementations understand ALPN http/1.1 but not http/1.0. */ |
108 | 0 | if(only_http_10 && (wanted & CURL_HTTP_V1x)) |
109 | 0 | return &ALPN_SPEC_H10_H11; |
110 | 0 | #ifdef USE_HTTP2 |
111 | 0 | if(wanted & CURL_HTTP_V2x) { |
112 | 0 | if(wanted & CURL_HTTP_V1x) |
113 | 0 | return (preferred == CURL_HTTP_V1x) ? |
114 | 0 | &ALPN_SPEC_H11_H2 : &ALPN_SPEC_H2_H11; |
115 | 0 | return &ALPN_SPEC_H2; |
116 | 0 | } |
117 | | #else |
118 | | (void)wanted; |
119 | | (void)preferred; |
120 | | #endif |
121 | 0 | return &ALPN_SPEC_H11; |
122 | 0 | } |
123 | | #endif /* !CURL_DISABLE_HTTP || !CURL_DISABLE_PROXY */ |
124 | | #endif /* USE_SSL */ |
125 | | |
126 | | #ifdef USE_SSL |
127 | | static int multissl_setup(const struct Curl_ssl *backend); |
128 | | #endif |
129 | | |
130 | | curl_sslbackend Curl_ssl_backend(void) |
131 | 0 | { |
132 | 0 | #ifdef USE_SSL |
133 | 0 | multissl_setup(NULL); |
134 | 0 | return Curl_ssl->info.id; |
135 | | #else |
136 | | return CURLSSLBACKEND_NONE; |
137 | | #endif |
138 | 0 | } |
139 | | |
140 | | #ifdef USE_SSL |
141 | | |
142 | | /* "global" init done? */ |
143 | | static bool init_ssl = FALSE; |
144 | | |
145 | | /** |
146 | | * Global SSL init |
147 | | * |
148 | | * @retval 0 error initializing SSL |
149 | | * @retval 1 SSL initialized successfully |
150 | | */ |
151 | | int Curl_ssl_init(void) |
152 | 1 | { |
153 | | /* make sure this is only done once */ |
154 | 1 | if(init_ssl) |
155 | 0 | return 1; |
156 | 1 | init_ssl = TRUE; /* never again */ |
157 | | |
158 | 1 | if(Curl_ssl->init) |
159 | 1 | return Curl_ssl->init(); |
160 | 0 | return 1; |
161 | 1 | } |
162 | | |
163 | | static bool ssl_prefs_check(struct Curl_cfilter *cf, |
164 | | struct Curl_easy *data) |
165 | 0 | { |
166 | 0 | struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf); |
167 | | /* check for CURLOPT_SSLVERSION invalid parameter value */ |
168 | 0 | if(conn_config->version >= CURL_SSLVERSION_LAST) { |
169 | 0 | failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION"); |
170 | 0 | return FALSE; |
171 | 0 | } |
172 | | |
173 | 0 | switch(conn_config->version_max) { |
174 | 0 | case CURL_SSLVERSION_MAX_NONE: |
175 | 0 | case CURL_SSLVERSION_MAX_DEFAULT: |
176 | 0 | break; |
177 | | |
178 | 0 | default: |
179 | 0 | if((conn_config->version_max >> 16) < conn_config->version) { |
180 | 0 | failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION"); |
181 | 0 | return FALSE; |
182 | 0 | } |
183 | 0 | } |
184 | | |
185 | 0 | return TRUE; |
186 | 0 | } |
187 | | |
188 | | static struct ssl_connect_data *cf_ctx_new(struct Curl_easy *data, |
189 | | const struct alpn_spec *alpn) |
190 | 0 | { |
191 | 0 | struct ssl_connect_data *ctx; |
192 | |
|
193 | 0 | (void)data; |
194 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
195 | 0 | if(!ctx) |
196 | 0 | return NULL; |
197 | | |
198 | 0 | ctx->ssl_impl = Curl_ssl; |
199 | 0 | ctx->alpn = alpn; |
200 | 0 | Curl_bufq_init2(&ctx->earlydata, CURL_SSL_EARLY_MAX, 1, BUFQ_OPT_NO_SPARES); |
201 | 0 | ctx->backend = curlx_calloc(1, ctx->ssl_impl->sizeof_ssl_backend_data); |
202 | 0 | if(!ctx->backend) { |
203 | 0 | curlx_free(ctx); |
204 | 0 | return NULL; |
205 | 0 | } |
206 | 0 | return ctx; |
207 | 0 | } |
208 | | |
209 | | static void cf_ctx_free(struct ssl_connect_data *ctx) |
210 | 0 | { |
211 | 0 | if(ctx) { |
212 | 0 | curlx_safefree(ctx->negotiated.alpn); |
213 | 0 | Curl_bufq_free(&ctx->earlydata); |
214 | 0 | curlx_free(ctx->backend); |
215 | 0 | curlx_free(ctx); |
216 | 0 | } |
217 | 0 | } |
218 | | |
219 | | CURLcode Curl_ssl_get_channel_binding(struct Curl_easy *data, |
220 | | struct dynbuf *binding) |
221 | 0 | { |
222 | 0 | if(Curl_ssl->get_channel_binding) |
223 | 0 | return Curl_ssl->get_channel_binding(data, binding); |
224 | 0 | return CURLE_OK; |
225 | 0 | } |
226 | | |
227 | | void Curl_ssl_close_all(struct Curl_easy *data) |
228 | 54 | { |
229 | 54 | if(Curl_ssl->close_all) |
230 | 54 | Curl_ssl->close_all(data); |
231 | 54 | } |
232 | | |
233 | | CURLcode Curl_ssl_adjust_pollset(struct Curl_cfilter *cf, |
234 | | struct Curl_easy *data, |
235 | | struct easy_pollset *ps) |
236 | 0 | { |
237 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
238 | |
|
239 | 0 | if(connssl->io_need) { |
240 | 0 | curl_socket_t sock = cf->chain->sock; |
241 | 0 | CURLcode result = CURLE_OK; |
242 | 0 | if(sock != CURL_SOCKET_BAD) { |
243 | 0 | if(connssl->io_need & CURL_SSL_IO_NEED_SEND) { |
244 | 0 | result = Curl_pollset_set_out_only(data, ps, sock); |
245 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, POLLOUT fd=%" FMT_SOCKET_T, |
246 | 0 | sock); |
247 | 0 | } |
248 | 0 | else { |
249 | 0 | result = Curl_pollset_set_in_only(data, ps, sock); |
250 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, POLLIN fd=%" FMT_SOCKET_T, |
251 | 0 | sock); |
252 | 0 | } |
253 | 0 | } |
254 | 0 | return result; |
255 | 0 | } |
256 | 0 | return CURLE_OK; |
257 | 0 | } |
258 | | |
259 | | /* Return list of OpenSSL crypto engine names. */ |
260 | | struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data) |
261 | 0 | { |
262 | 0 | if(Curl_ssl->engines_list) |
263 | 0 | return Curl_ssl->engines_list(data); |
264 | 0 | return NULL; |
265 | 0 | } |
266 | | |
267 | | static size_t multissl_version(char *buffer, size_t size); |
268 | | |
269 | | void Curl_ssl_version(char *buffer, size_t size) |
270 | 0 | { |
271 | | #ifdef CURL_WITH_MULTI_SSL |
272 | | (void)multissl_version(buffer, size); |
273 | | #else |
274 | 0 | (void)Curl_ssl->version(buffer, size); |
275 | 0 | #endif |
276 | 0 | } |
277 | | |
278 | | void Curl_ssl_free_certinfo(struct Curl_easy *data) |
279 | 108 | { |
280 | 108 | struct curl_certinfo *ci = &data->info.certs; |
281 | | |
282 | 108 | if(ci->num_of_certs) { |
283 | | /* free all individual lists used */ |
284 | 0 | int i; |
285 | 0 | for(i = 0; i < ci->num_of_certs; i++) { |
286 | 0 | curl_slist_free_all(ci->certinfo[i]); |
287 | 0 | ci->certinfo[i] = NULL; |
288 | 0 | } |
289 | |
|
290 | 0 | curlx_safefree(ci->certinfo); /* free the actual array too */ |
291 | 0 | ci->num_of_certs = 0; |
292 | 0 | } |
293 | 108 | } |
294 | | |
295 | | CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num) |
296 | 0 | { |
297 | 0 | struct curl_certinfo *ci = &data->info.certs; |
298 | 0 | struct curl_slist **table; |
299 | | |
300 | | /* Free any previous certificate information structures */ |
301 | 0 | Curl_ssl_free_certinfo(data); |
302 | | |
303 | | /* Allocate the required certificate information structures */ |
304 | 0 | table = curlx_calloc((size_t)num, sizeof(struct curl_slist *)); |
305 | 0 | if(!table) |
306 | 0 | return CURLE_OUT_OF_MEMORY; |
307 | | |
308 | 0 | ci->num_of_certs = num; |
309 | 0 | ci->certinfo = table; |
310 | |
|
311 | 0 | return CURLE_OK; |
312 | 0 | } |
313 | | |
314 | | /* |
315 | | * 'value' is NOT a null-terminated string |
316 | | */ |
317 | | CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data, |
318 | | int certnum, |
319 | | const char *label, |
320 | | const char *value, |
321 | | size_t valuelen) |
322 | 0 | { |
323 | 0 | struct curl_certinfo *ci = &data->info.certs; |
324 | 0 | struct curl_slist *nl; |
325 | 0 | CURLcode result = CURLE_OK; |
326 | 0 | struct dynbuf build; |
327 | |
|
328 | 0 | DEBUGASSERT(certnum < ci->num_of_certs); |
329 | |
|
330 | 0 | curlx_dyn_init(&build, CURL_X509_STR_MAX); |
331 | |
|
332 | 0 | if(curlx_dyn_add(&build, label) || |
333 | 0 | curlx_dyn_addn(&build, ":", 1) || |
334 | 0 | curlx_dyn_addn(&build, value, valuelen)) |
335 | 0 | return CURLE_OUT_OF_MEMORY; |
336 | | |
337 | 0 | nl = Curl_slist_append_nodup(ci->certinfo[certnum], curlx_dyn_ptr(&build)); |
338 | 0 | if(!nl) { |
339 | 0 | curlx_dyn_free(&build); |
340 | 0 | curl_slist_free_all(ci->certinfo[certnum]); |
341 | 0 | result = CURLE_OUT_OF_MEMORY; |
342 | 0 | } |
343 | |
|
344 | 0 | ci->certinfo[certnum] = nl; |
345 | 0 | return result; |
346 | 0 | } |
347 | | |
348 | | /* get length bytes of randomness */ |
349 | | CURLcode Curl_ssl_random(struct Curl_easy *data, |
350 | | unsigned char *buffer, size_t length) |
351 | 1 | { |
352 | 1 | if(Curl_ssl->random) |
353 | 1 | return Curl_ssl->random(data, buffer, length); |
354 | 0 | else |
355 | 0 | return CURLE_NOT_BUILT_IN; |
356 | 1 | } |
357 | | |
358 | | /* |
359 | | * Public key pem to der conversion |
360 | | */ |
361 | | |
362 | | static CURLcode pubkey_pem_to_der(const char *pem, |
363 | | unsigned char **der, size_t *der_len) |
364 | 0 | { |
365 | 0 | const char *begin_pos, *end_pos; |
366 | 0 | size_t pem_count, pem_len; |
367 | 0 | CURLcode result; |
368 | 0 | struct dynbuf pbuf; |
369 | | |
370 | | /* if no pem, exit. */ |
371 | 0 | if(!pem) |
372 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
373 | | |
374 | 0 | curlx_dyn_init(&pbuf, MAX_PINNED_PUBKEY_SIZE); |
375 | |
|
376 | 0 | begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----"); |
377 | 0 | if(!begin_pos) |
378 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
379 | | |
380 | 0 | pem_count = begin_pos - pem; |
381 | | /* Invalid if not at beginning AND not directly following \n */ |
382 | 0 | if(pem_count && '\n' != pem[pem_count - 1]) |
383 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
384 | | |
385 | | /* 26 is length of "-----BEGIN PUBLIC KEY-----" */ |
386 | 0 | pem_count += 26; |
387 | | |
388 | | /* Invalid if not directly following \n */ |
389 | 0 | end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----"); |
390 | 0 | if(!end_pos) |
391 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
392 | | |
393 | 0 | pem_len = end_pos - pem; |
394 | | |
395 | | /* |
396 | | * Here we loop through the pem array one character at a time between the |
397 | | * correct indices, and place each character that is not '\n' or '\r' |
398 | | * into the stripped_pem array, which should represent the raw base64 string |
399 | | */ |
400 | 0 | while(pem_count < pem_len) { |
401 | 0 | if('\n' != pem[pem_count] && '\r' != pem[pem_count]) { |
402 | 0 | result = curlx_dyn_addn(&pbuf, &pem[pem_count], 1); |
403 | 0 | if(result) |
404 | 0 | return result; |
405 | 0 | } |
406 | 0 | ++pem_count; |
407 | 0 | } |
408 | | |
409 | 0 | if(curlx_dyn_len(&pbuf)) { |
410 | 0 | result = curlx_base64_decode(curlx_dyn_ptr(&pbuf), der, der_len); |
411 | 0 | curlx_dyn_free(&pbuf); |
412 | 0 | } |
413 | 0 | else |
414 | 0 | result = CURLE_BAD_CONTENT_ENCODING; |
415 | |
|
416 | 0 | return result; |
417 | 0 | } |
418 | | |
419 | | /* |
420 | | * Generic pinned public key check. |
421 | | */ |
422 | | |
423 | | CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data, |
424 | | const char *pinnedpubkey, |
425 | | const unsigned char *pubkey, size_t pubkeylen) |
426 | 0 | { |
427 | 0 | CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH; |
428 | | |
429 | | /* if a path was not specified, do not pin */ |
430 | 0 | if(!pinnedpubkey) |
431 | 0 | return CURLE_OK; |
432 | 0 | if(!pubkey || !pubkeylen) |
433 | 0 | return result; |
434 | | |
435 | | /* only do this if pinnedpubkey starts with "sha256//", length 8 */ |
436 | 0 | if(!strncmp(pinnedpubkey, "sha256//", 8)) { |
437 | 0 | CURLcode encode; |
438 | 0 | char *cert_hash = NULL; |
439 | 0 | const char *pinned_hash, *end_pos; |
440 | 0 | size_t cert_hash_len = 0, pinned_hash_len; |
441 | 0 | uint8_t *sha256_digest; |
442 | | |
443 | | /* compute sha256sum of public key */ |
444 | 0 | sha256_digest = curlx_malloc(CURL_SHA256_DIGEST_LEN); |
445 | 0 | if(!sha256_digest) |
446 | 0 | return CURLE_OUT_OF_MEMORY; |
447 | 0 | encode = Curl_sha256it(sha256_digest, pubkey, pubkeylen); |
448 | 0 | if(!encode) |
449 | 0 | encode = curlx_base64_encode(sha256_digest, CURL_SHA256_DIGEST_LEN, |
450 | 0 | &cert_hash, &cert_hash_len); |
451 | 0 | curlx_safefree(sha256_digest); |
452 | |
|
453 | 0 | if(encode) |
454 | 0 | return encode; |
455 | | |
456 | 0 | infof(data, " public key hash: sha256//%s", cert_hash); |
457 | |
|
458 | 0 | pinned_hash = pinnedpubkey; |
459 | 0 | while(pinned_hash && |
460 | 0 | !strncmp(pinned_hash, "sha256//", CURL_CSTRLEN("sha256//"))) { |
461 | 0 | pinned_hash = pinned_hash + CURL_CSTRLEN("sha256//"); |
462 | 0 | end_pos = strchr(pinned_hash, ';'); |
463 | 0 | pinned_hash_len = end_pos ? |
464 | 0 | (size_t)(end_pos - pinned_hash) : strlen(pinned_hash); |
465 | | |
466 | | /* compare base64 sha256 digests */ |
467 | 0 | if(cert_hash_len == pinned_hash_len && |
468 | 0 | !memcmp(cert_hash, pinned_hash, cert_hash_len)) { |
469 | 0 | DEBUGF(infof(data, "public key hash matches pinned value")); |
470 | 0 | result = CURLE_OK; |
471 | 0 | break; |
472 | 0 | } |
473 | | |
474 | 0 | DEBUGF(infof(data, "public key hash does not match 'sha256//%.*s'", |
475 | 0 | (int)pinned_hash_len, pinned_hash)); |
476 | | /* next one or we are at the end */ |
477 | 0 | pinned_hash = end_pos ? (end_pos + 1) : NULL; |
478 | 0 | } |
479 | 0 | curlx_safefree(cert_hash); |
480 | 0 | } |
481 | 0 | else { |
482 | 0 | long filesize; |
483 | 0 | size_t size, pem_len; |
484 | 0 | CURLcode pem_read; |
485 | 0 | struct dynbuf buf; |
486 | 0 | unsigned char *pem_ptr = NULL; |
487 | 0 | size_t left; |
488 | 0 | FILE *fp = curlx_fopen(pinnedpubkey, "rb"); |
489 | 0 | if(!fp) |
490 | 0 | return result; |
491 | | |
492 | 0 | curlx_dyn_init(&buf, MAX_PINNED_PUBKEY_SIZE); |
493 | | |
494 | | /* Determine the file's size */ |
495 | 0 | if(fseek(fp, 0, SEEK_END)) |
496 | 0 | goto end; |
497 | 0 | filesize = ftell(fp); |
498 | 0 | if(fseek(fp, 0, SEEK_SET)) |
499 | 0 | goto end; |
500 | 0 | if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE) |
501 | 0 | goto end; |
502 | | |
503 | | /* |
504 | | * if the size of our certificate is bigger than the file |
505 | | * size then it cannot match |
506 | | */ |
507 | 0 | size = curlx_sotouz((curl_off_t)filesize); |
508 | 0 | if(pubkeylen > size) |
509 | 0 | goto end; |
510 | | |
511 | | /* |
512 | | * Read the file into the dynbuf |
513 | | */ |
514 | 0 | left = size; |
515 | 0 | do { |
516 | 0 | char buffer[1024]; |
517 | 0 | size_t want = left > sizeof(buffer) ? sizeof(buffer) : left; |
518 | 0 | if(fread(buffer, 1, want, fp) != want) |
519 | 0 | goto end; |
520 | 0 | if(curlx_dyn_addn(&buf, buffer, want)) |
521 | 0 | goto end; |
522 | 0 | left -= want; |
523 | 0 | } while(left); |
524 | | |
525 | | /* If the sizes are the same, it cannot be base64 encoded, must be der */ |
526 | 0 | if(pubkeylen == size) { |
527 | 0 | if(!memcmp(pubkey, curlx_dyn_ptr(&buf), pubkeylen)) |
528 | 0 | result = CURLE_OK; |
529 | 0 | goto end; |
530 | 0 | } |
531 | | |
532 | | /* |
533 | | * Otherwise we assume it is PEM and try to decode it after placing |
534 | | * null-terminator |
535 | | */ |
536 | 0 | pem_read = pubkey_pem_to_der(curlx_dyn_ptr(&buf), &pem_ptr, &pem_len); |
537 | | /* if it was not read successfully, exit */ |
538 | 0 | if(pem_read) |
539 | 0 | goto end; |
540 | | |
541 | | /* |
542 | | * if the size of our certificate does not match the size of |
543 | | * the decoded file, they cannot be the same, otherwise compare |
544 | | */ |
545 | 0 | if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen)) |
546 | 0 | result = CURLE_OK; |
547 | 0 | end: |
548 | 0 | curlx_dyn_free(&buf); |
549 | 0 | curlx_safefree(pem_ptr); |
550 | 0 | curlx_fclose(fp); |
551 | 0 | } |
552 | | |
553 | 0 | return result; |
554 | 0 | } |
555 | | |
556 | | /* |
557 | | * Check whether the SSL backend supports the status_request extension. |
558 | | */ |
559 | | bool Curl_ssl_cert_status_request(void) |
560 | 0 | { |
561 | 0 | if(Curl_ssl->cert_status_request) |
562 | 0 | return Curl_ssl->cert_status_request(); |
563 | 0 | return FALSE; |
564 | 0 | } |
565 | | |
566 | | static int multissl_init(void) |
567 | 0 | { |
568 | 0 | if(multissl_setup(NULL)) |
569 | 0 | return 1; |
570 | 0 | if(Curl_ssl->init) |
571 | 0 | return Curl_ssl->init(); |
572 | 0 | return 1; |
573 | 0 | } |
574 | | |
575 | | static CURLcode multissl_random(struct Curl_easy *data, |
576 | | unsigned char *entropy, size_t length) |
577 | 0 | { |
578 | 0 | if(multissl_setup(NULL)) |
579 | 0 | return CURLE_FAILED_INIT; |
580 | 0 | return Curl_ssl->random(data, entropy, length); |
581 | 0 | } |
582 | | |
583 | | static CURLcode multissl_connect(struct Curl_cfilter *cf, |
584 | | struct Curl_easy *data, bool *done) |
585 | 0 | { |
586 | 0 | if(multissl_setup(NULL)) |
587 | 0 | return CURLE_FAILED_INIT; |
588 | 0 | return Curl_ssl->do_connect(cf, data, done); |
589 | 0 | } |
590 | | |
591 | | static CURLcode multissl_adjust_pollset(struct Curl_cfilter *cf, |
592 | | struct Curl_easy *data, |
593 | | struct easy_pollset *ps) |
594 | 0 | { |
595 | 0 | if(multissl_setup(NULL)) |
596 | 0 | return CURLE_OK; |
597 | 0 | return Curl_ssl->adjust_pollset(cf, data, ps); |
598 | 0 | } |
599 | | |
600 | | static void *multissl_get_internals(struct ssl_connect_data *connssl, |
601 | | CURLINFO info) |
602 | 0 | { |
603 | 0 | if(multissl_setup(NULL)) |
604 | 0 | return NULL; |
605 | 0 | return Curl_ssl->get_internals(connssl, info); |
606 | 0 | } |
607 | | |
608 | | static void multissl_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
609 | 0 | { |
610 | 0 | if(multissl_setup(NULL)) |
611 | 0 | return; |
612 | 0 | Curl_ssl->close(cf, data); |
613 | 0 | } |
614 | | |
615 | | static CURLcode multissl_recv_plain(struct Curl_cfilter *cf, |
616 | | struct Curl_easy *data, |
617 | | char *buf, size_t len, size_t *pnread) |
618 | 0 | { |
619 | 0 | if(multissl_setup(NULL)) |
620 | 0 | return CURLE_FAILED_INIT; |
621 | 0 | return Curl_ssl->recv_plain(cf, data, buf, len, pnread); |
622 | 0 | } |
623 | | |
624 | | static CURLcode multissl_send_plain(struct Curl_cfilter *cf, |
625 | | struct Curl_easy *data, |
626 | | const void *mem, size_t len, |
627 | | size_t *pnwritten) |
628 | 0 | { |
629 | 0 | if(multissl_setup(NULL)) |
630 | 0 | return CURLE_FAILED_INIT; |
631 | 0 | return Curl_ssl->send_plain(cf, data, mem, len, pnwritten); |
632 | 0 | } |
633 | | |
634 | | static const struct Curl_ssl Curl_ssl_multi = { |
635 | | { CURLSSLBACKEND_NONE, "multi" }, /* info */ |
636 | | 0, /* supports nothing */ |
637 | | (size_t)-1, /* something insanely large to be on the safe side */ |
638 | | |
639 | | multissl_init, /* init */ |
640 | | NULL, /* cleanup */ |
641 | | multissl_version, /* version */ |
642 | | NULL, /* shutdown */ |
643 | | NULL, /* data_pending */ |
644 | | multissl_random, /* random */ |
645 | | NULL, /* cert_status_request */ |
646 | | multissl_connect, /* connect */ |
647 | | multissl_adjust_pollset, /* adjust_pollset */ |
648 | | multissl_get_internals, /* get_internals */ |
649 | | multissl_close, /* close_one */ |
650 | | NULL, /* close_all */ |
651 | | NULL, /* engines_list */ |
652 | | multissl_recv_plain, /* recv decrypted data */ |
653 | | multissl_send_plain, /* send data to encrypt */ |
654 | | NULL, /* get_channel_binding */ |
655 | | }; |
656 | | |
657 | | const struct Curl_ssl *Curl_ssl = |
658 | | #ifdef CURL_WITH_MULTI_SSL |
659 | | &Curl_ssl_multi; |
660 | | #elif defined(USE_WOLFSSL) |
661 | | &Curl_ssl_wolfssl; |
662 | | #elif defined(USE_GNUTLS) |
663 | | &Curl_ssl_gnutls; |
664 | | #elif defined(USE_MBEDTLS) |
665 | | &Curl_ssl_mbedtls; |
666 | | #elif defined(USE_RUSTLS) |
667 | | &Curl_ssl_rustls; |
668 | | #elif defined(USE_OPENSSL) |
669 | | &Curl_ssl_openssl; |
670 | | #elif defined(USE_SCHANNEL) |
671 | | &Curl_ssl_schannel; |
672 | | #else |
673 | | #error "Missing struct Curl_ssl for selected SSL backend" |
674 | | #endif |
675 | | |
676 | | static const struct Curl_ssl *available_backends[] = { |
677 | | #ifdef USE_WOLFSSL |
678 | | &Curl_ssl_wolfssl, |
679 | | #endif |
680 | | #ifdef USE_GNUTLS |
681 | | &Curl_ssl_gnutls, |
682 | | #endif |
683 | | #ifdef USE_MBEDTLS |
684 | | &Curl_ssl_mbedtls, |
685 | | #endif |
686 | | #ifdef USE_OPENSSL |
687 | | &Curl_ssl_openssl, |
688 | | #endif |
689 | | #ifdef USE_SCHANNEL |
690 | | &Curl_ssl_schannel, |
691 | | #endif |
692 | | #ifdef USE_RUSTLS |
693 | | &Curl_ssl_rustls, |
694 | | #endif |
695 | | NULL |
696 | | }; |
697 | | |
698 | | /* Global cleanup */ |
699 | | void Curl_ssl_cleanup(void) |
700 | 1 | { |
701 | 1 | if(init_ssl) { |
702 | | /* only cleanup if we did a previous init */ |
703 | 1 | if(Curl_ssl->cleanup) |
704 | 1 | Curl_ssl->cleanup(); |
705 | | #ifdef CURL_WITH_MULTI_SSL |
706 | | Curl_ssl = &Curl_ssl_multi; |
707 | | #endif |
708 | 1 | init_ssl = FALSE; |
709 | 1 | } |
710 | 1 | } |
711 | | |
712 | | static size_t multissl_version(char *buffer, size_t size) |
713 | 0 | { |
714 | 0 | static const struct Curl_ssl *selected; |
715 | 0 | static char backends[200]; |
716 | 0 | static size_t backends_len; |
717 | 0 | const struct Curl_ssl *current; |
718 | |
|
719 | 0 | current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl; |
720 | |
|
721 | 0 | if(current != selected) { |
722 | 0 | char *p = backends; |
723 | 0 | const char *end = backends + sizeof(backends); |
724 | 0 | int i; |
725 | |
|
726 | 0 | selected = current; |
727 | |
|
728 | 0 | backends[0] = '\0'; |
729 | |
|
730 | 0 | for(i = 0; available_backends[i]; ++i) { |
731 | 0 | char vb[200]; |
732 | 0 | bool paren = (selected != available_backends[i]); |
733 | |
|
734 | 0 | if(available_backends[i]->version(vb, sizeof(vb))) { |
735 | 0 | p += curl_msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""), |
736 | 0 | (paren ? "(" : ""), vb, (paren ? ")" : "")); |
737 | 0 | } |
738 | 0 | } |
739 | |
|
740 | 0 | backends_len = p - backends; |
741 | 0 | } |
742 | |
|
743 | 0 | if(size) { |
744 | 0 | curlx_strcopy(buffer, size, backends, backends_len); |
745 | 0 | } |
746 | 0 | return 0; |
747 | 0 | } |
748 | | |
749 | | static int multissl_setup(const struct Curl_ssl *backend) |
750 | 0 | { |
751 | 0 | int i; |
752 | 0 | char *env; |
753 | |
|
754 | 0 | if(Curl_ssl != &Curl_ssl_multi) |
755 | 0 | return 1; |
756 | | |
757 | 0 | if(backend) { |
758 | 0 | Curl_ssl = backend; |
759 | 0 | return 0; |
760 | 0 | } |
761 | | |
762 | 0 | if(!available_backends[0]) |
763 | 0 | return 1; |
764 | | |
765 | 0 | env = curl_getenv("CURL_SSL_BACKEND"); |
766 | 0 | if(env) { |
767 | 0 | for(i = 0; available_backends[i]; i++) { |
768 | 0 | if(curl_strequal(env, available_backends[i]->info.name)) { |
769 | 0 | Curl_ssl = available_backends[i]; |
770 | 0 | curlx_free(env); |
771 | 0 | return 0; |
772 | 0 | } |
773 | 0 | } |
774 | 0 | } |
775 | | |
776 | | #ifdef CURL_DEFAULT_SSL_BACKEND |
777 | | for(i = 0; available_backends[i]; i++) { |
778 | | if(curl_strequal(CURL_DEFAULT_SSL_BACKEND, |
779 | | available_backends[i]->info.name)) { |
780 | | Curl_ssl = available_backends[i]; |
781 | | curlx_free(env); |
782 | | return 0; |
783 | | } |
784 | | } |
785 | | #endif |
786 | | |
787 | | /* Fall back to first available backend */ |
788 | 0 | Curl_ssl = available_backends[0]; |
789 | 0 | curlx_free(env); |
790 | 0 | return 0; |
791 | 0 | } |
792 | | |
793 | | /* This function is used to select the SSL backend to use. It is called by |
794 | | curl_global_sslset (easy.c) which uses the global init lock. */ |
795 | | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
796 | | const curl_ssl_backend ***avail) |
797 | 0 | { |
798 | 0 | int i; |
799 | |
|
800 | 0 | if(avail) |
801 | 0 | *avail = (const curl_ssl_backend **)&available_backends; |
802 | |
|
803 | 0 | if(Curl_ssl != &Curl_ssl_multi) |
804 | 0 | return id == Curl_ssl->info.id || |
805 | 0 | (name && curl_strequal(name, Curl_ssl->info.name)) ? |
806 | 0 | CURLSSLSET_OK : |
807 | | #ifdef CURL_WITH_MULTI_SSL |
808 | | CURLSSLSET_TOO_LATE; |
809 | | #else |
810 | 0 | CURLSSLSET_UNKNOWN_BACKEND; |
811 | 0 | #endif |
812 | | |
813 | 0 | for(i = 0; available_backends[i]; i++) { |
814 | 0 | if(available_backends[i]->info.id == id || |
815 | 0 | (name && curl_strequal(available_backends[i]->info.name, name))) { |
816 | 0 | multissl_setup(available_backends[i]); |
817 | 0 | return CURLSSLSET_OK; |
818 | 0 | } |
819 | 0 | } |
820 | | |
821 | 0 | return CURLSSLSET_UNKNOWN_BACKEND; |
822 | 0 | } |
823 | | |
824 | | #else /* USE_SSL */ |
825 | | CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name, |
826 | | const curl_ssl_backend ***avail) |
827 | | { |
828 | | (void)id; |
829 | | (void)name; |
830 | | (void)avail; |
831 | | return CURLSSLSET_NO_BACKENDS; |
832 | | } |
833 | | |
834 | | #endif /* !USE_SSL */ |
835 | | |
836 | | #ifdef USE_SSL |
837 | | |
838 | | void Curl_ssl_peer_cleanup(struct ssl_peer *peer) |
839 | 0 | { |
840 | 0 | Curl_peer_unlink(&peer->origin); |
841 | 0 | Curl_peer_unlink(&peer->peer); |
842 | 0 | curlx_safefree(peer->sni); |
843 | 0 | curlx_safefree(peer->scache_key); |
844 | 0 | peer->transport = TRNSPRT_NONE; |
845 | 0 | peer->type = CURL_SSL_PEER_DNS; |
846 | 0 | } |
847 | | |
848 | | static ssl_peer_type get_peer_type(const char *hostname) |
849 | 0 | { |
850 | 0 | if(hostname && hostname[0]) { |
851 | 0 | #ifdef USE_IPV6 |
852 | 0 | struct in6_addr addr; |
853 | | #else |
854 | | struct in_addr addr; |
855 | | #endif |
856 | 0 | if(curlx_inet_pton(AF_INET, hostname, &addr)) |
857 | 0 | return CURL_SSL_PEER_IPV4; |
858 | 0 | #ifdef USE_IPV6 |
859 | 0 | else if(curlx_inet_pton(AF_INET6, hostname, &addr)) { |
860 | 0 | return CURL_SSL_PEER_IPV6; |
861 | 0 | } |
862 | 0 | #endif |
863 | 0 | } |
864 | 0 | return CURL_SSL_PEER_DNS; |
865 | 0 | } |
866 | | |
867 | | CURLcode Curl_ssl_peer_init(struct ssl_peer *ssl_peer, |
868 | | struct Curl_peer *origin, |
869 | | struct Curl_peer *peer, |
870 | | struct ssl_filter_config *sslc, |
871 | | const char *tls_id, |
872 | | uint8_t transport) |
873 | 0 | { |
874 | 0 | CURLcode result = CURLE_OUT_OF_MEMORY; |
875 | | |
876 | | /* We expect a clean struct, e.g. called only ONCE */ |
877 | 0 | if(!ssl_peer || !origin) { |
878 | 0 | DEBUGASSERT(0); |
879 | 0 | return CURLE_FAILED_INIT; |
880 | 0 | } |
881 | 0 | DEBUGASSERT(!ssl_peer->origin); |
882 | 0 | DEBUGASSERT(!ssl_peer->peer); |
883 | 0 | DEBUGASSERT(!ssl_peer->sni); |
884 | 0 | ssl_peer->transport = transport; |
885 | |
|
886 | 0 | Curl_peer_link(&ssl_peer->origin, origin); |
887 | 0 | Curl_peer_link(&ssl_peer->peer, peer); |
888 | 0 | ssl_peer->type = get_peer_type(origin->hostname); |
889 | 0 | if(ssl_peer->type == CURL_SSL_PEER_DNS) { |
890 | | /* not an IP address, normalize according to RCC 6066 ch. 3, |
891 | | * max len of SNI is 2^16-1, no trailing dot */ |
892 | 0 | size_t len = strlen(origin->hostname); |
893 | 0 | if(len && (origin->hostname[len - 1] == '.')) |
894 | 0 | len--; |
895 | 0 | if(len < USHRT_MAX) { |
896 | 0 | ssl_peer->sni = curlx_calloc(1, len + 1); |
897 | 0 | if(!ssl_peer->sni) |
898 | 0 | goto out; |
899 | 0 | Curl_strntolower(ssl_peer->sni, origin->hostname, len); |
900 | 0 | ssl_peer->sni[len] = 0; |
901 | 0 | } |
902 | 0 | } |
903 | | |
904 | 0 | result = Curl_ssl_peer_key_make(ssl_peer, sslc, tls_id, |
905 | 0 | &ssl_peer->scache_key); |
906 | |
|
907 | 0 | out: |
908 | 0 | if(result) |
909 | 0 | Curl_ssl_peer_cleanup(ssl_peer); |
910 | 0 | return result; |
911 | 0 | } |
912 | | |
913 | | static void ssl_cf_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
914 | 0 | { |
915 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
916 | 0 | if(connssl) { |
917 | 0 | connssl->ssl_impl->close(cf, data); |
918 | 0 | connssl->state = ssl_connection_none; |
919 | 0 | connssl->connecting_state = ssl_connect_1; |
920 | 0 | connssl->prefs_checked = FALSE; |
921 | 0 | Curl_ssl_peer_cleanup(&connssl->peer); |
922 | 0 | Curl_ssl_session_destroy(connssl->session); |
923 | 0 | cf_ctx_free(connssl); |
924 | 0 | cf->ctx = NULL; |
925 | 0 | } |
926 | 0 | cf->connected = FALSE; |
927 | 0 | } |
928 | | |
929 | | static CURLcode ssl_cf_connect(struct Curl_cfilter *cf, |
930 | | struct Curl_easy *data, |
931 | | bool *done) |
932 | 0 | { |
933 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
934 | 0 | struct cf_call_data save; |
935 | 0 | CURLcode result; |
936 | |
|
937 | 0 | if(cf->connected && (connssl->state != ssl_connection_deferred)) { |
938 | 0 | *done = TRUE; |
939 | 0 | return CURLE_OK; |
940 | 0 | } |
941 | | |
942 | 0 | if(!cf->next || !connssl->peer.origin) { |
943 | 0 | *done = FALSE; |
944 | 0 | return CURLE_FAILED_INIT; |
945 | 0 | } |
946 | | |
947 | 0 | if(!cf->next->connected) { |
948 | 0 | result = cf->next->cft->do_connect(cf->next, data, done); |
949 | 0 | if(result || !*done) |
950 | 0 | return result; |
951 | 0 | } |
952 | | |
953 | 0 | CF_DATA_SAVE(save, cf, data); |
954 | 0 | CURL_TRC_CF(data, cf, "cf_connect()"); |
955 | 0 | DEBUGASSERT(connssl); |
956 | |
|
957 | 0 | *done = FALSE; |
958 | |
|
959 | 0 | if(!connssl->prefs_checked) { |
960 | 0 | if(!ssl_prefs_check(cf, data)) { |
961 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
962 | 0 | goto out; |
963 | 0 | } |
964 | 0 | connssl->prefs_checked = TRUE; |
965 | 0 | } |
966 | | |
967 | 0 | result = connssl->ssl_impl->do_connect(cf, data, done); |
968 | |
|
969 | 0 | if(!result && *done) { |
970 | 0 | cf->connected = TRUE; |
971 | 0 | if(connssl->state == ssl_connection_complete) { |
972 | 0 | connssl->handshake_done = *Curl_pgrs_now(data); |
973 | 0 | } |
974 | 0 | if(Curl_tls_keylog_enabled()) { |
975 | 0 | infof(data, "SSLKEYLOGFILE set, all TLS secrets are logged to '%s'", |
976 | 0 | Curl_tls_keylog_file_name()); |
977 | | #ifdef LIBRESSL_VERSION_NUMBER |
978 | | infof(data, "Note LibreSSL only supports SSLKEYLOGFILE for TLS <= 1.2"); |
979 | | #endif |
980 | 0 | } |
981 | | /* Connection can be deferred when sending early data */ |
982 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete || |
983 | 0 | connssl->state == ssl_connection_deferred); |
984 | 0 | DEBUGASSERT(connssl->state != ssl_connection_deferred || |
985 | 0 | connssl->earlydata_state > ssl_earlydata_none); |
986 | 0 | } |
987 | 0 | out: |
988 | 0 | CURL_TRC_CF(data, cf, "cf_connect() -> %d, done=%d", (int)result, *done); |
989 | 0 | CF_DATA_RESTORE(cf, save); |
990 | 0 | return result; |
991 | 0 | } |
992 | | |
993 | | static CURLcode ssl_cf_set_earlydata(struct Curl_cfilter *cf, |
994 | | struct Curl_easy *data, |
995 | | const void *buf, size_t blen) |
996 | 0 | { |
997 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
998 | 0 | size_t nwritten = 0; |
999 | 0 | CURLcode result = CURLE_OK; |
1000 | |
|
1001 | 0 | DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_await); |
1002 | 0 | DEBUGASSERT(Curl_bufq_is_empty(&connssl->earlydata)); |
1003 | 0 | if(blen) { |
1004 | 0 | if(blen > connssl->earlydata_max) |
1005 | 0 | blen = connssl->earlydata_max; |
1006 | 0 | result = Curl_bufq_write(&connssl->earlydata, buf, blen, &nwritten); |
1007 | 0 | CURL_TRC_CF(data, cf, "ssl_cf_set_earlydata(len=%zu) -> %zu", |
1008 | 0 | blen, nwritten); |
1009 | 0 | if(result) |
1010 | 0 | return result; |
1011 | 0 | } |
1012 | 0 | return CURLE_OK; |
1013 | 0 | } |
1014 | | |
1015 | | static CURLcode ssl_cf_connect_deferred(struct Curl_cfilter *cf, |
1016 | | struct Curl_easy *data, |
1017 | | const void *buf, size_t blen, |
1018 | | bool *done) |
1019 | 0 | { |
1020 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1021 | 0 | CURLcode result = CURLE_OK; |
1022 | |
|
1023 | 0 | DEBUGASSERT(connssl->state == ssl_connection_deferred); |
1024 | 0 | *done = FALSE; |
1025 | 0 | if(connssl->earlydata_state == ssl_earlydata_await) { |
1026 | 0 | result = ssl_cf_set_earlydata(cf, data, buf, blen); |
1027 | 0 | if(result) |
1028 | 0 | return result; |
1029 | | /* we buffered any early data we would like to send. Actually |
1030 | | * do the connect now which sends it and performs the handshake. */ |
1031 | 0 | connssl->earlydata_state = ssl_earlydata_sending; |
1032 | 0 | connssl->earlydata_skip = Curl_bufq_len(&connssl->earlydata); |
1033 | 0 | } |
1034 | | |
1035 | 0 | result = ssl_cf_connect(cf, data, done); |
1036 | |
|
1037 | 0 | if(result) { |
1038 | | /* prevent any further connection use after handshake failed */ |
1039 | 0 | connclose(CF_CONN(cf)); |
1040 | 0 | } |
1041 | 0 | else if(*done) { |
1042 | 0 | if(!connssl->stats_reported && (cf->cft == &Curl_cft_ssl)) { |
1043 | 0 | Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done); |
1044 | 0 | connssl->stats_reported = TRUE; |
1045 | 0 | } |
1046 | 0 | switch(connssl->earlydata_state) { |
1047 | 0 | case ssl_earlydata_none: |
1048 | 0 | break; |
1049 | 0 | case ssl_earlydata_accepted: |
1050 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) |
1051 | 0 | Curl_pgrsEarlyData(data, (curl_off_t)connssl->earlydata_skip); |
1052 | 0 | infof(data, "Server accepted %zu bytes of TLS early data.", |
1053 | 0 | connssl->earlydata_skip); |
1054 | 0 | break; |
1055 | 0 | case ssl_earlydata_rejected: |
1056 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) |
1057 | 0 | Curl_pgrsEarlyData(data, -(curl_off_t)connssl->earlydata_skip); |
1058 | 0 | infof(data, "Server rejected TLS early data."); |
1059 | 0 | connssl->earlydata_skip = 0; |
1060 | 0 | break; |
1061 | 0 | default: |
1062 | | /* This should not happen. Either we do not use early data or we |
1063 | | * should know if it was accepted or not. */ |
1064 | 0 | DEBUGASSERT(NULL); |
1065 | 0 | break; |
1066 | 0 | } |
1067 | 0 | } |
1068 | 0 | return result; |
1069 | 0 | } |
1070 | | |
1071 | | static bool ssl_cf_data_pending(struct Curl_cfilter *cf, |
1072 | | const struct Curl_easy *data) |
1073 | 0 | { |
1074 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1075 | 0 | struct cf_call_data save; |
1076 | 0 | bool pending; |
1077 | |
|
1078 | 0 | CF_DATA_SAVE(save, cf, data); |
1079 | 0 | if(connssl->ssl_impl->data_pending && |
1080 | 0 | connssl->ssl_impl->data_pending(cf, data)) |
1081 | 0 | pending = TRUE; |
1082 | 0 | else |
1083 | 0 | pending = cf->next->cft->has_data_pending(cf->next, data); |
1084 | 0 | CF_DATA_RESTORE(cf, save); |
1085 | 0 | return pending; |
1086 | 0 | } |
1087 | | |
1088 | | static CURLcode ssl_cf_send(struct Curl_cfilter *cf, |
1089 | | struct Curl_easy *data, |
1090 | | const uint8_t *buf, size_t blen, |
1091 | | bool eos, size_t *pnwritten) |
1092 | 0 | { |
1093 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1094 | 0 | struct cf_call_data save; |
1095 | 0 | CURLcode result = CURLE_OK; |
1096 | |
|
1097 | 0 | (void)eos; |
1098 | 0 | *pnwritten = 0; |
1099 | 0 | CF_DATA_SAVE(save, cf, data); |
1100 | |
|
1101 | 0 | if(connssl->state == ssl_connection_deferred) { |
1102 | 0 | bool done = FALSE; |
1103 | 0 | result = ssl_cf_connect_deferred(cf, data, buf, blen, &done); |
1104 | 0 | if(result) |
1105 | 0 | goto out; |
1106 | 0 | else if(!done) { |
1107 | 0 | result = CURLE_AGAIN; |
1108 | 0 | goto out; |
1109 | 0 | } |
1110 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete); |
1111 | 0 | } |
1112 | | |
1113 | 0 | if(connssl->earlydata_skip) { |
1114 | | /* earlydata_skip bytes were sent as early data, so report only them, so |
1115 | | * that a blocked send of the rest does not make the caller send the early |
1116 | | * data bytes again. */ |
1117 | 0 | *pnwritten = CURLMIN(connssl->earlydata_skip, blen); |
1118 | 0 | connssl->earlydata_skip -= *pnwritten; |
1119 | 0 | result = CURLE_OK; |
1120 | 0 | goto out; |
1121 | 0 | } |
1122 | | |
1123 | | /* OpenSSL and maybe other TLS libs do not like 0-length writes. Skip. */ |
1124 | 0 | if(blen > 0) { |
1125 | 0 | size_t nwritten; |
1126 | 0 | result = connssl->ssl_impl->send_plain(cf, data, buf, blen, &nwritten); |
1127 | 0 | if(!result) |
1128 | 0 | *pnwritten += nwritten; |
1129 | 0 | } |
1130 | |
|
1131 | 0 | out: |
1132 | 0 | CF_DATA_RESTORE(cf, save); |
1133 | 0 | return result; |
1134 | 0 | } |
1135 | | |
1136 | | static CURLcode ssl_cf_recv(struct Curl_cfilter *cf, |
1137 | | struct Curl_easy *data, char *buf, size_t len, |
1138 | | size_t *pnread) |
1139 | 0 | { |
1140 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1141 | 0 | struct cf_call_data save; |
1142 | 0 | CURLcode result = CURLE_OK; |
1143 | |
|
1144 | 0 | CF_DATA_SAVE(save, cf, data); |
1145 | 0 | *pnread = 0; |
1146 | 0 | if(connssl->state == ssl_connection_deferred) { |
1147 | 0 | bool done = FALSE; |
1148 | 0 | result = ssl_cf_connect_deferred(cf, data, NULL, 0, &done); |
1149 | 0 | if(result) |
1150 | 0 | goto out; |
1151 | 0 | else if(!done) { |
1152 | 0 | result = CURLE_AGAIN; |
1153 | 0 | goto out; |
1154 | 0 | } |
1155 | 0 | DEBUGASSERT(connssl->state == ssl_connection_complete); |
1156 | 0 | } |
1157 | | |
1158 | 0 | result = connssl->ssl_impl->recv_plain(cf, data, buf, len, pnread); |
1159 | |
|
1160 | 0 | out: |
1161 | 0 | CF_DATA_RESTORE(cf, save); |
1162 | 0 | return result; |
1163 | 0 | } |
1164 | | |
1165 | | static CURLcode ssl_cf_shutdown(struct Curl_cfilter *cf, |
1166 | | struct Curl_easy *data, |
1167 | | bool *done) |
1168 | 0 | { |
1169 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1170 | 0 | CURLcode result = CURLE_OK; |
1171 | |
|
1172 | 0 | *done = TRUE; |
1173 | | /* If we have done the SSL handshake, shut down the connection cleanly */ |
1174 | 0 | if(cf->connected && (connssl->state == ssl_connection_complete) && |
1175 | 0 | !cf->shutdown && Curl_ssl->shut_down) { |
1176 | 0 | struct cf_call_data save; |
1177 | |
|
1178 | 0 | CF_DATA_SAVE(save, cf, data); |
1179 | 0 | result = connssl->ssl_impl->shut_down(cf, data, TRUE, done); |
1180 | 0 | CURL_TRC_CF(data, cf, "cf_shutdown -> %d, done=%d", (int)result, *done); |
1181 | 0 | CF_DATA_RESTORE(cf, save); |
1182 | 0 | cf->shutdown = (result || *done); |
1183 | 0 | } |
1184 | 0 | return result; |
1185 | 0 | } |
1186 | | |
1187 | | static CURLcode ssl_cf_adjust_pollset(struct Curl_cfilter *cf, |
1188 | | struct Curl_easy *data, |
1189 | | struct easy_pollset *ps) |
1190 | 0 | { |
1191 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1192 | 0 | struct cf_call_data save; |
1193 | 0 | CURLcode result; |
1194 | |
|
1195 | 0 | CF_DATA_SAVE(save, cf, data); |
1196 | 0 | result = connssl->ssl_impl->adjust_pollset(cf, data, ps); |
1197 | 0 | CF_DATA_RESTORE(cf, save); |
1198 | 0 | return result; |
1199 | 0 | } |
1200 | | |
1201 | | static CURLcode ssl_cf_query(struct Curl_cfilter *cf, |
1202 | | struct Curl_easy *data, |
1203 | | int query, int *pres1, void *pres2) |
1204 | 0 | { |
1205 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1206 | |
|
1207 | 0 | switch(query) { |
1208 | 0 | case CF_QUERY_SSL_INFO: |
1209 | 0 | case CF_QUERY_SSL_CTX_INFO: |
1210 | 0 | if(!Curl_ssl_cf_is_proxy(cf)) { |
1211 | 0 | struct curl_tlssessioninfo *info = pres2; |
1212 | 0 | struct cf_call_data save; |
1213 | 0 | CF_DATA_SAVE(save, cf, data); |
1214 | 0 | info->backend = Curl_ssl_backend(); |
1215 | 0 | info->internals = connssl->ssl_impl->get_internals( |
1216 | 0 | cf->ctx, (query == CF_QUERY_SSL_INFO) ? |
1217 | 0 | CURLINFO_TLS_SSL_PTR : CURLINFO_TLS_SESSION); |
1218 | 0 | CF_DATA_RESTORE(cf, save); |
1219 | 0 | return CURLE_OK; |
1220 | 0 | } |
1221 | 0 | break; |
1222 | 0 | case CF_QUERY_ALPN_NEGOTIATED: { |
1223 | 0 | const char **palpn = pres2; |
1224 | 0 | DEBUGASSERT(palpn); |
1225 | 0 | *palpn = connssl->negotiated.alpn; |
1226 | 0 | CURL_TRC_CF(data, cf, "query ALPN: returning '%s'", *palpn); |
1227 | 0 | return CURLE_OK; |
1228 | 0 | } |
1229 | 0 | default: |
1230 | 0 | break; |
1231 | 0 | } |
1232 | 0 | return cf->next ? |
1233 | 0 | cf->next->cft->query(cf->next, data, query, pres1, pres2) : |
1234 | 0 | CURLE_UNKNOWN_OPTION; |
1235 | 0 | } |
1236 | | |
1237 | | static CURLcode ssl_cf_cntrl(struct Curl_cfilter *cf, |
1238 | | struct Curl_easy *data, |
1239 | | int event, int arg1, void *arg2) |
1240 | 0 | { |
1241 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1242 | |
|
1243 | 0 | (void)arg1; |
1244 | 0 | (void)arg2; |
1245 | 0 | (void)data; |
1246 | 0 | switch(event) { |
1247 | 0 | case CF_CTRL_CONN_INFO_UPDATE: |
1248 | 0 | if(connssl->negotiated.alpn && CF_IS_PRIMARY(cf)) { |
1249 | 0 | if(!strcmp("http/1.1", connssl->negotiated.alpn)) |
1250 | 0 | CF_CONN(cf)->httpversion_seen = 11; |
1251 | 0 | else if(!strcmp("h2", connssl->negotiated.alpn)) |
1252 | 0 | CF_CONN(cf)->httpversion_seen = 20; |
1253 | 0 | else if(!strcmp("h3", connssl->negotiated.alpn)) |
1254 | 0 | CF_CONN(cf)->httpversion_seen = 30; |
1255 | 0 | } |
1256 | 0 | break; |
1257 | 0 | case CF_CTRL_REPORT_STATS: |
1258 | 0 | if(cf->connected && !connssl->stats_reported && |
1259 | 0 | (cf->cft == &Curl_cft_ssl) && |
1260 | 0 | (connssl->handshake_done.tv_sec || connssl->handshake_done.tv_usec)) { |
1261 | 0 | Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done); |
1262 | 0 | connssl->stats_reported = TRUE; |
1263 | 0 | } |
1264 | 0 | break; |
1265 | 0 | } |
1266 | 0 | return CURLE_OK; |
1267 | 0 | } |
1268 | | |
1269 | | struct Curl_cftype Curl_cft_ssl = { |
1270 | | "SSL", |
1271 | | CF_TYPE_SSL, |
1272 | | CURL_LOG_LVL_NONE, |
1273 | | ssl_cf_destroy, |
1274 | | ssl_cf_connect, |
1275 | | ssl_cf_shutdown, |
1276 | | ssl_cf_adjust_pollset, |
1277 | | ssl_cf_data_pending, |
1278 | | ssl_cf_send, |
1279 | | ssl_cf_recv, |
1280 | | ssl_cf_cntrl, |
1281 | | Curl_cf_def_conn_is_alive, |
1282 | | Curl_cf_def_conn_keep_alive, |
1283 | | ssl_cf_query, |
1284 | | }; |
1285 | | |
1286 | | #ifndef CURL_DISABLE_PROXY |
1287 | | |
1288 | | struct Curl_cftype Curl_cft_ssl_proxy = { |
1289 | | "SSL-PROXY", |
1290 | | CF_TYPE_SSL | CF_TYPE_PROXY, |
1291 | | CURL_LOG_LVL_NONE, |
1292 | | ssl_cf_destroy, |
1293 | | ssl_cf_connect, |
1294 | | ssl_cf_shutdown, |
1295 | | ssl_cf_adjust_pollset, |
1296 | | ssl_cf_data_pending, |
1297 | | ssl_cf_send, |
1298 | | ssl_cf_recv, |
1299 | | Curl_cf_def_cntrl, |
1300 | | Curl_cf_def_conn_is_alive, |
1301 | | Curl_cf_def_conn_keep_alive, |
1302 | | ssl_cf_query, |
1303 | | }; |
1304 | | |
1305 | | #endif /* !CURL_DISABLE_PROXY */ |
1306 | | |
1307 | | static CURLcode cf_ssl_create(struct Curl_cfilter **pcf, |
1308 | | struct Curl_easy *data, |
1309 | | struct connectdata *conn) |
1310 | 0 | { |
1311 | 0 | struct Curl_cfilter *cf = NULL; |
1312 | 0 | struct ssl_connect_data *ctx; |
1313 | 0 | CURLcode result; |
1314 | |
|
1315 | 0 | DEBUGASSERT(data->conn); |
1316 | |
|
1317 | | #ifdef CURL_DISABLE_HTTP |
1318 | | (void)conn; |
1319 | | /* We only support ALPN for HTTP so far. */ |
1320 | | DEBUGASSERT(!conn->bits.tls_enable_alpn); |
1321 | | ctx = cf_ctx_new(data, NULL); |
1322 | | #else |
1323 | 0 | ctx = cf_ctx_new(data, alpn_get_spec(data->state.http_neg.wanted, |
1324 | 0 | data->state.http_neg.preferred, |
1325 | 0 | (bool)data->state.http_neg.only_10, |
1326 | 0 | (bool)conn->bits.tls_enable_alpn)); |
1327 | 0 | #endif |
1328 | 0 | if(!ctx) { |
1329 | 0 | result = CURLE_OUT_OF_MEMORY; |
1330 | 0 | goto out; |
1331 | 0 | } |
1332 | | |
1333 | 0 | result = Curl_cf_create(&cf, &Curl_cft_ssl, ctx); |
1334 | |
|
1335 | 0 | out: |
1336 | 0 | if(result) |
1337 | 0 | cf_ctx_free(ctx); |
1338 | 0 | *pcf = result ? NULL : cf; |
1339 | 0 | return result; |
1340 | 0 | } |
1341 | | |
1342 | | static CURLcode cf_ssl_peer_init(struct Curl_cfilter *cf, |
1343 | | struct Curl_peer *origin, |
1344 | | struct Curl_peer *peer, |
1345 | | struct ssl_filter_config *sslc) |
1346 | 0 | { |
1347 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1348 | 0 | char tls_id[80]; |
1349 | 0 | connssl->ssl_impl->version(tls_id, sizeof(tls_id) - 1); |
1350 | 0 | return Curl_ssl_peer_init(&connssl->peer, origin, peer, sslc, |
1351 | 0 | tls_id, TRNSPRT_TCP); |
1352 | 0 | } |
1353 | | |
1354 | | CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data, |
1355 | | struct Curl_peer *origin, |
1356 | | struct Curl_cfchain *chain) |
1357 | 0 | { |
1358 | 0 | struct connectdata *conn = chain->conn; |
1359 | 0 | struct Curl_cfilter *cf; |
1360 | 0 | struct Curl_peer *peer = Curl_cfchain_get_via_peer(chain); |
1361 | 0 | CURLcode result; |
1362 | |
|
1363 | 0 | result = cf_ssl_create(&cf, data, conn); |
1364 | 0 | if(!result) |
1365 | 0 | result = cf_ssl_peer_init(cf, origin, peer, &conn->ssl_config); |
1366 | 0 | if(!result) |
1367 | 0 | Curl_cfchain_add(chain, cf); |
1368 | 0 | else if(cf) |
1369 | 0 | Curl_conn_cf_discard_chain(&cf, data); |
1370 | 0 | return result; |
1371 | 0 | } |
1372 | | |
1373 | | CURLcode Curl_ssl_cf_insert_after(struct Curl_cfilter *cf_at, |
1374 | | struct Curl_easy *data, |
1375 | | struct Curl_peer *origin, |
1376 | | struct Curl_peer *peer) |
1377 | 0 | { |
1378 | 0 | struct Curl_cfilter *cf; |
1379 | 0 | CURLcode result; |
1380 | |
|
1381 | 0 | result = cf_ssl_create(&cf, data, CF_CONN(cf_at)); |
1382 | 0 | if(!result) |
1383 | 0 | result = cf_ssl_peer_init(cf, origin, peer, &CF_CONN(cf_at)->ssl_config); |
1384 | 0 | if(!result) { |
1385 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1386 | 0 | #if defined(USE_HTTPSRR) && defined(USE_ECH) |
1387 | | /* When using ECH, kick off the HTTPS-RR resolve */ |
1388 | 0 | if((origin->scheme->family == CURLPROTO_HTTP) && |
1389 | 0 | Curl_ssl_need_httpsrr(cf)) { |
1390 | 0 | result = Curl_dns_cf_add_https_resolve(data, cf, origin); |
1391 | 0 | } |
1392 | 0 | #endif /* USE_HTTPSRR && USE_ECH */ |
1393 | 0 | } |
1394 | 0 | else if(cf) |
1395 | 0 | Curl_conn_cf_discard_chain(&cf, data); |
1396 | 0 | return result; |
1397 | 0 | } |
1398 | | |
1399 | | #ifndef CURL_DISABLE_PROXY |
1400 | | |
1401 | | static CURLcode cf_ssl_proxy_create(struct Curl_cfilter **pcf, |
1402 | | struct Curl_easy *data, |
1403 | | struct connectdata *conn) |
1404 | 0 | { |
1405 | 0 | struct Curl_cfilter *cf = NULL; |
1406 | 0 | struct ssl_connect_data *ctx; |
1407 | 0 | CURLcode result; |
1408 | | /* ALPN is default, but if user explicitly disables it, obey */ |
1409 | 0 | bool use_alpn = (bool)data->set.ssl_enable_alpn; |
1410 | 0 | http_majors wanted = CURL_HTTP_V1x; |
1411 | |
|
1412 | 0 | (void)conn; |
1413 | 0 | #ifdef USE_HTTP2 |
1414 | 0 | if(conn->http_proxy.proxytype == CURLPROXY_HTTPS2) { |
1415 | 0 | use_alpn = TRUE; |
1416 | 0 | wanted = (CURL_HTTP_V1x | CURL_HTTP_V2x); |
1417 | 0 | } |
1418 | 0 | #endif |
1419 | |
|
1420 | 0 | ctx = cf_ctx_new(data, alpn_get_spec(wanted, 0, FALSE, use_alpn)); |
1421 | 0 | if(!ctx) { |
1422 | 0 | result = CURLE_OUT_OF_MEMORY; |
1423 | 0 | goto out; |
1424 | 0 | } |
1425 | 0 | result = Curl_cf_create(&cf, &Curl_cft_ssl_proxy, ctx); |
1426 | |
|
1427 | 0 | out: |
1428 | 0 | if(result) |
1429 | 0 | cf_ctx_free(ctx); |
1430 | 0 | *pcf = result ? NULL : cf; |
1431 | 0 | return result; |
1432 | 0 | } |
1433 | | |
1434 | | CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at, |
1435 | | struct Curl_easy *data, |
1436 | | struct Curl_peer *peer) |
1437 | 0 | { |
1438 | 0 | struct Curl_cfilter *cf; |
1439 | 0 | CURLcode result; |
1440 | |
|
1441 | 0 | result = cf_ssl_proxy_create(&cf, data, CF_CONN(cf_at)); |
1442 | 0 | if(!result) |
1443 | 0 | result = cf_ssl_peer_init(cf, peer, NULL, |
1444 | 0 | &CF_CONN(cf_at)->proxy_ssl_config); |
1445 | 0 | if(!result) |
1446 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
1447 | 0 | else if(cf) |
1448 | 0 | Curl_conn_cf_discard_chain(&cf, data); |
1449 | 0 | return result; |
1450 | 0 | } |
1451 | | |
1452 | | #endif /* !CURL_DISABLE_PROXY */ |
1453 | | |
1454 | | bool Curl_ssl_supports(struct Curl_easy *data, unsigned int ssl_option) |
1455 | 0 | { |
1456 | 0 | (void)data; |
1457 | 0 | return (Curl_ssl->supports & ssl_option); |
1458 | 0 | } |
1459 | | |
1460 | | #ifdef USE_ECH |
1461 | | bool Curl_ssl_ech_enabled(struct Curl_cfilter *cf) |
1462 | 0 | { |
1463 | 0 | if(Curl_ssl->supports & SSLSUPP_ECH) { |
1464 | 0 | struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf); |
1465 | 0 | return !!conn_config->ech; |
1466 | 0 | } |
1467 | 0 | return FALSE; |
1468 | 0 | } |
1469 | | |
1470 | | bool Curl_ssl_need_httpsrr(struct Curl_cfilter *cf) |
1471 | 0 | { |
1472 | 0 | if(Curl_ssl->supports & SSLSUPP_ECH) { |
1473 | 0 | struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf); |
1474 | 0 | if(!conn_config->ech) |
1475 | 0 | return FALSE; |
1476 | 0 | if((conn_config->ech == CURLECH_GREASE) || conn_config->ech_config) |
1477 | 0 | return FALSE; |
1478 | 0 | return TRUE; |
1479 | 0 | } |
1480 | 0 | return FALSE; |
1481 | 0 | } |
1482 | | #endif |
1483 | | |
1484 | | static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf, |
1485 | | struct Curl_easy *data, |
1486 | | bool send_shutdown, bool *done) |
1487 | 0 | { |
1488 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1489 | 0 | struct cf_call_data save; |
1490 | 0 | CURLcode result = CURLE_OK; |
1491 | 0 | timediff_t timeout_ms; |
1492 | 0 | int what, loop = 10; |
1493 | |
|
1494 | 0 | if(cf->shutdown) { |
1495 | 0 | *done = TRUE; |
1496 | 0 | return CURLE_OK; |
1497 | 0 | } |
1498 | 0 | CF_DATA_SAVE(save, cf, data); |
1499 | |
|
1500 | 0 | *done = FALSE; |
1501 | 0 | while(!result && !*done && loop--) { |
1502 | 0 | timeout_ms = Curl_cshutdn_timeleft_ms(data, cf->chain); |
1503 | |
|
1504 | 0 | if(timeout_ms < 0) { |
1505 | | /* no need to continue if time is already up */ |
1506 | 0 | failf(data, "SSL shutdown timeout"); |
1507 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
1508 | 0 | goto out; |
1509 | 0 | } |
1510 | | |
1511 | 0 | result = connssl->ssl_impl->shut_down(cf, data, send_shutdown, done); |
1512 | 0 | if(result || *done) |
1513 | 0 | goto out; |
1514 | | |
1515 | 0 | if(connssl->io_need) { |
1516 | 0 | what = Curl_conn_cf_poll(cf, data, timeout_ms); |
1517 | 0 | if(what < 0) { |
1518 | | /* fatal error */ |
1519 | 0 | failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO); |
1520 | 0 | result = CURLE_RECV_ERROR; |
1521 | 0 | goto out; |
1522 | 0 | } |
1523 | 0 | else if(what == 0) { |
1524 | | /* timeout */ |
1525 | 0 | failf(data, "SSL shutdown timeout"); |
1526 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
1527 | 0 | goto out; |
1528 | 0 | } |
1529 | | /* socket is readable or writable */ |
1530 | 0 | } |
1531 | 0 | } |
1532 | 0 | out: |
1533 | 0 | CF_DATA_RESTORE(cf, save); |
1534 | 0 | cf->shutdown = (result || *done); |
1535 | 0 | return result; |
1536 | 0 | } |
1537 | | |
1538 | | CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data, |
1539 | | bool send_shutdown) |
1540 | 0 | { |
1541 | 0 | struct Curl_cfilter *cf, *head; |
1542 | 0 | CURLcode result = CURLE_OK; |
1543 | |
|
1544 | 0 | head = data->conn ? data->conn->chain.cf : NULL; |
1545 | 0 | for(cf = head; cf; cf = cf->next) { |
1546 | 0 | if(cf->cft == &Curl_cft_ssl) { |
1547 | 0 | bool done; |
1548 | 0 | CURL_TRC_CF(data, cf, "shutdown and remove SSL, start"); |
1549 | 0 | Curl_cshutdn_start_timer(data, cf->chain, 0); |
1550 | 0 | result = vtls_shutdown_blocking(cf, data, send_shutdown, &done); |
1551 | 0 | Curl_cshutdn_clear_timer(cf->chain); |
1552 | 0 | if(!result && !done) /* blocking failed? */ |
1553 | 0 | result = CURLE_SSL_SHUTDOWN_FAILED; |
1554 | 0 | Curl_conn_cf_discard(&cf, data); |
1555 | 0 | CURL_TRC_CF(data, cf, "shutdown and remove SSL, done -> %d", |
1556 | 0 | (int)result); |
1557 | 0 | break; |
1558 | 0 | } |
1559 | 0 | } |
1560 | 0 | return result; |
1561 | 0 | } |
1562 | | |
1563 | | bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf) |
1564 | 0 | { |
1565 | 0 | return (cf->cft->flags & CF_TYPE_SSL) && (cf->cft->flags & CF_TYPE_PROXY); |
1566 | 0 | } |
1567 | | |
1568 | | struct ssl_easy_config *Curl_ssl_cf_get_easy_config( |
1569 | | struct Curl_cfilter *cf, struct Curl_easy *data) |
1570 | 0 | { |
1571 | | #ifdef CURL_DISABLE_PROXY |
1572 | | (void)cf; |
1573 | | return &data->set.ssl; |
1574 | | #else |
1575 | 0 | return Curl_ssl_cf_is_proxy(cf) ? &data->set.proxy_ssl : &data->set.ssl; |
1576 | 0 | #endif |
1577 | 0 | } |
1578 | | |
1579 | | struct ssl_filter_config *Curl_ssl_cf_get_filter_config( |
1580 | | struct Curl_cfilter *cf) |
1581 | 0 | { |
1582 | | #ifdef CURL_DISABLE_PROXY |
1583 | | return &CF_CONN(cf)->ssl_config; |
1584 | | #else |
1585 | 0 | return Curl_ssl_cf_is_proxy(cf) ? |
1586 | 0 | &CF_CONN(cf)->proxy_ssl_config : &CF_CONN(cf)->ssl_config; |
1587 | 0 | #endif |
1588 | 0 | } |
1589 | | |
1590 | | CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf, |
1591 | | const struct alpn_spec *spec) |
1592 | 0 | { |
1593 | 0 | size_t i, len; |
1594 | 0 | int off = 0; |
1595 | 0 | unsigned char blen; |
1596 | |
|
1597 | 0 | memset(buf, 0, sizeof(*buf)); |
1598 | 0 | for(i = 0; spec && i < spec->count; ++i) { |
1599 | 0 | len = strlen(spec->entries[i]); |
1600 | 0 | if(len >= ALPN_NAME_MAX) |
1601 | 0 | return CURLE_FAILED_INIT; |
1602 | 0 | blen = (unsigned char)len; |
1603 | 0 | if(off + blen + 1 >= (int)sizeof(buf->data)) |
1604 | 0 | return CURLE_FAILED_INIT; |
1605 | 0 | buf->data[off++] = blen; |
1606 | 0 | memcpy(buf->data + off, spec->entries[i], blen); |
1607 | 0 | off += blen; |
1608 | 0 | } |
1609 | 0 | buf->len = off; |
1610 | 0 | return CURLE_OK; |
1611 | 0 | } |
1612 | | |
1613 | | CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf, |
1614 | | const struct alpn_spec *spec) |
1615 | 0 | { |
1616 | 0 | size_t i, len; |
1617 | 0 | size_t off = 0; |
1618 | |
|
1619 | 0 | memset(buf, 0, sizeof(*buf)); |
1620 | 0 | for(i = 0; spec && i < spec->count; ++i) { |
1621 | 0 | len = strlen(spec->entries[i]); |
1622 | 0 | if(len >= ALPN_NAME_MAX) |
1623 | 0 | return CURLE_FAILED_INIT; |
1624 | 0 | if(off + len + 2 >= sizeof(buf->data)) |
1625 | 0 | return CURLE_FAILED_INIT; |
1626 | 0 | if(off) |
1627 | 0 | buf->data[off++] = ','; |
1628 | 0 | memcpy(buf->data + off, spec->entries[i], len); |
1629 | 0 | off += len; |
1630 | 0 | } |
1631 | 0 | buf->data[off] = '\0'; |
1632 | 0 | buf->len = (int)off; |
1633 | 0 | return CURLE_OK; |
1634 | 0 | } |
1635 | | |
1636 | | bool Curl_alpn_contains_proto(const struct alpn_spec *spec, |
1637 | | const char *proto) |
1638 | 0 | { |
1639 | 0 | size_t i, plen = proto ? strlen(proto) : 0; |
1640 | 0 | for(i = 0; spec && plen && i < spec->count; ++i) { |
1641 | 0 | size_t slen = strlen(spec->entries[i]); |
1642 | 0 | if((slen == plen) && !memcmp(proto, spec->entries[i], plen)) |
1643 | 0 | return TRUE; |
1644 | 0 | } |
1645 | 0 | return FALSE; |
1646 | 0 | } |
1647 | | |
1648 | | void Curl_alpn_restrict_to(struct alpn_spec *spec, const char *proto) |
1649 | 0 | { |
1650 | 0 | size_t plen = strlen(proto); |
1651 | 0 | DEBUGASSERT(plen < sizeof(spec->entries[0])); |
1652 | 0 | if(plen < sizeof(spec->entries[0])) { |
1653 | 0 | memcpy(spec->entries[0], proto, plen + 1); |
1654 | 0 | spec->count = 1; |
1655 | 0 | } |
1656 | 0 | } |
1657 | | |
1658 | | void Curl_alpn_copy(struct alpn_spec *dest, const struct alpn_spec *src) |
1659 | 0 | { |
1660 | 0 | if(src) |
1661 | 0 | memcpy(dest, src, sizeof(*dest)); |
1662 | 0 | else |
1663 | 0 | memset(dest, 0, sizeof(*dest)); |
1664 | 0 | } |
1665 | | |
1666 | | CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf, |
1667 | | struct Curl_easy *data, |
1668 | | struct ssl_connect_data *connssl, |
1669 | | const unsigned char *proto, |
1670 | | size_t proto_len) |
1671 | 0 | { |
1672 | 0 | CURLcode result = CURLE_OK; |
1673 | 0 | (void)cf; |
1674 | |
|
1675 | 0 | if(connssl->negotiated.alpn) { |
1676 | | /* When we ask for a specific ALPN protocol, we need the confirmation |
1677 | | * of it by the server, as we have installed protocol handler and |
1678 | | * connection filter chain for exactly this protocol. */ |
1679 | 0 | if(!proto_len) { |
1680 | 0 | failf(data, "ALPN: asked for '%s' from previous session, " |
1681 | 0 | "but server did not confirm it. Refusing to continue.", |
1682 | 0 | connssl->negotiated.alpn); |
1683 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1684 | 0 | goto out; |
1685 | 0 | } |
1686 | 0 | else if(!proto) { |
1687 | 0 | DEBUGASSERT(0); /* with length, we need a pointer */ |
1688 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1689 | 0 | goto out; |
1690 | 0 | } |
1691 | 0 | else if((strlen(connssl->negotiated.alpn) != proto_len) || |
1692 | 0 | memcmp(connssl->negotiated.alpn, proto, proto_len)) { |
1693 | 0 | failf(data, "ALPN: asked for '%s' from previous session, but server " |
1694 | 0 | "selected '%.*s'. Refusing to continue.", |
1695 | 0 | connssl->negotiated.alpn, (int)proto_len, proto); |
1696 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1697 | 0 | goto out; |
1698 | 0 | } |
1699 | | /* ALPN is exactly what we asked for, done. */ |
1700 | 0 | infof(data, "ALPN: server confirmed to use '%s'", |
1701 | 0 | connssl->negotiated.alpn); |
1702 | 0 | goto out; |
1703 | 0 | } |
1704 | | |
1705 | 0 | if(proto && proto_len) { |
1706 | 0 | if(memchr(proto, '\0', proto_len)) { |
1707 | 0 | failf(data, "ALPN: server selected protocol contains NUL. " |
1708 | 0 | "Refusing to continue."); |
1709 | 0 | result = CURLE_SSL_CONNECT_ERROR; |
1710 | 0 | goto out; |
1711 | 0 | } |
1712 | 0 | connssl->negotiated.alpn = curlx_memdup0((const char *)proto, proto_len); |
1713 | 0 | if(!connssl->negotiated.alpn) |
1714 | 0 | return CURLE_OUT_OF_MEMORY; |
1715 | 0 | } |
1716 | | |
1717 | 0 | if(proto && proto_len) { |
1718 | 0 | if(connssl->state == ssl_connection_deferred) |
1719 | 0 | infof(data, VTLS_INFOF_ALPN_DEFERRED, (int)proto_len, proto); |
1720 | 0 | else |
1721 | 0 | infof(data, VTLS_INFOF_ALPN_ACCEPTED, (int)proto_len, proto); |
1722 | 0 | } |
1723 | 0 | else { |
1724 | 0 | if(connssl->state == ssl_connection_deferred) |
1725 | 0 | infof(data, VTLS_INFOF_NO_ALPN_DEFERRED); |
1726 | 0 | else |
1727 | 0 | infof(data, VTLS_INFOF_NO_ALPN); |
1728 | 0 | } |
1729 | |
|
1730 | 0 | out: |
1731 | 0 | return result; |
1732 | 0 | } |
1733 | | |
1734 | | CURLcode Curl_on_session_reuse(struct Curl_cfilter *cf, |
1735 | | struct Curl_easy *data, |
1736 | | struct alpn_spec *alpns, |
1737 | | struct Curl_ssl_session *scs, |
1738 | | bool *do_early_data, bool early_data_allowed) |
1739 | 0 | { |
1740 | 0 | struct ssl_connect_data *connssl = cf->ctx; |
1741 | 0 | CURLcode result = CURLE_OK; |
1742 | |
|
1743 | 0 | *do_early_data = FALSE; |
1744 | |
|
1745 | 0 | if(!early_data_allowed) { |
1746 | 0 | CURL_TRC_CF(data, cf, "SSL session does not allow earlydata"); |
1747 | 0 | } |
1748 | 0 | else if(!Curl_alpn_contains_proto(alpns, scs->alpn)) { |
1749 | 0 | CURL_TRC_CF(data, cf, "SSL session has different ALPN, no early data"); |
1750 | 0 | } |
1751 | 0 | else { |
1752 | 0 | infof(data, "SSL session allows %zu bytes of early data, " |
1753 | 0 | "reusing ALPN '%s'", connssl->earlydata_max, scs->alpn); |
1754 | 0 | connssl->earlydata_state = ssl_earlydata_await; |
1755 | 0 | connssl->state = ssl_connection_deferred; |
1756 | 0 | result = Curl_alpn_set_negotiated(cf, data, connssl, |
1757 | 0 | (const unsigned char *)scs->alpn, |
1758 | 0 | scs->alpn ? strlen(scs->alpn) : 0); |
1759 | 0 | *do_early_data = !result; |
1760 | 0 | } |
1761 | 0 | return result; |
1762 | 0 | } |
1763 | | |
1764 | | struct Curl_ssl_session *Curl_ssl_get_primary_session( |
1765 | | struct Curl_easy *data, const struct Curl_cftype *cft) |
1766 | 0 | { |
1767 | 0 | if(data->conn && |
1768 | 0 | #ifndef CURL_DISABLE_PROXY |
1769 | 0 | ((cft == &Curl_cft_ssl) || (cft == &Curl_cft_ssl_proxy))) { |
1770 | | #else |
1771 | | (cft == &Curl_cft_ssl)) { |
1772 | | #endif |
1773 | 0 | struct Curl_cfilter *cf1 = data->conn->chain.cf; |
1774 | 0 | for(; cf1; cf1 = cf1->next) { |
1775 | | /* A tunneling proxy does not offer end2end encryption, even if |
1776 | | * it does SSL itself (e.g. QUIC H3 proxy) */ |
1777 | 0 | if(cf1->cft == cft) |
1778 | 0 | break; |
1779 | 0 | } |
1780 | 0 | if(cf1) { |
1781 | 0 | struct ssl_connect_data *connssl = cf1->ctx; |
1782 | 0 | if(connssl) |
1783 | 0 | return connssl->session; |
1784 | 0 | } |
1785 | 0 | } |
1786 | 0 | return NULL; |
1787 | 0 | } |
1788 | | |
1789 | | #endif /* USE_SSL */ |