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"
|
|
|
|
|
2003-06-27 06:04:00 +02:00
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
2004-10-08 22:48:44 +02:00
|
|
|
#include <sys/types.h>
|
2003-06-21 01:26:56 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
|
|
|
# include <sys/socket.h>
|
|
|
|
#endif
|
2003-06-27 06:04:00 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#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>
|
2003-06-21 01:26:56 +02:00
|
|
|
|
|
|
|
#include "wine/library.h"
|
|
|
|
#include "windef.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "wininet.h"
|
|
|
|
#include "winerror.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
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "internet.h"
|
2006-01-20 20:16:56 +01:00
|
|
|
#include "wincrypt.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
|
|
|
|
*/
|
|
|
|
|
2005-12-01 11:18:43 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
|
|
|
|
|
|
|
#include <openssl/err.h>
|
2003-06-21 01:26:56 +02:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
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);
|
|
|
|
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);
|
2005-12-01 11:18:43 +01:00
|
|
|
MAKE_FUNCPTR(SSL_get_verify_result);
|
|
|
|
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);
|
2006-01-20 20:16:56 +01:00
|
|
|
MAKE_FUNCPTR(i2d_X509);
|
2003-06-21 01:26:56 +02:00
|
|
|
|
|
|
|
/* OpenSSL's libcrypto functions that we use */
|
|
|
|
MAKE_FUNCPTR(BIO_new_fp);
|
2005-12-01 11:18:43 +01:00
|
|
|
MAKE_FUNCPTR(ERR_get_error);
|
|
|
|
MAKE_FUNCPTR(ERR_error_string);
|
2003-06-21 01:26:56 +02:00
|
|
|
#undef MAKE_FUNCPTR
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2006-03-06 18:31:09 +01:00
|
|
|
BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2005-11-28 11:54:31 +01:00
|
|
|
connection->useSSL = FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
connection->socketFD = -1;
|
2005-11-28 11:54:31 +01:00
|
|
|
if (useSSL)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2005-12-01 11:18:43 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
2003-06-21 01:26:56 +02:00
|
|
|
TRACE("using SSL connection\n");
|
2006-03-06 18:31:09 +01:00
|
|
|
if (OpenSSL_ssl_handle) /* already initialized everything */
|
|
|
|
return TRUE;
|
2003-06-21 01:26:56 +02:00
|
|
|
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);
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
|
|
|
|
return FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
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);
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
|
|
|
|
return FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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); \
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
|
|
|
|
return FALSE; \
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DYNSSL(SSL_library_init);
|
|
|
|
DYNSSL(SSL_load_error_strings);
|
|
|
|
DYNSSL(SSLv23_method);
|
|
|
|
DYNSSL(SSL_CTX_new);
|
|
|
|
DYNSSL(SSL_new);
|
2005-12-01 11:18:43 +01:00
|
|
|
DYNSSL(SSL_free);
|
|
|
|
DYNSSL(SSL_set_fd);
|
2003-06-21 01:26:56 +02:00
|
|
|
DYNSSL(SSL_connect);
|
2005-12-01 11:18:43 +01:00
|
|
|
DYNSSL(SSL_shutdown);
|
2003-06-21 01:26:56 +02:00
|
|
|
DYNSSL(SSL_write);
|
|
|
|
DYNSSL(SSL_read);
|
2005-12-01 11:18:43 +01:00
|
|
|
DYNSSL(SSL_get_verify_result);
|
|
|
|
DYNSSL(SSL_get_peer_certificate);
|
2003-06-21 01:26:56 +02:00
|
|
|
DYNSSL(SSL_CTX_get_timeout);
|
2005-12-01 11:18:43 +01:00
|
|
|
DYNSSL(SSL_CTX_set_timeout);
|
|
|
|
DYNSSL(SSL_CTX_set_default_verify_paths);
|
2006-01-20 20:16:56 +01:00
|
|
|
DYNSSL(i2d_X509);
|
2003-06-21 01:26:56 +02:00
|
|
|
#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); \
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
|
|
|
|
return FALSE; \
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
DYNCRYPTO(BIO_new_fp);
|
2005-12-01 11:18:43 +01:00
|
|
|
DYNCRYPTO(ERR_get_error);
|
|
|
|
DYNCRYPTO(ERR_error_string);
|
2003-06-21 01:26:56 +02:00
|
|
|
#undef DYNCRYPTO
|
|
|
|
|
|
|
|
pSSL_library_init();
|
|
|
|
pSSL_load_error_strings();
|
|
|
|
pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
|
|
|
|
|
|
|
|
meth = pSSLv23_method();
|
2005-10-20 13:35:54 +02:00
|
|
|
connection->peek_msg = NULL;
|
|
|
|
connection->peek_msg_mem = NULL;
|
2003-06-21 01:26:56 +02:00
|
|
|
#else
|
|
|
|
FIXME("can't use SSL, not compiled in.\n");
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
|
|
|
|
return FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
#endif
|
|
|
|
}
|
2006-03-06 18:31:09 +01:00
|
|
|
return TRUE;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NETCON_connected(WININET_NETCONNECTION *connection)
|
|
|
|
{
|
2005-11-15 13:01:40 +01:00
|
|
|
if (connection->socketFD == -1)
|
|
|
|
return FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2005-12-03 18:10:14 +01:00
|
|
|
/* translate a unix error code into a winsock one */
|
|
|
|
static int sock_get_error( int err )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-21 01:26:56 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_create
|
2005-11-28 11:54:31 +01:00
|
|
|
* Basically calls 'socket()'
|
2003-06-21 01:26:56 +02:00
|
|
|
*/
|
|
|
|
BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
|
|
|
|
int type, int protocol)
|
|
|
|
{
|
2005-12-01 11:18:43 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
2005-11-15 13:01:40 +01:00
|
|
|
if (connection->useSSL)
|
|
|
|
return FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
#endif
|
2005-11-15 13:01:40 +01:00
|
|
|
|
|
|
|
connection->socketFD = socket(domain, type, protocol);
|
|
|
|
if (connection->socketFD == -1)
|
2005-12-03 18:10:14 +01:00
|
|
|
{
|
|
|
|
INTERNET_SetLastError(sock_get_error(errno));
|
2005-11-15 13:01:40 +01:00
|
|
|
return FALSE;
|
2005-12-03 18:10:14 +01:00
|
|
|
}
|
2005-11-15 13:01:40 +01:00
|
|
|
return TRUE;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_close
|
|
|
|
* Basically calls 'close()' unless we should use SSL
|
|
|
|
*/
|
|
|
|
BOOL NETCON_close(WININET_NETCONNECTION *connection)
|
|
|
|
{
|
2005-11-15 13:01:40 +01:00
|
|
|
int result;
|
|
|
|
|
2003-09-22 23:15:34 +02:00
|
|
|
if (!NETCON_connected(connection)) return FALSE;
|
2005-11-15 13:01:40 +01:00
|
|
|
|
2005-12-01 11:18:43 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
2005-11-15 13:01:40 +01:00
|
|
|
if (connection->useSSL)
|
|
|
|
{
|
2005-10-20 13:35:54 +02:00
|
|
|
HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
|
|
|
|
connection->peek_msg = NULL;
|
|
|
|
connection->peek_msg_mem = NULL;
|
2006-04-26 11:20:00 +02:00
|
|
|
connection->peek_len = 0;
|
2005-12-01 11:18:43 +01:00
|
|
|
|
|
|
|
pSSL_shutdown(connection->ssl_s);
|
|
|
|
pSSL_free(connection->ssl_s);
|
|
|
|
connection->ssl_s = NULL;
|
|
|
|
|
2005-11-28 11:54:31 +01:00
|
|
|
connection->useSSL = FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
2005-11-15 13:01:40 +01:00
|
|
|
#endif
|
|
|
|
|
2005-12-01 11:18:43 +01:00
|
|
|
result = closesocket(connection->socketFD);
|
|
|
|
connection->socketFD = -1;
|
|
|
|
|
2005-11-15 13:01:40 +01:00
|
|
|
if (result == -1)
|
2005-12-03 18:10:14 +01:00
|
|
|
{
|
|
|
|
INTERNET_SetLastError(sock_get_error(errno));
|
2005-11-15 13:01:40 +01:00
|
|
|
return FALSE;
|
2005-12-03 18:10:14 +01:00
|
|
|
}
|
2005-11-15 13:01:40 +01:00
|
|
|
return TRUE;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
2005-12-05 12:01:56 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
2005-12-01 11:18:43 +01:00
|
|
|
static BOOL check_hostname(X509 *cert, char *hostname)
|
|
|
|
{
|
|
|
|
/* FIXME: implement */
|
|
|
|
return TRUE;
|
|
|
|
}
|
2005-12-05 12:01:56 +01:00
|
|
|
#endif
|
2005-11-28 11:54:31 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_secure_connect
|
|
|
|
* Initiates a secure connection over an existing plaintext connection.
|
|
|
|
*/
|
|
|
|
BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
|
|
|
|
{
|
2005-12-01 11:18:43 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
|
|
|
long verify_res;
|
|
|
|
X509 *cert;
|
|
|
|
int len;
|
|
|
|
char *hostname_unix;
|
2005-11-28 11:54:31 +01:00
|
|
|
|
2005-12-01 11:18:43 +01:00
|
|
|
/* can't connect if we are already connected */
|
2005-11-28 11:54:31 +01:00
|
|
|
if (connection->useSSL)
|
2005-12-01 11:18:43 +01:00
|
|
|
{
|
|
|
|
ERR("already connected\n");
|
2005-11-28 11:54:31 +01:00
|
|
|
return FALSE;
|
2005-12-01 11:18:43 +01:00
|
|
|
}
|
2005-11-28 11:54:31 +01:00
|
|
|
|
|
|
|
ctx = pSSL_CTX_new(meth);
|
2005-12-01 11:18:43 +01:00
|
|
|
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));
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_OUTOFMEMORY);
|
2005-12-01 11:18:43 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2005-11-28 11:54:31 +01:00
|
|
|
connection->ssl_s = pSSL_new(ctx);
|
2005-12-01 11:18:43 +01:00
|
|
|
if (!connection->ssl_s)
|
|
|
|
{
|
|
|
|
ERR("SSL_new failed: %s\n",
|
|
|
|
pERR_error_string(pERR_get_error(), 0));
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_OUTOFMEMORY);
|
2005-12-01 11:18:43 +01:00
|
|
|
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));
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
|
2005-12-01 11:18:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2005-11-28 11:54:31 +01:00
|
|
|
|
|
|
|
if (pSSL_connect(connection->ssl_s) <= 0)
|
|
|
|
{
|
2005-12-01 11:18:43 +01:00
|
|
|
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_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
|
|
|
|
hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
|
|
|
|
if (!hostname_unix)
|
|
|
|
{
|
2006-03-06 18:31:09 +01:00
|
|
|
INTERNET_SetLastError(ERROR_OUTOFMEMORY);
|
2005-12-01 11:18:43 +01:00
|
|
|
goto fail;
|
2005-11-28 11:54:31 +01:00
|
|
|
}
|
2005-12-01 11:18:43 +01:00
|
|
|
WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
|
|
|
|
|
|
|
|
if (!check_hostname(cert, hostname_unix))
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, hostname_unix);
|
|
|
|
INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, hostname_unix);
|
2005-11-28 11:54:31 +01:00
|
|
|
connection->useSSL = TRUE;
|
|
|
|
return TRUE;
|
2005-12-01 11:18:43 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
if (connection->ssl_s)
|
|
|
|
{
|
|
|
|
pSSL_shutdown(connection->ssl_s);
|
|
|
|
pSSL_free(connection->ssl_s);
|
|
|
|
connection->ssl_s = NULL;
|
|
|
|
}
|
2005-11-28 11:54:31 +01:00
|
|
|
#endif
|
2005-12-01 11:18:43 +01:00
|
|
|
return FALSE;
|
2005-11-28 11:54:31 +01:00
|
|
|
}
|
|
|
|
|
2003-06-21 01:26:56 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_connect
|
2005-11-28 11:54:31 +01:00
|
|
|
* Connects to the specified address.
|
2003-06-21 01:26:56 +02:00
|
|
|
*/
|
|
|
|
BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
|
2004-09-16 22:34:27 +02:00
|
|
|
unsigned int addrlen)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2005-11-15 13:01:40 +01:00
|
|
|
int result;
|
|
|
|
|
2003-09-22 23:15:34 +02:00
|
|
|
if (!NETCON_connected(connection)) return FALSE;
|
2005-11-15 13:01:40 +01:00
|
|
|
|
|
|
|
result = connect(connection->socketFD, serv_addr, addrlen);
|
|
|
|
if (result == -1)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2005-12-03 18:10:14 +01:00
|
|
|
WARN("Unable to connect to host (%s)\n", strerror(errno));
|
|
|
|
INTERNET_SetLastError(sock_get_error(errno));
|
|
|
|
|
2005-11-15 13:01:40 +01:00
|
|
|
closesocket(connection->socketFD);
|
|
|
|
connection->socketFD = -1;
|
|
|
|
return FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
2005-11-15 13:01:40 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
|
|
|
|
int *sent /* out */)
|
|
|
|
{
|
2003-09-22 23:15:34 +02:00
|
|
|
if (!NETCON_connected(connection)) return FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
if (!connection->useSSL)
|
|
|
|
{
|
|
|
|
*sent = send(connection->socketFD, msg, len, flags);
|
|
|
|
if (*sent == -1)
|
2005-12-03 18:10:14 +01:00
|
|
|
{
|
|
|
|
INTERNET_SetLastError(sock_get_error(errno));
|
2003-06-21 01:26:56 +02:00
|
|
|
return FALSE;
|
2005-12-03 18:10:14 +01:00
|
|
|
}
|
2003-06-21 01:26:56 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-01 11:18:43 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
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)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#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
|
|
|
*/
|
|
|
|
BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
|
|
|
|
int *recvd /* out */)
|
|
|
|
{
|
2003-09-22 23:15:34 +02:00
|
|
|
if (!NETCON_connected(connection)) return FALSE;
|
2003-06-21 01:26:56 +02:00
|
|
|
if (!connection->useSSL)
|
|
|
|
{
|
|
|
|
*recvd = recv(connection->socketFD, buf, len, flags);
|
|
|
|
if (*recvd == -1)
|
2005-12-03 18:10:14 +01:00
|
|
|
{
|
|
|
|
INTERNET_SetLastError(sock_get_error(errno));
|
2003-06-21 01:26:56 +02:00
|
|
|
return FALSE;
|
2005-12-03 18:10:14 +01:00
|
|
|
}
|
2003-06-21 01:26:56 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-01 11:18:43 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
2006-08-08 22:37:41 +02:00
|
|
|
if (flags & ~(MSG_PEEK|MSG_WAITALL))
|
2003-06-21 01:26:56 +02:00
|
|
|
FIXME("SSL_read does not support the following flag: %08x\n", flags);
|
|
|
|
|
|
|
|
/* this ugly hack is all for MSG_PEEK. eww gross */
|
2005-10-20 13:35:54 +02:00
|
|
|
if (flags & MSG_PEEK && !connection->peek_msg)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2005-10-20 13:35:54 +02:00
|
|
|
connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
2005-10-20 13:35:54 +02:00
|
|
|
else if (flags & MSG_PEEK && connection->peek_msg)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2006-04-26 11:20:00 +02:00
|
|
|
if (len < connection->peek_len)
|
2003-06-21 01:26:56 +02:00
|
|
|
FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
|
2006-04-26 11:20:00 +02:00
|
|
|
*recvd = min(len, connection->peek_len);
|
|
|
|
memcpy(buf, connection->peek_msg, *recvd);
|
2003-06-21 01:26:56 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
2005-10-20 13:35:54 +02:00
|
|
|
else if (connection->peek_msg)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2006-04-26 11:20:00 +02:00
|
|
|
*recvd = min(len, connection->peek_len);
|
|
|
|
memcpy(buf, connection->peek_msg, *recvd);
|
|
|
|
connection->peek_len -= *recvd;
|
|
|
|
connection->peek_msg += *recvd;
|
|
|
|
if (connection->peek_len == 0)
|
2003-06-21 01:26:56 +02:00
|
|
|
{
|
2005-10-20 13:35:54 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
|
|
|
|
connection->peek_msg_mem = NULL;
|
|
|
|
connection->peek_msg = NULL;
|
2006-08-08 22:37:41 +02:00
|
|
|
/* check if the peek buffer held too few data */
|
|
|
|
if ((flags & MSG_WAITALL) && (*recvd < len))
|
|
|
|
{
|
|
|
|
int recv2 = 0;
|
|
|
|
/* recursive call - but now the peek buffer is empty */
|
|
|
|
if (!NETCON_recv(connection, (char*)buf + *recvd, len - *recvd, flags, &recv2))
|
|
|
|
return FALSE;
|
|
|
|
*recvd += recv2;
|
|
|
|
}
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
*recvd = pSSL_read(connection->ssl_s, buf, len);
|
|
|
|
if (flags & MSG_PEEK) /* must copy stuff into buffer */
|
|
|
|
{
|
2006-04-26 11:20:00 +02:00
|
|
|
connection->peek_len = *recvd;
|
2003-06-21 01:26:56 +02:00
|
|
|
if (!*recvd)
|
|
|
|
{
|
2005-10-20 13:35:54 +02:00
|
|
|
HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
|
|
|
|
connection->peek_msg_mem = NULL;
|
|
|
|
connection->peek_msg = NULL;
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
else
|
2005-10-20 13:35:54 +02:00
|
|
|
memcpy(connection->peek_msg, buf, *recvd);
|
2003-06-21 01:26:56 +02:00
|
|
|
}
|
|
|
|
if (*recvd < 1 && len)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* NETCON_getNextLine
|
|
|
|
*/
|
|
|
|
BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE("\n");
|
|
|
|
|
|
|
|
if (!NETCON_connected(connection)) return FALSE;
|
|
|
|
|
|
|
|
if (!connection->useSSL)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set infd;
|
|
|
|
BOOL bSuccess = FALSE;
|
2004-08-09 20:54:23 +02:00
|
|
|
DWORD nRecv = 0;
|
2003-06-21 01:26:56 +02:00
|
|
|
|
|
|
|
FD_ZERO(&infd);
|
|
|
|
FD_SET(connection->socketFD, &infd);
|
|
|
|
tv.tv_sec=RESPONSE_TIMEOUT;
|
|
|
|
tv.tv_usec=0;
|
|
|
|
|
|
|
|
while (nRecv < *dwBuffer)
|
|
|
|
{
|
|
|
|
if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
|
|
|
|
{
|
|
|
|
if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
|
|
|
|
{
|
2005-12-03 18:10:14 +01:00
|
|
|
INTERNET_SetLastError(sock_get_error(errno));
|
2003-06-21 01:26:56 +02:00
|
|
|
goto lend;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpszBuffer[nRecv] == '\n')
|
|
|
|
{
|
|
|
|
bSuccess = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lpszBuffer[nRecv] != '\r')
|
|
|
|
nRecv++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
|
|
|
|
goto lend;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lend: /* FIXME: don't use labels */
|
|
|
|
if (bSuccess)
|
|
|
|
{
|
2004-03-30 06:36:09 +02:00
|
|
|
lpszBuffer[nRecv++] = '\0';
|
|
|
|
*dwBuffer = nRecv;
|
2004-08-09 20:54:23 +02:00
|
|
|
TRACE(":%lu %s\n", nRecv, lpszBuffer);
|
2003-06-21 01:26:56 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-01 11:18:43 +01:00
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
2003-06-21 01:26:56 +02:00
|
|
|
long prev_timeout;
|
2004-08-09 20:54:23 +02:00
|
|
|
DWORD nRecv = 0;
|
2003-06-21 01:26:56 +02:00
|
|
|
BOOL success = TRUE;
|
|
|
|
|
|
|
|
prev_timeout = pSSL_CTX_get_timeout(ctx);
|
|
|
|
pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
|
|
|
|
|
|
|
|
while (nRecv < *dwBuffer)
|
|
|
|
{
|
|
|
|
int recv = 1;
|
|
|
|
if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
|
|
|
|
{
|
|
|
|
INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
|
|
|
|
success = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpszBuffer[nRecv] == '\n')
|
|
|
|
{
|
|
|
|
success = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (lpszBuffer[nRecv] != '\r')
|
|
|
|
nRecv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
pSSL_CTX_set_timeout(ctx, prev_timeout);
|
|
|
|
if (success)
|
|
|
|
{
|
2004-03-30 06:36:09 +02:00
|
|
|
lpszBuffer[nRecv++] = '\0';
|
|
|
|
*dwBuffer = nRecv;
|
2004-08-09 20:54:23 +02:00
|
|
|
TRACE("_SSL:%lu %s\n", nRecv, lpszBuffer);
|
2003-06-21 01:26:56 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
#else
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2006-01-20 20:16:56 +01:00
|
|
|
|
|
|
|
|
|
|
|
LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
|
|
|
|
{
|
|
|
|
|
|
|
|
#if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
|
|
|
|
X509* cert;
|
|
|
|
unsigned char* buffer,*p;
|
|
|
|
INT len;
|
|
|
|
BOOL malloced = FALSE;
|
|
|
|
LPCVOID r = NULL;
|
|
|
|
|
|
|
|
if (!connection->useSSL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
cert = pSSL_get_peer_certificate(connection->ssl_s);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
buffer = HeapAlloc(GetProcessHeap(),0,len);
|
|
|
|
p = buffer;
|
|
|
|
len = pi2d_X509(cert,&p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer = p;
|
|
|
|
malloced = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
|
|
|
|
|
|
|
|
if (malloced)
|
|
|
|
free(buffer);
|
|
|
|
else
|
|
|
|
HeapFree(GetProcessHeap(),0,buffer);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
2006-03-14 15:36:11 +01:00
|
|
|
|
|
|
|
BOOL NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
/* FIXME: we should probably store the timeout in the connection to set
|
|
|
|
* when we do connect */
|
|
|
|
if (!NETCON_connected(connection))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* 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,
|
|
|
|
send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
|
|
|
|
sizeof(tv));
|
|
|
|
|
|
|
|
if (result == -1)
|
|
|
|
{
|
|
|
|
WARN("setsockopt failed (%s)\n", strerror(errno));
|
|
|
|
INTERNET_SetLastError(sock_get_error(errno));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|