VirtualBox

source: vbox/trunk/src/libs/curl-7.87.0/lib/vtls/wolfssl.c@ 98326

最後變更 在這個檔案從98326是 98326,由 vboxsync 提交於 2 年 前

curl-7.87.0: Applied and adjusted our curl changes to 7.83.1. bugref:10356

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette