wolfssl.c 42 KB

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