msi: GetDriveType and GetVolumeInformation require a path terminated by a slash.
[wine] / dlls / winhttp / net.c
1 /*
2  * Copyright 2008 Hans Leidekker for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <errno.h>
25
26 #include <sys/types.h>
27 #ifdef HAVE_SYS_SOCKET_H
28 # include <sys/socket.h>
29 #endif
30 #ifdef HAVE_SYS_IOCTL_H
31 # include <sys/ioctl.h>
32 #endif
33 #ifdef HAVE_SYS_FILIO_H
34 # include <sys/filio.h>
35 #endif
36 #ifdef HAVE_POLL_H
37 # include <poll.h>
38 #endif
39 #ifdef HAVE_OPENSSL_SSL_H
40 # include <openssl/ssl.h>
41 #undef FAR
42 #undef DSA
43 #endif
44
45 #include "wine/debug.h"
46 #include "wine/library.h"
47
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winhttp.h"
51 #include "wincrypt.h"
52
53 #include "winhttp_private.h"
54
55 /* to avoid conflicts with the Unix socket headers */
56 #define USE_WS_PREFIX
57 #include "winsock2.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
60
61 #ifndef HAVE_GETADDRINFO
62
63 /* critical section to protect non-reentrant gethostbyname() */
64 static CRITICAL_SECTION cs_gethostbyname;
65 static CRITICAL_SECTION_DEBUG critsect_debug =
66 {
67     0, 0, &cs_gethostbyname,
68     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
69       0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
70 };
71 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
72
73 #endif
74
75 #ifdef SONAME_LIBSSL
76
77 #include <openssl/err.h>
78
79 static void *libssl_handle;
80 static void *libcrypto_handle;
81
82 static SSL_METHOD *method;
83 static SSL_CTX *ctx;
84
85 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
86
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_set_default_verify_paths );
101
102 MAKE_FUNCPTR( BIO_new_fp );
103 MAKE_FUNCPTR( ERR_get_error );
104 MAKE_FUNCPTR( ERR_error_string );
105 MAKE_FUNCPTR( i2d_X509 );
106 #undef MAKE_FUNCPTR
107
108 #endif
109
110 /* translate a unix error code into a winsock error code */
111 static int sock_get_error( int err )
112 {
113 #if !defined(__MINGW32__) && !defined (_MSC_VER)
114     switch (err)
115     {
116         case EINTR:             return WSAEINTR;
117         case EBADF:             return WSAEBADF;
118         case EPERM:
119         case EACCES:            return WSAEACCES;
120         case EFAULT:            return WSAEFAULT;
121         case EINVAL:            return WSAEINVAL;
122         case EMFILE:            return WSAEMFILE;
123         case EWOULDBLOCK:       return WSAEWOULDBLOCK;
124         case EINPROGRESS:       return WSAEINPROGRESS;
125         case EALREADY:          return WSAEALREADY;
126         case ENOTSOCK:          return WSAENOTSOCK;
127         case EDESTADDRREQ:      return WSAEDESTADDRREQ;
128         case EMSGSIZE:          return WSAEMSGSIZE;
129         case EPROTOTYPE:        return WSAEPROTOTYPE;
130         case ENOPROTOOPT:       return WSAENOPROTOOPT;
131         case EPROTONOSUPPORT:   return WSAEPROTONOSUPPORT;
132         case ESOCKTNOSUPPORT:   return WSAESOCKTNOSUPPORT;
133         case EOPNOTSUPP:        return WSAEOPNOTSUPP;
134         case EPFNOSUPPORT:      return WSAEPFNOSUPPORT;
135         case EAFNOSUPPORT:      return WSAEAFNOSUPPORT;
136         case EADDRINUSE:        return WSAEADDRINUSE;
137         case EADDRNOTAVAIL:     return WSAEADDRNOTAVAIL;
138         case ENETDOWN:          return WSAENETDOWN;
139         case ENETUNREACH:       return WSAENETUNREACH;
140         case ENETRESET:         return WSAENETRESET;
141         case ECONNABORTED:      return WSAECONNABORTED;
142         case EPIPE:
143         case ECONNRESET:        return WSAECONNRESET;
144         case ENOBUFS:           return WSAENOBUFS;
145         case EISCONN:           return WSAEISCONN;
146         case ENOTCONN:          return WSAENOTCONN;
147         case ESHUTDOWN:         return WSAESHUTDOWN;
148         case ETOOMANYREFS:      return WSAETOOMANYREFS;
149         case ETIMEDOUT:         return WSAETIMEDOUT;
150         case ECONNREFUSED:      return WSAECONNREFUSED;
151         case ELOOP:             return WSAELOOP;
152         case ENAMETOOLONG:      return WSAENAMETOOLONG;
153         case EHOSTDOWN:         return WSAEHOSTDOWN;
154         case EHOSTUNREACH:      return WSAEHOSTUNREACH;
155         case ENOTEMPTY:         return WSAENOTEMPTY;
156 #ifdef EPROCLIM
157         case EPROCLIM:          return WSAEPROCLIM;
158 #endif
159 #ifdef EUSERS
160         case EUSERS:            return WSAEUSERS;
161 #endif
162 #ifdef EDQUOT
163         case EDQUOT:            return WSAEDQUOT;
164 #endif
165 #ifdef ESTALE
166         case ESTALE:            return WSAESTALE;
167 #endif
168 #ifdef EREMOTE
169         case EREMOTE:           return WSAEREMOTE;
170 #endif
171     default: errno = err; perror( "sock_set_error" ); return WSAEFAULT;
172     }
173 #endif
174     return err;
175 }
176
177 BOOL netconn_init( netconn_t *conn, BOOL secure )
178 {
179     conn->socket = -1;
180     if (!secure) return TRUE;
181
182 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
183     if (libssl_handle) return TRUE;
184     if (!(libssl_handle = wine_dlopen( SONAME_LIBSSL, RTLD_NOW, NULL, 0 )))
185     {
186         ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
187         set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
188         return FALSE;
189     }
190     if (!(libcrypto_handle = wine_dlopen( SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0 )))
191     {
192         ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
193         set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
194         return FALSE;
195     }
196 #define LOAD_FUNCPTR(x) \
197     if (!(p##x = wine_dlsym( libssl_handle, #x, NULL, 0 ))) \
198     { \
199         ERR("Failed to load symbol %s\n", #x); \
200         set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
201         return FALSE; \
202     }
203     LOAD_FUNCPTR( SSL_library_init );
204     LOAD_FUNCPTR( SSL_load_error_strings );
205     LOAD_FUNCPTR( SSLv23_method );
206     LOAD_FUNCPTR( SSL_CTX_new );
207     LOAD_FUNCPTR( SSL_new );
208     LOAD_FUNCPTR( SSL_free );
209     LOAD_FUNCPTR( SSL_set_fd );
210     LOAD_FUNCPTR( SSL_connect );
211     LOAD_FUNCPTR( SSL_shutdown );
212     LOAD_FUNCPTR( SSL_write );
213     LOAD_FUNCPTR( SSL_read );
214     LOAD_FUNCPTR( SSL_get_verify_result );
215     LOAD_FUNCPTR( SSL_get_peer_certificate );
216     LOAD_FUNCPTR( SSL_CTX_set_default_verify_paths );
217 #undef LOAD_FUNCPTR
218
219 #define LOAD_FUNCPTR(x) \
220     if (!(p##x = wine_dlsym( libcrypto_handle, #x, NULL, 0 ))) \
221     { \
222         ERR("Failed to load symbol %s\n", #x); \
223         set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
224         return FALSE; \
225     }
226     LOAD_FUNCPTR( BIO_new_fp );
227     LOAD_FUNCPTR( ERR_get_error );
228     LOAD_FUNCPTR( ERR_error_string );
229     LOAD_FUNCPTR( i2d_X509 );
230 #undef LOAD_FUNCPTR
231
232     pSSL_library_init();
233     pSSL_load_error_strings();
234     pBIO_new_fp( stderr, BIO_NOCLOSE );
235
236     method = pSSLv23_method();
237 #else
238     WARN("SSL support not compiled in.\n");
239     set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
240     return FALSE;
241 #endif
242     return TRUE;
243 }
244
245 BOOL netconn_connected( netconn_t *conn )
246 {
247     return (conn->socket != -1);
248 }
249
250 BOOL netconn_create( netconn_t *conn, int domain, int type, int protocol )
251 {
252     if ((conn->socket = socket( domain, type, protocol )) == -1)
253     {
254         WARN("unable to create socket (%s)\n", strerror(errno));
255         set_last_error( sock_get_error( errno ) );
256         return FALSE;
257     }
258     return TRUE;
259 }
260
261 BOOL netconn_close( netconn_t *conn )
262 {
263     int res;
264
265 #ifdef SONAME_LIBSSL
266     if (conn->secure)
267     {
268         heap_free( conn->peek_msg_mem );
269         conn->peek_msg_mem = NULL;
270         conn->peek_msg = NULL;
271         conn->peek_len = 0;
272
273         pSSL_shutdown( conn->ssl_conn );
274         pSSL_free( conn->ssl_conn );
275
276         conn->ssl_conn = NULL;
277         conn->secure = FALSE;
278     }
279 #endif
280     res = closesocket( conn->socket );
281     conn->socket = -1;
282     if (res == -1)
283     {
284         set_last_error( sock_get_error( errno ) );
285         return FALSE;
286     }
287     return TRUE;
288 }
289
290 BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned int addr_len, int timeout )
291 {
292     BOOL ret = FALSE;
293     int res = 0, state;
294
295     if (timeout > 0)
296     {
297         state = 1;
298         ioctlsocket( conn->socket, FIONBIO, &state );
299     }
300     if (connect( conn->socket, sockaddr, addr_len ) < 0)
301     {
302         res = sock_get_error( errno );
303         if (res == WSAEWOULDBLOCK || res == WSAEINPROGRESS)
304         {
305             struct pollfd pfd;
306
307             pfd.fd = conn->socket;
308             pfd.events = POLLOUT;
309             if (poll( &pfd, 1, timeout ) > 0)
310                 ret = TRUE;
311             else
312                 res = sock_get_error( errno );
313         }
314     }
315     else
316         ret = TRUE;
317     if (timeout > 0)
318     {
319         state = 0;
320         ioctlsocket( conn->socket, FIONBIO, &state );
321     }
322     if (!ret)
323     {
324         WARN("unable to connect to host (%d)\n", res);
325         set_last_error( res );
326     }
327     return ret;
328 }
329
330 BOOL netconn_secure_connect( netconn_t *conn )
331 {
332 #ifdef SONAME_LIBSSL
333     X509 *cert;
334     long res;
335
336     ctx = pSSL_CTX_new( method );
337     if (!pSSL_CTX_set_default_verify_paths( ctx ))
338     {
339         ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
340         set_last_error( ERROR_OUTOFMEMORY );
341         return FALSE;
342     }
343     if (!(conn->ssl_conn = pSSL_new( ctx )))
344     {
345         ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
346         set_last_error( ERROR_OUTOFMEMORY );
347         goto fail;
348     }
349     if (!pSSL_set_fd( conn->ssl_conn, conn->socket ))
350     {
351         ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
352         set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
353         goto fail;
354     }
355     if (pSSL_connect( conn->ssl_conn ) <= 0)
356     {
357         ERR("SSL_connect failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
358         set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
359         goto fail;
360     }
361     if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn )))
362     {
363         ERR("No certificate for server: %s\n", pERR_error_string( pERR_get_error(), 0 ));
364         set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
365         goto fail;
366     }
367     if ((res = pSSL_get_verify_result( conn->ssl_conn )) != X509_V_OK)
368     {
369         /* FIXME: we should set an error and return, but we only print an error at the moment */
370         ERR("couldn't verify server certificate (%ld)\n", res);
371     }
372     TRACE("established SSL connection\n");
373     conn->secure = TRUE;
374     return TRUE;
375
376 fail:
377     if (conn->ssl_conn)
378     {
379         pSSL_shutdown( conn->ssl_conn );
380         pSSL_free( conn->ssl_conn );
381         conn->ssl_conn = NULL;
382     }
383 #endif
384     return FALSE;
385 }
386
387 BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int flags, int *sent )
388 {
389     if (!netconn_connected( conn )) return FALSE;
390     if (conn->secure)
391     {
392 #ifdef SONAME_LIBSSL
393         if (flags) FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
394         *sent = pSSL_write( conn->ssl_conn, msg, len );
395         if (*sent < 1 && len) return FALSE;
396         return TRUE;
397 #else
398         return FALSE;
399 #endif
400     }
401     if ((*sent = send( conn->socket, msg, len, flags )) == -1)
402     {
403         set_last_error( sock_get_error( errno ) );
404         return FALSE;
405     }
406     return TRUE;
407 }
408
409 BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
410 {
411     *recvd = 0;
412     if (!netconn_connected( conn )) return FALSE;
413     if (!len) return TRUE;
414
415     if (conn->secure)
416     {
417 #ifdef SONAME_LIBSSL
418         if (flags & ~(MSG_PEEK | MSG_WAITALL))
419             FIXME("SSL_read does not support the following flags: %08x\n", flags);
420
421         /* this ugly hack is all for MSG_PEEK */
422         if (flags & MSG_PEEK && !conn->peek_msg)
423         {
424             if (!(conn->peek_msg = conn->peek_msg_mem = heap_alloc( len + 1 ))) return FALSE;
425         }
426         else if (flags & MSG_PEEK && conn->peek_msg)
427         {
428             if (len < conn->peek_len) FIXME("buffer isn't big enough, should we wrap?\n");
429             *recvd = min( len, conn->peek_len );
430             memcpy( buf, conn->peek_msg, *recvd );
431             return TRUE;
432         }
433         else if (conn->peek_msg)
434         {
435             *recvd = min( len, conn->peek_len );
436             memcpy( buf, conn->peek_msg, *recvd );
437             conn->peek_len -= *recvd;
438             conn->peek_msg += *recvd;
439
440             if (conn->peek_len == 0)
441             {
442                 heap_free( conn->peek_msg_mem );
443                 conn->peek_msg_mem = NULL;
444                 conn->peek_msg = NULL;
445             }
446             /* check if we have enough data from the peek buffer */
447             if (!(flags & MSG_WAITALL) || (*recvd == len)) return TRUE;
448         }
449         *recvd += pSSL_read( conn->ssl_conn, (char *)buf + *recvd, len - *recvd );
450         if (flags & MSG_PEEK) /* must copy into buffer */
451         {
452             conn->peek_len = *recvd;
453             if (!*recvd)
454             {
455                 heap_free( conn->peek_msg_mem );
456                 conn->peek_msg_mem = NULL;
457                 conn->peek_msg = NULL;
458             }
459             else memcpy( conn->peek_msg, buf, *recvd );
460         }
461         if (*recvd < 1 && len) return FALSE;
462         return TRUE;
463 #else
464         return FALSE;
465 #endif
466     }
467     if ((*recvd = recv( conn->socket, buf, len, flags )) == -1)
468     {
469         set_last_error( sock_get_error( errno ) );
470         return FALSE;
471     }
472     return TRUE;
473 }
474
475 BOOL netconn_query_data_available( netconn_t *conn, DWORD *available )
476 {
477 #ifdef FIONREAD
478     int ret, unread;
479 #endif
480     *available = 0;
481     if (!netconn_connected( conn )) return FALSE;
482
483     if (conn->secure)
484     {
485 #ifdef SONAME_LIBSSL
486         if (conn->peek_msg) *available = conn->peek_len;
487 #endif
488         return TRUE;
489     }
490 #ifdef FIONREAD
491     if (!(ret = ioctlsocket( conn->socket, FIONREAD, &unread ))) *available = unread;
492 #endif
493     return TRUE;
494 }
495
496 BOOL netconn_get_next_line( netconn_t *conn, char *buffer, DWORD *buflen )
497 {
498     struct pollfd pfd;
499     BOOL ret = FALSE;
500     DWORD recvd = 0;
501
502     if (!netconn_connected( conn )) return FALSE;
503
504     if (conn->secure)
505     {
506 #ifdef SONAME_LIBSSL
507         while (recvd < *buflen)
508         {
509             int dummy;
510             if (!netconn_recv( conn, &buffer[recvd], 1, 0, &dummy ))
511             {
512                 set_last_error( ERROR_CONNECTION_ABORTED );
513                 break;
514             }
515             if (buffer[recvd] == '\n')
516             {
517                 ret = TRUE;
518                 break;
519             }
520             if (buffer[recvd] != '\r') recvd++;
521         }
522         if (ret)
523         {
524             buffer[recvd++] = 0;
525             *buflen = recvd;
526             TRACE("received line %s\n", debugstr_a(buffer));
527         }
528         return ret;
529 #else
530         return FALSE;
531 #endif
532     }
533
534     pfd.fd = conn->socket;
535     pfd.events = POLLIN;
536     while (recvd < *buflen)
537     {
538         int timeout, res;
539         struct timeval tv;
540         socklen_t len = sizeof(tv);
541
542         if ((res = getsockopt( conn->socket, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, &len ) != -1))
543             timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
544         else
545             timeout = -1;
546         if (poll( &pfd, 1, timeout ) > 0)
547         {
548             if ((res = recv( conn->socket, &buffer[recvd], 1, 0 )) <= 0)
549             {
550                 if (res == -1) set_last_error( sock_get_error( errno ) );
551                 break;
552             }
553             if (buffer[recvd] == '\n')
554             {
555                 ret = TRUE;
556                 break;
557             }
558             if (buffer[recvd] != '\r') recvd++;
559         }
560         else
561         {
562             set_last_error( ERROR_WINHTTP_TIMEOUT );
563             break;
564         }
565     }
566     if (ret)
567     {
568         buffer[recvd++] = 0;
569         *buflen = recvd;
570         TRACE("received line %s\n", debugstr_a(buffer));
571     }
572     return ret;
573 }
574
575 DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
576 {
577     int res;
578     struct timeval tv;
579
580     /* value is in milliseconds, convert to struct timeval */
581     tv.tv_sec = value / 1000;
582     tv.tv_usec = (value % 1000) * 1000;
583
584     if ((res = setsockopt( netconn->socket, SOL_SOCKET, send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv, sizeof(tv) ) == -1))
585     {
586         WARN("setsockopt failed (%s)\n", strerror( errno ));
587         return sock_get_error( errno );
588     }
589     return ERROR_SUCCESS;
590 }
591
592 BOOL netconn_resolve( WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr *sa, socklen_t *sa_len )
593 {
594     char *hostname;
595 #ifdef HAVE_GETADDRINFO
596     struct addrinfo *res, hints;
597     int ret;
598 #else
599     struct hostent *he;
600     struct sockaddr_in *sin = (struct sockaddr_in *)sa;
601 #endif
602
603     if (!(hostname = strdupWA( hostnameW ))) return FALSE;
604
605 #ifdef HAVE_GETADDRINFO
606     memset( &hints, 0, sizeof(struct addrinfo) );
607     /* Prefer IPv4 to IPv6 addresses, since some web servers do not listen on
608      * their IPv6 addresses even though they have IPv6 addresses in the DNS.
609      */
610     hints.ai_family = AF_INET;
611
612     ret = getaddrinfo( hostname, NULL, &hints, &res );
613     if (ret != 0)
614     {
615         TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(hostnameW), gai_strerror(ret));
616         hints.ai_family = AF_INET6;
617         ret = getaddrinfo( hostname, NULL, &hints, &res );
618         if (ret != 0)
619         {
620             TRACE("failed to get address of %s (%s)\n", debugstr_w(hostnameW), gai_strerror(ret));
621             heap_free( hostname );
622             return FALSE;
623         }
624     }
625     heap_free( hostname );
626     if (*sa_len < res->ai_addrlen)
627     {
628         WARN("address too small\n");
629         freeaddrinfo( res );
630         return FALSE;
631     }
632     *sa_len = res->ai_addrlen;
633     memcpy( sa, res->ai_addr, res->ai_addrlen );
634     /* Copy port */
635     switch (res->ai_family)
636     {
637     case AF_INET:
638         ((struct sockaddr_in *)sa)->sin_port = htons( port );
639         break;
640     case AF_INET6:
641         ((struct sockaddr_in6 *)sa)->sin6_port = htons( port );
642         break;
643     }
644
645     freeaddrinfo( res );
646     return TRUE;
647 #else
648     EnterCriticalSection( &cs_gethostbyname );
649
650     he = gethostbyname( hostname );
651     heap_free( hostname );
652     if (!he)
653     {
654         TRACE("failed to get address of %s (%d)\n", debugstr_w(hostnameW), h_errno);
655         LeaveCriticalSection( &cs_gethostbyname );
656         return FALSE;
657     }
658     if (*sa_len < sizeof(struct sockaddr_in))
659     {
660         WARN("address too small\n");
661         LeaveCriticalSection( &cs_gethostbyname );
662         return FALSE;
663     }
664     *sa_len = sizeof(struct sockaddr_in);
665     memset( sa, 0, sizeof(struct sockaddr_in) );
666     memcpy( &sin->sin_addr, he->h_addr, he->h_length );
667     sin->sin_family = he->h_addrtype;
668     sin->sin_port = htons( port );
669
670     LeaveCriticalSection( &cs_gethostbyname );
671     return TRUE;
672 #endif
673 }
674
675 const void *netconn_get_certificate( netconn_t *conn )
676 {
677 #ifdef SONAME_LIBSSL
678     X509 *cert;
679     unsigned char *buffer, *p;
680     int len;
681     BOOL malloc = FALSE;
682     const CERT_CONTEXT *ret;
683
684     if (!conn->secure) return NULL;
685
686     if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn ))) return NULL;
687     p = NULL;
688     if ((len = pi2d_X509( cert, &p )) < 0) return NULL;
689     /*
690      * SSL 0.9.7 and above malloc the buffer if it is null.
691      * however earlier version do not and so we would need to alloc the buffer.
692      *
693      * see the i2d_X509 man page for more details.
694      */
695     if (!p)
696     {
697         if (!(buffer = heap_alloc( len ))) return NULL;
698         p = buffer;
699         len = pi2d_X509( cert, &p );
700     }
701     else
702     {
703         buffer = p;
704         malloc = TRUE;
705     }
706
707     ret = CertCreateCertificateContext( X509_ASN_ENCODING, buffer, len );
708
709     if (malloc) free( buffer );
710     else heap_free( buffer );
711
712     return ret;
713 #else
714     return NULL;
715 #endif
716 }