wolfssl.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  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. };
  1094. #endif