mbedtls.c 50 KB

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