2 * Wininet - networking layer. Uses unix sockets or OpenSSL.
4 * Copyright 2002 TransGaming Technologies Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/port.h"
26 #define NONAMELESSUNION
28 #if defined(__MINGW32__) || defined (_MSC_VER)
32 #include <sys/types.h>
36 #ifdef HAVE_SYS_POLL_H
37 # include <sys/poll.h>
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
42 #ifdef HAVE_SYS_SOCKET_H
43 # include <sys/socket.h>
45 #ifdef HAVE_SYS_FILIO_H
46 # include <sys/filio.h>
51 #ifdef HAVE_SYS_IOCTL_H
52 # include <sys/ioctl.h>
58 #ifdef HAVE_NETINET_IN_H
59 # include <netinet/in.h>
61 #ifdef HAVE_NETINET_TCP_H
62 # include <netinet/tcp.h>
64 #ifdef HAVE_OPENSSL_SSL_H
65 # include <openssl/ssl.h>
66 # include <openssl/opensslv.h>
78 #include "wine/library.h"
85 #include "wine/debug.h"
88 /* To avoid conflicts with the Unix socket headers. we only need it for
89 * the error codes anyway. */
93 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
96 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
99 * This should use winsock - To use winsock the functions will have to change a bit
100 * as they are designed for unix sockets.
101 * SSL stuff should use crypt32.dll
106 #include <openssl/err.h>
108 static void *OpenSSL_ssl_handle;
109 static void *OpenSSL_crypto_handle;
111 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER > 0x10000000)
112 static const SSL_METHOD *meth;
114 static SSL_METHOD *meth;
117 static int hostname_idx;
118 static int error_idx;
121 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
123 /* OpenSSL functions that we use */
124 MAKE_FUNCPTR(SSL_library_init);
125 MAKE_FUNCPTR(SSL_load_error_strings);
126 MAKE_FUNCPTR(SSLv23_method);
127 MAKE_FUNCPTR(SSL_CTX_free);
128 MAKE_FUNCPTR(SSL_CTX_new);
129 MAKE_FUNCPTR(SSL_new);
130 MAKE_FUNCPTR(SSL_free);
131 MAKE_FUNCPTR(SSL_set_fd);
132 MAKE_FUNCPTR(SSL_connect);
133 MAKE_FUNCPTR(SSL_shutdown);
134 MAKE_FUNCPTR(SSL_write);
135 MAKE_FUNCPTR(SSL_read);
136 MAKE_FUNCPTR(SSL_pending);
137 MAKE_FUNCPTR(SSL_get_error);
138 MAKE_FUNCPTR(SSL_get_ex_new_index);
139 MAKE_FUNCPTR(SSL_get_ex_data);
140 MAKE_FUNCPTR(SSL_set_ex_data);
141 MAKE_FUNCPTR(SSL_get_ex_data_X509_STORE_CTX_idx);
142 MAKE_FUNCPTR(SSL_get_peer_certificate);
143 MAKE_FUNCPTR(SSL_CTX_get_timeout);
144 MAKE_FUNCPTR(SSL_CTX_set_timeout);
145 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
146 MAKE_FUNCPTR(SSL_CTX_set_verify);
147 MAKE_FUNCPTR(SSL_get_current_cipher);
148 MAKE_FUNCPTR(SSL_CIPHER_get_bits);
150 /* OpenSSL's libcrypto functions that we use */
151 MAKE_FUNCPTR(BIO_new_fp);
152 MAKE_FUNCPTR(CRYPTO_num_locks);
153 MAKE_FUNCPTR(CRYPTO_set_id_callback);
154 MAKE_FUNCPTR(CRYPTO_set_locking_callback);
155 MAKE_FUNCPTR(ERR_free_strings);
156 MAKE_FUNCPTR(ERR_get_error);
157 MAKE_FUNCPTR(ERR_error_string);
158 MAKE_FUNCPTR(X509_STORE_CTX_get_ex_data);
159 MAKE_FUNCPTR(X509_STORE_CTX_get_chain);
160 MAKE_FUNCPTR(i2d_X509);
161 MAKE_FUNCPTR(sk_num);
162 MAKE_FUNCPTR(sk_value);
165 static CRITICAL_SECTION *ssl_locks;
166 static unsigned int num_ssl_locks;
168 static unsigned long ssl_thread_id(void)
170 return GetCurrentThreadId();
173 static void ssl_lock_callback(int mode, int type, const char *file, int line)
175 if (mode & CRYPTO_LOCK)
176 EnterCriticalSection(&ssl_locks[type]);
178 LeaveCriticalSection(&ssl_locks[type]);
181 static PCCERT_CONTEXT X509_to_cert_context(X509 *cert)
183 unsigned char* buffer,*p;
185 BOOL malloced = FALSE;
189 len = pi2d_X509(cert,&p);
191 * SSL 0.9.7 and above malloc the buffer if it is null.
192 * however earlier version do not and so we would need to alloc the buffer.
194 * see the i2d_X509 man page for more details.
198 buffer = heap_alloc(len);
200 len = pi2d_X509(cert,&p);
208 ret = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
218 static DWORD netconn_verify_cert(PCCERT_CONTEXT cert, HCERTSTORE store,
219 WCHAR *server, DWORD security_flags)
222 CERT_CHAIN_PARA chainPara = { sizeof(chainPara), { 0 } };
223 PCCERT_CHAIN_CONTEXT chain;
224 char oid_server_auth[] = szOID_PKIX_KP_SERVER_AUTH;
225 char *server_auth[] = { oid_server_auth };
226 DWORD err = ERROR_SUCCESS, chainFlags = 0;
228 TRACE("verifying %s\n", debugstr_w(server));
229 chainPara.RequestedUsage.Usage.cUsageIdentifier = 1;
230 chainPara.RequestedUsage.Usage.rgpszUsageIdentifier = server_auth;
231 if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION))
232 chainFlags |= CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
233 if ((ret = CertGetCertificateChain(NULL, cert, NULL, store, &chainPara,
234 chainFlags, NULL, &chain)))
236 if (chain->TrustStatus.dwErrorStatus)
238 static const DWORD supportedErrors =
239 CERT_TRUST_IS_NOT_TIME_VALID |
240 CERT_TRUST_IS_UNTRUSTED_ROOT |
241 CERT_TRUST_IS_PARTIAL_CHAIN |
242 CERT_TRUST_IS_OFFLINE_REVOCATION |
243 CERT_TRUST_REVOCATION_STATUS_UNKNOWN |
244 CERT_TRUST_IS_REVOKED |
245 CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
247 if (chain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_NOT_TIME_VALID &&
248 !(security_flags & SECURITY_FLAG_IGNORE_CERT_DATE_INVALID))
249 err = ERROR_INTERNET_SEC_CERT_DATE_INVALID;
250 else if (chain->TrustStatus.dwErrorStatus &
251 (CERT_TRUST_IS_UNTRUSTED_ROOT | CERT_TRUST_IS_PARTIAL_CHAIN) &&
252 !(security_flags & SECURITY_FLAG_IGNORE_UNKNOWN_CA))
253 err = ERROR_INTERNET_INVALID_CA;
254 else if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION) &&
255 ((chain->TrustStatus.dwErrorStatus &
256 CERT_TRUST_IS_OFFLINE_REVOCATION) ||
257 (chain->TrustStatus.dwErrorStatus &
258 CERT_TRUST_REVOCATION_STATUS_UNKNOWN)))
259 err = ERROR_INTERNET_SEC_CERT_NO_REV;
260 else if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION) &&
261 (chain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_REVOKED))
262 err = ERROR_INTERNET_SEC_CERT_REVOKED;
263 else if (!(security_flags & SECURITY_FLAG_IGNORE_WRONG_USAGE) &&
264 (chain->TrustStatus.dwErrorStatus &
265 CERT_TRUST_IS_NOT_VALID_FOR_USAGE))
266 err = ERROR_INTERNET_SEC_INVALID_CERT;
267 else if (chain->TrustStatus.dwErrorStatus & ~supportedErrors)
268 err = ERROR_INTERNET_SEC_INVALID_CERT;
272 CERT_CHAIN_POLICY_PARA policyPara;
273 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara;
274 CERT_CHAIN_POLICY_STATUS policyStatus;
275 CERT_CHAIN_CONTEXT chainCopy;
277 /* Clear chain->TrustStatus.dwErrorStatus so
278 * CertVerifyCertificateChainPolicy will verify additional checks
279 * rather than stopping with an existing, ignored error.
281 memcpy(&chainCopy, chain, sizeof(chainCopy));
282 chainCopy.TrustStatus.dwErrorStatus = 0;
283 sslExtraPolicyPara.u.cbSize = sizeof(sslExtraPolicyPara);
284 sslExtraPolicyPara.dwAuthType = AUTHTYPE_SERVER;
285 sslExtraPolicyPara.pwszServerName = server;
286 sslExtraPolicyPara.fdwChecks = security_flags;
287 policyPara.cbSize = sizeof(policyPara);
288 policyPara.dwFlags = 0;
289 policyPara.pvExtraPolicyPara = &sslExtraPolicyPara;
290 ret = CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL,
291 &chainCopy, &policyPara, &policyStatus);
292 /* Any error in the policy status indicates that the
293 * policy couldn't be verified.
295 if (ret && policyStatus.dwError)
297 if (policyStatus.dwError == CERT_E_CN_NO_MATCH)
298 err = ERROR_INTERNET_SEC_CERT_CN_INVALID;
300 err = ERROR_INTERNET_SEC_INVALID_CERT;
303 CertFreeCertificateChain(chain);
305 TRACE("returning %08x\n", err);
309 static int netconn_secure_verify(int preverify_ok, X509_STORE_CTX *ctx)
314 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
315 CERT_STORE_CREATE_NEW_FLAG, NULL);
318 ssl = pX509_STORE_CTX_get_ex_data(ctx,
319 pSSL_get_ex_data_X509_STORE_CTX_idx());
320 server = pSSL_get_ex_data(ssl, hostname_idx);
321 conn = pSSL_get_ex_data(ssl, conn_idx);
326 PCCERT_CONTEXT endCert = NULL;
327 struct stack_st *chain = (struct stack_st *)pX509_STORE_CTX_get_chain( ctx );
330 for (i = 0; ret && i < psk_num(chain); i++)
332 PCCERT_CONTEXT context;
334 cert = (X509 *)psk_value(chain, i);
335 if ((context = X509_to_cert_context(cert)))
337 ret = CertAddCertificateContextToStore(store, context,
338 CERT_STORE_ADD_ALWAYS, i ? NULL : &endCert);
339 CertFreeCertificateContext(context);
342 if (!endCert) ret = FALSE;
345 DWORD_PTR err = netconn_verify_cert(endCert, store, server,
346 conn->security_flags);
350 pSSL_set_ex_data(ssl, error_idx, (void *)err);
354 CertFreeCertificateContext(endCert);
355 CertCloseStore(store, 0);
362 static CRITICAL_SECTION init_ssl_cs;
363 static CRITICAL_SECTION_DEBUG init_ssl_cs_debug =
366 { &init_ssl_cs_debug.ProcessLocksList,
367 &init_ssl_cs_debug.ProcessLocksList },
368 0, 0, { (DWORD_PTR)(__FILE__ ": init_ssl_cs") }
370 static CRITICAL_SECTION init_ssl_cs = { &init_ssl_cs_debug, -1, 0, 0, 0, 0 };
372 static DWORD init_openssl(void)
374 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
377 if(OpenSSL_ssl_handle)
378 return ERROR_SUCCESS;
380 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
381 if(!OpenSSL_ssl_handle) {
382 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
383 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
386 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
387 if(!OpenSSL_crypto_handle) {
388 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
389 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
392 /* mmm nice ugly macroness */
394 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
396 ERR("failed to load symbol %s\n", #x); \
397 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
400 DYNSSL(SSL_library_init);
401 DYNSSL(SSL_load_error_strings);
402 DYNSSL(SSLv23_method);
403 DYNSSL(SSL_CTX_free);
409 DYNSSL(SSL_shutdown);
413 DYNSSL(SSL_get_error);
414 DYNSSL(SSL_get_ex_new_index);
415 DYNSSL(SSL_get_ex_data);
416 DYNSSL(SSL_set_ex_data);
417 DYNSSL(SSL_get_ex_data_X509_STORE_CTX_idx);
418 DYNSSL(SSL_get_peer_certificate);
419 DYNSSL(SSL_CTX_get_timeout);
420 DYNSSL(SSL_CTX_set_timeout);
421 DYNSSL(SSL_CTX_set_default_verify_paths);
422 DYNSSL(SSL_CTX_set_verify);
423 DYNSSL(SSL_get_current_cipher);
424 DYNSSL(SSL_CIPHER_get_bits);
427 #define DYNCRYPTO(x) \
428 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
430 ERR("failed to load symbol %s\n", #x); \
431 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
434 DYNCRYPTO(BIO_new_fp);
435 DYNCRYPTO(CRYPTO_num_locks);
436 DYNCRYPTO(CRYPTO_set_id_callback);
437 DYNCRYPTO(CRYPTO_set_locking_callback);
438 DYNCRYPTO(ERR_free_strings);
439 DYNCRYPTO(ERR_get_error);
440 DYNCRYPTO(ERR_error_string);
441 DYNCRYPTO(X509_STORE_CTX_get_ex_data);
442 DYNCRYPTO(X509_STORE_CTX_get_chain);
449 pSSL_load_error_strings();
450 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
452 meth = pSSLv23_method();
453 ctx = pSSL_CTX_new(meth);
454 if(!pSSL_CTX_set_default_verify_paths(ctx)) {
455 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
456 pERR_error_string(pERR_get_error(), 0));
457 return ERROR_OUTOFMEMORY;
460 hostname_idx = pSSL_get_ex_new_index(0, (void *)"hostname index", NULL, NULL, NULL);
461 if(hostname_idx == -1) {
462 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
463 return ERROR_OUTOFMEMORY;
466 error_idx = pSSL_get_ex_new_index(0, (void *)"error index", NULL, NULL, NULL);
467 if(error_idx == -1) {
468 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
469 return ERROR_OUTOFMEMORY;
472 conn_idx = pSSL_get_ex_new_index(0, (void *)"netconn index", NULL, NULL, NULL);
474 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
475 return ERROR_OUTOFMEMORY;
478 pSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, netconn_secure_verify);
480 pCRYPTO_set_id_callback(ssl_thread_id);
481 num_ssl_locks = pCRYPTO_num_locks();
482 ssl_locks = heap_alloc(num_ssl_locks * sizeof(CRITICAL_SECTION));
484 return ERROR_OUTOFMEMORY;
486 for(i = 0; i < num_ssl_locks; i++)
488 InitializeCriticalSection(&ssl_locks[i]);
489 ssl_locks[i].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ssl_locks");
491 pCRYPTO_set_locking_callback(ssl_lock_callback);
493 return ERROR_SUCCESS;
495 FIXME("can't use SSL, not compiled in.\n");
496 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
500 DWORD create_netconn(BOOL useSSL, server_t *server, DWORD security_flags, DWORD timeout, netconn_t **ret)
508 TRACE("using SSL connection\n");
510 EnterCriticalSection(&init_ssl_cs);
511 res = init_openssl();
512 LeaveCriticalSection(&init_ssl_cs);
513 if(res != ERROR_SUCCESS)
517 netconn = heap_alloc_zero(sizeof(*netconn));
519 return ERROR_OUTOFMEMORY;
521 netconn->useSSL = useSSL;
522 netconn->socketFD = -1;
523 netconn->security_flags = security_flags;
524 list_init(&netconn->pool_entry);
526 assert(server->addr_len);
527 result = netconn->socketFD = socket(server->addr.ss_family, SOCK_STREAM, 0);
530 ioctlsocket(netconn->socketFD, FIONBIO, &flag);
531 result = connect(netconn->socketFD, (struct sockaddr*)&server->addr, server->addr_len);
534 if (sock_get_error(errno) == WSAEINPROGRESS) {
538 pfd.fd = netconn->socketFD;
539 pfd.events = POLLOUT;
540 res = poll(&pfd, 1, timeout);
543 closesocket(netconn->socketFD);
545 return ERROR_INTERNET_CANNOT_CONNECT;
550 socklen_t len = sizeof(err);
551 if (!getsockopt(netconn->socketFD, SOL_SOCKET, SO_ERROR, &err, &len) && !err)
557 closesocket(netconn->socketFD);
560 ioctlsocket(netconn->socketFD, FIONBIO, &flag);
565 return sock_get_error(errno);
570 result = setsockopt(netconn->socketFD, IPPROTO_TCP, TCP_NODELAY, (void*)&flag, sizeof(flag));
572 WARN("setsockopt(TCP_NODELAY) failed\n");
575 server_addref(server);
576 netconn->server = server;
579 return ERROR_SUCCESS;
582 void free_netconn(netconn_t *netconn)
584 server_release(netconn->server);
587 if (netconn->ssl_s) {
588 pSSL_shutdown(netconn->ssl_s);
589 pSSL_free(netconn->ssl_s);
593 closesocket(netconn->socketFD);
597 void NETCON_unload(void)
599 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
600 if (OpenSSL_crypto_handle)
603 wine_dlclose(OpenSSL_crypto_handle, NULL, 0);
605 if (OpenSSL_ssl_handle)
609 wine_dlclose(OpenSSL_ssl_handle, NULL, 0);
614 for (i = 0; i < num_ssl_locks; i++)
616 ssl_locks[i].DebugInfo->Spare[0] = 0;
617 DeleteCriticalSection(&ssl_locks[i]);
619 heap_free(ssl_locks);
624 /* translate a unix error code into a winsock one */
625 int sock_get_error( int err )
627 #if !defined(__MINGW32__) && !defined (_MSC_VER)
630 case EINTR: return WSAEINTR;
631 case EBADF: return WSAEBADF;
633 case EACCES: return WSAEACCES;
634 case EFAULT: return WSAEFAULT;
635 case EINVAL: return WSAEINVAL;
636 case EMFILE: return WSAEMFILE;
637 case EWOULDBLOCK: return WSAEWOULDBLOCK;
638 case EINPROGRESS: return WSAEINPROGRESS;
639 case EALREADY: return WSAEALREADY;
640 case ENOTSOCK: return WSAENOTSOCK;
641 case EDESTADDRREQ: return WSAEDESTADDRREQ;
642 case EMSGSIZE: return WSAEMSGSIZE;
643 case EPROTOTYPE: return WSAEPROTOTYPE;
644 case ENOPROTOOPT: return WSAENOPROTOOPT;
645 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
646 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
647 case EOPNOTSUPP: return WSAEOPNOTSUPP;
648 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
649 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
650 case EADDRINUSE: return WSAEADDRINUSE;
651 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
652 case ENETDOWN: return WSAENETDOWN;
653 case ENETUNREACH: return WSAENETUNREACH;
654 case ENETRESET: return WSAENETRESET;
655 case ECONNABORTED: return WSAECONNABORTED;
657 case ECONNRESET: return WSAECONNRESET;
658 case ENOBUFS: return WSAENOBUFS;
659 case EISCONN: return WSAEISCONN;
660 case ENOTCONN: return WSAENOTCONN;
661 case ESHUTDOWN: return WSAESHUTDOWN;
662 case ETOOMANYREFS: return WSAETOOMANYREFS;
663 case ETIMEDOUT: return WSAETIMEDOUT;
664 case ECONNREFUSED: return WSAECONNREFUSED;
665 case ELOOP: return WSAELOOP;
666 case ENAMETOOLONG: return WSAENAMETOOLONG;
667 case EHOSTDOWN: return WSAEHOSTDOWN;
668 case EHOSTUNREACH: return WSAEHOSTUNREACH;
669 case ENOTEMPTY: return WSAENOTEMPTY;
671 case EPROCLIM: return WSAEPROCLIM;
674 case EUSERS: return WSAEUSERS;
677 case EDQUOT: return WSAEDQUOT;
680 case ESTALE: return WSAESTALE;
683 case EREMOTE: return WSAEREMOTE;
685 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
691 /******************************************************************************
692 * NETCON_secure_connect
693 * Initiates a secure connection over an existing plaintext connection.
695 DWORD NETCON_secure_connect(netconn_t *connection, LPWSTR hostname)
697 DWORD res = ERROR_NOT_SUPPORTED;
701 /* can't connect if we are already connected */
702 if (connection->ssl_s)
704 ERR("already connected\n");
705 return ERROR_INTERNET_CANNOT_CONNECT;
708 ssl_s = pSSL_new(ctx);
711 ERR("SSL_new failed: %s\n",
712 pERR_error_string(pERR_get_error(), 0));
713 return ERROR_OUTOFMEMORY;
716 if (!pSSL_set_fd(ssl_s, connection->socketFD))
718 ERR("SSL_set_fd failed: %s\n",
719 pERR_error_string(pERR_get_error(), 0));
720 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
724 if (!pSSL_set_ex_data(ssl_s, hostname_idx, hostname))
726 ERR("SSL_set_ex_data failed: %s\n",
727 pERR_error_string(pERR_get_error(), 0));
728 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
731 if (!pSSL_set_ex_data(ssl_s, conn_idx, connection))
733 ERR("SSL_set_ex_data failed: %s\n",
734 pERR_error_string(pERR_get_error(), 0));
735 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
738 if (pSSL_connect(ssl_s) <= 0)
740 res = (DWORD_PTR)pSSL_get_ex_data(ssl_s, error_idx);
742 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
743 ERR("SSL_connect failed: %d\n", res);
747 connection->ssl_s = ssl_s;
748 return ERROR_SUCCESS;
753 pSSL_shutdown(ssl_s);
760 /******************************************************************************
762 * Basically calls 'send()' unless we should use SSL
763 * number of chars send is put in *sent
765 DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags,
768 if (!connection->useSSL)
770 *sent = send(connection->socketFD, msg, len, flags);
772 return sock_get_error(errno);
773 return ERROR_SUCCESS;
778 if(!connection->ssl_s) {
779 FIXME("not connected\n");
780 return ERROR_NOT_SUPPORTED;
783 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
784 *sent = pSSL_write(connection->ssl_s, msg, len);
785 if (*sent < 1 && len)
786 return ERROR_INTERNET_CONNECTION_ABORTED;
787 return ERROR_SUCCESS;
789 return ERROR_NOT_SUPPORTED;
794 /******************************************************************************
796 * Basically calls 'recv()' unless we should use SSL
797 * number of chars received is put in *recvd
799 DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, int flags,
800 int *recvd /* out */)
804 return ERROR_SUCCESS;
805 if (!connection->useSSL)
807 *recvd = recv(connection->socketFD, buf, len, flags);
808 return *recvd == -1 ? sock_get_error(errno) : ERROR_SUCCESS;
813 if(!connection->ssl_s) {
814 FIXME("not connected\n");
815 return ERROR_NOT_SUPPORTED;
817 *recvd = pSSL_read(connection->ssl_s, buf, len);
819 /* Check if EOF was received */
820 if(!*recvd && (pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_ZERO_RETURN
821 || pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_SYSCALL))
822 return ERROR_SUCCESS;
824 return *recvd > 0 ? ERROR_SUCCESS : ERROR_INTERNET_CONNECTION_ABORTED;
826 return ERROR_NOT_SUPPORTED;
831 /******************************************************************************
832 * NETCON_query_data_available
833 * Returns the number of bytes of peeked data plus the number of bytes of
834 * queued, but unread data.
836 BOOL NETCON_query_data_available(netconn_t *connection, DWORD *available)
840 if (!connection->useSSL)
844 int retval = ioctlsocket(connection->socketFD, FIONREAD, &unread);
847 TRACE("%d bytes of queued, but unread data\n", unread);
848 *available += unread;
855 *available = connection->ssl_s ? pSSL_pending(connection->ssl_s) : 0;
861 BOOL NETCON_is_alive(netconn_t *netconn)
867 len = recv(netconn->socketFD, &b, 1, MSG_PEEK|MSG_DONTWAIT);
868 return len == 1 || (len == -1 && errno == EWOULDBLOCK);
869 #elif defined(__MINGW32__) || defined(_MSC_VER)
875 if(!ioctlsocket(netconn->socketFD, FIONBIO, &mode))
878 len = recv(netconn->socketFD, &b, 1, MSG_PEEK);
881 if(!ioctlsocket(netconn->socketFD, FIONBIO, &mode))
884 return len == 1 || (len == -1 && errno == WSAEWOULDBLOCK);
886 FIXME("not supported on this platform\n");
891 LPCVOID NETCON_GetCert(netconn_t *connection)
897 if (!connection->ssl_s)
900 cert = pSSL_get_peer_certificate(connection->ssl_s);
901 r = X509_to_cert_context(cert);
908 int NETCON_GetCipherStrength(netconn_t *connection)
911 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
912 const SSL_CIPHER *cipher;
918 if (!connection->ssl_s)
920 cipher = pSSL_get_current_cipher(connection->ssl_s);
923 pSSL_CIPHER_get_bits(cipher, &bits);
930 DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, DWORD value)
935 /* value is in milliseconds, convert to struct timeval */
936 if (value == INFINITE)
943 tv.tv_sec = value / 1000;
944 tv.tv_usec = (value % 1000) * 1000;
946 result = setsockopt(connection->socketFD, SOL_SOCKET,
947 send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv,
951 WARN("setsockopt failed (%s)\n", strerror(errno));
952 return sock_get_error(errno);
954 return ERROR_SUCCESS;