Artifactspublic-v0

curl

master
ply4 git calls141 ms299 KiB2 cachedprotocol v2
1526 lines47 KiBRaw
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Jacob Hoffman-Andrews, <github@hoffman-andrews.com>
9 * Copyright (C) kpcyrd, <kpcyrd@archlinux.org>
10 * Copyright (C) Daniel McCarney, <daniel@binaryparadox.net>
11 *
12 * This software is licensed as described in the file COPYING, which
13 * you should have received as part of this distribution. The terms
14 * are also available at https://curl.se/docs/copyright.html.
15 *
16 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17 * copies of the Software, and permit persons to whom the Software is
18 * furnished to do so, under the terms of the COPYING file.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 * SPDX-License-Identifier: curl
24 *
25 ***************************************************************************/
26#include "curl_setup.h"
28#ifdef USE_RUSTLS
30#include <rustls.h>
32#include "curlx/win32-fopen.h"
33#include "curlx/strerr.h"
34#include "urldata.h"
35#include "curl_trc.h"
36#include "vdns/cf-dns.h"
37#include "vdns/httpsrr.h"
38#include "vtls/vtls.h"
39#include "vtls/vtls_int.h"
40#include "vtls/rustls.h"
41#include "vtls/keylog.h"
42#include "vtls/cipher_suite.h"
43#include "vtls/x509asn1.h"
44#ifdef USE_ECH
45#include "curlx/base64.h"
46#endif
48#if EAGAIN != EWOULDBLOCK
49#define RAW_EAGAIN(e) ((e) == EWOULDBLOCK || (e) == EAGAIN)
50#else
51#define RAW_EAGAIN(e) ((e) == EWOULDBLOCK)
52#endif
54struct rustls_ssl_backend_data {
55 const struct rustls_client_config *config;
56 struct rustls_connection *conn;
57 size_t plain_out_buffered;
58 BIT(data_in_pending);
59 BIT(sent_shutdown);
60};
62/* For a given rustls_result error code, return the best-matching CURLcode. */
63static CURLcode map_error(const rustls_result r)
65 if(rustls_result_is_cert_error(r)) {
66 return CURLE_PEER_FAILED_VERIFICATION;
67 }
68 switch(r) {
69 case RUSTLS_RESULT_OK:
70 return CURLE_OK;
71 case RUSTLS_RESULT_NULL_PARAMETER:
72 return CURLE_BAD_FUNCTION_ARGUMENT;
73 default:
74 return CURLE_RECV_ERROR;
75 }
78static void rustls_failf(struct Curl_easy *data, const rustls_result rr,
79 const char *msg)
81 char errorbuf[STRERROR_LEN];
82 size_t errorlen;
83 rustls_error(rr, errorbuf, sizeof(errorbuf), &errorlen);
84 failf(data, "%s: %.*s", msg, (int)errorlen, errorbuf);
87static bool cr_data_pending(struct Curl_cfilter *cf,
88 const struct Curl_easy *data)
90 const struct ssl_connect_data *ctx = cf->ctx;
91 struct rustls_ssl_backend_data *backend;
93 (void)data;
94 DEBUGASSERT(ctx && ctx->backend);
95 backend = (struct rustls_ssl_backend_data *)ctx->backend;
96 return (bool)backend->data_in_pending;
99struct io_ctx {
100 struct Curl_cfilter *cf;
101 struct Curl_easy *data;
102};
104static int read_cb(void *userdata, uint8_t *buf, uintptr_t len,
105 uintptr_t *out_n)
107 const struct io_ctx *io_ctx = userdata;
108 struct ssl_connect_data * const connssl = io_ctx->cf->ctx;
109 CURLcode result;
110 int ret = 0;
111 size_t nread;
113 result = Curl_conn_cf_recv(io_ctx->cf->next, io_ctx->data,
114 (char *)buf, len, &nread);
115 if(result) {
116 nread = 0;
117 /* !checksrc! disable ERRNOVAR 4 */
118 if(result == CURLE_AGAIN)
119 ret = EAGAIN;
120 else
121 ret = EINVAL;
122 }
123 else if(nread == 0)
124 connssl->peer_closed = TRUE;
125 *out_n = (uintptr_t)nread;
126 CURL_TRC_CF(io_ctx->data, io_ctx->cf, "cf->next recv(len=%zu) -> %d, %zu",
127 (size_t)len, (int)result, nread);
128 return ret;
131static int write_cb(void *userdata, const uint8_t *buf, uintptr_t len,
132 uintptr_t *out_n)
134 const struct io_ctx *io_ctx = userdata;
135 CURLcode result;
136 int ret = 0;
137 size_t nwritten;
139 result = Curl_conn_cf_send(io_ctx->cf->next, io_ctx->data,
140 buf, len, FALSE, &nwritten);
141 if(result) {
142 nwritten = 0;
143 if(result == CURLE_AGAIN)
144 ret = EAGAIN;
145 else
146 ret = EINVAL;
147 }
148 *out_n = (uintptr_t)nwritten;
149 CURL_TRC_CF(io_ctx->data, io_ctx->cf, "cf->next send(len=%zu) -> %d, %zu",
150 len, (int)result, nwritten);
151 return ret;
154static ssize_t tls_recv_more(struct Curl_cfilter *cf,
155 struct Curl_easy *data, CURLcode *err)
157 const struct ssl_connect_data * const connssl = cf->ctx;
158 struct rustls_ssl_backend_data * const backend =
159 (struct rustls_ssl_backend_data *)connssl->backend;
160 struct io_ctx io_ctx;
161 size_t tls_bytes_read = 0;
162 rustls_io_result io_error;
163 rustls_result rresult = 0;
165 io_ctx.cf = cf;
166 io_ctx.data = data;
167 io_error = rustls_connection_read_tls(backend->conn, read_cb, &io_ctx,
168 &tls_bytes_read);
169 if(RAW_EAGAIN(io_error)) {
170 *err = CURLE_AGAIN;
171 return -1;
172 }
173 else if(io_error) {
174 char buffer[STRERROR_LEN];
175 failf(data, "reading from socket: %s",
176 curlx_strerror(io_error, buffer, sizeof(buffer)));
177 *err = CURLE_RECV_ERROR;
178 return -1;
179 }
181 rresult = rustls_connection_process_new_packets(backend->conn);
182 if(rresult != RUSTLS_RESULT_OK) {
183 rustls_failf(data, rresult, "rustls_connection_process_new_packets");
184 *err = map_error(rresult);
185 return -1;
186 }
188 backend->data_in_pending = TRUE;
189 *err = CURLE_OK;
190 return (ssize_t)tls_bytes_read;
193/*
194 * Filter receive method implementation. `plainbuf` and `plainlen`
195 * are always not NULL/0.
196 */
197static CURLcode cr_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
198 char *plainbuf, size_t plainlen, size_t *pnread)
200 const struct ssl_connect_data * const connssl = cf->ctx;
201 struct rustls_ssl_backend_data * const backend =
202 (struct rustls_ssl_backend_data *)connssl->backend;
203 struct rustls_connection *rconn = NULL;
204 CURLcode result = CURLE_OK;
205 size_t n = 0;
206 rustls_result rresult = 0;
207 bool eof = FALSE;
209 DEBUGASSERT(backend);
210 *pnread = 0;
211 rconn = backend->conn;
213 while(*pnread < plainlen) {
214 if(!backend->data_in_pending) {
215 if(tls_recv_more(cf, data, &result) < 0) {
216 if(result != CURLE_AGAIN) {
217 goto out;
218 }
219 result = CURLE_OK;
220 break;
221 }
222 }
224 rresult = rustls_connection_read(rconn,
225 (uint8_t *)plainbuf + *pnread,
226 plainlen - *pnread,
227 &n);
228 if(rresult == RUSTLS_RESULT_PLAINTEXT_EMPTY) {
229 /* A TLS message that carried no plaintext. Break out of the loop
230 * to prevent a server from keeping us here forever. */
231 backend->data_in_pending = FALSE;
232 break;
233 }
234 else if(rresult == RUSTLS_RESULT_UNEXPECTED_EOF) {
235 failf(data, "rustls: peer closed TCP connection "
236 "without first closing TLS connection");
237 result = CURLE_RECV_ERROR;
238 goto out;
239 }
240 else if(rresult != RUSTLS_RESULT_OK) {
241 /* n always equals 0 in this case, do not need to check it */
242 rustls_failf(data, rresult, "rustls_connection_read");
243 result = CURLE_RECV_ERROR;
244 goto out;
245 }
246 else if(n == 0) {
247 /* n == 0 indicates clean EOF, but we may have read some other
248 plaintext bytes before we reached this. Break out of the loop
249 so we can figure out whether to return success or EOF. */
250 eof = TRUE;
251 break;
252 }
253 else {
254 *pnread += n;
255 }
256 }
258 if(!eof && !*pnread) {
259 result = CURLE_AGAIN;
260 }
262out:
263 CURL_TRC_CF(data, cf, "rustls_recv(len=%zu) -> %d, %zu",
264 plainlen, (int)result, *pnread);
265 return result;
268static CURLcode cr_flush_out(struct Curl_cfilter *cf, struct Curl_easy *data,
269 struct rustls_connection *rconn)
271 struct io_ctx io_ctx;
272 rustls_io_result io_error;
273 size_t tlswritten = 0;
274 size_t tlswritten_total = 0;
276 io_ctx.cf = cf;
277 io_ctx.data = data;
279 while(rustls_connection_wants_write(rconn)) {
280 io_error = rustls_connection_write_tls(rconn, write_cb, &io_ctx,
281 &tlswritten);
282 if(RAW_EAGAIN(io_error)) {
283 CURL_TRC_CF(data, cf, "cf_send: EAGAIN after %zu bytes",
284 tlswritten_total);
285 return CURLE_AGAIN;
286 }
287 else if(io_error) {
288 char buffer[STRERROR_LEN];
289 failf(data, "writing to socket: %s",
290 curlx_strerror(io_error, buffer, sizeof(buffer)));
291 return CURLE_SEND_ERROR;
292 }
293 if(tlswritten == 0) {
294 failf(data, "EOF in swrite");
295 return CURLE_SEND_ERROR;
296 }
297 CURL_TRC_CF(data, cf, "cf_send: wrote %zu TLS bytes", tlswritten);
298 tlswritten_total += tlswritten;
299 }
300 return CURLE_OK;
303/*
304 * On each call:
305 * - Copy new plaintext into Rustls, or acknowledge a previous blocked send.
306 * - Fully drain Rustls' TLS output buffer into the socket until
307 * we get either an error or EAGAIN/EWOULDBLOCK.
308 *
309 * it is okay to call this function with plainbuf == NULL and plainlen == 0.
310 * In that case, only drain TLS output, without acknowledging plaintext.
311 */
312static CURLcode cr_send(struct Curl_cfilter *cf, struct Curl_easy *data,
313 const void *plainbuf, size_t plainlen,
314 size_t *pnwritten)
316 const struct ssl_connect_data * const connssl = cf->ctx;
317 struct rustls_ssl_backend_data * const backend =
318 (struct rustls_ssl_backend_data *)connssl->backend;
319 struct rustls_connection *rconn = NULL;
320 size_t plainwritten = 0;
321 const unsigned char *buf = plainbuf;
322 CURLcode result = CURLE_OK;
323 size_t blen = plainlen;
325 DEBUGASSERT(backend);
326 *pnwritten = 0;
327 rconn = backend->conn;
328 DEBUGASSERT(rconn);
330 CURL_TRC_CF(data, cf, "cf_send(len=%zu)", plainlen);
332 /* Handshake and shutdown flushes are not retries of application data. */
333 if(!plainbuf && !plainlen) {
334 result = cr_flush_out(cf, data, rconn);
335 goto out;
336 }
338 /* If a previous send blocked, we already added its plain bytes
339 * to Rustls and must not do that again. Flush the TLS bytes and,
340 * if successful, deduct the previous plain bytes from the current
341 * send. */
342 if(backend->plain_out_buffered) {
343 if(blen < backend->plain_out_buffered) {
344 DEBUGASSERT(0);
345 failf(data, "rustls: send retried with less data (%zu) than the %zu "
346 "bytes already buffered", blen, backend->plain_out_buffered);
347 result = CURLE_BAD_FUNCTION_ARGUMENT;
348 goto out;
349 }
350 result = cr_flush_out(cf, data, rconn);
351 CURL_TRC_CF(data, cf, "cf_send: flushing %zu previously added bytes -> %d",
352 backend->plain_out_buffered, (int)result);
353 if(result)
354 return result;
355 blen -= backend->plain_out_buffered;
356 buf += backend->plain_out_buffered;
357 *pnwritten += backend->plain_out_buffered;
358 backend->plain_out_buffered = 0;
359 }
361 if(blen > 0) {
362 rustls_result rresult;
363 CURL_TRC_CF(data, cf, "cf_send: adding %zu plain bytes to Rustls", blen);
364 rresult = rustls_connection_write(rconn, buf, blen, &plainwritten);
365 if(rresult != RUSTLS_RESULT_OK) {
366 rustls_failf(data, rresult, "rustls_connection_write");
367 result = CURLE_WRITE_ERROR;
368 goto out;
369 }
370 else if(plainwritten == 0) {
371 failf(data, "rustls_connection_write: EOF");
372 result = CURLE_WRITE_ERROR;
373 goto out;
374 }
375 }
377 result = cr_flush_out(cf, data, rconn);
378 if(result) {
379 if(result == CURLE_AGAIN) {
380 /* The TLS bytes may have been partially written, but we fail the
381 * complete send() and remember how much we already added to Rustls. */
382 backend->plain_out_buffered = plainwritten;
383 if(*pnwritten) {
384 result = CURLE_OK;
385 }
386 }
387 goto out;
388 }
389 else
390 *pnwritten += plainwritten;
392out:
393 CURL_TRC_CF(data, cf, "rustls_send(len=%zu) -> %d, %zu",
394 plainlen, (int)result, *pnwritten);
395 return result;
398/* A server certificate verify callback for Rustls that always returns
399 RUSTLS_RESULT_OK, or in other words disable certificate verification. */
400static uint32_t cr_verify_none(void *userdata,
401 const rustls_verify_server_cert_params *params)
403 (void)userdata;
404 (void)params;
405 return RUSTLS_RESULT_OK;
408#ifdef UNITTESTS
409/* @unittest 2640 */
410UNITTEST CURLcode rustls_test_init(void *sslctx, size_t buffer_limit);
411UNITTEST CURLcode rustls_test_init(void *sslctx, size_t buffer_limit)
413 struct ssl_connect_data *connssl = sslctx;
414 struct rustls_ssl_backend_data *backend = connssl->backend;
415 struct rustls_client_config_builder *builder;
416 rustls_result rr;
418 builder = rustls_client_config_builder_new();
419 if(!builder)
420 return CURLE_OUT_OF_MEMORY;
421 rr = rustls_client_config_builder_dangerous_set_certificate_verifier(
422 builder, cr_verify_none);
423 if(rr != RUSTLS_RESULT_OK) {
424 rustls_client_config_builder_free(builder);
425 return map_error(rr);
426 }
427 rr = rustls_client_config_builder_build(builder, &backend->config);
428 if(rr != RUSTLS_RESULT_OK)
429 return map_error(rr);
430 rr = rustls_client_connection_new(backend->config, "localhost",
431 &backend->conn);
432 if(rr != RUSTLS_RESULT_OK)
433 return map_error(rr);
434 rustls_connection_set_buffer_limit(backend->conn, buffer_limit);
435 return CURLE_OK;
437#endif
439static int read_file_into(const char *filename, struct dynbuf *out)
441 FILE *f = curlx_fopen(filename, FOPEN_READTEXT);
442 if(!f) {
443 return 0;
444 }
446 for(;;) {
447 uint8_t buf[256];
448 const size_t rr = fread(buf, 1, sizeof(buf), f);
449 if((!rr && !feof(f)) ||
450 curlx_dyn_addn(out, buf, rr)) {
451 curlx_fclose(f);
452 return 0;
453 }
454 if(rr < sizeof(buf))
455 break;
456 }
458 return curlx_fclose(f) == 0;
461static void
462cr_get_selected_ciphers(struct Curl_easy *data,
463 const char *ciphers12,
464 const char *ciphers13,
465 const struct rustls_supported_ciphersuite **selected,
466 size_t *selected_size)
468 const size_t supported_len = *selected_size;
469 const size_t default_len = rustls_default_crypto_provider_ciphersuites_len();
470 const struct rustls_supported_ciphersuite *entry = NULL;
471 const char *ciphers = ciphers12;
472 size_t count = 0, default13_count = 0, i, j;
473 const char *ptr, *end;
475 DEBUGASSERT(default_len <= supported_len);
477 if(!ciphers13) {
478 /* Add default TLSv1.3 ciphers to selection */
479 for(j = 0; j < default_len; j++) {
480 entry = rustls_default_crypto_provider_ciphersuites_get(j);
481 if(rustls_supported_ciphersuite_protocol_version(entry) !=
482 RUSTLS_TLS_VERSION_TLSV1_3)
483 continue;
485 selected[count++] = entry;
486 }
488 default13_count = count;
490 if(!ciphers)
491 ciphers = "";
492 }
493 else
494 ciphers = ciphers13;
496add_ciphers:
497 for(ptr = ciphers; ptr[0] != '\0' && count < supported_len; ptr = end) {
498 uint16_t id = Curl_cipher_suite_walk_str(&ptr, &end);
500 /* Check if cipher is supported */
501 if(id) {
502 for(i = 0; i < supported_len; i++) {
503 entry = rustls_default_crypto_provider_ciphersuites_get(i);
504 if(rustls_supported_ciphersuite_get_suite(entry) == id)
505 break;
506 }
507 if(i == supported_len)
508 id = 0;
509 }
510 if(!id) {
511 if(ptr[0] != '\0')
512 infof(data, "rustls: unknown cipher in list: \"%.*s\"",
513 (int)(end - ptr), ptr);
514 continue;
515 }
517 /* No duplicates allowed (so selected cannot overflow) */
518 for(i = 0; i < count && selected[i] != entry; i++)
519 ;
520 if(i < count) {
521 if(i >= default13_count)
522 infof(data, "rustls: duplicate cipher in list: \"%.*s\"",
523 (int)(end - ptr), ptr);
524 continue;
525 }
527 selected[count++] = entry;
528 }
530 if(ciphers == ciphers13 && ciphers12) {
531 ciphers = ciphers12;
532 goto add_ciphers;
533 }
535 if(!ciphers12) {
536 /* Add default TLSv1.2 ciphers to selection */
537 for(j = 0; j < default_len; j++) {
538 entry = rustls_default_crypto_provider_ciphersuites_get(j);
539 if(rustls_supported_ciphersuite_protocol_version(entry) ==
540 RUSTLS_TLS_VERSION_TLSV1_3)
541 continue;
543 /* No duplicates allowed (so selected cannot overflow) */
544 for(i = 0; i < count && selected[i] != entry; i++)
545 ;
546 if(i < count)
547 continue;
549 selected[count++] = entry;
550 }
551 }
553 *selected_size = count;
556static void cr_keylog_log_cb(struct rustls_str label,
557 const uint8_t *client_random,
558 size_t client_random_len, const uint8_t *secret,
559 size_t secret_len)
561 char clabel[KEYLOG_LABEL_MAXLEN];
562 DEBUGASSERT(client_random_len == CLIENT_RANDOM_SIZE);
563 /* Turning a "rustls_str" into a null delimited "c" string */
564 curl_msnprintf(clabel, sizeof(clabel), "%.*s", (int)label.len, label.data);
565 Curl_tls_keylog_write(clabel, client_random, client_random_len,
566 secret, secret_len);
569static CURLcode
570init_config_builder(struct Curl_cfilter *cf,
571 struct Curl_easy *data,
572 const struct ssl_filter_config *conn_config,
573 struct rustls_client_config_builder **config_builder)
575 const struct rustls_supported_ciphersuite **cipher_suites = NULL;
576 struct rustls_crypto_provider_builder *custom_provider_builder = NULL;
577 const struct rustls_crypto_provider *custom_provider = NULL;
579 uint16_t tls_versions[2] = {
580 RUSTLS_TLS_VERSION_TLSV1_2,
581 RUSTLS_TLS_VERSION_TLSV1_3,
582 };
583 size_t tls_versions_len = 2;
584 size_t cipher_suites_len = rustls_default_crypto_provider_ciphersuites_len();
586 CURLcode result = CURLE_OK;
587 rustls_result rr;
589 DEBUGASSERT(conn_config->version != CURL_SSLVERSION_DEFAULT);
590 switch(conn_config->version) {
591 case CURL_SSLVERSION_TLSv1:
592 case CURL_SSLVERSION_TLSv1_0:
593 case CURL_SSLVERSION_TLSv1_1:
594 case CURL_SSLVERSION_TLSv1_2:
595 break;
596 case CURL_SSLVERSION_TLSv1_3:
597 tls_versions[0] = RUSTLS_TLS_VERSION_TLSV1_3;
598 tls_versions_len = 1;
599 break;
600 default:
601 failf(data, "rustls: unsupported minimum TLS version value");
602 result = CURLE_BAD_FUNCTION_ARGUMENT;
603 goto cleanup;
604 }
606 switch(conn_config->version_max) {
607 case CURL_SSLVERSION_MAX_DEFAULT:
608 case CURL_SSLVERSION_MAX_NONE:
609 case CURL_SSLVERSION_MAX_TLSv1_3:
610 break;
611 case CURL_SSLVERSION_MAX_TLSv1_2:
612 if(tls_versions[0] == RUSTLS_TLS_VERSION_TLSV1_2) {
613 tls_versions_len = 1;
614 break;
615 }
616 FALLTHROUGH();
617 case CURL_SSLVERSION_MAX_TLSv1_1:
618 case CURL_SSLVERSION_MAX_TLSv1_0:
619 default:
620 failf(data, "rustls: unsupported maximum TLS version value");
621 result = CURLE_BAD_FUNCTION_ARGUMENT;
622 goto cleanup;
623 }
625#ifdef USE_ECH
626 if(Curl_ssl_ech_enabled(cf)) {
627 tls_versions[0] = RUSTLS_TLS_VERSION_TLSV1_3;
628 tls_versions_len = 1;
629 infof(data, "rustls: ECH enabled, forcing TLSv1.3");
630 }
631#else
632 (void)cf;
633#endif /* USE_ECH */
635 cipher_suites = curlx_malloc(sizeof(*cipher_suites) * cipher_suites_len);
636 if(!cipher_suites) {
637 result = CURLE_OUT_OF_MEMORY;
638 goto cleanup;
639 }
641 cr_get_selected_ciphers(data,
642 conn_config->cipher_list,
643 conn_config->cipher_list13,
644 cipher_suites, &cipher_suites_len);
645 if(cipher_suites_len == 0) {
646 failf(data, "rustls: no supported cipher in list");
647 result = CURLE_SSL_CIPHER;
648 goto cleanup;
649 }
651 rr = rustls_crypto_provider_builder_new_from_default(
652 &custom_provider_builder);
653 if(rr != RUSTLS_RESULT_OK) {
654 rustls_failf(data, rr,
655 "failed to create crypto provider builder from default");
656 result = CURLE_SSL_CIPHER;
657 goto cleanup;
658 }
660 rr =
661 rustls_crypto_provider_builder_set_cipher_suites(
662 custom_provider_builder,
663 cipher_suites,
664 cipher_suites_len);
665 if(rr != RUSTLS_RESULT_OK) {
666 rustls_failf(data, rr,
667 "failed to set ciphersuites for crypto provider builder");
668 result = CURLE_SSL_CIPHER;
669 goto cleanup;
670 }
672 rr = rustls_crypto_provider_builder_build(custom_provider_builder,
673 &custom_provider);
674 if(rr != RUSTLS_RESULT_OK) {
675 rustls_failf(data, rr, "failed to build custom crypto provider");
676 result = CURLE_SSL_CIPHER;
677 goto cleanup;
678 }
680 rr = rustls_client_config_builder_new_custom(custom_provider,
681 tls_versions,
682 tls_versions_len,
683 config_builder);
684 if(rr != RUSTLS_RESULT_OK) {
685 rustls_failf(data, rr, "failed to create client config builder");
686 result = CURLE_SSL_CIPHER;
687 goto cleanup;
688 }
690cleanup:
691 if(cipher_suites) {
692 curlx_free(cipher_suites);
693 }
694 if(custom_provider_builder) {
695 rustls_crypto_provider_builder_free(custom_provider_builder);
696 }
697 if(custom_provider) {
698 rustls_crypto_provider_free(custom_provider);
699 }
700 return result;
703static void
704init_config_builder_alpn(struct Curl_easy *data,
705 const struct ssl_connect_data *connssl,
706 struct rustls_client_config_builder *config_builder)
708 struct alpn_proto_buf proto;
709 rustls_slice_bytes alpn[ALPN_ENTRIES_MAX];
710 size_t i;
712 for(i = 0; i < connssl->alpn->count; ++i) {
713 alpn[i].data = (const uint8_t *)connssl->alpn->entries[i];
714 alpn[i].len = strlen(connssl->alpn->entries[i]);
715 }
716 rustls_client_config_builder_set_alpn_protocols(config_builder, alpn,
717 connssl->alpn->count);
718 Curl_alpn_to_proto_str(&proto, connssl->alpn);
719 infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
722static CURLcode init_config_builder_verifier_crl(
723 struct Curl_easy *data,
724 const struct ssl_filter_config *conn_config,
725 struct rustls_web_pki_server_cert_verifier_builder *builder)
727 CURLcode result = CURLE_OK;
728 struct dynbuf crl_contents;
729 rustls_result rr;
731 curlx_dyn_init(&crl_contents, DYN_CRLFILE_SIZE);
732 if(!read_file_into(conn_config->CRLfile, &crl_contents)) {
733 failf(data, "rustls: failed to read revocation list file");
734 result = CURLE_SSL_CRL_BADFILE;
735 goto cleanup;
736 }
738 rr = rustls_web_pki_server_cert_verifier_builder_add_crl(
739 builder,
740 curlx_dyn_uptr(&crl_contents),
741 curlx_dyn_len(&crl_contents));
742 if(rr != RUSTLS_RESULT_OK) {
743 rustls_failf(data, rr, "failed to parse revocation list");
744 result = CURLE_SSL_CRL_BADFILE;
745 goto cleanup;
746 }
748cleanup:
749 curlx_dyn_free(&crl_contents);
750 return result;
753static CURLcode
754init_config_builder_verifier(struct Curl_easy *data,
755 struct rustls_client_config_builder *builder,
756 const struct ssl_filter_config *conn_config,
757 const struct curl_blob *ca_info_blob,
758 const char * const ssl_cafile)
760 const struct rustls_root_cert_store *roots = NULL;
761 struct rustls_root_cert_store_builder *roots_builder = NULL;
762 struct rustls_web_pki_server_cert_verifier_builder *verifier_builder = NULL;
763 struct rustls_server_cert_verifier *server_cert_verifier = NULL;
764 rustls_result rr = RUSTLS_RESULT_OK;
765 CURLcode result = CURLE_OK;
767 roots_builder = rustls_root_cert_store_builder_new();
768 if(ca_info_blob) {
769 rr = rustls_root_cert_store_builder_add_pem(roots_builder,
770 ca_info_blob->data,
771 ca_info_blob->len,
772 1);
773 if(rr != RUSTLS_RESULT_OK) {
774 rustls_failf(data, rr, "failed to parse trusted certificates from blob");
775 result = CURLE_SSL_CACERT_BADFILE;
776 goto cleanup;
777 }
778 }
779 else if(ssl_cafile) {
780 rr = rustls_root_cert_store_builder_load_roots_from_file(roots_builder,
781 ssl_cafile,
782 1);
783 if(rr != RUSTLS_RESULT_OK) {
784 rustls_failf(data, rr, "failed to load trusted certificates");
785 result = CURLE_SSL_CACERT_BADFILE;
786 goto cleanup;
787 }
788 }
790 rr = rustls_root_cert_store_builder_build(roots_builder, &roots);
791 if(rr != RUSTLS_RESULT_OK) {
792 rustls_failf(data, rr, "failed to build trusted root certificate store");
793 result = CURLE_SSL_CACERT_BADFILE;
794 goto cleanup;
795 }
797 verifier_builder = rustls_web_pki_server_cert_verifier_builder_new(roots);
798 if(!verifier_builder) {
799 result = CURLE_OUT_OF_MEMORY;
800 goto cleanup;
801 }
803 if(conn_config->CRLfile) {
804 result = init_config_builder_verifier_crl(data,
805 conn_config,
806 verifier_builder);
807 if(result) {
808 goto cleanup;
809 }
810 }
812 rr = rustls_web_pki_server_cert_verifier_builder_build(
813 verifier_builder, &server_cert_verifier);
814 if(rr != RUSTLS_RESULT_OK) {
815 rustls_failf(data, rr, "failed to build certificate verifier");
816 result = CURLE_SSL_CACERT_BADFILE;
817 goto cleanup;
818 }
820 rustls_client_config_builder_set_server_verifier(builder,
821 server_cert_verifier);
822cleanup:
823 if(roots_builder) {
824 rustls_root_cert_store_builder_free(roots_builder);
825 }
826 if(roots) {
827 rustls_root_cert_store_free(roots);
828 }
829 if(verifier_builder) {
830 rustls_web_pki_server_cert_verifier_builder_free(verifier_builder);
831 }
832 if(server_cert_verifier) {
833 rustls_server_cert_verifier_free(server_cert_verifier);
834 }
836 return result;
839static CURLcode init_config_builder_platform_verifier(
840 struct Curl_easy *data,
841 struct rustls_client_config_builder *builder)
843 struct rustls_server_cert_verifier *server_cert_verifier = NULL;
844 CURLcode result = CURLE_OK;
845 rustls_result rr;
847 rr = rustls_platform_server_cert_verifier(&server_cert_verifier);
848 if(rr != RUSTLS_RESULT_OK) {
849 rustls_failf(data, rr, "failed to create platform certificate verifier");
850 result = CURLE_SSL_CACERT_BADFILE;
851 goto cleanup;
852 }
854 rustls_client_config_builder_set_server_verifier(builder,
855 server_cert_verifier);
857cleanup:
858 if(server_cert_verifier) {
859 rustls_server_cert_verifier_free(server_cert_verifier);
860 }
861 return result;
864static CURLcode
865init_config_builder_keylog(struct Curl_easy *data,
866 struct rustls_client_config_builder *builder)
868 rustls_result rr;
870 Curl_tls_keylog_open();
871 if(!Curl_tls_keylog_enabled()) {
872 return CURLE_OK;
873 }
875 rr = rustls_client_config_builder_set_key_log(builder,
876 cr_keylog_log_cb,
877 NULL);
878 if(rr != RUSTLS_RESULT_OK) {
879 rustls_failf(data, rr, "rustls_client_config_builder_set_key_log");
880 Curl_tls_keylog_close();
881 return map_error(rr);
882 }
884 return CURLE_OK;
887static CURLcode
888init_config_builder_client_auth(struct Curl_easy *data,
889 const struct ssl_filter_config *conn_config,
890 struct rustls_client_config_builder *builder)
892 struct dynbuf cert_contents;
893 struct dynbuf key_contents;
894 rustls_result rr;
895 const struct rustls_certified_key *certified_key = NULL;
896 CURLcode result = CURLE_OK;
897 const bool have_cert = conn_config->clientcert || conn_config->cert_blob;
898 const bool have_key = conn_config->key || conn_config->key_blob;
900 if(have_cert && !have_key) {
901 failf(data, "rustls: must provide key with certificate");
902 return CURLE_SSL_CERTPROBLEM;
903 }
904 else if(!have_cert && have_key) {
905 failf(data, "rustls: must provide certificate with key");
906 return CURLE_SSL_CERTPROBLEM;
907 }
909 if(conn_config->cert_type && !curl_strequal(conn_config->cert_type, "PEM")) {
910 failf(data, "rustls: unsupported certificate type '%s', only 'PEM' is "
911 "supported", conn_config->cert_type);
912 return CURLE_SSL_CERTPROBLEM;
913 }
914 if(conn_config->key_type && !curl_strequal(conn_config->key_type, "PEM")) {
915 failf(data, "rustls: unsupported key type '%s', only 'PEM' is supported",
916 conn_config->key_type);
917 return CURLE_SSL_CERTPROBLEM;
918 }
920 curlx_dyn_init(&cert_contents, DYN_CERTFILE_SIZE);
921 curlx_dyn_init(&key_contents, DYN_KEYFILE_SIZE);
923 if(conn_config->cert_blob) {
924 result = curlx_dyn_addn(&cert_contents,
925 conn_config->cert_blob->data,
926 conn_config->cert_blob->len);
927 if(result) {
928 failf(data, "rustls: failed to copy client certificate blob (%s)",
929 result == CURLE_TOO_LARGE ? "too large" : "out of memory");
930 goto cleanup;
931 }
932 }
933 else if(!read_file_into(conn_config->clientcert, &cert_contents)) {
934 failf(data, "rustls: failed to read client certificate file: '%s'",
935 conn_config->clientcert);
936 result = CURLE_SSL_CERTPROBLEM;
937 goto cleanup;
938 }
940 if(conn_config->key_blob) {
941 result = curlx_dyn_addn(&key_contents,
942 conn_config->key_blob->data,
943 conn_config->key_blob->len);
944 if(result) {
945 failf(data, "rustls: failed to copy key blob (%s)",
946 result == CURLE_TOO_LARGE ? "too large" : "out of memory");
947 goto cleanup;
948 }
949 }
950 else if(!read_file_into(conn_config->key, &key_contents)) {
951 failf(data, "rustls: failed to read key file: '%s'",
952 conn_config->key);
953 result = CURLE_SSL_CERTPROBLEM;
954 goto cleanup;
955 }
957 rr = rustls_certified_key_build(curlx_dyn_uptr(&cert_contents),
958 curlx_dyn_len(&cert_contents),
959 curlx_dyn_uptr(&key_contents),
960 curlx_dyn_len(&key_contents),
961 &certified_key);
962 if(rr != RUSTLS_RESULT_OK) {
963 rustls_failf(data, rr, "rustls: failed to build certified key");
964 result = CURLE_SSL_CERTPROBLEM;
965 goto cleanup;
966 }
968 rr = rustls_certified_key_keys_match(certified_key);
969 if(rr != RUSTLS_RESULT_OK) {
970 rustls_failf(data, rr,
971 "rustls: client certificate and keypair files do not match:");
972 result = CURLE_SSL_CERTPROBLEM;
973 goto cleanup;
974 }
976 rr = rustls_client_config_builder_set_certified_key(builder,
977 &certified_key,
978 1);
979 if(rr != RUSTLS_RESULT_OK) {
980 rustls_failf(data, rr, "rustls: failed to set certified key");
981 result = CURLE_SSL_CERTPROBLEM;
982 goto cleanup;
983 }
985cleanup:
986 curlx_dyn_free(&cert_contents);
987 curlx_dyn_free(&key_contents);
988 if(certified_key) {
989 rustls_certified_key_free(certified_key);
990 }
991 return result;
994#ifdef USE_ECH
996static CURLcode
997init_config_builder_ech(struct Curl_easy *data,
998 struct Curl_cfilter *cf,
999 struct rustls_client_config_builder *builder)
1001 struct ssl_filter_config *conn_config = Curl_ssl_cf_get_filter_config(cf);
1002 const rustls_hpke *hpke = rustls_supported_hpke();
1003 unsigned char *ech_config = NULL;
1004 size_t ech_config_len = 0;
1005 struct Curl_dns_entry *dns = NULL;
1006 CURLcode result = CURLE_OK;
1007 rustls_result rr;
1009 if(!hpke) {
1010 failf(data,
1011 "rustls: ECH unavailable, rustls-ffi built without "
1012 "HPKE compatible crypto provider");
1013 result = CURLE_SSL_CONNECT_ERROR;
1014 goto cleanup;
1017 if(conn_config->ech_public) {
1018 failf(data, "rustls: ECH outername not supported");
1019 result = CURLE_SSL_CONNECT_ERROR;
1020 goto cleanup;
1023 if(conn_config->ech == CURLECH_GREASE) {
1024 rr = rustls_client_config_builder_enable_ech_grease(builder, hpke);
1025 if(rr != RUSTLS_RESULT_OK) {
1026 rustls_failf(data, rr, "rustls: failed to configure ECH GREASE");
1027 result = CURLE_SSL_CONNECT_ERROR;
1028 goto cleanup;
1030 return CURLE_OK;
1033 if(conn_config->ech && conn_config->ech_config) {
1034 const char *b64 = conn_config->ech_config;
1035 size_t decode_result;
1036 if(!b64[0]) {
1037 infof(data, "rustls: ECHConfig from command line empty");
1038 result = CURLE_SSL_CONNECT_ERROR;
1039 goto cleanup;
1041 /* rustls-ffi expects the raw TLS encoded ECHConfigList bytes */
1042 decode_result = curlx_base64_decode(b64, &ech_config, &ech_config_len);
1043 if(decode_result || !ech_config) {
1044 infof(data, "rustls: cannot base64 decode ECHConfig from command line");
1045 result = CURLE_SSL_CONNECT_ERROR;
1046 goto cleanup;
1049 else {
1050 const struct ssl_connect_data *connssl = cf->ctx;
1051 const struct Curl_https_rrinfo *rinfo =
1052 Curl_conn_dns_get_https(data, cf->sockindex, connssl->peer.origin);
1054 if(!Curl_httpsrr_is_for_peer(connssl->peer.origin, rinfo) ||
1055 !rinfo->echconfiglist) {
1056 failf(data, "rustls: ECH requested but no ECHConfig available");
1057 result = CURLE_SSL_CONNECT_ERROR;
1058 goto cleanup;
1060 ech_config = rinfo->echconfiglist;
1061 ech_config_len = rinfo->echconfiglist_len;
1064 rr = rustls_client_config_builder_enable_ech(builder,
1065 ech_config,
1066 ech_config_len,
1067 hpke);
1068 if(rr != RUSTLS_RESULT_OK) {
1069 rustls_failf(data, rr, "rustls: failed to configure ECH");
1070 result = CURLE_SSL_CONNECT_ERROR;
1071 goto cleanup;
1073cleanup:
1074 /* if we base64 decoded, we can free now */
1075 if(conn_config->ech && conn_config->ech_config) {
1076 curlx_free(ech_config);
1078 if(dns) {
1079 Curl_dns_entry_unlink(data, &dns);
1081 return result;
1083#endif /* USE_ECH */
1085static CURLcode cr_init_backend(struct Curl_cfilter *cf,
1086 struct Curl_easy *data,
1087 struct rustls_ssl_backend_data * const backend)
1089 const struct ssl_connect_data *connssl = cf->ctx;
1090 const struct ssl_filter_config *conn_config =
1091 Curl_ssl_cf_get_filter_config(cf);
1092 struct rustls_connection *rconn = NULL;
1093 struct rustls_client_config_builder *config_builder = NULL;
1095 const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
1096 const char * const ssl_cafile =
1097 /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
1098 (ca_info_blob ? NULL : conn_config->CAfile);
1099 CURLcode result = CURLE_OK;
1100 rustls_result rr;
1102 DEBUGASSERT(backend);
1103 rconn = backend->conn;
1105 result = init_config_builder(cf, data, conn_config, &config_builder);
1106 if(result != CURLE_OK) {
1107 return result;
1110 if(connssl->alpn) {
1111 init_config_builder_alpn(data, connssl, config_builder);
1114 if(!conn_config->verifypeer) {
1115 rustls_client_config_builder_dangerous_set_certificate_verifier(
1116 config_builder, cr_verify_none);
1118 else if(conn_config->native_ca_store) {
1119 if(conn_config->CRLfile) {
1120 failf(data, "rustls: CRL file not supported with native CA store; "
1121 "the platform verifier has no CRL attachment API");
1122 rustls_client_config_builder_free(config_builder);
1123 return CURLE_NOT_BUILT_IN;
1125 result = init_config_builder_platform_verifier(data, config_builder);
1126 if(result != CURLE_OK) {
1127 rustls_client_config_builder_free(config_builder);
1128 return result;
1131 else if(ca_info_blob || ssl_cafile) {
1132 result = init_config_builder_verifier(data,
1133 config_builder,
1134 conn_config,
1135 ca_info_blob,
1136 ssl_cafile);
1137 if(result != CURLE_OK) {
1138 rustls_client_config_builder_free(config_builder);
1139 return result;
1143 if(conn_config->clientcert || conn_config->cert_blob ||
1144 conn_config->key || conn_config->key_blob) {
1145 result = init_config_builder_client_auth(data,
1146 conn_config,
1147 config_builder);
1148 if(result != CURLE_OK) {
1149 rustls_client_config_builder_free(config_builder);
1150 return result;
1154#ifdef USE_ECH
1155 if(Curl_ssl_ech_enabled(cf)) {
1156 result = init_config_builder_ech(data, cf, config_builder);
1157 if((result != CURLE_OK) && (conn_config->ech == CURLECH_HARD)) {
1158 rustls_client_config_builder_free(config_builder);
1159 return result;
1162#endif /* USE_ECH */
1164 result = init_config_builder_keylog(data, config_builder);
1165 if(result != CURLE_OK) {
1166 rustls_client_config_builder_free(config_builder);
1167 return result;
1170 rr = rustls_client_config_builder_build(config_builder, &backend->config);
1171 if(rr != RUSTLS_RESULT_OK) {
1172 rustls_failf(data, rr, "failed to build client config");
1173 return CURLE_SSL_CONNECT_ERROR;
1176 DEBUGASSERT(!rconn);
1177 rr = rustls_client_connection_new(backend->config,
1178 connssl->peer.origin->hostname,
1179 &rconn);
1180 if(rr != RUSTLS_RESULT_OK) {
1181 rustls_failf(data, rr, "rustls_client_connection_new");
1182 rustls_client_config_free(backend->config);
1183 backend->config = NULL;
1184 return CURLE_COULDNT_CONNECT;
1186 DEBUGASSERT(rconn);
1187 rustls_connection_set_userdata(rconn, backend);
1188 backend->conn = rconn;
1190 return result;
1193static void cr_set_negotiated_alpn(struct Curl_cfilter *cf,
1194 struct Curl_easy *data,
1195 const struct rustls_connection *rconn)
1197 struct ssl_connect_data * const connssl = cf->ctx;
1198 const uint8_t *protocol = NULL;
1199 size_t len = 0;
1201 rustls_connection_get_alpn_protocol(rconn, &protocol, &len);
1202 Curl_alpn_set_negotiated(cf, data, connssl, protocol, len);
1205/* Given an established network connection, do a TLS handshake.
1207 * This function sets `*done` to true once the handshake is complete.
1208 * This function never reads the value of `*done*`.
1209 */
1210static CURLcode cr_connect(struct Curl_cfilter *cf, struct Curl_easy *data,
1211 bool *done)
1213 struct ssl_connect_data * const connssl = cf->ctx;
1214 const struct rustls_ssl_backend_data * const backend =
1215 (struct rustls_ssl_backend_data *)connssl->backend;
1216 const struct rustls_connection *rconn = NULL;
1217 CURLcode tmperr = CURLE_OK;
1218 CURLcode result;
1219 bool wants_read;
1220 bool wants_write;
1221 ssize_t nread;
1223 DEBUGASSERT(backend);
1225 CURL_TRC_CF(data, cf, "cr_connect, state=%d", (int)connssl->state);
1226 *done = FALSE;
1228#ifdef USE_ECH
1229 /* if we do ECH and need the HTTPS-RR information for it,
1230 * we delay the connect until it arrives or DNS resolve fails. */
1231 if(Curl_ssl_need_httpsrr(cf) &&
1232 !Curl_conn_dns_resolved_https(data, cf->sockindex,
1233 connssl->peer.peer)) {
1234 CURL_TRC_CF(data, cf, "need HTTPS-RR for ECH, delaying connect");
1235 return CURLE_OK;
1237#endif /* USE_ECH */
1239 if(!backend->conn) {
1240 result =
1241 cr_init_backend(cf, data,
1242 (struct rustls_ssl_backend_data *)connssl->backend);
1243 CURL_TRC_CF(data, cf, "cr_connect, init backend -> %d", (int)result);
1244 if(result)
1245 return result;
1246 connssl->state = ssl_connection_negotiating;
1248 rconn = backend->conn;
1250 /* Read/write data until the handshake is done or the socket would block. */
1251 for(;;) {
1252 /*
1253 * Connection has been established according to Rustls. Set send/recv
1254 * handlers, and update the state machine.
1255 */
1256 connssl->io_need = CURL_SSL_IO_NEED_NONE;
1257 if(!rustls_connection_is_handshaking(rconn)) {
1258 /* Rustls claims it is no longer handshaking *before* it has
1259 * send its FINISHED message off. We attempt to let it write
1260 * one more time. Oh my.
1261 */
1262 size_t nwritten;
1263 cr_set_negotiated_alpn(cf, data, rconn);
1264 tmperr = cr_send(cf, data, NULL, 0, &nwritten);
1265 if(tmperr == CURLE_AGAIN) {
1266 connssl->io_need = CURL_SSL_IO_NEED_SEND;
1267 return CURLE_OK;
1269 else if(tmperr != CURLE_OK) {
1270 return tmperr;
1272 /* REALLY Done with the handshake. */
1274#ifdef CURLVERBOSE
1275 const uint16_t proto = rustls_connection_get_protocol_version(rconn);
1276 const rustls_str ciphersuite_name =
1277 rustls_connection_get_negotiated_ciphersuite_name(rconn);
1278 const rustls_str kex_group_name =
1279 rustls_connection_get_negotiated_key_exchange_group_name(rconn);
1280 const char *ver = "TLS version unknown";
1281 if(proto == RUSTLS_TLS_VERSION_TLSV1_3)
1282 ver = "TLSv1.3";
1283 if(proto == RUSTLS_TLS_VERSION_TLSV1_2)
1284 ver = "TLSv1.2";
1285#endif
1286 infof(data,
1287 "rustls: handshake complete, %s, ciphersuite: %.*s, "
1288 "key exchange group: %.*s",
1289 ver,
1290 (int)ciphersuite_name.len,
1291 ciphersuite_name.data,
1292 (int)kex_group_name.len,
1293 kex_group_name.data);
1295 if(data->set.ssl.certinfo) {
1296 size_t num_certs = 0;
1297 size_t i;
1298 while(rustls_connection_get_peer_certificate(rconn, num_certs)) {
1299 num_certs++;
1300 if(num_certs > MAX_ALLOWED_CERT_AMOUNT) {
1301 failf(data, "%zu certificates is more than allowed (%d)",
1302 num_certs, MAX_ALLOWED_CERT_AMOUNT);
1303 return CURLE_SSL_CONNECT_ERROR;
1306 result = Curl_ssl_init_certinfo(data, (int)num_certs);
1307 if(result)
1308 return result;
1309 for(i = 0; i < num_certs; i++) {
1310 const rustls_certificate *cert;
1311 const unsigned char *der_data;
1312 size_t der_len;
1313 rustls_result rresult = RUSTLS_RESULT_OK;
1314 cert = rustls_connection_get_peer_certificate(rconn, i);
1315 DEBUGASSERT(cert); /* Should exist since we counted already */
1316 rresult = rustls_certificate_get_der(cert, &der_data, &der_len);
1317 if(rresult != RUSTLS_RESULT_OK) {
1318 char errorbuf[255];
1319 size_t errorlen;
1320 rustls_error(rresult, errorbuf, sizeof(errorbuf), &errorlen);
1321 failf(data,
1322 "Failed getting DER of server certificate #%zu: %.*s", i,
1323 (int)errorlen, errorbuf);
1324 return map_error(rresult);
1326 result = Curl_extract_certinfo(data, (int)i,
1327 der_data, der_data + der_len);
1328 if(result)
1329 return result;
1333 connssl->state = ssl_connection_complete;
1334 *done = TRUE;
1335 return CURLE_OK;
1338 connssl->connecting_state = ssl_connect_2;
1339 wants_read = rustls_connection_wants_read(rconn);
1340 wants_write = rustls_connection_wants_write(rconn);
1341 DEBUGASSERT(wants_read || wants_write);
1343 if(wants_write) {
1344 size_t nwritten;
1345 CURL_TRC_CF(data, cf, "rustls_connection wants us to write_tls.");
1346 tmperr = cr_send(cf, data, NULL, 0, &nwritten);
1347 if(tmperr == CURLE_AGAIN) {
1348 CURL_TRC_CF(data, cf, "writing would block");
1349 connssl->io_need = CURL_SSL_IO_NEED_SEND;
1350 return CURLE_OK;
1352 else if(tmperr != CURLE_OK) {
1353 return tmperr;
1357 if(wants_read) {
1358 CURL_TRC_CF(data, cf, "rustls_connection wants us to read_tls.");
1359 nread = tls_recv_more(cf, data, &tmperr);
1360 if(nread == 0) {
1361 connssl->peer_closed = TRUE;
1362 failf(data, "TLS connect error: Connection closed abruptly");
1363 return CURLE_SSL_CONNECT_ERROR;
1365 if(nread < 0) {
1366 if(tmperr == CURLE_AGAIN) {
1367 CURL_TRC_CF(data, cf, "reading would block");
1368 connssl->io_need = CURL_SSL_IO_NEED_RECV;
1369 return CURLE_OK;
1371 else if(tmperr == CURLE_RECV_ERROR) {
1372 return CURLE_SSL_CONNECT_ERROR;
1374 else {
1375 return tmperr;
1381 /* We should never fall through the loop. We should return either because
1382 the handshake is done or because we cannot read/write without blocking. */
1383 DEBUGASSERT(FALSE);
1386static void *cr_get_internals(struct ssl_connect_data *connssl, CURLINFO info)
1388 struct rustls_ssl_backend_data *backend =
1389 (struct rustls_ssl_backend_data *)connssl->backend;
1390 (void)info;
1391 DEBUGASSERT(backend);
1392 return backend->conn;
1395static CURLcode cr_shutdown(struct Curl_cfilter *cf, struct Curl_easy *data,
1396 const bool send_shutdown, bool *done)
1398 struct ssl_connect_data *connssl = cf->ctx;
1399 struct rustls_ssl_backend_data *backend =
1400 (struct rustls_ssl_backend_data *)connssl->backend;
1401 CURLcode result = CURLE_OK;
1402 size_t i, nread = 0, nwritten;
1404 DEBUGASSERT(backend);
1405 if(!backend->conn || cf->shutdown) {
1406 *done = TRUE;
1407 goto out;
1410 connssl->io_need = CURL_SSL_IO_NEED_NONE;
1411 *done = FALSE;
1413 if(!backend->sent_shutdown) {
1414 /* do this only once */
1415 backend->sent_shutdown = TRUE;
1416 if(send_shutdown) {
1417 rustls_connection_send_close_notify(backend->conn);
1421 result = cr_send(cf, data, NULL, 0, &nwritten);
1422 if(result) {
1423 if(result == CURLE_AGAIN) {
1424 connssl->io_need = CURL_SSL_IO_NEED_SEND;
1425 result = CURLE_OK;
1426 goto out;
1428 DEBUGASSERT(result);
1429 CURL_TRC_CF(data, cf, "shutdown send failed: %d", (int)result);
1430 goto out;
1433 for(i = 0; i < 10; ++i) {
1434 char buf[1024];
1435 result = cr_recv(cf, data, buf, (int)sizeof(buf), &nread);
1436 if(result)
1437 break;
1440 if(result == CURLE_AGAIN) {
1441 connssl->io_need = CURL_SSL_IO_NEED_RECV;
1442 result = CURLE_OK;
1444 else if(result) {
1445 DEBUGASSERT(result);
1446 CURL_TRC_CF(data, cf, "shutdown, error: %d", (int)result);
1448 else if(nread == 0) {
1449 /* We got the close notify alert and are done. */
1450 *done = TRUE;
1453out:
1454 cf->shutdown = (result || *done);
1455 return result;
1458static void cr_close(struct Curl_cfilter *cf, struct Curl_easy *data)
1460 const struct ssl_connect_data *connssl = cf->ctx;
1461 struct rustls_ssl_backend_data *backend =
1462 (struct rustls_ssl_backend_data *)connssl->backend;
1464 (void)data;
1465 DEBUGASSERT(backend);
1466 if(backend->conn) {
1467 rustls_connection_free(backend->conn);
1468 backend->conn = NULL;
1470 if(backend->config) {
1471 rustls_client_config_free(backend->config);
1472 backend->config = NULL;
1476static size_t cr_version(char *buffer, size_t size)
1478 const struct rustls_str ver = rustls_version();
1479 return curl_msnprintf(buffer, size, "%.*s", (int)ver.len, ver.data);
1482static CURLcode cr_random(struct Curl_easy *data, unsigned char *entropy,
1483 size_t length)
1485 rustls_result rresult = 0;
1486 (void)data;
1487 rresult = rustls_default_crypto_provider_random(entropy, length);
1488 return map_error(rresult);
1491static void cr_cleanup(void)
1493 Curl_tls_keylog_close();
1496const struct Curl_ssl Curl_ssl_rustls = {
1497 { CURLSSLBACKEND_RUSTLS, "rustls" },
1498 SSLSUPP_CAINFO_BLOB | /* supports */
1499 SSLSUPP_HTTPS_PROXY |
1500 SSLSUPP_CIPHER_LIST |
1501 SSLSUPP_TLS13_CIPHERSUITES |
1502 SSLSUPP_CERTINFO |
1503 SSLSUPP_ECH |
1504 SSLSUPP_CRLFILE,
1505 sizeof(struct rustls_ssl_backend_data),
1507 NULL, /* init */
1508 cr_cleanup, /* cleanup */
1509 cr_version, /* version */
1510 cr_shutdown, /* shutdown */
1511 cr_data_pending, /* data_pending */
1512 cr_random, /* random */
1513 NULL, /* cert_status_request */
1514 cr_connect, /* connect */
1515 Curl_ssl_adjust_pollset, /* adjust_pollset */
1516 cr_get_internals, /* get_internals */
1517 cr_close, /* close_one */
1518 NULL, /* close_all */
1519 NULL, /* engines_list */
1520 NULL, /* sha256sum */
1521 cr_recv, /* recv decrypted data */
1522 cr_send, /* send data to encrypt */
1523 NULL, /* get_channel_binding */
1526#endif /* USE_RUSTLS */
Diagnostics4 git calls · 141 ms
CallStatusSizeTime0141 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 · hit3 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=3
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
fetch20012 KiB135 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=135
git.response.bytes=12277
git.response.cache=MISS
git.response.duration_ms=135
git.fetch.wants=642e63a28b058621b3f92aebb178ebfc436aa494
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=12277
git.pkt.payload.max=12256
git.sideband.band1.bytes=12255
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
a5e30fa3eef3bdc9
Request
b598f410…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format