diff options
author | beck <> | 2014-04-16 03:24:53 +0000 |
---|---|---|
committer | beck <> | 2014-04-16 03:24:53 +0000 |
commit | 5ebad8aceae77c6da6a1e47c3f7e70e8ffae3dae (patch) | |
tree | e60f8186026303c75b1e90f9e9bd192dc1b02171 /src | |
parent | 3c1ef33c56df584ddf4397f1cf1c34bf828b86da (diff) | |
download | openbsd-5ebad8aceae77c6da6a1e47c3f7e70e8ffae3dae.tar.gz openbsd-5ebad8aceae77c6da6a1e47c3f7e70e8ffae3dae.tar.bz2 openbsd-5ebad8aceae77c6da6a1e47c3f7e70e8ffae3dae.zip |
Your operating system memory allocation functions are your friend. If they
are not please fix your operating system. Replace mem.c with an API-compatible
wrapper that just calls the system functions and does not allow a one word
modification of a variable in a running shared library to turn on memory
debug functions that expose things that should not be seen.
ok tedu@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/crypto/Makefile | 4 | ||||
-rw-r--r-- | src/lib/libcrypto/malloc-wrapper.c | 199 | ||||
-rw-r--r-- | src/lib/libcrypto/mem.c | 441 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/malloc-wrapper.c | 199 | ||||
-rw-r--r-- | src/lib/libssl/src/crypto/mem.c | 441 |
5 files changed, 400 insertions, 884 deletions
diff --git a/src/lib/libcrypto/crypto/Makefile b/src/lib/libcrypto/crypto/Makefile index 447fc28bbe..1e95a60209 100644 --- a/src/lib/libcrypto/crypto/Makefile +++ b/src/lib/libcrypto/crypto/Makefile | |||
@@ -1,4 +1,4 @@ | |||
1 | # $OpenBSD: Makefile,v 1.13 2014/04/15 22:44:15 tedu Exp $ | 1 | # $OpenBSD: Makefile,v 1.14 2014/04/16 03:24:53 beck Exp $ |
2 | 2 | ||
3 | LIB= crypto | 3 | LIB= crypto |
4 | 4 | ||
@@ -42,7 +42,7 @@ CFLAGS+= -I${LCRYPTO_SRC} | |||
42 | CFLAGS+= -I${LCRYPTO_SRC}/modes -I${LCRYPTO_SRC}/asn1 -I${LCRYPTO_SRC}/evp | 42 | CFLAGS+= -I${LCRYPTO_SRC}/modes -I${LCRYPTO_SRC}/asn1 -I${LCRYPTO_SRC}/evp |
43 | 43 | ||
44 | # crypto/ | 44 | # crypto/ |
45 | SRCS+= cryptlib.c mem.c mem_dbg.c cversion.c ex_data.c cpt_err.c | 45 | SRCS+= cryptlib.c malloc-wrapper.c mem_dbg.c cversion.c ex_data.c cpt_err.c |
46 | SRCS+= uid.c o_time.c o_str.c o_fips.c o_init.c | 46 | SRCS+= uid.c o_time.c o_str.c o_fips.c o_init.c |
47 | 47 | ||
48 | # aes/ | 48 | # aes/ |
diff --git a/src/lib/libcrypto/malloc-wrapper.c b/src/lib/libcrypto/malloc-wrapper.c new file mode 100644 index 0000000000..49d7c572c8 --- /dev/null +++ b/src/lib/libcrypto/malloc-wrapper.c | |||
@@ -0,0 +1,199 @@ | |||
1 | /* $OpenBSD: malloc-wrapper.c,v 1.1 2014/04/16 03:24:53 beck Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2014 Bob Beck | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | #include <stdio.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | |||
21 | int | ||
22 | CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), | ||
23 | void (*f)(void *)) | ||
24 | { | ||
25 | return 0; | ||
26 | } | ||
27 | |||
28 | int | ||
29 | CRYPTO_set_mem_ex_functions(void *(*m)(size_t, const char *, int), | ||
30 | void *(*r)(void *, size_t, const char *, int), void (*f)(void *)) | ||
31 | { | ||
32 | return 0; | ||
33 | } | ||
34 | |||
35 | int | ||
36 | CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) | ||
37 | { | ||
38 | return 0; | ||
39 | } | ||
40 | |||
41 | int | ||
42 | CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t, const char *, int), | ||
43 | void (*f)(void *)) | ||
44 | { | ||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | int | ||
49 | CRYPTO_set_mem_debug_functions(void (*m)(void *, int, const char *, int, int), | ||
50 | void (*r)(void *, void *, int, const char *, int, int), | ||
51 | void (*f)(void *, int), void (*so)(long), long (*go)(void)) | ||
52 | { | ||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | |||
57 | void | ||
58 | CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), | ||
59 | void (**f)(void *)) | ||
60 | { | ||
61 | if (m != NULL) | ||
62 | *m = malloc; | ||
63 | if (r != NULL) | ||
64 | *r = realloc; | ||
65 | if (f != NULL) | ||
66 | *f = free; | ||
67 | } | ||
68 | |||
69 | void | ||
70 | CRYPTO_get_mem_ex_functions(void *(**m)(size_t, const char *, int), | ||
71 | void *(**r)(void *, size_t, const char *, int), void (**f)(void *)) | ||
72 | { | ||
73 | if (m != NULL) | ||
74 | *m = NULL; | ||
75 | if (r != NULL) | ||
76 | *r = NULL; | ||
77 | if (f != NULL) | ||
78 | *f = free; | ||
79 | } | ||
80 | |||
81 | void | ||
82 | CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) | ||
83 | { | ||
84 | if (m != NULL) | ||
85 | *m = malloc; | ||
86 | if (f != NULL) | ||
87 | *f = free; | ||
88 | } | ||
89 | |||
90 | void | ||
91 | CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t, const char *, int), | ||
92 | void (**f)(void *)) | ||
93 | { | ||
94 | if (m != NULL) | ||
95 | *m = NULL; | ||
96 | if (f != NULL) | ||
97 | *f = free; | ||
98 | } | ||
99 | |||
100 | void | ||
101 | CRYPTO_get_mem_debug_functions(void (**m)(void *, int, const char *, int, int), | ||
102 | void (**r)(void *, void *, int, const char *, int, int), | ||
103 | void (**f)(void *, int), void (**so)(long), long (**go)(void)) | ||
104 | { | ||
105 | if (m != NULL) | ||
106 | *m = NULL; | ||
107 | if (r != NULL) | ||
108 | *r = NULL; | ||
109 | if (f != NULL) | ||
110 | *f = NULL; | ||
111 | if (so != NULL) | ||
112 | *so = NULL; | ||
113 | if (go != NULL) | ||
114 | *go = NULL; | ||
115 | } | ||
116 | |||
117 | |||
118 | void * | ||
119 | CRYPTO_malloc_locked(int num, const char *file, int line) | ||
120 | { | ||
121 | void *ret = NULL; | ||
122 | |||
123 | if (num <= 0) | ||
124 | return NULL; | ||
125 | return malloc(num); | ||
126 | } | ||
127 | |||
128 | void | ||
129 | CRYPTO_free_locked(void *ptr) | ||
130 | { | ||
131 | free(ptr); | ||
132 | } | ||
133 | |||
134 | void * | ||
135 | CRYPTO_malloc(int num, const char *file, int line) | ||
136 | { | ||
137 | if (num <= 0) | ||
138 | return NULL; | ||
139 | return malloc(num); | ||
140 | } | ||
141 | |||
142 | char * | ||
143 | CRYPTO_strdup(const char *str, const char *file, int line) | ||
144 | { | ||
145 | return strdup(str); | ||
146 | } | ||
147 | |||
148 | void * | ||
149 | CRYPTO_realloc(void *ptr, int num, const char *file, int line) | ||
150 | { | ||
151 | if (num <= 0) | ||
152 | return NULL; | ||
153 | |||
154 | return realloc(ptr, num); | ||
155 | } | ||
156 | |||
157 | void * | ||
158 | CRYPTO_realloc_clean(void *ptr, int old_len, int num, const char *file, | ||
159 | int line) | ||
160 | { | ||
161 | void *ret = NULL; | ||
162 | |||
163 | if (num <= 0) | ||
164 | return NULL; | ||
165 | if (num < old_len) | ||
166 | return NULL; /* original does not support shrinking */ | ||
167 | ret = malloc(num); | ||
168 | if (ret && ptr && old_len > 0) { | ||
169 | memcpy(ret, ptr, old_len); | ||
170 | explicit_bzero(ptr, old_len); | ||
171 | free(ptr); | ||
172 | } | ||
173 | return ret; | ||
174 | } | ||
175 | |||
176 | void | ||
177 | CRYPTO_free(void *ptr) | ||
178 | { | ||
179 | free(ptr); | ||
180 | } | ||
181 | |||
182 | void * | ||
183 | CRYPTO_remalloc(void *a, int num, const char *file, int line) | ||
184 | { | ||
185 | free(a); | ||
186 | return malloc(num); | ||
187 | } | ||
188 | |||
189 | void | ||
190 | CRYPTO_set_mem_debug_options(long bits) | ||
191 | { | ||
192 | return; | ||
193 | } | ||
194 | |||
195 | long | ||
196 | CRYPTO_get_mem_debug_options(void) | ||
197 | { | ||
198 | return 0; | ||
199 | } | ||
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 | } | ||
diff --git a/src/lib/libssl/src/crypto/malloc-wrapper.c b/src/lib/libssl/src/crypto/malloc-wrapper.c new file mode 100644 index 0000000000..49d7c572c8 --- /dev/null +++ b/src/lib/libssl/src/crypto/malloc-wrapper.c | |||
@@ -0,0 +1,199 @@ | |||
1 | /* $OpenBSD: malloc-wrapper.c,v 1.1 2014/04/16 03:24:53 beck Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2014 Bob Beck | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | */ | ||
17 | #include <stdio.h> | ||
18 | #include <stdlib.h> | ||
19 | #include <string.h> | ||
20 | |||
21 | int | ||
22 | CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), | ||
23 | void (*f)(void *)) | ||
24 | { | ||
25 | return 0; | ||
26 | } | ||
27 | |||
28 | int | ||
29 | CRYPTO_set_mem_ex_functions(void *(*m)(size_t, const char *, int), | ||
30 | void *(*r)(void *, size_t, const char *, int), void (*f)(void *)) | ||
31 | { | ||
32 | return 0; | ||
33 | } | ||
34 | |||
35 | int | ||
36 | CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) | ||
37 | { | ||
38 | return 0; | ||
39 | } | ||
40 | |||
41 | int | ||
42 | CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t, const char *, int), | ||
43 | void (*f)(void *)) | ||
44 | { | ||
45 | return 0; | ||
46 | } | ||
47 | |||
48 | int | ||
49 | CRYPTO_set_mem_debug_functions(void (*m)(void *, int, const char *, int, int), | ||
50 | void (*r)(void *, void *, int, const char *, int, int), | ||
51 | void (*f)(void *, int), void (*so)(long), long (*go)(void)) | ||
52 | { | ||
53 | return 0; | ||
54 | } | ||
55 | |||
56 | |||
57 | void | ||
58 | CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), | ||
59 | void (**f)(void *)) | ||
60 | { | ||
61 | if (m != NULL) | ||
62 | *m = malloc; | ||
63 | if (r != NULL) | ||
64 | *r = realloc; | ||
65 | if (f != NULL) | ||
66 | *f = free; | ||
67 | } | ||
68 | |||
69 | void | ||
70 | CRYPTO_get_mem_ex_functions(void *(**m)(size_t, const char *, int), | ||
71 | void *(**r)(void *, size_t, const char *, int), void (**f)(void *)) | ||
72 | { | ||
73 | if (m != NULL) | ||
74 | *m = NULL; | ||
75 | if (r != NULL) | ||
76 | *r = NULL; | ||
77 | if (f != NULL) | ||
78 | *f = free; | ||
79 | } | ||
80 | |||
81 | void | ||
82 | CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) | ||
83 | { | ||
84 | if (m != NULL) | ||
85 | *m = malloc; | ||
86 | if (f != NULL) | ||
87 | *f = free; | ||
88 | } | ||
89 | |||
90 | void | ||
91 | CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t, const char *, int), | ||
92 | void (**f)(void *)) | ||
93 | { | ||
94 | if (m != NULL) | ||
95 | *m = NULL; | ||
96 | if (f != NULL) | ||
97 | *f = free; | ||
98 | } | ||
99 | |||
100 | void | ||
101 | CRYPTO_get_mem_debug_functions(void (**m)(void *, int, const char *, int, int), | ||
102 | void (**r)(void *, void *, int, const char *, int, int), | ||
103 | void (**f)(void *, int), void (**so)(long), long (**go)(void)) | ||
104 | { | ||
105 | if (m != NULL) | ||
106 | *m = NULL; | ||
107 | if (r != NULL) | ||
108 | *r = NULL; | ||
109 | if (f != NULL) | ||
110 | *f = NULL; | ||
111 | if (so != NULL) | ||
112 | *so = NULL; | ||
113 | if (go != NULL) | ||
114 | *go = NULL; | ||
115 | } | ||
116 | |||
117 | |||
118 | void * | ||
119 | CRYPTO_malloc_locked(int num, const char *file, int line) | ||
120 | { | ||
121 | void *ret = NULL; | ||
122 | |||
123 | if (num <= 0) | ||
124 | return NULL; | ||
125 | return malloc(num); | ||
126 | } | ||
127 | |||
128 | void | ||
129 | CRYPTO_free_locked(void *ptr) | ||
130 | { | ||
131 | free(ptr); | ||
132 | } | ||
133 | |||
134 | void * | ||
135 | CRYPTO_malloc(int num, const char *file, int line) | ||
136 | { | ||
137 | if (num <= 0) | ||
138 | return NULL; | ||
139 | return malloc(num); | ||
140 | } | ||
141 | |||
142 | char * | ||
143 | CRYPTO_strdup(const char *str, const char *file, int line) | ||
144 | { | ||
145 | return strdup(str); | ||
146 | } | ||
147 | |||
148 | void * | ||
149 | CRYPTO_realloc(void *ptr, int num, const char *file, int line) | ||
150 | { | ||
151 | if (num <= 0) | ||
152 | return NULL; | ||
153 | |||
154 | return realloc(ptr, num); | ||
155 | } | ||
156 | |||
157 | void * | ||
158 | CRYPTO_realloc_clean(void *ptr, int old_len, int num, const char *file, | ||
159 | int line) | ||
160 | { | ||
161 | void *ret = NULL; | ||
162 | |||
163 | if (num <= 0) | ||
164 | return NULL; | ||
165 | if (num < old_len) | ||
166 | return NULL; /* original does not support shrinking */ | ||
167 | ret = malloc(num); | ||
168 | if (ret && ptr && old_len > 0) { | ||
169 | memcpy(ret, ptr, old_len); | ||
170 | explicit_bzero(ptr, old_len); | ||
171 | free(ptr); | ||
172 | } | ||
173 | return ret; | ||
174 | } | ||
175 | |||
176 | void | ||
177 | CRYPTO_free(void *ptr) | ||
178 | { | ||
179 | free(ptr); | ||
180 | } | ||
181 | |||
182 | void * | ||
183 | CRYPTO_remalloc(void *a, int num, const char *file, int line) | ||
184 | { | ||
185 | free(a); | ||
186 | return malloc(num); | ||
187 | } | ||
188 | |||
189 | void | ||
190 | CRYPTO_set_mem_debug_options(long bits) | ||
191 | { | ||
192 | return; | ||
193 | } | ||
194 | |||
195 | long | ||
196 | CRYPTO_get_mem_debug_options(void) | ||
197 | { | ||
198 | return 0; | ||
199 | } | ||
diff --git a/src/lib/libssl/src/crypto/mem.c b/src/lib/libssl/src/crypto/mem.c deleted file mode 100644 index 548c248785..0000000000 --- a/src/lib/libssl/src/crypto/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 | } | ||