ssl_applink.c revision 316069
1/*
2 * include/ssl_applink.c -- common NTP code for openssl/applink.c
3 *
4 * Each program which uses OpenSSL should include this file in _one_
5 * of its source files and call ssl_applink() before any OpenSSL
6 * functions.
7 */
8
9#if defined(OPENSSL) && defined(SYS_WINNT)
10# ifdef _MSC_VER
11#  pragma warning(push)
12#  pragma warning(disable: 4152)
13#  ifndef OPENSSL_NO_AUTOLINK
14#   include "msvc_ssl_autolib.h"
15#  endif
16# endif
17# if OPENSSL_VERSION_NUMBER < 0x10100000L
18#  include <openssl/applink.c>
19# endif
20# ifdef _MSC_VER
21#  pragma warning(pop)
22# endif
23#endif
24
25#if defined(OPENSSL) && defined(_MSC_VER) && defined(_DEBUG)
26#define WRAP_DBG_MALLOC
27#endif
28
29#ifdef WRAP_DBG_MALLOC
30void *wrap_dbg_malloc(size_t s, const char *f, int l);
31void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l);
32void wrap_dbg_free(void *p);
33void wrap_dbg_free_ex(void *p, const char *f, int l);
34#endif
35
36
37#if defined(OPENSSL) && defined(SYS_WINNT)
38
39void ssl_applink(void);
40
41void
42ssl_applink(void)
43{
44#if OPENSSL_VERSION_NUMBER >= 0x10100000L
45#   ifdef WRAP_DBG_MALLOC
46	CRYPTO_set_mem_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free_ex);
47#   else
48	OPENSSL_malloc_init();
49#   endif
50#else
51#   ifdef WRAP_DBG_MALLOC
52	CRYPTO_set_mem_ex_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free);
53#   else
54	CRYPTO_malloc_init();
55#   endif
56#endif /* OpenSSL version cascade */
57}
58#else	/* !OPENSSL || !SYS_WINNT */
59#define ssl_applink()	do {} while (0)
60#endif
61
62
63#ifdef WRAP_DBG_MALLOC
64/*
65 * OpenSSL malloc overriding uses different parameters
66 * for DEBUG malloc/realloc/free (lacking block type).
67 * Simple wrappers convert.
68 */
69void *wrap_dbg_malloc(size_t s, const char *f, int l)
70{
71	void *ret;
72
73	ret = _malloc_dbg(s, _NORMAL_BLOCK, f, l);
74	return ret;
75}
76
77void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l)
78{
79	void *ret;
80
81	ret = _realloc_dbg(p, s, _NORMAL_BLOCK, f, l);
82	return ret;
83}
84
85void wrap_dbg_free(void *p)
86{
87	_free_dbg(p, _NORMAL_BLOCK);
88}
89
90void wrap_dbg_free_ex(void *p, const char *f, int l)
91{
92	(void)f;
93	(void)l;
94	_free_dbg(p, _NORMAL_BLOCK);
95}
96#endif	/* WRAP_DBG_MALLOC */
97