rustls.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. size_t errorlen;
  140. rustls_io_result io_error;
  141. DEBUGASSERT(backend);
  142. rconn = backend->conn;
  143. io_ctx.cf = cf;
  144. io_ctx.data = data;
  145. io_error = rustls_connection_read_tls(rconn, read_cb, &io_ctx,
  146. &tls_bytes_read);
  147. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  148. DEBUGF(LOG_CF(data, cf, "cr_recv: EAGAIN or EWOULDBLOCK"));
  149. }
  150. else if(io_error) {
  151. char buffer[STRERROR_LEN];
  152. failf(data, "reading from socket: %s",
  153. Curl_strerror(io_error, buffer, sizeof(buffer)));
  154. *err = CURLE_READ_ERROR;
  155. return -1;
  156. }
  157. DEBUGF(LOG_CF(data, cf, "cr_recv: read %ld TLS bytes", tls_bytes_read));
  158. rresult = rustls_connection_process_new_packets(rconn);
  159. if(rresult != RUSTLS_RESULT_OK) {
  160. rustls_error(rresult, errorbuf, sizeof(errorbuf), &errorlen);
  161. failf(data, "rustls_connection_process_new_packets: %.*s",
  162. errorlen, errorbuf);
  163. *err = map_error(rresult);
  164. return -1;
  165. }
  166. backend->data_pending = TRUE;
  167. while(plain_bytes_copied < plainlen) {
  168. rresult = rustls_connection_read(rconn,
  169. (uint8_t *)plainbuf + plain_bytes_copied,
  170. plainlen - plain_bytes_copied,
  171. &n);
  172. if(rresult == RUSTLS_RESULT_PLAINTEXT_EMPTY) {
  173. DEBUGF(LOG_CF(data, cf, "cr_recv: got PLAINTEXT_EMPTY. "
  174. "will try again later."));
  175. backend->data_pending = FALSE;
  176. break;
  177. }
  178. else if(rresult == RUSTLS_RESULT_UNEXPECTED_EOF) {
  179. failf(data, "rustls: peer closed TCP connection "
  180. "without first closing TLS connection");
  181. *err = CURLE_READ_ERROR;
  182. return -1;
  183. }
  184. else if(rresult != RUSTLS_RESULT_OK) {
  185. /* n always equals 0 in this case, don't need to check it */
  186. rustls_error(rresult, errorbuf, sizeof(errorbuf), &errorlen);
  187. failf(data, "rustls_connection_read: %.*s", errorlen, errorbuf);
  188. *err = CURLE_READ_ERROR;
  189. return -1;
  190. }
  191. else if(n == 0) {
  192. /* n == 0 indicates clean EOF, but we may have read some other
  193. plaintext bytes before we reached this. Break out of the loop
  194. so we can figure out whether to return success or EOF. */
  195. break;
  196. }
  197. else {
  198. DEBUGF(LOG_CF(data, cf, "cr_recv: got %ld plain bytes", n));
  199. plain_bytes_copied += n;
  200. }
  201. }
  202. if(plain_bytes_copied) {
  203. *err = CURLE_OK;
  204. return plain_bytes_copied;
  205. }
  206. /* If we wrote out 0 plaintext bytes, that means either we hit a clean EOF,
  207. OR we got a RUSTLS_RESULT_PLAINTEXT_EMPTY.
  208. If the latter, return CURLE_AGAIN so curl doesn't treat this as EOF. */
  209. if(!backend->data_pending) {
  210. *err = CURLE_AGAIN;
  211. return -1;
  212. }
  213. /* Zero bytes read, and no RUSTLS_RESULT_PLAINTEXT_EMPTY, means the TCP
  214. connection was cleanly closed (with a close_notify alert). */
  215. *err = CURLE_OK;
  216. return 0;
  217. }
  218. /*
  219. * On each call:
  220. * - Copy `plainlen` bytes into rustls' plaintext input buffer (if > 0).
  221. * - Fully drain rustls' plaintext output buffer into the socket until
  222. * we get either an error or EAGAIN/EWOULDBLOCK.
  223. *
  224. * It's okay to call this function with plainbuf == NULL and plainlen == 0.
  225. * In that case, it won't read anything into rustls' plaintext input buffer.
  226. * It will only drain rustls' plaintext output buffer into the socket.
  227. */
  228. static ssize_t
  229. cr_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  230. const void *plainbuf, size_t plainlen, CURLcode *err)
  231. {
  232. struct ssl_connect_data *const connssl = cf->ctx;
  233. struct ssl_backend_data *const backend = connssl->backend;
  234. struct rustls_connection *rconn = NULL;
  235. struct io_ctx io_ctx;
  236. size_t plainwritten = 0;
  237. size_t tlswritten = 0;
  238. size_t tlswritten_total = 0;
  239. rustls_result rresult;
  240. rustls_io_result io_error;
  241. char errorbuf[256];
  242. size_t errorlen;
  243. DEBUGASSERT(backend);
  244. rconn = backend->conn;
  245. DEBUGF(LOG_CF(data, cf, "cr_send: %ld plain bytes", plainlen));
  246. if(plainlen > 0) {
  247. rresult = rustls_connection_write(rconn, plainbuf, plainlen,
  248. &plainwritten);
  249. if(rresult != RUSTLS_RESULT_OK) {
  250. rustls_error(rresult, errorbuf, sizeof(errorbuf), &errorlen);
  251. failf(data, "rustls_connection_write: %.*s", errorlen, errorbuf);
  252. *err = CURLE_WRITE_ERROR;
  253. return -1;
  254. }
  255. else if(plainwritten == 0) {
  256. failf(data, "rustls_connection_write: EOF");
  257. *err = CURLE_WRITE_ERROR;
  258. return -1;
  259. }
  260. }
  261. io_ctx.cf = cf;
  262. io_ctx.data = data;
  263. while(rustls_connection_wants_write(rconn)) {
  264. io_error = rustls_connection_write_tls(rconn, write_cb, &io_ctx,
  265. &tlswritten);
  266. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  267. DEBUGF(LOG_CF(data, cf, "cr_send: EAGAIN after %zu bytes",
  268. tlswritten_total));
  269. *err = CURLE_AGAIN;
  270. return -1;
  271. }
  272. else if(io_error) {
  273. char buffer[STRERROR_LEN];
  274. failf(data, "writing to socket: %s",
  275. Curl_strerror(io_error, buffer, sizeof(buffer)));
  276. *err = CURLE_WRITE_ERROR;
  277. return -1;
  278. }
  279. if(tlswritten == 0) {
  280. failf(data, "EOF in swrite");
  281. *err = CURLE_WRITE_ERROR;
  282. return -1;
  283. }
  284. DEBUGF(LOG_CF(data, cf, "cr_send: wrote %zu TLS bytes", tlswritten));
  285. tlswritten_total += tlswritten;
  286. }
  287. return plainwritten;
  288. }
  289. /* A server certificate verify callback for rustls that always returns
  290. RUSTLS_RESULT_OK, or in other words disable certificate verification. */
  291. static enum rustls_result
  292. cr_verify_none(void *userdata UNUSED_PARAM,
  293. const rustls_verify_server_cert_params *params UNUSED_PARAM)
  294. {
  295. return RUSTLS_RESULT_OK;
  296. }
  297. static bool
  298. cr_hostname_is_ip(const char *hostname)
  299. {
  300. struct in_addr in;
  301. #ifdef ENABLE_IPV6
  302. struct in6_addr in6;
  303. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0) {
  304. return true;
  305. }
  306. #endif /* ENABLE_IPV6 */
  307. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  308. return true;
  309. }
  310. return false;
  311. }
  312. static CURLcode
  313. cr_init_backend(struct Curl_cfilter *cf, struct Curl_easy *data,
  314. struct ssl_backend_data *const backend)
  315. {
  316. struct ssl_connect_data *connssl = cf->ctx;
  317. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  318. struct rustls_connection *rconn = NULL;
  319. struct rustls_client_config_builder *config_builder = NULL;
  320. struct rustls_root_cert_store *roots = NULL;
  321. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  322. const char * const ssl_cafile =
  323. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  324. (ca_info_blob ? NULL : conn_config->CAfile);
  325. const bool verifypeer = conn_config->verifypeer;
  326. const char *hostname = connssl->hostname;
  327. char errorbuf[256];
  328. size_t errorlen;
  329. int result;
  330. DEBUGASSERT(backend);
  331. rconn = backend->conn;
  332. config_builder = rustls_client_config_builder_new();
  333. if(connssl->alpn) {
  334. struct alpn_proto_buf proto;
  335. rustls_slice_bytes alpn[ALPN_ENTRIES_MAX];
  336. size_t i;
  337. for(i = 0; i < connssl->alpn->count; ++i) {
  338. alpn[i].data = (const uint8_t *)connssl->alpn->entries[i];
  339. alpn[i].len = strlen(connssl->alpn->entries[i]);
  340. }
  341. rustls_client_config_builder_set_alpn_protocols(config_builder, alpn,
  342. connssl->alpn->count);
  343. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  344. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  345. }
  346. if(!verifypeer) {
  347. rustls_client_config_builder_dangerous_set_certificate_verifier(
  348. config_builder, cr_verify_none);
  349. /* rustls doesn't support IP addresses (as of 0.19.0), and will reject
  350. * connections created with an IP address, even when certificate
  351. * verification is turned off. Set a placeholder hostname and disable
  352. * SNI. */
  353. if(cr_hostname_is_ip(hostname)) {
  354. rustls_client_config_builder_set_enable_sni(config_builder, false);
  355. hostname = "example.invalid";
  356. }
  357. }
  358. else if(ca_info_blob) {
  359. roots = rustls_root_cert_store_new();
  360. /* Enable strict parsing only if verification isn't disabled. */
  361. result = rustls_root_cert_store_add_pem(roots, ca_info_blob->data,
  362. ca_info_blob->len, verifypeer);
  363. if(result != RUSTLS_RESULT_OK) {
  364. failf(data, "rustls: failed to parse trusted certificates from blob");
  365. rustls_root_cert_store_free(roots);
  366. rustls_client_config_free(
  367. rustls_client_config_builder_build(config_builder));
  368. return CURLE_SSL_CACERT_BADFILE;
  369. }
  370. result = rustls_client_config_builder_use_roots(config_builder, roots);
  371. rustls_root_cert_store_free(roots);
  372. if(result != RUSTLS_RESULT_OK) {
  373. failf(data, "rustls: failed to load trusted certificates");
  374. rustls_client_config_free(
  375. rustls_client_config_builder_build(config_builder));
  376. return CURLE_SSL_CACERT_BADFILE;
  377. }
  378. }
  379. else if(ssl_cafile) {
  380. result = rustls_client_config_builder_load_roots_from_file(
  381. config_builder, ssl_cafile);
  382. if(result != RUSTLS_RESULT_OK) {
  383. failf(data, "rustls: failed to load trusted certificates");
  384. rustls_client_config_free(
  385. rustls_client_config_builder_build(config_builder));
  386. return CURLE_SSL_CACERT_BADFILE;
  387. }
  388. }
  389. backend->config = rustls_client_config_builder_build(config_builder);
  390. DEBUGASSERT(rconn == NULL);
  391. {
  392. char *snihost = Curl_ssl_snihost(data, hostname, NULL);
  393. if(!snihost) {
  394. failf(data, "rustls: failed to get SNI");
  395. return CURLE_SSL_CONNECT_ERROR;
  396. }
  397. result = rustls_client_connection_new(backend->config, snihost, &rconn);
  398. }
  399. if(result != RUSTLS_RESULT_OK) {
  400. rustls_error(result, errorbuf, sizeof(errorbuf), &errorlen);
  401. failf(data, "rustls_client_connection_new: %.*s", errorlen, errorbuf);
  402. return CURLE_COULDNT_CONNECT;
  403. }
  404. rustls_connection_set_userdata(rconn, backend);
  405. backend->conn = rconn;
  406. return CURLE_OK;
  407. }
  408. static void
  409. cr_set_negotiated_alpn(struct Curl_cfilter *cf, struct Curl_easy *data,
  410. const struct rustls_connection *rconn)
  411. {
  412. const uint8_t *protocol = NULL;
  413. size_t len = 0;
  414. rustls_connection_get_alpn_protocol(rconn, &protocol, &len);
  415. Curl_alpn_set_negotiated(cf, data, protocol, len);
  416. }
  417. static CURLcode
  418. cr_connect_nonblocking(struct Curl_cfilter *cf,
  419. struct Curl_easy *data, bool *done)
  420. {
  421. struct ssl_connect_data *const connssl = cf->ctx;
  422. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  423. struct ssl_backend_data *const backend = connssl->backend;
  424. struct rustls_connection *rconn = NULL;
  425. CURLcode tmperr = CURLE_OK;
  426. int result;
  427. int what;
  428. bool wants_read;
  429. bool wants_write;
  430. curl_socket_t writefd;
  431. curl_socket_t readfd;
  432. DEBUGASSERT(backend);
  433. if(ssl_connection_none == connssl->state) {
  434. result = cr_init_backend(cf, data, connssl->backend);
  435. if(result != CURLE_OK) {
  436. return result;
  437. }
  438. connssl->state = ssl_connection_negotiating;
  439. }
  440. rconn = backend->conn;
  441. /* Read/write data until the handshake is done or the socket would block. */
  442. for(;;) {
  443. /*
  444. * Connection has been established according to rustls. Set send/recv
  445. * handlers, and update the state machine.
  446. */
  447. if(!rustls_connection_is_handshaking(rconn)) {
  448. infof(data, "Done handshaking");
  449. /* Done with the handshake. Set up callbacks to send/receive data. */
  450. connssl->state = ssl_connection_complete;
  451. cr_set_negotiated_alpn(cf, data, rconn);
  452. *done = TRUE;
  453. return CURLE_OK;
  454. }
  455. wants_read = rustls_connection_wants_read(rconn);
  456. wants_write = rustls_connection_wants_write(rconn);
  457. DEBUGASSERT(wants_read || wants_write);
  458. writefd = wants_write?sockfd:CURL_SOCKET_BAD;
  459. readfd = wants_read?sockfd:CURL_SOCKET_BAD;
  460. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd, 0);
  461. if(what < 0) {
  462. /* fatal error */
  463. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  464. return CURLE_SSL_CONNECT_ERROR;
  465. }
  466. if(0 == what) {
  467. infof(data, "Curl_socket_check: %s would block",
  468. wants_read&&wants_write ? "writing and reading" :
  469. wants_write ? "writing" : "reading");
  470. *done = FALSE;
  471. return CURLE_OK;
  472. }
  473. /* socket is readable or writable */
  474. if(wants_write) {
  475. infof(data, "rustls_connection wants us to write_tls.");
  476. cr_send(cf, data, NULL, 0, &tmperr);
  477. if(tmperr == CURLE_AGAIN) {
  478. infof(data, "writing would block");
  479. /* fall through */
  480. }
  481. else if(tmperr != CURLE_OK) {
  482. return tmperr;
  483. }
  484. }
  485. if(wants_read) {
  486. infof(data, "rustls_connection wants us to read_tls.");
  487. cr_recv(cf, data, NULL, 0, &tmperr);
  488. if(tmperr == CURLE_AGAIN) {
  489. infof(data, "reading would block");
  490. /* fall through */
  491. }
  492. else if(tmperr != CURLE_OK) {
  493. if(tmperr == CURLE_READ_ERROR) {
  494. return CURLE_SSL_CONNECT_ERROR;
  495. }
  496. else {
  497. return tmperr;
  498. }
  499. }
  500. }
  501. }
  502. /* We should never fall through the loop. We should return either because
  503. the handshake is done or because we can't read/write without blocking. */
  504. DEBUGASSERT(false);
  505. }
  506. /* returns a bitmap of flags for this connection's first socket indicating
  507. whether we want to read or write */
  508. static int
  509. cr_get_select_socks(struct Curl_cfilter *cf, struct Curl_easy *data,
  510. curl_socket_t *socks)
  511. {
  512. struct ssl_connect_data *const connssl = cf->ctx;
  513. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  514. struct ssl_backend_data *const backend = connssl->backend;
  515. struct rustls_connection *rconn = NULL;
  516. (void)data;
  517. DEBUGASSERT(backend);
  518. rconn = backend->conn;
  519. if(rustls_connection_wants_write(rconn)) {
  520. socks[0] = sockfd;
  521. return GETSOCK_WRITESOCK(0);
  522. }
  523. if(rustls_connection_wants_read(rconn)) {
  524. socks[0] = sockfd;
  525. return GETSOCK_READSOCK(0);
  526. }
  527. return GETSOCK_BLANK;
  528. }
  529. static void *
  530. cr_get_internals(struct ssl_connect_data *connssl,
  531. CURLINFO info UNUSED_PARAM)
  532. {
  533. struct ssl_backend_data *backend = connssl->backend;
  534. DEBUGASSERT(backend);
  535. return &backend->conn;
  536. }
  537. static void
  538. cr_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  539. {
  540. struct ssl_connect_data *connssl = cf->ctx;
  541. struct ssl_backend_data *backend = connssl->backend;
  542. CURLcode tmperr = CURLE_OK;
  543. ssize_t n = 0;
  544. DEBUGASSERT(backend);
  545. if(backend->conn) {
  546. rustls_connection_send_close_notify(backend->conn);
  547. n = cr_send(cf, data, NULL, 0, &tmperr);
  548. if(n < 0) {
  549. failf(data, "rustls: error sending close_notify: %d", tmperr);
  550. }
  551. rustls_connection_free(backend->conn);
  552. backend->conn = NULL;
  553. }
  554. if(backend->config) {
  555. rustls_client_config_free(backend->config);
  556. backend->config = NULL;
  557. }
  558. }
  559. static size_t cr_version(char *buffer, size_t size)
  560. {
  561. struct rustls_str ver = rustls_version();
  562. return msnprintf(buffer, size, "%.*s", (int)ver.len, ver.data);
  563. }
  564. const struct Curl_ssl Curl_ssl_rustls = {
  565. { CURLSSLBACKEND_RUSTLS, "rustls" },
  566. SSLSUPP_CAINFO_BLOB | /* supports */
  567. SSLSUPP_TLS13_CIPHERSUITES |
  568. SSLSUPP_HTTPS_PROXY,
  569. sizeof(struct ssl_backend_data),
  570. Curl_none_init, /* init */
  571. Curl_none_cleanup, /* cleanup */
  572. cr_version, /* version */
  573. Curl_none_check_cxn, /* check_cxn */
  574. Curl_none_shutdown, /* shutdown */
  575. cr_data_pending, /* data_pending */
  576. Curl_none_random, /* random */
  577. Curl_none_cert_status_request, /* cert_status_request */
  578. cr_connect, /* connect */
  579. cr_connect_nonblocking, /* connect_nonblocking */
  580. cr_get_select_socks, /* get_select_socks */
  581. cr_get_internals, /* get_internals */
  582. cr_close, /* close_one */
  583. Curl_none_close_all, /* close_all */
  584. Curl_none_session_free, /* session_free */
  585. Curl_none_set_engine, /* set_engine */
  586. Curl_none_set_engine_default, /* set_engine_default */
  587. Curl_none_engines_list, /* engines_list */
  588. Curl_none_false_start, /* false_start */
  589. NULL, /* sha256sum */
  590. NULL, /* associate_connection */
  591. NULL, /* disassociate_connection */
  592. NULL, /* free_multi_ssl_backend_data */
  593. cr_recv, /* recv decrypted data */
  594. cr_send, /* send data to encrypt */
  595. };
  596. #endif /* USE_RUSTLS */