2003-06-21 01:26:56 +02:00
|
|
|
/*
|
|
|
|
* Wininet - networking layer. Uses unix sockets or OpenSSL.
|
|
|
|
*
|
|
|
|
* Copyright 2002 TransGaming Technologies Inc.
|
|
|
|
*
|
|
|
|
* David Hammerton
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-06-21 01:26:56 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2009-12-14 01:05:35 +01:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
|
2008-12-09 11:33:25 +01:00
|
|
|
#if defined(__MINGW32__) || defined (_MSC_VER)
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#endif
|
|
|
|
|
2008-06-02 12:14:37 +02:00
|
|
|
#include <sys/types.h>
|
2008-03-24 13:12:49 +01:00
|
|
|
#ifdef HAVE_POLL_H
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_POLL_H
|
|
|
|
# include <sys/poll.h>
|
|
|
|
#endif
|
2003-06-27 06:04:00 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2003-06-21 01:26:56 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
2008-08-26 20:40:57 +02:00
|
|
|
#ifdef HAVE_SYS_FILIO_H
|
|
|
|
# include <sys/filio.h>
|
|
|
|
#endif
|
2003-06-27 06:04:00 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2007-05-28 12:16:06 +02:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
2008-06-02 12:14:37 +02:00
|
|
|
#include <time.h>
|
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
# include <netdb.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
# include <netinet/in.h>
|
|
|
|
#endif
|
2011-05-11 13:32:34 +02:00
|
|
|
#ifdef HAVE_NETINET_TCP_H
|
|
|
|
# include <netinet/tcp.h>
|
|
|
|
#endif
|
2008-06-02 12:14:37 +02:00
|
|
|
#ifdef HAVE_OPENSSL_SSL_H
|
|
|
|
# include <openssl/ssl.h>
|
2010-07-26 18:41:00 +02:00
|
|
|
# include <openssl/opensslv.h>
|
2008-06-02 12:14:37 +02:00
|
|
|
#undef FAR
|
|
|
|
#undef DSA
|
|
|
|
#endif
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2003-06-21 01:26:56 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2005-12-05 12:01:56 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2011-05-10 11:26:43 +02:00
|
|
|
#include <assert.h>
|
2003-06-21 01:26:56 +02:00
|
|
|
|
|
|
|
#include "wine/library.h"
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "wininet.h"
|
|
|
|
#include "winerror.h"
|
2007-08-09 10:36:29 +02:00
|
|
|
#include "wincrypt.h"
|
2003-06-21 01:26:56 +02:00
|
|
|
|
2008-03-03 22:46:52 +01:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "internet.h"
|
|
|
|
|
2005-12-03 18:10:14 +01:00
|
|
|
/* To avoid conflicts with the Unix socket headers. we only need it for
|
|
|
|
* the error codes anyway. */
|
|
|
|
#define USE_WS_PREFIX
|
|
|
|
#include "winsock2.h"
|
|
|
|
|
2003-06-21 01:26:56 +02:00
|
|
|
#define RESPONSE_TIMEOUT 30 /* FROM internet.c */
|
|
|
|
|
|
|
|
|
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(wininet);
|
|
|
|
|
|
|
|
/* FIXME!!!!!!
|
2005-02-25 15:07:56 +01:00
|
|
|
* This should use winsock - To use winsock the functions will have to change a bit
|
2003-06-21 01:26:56 +02:00
|
|
|
* as they are designed for unix sockets.
|
|
|
|
* SSL stuff should use crypt32.dll
|
|
|
|
*/
|
|
|
|
|
2007-07-02 17:32:09 +02:00
|
|
|
#ifdef SONAME_LIBSSL
|
2005-12-01 11:18:43 +01:00
|
|
|
|
|
|
|
#include <openssl/err.h>
|
2003-06-21 01:26:56 +02:00
|
|
|
|
|
|
|
static void *OpenSSL_ssl_handle;
|
|
|
|
static void *OpenSSL_crypto_handle;
|
|
|
|
|
2010-10-25 07:09:48 +02:00
|
|
|
#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER > 0x10000000)
|
2010-07-26 18:41:00 +02:00
|
|
|
static const SSL_METHOD *meth;
|
|
|
|
#else
|
2003-06-21 01:26:56 +02:00
|
|
|
static SSL_METHOD *meth;
|
2010-07-26 18:41:00 +02:00
|
|
|
#endif
|
2003-06-21 01:26:56 +02:00
|
|
|
static SSL_CTX *ctx;
|
2009-12-14 00:31:24 +01:00
|
|
|
static int hostname_idx;
|
2009-12-15 01:02:33 +01:00
|
|
|
static int error_idx;
|
2010-09-29 01:33:19 +02:00
|
|
|
static int conn_idx;
|
2003-06-21 01:26:56 +02:00
|
|
|
|
|
|
|
#define MAKE_FUNCPTR(f) static typeof(f) * p##f
|
|
|
|
|
2005-02-25 15:07:56 +01:00
|
|
|
/* OpenSSL functions that we use */
|
2003-06-21 01:26:56 +02:00
|
|
|
MAKE_FUNCPTR(SSL_library_init);
|
|
|
|
MAKE_FUNCPTR(SSL_load_error_strings);
|
|
|
|
MAKE_FUNCPTR(SSLv23_method);
|
2009-10-02 16:43:45 +02:00
|
|
|
MAKE_FUNCPTR(SSL_CTX_free);
|
2003-06-21 01:26:56 +02:00
|
|
|
MAKE_FUNCPTR(SSL_CTX_new);
|
|
|
|
MAKE_FUNCPTR(SSL_new);
|
2005-12-01 11:18:43 +01:00
|
|
|
MAKE_FUNCPTR(SSL_free);
|
|
|
|
MAKE_FUNCPTR(SSL_set_fd);
|
2003-06-21 01:26:56 +02:00
|
|
|
MAKE_FUNCPTR(SSL_connect);
|
2005-12-01 11:18:43 +01:00
|
|
|
MAKE_FUNCPTR(SSL_shutdown);
|
2003-06-21 01:26:56 +02:00
|
|
|
MAKE_FUNCPTR(SSL_write);
|
|
|
|
MAKE_FUNCPTR(SSL_read);
|
2009-01-10 17:02:31 +01:00
|
|
|
MAKE_FUNCPTR(SSL_pending);
|
2010-05-17 11:55:24 +02:00
|
|
|
MAKE_FUNCPTR(SSL_get_error);
|
2009-12-14 00:31:24 +01:00
|
|
|
MAKE_FUNCPTR(SSL_get_ex_new_index);
|
2009-12-14 00:42:29 +01:00
|
|
|
MAKE_FUNCPTR(SSL_get_ex_data);
|
2009-12-14 00:31:24 +01:00
|
|
|
MAKE_FUNCPTR(SSL_set_ex_data);
|
2009-12-14 00:42:29 +01:00
|
|
|
MAKE_FUNCPTR(SSL_get_ex_data_X509_STORE_CTX_idx);
|
2005-12-01 11:18:43 +01:00
|
|
|
MAKE_FUNCPTR(SSL_get_peer_certificate);
|
2003-06-21 01:26:56 +02:00
|
|
|
MAKE_FUNCPTR(SSL_CTX_get_timeout);
|
|
|
|
MAKE_FUNCPTR(SSL_CTX_set_timeout);
|
2005-12-01 11:18:43 +01:00
|
|
|
MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
|
2009-12-14 00:42:29 +01:00
|
|
|
MAKE_FUNCPTR(SSL_CTX_set_verify);
|
2010-09-30 22:18:07 +02:00
|
|
|
MAKE_FUNCPTR(SSL_get_current_cipher);
|
|
|
|
MAKE_FUNCPTR(SSL_CIPHER_get_bits);
|
2003-06-21 01:26:56 +02:00
|
|
|
|
|
|
|
/* OpenSSL's libcrypto functions that we use */
|
|
|
|
MAKE_FUNCPTR(BIO_new_fp);
|
2009-10-02 16:47:48 +02:00
|
|
|
MAKE_FUNCPTR(CRYPTO_num_locks);
|
|
|
|
MAKE_FUNCPTR(CRYPTO_set_id_callback);
|
|
|
|
MAKE_FUNCPTR(CRYPTO_set_locking_callback);
|
2009-12-14 00:23:27 +01:00
|
|
|
MAKE_FUNCPTR(ERR_free_strings);
|
2005-12-01 11:18:43 +01:00
|
|
|
MAKE_FUNCPTR(ERR_get_error);
|
|
|
|
MAKE_FUNCPTR(ERR_error_string);
|
2010-10-08 19:15:45 +02:00
|
|
|
MAKE_FUNCPTR(X509_STORE_CTX_get_ex_data);
|
2011-04-20 16:28:27 +02:00
|
|
|
MAKE_FUNCPTR(X509_STORE_CTX_get_chain);
|
2009-03-05 10:46:10 +01:00
|
|
|
MAKE_FUNCPTR(i2d_X509);
|
2009-12-14 01:05:35 +01:00
|
|
|
MAKE_FUNCPTR(sk_num);
|
|
|
|
MAKE_FUNCPTR(sk_value);
|
2003-06-21 01:26:56 +02:00
|
|
|
#undef MAKE_FUNCPTR
|
|
|
|
|
2009-10-02 16:47:48 +02:00
|
|
|
static CRITICAL_SECTION *ssl_locks;
|
2010-02-08 09:20:58 +01:00
|
|
|
static unsigned int num_ssl_locks;
|
2009-10-02 16:47:48 +02:00
|
|
|
|
|
|
|
static unsigned long ssl_thread_id(void)
|
|
|
|
{
|
|
|
|
return GetCurrentThreadId();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ssl_lock_callback(int mode, int type, const char *file, int line)
|
|
|
|
{
|
|
|
|
if (mode & CRYPTO_LOCK)
|
|
|
|
EnterCriticalSection(&ssl_locks[type]);
|
|
|
|
else
|
|
|
|
LeaveCriticalSection(&ssl_locks[type]);
|
|
|
|
}
|
|
|
|
|
2009-12-14 01:05:35 +01:00
|
|
|
static PCCERT_CONTEXT X509_to_cert_context(X509 *cert)
|
|
|
|
{
|
|
|
|
unsigned char* buffer,*p;
|
|
|
|
INT len;
|
|
|
|
BOOL malloced = FALSE;
|
|
|
|
PCCERT_CONTEXT ret;
|
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
len = pi2d_X509(cert,&p);
|
|
|
|
/*
|
|
|
|
* SSL 0.9.7 and above malloc the buffer if it is null.
|
|
|
|
* however earlier version do not and so we would need to alloc the buffer.
|
|
|
|
*
|
|
|
|
* see the i2d_X509 man page for more details.
|
|
|
|
*/
|
|
|
|
if (!p)
|
|
|
|
{
|
2011-04-21 13:39:03 +02:00
|
|
|
buffer = heap_alloc(len);
|
2009-12-14 01:05:35 +01:00
|
|
|
p = buffer;
|
|
|
|
len = pi2d_X509(cert,&p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = p;
|
|
|
|
malloced = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
|
|
|
|
|
|
|
|
if (malloced)
|
|
|
|
free(buffer);
|
|
|
|
else
|
2011-06-01 11:50:53 +02:00
|
|
|
heap_free(buffer);
|
2009-12-14 01:05:35 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-12-15 00:58:44 +01:00
|
|
|
static DWORD netconn_verify_cert(PCCERT_CONTEXT cert, HCERTSTORE store,
|
2010-09-29 16:53:53 +02:00
|
|
|
WCHAR *server, DWORD security_flags)
|
2009-12-14 01:05:35 +01:00
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
CERT_CHAIN_PARA chainPara = { sizeof(chainPara), { 0 } };
|
|
|
|
PCCERT_CHAIN_CONTEXT chain;
|
|
|
|
char oid_server_auth[] = szOID_PKIX_KP_SERVER_AUTH;
|
|
|
|
char *server_auth[] = { oid_server_auth };
|
2010-11-16 02:07:22 +01:00
|
|
|
DWORD err = ERROR_SUCCESS, chainFlags = 0;
|
2009-12-14 01:05:35 +01:00
|
|
|
|
|
|
|
TRACE("verifying %s\n", debugstr_w(server));
|
|
|
|
chainPara.RequestedUsage.Usage.cUsageIdentifier = 1;
|
|
|
|
chainPara.RequestedUsage.Usage.rgpszUsageIdentifier = server_auth;
|
2010-11-16 02:07:22 +01:00
|
|
|
if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION))
|
|
|
|
chainFlags |= CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
|
|
|
|
if ((ret = CertGetCertificateChain(NULL, cert, NULL, store, &chainPara,
|
|
|
|
chainFlags, NULL, &chain)))
|
2009-12-14 01:05:35 +01:00
|
|
|
{
|
|
|
|
if (chain->TrustStatus.dwErrorStatus)
|
|
|
|
{
|
2010-09-29 16:53:53 +02:00
|
|
|
static const DWORD supportedErrors =
|
|
|
|
CERT_TRUST_IS_NOT_TIME_VALID |
|
|
|
|
CERT_TRUST_IS_UNTRUSTED_ROOT |
|
|
|
|
CERT_TRUST_IS_OFFLINE_REVOCATION |
|
|
|
|
CERT_TRUST_REVOCATION_STATUS_UNKNOWN |
|
|
|
|
CERT_TRUST_IS_REVOKED |
|
|
|
|
CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
|
|
|
|
|
2010-12-19 21:27:23 +01:00
|
|
|
if (chain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_NOT_TIME_VALID &&
|
|
|
|
!(security_flags & SECURITY_FLAG_IGNORE_CERT_DATE_INVALID))
|
2009-12-14 01:05:35 +01:00
|
|
|
err = ERROR_INTERNET_SEC_CERT_DATE_INVALID;
|
|
|
|
else if (chain->TrustStatus.dwErrorStatus &
|
2010-09-29 16:53:53 +02:00
|
|
|
CERT_TRUST_IS_UNTRUSTED_ROOT &&
|
|
|
|
!(security_flags & SECURITY_FLAG_IGNORE_UNKNOWN_CA))
|
2009-12-14 01:05:35 +01:00
|
|
|
err = ERROR_INTERNET_INVALID_CA;
|
2010-09-29 16:53:53 +02:00
|
|
|
else if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION) &&
|
|
|
|
((chain->TrustStatus.dwErrorStatus &
|
2009-12-14 01:05:35 +01:00
|
|
|
CERT_TRUST_IS_OFFLINE_REVOCATION) ||
|
2010-09-29 16:53:53 +02:00
|
|
|
(chain->TrustStatus.dwErrorStatus &
|
|
|
|
CERT_TRUST_REVOCATION_STATUS_UNKNOWN)))
|
2009-12-14 01:05:35 +01:00
|
|
|
err = ERROR_INTERNET_SEC_CERT_NO_REV;
|
2010-09-29 16:53:53 +02:00
|
|
|
else if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION) &&
|
|
|
|
(chain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_REVOKED))
|
2009-12-14 01:05:35 +01:00
|
|
|
err = ERROR_INTERNET_SEC_CERT_REVOKED;
|
2010-09-29 16:53:53 +02:00
|
|
|
else if (!(security_flags & SECURITY_FLAG_IGNORE_WRONG_USAGE) &&
|
|
|
|
(chain->TrustStatus.dwErrorStatus &
|
|
|
|
CERT_TRUST_IS_NOT_VALID_FOR_USAGE))
|
2009-12-14 01:05:35 +01:00
|
|
|
err = ERROR_INTERNET_SEC_INVALID_CERT;
|
2010-09-29 16:53:53 +02:00
|
|
|
else if (chain->TrustStatus.dwErrorStatus & ~supportedErrors)
|
2009-12-14 01:05:35 +01:00
|
|
|
err = ERROR_INTERNET_SEC_INVALID_CERT;
|
|
|
|
}
|
2010-09-29 16:53:53 +02:00
|
|
|
if (!err)
|
2009-12-14 01:05:35 +01:00
|
|
|
{
|
|
|
|
CERT_CHAIN_POLICY_PARA policyPara;
|
|
|
|
SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara;
|
|
|
|
CERT_CHAIN_POLICY_STATUS policyStatus;
|
2010-09-29 16:53:53 +02:00
|
|
|
CERT_CHAIN_CONTEXT chainCopy;
|
2009-12-14 01:05:35 +01:00
|
|
|
|
2010-09-29 16:53:53 +02:00
|
|
|
/* Clear chain->TrustStatus.dwErrorStatus so
|
|
|
|
* CertVerifyCertificateChainPolicy will verify additional checks
|
|
|
|
* rather than stopping with an existing, ignored error.
|
|
|
|
*/
|
|
|
|
memcpy(&chainCopy, chain, sizeof(chainCopy));
|
|
|
|
chainCopy.TrustStatus.dwErrorStatus = 0;
|
2009-12-14 01:05:35 +01:00
|
|
|
sslExtraPolicyPara.u.cbSize = sizeof(sslExtraPolicyPara);
|
|
|
|
sslExtraPolicyPara.dwAuthType = AUTHTYPE_SERVER;
|
|
|
|
sslExtraPolicyPara.pwszServerName = server;
|
2010-09-29 23:19:23 +02:00
|
|
|
sslExtraPolicyPara.fdwChecks = security_flags;
|
2009-12-14 01:05:35 +01:00
|
|
|
policyPara.cbSize = sizeof(policyPara);
|
|
|
|
policyPara.dwFlags = 0;
|
|
|
|
policyPara.pvExtraPolicyPara = &sslExtraPolicyPara;
|
|
|
|
ret = CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL,
|
2010-09-29 16:53:53 +02:00
|
|
|
&chainCopy, &policyPara, &policyStatus);
|
2009-12-14 01:05:35 +01:00
|
|
|
/* Any error in the policy status indicates that the
|
|
|
|
* policy couldn't be verified.
|
|
|
|
*/
|
|
|
|
if (ret && policyStatus.dwError)
|
|
|
|
{
|
|
|
|
if (policyStatus.dwError == CERT_E_CN_NO_MATCH)
|
2010-09-29 23:19:23 +02:00
|
|
|
err = ERROR_INTERNET_SEC_CERT_CN_INVALID;
|
2009-12-14 01:05:35 +01:00
|
|
|
else
|
|
|
|
err = ERROR_INTERNET_SEC_INVALID_CERT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CertFreeCertificateChain(chain);
|
|
|
|
}
|
2009-12-15 00:58:44 +01:00
|
|
|
TRACE("returning %08x\n", err);
|
|
|
|
return err;
|
2009-12-14 01:05:35 +01:00
|
|
|
}
|
|
|
|
|
2009-12-14 00:42:29 +01:00
|
|
|
static int netconn_secure_verify(int preverify_ok, X509_STORE_CTX *ctx)
|
|
|
|
{
|
|
|
|
SSL *ssl;
|
|
|
|
WCHAR *server;
|
2009-12-14 01:05:35 +01:00
|
|
|
BOOL ret = FALSE;
|
2010-09-29 01:14:21 +02:00
|
|
|
HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
|
|
|
|
CERT_STORE_CREATE_NEW_FLAG, NULL);
|
2011-04-28 18:29:04 +02:00
|
|
|
netconn_t *conn;
|
2009-12-14 00:42:29 +01:00
|
|
|
|
|
|
|
ssl = pX509_STORE_CTX_get_ex_data(ctx,
|
|
|
|
pSSL_get_ex_data_X509_STORE_CTX_idx());
|
|
|
|
server = pSSL_get_ex_data(ssl, hostname_idx);
|
2010-09-29 16:53:53 +02:00
|
|
|
conn = pSSL_get_ex_data(ssl, conn_idx);
|
2010-09-29 01:14:21 +02:00
|
|
|
if (store)
|
2009-12-14 01:05:35 +01:00
|
|
|
{
|
2010-09-29 01:14:21 +02:00
|
|
|
X509 *cert;
|
|
|
|
int i;
|
|
|
|
PCCERT_CONTEXT endCert = NULL;
|
2011-04-20 16:28:27 +02:00
|
|
|
struct stack_st *chain = (struct stack_st *)pX509_STORE_CTX_get_chain( ctx );
|
2009-12-14 01:05:35 +01:00
|
|
|
|
2010-09-29 01:14:21 +02:00
|
|
|
ret = TRUE;
|
2011-04-20 16:28:27 +02:00
|
|
|
for (i = 0; ret && i < psk_num(chain); i++)
|
2009-12-14 01:05:35 +01:00
|
|
|
{
|
2010-09-29 01:14:21 +02:00
|
|
|
PCCERT_CONTEXT context;
|
2009-12-14 01:05:35 +01:00
|
|
|
|
2011-04-20 16:28:27 +02:00
|
|
|
cert = (X509 *)psk_value(chain, i);
|
2010-09-29 01:14:21 +02:00
|
|
|
if ((context = X509_to_cert_context(cert)))
|
2009-12-14 01:05:35 +01:00
|
|
|
{
|
2010-09-29 01:14:21 +02:00
|
|
|
if (i == 0)
|
|
|
|
ret = CertAddCertificateContextToStore(store, context,
|
|
|
|
CERT_STORE_ADD_ALWAYS, &endCert);
|
|
|
|
else
|
|
|
|
ret = CertAddCertificateContextToStore(store, context,
|
|
|
|
CERT_STORE_ADD_ALWAYS, NULL);
|
|
|
|
CertFreeCertificateContext(context);
|
2009-12-14 01:05:35 +01:00
|
|
|
}
|
2010-09-29 01:14:21 +02:00
|
|
|
}
|
|
|
|
if (!endCert) ret = FALSE;
|
|
|
|
if (ret)
|
|
|
|
{
|
2010-09-29 16:53:53 +02:00
|
|
|
DWORD_PTR err = netconn_verify_cert(endCert, store, server,
|
|
|
|
conn->security_flags);
|
2009-12-15 00:58:44 +01:00
|
|
|
|
2010-09-29 01:14:21 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
pSSL_set_ex_data(ssl, error_idx, (void *)err);
|
|
|
|
ret = FALSE;
|
2009-12-15 00:58:44 +01:00
|
|
|
}
|
2009-12-14 01:05:35 +01:00
|
|
|
}
|
2010-09-29 01:14:21 +02:00
|
|
|
CertFreeCertificateContext(endCert);
|
|
|
|
CertCloseStore(store, 0);
|
|
|
|
}
|
2009-12-14 01:05:35 +01:00
|
|
|
return ret;
|
2009-12-14 00:42:29 +01:00
|
|
|
}
|
|
|
|
|
2003-06-21 01:26:56 +02:00
|
|
|
#endif
|
|
|
|
|
2011-04-28 18:29:37 +02:00
|
|
|
static CRITICAL_SECTION init_ssl_cs;
|
|
|
|
static CRITICAL_SECTION_DEBUG init_ssl_cs_debug =
|
|
|
|
{
|
|
|
|
0, 0, &init_ssl_cs,
|
|
|
|
{ &init_ssl_cs_debug.ProcessLocksList,
|
|
|
|
&init_ssl_cs_debug.ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": init_ssl_cs") }
|
|
|
|
};
|
|
|
|
static CRITICAL_SECTION init_ssl_cs = { &init_ssl_cs_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
static DWORD init_openssl(void)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2008-05-13 10:42:54 +02:00
|
|
|
#if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
|
2011-04-28 18:29:37 +02:00
|
|
|
int i;
|
2009-10-02 16:47:48 +02:00
|
|
|
|
2011-04-28 18:29:37 +02:00
|
|
|
if(OpenSSL_ssl_handle)
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
|
|
|
OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
|
|
|
|
if(!OpenSSL_ssl_handle) {
|
|
|
|
ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
|
|
|
|
return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
|
|
|
|
if(!OpenSSL_crypto_handle) {
|
|
|
|
ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
|
|
|
|
return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mmm nice ugly macroness */
|
2003-06-21 01:26:56 +02:00
|
|
|
#define DYNSSL(x) \
|
|
|
|
p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
|
2011-04-28 18:29:37 +02:00
|
|
|
if (!p##x) { \
|
2003-06-21 01:26:56 +02:00
|
|
|
ERR("failed to load symbol %s\n", #x); \
|
2009-11-30 20:00:11 +01:00
|
|
|
return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
|
2011-04-28 18:29:37 +02:00
|
|
|
DYNSSL(SSL_library_init);
|
|
|
|
DYNSSL(SSL_load_error_strings);
|
|
|
|
DYNSSL(SSLv23_method);
|
|
|
|
DYNSSL(SSL_CTX_free);
|
|
|
|
DYNSSL(SSL_CTX_new);
|
|
|
|
DYNSSL(SSL_new);
|
|
|
|
DYNSSL(SSL_free);
|
|
|
|
DYNSSL(SSL_set_fd);
|
|
|
|
DYNSSL(SSL_connect);
|
|
|
|
DYNSSL(SSL_shutdown);
|
|
|
|
DYNSSL(SSL_write);
|
|
|
|
DYNSSL(SSL_read);
|
|
|
|
DYNSSL(SSL_pending);
|
|
|
|
DYNSSL(SSL_get_error);
|
|
|
|
DYNSSL(SSL_get_ex_new_index);
|
|
|
|
DYNSSL(SSL_get_ex_data);
|
|
|
|
DYNSSL(SSL_set_ex_data);
|
|
|
|
DYNSSL(SSL_get_ex_data_X509_STORE_CTX_idx);
|
|
|
|
DYNSSL(SSL_get_peer_certificate);
|
|
|
|
DYNSSL(SSL_CTX_get_timeout);
|
|
|
|
DYNSSL(SSL_CTX_set_timeout);
|
|
|
|
DYNSSL(SSL_CTX_set_default_verify_paths);
|
|
|
|
DYNSSL(SSL_CTX_set_verify);
|
|
|
|
DYNSSL(SSL_get_current_cipher);
|
|
|
|
DYNSSL(SSL_CIPHER_get_bits);
|
2003-06-21 01:26:56 +02:00
|
|
|
#undef DYNSSL
|
|
|
|
|
|
|
|
#define DYNCRYPTO(x) \
|
|
|
|
p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
|
2011-04-28 18:29:37 +02:00
|
|
|
if (!p##x) { \
|
2003-06-21 01:26:56 +02:00
|
|
|
ERR("failed to load symbol %s\n", #x); \
|
2009-11-30 20:00:11 +01:00
|
|
|
return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
2011-04-28 18:29:37 +02:00
|
|
|
|
|
|
|
DYNCRYPTO(BIO_new_fp);
|
|
|
|
DYNCRYPTO(CRYPTO_num_locks);
|
|
|
|
DYNCRYPTO(CRYPTO_set_id_callback);
|
|
|
|
DYNCRYPTO(CRYPTO_set_locking_callback);
|
|
|
|
DYNCRYPTO(ERR_free_strings);
|
|
|
|
DYNCRYPTO(ERR_get_error);
|
|
|
|
DYNCRYPTO(ERR_error_string);
|
|
|
|
DYNCRYPTO(X509_STORE_CTX_get_ex_data);
|
|
|
|
DYNCRYPTO(X509_STORE_CTX_get_chain);
|
|
|
|
DYNCRYPTO(i2d_X509);
|
|
|
|
DYNCRYPTO(sk_num);
|
|
|
|
DYNCRYPTO(sk_value);
|
2003-06-21 01:26:56 +02:00
|
|
|
#undef DYNCRYPTO
|
|
|
|
|
2011-04-28 18:29:37 +02:00
|
|
|
pSSL_library_init();
|
|
|
|
pSSL_load_error_strings();
|
|
|
|
pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
|
2003-06-21 01:26:56 +02:00
|
|
|
|
2011-04-28 18:29:37 +02:00
|
|
|
meth = pSSLv23_method();
|
|
|
|
ctx = pSSL_CTX_new(meth);
|
|
|
|
if(!pSSL_CTX_set_default_verify_paths(ctx)) {
|
|
|
|
ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
|
|
|
|
pERR_error_string(pERR_get_error(), 0));
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
}
|
2009-10-02 16:47:48 +02:00
|
|
|
|
2011-04-28 18:29:37 +02:00
|
|
|
hostname_idx = pSSL_get_ex_new_index(0, (void *)"hostname index", NULL, NULL, NULL);
|
|
|
|
if(hostname_idx == -1) {
|
|
|
|
ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_idx = pSSL_get_ex_new_index(0, (void *)"error index", NULL, NULL, NULL);
|
|
|
|
if(error_idx == -1) {
|
|
|
|
ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn_idx = pSSL_get_ex_new_index(0, (void *)"netconn index", NULL, NULL, NULL);
|
|
|
|
if(conn_idx == -1) {
|
|
|
|
ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
pSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, netconn_secure_verify);
|
|
|
|
|
|
|
|
pCRYPTO_set_id_callback(ssl_thread_id);
|
|
|
|
num_ssl_locks = pCRYPTO_num_locks();
|
2011-06-01 11:50:53 +02:00
|
|
|
ssl_locks = heap_alloc(num_ssl_locks * sizeof(CRITICAL_SECTION));
|
2011-04-28 18:29:37 +02:00
|
|
|
if(!ssl_locks)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
for(i = 0; i < num_ssl_locks; i++)
|
|
|
|
InitializeCriticalSection(&ssl_locks[i]);
|
|
|
|
pCRYPTO_set_locking_callback(ssl_lock_callback);
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
2003-06-21 01:26:56 +02:00
|
|
|
#else
|
2011-04-28 18:29:37 +02:00
|
|
|
FIXME("can't use SSL, not compiled in.\n");
|
|
|
|
return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
2003-06-21 01:26:56 +02:00
|
|
|
#endif
|
2011-04-28 18:29:37 +02:00
|
|
|
}
|
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
DWORD create_netconn(BOOL useSSL, server_t *server, DWORD security_flags, netconn_t **ret)
|
2011-04-28 18:29:37 +02:00
|
|
|
{
|
2011-05-10 11:26:43 +02:00
|
|
|
netconn_t *netconn;
|
2011-05-11 13:32:34 +02:00
|
|
|
int result, flag;
|
2011-04-28 18:29:37 +02:00
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
if(useSSL) {
|
|
|
|
DWORD res;
|
2011-04-28 18:29:37 +02:00
|
|
|
|
|
|
|
TRACE("using SSL connection\n");
|
|
|
|
|
|
|
|
EnterCriticalSection(&init_ssl_cs);
|
|
|
|
res = init_openssl();
|
|
|
|
LeaveCriticalSection(&init_ssl_cs);
|
2011-05-10 11:26:43 +02:00
|
|
|
if(res != ERROR_SUCCESS)
|
|
|
|
return res;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
2011-04-28 18:29:37 +02:00
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
netconn = heap_alloc_zero(sizeof(*netconn));
|
|
|
|
if(!netconn)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
netconn->useSSL = useSSL;
|
|
|
|
netconn->socketFD = -1;
|
|
|
|
netconn->security_flags = security_flags;
|
|
|
|
list_init(&netconn->pool_entry);
|
|
|
|
|
|
|
|
assert(server->addr_len);
|
|
|
|
result = netconn->socketFD = socket(server->addr.ss_family, SOCK_STREAM, 0);
|
|
|
|
if(result != -1) {
|
|
|
|
result = connect(netconn->socketFD, (struct sockaddr*)&server->addr, server->addr_len);
|
|
|
|
if(result == -1)
|
|
|
|
closesocket(netconn->socketFD);
|
|
|
|
}
|
|
|
|
if(result == -1) {
|
|
|
|
heap_free(netconn);
|
|
|
|
return sock_get_error(errno);
|
|
|
|
}
|
|
|
|
|
2011-05-11 13:32:34 +02:00
|
|
|
#ifdef TCP_NODELAY
|
|
|
|
flag = 1;
|
|
|
|
result = setsockopt(netconn->socketFD, IPPROTO_TCP, TCP_NODELAY, (void*)&flag, sizeof(flag));
|
|
|
|
if(result < 0)
|
|
|
|
WARN("setsockopt(TCP_NODELAY) failed\n");
|
|
|
|
#endif
|
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
server_addref(server);
|
|
|
|
netconn->server = server;
|
|
|
|
|
|
|
|
*ret = netconn;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_netconn(netconn_t *netconn)
|
|
|
|
{
|
|
|
|
server_release(netconn->server);
|
|
|
|
|
|
|
|
#ifdef SONAME_LIBSSL
|
|
|
|
if (netconn->ssl_s) {
|
|
|
|
pSSL_shutdown(netconn->ssl_s);
|
|
|
|
pSSL_free(netconn->ssl_s);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
closesocket(netconn->socketFD);
|
|
|
|
heap_free(netconn);
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
|
2009-10-02 16:43:45 +02:00
|
|
|
void NETCON_unload(void)
|
|
|
|
{
|
|
|
|
#if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
|
|
|
|
if (OpenSSL_crypto_handle)
|
2009-10-02 16:47:48 +02:00
|
|
|
{
|
2009-12-14 00:23:27 +01:00
|
|
|
pERR_free_strings();
|
2009-10-02 16:43:45 +02:00
|
|
|
wine_dlclose(OpenSSL_crypto_handle, NULL, 0);
|
2009-10-02 16:47:48 +02:00
|
|
|
}
|
2009-10-02 16:43:45 +02:00
|
|
|
if (OpenSSL_ssl_handle)
|
|
|
|
{
|
|
|
|
if (ctx)
|
|
|
|
pSSL_CTX_free(ctx);
|
|
|
|
wine_dlclose(OpenSSL_ssl_handle, NULL, 0);
|
|
|
|
}
|
2009-10-27 10:04:20 +01:00
|
|
|
if (ssl_locks)
|
|
|
|
{
|
|
|
|
int i;
|
2010-02-08 09:20:58 +01:00
|
|
|
for (i = 0; i < num_ssl_locks; i++) DeleteCriticalSection(&ssl_locks[i]);
|
2011-06-01 11:50:53 +02:00
|
|
|
heap_free(ssl_locks);
|
2009-10-27 10:04:20 +01:00
|
|
|
}
|
2009-10-02 16:43:45 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-12-03 18:10:14 +01:00
|
|
|
/* translate a unix error code into a winsock one */
|
2009-11-30 00:13:39 +01:00
|
|
|
int sock_get_error( int err )
|
2005-12-03 18:10:14 +01:00
|
|
|
{
|
2008-03-03 22:46:52 +01:00
|
|
|
#if !defined(__MINGW32__) && !defined (_MSC_VER)
|
2005-12-03 18:10:14 +01:00
|
|
|
switch (err)
|
|
|
|
{
|
|
|
|
case EINTR: return WSAEINTR;
|
|
|
|
case EBADF: return WSAEBADF;
|
|
|
|
case EPERM:
|
|
|
|
case EACCES: return WSAEACCES;
|
|
|
|
case EFAULT: return WSAEFAULT;
|
|
|
|
case EINVAL: return WSAEINVAL;
|
|
|
|
case EMFILE: return WSAEMFILE;
|
|
|
|
case EWOULDBLOCK: return WSAEWOULDBLOCK;
|
|
|
|
case EINPROGRESS: return WSAEINPROGRESS;
|
|
|
|
case EALREADY: return WSAEALREADY;
|
|
|
|
case ENOTSOCK: return WSAENOTSOCK;
|
|
|
|
case EDESTADDRREQ: return WSAEDESTADDRREQ;
|
|
|
|
case EMSGSIZE: return WSAEMSGSIZE;
|
|
|
|
case EPROTOTYPE: return WSAEPROTOTYPE;
|
|
|
|
case ENOPROTOOPT: return WSAENOPROTOOPT;
|
|
|
|
case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
|
|
|
|
case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
|
|
|
|
case EOPNOTSUPP: return WSAEOPNOTSUPP;
|
|
|
|
case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
|
|
|
|
case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
|
|
|
|
case EADDRINUSE: return WSAEADDRINUSE;
|
|
|
|
case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
|
|
|
|
case ENETDOWN: return WSAENETDOWN;
|
|
|
|
case ENETUNREACH: return WSAENETUNREACH;
|
|
|
|
case ENETRESET: return WSAENETRESET;
|
|
|
|
case ECONNABORTED: return WSAECONNABORTED;
|
|
|
|
case EPIPE:
|
|
|
|
case ECONNRESET: return WSAECONNRESET;
|
|
|
|
case ENOBUFS: return WSAENOBUFS;
|
|
|
|
case EISCONN: return WSAEISCONN;
|
|
|
|
case ENOTCONN: return WSAENOTCONN;
|
|
|
|
case ESHUTDOWN: return WSAESHUTDOWN;
|
|
|
|
case ETOOMANYREFS: return WSAETOOMANYREFS;
|
|
|
|
case ETIMEDOUT: return WSAETIMEDOUT;
|
|
|
|
case ECONNREFUSED: return WSAECONNREFUSED;
|
|
|
|
case ELOOP: return WSAELOOP;
|
|
|
|
case ENAMETOOLONG: return WSAENAMETOOLONG;
|
|
|
|
case EHOSTDOWN: return WSAEHOSTDOWN;
|
|
|
|
case EHOSTUNREACH: return WSAEHOSTUNREACH;
|
|
|
|
case ENOTEMPTY: return WSAENOTEMPTY;
|
|
|
|
#ifdef EPROCLIM
|
|
|
|
case EPROCLIM: return WSAEPROCLIM;
|
|
|
|
#endif
|
|
|
|
#ifdef EUSERS
|
|
|
|
case EUSERS: return WSAEUSERS;
|
|
|
|
#endif
|
|
|
|
#ifdef EDQUOT
|
|
|
|
case EDQUOT: return WSAEDQUOT;
|
|
|
|
#endif
|
|
|
|
#ifdef ESTALE
|
|
|
|
case ESTALE: return WSAESTALE;
|
|
|
|
#endif
|
|
|
|
#ifdef EREMOTE
|
|
|
|
case EREMOTE: return WSAEREMOTE;
|
|
|
|
#endif
|
|
|
|
default: errno=err; perror("sock_set_error"); return WSAEFAULT;
|
|
|
|
}
|
2008-03-03 22:46:52 +01:00
|
|
|
#endif
|
|
|
|
return err;
|
2005-12-03 18:10:14 +01:00
|
|
|
}
|
|
|
|
|
2005-11-28 11:54:31 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_secure_connect
|
|
|
|
* Initiates a secure connection over an existing plaintext connection.
|
|
|
|
*/
|
2011-04-28 18:29:04 +02:00
|
|
|
DWORD NETCON_secure_connect(netconn_t *connection, LPWSTR hostname)
|
2005-11-28 11:54:31 +01:00
|
|
|
{
|
2011-01-19 20:16:02 +01:00
|
|
|
void *ssl_s;
|
2009-11-30 00:13:21 +01:00
|
|
|
DWORD res = ERROR_NOT_SUPPORTED;
|
2005-11-28 11:54:31 +01:00
|
|
|
|
2010-09-29 17:16:07 +02:00
|
|
|
#ifdef SONAME_LIBSSL
|
2005-12-01 11:18:43 +01:00
|
|
|
/* can't connect if we are already connected */
|
2011-01-19 20:16:02 +01:00
|
|
|
if (connection->ssl_s)
|
2005-12-01 11:18:43 +01:00
|
|
|
{
|
|
|
|
ERR("already connected\n");
|
2009-11-30 00:13:21 +01:00
|
|
|
return ERROR_INTERNET_CANNOT_CONNECT;
|
2005-12-01 11:18:43 +01:00
|
|
|
}
|
2005-11-28 11:54:31 +01:00
|
|
|
|
2011-01-19 20:16:02 +01:00
|
|
|
ssl_s = pSSL_new(ctx);
|
|
|
|
if (!ssl_s)
|
2005-12-01 11:18:43 +01:00
|
|
|
{
|
|
|
|
ERR("SSL_new failed: %s\n",
|
|
|
|
pERR_error_string(pERR_get_error(), 0));
|
2011-01-19 20:16:02 +01:00
|
|
|
return ERROR_OUTOFMEMORY;
|
2005-12-01 11:18:43 +01:00
|
|
|
}
|
|
|
|
|
2011-01-19 20:16:02 +01:00
|
|
|
if (!pSSL_set_fd(ssl_s, connection->socketFD))
|
2005-12-01 11:18:43 +01:00
|
|
|
{
|
|
|
|
ERR("SSL_set_fd failed: %s\n",
|
|
|
|
pERR_error_string(pERR_get_error(), 0));
|
2009-11-30 00:13:21 +01:00
|
|
|
res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
2005-12-01 11:18:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2005-11-28 11:54:31 +01:00
|
|
|
|
2011-01-19 20:16:02 +01:00
|
|
|
if (!pSSL_set_ex_data(ssl_s, hostname_idx, hostname))
|
2010-09-29 01:33:19 +02:00
|
|
|
{
|
|
|
|
ERR("SSL_set_ex_data failed: %s\n",
|
|
|
|
pERR_error_string(pERR_get_error(), 0));
|
|
|
|
res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-01-19 20:16:02 +01:00
|
|
|
if (!pSSL_set_ex_data(ssl_s, conn_idx, connection))
|
2010-09-29 01:33:19 +02:00
|
|
|
{
|
|
|
|
ERR("SSL_set_ex_data failed: %s\n",
|
|
|
|
pERR_error_string(pERR_get_error(), 0));
|
|
|
|
res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
2011-01-19 20:16:02 +01:00
|
|
|
if (pSSL_connect(ssl_s) <= 0)
|
2010-09-29 03:15:58 +02:00
|
|
|
{
|
2011-01-19 20:16:02 +01:00
|
|
|
res = (DWORD_PTR)pSSL_get_ex_data(ssl_s, error_idx);
|
2010-09-29 03:15:58 +02:00
|
|
|
if (!res)
|
|
|
|
res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
ERR("SSL_connect failed: %d\n", res);
|
|
|
|
goto fail;
|
|
|
|
}
|
2005-12-01 11:18:43 +01:00
|
|
|
|
2011-01-19 20:16:02 +01:00
|
|
|
connection->ssl_s = ssl_s;
|
2009-11-30 00:13:21 +01:00
|
|
|
return ERROR_SUCCESS;
|
2005-12-01 11:18:43 +01:00
|
|
|
|
|
|
|
fail:
|
2011-01-19 20:16:02 +01:00
|
|
|
if (ssl_s)
|
2005-12-01 11:18:43 +01:00
|
|
|
{
|
2011-01-19 20:16:02 +01:00
|
|
|
pSSL_shutdown(ssl_s);
|
|
|
|
pSSL_free(ssl_s);
|
2005-12-01 11:18:43 +01:00
|
|
|
}
|
2005-11-28 11:54:31 +01:00
|
|
|
#endif
|
2009-11-30 00:13:21 +01:00
|
|
|
return res;
|
2005-11-28 11:54:31 +01:00
|
|
|
}
|
|
|
|
|
2003-06-21 01:26:56 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_send
|
|
|
|
* Basically calls 'send()' unless we should use SSL
|
|
|
|
* number of chars send is put in *sent
|
|
|
|
*/
|
2011-04-28 18:29:04 +02:00
|
|
|
DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags,
|
2003-06-21 01:26:56 +02:00
|
|
|
int *sent /* out */)
|
|
|
|
{
|
|
|
|
if (!connection->useSSL)
|
|
|
|
{
|
|
|
|
*sent = send(connection->socketFD, msg, len, flags);
|
|
|
|
if (*sent == -1)
|
2009-11-30 00:13:21 +01:00
|
|
|
return sock_get_error(errno);
|
|
|
|
return ERROR_SUCCESS;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-02 17:32:09 +02:00
|
|
|
#ifdef SONAME_LIBSSL
|
2011-01-19 20:16:02 +01:00
|
|
|
if(!connection->ssl_s) {
|
|
|
|
FIXME("not connected\n");
|
|
|
|
return ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2003-06-21 01:26:56 +02:00
|
|
|
if (flags)
|
|
|
|
FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
|
|
|
|
*sent = pSSL_write(connection->ssl_s, msg, len);
|
|
|
|
if (*sent < 1 && len)
|
2009-11-30 00:13:21 +01:00
|
|
|
return ERROR_INTERNET_CONNECTION_ABORTED;
|
|
|
|
return ERROR_SUCCESS;
|
2003-06-21 01:26:56 +02:00
|
|
|
#else
|
2009-11-30 00:13:21 +01:00
|
|
|
return ERROR_NOT_SUPPORTED;
|
2003-06-21 01:26:56 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_recv
|
|
|
|
* Basically calls 'recv()' unless we should use SSL
|
2003-10-08 21:09:44 +02:00
|
|
|
* number of chars received is put in *recvd
|
2003-06-21 01:26:56 +02:00
|
|
|
*/
|
2011-04-28 18:29:04 +02:00
|
|
|
DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, int flags,
|
2003-06-21 01:26:56 +02:00
|
|
|
int *recvd /* out */)
|
|
|
|
{
|
2007-02-21 18:11:57 +01:00
|
|
|
*recvd = 0;
|
2007-01-10 16:30:22 +01:00
|
|
|
if (!len)
|
2009-11-30 19:59:40 +01:00
|
|
|
return ERROR_SUCCESS;
|
2003-06-21 01:26:56 +02:00
|
|
|
if (!connection->useSSL)
|
|
|
|
{
|
|
|
|
*recvd = recv(connection->socketFD, buf, len, flags);
|
2009-11-30 19:59:40 +01:00
|
|
|
return *recvd == -1 ? sock_get_error(errno) : ERROR_SUCCESS;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-02 17:32:09 +02:00
|
|
|
#ifdef SONAME_LIBSSL
|
2011-01-19 20:16:02 +01:00
|
|
|
if(!connection->ssl_s) {
|
|
|
|
FIXME("not connected\n");
|
|
|
|
return ERROR_NOT_SUPPORTED;
|
|
|
|
}
|
2009-05-14 16:55:29 +02:00
|
|
|
*recvd = pSSL_read(connection->ssl_s, buf, len);
|
2010-05-17 11:55:24 +02:00
|
|
|
|
|
|
|
/* Check if EOF was received */
|
|
|
|
if(!*recvd && (pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_ZERO_RETURN
|
2011-05-10 11:26:43 +02:00
|
|
|
|| pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_SYSCALL))
|
2010-05-17 11:55:24 +02:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
|
2009-11-30 19:59:40 +01:00
|
|
|
return *recvd > 0 ? ERROR_SUCCESS : ERROR_INTERNET_CONNECTION_ABORTED;
|
2003-06-21 01:26:56 +02:00
|
|
|
#else
|
2009-11-30 19:59:40 +01:00
|
|
|
return ERROR_NOT_SUPPORTED;
|
2003-06-21 01:26:56 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-28 12:16:06 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_query_data_available
|
|
|
|
* Returns the number of bytes of peeked data plus the number of bytes of
|
|
|
|
* queued, but unread data.
|
|
|
|
*/
|
2011-04-28 18:29:04 +02:00
|
|
|
BOOL NETCON_query_data_available(netconn_t *connection, DWORD *available)
|
2007-05-28 12:16:06 +02:00
|
|
|
{
|
2007-08-12 22:40:46 +02:00
|
|
|
*available = 0;
|
2007-05-28 12:16:06 +02:00
|
|
|
|
|
|
|
if (!connection->useSSL)
|
|
|
|
{
|
2009-05-14 16:55:29 +02:00
|
|
|
#ifdef FIONREAD
|
2007-05-28 12:16:06 +02:00
|
|
|
int unread;
|
2008-03-03 22:46:52 +01:00
|
|
|
int retval = ioctlsocket(connection->socketFD, FIONREAD, &unread);
|
2007-05-28 12:16:06 +02:00
|
|
|
if (!retval)
|
|
|
|
{
|
|
|
|
TRACE("%d bytes of queued, but unread data\n", unread);
|
|
|
|
*available += unread;
|
|
|
|
}
|
|
|
|
#endif
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-07-02 17:32:09 +02:00
|
|
|
#ifdef SONAME_LIBSSL
|
2011-01-19 20:16:02 +01:00
|
|
|
*available = connection->ssl_s ? pSSL_pending(connection->ssl_s) : 0;
|
2003-06-21 01:26:56 +02:00
|
|
|
#endif
|
|
|
|
}
|
2009-05-14 16:55:29 +02:00
|
|
|
return TRUE;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
2006-01-20 20:16:56 +01:00
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
BOOL NETCON_is_alive(netconn_t *netconn)
|
|
|
|
{
|
|
|
|
#ifdef MSG_DONTWAIT
|
|
|
|
ssize_t len;
|
|
|
|
BYTE b;
|
|
|
|
|
|
|
|
len = recv(netconn->socketFD, &b, 1, MSG_PEEK|MSG_DONTWAIT);
|
|
|
|
return len == 1 || (len == -1 && errno == EWOULDBLOCK);
|
|
|
|
#elif defined(__MINGW32__) || defined(_MSC_VER)
|
|
|
|
ULONG mode;
|
|
|
|
int len;
|
|
|
|
char b;
|
|
|
|
|
|
|
|
mode = 1;
|
|
|
|
if(!ioctlsocket(netconn->socketFD, FIONBIO, &mode))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
len = recv(netconn->socketFD, &b, 1, MSG_PEEK);
|
|
|
|
|
|
|
|
mode = 0;
|
|
|
|
if(!ioctlsocket(netconn->socketFD, FIONBIO, &mode))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return len == 1 || (len == -1 && errno == WSAEWOULDBLOCK);
|
|
|
|
#else
|
|
|
|
FIXME("not supported on this platform\n");
|
|
|
|
return TRUE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-28 18:29:04 +02:00
|
|
|
LPCVOID NETCON_GetCert(netconn_t *connection)
|
2006-01-20 20:16:56 +01:00
|
|
|
{
|
2007-07-02 17:32:09 +02:00
|
|
|
#ifdef SONAME_LIBSSL
|
2006-01-20 20:16:56 +01:00
|
|
|
X509* cert;
|
|
|
|
LPCVOID r = NULL;
|
|
|
|
|
2011-01-19 20:16:02 +01:00
|
|
|
if (!connection->ssl_s)
|
2006-01-20 20:16:56 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cert = pSSL_get_peer_certificate(connection->ssl_s);
|
2009-12-14 01:05:35 +01:00
|
|
|
r = X509_to_cert_context(cert);
|
2006-01-20 20:16:56 +01:00
|
|
|
return r;
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
2006-03-14 15:36:11 +01:00
|
|
|
|
2011-04-28 18:29:04 +02:00
|
|
|
int NETCON_GetCipherStrength(netconn_t *connection)
|
2010-09-30 22:18:07 +02:00
|
|
|
{
|
|
|
|
#ifdef SONAME_LIBSSL
|
2010-10-25 06:42:02 +02:00
|
|
|
#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
|
|
|
|
const SSL_CIPHER *cipher;
|
|
|
|
#else
|
2010-09-30 22:18:07 +02:00
|
|
|
SSL_CIPHER *cipher;
|
2010-10-25 06:42:02 +02:00
|
|
|
#endif
|
2010-09-30 22:18:07 +02:00
|
|
|
int bits = 0;
|
|
|
|
|
2011-01-19 20:16:02 +01:00
|
|
|
if (!connection->ssl_s)
|
2010-09-30 22:18:07 +02:00
|
|
|
return 0;
|
|
|
|
cipher = pSSL_get_current_cipher(connection->ssl_s);
|
|
|
|
if (!cipher)
|
|
|
|
return 0;
|
|
|
|
pSSL_CIPHER_get_bits(cipher, &bits);
|
|
|
|
return bits;
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-04-28 18:29:04 +02:00
|
|
|
DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, int value)
|
2006-03-14 15:36:11 +01:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
/* value is in milliseconds, convert to struct timeval */
|
|
|
|
tv.tv_sec = value / 1000;
|
|
|
|
tv.tv_usec = (value % 1000) * 1000;
|
|
|
|
|
|
|
|
result = setsockopt(connection->socketFD, SOL_SOCKET,
|
2008-12-08 09:28:43 +01:00
|
|
|
send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv,
|
2006-03-14 15:36:11 +01:00
|
|
|
sizeof(tv));
|
|
|
|
|
|
|
|
if (result == -1)
|
|
|
|
{
|
|
|
|
WARN("setsockopt failed (%s)\n", strerror(errno));
|
2008-02-26 20:22:02 +01:00
|
|
|
return sock_get_error(errno);
|
2006-03-14 15:36:11 +01:00
|
|
|
}
|
|
|
|
|
2008-02-26 20:22:02 +01:00
|
|
|
return ERROR_SUCCESS;
|
2006-03-14 15:36:11 +01:00
|
|
|
}
|