155714Skris/* crypto/mem.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include <stdlib.h> 6155714Skris#include <openssl/crypto.h> 6255714Skris#include "cryptlib.h" 6355714Skris 64296341Sdelphijstatic int allow_customize = 1; /* we provide flexible functions for */ 65296341Sdelphijstatic int allow_customize_debug = 1; /* exchanging memory-related functions 66296341Sdelphij * at run-time, but this must be done 67296341Sdelphij * before any blocks are actually 68296341Sdelphij * allocated; or we'll run into huge 69296341Sdelphij * problems when malloc/free pairs 70296341Sdelphij * don't match etc. */ 7155714Skris 72296341Sdelphij/* 73296341Sdelphij * the following pointers may be changed as long as 'allow_customize' is set 74296341Sdelphij */ 7555714Skris 76296341Sdelphijstatic void *(*malloc_func) (size_t) = malloc; 77296341Sdelphijstatic void *default_malloc_ex(size_t num, const char *file, int line) 78296341Sdelphij{ 79296341Sdelphij return malloc_func(num); 80296341Sdelphij} 81109998Smarkm 82296341Sdelphijstatic void *(*malloc_ex_func) (size_t, const char *file, int line) 83296341Sdelphij = default_malloc_ex; 84109998Smarkm 85296341Sdelphijstatic void *(*realloc_func) (void *, size_t) = realloc; 86296341Sdelphijstatic void *default_realloc_ex(void *str, size_t num, 87296341Sdelphij const char *file, int line) 88296341Sdelphij{ 89296341Sdelphij return realloc_func(str, num); 90296341Sdelphij} 91109998Smarkm 92296341Sdelphijstatic void *(*realloc_ex_func) (void *, size_t, const char *file, int line) 93296341Sdelphij = default_realloc_ex; 94109998Smarkm 95296341Sdelphijstatic void (*free_func) (void *) = free; 96109998Smarkm 97296341Sdelphijstatic void *(*malloc_locked_func) (size_t) = malloc; 98109998Smarkmstatic void *default_malloc_locked_ex(size_t num, const char *file, int line) 99296341Sdelphij{ 100296341Sdelphij return malloc_locked_func(num); 101296341Sdelphij} 102109998Smarkm 103296341Sdelphijstatic void *(*malloc_locked_ex_func) (size_t, const char *file, int line) 104296341Sdelphij = default_malloc_locked_ex; 105109998Smarkm 106296341Sdelphijstatic void (*free_locked_func) (void *) = free; 107109998Smarkm 108109998Smarkm/* may be changed as long as 'allow_customize_debug' is set */ 10959191Skris/* XXX use correct function pointer types */ 110238405Sjkim#ifdef CRYPTO_MDEBUG 11168651Skris/* use default functions from mem_dbg.c */ 112296341Sdelphijstatic void (*malloc_debug_func) (void *, int, const char *, int, int) 113296341Sdelphij = CRYPTO_dbg_malloc; 114296341Sdelphijstatic void (*realloc_debug_func) (void *, void *, int, const char *, int, 115296341Sdelphij int) 116296341Sdelphij = CRYPTO_dbg_realloc; 117296341Sdelphijstatic void (*free_debug_func) (void *, int) = CRYPTO_dbg_free; 118296341Sdelphijstatic void (*set_debug_options_func) (long) = CRYPTO_dbg_set_options; 119296341Sdelphijstatic long (*get_debug_options_func) (void) = CRYPTO_dbg_get_options; 12059191Skris#else 121296341Sdelphij/* 122296341Sdelphij * applications can use CRYPTO_malloc_debug_init() to select above case at 123296341Sdelphij * run-time 124296341Sdelphij */ 125296341Sdelphijstatic void (*malloc_debug_func) (void *, int, const char *, int, int) = NULL; 126296341Sdelphijstatic void (*realloc_debug_func) (void *, void *, int, const char *, int, 127296341Sdelphij int) 128296341Sdelphij = NULL; 129296341Sdelphijstatic void (*free_debug_func) (void *, int) = NULL; 130296341Sdelphijstatic void (*set_debug_options_func) (long) = NULL; 131296341Sdelphijstatic long (*get_debug_options_func) (void) = NULL; 13255714Skris#endif 13355714Skris 134296341Sdelphijint CRYPTO_set_mem_functions(void *(*m) (size_t), void *(*r) (void *, size_t), 135296341Sdelphij void (*f) (void *)) 136296341Sdelphij{ 137296341Sdelphij /* Dummy call just to ensure OPENSSL_init() gets linked in */ 138296341Sdelphij OPENSSL_init(); 139296341Sdelphij if (!allow_customize) 140296341Sdelphij return 0; 141296341Sdelphij if ((m == 0) || (r == 0) || (f == 0)) 142296341Sdelphij return 0; 143296341Sdelphij malloc_func = m; 144296341Sdelphij malloc_ex_func = default_malloc_ex; 145296341Sdelphij realloc_func = r; 146296341Sdelphij realloc_ex_func = default_realloc_ex; 147296341Sdelphij free_func = f; 148296341Sdelphij malloc_locked_func = m; 149296341Sdelphij malloc_locked_ex_func = default_malloc_locked_ex; 150296341Sdelphij free_locked_func = f; 151296341Sdelphij return 1; 152296341Sdelphij} 15355714Skris 154296341Sdelphijint CRYPTO_set_mem_ex_functions(void *(*m) (size_t, const char *, int), 155296341Sdelphij void *(*r) (void *, size_t, const char *, 156296341Sdelphij int), void (*f) (void *)) 157296341Sdelphij{ 158296341Sdelphij if (!allow_customize) 159296341Sdelphij return 0; 160296341Sdelphij if ((m == 0) || (r == 0) || (f == 0)) 161296341Sdelphij return 0; 162296341Sdelphij malloc_func = 0; 163296341Sdelphij malloc_ex_func = m; 164296341Sdelphij realloc_func = 0; 165296341Sdelphij realloc_ex_func = r; 166296341Sdelphij free_func = f; 167296341Sdelphij malloc_locked_func = 0; 168296341Sdelphij malloc_locked_ex_func = m; 169296341Sdelphij free_locked_func = f; 170296341Sdelphij return 1; 171296341Sdelphij} 172109998Smarkm 173296341Sdelphijint CRYPTO_set_locked_mem_functions(void *(*m) (size_t), void (*f) (void *)) 174296341Sdelphij{ 175296341Sdelphij if (!allow_customize) 176296341Sdelphij return 0; 177296341Sdelphij if ((m == NULL) || (f == NULL)) 178296341Sdelphij return 0; 179296341Sdelphij malloc_locked_func = m; 180296341Sdelphij malloc_locked_ex_func = default_malloc_locked_ex; 181296341Sdelphij free_locked_func = f; 182296341Sdelphij return 1; 183296341Sdelphij} 18455714Skris 185296341Sdelphijint CRYPTO_set_locked_mem_ex_functions(void *(*m) (size_t, const char *, int), 186296341Sdelphij void (*f) (void *)) 187296341Sdelphij{ 188296341Sdelphij if (!allow_customize) 189296341Sdelphij return 0; 190296341Sdelphij if ((m == NULL) || (f == NULL)) 191296341Sdelphij return 0; 192296341Sdelphij malloc_locked_func = 0; 193296341Sdelphij malloc_locked_ex_func = m; 194296341Sdelphij free_func = f; 195296341Sdelphij return 1; 196296341Sdelphij} 197109998Smarkm 198296341Sdelphijint CRYPTO_set_mem_debug_functions(void (*m) 199296341Sdelphij (void *, int, const char *, int, int), 200296341Sdelphij void (*r) (void *, void *, int, 201296341Sdelphij const char *, int, int), 202296341Sdelphij void (*f) (void *, int), void (*so) (long), 203296341Sdelphij long (*go) (void)) 204296341Sdelphij{ 205296341Sdelphij if (!allow_customize_debug) 206296341Sdelphij return 0; 207296341Sdelphij OPENSSL_init(); 208296341Sdelphij malloc_debug_func = m; 209296341Sdelphij realloc_debug_func = r; 210296341Sdelphij free_debug_func = f; 211296341Sdelphij set_debug_options_func = so; 212296341Sdelphij get_debug_options_func = go; 213296341Sdelphij return 1; 214296341Sdelphij} 21559191Skris 216296341Sdelphijvoid CRYPTO_get_mem_functions(void *(**m) (size_t), 217296341Sdelphij void *(**r) (void *, size_t), 218296341Sdelphij void (**f) (void *)) 219296341Sdelphij{ 220296341Sdelphij if (m != NULL) 221296341Sdelphij *m = (malloc_ex_func == default_malloc_ex) ? malloc_func : 0; 222296341Sdelphij if (r != NULL) 223296341Sdelphij *r = (realloc_ex_func == default_realloc_ex) ? realloc_func : 0; 224296341Sdelphij if (f != NULL) 225296341Sdelphij *f = free_func; 226296341Sdelphij} 227109998Smarkm 228296341Sdelphijvoid CRYPTO_get_mem_ex_functions(void *(**m) (size_t, const char *, int), 229296341Sdelphij void *(**r) (void *, size_t, const char *, 230296341Sdelphij int), void (**f) (void *)) 231296341Sdelphij{ 232296341Sdelphij if (m != NULL) 233296341Sdelphij *m = (malloc_ex_func != default_malloc_ex) ? malloc_ex_func : 0; 234296341Sdelphij if (r != NULL) 235296341Sdelphij *r = (realloc_ex_func != default_realloc_ex) ? realloc_ex_func : 0; 236296341Sdelphij if (f != NULL) 237296341Sdelphij *f = free_func; 238296341Sdelphij} 23955714Skris 240296341Sdelphijvoid CRYPTO_get_locked_mem_functions(void *(**m) (size_t), 241296341Sdelphij void (**f) (void *)) 242296341Sdelphij{ 243296341Sdelphij if (m != NULL) 244296341Sdelphij *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? 245296341Sdelphij malloc_locked_func : 0; 246296341Sdelphij if (f != NULL) 247296341Sdelphij *f = free_locked_func; 248296341Sdelphij} 249109998Smarkm 250296341Sdelphijvoid CRYPTO_get_locked_mem_ex_functions(void 251296341Sdelphij *(**m) (size_t, const char *, int), 252296341Sdelphij void (**f) (void *)) 253296341Sdelphij{ 254296341Sdelphij if (m != NULL) 255296341Sdelphij *m = (malloc_locked_ex_func != default_malloc_locked_ex) ? 256296341Sdelphij malloc_locked_ex_func : 0; 257296341Sdelphij if (f != NULL) 258296341Sdelphij *f = free_locked_func; 259296341Sdelphij} 26055714Skris 261296341Sdelphijvoid CRYPTO_get_mem_debug_functions(void (**m) 262296341Sdelphij (void *, int, const char *, int, int), 263296341Sdelphij void (**r) (void *, void *, int, 264296341Sdelphij const char *, int, int), 265296341Sdelphij void (**f) (void *, int), 266296341Sdelphij void (**so) (long), long (**go) (void)) 267296341Sdelphij{ 268296341Sdelphij if (m != NULL) 269296341Sdelphij *m = malloc_debug_func; 270296341Sdelphij if (r != NULL) 271296341Sdelphij *r = realloc_debug_func; 272296341Sdelphij if (f != NULL) 273296341Sdelphij *f = free_debug_func; 274296341Sdelphij if (so != NULL) 275296341Sdelphij *so = set_debug_options_func; 276296341Sdelphij if (go != NULL) 277296341Sdelphij *go = get_debug_options_func; 278296341Sdelphij} 279109998Smarkm 28059191Skrisvoid *CRYPTO_malloc_locked(int num, const char *file, int line) 281296341Sdelphij{ 282296341Sdelphij void *ret = NULL; 28355714Skris 284296341Sdelphij if (num <= 0) 285296341Sdelphij return NULL; 286111147Snectar 287296341Sdelphij if (allow_customize) 288296341Sdelphij allow_customize = 0; 289296341Sdelphij if (malloc_debug_func != NULL) { 290296341Sdelphij if (allow_customize_debug) 291296341Sdelphij allow_customize_debug = 0; 292296341Sdelphij malloc_debug_func(NULL, num, file, line, 0); 293296341Sdelphij } 294296341Sdelphij ret = malloc_locked_ex_func(num, file, line); 295109998Smarkm#ifdef LEVITTE_DEBUG_MEM 296296341Sdelphij fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); 29759191Skris#endif 298296341Sdelphij if (malloc_debug_func != NULL) 299296341Sdelphij malloc_debug_func(ret, num, file, line, 1); 30055714Skris 301238405Sjkim#ifndef OPENSSL_CPUID_OBJ 302296341Sdelphij /* 303296341Sdelphij * Create a dependency on the value of 'cleanse_ctr' so our memory 304296341Sdelphij * sanitisation function can't be optimised out. NB: We only do this for 305296341Sdelphij * >2Kb so the overhead doesn't bother us. 306296341Sdelphij */ 307296341Sdelphij if (ret && (num > 2048)) { 308296341Sdelphij extern unsigned char cleanse_ctr; 309296341Sdelphij ((unsigned char *)ret)[0] = cleanse_ctr; 310296341Sdelphij } 311238405Sjkim#endif 312109998Smarkm 313296341Sdelphij return ret; 314296341Sdelphij} 31555714Skris 31659191Skrisvoid CRYPTO_free_locked(void *str) 317296341Sdelphij{ 318296341Sdelphij if (free_debug_func != NULL) 319296341Sdelphij free_debug_func(str, 0); 320109998Smarkm#ifdef LEVITTE_DEBUG_MEM 321296341Sdelphij fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str); 32255714Skris#endif 323296341Sdelphij free_locked_func(str); 324296341Sdelphij if (free_debug_func != NULL) 325296341Sdelphij free_debug_func(NULL, 1); 326296341Sdelphij} 32755714Skris 32859191Skrisvoid *CRYPTO_malloc(int num, const char *file, int line) 329296341Sdelphij{ 330296341Sdelphij void *ret = NULL; 33155714Skris 332296341Sdelphij if (num <= 0) 333296341Sdelphij return NULL; 334111147Snectar 335296341Sdelphij if (allow_customize) 336296341Sdelphij allow_customize = 0; 337296341Sdelphij if (malloc_debug_func != NULL) { 338296341Sdelphij if (allow_customize_debug) 339296341Sdelphij allow_customize_debug = 0; 340296341Sdelphij malloc_debug_func(NULL, num, file, line, 0); 341296341Sdelphij } 342296341Sdelphij ret = malloc_ex_func(num, file, line); 343109998Smarkm#ifdef LEVITTE_DEBUG_MEM 344296341Sdelphij fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); 34559191Skris#endif 346296341Sdelphij if (malloc_debug_func != NULL) 347296341Sdelphij malloc_debug_func(ret, num, file, line, 1); 34855714Skris 349238405Sjkim#ifndef OPENSSL_CPUID_OBJ 350296341Sdelphij /* 351296341Sdelphij * Create a dependency on the value of 'cleanse_ctr' so our memory 352296341Sdelphij * sanitisation function can't be optimised out. NB: We only do this for 353296341Sdelphij * >2Kb so the overhead doesn't bother us. 354296341Sdelphij */ 355296341Sdelphij if (ret && (num > 2048)) { 356296341Sdelphij extern unsigned char cleanse_ctr; 357296341Sdelphij ((unsigned char *)ret)[0] = cleanse_ctr; 358296341Sdelphij } 359238405Sjkim#endif 360109998Smarkm 361296341Sdelphij return ret; 362296341Sdelphij} 363296341Sdelphij 364238405Sjkimchar *CRYPTO_strdup(const char *str, const char *file, int line) 365296341Sdelphij{ 366296341Sdelphij char *ret = CRYPTO_malloc(strlen(str) + 1, file, line); 36755714Skris 368296341Sdelphij if (ret == NULL) 369296341Sdelphij return NULL; 370238405Sjkim 371296341Sdelphij strcpy(ret, str); 372296341Sdelphij return ret; 373296341Sdelphij} 374296341Sdelphij 37559191Skrisvoid *CRYPTO_realloc(void *str, int num, const char *file, int line) 376296341Sdelphij{ 377296341Sdelphij void *ret = NULL; 37855714Skris 379296341Sdelphij if (str == NULL) 380296341Sdelphij return CRYPTO_malloc(num, file, line); 381111147Snectar 382296341Sdelphij if (num <= 0) 383296341Sdelphij return NULL; 384160814Ssimon 385296341Sdelphij if (realloc_debug_func != NULL) 386296341Sdelphij realloc_debug_func(str, NULL, num, file, line, 0); 387296341Sdelphij ret = realloc_ex_func(str, num, file, line); 388109998Smarkm#ifdef LEVITTE_DEBUG_MEM 389296341Sdelphij fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, 390296341Sdelphij ret, num); 391109998Smarkm#endif 392296341Sdelphij if (realloc_debug_func != NULL) 393296341Sdelphij realloc_debug_func(str, ret, num, file, line, 1); 394101613Snectar 395296341Sdelphij return ret; 396296341Sdelphij} 397109998Smarkm 398109998Smarkmvoid *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file, 399296341Sdelphij int line) 400296341Sdelphij{ 401296341Sdelphij void *ret = NULL; 402109998Smarkm 403296341Sdelphij if (str == NULL) 404296341Sdelphij return CRYPTO_malloc(num, file, line); 405160814Ssimon 406296341Sdelphij if (num <= 0) 407296341Sdelphij return NULL; 408160814Ssimon 409296341Sdelphij /* 410296341Sdelphij * We don't support shrinking the buffer. Note the memcpy that copies 411296341Sdelphij * |old_len| bytes to the new buffer, below. 412296341Sdelphij */ 413296341Sdelphij if (num < old_len) 414296341Sdelphij return NULL; 415234954Sbz 416296341Sdelphij if (realloc_debug_func != NULL) 417296341Sdelphij realloc_debug_func(str, NULL, num, file, line, 0); 418296341Sdelphij ret = malloc_ex_func(num, file, line); 419296341Sdelphij if (ret) { 420296341Sdelphij memcpy(ret, str, old_len); 421296341Sdelphij OPENSSL_cleanse(str, old_len); 422296341Sdelphij free_func(str); 423296341Sdelphij } 424109998Smarkm#ifdef LEVITTE_DEBUG_MEM 425296341Sdelphij fprintf(stderr, 426296341Sdelphij "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", 427296341Sdelphij str, ret, num); 42855714Skris#endif 429296341Sdelphij if (realloc_debug_func != NULL) 430296341Sdelphij realloc_debug_func(str, ret, num, file, line, 1); 43155714Skris 432296341Sdelphij return ret; 433296341Sdelphij} 43455714Skris 43559191Skrisvoid CRYPTO_free(void *str) 436296341Sdelphij{ 437296341Sdelphij if (free_debug_func != NULL) 438296341Sdelphij free_debug_func(str, 0); 439109998Smarkm#ifdef LEVITTE_DEBUG_MEM 440296341Sdelphij fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str); 44155714Skris#endif 442296341Sdelphij free_func(str); 443296341Sdelphij if (free_debug_func != NULL) 444296341Sdelphij free_debug_func(NULL, 1); 445296341Sdelphij} 44655714Skris 44759191Skrisvoid *CRYPTO_remalloc(void *a, int num, const char *file, int line) 448296341Sdelphij{ 449296341Sdelphij if (a != NULL) 450296341Sdelphij OPENSSL_free(a); 451296341Sdelphij a = (char *)OPENSSL_malloc(num); 452296341Sdelphij return (a); 453296341Sdelphij} 45455714Skris 45559191Skrisvoid CRYPTO_set_mem_debug_options(long bits) 456296341Sdelphij{ 457296341Sdelphij if (set_debug_options_func != NULL) 458296341Sdelphij set_debug_options_func(bits); 459296341Sdelphij} 46055714Skris 46159191Skrislong CRYPTO_get_mem_debug_options(void) 462296341Sdelphij{ 463296341Sdelphij if (get_debug_options_func != NULL) 464296341Sdelphij return get_debug_options_func(); 465296341Sdelphij return 0; 466296341Sdelphij} 467