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_OFFLINE_REVOCATION
|
242 CERT_TRUST_REVOCATION_STATUS_UNKNOWN
|
243 CERT_TRUST_IS_REVOKED
|
244 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
;
246 if (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_NOT_TIME_VALID
&&
247 !(security_flags
& SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
))
248 err
= ERROR_INTERNET_SEC_CERT_DATE_INVALID
;
249 else if (chain
->TrustStatus
.dwErrorStatus
&
250 CERT_TRUST_IS_UNTRUSTED_ROOT
&&
251 !(security_flags
& SECURITY_FLAG_IGNORE_UNKNOWN_CA
))
252 err
= ERROR_INTERNET_INVALID_CA
;
253 else if (!(security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
) &&
254 ((chain
->TrustStatus
.dwErrorStatus
&
255 CERT_TRUST_IS_OFFLINE_REVOCATION
) ||
256 (chain
->TrustStatus
.dwErrorStatus
&
257 CERT_TRUST_REVOCATION_STATUS_UNKNOWN
)))
258 err
= ERROR_INTERNET_SEC_CERT_NO_REV
;
259 else if (!(security_flags
& SECURITY_FLAG_IGNORE_REVOCATION
) &&
260 (chain
->TrustStatus
.dwErrorStatus
& CERT_TRUST_IS_REVOKED
))
261 err
= ERROR_INTERNET_SEC_CERT_REVOKED
;
262 else if (!(security_flags
& SECURITY_FLAG_IGNORE_WRONG_USAGE
) &&
263 (chain
->TrustStatus
.dwErrorStatus
&
264 CERT_TRUST_IS_NOT_VALID_FOR_USAGE
))
265 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
266 else if (chain
->TrustStatus
.dwErrorStatus
& ~supportedErrors
)
267 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
271 CERT_CHAIN_POLICY_PARA policyPara
;
272 SSL_EXTRA_CERT_CHAIN_POLICY_PARA sslExtraPolicyPara
;
273 CERT_CHAIN_POLICY_STATUS policyStatus
;
274 CERT_CHAIN_CONTEXT chainCopy
;
276 /* Clear chain->TrustStatus.dwErrorStatus so
277 * CertVerifyCertificateChainPolicy will verify additional checks
278 * rather than stopping with an existing, ignored error.
280 memcpy(&chainCopy
, chain
, sizeof(chainCopy
));
281 chainCopy
.TrustStatus
.dwErrorStatus
= 0;
282 sslExtraPolicyPara
.u
.cbSize
= sizeof(sslExtraPolicyPara
);
283 sslExtraPolicyPara
.dwAuthType
= AUTHTYPE_SERVER
;
284 sslExtraPolicyPara
.pwszServerName
= server
;
285 sslExtraPolicyPara
.fdwChecks
= security_flags
;
286 policyPara
.cbSize
= sizeof(policyPara
);
287 policyPara
.dwFlags
= 0;
288 policyPara
.pvExtraPolicyPara
= &sslExtraPolicyPara
;
289 ret
= CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL
,
290 &chainCopy
, &policyPara
, &policyStatus
);
291 /* Any error in the policy status indicates that the
292 * policy couldn't be verified.
294 if (ret
&& policyStatus
.dwError
)
296 if (policyStatus
.dwError
== CERT_E_CN_NO_MATCH
)
297 err
= ERROR_INTERNET_SEC_CERT_CN_INVALID
;
299 err
= ERROR_INTERNET_SEC_INVALID_CERT
;
302 CertFreeCertificateChain(chain
);
304 TRACE("returning %08x\n", err
);
308 static int netconn_secure_verify(int preverify_ok
, X509_STORE_CTX
*ctx
)
313 HCERTSTORE store
= CertOpenStore(CERT_STORE_PROV_MEMORY
, 0, 0,
314 CERT_STORE_CREATE_NEW_FLAG
, NULL
);
317 ssl
= pX509_STORE_CTX_get_ex_data(ctx
,
318 pSSL_get_ex_data_X509_STORE_CTX_idx());
319 server
= pSSL_get_ex_data(ssl
, hostname_idx
);
320 conn
= pSSL_get_ex_data(ssl
, conn_idx
);
325 PCCERT_CONTEXT endCert
= NULL
;
326 struct stack_st
*chain
= (struct stack_st
*)pX509_STORE_CTX_get_chain( ctx
);
329 for (i
= 0; ret
&& i
< psk_num(chain
); i
++)
331 PCCERT_CONTEXT context
;
333 cert
= (X509
*)psk_value(chain
, i
);
334 if ((context
= X509_to_cert_context(cert
)))
336 ret
= CertAddCertificateContextToStore(store
, context
,
337 CERT_STORE_ADD_ALWAYS
, i
? NULL
: &endCert
);
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
= heap_alloc(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
create_netconn(BOOL useSSL
, server_t
*server
, DWORD security_flags
, netconn_t
**ret
)
504 TRACE("using SSL connection\n");
506 EnterCriticalSection(&init_ssl_cs
);
507 res
= init_openssl();
508 LeaveCriticalSection(&init_ssl_cs
);
509 if(res
!= ERROR_SUCCESS
)
513 netconn
= heap_alloc_zero(sizeof(*netconn
));
515 return ERROR_OUTOFMEMORY
;
517 netconn
->useSSL
= useSSL
;
518 netconn
->socketFD
= -1;
519 netconn
->security_flags
= security_flags
;
520 list_init(&netconn
->pool_entry
);
522 assert(server
->addr_len
);
523 result
= netconn
->socketFD
= socket(server
->addr
.ss_family
, SOCK_STREAM
, 0);
525 result
= connect(netconn
->socketFD
, (struct sockaddr
*)&server
->addr
, server
->addr_len
);
527 closesocket(netconn
->socketFD
);
531 return sock_get_error(errno
);
536 result
= setsockopt(netconn
->socketFD
, IPPROTO_TCP
, TCP_NODELAY
, (void*)&flag
, sizeof(flag
));
538 WARN("setsockopt(TCP_NODELAY) failed\n");
541 server_addref(server
);
542 netconn
->server
= server
;
545 return ERROR_SUCCESS
;
548 void free_netconn(netconn_t
*netconn
)
550 server_release(netconn
->server
);
553 if (netconn
->ssl_s
) {
554 pSSL_shutdown(netconn
->ssl_s
);
555 pSSL_free(netconn
->ssl_s
);
559 closesocket(netconn
->socketFD
);
563 void NETCON_unload(void)
565 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
566 if (OpenSSL_crypto_handle
)
569 wine_dlclose(OpenSSL_crypto_handle
, NULL
, 0);
571 if (OpenSSL_ssl_handle
)
575 wine_dlclose(OpenSSL_ssl_handle
, NULL
, 0);
580 for (i
= 0; i
< num_ssl_locks
; i
++) DeleteCriticalSection(&ssl_locks
[i
]);
581 heap_free(ssl_locks
);
586 /* translate a unix error code into a winsock one */
587 int sock_get_error( int err
)
589 #if !defined(__MINGW32__) && !defined (_MSC_VER)
592 case EINTR
: return WSAEINTR
;
593 case EBADF
: return WSAEBADF
;
595 case EACCES
: return WSAEACCES
;
596 case EFAULT
: return WSAEFAULT
;
597 case EINVAL
: return WSAEINVAL
;
598 case EMFILE
: return WSAEMFILE
;
599 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
600 case EINPROGRESS
: return WSAEINPROGRESS
;
601 case EALREADY
: return WSAEALREADY
;
602 case ENOTSOCK
: return WSAENOTSOCK
;
603 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
604 case EMSGSIZE
: return WSAEMSGSIZE
;
605 case EPROTOTYPE
: return WSAEPROTOTYPE
;
606 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
607 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
608 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
609 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
610 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
611 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
612 case EADDRINUSE
: return WSAEADDRINUSE
;
613 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
614 case ENETDOWN
: return WSAENETDOWN
;
615 case ENETUNREACH
: return WSAENETUNREACH
;
616 case ENETRESET
: return WSAENETRESET
;
617 case ECONNABORTED
: return WSAECONNABORTED
;
619 case ECONNRESET
: return WSAECONNRESET
;
620 case ENOBUFS
: return WSAENOBUFS
;
621 case EISCONN
: return WSAEISCONN
;
622 case ENOTCONN
: return WSAENOTCONN
;
623 case ESHUTDOWN
: return WSAESHUTDOWN
;
624 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
625 case ETIMEDOUT
: return WSAETIMEDOUT
;
626 case ECONNREFUSED
: return WSAECONNREFUSED
;
627 case ELOOP
: return WSAELOOP
;
628 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
629 case EHOSTDOWN
: return WSAEHOSTDOWN
;
630 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
631 case ENOTEMPTY
: return WSAENOTEMPTY
;
633 case EPROCLIM
: return WSAEPROCLIM
;
636 case EUSERS
: return WSAEUSERS
;
639 case EDQUOT
: return WSAEDQUOT
;
642 case ESTALE
: return WSAESTALE
;
645 case EREMOTE
: return WSAEREMOTE
;
647 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
653 /******************************************************************************
654 * NETCON_secure_connect
655 * Initiates a secure connection over an existing plaintext connection.
657 DWORD
NETCON_secure_connect(netconn_t
*connection
, LPWSTR hostname
)
659 DWORD res
= ERROR_NOT_SUPPORTED
;
663 /* can't connect if we are already connected */
664 if (connection
->ssl_s
)
666 ERR("already connected\n");
667 return ERROR_INTERNET_CANNOT_CONNECT
;
670 ssl_s
= pSSL_new(ctx
);
673 ERR("SSL_new failed: %s\n",
674 pERR_error_string(pERR_get_error(), 0));
675 return ERROR_OUTOFMEMORY
;
678 if (!pSSL_set_fd(ssl_s
, connection
->socketFD
))
680 ERR("SSL_set_fd failed: %s\n",
681 pERR_error_string(pERR_get_error(), 0));
682 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
686 if (!pSSL_set_ex_data(ssl_s
, hostname_idx
, hostname
))
688 ERR("SSL_set_ex_data failed: %s\n",
689 pERR_error_string(pERR_get_error(), 0));
690 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
693 if (!pSSL_set_ex_data(ssl_s
, conn_idx
, connection
))
695 ERR("SSL_set_ex_data failed: %s\n",
696 pERR_error_string(pERR_get_error(), 0));
697 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
700 if (pSSL_connect(ssl_s
) <= 0)
702 res
= (DWORD_PTR
)pSSL_get_ex_data(ssl_s
, error_idx
);
704 res
= ERROR_INTERNET_SECURITY_CHANNEL_ERROR
;
705 ERR("SSL_connect failed: %d\n", res
);
709 connection
->ssl_s
= ssl_s
;
710 return ERROR_SUCCESS
;
715 pSSL_shutdown(ssl_s
);
722 /******************************************************************************
724 * Basically calls 'send()' unless we should use SSL
725 * number of chars send is put in *sent
727 DWORD
NETCON_send(netconn_t
*connection
, const void *msg
, size_t len
, int flags
,
730 if (!connection
->useSSL
)
732 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
734 return sock_get_error(errno
);
735 return ERROR_SUCCESS
;
740 if(!connection
->ssl_s
) {
741 FIXME("not connected\n");
742 return ERROR_NOT_SUPPORTED
;
745 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
746 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
747 if (*sent
< 1 && len
)
748 return ERROR_INTERNET_CONNECTION_ABORTED
;
749 return ERROR_SUCCESS
;
751 return ERROR_NOT_SUPPORTED
;
756 /******************************************************************************
758 * Basically calls 'recv()' unless we should use SSL
759 * number of chars received is put in *recvd
761 DWORD
NETCON_recv(netconn_t
*connection
, void *buf
, size_t len
, int flags
,
762 int *recvd
/* out */)
766 return ERROR_SUCCESS
;
767 if (!connection
->useSSL
)
769 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
770 return *recvd
== -1 ? sock_get_error(errno
) : ERROR_SUCCESS
;
775 if(!connection
->ssl_s
) {
776 FIXME("not connected\n");
777 return ERROR_NOT_SUPPORTED
;
779 *recvd
= pSSL_read(connection
->ssl_s
, buf
, len
);
781 /* Check if EOF was received */
782 if(!*recvd
&& (pSSL_get_error(connection
->ssl_s
, *recvd
)==SSL_ERROR_ZERO_RETURN
783 || pSSL_get_error(connection
->ssl_s
, *recvd
)==SSL_ERROR_SYSCALL
))
784 return ERROR_SUCCESS
;
786 return *recvd
> 0 ? ERROR_SUCCESS
: ERROR_INTERNET_CONNECTION_ABORTED
;
788 return ERROR_NOT_SUPPORTED
;
793 /******************************************************************************
794 * NETCON_query_data_available
795 * Returns the number of bytes of peeked data plus the number of bytes of
796 * queued, but unread data.
798 BOOL
NETCON_query_data_available(netconn_t
*connection
, DWORD
*available
)
802 if (!connection
->useSSL
)
806 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
809 TRACE("%d bytes of queued, but unread data\n", unread
);
810 *available
+= unread
;
817 *available
= connection
->ssl_s
? pSSL_pending(connection
->ssl_s
) : 0;
823 BOOL
NETCON_is_alive(netconn_t
*netconn
)
829 len
= recv(netconn
->socketFD
, &b
, 1, MSG_PEEK
|MSG_DONTWAIT
);
830 return len
== 1 || (len
== -1 && errno
== EWOULDBLOCK
);
831 #elif defined(__MINGW32__) || defined(_MSC_VER)
837 if(!ioctlsocket(netconn
->socketFD
, FIONBIO
, &mode
))
840 len
= recv(netconn
->socketFD
, &b
, 1, MSG_PEEK
);
843 if(!ioctlsocket(netconn
->socketFD
, FIONBIO
, &mode
))
846 return len
== 1 || (len
== -1 && errno
== WSAEWOULDBLOCK
);
848 FIXME("not supported on this platform\n");
853 LPCVOID
NETCON_GetCert(netconn_t
*connection
)
859 if (!connection
->ssl_s
)
862 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
863 r
= X509_to_cert_context(cert
);
870 int NETCON_GetCipherStrength(netconn_t
*connection
)
873 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x0090707f)
874 const SSL_CIPHER
*cipher
;
880 if (!connection
->ssl_s
)
882 cipher
= pSSL_get_current_cipher(connection
->ssl_s
);
885 pSSL_CIPHER_get_bits(cipher
, &bits
);
892 DWORD
NETCON_set_timeout(netconn_t
*connection
, BOOL send
, int value
)
897 /* value is in milliseconds, convert to struct timeval */
898 tv
.tv_sec
= value
/ 1000;
899 tv
.tv_usec
= (value
% 1000) * 1000;
901 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
902 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, (void*)&tv
,
907 WARN("setsockopt failed (%s)\n", strerror(errno
));
908 return sock_get_error(errno
);
911 return ERROR_SUCCESS
;