xref: /freebsd/contrib/ntp/include/ssl_applink.c (revision 091002585974d17c9533f943ec351c13a69788ab)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * include/ssl_applink.c -- common NTP code for openssl/applink.c
32b15cb3dSCy Schubert  *
42b15cb3dSCy Schubert  * Each program which uses OpenSSL should include this file in _one_
52b15cb3dSCy Schubert  * of its source files and call ssl_applink() before any OpenSSL
62b15cb3dSCy Schubert  * functions.
72b15cb3dSCy Schubert  */
82b15cb3dSCy Schubert 
92b15cb3dSCy Schubert #if defined(OPENSSL) && defined(SYS_WINNT)
102b15cb3dSCy Schubert # ifdef _MSC_VER
112b15cb3dSCy Schubert #  pragma warning(push)
122b15cb3dSCy Schubert #  pragma warning(disable: 4152)
13f0574f5cSXin LI #  ifndef OPENSSL_NO_AUTOLINK
14f0574f5cSXin LI #   include "msvc_ssl_autolib.h"
152b15cb3dSCy Schubert #  endif
16f0574f5cSXin LI # endif
17f0574f5cSXin LI # if OPENSSL_VERSION_NUMBER < 0x10100000L
182b15cb3dSCy Schubert #  include <openssl/applink.c>
19f0574f5cSXin LI # endif
202b15cb3dSCy Schubert # ifdef _MSC_VER
212b15cb3dSCy Schubert #  pragma warning(pop)
222b15cb3dSCy Schubert # endif
232b15cb3dSCy Schubert #endif
242b15cb3dSCy Schubert 
252b15cb3dSCy Schubert #if defined(OPENSSL) && defined(_MSC_VER) && defined(_DEBUG)
262b15cb3dSCy Schubert #define WRAP_DBG_MALLOC
272b15cb3dSCy Schubert #endif
282b15cb3dSCy Schubert 
292b15cb3dSCy Schubert #ifdef WRAP_DBG_MALLOC
30*09100258SXin LI static void *wrap_dbg_malloc(size_t s, const char *f, int l);
31*09100258SXin LI static void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l);
32*09100258SXin LI static void wrap_dbg_free(void *p);
33*09100258SXin LI static void wrap_dbg_free_ex(void *p, const char *f, int l);
342b15cb3dSCy Schubert #endif
352b15cb3dSCy Schubert 
362b15cb3dSCy Schubert 
372b15cb3dSCy Schubert #if defined(OPENSSL) && defined(SYS_WINNT)
38f0574f5cSXin LI 
392b15cb3dSCy Schubert void ssl_applink(void);
402b15cb3dSCy Schubert 
412b15cb3dSCy Schubert void
422b15cb3dSCy Schubert ssl_applink(void)
432b15cb3dSCy Schubert {
44f0574f5cSXin LI #if OPENSSL_VERSION_NUMBER >= 0x10100000L
45*09100258SXin LI 
46f0574f5cSXin LI #   ifdef WRAP_DBG_MALLOC
47f0574f5cSXin LI 	CRYPTO_set_mem_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free_ex);
48f0574f5cSXin LI #   else
49f0574f5cSXin LI 	OPENSSL_malloc_init();
50f0574f5cSXin LI #   endif
51*09100258SXin LI 
52f0574f5cSXin LI #  else
53*09100258SXin LI 
542b15cb3dSCy Schubert #   ifdef WRAP_DBG_MALLOC
552b15cb3dSCy Schubert 	CRYPTO_set_mem_ex_functions(wrap_dbg_malloc, wrap_dbg_realloc, wrap_dbg_free);
562b15cb3dSCy Schubert #   else
572b15cb3dSCy Schubert 	CRYPTO_malloc_init();
582b15cb3dSCy Schubert #   endif
59*09100258SXin LI 
60f0574f5cSXin LI #endif /* OpenSSL version cascade */
612b15cb3dSCy Schubert }
622b15cb3dSCy Schubert #else	/* !OPENSSL || !SYS_WINNT */
632b15cb3dSCy Schubert #define ssl_applink()	do {} while (0)
642b15cb3dSCy Schubert #endif
652b15cb3dSCy Schubert 
662b15cb3dSCy Schubert 
672b15cb3dSCy Schubert #ifdef WRAP_DBG_MALLOC
682b15cb3dSCy Schubert /*
692b15cb3dSCy Schubert  * OpenSSL malloc overriding uses different parameters
702b15cb3dSCy Schubert  * for DEBUG malloc/realloc/free (lacking block type).
712b15cb3dSCy Schubert  * Simple wrappers convert.
722b15cb3dSCy Schubert  */
73*09100258SXin LI static void *wrap_dbg_malloc(size_t s, const char *f, int l)
742b15cb3dSCy Schubert {
752b15cb3dSCy Schubert 	void *ret;
762b15cb3dSCy Schubert 
772b15cb3dSCy Schubert 	ret = _malloc_dbg(s, _NORMAL_BLOCK, f, l);
782b15cb3dSCy Schubert 	return ret;
792b15cb3dSCy Schubert }
802b15cb3dSCy Schubert 
81*09100258SXin LI static void *wrap_dbg_realloc(void *p, size_t s, const char *f, int l)
822b15cb3dSCy Schubert {
832b15cb3dSCy Schubert 	void *ret;
842b15cb3dSCy Schubert 
852b15cb3dSCy Schubert 	ret = _realloc_dbg(p, s, _NORMAL_BLOCK, f, l);
862b15cb3dSCy Schubert 	return ret;
872b15cb3dSCy Schubert }
882b15cb3dSCy Schubert 
89*09100258SXin LI static void wrap_dbg_free(void *p)
902b15cb3dSCy Schubert {
912b15cb3dSCy Schubert 	_free_dbg(p, _NORMAL_BLOCK);
922b15cb3dSCy Schubert }
93f0574f5cSXin LI 
94*09100258SXin LI static void wrap_dbg_free_ex(void *p, const char *f, int l)
95f0574f5cSXin LI {
96f0574f5cSXin LI 	(void)f;
97f0574f5cSXin LI 	(void)l;
98f0574f5cSXin LI 	_free_dbg(p, _NORMAL_BLOCK);
99f0574f5cSXin LI }
1002b15cb3dSCy Schubert #endif	/* WRAP_DBG_MALLOC */
101