sl@0: /* crypto/mem.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: /* sl@0: © Portions copyright (c) 2006 Nokia Corporation. All rights reserved. sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "cryptlib.h" sl@0: #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__))) sl@0: #include "libcrypto_wsd_macros.h" sl@0: #include "libcrypto_wsd.h" sl@0: #endif sl@0: sl@0: #ifndef EMULATOR sl@0: static int allow_customize = 1; /* we provide flexible functions for */ sl@0: static int allow_customize_debug = 1;/* exchanging memory-related functions at sl@0: * run-time, but this must be done sl@0: * before any blocks are actually sl@0: * allocated; or we'll run into huge sl@0: * problems when malloc/free pairs sl@0: * don't match etc. */ sl@0: #else sl@0: GET_STATIC_VAR_FROM_TLS(allow_customize,mem,int) sl@0: #define allow_customize (*GET_WSD_VAR_NAME(allow_customize,mem,s)()) sl@0: GET_STATIC_VAR_FROM_TLS(allow_customize_debug,mem,int) sl@0: #define allow_customize_debug (*GET_WSD_VAR_NAME(allow_customize_debug,mem,s)()) sl@0: #endif sl@0: sl@0: sl@0: /* the following pointers may be changed as long as 'allow_customize' is set */ sl@0: #ifndef EMULATOR sl@0: typedef void *(*malloc_func_t)(size_t); sl@0: static void *(*malloc_func)(size_t) = NULL; sl@0: static malloc_func_t const malloc_func_def = malloc; sl@0: static void *default_malloc_ex(size_t num, const char *file, int line) sl@0: { sl@0: if(malloc_func) sl@0: return malloc_func(num); sl@0: else sl@0: return malloc_func_def(num); sl@0: } sl@0: #else sl@0: #define malloc_func libcrypto_ImpurePtr()->malloc_func sl@0: void *default_malloc_ex(size_t num, const char *file, int line) sl@0: { return malloc_func(num); } sl@0: #endif sl@0: #ifndef EMULATOR sl@0: static void *(*malloc_ex_func)(size_t, const char *file, int line) sl@0: = default_malloc_ex; sl@0: #else sl@0: #define malloc_ex_func libcrypto_ImpurePtr()->malloc_ex_func sl@0: #endif sl@0: sl@0: #ifndef EMULATOR sl@0: typedef void *(*realloc_func_t)(void *, size_t); sl@0: static void *(*realloc_func)(void *, size_t)= NULL; sl@0: static realloc_func_t const realloc_func_def = realloc ; sl@0: static void *default_realloc_ex(void *str, size_t num, sl@0: const char *file, int line) sl@0: { sl@0: if(realloc_func) sl@0: return realloc_func(str,num); sl@0: else sl@0: return realloc_func_def(str,num); sl@0: } sl@0: #else sl@0: #define realloc_func libcrypto_ImpurePtr()->realloc_func sl@0: void *default_realloc_ex(void *str, size_t num, sl@0: const char *file, int line) sl@0: { return realloc_func(str,num); } sl@0: #endif sl@0: sl@0: sl@0: #ifndef EMULATOR sl@0: static void *(*realloc_ex_func)(void *, size_t, const char *file, int line) sl@0: = default_realloc_ex; sl@0: #else sl@0: #define realloc_ex_func libcrypto_ImpurePtr()->realloc_ex_func sl@0: #endif sl@0: sl@0: #ifndef EMULATOR sl@0: typedef void (*free_func_t)(void *); sl@0: static void (*free_func)(void *) = NULL; sl@0: static free_func_t const free_func_def = free; sl@0: #else sl@0: #define free_func_openssl libcrypto_ImpurePtr()->free_func_openssl sl@0: #endif sl@0: sl@0: #ifndef EMULATOR sl@0: typedef void *(*malloc_locked_func_t)(size_t); sl@0: static void *(*malloc_locked_func)(size_t) = NULL; sl@0: static malloc_locked_func_t const malloc_locked_func_def = malloc; sl@0: #else sl@0: #define malloc_locked_func libcrypto_ImpurePtr()->malloc_locked_func sl@0: #endif sl@0: sl@0: #ifndef EMULATOR sl@0: static void *default_malloc_locked_ex(size_t num, const char *file, int line) sl@0: { sl@0: if(malloc_locked_func) sl@0: return malloc_locked_func(num); sl@0: else sl@0: return malloc_locked_func_def(num); sl@0: } sl@0: sl@0: static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) sl@0: = default_malloc_locked_ex; sl@0: sl@0: typedef void (*free_locked_func_t)(void *); sl@0: static void (*free_locked_func)(void *) = NULL; sl@0: static free_locked_func_t const free_locked_func_def = free; sl@0: #else sl@0: void *default_malloc_locked_ex(size_t num, const char *file, int line) sl@0: { return malloc_locked_func(num); } sl@0: sl@0: #define malloc_locked_ex_func libcrypto_ImpurePtr()->malloc_locked_ex_func sl@0: #define free_locked_func libcrypto_ImpurePtr()->free_locked_func sl@0: #endif sl@0: sl@0: sl@0: sl@0: sl@0: /* may be changed as long as 'allow_customize_debug' is set */ sl@0: /* XXX use correct function pointer types */ sl@0: #ifdef CRYPTO_MDEBUG sl@0: /* use default functions from mem_dbg.c */ sl@0: #ifndef EMULATOR sl@0: static void (*malloc_debug_func)(void *,int,const char *,int,int) sl@0: = CRYPTO_dbg_malloc; sl@0: static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) sl@0: = CRYPTO_dbg_realloc; sl@0: static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free; sl@0: static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; sl@0: static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; sl@0: #else sl@0: #define malloc_debug_func libcrypto_ImpurePtr()->malloc_debug_func sl@0: #define realloc_debug_func libcrypto_ImpurePtr()->realloc_debug_func sl@0: #define free_debug_func libcrypto_ImpurePtr()->free_debug_func sl@0: #define set_debug_options_func libcrypto_ImpurePtr()->set_debug_options_func sl@0: #define get_debug_options_func libcrypto_ImpurePtr()->get_debug_options_func sl@0: sl@0: #endif sl@0: #else sl@0: /* applications can use CRYPTO_malloc_debug_init() to select above case sl@0: * at run-time */ sl@0: static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL; sl@0: static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) sl@0: = NULL; sl@0: static void (*free_debug_func)(void *,int) = NULL; sl@0: static void (*set_debug_options_func)(long) = NULL; sl@0: static long (*get_debug_options_func)(void) = NULL; sl@0: #endif sl@0: sl@0: sl@0: EXPORT_C int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), sl@0: void (*f)(void *)) sl@0: { sl@0: if (!allow_customize) sl@0: return 0; sl@0: if ((m == 0) || (r == 0) || (f == 0)) sl@0: return 0; sl@0: malloc_func=m; malloc_ex_func=default_malloc_ex; sl@0: realloc_func=r; realloc_ex_func=default_realloc_ex; sl@0: #ifndef EMULATOR sl@0: free_func=f; sl@0: #else sl@0: free_func_openssl=f; sl@0: #endif sl@0: malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex; sl@0: free_locked_func=f; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int CRYPTO_set_mem_ex_functions( sl@0: void *(*m)(size_t,const char *,int), sl@0: void *(*r)(void *, size_t,const char *,int), sl@0: void (*f)(void *)) sl@0: { sl@0: if (!allow_customize) sl@0: return 0; sl@0: if ((m == 0) || (r == 0) || (f == 0)) sl@0: return 0; sl@0: malloc_func=0; malloc_ex_func=m; sl@0: realloc_func=0; realloc_ex_func=r; sl@0: #ifndef EMULATOR sl@0: free_func=f; sl@0: #else sl@0: free_func_openssl=f; sl@0: #endif sl@0: malloc_locked_func=0; malloc_locked_ex_func=m; sl@0: free_locked_func=f; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) sl@0: { sl@0: if (!allow_customize) sl@0: return 0; sl@0: if ((m == NULL) || (f == NULL)) sl@0: return 0; sl@0: malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex; sl@0: free_locked_func=f; sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int CRYPTO_set_locked_mem_ex_functions( sl@0: void *(*m)(size_t,const char *,int), sl@0: void (*f)(void *)) sl@0: { sl@0: if (!allow_customize) sl@0: return 0; sl@0: if ((m == NULL) || (f == NULL)) sl@0: return 0; sl@0: malloc_locked_func=0; malloc_locked_ex_func=m; sl@0: #ifndef EMULATOR sl@0: free_func=f; sl@0: #else sl@0: free_func_openssl=f; sl@0: #endif sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int), sl@0: void (*r)(void *,void *,int,const char *,int,int), sl@0: void (*f)(void *,int), sl@0: void (*so)(long), sl@0: long (*go)(void)) sl@0: { sl@0: if (!allow_customize_debug) sl@0: return 0; sl@0: malloc_debug_func=m; sl@0: realloc_debug_func=r; sl@0: free_debug_func=f; sl@0: set_debug_options_func=so; sl@0: get_debug_options_func=go; sl@0: return 1; sl@0: } sl@0: sl@0: sl@0: EXPORT_C void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), sl@0: void (**f)(void *)) sl@0: { sl@0: if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? sl@0: #ifndef EMULATOR sl@0: (malloc_func!=NULL?malloc_func:malloc_func_def) : 0; sl@0: #else sl@0: malloc_func : 0; sl@0: #endif sl@0: sl@0: if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? sl@0: #ifndef EMULATOR sl@0: (realloc_func!=NULL?realloc_func:realloc_func_def) : 0; sl@0: #else sl@0: realloc_func : 0; sl@0: #endif sl@0: sl@0: #ifndef EMULATOR sl@0: if (f != NULL) *f=(free_func?free_func:free_func_def); sl@0: #else sl@0: if (f != NULL) *f=free_func_openssl; sl@0: #endif sl@0: } sl@0: sl@0: EXPORT_C void CRYPTO_get_mem_ex_functions( sl@0: void *(**m)(size_t,const char *,int), sl@0: void *(**r)(void *, size_t,const char *,int), sl@0: void (**f)(void *)) sl@0: { sl@0: if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ? sl@0: malloc_ex_func : 0; sl@0: if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ? sl@0: realloc_ex_func : 0; sl@0: #ifndef EMULATOR sl@0: if (f != NULL) *f=(free_func?free_func:free_func_def); sl@0: #else sl@0: if (f != NULL) *f=free_func_openssl; sl@0: #endif sl@0: } sl@0: sl@0: EXPORT_C void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) sl@0: { sl@0: if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? sl@0: #ifndef EMULATOR sl@0: (malloc_locked_func?malloc_locked_func:malloc_locked_func_def) : 0; sl@0: #else sl@0: malloc_locked_func : 0; sl@0: #endif sl@0: #ifndef EMULATOR sl@0: if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def); sl@0: #else sl@0: if (f != NULL) *f=free_locked_func; sl@0: #endif sl@0: } sl@0: sl@0: EXPORT_C void CRYPTO_get_locked_mem_ex_functions( sl@0: void *(**m)(size_t,const char *,int), sl@0: void (**f)(void *)) sl@0: { sl@0: if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ? sl@0: malloc_locked_ex_func : 0; sl@0: #ifndef EMULATOR sl@0: if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def); sl@0: #else sl@0: if (f != NULL) *f= free_locked_func; sl@0: #endif sl@0: } sl@0: sl@0: EXPORT_C void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int), sl@0: void (**r)(void *,void *,int,const char *,int,int), sl@0: void (**f)(void *,int), sl@0: void (**so)(long), sl@0: long (**go)(void)) sl@0: { sl@0: if (m != NULL) *m=malloc_debug_func; sl@0: if (r != NULL) *r=realloc_debug_func; sl@0: if (f != NULL) *f=free_debug_func; sl@0: if (so != NULL) *so=set_debug_options_func; sl@0: if (go != NULL) *go=get_debug_options_func; sl@0: } sl@0: sl@0: #ifdef EMULATOR sl@0: sl@0: unsigned char GET_WSD_VAR_NAME(cleanse_ctr,mem_clr, g)(); sl@0: #define cleanse_ctr (*GET_WSD_VAR_NAME(cleanse_ctr,mem_clr, g)()) sl@0: #endif sl@0: sl@0: EXPORT_C void *CRYPTO_malloc_locked(int num, const char *file, int line) sl@0: { sl@0: void *ret = NULL; sl@0: extern unsigned char cleanse_ctr; sl@0: sl@0: if (num <= 0) return NULL; sl@0: sl@0: allow_customize = 0; sl@0: if (malloc_debug_func != NULL) sl@0: { sl@0: allow_customize_debug = 0; sl@0: malloc_debug_func(NULL, num, file, line, 0); sl@0: } sl@0: ret = malloc_locked_ex_func(num,file,line); sl@0: #ifdef LEVITTE_DEBUG_MEM sl@0: fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); sl@0: #endif sl@0: if (malloc_debug_func != NULL) sl@0: malloc_debug_func(ret, num, file, line, 1); sl@0: sl@0: /* Create a dependency on the value of 'cleanse_ctr' so our memory sl@0: * sanitisation function can't be optimised out. NB: We only do sl@0: * this for >2Kb so the overhead doesn't bother us. */ sl@0: if(ret && (num > 2048)) sl@0: ((unsigned char *)ret)[0] = cleanse_ctr; sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C void CRYPTO_free_locked(void *str) sl@0: { sl@0: if (free_debug_func != NULL) sl@0: free_debug_func(str, 0); sl@0: #ifdef LEVITTE_DEBUG_MEM sl@0: fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str); sl@0: #endif sl@0: #ifndef EMULATOR sl@0: free_locked_func?free_locked_func(str):free_locked_func_def(str); sl@0: #else sl@0: free_locked_func(str); sl@0: #endif sl@0: if (free_debug_func != NULL) sl@0: free_debug_func(NULL, 1); sl@0: } sl@0: sl@0: sl@0: sl@0: EXPORT_C void *CRYPTO_malloc(int num, const char *file, int line) sl@0: { sl@0: void *ret = NULL; sl@0: extern unsigned char cleanse_ctr; sl@0: sl@0: if (num <= 0) return NULL; sl@0: sl@0: allow_customize = 0; sl@0: if (malloc_debug_func != NULL) sl@0: { sl@0: allow_customize_debug = 0; sl@0: malloc_debug_func(NULL, num, file, line, 0); sl@0: } sl@0: ret = malloc_ex_func(num,file,line); sl@0: #ifdef LEVITTE_DEBUG_MEM sl@0: fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); sl@0: #endif sl@0: if (malloc_debug_func != NULL) sl@0: malloc_debug_func(ret, num, file, line, 1); sl@0: sl@0: /* Create a dependency on the value of 'cleanse_ctr' so our memory sl@0: * sanitisation function can't be optimised out. NB: We only do sl@0: * this for >2Kb so the overhead doesn't bother us. */ sl@0: if(ret && (num > 2048)) sl@0: ((unsigned char *)ret)[0] = cleanse_ctr; sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C void *CRYPTO_realloc(void *str, int num, const char *file, int line) sl@0: { sl@0: void *ret = NULL; sl@0: sl@0: if (str == NULL) sl@0: return CRYPTO_malloc(num, file, line); sl@0: sl@0: if (num <= 0) return NULL; sl@0: sl@0: if (realloc_debug_func != NULL) sl@0: realloc_debug_func(str, NULL, num, file, line, 0); sl@0: ret = realloc_ex_func(str,num,file,line); sl@0: #ifdef LEVITTE_DEBUG_MEM sl@0: fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num); sl@0: #endif sl@0: if (realloc_debug_func != NULL) sl@0: realloc_debug_func(str, ret, num, file, line, 1); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: EXPORT_C void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file, sl@0: int line) sl@0: { sl@0: void *ret = NULL; sl@0: sl@0: if (str == NULL) sl@0: return CRYPTO_malloc(num, file, line); sl@0: sl@0: if (num <= 0) return NULL; sl@0: sl@0: if (realloc_debug_func != NULL) sl@0: realloc_debug_func(str, NULL, num, file, line, 0); sl@0: ret=malloc_ex_func(num,file,line); sl@0: if(ret) sl@0: { sl@0: memcpy(ret,str,old_len); sl@0: OPENSSL_cleanse(str,old_len); sl@0: #ifndef EMULATOR sl@0: free_func?free_func(str):free_func_def(str); sl@0: #else sl@0: free_func_openssl(str); sl@0: #endif sl@0: } sl@0: #ifdef LEVITTE_DEBUG_MEM sl@0: fprintf(stderr, sl@0: "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", sl@0: str, ret, num); sl@0: #endif sl@0: if (realloc_debug_func != NULL) sl@0: realloc_debug_func(str, ret, num, file, line, 1); sl@0: sl@0: return ret; sl@0: } sl@0: sl@0: sl@0: EXPORT_C void CRYPTO_free(void *str) sl@0: { sl@0: if (free_debug_func != NULL) sl@0: free_debug_func(str, 0); sl@0: #ifdef LEVITTE_DEBUG_MEM sl@0: fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str); sl@0: #endif sl@0: #ifndef EMULATOR sl@0: free_func?free_func(str):free_func_def(str); sl@0: #else sl@0: free_func_openssl(str); sl@0: #endif sl@0: if (free_debug_func != NULL) sl@0: free_debug_func(NULL, 1); sl@0: } sl@0: sl@0: sl@0: EXPORT_C void *CRYPTO_remalloc(void *a, int num, const char *file, int line) sl@0: { sl@0: if (a != NULL) OPENSSL_free(a); sl@0: a=(char *)OPENSSL_malloc(num); sl@0: return(a); sl@0: } sl@0: sl@0: EXPORT_C void CRYPTO_set_mem_debug_options(long bits) sl@0: { sl@0: if (set_debug_options_func != NULL) sl@0: set_debug_options_func(bits); sl@0: } sl@0: sl@0: EXPORT_C long CRYPTO_get_mem_debug_options(void) sl@0: { sl@0: if (get_debug_options_func != NULL) sl@0: return get_debug_options_func(); sl@0: return 0; sl@0: } sl@0: sl@0: