wolfssl.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526
  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. struct wolfssl_ssl_backend_data {
  89. WOLFSSL_CTX *ctx;
  90. WOLFSSL *handle;
  91. CURLcode io_result; /* result of last BIO cfilter operation */
  92. };
  93. #ifdef OPENSSL_EXTRA
  94. /*
  95. * Availability note:
  96. * The TLS 1.3 secret callback (wolfSSL_set_tls13_secret_cb) was added in
  97. * WolfSSL 4.4.0, but requires the -DHAVE_SECRET_CALLBACK build option. If that
  98. * option is not set, then TLS 1.3 will not be logged.
  99. * For TLS 1.2 and before, we use wolfSSL_get_keys().
  100. * SSL_get_client_random and wolfSSL_get_keys require OPENSSL_EXTRA
  101. * (--enable-opensslextra or --enable-all).
  102. */
  103. #if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
  104. static int
  105. wolfssl_tls13_secret_callback(SSL *ssl, int id, const unsigned char *secret,
  106. int secretSz, void *ctx)
  107. {
  108. const char *label;
  109. unsigned char client_random[SSL3_RANDOM_SIZE];
  110. (void)ctx;
  111. if(!ssl || !Curl_tls_keylog_enabled()) {
  112. return 0;
  113. }
  114. switch(id) {
  115. case CLIENT_EARLY_TRAFFIC_SECRET:
  116. label = "CLIENT_EARLY_TRAFFIC_SECRET";
  117. break;
  118. case CLIENT_HANDSHAKE_TRAFFIC_SECRET:
  119. label = "CLIENT_HANDSHAKE_TRAFFIC_SECRET";
  120. break;
  121. case SERVER_HANDSHAKE_TRAFFIC_SECRET:
  122. label = "SERVER_HANDSHAKE_TRAFFIC_SECRET";
  123. break;
  124. case CLIENT_TRAFFIC_SECRET:
  125. label = "CLIENT_TRAFFIC_SECRET_0";
  126. break;
  127. case SERVER_TRAFFIC_SECRET:
  128. label = "SERVER_TRAFFIC_SECRET_0";
  129. break;
  130. case EARLY_EXPORTER_SECRET:
  131. label = "EARLY_EXPORTER_SECRET";
  132. break;
  133. case EXPORTER_SECRET:
  134. label = "EXPORTER_SECRET";
  135. break;
  136. default:
  137. return 0;
  138. }
  139. if(SSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE) == 0) {
  140. /* Should never happen as wolfSSL_KeepArrays() was called before. */
  141. return 0;
  142. }
  143. Curl_tls_keylog_write(label, client_random, secret, secretSz);
  144. return 0;
  145. }
  146. #endif /* defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13) */
  147. static void
  148. wolfssl_log_tls12_secret(SSL *ssl)
  149. {
  150. unsigned char *ms, *sr, *cr;
  151. unsigned int msLen, srLen, crLen, i, x = 0;
  152. #if LIBWOLFSSL_VERSION_HEX >= 0x0300d000 /* >= 3.13.0 */
  153. /* wolfSSL_GetVersion is available since 3.13, we use it instead of
  154. * SSL_version since the latter relies on OPENSSL_ALL (--enable-opensslall or
  155. * --enable-all). Failing to perform this check could result in an unusable
  156. * key log line when TLS 1.3 is actually negotiated. */
  157. switch(wolfSSL_GetVersion(ssl)) {
  158. case WOLFSSL_SSLV3:
  159. case WOLFSSL_TLSV1:
  160. case WOLFSSL_TLSV1_1:
  161. case WOLFSSL_TLSV1_2:
  162. break;
  163. default:
  164. /* TLS 1.3 does not use this mechanism, the "master secret" returned below
  165. * is not directly usable. */
  166. return;
  167. }
  168. #endif
  169. if(wolfSSL_get_keys(ssl, &ms, &msLen, &sr, &srLen, &cr, &crLen) !=
  170. SSL_SUCCESS) {
  171. return;
  172. }
  173. /* Check for a missing master secret and skip logging. That can happen if
  174. * curl rejects the server certificate and aborts the handshake.
  175. */
  176. for(i = 0; i < msLen; i++) {
  177. x |= ms[i];
  178. }
  179. if(x == 0) {
  180. return;
  181. }
  182. Curl_tls_keylog_write("CLIENT_RANDOM", cr, ms, msLen);
  183. }
  184. #endif /* OPENSSL_EXTRA */
  185. static int do_file_type(const char *type)
  186. {
  187. if(!type || !type[0])
  188. return SSL_FILETYPE_PEM;
  189. if(strcasecompare(type, "PEM"))
  190. return SSL_FILETYPE_PEM;
  191. if(strcasecompare(type, "DER"))
  192. return SSL_FILETYPE_ASN1;
  193. return -1;
  194. }
  195. #ifdef HAVE_LIBOQS
  196. struct group_name_map {
  197. const word16 group;
  198. const char *name;
  199. };
  200. static const struct group_name_map gnm[] = {
  201. { WOLFSSL_KYBER_LEVEL1, "KYBER_LEVEL1" },
  202. { WOLFSSL_KYBER_LEVEL3, "KYBER_LEVEL3" },
  203. { WOLFSSL_KYBER_LEVEL5, "KYBER_LEVEL5" },
  204. { WOLFSSL_P256_KYBER_LEVEL1, "P256_KYBER_LEVEL1" },
  205. { WOLFSSL_P384_KYBER_LEVEL3, "P384_KYBER_LEVEL3" },
  206. { WOLFSSL_P521_KYBER_LEVEL5, "P521_KYBER_LEVEL5" },
  207. { 0, NULL }
  208. };
  209. #endif
  210. #ifdef USE_BIO_CHAIN
  211. static int wolfssl_bio_cf_create(WOLFSSL_BIO *bio)
  212. {
  213. wolfSSL_BIO_set_shutdown(bio, 1);
  214. wolfSSL_BIO_set_data(bio, NULL);
  215. return 1;
  216. }
  217. static int wolfssl_bio_cf_destroy(WOLFSSL_BIO *bio)
  218. {
  219. if(!bio)
  220. return 0;
  221. return 1;
  222. }
  223. static long wolfssl_bio_cf_ctrl(WOLFSSL_BIO *bio, int cmd, long num, void *ptr)
  224. {
  225. struct Curl_cfilter *cf = BIO_get_data(bio);
  226. long ret = 1;
  227. (void)cf;
  228. (void)ptr;
  229. switch(cmd) {
  230. case BIO_CTRL_GET_CLOSE:
  231. ret = (long)wolfSSL_BIO_get_shutdown(bio);
  232. break;
  233. case BIO_CTRL_SET_CLOSE:
  234. wolfSSL_BIO_set_shutdown(bio, (int)num);
  235. break;
  236. case BIO_CTRL_FLUSH:
  237. /* we do no delayed writes, but if we ever would, this
  238. * needs to trigger it. */
  239. ret = 1;
  240. break;
  241. case BIO_CTRL_DUP:
  242. ret = 1;
  243. break;
  244. #ifdef BIO_CTRL_EOF
  245. case BIO_CTRL_EOF:
  246. /* EOF has been reached on input? */
  247. return (!cf->next || !cf->next->connected);
  248. #endif
  249. default:
  250. ret = 0;
  251. break;
  252. }
  253. return ret;
  254. }
  255. static int wolfssl_bio_cf_out_write(WOLFSSL_BIO *bio,
  256. const char *buf, int blen)
  257. {
  258. struct Curl_cfilter *cf = wolfSSL_BIO_get_data(bio);
  259. struct ssl_connect_data *connssl = cf->ctx;
  260. struct wolfssl_ssl_backend_data *backend =
  261. (struct wolfssl_ssl_backend_data *)connssl->backend;
  262. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  263. ssize_t nwritten;
  264. CURLcode result = CURLE_OK;
  265. DEBUGASSERT(data);
  266. nwritten = Curl_conn_cf_send(cf->next, data, buf, blen, &result);
  267. backend->io_result = result;
  268. CURL_TRC_CF(data, cf, "bio_write(len=%d) -> %zd, %d",
  269. blen, nwritten, result);
  270. wolfSSL_BIO_clear_retry_flags(bio);
  271. if(nwritten < 0 && CURLE_AGAIN == result)
  272. BIO_set_retry_write(bio);
  273. return (int)nwritten;
  274. }
  275. static int wolfssl_bio_cf_in_read(WOLFSSL_BIO *bio, char *buf, int blen)
  276. {
  277. struct Curl_cfilter *cf = wolfSSL_BIO_get_data(bio);
  278. struct ssl_connect_data *connssl = cf->ctx;
  279. struct wolfssl_ssl_backend_data *backend =
  280. (struct wolfssl_ssl_backend_data *)connssl->backend;
  281. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  282. ssize_t nread;
  283. CURLcode result = CURLE_OK;
  284. DEBUGASSERT(data);
  285. /* OpenSSL catches this case, so should we. */
  286. if(!buf)
  287. return 0;
  288. nread = Curl_conn_cf_recv(cf->next, data, buf, blen, &result);
  289. backend->io_result = result;
  290. CURL_TRC_CF(data, cf, "bio_read(len=%d) -> %zd, %d", blen, nread, result);
  291. wolfSSL_BIO_clear_retry_flags(bio);
  292. if(nread < 0 && CURLE_AGAIN == result)
  293. BIO_set_retry_read(bio);
  294. else if(nread == 0)
  295. connssl->peer_closed = TRUE;
  296. return (int)nread;
  297. }
  298. static WOLFSSL_BIO_METHOD *wolfssl_bio_cf_method = NULL;
  299. static void wolfssl_bio_cf_init_methods(void)
  300. {
  301. wolfssl_bio_cf_method = wolfSSL_BIO_meth_new(BIO_TYPE_MEM, "wolfSSL CF BIO");
  302. wolfSSL_BIO_meth_set_write(wolfssl_bio_cf_method, &wolfssl_bio_cf_out_write);
  303. wolfSSL_BIO_meth_set_read(wolfssl_bio_cf_method, &wolfssl_bio_cf_in_read);
  304. wolfSSL_BIO_meth_set_ctrl(wolfssl_bio_cf_method, &wolfssl_bio_cf_ctrl);
  305. wolfSSL_BIO_meth_set_create(wolfssl_bio_cf_method, &wolfssl_bio_cf_create);
  306. wolfSSL_BIO_meth_set_destroy(wolfssl_bio_cf_method, &wolfssl_bio_cf_destroy);
  307. }
  308. static void wolfssl_bio_cf_free_methods(void)
  309. {
  310. wolfSSL_BIO_meth_free(wolfssl_bio_cf_method);
  311. }
  312. #else /* USE_BIO_CHAIN */
  313. #define wolfssl_bio_cf_init_methods() Curl_nop_stmt
  314. #define wolfssl_bio_cf_free_methods() Curl_nop_stmt
  315. #endif /* !USE_BIO_CHAIN */
  316. /*
  317. * This function loads all the client/CA certificates and CRLs. Setup the TLS
  318. * layer and do all necessary magic.
  319. */
  320. static CURLcode
  321. wolfssl_connect_step1(struct Curl_cfilter *cf, struct Curl_easy *data)
  322. {
  323. char *ciphers, *curves;
  324. struct ssl_connect_data *connssl = cf->ctx;
  325. struct wolfssl_ssl_backend_data *backend =
  326. (struct wolfssl_ssl_backend_data *)connssl->backend;
  327. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  328. const struct curl_blob *ca_info_blob = conn_config->ca_info_blob;
  329. const struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  330. const char * const ssl_cafile =
  331. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  332. (ca_info_blob ? NULL : conn_config->CAfile);
  333. const char * const ssl_capath = conn_config->CApath;
  334. WOLFSSL_METHOD* req_method = NULL;
  335. #ifdef HAVE_LIBOQS
  336. word16 oqsAlg = 0;
  337. size_t idx = 0;
  338. #endif
  339. #ifdef HAVE_SNI
  340. bool sni = FALSE;
  341. #define use_sni(x) sni = (x)
  342. #else
  343. #define use_sni(x) Curl_nop_stmt
  344. #endif
  345. bool imported_native_ca = false;
  346. bool imported_ca_info_blob = false;
  347. DEBUGASSERT(backend);
  348. if(connssl->state == ssl_connection_complete)
  349. return CURLE_OK;
  350. if(conn_config->version_max != CURL_SSLVERSION_MAX_NONE) {
  351. failf(data, "wolfSSL does not support to set maximum SSL/TLS version");
  352. return CURLE_SSL_CONNECT_ERROR;
  353. }
  354. /* check to see if we've been told to use an explicit SSL/TLS version */
  355. switch(conn_config->version) {
  356. case CURL_SSLVERSION_DEFAULT:
  357. case CURL_SSLVERSION_TLSv1:
  358. #if LIBWOLFSSL_VERSION_HEX >= 0x03003000 /* >= 3.3.0 */
  359. /* minimum protocol version is set later after the CTX object is created */
  360. req_method = SSLv23_client_method();
  361. #else
  362. infof(data, "wolfSSL <3.3.0 cannot be configured to use TLS 1.0-1.2, "
  363. "TLS 1.0 is used exclusively");
  364. req_method = TLSv1_client_method();
  365. #endif
  366. use_sni(TRUE);
  367. break;
  368. case CURL_SSLVERSION_TLSv1_0:
  369. #if defined(WOLFSSL_ALLOW_TLSV10) && !defined(NO_OLD_TLS)
  370. req_method = TLSv1_client_method();
  371. use_sni(TRUE);
  372. break;
  373. #else
  374. failf(data, "wolfSSL does not support TLS 1.0");
  375. return CURLE_NOT_BUILT_IN;
  376. #endif
  377. case CURL_SSLVERSION_TLSv1_1:
  378. #ifndef NO_OLD_TLS
  379. req_method = TLSv1_1_client_method();
  380. use_sni(TRUE);
  381. #else
  382. failf(data, "wolfSSL does not support TLS 1.1");
  383. return CURLE_NOT_BUILT_IN;
  384. #endif
  385. break;
  386. case CURL_SSLVERSION_TLSv1_2:
  387. #ifndef WOLFSSL_NO_TLS12
  388. req_method = TLSv1_2_client_method();
  389. use_sni(TRUE);
  390. #else
  391. failf(data, "wolfSSL does not support TLS 1.2");
  392. return CURLE_NOT_BUILT_IN;
  393. #endif
  394. break;
  395. case CURL_SSLVERSION_TLSv1_3:
  396. #ifdef WOLFSSL_TLS13
  397. req_method = wolfTLSv1_3_client_method();
  398. use_sni(TRUE);
  399. break;
  400. #else
  401. failf(data, "wolfSSL: TLS 1.3 is not yet supported");
  402. return CURLE_SSL_CONNECT_ERROR;
  403. #endif
  404. default:
  405. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  406. return CURLE_SSL_CONNECT_ERROR;
  407. }
  408. if(!req_method) {
  409. failf(data, "SSL: couldn't create a method");
  410. return CURLE_OUT_OF_MEMORY;
  411. }
  412. if(backend->ctx)
  413. wolfSSL_CTX_free(backend->ctx);
  414. backend->ctx = wolfSSL_CTX_new(req_method);
  415. if(!backend->ctx) {
  416. failf(data, "SSL: couldn't create a context");
  417. return CURLE_OUT_OF_MEMORY;
  418. }
  419. switch(conn_config->version) {
  420. case CURL_SSLVERSION_DEFAULT:
  421. case CURL_SSLVERSION_TLSv1:
  422. #if LIBWOLFSSL_VERSION_HEX > 0x03004006 /* > 3.4.6 */
  423. /* Versions 3.3.0 to 3.4.6 we know the minimum protocol version is
  424. * whatever minimum version of TLS was built in and at least TLS 1.0. For
  425. * later library versions that could change (eg TLS 1.0 built in but
  426. * defaults to TLS 1.1) so we have this short circuit evaluation to find
  427. * the minimum supported TLS version.
  428. */
  429. if((wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1) != 1) &&
  430. (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_1) != 1) &&
  431. (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_2) != 1)
  432. #ifdef WOLFSSL_TLS13
  433. && (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_3) != 1)
  434. #endif
  435. ) {
  436. failf(data, "SSL: couldn't set the minimum protocol version");
  437. return CURLE_SSL_CONNECT_ERROR;
  438. }
  439. #endif
  440. FALLTHROUGH();
  441. default:
  442. break;
  443. }
  444. ciphers = conn_config->cipher_list;
  445. if(ciphers) {
  446. if(!SSL_CTX_set_cipher_list(backend->ctx, ciphers)) {
  447. failf(data, "failed setting cipher list: %s", ciphers);
  448. return CURLE_SSL_CIPHER;
  449. }
  450. infof(data, "Cipher selection: %s", ciphers);
  451. }
  452. curves = conn_config->curves;
  453. if(curves) {
  454. #ifdef HAVE_LIBOQS
  455. for(idx = 0; gnm[idx].name != NULL; idx++) {
  456. if(strncmp(curves, gnm[idx].name, strlen(gnm[idx].name)) == 0) {
  457. oqsAlg = gnm[idx].group;
  458. break;
  459. }
  460. }
  461. if(oqsAlg == 0)
  462. #endif
  463. {
  464. if(!SSL_CTX_set1_curves_list(backend->ctx, curves)) {
  465. failf(data, "failed setting curves list: '%s'", curves);
  466. return CURLE_SSL_CIPHER;
  467. }
  468. }
  469. }
  470. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SYS_CA_CERTS)
  471. /* load native CA certificates */
  472. if(ssl_config->native_ca_store) {
  473. if(wolfSSL_CTX_load_system_CA_certs(backend->ctx) != WOLFSSL_SUCCESS) {
  474. infof(data, "error importing native CA store, continuing anyway");
  475. }
  476. else {
  477. imported_native_ca = true;
  478. infof(data, "successfully imported native CA store");
  479. }
  480. }
  481. #endif /* !NO_FILESYSTEM */
  482. /* load certificate blob */
  483. if(ca_info_blob) {
  484. if(wolfSSL_CTX_load_verify_buffer(backend->ctx, ca_info_blob->data,
  485. ca_info_blob->len,
  486. SSL_FILETYPE_PEM) != SSL_SUCCESS) {
  487. if(imported_native_ca) {
  488. infof(data, "error importing CA certificate blob, continuing anyway");
  489. }
  490. else {
  491. failf(data, "error importing CA certificate blob");
  492. return CURLE_SSL_CACERT_BADFILE;
  493. }
  494. }
  495. else {
  496. imported_ca_info_blob = true;
  497. infof(data, "successfully imported CA certificate blob");
  498. }
  499. }
  500. #ifndef NO_FILESYSTEM
  501. /* load trusted cacert from file if not blob */
  502. if(ssl_cafile || ssl_capath) {
  503. int rc =
  504. wolfSSL_CTX_load_verify_locations_ex(backend->ctx,
  505. ssl_cafile,
  506. ssl_capath,
  507. WOLFSSL_LOAD_FLAG_IGNORE_ERR);
  508. if(SSL_SUCCESS != rc) {
  509. if(conn_config->verifypeer && !imported_ca_info_blob &&
  510. !imported_native_ca) {
  511. /* Fail if we insist on successfully verifying the server. */
  512. failf(data, "error setting certificate verify locations:"
  513. " CAfile: %s CApath: %s",
  514. ssl_cafile ? ssl_cafile : "none",
  515. ssl_capath ? ssl_capath : "none");
  516. return CURLE_SSL_CACERT_BADFILE;
  517. }
  518. else {
  519. /* Just continue with a warning if no strict certificate
  520. verification is required. */
  521. infof(data, "error setting certificate verify locations,"
  522. " continuing anyway:");
  523. }
  524. }
  525. else {
  526. /* Everything is fine. */
  527. infof(data, "successfully set certificate verify locations:");
  528. }
  529. infof(data, " CAfile: %s", ssl_cafile ? ssl_cafile : "none");
  530. infof(data, " CApath: %s", ssl_capath ? ssl_capath : "none");
  531. }
  532. /* Load the client certificate, and private key */
  533. if(ssl_config->primary.clientcert && ssl_config->key) {
  534. int file_type = do_file_type(ssl_config->cert_type);
  535. if(file_type == WOLFSSL_FILETYPE_PEM) {
  536. if(wolfSSL_CTX_use_certificate_chain_file(backend->ctx,
  537. ssl_config->primary.clientcert)
  538. != 1) {
  539. failf(data, "unable to use client certificate");
  540. return CURLE_SSL_CONNECT_ERROR;
  541. }
  542. }
  543. else if(file_type == WOLFSSL_FILETYPE_ASN1) {
  544. if(wolfSSL_CTX_use_certificate_file(backend->ctx,
  545. ssl_config->primary.clientcert,
  546. file_type) != 1) {
  547. failf(data, "unable to use client certificate");
  548. return CURLE_SSL_CONNECT_ERROR;
  549. }
  550. }
  551. else {
  552. failf(data, "unknown cert type");
  553. return CURLE_BAD_FUNCTION_ARGUMENT;
  554. }
  555. file_type = do_file_type(ssl_config->key_type);
  556. if(wolfSSL_CTX_use_PrivateKey_file(backend->ctx, ssl_config->key,
  557. file_type) != 1) {
  558. failf(data, "unable to set private key");
  559. return CURLE_SSL_CONNECT_ERROR;
  560. }
  561. }
  562. #endif /* !NO_FILESYSTEM */
  563. /* SSL always tries to verify the peer, this only says whether it should
  564. * fail to connect if the verification fails, or if it should continue
  565. * anyway. In the latter case the result of the verification is checked with
  566. * SSL_get_verify_result() below. */
  567. wolfSSL_CTX_set_verify(backend->ctx,
  568. conn_config->verifypeer?SSL_VERIFY_PEER:
  569. SSL_VERIFY_NONE, NULL);
  570. #ifdef HAVE_SNI
  571. if(sni && connssl->peer.sni) {
  572. size_t sni_len = strlen(connssl->peer.sni);
  573. if((sni_len < USHRT_MAX)) {
  574. if(wolfSSL_CTX_UseSNI(backend->ctx, WOLFSSL_SNI_HOST_NAME,
  575. connssl->peer.sni,
  576. (unsigned short)sni_len) != 1) {
  577. failf(data, "Failed to set SNI");
  578. return CURLE_SSL_CONNECT_ERROR;
  579. }
  580. }
  581. }
  582. #endif
  583. /* give application a chance to interfere with SSL set up. */
  584. if(data->set.ssl.fsslctx) {
  585. CURLcode result = (*data->set.ssl.fsslctx)(data, backend->ctx,
  586. data->set.ssl.fsslctxp);
  587. if(result) {
  588. failf(data, "error signaled by ssl ctx callback");
  589. return result;
  590. }
  591. }
  592. #ifdef NO_FILESYSTEM
  593. else if(conn_config->verifypeer) {
  594. failf(data, "SSL: Certificates can't be loaded because wolfSSL was built"
  595. " with \"no filesystem\". Either disable peer verification"
  596. " (insecure) or if you are building an application with libcurl you"
  597. " can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
  598. return CURLE_SSL_CONNECT_ERROR;
  599. }
  600. #endif
  601. /* Let's make an SSL structure */
  602. if(backend->handle)
  603. wolfSSL_free(backend->handle);
  604. backend->handle = wolfSSL_new(backend->ctx);
  605. if(!backend->handle) {
  606. failf(data, "SSL: couldn't create a handle");
  607. return CURLE_OUT_OF_MEMORY;
  608. }
  609. #ifdef HAVE_LIBOQS
  610. if(oqsAlg) {
  611. if(wolfSSL_UseKeyShare(backend->handle, oqsAlg) != WOLFSSL_SUCCESS) {
  612. failf(data, "unable to use oqs KEM");
  613. }
  614. }
  615. #endif
  616. #ifdef HAVE_ALPN
  617. if(connssl->alpn) {
  618. struct alpn_proto_buf proto;
  619. CURLcode result;
  620. result = Curl_alpn_to_proto_str(&proto, connssl->alpn);
  621. if(result ||
  622. wolfSSL_UseALPN(backend->handle, (char *)proto.data, proto.len,
  623. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) != SSL_SUCCESS) {
  624. failf(data, "SSL: failed setting ALPN protocols");
  625. return CURLE_SSL_CONNECT_ERROR;
  626. }
  627. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  628. }
  629. #endif /* HAVE_ALPN */
  630. #ifdef OPENSSL_EXTRA
  631. if(Curl_tls_keylog_enabled()) {
  632. /* Ensure the Client Random is preserved. */
  633. wolfSSL_KeepArrays(backend->handle);
  634. #if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
  635. wolfSSL_set_tls13_secret_cb(backend->handle,
  636. wolfssl_tls13_secret_callback, NULL);
  637. #endif
  638. }
  639. #endif /* OPENSSL_EXTRA */
  640. #ifdef HAVE_SECURE_RENEGOTIATION
  641. if(wolfSSL_UseSecureRenegotiation(backend->handle) != SSL_SUCCESS) {
  642. failf(data, "SSL: failed setting secure renegotiation");
  643. return CURLE_SSL_CONNECT_ERROR;
  644. }
  645. #endif /* HAVE_SECURE_RENEGOTIATION */
  646. /* Check if there's a cached ID we can/should use here! */
  647. if(ssl_config->primary.sessionid) {
  648. void *ssl_sessionid = NULL;
  649. Curl_ssl_sessionid_lock(data);
  650. if(!Curl_ssl_getsessionid(cf, data, &connssl->peer,
  651. &ssl_sessionid, NULL)) {
  652. /* we got a session id, use it! */
  653. if(!SSL_set_session(backend->handle, ssl_sessionid)) {
  654. Curl_ssl_delsessionid(data, ssl_sessionid);
  655. infof(data, "Can't use session ID, going on without");
  656. }
  657. else
  658. infof(data, "SSL reusing session ID");
  659. }
  660. Curl_ssl_sessionid_unlock(data);
  661. }
  662. #ifdef USE_ECH
  663. if(ECH_ENABLED(data)) {
  664. int trying_ech_now = 0;
  665. if(data->set.str[STRING_ECH_PUBLIC]) {
  666. infof(data, "ECH: outername not (yet) supported with WolfSSL");
  667. return CURLE_SSL_CONNECT_ERROR;
  668. }
  669. if(data->set.tls_ech == CURLECH_GREASE) {
  670. infof(data, "ECH: GREASE'd ECH not yet supported for wolfSSL");
  671. return CURLE_SSL_CONNECT_ERROR;
  672. }
  673. if(data->set.tls_ech & CURLECH_CLA_CFG
  674. && data->set.str[STRING_ECH_CONFIG]) {
  675. char *b64val = data->set.str[STRING_ECH_CONFIG];
  676. word32 b64len = 0;
  677. b64len = (word32) strlen(b64val);
  678. if(b64len
  679. && wolfSSL_SetEchConfigsBase64(backend->handle, b64val, b64len)
  680. != WOLFSSL_SUCCESS) {
  681. if(data->set.tls_ech & CURLECH_HARD)
  682. return CURLE_SSL_CONNECT_ERROR;
  683. }
  684. else {
  685. trying_ech_now = 1;
  686. infof(data, "ECH: ECHConfig from command line");
  687. }
  688. }
  689. else {
  690. struct Curl_dns_entry *dns = NULL;
  691. dns = Curl_fetch_addr(data, connssl->peer.hostname, connssl->peer.port);
  692. if(!dns) {
  693. infof(data, "ECH: requested but no DNS info available");
  694. if(data->set.tls_ech & CURLECH_HARD)
  695. return CURLE_SSL_CONNECT_ERROR;
  696. }
  697. else {
  698. struct Curl_https_rrinfo *rinfo = NULL;
  699. rinfo = dns->hinfo;
  700. if(rinfo && rinfo->echconfiglist) {
  701. unsigned char *ecl = rinfo->echconfiglist;
  702. size_t elen = rinfo->echconfiglist_len;
  703. infof(data, "ECH: ECHConfig from DoH HTTPS RR");
  704. if(wolfSSL_SetEchConfigs(backend->handle, ecl, (word32) elen) !=
  705. WOLFSSL_SUCCESS) {
  706. infof(data, "ECH: wolfSSL_SetEchConfigs failed");
  707. if(data->set.tls_ech & CURLECH_HARD)
  708. return CURLE_SSL_CONNECT_ERROR;
  709. }
  710. else {
  711. trying_ech_now = 1;
  712. infof(data, "ECH: imported ECHConfigList of length %ld", elen);
  713. }
  714. }
  715. else {
  716. infof(data, "ECH: requested but no ECHConfig available");
  717. if(data->set.tls_ech & CURLECH_HARD)
  718. return CURLE_SSL_CONNECT_ERROR;
  719. }
  720. Curl_resolv_unlock(data, dns);
  721. }
  722. }
  723. if(trying_ech_now
  724. && SSL_set_min_proto_version(backend->handle, TLS1_3_VERSION) != 1) {
  725. infof(data, "ECH: Can't force TLSv1.3 [ERROR]");
  726. return CURLE_SSL_CONNECT_ERROR;
  727. }
  728. }
  729. #endif /* USE_ECH */
  730. #ifdef USE_BIO_CHAIN
  731. {
  732. WOLFSSL_BIO *bio;
  733. bio = BIO_new(wolfssl_bio_cf_method);
  734. if(!bio)
  735. return CURLE_OUT_OF_MEMORY;
  736. wolfSSL_BIO_set_data(bio, cf);
  737. wolfSSL_set_bio(backend->handle, bio, bio);
  738. }
  739. #else /* USE_BIO_CHAIN */
  740. /* pass the raw socket into the SSL layer */
  741. if(!wolfSSL_set_fd(backend->handle,
  742. (int)Curl_conn_cf_get_socket(cf, data))) {
  743. failf(data, "SSL: SSL_set_fd failed");
  744. return CURLE_SSL_CONNECT_ERROR;
  745. }
  746. #endif /* !USE_BIO_CHAIN */
  747. connssl->connecting_state = ssl_connect_2;
  748. return CURLE_OK;
  749. }
  750. static CURLcode
  751. wolfssl_connect_step2(struct Curl_cfilter *cf, struct Curl_easy *data)
  752. {
  753. int ret = -1;
  754. struct ssl_connect_data *connssl = cf->ctx;
  755. struct wolfssl_ssl_backend_data *backend =
  756. (struct wolfssl_ssl_backend_data *)connssl->backend;
  757. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  758. #ifndef CURL_DISABLE_PROXY
  759. const char * const pinnedpubkey = Curl_ssl_cf_is_proxy(cf)?
  760. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
  761. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  762. #else
  763. const char * const pinnedpubkey = data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  764. #endif
  765. DEBUGASSERT(backend);
  766. wolfSSL_ERR_clear_error();
  767. /* Enable RFC2818 checks */
  768. if(conn_config->verifyhost) {
  769. char *snihost = connssl->peer.sni?
  770. connssl->peer.sni : connssl->peer.hostname;
  771. if(wolfSSL_check_domain_name(backend->handle, snihost) == SSL_FAILURE)
  772. return CURLE_SSL_CONNECT_ERROR;
  773. }
  774. ret = wolfSSL_connect(backend->handle);
  775. #ifdef OPENSSL_EXTRA
  776. if(Curl_tls_keylog_enabled()) {
  777. /* If key logging is enabled, wait for the handshake to complete and then
  778. * proceed with logging secrets (for TLS 1.2 or older).
  779. *
  780. * During the handshake (ret==-1), wolfSSL_want_read() is true as it waits
  781. * for the server response. At that point the master secret is not yet
  782. * available, so we must not try to read it.
  783. * To log the secret on completion with a handshake failure, detect
  784. * completion via the observation that there is nothing to read or write.
  785. * Note that OpenSSL SSL_want_read() is always true here. If wolfSSL ever
  786. * changes, the worst case is that no key is logged on error.
  787. */
  788. if(ret == SSL_SUCCESS ||
  789. (!wolfSSL_want_read(backend->handle) &&
  790. !wolfSSL_want_write(backend->handle))) {
  791. wolfssl_log_tls12_secret(backend->handle);
  792. /* Client Random and master secrets are no longer needed, erase these.
  793. * Ignored while the handshake is still in progress. */
  794. wolfSSL_FreeArrays(backend->handle);
  795. }
  796. }
  797. #endif /* OPENSSL_EXTRA */
  798. if(ret != 1) {
  799. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  800. int detail = wolfSSL_get_error(backend->handle, ret);
  801. if(SSL_ERROR_WANT_READ == detail) {
  802. connssl->connecting_state = ssl_connect_2_reading;
  803. return CURLE_OK;
  804. }
  805. else if(SSL_ERROR_WANT_WRITE == detail) {
  806. connssl->connecting_state = ssl_connect_2_writing;
  807. return CURLE_OK;
  808. }
  809. /* There is no easy way to override only the CN matching.
  810. * This will enable the override of both mismatching SubjectAltNames
  811. * as also mismatching CN fields */
  812. else if(DOMAIN_NAME_MISMATCH == detail) {
  813. #if 1
  814. failf(data, " subject alt name(s) or common name do not match \"%s\"",
  815. connssl->peer.dispname);
  816. return CURLE_PEER_FAILED_VERIFICATION;
  817. #else
  818. /* When the wolfssl_check_domain_name() is used and you desire to
  819. * continue on a DOMAIN_NAME_MISMATCH, i.e. 'ssl_config.verifyhost
  820. * == 0', CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA
  821. * error. The only way to do this is currently to switch the
  822. * Wolfssl_check_domain_name() in and out based on the
  823. * 'ssl_config.verifyhost' value. */
  824. if(conn_config->verifyhost) {
  825. failf(data,
  826. " subject alt name(s) or common name do not match \"%s\"\n",
  827. connssl->dispname);
  828. return CURLE_PEER_FAILED_VERIFICATION;
  829. }
  830. else {
  831. infof(data,
  832. " subject alt name(s) and/or common name do not match \"%s\"",
  833. connssl->dispname);
  834. return CURLE_OK;
  835. }
  836. #endif
  837. }
  838. #if LIBWOLFSSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
  839. else if(ASN_NO_SIGNER_E == detail) {
  840. if(conn_config->verifypeer) {
  841. failf(data, " CA signer not available for verification");
  842. return CURLE_SSL_CACERT_BADFILE;
  843. }
  844. else {
  845. /* Just continue with a warning if no strict certificate
  846. verification is required. */
  847. infof(data, "CA signer not available for verification, "
  848. "continuing anyway");
  849. }
  850. }
  851. #endif
  852. #ifdef USE_ECH
  853. else if(-1 == detail) {
  854. /* try access a retry_config ECHConfigList for tracing */
  855. byte echConfigs[1000];
  856. word32 echConfigsLen = 1000;
  857. int rv = 0;
  858. /* this currently doesn't produce the retry_configs */
  859. rv = wolfSSL_GetEchConfigs(backend->handle, echConfigs,
  860. &echConfigsLen);
  861. if(rv != WOLFSSL_SUCCESS) {
  862. infof(data, "Failed to get ECHConfigs");
  863. }
  864. else {
  865. char *b64str = NULL;
  866. size_t blen = 0;
  867. rv = Curl_base64_encode((const char *)echConfigs, echConfigsLen,
  868. &b64str, &blen);
  869. if(!rv && b64str)
  870. infof(data, "ECH: (not yet) retry_configs %s", b64str);
  871. free(b64str);
  872. }
  873. }
  874. #endif
  875. else if(backend->io_result == CURLE_AGAIN) {
  876. return CURLE_OK;
  877. }
  878. else {
  879. failf(data, "SSL_connect failed with error %d: %s", detail,
  880. wolfSSL_ERR_error_string(detail, error_buffer));
  881. return CURLE_SSL_CONNECT_ERROR;
  882. }
  883. }
  884. if(pinnedpubkey) {
  885. #ifdef KEEP_PEER_CERT
  886. X509 *x509;
  887. const char *x509_der;
  888. int x509_der_len;
  889. struct Curl_X509certificate x509_parsed;
  890. struct Curl_asn1Element *pubkey;
  891. CURLcode result;
  892. x509 = wolfSSL_get_peer_certificate(backend->handle);
  893. if(!x509) {
  894. failf(data, "SSL: failed retrieving server certificate");
  895. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  896. }
  897. x509_der = (const char *)wolfSSL_X509_get_der(x509, &x509_der_len);
  898. if(!x509_der) {
  899. failf(data, "SSL: failed retrieving ASN.1 server certificate");
  900. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  901. }
  902. memset(&x509_parsed, 0, sizeof(x509_parsed));
  903. if(Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len))
  904. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  905. pubkey = &x509_parsed.subjectPublicKeyInfo;
  906. if(!pubkey->header || pubkey->end <= pubkey->header) {
  907. failf(data, "SSL: failed retrieving public key from server certificate");
  908. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  909. }
  910. result = Curl_pin_peer_pubkey(data,
  911. pinnedpubkey,
  912. (const unsigned char *)pubkey->header,
  913. (size_t)(pubkey->end - pubkey->header));
  914. wolfSSL_FreeX509(x509);
  915. if(result) {
  916. failf(data, "SSL: public key does not match pinned public key");
  917. return result;
  918. }
  919. #else
  920. failf(data, "Library lacks pinning support built-in");
  921. return CURLE_NOT_BUILT_IN;
  922. #endif
  923. }
  924. #ifdef HAVE_ALPN
  925. if(connssl->alpn) {
  926. int rc;
  927. char *protocol = NULL;
  928. unsigned short protocol_len = 0;
  929. rc = wolfSSL_ALPN_GetProtocol(backend->handle, &protocol, &protocol_len);
  930. if(rc == SSL_SUCCESS) {
  931. Curl_alpn_set_negotiated(cf, data, (const unsigned char *)protocol,
  932. protocol_len);
  933. }
  934. else if(rc == SSL_ALPN_NOT_FOUND)
  935. Curl_alpn_set_negotiated(cf, data, NULL, 0);
  936. else {
  937. failf(data, "ALPN, failure getting protocol, error %d", rc);
  938. return CURLE_SSL_CONNECT_ERROR;
  939. }
  940. }
  941. #endif /* HAVE_ALPN */
  942. connssl->connecting_state = ssl_connect_3;
  943. #if (LIBWOLFSSL_VERSION_HEX >= 0x03009010)
  944. infof(data, "SSL connection using %s / %s",
  945. wolfSSL_get_version(backend->handle),
  946. wolfSSL_get_cipher_name(backend->handle));
  947. #else
  948. infof(data, "SSL connected");
  949. #endif
  950. return CURLE_OK;
  951. }
  952. static void wolfssl_session_free(void *sessionid, size_t idsize)
  953. {
  954. (void)idsize;
  955. wolfSSL_SESSION_free(sessionid);
  956. }
  957. static CURLcode
  958. wolfssl_connect_step3(struct Curl_cfilter *cf, struct Curl_easy *data)
  959. {
  960. CURLcode result = CURLE_OK;
  961. struct ssl_connect_data *connssl = cf->ctx;
  962. struct wolfssl_ssl_backend_data *backend =
  963. (struct wolfssl_ssl_backend_data *)connssl->backend;
  964. const struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  965. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  966. DEBUGASSERT(backend);
  967. if(ssl_config->primary.sessionid) {
  968. /* wolfSSL_get1_session allocates memory that has to be freed. */
  969. WOLFSSL_SESSION *our_ssl_sessionid = wolfSSL_get1_session(backend->handle);
  970. if(our_ssl_sessionid) {
  971. void *old_ssl_sessionid = NULL;
  972. bool incache;
  973. Curl_ssl_sessionid_lock(data);
  974. incache = !(Curl_ssl_getsessionid(cf, data, &connssl->peer,
  975. &old_ssl_sessionid, NULL));
  976. if(incache) {
  977. Curl_ssl_delsessionid(data, old_ssl_sessionid);
  978. }
  979. /* call takes ownership of `our_ssl_sessionid` */
  980. result = Curl_ssl_addsessionid(cf, data, &connssl->peer,
  981. our_ssl_sessionid, 0,
  982. wolfssl_session_free);
  983. Curl_ssl_sessionid_unlock(data);
  984. if(result) {
  985. failf(data, "failed to store ssl session");
  986. return result;
  987. }
  988. }
  989. }
  990. connssl->connecting_state = ssl_connect_done;
  991. return result;
  992. }
  993. static ssize_t wolfssl_send(struct Curl_cfilter *cf,
  994. struct Curl_easy *data,
  995. const void *mem,
  996. size_t len,
  997. CURLcode *curlcode)
  998. {
  999. struct ssl_connect_data *connssl = cf->ctx;
  1000. struct wolfssl_ssl_backend_data *backend =
  1001. (struct wolfssl_ssl_backend_data *)connssl->backend;
  1002. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  1003. int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  1004. int rc;
  1005. DEBUGASSERT(backend);
  1006. wolfSSL_ERR_clear_error();
  1007. rc = wolfSSL_write(backend->handle, mem, memlen);
  1008. if(rc <= 0) {
  1009. int err = wolfSSL_get_error(backend->handle, rc);
  1010. switch(err) {
  1011. case SSL_ERROR_WANT_READ:
  1012. case SSL_ERROR_WANT_WRITE:
  1013. /* there's data pending, re-invoke SSL_write() */
  1014. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> AGAIN", len);
  1015. *curlcode = CURLE_AGAIN;
  1016. return -1;
  1017. default:
  1018. if(backend->io_result == CURLE_AGAIN) {
  1019. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> AGAIN", len);
  1020. *curlcode = CURLE_AGAIN;
  1021. return -1;
  1022. }
  1023. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> %d, %d", len, rc, err);
  1024. failf(data, "SSL write: %s, errno %d",
  1025. wolfSSL_ERR_error_string(err, error_buffer),
  1026. SOCKERRNO);
  1027. *curlcode = CURLE_SEND_ERROR;
  1028. return -1;
  1029. }
  1030. }
  1031. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> %d", len, rc);
  1032. return rc;
  1033. }
  1034. static void wolfssl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1035. {
  1036. struct ssl_connect_data *connssl = cf->ctx;
  1037. struct wolfssl_ssl_backend_data *backend =
  1038. (struct wolfssl_ssl_backend_data *)connssl->backend;
  1039. (void) data;
  1040. DEBUGASSERT(backend);
  1041. if(backend->handle) {
  1042. char buf[32];
  1043. /* Maybe the server has already sent a close notify alert.
  1044. Read it to avoid an RST on the TCP connection. */
  1045. (void)wolfSSL_read(backend->handle, buf, (int)sizeof(buf));
  1046. if(!connssl->peer_closed)
  1047. (void)wolfSSL_shutdown(backend->handle);
  1048. wolfSSL_free(backend->handle);
  1049. backend->handle = NULL;
  1050. }
  1051. if(backend->ctx) {
  1052. wolfSSL_CTX_free(backend->ctx);
  1053. backend->ctx = NULL;
  1054. }
  1055. }
  1056. static ssize_t wolfssl_recv(struct Curl_cfilter *cf,
  1057. struct Curl_easy *data,
  1058. char *buf, size_t blen,
  1059. CURLcode *curlcode)
  1060. {
  1061. struct ssl_connect_data *connssl = cf->ctx;
  1062. struct wolfssl_ssl_backend_data *backend =
  1063. (struct wolfssl_ssl_backend_data *)connssl->backend;
  1064. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  1065. int buffsize = (blen > (size_t)INT_MAX) ? INT_MAX : (int)blen;
  1066. int nread;
  1067. DEBUGASSERT(backend);
  1068. wolfSSL_ERR_clear_error();
  1069. *curlcode = CURLE_OK;
  1070. nread = wolfSSL_read(backend->handle, buf, buffsize);
  1071. if(nread <= 0) {
  1072. int err = wolfSSL_get_error(backend->handle, nread);
  1073. switch(err) {
  1074. case SSL_ERROR_ZERO_RETURN: /* no more data */
  1075. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> CLOSED", blen);
  1076. *curlcode = CURLE_OK;
  1077. return 0;
  1078. case SSL_ERROR_NONE:
  1079. case SSL_ERROR_WANT_READ:
  1080. case SSL_ERROR_WANT_WRITE:
  1081. /* there's data pending, re-invoke wolfSSL_read() */
  1082. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> AGAIN", blen);
  1083. *curlcode = CURLE_AGAIN;
  1084. return -1;
  1085. default:
  1086. if(backend->io_result == CURLE_AGAIN) {
  1087. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> AGAIN", blen);
  1088. *curlcode = CURLE_AGAIN;
  1089. return -1;
  1090. }
  1091. failf(data, "SSL read: %s, errno %d",
  1092. wolfSSL_ERR_error_string(err, error_buffer), SOCKERRNO);
  1093. *curlcode = CURLE_RECV_ERROR;
  1094. return -1;
  1095. }
  1096. }
  1097. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> %d", blen, nread);
  1098. return nread;
  1099. }
  1100. static size_t wolfssl_version(char *buffer, size_t size)
  1101. {
  1102. #if LIBWOLFSSL_VERSION_HEX >= 0x03006000
  1103. return msnprintf(buffer, size, "wolfSSL/%s", wolfSSL_lib_version());
  1104. #elif defined(WOLFSSL_VERSION)
  1105. return msnprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
  1106. #endif
  1107. }
  1108. static int wolfssl_init(void)
  1109. {
  1110. int ret;
  1111. #ifdef OPENSSL_EXTRA
  1112. Curl_tls_keylog_open();
  1113. #endif
  1114. ret = (wolfSSL_Init() == SSL_SUCCESS);
  1115. wolfssl_bio_cf_init_methods();
  1116. return ret;
  1117. }
  1118. static void wolfssl_cleanup(void)
  1119. {
  1120. wolfssl_bio_cf_free_methods();
  1121. wolfSSL_Cleanup();
  1122. #ifdef OPENSSL_EXTRA
  1123. Curl_tls_keylog_close();
  1124. #endif
  1125. }
  1126. static bool wolfssl_data_pending(struct Curl_cfilter *cf,
  1127. const struct Curl_easy *data)
  1128. {
  1129. struct ssl_connect_data *ctx = cf->ctx;
  1130. struct wolfssl_ssl_backend_data *backend;
  1131. (void)data;
  1132. DEBUGASSERT(ctx && ctx->backend);
  1133. backend = (struct wolfssl_ssl_backend_data *)ctx->backend;
  1134. if(backend->handle) /* SSL is in use */
  1135. return (0 != wolfSSL_pending(backend->handle)) ? TRUE : FALSE;
  1136. else
  1137. return FALSE;
  1138. }
  1139. /*
  1140. * This function is called to shut down the SSL layer but keep the
  1141. * socket open (CCC - Clear Command Channel)
  1142. */
  1143. static int wolfssl_shutdown(struct Curl_cfilter *cf,
  1144. struct Curl_easy *data)
  1145. {
  1146. struct ssl_connect_data *ctx = cf->ctx;
  1147. struct wolfssl_ssl_backend_data *backend;
  1148. int retval = 0;
  1149. (void)data;
  1150. DEBUGASSERT(ctx && ctx->backend);
  1151. backend = (struct wolfssl_ssl_backend_data *)ctx->backend;
  1152. if(backend->handle) {
  1153. wolfSSL_ERR_clear_error();
  1154. wolfSSL_free(backend->handle);
  1155. backend->handle = NULL;
  1156. }
  1157. return retval;
  1158. }
  1159. static CURLcode
  1160. wolfssl_connect_common(struct Curl_cfilter *cf,
  1161. struct Curl_easy *data,
  1162. bool nonblocking,
  1163. bool *done)
  1164. {
  1165. CURLcode result;
  1166. struct ssl_connect_data *connssl = cf->ctx;
  1167. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  1168. int what;
  1169. /* check if the connection has already been established */
  1170. if(ssl_connection_complete == connssl->state) {
  1171. *done = TRUE;
  1172. return CURLE_OK;
  1173. }
  1174. if(ssl_connect_1 == connssl->connecting_state) {
  1175. /* Find out how much more time we're allowed */
  1176. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1177. if(timeout_ms < 0) {
  1178. /* no need to continue if time already is up */
  1179. failf(data, "SSL connection timeout");
  1180. return CURLE_OPERATION_TIMEDOUT;
  1181. }
  1182. result = wolfssl_connect_step1(cf, data);
  1183. if(result)
  1184. return result;
  1185. }
  1186. while(ssl_connect_2 == connssl->connecting_state ||
  1187. ssl_connect_2_reading == connssl->connecting_state ||
  1188. ssl_connect_2_writing == connssl->connecting_state) {
  1189. /* check allowed time left */
  1190. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1191. if(timeout_ms < 0) {
  1192. /* no need to continue if time already is up */
  1193. failf(data, "SSL connection timeout");
  1194. return CURLE_OPERATION_TIMEDOUT;
  1195. }
  1196. /* if ssl is expecting something, check if it's available. */
  1197. if(connssl->connecting_state == ssl_connect_2_reading
  1198. || connssl->connecting_state == ssl_connect_2_writing) {
  1199. curl_socket_t writefd = ssl_connect_2_writing ==
  1200. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  1201. curl_socket_t readfd = ssl_connect_2_reading ==
  1202. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  1203. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  1204. nonblocking?0:timeout_ms);
  1205. if(what < 0) {
  1206. /* fatal error */
  1207. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1208. return CURLE_SSL_CONNECT_ERROR;
  1209. }
  1210. else if(0 == what) {
  1211. if(nonblocking) {
  1212. *done = FALSE;
  1213. return CURLE_OK;
  1214. }
  1215. else {
  1216. /* timeout */
  1217. failf(data, "SSL connection timeout");
  1218. return CURLE_OPERATION_TIMEDOUT;
  1219. }
  1220. }
  1221. /* socket is readable or writable */
  1222. }
  1223. /* Run transaction, and return to the caller if it failed or if
  1224. * this connection is part of a multi handle and this loop would
  1225. * execute again. This permits the owner of a multi handle to
  1226. * abort a connection attempt before step2 has completed while
  1227. * ensuring that a client using select() or epoll() will always
  1228. * have a valid fdset to wait on.
  1229. */
  1230. result = wolfssl_connect_step2(cf, data);
  1231. if(result || (nonblocking &&
  1232. (ssl_connect_2 == connssl->connecting_state ||
  1233. ssl_connect_2_reading == connssl->connecting_state ||
  1234. ssl_connect_2_writing == connssl->connecting_state)))
  1235. return result;
  1236. } /* repeat step2 until all transactions are done. */
  1237. if(ssl_connect_3 == connssl->connecting_state) {
  1238. result = wolfssl_connect_step3(cf, data);
  1239. if(result)
  1240. return result;
  1241. }
  1242. if(ssl_connect_done == connssl->connecting_state) {
  1243. connssl->state = ssl_connection_complete;
  1244. *done = TRUE;
  1245. }
  1246. else
  1247. *done = FALSE;
  1248. /* Reset our connect state machine */
  1249. connssl->connecting_state = ssl_connect_1;
  1250. return CURLE_OK;
  1251. }
  1252. static CURLcode wolfssl_connect_nonblocking(struct Curl_cfilter *cf,
  1253. struct Curl_easy *data,
  1254. bool *done)
  1255. {
  1256. return wolfssl_connect_common(cf, data, TRUE, done);
  1257. }
  1258. static CURLcode wolfssl_connect(struct Curl_cfilter *cf,
  1259. struct Curl_easy *data)
  1260. {
  1261. CURLcode result;
  1262. bool done = FALSE;
  1263. result = wolfssl_connect_common(cf, data, FALSE, &done);
  1264. if(result)
  1265. return result;
  1266. DEBUGASSERT(done);
  1267. return CURLE_OK;
  1268. }
  1269. static CURLcode wolfssl_random(struct Curl_easy *data,
  1270. unsigned char *entropy, size_t length)
  1271. {
  1272. WC_RNG rng;
  1273. (void)data;
  1274. if(wc_InitRng(&rng))
  1275. return CURLE_FAILED_INIT;
  1276. if(length > UINT_MAX)
  1277. return CURLE_FAILED_INIT;
  1278. if(wc_RNG_GenerateBlock(&rng, entropy, (unsigned)length))
  1279. return CURLE_FAILED_INIT;
  1280. if(wc_FreeRng(&rng))
  1281. return CURLE_FAILED_INIT;
  1282. return CURLE_OK;
  1283. }
  1284. static CURLcode wolfssl_sha256sum(const unsigned char *tmp, /* input */
  1285. size_t tmplen,
  1286. unsigned char *sha256sum /* output */,
  1287. size_t unused)
  1288. {
  1289. wc_Sha256 SHA256pw;
  1290. (void)unused;
  1291. if(wc_InitSha256(&SHA256pw))
  1292. return CURLE_FAILED_INIT;
  1293. wc_Sha256Update(&SHA256pw, tmp, (word32)tmplen);
  1294. wc_Sha256Final(&SHA256pw, sha256sum);
  1295. return CURLE_OK;
  1296. }
  1297. static void *wolfssl_get_internals(struct ssl_connect_data *connssl,
  1298. CURLINFO info UNUSED_PARAM)
  1299. {
  1300. struct wolfssl_ssl_backend_data *backend =
  1301. (struct wolfssl_ssl_backend_data *)connssl->backend;
  1302. (void)info;
  1303. DEBUGASSERT(backend);
  1304. return backend->handle;
  1305. }
  1306. const struct Curl_ssl Curl_ssl_wolfssl = {
  1307. { CURLSSLBACKEND_WOLFSSL, "WolfSSL" }, /* info */
  1308. #ifdef KEEP_PEER_CERT
  1309. SSLSUPP_PINNEDPUBKEY |
  1310. #endif
  1311. #ifdef USE_BIO_CHAIN
  1312. SSLSUPP_HTTPS_PROXY |
  1313. #endif
  1314. SSLSUPP_CA_PATH |
  1315. SSLSUPP_CAINFO_BLOB |
  1316. #ifdef USE_ECH
  1317. SSLSUPP_ECH |
  1318. #endif
  1319. SSLSUPP_SSL_CTX,
  1320. sizeof(struct wolfssl_ssl_backend_data),
  1321. wolfssl_init, /* init */
  1322. wolfssl_cleanup, /* cleanup */
  1323. wolfssl_version, /* version */
  1324. Curl_none_check_cxn, /* check_cxn */
  1325. wolfssl_shutdown, /* shutdown */
  1326. wolfssl_data_pending, /* data_pending */
  1327. wolfssl_random, /* random */
  1328. Curl_none_cert_status_request, /* cert_status_request */
  1329. wolfssl_connect, /* connect */
  1330. wolfssl_connect_nonblocking, /* connect_nonblocking */
  1331. Curl_ssl_adjust_pollset, /* adjust_pollset */
  1332. wolfssl_get_internals, /* get_internals */
  1333. wolfssl_close, /* close_one */
  1334. Curl_none_close_all, /* close_all */
  1335. Curl_none_set_engine, /* set_engine */
  1336. Curl_none_set_engine_default, /* set_engine_default */
  1337. Curl_none_engines_list, /* engines_list */
  1338. Curl_none_false_start, /* false_start */
  1339. wolfssl_sha256sum, /* sha256sum */
  1340. NULL, /* associate_connection */
  1341. NULL, /* disassociate_connection */
  1342. NULL, /* free_multi_ssl_backend_data */
  1343. wolfssl_recv, /* recv decrypted data */
  1344. wolfssl_send, /* send data to encrypt */
  1345. };
  1346. #endif