rustls.c 21 KB

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