wolfssl.c 37 KB

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