rustls.c 25 KB

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