📄 netconnection.c
字号:
/*
* 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
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "wine/port.h"
#ifdef HAVE_SYS_TIME_H
# include <sys/time.h>
#endif
#include <sys/types.h>
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "wine/library.h"
#include "windef.h"
#include "winbase.h"
#include "wininet.h"
#include "winerror.h"
/* To avoid conflicts with the Unix socket headers. we only need it for
* the error codes anyway. */
#define USE_WS_PREFIX
#include "winsock2.h"
#include "wine/debug.h"
#include "internet.h"
#include "wincrypt.h"
#define RESPONSE_TIMEOUT 30 /* FROM internet.c */
WINE_DEFAULT_DEBUG_CHANNEL(wininet);
/* FIXME!!!!!!
* This should use winsock - To use winsock the functions will have to change a bit
* as they are designed for unix sockets.
* SSL stuff should use crypt32.dll
*/
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
#include <openssl/err.h>
#ifndef SONAME_LIBSSL
#define SONAME_LIBSSL "libssl.so"
#endif
#ifndef SONAME_LIBCRYPTO
#define SONAME_LIBCRYPTO "libcrypto.so"
#endif
static void *OpenSSL_ssl_handle;
static void *OpenSSL_crypto_handle;
static SSL_METHOD *meth;
static SSL_CTX *ctx;
#define MAKE_FUNCPTR(f) static typeof(f) * p##f
/* OpenSSL functions that we use */
MAKE_FUNCPTR(SSL_library_init);
MAKE_FUNCPTR(SSL_load_error_strings);
MAKE_FUNCPTR(SSLv23_method);
MAKE_FUNCPTR(SSL_CTX_new);
MAKE_FUNCPTR(SSL_new);
MAKE_FUNCPTR(SSL_free);
MAKE_FUNCPTR(SSL_set_fd);
MAKE_FUNCPTR(SSL_connect);
MAKE_FUNCPTR(SSL_shutdown);
MAKE_FUNCPTR(SSL_write);
MAKE_FUNCPTR(SSL_read);
MAKE_FUNCPTR(SSL_get_verify_result);
MAKE_FUNCPTR(SSL_get_peer_certificate);
MAKE_FUNCPTR(SSL_CTX_get_timeout);
MAKE_FUNCPTR(SSL_CTX_set_timeout);
MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
MAKE_FUNCPTR(i2d_X509);
/* OpenSSL's libcrypto functions that we use */
MAKE_FUNCPTR(BIO_new_fp);
MAKE_FUNCPTR(ERR_get_error);
MAKE_FUNCPTR(ERR_error_string);
#undef MAKE_FUNCPTR
#endif
BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
{
connection->useSSL = FALSE;
connection->socketFD = -1;
if (useSSL)
{
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
TRACE("using SSL connection\n");
if (OpenSSL_ssl_handle) /* already initialized everything */
return TRUE;
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);
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
return FALSE;
}
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);
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
return FALSE;
}
/* mmm nice ugly macroness */
#define DYNSSL(x) \
p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
if (!p##x) \
{ \
ERR("failed to load symbol %s\n", #x); \
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
return FALSE; \
}
DYNSSL(SSL_library_init);
DYNSSL(SSL_load_error_strings);
DYNSSL(SSLv23_method);
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_get_verify_result);
DYNSSL(SSL_get_peer_certificate);
DYNSSL(SSL_CTX_get_timeout);
DYNSSL(SSL_CTX_set_timeout);
DYNSSL(SSL_CTX_set_default_verify_paths);
DYNSSL(i2d_X509);
#undef DYNSSL
#define DYNCRYPTO(x) \
p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
if (!p##x) \
{ \
ERR("failed to load symbol %s\n", #x); \
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
return FALSE; \
}
DYNCRYPTO(BIO_new_fp);
DYNCRYPTO(ERR_get_error);
DYNCRYPTO(ERR_error_string);
#undef DYNCRYPTO
pSSL_library_init();
pSSL_load_error_strings();
pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
meth = pSSLv23_method();
connection->peek_msg = NULL;
connection->peek_msg_mem = NULL;
#else
FIXME("can't use SSL, not compiled in.\n");
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
return FALSE;
#endif
}
return TRUE;
}
BOOL NETCON_connected(WININET_NETCONNECTION *connection)
{
if (connection->socketFD == -1)
return FALSE;
else
return TRUE;
}
/******************************************************************************
* NETCON_create
* Basically calls 'socket()'
*/
BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
int type, int protocol)
{
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
if (connection->useSSL)
return FALSE;
#endif
connection->socketFD = socket(domain, type, protocol);
if (INVALID_SOCKET == connection->socketFD)
{
INTERNET_SetLastError(WSAGetLastError());
return FALSE;
}
return TRUE;
}
/******************************************************************************
* NETCON_close
* Basically calls 'close()' unless we should use SSL
*/
BOOL NETCON_close(WININET_NETCONNECTION *connection)
{
int result;
if (!NETCON_connected(connection)) return FALSE;
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
if (connection->useSSL)
{
HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
connection->peek_msg = NULL;
connection->peek_msg_mem = NULL;
pSSL_shutdown(connection->ssl_s);
pSSL_free(connection->ssl_s);
connection->ssl_s = NULL;
connection->useSSL = FALSE;
}
#endif
result = closesocket(connection->socketFD);
connection->socketFD = -1;
if (0 != result)
{
INTERNET_SetLastError(WSAGetLastError());
return FALSE;
}
return TRUE;
}
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
static BOOL check_hostname(X509 *cert, char *hostname)
{
/* FIXME: implement */
return TRUE;
}
#endif
/******************************************************************************
* NETCON_secure_connect
* Initiates a secure connection over an existing plaintext connection.
*/
BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
{
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
long verify_res;
X509 *cert;
int len;
char *hostname_unix;
/* can't connect if we are already connected */
if (connection->useSSL)
{
ERR("already connected\n");
return FALSE;
}
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));
INTERNET_SetLastError(ERROR_OUTOFMEMORY);
return FALSE;
}
connection->ssl_s = pSSL_new(ctx);
if (!connection->ssl_s)
{
ERR("SSL_new failed: %s\n",
pERR_error_string(pERR_get_error(), 0));
INTERNET_SetLastError(ERROR_OUTOFMEMORY);
goto fail;
}
if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
{
ERR("SSL_set_fd failed: %s\n",
pERR_error_string(pERR_get_error(), 0));
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
goto fail;
}
if (pSSL_connect(connection->ssl_s) <= 0)
{
ERR("SSL_connect failed: %s\n",
pERR_error_string(pERR_get_error(), 0));
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
goto fail;
}
cert = pSSL_get_peer_certificate(connection->ssl_s);
if (!cert)
{
ERR("no certificate for server %s\n", debugstr_w(hostname));
/* FIXME: is this the best error? */
INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
goto fail;
}
verify_res = pSSL_get_verify_result(connection->ssl_s);
if (verify_res != X509_V_OK)
{
ERR("couldn't verify the security of the connection, %ld\n", verify_res);
/* FIXME: we should set an error and return, but we only warn at
* the moment */
}
len = WideCharToMultiByte(CP_THREAD_ACP, 0, hostname, -1, NULL, 0, NULL, NULL);
hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
if (!hostname_unix)
{
INTERNET_SetLastError(ERROR_OUTOFMEMORY);
goto fail;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -