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 */
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)
226 /* translate a unix error code into a winsock one */
227 static int sock_get_error( int err
)
229 #if !defined(__MINGW32__) && !defined (_MSC_VER)
232 case EINTR
: return WSAEINTR
;
233 case EBADF
: return WSAEBADF
;
235 case EACCES
: return WSAEACCES
;
236 case EFAULT
: return WSAEFAULT
;
237 case EINVAL
: return WSAEINVAL
;
238 case EMFILE
: return WSAEMFILE
;
239 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
240 case EINPROGRESS
: return WSAEINPROGRESS
;
241 case EALREADY
: return WSAEALREADY
;
242 case ENOTSOCK
: return WSAENOTSOCK
;
243 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
244 case EMSGSIZE
: return WSAEMSGSIZE
;
245 case EPROTOTYPE
: return WSAEPROTOTYPE
;
246 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
247 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
248 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
249 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
250 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
251 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
252 case EADDRINUSE
: return WSAEADDRINUSE
;
253 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
254 case ENETDOWN
: return WSAENETDOWN
;
255 case ENETUNREACH
: return WSAENETUNREACH
;
256 case ENETRESET
: return WSAENETRESET
;
257 case ECONNABORTED
: return WSAECONNABORTED
;
259 case ECONNRESET
: return WSAECONNRESET
;
260 case ENOBUFS
: return WSAENOBUFS
;
261 case EISCONN
: return WSAEISCONN
;
262 case ENOTCONN
: return WSAENOTCONN
;
263 case ESHUTDOWN
: return WSAESHUTDOWN
;
264 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
265 case ETIMEDOUT
: return WSAETIMEDOUT
;
266 case ECONNREFUSED
: return WSAECONNREFUSED
;
267 case ELOOP
: return WSAELOOP
;
268 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
269 case EHOSTDOWN
: return WSAEHOSTDOWN
;
270 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
271 case ENOTEMPTY
: return WSAENOTEMPTY
;
273 case EPROCLIM
: return WSAEPROCLIM
;
276 case EUSERS
: return WSAEUSERS
;
279 case EDQUOT
: return WSAEDQUOT
;
282 case ESTALE
: return WSAESTALE
;
285 case EREMOTE
: return WSAEREMOTE
;
287 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
293 /******************************************************************************
295 * Basically calls 'socket()'
297 BOOL
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
298 int type
, int protocol
)
301 if (connection
->useSSL
)
305 connection
->socketFD
= socket(domain
, type
, protocol
);
306 if (connection
->socketFD
== -1)
308 INTERNET_SetLastError(sock_get_error(errno
));
314 /******************************************************************************
316 * Basically calls 'close()' unless we should use SSL
318 BOOL
NETCON_close(WININET_NETCONNECTION
*connection
)
322 if (!NETCON_connected(connection
)) return FALSE
;
325 if (connection
->useSSL
)
327 HeapFree(GetProcessHeap(),0,connection
->peek_msg_mem
);
328 connection
->peek_msg
= NULL
;
329 connection
->peek_msg_mem
= NULL
;
330 connection
->peek_len
= 0;
332 pSSL_shutdown(connection
->ssl_s
);
333 pSSL_free(connection
->ssl_s
);
334 connection
->ssl_s
= NULL
;
336 connection
->useSSL
= FALSE
;
340 result
= closesocket(connection
->socketFD
);
341 connection
->socketFD
= -1;
345 INTERNET_SetLastError(sock_get_error(errno
));
351 static BOOL
check_hostname(X509
*cert
, char *hostname
)
353 /* FIXME: implement */
357 /******************************************************************************
358 * NETCON_secure_connect
359 * Initiates a secure connection over an existing plaintext connection.
361 BOOL
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPCWSTR hostname
)
369 /* can't connect if we are already connected */
370 if (connection
->useSSL
)
372 ERR("already connected\n");
376 ctx
= pSSL_CTX_new(meth
);
377 if (!pSSL_CTX_set_default_verify_paths(ctx
))
379 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
380 pERR_error_string(pERR_get_error(), 0));
381 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
384 connection
->ssl_s
= pSSL_new(ctx
);
385 if (!connection
->ssl_s
)
387 ERR("SSL_new failed: %s\n",
388 pERR_error_string(pERR_get_error(), 0));
389 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
393 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
395 ERR("SSL_set_fd failed: %s\n",
396 pERR_error_string(pERR_get_error(), 0));
397 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
401 if (pSSL_connect(connection
->ssl_s
) <= 0)
403 ERR("SSL_connect failed: %s\n",
404 pERR_error_string(pERR_get_error(), 0));
405 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
408 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
411 ERR("no certificate for server %s\n", debugstr_w(hostname
));
412 /* FIXME: is this the best error? */
413 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA
);
416 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
417 if (verify_res
!= X509_V_OK
)
419 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
420 /* FIXME: we should set an error and return, but we only warn at
424 len
= WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, NULL
, 0, NULL
, NULL
);
425 hostname_unix
= HeapAlloc(GetProcessHeap(), 0, len
);
428 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
431 WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, hostname_unix
, len
, NULL
, NULL
);
433 if (!check_hostname(cert
, hostname_unix
))
435 HeapFree(GetProcessHeap(), 0, hostname_unix
);
436 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID
);
440 HeapFree(GetProcessHeap(), 0, hostname_unix
);
441 connection
->useSSL
= TRUE
;
445 if (connection
->ssl_s
)
447 pSSL_shutdown(connection
->ssl_s
);
448 pSSL_free(connection
->ssl_s
);
449 connection
->ssl_s
= NULL
;
455 /******************************************************************************
457 * Connects to the specified address.
459 BOOL
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
460 unsigned int addrlen
)
464 if (!NETCON_connected(connection
)) return FALSE
;
466 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
469 WARN("Unable to connect to host (%s)\n", strerror(errno
));
470 INTERNET_SetLastError(sock_get_error(errno
));
472 closesocket(connection
->socketFD
);
473 connection
->socketFD
= -1;
480 /******************************************************************************
482 * Basically calls 'send()' unless we should use SSL
483 * number of chars send is put in *sent
485 BOOL
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
488 if (!NETCON_connected(connection
)) return FALSE
;
489 if (!connection
->useSSL
)
491 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
494 INTERNET_SetLastError(sock_get_error(errno
));
503 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
504 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
505 if (*sent
< 1 && len
)
514 /******************************************************************************
516 * Basically calls 'recv()' unless we should use SSL
517 * number of chars received is put in *recvd
519 BOOL
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
520 int *recvd
/* out */)
523 if (!NETCON_connected(connection
)) return FALSE
;
526 if (!connection
->useSSL
)
528 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
531 INTERNET_SetLastError(sock_get_error(errno
));
539 if (flags
& ~(MSG_PEEK
|MSG_WAITALL
))
540 FIXME("SSL_read does not support the following flag: %08x\n", flags
);
542 /* this ugly hack is all for MSG_PEEK. eww gross */
543 if (flags
& MSG_PEEK
&& !connection
->peek_msg
)
545 connection
->peek_msg
= connection
->peek_msg_mem
= HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len
) + 1);
547 else if (flags
& MSG_PEEK
&& connection
->peek_msg
)
549 if (len
< connection
->peek_len
)
550 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
551 *recvd
= min(len
, connection
->peek_len
);
552 memcpy(buf
, connection
->peek_msg
, *recvd
);
555 else if (connection
->peek_msg
)
557 *recvd
= min(len
, connection
->peek_len
);
558 memcpy(buf
, connection
->peek_msg
, *recvd
);
559 connection
->peek_len
-= *recvd
;
560 connection
->peek_msg
+= *recvd
;
561 if (connection
->peek_len
== 0)
563 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
564 connection
->peek_msg_mem
= NULL
;
565 connection
->peek_msg
= NULL
;
567 /* check if we got enough data from the peek buffer */
568 if (!(flags
& MSG_WAITALL
) || (*recvd
== len
))
570 /* otherwise, fall through */
572 *recvd
+= pSSL_read(connection
->ssl_s
, (char*)buf
+ *recvd
, len
- *recvd
);
573 if (flags
& MSG_PEEK
) /* must copy stuff into buffer */
575 connection
->peek_len
= *recvd
;
578 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
579 connection
->peek_msg_mem
= NULL
;
580 connection
->peek_msg
= NULL
;
583 memcpy(connection
->peek_msg
, buf
, *recvd
);
585 if (*recvd
< 1 && len
)
594 /******************************************************************************
595 * NETCON_query_data_available
596 * Returns the number of bytes of peeked data plus the number of bytes of
597 * queued, but unread data.
599 BOOL
NETCON_query_data_available(WININET_NETCONNECTION
*connection
, DWORD
*available
)
602 if (!NETCON_connected(connection
))
606 if (connection
->peek_msg
) *available
= connection
->peek_len
;
610 if (!connection
->useSSL
)
613 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
616 TRACE("%d bytes of queued, but unread data\n", unread
);
617 *available
+= unread
;
624 /******************************************************************************
627 BOOL
NETCON_getNextLine(WININET_NETCONNECTION
*connection
, LPSTR lpszBuffer
, LPDWORD dwBuffer
)
632 if (!NETCON_connected(connection
)) return FALSE
;
634 if (!connection
->useSSL
)
637 BOOL bSuccess
= FALSE
;
640 pfd
.fd
= connection
->socketFD
;
643 while (nRecv
< *dwBuffer
)
645 if (poll(&pfd
,1, RESPONSE_TIMEOUT
* 1000) > 0)
647 if (recv(connection
->socketFD
, &lpszBuffer
[nRecv
], 1, 0) <= 0)
649 INTERNET_SetLastError(sock_get_error(errno
));
653 if (lpszBuffer
[nRecv
] == '\n')
658 if (lpszBuffer
[nRecv
] != '\r')
663 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT
);
668 lend
: /* FIXME: don't use labels */
671 lpszBuffer
[nRecv
++] = '\0';
673 TRACE(":%u %s\n", nRecv
, lpszBuffer
);
688 prev_timeout
= pSSL_CTX_get_timeout(ctx
);
689 pSSL_CTX_set_timeout(ctx
, RESPONSE_TIMEOUT
);
691 while (nRecv
< *dwBuffer
)
694 if (!NETCON_recv(connection
, &lpszBuffer
[nRecv
], 1, 0, &recv
))
696 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED
);
700 if (lpszBuffer
[nRecv
] == '\n')
705 if (lpszBuffer
[nRecv
] != '\r')
709 pSSL_CTX_set_timeout(ctx
, prev_timeout
);
712 lpszBuffer
[nRecv
++] = '\0';
714 TRACE("_SSL:%u %s\n", nRecv
, lpszBuffer
);
725 LPCVOID
NETCON_GetCert(WININET_NETCONNECTION
*connection
)
729 unsigned char* buffer
,*p
;
731 BOOL malloced
= FALSE
;
734 if (!connection
->useSSL
)
737 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
739 len
= pi2d_X509(cert
,&p
);
741 * SSL 0.9.7 and above malloc the buffer if it is null.
742 * however earlier version do not and so we would need to alloc the buffer.
744 * see the i2d_X509 man page for more details.
748 buffer
= HeapAlloc(GetProcessHeap(),0,len
);
750 len
= pi2d_X509(cert
,&p
);
758 r
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
763 HeapFree(GetProcessHeap(),0,buffer
);
771 DWORD
NETCON_set_timeout(WININET_NETCONNECTION
*connection
, BOOL send
, int value
)
776 /* FIXME: we should probably store the timeout in the connection to set
777 * when we do connect */
778 if (!NETCON_connected(connection
))
779 return ERROR_SUCCESS
;
781 /* value is in milliseconds, convert to struct timeval */
782 tv
.tv_sec
= value
/ 1000;
783 tv
.tv_usec
= (value
% 1000) * 1000;
785 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
786 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, &tv
,
791 WARN("setsockopt failed (%s)\n", strerror(errno
));
792 return sock_get_error(errno
);
795 return ERROR_SUCCESS
;