[WINHTTP] Sync with Wine Staging 3.9. CORE-14656
[reactos.git] / dll / win32 / winhttp / net.c
index 458312c..e9ff86a 100644 (file)
@@ -1,5 +1,6 @@
 /*
  * Copyright 2008 Hans Leidekker for CodeWeavers
+ * Copyright 2013 Jacek Caban for CodeWeavers
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-#define WIN32_NO_STATUS
-#define _INC_WINDOWS
-#define COM_NO_WINDOWS_H
+#include "config.h"
+#include "wine/port.h"
 
-#include <config.h>
-//#include "wine/port.h"
+#include <stdarg.h>
+#include <stdio.h>
+#include <errno.h>
+#include <assert.h>
 
-//#include <stdarg.h>
-//#include <stdio.h>
-//#include <errno.h>
-
-//#include <sys/types.h>
+#include <sys/types.h>
 #ifdef HAVE_SYS_SOCKET_H
 # include <sys/socket.h>
 #endif
 #ifdef HAVE_POLL_H
 # include <poll.h>
 #endif
-#ifdef HAVE_OPENSSL_SSL_H
-# include <openssl/ssl.h>
-# include <openssl/opensslv.h>
-#undef FAR
-#undef DSA
-#endif
 
 #define NONAMELESSUNION
 
-#include <wine/debug.h>
-#include <wine/library.h>
+#include "wine/debug.h"
+#include "wine/library.h"
 
-//#include "windef.h"
-//#include "winbase.h"
-#include <winhttp.h>
-//#include "wincrypt.h"
+#include "windef.h"
+#include "winbase.h"
+#include "winhttp.h"
+#include "wincrypt.h"
+#include "schannel.h"
 
 #include "winhttp_private.h"
 
 /* to avoid conflicts with the Unix socket headers */
 #define USE_WS_PREFIX
-//#include "winsock2.h"
+#include "winsock2.h"
 
 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
 
@@ -79,90 +72,6 @@ static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
 
 #endif
 
-#ifdef SONAME_LIBSSL
-
-#include <openssl/err.h>
-
-static CRITICAL_SECTION init_ssl_cs;
-static CRITICAL_SECTION_DEBUG init_ssl_cs_debug =
-{
-    0, 0, &init_ssl_cs,
-    { &init_ssl_cs_debug.ProcessLocksList,
-      &init_ssl_cs_debug.ProcessLocksList },
-    0, 0, { (DWORD_PTR)(__FILE__ ": init_ssl_cs") }
-};
-static CRITICAL_SECTION init_ssl_cs = { &init_ssl_cs_debug, -1, 0, 0, 0, 0 };
-
-static void *libssl_handle;
-static void *libcrypto_handle;
-
-#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER > 0x10000000)
-static const SSL_METHOD *method;
-#else
-static SSL_METHOD *method;
-#endif
-static SSL_CTX *ctx;
-static int hostname_idx;
-static int error_idx;
-static int conn_idx;
-
-#define MAKE_FUNCPTR(f) static typeof(f) * p##f
-
-MAKE_FUNCPTR( SSL_library_init );
-MAKE_FUNCPTR( SSL_load_error_strings );
-MAKE_FUNCPTR( SSLv23_method );
-MAKE_FUNCPTR( SSL_CTX_free );
-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_pending );
-MAKE_FUNCPTR( SSL_get_error );
-MAKE_FUNCPTR( SSL_get_ex_new_index );
-MAKE_FUNCPTR( SSL_get_ex_data );
-MAKE_FUNCPTR( SSL_set_ex_data );
-MAKE_FUNCPTR( SSL_get_ex_data_X509_STORE_CTX_idx );
-MAKE_FUNCPTR( SSL_get_peer_certificate );
-MAKE_FUNCPTR( SSL_CTX_set_default_verify_paths );
-MAKE_FUNCPTR( SSL_CTX_set_verify );
-MAKE_FUNCPTR( SSL_get_current_cipher );
-MAKE_FUNCPTR( SSL_CIPHER_get_bits );
-
-MAKE_FUNCPTR( CRYPTO_num_locks );
-MAKE_FUNCPTR( CRYPTO_set_id_callback );
-MAKE_FUNCPTR( CRYPTO_set_locking_callback );
-MAKE_FUNCPTR( ERR_free_strings );
-MAKE_FUNCPTR( ERR_get_error );
-MAKE_FUNCPTR( ERR_error_string );
-MAKE_FUNCPTR( X509_STORE_CTX_get_ex_data );
-MAKE_FUNCPTR( X509_STORE_CTX_get_chain );
-MAKE_FUNCPTR( i2d_X509 );
-MAKE_FUNCPTR( sk_value );
-MAKE_FUNCPTR( sk_num );
-#undef MAKE_FUNCPTR
-
-static CRITICAL_SECTION *ssl_locks;
-static unsigned int num_ssl_locks;
-
-static unsigned long ssl_thread_id(void)
-{
-    return GetCurrentThreadId();
-}
-
-static void ssl_lock_callback(int mode, int type, const char *file, int line)
-{
-    if (mode & CRYPTO_LOCK)
-        EnterCriticalSection( &ssl_locks[type] );
-    else
-        LeaveCriticalSection( &ssl_locks[type] );
-}
-
-#endif
-
 /* translate a unix error code into a winsock error code */
 #ifndef __REACTOS__
 static int sock_get_error( int err )
