vquic-tls.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #if defined(ENABLE_QUIC) && \
  26. (defined(USE_OPENSSL) || defined(USE_GNUTLS) || defined(USE_WOLFSSL))
  27. #ifdef USE_OPENSSL
  28. #include <openssl/err.h>
  29. #include "vtls/openssl.h"
  30. #elif defined(USE_GNUTLS)
  31. #include <gnutls/abstract.h>
  32. #include <gnutls/gnutls.h>
  33. #include <gnutls/x509.h>
  34. #include <gnutls/crypto.h>
  35. #include <nettle/sha2.h>
  36. #include "vtls/gtls.h"
  37. #elif defined(USE_WOLFSSL)
  38. #include <wolfssl/options.h>
  39. #include <wolfssl/ssl.h>
  40. #include <wolfssl/quic.h>
  41. #include "vtls/wolfssl.h"
  42. #endif
  43. #include "urldata.h"
  44. #include "curl_trc.h"
  45. #include "cfilters.h"
  46. #include "multiif.h"
  47. #include "vtls/keylog.h"
  48. #include "vtls/vtls.h"
  49. #include "vquic-tls.h"
  50. /* The last 3 #include files should be in this order */
  51. #include "curl_printf.h"
  52. #include "curl_memory.h"
  53. #include "memdebug.h"
  54. #ifndef ARRAYSIZE
  55. #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
  56. #endif
  57. #ifdef USE_OPENSSL
  58. #define QUIC_CIPHERS \
  59. "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_" \
  60. "POLY1305_SHA256:TLS_AES_128_CCM_SHA256"
  61. #define QUIC_GROUPS "P-256:X25519:P-384:P-521"
  62. #elif defined(USE_GNUTLS)
  63. #define QUIC_PRIORITY \
  64. "NORMAL:-VERS-ALL:+VERS-TLS1.3:-CIPHER-ALL:+AES-128-GCM:+AES-256-GCM:" \
  65. "+CHACHA20-POLY1305:+AES-128-CCM:-GROUP-ALL:+GROUP-SECP256R1:" \
  66. "+GROUP-X25519:+GROUP-SECP384R1:+GROUP-SECP521R1:" \
  67. "%DISABLE_TLS13_COMPAT_MODE"
  68. #elif defined(USE_WOLFSSL)
  69. #define QUIC_CIPHERS \
  70. "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_" \
  71. "POLY1305_SHA256:TLS_AES_128_CCM_SHA256"
  72. #define QUIC_GROUPS "P-256:P-384:P-521"
  73. #endif
  74. #ifdef USE_OPENSSL
  75. static void keylog_callback(const SSL *ssl, const char *line)
  76. {
  77. (void)ssl;
  78. Curl_tls_keylog_write_line(line);
  79. }
  80. static CURLcode curl_ossl_init_ctx(struct quic_tls_ctx *ctx,
  81. struct Curl_cfilter *cf,
  82. struct Curl_easy *data,
  83. Curl_vquic_tls_ctx_setup *ctx_setup)
  84. {
  85. struct ssl_primary_config *conn_config;
  86. CURLcode result = CURLE_FAILED_INIT;
  87. DEBUGASSERT(!ctx->ssl_ctx);
  88. #ifdef USE_OPENSSL_QUIC
  89. ctx->ssl_ctx = SSL_CTX_new(OSSL_QUIC_client_method());
  90. #else
  91. ctx->ssl_ctx = SSL_CTX_new(TLS_method());
  92. #endif
  93. if(!ctx->ssl_ctx) {
  94. result = CURLE_OUT_OF_MEMORY;
  95. goto out;
  96. }
  97. conn_config = Curl_ssl_cf_get_primary_config(cf);
  98. if(!conn_config) {
  99. result = CURLE_FAILED_INIT;
  100. goto out;
  101. }
  102. if(ctx_setup) {
  103. result = ctx_setup(ctx, cf, data);
  104. if(result)
  105. goto out;
  106. }
  107. SSL_CTX_set_default_verify_paths(ctx->ssl_ctx);
  108. {
  109. const char *curves = conn_config->curves ?
  110. conn_config->curves : QUIC_GROUPS;
  111. if(!SSL_CTX_set1_curves_list(ctx->ssl_ctx, curves)) {
  112. failf(data, "failed setting curves list for QUIC: '%s'", curves);
  113. return CURLE_SSL_CIPHER;
  114. }
  115. }
  116. #ifndef OPENSSL_IS_BORINGSSL
  117. {
  118. const char *ciphers13 = conn_config->cipher_list13 ?
  119. conn_config->cipher_list13 : QUIC_CIPHERS;
  120. if(SSL_CTX_set_ciphersuites(ctx->ssl_ctx, ciphers13) != 1) {
  121. failf(data, "failed setting QUIC cipher suite: %s", ciphers13);
  122. return CURLE_SSL_CIPHER;
  123. }
  124. infof(data, "QUIC cipher selection: %s", ciphers13);
  125. }
  126. #endif
  127. /* Open the file if a TLS or QUIC backend has not done this before. */
  128. Curl_tls_keylog_open();
  129. if(Curl_tls_keylog_enabled()) {
  130. SSL_CTX_set_keylog_callback(ctx->ssl_ctx, keylog_callback);
  131. }
  132. /* OpenSSL always tries to verify the peer, this only says whether it should
  133. * fail to connect if the verification fails, or if it should continue
  134. * anyway. In the latter case the result of the verification is checked with
  135. * SSL_get_verify_result() below. */
  136. SSL_CTX_set_verify(ctx->ssl_ctx, conn_config->verifypeer ?
  137. SSL_VERIFY_PEER : SSL_VERIFY_NONE, NULL);
  138. /* give application a chance to interfere with SSL set up. */
  139. if(data->set.ssl.fsslctx) {
  140. /* When a user callback is installed to modify the SSL_CTX,
  141. * we need to do the full initialization before calling it.
  142. * See: #11800 */
  143. if(!ctx->x509_store_setup) {
  144. result = Curl_ssl_setup_x509_store(cf, data, ctx->ssl_ctx);
  145. if(result)
  146. goto out;
  147. ctx->x509_store_setup = TRUE;
  148. }
  149. Curl_set_in_callback(data, true);
  150. result = (*data->set.ssl.fsslctx)(data, ctx->ssl_ctx,
  151. data->set.ssl.fsslctxp);
  152. Curl_set_in_callback(data, false);
  153. if(result) {
  154. failf(data, "error signaled by ssl ctx callback");
  155. goto out;
  156. }
  157. }
  158. result = CURLE_OK;
  159. out:
  160. if(result && ctx->ssl_ctx) {
  161. SSL_CTX_free(ctx->ssl_ctx);
  162. ctx->ssl_ctx = NULL;
  163. }
  164. return result;
  165. }
  166. static CURLcode curl_ossl_set_client_cert(struct quic_tls_ctx *ctx,
  167. struct Curl_cfilter *cf,
  168. struct Curl_easy *data)
  169. {
  170. SSL_CTX *ssl_ctx = ctx->ssl_ctx;
  171. const struct ssl_config_data *ssl_config;
  172. ssl_config = Curl_ssl_cf_get_config(cf, data);
  173. DEBUGASSERT(ssl_config);
  174. if(ssl_config->primary.clientcert ||
  175. ssl_config->primary.cert_blob ||
  176. ssl_config->cert_type) {
  177. return Curl_ossl_set_client_cert(
  178. data, ssl_ctx, ssl_config->primary.clientcert,
  179. ssl_config->primary.cert_blob, ssl_config->cert_type,
  180. ssl_config->key, ssl_config->key_blob,
  181. ssl_config->key_type, ssl_config->key_passwd);
  182. }
  183. return CURLE_OK;
  184. }
  185. /** SSL callbacks ***/
  186. static CURLcode curl_ossl_init_ssl(struct quic_tls_ctx *ctx,
  187. struct Curl_easy *data,
  188. struct ssl_peer *peer,
  189. const char *alpn, size_t alpn_len,
  190. void *user_data)
  191. {
  192. DEBUGASSERT(!ctx->ssl);
  193. ctx->ssl = SSL_new(ctx->ssl_ctx);
  194. SSL_set_app_data(ctx->ssl, user_data);
  195. SSL_set_connect_state(ctx->ssl);
  196. #ifndef USE_OPENSSL_QUIC
  197. SSL_set_quic_use_legacy_codepoint(ctx->ssl, 0);
  198. #endif
  199. if(alpn)
  200. SSL_set_alpn_protos(ctx->ssl, (const uint8_t *)alpn, (int)alpn_len);
  201. if(peer->sni) {
  202. if(!SSL_set_tlsext_host_name(ctx->ssl, peer->sni)) {
  203. failf(data, "Failed set SNI");
  204. SSL_free(ctx->ssl);
  205. ctx->ssl = NULL;
  206. return CURLE_QUIC_CONNECT_ERROR;
  207. }
  208. }
  209. return CURLE_OK;
  210. }
  211. #elif defined(USE_GNUTLS)
  212. static int keylog_callback(gnutls_session_t session, const char *label,
  213. const gnutls_datum_t *secret)
  214. {
  215. gnutls_datum_t crandom;
  216. gnutls_datum_t srandom;
  217. gnutls_session_get_random(session, &crandom, &srandom);
  218. if(crandom.size != 32) {
  219. return -1;
  220. }
  221. Curl_tls_keylog_write(label, crandom.data, secret->data, secret->size);
  222. return 0;
  223. }
  224. static CURLcode curl_gtls_init_ctx(struct quic_tls_ctx *ctx,
  225. struct Curl_cfilter *cf,
  226. struct Curl_easy *data,
  227. struct ssl_peer *peer,
  228. const char *alpn, size_t alpn_len,
  229. Curl_vquic_tls_ctx_setup *ctx_setup,
  230. void *user_data)
  231. {
  232. struct ssl_primary_config *conn_config;
  233. CURLcode result;
  234. gnutls_datum_t alpns[5];
  235. /* this will need some attention when HTTPS proxy over QUIC get fixed */
  236. long * const pverifyresult = &data->set.ssl.certverifyresult;
  237. int rc;
  238. conn_config = Curl_ssl_cf_get_primary_config(cf);
  239. if(!conn_config)
  240. return CURLE_FAILED_INIT;
  241. DEBUGASSERT(ctx->gtls == NULL);
  242. ctx->gtls = calloc(1, sizeof(*(ctx->gtls)));
  243. if(!ctx->gtls)
  244. return CURLE_OUT_OF_MEMORY;
  245. result = gtls_client_init(data, conn_config, &data->set.ssl,
  246. peer, ctx->gtls, pverifyresult);
  247. if(result)
  248. return result;
  249. gnutls_session_set_ptr(ctx->gtls->session, user_data);
  250. if(ctx_setup) {
  251. result = ctx_setup(ctx, cf, data);
  252. if(result)
  253. return result;
  254. }
  255. rc = gnutls_priority_set_direct(ctx->gtls->session, QUIC_PRIORITY, NULL);
  256. if(rc < 0) {
  257. CURL_TRC_CF(data, cf, "gnutls_priority_set_direct failed: %s\n",
  258. gnutls_strerror(rc));
  259. return CURLE_QUIC_CONNECT_ERROR;
  260. }
  261. /* Open the file if a TLS or QUIC backend has not done this before. */
  262. Curl_tls_keylog_open();
  263. if(Curl_tls_keylog_enabled()) {
  264. gnutls_session_set_keylog_function(ctx->gtls->session, keylog_callback);
  265. }
  266. /* convert the ALPN string from our arguments to a list of strings
  267. * that gnutls wants and will convert internally back to this very
  268. * string for sending to the server. nice. */
  269. if(alpn) {
  270. size_t i, alen = alpn_len;
  271. unsigned char *s = (unsigned char *)alpn;
  272. unsigned char slen;
  273. for(i = 0; (i < ARRAYSIZE(alpns)) && alen; ++i) {
  274. slen = s[0];
  275. if(slen >= alen)
  276. return CURLE_FAILED_INIT;
  277. alpns[i].data = s + 1;
  278. alpns[i].size = slen;
  279. s += slen + 1;
  280. alen -= (size_t)slen + 1;
  281. }
  282. if(alen) /* not all alpn chars used, wrong format or too many */
  283. return CURLE_FAILED_INIT;
  284. if(i) {
  285. gnutls_alpn_set_protocols(ctx->gtls->session,
  286. alpns, (unsigned int)i,
  287. GNUTLS_ALPN_MANDATORY);
  288. }
  289. }
  290. return CURLE_OK;
  291. }
  292. #elif defined(USE_WOLFSSL)
  293. #if defined(HAVE_SECRET_CALLBACK)
  294. static void keylog_callback(const WOLFSSL *ssl, const char *line)
  295. {
  296. (void)ssl;
  297. Curl_tls_keylog_write_line(line);
  298. }
  299. #endif
  300. static CURLcode curl_wssl_init_ctx(struct quic_tls_ctx *ctx,
  301. struct Curl_cfilter *cf,
  302. struct Curl_easy *data,
  303. Curl_vquic_tls_ctx_setup *ctx_setup)
  304. {
  305. struct ssl_primary_config *conn_config;
  306. CURLcode result = CURLE_FAILED_INIT;
  307. conn_config = Curl_ssl_cf_get_primary_config(cf);
  308. if(!conn_config) {
  309. result = CURLE_FAILED_INIT;
  310. goto out;
  311. }
  312. ctx->ssl_ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  313. if(!ctx->ssl_ctx) {
  314. result = CURLE_OUT_OF_MEMORY;
  315. goto out;
  316. }
  317. if(ctx_setup) {
  318. result = ctx_setup(ctx, cf, data);
  319. if(result)
  320. goto out;
  321. }
  322. wolfSSL_CTX_set_default_verify_paths(ctx->ssl_ctx);
  323. if(wolfSSL_CTX_set_cipher_list(ctx->ssl_ctx, conn_config->cipher_list13 ?
  324. conn_config->cipher_list13 :
  325. QUIC_CIPHERS) != 1) {
  326. char error_buffer[256];
  327. ERR_error_string_n(ERR_get_error(), error_buffer, sizeof(error_buffer));
  328. failf(data, "wolfSSL failed to set ciphers: %s", error_buffer);
  329. result = CURLE_BAD_FUNCTION_ARGUMENT;
  330. goto out;
  331. }
  332. if(wolfSSL_CTX_set1_groups_list(ctx->ssl_ctx, conn_config->curves ?
  333. conn_config->curves :
  334. (char *)QUIC_GROUPS) != 1) {
  335. failf(data, "wolfSSL failed to set curves");
  336. result = CURLE_BAD_FUNCTION_ARGUMENT;
  337. goto out;
  338. }
  339. /* Open the file if a TLS or QUIC backend has not done this before. */
  340. Curl_tls_keylog_open();
  341. if(Curl_tls_keylog_enabled()) {
  342. #if defined(HAVE_SECRET_CALLBACK)
  343. wolfSSL_CTX_set_keylog_callback(ctx->ssl_ctx, keylog_callback);
  344. #else
  345. failf(data, "wolfSSL was built without keylog callback");
  346. result = CURLE_NOT_BUILT_IN;
  347. goto out;
  348. #endif
  349. }
  350. if(conn_config->verifypeer) {
  351. const char * const ssl_cafile = conn_config->CAfile;
  352. const char * const ssl_capath = conn_config->CApath;
  353. wolfSSL_CTX_set_verify(ctx->ssl_ctx, SSL_VERIFY_PEER, NULL);
  354. if(ssl_cafile || ssl_capath) {
  355. /* tell wolfSSL where to find CA certificates that are used to verify
  356. the server's certificate. */
  357. int rc =
  358. wolfSSL_CTX_load_verify_locations_ex(ctx->ssl_ctx, ssl_cafile,
  359. ssl_capath,
  360. WOLFSSL_LOAD_FLAG_IGNORE_ERR);
  361. if(SSL_SUCCESS != rc) {
  362. /* Fail if we insist on successfully verifying the server. */
  363. failf(data, "error setting certificate verify locations:"
  364. " CAfile: %s CApath: %s",
  365. ssl_cafile ? ssl_cafile : "none",
  366. ssl_capath ? ssl_capath : "none");
  367. result = CURLE_SSL_CACERT_BADFILE;
  368. goto out;
  369. }
  370. infof(data, " CAfile: %s", ssl_cafile ? ssl_cafile : "none");
  371. infof(data, " CApath: %s", ssl_capath ? ssl_capath : "none");
  372. }
  373. #ifdef CURL_CA_FALLBACK
  374. else {
  375. /* verifying the peer without any CA certificates won't work so
  376. use wolfssl's built-in default as fallback */
  377. wolfSSL_CTX_set_default_verify_paths(ctx->ssl_ctx);
  378. }
  379. #endif
  380. }
  381. else {
  382. wolfSSL_CTX_set_verify(ctx->ssl_ctx, SSL_VERIFY_NONE, NULL);
  383. }
  384. /* give application a chance to interfere with SSL set up. */
  385. if(data->set.ssl.fsslctx) {
  386. Curl_set_in_callback(data, true);
  387. result = (*data->set.ssl.fsslctx)(data, ctx->ssl_ctx,
  388. data->set.ssl.fsslctxp);
  389. Curl_set_in_callback(data, false);
  390. if(result) {
  391. failf(data, "error signaled by ssl ctx callback");
  392. goto out;
  393. }
  394. }
  395. result = CURLE_OK;
  396. out:
  397. if(result && ctx->ssl_ctx) {
  398. SSL_CTX_free(ctx->ssl_ctx);
  399. ctx->ssl_ctx = NULL;
  400. }
  401. return result;
  402. }
  403. /** SSL callbacks ***/
  404. static CURLcode curl_wssl_init_ssl(struct quic_tls_ctx *ctx,
  405. struct Curl_easy *data,
  406. struct ssl_peer *peer,
  407. const char *alpn, size_t alpn_len,
  408. void *user_data)
  409. {
  410. (void)data;
  411. DEBUGASSERT(!ctx->ssl);
  412. DEBUGASSERT(ctx->ssl_ctx);
  413. ctx->ssl = wolfSSL_new(ctx->ssl_ctx);
  414. wolfSSL_set_app_data(ctx->ssl, user_data);
  415. wolfSSL_set_connect_state(ctx->ssl);
  416. wolfSSL_set_quic_use_legacy_codepoint(ctx->ssl, 0);
  417. if(alpn)
  418. wolfSSL_set_alpn_protos(ctx->ssl, (const unsigned char *)alpn,
  419. (int)alpn_len);
  420. if(peer->sni) {
  421. wolfSSL_UseSNI(ctx->ssl, WOLFSSL_SNI_HOST_NAME,
  422. peer->sni, (unsigned short)strlen(peer->sni));
  423. }
  424. return CURLE_OK;
  425. }
  426. #endif /* defined(USE_WOLFSSL) */
  427. CURLcode Curl_vquic_tls_init(struct quic_tls_ctx *ctx,
  428. struct Curl_cfilter *cf,
  429. struct Curl_easy *data,
  430. struct ssl_peer *peer,
  431. const char *alpn, size_t alpn_len,
  432. Curl_vquic_tls_ctx_setup *ctx_setup,
  433. void *user_data)
  434. {
  435. CURLcode result;
  436. #ifdef USE_OPENSSL
  437. result = curl_ossl_init_ctx(ctx, cf, data, ctx_setup);
  438. if(result)
  439. return result;
  440. result = curl_ossl_set_client_cert(ctx, cf, data);
  441. if(result)
  442. return result;
  443. return curl_ossl_init_ssl(ctx, data, peer, alpn, alpn_len, user_data);
  444. #elif defined(USE_GNUTLS)
  445. (void)result;
  446. return curl_gtls_init_ctx(ctx, cf, data, peer, alpn, alpn_len,
  447. ctx_setup, user_data);
  448. #elif defined(USE_WOLFSSL)
  449. result = curl_wssl_init_ctx(ctx, cf, data, ctx_setup);
  450. if(result)
  451. return result;
  452. return curl_wssl_init_ssl(ctx, data, peer, alpn, alpn_len, user_data);
  453. #else
  454. #error "no TLS lib in used, should not happen"
  455. return CURLE_FAILED_INIT;
  456. #endif
  457. }
  458. void Curl_vquic_tls_cleanup(struct quic_tls_ctx *ctx)
  459. {
  460. #ifdef USE_OPENSSL
  461. if(ctx->ssl)
  462. SSL_free(ctx->ssl);
  463. if(ctx->ssl_ctx)
  464. SSL_CTX_free(ctx->ssl_ctx);
  465. #elif defined(USE_GNUTLS)
  466. if(ctx->gtls) {
  467. if(ctx->gtls->cred)
  468. gnutls_certificate_free_credentials(ctx->gtls->cred);
  469. if(ctx->gtls->session)
  470. gnutls_deinit(ctx->gtls->session);
  471. free(ctx->gtls);
  472. }
  473. #elif defined(USE_WOLFSSL)
  474. if(ctx->ssl)
  475. wolfSSL_free(ctx->ssl);
  476. if(ctx->ssl_ctx)
  477. wolfSSL_CTX_free(ctx->ssl_ctx);
  478. #endif
  479. memset(ctx, 0, sizeof(*ctx));
  480. }
  481. CURLcode Curl_vquic_tls_before_recv(struct quic_tls_ctx *ctx,
  482. struct Curl_cfilter *cf,
  483. struct Curl_easy *data)
  484. {
  485. #ifdef USE_OPENSSL
  486. if(!ctx->x509_store_setup) {
  487. CURLcode result = Curl_ssl_setup_x509_store(cf, data, ctx->ssl_ctx);
  488. if(result)
  489. return result;
  490. ctx->x509_store_setup = TRUE;
  491. }
  492. #else
  493. (void)ctx; (void)cf; (void)data;
  494. #endif
  495. return CURLE_OK;
  496. }
  497. CURLcode Curl_vquic_tls_verify_peer(struct quic_tls_ctx *ctx,
  498. struct Curl_cfilter *cf,
  499. struct Curl_easy *data,
  500. struct ssl_peer *peer)
  501. {
  502. struct ssl_primary_config *conn_config;
  503. CURLcode result = CURLE_OK;
  504. conn_config = Curl_ssl_cf_get_primary_config(cf);
  505. if(!conn_config)
  506. return CURLE_FAILED_INIT;
  507. if(conn_config->verifyhost) {
  508. #ifdef USE_OPENSSL
  509. X509 *server_cert;
  510. server_cert = SSL_get1_peer_certificate(ctx->ssl);
  511. if(!server_cert) {
  512. return CURLE_PEER_FAILED_VERIFICATION;
  513. }
  514. result = Curl_ossl_verifyhost(data, cf->conn, peer, server_cert);
  515. X509_free(server_cert);
  516. if(result)
  517. return result;
  518. #elif defined(USE_GNUTLS)
  519. result = Curl_gtls_verifyserver(data, ctx->gtls->session,
  520. conn_config, &data->set.ssl, peer,
  521. data->set.str[STRING_SSL_PINNEDPUBLICKEY]);
  522. if(result)
  523. return result;
  524. #elif defined(USE_WOLFSSL)
  525. if(!peer->sni ||
  526. wolfSSL_check_domain_name(ctx->ssl, peer->sni) == SSL_FAILURE)
  527. return CURLE_PEER_FAILED_VERIFICATION;
  528. #endif
  529. infof(data, "Verified certificate just fine");
  530. }
  531. else
  532. infof(data, "Skipped certificate verification");
  533. #ifdef USE_OPENSSL
  534. if(data->set.ssl.certinfo)
  535. /* asked to gather certificate info */
  536. (void)Curl_ossl_certchain(data, ctx->ssl);
  537. #endif
  538. return result;
  539. }
  540. #endif /* !ENABLE_QUIC && (USE_OPENSSL || USE_GNUTLS || USE_WOLFSSL) */