2
0

wolfssl.c 62 KB

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