xref: /freebsd/contrib/ntp/include/ssl_applink.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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
30 static void *wrap_dbg_malloc(size_t s, const char *f, int l);
31 static void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l);
32 static void wrap_dbg_free(void *p);
33 static void wrap_dbg_free_ex(void *p, const char *f, int l);
34 #endif
35 
36 
37 #if defined(OPENSSL) && defined(SYS_WINNT)
38 
39 void ssl_applink(void);
40 
41 void
42 ssl_applink(void)
43 {
44 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
45 
46 #   ifdef WRAP_DBG_MALLOC
47 	CRYPTO_set_mem_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free_ex);
48 #   else
49 	OPENSSL_malloc_init();
50 #   endif
51 
52 #  else
53 
54 #   ifdef WRAP_DBG_MALLOC
55 	CRYPTO_set_mem_ex_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free);
56 #   else
57 	CRYPTO_malloc_init();
58 #   endif
59 
60 #endif /* OpenSSL version cascade */
61 }
62 #else	/* !OPENSSL || !SYS_WINNT */
63 #define ssl_applink()	do {} while (0)
64 #endif
65 
66 
67 #ifdef WRAP_DBG_MALLOC
68 /*
69  * OpenSSL malloc overriding uses different parameters
70  * for DEBUG malloc/realloc/free (lacking block type).
71  * Simple wrappers convert.
72  */
73 static void *wrap_dbg_malloc(size_t s, const char *f, int l)
74 {
75 	void *ret;
76 
77 	ret = _malloc_dbg(s, _NORMAL_BLOCK, f, l);
78 	return ret;
79 }
80 
81 static void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l)
82 {
83 	void *ret;
84 
85 	ret = _realloc_dbg(p, s, _NORMAL_BLOCK, f, l);
86 	return ret;
87 }
88 
89 static void wrap_dbg_free(void *p)
90 {
91 	_free_dbg(p, _NORMAL_BLOCK);
92 }
93 
94 static void wrap_dbg_free_ex(void *p, const char *f, int l)
95 {
96 	(void)f;
97 	(void)l;
98 	_free_dbg(p, _NORMAL_BLOCK);
99 }
100 #endif	/* WRAP_DBG_MALLOC */
101