2003-06-21 01:26:56 +02:00
|
|
|
/*
|
2015-02-13 12:12:29 +01:00
|
|
|
* Wininet - networking layer
|
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
|
|
|
*/
|
|
|
|
|
2009-12-14 01:05:35 +01:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
|
2015-02-13 12:12:29 +01:00
|
|
|
#include "ws2tcpip.h"
|
2008-06-02 12:14:37 +02:00
|
|
|
|
2015-02-13 12:12:29 +01:00
|
|
|
#include <time.h>
|
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>
|
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"
|
|
|
|
|
2003-06-21 01:26:56 +02:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(wininet);
|
|
|
|
|
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 |
|
2014-12-31 06:47:00 +01:00
|
|
|
CERT_TRUST_IS_NOT_SIGNATURE_VALID |
|
2012-05-25 16:51:20 +02:00
|
|
|
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
|
|
|
|
2014-12-31 06:47:00 +01:00
|
|
|
if(errors & CERT_TRUST_IS_NOT_SIGNATURE_VALID) {
|
|
|
|
WARN("CERT_TRUST_IS_NOT_SIGNATURE_VALID\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_NOT_SIGNATURE_VALID;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-02-12 12:05:23 +01:00
|
|
|
static BOOL winsock_loaded = FALSE;
|
|
|
|
|
|
|
|
static BOOL WINAPI winsock_startup(INIT_ONCE *once, void *param, void **context)
|
|
|
|
{
|
|
|
|
WSADATA wsa_data;
|
|
|
|
DWORD res;
|
|
|
|
|
|
|
|
res = WSAStartup(MAKEWORD(1,1), &wsa_data);
|
|
|
|
if(res == ERROR_SUCCESS)
|
|
|
|
winsock_loaded = TRUE;
|
|
|
|
else
|
|
|
|
ERR("WSAStartup failed: %u\n", res);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_winsock(void)
|
|
|
|
{
|
|
|
|
static INIT_ONCE init_once = INIT_ONCE_STATIC_INIT;
|
|
|
|
InitOnceExecuteOnce(&init_once, winsock_startup, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
2015-06-19 15:21:36 +02:00
|
|
|
static void set_socket_blocking(netconn_t *conn, BOOL is_blocking)
|
2015-02-13 12:11:39 +01:00
|
|
|
{
|
2015-06-19 15:21:46 +02:00
|
|
|
if(conn->is_blocking != is_blocking) {
|
|
|
|
ULONG arg = !is_blocking;
|
|
|
|
ioctlsocket(conn->socket, FIONBIO, &arg);
|
|
|
|
}
|
|
|
|
conn->is_blocking = is_blocking;
|
2015-02-13 12:11:39 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2015-02-12 12:05:23 +01:00
|
|
|
DWORD res;
|
|
|
|
|
|
|
|
init_winsock();
|
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) {
|
2015-06-19 15:21:36 +02:00
|
|
|
set_socket_blocking(netconn, FALSE);
|
2013-03-01 11:16:17 +01:00
|
|
|
result = connect(netconn->socket, (struct sockaddr*)&server->addr, server->addr_len);
|
2012-01-13 15:15:04 +01:00
|
|
|
if(result == -1)
|
|
|
|
{
|
2015-03-11 16:52:51 +01:00
|
|
|
res = WSAGetLastError();
|
2015-02-12 12:05:23 +01:00
|
|
|
if (res == WSAEINPROGRESS || res == WSAEWOULDBLOCK) {
|
2015-02-13 12:12:29 +01:00
|
|
|
FD_SET set;
|
2012-01-13 15:15:04 +01:00
|
|
|
int res;
|
2015-02-13 12:12:29 +01:00
|
|
|
socklen_t len = sizeof(res);
|
|
|
|
TIMEVAL timeout_timeval = {0, timeout*1000};
|
2012-01-13 15:15:04 +01:00
|
|
|
|
2015-02-13 12:12:29 +01:00
|
|
|
FD_ZERO(&set);
|
|
|
|
FD_SET(netconn->socket, &set);
|
|
|
|
res = select(netconn->socket+1, NULL, &set, NULL, &timeout_timeval);
|
|
|
|
if(!res || res == SOCKET_ERROR) {
|
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;
|
|
|
|
}
|
2015-02-13 12:12:29 +01:00
|
|
|
if (!getsockopt(netconn->socket, SOL_SOCKET, SO_ERROR, (void *)&res, &len) && !res)
|
|
|
|
result = 0;
|
2012-01-13 15:15:04 +01:00
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
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
|
|
|
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");
|
|
|
|
|
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
|
|
|
|
2016-04-11 10:41:25 +02:00
|
|
|
close_netconn(netconn);
|
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);
|
2015-02-24 13:18:57 +01:00
|
|
|
if(winsock_loaded)
|
|
|
|
WSACleanup();
|
2009-10-02 16:43:45 +02:00
|
|
|
}
|
|
|
|
|
2014-05-30 02:20:14 +02:00
|
|
|
int sock_send(int fd, const void *msg, size_t len, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = send(fd, msg, len, flags);
|
|
|
|
}
|
2015-03-11 16:52:51 +01:00
|
|
|
while(ret == -1 && WSAGetLastError() == WSAEINTR);
|
2014-05-30 02:20:14 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-06-12 07:24:45 +02:00
|
|
|
int sock_recv(int fd, void *msg, size_t len, int flags)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = recv(fd, msg, len, flags);
|
|
|
|
}
|
2015-03-11 16:52:51 +01:00
|
|
|
while(ret == -1 && WSAGetLastError() == WSAEINTR);
|
2014-06-12 07:24:45 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2015-06-19 15:21:36 +02:00
|
|
|
set_socket_blocking(connection, TRUE);
|
|
|
|
|
2013-04-03 14:03:13 +02:00
|
|
|
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);
|
|
|
|
|
2014-05-30 02:20:14 +02:00
|
|
|
size = sock_send(connection->socket, out_buf.pvBuffer, out_buf.cbBuffer, 0);
|
2013-04-03 14:03:13 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-12 07:24:45 +02:00
|
|
|
size = sock_recv(connection->socket, read_buf+in_bufs[0].cbBuffer, read_buf_size-in_bufs[0].cbBuffer, 0);
|
2013-04-03 14:03:13 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-13 13:42:58 +02:00
|
|
|
heap_free(read_buf);
|
|
|
|
|
2013-04-03 14:03:13 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-30 02:20:14 +02:00
|
|
|
if(sock_send(conn->socket, conn->ssl_buf, bufs[0].cbBuffer+bufs[1].cbBuffer+bufs[2].cbBuffer, 0) < 1) {
|
2013-04-03 14:03:34 +02:00
|
|
|
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 */)
|
|
|
|
{
|
2015-06-19 15:21:36 +02:00
|
|
|
/* send is always blocking. */
|
|
|
|
set_socket_blocking(connection, TRUE);
|
|
|
|
|
2013-03-01 11:15:53 +01:00
|
|
|
if(!connection->secure)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2014-05-30 02:20:14 +02:00
|
|
|
*sent = sock_send(connection->socket, msg, len, flags);
|
2015-03-11 16:52:51 +01:00
|
|
|
return *sent == -1 ? WSAGetLastError() : 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-18 15:17:46 +02:00
|
|
|
static BOOL read_ssl_chunk(netconn_t *conn, void *buf, SIZE_T buf_size, BOOL blocking, SIZE_T *ret_size, BOOL *eof)
|
2013-04-03 14:03:52 +02:00
|
|
|
{
|
|
|
|
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};
|
2014-03-05 18:27:53 +01:00
|
|
|
SSIZE_T size, buf_len = 0;
|
2013-04-03 14:03:52 +02:00
|
|
|
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;
|
2014-03-05 18:27:53 +01:00
|
|
|
}
|
2013-04-03 14:03:52 +02:00
|
|
|
|
2015-06-19 15:21:36 +02:00
|
|
|
set_socket_blocking(conn, blocking && !buf_len);
|
2015-02-13 12:12:29 +01:00
|
|
|
size = sock_recv(conn->socket, conn->ssl_buf+buf_len, ssl_buf_size-buf_len, 0);
|
2014-03-05 18:27:53 +01:00
|
|
|
if(size < 0) {
|
2013-04-03 14:03:52 +02:00
|
|
|
if(!buf_len) {
|
2015-03-11 16:52:51 +01:00
|
|
|
if(WSAGetLastError() == WSAEWOULDBLOCK) {
|
2014-03-05 18:27:53 +01:00
|
|
|
TRACE("would block\n");
|
|
|
|
return WSAEWOULDBLOCK;
|
|
|
|
}
|
|
|
|
WARN("recv failed\n");
|
|
|
|
return ERROR_INTERNET_CONNECTION_ABORTED;
|
2013-04-03 14:03:52 +02:00
|
|
|
}
|
2014-03-05 18:27:53 +01:00
|
|
|
}else {
|
|
|
|
buf_len += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!buf_len) {
|
2015-06-18 13:28:24 +02:00
|
|
|
TRACE("EOF\n");
|
2014-03-05 18:27:53 +01:00
|
|
|
*eof = TRUE;
|
2015-06-18 15:17:46 +02:00
|
|
|
*ret_size = 0;
|
2014-03-05 18:27:53 +01:00
|
|
|
return ERROR_SUCCESS;
|
2013-04-03 14:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*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;
|
2014-03-05 18:27:53 +01:00
|
|
|
return ERROR_SUCCESS;
|
2013-04-03 14:03:52 +02:00
|
|
|
case SEC_E_INCOMPLETE_MESSAGE:
|
|
|
|
assert(buf_len < ssl_buf_size);
|
|
|
|
|
2015-06-19 15:21:36 +02:00
|
|
|
set_socket_blocking(conn, blocking);
|
2015-02-13 12:12:29 +01:00
|
|
|
size = sock_recv(conn->socket, conn->ssl_buf+buf_len, ssl_buf_size-buf_len, 0);
|
2014-03-05 18:27:53 +01:00
|
|
|
if(size < 1) {
|
2015-03-11 16:52:51 +01:00
|
|
|
if(size < 0 && WSAGetLastError() == WSAEWOULDBLOCK) {
|
2014-03-05 18:27:53 +01:00
|
|
|
TRACE("would block\n");
|
|
|
|
|
|
|
|
/* FIXME: Optimize extra_buf usage. */
|
|
|
|
conn->extra_buf = heap_alloc(buf_len);
|
|
|
|
if(!conn->extra_buf)
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
|
|
|
conn->extra_len = buf_len;
|
|
|
|
memcpy(conn->extra_buf, conn->ssl_buf, conn->extra_len);
|
|
|
|
return WSAEWOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ERROR_INTERNET_CONNECTION_ABORTED;
|
|
|
|
}
|
2013-04-03 14:03:52 +02:00
|
|
|
|
|
|
|
buf_len += size;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
WARN("failed: %08x\n", res);
|
2014-03-05 18:27:53 +01:00
|
|
|
return ERROR_INTERNET_CONNECTION_ABORTED;
|
2013-04-03 14:03:52 +02:00
|
|
|
}
|
|
|
|
} 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)
|
2014-03-05 18:27:53 +01:00
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
2013-04-03 14:03:52 +02:00
|
|
|
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)
|
2014-03-05 18:27:53 +01:00
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
2013-04-03 14:03:52 +02:00
|
|
|
|
|
|
|
conn->extra_len = bufs[i].cbBuffer;
|
|
|
|
memcpy(conn->extra_buf, bufs[i].pvBuffer, conn->extra_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 18:27:53 +01:00
|
|
|
return ERROR_SUCCESS;
|
2013-04-03 14:03:52 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2015-06-18 13:28:24 +02:00
|
|
|
DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, BOOL blocking, 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
|
|
|
{
|
2015-06-19 15:21:36 +02:00
|
|
|
set_socket_blocking(connection, blocking);
|
2015-06-18 15:17:46 +02:00
|
|
|
*recvd = sock_recv(connection->socket, buf, len, 0);
|
|
|
|
return *recvd == -1 ? WSAGetLastError() : ERROR_SUCCESS;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-18 13:28:24 +02:00
|
|
|
SIZE_T size = 0;
|
2014-03-05 18:27:53 +01:00
|
|
|
BOOL eof;
|
|
|
|
DWORD res;
|
2013-04-03 14:03:52 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-06-18 13:28:24 +02:00
|
|
|
*recvd = size;
|
|
|
|
return ERROR_SUCCESS;
|
2014-03-05 18:27:53 +01:00
|
|
|
}
|
2014-03-05 18:27:36 +01:00
|
|
|
|
2013-04-03 14:03:52 +02:00
|
|
|
do {
|
2015-06-18 15:17:46 +02:00
|
|
|
res = read_ssl_chunk(connection, (BYTE*)buf, len, blocking, &size, &eof);
|
2014-03-05 18:27:53 +01:00
|
|
|
if(res != ERROR_SUCCESS) {
|
|
|
|
if(res == WSAEWOULDBLOCK) {
|
|
|
|
if(size)
|
|
|
|
res = ERROR_SUCCESS;
|
|
|
|
}else {
|
|
|
|
WARN("read_ssl_chunk failed\n");
|
|
|
|
}
|
2013-04-03 14:03:52 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-06-18 13:28:24 +02:00
|
|
|
}while(!size && !eof);
|
2013-04-03 14:03:52 +02:00
|
|
|
|
|
|
|
TRACE("received %ld bytes\n", size);
|
|
|
|
*recvd = size;
|
2014-03-05 18:27:53 +01:00
|
|
|
return res;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-10 11:26:43 +02:00
|
|
|
BOOL NETCON_is_alive(netconn_t *netconn)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
char b;
|
|
|
|
|
2015-06-19 15:21:36 +02:00
|
|
|
set_socket_blocking(netconn, FALSE);
|
2014-06-12 07:24:45 +02:00
|
|
|
len = sock_recv(netconn->socket, &b, 1, MSG_PEEK);
|
2011-05-10 11:26:43 +02:00
|
|
|
|
2015-03-11 16:52:51 +01:00
|
|
|
return len == 1 || (len == -1 && WSAGetLastError() == WSAEWOULDBLOCK);
|
2011-05-10 11:26:43 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2013-03-01 11:16:17 +01:00
|
|
|
result = setsockopt(connection->socket, SOL_SOCKET,
|
2015-03-25 13:10:14 +01:00
|
|
|
send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&value,
|
|
|
|
sizeof(value));
|
2006-03-14 15:36:11 +01:00
|
|
|
if (result == -1)
|
|
|
|
{
|
2015-02-11 19:37:37 +01:00
|
|
|
WARN("setsockopt failed\n");
|
2015-03-11 16:52:51 +01:00
|
|
|
return WSAGetLastError();
|
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
|
|
|
}
|