[331] | 1 | /*
|
---|
| 2 | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
| 3 | *
|
---|
| 4 | * Licensed under the OpenSSL license (the "License"). You may not use
|
---|
| 5 | * this file except in compliance with the License. You can obtain a copy
|
---|
| 6 | * in the file LICENSE in the source distribution or at
|
---|
| 7 | * https://www.openssl.org/source/license.html
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 | #include <stdio.h>
|
---|
| 11 | #include <stdlib.h>
|
---|
| 12 | #include <limits.h>
|
---|
| 13 | #include <openssl/crypto.h>
|
---|
| 14 | #include "internal/cryptlib.h"
|
---|
| 15 |
|
---|
| 16 | /*
|
---|
| 17 | * the following pointers may be changed as long as 'allow_customize' is set
|
---|
| 18 | */
|
---|
| 19 | static int allow_customize = 1;
|
---|
| 20 |
|
---|
| 21 | static void *(*malloc_impl)(size_t, const char *, int)
|
---|
| 22 | = CRYPTO_malloc;
|
---|
| 23 | static void *(*realloc_impl)(void *, size_t, const char *, int)
|
---|
| 24 | = CRYPTO_realloc;
|
---|
| 25 | static void (*free_impl)(void *, const char *, int)
|
---|
| 26 | = CRYPTO_free;
|
---|
| 27 |
|
---|
| 28 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG
|
---|
| 29 | static int call_malloc_debug = 1;
|
---|
| 30 | #else
|
---|
| 31 | static int call_malloc_debug = 0;
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 | int CRYPTO_set_mem_functions(
|
---|
| 35 | void *(*m)(size_t, const char *, int),
|
---|
| 36 | void *(*r)(void *, size_t, const char *, int),
|
---|
| 37 | void (*f)(void *, const char *, int))
|
---|
| 38 | {
|
---|
| 39 | if (!allow_customize)
|
---|
| 40 | return 0;
|
---|
| 41 | if (m)
|
---|
| 42 | malloc_impl = m;
|
---|
| 43 | if (r)
|
---|
| 44 | realloc_impl = r;
|
---|
| 45 | if (f)
|
---|
| 46 | free_impl = f;
|
---|
| 47 | return 1;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | int CRYPTO_set_mem_debug(int flag)
|
---|
| 51 | {
|
---|
| 52 | if (!allow_customize)
|
---|
| 53 | return 0;
|
---|
| 54 | call_malloc_debug = flag;
|
---|
| 55 | return 1;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | void CRYPTO_get_mem_functions(
|
---|
| 59 | void *(**m)(size_t, const char *, int),
|
---|
| 60 | void *(**r)(void *, size_t, const char *, int),
|
---|
| 61 | void (**f)(void *, const char *, int))
|
---|
| 62 | {
|
---|
| 63 | if (m != NULL)
|
---|
| 64 | *m = malloc_impl;
|
---|
| 65 | if (r != NULL)
|
---|
| 66 | *r = realloc_impl;
|
---|
| 67 | if (f != NULL)
|
---|
| 68 | *f = free_impl;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | void *CRYPTO_malloc(size_t num, const char *file, int line)
|
---|
| 72 | {
|
---|
| 73 | void *ret = NULL;
|
---|
| 74 |
|
---|
| 75 | if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc)
|
---|
| 76 | return malloc_impl(num, file, line);
|
---|
| 77 |
|
---|
| 78 | if (num <= 0)
|
---|
| 79 | return NULL;
|
---|
| 80 |
|
---|
| 81 | allow_customize = 0;
|
---|
| 82 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG
|
---|
| 83 | if (call_malloc_debug) {
|
---|
| 84 | CRYPTO_mem_debug_malloc(NULL, num, 0, file, line);
|
---|
| 85 | ret = malloc(num);
|
---|
| 86 | CRYPTO_mem_debug_malloc(ret, num, 1, file, line);
|
---|
| 87 | } else {
|
---|
| 88 | ret = malloc(num);
|
---|
| 89 | }
|
---|
| 90 | #else
|
---|
| 91 | osslargused(file); osslargused(line);
|
---|
| 92 | ret = malloc(num);
|
---|
| 93 | #endif
|
---|
| 94 |
|
---|
| 95 | return ret;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | void *CRYPTO_zalloc(size_t num, const char *file, int line)
|
---|
| 99 | {
|
---|
| 100 | void *ret = CRYPTO_malloc(num, file, line);
|
---|
| 101 |
|
---|
| 102 | if (ret != NULL)
|
---|
| 103 | memset(ret, 0, num);
|
---|
| 104 | return ret;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | void *CRYPTO_realloc(void *str, size_t num, const char *file, int line)
|
---|
| 108 | {
|
---|
| 109 | if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc)
|
---|
| 110 | return realloc_impl(str, num, file, line);
|
---|
| 111 |
|
---|
| 112 | if (str == NULL)
|
---|
| 113 | return CRYPTO_malloc(num, file, line);
|
---|
| 114 |
|
---|
| 115 | if (num == 0) {
|
---|
| 116 | CRYPTO_free(str, file, line);
|
---|
| 117 | return NULL;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | allow_customize = 0;
|
---|
| 121 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG
|
---|
| 122 | if (call_malloc_debug) {
|
---|
| 123 | void *ret;
|
---|
| 124 | CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line);
|
---|
| 125 | ret = realloc(str, num);
|
---|
| 126 | CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line);
|
---|
| 127 | return ret;
|
---|
| 128 | }
|
---|
| 129 | #else
|
---|
| 130 | osslargused(file); osslargused(line);
|
---|
| 131 | #endif
|
---|
| 132 | return realloc(str, num);
|
---|
| 133 |
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num,
|
---|
| 137 | const char *file, int line)
|
---|
| 138 | {
|
---|
| 139 | void *ret = NULL;
|
---|
| 140 |
|
---|
| 141 | if (str == NULL)
|
---|
| 142 | return CRYPTO_malloc(num, file, line);
|
---|
| 143 |
|
---|
| 144 | if (num == 0) {
|
---|
| 145 | CRYPTO_clear_free(str, old_len, file, line);
|
---|
| 146 | return NULL;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */
|
---|
| 150 | if (num < old_len) {
|
---|
| 151 | OPENSSL_cleanse((char*)str + num, old_len - num);
|
---|
| 152 | return str;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | ret = CRYPTO_malloc(num, file, line);
|
---|
| 156 | if (ret != NULL) {
|
---|
| 157 | memcpy(ret, str, old_len);
|
---|
| 158 | CRYPTO_clear_free(str, old_len, file, line);
|
---|
| 159 | }
|
---|
| 160 | return ret;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | void CRYPTO_free(void *str, const char *file, int line)
|
---|
| 164 | {
|
---|
| 165 | if (free_impl != NULL && free_impl != &CRYPTO_free) {
|
---|
| 166 | free_impl(str, file, line);
|
---|
| 167 | return;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG
|
---|
| 171 | if (call_malloc_debug) {
|
---|
| 172 | CRYPTO_mem_debug_free(str, 0, file, line);
|
---|
| 173 | free(str);
|
---|
| 174 | CRYPTO_mem_debug_free(str, 1, file, line);
|
---|
| 175 | } else {
|
---|
| 176 | free(str);
|
---|
| 177 | }
|
---|
| 178 | #else
|
---|
| 179 | free(str);
|
---|
| 180 | #endif
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | void CRYPTO_clear_free(void *str, size_t num, const char *file, int line)
|
---|
| 184 | {
|
---|
| 185 | if (str == NULL)
|
---|
| 186 | return;
|
---|
| 187 | if (num)
|
---|
| 188 | OPENSSL_cleanse(str, num);
|
---|
| 189 | CRYPTO_free(str, file, line);
|
---|
| 190 | }
|
---|