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 #include <sys/types.h>
30 #ifdef HAVE_SYS_POLL_H
31 # include <sys/poll.h>
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
50 #ifdef HAVE_NETINET_IN_H
51 # include <netinet/in.h>
53 #ifdef HAVE_OPENSSL_SSL_H
54 # include <openssl/ssl.h>
58 #ifdef HAVE_SYS_SOCKET_H
59 # include <sys/socket.h>
68 #include "wine/library.h"
75 #include "wine/debug.h"
78 /* To avoid conflicts with the Unix socket headers. we only need it for
79 * the error codes anyway. */
83 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
84 #define sock_get_error(x) WSAGetLastError()
86 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
89 * This should use winsock - To use winsock the functions will have to change a bit
90 * as they are designed for unix sockets.
91 * SSL stuff should use crypt32.dll
96 #include <openssl/err.h>
98 static void *OpenSSL_ssl_handle
;
99 static void *OpenSSL_crypto_handle
;
101 static SSL_METHOD
*meth
;
104 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
106 /* OpenSSL functions that we use */
107 MAKE_FUNCPTR(SSL_library_init
);
108 MAKE_FUNCPTR(SSL_load_error_strings
);
109 MAKE_FUNCPTR(SSLv23_method
);
110 MAKE_FUNCPTR(SSL_CTX_new
);
111 MAKE_FUNCPTR(SSL_new
);
112 MAKE_FUNCPTR(SSL_free
);
113 MAKE_FUNCPTR(SSL_set_fd
);
114 MAKE_FUNCPTR(SSL_connect
);
115 MAKE_FUNCPTR(SSL_shutdown
);
116 MAKE_FUNCPTR(SSL_write
);
117 MAKE_FUNCPTR(SSL_read
);
118 MAKE_FUNCPTR(SSL_get_verify_result
);
119 MAKE_FUNCPTR(SSL_get_peer_certificate
);
120 MAKE_FUNCPTR(SSL_CTX_get_timeout
);
121 MAKE_FUNCPTR(SSL_CTX_set_timeout
);
122 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths
);
123 MAKE_FUNCPTR(i2d_X509
);
125 /* OpenSSL's libcrypto functions that we use */
126 MAKE_FUNCPTR(BIO_new_fp
);
127 MAKE_FUNCPTR(ERR_get_error
);
128 MAKE_FUNCPTR(ERR_error_string
);
133 BOOL
NETCON_init(WININET_NETCONNECTION
*connection
, BOOL useSSL
)
135 connection
->useSSL
= FALSE
;
136 connection
->socketFD
= -1;
139 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
140 TRACE("using SSL connection\n");
141 if (OpenSSL_ssl_handle
) /* already initialized everything */
143 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
144 if (!OpenSSL_ssl_handle
)
146 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
148 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
151 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
152 if (!OpenSSL_crypto_handle
)
154 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
156 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
160 /* mmm nice ugly macroness */
162 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
165 ERR("failed to load symbol %s\n", #x); \
166 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
170 DYNSSL(SSL_library_init
);
171 DYNSSL(SSL_load_error_strings
);
172 DYNSSL(SSLv23_method
);
178 DYNSSL(SSL_shutdown
);
181 DYNSSL(SSL_get_verify_result
);
182 DYNSSL(SSL_get_peer_certificate
);
183 DYNSSL(SSL_CTX_get_timeout
);
184 DYNSSL(SSL_CTX_set_timeout
);
185 DYNSSL(SSL_CTX_set_default_verify_paths
);
189 #define DYNCRYPTO(x) \
190 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
193 ERR("failed to load symbol %s\n", #x); \
194 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
197 DYNCRYPTO(BIO_new_fp
);
198 DYNCRYPTO(ERR_get_error
);
199 DYNCRYPTO(ERR_error_string
);
203 pSSL_load_error_strings();
204 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
206 meth
= pSSLv23_method();
207 connection
->peek_msg
= NULL
;
208 connection
->peek_msg_mem
= NULL
;
210 FIXME("can't use SSL, not compiled in.\n");
211 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
218 BOOL
NETCON_connected(WININET_NETCONNECTION
*connection
)
220 if (connection
->socketFD
== -1)
227 /* translate a unix error code into a winsock one */
228 static int sock_get_error( int err
)
230 #if !defined(__MINGW32__) && !defined (_MSC_VER)
233 case EINTR
: return WSAEINTR
;
234 case EBADF
: return WSAEBADF
;
236 case EACCES
: return WSAEACCES
;
237 case EFAULT
: return WSAEFAULT
;
238 case EINVAL
: return WSAEINVAL
;
239 case EMFILE
: return WSAEMFILE
;
240 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
241 case EINPROGRESS
: return WSAEINPROGRESS
;
242 case EALREADY
: return WSAEALREADY
;
243 case ENOTSOCK
: return WSAENOTSOCK
;
244 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
245 case EMSGSIZE
: return WSAEMSGSIZE
;
246 case EPROTOTYPE
: return WSAEPROTOTYPE
;
247 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
248 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
249 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
250 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
251 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
252 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
253 case EADDRINUSE
: return WSAEADDRINUSE
;
254 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
255 case ENETDOWN
: return WSAENETDOWN
;
256 case ENETUNREACH
: return WSAENETUNREACH
;
257 case ENETRESET
: return WSAENETRESET
;
258 case ECONNABORTED
: return WSAECONNABORTED
;
260 case ECONNRESET
: return WSAECONNRESET
;
261 case ENOBUFS
: return WSAENOBUFS
;
262 case EISCONN
: return WSAEISCONN
;
263 case ENOTCONN
: return WSAENOTCONN
;
264 case ESHUTDOWN
: return WSAESHUTDOWN
;
265 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
266 case ETIMEDOUT
: return WSAETIMEDOUT
;
267 case ECONNREFUSED
: return WSAECONNREFUSED
;
268 case ELOOP
: return WSAELOOP
;
269 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
270 case EHOSTDOWN
: return WSAEHOSTDOWN
;
271 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
272 case ENOTEMPTY
: return WSAENOTEMPTY
;
274 case EPROCLIM
: return WSAEPROCLIM
;
277 case EUSERS
: return WSAEUSERS
;
280 case EDQUOT
: return WSAEDQUOT
;
283 case ESTALE
: return WSAESTALE
;
286 case EREMOTE
: return WSAEREMOTE
;
288 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
295 /******************************************************************************
297 * Basically calls 'socket()'
299 BOOL
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
300 int type
, int protocol
)
303 if (connection
->useSSL
)
307 connection
->socketFD
= socket(domain
, type
, protocol
);
308 if (connection
->socketFD
== -1)
310 INTERNET_SetLastError(sock_get_error(errno
));
316 /******************************************************************************
318 * Basically calls 'close()' unless we should use SSL
320 BOOL
NETCON_close(WININET_NETCONNECTION
*connection
)
324 if (!NETCON_connected(connection
)) return FALSE
;
327 if (connection
->useSSL
)
329 HeapFree(GetProcessHeap(),0,connection
->peek_msg_mem
);
330 connection
->peek_msg
= NULL
;
331 connection
->peek_msg_mem
= NULL
;
332 connection
->peek_len
= 0;
334 pSSL_shutdown(connection
->ssl_s
);
335 pSSL_free(connection
->ssl_s
);
336 connection
->ssl_s
= NULL
;
338 connection
->useSSL
= FALSE
;
342 result
= closesocket(connection
->socketFD
);
343 connection
->socketFD
= -1;
347 INTERNET_SetLastError(sock_get_error(errno
));
353 static BOOL
check_hostname(X509
*cert
, char *hostname
)
355 /* FIXME: implement */
359 /******************************************************************************
360 * NETCON_secure_connect
361 * Initiates a secure connection over an existing plaintext connection.
363 BOOL
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPCWSTR hostname
)
371 /* can't connect if we are already connected */
372 if (connection
->useSSL
)
374 ERR("already connected\n");
378 ctx
= pSSL_CTX_new(meth
);
379 if (!pSSL_CTX_set_default_verify_paths(ctx
))
381 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
382 pERR_error_string(pERR_get_error(), 0));
383 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
386 connection
->ssl_s
= pSSL_new(ctx
);
387 if (!connection
->ssl_s
)
389 ERR("SSL_new failed: %s\n",
390 pERR_error_string(pERR_get_error(), 0));
391 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
395 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
397 ERR("SSL_set_fd failed: %s\n",
398 pERR_error_string(pERR_get_error(), 0));
399 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
403 if (pSSL_connect(connection
->ssl_s
) <= 0)
405 ERR("SSL_connect failed: %s\n",
406 pERR_error_string(pERR_get_error(), 0));
407 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
410 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
413 ERR("no certificate for server %s\n", debugstr_w(hostname
));
414 /* FIXME: is this the best error? */
415 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA
);
418 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
419 if (verify_res
!= X509_V_OK
)
421 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
422 /* FIXME: we should set an error and return, but we only warn at
426 len
= WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, NULL
, 0, NULL
, NULL
);
427 hostname_unix
= HeapAlloc(GetProcessHeap(), 0, len
);
430 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
433 WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, hostname_unix
, len
, NULL
, NULL
);
435 if (!check_hostname(cert
, hostname_unix
))
437 HeapFree(GetProcessHeap(), 0, hostname_unix
);
438 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID
);
442 HeapFree(GetProcessHeap(), 0, hostname_unix
);
443 connection
->useSSL
= TRUE
;
447 if (connection
->ssl_s
)
449 pSSL_shutdown(connection
->ssl_s
);
450 pSSL_free(connection
->ssl_s
);
451 connection
->ssl_s
= NULL
;
457 /******************************************************************************
459 * Connects to the specified address.
461 BOOL
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
462 unsigned int addrlen
)
466 if (!NETCON_connected(connection
)) return FALSE
;
468 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
471 WARN("Unable to connect to host (%s)\n", strerror(errno
));
472 INTERNET_SetLastError(sock_get_error(errno
));
474 closesocket(connection
->socketFD
);
475 connection
->socketFD
= -1;
482 /******************************************************************************
484 * Basically calls 'send()' unless we should use SSL
485 * number of chars send is put in *sent
487 BOOL
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
490 if (!NETCON_connected(connection
)) return FALSE
;
491 if (!connection
->useSSL
)
493 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
496 INTERNET_SetLastError(sock_get_error(errno
));
505 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
506 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
507 if (*sent
< 1 && len
)
516 /******************************************************************************
518 * Basically calls 'recv()' unless we should use SSL
519 * number of chars received is put in *recvd
521 BOOL
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
522 int *recvd
/* out */)
525 if (!NETCON_connected(connection
)) return FALSE
;
528 if (!connection
->useSSL
)
530 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
533 INTERNET_SetLastError(sock_get_error(errno
));
541 if (flags
& ~(MSG_PEEK
|MSG_WAITALL
))
542 FIXME("SSL_read does not support the following flag: %08x\n", flags
);
544 /* this ugly hack is all for MSG_PEEK. eww gross */
545 if (flags
& MSG_PEEK
&& !connection
->peek_msg
)
547 connection
->peek_msg
= connection
->peek_msg_mem
= HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len
) + 1);
549 else if (flags
& MSG_PEEK
&& connection
->peek_msg
)
551 if (len
< connection
->peek_len
)
552 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
553 *recvd
= min(len
, connection
->peek_len
);
554 memcpy(buf
, connection
->peek_msg
, *recvd
);
557 else if (connection
->peek_msg
)
559 *recvd
= min(len
, connection
->peek_len
);
560 memcpy(buf
, connection
->peek_msg
, *recvd
);
561 connection
->peek_len
-= *recvd
;
562 connection
->peek_msg
+= *recvd
;
563 if (connection
->peek_len
== 0)
565 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
566 connection
->peek_msg_mem
= NULL
;
567 connection
->peek_msg
= NULL
;
569 /* check if we got enough data from the peek buffer */
570 if (!(flags
& MSG_WAITALL
) || (*recvd
== len
))
572 /* otherwise, fall through */
574 *recvd
+= pSSL_read(connection
->ssl_s
, (char*)buf
+ *recvd
, len
- *recvd
);
575 if (flags
& MSG_PEEK
) /* must copy stuff into buffer */
577 connection
->peek_len
= *recvd
;
580 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
581 connection
->peek_msg_mem
= NULL
;
582 connection
->peek_msg
= NULL
;
585 memcpy(connection
->peek_msg
, buf
, *recvd
);
587 if (*recvd
< 1 && len
)
596 /******************************************************************************
597 * NETCON_query_data_available
598 * Returns the number of bytes of peeked data plus the number of bytes of
599 * queued, but unread data.
601 BOOL
NETCON_query_data_available(WININET_NETCONNECTION
*connection
, DWORD
*available
)
604 if (!NETCON_connected(connection
))
608 if (connection
->peek_msg
) *available
= connection
->peek_len
;
612 if (!connection
->useSSL
)
615 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
618 TRACE("%d bytes of queued, but unread data\n", unread
);
619 *available
+= unread
;
626 /******************************************************************************
629 BOOL
NETCON_getNextLine(WININET_NETCONNECTION
*connection
, LPSTR lpszBuffer
, LPDWORD dwBuffer
)
634 if (!NETCON_connected(connection
)) return FALSE
;
636 if (!connection
->useSSL
)
640 BOOL bSuccess
= FALSE
;
644 FD_SET(connection
->socketFD
, &infd
);
645 tv
.tv_sec
=RESPONSE_TIMEOUT
;
648 while (nRecv
< *dwBuffer
)
650 if (select(connection
->socketFD
+1,&infd
,NULL
,NULL
,&tv
) > 0)
652 if (recv(connection
->socketFD
, &lpszBuffer
[nRecv
], 1, 0) <= 0)
654 INTERNET_SetLastError(sock_get_error(errno
));
658 if (lpszBuffer
[nRecv
] == '\n')
663 if (lpszBuffer
[nRecv
] != '\r')
668 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT
);
673 lend
: /* FIXME: don't use labels */
676 lpszBuffer
[nRecv
++] = '\0';
678 TRACE(":%u %s\n", nRecv
, lpszBuffer
);
693 prev_timeout
= pSSL_CTX_get_timeout(ctx
);
694 pSSL_CTX_set_timeout(ctx
, RESPONSE_TIMEOUT
);
696 while (nRecv
< *dwBuffer
)
699 if (!NETCON_recv(connection
, &lpszBuffer
[nRecv
], 1, 0, &recv
))
701 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED
);
705 if (lpszBuffer
[nRecv
] == '\n')
710 if (lpszBuffer
[nRecv
] != '\r')
714 pSSL_CTX_set_timeout(ctx
, prev_timeout
);
717 lpszBuffer
[nRecv
++] = '\0';
719 TRACE("_SSL:%u %s\n", nRecv
, lpszBuffer
);
730 LPCVOID
NETCON_GetCert(WININET_NETCONNECTION
*connection
)
734 unsigned char* buffer
,*p
;
736 BOOL malloced
= FALSE
;
739 if (!connection
->useSSL
)
742 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
744 len
= pi2d_X509(cert
,&p
);
746 * SSL 0.9.7 and above malloc the buffer if it is null.
747 * however earlier version do not and so we would need to alloc the buffer.
749 * see the i2d_X509 man page for more details.
753 buffer
= HeapAlloc(GetProcessHeap(),0,len
);
755 len
= pi2d_X509(cert
,&p
);
763 r
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
768 HeapFree(GetProcessHeap(),0,buffer
);
776 DWORD
NETCON_set_timeout(WININET_NETCONNECTION
*connection
, BOOL send
, int value
)
781 /* FIXME: we should probably store the timeout in the connection to set
782 * when we do connect */
783 if (!NETCON_connected(connection
))
784 return ERROR_SUCCESS
;
786 /* value is in milliseconds, convert to struct timeval */
787 tv
.tv_sec
= value
/ 1000;
788 tv
.tv_usec
= (value
% 1000) * 1000;
790 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
791 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, &tv
,
796 WARN("setsockopt failed (%s)\n", strerror(errno
));
797 return sock_get_error(errno
);
800 return ERROR_SUCCESS
;