rustls.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  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 "rustls.h"
  37. #include "select.h"
  38. #include "strerror.h"
  39. #include "multiif.h"
  40. #include "connect.h" /* for the connect timeout */
  41. #include "cipher_suite.h"
  42. #include "rand.h"
  43. struct rustls_ssl_backend_data
  44. {
  45. const struct rustls_client_config *config;
  46. struct rustls_connection *conn;
  47. size_t plain_out_buffered;
  48. BIT(data_in_pending);
  49. BIT(sent_shutdown);
  50. };
  51. /* For a given rustls_result error code, return the best-matching CURLcode. */
  52. static CURLcode map_error(rustls_result r)
  53. {
  54. if(rustls_result_is_cert_error(r)) {
  55. return CURLE_PEER_FAILED_VERIFICATION;
  56. }
  57. switch(r) {
  58. case RUSTLS_RESULT_OK:
  59. return CURLE_OK;
  60. case RUSTLS_RESULT_NULL_PARAMETER:
  61. return CURLE_BAD_FUNCTION_ARGUMENT;
  62. default:
  63. return CURLE_RECV_ERROR;
  64. }
  65. }
  66. static bool
  67. cr_data_pending(struct Curl_cfilter *cf, const struct Curl_easy *data)
  68. {
  69. struct ssl_connect_data *ctx = cf->ctx;
  70. struct rustls_ssl_backend_data *backend;
  71. (void)data;
  72. DEBUGASSERT(ctx && ctx->backend);
  73. backend = (struct rustls_ssl_backend_data *)ctx->backend;
  74. return backend->data_in_pending;
  75. }
  76. struct io_ctx {
  77. struct Curl_cfilter *cf;
  78. struct Curl_easy *data;
  79. };
  80. static int
  81. read_cb(void *userdata, uint8_t *buf, uintptr_t len, uintptr_t *out_n)
  82. {
  83. struct io_ctx *io_ctx = userdata;
  84. struct ssl_connect_data *const connssl = io_ctx->cf->ctx;
  85. CURLcode result;
  86. int ret = 0;
  87. ssize_t nread = Curl_conn_cf_recv(io_ctx->cf->next, io_ctx->data,
  88. (char *)buf, len, &result);
  89. if(nread < 0) {
  90. nread = 0;
  91. if(CURLE_AGAIN == result)
  92. ret = EAGAIN;
  93. else
  94. ret = EINVAL;
  95. }
  96. else if(nread == 0)
  97. connssl->peer_closed = TRUE;
  98. *out_n = (uintptr_t)nread;
  99. CURL_TRC_CF(io_ctx->data, io_ctx->cf, "cf->next recv(len=%zu) -> %zd, %d",
  100. len, nread, result);
  101. return ret;
  102. }
  103. static int
  104. write_cb(void *userdata, const uint8_t *buf, uintptr_t len, uintptr_t *out_n)
  105. {
  106. struct io_ctx *io_ctx = userdata;
  107. CURLcode result;
  108. int ret = 0;
  109. ssize_t nwritten = Curl_conn_cf_send(io_ctx->cf->next, io_ctx->data,
  110. (const char *)buf, len, FALSE,
  111. &result);
  112. if(nwritten < 0) {
  113. nwritten = 0;
  114. if(CURLE_AGAIN == result)
  115. ret = EAGAIN;
  116. else
  117. ret = EINVAL;
  118. }
  119. *out_n = (uintptr_t)nwritten;
  120. CURL_TRC_CF(io_ctx->data, io_ctx->cf, "cf->next send(len=%zu) -> %zd, %d",
  121. len, nwritten, result);
  122. return ret;
  123. }
  124. static ssize_t tls_recv_more(struct Curl_cfilter *cf,
  125. struct Curl_easy *data, CURLcode *err)
  126. {
  127. struct ssl_connect_data *const connssl = cf->ctx;
  128. struct rustls_ssl_backend_data *const backend =
  129. (struct rustls_ssl_backend_data *)connssl->backend;
  130. struct io_ctx io_ctx;
  131. size_t tls_bytes_read = 0;
  132. rustls_io_result io_error;
  133. rustls_result rresult = 0;
  134. io_ctx.cf = cf;
  135. io_ctx.data = data;
  136. io_error = rustls_connection_read_tls(backend->conn, read_cb, &io_ctx,
  137. &tls_bytes_read);
  138. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  139. *err = CURLE_AGAIN;
  140. return -1;
  141. }
  142. else if(io_error) {
  143. char buffer[STRERROR_LEN];
  144. failf(data, "reading from socket: %s",
  145. Curl_strerror(io_error, buffer, sizeof(buffer)));
  146. *err = CURLE_RECV_ERROR;
  147. return -1;
  148. }
  149. rresult = rustls_connection_process_new_packets(backend->conn);
  150. if(rresult != RUSTLS_RESULT_OK) {
  151. char errorbuf[255];
  152. size_t errorlen;
  153. rustls_error(rresult, errorbuf, sizeof(errorbuf), &errorlen);
  154. failf(data, "rustls_connection_process_new_packets: %.*s",
  155. (int)errorlen, errorbuf);
  156. *err = map_error(rresult);
  157. return -1;
  158. }
  159. backend->data_in_pending = TRUE;
  160. *err = CURLE_OK;
  161. return (ssize_t)tls_bytes_read;
  162. }
  163. /*
  164. * On each run:
  165. * - Read a chunk of bytes from the socket into Rustls' TLS input buffer.
  166. * - Tell Rustls to process any new packets.
  167. * - Read out as many plaintext bytes from Rustls as possible, until hitting
  168. * error, EOF, or EAGAIN/EWOULDBLOCK, or plainbuf/plainlen is filled up.
  169. *
  170. * it is okay to call this function with plainbuf == NULL and plainlen == 0. In
  171. * that case, it will copy bytes from the socket into Rustls' TLS input
  172. * buffer, and process packets, but will not consume bytes from Rustls'
  173. * plaintext output buffer.
  174. */
  175. static ssize_t
  176. cr_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  177. char *plainbuf, size_t plainlen, CURLcode *err)
  178. {
  179. struct ssl_connect_data *const connssl = cf->ctx;
  180. struct rustls_ssl_backend_data *const backend =
  181. (struct rustls_ssl_backend_data *)connssl->backend;
  182. struct rustls_connection *rconn = NULL;
  183. size_t n = 0;
  184. size_t plain_bytes_copied = 0;
  185. rustls_result rresult = 0;
  186. ssize_t nread;
  187. bool eof = FALSE;
  188. DEBUGASSERT(backend);
  189. rconn = backend->conn;
  190. while(plain_bytes_copied < plainlen) {
  191. if(!backend->data_in_pending) {
  192. if(tls_recv_more(cf, data, err) < 0) {
  193. if(*err != CURLE_AGAIN) {
  194. nread = -1;
  195. goto out;
  196. }
  197. break;
  198. }
  199. }
  200. rresult = rustls_connection_read(rconn,
  201. (uint8_t *)plainbuf + plain_bytes_copied,
  202. plainlen - plain_bytes_copied,
  203. &n);
  204. if(rresult == RUSTLS_RESULT_PLAINTEXT_EMPTY) {
  205. backend->data_in_pending = FALSE;
  206. }
  207. else if(rresult == RUSTLS_RESULT_UNEXPECTED_EOF) {
  208. failf(data, "rustls: peer closed TCP connection "
  209. "without first closing TLS connection");
  210. *err = CURLE_RECV_ERROR;
  211. nread = -1;
  212. goto out;
  213. }
  214. else if(rresult != RUSTLS_RESULT_OK) {
  215. /* n always equals 0 in this case, do not need to check it */
  216. char errorbuf[255];
  217. size_t errorlen;
  218. rustls_error(rresult, errorbuf, sizeof(errorbuf), &errorlen);
  219. failf(data, "rustls_connection_read: %.*s", (int)errorlen, errorbuf);
  220. *err = CURLE_RECV_ERROR;
  221. nread = -1;
  222. goto out;
  223. }
  224. else if(n == 0) {
  225. /* n == 0 indicates clean EOF, but we may have read some other
  226. plaintext bytes before we reached this. Break out of the loop
  227. so we can figure out whether to return success or EOF. */
  228. eof = TRUE;
  229. break;
  230. }
  231. else {
  232. plain_bytes_copied += n;
  233. }
  234. }
  235. if(plain_bytes_copied) {
  236. *err = CURLE_OK;
  237. nread = (ssize_t)plain_bytes_copied;
  238. }
  239. else if(eof) {
  240. *err = CURLE_OK;
  241. nread = 0;
  242. }
  243. else {
  244. *err = CURLE_AGAIN;
  245. nread = -1;
  246. }
  247. out:
  248. CURL_TRC_CF(data, cf, "cf_recv(len=%zu) -> %zd, %d",
  249. plainlen, nread, *err);
  250. return nread;
  251. }
  252. static CURLcode cr_flush_out(struct Curl_cfilter *cf, struct Curl_easy *data,
  253. struct rustls_connection *rconn)
  254. {
  255. struct io_ctx io_ctx;
  256. rustls_io_result io_error;
  257. size_t tlswritten = 0;
  258. size_t tlswritten_total = 0;
  259. CURLcode result = CURLE_OK;
  260. io_ctx.cf = cf;
  261. io_ctx.data = data;
  262. while(rustls_connection_wants_write(rconn)) {
  263. io_error = rustls_connection_write_tls(rconn, write_cb, &io_ctx,
  264. &tlswritten);
  265. if(io_error == EAGAIN || io_error == EWOULDBLOCK) {
  266. CURL_TRC_CF(data, cf, "cf_send: EAGAIN after %zu bytes",
  267. tlswritten_total);
  268. return CURLE_AGAIN;
  269. }
  270. else if(io_error) {
  271. char buffer[STRERROR_LEN];
  272. failf(data, "writing to socket: %s",
  273. Curl_strerror(io_error, buffer, sizeof(buffer)));
  274. return CURLE_SEND_ERROR;
  275. }
  276. if(tlswritten == 0) {
  277. failf(data, "EOF in swrite");
  278. return CURLE_SEND_ERROR;
  279. }
  280. CURL_TRC_CF(data, cf, "cf_send: wrote %zu TLS bytes", tlswritten);
  281. tlswritten_total += tlswritten;
  282. }
  283. return result;
  284. }
  285. /*
  286. * On each call:
  287. * - Copy `plainlen` bytes into Rustls' plaintext input buffer (if > 0).
  288. * - Fully drain Rustls' plaintext output buffer into the socket until
  289. * we get either an error or EAGAIN/EWOULDBLOCK.
  290. *
  291. * it is okay to call this function with plainbuf == NULL and plainlen == 0.
  292. * In that case, it will not read anything into Rustls' plaintext input buffer.
  293. * It will only drain Rustls' plaintext output buffer into the socket.
  294. */
  295. static ssize_t
  296. cr_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  297. const void *plainbuf, size_t plainlen, CURLcode *err)
  298. {
  299. struct ssl_connect_data *const connssl = cf->ctx;
  300. struct rustls_ssl_backend_data *const backend =
  301. (struct rustls_ssl_backend_data *)connssl->backend;
  302. struct rustls_connection *rconn = NULL;
  303. size_t plainwritten = 0;
  304. rustls_result rresult;
  305. char errorbuf[256];
  306. size_t errorlen;
  307. const unsigned char *buf = plainbuf;
  308. size_t blen = plainlen;
  309. ssize_t nwritten = 0;
  310. DEBUGASSERT(backend);
  311. rconn = backend->conn;
  312. DEBUGASSERT(rconn);
  313. CURL_TRC_CF(data, cf, "cf_send(len=%zu)", plainlen);
  314. /* If a previous send blocked, we already added its plain bytes
  315. * to rustsls and must not do that again. Flush the TLS bytes and,
  316. * if successful, deduct the previous plain bytes from the current
  317. * send. */
  318. if(backend->plain_out_buffered) {
  319. *err = cr_flush_out(cf, data, rconn);
  320. CURL_TRC_CF(data, cf, "cf_send: flushing %zu previously added bytes -> %d",
  321. backend->plain_out_buffered, *err);
  322. if(*err)
  323. return -1;
  324. if(blen > backend->plain_out_buffered) {
  325. blen -= backend->plain_out_buffered;
  326. buf += backend->plain_out_buffered;
  327. }
  328. else
  329. blen = 0;
  330. nwritten += (ssize_t)backend->plain_out_buffered;
  331. backend->plain_out_buffered = 0;
  332. }
  333. if(blen > 0) {
  334. CURL_TRC_CF(data, cf, "cf_send: adding %zu plain bytes to Rustls", blen);
  335. rresult = rustls_connection_write(rconn, buf, blen, &plainwritten);
  336. if(rresult != RUSTLS_RESULT_OK) {
  337. rustls_error(rresult, errorbuf, sizeof(errorbuf), &errorlen);
  338. failf(data, "rustls_connection_write: %.*s", (int)errorlen, errorbuf);
  339. *err = CURLE_WRITE_ERROR;
  340. return -1;
  341. }
  342. else if(plainwritten == 0) {
  343. failf(data, "rustls_connection_write: EOF");
  344. *err = CURLE_WRITE_ERROR;
  345. return -1;
  346. }
  347. }
  348. *err = cr_flush_out(cf, data, rconn);
  349. if(*err) {
  350. if(CURLE_AGAIN == *err) {
  351. /* The TLS bytes may have been partially written, but we fail the
  352. * complete send() and remember how much we already added to Rustls. */
  353. CURL_TRC_CF(data, cf, "cf_send: EAGAIN, remember we added %zu plain"
  354. " bytes already to Rustls", blen);
  355. backend->plain_out_buffered = plainwritten;
  356. if(nwritten) {
  357. *err = CURLE_OK;
  358. return (ssize_t)nwritten;
  359. }
  360. }
  361. return -1;
  362. }
  363. else
  364. nwritten += (ssize_t)plainwritten;
  365. CURL_TRC_CF(data, cf, "cf_send(len=%zu) -> %d, %zd",
  366. plainlen, *err, nwritten);
  367. return nwritten;
  368. }
  369. /* A server certificate verify callback for Rustls that always returns
  370. RUSTLS_RESULT_OK, or in other words disable certificate verification. */
  371. static uint32_t
  372. cr_verify_none(void *userdata UNUSED_PARAM,
  373. const rustls_verify_server_cert_params *params UNUSED_PARAM)
  374. {
  375. return RUSTLS_RESULT_OK;
  376. }
  377. static int
  378. read_file_into(const char *filename,
  379. struct dynbuf *out)
  380. {
  381. FILE *f = fopen(filename, FOPEN_READTEXT);
  382. if(!f) {
  383. return 0;
  384. }
  385. while(!feof(f)) {
  386. uint8_t buf[256];
  387. size_t rr = fread(buf, 1, sizeof(buf), f);
  388. if(rr == 0 ||
  389. CURLE_OK != Curl_dyn_addn(out, buf, rr)) {
  390. fclose(f);
  391. return 0;
  392. }
  393. }
  394. return fclose(f) == 0;
  395. }
  396. static void
  397. cr_get_selected_ciphers(struct Curl_easy *data,
  398. const char *ciphers12,
  399. const char *ciphers13,
  400. const struct rustls_supported_ciphersuite **selected,
  401. size_t *selected_size)
  402. {
  403. size_t supported_len = *selected_size;
  404. size_t default_len = rustls_default_crypto_provider_ciphersuites_len();
  405. const struct rustls_supported_ciphersuite *entry;
  406. const char *ciphers = ciphers12;
  407. size_t count = 0, default13_count = 0, i, j;
  408. const char *ptr, *end;
  409. DEBUGASSERT(default_len <= supported_len);
  410. if(!ciphers13) {
  411. /* Add default TLSv1.3 ciphers to selection */
  412. for(j = 0; j < default_len; j++) {
  413. entry = rustls_default_crypto_provider_ciphersuites_get(j);
  414. if(rustls_supported_ciphersuite_protocol_version(entry) !=
  415. RUSTLS_TLS_VERSION_TLSV1_3)
  416. continue;
  417. selected[count++] = entry;
  418. }
  419. default13_count = count;
  420. if(!ciphers)
  421. ciphers = "";
  422. }
  423. else
  424. ciphers = ciphers13;
  425. add_ciphers:
  426. for(ptr = ciphers; ptr[0] != '\0' && count < supported_len; ptr = end) {
  427. uint16_t id = Curl_cipher_suite_walk_str(&ptr, &end);
  428. /* Check if cipher is supported */
  429. if(id) {
  430. for(i = 0; i < supported_len; i++) {
  431. entry = rustls_default_crypto_provider_ciphersuites_get(i);
  432. if(rustls_supported_ciphersuite_get_suite(entry) == id)
  433. break;
  434. }
  435. if(i == supported_len)
  436. id = 0;
  437. }
  438. if(!id) {
  439. if(ptr[0] != '\0')
  440. infof(data, "rustls: unknown cipher in list: \"%.*s\"",
  441. (int) (end - ptr), ptr);
  442. continue;
  443. }
  444. /* No duplicates allowed (so selected cannot overflow) */
  445. for(i = 0; i < count && selected[i] != entry; i++);
  446. if(i < count) {
  447. if(i >= default13_count)
  448. infof(data, "rustls: duplicate cipher in list: \"%.*s\"",
  449. (int) (end - ptr), ptr);
  450. continue;
  451. }
  452. selected[count++] = entry;
  453. }
  454. if(ciphers == ciphers13 && ciphers12) {
  455. ciphers = ciphers12;
  456. goto add_ciphers;
  457. }
  458. if(!ciphers12) {
  459. /* Add default TLSv1.2 ciphers to selection */
  460. for(j = 0; j < default_len; j++) {
  461. entry = rustls_default_crypto_provider_ciphersuites_get(j);
  462. if(rustls_supported_ciphersuite_protocol_version(entry) ==
  463. RUSTLS_TLS_VERSION_TLSV1_3)
  464. continue;
  465. /* No duplicates allowed (so selected cannot overflow) */
  466. for(i = 0; i < count && selected[i] != entry; i++);
  467. if(i < count)
  468. continue;
  469. selected[count++] = entry;
  470. }
  471. }
  472. *selected_size = count;
  473. }
  474. static CURLcode
  475. cr_init_backend(struct Curl_cfilter *cf, struct Curl_easy *data,
  476. struct rustls_ssl_backend_data *const backend)
  477. {
  478. struct ssl_connect_data *connssl = cf->ctx;
  479. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  480. struct rustls_crypto_provider_builder *custom_provider_builder = NULL;
  481. const struct rustls_crypto_provider *custom_provider = NULL;
  482. struct rustls_connection *rconn = NULL;
  483. struct rustls_client_config_builder *config_builder = NULL;
  484. const struct rustls_root_cert_store *roots = NULL;
  485. struct rustls_root_cert_store_builder *roots_builder = NULL;
  486. struct rustls_web_pki_server_cert_verifier_builder *verifier_builder = NULL;
  487. struct rustls_server_cert_verifier *server_cert_verifier = NULL;
  488. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  489. const char * const ssl_cafile =
  490. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  491. (ca_info_blob ? NULL : conn_config->CAfile);
  492. const bool verifypeer = conn_config->verifypeer;
  493. char errorbuf[256];
  494. size_t errorlen;
  495. rustls_result result;
  496. DEBUGASSERT(backend);
  497. rconn = backend->conn;
  498. {
  499. uint16_t tls_versions[2] = {
  500. RUSTLS_TLS_VERSION_TLSV1_2,
  501. RUSTLS_TLS_VERSION_TLSV1_3,
  502. };
  503. size_t tls_versions_len = 2;
  504. const struct rustls_supported_ciphersuite **cipher_suites;
  505. size_t cipher_suites_len =
  506. rustls_default_crypto_provider_ciphersuites_len();
  507. switch(conn_config->version) {
  508. case CURL_SSLVERSION_DEFAULT:
  509. case CURL_SSLVERSION_TLSv1:
  510. case CURL_SSLVERSION_TLSv1_0:
  511. case CURL_SSLVERSION_TLSv1_1:
  512. case CURL_SSLVERSION_TLSv1_2:
  513. break;
  514. case CURL_SSLVERSION_TLSv1_3:
  515. tls_versions[0] = RUSTLS_TLS_VERSION_TLSV1_3;
  516. tls_versions_len = 1;
  517. break;
  518. default:
  519. failf(data, "rustls: unsupported minimum TLS version value");
  520. return CURLE_BAD_FUNCTION_ARGUMENT;
  521. }
  522. switch(conn_config->version_max) {
  523. case CURL_SSLVERSION_MAX_DEFAULT:
  524. case CURL_SSLVERSION_MAX_NONE:
  525. case CURL_SSLVERSION_MAX_TLSv1_3:
  526. break;
  527. case CURL_SSLVERSION_MAX_TLSv1_2:
  528. if(tls_versions[0] == RUSTLS_TLS_VERSION_TLSV1_2) {
  529. tls_versions_len = 1;
  530. break;
  531. }
  532. FALLTHROUGH();
  533. case CURL_SSLVERSION_MAX_TLSv1_1:
  534. case CURL_SSLVERSION_MAX_TLSv1_0:
  535. default:
  536. failf(data, "rustls: unsupported maximum TLS version value");
  537. return CURLE_BAD_FUNCTION_ARGUMENT;
  538. }
  539. cipher_suites = malloc(sizeof(cipher_suites) * (cipher_suites_len));
  540. if(!cipher_suites)
  541. return CURLE_OUT_OF_MEMORY;
  542. cr_get_selected_ciphers(data,
  543. conn_config->cipher_list,
  544. conn_config->cipher_list13,
  545. cipher_suites, &cipher_suites_len);
  546. if(cipher_suites_len == 0) {
  547. failf(data, "rustls: no supported cipher in list");
  548. free(cipher_suites);
  549. return CURLE_SSL_CIPHER;
  550. }
  551. result = rustls_crypto_provider_builder_new_from_default(
  552. &custom_provider_builder);
  553. if(result != RUSTLS_RESULT_OK) {
  554. failf(data,
  555. "rustls: failed to create crypto provider builder from default");
  556. return CURLE_SSL_CIPHER;
  557. }
  558. result =
  559. rustls_crypto_provider_builder_set_cipher_suites(
  560. custom_provider_builder,
  561. cipher_suites,
  562. cipher_suites_len);
  563. if(result != RUSTLS_RESULT_OK) {
  564. failf(data,
  565. "rustls: failed to set ciphersuites for crypto provider builder");
  566. rustls_crypto_provider_builder_free(custom_provider_builder);
  567. return CURLE_SSL_CIPHER;
  568. }
  569. result = rustls_crypto_provider_builder_build(
  570. custom_provider_builder, &custom_provider);
  571. if(result != RUSTLS_RESULT_OK) {
  572. failf(data, "rustls: failed to build custom crypto provider");
  573. rustls_crypto_provider_builder_free(custom_provider_builder);
  574. return CURLE_SSL_CIPHER;
  575. }
  576. result = rustls_client_config_builder_new_custom(custom_provider,
  577. tls_versions,
  578. tls_versions_len,
  579. &config_builder);
  580. free(cipher_suites);
  581. if(result != RUSTLS_RESULT_OK) {
  582. failf(data, "rustls: failed to create client config");
  583. return CURLE_SSL_CIPHER;
  584. }
  585. }
  586. rustls_crypto_provider_builder_free(custom_provider_builder);
  587. rustls_crypto_provider_free(custom_provider);
  588. if(connssl->alpn) {
  589. struct alpn_proto_buf proto;
  590. rustls_slice_bytes alpn[ALPN_ENTRIES_MAX];
  591. size_t i;
  592. for(i = 0; i < connssl->alpn->count; ++i) {
  593. alpn[i].data = (const uint8_t *)connssl->alpn->entries[i];
  594. alpn[i].len = strlen(connssl->alpn->entries[i]);
  595. }
  596. rustls_client_config_builder_set_alpn_protocols(config_builder, alpn,
  597. connssl->alpn->count);
  598. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  599. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  600. }
  601. if(!verifypeer) {
  602. rustls_client_config_builder_dangerous_set_certificate_verifier(
  603. config_builder, cr_verify_none);
  604. }
  605. else if(ca_info_blob || ssl_cafile) {
  606. roots_builder = rustls_root_cert_store_builder_new();
  607. if(ca_info_blob) {
  608. /* Enable strict parsing only if verification is not disabled. */
  609. result = rustls_root_cert_store_builder_add_pem(roots_builder,
  610. ca_info_blob->data,
  611. ca_info_blob->len,
  612. verifypeer);
  613. if(result != RUSTLS_RESULT_OK) {
  614. failf(data, "rustls: failed to parse trusted certificates from blob");
  615. rustls_root_cert_store_builder_free(roots_builder);
  616. rustls_client_config_builder_free(config_builder);
  617. return CURLE_SSL_CACERT_BADFILE;
  618. }
  619. }
  620. else if(ssl_cafile) {
  621. /* Enable strict parsing only if verification is not disabled. */
  622. result = rustls_root_cert_store_builder_load_roots_from_file(
  623. roots_builder, ssl_cafile, verifypeer);
  624. if(result != RUSTLS_RESULT_OK) {
  625. failf(data, "rustls: failed to load trusted certificates");
  626. rustls_root_cert_store_builder_free(roots_builder);
  627. rustls_client_config_builder_free(config_builder);
  628. return CURLE_SSL_CACERT_BADFILE;
  629. }
  630. }
  631. result = rustls_root_cert_store_builder_build(roots_builder, &roots);
  632. rustls_root_cert_store_builder_free(roots_builder);
  633. if(result != RUSTLS_RESULT_OK) {
  634. failf(data, "rustls: failed to build trusted root certificate store");
  635. rustls_client_config_builder_free(config_builder);
  636. return CURLE_SSL_CACERT_BADFILE;
  637. }
  638. verifier_builder = rustls_web_pki_server_cert_verifier_builder_new(roots);
  639. rustls_root_cert_store_free(roots);
  640. if(conn_config->CRLfile) {
  641. struct dynbuf crl_contents;
  642. Curl_dyn_init(&crl_contents, SIZE_MAX);
  643. if(!read_file_into(conn_config->CRLfile, &crl_contents)) {
  644. failf(data, "rustls: failed to read revocation list file");
  645. Curl_dyn_free(&crl_contents);
  646. rustls_web_pki_server_cert_verifier_builder_free(verifier_builder);
  647. return CURLE_SSL_CRL_BADFILE;
  648. }
  649. result = rustls_web_pki_server_cert_verifier_builder_add_crl(
  650. verifier_builder,
  651. Curl_dyn_uptr(&crl_contents),
  652. Curl_dyn_len(&crl_contents));
  653. Curl_dyn_free(&crl_contents);
  654. if(result != RUSTLS_RESULT_OK) {
  655. failf(data, "rustls: failed to parse revocation list");
  656. rustls_web_pki_server_cert_verifier_builder_free(verifier_builder);
  657. return CURLE_SSL_CRL_BADFILE;
  658. }
  659. }
  660. result = rustls_web_pki_server_cert_verifier_builder_build(
  661. verifier_builder, &server_cert_verifier);
  662. rustls_web_pki_server_cert_verifier_builder_free(verifier_builder);
  663. if(result != RUSTLS_RESULT_OK) {
  664. failf(data, "rustls: failed to build certificate verifier");
  665. rustls_server_cert_verifier_free(server_cert_verifier);
  666. rustls_client_config_builder_free(config_builder);
  667. return CURLE_SSL_CACERT_BADFILE;
  668. }
  669. rustls_client_config_builder_set_server_verifier(config_builder,
  670. server_cert_verifier);
  671. rustls_server_cert_verifier_free(server_cert_verifier);
  672. }
  673. result = rustls_client_config_builder_build(
  674. config_builder,
  675. &backend->config);
  676. if(result != RUSTLS_RESULT_OK) {
  677. failf(data, "rustls: failed to build client config");
  678. rustls_client_config_free(backend->config);
  679. return CURLE_SSL_CONNECT_ERROR;
  680. }
  681. DEBUGASSERT(rconn == NULL);
  682. result = rustls_client_connection_new(backend->config,
  683. connssl->peer.hostname, &rconn);
  684. if(result != RUSTLS_RESULT_OK) {
  685. rustls_error(result, errorbuf, sizeof(errorbuf), &errorlen);
  686. failf(data, "rustls_client_connection_new: %.*s", (int)errorlen, errorbuf);
  687. return CURLE_COULDNT_CONNECT;
  688. }
  689. DEBUGASSERT(rconn);
  690. rustls_connection_set_userdata(rconn, backend);
  691. backend->conn = rconn;
  692. return CURLE_OK;
  693. }
  694. static void
  695. cr_set_negotiated_alpn(struct Curl_cfilter *cf, struct Curl_easy *data,
  696. const struct rustls_connection *rconn)
  697. {
  698. struct ssl_connect_data *const connssl = cf->ctx;
  699. const uint8_t *protocol = NULL;
  700. size_t len = 0;
  701. rustls_connection_get_alpn_protocol(rconn, &protocol, &len);
  702. Curl_alpn_set_negotiated(cf, data, connssl, protocol, len);
  703. }
  704. /* Given an established network connection, do a TLS handshake.
  705. *
  706. * If `blocking` is true, this function will block until the handshake is
  707. * complete. Otherwise it will return as soon as I/O would block.
  708. *
  709. * For the non-blocking I/O case, this function will set `*done` to true
  710. * once the handshake is complete. This function never reads the value of
  711. * `*done*`.
  712. */
  713. static CURLcode
  714. cr_connect_common(struct Curl_cfilter *cf,
  715. struct Curl_easy *data,
  716. bool blocking,
  717. bool *done)
  718. {
  719. struct ssl_connect_data *const connssl = cf->ctx;
  720. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  721. struct rustls_ssl_backend_data *const backend =
  722. (struct rustls_ssl_backend_data *)connssl->backend;
  723. struct rustls_connection *rconn = NULL;
  724. CURLcode tmperr = CURLE_OK;
  725. int result;
  726. int what;
  727. bool wants_read;
  728. bool wants_write;
  729. curl_socket_t writefd;
  730. curl_socket_t readfd;
  731. timediff_t timeout_ms;
  732. timediff_t socket_check_timeout;
  733. DEBUGASSERT(backend);
  734. CURL_TRC_CF(data, cf, "cr_connect_common, state=%d", connssl->state);
  735. *done = FALSE;
  736. if(!backend->conn) {
  737. result = cr_init_backend(cf, data,
  738. (struct rustls_ssl_backend_data *)connssl->backend);
  739. CURL_TRC_CF(data, cf, "cr_connect_common, init backend -> %d", result);
  740. if(result != CURLE_OK) {
  741. return result;
  742. }
  743. connssl->state = ssl_connection_negotiating;
  744. }
  745. rconn = backend->conn;
  746. /* Read/write data until the handshake is done or the socket would block. */
  747. for(;;) {
  748. /*
  749. * Connection has been established according to Rustls. Set send/recv
  750. * handlers, and update the state machine.
  751. */
  752. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  753. if(!rustls_connection_is_handshaking(rconn)) {
  754. /* Rustls claims it is no longer handshaking *before* it has
  755. * send its FINISHED message off. We attempt to let it write
  756. * one more time. Oh my.
  757. */
  758. cr_set_negotiated_alpn(cf, data, rconn);
  759. cr_send(cf, data, NULL, 0, &tmperr);
  760. if(tmperr == CURLE_AGAIN) {
  761. connssl->io_need = CURL_SSL_IO_NEED_SEND;
  762. return CURLE_OK;
  763. }
  764. else if(tmperr != CURLE_OK) {
  765. return tmperr;
  766. }
  767. /* REALLY Done with the handshake. */
  768. {
  769. uint16_t proto = rustls_connection_get_protocol_version(rconn);
  770. uint16_t cipher = rustls_connection_get_negotiated_ciphersuite(rconn);
  771. char buf[64] = "";
  772. const char *ver = "TLS version unknown";
  773. if(proto == RUSTLS_TLS_VERSION_TLSV1_3)
  774. ver = "TLSv1.3";
  775. if(proto == RUSTLS_TLS_VERSION_TLSV1_2)
  776. ver = "TLSv1.2";
  777. Curl_cipher_suite_get_str(cipher, buf, sizeof(buf), TRUE);
  778. infof(data, "rustls: handshake complete, %s, cipher: %s",
  779. ver, buf);
  780. }
  781. connssl->state = ssl_connection_complete;
  782. *done = TRUE;
  783. return CURLE_OK;
  784. }
  785. connssl->connecting_state = ssl_connect_2;
  786. wants_read = rustls_connection_wants_read(rconn);
  787. wants_write = rustls_connection_wants_write(rconn) ||
  788. backend->plain_out_buffered;
  789. DEBUGASSERT(wants_read || wants_write);
  790. writefd = wants_write ? sockfd : CURL_SOCKET_BAD;
  791. readfd = wants_read ? sockfd : CURL_SOCKET_BAD;
  792. /* check allowed time left */
  793. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  794. if(timeout_ms < 0) {
  795. /* no need to continue if time already is up */
  796. failf(data, "rustls: operation timed out before socket check");
  797. return CURLE_OPERATION_TIMEDOUT;
  798. }
  799. socket_check_timeout = blocking ? timeout_ms : 0;
  800. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  801. socket_check_timeout);
  802. if(what < 0) {
  803. /* fatal error */
  804. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  805. return CURLE_SSL_CONNECT_ERROR;
  806. }
  807. if(blocking && 0 == what) {
  808. failf(data, "rustls: connection timeout after %" FMT_TIMEDIFF_T " ms",
  809. socket_check_timeout);
  810. return CURLE_OPERATION_TIMEDOUT;
  811. }
  812. if(0 == what) {
  813. CURL_TRC_CF(data, cf, "Curl_socket_check: %s would block",
  814. wants_read && wants_write ? "writing and reading" :
  815. wants_write ? "writing" : "reading");
  816. if(wants_write)
  817. connssl->io_need |= CURL_SSL_IO_NEED_SEND;
  818. if(wants_read)
  819. connssl->io_need |= CURL_SSL_IO_NEED_RECV;
  820. return CURLE_OK;
  821. }
  822. /* socket is readable or writable */
  823. if(wants_write) {
  824. CURL_TRC_CF(data, cf, "rustls_connection wants us to write_tls.");
  825. cr_send(cf, data, NULL, 0, &tmperr);
  826. if(tmperr == CURLE_AGAIN) {
  827. CURL_TRC_CF(data, cf, "writing would block");
  828. /* fall through */
  829. }
  830. else if(tmperr != CURLE_OK) {
  831. return tmperr;
  832. }
  833. }
  834. if(wants_read) {
  835. CURL_TRC_CF(data, cf, "rustls_connection wants us to read_tls.");
  836. if(tls_recv_more(cf, data, &tmperr) < 0) {
  837. if(tmperr == CURLE_AGAIN) {
  838. CURL_TRC_CF(data, cf, "reading would block");
  839. /* fall through */
  840. }
  841. else if(tmperr == CURLE_RECV_ERROR) {
  842. return CURLE_SSL_CONNECT_ERROR;
  843. }
  844. else {
  845. return tmperr;
  846. }
  847. }
  848. }
  849. }
  850. /* We should never fall through the loop. We should return either because
  851. the handshake is done or because we cannot read/write without blocking. */
  852. DEBUGASSERT(FALSE);
  853. }
  854. static CURLcode
  855. cr_connect_nonblocking(struct Curl_cfilter *cf,
  856. struct Curl_easy *data, bool *done)
  857. {
  858. return cr_connect_common(cf, data, false, done);
  859. }
  860. static CURLcode
  861. cr_connect_blocking(struct Curl_cfilter *cf, struct Curl_easy *data)
  862. {
  863. bool done; /* unused */
  864. return cr_connect_common(cf, data, true, &done);
  865. }
  866. static void *
  867. cr_get_internals(struct ssl_connect_data *connssl,
  868. CURLINFO info UNUSED_PARAM)
  869. {
  870. struct rustls_ssl_backend_data *backend =
  871. (struct rustls_ssl_backend_data *)connssl->backend;
  872. DEBUGASSERT(backend);
  873. return &backend->conn;
  874. }
  875. static CURLcode
  876. cr_shutdown(struct Curl_cfilter *cf,
  877. struct Curl_easy *data,
  878. bool send_shutdown, bool *done)
  879. {
  880. struct ssl_connect_data *connssl = cf->ctx;
  881. struct rustls_ssl_backend_data *backend =
  882. (struct rustls_ssl_backend_data *)connssl->backend;
  883. CURLcode result = CURLE_OK;
  884. ssize_t nwritten, nread;
  885. char buf[1024];
  886. size_t i;
  887. DEBUGASSERT(backend);
  888. if(!backend->conn || cf->shutdown) {
  889. *done = TRUE;
  890. goto out;
  891. }
  892. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  893. *done = FALSE;
  894. if(!backend->sent_shutdown) {
  895. /* do this only once */
  896. backend->sent_shutdown = TRUE;
  897. if(send_shutdown) {
  898. rustls_connection_send_close_notify(backend->conn);
  899. }
  900. }
  901. nwritten = cr_send(cf, data, NULL, 0, &result);
  902. if(nwritten < 0) {
  903. if(result == CURLE_AGAIN) {
  904. connssl->io_need = CURL_SSL_IO_NEED_SEND;
  905. result = CURLE_OK;
  906. goto out;
  907. }
  908. DEBUGASSERT(result);
  909. CURL_TRC_CF(data, cf, "shutdown send failed: %d", result);
  910. goto out;
  911. }
  912. for(i = 0; i < 10; ++i) {
  913. nread = cr_recv(cf, data, buf, (int)sizeof(buf), &result);
  914. if(nread <= 0)
  915. break;
  916. }
  917. if(nread > 0) {
  918. /* still data coming in? */
  919. }
  920. else if(nread == 0) {
  921. /* We got the close notify alert and are done. */
  922. *done = TRUE;
  923. }
  924. else if(result == CURLE_AGAIN) {
  925. connssl->io_need = CURL_SSL_IO_NEED_RECV;
  926. result = CURLE_OK;
  927. }
  928. else {
  929. DEBUGASSERT(result);
  930. CURL_TRC_CF(data, cf, "shutdown, error: %d", result);
  931. }
  932. out:
  933. cf->shutdown = (result || *done);
  934. return result;
  935. }
  936. static void
  937. cr_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  938. {
  939. struct ssl_connect_data *connssl = cf->ctx;
  940. struct rustls_ssl_backend_data *backend =
  941. (struct rustls_ssl_backend_data *)connssl->backend;
  942. (void)data;
  943. DEBUGASSERT(backend);
  944. if(backend->conn) {
  945. rustls_connection_free(backend->conn);
  946. backend->conn = NULL;
  947. }
  948. if(backend->config) {
  949. rustls_client_config_free(backend->config);
  950. backend->config = NULL;
  951. }
  952. }
  953. static size_t cr_version(char *buffer, size_t size)
  954. {
  955. struct rustls_str ver = rustls_version();
  956. return msnprintf(buffer, size, "%.*s", (int)ver.len, ver.data);
  957. }
  958. static CURLcode
  959. cr_random(struct Curl_easy *data, unsigned char *entropy, size_t length)
  960. {
  961. rustls_result rresult = 0;
  962. (void)data;
  963. rresult =
  964. rustls_default_crypto_provider_random(entropy, length);
  965. return map_error(rresult);
  966. }
  967. const struct Curl_ssl Curl_ssl_rustls = {
  968. { CURLSSLBACKEND_RUSTLS, "rustls" },
  969. SSLSUPP_CAINFO_BLOB | /* supports */
  970. SSLSUPP_HTTPS_PROXY |
  971. SSLSUPP_CIPHER_LIST |
  972. SSLSUPP_TLS13_CIPHERSUITES,
  973. sizeof(struct rustls_ssl_backend_data),
  974. Curl_none_init, /* init */
  975. Curl_none_cleanup, /* cleanup */
  976. cr_version, /* version */
  977. Curl_none_check_cxn, /* check_cxn */
  978. cr_shutdown, /* shutdown */
  979. cr_data_pending, /* data_pending */
  980. cr_random, /* random */
  981. Curl_none_cert_status_request, /* cert_status_request */
  982. cr_connect_blocking, /* connect */
  983. cr_connect_nonblocking, /* connect_nonblocking */
  984. Curl_ssl_adjust_pollset, /* adjust_pollset */
  985. cr_get_internals, /* get_internals */
  986. cr_close, /* close_one */
  987. Curl_none_close_all, /* close_all */
  988. Curl_none_set_engine, /* set_engine */
  989. Curl_none_set_engine_default, /* set_engine_default */
  990. Curl_none_engines_list, /* engines_list */
  991. Curl_none_false_start, /* false_start */
  992. NULL, /* sha256sum */
  993. NULL, /* associate_connection */
  994. NULL, /* disassociate_connection */
  995. cr_recv, /* recv decrypted data */
  996. cr_send, /* send data to encrypt */
  997. NULL, /* get_channel_binding */
  998. };
  999. #endif /* USE_RUSTLS */