@@ -240,45 +149,31 @@ static inline int unix_ioctl(int filedes, long request, void *arg)
 #define ioctlsocket unix_ioctl
 #endif
 
-#ifdef SONAME_LIBSSL
-static PCCERT_CONTEXT X509_to_cert_context(X509 *cert)
+static int sock_send(int fd, const void *msg, size_t len, int flags)
 {
-    unsigned char *buffer, *p;
-    int len;
-    BOOL malloc = FALSE;
-    PCCERT_CONTEXT ret;
-
-    p = NULL;
-    if ((len = pi2d_X509( cert, &p )) < 0) return NULL;
-    /*
-     * 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)
+    int ret;
+    do
     {
-        if (!(buffer = heap_alloc( len ))) return NULL;
-        p = buffer;
-        len = pi2d_X509( cert, &p );
+        if ((ret = send(fd, msg, len, flags)) == -1) WARN("send error %s\n", strerror(errno));
     }
-    else
+    while(ret == -1 && errno == EINTR);
+    return ret;
+}
+
+static int sock_recv(int fd, void *msg, size_t len, int flags)
+{
+    int ret;
+    do
     {
-        buffer = p;
-        malloc = TRUE;
+        if ((ret = recv(fd, msg, len, flags)) == -1) WARN("recv error %s\n", strerror(errno));
     }
-
-    ret = CertCreateCertificateContext( X509_ASN_ENCODING, buffer, len );
-
-    if (malloc) free( buffer );
-    else heap_free( buffer );
-
+    while(ret == -1 && errno == EINTR);
     return ret;
 }
 
-static DWORD netconn_verify_cert( PCCERT_CONTEXT cert, HCERTSTORE store,
-                                  WCHAR *server, DWORD security_flags )
+static DWORD netconn_verify_cert( PCCERT_CONTEXT cert, WCHAR *server, DWORD security_flags )
 {
+    HCERTSTORE store = cert->hCertStore;
     BOOL ret;
     CERT_CHAIN_PARA chainPara = { sizeof(chainPara), { 0 } };
     PCCERT_CHAIN_CONTEXT chain;
@@ -369,317 +264,129 @@ static DWORD netconn_verify_cert( PCCERT_CONTEXT cert, HCERTSTORE store,
     return err;
 }
 
-static int netconn_secure_verify( int preverify_ok, X509_STORE_CTX *ctx )
+#ifdef __REACTOS__
+static BOOL winsock_initialized = FALSE;
+BOOL netconn_init_winsock()
 {
-    SSL *ssl;
-    WCHAR *server;
-    BOOL ret = FALSE;
-    netconn_t *conn;
-    HCERTSTORE store = CertOpenStore( CERT_STORE_PROV_MEMORY, 0, 0,
-     CERT_STORE_CREATE_NEW_FLAG, NULL );
-
-    ssl = pX509_STORE_CTX_get_ex_data( ctx, pSSL_get_ex_data_X509_STORE_CTX_idx() );
-    server = pSSL_get_ex_data( ssl, hostname_idx );
-    conn = pSSL_get_ex_data( ssl, conn_idx );
-    if (store)
+    WSADATA wsaData;
+    int error;
+    if (!winsock_initialized)
     {
-        X509 *cert;
-        int i;
-        PCCERT_CONTEXT endCert = NULL;
-        struct stack_st *chain = (struct stack_st *)pX509_STORE_CTX_get_chain( ctx );
-
-        ret = TRUE;
-        for (i = 0; ret && i < psk_num(chain); i++)
+        error = WSAStartup(MAKEWORD(1, 1), &wsaData);
+        if (error)
         {
-            PCCERT_CONTEXT context;
-
-            cert = (X509 *)psk_value(chain, i);
-            if ((context = X509_to_cert_context( cert )))
-            {
-                if (i == 0)
-                    ret = CertAddCertificateContextToStore( store, context,
-                        CERT_STORE_ADD_ALWAYS, &endCert );
-                else
-                    ret = CertAddCertificateContextToStore( store, context,
-                        CERT_STORE_ADD_ALWAYS, NULL );
-                CertFreeCertificateContext( context );
-            }
-        }
-        if (!endCert) ret = FALSE;
-        if (ret)
-        {
-            DWORD_PTR err = netconn_verify_cert( endCert, store, server,
-                                                 conn->security_flags );
-
-            if (err)
-            {
-                pSSL_set_ex_data( ssl, error_idx, (void *)err );
-                ret = FALSE;
-            }
+            ERR("WSAStartup failed: %d\n", error);
+            return FALSE;
         }
-        CertFreeCertificateContext( endCert );
-        CertCloseStore( store, 0 );
+        else
+            winsock_initialized = TRUE;
     }
-    return ret;
+    return winsock_initialized;
 }
-#endif
 
-BOOL netconn_init( netconn_t *conn, BOOL secure )
-{
-#if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
-    int i;
 #endif
 
-    conn->socket = -1;
-    if (!secure) return TRUE;
-
-#if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
-    EnterCriticalSection( &init_ssl_cs );
-    if (libssl_handle)
-    {
-        LeaveCriticalSection( &init_ssl_cs );
-        return TRUE;
-    }
-    if (!(libssl_handle = wine_dlopen( SONAME_LIBSSL, RTLD_NOW, NULL, 0 )))
-    {
-        ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
-        set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
-        LeaveCriticalSection( &init_ssl_cs );
-        return FALSE;
-    }
-    if (!(libcrypto_handle = wine_dlopen( SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0 )))
-    {
-        ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
-        set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
-        LeaveCriticalSection( &init_ssl_cs );
-        return FALSE;
-    }
-#define LOAD_FUNCPTR(x) \
-    if (!(p##x = wine_dlsym( libssl_handle, #x, NULL, 0 ))) \
-    { \
-        ERR("Failed to load symbol %s\n", #x); \
-        set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
-        LeaveCriticalSection( &init_ssl_cs ); \
-        return FALSE; \
-    }
-    LOAD_FUNCPTR( SSL_library_init );
-    LOAD_FUNCPTR( SSL_load_error_strings );
-    LOAD_FUNCPTR( SSLv23_method );
-    LOAD_FUNCPTR( SSL_CTX_free );
-    LOAD_FUNCPTR( SSL_CTX_new );
-    LOAD_FUNCPTR( SSL_new );
-    LOAD_FUNCPTR( SSL_free );
-    LOAD_FUNCPTR( SSL_set_fd );
-    LOAD_FUNCPTR( SSL_connect );
-    LOAD_FUNCPTR( SSL_shutdown );
-    LOAD_FUNCPTR( SSL_write );
-    LOAD_FUNCPTR( SSL_read );
-    LOAD_FUNCPTR( SSL_pending );
-    LOAD_FUNCPTR( SSL_get_error );
-    LOAD_FUNCPTR( SSL_get_ex_new_index );
-    LOAD_FUNCPTR( SSL_get_ex_data );
-    LOAD_FUNCPTR( SSL_set_ex_data );
-    LOAD_FUNCPTR( SSL_get_ex_data_X509_STORE_CTX_idx );
-    LOAD_FUNCPTR( SSL_get_peer_certificate );
-    LOAD_FUNCPTR( SSL_CTX_set_default_verify_paths );
-    LOAD_FUNCPTR( SSL_CTX_set_verify );
-    LOAD_FUNCPTR( SSL_get_current_cipher );
-    LOAD_FUNCPTR( SSL_CIPHER_get_bits );
-#undef LOAD_FUNCPTR
-
-#define LOAD_FUNCPTR(x) \
-    if (!(p##x = wine_dlsym( libcrypto_handle, #x, NULL, 0 ))) \
-    { \
-        ERR("Failed to load symbol %s\n", #x); \
-        set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
-        LeaveCriticalSection( &init_ssl_cs ); \
-        return FALSE; \
-    }
-    LOAD_FUNCPTR( CRYPTO_num_locks );
-    LOAD_FUNCPTR( CRYPTO_set_id_callback );
-    LOAD_FUNCPTR( CRYPTO_set_locking_callback );
-    LOAD_FUNCPTR( ERR_free_strings );
-    LOAD_FUNCPTR( ERR_get_error );
-    LOAD_FUNCPTR( ERR_error_string );
-    LOAD_FUNCPTR( X509_STORE_CTX_get_ex_data );
-    LOAD_FUNCPTR( X509_STORE_CTX_get_chain );
-    LOAD_FUNCPTR( i2d_X509 );
-    LOAD_FUNCPTR( sk_value );
-    LOAD_FUNCPTR( sk_num );
-#undef LOAD_FUNCPTR
-
-    pSSL_library_init();
-    pSSL_load_error_strings();
-
-    method = pSSLv23_method();
-    ctx = pSSL_CTX_new( method );
-    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 ));
-        set_last_error( ERROR_OUTOFMEMORY );
-        LeaveCriticalSection( &init_ssl_cs );
-        return FALSE;
-    }
-    hostname_idx = pSSL_get_ex_new_index( 0, (void *)"hostname index", NULL, NULL, NULL );
-    if (hostname_idx == -1)
-    {
-        ERR("SSL_get_ex_new_index failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
-        set_last_error( ERROR_OUTOFMEMORY );
-        LeaveCriticalSection( &init_ssl_cs );
-        return FALSE;
-    }
-    error_idx = pSSL_get_ex_new_index( 0, (void *)"error index", NULL, NULL, NULL );
-    if (error_idx == -1)
-    {
-        ERR("SSL_get_ex_new_index failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
-        set_last_error( ERROR_OUTOFMEMORY );
-        LeaveCriticalSection( &init_ssl_cs );
-        return FALSE;
-    }
-    conn_idx = pSSL_get_ex_new_index( 0, (void *)"netconn index", NULL, NULL, NULL );
-    if (conn_idx == -1)
-    {
-        ERR("SSL_get_ex_new_index failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
-        set_last_error( ERROR_OUTOFMEMORY );
-        LeaveCriticalSection( &init_ssl_cs );
-        return FALSE;
-    }
-    pSSL_CTX_set_verify( ctx, SSL_VERIFY_PEER, netconn_secure_verify );
-
-    pCRYPTO_set_id_callback(ssl_thread_id);
-    num_ssl_locks = pCRYPTO_num_locks();
-    ssl_locks = heap_alloc(num_ssl_locks * sizeof(CRITICAL_SECTION));
-    if (!ssl_locks)
-    {
-        set_last_error( ERROR_OUTOFMEMORY );
-        LeaveCriticalSection( &init_ssl_cs );
-        return FALSE;
-    }
-    for (i = 0; i < num_ssl_locks; i++)
-    {
-        InitializeCriticalSection( &ssl_locks[i] );
-        ssl_locks[i].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ssl_locks");
-    }
-    pCRYPTO_set_locking_callback(ssl_lock_callback);
-
-    LeaveCriticalSection( &init_ssl_cs );
-#else
-    WARN("SSL support not compiled in.\n");
-    set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
-    return FALSE;
-#endif
-    return TRUE;
-}
-
 void netconn_unload( void )
 {
-#if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
-    if (libcrypto_handle)
-    {
-        pERR_free_strings();
-        wine_dlclose( libcrypto_handle, NULL, 0 );
-    }
-    if (libssl_handle)
-    {
-        if (ctx)
-            pSSL_CTX_free( ctx );
-        wine_dlclose( libssl_handle, NULL, 0 );
-    }
-    if (ssl_locks)
-    {
-        int i;
-        for (i = 0; i < num_ssl_locks; i++)
-        {
-            ssl_locks[i].DebugInfo->Spare[0] = 0;
-            DeleteCriticalSection( &ssl_locks[i] );
-        }
-        heap_free( ssl_locks );
-    }
-    DeleteCriticalSection(&init_ssl_cs);
-#endif
 #ifndef HAVE_GETADDRINFO
     DeleteCriticalSection(&cs_gethostbyname);
 #endif
+#ifdef __REACTOS__
+    if(winsock_initialized)
+        WSACleanup();
+#endif
 }
 
-BOOL netconn_connected( netconn_t *conn )
+netconn_t *netconn_create( hostdata_t *host, const struct sockaddr_storage *sockaddr, int timeout )
 {
-    return (conn->socket != -1);
-}
+    netconn_t *conn;
+    unsigned int addr_len;
+    BOOL ret = FALSE;
+    int res;
+    ULONG state;
 
-BOOL netconn_create( netconn_t *conn, int domain, int type, int protocol )
-{
-    if ((conn->socket = socket( domain, type, protocol )) == -1)
+    conn = heap_alloc_zero(sizeof(*conn));
+    if (!conn) return NULL;
+    conn->host = host;
+    conn->sockaddr = *sockaddr;
+    if ((conn->socket = socket( sockaddr->ss_family, SOCK_STREAM, 0 )) == -1)
     {
         WARN("unable to create socket (%s)\n", strerror(errno));
         set_last_error( sock_get_error( errno ) );
-        return FALSE;
+        heap_free(conn);
+        return NULL;
     }
-    return TRUE;
-}
-
-BOOL netconn_close( netconn_t *conn )
-{
-    int res;
-
-#ifdef SONAME_LIBSSL
-    if (conn->secure)
-    {
-        heap_free( conn->peek_msg_mem );
-        conn->peek_msg_mem = NULL;
-        conn->peek_msg = NULL;
-        conn->peek_len = 0;
-
-        pSSL_shutdown( conn->ssl_conn );
-        pSSL_free( conn->ssl_conn );
 
-        conn->ssl_conn = NULL;
-        conn->secure = FALSE;
-    }
-#endif
-    res = closesocket( conn->socket );
-    conn->socket = -1;
-    if (res == -1)
+    switch (conn->sockaddr.ss_family)
     {
-        set_last_error( sock_get_error( errno ) );
-        return FALSE;
+    case AF_INET:
+        addr_len = sizeof(struct sockaddr_in);
+        break;
+    case AF_INET6:
+        addr_len = sizeof(struct sockaddr_in6);
+        break;
+    default:
+        assert(0);
     }
-    return TRUE;
-}
-
-BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned int addr_len, int timeout )
-{
-    BOOL ret = FALSE;
-    int res = 0, state;
 
     if (timeout > 0)
     {
         state = 1;
         ioctlsocket( conn->socket, FIONBIO, &state );
     }
-    if (connect( conn->socket, sockaddr, addr_len ) < 0)
+
+    for (;;)
     {
-        res = sock_get_error( errno );
-        if (res == WSAEWOULDBLOCK || res == WSAEINPROGRESS)
+        res = 0;
+        if (connect( conn->socket, (const struct sockaddr *)&conn->sockaddr, addr_len ) < 0)
         {
-            // ReactOS: use select instead of poll
-            fd_set outfd;
-            struct timeval tv;
+            res = sock_get_error( errno );
+            if (res == WSAEWOULDBLOCK || res == WSAEINPROGRESS)
+            {
+#ifdef __REACTOS__
+                /* ReactOS: use select instead of poll */
+                fd_set outfd;
+                struct timeval tv;
 
-            FD_ZERO(&outfd);
-            FD_SET(conn->socket, &outfd);
+                FD_ZERO(&outfd);
+                FD_SET(conn->socket, &outfd);
 
-            tv.tv_sec = 0;
-            tv.tv_usec = timeout * 1000;
+                tv.tv_sec = 0;
+                tv.tv_usec = timeout * 1000;
+                for (;;)
+                {
+                    res = 0;
 
-            if (select( 0, NULL, &outfd, NULL, &tv ) > 0)
-                ret = TRUE;
-            else
-                res = sock_get_error( errno );
+                    if (select( 0, NULL, &outfd, NULL, &tv ) > 0)
+#else
+                struct pollfd pfd;
+
+                pfd.fd = conn->socket;
+                pfd.events = POLLOUT;
+                for (;;)
+                {
+                    res = 0;
+                    if (poll( &pfd, 1, timeout ) > 0)
+#endif
+                    {
+                        ret = TRUE;
+                        break;
+                    }
+                    else
+                    {
+                        res = sock_get_error( errno );
+                        if (res != WSAEINTR) break;
+                    }
+                }
+            }
+            if (res != WSAEINTR) break;
+        }
+        else
+        {
+            ret = TRUE;
+            break;
         }
     }
