xref: /netbsd-src/external/bsd/ntp/dist/include/ssl_applink.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /*	$NetBSD: ssl_applink.c,v 1.5 2017/04/13 20:17:41 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 void *wrap_dbg_malloc(size_t s, const char *f, int l);
33 void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l);
34 void wrap_dbg_free(void *p);
35 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 #   ifdef WRAP_DBG_MALLOC
48 	CRYPTO_set_mem_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free_ex);
49 #   else
50 	OPENSSL_malloc_init();
51 #   endif
52 #else
53 #   ifdef WRAP_DBG_MALLOC
54 	CRYPTO_set_mem_ex_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free);
55 #   else
56 	CRYPTO_malloc_init();
57 #   endif
58 #endif /* OpenSSL version cascade */
59 }
60 #else	/* !OPENSSL || !SYS_WINNT */
61 #define ssl_applink()	do {} while (0)
62 #endif
63 
64 
65 #ifdef WRAP_DBG_MALLOC
66 /*
67  * OpenSSL malloc overriding uses different parameters
68  * for DEBUG malloc/realloc/free (lacking block type).
69  * Simple wrappers convert.
70  */
71 void *wrap_dbg_malloc(size_t s, const char *f, int l)
72 {
73 	void *ret;
74 
75 	ret = _malloc_dbg(s, _NORMAL_BLOCK, f, l);
76 	return ret;
77 }
78 
79 void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l)
80 {
81 	void *ret;
82 
83 	ret = _realloc_dbg(p, s, _NORMAL_BLOCK, f, l);
84 	return ret;
85 }
86 
87 void wrap_dbg_free(void *p)
88 {
89 	_free_dbg(p, _NORMAL_BLOCK);
90 }
91 
92 void wrap_dbg_free_ex(void *p, const char *f, int l)
93 {
94 	(void)f;
95 	(void)l;
96 	_free_dbg(p, _NORMAL_BLOCK);
97 }
98 #endif	/* WRAP_DBG_MALLOC */
99