2
0

rustls.c 24 KB

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