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"
57 #define RESPONSE_TIMEOUT 30 /* FROM internet.c */
60 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
63 * This should use winsock - To use winsock the functions will have to change a bit
64 * as they are designed for unix sockets.
65 * SSL stuff should use crypt32.dll
68 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
70 #include <openssl/err.h>
73 #define SONAME_LIBSSL "libssl.so"
75 #ifndef SONAME_LIBCRYPTO
76 #define SONAME_LIBCRYPTO "libcrypto.so"
79 static void *OpenSSL_ssl_handle;
80 static void *OpenSSL_crypto_handle;
82 static SSL_METHOD *meth;
85 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
87 /* OpenSSL functions that we use */
88 MAKE_FUNCPTR(SSL_library_init);
89 MAKE_FUNCPTR(SSL_load_error_strings);
90 MAKE_FUNCPTR(SSLv23_method);
91 MAKE_FUNCPTR(SSL_CTX_new);
92 MAKE_FUNCPTR(SSL_new);
93 MAKE_FUNCPTR(SSL_free);
94 MAKE_FUNCPTR(SSL_set_fd);
95 MAKE_FUNCPTR(SSL_connect);
96 MAKE_FUNCPTR(SSL_shutdown);
97 MAKE_FUNCPTR(SSL_write);
98 MAKE_FUNCPTR(SSL_read);
99 MAKE_FUNCPTR(SSL_get_verify_result);
100 MAKE_FUNCPTR(SSL_get_peer_certificate);
101 MAKE_FUNCPTR(SSL_CTX_get_timeout);
102 MAKE_FUNCPTR(SSL_CTX_set_timeout);
103 MAKE_FUNCPTR(SSL_CTX_set_default_verify_paths);
104 MAKE_FUNCPTR(i2d_X509);
106 /* OpenSSL's libcrypto functions that we use */
107 MAKE_FUNCPTR(BIO_new_fp);
108 MAKE_FUNCPTR(ERR_get_error);
109 MAKE_FUNCPTR(ERR_error_string);
114 BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
116 connection->useSSL = FALSE;
117 connection->socketFD = -1;
120 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
121 TRACE("using SSL connection\n");
122 if (OpenSSL_ssl_handle) /* already initialized everything */
124 OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
125 if (!OpenSSL_ssl_handle)
127 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
129 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
132 OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
133 if (!OpenSSL_crypto_handle)
135 ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
137 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
141 /* mmm nice ugly macroness */
143 p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
146 ERR("failed to load symbol %s\n", #x); \
147 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
151 DYNSSL(SSL_library_init);
152 DYNSSL(SSL_load_error_strings);
153 DYNSSL(SSLv23_method);
159 DYNSSL(SSL_shutdown);
162 DYNSSL(SSL_get_verify_result);
163 DYNSSL(SSL_get_peer_certificate);
164 DYNSSL(SSL_CTX_get_timeout);
165 DYNSSL(SSL_CTX_set_timeout);
166 DYNSSL(SSL_CTX_set_default_verify_paths);
170 #define DYNCRYPTO(x) \
171 p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
174 ERR("failed to load symbol %s\n", #x); \
175 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
178 DYNCRYPTO(BIO_new_fp);
179 DYNCRYPTO(ERR_get_error);
180 DYNCRYPTO(ERR_error_string);
184 pSSL_load_error_strings();
185 pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
187 meth = pSSLv23_method();
188 connection->peek_msg = NULL;
189 connection->peek_msg_mem = NULL;
191 FIXME("can't use SSL, not compiled in.\n");
192 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
199 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
201 if (connection->socketFD == -1)
207 /* translate a unix error code into a winsock one */
208 static int sock_get_error( int err )
212 case EINTR: return WSAEINTR;
213 case EBADF: return WSAEBADF;
215 case EACCES: return WSAEACCES;
216 case EFAULT: return WSAEFAULT;
217 case EINVAL: return WSAEINVAL;
218 case EMFILE: return WSAEMFILE;
219 case EWOULDBLOCK: return WSAEWOULDBLOCK;
220 case EINPROGRESS: return WSAEINPROGRESS;
221 case EALREADY: return WSAEALREADY;
222 case ENOTSOCK: return WSAENOTSOCK;
223 case EDESTADDRREQ: return WSAEDESTADDRREQ;
224 case EMSGSIZE: return WSAEMSGSIZE;
225 case EPROTOTYPE: return WSAEPROTOTYPE;
226 case ENOPROTOOPT: return WSAENOPROTOOPT;
227 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
228 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
229 case EOPNOTSUPP: return WSAEOPNOTSUPP;
230 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
231 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
232 case EADDRINUSE: return WSAEADDRINUSE;
233 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
234 case ENETDOWN: return WSAENETDOWN;
235 case ENETUNREACH: return WSAENETUNREACH;
236 case ENETRESET: return WSAENETRESET;
237 case ECONNABORTED: return WSAECONNABORTED;
239 case ECONNRESET: return WSAECONNRESET;
240 case ENOBUFS: return WSAENOBUFS;
241 case EISCONN: return WSAEISCONN;
242 case ENOTCONN: return WSAENOTCONN;
243 case ESHUTDOWN: return WSAESHUTDOWN;
244 case ETOOMANYREFS: return WSAETOOMANYREFS;
245 case ETIMEDOUT: return WSAETIMEDOUT;
246 case ECONNREFUSED: return WSAECONNREFUSED;
247 case ELOOP: return WSAELOOP;
248 case ENAMETOOLONG: return WSAENAMETOOLONG;
249 case EHOSTDOWN: return WSAEHOSTDOWN;
250 case EHOSTUNREACH: return WSAEHOSTUNREACH;
251 case ENOTEMPTY: return WSAENOTEMPTY;
253 case EPROCLIM: return WSAEPROCLIM;
256 case EUSERS: return WSAEUSERS;
259 case EDQUOT: return WSAEDQUOT;
262 case ESTALE: return WSAESTALE;
265 case EREMOTE: return WSAEREMOTE;
267 default: errno=err; perror("sock_set_error"); return WSAEFAULT;
271 /******************************************************************************
273 * Basically calls 'socket()'
275 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
276 int type, int protocol)
278 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
279 if (connection->useSSL)
283 connection->socketFD = socket(domain, type, protocol);
284 if (connection->socketFD == -1)
286 INTERNET_SetLastError(sock_get_error(errno));
292 /******************************************************************************
294 * Basically calls 'close()' unless we should use SSL
296 BOOL NETCON_close(WININET_NETCONNECTION *connection)
300 if (!NETCON_connected(connection)) return FALSE;
302 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
303 if (connection->useSSL)
305 HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
306 connection->peek_msg = NULL;
307 connection->peek_msg_mem = NULL;
309 pSSL_shutdown(connection->ssl_s);
310 pSSL_free(connection->ssl_s);
311 connection->ssl_s = NULL;
313 connection->useSSL = FALSE;
317 result = closesocket(connection->socketFD);
318 connection->socketFD = -1;
322 INTERNET_SetLastError(sock_get_error(errno));
327 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
328 static BOOL check_hostname(X509 *cert, char *hostname)
330 /* FIXME: implement */
334 /******************************************************************************
335 * NETCON_secure_connect
336 * Initiates a secure connection over an existing plaintext connection.
338 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
340 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
346 /* can't connect if we are already connected */
347 if (connection->useSSL)
349 ERR("already connected\n");
353 ctx = pSSL_CTX_new(meth);
354 if (!pSSL_CTX_set_default_verify_paths(ctx))
356 ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
357 pERR_error_string(pERR_get_error(), 0));
358 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
361 connection->ssl_s = pSSL_new(ctx);
362 if (!connection->ssl_s)
364 ERR("SSL_new failed: %s\n",
365 pERR_error_string(pERR_get_error(), 0));
366 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
370 if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
372 ERR("SSL_set_fd failed: %s\n",
373 pERR_error_string(pERR_get_error(), 0));
374 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
378 if (pSSL_connect(connection->ssl_s) <= 0)
380 ERR("SSL_connect failed: %s\n",
381 pERR_error_string(pERR_get_error(), 0));
382 INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
385 cert = pSSL_get_peer_certificate(connection->ssl_s);
388 ERR("no certificate for server %s\n", debugstr_w(hostname));
389 /* FIXME: is this the best error? */
390 INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
393 verify_res = pSSL_get_verify_result(connection->ssl_s);
394 if (verify_res != X509_V_OK)
396 ERR("couldn't verify the security of the connection, %ld\n", verify_res);
397 /* FIXME: we should set an error and return, but we only warn at
401 len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
402 hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
405 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
408 WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
410 if (!check_hostname(cert, hostname_unix))
412 HeapFree(GetProcessHeap(), 0, hostname_unix);
413 INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
417 HeapFree(GetProcessHeap(), 0, hostname_unix);
418 connection->useSSL = TRUE;
422 if (connection->ssl_s)
424 pSSL_shutdown(connection->ssl_s);
425 pSSL_free(connection->ssl_s);
426 connection->ssl_s = NULL;
432 /******************************************************************************
434 * Connects to the specified address.
436 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
437 unsigned int addrlen)
441 if (!NETCON_connected(connection)) return FALSE;
443 result = connect(connection->socketFD, serv_addr, addrlen);
446 WARN("Unable to connect to host (%s)\n", strerror(errno));
447 INTERNET_SetLastError(sock_get_error(errno));
449 closesocket(connection->socketFD);
450 connection->socketFD = -1;
457 /******************************************************************************
459 * Basically calls 'send()' unless we should use SSL
460 * number of chars send is put in *sent
462 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
465 if (!NETCON_connected(connection)) return FALSE;
466 if (!connection->useSSL)
468 *sent = send(connection->socketFD, msg, len, flags);
471 INTERNET_SetLastError(sock_get_error(errno));
478 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
480 FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
481 *sent = pSSL_write(connection->ssl_s, msg, len);
482 if (*sent < 1 && len)
491 /******************************************************************************
493 * Basically calls 'recv()' unless we should use SSL
494 * number of chars received is put in *recvd
496 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
497 int *recvd /* out */)
499 if (!NETCON_connected(connection)) return FALSE;
500 if (!connection->useSSL)
502 *recvd = recv(connection->socketFD, buf, len, flags);
505 INTERNET_SetLastError(sock_get_error(errno));
512 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
513 if (flags & (~MSG_PEEK))
514 FIXME("SSL_read does not support the following flag: %08x\n", flags);
516 /* this ugly hack is all for MSG_PEEK. eww gross */
517 if (flags & MSG_PEEK && !connection->peek_msg)
519 connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
521 else if (flags & MSG_PEEK && connection->peek_msg)
523 size_t peek_msg_len = strlen(connection->peek_msg);
524 if (len < peek_msg_len)
525 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
526 memcpy(buf, connection->peek_msg, min(len,peek_msg_len+1));
527 *recvd = min(len, peek_msg_len);
530 else if (connection->peek_msg)
532 size_t peek_msg_len = strlen(connection->peek_msg);
533 memcpy(buf, connection->peek_msg, min(len,peek_msg_len+1));
534 connection->peek_msg += *recvd = min(len, peek_msg_len);
535 if (*connection->peek_msg == '\0' || *(connection->peek_msg - 1) == '\0')
537 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
538 connection->peek_msg_mem = NULL;
539 connection->peek_msg = NULL;
543 *recvd = pSSL_read(connection->ssl_s, buf, len);
544 if (flags & MSG_PEEK) /* must copy stuff into buffer */
548 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
549 connection->peek_msg_mem = NULL;
550 connection->peek_msg = NULL;
554 memcpy(connection->peek_msg, buf, *recvd);
555 connection->peek_msg[*recvd] = '\0';
558 if (*recvd < 1 && len)
567 /******************************************************************************
570 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
575 if (!NETCON_connected(connection)) return FALSE;
577 if (!connection->useSSL)
581 BOOL bSuccess = FALSE;
585 FD_SET(connection->socketFD, &infd);
586 tv.tv_sec=RESPONSE_TIMEOUT;
589 while (nRecv < *dwBuffer)
591 if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
593 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
595 INTERNET_SetLastError(sock_get_error(errno));
599 if (lpszBuffer[nRecv] == '\n')
604 if (lpszBuffer[nRecv] != '\r')
609 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
614 lend: /* FIXME: don't use labels */
617 lpszBuffer[nRecv++] = '\0';
619 TRACE(":%lu %s\n", nRecv, lpszBuffer);
629 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
634 prev_timeout = pSSL_CTX_get_timeout(ctx);
635 pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
637 while (nRecv < *dwBuffer)
640 if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
642 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
646 if (lpszBuffer[nRecv] == '\n')
651 if (lpszBuffer[nRecv] != '\r')
655 pSSL_CTX_set_timeout(ctx, prev_timeout);
658 lpszBuffer[nRecv++] = '\0';
660 TRACE("_SSL:%lu %s\n", nRecv, lpszBuffer);
671 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
674 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
676 unsigned char* buffer,*p;
678 BOOL malloced = FALSE;
681 if (!connection->useSSL)
684 cert = pSSL_get_peer_certificate(connection->ssl_s);
686 len = pi2d_X509(cert,&p);
688 * SSL 0.9.7 and above malloc the buffer if it is null.
689 * however earlier version do not and so we would need to alloc the buffer.
691 * see the i2d_X509 man page for more details.
695 buffer = HeapAlloc(GetProcessHeap(),0,len);
697 len = pi2d_X509(cert,&p);
705 r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
710 HeapFree(GetProcessHeap(),0,buffer);
718 BOOL NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
723 /* FIXME: we should probably store the timeout in the connection to set
724 * when we do connect */
725 if (!NETCON_connected(connection))
728 /* value is in milliseconds, convert to struct timeval */
729 tv.tv_sec = value / 1000;
730 tv.tv_usec = (value % 1000) * 1000;
732 result = setsockopt(connection->socketFD, SOL_SOCKET,
733 send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
738 WARN("setsockopt failed (%s)\n", strerror(errno));
739 INTERNET_SetLastError(sock_get_error(errno));