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