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