Artifactspublic-v0

curl

master
ply4 git calls37 ms301 KiB3 cachedprotocol v2
1813 lines50 KiBRaw
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 ***************************************************************************/
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.
29 SSL-functions in libcurl should call functions in this source file, and not
30 to any specific SSL-layer.
32 Curl_ssl_ - prefix for generic ones
34 Note that this source code uses the functions of the configured SSL
35 backend via the global Curl_ssl instance.
37 "SSL/TLS Strong Encryption: An Introduction"
38 https://httpd.apache.org/docs/2.0/ssl/ssl_intro.html
39 */
41#include "curl_setup.h"
43#include "urldata.h"
44#include "cfilters.h"
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"
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 */
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"
74#ifdef USE_APPLE_SECTRUST
75#include <Security/Security.h>
76#endif
78#ifdef USE_SSL
79#if !defined(CURL_DISABLE_HTTP) || !defined(CURL_DISABLE_PROXY)
80static const struct alpn_spec ALPN_SPEC_H11 = {
81 { ALPN_HTTP_1_1 }, 1
82};
83static const struct alpn_spec ALPN_SPEC_H10_H11 = {
84 { ALPN_HTTP_1_0, ALPN_HTTP_1_1 }, 2
85};
86#ifdef USE_HTTP2
87static const struct alpn_spec ALPN_SPEC_H2 = {
88 { ALPN_H2 }, 1
89};
90static const struct alpn_spec ALPN_SPEC_H2_H11 = {
91 { ALPN_H2, ALPN_HTTP_1_1 }, 2
92};
93static const struct alpn_spec ALPN_SPEC_H11_H2 = {
94 { ALPN_HTTP_1_1, ALPN_H2 }, 2
95};
96#endif /* USE_HTTP2 */
98static const struct alpn_spec *alpn_get_spec(http_majors wanted,
99 http_majors preferred,
100 bool only_http_10,
101 bool use_alpn)
103 if(!use_alpn)
104 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 if(only_http_10 && (wanted & CURL_HTTP_V1x))
109 return &ALPN_SPEC_H10_H11;
110#ifdef USE_HTTP2
111 if(wanted & CURL_HTTP_V2x) {
112 if(wanted & CURL_HTTP_V1x)
113 return (preferred == CURL_HTTP_V1x) ?
114 &ALPN_SPEC_H11_H2 : &ALPN_SPEC_H2_H11;
115 return &ALPN_SPEC_H2;
116 }
117#else
118 (void)wanted;
119 (void)preferred;
120#endif
121 return &ALPN_SPEC_H11;
123#endif /* !CURL_DISABLE_HTTP || !CURL_DISABLE_PROXY */
124#endif /* USE_SSL */
126#ifdef USE_SSL
127static int multissl_setup(const struct Curl_ssl *backend);
128#endif
130curl_sslbackend Curl_ssl_backend(void)
132#ifdef USE_SSL
133 multissl_setup(NULL);
134 return Curl_ssl->info.id;
135#else
136 return CURLSSLBACKEND_NONE;
137#endif
140#ifdef USE_SSL
142/* "global" init done? */
143static bool init_ssl = FALSE;
145/**
146 * Global SSL init
147 *
148 * @retval 0 error initializing SSL
149 * @retval 1 SSL initialized successfully
150 */
151int Curl_ssl_init(void)
153 /* make sure this is only done once */
154 if(init_ssl)
155 return 1;
156 init_ssl = TRUE; /* never again */
158 if(Curl_ssl->init)
159 return Curl_ssl->init();
160 return 1;
163static bool ssl_prefs_check(struct Curl_cfilter *cf,
164 struct Curl_easy *data)
166 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
167 /* check for CURLOPT_SSLVERSION invalid parameter value */
168 if(conn_config->version >= CURL_SSLVERSION_LAST) {
169 failf(data, "Unrecognized parameter value passed via CURLOPT_SSLVERSION");
170 return FALSE;
171 }
173 switch(conn_config->version_max) {
174 case CURL_SSLVERSION_MAX_NONE:
175 case CURL_SSLVERSION_MAX_DEFAULT:
176 break;
178 default:
179 if((conn_config->version_max >> 16) < conn_config->version) {
180 failf(data, "CURL_SSLVERSION_MAX incompatible with CURL_SSLVERSION");
181 return FALSE;
182 }
183 }
185 return TRUE;
188static struct ssl_connect_data *cf_ctx_new(struct Curl_easy *data,
189 const struct alpn_spec *alpn)
191 struct ssl_connect_data *ctx;
193 (void)data;
194 ctx = curlx_calloc(1, sizeof(*ctx));
195 if(!ctx)
196 return NULL;
198 ctx->ssl_impl = Curl_ssl;
199 ctx->alpn = alpn;
200 Curl_bufq_init2(&ctx->earlydata, CURL_SSL_EARLY_MAX, 1, BUFQ_OPT_NO_SPARES);
201 ctx->backend = curlx_calloc(1, ctx->ssl_impl->sizeof_ssl_backend_data);
202 if(!ctx->backend) {
203 curlx_free(ctx);
204 return NULL;
205 }
206 return ctx;
209static void cf_ctx_free(struct ssl_connect_data *ctx)
211 if(ctx) {
212 curlx_safefree(ctx->negotiated.alpn);
213 Curl_bufq_free(&ctx->earlydata);
214 curlx_free(ctx->backend);
215 curlx_free(ctx);
216 }
219CURLcode Curl_ssl_get_channel_binding(struct Curl_easy *data, int8_t sockindex,
220 struct dynbuf *binding)
222 if(Curl_ssl->get_channel_binding)
223 return Curl_ssl->get_channel_binding(data, sockindex, binding);
224 return CURLE_OK;
227void Curl_ssl_close_all(struct Curl_easy *data)
229 if(Curl_ssl->close_all)
230 Curl_ssl->close_all(data);
233CURLcode Curl_ssl_adjust_pollset(struct Curl_cfilter *cf,
234 struct Curl_easy *data,
235 struct easy_pollset *ps)
237 struct ssl_connect_data *connssl = cf->ctx;
239 if(connssl->io_need) {
240 curl_socket_t sock = Curl_conn_cf_get_socket(cf->next, data);
241 CURLcode result = CURLE_OK;
242 if(sock != CURL_SOCKET_BAD) {
243 if(connssl->io_need & CURL_SSL_IO_NEED_SEND) {
244 result = Curl_pollset_set_out_only(data, ps, sock);
245 CURL_TRC_CF(data, cf, "adjust_pollset, POLLOUT fd=%" FMT_SOCKET_T,
246 sock);
247 }
248 else {
249 result = Curl_pollset_set_in_only(data, ps, sock);
250 CURL_TRC_CF(data, cf, "adjust_pollset, POLLIN fd=%" FMT_SOCKET_T,
251 sock);
252 }
253 }
254 return result;
255 }
256 return CURLE_OK;
259/* Return list of OpenSSL crypto engine names. */
260struct curl_slist *Curl_ssl_engines_list(struct Curl_easy *data)
262 if(Curl_ssl->engines_list)
263 return Curl_ssl->engines_list(data);
264 return NULL;
267static size_t multissl_version(char *buffer, size_t size);
269void Curl_ssl_version(char *buffer, size_t size)
271#ifdef CURL_WITH_MULTI_SSL
272 (void)multissl_version(buffer, size);
273#else
274 (void)Curl_ssl->version(buffer, size);
275#endif
278void Curl_ssl_free_certinfo(struct Curl_easy *data)
280 struct curl_certinfo *ci = &data->info.certs;
282 if(ci->num_of_certs) {
283 /* free all individual lists used */
284 int i;
285 for(i = 0; i < ci->num_of_certs; i++) {
286 curl_slist_free_all(ci->certinfo[i]);
287 ci->certinfo[i] = NULL;
288 }
290 curlx_safefree(ci->certinfo); /* free the actual array too */
291 ci->num_of_certs = 0;
292 }
295CURLcode Curl_ssl_init_certinfo(struct Curl_easy *data, int num)
297 struct curl_certinfo *ci = &data->info.certs;
298 struct curl_slist **table;
300 /* Free any previous certificate information structures */
301 Curl_ssl_free_certinfo(data);
303 /* Allocate the required certificate information structures */
304 table = curlx_calloc((size_t)num, sizeof(struct curl_slist *));
305 if(!table)
306 return CURLE_OUT_OF_MEMORY;
308 ci->num_of_certs = num;
309 ci->certinfo = table;
311 return CURLE_OK;
314/*
315 * 'value' is NOT a null-terminated string
316 */
317CURLcode Curl_ssl_push_certinfo_len(struct Curl_easy *data,
318 int certnum,
319 const char *label,
320 const char *value,
321 size_t valuelen)
323 struct curl_certinfo *ci = &data->info.certs;
324 struct curl_slist *nl;
325 CURLcode result = CURLE_OK;
326 struct dynbuf build;
328 DEBUGASSERT(certnum < ci->num_of_certs);
330 curlx_dyn_init(&build, CURL_X509_STR_MAX);
332 if(curlx_dyn_add(&build, label) ||
333 curlx_dyn_addn(&build, ":", 1) ||
334 curlx_dyn_addn(&build, value, valuelen))
335 return CURLE_OUT_OF_MEMORY;
337 nl = Curl_slist_append_nodup(ci->certinfo[certnum], curlx_dyn_ptr(&build));
338 if(!nl) {
339 curlx_dyn_free(&build);
340 curl_slist_free_all(ci->certinfo[certnum]);
341 result = CURLE_OUT_OF_MEMORY;
342 }
344 ci->certinfo[certnum] = nl;
345 return result;
348/* get length bytes of randomness */
349CURLcode Curl_ssl_random(struct Curl_easy *data,
350 unsigned char *buffer, size_t length)
352 if(Curl_ssl->random)
353 return Curl_ssl->random(data, buffer, length);
354 else
355 return CURLE_NOT_BUILT_IN;
358/*
359 * Public key pem to der conversion
360 */
362static CURLcode pubkey_pem_to_der(const char *pem,
363 unsigned char **der, size_t *der_len)
365 const char *begin_pos, *end_pos;
366 size_t pem_count, pem_len;
367 CURLcode result;
368 struct dynbuf pbuf;
370 /* if no pem, exit. */
371 if(!pem)
372 return CURLE_BAD_CONTENT_ENCODING;
374 curlx_dyn_init(&pbuf, MAX_PINNED_PUBKEY_SIZE);
376 begin_pos = strstr(pem, "-----BEGIN PUBLIC KEY-----");
377 if(!begin_pos)
378 return CURLE_BAD_CONTENT_ENCODING;
380 pem_count = begin_pos - pem;
381 /* Invalid if not at beginning AND not directly following \n */
382 if(pem_count && '\n' != pem[pem_count - 1])
383 return CURLE_BAD_CONTENT_ENCODING;
385 /* 26 is length of "-----BEGIN PUBLIC KEY-----" */
386 pem_count += 26;
388 /* Invalid if not directly following \n */
389 end_pos = strstr(pem + pem_count, "\n-----END PUBLIC KEY-----");
390 if(!end_pos)
391 return CURLE_BAD_CONTENT_ENCODING;
393 pem_len = end_pos - pem;
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 while(pem_count < pem_len) {
401 if('\n' != pem[pem_count] && '\r' != pem[pem_count]) {
402 result = curlx_dyn_addn(&pbuf, &pem[pem_count], 1);
403 if(result)
404 return result;
405 }
406 ++pem_count;
407 }
409 if(curlx_dyn_len(&pbuf)) {
410 result = curlx_base64_decode(curlx_dyn_ptr(&pbuf), der, der_len);
411 curlx_dyn_free(&pbuf);
412 }
413 else
414 result = CURLE_BAD_CONTENT_ENCODING;
416 return result;
419/*
420 * Generic pinned public key check.
421 */
423CURLcode Curl_pin_peer_pubkey(struct Curl_easy *data,
424 const char *pinnedpubkey,
425 const unsigned char *pubkey, size_t pubkeylen)
427 CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
429 /* if a path was not specified, do not pin */
430 if(!pinnedpubkey)
431 return CURLE_OK;
432 if(!pubkey || !pubkeylen)
433 return result;
435 /* only do this if pinnedpubkey starts with "sha256//", length 8 */
436 if(!strncmp(pinnedpubkey, "sha256//", 8)) {
437 CURLcode encode;
438 char *cert_hash = NULL;
439 const char *pinned_hash, *end_pos;
440 size_t cert_hash_len = 0, pinned_hash_len;
441 unsigned char *sha256sumdigest;
443 if(!Curl_ssl->sha256sum) {
444 /* without sha256 support, this cannot match */
445 return result;
446 }
448 /* compute sha256sum of public key */
449 sha256sumdigest = curlx_malloc(CURL_SHA256_DIGEST_LENGTH);
450 if(!sha256sumdigest)
451 return CURLE_OUT_OF_MEMORY;
452 encode = Curl_ssl->sha256sum(pubkey, pubkeylen,
453 sha256sumdigest, CURL_SHA256_DIGEST_LENGTH);
455 if(!encode)
456 encode = curlx_base64_encode(sha256sumdigest,
457 CURL_SHA256_DIGEST_LENGTH,
458 &cert_hash, &cert_hash_len);
459 curlx_safefree(sha256sumdigest);
461 if(encode)
462 return encode;
464 infof(data, " public key hash: sha256//%s", cert_hash);
466 pinned_hash = pinnedpubkey;
467 while(pinned_hash &&
468 !strncmp(pinned_hash, "sha256//", CURL_CSTRLEN("sha256//"))) {
469 pinned_hash = pinned_hash + CURL_CSTRLEN("sha256//");
470 end_pos = strchr(pinned_hash, ';');
471 pinned_hash_len = end_pos ?
472 (size_t)(end_pos - pinned_hash) : strlen(pinned_hash);
474 /* compare base64 sha256 digests */
475 if(cert_hash_len == pinned_hash_len &&
476 !memcmp(cert_hash, pinned_hash, cert_hash_len)) {
477 DEBUGF(infof(data, "public key hash matches pinned value"));
478 result = CURLE_OK;
479 break;
480 }
482 DEBUGF(infof(data, "public key hash does not match 'sha256//%.*s'",
483 (int)pinned_hash_len, pinned_hash));
484 /* next one or we are at the end */
485 pinned_hash = end_pos ? (end_pos + 1) : NULL;
486 }
487 curlx_safefree(cert_hash);
488 }
489 else {
490 long filesize;
491 size_t size, pem_len;
492 CURLcode pem_read;
493 struct dynbuf buf;
494 unsigned char *pem_ptr = NULL;
495 size_t left;
496 FILE *fp = curlx_fopen(pinnedpubkey, "rb");
497 if(!fp)
498 return result;
500 curlx_dyn_init(&buf, MAX_PINNED_PUBKEY_SIZE);
502 /* Determine the file's size */
503 if(fseek(fp, 0, SEEK_END))
504 goto end;
505 filesize = ftell(fp);
506 if(fseek(fp, 0, SEEK_SET))
507 goto end;
508 if(filesize < 0 || filesize > MAX_PINNED_PUBKEY_SIZE)
509 goto end;
511 /*
512 * if the size of our certificate is bigger than the file
513 * size then it cannot match
514 */
515 size = curlx_sotouz((curl_off_t)filesize);
516 if(pubkeylen > size)
517 goto end;
519 /*
520 * Read the file into the dynbuf
521 */
522 left = size;
523 do {
524 char buffer[1024];
525 size_t want = left > sizeof(buffer) ? sizeof(buffer) : left;
526 if(fread(buffer, 1, want, fp) != want)
527 goto end;
528 if(curlx_dyn_addn(&buf, buffer, want))
529 goto end;
530 left -= want;
531 } while(left);
533 /* If the sizes are the same, it cannot be base64 encoded, must be der */
534 if(pubkeylen == size) {
535 if(!memcmp(pubkey, curlx_dyn_ptr(&buf), pubkeylen))
536 result = CURLE_OK;
537 goto end;
538 }
540 /*
541 * Otherwise we assume it is PEM and try to decode it after placing
542 * null-terminator
543 */
544 pem_read = pubkey_pem_to_der(curlx_dyn_ptr(&buf), &pem_ptr, &pem_len);
545 /* if it was not read successfully, exit */
546 if(pem_read)
547 goto end;
549 /*
550 * if the size of our certificate does not match the size of
551 * the decoded file, they cannot be the same, otherwise compare
552 */
553 if(pubkeylen == pem_len && !memcmp(pubkey, pem_ptr, pubkeylen))
554 result = CURLE_OK;
555end:
556 curlx_dyn_free(&buf);
557 curlx_safefree(pem_ptr);
558 curlx_fclose(fp);
559 }
561 return result;
564/*
565 * Check whether the SSL backend supports the status_request extension.
566 */
567bool Curl_ssl_cert_status_request(void)
569 if(Curl_ssl->cert_status_request)
570 return Curl_ssl->cert_status_request();
571 return FALSE;
574static int multissl_init(void)
576 if(multissl_setup(NULL))
577 return 1;
578 if(Curl_ssl->init)
579 return Curl_ssl->init();
580 return 1;
583static CURLcode multissl_random(struct Curl_easy *data,
584 unsigned char *entropy, size_t length)
586 if(multissl_setup(NULL))
587 return CURLE_FAILED_INIT;
588 return Curl_ssl->random(data, entropy, length);
591static CURLcode multissl_connect(struct Curl_cfilter *cf,
592 struct Curl_easy *data, bool *done)
594 if(multissl_setup(NULL))
595 return CURLE_FAILED_INIT;
596 return Curl_ssl->do_connect(cf, data, done);
599static CURLcode multissl_adjust_pollset(struct Curl_cfilter *cf,
600 struct Curl_easy *data,
601 struct easy_pollset *ps)
603 if(multissl_setup(NULL))
604 return CURLE_OK;
605 return Curl_ssl->adjust_pollset(cf, data, ps);
608static void *multissl_get_internals(struct ssl_connect_data *connssl,
609 CURLINFO info)
611 if(multissl_setup(NULL))
612 return NULL;
613 return Curl_ssl->get_internals(connssl, info);
616static void multissl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
618 if(multissl_setup(NULL))
619 return;
620 Curl_ssl->close(cf, data);
623static CURLcode multissl_recv_plain(struct Curl_cfilter *cf,
624 struct Curl_easy *data,
625 char *buf, size_t len, size_t *pnread)
627 if(multissl_setup(NULL))
628 return CURLE_FAILED_INIT;
629 return Curl_ssl->recv_plain(cf, data, buf, len, pnread);
632static CURLcode multissl_send_plain(struct Curl_cfilter *cf,
633 struct Curl_easy *data,
634 const void *mem, size_t len,
635 size_t *pnwritten)
637 if(multissl_setup(NULL))
638 return CURLE_FAILED_INIT;
639 return Curl_ssl->send_plain(cf, data, mem, len, pnwritten);
642static const struct Curl_ssl Curl_ssl_multi = {
643 { CURLSSLBACKEND_NONE, "multi" }, /* info */
644 0, /* supports nothing */
645 (size_t)-1, /* something insanely large to be on the safe side */
647 multissl_init, /* init */
648 NULL, /* cleanup */
649 multissl_version, /* version */
650 NULL, /* shutdown */
651 NULL, /* data_pending */
652 multissl_random, /* random */
653 NULL, /* cert_status_request */
654 multissl_connect, /* connect */
655 multissl_adjust_pollset, /* adjust_pollset */
656 multissl_get_internals, /* get_internals */
657 multissl_close, /* close_one */
658 NULL, /* close_all */
659 NULL, /* engines_list */
660 NULL, /* sha256sum */
661 multissl_recv_plain, /* recv decrypted data */
662 multissl_send_plain, /* send data to encrypt */
663 NULL, /* get_channel_binding */
664};
666const struct Curl_ssl *Curl_ssl =
667#ifdef CURL_WITH_MULTI_SSL
668 &Curl_ssl_multi;
669#elif defined(USE_WOLFSSL)
670 &Curl_ssl_wolfssl;
671#elif defined(USE_GNUTLS)
672 &Curl_ssl_gnutls;
673#elif defined(USE_MBEDTLS)
674 &Curl_ssl_mbedtls;
675#elif defined(USE_RUSTLS)
676 &Curl_ssl_rustls;
677#elif defined(USE_OPENSSL)
678 &Curl_ssl_openssl;
679#elif defined(USE_SCHANNEL)
680 &Curl_ssl_schannel;
681#else
682#error "Missing struct Curl_ssl for selected SSL backend"
683#endif
685static const struct Curl_ssl *available_backends[] = {
686#ifdef USE_WOLFSSL
687 &Curl_ssl_wolfssl,
688#endif
689#ifdef USE_GNUTLS
690 &Curl_ssl_gnutls,
691#endif
692#ifdef USE_MBEDTLS
693 &Curl_ssl_mbedtls,
694#endif
695#ifdef USE_OPENSSL
696 &Curl_ssl_openssl,
697#endif
698#ifdef USE_SCHANNEL
699 &Curl_ssl_schannel,
700#endif
701#ifdef USE_RUSTLS
702 &Curl_ssl_rustls,
703#endif
704 NULL
705};
707/* Global cleanup */
708void Curl_ssl_cleanup(void)
710 if(init_ssl) {
711 /* only cleanup if we did a previous init */
712 if(Curl_ssl->cleanup)
713 Curl_ssl->cleanup();
714#ifdef CURL_WITH_MULTI_SSL
715 Curl_ssl = &Curl_ssl_multi;
716#endif
717 init_ssl = FALSE;
718 }
721static size_t multissl_version(char *buffer, size_t size)
723 static const struct Curl_ssl *selected;
724 static char backends[200];
725 static size_t backends_len;
726 const struct Curl_ssl *current;
728 current = Curl_ssl == &Curl_ssl_multi ? available_backends[0] : Curl_ssl;
730 if(current != selected) {
731 char *p = backends;
732 const char *end = backends + sizeof(backends);
733 int i;
735 selected = current;
737 backends[0] = '\0';
739 for(i = 0; available_backends[i]; ++i) {
740 char vb[200];
741 bool paren = (selected != available_backends[i]);
743 if(available_backends[i]->version(vb, sizeof(vb))) {
744 p += curl_msnprintf(p, end - p, "%s%s%s%s", (p != backends ? " " : ""),
745 (paren ? "(" : ""), vb, (paren ? ")" : ""));
746 }
747 }
749 backends_len = p - backends;
750 }
752 if(size) {
753 curlx_strcopy(buffer, size, backends, backends_len);
754 }
755 return 0;
758static int multissl_setup(const struct Curl_ssl *backend)
760 int i;
761 char *env;
763 if(Curl_ssl != &Curl_ssl_multi)
764 return 1;
766 if(backend) {
767 Curl_ssl = backend;
768 return 0;
769 }
771 if(!available_backends[0])
772 return 1;
774 env = curl_getenv("CURL_SSL_BACKEND");
775 if(env) {
776 for(i = 0; available_backends[i]; i++) {
777 if(curl_strequal(env, available_backends[i]->info.name)) {
778 Curl_ssl = available_backends[i];
779 curlx_free(env);
780 return 0;
781 }
782 }
783 }
785#ifdef CURL_DEFAULT_SSL_BACKEND
786 for(i = 0; available_backends[i]; i++) {
787 if(curl_strequal(CURL_DEFAULT_SSL_BACKEND,
788 available_backends[i]->info.name)) {
789 Curl_ssl = available_backends[i];
790 curlx_free(env);
791 return 0;
792 }
793 }
794#endif
796 /* Fall back to first available backend */
797 Curl_ssl = available_backends[0];
798 curlx_free(env);
799 return 0;
802/* This function is used to select the SSL backend to use. It is called by
803 curl_global_sslset (easy.c) which uses the global init lock. */
804CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
805 const curl_ssl_backend ***avail)
807 int i;
809 if(avail)
810 *avail = (const curl_ssl_backend **)&available_backends;
812 if(Curl_ssl != &Curl_ssl_multi)
813 return id == Curl_ssl->info.id ||
814 (name && curl_strequal(name, Curl_ssl->info.name)) ?
815 CURLSSLSET_OK :
816#ifdef CURL_WITH_MULTI_SSL
817 CURLSSLSET_TOO_LATE;
818#else
819 CURLSSLSET_UNKNOWN_BACKEND;
820#endif
822 for(i = 0; available_backends[i]; i++) {
823 if(available_backends[i]->info.id == id ||
824 (name && curl_strequal(available_backends[i]->info.name, name))) {
825 multissl_setup(available_backends[i]);
826 return CURLSSLSET_OK;
827 }
828 }
830 return CURLSSLSET_UNKNOWN_BACKEND;
833#else /* USE_SSL */
834CURLsslset Curl_init_sslset_nolock(curl_sslbackend id, const char *name,
835 const curl_ssl_backend ***avail)
837 (void)id;
838 (void)name;
839 (void)avail;
840 return CURLSSLSET_NO_BACKENDS;
843#endif /* !USE_SSL */
845#ifdef USE_SSL
847void Curl_ssl_peer_cleanup(struct ssl_peer *peer)
849 Curl_peer_unlink(&peer->origin);
850 Curl_peer_unlink(&peer->peer);
851 curlx_safefree(peer->sni);
852 curlx_safefree(peer->scache_key);
853 peer->transport = TRNSPRT_NONE;
854 peer->type = CURL_SSL_PEER_DNS;
857static ssl_peer_type get_peer_type(const char *hostname)
859 if(hostname && hostname[0]) {
860#ifdef USE_IPV6
861 struct in6_addr addr;
862#else
863 struct in_addr addr;
864#endif
865 if(curlx_inet_pton(AF_INET, hostname, &addr))
866 return CURL_SSL_PEER_IPV4;
867#ifdef USE_IPV6
868 else if(curlx_inet_pton(AF_INET6, hostname, &addr)) {
869 return CURL_SSL_PEER_IPV6;
870 }
871#endif
872 }
873 return CURL_SSL_PEER_DNS;
876CURLcode Curl_ssl_peer_init(struct ssl_peer *ssl_peer,
877 struct Curl_peer *origin,
878 struct Curl_peer *peer,
879 struct ssl_filter_config *sslc,
880 const char *tls_id,
881 uint8_t transport)
883 CURLcode result = CURLE_OUT_OF_MEMORY;
885 /* We expect a clean struct, e.g. called only ONCE */
886 if(!ssl_peer || !origin) {
887 DEBUGASSERT(0);
888 return CURLE_FAILED_INIT;
889 }
890 DEBUGASSERT(!ssl_peer->origin);
891 DEBUGASSERT(!ssl_peer->peer);
892 DEBUGASSERT(!ssl_peer->sni);
893 ssl_peer->transport = transport;
895 Curl_peer_link(&ssl_peer->origin, origin);
896 Curl_peer_link(&ssl_peer->peer, peer);
897 ssl_peer->type = get_peer_type(origin->hostname);
898 if(ssl_peer->type == CURL_SSL_PEER_DNS) {
899 /* not an IP address, normalize according to RCC 6066 ch. 3,
900 * max len of SNI is 2^16-1, no trailing dot */
901 size_t len = strlen(origin->hostname);
902 if(len && (origin->hostname[len - 1] == '.'))
903 len--;
904 if(len < USHRT_MAX) {
905 ssl_peer->sni = curlx_calloc(1, len + 1);
906 if(!ssl_peer->sni)
907 goto out;
908 Curl_strntolower(ssl_peer->sni, origin->hostname, len);
909 ssl_peer->sni[len] = 0;
910 }
911 }
913 result = Curl_ssl_peer_key_make(ssl_peer, sslc, tls_id,
914 &ssl_peer->scache_key);
916out:
917 if(result)
918 Curl_ssl_peer_cleanup(ssl_peer);
919 return result;
922static void ssl_cf_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
924 struct ssl_connect_data *connssl = cf->ctx;
925 if(connssl) {
926 connssl->ssl_impl->close(cf, data);
927 connssl->state = ssl_connection_none;
928 connssl->connecting_state = ssl_connect_1;
929 connssl->prefs_checked = FALSE;
930 Curl_ssl_peer_cleanup(&connssl->peer);
931 Curl_ssl_session_destroy(connssl->session);
932 cf_ctx_free(connssl);
933 cf->ctx = NULL;
934 }
935 cf->connected = FALSE;
938static CURLcode ssl_cf_connect(struct Curl_cfilter *cf,
939 struct Curl_easy *data,
940 bool *done)
942 struct ssl_connect_data *connssl = cf->ctx;
943 struct cf_call_data save;
944 CURLcode result;
946 if(cf->connected && (connssl->state != ssl_connection_deferred)) {
947 *done = TRUE;
948 return CURLE_OK;
949 }
951 if(!cf->next || !connssl->peer.origin) {
952 *done = FALSE;
953 return CURLE_FAILED_INIT;
954 }
956 if(!cf->next->connected) {
957 result = cf->next->cft->do_connect(cf->next, data, done);
958 if(result || !*done)
959 return result;
960 }
962 CF_DATA_SAVE(save, cf, data);
963 CURL_TRC_CF(data, cf, "cf_connect()");
964 DEBUGASSERT(connssl);
966 *done = FALSE;
968 if(!connssl->prefs_checked) {
969 if(!ssl_prefs_check(cf, data)) {
970 result = CURLE_SSL_CONNECT_ERROR;
971 goto out;
972 }
973 connssl->prefs_checked = TRUE;
974 }
976 result = connssl->ssl_impl->do_connect(cf, data, done);
978 if(!result && *done) {
979 cf->connected = TRUE;
980 if(connssl->state == ssl_connection_complete) {
981 connssl->handshake_done = *Curl_pgrs_now(data);
982 }
983 if(Curl_tls_keylog_enabled()) {
984 infof(data, "SSLKEYLOGFILE set, all TLS secrets are logged to '%s'",
985 Curl_tls_keylog_file_name());
986#ifdef LIBRESSL_VERSION_NUMBER
987 infof(data, "Note LibreSSL only supports SSLKEYLOGFILE for TLS <= 1.2");
988#endif
989 }
990 /* Connection can be deferred when sending early data */
991 DEBUGASSERT(connssl->state == ssl_connection_complete ||
992 connssl->state == ssl_connection_deferred);
993 DEBUGASSERT(connssl->state != ssl_connection_deferred ||
994 connssl->earlydata_state > ssl_earlydata_none);
995 }
996out:
997 CURL_TRC_CF(data, cf, "cf_connect() -> %d, done=%d", (int)result, *done);
998 CF_DATA_RESTORE(cf, save);
999 return result;
1002static CURLcode ssl_cf_set_earlydata(struct Curl_cfilter *cf,
1003 struct Curl_easy *data,
1004 const void *buf, size_t blen)
1006 struct ssl_connect_data *connssl = cf->ctx;
1007 size_t nwritten = 0;
1008 CURLcode result = CURLE_OK;
1010 DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_await);
1011 DEBUGASSERT(Curl_bufq_is_empty(&connssl->earlydata));
1012 if(blen) {
1013 if(blen > connssl->earlydata_max)
1014 blen = connssl->earlydata_max;
1015 result = Curl_bufq_write(&connssl->earlydata, buf, blen, &nwritten);
1016 CURL_TRC_CF(data, cf, "ssl_cf_set_earlydata(len=%zu) -> %zu",
1017 blen, nwritten);
1018 if(result)
1019 return result;
1021 return CURLE_OK;
1024static CURLcode ssl_cf_connect_deferred(struct Curl_cfilter *cf,
1025 struct Curl_easy *data,
1026 const void *buf, size_t blen,
1027 bool *done)
1029 struct ssl_connect_data *connssl = cf->ctx;
1030 CURLcode result = CURLE_OK;
1032 DEBUGASSERT(connssl->state == ssl_connection_deferred);
1033 *done = FALSE;
1034 if(connssl->earlydata_state == ssl_earlydata_await) {
1035 result = ssl_cf_set_earlydata(cf, data, buf, blen);
1036 if(result)
1037 return result;
1038 /* we buffered any early data we would like to send. Actually
1039 * do the connect now which sends it and performs the handshake. */
1040 connssl->earlydata_state = ssl_earlydata_sending;
1041 connssl->earlydata_skip = Curl_bufq_len(&connssl->earlydata);
1044 result = ssl_cf_connect(cf, data, done);
1046 if(!result && *done) {
1047 if(!connssl->stats_reported && (cf->cft == &Curl_cft_ssl)) {
1048 Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done);
1049 connssl->stats_reported = TRUE;
1051 switch(connssl->earlydata_state) {
1052 case ssl_earlydata_none:
1053 break;
1054 case ssl_earlydata_accepted:
1055 if(!Curl_ssl_cf_is_proxy(cf))
1056 Curl_pgrsEarlyData(data, (curl_off_t)connssl->earlydata_skip);
1057 infof(data, "Server accepted %zu bytes of TLS early data.",
1058 connssl->earlydata_skip);
1059 break;
1060 case ssl_earlydata_rejected:
1061 if(!Curl_ssl_cf_is_proxy(cf))
1062 Curl_pgrsEarlyData(data, -(curl_off_t)connssl->earlydata_skip);
1063 infof(data, "Server rejected TLS early data.");
1064 connssl->earlydata_skip = 0;
1065 break;
1066 default:
1067 /* This should not happen. Either we do not use early data or we
1068 * should know if it was accepted or not. */
1069 DEBUGASSERT(NULL);
1070 break;
1073 return result;
1076static bool ssl_cf_data_pending(struct Curl_cfilter *cf,
1077 const struct Curl_easy *data)
1079 struct ssl_connect_data *connssl = cf->ctx;
1080 struct cf_call_data save;
1081 bool pending;
1083 CF_DATA_SAVE(save, cf, data);
1084 if(connssl->ssl_impl->data_pending &&
1085 connssl->ssl_impl->data_pending(cf, data))
1086 pending = TRUE;
1087 else
1088 pending = cf->next->cft->has_data_pending(cf->next, data);
1089 CF_DATA_RESTORE(cf, save);
1090 return pending;
1093static CURLcode ssl_cf_send(struct Curl_cfilter *cf,
1094 struct Curl_easy *data,
1095 const uint8_t *buf, size_t blen,
1096 bool eos, size_t *pnwritten)
1098 struct ssl_connect_data *connssl = cf->ctx;
1099 struct cf_call_data save;
1100 CURLcode result = CURLE_OK;
1102 (void)eos;
1103 *pnwritten = 0;
1104 CF_DATA_SAVE(save, cf, data);
1106 if(connssl->state == ssl_connection_deferred) {
1107 bool done = FALSE;
1108 result = ssl_cf_connect_deferred(cf, data, buf, blen, &done);
1109 if(result)
1110 goto out;
1111 else if(!done) {
1112 result = CURLE_AGAIN;
1113 goto out;
1115 DEBUGASSERT(connssl->state == ssl_connection_complete);
1118 if(connssl->earlydata_skip) {
1119 if(connssl->earlydata_skip >= blen) {
1120 connssl->earlydata_skip -= blen;
1121 result = CURLE_OK;
1122 *pnwritten = blen;
1123 goto out;
1125 else {
1126 *pnwritten = connssl->earlydata_skip;
1127 buf = buf + connssl->earlydata_skip;
1128 blen -= connssl->earlydata_skip;
1129 connssl->earlydata_skip = 0;
1133 /* OpenSSL and maybe other TLS libs do not like 0-length writes. Skip. */
1134 if(blen > 0) {
1135 size_t nwritten;
1136 result = connssl->ssl_impl->send_plain(cf, data, buf, blen, &nwritten);
1137 if(!result)
1138 *pnwritten += nwritten;
1141out:
1142 CF_DATA_RESTORE(cf, save);
1143 return result;
1146static CURLcode ssl_cf_recv(struct Curl_cfilter *cf,
1147 struct Curl_easy *data, char *buf, size_t len,
1148 size_t *pnread)
1150 struct ssl_connect_data *connssl = cf->ctx;
1151 struct cf_call_data save;
1152 CURLcode result = CURLE_OK;
1154 CF_DATA_SAVE(save, cf, data);
1155 *pnread = 0;
1156 if(connssl->state == ssl_connection_deferred) {
1157 bool done = FALSE;
1158 result = ssl_cf_connect_deferred(cf, data, NULL, 0, &done);
1159 if(result)
1160 goto out;
1161 else if(!done) {
1162 result = CURLE_AGAIN;
1163 goto out;
1165 DEBUGASSERT(connssl->state == ssl_connection_complete);
1168 result = connssl->ssl_impl->recv_plain(cf, data, buf, len, pnread);
1170out:
1171 CF_DATA_RESTORE(cf, save);
1172 return result;
1175static CURLcode ssl_cf_shutdown(struct Curl_cfilter *cf,
1176 struct Curl_easy *data,
1177 bool *done)
1179 struct ssl_connect_data *connssl = cf->ctx;
1180 CURLcode result = CURLE_OK;
1182 *done = TRUE;
1183 /* If we have done the SSL handshake, shut down the connection cleanly */
1184 if(cf->connected && (connssl->state == ssl_connection_complete) &&
1185 !cf->shutdown && Curl_ssl->shut_down) {
1186 struct cf_call_data save;
1188 CF_DATA_SAVE(save, cf, data);
1189 result = connssl->ssl_impl->shut_down(cf, data, TRUE, done);
1190 CURL_TRC_CF(data, cf, "cf_shutdown -> %d, done=%d", (int)result, *done);
1191 CF_DATA_RESTORE(cf, save);
1192 cf->shutdown = (result || *done);
1194 return result;
1197static CURLcode ssl_cf_adjust_pollset(struct Curl_cfilter *cf,
1198 struct Curl_easy *data,
1199 struct easy_pollset *ps)
1201 struct ssl_connect_data *connssl = cf->ctx;
1202 struct cf_call_data save;
1203 CURLcode result;
1205 CF_DATA_SAVE(save, cf, data);
1206 result = connssl->ssl_impl->adjust_pollset(cf, data, ps);
1207 CF_DATA_RESTORE(cf, save);
1208 return result;
1211static CURLcode ssl_cf_query(struct Curl_cfilter *cf,
1212 struct Curl_easy *data,
1213 int query, int *pres1, void *pres2)
1215 struct ssl_connect_data *connssl = cf->ctx;
1217 switch(query) {
1218 case CF_QUERY_SSL_INFO:
1219 case CF_QUERY_SSL_CTX_INFO:
1220 if(!Curl_ssl_cf_is_proxy(cf)) {
1221 struct curl_tlssessioninfo *info = pres2;
1222 struct cf_call_data save;
1223 CF_DATA_SAVE(save, cf, data);
1224 info->backend = Curl_ssl_backend();
1225 info->internals = connssl->ssl_impl->get_internals(
1226 cf->ctx, (query == CF_QUERY_SSL_INFO) ?
1227 CURLINFO_TLS_SSL_PTR : CURLINFO_TLS_SESSION);
1228 CF_DATA_RESTORE(cf, save);
1229 return CURLE_OK;
1231 break;
1232 case CF_QUERY_ALPN_NEGOTIATED: {
1233 const char **palpn = pres2;
1234 DEBUGASSERT(palpn);
1235 *palpn = connssl->negotiated.alpn;
1236 CURL_TRC_CF(data, cf, "query ALPN: returning '%s'", *palpn);
1237 return CURLE_OK;
1239 default:
1240 break;
1242 return cf->next ?
1243 cf->next->cft->query(cf->next, data, query, pres1, pres2) :
1244 CURLE_UNKNOWN_OPTION;
1247static CURLcode ssl_cf_cntrl(struct Curl_cfilter *cf,
1248 struct Curl_easy *data,
1249 int event, int arg1, void *arg2)
1251 struct ssl_connect_data *connssl = cf->ctx;
1253 (void)arg1;
1254 (void)arg2;
1255 (void)data;
1256 switch(event) {
1257 case CF_CTRL_CONN_INFO_UPDATE:
1258 if(connssl->negotiated.alpn && !cf->sockindex) {
1259 if(!strcmp("http/1.1", connssl->negotiated.alpn))
1260 cf->conn->httpversion_seen = 11;
1261 else if(!strcmp("h2", connssl->negotiated.alpn))
1262 cf->conn->httpversion_seen = 20;
1263 else if(!strcmp("h3", connssl->negotiated.alpn))
1264 cf->conn->httpversion_seen = 30;
1266 break;
1267 case CF_CTRL_REPORT_STATS:
1268 if(cf->connected && !connssl->stats_reported &&
1269 (cf->cft == &Curl_cft_ssl) &&
1270 (connssl->handshake_done.tv_sec || connssl->handshake_done.tv_usec)) {
1271 Curl_pgrsTimeWas(data, TIMER_APPCONNECT, connssl->handshake_done);
1272 connssl->stats_reported = TRUE;
1274 break;
1276 return CURLE_OK;
1279static bool cf_ssl_is_alive(struct Curl_cfilter *cf, struct Curl_easy *data,
1280 bool *input_pending)
1282 /*
1283 * This function tries to determine connection status.
1284 */
1285 return cf->next ?
1286 cf->next->cft->is_alive(cf->next, data, input_pending) :
1287 FALSE; /* pessimistic in absence of data */
1290struct Curl_cftype Curl_cft_ssl = {
1291 "SSL",
1292 CF_TYPE_SSL,
1293 CURL_LOG_LVL_NONE,
1294 ssl_cf_destroy,
1295 ssl_cf_connect,
1296 ssl_cf_shutdown,
1297 ssl_cf_adjust_pollset,
1298 ssl_cf_data_pending,
1299 ssl_cf_send,
1300 ssl_cf_recv,
1301 ssl_cf_cntrl,
1302 cf_ssl_is_alive,
1303 Curl_cf_def_conn_keep_alive,
1304 ssl_cf_query,
1307#ifndef CURL_DISABLE_PROXY
1309struct Curl_cftype Curl_cft_ssl_proxy = {
1310 "SSL-PROXY",
1311 CF_TYPE_SSL | CF_TYPE_PROXY,
1312 CURL_LOG_LVL_NONE,
1313 ssl_cf_destroy,
1314 ssl_cf_connect,
1315 ssl_cf_shutdown,
1316 ssl_cf_adjust_pollset,
1317 ssl_cf_data_pending,
1318 ssl_cf_send,
1319 ssl_cf_recv,
1320 Curl_cf_def_cntrl,
1321 cf_ssl_is_alive,
1322 Curl_cf_def_conn_keep_alive,
1323 ssl_cf_query,
1326#endif /* !CURL_DISABLE_PROXY */
1328static CURLcode cf_ssl_create(struct Curl_cfilter **pcf,
1329 struct Curl_easy *data,
1330 struct connectdata *conn)
1332 struct Curl_cfilter *cf = NULL;
1333 struct ssl_connect_data *ctx;
1334 CURLcode result;
1336 DEBUGASSERT(data->conn);
1338#ifdef CURL_DISABLE_HTTP
1339 (void)conn;
1340 /* We only support ALPN for HTTP so far. */
1341 DEBUGASSERT(!conn->bits.tls_enable_alpn);
1342 ctx = cf_ctx_new(data, NULL);
1343#else
1344 ctx = cf_ctx_new(data, alpn_get_spec(data->state.http_neg.wanted,
1345 data->state.http_neg.preferred,
1346 (bool)data->state.http_neg.only_10,
1347 (bool)conn->bits.tls_enable_alpn));
1348#endif
1349 if(!ctx) {
1350 result = CURLE_OUT_OF_MEMORY;
1351 goto out;
1354 result = Curl_cf_create(&cf, &Curl_cft_ssl, ctx);
1356out:
1357 if(result)
1358 cf_ctx_free(ctx);
1359 *pcf = result ? NULL : cf;
1360 return result;
1363static CURLcode cf_ssl_peer_init(struct Curl_cfilter *cf,
1364 struct Curl_peer *origin,
1365 struct Curl_peer *peer,
1366 struct ssl_filter_config *sslc)
1368 struct ssl_connect_data *connssl = cf->ctx;
1369 char tls_id[80];
1370 connssl->ssl_impl->version(tls_id, sizeof(tls_id) - 1);
1371 return Curl_ssl_peer_init(&connssl->peer, origin, peer, sslc,
1372 tls_id, TRNSPRT_TCP);
1375CURLcode Curl_ssl_cfilter_add(struct Curl_easy *data,
1376 struct Curl_peer *origin,
1377 struct connectdata *conn,
1378 int8_t sockindex)
1380 struct Curl_cfilter *cf;
1381 struct Curl_peer *peer = (sockindex == SECONDARYSOCKET) ?
1382 conn->via_peer2 : conn->via_peer;
1383 CURLcode result;
1385 result = cf_ssl_create(&cf, data, conn);
1386 if(!result)
1387 result = cf_ssl_peer_init(cf, origin, peer, &conn->ssl_config);
1388 if(!result)
1389 Curl_conn_cf_add(data, conn, sockindex, cf);
1390 else if(cf)
1391 Curl_conn_cf_discard_chain(&cf, data);
1392 return result;
1395CURLcode Curl_cf_ssl_insert_after(struct Curl_cfilter *cf_at,
1396 struct Curl_easy *data,
1397 struct Curl_peer *origin,
1398 struct Curl_peer *peer)
1400 struct Curl_cfilter *cf;
1401 CURLcode result;
1403 result = cf_ssl_create(&cf, data, cf_at->conn);
1404 if(!result)
1405 result = cf_ssl_peer_init(cf, origin, peer, &cf_at->conn->ssl_config);
1406 if(!result) {
1407 Curl_conn_cf_insert_after(cf_at, cf);
1408#if defined(USE_HTTPSRR) && defined(USE_ECH)
1409 /* When using ECH, kick off the HTTPS-RR resolve */
1410 if((origin->scheme->family == CURLPROTO_HTTP) &&
1411 Curl_ssl_need_httpsrr(cf)) {
1412 result = Curl_conn_dns_add_https_resolve(data, cf->conn, cf->sockindex,
1413 origin);
1415#endif /* USE_HTTPSRR && USE_ECH */
1417 else if(cf)
1418 Curl_conn_cf_discard_chain(&cf, data);
1419 return result;
1422#ifndef CURL_DISABLE_PROXY
1424static CURLcode cf_ssl_proxy_create(struct Curl_cfilter **pcf,
1425 struct Curl_easy *data,
1426 struct connectdata *conn)
1428 struct Curl_cfilter *cf = NULL;
1429 struct ssl_connect_data *ctx;
1430 CURLcode result;
1431 /* ALPN is default, but if user explicitly disables it, obey */
1432 bool use_alpn = (bool)data->set.ssl_enable_alpn;
1433 http_majors wanted = CURL_HTTP_V1x;
1435 (void)conn;
1436#ifdef USE_HTTP2
1437 if(conn->http_proxy.proxytype == CURLPROXY_HTTPS2) {
1438 use_alpn = TRUE;
1439 wanted = (CURL_HTTP_V1x | CURL_HTTP_V2x);
1441#endif
1443 ctx = cf_ctx_new(data, alpn_get_spec(wanted, 0, FALSE, use_alpn));
1444 if(!ctx) {
1445 result = CURLE_OUT_OF_MEMORY;
1446 goto out;
1448 result = Curl_cf_create(&cf, &Curl_cft_ssl_proxy, ctx);
1450out:
1451 if(result)
1452 cf_ctx_free(ctx);
1453 *pcf = result ? NULL : cf;
1454 return result;
1457CURLcode Curl_cf_ssl_proxy_insert_after(struct Curl_cfilter *cf_at,
1458 struct Curl_easy *data,
1459 struct Curl_peer *peer)
1461 struct Curl_cfilter *cf;
1462 CURLcode result;
1464 result = cf_ssl_proxy_create(&cf, data, cf_at->conn);
1465 if(!result)
1466 result = cf_ssl_peer_init(cf, peer, NULL, &cf_at->conn->proxy_ssl_config);
1467 if(!result)
1468 Curl_conn_cf_insert_after(cf_at, cf);
1469 else if(cf)
1470 Curl_conn_cf_discard_chain(&cf, data);
1471 return result;
1474#endif /* !CURL_DISABLE_PROXY */
1476bool Curl_ssl_supports(struct Curl_easy *data, unsigned int ssl_option)
1478 (void)data;
1479 return (Curl_ssl->supports & ssl_option);
1482#ifdef USE_ECH
1483bool Curl_ssl_ech_enabled(struct Curl_cfilter *cf)
1485 if(Curl_ssl->supports & SSLSUPP_ECH) {
1486 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
1487 return !!conn_config->ech;
1489 return FALSE;
1492bool Curl_ssl_need_httpsrr(struct Curl_cfilter *cf)
1494 if(Curl_ssl->supports & SSLSUPP_ECH) {
1495 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
1496 if(!conn_config->ech)
1497 return FALSE;
1498 if((conn_config->ech == CURLECH_GREASE) || conn_config->ech_config)
1499 return FALSE;
1500 return TRUE;
1502 return FALSE;
1504#endif
1506static CURLcode vtls_shutdown_blocking(struct Curl_cfilter *cf,
1507 struct Curl_easy *data,
1508 bool send_shutdown, bool *done)
1510 struct ssl_connect_data *connssl = cf->ctx;
1511 struct cf_call_data save;
1512 CURLcode result = CURLE_OK;
1513 timediff_t timeout_ms;
1514 int what, loop = 10;
1516 if(cf->shutdown) {
1517 *done = TRUE;
1518 return CURLE_OK;
1520 CF_DATA_SAVE(save, cf, data);
1522 *done = FALSE;
1523 while(!result && !*done && loop--) {
1524 timeout_ms = Curl_cshutdn_timeleft_ms(data, cf->conn, cf->sockindex);
1526 if(timeout_ms < 0) {
1527 /* no need to continue if time is already up */
1528 failf(data, "SSL shutdown timeout");
1529 result = CURLE_OPERATION_TIMEDOUT;
1530 goto out;
1533 result = connssl->ssl_impl->shut_down(cf, data, send_shutdown, done);
1534 if(result || *done)
1535 goto out;
1537 if(connssl->io_need) {
1538 what = Curl_conn_cf_poll(cf, data, timeout_ms);
1539 if(what < 0) {
1540 /* fatal error */
1541 failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
1542 result = CURLE_RECV_ERROR;
1543 goto out;
1545 else if(what == 0) {
1546 /* timeout */
1547 failf(data, "SSL shutdown timeout");
1548 result = CURLE_OPERATION_TIMEDOUT;
1549 goto out;
1551 /* socket is readable or writable */
1554out:
1555 CF_DATA_RESTORE(cf, save);
1556 cf->shutdown = (result || *done);
1557 return result;
1560CURLcode Curl_ssl_cfilter_remove(struct Curl_easy *data,
1561 int8_t sockindex, bool send_shutdown)
1563 struct Curl_cfilter *cf, *head;
1564 CURLcode result = CURLE_OK;
1566 head = data->conn ? data->conn->cfilter[sockindex] : NULL;
1567 for(cf = head; cf; cf = cf->next) {
1568 if(cf->cft == &Curl_cft_ssl) {
1569 bool done;
1570 CURL_TRC_CF(data, cf, "shutdown and remove SSL, start");
1571 Curl_cshutdn_start_timer(data, sockindex, 0);
1572 result = vtls_shutdown_blocking(cf, data, send_shutdown, &done);
1573 Curl_cshutdn_clear_timer(data, sockindex);
1574 if(!result && !done) /* blocking failed? */
1575 result = CURLE_SSL_SHUTDOWN_FAILED;
1576 Curl_conn_cf_discard(&cf, data);
1577 CURL_TRC_CF(data, cf, "shutdown and remove SSL, done -> %d",
1578 (int)result);
1579 break;
1582 return result;
1585bool Curl_ssl_cf_is_proxy(struct Curl_cfilter *cf)
1587 return (cf->cft->flags & CF_TYPE_SSL) && (cf->cft->flags & CF_TYPE_PROXY);
1590struct ssl_easy_config *
1591Curl_ssl_cf_get_easy_config(struct Curl_cfilter *cf,
1592 struct Curl_easy *data)
1594#ifdef CURL_DISABLE_PROXY
1595 (void)cf;
1596 return &data->set.ssl;
1597#else
1598 return Curl_ssl_cf_is_proxy(cf) ? &data->set.proxy_ssl : &data->set.ssl;
1599#endif
1602struct ssl_filter_config *
1603Curl_ssl_cf_get_filter_config(struct Curl_cfilter *cf)
1605#ifdef CURL_DISABLE_PROXY
1606 return &cf->conn->ssl_config;
1607#else
1608 return Curl_ssl_cf_is_proxy(cf) ?
1609 &cf->conn->proxy_ssl_config : &cf->conn->ssl_config;
1610#endif
1613CURLcode Curl_alpn_to_proto_buf(struct alpn_proto_buf *buf,
1614 const struct alpn_spec *spec)
1616 size_t i, len;
1617 int off = 0;
1618 unsigned char blen;
1620 memset(buf, 0, sizeof(*buf));
1621 for(i = 0; spec && i < spec->count; ++i) {
1622 len = strlen(spec->entries[i]);
1623 if(len >= ALPN_NAME_MAX)
1624 return CURLE_FAILED_INIT;
1625 blen = (unsigned char)len;
1626 if(off + blen + 1 >= (int)sizeof(buf->data))
1627 return CURLE_FAILED_INIT;
1628 buf->data[off++] = blen;
1629 memcpy(buf->data + off, spec->entries[i], blen);
1630 off += blen;
1632 buf->len = off;
1633 return CURLE_OK;
1636CURLcode Curl_alpn_to_proto_str(struct alpn_proto_buf *buf,
1637 const struct alpn_spec *spec)
1639 size_t i, len;
1640 size_t off = 0;
1642 memset(buf, 0, sizeof(*buf));
1643 for(i = 0; spec && i < spec->count; ++i) {
1644 len = strlen(spec->entries[i]);
1645 if(len >= ALPN_NAME_MAX)
1646 return CURLE_FAILED_INIT;
1647 if(off + len + 2 >= sizeof(buf->data))
1648 return CURLE_FAILED_INIT;
1649 if(off)
1650 buf->data[off++] = ',';
1651 memcpy(buf->data + off, spec->entries[i], len);
1652 off += len;
1654 buf->data[off] = '\0';
1655 buf->len = (int)off;
1656 return CURLE_OK;
1659bool Curl_alpn_contains_proto(const struct alpn_spec *spec,
1660 const char *proto)
1662 size_t i, plen = proto ? strlen(proto) : 0;
1663 for(i = 0; spec && plen && i < spec->count; ++i) {
1664 size_t slen = strlen(spec->entries[i]);
1665 if((slen == plen) && !memcmp(proto, spec->entries[i], plen))
1666 return TRUE;
1668 return FALSE;
1671void Curl_alpn_restrict_to(struct alpn_spec *spec, const char *proto)
1673 size_t plen = strlen(proto);
1674 DEBUGASSERT(plen < sizeof(spec->entries[0]));
1675 if(plen < sizeof(spec->entries[0])) {
1676 memcpy(spec->entries[0], proto, plen + 1);
1677 spec->count = 1;
1681void Curl_alpn_copy(struct alpn_spec *dest, const struct alpn_spec *src)
1683 if(src)
1684 memcpy(dest, src, sizeof(*dest));
1685 else
1686 memset(dest, 0, sizeof(*dest));
1689CURLcode Curl_alpn_set_negotiated(struct Curl_cfilter *cf,
1690 struct Curl_easy *data,
1691 struct ssl_connect_data *connssl,
1692 const unsigned char *proto,
1693 size_t proto_len)
1695 CURLcode result = CURLE_OK;
1696 (void)cf;
1698 if(connssl->negotiated.alpn) {
1699 /* When we ask for a specific ALPN protocol, we need the confirmation
1700 * of it by the server, as we have installed protocol handler and
1701 * connection filter chain for exactly this protocol. */
1702 if(!proto_len) {
1703 failf(data, "ALPN: asked for '%s' from previous session, "
1704 "but server did not confirm it. Refusing to continue.",
1705 connssl->negotiated.alpn);
1706 result = CURLE_SSL_CONNECT_ERROR;
1707 goto out;
1709 else if(!proto) {
1710 DEBUGASSERT(0); /* with length, we need a pointer */
1711 result = CURLE_SSL_CONNECT_ERROR;
1712 goto out;
1714 else if((strlen(connssl->negotiated.alpn) != proto_len) ||
1715 memcmp(connssl->negotiated.alpn, proto, proto_len)) {
1716 failf(data, "ALPN: asked for '%s' from previous session, but server "
1717 "selected '%.*s'. Refusing to continue.",
1718 connssl->negotiated.alpn, (int)proto_len, proto);
1719 result = CURLE_SSL_CONNECT_ERROR;
1720 goto out;
1722 /* ALPN is exactly what we asked for, done. */
1723 infof(data, "ALPN: server confirmed to use '%s'",
1724 connssl->negotiated.alpn);
1725 goto out;
1728 if(proto && proto_len) {
1729 if(memchr(proto, '\0', proto_len)) {
1730 failf(data, "ALPN: server selected protocol contains NUL. "
1731 "Refusing to continue.");
1732 result = CURLE_SSL_CONNECT_ERROR;
1733 goto out;
1735 connssl->negotiated.alpn = curlx_memdup0((const char *)proto, proto_len);
1736 if(!connssl->negotiated.alpn)
1737 return CURLE_OUT_OF_MEMORY;
1740 if(proto && proto_len) {
1741 if(connssl->state == ssl_connection_deferred)
1742 infof(data, VTLS_INFOF_ALPN_DEFERRED, (int)proto_len, proto);
1743 else
1744 infof(data, VTLS_INFOF_ALPN_ACCEPTED, (int)proto_len, proto);
1746 else {
1747 if(connssl->state == ssl_connection_deferred)
1748 infof(data, VTLS_INFOF_NO_ALPN_DEFERRED);
1749 else
1750 infof(data, VTLS_INFOF_NO_ALPN);
1753out:
1754 return result;
1757CURLcode Curl_on_session_reuse(struct Curl_cfilter *cf,
1758 struct Curl_easy *data,
1759 struct alpn_spec *alpns,
1760 struct Curl_ssl_session *scs,
1761 bool *do_early_data, bool early_data_allowed)
1763 struct ssl_connect_data *connssl = cf->ctx;
1764 CURLcode result = CURLE_OK;
1766 *do_early_data = FALSE;
1768 if(!early_data_allowed) {
1769 CURL_TRC_CF(data, cf, "SSL session does not allow earlydata");
1771 else if(!Curl_alpn_contains_proto(alpns, scs->alpn)) {
1772 CURL_TRC_CF(data, cf, "SSL session has different ALPN, no early data");
1774 else {
1775 infof(data, "SSL session allows %zu bytes of early data, "
1776 "reusing ALPN '%s'", connssl->earlydata_max, scs->alpn);
1777 connssl->earlydata_state = ssl_earlydata_await;
1778 connssl->state = ssl_connection_deferred;
1779 result = Curl_alpn_set_negotiated(cf, data, connssl,
1780 (const unsigned char *)scs->alpn,
1781 scs->alpn ? strlen(scs->alpn) : 0);
1782 *do_early_data = !result;
1784 return result;
1787struct Curl_ssl_session *Curl_ssl_get_cf_session(struct Curl_easy *data,
1788 const struct Curl_cftype *cft,
1789 int8_t sockindex)
1791 if(data->conn &&
1792#ifndef CURL_DISABLE_PROXY
1793 ((cft == &Curl_cft_ssl) || (cft == &Curl_cft_ssl_proxy))) {
1794#else
1795 (cft == &Curl_cft_ssl)) {
1796#endif
1797 struct Curl_cfilter *cf1 = data->conn->cfilter[sockindex];
1798 for(; cf1; cf1 = cf1->next) {
1799 /* A tunneling proxy does not offer end2end encryption, even if
1800 * it does SSL itself (e.g. QUIC H3 proxy) */
1801 if(cf1->cft == cft)
1802 break;
1804 if(cf1) {
1805 struct ssl_connect_data *connssl = cf1->ctx;
1806 if(connssl)
1807 return connssl->session;
1810 return NULL;
1813#endif /* USE_SSL */
Diagnostics4 git calls · 37 ms
CallStatusSizeTime037 ms
discover200152 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch20014 KiB · hit32 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=31
git.response.bytes=14183
git.response.cache=HIT
git.response.duration_ms=32
git.fetch.wants=568882fdf3682ae4b7be0424120eef0d2c698987
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=14183
git.pkt.payload.max=14162
git.sideband.band1.bytes=14161
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e352b96f63224d
Request
f1b61f42…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format