2
0

wolfssl.c 41 KB

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