2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_SOCKET_H
28 # include <sys/socket.h>
30 #ifdef HAVE_SYS_IOCTL_H
31 # include <sys/ioctl.h>
33 #ifdef HAVE_SYS_FILIO_H
34 # include <sys/filio.h>
39 #ifdef HAVE_OPENSSL_SSL_H
40 # include <openssl/ssl.h>
45 #include "wine/debug.h"
46 #include "wine/library.h"
53 #include "winhttp_private.h"
55 /* to avoid conflicts with the Unix socket headers */
59 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
61 #define DEFAULT_SEND_TIMEOUT 30
62 #define DEFAULT_RECEIVE_TIMEOUT 30
64 #ifndef HAVE_GETADDRINFO
66 /* critical section to protect non-reentrant gethostbyname() */
67 static CRITICAL_SECTION cs_gethostbyname;
68 static CRITICAL_SECTION_DEBUG critsect_debug =
70 0, 0, &cs_gethostbyname,
71 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
72 0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
74 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
80 #include <openssl/err.h>
82 static void *libssl_handle;
83 static void *libcrypto_handle;
85 static SSL_METHOD *method;
88 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
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 );
107 MAKE_FUNCPTR( BIO_new_fp );
108 MAKE_FUNCPTR( ERR_get_error );
109 MAKE_FUNCPTR( ERR_error_string );
110 MAKE_FUNCPTR( i2d_X509 );
115 /* translate a unix error code into a winsock error code */
116 static int sock_get_error( int err )
118 #if !defined(__MINGW32__) && !defined (_MSC_VER)
121 case EINTR: return WSAEINTR;
122 case EBADF: return WSAEBADF;
124 case EACCES: return WSAEACCES;
125 case EFAULT: return WSAEFAULT;
126 case EINVAL: return WSAEINVAL;
127 case EMFILE: return WSAEMFILE;
128 case EWOULDBLOCK: return WSAEWOULDBLOCK;
129 case EINPROGRESS: return WSAEINPROGRESS;
130 case EALREADY: return WSAEALREADY;
131 case ENOTSOCK: return WSAENOTSOCK;
132 case EDESTADDRREQ: return WSAEDESTADDRREQ;
133 case EMSGSIZE: return WSAEMSGSIZE;
134 case EPROTOTYPE: return WSAEPROTOTYPE;
135 case ENOPROTOOPT: return WSAENOPROTOOPT;
136 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
137 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
138 case EOPNOTSUPP: return WSAEOPNOTSUPP;
139 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
140 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
141 case EADDRINUSE: return WSAEADDRINUSE;
142 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
143 case ENETDOWN: return WSAENETDOWN;
144 case ENETUNREACH: return WSAENETUNREACH;
145 case ENETRESET: return WSAENETRESET;
146 case ECONNABORTED: return WSAECONNABORTED;
148 case ECONNRESET: return WSAECONNRESET;
149 case ENOBUFS: return WSAENOBUFS;
150 case EISCONN: return WSAEISCONN;
151 case ENOTCONN: return WSAENOTCONN;
152 case ESHUTDOWN: return WSAESHUTDOWN;
153 case ETOOMANYREFS: return WSAETOOMANYREFS;
154 case ETIMEDOUT: return WSAETIMEDOUT;
155 case ECONNREFUSED: return WSAECONNREFUSED;
156 case ELOOP: return WSAELOOP;
157 case ENAMETOOLONG: return WSAENAMETOOLONG;
158 case EHOSTDOWN: return WSAEHOSTDOWN;
159 case EHOSTUNREACH: return WSAEHOSTUNREACH;
160 case ENOTEMPTY: return WSAENOTEMPTY;
162 case EPROCLIM: return WSAEPROCLIM;
165 case EUSERS: return WSAEUSERS;
168 case EDQUOT: return WSAEDQUOT;
171 case ESTALE: return WSAESTALE;
174 case EREMOTE: return WSAEREMOTE;
176 default: errno = err; perror( "sock_set_error" ); return WSAEFAULT;
182 BOOL netconn_init( netconn_t *conn, BOOL secure )
185 if (!secure) return TRUE;
187 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
188 if (libssl_handle) return TRUE;
189 if (!(libssl_handle = wine_dlopen( SONAME_LIBSSL, RTLD_NOW, NULL, 0 )))
191 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
192 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
195 if (!(libcrypto_handle = wine_dlopen( SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0 )))
197 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
198 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
201 #define LOAD_FUNCPTR(x) \
202 if (!(p##x = wine_dlsym( libssl_handle, #x, NULL, 0 ))) \
204 ERR("Failed to load symbol %s\n", #x); \
205 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
208 LOAD_FUNCPTR( SSL_library_init );
209 LOAD_FUNCPTR( SSL_load_error_strings );
210 LOAD_FUNCPTR( SSLv23_method );
211 LOAD_FUNCPTR( SSL_CTX_new );
212 LOAD_FUNCPTR( SSL_new );
213 LOAD_FUNCPTR( SSL_free );
214 LOAD_FUNCPTR( SSL_set_fd );
215 LOAD_FUNCPTR( SSL_connect );
216 LOAD_FUNCPTR( SSL_shutdown );
217 LOAD_FUNCPTR( SSL_write );
218 LOAD_FUNCPTR( SSL_read );
219 LOAD_FUNCPTR( SSL_get_verify_result );
220 LOAD_FUNCPTR( SSL_get_peer_certificate );
221 LOAD_FUNCPTR( SSL_CTX_get_timeout );
222 LOAD_FUNCPTR( SSL_CTX_set_timeout );
223 LOAD_FUNCPTR( SSL_CTX_set_default_verify_paths );
226 #define LOAD_FUNCPTR(x) \
227 if (!(p##x = wine_dlsym( libcrypto_handle, #x, NULL, 0 ))) \
229 ERR("Failed to load symbol %s\n", #x); \
230 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
233 LOAD_FUNCPTR( BIO_new_fp );
234 LOAD_FUNCPTR( ERR_get_error );
235 LOAD_FUNCPTR( ERR_error_string );
236 LOAD_FUNCPTR( i2d_X509 );
240 pSSL_load_error_strings();
241 pBIO_new_fp( stderr, BIO_NOCLOSE );
243 method = pSSLv23_method();
245 WARN("SSL support not compiled in.\n");
246 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
252 BOOL netconn_connected( netconn_t *conn )
254 return (conn->socket != -1);
257 BOOL netconn_create( netconn_t *conn, int domain, int type, int protocol )
259 if ((conn->socket = socket( domain, type, protocol )) == -1)
261 WARN("unable to create socket (%s)\n", strerror(errno));
262 set_last_error( sock_get_error( errno ) );
268 BOOL netconn_close( netconn_t *conn )
275 heap_free( conn->peek_msg_mem );
276 conn->peek_msg_mem = NULL;
277 conn->peek_msg = NULL;
280 pSSL_shutdown( conn->ssl_conn );
281 pSSL_free( conn->ssl_conn );
283 conn->ssl_conn = NULL;
284 conn->secure = FALSE;
287 res = closesocket( conn->socket );
291 set_last_error( sock_get_error( errno ) );
297 BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned int addr_len )
299 if (connect( conn->socket, sockaddr, addr_len ) == -1)
301 WARN("unable to connect to host (%s)\n", strerror(errno));
302 set_last_error( sock_get_error( errno ) );
308 BOOL netconn_secure_connect( netconn_t *conn )
314 ctx = pSSL_CTX_new( method );
315 if (!pSSL_CTX_set_default_verify_paths( ctx ))
317 ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
318 set_last_error( ERROR_OUTOFMEMORY );
321 if (!(conn->ssl_conn = pSSL_new( ctx )))
323 ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
324 set_last_error( ERROR_OUTOFMEMORY );
327 if (!pSSL_set_fd( conn->ssl_conn, conn->socket ))
329 ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
330 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
333 if (pSSL_connect( conn->ssl_conn ) <= 0)
335 ERR("SSL_connect failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
336 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
339 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn )))
341 ERR("No certificate for server: %s\n", pERR_error_string( pERR_get_error(), 0 ));
342 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
345 if ((res = pSSL_get_verify_result( conn->ssl_conn )) != X509_V_OK)
347 /* FIXME: we should set an error and return, but we only print an error at the moment */
348 ERR("couldn't verify server certificate (%ld)\n", res);
350 TRACE("established SSL connection\n");
357 pSSL_shutdown( conn->ssl_conn );
358 pSSL_free( conn->ssl_conn );
359 conn->ssl_conn = NULL;
365 BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int flags, int *sent )
367 if (!netconn_connected( conn )) return FALSE;
371 if (flags) FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
372 *sent = pSSL_write( conn->ssl_conn, msg, len );
373 if (*sent < 1 && len) return FALSE;
379 if ((*sent = send( conn->socket, msg, len, flags )) == -1)
381 set_last_error( sock_get_error( errno ) );
387 BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
390 if (!netconn_connected( conn )) return FALSE;
391 if (!len) return TRUE;
396 if (flags & ~(MSG_PEEK | MSG_WAITALL))
397 FIXME("SSL_read does not support the following flags: %08x\n", flags);
399 /* this ugly hack is all for MSG_PEEK */
400 if (flags & MSG_PEEK && !conn->peek_msg)
402 if (!(conn->peek_msg = conn->peek_msg_mem = heap_alloc( len + 1 ))) return FALSE;
404 else if (flags & MSG_PEEK && conn->peek_msg)
406 if (len < conn->peek_len) FIXME("buffer isn't big enough, should we wrap?\n");
407 *recvd = min( len, conn->peek_len );
408 memcpy( buf, conn->peek_msg, *recvd );
411 else if (conn->peek_msg)
413 *recvd = min( len, conn->peek_len );
414 memcpy( buf, conn->peek_msg, *recvd );
415 conn->peek_len -= *recvd;
416 conn->peek_msg += *recvd;
418 if (conn->peek_len == 0)
420 heap_free( conn->peek_msg_mem );
421 conn->peek_msg_mem = NULL;
422 conn->peek_msg = NULL;
424 /* check if we have enough data from the peek buffer */
425 if (!(flags & MSG_WAITALL) || (*recvd == len)) return TRUE;
427 *recvd += pSSL_read( conn->ssl_conn, (char *)buf + *recvd, len - *recvd );
428 if (flags & MSG_PEEK) /* must copy into buffer */
430 conn->peek_len = *recvd;
433 heap_free( conn->peek_msg_mem );
434 conn->peek_msg_mem = NULL;
435 conn->peek_msg = NULL;
437 else memcpy( conn->peek_msg, buf, *recvd );
439 if (*recvd < 1 && len) return FALSE;
445 if ((*recvd = recv( conn->socket, buf, len, flags )) == -1)
447 set_last_error( sock_get_error( errno ) );
453 BOOL netconn_query_data_available( netconn_t *conn, DWORD *available )
459 if (!netconn_connected( conn )) return FALSE;
464 if (conn->peek_msg) *available = conn->peek_len;
469 if (!(ret = ioctlsocket( conn->socket, FIONREAD, &unread ))) *available = unread;
474 BOOL netconn_get_next_line( netconn_t *conn, char *buffer, DWORD *buflen )
480 if (!netconn_connected( conn )) return FALSE;
487 timeout = pSSL_CTX_get_timeout( ctx );
488 pSSL_CTX_set_timeout( ctx, DEFAULT_RECEIVE_TIMEOUT );
490 while (recvd < *buflen)
493 if (!netconn_recv( conn, &buffer[recvd], 1, 0, &dummy ))
495 set_last_error( ERROR_CONNECTION_ABORTED );
498 if (buffer[recvd] == '\n')
503 if (buffer[recvd] != '\r') recvd++;
505 pSSL_CTX_set_timeout( ctx, timeout );
510 TRACE("received line %s\n", debugstr_a(buffer));
518 pfd.fd = conn->socket;
520 while (recvd < *buflen)
522 if (poll( &pfd, 1, DEFAULT_RECEIVE_TIMEOUT * 1000 ) > 0)
525 if ((res = recv( conn->socket, &buffer[recvd], 1, 0 )) <= 0)
527 if (res == -1) set_last_error( sock_get_error( errno ) );
530 if (buffer[recvd] == '\n')
535 if (buffer[recvd] != '\r') recvd++;
539 set_last_error( ERROR_WINHTTP_TIMEOUT );
547 TRACE("received line %s\n", debugstr_a(buffer));
552 DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
557 /* value is in milliseconds, convert to struct timeval */
558 tv.tv_sec = value / 1000;
559 tv.tv_usec = (value % 1000) * 1000;
561 if ((res = setsockopt( netconn->socket, SOL_SOCKET, send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv, sizeof(tv) ) == -1))
563 WARN("setsockopt failed (%s)\n", strerror( errno ));
564 return sock_get_error( errno );
566 return ERROR_SUCCESS;
569 BOOL netconn_resolve( WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr_in *sa )
572 #ifdef HAVE_GETADDRINFO
573 struct addrinfo *res, hints;
579 if (!(hostname = strdupWA( hostnameW ))) return FALSE;
581 #ifdef HAVE_GETADDRINFO
582 memset( &hints, 0, sizeof(struct addrinfo) );
583 hints.ai_family = AF_INET;
585 ret = getaddrinfo( hostname, NULL, &hints, &res );
586 heap_free( hostname );
589 TRACE("failed to get address of %s (%s)\n", debugstr_w(hostnameW), gai_strerror(ret));
592 memset( sa, 0, sizeof(struct sockaddr_in) );
593 memcpy( &sa->sin_addr, &((struct sockaddr_in *)res->ai_addr)->sin_addr, sizeof(struct in_addr) );
594 sa->sin_family = res->ai_family;
595 sa->sin_port = htons( port );
599 EnterCriticalSection( &cs_gethostbyname );
601 he = gethostbyname( hostname );
602 heap_free( hostname );
605 TRACE("failed to get address of %s (%d)\n", debugstr_w(hostnameW), h_errno);
606 LeaveCriticalSection( &cs_gethostbyname );
609 memset( sa, 0, sizeof(struct sockaddr_in) );
610 memcpy( &sa->sin_addr, he->h_addr, he->h_length );
611 sa->sin_family = he->h_addrtype;
612 sa->sin_port = htons( port );
614 LeaveCriticalSection( &cs_gethostbyname );
619 const void *netconn_get_certificate( netconn_t *conn )
623 unsigned char *buffer, *p;
626 const CERT_CONTEXT *ret;
628 if (!conn->secure) return NULL;
630 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn ))) return NULL;
632 if ((len = pi2d_X509( cert, &p )) < 0) return NULL;
634 * SSL 0.9.7 and above malloc the buffer if it is null.
635 * however earlier version do not and so we would need to alloc the buffer.
637 * see the i2d_X509 man page for more details.
641 if (!(buffer = heap_alloc( len ))) return NULL;
643 len = pi2d_X509( cert, &p );
651 ret = CertCreateCertificateContext( X509_ASN_ENCODING, buffer, len );
653 if (malloc) free( buffer );
654 else heap_free( buffer );