2
0

wolfssl.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /*
  25. * Source file for all wolfSSL specific code for the TLS/SSL layer. No code
  26. * but vtls.c should ever call or use these functions.
  27. *
  28. */
  29. #include "curl_setup.h"
  30. #ifdef USE_WOLFSSL
  31. #define WOLFSSL_OPTIONS_IGNORE_SYS
  32. #include <wolfssl/version.h>
  33. #include <wolfssl/options.h>
  34. /* To determine what functions are available we rely on one or both of:
  35. - the user's options.h generated by wolfSSL
  36. - the symbols detected by curl's configure
  37. Since they are markedly different from one another, and one or the other may
  38. not be available, we do some checking below to bring things in sync. */
  39. /* HAVE_ALPN is wolfSSL's build time symbol for enabling ALPN in options.h. */
  40. #ifndef HAVE_ALPN
  41. #ifdef HAVE_WOLFSSL_USEALPN
  42. #define HAVE_ALPN
  43. #endif
  44. #endif
  45. #include <limits.h>
  46. #include "urldata.h"
  47. #include "sendf.h"
  48. #include "inet_pton.h"
  49. #include "vtls.h"
  50. #include "vtls_int.h"
  51. #include "keylog.h"
  52. #include "parsedate.h"
  53. #include "connect.h" /* for the connect timeout */
  54. #include "select.h"
  55. #include "strcase.h"
  56. #include "x509asn1.h"
  57. #include "curl_printf.h"
  58. #include "multiif.h"
  59. #include <wolfssl/openssl/ssl.h>
  60. #include <wolfssl/ssl.h>
  61. #include <wolfssl/error-ssl.h>
  62. #include "wolfssl.h"
  63. /* The last #include files should be: */
  64. #include "curl_memory.h"
  65. #include "memdebug.h"
  66. #ifdef USE_ECH
  67. # include "curl_base64.h"
  68. # define ECH_ENABLED(__data__) \
  69. (__data__->set.tls_ech && \
  70. !(__data__->set.tls_ech & CURLECH_DISABLE)\
  71. )
  72. #endif /* USE_ECH */
  73. /* KEEP_PEER_CERT is a product of the presence of build time symbol
  74. OPENSSL_EXTRA without NO_CERTS, depending on the version. KEEP_PEER_CERT is
  75. in wolfSSL's settings.h, and the latter two are build time symbols in
  76. options.h. */
  77. #ifndef KEEP_PEER_CERT
  78. #if defined(HAVE_WOLFSSL_GET_PEER_CERTIFICATE) || \
  79. (defined(OPENSSL_EXTRA) && !defined(NO_CERTS))
  80. #define KEEP_PEER_CERT
  81. #endif
  82. #endif
  83. #if defined(HAVE_WOLFSSL_FULL_BIO) && HAVE_WOLFSSL_FULL_BIO
  84. #define USE_BIO_CHAIN
  85. #else
  86. #undef USE_BIO_CHAIN
  87. #endif
  88. #ifdef OPENSSL_EXTRA
  89. /*
  90. * Availability note:
  91. * The TLS 1.3 secret callback (wolfSSL_set_tls13_secret_cb) was added in
  92. * wolfSSL 4.4.0, but requires the -DHAVE_SECRET_CALLBACK build option. If that
  93. * option is not set, then TLS 1.3 will not be logged.
  94. * For TLS 1.2 and before, we use wolfSSL_get_keys().
  95. * SSL_get_client_random and wolfSSL_get_keys require OPENSSL_EXTRA
  96. * (--enable-opensslextra or --enable-all).
  97. */
  98. #if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
  99. static int
  100. wolfssl_tls13_secret_callback(SSL *ssl, int id, const unsigned char *secret,
  101. int secretSz, void *ctx)
  102. {
  103. const char *label;
  104. unsigned char client_random[SSL3_RANDOM_SIZE];
  105. (void)ctx;
  106. if(!ssl || !Curl_tls_keylog_enabled()) {
  107. return 0;
  108. }
  109. switch(id) {
  110. case CLIENT_EARLY_TRAFFIC_SECRET:
  111. label = "CLIENT_EARLY_TRAFFIC_SECRET";
  112. break;
  113. case CLIENT_HANDSHAKE_TRAFFIC_SECRET:
  114. label = "CLIENT_HANDSHAKE_TRAFFIC_SECRET";
  115. break;
  116. case SERVER_HANDSHAKE_TRAFFIC_SECRET:
  117. label = "SERVER_HANDSHAKE_TRAFFIC_SECRET";
  118. break;
  119. case CLIENT_TRAFFIC_SECRET:
  120. label = "CLIENT_TRAFFIC_SECRET_0";
  121. break;
  122. case SERVER_TRAFFIC_SECRET:
  123. label = "SERVER_TRAFFIC_SECRET_0";
  124. break;
  125. case EARLY_EXPORTER_SECRET:
  126. label = "EARLY_EXPORTER_SECRET";
  127. break;
  128. case EXPORTER_SECRET:
  129. label = "EXPORTER_SECRET";
  130. break;
  131. default:
  132. return 0;
  133. }
  134. if(SSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE) == 0) {
  135. /* Should never happen as wolfSSL_KeepArrays() was called before. */
  136. return 0;
  137. }
  138. Curl_tls_keylog_write(label, client_random, secret, secretSz);
  139. return 0;
  140. }
  141. #endif /* defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13) */
  142. static void
  143. wolfssl_log_tls12_secret(SSL *ssl)
  144. {
  145. unsigned char *ms, *sr, *cr;
  146. unsigned int msLen, srLen, crLen, i, x = 0;
  147. #if LIBWOLFSSL_VERSION_HEX >= 0x0300d000 /* >= 3.13.0 */
  148. /* wolfSSL_GetVersion is available since 3.13, we use it instead of
  149. * SSL_version since the latter relies on OPENSSL_ALL (--enable-opensslall or
  150. * --enable-all). Failing to perform this check could result in an unusable
  151. * key log line when TLS 1.3 is actually negotiated. */
  152. switch(wolfSSL_GetVersion(ssl)) {
  153. case WOLFSSL_SSLV3:
  154. case WOLFSSL_TLSV1:
  155. case WOLFSSL_TLSV1_1:
  156. case WOLFSSL_TLSV1_2:
  157. break;
  158. default:
  159. /* TLS 1.3 does not use this mechanism, the "master secret" returned below
  160. * is not directly usable. */
  161. return;
  162. }
  163. #endif
  164. if(wolfSSL_get_keys(ssl, &ms, &msLen, &sr, &srLen, &cr, &crLen) !=
  165. SSL_SUCCESS) {
  166. return;
  167. }
  168. /* Check for a missing master secret and skip logging. That can happen if
  169. * curl rejects the server certificate and aborts the handshake.
  170. */
  171. for(i = 0; i < msLen; i++) {
  172. x |= ms[i];
  173. }
  174. if(x == 0) {
  175. return;
  176. }
  177. Curl_tls_keylog_write("CLIENT_RANDOM", cr, ms, msLen);
  178. }
  179. #endif /* OPENSSL_EXTRA */
  180. static int do_file_type(const char *type)
  181. {
  182. if(!type || !type[0])
  183. return SSL_FILETYPE_PEM;
  184. if(strcasecompare(type, "PEM"))
  185. return SSL_FILETYPE_PEM;
  186. if(strcasecompare(type, "DER"))
  187. return SSL_FILETYPE_ASN1;
  188. return -1;
  189. }
  190. #ifdef WOLFSSL_HAVE_KYBER
  191. struct group_name_map {
  192. const word16 group;
  193. const char *name;
  194. };
  195. static const struct group_name_map gnm[] = {
  196. { WOLFSSL_KYBER_LEVEL1, "KYBER_LEVEL1" },
  197. { WOLFSSL_KYBER_LEVEL3, "KYBER_LEVEL3" },
  198. { WOLFSSL_KYBER_LEVEL5, "KYBER_LEVEL5" },
  199. { WOLFSSL_P256_KYBER_LEVEL1, "P256_KYBER_LEVEL1" },
  200. { WOLFSSL_P384_KYBER_LEVEL3, "P384_KYBER_LEVEL3" },
  201. { WOLFSSL_P521_KYBER_LEVEL5, "P521_KYBER_LEVEL5" },
  202. { 0, NULL }
  203. };
  204. #endif
  205. #ifdef USE_BIO_CHAIN
  206. static int wolfssl_bio_cf_create(WOLFSSL_BIO *bio)
  207. {
  208. wolfSSL_BIO_set_shutdown(bio, 1);
  209. wolfSSL_BIO_set_data(bio, NULL);
  210. return 1;
  211. }
  212. static int wolfssl_bio_cf_destroy(WOLFSSL_BIO *bio)
  213. {
  214. if(!bio)
  215. return 0;
  216. return 1;
  217. }
  218. static long wolfssl_bio_cf_ctrl(WOLFSSL_BIO *bio, int cmd, long num, void *ptr)
  219. {
  220. struct Curl_cfilter *cf = BIO_get_data(bio);
  221. long ret = 1;
  222. (void)cf;
  223. (void)ptr;
  224. switch(cmd) {
  225. case BIO_CTRL_GET_CLOSE:
  226. ret = (long)wolfSSL_BIO_get_shutdown(bio);
  227. break;
  228. case BIO_CTRL_SET_CLOSE:
  229. wolfSSL_BIO_set_shutdown(bio, (int)num);
  230. break;
  231. case BIO_CTRL_FLUSH:
  232. /* we do no delayed writes, but if we ever would, this
  233. * needs to trigger it. */
  234. ret = 1;
  235. break;
  236. case BIO_CTRL_DUP:
  237. ret = 1;
  238. break;
  239. #ifdef BIO_CTRL_EOF
  240. case BIO_CTRL_EOF:
  241. /* EOF has been reached on input? */
  242. return (!cf->next || !cf->next->connected);
  243. #endif
  244. default:
  245. ret = 0;
  246. break;
  247. }
  248. return ret;
  249. }
  250. static int wolfssl_bio_cf_out_write(WOLFSSL_BIO *bio,
  251. const char *buf, int blen)
  252. {
  253. struct Curl_cfilter *cf = wolfSSL_BIO_get_data(bio);
  254. struct ssl_connect_data *connssl = cf->ctx;
  255. struct wolfssl_ctx *backend =
  256. (struct wolfssl_ctx *)connssl->backend;
  257. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  258. ssize_t nwritten;
  259. CURLcode result = CURLE_OK;
  260. DEBUGASSERT(data);
  261. nwritten = Curl_conn_cf_send(cf->next, data, buf, blen, &result);
  262. backend->io_result = result;
  263. CURL_TRC_CF(data, cf, "bio_write(len=%d) -> %zd, %d",
  264. blen, nwritten, result);
  265. wolfSSL_BIO_clear_retry_flags(bio);
  266. if(nwritten < 0 && CURLE_AGAIN == result)
  267. BIO_set_retry_write(bio);
  268. return (int)nwritten;
  269. }
  270. static int wolfssl_bio_cf_in_read(WOLFSSL_BIO *bio, char *buf, int blen)
  271. {
  272. struct Curl_cfilter *cf = wolfSSL_BIO_get_data(bio);
  273. struct ssl_connect_data *connssl = cf->ctx;
  274. struct wolfssl_ctx *backend =
  275. (struct wolfssl_ctx *)connssl->backend;
  276. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  277. ssize_t nread;
  278. CURLcode result = CURLE_OK;
  279. DEBUGASSERT(data);
  280. /* OpenSSL catches this case, so should we. */
  281. if(!buf)
  282. return 0;
  283. nread = Curl_conn_cf_recv(cf->next, data, buf, blen, &result);
  284. backend->io_result = result;
  285. CURL_TRC_CF(data, cf, "bio_read(len=%d) -> %zd, %d", blen, nread, result);
  286. wolfSSL_BIO_clear_retry_flags(bio);
  287. if(nread < 0 && CURLE_AGAIN == result)
  288. BIO_set_retry_read(bio);
  289. else if(nread == 0)
  290. connssl->peer_closed = TRUE;
  291. return (int)nread;
  292. }
  293. static WOLFSSL_BIO_METHOD *wolfssl_bio_cf_method = NULL;
  294. static void wolfssl_bio_cf_init_methods(void)
  295. {
  296. wolfssl_bio_cf_method = wolfSSL_BIO_meth_new(BIO_TYPE_MEM, "wolfSSL CF BIO");
  297. wolfSSL_BIO_meth_set_write(wolfssl_bio_cf_method, &wolfssl_bio_cf_out_write);
  298. wolfSSL_BIO_meth_set_read(wolfssl_bio_cf_method, &wolfssl_bio_cf_in_read);
  299. wolfSSL_BIO_meth_set_ctrl(wolfssl_bio_cf_method, &wolfssl_bio_cf_ctrl);
  300. wolfSSL_BIO_meth_set_create(wolfssl_bio_cf_method, &wolfssl_bio_cf_create);
  301. wolfSSL_BIO_meth_set_destroy(wolfssl_bio_cf_method, &wolfssl_bio_cf_destroy);
  302. }
  303. static void wolfssl_bio_cf_free_methods(void)
  304. {
  305. wolfSSL_BIO_meth_free(wolfssl_bio_cf_method);
  306. }
  307. #else /* USE_BIO_CHAIN */
  308. #define wolfssl_bio_cf_init_methods() Curl_nop_stmt
  309. #define wolfssl_bio_cf_free_methods() Curl_nop_stmt
  310. #endif /* !USE_BIO_CHAIN */
  311. static CURLcode populate_x509_store(struct Curl_cfilter *cf,
  312. struct Curl_easy *data,
  313. X509_STORE *store,
  314. struct wolfssl_ctx *wssl)
  315. {
  316. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  317. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  318. const char * const ssl_cafile =
  319. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  320. (ca_info_blob ? NULL : conn_config->CAfile);
  321. const char * const ssl_capath = conn_config->CApath;
  322. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  323. bool imported_native_ca = false;
  324. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SYS_CA_CERTS)
  325. /* load native CA certificates */
  326. if(ssl_config->native_ca_store) {
  327. if(wolfSSL_CTX_load_system_CA_certs(wssl->ctx) != WOLFSSL_SUCCESS) {
  328. infof(data, "error importing native CA store, continuing anyway");
  329. }
  330. else {
  331. imported_native_ca = true;
  332. infof(data, "successfully imported native CA store");
  333. wssl->x509_store_setup = TRUE;
  334. }
  335. }
  336. #endif /* !NO_FILESYSTEM */
  337. /* load certificate blob */
  338. if(ca_info_blob) {
  339. if(wolfSSL_CTX_load_verify_buffer(wssl->ctx, ca_info_blob->data,
  340. (long)ca_info_blob->len,
  341. SSL_FILETYPE_PEM) != SSL_SUCCESS) {
  342. if(imported_native_ca) {
  343. infof(data, "error importing CA certificate blob, continuing anyway");
  344. }
  345. else {
  346. failf(data, "error importing CA certificate blob");
  347. return CURLE_SSL_CACERT_BADFILE;
  348. }
  349. }
  350. else {
  351. infof(data, "successfully imported CA certificate blob");
  352. wssl->x509_store_setup = TRUE;
  353. }
  354. }
  355. #ifndef NO_FILESYSTEM
  356. /* load trusted cacert from file if not blob */
  357. CURL_TRC_CF(data, cf, "populate_x509_store, path=%s, blob=%d",
  358. ssl_cafile? ssl_cafile : "none", !!ca_info_blob);
  359. if(!store)
  360. return CURLE_OUT_OF_MEMORY;
  361. if((ssl_cafile || ssl_capath) && (!wssl->x509_store_setup)) {
  362. int rc =
  363. wolfSSL_CTX_load_verify_locations_ex(wssl->ctx,
  364. ssl_cafile,
  365. ssl_capath,
  366. WOLFSSL_LOAD_FLAG_IGNORE_ERR);
  367. if(SSL_SUCCESS != rc) {
  368. if(conn_config->verifypeer) {
  369. /* Fail if we insist on successfully verifying the server. */
  370. failf(data, "error setting certificate verify locations:"
  371. " CAfile: %s CApath: %s",
  372. ssl_cafile ? ssl_cafile : "none",
  373. ssl_capath ? ssl_capath : "none");
  374. return CURLE_SSL_CACERT_BADFILE;
  375. }
  376. else {
  377. /* Just continue with a warning if no strict certificate
  378. verification is required. */
  379. infof(data, "error setting certificate verify locations,"
  380. " continuing anyway:");
  381. }
  382. }
  383. else {
  384. /* Everything is fine. */
  385. infof(data, "successfully set certificate verify locations:");
  386. }
  387. infof(data, " CAfile: %s", ssl_cafile ? ssl_cafile : "none");
  388. infof(data, " CApath: %s", ssl_capath ? ssl_capath : "none");
  389. }
  390. #endif
  391. (void)store;
  392. wssl->x509_store_setup = TRUE;
  393. return CURLE_OK;
  394. }
  395. /* key to use at `multi->proto_hash` */
  396. #define MPROTO_WSSL_X509_KEY "tls:wssl:x509:share"
  397. struct wssl_x509_share {
  398. char *CAfile; /* CAfile path used to generate X509 store */
  399. WOLFSSL_X509_STORE *store; /* cached X509 store or NULL if none */
  400. struct curltime time; /* when the cached store was created */
  401. };
  402. static void wssl_x509_share_free(void *key, size_t key_len, void *p)
  403. {
  404. struct wssl_x509_share *share = p;
  405. DEBUGASSERT(key_len == (sizeof(MPROTO_WSSL_X509_KEY)-1));
  406. DEBUGASSERT(!memcmp(MPROTO_WSSL_X509_KEY, key, key_len));
  407. (void)key;
  408. (void)key_len;
  409. if(share->store) {
  410. wolfSSL_X509_STORE_free(share->store);
  411. }
  412. free(share->CAfile);
  413. free(share);
  414. }
  415. static bool
  416. cached_x509_store_expired(const struct Curl_easy *data,
  417. const struct wssl_x509_share *mb)
  418. {
  419. const struct ssl_general_config *cfg = &data->set.general_ssl;
  420. struct curltime now = Curl_now();
  421. timediff_t elapsed_ms = Curl_timediff(now, mb->time);
  422. timediff_t timeout_ms = cfg->ca_cache_timeout * (timediff_t)1000;
  423. if(timeout_ms < 0)
  424. return false;
  425. return elapsed_ms >= timeout_ms;
  426. }
  427. static bool
  428. cached_x509_store_different(struct Curl_cfilter *cf,
  429. const struct wssl_x509_share *mb)
  430. {
  431. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  432. if(!mb->CAfile || !conn_config->CAfile)
  433. return mb->CAfile != conn_config->CAfile;
  434. return strcmp(mb->CAfile, conn_config->CAfile);
  435. }
  436. static X509_STORE *get_cached_x509_store(struct Curl_cfilter *cf,
  437. const struct Curl_easy *data)
  438. {
  439. struct Curl_multi *multi = data->multi;
  440. struct wssl_x509_share *share;
  441. WOLFSSL_X509_STORE *store = NULL;
  442. DEBUGASSERT(multi);
  443. share = multi? Curl_hash_pick(&multi->proto_hash,
  444. (void *)MPROTO_WSSL_X509_KEY,
  445. sizeof(MPROTO_WSSL_X509_KEY)-1) : NULL;
  446. if(share && share->store &&
  447. !cached_x509_store_expired(data, share) &&
  448. !cached_x509_store_different(cf, share)) {
  449. store = share->store;
  450. }
  451. return store;
  452. }
  453. static void set_cached_x509_store(struct Curl_cfilter *cf,
  454. const struct Curl_easy *data,
  455. X509_STORE *store)
  456. {
  457. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  458. struct Curl_multi *multi = data->multi;
  459. struct wssl_x509_share *share;
  460. DEBUGASSERT(multi);
  461. if(!multi)
  462. return;
  463. share = Curl_hash_pick(&multi->proto_hash,
  464. (void *)MPROTO_WSSL_X509_KEY,
  465. sizeof(MPROTO_WSSL_X509_KEY)-1);
  466. if(!share) {
  467. share = calloc(1, sizeof(*share));
  468. if(!share)
  469. return;
  470. if(!Curl_hash_add2(&multi->proto_hash,
  471. (void *)MPROTO_WSSL_X509_KEY,
  472. sizeof(MPROTO_WSSL_X509_KEY)-1,
  473. share, wssl_x509_share_free)) {
  474. free(share);
  475. return;
  476. }
  477. }
  478. if(wolfSSL_X509_STORE_up_ref(store)) {
  479. char *CAfile = NULL;
  480. if(conn_config->CAfile) {
  481. CAfile = strdup(conn_config->CAfile);
  482. if(!CAfile) {
  483. X509_STORE_free(store);
  484. return;
  485. }
  486. }
  487. if(share->store) {
  488. X509_STORE_free(share->store);
  489. free(share->CAfile);
  490. }
  491. share->time = Curl_now();
  492. share->store = store;
  493. share->CAfile = CAfile;
  494. }
  495. }
  496. CURLcode Curl_wssl_setup_x509_store(struct Curl_cfilter *cf,
  497. struct Curl_easy *data,
  498. struct wolfssl_ctx *wssl)
  499. {
  500. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  501. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  502. CURLcode result = CURLE_OK;
  503. WOLFSSL_X509_STORE *cached_store;
  504. bool cache_criteria_met;
  505. /* Consider the X509 store cacheable if it comes exclusively from a CAfile,
  506. or no source is provided and we are falling back to wolfSSL's built-in
  507. default. */
  508. cache_criteria_met = (data->set.general_ssl.ca_cache_timeout != 0) &&
  509. conn_config->verifypeer &&
  510. !conn_config->CApath &&
  511. !conn_config->ca_info_blob &&
  512. !ssl_config->primary.CRLfile &&
  513. !ssl_config->native_ca_store;
  514. cached_store = cache_criteria_met ? get_cached_x509_store(cf, data) : NULL;
  515. if(cached_store && wolfSSL_X509_STORE_up_ref(cached_store)) {
  516. wolfSSL_CTX_set_cert_store(wssl->ctx, cached_store);
  517. }
  518. else if(cache_criteria_met) {
  519. /* wolfSSL's initial store in CTX is not shareable by default.
  520. * Make a new one, suitable for adding to the cache. See #14278 */
  521. X509_STORE *store = wolfSSL_X509_STORE_new();
  522. if(!store) {
  523. failf(data, "SSL: could not create a X509 store");
  524. return CURLE_OUT_OF_MEMORY;
  525. }
  526. wolfSSL_CTX_set_cert_store(wssl->ctx, store);
  527. result = populate_x509_store(cf, data, store, wssl);
  528. if(!result) {
  529. set_cached_x509_store(cf, data, store);
  530. }
  531. }
  532. else {
  533. /* We never share the CTX's store, use it. */
  534. X509_STORE *store = wolfSSL_CTX_get_cert_store(wssl->ctx);
  535. result = populate_x509_store(cf, data, store, wssl);
  536. }
  537. return result;
  538. }
  539. /*
  540. * This function loads all the client/CA certificates and CRLs. Setup the TLS
  541. * layer and do all necessary magic.
  542. */
  543. static CURLcode
  544. wolfssl_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
  545. {
  546. char *ciphers, *curves;
  547. struct ssl_connect_data *connssl = cf->ctx;
  548. struct wolfssl_ctx *backend =
  549. (struct wolfssl_ctx *)connssl->backend;
  550. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  551. const struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  552. WOLFSSL_METHOD* req_method = NULL;
  553. #ifdef WOLFSSL_HAVE_KYBER
  554. word16 pqkem = 0;
  555. size_t idx = 0;
  556. #endif
  557. #ifdef HAVE_SNI
  558. bool sni = FALSE;
  559. #define use_sni(x) sni = (x)
  560. #else
  561. #define use_sni(x) Curl_nop_stmt
  562. #endif
  563. DEBUGASSERT(backend);
  564. if(connssl->state == ssl_connection_complete)
  565. return CURLE_OK;
  566. if(conn_config->version_max != CURL_SSLVERSION_MAX_NONE) {
  567. failf(data, "wolfSSL does not support to set maximum SSL/TLS version");
  568. return CURLE_SSL_CONNECT_ERROR;
  569. }
  570. /* check to see if we have been told to use an explicit SSL/TLS version */
  571. switch(conn_config->version) {
  572. case CURL_SSLVERSION_DEFAULT:
  573. case CURL_SSLVERSION_TLSv1:
  574. #if LIBWOLFSSL_VERSION_HEX >= 0x03003000 /* >= 3.3.0 */
  575. /* minimum protocol version is set later after the CTX object is created */
  576. req_method = SSLv23_client_method();
  577. #else
  578. infof(data, "wolfSSL <3.3.0 cannot be configured to use TLS 1.0-1.2, "
  579. "TLS 1.0 is used exclusively");
  580. req_method = TLSv1_client_method();
  581. #endif
  582. use_sni(TRUE);
  583. break;
  584. case CURL_SSLVERSION_TLSv1_0:
  585. #if defined(WOLFSSL_ALLOW_TLSV10) && !defined(NO_OLD_TLS)
  586. req_method = TLSv1_client_method();
  587. use_sni(TRUE);
  588. break;
  589. #else
  590. failf(data, "wolfSSL does not support TLS 1.0");
  591. return CURLE_NOT_BUILT_IN;
  592. #endif
  593. case CURL_SSLVERSION_TLSv1_1:
  594. #ifndef NO_OLD_TLS
  595. req_method = TLSv1_1_client_method();
  596. use_sni(TRUE);
  597. break;
  598. #else
  599. failf(data, "wolfSSL does not support TLS 1.1");
  600. return CURLE_NOT_BUILT_IN;
  601. #endif
  602. case CURL_SSLVERSION_TLSv1_2:
  603. #ifndef WOLFSSL_NO_TLS12
  604. req_method = TLSv1_2_client_method();
  605. use_sni(TRUE);
  606. #else
  607. failf(data, "wolfSSL does not support TLS 1.2");
  608. return CURLE_NOT_BUILT_IN;
  609. #endif
  610. break;
  611. case CURL_SSLVERSION_TLSv1_3:
  612. #ifdef WOLFSSL_TLS13
  613. req_method = wolfTLSv1_3_client_method();
  614. use_sni(TRUE);
  615. break;
  616. #else
  617. failf(data, "wolfSSL: TLS 1.3 is not yet supported");
  618. return CURLE_SSL_CONNECT_ERROR;
  619. #endif
  620. default:
  621. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  622. return CURLE_SSL_CONNECT_ERROR;
  623. }
  624. if(!req_method) {
  625. failf(data, "SSL: could not create a method");
  626. return CURLE_OUT_OF_MEMORY;
  627. }
  628. if(backend->ctx)
  629. wolfSSL_CTX_free(backend->ctx);
  630. backend->ctx = wolfSSL_CTX_new(req_method);
  631. if(!backend->ctx) {
  632. failf(data, "SSL: could not create a context");
  633. return CURLE_OUT_OF_MEMORY;
  634. }
  635. switch(conn_config->version) {
  636. case CURL_SSLVERSION_DEFAULT:
  637. case CURL_SSLVERSION_TLSv1:
  638. #if LIBWOLFSSL_VERSION_HEX > 0x03004006 /* > 3.4.6 */
  639. /* Versions 3.3.0 to 3.4.6 we know the minimum protocol version is
  640. * whatever minimum version of TLS was built in and at least TLS 1.0. For
  641. * later library versions that could change (eg TLS 1.0 built in but
  642. * defaults to TLS 1.1) so we have this short circuit evaluation to find
  643. * the minimum supported TLS version.
  644. */
  645. if((wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1) != 1) &&
  646. (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_1) != 1) &&
  647. (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_2) != 1)
  648. #ifdef WOLFSSL_TLS13
  649. && (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_3) != 1)
  650. #endif
  651. ) {
  652. failf(data, "SSL: could not set the minimum protocol version");
  653. return CURLE_SSL_CONNECT_ERROR;
  654. }
  655. #endif
  656. FALLTHROUGH();
  657. default:
  658. break;
  659. }
  660. ciphers = conn_config->cipher_list;
  661. if(ciphers) {
  662. if(!SSL_CTX_set_cipher_list(backend->ctx, ciphers)) {
  663. failf(data, "failed setting cipher list: %s", ciphers);
  664. return CURLE_SSL_CIPHER;
  665. }
  666. infof(data, "Cipher selection: %s", ciphers);
  667. }
  668. curves = conn_config->curves;
  669. if(curves) {
  670. #ifdef WOLFSSL_HAVE_KYBER
  671. for(idx = 0; gnm[idx].name != NULL; idx++) {
  672. if(strncmp(curves, gnm[idx].name, strlen(gnm[idx].name)) == 0) {
  673. pqkem = gnm[idx].group;
  674. break;
  675. }
  676. }
  677. if(pqkem == 0)
  678. #endif
  679. {
  680. if(!SSL_CTX_set1_curves_list(backend->ctx, curves)) {
  681. failf(data, "failed setting curves list: '%s'", curves);
  682. return CURLE_SSL_CIPHER;
  683. }
  684. }
  685. }
  686. #ifndef NO_FILESYSTEM
  687. /* Load the client certificate, and private key */
  688. if(ssl_config->primary.clientcert) {
  689. char *key_file = ssl_config->key;
  690. int file_type = do_file_type(ssl_config->cert_type);
  691. if(file_type == WOLFSSL_FILETYPE_PEM) {
  692. if(wolfSSL_CTX_use_certificate_chain_file(backend->ctx,
  693. ssl_config->primary.clientcert)
  694. != 1) {
  695. failf(data, "unable to use client certificate");
  696. return CURLE_SSL_CONNECT_ERROR;
  697. }
  698. }
  699. else if(file_type == WOLFSSL_FILETYPE_ASN1) {
  700. if(wolfSSL_CTX_use_certificate_file(backend->ctx,
  701. ssl_config->primary.clientcert,
  702. file_type) != 1) {
  703. failf(data, "unable to use client certificate");
  704. return CURLE_SSL_CONNECT_ERROR;
  705. }
  706. }
  707. else {
  708. failf(data, "unknown cert type");
  709. return CURLE_BAD_FUNCTION_ARGUMENT;
  710. }
  711. if(!key_file)
  712. key_file = ssl_config->primary.clientcert;
  713. else
  714. file_type = do_file_type(ssl_config->key_type);
  715. if(wolfSSL_CTX_use_PrivateKey_file(backend->ctx, key_file,
  716. file_type) != 1) {
  717. failf(data, "unable to set private key");
  718. return CURLE_SSL_CONNECT_ERROR;
  719. }
  720. }
  721. #endif /* !NO_FILESYSTEM */
  722. /* SSL always tries to verify the peer, this only says whether it should
  723. * fail to connect if the verification fails, or if it should continue
  724. * anyway. In the latter case the result of the verification is checked with
  725. * SSL_get_verify_result() below. */
  726. wolfSSL_CTX_set_verify(backend->ctx,
  727. conn_config->verifypeer?SSL_VERIFY_PEER:
  728. SSL_VERIFY_NONE, NULL);
  729. #ifdef HAVE_SNI
  730. if(sni && connssl->peer.sni) {
  731. size_t sni_len = strlen(connssl->peer.sni);
  732. if((sni_len < USHRT_MAX)) {
  733. if(wolfSSL_CTX_UseSNI(backend->ctx, WOLFSSL_SNI_HOST_NAME,
  734. connssl->peer.sni,
  735. (unsigned short)sni_len) != 1) {
  736. failf(data, "Failed to set SNI");
  737. return CURLE_SSL_CONNECT_ERROR;
  738. }
  739. }
  740. }
  741. #endif
  742. /* give application a chance to interfere with SSL set up. */
  743. if(data->set.ssl.fsslctx) {
  744. CURLcode result;
  745. if(!backend->x509_store_setup) {
  746. result = Curl_wssl_setup_x509_store(cf, data, backend);
  747. if(result)
  748. return result;
  749. }
  750. result = (*data->set.ssl.fsslctx)(data, backend->ctx,
  751. data->set.ssl.fsslctxp);
  752. if(result) {
  753. failf(data, "error signaled by ssl ctx callback");
  754. return result;
  755. }
  756. }
  757. #ifdef NO_FILESYSTEM
  758. else if(conn_config->verifypeer) {
  759. failf(data, "SSL: Certificates cannot be loaded because wolfSSL was built"
  760. " with \"no filesystem\". Either disable peer verification"
  761. " (insecure) or if you are building an application with libcurl you"
  762. " can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
  763. return CURLE_SSL_CONNECT_ERROR;
  764. }
  765. #endif
  766. /* Let's make an SSL structure */
  767. if(backend->handle)
  768. wolfSSL_free(backend->handle);
  769. backend->handle = wolfSSL_new(backend->ctx);
  770. if(!backend->handle) {
  771. failf(data, "SSL: could not create a handle");
  772. return CURLE_OUT_OF_MEMORY;
  773. }
  774. #ifdef WOLFSSL_HAVE_KYBER
  775. if(pqkem) {
  776. if(wolfSSL_UseKeyShare(backend->handle, pqkem) != WOLFSSL_SUCCESS) {
  777. failf(data, "unable to use PQ KEM");
  778. }
  779. }
  780. #endif
  781. #ifdef HAVE_ALPN
  782. if(connssl->alpn) {
  783. struct alpn_proto_buf proto;
  784. CURLcode result;
  785. result = Curl_alpn_to_proto_str(&proto, connssl->alpn);
  786. if(result ||
  787. wolfSSL_UseALPN(backend->handle,
  788. (char *)proto.data, (unsigned int)proto.len,
  789. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) != SSL_SUCCESS) {
  790. failf(data, "SSL: failed setting ALPN protocols");
  791. return CURLE_SSL_CONNECT_ERROR;
  792. }
  793. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  794. }
  795. #endif /* HAVE_ALPN */
  796. #ifdef OPENSSL_EXTRA
  797. if(Curl_tls_keylog_enabled()) {
  798. /* Ensure the Client Random is preserved. */
  799. wolfSSL_KeepArrays(backend->handle);
  800. #if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
  801. wolfSSL_set_tls13_secret_cb(backend->handle,
  802. wolfssl_tls13_secret_callback, NULL);
  803. #endif
  804. }
  805. #endif /* OPENSSL_EXTRA */
  806. #ifdef HAVE_SECURE_RENEGOTIATION
  807. if(wolfSSL_UseSecureRenegotiation(backend->handle) != SSL_SUCCESS) {
  808. failf(data, "SSL: failed setting secure renegotiation");
  809. return CURLE_SSL_CONNECT_ERROR;
  810. }
  811. #endif /* HAVE_SECURE_RENEGOTIATION */
  812. /* Check if there is a cached ID we can/should use here! */
  813. if(ssl_config->primary.cache_session) {
  814. void *ssl_sessionid = NULL;
  815. Curl_ssl_sessionid_lock(data);
  816. if(!Curl_ssl_getsessionid(cf, data, &connssl->peer,
  817. &ssl_sessionid, NULL)) {
  818. /* we got a session id, use it! */
  819. if(!SSL_set_session(backend->handle, ssl_sessionid)) {
  820. Curl_ssl_delsessionid(data, ssl_sessionid);
  821. infof(data, "cannot use session ID, going on without");
  822. }
  823. else
  824. infof(data, "SSL reusing session ID");
  825. }
  826. Curl_ssl_sessionid_unlock(data);
  827. }
  828. #ifdef USE_ECH
  829. if(ECH_ENABLED(data)) {
  830. int trying_ech_now = 0;
  831. if(data->set.str[STRING_ECH_PUBLIC]) {
  832. infof(data, "ECH: outername not (yet) supported with wolfSSL");
  833. return CURLE_SSL_CONNECT_ERROR;
  834. }
  835. if(data->set.tls_ech == CURLECH_GREASE) {
  836. infof(data, "ECH: GREASE'd ECH not yet supported for wolfSSL");
  837. return CURLE_SSL_CONNECT_ERROR;
  838. }
  839. if(data->set.tls_ech & CURLECH_CLA_CFG
  840. && data->set.str[STRING_ECH_CONFIG]) {
  841. char *b64val = data->set.str[STRING_ECH_CONFIG];
  842. word32 b64len = 0;
  843. b64len = (word32) strlen(b64val);
  844. if(b64len
  845. && wolfSSL_SetEchConfigsBase64(backend->handle, b64val, b64len)
  846. != WOLFSSL_SUCCESS) {
  847. if(data->set.tls_ech & CURLECH_HARD)
  848. return CURLE_SSL_CONNECT_ERROR;
  849. }
  850. else {
  851. trying_ech_now = 1;
  852. infof(data, "ECH: ECHConfig from command line");
  853. }
  854. }
  855. else {
  856. struct Curl_dns_entry *dns = NULL;
  857. dns = Curl_fetch_addr(data, connssl->peer.hostname, connssl->peer.port);
  858. if(!dns) {
  859. infof(data, "ECH: requested but no DNS info available");
  860. if(data->set.tls_ech & CURLECH_HARD)
  861. return CURLE_SSL_CONNECT_ERROR;
  862. }
  863. else {
  864. struct Curl_https_rrinfo *rinfo = NULL;
  865. rinfo = dns->hinfo;
  866. if(rinfo && rinfo->echconfiglist) {
  867. unsigned char *ecl = rinfo->echconfiglist;
  868. size_t elen = rinfo->echconfiglist_len;
  869. infof(data, "ECH: ECHConfig from DoH HTTPS RR");
  870. if(wolfSSL_SetEchConfigs(backend->handle, ecl, (word32) elen) !=
  871. WOLFSSL_SUCCESS) {
  872. infof(data, "ECH: wolfSSL_SetEchConfigs failed");
  873. if(data->set.tls_ech & CURLECH_HARD)
  874. return CURLE_SSL_CONNECT_ERROR;
  875. }
  876. else {
  877. trying_ech_now = 1;
  878. infof(data, "ECH: imported ECHConfigList of length %ld", elen);
  879. }
  880. }
  881. else {
  882. infof(data, "ECH: requested but no ECHConfig available");
  883. if(data->set.tls_ech & CURLECH_HARD)
  884. return CURLE_SSL_CONNECT_ERROR;
  885. }
  886. Curl_resolv_unlock(data, dns);
  887. }
  888. }
  889. if(trying_ech_now
  890. && SSL_set_min_proto_version(backend->handle, TLS1_3_VERSION) != 1) {
  891. infof(data, "ECH: cannot force TLSv1.3 [ERROR]");
  892. return CURLE_SSL_CONNECT_ERROR;
  893. }
  894. }
  895. #endif /* USE_ECH */
  896. #ifdef USE_BIO_CHAIN
  897. {
  898. WOLFSSL_BIO *bio;
  899. bio = BIO_new(wolfssl_bio_cf_method);
  900. if(!bio)
  901. return CURLE_OUT_OF_MEMORY;
  902. wolfSSL_BIO_set_data(bio, cf);
  903. wolfSSL_set_bio(backend->handle, bio, bio);
  904. }
  905. #else /* USE_BIO_CHAIN */
  906. /* pass the raw socket into the SSL layer */
  907. if(!wolfSSL_set_fd(backend->handle,
  908. (int)Curl_conn_cf_get_socket(cf, data))) {
  909. failf(data, "SSL: SSL_set_fd failed");
  910. return CURLE_SSL_CONNECT_ERROR;
  911. }
  912. #endif /* !USE_BIO_CHAIN */
  913. connssl->connecting_state = ssl_connect_2;
  914. return CURLE_OK;
  915. }
  916. static char *wolfssl_strerror(unsigned long error, char *buf,
  917. unsigned long size)
  918. {
  919. DEBUGASSERT(size);
  920. *buf = '\0';
  921. wolfSSL_ERR_error_string_n(error, buf, size);
  922. if(!*buf) {
  923. const char *msg = error ? "Unknown error" : "No error";
  924. strncpy(buf, msg, size - 1);
  925. buf[size - 1] = '\0';
  926. }
  927. return buf;
  928. }
  929. static CURLcode
  930. wolfssl_connect_step2(struct Curl_cfilter *cf, struct Curl_easy *data)
  931. {
  932. int ret = -1;
  933. struct ssl_connect_data *connssl = cf->ctx;
  934. struct wolfssl_ctx *backend =
  935. (struct wolfssl_ctx *)connssl->backend;
  936. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  937. #ifndef CURL_DISABLE_PROXY
  938. const char * const pinnedpubkey = Curl_ssl_cf_is_proxy(cf)?
  939. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
  940. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  941. #else
  942. const char * const pinnedpubkey = data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  943. #endif
  944. DEBUGASSERT(backend);
  945. wolfSSL_ERR_clear_error();
  946. /* Enable RFC2818 checks */
  947. if(conn_config->verifyhost) {
  948. char *snihost = connssl->peer.sni?
  949. connssl->peer.sni : connssl->peer.hostname;
  950. if(wolfSSL_check_domain_name(backend->handle, snihost) == SSL_FAILURE)
  951. return CURLE_SSL_CONNECT_ERROR;
  952. }
  953. if(!backend->x509_store_setup) {
  954. /* After having send off the ClientHello, we prepare the x509
  955. * store to verify the coming certificate from the server */
  956. CURLcode result;
  957. result = Curl_wssl_setup_x509_store(cf, data, backend);
  958. if(result)
  959. return result;
  960. }
  961. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  962. ret = wolfSSL_connect(backend->handle);
  963. #ifdef OPENSSL_EXTRA
  964. if(Curl_tls_keylog_enabled()) {
  965. /* If key logging is enabled, wait for the handshake to complete and then
  966. * proceed with logging secrets (for TLS 1.2 or older).
  967. *
  968. * During the handshake (ret==-1), wolfSSL_want_read() is true as it waits
  969. * for the server response. At that point the master secret is not yet
  970. * available, so we must not try to read it.
  971. * To log the secret on completion with a handshake failure, detect
  972. * completion via the observation that there is nothing to read or write.
  973. * Note that OpenSSL SSL_want_read() is always true here. If wolfSSL ever
  974. * changes, the worst case is that no key is logged on error.
  975. */
  976. if(ret == SSL_SUCCESS ||
  977. (!wolfSSL_want_read(backend->handle) &&
  978. !wolfSSL_want_write(backend->handle))) {
  979. wolfssl_log_tls12_secret(backend->handle);
  980. /* Client Random and master secrets are no longer needed, erase these.
  981. * Ignored while the handshake is still in progress. */
  982. wolfSSL_FreeArrays(backend->handle);
  983. }
  984. }
  985. #endif /* OPENSSL_EXTRA */
  986. if(ret != 1) {
  987. int detail = wolfSSL_get_error(backend->handle, ret);
  988. if(SSL_ERROR_WANT_READ == detail) {
  989. connssl->io_need = CURL_SSL_IO_NEED_RECV;
  990. return CURLE_OK;
  991. }
  992. else if(SSL_ERROR_WANT_WRITE == detail) {
  993. connssl->io_need = CURL_SSL_IO_NEED_SEND;
  994. return CURLE_OK;
  995. }
  996. /* There is no easy way to override only the CN matching.
  997. * This will enable the override of both mismatching SubjectAltNames
  998. * as also mismatching CN fields */
  999. else if(DOMAIN_NAME_MISMATCH == detail) {
  1000. #if 1
  1001. failf(data, " subject alt name(s) or common name do not match \"%s\"",
  1002. connssl->peer.dispname);
  1003. return CURLE_PEER_FAILED_VERIFICATION;
  1004. #else
  1005. /* When the wolfssl_check_domain_name() is used and you desire to
  1006. * continue on a DOMAIN_NAME_MISMATCH, i.e. 'ssl_config.verifyhost
  1007. * == 0', CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA
  1008. * error. The only way to do this is currently to switch the
  1009. * Wolfssl_check_domain_name() in and out based on the
  1010. * 'ssl_config.verifyhost' value. */
  1011. if(conn_config->verifyhost) {
  1012. failf(data,
  1013. " subject alt name(s) or common name do not match \"%s\"\n",
  1014. connssl->dispname);
  1015. return CURLE_PEER_FAILED_VERIFICATION;
  1016. }
  1017. else {
  1018. infof(data,
  1019. " subject alt name(s) and/or common name do not match \"%s\"",
  1020. connssl->dispname);
  1021. return CURLE_OK;
  1022. }
  1023. #endif
  1024. }
  1025. #if LIBWOLFSSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
  1026. else if(ASN_NO_SIGNER_E == detail) {
  1027. if(conn_config->verifypeer) {
  1028. failf(data, " CA signer not available for verification");
  1029. return CURLE_SSL_CACERT_BADFILE;
  1030. }
  1031. else {
  1032. /* Just continue with a warning if no strict certificate
  1033. verification is required. */
  1034. infof(data, "CA signer not available for verification, "
  1035. "continuing anyway");
  1036. }
  1037. }
  1038. #endif
  1039. #ifdef USE_ECH
  1040. else if(-1 == detail) {
  1041. /* try access a retry_config ECHConfigList for tracing */
  1042. byte echConfigs[1000];
  1043. word32 echConfigsLen = 1000;
  1044. int rv = 0;
  1045. /* this currently does not produce the retry_configs */
  1046. rv = wolfSSL_GetEchConfigs(backend->handle, echConfigs,
  1047. &echConfigsLen);
  1048. if(rv != WOLFSSL_SUCCESS) {
  1049. infof(data, "Failed to get ECHConfigs");
  1050. }
  1051. else {
  1052. char *b64str = NULL;
  1053. size_t blen = 0;
  1054. rv = Curl_base64_encode((const char *)echConfigs, echConfigsLen,
  1055. &b64str, &blen);
  1056. if(!rv && b64str)
  1057. infof(data, "ECH: (not yet) retry_configs %s", b64str);
  1058. free(b64str);
  1059. }
  1060. }
  1061. #endif
  1062. else if(backend->io_result == CURLE_AGAIN) {
  1063. return CURLE_OK;
  1064. }
  1065. else {
  1066. char error_buffer[256];
  1067. failf(data, "SSL_connect failed with error %d: %s", detail,
  1068. wolfssl_strerror((unsigned long)detail, error_buffer,
  1069. sizeof(error_buffer)));
  1070. return CURLE_SSL_CONNECT_ERROR;
  1071. }
  1072. }
  1073. if(pinnedpubkey) {
  1074. #ifdef KEEP_PEER_CERT
  1075. X509 *x509;
  1076. const char *x509_der;
  1077. int x509_der_len;
  1078. struct Curl_X509certificate x509_parsed;
  1079. struct Curl_asn1Element *pubkey;
  1080. CURLcode result;
  1081. x509 = wolfSSL_get_peer_certificate(backend->handle);
  1082. if(!x509) {
  1083. failf(data, "SSL: failed retrieving server certificate");
  1084. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  1085. }
  1086. x509_der = (const char *)wolfSSL_X509_get_der(x509, &x509_der_len);
  1087. if(!x509_der) {
  1088. failf(data, "SSL: failed retrieving ASN.1 server certificate");
  1089. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  1090. }
  1091. memset(&x509_parsed, 0, sizeof(x509_parsed));
  1092. if(Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len))
  1093. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  1094. pubkey = &x509_parsed.subjectPublicKeyInfo;
  1095. if(!pubkey->header || pubkey->end <= pubkey->header) {
  1096. failf(data, "SSL: failed retrieving public key from server certificate");
  1097. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  1098. }
  1099. result = Curl_pin_peer_pubkey(data,
  1100. pinnedpubkey,
  1101. (const unsigned char *)pubkey->header,
  1102. (size_t)(pubkey->end - pubkey->header));
  1103. wolfSSL_FreeX509(x509);
  1104. if(result) {
  1105. failf(data, "SSL: public key does not match pinned public key");
  1106. return result;
  1107. }
  1108. #else
  1109. failf(data, "Library lacks pinning support built-in");
  1110. return CURLE_NOT_BUILT_IN;
  1111. #endif
  1112. }
  1113. #ifdef HAVE_ALPN
  1114. if(connssl->alpn) {
  1115. int rc;
  1116. char *protocol = NULL;
  1117. unsigned short protocol_len = 0;
  1118. rc = wolfSSL_ALPN_GetProtocol(backend->handle, &protocol, &protocol_len);
  1119. if(rc == SSL_SUCCESS) {
  1120. Curl_alpn_set_negotiated(cf, data, (const unsigned char *)protocol,
  1121. protocol_len);
  1122. }
  1123. else if(rc == SSL_ALPN_NOT_FOUND)
  1124. Curl_alpn_set_negotiated(cf, data, NULL, 0);
  1125. else {
  1126. failf(data, "ALPN, failure getting protocol, error %d", rc);
  1127. return CURLE_SSL_CONNECT_ERROR;
  1128. }
  1129. }
  1130. #endif /* HAVE_ALPN */
  1131. connssl->connecting_state = ssl_connect_3;
  1132. #if (LIBWOLFSSL_VERSION_HEX >= 0x03009010)
  1133. infof(data, "SSL connection using %s / %s",
  1134. wolfSSL_get_version(backend->handle),
  1135. wolfSSL_get_cipher_name(backend->handle));
  1136. #else
  1137. infof(data, "SSL connected");
  1138. #endif
  1139. return CURLE_OK;
  1140. }
  1141. static void wolfssl_session_free(void *sessionid, size_t idsize)
  1142. {
  1143. (void)idsize;
  1144. wolfSSL_SESSION_free(sessionid);
  1145. }
  1146. static CURLcode
  1147. wolfssl_connect_step3(struct Curl_cfilter *cf, struct Curl_easy *data)
  1148. {
  1149. CURLcode result = CURLE_OK;
  1150. struct ssl_connect_data *connssl = cf->ctx;
  1151. struct wolfssl_ctx *backend =
  1152. (struct wolfssl_ctx *)connssl->backend;
  1153. const struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  1154. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  1155. DEBUGASSERT(backend);
  1156. if(ssl_config->primary.cache_session) {
  1157. /* wolfSSL_get1_session allocates memory that has to be freed. */
  1158. WOLFSSL_SESSION *our_ssl_sessionid = wolfSSL_get1_session(backend->handle);
  1159. if(our_ssl_sessionid) {
  1160. Curl_ssl_sessionid_lock(data);
  1161. /* call takes ownership of `our_ssl_sessionid` */
  1162. result = Curl_ssl_set_sessionid(cf, data, &connssl->peer,
  1163. our_ssl_sessionid, 0,
  1164. wolfssl_session_free);
  1165. Curl_ssl_sessionid_unlock(data);
  1166. if(result) {
  1167. failf(data, "failed to store ssl session");
  1168. return result;
  1169. }
  1170. }
  1171. }
  1172. connssl->connecting_state = ssl_connect_done;
  1173. return result;
  1174. }
  1175. static ssize_t wolfssl_send(struct Curl_cfilter *cf,
  1176. struct Curl_easy *data,
  1177. const void *mem,
  1178. size_t len,
  1179. CURLcode *curlcode)
  1180. {
  1181. struct ssl_connect_data *connssl = cf->ctx;
  1182. struct wolfssl_ctx *backend =
  1183. (struct wolfssl_ctx *)connssl->backend;
  1184. int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  1185. int rc;
  1186. DEBUGASSERT(backend);
  1187. wolfSSL_ERR_clear_error();
  1188. rc = wolfSSL_write(backend->handle, mem, memlen);
  1189. if(rc <= 0) {
  1190. int err = wolfSSL_get_error(backend->handle, rc);
  1191. switch(err) {
  1192. case SSL_ERROR_WANT_READ:
  1193. case SSL_ERROR_WANT_WRITE:
  1194. /* there is data pending, re-invoke SSL_write() */
  1195. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> AGAIN", len);
  1196. *curlcode = CURLE_AGAIN;
  1197. return -1;
  1198. default:
  1199. if(backend->io_result == CURLE_AGAIN) {
  1200. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> AGAIN", len);
  1201. *curlcode = CURLE_AGAIN;
  1202. return -1;
  1203. }
  1204. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> %d, %d", len, rc, err);
  1205. {
  1206. char error_buffer[256];
  1207. failf(data, "SSL write: %s, errno %d",
  1208. wolfssl_strerror((unsigned long)err, error_buffer,
  1209. sizeof(error_buffer)),
  1210. SOCKERRNO);
  1211. }
  1212. *curlcode = CURLE_SEND_ERROR;
  1213. return -1;
  1214. }
  1215. }
  1216. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> %d", len, rc);
  1217. return rc;
  1218. }
  1219. static CURLcode wolfssl_shutdown(struct Curl_cfilter *cf,
  1220. struct Curl_easy *data,
  1221. bool send_shutdown, bool *done)
  1222. {
  1223. struct ssl_connect_data *connssl = cf->ctx;
  1224. struct wolfssl_ctx *wctx = (struct wolfssl_ctx *)connssl->backend;
  1225. CURLcode result = CURLE_OK;
  1226. char buf[1024];
  1227. int nread, err;
  1228. DEBUGASSERT(wctx);
  1229. if(!wctx->handle || cf->shutdown) {
  1230. *done = TRUE;
  1231. goto out;
  1232. }
  1233. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  1234. *done = FALSE;
  1235. if(!(wolfSSL_get_shutdown(wctx->handle) & SSL_SENT_SHUTDOWN)) {
  1236. /* We have not started the shutdown from our side yet. Check
  1237. * if the server already sent us one. */
  1238. ERR_clear_error();
  1239. nread = wolfSSL_read(wctx->handle, buf, (int)sizeof(buf));
  1240. err = wolfSSL_get_error(wctx->handle, nread);
  1241. if(!nread && err == SSL_ERROR_ZERO_RETURN) {
  1242. bool input_pending;
  1243. /* Yes, it did. */
  1244. if(!send_shutdown) {
  1245. CURL_TRC_CF(data, cf, "SSL shutdown received, not sending");
  1246. *done = TRUE;
  1247. goto out;
  1248. }
  1249. else if(!cf->next->cft->is_alive(cf->next, data, &input_pending)) {
  1250. /* Server closed the connection after its closy notify. It
  1251. * seems not interested to see our close notify, so do not
  1252. * send it. We are done. */
  1253. CURL_TRC_CF(data, cf, "peer closed connection");
  1254. connssl->peer_closed = TRUE;
  1255. *done = TRUE;
  1256. goto out;
  1257. }
  1258. }
  1259. }
  1260. if(send_shutdown && wolfSSL_shutdown(wctx->handle) == 1) {
  1261. CURL_TRC_CF(data, cf, "SSL shutdown finished");
  1262. *done = TRUE;
  1263. goto out;
  1264. }
  1265. else {
  1266. size_t i;
  1267. /* SSL should now have started the shutdown from our side. Since it
  1268. * was not complete, we are lacking the close notify from the server. */
  1269. for(i = 0; i < 10; ++i) {
  1270. ERR_clear_error();
  1271. nread = wolfSSL_read(wctx->handle, buf, (int)sizeof(buf));
  1272. if(nread <= 0)
  1273. break;
  1274. }
  1275. err = wolfSSL_get_error(wctx->handle, nread);
  1276. switch(err) {
  1277. case SSL_ERROR_ZERO_RETURN: /* no more data */
  1278. CURL_TRC_CF(data, cf, "SSL shutdown received");
  1279. *done = TRUE;
  1280. break;
  1281. case SSL_ERROR_NONE: /* just did not get anything */
  1282. case SSL_ERROR_WANT_READ:
  1283. /* SSL has send its notify and now wants to read the reply
  1284. * from the server. We are not really interested in that. */
  1285. CURL_TRC_CF(data, cf, "SSL shutdown sent, want receive");
  1286. connssl->io_need = CURL_SSL_IO_NEED_RECV;
  1287. break;
  1288. case SSL_ERROR_WANT_WRITE:
  1289. CURL_TRC_CF(data, cf, "SSL shutdown send blocked");
  1290. connssl->io_need = CURL_SSL_IO_NEED_SEND;
  1291. break;
  1292. default: {
  1293. char error_buffer[256];
  1294. int detail = wolfSSL_get_error(wctx->handle, err);
  1295. CURL_TRC_CF(data, cf, "SSL shutdown, error: '%s'(%d)",
  1296. wolfssl_strerror((unsigned long)err, error_buffer,
  1297. sizeof(error_buffer)),
  1298. detail);
  1299. result = CURLE_RECV_ERROR;
  1300. break;
  1301. }
  1302. }
  1303. }
  1304. out:
  1305. cf->shutdown = (result || *done);
  1306. return result;
  1307. }
  1308. static void wolfssl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1309. {
  1310. struct ssl_connect_data *connssl = cf->ctx;
  1311. struct wolfssl_ctx *backend =
  1312. (struct wolfssl_ctx *)connssl->backend;
  1313. (void) data;
  1314. DEBUGASSERT(backend);
  1315. if(backend->handle) {
  1316. wolfSSL_free(backend->handle);
  1317. backend->handle = NULL;
  1318. }
  1319. if(backend->ctx) {
  1320. wolfSSL_CTX_free(backend->ctx);
  1321. backend->ctx = NULL;
  1322. }
  1323. }
  1324. static ssize_t wolfssl_recv(struct Curl_cfilter *cf,
  1325. struct Curl_easy *data,
  1326. char *buf, size_t blen,
  1327. CURLcode *curlcode)
  1328. {
  1329. struct ssl_connect_data *connssl = cf->ctx;
  1330. struct wolfssl_ctx *backend =
  1331. (struct wolfssl_ctx *)connssl->backend;
  1332. int buffsize = (blen > (size_t)INT_MAX) ? INT_MAX : (int)blen;
  1333. int nread;
  1334. DEBUGASSERT(backend);
  1335. wolfSSL_ERR_clear_error();
  1336. *curlcode = CURLE_OK;
  1337. nread = wolfSSL_read(backend->handle, buf, buffsize);
  1338. if(nread <= 0) {
  1339. int err = wolfSSL_get_error(backend->handle, nread);
  1340. switch(err) {
  1341. case SSL_ERROR_ZERO_RETURN: /* no more data */
  1342. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> CLOSED", blen);
  1343. *curlcode = CURLE_OK;
  1344. return 0;
  1345. case SSL_ERROR_NONE:
  1346. case SSL_ERROR_WANT_READ:
  1347. case SSL_ERROR_WANT_WRITE:
  1348. /* there is data pending, re-invoke wolfSSL_read() */
  1349. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> AGAIN", blen);
  1350. *curlcode = CURLE_AGAIN;
  1351. return -1;
  1352. default:
  1353. if(backend->io_result == CURLE_AGAIN) {
  1354. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> AGAIN", blen);
  1355. *curlcode = CURLE_AGAIN;
  1356. return -1;
  1357. }
  1358. {
  1359. char error_buffer[256];
  1360. failf(data, "SSL read: %s, errno %d",
  1361. wolfssl_strerror((unsigned long)err, error_buffer,
  1362. sizeof(error_buffer)),
  1363. SOCKERRNO);
  1364. }
  1365. *curlcode = CURLE_RECV_ERROR;
  1366. return -1;
  1367. }
  1368. }
  1369. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> %d", blen, nread);
  1370. return nread;
  1371. }
  1372. static size_t wolfssl_version(char *buffer, size_t size)
  1373. {
  1374. #if LIBWOLFSSL_VERSION_HEX >= 0x03006000
  1375. return msnprintf(buffer, size, "wolfSSL/%s", wolfSSL_lib_version());
  1376. #elif defined(WOLFSSL_VERSION)
  1377. return msnprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
  1378. #endif
  1379. }
  1380. static int wolfssl_init(void)
  1381. {
  1382. int ret;
  1383. #ifdef OPENSSL_EXTRA
  1384. Curl_tls_keylog_open();
  1385. #endif
  1386. ret = (wolfSSL_Init() == SSL_SUCCESS);
  1387. wolfssl_bio_cf_init_methods();
  1388. return ret;
  1389. }
  1390. static void wolfssl_cleanup(void)
  1391. {
  1392. wolfssl_bio_cf_free_methods();
  1393. wolfSSL_Cleanup();
  1394. #ifdef OPENSSL_EXTRA
  1395. Curl_tls_keylog_close();
  1396. #endif
  1397. }
  1398. static bool wolfssl_data_pending(struct Curl_cfilter *cf,
  1399. const struct Curl_easy *data)
  1400. {
  1401. struct ssl_connect_data *ctx = cf->ctx;
  1402. struct wolfssl_ctx *backend;
  1403. (void)data;
  1404. DEBUGASSERT(ctx && ctx->backend);
  1405. backend = (struct wolfssl_ctx *)ctx->backend;
  1406. if(backend->handle) /* SSL is in use */
  1407. return (0 != wolfSSL_pending(backend->handle)) ? TRUE : FALSE;
  1408. else
  1409. return FALSE;
  1410. }
  1411. static CURLcode
  1412. wolfssl_connect_common(struct Curl_cfilter *cf,
  1413. struct Curl_easy *data,
  1414. bool nonblocking,
  1415. bool *done)
  1416. {
  1417. CURLcode result;
  1418. struct ssl_connect_data *connssl = cf->ctx;
  1419. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  1420. int what;
  1421. /* check if the connection has already been established */
  1422. if(ssl_connection_complete == connssl->state) {
  1423. *done = TRUE;
  1424. return CURLE_OK;
  1425. }
  1426. if(ssl_connect_1 == connssl->connecting_state) {
  1427. /* Find out how much more time we are allowed */
  1428. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1429. if(timeout_ms < 0) {
  1430. /* no need to continue if time already is up */
  1431. failf(data, "SSL connection timeout");
  1432. return CURLE_OPERATION_TIMEDOUT;
  1433. }
  1434. result = wolfssl_connect_step1(cf, data);
  1435. if(result)
  1436. return result;
  1437. }
  1438. while(ssl_connect_2 == connssl->connecting_state) {
  1439. /* check allowed time left */
  1440. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1441. if(timeout_ms < 0) {
  1442. /* no need to continue if time already is up */
  1443. failf(data, "SSL connection timeout");
  1444. return CURLE_OPERATION_TIMEDOUT;
  1445. }
  1446. /* if ssl is expecting something, check if it is available. */
  1447. if(connssl->io_need) {
  1448. curl_socket_t writefd = (connssl->io_need & CURL_SSL_IO_NEED_SEND)?
  1449. sockfd:CURL_SOCKET_BAD;
  1450. curl_socket_t readfd = (connssl->io_need & CURL_SSL_IO_NEED_RECV)?
  1451. sockfd:CURL_SOCKET_BAD;
  1452. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  1453. nonblocking?0:timeout_ms);
  1454. if(what < 0) {
  1455. /* fatal error */
  1456. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1457. return CURLE_SSL_CONNECT_ERROR;
  1458. }
  1459. else if(0 == what) {
  1460. if(nonblocking) {
  1461. *done = FALSE;
  1462. return CURLE_OK;
  1463. }
  1464. else {
  1465. /* timeout */
  1466. failf(data, "SSL connection timeout");
  1467. return CURLE_OPERATION_TIMEDOUT;
  1468. }
  1469. }
  1470. /* socket is readable or writable */
  1471. }
  1472. /* Run transaction, and return to the caller if it failed or if
  1473. * this connection is part of a multi handle and this loop would
  1474. * execute again. This permits the owner of a multi handle to
  1475. * abort a connection attempt before step2 has completed while
  1476. * ensuring that a client using select() or epoll() will always
  1477. * have a valid fdset to wait on.
  1478. */
  1479. result = wolfssl_connect_step2(cf, data);
  1480. if(result || (nonblocking && (ssl_connect_2 == connssl->connecting_state)))
  1481. return result;
  1482. } /* repeat step2 until all transactions are done. */
  1483. if(ssl_connect_3 == connssl->connecting_state) {
  1484. result = wolfssl_connect_step3(cf, data);
  1485. if(result)
  1486. return result;
  1487. }
  1488. if(ssl_connect_done == connssl->connecting_state) {
  1489. connssl->state = ssl_connection_complete;
  1490. *done = TRUE;
  1491. }
  1492. else
  1493. *done = FALSE;
  1494. /* Reset our connect state machine */
  1495. connssl->connecting_state = ssl_connect_1;
  1496. return CURLE_OK;
  1497. }
  1498. static CURLcode wolfssl_connect_nonblocking(struct Curl_cfilter *cf,
  1499. struct Curl_easy *data,
  1500. bool *done)
  1501. {
  1502. return wolfssl_connect_common(cf, data, TRUE, done);
  1503. }
  1504. static CURLcode wolfssl_connect(struct Curl_cfilter *cf,
  1505. struct Curl_easy *data)
  1506. {
  1507. CURLcode result;
  1508. bool done = FALSE;
  1509. result = wolfssl_connect_common(cf, data, FALSE, &done);
  1510. if(result)
  1511. return result;
  1512. DEBUGASSERT(done);
  1513. return CURLE_OK;
  1514. }
  1515. static CURLcode wolfssl_random(struct Curl_easy *data,
  1516. unsigned char *entropy, size_t length)
  1517. {
  1518. WC_RNG rng;
  1519. (void)data;
  1520. if(wc_InitRng(&rng))
  1521. return CURLE_FAILED_INIT;
  1522. if(length > UINT_MAX)
  1523. return CURLE_FAILED_INIT;
  1524. if(wc_RNG_GenerateBlock(&rng, entropy, (unsigned)length))
  1525. return CURLE_FAILED_INIT;
  1526. if(wc_FreeRng(&rng))
  1527. return CURLE_FAILED_INIT;
  1528. return CURLE_OK;
  1529. }
  1530. static CURLcode wolfssl_sha256sum(const unsigned char *tmp, /* input */
  1531. size_t tmplen,
  1532. unsigned char *sha256sum /* output */,
  1533. size_t unused)
  1534. {
  1535. wc_Sha256 SHA256pw;
  1536. (void)unused;
  1537. if(wc_InitSha256(&SHA256pw))
  1538. return CURLE_FAILED_INIT;
  1539. wc_Sha256Update(&SHA256pw, tmp, (word32)tmplen);
  1540. wc_Sha256Final(&SHA256pw, sha256sum);
  1541. return CURLE_OK;
  1542. }
  1543. static void *wolfssl_get_internals(struct ssl_connect_data *connssl,
  1544. CURLINFO info UNUSED_PARAM)
  1545. {
  1546. struct wolfssl_ctx *backend =
  1547. (struct wolfssl_ctx *)connssl->backend;
  1548. (void)info;
  1549. DEBUGASSERT(backend);
  1550. return backend->handle;
  1551. }
  1552. const struct Curl_ssl Curl_ssl_wolfssl = {
  1553. { CURLSSLBACKEND_WOLFSSL, "wolfssl" }, /* info */
  1554. #ifdef KEEP_PEER_CERT
  1555. SSLSUPP_PINNEDPUBKEY |
  1556. #endif
  1557. #ifdef USE_BIO_CHAIN
  1558. SSLSUPP_HTTPS_PROXY |
  1559. #endif
  1560. SSLSUPP_CA_PATH |
  1561. SSLSUPP_CAINFO_BLOB |
  1562. #ifdef USE_ECH
  1563. SSLSUPP_ECH |
  1564. #endif
  1565. SSLSUPP_SSL_CTX |
  1566. SSLSUPP_CA_CACHE,
  1567. sizeof(struct wolfssl_ctx),
  1568. wolfssl_init, /* init */
  1569. wolfssl_cleanup, /* cleanup */
  1570. wolfssl_version, /* version */
  1571. Curl_none_check_cxn, /* check_cxn */
  1572. wolfssl_shutdown, /* shutdown */
  1573. wolfssl_data_pending, /* data_pending */
  1574. wolfssl_random, /* random */
  1575. Curl_none_cert_status_request, /* cert_status_request */
  1576. wolfssl_connect, /* connect */
  1577. wolfssl_connect_nonblocking, /* connect_nonblocking */
  1578. Curl_ssl_adjust_pollset, /* adjust_pollset */
  1579. wolfssl_get_internals, /* get_internals */
  1580. wolfssl_close, /* close_one */
  1581. Curl_none_close_all, /* close_all */
  1582. Curl_none_set_engine, /* set_engine */
  1583. Curl_none_set_engine_default, /* set_engine_default */
  1584. Curl_none_engines_list, /* engines_list */
  1585. Curl_none_false_start, /* false_start */
  1586. wolfssl_sha256sum, /* sha256sum */
  1587. NULL, /* associate_connection */
  1588. NULL, /* disassociate_connection */
  1589. wolfssl_recv, /* recv decrypted data */
  1590. wolfssl_send, /* send data to encrypt */
  1591. };
  1592. #endif