rustls.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2020 - 2021, Jacob Hoffman-Andrews,
  9. * <[email protected]>
  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. ***************************************************************************/
  23. #include "curl_setup.h"
  24. #ifdef USE_RUSTLS
  25. #include "curl_printf.h"
  26. #include <errno.h>
  27. #include <crustls.h>
  28. #include "inet_pton.h"
  29. #include "urldata.h"
  30. #include "sendf.h"
  31. #include "vtls.h"
  32. #include "select.h"
  33. #include "strerror.h"
  34. #include "multiif.h"
  35. struct ssl_backend_data
  36. {
  37. const struct rustls_client_config *config;
  38. struct rustls_connection *conn;
  39. bool data_pending;
  40. };
  41. /* For a given rustls_result error code, return the best-matching CURLcode. */
  42. static CURLcode map_error(rustls_result r)
  43. {
  44. if(rustls_result_is_cert_error(r)) {
  45. return CURLE_PEER_FAILED_VERIFICATION;
  46. }
  47. switch(r) {
  48. case RUSTLS_RESULT_OK:
  49. return CURLE_OK;
  50. case RUSTLS_RESULT_NULL_PARAMETER:
  51. return CURLE_BAD_FUNCTION_ARGUMENT;
  52. default:
  53. return CURLE_READ_ERROR;
  54. }
  55. }
  56. static bool
  57. cr_data_pending(const struct connectdata *conn, int sockindex)
  58. {
  59. const struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  60. struct ssl_backend_data *backend = connssl->backend;
  61. return backend->data_pending;
  62. }
  63. static CURLcode
  64. cr_connect(struct Curl_easy *data UNUSED_PARAM,
  65. struct connectdata *conn UNUSED_PARAM,
  66. int sockindex UNUSED_PARAM)
  67. {
  68. infof(data, "rustls_connect: unimplemented");
  69. return CURLE_SSL_CONNECT_ERROR;
  70. }
  71. static int
  72. read_cb(void *userdata, uint8_t *buf, uintptr_t len, uintptr_t *out_n)
  73. {
  74. ssize_t n = sread(*(int *)userdata, buf, len);
  75. if(n < 0) {
  76. return SOCKERRNO;
  77. }
  78. *out_n = n;
  79. return 0;
  80. }
  81. static int
  82. write_cb(void *userdata, const uint8_t *buf, uintptr_t len, uintptr_t *out_n)
  83. {
  84. ssize_t n = swrite(*(int *)userdata, buf, len);
  85. if(n < 0) {
  86. return SOCKERRNO;
  87. }
  88. *out_n = n;
  89. return 0;
  90. }
  91. /*
  92. * On each run:
  93. * - Read a chunk of bytes from the socket into rustls' TLS input buffer.
  94. * - Tell rustls to process any new packets.
  95. * - Read out as many plaintext bytes from rustls as possible, until hitting
  96. * error, EOF, or EAGAIN/EWOULDBLOCK, or plainbuf/plainlen is filled up.
  97. *
  98. * It's okay to call this function with plainbuf == NULL and plainlen == 0.
  99. * In that case, it will copy bytes from the socket into rustls' TLS input
  100. * buffer, and process packets, but won't consume bytes from rustls' plaintext
  101. * output buffer.
  102. */
  103. static ssize_t
  104. cr_recv(struct Curl_easy *data, int sockindex,
  105. char *plainbuf, size_t plainlen, CURLcode *err)
  106. {
  107. struct connectdata *conn = data->conn;
  108. struct ssl_connect_data *const connssl = &conn->ssl[sockindex];
  109. struct ssl_backend_data *const backend = connssl->backend;
  110. struct rustls_connection *const rconn = backend->conn;
  111. size_t n = 0;
  112. size_t tls_bytes_read = 0;
  113. size_t plain_bytes_copied = 0;
  114. rustls_result rresult = 0;
  115. char errorbuf[255];
  116. rustls_io_result io_error;
  117. io_error = rustls_connection_read_tls(rconn, read_cb,
  118. &conn->sock[sockindex], &tls_bytes_read);
  119. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  120. infof(data, "sread: EAGAIN or EWOULDBLOCK");
  121. }
  122. else if(io_error) {
  123. char buffer[STRERROR_LEN];
  124. failf(data, "reading from socket: %s",
  125. Curl_strerror(io_error, buffer, sizeof(buffer)));
  126. *err = CURLE_READ_ERROR;
  127. return -1;
  128. }
  129. else if(tls_bytes_read == 0) {
  130. failf(data, "connection closed without TLS close_notify alert");
  131. *err = CURLE_READ_ERROR;
  132. return -1;
  133. }
  134. infof(data, "cr_recv read %ld bytes from the network", tls_bytes_read);
  135. rresult = rustls_connection_process_new_packets(rconn);
  136. if(rresult != RUSTLS_RESULT_OK) {
  137. rustls_error(rresult, errorbuf, sizeof(errorbuf), &n);
  138. failf(data, "%.*s", n, errorbuf);
  139. *err = map_error(rresult);
  140. return -1;
  141. }
  142. backend->data_pending = TRUE;
  143. while(plain_bytes_copied < plainlen) {
  144. rresult = rustls_connection_read(rconn,
  145. (uint8_t *)plainbuf + plain_bytes_copied,
  146. plainlen - plain_bytes_copied,
  147. &n);
  148. if(rresult == RUSTLS_RESULT_ALERT_CLOSE_NOTIFY) {
  149. *err = CURLE_OK;
  150. return 0;
  151. }
  152. else if(rresult != RUSTLS_RESULT_OK) {
  153. failf(data, "error in rustls_connection_read");
  154. *err = CURLE_READ_ERROR;
  155. return -1;
  156. }
  157. else if(n == 0) {
  158. /* rustls returns 0 from connection_read to mean "all currently
  159. available data has been read." If we bring in more ciphertext with
  160. read_tls, more plaintext will become available. So don't tell curl
  161. this is an EOF. Instead, say "come back later." */
  162. infof(data, "cr_recv got 0 bytes of plaintext");
  163. backend->data_pending = FALSE;
  164. break;
  165. }
  166. else {
  167. infof(data, "cr_recv copied out %ld bytes of plaintext", n);
  168. plain_bytes_copied += n;
  169. }
  170. }
  171. /* If we wrote out 0 plaintext bytes, it might just mean we haven't yet
  172. read a full TLS record. Return CURLE_AGAIN so curl doesn't treat this
  173. as EOF. */
  174. if(plain_bytes_copied == 0) {
  175. *err = CURLE_AGAIN;
  176. return -1;
  177. }
  178. return plain_bytes_copied;
  179. }
  180. /*
  181. * On each call:
  182. * - Copy `plainlen` bytes into rustls' plaintext input buffer (if > 0).
  183. * - Fully drain rustls' plaintext output buffer into the socket until
  184. * we get either an error or EAGAIN/EWOULDBLOCK.
  185. *
  186. * It's okay to call this function with plainbuf == NULL and plainlen == 0.
  187. * In that case, it won't read anything into rustls' plaintext input buffer.
  188. * It will only drain rustls' plaintext output buffer into the socket.
  189. */
  190. static ssize_t
  191. cr_send(struct Curl_easy *data, int sockindex,
  192. const void *plainbuf, size_t plainlen, CURLcode *err)
  193. {
  194. struct connectdata *conn = data->conn;
  195. struct ssl_connect_data *const connssl = &conn->ssl[sockindex];
  196. struct ssl_backend_data *const backend = connssl->backend;
  197. struct rustls_connection *const rconn = backend->conn;
  198. size_t plainwritten = 0;
  199. size_t tlswritten = 0;
  200. size_t tlswritten_total = 0;
  201. rustls_result rresult;
  202. rustls_io_result io_error;
  203. infof(data, "cr_send %ld bytes of plaintext", plainlen);
  204. if(plainlen > 0) {
  205. rresult = rustls_connection_write(rconn, plainbuf, plainlen,
  206. &plainwritten);
  207. if(rresult != RUSTLS_RESULT_OK) {
  208. failf(data, "error in rustls_connection_write");
  209. *err = CURLE_WRITE_ERROR;
  210. return -1;
  211. }
  212. else if(plainwritten == 0) {
  213. failf(data, "EOF in rustls_connection_write");
  214. *err = CURLE_WRITE_ERROR;
  215. return -1;
  216. }
  217. }
  218. while(rustls_connection_wants_write(rconn)) {
  219. io_error = rustls_connection_write_tls(rconn, write_cb,
  220. &conn->sock[sockindex], &tlswritten);
  221. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  222. infof(data, "swrite: EAGAIN after %ld bytes", tlswritten_total);
  223. *err = CURLE_AGAIN;
  224. return -1;
  225. }
  226. else if(io_error) {
  227. char buffer[STRERROR_LEN];
  228. failf(data, "writing to socket: %s",
  229. Curl_strerror(io_error, buffer, sizeof(buffer)));
  230. *err = CURLE_WRITE_ERROR;
  231. return -1;
  232. }
  233. if(tlswritten == 0) {
  234. failf(data, "EOF in swrite");
  235. *err = CURLE_WRITE_ERROR;
  236. return -1;
  237. }
  238. infof(data, "cr_send wrote %ld bytes to network", tlswritten);
  239. tlswritten_total += tlswritten;
  240. }
  241. return plainwritten;
  242. }
  243. /* A server certificate verify callback for rustls that always returns
  244. RUSTLS_RESULT_OK, or in other words disable certificate verification. */
  245. static enum rustls_result
  246. cr_verify_none(void *userdata UNUSED_PARAM,
  247. const rustls_verify_server_cert_params *params UNUSED_PARAM)
  248. {
  249. return RUSTLS_RESULT_OK;
  250. }
  251. static bool
  252. cr_hostname_is_ip(const char *hostname)
  253. {
  254. struct in_addr in;
  255. #ifdef ENABLE_IPV6
  256. struct in6_addr in6;
  257. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0) {
  258. return true;
  259. }
  260. #endif /* ENABLE_IPV6 */
  261. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  262. return true;
  263. }
  264. return false;
  265. }
  266. static CURLcode
  267. cr_init_backend(struct Curl_easy *data, struct connectdata *conn,
  268. struct ssl_backend_data *const backend)
  269. {
  270. struct rustls_connection *rconn = backend->conn;
  271. struct rustls_client_config_builder *config_builder = NULL;
  272. const char *const ssl_cafile = SSL_CONN_CONFIG(CAfile);
  273. const bool verifypeer = SSL_CONN_CONFIG(verifypeer);
  274. const char *hostname = conn->host.name;
  275. char errorbuf[256];
  276. size_t errorlen;
  277. int result;
  278. rustls_slice_bytes alpn[2] = {
  279. { (const uint8_t *)ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH },
  280. { (const uint8_t *)ALPN_H2, ALPN_H2_LENGTH },
  281. };
  282. config_builder = rustls_client_config_builder_new();
  283. #ifdef USE_HTTP2
  284. infof(data, "offering ALPN for HTTP/1.1 and HTTP/2");
  285. rustls_client_config_builder_set_protocols(config_builder, alpn, 2);
  286. #else
  287. infof(data, "offering ALPN for HTTP/1.1 only");
  288. rustls_client_config_builder_set_protocols(config_builder, alpn, 1);
  289. #endif
  290. if(!verifypeer) {
  291. rustls_client_config_builder_dangerous_set_certificate_verifier(
  292. config_builder, cr_verify_none);
  293. /* rustls doesn't support IP addresses (as of 0.19.0), and will reject
  294. * connections created with an IP address, even when certificate
  295. * verification is turned off. Set a placeholder hostname and disable
  296. * SNI. */
  297. if(cr_hostname_is_ip(hostname)) {
  298. rustls_client_config_builder_set_enable_sni(config_builder, false);
  299. hostname = "example.invalid";
  300. }
  301. }
  302. else if(ssl_cafile) {
  303. result = rustls_client_config_builder_load_roots_from_file(
  304. config_builder, ssl_cafile);
  305. if(result != RUSTLS_RESULT_OK) {
  306. failf(data, "failed to load trusted certificates");
  307. rustls_client_config_free(
  308. rustls_client_config_builder_build(config_builder));
  309. return CURLE_SSL_CACERT_BADFILE;
  310. }
  311. }
  312. backend->config = rustls_client_config_builder_build(config_builder);
  313. DEBUGASSERT(rconn == NULL);
  314. result = rustls_client_connection_new(backend->config, hostname, &rconn);
  315. if(result != RUSTLS_RESULT_OK) {
  316. rustls_error(result, errorbuf, sizeof(errorbuf), &errorlen);
  317. failf(data, "rustls_client_connection_new: %.*s", errorlen, errorbuf);
  318. return CURLE_COULDNT_CONNECT;
  319. }
  320. rustls_connection_set_userdata(rconn, backend);
  321. backend->conn = rconn;
  322. return CURLE_OK;
  323. }
  324. static void
  325. cr_set_negotiated_alpn(struct Curl_easy *data, struct connectdata *conn,
  326. const struct rustls_connection *rconn)
  327. {
  328. const uint8_t *protocol = NULL;
  329. size_t len = 0;
  330. rustls_connection_get_alpn_protocol(rconn, &protocol, &len);
  331. if(NULL == protocol) {
  332. infof(data, "ALPN, server did not agree to a protocol");
  333. return;
  334. }
  335. #ifdef USE_HTTP2
  336. if(len == ALPN_H2_LENGTH && 0 == memcmp(ALPN_H2, protocol, len)) {
  337. infof(data, "ALPN, negotiated h2");
  338. conn->negnpn = CURL_HTTP_VERSION_2;
  339. }
  340. else
  341. #endif
  342. if(len == ALPN_HTTP_1_1_LENGTH &&
  343. 0 == memcmp(ALPN_HTTP_1_1, protocol, len)) {
  344. infof(data, "ALPN, negotiated http/1.1");
  345. conn->negnpn = CURL_HTTP_VERSION_1_1;
  346. }
  347. else {
  348. infof(data, "ALPN, negotiated an unrecognized protocol");
  349. }
  350. Curl_multiuse_state(data, conn->negnpn == CURL_HTTP_VERSION_2 ?
  351. BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
  352. }
  353. static CURLcode
  354. cr_connect_nonblocking(struct Curl_easy *data, struct connectdata *conn,
  355. int sockindex, bool *done)
  356. {
  357. struct ssl_connect_data *const connssl = &conn->ssl[sockindex];
  358. curl_socket_t sockfd = conn->sock[sockindex];
  359. struct ssl_backend_data *const backend = connssl->backend;
  360. struct rustls_connection *rconn = NULL;
  361. CURLcode tmperr = CURLE_OK;
  362. int result;
  363. int what;
  364. bool wants_read;
  365. bool wants_write;
  366. curl_socket_t writefd;
  367. curl_socket_t readfd;
  368. if(ssl_connection_none == connssl->state) {
  369. result = cr_init_backend(data, conn, connssl->backend);
  370. if(result != CURLE_OK) {
  371. return result;
  372. }
  373. connssl->state = ssl_connection_negotiating;
  374. }
  375. rconn = backend->conn;
  376. /* Read/write data until the handshake is done or the socket would block. */
  377. for(;;) {
  378. /*
  379. * Connection has been established according to rustls. Set send/recv
  380. * handlers, and update the state machine.
  381. * This check has to come last because is_handshaking starts out false,
  382. * then becomes true when we first write data, then becomes false again
  383. * once the handshake is done.
  384. */
  385. if(!rustls_connection_is_handshaking(rconn)) {
  386. infof(data, "Done handshaking");
  387. /* Done with the handshake. Set up callbacks to send/receive data. */
  388. connssl->state = ssl_connection_complete;
  389. cr_set_negotiated_alpn(data, conn, rconn);
  390. conn->recv[sockindex] = cr_recv;
  391. conn->send[sockindex] = cr_send;
  392. *done = TRUE;
  393. return CURLE_OK;
  394. }
  395. wants_read = rustls_connection_wants_read(rconn);
  396. wants_write = rustls_connection_wants_write(rconn);
  397. DEBUGASSERT(wants_read || wants_write);
  398. writefd = wants_write?sockfd:CURL_SOCKET_BAD;
  399. readfd = wants_read?sockfd:CURL_SOCKET_BAD;
  400. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd, 0);
  401. if(what < 0) {
  402. /* fatal error */
  403. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  404. return CURLE_SSL_CONNECT_ERROR;
  405. }
  406. if(0 == what) {
  407. infof(data, "Curl_socket_check: %s would block",
  408. wants_read&&wants_write ? "writing and reading" :
  409. wants_write ? "writing" : "reading");
  410. *done = FALSE;
  411. return CURLE_OK;
  412. }
  413. /* socket is readable or writable */
  414. if(wants_write) {
  415. infof(data, "rustls_connection wants us to write_tls.");
  416. cr_send(data, sockindex, NULL, 0, &tmperr);
  417. if(tmperr == CURLE_AGAIN) {
  418. infof(data, "writing would block");
  419. /* fall through */
  420. }
  421. else if(tmperr != CURLE_OK) {
  422. return tmperr;
  423. }
  424. }
  425. if(wants_read) {
  426. infof(data, "rustls_connection wants us to read_tls.");
  427. cr_recv(data, sockindex, NULL, 0, &tmperr);
  428. if(tmperr == CURLE_AGAIN) {
  429. infof(data, "reading would block");
  430. /* fall through */
  431. }
  432. else if(tmperr != CURLE_OK) {
  433. if(tmperr == CURLE_READ_ERROR) {
  434. return CURLE_SSL_CONNECT_ERROR;
  435. }
  436. else {
  437. return tmperr;
  438. }
  439. }
  440. }
  441. }
  442. /* We should never fall through the loop. We should return either because
  443. the handshake is done or because we can't read/write without blocking. */
  444. DEBUGASSERT(false);
  445. }
  446. /* returns a bitmap of flags for this connection's first socket indicating
  447. whether we want to read or write */
  448. static int
  449. cr_getsock(struct connectdata *conn, curl_socket_t *socks)
  450. {
  451. struct ssl_connect_data *const connssl = &conn->ssl[FIRSTSOCKET];
  452. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  453. struct ssl_backend_data *const backend = connssl->backend;
  454. struct rustls_connection *rconn = backend->conn;
  455. if(rustls_connection_wants_write(rconn)) {
  456. socks[0] = sockfd;
  457. return GETSOCK_WRITESOCK(0);
  458. }
  459. if(rustls_connection_wants_read(rconn)) {
  460. socks[0] = sockfd;
  461. return GETSOCK_READSOCK(0);
  462. }
  463. return GETSOCK_BLANK;
  464. }
  465. static void *
  466. cr_get_internals(struct ssl_connect_data *connssl,
  467. CURLINFO info UNUSED_PARAM)
  468. {
  469. struct ssl_backend_data *backend = connssl->backend;
  470. return &backend->conn;
  471. }
  472. static void
  473. cr_close(struct Curl_easy *data, struct connectdata *conn,
  474. int sockindex)
  475. {
  476. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  477. struct ssl_backend_data *backend = connssl->backend;
  478. CURLcode tmperr = CURLE_OK;
  479. ssize_t n = 0;
  480. if(backend->conn) {
  481. rustls_connection_send_close_notify(backend->conn);
  482. n = cr_send(data, sockindex, NULL, 0, &tmperr);
  483. if(n < 0) {
  484. failf(data, "error sending close notify: %d", tmperr);
  485. }
  486. rustls_connection_free(backend->conn);
  487. backend->conn = NULL;
  488. }
  489. if(backend->config) {
  490. rustls_client_config_free(backend->config);
  491. backend->config = NULL;
  492. }
  493. }
  494. const struct Curl_ssl Curl_ssl_rustls = {
  495. { CURLSSLBACKEND_RUSTLS, "rustls" },
  496. SSLSUPP_TLS13_CIPHERSUITES, /* supports */
  497. sizeof(struct ssl_backend_data),
  498. Curl_none_init, /* init */
  499. Curl_none_cleanup, /* cleanup */
  500. rustls_version, /* version */
  501. Curl_none_check_cxn, /* check_cxn */
  502. Curl_none_shutdown, /* shutdown */
  503. cr_data_pending, /* data_pending */
  504. Curl_none_random, /* random */
  505. Curl_none_cert_status_request, /* cert_status_request */
  506. cr_connect, /* connect */
  507. cr_connect_nonblocking, /* connect_nonblocking */
  508. cr_getsock, /* cr_getsock */
  509. cr_get_internals, /* get_internals */
  510. cr_close, /* close_one */
  511. Curl_none_close_all, /* close_all */
  512. Curl_none_session_free, /* session_free */
  513. Curl_none_set_engine, /* set_engine */
  514. Curl_none_set_engine_default, /* set_engine_default */
  515. Curl_none_engines_list, /* engines_list */
  516. Curl_none_false_start, /* false_start */
  517. NULL, /* sha256sum */
  518. NULL, /* associate_connection */
  519. NULL /* disassociate_connection */
  520. };
  521. #endif /* USE_RUSTLS */