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