diff options
Diffstat (limited to 'src/lib/libcrypto/mem.c')
| -rw-r--r-- | src/lib/libcrypto/mem.c | 441 |
1 files changed, 0 insertions, 441 deletions
diff --git a/src/lib/libcrypto/mem.c b/src/lib/libcrypto/mem.c deleted file mode 100644 index 548c248785..0000000000 --- a/src/lib/libcrypto/mem.c +++ /dev/null | |||
| @@ -1,441 +0,0 @@ | |||
| 1 | /* crypto/mem.c */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <stdio.h> | ||
| 60 | #include <stdlib.h> | ||
| 61 | #include <openssl/crypto.h> | ||
| 62 | #include "cryptlib.h" | ||
| 63 | |||
| 64 | |||
| 65 | static int allow_customize = 1; | ||
| 66 | /* we provide flexible functions for */ | ||
| 67 | static int allow_customize_debug = 1;/* exchanging memory - related functions at | ||
| 68 | * run - time, but this must be done | ||
| 69 | * before any blocks are actually | ||
| 70 | * allocated; or we'll run into huge | ||
| 71 | * problems when malloc/free pairs | ||
| 72 | * don't match etc. */ | ||
| 73 | |||
| 74 | /* the following pointers may be changed as long as 'allow_customize' is set */ | ||
| 75 | |||
| 76 | static void *(*malloc_func)(size_t) = malloc; | ||
| 77 | static void | ||
| 78 | *default_malloc_ex(size_t num, const char *file, int line) | ||
| 79 | { | ||
| 80 | return malloc_func(num); | ||
| 81 | } | ||
| 82 | static void *(*malloc_ex_func)(size_t, const char *file, int line) = | ||
| 83 | default_malloc_ex; | ||
| 84 | |||
| 85 | static void *(*realloc_func)(void *, size_t) = realloc; | ||
| 86 | static void | ||
| 87 | *default_realloc_ex(void *str, size_t num, const char *file, int line) | ||
| 88 | { | ||
| 89 | return realloc_func(str, num); | ||
| 90 | } | ||
| 91 | static void *(*realloc_ex_func)(void *, size_t, const char *file, int line) = | ||
| 92 | default_realloc_ex; | ||
| 93 | |||
| 94 | static void (*free_func)(void *) = free; | ||
| 95 | |||
| 96 | static void *(*malloc_locked_func)(size_t) = malloc; | ||
| 97 | static void | ||
| 98 | *default_malloc_locked_ex(size_t num, const char *file, int line) | ||
| 99 | { | ||
| 100 | return malloc_locked_func(num); | ||
| 101 | } | ||
| 102 | static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) = | ||
| 103 | default_malloc_locked_ex; | ||
| 104 | |||
| 105 | static void (*free_locked_func)(void *) = free; | ||
| 106 | |||
| 107 | |||
| 108 | /* may be changed as long as 'allow_customize_debug' is set */ | ||
| 109 | /* XXX use correct function pointer types */ | ||
| 110 | #ifdef CRYPTO_MDEBUG | ||
| 111 | /* use default functions from mem_dbg.c */ | ||
| 112 | static void (*malloc_debug_func)(void *, int, const char *, int, int) = | ||
| 113 | CRYPTO_dbg_malloc; | ||
| 114 | static void (*realloc_debug_func)(void *, void *, int, const char *, int, int) = | ||
| 115 | CRYPTO_dbg_realloc; | ||
| 116 | static void (*free_debug_func)(void *, int) = CRYPTO_dbg_free; | ||
| 117 | static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; | ||
| 118 | static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; | ||
| 119 | #else | ||
| 120 | /* applications can use CRYPTO_malloc_debug_init() to select above case | ||
| 121 | * at run-time */ | ||
| 122 | static void (*malloc_debug_func)(void *, int, const char *, int, int) = NULL; | ||
| 123 | static void (*realloc_debug_func)(void *, void *, int, const char *, int, int) = | ||
| 124 | NULL; | ||
| 125 | static void (*free_debug_func)(void *, int) = NULL; | ||
| 126 | static void (*set_debug_options_func)(long) = NULL; | ||
| 127 | static long (*get_debug_options_func)(void) = NULL; | ||
| 128 | #endif | ||
| 129 | |||
| 130 | int | ||
| 131 | CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), | ||
| 132 | void (*f)(void *)) | ||
| 133 | { | ||
| 134 | /* Dummy call just to ensure OPENSSL_init() gets linked in */ | ||
| 135 | OPENSSL_init(); | ||
| 136 | if (!allow_customize) | ||
| 137 | return 0; | ||
| 138 | if ((m == 0) || (r == 0) || (f == 0)) | ||
| 139 | return 0; | ||
| 140 | malloc_func = m; | ||
| 141 | malloc_ex_func = default_malloc_ex; | ||
| 142 | realloc_func = r; | ||
| 143 | realloc_ex_func = default_realloc_ex; | ||
| 144 | free_func = f; | ||
| 145 | malloc_locked_func = m; | ||
| 146 | malloc_locked_ex_func = default_malloc_locked_ex; | ||
| 147 | free_locked_func = f; | ||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | |||
| 151 | int | ||
| 152 | CRYPTO_set_mem_ex_functions(void *(*m)(size_t, const char *, int), | ||
| 153 | void *(*r)(void *, size_t, const char *, int), void (*f)(void *)) | ||
| 154 | { | ||
| 155 | if (!allow_customize) | ||
| 156 | return 0; | ||
| 157 | if ((m == 0) || (r == 0) || (f == 0)) | ||
| 158 | return 0; | ||
| 159 | malloc_func = 0; | ||
| 160 | malloc_ex_func = m; | ||
| 161 | realloc_func = 0; | ||
| 162 | realloc_ex_func = r; | ||
| 163 | free_func = f; | ||
| 164 | malloc_locked_func = 0; | ||
| 165 | malloc_locked_ex_func = m; | ||
| 166 | free_locked_func = f; | ||
| 167 | return 1; | ||
| 168 | } | ||
| 169 | |||
| 170 | int | ||
| 171 | CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) | ||
| 172 | { | ||
| 173 | if (!allow_customize) | ||
| 174 | return 0; | ||
| 175 | if ((m == NULL) || (f == NULL)) | ||
| 176 | return 0; | ||
| 177 | malloc_locked_func = m; | ||
| 178 | malloc_locked_ex_func = default_malloc_locked_ex; | ||
| 179 | free_locked_func = f; | ||
| 180 | return 1; | ||
| 181 | } | ||
| 182 | |||
| 183 | int | ||
| 184 | CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t, const char *, int), | ||
| 185 | void (*f)(void *)) | ||
| 186 | { | ||
| 187 | if (!allow_customize) | ||
| 188 | return 0; | ||
| 189 | if ((m == NULL) || (f == NULL)) | ||
| 190 | return 0; | ||
| 191 | malloc_locked_func = 0; | ||
| 192 | malloc_locked_ex_func = m; | ||
| 193 | free_func = f; | ||
| 194 | return 1; | ||
| 195 | } | ||
| 196 | |||
| 197 | int | ||
| 198 | CRYPTO_set_mem_debug_functions(void (*m)(void *, int, const char *, int, int), | ||
| 199 | void (*r)(void *, void *, int, const char *, int, int), | ||
| 200 | void (*f)(void *, int), void (*so)(long), long (*go)(void)) | ||
| 201 | { | ||
| 202 | if (!allow_customize_debug) | ||
| 203 | return 0; | ||
| 204 | OPENSSL_init(); | ||
| 205 | malloc_debug_func = m; | ||
| 206 | realloc_debug_func = r; | ||
| 207 | free_debug_func = f; | ||
| 208 | set_debug_options_func = so; | ||
| 209 | get_debug_options_func = go; | ||
| 210 | return 1; | ||
| 211 | } | ||
| 212 | |||
| 213 | |||
| 214 | void | ||
| 215 | CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), | ||
| 216 | void (**f)(void *)) | ||
| 217 | { | ||
| 218 | if (m != NULL) | ||
| 219 | *m = (malloc_ex_func == default_malloc_ex) ? malloc_func : 0; | ||
| 220 | if (r != NULL) | ||
| 221 | *r = (realloc_ex_func == default_realloc_ex) ? realloc_func : 0; | ||
| 222 | if (f != NULL) | ||
| 223 | *f = free_func; | ||
| 224 | } | ||
| 225 | |||
| 226 | void | ||
| 227 | CRYPTO_get_mem_ex_functions(void *(**m)(size_t, const char *, int), | ||
| 228 | void *(**r)(void *, size_t, const char *, int), void (**f)(void *)) | ||
| 229 | { | ||
| 230 | if (m != NULL) | ||
| 231 | *m = (malloc_ex_func != default_malloc_ex) ? malloc_ex_func : 0; | ||
| 232 | if (r != NULL) | ||
| 233 | *r = (realloc_ex_func != default_realloc_ex) ? | ||
| 234 | realloc_ex_func : 0; | ||
| 235 | if (f != NULL) | ||
| 236 | *f = free_func; | ||
| 237 | } | ||
| 238 | |||
| 239 | void | ||
| 240 | CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) | ||
| 241 | { | ||
| 242 | if (m != NULL) | ||
| 243 | *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? | ||
| 244 | malloc_locked_func : 0; | ||
| 245 | if (f != NULL) | ||
| 246 | *f = free_locked_func; | ||
| 247 | } | ||
| 248 | |||
| 249 | void | ||
| 250 | CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t, const char *, int), | ||
| 251 | void (**f)(void *)) | ||
| 252 | { | ||
| 253 | if (m != NULL) | ||
| 254 | *m = (malloc_locked_ex_func != default_malloc_locked_ex) ? | ||
| 255 | malloc_locked_ex_func : 0; | ||
| 256 | if (f != NULL) | ||
| 257 | *f = free_locked_func; | ||
| 258 | } | ||
| 259 | |||
| 260 | void | ||
| 261 | CRYPTO_get_mem_debug_functions(void (**m)(void *, int, const char *, int, int), | ||
| 262 | void (**r)(void *, void *, int, const char *, int, int), | ||
| 263 | void (**f)(void *, int), void (**so)(long), long (**go)(void)) | ||
| 264 | { | ||
| 265 | if (m != NULL) | ||
| 266 | *m = malloc_debug_func; | ||
| 267 | if (r != NULL) | ||
| 268 | *r = realloc_debug_func; | ||
| 269 | if (f != NULL) | ||
| 270 | *f = free_debug_func; | ||
| 271 | if (so != NULL) | ||
| 272 | *so = set_debug_options_func; | ||
| 273 | if (go != NULL) | ||
| 274 | *go = get_debug_options_func; | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | void * | ||
| 279 | CRYPTO_malloc_locked(int num, const char *file, int line) | ||
| 280 | { | ||
| 281 | void *ret = NULL; | ||
| 282 | |||
| 283 | if (num <= 0) | ||
| 284 | return NULL; | ||
| 285 | |||
| 286 | allow_customize = 0; | ||
| 287 | if (malloc_debug_func != NULL) { | ||
| 288 | allow_customize_debug = 0; | ||
| 289 | malloc_debug_func(NULL, num, file, line, 0); | ||
| 290 | } | ||
| 291 | ret = malloc_locked_ex_func(num, file, line); | ||
| 292 | #ifdef LEVITTE_DEBUG_MEM | ||
| 293 | fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); | ||
| 294 | #endif | ||
| 295 | if (malloc_debug_func != NULL) | ||
| 296 | malloc_debug_func(ret, num, file, line, 1); | ||
| 297 | |||
| 298 | return ret; | ||
| 299 | } | ||
| 300 | |||
| 301 | void | ||
| 302 | CRYPTO_free_locked(void *str) | ||
| 303 | { | ||
| 304 | if (free_debug_func != NULL) | ||
| 305 | free_debug_func(str, 0); | ||
| 306 | #ifdef LEVITTE_DEBUG_MEM | ||
| 307 | fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str); | ||
| 308 | #endif | ||
| 309 | free_locked_func(str); | ||
| 310 | if (free_debug_func != NULL) | ||
| 311 | free_debug_func(NULL, 1); | ||
| 312 | } | ||
| 313 | |||
| 314 | void * | ||
| 315 | CRYPTO_malloc(int num, const char *file, int line) | ||
| 316 | { | ||
| 317 | void *ret = NULL; | ||
| 318 | |||
| 319 | if (num <= 0) | ||
| 320 | return NULL; | ||
| 321 | |||
| 322 | allow_customize = 0; | ||
| 323 | if (malloc_debug_func != NULL) { | ||
| 324 | allow_customize_debug = 0; | ||
| 325 | malloc_debug_func(NULL, num, file, line, 0); | ||
| 326 | } | ||
| 327 | ret = malloc_ex_func(num, file, line); | ||
| 328 | #ifdef LEVITTE_DEBUG_MEM | ||
| 329 | fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); | ||
| 330 | #endif | ||
| 331 | if (malloc_debug_func != NULL) | ||
| 332 | malloc_debug_func(ret, num, file, line, 1); | ||
| 333 | |||
| 334 | return ret; | ||
| 335 | } | ||
| 336 | |||
| 337 | char * | ||
| 338 | CRYPTO_strdup(const char *str, const char *file, int line) | ||
| 339 | { | ||
| 340 | size_t len = strlen(str) + 1; | ||
| 341 | char *ret = CRYPTO_malloc(len, file, line); | ||
| 342 | |||
| 343 | memcpy(ret, str, len); | ||
| 344 | return ret; | ||
| 345 | } | ||
| 346 | |||
| 347 | void * | ||
| 348 | CRYPTO_realloc(void *str, int num, const char *file, int line) | ||
| 349 | { | ||
| 350 | void *ret = NULL; | ||
| 351 | |||
| 352 | if (str == NULL) | ||
| 353 | return CRYPTO_malloc(num, file, line); | ||
| 354 | |||
| 355 | if (num <= 0) | ||
| 356 | return NULL; | ||
| 357 | |||
| 358 | if (realloc_debug_func != NULL) | ||
| 359 | realloc_debug_func(str, NULL, num, file, line, 0); | ||
| 360 | ret = realloc_ex_func(str, num, file, line); | ||
| 361 | #ifdef LEVITTE_DEBUG_MEM | ||
| 362 | fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num); | ||
| 363 | #endif | ||
| 364 | if (realloc_debug_func != NULL) | ||
| 365 | realloc_debug_func(str, ret, num, file, line, 1); | ||
| 366 | |||
| 367 | return ret; | ||
| 368 | } | ||
| 369 | |||
| 370 | void * | ||
| 371 | CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file, | ||
| 372 | int line) | ||
| 373 | { | ||
| 374 | void *ret = NULL; | ||
| 375 | |||
| 376 | if (str == NULL) | ||
| 377 | return CRYPTO_malloc(num, file, line); | ||
| 378 | |||
| 379 | if (num <= 0) | ||
| 380 | return NULL; | ||
| 381 | |||
| 382 | /* We don't support shrinking the buffer. Note the memcpy that copies | ||
| 383 | * |old_len| bytes to the new buffer, below. */ | ||
| 384 | if (num < old_len) | ||
| 385 | return NULL; | ||
| 386 | |||
| 387 | if (realloc_debug_func != NULL) | ||
| 388 | realloc_debug_func(str, NULL, num, file, line, 0); | ||
| 389 | ret = malloc_ex_func(num, file, line); | ||
| 390 | if (ret) { | ||
| 391 | memcpy(ret, str, old_len); | ||
| 392 | OPENSSL_cleanse(str, old_len); | ||
| 393 | free_func(str); | ||
| 394 | } | ||
| 395 | #ifdef LEVITTE_DEBUG_MEM | ||
| 396 | fprintf(stderr, | ||
| 397 | "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", | ||
| 398 | str, ret, num); | ||
| 399 | #endif | ||
| 400 | if (realloc_debug_func != NULL) | ||
| 401 | realloc_debug_func(str, ret, num, file, line, 1); | ||
| 402 | |||
| 403 | return ret; | ||
| 404 | } | ||
| 405 | |||
| 406 | void | ||
| 407 | CRYPTO_free(void *str) | ||
| 408 | { | ||
| 409 | if (free_debug_func != NULL) | ||
| 410 | free_debug_func(str, 0); | ||
| 411 | #ifdef LEVITTE_DEBUG_MEM | ||
| 412 | fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str); | ||
| 413 | #endif | ||
| 414 | free_func(str); | ||
| 415 | if (free_debug_func != NULL) | ||
| 416 | free_debug_func(NULL, 1); | ||
| 417 | } | ||
| 418 | |||
| 419 | void * | ||
| 420 | CRYPTO_remalloc(void *a, int num, const char *file, int line) | ||
| 421 | { | ||
| 422 | if (a != NULL) | ||
| 423 | OPENSSL_free(a); | ||
| 424 | a = (char *)OPENSSL_malloc(num); | ||
| 425 | return (a); | ||
| 426 | } | ||
| 427 | |||
| 428 | void | ||
| 429 | CRYPTO_set_mem_debug_options(long bits) | ||
| 430 | { | ||
| 431 | if (set_debug_options_func != NULL) | ||
| 432 | set_debug_options_func(bits); | ||
| 433 | } | ||
| 434 | |||
| 435 | long | ||
| 436 | CRYPTO_get_mem_debug_options(void) | ||
| 437 | { | ||
| 438 | if (get_debug_options_func != NULL) | ||
| 439 | return get_debug_options_func(); | ||
| 440 | return 0; | ||
| 441 | } | ||
