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"
84 #include "wine/debug.h"
87 /* To avoid conflicts with the Unix socket headers. we only need it for
88 * the error codes anyway. */
92 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
95 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
98 * This should use winsock - To use winsock the functions will have to change a bit
99 * as they are designed for unix sockets.
100 * SSL stuff should use crypt32.dll
105 #include <openssl/err.h>
107 static void *OpenSSL_ssl_handle;
108 static void *OpenSSL_crypto_handle;
110 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER > 0x10000000)
111 static const SSL_METHOD *meth;
113 static SSL_METHOD *meth;
116 static int error_idx;
119 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
121 /* OpenSSL functions that we use */
122 MAKE_FUNCPTR(SSL_library_init);
123 MAKE_FUNCPTR(SSL_load_error_strings);
124 MAKE_FUNCPTR(SSLv23_method);
125 MAKE_FUNCPTR(SSL_CTX_free);
126 MAKE_FUNCPTR(SSL_CTX_new);
127 MAKE_FUNCPTR(SSL_CTX_ctrl);
128 MAKE_FUNCPTR(SSL_new);
129 MAKE_FUNCPTR(SSL_free);
130 MAKE_FUNCPTR(SSL_ctrl);
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);
217 #endif /* SONAME_LIBSSL */
219 static DWORD netconn_verify_cert(netconn_t *conn, PCCERT_CONTEXT cert, HCERTSTORE store)
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, errors;
228 static const DWORD supportedErrors =
229 CERT_TRUST_IS_NOT_TIME_VALID |
230 CERT_TRUST_IS_UNTRUSTED_ROOT |
231 CERT_TRUST_IS_PARTIAL_CHAIN |
232 CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
234 TRACE("verifying %s\n", debugstr_w(conn->server->name));
236 chainPara.RequestedUsage.Usage.cUsageIdentifier = 1;
237 chainPara.RequestedUsage.Usage.rgpszUsageIdentifier = server_auth;
238 if (!(ret = CertGetCertificateChain(NULL, cert, NULL, store, &chainPara, 0, NULL, &chain))) {
240 return GetLastError();
243 errors = chain->TrustStatus.dwErrorStatus;
246 /* This seems strange, but that's what tests show */
247 if(errors & CERT_TRUST_IS_PARTIAL_CHAIN) {
248 WARN("ERROR_INTERNET_SEC_CERT_REV_FAILED\n");
249 err = ERROR_INTERNET_SEC_CERT_REV_FAILED;
250 if(conn->mask_errors)
251 conn->security_flags |= _SECURITY_FLAG_CERT_REV_FAILED;
252 if(!(conn->security_flags & SECURITY_FLAG_IGNORE_REVOCATION))
256 if (chain->TrustStatus.dwErrorStatus & ~supportedErrors) {
257 WARN("error status %x\n", chain->TrustStatus.dwErrorStatus & ~supportedErrors);
258 err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_INVALID_CERT;
259 errors &= supportedErrors;
260 if(!conn->mask_errors)
262 WARN("unknown error flags\n");
265 if(errors & CERT_TRUST_IS_NOT_TIME_VALID) {
266 WARN("CERT_TRUST_IS_NOT_TIME_VALID\n");
267 if(!(conn->security_flags & SECURITY_FLAG_IGNORE_CERT_DATE_INVALID)) {
268 err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_CERT_DATE_INVALID;
269 if(!conn->mask_errors)
271 conn->security_flags |= _SECURITY_FLAG_CERT_INVALID_DATE;
273 errors &= ~CERT_TRUST_IS_NOT_TIME_VALID;
276 if(errors & CERT_TRUST_IS_UNTRUSTED_ROOT) {
277 WARN("CERT_TRUST_IS_UNTRUSTED_ROOT\n");
278 if(!(conn->security_flags & SECURITY_FLAG_IGNORE_UNKNOWN_CA)) {
279 err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_INVALID_CA;
280 if(!conn->mask_errors)
282 conn->security_flags |= _SECURITY_FLAG_CERT_INVALID_CA;
284 errors &= ~CERT_TRUST_IS_UNTRUSTED_ROOT;
287 if(errors & CERT_TRUST_IS_PARTIAL_CHAIN) {
288 WARN("CERT_TRUST_IS_PARTIAL_CHAIN\n");
289 if(!(conn->security_flags & SECURITY_FLAG_IGNORE_UNKNOWN_CA)) {
290 err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_INVALID_CA;
291 if(!conn->mask_errors)
293 conn->security_flags |= _SECURITY_FLAG_CERT_INVALID_CA;
295 errors &= ~CERT_TRUST_IS_PARTIAL_CHAIN;
298 if(errors & CERT_TRUST_IS_NOT_VALID_FOR_USAGE) {
299 WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE\n");
300 if(!(conn->security_flags & SECURITY_FLAG_IGNORE_WRONG_USAGE)) {
301 err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_INVALID_CERT;
302 if(!conn->mask_errors)
304 WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE, unknown error flags\n");
306 errors &= ~CERT_TRUST_IS_NOT_VALID_FOR_USAGE;
309 if(err == ERROR_INTERNET_SEC_CERT_REV_FAILED) {
310 assert(conn->security_flags & SECURITY_FLAG_IGNORE_REVOCATION);
315 if(!err || conn->mask_errors) {
316 CERT_CHAIN_POLICY_PARA policyPara;
317 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara;
318 CERT_CHAIN_POLICY_STATUS policyStatus;
319 CERT_CHAIN_CONTEXT chainCopy;
321 /* Clear chain->TrustStatus.dwErrorStatus so
322 * CertVerifyCertificateChainPolicy will verify additional checks
323 * rather than stopping with an existing, ignored error.
325 memcpy(&chainCopy, chain, sizeof(chainCopy));
326 chainCopy.TrustStatus.dwErrorStatus = 0;
327 sslExtraPolicyPara.u.cbSize = sizeof(sslExtraPolicyPara);
328 sslExtraPolicyPara.dwAuthType = AUTHTYPE_SERVER;
329 sslExtraPolicyPara.pwszServerName = conn->server->name;
330 sslExtraPolicyPara.fdwChecks = conn->security_flags;
331 policyPara.cbSize = sizeof(policyPara);
332 policyPara.dwFlags = 0;
333 policyPara.pvExtraPolicyPara = &sslExtraPolicyPara;
334 ret = CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL,
335 &chainCopy, &policyPara, &policyStatus);
336 /* Any error in the policy status indicates that the
337 * policy couldn't be verified.
340 if(policyStatus.dwError == CERT_E_CN_NO_MATCH) {
341 WARN("CERT_E_CN_NO_MATCH\n");
342 if(conn->mask_errors)
343 conn->security_flags |= _SECURITY_FLAG_CERT_INVALID_CN;
344 err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_CERT_CN_INVALID;
345 }else if(policyStatus.dwError) {
346 WARN("policyStatus.dwError %x\n", policyStatus.dwError);
347 if(conn->mask_errors)
348 WARN("unknown error flags for policy status %x\n", policyStatus.dwError);
349 err = conn->mask_errors && err ? ERROR_INTERNET_SEC_CERT_ERRORS : ERROR_INTERNET_SEC_INVALID_CERT;
352 err = GetLastError();
357 WARN("failed %u\n", err);
358 CertFreeCertificateChain(chain);
359 if(conn->server->cert_chain) {
360 CertFreeCertificateChain(conn->server->cert_chain);
361 conn->server->cert_chain = NULL;
363 if(conn->mask_errors)
364 conn->server->security_flags |= conn->security_flags & _SECURITY_ERROR_FLAGS_MASK;
368 /* FIXME: Reuse cached chain */
369 if(conn->server->cert_chain)
370 CertFreeCertificateChain(chain);
372 conn->server->cert_chain = chain;
373 return ERROR_SUCCESS;
377 static int netconn_secure_verify(int preverify_ok, X509_STORE_CTX *ctx)
381 HCERTSTORE store = CertOpenStore(CERT_STORE_PROV_MEMORY, 0, 0,
382 CERT_STORE_CREATE_NEW_FLAG, NULL);
385 ssl = pX509_STORE_CTX_get_ex_data(ctx,
386 pSSL_get_ex_data_X509_STORE_CTX_idx());
387 conn = pSSL_get_ex_data(ssl, conn_idx);
392 PCCERT_CONTEXT endCert = NULL;
393 struct stack_st *chain = (struct stack_st *)pX509_STORE_CTX_get_chain( ctx );
396 for (i = 0; ret && i < psk_num(chain); i++)
398 PCCERT_CONTEXT context;
400 cert = (X509 *)psk_value(chain, i);
401 if ((context = X509_to_cert_context(cert)))
403 ret = CertAddCertificateContextToStore(store, context,
404 CERT_STORE_ADD_ALWAYS, i ? NULL : &endCert);
405 CertFreeCertificateContext(context);
408 if (!endCert) ret = FALSE;
411 DWORD_PTR err = netconn_verify_cert(conn, endCert, store);
415 pSSL_set_ex_data(ssl, error_idx, (void *)err);
419 CertFreeCertificateContext(endCert);
420 CertCloseStore(store, 0);
425 static long get_tls_option(void) {
426 long tls_option = SSL_OP_NO_SSLv2; /* disable SSLv2 for security reason, secur32/Schannel(GnuTLS) don't support it */
427 #ifdef SSL_OP_NO_TLSv1_2
428 DWORD type, val, size;
429 HKEY hkey,tls12_client,tls11_client;
431 const WCHAR Schannel_Prot[] = { /* SYSTEM\\CurrentControlSet\\Control\\SecurityProviders\\SCANNEL\\Protocols */
432 'S','Y','S','T','E','M','\\',
433 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
434 'C','o','n','t','r','o','l','\\',
435 'S','e','c','u','r','i','t','y','P','r','o','v','i','d','e','r','s','\\',
436 'S','C','H','A','N','N','E','L','\\',
437 'P','r','o','t','o','c','o','l','s',0 };
438 const WCHAR TLS12_Client[] = {'T','L','S',' ','1','.','2','\\','C','l','i','e','n','t',0};
439 const WCHAR TLS11_Client[] = {'T','L','S',' ','1','.','1','\\','C','l','i','e','n','t',0};
440 const WCHAR DisabledByDefault[] = {'D','i','s','a','b','l','e','d','B','y','D','e','f','a','u','l','t',0};
442 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE,
445 if (res != ERROR_SUCCESS) { /* enabled TLSv1.1/1.2 when no registry entry */
448 if (RegOpenKeyExW(hkey, TLS12_Client, 0, KEY_READ, &tls12_client) == ERROR_SUCCESS) {
449 size = sizeof(DWORD);
450 if (RegQueryValueExW(tls12_client, DisabledByDefault, NULL, &type, (LPBYTE) &val, &size) == ERROR_SUCCESS
451 && type == REG_DWORD) {
452 tls_option |= val?SSL_OP_NO_TLSv1_2:0;
454 RegCloseKey(tls12_client);
456 if (RegOpenKeyExW(hkey, TLS11_Client, 0, KEY_READ, &tls11_client) == ERROR_SUCCESS) {
457 size = sizeof(DWORD);
458 if (RegQueryValueExW(tls11_client, DisabledByDefault, NULL, &type, (LPBYTE) &val, &size) == ERROR_SUCCESS
459 && type == REG_DWORD) {
460 tls_option |= val?SSL_OP_NO_TLSv1_1:0;
462 RegCloseKey(tls11_client);
469 static CRITICAL_SECTION init_ssl_cs;
470 static CRITICAL_SECTION_DEBUG init_ssl_cs_debug =
473 { &init_ssl_cs_debug.ProcessLocksList,
474 &init_ssl_cs_debug.ProcessLocksList },
475 0, 0, { (DWORD_PTR)(__FILE__ ": init_ssl_cs") }
477 static CRITICAL_SECTION init_ssl_cs = { &init_ssl_cs_debug, -1, 0, 0, 0, 0 };
479 static DWORD init_openssl(void)
481 #ifdef SONAME_LIBCRYPTO
484 if(OpenSSL_ssl_handle)
485 return ERROR_SUCCESS;
487 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
488 if(!OpenSSL_ssl_handle) {
489 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
490 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
493 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
494 if(!OpenSSL_crypto_handle) {
495 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
496 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
499 /* mmm nice ugly macroness */
501 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
503 ERR("failed to load symbol %s\n", #x); \
504 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
507 DYNSSL(SSL_library_init);
508 DYNSSL(SSL_load_error_strings);
509 DYNSSL(SSLv23_method);
510 DYNSSL(SSL_CTX_free);
512 DYNSSL(SSL_CTX_ctrl);
518 DYNSSL(SSL_shutdown);
522 DYNSSL(SSL_get_error);
523 DYNSSL(SSL_get_ex_new_index);
524 DYNSSL(SSL_get_ex_data);
525 DYNSSL(SSL_set_ex_data);
526 DYNSSL(SSL_get_ex_data_X509_STORE_CTX_idx);
527 DYNSSL(SSL_get_peer_certificate);
528 DYNSSL(SSL_CTX_get_timeout);
529 DYNSSL(SSL_CTX_set_timeout);
530 DYNSSL(SSL_CTX_set_default_verify_paths);
531 DYNSSL(SSL_CTX_set_verify);
532 DYNSSL(SSL_get_current_cipher);
533 DYNSSL(SSL_CIPHER_get_bits);
536 #define DYNCRYPTO(x) \
537 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
539 ERR("failed to load symbol %s\n", #x); \
540 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
543 DYNCRYPTO(BIO_new_fp);
544 DYNCRYPTO(CRYPTO_num_locks);
545 DYNCRYPTO(CRYPTO_set_id_callback);
546 DYNCRYPTO(CRYPTO_set_locking_callback);
547 DYNCRYPTO(ERR_free_strings);
548 DYNCRYPTO(ERR_get_error);
549 DYNCRYPTO(ERR_error_string);
550 DYNCRYPTO(X509_STORE_CTX_get_ex_data);
551 DYNCRYPTO(X509_STORE_CTX_get_chain);
557 #define pSSL_CTX_set_options(ctx,op) \
558 pSSL_CTX_ctrl((ctx),SSL_CTRL_OPTIONS,(op),NULL)
559 #define pSSL_set_options(ssl,op) \
560 pSSL_ctrl((ssl),SSL_CTRL_OPTIONS,(op),NULL)
563 pSSL_load_error_strings();
564 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
566 meth = pSSLv23_method();
567 ctx = pSSL_CTX_new(meth);
568 pSSL_CTX_set_options(ctx, get_tls_option());
569 if(!pSSL_CTX_set_default_verify_paths(ctx)) {
570 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
571 pERR_error_string(pERR_get_error(), 0));
572 return ERROR_OUTOFMEMORY;
575 error_idx = pSSL_get_ex_new_index(0, (void *)"error index", NULL, NULL, NULL);
576 if(error_idx == -1) {
577 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
578 return ERROR_OUTOFMEMORY;
581 conn_idx = pSSL_get_ex_new_index(0, (void *)"netconn index", NULL, NULL, NULL);
583 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
584 return ERROR_OUTOFMEMORY;
587 pSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, netconn_secure_verify);
589 pCRYPTO_set_id_callback(ssl_thread_id);
590 num_ssl_locks = pCRYPTO_num_locks();
591 ssl_locks = heap_alloc(num_ssl_locks * sizeof(CRITICAL_SECTION));
593 return ERROR_OUTOFMEMORY;
595 for(i = 0; i < num_ssl_locks; i++)
597 InitializeCriticalSection(&ssl_locks[i]);
598 ssl_locks[i].DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ssl_locks");
600 pCRYPTO_set_locking_callback(ssl_lock_callback);
602 return ERROR_SUCCESS;
604 FIXME("can't use SSL, libcrypto not compiled in.\n");
605 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
610 static SecHandle cred_handle, compat_cred_handle;
611 static BOOL cred_handle_initialized, have_compat_cred_handle;
613 static CRITICAL_SECTION init_sechandle_cs;
614 static CRITICAL_SECTION_DEBUG init_sechandle_cs_debug = {
615 0, 0, &init_sechandle_cs,
616 { &init_sechandle_cs_debug.ProcessLocksList,
617 &init_sechandle_cs_debug.ProcessLocksList },
618 0, 0, { (DWORD_PTR)(__FILE__ ": init_sechandle_cs") }
620 static CRITICAL_SECTION init_sechandle_cs = { &init_sechandle_cs_debug, -1, 0, 0, 0, 0 };
622 static BOOL ensure_cred_handle(void)
624 SECURITY_STATUS res = SEC_E_OK;
626 EnterCriticalSection(&init_sechandle_cs);
628 if(!cred_handle_initialized) {
629 SCHANNEL_CRED cred = {SCHANNEL_CRED_VERSION};
630 SecPkgCred_SupportedProtocols prots;
632 res = AcquireCredentialsHandleW(NULL, (WCHAR*)UNISP_NAME_W, SECPKG_CRED_OUTBOUND, NULL, &cred,
633 NULL, NULL, &cred_handle, NULL);
634 if(res == SEC_E_OK) {
635 res = QueryCredentialsAttributesA(&cred_handle, SECPKG_ATTR_SUPPORTED_PROTOCOLS, &prots);
636 if(res != SEC_E_OK || (prots.grbitProtocol & SP_PROT_TLS1_1PLUS_CLIENT)) {
637 cred.grbitEnabledProtocols = prots.grbitProtocol & ~SP_PROT_TLS1_1PLUS_CLIENT;
638 res = AcquireCredentialsHandleW(NULL, (WCHAR*)UNISP_NAME_W, SECPKG_CRED_OUTBOUND, NULL, &cred,
639 NULL, NULL, &compat_cred_handle, NULL);
640 have_compat_cred_handle = res == SEC_E_OK;
644 cred_handle_initialized = res == SEC_E_OK;
647 LeaveCriticalSection(&init_sechandle_cs);
649 if(res != SEC_E_OK) {
650 WARN("Failed: %08x\n", res);
657 #endif /* SONAME_LIBSSL */
659 static DWORD create_netconn_socket(server_t *server, netconn_t *netconn, DWORD timeout)
664 assert(server->addr_len);
665 result = netconn->socket = socket(server->addr.ss_family, SOCK_STREAM, 0);
668 ioctlsocket(netconn->socket, FIONBIO, &flag);
669 result = connect(netconn->socket, (struct sockaddr*)&server->addr, server->addr_len);
672 if (sock_get_error(errno) == WSAEINPROGRESS) {
676 pfd.fd = netconn->socket;
677 pfd.events = POLLOUT;
678 res = poll(&pfd, 1, timeout);
681 closesocket(netconn->socket);
682 return ERROR_INTERNET_CANNOT_CONNECT;
687 socklen_t len = sizeof(err);
688 if (!getsockopt(netconn->socket, SOL_SOCKET, SO_ERROR, (void *)&err, &len) && !err)
694 closesocket(netconn->socket);
697 ioctlsocket(netconn->socket, FIONBIO, &flag);
701 return ERROR_INTERNET_CANNOT_CONNECT;
705 result = setsockopt(netconn->socket, IPPROTO_TCP, TCP_NODELAY, (void*)&flag, sizeof(flag));
707 WARN("setsockopt(TCP_NODELAY) failed\n");
710 return ERROR_SUCCESS;
713 DWORD create_netconn(BOOL useSSL, server_t *server, DWORD security_flags, BOOL mask_errors, DWORD timeout, netconn_t **ret)
722 TRACE("using SSL connection\n");
724 EnterCriticalSection(&init_ssl_cs);
725 res = init_openssl();
726 LeaveCriticalSection(&init_ssl_cs);
727 if(res != ERROR_SUCCESS)
732 netconn = heap_alloc_zero(sizeof(*netconn));
734 return ERROR_OUTOFMEMORY;
736 netconn->socket = -1;
737 netconn->security_flags = security_flags | server->security_flags;
738 netconn->mask_errors = mask_errors;
739 list_init(&netconn->pool_entry);
741 result = create_netconn_socket(server, netconn, timeout);
742 if (result != ERROR_SUCCESS) {
747 server_addref(server);
748 netconn->server = server;
753 void free_netconn(netconn_t *netconn)
755 server_release(netconn->server);
757 if (netconn->secure) {
759 pSSL_shutdown(netconn->ssl_s);
760 pSSL_free(netconn->ssl_s);
762 heap_free(netconn->ssl_buf);
763 netconn->ssl_buf = NULL;
764 DeleteSecurityContext(&netconn->ssl_ctx);
768 closesocket(netconn->socket);
772 void NETCON_unload(void)
774 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
775 if (OpenSSL_crypto_handle)
778 wine_dlclose(OpenSSL_crypto_handle, NULL, 0);
780 if (OpenSSL_ssl_handle)
784 wine_dlclose(OpenSSL_ssl_handle, NULL, 0);
789 for (i = 0; i < num_ssl_locks; i++)
791 ssl_locks[i].DebugInfo->Spare[0] = 0;
792 DeleteCriticalSection(&ssl_locks[i]);
794 heap_free(ssl_locks);
797 if(cred_handle_initialized)
798 FreeCredentialsHandle(&cred_handle);
799 if(have_compat_cred_handle)
800 FreeCredentialsHandle(&compat_cred_handle);
801 DeleteCriticalSection(&init_sechandle_cs);
805 /* translate a unix error code into a winsock one */
806 int sock_get_error( int err )
808 #if !defined(__MINGW32__) && !defined (_MSC_VER)
811 case EINTR: return WSAEINTR;
812 case EBADF: return WSAEBADF;
814 case EACCES: return WSAEACCES;
815 case EFAULT: return WSAEFAULT;
816 case EINVAL: return WSAEINVAL;
817 case EMFILE: return WSAEMFILE;
818 case EWOULDBLOCK: return WSAEWOULDBLOCK;
819 case EINPROGRESS: return WSAEINPROGRESS;
820 case EALREADY: return WSAEALREADY;
821 case ENOTSOCK: return WSAENOTSOCK;
822 case EDESTADDRREQ: return WSAEDESTADDRREQ;
823 case EMSGSIZE: return WSAEMSGSIZE;
824 case EPROTOTYPE: return WSAEPROTOTYPE;
825 case ENOPROTOOPT: return WSAENOPROTOOPT;
826 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
827 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
828 case EOPNOTSUPP: return WSAEOPNOTSUPP;
829 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
830 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
831 case EADDRINUSE: return WSAEADDRINUSE;
832 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
833 case ENETDOWN: return WSAENETDOWN;
834 case ENETUNREACH: return WSAENETUNREACH;
835 case ENETRESET: return WSAENETRESET;
836 case ECONNABORTED: return WSAECONNABORTED;
838 case ECONNRESET: return WSAECONNRESET;
839 case ENOBUFS: return WSAENOBUFS;
840 case EISCONN: return WSAEISCONN;
841 case ENOTCONN: return WSAENOTCONN;
842 case ESHUTDOWN: return WSAESHUTDOWN;
843 case ETOOMANYREFS: return WSAETOOMANYREFS;
844 case ETIMEDOUT: return WSAETIMEDOUT;
845 case ECONNREFUSED: return WSAECONNREFUSED;
846 case ELOOP: return WSAELOOP;
847 case ENAMETOOLONG: return WSAENAMETOOLONG;
848 case EHOSTDOWN: return WSAEHOSTDOWN;
849 case EHOSTUNREACH: return WSAEHOSTUNREACH;
850 case ENOTEMPTY: return WSAENOTEMPTY;
852 case EPROCLIM: return WSAEPROCLIM;
855 case EUSERS: return WSAEUSERS;
858 case EDQUOT: return WSAEDQUOT;
861 case ESTALE: return WSAESTALE;
864 case EREMOTE: return WSAEREMOTE;
866 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
872 static DWORD netcon_secure_connect_setup(netconn_t *connection, BOOL compat_mode)
880 tls_option = get_tls_option();
883 #ifdef SSL_OP_NO_TLSv1_2
884 tls_option |= SSL_OP_NO_TLSv1_1|SSL_OP_NO_TLSv1_2;
885 pSSL_CTX_set_options(ctx,SSL_OP_NO_TLSv1_1|SSL_OP_NO_TLSv1_2);
887 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
891 ssl_s = pSSL_new(ctx);
894 ERR("SSL_new failed: %s\n",
895 pERR_error_string(pERR_get_error(), 0));
896 return ERROR_OUTOFMEMORY;
899 pSSL_set_options(ssl_s, tls_option);
900 if (!pSSL_set_fd(ssl_s, connection->socket))
902 ERR("SSL_set_fd failed: %s\n",
903 pERR_error_string(pERR_get_error(), 0));
904 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
908 if (!pSSL_set_ex_data(ssl_s, conn_idx, connection))
910 ERR("SSL_set_ex_data failed: %s\n",
911 pERR_error_string(pERR_get_error(), 0));
912 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
915 if (pSSL_connect(ssl_s) <= 0)
917 res = (DWORD_PTR)pSSL_get_ex_data(ssl_s, error_idx);
919 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
920 ERR("SSL_connect failed: %d\n", res);
924 connection->ssl_s = ssl_s;
926 SecBuffer out_buf = {0, SECBUFFER_TOKEN, NULL}, in_bufs[2] = {{0, SECBUFFER_TOKEN}, {0, SECBUFFER_EMPTY}};
927 SecBufferDesc out_desc = {SECBUFFER_VERSION, 1, &out_buf}, in_desc = {SECBUFFER_VERSION, 2, in_bufs};
928 SecHandle *cred = &cred_handle;
930 SIZE_T read_buf_size = 2048;
935 const CERT_CONTEXT *cert;
936 SECURITY_STATUS status;
937 DWORD res = ERROR_SUCCESS;
939 const DWORD isc_req_flags = ISC_REQ_ALLOCATE_MEMORY|ISC_REQ_USE_SESSION_KEY|ISC_REQ_CONFIDENTIALITY
940 |ISC_REQ_SEQUENCE_DETECT|ISC_REQ_REPLAY_DETECT|ISC_REQ_MANUAL_CRED_VALIDATION;
942 if(!ensure_cred_handle())
946 if(!have_compat_cred_handle)
947 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
948 cred = &compat_cred_handle;
951 read_buf = heap_alloc(read_buf_size);
953 return ERROR_OUTOFMEMORY;
955 status = InitializeSecurityContextW(cred, NULL, connection->server->name, isc_req_flags, 0, 0, NULL, 0,
956 &ctx, &out_desc, &attrs, NULL);
958 assert(status != SEC_E_OK);
960 while(status == SEC_I_CONTINUE_NEEDED || status == SEC_E_INCOMPLETE_MESSAGE) {
961 if(out_buf.cbBuffer) {
962 assert(status == SEC_I_CONTINUE_NEEDED);
964 TRACE("sending %u bytes\n", out_buf.cbBuffer);
966 size = send(connection->socket, out_buf.pvBuffer, out_buf.cbBuffer, 0);
967 if(size != out_buf.cbBuffer) {
968 ERR("send failed\n");
969 status = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
973 FreeContextBuffer(out_buf.pvBuffer);
974 out_buf.pvBuffer = NULL;
975 out_buf.cbBuffer = 0;
978 if(status == SEC_I_CONTINUE_NEEDED) {
979 assert(in_bufs[1].cbBuffer < read_buf_size);
981 memmove(read_buf, (BYTE*)in_bufs[0].pvBuffer+in_bufs[0].cbBuffer-in_bufs[1].cbBuffer, in_bufs[1].cbBuffer);
982 in_bufs[0].cbBuffer = in_bufs[1].cbBuffer;
984 in_bufs[1].BufferType = SECBUFFER_EMPTY;
985 in_bufs[1].cbBuffer = 0;
986 in_bufs[1].pvBuffer = NULL;
989 assert(in_bufs[0].BufferType == SECBUFFER_TOKEN);
990 assert(in_bufs[1].BufferType == SECBUFFER_EMPTY);
992 if(in_bufs[0].cbBuffer + 1024 > read_buf_size) {
995 new_read_buf = heap_realloc(read_buf, read_buf_size + 1024);
997 status = E_OUTOFMEMORY;
1001 in_bufs[0].pvBuffer = read_buf = new_read_buf;
1002 read_buf_size += 1024;
1005 size = recv(connection->socket, read_buf+in_bufs[0].cbBuffer, read_buf_size-in_bufs[0].cbBuffer, 0);
1007 WARN("recv error\n");
1008 res = ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
1012 TRACE("recv %lu bytes\n", size);
1014 in_bufs[0].cbBuffer += size;
1015 in_bufs[0].pvBuffer = read_buf;
1016 status = InitializeSecurityContextW(cred, &ctx, connection->server->name, isc_req_flags, 0, 0, &in_desc,
1017 0, NULL, &out_desc, &attrs, NULL);
1018 TRACE("InitializeSecurityContext ret %08x\n", status);
1020 if(status == SEC_E_OK) {
1021 if(in_bufs[1].BufferType == SECBUFFER_EXTRA)
1022 FIXME("SECBUFFER_EXTRA not supported\n");
1024 status = QueryContextAttributesW(&ctx, SECPKG_ATTR_STREAM_SIZES, &connection->ssl_sizes);
1025 if(status != SEC_E_OK) {
1026 WARN("Could not get sizes\n");
1030 status = QueryContextAttributesW(&ctx, SECPKG_ATTR_REMOTE_CERT_CONTEXT, (void*)&cert);
1031 if(status == SEC_E_OK) {
1032 res = netconn_verify_cert(connection, cert, cert->hCertStore);
1033 CertFreeCertificateContext(cert);
1034 if(res != ERROR_SUCCESS) {
1035 WARN("cert verify failed: %u\n", res);
1039 WARN("Could not get cert\n");
1043 connection->ssl_buf = heap_alloc(connection->ssl_sizes.cbHeader + connection->ssl_sizes.cbMaximumMessage
1044 + connection->ssl_sizes.cbTrailer);
1045 if(!connection->ssl_buf) {
1046 res = GetLastError();
1053 if(status != SEC_E_OK || res != ERROR_SUCCESS) {
1054 WARN("Failed to initialize security context failed: %08x\n", status);
1055 heap_free(connection->ssl_buf);
1056 connection->ssl_buf = NULL;
1057 DeleteSecurityContext(&ctx);
1058 return res ? res : ERROR_INTERNET_SECURITY_CHANNEL_ERROR;
1062 TRACE("established SSL connection\n");
1063 connection->ssl_ctx = ctx;
1066 connection->secure = TRUE;
1067 connection->security_flags |= SECURITY_FLAG_SECURE;
1069 bits = NETCON_GetCipherStrength(connection);
1071 connection->security_flags |= SECURITY_FLAG_STRENGTH_STRONG;
1072 else if (bits >= 56)
1073 connection->security_flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
1075 connection->security_flags |= SECURITY_FLAG_STRENGTH_WEAK;
1077 if(connection->mask_errors)
1078 connection->server->security_flags = connection->security_flags;
1079 return ERROR_SUCCESS;
1081 #ifdef SONAME_LIBSSL
1085 pSSL_shutdown(ssl_s);
1092 /******************************************************************************
1093 * NETCON_secure_connect
1094 * Initiates a secure connection over an existing plaintext connection.
1096 DWORD NETCON_secure_connect(netconn_t *connection, server_t *server)
1100 /* can't connect if we are already connected */
1101 if(connection->secure) {
1102 ERR("already connected\n");
1103 return ERROR_INTERNET_CANNOT_CONNECT;
1106 if(server != connection->server) {
1107 server_release(connection->server);
1108 server_addref(server);
1109 connection->server = server;
1112 /* connect with given TLS options */
1113 res = netcon_secure_connect_setup(connection, FALSE);
1114 if (res == ERROR_SUCCESS)
1117 /* FIXME: when got version alert and FIN from server */
1118 /* fallback to connect without TLSv1.1/TLSv1.2 */
1119 if (res == ERROR_INTERNET_SECURITY_CHANNEL_ERROR)
1121 closesocket(connection->socket);
1122 res = create_netconn_socket(connection->server, connection, 500);
1123 if (res != ERROR_SUCCESS)
1125 res = netcon_secure_connect_setup(connection, TRUE);
1130 /******************************************************************************
1132 * Basically calls 'send()' unless we should use SSL
1133 * number of chars send is put in *sent
1135 DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags,
1136 int *sent /* out */)
1138 if(!connection->secure)
1140 *sent = send(connection->socket, msg, len, flags);
1142 return sock_get_error(errno);
1143 return ERROR_SUCCESS;
1147 #ifdef SONAME_LIBSSL
1148 if(!connection->secure) {
1149 FIXME("not connected\n");
1150 return ERROR_NOT_SUPPORTED;
1153 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
1154 *sent = pSSL_write(connection->ssl_s, msg, len);
1155 if (*sent < 1 && len)
1156 return ERROR_INTERNET_CONNECTION_ABORTED;
1157 return ERROR_SUCCESS;
1159 FIXME("not supported on this platform\n");
1160 return ERROR_NOT_SUPPORTED;
1165 /******************************************************************************
1167 * Basically calls 'recv()' unless we should use SSL
1168 * number of chars received is put in *recvd
1170 DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, int flags, int *recvd)
1174 return ERROR_SUCCESS;
1176 if (!connection->secure)
1178 *recvd = recv(connection->socket, buf, len, flags);
1179 return *recvd == -1 ? sock_get_error(errno) : ERROR_SUCCESS;
1183 #ifdef SONAME_LIBSSL
1184 if(!connection->secure) {
1185 FIXME("not connected\n");
1186 return ERROR_NOT_SUPPORTED;
1188 *recvd = pSSL_read(connection->ssl_s, buf, len);
1190 /* Check if EOF was received */
1191 if(!*recvd && (pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_ZERO_RETURN
1192 || pSSL_get_error(connection->ssl_s, *recvd)==SSL_ERROR_SYSCALL))
1193 return ERROR_SUCCESS;
1195 return *recvd > 0 ? ERROR_SUCCESS : ERROR_INTERNET_CONNECTION_ABORTED;
1197 FIXME("not supported on this platform\n");
1198 return ERROR_NOT_SUPPORTED;
1203 /******************************************************************************
1204 * NETCON_query_data_available
1205 * Returns the number of bytes of peeked data plus the number of bytes of
1206 * queued, but unread data.
1208 BOOL NETCON_query_data_available(netconn_t *connection, DWORD *available)
1212 if(!connection->secure)
1216 int retval = ioctlsocket(connection->socket, FIONREAD, &unread);
1219 TRACE("%d bytes of queued, but unread data\n", unread);
1220 *available += unread;
1226 #ifdef SONAME_LIBSSL
1227 *available = pSSL_pending(connection->ssl_s);
1229 FIXME("not supported on this platform\n");
1236 BOOL NETCON_is_alive(netconn_t *netconn)
1242 len = recv(netconn->socket, &b, 1, MSG_PEEK|MSG_DONTWAIT);
1243 return len == 1 || (len == -1 && errno == EWOULDBLOCK);
1244 #elif defined(__MINGW32__) || defined(_MSC_VER)
1250 if(!ioctlsocket(netconn->socket, FIONBIO, &mode))
1253 len = recv(netconn->socket, &b, 1, MSG_PEEK);
1256 if(!ioctlsocket(netconn->socket, FIONBIO, &mode))
1259 return len == 1 || (len == -1 && errno == WSAEWOULDBLOCK);
1261 FIXME("not supported on this platform\n");
1266 LPCVOID NETCON_GetCert(netconn_t *connection)
1268 #ifdef SONAME_LIBSSL
1272 if (!connection->secure)
1275 cert = pSSL_get_peer_certificate(connection->ssl_s);
1276 r = X509_to_cert_context(cert);
1279 FIXME("not supported on this platform\n");
1284 int NETCON_GetCipherStrength(netconn_t *connection)
1286 #ifdef SONAME_LIBSSL
1287 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
1288 const SSL_CIPHER *cipher;
1294 if (!connection->secure)
1296 cipher = pSSL_get_current_cipher(connection->ssl_s);
1299 pSSL_CIPHER_get_bits(cipher, &bits);
1302 SecPkgContext_ConnectionInfo conn_info;
1303 SECURITY_STATUS res;
1305 if (!connection->secure)
1308 res = QueryContextAttributesW(&connection->ssl_ctx, SECPKG_ATTR_CONNECTION_INFO, (void*)&conn_info);
1310 WARN("QueryContextAttributesW failed: %08x\n", res);
1311 return res == SEC_E_OK ? conn_info.dwCipherStrength : 0;
1315 DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, DWORD value)
1320 /* value is in milliseconds, convert to struct timeval */
1321 if (value == INFINITE)
1328 tv.tv_sec = value / 1000;
1329 tv.tv_usec = (value % 1000) * 1000;
1331 result = setsockopt(connection->socket, SOL_SOCKET,
1332 send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv,
1336 WARN("setsockopt failed (%s)\n", strerror(errno));
1337 return sock_get_error(errno);
1339 return ERROR_SUCCESS;