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_OPENSSL_SSL_H
62 # include <openssl/ssl.h>
63 # include <openssl/opensslv.h>
74 #include "wine/library.h"
81 #include "wine/debug.h"
84 /* To avoid conflicts with the Unix socket headers. we only need it for
85 * the error codes anyway. */
89 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
92 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
95 * This should use winsock - To use winsock the functions will have to change a bit
96 * as they are designed for unix sockets.
97 * SSL stuff should use crypt32.dll
102 #include <openssl/err.h>
104 static void *OpenSSL_ssl_handle;
105 static void *OpenSSL_crypto_handle;
107 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER > 0x10000000)
108 static const SSL_METHOD *meth;
110 static SSL_METHOD *meth;
113 static int hostname_idx;
114 static int error_idx;
117 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
119 /* OpenSSL functions that we use */
120 MAKE_FUNCPTR(SSL_library_init);
121 MAKE_FUNCPTR(SSL_load_error_strings);
122 MAKE_FUNCPTR(SSLv23_method);
123 MAKE_FUNCPTR(SSL_CTX_free);
124 MAKE_FUNCPTR(SSL_CTX_new);
125 MAKE_FUNCPTR(SSL_new);
126 MAKE_FUNCPTR(SSL_free);
127 MAKE_FUNCPTR(SSL_set_fd);
128 MAKE_FUNCPTR(SSL_connect);
129 MAKE_FUNCPTR(SSL_shutdown);
130 MAKE_FUNCPTR(SSL_write);
131 MAKE_FUNCPTR(SSL_read);
132 MAKE_FUNCPTR(SSL_pending);
133 MAKE_FUNCPTR(SSL_get_error);
134 MAKE_FUNCPTR(SSL_get_ex_new_index);
135 MAKE_FUNCPTR(SSL_get_ex_data);
136 MAKE_FUNCPTR(SSL_set_ex_data);
137 MAKE_FUNCPTR(SSL_get_ex_data_X509_STORE_CTX_idx);
138 MAKE_FUNCPTR(SSL_get_peer_certificate);
139 MAKE_FUNCPTR(SSL_CTX_get_timeout);
140 MAKE_FUNCPTR(SSL_CTX_set_timeout);
141 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
142 MAKE_FUNCPTR(SSL_CTX_set_verify);
143 MAKE_FUNCPTR(SSL_get_current_cipher);
144 MAKE_FUNCPTR(SSL_CIPHER_get_bits);
146 /* OpenSSL's libcrypto functions that we use */
147 MAKE_FUNCPTR(BIO_new_fp);
148 MAKE_FUNCPTR(CRYPTO_num_locks);
149 MAKE_FUNCPTR(CRYPTO_set_id_callback);
150 MAKE_FUNCPTR(CRYPTO_set_locking_callback);
151 MAKE_FUNCPTR(ERR_free_strings);
152 MAKE_FUNCPTR(ERR_get_error);
153 MAKE_FUNCPTR(ERR_error_string);
154 MAKE_FUNCPTR(X509_STORE_CTX_get_ex_data);
155 MAKE_FUNCPTR(X509_STORE_CTX_get_chain);
156 MAKE_FUNCPTR(i2d_X509);
157 MAKE_FUNCPTR(sk_num);
158 MAKE_FUNCPTR(sk_value);
161 static CRITICAL_SECTION *ssl_locks;
162 static unsigned int num_ssl_locks;
164 static unsigned long ssl_thread_id(void)
166 return GetCurrentThreadId();
169 static void ssl_lock_callback(int mode, int type, const char *file, int line)
171 if (mode & CRYPTO_LOCK)
172 EnterCriticalSection(&ssl_locks[type]);
174 LeaveCriticalSection(&ssl_locks[type]);
177 static PCCERT_CONTEXT X509_to_cert_context(X509 *cert)
179 unsigned char* buffer,*p;
181 BOOL malloced = FALSE;
185 len = pi2d_X509(cert,&p);
187 * SSL 0.9.7 and above malloc the buffer if it is null.
188 * however earlier version do not and so we would need to alloc the buffer.
190 * see the i2d_X509 man page for more details.
194 buffer = heap_alloc(len);
196 len = pi2d_X509(cert,&p);
204 ret = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
209 HeapFree(GetProcessHeap(),0,buffer);
214 static DWORD netconn_verify_cert(PCCERT_CONTEXT cert, HCERTSTORE store,
215 WCHAR *server, DWORD security_flags)
218 CERT_CHAIN_PARA chainPara = { sizeof(chainPara), { 0 } };
219 PCCERT_CHAIN_CONTEXT chain;
220 char oid_server_auth[] = szOID_PKIX_KP_SERVER_AUTH;
221 char *server_auth[] = { oid_server_auth };
222 DWORD err = ERROR_SUCCESS, chainFlags = 0;
224 TRACE("verifying %s\n", debugstr_w(server));
225 chainPara.RequestedUsage.Usage.cUsageIdentifier = 1;
226 chainPara.RequestedUsage.Usage.rgpszUsageIdentifier = server_auth;
227 if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION))
228 chainFlags |= CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
229 if ((ret = CertGetCertificateChain(NULL, cert, NULL, store, &chainPara,
230 chainFlags, NULL, &chain)))
232 if (chain->TrustStatus.dwErrorStatus)
234 static const DWORD supportedErrors =
235 CERT_TRUST_IS_NOT_TIME_VALID |
236 CERT_TRUST_IS_UNTRUSTED_ROOT |
237 CERT_TRUST_IS_OFFLINE_REVOCATION |
238 CERT_TRUST_REVOCATION_STATUS_UNKNOWN |
239 CERT_TRUST_IS_REVOKED |
240 CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
242 if (chain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_NOT_TIME_VALID &&
243 !(security_flags & SECURITY_FLAG_IGNORE_CERT_DATE_INVALID))
244 err = ERROR_INTERNET_SEC_CERT_DATE_INVALID;
245 else if (chain->TrustStatus.dwErrorStatus &
246 CERT_TRUST_IS_UNTRUSTED_ROOT &&
247 !(security_flags & SECURITY_FLAG_IGNORE_UNKNOWN_CA))
248 err = ERROR_INTERNET_INVALID_CA;
249 else if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION) &&
250 ((chain->TrustStatus.dwErrorStatus &
251 CERT_TRUST_IS_OFFLINE_REVOCATION) ||
252 (chain->TrustStatus.dwErrorStatus &
253 CERT_TRUST_REVOCATION_STATUS_UNKNOWN)))
254 err = ERROR_INTERNET_SEC_CERT_NO_REV;
255 else if (!(security_flags & SECURITY_FLAG_IGNORE_REVOCATION) &&
256 (chain->TrustStatus.dwErrorStatus & CERT_TRUST_IS_REVOKED))
257 err = ERROR_INTERNET_SEC_CERT_REVOKED;
258 else if (!(security_flags & SECURITY_FLAG_IGNORE_WRONG_USAGE) &&
259 (chain->TrustStatus.dwErrorStatus &
260 CERT_TRUST_IS_NOT_VALID_FOR_USAGE))
261 err = ERROR_INTERNET_SEC_INVALID_CERT;
262 else if (chain->TrustStatus.dwErrorStatus & ~supportedErrors)
263 err = ERROR_INTERNET_SEC_INVALID_CERT;
267 CERT_CHAIN_POLICY_PARA policyPara;
268 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara;
269 CERT_CHAIN_POLICY_STATUS policyStatus;
270 CERT_CHAIN_CONTEXT chainCopy;
272 /* Clear chain->TrustStatus.dwErrorStatus so
273 * CertVerifyCertificateChainPolicy will verify additional checks
274 * rather than stopping with an existing, ignored error.
276 memcpy(&chainCopy, chain, sizeof(chainCopy));
277 chainCopy.TrustStatus.dwErrorStatus = 0;
278 sslExtraPolicyPara.u.cbSize = sizeof(sslExtraPolicyPara);
279 sslExtraPolicyPara.dwAuthType = AUTHTYPE_SERVER;
280 sslExtraPolicyPara.pwszServerName = server;
281 sslExtraPolicyPara.fdwChecks = security_flags;
282 policyPara.cbSize = sizeof(policyPara);
283 policyPara.dwFlags = 0;
284 policyPara.pvExtraPolicyPara = &sslExtraPolicyPara;
285 ret = CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL,
286 &chainCopy, &policyPara, &policyStatus);
287 /* Any error in the policy status indicates that the
288 * policy couldn't be verified.
290 if (ret && policyStatus.dwError)
292 if (policyStatus.dwError == CERT_E_CN_NO_MATCH)
293 err = ERROR_INTERNET_SEC_CERT_CN_INVALID;
295 err = ERROR_INTERNET_SEC_INVALID_CERT;
298 CertFreeCertificateChain(chain);
300 TRACE("returning %08x\n", err);
304 static int netconn_secure_verify(int preverify_ok, X509_STORE_CTX *ctx)
309 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
310 CERT_STORE_CREATE_NEW_FLAG, NULL);
313 ssl = pX509_STORE_CTX_get_ex_data(ctx,
314 pSSL_get_ex_data_X509_STORE_CTX_idx());
315 server = pSSL_get_ex_data(ssl, hostname_idx);
316 conn = pSSL_get_ex_data(ssl, conn_idx);
321 PCCERT_CONTEXT endCert = NULL;
322 struct stack_st *chain = (struct stack_st *)pX509_STORE_CTX_get_chain( ctx );
325 for (i = 0; ret && i < psk_num(chain); i++)
327 PCCERT_CONTEXT context;
329 cert = (X509 *)psk_value(chain, i);
330 if ((context = X509_to_cert_context(cert)))
333 ret = CertAddCertificateContextToStore(store, context,
334 CERT_STORE_ADD_ALWAYS, &endCert);
336 ret = CertAddCertificateContextToStore(store, context,
337 CERT_STORE_ADD_ALWAYS, NULL);
338 CertFreeCertificateContext(context);
341 if (!endCert) ret = FALSE;
344 DWORD_PTR err = netconn_verify_cert(endCert, store, server,
345 conn->security_flags);
349 pSSL_set_ex_data(ssl, error_idx, (void *)err);
353 CertFreeCertificateContext(endCert);
354 CertCloseStore(store, 0);
361 static CRITICAL_SECTION init_ssl_cs;
362 static CRITICAL_SECTION_DEBUG init_ssl_cs_debug =
365 { &init_ssl_cs_debug.ProcessLocksList,
366 &init_ssl_cs_debug.ProcessLocksList },
367 0, 0, { (DWORD_PTR)(__FILE__ ": init_ssl_cs") }
369 static CRITICAL_SECTION init_ssl_cs = { &init_ssl_cs_debug, -1, 0, 0, 0, 0 };
371 static DWORD init_openssl(void)
373 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
376 if(OpenSSL_ssl_handle)
377 return ERROR_SUCCESS;
379 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
380 if(!OpenSSL_ssl_handle) {
381 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
382 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
385 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
386 if(!OpenSSL_crypto_handle) {
387 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
388 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
391 /* mmm nice ugly macroness */
393 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
395 ERR("failed to load symbol %s\n", #x); \
396 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
399 DYNSSL(SSL_library_init);
400 DYNSSL(SSL_load_error_strings);
401 DYNSSL(SSLv23_method);
402 DYNSSL(SSL_CTX_free);
408 DYNSSL(SSL_shutdown);
412 DYNSSL(SSL_get_error);
413 DYNSSL(SSL_get_ex_new_index);
414 DYNSSL(SSL_get_ex_data);
415 DYNSSL(SSL_set_ex_data);
416 DYNSSL(SSL_get_ex_data_X509_STORE_CTX_idx);
417 DYNSSL(SSL_get_peer_certificate);
418 DYNSSL(SSL_CTX_get_timeout);
419 DYNSSL(SSL_CTX_set_timeout);
420 DYNSSL(SSL_CTX_set_default_verify_paths);
421 DYNSSL(SSL_CTX_set_verify);
422 DYNSSL(SSL_get_current_cipher);
423 DYNSSL(SSL_CIPHER_get_bits);
426 #define DYNCRYPTO(x) \
427 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
429 ERR("failed to load symbol %s\n", #x); \
430 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
433 DYNCRYPTO(BIO_new_fp);
434 DYNCRYPTO(CRYPTO_num_locks);
435 DYNCRYPTO(CRYPTO_set_id_callback);
436 DYNCRYPTO(CRYPTO_set_locking_callback);
437 DYNCRYPTO(ERR_free_strings);
438 DYNCRYPTO(ERR_get_error);
439 DYNCRYPTO(ERR_error_string);
440 DYNCRYPTO(X509_STORE_CTX_get_ex_data);
441 DYNCRYPTO(X509_STORE_CTX_get_chain);
448 pSSL_load_error_strings();
449 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
451 meth = pSSLv23_method();
452 ctx = pSSL_CTX_new(meth);
453 if(!pSSL_CTX_set_default_verify_paths(ctx)) {
454 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
455 pERR_error_string(pERR_get_error(), 0));
456 return ERROR_OUTOFMEMORY;
459 hostname_idx = pSSL_get_ex_new_index(0, (void *)"hostname index", NULL, NULL, NULL);
460 if(hostname_idx == -1) {
461 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
462 return ERROR_OUTOFMEMORY;
465 error_idx = pSSL_get_ex_new_index(0, (void *)"error index", NULL, NULL, NULL);
466 if(error_idx == -1) {
467 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
468 return ERROR_OUTOFMEMORY;
471 conn_idx = pSSL_get_ex_new_index(0, (void *)"netconn index", NULL, NULL, NULL);
473 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
474 return ERROR_OUTOFMEMORY;
477 pSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, netconn_secure_verify);
479 pCRYPTO_set_id_callback(ssl_thread_id);
480 num_ssl_locks = pCRYPTO_num_locks();
481 ssl_locks = HeapAlloc(GetProcessHeap(), 0, num_ssl_locks * sizeof(CRITICAL_SECTION));
483 return ERROR_OUTOFMEMORY;
485 for(i = 0; i < num_ssl_locks; i++)
486 InitializeCriticalSection(&ssl_locks[i]);
487 pCRYPTO_set_locking_callback(ssl_lock_callback);
489 return ERROR_SUCCESS;
491 FIXME("can't use SSL, not compiled in.\n");
492 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
496 DWORD NETCON_init(netconn_t *connection, BOOL useSSL)
498 DWORD res = ERROR_SUCCESS;
500 connection->useSSL = useSSL;
501 connection->socketFD = -1;
504 TRACE("using SSL connection\n");
506 EnterCriticalSection(&init_ssl_cs);
507 res = init_openssl();
508 LeaveCriticalSection(&init_ssl_cs);
514 void NETCON_unload(void)
516 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
517 if (OpenSSL_crypto_handle)
520 wine_dlclose(OpenSSL_crypto_handle, NULL, 0);
522 if (OpenSSL_ssl_handle)
526 wine_dlclose(OpenSSL_ssl_handle, NULL, 0);
531 for (i = 0; i < num_ssl_locks; i++) DeleteCriticalSection(&ssl_locks[i]);
532 HeapFree(GetProcessHeap(), 0, ssl_locks);
537 BOOL NETCON_connected(netconn_t *connection)
539 return connection->socketFD != -1;
542 /* translate a unix error code into a winsock one */
543 int sock_get_error( int err )
545 #if !defined(__MINGW32__) && !defined (_MSC_VER)
548 case EINTR: return WSAEINTR;
549 case EBADF: return WSAEBADF;
551 case EACCES: return WSAEACCES;
552 case EFAULT: return WSAEFAULT;
553 case EINVAL: return WSAEINVAL;
554 case EMFILE: return WSAEMFILE;
555 case EWOULDBLOCK: return WSAEWOULDBLOCK;
556 case EINPROGRESS: return WSAEINPROGRESS;
557 case EALREADY: return WSAEALREADY;
558 case ENOTSOCK: return WSAENOTSOCK;
559 case EDESTADDRREQ: return WSAEDESTADDRREQ;
560 case EMSGSIZE: return WSAEMSGSIZE;
561 case EPROTOTYPE: return WSAEPROTOTYPE;
562 case ENOPROTOOPT: return WSAENOPROTOOPT;
563 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
564 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
565 case EOPNOTSUPP: return WSAEOPNOTSUPP;
566 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
567 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
568 case EADDRINUSE: return WSAEADDRINUSE;
569 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
570 case ENETDOWN: return WSAENETDOWN;
571 case ENETUNREACH: return WSAENETUNREACH;
572 case ENETRESET: return WSAENETRESET;
573 case ECONNABORTED: return WSAECONNABORTED;
575 case ECONNRESET: return WSAECONNRESET;
576 case ENOBUFS: return WSAENOBUFS;
577 case EISCONN: return WSAEISCONN;
578 case ENOTCONN: return WSAENOTCONN;
579 case ESHUTDOWN: return WSAESHUTDOWN;
580 case ETOOMANYREFS: return WSAETOOMANYREFS;
581 case ETIMEDOUT: return WSAETIMEDOUT;
582 case ECONNREFUSED: return WSAECONNREFUSED;
583 case ELOOP: return WSAELOOP;
584 case ENAMETOOLONG: return WSAENAMETOOLONG;
585 case EHOSTDOWN: return WSAEHOSTDOWN;
586 case EHOSTUNREACH: return WSAEHOSTUNREACH;
587 case ENOTEMPTY: return WSAENOTEMPTY;
589 case EPROCLIM: return WSAEPROCLIM;
592 case EUSERS: return WSAEUSERS;
595 case EDQUOT: return WSAEDQUOT;
598 case ESTALE: return WSAESTALE;
601 case EREMOTE: return WSAEREMOTE;
603 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
609 /******************************************************************************
611 * Basically calls 'socket()'
613 DWORD NETCON_create(netconn_t *connection, int domain,
614 int type, int protocol)
617 if (connection->ssl_s)
618 return ERROR_NOT_SUPPORTED;
621 connection->socketFD = socket(domain, type, protocol);
622 if (connection->socketFD == -1)
623 return sock_get_error(errno);
625 return ERROR_SUCCESS;
628 /******************************************************************************
630 * Basically calls 'close()' unless we should use SSL
632 DWORD NETCON_close(netconn_t *connection)
636 if (!NETCON_connected(connection)) return ERROR_SUCCESS;
639 if (connection->ssl_s)
641 pSSL_shutdown(connection->ssl_s);
642 pSSL_free(connection->ssl_s);
643 connection->ssl_s = NULL;
647 result = closesocket(connection->socketFD);
648 connection->socketFD = -1;
651 return sock_get_error(errno);
652 return ERROR_SUCCESS;
655 /******************************************************************************
656 * NETCON_secure_connect
657 * Initiates a secure connection over an existing plaintext connection.
659 DWORD NETCON_secure_connect(netconn_t *connection, LPWSTR hostname)
662 DWORD res = ERROR_NOT_SUPPORTED;
665 /* can't connect if we are already connected */
666 if (connection->ssl_s)
668 ERR("already connected\n");
669 return ERROR_INTERNET_CANNOT_CONNECT;
672 ssl_s = pSSL_new(ctx);
675 ERR("SSL_new failed: %s\n",
676 pERR_error_string(pERR_get_error(), 0));
677 return ERROR_OUTOFMEMORY;
680 if (!pSSL_set_fd(ssl_s, connection->socketFD))
682 ERR("SSL_set_fd failed: %s\n",
683 pERR_error_string(pERR_get_error(), 0));
684 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
688 if (!pSSL_set_ex_data(ssl_s, hostname_idx, hostname))
690 ERR("SSL_set_ex_data failed: %s\n",
691 pERR_error_string(pERR_get_error(), 0));
692 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
695 if (!pSSL_set_ex_data(ssl_s, conn_idx, connection))
697 ERR("SSL_set_ex_data failed: %s\n",
698 pERR_error_string(pERR_get_error(), 0));
699 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
702 if (pSSL_connect(ssl_s) <= 0)
704 res = (DWORD_PTR)pSSL_get_ex_data(ssl_s, error_idx);
706 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
707 ERR("SSL_connect failed: %d\n", res);
711 connection->ssl_s = ssl_s;
712 return ERROR_SUCCESS;
717 pSSL_shutdown(ssl_s);
724 /******************************************************************************
726 * Connects to the specified address.
728 DWORD NETCON_connect(netconn_t *connection, const struct sockaddr *serv_addr,
729 unsigned int addrlen)
733 result = connect(connection->socketFD, serv_addr, addrlen);
736 WARN("Unable to connect to host (%s)\n", strerror(errno));
738 closesocket(connection->socketFD);
739 connection->socketFD = -1;
740 return sock_get_error(errno);
743 return ERROR_SUCCESS;
746 /******************************************************************************
748 * Basically calls 'send()' unless we should use SSL
749 * number of chars send is put in *sent
751 DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags,
754 if (!NETCON_connected(connection)) return ERROR_INTERNET_CONNECTION_ABORTED;
755 if (!connection->useSSL)
757 *sent = send(connection->socketFD, msg, len, flags);
759 return sock_get_error(errno);
760 return ERROR_SUCCESS;
765 if(!connection->ssl_s) {
766 FIXME("not connected\n");
767 return ERROR_NOT_SUPPORTED;
770 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
771 *sent = pSSL_write(connection->ssl_s, msg, len);
772 if (*sent < 1 && len)
773 return ERROR_INTERNET_CONNECTION_ABORTED;
774 return ERROR_SUCCESS;
776 return ERROR_NOT_SUPPORTED;
781 /******************************************************************************
783 * Basically calls 'recv()' unless we should use SSL
784 * number of chars received is put in *recvd
786 DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, int flags,
787 int *recvd /* out */)
790 if (!NETCON_connected(connection)) return ERROR_INTERNET_CONNECTION_ABORTED;
792 return ERROR_SUCCESS;
793 if (!connection->useSSL)
795 *recvd = recv(connection->socketFD, buf, len, flags);
797 NETCON_close(connection);
798 return *recvd == -1 ? sock_get_error(errno) : ERROR_SUCCESS;
803 if(!connection->ssl_s) {
804 FIXME("not connected\n");
805 return ERROR_NOT_SUPPORTED;
807 *recvd = pSSL_read(connection->ssl_s, buf, len);
809 /* Check if EOF was received */
810 if(!*recvd && (pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_ZERO_RETURN
811 || pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_SYSCALL)) {
812 NETCON_close(connection);
813 return ERROR_SUCCESS;
816 return *recvd > 0 ? ERROR_SUCCESS : ERROR_INTERNET_CONNECTION_ABORTED;
818 return ERROR_NOT_SUPPORTED;
823 /******************************************************************************
824 * NETCON_query_data_available
825 * Returns the number of bytes of peeked data plus the number of bytes of
826 * queued, but unread data.
828 BOOL NETCON_query_data_available(netconn_t *connection, DWORD *available)
831 if (!NETCON_connected(connection))
834 if (!connection->useSSL)
838 int retval = ioctlsocket(connection->socketFD, FIONREAD, &unread);
841 TRACE("%d bytes of queued, but unread data\n", unread);
842 *available += unread;
849 *available = connection->ssl_s ? pSSL_pending(connection->ssl_s) : 0;
855 LPCVOID NETCON_GetCert(netconn_t *connection)
861 if (!connection->ssl_s)
864 cert = pSSL_get_peer_certificate(connection->ssl_s);
865 r = X509_to_cert_context(cert);
872 int NETCON_GetCipherStrength(netconn_t *connection)
875 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
876 const SSL_CIPHER *cipher;
882 if (!connection->ssl_s)
884 cipher = pSSL_get_current_cipher(connection->ssl_s);
887 pSSL_CIPHER_get_bits(cipher, &bits);
894 DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, int value)
899 /* value is in milliseconds, convert to struct timeval */
900 tv.tv_sec = value / 1000;
901 tv.tv_usec = (value % 1000) * 1000;
903 result = setsockopt(connection->socketFD, SOL_SOCKET,
904 send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv,
909 WARN("setsockopt failed (%s)\n", strerror(errno));
910 return sock_get_error(errno);
913 return ERROR_SUCCESS;