]>
Commit | Line | Data |
---|---|---|
4f22f405 | 1 | /* |
f520f134 | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
4f22f405 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
bbd86bf5 | 12 | #include <limits.h> |
458cddc1 | 13 | #include <openssl/crypto.h> |
b39fc560 | 14 | #include "internal/cryptlib.h" |
d02b48c6 | 15 | |
0f113f3e MC |
16 | /* |
17 | * the following pointers may be changed as long as 'allow_customize' is set | |
18 | */ | |
bbd86bf5 | 19 | static int allow_customize = 1; |
a5435e8b | 20 | |
05c7b163 | 21 | static void *(*malloc_impl)(size_t, const char *, int) |
bbd86bf5 | 22 | = CRYPTO_malloc; |
05c7b163 | 23 | static void *(*realloc_impl)(void *, size_t, const char *, int) |
bbd86bf5 | 24 | = CRYPTO_realloc; |
05c7b163 | 25 | static void (*free_impl)(void *, const char *, int) |
bbd86bf5 | 26 | = CRYPTO_free; |
74924dcb | 27 | |
c2e27310 | 28 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
bbd86bf5 | 29 | static int call_malloc_debug = 1; |
f3a2a044 | 30 | #else |
bbd86bf5 | 31 | static int call_malloc_debug = 0; |
f3a2a044 | 32 | #endif |
9ac42ed8 | 33 | |
bbd86bf5 RS |
34 | int CRYPTO_set_mem_functions( |
35 | void *(*m)(size_t, const char *, int), | |
36 | void *(*r)(void *, size_t, const char *, int), | |
05c7b163 | 37 | void (*f)(void *, const char *, int)) |
74924dcb | 38 | { |
74924dcb RS |
39 | if (!allow_customize) |
40 | return 0; | |
bbd86bf5 | 41 | if (m) |
05c7b163 | 42 | malloc_impl = m; |
bbd86bf5 | 43 | if (r) |
05c7b163 | 44 | realloc_impl = r; |
bbd86bf5 | 45 | if (f) |
05c7b163 | 46 | free_impl = f; |
74924dcb RS |
47 | return 1; |
48 | } | |
49 | ||
bbd86bf5 | 50 | int CRYPTO_set_mem_debug(int flag) |
74924dcb RS |
51 | { |
52 | if (!allow_customize) | |
53 | return 0; | |
bbd86bf5 | 54 | call_malloc_debug = flag; |
0f113f3e MC |
55 | return 1; |
56 | } | |
57 | ||
bbd86bf5 RS |
58 | void CRYPTO_get_mem_functions( |
59 | void *(**m)(size_t, const char *, int), | |
60 | void *(**r)(void *, size_t, const char *, int), | |
05c7b163 | 61 | void (**f)(void *, const char *, int)) |
0f113f3e MC |
62 | { |
63 | if (m != NULL) | |
05c7b163 | 64 | *m = malloc_impl; |
0f113f3e | 65 | if (r != NULL) |
05c7b163 | 66 | *r = realloc_impl; |
74924dcb | 67 | if (f != NULL) |
05c7b163 | 68 | *f = free_impl; |
0f113f3e | 69 | } |
d02b48c6 | 70 | |
ff842856 | 71 | void *CRYPTO_malloc(size_t num, const char *file, int line) |
0f113f3e MC |
72 | { |
73 | void *ret = NULL; | |
74 | ||
05c7b163 RL |
75 | if (malloc_impl != NULL && malloc_impl != CRYPTO_malloc) |
76 | return malloc_impl(num, file, line); | |
77 | ||
c62ee125 | 78 | if (num == 0) |
0f113f3e MC |
79 | return NULL; |
80 | ||
4044ebfe | 81 | if (allow_customize) { |
82 | /* | |
83 | * Disallow customization after the first allocation. We only set this | |
84 | * if necessary to avoid a store to the same cache line on every | |
85 | * allocation. | |
86 | */ | |
87 | allow_customize = 0; | |
88 | } | |
c2e27310 | 89 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
bbd86bf5 RS |
90 | if (call_malloc_debug) { |
91 | CRYPTO_mem_debug_malloc(NULL, num, 0, file, line); | |
92 | ret = malloc(num); | |
93 | CRYPTO_mem_debug_malloc(ret, num, 1, file, line); | |
94 | } else { | |
95 | ret = malloc(num); | |
0f113f3e | 96 | } |
bbd86bf5 | 97 | #else |
a773b52a | 98 | osslargused(file); osslargused(line); |
bbd86bf5 RS |
99 | ret = malloc(num); |
100 | #endif | |
d02b48c6 | 101 | |
0f113f3e MC |
102 | return ret; |
103 | } | |
104 | ||
ff842856 | 105 | void *CRYPTO_zalloc(size_t num, const char *file, int line) |
b51bce94 RS |
106 | { |
107 | void *ret = CRYPTO_malloc(num, file, line); | |
108 | ||
109 | if (ret != NULL) | |
110 | memset(ret, 0, num); | |
111 | return ret; | |
112 | } | |
113 | ||
ff842856 | 114 | void *CRYPTO_realloc(void *str, size_t num, const char *file, int line) |
0f113f3e | 115 | { |
05c7b163 RL |
116 | if (realloc_impl != NULL && realloc_impl != &CRYPTO_realloc) |
117 | return realloc_impl(str, num, file, line); | |
118 | ||
0f113f3e MC |
119 | if (str == NULL) |
120 | return CRYPTO_malloc(num, file, line); | |
d5234c7b | 121 | |
bbd86bf5 | 122 | if (num == 0) { |
05c7b163 | 123 | CRYPTO_free(str, file, line); |
0f113f3e | 124 | return NULL; |
bbd86bf5 | 125 | } |
d5234c7b | 126 | |
c2e27310 | 127 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
bbd86bf5 RS |
128 | if (call_malloc_debug) { |
129 | void *ret; | |
130 | CRYPTO_mem_debug_realloc(str, NULL, num, 0, file, line); | |
131 | ret = realloc(str, num); | |
132 | CRYPTO_mem_debug_realloc(str, ret, num, 1, file, line); | |
133 | return ret; | |
134 | } | |
135 | #else | |
a773b52a | 136 | osslargused(file); osslargused(line); |
bbd86bf5 RS |
137 | #endif |
138 | return realloc(str, num); | |
9ac42ed8 | 139 | |
0f113f3e | 140 | } |
d02b48c6 | 141 | |
c99de053 | 142 | void *CRYPTO_clear_realloc(void *str, size_t old_len, size_t num, |
ff842856 | 143 | const char *file, int line) |
0f113f3e MC |
144 | { |
145 | void *ret = NULL; | |
146 | ||
147 | if (str == NULL) | |
148 | return CRYPTO_malloc(num, file, line); | |
149 | ||
bbd86bf5 | 150 | if (num == 0) { |
05c7b163 | 151 | CRYPTO_clear_free(str, old_len, file, line); |
0f113f3e | 152 | return NULL; |
bbd86bf5 | 153 | } |
0f113f3e | 154 | |
bbd86bf5 RS |
155 | /* Can't shrink the buffer since memcpy below copies |old_len| bytes. */ |
156 | if (num < old_len) { | |
3ce2fdab | 157 | OPENSSL_cleanse((char*)str + num, old_len - num); |
bbd86bf5 RS |
158 | return str; |
159 | } | |
0f113f3e | 160 | |
05c7b163 | 161 | ret = CRYPTO_malloc(num, file, line); |
2ac7753c | 162 | if (ret != NULL) { |
bbd86bf5 | 163 | memcpy(ret, str, old_len); |
2ac7753c DSH |
164 | CRYPTO_clear_free(str, old_len, file, line); |
165 | } | |
0f113f3e MC |
166 | return ret; |
167 | } | |
54a656ef | 168 | |
05c7b163 | 169 | void CRYPTO_free(void *str, const char *file, int line) |
0f113f3e | 170 | { |
05c7b163 RL |
171 | if (free_impl != NULL && free_impl != &CRYPTO_free) { |
172 | free_impl(str, file, line); | |
173 | return; | |
174 | } | |
175 | ||
c2e27310 | 176 | #ifndef OPENSSL_NO_CRYPTO_MDEBUG |
bbd86bf5 | 177 | if (call_malloc_debug) { |
05c7b163 | 178 | CRYPTO_mem_debug_free(str, 0, file, line); |
bbd86bf5 | 179 | free(str); |
05c7b163 | 180 | CRYPTO_mem_debug_free(str, 1, file, line); |
bbd86bf5 RS |
181 | } else { |
182 | free(str); | |
183 | } | |
184 | #else | |
185 | free(str); | |
186 | #endif | |
0f113f3e | 187 | } |
d02b48c6 | 188 | |
05c7b163 | 189 | void CRYPTO_clear_free(void *str, size_t num, const char *file, int line) |
4b45c6e5 | 190 | { |
bbd86bf5 | 191 | if (str == NULL) |
4b45c6e5 RS |
192 | return; |
193 | if (num) | |
194 | OPENSSL_cleanse(str, num); | |
05c7b163 | 195 | CRYPTO_free(str, file, line); |
4b45c6e5 | 196 | } |