2
0

rustls.c 21 KB

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