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"
29 #ifdef HAVE_SYS_POLL_H
30 # include <sys/poll.h>
32 #ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 # include <sys/socket.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
51 #include "wine/library.h"
58 #include "wine/debug.h"
61 /* To avoid conflicts with the Unix socket headers. we only need it for
62 * the error codes anyway. */
66 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
67 #define sock_get_error(x) WSAGetLastError()
69 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
72 * This should use winsock - To use winsock the functions will have to change a bit
73 * as they are designed for unix sockets.
74 * SSL stuff should use crypt32.dll
79 #include <openssl/err.h>
81 static void *OpenSSL_ssl_handle
;
82 static void *OpenSSL_crypto_handle
;
84 static SSL_METHOD
*meth
;
87 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
89 /* OpenSSL functions that we use */
90 MAKE_FUNCPTR(SSL_library_init
);
91 MAKE_FUNCPTR(SSL_load_error_strings
);
92 MAKE_FUNCPTR(SSLv23_method
);
93 MAKE_FUNCPTR(SSL_CTX_new
);
94 MAKE_FUNCPTR(SSL_new
);
95 MAKE_FUNCPTR(SSL_free
);
96 MAKE_FUNCPTR(SSL_set_fd
);
97 MAKE_FUNCPTR(SSL_connect
);
98 MAKE_FUNCPTR(SSL_shutdown
);
99 MAKE_FUNCPTR(SSL_write
);
100 MAKE_FUNCPTR(SSL_read
);
101 MAKE_FUNCPTR(SSL_get_verify_result
);
102 MAKE_FUNCPTR(SSL_get_peer_certificate
);
103 MAKE_FUNCPTR(SSL_CTX_get_timeout
);
104 MAKE_FUNCPTR(SSL_CTX_set_timeout
);
105 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths
);
106 MAKE_FUNCPTR(i2d_X509
);
108 /* OpenSSL's libcrypto functions that we use */
109 MAKE_FUNCPTR(BIO_new_fp
);
110 MAKE_FUNCPTR(ERR_get_error
);
111 MAKE_FUNCPTR(ERR_error_string
);
116 BOOL
NETCON_init(WININET_NETCONNECTION
*connection
, BOOL useSSL
)
118 connection
->useSSL
= FALSE
;
119 connection
->socketFD
= -1;
122 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
123 TRACE("using SSL connection\n");
124 if (OpenSSL_ssl_handle
) /* already initialized everything */
126 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
127 if (!OpenSSL_ssl_handle
)
129 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
131 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
134 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
135 if (!OpenSSL_crypto_handle
)
137 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
139 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
143 /* mmm nice ugly macroness */
145 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
148 ERR("failed to load symbol %s\n", #x); \
149 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
153 DYNSSL(SSL_library_init
);
154 DYNSSL(SSL_load_error_strings
);
155 DYNSSL(SSLv23_method
);
161 DYNSSL(SSL_shutdown
);
164 DYNSSL(SSL_get_verify_result
);
165 DYNSSL(SSL_get_peer_certificate
);
166 DYNSSL(SSL_CTX_get_timeout
);
167 DYNSSL(SSL_CTX_set_timeout
);
168 DYNSSL(SSL_CTX_set_default_verify_paths
);
172 #define DYNCRYPTO(x) \
173 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
176 ERR("failed to load symbol %s\n", #x); \
177 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
180 DYNCRYPTO(BIO_new_fp
);
181 DYNCRYPTO(ERR_get_error
);
182 DYNCRYPTO(ERR_error_string
);
186 pSSL_load_error_strings();
187 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
189 meth
= pSSLv23_method();
190 connection
->peek_msg
= NULL
;
191 connection
->peek_msg_mem
= NULL
;
193 FIXME("can't use SSL, not compiled in.\n");
194 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
201 BOOL
NETCON_connected(WININET_NETCONNECTION
*connection
)
203 if (connection
->socketFD
== -1)
210 /* translate a unix error code into a winsock one */
211 static int sock_get_error( int err
)
213 #if !defined(__MINGW32__) && !defined (_MSC_VER)
216 case EINTR
: return WSAEINTR
;
217 case EBADF
: return WSAEBADF
;
219 case EACCES
: return WSAEACCES
;
220 case EFAULT
: return WSAEFAULT
;
221 case EINVAL
: return WSAEINVAL
;
222 case EMFILE
: return WSAEMFILE
;
223 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
224 case EINPROGRESS
: return WSAEINPROGRESS
;
225 case EALREADY
: return WSAEALREADY
;
226 case ENOTSOCK
: return WSAENOTSOCK
;
227 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
228 case EMSGSIZE
: return WSAEMSGSIZE
;
229 case EPROTOTYPE
: return WSAEPROTOTYPE
;
230 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
231 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
232 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
233 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
234 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
235 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
236 case EADDRINUSE
: return WSAEADDRINUSE
;
237 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
238 case ENETDOWN
: return WSAENETDOWN
;
239 case ENETUNREACH
: return WSAENETUNREACH
;
240 case ENETRESET
: return WSAENETRESET
;
241 case ECONNABORTED
: return WSAECONNABORTED
;
243 case ECONNRESET
: return WSAECONNRESET
;
244 case ENOBUFS
: return WSAENOBUFS
;
245 case EISCONN
: return WSAEISCONN
;
246 case ENOTCONN
: return WSAENOTCONN
;
247 case ESHUTDOWN
: return WSAESHUTDOWN
;
248 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
249 case ETIMEDOUT
: return WSAETIMEDOUT
;
250 case ECONNREFUSED
: return WSAECONNREFUSED
;
251 case ELOOP
: return WSAELOOP
;
252 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
253 case EHOSTDOWN
: return WSAEHOSTDOWN
;
254 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
255 case ENOTEMPTY
: return WSAENOTEMPTY
;
257 case EPROCLIM
: return WSAEPROCLIM
;
260 case EUSERS
: return WSAEUSERS
;
263 case EDQUOT
: return WSAEDQUOT
;
266 case ESTALE
: return WSAESTALE
;
269 case EREMOTE
: return WSAEREMOTE
;
271 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
278 /******************************************************************************
280 * Basically calls 'socket()'
282 BOOL
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
283 int type
, int protocol
)
286 if (connection
->useSSL
)
290 connection
->socketFD
= socket(domain
, type
, protocol
);
291 if (connection
->socketFD
== -1)
293 INTERNET_SetLastError(sock_get_error(errno
));
299 /******************************************************************************
301 * Basically calls 'close()' unless we should use SSL
303 BOOL
NETCON_close(WININET_NETCONNECTION
*connection
)
307 if (!NETCON_connected(connection
)) return FALSE
;
310 if (connection
->useSSL
)
312 HeapFree(GetProcessHeap(),0,connection
->peek_msg_mem
);
313 connection
->peek_msg
= NULL
;
314 connection
->peek_msg_mem
= NULL
;
315 connection
->peek_len
= 0;
317 pSSL_shutdown(connection
->ssl_s
);
318 pSSL_free(connection
->ssl_s
);
319 connection
->ssl_s
= NULL
;
321 connection
->useSSL
= FALSE
;
325 result
= closesocket(connection
->socketFD
);
326 connection
->socketFD
= -1;
330 INTERNET_SetLastError(sock_get_error(errno
));
336 static BOOL
check_hostname(X509
*cert
, char *hostname
)
338 /* FIXME: implement */
342 /******************************************************************************
343 * NETCON_secure_connect
344 * Initiates a secure connection over an existing plaintext connection.
346 BOOL
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPCWSTR hostname
)
354 /* can't connect if we are already connected */
355 if (connection
->useSSL
)
357 ERR("already connected\n");
361 ctx
= pSSL_CTX_new(meth
);
362 if (!pSSL_CTX_set_default_verify_paths(ctx
))
364 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
365 pERR_error_string(pERR_get_error(), 0));
366 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
369 connection
->ssl_s
= pSSL_new(ctx
);
370 if (!connection
->ssl_s
)
372 ERR("SSL_new failed: %s\n",
373 pERR_error_string(pERR_get_error(), 0));
374 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
378 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
380 ERR("SSL_set_fd failed: %s\n",
381 pERR_error_string(pERR_get_error(), 0));
382 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
386 if (pSSL_connect(connection
->ssl_s
) <= 0)
388 ERR("SSL_connect failed: %s\n",
389 pERR_error_string(pERR_get_error(), 0));
390 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
393 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
396 ERR("no certificate for server %s\n", debugstr_w(hostname
));
397 /* FIXME: is this the best error? */
398 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA
);
401 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
402 if (verify_res
!= X509_V_OK
)
404 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
405 /* FIXME: we should set an error and return, but we only warn at
409 len
= WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, NULL
, 0, NULL
, NULL
);
410 hostname_unix
= HeapAlloc(GetProcessHeap(), 0, len
);
413 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
416 WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, hostname_unix
, len
, NULL
, NULL
);
418 if (!check_hostname(cert
, hostname_unix
))
420 HeapFree(GetProcessHeap(), 0, hostname_unix
);
421 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID
);
425 HeapFree(GetProcessHeap(), 0, hostname_unix
);
426 connection
->useSSL
= TRUE
;
430 if (connection
->ssl_s
)
432 pSSL_shutdown(connection
->ssl_s
);
433 pSSL_free(connection
->ssl_s
);
434 connection
->ssl_s
= NULL
;
440 /******************************************************************************
442 * Connects to the specified address.
444 BOOL
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
445 unsigned int addrlen
)
449 if (!NETCON_connected(connection
)) return FALSE
;
451 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
454 WARN("Unable to connect to host (%s)\n", strerror(errno
));
455 INTERNET_SetLastError(sock_get_error(errno
));
457 closesocket(connection
->socketFD
);
458 connection
->socketFD
= -1;
465 /******************************************************************************
467 * Basically calls 'send()' unless we should use SSL
468 * number of chars send is put in *sent
470 BOOL
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
473 if (!NETCON_connected(connection
)) return FALSE
;
474 if (!connection
->useSSL
)
476 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
479 INTERNET_SetLastError(sock_get_error(errno
));
488 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
489 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
490 if (*sent
< 1 && len
)
499 /******************************************************************************
501 * Basically calls 'recv()' unless we should use SSL
502 * number of chars received is put in *recvd
504 BOOL
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
505 int *recvd
/* out */)
508 if (!NETCON_connected(connection
)) return FALSE
;
511 if (!connection
->useSSL
)
513 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
516 INTERNET_SetLastError(sock_get_error(errno
));
524 if (flags
& ~(MSG_PEEK
|MSG_WAITALL
))
525 FIXME("SSL_read does not support the following flag: %08x\n", flags
);
527 /* this ugly hack is all for MSG_PEEK. eww gross */
528 if (flags
& MSG_PEEK
&& !connection
->peek_msg
)
530 connection
->peek_msg
= connection
->peek_msg_mem
= HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len
) + 1);
532 else if (flags
& MSG_PEEK
&& connection
->peek_msg
)
534 if (len
< connection
->peek_len
)
535 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
536 *recvd
= min(len
, connection
->peek_len
);
537 memcpy(buf
, connection
->peek_msg
, *recvd
);
540 else if (connection
->peek_msg
)
542 *recvd
= min(len
, connection
->peek_len
);
543 memcpy(buf
, connection
->peek_msg
, *recvd
);
544 connection
->peek_len
-= *recvd
;
545 connection
->peek_msg
+= *recvd
;
546 if (connection
->peek_len
== 0)
548 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
549 connection
->peek_msg_mem
= NULL
;
550 connection
->peek_msg
= NULL
;
552 /* check if we got enough data from the peek buffer */
553 if (!(flags
& MSG_WAITALL
) || (*recvd
== len
))
555 /* otherwise, fall through */
557 *recvd
+= pSSL_read(connection
->ssl_s
, (char*)buf
+ *recvd
, len
- *recvd
);
558 if (flags
& MSG_PEEK
) /* must copy stuff into buffer */
560 connection
->peek_len
= *recvd
;
563 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
564 connection
->peek_msg_mem
= NULL
;
565 connection
->peek_msg
= NULL
;
568 memcpy(connection
->peek_msg
, buf
, *recvd
);
570 if (*recvd
< 1 && len
)
579 /******************************************************************************
580 * NETCON_query_data_available
581 * Returns the number of bytes of peeked data plus the number of bytes of
582 * queued, but unread data.
584 BOOL
NETCON_query_data_available(WININET_NETCONNECTION
*connection
, DWORD
*available
)
587 if (!NETCON_connected(connection
))
591 if (connection
->peek_msg
) *available
= connection
->peek_len
;
595 if (!connection
->useSSL
)
598 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
601 TRACE("%d bytes of queued, but unread data\n", unread
);
602 *available
+= unread
;
609 /******************************************************************************
612 BOOL
NETCON_getNextLine(WININET_NETCONNECTION
*connection
, LPSTR lpszBuffer
, LPDWORD dwBuffer
)
617 if (!NETCON_connected(connection
)) return FALSE
;
619 if (!connection
->useSSL
)
623 BOOL bSuccess
= FALSE
;
627 FD_SET(connection
->socketFD
, &infd
);
628 tv
.tv_sec
=RESPONSE_TIMEOUT
;
631 while (nRecv
< *dwBuffer
)
633 if (select(connection
->socketFD
+1,&infd
,NULL
,NULL
,&tv
) > 0)
635 if (recv(connection
->socketFD
, &lpszBuffer
[nRecv
], 1, 0) <= 0)
637 INTERNET_SetLastError(sock_get_error(errno
));
641 if (lpszBuffer
[nRecv
] == '\n')
646 if (lpszBuffer
[nRecv
] != '\r')
651 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT
);
656 lend
: /* FIXME: don't use labels */
659 lpszBuffer
[nRecv
++] = '\0';
661 TRACE(":%u %s\n", nRecv
, lpszBuffer
);
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
);
713 LPCVOID
NETCON_GetCert(WININET_NETCONNECTION
*connection
)
717 unsigned char* buffer
,*p
;
719 BOOL malloced
= FALSE
;
722 if (!connection
->useSSL
)
725 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
727 len
= pi2d_X509(cert
,&p
);
729 * SSL 0.9.7 and above malloc the buffer if it is null.
730 * however earlier version do not and so we would need to alloc the buffer.
732 * see the i2d_X509 man page for more details.
736 buffer
= HeapAlloc(GetProcessHeap(),0,len
);
738 len
= pi2d_X509(cert
,&p
);
746 r
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
751 HeapFree(GetProcessHeap(),0,buffer
);
759 DWORD
NETCON_set_timeout(WININET_NETCONNECTION
*connection
, BOOL send
, int value
)
764 /* FIXME: we should probably store the timeout in the connection to set
765 * when we do connect */
766 if (!NETCON_connected(connection
))
767 return ERROR_SUCCESS
;
769 /* value is in milliseconds, convert to struct timeval */
770 tv
.tv_sec
= value
/ 1000;
771 tv
.tv_usec
= (value
% 1000) * 1000;
773 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
774 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, &tv
,
779 WARN("setsockopt failed (%s)\n", strerror(errno
));
780 return sock_get_error(errno
);
783 return ERROR_SUCCESS
;