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