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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
42 #include "wine/library.h"
48 /* To avoid conflicts with the Unix socket headers. we only need it for
49 * the error codes anyway. */
53 #include "wine/debug.h"
56 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
59 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
62 * This should use winsock - To use winsock the functions will have to change a bit
63 * as they are designed for unix sockets.
64 * SSL stuff should use crypt32.dll
67 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
69 #include <openssl/err.h>
72 #define SONAME_LIBSSL "libssl.so"
74 #ifndef SONAME_LIBCRYPTO
75 #define SONAME_LIBCRYPTO "libcrypto.so"
78 static void *OpenSSL_ssl_handle
;
79 static void *OpenSSL_crypto_handle
;
81 static SSL_METHOD
*meth
;
84 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
86 /* OpenSSL functions that we use */
87 MAKE_FUNCPTR(SSL_library_init
);
88 MAKE_FUNCPTR(SSL_load_error_strings
);
89 MAKE_FUNCPTR(SSLv23_method
);
90 MAKE_FUNCPTR(SSL_CTX_new
);
91 MAKE_FUNCPTR(SSL_new
);
92 MAKE_FUNCPTR(SSL_free
);
93 MAKE_FUNCPTR(SSL_set_fd
);
94 MAKE_FUNCPTR(SSL_connect
);
95 MAKE_FUNCPTR(SSL_shutdown
);
96 MAKE_FUNCPTR(SSL_write
);
97 MAKE_FUNCPTR(SSL_read
);
98 MAKE_FUNCPTR(SSL_get_verify_result
);
99 MAKE_FUNCPTR(SSL_get_peer_certificate
);
100 MAKE_FUNCPTR(SSL_CTX_get_timeout
);
101 MAKE_FUNCPTR(SSL_CTX_set_timeout
);
102 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths
);
104 /* OpenSSL's libcrypto functions that we use */
105 MAKE_FUNCPTR(BIO_new_fp
);
106 MAKE_FUNCPTR(ERR_get_error
);
107 MAKE_FUNCPTR(ERR_error_string
);
112 void NETCON_init(WININET_NETCONNECTION
*connection
, BOOL useSSL
)
114 connection
->useSSL
= FALSE
;
115 connection
->socketFD
= -1;
118 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
119 TRACE("using SSL connection\n");
120 if (OpenSSL_ssl_handle
) /* already initilzed everything */
122 OpenSSL_ssl_handle
= wine_dlopen(SONAME_LIBSSL
, RTLD_NOW
, NULL
, 0);
123 if (!OpenSSL_ssl_handle
)
125 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
127 connection
->useSSL
= FALSE
;
130 OpenSSL_crypto_handle
= wine_dlopen(SONAME_LIBCRYPTO
, RTLD_NOW
, NULL
, 0);
131 if (!OpenSSL_crypto_handle
)
133 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
135 connection
->useSSL
= FALSE
;
139 /* mmm nice ugly macroness */
141 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
144 ERR("failed to load symbol %s\n", #x); \
145 connection->useSSL = FALSE; \
149 DYNSSL(SSL_library_init
);
150 DYNSSL(SSL_load_error_strings
);
151 DYNSSL(SSLv23_method
);
157 DYNSSL(SSL_shutdown
);
160 DYNSSL(SSL_get_verify_result
);
161 DYNSSL(SSL_get_peer_certificate
);
162 DYNSSL(SSL_CTX_get_timeout
);
163 DYNSSL(SSL_CTX_set_timeout
);
164 DYNSSL(SSL_CTX_set_default_verify_paths
);
167 #define DYNCRYPTO(x) \
168 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
171 ERR("failed to load symbol %s\n", #x); \
172 connection->useSSL = FALSE; \
175 DYNCRYPTO(BIO_new_fp
);
176 DYNCRYPTO(ERR_get_error
);
177 DYNCRYPTO(ERR_error_string
);
181 pSSL_load_error_strings();
182 pBIO_new_fp(stderr
, BIO_NOCLOSE
); /* FIXME: should use winedebug stuff */
184 meth
= pSSLv23_method();
185 connection
->peek_msg
= NULL
;
186 connection
->peek_msg_mem
= NULL
;
188 FIXME("can't use SSL, not compiled in.\n");
189 connection
->useSSL
= FALSE
;
194 BOOL
NETCON_connected(WININET_NETCONNECTION
*connection
)
196 if (connection
->socketFD
== -1)
202 /* translate a unix error code into a winsock one */
203 static int sock_get_error( int err
)
207 case EINTR
: return WSAEINTR
;
208 case EBADF
: return WSAEBADF
;
210 case EACCES
: return WSAEACCES
;
211 case EFAULT
: return WSAEFAULT
;
212 case EINVAL
: return WSAEINVAL
;
213 case EMFILE
: return WSAEMFILE
;
214 case EWOULDBLOCK
: return WSAEWOULDBLOCK
;
215 case EINPROGRESS
: return WSAEINPROGRESS
;
216 case EALREADY
: return WSAEALREADY
;
217 case ENOTSOCK
: return WSAENOTSOCK
;
218 case EDESTADDRREQ
: return WSAEDESTADDRREQ
;
219 case EMSGSIZE
: return WSAEMSGSIZE
;
220 case EPROTOTYPE
: return WSAEPROTOTYPE
;
221 case ENOPROTOOPT
: return WSAENOPROTOOPT
;
222 case EPROTONOSUPPORT
: return WSAEPROTONOSUPPORT
;
223 case ESOCKTNOSUPPORT
: return WSAESOCKTNOSUPPORT
;
224 case EOPNOTSUPP
: return WSAEOPNOTSUPP
;
225 case EPFNOSUPPORT
: return WSAEPFNOSUPPORT
;
226 case EAFNOSUPPORT
: return WSAEAFNOSUPPORT
;
227 case EADDRINUSE
: return WSAEADDRINUSE
;
228 case EADDRNOTAVAIL
: return WSAEADDRNOTAVAIL
;
229 case ENETDOWN
: return WSAENETDOWN
;
230 case ENETUNREACH
: return WSAENETUNREACH
;
231 case ENETRESET
: return WSAENETRESET
;
232 case ECONNABORTED
: return WSAECONNABORTED
;
234 case ECONNRESET
: return WSAECONNRESET
;
235 case ENOBUFS
: return WSAENOBUFS
;
236 case EISCONN
: return WSAEISCONN
;
237 case ENOTCONN
: return WSAENOTCONN
;
238 case ESHUTDOWN
: return WSAESHUTDOWN
;
239 case ETOOMANYREFS
: return WSAETOOMANYREFS
;
240 case ETIMEDOUT
: return WSAETIMEDOUT
;
241 case ECONNREFUSED
: return WSAECONNREFUSED
;
242 case ELOOP
: return WSAELOOP
;
243 case ENAMETOOLONG
: return WSAENAMETOOLONG
;
244 case EHOSTDOWN
: return WSAEHOSTDOWN
;
245 case EHOSTUNREACH
: return WSAEHOSTUNREACH
;
246 case ENOTEMPTY
: return WSAENOTEMPTY
;
248 case EPROCLIM
: return WSAEPROCLIM
;
251 case EUSERS
: return WSAEUSERS
;
254 case EDQUOT
: return WSAEDQUOT
;
257 case ESTALE
: return WSAESTALE
;
260 case EREMOTE
: return WSAEREMOTE
;
262 default: errno
=err
; perror("sock_set_error"); return WSAEFAULT
;
266 /******************************************************************************
268 * Basically calls 'socket()'
270 BOOL
NETCON_create(WININET_NETCONNECTION
*connection
, int domain
,
271 int type
, int protocol
)
273 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
274 if (connection
->useSSL
)
278 connection
->socketFD
= socket(domain
, type
, protocol
);
279 if (connection
->socketFD
== -1)
281 INTERNET_SetLastError(sock_get_error(errno
));
287 /******************************************************************************
289 * Basically calls 'close()' unless we should use SSL
291 BOOL
NETCON_close(WININET_NETCONNECTION
*connection
)
295 if (!NETCON_connected(connection
)) return FALSE
;
297 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
298 if (connection
->useSSL
)
300 HeapFree(GetProcessHeap(),0,connection
->peek_msg_mem
);
301 connection
->peek_msg
= NULL
;
302 connection
->peek_msg_mem
= NULL
;
304 pSSL_shutdown(connection
->ssl_s
);
305 pSSL_free(connection
->ssl_s
);
306 connection
->ssl_s
= NULL
;
308 connection
->useSSL
= FALSE
;
312 result
= closesocket(connection
->socketFD
);
313 connection
->socketFD
= -1;
317 INTERNET_SetLastError(sock_get_error(errno
));
322 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
323 static BOOL
check_hostname(X509
*cert
, char *hostname
)
325 /* FIXME: implement */
329 /******************************************************************************
330 * NETCON_secure_connect
331 * Initiates a secure connection over an existing plaintext connection.
333 BOOL
NETCON_secure_connect(WININET_NETCONNECTION
*connection
, LPCWSTR hostname
)
335 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
341 /* can't connect if we are already connected */
342 if (connection
->useSSL
)
344 ERR("already connected\n");
348 ctx
= pSSL_CTX_new(meth
);
349 if (!pSSL_CTX_set_default_verify_paths(ctx
))
351 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
352 pERR_error_string(pERR_get_error(), 0));
355 connection
->ssl_s
= pSSL_new(ctx
);
356 if (!connection
->ssl_s
)
358 ERR("SSL_new failed: %s\n",
359 pERR_error_string(pERR_get_error(), 0));
363 if (!pSSL_set_fd(connection
->ssl_s
, connection
->socketFD
))
365 ERR("SSL_set_fd failed: %s\n",
366 pERR_error_string(pERR_get_error(), 0));
370 if (pSSL_connect(connection
->ssl_s
) <= 0)
372 ERR("SSL_connect failed: %s\n",
373 pERR_error_string(pERR_get_error(), 0));
374 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR
);
377 cert
= pSSL_get_peer_certificate(connection
->ssl_s
);
380 ERR("no certificate for server %s\n", debugstr_w(hostname
));
381 /* FIXME: is this the best error? */
382 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA
);
385 verify_res
= pSSL_get_verify_result(connection
->ssl_s
);
386 if (verify_res
!= X509_V_OK
)
388 ERR("couldn't verify the security of the connection, %ld\n", verify_res
);
389 /* FIXME: we should set an error and return, but we only warn at
393 len
= WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, NULL
, 0, NULL
, NULL
);
394 hostname_unix
= HeapAlloc(GetProcessHeap(), 0, len
);
397 INTERNET_SetLastError(ERROR_NOT_ENOUGH_MEMORY
);
400 WideCharToMultiByte(CP_UNIXCP
, 0, hostname
, -1, hostname_unix
, len
, NULL
, NULL
);
402 if (!check_hostname(cert
, hostname_unix
))
404 HeapFree(GetProcessHeap(), 0, hostname_unix
);
405 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID
);
409 HeapFree(GetProcessHeap(), 0, hostname_unix
);
410 connection
->useSSL
= TRUE
;
414 if (connection
->ssl_s
)
416 pSSL_shutdown(connection
->ssl_s
);
417 pSSL_free(connection
->ssl_s
);
418 connection
->ssl_s
= NULL
;
424 /******************************************************************************
426 * Connects to the specified address.
428 BOOL
NETCON_connect(WININET_NETCONNECTION
*connection
, const struct sockaddr
*serv_addr
,
429 unsigned int addrlen
)
433 if (!NETCON_connected(connection
)) return FALSE
;
435 result
= connect(connection
->socketFD
, serv_addr
, addrlen
);
438 WARN("Unable to connect to host (%s)\n", strerror(errno
));
439 INTERNET_SetLastError(sock_get_error(errno
));
441 closesocket(connection
->socketFD
);
442 connection
->socketFD
= -1;
449 /******************************************************************************
451 * Basically calls 'send()' unless we should use SSL
452 * number of chars send is put in *sent
454 BOOL
NETCON_send(WININET_NETCONNECTION
*connection
, const void *msg
, size_t len
, int flags
,
457 if (!NETCON_connected(connection
)) return FALSE
;
458 if (!connection
->useSSL
)
460 *sent
= send(connection
->socketFD
, msg
, len
, flags
);
463 INTERNET_SetLastError(sock_get_error(errno
));
470 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
472 FIXME("SSL_write doesn't support any flags (%08x)\n", flags
);
473 *sent
= pSSL_write(connection
->ssl_s
, msg
, len
);
474 if (*sent
< 1 && len
)
483 /******************************************************************************
485 * Basically calls 'recv()' unless we should use SSL
486 * number of chars received is put in *recvd
488 BOOL
NETCON_recv(WININET_NETCONNECTION
*connection
, void *buf
, size_t len
, int flags
,
489 int *recvd
/* out */)
491 if (!NETCON_connected(connection
)) return FALSE
;
492 if (!connection
->useSSL
)
494 *recvd
= recv(connection
->socketFD
, buf
, len
, flags
);
497 INTERNET_SetLastError(sock_get_error(errno
));
504 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
505 if (flags
& (~MSG_PEEK
))
506 FIXME("SSL_read does not support the following flag: %08x\n", flags
);
508 /* this ugly hack is all for MSG_PEEK. eww gross */
509 if (flags
& MSG_PEEK
&& !connection
->peek_msg
)
511 connection
->peek_msg
= connection
->peek_msg_mem
= HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len
) + 1);
513 else if (flags
& MSG_PEEK
&& connection
->peek_msg
)
515 size_t peek_msg_len
= strlen(connection
->peek_msg
);
516 if (len
< peek_msg_len
)
517 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
518 memcpy(buf
, connection
->peek_msg
, min(len
,peek_msg_len
+1));
519 *recvd
= min(len
, peek_msg_len
);
522 else if (connection
->peek_msg
)
524 size_t peek_msg_len
= strlen(connection
->peek_msg
);
525 memcpy(buf
, connection
->peek_msg
, min(len
,peek_msg_len
+1));
526 connection
->peek_msg
+= *recvd
= min(len
, peek_msg_len
);
527 if (*connection
->peek_msg
== '\0' || *(connection
->peek_msg
- 1) == '\0')
529 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
530 connection
->peek_msg_mem
= NULL
;
531 connection
->peek_msg
= NULL
;
535 *recvd
= pSSL_read(connection
->ssl_s
, buf
, len
);
536 if (flags
& MSG_PEEK
) /* must copy stuff into buffer */
540 HeapFree(GetProcessHeap(), 0, connection
->peek_msg_mem
);
541 connection
->peek_msg_mem
= NULL
;
542 connection
->peek_msg
= NULL
;
546 memcpy(connection
->peek_msg
, buf
, *recvd
);
547 connection
->peek_msg
[*recvd
] = '\0';
550 if (*recvd
< 1 && len
)
559 /******************************************************************************
562 BOOL
NETCON_getNextLine(WININET_NETCONNECTION
*connection
, LPSTR lpszBuffer
, LPDWORD dwBuffer
)
567 if (!NETCON_connected(connection
)) return FALSE
;
569 if (!connection
->useSSL
)
573 BOOL bSuccess
= FALSE
;
577 FD_SET(connection
->socketFD
, &infd
);
578 tv
.tv_sec
=RESPONSE_TIMEOUT
;
581 while (nRecv
< *dwBuffer
)
583 if (select(connection
->socketFD
+1,&infd
,NULL
,NULL
,&tv
) > 0)
585 if (recv(connection
->socketFD
, &lpszBuffer
[nRecv
], 1, 0) <= 0)
587 INTERNET_SetLastError(sock_get_error(errno
));
591 if (lpszBuffer
[nRecv
] == '\n')
596 if (lpszBuffer
[nRecv
] != '\r')
601 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT
);
606 lend
: /* FIXME: don't use labels */
609 lpszBuffer
[nRecv
++] = '\0';
611 TRACE(":%lu %s\n", nRecv
, lpszBuffer
);
621 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
626 prev_timeout
= pSSL_CTX_get_timeout(ctx
);
627 pSSL_CTX_set_timeout(ctx
, RESPONSE_TIMEOUT
);
629 while (nRecv
< *dwBuffer
)
632 if (!NETCON_recv(connection
, &lpszBuffer
[nRecv
], 1, 0, &recv
))
634 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED
);
638 if (lpszBuffer
[nRecv
] == '\n')
643 if (lpszBuffer
[nRecv
] != '\r')
647 pSSL_CTX_set_timeout(ctx
, prev_timeout
);
650 lpszBuffer
[nRecv
++] = '\0';
652 TRACE("_SSL:%lu %s\n", nRecv
, lpszBuffer
);