winhttp: Protect OpenSSL initialization with critical section.
[wine/hacks.git] / dlls / winhttp / net.c
bloba824271dad528578e8a6fca890093fe1ce8971ad
1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
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.
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.
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
19 #include "config.h"
20 #include "wine/port.h"
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <errno.h>
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
45 #include "wine/debug.h"
46 #include "wine/library.h"
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winhttp.h"
51 #include "wincrypt.h"
53 #include "winhttp_private.h"
55 /* to avoid conflicts with the Unix socket headers */
56 #define USE_WS_PREFIX
57 #include "winsock2.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
61 #ifndef HAVE_GETADDRINFO
63 /* critical section to protect non-reentrant gethostbyname() */
64 static CRITICAL_SECTION cs_gethostbyname;
65 static CRITICAL_SECTION_DEBUG critsect_debug =
67 0, 0, &cs_gethostbyname,
68 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
69 0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
71 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
73 #endif
75 #ifdef SONAME_LIBSSL
77 #include <openssl/err.h>
79 static CRITICAL_SECTION init_ssl_cs;
80 static CRITICAL_SECTION_DEBUG init_ssl_cs_debug =
82 0, 0, &init_ssl_cs,
83 { &init_ssl_cs_debug.ProcessLocksList,
84 &init_ssl_cs_debug.ProcessLocksList },
85 0, 0, { (DWORD_PTR)(__FILE__ ": init_ssl_cs") }
87 static CRITICAL_SECTION init_ssl_cs = { &init_ssl_cs_debug, -1, 0, 0, 0, 0 };
89 static void *libssl_handle;
90 static void *libcrypto_handle;
92 static SSL_METHOD *method;
93 static SSL_CTX *ctx;
95 #define MAKE_FUNCPTR(f) static typeof(f) * p##f
97 MAKE_FUNCPTR( SSL_library_init );
98 MAKE_FUNCPTR( SSL_load_error_strings );
99 MAKE_FUNCPTR( SSLv23_method );
100 MAKE_FUNCPTR( SSL_CTX_new );
101 MAKE_FUNCPTR( SSL_new );
102 MAKE_FUNCPTR( SSL_free );
103 MAKE_FUNCPTR( SSL_set_fd );
104 MAKE_FUNCPTR( SSL_connect );
105 MAKE_FUNCPTR( SSL_shutdown );
106 MAKE_FUNCPTR( SSL_write );
107 MAKE_FUNCPTR( SSL_read );
108 MAKE_FUNCPTR( SSL_get_verify_result );
109 MAKE_FUNCPTR( SSL_get_peer_certificate );
110 MAKE_FUNCPTR( SSL_CTX_set_default_verify_paths );
112 MAKE_FUNCPTR( BIO_new_fp );
113 MAKE_FUNCPTR( ERR_get_error );
114 MAKE_FUNCPTR( ERR_error_string );
115 MAKE_FUNCPTR( i2d_X509 );
116 #undef MAKE_FUNCPTR
118 #endif
120 /* translate a unix error code into a winsock error code */
121 static int sock_get_error( int err )
123 #if !defined(__MINGW32__) && !defined (_MSC_VER)
124 switch (err)
126 case EINTR: return WSAEINTR;
127 case EBADF: return WSAEBADF;
128 case EPERM:
129 case EACCES: return WSAEACCES;
130 case EFAULT: return WSAEFAULT;
131 case EINVAL: return WSAEINVAL;
132 case EMFILE: return WSAEMFILE;
133 case EWOULDBLOCK: return WSAEWOULDBLOCK;
134 case EINPROGRESS: return WSAEINPROGRESS;
135 case EALREADY: return WSAEALREADY;
136 case ENOTSOCK: return WSAENOTSOCK;
137 case EDESTADDRREQ: return WSAEDESTADDRREQ;
138 case EMSGSIZE: return WSAEMSGSIZE;
139 case EPROTOTYPE: return WSAEPROTOTYPE;
140 case ENOPROTOOPT: return WSAENOPROTOOPT;
141 case EPROTONOSUPPORT: return WSAEPROTONOSUPPORT;
142 case ESOCKTNOSUPPORT: return WSAESOCKTNOSUPPORT;
143 case EOPNOTSUPP: return WSAEOPNOTSUPP;
144 case EPFNOSUPPORT: return WSAEPFNOSUPPORT;
145 case EAFNOSUPPORT: return WSAEAFNOSUPPORT;
146 case EADDRINUSE: return WSAEADDRINUSE;
147 case EADDRNOTAVAIL: return WSAEADDRNOTAVAIL;
148 case ENETDOWN: return WSAENETDOWN;
149 case ENETUNREACH: return WSAENETUNREACH;
150 case ENETRESET: return WSAENETRESET;
151 case ECONNABORTED: return WSAECONNABORTED;
152 case EPIPE:
153 case ECONNRESET: return WSAECONNRESET;
154 case ENOBUFS: return WSAENOBUFS;
155 case EISCONN: return WSAEISCONN;
156 case ENOTCONN: return WSAENOTCONN;
157 case ESHUTDOWN: return WSAESHUTDOWN;
158 case ETOOMANYREFS: return WSAETOOMANYREFS;
159 case ETIMEDOUT: return WSAETIMEDOUT;
160 case ECONNREFUSED: return WSAECONNREFUSED;
161 case ELOOP: return WSAELOOP;
162 case ENAMETOOLONG: return WSAENAMETOOLONG;
163 case EHOSTDOWN: return WSAEHOSTDOWN;
164 case EHOSTUNREACH: return WSAEHOSTUNREACH;
165 case ENOTEMPTY: return WSAENOTEMPTY;
166 #ifdef EPROCLIM
167 case EPROCLIM: return WSAEPROCLIM;
168 #endif
169 #ifdef EUSERS
170 case EUSERS: return WSAEUSERS;
171 #endif
172 #ifdef EDQUOT
173 case EDQUOT: return WSAEDQUOT;
174 #endif
175 #ifdef ESTALE
176 case ESTALE: return WSAESTALE;
177 #endif
178 #ifdef EREMOTE
179 case EREMOTE: return WSAEREMOTE;
180 #endif
181 default: errno = err; perror( "sock_set_error" ); return WSAEFAULT;
183 #endif
184 return err;
187 BOOL netconn_init( netconn_t *conn, BOOL secure )
189 conn->socket = -1;
190 if (!secure) return TRUE;
192 #if defined(SONAME_LIBSSL) && defined(SONAME_LIBCRYPTO)
193 EnterCriticalSection( &init_ssl_cs );
194 if (libssl_handle)
196 LeaveCriticalSection( &init_ssl_cs );
197 return TRUE;
199 if (!(libssl_handle = wine_dlopen( SONAME_LIBSSL, RTLD_NOW, NULL, 0 )))
201 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBSSL);
202 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
203 LeaveCriticalSection( &init_ssl_cs );
204 return FALSE;
206 if (!(libcrypto_handle = wine_dlopen( SONAME_LIBCRYPTO, RTLD_NOW, NULL, 0 )))
208 ERR("Trying to use SSL but couldn't load %s. Expect trouble.\n", SONAME_LIBCRYPTO);
209 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
210 LeaveCriticalSection( &init_ssl_cs );
211 return FALSE;
213 #define LOAD_FUNCPTR(x) \
214 if (!(p##x = wine_dlsym( libssl_handle, #x, NULL, 0 ))) \
216 ERR("Failed to load symbol %s\n", #x); \
217 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
218 LeaveCriticalSection( &init_ssl_cs ); \
219 return FALSE; \
221 LOAD_FUNCPTR( SSL_library_init );
222 LOAD_FUNCPTR( SSL_load_error_strings );
223 LOAD_FUNCPTR( SSLv23_method );
224 LOAD_FUNCPTR( SSL_CTX_new );
225 LOAD_FUNCPTR( SSL_new );
226 LOAD_FUNCPTR( SSL_free );
227 LOAD_FUNCPTR( SSL_set_fd );
228 LOAD_FUNCPTR( SSL_connect );
229 LOAD_FUNCPTR( SSL_shutdown );
230 LOAD_FUNCPTR( SSL_write );
231 LOAD_FUNCPTR( SSL_read );
232 LOAD_FUNCPTR( SSL_get_verify_result );
233 LOAD_FUNCPTR( SSL_get_peer_certificate );
234 LOAD_FUNCPTR( SSL_CTX_set_default_verify_paths );
235 #undef LOAD_FUNCPTR
237 #define LOAD_FUNCPTR(x) \
238 if (!(p##x = wine_dlsym( libcrypto_handle, #x, NULL, 0 ))) \
240 ERR("Failed to load symbol %s\n", #x); \
241 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR ); \
242 LeaveCriticalSection( &init_ssl_cs ); \
243 return FALSE; \
245 LOAD_FUNCPTR( BIO_new_fp );
246 LOAD_FUNCPTR( ERR_get_error );
247 LOAD_FUNCPTR( ERR_error_string );
248 LOAD_FUNCPTR( i2d_X509 );
249 #undef LOAD_FUNCPTR
251 pSSL_library_init();
252 pSSL_load_error_strings();
253 pBIO_new_fp( stderr, BIO_NOCLOSE );
255 method = pSSLv23_method();
256 ctx = pSSL_CTX_new( method );
257 if (!pSSL_CTX_set_default_verify_paths( ctx ))
259 ERR("SSL_CTX_set_default_verify_paths failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
260 set_last_error( ERROR_OUTOFMEMORY );
261 LeaveCriticalSection( &init_ssl_cs );
262 return FALSE;
264 LeaveCriticalSection( &init_ssl_cs );
265 #else
266 WARN("SSL support not compiled in.\n");
267 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
268 return FALSE;
269 #endif
270 return TRUE;
273 BOOL netconn_connected( netconn_t *conn )
275 return (conn->socket != -1);
278 BOOL netconn_create( netconn_t *conn, int domain, int type, int protocol )
280 if ((conn->socket = socket( domain, type, protocol )) == -1)
282 WARN("unable to create socket (%s)\n", strerror(errno));
283 set_last_error( sock_get_error( errno ) );
284 return FALSE;
286 return TRUE;
289 BOOL netconn_close( netconn_t *conn )
291 int res;
293 #ifdef SONAME_LIBSSL
294 if (conn->secure)
296 heap_free( conn->peek_msg_mem );
297 conn->peek_msg_mem = NULL;
298 conn->peek_msg = NULL;
299 conn->peek_len = 0;
301 pSSL_shutdown( conn->ssl_conn );
302 pSSL_free( conn->ssl_conn );
304 conn->ssl_conn = NULL;
305 conn->secure = FALSE;
307 #endif
308 res = closesocket( conn->socket );
309 conn->socket = -1;
310 if (res == -1)
312 set_last_error( sock_get_error( errno ) );
313 return FALSE;
315 return TRUE;
318 BOOL netconn_connect( netconn_t *conn, const struct sockaddr *sockaddr, unsigned int addr_len, int timeout )
320 BOOL ret = FALSE;
321 int res = 0, state;
323 if (timeout > 0)
325 state = 1;
326 ioctlsocket( conn->socket, FIONBIO, &state );
328 if (connect( conn->socket, sockaddr, addr_len ) < 0)
330 res = sock_get_error( errno );
331 if (res == WSAEWOULDBLOCK || res == WSAEINPROGRESS)
333 struct pollfd pfd;
335 pfd.fd = conn->socket;
336 pfd.events = POLLOUT;
337 if (poll( &pfd, 1, timeout ) > 0)
338 ret = TRUE;
339 else
340 res = sock_get_error( errno );
343 else
344 ret = TRUE;
345 if (timeout > 0)
347 state = 0;
348 ioctlsocket( conn->socket, FIONBIO, &state );
350 if (!ret)
352 WARN("unable to connect to host (%d)\n", res);
353 set_last_error( res );
355 return ret;
358 BOOL netconn_secure_connect( netconn_t *conn )
360 #ifdef SONAME_LIBSSL
361 X509 *cert;
362 long res;
364 if (!(conn->ssl_conn = pSSL_new( ctx )))
366 ERR("SSL_new failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
367 set_last_error( ERROR_OUTOFMEMORY );
368 goto fail;
370 if (!pSSL_set_fd( conn->ssl_conn, conn->socket ))
372 ERR("SSL_set_fd failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
373 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
374 goto fail;
376 if (pSSL_connect( conn->ssl_conn ) <= 0)
378 ERR("SSL_connect failed: %s\n", pERR_error_string( pERR_get_error(), 0 ));
379 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
380 goto fail;
382 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn )))
384 ERR("No certificate for server: %s\n", pERR_error_string( pERR_get_error(), 0 ));
385 set_last_error( ERROR_WINHTTP_SECURE_CHANNEL_ERROR );
386 goto fail;
388 if ((res = pSSL_get_verify_result( conn->ssl_conn )) != X509_V_OK)
390 /* FIXME: we should set an error and return, but we only print an error at the moment */
391 ERR("couldn't verify server certificate (%ld)\n", res);
393 TRACE("established SSL connection\n");
394 conn->secure = TRUE;
395 return TRUE;
397 fail:
398 if (conn->ssl_conn)
400 pSSL_shutdown( conn->ssl_conn );
401 pSSL_free( conn->ssl_conn );
402 conn->ssl_conn = NULL;
404 #endif
405 return FALSE;
408 BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int flags, int *sent )
410 if (!netconn_connected( conn )) return FALSE;
411 if (conn->secure)
413 #ifdef SONAME_LIBSSL
414 if (flags) FIXME("SSL_write doesn't support any flags (%08x)\n", flags);
415 *sent = pSSL_write( conn->ssl_conn, msg, len );
416 if (*sent < 1 && len) return FALSE;
417 return TRUE;
418 #else
419 return FALSE;
420 #endif
422 if ((*sent = send( conn->socket, msg, len, flags )) == -1)
424 set_last_error( sock_get_error( errno ) );
425 return FALSE;
427 return TRUE;
430 BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
432 *recvd = 0;
433 if (!netconn_connected( conn )) return FALSE;
434 if (!len) return TRUE;
436 if (conn->secure)
438 #ifdef SONAME_LIBSSL
439 if (flags & ~(MSG_PEEK | MSG_WAITALL))
440 FIXME("SSL_read does not support the following flags: %08x\n", flags);
442 /* this ugly hack is all for MSG_PEEK */
443 if (flags & MSG_PEEK && !conn->peek_msg)
445 if (!(conn->peek_msg = conn->peek_msg_mem = heap_alloc( len + 1 ))) return FALSE;
447 else if (flags & MSG_PEEK && conn->peek_msg)
449 if (len < conn->peek_len) FIXME("buffer isn't big enough, should we wrap?\n");
450 *recvd = min( len, conn->peek_len );
451 memcpy( buf, conn->peek_msg, *recvd );
452 return TRUE;
454 else if (conn->peek_msg)
456 *recvd = min( len, conn->peek_len );
457 memcpy( buf, conn->peek_msg, *recvd );
458 conn->peek_len -= *recvd;
459 conn->peek_msg += *recvd;
461 if (conn->peek_len == 0)
463 heap_free( conn->peek_msg_mem );
464 conn->peek_msg_mem = NULL;
465 conn->peek_msg = NULL;
467 /* check if we have enough data from the peek buffer */
468 if (!(flags & MSG_WAITALL) || (*recvd == len)) return TRUE;
470 *recvd += pSSL_read( conn->ssl_conn, (char *)buf + *recvd, len - *recvd );
471 if (flags & MSG_PEEK) /* must copy into buffer */
473 conn->peek_len = *recvd;
474 if (!*recvd)
476 heap_free( conn->peek_msg_mem );
477 conn->peek_msg_mem = NULL;
478 conn->peek_msg = NULL;
480 else memcpy( conn->peek_msg, buf, *recvd );
482 if (*recvd < 1 && len) return FALSE;
483 return TRUE;
484 #else
485 return FALSE;
486 #endif
488 if ((*recvd = recv( conn->socket, buf, len, flags )) == -1)
490 set_last_error( sock_get_error( errno ) );
491 return FALSE;
493 return TRUE;
496 BOOL netconn_query_data_available( netconn_t *conn, DWORD *available )
498 #ifdef FIONREAD
499 int ret, unread;
500 #endif
501 *available = 0;
502 if (!netconn_connected( conn )) return FALSE;
504 if (conn->secure)
506 #ifdef SONAME_LIBSSL
507 if (conn->peek_msg) *available = conn->peek_len;
508 #endif
509 return TRUE;
511 #ifdef FIONREAD
512 if (!(ret = ioctlsocket( conn->socket, FIONREAD, &unread ))) *available = unread;
513 #endif
514 return TRUE;
517 BOOL netconn_get_next_line( netconn_t *conn, char *buffer, DWORD *buflen )
519 struct pollfd pfd;
520 BOOL ret = FALSE;
521 DWORD recvd = 0;
523 if (!netconn_connected( conn )) return FALSE;
525 if (conn->secure)
527 #ifdef SONAME_LIBSSL
528 while (recvd < *buflen)
530 int dummy;
531 if (!netconn_recv( conn, &buffer[recvd], 1, 0, &dummy ))
533 set_last_error( ERROR_CONNECTION_ABORTED );
534 break;
536 if (buffer[recvd] == '\n')
538 ret = TRUE;
539 break;
541 if (buffer[recvd] != '\r') recvd++;
543 if (ret)
545 buffer[recvd++] = 0;
546 *buflen = recvd;
547 TRACE("received line %s\n", debugstr_a(buffer));
549 return ret;
550 #else
551 return FALSE;
552 #endif
555 pfd.fd = conn->socket;
556 pfd.events = POLLIN;
557 while (recvd < *buflen)
559 int timeout, res;
560 struct timeval tv;
561 socklen_t len = sizeof(tv);
563 if ((res = getsockopt( conn->socket, SOL_SOCKET, SO_RCVTIMEO, (void*)&tv, &len ) != -1))
564 timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000;
565 else
566 timeout = -1;
567 if (poll( &pfd, 1, timeout ) > 0)
569 if ((res = recv( conn->socket, &buffer[recvd], 1, 0 )) <= 0)
571 if (res == -1) set_last_error( sock_get_error( errno ) );
572 break;
574 if (buffer[recvd] == '\n')
576 ret = TRUE;
577 break;
579 if (buffer[recvd] != '\r') recvd++;
581 else
583 set_last_error( ERROR_WINHTTP_TIMEOUT );
584 break;
587 if (ret)
589 buffer[recvd++] = 0;
590 *buflen = recvd;
591 TRACE("received line %s\n", debugstr_a(buffer));
593 return ret;
596 DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
598 int res;
599 struct timeval tv;
601 /* value is in milliseconds, convert to struct timeval */
602 tv.tv_sec = value / 1000;
603 tv.tv_usec = (value % 1000) * 1000;
605 if ((res = setsockopt( netconn->socket, SOL_SOCKET, send ? SO_SNDTIMEO : SO_RCVTIMEO, (void*)&tv, sizeof(tv) ) == -1))
607 WARN("setsockopt failed (%s)\n", strerror( errno ));
608 return sock_get_error( errno );
610 return ERROR_SUCCESS;
613 BOOL netconn_resolve( WCHAR *hostnameW, INTERNET_PORT port, struct sockaddr *sa, socklen_t *sa_len )
615 char *hostname;
616 #ifdef HAVE_GETADDRINFO
617 struct addrinfo *res, hints;
618 int ret;
619 #else
620 struct hostent *he;
621 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
622 #endif
624 if (!(hostname = strdupWA( hostnameW ))) return FALSE;
626 #ifdef HAVE_GETADDRINFO
627 memset( &hints, 0, sizeof(struct addrinfo) );
628 /* Prefer IPv4 to IPv6 addresses, since some web servers do not listen on
629 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
631 hints.ai_family = AF_INET;
633 ret = getaddrinfo( hostname, NULL, &hints, &res );
634 if (ret != 0)
636 TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(hostnameW), gai_strerror(ret));
637 hints.ai_family = AF_INET6;
638 ret = getaddrinfo( hostname, NULL, &hints, &res );
639 if (ret != 0)
641 TRACE("failed to get address of %s (%s)\n", debugstr_w(hostnameW), gai_strerror(ret));
642 heap_free( hostname );
643 return FALSE;
646 heap_free( hostname );
647 if (*sa_len < res->ai_addrlen)
649 WARN("address too small\n");
650 freeaddrinfo( res );
651 return FALSE;
653 *sa_len = res->ai_addrlen;
654 memcpy( sa, res->ai_addr, res->ai_addrlen );
655 /* Copy port */
656 switch (res->ai_family)
658 case AF_INET:
659 ((struct sockaddr_in *)sa)->sin_port = htons( port );
660 break;
661 case AF_INET6:
662 ((struct sockaddr_in6 *)sa)->sin6_port = htons( port );
663 break;
666 freeaddrinfo( res );
667 return TRUE;
668 #else
669 EnterCriticalSection( &cs_gethostbyname );
671 he = gethostbyname( hostname );
672 heap_free( hostname );
673 if (!he)
675 TRACE("failed to get address of %s (%d)\n", debugstr_w(hostnameW), h_errno);
676 LeaveCriticalSection( &cs_gethostbyname );
677 return FALSE;
679 if (*sa_len < sizeof(struct sockaddr_in))
681 WARN("address too small\n");
682 LeaveCriticalSection( &cs_gethostbyname );
683 return FALSE;
685 *sa_len = sizeof(struct sockaddr_in);
686 memset( sa, 0, sizeof(struct sockaddr_in) );
687 memcpy( &sin->sin_addr, he->h_addr, he->h_length );
688 sin->sin_family = he->h_addrtype;
689 sin->sin_port = htons( port );
691 LeaveCriticalSection( &cs_gethostbyname );
692 return TRUE;
693 #endif
696 const void *netconn_get_certificate( netconn_t *conn )
698 #ifdef SONAME_LIBSSL
699 X509 *cert;
700 unsigned char *buffer, *p;
701 int len;
702 BOOL malloc = FALSE;
703 const CERT_CONTEXT *ret;
705 if (!conn->secure) return NULL;
707 if (!(cert = pSSL_get_peer_certificate( conn->ssl_conn ))) return NULL;
708 p = NULL;
709 if ((len = pi2d_X509( cert, &p )) < 0) return NULL;
711 * SSL 0.9.7 and above malloc the buffer if it is null.
712 * however earlier version do not and so we would need to alloc the buffer.
714 * see the i2d_X509 man page for more details.
716 if (!p)
718 if (!(buffer = heap_alloc( len ))) return NULL;
719 p = buffer;
720 len = pi2d_X509( cert, &p );
722 else
724 buffer = p;
725 malloc = TRUE;
728 ret = CertCreateCertificateContext( X509_ASN_ENCODING, buffer, len );
730 if (malloc) free( buffer );
731 else heap_free( buffer );
733 return ret;
734 #else
735 return NULL;
736 #endif