2011-02-28 21:02:47 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2015-01-17 11:41:03 -05:00
|
|
|
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2011-02-28 21:02:47 -05:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
|
|
|
* are also available at http://curl.haxx.se/docs/copyright.html.
|
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Source file for all CyaSSL-specific code for the TLS/SSL layer. No code
|
2013-12-25 05:30:51 -05:00
|
|
|
* but vtls.c should ever call or use these functions.
|
2011-02-28 21:02:47 -05:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-01-06 13:06:49 -05:00
|
|
|
#include "curl_setup.h"
|
2011-07-26 11:23:27 -04:00
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
#ifdef USE_CYASSL
|
|
|
|
|
2015-04-17 15:22:48 -04:00
|
|
|
#define WOLFSSL_OPTIONS_IGNORE_SYS
|
|
|
|
/* CyaSSL's version.h, which should contain only the version, should come
|
|
|
|
before all other CyaSSL includes and be immediately followed by build config
|
|
|
|
aka options.h. http://curl.haxx.se/mail/lib-2015-04/0069.html */
|
|
|
|
#include <cyassl/version.h>
|
|
|
|
#if defined(HAVE_CYASSL_OPTIONS_H) && (LIBCYASSL_VERSION_HEX > 0x03004008)
|
|
|
|
#if defined(CYASSL_API) || defined(WOLFSSL_API)
|
|
|
|
/* Safety measure. If either is defined some API include was already included
|
|
|
|
and that's a problem since options.h hasn't been included yet. */
|
|
|
|
#error "CyaSSL API was included before the CyaSSL build options."
|
|
|
|
#endif
|
|
|
|
#include <cyassl/options.h>
|
|
|
|
#endif
|
|
|
|
|
2011-08-24 08:00:42 -04:00
|
|
|
#ifdef HAVE_LIMITS_H
|
|
|
|
#include <limits.h>
|
|
|
|
#endif
|
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "sendf.h"
|
|
|
|
#include "inet_pton.h"
|
|
|
|
#include "cyassl.h"
|
2013-12-25 05:20:39 -05:00
|
|
|
#include "vtls.h"
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "parsedate.h"
|
|
|
|
#include "connect.h" /* for the connect timeout */
|
|
|
|
#include "select.h"
|
|
|
|
#include "rawstr.h"
|
2015-04-05 01:48:16 -04:00
|
|
|
#include "x509asn1.h"
|
2015-03-03 17:17:43 -05:00
|
|
|
#include "curl_printf.h"
|
2014-04-23 05:01:30 -04:00
|
|
|
|
2012-11-02 21:06:51 -04:00
|
|
|
#include <cyassl/ssl.h>
|
2014-04-23 05:01:30 -04:00
|
|
|
#ifdef HAVE_CYASSL_ERROR_SSL_H
|
|
|
|
#include <cyassl/error-ssl.h>
|
|
|
|
#else
|
2012-11-02 21:06:51 -04:00
|
|
|
#include <cyassl/error.h>
|
2014-04-23 05:01:30 -04:00
|
|
|
#endif
|
2014-07-30 18:09:13 -04:00
|
|
|
#include <cyassl/ctaocrypt/random.h>
|
2015-06-30 20:23:54 -04:00
|
|
|
#include <cyassl/ctaocrypt/sha256.h>
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2015-03-24 18:12:03 -04:00
|
|
|
/* The last #include files should be: */
|
|
|
|
#include "curl_memory.h"
|
2014-04-23 05:01:30 -04:00
|
|
|
#include "memdebug.h"
|
2011-02-28 21:02:47 -05:00
|
|
|
|
2015-04-04 02:12:03 -04:00
|
|
|
#if LIBCYASSL_VERSION_HEX < 0x02007002 /* < 2.7.2 */
|
|
|
|
#define CYASSL_MAX_ERROR_SZ 80
|
|
|
|
#endif
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
static Curl_recv cyassl_recv;
|
|
|
|
static Curl_send cyassl_send;
|
|
|
|
|
|
|
|
|
|
|
|
static int do_file_type(const char *type)
|
|
|
|
{
|
|
|
|
if(!type || !type[0])
|
|
|
|
return SSL_FILETYPE_PEM;
|
|
|
|
if(Curl_raw_equal(type, "PEM"))
|
|
|
|
return SSL_FILETYPE_PEM;
|
|
|
|
if(Curl_raw_equal(type, "DER"))
|
|
|
|
return SSL_FILETYPE_ASN1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function loads all the client/CA certificates and CRLs. Setup the TLS
|
|
|
|
* layer and do all necessary magic.
|
|
|
|
*/
|
|
|
|
static CURLcode
|
|
|
|
cyassl_connect_step1(struct connectdata *conn,
|
|
|
|
int sockindex)
|
|
|
|
{
|
2015-04-04 02:12:03 -04:00
|
|
|
char error_buffer[CYASSL_MAX_ERROR_SZ];
|
2011-02-28 21:02:47 -05:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct ssl_connect_data* conssl = &conn->ssl[sockindex];
|
|
|
|
SSL_METHOD* req_method = NULL;
|
|
|
|
void* ssl_sessionid = NULL;
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
2015-04-13 01:07:28 -04:00
|
|
|
#ifdef HAVE_SNI
|
|
|
|
bool sni = FALSE;
|
|
|
|
#define use_sni(x) sni = (x)
|
|
|
|
#else
|
|
|
|
#define use_sni(x) Curl_nop_stmt
|
|
|
|
#endif
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
if(conssl->state == ssl_connection_complete)
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
/* check to see if we've been told to use an explicit SSL/TLS version */
|
|
|
|
switch(data->set.ssl.version) {
|
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
2015-04-03 02:11:35 -04:00
|
|
|
#if LIBCYASSL_VERSION_HEX >= 0x03003000 /* >= 3.3.0 */
|
|
|
|
/* minimum protocol version is set later after the CTX object is created */
|
2015-03-25 02:40:00 -04:00
|
|
|
req_method = SSLv23_client_method();
|
|
|
|
#else
|
|
|
|
infof(data, "CyaSSL <3.3.0 cannot be configured to use TLS 1.0-1.2, "
|
2013-09-19 09:17:13 -04:00
|
|
|
"TLS 1.0 is used exclusively\n");
|
2011-02-28 21:02:47 -05:00
|
|
|
req_method = TLSv1_client_method();
|
2015-03-25 02:40:00 -04:00
|
|
|
#endif
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(TRUE);
|
2011-02-28 21:02:47 -05:00
|
|
|
break;
|
2013-09-19 09:17:13 -04:00
|
|
|
case CURL_SSLVERSION_TLSv1_0:
|
|
|
|
req_method = TLSv1_client_method();
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(TRUE);
|
2013-09-19 09:17:13 -04:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_1:
|
|
|
|
req_method = TLSv1_1_client_method();
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(TRUE);
|
2013-09-19 09:17:13 -04:00
|
|
|
break;
|
|
|
|
case CURL_SSLVERSION_TLSv1_2:
|
|
|
|
req_method = TLSv1_2_client_method();
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(TRUE);
|
2013-09-19 09:17:13 -04:00
|
|
|
break;
|
2011-02-28 21:02:47 -05:00
|
|
|
case CURL_SSLVERSION_SSLv3:
|
|
|
|
req_method = SSLv3_client_method();
|
2015-04-13 01:07:28 -04:00
|
|
|
use_sni(FALSE);
|
2011-02-28 21:02:47 -05:00
|
|
|
break;
|
2015-03-25 02:40:00 -04:00
|
|
|
case CURL_SSLVERSION_SSLv2:
|
|
|
|
failf(data, "CyaSSL does not support SSLv2");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
default:
|
|
|
|
failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!req_method) {
|
|
|
|
failf(data, "SSL: couldn't create a method!");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(conssl->ctx)
|
|
|
|
SSL_CTX_free(conssl->ctx);
|
|
|
|
conssl->ctx = SSL_CTX_new(req_method);
|
|
|
|
|
|
|
|
if(!conssl->ctx) {
|
|
|
|
failf(data, "SSL: couldn't create a context!");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2015-04-03 02:11:35 -04:00
|
|
|
switch(data->set.ssl.version) {
|
|
|
|
case CURL_SSLVERSION_DEFAULT:
|
|
|
|
case CURL_SSLVERSION_TLSv1:
|
|
|
|
#if LIBCYASSL_VERSION_HEX > 0x03004006 /* > 3.4.6 */
|
|
|
|
/* Versions 3.3.0 to 3.4.6 we know the minimum protocol version is whatever
|
|
|
|
minimum version of TLS was built in and at least TLS 1.0. For later library
|
|
|
|
versions that could change (eg TLS 1.0 built in but defaults to TLS 1.1) so
|
|
|
|
we have this short circuit evaluation to find the minimum supported TLS
|
|
|
|
version. We use wolfSSL_CTX_SetMinVersion and not CyaSSL_SetMinVersion
|
|
|
|
because only the former will work before the user's CTX callback is called.
|
|
|
|
*/
|
|
|
|
if((wolfSSL_CTX_SetMinVersion(conssl->ctx, WOLFSSL_TLSV1) != 1) &&
|
|
|
|
(wolfSSL_CTX_SetMinVersion(conssl->ctx, WOLFSSL_TLSV1_1) != 1) &&
|
|
|
|
(wolfSSL_CTX_SetMinVersion(conssl->ctx, WOLFSSL_TLSV1_2) != 1)) {
|
|
|
|
failf(data, "SSL: couldn't set the minimum protocol version");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-05-20 17:40:59 -04:00
|
|
|
#ifndef NO_FILESYSTEM
|
2011-02-28 21:02:47 -05:00
|
|
|
/* load trusted cacert */
|
|
|
|
if(data->set.str[STRING_SSL_CAFILE]) {
|
2015-04-02 01:51:07 -04:00
|
|
|
if(1 != SSL_CTX_load_verify_locations(conssl->ctx,
|
|
|
|
data->set.str[STRING_SSL_CAFILE],
|
|
|
|
data->set.str[STRING_SSL_CAPATH])) {
|
2011-04-20 09:17:42 -04:00
|
|
|
if(data->set.ssl.verifypeer) {
|
2014-07-22 18:43:47 -04:00
|
|
|
/* Fail if we insist on successfully verifying the server. */
|
2015-03-17 08:41:49 -04:00
|
|
|
failf(data, "error setting certificate verify locations:\n"
|
2012-06-14 07:32:05 -04:00
|
|
|
" CAfile: %s\n CApath: %s",
|
2011-02-28 21:02:47 -05:00
|
|
|
data->set.str[STRING_SSL_CAFILE]?
|
|
|
|
data->set.str[STRING_SSL_CAFILE]: "none",
|
|
|
|
data->set.str[STRING_SSL_CAPATH]?
|
|
|
|
data->set.str[STRING_SSL_CAPATH] : "none");
|
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
else {
|
2014-07-22 18:43:47 -04:00
|
|
|
/* Just continue with a warning if no strict certificate
|
2011-02-28 21:02:47 -05:00
|
|
|
verification is required. */
|
|
|
|
infof(data, "error setting certificate verify locations,"
|
|
|
|
" continuing anyway:\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Everything is fine. */
|
|
|
|
infof(data, "successfully set certificate verify locations:\n");
|
|
|
|
}
|
|
|
|
infof(data,
|
|
|
|
" CAfile: %s\n"
|
|
|
|
" CApath: %s\n",
|
|
|
|
data->set.str[STRING_SSL_CAFILE] ? data->set.str[STRING_SSL_CAFILE]:
|
|
|
|
"none",
|
|
|
|
data->set.str[STRING_SSL_CAPATH] ? data->set.str[STRING_SSL_CAPATH]:
|
|
|
|
"none");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the client certificate, and private key */
|
|
|
|
if(data->set.str[STRING_CERT] && data->set.str[STRING_KEY]) {
|
|
|
|
int file_type = do_file_type(data->set.str[STRING_CERT_TYPE]);
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(SSL_CTX_use_certificate_file(conssl->ctx, data->set.str[STRING_CERT],
|
2011-02-28 21:02:47 -05:00
|
|
|
file_type) != 1) {
|
|
|
|
failf(data, "unable to use client certificate (no key or wrong pass"
|
|
|
|
" phrase?)");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
file_type = do_file_type(data->set.str[STRING_KEY_TYPE]);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(SSL_CTX_use_PrivateKey_file(conssl->ctx, data->set.str[STRING_KEY],
|
2011-02-28 21:02:47 -05:00
|
|
|
file_type) != 1) {
|
|
|
|
failf(data, "unable to set private key");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2015-03-28 00:16:08 -04:00
|
|
|
#endif /* !NO_FILESYSTEM */
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
/* SSL always tries to verify the peer, this only says whether it should
|
|
|
|
* fail to connect if the verification fails, or if it should continue
|
|
|
|
* anyway. In the latter case the result of the verification is checked with
|
|
|
|
* SSL_get_verify_result() below. */
|
|
|
|
SSL_CTX_set_verify(conssl->ctx,
|
|
|
|
data->set.ssl.verifypeer?SSL_VERIFY_PEER:SSL_VERIFY_NONE,
|
|
|
|
NULL);
|
|
|
|
|
2015-04-13 01:07:28 -04:00
|
|
|
#ifdef HAVE_SNI
|
|
|
|
if(sni) {
|
|
|
|
struct in_addr addr4;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct in6_addr addr6;
|
|
|
|
#endif
|
|
|
|
size_t hostname_len = strlen(conn->host.name);
|
|
|
|
if((hostname_len < USHRT_MAX) &&
|
|
|
|
(0 == Curl_inet_pton(AF_INET, conn->host.name, &addr4)) &&
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
(0 == Curl_inet_pton(AF_INET6, conn->host.name, &addr6)) &&
|
|
|
|
#endif
|
|
|
|
(CyaSSL_CTX_UseSNI(conssl->ctx, CYASSL_SNI_HOST_NAME, conn->host.name,
|
|
|
|
(unsigned short)hostname_len) != 1)) {
|
|
|
|
infof(data, "WARNING: failed to configure server name indication (SNI) "
|
|
|
|
"TLS extension\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-27 07:22:32 -04:00
|
|
|
/* give application a chance to interfere with SSL set up. */
|
|
|
|
if(data->set.ssl.fsslctx) {
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
result = (*data->set.ssl.fsslctx)(data, conssl->ctx,
|
2015-03-28 00:16:08 -04:00
|
|
|
data->set.ssl.fsslctxp);
|
2015-03-27 07:22:32 -04:00
|
|
|
if(result) {
|
|
|
|
failf(data, "error signaled by ssl ctx callback");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef NO_FILESYSTEM
|
|
|
|
else if(data->set.ssl.verifypeer) {
|
2015-03-28 00:16:08 -04:00
|
|
|
failf(data, "SSL: Certificates couldn't be loaded because CyaSSL was built"
|
|
|
|
" with \"no filesystem\". Either disable peer verification"
|
|
|
|
" (insecure) or if you are building an application with libcurl you"
|
|
|
|
" can load certificates via CURLOPT_SSL_CTX_FUNCTION.");
|
2015-03-27 07:22:32 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
/* Let's make an SSL structure */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conssl->handle)
|
2011-02-28 21:02:47 -05:00
|
|
|
SSL_free(conssl->handle);
|
|
|
|
conssl->handle = SSL_new(conssl->ctx);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!conssl->handle) {
|
2011-02-28 21:02:47 -05:00
|
|
|
failf(data, "SSL: couldn't create a context (handle)!");
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if there's a cached ID we can/should use here! */
|
|
|
|
if(!Curl_ssl_getsessionid(conn, &ssl_sessionid, NULL)) {
|
|
|
|
/* we got a session id, use it! */
|
|
|
|
if(!SSL_set_session(conssl->handle, ssl_sessionid)) {
|
|
|
|
failf(data, "SSL: SSL_set_session failed: %s",
|
2015-04-04 02:12:03 -04:00
|
|
|
ERR_error_string(SSL_get_error(conssl->handle, 0), error_buffer));
|
2011-02-28 21:02:47 -05:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
/* Informational message */
|
|
|
|
infof (data, "SSL re-using session ID\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pass the raw socket into the SSL layer */
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!SSL_set_fd(conssl->handle, (int)sockfd)) {
|
2011-02-28 21:02:47 -05:00
|
|
|
failf(data, "SSL: SSL_set_fd failed");
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
conssl->connecting_state = ssl_connect_2;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CURLcode
|
|
|
|
cyassl_connect_step2(struct connectdata *conn,
|
|
|
|
int sockindex)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct ssl_connect_data* conssl = &conn->ssl[sockindex];
|
|
|
|
|
|
|
|
conn->recv[sockindex] = cyassl_recv;
|
|
|
|
conn->send[sockindex] = cyassl_send;
|
|
|
|
|
2012-11-02 21:06:51 -04:00
|
|
|
/* Enable RFC2818 checks */
|
|
|
|
if(data->set.ssl.verifyhost) {
|
|
|
|
ret = CyaSSL_check_domain_name(conssl->handle, conn->host.name);
|
|
|
|
if(ret == SSL_FAILURE)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
ret = SSL_connect(conssl->handle);
|
2011-04-20 09:17:42 -04:00
|
|
|
if(ret != 1) {
|
2015-04-04 02:12:03 -04:00
|
|
|
char error_buffer[CYASSL_MAX_ERROR_SZ];
|
2011-02-28 21:02:47 -05:00
|
|
|
int detail = SSL_get_error(conssl->handle, ret);
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(SSL_ERROR_WANT_READ == detail) {
|
2011-02-28 21:02:47 -05:00
|
|
|
conssl->connecting_state = ssl_connect_2_reading;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2012-11-02 21:06:51 -04:00
|
|
|
else if(SSL_ERROR_WANT_WRITE == detail) {
|
2011-02-28 21:02:47 -05:00
|
|
|
conssl->connecting_state = ssl_connect_2_writing;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2012-11-02 21:06:51 -04:00
|
|
|
/* There is no easy way to override only the CN matching.
|
|
|
|
* This will enable the override of both mismatching SubjectAltNames
|
|
|
|
* as also mismatching CN fields */
|
|
|
|
else if(DOMAIN_NAME_MISMATCH == detail) {
|
|
|
|
#if 1
|
|
|
|
failf(data, "\tsubject alt name(s) or common name do not match \"%s\"\n",
|
|
|
|
conn->host.dispname);
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
#else
|
|
|
|
/* When the CyaSSL_check_domain_name() is used and you desire to continue
|
|
|
|
* on a DOMAIN_NAME_MISMATCH, i.e. 'data->set.ssl.verifyhost == 0',
|
|
|
|
* CyaSSL version 2.4.0 will fail with an INCOMPLETE_DATA error. The only
|
|
|
|
* way to do this is currently to switch the CyaSSL_check_domain_name()
|
|
|
|
* in and out based on the 'data->set.ssl.verifyhost' value. */
|
|
|
|
if(data->set.ssl.verifyhost) {
|
|
|
|
failf(data,
|
|
|
|
"\tsubject alt name(s) or common name do not match \"%s\"\n",
|
|
|
|
conn->host.dispname);
|
|
|
|
return CURLE_PEER_FAILED_VERIFICATION;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
infof(data,
|
|
|
|
"\tsubject alt name(s) and/or common name do not match \"%s\"\n",
|
|
|
|
conn->host.dispname);
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2014-07-30 18:31:36 -04:00
|
|
|
#if LIBCYASSL_VERSION_HEX >= 0x02007000 /* 2.7.0 */
|
2014-07-22 18:43:47 -04:00
|
|
|
else if(ASN_NO_SIGNER_E == detail) {
|
|
|
|
if(data->set.ssl.verifypeer) {
|
|
|
|
failf(data, "\tCA signer not available for verification\n");
|
|
|
|
return CURLE_SSL_CACERT_BADFILE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* Just continue with a warning if no strict certificate
|
|
|
|
verification is required. */
|
|
|
|
infof(data, "CA signer not available for verification, "
|
|
|
|
"continuing anyway\n");
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 04:07:42 -04:00
|
|
|
#endif
|
2012-11-02 21:06:51 -04:00
|
|
|
else {
|
|
|
|
failf(data, "SSL_connect failed with error %d: %s", detail,
|
2011-02-28 21:02:47 -05:00
|
|
|
ERR_error_string(detail, error_buffer));
|
2012-11-02 21:06:51 -04:00
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
2015-04-05 01:48:16 -04:00
|
|
|
if(data->set.str[STRING_SSL_PINNEDPUBLICKEY]) {
|
|
|
|
X509 *x509;
|
|
|
|
const char *x509_der;
|
|
|
|
int x509_der_len;
|
|
|
|
curl_X509certificate x509_parsed;
|
|
|
|
curl_asn1Element *pubkey;
|
|
|
|
CURLcode result;
|
|
|
|
|
|
|
|
x509 = SSL_get_peer_certificate(conssl->handle);
|
|
|
|
if(!x509) {
|
|
|
|
failf(data, "SSL: failed retrieving server certificate");
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
x509_der = (const char *)CyaSSL_X509_get_der(x509, &x509_der_len);
|
|
|
|
if(!x509_der) {
|
|
|
|
failf(data, "SSL: failed retrieving ASN.1 server certificate");
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&x509_parsed, 0, sizeof x509_parsed);
|
|
|
|
Curl_parseX509(&x509_parsed, x509_der, x509_der + x509_der_len);
|
|
|
|
|
|
|
|
pubkey = &x509_parsed.subjectPublicKeyInfo;
|
|
|
|
if(!pubkey->header || pubkey->end <= pubkey->header) {
|
|
|
|
failf(data, "SSL: failed retrieving public key from server certificate");
|
|
|
|
return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
|
|
|
|
}
|
|
|
|
|
2015-09-12 17:35:12 -04:00
|
|
|
result = Curl_pin_peer_pubkey(data,
|
|
|
|
data->set.str[STRING_SSL_PINNEDPUBLICKEY],
|
2015-04-05 01:48:16 -04:00
|
|
|
(const unsigned char *)pubkey->header,
|
|
|
|
(size_t)(pubkey->end - pubkey->header));
|
|
|
|
if(result) {
|
|
|
|
failf(data, "SSL: public key does not match pinned public key!");
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
conssl->connecting_state = ssl_connect_3;
|
2012-01-16 15:14:05 -05:00
|
|
|
infof(data, "SSL connected\n");
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CURLcode
|
|
|
|
cyassl_connect_step3(struct connectdata *conn,
|
|
|
|
int sockindex)
|
|
|
|
{
|
2014-12-25 06:39:47 -05:00
|
|
|
CURLcode result = CURLE_OK;
|
2011-02-28 21:02:47 -05:00
|
|
|
void *old_ssl_sessionid=NULL;
|
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
2014-12-25 12:15:15 -05:00
|
|
|
bool incache;
|
2011-02-28 21:02:47 -05:00
|
|
|
SSL_SESSION *our_ssl_sessionid;
|
|
|
|
|
|
|
|
DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
|
|
|
|
|
|
|
|
our_ssl_sessionid = SSL_get_session(connssl->handle);
|
|
|
|
|
|
|
|
incache = !(Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL));
|
2011-04-20 09:17:42 -04:00
|
|
|
if(incache) {
|
|
|
|
if(old_ssl_sessionid != our_ssl_sessionid) {
|
2011-02-28 21:02:47 -05:00
|
|
|
infof(data, "old SSL session ID is stale, removing\n");
|
|
|
|
Curl_ssl_delsessionid(conn, old_ssl_sessionid);
|
|
|
|
incache = FALSE;
|
|
|
|
}
|
|
|
|
}
|
2014-12-25 12:15:15 -05:00
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(!incache) {
|
2014-12-25 06:39:47 -05:00
|
|
|
result = Curl_ssl_addsessionid(conn, our_ssl_sessionid,
|
|
|
|
0 /* unknown size */);
|
|
|
|
if(result) {
|
2011-02-28 21:02:47 -05:00
|
|
|
failf(data, "failed to store ssl session");
|
2014-12-25 06:39:47 -05:00
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
connssl->connecting_state = ssl_connect_done;
|
|
|
|
|
2014-12-25 06:39:47 -05:00
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ssize_t cyassl_send(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
const void *mem,
|
|
|
|
size_t len,
|
|
|
|
CURLcode *curlcode)
|
|
|
|
{
|
2015-04-04 02:12:03 -04:00
|
|
|
char error_buffer[CYASSL_MAX_ERROR_SZ];
|
2011-02-28 21:02:47 -05:00
|
|
|
int memlen = (len > (size_t)INT_MAX) ? INT_MAX : (int)len;
|
|
|
|
int rc = SSL_write(conn->ssl[sockindex].handle, mem, memlen);
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(rc < 0) {
|
2011-02-28 21:02:47 -05:00
|
|
|
int err = SSL_get_error(conn->ssl[sockindex].handle, rc);
|
|
|
|
|
|
|
|
switch(err) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
/* there's data pending, re-invoke SSL_write() */
|
|
|
|
*curlcode = CURLE_AGAIN;
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
failf(conn->data, "SSL write: %s, errno %d",
|
|
|
|
ERR_error_string(err, error_buffer),
|
|
|
|
SOCKERRNO);
|
|
|
|
*curlcode = CURLE_SEND_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Curl_cyassl_close(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
|
|
|
struct ssl_connect_data *conssl = &conn->ssl[sockindex];
|
|
|
|
|
|
|
|
if(conssl->handle) {
|
|
|
|
(void)SSL_shutdown(conssl->handle);
|
|
|
|
SSL_free (conssl->handle);
|
|
|
|
conssl->handle = NULL;
|
|
|
|
}
|
|
|
|
if(conssl->ctx) {
|
|
|
|
SSL_CTX_free (conssl->ctx);
|
|
|
|
conssl->ctx = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t cyassl_recv(struct connectdata *conn,
|
|
|
|
int num,
|
|
|
|
char *buf,
|
|
|
|
size_t buffersize,
|
|
|
|
CURLcode *curlcode)
|
|
|
|
{
|
2015-04-04 02:12:03 -04:00
|
|
|
char error_buffer[CYASSL_MAX_ERROR_SZ];
|
2011-02-28 21:02:47 -05:00
|
|
|
int buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
|
|
|
|
int nread = SSL_read(conn->ssl[num].handle, buf, buffsize);
|
|
|
|
|
2011-04-20 09:17:42 -04:00
|
|
|
if(nread < 0) {
|
2011-02-28 21:02:47 -05:00
|
|
|
int err = SSL_get_error(conn->ssl[num].handle, nread);
|
|
|
|
|
|
|
|
switch(err) {
|
|
|
|
case SSL_ERROR_ZERO_RETURN: /* no more data */
|
|
|
|
break;
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
|
|
|
/* there's data pending, re-invoke SSL_read() */
|
|
|
|
*curlcode = CURLE_AGAIN;
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
failf(conn->data, "SSL read: %s, errno %d",
|
|
|
|
ERR_error_string(err, error_buffer),
|
|
|
|
SOCKERRNO);
|
|
|
|
*curlcode = CURLE_RECV_ERROR;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Curl_cyassl_session_free(void *ptr)
|
|
|
|
{
|
2011-03-08 08:09:20 -05:00
|
|
|
(void)ptr;
|
2011-02-28 21:02:47 -05:00
|
|
|
/* CyaSSL reuses sessions on own, no free */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t Curl_cyassl_version(char *buffer, size_t size)
|
|
|
|
{
|
2015-03-25 02:33:44 -04:00
|
|
|
#ifdef WOLFSSL_VERSION
|
|
|
|
return snprintf(buffer, size, "wolfSSL/%s", WOLFSSL_VERSION);
|
2015-03-20 18:49:53 -04:00
|
|
|
#elif defined(CYASSL_VERSION)
|
2011-02-28 21:02:47 -05:00
|
|
|
return snprintf(buffer, size, "CyaSSL/%s", CYASSL_VERSION);
|
|
|
|
#else
|
|
|
|
return snprintf(buffer, size, "CyaSSL/%s", "<1.8.8");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Curl_cyassl_init(void)
|
|
|
|
{
|
2015-03-30 19:09:26 -04:00
|
|
|
return (CyaSSL_Init() == SSL_SUCCESS);
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Curl_cyassl_data_pending(const struct connectdata* conn, int connindex)
|
|
|
|
{
|
2011-04-20 09:17:42 -04:00
|
|
|
if(conn->ssl[connindex].handle) /* SSL is in use */
|
2011-09-05 14:46:09 -04:00
|
|
|
return (0 != SSL_pending(conn->ssl[connindex].handle)) ? TRUE : FALSE;
|
2011-02-28 21:02:47 -05:00
|
|
|
else
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called to shut down the SSL layer but keep the
|
|
|
|
* socket open (CCC - Clear Command Channel)
|
|
|
|
*/
|
|
|
|
int Curl_cyassl_shutdown(struct connectdata *conn, int sockindex)
|
|
|
|
{
|
|
|
|
int retval = 0;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
|
|
|
|
if(connssl->handle) {
|
|
|
|
SSL_free (connssl->handle);
|
|
|
|
connssl->handle = NULL;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static CURLcode
|
|
|
|
cyassl_connect_common(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
bool nonblocking,
|
|
|
|
bool *done)
|
|
|
|
{
|
2014-12-25 06:39:47 -05:00
|
|
|
CURLcode result;
|
2011-02-28 21:02:47 -05:00
|
|
|
struct SessionHandle *data = conn->data;
|
|
|
|
struct ssl_connect_data *connssl = &conn->ssl[sockindex];
|
|
|
|
curl_socket_t sockfd = conn->sock[sockindex];
|
|
|
|
long timeout_ms;
|
|
|
|
int what;
|
|
|
|
|
|
|
|
/* check if the connection has already been established */
|
|
|
|
if(ssl_connection_complete == connssl->state) {
|
|
|
|
*done = TRUE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ssl_connect_1==connssl->connecting_state) {
|
|
|
|
/* Find out how much more time we're allowed */
|
|
|
|
timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* no need to continue if time already is up */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
2014-12-25 06:39:47 -05:00
|
|
|
|
|
|
|
result = cyassl_connect_step1(conn, sockindex);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
while(ssl_connect_2 == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_reading == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_writing == connssl->connecting_state) {
|
|
|
|
|
|
|
|
/* check allowed time left */
|
|
|
|
timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
|
|
|
|
|
|
|
if(timeout_ms < 0) {
|
|
|
|
/* no need to continue if time already is up */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if ssl is expecting something, check if it's available. */
|
|
|
|
if(connssl->connecting_state == ssl_connect_2_reading
|
|
|
|
|| connssl->connecting_state == ssl_connect_2_writing) {
|
|
|
|
|
|
|
|
curl_socket_t writefd = ssl_connect_2_writing==
|
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
|
|
|
curl_socket_t readfd = ssl_connect_2_reading==
|
|
|
|
connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
|
|
|
|
|
2011-06-04 15:19:14 -04:00
|
|
|
what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
|
2011-02-28 21:02:47 -05:00
|
|
|
if(what < 0) {
|
|
|
|
/* fatal error */
|
|
|
|
failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
|
|
|
|
return CURLE_SSL_CONNECT_ERROR;
|
|
|
|
}
|
|
|
|
else if(0 == what) {
|
|
|
|
if(nonblocking) {
|
|
|
|
*done = FALSE;
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* timeout */
|
|
|
|
failf(data, "SSL connection timeout");
|
|
|
|
return CURLE_OPERATION_TIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* socket is readable or writable */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run transaction, and return to the caller if it failed or if
|
|
|
|
* this connection is part of a multi handle and this loop would
|
|
|
|
* execute again. This permits the owner of a multi handle to
|
|
|
|
* abort a connection attempt before step2 has completed while
|
|
|
|
* ensuring that a client using select() or epoll() will always
|
|
|
|
* have a valid fdset to wait on.
|
|
|
|
*/
|
2014-12-25 06:39:47 -05:00
|
|
|
result = cyassl_connect_step2(conn, sockindex);
|
|
|
|
if(result || (nonblocking &&
|
|
|
|
(ssl_connect_2 == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_reading == connssl->connecting_state ||
|
|
|
|
ssl_connect_2_writing == connssl->connecting_state)))
|
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
} /* repeat step2 until all transactions are done. */
|
|
|
|
|
2014-12-25 06:39:47 -05:00
|
|
|
if(ssl_connect_3 == connssl->connecting_state) {
|
|
|
|
result = cyassl_connect_step3(conn, sockindex);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
}
|
|
|
|
|
2014-12-25 06:39:47 -05:00
|
|
|
if(ssl_connect_done == connssl->connecting_state) {
|
2011-02-28 21:02:47 -05:00
|
|
|
connssl->state = ssl_connection_complete;
|
|
|
|
conn->recv[sockindex] = cyassl_recv;
|
|
|
|
conn->send[sockindex] = cyassl_send;
|
|
|
|
*done = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*done = FALSE;
|
|
|
|
|
|
|
|
/* Reset our connect state machine */
|
|
|
|
connssl->connecting_state = ssl_connect_1;
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CURLcode
|
|
|
|
Curl_cyassl_connect_nonblocking(struct connectdata *conn,
|
|
|
|
int sockindex,
|
|
|
|
bool *done)
|
|
|
|
{
|
|
|
|
return cyassl_connect_common(conn, sockindex, TRUE, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
CURLcode
|
|
|
|
Curl_cyassl_connect(struct connectdata *conn,
|
|
|
|
int sockindex)
|
|
|
|
{
|
2014-12-25 06:39:47 -05:00
|
|
|
CURLcode result;
|
2011-02-28 21:02:47 -05:00
|
|
|
bool done = FALSE;
|
|
|
|
|
2014-12-25 06:39:47 -05:00
|
|
|
result = cyassl_connect_common(conn, sockindex, FALSE, &done);
|
|
|
|
if(result)
|
|
|
|
return result;
|
2011-02-28 21:02:47 -05:00
|
|
|
|
|
|
|
DEBUGASSERT(done);
|
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-30 18:09:13 -04:00
|
|
|
int Curl_cyassl_random(struct SessionHandle *data,
|
|
|
|
unsigned char *entropy,
|
|
|
|
size_t length)
|
|
|
|
{
|
|
|
|
RNG rng;
|
|
|
|
(void)data;
|
|
|
|
if(InitRng(&rng))
|
|
|
|
return 1;
|
2015-03-25 02:37:20 -04:00
|
|
|
if(length > UINT_MAX)
|
|
|
|
return 1;
|
|
|
|
if(RNG_GenerateBlock(&rng, entropy, (unsigned)length))
|
2014-07-30 18:09:13 -04:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-06-30 20:23:54 -04:00
|
|
|
void Curl_cyassl_sha256sum(const unsigned char *tmp, /* input */
|
|
|
|
size_t tmplen,
|
|
|
|
unsigned char *sha256sum /* output */,
|
|
|
|
size_t unused)
|
|
|
|
{
|
|
|
|
Sha256 SHA256pw;
|
|
|
|
(void)unused;
|
|
|
|
InitSha256(&SHA256pw);
|
|
|
|
Sha256Update(&SHA256pw, tmp, tmplen);
|
|
|
|
Sha256Final(&SHA256pw, sha256sum);
|
|
|
|
}
|
|
|
|
|
2011-02-28 21:02:47 -05:00
|
|
|
#endif
|