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