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 #if defined(__MINGW32__) || defined (_MSC_VER)
30 #include <sys/types.h>
34 #ifdef HAVE_SYS_POLL_H
35 # include <sys/poll.h>
37 #ifdef HAVE_SYS_TIME_H
38 # include <sys/time.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 # include <sys/socket.h>
43 #ifdef HAVE_SYS_FILIO_H
44 # include <sys/filio.h>
49 #ifdef HAVE_SYS_IOCTL_H
50 # include <sys/ioctl.h>
56 #ifdef HAVE_NETINET_IN_H
57 # include <netinet/in.h>
59 #ifdef HAVE_OPENSSL_SSL_H
60 # include <openssl/ssl.h>
71 #include "wine/library.h"
78 #include "wine/debug.h"
81 /* To avoid conflicts with the Unix socket headers. we only need it for
82 * the error codes anyway. */
86 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
89 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
92 * This should use winsock - To use winsock the functions will have to change a bit
93 * as they are designed for unix sockets.
94 * SSL stuff should use crypt32.dll
99 #include <openssl/err.h>
101 static CRITICAL_SECTION init_ssl_cs
;
102 static CRITICAL_SECTION_DEBUG init_ssl_cs_debug
=
105 { &init_ssl_cs_debug
.ProcessLocksList
,
106 &init_ssl_cs_debug
.ProcessLocksList
},
107 0, 0, { (DWORD_PTR
)(__FILE__
": init_ssl_cs") }
109 static CRITICAL_SECTION init_ssl_cs
= { &init_ssl_cs_debug
, -1, 0, 0, 0, 0 };
111 static void *OpenSSL_ssl_handle
;
112 static void *OpenSSL_crypto_handle
;
114 static SSL_METHOD
*meth
;
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_new
);
124 MAKE_FUNCPTR(SSL_new
);
125 MAKE_FUNCPTR(SSL_free
);
126 MAKE_FUNCPTR(SSL_set_fd
);
127 MAKE_FUNCPTR(SSL_connect
);
128 MAKE_FUNCPTR(SSL_shutdown
);
129 MAKE_FUNCPTR(SSL_write
);
130 MAKE_FUNCPTR(SSL_read
);
131 MAKE_FUNCPTR(SSL_pending
);
132 MAKE_FUNCPTR(SSL_get_verify_result
);
133 MAKE_FUNCPTR(SSL_get_peer_certificate
);
134 MAKE_FUNCPTR(SSL_CTX_get_timeout
);
135 MAKE_FUNCPTR(SSL_CTX_set_timeout
);
136 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths
);
138 /* OpenSSL's libcrypto functions that we use */
139 MAKE_FUNCPTR(BIO_new_fp
);
140 MAKE_FUNCPTR(ERR_get_error
);
141 MAKE_FUNCPTR(ERR_error_string
);
142 MAKE_FUNCPTR(i2d_X509
);
147 BOOL
NETCON_init(WININET_NETCONNECTION
*connection
, BOOL useSSL
)
149 connection
->useSSL
= FALSE
;
150 connection
->socketFD
= -1;
153 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
154 TRACE("using SSL connection\n");
155 EnterCriticalSection(&init_ssl_cs
);
156 if (OpenSSL_ssl_handle
) /* already initialized everything */
158 LeaveCriticalSection(&init_ssl_cs
);
161 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
162 if (!OpenSSL_ssl_handle
)
164 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
166 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
167 LeaveCriticalSection(&init_ssl_cs
);
170 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
171 if (!OpenSSL_crypto_handle
)
173 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
175 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
176 LeaveCriticalSection(&init_ssl_cs
);
180 /* mmm nice ugly macroness */
182 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
185 ERR("failed to load symbol %s\n", #x); \
186 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
187 LeaveCriticalSection(&init_ssl_cs); \
191 DYNSSL(SSL_library_init
);
192 DYNSSL(SSL_load_error_strings
);
193 DYNSSL(SSLv23_method
);
199 DYNSSL(SSL_shutdown
);
203 DYNSSL(SSL_get_verify_result
);
204 DYNSSL(SSL_get_peer_certificate
);
205 DYNSSL(SSL_CTX_get_timeout
);
206 DYNSSL(SSL_CTX_set_timeout
);
207 DYNSSL(SSL_CTX_set_default_verify_paths
);
210 #define DYNCRYPTO(x) \
211 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
214 ERR("failed to load symbol %s\n", #x); \
215 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
216 LeaveCriticalSection(&init_ssl_cs); \
219 DYNCRYPTO(BIO_new_fp
);
220 DYNCRYPTO(ERR_get_error
);
221 DYNCRYPTO(ERR_error_string
);
226 pSSL_load_error_strings();
227 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
229 meth
= pSSLv23_method();
230 ctx
= pSSL_CTX_new(meth
);
231 if (!pSSL_CTX_set_default_verify_paths(ctx
))
233 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
234 pERR_error_string(pERR_get_error(), 0));
235 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
236 LeaveCriticalSection(&init_ssl_cs
);
239 LeaveCriticalSection(&init_ssl_cs
);
241 FIXME("can't use SSL, not compiled in.\n");
242 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
249 BOOL
NETCON_connected(WININET_NETCONNECTION
*connection
)
251 if (connection
->socketFD
== -1)
257 /* translate a unix error code into a winsock one */
258 static int sock_get_error( int err
)
260 #if !defined(__MINGW32__) && !defined (_MSC_VER)
263 case EINTR
: return WSAEINTR
;
264 case EBADF
: return WSAEBADF
;
266 case EACCES
: return WSAEACCES
;
267 case EFAULT
: return WSAEFAULT
;
268 case EINVAL
: return WSAEINVAL
;
269 case EMFILE
: return WSAEMFILE
;
270 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
271 case EINPROGRESS
: return WSAEINPROGRESS
;
272 case EALREADY
: return WSAEALREADY
;
273 case ENOTSOCK
: return WSAENOTSOCK
;
274 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
275 case EMSGSIZE
: return WSAEMSGSIZE
;
276 case EPROTOTYPE
: return WSAEPROTOTYPE
;
277 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
278 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
279 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
280 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
281 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
282 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
283 case EADDRINUSE
: return WSAEADDRINUSE
;
284 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
285 case ENETDOWN
: return WSAENETDOWN
;
286 case ENETUNREACH
: return WSAENETUNREACH
;
287 case ENETRESET
: return WSAENETRESET
;
288 case ECONNABORTED
: return WSAECONNABORTED
;
290 case ECONNRESET
: return WSAECONNRESET
;
291 case ENOBUFS
: return WSAENOBUFS
;
292 case EISCONN
: return WSAEISCONN
;
293 case ENOTCONN
: return WSAENOTCONN
;
294 case ESHUTDOWN
: return WSAESHUTDOWN
;
295 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
296 case ETIMEDOUT
: return WSAETIMEDOUT
;
297 case ECONNREFUSED
: return WSAECONNREFUSED
;
298 case ELOOP
: return WSAELOOP
;
299 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
300 case EHOSTDOWN
: return WSAEHOSTDOWN
;
301 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
302 case ENOTEMPTY
: return WSAENOTEMPTY
;
304 case EPROCLIM
: return WSAEPROCLIM
;
307 case EUSERS
: return WSAEUSERS
;
310 case EDQUOT
: return WSAEDQUOT
;
313 case ESTALE
: return WSAESTALE
;
316 case EREMOTE
: return WSAEREMOTE
;
318 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
324 /******************************************************************************
326 * Basically calls 'socket()'
328 BOOL
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
329 int type
, int protocol
)
332 if (connection
->useSSL
)
336 connection
->socketFD
= socket(domain
, type
, protocol
);
337 if (connection
->socketFD
== -1)
339 INTERNET_SetLastError(sock_get_error(errno
));
345 /******************************************************************************
347 * Basically calls 'close()' unless we should use SSL
349 BOOL
NETCON_close(WININET_NETCONNECTION
*connection
)
353 if (!NETCON_connected(connection
)) return FALSE
;
356 if (connection
->useSSL
)
358 pSSL_shutdown(connection
->ssl_s
);
359 pSSL_free(connection
->ssl_s
);
360 connection
->ssl_s
= NULL
;
362 connection
->useSSL
= FALSE
;
366 result
= closesocket(connection
->socketFD
);
367 connection
->socketFD
= -1;
371 INTERNET_SetLastError(sock_get_error(errno
));
377 static BOOL
check_hostname(X509
*cert
, char *hostname
)
379 /* FIXME: implement */
383 /******************************************************************************
384 * NETCON_secure_connect
385 * Initiates a secure connection over an existing plaintext connection.
387 BOOL
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPCWSTR hostname
)
395 /* can't connect if we are already connected */
396 if (connection
->useSSL
)
398 ERR("already connected\n");
402 connection
->ssl_s
= pSSL_new(ctx
);
403 if (!connection
->ssl_s
)
405 ERR("SSL_new failed: %s\n",
406 pERR_error_string(pERR_get_error(), 0));
407 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
411 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
413 ERR("SSL_set_fd failed: %s\n",
414 pERR_error_string(pERR_get_error(), 0));
415 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
419 if (pSSL_connect(connection
->ssl_s
) <= 0)
421 ERR("SSL_connect failed: %s\n",
422 pERR_error_string(pERR_get_error(), 0));
423 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
426 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
429 ERR("no certificate for server %s\n", debugstr_w(hostname
));
430 /* FIXME: is this the best error? */
431 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA
);
434 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
435 if (verify_res
!= X509_V_OK
)
437 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
438 /* FIXME: we should set an error and return, but we only warn at
442 len
= WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, NULL
, 0, NULL
, NULL
);
443 hostname_unix
= HeapAlloc(GetProcessHeap(), 0, len
);
446 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
449 WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, hostname_unix
, len
, NULL
, NULL
);
451 if (!check_hostname(cert
, hostname_unix
))
453 HeapFree(GetProcessHeap(), 0, hostname_unix
);
454 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID
);
458 HeapFree(GetProcessHeap(), 0, hostname_unix
);
459 connection
->useSSL
= TRUE
;
463 if (connection
->ssl_s
)
465 pSSL_shutdown(connection
->ssl_s
);
466 pSSL_free(connection
->ssl_s
);
467 connection
->ssl_s
= NULL
;
473 /******************************************************************************
475 * Connects to the specified address.
477 BOOL
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
478 unsigned int addrlen
)
482 if (!NETCON_connected(connection
)) return FALSE
;
484 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
487 WARN("Unable to connect to host (%s)\n", strerror(errno
));
488 INTERNET_SetLastError(sock_get_error(errno
));
490 closesocket(connection
->socketFD
);
491 connection
->socketFD
= -1;
498 /******************************************************************************
500 * Basically calls 'send()' unless we should use SSL
501 * number of chars send is put in *sent
503 BOOL
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
506 if (!NETCON_connected(connection
)) return FALSE
;
507 if (!connection
->useSSL
)
509 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
512 INTERNET_SetLastError(sock_get_error(errno
));
521 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
522 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
523 if (*sent
< 1 && len
)
532 /******************************************************************************
534 * Basically calls 'recv()' unless we should use SSL
535 * number of chars received is put in *recvd
537 BOOL
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
538 int *recvd
/* out */)
541 if (!NETCON_connected(connection
)) return FALSE
;
544 if (!connection
->useSSL
)
546 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
549 INTERNET_SetLastError(sock_get_error(errno
));
557 *recvd
= pSSL_read(connection
->ssl_s
, buf
, len
);
558 return *recvd
> 0 || !len
;
565 /******************************************************************************
566 * NETCON_query_data_available
567 * Returns the number of bytes of peeked data plus the number of bytes of
568 * queued, but unread data.
570 BOOL
NETCON_query_data_available(WININET_NETCONNECTION
*connection
, DWORD
*available
)
573 if (!NETCON_connected(connection
))
576 if (!connection
->useSSL
)
580 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
583 TRACE("%d bytes of queued, but unread data\n", unread
);
584 *available
+= unread
;
591 *available
= pSSL_pending(connection
->ssl_s
);
597 LPCVOID
NETCON_GetCert(WININET_NETCONNECTION
*connection
)
601 unsigned char* buffer
,*p
;
603 BOOL malloced
= FALSE
;
606 if (!connection
->useSSL
)
609 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
611 len
= pi2d_X509(cert
,&p
);
613 * SSL 0.9.7 and above malloc the buffer if it is null.
614 * however earlier version do not and so we would need to alloc the buffer.
616 * see the i2d_X509 man page for more details.
620 buffer
= HeapAlloc(GetProcessHeap(),0,len
);
622 len
= pi2d_X509(cert
,&p
);
630 r
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
635 HeapFree(GetProcessHeap(),0,buffer
);
643 DWORD
NETCON_set_timeout(WININET_NETCONNECTION
*connection
, BOOL send
, int value
)
648 /* FIXME: we should probably store the timeout in the connection to set
649 * when we do connect */
650 if (!NETCON_connected(connection
))
651 return ERROR_SUCCESS
;
653 /* value is in milliseconds, convert to struct timeval */
654 tv
.tv_sec
= value
/ 1000;
655 tv
.tv_usec
= (value
% 1000) * 1000;
657 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
658 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, (void*)&tv
,
663 WARN("setsockopt failed (%s)\n", strerror(errno
));
664 return sock_get_error(errno
);
667 return ERROR_SUCCESS
;