wined3d: Implemented WINED3DRS_SLOPESCALEDEPTHBIAS.
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 void 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 initilzed everything */
123             return;
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             connection->useSSL = FALSE;
130             return;
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             connection->useSSL = FALSE;
138             return;
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         connection->useSSL = FALSE; \
148         return; \
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         connection->useSSL = FALSE; \
176         return; \
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         connection->useSSL = FALSE;
193 #endif
194     }
195 }
196
197 BOOL NETCON_connected(WININET_NETCONNECTION *connection)
198 {
199     if (connection->socketFD == -1)
200         return FALSE;
201     else
202         return TRUE;
203 }
204
205 /* translate a unix error code into a winsock one */
206 static int sock_get_error( int err )
207 {
208     switch (err)
209     {
210         case EINTR:             return WSAEINTR;
211         case EBADF:             return WSAEBADF;
212         case EPERM:
213         case EACCES:            return WSAEACCES;
214         case EFAULT:            return WSAEFAULT;
215         case EINVAL:            return WSAEINVAL;
216         case EMFILE:            return WSAEMFILE;
217         case EWOULDBLOCK:       return WSAEWOULDBLOCK;
218         case EINPROGRESS:       return WSAEINPROGRESS;
219         case EALREADY:          return WSAEALREADY;
220         case ENOTSOCK:          return WSAENOTSOCK;
221         case EDESTADDRREQ:      return WSAEDESTADDRREQ;
222         case EMSGSIZE:          return WSAEMSGSIZE;
223         case EPROTOTYPE:        return WSAEPROTOTYPE;
224         case ENOPROTOOPT:       return WSAENOPROTOOPT;
225         case EPROTONOSUPPORT:   return WSAEPROTONOSUPPORT;
226         case ESOCKTNOSUPPORT:   return WSAESOCKTNOSUPPORT;
227         case EOPNOTSUPP:        return WSAEOPNOTSUPP;
228         case EPFNOSUPPORT:      return WSAEPFNOSUPPORT;
229         case EAFNOSUPPORT:      return WSAEAFNOSUPPORT;
230         case EADDRINUSE:        return WSAEADDRINUSE;
231         case EADDRNOTAVAIL:     return WSAEADDRNOTAVAIL;
232         case ENETDOWN:          return WSAENETDOWN;
233         case ENETUNREACH:       return WSAENETUNREACH;
234         case ENETRESET:         return WSAENETRESET;
235         case ECONNABORTED:      return WSAECONNABORTED;
236         case EPIPE:
237         case ECONNRESET:        return WSAECONNRESET;
238         case ENOBUFS:           return WSAENOBUFS;
239         case EISCONN:           return WSAEISCONN;
240         case ENOTCONN:          return WSAENOTCONN;
241         case ESHUTDOWN:         return WSAESHUTDOWN;
242         case ETOOMANYREFS:      return WSAETOOMANYREFS;
243         case ETIMEDOUT:         return WSAETIMEDOUT;
244         case ECONNREFUSED:      return WSAECONNREFUSED;
245         case ELOOP:             return WSAELOOP;
246         case ENAMETOOLONG:      return WSAENAMETOOLONG;
247         case EHOSTDOWN:         return WSAEHOSTDOWN;
248         case EHOSTUNREACH:      return WSAEHOSTUNREACH;
249         case ENOTEMPTY:         return WSAENOTEMPTY;
250 #ifdef EPROCLIM
251         case EPROCLIM:          return WSAEPROCLIM;
252 #endif
253 #ifdef EUSERS
254         case EUSERS:            return WSAEUSERS;
255 #endif
256 #ifdef EDQUOT
257         case EDQUOT:            return WSAEDQUOT;
258 #endif
259 #ifdef ESTALE
260         case ESTALE:            return WSAESTALE;
261 #endif
262 #ifdef EREMOTE
263         case EREMOTE:           return WSAEREMOTE;
264 #endif
265     default: errno=err; perror("sock_set_error"); return WSAEFAULT;
266     }
267 }
268
269 /******************************************************************************
270  * NETCON_create
271  * Basically calls 'socket()'
272  */
273 BOOL NETCON_create(WININET_NETCONNECTION *connection, int domain,
274               int type, int protocol)
275 {
276 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
277     if (connection->useSSL)
278         return FALSE;
279 #endif
280
281     connection->socketFD = socket(domain, type, protocol);
282     if (connection->socketFD == -1)
283     {
284         INTERNET_SetLastError(sock_get_error(errno));
285         return FALSE;
286     }
287     return TRUE;
288 }
289
290 /******************************************************************************
291  * NETCON_close
292  * Basically calls 'close()' unless we should use SSL
293  */
294 BOOL NETCON_close(WININET_NETCONNECTION *connection)
295 {
296     int result;
297
298     if (!NETCON_connected(connection)) return FALSE;
299
300 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
301     if (connection->useSSL)
302     {
303         HeapFree(GetProcessHeap(),0,connection->peek_msg_mem);
304         connection->peek_msg = NULL;
305         connection->peek_msg_mem = NULL;
306
307         pSSL_shutdown(connection->ssl_s);
308         pSSL_free(connection->ssl_s);
309         connection->ssl_s = NULL;
310
311         connection->useSSL = FALSE;
312     }
313 #endif
314
315     result = closesocket(connection->socketFD);
316     connection->socketFD = -1;
317
318     if (result == -1)
319     {
320         INTERNET_SetLastError(sock_get_error(errno));
321         return FALSE;
322     }
323     return TRUE;
324 }
325 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
326 static BOOL check_hostname(X509 *cert, char *hostname)
327 {
328     /* FIXME: implement */
329     return TRUE;
330 }
331 #endif
332 /******************************************************************************
333  * NETCON_secure_connect
334  * Initiates a secure connection over an existing plaintext connection.
335  */
336 BOOL NETCON_secure_connect(WININET_NETCONNECTION *connection, LPCWSTR hostname)
337 {
338 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
339     long verify_res;
340     X509 *cert;
341     int len;
342     char *hostname_unix;
343
344     /* can't connect if we are already connected */
345     if (connection->useSSL)
346     {
347         ERR("already connected\n");
348         return FALSE;
349     }
350
351     ctx = pSSL_CTX_new(meth);
352     if (!pSSL_CTX_set_default_verify_paths(ctx))
353     {
354         ERR("SSL_CTX_set_default_verify_paths failed: %s\n",
355             pERR_error_string(pERR_get_error(), 0));
356         return FALSE;
357     }
358     connection->ssl_s = pSSL_new(ctx);
359     if (!connection->ssl_s)
360     {
361         ERR("SSL_new failed: %s\n",
362             pERR_error_string(pERR_get_error(), 0));
363         goto fail;
364     }
365
366     if (!pSSL_set_fd(connection->ssl_s, connection->socketFD))
367     {
368         ERR("SSL_set_fd failed: %s\n",
369             pERR_error_string(pERR_get_error(), 0));
370         goto fail;
371     }
372
373     if (pSSL_connect(connection->ssl_s) <= 0)
374     {
375         ERR("SSL_connect failed: %s\n",
376             pERR_error_string(pERR_get_error(), 0));
377         INTERNET_SetLastError(ERROR_INTERNET_SECURITY_CHANNEL_ERROR);
378         goto fail;
379     }
380     cert = pSSL_get_peer_certificate(connection->ssl_s);
381     if (!cert)
382     {
383         ERR("no certificate for server %s\n", debugstr_w(hostname));
384         /* FIXME: is this the best error? */
385         INTERNET_SetLastError(ERROR_INTERNET_INVALID_CA);
386         goto fail;
387     }
388     verify_res = pSSL_get_verify_result(connection->ssl_s);
389     if (verify_res != X509_V_OK)
390     {
391         ERR("couldn't verify the security of the connection, %ld\n", verify_res);
392         /* FIXME: we should set an error and return, but we only warn at
393          * the moment */
394     }
395
396     len = WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, NULL, 0, NULL, NULL);
397     hostname_unix = HeapAlloc(GetProcessHeap(), 0, len);
398     if (!hostname_unix)
399     {
400         INTERNET_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
401         goto fail;
402     }
403     WideCharToMultiByte(CP_UNIXCP, 0, hostname, -1, hostname_unix, len, NULL, NULL);
404
405     if (!check_hostname(cert, hostname_unix))
406     {
407         HeapFree(GetProcessHeap(), 0, hostname_unix);
408         INTERNET_SetLastError(ERROR_INTERNET_SEC_CERT_CN_INVALID);
409         goto fail;
410     }
411
412     HeapFree(GetProcessHeap(), 0, hostname_unix);
413     connection->useSSL = TRUE;
414     return TRUE;
415
416 fail:
417     if (connection->ssl_s)
418     {
419         pSSL_shutdown(connection->ssl_s);
420         pSSL_free(connection->ssl_s);
421         connection->ssl_s = NULL;
422     }
423 #endif
424     return FALSE;
425 }
426
427 /******************************************************************************
428  * NETCON_connect
429  * Connects to the specified address.
430  */
431 BOOL NETCON_connect(WININET_NETCONNECTION *connection, const struct sockaddr *serv_addr,
432                     unsigned int addrlen)
433 {
434     int result;
435
436     if (!NETCON_connected(connection)) return FALSE;
437
438     result = connect(connection->socketFD, serv_addr, addrlen);
439     if (result == -1)
440     {
441         WARN("Unable to connect to host (%s)\n", strerror(errno));
442         INTERNET_SetLastError(sock_get_error(errno));
443
444         closesocket(connection->socketFD);
445         connection->socketFD = -1;
446         return FALSE;
447     }
448
449     return TRUE;
450 }
451
452 /******************************************************************************
453  * NETCON_send
454  * Basically calls 'send()' unless we should use SSL
455  * number of chars send is put in *sent
456  */
457 BOOL NETCON_send(WININET_NETCONNECTION *connection, const void *msg, size_t len, int flags,
458                 int *sent /* out */)
459 {
460     if (!NETCON_connected(connection)) return FALSE;
461     if (!connection->useSSL)
462     {
463         *sent = send(connection->socketFD, msg, len, flags);
464         if (*sent == -1)
465         {
466             INTERNET_SetLastError(sock_get_error(errno));
467             return FALSE;
468         }
469         return TRUE;
470     }
471     else
472     {
473 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
474         if (flags)
475             FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
476         *sent = pSSL_write(connection->ssl_s, msg, len);
477         if (*sent < 1 && len)
478             return FALSE;
479         return TRUE;
480 #else
481         return FALSE;
482 #endif
483     }
484 }
485
486 /******************************************************************************
487  * NETCON_recv
488  * Basically calls 'recv()' unless we should use SSL
489  * number of chars received is put in *recvd
490  */
491 BOOL NETCON_recv(WININET_NETCONNECTION *connection, void *buf, size_t len, int flags,
492                 int *recvd /* out */)
493 {
494     if (!NETCON_connected(connection)) return FALSE;
495     if (!connection->useSSL)
496     {
497         *recvd = recv(connection->socketFD, buf, len, flags);
498         if (*recvd == -1)
499         {
500             INTERNET_SetLastError(sock_get_error(errno));
501             return FALSE;
502         }
503         return TRUE;
504     }
505     else
506     {
507 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
508         if (flags & (~MSG_PEEK))
509             FIXME("SSL_read does not support the following flag: %08x\n", flags);
510
511         /* this ugly hack is all for MSG_PEEK. eww gross */
512         if (flags & MSG_PEEK && !connection->peek_msg)
513         {
514             connection->peek_msg = connection->peek_msg_mem = HeapAlloc(GetProcessHeap(), 0, (sizeof(char) * len) + 1);
515         }
516         else if (flags & MSG_PEEK && connection->peek_msg)
517         {
518             size_t peek_msg_len = strlen(connection->peek_msg);
519             if (len < peek_msg_len)
520                 FIXME("buffer isn't big enough. Do the expect us to wrap?\n");
521             memcpy(buf, connection->peek_msg, min(len,peek_msg_len+1));
522             *recvd = min(len, peek_msg_len);
523             return TRUE;
524         }
525         else if (connection->peek_msg)
526         {
527             size_t peek_msg_len = strlen(connection->peek_msg);
528             memcpy(buf, connection->peek_msg, min(len,peek_msg_len+1));
529             connection->peek_msg += *recvd = min(len, peek_msg_len);
530             if (*connection->peek_msg == '\0' || *(connection->peek_msg - 1) == '\0')
531             {
532                 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
533                 connection->peek_msg_mem = NULL;
534                 connection->peek_msg = NULL;
535             }
536             return TRUE;
537         }
538         *recvd = pSSL_read(connection->ssl_s, buf, len);
539         if (flags & MSG_PEEK) /* must copy stuff into buffer */
540         {
541             if (!*recvd)
542             {
543                 HeapFree(GetProcessHeap(), 0, connection->peek_msg_mem);
544                 connection->peek_msg_mem = NULL;
545                 connection->peek_msg = NULL;
546             }
547             else
548             {
549                 memcpy(connection->peek_msg, buf, *recvd);
550                 connection->peek_msg[*recvd] = '\0';
551             }
552         }
553         if (*recvd < 1 && len)
554             return FALSE;
555         return TRUE;
556 #else
557         return FALSE;
558 #endif
559     }
560 }
561
562 /******************************************************************************
563  * NETCON_getNextLine
564  */
565 BOOL NETCON_getNextLine(WININET_NETCONNECTION *connection, LPSTR lpszBuffer, LPDWORD dwBuffer)
566 {
567
568     TRACE("\n");
569
570     if (!NETCON_connected(connection)) return FALSE;
571
572     if (!connection->useSSL)
573     {
574         struct timeval tv;
575         fd_set infd;
576         BOOL bSuccess = FALSE;
577         DWORD nRecv = 0;
578
579         FD_ZERO(&infd);
580         FD_SET(connection->socketFD, &infd);
581         tv.tv_sec=RESPONSE_TIMEOUT;
582         tv.tv_usec=0;
583
584         while (nRecv < *dwBuffer)
585         {
586             if (select(connection->socketFD+1,&infd,NULL,NULL,&tv) > 0)
587             {
588                 if (recv(connection->socketFD, &lpszBuffer[nRecv], 1, 0) <= 0)
589                 {
590                     INTERNET_SetLastError(sock_get_error(errno));
591                     goto lend;
592                 }
593
594                 if (lpszBuffer[nRecv] == '\n')
595                 {
596                     bSuccess = TRUE;
597                     break;
598                 }
599                 if (lpszBuffer[nRecv] != '\r')
600                     nRecv++;
601             }
602             else
603             {
604                 INTERNET_SetLastError(ERROR_INTERNET_TIMEOUT);
605                 goto lend;
606             }
607         }
608
609     lend:             /* FIXME: don't use labels */
610         if (bSuccess)
611         {
612             lpszBuffer[nRecv++] = '\0';
613             *dwBuffer = nRecv;
614             TRACE(":%lu %s\n", nRecv, lpszBuffer);
615             return TRUE;
616         }
617         else
618         {
619             return FALSE;
620         }
621     }
622     else
623     {
624 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
625         long prev_timeout;
626         DWORD nRecv = 0;
627         BOOL success = TRUE;
628
629         prev_timeout = pSSL_CTX_get_timeout(ctx);
630         pSSL_CTX_set_timeout(ctx, RESPONSE_TIMEOUT);
631
632         while (nRecv < *dwBuffer)
633         {
634             int recv = 1;
635             if (!NETCON_recv(connection, &lpszBuffer[nRecv], 1, 0, &recv))
636             {
637                 INTERNET_SetLastError(ERROR_CONNECTION_ABORTED);
638                 success = FALSE;
639             }
640
641             if (lpszBuffer[nRecv] == '\n')
642             {
643                 success = TRUE;
644                 break;
645             }
646             if (lpszBuffer[nRecv] != '\r')
647                 nRecv++;
648         }
649
650         pSSL_CTX_set_timeout(ctx, prev_timeout);
651         if (success)
652         {
653             lpszBuffer[nRecv++] = '\0';
654             *dwBuffer = nRecv;
655             TRACE("_SSL:%lu %s\n", nRecv, lpszBuffer);
656             return TRUE;
657         }
658         return FALSE;
659 #else
660         return FALSE;
661 #endif
662     }
663 }
664
665
666 LPCVOID NETCON_GetCert(WININET_NETCONNECTION *connection)
667 {
668
669 #if defined HAVE_OPENSSL_SSL_H && defined HAVE_OPENSSL_ERR_H
670     X509* cert;
671     unsigned char* buffer,*p;
672     INT len;
673     BOOL malloced = FALSE;
674     LPCVOID r = NULL;
675
676     if (!connection->useSSL)
677         return NULL;
678
679     cert = pSSL_get_peer_certificate(connection->ssl_s);
680     p = NULL;
681     len = pi2d_X509(cert,&p);
682     /*
683      * SSL 0.9.7 and above malloc the buffer if it is null. 
684      * however earlier version do not and so we would need to alloc the buffer.
685      *
686      * see the i2d_X509 man page for more details.
687      */
688     if (!p)
689     {
690         buffer = HeapAlloc(GetProcessHeap(),0,len);
691         p = buffer;
692         len = pi2d_X509(cert,&p);
693     }
694     else
695     {
696         buffer = p;
697         malloced = TRUE;
698     }
699
700     r = CertCreateCertificateContext(X509_ASN_ENCODING,buffer,len);
701
702     if (malloced)
703         free(buffer);
704     else
705         HeapFree(GetProcessHeap(),0,buffer);
706
707     return r;
708 #else
709     return NULL;
710 #endif
711 }