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 */
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;
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)
209 /* translate a unix error code into a winsock one */
210 static int sock_get_error( int err
)
212 #if !defined(__MINGW32__) && !defined (_MSC_VER)
215 case EINTR
: return WSAEINTR
;
216 case EBADF
: return WSAEBADF
;
218 case EACCES
: return WSAEACCES
;
219 case EFAULT
: return WSAEFAULT
;
220 case EINVAL
: return WSAEINVAL
;
221 case EMFILE
: return WSAEMFILE
;
222 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
223 case EINPROGRESS
: return WSAEINPROGRESS
;
224 case EALREADY
: return WSAEALREADY
;
225 case ENOTSOCK
: return WSAENOTSOCK
;
226 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
227 case EMSGSIZE
: return WSAEMSGSIZE
;
228 case EPROTOTYPE
: return WSAEPROTOTYPE
;
229 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
230 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
231 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
232 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
233 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
234 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
235 case EADDRINUSE
: return WSAEADDRINUSE
;
236 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
237 case ENETDOWN
: return WSAENETDOWN
;
238 case ENETUNREACH
: return WSAENETUNREACH
;
239 case ENETRESET
: return WSAENETRESET
;
240 case ECONNABORTED
: return WSAECONNABORTED
;
242 case ECONNRESET
: return WSAECONNRESET
;
243 case ENOBUFS
: return WSAENOBUFS
;
244 case EISCONN
: return WSAEISCONN
;
245 case ENOTCONN
: return WSAENOTCONN
;
246 case ESHUTDOWN
: return WSAESHUTDOWN
;
247 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
248 case ETIMEDOUT
: return WSAETIMEDOUT
;
249 case ECONNREFUSED
: return WSAECONNREFUSED
;
250 case ELOOP
: return WSAELOOP
;
251 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
252 case EHOSTDOWN
: return WSAEHOSTDOWN
;
253 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
254 case ENOTEMPTY
: return WSAENOTEMPTY
;
256 case EPROCLIM
: return WSAEPROCLIM
;
259 case EUSERS
: return WSAEUSERS
;
262 case EDQUOT
: return WSAEDQUOT
;
265 case ESTALE
: return WSAESTALE
;
268 case EREMOTE
: return WSAEREMOTE
;
270 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
276 /******************************************************************************
278 * Basically calls 'socket()'
280 BOOL
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
281 int type
, int protocol
)
284 if (connection
->useSSL
)
288 connection
->socketFD
= socket(domain
, type
, protocol
);
289 if (connection
->socketFD
== -1)
291 INTERNET_SetLastError(sock_get_error(errno
));
297 /******************************************************************************
299 * Basically calls 'close()' unless we should use SSL
301 BOOL
NETCON_close(WININET_NETCONNECTION
*connection
)
305 if (!NETCON_connected(connection
)) return FALSE
;
308 if (connection
->useSSL
)
310 HeapFree(GetProcessHeap(),0,connection
->peek_msg_mem
);
311 connection
->peek_msg
= NULL
;
312 connection
->peek_msg_mem
= NULL
;
313 connection
->peek_len
= 0;
315 pSSL_shutdown(connection
->ssl_s
);
316 pSSL_free(connection
->ssl_s
);
317 connection
->ssl_s
= NULL
;
319 connection
->useSSL
= FALSE
;
323 result
= closesocket(connection
->socketFD
);
324 connection
->socketFD
= -1;
328 INTERNET_SetLastError(sock_get_error(errno
));
334 static BOOL
check_hostname(X509
*cert
, char *hostname
)
336 /* FIXME: implement */
340 /******************************************************************************
341 * NETCON_secure_connect
342 * Initiates a secure connection over an existing plaintext connection.
344 BOOL
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPCWSTR hostname
)
352 /* can't connect if we are already connected */
353 if (connection
->useSSL
)
355 ERR("already connected\n");
359 ctx
= pSSL_CTX_new(meth
);
360 if (!pSSL_CTX_set_default_verify_paths(ctx
))
362 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
363 pERR_error_string(pERR_get_error(), 0));
364 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
367 connection
->ssl_s
= pSSL_new(ctx
);
368 if (!connection
->ssl_s
)
370 ERR("SSL_new failed: %s\n",
371 pERR_error_string(pERR_get_error(), 0));
372 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
376 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
378 ERR("SSL_set_fd failed: %s\n",
379 pERR_error_string(pERR_get_error(), 0));
380 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
384 if (pSSL_connect(connection
->ssl_s
) <= 0)
386 ERR("SSL_connect failed: %s\n",
387 pERR_error_string(pERR_get_error(), 0));
388 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
391 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
394 ERR("no certificate for server %s\n", debugstr_w(hostname
));
395 /* FIXME: is this the best error? */
396 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA
);
399 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
400 if (verify_res
!= X509_V_OK
)
402 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
403 /* FIXME: we should set an error and return, but we only warn at
407 len
= WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, NULL
, 0, NULL
, NULL
);
408 hostname_unix
= HeapAlloc(GetProcessHeap(), 0, len
);
411 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
414 WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, hostname_unix
, len
, NULL
, NULL
);
416 if (!check_hostname(cert
, hostname_unix
))
418 HeapFree(GetProcessHeap(), 0, hostname_unix
);
419 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID
);
423 HeapFree(GetProcessHeap(), 0, hostname_unix
);
424 connection
->useSSL
= TRUE
;
428 if (connection
->ssl_s
)
430 pSSL_shutdown(connection
->ssl_s
);
431 pSSL_free(connection
->ssl_s
);
432 connection
->ssl_s
= NULL
;
438 /******************************************************************************
440 * Connects to the specified address.
442 BOOL
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
443 unsigned int addrlen
)
447 if (!NETCON_connected(connection
)) return FALSE
;
449 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
452 WARN("Unable to connect to host (%s)\n", strerror(errno
));
453 INTERNET_SetLastError(sock_get_error(errno
));
455 closesocket(connection
->socketFD
);
456 connection
->socketFD
= -1;
463 /******************************************************************************
465 * Basically calls 'send()' unless we should use SSL
466 * number of chars send is put in *sent
468 BOOL
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
471 if (!NETCON_connected(connection
)) return FALSE
;
472 if (!connection
->useSSL
)
474 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
477 INTERNET_SetLastError(sock_get_error(errno
));
486 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
487 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
488 if (*sent
< 1 && len
)
497 /******************************************************************************
499 * Basically calls 'recv()' unless we should use SSL
500 * number of chars received is put in *recvd
502 BOOL
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
503 int *recvd
/* out */)
506 if (!NETCON_connected(connection
)) return FALSE
;
509 if (!connection
->useSSL
)
511 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
514 INTERNET_SetLastError(sock_get_error(errno
));
522 if (flags
& ~(MSG_PEEK
|MSG_WAITALL
))
523 FIXME("SSL_read does not support the following flag: %08x\n", flags
);
525 /* this ugly hack is all for MSG_PEEK. eww gross */
526 if (flags
& MSG_PEEK
&& !connection
->peek_msg
)
528 connection
->peek_msg
= connection
->peek_msg_mem
= HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len
) + 1);
530 else if (flags
& MSG_PEEK
&& connection
->peek_msg
)
532 if (len
< connection
->peek_len
)
533 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
534 *recvd
= min(len
, connection
->peek_len
);
535 memcpy(buf
, connection
->peek_msg
, *recvd
);
538 else if (connection
->peek_msg
)
540 *recvd
= min(len
, connection
->peek_len
);
541 memcpy(buf
, connection
->peek_msg
, *recvd
);
542 connection
->peek_len
-= *recvd
;
543 connection
->peek_msg
+= *recvd
;
544 if (connection
->peek_len
== 0)
546 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
547 connection
->peek_msg_mem
= NULL
;
548 connection
->peek_msg
= NULL
;
550 /* check if we got enough data from the peek buffer */
551 if (!(flags
& MSG_WAITALL
) || (*recvd
== len
))
553 /* otherwise, fall through */
555 *recvd
+= pSSL_read(connection
->ssl_s
, (char*)buf
+ *recvd
, len
- *recvd
);
556 if (flags
& MSG_PEEK
) /* must copy stuff into buffer */
558 connection
->peek_len
= *recvd
;
561 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
562 connection
->peek_msg_mem
= NULL
;
563 connection
->peek_msg
= NULL
;
566 memcpy(connection
->peek_msg
, buf
, *recvd
);
568 if (*recvd
< 1 && len
)
577 /******************************************************************************
578 * NETCON_query_data_available
579 * Returns the number of bytes of peeked data plus the number of bytes of
580 * queued, but unread data.
582 BOOL
NETCON_query_data_available(WININET_NETCONNECTION
*connection
, DWORD
*available
)
585 if (!NETCON_connected(connection
))
589 if (connection
->peek_msg
) *available
= connection
->peek_len
;
593 if (!connection
->useSSL
)
596 int retval
= ioctlsocket(connection
->socketFD
, FIONREAD
, &unread
);
599 TRACE("%d bytes of queued, but unread data\n", unread
);
600 *available
+= unread
;
607 /******************************************************************************
610 BOOL
NETCON_getNextLine(WININET_NETCONNECTION
*connection
, LPSTR lpszBuffer
, LPDWORD dwBuffer
)
615 if (!NETCON_connected(connection
)) return FALSE
;
617 if (!connection
->useSSL
)
620 BOOL bSuccess
= FALSE
;
623 pfd
.fd
= connection
->socketFD
;
626 while (nRecv
< *dwBuffer
)
628 if (poll(&pfd
,1, RESPONSE_TIMEOUT
* 1000) > 0)
630 if (recv(connection
->socketFD
, &lpszBuffer
[nRecv
], 1, 0) <= 0)
632 INTERNET_SetLastError(sock_get_error(errno
));
636 if (lpszBuffer
[nRecv
] == '\n')
641 if (lpszBuffer
[nRecv
] != '\r')
646 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT
);
651 lend
: /* FIXME: don't use labels */
654 lpszBuffer
[nRecv
++] = '\0';
656 TRACE(":%u %s\n", nRecv
, lpszBuffer
);
671 prev_timeout
= pSSL_CTX_get_timeout(ctx
);
672 pSSL_CTX_set_timeout(ctx
, RESPONSE_TIMEOUT
);
674 while (nRecv
< *dwBuffer
)
677 if (!NETCON_recv(connection
, &lpszBuffer
[nRecv
], 1, 0, &recv
))
679 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED
);
683 if (lpszBuffer
[nRecv
] == '\n')
688 if (lpszBuffer
[nRecv
] != '\r')
692 pSSL_CTX_set_timeout(ctx
, prev_timeout
);
695 lpszBuffer
[nRecv
++] = '\0';
697 TRACE("_SSL:%u %s\n", nRecv
, lpszBuffer
);
708 LPCVOID
NETCON_GetCert(WININET_NETCONNECTION
*connection
)
712 unsigned char* buffer
,*p
;
714 BOOL malloced
= FALSE
;
717 if (!connection
->useSSL
)
720 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
722 len
= pi2d_X509(cert
,&p
);
724 * SSL 0.9.7 and above malloc the buffer if it is null.
725 * however earlier version do not and so we would need to alloc the buffer.
727 * see the i2d_X509 man page for more details.
731 buffer
= HeapAlloc(GetProcessHeap(),0,len
);
733 len
= pi2d_X509(cert
,&p
);
741 r
= CertCreateCertificateContext(X509_ASN_ENCODING
,buffer
,len
);
746 HeapFree(GetProcessHeap(),0,buffer
);
754 DWORD
NETCON_set_timeout(WININET_NETCONNECTION
*connection
, BOOL send
, int value
)
759 /* FIXME: we should probably store the timeout in the connection to set
760 * when we do connect */
761 if (!NETCON_connected(connection
))
762 return ERROR_SUCCESS
;
764 /* value is in milliseconds, convert to struct timeval */
765 tv
.tv_sec
= value
/ 1000;
766 tv
.tv_usec
= (value
% 1000) * 1000;
768 result
= setsockopt(connection
->socketFD
, SOL_SOCKET
,
769 send
? SO_SNDTIMEO
: SO_RCVTIMEO
, &tv
,
774 WARN("setsockopt failed (%s)\n", strerror(errno
));
775 return sock_get_error(errno
);
778 return ERROR_SUCCESS
;