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++)
487 InitializeCriticalSection(&ssl_locks[i]);
488 pCRYPTO_set_locking_callback(ssl_lock_callback);
490 return ERROR_SUCCESS;
492 FIXME("can't use SSL, not compiled in.\n");
493 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
497 DWORD create_netconn(BOOL useSSL, server_t *server, DWORD security_flags, netconn_t **ret)
505 TRACE("using SSL connection\n");
507 EnterCriticalSection(&init_ssl_cs);
508 res = init_openssl();
509 LeaveCriticalSection(&init_ssl_cs);
510 if(res != ERROR_SUCCESS)
514 netconn = heap_alloc_zero(sizeof(*netconn));
516 return ERROR_OUTOFMEMORY;
518 netconn->useSSL = useSSL;
519 netconn->socketFD = -1;
520 netconn->security_flags = security_flags;
521 list_init(&netconn->pool_entry);
523 assert(server->addr_len);
524 result = netconn->socketFD = socket(server->addr.ss_family, SOCK_STREAM, 0);
526 result = connect(netconn->socketFD, (struct sockaddr*)&server->addr, server->addr_len);
528 closesocket(netconn->socketFD);
532 return sock_get_error(errno);
537 result = setsockopt(netconn->socketFD, IPPROTO_TCP, TCP_NODELAY, (void*)&flag, sizeof(flag));
539 WARN("setsockopt(TCP_NODELAY) failed\n");
542 server_addref(server);
543 netconn->server = server;
546 return ERROR_SUCCESS;
549 void free_netconn(netconn_t *netconn)
551 server_release(netconn->server);
554 if (netconn->ssl_s) {
555 pSSL_shutdown(netconn->ssl_s);
556 pSSL_free(netconn->ssl_s);
560 closesocket(netconn->socketFD);
564 void NETCON_unload(void)
566 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
567 if (OpenSSL_crypto_handle)
570 wine_dlclose(OpenSSL_crypto_handle, NULL, 0);
572 if (OpenSSL_ssl_handle)
576 wine_dlclose(OpenSSL_ssl_handle, NULL, 0);
581 for (i = 0; i < num_ssl_locks; i++) DeleteCriticalSection(&ssl_locks[i]);
582 heap_free(ssl_locks);
587 /* translate a unix error code into a winsock one */
588 int sock_get_error( int err )
590 #if !defined(__MINGW32__) && !defined (_MSC_VER)
593 case EINTR: return WSAEINTR;
594 case EBADF: return WSAEBADF;
596 case EACCES: return WSAEACCES;
597 case EFAULT: return WSAEFAULT;
598 case EINVAL: return WSAEINVAL;
599 case EMFILE: return WSAEMFILE;
600 case EWOULDBLOCK: return WSAEWOULDBLOCK;
601 case EINPROGRESS: return WSAEINPROGRESS;
602 case EALREADY: return WSAEALREADY;
603 case ENOTSOCK: return WSAENOTSOCK;
604 case EDESTADDRREQ: return WSAEDESTADDRREQ;
605 case EMSGSIZE: return WSAEMSGSIZE;
606 case EPROTOTYPE: return WSAEPROTOTYPE;
607 case ENOPROTOOPT: return WSAENOPROTOOPT;
608 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
609 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
610 case EOPNOTSUPP: return WSAEOPNOTSUPP;
611 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
612 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
613 case EADDRINUSE: return WSAEADDRINUSE;
614 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
615 case ENETDOWN: return WSAENETDOWN;
616 case ENETUNREACH: return WSAENETUNREACH;
617 case ENETRESET: return WSAENETRESET;
618 case ECONNABORTED: return WSAECONNABORTED;
620 case ECONNRESET: return WSAECONNRESET;
621 case ENOBUFS: return WSAENOBUFS;
622 case EISCONN: return WSAEISCONN;
623 case ENOTCONN: return WSAENOTCONN;
624 case ESHUTDOWN: return WSAESHUTDOWN;
625 case ETOOMANYREFS: return WSAETOOMANYREFS;
626 case ETIMEDOUT: return WSAETIMEDOUT;
627 case ECONNREFUSED: return WSAECONNREFUSED;
628 case ELOOP: return WSAELOOP;
629 case ENAMETOOLONG: return WSAENAMETOOLONG;
630 case EHOSTDOWN: return WSAEHOSTDOWN;
631 case EHOSTUNREACH: return WSAEHOSTUNREACH;
632 case ENOTEMPTY: return WSAENOTEMPTY;
634 case EPROCLIM: return WSAEPROCLIM;
637 case EUSERS: return WSAEUSERS;
640 case EDQUOT: return WSAEDQUOT;
643 case ESTALE: return WSAESTALE;
646 case EREMOTE: return WSAEREMOTE;
648 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
654 /******************************************************************************
655 * NETCON_secure_connect
656 * Initiates a secure connection over an existing plaintext connection.
658 DWORD NETCON_secure_connect(netconn_t *connection, LPWSTR hostname)
660 DWORD res = ERROR_NOT_SUPPORTED;
664 /* can't connect if we are already connected */
665 if (connection->ssl_s)
667 ERR("already connected\n");
668 return ERROR_INTERNET_CANNOT_CONNECT;
671 ssl_s = pSSL_new(ctx);
674 ERR("SSL_new failed: %s\n",
675 pERR_error_string(pERR_get_error(), 0));
676 return ERROR_OUTOFMEMORY;
679 if (!pSSL_set_fd(ssl_s, connection->socketFD))
681 ERR("SSL_set_fd failed: %s\n",
682 pERR_error_string(pERR_get_error(), 0));
683 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
687 if (!pSSL_set_ex_data(ssl_s, hostname_idx, hostname))
689 ERR("SSL_set_ex_data failed: %s\n",
690 pERR_error_string(pERR_get_error(), 0));
691 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
694 if (!pSSL_set_ex_data(ssl_s, conn_idx, connection))
696 ERR("SSL_set_ex_data failed: %s\n",
697 pERR_error_string(pERR_get_error(), 0));
698 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
701 if (pSSL_connect(ssl_s) <= 0)
703 res = (DWORD_PTR)pSSL_get_ex_data(ssl_s, error_idx);
705 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
706 ERR("SSL_connect failed: %d\n", res);
710 connection->ssl_s = ssl_s;
711 return ERROR_SUCCESS;
716 pSSL_shutdown(ssl_s);
723 /******************************************************************************
725 * Basically calls 'send()' unless we should use SSL
726 * number of chars send is put in *sent
728 DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags,
731 if (!connection->useSSL)
733 *sent = send(connection->socketFD, msg, len, flags);
735 return sock_get_error(errno);
736 return ERROR_SUCCESS;
741 if(!connection->ssl_s) {
742 FIXME("not connected\n");
743 return ERROR_NOT_SUPPORTED;
746 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
747 *sent = pSSL_write(connection->ssl_s, msg, len);
748 if (*sent < 1 && len)
749 return ERROR_INTERNET_CONNECTION_ABORTED;
750 return ERROR_SUCCESS;
752 return ERROR_NOT_SUPPORTED;
757 /******************************************************************************
759 * Basically calls 'recv()' unless we should use SSL
760 * number of chars received is put in *recvd
762 DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, int flags,
763 int *recvd /* out */)
767 return ERROR_SUCCESS;
768 if (!connection->useSSL)
770 *recvd = recv(connection->socketFD, buf, len, flags);
771 return *recvd == -1 ? sock_get_error(errno) : ERROR_SUCCESS;
776 if(!connection->ssl_s) {
777 FIXME("not connected\n");
778 return ERROR_NOT_SUPPORTED;
780 *recvd = pSSL_read(connection->ssl_s, buf, len);
782 /* Check if EOF was received */
783 if(!*recvd && (pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_ZERO_RETURN
784 || pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_SYSCALL))
785 return ERROR_SUCCESS;
787 return *recvd > 0 ? ERROR_SUCCESS : ERROR_INTERNET_CONNECTION_ABORTED;
789 return ERROR_NOT_SUPPORTED;
794 /******************************************************************************
795 * NETCON_query_data_available
796 * Returns the number of bytes of peeked data plus the number of bytes of
797 * queued, but unread data.
799 BOOL NETCON_query_data_available(netconn_t *connection, DWORD *available)
803 if (!connection->useSSL)
807 int retval = ioctlsocket(connection->socketFD, FIONREAD, &unread);
810 TRACE("%d bytes of queued, but unread data\n", unread);
811 *available += unread;
818 *available = connection->ssl_s ? pSSL_pending(connection->ssl_s) : 0;
824 BOOL NETCON_is_alive(netconn_t *netconn)
830 len = recv(netconn->socketFD, &b, 1, MSG_PEEK|MSG_DONTWAIT);
831 return len == 1 || (len == -1 && errno == EWOULDBLOCK);
832 #elif defined(__MINGW32__) || defined(_MSC_VER)
838 if(!ioctlsocket(netconn->socketFD, FIONBIO, &mode))
841 len = recv(netconn->socketFD, &b, 1, MSG_PEEK);
844 if(!ioctlsocket(netconn->socketFD, FIONBIO, &mode))
847 return len == 1 || (len == -1 && errno == WSAEWOULDBLOCK);
849 FIXME("not supported on this platform\n");
854 LPCVOID NETCON_GetCert(netconn_t *connection)
860 if (!connection->ssl_s)
863 cert = pSSL_get_peer_certificate(connection->ssl_s);
864 r = X509_to_cert_context(cert);
871 int NETCON_GetCipherStrength(netconn_t *connection)
874 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
875 const SSL_CIPHER *cipher;
881 if (!connection->ssl_s)
883 cipher = pSSL_get_current_cipher(connection->ssl_s);
886 pSSL_CIPHER_get_bits(cipher, &bits);
893 DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, int value)
898 /* value is in milliseconds, convert to struct timeval */
899 tv.tv_sec = value / 1000;
900 tv.tv_usec = (value % 1000) * 1000;
902 result = setsockopt(connection->socketFD, SOL_SOCKET,
903 send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv,
908 WARN("setsockopt failed (%s)\n", strerror(errno));
909 return sock_get_error(errno);
912 return ERROR_SUCCESS;