wolfssl.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, 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 "keylog.h"
  51. #include "parsedate.h"
  52. #include "connect.h" /* for the connect timeout */
  53. #include "select.h"
  54. #include "strcase.h"
  55. #include "x509asn1.h"
  56. #include "curl_printf.h"
  57. #include "multiif.h"
  58. #include <wolfssl/openssl/ssl.h>
  59. #include <wolfssl/ssl.h>
  60. #include <wolfssl/error-ssl.h>
  61. #include "wolfssl.h"
  62. /* The last #include files should be: */
  63. #include "curl_memory.h"
  64. #include "memdebug.h"
  65. /* KEEP_PEER_CERT is a product of the presence of build time symbol
  66. OPENSSL_EXTRA without NO_CERTS, depending on the version. KEEP_PEER_CERT is
  67. in wolfSSL's settings.h, and the latter two are build time symbols in
  68. options.h. */
  69. #ifndef KEEP_PEER_CERT
  70. #if defined(HAVE_WOLFSSL_GET_PEER_CERTIFICATE) || \
  71. (defined(OPENSSL_EXTRA) && !defined(NO_CERTS))
  72. #define KEEP_PEER_CERT
  73. #endif
  74. #endif
  75. struct ssl_backend_data {
  76. SSL_CTX* ctx;
  77. SSL* handle;
  78. };
  79. static Curl_recv wolfssl_recv;
  80. static Curl_send wolfssl_send;
  81. #ifdef OPENSSL_EXTRA
  82. /*
  83. * Availability note:
  84. * The TLS 1.3 secret callback (wolfSSL_set_tls13_secret_cb) was added in
  85. * WolfSSL 4.4.0, but requires the -DHAVE_SECRET_CALLBACK build option. If that
  86. * option is not set, then TLS 1.3 will not be logged.
  87. * For TLS 1.2 and before, we use wolfSSL_get_keys().
  88. * SSL_get_client_random and wolfSSL_get_keys require OPENSSL_EXTRA
  89. * (--enable-opensslextra or --enable-all).
  90. */
  91. #if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
  92. static int
  93. wolfssl_tls13_secret_callback(SSL *ssl, int id, const unsigned char *secret,
  94. int secretSz, void *ctx)
  95. {
  96. const char *label;
  97. unsigned char client_random[SSL3_RANDOM_SIZE];
  98. (void)ctx;
  99. if(!ssl || !Curl_tls_keylog_enabled()) {
  100. return 0;
  101. }
  102. switch(id) {
  103. case CLIENT_EARLY_TRAFFIC_SECRET:
  104. label = "CLIENT_EARLY_TRAFFIC_SECRET";
  105. break;
  106. case CLIENT_HANDSHAKE_TRAFFIC_SECRET:
  107. label = "CLIENT_HANDSHAKE_TRAFFIC_SECRET";
  108. break;
  109. case SERVER_HANDSHAKE_TRAFFIC_SECRET:
  110. label = "SERVER_HANDSHAKE_TRAFFIC_SECRET";
  111. break;
  112. case CLIENT_TRAFFIC_SECRET:
  113. label = "CLIENT_TRAFFIC_SECRET_0";
  114. break;
  115. case SERVER_TRAFFIC_SECRET:
  116. label = "SERVER_TRAFFIC_SECRET_0";
  117. break;
  118. case EARLY_EXPORTER_SECRET:
  119. label = "EARLY_EXPORTER_SECRET";
  120. break;
  121. case EXPORTER_SECRET:
  122. label = "EXPORTER_SECRET";
  123. break;
  124. default:
  125. return 0;
  126. }
  127. if(SSL_get_client_random(ssl, client_random, SSL3_RANDOM_SIZE) == 0) {
  128. /* Should never happen as wolfSSL_KeepArrays() was called before. */
  129. return 0;
  130. }
  131. Curl_tls_keylog_write(label, client_random, secret, secretSz);
  132. return 0;
  133. }
  134. #endif /* defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13) */
  135. static void
  136. wolfssl_log_tls12_secret(SSL *ssl)
  137. {
  138. unsigned char *ms, *sr, *cr;
  139. unsigned int msLen, srLen, crLen, i, x = 0;
  140. #if LIBWOLFSSL_VERSION_HEX >= 0x0300d000 /* >= 3.13.0 */
  141. /* wolfSSL_GetVersion is available since 3.13, we use it instead of
  142. * SSL_version since the latter relies on OPENSSL_ALL (--enable-opensslall or
  143. * --enable-all). Failing to perform this check could result in an unusable
  144. * key log line when TLS 1.3 is actually negotiated. */
  145. switch(wolfSSL_GetVersion(ssl)) {
  146. case WOLFSSL_SSLV3:
  147. case WOLFSSL_TLSV1:
  148. case WOLFSSL_TLSV1_1:
  149. case WOLFSSL_TLSV1_2:
  150. break;
  151. default:
  152. /* TLS 1.3 does not use this mechanism, the "master secret" returned below
  153. * is not directly usable. */
  154. return;
  155. }
  156. #endif
  157. if(SSL_get_keys(ssl, &ms, &msLen, &sr, &srLen, &cr, &crLen) != SSL_SUCCESS) {
  158. return;
  159. }
  160. /* Check for a missing master secret and skip logging. That can happen if
  161. * curl rejects the server certificate and aborts the handshake.
  162. */
  163. for(i = 0; i < msLen; i++) {
  164. x |= ms[i];
  165. }
  166. if(x == 0) {
  167. return;
  168. }
  169. Curl_tls_keylog_write("CLIENT_RANDOM", cr, ms, msLen);
  170. }
  171. #endif /* OPENSSL_EXTRA */
  172. static int do_file_type(const char *type)
  173. {
  174. if(!type || !type[0])
  175. return SSL_FILETYPE_PEM;
  176. if(strcasecompare(type, "PEM"))
  177. return SSL_FILETYPE_PEM;
  178. if(strcasecompare(type, "DER"))
  179. return SSL_FILETYPE_ASN1;
  180. return -1;
  181. }
  182. #ifdef HAVE_LIBOQS
  183. struct group_name_map {
  184. const word16 group;
  185. const char *name;
  186. };
  187. static const struct group_name_map gnm[] = {
  188. { WOLFSSL_KYBER_LEVEL1, "KYBER_LEVEL1" },
  189. { WOLFSSL_KYBER_LEVEL3, "KYBER_LEVEL3" },
  190. { WOLFSSL_KYBER_LEVEL5, "KYBER_LEVEL5" },
  191. { WOLFSSL_NTRU_HPS_LEVEL1, "NTRU_HPS_LEVEL1" },
  192. { WOLFSSL_NTRU_HPS_LEVEL3, "NTRU_HPS_LEVEL3" },
  193. { WOLFSSL_NTRU_HPS_LEVEL5, "NTRU_HPS_LEVEL5" },
  194. { WOLFSSL_NTRU_HRSS_LEVEL3, "NTRU_HRSS_LEVEL3" },
  195. { WOLFSSL_SABER_LEVEL1, "SABER_LEVEL1" },
  196. { WOLFSSL_SABER_LEVEL3, "SABER_LEVEL3" },
  197. { WOLFSSL_SABER_LEVEL5, "SABER_LEVEL5" },
  198. { WOLFSSL_KYBER_90S_LEVEL1, "KYBER_90S_LEVEL1" },
  199. { WOLFSSL_KYBER_90S_LEVEL3, "KYBER_90S_LEVEL3" },
  200. { WOLFSSL_KYBER_90S_LEVEL5, "KYBER_90S_LEVEL5" },
  201. { WOLFSSL_P256_NTRU_HPS_LEVEL1, "P256_NTRU_HPS_LEVEL1" },
  202. { WOLFSSL_P384_NTRU_HPS_LEVEL3, "P384_NTRU_HPS_LEVEL3" },
  203. { WOLFSSL_P521_NTRU_HPS_LEVEL5, "P521_NTRU_HPS_LEVEL5" },
  204. { WOLFSSL_P384_NTRU_HRSS_LEVEL3, "P384_NTRU_HRSS_LEVEL3" },
  205. { WOLFSSL_P256_SABER_LEVEL1, "P256_SABER_LEVEL1" },
  206. { WOLFSSL_P384_SABER_LEVEL3, "P384_SABER_LEVEL3" },
  207. { WOLFSSL_P521_SABER_LEVEL5, "P521_SABER_LEVEL5" },
  208. { WOLFSSL_P256_KYBER_LEVEL1, "P256_KYBER_LEVEL1" },
  209. { WOLFSSL_P384_KYBER_LEVEL3, "P384_KYBER_LEVEL3" },
  210. { WOLFSSL_P521_KYBER_LEVEL5, "P521_KYBER_LEVEL5" },
  211. { WOLFSSL_P256_KYBER_90S_LEVEL1, "P256_KYBER_90S_LEVEL1" },
  212. { WOLFSSL_P384_KYBER_90S_LEVEL3, "P384_KYBER_90S_LEVEL3" },
  213. { WOLFSSL_P521_KYBER_90S_LEVEL5, "P521_KYBER_90S_LEVEL5" },
  214. { 0, NULL }
  215. };
  216. #endif
  217. /*
  218. * This function loads all the client/CA certificates and CRLs. Setup the TLS
  219. * layer and do all necessary magic.
  220. */
  221. static CURLcode
  222. wolfssl_connect_step1(struct Curl_easy *data, struct connectdata *conn,
  223. int sockindex)
  224. {
  225. char *ciphers, *curves;
  226. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  227. struct ssl_backend_data *backend = connssl->backend;
  228. SSL_METHOD* req_method = NULL;
  229. curl_socket_t sockfd = conn->sock[sockindex];
  230. #ifdef HAVE_LIBOQS
  231. word16 oqsAlg = 0;
  232. size_t idx = 0;
  233. #endif
  234. #ifdef HAVE_SNI
  235. bool sni = FALSE;
  236. #define use_sni(x) sni = (x)
  237. #else
  238. #define use_sni(x) Curl_nop_stmt
  239. #endif
  240. DEBUGASSERT(backend);
  241. if(connssl->state == ssl_connection_complete)
  242. return CURLE_OK;
  243. if(SSL_CONN_CONFIG(version_max) != CURL_SSLVERSION_MAX_NONE) {
  244. failf(data, "wolfSSL does not support to set maximum SSL/TLS version");
  245. return CURLE_SSL_CONNECT_ERROR;
  246. }
  247. /* check to see if we've been told to use an explicit SSL/TLS version */
  248. switch(SSL_CONN_CONFIG(version)) {
  249. case CURL_SSLVERSION_DEFAULT:
  250. case CURL_SSLVERSION_TLSv1:
  251. #if LIBWOLFSSL_VERSION_HEX >= 0x03003000 /* >= 3.3.0 */
  252. /* minimum protocol version is set later after the CTX object is created */
  253. req_method = SSLv23_client_method();
  254. #else
  255. infof(data, "wolfSSL <3.3.0 cannot be configured to use TLS 1.0-1.2, "
  256. "TLS 1.0 is used exclusively");
  257. req_method = TLSv1_client_method();
  258. #endif
  259. use_sni(TRUE);
  260. break;
  261. case CURL_SSLVERSION_TLSv1_0:
  262. #if defined(WOLFSSL_ALLOW_TLSV10) && !defined(NO_OLD_TLS)
  263. req_method = TLSv1_client_method();
  264. use_sni(TRUE);
  265. #else
  266. failf(data, "wolfSSL does not support TLS 1.0");
  267. return CURLE_NOT_BUILT_IN;
  268. #endif
  269. break;
  270. case CURL_SSLVERSION_TLSv1_1:
  271. #ifndef NO_OLD_TLS
  272. req_method = TLSv1_1_client_method();
  273. use_sni(TRUE);
  274. #else
  275. failf(data, "wolfSSL does not support TLS 1.1");
  276. return CURLE_NOT_BUILT_IN;
  277. #endif
  278. break;
  279. case CURL_SSLVERSION_TLSv1_2:
  280. req_method = TLSv1_2_client_method();
  281. use_sni(TRUE);
  282. break;
  283. case CURL_SSLVERSION_TLSv1_3:
  284. #ifdef WOLFSSL_TLS13
  285. req_method = wolfTLSv1_3_client_method();
  286. use_sni(TRUE);
  287. break;
  288. #else
  289. failf(data, "wolfSSL: TLS 1.3 is not yet supported");
  290. return CURLE_SSL_CONNECT_ERROR;
  291. #endif
  292. default:
  293. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  294. return CURLE_SSL_CONNECT_ERROR;
  295. }
  296. if(!req_method) {
  297. failf(data, "SSL: couldn't create a method");
  298. return CURLE_OUT_OF_MEMORY;
  299. }
  300. if(backend->ctx)
  301. SSL_CTX_free(backend->ctx);
  302. backend->ctx = SSL_CTX_new(req_method);
  303. if(!backend->ctx) {
  304. failf(data, "SSL: couldn't create a context");
  305. return CURLE_OUT_OF_MEMORY;
  306. }
  307. switch(SSL_CONN_CONFIG(version)) {
  308. case CURL_SSLVERSION_DEFAULT:
  309. case CURL_SSLVERSION_TLSv1:
  310. #if LIBWOLFSSL_VERSION_HEX > 0x03004006 /* > 3.4.6 */
  311. /* Versions 3.3.0 to 3.4.6 we know the minimum protocol version is
  312. * whatever minimum version of TLS was built in and at least TLS 1.0. For
  313. * later library versions that could change (eg TLS 1.0 built in but
  314. * defaults to TLS 1.1) so we have this short circuit evaluation to find
  315. * the minimum supported TLS version.
  316. */
  317. if((wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1) != 1) &&
  318. (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_1) != 1) &&
  319. (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_2) != 1)
  320. #ifdef WOLFSSL_TLS13
  321. && (wolfSSL_CTX_SetMinVersion(backend->ctx, WOLFSSL_TLSV1_3) != 1)
  322. #endif
  323. ) {
  324. failf(data, "SSL: couldn't set the minimum protocol version");
  325. return CURLE_SSL_CONNECT_ERROR;
  326. }
  327. #endif
  328. break;
  329. }
  330. ciphers = SSL_CONN_CONFIG(cipher_list);
  331. if(ciphers) {
  332. if(!SSL_CTX_set_cipher_list(backend->ctx, ciphers)) {
  333. failf(data, "failed setting cipher list: %s", ciphers);
  334. return CURLE_SSL_CIPHER;
  335. }
  336. infof(data, "Cipher selection: %s", ciphers);
  337. }
  338. curves = SSL_CONN_CONFIG(curves);
  339. if(curves) {
  340. #ifdef HAVE_LIBOQS
  341. for(idx = 0; gnm[idx].name != NULL; idx++) {
  342. if(strncmp(curves, gnm[idx].name, strlen(gnm[idx].name)) == 0) {
  343. oqsAlg = gnm[idx].group;
  344. break;
  345. }
  346. }
  347. if(oqsAlg == 0)
  348. #endif
  349. {
  350. if(!SSL_CTX_set1_curves_list(backend->ctx, curves)) {
  351. failf(data, "failed setting curves list: '%s'", curves);
  352. return CURLE_SSL_CIPHER;
  353. }
  354. }
  355. }
  356. #ifndef NO_FILESYSTEM
  357. /* load trusted cacert */
  358. if(SSL_CONN_CONFIG(CAfile)) {
  359. if(1 != SSL_CTX_load_verify_locations(backend->ctx,
  360. SSL_CONN_CONFIG(CAfile),
  361. SSL_CONN_CONFIG(CApath))) {
  362. if(SSL_CONN_CONFIG(verifypeer)) {
  363. /* Fail if we insist on successfully verifying the server. */
  364. failf(data, "error setting certificate verify locations:"
  365. " CAfile: %s CApath: %s",
  366. SSL_CONN_CONFIG(CAfile)?
  367. SSL_CONN_CONFIG(CAfile): "none",
  368. SSL_CONN_CONFIG(CApath)?
  369. SSL_CONN_CONFIG(CApath) : "none");
  370. return CURLE_SSL_CACERT_BADFILE;
  371. }
  372. else {
  373. /* Just continue with a warning if no strict certificate
  374. verification is required. */
  375. infof(data, "error setting certificate verify locations,"
  376. " continuing anyway:");
  377. }
  378. }
  379. else {
  380. /* Everything is fine. */
  381. infof(data, "successfully set certificate verify locations:");
  382. }
  383. infof(data, " CAfile: %s",
  384. SSL_CONN_CONFIG(CAfile) ? SSL_CONN_CONFIG(CAfile) : "none");
  385. infof(data, " CApath: %s",
  386. SSL_CONN_CONFIG(CApath) ? SSL_CONN_CONFIG(CApath) : "none");
  387. }
  388. /* Load the client certificate, and private key */
  389. if(SSL_SET_OPTION(primary.clientcert) && SSL_SET_OPTION(key)) {
  390. int file_type = do_file_type(SSL_SET_OPTION(cert_type));
  391. if(SSL_CTX_use_certificate_file(backend->ctx,
  392. SSL_SET_OPTION(primary.clientcert),
  393. file_type) != 1) {
  394. failf(data, "unable to use client certificate (no key or wrong pass"
  395. " phrase?)");
  396. return CURLE_SSL_CONNECT_ERROR;
  397. }
  398. file_type = do_file_type(SSL_SET_OPTION(key_type));
  399. if(SSL_CTX_use_PrivateKey_file(backend->ctx, SSL_SET_OPTION(key),
  400. file_type) != 1) {
  401. failf(data, "unable to set private key");
  402. return CURLE_SSL_CONNECT_ERROR;
  403. }
  404. }
  405. #endif /* !NO_FILESYSTEM */
  406. /* SSL always tries to verify the peer, this only says whether it should
  407. * fail to connect if the verification fails, or if it should continue
  408. * anyway. In the latter case the result of the verification is checked with
  409. * SSL_get_verify_result() below. */
  410. SSL_CTX_set_verify(backend->ctx,
  411. SSL_CONN_CONFIG(verifypeer)?SSL_VERIFY_PEER:
  412. SSL_VERIFY_NONE,
  413. NULL);
  414. #ifdef HAVE_SNI
  415. if(sni) {
  416. struct in_addr addr4;
  417. #ifdef ENABLE_IPV6
  418. struct in6_addr addr6;
  419. #endif
  420. const char * const hostname = SSL_HOST_NAME();
  421. size_t hostname_len = strlen(hostname);
  422. if((hostname_len < USHRT_MAX) &&
  423. !Curl_inet_pton(AF_INET, hostname, &addr4)
  424. #ifdef ENABLE_IPV6
  425. && !Curl_inet_pton(AF_INET6, hostname, &addr6)
  426. #endif
  427. ) {
  428. size_t snilen;
  429. char *snihost = Curl_ssl_snihost(data, hostname, &snilen);
  430. if(!snihost ||
  431. wolfSSL_CTX_UseSNI(backend->ctx, WOLFSSL_SNI_HOST_NAME, snihost,
  432. (unsigned short)snilen) != 1) {
  433. failf(data, "Failed to set SNI");
  434. return CURLE_SSL_CONNECT_ERROR;
  435. }
  436. }
  437. }
  438. #endif
  439. /* give application a chance to interfere with SSL set up. */
  440. if(data->set.ssl.fsslctx) {
  441. CURLcode result = (*data->set.ssl.fsslctx)(data, backend->ctx,
  442. data->set.ssl.fsslctxp);
  443. if(result) {
  444. failf(data, "error signaled by ssl ctx callback");
  445. return result;
  446. }
  447. }
  448. #ifdef NO_FILESYSTEM
  449. else if(SSL_CONN_CONFIG(verifypeer)) {
  450. failf(data, "SSL: Certificates can't be loaded because wolfSSL was built"
  451. " with \"no filesystem\". Either disable peer verification"
  452. " (insecure) or if you are building an application with libcurl you"
  453. " can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
  454. return CURLE_SSL_CONNECT_ERROR;
  455. }
  456. #endif
  457. /* Let's make an SSL structure */
  458. if(backend->handle)
  459. SSL_free(backend->handle);
  460. backend->handle = SSL_new(backend->ctx);
  461. if(!backend->handle) {
  462. failf(data, "SSL: couldn't create a handle");
  463. return CURLE_OUT_OF_MEMORY;
  464. }
  465. #ifdef HAVE_LIBOQS
  466. if(oqsAlg) {
  467. if(wolfSSL_UseKeyShare(backend->handle, oqsAlg) != WOLFSSL_SUCCESS) {
  468. failf(data, "unable to use oqs KEM");
  469. }
  470. }
  471. #endif
  472. #ifdef HAVE_ALPN
  473. if(conn->bits.tls_enable_alpn) {
  474. char protocols[128];
  475. *protocols = '\0';
  476. /* wolfSSL's ALPN protocol name list format is a comma separated string of
  477. protocols in descending order of preference, eg: "h2,http/1.1" */
  478. #ifdef USE_HTTP2
  479. if(data->state.httpwant >= CURL_HTTP_VERSION_2) {
  480. strcpy(protocols + strlen(protocols), ALPN_H2 ",");
  481. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, ALPN_H2);
  482. }
  483. #endif
  484. strcpy(protocols + strlen(protocols), ALPN_HTTP_1_1);
  485. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, ALPN_HTTP_1_1);
  486. if(wolfSSL_UseALPN(backend->handle, protocols,
  487. (unsigned)strlen(protocols),
  488. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) != SSL_SUCCESS) {
  489. failf(data, "SSL: failed setting ALPN protocols");
  490. return CURLE_SSL_CONNECT_ERROR;
  491. }
  492. }
  493. #endif /* HAVE_ALPN */
  494. #ifdef OPENSSL_EXTRA
  495. if(Curl_tls_keylog_enabled()) {
  496. /* Ensure the Client Random is preserved. */
  497. wolfSSL_KeepArrays(backend->handle);
  498. #if defined(HAVE_SECRET_CALLBACK) && defined(WOLFSSL_TLS13)
  499. wolfSSL_set_tls13_secret_cb(backend->handle,
  500. wolfssl_tls13_secret_callback, NULL);
  501. #endif
  502. }
  503. #endif /* OPENSSL_EXTRA */
  504. #ifdef HAVE_SECURE_RENEGOTIATION
  505. if(wolfSSL_UseSecureRenegotiation(backend->handle) != SSL_SUCCESS) {
  506. failf(data, "SSL: failed setting secure renegotiation");
  507. return CURLE_SSL_CONNECT_ERROR;
  508. }
  509. #endif /* HAVE_SECURE_RENEGOTIATION */
  510. /* Check if there's a cached ID we can/should use here! */
  511. if(SSL_SET_OPTION(primary.sessionid)) {
  512. void *ssl_sessionid = NULL;
  513. Curl_ssl_sessionid_lock(data);
  514. if(!Curl_ssl_getsessionid(data, conn,
  515. SSL_IS_PROXY() ? TRUE : FALSE,
  516. &ssl_sessionid, NULL, sockindex)) {
  517. /* we got a session id, use it! */
  518. if(!SSL_set_session(backend->handle, ssl_sessionid)) {
  519. Curl_ssl_delsessionid(data, ssl_sessionid);
  520. infof(data, "Can't use session ID, going on without");
  521. }
  522. else
  523. infof(data, "SSL re-using session ID");
  524. }
  525. Curl_ssl_sessionid_unlock(data);
  526. }
  527. /* pass the raw socket into the SSL layer */
  528. if(!SSL_set_fd(backend->handle, (int)sockfd)) {
  529. failf(data, "SSL: SSL_set_fd failed");
  530. return CURLE_SSL_CONNECT_ERROR;
  531. }
  532. connssl->connecting_state = ssl_connect_2;
  533. return CURLE_OK;
  534. }
  535. static CURLcode
  536. wolfssl_connect_step2(struct Curl_easy *data, struct connectdata *conn,
  537. int sockindex)
  538. {
  539. int ret = -1;
  540. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  541. struct ssl_backend_data *backend = connssl->backend;
  542. const char * const dispname = SSL_HOST_DISPNAME();
  543. const char * const pinnedpubkey = SSL_PINNED_PUB_KEY();
  544. DEBUGASSERT(backend);
  545. ERR_clear_error();
  546. conn->recv[sockindex] = wolfssl_recv;
  547. conn->send[sockindex] = wolfssl_send;
  548. /* Enable RFC2818 checks */
  549. if(SSL_CONN_CONFIG(verifyhost)) {
  550. char *snihost = Curl_ssl_snihost(data, SSL_HOST_NAME(), NULL);
  551. if(!snihost ||
  552. (wolfSSL_check_domain_name(backend->handle, snihost) == SSL_FAILURE))
  553. return CURLE_SSL_CONNECT_ERROR;
  554. }
  555. ret = SSL_connect(backend->handle);
  556. #ifdef OPENSSL_EXTRA
  557. if(Curl_tls_keylog_enabled()) {
  558. /* If key logging is enabled, wait for the handshake to complete and then
  559. * proceed with logging secrets (for TLS 1.2 or older).
  560. *
  561. * During the handshake (ret==-1), wolfSSL_want_read() is true as it waits
  562. * for the server response. At that point the master secret is not yet
  563. * available, so we must not try to read it.
  564. * To log the secret on completion with a handshake failure, detect
  565. * completion via the observation that there is nothing to read or write.
  566. * Note that OpenSSL SSL_want_read() is always true here. If wolfSSL ever
  567. * changes, the worst case is that no key is logged on error.
  568. */
  569. if(ret == SSL_SUCCESS ||
  570. (!wolfSSL_want_read(backend->handle) &&
  571. !wolfSSL_want_write(backend->handle))) {
  572. wolfssl_log_tls12_secret(backend->handle);
  573. /* Client Random and master secrets are no longer needed, erase these.
  574. * Ignored while the handshake is still in progress. */
  575. wolfSSL_FreeArrays(backend->handle);
  576. }
  577. }
  578. #endif /* OPENSSL_EXTRA */
  579. if(ret != 1) {
  580. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  581. int detail = SSL_get_error(backend->handle, ret);
  582. if(SSL_ERROR_WANT_READ == detail) {
  583. connssl->connecting_state = ssl_connect_2_reading;
  584. return CURLE_OK;
  585. }
  586. else if(SSL_ERROR_WANT_WRITE == detail) {
  587. connssl->connecting_state = ssl_connect_2_writing;
  588. return CURLE_OK;
  589. }
  590. /* There is no easy way to override only the CN matching.
  591. * This will enable the override of both mismatching SubjectAltNames
  592. * as also mismatching CN fields */
  593. else if(DOMAIN_NAME_MISMATCH == detail) {
  594. #if 1
  595. failf(data, " subject alt name(s) or common name do not match \"%s\"",
  596. dispname);
  597. return CURLE_PEER_FAILED_VERIFICATION;
  598. #else
  599. /* When the wolfssl_check_domain_name() is used and you desire to
  600. * continue on a DOMAIN_NAME_MISMATCH, i.e. 'conn->ssl_config.verifyhost
  601. * == 0', CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA
  602. * error. The only way to do this is currently to switch the
  603. * Wolfssl_check_domain_name() in and out based on the
  604. * 'conn->ssl_config.verifyhost' value. */
  605. if(SSL_CONN_CONFIG(verifyhost)) {
  606. failf(data,
  607. " subject alt name(s) or common name do not match \"%s\"\n",
  608. dispname);
  609. return CURLE_PEER_FAILED_VERIFICATION;
  610. }
  611. else {
  612. infof(data,
  613. " subject alt name(s) and/or common name do not match \"%s\"",
  614. dispname);
  615. return CURLE_OK;
  616. }
  617. #endif
  618. }
  619. #if LIBWOLFSSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
  620. else if(ASN_NO_SIGNER_E == detail) {
  621. if(SSL_CONN_CONFIG(verifypeer)) {
  622. failf(data, " CA signer not available for verification");
  623. return CURLE_SSL_CACERT_BADFILE;
  624. }
  625. else {
  626. /* Just continue with a warning if no strict certificate
  627. verification is required. */
  628. infof(data, "CA signer not available for verification, "
  629. "continuing anyway");
  630. }
  631. }
  632. #endif
  633. else {
  634. failf(data, "SSL_connect failed with error %d: %s", detail,
  635. ERR_error_string(detail, error_buffer));
  636. return CURLE_SSL_CONNECT_ERROR;
  637. }
  638. }
  639. if(pinnedpubkey) {
  640. #ifdef KEEP_PEER_CERT
  641. X509 *x509;
  642. const char *x509_der;
  643. int x509_der_len;
  644. struct Curl_X509certificate x509_parsed;
  645. struct Curl_asn1Element *pubkey;
  646. CURLcode result;
  647. x509 = SSL_get_peer_certificate(backend->handle);
  648. if(!x509) {
  649. failf(data, "SSL: failed retrieving server certificate");
  650. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  651. }
  652. x509_der = (const char *)wolfSSL_X509_get_der(x509, &x509_der_len);
  653. if(!x509_der) {
  654. failf(data, "SSL: failed retrieving ASN.1 server certificate");
  655. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  656. }
  657. memset(&x509_parsed, 0, sizeof(x509_parsed));
  658. if(Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len))
  659. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  660. pubkey = &x509_parsed.subjectPublicKeyInfo;
  661. if(!pubkey->header || pubkey->end <= pubkey->header) {
  662. failf(data, "SSL: failed retrieving public key from server certificate");
  663. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  664. }
  665. result = Curl_pin_peer_pubkey(data,
  666. pinnedpubkey,
  667. (const unsigned char *)pubkey->header,
  668. (size_t)(pubkey->end - pubkey->header));
  669. if(result) {
  670. failf(data, "SSL: public key does not match pinned public key");
  671. return result;
  672. }
  673. #else
  674. failf(data, "Library lacks pinning support built-in");
  675. return CURLE_NOT_BUILT_IN;
  676. #endif
  677. }
  678. #ifdef HAVE_ALPN
  679. if(conn->bits.tls_enable_alpn) {
  680. int rc;
  681. char *protocol = NULL;
  682. unsigned short protocol_len = 0;
  683. rc = wolfSSL_ALPN_GetProtocol(backend->handle, &protocol, &protocol_len);
  684. if(rc == SSL_SUCCESS) {
  685. infof(data, VTLS_INFOF_ALPN_ACCEPTED_LEN_1STR, protocol_len, protocol);
  686. if(protocol_len == ALPN_HTTP_1_1_LENGTH &&
  687. !memcmp(protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH))
  688. conn->alpn = CURL_HTTP_VERSION_1_1;
  689. #ifdef USE_HTTP2
  690. else if(data->state.httpwant >= CURL_HTTP_VERSION_2 &&
  691. protocol_len == ALPN_H2_LENGTH &&
  692. !memcmp(protocol, ALPN_H2, ALPN_H2_LENGTH))
  693. conn->alpn = CURL_HTTP_VERSION_2;
  694. #endif
  695. else
  696. infof(data, "ALPN, unrecognized protocol %.*s", protocol_len,
  697. protocol);
  698. Curl_multiuse_state(data, conn->alpn == CURL_HTTP_VERSION_2 ?
  699. BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
  700. }
  701. else if(rc == SSL_ALPN_NOT_FOUND)
  702. infof(data, VTLS_INFOF_NO_ALPN);
  703. else {
  704. failf(data, "ALPN, failure getting protocol, error %d", rc);
  705. return CURLE_SSL_CONNECT_ERROR;
  706. }
  707. }
  708. #endif /* HAVE_ALPN */
  709. connssl->connecting_state = ssl_connect_3;
  710. #if (LIBWOLFSSL_VERSION_HEX >= 0x03009010)
  711. infof(data, "SSL connection using %s / %s",
  712. wolfSSL_get_version(backend->handle),
  713. wolfSSL_get_cipher_name(backend->handle));
  714. #else
  715. infof(data, "SSL connected");
  716. #endif
  717. return CURLE_OK;
  718. }
  719. static CURLcode
  720. wolfssl_connect_step3(struct Curl_easy *data, struct connectdata *conn,
  721. int sockindex)
  722. {
  723. CURLcode result = CURLE_OK;
  724. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  725. struct ssl_backend_data *backend = connssl->backend;
  726. DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
  727. DEBUGASSERT(backend);
  728. if(SSL_SET_OPTION(primary.sessionid)) {
  729. bool incache;
  730. bool added = FALSE;
  731. void *old_ssl_sessionid = NULL;
  732. /* SSL_get1_session allocates memory that has to be freed. */
  733. SSL_SESSION *our_ssl_sessionid = SSL_get1_session(backend->handle);
  734. bool isproxy = SSL_IS_PROXY() ? TRUE : FALSE;
  735. if(our_ssl_sessionid) {
  736. Curl_ssl_sessionid_lock(data);
  737. incache = !(Curl_ssl_getsessionid(data, conn, isproxy,
  738. &old_ssl_sessionid, NULL, sockindex));
  739. if(incache) {
  740. if(old_ssl_sessionid != our_ssl_sessionid) {
  741. infof(data, "old SSL session ID is stale, removing");
  742. Curl_ssl_delsessionid(data, old_ssl_sessionid);
  743. incache = FALSE;
  744. }
  745. }
  746. if(!incache) {
  747. result = Curl_ssl_addsessionid(data, conn, isproxy, our_ssl_sessionid,
  748. 0, sockindex, NULL);
  749. if(result) {
  750. Curl_ssl_sessionid_unlock(data);
  751. SSL_SESSION_free(our_ssl_sessionid);
  752. failf(data, "failed to store ssl session");
  753. return result;
  754. }
  755. else {
  756. added = TRUE;
  757. }
  758. }
  759. Curl_ssl_sessionid_unlock(data);
  760. if(!added) {
  761. /* If the session info wasn't added to the cache, free our copy. */
  762. SSL_SESSION_free(our_ssl_sessionid);
  763. }
  764. }
  765. }
  766. connssl->connecting_state = ssl_connect_done;
  767. return result;
  768. }
  769. static ssize_t wolfssl_send(struct Curl_easy *data,
  770. int sockindex,
  771. const void *mem,
  772. size_t len,
  773. CURLcode *curlcode)
  774. {
  775. struct connectdata *conn = data->conn;
  776. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  777. struct ssl_backend_data *backend = connssl->backend;
  778. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  779. int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
  780. int rc;
  781. DEBUGASSERT(backend);
  782. ERR_clear_error();
  783. rc = SSL_write(backend->handle, mem, memlen);
  784. if(rc <= 0) {
  785. int err = SSL_get_error(backend->handle, rc);
  786. switch(err) {
  787. case SSL_ERROR_WANT_READ:
  788. case SSL_ERROR_WANT_WRITE:
  789. /* there's data pending, re-invoke SSL_write() */
  790. *curlcode = CURLE_AGAIN;
  791. return -1;
  792. default:
  793. failf(data, "SSL write: %s, errno %d",
  794. ERR_error_string(err, error_buffer),
  795. SOCKERRNO);
  796. *curlcode = CURLE_SEND_ERROR;
  797. return -1;
  798. }
  799. }
  800. return rc;
  801. }
  802. static void wolfssl_close(struct Curl_easy *data, struct connectdata *conn,
  803. int sockindex)
  804. {
  805. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  806. struct ssl_backend_data *backend = connssl->backend;
  807. (void) data;
  808. DEBUGASSERT(backend);
  809. if(backend->handle) {
  810. char buf[32];
  811. /* Maybe the server has already sent a close notify alert.
  812. Read it to avoid an RST on the TCP connection. */
  813. (void)SSL_read(backend->handle, buf, (int)sizeof(buf));
  814. (void)SSL_shutdown(backend->handle);
  815. SSL_free(backend->handle);
  816. backend->handle = NULL;
  817. }
  818. if(backend->ctx) {
  819. SSL_CTX_free(backend->ctx);
  820. backend->ctx = NULL;
  821. }
  822. }
  823. static ssize_t wolfssl_recv(struct Curl_easy *data,
  824. int num,
  825. char *buf,
  826. size_t buffersize,
  827. CURLcode *curlcode)
  828. {
  829. struct connectdata *conn = data->conn;
  830. struct ssl_connect_data *connssl = &conn->ssl[num];
  831. struct ssl_backend_data *backend = connssl->backend;
  832. char error_buffer[WOLFSSL_MAX_ERROR_SZ];
  833. int buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
  834. int nread;
  835. DEBUGASSERT(backend);
  836. ERR_clear_error();
  837. nread = SSL_read(backend->handle, buf, buffsize);
  838. if(nread <= 0) {
  839. int err = SSL_get_error(backend->handle, nread);
  840. switch(err) {
  841. case SSL_ERROR_ZERO_RETURN: /* no more data */
  842. break;
  843. case SSL_ERROR_NONE:
  844. /* FALLTHROUGH */
  845. case SSL_ERROR_WANT_READ:
  846. /* FALLTHROUGH */
  847. case SSL_ERROR_WANT_WRITE:
  848. /* there's data pending, re-invoke SSL_read() */
  849. *curlcode = CURLE_AGAIN;
  850. return -1;
  851. default:
  852. failf(data, "SSL read: %s, errno %d",
  853. ERR_error_string(err, error_buffer), SOCKERRNO);
  854. *curlcode = CURLE_RECV_ERROR;
  855. return -1;
  856. }
  857. }
  858. return nread;
  859. }
  860. static void wolfssl_session_free(void *ptr)
  861. {
  862. SSL_SESSION_free(ptr);
  863. }
  864. static size_t wolfssl_version(char *buffer, size_t size)
  865. {
  866. #if LIBWOLFSSL_VERSION_HEX >= 0x03006000
  867. return msnprintf(buffer, size, "wolfSSL/%s", wolfSSL_lib_version());
  868. #elif defined(WOLFSSL_VERSION)
  869. return msnprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
  870. #endif
  871. }
  872. static int wolfssl_init(void)
  873. {
  874. #ifdef OPENSSL_EXTRA
  875. Curl_tls_keylog_open();
  876. #endif
  877. return (wolfSSL_Init() == SSL_SUCCESS);
  878. }
  879. static void wolfssl_cleanup(void)
  880. {
  881. wolfSSL_Cleanup();
  882. #ifdef OPENSSL_EXTRA
  883. Curl_tls_keylog_close();
  884. #endif
  885. }
  886. static bool wolfssl_data_pending(const struct connectdata *conn,
  887. int connindex)
  888. {
  889. const struct ssl_connect_data *connssl = &conn->ssl[connindex];
  890. struct ssl_backend_data *backend = connssl->backend;
  891. DEBUGASSERT(backend);
  892. if(backend->handle) /* SSL is in use */
  893. return (0 != SSL_pending(backend->handle)) ? TRUE : FALSE;
  894. else
  895. return FALSE;
  896. }
  897. /*
  898. * This function is called to shut down the SSL layer but keep the
  899. * socket open (CCC - Clear Command Channel)
  900. */
  901. static int wolfssl_shutdown(struct Curl_easy *data, struct connectdata *conn,
  902. int sockindex)
  903. {
  904. int retval = 0;
  905. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  906. struct ssl_backend_data *backend = connssl->backend;
  907. (void) data;
  908. DEBUGASSERT(backend);
  909. if(backend->handle) {
  910. ERR_clear_error();
  911. SSL_free(backend->handle);
  912. backend->handle = NULL;
  913. }
  914. return retval;
  915. }
  916. static CURLcode
  917. wolfssl_connect_common(struct Curl_easy *data,
  918. struct connectdata *conn,
  919. int sockindex,
  920. bool nonblocking,
  921. bool *done)
  922. {
  923. CURLcode result;
  924. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  925. curl_socket_t sockfd = conn->sock[sockindex];
  926. int what;
  927. /* check if the connection has already been established */
  928. if(ssl_connection_complete == connssl->state) {
  929. *done = TRUE;
  930. return CURLE_OK;
  931. }
  932. if(ssl_connect_1 == connssl->connecting_state) {
  933. /* Find out how much more time we're allowed */
  934. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  935. if(timeout_ms < 0) {
  936. /* no need to continue if time already is up */
  937. failf(data, "SSL connection timeout");
  938. return CURLE_OPERATION_TIMEDOUT;
  939. }
  940. result = wolfssl_connect_step1(data, conn, sockindex);
  941. if(result)
  942. return result;
  943. }
  944. while(ssl_connect_2 == connssl->connecting_state ||
  945. ssl_connect_2_reading == connssl->connecting_state ||
  946. ssl_connect_2_writing == connssl->connecting_state) {
  947. /* check allowed time left */
  948. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  949. if(timeout_ms < 0) {
  950. /* no need to continue if time already is up */
  951. failf(data, "SSL connection timeout");
  952. return CURLE_OPERATION_TIMEDOUT;
  953. }
  954. /* if ssl is expecting something, check if it's available. */
  955. if(connssl->connecting_state == ssl_connect_2_reading
  956. || connssl->connecting_state == ssl_connect_2_writing) {
  957. curl_socket_t writefd = ssl_connect_2_writing ==
  958. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  959. curl_socket_t readfd = ssl_connect_2_reading ==
  960. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  961. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  962. nonblocking?0:timeout_ms);
  963. if(what < 0) {
  964. /* fatal error */
  965. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  966. return CURLE_SSL_CONNECT_ERROR;
  967. }
  968. else if(0 == what) {
  969. if(nonblocking) {
  970. *done = FALSE;
  971. return CURLE_OK;
  972. }
  973. else {
  974. /* timeout */
  975. failf(data, "SSL connection timeout");
  976. return CURLE_OPERATION_TIMEDOUT;
  977. }
  978. }
  979. /* socket is readable or writable */
  980. }
  981. /* Run transaction, and return to the caller if it failed or if
  982. * this connection is part of a multi handle and this loop would
  983. * execute again. This permits the owner of a multi handle to
  984. * abort a connection attempt before step2 has completed while
  985. * ensuring that a client using select() or epoll() will always
  986. * have a valid fdset to wait on.
  987. */
  988. result = wolfssl_connect_step2(data, conn, sockindex);
  989. if(result || (nonblocking &&
  990. (ssl_connect_2 == connssl->connecting_state ||
  991. ssl_connect_2_reading == connssl->connecting_state ||
  992. ssl_connect_2_writing == connssl->connecting_state)))
  993. return result;
  994. } /* repeat step2 until all transactions are done. */
  995. if(ssl_connect_3 == connssl->connecting_state) {
  996. result = wolfssl_connect_step3(data, conn, sockindex);
  997. if(result)
  998. return result;
  999. }
  1000. if(ssl_connect_done == connssl->connecting_state) {
  1001. connssl->state = ssl_connection_complete;
  1002. conn->recv[sockindex] = wolfssl_recv;
  1003. conn->send[sockindex] = wolfssl_send;
  1004. *done = TRUE;
  1005. }
  1006. else
  1007. *done = FALSE;
  1008. /* Reset our connect state machine */
  1009. connssl->connecting_state = ssl_connect_1;
  1010. return CURLE_OK;
  1011. }
  1012. static CURLcode wolfssl_connect_nonblocking(struct Curl_easy *data,
  1013. struct connectdata *conn,
  1014. int sockindex, bool *done)
  1015. {
  1016. return wolfssl_connect_common(data, conn, sockindex, TRUE, done);
  1017. }
  1018. static CURLcode wolfssl_connect(struct Curl_easy *data,
  1019. struct connectdata *conn, int sockindex)
  1020. {
  1021. CURLcode result;
  1022. bool done = FALSE;
  1023. result = wolfssl_connect_common(data, conn, sockindex, FALSE, &done);
  1024. if(result)
  1025. return result;
  1026. DEBUGASSERT(done);
  1027. return CURLE_OK;
  1028. }
  1029. static CURLcode wolfssl_random(struct Curl_easy *data,
  1030. unsigned char *entropy, size_t length)
  1031. {
  1032. WC_RNG rng;
  1033. (void)data;
  1034. if(wc_InitRng(&rng))
  1035. return CURLE_FAILED_INIT;
  1036. if(length > UINT_MAX)
  1037. return CURLE_FAILED_INIT;
  1038. if(wc_RNG_GenerateBlock(&rng, entropy, (unsigned)length))
  1039. return CURLE_FAILED_INIT;
  1040. if(wc_FreeRng(&rng))
  1041. return CURLE_FAILED_INIT;
  1042. return CURLE_OK;
  1043. }
  1044. static CURLcode wolfssl_sha256sum(const unsigned char *tmp, /* input */
  1045. size_t tmplen,
  1046. unsigned char *sha256sum /* output */,
  1047. size_t unused)
  1048. {
  1049. wc_Sha256 SHA256pw;
  1050. (void)unused;
  1051. wc_InitSha256(&SHA256pw);
  1052. wc_Sha256Update(&SHA256pw, tmp, (word32)tmplen);
  1053. wc_Sha256Final(&SHA256pw, sha256sum);
  1054. return CURLE_OK;
  1055. }
  1056. static void *wolfssl_get_internals(struct ssl_connect_data *connssl,
  1057. CURLINFO info UNUSED_PARAM)
  1058. {
  1059. struct ssl_backend_data *backend = connssl->backend;
  1060. (void)info;
  1061. DEBUGASSERT(backend);
  1062. return backend->handle;
  1063. }
  1064. const struct Curl_ssl Curl_ssl_wolfssl = {
  1065. { CURLSSLBACKEND_WOLFSSL, "WolfSSL" }, /* info */
  1066. #ifdef KEEP_PEER_CERT
  1067. SSLSUPP_PINNEDPUBKEY |
  1068. #endif
  1069. SSLSUPP_SSL_CTX,
  1070. sizeof(struct ssl_backend_data),
  1071. wolfssl_init, /* init */
  1072. wolfssl_cleanup, /* cleanup */
  1073. wolfssl_version, /* version */
  1074. Curl_none_check_cxn, /* check_cxn */
  1075. wolfssl_shutdown, /* shutdown */
  1076. wolfssl_data_pending, /* data_pending */
  1077. wolfssl_random, /* random */
  1078. Curl_none_cert_status_request, /* cert_status_request */
  1079. wolfssl_connect, /* connect */
  1080. wolfssl_connect_nonblocking, /* connect_nonblocking */
  1081. Curl_ssl_getsock, /* getsock */
  1082. wolfssl_get_internals, /* get_internals */
  1083. wolfssl_close, /* close_one */
  1084. Curl_none_close_all, /* close_all */
  1085. wolfssl_session_free, /* session_free */
  1086. Curl_none_set_engine, /* set_engine */
  1087. Curl_none_set_engine_default, /* set_engine_default */
  1088. Curl_none_engines_list, /* engines_list */
  1089. Curl_none_false_start, /* false_start */
  1090. wolfssl_sha256sum, /* sha256sum */
  1091. NULL, /* associate_connection */
  1092. NULL, /* disassociate_connection */
  1093. NULL /* free_multi_ssl_backend_data */
  1094. };
  1095. #endif