1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
24 * Source file for all CyaSSL-specific code for the TLS/SSL layer. No code
25 * but vtls.c should ever call or use these functions.
29 #include "curl_setup.h"
33 #define WOLFSSL_OPTIONS_IGNORE_SYS
34 /* CyaSSL's version.h, which should contain only the version, should come
35 before all other CyaSSL includes and be immediately followed by build config
36 aka options.h. https://curl.haxx.se/mail/lib-2015-04/0069.html */
37 #include <cyassl/version.h>
38 #if defined(HAVE_CYASSL_OPTIONS_H) && (LIBCYASSL_VERSION_HEX > 0x03004008)
39 #if defined(CYASSL_API) || defined(WOLFSSL_API)
40 /* Safety measure. If either is defined some API include was already included
41 and that's a problem since options.h hasn't been included yet. */
42 #error "CyaSSL API was included before the CyaSSL build options."
44 #include <cyassl/options.h>
53 #include "inet_pton.h"
55 #include "parsedate.h"
56 #include "connect.h" /* for the connect timeout */
60 #include "curl_printf.h"
62 #include <cyassl/ssl.h>
63 #ifdef HAVE_CYASSL_ERROR_SSL_H
64 #include <cyassl/error-ssl.h>
66 #include <cyassl/error.h>
68 #include <cyassl/ctaocrypt/random.h>
69 #include <cyassl/ctaocrypt/sha256.h>
73 /* The last #include files should be: */
74 #include "curl_memory.h"
77 #if LIBCYASSL_VERSION_HEX < 0x02007002 /* < 2.7.2 */
78 #define CYASSL_MAX_ERROR_SZ 80
81 /* To determine what functions are available we rely on one or both of:
82 - the user's options.h generated by CyaSSL/wolfSSL
83 - the symbols detected by curl's configure
84 Since they are markedly different from one another, and one or the other may
85 not be available, we do some checking below to bring things in sync. */
87 /* HAVE_ALPN is wolfSSL's build time symbol for enabling ALPN in options.h. */
89 #ifdef HAVE_WOLFSSL_USEALPN
94 /* WOLFSSL_ALLOW_SSLV3 is wolfSSL's build time symbol for enabling SSLv3 in
95 options.h, but is only seen in >= 3.6.6 since that's when they started
96 disabling SSLv3 by default. */
97 #ifndef WOLFSSL_ALLOW_SSLV3
98 #if (LIBCYASSL_VERSION_HEX < 0x03006006) || \
99 defined(HAVE_WOLFSSLV3_CLIENT_METHOD)
100 #define WOLFSSL_ALLOW_SSLV3
104 /* HAVE_SUPPORTED_CURVES is wolfSSL's build time symbol for enabling the ECC
105 supported curve extension in options.h. Note ECC is enabled separately. */
106 #ifndef HAVE_SUPPORTED_CURVES
107 #if defined(HAVE_CYASSL_CTX_USESUPPORTEDCURVE) || \
108 defined(HAVE_WOLFSSL_CTX_USESUPPORTEDCURVE)
109 #define HAVE_SUPPORTED_CURVES
113 static Curl_recv cyassl_recv;
114 static Curl_send cyassl_send;
117 static int do_file_type(const char *type)
119 if(!type || !type[0])
120 return SSL_FILETYPE_PEM;
121 if(Curl_raw_equal(type, "PEM"))
122 return SSL_FILETYPE_PEM;
123 if(Curl_raw_equal(type, "DER"))
124 return SSL_FILETYPE_ASN1;
129 * This function loads all the client/CA certificates and CRLs. Setup the TLS
130 * layer and do all necessary magic.
133 cyassl_connect_step1(struct connectdata *conn,
136 char error_buffer[CYASSL_MAX_ERROR_SZ];
137 struct Curl_easy *data = conn->data;
138 struct ssl_connect_data* conssl = &conn->ssl[sockindex];
139 SSL_METHOD* req_method = NULL;
140 curl_socket_t sockfd = conn->sock[sockindex];
143 #define use_sni(x) sni = (x)
145 #define use_sni(x) Curl_nop_stmt
148 if(conssl->state == ssl_connection_complete)
151 /* check to see if we've been told to use an explicit SSL/TLS version */
152 switch(data->set.ssl.version) {
153 case CURL_SSLVERSION_DEFAULT:
154 case CURL_SSLVERSION_TLSv1:
155 #if LIBCYASSL_VERSION_HEX >= 0x03003000 /* >= 3.3.0 */
156 /* minimum protocol version is set later after the CTX object is created */
157 req_method = SSLv23_client_method();
159 infof(data, "CyaSSL <3.3.0 cannot be configured to use TLS 1.0-1.2, "
160 "TLS 1.0 is used exclusively\n");
161 req_method = TLSv1_client_method();
165 case CURL_SSLVERSION_TLSv1_0:
166 req_method = TLSv1_client_method();
169 case CURL_SSLVERSION_TLSv1_1:
170 req_method = TLSv1_1_client_method();
173 case CURL_SSLVERSION_TLSv1_2:
174 req_method = TLSv1_2_client_method();
177 case CURL_SSLVERSION_SSLv3:
178 #ifdef WOLFSSL_ALLOW_SSLV3
179 req_method = SSLv3_client_method();
182 failf(data, "No support for SSLv3");
183 return CURLE_NOT_BUILT_IN;
186 case CURL_SSLVERSION_SSLv2:
187 failf(data, "CyaSSL does not support SSLv2");
188 return CURLE_SSL_CONNECT_ERROR;
190 failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
191 return CURLE_SSL_CONNECT_ERROR;
195 failf(data, "SSL: couldn't create a method!");
196 return CURLE_OUT_OF_MEMORY;
200 SSL_CTX_free(conssl->ctx);
201 conssl->ctx = SSL_CTX_new(req_method);
204 failf(data, "SSL: couldn't create a context!");
205 return CURLE_OUT_OF_MEMORY;
208 switch(data->set.ssl.version) {
209 case CURL_SSLVERSION_DEFAULT:
210 case CURL_SSLVERSION_TLSv1:
211 #if LIBCYASSL_VERSION_HEX > 0x03004006 /* > 3.4.6 */
212 /* Versions 3.3.0 to 3.4.6 we know the minimum protocol version is whatever
213 minimum version of TLS was built in and at least TLS 1.0. For later library
214 versions that could change (eg TLS 1.0 built in but defaults to TLS 1.1) so
215 we have this short circuit evaluation to find the minimum supported TLS
216 version. We use wolfSSL_CTX_SetMinVersion and not CyaSSL_SetMinVersion
217 because only the former will work before the user's CTX callback is called.
219 if((wolfSSL_CTX_SetMinVersion(conssl->ctx, WOLFSSL_TLSV1) != 1) &&
220 (wolfSSL_CTX_SetMinVersion(conssl->ctx, WOLFSSL_TLSV1_1) != 1) &&
221 (wolfSSL_CTX_SetMinVersion(conssl->ctx, WOLFSSL_TLSV1_2) != 1)) {
222 failf(data, "SSL: couldn't set the minimum protocol version");
223 return CURLE_SSL_CONNECT_ERROR;
229 #ifndef NO_FILESYSTEM
230 /* load trusted cacert */
231 if(data->set.str[STRING_SSL_CAFILE]) {
232 if(1 != SSL_CTX_load_verify_locations(conssl->ctx,
233 data->set.str[STRING_SSL_CAFILE],
234 data->set.str[STRING_SSL_CAPATH])) {
235 if(data->set.ssl.verifypeer) {
236 /* Fail if we insist on successfully verifying the server. */
237 failf(data, "error setting certificate verify locations:\n"
238 " CAfile: %s\n CApath: %s",
239 data->set.str[STRING_SSL_CAFILE]?
240 data->set.str[STRING_SSL_CAFILE]: "none",
241 data->set.str[STRING_SSL_CAPATH]?
242 data->set.str[STRING_SSL_CAPATH] : "none");
243 return CURLE_SSL_CACERT_BADFILE;
246 /* Just continue with a warning if no strict certificate
247 verification is required. */
248 infof(data, "error setting certificate verify locations,"
249 " continuing anyway:\n");
253 /* Everything is fine. */
254 infof(data, "successfully set certificate verify locations:\n");
259 data->set.str[STRING_SSL_CAFILE] ? data->set.str[STRING_SSL_CAFILE]:
261 data->set.str[STRING_SSL_CAPATH] ? data->set.str[STRING_SSL_CAPATH]:
265 /* Load the client certificate, and private key */
266 if(data->set.str[STRING_CERT] && data->set.str[STRING_KEY]) {
267 int file_type = do_file_type(data->set.str[STRING_CERT_TYPE]);
269 if(SSL_CTX_use_certificate_file(conssl->ctx, data->set.str[STRING_CERT],
271 failf(data, "unable to use client certificate (no key or wrong pass"
273 return CURLE_SSL_CONNECT_ERROR;
276 file_type = do_file_type(data->set.str[STRING_KEY_TYPE]);
277 if(SSL_CTX_use_PrivateKey_file(conssl->ctx, data->set.str[STRING_KEY],
279 failf(data, "unable to set private key");
280 return CURLE_SSL_CONNECT_ERROR;
283 #endif /* !NO_FILESYSTEM */
285 /* SSL always tries to verify the peer, this only says whether it should
286 * fail to connect if the verification fails, or if it should continue
287 * anyway. In the latter case the result of the verification is checked with
288 * SSL_get_verify_result() below. */
289 SSL_CTX_set_verify(conssl->ctx,
290 data->set.ssl.verifypeer?SSL_VERIFY_PEER:SSL_VERIFY_NONE,
295 struct in_addr addr4;
297 struct in6_addr addr6;
299 size_t hostname_len = strlen(conn->host.name);
300 if((hostname_len < USHRT_MAX) &&
301 (0 == Curl_inet_pton(AF_INET, conn->host.name, &addr4)) &&
303 (0 == Curl_inet_pton(AF_INET6, conn->host.name, &addr6)) &&
305 (CyaSSL_CTX_UseSNI(conssl->ctx, CYASSL_SNI_HOST_NAME, conn->host.name,
306 (unsigned short)hostname_len) != 1)) {
307 infof(data, "WARNING: failed to configure server name indication (SNI) "
313 #ifdef HAVE_SUPPORTED_CURVES
314 /* CyaSSL/wolfSSL does not send the supported ECC curves ext automatically:
315 https://github.com/wolfSSL/wolfssl/issues/366
316 The supported curves below are those also supported by OpenSSL 1.0.2 and
317 in the same order. */
318 CyaSSL_CTX_UseSupportedCurve(conssl->ctx, 0x17); /* secp256r1 */
319 CyaSSL_CTX_UseSupportedCurve(conssl->ctx, 0x19); /* secp521r1 */
320 CyaSSL_CTX_UseSupportedCurve(conssl->ctx, 0x18); /* secp384r1 */
323 /* give application a chance to interfere with SSL set up. */
324 if(data->set.ssl.fsslctx) {
325 CURLcode result = CURLE_OK;
326 result = (*data->set.ssl.fsslctx)(data, conssl->ctx,
327 data->set.ssl.fsslctxp);
329 failf(data, "error signaled by ssl ctx callback");
334 else if(data->set.ssl.verifypeer) {
335 failf(data, "SSL: Certificates couldn't be loaded because CyaSSL was built"
336 " with \"no filesystem\". Either disable peer verification"
337 " (insecure) or if you are building an application with libcurl you"
338 " can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
339 return CURLE_SSL_CONNECT_ERROR;
343 /* Let's make an SSL structure */
345 SSL_free(conssl->handle);
346 conssl->handle = SSL_new(conssl->ctx);
347 if(!conssl->handle) {
348 failf(data, "SSL: couldn't create a context (handle)!");
349 return CURLE_OUT_OF_MEMORY;
353 if(conn->bits.tls_enable_alpn) {
357 /* wolfSSL's ALPN protocol name list format is a comma separated string of
358 protocols in descending order of preference, eg: "h2,http/1.1" */
361 if(data->set.httpversion >= CURL_HTTP_VERSION_2) {
362 strcpy(protocols + strlen(protocols), NGHTTP2_PROTO_VERSION_ID ",");
363 infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID);
367 strcpy(protocols + strlen(protocols), ALPN_HTTP_1_1);
368 infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1);
370 if(wolfSSL_UseALPN(conssl->handle, protocols,
371 (unsigned)strlen(protocols),
372 WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) != SSL_SUCCESS) {
373 failf(data, "SSL: failed setting ALPN protocols");
374 return CURLE_SSL_CONNECT_ERROR;
377 #endif /* HAVE_ALPN */
379 /* Check if there's a cached ID we can/should use here! */
380 if(conn->ssl_config.sessionid) {
381 void *ssl_sessionid = NULL;
383 Curl_ssl_sessionid_lock(conn);
384 if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL)) {
385 /* we got a session id, use it! */
386 if(!SSL_set_session(conssl->handle, ssl_sessionid)) {
387 Curl_ssl_sessionid_unlock(conn);
388 failf(data, "SSL: SSL_set_session failed: %s",
389 ERR_error_string(SSL_get_error(conssl->handle, 0),
391 return CURLE_SSL_CONNECT_ERROR;
393 /* Informational message */
394 infof (data, "SSL re-using session ID\n");
396 Curl_ssl_sessionid_unlock(conn);
399 /* pass the raw socket into the SSL layer */
400 if(!SSL_set_fd(conssl->handle, (int)sockfd)) {
401 failf(data, "SSL: SSL_set_fd failed");
402 return CURLE_SSL_CONNECT_ERROR;
405 conssl->connecting_state = ssl_connect_2;
411 cyassl_connect_step2(struct connectdata *conn,
415 struct Curl_easy *data = conn->data;
416 struct ssl_connect_data* conssl = &conn->ssl[sockindex];
418 conn->recv[sockindex] = cyassl_recv;
419 conn->send[sockindex] = cyassl_send;
421 /* Enable RFC2818 checks */
422 if(data->set.ssl.verifyhost) {
423 ret = CyaSSL_check_domain_name(conssl->handle, conn->host.name);
424 if(ret == SSL_FAILURE)
425 return CURLE_OUT_OF_MEMORY;
428 ret = SSL_connect(conssl->handle);
430 char error_buffer[CYASSL_MAX_ERROR_SZ];
431 int detail = SSL_get_error(conssl->handle, ret);
433 if(SSL_ERROR_WANT_READ == detail) {
434 conssl->connecting_state = ssl_connect_2_reading;
437 else if(SSL_ERROR_WANT_WRITE == detail) {
438 conssl->connecting_state = ssl_connect_2_writing;
441 /* There is no easy way to override only the CN matching.
442 * This will enable the override of both mismatching SubjectAltNames
443 * as also mismatching CN fields */
444 else if(DOMAIN_NAME_MISMATCH == detail) {
446 failf(data, "\tsubject alt name(s) or common name do not match \"%s\"\n",
447 conn->host.dispname);
448 return CURLE_PEER_FAILED_VERIFICATION;
450 /* When the CyaSSL_check_domain_name() is used and you desire to continue
451 * on a DOMAIN_NAME_MISMATCH, i.e. 'data->set.ssl.verifyhost == 0',
452 * CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA error. The only
453 * way to do this is currently to switch the CyaSSL_check_domain_name()
454 * in and out based on the 'data->set.ssl.verifyhost' value. */
455 if(data->set.ssl.verifyhost) {
457 "\tsubject alt name(s) or common name do not match \"%s\"\n",
458 conn->host.dispname);
459 return CURLE_PEER_FAILED_VERIFICATION;
463 "\tsubject alt name(s) and/or common name do not match \"%s\"\n",
464 conn->host.dispname);
469 #if LIBCYASSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
470 else if(ASN_NO_SIGNER_E == detail) {
471 if(data->set.ssl.verifypeer) {
472 failf(data, "\tCA signer not available for verification\n");
473 return CURLE_SSL_CACERT_BADFILE;
476 /* Just continue with a warning if no strict certificate
477 verification is required. */
478 infof(data, "CA signer not available for verification, "
479 "continuing anyway\n");
484 failf(data, "SSL_connect failed with error %d: %s", detail,
485 ERR_error_string(detail, error_buffer));
486 return CURLE_SSL_CONNECT_ERROR;
490 if(data->set.str[STRING_SSL_PINNEDPUBLICKEY]) {
491 #ifdef KEEP_PEER_CERT
493 const char *x509_der;
495 curl_X509certificate x509_parsed;
496 curl_asn1Element *pubkey;
499 x509 = SSL_get_peer_certificate(conssl->handle);
501 failf(data, "SSL: failed retrieving server certificate");
502 return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
505 x509_der = (const char *)CyaSSL_X509_get_der(x509, &x509_der_len);
507 failf(data, "SSL: failed retrieving ASN.1 server certificate");
508 return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
511 memset(&x509_parsed, 0, sizeof x509_parsed);
512 Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len);
514 pubkey = &x509_parsed.subjectPublicKeyInfo;
515 if(!pubkey->header || pubkey->end <= pubkey->header) {
516 failf(data, "SSL: failed retrieving public key from server certificate");
517 return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
520 result = Curl_pin_peer_pubkey(data,
521 data->set.str[STRING_SSL_PINNEDPUBLICKEY],
522 (const unsigned char *)pubkey->header,
523 (size_t)(pubkey->end - pubkey->header));
525 failf(data, "SSL: public key does not match pinned public key!");
529 failf(data, "Library lacks pinning support built-in");
530 return CURLE_NOT_BUILT_IN;
535 if(conn->bits.tls_enable_alpn) {
537 char *protocol = NULL;
538 unsigned short protocol_len = 0;
540 rc = wolfSSL_ALPN_GetProtocol(conssl->handle, &protocol, &protocol_len);
542 if(rc == SSL_SUCCESS) {
543 infof(data, "ALPN, server accepted to use %.*s\n", protocol_len,
546 if(protocol_len == ALPN_HTTP_1_1_LENGTH &&
547 !memcmp(protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH))
548 conn->negnpn = CURL_HTTP_VERSION_1_1;
550 else if(data->set.httpversion >= CURL_HTTP_VERSION_2 &&
551 protocol_len == NGHTTP2_PROTO_VERSION_ID_LEN &&
552 !memcmp(protocol, NGHTTP2_PROTO_VERSION_ID,
553 NGHTTP2_PROTO_VERSION_ID_LEN))
554 conn->negnpn = CURL_HTTP_VERSION_2;
557 infof(data, "ALPN, unrecognized protocol %.*s\n", protocol_len,
560 else if(rc == SSL_ALPN_NOT_FOUND)
561 infof(data, "ALPN, server did not agree to a protocol\n");
563 failf(data, "ALPN, failure getting protocol, error %d", rc);
564 return CURLE_SSL_CONNECT_ERROR;
567 #endif /* HAVE_ALPN */
569 conssl->connecting_state = ssl_connect_3;
570 infof(data, "SSL connected\n");
577 cyassl_connect_step3(struct connectdata *conn,
580 CURLcode result = CURLE_OK;
581 struct Curl_easy *data = conn->data;
582 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
584 DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
586 if(conn->ssl_config.sessionid) {
588 SSL_SESSION *our_ssl_sessionid;
589 void *old_ssl_sessionid = NULL;
591 our_ssl_sessionid = SSL_get_session(connssl->handle);
593 Curl_ssl_sessionid_lock(conn);
594 incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL));
596 if(old_ssl_sessionid != our_ssl_sessionid) {
597 infof(data, "old SSL session ID is stale, removing\n");
598 Curl_ssl_delsessionid(conn, old_ssl_sessionid);
604 result = Curl_ssl_addsessionid(conn, our_ssl_sessionid,
605 0 /* unknown size */);
607 Curl_ssl_sessionid_unlock(conn);
608 failf(data, "failed to store ssl session");
612 Curl_ssl_sessionid_unlock(conn);
615 connssl->connecting_state = ssl_connect_done;
621 static ssize_t cyassl_send(struct connectdata *conn,
627 char error_buffer[CYASSL_MAX_ERROR_SZ];
628 int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
629 int rc = SSL_write(conn->ssl[sockindex].handle, mem, memlen);
632 int err = SSL_get_error(conn->ssl[sockindex].handle, rc);
635 case SSL_ERROR_WANT_READ:
636 case SSL_ERROR_WANT_WRITE:
637 /* there's data pending, re-invoke SSL_write() */
638 *curlcode = CURLE_AGAIN;
641 failf(conn->data, "SSL write: %s, errno %d",
642 ERR_error_string(err, error_buffer),
644 *curlcode = CURLE_SEND_ERROR;
651 void Curl_cyassl_close(struct connectdata *conn, int sockindex)
653 struct ssl_connect_data *conssl = &conn->ssl[sockindex];
656 (void)SSL_shutdown(conssl->handle);
657 SSL_free (conssl->handle);
658 conssl->handle = NULL;
661 SSL_CTX_free (conssl->ctx);
666 static ssize_t cyassl_recv(struct connectdata *conn,
672 char error_buffer[CYASSL_MAX_ERROR_SZ];
673 int buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
674 int nread = SSL_read(conn->ssl[num].handle, buf, buffsize);
677 int err = SSL_get_error(conn->ssl[num].handle, nread);
680 case SSL_ERROR_ZERO_RETURN: /* no more data */
682 case SSL_ERROR_WANT_READ:
683 case SSL_ERROR_WANT_WRITE:
684 /* there's data pending, re-invoke SSL_read() */
685 *curlcode = CURLE_AGAIN;
688 failf(conn->data, "SSL read: %s, errno %d",
689 ERR_error_string(err, error_buffer),
691 *curlcode = CURLE_RECV_ERROR;
699 void Curl_cyassl_session_free(void *ptr)
702 /* CyaSSL reuses sessions on own, no free */
706 size_t Curl_cyassl_version(char *buffer, size_t size)
708 #ifdef WOLFSSL_VERSION
709 return snprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
710 #elif defined(CYASSL_VERSION)
711 return snprintf(buffer, size, "CyaSSL/%s", CYASSL_VERSION);
713 return snprintf(buffer, size, "CyaSSL/%s", "<1.8.8");
718 int Curl_cyassl_init(void)
720 return (CyaSSL_Init() == SSL_SUCCESS);
724 bool Curl_cyassl_data_pending(const struct connectdata* conn, int connindex)
726 if(conn->ssl[connindex].handle) /* SSL is in use */
727 return (0 != SSL_pending(conn->ssl[connindex].handle)) ? TRUE : FALSE;
734 * This function is called to shut down the SSL layer but keep the
735 * socket open (CCC - Clear Command Channel)
737 int Curl_cyassl_shutdown(struct connectdata *conn, int sockindex)
740 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
742 if(connssl->handle) {
743 SSL_free (connssl->handle);
744 connssl->handle = NULL;
751 cyassl_connect_common(struct connectdata *conn,
757 struct Curl_easy *data = conn->data;
758 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
759 curl_socket_t sockfd = conn->sock[sockindex];
763 /* check if the connection has already been established */
764 if(ssl_connection_complete == connssl->state) {
769 if(ssl_connect_1==connssl->connecting_state) {
770 /* Find out how much more time we're allowed */
771 timeout_ms = Curl_timeleft(data, NULL, TRUE);
774 /* no need to continue if time already is up */
775 failf(data, "SSL connection timeout");
776 return CURLE_OPERATION_TIMEDOUT;
779 result = cyassl_connect_step1(conn, sockindex);
784 while(ssl_connect_2 == connssl->connecting_state ||
785 ssl_connect_2_reading == connssl->connecting_state ||
786 ssl_connect_2_writing == connssl->connecting_state) {
788 /* check allowed time left */
789 timeout_ms = Curl_timeleft(data, NULL, TRUE);
792 /* no need to continue if time already is up */
793 failf(data, "SSL connection timeout");
794 return CURLE_OPERATION_TIMEDOUT;
797 /* if ssl is expecting something, check if it's available. */
798 if(connssl->connecting_state == ssl_connect_2_reading
799 || connssl->connecting_state == ssl_connect_2_writing) {
801 curl_socket_t writefd = ssl_connect_2_writing==
802 connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
803 curl_socket_t readfd = ssl_connect_2_reading==
804 connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
806 what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
809 failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
810 return CURLE_SSL_CONNECT_ERROR;
819 failf(data, "SSL connection timeout");
820 return CURLE_OPERATION_TIMEDOUT;
823 /* socket is readable or writable */
826 /* Run transaction, and return to the caller if it failed or if
827 * this connection is part of a multi handle and this loop would
828 * execute again. This permits the owner of a multi handle to
829 * abort a connection attempt before step2 has completed while
830 * ensuring that a client using select() or epoll() will always
831 * have a valid fdset to wait on.
833 result = cyassl_connect_step2(conn, sockindex);
834 if(result || (nonblocking &&
835 (ssl_connect_2 == connssl->connecting_state ||
836 ssl_connect_2_reading == connssl->connecting_state ||
837 ssl_connect_2_writing == connssl->connecting_state)))
839 } /* repeat step2 until all transactions are done. */
841 if(ssl_connect_3 == connssl->connecting_state) {
842 result = cyassl_connect_step3(conn, sockindex);
847 if(ssl_connect_done == connssl->connecting_state) {
848 connssl->state = ssl_connection_complete;
849 conn->recv[sockindex] = cyassl_recv;
850 conn->send[sockindex] = cyassl_send;
856 /* Reset our connect state machine */
857 connssl->connecting_state = ssl_connect_1;
864 Curl_cyassl_connect_nonblocking(struct connectdata *conn,
868 return cyassl_connect_common(conn, sockindex, TRUE, done);
873 Curl_cyassl_connect(struct connectdata *conn,
879 result = cyassl_connect_common(conn, sockindex, FALSE, &done);
888 int Curl_cyassl_random(struct Curl_easy *data,
889 unsigned char *entropy,
896 if(length > UINT_MAX)
898 if(RNG_GenerateBlock(&rng, entropy, (unsigned)length))
903 void Curl_cyassl_sha256sum(const unsigned char *tmp, /* input */
905 unsigned char *sha256sum /* output */,
910 InitSha256(&SHA256pw);
911 Sha256Update(&SHA256pw, tmp, (word32)tmplen);
912 Sha256Final(&SHA256pw, sha256sum);