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_new
);
128 MAKE_FUNCPTR(SSL_free
);
129 MAKE_FUNCPTR(SSL_set_fd
);
130 MAKE_FUNCPTR(SSL_connect
);
131 MAKE_FUNCPTR(SSL_shutdown
);
132 MAKE_FUNCPTR(SSL_write
);
133 MAKE_FUNCPTR(SSL_read
);
134 MAKE_FUNCPTR(SSL_pending
);
135 MAKE_FUNCPTR(SSL_get_error
);
136 MAKE_FUNCPTR(SSL_get_ex_new_index
);
137 MAKE_FUNCPTR(SSL_get_ex_data
);
138 MAKE_FUNCPTR(SSL_set_ex_data
);
139 MAKE_FUNCPTR(SSL_get_ex_data_X509_STORE_CTX_idx
);
140 MAKE_FUNCPTR(SSL_get_peer_certificate
);
141 MAKE_FUNCPTR(SSL_CTX_get_timeout
);
142 MAKE_FUNCPTR(SSL_CTX_set_timeout
);
143 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths
);
144 MAKE_FUNCPTR(SSL_CTX_set_verify
);
145 MAKE_FUNCPTR(SSL_get_current_cipher
);
146 MAKE_FUNCPTR(SSL_CIPHER_get_bits
);
148 /* OpenSSL's libcrypto functions that we use */
149 MAKE_FUNCPTR(BIO_new_fp
);
150 MAKE_FUNCPTR(CRYPTO_num_locks
);
151 MAKE_FUNCPTR(CRYPTO_set_id_callback
);
152 MAKE_FUNCPTR(CRYPTO_set_locking_callback
);
153 MAKE_FUNCPTR(ERR_free_strings
);
154 MAKE_FUNCPTR(ERR_get_error
);
155 MAKE_FUNCPTR(ERR_error_string
);
156 MAKE_FUNCPTR(X509_STORE_CTX_get_ex_data
);
157 MAKE_FUNCPTR(X509_STORE_CTX_get_chain
);
158 MAKE_FUNCPTR(i2d_X509
);
159 MAKE_FUNCPTR(sk_num
);
160 MAKE_FUNCPTR(sk_value
);
163 static CRITICAL_SECTION
*ssl_locks
;
164 static unsigned int num_ssl_locks
;
166 static unsigned long ssl_thread_id(void)
168 return GetCurrentThreadId();
171 static void ssl_lock_callback(int mode
, int type
, const char *file
, int line
)
173 if (mode
& CRYPTO_LOCK
)
174 EnterCriticalSection(&ssl_locks
[type
]);
176 LeaveCriticalSection(&ssl_locks
[type
]);
179 static PCCERT_CONTEXT
X509_to_cert_context(X509
*cert
)
181 unsigned char* buffer
,*p
;
183 BOOL malloced
= FALSE
;
187 len
= pi2d_X509(cert
,&p
);
189 * SSL 0.9.7 and above malloc the buffer if it is null.
190 * however earlier version do not and so we would need to alloc the buffer.
192 * see the i2d_X509 man page for more details.
196 buffer
= heap_alloc(len
);
198 len
= pi2d_X509(cert
,&p
);
206 ret
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
216 static DWORD
netconn_verify_cert(netconn_t
*conn
, PCCERT_CONTEXT cert
, HCERTSTORE store
)
219 CERT_CHAIN_PARA chainPara
= { sizeof(chainPara
), { 0 } };
220 PCCERT_CHAIN_CONTEXT chain
;
221 char oid_server_auth
[] = szOID_PKIX_KP_SERVER_AUTH
;
222 char *server_auth
[] = { oid_server_auth
};
223 DWORD err
= ERROR_SUCCESS
, chainFlags
= 0, errors
;
225 static const DWORD supportedErrors
=
226 CERT_TRUST_IS_NOT_TIME_VALID
|
227 CERT_TRUST_IS_UNTRUSTED_ROOT
|
228 CERT_TRUST_IS_PARTIAL_CHAIN
|
229 CERT_TRUST_IS_OFFLINE_REVOCATION
|
230 CERT_TRUST_REVOCATION_STATUS_UNKNOWN
|
231 CERT_TRUST_IS_REVOKED
|
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 (!(conn
->security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
))
239 chainFlags
|= CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT
;
241 if (!(ret
= CertGetCertificateChain(NULL
, cert
, NULL
, store
, &chainPara
, chainFlags
, NULL
, &chain
))) {
243 return GetLastError();
246 errors
= chain
->TrustStatus
.dwErrorStatus
;
248 if (chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
) {
249 WARN("error status %x\n", chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
);
250 if(conn
->mask_errors
)
251 WARN("CERT_TRUST_IS_NOT_TIME_VALID, unknown error flags\n");
252 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
253 errors
&= supportedErrors
;
256 if(errors
& CERT_TRUST_IS_NOT_TIME_VALID
) {
257 WARN("CERT_TRUST_IS_NOT_TIME_VALID\n");
258 if(conn
->mask_errors
)
259 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_DATE
;
260 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
))
261 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_DATE_INVALID
;
262 errors
&= ~CERT_TRUST_IS_NOT_TIME_VALID
;
265 if(errors
& CERT_TRUST_IS_UNTRUSTED_ROOT
) {
266 WARN("CERT_TRUST_IS_UNTRUSTED_ROOT\n");
267 if(conn
->mask_errors
)
268 WARN("CERT_TRUST_IS_UNTRUSTED_ROOT, unknown flags\n");
269 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
))
270 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_INVALID_CA
;
271 errors
&= ~CERT_TRUST_IS_UNTRUSTED_ROOT
;
274 /* This seems strange, but that's what tests show */
275 if(errors
& CERT_TRUST_IS_PARTIAL_CHAIN
) {
276 WARN("CERT_TRUST_IS_PARTIAL_CHAIN\n");
277 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
)) {
278 if(!(conn
->security_flags
& _SECURITY_FLAG_CERT_REV_FAILED
))
279 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_REV_FAILED
;
281 err
= conn
->mask_errors
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_INVALID_CA
;
283 if(conn
->mask_errors
) {
284 if(!(conn
->security_flags
& _SECURITY_FLAG_CERT_REV_FAILED
))
285 conn
->security_flags
|= _SECURITY_FLAG_CERT_REV_FAILED
;
287 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CA
;
289 errors
&= ~CERT_TRUST_IS_PARTIAL_CHAIN
;
292 if(errors
& (CERT_TRUST_IS_OFFLINE_REVOCATION
| CERT_TRUST_REVOCATION_STATUS_UNKNOWN
)) {
293 WARN("CERT_TRUST_IS_OFFLINE_REVOCATION | CERT_TRUST_REVOCATION_STATUS_UNKNOWN\n");
294 if(conn
->mask_errors
)
295 WARN("TRUST_IS_OFFLINE_REVOCATION | CERT_TRUST_REVOCATION_STATUS_UNKNOWN, unknown error flags\n");
296 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
))
297 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_NO_REV
;
298 errors
&= ~(CERT_TRUST_IS_OFFLINE_REVOCATION
| CERT_TRUST_REVOCATION_STATUS_UNKNOWN
);
301 if(errors
& CERT_TRUST_IS_REVOKED
) {
302 WARN("CERT_TRUST_IS_REVOKED\n");
303 if(conn
->mask_errors
)
304 WARN("TRUST_IS_OFFLINE_REVOCATION | CERT_TRUST_REVOCATION_STATUS_UNKNOWN, unknown error flags\n");
305 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
))
306 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_REVOKED
;
307 errors
&= ~CERT_TRUST_IS_REVOKED
;
310 if(errors
& CERT_TRUST_IS_NOT_VALID_FOR_USAGE
) {
311 WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE\n");
312 if(conn
->mask_errors
)
313 WARN("CERT_TRUST_IS_NOT_VALID_FOR_USAGE, unknown error flags\n");
314 if(!(conn
->security_flags
& SECURITY_FLAG_IGNORE_WRONG_USAGE
))
315 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
316 errors
&= ~CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
319 if(!err
|| conn
->mask_errors
) {
320 CERT_CHAIN_POLICY_PARA policyPara
;
321 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara
;
322 CERT_CHAIN_POLICY_STATUS policyStatus
;
323 CERT_CHAIN_CONTEXT chainCopy
;
325 /* Clear chain->TrustStatus.dwErrorStatus so
326 * CertVerifyCertificateChainPolicy will verify additional checks
327 * rather than stopping with an existing, ignored error.
329 memcpy(&chainCopy
, chain
, sizeof(chainCopy
));
330 chainCopy
.TrustStatus
.dwErrorStatus
= 0;
331 sslExtraPolicyPara
.u
.cbSize
= sizeof(sslExtraPolicyPara
);
332 sslExtraPolicyPara
.dwAuthType
= AUTHTYPE_SERVER
;
333 sslExtraPolicyPara
.pwszServerName
= conn
->server
->name
;
334 sslExtraPolicyPara
.fdwChecks
= conn
->security_flags
;
335 policyPara
.cbSize
= sizeof(policyPara
);
336 policyPara
.dwFlags
= 0;
337 policyPara
.pvExtraPolicyPara
= &sslExtraPolicyPara
;
338 ret
= CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL
,
339 &chainCopy
, &policyPara
, &policyStatus
);
340 /* Any error in the policy status indicates that the
341 * policy couldn't be verified.
344 if(policyStatus
.dwError
== CERT_E_CN_NO_MATCH
) {
345 WARN("CERT_E_CN_NO_MATCH\n");
346 if(conn
->mask_errors
)
347 conn
->security_flags
|= _SECURITY_FLAG_CERT_INVALID_CN
;
348 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_CERT_CN_INVALID
;
349 }else if(policyStatus
.dwError
) {
350 WARN("policyStatus.dwError %x\n", policyStatus
.dwError
);
351 if(conn
->mask_errors
)
352 WARN("unknown error flags for policy status %x\n", policyStatus
.dwError
);
353 err
= conn
->mask_errors
&& err
? ERROR_INTERNET_SEC_CERT_ERRORS
: ERROR_INTERNET_SEC_INVALID_CERT
;
356 err
= GetLastError();
361 WARN("failed %u\n", err
);
362 CertFreeCertificateChain(chain
);
363 if(conn
->server
->cert_chain
) {
364 CertFreeCertificateChain(conn
->server
->cert_chain
);
365 conn
->server
->cert_chain
= NULL
;
367 if(conn
->mask_errors
)
368 conn
->server
->security_flags
|= conn
->security_flags
& _SECURITY_ERROR_FLAGS_MASK
;
372 /* FIXME: Reuse cached chain */
373 if(conn
->server
->cert_chain
)
374 CertFreeCertificateChain(chain
);
376 conn
->server
->cert_chain
= chain
;
377 return ERROR_SUCCESS
;
380 static int netconn_secure_verify(int preverify_ok
, X509_STORE_CTX
*ctx
)
384 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
385 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
388 ssl
= pX509_STORE_CTX_get_ex_data(ctx
,
389 pSSL_get_ex_data_X509_STORE_CTX_idx());
390 conn
= pSSL_get_ex_data(ssl
, conn_idx
);
395 PCCERT_CONTEXT endCert
= NULL
;
396 struct stack_st
*chain
= (struct stack_st
*)pX509_STORE_CTX_get_chain( ctx
);
399 for (i
= 0; ret
&& i
< psk_num(chain
); i
++)
401 PCCERT_CONTEXT context
;
403 cert
= (X509
*)psk_value(chain
, i
);
404 if ((context
= X509_to_cert_context(cert
)))
406 ret
= CertAddCertificateContextToStore(store
, context
,
407 CERT_STORE_ADD_ALWAYS
, i
? NULL
: &endCert
);
408 CertFreeCertificateContext(context
);
411 if (!endCert
) ret
= FALSE
;
414 DWORD_PTR err
= netconn_verify_cert(conn
, endCert
, store
);
418 pSSL_set_ex_data(ssl
, error_idx
, (void *)err
);
422 CertFreeCertificateContext(endCert
);
423 CertCloseStore(store
, 0);
430 static CRITICAL_SECTION init_ssl_cs
;
431 static CRITICAL_SECTION_DEBUG init_ssl_cs_debug
=
434 { &init_ssl_cs_debug
.ProcessLocksList
,
435 &init_ssl_cs_debug
.ProcessLocksList
},
436 0, 0, { (DWORD_PTR
)(__FILE__
": init_ssl_cs") }
438 static CRITICAL_SECTION init_ssl_cs
= { &init_ssl_cs_debug
, -1, 0, 0, 0, 0 };
440 static DWORD
init_openssl(void)
442 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
445 if(OpenSSL_ssl_handle
)
446 return ERROR_SUCCESS
;
448 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
449 if(!OpenSSL_ssl_handle
) {
450 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL
);
451 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
454 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
455 if(!OpenSSL_crypto_handle
) {
456 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO
);
457 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
460 /* mmm nice ugly macroness */
462 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
464 ERR("failed to load symbol %s\n", #x); \
465 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
468 DYNSSL(SSL_library_init
);
469 DYNSSL(SSL_load_error_strings
);
470 DYNSSL(SSLv23_method
);
471 DYNSSL(SSL_CTX_free
);
477 DYNSSL(SSL_shutdown
);
481 DYNSSL(SSL_get_error
);
482 DYNSSL(SSL_get_ex_new_index
);
483 DYNSSL(SSL_get_ex_data
);
484 DYNSSL(SSL_set_ex_data
);
485 DYNSSL(SSL_get_ex_data_X509_STORE_CTX_idx
);
486 DYNSSL(SSL_get_peer_certificate
);
487 DYNSSL(SSL_CTX_get_timeout
);
488 DYNSSL(SSL_CTX_set_timeout
);
489 DYNSSL(SSL_CTX_set_default_verify_paths
);
490 DYNSSL(SSL_CTX_set_verify
);
491 DYNSSL(SSL_get_current_cipher
);
492 DYNSSL(SSL_CIPHER_get_bits
);
495 #define DYNCRYPTO(x) \
496 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
498 ERR("failed to load symbol %s\n", #x); \
499 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR; \
502 DYNCRYPTO(BIO_new_fp
);
503 DYNCRYPTO(CRYPTO_num_locks
);
504 DYNCRYPTO(CRYPTO_set_id_callback
);
505 DYNCRYPTO(CRYPTO_set_locking_callback
);
506 DYNCRYPTO(ERR_free_strings
);
507 DYNCRYPTO(ERR_get_error
);
508 DYNCRYPTO(ERR_error_string
);
509 DYNCRYPTO(X509_STORE_CTX_get_ex_data
);
510 DYNCRYPTO(X509_STORE_CTX_get_chain
);
517 pSSL_load_error_strings();
518 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
520 meth
= pSSLv23_method();
521 ctx
= pSSL_CTX_new(meth
);
522 if(!pSSL_CTX_set_default_verify_paths(ctx
)) {
523 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
524 pERR_error_string(pERR_get_error(), 0));
525 return ERROR_OUTOFMEMORY
;
528 error_idx
= pSSL_get_ex_new_index(0, (void *)"error index", NULL
, NULL
, NULL
);
529 if(error_idx
== -1) {
530 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
531 return ERROR_OUTOFMEMORY
;
534 conn_idx
= pSSL_get_ex_new_index(0, (void *)"netconn index", NULL
, NULL
, NULL
);
536 ERR("SSL_get_ex_new_index failed; %s\n", pERR_error_string(pERR_get_error(), 0));
537 return ERROR_OUTOFMEMORY
;
540 pSSL_CTX_set_verify(ctx
, SSL_VERIFY_PEER
, netconn_secure_verify
);
542 pCRYPTO_set_id_callback(ssl_thread_id
);
543 num_ssl_locks
= pCRYPTO_num_locks();
544 ssl_locks
= heap_alloc(num_ssl_locks
* sizeof(CRITICAL_SECTION
));
546 return ERROR_OUTOFMEMORY
;
548 for(i
= 0; i
< num_ssl_locks
; i
++)
550 InitializeCriticalSection(&ssl_locks
[i
]);
551 ssl_locks
[i
].DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": ssl_locks");
553 pCRYPTO_set_locking_callback(ssl_lock_callback
);
555 return ERROR_SUCCESS
;
557 FIXME("can't use SSL, not compiled in.\n");
558 return ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
562 DWORD
create_netconn(BOOL useSSL
, server_t
*server
, DWORD security_flags
, BOOL mask_errors
, DWORD timeout
, netconn_t
**ret
)
570 TRACE("using SSL connection\n");
572 EnterCriticalSection(&init_ssl_cs
);
573 res
= init_openssl();
574 LeaveCriticalSection(&init_ssl_cs
);
575 if(res
!= ERROR_SUCCESS
)
579 netconn
= heap_alloc_zero(sizeof(*netconn
));
581 return ERROR_OUTOFMEMORY
;
583 netconn
->useSSL
= useSSL
;
584 netconn
->socketFD
= -1;
585 netconn
->security_flags
= security_flags
| server
->security_flags
;
586 netconn
->mask_errors
= mask_errors
;
587 list_init(&netconn
->pool_entry
);
589 assert(server
->addr_len
);
590 result
= netconn
->socketFD
= socket(server
->addr
.ss_family
, SOCK_STREAM
, 0);
593 ioctlsocket(netconn
->socketFD
, FIONBIO
, &flag
);
594 result
= connect(netconn
->socketFD
, (struct sockaddr
*)&server
->addr
, server
->addr_len
);
597 if (sock_get_error(errno
) == WSAEINPROGRESS
) {
601 pfd
.fd
= netconn
->socketFD
;
602 pfd
.events
= POLLOUT
;
603 res
= poll(&pfd
, 1, timeout
);
606 closesocket(netconn
->socketFD
);
608 return ERROR_INTERNET_CANNOT_CONNECT
;
613 socklen_t len
= sizeof(err
);
614 if (!getsockopt(netconn
->socketFD
, SOL_SOCKET
, SO_ERROR
, &err
, &len
) && !err
)
620 closesocket(netconn
->socketFD
);
623 ioctlsocket(netconn
->socketFD
, FIONBIO
, &flag
);
628 return sock_get_error(errno
);
633 result
= setsockopt(netconn
->socketFD
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&flag
, sizeof(flag
));
635 WARN("setsockopt(TCP_NODELAY) failed\n");
638 server_addref(server
);
639 netconn
->server
= server
;
642 return ERROR_SUCCESS
;
645 void free_netconn(netconn_t
*netconn
)
647 server_release(netconn
->server
);
650 if (netconn
->ssl_s
) {
651 pSSL_shutdown(netconn
->ssl_s
);
652 pSSL_free(netconn
->ssl_s
);
656 closesocket(netconn
->socketFD
);
660 void NETCON_unload(void)
662 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
663 if (OpenSSL_crypto_handle
)
666 wine_dlclose(OpenSSL_crypto_handle
, NULL
, 0);
668 if (OpenSSL_ssl_handle
)
672 wine_dlclose(OpenSSL_ssl_handle
, NULL
, 0);
677 for (i
= 0; i
< num_ssl_locks
; i
++)
679 ssl_locks
[i
].DebugInfo
->Spare
[0] = 0;
680 DeleteCriticalSection(&ssl_locks
[i
]);
682 heap_free(ssl_locks
);
687 /* translate a unix error code into a winsock one */
688 int sock_get_error( int err
)
690 #if !defined(__MINGW32__) && !defined (_MSC_VER)
693 case EINTR
: return WSAEINTR
;
694 case EBADF
: return WSAEBADF
;
696 case EACCES
: return WSAEACCES
;
697 case EFAULT
: return WSAEFAULT
;
698 case EINVAL
: return WSAEINVAL
;
699 case EMFILE
: return WSAEMFILE
;
700 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
701 case EINPROGRESS
: return WSAEINPROGRESS
;
702 case EALREADY
: return WSAEALREADY
;
703 case ENOTSOCK
: return WSAENOTSOCK
;
704 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
705 case EMSGSIZE
: return WSAEMSGSIZE
;
706 case EPROTOTYPE
: return WSAEPROTOTYPE
;
707 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
708 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
709 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
710 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
711 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
712 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
713 case EADDRINUSE
: return WSAEADDRINUSE
;
714 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
715 case ENETDOWN
: return WSAENETDOWN
;
716 case ENETUNREACH
: return WSAENETUNREACH
;
717 case ENETRESET
: return WSAENETRESET
;
718 case ECONNABORTED
: return WSAECONNABORTED
;
720 case ECONNRESET
: return WSAECONNRESET
;
721 case ENOBUFS
: return WSAENOBUFS
;
722 case EISCONN
: return WSAEISCONN
;
723 case ENOTCONN
: return WSAENOTCONN
;
724 case ESHUTDOWN
: return WSAESHUTDOWN
;
725 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
726 case ETIMEDOUT
: return WSAETIMEDOUT
;
727 case ECONNREFUSED
: return WSAECONNREFUSED
;
728 case ELOOP
: return WSAELOOP
;
729 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
730 case EHOSTDOWN
: return WSAEHOSTDOWN
;
731 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
732 case ENOTEMPTY
: return WSAENOTEMPTY
;
734 case EPROCLIM
: return WSAEPROCLIM
;
737 case EUSERS
: return WSAEUSERS
;
740 case EDQUOT
: return WSAEDQUOT
;
743 case ESTALE
: return WSAESTALE
;
746 case EREMOTE
: return WSAEREMOTE
;
748 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
754 /******************************************************************************
755 * NETCON_secure_connect
756 * Initiates a secure connection over an existing plaintext connection.
758 DWORD
NETCON_secure_connect(netconn_t
*connection
)
760 DWORD res
= ERROR_NOT_SUPPORTED
;
765 /* can't connect if we are already connected */
766 if (connection
->ssl_s
)
768 ERR("already connected\n");
769 return ERROR_INTERNET_CANNOT_CONNECT
;
772 ssl_s
= pSSL_new(ctx
);
775 ERR("SSL_new failed: %s\n",
776 pERR_error_string(pERR_get_error(), 0));
777 return ERROR_OUTOFMEMORY
;
780 if (!pSSL_set_fd(ssl_s
, connection
->socketFD
))
782 ERR("SSL_set_fd failed: %s\n",
783 pERR_error_string(pERR_get_error(), 0));
784 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
788 if (!pSSL_set_ex_data(ssl_s
, conn_idx
, connection
))
790 ERR("SSL_set_ex_data failed: %s\n",
791 pERR_error_string(pERR_get_error(), 0));
792 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
795 if (pSSL_connect(ssl_s
) <= 0)
797 res
= (DWORD_PTR
)pSSL_get_ex_data(ssl_s
, error_idx
);
799 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
800 ERR("SSL_connect failed: %d\n", res
);
804 connection
->ssl_s
= ssl_s
;
806 bits
= NETCON_GetCipherStrength(connection
);
808 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_STRONG
;
810 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_MEDIUM
;
812 connection
->security_flags
|= SECURITY_FLAG_STRENGTH_WEAK
;
813 connection
->security_flags
|= SECURITY_FLAG_SECURE
;
815 if(connection
->mask_errors
)
816 connection
->server
->security_flags
= connection
->security_flags
;
817 return ERROR_SUCCESS
;
822 pSSL_shutdown(ssl_s
);
829 /******************************************************************************
831 * Basically calls 'send()' unless we should use SSL
832 * number of chars send is put in *sent
834 DWORD
NETCON_send(netconn_t
*connection
, const void *msg
, size_t len
, int flags
,
837 if (!connection
->useSSL
)
839 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
841 return sock_get_error(errno
);
842 return ERROR_SUCCESS
;
847 if(!connection
->ssl_s
) {
848 FIXME("not connected\n");
849 return ERROR_NOT_SUPPORTED
;
852 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
853 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
854 if (*sent
< 1 && len
)
855 return ERROR_INTERNET_CONNECTION_ABORTED
;
856 return ERROR_SUCCESS
;
858 return ERROR_NOT_SUPPORTED
;
863 /******************************************************************************
865 * Basically calls 'recv()' unless we should use SSL
866 * number of chars received is put in *recvd
868 DWORD
NETCON_recv(netconn_t
*connection
, void *buf
, size_t len
, int flags
,
869 int *recvd
/* out */)
873 return ERROR_SUCCESS
;
874 if (!connection
->useSSL
)
876 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
877 return *recvd
== -1 ? sock_get_error(errno
) : ERROR_SUCCESS
;
882 if(!connection
->ssl_s
) {
883 FIXME("not connected\n");
884 return ERROR_NOT_SUPPORTED
;
886 *recvd
= pSSL_read(connection
->ssl_s
, buf
, len
);
888 /* Check if EOF was received */
889 if(!*recvd
&& (pSSL_get_error(connection
->ssl_s
, *recvd
)==SSL_ERROR_ZERO_RETURN
890 || pSSL_get_error(connection
->ssl_s
, *recvd
)==SSL_ERROR_SYSCALL
))
891 return ERROR_SUCCESS
;
893 return *recvd
> 0 ? ERROR_SUCCESS
: ERROR_INTERNET_CONNECTION_ABORTED
;
895 return ERROR_NOT_SUPPORTED
;
900 /******************************************************************************
901 * NETCON_query_data_available
902 * Returns the number of bytes of peeked data plus the number of bytes of
903 * queued, but unread data.
905 BOOL
NETCON_query_data_available(netconn_t
*connection
, DWORD
*available
)
909 if (!connection
->useSSL
)
913 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
916 TRACE("%d bytes of queued, but unread data\n", unread
);
917 *available
+= unread
;
924 *available
= connection
->ssl_s
? pSSL_pending(connection
->ssl_s
) : 0;
930 BOOL
NETCON_is_alive(netconn_t
*netconn
)
936 len
= recv(netconn
->socketFD
, &b
, 1, MSG_PEEK
|MSG_DONTWAIT
);
937 return len
== 1 || (len
== -1 && errno
== EWOULDBLOCK
);
938 #elif defined(__MINGW32__) || defined(_MSC_VER)
944 if(!ioctlsocket(netconn
->socketFD
, FIONBIO
, &mode
))
947 len
= recv(netconn
->socketFD
, &b
, 1, MSG_PEEK
);
950 if(!ioctlsocket(netconn
->socketFD
, FIONBIO
, &mode
))
953 return len
== 1 || (len
== -1 && errno
== WSAEWOULDBLOCK
);
955 FIXME("not supported on this platform\n");
960 LPCVOID
NETCON_GetCert(netconn_t
*connection
)
966 if (!connection
->ssl_s
)
969 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
970 r
= X509_to_cert_context(cert
);
977 int NETCON_GetCipherStrength(netconn_t
*connection
)
980 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
981 const SSL_CIPHER
*cipher
;
987 if (!connection
->ssl_s
)
989 cipher
= pSSL_get_current_cipher(connection
->ssl_s
);
992 pSSL_CIPHER_get_bits(cipher
, &bits
);
999 DWORD
NETCON_set_timeout(netconn_t
*connection
, BOOL send
, DWORD value
)
1004 /* value is in milliseconds, convert to struct timeval */
1005 if (value
== INFINITE
)
1012 tv
.tv_sec
= value
/ 1000;
1013 tv
.tv_usec
= (value
% 1000) * 1000;
1015 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
1016 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, (void*)&tv
,
1020 WARN("setsockopt failed (%s)\n", strerror(errno
));
1021 return sock_get_error(errno
);
1023 return ERROR_SUCCESS
;