rustls.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Jacob Hoffman-Andrews,
  9. * <github@hoffman-andrews.com>
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. #include "curl_setup.h"
  26. #ifdef USE_RUSTLS
  27. #include "curl_printf.h"
  28. #include <errno.h>
  29. #include <rustls.h>
  30. #include "inet_pton.h"
  31. #include "urldata.h"
  32. #include "sendf.h"
  33. #include "vtls.h"
  34. #include "vtls_int.h"
  35. #include "select.h"
  36. #include "strerror.h"
  37. #include "multiif.h"
  38. struct ssl_backend_data
  39. {
  40. const struct rustls_client_config *config;
  41. struct rustls_connection *conn;
  42. bool data_pending;
  43. };
  44. /* For a given rustls_result error code, return the best-matching CURLcode. */
  45. static CURLcode map_error(rustls_result r)
  46. {
  47. if(rustls_result_is_cert_error(r)) {
  48. return CURLE_PEER_FAILED_VERIFICATION;
  49. }
  50. switch(r) {
  51. case RUSTLS_RESULT_OK:
  52. return CURLE_OK;
  53. case RUSTLS_RESULT_NULL_PARAMETER:
  54. return CURLE_BAD_FUNCTION_ARGUMENT;
  55. default:
  56. return CURLE_READ_ERROR;
  57. }
  58. }
  59. static bool
  60. cr_data_pending(struct Curl_cfilter *cf, const struct Curl_easy *data)
  61. {
  62. struct ssl_connect_data *ctx = cf->ctx;
  63. (void)data;
  64. DEBUGASSERT(ctx && ctx->backend);
  65. return ctx->backend->data_pending;
  66. }
  67. static CURLcode
  68. cr_connect(struct Curl_cfilter *cf UNUSED_PARAM,
  69. struct Curl_easy *data UNUSED_PARAM)
  70. {
  71. infof(data, "rustls_connect: unimplemented");
  72. return CURLE_SSL_CONNECT_ERROR;
  73. }
  74. struct io_ctx {
  75. struct Curl_cfilter *cf;
  76. struct Curl_easy *data;
  77. };
  78. static int
  79. read_cb(void *userdata, uint8_t *buf, uintptr_t len, uintptr_t *out_n)
  80. {
  81. struct io_ctx *io_ctx = userdata;
  82. CURLcode result;
  83. int ret = 0;
  84. ssize_t nread = Curl_conn_cf_recv(io_ctx->cf->next, io_ctx->data,
  85. (char *)buf, len, &result);
  86. if(nread < 0) {
  87. nread = 0;
  88. if(CURLE_AGAIN == result)
  89. ret = EAGAIN;
  90. else
  91. ret = EINVAL;
  92. }
  93. *out_n = (int)nread;
  94. return ret;
  95. }
  96. static int
  97. write_cb(void *userdata, const uint8_t *buf, uintptr_t len, uintptr_t *out_n)
  98. {
  99. struct io_ctx *io_ctx = userdata;
  100. CURLcode result;
  101. int ret = 0;
  102. ssize_t nwritten = Curl_conn_cf_send(io_ctx->cf->next, io_ctx->data,
  103. (const char *)buf, len, &result);
  104. if(nwritten < 0) {
  105. nwritten = 0;
  106. if(CURLE_AGAIN == result)
  107. ret = EAGAIN;
  108. else
  109. ret = EINVAL;
  110. }
  111. *out_n = (int)nwritten;
  112. return ret;
  113. }
  114. /*
  115. * On each run:
  116. * - Read a chunk of bytes from the socket into rustls' TLS input buffer.
  117. * - Tell rustls to process any new packets.
  118. * - Read out as many plaintext bytes from rustls as possible, until hitting
  119. * error, EOF, or EAGAIN/EWOULDBLOCK, or plainbuf/plainlen is filled up.
  120. *
  121. * It's okay to call this function with plainbuf == NULL and plainlen == 0.
  122. * In that case, it will copy bytes from the socket into rustls' TLS input
  123. * buffer, and process packets, but won't consume bytes from rustls' plaintext
  124. * output buffer.
  125. */
  126. static ssize_t
  127. cr_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  128. char *plainbuf, size_t plainlen, CURLcode *err)
  129. {
  130. struct ssl_connect_data *const connssl = cf->ctx;
  131. struct ssl_backend_data *const backend = connssl->backend;
  132. struct rustls_connection *rconn = NULL;
  133. struct io_ctx io_ctx;
  134. size_t n = 0;
  135. size_t tls_bytes_read = 0;
  136. size_t plain_bytes_copied = 0;
  137. rustls_result rresult = 0;
  138. char errorbuf[255];
  139. rustls_io_result io_error;
  140. DEBUGASSERT(backend);
  141. rconn = backend->conn;
  142. io_ctx.cf = cf;
  143. io_ctx.data = data;
  144. io_error = rustls_connection_read_tls(rconn, read_cb, &io_ctx,
  145. &tls_bytes_read);
  146. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  147. DEBUGF(LOG_CF(data, cf, "cr_recv: EAGAIN or EWOULDBLOCK"));
  148. }
  149. else if(io_error) {
  150. char buffer[STRERROR_LEN];
  151. failf(data, "reading from socket: %s",
  152. Curl_strerror(io_error, buffer, sizeof(buffer)));
  153. *err = CURLE_READ_ERROR;
  154. return -1;
  155. }
  156. DEBUGF(LOG_CF(data, cf, "cr_recv: read %ld TLS bytes", tls_bytes_read));
  157. rresult = rustls_connection_process_new_packets(rconn);
  158. if(rresult != RUSTLS_RESULT_OK) {
  159. rustls_error(rresult, errorbuf, sizeof(errorbuf), &n);
  160. failf(data, "%.*s", n, errorbuf);
  161. *err = map_error(rresult);
  162. return -1;
  163. }
  164. backend->data_pending = TRUE;
  165. while(plain_bytes_copied < plainlen) {
  166. rresult = rustls_connection_read(rconn,
  167. (uint8_t *)plainbuf + plain_bytes_copied,
  168. plainlen - plain_bytes_copied,
  169. &n);
  170. if(rresult == RUSTLS_RESULT_PLAINTEXT_EMPTY) {
  171. DEBUGF(LOG_CF(data, cf, "cr_recv: got PLAINTEXT_EMPTY. "
  172. "will try again later."));
  173. backend->data_pending = FALSE;
  174. break;
  175. }
  176. else if(rresult != RUSTLS_RESULT_OK) {
  177. /* n always equals 0 in this case, don't need to check it */
  178. failf(data, "error in rustls_connection_read: %d", rresult);
  179. *err = CURLE_READ_ERROR;
  180. return -1;
  181. }
  182. else if(n == 0) {
  183. /* n == 0 indicates clean EOF, but we may have read some other
  184. plaintext bytes before we reached this. Break out of the loop
  185. so we can figure out whether to return success or EOF. */
  186. break;
  187. }
  188. else {
  189. DEBUGF(LOG_CF(data, cf, "cr_recv: got %ld plain bytes", n));
  190. plain_bytes_copied += n;
  191. }
  192. }
  193. if(plain_bytes_copied) {
  194. *err = CURLE_OK;
  195. return plain_bytes_copied;
  196. }
  197. /* If we wrote out 0 plaintext bytes, that means either we hit a clean EOF,
  198. OR we got a RUSTLS_RESULT_PLAINTEXT_EMPTY.
  199. If the latter, return CURLE_AGAIN so curl doesn't treat this as EOF. */
  200. if(!backend->data_pending) {
  201. *err = CURLE_AGAIN;
  202. return -1;
  203. }
  204. /* Zero bytes read, and no RUSTLS_RESULT_PLAINTEXT_EMPTY, means the TCP
  205. connection was cleanly closed (with a close_notify alert). */
  206. *err = CURLE_OK;
  207. return 0;
  208. }
  209. /*
  210. * On each call:
  211. * - Copy `plainlen` bytes into rustls' plaintext input buffer (if > 0).
  212. * - Fully drain rustls' plaintext output buffer into the socket until
  213. * we get either an error or EAGAIN/EWOULDBLOCK.
  214. *
  215. * It's okay to call this function with plainbuf == NULL and plainlen == 0.
  216. * In that case, it won't read anything into rustls' plaintext input buffer.
  217. * It will only drain rustls' plaintext output buffer into the socket.
  218. */
  219. static ssize_t
  220. cr_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  221. const void *plainbuf, size_t plainlen, CURLcode *err)
  222. {
  223. struct ssl_connect_data *const connssl = cf->ctx;
  224. struct ssl_backend_data *const backend = connssl->backend;
  225. struct rustls_connection *rconn = NULL;
  226. struct io_ctx io_ctx;
  227. size_t plainwritten = 0;
  228. size_t tlswritten = 0;
  229. size_t tlswritten_total = 0;
  230. rustls_result rresult;
  231. rustls_io_result io_error;
  232. DEBUGASSERT(backend);
  233. rconn = backend->conn;
  234. DEBUGF(LOG_CF(data, cf, "cr_send: %ld plain bytes", plainlen));
  235. if(plainlen > 0) {
  236. rresult = rustls_connection_write(rconn, plainbuf, plainlen,
  237. &plainwritten);
  238. if(rresult != RUSTLS_RESULT_OK) {
  239. failf(data, "error in rustls_connection_write");
  240. *err = CURLE_WRITE_ERROR;
  241. return -1;
  242. }
  243. else if(plainwritten == 0) {
  244. failf(data, "EOF in rustls_connection_write");
  245. *err = CURLE_WRITE_ERROR;
  246. return -1;
  247. }
  248. }
  249. io_ctx.cf = cf;
  250. io_ctx.data = data;
  251. while(rustls_connection_wants_write(rconn)) {
  252. io_error = rustls_connection_write_tls(rconn, write_cb, &io_ctx,
  253. &tlswritten);
  254. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  255. DEBUGF(LOG_CF(data, cf, "cr_send: EAGAIN after %zu bytes",
  256. tlswritten_total));
  257. *err = CURLE_AGAIN;
  258. return -1;
  259. }
  260. else if(io_error) {
  261. char buffer[STRERROR_LEN];
  262. failf(data, "writing to socket: %s",
  263. Curl_strerror(io_error, buffer, sizeof(buffer)));
  264. *err = CURLE_WRITE_ERROR;
  265. return -1;
  266. }
  267. if(tlswritten == 0) {
  268. failf(data, "EOF in swrite");
  269. *err = CURLE_WRITE_ERROR;
  270. return -1;
  271. }
  272. DEBUGF(LOG_CF(data, cf, "cr_send: wrote %zu TLS bytes", tlswritten));
  273. tlswritten_total += tlswritten;
  274. }
  275. return plainwritten;
  276. }
  277. /* A server certificate verify callback for rustls that always returns
  278. RUSTLS_RESULT_OK, or in other words disable certificate verification. */
  279. static enum rustls_result
  280. cr_verify_none(void *userdata UNUSED_PARAM,
  281. const rustls_verify_server_cert_params *params UNUSED_PARAM)
  282. {
  283. return RUSTLS_RESULT_OK;
  284. }
  285. static bool
  286. cr_hostname_is_ip(const char *hostname)
  287. {
  288. struct in_addr in;
  289. #ifdef ENABLE_IPV6
  290. struct in6_addr in6;
  291. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0) {
  292. return true;
  293. }
  294. #endif /* ENABLE_IPV6 */
  295. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  296. return true;
  297. }
  298. return false;
  299. }
  300. static CURLcode
  301. cr_init_backend(struct Curl_cfilter *cf, struct Curl_easy *data,
  302. struct ssl_backend_data *const backend)
  303. {
  304. struct ssl_connect_data *connssl = cf->ctx;
  305. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  306. struct rustls_connection *rconn = NULL;
  307. struct rustls_client_config_builder *config_builder = NULL;
  308. struct rustls_root_cert_store *roots = NULL;
  309. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  310. const char * const ssl_cafile =
  311. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  312. (ca_info_blob ? NULL : conn_config->CAfile);
  313. const bool verifypeer = conn_config->verifypeer;
  314. const char *hostname = connssl->hostname;
  315. char errorbuf[256];
  316. size_t errorlen;
  317. int result;
  318. DEBUGASSERT(backend);
  319. rconn = backend->conn;
  320. config_builder = rustls_client_config_builder_new();
  321. if(connssl->alpn) {
  322. struct alpn_proto_buf proto;
  323. rustls_slice_bytes alpn[ALPN_ENTRIES_MAX];
  324. size_t i;
  325. for(i = 0; i < connssl->alpn->count; ++i) {
  326. alpn[i].data = (const uint8_t *)connssl->alpn->entries[i];
  327. alpn[i].len = strlen(connssl->alpn->entries[i]);
  328. }
  329. rustls_client_config_builder_set_alpn_protocols(config_builder, alpn,
  330. connssl->alpn->count);
  331. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  332. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  333. }
  334. if(!verifypeer) {
  335. rustls_client_config_builder_dangerous_set_certificate_verifier(
  336. config_builder, cr_verify_none);
  337. /* rustls doesn't support IP addresses (as of 0.19.0), and will reject
  338. * connections created with an IP address, even when certificate
  339. * verification is turned off. Set a placeholder hostname and disable
  340. * SNI. */
  341. if(cr_hostname_is_ip(hostname)) {
  342. rustls_client_config_builder_set_enable_sni(config_builder, false);
  343. hostname = "example.invalid";
  344. }
  345. }
  346. else if(ca_info_blob) {
  347. roots = rustls_root_cert_store_new();
  348. /* Enable strict parsing only if verification isn't disabled. */
  349. result = rustls_root_cert_store_add_pem(roots, ca_info_blob->data,
  350. ca_info_blob->len, verifypeer);
  351. if(result != RUSTLS_RESULT_OK) {
  352. failf(data, "failed to parse trusted certificates from blob");
  353. rustls_root_cert_store_free(roots);
  354. rustls_client_config_free(
  355. rustls_client_config_builder_build(config_builder));
  356. return CURLE_SSL_CACERT_BADFILE;
  357. }
  358. result = rustls_client_config_builder_use_roots(config_builder, roots);
  359. rustls_root_cert_store_free(roots);
  360. if(result != RUSTLS_RESULT_OK) {
  361. failf(data, "failed to load trusted certificates");
  362. rustls_client_config_free(
  363. rustls_client_config_builder_build(config_builder));
  364. return CURLE_SSL_CACERT_BADFILE;
  365. }
  366. }
  367. else if(ssl_cafile) {
  368. result = rustls_client_config_builder_load_roots_from_file(
  369. config_builder, ssl_cafile);
  370. if(result != RUSTLS_RESULT_OK) {
  371. failf(data, "failed to load trusted certificates");
  372. rustls_client_config_free(
  373. rustls_client_config_builder_build(config_builder));
  374. return CURLE_SSL_CACERT_BADFILE;
  375. }
  376. }
  377. backend->config = rustls_client_config_builder_build(config_builder);
  378. DEBUGASSERT(rconn == NULL);
  379. {
  380. char *snihost = Curl_ssl_snihost(data, hostname, NULL);
  381. if(!snihost) {
  382. failf(data, "Failed to set SNI");
  383. return CURLE_SSL_CONNECT_ERROR;
  384. }
  385. result = rustls_client_connection_new(backend->config, snihost, &rconn);
  386. }
  387. if(result != RUSTLS_RESULT_OK) {
  388. rustls_error(result, errorbuf, sizeof(errorbuf), &errorlen);
  389. failf(data, "rustls_client_connection_new: %.*s", errorlen, errorbuf);
  390. return CURLE_COULDNT_CONNECT;
  391. }
  392. rustls_connection_set_userdata(rconn, backend);
  393. backend->conn = rconn;
  394. return CURLE_OK;
  395. }
  396. static void
  397. cr_set_negotiated_alpn(struct Curl_cfilter *cf, struct Curl_easy *data,
  398. const struct rustls_connection *rconn)
  399. {
  400. const uint8_t *protocol = NULL;
  401. size_t len = 0;
  402. rustls_connection_get_alpn_protocol(rconn, &protocol, &len);
  403. Curl_alpn_set_negotiated(cf, data, protocol, len);
  404. }
  405. static CURLcode
  406. cr_connect_nonblocking(struct Curl_cfilter *cf,
  407. struct Curl_easy *data, bool *done)
  408. {
  409. struct ssl_connect_data *const connssl = cf->ctx;
  410. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  411. struct ssl_backend_data *const backend = connssl->backend;
  412. struct rustls_connection *rconn = NULL;
  413. CURLcode tmperr = CURLE_OK;
  414. int result;
  415. int what;
  416. bool wants_read;
  417. bool wants_write;
  418. curl_socket_t writefd;
  419. curl_socket_t readfd;
  420. DEBUGASSERT(backend);
  421. if(ssl_connection_none == connssl->state) {
  422. result = cr_init_backend(cf, data, connssl->backend);
  423. if(result != CURLE_OK) {
  424. return result;
  425. }
  426. connssl->state = ssl_connection_negotiating;
  427. }
  428. rconn = backend->conn;
  429. /* Read/write data until the handshake is done or the socket would block. */
  430. for(;;) {
  431. /*
  432. * Connection has been established according to rustls. Set send/recv
  433. * handlers, and update the state machine.
  434. */
  435. if(!rustls_connection_is_handshaking(rconn)) {
  436. infof(data, "Done handshaking");
  437. /* Done with the handshake. Set up callbacks to send/receive data. */
  438. connssl->state = ssl_connection_complete;
  439. cr_set_negotiated_alpn(cf, data, rconn);
  440. *done = TRUE;
  441. return CURLE_OK;
  442. }
  443. wants_read = rustls_connection_wants_read(rconn);
  444. wants_write = rustls_connection_wants_write(rconn);
  445. DEBUGASSERT(wants_read || wants_write);
  446. writefd = wants_write?sockfd:CURL_SOCKET_BAD;
  447. readfd = wants_read?sockfd:CURL_SOCKET_BAD;
  448. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd, 0);
  449. if(what < 0) {
  450. /* fatal error */
  451. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  452. return CURLE_SSL_CONNECT_ERROR;
  453. }
  454. if(0 == what) {
  455. infof(data, "Curl_socket_check: %s would block",
  456. wants_read&&wants_write ? "writing and reading" :
  457. wants_write ? "writing" : "reading");
  458. *done = FALSE;
  459. return CURLE_OK;
  460. }
  461. /* socket is readable or writable */
  462. if(wants_write) {
  463. infof(data, "rustls_connection wants us to write_tls.");
  464. cr_send(cf, data, NULL, 0, &tmperr);
  465. if(tmperr == CURLE_AGAIN) {
  466. infof(data, "writing would block");
  467. /* fall through */
  468. }
  469. else if(tmperr != CURLE_OK) {
  470. return tmperr;
  471. }
  472. }
  473. if(wants_read) {
  474. infof(data, "rustls_connection wants us to read_tls.");
  475. cr_recv(cf, data, NULL, 0, &tmperr);
  476. if(tmperr == CURLE_AGAIN) {
  477. infof(data, "reading would block");
  478. /* fall through */
  479. }
  480. else if(tmperr != CURLE_OK) {
  481. if(tmperr == CURLE_READ_ERROR) {
  482. return CURLE_SSL_CONNECT_ERROR;
  483. }
  484. else {
  485. return tmperr;
  486. }
  487. }
  488. }
  489. }
  490. /* We should never fall through the loop. We should return either because
  491. the handshake is done or because we can't read/write without blocking. */
  492. DEBUGASSERT(false);
  493. }
  494. /* returns a bitmap of flags for this connection's first socket indicating
  495. whether we want to read or write */
  496. static int
  497. cr_get_select_socks(struct Curl_cfilter *cf, struct Curl_easy *data,
  498. curl_socket_t *socks)
  499. {
  500. struct ssl_connect_data *const connssl = cf->ctx;
  501. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  502. struct ssl_backend_data *const backend = connssl->backend;
  503. struct rustls_connection *rconn = NULL;
  504. (void)data;
  505. DEBUGASSERT(backend);
  506. rconn = backend->conn;
  507. if(rustls_connection_wants_write(rconn)) {
  508. socks[0] = sockfd;
  509. return GETSOCK_WRITESOCK(0);
  510. }
  511. if(rustls_connection_wants_read(rconn)) {
  512. socks[0] = sockfd;
  513. return GETSOCK_READSOCK(0);
  514. }
  515. return GETSOCK_BLANK;
  516. }
  517. static void *
  518. cr_get_internals(struct ssl_connect_data *connssl,
  519. CURLINFO info UNUSED_PARAM)
  520. {
  521. struct ssl_backend_data *backend = connssl->backend;
  522. DEBUGASSERT(backend);
  523. return &backend->conn;
  524. }
  525. static void
  526. cr_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  527. {
  528. struct ssl_connect_data *connssl = cf->ctx;
  529. struct ssl_backend_data *backend = connssl->backend;
  530. CURLcode tmperr = CURLE_OK;
  531. ssize_t n = 0;
  532. DEBUGASSERT(backend);
  533. if(backend->conn) {
  534. rustls_connection_send_close_notify(backend->conn);
  535. n = cr_send(cf, data, NULL, 0, &tmperr);
  536. if(n < 0) {
  537. failf(data, "error sending close notify: %d", tmperr);
  538. }
  539. rustls_connection_free(backend->conn);
  540. backend->conn = NULL;
  541. }
  542. if(backend->config) {
  543. rustls_client_config_free(backend->config);
  544. backend->config = NULL;
  545. }
  546. }
  547. static size_t cr_version(char *buffer, size_t size)
  548. {
  549. struct rustls_str ver = rustls_version();
  550. return msnprintf(buffer, size, "%.*s", (int)ver.len, ver.data);
  551. }
  552. const struct Curl_ssl Curl_ssl_rustls = {
  553. { CURLSSLBACKEND_RUSTLS, "rustls" },
  554. SSLSUPP_CAINFO_BLOB | /* supports */
  555. SSLSUPP_TLS13_CIPHERSUITES |
  556. SSLSUPP_HTTPS_PROXY,
  557. sizeof(struct ssl_backend_data),
  558. Curl_none_init, /* init */
  559. Curl_none_cleanup, /* cleanup */
  560. cr_version, /* version */
  561. Curl_none_check_cxn, /* check_cxn */
  562. Curl_none_shutdown, /* shutdown */
  563. cr_data_pending, /* data_pending */
  564. Curl_none_random, /* random */
  565. Curl_none_cert_status_request, /* cert_status_request */
  566. cr_connect, /* connect */
  567. cr_connect_nonblocking, /* connect_nonblocking */
  568. cr_get_select_socks, /* get_select_socks */
  569. cr_get_internals, /* get_internals */
  570. cr_close, /* close_one */
  571. Curl_none_close_all, /* close_all */
  572. Curl_none_session_free, /* session_free */
  573. Curl_none_set_engine, /* set_engine */
  574. Curl_none_set_engine_default, /* set_engine_default */
  575. Curl_none_engines_list, /* engines_list */
  576. Curl_none_false_start, /* false_start */
  577. NULL, /* sha256sum */
  578. NULL, /* associate_connection */
  579. NULL, /* disassociate_connection */
  580. NULL, /* free_multi_ssl_backend_data */
  581. cr_recv, /* recv decrypted data */
  582. cr_send, /* send data to encrypt */
  583. };
  584. #endif /* USE_RUSTLS */