rustls.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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 <rustls.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. infof(data, "cr_recv read %ld bytes from the network", tls_bytes_read);
  130. rresult = rustls_connection_process_new_packets(rconn);
  131. if(rresult != RUSTLS_RESULT_OK) {
  132. rustls_error(rresult, errorbuf, sizeof(errorbuf), &n);
  133. failf(data, "%.*s", n, errorbuf);
  134. *err = map_error(rresult);
  135. return -1;
  136. }
  137. backend->data_pending = TRUE;
  138. while(plain_bytes_copied < plainlen) {
  139. rresult = rustls_connection_read(rconn,
  140. (uint8_t *)plainbuf + plain_bytes_copied,
  141. plainlen - plain_bytes_copied,
  142. &n);
  143. if(rresult == RUSTLS_RESULT_PLAINTEXT_EMPTY) {
  144. infof(data, "cr_recv got PLAINTEXT_EMPTY. will try again later.");
  145. backend->data_pending = FALSE;
  146. break;
  147. }
  148. else if(rresult != RUSTLS_RESULT_OK) {
  149. /* n always equals 0 in this case, don't need to check it */
  150. failf(data, "error in rustls_connection_read: %d", rresult);
  151. *err = CURLE_READ_ERROR;
  152. return -1;
  153. }
  154. else if(n == 0) {
  155. /* n == 0 indicates clean EOF, but we may have read some other
  156. plaintext bytes before we reached this. Break out of the loop
  157. so we can figure out whether to return success or EOF. */
  158. break;
  159. }
  160. else {
  161. infof(data, "cr_recv copied out %ld bytes of plaintext", n);
  162. plain_bytes_copied += n;
  163. }
  164. }
  165. if(plain_bytes_copied) {
  166. *err = CURLE_OK;
  167. return plain_bytes_copied;
  168. }
  169. /* If we wrote out 0 plaintext bytes, that means either we hit a clean EOF,
  170. OR we got a RUSTLS_RESULT_PLAINTEXT_EMPTY.
  171. If the latter, return CURLE_AGAIN so curl doesn't treat this as EOF. */
  172. if(!backend->data_pending) {
  173. *err = CURLE_AGAIN;
  174. return -1;
  175. }
  176. /* Zero bytes read, and no RUSTLS_RESULT_PLAINTEXT_EMPTY, means the TCP
  177. connection was cleanly closed (with a close_notify alert). */
  178. *err = CURLE_OK;
  179. return 0;
  180. }
  181. /*
  182. * On each call:
  183. * - Copy `plainlen` bytes into rustls' plaintext input buffer (if > 0).
  184. * - Fully drain rustls' plaintext output buffer into the socket until
  185. * we get either an error or EAGAIN/EWOULDBLOCK.
  186. *
  187. * It's okay to call this function with plainbuf == NULL and plainlen == 0.
  188. * In that case, it won't read anything into rustls' plaintext input buffer.
  189. * It will only drain rustls' plaintext output buffer into the socket.
  190. */
  191. static ssize_t
  192. cr_send(struct Curl_easy *data, int sockindex,
  193. const void *plainbuf, size_t plainlen, CURLcode *err)
  194. {
  195. struct connectdata *conn = data->conn;
  196. struct ssl_connect_data *const connssl = &conn->ssl[sockindex];
  197. struct ssl_backend_data *const backend = connssl->backend;
  198. struct rustls_connection *const rconn = backend->conn;
  199. size_t plainwritten = 0;
  200. size_t tlswritten = 0;
  201. size_t tlswritten_total = 0;
  202. rustls_result rresult;
  203. rustls_io_result io_error;
  204. infof(data, "cr_send %ld bytes of plaintext", plainlen);
  205. if(plainlen > 0) {
  206. rresult = rustls_connection_write(rconn, plainbuf, plainlen,
  207. &plainwritten);
  208. if(rresult != RUSTLS_RESULT_OK) {
  209. failf(data, "error in rustls_connection_write");
  210. *err = CURLE_WRITE_ERROR;
  211. return -1;
  212. }
  213. else if(plainwritten == 0) {
  214. failf(data, "EOF in rustls_connection_write");
  215. *err = CURLE_WRITE_ERROR;
  216. return -1;
  217. }
  218. }
  219. while(rustls_connection_wants_write(rconn)) {
  220. io_error = rustls_connection_write_tls(rconn, write_cb,
  221. &conn->sock[sockindex], &tlswritten);
  222. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  223. infof(data, "swrite: EAGAIN after %ld bytes", tlswritten_total);
  224. *err = CURLE_AGAIN;
  225. return -1;
  226. }
  227. else if(io_error) {
  228. char buffer[STRERROR_LEN];
  229. failf(data, "writing to socket: %s",
  230. Curl_strerror(io_error, buffer, sizeof(buffer)));
  231. *err = CURLE_WRITE_ERROR;
  232. return -1;
  233. }
  234. if(tlswritten == 0) {
  235. failf(data, "EOF in swrite");
  236. *err = CURLE_WRITE_ERROR;
  237. return -1;
  238. }
  239. infof(data, "cr_send wrote %ld bytes to network", tlswritten);
  240. tlswritten_total += tlswritten;
  241. }
  242. return plainwritten;
  243. }
  244. /* A server certificate verify callback for rustls that always returns
  245. RUSTLS_RESULT_OK, or in other words disable certificate verification. */
  246. static enum rustls_result
  247. cr_verify_none(void *userdata UNUSED_PARAM,
  248. const rustls_verify_server_cert_params *params UNUSED_PARAM)
  249. {
  250. return RUSTLS_RESULT_OK;
  251. }
  252. static bool
  253. cr_hostname_is_ip(const char *hostname)
  254. {
  255. struct in_addr in;
  256. #ifdef ENABLE_IPV6
  257. struct in6_addr in6;
  258. if(Curl_inet_pton(AF_INET6, hostname, &in6) > 0) {
  259. return true;
  260. }
  261. #endif /* ENABLE_IPV6 */
  262. if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
  263. return true;
  264. }
  265. return false;
  266. }
  267. static CURLcode
  268. cr_init_backend(struct Curl_easy *data, struct connectdata *conn,
  269. struct ssl_backend_data *const backend)
  270. {
  271. struct rustls_connection *rconn = backend->conn;
  272. struct rustls_client_config_builder *config_builder = NULL;
  273. const char *const ssl_cafile = SSL_CONN_CONFIG(CAfile);
  274. const bool verifypeer = SSL_CONN_CONFIG(verifypeer);
  275. const char *hostname = conn->host.name;
  276. char errorbuf[256];
  277. size_t errorlen;
  278. int result;
  279. rustls_slice_bytes alpn[2] = {
  280. { (const uint8_t *)ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH },
  281. { (const uint8_t *)ALPN_H2, ALPN_H2_LENGTH },
  282. };
  283. config_builder = rustls_client_config_builder_new();
  284. #ifdef USE_HTTP2
  285. infof(data, "offering ALPN for HTTP/1.1 and HTTP/2");
  286. rustls_client_config_builder_set_alpn_protocols(config_builder, alpn, 2);
  287. #else
  288. infof(data, "offering ALPN for HTTP/1.1 only");
  289. rustls_client_config_builder_set_alpn_protocols(config_builder, alpn, 1);
  290. #endif
  291. if(!verifypeer) {
  292. rustls_client_config_builder_dangerous_set_certificate_verifier(
  293. config_builder, cr_verify_none);
  294. /* rustls doesn't support IP addresses (as of 0.19.0), and will reject
  295. * connections created with an IP address, even when certificate
  296. * verification is turned off. Set a placeholder hostname and disable
  297. * SNI. */
  298. if(cr_hostname_is_ip(hostname)) {
  299. rustls_client_config_builder_set_enable_sni(config_builder, false);
  300. hostname = "example.invalid";
  301. }
  302. }
  303. else if(ssl_cafile) {
  304. result = rustls_client_config_builder_load_roots_from_file(
  305. config_builder, ssl_cafile);
  306. if(result != RUSTLS_RESULT_OK) {
  307. failf(data, "failed to load trusted certificates");
  308. rustls_client_config_free(
  309. rustls_client_config_builder_build(config_builder));
  310. return CURLE_SSL_CACERT_BADFILE;
  311. }
  312. }
  313. backend->config = rustls_client_config_builder_build(config_builder);
  314. DEBUGASSERT(rconn == NULL);
  315. result = rustls_client_connection_new(backend->config, hostname, &rconn);
  316. if(result != RUSTLS_RESULT_OK) {
  317. rustls_error(result, errorbuf, sizeof(errorbuf), &errorlen);
  318. failf(data, "rustls_client_connection_new: %.*s", errorlen, errorbuf);
  319. return CURLE_COULDNT_CONNECT;
  320. }
  321. rustls_connection_set_userdata(rconn, backend);
  322. backend->conn = rconn;
  323. return CURLE_OK;
  324. }
  325. static void
  326. cr_set_negotiated_alpn(struct Curl_easy *data, struct connectdata *conn,
  327. const struct rustls_connection *rconn)
  328. {
  329. const uint8_t *protocol = NULL;
  330. size_t len = 0;
  331. rustls_connection_get_alpn_protocol(rconn, &protocol, &len);
  332. if(!protocol) {
  333. infof(data, "ALPN, server did not agree to a protocol");
  334. return;
  335. }
  336. #ifdef USE_HTTP2
  337. if(len == ALPN_H2_LENGTH && 0 == memcmp(ALPN_H2, protocol, len)) {
  338. infof(data, "ALPN, negotiated h2");
  339. conn->negnpn = CURL_HTTP_VERSION_2;
  340. }
  341. else
  342. #endif
  343. if(len == ALPN_HTTP_1_1_LENGTH &&
  344. 0 == memcmp(ALPN_HTTP_1_1, protocol, len)) {
  345. infof(data, "ALPN, negotiated http/1.1");
  346. conn->negnpn = CURL_HTTP_VERSION_1_1;
  347. }
  348. else {
  349. infof(data, "ALPN, negotiated an unrecognized protocol");
  350. }
  351. Curl_multiuse_state(data, conn->negnpn == CURL_HTTP_VERSION_2 ?
  352. BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
  353. }
  354. static CURLcode
  355. cr_connect_nonblocking(struct Curl_easy *data, struct connectdata *conn,
  356. int sockindex, bool *done)
  357. {
  358. struct ssl_connect_data *const connssl = &conn->ssl[sockindex];
  359. curl_socket_t sockfd = conn->sock[sockindex];
  360. struct ssl_backend_data *const backend = connssl->backend;
  361. struct rustls_connection *rconn = NULL;
  362. CURLcode tmperr = CURLE_OK;
  363. int result;
  364. int what;
  365. bool wants_read;
  366. bool wants_write;
  367. curl_socket_t writefd;
  368. curl_socket_t readfd;
  369. if(ssl_connection_none == connssl->state) {
  370. result = cr_init_backend(data, conn, connssl->backend);
  371. if(result != CURLE_OK) {
  372. return result;
  373. }
  374. connssl->state = ssl_connection_negotiating;
  375. }
  376. rconn = backend->conn;
  377. /* Read/write data until the handshake is done or the socket would block. */
  378. for(;;) {
  379. /*
  380. * Connection has been established according to rustls. Set send/recv
  381. * handlers, and update the state machine.
  382. */
  383. if(!rustls_connection_is_handshaking(rconn)) {
  384. infof(data, "Done handshaking");
  385. /* Done with the handshake. Set up callbacks to send/receive data. */
  386. connssl->state = ssl_connection_complete;
  387. cr_set_negotiated_alpn(data, conn, rconn);
  388. conn->recv[sockindex] = cr_recv;
  389. conn->send[sockindex] = cr_send;
  390. *done = TRUE;
  391. return CURLE_OK;
  392. }
  393. wants_read = rustls_connection_wants_read(rconn);
  394. wants_write = rustls_connection_wants_write(rconn);
  395. DEBUGASSERT(wants_read || wants_write);
  396. writefd = wants_write?sockfd:CURL_SOCKET_BAD;
  397. readfd = wants_read?sockfd:CURL_SOCKET_BAD;
  398. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd, 0);
  399. if(what < 0) {
  400. /* fatal error */
  401. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  402. return CURLE_SSL_CONNECT_ERROR;
  403. }
  404. if(0 == what) {
  405. infof(data, "Curl_socket_check: %s would block",
  406. wants_read&&wants_write ? "writing and reading" :
  407. wants_write ? "writing" : "reading");
  408. *done = FALSE;
  409. return CURLE_OK;
  410. }
  411. /* socket is readable or writable */
  412. if(wants_write) {
  413. infof(data, "rustls_connection wants us to write_tls.");
  414. cr_send(data, sockindex, NULL, 0, &tmperr);
  415. if(tmperr == CURLE_AGAIN) {
  416. infof(data, "writing would block");
  417. /* fall through */
  418. }
  419. else if(tmperr != CURLE_OK) {
  420. return tmperr;
  421. }
  422. }
  423. if(wants_read) {
  424. infof(data, "rustls_connection wants us to read_tls.");
  425. cr_recv(data, sockindex, NULL, 0, &tmperr);
  426. if(tmperr == CURLE_AGAIN) {
  427. infof(data, "reading would block");
  428. /* fall through */
  429. }
  430. else if(tmperr != CURLE_OK) {
  431. if(tmperr == CURLE_READ_ERROR) {
  432. return CURLE_SSL_CONNECT_ERROR;
  433. }
  434. else {
  435. return tmperr;
  436. }
  437. }
  438. }
  439. }
  440. /* We should never fall through the loop. We should return either because
  441. the handshake is done or because we can't read/write without blocking. */
  442. DEBUGASSERT(false);
  443. }
  444. /* returns a bitmap of flags for this connection's first socket indicating
  445. whether we want to read or write */
  446. static int
  447. cr_getsock(struct connectdata *conn, curl_socket_t *socks)
  448. {
  449. struct ssl_connect_data *const connssl = &conn->ssl[FIRSTSOCKET];
  450. curl_socket_t sockfd = conn->sock[FIRSTSOCKET];
  451. struct ssl_backend_data *const backend = connssl->backend;
  452. struct rustls_connection *rconn = backend->conn;
  453. if(rustls_connection_wants_write(rconn)) {
  454. socks[0] = sockfd;
  455. return GETSOCK_WRITESOCK(0);
  456. }
  457. if(rustls_connection_wants_read(rconn)) {
  458. socks[0] = sockfd;
  459. return GETSOCK_READSOCK(0);
  460. }
  461. return GETSOCK_BLANK;
  462. }
  463. static void *
  464. cr_get_internals(struct ssl_connect_data *connssl,
  465. CURLINFO info UNUSED_PARAM)
  466. {
  467. struct ssl_backend_data *backend = connssl->backend;
  468. return &backend->conn;
  469. }
  470. static void
  471. cr_close(struct Curl_easy *data, struct connectdata *conn,
  472. int sockindex)
  473. {
  474. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  475. struct ssl_backend_data *backend = connssl->backend;
  476. CURLcode tmperr = CURLE_OK;
  477. ssize_t n = 0;
  478. if(backend->conn) {
  479. rustls_connection_send_close_notify(backend->conn);
  480. n = cr_send(data, sockindex, NULL, 0, &tmperr);
  481. if(n < 0) {
  482. failf(data, "error sending close notify: %d", tmperr);
  483. }
  484. rustls_connection_free(backend->conn);
  485. backend->conn = NULL;
  486. }
  487. if(backend->config) {
  488. rustls_client_config_free(backend->config);
  489. backend->config = NULL;
  490. }
  491. }
  492. static size_t cr_version(char *buffer, size_t size)
  493. {
  494. struct rustls_str ver = rustls_version();
  495. return msnprintf(buffer, size, "%.*s", (int)ver.len, ver.data);
  496. }
  497. const struct Curl_ssl Curl_ssl_rustls = {
  498. { CURLSSLBACKEND_RUSTLS, "rustls" },
  499. SSLSUPP_TLS13_CIPHERSUITES, /* supports */
  500. sizeof(struct ssl_backend_data),
  501. Curl_none_init, /* init */
  502. Curl_none_cleanup, /* cleanup */
  503. cr_version, /* version */
  504. Curl_none_check_cxn, /* check_cxn */
  505. Curl_none_shutdown, /* shutdown */
  506. cr_data_pending, /* data_pending */
  507. Curl_none_random, /* random */
  508. Curl_none_cert_status_request, /* cert_status_request */
  509. cr_connect, /* connect */
  510. cr_connect_nonblocking, /* connect_nonblocking */
  511. cr_getsock, /* cr_getsock */
  512. cr_get_internals, /* get_internals */
  513. cr_close, /* close_one */
  514. Curl_none_close_all, /* close_all */
  515. Curl_none_session_free, /* session_free */
  516. Curl_none_set_engine, /* set_engine */
  517. Curl_none_set_engine_default, /* set_engine_default */
  518. Curl_none_engines_list, /* engines_list */
  519. Curl_none_false_start, /* false_start */
  520. NULL, /* sha256sum */
  521. NULL, /* associate_connection */
  522. NULL /* disassociate_connection */
  523. };
  524. #endif /* USE_RUSTLS */