2
0

rustls.c 34 KB

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