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