mbedtls.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) Hoi-Ho Chan, <hoiho.chan@gmail.com>
  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. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. /*
  26. * Source file for all mbedTLS-specific code for the TLS/SSL layer. No code
  27. * but vtls.c should ever call or use these functions.
  28. *
  29. */
  30. #include "curl_setup.h"
  31. #ifdef USE_MBEDTLS
  32. /* Define this to enable lots of debugging for mbedTLS */
  33. /* #define MBEDTLS_DEBUG */
  34. #include <mbedtls/version.h>
  35. #if MBEDTLS_VERSION_NUMBER >= 0x02040000
  36. #include <mbedtls/net_sockets.h>
  37. #else
  38. #include <mbedtls/net.h>
  39. #endif
  40. #include <mbedtls/ssl.h>
  41. #include <mbedtls/x509.h>
  42. #include <mbedtls/error.h>
  43. #include <mbedtls/entropy.h>
  44. #include <mbedtls/ctr_drbg.h>
  45. #include <mbedtls/sha256.h>
  46. #if MBEDTLS_VERSION_MAJOR >= 2
  47. # ifdef MBEDTLS_DEBUG
  48. # include <mbedtls/debug.h>
  49. # endif
  50. #endif /* MBEDTLS_VERSION_MAJOR >= 2 */
  51. #include "cipher_suite.h"
  52. #include "strcase.h"
  53. #include "urldata.h"
  54. #include "sendf.h"
  55. #include "inet_pton.h"
  56. #include "mbedtls.h"
  57. #include "vtls.h"
  58. #include "vtls_int.h"
  59. #include "x509asn1.h"
  60. #include "parsedate.h"
  61. #include "connect.h" /* for the connect timeout */
  62. #include "select.h"
  63. #include "multiif.h"
  64. #include "mbedtls_threadlock.h"
  65. #include "strdup.h"
  66. /* The last 3 #include files should be in this order */
  67. #include "curl_printf.h"
  68. #include "curl_memory.h"
  69. #include "memdebug.h"
  70. /* ALPN for http2 */
  71. #ifdef USE_HTTP2
  72. # undef HAS_ALPN
  73. # ifdef MBEDTLS_SSL_ALPN
  74. # define HAS_ALPN
  75. # endif
  76. #endif
  77. struct mbed_ssl_backend_data {
  78. mbedtls_ctr_drbg_context ctr_drbg;
  79. mbedtls_entropy_context entropy;
  80. mbedtls_ssl_context ssl;
  81. mbedtls_x509_crt cacert;
  82. mbedtls_x509_crt clicert;
  83. #ifdef MBEDTLS_X509_CRL_PARSE_C
  84. mbedtls_x509_crl crl;
  85. #endif
  86. mbedtls_pk_context pk;
  87. mbedtls_ssl_config config;
  88. #ifdef HAS_ALPN
  89. const char *protocols[3];
  90. #endif
  91. int *ciphersuites;
  92. BIT(initialized); /* mbedtls_ssl_context is initialized */
  93. BIT(sent_shutdown);
  94. };
  95. /* apply threading? */
  96. #if (defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)) || \
  97. defined(_WIN32)
  98. #define THREADING_SUPPORT
  99. #endif
  100. #ifndef MBEDTLS_ERROR_C
  101. #define mbedtls_strerror(a,b,c) b[0] = 0
  102. #endif
  103. #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && MBEDTLS_VERSION_NUMBER >= 0x03060000
  104. #define TLS13_SUPPORT
  105. #endif
  106. #if defined(TLS13_SUPPORT) && defined(MBEDTLS_SSL_SESSION_TICKETS)
  107. #define HAS_SESSION_TICKETS
  108. #endif
  109. #ifdef THREADING_SUPPORT
  110. static mbedtls_entropy_context ts_entropy;
  111. static int entropy_init_initialized = 0;
  112. static void entropy_init_mutex(mbedtls_entropy_context *ctx)
  113. {
  114. /* lock 0 = entropy_init_mutex() */
  115. Curl_mbedtlsthreadlock_lock_function(0);
  116. if(entropy_init_initialized == 0) {
  117. mbedtls_entropy_init(ctx);
  118. entropy_init_initialized = 1;
  119. }
  120. Curl_mbedtlsthreadlock_unlock_function(0);
  121. }
  122. static void entropy_cleanup_mutex(mbedtls_entropy_context *ctx)
  123. {
  124. /* lock 0 = use same lock as init */
  125. Curl_mbedtlsthreadlock_lock_function(0);
  126. if(entropy_init_initialized == 1) {
  127. mbedtls_entropy_free(ctx);
  128. entropy_init_initialized = 0;
  129. }
  130. Curl_mbedtlsthreadlock_unlock_function(0);
  131. }
  132. static int entropy_func_mutex(void *data, unsigned char *output, size_t len)
  133. {
  134. int ret;
  135. /* lock 1 = entropy_func_mutex() */
  136. Curl_mbedtlsthreadlock_lock_function(1);
  137. ret = mbedtls_entropy_func(data, output, len);
  138. Curl_mbedtlsthreadlock_unlock_function(1);
  139. return ret;
  140. }
  141. #endif /* THREADING_SUPPORT */
  142. #ifdef MBEDTLS_DEBUG
  143. static void mbed_debug(void *context, int level, const char *f_name,
  144. int line_nb, const char *line)
  145. {
  146. struct Curl_easy *data = (struct Curl_easy *)context;
  147. (void) level;
  148. (void) line_nb;
  149. (void) f_name;
  150. if(data) {
  151. size_t len = strlen(line);
  152. if(len && (line[len - 1] == '\n'))
  153. /* discount any trailing newline */
  154. len--;
  155. infof(data, "%.*s", (int)len, line);
  156. }
  157. }
  158. #endif
  159. static int mbedtls_bio_cf_write(void *bio,
  160. const unsigned char *buf, size_t blen)
  161. {
  162. struct Curl_cfilter *cf = bio;
  163. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  164. ssize_t nwritten;
  165. CURLcode result;
  166. DEBUGASSERT(data);
  167. if(!data)
  168. return 0;
  169. nwritten = Curl_conn_cf_send(cf->next, data, (char *)buf, blen, FALSE,
  170. &result);
  171. CURL_TRC_CF(data, cf, "mbedtls_bio_cf_out_write(len=%zu) -> %zd, err=%d",
  172. blen, nwritten, result);
  173. if(nwritten < 0 && CURLE_AGAIN == result) {
  174. nwritten = MBEDTLS_ERR_SSL_WANT_WRITE;
  175. }
  176. return (int)nwritten;
  177. }
  178. static int mbedtls_bio_cf_read(void *bio, unsigned char *buf, size_t blen)
  179. {
  180. struct Curl_cfilter *cf = bio;
  181. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  182. ssize_t nread;
  183. CURLcode result;
  184. DEBUGASSERT(data);
  185. if(!data)
  186. return 0;
  187. /* OpenSSL catches this case, so should we. */
  188. if(!buf)
  189. return 0;
  190. nread = Curl_conn_cf_recv(cf->next, data, (char *)buf, blen, &result);
  191. CURL_TRC_CF(data, cf, "mbedtls_bio_cf_in_read(len=%zu) -> %zd, err=%d",
  192. blen, nread, result);
  193. if(nread < 0 && CURLE_AGAIN == result) {
  194. nread = MBEDTLS_ERR_SSL_WANT_READ;
  195. }
  196. return (int)nread;
  197. }
  198. /*
  199. * profile
  200. */
  201. static const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_fr =
  202. {
  203. /* Hashes from SHA-1 and above */
  204. MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
  205. MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_RIPEMD160) |
  206. MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
  207. MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
  208. MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
  209. MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
  210. 0xFFFFFFF, /* Any PK alg */
  211. 0xFFFFFFF, /* Any curve */
  212. 1024, /* RSA min key len */
  213. };
  214. /* See https://web.archive.org/web/20200921194007/tls.mbed.org/discussions/
  215. generic/howto-determine-exact-buffer-len-for-mbedtls_pk_write_pubkey_der
  216. */
  217. #define RSA_PUB_DER_MAX_BYTES (38 + 2 * MBEDTLS_MPI_MAX_SIZE)
  218. #define ECP_PUB_DER_MAX_BYTES (30 + 2 * MBEDTLS_ECP_MAX_BYTES)
  219. #define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
  220. RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES)
  221. static CURLcode
  222. mbed_set_ssl_version_min_max(struct Curl_easy *data,
  223. struct mbed_ssl_backend_data *backend,
  224. struct ssl_primary_config *conn_config)
  225. {
  226. /* TLS 1.0 and TLS 1.1 were dropped with mbedTLS 3.0.0 (2021). So, since
  227. * then, and before the introduction of TLS 1.3 in 3.6.0 (2024), this
  228. * function basically always sets TLS 1.2 as min/max, unless given
  229. * unsupported option values. */
  230. #if MBEDTLS_VERSION_NUMBER < 0x03020000
  231. int ver_min = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
  232. int ver_max = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
  233. #else
  234. /* mbedTLS 3.2.0 (2022) introduced new methods for setting TLS version */
  235. mbedtls_ssl_protocol_version ver_min = MBEDTLS_SSL_VERSION_TLS1_2;
  236. mbedtls_ssl_protocol_version ver_max = MBEDTLS_SSL_VERSION_TLS1_2;
  237. #endif
  238. switch(conn_config->version) {
  239. case CURL_SSLVERSION_DEFAULT:
  240. #if MBEDTLS_VERSION_NUMBER < 0x03000000
  241. case CURL_SSLVERSION_TLSv1:
  242. case CURL_SSLVERSION_TLSv1_0:
  243. ver_min = MBEDTLS_SSL_MINOR_VERSION_1;
  244. break;
  245. case CURL_SSLVERSION_TLSv1_1:
  246. ver_min = MBEDTLS_SSL_MINOR_VERSION_2;
  247. break;
  248. #else
  249. case CURL_SSLVERSION_TLSv1:
  250. case CURL_SSLVERSION_TLSv1_0:
  251. case CURL_SSLVERSION_TLSv1_1:
  252. #endif
  253. case CURL_SSLVERSION_TLSv1_2:
  254. /* ver_min = MBEDTLS_SSL_VERSION_TLS1_2; */
  255. break;
  256. case CURL_SSLVERSION_TLSv1_3:
  257. #ifdef TLS13_SUPPORT
  258. ver_min = MBEDTLS_SSL_VERSION_TLS1_3;
  259. break;
  260. #endif
  261. default:
  262. failf(data, "mbedTLS: unsupported minimum TLS version value: %x",
  263. conn_config->version);
  264. return CURLE_SSL_CONNECT_ERROR;
  265. }
  266. switch(conn_config->version_max) {
  267. case CURL_SSLVERSION_MAX_DEFAULT:
  268. case CURL_SSLVERSION_MAX_NONE:
  269. case CURL_SSLVERSION_MAX_TLSv1_3:
  270. #ifdef TLS13_SUPPORT
  271. ver_max = MBEDTLS_SSL_VERSION_TLS1_3;
  272. break;
  273. #endif
  274. case CURL_SSLVERSION_MAX_TLSv1_2:
  275. /* ver_max = MBEDTLS_SSL_VERSION_TLS1_2; */
  276. break;
  277. #if MBEDTLS_VERSION_NUMBER < 0x03000000
  278. case CURL_SSLVERSION_MAX_TLSv1_1:
  279. ver_max = MBEDTLS_SSL_MINOR_VERSION_2;
  280. break;
  281. case CURL_SSLVERSION_MAX_TLSv1_0:
  282. ver_max = MBEDTLS_SSL_MINOR_VERSION_1;
  283. break;
  284. #else
  285. case CURL_SSLVERSION_MAX_TLSv1_1:
  286. case CURL_SSLVERSION_MAX_TLSv1_0:
  287. #endif
  288. default:
  289. failf(data, "mbedTLS: unsupported maximum TLS version value");
  290. return CURLE_SSL_CONNECT_ERROR;
  291. }
  292. #if MBEDTLS_VERSION_NUMBER < 0x03020000
  293. mbedtls_ssl_conf_min_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
  294. ver_min);
  295. mbedtls_ssl_conf_max_version(&backend->config, MBEDTLS_SSL_MAJOR_VERSION_3,
  296. ver_max);
  297. #else
  298. mbedtls_ssl_conf_min_tls_version(&backend->config, ver_min);
  299. mbedtls_ssl_conf_max_tls_version(&backend->config, ver_max);
  300. #endif
  301. return CURLE_OK;
  302. }
  303. /* TLS_ECJPAKE_WITH_AES_128_CCM_8 (0xC0FF) is marked experimental
  304. in mbedTLS. The number is not reserved by IANA nor is the
  305. cipher suite present in other SSL implementations. Provide
  306. provisional support for specifying the cipher suite here. */
  307. #ifdef MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8
  308. #if MBEDTLS_VERSION_NUMBER >= 0x03020000
  309. static int
  310. mbed_cipher_suite_get_str(uint16_t id, char *buf, size_t buf_size,
  311. bool prefer_rfc)
  312. {
  313. if(id == MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8)
  314. msnprintf(buf, buf_size, "%s", "TLS_ECJPAKE_WITH_AES_128_CCM_8");
  315. else
  316. return Curl_cipher_suite_get_str(id, buf, buf_size, prefer_rfc);
  317. return 0;
  318. }
  319. #endif
  320. static uint16_t
  321. mbed_cipher_suite_walk_str(const char **str, const char **end)
  322. {
  323. uint16_t id = Curl_cipher_suite_walk_str(str, end);
  324. size_t len = *end - *str;
  325. if(!id) {
  326. if(strncasecompare("TLS_ECJPAKE_WITH_AES_128_CCM_8", *str, len))
  327. id = MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8;
  328. }
  329. return id;
  330. }
  331. #else
  332. #define mbed_cipher_suite_get_str Curl_cipher_suite_get_str
  333. #define mbed_cipher_suite_walk_str Curl_cipher_suite_walk_str
  334. #endif
  335. static CURLcode
  336. mbed_set_selected_ciphers(struct Curl_easy *data,
  337. struct mbed_ssl_backend_data *backend,
  338. const char *ciphers12,
  339. const char *ciphers13)
  340. {
  341. const char *ciphers = ciphers12;
  342. const int *supported;
  343. int *selected;
  344. size_t supported_len, count = 0, default13_count = 0, i, j;
  345. const char *ptr, *end;
  346. supported = mbedtls_ssl_list_ciphersuites();
  347. for(i = 0; supported[i] != 0; i++);
  348. supported_len = i;
  349. selected = malloc(sizeof(int) * (supported_len + 1));
  350. if(!selected)
  351. return CURLE_OUT_OF_MEMORY;
  352. #ifndef TLS13_SUPPORT
  353. (void) ciphers13, (void) j;
  354. #else
  355. if(!ciphers13) {
  356. /* Add default TLSv1.3 ciphers to selection */
  357. for(j = 0; j < supported_len; j++) {
  358. uint16_t id = (uint16_t) supported[j];
  359. if(strncmp(mbedtls_ssl_get_ciphersuite_name(id), "TLS1-3", 6) != 0)
  360. continue;
  361. selected[count++] = id;
  362. }
  363. default13_count = count;
  364. }
  365. else
  366. ciphers = ciphers13;
  367. add_ciphers:
  368. #endif
  369. for(ptr = ciphers; ptr[0] != '\0' && count < supported_len; ptr = end) {
  370. uint16_t id = mbed_cipher_suite_walk_str(&ptr, &end);
  371. /* Check if cipher is supported */
  372. if(id) {
  373. for(i = 0; i < supported_len && supported[i] != id; i++);
  374. if(i == supported_len)
  375. id = 0;
  376. }
  377. if(!id) {
  378. if(ptr[0] != '\0')
  379. infof(data, "mbedTLS: unknown cipher in list: \"%.*s\"",
  380. (int) (end - ptr), ptr);
  381. continue;
  382. }
  383. /* No duplicates allowed (so selected cannot overflow) */
  384. for(i = 0; i < count && selected[i] != id; i++);
  385. if(i < count) {
  386. if(i >= default13_count)
  387. infof(data, "mbedTLS: duplicate cipher in list: \"%.*s\"",
  388. (int) (end - ptr), ptr);
  389. continue;
  390. }
  391. selected[count++] = id;
  392. }
  393. #ifdef TLS13_SUPPORT
  394. if(ciphers == ciphers13 && ciphers12) {
  395. ciphers = ciphers12;
  396. goto add_ciphers;
  397. }
  398. if(!ciphers12) {
  399. /* Add default TLSv1.2 ciphers to selection */
  400. for(j = 0; j < supported_len; j++) {
  401. uint16_t id = (uint16_t) supported[j];
  402. if(strncmp(mbedtls_ssl_get_ciphersuite_name(id), "TLS1-3", 6) == 0)
  403. continue;
  404. /* No duplicates allowed (so selected cannot overflow) */
  405. for(i = 0; i < count && selected[i] != id; i++);
  406. if(i < count)
  407. continue;
  408. selected[count++] = id;
  409. }
  410. }
  411. #endif
  412. selected[count] = 0;
  413. if(count == 0) {
  414. free(selected);
  415. failf(data, "mbedTLS: no supported cipher in list");
  416. return CURLE_SSL_CIPHER;
  417. }
  418. /* mbedtls_ssl_conf_ciphersuites(): The ciphersuites array is not copied.
  419. It must remain valid for the lifetime of the SSL configuration */
  420. backend->ciphersuites = selected;
  421. mbedtls_ssl_conf_ciphersuites(&backend->config, backend->ciphersuites);
  422. return CURLE_OK;
  423. }
  424. static void
  425. mbed_dump_cert_info(struct Curl_easy *data, const mbedtls_x509_crt *crt)
  426. {
  427. #if defined(CURL_DISABLE_VERBOSE_STRINGS) || \
  428. (MBEDTLS_VERSION_NUMBER >= 0x03000000 && defined(MBEDTLS_X509_REMOVE_INFO))
  429. (void) data, (void) crt;
  430. #else
  431. const size_t bufsize = 16384;
  432. char *p, *buffer = malloc(bufsize);
  433. if(buffer && mbedtls_x509_crt_info(buffer, bufsize, " ", crt) > 0) {
  434. infof(data, "Server certificate:");
  435. for(p = buffer; *p; p += *p != '\0') {
  436. size_t s = strcspn(p, "\n");
  437. infof(data, "%.*s", (int) s, p);
  438. p += s;
  439. }
  440. }
  441. else
  442. infof(data, "Unable to dump certificate information");
  443. free(buffer);
  444. #endif
  445. }
  446. static void
  447. mbed_extract_certinfo(struct Curl_easy *data, const mbedtls_x509_crt *crt)
  448. {
  449. CURLcode result;
  450. const mbedtls_x509_crt *cur;
  451. int i;
  452. for(i = 0, cur = crt; cur; ++i, cur = cur->next);
  453. result = Curl_ssl_init_certinfo(data, i);
  454. for(i = 0, cur = crt; result == CURLE_OK && cur; ++i, cur = cur->next) {
  455. const char *beg = (const char *) cur->raw.p;
  456. const char *end = beg + cur->raw.len;
  457. result = Curl_extract_certinfo(data, i, beg, end);
  458. }
  459. }
  460. static int mbed_verify_cb(void *ptr, mbedtls_x509_crt *crt,
  461. int depth, uint32_t *flags)
  462. {
  463. struct Curl_cfilter *cf = (struct Curl_cfilter *) ptr;
  464. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  465. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  466. if(depth == 0) {
  467. if(data->set.verbose)
  468. mbed_dump_cert_info(data, crt);
  469. if(data->set.ssl.certinfo)
  470. mbed_extract_certinfo(data, crt);
  471. }
  472. if(!conn_config->verifypeer)
  473. *flags = 0;
  474. else if(!conn_config->verifyhost)
  475. *flags &= ~MBEDTLS_X509_BADCERT_CN_MISMATCH;
  476. if(*flags) {
  477. #if MBEDTLS_VERSION_NUMBER < 0x03000000 || !defined(MBEDTLS_X509_REMOVE_INFO)
  478. char buf[128];
  479. mbedtls_x509_crt_verify_info(buf, sizeof(buf), "", *flags);
  480. failf(data, "mbedTLS: %s", buf);
  481. #else
  482. failf(data, "mbedTLS: certificate verification error 0x%08x", *flags);
  483. #endif
  484. }
  485. return 0;
  486. }
  487. static CURLcode
  488. mbed_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
  489. {
  490. struct ssl_connect_data *connssl = cf->ctx;
  491. struct mbed_ssl_backend_data *backend =
  492. (struct mbed_ssl_backend_data *)connssl->backend;
  493. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  494. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  495. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  496. const char * const ssl_cafile =
  497. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  498. (ca_info_blob ? NULL : conn_config->CAfile);
  499. const bool verifypeer = conn_config->verifypeer;
  500. const char * const ssl_capath = conn_config->CApath;
  501. char * const ssl_cert = ssl_config->primary.clientcert;
  502. const struct curl_blob *ssl_cert_blob = ssl_config->primary.cert_blob;
  503. const char * const ssl_crlfile = ssl_config->primary.CRLfile;
  504. const char *hostname = connssl->peer.hostname;
  505. int ret = -1;
  506. char errorbuf[128];
  507. DEBUGASSERT(backend);
  508. DEBUGASSERT(!backend->initialized);
  509. if((conn_config->version == CURL_SSLVERSION_SSLv2) ||
  510. (conn_config->version == CURL_SSLVERSION_SSLv3)) {
  511. failf(data, "Not supported SSL version");
  512. return CURLE_NOT_BUILT_IN;
  513. }
  514. #ifdef THREADING_SUPPORT
  515. mbedtls_ctr_drbg_init(&backend->ctr_drbg);
  516. ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, entropy_func_mutex,
  517. &ts_entropy, NULL, 0);
  518. if(ret) {
  519. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  520. failf(data, "mbedtls_ctr_drbg_seed returned (-0x%04X) %s",
  521. -ret, errorbuf);
  522. return CURLE_FAILED_INIT;
  523. }
  524. #else
  525. mbedtls_entropy_init(&backend->entropy);
  526. mbedtls_ctr_drbg_init(&backend->ctr_drbg);
  527. ret = mbedtls_ctr_drbg_seed(&backend->ctr_drbg, mbedtls_entropy_func,
  528. &backend->entropy, NULL, 0);
  529. if(ret) {
  530. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  531. failf(data, "mbedtls_ctr_drbg_seed returned (-0x%04X) %s",
  532. -ret, errorbuf);
  533. return CURLE_FAILED_INIT;
  534. }
  535. #endif /* THREADING_SUPPORT */
  536. /* Load the trusted CA */
  537. mbedtls_x509_crt_init(&backend->cacert);
  538. if(ca_info_blob && verifypeer) {
  539. /* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null
  540. terminated even when provided the exact length, forcing us to waste
  541. extra memory here. */
  542. unsigned char *newblob = Curl_memdup0(ca_info_blob->data,
  543. ca_info_blob->len);
  544. if(!newblob)
  545. return CURLE_OUT_OF_MEMORY;
  546. ret = mbedtls_x509_crt_parse(&backend->cacert, newblob,
  547. ca_info_blob->len + 1);
  548. free(newblob);
  549. if(ret < 0) {
  550. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  551. failf(data, "Error importing ca cert blob - mbedTLS: (-0x%04X) %s",
  552. -ret, errorbuf);
  553. return CURLE_SSL_CERTPROBLEM;
  554. }
  555. }
  556. if(ssl_cafile && verifypeer) {
  557. #ifdef MBEDTLS_FS_IO
  558. ret = mbedtls_x509_crt_parse_file(&backend->cacert, ssl_cafile);
  559. if(ret < 0) {
  560. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  561. failf(data, "Error reading ca cert file %s - mbedTLS: (-0x%04X) %s",
  562. ssl_cafile, -ret, errorbuf);
  563. return CURLE_SSL_CACERT_BADFILE;
  564. }
  565. #else
  566. failf(data, "mbedtls: functions that use the filesystem not built in");
  567. return CURLE_NOT_BUILT_IN;
  568. #endif
  569. }
  570. if(ssl_capath) {
  571. #ifdef MBEDTLS_FS_IO
  572. ret = mbedtls_x509_crt_parse_path(&backend->cacert, ssl_capath);
  573. if(ret < 0) {
  574. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  575. failf(data, "Error reading ca cert path %s - mbedTLS: (-0x%04X) %s",
  576. ssl_capath, -ret, errorbuf);
  577. if(verifypeer)
  578. return CURLE_SSL_CACERT_BADFILE;
  579. }
  580. #else
  581. failf(data, "mbedtls: functions that use the filesystem not built in");
  582. return CURLE_NOT_BUILT_IN;
  583. #endif
  584. }
  585. /* Load the client certificate */
  586. mbedtls_x509_crt_init(&backend->clicert);
  587. if(ssl_cert) {
  588. #ifdef MBEDTLS_FS_IO
  589. ret = mbedtls_x509_crt_parse_file(&backend->clicert, ssl_cert);
  590. if(ret) {
  591. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  592. failf(data, "Error reading client cert file %s - mbedTLS: (-0x%04X) %s",
  593. ssl_cert, -ret, errorbuf);
  594. return CURLE_SSL_CERTPROBLEM;
  595. }
  596. #else
  597. failf(data, "mbedtls: functions that use the filesystem not built in");
  598. return CURLE_NOT_BUILT_IN;
  599. #endif
  600. }
  601. if(ssl_cert_blob) {
  602. /* Unfortunately, mbedtls_x509_crt_parse() requires the data to be null
  603. terminated even when provided the exact length, forcing us to waste
  604. extra memory here. */
  605. unsigned char *newblob = Curl_memdup0(ssl_cert_blob->data,
  606. ssl_cert_blob->len);
  607. if(!newblob)
  608. return CURLE_OUT_OF_MEMORY;
  609. ret = mbedtls_x509_crt_parse(&backend->clicert, newblob,
  610. ssl_cert_blob->len + 1);
  611. free(newblob);
  612. if(ret) {
  613. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  614. failf(data, "Error reading client cert data %s - mbedTLS: (-0x%04X) %s",
  615. ssl_config->key, -ret, errorbuf);
  616. return CURLE_SSL_CERTPROBLEM;
  617. }
  618. }
  619. /* Load the client private key */
  620. mbedtls_pk_init(&backend->pk);
  621. if(ssl_config->key || ssl_config->key_blob) {
  622. if(ssl_config->key) {
  623. #ifdef MBEDTLS_FS_IO
  624. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  625. ret = mbedtls_pk_parse_keyfile(&backend->pk, ssl_config->key,
  626. ssl_config->key_passwd,
  627. mbedtls_ctr_drbg_random,
  628. &backend->ctr_drbg);
  629. #else
  630. ret = mbedtls_pk_parse_keyfile(&backend->pk, ssl_config->key,
  631. ssl_config->key_passwd);
  632. #endif
  633. if(ret) {
  634. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  635. failf(data, "Error reading private key %s - mbedTLS: (-0x%04X) %s",
  636. ssl_config->key, -ret, errorbuf);
  637. return CURLE_SSL_CERTPROBLEM;
  638. }
  639. #else
  640. failf(data, "mbedtls: functions that use the filesystem not built in");
  641. return CURLE_NOT_BUILT_IN;
  642. #endif
  643. }
  644. else {
  645. const struct curl_blob *ssl_key_blob = ssl_config->key_blob;
  646. const unsigned char *key_data =
  647. (const unsigned char *)ssl_key_blob->data;
  648. const char *passwd = ssl_config->key_passwd;
  649. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  650. ret = mbedtls_pk_parse_key(&backend->pk, key_data, ssl_key_blob->len,
  651. (const unsigned char *)passwd,
  652. passwd ? strlen(passwd) : 0,
  653. mbedtls_ctr_drbg_random,
  654. &backend->ctr_drbg);
  655. #else
  656. ret = mbedtls_pk_parse_key(&backend->pk, key_data, ssl_key_blob->len,
  657. (const unsigned char *)passwd,
  658. passwd ? strlen(passwd) : 0);
  659. #endif
  660. if(ret) {
  661. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  662. failf(data, "Error parsing private key - mbedTLS: (-0x%04X) %s",
  663. -ret, errorbuf);
  664. return CURLE_SSL_CERTPROBLEM;
  665. }
  666. }
  667. if(ret == 0 && !(mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_RSA) ||
  668. mbedtls_pk_can_do(&backend->pk, MBEDTLS_PK_ECKEY)))
  669. ret = MBEDTLS_ERR_PK_TYPE_MISMATCH;
  670. }
  671. /* Load the CRL */
  672. #ifdef MBEDTLS_X509_CRL_PARSE_C
  673. mbedtls_x509_crl_init(&backend->crl);
  674. if(ssl_crlfile) {
  675. #ifdef MBEDTLS_FS_IO
  676. ret = mbedtls_x509_crl_parse_file(&backend->crl, ssl_crlfile);
  677. if(ret) {
  678. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  679. failf(data, "Error reading CRL file %s - mbedTLS: (-0x%04X) %s",
  680. ssl_crlfile, -ret, errorbuf);
  681. return CURLE_SSL_CRL_BADFILE;
  682. }
  683. #else
  684. failf(data, "mbedtls: functions that use the filesystem not built in");
  685. return CURLE_NOT_BUILT_IN;
  686. #endif
  687. }
  688. #else
  689. if(ssl_crlfile) {
  690. failf(data, "mbedtls: crl support not built in");
  691. return CURLE_NOT_BUILT_IN;
  692. }
  693. #endif
  694. infof(data, "mbedTLS: Connecting to %s:%d", hostname, connssl->peer.port);
  695. mbedtls_ssl_config_init(&backend->config);
  696. ret = mbedtls_ssl_config_defaults(&backend->config,
  697. MBEDTLS_SSL_IS_CLIENT,
  698. MBEDTLS_SSL_TRANSPORT_STREAM,
  699. MBEDTLS_SSL_PRESET_DEFAULT);
  700. if(ret) {
  701. failf(data, "mbedTLS: ssl_config failed");
  702. return CURLE_SSL_CONNECT_ERROR;
  703. }
  704. #ifdef MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_ENABLED
  705. /* New in mbedTLS 3.6.1, need to enable, default is now disabled */
  706. mbedtls_ssl_conf_tls13_enable_signal_new_session_tickets(&backend->config,
  707. MBEDTLS_SSL_TLS1_3_SIGNAL_NEW_SESSION_TICKETS_ENABLED);
  708. #endif
  709. /* Always let mbedTLS verify certificates, if verifypeer or verifyhost are
  710. * disabled we clear the corresponding error flags in the verify callback
  711. * function. That is also where we log verification errors. */
  712. mbedtls_ssl_conf_verify(&backend->config, mbed_verify_cb, cf);
  713. mbedtls_ssl_conf_authmode(&backend->config, MBEDTLS_SSL_VERIFY_REQUIRED);
  714. mbedtls_ssl_init(&backend->ssl);
  715. backend->initialized = TRUE;
  716. /* new profile with RSA min key len = 1024 ... */
  717. mbedtls_ssl_conf_cert_profile(&backend->config,
  718. &mbedtls_x509_crt_profile_fr);
  719. ret = mbed_set_ssl_version_min_max(data, backend, conn_config);
  720. if(ret != CURLE_OK)
  721. return ret;
  722. mbedtls_ssl_conf_rng(&backend->config, mbedtls_ctr_drbg_random,
  723. &backend->ctr_drbg);
  724. ret = mbedtls_ssl_setup(&backend->ssl, &backend->config);
  725. if(ret) {
  726. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  727. failf(data, "ssl_setup failed - mbedTLS: (-0x%04X) %s",
  728. -ret, errorbuf);
  729. return CURLE_SSL_CONNECT_ERROR;
  730. }
  731. mbedtls_ssl_set_bio(&backend->ssl, cf,
  732. mbedtls_bio_cf_write,
  733. mbedtls_bio_cf_read,
  734. NULL /* rev_timeout() */);
  735. #ifndef TLS13_SUPPORT
  736. if(conn_config->cipher_list) {
  737. CURLcode result = mbed_set_selected_ciphers(data, backend,
  738. conn_config->cipher_list,
  739. NULL);
  740. #else
  741. if(conn_config->cipher_list || conn_config->cipher_list13) {
  742. CURLcode result = mbed_set_selected_ciphers(data, backend,
  743. conn_config->cipher_list,
  744. conn_config->cipher_list13);
  745. #endif
  746. if(result != CURLE_OK) {
  747. failf(data, "mbedTLS: failed to set cipher suites");
  748. return result;
  749. }
  750. }
  751. else {
  752. mbedtls_ssl_conf_ciphersuites(&backend->config,
  753. mbedtls_ssl_list_ciphersuites());
  754. }
  755. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  756. mbedtls_ssl_conf_renegotiation(&backend->config,
  757. MBEDTLS_SSL_RENEGOTIATION_ENABLED);
  758. #endif
  759. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  760. mbedtls_ssl_conf_session_tickets(&backend->config,
  761. MBEDTLS_SSL_SESSION_TICKETS_DISABLED);
  762. #endif
  763. /* Check if there is a cached ID we can/should use here! */
  764. if(ssl_config->primary.cache_session) {
  765. void *sdata = NULL;
  766. size_t slen = 0;
  767. Curl_ssl_sessionid_lock(data);
  768. if(!Curl_ssl_getsessionid(cf, data, &connssl->peer,
  769. &sdata, &slen, NULL) && slen) {
  770. mbedtls_ssl_session session;
  771. mbedtls_ssl_session_init(&session);
  772. ret = mbedtls_ssl_session_load(&session, sdata, slen);
  773. if(ret) {
  774. failf(data, "error loading cached session: -0x%x", -ret);
  775. }
  776. else {
  777. ret = mbedtls_ssl_set_session(&backend->ssl, &session);
  778. if(ret)
  779. failf(data, "error setting session: -0x%x", -ret);
  780. else
  781. infof(data, "SSL reusing session ID");
  782. }
  783. mbedtls_ssl_session_free(&session);
  784. }
  785. Curl_ssl_sessionid_unlock(data);
  786. }
  787. mbedtls_ssl_conf_ca_chain(&backend->config,
  788. &backend->cacert,
  789. #ifdef MBEDTLS_X509_CRL_PARSE_C
  790. &backend->crl);
  791. #else
  792. NULL);
  793. #endif
  794. if(ssl_config->key || ssl_config->key_blob) {
  795. mbedtls_ssl_conf_own_cert(&backend->config,
  796. &backend->clicert, &backend->pk);
  797. }
  798. if(mbedtls_ssl_set_hostname(&backend->ssl, connssl->peer.sni ?
  799. connssl->peer.sni : connssl->peer.hostname)) {
  800. /* mbedtls_ssl_set_hostname() sets the name to use in CN/SAN checks and
  801. the name to set in the SNI extension. So even if curl connects to a
  802. host specified as an IP address, this function must be used. */
  803. failf(data, "Failed to set SNI");
  804. return CURLE_SSL_CONNECT_ERROR;
  805. }
  806. #ifdef HAS_ALPN
  807. if(connssl->alpn) {
  808. struct alpn_proto_buf proto;
  809. size_t i;
  810. for(i = 0; i < connssl->alpn->count; ++i) {
  811. backend->protocols[i] = connssl->alpn->entries[i];
  812. }
  813. /* this function does not clone the protocols array, which is why we need
  814. to keep it around */
  815. if(mbedtls_ssl_conf_alpn_protocols(&backend->config,
  816. &backend->protocols[0])) {
  817. failf(data, "Failed setting ALPN protocols");
  818. return CURLE_SSL_CONNECT_ERROR;
  819. }
  820. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  821. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  822. }
  823. #endif
  824. #ifdef MBEDTLS_DEBUG
  825. /* In order to make that work in mbedtls MBEDTLS_DEBUG_C must be defined. */
  826. mbedtls_ssl_conf_dbg(&backend->config, mbed_debug, data);
  827. /* - 0 No debug
  828. * - 1 Error
  829. * - 2 State change
  830. * - 3 Informational
  831. * - 4 Verbose
  832. */
  833. mbedtls_debug_set_threshold(4);
  834. #endif
  835. /* give application a chance to interfere with mbedTLS set up. */
  836. if(data->set.ssl.fsslctx) {
  837. CURLcode result = (*data->set.ssl.fsslctx)(data, &backend->config,
  838. data->set.ssl.fsslctxp);
  839. if(result != CURLE_OK) {
  840. failf(data, "error signaled by ssl ctx callback");
  841. return result;
  842. }
  843. }
  844. connssl->connecting_state = ssl_connect_2;
  845. return CURLE_OK;
  846. }
  847. static CURLcode
  848. mbed_connect_step2(struct Curl_cfilter *cf, struct Curl_easy *data)
  849. {
  850. int ret;
  851. struct ssl_connect_data *connssl = cf->ctx;
  852. struct mbed_ssl_backend_data *backend =
  853. (struct mbed_ssl_backend_data *)connssl->backend;
  854. #ifndef CURL_DISABLE_PROXY
  855. const char * const pinnedpubkey = Curl_ssl_cf_is_proxy(cf) ?
  856. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
  857. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  858. #else
  859. const char * const pinnedpubkey = data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  860. #endif
  861. DEBUGASSERT(backend);
  862. ret = mbedtls_ssl_handshake(&backend->ssl);
  863. if(ret == MBEDTLS_ERR_SSL_WANT_READ) {
  864. connssl->io_need = CURL_SSL_IO_NEED_RECV;
  865. return CURLE_OK;
  866. }
  867. else if(ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  868. connssl->io_need = CURL_SSL_IO_NEED_SEND;
  869. return CURLE_OK;
  870. }
  871. else if(ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
  872. failf(data, "peer certificate could not be verified");
  873. return CURLE_PEER_FAILED_VERIFICATION;
  874. }
  875. else if(ret) {
  876. char errorbuf[128];
  877. #if MBEDTLS_VERSION_NUMBER >= 0x03020000
  878. CURL_TRC_CF(data, cf, "TLS version %04X",
  879. mbedtls_ssl_get_version_number(&backend->ssl));
  880. #endif
  881. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  882. failf(data, "ssl_handshake returned: (-0x%04X) %s",
  883. -ret, errorbuf);
  884. return CURLE_SSL_CONNECT_ERROR;
  885. }
  886. #if MBEDTLS_VERSION_NUMBER >= 0x03020000
  887. {
  888. char cipher_str[64];
  889. uint16_t cipher_id;
  890. cipher_id = (uint16_t)
  891. mbedtls_ssl_get_ciphersuite_id_from_ssl(&backend->ssl);
  892. mbed_cipher_suite_get_str(cipher_id, cipher_str, sizeof(cipher_str), TRUE);
  893. infof(data, "mbedTLS: %s Handshake complete, cipher is %s",
  894. mbedtls_ssl_get_version(&backend->ssl), cipher_str);
  895. }
  896. #else
  897. infof(data, "mbedTLS: %s Handshake complete",
  898. mbedtls_ssl_get_version(&backend->ssl));
  899. #endif
  900. if(pinnedpubkey) {
  901. int size;
  902. CURLcode result;
  903. const mbedtls_x509_crt *peercert;
  904. mbedtls_x509_crt *p = NULL;
  905. unsigned char *pubkey = NULL;
  906. peercert = mbedtls_ssl_get_peer_cert(&backend->ssl);
  907. #if MBEDTLS_VERSION_NUMBER == 0x03000000
  908. if(!peercert || !peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(p) ||
  909. !peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(len)) {
  910. #else
  911. if(!peercert || !peercert->raw.p || !peercert->raw.len) {
  912. #endif
  913. failf(data, "Failed due to missing peer certificate");
  914. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  915. }
  916. p = calloc(1, sizeof(*p));
  917. if(!p)
  918. return CURLE_OUT_OF_MEMORY;
  919. pubkey = malloc(PUB_DER_MAX_BYTES);
  920. if(!pubkey) {
  921. result = CURLE_OUT_OF_MEMORY;
  922. goto pinnedpubkey_error;
  923. }
  924. mbedtls_x509_crt_init(p);
  925. /* Make a copy of our const peercert because mbedtls_pk_write_pubkey_der
  926. needs a non-const key, for now.
  927. https://github.com/Mbed-TLS/mbedtls/issues/396 */
  928. #if MBEDTLS_VERSION_NUMBER == 0x03000000
  929. if(mbedtls_x509_crt_parse_der(p,
  930. peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(p),
  931. peercert->MBEDTLS_PRIVATE(raw).MBEDTLS_PRIVATE(len))) {
  932. #else
  933. if(mbedtls_x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) {
  934. #endif
  935. failf(data, "Failed copying peer certificate");
  936. result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  937. goto pinnedpubkey_error;
  938. }
  939. #if MBEDTLS_VERSION_NUMBER == 0x03000000
  940. size = mbedtls_pk_write_pubkey_der(&p->MBEDTLS_PRIVATE(pk), pubkey,
  941. PUB_DER_MAX_BYTES);
  942. #else
  943. size = mbedtls_pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES);
  944. #endif
  945. if(size <= 0) {
  946. failf(data, "Failed copying public key from peer certificate");
  947. result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  948. goto pinnedpubkey_error;
  949. }
  950. /* mbedtls_pk_write_pubkey_der writes data at the end of the buffer. */
  951. result = Curl_pin_peer_pubkey(data,
  952. pinnedpubkey,
  953. &pubkey[PUB_DER_MAX_BYTES - size], size);
  954. pinnedpubkey_error:
  955. mbedtls_x509_crt_free(p);
  956. free(p);
  957. free(pubkey);
  958. if(result) {
  959. return result;
  960. }
  961. }
  962. #ifdef HAS_ALPN
  963. if(connssl->alpn) {
  964. const char *proto = mbedtls_ssl_get_alpn_protocol(&backend->ssl);
  965. Curl_alpn_set_negotiated(cf, data, connssl, (const unsigned char *)proto,
  966. proto ? strlen(proto) : 0);
  967. }
  968. #endif
  969. connssl->connecting_state = ssl_connect_3;
  970. infof(data, "SSL connected");
  971. return CURLE_OK;
  972. }
  973. static void mbedtls_session_free(void *session, size_t slen)
  974. {
  975. (void)slen;
  976. free(session);
  977. }
  978. static CURLcode
  979. mbed_new_session(struct Curl_cfilter *cf, struct Curl_easy *data)
  980. {
  981. struct ssl_connect_data *connssl = cf->ctx;
  982. struct mbed_ssl_backend_data *backend =
  983. (struct mbed_ssl_backend_data *)connssl->backend;
  984. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  985. CURLcode result = CURLE_OK;
  986. DEBUGASSERT(backend);
  987. if(ssl_config->primary.cache_session) {
  988. int ret;
  989. mbedtls_ssl_session session;
  990. unsigned char *sdata = NULL;
  991. size_t slen = 0;
  992. mbedtls_ssl_session_init(&session);
  993. ret = mbedtls_ssl_get_session(&backend->ssl, &session);
  994. if(ret) {
  995. if(ret != MBEDTLS_ERR_SSL_ALLOC_FAILED)
  996. mbedtls_ssl_session_free(&session);
  997. failf(data, "mbedtls_ssl_get_session returned -0x%x", -ret);
  998. return CURLE_SSL_CONNECT_ERROR;
  999. }
  1000. mbedtls_ssl_session_save(&session, NULL, 0, &slen);
  1001. if(!slen) {
  1002. failf(data, "failed to serialize session: length is 0");
  1003. }
  1004. else {
  1005. sdata = malloc(slen);
  1006. if(sdata) {
  1007. ret = mbedtls_ssl_session_save(&session, sdata, slen, &slen);
  1008. if(ret) {
  1009. failf(data, "failed to serialize session: -0x%x", -ret);
  1010. }
  1011. else {
  1012. Curl_ssl_sessionid_lock(data);
  1013. result = Curl_ssl_set_sessionid(cf, data, &connssl->peer, NULL,
  1014. sdata, slen, mbedtls_session_free);
  1015. Curl_ssl_sessionid_unlock(data);
  1016. if(!result)
  1017. sdata = NULL;
  1018. }
  1019. }
  1020. }
  1021. mbedtls_ssl_session_free(&session);
  1022. free(sdata);
  1023. }
  1024. return result;
  1025. }
  1026. static ssize_t mbed_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  1027. const void *mem, size_t len,
  1028. CURLcode *curlcode)
  1029. {
  1030. struct ssl_connect_data *connssl = cf->ctx;
  1031. struct mbed_ssl_backend_data *backend =
  1032. (struct mbed_ssl_backend_data *)connssl->backend;
  1033. int ret = -1;
  1034. (void)data;
  1035. DEBUGASSERT(backend);
  1036. ret = mbedtls_ssl_write(&backend->ssl, (unsigned char *)mem, len);
  1037. if(ret < 0) {
  1038. CURL_TRC_CF(data, cf, "mbedtls_ssl_write(len=%zu) -> -0x%04X",
  1039. len, -ret);
  1040. *curlcode = ((ret == MBEDTLS_ERR_SSL_WANT_WRITE)
  1041. #ifdef TLS13_SUPPORT
  1042. || (ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET)
  1043. #endif
  1044. ) ? CURLE_AGAIN : CURLE_SEND_ERROR;
  1045. ret = -1;
  1046. }
  1047. return ret;
  1048. }
  1049. static void mbedtls_close_all(struct Curl_easy *data)
  1050. {
  1051. (void)data;
  1052. }
  1053. static CURLcode mbedtls_shutdown(struct Curl_cfilter *cf,
  1054. struct Curl_easy *data,
  1055. bool send_shutdown, bool *done)
  1056. {
  1057. struct ssl_connect_data *connssl = cf->ctx;
  1058. struct mbed_ssl_backend_data *backend =
  1059. (struct mbed_ssl_backend_data *)connssl->backend;
  1060. unsigned char buf[1024];
  1061. CURLcode result = CURLE_OK;
  1062. int ret;
  1063. size_t i;
  1064. DEBUGASSERT(backend);
  1065. if(!backend->initialized || cf->shutdown) {
  1066. *done = TRUE;
  1067. return CURLE_OK;
  1068. }
  1069. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  1070. *done = FALSE;
  1071. if(!backend->sent_shutdown) {
  1072. /* do this only once */
  1073. backend->sent_shutdown = TRUE;
  1074. if(send_shutdown) {
  1075. ret = mbedtls_ssl_close_notify(&backend->ssl);
  1076. switch(ret) {
  1077. case 0: /* we sent it, receive from the server */
  1078. break;
  1079. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY: /* server also closed */
  1080. *done = TRUE;
  1081. goto out;
  1082. case MBEDTLS_ERR_SSL_WANT_READ:
  1083. connssl->io_need = CURL_SSL_IO_NEED_RECV;
  1084. goto out;
  1085. case MBEDTLS_ERR_SSL_WANT_WRITE:
  1086. connssl->io_need = CURL_SSL_IO_NEED_SEND;
  1087. goto out;
  1088. default:
  1089. CURL_TRC_CF(data, cf, "mbedtls_shutdown error -0x%04X", -ret);
  1090. result = CURLE_RECV_ERROR;
  1091. goto out;
  1092. }
  1093. }
  1094. }
  1095. /* SSL should now have started the shutdown from our side. Since it
  1096. * was not complete, we are lacking the close notify from the server. */
  1097. for(i = 0; i < 10; ++i) {
  1098. ret = mbedtls_ssl_read(&backend->ssl, buf, sizeof(buf));
  1099. /* This seems to be a bug in mbedTLS TLSv1.3 where it reports
  1100. * WANT_READ, but has not encountered an EAGAIN. */
  1101. if(ret == MBEDTLS_ERR_SSL_WANT_READ)
  1102. ret = mbedtls_ssl_read(&backend->ssl, buf, sizeof(buf));
  1103. #ifdef TLS13_SUPPORT
  1104. if(ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET)
  1105. continue;
  1106. #endif
  1107. if(ret <= 0)
  1108. break;
  1109. }
  1110. if(ret > 0) {
  1111. /* still data coming in? */
  1112. CURL_TRC_CF(data, cf, "mbedtls_shutdown, still getting data");
  1113. }
  1114. else if(ret == 0 || (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)) {
  1115. /* We got the close notify alert and are done. */
  1116. CURL_TRC_CF(data, cf, "mbedtls_shutdown done");
  1117. *done = TRUE;
  1118. }
  1119. else if(ret == MBEDTLS_ERR_SSL_WANT_READ) {
  1120. CURL_TRC_CF(data, cf, "mbedtls_shutdown, need RECV");
  1121. connssl->io_need = CURL_SSL_IO_NEED_RECV;
  1122. }
  1123. else if(ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
  1124. CURL_TRC_CF(data, cf, "mbedtls_shutdown, need SEND");
  1125. connssl->io_need = CURL_SSL_IO_NEED_SEND;
  1126. }
  1127. else {
  1128. CURL_TRC_CF(data, cf, "mbedtls_shutdown error -0x%04X", -ret);
  1129. result = CURLE_RECV_ERROR;
  1130. }
  1131. out:
  1132. cf->shutdown = (result || *done);
  1133. return result;
  1134. }
  1135. static void mbedtls_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1136. {
  1137. struct ssl_connect_data *connssl = cf->ctx;
  1138. struct mbed_ssl_backend_data *backend =
  1139. (struct mbed_ssl_backend_data *)connssl->backend;
  1140. (void)data;
  1141. DEBUGASSERT(backend);
  1142. if(backend->initialized) {
  1143. mbedtls_pk_free(&backend->pk);
  1144. mbedtls_x509_crt_free(&backend->clicert);
  1145. mbedtls_x509_crt_free(&backend->cacert);
  1146. #ifdef MBEDTLS_X509_CRL_PARSE_C
  1147. mbedtls_x509_crl_free(&backend->crl);
  1148. #endif
  1149. Curl_safefree(backend->ciphersuites);
  1150. mbedtls_ssl_config_free(&backend->config);
  1151. mbedtls_ssl_free(&backend->ssl);
  1152. mbedtls_ctr_drbg_free(&backend->ctr_drbg);
  1153. #ifndef THREADING_SUPPORT
  1154. mbedtls_entropy_free(&backend->entropy);
  1155. #endif /* THREADING_SUPPORT */
  1156. backend->initialized = FALSE;
  1157. }
  1158. }
  1159. static ssize_t mbed_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  1160. char *buf, size_t buffersize,
  1161. CURLcode *curlcode)
  1162. {
  1163. struct ssl_connect_data *connssl = cf->ctx;
  1164. struct mbed_ssl_backend_data *backend =
  1165. (struct mbed_ssl_backend_data *)connssl->backend;
  1166. int ret = -1;
  1167. (void)data;
  1168. DEBUGASSERT(backend);
  1169. ret = mbedtls_ssl_read(&backend->ssl, (unsigned char *)buf,
  1170. buffersize);
  1171. if(ret <= 0) {
  1172. CURL_TRC_CF(data, cf, "mbedtls_ssl_read(len=%zu) -> -0x%04X",
  1173. buffersize, -ret);
  1174. switch(ret) {
  1175. #ifdef HAS_SESSION_TICKETS
  1176. case MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET:
  1177. mbed_new_session(cf, data);
  1178. FALLTHROUGH();
  1179. #endif
  1180. case MBEDTLS_ERR_SSL_WANT_READ:
  1181. *curlcode = CURLE_AGAIN;
  1182. ret = -1;
  1183. break;
  1184. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  1185. *curlcode = CURLE_OK;
  1186. ret = 0;
  1187. break;
  1188. default: {
  1189. char errorbuf[128];
  1190. mbedtls_strerror(ret, errorbuf, sizeof(errorbuf));
  1191. failf(data, "ssl_read returned: (-0x%04X) %s", -ret, errorbuf);
  1192. *curlcode = CURLE_RECV_ERROR;
  1193. ret = -1;
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. return (ssize_t)ret;
  1199. }
  1200. static size_t mbedtls_version(char *buffer, size_t size)
  1201. {
  1202. #ifdef MBEDTLS_VERSION_C
  1203. /* if mbedtls_version_get_number() is available it is better */
  1204. unsigned int version = mbedtls_version_get_number();
  1205. return msnprintf(buffer, size, "mbedTLS/%u.%u.%u", version >> 24,
  1206. (version >> 16) & 0xff, (version >> 8) & 0xff);
  1207. #else
  1208. return msnprintf(buffer, size, "mbedTLS/%s", MBEDTLS_VERSION_STRING);
  1209. #endif
  1210. }
  1211. /* 'data' might be NULL */
  1212. static CURLcode mbedtls_random(struct Curl_easy *data,
  1213. unsigned char *entropy, size_t length)
  1214. {
  1215. #if defined(MBEDTLS_CTR_DRBG_C)
  1216. int ret;
  1217. mbedtls_entropy_context ctr_entropy;
  1218. mbedtls_ctr_drbg_context ctr_drbg;
  1219. mbedtls_entropy_init(&ctr_entropy);
  1220. mbedtls_ctr_drbg_init(&ctr_drbg);
  1221. (void)data;
  1222. ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
  1223. &ctr_entropy, NULL, 0);
  1224. if(!ret)
  1225. ret = mbedtls_ctr_drbg_random(&ctr_drbg, entropy, length);
  1226. mbedtls_ctr_drbg_free(&ctr_drbg);
  1227. mbedtls_entropy_free(&ctr_entropy);
  1228. return ret == 0 ? CURLE_OK : CURLE_FAILED_INIT;
  1229. #elif defined(MBEDTLS_HAVEGE_C)
  1230. mbedtls_havege_state hs;
  1231. mbedtls_havege_init(&hs);
  1232. mbedtls_havege_random(&hs, entropy, length);
  1233. mbedtls_havege_free(&hs);
  1234. return CURLE_OK;
  1235. #else
  1236. return CURLE_NOT_BUILT_IN;
  1237. #endif
  1238. }
  1239. static CURLcode
  1240. mbed_connect_common(struct Curl_cfilter *cf, struct Curl_easy *data,
  1241. bool nonblocking,
  1242. bool *done)
  1243. {
  1244. CURLcode retcode;
  1245. struct ssl_connect_data *connssl = cf->ctx;
  1246. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  1247. timediff_t timeout_ms;
  1248. int what;
  1249. /* check if the connection has already been established */
  1250. if(ssl_connection_complete == connssl->state) {
  1251. *done = TRUE;
  1252. return CURLE_OK;
  1253. }
  1254. if(ssl_connect_1 == connssl->connecting_state) {
  1255. /* Find out how much more time we are allowed */
  1256. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1257. if(timeout_ms < 0) {
  1258. /* no need to continue if time already is up */
  1259. failf(data, "SSL connection timeout");
  1260. return CURLE_OPERATION_TIMEDOUT;
  1261. }
  1262. retcode = mbed_connect_step1(cf, data);
  1263. if(retcode)
  1264. return retcode;
  1265. }
  1266. while(ssl_connect_2 == connssl->connecting_state) {
  1267. /* check allowed time left */
  1268. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1269. if(timeout_ms < 0) {
  1270. /* no need to continue if time already is up */
  1271. failf(data, "SSL connection timeout");
  1272. return CURLE_OPERATION_TIMEDOUT;
  1273. }
  1274. /* if ssl is expecting something, check if it is available. */
  1275. if(connssl->io_need) {
  1276. curl_socket_t writefd = (connssl->io_need & CURL_SSL_IO_NEED_SEND) ?
  1277. sockfd : CURL_SOCKET_BAD;
  1278. curl_socket_t readfd = (connssl->io_need & CURL_SSL_IO_NEED_RECV) ?
  1279. sockfd : CURL_SOCKET_BAD;
  1280. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  1281. nonblocking ? 0 : timeout_ms);
  1282. if(what < 0) {
  1283. /* fatal error */
  1284. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1285. return CURLE_SSL_CONNECT_ERROR;
  1286. }
  1287. else if(0 == what) {
  1288. if(nonblocking) {
  1289. *done = FALSE;
  1290. return CURLE_OK;
  1291. }
  1292. else {
  1293. /* timeout */
  1294. failf(data, "SSL connection timeout");
  1295. return CURLE_OPERATION_TIMEDOUT;
  1296. }
  1297. }
  1298. /* socket is readable or writable */
  1299. }
  1300. /* Run transaction, and return to the caller if it failed or if
  1301. * this connection is part of a multi handle and this loop would
  1302. * execute again. This permits the owner of a multi handle to
  1303. * abort a connection attempt before step2 has completed while
  1304. * ensuring that a client using select() or epoll() will always
  1305. * have a valid fdset to wait on.
  1306. */
  1307. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  1308. retcode = mbed_connect_step2(cf, data);
  1309. if(retcode ||
  1310. (nonblocking && (ssl_connect_2 == connssl->connecting_state)))
  1311. return retcode;
  1312. } /* repeat step2 until all transactions are done. */
  1313. if(ssl_connect_3 == connssl->connecting_state) {
  1314. /* For tls1.3 we get notified about new sessions */
  1315. #if MBEDTLS_VERSION_NUMBER >= 0x03020000
  1316. struct ssl_connect_data *ctx = cf->ctx;
  1317. struct mbed_ssl_backend_data *backend =
  1318. (struct mbed_ssl_backend_data *)ctx->backend;
  1319. if(mbedtls_ssl_get_version_number(&backend->ssl) <=
  1320. MBEDTLS_SSL_VERSION_TLS1_2) {
  1321. #else
  1322. { /* no TLSv1.3 supported here */
  1323. #endif
  1324. retcode = mbed_new_session(cf, data);
  1325. if(retcode)
  1326. return retcode;
  1327. }
  1328. connssl->connecting_state = ssl_connect_done;
  1329. }
  1330. if(ssl_connect_done == connssl->connecting_state) {
  1331. connssl->state = ssl_connection_complete;
  1332. *done = TRUE;
  1333. }
  1334. else
  1335. *done = FALSE;
  1336. /* Reset our connect state machine */
  1337. connssl->connecting_state = ssl_connect_1;
  1338. return CURLE_OK;
  1339. }
  1340. static CURLcode mbedtls_connect_nonblocking(struct Curl_cfilter *cf,
  1341. struct Curl_easy *data,
  1342. bool *done)
  1343. {
  1344. return mbed_connect_common(cf, data, TRUE, done);
  1345. }
  1346. static CURLcode mbedtls_connect(struct Curl_cfilter *cf,
  1347. struct Curl_easy *data)
  1348. {
  1349. CURLcode retcode;
  1350. bool done = FALSE;
  1351. retcode = mbed_connect_common(cf, data, FALSE, &done);
  1352. if(retcode)
  1353. return retcode;
  1354. DEBUGASSERT(done);
  1355. return CURLE_OK;
  1356. }
  1357. /*
  1358. * return 0 error initializing SSL
  1359. * return 1 SSL initialized successfully
  1360. */
  1361. static int mbedtls_init(void)
  1362. {
  1363. if(!Curl_mbedtlsthreadlock_thread_setup())
  1364. return 0;
  1365. #ifdef THREADING_SUPPORT
  1366. entropy_init_mutex(&ts_entropy);
  1367. #endif
  1368. #ifdef TLS13_SUPPORT
  1369. {
  1370. int ret;
  1371. #ifdef THREADING_SUPPORT
  1372. Curl_mbedtlsthreadlock_lock_function(0);
  1373. #endif
  1374. ret = psa_crypto_init();
  1375. #ifdef THREADING_SUPPORT
  1376. Curl_mbedtlsthreadlock_unlock_function(0);
  1377. #endif
  1378. if(ret != PSA_SUCCESS)
  1379. return 0;
  1380. }
  1381. #endif /* TLS13_SUPPORT */
  1382. return 1;
  1383. }
  1384. static void mbedtls_cleanup(void)
  1385. {
  1386. #ifdef THREADING_SUPPORT
  1387. entropy_cleanup_mutex(&ts_entropy);
  1388. #endif
  1389. (void)Curl_mbedtlsthreadlock_thread_cleanup();
  1390. }
  1391. static bool mbedtls_data_pending(struct Curl_cfilter *cf,
  1392. const struct Curl_easy *data)
  1393. {
  1394. struct ssl_connect_data *ctx = cf->ctx;
  1395. struct mbed_ssl_backend_data *backend;
  1396. (void)data;
  1397. DEBUGASSERT(ctx && ctx->backend);
  1398. backend = (struct mbed_ssl_backend_data *)ctx->backend;
  1399. return mbedtls_ssl_get_bytes_avail(&backend->ssl) != 0;
  1400. }
  1401. static CURLcode mbedtls_sha256sum(const unsigned char *input,
  1402. size_t inputlen,
  1403. unsigned char *sha256sum,
  1404. size_t sha256len UNUSED_PARAM)
  1405. {
  1406. /* TODO: explain this for different mbedtls 2.x vs 3 version */
  1407. (void)sha256len;
  1408. #if MBEDTLS_VERSION_NUMBER < 0x02070000
  1409. mbedtls_sha256(input, inputlen, sha256sum, 0);
  1410. #else
  1411. /* returns 0 on success, otherwise failure */
  1412. #if MBEDTLS_VERSION_NUMBER >= 0x03000000
  1413. if(mbedtls_sha256(input, inputlen, sha256sum, 0) != 0)
  1414. #else
  1415. if(mbedtls_sha256_ret(input, inputlen, sha256sum, 0) != 0)
  1416. #endif
  1417. return CURLE_BAD_FUNCTION_ARGUMENT;
  1418. #endif
  1419. return CURLE_OK;
  1420. }
  1421. static void *mbedtls_get_internals(struct ssl_connect_data *connssl,
  1422. CURLINFO info UNUSED_PARAM)
  1423. {
  1424. struct mbed_ssl_backend_data *backend =
  1425. (struct mbed_ssl_backend_data *)connssl->backend;
  1426. (void)info;
  1427. DEBUGASSERT(backend);
  1428. return &backend->ssl;
  1429. }
  1430. const struct Curl_ssl Curl_ssl_mbedtls = {
  1431. { CURLSSLBACKEND_MBEDTLS, "mbedtls" }, /* info */
  1432. SSLSUPP_CA_PATH |
  1433. SSLSUPP_CAINFO_BLOB |
  1434. SSLSUPP_CERTINFO |
  1435. SSLSUPP_PINNEDPUBKEY |
  1436. SSLSUPP_SSL_CTX |
  1437. #ifdef TLS13_SUPPORT
  1438. SSLSUPP_TLS13_CIPHERSUITES |
  1439. #endif
  1440. SSLSUPP_HTTPS_PROXY |
  1441. SSLSUPP_CIPHER_LIST,
  1442. sizeof(struct mbed_ssl_backend_data),
  1443. mbedtls_init, /* init */
  1444. mbedtls_cleanup, /* cleanup */
  1445. mbedtls_version, /* version */
  1446. Curl_none_check_cxn, /* check_cxn */
  1447. mbedtls_shutdown, /* shutdown */
  1448. mbedtls_data_pending, /* data_pending */
  1449. mbedtls_random, /* random */
  1450. Curl_none_cert_status_request, /* cert_status_request */
  1451. mbedtls_connect, /* connect */
  1452. mbedtls_connect_nonblocking, /* connect_nonblocking */
  1453. Curl_ssl_adjust_pollset, /* adjust_pollset */
  1454. mbedtls_get_internals, /* get_internals */
  1455. mbedtls_close, /* close_one */
  1456. mbedtls_close_all, /* close_all */
  1457. Curl_none_set_engine, /* set_engine */
  1458. Curl_none_set_engine_default, /* set_engine_default */
  1459. Curl_none_engines_list, /* engines_list */
  1460. Curl_none_false_start, /* false_start */
  1461. mbedtls_sha256sum, /* sha256sum */
  1462. NULL, /* associate_connection */
  1463. NULL, /* disassociate_connection */
  1464. mbed_recv, /* recv decrypted data */
  1465. mbed_send, /* send data to encrypt */
  1466. NULL, /* get_channel_binding */
  1467. };
  1468. #endif /* USE_MBEDTLS */