Artifactspublic-v0

curl

master
ply4 git calls76 ms307 KiB3 cachedprotocol v2
2324 lines71 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 ***************************************************************************/
24/*
25 * Source file for all wolfSSL specific code for the TLS/SSL layer. No code
26 * but vtls.c should ever call or use these functions.
27 *
28 */
29#include "curl_setup.h"
31#ifdef USE_WOLFSSL
33#include <wolfssl/options.h>
34#include <wolfssl/version.h>
36#if LIBWOLFSSL_VERSION_HEX < 0x05000000 /* wolfSSL 5.0.0 (2021-11-01) */
37#error "wolfSSL 5.0.0 or greater required"
38#endif
39#if defined(OPENSSL_COEXIST) && LIBWOLFSSL_VERSION_HEX < 0x05007006
40#error "wolfSSL 5.7.6 or greater required to coexist with OpenSSL"
41#endif
43/* To determine what functions are available we rely on one or both of:
44 - the user's options.h generated by wolfSSL
45 - the symbols detected by curl's configure
46 Since they are markedly different from one another, and one or the other may
47 not be available, we do some checking below to bring things in sync. */
49/* HAVE_ALPN is wolfSSL's build time symbol for enabling ALPN in options.h. */
50#ifndef HAVE_ALPN
51#ifdef HAVE_WOLFSSL_USEALPN
52#define HAVE_ALPN
53#endif
54#endif
56#include "urldata.h"
57#include "curl_trc.h"
58#include "vdns/cf-dns.h"
59#include "vdns/httpsrr.h"
60#include "vtls/vtls.h"
61#include "vtls/vtls_int.h"
62#include "vtls/vtls_scache.h"
63#include "vtls/keylog.h"
64#include "connect.h" /* for the connect timeout */
65#include "progress.h"
66#include "curlx/strdup.h"
67#include "curlx/strcopy.h"
68#ifdef USE_ECH
69#include "curlx/base64.h"
70#endif
72#include <wolfssl/ssl.h>
73#include <wolfssl/error-ssl.h>
75#include "vtls/wolfssl.h"
77/* KEEP_PEER_CERT is a product of the presence of build time symbol
78 OPENSSL_EXTRA without NO_CERTS, depending on the version. KEEP_PEER_CERT is
79 in wolfSSL's settings.h, and the latter two are build time symbols in
80 options.h. */
81#ifndef KEEP_PEER_CERT
82#if defined(HAVE_WOLFSSL_GET_PEER_CERTIFICATE) || \
83 (defined(OPENSSL_EXTRA) && !defined(NO_CERTS))
84#define KEEP_PEER_CERT
85#endif
86#endif
88#ifdef HAVE_WOLFSSL_BIO_NEW
89#define USE_BIO_CHAIN
90#ifdef HAVE_WOLFSSL_BIO_SET_SHUTDOWN
91#define USE_FULL_BIO
92#else /* HAVE_WOLFSSL_BIO_SET_SHUTDOWN */
93#undef USE_FULL_BIO
94#endif
95/* wolfSSL 5.7.4 and older do not have these symbols, but only the
96 * OpenSSL ones. */
97#ifndef WOLFSSL_BIO_CTRL_GET_CLOSE
98#define WOLFSSL_BIO_CTRL_GET_CLOSE BIO_CTRL_GET_CLOSE
99#define WOLFSSL_BIO_CTRL_SET_CLOSE BIO_CTRL_SET_CLOSE
100#define WOLFSSL_BIO_CTRL_FLUSH BIO_CTRL_FLUSH
101#define WOLFSSL_BIO_CTRL_DUP BIO_CTRL_DUP
102#define wolfSSL_BIO_set_retry_write BIO_set_retry_write
103#define wolfSSL_BIO_set_retry_read BIO_set_retry_read
104#endif /* !WOLFSSL_BIO_CTRL_GET_CLOSE */
106#else /* HAVE_WOLFSSL_BIO_NEW */
107#undef USE_BIO_CHAIN
108#endif
110#ifdef OPENSSL_EXTRA
111/*
112 * Availability note:
113 * The TLS 1.3 secret callback (wolfSSL_set_tls13_secret_cb) was added in
114 * wolfSSL 4.4.0, but requires the -DHAVE_SECRET_CALLBACK build option. If that
115 * option is not set, then TLS 1.3 is not logged.
116 * For TLS 1.2 and before, we use wolfSSL_get_keys().
117 * wolfSSL_get_client_random and wolfSSL_get_keys require OPENSSL_EXTRA
118 * (--enable-opensslextra or --enable-all).
119 */
120#if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
121static int wssl_tls13_secret_callback(SSL *ssl, int id,
122 const unsigned char *secret,
123 int secretSz, void *ctx)
125 const char *label;
126 unsigned char client_random[SSL3_RANDOM_SIZE];
127 (void)ctx;
129 if(!ssl || !Curl_tls_keylog_enabled()) {
130 return 0;
131 }
133 switch(id) {
134 case CLIENT_EARLY_TRAFFIC_SECRET:
135 label = "CLIENT_EARLY_TRAFFIC_SECRET";
136 break;
137 case CLIENT_HANDSHAKE_TRAFFIC_SECRET:
138 label = "CLIENT_HANDSHAKE_TRAFFIC_SECRET";
139 break;
140 case SERVER_HANDSHAKE_TRAFFIC_SECRET:
141 label = "SERVER_HANDSHAKE_TRAFFIC_SECRET";
142 break;
143 case CLIENT_TRAFFIC_SECRET:
144 label = "CLIENT_TRAFFIC_SECRET_0";
145 break;
146 case SERVER_TRAFFIC_SECRET:
147 label = "SERVER_TRAFFIC_SECRET_0";
148 break;
149 case EARLY_EXPORTER_SECRET:
150 label = "EARLY_EXPORTER_SECRET";
151 break;
152 case EXPORTER_SECRET:
153 label = "EXPORTER_SECRET";
154 break;
155 default:
156 return 0;
157 }
159 if(wolfSSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE) == 0) {
160 /* Should never happen as wolfSSL_KeepArrays() was called before. */
161 return 0;
162 }
164 Curl_tls_keylog_write(label, client_random, sizeof(client_random),
165 secret, secretSz);
166 return 0;
168#endif /* HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
170static void wssl_log_tls12_secret(WOLFSSL *ssl)
172 unsigned char *ms, *sr, *cr;
173 unsigned int msLen, srLen, crLen, i, x = 0;
175 /* wolfSSL_GetVersion is available since 3.13, we use it instead of
176 * SSL_version since the latter relies on OPENSSL_ALL (--enable-opensslall or
177 * --enable-all). Failing to perform this check could result in an unusable
178 * key log line when TLS 1.3 is actually negotiated. */
179 switch(wolfSSL_GetVersion(ssl)) {
180 case WOLFSSL_SSLV3:
181 case WOLFSSL_TLSV1:
182 case WOLFSSL_TLSV1_1:
183 case WOLFSSL_TLSV1_2:
184 break;
185 default:
186 /* TLS 1.3 does not use this mechanism, the "master secret" returned below
187 * is not directly usable. */
188 return;
189 }
191 if(wolfSSL_get_keys(ssl, &ms, &msLen, &sr, &srLen, &cr, &crLen) !=
192 WOLFSSL_SUCCESS) {
193 return;
194 }
196 /* Check for a missing master secret and skip logging. That can happen if
197 * curl rejects the server certificate and aborts the handshake.
198 */
199 for(i = 0; i < msLen; i++) {
200 x |= ms[i];
201 }
202 if(x == 0) {
203 return;
204 }
206 Curl_tls_keylog_write("CLIENT_RANDOM", cr, crLen, ms, msLen);
208#endif /* OPENSSL_EXTRA */
210static int wssl_do_file_type(const char *type)
212 if(!type || !type[0])
213 return WOLFSSL_FILETYPE_PEM;
214 if(curl_strequal(type, "PEM"))
215 return WOLFSSL_FILETYPE_PEM;
216 if(curl_strequal(type, "DER"))
217 return WOLFSSL_FILETYPE_ASN1;
218 return -1;
221#ifdef USE_BIO_CHAIN
223static int wssl_bio_cf_create(WOLFSSL_BIO *bio)
225#ifdef USE_FULL_BIO
226 wolfSSL_BIO_set_shutdown(bio, 1);
227#endif
228 wolfSSL_BIO_set_data(bio, NULL);
229 return 1;
232static long wssl_bio_cf_ctrl(WOLFSSL_BIO *bio, int cmd, long num, void *ptr)
234 struct Curl_cfilter *cf = wolfSSL_BIO_get_data(bio);
235 long ret = 1;
237 (void)cf;
238 (void)ptr;
239 (void)num;
240 switch(cmd) {
241 case WOLFSSL_BIO_CTRL_GET_CLOSE:
242#ifdef USE_FULL_BIO
243 ret = (long)wolfSSL_BIO_get_shutdown(bio);
244#else
245 ret = 0;
246#endif
247 break;
248 case WOLFSSL_BIO_CTRL_SET_CLOSE:
249#ifdef USE_FULL_BIO
250 wolfSSL_BIO_set_shutdown(bio, (int)num);
251#endif
252 break;
253 case WOLFSSL_BIO_CTRL_FLUSH:
254 /* we do no delayed writes, but if we ever would, this
255 * needs to trigger it. */
256 ret = 1;
257 break;
258 case WOLFSSL_BIO_CTRL_DUP:
259 ret = 1;
260 break;
261#ifdef WOLFSSL_BIO_CTRL_EOF
262 case WOLFSSL_BIO_CTRL_EOF: {
263 /* EOF has been reached on input? */
264 struct ssl_connect_data *connssl = cf->ctx;
265 return connssl->peer_closed;
266 }
267#endif
268 default:
269 ret = 0;
270 break;
271 }
272 return ret;
275static int wssl_bio_cf_out_write(WOLFSSL_BIO *bio, const char *buf, int blen)
277 struct Curl_cfilter *cf = wolfSSL_BIO_get_data(bio);
278 struct ssl_connect_data *connssl = cf->ctx;
279 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
280 struct Curl_easy *data = CF_DATA_CURRENT(cf);
281 size_t nwritten, skiplen = 0;
282 CURLcode result = CURLE_OK;
284 DEBUGASSERT(data);
285 if(wssl->shutting_down && wssl->io_send_blocked_len &&
286 (wssl->io_send_blocked_len < blen)) {
287 /* bug in wolfSSL: <https://github.com/wolfSSL/wolfssl/issues/7784>
288 * It adds the close notify message again every time we retry
289 * sending during shutdown. */
290 CURL_TRC_CF(data, cf, "bio_write, shutdown restrict send of %d"
291 " to %d bytes", blen, wssl->io_send_blocked_len);
292 skiplen = (size_t)(blen - wssl->io_send_blocked_len);
293 blen = wssl->io_send_blocked_len;
294 }
295 result = Curl_conn_cf_send(cf->next, data,
296 (const uint8_t *)buf, blen, FALSE, &nwritten);
297 wssl->io_result = result;
298 CURL_TRC_CF(data, cf, "bio_write(len=%d) -> %d, %zu",
299 blen, (int)result, nwritten);
300#ifdef USE_FULL_BIO
301 wolfSSL_BIO_clear_retry_flags(bio);
302#endif
303 if(result == CURLE_AGAIN) {
304 wolfSSL_BIO_set_retry_write(bio);
305 if(wssl->shutting_down && !wssl->io_send_blocked_len)
306 wssl->io_send_blocked_len = blen;
307 }
308 else if(!result && skiplen)
309 nwritten += skiplen;
310 return result ? -1 : (int)nwritten;
313static int wssl_bio_cf_in_read(WOLFSSL_BIO *bio, char *buf, int blen)
315 struct Curl_cfilter *cf = wolfSSL_BIO_get_data(bio);
316 struct ssl_connect_data *connssl = cf->ctx;
317 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
318 struct Curl_easy *data = CF_DATA_CURRENT(cf);
319 size_t nread = 0;
320 CURLcode result = CURLE_OK;
322 DEBUGASSERT(data);
323 if(!data || (blen < 0)) {
324 wssl->io_result = CURLE_FAILED_INIT;
325 return -1;
326 }
327 if(!buf || !blen)
328 return 0;
330 if((connssl->connecting_state == ssl_connect_2) &&
331 !wssl->x509_store_setup) {
332 /* During handshake, init the x509 store before receiving the
333 * server response. This allows sending of ClientHello without delay. */
334 result = Curl_wssl_setup_x509_store(cf, data, wssl);
335 if(result) {
336 CURL_TRC_CF(data, cf, "Curl_wssl_setup_x509_store() -> %d", (int)result);
337 wssl->io_result = result;
338 return -1;
339 }
340 }
342 result = Curl_conn_cf_recv(cf->next, data, buf, blen, &nread);
343 wssl->io_result = result;
344 CURL_TRC_CF(data, cf, "bio_read(len=%d) -> %d, %zu", blen, (int)result,
345 nread);
346#ifdef USE_FULL_BIO
347 wolfSSL_BIO_clear_retry_flags(bio);
348#endif
349 if(result == CURLE_AGAIN)
350 wolfSSL_BIO_set_retry_read(bio);
351 else if(nread == 0)
352 connssl->peer_closed = TRUE;
353 return result ? -1 : (int)nread;
356static WOLFSSL_BIO_METHOD *wssl_bio_cf_method = NULL;
358static int wssl_bio_cf_init_methods(void)
360 wssl_bio_cf_method = wolfSSL_BIO_meth_new(WOLFSSL_BIO_MEMORY,
361 "wolfSSL CF BIO");
362 if(!wssl_bio_cf_method)
363 return FALSE; /* error */
364 wolfSSL_BIO_meth_set_write(wssl_bio_cf_method, &wssl_bio_cf_out_write);
365 wolfSSL_BIO_meth_set_read(wssl_bio_cf_method, &wssl_bio_cf_in_read);
366 wolfSSL_BIO_meth_set_ctrl(wssl_bio_cf_method, &wssl_bio_cf_ctrl);
367 wolfSSL_BIO_meth_set_create(wssl_bio_cf_method, &wssl_bio_cf_create);
368 return TRUE; /* fine */
371static void wssl_bio_cf_free_methods(void)
373 wolfSSL_BIO_meth_free(wssl_bio_cf_method);
374 wssl_bio_cf_method = NULL;
377#else /* USE_BIO_CHAIN */
379#define wssl_bio_cf_init_methods() TRUE
380#define wssl_bio_cf_free_methods() Curl_nop_stmt
382#endif /* !USE_BIO_CHAIN */
384#ifdef HAVE_EX_DATA
385CURLcode Curl_wssl_cache_session(struct Curl_cfilter *cf,
386 struct Curl_easy *data,
387 const char *ssl_peer_key,
388 WOLFSSL_SESSION *session,
389 int ietf_tls_id,
390 const char *alpn,
391 unsigned char *quic_tp,
392 size_t quic_tp_len,
393 struct Curl_ssl_session **pscs)
395 CURLcode result = CURLE_OK;
396 struct Curl_ssl_session *sc_session = NULL, *sc_dup = NULL;
397 unsigned char *sdata = NULL, *sdata_ptr, *qtp_clone = NULL;
398 unsigned int sdata_len;
399 unsigned int earlydata_max = 0;
401 if(pscs)
402 *pscs = NULL;
403 if(!session)
404 goto out;
406 sdata_len = wolfSSL_i2d_SSL_SESSION(session, NULL);
407 if(sdata_len <= 0) {
408 CURL_TRC_CF(data, cf, "fail to assess session length: %u", sdata_len);
409 result = CURLE_FAILED_INIT;
410 goto out;
411 }
412 sdata = sdata_ptr = curlx_calloc(1, sdata_len);
413 if(!sdata) {
414 failf(data, "unable to allocate session buffer of %u bytes", sdata_len);
415 result = CURLE_OUT_OF_MEMORY;
416 goto out;
417 }
418 /* wolfSSL right now does not change the last parameter here, but it
419 * might one day decide to do so for OpenSSL compatibility. */
420 sdata_len = wolfSSL_i2d_SSL_SESSION(session, &sdata_ptr);
421 if(sdata_len <= 0) {
422 CURL_TRC_CF(data, cf, "fail to serialize session: %u", sdata_len);
423 result = CURLE_FAILED_INIT;
424 goto out;
425 }
426 if(quic_tp && quic_tp_len) {
427 qtp_clone = curlx_memdup0((const char *)quic_tp, quic_tp_len);
428 if(!qtp_clone) {
429 curlx_free(sdata);
430 return CURLE_OUT_OF_MEMORY;
431 }
432 }
433#ifdef WOLFSSL_EARLY_DATA
434 earlydata_max = wolfSSL_SESSION_get_max_early_data(session);
435#endif
437 result = Curl_ssl_session_create2(sdata, sdata_len,
438 ietf_tls_id, alpn,
439 (curl_off_t)time(NULL) +
440 wolfSSL_SESSION_get_timeout(session),
441 earlydata_max, qtp_clone, quic_tp_len,
442 &sc_session);
443 sdata = NULL; /* took ownership of sdata */
444 if(!result && pscs && /* return a duplicate if asked for and FTP */
445 (cf->conn->scheme->family == CURLPROTO_FTP))
446 result = Curl_ssl_session_dup(sc_session, &sc_dup);
447 if(!result) {
448 result = Curl_ssl_scache_put(cf, data, ssl_peer_key, sc_session);
449 /* took ownership of `sc_session` */
450 sc_session = NULL;
451 }
453out:
454 curlx_free(sdata);
455 if(!result && pscs) {
456 *pscs = sc_dup;
457 sc_dup = NULL;
458 }
459 Curl_ssl_session_destroy(sc_session);
460 Curl_ssl_session_destroy(sc_dup);
461 return result;
464static int wssl_vtls_new_session_cb(WOLFSSL *ssl, WOLFSSL_SESSION *session)
466 struct Curl_cfilter *cf;
468 cf = (struct Curl_cfilter *)wolfSSL_get_app_data(ssl);
469 DEBUGASSERT(cf);
470 if(cf && session) {
471 struct ssl_connect_data *connssl = cf->ctx;
472 struct Curl_easy *data = CF_DATA_CURRENT(cf);
473 struct Curl_ssl_session *scs = NULL;
474 DEBUGASSERT(connssl);
475 DEBUGASSERT(data);
476 if(connssl && data) {
477 (void)Curl_wssl_cache_session(cf, data, connssl->peer.scache_key,
478 session, wolfSSL_version(ssl),
479 connssl->negotiated.alpn, NULL, 0, &scs);
480 if(scs) {
481 Curl_ssl_session_destroy(connssl->session);
482 connssl->session = scs;
483 }
484 }
485 }
486 return 0;
488#endif
490static CURLcode wssl_on_session_reuse(struct Curl_cfilter *cf,
491 struct Curl_easy *data,
492 struct alpn_spec *alpns,
493 struct Curl_ssl_session *scs,
494 bool *do_early_data)
496 struct ssl_connect_data *connssl = cf->ctx;
497#ifdef WOLFSSL_EARLY_DATA
498 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
500 connssl->earlydata_max = wolfSSL_SESSION_get_max_early_data(
501 wolfSSL_get_session(wssl->ssl));
502#else
503 connssl->earlydata_max = 0;
504#endif
506 /* Seems to be no wolfSSL way to signal no EarlyData in session */
507 return Curl_on_session_reuse(cf, data, alpns, scs, do_early_data,
508 connssl->earlydata_max);
511static bool wssl_apply_session(
512 struct Curl_cfilter *cf,
513 struct Curl_easy *data,
514 struct wssl_ctx *wss,
515 struct alpn_spec *alpns,
516 Curl_wssl_init_session_reuse_cb *sess_reuse_cb,
517 struct Curl_ssl_session *scs)
519 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
520 CURLcode result = CURLE_OK;
521 WOLFSSL_SESSION *session = NULL;
522 bool success = FALSE;
524 if(scs && scs->sdata && scs->sdata_len &&
525 (!scs->alpn || Curl_alpn_contains_proto(alpns, scs->alpn))) {
526 /* wolfSSL changes the passed pointer for whatever reasons, yikes */
527 const unsigned char *sdata = scs->sdata;
528 session = wolfSSL_d2i_SSL_SESSION(NULL, &sdata, (long)scs->sdata_len);
529 if(session) {
530 int ret = wolfSSL_set_session(wss->ssl, session);
531 if(ret != WOLFSSL_SUCCESS) {
532 infof(data, "cached session not accepted (%d), "
533 "removing from cache", ret);
534 goto out;
535 }
536 else {
537 infof(data, "SSL reusing session with ALPN '%s'",
538 scs->alpn ? scs->alpn : "-");
539 success = TRUE;
540 if(conn_config->earlydata &&
541 !cf->conn->bits.connect_only &&
542 !strcmp("TLSv1.3", wolfSSL_get_version(wss->ssl))) {
543 bool do_early_data = FALSE;
544 if(sess_reuse_cb) {
545 result = sess_reuse_cb(cf, data, alpns, scs, &do_early_data);
546 if(result) {
547 goto out;
548 }
549 }
550#ifdef WOLFSSL_EARLY_DATA
551 if(do_early_data) {
552 unsigned int edmax = (scs->earlydata_max < UINT_MAX) ?
553 (unsigned int)scs->earlydata_max : UINT_MAX;
554 /* We only try the ALPN protocol the session used before,
555 * otherwise we might send early data for the wrong protocol */
556 Curl_alpn_restrict_to(alpns, scs->alpn);
557 wolfSSL_set_max_early_data(wss->ssl, edmax);
558 }
559#else
560 /* Should never enable when not supported */
561 DEBUGASSERT(!do_early_data);
562#endif
563 }
564 }
565 }
566 else {
567 failf(data, "could not decode previous session");
568 }
569 }
570out:
571 wolfSSL_SESSION_free(session);
572 return success;
575static CURLcode wssl_populate_x509_store(struct Curl_cfilter *cf,
576 struct Curl_easy *data,
577 WOLFSSL_X509_STORE *store,
578 struct wssl_ctx *wssl)
580 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
581 const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
582 const char * const ssl_cafile =
583 /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
584 (ca_info_blob ? NULL : conn_config->CAfile);
585 const char * const ssl_capath = conn_config->CApath;
586 bool imported_native_ca = FALSE;
587 bool imported_ca_info_blob = FALSE;
589#ifndef NO_FILESYSTEM
590 /* load native CA certificates */
591 if(conn_config->native_ca_store) {
592#ifdef WOLFSSL_SYS_CA_CERTS
593 if(wolfSSL_CTX_load_system_CA_certs(wssl->ssl_ctx) != WOLFSSL_SUCCESS) {
594 infof(data, "error importing native CA store, continuing anyway");
595 }
596 else {
597 imported_native_ca = TRUE;
598 infof(data, "successfully imported native CA store");
599 }
600#else
601 infof(data, "ignoring native CA option because wolfSSL was built without "
602 "native CA support");
603#endif
604 }
605#endif /* !NO_FILESYSTEM */
607 /* load certificate blob */
608 if(ca_info_blob) {
609 if(wolfSSL_CTX_load_verify_buffer(wssl->ssl_ctx, ca_info_blob->data,
610 (long)ca_info_blob->len,
611 WOLFSSL_FILETYPE_PEM) !=
612 WOLFSSL_SUCCESS) {
613 failf(data, "error importing CA certificate blob");
614 return CURLE_SSL_CACERT_BADFILE;
615 }
616 else {
617 imported_ca_info_blob = TRUE;
618 infof(data, "successfully imported CA certificate blob");
619 }
620 }
622#ifndef NO_FILESYSTEM
623 /* load trusted cacert from file if not blob */
625 CURL_TRC_CF(data, cf, "wssl_populate_x509_store, path=%s, blob=%d",
626 ssl_cafile ? ssl_cafile : "none", !!ca_info_blob);
627 if(!store)
628 return CURLE_OUT_OF_MEMORY;
630 if(ssl_cafile || ssl_capath) {
631 int rc =
632 wolfSSL_CTX_load_verify_locations_ex(wssl->ssl_ctx,
633 ssl_cafile,
634 ssl_capath,
635 WOLFSSL_LOAD_FLAG_IGNORE_ERR);
636 if(rc != WOLFSSL_SUCCESS) {
637 if(conn_config->verifypeer &&
638 !imported_native_ca && !imported_ca_info_blob) {
639 /* Fail if we insist on successfully verifying the server. */
640 failf(data, "error setting certificate verify locations:"
641 " CAfile: %s CApath: %s",
642 ssl_cafile ? ssl_cafile : "none",
643 ssl_capath ? ssl_capath : "none");
644 return CURLE_SSL_CACERT_BADFILE;
645 }
646 else {
647 /* continue with a warning if no strict certificate
648 verification is required. */
649 infof(data, "error setting certificate verify locations,"
650 " continuing anyway:");
651 }
652 }
653 else {
654 /* Everything is fine. */
655 infof(data, "successfully set certificate verify locations:");
656 }
657 infof(data, " CAfile: %s", ssl_cafile ? ssl_cafile : "none");
658 infof(data, " CApath: %s", ssl_capath ? ssl_capath : "none");
659 }
660#endif
661 (void)store;
662 return CURLE_OK;
665/* key to use at `multi->proto_hash` */
666#define MPROTO_WSSL_X509_KEY "tls:wssl:x509:share"
668struct wssl_x509_share {
669 char *CAfile; /* CAfile path used to generate X509 store */
670 WOLFSSL_X509_STORE *store; /* cached X509 store or NULL if none */
671 struct curltime time; /* when the cached store was created */
672};
674static void wssl_x509_share_free(const void *key, size_t key_len, void *p)
676 struct wssl_x509_share *share = p;
677 DEBUGASSERT(key_len == CURL_CSTRLEN(MPROTO_WSSL_X509_KEY));
678 DEBUGASSERT(!memcmp(MPROTO_WSSL_X509_KEY, key, key_len));
679 (void)key;
680 (void)key_len;
681 if(share->store) {
682 wolfSSL_X509_STORE_free(share->store);
683 }
684 curlx_free(share->CAfile);
685 curlx_free(share);
688static bool wssl_cached_x509_store_expired(struct Curl_easy *data,
689 const struct wssl_x509_share *mb)
691 timediff_t elapsed_ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &mb->time);
692 timediff_t timeout_ms = data->set.ssl_ca_cache_timeout * (timediff_t)1000;
694 if(timeout_ms < 0)
695 return FALSE;
697 return elapsed_ms >= timeout_ms;
700static bool wssl_cached_x509_store_different(struct Curl_cfilter *cf,
701 const struct wssl_x509_share *mb)
703 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
704 if(!mb->CAfile || !conn_config->CAfile)
705 return mb->CAfile != conn_config->CAfile;
707 return strcmp(mb->CAfile, conn_config->CAfile);
710static WOLFSSL_X509_STORE *wssl_get_cached_x509_store(struct Curl_cfilter *cf,
711 struct Curl_easy *data)
713 struct Curl_multi *multi = data->multi;
714 struct wssl_x509_share *share;
715 WOLFSSL_X509_STORE *store = NULL;
717 DEBUGASSERT(multi);
718 share = multi ? Curl_hash_pick(&multi->proto_hash,
719 MPROTO_WSSL_X509_KEY,
720 CURL_CSTRLEN(MPROTO_WSSL_X509_KEY)) : NULL;
721 if(share && share->store &&
722 !wssl_cached_x509_store_expired(data, share) &&
723 !wssl_cached_x509_store_different(cf, share)) {
724 store = share->store;
725 }
727 return store;
730static void wssl_set_cached_x509_store(struct Curl_cfilter *cf,
731 struct Curl_easy *data,
732 WOLFSSL_X509_STORE *store)
734 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
735 struct Curl_multi *multi = data->multi;
736 struct wssl_x509_share *share;
738 DEBUGASSERT(multi);
739 if(!multi)
740 return;
741 share = Curl_hash_pick(&multi->proto_hash,
742 MPROTO_WSSL_X509_KEY,
743 CURL_CSTRLEN(MPROTO_WSSL_X509_KEY));
745 if(!share) {
746 share = curlx_calloc(1, sizeof(*share));
747 if(!share)
748 return;
749 if(!Curl_hash_add2(&multi->proto_hash,
750 MPROTO_WSSL_X509_KEY,
751 CURL_CSTRLEN(MPROTO_WSSL_X509_KEY),
752 share, wssl_x509_share_free)) {
753 curlx_free(share);
754 return;
755 }
756 }
758 if(wolfSSL_X509_STORE_up_ref(store)) {
759 char *CAfile = NULL;
761 if(conn_config->CAfile) {
762 CAfile = curlx_strdup(conn_config->CAfile);
763 if(!CAfile) {
764 wolfSSL_X509_STORE_free(store);
765 return;
766 }
767 }
769 if(share->store) {
770 wolfSSL_X509_STORE_free(share->store);
771 curlx_free(share->CAfile);
772 }
774 share->time = *Curl_pgrs_now(data);
775 share->store = store;
776 share->CAfile = CAfile;
777 }
780CURLcode Curl_wssl_setup_x509_store(struct Curl_cfilter *cf,
781 struct Curl_easy *data,
782 struct wssl_ctx *wssl)
784 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
785 CURLcode result = CURLE_OK;
786 WOLFSSL_X509_STORE *cached_store;
787 bool cache_criteria_met;
789 /* We do not want to do this again, no matter the outcome */
790 wssl->x509_store_setup = TRUE;
792 /* Consider the X509 store cacheable if it comes exclusively from a CAfile,
793 or no source is provided and we are falling back to wolfSSL's built-in
794 default. */
795 cache_criteria_met = (data->set.ssl_ca_cache_timeout != 0) &&
796 conn_config->verifypeer &&
797 !conn_config->CApath &&
798 !conn_config->ca_info_blob &&
799 !conn_config->CRLfile &&
800 !conn_config->native_ca_store;
802 cached_store = cache_criteria_met ? wssl_get_cached_x509_store(cf, data)
803 : NULL;
804 if(cached_store &&
805 wolfSSL_CTX_get_cert_store(wssl->ssl_ctx) == cached_store) {
806 /* The cached store is already in use, do nothing. */
807 }
808 else if(cached_store && wolfSSL_X509_STORE_up_ref(cached_store)) {
809 wolfSSL_CTX_set_cert_store(wssl->ssl_ctx, cached_store);
810 }
811 else if(cache_criteria_met) {
812 /* wolfSSL's initial store in CTX is not shareable by default.
813 * Make a new one, suitable for adding to the cache. See #14278 */
814 WOLFSSL_X509_STORE *store = wolfSSL_X509_STORE_new();
815 if(!store) {
816 failf(data, "SSL: could not create a X509 store");
817 return CURLE_OUT_OF_MEMORY;
818 }
819 wolfSSL_CTX_set_cert_store(wssl->ssl_ctx, store);
821 result = wssl_populate_x509_store(cf, data, store, wssl);
822 if(!result) {
823 wssl_set_cached_x509_store(cf, data, store);
824 }
825 }
826 else {
827 /* We never share the CTX's store, use it. */
828 WOLFSSL_X509_STORE *store = wolfSSL_CTX_get_cert_store(wssl->ssl_ctx);
829 result = wssl_populate_x509_store(cf, data, store, wssl);
830 }
832 return result;
835#ifdef WOLFSSL_TLS13
836static CURLcode wssl_add_default_ciphers(bool tls13, struct dynbuf *buf)
838 int i;
839 char *str;
841 for(i = 0; (str = wolfSSL_get_cipher_list(i)) != NULL; i++) {
842 size_t n;
843 if((!strncmp(str, "TLS13", 5)) != tls13)
844 continue;
846 /* if there already is data in the string, add colon separator */
847 if(curlx_dyn_len(buf)) {
848 CURLcode result = curlx_dyn_addn(buf, ":", 1);
849 if(result)
850 return result;
851 }
853 n = strlen(str);
854 if(curlx_dyn_addn(buf, str, n))
855 return CURLE_OUT_OF_MEMORY;
856 }
858 return CURLE_OK;
860#endif
862#ifndef OPENSSL_EXTRA
863static int wssl_legacy_CTX_set_min_proto_version(WOLFSSL_CTX *ctx, int version)
865 int res;
866 switch(version) {
867 default:
868 case TLS1_VERSION:
869 res = wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1);
870 if(res == WOLFSSL_SUCCESS)
871 return res;
872 FALLTHROUGH();
873 case TLS1_1_VERSION:
874 res = wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1_1);
875 if(res == WOLFSSL_SUCCESS)
876 return res;
877 FALLTHROUGH();
878 case TLS1_2_VERSION:
879 res = wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1_2);
880#ifdef WOLFSSL_TLS13
881 if(res == WOLFSSL_SUCCESS)
882 return res;
883 FALLTHROUGH();
884 case TLS1_3_VERSION:
885 res = wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_TLSV1_3);
886#endif
887 }
888 return res;
891static int wssl_legacy_CTX_set_max_proto_version(WOLFSSL_CTX *ctx, int version)
893 (void)ctx, (void)version;
894 return WOLFSSL_NOT_IMPLEMENTED;
896#define wolfSSL_CTX_set_min_proto_version wssl_legacy_CTX_set_min_proto_version
897#define wolfSSL_CTX_set_max_proto_version wssl_legacy_CTX_set_max_proto_version
898#endif /* OPENSSL_EXTRA */
900static CURLcode wssl_client_cert(struct Curl_easy *data,
901 struct ssl_filter_config *conn_config,
902 struct wssl_ctx *wctx)
904 /* Load the client certificate, and private key */
905#ifndef NO_FILESYSTEM
906 if(conn_config->cert_blob || conn_config->clientcert) {
907 const char *cert_file = conn_config->clientcert;
908 const char *key_file = conn_config->key;
909 const struct curl_blob *cert_blob = conn_config->cert_blob;
910 const struct curl_blob *key_blob = conn_config->key_blob;
911 int file_type = wssl_do_file_type(conn_config->cert_type);
912 int rc;
914 switch(file_type) {
915 case WOLFSSL_FILETYPE_PEM:
916 rc = cert_blob ?
917 wolfSSL_CTX_use_certificate_chain_buffer(wctx->ssl_ctx,
918 cert_blob->data,
919 (long)cert_blob->len) :
920 wolfSSL_CTX_use_certificate_chain_file(wctx->ssl_ctx, cert_file);
921 break;
922 case WOLFSSL_FILETYPE_ASN1:
923 rc = cert_blob ?
924 wolfSSL_CTX_use_certificate_buffer(wctx->ssl_ctx, cert_blob->data,
925 (long)cert_blob->len, file_type) :
926 wolfSSL_CTX_use_certificate_file(wctx->ssl_ctx, cert_file, file_type);
927 break;
928 default:
929 failf(data, "unknown cert type");
930 return CURLE_BAD_FUNCTION_ARGUMENT;
931 }
932 if(rc != 1) {
933 failf(data, "unable to use client certificate");
934 return CURLE_SSL_CONNECT_ERROR;
935 }
937 if(!key_blob && !key_file) {
938 key_blob = cert_blob;
939 key_file = cert_file;
940 }
941 else
942 file_type = wssl_do_file_type(conn_config->key_type);
944 rc = key_blob ?
945 wolfSSL_CTX_use_PrivateKey_buffer(wctx->ssl_ctx, key_blob->data,
946 (long)key_blob->len, file_type) :
947 wolfSSL_CTX_use_PrivateKey_file(wctx->ssl_ctx, key_file, file_type);
948 if(rc != 1) {
949 failf(data, "unable to set private key");
950 return CURLE_SSL_CONNECT_ERROR;
951 }
952 }
953#else /* NO_FILESYSTEM */
954 if(conn_config->cert_blob) {
955 const struct curl_blob *cert_blob = conn_config->cert_blob;
956 const struct curl_blob *key_blob = conn_config->key_blob;
957 int file_type = wssl_do_file_type(conn_config->cert_type);
958 int rc;
960 switch(file_type) {
961 case WOLFSSL_FILETYPE_PEM:
962 rc = wolfSSL_CTX_use_certificate_chain_buffer(wctx->ssl_ctx,
963 cert_blob->data,
964 (long)cert_blob->len);
965 break;
966 case WOLFSSL_FILETYPE_ASN1:
967 rc = wolfSSL_CTX_use_certificate_buffer(wctx->ssl_ctx, cert_blob->data,
968 (long)cert_blob->len, file_type);
969 break;
970 default:
971 failf(data, "unknown cert type");
972 return CURLE_BAD_FUNCTION_ARGUMENT;
973 }
974 if(rc != 1) {
975 failf(data, "unable to use client certificate");
976 return CURLE_SSL_CONNECT_ERROR;
977 }
979 if(!key_blob)
980 key_blob = cert_blob;
981 else
982 file_type = wssl_do_file_type(conn_config->key_type);
984 if(wolfSSL_CTX_use_PrivateKey_buffer(wctx->ssl_ctx, key_blob->data,
985 (long)key_blob->len,
986 file_type) != 1) {
987 failf(data, "unable to set private key");
988 return CURLE_SSL_CONNECT_ERROR;
989 }
990 }
991#endif /* !NO_FILESYSTEM */
992 return CURLE_OK;
995static CURLcode ssl_version(struct Curl_easy *data,
996 struct ssl_filter_config *conn_config,
997 struct wssl_ctx *wctx,
998 int *min_version, int *max_version)
1000 int res;
1001 *min_version = *max_version = 0;
1002 DEBUGASSERT(conn_config->version != CURL_SSLVERSION_DEFAULT);
1004 switch(conn_config->version) {
1005 case CURL_SSLVERSION_TLSv1:
1006 case CURL_SSLVERSION_TLSv1_0:
1007 *min_version = TLS1_VERSION;
1008 break;
1009 case CURL_SSLVERSION_TLSv1_1:
1010 *min_version = TLS1_1_VERSION;
1011 break;
1012 case CURL_SSLVERSION_TLSv1_2:
1013 *min_version = TLS1_2_VERSION;
1014 break;
1015#ifdef WOLFSSL_TLS13
1016 case CURL_SSLVERSION_TLSv1_3:
1017 *min_version = TLS1_3_VERSION;
1018 break;
1019#endif
1020 default:
1021 failf(data, "wolfSSL: unsupported minimum TLS version value");
1022 return CURLE_SSL_CONNECT_ERROR;
1025 switch(conn_config->version_max) {
1026#ifdef WOLFSSL_TLS13
1027 case CURL_SSLVERSION_MAX_TLSv1_3:
1028 *max_version = TLS1_3_VERSION;
1029 break;
1030#endif
1031 case CURL_SSLVERSION_MAX_TLSv1_2:
1032 *max_version = TLS1_2_VERSION;
1033 break;
1034 case CURL_SSLVERSION_MAX_TLSv1_1:
1035 *max_version = TLS1_1_VERSION;
1036 break;
1037 case CURL_SSLVERSION_MAX_TLSv1_0:
1038 *max_version = TLS1_VERSION;
1039 break;
1040 case CURL_SSLVERSION_MAX_DEFAULT:
1041 case CURL_SSLVERSION_MAX_NONE:
1042 break;
1043 default:
1044 failf(data, "wolfSSL: unsupported maximum TLS version value");
1045 return CURLE_SSL_CONNECT_ERROR;
1048 res = wolfSSL_CTX_set_min_proto_version(wctx->ssl_ctx, *min_version);
1049 if(res != WOLFSSL_SUCCESS) {
1050 failf(data, "wolfSSL: failed set the minimum TLS version");
1051 return CURLE_SSL_CONNECT_ERROR;
1054 if(*max_version) {
1055 res = wolfSSL_CTX_set_max_proto_version(wctx->ssl_ctx, *max_version);
1056 if(res != WOLFSSL_SUCCESS) {
1057 failf(data, "wolfSSL: failed set the maximum TLS version");
1058 return CURLE_SSL_CONNECT_ERROR;
1061 return CURLE_OK;
1064#ifdef WOLFSSL_TLS13
1065#define MAX_CIPHER_LEN 4096
1066#endif
1068static CURLcode wssl_init_ciphers(struct Curl_easy *data,
1069 struct wssl_ctx *wctx,
1070 struct ssl_filter_config *conn_config,
1071 int tls_min, int tls_max)
1073#ifndef WOLFSSL_TLS13
1074 const char *ciphers = conn_config->cipher_list;
1075 (void)tls_min;
1076 (void)tls_max;
1077 if(ciphers) {
1078 if(!SSL_CTX_set_cipher_list(wctx->ssl_ctx, ciphers)) {
1079 failf(data, "failed setting cipher list: %s", ciphers);
1080 return CURLE_SSL_CIPHER;
1082 infof(data, "Cipher selection: %s", ciphers);
1084 return CURLE_OK;
1085#else
1086 CURLcode result = CURLE_OK;
1087 if(conn_config->cipher_list || conn_config->cipher_list13) {
1088 const char *ciphers12 = conn_config->cipher_list;
1089 const char *ciphers13 = conn_config->cipher_list13;
1090 struct dynbuf c;
1091 curlx_dyn_init(&c, MAX_CIPHER_LEN);
1093 if(!tls_max || (tls_max >= TLS1_3_VERSION)) {
1094 if(ciphers13)
1095 result = curlx_dyn_add(&c, ciphers13);
1096 else
1097 result = wssl_add_default_ciphers(TRUE, &c);
1100 if(!result && (tls_min < TLS1_3_VERSION)) {
1101 if(ciphers12) {
1102 if(curlx_dyn_len(&c))
1103 result = curlx_dyn_addn(&c, ":", 1);
1104 if(!result)
1105 result = curlx_dyn_add(&c, ciphers12);
1107 else
1108 result = wssl_add_default_ciphers(FALSE, &c);
1110 if(!result) {
1111 if(!wolfSSL_CTX_set_cipher_list(wctx->ssl_ctx, curlx_dyn_ptr(&c))) {
1112 failf(data, "failed setting cipher list: %s", curlx_dyn_ptr(&c));
1113 result = CURLE_SSL_CIPHER;
1115 else
1116 infof(data, "Cipher selection: %s", curlx_dyn_ptr(&c));
1118 curlx_dyn_free(&c);
1120 return result;
1121#endif
1124/* wolfSSL_CTX_set1_groups_list() accepts PQC/hybrid groups (e.g.
1125 X25519MLKEM768) that wolfSSL_CTX_set1_curves_list() rejects. It needs
1126 OPENSSL_EXTRA, which wolfSSL's --enable-curl sets but --enable-curl=tiny
1127 does not. */
1128#ifdef OPENSSL_EXTRA
1129#define wssl_CTX_set1_groups_list wolfSSL_CTX_set1_groups_list
1130#else
1131#define wssl_CTX_set1_groups_list wolfSSL_CTX_set1_curves_list
1132#endif
1134static CURLcode wssl_init_curves(struct Curl_easy *data,
1135 struct wssl_ctx *wctx,
1136 struct ssl_filter_config *conn_config)
1138 char *curves = conn_config->curves;
1139 /* Without an explicit list, leave the key share group selection to
1140 wolfSSL's own default. */
1141 if(curves && !wssl_CTX_set1_groups_list(wctx->ssl_ctx, curves)) {
1142 failf(data, "failed setting curves list: '%s'", curves);
1143 return CURLE_SSL_CIPHER;
1145 return CURLE_OK;
1148static CURLcode wssl_init_ssl_handle(
1149 struct wssl_ctx *wctx,
1150 struct Curl_cfilter *cf,
1151 struct Curl_easy *data,
1152 struct ssl_peer *peer,
1153 struct alpn_spec *alpns,
1154 void *ssl_user_data,
1155 unsigned char transport,
1156 Curl_wssl_init_session_reuse_cb *sess_reuse_cb)
1158 struct Curl_ssl_session *scs = NULL;
1159 bool session_applied = FALSE;
1161 /* Let's make an SSL structure */
1162 wctx->ssl = wolfSSL_new(wctx->ssl_ctx);
1163 if(!wctx->ssl) {
1164 failf(data, "SSL: could not create a handle");
1165 return CURLE_OUT_OF_MEMORY;
1168#ifdef HAVE_EX_DATA
1169 wolfSSL_set_app_data(wctx->ssl, ssl_user_data);
1170#else
1171 (void)ssl_user_data;
1172#endif
1173#ifdef WOLFSSL_QUIC
1174 if(transport == TRNSPRT_QUIC)
1175 wolfSSL_set_quic_use_legacy_codepoint(wctx->ssl, 0);
1176#else
1177 (void)transport;
1178#endif
1180 if((cf->sockindex == SECONDARYSOCKET) && !(cf->cft->flags & CF_TYPE_PROXY)) {
1181 /* FTP is a bitch. On TLS secured transfers, it is a common server
1182 * option to require the client to use the SAME TLS session as on
1183 * the control connection or it fails the request. See #22225. */
1184 scs = Curl_ssl_get_cf_session(data, cf->cft, FIRSTSOCKET);
1185 if(scs) {
1186 session_applied = wssl_apply_session(cf, data, wctx, alpns,
1187 sess_reuse_cb, scs);
1188 if(session_applied)
1189 CURL_TRC_CF(data, cf, "applied SSL session from control connection");
1190 scs = NULL;
1194 /* Check if there is a cached ID we can/should use here! */
1195 if(!session_applied && Curl_ssl_scache_use(cf, data)) {
1196 /* Set session from cache if there is one */
1197 CURLcode result = Curl_ssl_scache_take(cf, data, peer->scache_key, &scs);
1198 if(!result && scs) {
1199 if(wssl_apply_session(cf, data, wctx, alpns, sess_reuse_cb, scs))
1200 Curl_ssl_scache_return(cf, data, peer->scache_key, scs);
1201 else
1202 Curl_ssl_session_destroy(scs);
1206#ifdef HAVE_ALPN
1207 if(alpns->count) {
1208 struct alpn_proto_buf proto;
1209 memset(&proto, 0, sizeof(proto));
1210 Curl_alpn_to_proto_str(&proto, alpns);
1212 if(wolfSSL_UseALPN(wctx->ssl, (char *)proto.data,
1213 (unsigned int)proto.len,
1214 WOLFSSL_ALPN_CONTINUE_ON_MISMATCH)
1215 != WOLFSSL_SUCCESS) {
1216 failf(data, "SSL: failed setting ALPN protocols");
1217 return CURLE_SSL_CONNECT_ERROR;
1219 CURL_TRC_CF(data, cf, "set ALPN: %s", proto.data);
1221#endif /* HAVE_ALPN */
1223#ifdef OPENSSL_EXTRA
1224 if(Curl_tls_keylog_enabled()) {
1225 /* Ensure the Client Random is preserved. */
1226 wolfSSL_KeepArrays(wctx->ssl);
1227#if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
1228 wolfSSL_set_tls13_secret_cb(wctx->ssl, wssl_tls13_secret_callback, NULL);
1229#endif
1231#endif /* OPENSSL_EXTRA */
1233#ifdef HAVE_SECURE_RENEGOTIATION
1234 if(wolfSSL_UseSecureRenegotiation(wctx->ssl) != SSL_SUCCESS) {
1235 failf(data, "SSL: failed setting secure renegotiation");
1236 return CURLE_SSL_CONNECT_ERROR;
1238#endif /* HAVE_SECURE_RENEGOTIATION */
1240 return CURLE_OK;
1243#ifdef HAVE_WOLFSSL_CTX_GENERATEECHCONFIG
1244static CURLcode wssl_init_ech(struct wssl_ctx *wctx,
1245 struct Curl_cfilter *cf,
1246 struct Curl_easy *data,
1247 struct ssl_peer *peer)
1249 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
1250 int trying_ech_now = 0;
1252 if(conn_config->ech_public) {
1253 infof(data, "ECH: outername not supported with wolfSSL");
1254 return CURLE_SSL_CONNECT_ERROR;
1257 if(conn_config->ech == CURLECH_GREASE) {
1258 infof(data, "ECH: GREASE is done by default by wolfSSL: no need to ask");
1260 else if(conn_config->ech && conn_config->ech_config) {
1261 const char *b64val = conn_config->ech_config;
1262 word32 b64len = 0;
1264 b64len = (word32)strlen(b64val);
1265 if(b64len && wolfSSL_SetEchConfigsBase64(wctx->ssl, CURL_UNCONST(b64val),
1266 b64len) != WOLFSSL_SUCCESS) {
1267 if(conn_config->ech == CURLECH_HARD)
1268 return CURLE_SSL_CONNECT_ERROR;
1270 else {
1271 trying_ech_now = 1;
1272 infof(data, "ECH: ECHConfig from command line");
1275 else {
1276 const struct Curl_https_rrinfo *rinfo =
1277 Curl_conn_dns_get_https(data, cf->sockindex, peer->origin);
1279 if(Curl_httpsrr_is_for_peer(peer->origin, rinfo) && rinfo->echconfiglist) {
1280 const unsigned char *ecl = rinfo->echconfiglist;
1281 size_t elen = rinfo->echconfiglist_len;
1283 infof(data, "ECH: ECHConfig from HTTPS RR");
1284 if(wolfSSL_SetEchConfigs(wctx->ssl, ecl, (word32)elen) !=
1285 WOLFSSL_SUCCESS) {
1286 infof(data, "ECH: wolfSSL_SetEchConfigs failed");
1287 if(conn_config->ech == CURLECH_HARD) {
1288 return CURLE_SSL_CONNECT_ERROR;
1291 else {
1292 trying_ech_now = 1;
1293 infof(data, "ECH: imported ECHConfigList of length %zu", elen);
1296 else {
1297 infof(data, "ECH: requested but no ECHConfig available");
1298 if(conn_config->ech == CURLECH_HARD) {
1299 return CURLE_SSL_CONNECT_ERROR;
1304 if(trying_ech_now &&
1305 wolfSSL_set_min_proto_version(wctx->ssl, TLS1_3_VERSION) != 1) {
1306 infof(data, "ECH: cannot force TLSv1.3 [ERROR]");
1307 return CURLE_SSL_CONNECT_ERROR;
1309 return CURLE_OK;
1311#endif /* HAVE_WOLFSSL_CTX_GENERATEECHCONFIG */
1313CURLcode Curl_wssl_ctx_init(struct wssl_ctx *wctx,
1314 struct Curl_cfilter *cf,
1315 struct Curl_easy *data,
1316 struct ssl_peer *peer,
1317 const struct alpn_spec *alpns_requested,
1318 Curl_wssl_ctx_setup_cb *cb_setup,
1319 void *cb_user_data,
1320 void *ssl_user_data,
1321 Curl_wssl_init_session_reuse_cb *sess_reuse_cb)
1323 struct ssl_filter_config *conn_config;
1324 WOLFSSL_METHOD *req_method = NULL;
1325 struct alpn_spec alpns;
1326 CURLcode result = CURLE_FAILED_INIT;
1327 unsigned char transport;
1328 int tls_min, tls_max;
1330 DEBUGASSERT(!wctx->ssl_ctx);
1331 DEBUGASSERT(!wctx->ssl);
1332 conn_config = Curl_ssl_cf_get_filter_config(cf);
1333 if(!conn_config) {
1334 result = CURLE_FAILED_INIT;
1335 goto out;
1337 Curl_alpn_copy(&alpns, alpns_requested);
1338 DEBUGASSERT(cf->next);
1339 transport = Curl_conn_cf_get_transport(cf->next, data);
1341 req_method = wolfTLS_client_method();
1342 if(!req_method) {
1343 failf(data, "wolfSSL: could not create a client method");
1344 result = CURLE_OUT_OF_MEMORY;
1345 goto out;
1348 if(wctx->ssl_ctx)
1349 wolfSSL_CTX_free(wctx->ssl_ctx);
1351 wctx->ssl_ctx = wolfSSL_CTX_new(req_method);
1352 if(!wctx->ssl_ctx) {
1353 failf(data, "wolfSSL: could not create a context");
1354 result = CURLE_OUT_OF_MEMORY;
1355 goto out;
1358 result = ssl_version(data, conn_config, wctx, &tls_min, &tls_max);
1359 if(result)
1360 goto out;
1362 result = wssl_init_ciphers(data, wctx, conn_config, tls_min, tls_max);
1363 if(result)
1364 goto out;
1366 result = wssl_init_curves(data, wctx, conn_config);
1367 if(result)
1368 goto out;
1370 result = wssl_client_cert(data, conn_config, wctx);
1371 if(result)
1372 goto out;
1374 /* SSL always tries to verify the peer, this only says whether it should
1375 * fail to connect if the verification fails, or if it should continue
1376 * anyway. In the latter case the result of the verification is checked with
1377 * SSL_get_verify_result() below. */
1378 wolfSSL_CTX_set_verify(wctx->ssl_ctx, conn_config->verifypeer ?
1379 WOLFSSL_VERIFY_PEER : WOLFSSL_VERIFY_NONE, NULL);
1381#ifdef HAVE_SNI
1382 if(peer->sni) {
1383 size_t sni_len = strlen(peer->sni);
1384 if((sni_len < USHRT_MAX)) {
1385 if(wolfSSL_CTX_UseSNI(wctx->ssl_ctx, WOLFSSL_SNI_HOST_NAME,
1386 peer->sni, (unsigned short)sni_len) != 1) {
1387 failf(data, "Failed to set SNI");
1388 result = CURLE_SSL_CONNECT_ERROR;
1389 goto out;
1391 CURL_TRC_CF(data, cf, "set SNI '%s'", peer->sni);
1394#endif
1396#ifdef HAVE_EX_DATA
1397 if(Curl_ssl_scache_use(cf, data) && (transport != TRNSPRT_QUIC)) {
1398 /* Register to get notified when a new session is received */
1399 wolfSSL_CTX_sess_set_new_cb(wctx->ssl_ctx, wssl_vtls_new_session_cb);
1401#endif
1403 if(cb_setup) {
1404 result = cb_setup(cf, data, cb_user_data);
1405 if(result)
1406 goto out;
1409 /* give application a chance to interfere with SSL set up. */
1410 if(data->set.ssl_fsslctx) {
1411 if(!wctx->x509_store_setup) {
1412 result = Curl_wssl_setup_x509_store(cf, data, wctx);
1413 if(result)
1414 goto out;
1416 result = (*data->set.ssl_fsslctx)(data, wctx->ssl_ctx,
1417 data->set.ssl_fsslctxp);
1418 if(result) {
1419 failf(data, "error signaled by SSL ctx callback");
1420 goto out;
1423#ifdef NO_FILESYSTEM
1424 else if(conn_config->verifypeer) {
1425 failf(data, "SSL: Certificates cannot be loaded because wolfSSL was built"
1426 " with no file system. Either disable peer verification"
1427 " (insecure) or if you are building an application with libcurl you"
1428 " can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
1429 result = CURLE_SSL_CONNECT_ERROR;
1430 goto out;
1432#endif
1434 result = wssl_init_ssl_handle(wctx, cf, data, peer, &alpns, ssl_user_data,
1435 transport, sess_reuse_cb);
1436 if(result)
1437 goto out;
1439#ifdef HAVE_WOLFSSL_CTX_GENERATEECHCONFIG
1440 if(Curl_ssl_ech_enabled(cf)) {
1441 result = wssl_init_ech(wctx, cf, data, peer);
1442 if(result)
1443 goto out;
1445#endif /* HAVE_WOLFSSL_CTX_GENERATEECHCONFIG */
1447 result = CURLE_OK;
1449out:
1450 if(result && wctx->ssl) {
1451 wolfSSL_free(wctx->ssl);
1452 wctx->ssl = NULL;
1454 if(result && wctx->ssl_ctx) {
1455 wolfSSL_CTX_free(wctx->ssl_ctx);
1456 wctx->ssl_ctx = NULL;
1458 return result;
1462 * This function loads all the client/CA certificates and CRLs. Setup the TLS
1463 * layer and do all necessary magic.
1464 */
1465static CURLcode wssl_connect_step1(struct Curl_cfilter *cf,
1466 struct Curl_easy *data)
1468 struct ssl_connect_data *connssl = cf->ctx;
1469 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
1470 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
1471 CURLcode result;
1473 DEBUGASSERT(wssl);
1475 if(connssl->state == ssl_connection_complete)
1476 return CURLE_OK;
1478 result = Curl_wssl_ctx_init(wssl, cf, data, &connssl->peer, connssl->alpn,
1479 NULL, NULL, cf, wssl_on_session_reuse);
1480 if(result)
1481 return result;
1483#ifdef HAVE_ALPN
1484 if(connssl->alpn && (connssl->state != ssl_connection_deferred)) {
1485 struct alpn_proto_buf proto;
1486 memset(&proto, 0, sizeof(proto));
1487 Curl_alpn_to_proto_str(&proto, connssl->alpn);
1488 infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
1490#endif
1492 /* Enable RFC2818 checks on domain names. This cannot check
1493 * IP addresses which we need to do extra after the handshake. */
1494 if(conn_config->verifyhost && connssl->peer.sni) {
1495 if(wolfSSL_check_domain_name(wssl->ssl, connssl->peer.sni) !=
1496 WOLFSSL_SUCCESS) {
1497 return CURLE_SSL_CONNECT_ERROR;
1501#ifdef USE_BIO_CHAIN
1503 WOLFSSL_BIO *bio;
1505 if(!wssl_bio_cf_method)
1506 return CURLE_FAILED_INIT;
1507 bio = wolfSSL_BIO_new(wssl_bio_cf_method);
1508 if(!bio)
1509 return CURLE_OUT_OF_MEMORY;
1511 wolfSSL_BIO_set_data(bio, cf);
1512 wolfSSL_set_bio(wssl->ssl, bio, bio);
1514#else /* !USE_BIO_CHAIN */
1516 curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
1517 if(sockfd > INT_MAX) {
1518 failf(data, "SSL: socket value too large");
1519 return CURLE_SSL_CONNECT_ERROR;
1521 /* pass the raw socket into the SSL layer */
1522 if(!wolfSSL_set_fd(wssl->ssl, (int)sockfd)) {
1523 failf(data, "SSL: wolfSSL_set_fd failed");
1524 return CURLE_SSL_CONNECT_ERROR;
1527#endif /* USE_BIO_CHAIN */
1529 return CURLE_OK;
1532static char *wssl_strerror(unsigned long error, char *buf, unsigned long size)
1534 DEBUGASSERT(size > 40);
1535 *buf = '\0';
1537 wolfSSL_ERR_error_string_n(error, buf, size);
1539 if(!*buf) {
1540 const char *msg = error ? "Unknown error" : "No error";
1541 curlx_strcopy(buf, size, msg, strlen(msg));
1544 return buf;
1547CURLcode Curl_wssl_verify_pinned(struct Curl_cfilter *cf,
1548 struct Curl_easy *data,
1549 struct wssl_ctx *wssl)
1551 WOLFSSL_X509 *x509 = NULL;
1552 unsigned char *pubkey = NULL;
1553 CURLcode result = CURLE_OK;
1554#ifndef CURL_DISABLE_PROXY
1555 const char * const pinnedpubkey = Curl_ssl_cf_is_proxy(cf) ?
1556 CURL_EASY_STR(data, STRING_SSL_PINNEDPUBLICKEY_PROXY) :
1557 CURL_EASY_STR(data, STRING_SSL_PINNEDPUBLICKEY);
1558#else
1559 const char * const pinnedpubkey =
1560 CURL_EASY_STR(data, STRING_SSL_PINNEDPUBLICKEY);
1561 (void)cf;
1562#endif
1564 if(pinnedpubkey) {
1565#ifdef KEEP_PEER_CERT
1566 int pubkey_len = 0;
1568 result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
1570 x509 = wolfSSL_get_peer_certificate(wssl->ssl);
1571 if(!x509) {
1572 failf(data, "SSL: failed retrieving server certificate");
1573 goto end;
1576 /* Get the SubjectPublicKeyInfo from wolfSSL's own parse of the
1577 certificate, so the pinned key is the one the handshake used. */
1578 if(wolfSSL_X509_get_pubkey_buffer(x509, NULL, &pubkey_len) !=
1579 WOLFSSL_SUCCESS || pubkey_len <= 0) {
1580 failf(data, "SSL: failed retrieving public key from server certificate");
1581 goto end;
1583 pubkey = curlx_malloc((size_t)pubkey_len);
1584 if(!pubkey) {
1585 result = CURLE_OUT_OF_MEMORY;
1586 goto end;
1588 if(wolfSSL_X509_get_pubkey_buffer(x509, pubkey, &pubkey_len) !=
1589 WOLFSSL_SUCCESS) {
1590 failf(data, "SSL: failed retrieving public key from server certificate");
1591 goto end;
1594 result = Curl_pin_peer_pubkey(data, pinnedpubkey, pubkey,
1595 (size_t)pubkey_len);
1596 if(result)
1597 failf(data, "SSL: public key does not match pinned public key");
1598#else
1599 failf(data, "Library lacks pinning support built-in");
1600 return CURLE_NOT_BUILT_IN;
1601#endif
1603end:
1604 curlx_free(pubkey);
1605 wolfSSL_FreeX509(x509);
1606 return result;
1609#ifdef WOLFSSL_EARLY_DATA
1610static CURLcode wssl_send_earlydata(struct Curl_cfilter *cf,
1611 struct Curl_easy *data)
1613 struct ssl_connect_data *connssl = cf->ctx;
1614 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
1615 const unsigned char *buf;
1616 size_t blen;
1618 DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_sending);
1619 wssl->io_result = CURLE_OK;
1620 while(Curl_bufq_peek(&connssl->earlydata, &buf, &blen)) {
1621 int nwritten = 0, rc;
1623 wolfSSL_ERR_clear_error();
1624 rc = wolfSSL_write_early_data(wssl->ssl, buf, (int)blen, &nwritten);
1625 CURL_TRC_CF(data, cf, "wolfSSL_write_early_data(len=%zu) -> %d, %d",
1626 blen, rc, nwritten);
1627 if(rc < 0) {
1628 int err = wolfSSL_get_error(wssl->ssl, rc);
1629 char error_buffer[256];
1630 switch(err) {
1631 case WOLFSSL_ERROR_NONE: /* did not get anything */
1632 case WOLFSSL_ERROR_WANT_READ:
1633 case WOLFSSL_ERROR_WANT_WRITE:
1634 return CURLE_AGAIN;
1636 CURL_TRC_CF(data, cf, "SSL send early data, error: '%s'(%d)",
1637 wssl_strerror((unsigned long)err, error_buffer,
1638 sizeof(error_buffer)), err);
1639 return CURLE_SEND_ERROR;
1642 Curl_bufq_skip(&connssl->earlydata, (size_t)nwritten);
1644 /* sent everything there was */
1645 connssl->earlydata_state = ssl_earlydata_sent;
1646 if(!Curl_ssl_cf_is_proxy(cf))
1647 Curl_pgrsEarlyData(data, (curl_off_t)connssl->earlydata_skip);
1648 infof(data, "SSL sending %zu bytes of early data", connssl->earlydata_skip);
1649 return CURLE_OK;
1651#endif /* WOLFSSL_EARLY_DATA */
1653static CURLcode wssl_handshake(struct Curl_cfilter *cf, struct Curl_easy *data)
1655 struct ssl_connect_data *connssl = cf->ctx;
1656 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
1657 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
1658 int ret = -1, detail;
1659 CURLcode result;
1661 DEBUGASSERT(wssl);
1662 connssl->io_need = CURL_SSL_IO_NEED_NONE;
1664#ifdef WOLFSSL_EARLY_DATA
1665 if(connssl->earlydata_state == ssl_earlydata_sending) {
1666 result = wssl_send_earlydata(cf, data);
1667 if(result)
1668 return result;
1670 DEBUGASSERT((connssl->earlydata_state == ssl_earlydata_none) ||
1671 (connssl->earlydata_state == ssl_earlydata_sent));
1672#else
1673 DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_none);
1674#endif /* WOLFSSL_EARLY_DATA */
1676 wolfSSL_ERR_clear_error();
1677 ret = wolfSSL_connect(wssl->ssl);
1679 if(!wssl->x509_store_setup) {
1680 /* After having send off the ClientHello, we prepare the x509
1681 * store to verify the coming certificate from the server */
1682 result = Curl_wssl_setup_x509_store(cf, data, wssl);
1683 if(result) {
1684 CURL_TRC_CF(data, cf, "Curl_wssl_setup_x509_store() -> %d", (int)result);
1685 return result;
1689#ifdef OPENSSL_EXTRA
1690 if(Curl_tls_keylog_enabled()) {
1691 /* If key logging is enabled, wait for the handshake to complete and then
1692 * proceed with logging secrets (for TLS 1.2 or older).
1694 * During the handshake (ret==-1), wolfSSL_want_read() is true as it waits
1695 * for the server response. At that point the master secret is not yet
1696 * available, so we must not try to read it.
1697 * To log the secret on completion with a handshake failure, detect
1698 * completion via the observation that there is nothing to read or write.
1699 * Note that OpenSSL SSL_want_read() is always true here. If wolfSSL ever
1700 * changes, the worst case is that no key is logged on error.
1701 */
1702 if(ret == WOLFSSL_SUCCESS ||
1703 (!wolfSSL_want_read(wssl->ssl) &&
1704 !wolfSSL_want_write(wssl->ssl))) {
1705 wssl_log_tls12_secret(wssl->ssl);
1706 /* Client Random and master secrets are no longer needed, erase these.
1707 * Ignored while the handshake is still in progress. */
1708 wolfSSL_FreeArrays(wssl->ssl);
1711#endif /* OPENSSL_EXTRA */
1713 detail = wolfSSL_get_error(wssl->ssl, ret);
1714 CURL_TRC_CF(data, cf, "wolfSSL_connect() -> %d, detail=%d", ret, detail);
1716 /* On a successful handshake, require a peer certificate when it is to be
1717 * verified. With an anonymous cipher suite the server sends none and
1718 * neither the CA nor the hostname check runs. For an IP address as
1719 * hostname, also check the certificate for an IP match. */
1720 if(ret == WOLFSSL_SUCCESS &&
1721 (conn_config->verifypeer || conn_config->verifyhost)) {
1722 WOLFSSL_X509 *cert = wolfSSL_get_peer_certificate(wssl->ssl);
1723 if(!cert) {
1724 failf(data, "unable to get peer certificate");
1725 return CURLE_PEER_FAILED_VERIFICATION;
1727 if(conn_config->verifyhost && !connssl->peer.sni) {
1728 ret = wolfSSL_X509_check_ip_asc(cert, connssl->peer.origin->hostname,
1729 0);
1730 CURL_TRC_CF(data, cf, "check peer certificate for IP match on %s -> %d",
1731 connssl->peer.origin->hostname, ret);
1732 if(ret != WOLFSSL_SUCCESS)
1733 detail = DOMAIN_NAME_MISMATCH;
1735 wolfSSL_X509_free(cert);
1738 if(ret == WOLFSSL_SUCCESS) {
1739 return CURLE_OK;
1741 else {
1742 if(detail == WOLFSSL_ERROR_WANT_READ) {
1743 connssl->io_need = CURL_SSL_IO_NEED_RECV;
1744 return CURLE_AGAIN;
1746 else if(detail == WOLFSSL_ERROR_WANT_WRITE) {
1747 connssl->io_need = CURL_SSL_IO_NEED_SEND;
1748 return CURLE_AGAIN;
1750 else if(detail == DOMAIN_NAME_MISMATCH) {
1751 /* There is no easy way to override only the CN matching.
1752 * This enables the override of both mismatching SubjectAltNames
1753 * as also mismatching CN fields */
1754 failf(data, " subject alt name(s) or common name do not match \"%s\"",
1755 connssl->peer.origin->hostname);
1756 return CURLE_PEER_FAILED_VERIFICATION;
1758 else if(detail == ASN_NO_SIGNER_E) {
1759 if(conn_config->verifypeer) {
1760 failf(data, " CA signer not available for verification");
1761 return CURLE_SSL_CACERT_BADFILE;
1763 /* Continue with a warning if no strict certificate
1764 verification is required. */
1765 infof(data, "CA signer not available for verification, "
1766 "continuing anyway");
1767 return CURLE_OK;
1769 else if(detail == ASN_AFTER_DATE_E) {
1770 failf(data, "server verification failed: certificate has expired.");
1771 return CURLE_PEER_FAILED_VERIFICATION;
1773 else if(detail == ASN_BEFORE_DATE_E) {
1774 failf(data, "server verification failed: certificate not valid yet.");
1775 return CURLE_PEER_FAILED_VERIFICATION;
1777 else if(wssl->io_result) {
1778 switch(wssl->io_result) {
1779 case CURLE_SEND_ERROR:
1780 case CURLE_RECV_ERROR:
1781 return CURLE_SSL_CONNECT_ERROR;
1782 default:
1783 return wssl->io_result;
1786#ifdef HAVE_WOLFSSL_CTX_GENERATEECHCONFIG
1787 else if(detail == -1) {
1788 /* try access a retry_config ECHConfigList for tracing */
1789 byte echConfigs[1000];
1790 word32 echConfigsLen = 1000;
1791 int rv = 0;
1793 /* this currently does not produce the retry_configs */
1794 rv = wolfSSL_GetEchConfigs(wssl->ssl, echConfigs, &echConfigsLen);
1795 if(rv != WOLFSSL_SUCCESS) {
1796 infof(data, "Failed to get ECHConfigs");
1798 else {
1799 char *b64str = NULL;
1800 size_t blen = 0;
1802 result = curlx_base64_encode(echConfigs, echConfigsLen,
1803 &b64str, &blen);
1804 if(!result && b64str)
1805 infof(data, "ECH: (not yet) retry_configs %s", b64str);
1806 curlx_free(b64str);
1808 return CURLE_SSL_CONNECT_ERROR;
1810#endif
1811 else {
1812 char error_buffer[256];
1813 failf(data, "SSL_connect failed with error %d: %s", detail,
1814 wssl_strerror((unsigned long)detail, error_buffer,
1815 sizeof(error_buffer)));
1816 return CURLE_SSL_CONNECT_ERROR;
1821static CURLcode wssl_send(struct Curl_cfilter *cf,
1822 struct Curl_easy *data,
1823 const void *buf, size_t blen,
1824 size_t *pnwritten)
1826 struct ssl_connect_data *connssl = cf->ctx;
1827 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
1828 CURLcode result = CURLE_OK;
1829 int nwritten;
1831 DEBUGASSERT(wssl);
1832 *pnwritten = 0;
1833 wolfSSL_ERR_clear_error();
1835 if(blen) {
1836 int memlen = (blen > (size_t)INT_MAX) ? INT_MAX : (int)blen;
1838 nwritten = wolfSSL_write(wssl->ssl, buf, memlen);
1840 if(nwritten > 0)
1841 *pnwritten += (size_t)nwritten;
1842 else {
1843 int err = wolfSSL_get_error(wssl->ssl, nwritten);
1845 switch(err) {
1846 case WOLFSSL_ERROR_WANT_READ:
1847 case WOLFSSL_ERROR_WANT_WRITE:
1848 /* there is data pending, re-invoke wolfSSL_write() */
1849 if(*pnwritten) {
1850 result = CURLE_OK;
1851 goto out;
1853 result = CURLE_AGAIN;
1854 goto out;
1856 default:
1857 if(wssl->io_result == CURLE_AGAIN) {
1858 if(*pnwritten) {
1859 result = CURLE_OK;
1860 goto out;
1862 result = CURLE_AGAIN;
1863 goto out;
1866 char error_buffer[256];
1867 failf(data, "SSL write: %s, errno %d",
1868 wssl_strerror((unsigned long)err, error_buffer,
1869 sizeof(error_buffer)),
1870 SOCKERRNO);
1872 result = CURLE_SEND_ERROR;
1873 goto out;
1878out:
1879 CURL_TRC_CF(data, cf, "wssl_send(len=%zu) -> %d, %zu",
1880 blen, (int)result, *pnwritten);
1881 return result;
1884static CURLcode wssl_shutdown(struct Curl_cfilter *cf,
1885 struct Curl_easy *data,
1886 bool send_shutdown, bool *done)
1888 struct ssl_connect_data *connssl = cf->ctx;
1889 struct wssl_ctx *wctx = (struct wssl_ctx *)connssl->backend;
1890 CURLcode result = CURLE_OK;
1891 char buf[1024];
1892 int nread = -1, err;
1893 size_t i;
1894 VERBOSE(char error_buffer[256]);
1896 DEBUGASSERT(wctx);
1897 if(!wctx->ssl || cf->shutdown) {
1898 *done = TRUE;
1899 goto out;
1902 wctx->shutting_down = TRUE;
1903 connssl->io_need = CURL_SSL_IO_NEED_NONE;
1904 *done = FALSE;
1905 if(!(wolfSSL_get_shutdown(wctx->ssl) & WOLFSSL_SENT_SHUTDOWN)) {
1906 /* We have not started the shutdown from our side yet. Check
1907 * if the server already sent us one. */
1908 wolfSSL_ERR_clear_error();
1909 nread = wolfSSL_read(wctx->ssl, buf, (int)sizeof(buf));
1910 err = wolfSSL_get_error(wctx->ssl, nread);
1911 CURL_TRC_CF(data, cf, "wolfSSL_read, nread=%d, err=%d", nread, err);
1912 if(!nread && err == WOLFSSL_ERROR_ZERO_RETURN) {
1913 bool input_pending;
1914 /* Yes, it did. */
1915 if(!send_shutdown) {
1916 CURL_TRC_CF(data, cf, "SSL shutdown received, not sending");
1917 *done = TRUE;
1918 goto out;
1920 else if(!cf->next->cft->is_alive(cf->next, data, &input_pending)) {
1921 /* Server closed the connection after its closy notify. It
1922 * seems not interested to see our close notify, so do not
1923 * send it. We are done. */
1924 CURL_TRC_CF(data, cf, "peer closed connection");
1925 connssl->peer_closed = TRUE;
1926 *done = TRUE;
1927 goto out;
1932 /* wolfSSL should now have started the shutdown from our side. Since it
1933 * was not complete, we are lacking the close notify from the server. */
1934 if(send_shutdown) {
1935 wolfSSL_ERR_clear_error();
1936 nread = wolfSSL_shutdown(wctx->ssl);
1937 if(nread == 1) {
1938 CURL_TRC_CF(data, cf, "SSL shutdown finished");
1939 *done = TRUE;
1940 goto out;
1942 if(wolfSSL_get_error(wctx->ssl, nread) == WOLFSSL_ERROR_WANT_WRITE) {
1943 CURL_TRC_CF(data, cf, "SSL shutdown still wants to send");
1944 connssl->io_need = CURL_SSL_IO_NEED_SEND;
1945 goto out;
1947 /* Having sent the close notify, we use wolfSSL_read() to get the
1948 * missing close notify from the server. */
1951 for(i = 0; i < 10; ++i) {
1952 wolfSSL_ERR_clear_error();
1953 nread = wolfSSL_read(wctx->ssl, buf, (int)sizeof(buf));
1954 if(nread <= 0)
1955 break;
1957 err = wolfSSL_get_error(wctx->ssl, nread);
1958 switch(err) {
1959 case WOLFSSL_ERROR_ZERO_RETURN: /* no more data */
1960 CURL_TRC_CF(data, cf, "SSL shutdown received");
1961 *done = TRUE;
1962 break;
1963 case WOLFSSL_ERROR_NONE: /* did not get anything */
1964 case WOLFSSL_ERROR_WANT_READ:
1965 /* wolfSSL has sent its notify and now wants to read the reply
1966 * from the server. We are not really interested in that. */
1967 CURL_TRC_CF(data, cf, "SSL shutdown sent, want receive");
1968 connssl->io_need = CURL_SSL_IO_NEED_RECV;
1969 break;
1970 case WOLFSSL_ERROR_WANT_WRITE:
1971 CURL_TRC_CF(data, cf, "SSL shutdown send blocked");
1972 connssl->io_need = CURL_SSL_IO_NEED_SEND;
1973 break;
1974 default:
1975 CURL_TRC_CF(data, cf, "SSL shutdown, error: '%s'(%d)",
1976 wssl_strerror((unsigned long)err, error_buffer,
1977 sizeof(error_buffer)),
1978 err);
1979 result = CURLE_RECV_ERROR;
1980 break;
1983out:
1984 cf->shutdown = (result || *done);
1985 return result;
1988static void wssl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
1990 struct ssl_connect_data *connssl = cf->ctx;
1991 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
1993 (void)data;
1995 DEBUGASSERT(wssl);
1997 if(wssl->ssl) {
1998 wolfSSL_free(wssl->ssl);
1999 wssl->ssl = NULL;
2001 if(wssl->ssl_ctx) {
2002 wolfSSL_CTX_free(wssl->ssl_ctx);
2003 wssl->ssl_ctx = NULL;
2007static CURLcode wssl_recv(struct Curl_cfilter *cf,
2008 struct Curl_easy *data,
2009 char *buf, size_t blen,
2010 size_t *pnread)
2012 struct ssl_connect_data *connssl = cf->ctx;
2013 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
2014 int buffsize = (blen > (size_t)INT_MAX) ? INT_MAX : (int)blen;
2015 int nread;
2017 DEBUGASSERT(wssl);
2019 *pnread = 0;
2020 wolfSSL_ERR_clear_error();
2022 nread = wolfSSL_read(wssl->ssl, buf, buffsize);
2024 if(nread > 0)
2025 *pnread = (size_t)nread;
2026 else {
2027 int err = wolfSSL_get_error(wssl->ssl, nread);
2029 switch(err) {
2030 case WOLFSSL_ERROR_ZERO_RETURN: /* no more data */
2031 CURL_TRC_CF(data, cf, "wssl_recv(len=%zu) -> CLOSED", blen);
2032 return CURLE_OK;
2033 case WOLFSSL_ERROR_NONE:
2034 case WOLFSSL_ERROR_WANT_READ:
2035 case WOLFSSL_ERROR_WANT_WRITE:
2036 if(!wssl->io_result && !connssl->peer_closed) {
2037 /* there is data pending, re-invoke wolfSSL_read() */
2038 CURL_TRC_CF(data, cf, "wssl_recv(len=%zu) -> AGAIN", blen);
2039 return CURLE_AGAIN;
2041 /* fall through to default error handling below */
2042 FALLTHROUGH();
2043 default:
2044 if(wssl->io_result == CURLE_AGAIN) {
2045 CURL_TRC_CF(data, cf, "wssl_recv(len=%zu) -> AGAIN", blen);
2046 return CURLE_AGAIN;
2048 else if(!wssl->io_result && connssl->peer_closed) {
2049 CURL_TRC_CF(data, cf, "wssl_recv(len=%zu) -> CLOSED", blen);
2050 failf(data, "Connection closed abruptly");
2052 else {
2053 char error_buffer[256];
2054 failf(data, "SSL read: %s, errno %d",
2055 wssl_strerror((unsigned long)err, error_buffer,
2056 sizeof(error_buffer)),
2057 SOCKERRNO);
2059 return CURLE_RECV_ERROR;
2063 CURL_TRC_CF(data, cf, "wssl_recv(len=%zu) -> 0, %zu", blen, *pnread);
2064 return CURLE_OK;
2067size_t Curl_wssl_version(char *buffer, size_t size)
2069 return curl_msnprintf(buffer, size, "wolfSSL/%s", wolfSSL_lib_version());
2072static int wssl_init(void)
2074 int ret;
2076#ifdef OPENSSL_EXTRA
2077 Curl_tls_keylog_open();
2078#endif
2079 ret = (wolfSSL_Init() == WOLFSSL_SUCCESS);
2080 if(ret)
2081 ret = wssl_bio_cf_init_methods();
2082 return ret;
2085static void wssl_cleanup(void)
2087 wssl_bio_cf_free_methods();
2088 wolfSSL_Cleanup();
2089#ifdef OPENSSL_EXTRA
2090 Curl_tls_keylog_close();
2091#endif
2094static bool wssl_data_pending(struct Curl_cfilter *cf,
2095 const struct Curl_easy *data)
2097 struct ssl_connect_data *ctx = cf->ctx;
2098 struct wssl_ctx *wssl;
2100 (void)data;
2101 DEBUGASSERT(ctx && ctx->backend);
2103 wssl = (struct wssl_ctx *)ctx->backend;
2104 if(wssl->ssl) /* wolfSSL is in use */
2105 return wolfSSL_pending(wssl->ssl);
2106 else
2107 return FALSE;
2110void Curl_wssl_report_handshake(struct Curl_easy *data, struct wssl_ctx *wssl)
2112 (void)wssl;
2113 infof(data, "SSL connection using %s / %s",
2114 wolfSSL_get_version(wssl->ssl),
2115 wolfSSL_get_cipher_name(wssl->ssl));
2118static CURLcode wssl_connect(struct Curl_cfilter *cf,
2119 struct Curl_easy *data,
2120 bool *done)
2122 struct ssl_connect_data *connssl = cf->ctx;
2123 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
2124 CURLcode result = CURLE_OK;
2126 /* check if the connection has already been established */
2127 if(ssl_connection_complete == connssl->state) {
2128 *done = TRUE;
2129 return CURLE_OK;
2132 *done = FALSE;
2133 connssl->io_need = CURL_SSL_IO_NEED_NONE;
2135 if(connssl->connecting_state == ssl_connect_1) {
2136#ifdef HAVE_WOLFSSL_CTX_GENERATEECHCONFIG
2137 /* if we do ECH and need the HTTPS-RR information for it,
2138 * we delay the connect until it arrives or DNS resolve fails. */
2139 if(Curl_ssl_need_httpsrr(cf) &&
2140 !Curl_conn_dns_resolved_https(data, cf->sockindex,
2141 connssl->peer.peer)) {
2142 CURL_TRC_CF(data, cf, "need HTTPS-RR for ECH, delaying connect");
2143 return CURLE_OK;
2145#endif /* HAVE_WOLFSSL_CTX_GENERATEECHCONFIG */
2146 result = wssl_connect_step1(cf, data);
2147 if(result)
2148 return result;
2149 connssl->connecting_state = ssl_connect_2;
2152 if(connssl->connecting_state == ssl_connect_2) {
2153 if(connssl->earlydata_state == ssl_earlydata_await) {
2154 /* We defer the handshake until request data arrives. */
2155 DEBUGASSERT(connssl->state == ssl_connection_deferred);
2156 goto out;
2158 result = wssl_handshake(cf, data);
2159 if(result == CURLE_AGAIN)
2160 goto out;
2161 wssl->hs_result = result;
2162 connssl->connecting_state = ssl_connect_3;
2165 if(connssl->connecting_state == ssl_connect_3) {
2166 /* Once the handshake has errored, it stays in that state and
2167 * errors again on every call. */
2168 if(wssl->hs_result) {
2169 result = wssl->hs_result;
2170 goto out;
2172 result = Curl_wssl_verify_pinned(cf, data, wssl);
2173 if(result) {
2174 wssl->hs_result = result;
2175 goto out;
2177 /* handshake was done without errors */
2178#ifdef HAVE_ALPN
2179 if(connssl->alpn) {
2180 int rc;
2181 char *protocol = NULL;
2182 unsigned short protocol_len = 0;
2184 rc = wolfSSL_ALPN_GetProtocol(wssl->ssl, &protocol, &protocol_len);
2186 if(rc == WOLFSSL_SUCCESS) {
2187 Curl_alpn_set_negotiated(cf, data, connssl,
2188 (const unsigned char *)protocol,
2189 protocol_len);
2191 else if(rc == WOLFSSL_ALPN_NOT_FOUND)
2192 Curl_alpn_set_negotiated(cf, data, connssl, NULL, 0);
2193 else {
2194 failf(data, "ALPN, failure getting protocol, error %d", rc);
2195 wssl->hs_result = result = CURLE_SSL_CONNECT_ERROR;
2196 goto out;
2199#endif /* HAVE_ALPN */
2201 connssl->connecting_state = ssl_connect_done;
2202 connssl->state = ssl_connection_complete;
2203 Curl_wssl_report_handshake(data, wssl);
2205#ifdef WOLFSSL_EARLY_DATA
2206 if(connssl->earlydata_state > ssl_earlydata_none) {
2207 /* We should be in this state by now */
2208 DEBUGASSERT(connssl->earlydata_state == ssl_earlydata_sent);
2209 connssl->earlydata_state =
2210 (wolfSSL_get_early_data_status(wssl->ssl) ==
2211 WOLFSSL_EARLY_DATA_REJECTED) ?
2212 ssl_earlydata_rejected : ssl_earlydata_accepted;
2214#endif /* WOLFSSL_EARLY_DATA */
2217 if((connssl->connecting_state == ssl_connect_done) ||
2218 (connssl->state == ssl_connection_deferred)) {
2219 *done = TRUE;
2222out:
2223 if(result) {
2224 *done = FALSE;
2225 if(result == CURLE_AGAIN)
2226 return CURLE_OK;
2228 else if((connssl->connecting_state == ssl_connect_done) ||
2229 (connssl->state == ssl_connection_deferred)) {
2230 *done = TRUE;
2232 return result;
2235static CURLcode wssl_random(struct Curl_easy *data,
2236 unsigned char *entropy, size_t length)
2238 WC_RNG rng;
2239 (void)data;
2240 if(wc_InitRng(&rng))
2241 return CURLE_FAILED_INIT;
2242 if(length > UINT_MAX)
2243 return CURLE_FAILED_INIT;
2244 if(wc_RNG_GenerateBlock(&rng, entropy, (unsigned)length))
2245 return CURLE_FAILED_INIT;
2246 if(wc_FreeRng(&rng))
2247 return CURLE_FAILED_INIT;
2248 return CURLE_OK;
2251static CURLcode wssl_sha256sum(const unsigned char *input,
2252 size_t len,
2253 unsigned char *sha256sum /* output */,
2254 size_t unused)
2256 wc_Sha256 SHA256pw;
2257 (void)unused;
2258 if(wc_InitSha256(&SHA256pw))
2259 return CURLE_FAILED_INIT;
2260 do {
2261 word32 ilen = (word32)CURLMIN(len, UINT32_MAX);
2262 if(wc_Sha256Update(&SHA256pw, input, ilen))
2263 return CURLE_BAD_FUNCTION_ARGUMENT;
2264 len -= ilen;
2265 input += ilen;
2266 } while(len);
2267 if(wc_Sha256Final(&SHA256pw, sha256sum))
2268 return CURLE_BAD_FUNCTION_ARGUMENT;
2269 return CURLE_OK;
2272static void *wssl_get_internals(struct ssl_connect_data *connssl,
2273 CURLINFO info)
2275 struct wssl_ctx *wssl = (struct wssl_ctx *)connssl->backend;
2276 DEBUGASSERT(wssl);
2277 return info == CURLINFO_TLS_SESSION ?
2278 (void *)wssl->ssl_ctx : (void *)wssl->ssl;
2281const struct Curl_ssl Curl_ssl_wolfssl = {
2282 { CURLSSLBACKEND_WOLFSSL, "wolfssl" }, /* info */
2284#ifdef KEEP_PEER_CERT
2285 SSLSUPP_PINNEDPUBKEY |
2286#endif
2287#ifdef USE_BIO_CHAIN
2288 SSLSUPP_HTTPS_PROXY |
2289#endif
2290 SSLSUPP_CA_PATH |
2291 SSLSUPP_CAINFO_BLOB |
2292#ifdef HAVE_WOLFSSL_CTX_GENERATEECHCONFIG
2293 SSLSUPP_ECH |
2294#endif
2295 SSLSUPP_SSL_CTX |
2296#ifdef WOLFSSL_TLS13
2297 SSLSUPP_TLS13_CIPHERSUITES |
2298#endif
2299 SSLSUPP_CA_CACHE |
2300 SSLSUPP_CIPHER_LIST |
2301 SSLSUPP_SSL_EC_CURVES,
2303 sizeof(struct wssl_ctx),
2305 wssl_init, /* init */
2306 wssl_cleanup, /* cleanup */
2307 Curl_wssl_version, /* version */
2308 wssl_shutdown, /* shutdown */
2309 wssl_data_pending, /* data_pending */
2310 wssl_random, /* random */
2311 NULL, /* cert_status_request */
2312 wssl_connect, /* connect */
2313 Curl_ssl_adjust_pollset, /* adjust_pollset */
2314 wssl_get_internals, /* get_internals */
2315 wssl_close, /* close_one */
2316 NULL, /* close_all */
2317 NULL, /* engines_list */
2318 wssl_sha256sum, /* sha256sum */
2319 wssl_recv, /* recv decrypted data */
2320 wssl_send, /* send data to encrypt */
2321 NULL, /* get_channel_binding */
2324#endif
Diagnostics4 git calls · 76 ms
CallStatusSizeTime076 ms
discover200152 B2 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
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 · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
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 · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=23
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
fetch20020 KiB · hit51 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=51
git.response.bytes=20372
git.response.cache=HIT
git.response.duration_ms=51
git.fetch.wants=62fe70e01029fab0c08e37a3efc025b05eae80c3
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=20372
git.pkt.payload.max=20351
git.sideband.band1.bytes=20350
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
a5e2fc4b3ac5cf81
Request
d8ae04a4…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format