wolfssl.c 43 KB

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