2
0

mbedtls.c 50 KB

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