-    else
-        ret = TRUE;
     if (timeout > 0)
     {
         state = 0;
@@ -689,155 +396,215 @@ BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned
     {
         WARN("unable to connect to host (%d)\n", res);
         set_last_error( res );
+        netconn_close( conn );
+        return NULL;
     }
-    return ret;
+    return conn;
 }
 
-BOOL netconn_secure_connect( netconn_t *conn, WCHAR *hostname )
+BOOL netconn_close( netconn_t *conn )
 {
-#ifdef SONAME_LIBSSL
-    if (!(conn->ssl_conn = pSSL_new( ctx )))
-    {
-        ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
-        set_last_error( ERROR_OUTOFMEMORY );
-        goto fail;
-    }
-    if (!pSSL_set_ex_data( conn->ssl_conn, hostname_idx, hostname ))
+    int res;
+
+    if (conn->secure)
     {
-        ERR("SSL_set_ex_data failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
-        set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
-        goto fail;
+        heap_free( conn->peek_msg_mem );
+        heap_free(conn->ssl_buf);
+        heap_free(conn->extra_buf);
+        DeleteSecurityContext(&conn->ssl_ctx);
     }
-    if (!pSSL_set_ex_data( conn->ssl_conn, conn_idx, conn ))
+    res = closesocket( conn->socket );
+    release_host( conn->host );
+    heap_free(conn);
+    if (res == -1)
     {
-        ERR("SSL_set_ex_data failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
-        set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
+        set_last_error( sock_get_error( errno ) );
         return FALSE;
     }
-    if (!pSSL_set_fd( conn->ssl_conn, conn->socket ))
-    {
-        ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
-        set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
-        goto fail;
+    return TRUE;
+}
+
+BOOL netconn_secure_connect( netconn_t *conn, WCHAR *hostname, DWORD security_flags, CredHandle *cred_handle )
+{
+    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};
+    BYTE *read_buf;
+    SIZE_T read_buf_size = 2048;
+    ULONG attrs = 0;
+    CtxtHandle ctx;
+    SSIZE_T size;
+    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;
+
+    read_buf = heap_alloc(read_buf_size);
+    if(!read_buf)
+        return FALSE;
+
+    status = InitializeSecurityContextW(cred_handle, NULL, hostname, 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 = sock_send(conn->socket, out_buf.pvBuffer, out_buf.cbBuffer, 0);
+            if(size != out_buf.cbBuffer) {
+                ERR("send failed\n");
+                res = ERROR_WINHTTP_SECURE_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 = sock_recv(conn->socket, read_buf+in_bufs[0].cbBuffer, read_buf_size-in_bufs[0].cbBuffer, 0);
+        if(size < 1) {
+            status = ERROR_WINHTTP_SECURE_CHANNEL_ERROR;
+            break;
+        }
+
+        TRACE("recv %lu bytes\n", size);
+
+        in_bufs[0].cbBuffer += size;
+        in_bufs[0].pvBuffer = read_buf;
+        status = InitializeSecurityContextW(cred_handle, &ctx, hostname,  isc_req_flags, 0, 0, &in_desc,
+                0, NULL, &out_desc, &attrs, NULL);
+        TRACE("InitializeSecurityContext ret %08x\n", status);
+
+        if(status == SEC_E_OK) {
+            if(in_bufs[1].BufferType == SECBUFFER_EXTRA)
+                FIXME("SECBUFFER_EXTRA not supported\n");
+
+            status = QueryContextAttributesW(&ctx, SECPKG_ATTR_STREAM_SIZES, &conn->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(cert, hostname, security_flags);
+                CertFreeCertificateContext(cert);
+                if(res != ERROR_SUCCESS) {
+                    WARN("cert verify failed: %u\n", res);
+                    break;
+                }
+            }else {
+                WARN("Could not get cert\n");
+                break;
+            }
+
+            conn->ssl_buf = heap_alloc(conn->ssl_sizes.cbHeader + conn->ssl_sizes.cbMaximumMessage + conn->ssl_sizes.cbTrailer);
+            if(!conn->ssl_buf) {
+                res = GetLastError();
+                break;
+            }
+        }
     }
-    if (pSSL_connect( conn->ssl_conn ) <= 0)
-    {
-        DWORD err;
 
-        err = (DWORD_PTR)pSSL_get_ex_data( conn->ssl_conn, error_idx );
-        if (!err) err = ERROR_WINHTTP_SECURE_CHANNEL_ERROR;
-        ERR("couldn't verify server certificate (%d)\n", err);
-        set_last_error( err );
-        goto fail;
+    heap_free(read_buf);
+
+    if(status != SEC_E_OK || res != ERROR_SUCCESS) {
+        WARN("Failed to initialize security context failed: %08x\n", status);
+        heap_free(conn->ssl_buf);
+        conn->ssl_buf = NULL;
+        DeleteSecurityContext(&ctx);
+        set_last_error(res ? res : ERROR_WINHTTP_SECURE_CHANNEL_ERROR);
+        return FALSE;
     }
+
+
     TRACE("established SSL connection\n");
     conn->secure = TRUE;
+    conn->ssl_ctx = ctx;
     return TRUE;
-
-fail:
-    if (conn->ssl_conn)
-    {
-        pSSL_shutdown( conn->ssl_conn );
-        pSSL_free( conn->ssl_conn );
-        conn->ssl_conn = NULL;
-    }
-#endif
-    return FALSE;
 }
 
-BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int flags, int *sent )
+static BOOL send_ssl_chunk(netconn_t *conn, const void *msg, size_t size)
 {
-    if (!netconn_connected( conn )) return FALSE;
-    if (conn->secure)
-    {
-#ifdef SONAME_LIBSSL
-        if (flags) FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
-        *sent = pSSL_write( conn->ssl_conn, msg, len );
-        if (*sent < 1 && len) return FALSE;
-        return TRUE;
-#else
+    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;
-#endif
     }
-    if ((*sent = send( conn->socket, msg, len, flags )) == -1)
-    {
-        set_last_error( sock_get_error( errno ) );
+
+    if(sock_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;
 }
 
-BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
+BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int *sent )
 {
-    *recvd = 0;
-    if (!netconn_connected( conn )) return FALSE;
-    if (!len) return TRUE;
-
     if (conn->secure)
     {
-#ifdef SONAME_LIBSSL
-        int ret;
+        const BYTE *ptr = msg;
+        size_t chunk_size;
 
-        if (flags & ~(MSG_PEEK | MSG_WAITALL))
-            FIXME("SSL_read does not support the following flags: %08x\n", flags);
+        *sent = 0;
 
-        /* this ugly hack is all for MSG_PEEK */
-        if (flags & MSG_PEEK && !conn->peek_msg)
-        {
-            if (!(conn->peek_msg = conn->peek_msg_mem = heap_alloc( len + 1 ))) return FALSE;
-        }
-        else if (flags & MSG_PEEK && conn->peek_msg)
-        {
-            if (len < conn->peek_len) FIXME("buffer isn't big enough, should we wrap?\n");
-            *recvd = min( len, conn->peek_len );
-            memcpy( buf, conn->peek_msg, *recvd );
-            return TRUE;
-        }
-        else if (conn->peek_msg)
-        {
-            *recvd = min( len, conn->peek_len );
-            memcpy( buf, conn->peek_msg, *recvd );
-            conn->peek_len -= *recvd;
-            conn->peek_msg += *recvd;
+        while(len) {
+            chunk_size = min(len, conn->ssl_sizes.cbMaximumMessage);
+            if(!send_ssl_chunk(conn, ptr, chunk_size))
+                return FALSE;
 
-            if (conn->peek_len == 0)
-            {
-                heap_free( conn->peek_msg_mem );
-                conn->peek_msg_mem = NULL;
-                conn->peek_msg = NULL;
-            }
-            /* check if we have enough data from the peek buffer */
-            if (!(flags & MSG_WAITALL) || (*recvd == len)) return TRUE;
+            *sent += chunk_size;
+            ptr += chunk_size;
+            len -= chunk_size;
         }
-        ret = pSSL_read( conn->ssl_conn, (char *)buf + *recvd, len - *recvd );
-        if (ret < 0)
-            return FALSE;
 
-        /* check if EOF was received */
-        if (!ret && (pSSL_get_error( conn->ssl_conn, ret ) == SSL_ERROR_ZERO_RETURN ||
-                     pSSL_get_error( conn->ssl_conn, ret ) == SSL_ERROR_SYSCALL ))
-        {
-            netconn_close( conn );
-            return TRUE;
-        }
-        if (flags & MSG_PEEK) /* must copy into buffer */
-        {
-            conn->peek_len = ret;
-            if (!ret)
-            {
-                heap_free( conn->peek_msg_mem );
-                conn->peek_msg_mem = NULL;
-                conn->peek_msg = NULL;
-            }
-            else memcpy( conn->peek_msg, buf, ret );
-        }
-        *recvd += ret;
         return TRUE;
-#else
-        return FALSE;
-#endif
     }
-    if ((*recvd = recv( conn->socket, buf, len, flags )) == -1)
+    if ((*sent = sock_send( conn->socket, msg, len, 0 )) == -1)
     {
         set_last_error( sock_get_error( errno ) );
         return FALSE;
@@ -845,119 +612,168 @@ BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd
     return TRUE;
 }
 
-BOOL netconn_query_data_available( netconn_t *conn, DWORD *available )
+static BOOL read_ssl_chunk(netconn_t *conn, void *buf, SIZE_T buf_size, SIZE_T *ret_size, BOOL *eof)
 {
-#ifdef FIONREAD
-    int ret, unread;
-#endif
-    *available = 0;
-    if (!netconn_connected( conn )) return FALSE;
+    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;
+    unsigned 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 = sock_recv(conn->socket, conn->ssl_buf+conn->extra_len, ssl_buf_size-conn->extra_len, 0);
+        if(buf_len < 0)
+            return FALSE;
 
-    if (conn->secure)
-    {
-#ifdef SONAME_LIBSSL
-        *available = pSSL_pending( conn->ssl_conn ) + conn->peek_len;
-#endif
-        return TRUE;
+        if(!buf_len) {
+            *eof = TRUE;
+            return TRUE;
+        }
     }
-#ifdef FIONREAD
-    if (!(ret = ioctlsocket( conn->socket, FIONREAD, &unread ))) *available = unread;
-#endif
-    return TRUE;
-}
 
-BOOL netconn_get_next_line( netconn_t *conn, char *buffer, DWORD *buflen )
-{
-    // ReactOS: use select instead of poll
-    fd_set infd;
-    BOOL ret = FALSE;
-    DWORD recvd = 0;
+    *ret_size = 0;
+    *eof = FALSE;
 
-    if (!netconn_connected( conn )) return FALSE;
+    do {
+        memset(bufs, 0, sizeof(bufs));
+        bufs[0].BufferType = SECBUFFER_DATA;
+        bufs[0].cbBuffer = buf_len;
+        bufs[0].pvBuffer = conn->ssl_buf;
 
-    if (conn->secure)
-    {
-#ifdef SONAME_LIBSSL
-        while (recvd < *buflen)
-        {
-            int dummy;
-            if (!netconn_recv( conn, &buffer[recvd], 1, 0, &dummy ))
-            {
-                set_last_error( ERROR_CONNECTION_ABORTED );
-                break;
-            }
-            if (buffer[recvd] == '\n')
-            {
-                ret = TRUE;
-                break;
+        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 = sock_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);
             }
-            if (buffer[recvd] != '\r') recvd++;
+
+            *ret_size = size;
         }
-        if (ret)
-        {
-            buffer[recvd++] = 0;
-            *buflen = recvd;
-            TRACE("received line %s\n", debugstr_a(buffer));
+    }
+
+    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 ret;
-#else
-        return FALSE;
-#endif
     }
 
-    FD_ZERO(&infd);
-    FD_SET(conn->socket, &infd);
+    return TRUE;
+}
 
-    while (recvd < *buflen)
-    {
-        int res;
-        struct timeval tv, *ptv;
-        socklen_t len = sizeof(tv);
+BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
+{
+    *recvd = 0;
+    if (!len) return TRUE;
 
-        if ((res = getsockopt( conn->socket, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, &len ) != -1))
-            ptv = &tv;
-        else
-            ptv = NULL;
+    if (conn->secure)
+    {
+        SIZE_T size, cread;
+        BOOL res, eof;
 
-        if (select( 0, &infd, NULL, NULL, ptv ) > 0)
+        if (conn->peek_msg)
         {
-            if ((res = recv( conn->socket, &buffer[recvd], 1, 0 )) <= 0)
+            *recvd = min( len, conn->peek_len );
+            memcpy( buf, conn->peek_msg, *recvd );
+            conn->peek_len -= *recvd;
+            conn->peek_msg += *recvd;
+
+            if (conn->peek_len == 0)
             {
-                if (res == -1) set_last_error( sock_get_error( errno ) );
+                heap_free( conn->peek_msg_mem );
+                conn->peek_msg_mem = NULL;
+                conn->peek_msg = NULL;
+            }
+            /* check if we have enough data from the peek buffer */
+            if (!(flags & MSG_WAITALL) || *recvd == len) return TRUE;
+        }
+        size = *recvd;
+
+        do {
+            res = read_ssl_chunk(conn, (BYTE*)buf+size, len-size, &cread, &eof);
+            if(!res) {
+                WARN("read_ssl_chunk failed\n");
+                if(!size)
+                    return FALSE;
                 break;
             }
-            if (buffer[recvd] == '\n')
-            {
-                ret = TRUE;
+
+            if(eof) {
+                TRACE("EOF\n");
                 break;
             }
-            if (buffer[recvd] != '\r') recvd++;
-        }
-        else
-        {
-            set_last_error( ERROR_WINHTTP_TIMEOUT );
-            break;
-        }
+
+            size += cread;
+        }while(!size || ((flags & MSG_WAITALL) && size < len));
+
+        TRACE("received %ld bytes\n", size);
+        *recvd = size;
+        return TRUE;
     }
-    if (ret)
+    if ((*recvd = sock_recv( conn->socket, buf, len, flags )) == -1)
     {
-        buffer[recvd++] = 0;
-        *buflen = recvd;
-        TRACE("received line %s\n", debugstr_a(buffer));
+        set_last_error( sock_get_error( errno ) );
+        return FALSE;
     }
-    return ret;
+    return TRUE;
+}
+
+ULONG netconn_query_data_available( netconn_t *conn )
+{
+    return conn->secure ? conn->peek_len : 0;
 }
 
 DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
 {
-    int res;
     struct timeval tv;
 
     /* value is in milliseconds, convert to struct timeval */
     tv.tv_sec = value / 1000;
     tv.tv_usec = (value % 1000) * 1000;
 
-    if ((res = setsockopt( netconn->socket, SOL_SOCKET, send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv, sizeof(tv) ) == -1))
+    if (setsockopt( netconn->socket, SOL_SOCKET, send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv, sizeof(tv) ) == -1)
     {
         WARN("setsockopt failed (%s)\n", strerror( errno ));
         return sock_get_error( errno );
@@ -965,7 +781,37 @@ DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
     return ERROR_SUCCESS;
 }
 
-static DWORD resolve_hostname( WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr *sa, socklen_t *sa_len )
+BOOL netconn_is_alive( netconn_t *netconn )
+{
+#ifdef MSG_DONTWAIT
+    ssize_t len;
+    BYTE b;
+
+    len = recv( netconn->socket, &b, 1, MSG_PEEK | MSG_DONTWAIT );
+    return len == 1 || (len == -1 && errno == EWOULDBLOCK);
+#elif defined(__MINGW32__) || defined(_MSC_VER)
+    ULONG mode;
+    int len;
+    char b;
+
+    mode = 1;
+    if(!ioctlsocket(netconn->socket, FIONBIO, &mode))
+        return FALSE;
+
+    len = recv(netconn->socket, &b, 1, MSG_PEEK);
+
+    mode = 0;
+    if(!ioctlsocket(netconn->socket, FIONBIO, &mode))
+        return FALSE;
+
+    return len == 1 || (len == -1 && WSAGetLastError() == WSAEWOULDBLOCK);
+#else
+    FIXME("not supported on this platform\n");
+    return TRUE;
+#endif
+}
+
+static DWORD resolve_hostname( const WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr_storage *sa )
 {
     char *hostname;
 #ifdef HAVE_GETADDRINFO
@@ -999,13 +845,6 @@ static DWORD resolve_hostname( WCHAR *hostnameW, INTERNET_PORT port, struct sock
         }
     }
     heap_free( hostname );
-    if (*sa_len < res->ai_addrlen)
-    {
-        WARN("address too small\n");
-        freeaddrinfo( res );
-        return ERROR_WINHTTP_NAME_NOT_RESOLVED;
-    }
-    *sa_len = res->ai_addrlen;
     memcpy( sa, res->ai_addr, res->ai_addrlen );
     /* Copy port */
     switch (res->ai_family)
@@ -1031,13 +870,6 @@ static DWORD resolve_hostname( WCHAR *hostnameW, INTERNET_PORT port, struct sock
         LeaveCriticalSection( &cs_gethostbyname );
         return ERROR_WINHTTP_NAME_NOT_RESOLVED;
     }
-    if (*sa_len < sizeof(struct sockaddr_in))
-    {
-        WARN("address too small\n");
-        LeaveCriticalSection( &cs_gethostbyname );
-        return ERROR_WINHTTP_NAME_NOT_RESOLVED;
-    }
-    *sa_len = sizeof(struct sockaddr_in);
     memset( sa, 0, sizeof(struct sockaddr_in) );
     memcpy( &sin->sin_addr, he->h_addr, he->h_length );
     sin->sin_family = he->h_addrtype;
@@ -1050,19 +882,18 @@ static DWORD resolve_hostname( WCHAR *hostnameW, INTERNET_PORT port, struct sock
 
 struct resolve_args
 {
-    WCHAR           *hostname;
-    INTERNET_PORT    port;
-    struct sockaddr *sa;
-    socklen_t       *sa_len;
+    const WCHAR             *hostname;
+    INTERNET_PORT            port;
+    struct sockaddr_storage *sa;
 };
 
 static DWORD CALLBACK resolve_proc( LPVOID arg )
 {
     struct resolve_args *ra = arg;
-    return resolve_hostname( ra->hostname, ra->port, ra->sa, ra->sa_len );
+    return resolve_hostname( ra->hostname, ra->port, ra->sa );
 }
 
-BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr *sa, socklen_t *sa_len, int timeout )
+BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr_storage *sa, int timeout )
 {
     DWORD ret;
 
@@ -1075,7 +906,6 @@ BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr *sa,
         ra.hostname = hostname;
         ra.port     = port;
         ra.sa       = sa;
-        ra.sa_len   = sa_len;
 
         thread = CreateThread( NULL, 0, resolve_proc, &ra, 0, NULL );
         if (!thread) return FALSE;
@@ -1085,7 +915,7 @@ BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr *sa,
         else ret = ERROR_WINHTTP_TIMEOUT;
         CloseHandle( thread );
     }
-    else ret = resolve_hostname( hostname, port, sa, sa_len );
+    else ret = resolve_hostname( hostname, port, sa );
 
     if (ret)
     {
@@ -1097,35 +927,22 @@ BOOL netconn_resolve( WCHAR *hostname, INTERNET_PORT port, struct sockaddr *sa,
 
 const void *netconn_get_certificate( netconn_t *conn )
 {
-#ifdef SONAME_LIBSSL
-    X509 *cert;
     const CERT_CONTEXT *ret;
+    SECURITY_STATUS res;
 
     if (!conn->secure) return NULL;
-
-    if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn ))) return NULL;
-    ret = X509_to_cert_context( cert );
-    return ret;
-#else
-    return NULL;
-#endif
+    res = QueryContextAttributesW(&conn->ssl_ctx, SECPKG_ATTR_REMOTE_CERT_CONTEXT, (void*)&ret);
+    return res == SEC_E_OK ? ret : NULL;
 }
 
 int netconn_get_cipher_strength( netconn_t *conn )
 {
-#ifdef SONAME_LIBSSL
-#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
-    const SSL_CIPHER *cipher;
-#else
-    SSL_CIPHER *cipher;
-#endif
-    int bits = 0;
+    SecPkgContext_ConnectionInfo conn_info;
+    SECURITY_STATUS res;
 
     if (!conn->secure) return 0;
-    if (!(cipher = pSSL_get_current_cipher( conn->ssl_conn ))) return 0;
-    pSSL_CIPHER_get_bits( cipher, &bits );
-    return bits;
-#else
-    return 0;
-#endif
+    res = QueryContextAttributesW(&conn->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;
 }