| 1 |
/*************************************************************************** |
| 2 |
* _ _ ____ _ |
| 3 |
* Project ___| | | | _ \| | |
| 4 |
* / __| | | | |_) | | |
| 5 |
* | (__| |_| | _ <| |___ |
| 6 |
* \___|\___/|_| \_\_____| |
| 7 |
* |
| 8 |
* Copyright (C) Jacob Hoffman-Andrews, |
| 9 |
* <[email protected]> |
| 10 |
* Copyright (C) kpcyrd, <[email protected]> |
| 11 |
* Copyright (C) Daniel McCarney, <[email protected]> |
| 12 |
* |
| 13 |
* This software is licensed as described in the file COPYING, which |
| 14 |
* you should have received as part of this distribution. The terms |
| 15 |
* are also available at https://curl.se/docs/copyright.html. |
| 16 |
* |
| 17 |
* You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 18 |
* copies of the Software, and permit persons to whom the Software is |
| 19 |
* furnished to do so, under the terms of the COPYING file. |
| 20 |
* |
| 21 |
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 22 |
* KIND, either express or implied. |
| 23 |
* |
| 24 |
* SPDX-License-Identifier: curl |
| 25 |
* |
| 26 |
***************************************************************************/ |
| 27 |
#include "../curl_setup.h" |
| 28 |
|
| 29 |
#ifdef USE_RUSTLS |
| 30 |
|
| 31 |
#include "../curl_printf.h" |
| 32 |
|
| 33 |
#include <rustls.h> |
| 34 |
|
| 35 |
#include "../curlx/inet_pton.h" |
| 36 |
#include "../urldata.h" |
| 37 |
#include "../sendf.h" |
| 38 |
#include "vtls.h" |
| 39 |
#include "vtls_int.h" |
| 40 |
#include "rustls.h" |
| 41 |
#include "keylog.h" |
| 42 |
#include "../strerror.h" |
| 43 |
#include "cipher_suite.h" |
| 44 |
#include "x509asn1.h" |
| 45 |
|
| 46 |
struct rustls_ssl_backend_data |
| 47 |
{ |
| 48 |
const struct rustls_client_config *config; |
| 49 |
struct rustls_connection *conn; |
| 50 |
size_t plain_out_buffered; |
| 51 |
BIT(data_in_pending); |
| 52 |
BIT(sent_shutdown); |
| 53 |
}; |
| 54 |
|
| 55 |
/* For a given rustls_result error code, return the best-matching CURLcode. */ |
| 56 |
static CURLcode map_error(const rustls_result r) |
| 57 |
{ |
| 58 |
if(rustls_result_is_cert_error(r)) { |
| 59 |
return CURLE_PEER_FAILED_VERIFICATION; |
| 60 |
} |
| 61 |
switch(r) { |
| 62 |
case RUSTLS_RESULT_OK: |
| 63 |
return CURLE_OK; |
| 64 |
case RUSTLS_RESULT_NULL_PARAMETER: |
| 65 |
return CURLE_BAD_FUNCTION_ARGUMENT; |
| 66 |
default: |
| 67 |
return CURLE_RECV_ERROR; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
static void |
| 72 |
rustls_failf(struct Curl_easy *data, const rustls_result rr, const char *msg) |
| 73 |
{ |
| 74 |
char errorbuf[STRERROR_LEN]; |
| 75 |
size_t errorlen; |
| 76 |
rustls_error(rr, errorbuf, sizeof(errorbuf), &errorlen); |
| 77 |
failf(data, "%s: %.*s", msg, (int)errorlen, errorbuf); |
| 78 |
} |
| 79 |
|
| 80 |
static bool |
| 81 |
cr_data_pending(struct Curl_cfilter *cf, const struct Curl_easy *data) |
| 82 |
{ |
| 83 |
const struct ssl_connect_data *ctx = cf->ctx; |
| 84 |
struct rustls_ssl_backend_data *backend; |
| 85 |
|
| 86 |
(void)data; |
| 87 |
DEBUGASSERT(ctx && ctx->backend); |
| 88 |
backend = (struct rustls_ssl_backend_data *)ctx->backend; |
| 89 |
return backend->data_in_pending; |
| 90 |
} |
| 91 |
|
| 92 |
struct io_ctx { |
| 93 |
struct Curl_cfilter *cf; |
| 94 |
struct Curl_easy *data; |
| 95 |
}; |
| 96 |
|
| 97 |
static int |
| 98 |
read_cb(void *userdata, uint8_t *buf, uintptr_t len, uintptr_t *out_n) |
| 99 |
{ |
| 100 |
const struct io_ctx *io_ctx = userdata; |
| 101 |
struct ssl_connect_data *const connssl = io_ctx->cf->ctx; |
| 102 |
CURLcode result; |
| 103 |
int ret = 0; |
| 104 |
ssize_t nread = Curl_conn_cf_recv(io_ctx->cf->next, io_ctx->data, |
| 105 |
(char *)buf, len, &result); |
| 106 |
if(nread < 0) { |
| 107 |
nread = 0; |
| 108 |
/* !checksrc! disable ERRNOVAR 4 */ |
| 109 |
if(CURLE_AGAIN == result) |
| 110 |
ret = EAGAIN; |
| 111 |
else |
| 112 |
ret = EINVAL; |
| 113 |
} |
| 114 |
else if(nread == 0) |
| 115 |
connssl->peer_closed = TRUE; |
| 116 |
*out_n = (uintptr_t)nread; |
| 117 |
CURL_TRC_CF(io_ctx->data, io_ctx->cf, "cf->next recv(len=%zu) -> %zd, %d", |
| 118 |
len, nread, result); |
| 119 |
return ret; |
| 120 |
} |
| 121 |
|
| 122 |
static int |
| 123 |
write_cb(void *userdata, const uint8_t *buf, uintptr_t len, uintptr_t *out_n) |
| 124 |
{ |
| 125 |
const struct io_ctx *io_ctx = userdata; |
| 126 |
CURLcode result; |
| 127 |
int ret = 0; |
| 128 |
ssize_t nwritten = Curl_conn_cf_send(io_ctx->cf->next, io_ctx->data, |
| 129 |
(const char *)buf, len, FALSE, |
| 130 |
&result); |
| 131 |
if(nwritten < 0) { |
| 132 |
nwritten = 0; |
| 133 |
if(CURLE_AGAIN == result) |
| 134 |
ret = EAGAIN; |
| 135 |
else |
| 136 |
ret = EINVAL; |
| 137 |
} |
| 138 |
*out_n = (uintptr_t)nwritten; |
| 139 |
CURL_TRC_CF(io_ctx->data, io_ctx->cf, "cf->next send(len=%zu) -> %zd, %d", |
| 140 |
len, nwritten, result); |
| 141 |
return ret; |
| 142 |
} |
| 143 |
|
| 144 |
static ssize_t tls_recv_more(struct Curl_cfilter *cf, |
| 145 |
struct Curl_easy *data, CURLcode *err) |
| 146 |
{ |
| 147 |
const struct ssl_connect_data *const connssl = cf->ctx; |
| 148 |
struct rustls_ssl_backend_data *const backend = |
| 149 |
(struct rustls_ssl_backend_data *)connssl->backend; |
| 150 |
struct io_ctx io_ctx; |
| 151 |
size_t tls_bytes_read = 0; |
| 152 |
rustls_io_result io_error; |
| 153 |
rustls_result rresult = 0; |
| 154 |
|
| 155 |
io_ctx.cf = cf; |
| 156 |
io_ctx.data = data; |
| 157 |
io_error = rustls_connection_read_tls(backend->conn, read_cb, &io_ctx, |
| 158 |
&tls_bytes_read); |
| 159 |
if(io_error == EAGAIN || io_error == EWOULDBLOCK) { |
| 160 |
*err = CURLE_AGAIN; |
| 161 |
return -1; |
| 162 |
} |
| 163 |
else if(io_error) { |
| 164 |
char buffer[STRERROR_LEN]; |
| 165 |
failf(data, "reading from socket: %s", |
| 166 |
Curl_strerror(io_error, buffer, sizeof(buffer))); |
| 167 |
*err = CURLE_RECV_ERROR; |
| 168 |
return -1; |
| 169 |
} |
| 170 |
|
| 171 |
rresult = rustls_connection_process_new_packets(backend->conn); |
| 172 |
if(rresult != RUSTLS_RESULT_OK) { |
| 173 |
rustls_failf(data, rresult, "rustls_connection_process_new_packets"); |
| 174 |
*err = map_error(rresult); |
| 175 |
return -1; |
| 176 |
} |
| 177 |
|
| 178 |
backend->data_in_pending = TRUE; |
| 179 |
*err = CURLE_OK; |
| 180 |
return (ssize_t)tls_bytes_read; |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
* On each run: |
| 185 |
* - Read a chunk of bytes from the socket into Rustls' TLS input buffer. |
| 186 |
* - Tell Rustls to process any new packets. |
| 187 |
* - Read out as many plaintext bytes from Rustls as possible, until hitting |
| 188 |
* error, EOF, or EAGAIN/EWOULDBLOCK, or plainbuf/plainlen is filled up. |
| 189 |
* |
| 190 |
* it is okay to call this function with plainbuf == NULL and plainlen == 0. In |
| 191 |
* that case, it will copy bytes from the socket into Rustls' TLS input |
| 192 |
* buffer, and process packets, but will not consume bytes from Rustls' |
| 193 |
* plaintext output buffer. |
| 194 |
*/ |
| 195 |
static ssize_t |
| 196 |
cr_recv(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 197 |
char *plainbuf, size_t plainlen, CURLcode *err) |
| 198 |
{ |
| 199 |
const struct ssl_connect_data *const connssl = cf->ctx; |
| 200 |
struct rustls_ssl_backend_data *const backend = |
| 201 |
(struct rustls_ssl_backend_data *)connssl->backend; |
| 202 |
struct rustls_connection *rconn = NULL; |
| 203 |
size_t n = 0; |
| 204 |
size_t plain_bytes_copied = 0; |
| 205 |
rustls_result rresult = 0; |
| 206 |
ssize_t nread; |
| 207 |
bool eof = FALSE; |
| 208 |
|
| 209 |
DEBUGASSERT(backend); |
| 210 |
rconn = backend->conn; |
| 211 |
|
| 212 |
while(plain_bytes_copied < plainlen) { |
| 213 |
if(!backend->data_in_pending) { |
| 214 |
if(tls_recv_more(cf, data, err) < 0) { |
| 215 |
if(*err != CURLE_AGAIN) { |
| 216 |
nread = -1; |
| 217 |
goto out; |
| 218 |
} |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
rresult = rustls_connection_read(rconn, |
| 224 |
(uint8_t *)plainbuf + plain_bytes_copied, |
| 225 |
plainlen - plain_bytes_copied, |
| 226 |
&n); |
| 227 |
if(rresult == RUSTLS_RESULT_PLAINTEXT_EMPTY) { |
| 228 |
backend->data_in_pending = FALSE; |
| 229 |
} |
| 230 |
else if(rresult == RUSTLS_RESULT_UNEXPECTED_EOF) { |
| 231 |
failf(data, "rustls: peer closed TCP connection " |
| 232 |
"without first closing TLS connection"); |
| 233 |
*err = CURLE_RECV_ERROR; |
| 234 |
nread = -1; |
| 235 |
goto out; |
| 236 |
} |
| 237 |
else if(rresult != RUSTLS_RESULT_OK) { |
| 238 |
/* n always equals 0 in this case, do not need to check it */ |
| 239 |
rustls_failf(data, rresult, "rustls_connection_read"); |
| 240 |
*err = CURLE_RECV_ERROR; |
| 241 |
nread = -1; |
| 242 |
goto out; |
| 243 |
} |
| 244 |
else if(n == 0) { |
| 245 |
/* n == 0 indicates clean EOF, but we may have read some other |
| 246 |
plaintext bytes before we reached this. Break out of the loop |
| 247 |
so we can figure out whether to return success or EOF. */ |
| 248 |
eof = TRUE; |
| 249 |
break; |
| 250 |
} |
| 251 |
else { |
| 252 |
plain_bytes_copied += n; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if(plain_bytes_copied) { |
| 257 |
*err = CURLE_OK; |
| 258 |
nread = (ssize_t)plain_bytes_copied; |
| 259 |
} |
| 260 |
else if(eof) { |
| 261 |
*err = CURLE_OK; |
| 262 |
nread = 0; |
| 263 |
} |
| 264 |
else { |
| 265 |
*err = CURLE_AGAIN; |
| 266 |
nread = -1; |
| 267 |
} |
| 268 |
|
| 269 |
out: |
| 270 |
CURL_TRC_CF(data, cf, "cf_recv(len=%zu) -> %zd, %d", |
| 271 |
plainlen, nread, *err); |
| 272 |
return nread; |
| 273 |
} |
| 274 |
|
| 275 |
static CURLcode cr_flush_out(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 276 |
struct rustls_connection *rconn) |
| 277 |
{ |
| 278 |
struct io_ctx io_ctx; |
| 279 |
rustls_io_result io_error; |
| 280 |
size_t tlswritten = 0; |
| 281 |
size_t tlswritten_total = 0; |
| 282 |
|
| 283 |
io_ctx.cf = cf; |
| 284 |
io_ctx.data = data; |
| 285 |
|
| 286 |
while(rustls_connection_wants_write(rconn)) { |
| 287 |
io_error = rustls_connection_write_tls(rconn, write_cb, &io_ctx, |
| 288 |
&tlswritten); |
| 289 |
if(io_error == EAGAIN || io_error == EWOULDBLOCK) { |
| 290 |
CURL_TRC_CF(data, cf, "cf_send: EAGAIN after %zu bytes", |
| 291 |
tlswritten_total); |
| 292 |
return CURLE_AGAIN; |
| 293 |
} |
| 294 |
else if(io_error) { |
| 295 |
char buffer[STRERROR_LEN]; |
| 296 |
failf(data, "writing to socket: %s", |
| 297 |
Curl_strerror(io_error, buffer, sizeof(buffer))); |
| 298 |
return CURLE_SEND_ERROR; |
| 299 |
} |
| 300 |
if(tlswritten == 0) { |
| 301 |
failf(data, "EOF in swrite"); |
| 302 |
return CURLE_SEND_ERROR; |
| 303 |
} |
| 304 |
CURL_TRC_CF(data, cf, "cf_send: wrote %zu TLS bytes", tlswritten); |
| 305 |
tlswritten_total += tlswritten; |
| 306 |
} |
| 307 |
return CURLE_OK; |
| 308 |
} |
| 309 |
|
| 310 |
/* |
| 311 |
* On each call: |
| 312 |
* - Copy `plainlen` bytes into Rustls' plaintext input buffer (if > 0). |
| 313 |
* - Fully drain Rustls' plaintext output buffer into the socket until |
| 314 |
* we get either an error or EAGAIN/EWOULDBLOCK. |
| 315 |
* |
| 316 |
* it is okay to call this function with plainbuf == NULL and plainlen == 0. |
| 317 |
* In that case, it will not read anything into Rustls' plaintext input buffer. |
| 318 |
* It will only drain Rustls' plaintext output buffer into the socket. |
| 319 |
*/ |
| 320 |
static ssize_t |
| 321 |
cr_send(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 322 |
const void *plainbuf, size_t plainlen, CURLcode *err) |
| 323 |
{ |
| 324 |
const struct ssl_connect_data *const connssl = cf->ctx; |
| 325 |
struct rustls_ssl_backend_data *const backend = |
| 326 |
(struct rustls_ssl_backend_data *)connssl->backend; |
| 327 |
struct rustls_connection *rconn = NULL; |
| 328 |
size_t plainwritten = 0; |
| 329 |
const unsigned char *buf = plainbuf; |
| 330 |
size_t blen = plainlen; |
| 331 |
ssize_t nwritten = 0; |
| 332 |
|
| 333 |
DEBUGASSERT(backend); |
| 334 |
rconn = backend->conn; |
| 335 |
DEBUGASSERT(rconn); |
| 336 |
|
| 337 |
CURL_TRC_CF(data, cf, "cf_send(len=%zu)", plainlen); |
| 338 |
|
| 339 |
/* If a previous send blocked, we already added its plain bytes |
| 340 |
* to rustsls and must not do that again. Flush the TLS bytes and, |
| 341 |
* if successful, deduct the previous plain bytes from the current |
| 342 |
* send. */ |
| 343 |
if(backend->plain_out_buffered) { |
| 344 |
*err = cr_flush_out(cf, data, rconn); |
| 345 |
CURL_TRC_CF(data, cf, "cf_send: flushing %zu previously added bytes -> %d", |
| 346 |
backend->plain_out_buffered, *err); |
| 347 |
if(*err) |
| 348 |
return -1; |
| 349 |
if(blen > backend->plain_out_buffered) { |
| 350 |
blen -= backend->plain_out_buffered; |
| 351 |
buf += backend->plain_out_buffered; |
| 352 |
} |
| 353 |
else |
| 354 |
blen = 0; |
| 355 |
nwritten += (ssize_t)backend->plain_out_buffered; |
| 356 |
backend->plain_out_buffered = 0; |
| 357 |
} |
| 358 |
|
| 359 |
if(blen > 0) { |
| 360 |
rustls_result rresult; |
| 361 |
CURL_TRC_CF(data, cf, "cf_send: adding %zu plain bytes to Rustls", blen); |
| 362 |
rresult = rustls_connection_write(rconn, buf, blen, &plainwritten); |
| 363 |
if(rresult != RUSTLS_RESULT_OK) { |
| 364 |
rustls_failf(data, rresult, "rustls_connection_write"); |
| 365 |
*err = CURLE_WRITE_ERROR; |
| 366 |
return -1; |
| 367 |
} |
| 368 |
else if(plainwritten == 0) { |
| 369 |
failf(data, "rustls_connection_write: EOF"); |
| 370 |
*err = CURLE_WRITE_ERROR; |
| 371 |
return -1; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
*err = cr_flush_out(cf, data, rconn); |
| 376 |
if(*err) { |
| 377 |
if(CURLE_AGAIN == *err) { |
| 378 |
/* The TLS bytes may have been partially written, but we fail the |
| 379 |
* complete send() and remember how much we already added to Rustls. */ |
| 380 |
CURL_TRC_CF(data, cf, "cf_send: EAGAIN, remember we added %zu plain" |
| 381 |
" bytes already to Rustls", blen); |
| 382 |
backend->plain_out_buffered = plainwritten; |
| 383 |
if(nwritten) { |
| 384 |
*err = CURLE_OK; |
| 385 |
return (ssize_t)nwritten; |
| 386 |
} |
| 387 |
} |
| 388 |
return -1; |
| 389 |
} |
| 390 |
else |
| 391 |
nwritten += (ssize_t)plainwritten; |
| 392 |
|
| 393 |
CURL_TRC_CF(data, cf, "cf_send(len=%zu) -> %d, %zd", |
| 394 |
plainlen, *err, nwritten); |
| 395 |
return nwritten; |
| 396 |
} |
| 397 |
|
| 398 |
/* A server certificate verify callback for Rustls that always returns |
| 399 |
RUSTLS_RESULT_OK, or in other words disable certificate verification. */ |
| 400 |
static uint32_t |
| 401 |
cr_verify_none(void *userdata UNUSED_PARAM, |
| 402 |
const rustls_verify_server_cert_params *params UNUSED_PARAM) |
| 403 |
{ |
| 404 |
return RUSTLS_RESULT_OK; |
| 405 |
} |
| 406 |
|
| 407 |
static int |
| 408 |
read_file_into(const char *filename, |
| 409 |
struct dynbuf *out) |
| 410 |
{ |
| 411 |
FILE *f = fopen(filename, FOPEN_READTEXT); |
| 412 |
if(!f) { |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
while(!feof(f)) { |
| 417 |
uint8_t buf[256]; |
| 418 |
const size_t rr = fread(buf, 1, sizeof(buf), f); |
| 419 |
if(rr == 0 || |
| 420 |
CURLE_OK != curlx_dyn_addn(out, buf, rr)) { |
| 421 |
fclose(f); |
| 422 |
return 0; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
return fclose(f) == 0; |
| 427 |
} |
| 428 |
|
| 429 |
static void |
| 430 |
cr_get_selected_ciphers(struct Curl_easy *data, |
| 431 |
const char *ciphers12, |
| 432 |
const char *ciphers13, |
| 433 |
const struct rustls_supported_ciphersuite **selected, |
| 434 |
size_t *selected_size) |
| 435 |
{ |
| 436 |
const size_t supported_len = *selected_size; |
| 437 |
const size_t default_len = rustls_default_crypto_provider_ciphersuites_len(); |
| 438 |
const struct rustls_supported_ciphersuite *entry; |
| 439 |
const char *ciphers = ciphers12; |
| 440 |
size_t count = 0, default13_count = 0, i, j; |
| 441 |
const char *ptr, *end; |
| 442 |
|
| 443 |
DEBUGASSERT(default_len <= supported_len); |
| 444 |
|
| 445 |
if(!ciphers13) { |
| 446 |
/* Add default TLSv1.3 ciphers to selection */ |
| 447 |
for(j = 0; j < default_len; j++) { |
| 448 |
entry = rustls_default_crypto_provider_ciphersuites_get(j); |
| 449 |
if(rustls_supported_ciphersuite_protocol_version(entry) != |
| 450 |
RUSTLS_TLS_VERSION_TLSV1_3) |
| 451 |
continue; |
| 452 |
|
| 453 |
selected[count++] = entry; |
| 454 |
} |
| 455 |
|
| 456 |
default13_count = count; |
| 457 |
|
| 458 |
if(!ciphers) |
| 459 |
ciphers = ""; |
| 460 |
} |
| 461 |
else |
| 462 |
ciphers = ciphers13; |
| 463 |
|
| 464 |
add_ciphers: |
| 465 |
for(ptr = ciphers; ptr[0] != '\0' && count < supported_len; ptr = end) { |
| 466 |
uint16_t id = Curl_cipher_suite_walk_str(&ptr, &end); |
| 467 |
|
| 468 |
/* Check if cipher is supported */ |
| 469 |
if(id) { |
| 470 |
for(i = 0; i < supported_len; i++) { |
| 471 |
entry = rustls_default_crypto_provider_ciphersuites_get(i); |
| 472 |
if(rustls_supported_ciphersuite_get_suite(entry) == id) |
| 473 |
break; |
| 474 |
} |
| 475 |
if(i == supported_len) |
| 476 |
id = 0; |
| 477 |
} |
| 478 |
if(!id) { |
| 479 |
if(ptr[0] != '\0') |
| 480 |
infof(data, "rustls: unknown cipher in list: \"%.*s\"", |
| 481 |
(int) (end - ptr), ptr); |
| 482 |
continue; |
| 483 |
} |
| 484 |
|
| 485 |
/* No duplicates allowed (so selected cannot overflow) */ |
| 486 |
for(i = 0; i < count && selected[i] != entry; i++); |
| 487 |
if(i < count) { |
| 488 |
if(i >= default13_count) |
| 489 |
infof(data, "rustls: duplicate cipher in list: \"%.*s\"", |
| 490 |
(int) (end - ptr), ptr); |
| 491 |
continue; |
| 492 |
} |
| 493 |
|
| 494 |
selected[count++] = entry; |
| 495 |
} |
| 496 |
|
| 497 |
if(ciphers == ciphers13 && ciphers12) { |
| 498 |
ciphers = ciphers12; |
| 499 |
goto add_ciphers; |
| 500 |
} |
| 501 |
|
| 502 |
if(!ciphers12) { |
| 503 |
/* Add default TLSv1.2 ciphers to selection */ |
| 504 |
for(j = 0; j < default_len; j++) { |
| 505 |
entry = rustls_default_crypto_provider_ciphersuites_get(j); |
| 506 |
if(rustls_supported_ciphersuite_protocol_version(entry) == |
| 507 |
RUSTLS_TLS_VERSION_TLSV1_3) |
| 508 |
continue; |
| 509 |
|
| 510 |
/* No duplicates allowed (so selected cannot overflow) */ |
| 511 |
for(i = 0; i < count && selected[i] != entry; i++); |
| 512 |
if(i < count) |
| 513 |
continue; |
| 514 |
|
| 515 |
selected[count++] = entry; |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
*selected_size = count; |
| 520 |
} |
| 521 |
|
| 522 |
static void |
| 523 |
cr_keylog_log_cb(struct rustls_str label, |
| 524 |
const uint8_t *client_random, size_t client_random_len, |
| 525 |
const uint8_t *secret, size_t secret_len) |
| 526 |
{ |
| 527 |
char clabel[KEYLOG_LABEL_MAXLEN]; |
| 528 |
(void)client_random_len; |
| 529 |
DEBUGASSERT(client_random_len == CLIENT_RANDOM_SIZE); |
| 530 |
/* Turning a "rustls_str" into a null delimited "c" string */ |
| 531 |
msnprintf(clabel, label.len + 1, "%.*s", (int)label.len, label.data); |
| 532 |
Curl_tls_keylog_write(clabel, client_random, secret, secret_len); |
| 533 |
} |
| 534 |
|
| 535 |
static CURLcode |
| 536 |
init_config_builder(struct Curl_easy *data, |
| 537 |
const struct ssl_primary_config *conn_config, |
| 538 |
struct rustls_client_config_builder **config_builder) |
| 539 |
{ |
| 540 |
const struct rustls_supported_ciphersuite **cipher_suites = NULL; |
| 541 |
struct rustls_crypto_provider_builder *custom_provider_builder = NULL; |
| 542 |
const struct rustls_crypto_provider *custom_provider = NULL; |
| 543 |
|
| 544 |
uint16_t tls_versions[2] = { |
| 545 |
RUSTLS_TLS_VERSION_TLSV1_2, |
| 546 |
RUSTLS_TLS_VERSION_TLSV1_3, |
| 547 |
}; |
| 548 |
size_t tls_versions_len = 2; |
| 549 |
size_t cipher_suites_len = |
| 550 |
rustls_default_crypto_provider_ciphersuites_len(); |
| 551 |
|
| 552 |
CURLcode result = CURLE_OK; |
| 553 |
rustls_result rr; |
| 554 |
|
| 555 |
switch(conn_config->version) { |
| 556 |
case CURL_SSLVERSION_DEFAULT: |
| 557 |
case CURL_SSLVERSION_TLSv1: |
| 558 |
case CURL_SSLVERSION_TLSv1_0: |
| 559 |
case CURL_SSLVERSION_TLSv1_1: |
| 560 |
case CURL_SSLVERSION_TLSv1_2: |
| 561 |
break; |
| 562 |
case CURL_SSLVERSION_TLSv1_3: |
| 563 |
tls_versions[0] = RUSTLS_TLS_VERSION_TLSV1_3; |
| 564 |
tls_versions_len = 1; |
| 565 |
break; |
| 566 |
default: |
| 567 |
failf(data, "rustls: unsupported minimum TLS version value"); |
| 568 |
result = CURLE_BAD_FUNCTION_ARGUMENT; |
| 569 |
goto cleanup; |
| 570 |
} |
| 571 |
|
| 572 |
switch(conn_config->version_max) { |
| 573 |
case CURL_SSLVERSION_MAX_DEFAULT: |
| 574 |
case CURL_SSLVERSION_MAX_NONE: |
| 575 |
case CURL_SSLVERSION_MAX_TLSv1_3: |
| 576 |
break; |
| 577 |
case CURL_SSLVERSION_MAX_TLSv1_2: |
| 578 |
if(tls_versions[0] == RUSTLS_TLS_VERSION_TLSV1_2) { |
| 579 |
tls_versions_len = 1; |
| 580 |
break; |
| 581 |
} |
| 582 |
FALLTHROUGH(); |
| 583 |
case CURL_SSLVERSION_MAX_TLSv1_1: |
| 584 |
case CURL_SSLVERSION_MAX_TLSv1_0: |
| 585 |
default: |
| 586 |
failf(data, "rustls: unsupported maximum TLS version value"); |
| 587 |
result = CURLE_BAD_FUNCTION_ARGUMENT; |
| 588 |
goto cleanup; |
| 589 |
} |
| 590 |
|
| 591 |
#if defined(USE_ECH) |
| 592 |
if(ECH_ENABLED(data)) { |
| 593 |
tls_versions[0] = RUSTLS_TLS_VERSION_TLSV1_3; |
| 594 |
tls_versions_len = 1; |
| 595 |
infof(data, "rustls: ECH enabled, forcing TLSv1.3"); |
| 596 |
} |
| 597 |
#endif /* USE_ECH */ |
| 598 |
|
| 599 |
cipher_suites = malloc(sizeof(cipher_suites) * (cipher_suites_len)); |
| 600 |
if(!cipher_suites) { |
| 601 |
result = CURLE_OUT_OF_MEMORY; |
| 602 |
goto cleanup; |
| 603 |
} |
| 604 |
|
| 605 |
cr_get_selected_ciphers(data, |
| 606 |
conn_config->cipher_list, |
| 607 |
conn_config->cipher_list13, |
| 608 |
cipher_suites, &cipher_suites_len); |
| 609 |
if(cipher_suites_len == 0) { |
| 610 |
failf(data, "rustls: no supported cipher in list"); |
| 611 |
result = CURLE_SSL_CIPHER; |
| 612 |
goto cleanup; |
| 613 |
} |
| 614 |
|
| 615 |
rr = rustls_crypto_provider_builder_new_from_default( |
| 616 |
&custom_provider_builder); |
| 617 |
if(rr != RUSTLS_RESULT_OK) { |
| 618 |
rustls_failf(data, rr, |
| 619 |
"failed to create crypto provider builder from default"); |
| 620 |
result = CURLE_SSL_CIPHER; |
| 621 |
goto cleanup; |
| 622 |
} |
| 623 |
|
| 624 |
rr = |
| 625 |
rustls_crypto_provider_builder_set_cipher_suites( |
| 626 |
custom_provider_builder, |
| 627 |
cipher_suites, |
| 628 |
cipher_suites_len); |
| 629 |
if(rr != RUSTLS_RESULT_OK) { |
| 630 |
rustls_failf(data, rr, |
| 631 |
"failed to set ciphersuites for crypto provider builder"); |
| 632 |
result = CURLE_SSL_CIPHER; |
| 633 |
goto cleanup; |
| 634 |
} |
| 635 |
|
| 636 |
rr = rustls_crypto_provider_builder_build( |
| 637 |
custom_provider_builder, &custom_provider); |
| 638 |
if(rr != RUSTLS_RESULT_OK) { |
| 639 |
rustls_failf(data, rr, "failed to build custom crypto provider"); |
| 640 |
result = CURLE_SSL_CIPHER; |
| 641 |
goto cleanup; |
| 642 |
} |
| 643 |
|
| 644 |
rr = rustls_client_config_builder_new_custom(custom_provider, |
| 645 |
tls_versions, |
| 646 |
tls_versions_len, |
| 647 |
config_builder); |
| 648 |
if(rr != RUSTLS_RESULT_OK) { |
| 649 |
rustls_failf(data, rr, "failed to create client config builder"); |
| 650 |
result = CURLE_SSL_CIPHER; |
| 651 |
goto cleanup; |
| 652 |
} |
| 653 |
|
| 654 |
cleanup: |
| 655 |
if(cipher_suites) { |
| 656 |
free(cipher_suites); |
| 657 |
} |
| 658 |
if(custom_provider_builder) { |
| 659 |
rustls_crypto_provider_builder_free(custom_provider_builder); |
| 660 |
} |
| 661 |
if(custom_provider) { |
| 662 |
rustls_crypto_provider_free(custom_provider); |
| 663 |
} |
| 664 |
return result; |
| 665 |
} |
| 666 |
|
| 667 |
static void |
| 668 |
init_config_builder_alpn(struct Curl_easy *data, |
| 669 |
const struct ssl_connect_data *connssl, |
| 670 |
struct rustls_client_config_builder *config_builder) { |
| 671 |
struct alpn_proto_buf proto; |
| 672 |
rustls_slice_bytes alpn[ALPN_ENTRIES_MAX]; |
| 673 |
size_t i; |
| 674 |
|
| 675 |
for(i = 0; i < connssl->alpn->count; ++i) { |
| 676 |
alpn[i].data = (const uint8_t *)connssl->alpn->entries[i]; |
| 677 |
alpn[i].len = strlen(connssl->alpn->entries[i]); |
| 678 |
} |
| 679 |
rustls_client_config_builder_set_alpn_protocols(config_builder, alpn, |
| 680 |
connssl->alpn->count); |
| 681 |
Curl_alpn_to_proto_str(&proto, connssl->alpn); |
| 682 |
infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data); |
| 683 |
} |
| 684 |
|
| 685 |
static CURLcode |
| 686 |
init_config_builder_verifier_crl( |
| 687 |
struct Curl_easy *data, |
| 688 |
const struct ssl_primary_config *conn_config, |
| 689 |
struct rustls_web_pki_server_cert_verifier_builder *builder) |
| 690 |
{ |
| 691 |
CURLcode result = CURLE_OK; |
| 692 |
struct dynbuf crl_contents; |
| 693 |
rustls_result rr; |
| 694 |
|
| 695 |
curlx_dyn_init(&crl_contents, DYN_CRLFILE_SIZE); |
| 696 |
if(!read_file_into(conn_config->CRLfile, &crl_contents)) { |
| 697 |
failf(data, "rustls: failed to read revocation list file"); |
| 698 |
result = CURLE_SSL_CRL_BADFILE; |
| 699 |
goto cleanup; |
| 700 |
} |
| 701 |
|
| 702 |
rr = rustls_web_pki_server_cert_verifier_builder_add_crl( |
| 703 |
builder, |
| 704 |
curlx_dyn_uptr(&crl_contents), |
| 705 |
curlx_dyn_len(&crl_contents)); |
| 706 |
if(rr != RUSTLS_RESULT_OK) { |
| 707 |
rustls_failf(data, rr, "failed to parse revocation list"); |
| 708 |
result = CURLE_SSL_CRL_BADFILE; |
| 709 |
goto cleanup; |
| 710 |
} |
| 711 |
|
| 712 |
cleanup: |
| 713 |
curlx_dyn_free(&crl_contents); |
| 714 |
return result; |
| 715 |
} |
| 716 |
|
| 717 |
static CURLcode |
| 718 |
init_config_builder_verifier(struct Curl_easy *data, |
| 719 |
struct rustls_client_config_builder *builder, |
| 720 |
const struct ssl_primary_config *conn_config, |
| 721 |
const struct curl_blob *ca_info_blob, |
| 722 |
const char * const ssl_cafile) { |
| 723 |
const struct rustls_root_cert_store *roots = NULL; |
| 724 |
struct rustls_root_cert_store_builder *roots_builder = NULL; |
| 725 |
struct rustls_web_pki_server_cert_verifier_builder *verifier_builder = NULL; |
| 726 |
struct rustls_server_cert_verifier *server_cert_verifier = NULL; |
| 727 |
rustls_result rr = RUSTLS_RESULT_OK; |
| 728 |
CURLcode result = CURLE_OK; |
| 729 |
|
| 730 |
roots_builder = rustls_root_cert_store_builder_new(); |
| 731 |
if(ca_info_blob) { |
| 732 |
rr = rustls_root_cert_store_builder_add_pem(roots_builder, |
| 733 |
ca_info_blob->data, |
| 734 |
ca_info_blob->len, |
| 735 |
1); |
| 736 |
if(rr != RUSTLS_RESULT_OK) { |
| 737 |
rustls_failf(data, rr, "failed to parse trusted certificates from blob"); |
| 738 |
|
| 739 |
result = CURLE_SSL_CACERT_BADFILE; |
| 740 |
goto cleanup; |
| 741 |
} |
| 742 |
} |
| 743 |
else if(ssl_cafile) { |
| 744 |
rr = rustls_root_cert_store_builder_load_roots_from_file(roots_builder, |
| 745 |
ssl_cafile, |
| 746 |
1); |
| 747 |
if(rr != RUSTLS_RESULT_OK) { |
| 748 |
rustls_failf(data, rr, "failed to load trusted certificates"); |
| 749 |
|
| 750 |
result = CURLE_SSL_CACERT_BADFILE; |
| 751 |
goto cleanup; |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
rr = rustls_root_cert_store_builder_build(roots_builder, &roots); |
| 756 |
if(rr != RUSTLS_RESULT_OK) { |
| 757 |
rustls_failf(data, rr, "failed to build trusted root certificate store"); |
| 758 |
result = CURLE_SSL_CACERT_BADFILE; |
| 759 |
} |
| 760 |
|
| 761 |
verifier_builder = rustls_web_pki_server_cert_verifier_builder_new(roots); |
| 762 |
|
| 763 |
if(conn_config->CRLfile) { |
| 764 |
result = init_config_builder_verifier_crl(data, |
| 765 |
conn_config, |
| 766 |
verifier_builder); |
| 767 |
if(result != CURLE_OK) { |
| 768 |
goto cleanup; |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
rr = rustls_web_pki_server_cert_verifier_builder_build( |
| 773 |
verifier_builder, &server_cert_verifier); |
| 774 |
if(rr != RUSTLS_RESULT_OK) { |
| 775 |
rustls_failf(data, rr, "failed to build certificate verifier"); |
| 776 |
result = CURLE_SSL_CACERT_BADFILE; |
| 777 |
goto cleanup; |
| 778 |
} |
| 779 |
|
| 780 |
rustls_client_config_builder_set_server_verifier(builder, |
| 781 |
server_cert_verifier); |
| 782 |
cleanup: |
| 783 |
if(roots_builder) { |
| 784 |
rustls_root_cert_store_builder_free(roots_builder); |
| 785 |
} |
| 786 |
if(roots) { |
| 787 |
rustls_root_cert_store_free(roots); |
| 788 |
} |
| 789 |
if(verifier_builder) { |
| 790 |
rustls_web_pki_server_cert_verifier_builder_free(verifier_builder); |
| 791 |
} |
| 792 |
if(server_cert_verifier) { |
| 793 |
rustls_server_cert_verifier_free(server_cert_verifier); |
| 794 |
} |
| 795 |
|
| 796 |
return result; |
| 797 |
} |
| 798 |
|
| 799 |
static CURLcode |
| 800 |
init_config_builder_platform_verifier( |
| 801 |
struct Curl_easy *data, |
| 802 |
struct rustls_client_config_builder *builder) |
| 803 |
{ |
| 804 |
struct rustls_server_cert_verifier *server_cert_verifier = NULL; |
| 805 |
CURLcode result = CURLE_OK; |
| 806 |
rustls_result rr; |
| 807 |
|
| 808 |
rr = rustls_platform_server_cert_verifier(&server_cert_verifier); |
| 809 |
if(rr != RUSTLS_RESULT_OK) { |
| 810 |
rustls_failf(data, rr, "failed to create platform certificate verifier"); |
| 811 |
result = CURLE_SSL_CACERT_BADFILE; |
| 812 |
goto cleanup; |
| 813 |
} |
| 814 |
|
| 815 |
rustls_client_config_builder_set_server_verifier(builder, |
| 816 |
server_cert_verifier); |
| 817 |
|
| 818 |
cleanup: |
| 819 |
if(server_cert_verifier) { |
| 820 |
rustls_server_cert_verifier_free(server_cert_verifier); |
| 821 |
} |
| 822 |
return result; |
| 823 |
} |
| 824 |
|
| 825 |
static CURLcode |
| 826 |
init_config_builder_keylog(struct Curl_easy *data, |
| 827 |
struct rustls_client_config_builder *builder) |
| 828 |
{ |
| 829 |
rustls_result rr; |
| 830 |
|
| 831 |
Curl_tls_keylog_open(); |
| 832 |
if(!Curl_tls_keylog_enabled()) { |
| 833 |
return CURLE_OK; |
| 834 |
} |
| 835 |
|
| 836 |
rr = rustls_client_config_builder_set_key_log(builder, |
| 837 |
cr_keylog_log_cb, |
| 838 |
NULL); |
| 839 |
if(rr != RUSTLS_RESULT_OK) { |
| 840 |
rustls_failf(data, rr, "rustls_client_config_builder_set_key_log"); |
| 841 |
Curl_tls_keylog_close(); |
| 842 |
return map_error(rr); |
| 843 |
} |
| 844 |
|
| 845 |
return CURLE_OK; |
| 846 |
} |
| 847 |
|
| 848 |
static CURLcode |
| 849 |
init_config_builder_client_auth(struct Curl_easy *data, |
| 850 |
const struct ssl_primary_config *conn_config, |
| 851 |
const struct ssl_config_data *ssl_config, |
| 852 |
struct rustls_client_config_builder *builder) |
| 853 |
{ |
| 854 |
struct dynbuf cert_contents; |
| 855 |
struct dynbuf key_contents; |
| 856 |
rustls_result rr; |
| 857 |
const struct rustls_certified_key *certified_key = NULL; |
| 858 |
CURLcode result = CURLE_OK; |
| 859 |
|
| 860 |
if(conn_config->clientcert && !ssl_config->key) { |
| 861 |
failf(data, "rustls: must provide key with certificate '%s'", |
| 862 |
conn_config->clientcert); |
| 863 |
return CURLE_SSL_CERTPROBLEM; |
| 864 |
} |
| 865 |
else if(!conn_config->clientcert && ssl_config->key) { |
| 866 |
failf(data, "rustls: must provide certificate with key '%s'", |
| 867 |
conn_config->clientcert); |
| 868 |
return CURLE_SSL_CERTPROBLEM; |
| 869 |
} |
| 870 |
|
| 871 |
curlx_dyn_init(&cert_contents, DYN_CERTFILE_SIZE); |
| 872 |
curlx_dyn_init(&key_contents, DYN_KEYFILE_SIZE); |
| 873 |
|
| 874 |
if(!read_file_into(conn_config->clientcert, &cert_contents)) { |
| 875 |
failf(data, "rustls: failed to read client certificate file: '%s'", |
| 876 |
conn_config->clientcert); |
| 877 |
result = CURLE_SSL_CERTPROBLEM; |
| 878 |
goto cleanup; |
| 879 |
} |
| 880 |
|
| 881 |
if(!read_file_into(ssl_config->key, &key_contents)) { |
| 882 |
failf(data, "rustls: failed to read key file: '%s'", ssl_config->key); |
| 883 |
result = CURLE_SSL_CERTPROBLEM; |
| 884 |
goto cleanup; |
| 885 |
} |
| 886 |
|
| 887 |
rr = rustls_certified_key_build(curlx_dyn_uptr(&cert_contents), |
| 888 |
curlx_dyn_len(&cert_contents), |
| 889 |
curlx_dyn_uptr(&key_contents), |
| 890 |
curlx_dyn_len(&key_contents), |
| 891 |
&certified_key); |
| 892 |
if(rr != RUSTLS_RESULT_OK) { |
| 893 |
rustls_failf(data, rr, "rustls: failed to build certified key"); |
| 894 |
result = CURLE_SSL_CERTPROBLEM; |
| 895 |
goto cleanup; |
| 896 |
} |
| 897 |
|
| 898 |
rr = rustls_certified_key_keys_match(certified_key); |
| 899 |
if(rr != RUSTLS_RESULT_OK) { |
| 900 |
rustls_failf(data, |
| 901 |
rr, |
| 902 |
"rustls: client certificate and keypair files do not match:"); |
| 903 |
|
| 904 |
result = CURLE_SSL_CERTPROBLEM; |
| 905 |
goto cleanup; |
| 906 |
} |
| 907 |
|
| 908 |
rr = rustls_client_config_builder_set_certified_key(builder, |
| 909 |
&certified_key, |
| 910 |
1); |
| 911 |
if(rr != RUSTLS_RESULT_OK) { |
| 912 |
rustls_failf(data, rr, "rustls: failed to set certified key"); |
| 913 |
result = CURLE_SSL_CERTPROBLEM; |
| 914 |
goto cleanup; |
| 915 |
} |
| 916 |
|
| 917 |
cleanup: |
| 918 |
curlx_dyn_free(&cert_contents); |
| 919 |
curlx_dyn_free(&key_contents); |
| 920 |
if(certified_key) { |
| 921 |
rustls_certified_key_free(certified_key); |
| 922 |
} |
| 923 |
return result; |
| 924 |
} |
| 925 |
|
| 926 |
#if defined(USE_ECH) |
| 927 |
static CURLcode |
| 928 |
init_config_builder_ech(struct Curl_easy *data, |
| 929 |
const struct ssl_connect_data *connssl, |
| 930 |
struct rustls_client_config_builder *builder) |
| 931 |
{ |
| 932 |
const rustls_hpke *hpke = rustls_supported_hpke(); |
| 933 |
unsigned char *ech_config = NULL; |
| 934 |
size_t ech_config_len = 0; |
| 935 |
struct Curl_dns_entry *dns = NULL; |
| 936 |
struct Curl_https_rrinfo *rinfo = NULL; |
| 937 |
CURLcode result = CURLE_OK; |
| 938 |
rustls_result rr; |
| 939 |
|
| 940 |
if(!hpke) { |
| 941 |
failf(data, |
| 942 |
"rustls: ECH unavailable, rustls-ffi built without " |
| 943 |
"HPKE compatible crypto provider"); |
| 944 |
result = CURLE_SSL_CONNECT_ERROR; |
| 945 |
goto cleanup; |
| 946 |
} |
| 947 |
|
| 948 |
if(data->set.str[STRING_ECH_PUBLIC]) { |
| 949 |
failf(data, "rustls: ECH outername not supported"); |
| 950 |
result = CURLE_SSL_CONNECT_ERROR; |
| 951 |
goto cleanup; |
| 952 |
} |
| 953 |
|
| 954 |
if(data->set.tls_ech == CURLECH_GREASE) { |
| 955 |
rr = rustls_client_config_builder_enable_ech_grease(builder, hpke); |
| 956 |
if(rr != RUSTLS_RESULT_OK) { |
| 957 |
rustls_failf(data, rr, "rustls: failed to configure ECH GREASE"); |
| 958 |
result = CURLE_SSL_CONNECT_ERROR; |
| 959 |
goto cleanup; |
| 960 |
} |
| 961 |
return CURLE_OK; |
| 962 |
} |
| 963 |
|
| 964 |
if(data->set.tls_ech & CURLECH_CLA_CFG && data->set.str[STRING_ECH_CONFIG]) { |
| 965 |
const char *b64 = data->set.str[STRING_ECH_CONFIG]; |
| 966 |
size_t decode_result; |
| 967 |
if(!b64) { |
| 968 |
infof(data, "rustls: ECHConfig from command line empty"); |
| 969 |
result = CURLE_SSL_CONNECT_ERROR; |
| 970 |
goto cleanup; |
| 971 |
} |
| 972 |
/* rustls-ffi expects the raw TLS encoded ECHConfigList bytes */ |
| 973 |
decode_result = curlx_base64_decode(b64, &ech_config, &ech_config_len); |
| 974 |
if(decode_result || !ech_config) { |
| 975 |
infof(data, "rustls: cannot base64 decode ECHConfig from command line"); |
| 976 |
result = CURLE_SSL_CONNECT_ERROR; |
| 977 |
goto cleanup; |
| 978 |
} |
| 979 |
} |
| 980 |
else { |
| 981 |
if(connssl->peer.hostname) { |
| 982 |
dns = Curl_dnscache_get(data, connssl->peer.hostname, |
| 983 |
connssl->peer.port, data->conn->ip_version); |
| 984 |
} |
| 985 |
if(!dns) { |
| 986 |
failf(data, "rustls: ECH requested but no DNS info available"); |
| 987 |
result = CURLE_SSL_CONNECT_ERROR; |
| 988 |
goto cleanup; |
| 989 |
} |
| 990 |
rinfo = dns->hinfo; |
| 991 |
if(!rinfo || !rinfo->echconfiglist) { |
| 992 |
failf(data, "rustls: ECH requested but no ECHConfig available"); |
| 993 |
result = CURLE_SSL_CONNECT_ERROR; |
| 994 |
goto cleanup; |
| 995 |
} |
| 996 |
ech_config = rinfo->echconfiglist; |
| 997 |
ech_config_len = rinfo->echconfiglist_len; |
| 998 |
} |
| 999 |
|
| 1000 |
rr = rustls_client_config_builder_enable_ech(builder, |
| 1001 |
ech_config, |
| 1002 |
ech_config_len, |
| 1003 |
hpke); |
| 1004 |
if(rr != RUSTLS_RESULT_OK) { |
| 1005 |
rustls_failf(data, rr, "rustls: failed to configure ECH"); |
| 1006 |
result = CURLE_SSL_CONNECT_ERROR; |
| 1007 |
goto cleanup; |
| 1008 |
} |
| 1009 |
cleanup: |
| 1010 |
/* if we base64 decoded, we can free now */ |
| 1011 |
if(data->set.tls_ech & CURLECH_CLA_CFG && data->set.str[STRING_ECH_CONFIG]) { |
| 1012 |
free(ech_config); |
| 1013 |
} |
| 1014 |
if(dns) { |
| 1015 |
Curl_resolv_unlink(data, &dns); |
| 1016 |
} |
| 1017 |
return result; |
| 1018 |
} |
| 1019 |
#endif /* USE_ECH */ |
| 1020 |
|
| 1021 |
static CURLcode |
| 1022 |
cr_init_backend(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 1023 |
struct rustls_ssl_backend_data *const backend) |
| 1024 |
{ |
| 1025 |
const struct ssl_connect_data *connssl = cf->ctx; |
| 1026 |
const struct ssl_primary_config *conn_config = |
| 1027 |
Curl_ssl_cf_get_primary_config(cf); |
| 1028 |
struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data); |
| 1029 |
struct rustls_connection *rconn = NULL; |
| 1030 |
struct rustls_client_config_builder *config_builder = NULL; |
| 1031 |
|
| 1032 |
const struct curl_blob *ca_info_blob = conn_config->ca_info_blob; |
| 1033 |
const char * const ssl_cafile = |
| 1034 |
/* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */ |
| 1035 |
(ca_info_blob ? NULL : conn_config->CAfile); |
| 1036 |
CURLcode result = CURLE_OK; |
| 1037 |
rustls_result rr; |
| 1038 |
|
| 1039 |
DEBUGASSERT(backend); |
| 1040 |
rconn = backend->conn; |
| 1041 |
|
| 1042 |
result = init_config_builder(data, conn_config, &config_builder); |
| 1043 |
if(result != CURLE_OK) { |
| 1044 |
return result; |
| 1045 |
} |
| 1046 |
|
| 1047 |
if(connssl->alpn) { |
| 1048 |
init_config_builder_alpn(data, connssl, config_builder); |
| 1049 |
} |
| 1050 |
|
| 1051 |
if(!conn_config->verifypeer) { |
| 1052 |
rustls_client_config_builder_dangerous_set_certificate_verifier( |
| 1053 |
config_builder, cr_verify_none); |
| 1054 |
} |
| 1055 |
else if(ssl_config->native_ca_store) { |
| 1056 |
result = init_config_builder_platform_verifier(data, config_builder); |
| 1057 |
if(result != CURLE_OK) { |
| 1058 |
rustls_client_config_builder_free(config_builder); |
| 1059 |
return result; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
else if(ca_info_blob || ssl_cafile) { |
| 1063 |
result = init_config_builder_verifier(data, |
| 1064 |
config_builder, |
| 1065 |
conn_config, |
| 1066 |
ca_info_blob, |
| 1067 |
ssl_cafile); |
| 1068 |
if(result != CURLE_OK) { |
| 1069 |
rustls_client_config_builder_free(config_builder); |
| 1070 |
return result; |
| 1071 |
} |
| 1072 |
} |
| 1073 |
|
| 1074 |
if(conn_config->clientcert || ssl_config->key) { |
| 1075 |
result = init_config_builder_client_auth(data, |
| 1076 |
conn_config, |
| 1077 |
ssl_config, |
| 1078 |
config_builder); |
| 1079 |
if(result != CURLE_OK) { |
| 1080 |
rustls_client_config_builder_free(config_builder); |
| 1081 |
return result; |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
#if defined(USE_ECH) |
| 1086 |
if(ECH_ENABLED(data)) { |
| 1087 |
result = init_config_builder_ech(data, connssl, config_builder); |
| 1088 |
if(result != CURLE_OK && data->set.tls_ech & CURLECH_HARD) { |
| 1089 |
rustls_client_config_builder_free(config_builder); |
| 1090 |
return result; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
#endif /* USE_ECH */ |
| 1094 |
|
| 1095 |
result = init_config_builder_keylog(data, config_builder); |
| 1096 |
if(result != CURLE_OK) { |
| 1097 |
rustls_client_config_builder_free(config_builder); |
| 1098 |
return result; |
| 1099 |
} |
| 1100 |
|
| 1101 |
rr = rustls_client_config_builder_build( |
| 1102 |
config_builder, |
| 1103 |
&backend->config); |
| 1104 |
if(rr != RUSTLS_RESULT_OK) { |
| 1105 |
rustls_failf(data, rr, "failed to build client config"); |
| 1106 |
rustls_client_config_builder_free(config_builder); |
| 1107 |
rustls_client_config_free(backend->config); |
| 1108 |
return CURLE_SSL_CONNECT_ERROR; |
| 1109 |
} |
| 1110 |
|
| 1111 |
DEBUGASSERT(rconn == NULL); |
| 1112 |
rr = rustls_client_connection_new(backend->config, |
| 1113 |
connssl->peer.hostname, |
| 1114 |
&rconn); |
| 1115 |
if(rr != RUSTLS_RESULT_OK) { |
| 1116 |
rustls_failf(data, result, "rustls_client_connection_new"); |
| 1117 |
return CURLE_COULDNT_CONNECT; |
| 1118 |
} |
| 1119 |
DEBUGASSERT(rconn); |
| 1120 |
rustls_connection_set_userdata(rconn, backend); |
| 1121 |
backend->conn = rconn; |
| 1122 |
|
| 1123 |
return result; |
| 1124 |
} |
| 1125 |
|
| 1126 |
static void |
| 1127 |
cr_set_negotiated_alpn(struct Curl_cfilter *cf, struct Curl_easy *data, |
| 1128 |
const struct rustls_connection *rconn) |
| 1129 |
{ |
| 1130 |
struct ssl_connect_data *const connssl = cf->ctx; |
| 1131 |
const uint8_t *protocol = NULL; |
| 1132 |
size_t len = 0; |
| 1133 |
|
| 1134 |
rustls_connection_get_alpn_protocol(rconn, &protocol, &len); |
| 1135 |
Curl_alpn_set_negotiated(cf, data, connssl, protocol, len); |
| 1136 |
} |
| 1137 |
|
| 1138 |
/* Given an established network connection, do a TLS handshake. |
| 1139 |
* |
| 1140 |
* This function will set `*done` to true once the handshake is complete. |
| 1141 |
* This function never reads the value of `*done*`. |
| 1142 |
*/ |
| 1143 |
static CURLcode |
| 1144 |
cr_connect(struct Curl_cfilter *cf, |
| 1145 |
struct Curl_easy *data, bool *done) |
| 1146 |
{ |
| 1147 |
struct ssl_connect_data *const connssl = cf->ctx; |
| 1148 |
const struct rustls_ssl_backend_data *const backend = |
| 1149 |
(struct rustls_ssl_backend_data *)connssl->backend; |
| 1150 |
const struct rustls_connection *rconn = NULL; |
| 1151 |
CURLcode tmperr = CURLE_OK; |
| 1152 |
int result; |
| 1153 |
bool wants_read; |
| 1154 |
bool wants_write; |
| 1155 |
|
| 1156 |
DEBUGASSERT(backend); |
| 1157 |
|
| 1158 |
CURL_TRC_CF(data, cf, "cr_connect, state=%d", connssl->state); |
| 1159 |
*done = FALSE; |
| 1160 |
|
| 1161 |
if(!backend->conn) { |
| 1162 |
result = cr_init_backend(cf, data, |
| 1163 |
(struct rustls_ssl_backend_data *)connssl->backend); |
| 1164 |
CURL_TRC_CF(data, cf, "cr_connect, init backend -> %d", result); |
| 1165 |
if(result != CURLE_OK) { |
| 1166 |
return result; |
| 1167 |
} |
| 1168 |
connssl->state = ssl_connection_negotiating; |
| 1169 |
} |
| 1170 |
rconn = backend->conn; |
| 1171 |
|
| 1172 |
/* Read/write data until the handshake is done or the socket would block. */ |
| 1173 |
for(;;) { |
| 1174 |
/* |
| 1175 |
* Connection has been established according to Rustls. Set send/recv |
| 1176 |
* handlers, and update the state machine. |
| 1177 |
*/ |
| 1178 |
connssl->io_need = CURL_SSL_IO_NEED_NONE; |
| 1179 |
if(!rustls_connection_is_handshaking(rconn)) { |
| 1180 |
/* Rustls claims it is no longer handshaking *before* it has |
| 1181 |
* send its FINISHED message off. We attempt to let it write |
| 1182 |
* one more time. Oh my. |
| 1183 |
*/ |
| 1184 |
cr_set_negotiated_alpn(cf, data, rconn); |
| 1185 |
cr_send(cf, data, NULL, 0, &tmperr); |
| 1186 |
if(tmperr == CURLE_AGAIN) { |
| 1187 |
connssl->io_need = CURL_SSL_IO_NEED_SEND; |
| 1188 |
return CURLE_OK; |
| 1189 |
} |
| 1190 |
else if(tmperr != CURLE_OK) { |
| 1191 |
return tmperr; |
| 1192 |
} |
| 1193 |
/* REALLY Done with the handshake. */ |
| 1194 |
{ |
| 1195 |
const uint16_t proto = |
| 1196 |
rustls_connection_get_protocol_version(rconn); |
| 1197 |
const uint16_t cipher = |
| 1198 |
rustls_connection_get_negotiated_ciphersuite(rconn); |
| 1199 |
char buf[64] = ""; |
| 1200 |
const char *ver = "TLS version unknown"; |
| 1201 |
if(proto == RUSTLS_TLS_VERSION_TLSV1_3) |
| 1202 |
ver = "TLSv1.3"; |
| 1203 |
if(proto == RUSTLS_TLS_VERSION_TLSV1_2) |
| 1204 |
ver = "TLSv1.2"; |
| 1205 |
Curl_cipher_suite_get_str(cipher, buf, sizeof(buf), TRUE); |
| 1206 |
infof(data, "rustls: handshake complete, %s, cipher: %s", |
| 1207 |
ver, buf); |
| 1208 |
} |
| 1209 |
if(data->set.ssl.certinfo) { |
| 1210 |
size_t num_certs = 0; |
| 1211 |
while(rustls_connection_get_peer_certificate(rconn, (int)num_certs)) { |
| 1212 |
num_certs++; |
| 1213 |
} |
| 1214 |
result = Curl_ssl_init_certinfo(data, (int)num_certs); |
| 1215 |
if(result) |
| 1216 |
return result; |
| 1217 |
for(size_t i = 0; i < num_certs; i++) { |
| 1218 |
const rustls_certificate *cert; |
| 1219 |
const unsigned char *der_data; |
| 1220 |
size_t der_len; |
| 1221 |
rustls_result rresult = RUSTLS_RESULT_OK; |
| 1222 |
cert = rustls_connection_get_peer_certificate(rconn, i); |
| 1223 |
DEBUGASSERT(cert); /* Should exist since we counted already */ |
| 1224 |
rresult = rustls_certificate_get_der(cert, &der_data, &der_len); |
| 1225 |
if(rresult != RUSTLS_RESULT_OK) { |
| 1226 |
char errorbuf[255]; |
| 1227 |
size_t errorlen; |
| 1228 |
rustls_error(rresult, errorbuf, sizeof(errorbuf), &errorlen); |
| 1229 |
failf(data, |
| 1230 |
"Failed getting DER of server certificate #%ld: %.*s", i, |
| 1231 |
(int)errorlen, errorbuf); |
| 1232 |
return map_error(rresult); |
| 1233 |
} |
| 1234 |
{ |
| 1235 |
const char *beg; |
| 1236 |
const char *end; |
| 1237 |
beg = (const char *)der_data; |
| 1238 |
end = (const char *)(der_data + der_len); |
| 1239 |
result = Curl_extract_certinfo(data, (int)i, beg, end); |
| 1240 |
if(result) |
| 1241 |
return result; |
| 1242 |
} |
| 1243 |
} |
| 1244 |
} |
| 1245 |
|
| 1246 |
connssl->state = ssl_connection_complete; |
| 1247 |
*done = TRUE; |
| 1248 |
return CURLE_OK; |
| 1249 |
} |
| 1250 |
|
| 1251 |
connssl->connecting_state = ssl_connect_2; |
| 1252 |
wants_read = rustls_connection_wants_read(rconn); |
| 1253 |
wants_write = rustls_connection_wants_write(rconn) || |
| 1254 |
backend->plain_out_buffered; |
| 1255 |
DEBUGASSERT(wants_read || wants_write); |
| 1256 |
|
| 1257 |
if(wants_write) { |
| 1258 |
CURL_TRC_CF(data, cf, "rustls_connection wants us to write_tls."); |
| 1259 |
cr_send(cf, data, NULL, 0, &tmperr); |
| 1260 |
if(tmperr == CURLE_AGAIN) { |
| 1261 |
CURL_TRC_CF(data, cf, "writing would block"); |
| 1262 |
connssl->io_need = CURL_SSL_IO_NEED_SEND; |
| 1263 |
return CURLE_OK; |
| 1264 |
} |
| 1265 |
else if(tmperr != CURLE_OK) { |
| 1266 |
return tmperr; |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
if(wants_read) { |
| 1271 |
CURL_TRC_CF(data, cf, "rustls_connection wants us to read_tls."); |
| 1272 |
if(tls_recv_more(cf, data, &tmperr) < 0) { |
| 1273 |
if(tmperr == CURLE_AGAIN) { |
| 1274 |
CURL_TRC_CF(data, cf, "reading would block"); |
| 1275 |
connssl->io_need = CURL_SSL_IO_NEED_RECV; |
| 1276 |
return CURLE_OK; |
| 1277 |
} |
| 1278 |
else if(tmperr == CURLE_RECV_ERROR) { |
| 1279 |
return CURLE_SSL_CONNECT_ERROR; |
| 1280 |
} |
| 1281 |
else { |
| 1282 |
return tmperr; |
| 1283 |
} |
| 1284 |
} |
| 1285 |
} |
| 1286 |
} |
| 1287 |
|
| 1288 |
/* We should never fall through the loop. We should return either because |
| 1289 |
the handshake is done or because we cannot read/write without blocking. */ |
| 1290 |
DEBUGASSERT(FALSE); |
| 1291 |
} |
| 1292 |
|
| 1293 |
static void * |
| 1294 |
cr_get_internals(struct ssl_connect_data *connssl, |
| 1295 |
CURLINFO info UNUSED_PARAM) |
| 1296 |
{ |
| 1297 |
struct rustls_ssl_backend_data *backend = |
| 1298 |
(struct rustls_ssl_backend_data *)connssl->backend; |
| 1299 |
DEBUGASSERT(backend); |
| 1300 |
return &backend->conn; |
| 1301 |
} |
| 1302 |
|
| 1303 |
static CURLcode |
| 1304 |
cr_shutdown(struct Curl_cfilter *cf, |
| 1305 |
struct Curl_easy *data, |
| 1306 |
const bool send_shutdown, bool *done) |
| 1307 |
{ |
| 1308 |
struct ssl_connect_data *connssl = cf->ctx; |
| 1309 |
struct rustls_ssl_backend_data *backend = |
| 1310 |
(struct rustls_ssl_backend_data *)connssl->backend; |
| 1311 |
CURLcode result = CURLE_OK; |
| 1312 |
ssize_t nwritten, nread; |
| 1313 |
size_t i; |
| 1314 |
|
| 1315 |
DEBUGASSERT(backend); |
| 1316 |
if(!backend->conn || cf->shutdown) { |
| 1317 |
*done = TRUE; |
| 1318 |
goto out; |
| 1319 |
} |
| 1320 |
|
| 1321 |
connssl->io_need = CURL_SSL_IO_NEED_NONE; |
| 1322 |
*done = FALSE; |
| 1323 |
|
| 1324 |
if(!backend->sent_shutdown) { |
| 1325 |
/* do this only once */ |
| 1326 |
backend->sent_shutdown = TRUE; |
| 1327 |
if(send_shutdown) { |
| 1328 |
rustls_connection_send_close_notify(backend->conn); |
| 1329 |
} |
| 1330 |
} |
| 1331 |
|
| 1332 |
nwritten = cr_send(cf, data, NULL, 0, &result); |
| 1333 |
if(nwritten < 0) { |
| 1334 |
if(result == CURLE_AGAIN) { |
| 1335 |
connssl->io_need = CURL_SSL_IO_NEED_SEND; |
| 1336 |
result = CURLE_OK; |
| 1337 |
goto out; |
| 1338 |
} |
| 1339 |
DEBUGASSERT(result); |
| 1340 |
CURL_TRC_CF(data, cf, "shutdown send failed: %d", result); |
| 1341 |
goto out; |
| 1342 |
} |
| 1343 |
|
| 1344 |
for(i = 0; i < 10; ++i) { |
| 1345 |
char buf[1024]; |
| 1346 |
nread = cr_recv(cf, data, buf, (int)sizeof(buf), &result); |
| 1347 |
if(nread <= 0) |
| 1348 |
break; |
| 1349 |
} |
| 1350 |
|
| 1351 |
if(nread > 0) { |
| 1352 |
/* still data coming in? */ |
| 1353 |
} |
| 1354 |
else if(nread == 0) { |
| 1355 |
/* We got the close notify alert and are done. */ |
| 1356 |
*done = TRUE; |
| 1357 |
} |
| 1358 |
else if(result == CURLE_AGAIN) { |
| 1359 |
connssl->io_need = CURL_SSL_IO_NEED_RECV; |
| 1360 |
result = CURLE_OK; |
| 1361 |
} |
| 1362 |
else { |
| 1363 |
DEBUGASSERT(result); |
| 1364 |
CURL_TRC_CF(data, cf, "shutdown, error: %d", result); |
| 1365 |
} |
| 1366 |
|
| 1367 |
out: |
| 1368 |
cf->shutdown = (result || *done); |
| 1369 |
return result; |
| 1370 |
} |
| 1371 |
|
| 1372 |
static void |
| 1373 |
cr_close(struct Curl_cfilter *cf, struct Curl_easy *data) |
| 1374 |
{ |
| 1375 |
const struct ssl_connect_data *connssl = cf->ctx; |
| 1376 |
struct rustls_ssl_backend_data *backend = |
| 1377 |
(struct rustls_ssl_backend_data *)connssl->backend; |
| 1378 |
|
| 1379 |
(void)data; |
| 1380 |
DEBUGASSERT(backend); |
| 1381 |
if(backend->conn) { |
| 1382 |
rustls_connection_free(backend->conn); |
| 1383 |
backend->conn = NULL; |
| 1384 |
} |
| 1385 |
if(backend->config) { |
| 1386 |
rustls_client_config_free(backend->config); |
| 1387 |
backend->config = NULL; |
| 1388 |
} |
| 1389 |
} |
| 1390 |
|
| 1391 |
static size_t cr_version(char *buffer, size_t size) |
| 1392 |
{ |
| 1393 |
const struct rustls_str ver = rustls_version(); |
| 1394 |
return msnprintf(buffer, size, "%.*s", (int)ver.len, ver.data); |
| 1395 |
} |
| 1396 |
|
| 1397 |
static CURLcode |
| 1398 |
cr_random(struct Curl_easy *data, unsigned char *entropy, size_t length) |
| 1399 |
{ |
| 1400 |
rustls_result rresult = 0; |
| 1401 |
(void)data; |
| 1402 |
rresult = |
| 1403 |
rustls_default_crypto_provider_random(entropy, length); |
| 1404 |
return map_error(rresult); |
| 1405 |
} |
| 1406 |
|
| 1407 |
static void cr_cleanup(void) |
| 1408 |
{ |
| 1409 |
Curl_tls_keylog_close(); |
| 1410 |
} |
| 1411 |
|
| 1412 |
const struct Curl_ssl Curl_ssl_rustls = { |
| 1413 |
{ CURLSSLBACKEND_RUSTLS, "rustls" }, |
| 1414 |
SSLSUPP_CAINFO_BLOB | /* supports */ |
| 1415 |
SSLSUPP_HTTPS_PROXY | |
| 1416 |
SSLSUPP_CIPHER_LIST | |
| 1417 |
SSLSUPP_TLS13_CIPHERSUITES | |
| 1418 |
SSLSUPP_CERTINFO | |
| 1419 |
SSLSUPP_ECH, |
| 1420 |
sizeof(struct rustls_ssl_backend_data), |
| 1421 |
|
| 1422 |
NULL, /* init */ |
| 1423 |
cr_cleanup, /* cleanup */ |
| 1424 |
cr_version, /* version */ |
| 1425 |
cr_shutdown, /* shutdown */ |
| 1426 |
cr_data_pending, /* data_pending */ |
| 1427 |
cr_random, /* random */ |
| 1428 |
NULL, /* cert_status_request */ |
| 1429 |
cr_connect, /* connect */ |
| 1430 |
Curl_ssl_adjust_pollset, /* adjust_pollset */ |
| 1431 |
cr_get_internals, /* get_internals */ |
| 1432 |
cr_close, /* close_one */ |
| 1433 |
NULL, /* close_all */ |
| 1434 |
NULL, /* set_engine */ |
| 1435 |
NULL, /* set_engine_default */ |
| 1436 |
NULL, /* engines_list */ |
| 1437 |
NULL, /* false_start */ |
| 1438 |
NULL, /* sha256sum */ |
| 1439 |
cr_recv, /* recv decrypted data */ |
| 1440 |
cr_send, /* send data to encrypt */ |
| 1441 |
NULL, /* get_channel_binding */ |
| 1442 |
}; |
| 1443 |
|
| 1444 |
#endif /* USE_RUSTLS */ |