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 #ifdef HAVE_SYS_SOCKET_H
37 # include <sys/socket.h>
39 #ifdef HAVE_SYS_FILIO_H
40 # include <sys/filio.h>
45 #ifdef HAVE_SYS_IOCTL_H
46 # include <sys/ioctl.h>
52 #ifdef HAVE_NETINET_IN_H
53 # include <netinet/in.h>
55 #ifdef HAVE_OPENSSL_SSL_H
56 # include <openssl/ssl.h>
67 #include "wine/library.h"
74 #include "wine/debug.h"
77 /* To avoid conflicts with the Unix socket headers. we only need it for
78 * the error codes anyway. */
82 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
85 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
88 * This should use winsock - To use winsock the functions will have to change a bit
89 * as they are designed for unix sockets.
90 * SSL stuff should use crypt32.dll
95 #include <openssl/err.h>
97 static void *OpenSSL_ssl_handle
;
98 static void *OpenSSL_crypto_handle
;
100 static SSL_METHOD
*meth
;
103 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
105 /* OpenSSL functions that we use */
106 MAKE_FUNCPTR(SSL_library_init
);
107 MAKE_FUNCPTR(SSL_load_error_strings
);
108 MAKE_FUNCPTR(SSLv23_method
);
109 MAKE_FUNCPTR(SSL_CTX_new
);
110 MAKE_FUNCPTR(SSL_new
);
111 MAKE_FUNCPTR(SSL_free
);
112 MAKE_FUNCPTR(SSL_set_fd
);
113 MAKE_FUNCPTR(SSL_connect
);
114 MAKE_FUNCPTR(SSL_shutdown
);
115 MAKE_FUNCPTR(SSL_write
);
116 MAKE_FUNCPTR(SSL_read
);
117 MAKE_FUNCPTR(SSL_get_verify_result
);
118 MAKE_FUNCPTR(SSL_get_peer_certificate
);
119 MAKE_FUNCPTR(SSL_CTX_get_timeout
);
120 MAKE_FUNCPTR(SSL_CTX_set_timeout
);
121 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths
);
122 MAKE_FUNCPTR(i2d_X509
);
124 /* OpenSSL's libcrypto functions that we use */
125 MAKE_FUNCPTR(BIO_new_fp
);
126 MAKE_FUNCPTR(ERR_get_error
);
127 MAKE_FUNCPTR(ERR_error_string
);
132 BOOL
NETCON_init(WININET_NETCONNECTION
*connection
, BOOL useSSL
)
134 connection
->useSSL
= FALSE
;
135 connection
->socketFD
= -1;
138 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
139 TRACE("using SSL connection\n");
140 if (OpenSSL_ssl_handle
) /* already initialized everything */
142 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
143 if (!OpenSSL_ssl_handle
)
145 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
147 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
150 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
151 if (!OpenSSL_crypto_handle
)
153 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
155 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
159 /* mmm nice ugly macroness */
161 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
164 ERR("failed to load symbol %s\n", #x); \
165 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
169 DYNSSL(SSL_library_init
);
170 DYNSSL(SSL_load_error_strings
);
171 DYNSSL(SSLv23_method
);
177 DYNSSL(SSL_shutdown
);
180 DYNSSL(SSL_get_verify_result
);
181 DYNSSL(SSL_get_peer_certificate
);
182 DYNSSL(SSL_CTX_get_timeout
);
183 DYNSSL(SSL_CTX_set_timeout
);
184 DYNSSL(SSL_CTX_set_default_verify_paths
);
188 #define DYNCRYPTO(x) \
189 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
192 ERR("failed to load symbol %s\n", #x); \
193 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
196 DYNCRYPTO(BIO_new_fp
);
197 DYNCRYPTO(ERR_get_error
);
198 DYNCRYPTO(ERR_error_string
);
202 pSSL_load_error_strings();
203 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
205 meth
= pSSLv23_method();
206 connection
->peek_msg
= NULL
;
207 connection
->peek_msg_mem
= NULL
;
209 FIXME("can't use SSL, not compiled in.\n");
210 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
217 BOOL
NETCON_connected(WININET_NETCONNECTION
*connection
)
219 if (connection
->socketFD
== -1)
225 /* translate a unix error code into a winsock one */
226 static int sock_get_error( int err
)
228 #if !defined(__MINGW32__) && !defined (_MSC_VER)
231 case EINTR
: return WSAEINTR
;
232 case EBADF
: return WSAEBADF
;
234 case EACCES
: return WSAEACCES
;
235 case EFAULT
: return WSAEFAULT
;
236 case EINVAL
: return WSAEINVAL
;
237 case EMFILE
: return WSAEMFILE
;
238 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
239 case EINPROGRESS
: return WSAEINPROGRESS
;
240 case EALREADY
: return WSAEALREADY
;
241 case ENOTSOCK
: return WSAENOTSOCK
;
242 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
243 case EMSGSIZE
: return WSAEMSGSIZE
;
244 case EPROTOTYPE
: return WSAEPROTOTYPE
;
245 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
246 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
247 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
248 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
249 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
250 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
251 case EADDRINUSE
: return WSAEADDRINUSE
;
252 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
253 case ENETDOWN
: return WSAENETDOWN
;
254 case ENETUNREACH
: return WSAENETUNREACH
;
255 case ENETRESET
: return WSAENETRESET
;
256 case ECONNABORTED
: return WSAECONNABORTED
;
258 case ECONNRESET
: return WSAECONNRESET
;
259 case ENOBUFS
: return WSAENOBUFS
;
260 case EISCONN
: return WSAEISCONN
;
261 case ENOTCONN
: return WSAENOTCONN
;
262 case ESHUTDOWN
: return WSAESHUTDOWN
;
263 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
264 case ETIMEDOUT
: return WSAETIMEDOUT
;
265 case ECONNREFUSED
: return WSAECONNREFUSED
;
266 case ELOOP
: return WSAELOOP
;
267 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
268 case EHOSTDOWN
: return WSAEHOSTDOWN
;
269 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
270 case ENOTEMPTY
: return WSAENOTEMPTY
;
272 case EPROCLIM
: return WSAEPROCLIM
;
275 case EUSERS
: return WSAEUSERS
;
278 case EDQUOT
: return WSAEDQUOT
;
281 case ESTALE
: return WSAESTALE
;
284 case EREMOTE
: return WSAEREMOTE
;
286 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
292 /******************************************************************************
294 * Basically calls 'socket()'
296 BOOL
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
297 int type
, int protocol
)
300 if (connection
->useSSL
)
304 connection
->socketFD
= socket(domain
, type
, protocol
);
305 if (connection
->socketFD
== -1)
307 INTERNET_SetLastError(sock_get_error(errno
));
313 /******************************************************************************
315 * Basically calls 'close()' unless we should use SSL
317 BOOL
NETCON_close(WININET_NETCONNECTION
*connection
)
321 if (!NETCON_connected(connection
)) return FALSE
;
324 if (connection
->useSSL
)
326 HeapFree(GetProcessHeap(),0,connection
->peek_msg_mem
);
327 connection
->peek_msg
= NULL
;
328 connection
->peek_msg_mem
= NULL
;
329 connection
->peek_len
= 0;
331 pSSL_shutdown(connection
->ssl_s
);
332 pSSL_free(connection
->ssl_s
);
333 connection
->ssl_s
= NULL
;
335 connection
->useSSL
= FALSE
;
339 result
= closesocket(connection
->socketFD
);
340 connection
->socketFD
= -1;
344 INTERNET_SetLastError(sock_get_error(errno
));
350 static BOOL
check_hostname(X509
*cert
, char *hostname
)
352 /* FIXME: implement */
356 /******************************************************************************
357 * NETCON_secure_connect
358 * Initiates a secure connection over an existing plaintext connection.
360 BOOL
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPCWSTR hostname
)
368 /* can't connect if we are already connected */
369 if (connection
->useSSL
)
371 ERR("already connected\n");
375 ctx
= pSSL_CTX_new(meth
);
376 if (!pSSL_CTX_set_default_verify_paths(ctx
))
378 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
379 pERR_error_string(pERR_get_error(), 0));
380 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
383 connection
->ssl_s
= pSSL_new(ctx
);
384 if (!connection
->ssl_s
)
386 ERR("SSL_new failed: %s\n",
387 pERR_error_string(pERR_get_error(), 0));
388 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
392 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
394 ERR("SSL_set_fd failed: %s\n",
395 pERR_error_string(pERR_get_error(), 0));
396 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
400 if (pSSL_connect(connection
->ssl_s
) <= 0)
402 ERR("SSL_connect failed: %s\n",
403 pERR_error_string(pERR_get_error(), 0));
404 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
407 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
410 ERR("no certificate for server %s\n", debugstr_w(hostname
));
411 /* FIXME: is this the best error? */
412 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA
);
415 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
416 if (verify_res
!= X509_V_OK
)
418 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
419 /* FIXME: we should set an error and return, but we only warn at
423 len
= WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, NULL
, 0, NULL
, NULL
);
424 hostname_unix
= HeapAlloc(GetProcessHeap(), 0, len
);
427 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
430 WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, hostname_unix
, len
, NULL
, NULL
);
432 if (!check_hostname(cert
, hostname_unix
))
434 HeapFree(GetProcessHeap(), 0, hostname_unix
);
435 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID
);
439 HeapFree(GetProcessHeap(), 0, hostname_unix
);
440 connection
->useSSL
= TRUE
;
444 if (connection
->ssl_s
)
446 pSSL_shutdown(connection
->ssl_s
);
447 pSSL_free(connection
->ssl_s
);
448 connection
->ssl_s
= NULL
;
454 /******************************************************************************
456 * Connects to the specified address.
458 BOOL
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
459 unsigned int addrlen
)
463 if (!NETCON_connected(connection
)) return FALSE
;
465 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
468 WARN("Unable to connect to host (%s)\n", strerror(errno
));
469 INTERNET_SetLastError(sock_get_error(errno
));
471 closesocket(connection
->socketFD
);
472 connection
->socketFD
= -1;
479 /******************************************************************************
481 * Basically calls 'send()' unless we should use SSL
482 * number of chars send is put in *sent
484 BOOL
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
487 if (!NETCON_connected(connection
)) return FALSE
;
488 if (!connection
->useSSL
)
490 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
493 INTERNET_SetLastError(sock_get_error(errno
));
502 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
503 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
504 if (*sent
< 1 && len
)
513 /******************************************************************************
515 * Basically calls 'recv()' unless we should use SSL
516 * number of chars received is put in *recvd
518 BOOL
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
519 int *recvd
/* out */)
522 if (!NETCON_connected(connection
)) return FALSE
;
525 if (!connection
->useSSL
)
527 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
530 INTERNET_SetLastError(sock_get_error(errno
));
538 if (flags
& ~(MSG_PEEK
|MSG_WAITALL
))
539 FIXME("SSL_read does not support the following flag: %08x\n", flags
);
541 /* this ugly hack is all for MSG_PEEK. eww gross */
542 if (flags
& MSG_PEEK
&& !connection
->peek_msg
)
544 connection
->peek_msg
= connection
->peek_msg_mem
= HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len
) + 1);
546 else if (flags
& MSG_PEEK
&& connection
->peek_msg
)
548 if (len
< connection
->peek_len
)
549 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
550 *recvd
= min(len
, connection
->peek_len
);
551 memcpy(buf
, connection
->peek_msg
, *recvd
);
554 else if (connection
->peek_msg
)
556 *recvd
= min(len
, connection
->peek_len
);
557 memcpy(buf
, connection
->peek_msg
, *recvd
);
558 connection
->peek_len
-= *recvd
;
559 connection
->peek_msg
+= *recvd
;
560 if (connection
->peek_len
== 0)
562 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
563 connection
->peek_msg_mem
= NULL
;
564 connection
->peek_msg
= NULL
;
566 /* check if we got enough data from the peek buffer */
567 if (!(flags
& MSG_WAITALL
) || (*recvd
== len
))
569 /* otherwise, fall through */
571 *recvd
+= pSSL_read(connection
->ssl_s
, (char*)buf
+ *recvd
, len
- *recvd
);
572 if (flags
& MSG_PEEK
) /* must copy stuff into buffer */
574 connection
->peek_len
= *recvd
;
577 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
578 connection
->peek_msg_mem
= NULL
;
579 connection
->peek_msg
= NULL
;
582 memcpy(connection
->peek_msg
, buf
, *recvd
);
584 if (*recvd
< 1 && len
)
593 /******************************************************************************
594 * NETCON_query_data_available
595 * Returns the number of bytes of peeked data plus the number of bytes of
596 * queued, but unread data.
598 BOOL
NETCON_query_data_available(WININET_NETCONNECTION
*connection
, DWORD
*available
)
601 if (!NETCON_connected(connection
))
605 if (connection
->peek_msg
) *available
= connection
->peek_len
;
609 if (!connection
->useSSL
)
612 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
615 TRACE("%d bytes of queued, but unread data\n", unread
);
616 *available
+= unread
;
623 /******************************************************************************
626 BOOL
NETCON_getNextLine(WININET_NETCONNECTION
*connection
, LPSTR lpszBuffer
, LPDWORD dwBuffer
)
631 if (!NETCON_connected(connection
)) return FALSE
;
633 if (!connection
->useSSL
)
639 pfd
.fd
= connection
->socketFD
;
642 while (nRecv
< *dwBuffer
)
644 if (poll(&pfd
,1, RESPONSE_TIMEOUT
* 1000) > 0)
646 if ((ret
= recv(connection
->socketFD
, &lpszBuffer
[nRecv
], 1, 0)) <= 0)
648 if (ret
== -1) INTERNET_SetLastError(sock_get_error(errno
));
652 if (lpszBuffer
[nRecv
] == '\n')
654 lpszBuffer
[nRecv
++] = '\0';
656 TRACE(":%u %s\n", nRecv
, lpszBuffer
);
659 if (lpszBuffer
[nRecv
] != '\r')
664 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT
);
676 prev_timeout
= pSSL_CTX_get_timeout(ctx
);
677 pSSL_CTX_set_timeout(ctx
, RESPONSE_TIMEOUT
);
679 while (nRecv
< *dwBuffer
)
682 if (!NETCON_recv(connection
, &lpszBuffer
[nRecv
], 1, 0, &recv
))
684 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED
);
688 if (lpszBuffer
[nRecv
] == '\n')
693 if (lpszBuffer
[nRecv
] != '\r')
697 pSSL_CTX_set_timeout(ctx
, prev_timeout
);
700 lpszBuffer
[nRecv
++] = '\0';
702 TRACE("_SSL:%u %s\n", nRecv
, lpszBuffer
);
711 LPCVOID
NETCON_GetCert(WININET_NETCONNECTION
*connection
)
715 unsigned char* buffer
,*p
;
717 BOOL malloced
= FALSE
;
720 if (!connection
->useSSL
)
723 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
725 len
= pi2d_X509(cert
,&p
);
727 * SSL 0.9.7 and above malloc the buffer if it is null.
728 * however earlier version do not and so we would need to alloc the buffer.
730 * see the i2d_X509 man page for more details.
734 buffer
= HeapAlloc(GetProcessHeap(),0,len
);
736 len
= pi2d_X509(cert
,&p
);
744 r
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
749 HeapFree(GetProcessHeap(),0,buffer
);
757 DWORD
NETCON_set_timeout(WININET_NETCONNECTION
*connection
, BOOL send
, int value
)
762 /* FIXME: we should probably store the timeout in the connection to set
763 * when we do connect */
764 if (!NETCON_connected(connection
))
765 return ERROR_SUCCESS
;
767 /* value is in milliseconds, convert to struct timeval */
768 tv
.tv_sec
= value
/ 1000;
769 tv
.tv_usec
= (value
% 1000) * 1000;
771 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
772 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, &tv
,
777 WARN("setsockopt failed (%s)\n", strerror(errno
));
778 return sock_get_error(errno
);
781 return ERROR_SUCCESS
;