wininet/tests: Win64 printf format warning fixes.
[wine] / dlls / wininet / netconnection.c
1 /*
2  * Wininet - networking layer. Uses unix sockets or OpenSSL.
3  *
4  * Copyright 2002 TransGaming Technologies Inc.
5  *
6  * David Hammerton
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
28 #endif
29 #include <sys/types.h>
30 #ifdef HAVE_SYS_SOCKET_H
31 # include <sys/socket.h>
32 #endif
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40 #include <errno.h>
41
42 #include "wine/library.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "wininet.h"
46 #include "winerror.h"
47
48 /* To avoid conflicts with the Unix socket headers. we only need it for
49  * the error codes anyway. */
50 #define USE_WS_PREFIX
51 #include "winsock2.h"
52
53 #include "wine/debug.h"
54 #include "internet.h"
55 #include "wincrypt.h"
56
57 #define RESPONSE_TIMEOUT        30            /* FROM internet.c */
58
59
60 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
61
62 /* FIXME!!!!!!
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
66  */
67
68 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
69
70 #include <openssl/err.h>
71
72 #ifndef SONAME_LIBSSL
73 #define SONAME_LIBSSL "libssl.so"
74 #endif
75 #ifndef SONAME_LIBCRYPTO
76 #define SONAME_LIBCRYPTO "libcrypto.so"
77 #endif
78
79 static void *OpenSSL_ssl_handle;
80 static void *OpenSSL_crypto_handle;
81
82 static SSL_METHOD *meth;
83 static SSL_CTX *ctx;
84
85 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
86
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);
105
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);
110 #undef MAKE_FUNCPTR
111
112 #endif
113
114 BOOL NETCON_init(WININET_NETCONNECTION *connection, BOOL useSSL)
115 {
116     connection->useSSL = FALSE;
117     connection->socketFD = -1;
118     if (useSSL)
119     {
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 */
123             return TRUE;
124         OpenSSL_ssl_handle = wine_dlopen(SONAME_LIBSSL, RTLD_NOW, NULL, 0);
125         if (!OpenSSL_ssl_handle)
126         {
127             ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
128                 SONAME_LIBSSL);
129             INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
130             return FALSE;
131         }
132         OpenSSL_crypto_handle = wine_dlopen(SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0);
133         if (!OpenSSL_crypto_handle)
134         {
135             ERR("trying to use a SSL connection, but couldn't load %s. Expect trouble.\n",
136                 SONAME_LIBCRYPTO);
137             INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
138             return FALSE;
139         }
140
141         /* mmm nice ugly macroness */
142 #define DYNSSL(x) \
143     p##x = wine_dlsym(OpenSSL_ssl_handle, #x, NULL, 0); \
144     if (!p##x) \
145     { \
146         ERR("failed to load symbol %s\n", #x); \
147         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
148         return FALSE; \
149     }
150
151         DYNSSL(SSL_library_init);
152         DYNSSL(SSL_load_error_strings);
153         DYNSSL(SSLv23_method);
154         DYNSSL(SSL_CTX_new);
155         DYNSSL(SSL_new);
156         DYNSSL(SSL_free);
157         DYNSSL(SSL_set_fd);
158         DYNSSL(SSL_connect);
159         DYNSSL(SSL_shutdown);
160         DYNSSL(SSL_write);
161         DYNSSL(SSL_read);
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);
167         DYNSSL(i2d_X509);
168 #undef DYNSSL
169
170 #define DYNCRYPTO(x) \
171     p##x = wine_dlsym(OpenSSL_crypto_handle, #x, NULL, 0); \
172     if (!p##x) \
173     { \
174         ERR("failed to load symbol %s\n", #x); \
175         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR); \
176         return FALSE; \
177     }
178         DYNCRYPTO(BIO_new_fp);
179         DYNCRYPTO(ERR_get_error);
180         DYNCRYPTO(ERR_error_string);
181 #undef DYNCRYPTO
182
183         pSSL_library_init();
184         pSSL_load_error_strings();
185         pBIO_new_fp(stderr, BIO_NOCLOSE); /* FIXME: should use winedebug stuff */
186
187         meth = pSSLv23_method();
188         connection->peek_msg = NULL;
189         connection->peek_msg_mem = NULL;
190 #else
191         FIXME("can't use SSL, not compiled in.\n");
192         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
193         return FALSE;
194 #endif
195     }
196     return TRUE;
197 }
198
199 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
200 {
201     if (connection->socketFD == -1)
202         return FALSE;
203     else
204         return TRUE;
205 }
206
207 /* translate a unix error code into a winsock one */
208 static int sock_get_error( int err )
209 {
210     switch (err)
211     {
212         case EINTR:             return WSAEINTR;
213         case EBADF:             return WSAEBADF;
214         case EPERM:
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;
238         case EPIPE:
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;
252 #ifdef EPROCLIM
253         case EPROCLIM:          return WSAEPROCLIM;
254 #endif
255 #ifdef EUSERS
256         case EUSERS:            return WSAEUSERS;
257 #endif
258 #ifdef EDQUOT
259         case EDQUOT:            return WSAEDQUOT;
260 #endif
261 #ifdef ESTALE
262         case ESTALE:            return WSAESTALE;
263 #endif
264 #ifdef EREMOTE
265         case EREMOTE:           return WSAEREMOTE;
266 #endif
267     default: errno=err; perror("sock_set_error"); return WSAEFAULT;
268     }
269 }
270
271 /******************************************************************************
272  * NETCON_create
273  * Basically calls 'socket()'
274  */
275 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
276               int type, int protocol)
277 {
278 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
279     if (connection->useSSL)
280         return FALSE;
281 #endif
282
283     connection->socketFD = socket(domain, type, protocol);
284     if (connection->socketFD == -1)
285     {
286         INTERNET_SetLastError(sock_get_error(errno));
287         return FALSE;
288     }
289     return TRUE;
290 }
291
292 /******************************************************************************
293  * NETCON_close
294  * Basically calls 'close()' unless we should use SSL
295  */
296 BOOL NETCON_close(WININET_NETCONNECTION *connection)
297 {
298     int result;
299
300     if (!NETCON_connected(connection)) return FALSE;
301
302 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
303     if (connection->useSSL)
304     {
305         HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
306         connection->peek_msg = NULL;
307         connection->peek_msg_mem = NULL;
308         connection->peek_len = 0;
309
310         pSSL_shutdown(connection->ssl_s);
311         pSSL_free(connection->ssl_s);
312         connection->ssl_s = NULL;
313
314         connection->useSSL = FALSE;
315     }
316 #endif
317
318     result = closesocket(connection->socketFD);
319     connection->socketFD = -1;
320
321     if (result == -1)
322     {
323         INTERNET_SetLastError(sock_get_error(errno));
324         return FALSE;
325     }
326     return TRUE;
327 }
328 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
329 static BOOL check_hostname(X509 *cert, char *hostname)
330 {
331     /* FIXME: implement */
332     return TRUE;
333 }
334 #endif
335 /******************************************************************************
336  * NETCON_secure_connect
337  * Initiates a secure connection over an existing plaintext connection.
338  */
339 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
340 {
341 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
342     long verify_res;
343     X509 *cert;
344     int len;
345     char *hostname_unix;
346
347     /* can't connect if we are already connected */
348     if (connection->useSSL)
349     {
350         ERR("already connected\n");
351         return FALSE;
352     }
353
354     ctx = pSSL_CTX_new(meth);
355     if (!pSSL_CTX_set_default_verify_paths(ctx))
356     {
357         ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
358             pERR_error_string(pERR_get_error(), 0));
359         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
360         return FALSE;
361     }
362     connection->ssl_s = pSSL_new(ctx);
363     if (!connection->ssl_s)
364     {
365         ERR("SSL_new failed: %s\n",
366             pERR_error_string(pERR_get_error(), 0));
367         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
368         goto fail;
369     }
370
371     if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
372     {
373         ERR("SSL_set_fd failed: %s\n",
374             pERR_error_string(pERR_get_error(), 0));
375         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
376         goto fail;
377     }
378
379     if (pSSL_connect(connection->ssl_s) <= 0)
380     {
381         ERR("SSL_connect failed: %s\n",
382             pERR_error_string(pERR_get_error(), 0));
383         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
384         goto fail;
385     }
386     cert = pSSL_get_peer_certificate(connection->ssl_s);
387     if (!cert)
388     {
389         ERR("no certificate for server %s\n", debugstr_w(hostname));
390         /* FIXME: is this the best error? */
391         INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
392         goto fail;
393     }
394     verify_res = pSSL_get_verify_result(connection->ssl_s);
395     if (verify_res != X509_V_OK)
396     {
397         ERR("couldn't verify the security of the connection, %ld\n", verify_res);
398         /* FIXME: we should set an error and return, but we only warn at
399          * the moment */
400     }
401
402     len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
403     hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
404     if (!hostname_unix)
405     {
406         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
407         goto fail;
408     }
409     WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
410
411     if (!check_hostname(cert, hostname_unix))
412     {
413         HeapFree(GetProcessHeap(), 0, hostname_unix);
414         INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
415         goto fail;
416     }
417
418     HeapFree(GetProcessHeap(), 0, hostname_unix);
419     connection->useSSL = TRUE;
420     return TRUE;
421
422 fail:
423     if (connection->ssl_s)
424     {
425         pSSL_shutdown(connection->ssl_s);
426         pSSL_free(connection->ssl_s);
427         connection->ssl_s = NULL;
428     }
429 #endif
430     return FALSE;
431 }
432
433 /******************************************************************************
434  * NETCON_connect
435  * Connects to the specified address.
436  */
437 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
438                     unsigned int addrlen)
439 {
440     int result;
441
442     if (!NETCON_connected(connection)) return FALSE;
443
444     result = connect(connection->socketFD, serv_addr, addrlen);
445     if (result == -1)
446     {
447         WARN("Unable to connect to host (%s)\n", strerror(errno));
448         INTERNET_SetLastError(sock_get_error(errno));
449
450         closesocket(connection->socketFD);
451         connection->socketFD = -1;
452         return FALSE;
453     }
454
455     return TRUE;
456 }
457
458 /******************************************************************************
459  * NETCON_send
460  * Basically calls 'send()' unless we should use SSL
461  * number of chars send is put in *sent
462  */
463 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
464                 int *sent /* out */)
465 {
466     if (!NETCON_connected(connection)) return FALSE;
467     if (!connection->useSSL)
468     {
469         *sent = send(connection->socketFD, msg, len, flags);
470         if (*sent == -1)
471         {
472             INTERNET_SetLastError(sock_get_error(errno));
473             return FALSE;
474         }
475         return TRUE;
476     }
477     else
478     {
479 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
480         if (flags)
481             FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
482         *sent = pSSL_write(connection->ssl_s, msg, len);
483         if (*sent < 1 && len)
484             return FALSE;
485         return TRUE;
486 #else
487         return FALSE;
488 #endif
489     }
490 }
491
492 /******************************************************************************
493  * NETCON_recv
494  * Basically calls 'recv()' unless we should use SSL
495  * number of chars received is put in *recvd
496  */
497 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
498                 int *recvd /* out */)
499 {
500     if (!NETCON_connected(connection)) return FALSE;
501     if (!connection->useSSL)
502     {
503         *recvd = recv(connection->socketFD, buf, len, flags);
504         if (*recvd == -1)
505         {
506             INTERNET_SetLastError(sock_get_error(errno));
507             return FALSE;
508         }
509         return TRUE;
510     }
511     else
512     {
513 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
514         if (flags & ~(MSG_PEEK|MSG_WAITALL))
515             FIXME("SSL_read does not support the following flag: %08x\n", flags);
516
517         /* this ugly hack is all for MSG_PEEK. eww gross */
518         if (flags & MSG_PEEK && !connection->peek_msg)
519         {
520             connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
521         }
522         else if (flags & MSG_PEEK && connection->peek_msg)
523         {
524             if (len < connection->peek_len)
525                 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
526             *recvd = min(len, connection->peek_len);
527             memcpy(buf, connection->peek_msg, *recvd);
528             return TRUE;
529         }
530         else if (connection->peek_msg)
531         {
532             *recvd = min(len, connection->peek_len);
533             memcpy(buf, connection->peek_msg, *recvd);
534             connection->peek_len -= *recvd;
535             connection->peek_msg += *recvd;
536             if (connection->peek_len == 0)
537             {
538                 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
539                 connection->peek_msg_mem = NULL;
540                 connection->peek_msg = NULL;
541                 /* check if the peek buffer held too few data */
542                 if ((flags & MSG_WAITALL) && (*recvd < len))
543                 {
544                     int recv2 = 0;
545                     /* recursive call - but now the peek buffer is empty */
546                     if (!NETCON_recv(connection, (char*)buf + *recvd, len - *recvd, flags, &recv2))
547                         return FALSE;
548                     *recvd += recv2;
549                 }
550             }
551             return TRUE;
552         }
553         *recvd = pSSL_read(connection->ssl_s, buf, len);
554         if (flags & MSG_PEEK) /* must copy stuff into buffer */
555         {
556             connection->peek_len = *recvd;
557             if (!*recvd)
558             {
559                 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
560                 connection->peek_msg_mem = NULL;
561                 connection->peek_msg = NULL;
562             }
563             else
564                 memcpy(connection->peek_msg, buf, *recvd);
565         }
566         if (*recvd < 1 && len)
567             return FALSE;
568         return TRUE;
569 #else
570         return FALSE;
571 #endif
572     }
573 }
574
575 /******************************************************************************
576  * NETCON_getNextLine
577  */
578 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
579 {
580
581     TRACE("\n");
582
583     if (!NETCON_connected(connection)) return FALSE;
584
585     if (!connection->useSSL)
586     {
587         struct timeval tv;
588         fd_set infd;
589         BOOL bSuccess = FALSE;
590         DWORD nRecv = 0;
591
592         FD_ZERO(&infd);
593         FD_SET(connection->socketFD, &infd);
594         tv.tv_sec=RESPONSE_TIMEOUT;
595         tv.tv_usec=0;
596
597         while (nRecv < *dwBuffer)
598         {
599             if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
600             {
601                 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
602                 {
603                     INTERNET_SetLastError(sock_get_error(errno));
604                     goto lend;
605                 }
606
607                 if (lpszBuffer[nRecv] == '\n')
608                 {
609                     bSuccess = TRUE;
610                     break;
611                 }
612                 if (lpszBuffer[nRecv] != '\r')
613                     nRecv++;
614             }
615             else
616             {
617                 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
618                 goto lend;
619             }
620         }
621
622     lend:             /* FIXME: don't use labels */
623         if (bSuccess)
624         {
625             lpszBuffer[nRecv++] = '\0';
626             *dwBuffer = nRecv;
627             TRACE(":%lu %s\n", nRecv, lpszBuffer);
628             return TRUE;
629         }
630         else
631         {
632             return FALSE;
633         }
634     }
635     else
636     {
637 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
638         long prev_timeout;
639         DWORD nRecv = 0;
640         BOOL success = TRUE;
641
642         prev_timeout = pSSL_CTX_get_timeout(ctx);
643         pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
644
645         while (nRecv < *dwBuffer)
646         {
647             int recv = 1;
648             if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
649             {
650                 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
651                 success = FALSE;
652             }
653
654             if (lpszBuffer[nRecv] == '\n')
655             {
656                 success = TRUE;
657                 break;
658             }
659             if (lpszBuffer[nRecv] != '\r')
660                 nRecv++;
661         }
662
663         pSSL_CTX_set_timeout(ctx, prev_timeout);
664         if (success)
665         {
666             lpszBuffer[nRecv++] = '\0';
667             *dwBuffer = nRecv;
668             TRACE("_SSL:%lu %s\n", nRecv, lpszBuffer);
669             return TRUE;
670         }
671         return FALSE;
672 #else
673         return FALSE;
674 #endif
675     }
676 }
677
678
679 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
680 {
681
682 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
683     X509* cert;
684     unsigned char* buffer,*p;
685     INT len;
686     BOOL malloced = FALSE;
687     LPCVOID r = NULL;
688
689     if (!connection->useSSL)
690         return NULL;
691
692     cert = pSSL_get_peer_certificate(connection->ssl_s);
693     p = NULL;
694     len = pi2d_X509(cert,&p);
695     /*
696      * SSL 0.9.7 and above malloc the buffer if it is null. 
697      * however earlier version do not and so we would need to alloc the buffer.
698      *
699      * see the i2d_X509 man page for more details.
700      */
701     if (!p)
702     {
703         buffer = HeapAlloc(GetProcessHeap(),0,len);
704         p = buffer;
705         len = pi2d_X509(cert,&p);
706     }
707     else
708     {
709         buffer = p;
710         malloced = TRUE;
711     }
712
713     r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
714
715     if (malloced)
716         free(buffer);
717     else
718         HeapFree(GetProcessHeap(),0,buffer);
719
720     return r;
721 #else
722     return NULL;
723 #endif
724 }
725
726 BOOL NETCON_set_timeout(WININET_NETCONNECTION *connection, BOOL send, int value)
727 {
728     int result;
729     struct timeval tv;
730
731     /* FIXME: we should probably store the timeout in the connection to set
732      * when we do connect */
733     if (!NETCON_connected(connection))
734         return TRUE;
735
736     /* value is in milliseconds, convert to struct timeval */
737     tv.tv_sec = value / 1000;
738     tv.tv_usec = (value % 1000) * 1000;
739
740     result = setsockopt(connection->socketFD, SOL_SOCKET,
741                         send ? SO_SNDTIMEO : SO_RCVTIMEO, &tv,
742                         sizeof(tv));
743
744     if (result == -1)
745     {
746         WARN("setsockopt failed (%s)\n", strerror(errno));
747         INTERNET_SetLastError(sock_get_error(errno));
748         return FALSE;
749     }
750
751     return TRUE;
752 }