2003-06-21 01:26:56 +02:00
|
|
|
/*
|
2013-04-03 14:05:51 +02:00
|
|
|
* Wininet - networking layer. Uses unix sockets.
|
2003-06-21 01:26:56 +02:00
|
|
|
*
|
|
|
|
* Copyright 2002 TransGaming Technologies Inc.
|
2013-04-03 14:05:51 +02:00
|
|
|
* Copyright 2013 Jacek Caban for CodeWeavers
|
2003-06-21 01:26:56 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
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"
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2012-05-25 16:51:20 +02:00
|
|
|
static DWORD netconn_verify_cert(netconn_t *conn, PCCERT_CONTEXT cert, HCERTSTORE store)
|
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 };
|
2012-12-11 09:45:55 +01:00
|
|
|
DWORD err = ERROR_SUCCESS, errors;
|
2012-05-25 16:51:20 +02:00
|
|
|
|
|
|
|
static const DWORD supportedErrors =
|
|
|
|
CERT_TRUST_IS_NOT_TIME_VALID |
|
|
|
|
CERT_TRUST_IS_UNTRUSTED_ROOT |
|
|
|
|
CERT_TRUST_IS_PARTIAL_CHAIN |
|
|
|
|
CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
|
|
|
|
|
|
|
|
TRACE("verifying %s\n", debugstr_w(conn->server->name));
|
2009-12-14 01:05:35 +01:00
|
|
|
|
|
|
|
chainPara.RequestedUsage.Usage.cUsageIdentifier = 1;
|
|
|
|
chainPara.RequestedUsage.Usage.rgpszUsageIdentifier = server_auth;
|
2012-12-11 09:45:55 +01:00
|
|
|
if (!(ret = CertGetCertificateChain(NULL, cert, NULL, store, &chainPara, 0, NULL, &chain))) {
|
2012-05-25 16:51:20 +02:00
|
|
|
TRACE("failed\n");
|
|
|
|
return GetLastError();
|
|
|
|
}
|
|
|
|
|
|
|
|
errors = chain->TrustStatus.dwErrorStatus;
|
|
|
|
|
2012-06-18 17:42:18 +02:00
|
|
|
do {
|
|
|
|
/* This seems strange, but that's what tests show */
|
2012-12-11 09:45:55 +01:00
|
|
|
if(errors & CERT_TRUST_IS_PARTIAL_CHAIN) {
|
2012-06-18 17:42:18 +02:00
|
|
|
WARN("ERROR_INTERNET_SEC_CERT_REV_FAILED\n");
|
|
|
|
err = ERROR_INTERNET_SEC_CERT_REV_FAILED;
|
|
|
|
if(conn->mask_errors)
|
|
|
|
conn->security_flags |= _SECURITY_FLAG_CERT_REV_FAILED;
|
|
|
|
if(!(conn->security_flags & SECURITY_FLAG_IGNORE_REVOCATION))
|
|
|
|
break;
|
|
|
|
}
|
2012-05-25 16:51:20 +02:00
|
|
|
|
2012-06-18 17:42:18 +02:00
|
|
|
if (chain->TrustStatus.dwErrorStatus & ~supportedErrors) {
|
|
|
|
WARN("error status %x\n", chain->TrustStatus.dwErrorStatus & ~supportedErrors);
|
|
|
|
err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_INVALID_CERT;
|
|
|
|
errors &= supportedErrors;
|
|
|
|
if(!conn->mask_errors)
|
|
|
|
break;
|
|
|
|
WARN("unknown error flags\n");
|
|
|
|
}
|
2012-05-25 16:51:20 +02:00
|
|
|
|
2012-06-18 17:42:18 +02:00
|
|
|
if(errors & CERT_TRUST_IS_NOT_TIME_VALID) {
|
|
|
|
WARN("CERT_TRUST_IS_NOT_TIME_VALID\n");
|
|
|
|
if(!(conn->security_flags & SECURITY_FLAG_IGNORE_CERT_DATE_INVALID)) {
|
|
|
|
err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_CERT_DATE_INVALID;
|
|
|
|
if(!conn->mask_errors)
|
|
|
|
break;
|
|
|
|
conn->security_flags |= _SECURITY_FLAG_CERT_INVALID_DATE;
|
|
|
|
}
|
|
|
|
errors &= ~CERT_TRUST_IS_NOT_TIME_VALID;
|
|
|
|
}
|
2012-05-28 13:55:00 +02:00
|
|
|
|
2012-06-18 17:42:18 +02:00
|
|
|
if(errors & CERT_TRUST_IS_UNTRUSTED_ROOT) {
|
|
|
|
WARN("CERT_TRUST_IS_UNTRUSTED_ROOT\n");
|
|
|
|
if(!(conn->security_flags & SECURITY_FLAG_IGNORE_UNKNOWN_CA)) {
|
|
|
|
err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_INVALID_CA;
|
|
|
|
if(!conn->mask_errors)
|
|
|
|
break;
|
|
|
|
conn->security_flags |= _SECURITY_FLAG_CERT_INVALID_CA;
|
|
|
|
}
|
|
|
|
errors &= ~CERT_TRUST_IS_UNTRUSTED_ROOT;
|
2012-06-07 15:40:31 +02:00
|
|
|
}
|
2012-06-18 17:42:18 +02:00
|
|
|
|
|
|
|
if(errors & CERT_TRUST_IS_PARTIAL_CHAIN) {
|
|
|
|
WARN("CERT_TRUST_IS_PARTIAL_CHAIN\n");
|
|
|
|
if(!(conn->security_flags & SECURITY_FLAG_IGNORE_UNKNOWN_CA)) {
|
|
|
|
err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_INVALID_CA;
|
|
|
|
if(!conn->mask_errors)
|
|
|
|
break;
|
2012-06-07 15:40:31 +02:00
|
|
|
conn->security_flags |= _SECURITY_FLAG_CERT_INVALID_CA;
|
2012-06-18 17:42:18 +02:00
|
|
|
}
|
|
|
|
errors &= ~CERT_TRUST_IS_PARTIAL_CHAIN;
|
2012-06-07 15:40:31 +02:00
|
|
|
}
|
2012-05-25 16:51:20 +02:00
|
|
|
|
2012-06-18 17:42:18 +02:00
|
|
|
if(errors & CERT_TRUST_IS_NOT_VALID_FOR_USAGE) {
|
|
|
|
WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE\n");
|
|
|
|
if(!(conn->security_flags & SECURITY_FLAG_IGNORE_WRONG_USAGE)) {
|
|
|
|
err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_INVALID_CERT;
|
|
|
|
if(!conn->mask_errors)
|
|
|
|
break;
|
|
|
|
WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE, unknown error flags\n");
|
|
|
|
}
|
|
|
|
errors &= ~CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(err == ERROR_INTERNET_SEC_CERT_REV_FAILED) {
|
|
|
|
assert(conn->security_flags & SECURITY_FLAG_IGNORE_REVOCATION);
|
|
|
|
err = ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
}while(0);
|
2012-05-25 16:51:20 +02:00
|
|
|
|
|
|
|
if(!err || conn->mask_errors) {
|
|
|
|
CERT_CHAIN_POLICY_PARA policyPara;
|
|
|
|
SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara;
|
|
|
|
CERT_CHAIN_POLICY_STATUS policyStatus;
|
|
|
|
CERT_CHAIN_CONTEXT chainCopy;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
sslExtraPolicyPara.u.cbSize = sizeof(sslExtraPolicyPara);
|
|
|
|
sslExtraPolicyPara.dwAuthType = AUTHTYPE_SERVER;
|
|
|
|
sslExtraPolicyPara.pwszServerName = conn->server->name;
|
|
|
|
sslExtraPolicyPara.fdwChecks = conn->security_flags;
|
|
|
|
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);
|
2012-05-25 16:51:20 +02:00
|
|
|
/* Any error in the policy status indicates that the
|
|
|
|
* policy couldn't be verified.
|
|
|
|
*/
|
|
|
|
if(ret) {
|
|
|
|
if(policyStatus.dwError == CERT_E_CN_NO_MATCH) {
|
2012-06-01 16:41:44 +02:00
|
|
|
WARN("CERT_E_CN_NO_MATCH\n");
|
2012-05-28 13:54:32 +02:00
|
|
|
if(conn->mask_errors)
|
|
|
|
conn->security_flags |= _SECURITY_FLAG_CERT_INVALID_CN;
|
2012-05-28 13:55:00 +02:00
|
|
|
err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_CERT_CN_INVALID;
|
2012-05-25 16:51:20 +02:00
|
|
|
}else if(policyStatus.dwError) {
|
2012-06-01 16:41:44 +02:00
|
|
|
WARN("policyStatus.dwError %x\n", policyStatus.dwError);
|
2012-05-28 13:54:32 +02:00
|
|
|
if(conn->mask_errors)
|
|
|
|
WARN("unknown error flags for policy status %x\n", policyStatus.dwError);
|
2012-05-28 13:55:00 +02:00
|
|
|
err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_INVALID_CERT;
|
2009-12-14 01:05:35 +01:00
|
|
|
}
|
2012-05-25 16:51:20 +02:00
|
|
|
}else {
|
|
|
|
err = GetLastError();
|
2009-12-14 01:05:35 +01:00
|
|
|
}
|
|
|
|
}
|
2012-05-25 16:51:20 +02:00
|
|
|
|
|
|
|
if(err) {
|
|
|
|
WARN("failed %u\n", err);
|
2012-06-11 10:22:38 +02:00
|
|
|
CertFreeCertificateChain(chain);
|
|
|
|
if(conn->server->cert_chain) {
|
|
|
|
CertFreeCertificateChain(conn->server->cert_chain);
|
|
|
|
conn->server->cert_chain = NULL;
|
|
|
|
}
|
2012-05-28 13:55:00 +02:00
|
|
|
if(conn->mask_errors)
|
2012-05-28 13:54:32 +02:00
|
|
|
conn->server->security_flags |= conn->security_flags & _SECURITY_ERROR_FLAGS_MASK;
|
2012-05-25 16:51:20 +02:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2012-06-11 10:22:38 +02:00
|
|
|
/* FIXME: Reuse cached chain */
|
|
|
|
if(conn->server->cert_chain)
|
|
|
|
CertFreeCertificateChain(chain);
|
|
|
|
else
|
|
|
|
conn->server->cert_chain = chain;
|
2012-05-25 16:51:20 +02:00
|
|
|
return ERROR_SUCCESS;
|
2009-12-14 01:05:35 +01:00
|
|
|
}
|
|
|
|
|
2013-04-03 14:03:13 +02:00
|
|
|
static SecHandle cred_handle, compat_cred_handle;
|
|
|
|
static BOOL cred_handle_initialized, have_compat_cred_handle;
|
|
|
|
|
|
|
|
static CRITICAL_SECTION init_sechandle_cs;
|
|
|
|
static CRITICAL_SECTION_DEBUG init_sechandle_cs_debug = {
|
|
|
|
0, 0, &init_sechandle_cs,
|
|
|
|
{ &init_sechandle_cs_debug.ProcessLocksList,
|
|
|
|
&init_sechandle_cs_debug.ProcessLocksList },
|
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": init_sechandle_cs") }
|
|
|
|
};
|
|
|
|
static CRITICAL_SECTION init_sechandle_cs = { &init_sechandle_cs_debug, -1, 0, 0, 0, 0 };
|
|
|
|
|
|
|
|
static BOOL ensure_cred_handle(void)
|
|
|
|
{
|
|
|
|
SECURITY_STATUS res = SEC_E_OK;
|
|
|
|
|
|
|
|
EnterCriticalSection(&init_sechandle_cs);
|
|
|
|
|
|
|
|
if(!cred_handle_initialized) {
|
|
|
|
SCHANNEL_CRED cred = {SCHANNEL_CRED_VERSION};
|
|
|
|
SecPkgCred_SupportedProtocols prots;
|
|
|
|
|
|
|
|
res = AcquireCredentialsHandleW(NULL, (WCHAR*)UNISP_NAME_W, SECPKG_CRED_OUTBOUND, NULL, &cred,
|
|
|
|
NULL, NULL, &cred_handle, NULL);
|
|
|
|
if(res == SEC_E_OK) {
|
|
|
|
res = QueryCredentialsAttributesA(&cred_handle, SECPKG_ATTR_SUPPORTED_PROTOCOLS, &prots);
|
|
|
|
if(res != SEC_E_OK || (prots.grbitProtocol & SP_PROT_TLS1_1PLUS_CLIENT)) {
|
|
|
|
cred.grbitEnabledProtocols = prots.grbitProtocol & ~SP_PROT_TLS1_1PLUS_CLIENT;
|
|
|
|
res = AcquireCredentialsHandleW(NULL, (WCHAR*)UNISP_NAME_W, SECPKG_CRED_OUTBOUND, NULL, &cred,
|
|
|
|
NULL, NULL, &compat_cred_handle, NULL);
|
|
|
|
have_compat_cred_handle = res == SEC_E_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cred_handle_initialized = res == SEC_E_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
LeaveCriticalSection(&init_sechandle_cs);
|
|
|
|
|
|
|
|
if(res != SEC_E_OK) {
|
|
|
|
WARN("Failed: %08x\n", res);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-10-31 14:29:26 +01:00
|
|
|
static DWORD create_netconn_socket(server_t *server, netconn_t *netconn, DWORD timeout)
|
2011-04-28 18:29:37 +02:00
|
|
|
{
|
2013-03-06 13:32:49 +01:00
|
|
|
int result;
|
|
|
|
ULONG flag;
|
2011-04-28 18:29:37 +02:00
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
assert(server->addr_len);
|
2013-03-01 11:16:17 +01:00
|
|
|
result = netconn->socket = socket(server->addr.ss_family, SOCK_STREAM, 0);
|
2011-05-10 11:26:43 +02:00
|
|
|
if(result != -1) {
|
2012-01-13 15:15:04 +01:00
|
|
|
flag = 1;
|
2013-03-01 11:16:17 +01:00
|
|
|
ioctlsocket(netconn->socket, FIONBIO, &flag);
|
|
|
|
result = connect(netconn->socket, (struct sockaddr*)&server->addr, server->addr_len);
|
2012-01-13 15:15:04 +01:00
|
|
|
if(result == -1)
|
|
|
|
{
|
|
|
|
if (sock_get_error(errno) == WSAEINPROGRESS) {
|
|
|
|
struct pollfd pfd;
|
|
|
|
int res;
|
|
|
|
|
2013-03-01 11:16:17 +01:00
|
|
|
pfd.fd = netconn->socket;
|
2012-01-13 15:15:04 +01:00
|
|
|
pfd.events = POLLOUT;
|
|
|
|
res = poll(&pfd, 1, timeout);
|
|
|
|
if (!res)
|
|
|
|
{
|
2013-03-01 11:16:17 +01:00
|
|
|
closesocket(netconn->socket);
|
2013-12-11 10:48:41 +01:00
|
|
|
netconn->socket = -1;
|
2012-01-13 15:15:04 +01:00
|
|
|
return ERROR_INTERNET_CANNOT_CONNECT;
|
|
|
|
}
|
|
|
|
else if (res > 0)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
socklen_t len = sizeof(err);
|
2013-03-06 13:32:49 +01:00
|
|
|
if (!getsockopt(netconn->socket, SOL_SOCKET, SO_ERROR, (void *)&err, &len) && !err)
|
2012-01-13 15:15:04 +01:00
|
|
|
result = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-10 11:26:43 +02:00
|
|
|
if(result == -1)
|
2013-12-11 10:48:41 +01:00
|
|
|
{
|
2013-03-01 11:16:17 +01:00
|
|
|
closesocket(netconn->socket);
|
2013-12-11 10:48:41 +01:00
|
|
|
netconn->socket = -1;
|
|
|
|
}
|
2012-01-13 15:15:04 +01:00
|
|
|
else {
|
|
|
|
flag = 0;
|
2013-03-01 11:16:17 +01:00
|
|
|
ioctlsocket(netconn->socket, FIONBIO, &flag);
|
2012-01-13 15:15:04 +01:00
|
|
|
}
|
2011-05-10 11:26:43 +02:00
|
|
|
}
|
2012-11-14 19:59:31 +01:00
|
|
|
if(result == -1)
|
2012-12-19 12:02:36 +01:00
|
|
|
return ERROR_INTERNET_CANNOT_CONNECT;
|
2011-05-10 11:26:43 +02:00
|
|
|
|
2011-05-11 13:32:34 +02:00
|
|
|
#ifdef TCP_NODELAY
|
|
|
|
flag = 1;
|
2013-03-01 11:16:17 +01:00
|
|
|
result = setsockopt(netconn->socket, IPPROTO_TCP, TCP_NODELAY, (void*)&flag, sizeof(flag));
|
2011-05-11 13:32:34 +02:00
|
|
|
if(result < 0)
|
|
|
|
WARN("setsockopt(TCP_NODELAY) failed\n");
|
|
|
|
#endif
|
|
|
|
|
2012-10-31 14:29:26 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD create_netconn(BOOL useSSL, server_t *server, DWORD security_flags, BOOL mask_errors, DWORD timeout, netconn_t **ret)
|
|
|
|
{
|
|
|
|
netconn_t *netconn;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
netconn = heap_alloc_zero(sizeof(*netconn));
|
|
|
|
if(!netconn)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
2013-03-01 11:16:17 +01:00
|
|
|
netconn->socket = -1;
|
2012-10-31 14:29:26 +01:00
|
|
|
netconn->security_flags = security_flags | server->security_flags;
|
|
|
|
netconn->mask_errors = mask_errors;
|
|
|
|
list_init(&netconn->pool_entry);
|
2013-12-11 10:49:34 +01:00
|
|
|
SecInvalidateHandle(&netconn->ssl_ctx);
|
2012-10-31 14:29:26 +01:00
|
|
|
|
|
|
|
result = create_netconn_socket(server, netconn, timeout);
|
2012-11-14 19:59:31 +01:00
|
|
|
if (result != ERROR_SUCCESS) {
|
|
|
|
heap_free(netconn);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
server_addref(server);
|
|
|
|
netconn->server = server;
|
|
|
|
*ret = netconn;
|
2012-10-31 14:29:26 +01:00
|
|
|
return result;
|
2011-05-10 11:26:43 +02:00
|
|
|
}
|
|
|
|
|
2013-12-11 10:48:41 +01:00
|
|
|
BOOL is_valid_netconn(netconn_t *netconn)
|
|
|
|
{
|
|
|
|
return netconn && netconn->socket != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close_netconn(netconn_t *netconn)
|
|
|
|
{
|
|
|
|
closesocket(netconn->socket);
|
|
|
|
netconn->socket = -1;
|
|
|
|
}
|
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
void free_netconn(netconn_t *netconn)
|
|
|
|
{
|
|
|
|
server_release(netconn->server);
|
|
|
|
|
2013-03-04 17:48:59 +01:00
|
|
|
if (netconn->secure) {
|
2013-04-03 14:03:52 +02:00
|
|
|
heap_free(netconn->peek_msg_mem);
|
|
|
|
netconn->peek_msg_mem = NULL;
|
|
|
|
netconn->peek_msg = NULL;
|
|
|
|
netconn->peek_len = 0;
|
2013-04-03 14:03:13 +02:00
|
|
|
heap_free(netconn->ssl_buf);
|
|
|
|
netconn->ssl_buf = NULL;
|
2013-04-03 14:03:52 +02:00
|
|
|
heap_free(netconn->extra_buf);
|
|
|
|
netconn->extra_buf = NULL;
|
|
|
|
netconn->extra_len = 0;
|
2013-12-11 10:49:34 +01:00
|
|
|
if (SecIsValidHandle(&netconn->ssl_ctx))
|
|
|
|
DeleteSecurityContext(&netconn->ssl_ctx);
|
2013-03-04 17:48:59 +01:00
|
|
|
}
|
2011-05-10 11:26:43 +02:00
|
|
|
|
|
|
|
heap_free(netconn);
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
|
2009-10-02 16:43:45 +02:00
|
|
|
void NETCON_unload(void)
|
|
|
|
{
|
2013-04-03 14:03:13 +02:00
|
|
|
if(cred_handle_initialized)
|
|
|
|
FreeCredentialsHandle(&cred_handle);
|
|
|
|
if(have_compat_cred_handle)
|
|
|
|
FreeCredentialsHandle(&compat_cred_handle);
|
|
|
|
DeleteCriticalSection(&init_sechandle_cs);
|
2009-10-02 16:43:45 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-04-02 11:13:48 +02:00
|
|
|
static DWORD netcon_secure_connect_setup(netconn_t *connection, BOOL compat_mode)
|
2012-10-31 14:29:26 +01:00
|
|
|
{
|
2013-04-03 14:03:13 +02:00
|
|
|
SecBuffer out_buf = {0, SECBUFFER_TOKEN, NULL}, in_bufs[2] = {{0, SECBUFFER_TOKEN}, {0, SECBUFFER_EMPTY}};
|
|
|
|
SecBufferDesc out_desc = {SECBUFFER_VERSION, 1, &out_buf}, in_desc = {SECBUFFER_VERSION, 2, in_bufs};
|
|
|
|
SecHandle *cred = &cred_handle;
|
|
|
|
BYTE *read_buf;
|
|
|
|
SIZE_T read_buf_size = 2048;
|
|
|
|
ULONG attrs = 0;
|
|
|
|
CtxtHandle ctx;
|
|
|
|
SSIZE_T size;
|
|
|
|
int bits;
|
|
|
|
const CERT_CONTEXT *cert;
|
|
|
|
SECURITY_STATUS status;
|
|
|
|
DWORD res = ERROR_SUCCESS;
|
|
|
|
|
|
|
|
const DWORD isc_req_flags = ISC_REQ_ALLOCATE_MEMORY|ISC_REQ_USE_SESSION_KEY|ISC_REQ_CONFIDENTIALITY
|
|
|
|
|ISC_REQ_SEQUENCE_DETECT|ISC_REQ_REPLAY_DETECT|ISC_REQ_MANUAL_CRED_VALIDATION;
|
|
|
|
|
|
|
|
if(!ensure_cred_handle())
|
2013-12-11 10:47:54 +01:00
|
|
|
return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
2013-04-03 14:03:13 +02:00
|
|
|
|
|
|
|
if(compat_mode) {
|
|
|
|
if(!have_compat_cred_handle)
|
|
|
|
return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
cred = &compat_cred_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
read_buf = heap_alloc(read_buf_size);
|
|
|
|
if(!read_buf)
|
|
|
|
return ERROR_OUTOFMEMORY;
|
|
|
|
|
|
|
|
status = InitializeSecurityContextW(cred, NULL, connection->server->name, isc_req_flags, 0, 0, NULL, 0,
|
|
|
|
&ctx, &out_desc, &attrs, NULL);
|
|
|
|
|
|
|
|
assert(status != SEC_E_OK);
|
|
|
|
|
|
|
|
while(status == SEC_I_CONTINUE_NEEDED || status == SEC_E_INCOMPLETE_MESSAGE) {
|
|
|
|
if(out_buf.cbBuffer) {
|
|
|
|
assert(status == SEC_I_CONTINUE_NEEDED);
|
|
|
|
|
|
|
|
TRACE("sending %u bytes\n", out_buf.cbBuffer);
|
|
|
|
|
|
|
|
size = send(connection->socket, out_buf.pvBuffer, out_buf.cbBuffer, 0);
|
|
|
|
if(size != out_buf.cbBuffer) {
|
|
|
|
ERR("send failed\n");
|
|
|
|
status = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeContextBuffer(out_buf.pvBuffer);
|
|
|
|
out_buf.pvBuffer = NULL;
|
|
|
|
out_buf.cbBuffer = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(status == SEC_I_CONTINUE_NEEDED) {
|
|
|
|
assert(in_bufs[1].cbBuffer < read_buf_size);
|
|
|
|
|
|
|
|
memmove(read_buf, (BYTE*)in_bufs[0].pvBuffer+in_bufs[0].cbBuffer-in_bufs[1].cbBuffer, in_bufs[1].cbBuffer);
|
|
|
|
in_bufs[0].cbBuffer = in_bufs[1].cbBuffer;
|
|
|
|
|
|
|
|
in_bufs[1].BufferType = SECBUFFER_EMPTY;
|
|
|
|
in_bufs[1].cbBuffer = 0;
|
|
|
|
in_bufs[1].pvBuffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(in_bufs[0].BufferType == SECBUFFER_TOKEN);
|
|
|
|
assert(in_bufs[1].BufferType == SECBUFFER_EMPTY);
|
|
|
|
|
|
|
|
if(in_bufs[0].cbBuffer + 1024 > read_buf_size) {
|
|
|
|
BYTE *new_read_buf;
|
|
|
|
|
|
|
|
new_read_buf = heap_realloc(read_buf, read_buf_size + 1024);
|
|
|
|
if(!new_read_buf) {
|
|
|
|
status = E_OUTOFMEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
in_bufs[0].pvBuffer = read_buf = new_read_buf;
|
|
|
|
read_buf_size += 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = recv(connection->socket, read_buf+in_bufs[0].cbBuffer, read_buf_size-in_bufs[0].cbBuffer, 0);
|
|
|
|
if(size < 1) {
|
|
|
|
WARN("recv error\n");
|
|
|
|
res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("recv %lu bytes\n", size);
|
|
|
|
|
|
|
|
in_bufs[0].cbBuffer += size;
|
|
|
|
in_bufs[0].pvBuffer = read_buf;
|
|
|
|
status = InitializeSecurityContextW(cred, &ctx, connection->server->name, isc_req_flags, 0, 0, &in_desc,
|
|
|
|
0, NULL, &out_desc, &attrs, NULL);
|
|
|
|
TRACE("InitializeSecurityContext ret %08x\n", status);
|
|
|
|
|
|
|
|
if(status == SEC_E_OK) {
|
2013-12-11 10:49:34 +01:00
|
|
|
if(SecIsValidHandle(&connection->ssl_ctx))
|
|
|
|
DeleteSecurityContext(&connection->ssl_ctx);
|
|
|
|
connection->ssl_ctx = ctx;
|
|
|
|
|
2013-04-03 14:03:13 +02:00
|
|
|
if(in_bufs[1].BufferType == SECBUFFER_EXTRA)
|
|
|
|
FIXME("SECBUFFER_EXTRA not supported\n");
|
|
|
|
|
|
|
|
status = QueryContextAttributesW(&ctx, SECPKG_ATTR_STREAM_SIZES, &connection->ssl_sizes);
|
|
|
|
if(status != SEC_E_OK) {
|
|
|
|
WARN("Could not get sizes\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = QueryContextAttributesW(&ctx, SECPKG_ATTR_REMOTE_CERT_CONTEXT, (void*)&cert);
|
|
|
|
if(status == SEC_E_OK) {
|
|
|
|
res = netconn_verify_cert(connection, cert, cert->hCertStore);
|
|
|
|
CertFreeCertificateContext(cert);
|
|
|
|
if(res != ERROR_SUCCESS) {
|
|
|
|
WARN("cert verify failed: %u\n", res);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}else {
|
|
|
|
WARN("Could not get cert\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
connection->ssl_buf = heap_alloc(connection->ssl_sizes.cbHeader + connection->ssl_sizes.cbMaximumMessage
|
|
|
|
+ connection->ssl_sizes.cbTrailer);
|
|
|
|
if(!connection->ssl_buf) {
|
|
|
|
res = GetLastError();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(status != SEC_E_OK || res != ERROR_SUCCESS) {
|
2013-12-11 10:49:34 +01:00
|
|
|
WARN("Failed to establish SSL connection: %08x (%u)\n", status, res);
|
2013-04-03 14:03:13 +02:00
|
|
|
heap_free(connection->ssl_buf);
|
|
|
|
connection->ssl_buf = NULL;
|
|
|
|
return res ? res : ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("established SSL connection\n");
|
2013-03-01 11:15:53 +01:00
|
|
|
connection->secure = TRUE;
|
2013-04-02 11:13:48 +02:00
|
|
|
connection->security_flags |= SECURITY_FLAG_SECURE;
|
2012-05-25 16:34:59 +02:00
|
|
|
|
2012-05-25 16:35:12 +02:00
|
|
|
bits = NETCON_GetCipherStrength(connection);
|
|
|
|
if (bits >= 128)
|
|
|
|
connection->security_flags |= SECURITY_FLAG_STRENGTH_STRONG;
|
|
|
|
else if (bits >= 56)
|
|
|
|
connection->security_flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
|
|
|
|
else
|
|
|
|
connection->security_flags |= SECURITY_FLAG_STRENGTH_WEAK;
|
|
|
|
|
2012-05-28 13:54:32 +02:00
|
|
|
if(connection->mask_errors)
|
|
|
|
connection->server->security_flags = connection->security_flags;
|
2009-11-30 00:13:21 +01:00
|
|
|
return ERROR_SUCCESS;
|
2013-04-02 11:13:48 +02:00
|
|
|
}
|
2012-10-31 14:29:26 +01:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_secure_connect
|
|
|
|
* Initiates a secure connection over an existing plaintext connection.
|
|
|
|
*/
|
2012-11-16 14:39:16 +01:00
|
|
|
DWORD NETCON_secure_connect(netconn_t *connection, server_t *server)
|
2012-10-31 14:29:26 +01:00
|
|
|
{
|
2013-04-02 11:13:48 +02:00
|
|
|
DWORD res;
|
2013-03-01 11:15:53 +01:00
|
|
|
|
2012-10-31 14:29:26 +01:00
|
|
|
/* can't connect if we are already connected */
|
2013-03-01 11:15:53 +01:00
|
|
|
if(connection->secure) {
|
2012-10-31 14:29:26 +01:00
|
|
|
ERR("already connected\n");
|
|
|
|
return ERROR_INTERNET_CANNOT_CONNECT;
|
|
|
|
}
|
|
|
|
|
2012-11-16 14:39:16 +01:00
|
|
|
if(server != connection->server) {
|
|
|
|
server_release(connection->server);
|
|
|
|
server_addref(server);
|
|
|
|
connection->server = server;
|
|
|
|
}
|
|
|
|
|
2012-10-31 14:29:26 +01:00
|
|
|
/* connect with given TLS options */
|
2013-04-02 11:13:48 +02:00
|
|
|
res = netcon_secure_connect_setup(connection, FALSE);
|
2012-10-31 14:29:26 +01:00
|
|
|
if (res == ERROR_SUCCESS)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
/* FIXME: when got version alert and FIN from server */
|
|
|
|
/* fallback to connect without TLSv1.1/TLSv1.2 */
|
2013-04-08 16:58:28 +02:00
|
|
|
if (res == ERROR_INTERNET_SECURITY_CHANNEL_ERROR && have_compat_cred_handle)
|
2012-10-31 14:29:26 +01:00
|
|
|
{
|
2013-03-01 11:16:17 +01:00
|
|
|
closesocket(connection->socket);
|
2012-10-31 14:29:26 +01:00
|
|
|
res = create_netconn_socket(connection->server, connection, 500);
|
|
|
|
if (res != ERROR_SUCCESS)
|
|
|
|
return res;
|
2013-04-02 11:13:48 +02:00
|
|
|
res = netcon_secure_connect_setup(connection, TRUE);
|
2012-10-31 14:29:26 +01:00
|
|
|
}
|
2009-11-30 00:13:21 +01:00
|
|
|
return res;
|
2005-11-28 11:54:31 +01:00
|
|
|
}
|
|
|
|
|
2013-04-03 14:03:34 +02:00
|
|
|
static BOOL send_ssl_chunk(netconn_t *conn, const void *msg, size_t size)
|
|
|
|
{
|
|
|
|
SecBuffer bufs[4] = {
|
|
|
|
{conn->ssl_sizes.cbHeader, SECBUFFER_STREAM_HEADER, conn->ssl_buf},
|
|
|
|
{size, SECBUFFER_DATA, conn->ssl_buf+conn->ssl_sizes.cbHeader},
|
|
|
|
{conn->ssl_sizes.cbTrailer, SECBUFFER_STREAM_TRAILER, conn->ssl_buf+conn->ssl_sizes.cbHeader+size},
|
|
|
|
{0, SECBUFFER_EMPTY, NULL}
|
|
|
|
};
|
|
|
|
SecBufferDesc buf_desc = {SECBUFFER_VERSION, sizeof(bufs)/sizeof(*bufs), bufs};
|
|
|
|
SECURITY_STATUS res;
|
|
|
|
|
|
|
|
memcpy(bufs[1].pvBuffer, msg, size);
|
|
|
|
res = EncryptMessage(&conn->ssl_ctx, 0, &buf_desc, 0);
|
|
|
|
if(res != SEC_E_OK) {
|
|
|
|
WARN("EncryptMessage failed\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(send(conn->socket, conn->ssl_buf, bufs[0].cbBuffer+bufs[1].cbBuffer+bufs[2].cbBuffer, 0) < 1) {
|
|
|
|
WARN("send failed\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
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 */)
|
|
|
|
{
|
2013-03-01 11:15:53 +01:00
|
|
|
if(!connection->secure)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2013-03-01 11:16:17 +01:00
|
|
|
*sent = send(connection->socket, msg, len, flags);
|
2003-06-21 01:26:56 +02:00
|
|
|
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
|
|
|
|
{
|
2013-04-03 14:03:34 +02:00
|
|
|
const BYTE *ptr = msg;
|
|
|
|
size_t chunk_size;
|
|
|
|
|
|
|
|
*sent = 0;
|
|
|
|
|
|
|
|
while(len) {
|
|
|
|
chunk_size = min(len, connection->ssl_sizes.cbMaximumMessage);
|
|
|
|
if(!send_ssl_chunk(connection, ptr, chunk_size))
|
|
|
|
return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
|
|
|
|
|
|
|
|
*sent += chunk_size;
|
|
|
|
ptr += chunk_size;
|
|
|
|
len -= chunk_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_SUCCESS;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-03 14:03:52 +02:00
|
|
|
static BOOL read_ssl_chunk(netconn_t *conn, void *buf, SIZE_T buf_size, SIZE_T *ret_size, BOOL *eof)
|
|
|
|
{
|
|
|
|
const SIZE_T ssl_buf_size = conn->ssl_sizes.cbHeader+conn->ssl_sizes.cbMaximumMessage+conn->ssl_sizes.cbTrailer;
|
|
|
|
SecBuffer bufs[4];
|
|
|
|
SecBufferDesc buf_desc = {SECBUFFER_VERSION, sizeof(bufs)/sizeof(*bufs), bufs};
|
|
|
|
SSIZE_T size, buf_len;
|
|
|
|
int i;
|
|
|
|
SECURITY_STATUS res;
|
|
|
|
|
|
|
|
assert(conn->extra_len < ssl_buf_size);
|
|
|
|
|
|
|
|
if(conn->extra_len) {
|
|
|
|
memcpy(conn->ssl_buf, conn->extra_buf, conn->extra_len);
|
|
|
|
buf_len = conn->extra_len;
|
|
|
|
conn->extra_len = 0;
|
|
|
|
heap_free(conn->extra_buf);
|
|
|
|
conn->extra_buf = NULL;
|
|
|
|
}else {
|
|
|
|
buf_len = recv(conn->socket, conn->ssl_buf+conn->extra_len, ssl_buf_size-conn->extra_len, 0);
|
|
|
|
if(buf_len < 0) {
|
|
|
|
WARN("recv failed\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!buf_len) {
|
|
|
|
*eof = TRUE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret_size = 0;
|
|
|
|
*eof = FALSE;
|
|
|
|
|
|
|
|
do {
|
|
|
|
memset(bufs, 0, sizeof(bufs));
|
|
|
|
bufs[0].BufferType = SECBUFFER_DATA;
|
|
|
|
bufs[0].cbBuffer = buf_len;
|
|
|
|
bufs[0].pvBuffer = conn->ssl_buf;
|
|
|
|
|
|
|
|
res = DecryptMessage(&conn->ssl_ctx, &buf_desc, 0, NULL);
|
|
|
|
switch(res) {
|
|
|
|
case SEC_E_OK:
|
|
|
|
break;
|
|
|
|
case SEC_I_CONTEXT_EXPIRED:
|
|
|
|
TRACE("context expired\n");
|
|
|
|
*eof = TRUE;
|
|
|
|
return TRUE;
|
|
|
|
case SEC_E_INCOMPLETE_MESSAGE:
|
|
|
|
assert(buf_len < ssl_buf_size);
|
|
|
|
|
|
|
|
size = recv(conn->socket, conn->ssl_buf+buf_len, ssl_buf_size-buf_len, 0);
|
|
|
|
if(size < 1)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
buf_len += size;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
WARN("failed: %08x\n", res);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} while(res != SEC_E_OK);
|
|
|
|
|
|
|
|
for(i=0; i < sizeof(bufs)/sizeof(*bufs); i++) {
|
|
|
|
if(bufs[i].BufferType == SECBUFFER_DATA) {
|
|
|
|
size = min(buf_size, bufs[i].cbBuffer);
|
|
|
|
memcpy(buf, bufs[i].pvBuffer, size);
|
|
|
|
if(size < bufs[i].cbBuffer) {
|
|
|
|
assert(!conn->peek_len);
|
|
|
|
conn->peek_msg_mem = conn->peek_msg = heap_alloc(bufs[i].cbBuffer - size);
|
|
|
|
if(!conn->peek_msg)
|
|
|
|
return FALSE;
|
|
|
|
conn->peek_len = bufs[i].cbBuffer-size;
|
|
|
|
memcpy(conn->peek_msg, (char*)bufs[i].pvBuffer+size, conn->peek_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret_size = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i=0; i < sizeof(bufs)/sizeof(*bufs); i++) {
|
|
|
|
if(bufs[i].BufferType == SECBUFFER_EXTRA) {
|
|
|
|
conn->extra_buf = heap_alloc(bufs[i].cbBuffer);
|
|
|
|
if(!conn->extra_buf)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
conn->extra_len = bufs[i].cbBuffer;
|
|
|
|
memcpy(conn->extra_buf, bufs[i].pvBuffer, conn->extra_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2003-06-21 01:26:56 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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
|
|
|
*/
|
2013-03-01 11:15:53 +01:00
|
|
|
DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, int flags, int *recvd)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
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;
|
2013-03-01 11:15:53 +01:00
|
|
|
|
|
|
|
if (!connection->secure)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2013-03-01 11:16:17 +01:00
|
|
|
*recvd = recv(connection->socket, 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
|
|
|
|
{
|
2013-04-03 14:03:52 +02:00
|
|
|
SIZE_T size = 0, cread;
|
|
|
|
BOOL res, eof;
|
|
|
|
|
|
|
|
if(connection->peek_msg) {
|
|
|
|
size = min(len, connection->peek_len);
|
|
|
|
memcpy(buf, connection->peek_msg, size);
|
|
|
|
connection->peek_len -= size;
|
|
|
|
connection->peek_msg += size;
|
|
|
|
|
|
|
|
if(!connection->peek_len) {
|
|
|
|
heap_free(connection->peek_msg_mem);
|
|
|
|
connection->peek_msg_mem = connection->peek_msg = NULL;
|
|
|
|
}
|
|
|
|
/* check if we have enough data from the peek buffer */
|
|
|
|
if(!(flags & MSG_WAITALL) || size == len) {
|
|
|
|
*recvd = size;
|
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
res = read_ssl_chunk(connection, (BYTE*)buf+size, len-size, &cread, &eof);
|
|
|
|
if(!res) {
|
|
|
|
WARN("read_ssl_chunk failed\n");
|
|
|
|
if(!size)
|
|
|
|
return ERROR_INTERNET_CONNECTION_ABORTED;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(eof) {
|
|
|
|
TRACE("EOF\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size += cread;
|
|
|
|
}while(!size || ((flags & MSG_WAITALL) && size < len));
|
|
|
|
|
|
|
|
TRACE("received %ld bytes\n", size);
|
|
|
|
*recvd = size;
|
|
|
|
return ERROR_SUCCESS;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-03-01 11:15:53 +01:00
|
|
|
if(!connection->secure)
|
2007-05-28 12:16:06 +02:00
|
|
|
{
|
2009-05-14 16:55:29 +02:00
|
|
|
#ifdef FIONREAD
|
2013-03-06 13:32:49 +01:00
|
|
|
ULONG unread;
|
2013-03-01 11:16:17 +01:00
|
|
|
int retval = ioctlsocket(connection->socket, 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
|
|
|
|
{
|
2013-04-03 14:04:11 +02:00
|
|
|
*available = connection->peek_len;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
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;
|
|
|
|
|
2013-03-01 11:16:17 +01:00
|
|
|
len = recv(netconn->socket, &b, 1, MSG_PEEK|MSG_DONTWAIT);
|
2011-05-10 11:26:43 +02:00
|
|
|
return len == 1 || (len == -1 && errno == EWOULDBLOCK);
|
|
|
|
#elif defined(__MINGW32__) || defined(_MSC_VER)
|
|
|
|
ULONG mode;
|
|
|
|
int len;
|
|
|
|
char b;
|
|
|
|
|
|
|
|
mode = 1;
|
2013-03-01 11:16:17 +01:00
|
|
|
if(!ioctlsocket(netconn->socket, FIONBIO, &mode))
|
2011-05-10 11:26:43 +02:00
|
|
|
return FALSE;
|
|
|
|
|
2013-03-01 11:16:17 +01:00
|
|
|
len = recv(netconn->socket, &b, 1, MSG_PEEK);
|
2011-05-10 11:26:43 +02:00
|
|
|
|
|
|
|
mode = 0;
|
2013-03-01 11:16:17 +01:00
|
|
|
if(!ioctlsocket(netconn->socket, FIONBIO, &mode))
|
2011-05-10 11:26:43 +02:00
|
|
|
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
|
|
|
{
|
2013-04-03 14:04:01 +02:00
|
|
|
const CERT_CONTEXT *ret;
|
|
|
|
SECURITY_STATUS res;
|
|
|
|
|
|
|
|
res = QueryContextAttributesW(&connection->ssl_ctx, SECPKG_ATTR_REMOTE_CERT_CONTEXT, (void*)&ret);
|
|
|
|
return res == SEC_E_OK ? ret : NULL;
|
2006-01-20 20:16:56 +01:00
|
|
|
}
|
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
|
|
|
{
|
2013-04-03 14:03:24 +02:00
|
|
|
SecPkgContext_ConnectionInfo conn_info;
|
|
|
|
SECURITY_STATUS res;
|
|
|
|
|
|
|
|
if (!connection->secure)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
res = QueryContextAttributesW(&connection->ssl_ctx, SECPKG_ATTR_CONNECTION_INFO, (void*)&conn_info);
|
|
|
|
if(res != SEC_E_OK)
|
|
|
|
WARN("QueryContextAttributesW failed: %08x\n", res);
|
|
|
|
return res == SEC_E_OK ? conn_info.dwCipherStrength : 0;
|
2010-09-30 22:18:07 +02:00
|
|
|
}
|
|
|
|
|
2012-01-13 15:15:48 +01:00
|
|
|
DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, DWORD value)
|
2006-03-14 15:36:11 +01:00
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
/* value is in milliseconds, convert to struct timeval */
|
2012-01-13 15:15:48 +01:00
|
|
|
if (value == INFINITE)
|
|
|
|
{
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tv.tv_sec = value / 1000;
|
|
|
|
tv.tv_usec = (value % 1000) * 1000;
|
|
|
|
}
|
2013-03-01 11:16:17 +01:00
|
|
|
result = setsockopt(connection->socket, 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
|
|
|
}
|