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. break;
  433. }
  434. ciphers = conn_config->cipher_list;
  435. if(ciphers) {
  436. if(!SSL_CTX_set_cipher_list(backend->ctx, ciphers)) {
  437. failf(data, "failed setting cipher list: %s", ciphers);
  438. return CURLE_SSL_CIPHER;
  439. }
  440. infof(data, "Cipher selection: %s", ciphers);
  441. }
  442. curves = conn_config->curves;
  443. if(curves) {
  444. #ifdef HAVE_LIBOQS
  445. for(idx = 0; gnm[idx].name != NULL; idx++) {
  446. if(strncmp(curves, gnm[idx].name, strlen(gnm[idx].name)) == 0) {
  447. oqsAlg = gnm[idx].group;
  448. break;
  449. }
  450. }
  451. if(oqsAlg == 0)
  452. #endif
  453. {
  454. if(!SSL_CTX_set1_curves_list(backend->ctx, curves)) {
  455. failf(data, "failed setting curves list: '%s'", curves);
  456. return CURLE_SSL_CIPHER;
  457. }
  458. }
  459. }
  460. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SYS_CA_CERTS)
  461. /* load native CA certificates */
  462. if(ssl_config->native_ca_store) {
  463. if(wolfSSL_CTX_load_system_CA_certs(backend->ctx) != WOLFSSL_SUCCESS) {
  464. infof(data, "error importing native CA store, continuing anyway");
  465. }
  466. else {
  467. imported_native_ca = true;
  468. infof(data, "successfully imported native CA store");
  469. }
  470. }
  471. #endif /* !NO_FILESYSTEM */
  472. /* load certificate blob */
  473. if(ca_info_blob) {
  474. if(wolfSSL_CTX_load_verify_buffer(backend->ctx, ca_info_blob->data,
  475. ca_info_blob->len,
  476. SSL_FILETYPE_PEM) != SSL_SUCCESS) {
  477. if(imported_native_ca) {
  478. infof(data, "error importing CA certificate blob, continuing anyway");
  479. }
  480. else {
  481. failf(data, "error importing CA certificate blob");
  482. return CURLE_SSL_CACERT_BADFILE;
  483. }
  484. }
  485. else {
  486. imported_ca_info_blob = true;
  487. infof(data, "successfully imported CA certificate blob");
  488. }
  489. }
  490. #ifndef NO_FILESYSTEM
  491. /* load trusted cacert from file if not blob */
  492. if(ssl_cafile || ssl_capath) {
  493. int rc =
  494. wolfSSL_CTX_load_verify_locations_ex(backend->ctx,
  495. ssl_cafile,
  496. ssl_capath,
  497. WOLFSSL_LOAD_FLAG_IGNORE_ERR);
  498. if(SSL_SUCCESS != rc) {
  499. if(conn_config->verifypeer && !imported_ca_info_blob &&
  500. !imported_native_ca) {
  501. /* Fail if we insist on successfully verifying the server. */
  502. failf(data, "error setting certificate verify locations:"
  503. " CAfile: %s CApath: %s",
  504. ssl_cafile ? ssl_cafile : "none",
  505. ssl_capath ? ssl_capath : "none");
  506. return CURLE_SSL_CACERT_BADFILE;
  507. }
  508. else {
  509. /* Just continue with a warning if no strict certificate
  510. verification is required. */
  511. infof(data, "error setting certificate verify locations,"
  512. " continuing anyway:");
  513. }
  514. }
  515. else {
  516. /* Everything is fine. */
  517. infof(data, "successfully set certificate verify locations:");
  518. }
  519. infof(data, " CAfile: %s", ssl_cafile ? ssl_cafile : "none");
  520. infof(data, " CApath: %s", ssl_capath ? ssl_capath : "none");
  521. }
  522. /* Load the client certificate, and private key */
  523. if(ssl_config->primary.clientcert && ssl_config->key) {
  524. int file_type = do_file_type(ssl_config->cert_type);
  525. if(wolfSSL_CTX_use_certificate_file(backend->ctx,
  526. ssl_config->primary.clientcert,
  527. file_type) != 1) {
  528. failf(data, "unable to use client certificate (no key or wrong pass"
  529. " phrase?)");
  530. return CURLE_SSL_CONNECT_ERROR;
  531. }
  532. file_type = do_file_type(ssl_config->key_type);
  533. if(wolfSSL_CTX_use_PrivateKey_file(backend->ctx, ssl_config->key,
  534. file_type) != 1) {
  535. failf(data, "unable to set private key");
  536. return CURLE_SSL_CONNECT_ERROR;
  537. }
  538. }
  539. #endif /* !NO_FILESYSTEM */
  540. /* SSL always tries to verify the peer, this only says whether it should
  541. * fail to connect if the verification fails, or if it should continue
  542. * anyway. In the latter case the result of the verification is checked with
  543. * SSL_get_verify_result() below. */
  544. wolfSSL_CTX_set_verify(backend->ctx,
  545. conn_config->verifypeer?SSL_VERIFY_PEER:
  546. SSL_VERIFY_NONE, NULL);
  547. #ifdef HAVE_SNI
  548. if(sni) {
  549. struct in_addr addr4;
  550. #ifdef ENABLE_IPV6
  551. struct in6_addr addr6;
  552. #endif
  553. size_t hostname_len = strlen(connssl->hostname);
  554. if((hostname_len < USHRT_MAX) &&
  555. !Curl_inet_pton(AF_INET, connssl->hostname, &addr4)
  556. #ifdef ENABLE_IPV6
  557. && !Curl_inet_pton(AF_INET6, connssl->hostname, &addr6)
  558. #endif
  559. ) {
  560. size_t snilen;
  561. char *snihost = Curl_ssl_snihost(data, connssl->hostname, &snilen);
  562. if(!snihost ||
  563. wolfSSL_CTX_UseSNI(backend->ctx, WOLFSSL_SNI_HOST_NAME, snihost,
  564. (unsigned short)snilen) != 1) {
  565. failf(data, "Failed to set SNI");
  566. return CURLE_SSL_CONNECT_ERROR;
  567. }
  568. }
  569. }
  570. #endif
  571. /* give application a chance to interfere with SSL set up. */
  572. if(data->set.ssl.fsslctx) {
  573. CURLcode result = (*data->set.ssl.fsslctx)(data, backend->ctx,
  574. data->set.ssl.fsslctxp);
  575. if(result) {
  576. failf(data, "error signaled by ssl ctx callback");
  577. return result;
  578. }
  579. }
  580. #ifdef NO_FILESYSTEM
  581. else if(conn_config->verifypeer) {
  582. failf(data, "SSL: Certificates can't be loaded because wolfSSL was built"
  583. " with \"no filesystem\". Either disable peer verification"
  584. " (insecure) or if you are building an application with libcurl you"
  585. " can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
  586. return CURLE_SSL_CONNECT_ERROR;
  587. }
  588. #endif
  589. /* Let's make an SSL structure */
  590. if(backend->handle)
  591. wolfSSL_free(backend->handle);
  592. backend->handle = wolfSSL_new(backend->ctx);
  593. if(!backend->handle) {
  594. failf(data, "SSL: couldn't create a handle");
  595. return CURLE_OUT_OF_MEMORY;
  596. }
  597. #ifdef HAVE_LIBOQS
  598. if(oqsAlg) {
  599. if(wolfSSL_UseKeyShare(backend->handle, oqsAlg) != WOLFSSL_SUCCESS) {
  600. failf(data, "unable to use oqs KEM");
  601. }
  602. }
  603. #endif
  604. #ifdef HAVE_ALPN
  605. if(connssl->alpn) {
  606. struct alpn_proto_buf proto;
  607. CURLcode result;
  608. result = Curl_alpn_to_proto_str(&proto, connssl->alpn);
  609. if(result ||
  610. wolfSSL_UseALPN(backend->handle, (char *)proto.data, proto.len,
  611. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) != SSL_SUCCESS) {
  612. failf(data, "SSL: failed setting ALPN protocols");
  613. return CURLE_SSL_CONNECT_ERROR;
  614. }
  615. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  616. }
  617. #endif /* HAVE_ALPN */
  618. #ifdef OPENSSL_EXTRA
  619. if(Curl_tls_keylog_enabled()) {
  620. /* Ensure the Client Random is preserved. */
  621. wolfSSL_KeepArrays(backend->handle);
  622. #if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
  623. wolfSSL_set_tls13_secret_cb(backend->handle,
  624. wolfssl_tls13_secret_callback, NULL);
  625. #endif
  626. }
  627. #endif /* OPENSSL_EXTRA */
  628. #ifdef HAVE_SECURE_RENEGOTIATION
  629. if(wolfSSL_UseSecureRenegotiation(backend->handle) != SSL_SUCCESS) {
  630. failf(data, "SSL: failed setting secure renegotiation");
  631. return CURLE_SSL_CONNECT_ERROR;
  632. }
  633. #endif /* HAVE_SECURE_RENEGOTIATION */
  634. /* Check if there's a cached ID we can/should use here! */
  635. if(ssl_config->primary.sessionid) {
  636. void *ssl_sessionid = NULL;
  637. Curl_ssl_sessionid_lock(data);
  638. if(!Curl_ssl_getsessionid(cf, data, &ssl_sessionid, NULL)) {
  639. /* we got a session id, use it! */
  640. if(!SSL_set_session(backend->handle, ssl_sessionid)) {
  641. Curl_ssl_delsessionid(data, ssl_sessionid);
  642. infof(data, "Can't use session ID, going on without");
  643. }
  644. else
  645. infof(data, "SSL reusing session ID");
  646. }
  647. Curl_ssl_sessionid_unlock(data);
  648. }
  649. #ifdef USE_BIO_CHAIN
  650. {
  651. WOLFSSL_BIO *bio;
  652. bio = BIO_new(wolfssl_bio_cf_method);
  653. if(!bio)
  654. return CURLE_OUT_OF_MEMORY;
  655. wolfSSL_BIO_set_data(bio, cf);
  656. wolfSSL_set_bio(backend->handle, bio, bio);
  657. }
  658. #else /* USE_BIO_CHAIN */
  659. /* pass the raw socket into the SSL layer */
  660. if(!wolfSSL_set_fd(backend->handle,
  661. (int)Curl_conn_cf_get_socket(cf, data))) {
  662. failf(data, "SSL: SSL_set_fd failed");
  663. return CURLE_SSL_CONNECT_ERROR;
  664. }
  665. #endif /* !USE_BIO_CHAIN */
  666. connssl->connecting_state = ssl_connect_2;
  667. return CURLE_OK;
  668. }
  669. static CURLcode
  670. wolfssl_connect_step2(struct Curl_cfilter *cf, struct Curl_easy *data)
  671. {
  672. int ret = -1;
  673. struct ssl_connect_data *connssl = cf->ctx;
  674. struct wolfssl_ssl_backend_data *backend =
  675. (struct wolfssl_ssl_backend_data *)connssl->backend;
  676. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  677. const char * const pinnedpubkey = Curl_ssl_cf_is_proxy(cf)?
  678. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
  679. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  680. DEBUGASSERT(backend);
  681. wolfSSL_ERR_clear_error();
  682. /* Enable RFC2818 checks */
  683. if(conn_config->verifyhost) {
  684. char *snihost = Curl_ssl_snihost(data, connssl->hostname, NULL);
  685. if(!snihost ||
  686. (wolfSSL_check_domain_name(backend->handle, snihost) == SSL_FAILURE))
  687. return CURLE_SSL_CONNECT_ERROR;
  688. }
  689. ret = wolfSSL_connect(backend->handle);
  690. #ifdef OPENSSL_EXTRA
  691. if(Curl_tls_keylog_enabled()) {
  692. /* If key logging is enabled, wait for the handshake to complete and then
  693. * proceed with logging secrets (for TLS 1.2 or older).
  694. *
  695. * During the handshake (ret==-1), wolfSSL_want_read() is true as it waits
  696. * for the server response. At that point the master secret is not yet
  697. * available, so we must not try to read it.
  698. * To log the secret on completion with a handshake failure, detect
  699. * completion via the observation that there is nothing to read or write.
  700. * Note that OpenSSL SSL_want_read() is always true here. If wolfSSL ever
  701. * changes, the worst case is that no key is logged on error.
  702. */
  703. if(ret == SSL_SUCCESS ||
  704. (!wolfSSL_want_read(backend->handle) &&
  705. !wolfSSL_want_write(backend->handle))) {
  706. wolfssl_log_tls12_secret(backend->handle);
  707. /* Client Random and master secrets are no longer needed, erase these.
  708. * Ignored while the handshake is still in progress. */
  709. wolfSSL_FreeArrays(backend->handle);
  710. }
  711. }
  712. #endif /* OPENSSL_EXTRA */
  713. if(ret != 1) {
  714. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  715. int detail = wolfSSL_get_error(backend->handle, ret);
  716. if(SSL_ERROR_WANT_READ == detail) {
  717. connssl->connecting_state = ssl_connect_2_reading;
  718. return CURLE_OK;
  719. }
  720. else if(SSL_ERROR_WANT_WRITE == detail) {
  721. connssl->connecting_state = ssl_connect_2_writing;
  722. return CURLE_OK;
  723. }
  724. /* There is no easy way to override only the CN matching.
  725. * This will enable the override of both mismatching SubjectAltNames
  726. * as also mismatching CN fields */
  727. else if(DOMAIN_NAME_MISMATCH == detail) {
  728. #if 1
  729. failf(data, " subject alt name(s) or common name do not match \"%s\"",
  730. connssl->dispname);
  731. return CURLE_PEER_FAILED_VERIFICATION;
  732. #else
  733. /* When the wolfssl_check_domain_name() is used and you desire to
  734. * continue on a DOMAIN_NAME_MISMATCH, i.e. 'ssl_config.verifyhost
  735. * == 0', CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA
  736. * error. The only way to do this is currently to switch the
  737. * Wolfssl_check_domain_name() in and out based on the
  738. * 'ssl_config.verifyhost' value. */
  739. if(conn_config->verifyhost) {
  740. failf(data,
  741. " subject alt name(s) or common name do not match \"%s\"\n",
  742. connssl->dispname);
  743. return CURLE_PEER_FAILED_VERIFICATION;
  744. }
  745. else {
  746. infof(data,
  747. " subject alt name(s) and/or common name do not match \"%s\"",
  748. connssl->dispname);
  749. return CURLE_OK;
  750. }
  751. #endif
  752. }
  753. #if LIBWOLFSSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
  754. else if(ASN_NO_SIGNER_E == detail) {
  755. if(conn_config->verifypeer) {
  756. failf(data, " CA signer not available for verification");
  757. return CURLE_SSL_CACERT_BADFILE;
  758. }
  759. else {
  760. /* Just continue with a warning if no strict certificate
  761. verification is required. */
  762. infof(data, "CA signer not available for verification, "
  763. "continuing anyway");
  764. }
  765. }
  766. #endif
  767. else if(backend->io_result == CURLE_AGAIN) {
  768. return CURLE_OK;
  769. }
  770. else {
  771. failf(data, "SSL_connect failed with error %d: %s", detail,
  772. wolfSSL_ERR_error_string(detail, error_buffer));
  773. return CURLE_SSL_CONNECT_ERROR;
  774. }
  775. }
  776. if(pinnedpubkey) {
  777. #ifdef KEEP_PEER_CERT
  778. X509 *x509;
  779. const char *x509_der;
  780. int x509_der_len;
  781. struct Curl_X509certificate x509_parsed;
  782. struct Curl_asn1Element *pubkey;
  783. CURLcode result;
  784. x509 = wolfSSL_get_peer_certificate(backend->handle);
  785. if(!x509) {
  786. failf(data, "SSL: failed retrieving server certificate");
  787. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  788. }
  789. x509_der = (const char *)wolfSSL_X509_get_der(x509, &x509_der_len);
  790. if(!x509_der) {
  791. failf(data, "SSL: failed retrieving ASN.1 server certificate");
  792. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  793. }
  794. memset(&x509_parsed, 0, sizeof(x509_parsed));
  795. if(Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len))
  796. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  797. pubkey = &x509_parsed.subjectPublicKeyInfo;
  798. if(!pubkey->header || pubkey->end <= pubkey->header) {
  799. failf(data, "SSL: failed retrieving public key from server certificate");
  800. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  801. }
  802. result = Curl_pin_peer_pubkey(data,
  803. pinnedpubkey,
  804. (const unsigned char *)pubkey->header,
  805. (size_t)(pubkey->end - pubkey->header));
  806. if(result) {
  807. failf(data, "SSL: public key does not match pinned public key");
  808. return result;
  809. }
  810. #else
  811. failf(data, "Library lacks pinning support built-in");
  812. return CURLE_NOT_BUILT_IN;
  813. #endif
  814. }
  815. #ifdef HAVE_ALPN
  816. if(connssl->alpn) {
  817. int rc;
  818. char *protocol = NULL;
  819. unsigned short protocol_len = 0;
  820. rc = wolfSSL_ALPN_GetProtocol(backend->handle, &protocol, &protocol_len);
  821. if(rc == SSL_SUCCESS) {
  822. Curl_alpn_set_negotiated(cf, data, (const unsigned char *)protocol,
  823. protocol_len);
  824. }
  825. else if(rc == SSL_ALPN_NOT_FOUND)
  826. Curl_alpn_set_negotiated(cf, data, NULL, 0);
  827. else {
  828. failf(data, "ALPN, failure getting protocol, error %d", rc);
  829. return CURLE_SSL_CONNECT_ERROR;
  830. }
  831. }
  832. #endif /* HAVE_ALPN */
  833. connssl->connecting_state = ssl_connect_3;
  834. #if (LIBWOLFSSL_VERSION_HEX >= 0x03009010)
  835. infof(data, "SSL connection using %s / %s",
  836. wolfSSL_get_version(backend->handle),
  837. wolfSSL_get_cipher_name(backend->handle));
  838. #else
  839. infof(data, "SSL connected");
  840. #endif
  841. return CURLE_OK;
  842. }
  843. static CURLcode
  844. wolfssl_connect_step3(struct Curl_cfilter *cf, struct Curl_easy *data)
  845. {
  846. CURLcode result = CURLE_OK;
  847. struct ssl_connect_data *connssl = cf->ctx;
  848. struct wolfssl_ssl_backend_data *backend =
  849. (struct wolfssl_ssl_backend_data *)connssl->backend;
  850. const struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  851. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  852. DEBUGASSERT(backend);
  853. if(ssl_config->primary.sessionid) {
  854. bool incache;
  855. bool added = FALSE;
  856. void *old_ssl_sessionid = NULL;
  857. /* wolfSSL_get1_session allocates memory that has to be freed. */
  858. WOLFSSL_SESSION *our_ssl_sessionid = wolfSSL_get1_session(backend->handle);
  859. if(our_ssl_sessionid) {
  860. Curl_ssl_sessionid_lock(data);
  861. incache = !(Curl_ssl_getsessionid(cf, data, &old_ssl_sessionid, NULL));
  862. if(incache) {
  863. if(old_ssl_sessionid != our_ssl_sessionid) {
  864. infof(data, "old SSL session ID is stale, removing");
  865. Curl_ssl_delsessionid(data, old_ssl_sessionid);
  866. incache = FALSE;
  867. }
  868. }
  869. if(!incache) {
  870. result = Curl_ssl_addsessionid(cf, data, our_ssl_sessionid, 0, NULL);
  871. if(result) {
  872. Curl_ssl_sessionid_unlock(data);
  873. wolfSSL_SESSION_free(our_ssl_sessionid);
  874. failf(data, "failed to store ssl session");
  875. return result;
  876. }
  877. else {
  878. added = TRUE;
  879. }
  880. }
  881. Curl_ssl_sessionid_unlock(data);
  882. if(!added) {
  883. /* If the session info wasn't added to the cache, free our copy. */
  884. wolfSSL_SESSION_free(our_ssl_sessionid);
  885. }
  886. }
  887. }
  888. connssl->connecting_state = ssl_connect_done;
  889. return result;
  890. }
  891. static ssize_t wolfssl_send(struct Curl_cfilter *cf,
  892. struct Curl_easy *data,
  893. const void *mem,
  894. size_t len,
  895. CURLcode *curlcode)
  896. {
  897. struct ssl_connect_data *connssl = cf->ctx;
  898. struct wolfssl_ssl_backend_data *backend =
  899. (struct wolfssl_ssl_backend_data *)connssl->backend;
  900. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  901. int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  902. int rc;
  903. DEBUGASSERT(backend);
  904. wolfSSL_ERR_clear_error();
  905. rc = wolfSSL_write(backend->handle, mem, memlen);
  906. if(rc <= 0) {
  907. int err = wolfSSL_get_error(backend->handle, rc);
  908. switch(err) {
  909. case SSL_ERROR_WANT_READ:
  910. case SSL_ERROR_WANT_WRITE:
  911. /* there's data pending, re-invoke SSL_write() */
  912. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> AGAIN", len);
  913. *curlcode = CURLE_AGAIN;
  914. return -1;
  915. default:
  916. if(backend->io_result == CURLE_AGAIN) {
  917. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> AGAIN", len);
  918. *curlcode = CURLE_AGAIN;
  919. return -1;
  920. }
  921. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> %d, %d", len, rc, err);
  922. failf(data, "SSL write: %s, errno %d",
  923. wolfSSL_ERR_error_string(err, error_buffer),
  924. SOCKERRNO);
  925. *curlcode = CURLE_SEND_ERROR;
  926. return -1;
  927. }
  928. }
  929. CURL_TRC_CF(data, cf, "wolfssl_send(len=%zu) -> %d", len, rc);
  930. return rc;
  931. }
  932. static void wolfssl_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  933. {
  934. struct ssl_connect_data *connssl = cf->ctx;
  935. struct wolfssl_ssl_backend_data *backend =
  936. (struct wolfssl_ssl_backend_data *)connssl->backend;
  937. (void) data;
  938. DEBUGASSERT(backend);
  939. if(backend->handle) {
  940. char buf[32];
  941. /* Maybe the server has already sent a close notify alert.
  942. Read it to avoid an RST on the TCP connection. */
  943. (void)wolfSSL_read(backend->handle, buf, (int)sizeof(buf));
  944. (void)wolfSSL_shutdown(backend->handle);
  945. wolfSSL_free(backend->handle);
  946. backend->handle = NULL;
  947. }
  948. if(backend->ctx) {
  949. wolfSSL_CTX_free(backend->ctx);
  950. backend->ctx = NULL;
  951. }
  952. }
  953. static ssize_t wolfssl_recv(struct Curl_cfilter *cf,
  954. struct Curl_easy *data,
  955. char *buf, size_t blen,
  956. CURLcode *curlcode)
  957. {
  958. struct ssl_connect_data *connssl = cf->ctx;
  959. struct wolfssl_ssl_backend_data *backend =
  960. (struct wolfssl_ssl_backend_data *)connssl->backend;
  961. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  962. int buffsize = (blen > (size_t)INT_MAX) ? INT_MAX : (int)blen;
  963. int nread;
  964. DEBUGASSERT(backend);
  965. wolfSSL_ERR_clear_error();
  966. *curlcode = CURLE_OK;
  967. nread = wolfSSL_read(backend->handle, buf, buffsize);
  968. if(nread <= 0) {
  969. int err = wolfSSL_get_error(backend->handle, nread);
  970. switch(err) {
  971. case SSL_ERROR_ZERO_RETURN: /* no more data */
  972. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> CLOSED", blen);
  973. *curlcode = CURLE_OK;
  974. return 0;
  975. case SSL_ERROR_NONE:
  976. /* FALLTHROUGH */
  977. case SSL_ERROR_WANT_READ:
  978. /* FALLTHROUGH */
  979. case SSL_ERROR_WANT_WRITE:
  980. /* there's data pending, re-invoke wolfSSL_read() */
  981. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> AGAIN", blen);
  982. *curlcode = CURLE_AGAIN;
  983. return -1;
  984. default:
  985. if(backend->io_result == CURLE_AGAIN) {
  986. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> AGAIN", blen);
  987. *curlcode = CURLE_AGAIN;
  988. return -1;
  989. }
  990. failf(data, "SSL read: %s, errno %d",
  991. wolfSSL_ERR_error_string(err, error_buffer), SOCKERRNO);
  992. *curlcode = CURLE_RECV_ERROR;
  993. return -1;
  994. }
  995. }
  996. CURL_TRC_CF(data, cf, "wolfssl_recv(len=%zu) -> %d", blen, nread);
  997. return nread;
  998. }
  999. static void wolfssl_session_free(void *ptr)
  1000. {
  1001. wolfSSL_SESSION_free(ptr);
  1002. }
  1003. static size_t wolfssl_version(char *buffer, size_t size)
  1004. {
  1005. #if LIBWOLFSSL_VERSION_HEX >= 0x03006000
  1006. return msnprintf(buffer, size, "wolfSSL/%s", wolfSSL_lib_version());
  1007. #elif defined(WOLFSSL_VERSION)
  1008. return msnprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
  1009. #endif
  1010. }
  1011. static int wolfssl_init(void)
  1012. {
  1013. int ret;
  1014. #ifdef OPENSSL_EXTRA
  1015. Curl_tls_keylog_open();
  1016. #endif
  1017. ret = (wolfSSL_Init() == SSL_SUCCESS);
  1018. wolfssl_bio_cf_init_methods();
  1019. return ret;
  1020. }
  1021. static void wolfssl_cleanup(void)
  1022. {
  1023. wolfssl_bio_cf_free_methods();
  1024. wolfSSL_Cleanup();
  1025. #ifdef OPENSSL_EXTRA
  1026. Curl_tls_keylog_close();
  1027. #endif
  1028. }
  1029. static bool wolfssl_data_pending(struct Curl_cfilter *cf,
  1030. const struct Curl_easy *data)
  1031. {
  1032. struct ssl_connect_data *ctx = cf->ctx;
  1033. struct wolfssl_ssl_backend_data *backend;
  1034. (void)data;
  1035. DEBUGASSERT(ctx && ctx->backend);
  1036. backend = (struct wolfssl_ssl_backend_data *)ctx->backend;
  1037. if(backend->handle) /* SSL is in use */
  1038. return (0 != wolfSSL_pending(backend->handle)) ? TRUE : FALSE;
  1039. else
  1040. return FALSE;
  1041. }
  1042. /*
  1043. * This function is called to shut down the SSL layer but keep the
  1044. * socket open (CCC - Clear Command Channel)
  1045. */
  1046. static int wolfssl_shutdown(struct Curl_cfilter *cf,
  1047. struct Curl_easy *data)
  1048. {
  1049. struct ssl_connect_data *ctx = cf->ctx;
  1050. struct wolfssl_ssl_backend_data *backend;
  1051. int retval = 0;
  1052. (void)data;
  1053. DEBUGASSERT(ctx && ctx->backend);
  1054. backend = (struct wolfssl_ssl_backend_data *)ctx->backend;
  1055. if(backend->handle) {
  1056. wolfSSL_ERR_clear_error();
  1057. wolfSSL_free(backend->handle);
  1058. backend->handle = NULL;
  1059. }
  1060. return retval;
  1061. }
  1062. static CURLcode
  1063. wolfssl_connect_common(struct Curl_cfilter *cf,
  1064. struct Curl_easy *data,
  1065. bool nonblocking,
  1066. bool *done)
  1067. {
  1068. CURLcode result;
  1069. struct ssl_connect_data *connssl = cf->ctx;
  1070. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  1071. int what;
  1072. /* check if the connection has already been established */
  1073. if(ssl_connection_complete == connssl->state) {
  1074. *done = TRUE;
  1075. return CURLE_OK;
  1076. }
  1077. if(ssl_connect_1 == connssl->connecting_state) {
  1078. /* Find out how much more time we're allowed */
  1079. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1080. if(timeout_ms < 0) {
  1081. /* no need to continue if time already is up */
  1082. failf(data, "SSL connection timeout");
  1083. return CURLE_OPERATION_TIMEDOUT;
  1084. }
  1085. result = wolfssl_connect_step1(cf, data);
  1086. if(result)
  1087. return result;
  1088. }
  1089. while(ssl_connect_2 == connssl->connecting_state ||
  1090. ssl_connect_2_reading == connssl->connecting_state ||
  1091. ssl_connect_2_writing == connssl->connecting_state) {
  1092. /* check allowed time left */
  1093. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1094. if(timeout_ms < 0) {
  1095. /* no need to continue if time already is up */
  1096. failf(data, "SSL connection timeout");
  1097. return CURLE_OPERATION_TIMEDOUT;
  1098. }
  1099. /* if ssl is expecting something, check if it's available. */
  1100. if(connssl->connecting_state == ssl_connect_2_reading
  1101. || connssl->connecting_state == ssl_connect_2_writing) {
  1102. curl_socket_t writefd = ssl_connect_2_writing ==
  1103. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  1104. curl_socket_t readfd = ssl_connect_2_reading ==
  1105. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  1106. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  1107. nonblocking?0:timeout_ms);
  1108. if(what < 0) {
  1109. /* fatal error */
  1110. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1111. return CURLE_SSL_CONNECT_ERROR;
  1112. }
  1113. else if(0 == what) {
  1114. if(nonblocking) {
  1115. *done = FALSE;
  1116. return CURLE_OK;
  1117. }
  1118. else {
  1119. /* timeout */
  1120. failf(data, "SSL connection timeout");
  1121. return CURLE_OPERATION_TIMEDOUT;
  1122. }
  1123. }
  1124. /* socket is readable or writable */
  1125. }
  1126. /* Run transaction, and return to the caller if it failed or if
  1127. * this connection is part of a multi handle and this loop would
  1128. * execute again. This permits the owner of a multi handle to
  1129. * abort a connection attempt before step2 has completed while
  1130. * ensuring that a client using select() or epoll() will always
  1131. * have a valid fdset to wait on.
  1132. */
  1133. result = wolfssl_connect_step2(cf, data);
  1134. if(result || (nonblocking &&
  1135. (ssl_connect_2 == connssl->connecting_state ||
  1136. ssl_connect_2_reading == connssl->connecting_state ||
  1137. ssl_connect_2_writing == connssl->connecting_state)))
  1138. return result;
  1139. } /* repeat step2 until all transactions are done. */
  1140. if(ssl_connect_3 == connssl->connecting_state) {
  1141. result = wolfssl_connect_step3(cf, data);
  1142. if(result)
  1143. return result;
  1144. }
  1145. if(ssl_connect_done == connssl->connecting_state) {
  1146. connssl->state = ssl_connection_complete;
  1147. *done = TRUE;
  1148. }
  1149. else
  1150. *done = FALSE;
  1151. /* Reset our connect state machine */
  1152. connssl->connecting_state = ssl_connect_1;
  1153. return CURLE_OK;
  1154. }
  1155. static CURLcode wolfssl_connect_nonblocking(struct Curl_cfilter *cf,
  1156. struct Curl_easy *data,
  1157. bool *done)
  1158. {
  1159. return wolfssl_connect_common(cf, data, TRUE, done);
  1160. }
  1161. static CURLcode wolfssl_connect(struct Curl_cfilter *cf,
  1162. struct Curl_easy *data)
  1163. {
  1164. CURLcode result;
  1165. bool done = FALSE;
  1166. result = wolfssl_connect_common(cf, data, FALSE, &done);
  1167. if(result)
  1168. return result;
  1169. DEBUGASSERT(done);
  1170. return CURLE_OK;
  1171. }
  1172. static CURLcode wolfssl_random(struct Curl_easy *data,
  1173. unsigned char *entropy, size_t length)
  1174. {
  1175. WC_RNG rng;
  1176. (void)data;
  1177. if(wc_InitRng(&rng))
  1178. return CURLE_FAILED_INIT;
  1179. if(length > UINT_MAX)
  1180. return CURLE_FAILED_INIT;
  1181. if(wc_RNG_GenerateBlock(&rng, entropy, (unsigned)length))
  1182. return CURLE_FAILED_INIT;
  1183. if(wc_FreeRng(&rng))
  1184. return CURLE_FAILED_INIT;
  1185. return CURLE_OK;
  1186. }
  1187. static CURLcode wolfssl_sha256sum(const unsigned char *tmp, /* input */
  1188. size_t tmplen,
  1189. unsigned char *sha256sum /* output */,
  1190. size_t unused)
  1191. {
  1192. wc_Sha256 SHA256pw;
  1193. (void)unused;
  1194. if(wc_InitSha256(&SHA256pw))
  1195. return CURLE_FAILED_INIT;
  1196. wc_Sha256Update(&SHA256pw, tmp, (word32)tmplen);
  1197. wc_Sha256Final(&SHA256pw, sha256sum);
  1198. return CURLE_OK;
  1199. }
  1200. static void *wolfssl_get_internals(struct ssl_connect_data *connssl,
  1201. CURLINFO info UNUSED_PARAM)
  1202. {
  1203. struct wolfssl_ssl_backend_data *backend =
  1204. (struct wolfssl_ssl_backend_data *)connssl->backend;
  1205. (void)info;
  1206. DEBUGASSERT(backend);
  1207. return backend->handle;
  1208. }
  1209. const struct Curl_ssl Curl_ssl_wolfssl = {
  1210. { CURLSSLBACKEND_WOLFSSL, "WolfSSL" }, /* info */
  1211. #ifdef KEEP_PEER_CERT
  1212. SSLSUPP_PINNEDPUBKEY |
  1213. #endif
  1214. #ifdef USE_BIO_CHAIN
  1215. SSLSUPP_HTTPS_PROXY |
  1216. #endif
  1217. SSLSUPP_CA_PATH |
  1218. SSLSUPP_CAINFO_BLOB |
  1219. SSLSUPP_SSL_CTX,
  1220. sizeof(struct wolfssl_ssl_backend_data),
  1221. wolfssl_init, /* init */
  1222. wolfssl_cleanup, /* cleanup */
  1223. wolfssl_version, /* version */
  1224. Curl_none_check_cxn, /* check_cxn */
  1225. wolfssl_shutdown, /* shutdown */
  1226. wolfssl_data_pending, /* data_pending */
  1227. wolfssl_random, /* random */
  1228. Curl_none_cert_status_request, /* cert_status_request */
  1229. wolfssl_connect, /* connect */
  1230. wolfssl_connect_nonblocking, /* connect_nonblocking */
  1231. Curl_ssl_get_select_socks, /* getsock */
  1232. wolfssl_get_internals, /* get_internals */
  1233. wolfssl_close, /* close_one */
  1234. Curl_none_close_all, /* close_all */
  1235. wolfssl_session_free, /* session_free */
  1236. Curl_none_set_engine, /* set_engine */
  1237. Curl_none_set_engine_default, /* set_engine_default */
  1238. Curl_none_engines_list, /* engines_list */
  1239. Curl_none_false_start, /* false_start */
  1240. wolfssl_sha256sum, /* sha256sum */
  1241. NULL, /* associate_connection */
  1242. NULL, /* disassociate_connection */
  1243. NULL, /* free_multi_ssl_backend_data */
  1244. wolfssl_recv, /* recv decrypted data */
  1245. wolfssl_send, /* send data to encrypt */
  1246. };
  1247. #endif