mem.c revision 111147
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.
855714Skris *
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).
1555714Skris *
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.
2255714Skris *
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 :-).
3755714Skris * 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)"
4055714Skris *
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.
5255714Skris *
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
6455714Skris
6559191Skrisstatic int allow_customize = 1;      /* we provide flexible functions for */
6659191Skrisstatic int allow_customize_debug = 1;/* exchanging memory-related functions at
6759191Skris                                      * run-time, but this must be done
6859191Skris                                      * before any blocks are actually
6959191Skris                                      * allocated; or we'll run into huge
7059191Skris                                      * problems when malloc/free pairs
7159191Skris                                      * don't match etc. */
7255714Skris
73109998Smarkm
74109998Smarkm
75109998Smarkm/* the following pointers may be changed as long as 'allow_customize' is set */
76109998Smarkm
7759191Skrisstatic void *(*malloc_func)(size_t)         = malloc;
78109998Smarkmstatic void *default_malloc_ex(size_t num, const char *file, int line)
79109998Smarkm	{ return malloc_func(num); }
80109998Smarkmstatic void *(*malloc_ex_func)(size_t, const char *file, int line)
81109998Smarkm        = default_malloc_ex;
82109998Smarkm
8359191Skrisstatic void *(*realloc_func)(void *, size_t)= realloc;
84109998Smarkmstatic void *default_realloc_ex(void *str, size_t num,
85109998Smarkm        const char *file, int line)
86109998Smarkm	{ return realloc_func(str,num); }
87109998Smarkmstatic void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
88109998Smarkm        = default_realloc_ex;
89109998Smarkm
9059191Skrisstatic void (*free_func)(void *)            = free;
9155714Skris
92109998Smarkmstatic void *(*malloc_locked_func)(size_t)  = malloc;
93109998Smarkmstatic void *default_malloc_locked_ex(size_t num, const char *file, int line)
94109998Smarkm	{ return malloc_locked_func(num); }
95109998Smarkmstatic void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
96109998Smarkm        = default_malloc_locked_ex;
97109998Smarkm
98109998Smarkmstatic void (*free_locked_func)(void *)     = free;
99109998Smarkm
100109998Smarkm
101109998Smarkm
102109998Smarkm/* may be changed as long as 'allow_customize_debug' is set */
10359191Skris/* XXX use correct function pointer types */
10459191Skris#ifdef CRYPTO_MDEBUG
10568651Skris/* use default functions from mem_dbg.c */
10668651Skrisstatic void (*malloc_debug_func)(void *,int,const char *,int,int)
10768651Skris	= CRYPTO_dbg_malloc;
10868651Skrisstatic void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
10968651Skris	= CRYPTO_dbg_realloc;
11068651Skrisstatic void (*free_debug_func)(void *,int) = CRYPTO_dbg_free;
11168651Skrisstatic void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
11268651Skrisstatic long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
11359191Skris#else
11468651Skris/* applications can use CRYPTO_malloc_debug_init() to select above case
11568651Skris * at run-time */
11668651Skrisstatic void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL;
11768651Skrisstatic void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
11868651Skris	= NULL;
11968651Skrisstatic void (*free_debug_func)(void *,int) = NULL;
12068651Skrisstatic void (*set_debug_options_func)(long) = NULL;
12168651Skrisstatic long (*get_debug_options_func)(void) = NULL;
12255714Skris#endif
12355714Skris
12455714Skris
12559191Skrisint CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
12659191Skris	void (*f)(void *))
12755714Skris	{
12859191Skris	if (!allow_customize)
12959191Skris		return 0;
130109998Smarkm	if ((m == 0) || (r == 0) || (f == 0))
13159191Skris		return 0;
132109998Smarkm	malloc_func=m; malloc_ex_func=default_malloc_ex;
133109998Smarkm	realloc_func=r; realloc_ex_func=default_realloc_ex;
13455714Skris	free_func=f;
135109998Smarkm	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
13655714Skris	free_locked_func=f;
13759191Skris	return 1;
13855714Skris	}
13955714Skris
140109998Smarkmint CRYPTO_set_mem_ex_functions(
141109998Smarkm        void *(*m)(size_t,const char *,int),
142109998Smarkm        void *(*r)(void *, size_t,const char *,int),
143109998Smarkm	void (*f)(void *))
144109998Smarkm	{
145109998Smarkm	if (!allow_customize)
146109998Smarkm		return 0;
147109998Smarkm	if ((m == 0) || (r == 0) || (f == 0))
148109998Smarkm		return 0;
149109998Smarkm	malloc_func=0; malloc_ex_func=m;
150109998Smarkm	realloc_func=0; realloc_ex_func=r;
151109998Smarkm	free_func=f;
152109998Smarkm	malloc_locked_func=0; malloc_locked_ex_func=m;
153109998Smarkm	free_locked_func=f;
154109998Smarkm	return 1;
155109998Smarkm	}
156109998Smarkm
15759191Skrisint CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
15855714Skris	{
15959191Skris	if (!allow_customize)
16059191Skris		return 0;
16159191Skris	if ((m == NULL) || (f == NULL))
16259191Skris		return 0;
163109998Smarkm	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
16455714Skris	free_locked_func=f;
16559191Skris	return 1;
16655714Skris	}
16755714Skris
168109998Smarkmint CRYPTO_set_locked_mem_ex_functions(
169109998Smarkm        void *(*m)(size_t,const char *,int),
170109998Smarkm        void (*f)(void *))
171109998Smarkm	{
172109998Smarkm	if (!allow_customize)
173109998Smarkm		return 0;
174109998Smarkm	if ((m == NULL) || (f == NULL))
175109998Smarkm		return 0;
176109998Smarkm	malloc_locked_func=0; malloc_locked_ex_func=m;
177109998Smarkm	free_func=f;
178109998Smarkm	return 1;
179109998Smarkm	}
180109998Smarkm
18168651Skrisint CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
18268651Skris				   void (*r)(void *,void *,int,const char *,int,int),
18368651Skris				   void (*f)(void *,int),
18468651Skris				   void (*so)(long),
18568651Skris				   long (*go)(void))
18655714Skris	{
18759191Skris	if (!allow_customize_debug)
18859191Skris		return 0;
18959191Skris	malloc_debug_func=m;
19059191Skris	realloc_debug_func=r;
19159191Skris	free_debug_func=f;
19259191Skris	set_debug_options_func=so;
19359191Skris	get_debug_options_func=go;
19459191Skris	return 1;
19559191Skris	}
19659191Skris
197109998Smarkm
19859191Skrisvoid CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
19959191Skris	void (**f)(void *))
20059191Skris	{
201109998Smarkm	if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ?
202109998Smarkm	                     malloc_func : 0;
203109998Smarkm	if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ?
204109998Smarkm	                     realloc_func : 0;
20555714Skris	if (f != NULL) *f=free_func;
20655714Skris	}
20755714Skris
208109998Smarkmvoid CRYPTO_get_mem_ex_functions(
209109998Smarkm        void *(**m)(size_t,const char *,int),
210109998Smarkm        void *(**r)(void *, size_t,const char *,int),
211109998Smarkm	void (**f)(void *))
212109998Smarkm	{
213109998Smarkm	if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ?
214109998Smarkm	                    malloc_ex_func : 0;
215109998Smarkm	if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ?
216109998Smarkm	                    realloc_ex_func : 0;
217109998Smarkm	if (f != NULL) *f=free_func;
218109998Smarkm	}
219109998Smarkm
22059191Skrisvoid CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
22155714Skris	{
222109998Smarkm	if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ?
223109998Smarkm	                     malloc_locked_func : 0;
22455714Skris	if (f != NULL) *f=free_locked_func;
22555714Skris	}
22655714Skris
227109998Smarkmvoid CRYPTO_get_locked_mem_ex_functions(
228109998Smarkm        void *(**m)(size_t,const char *,int),
229109998Smarkm        void (**f)(void *))
230109998Smarkm	{
231109998Smarkm	if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
232109998Smarkm	                    malloc_locked_ex_func : 0;
233109998Smarkm	if (f != NULL) *f=free_locked_func;
234109998Smarkm	}
235109998Smarkm
23668651Skrisvoid CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
23768651Skris				    void (**r)(void *,void *,int,const char *,int,int),
23868651Skris				    void (**f)(void *,int),
23968651Skris				    void (**so)(long),
24068651Skris				    long (**go)(void))
24155714Skris	{
24259191Skris	if (m != NULL) *m=malloc_debug_func;
24359191Skris	if (r != NULL) *r=realloc_debug_func;
24459191Skris	if (f != NULL) *f=free_debug_func;
24559191Skris	if (so != NULL) *so=set_debug_options_func;
24659191Skris	if (go != NULL) *go=get_debug_options_func;
24755714Skris	}
24855714Skris
24955714Skris
25059191Skrisvoid *CRYPTO_malloc_locked(int num, const char *file, int line)
25155714Skris	{
25268651Skris	void *ret = NULL;
253109998Smarkm	extern unsigned char cleanse_ctr;
25455714Skris
255111147Snectar	if (num < 0) return NULL;
256111147Snectar
25759191Skris	allow_customize = 0;
25859191Skris	if (malloc_debug_func != NULL)
25959191Skris		{
26059191Skris		allow_customize_debug = 0;
26159191Skris		malloc_debug_func(NULL, num, file, line, 0);
26259191Skris		}
263109998Smarkm	ret = malloc_locked_ex_func(num,file,line);
264109998Smarkm#ifdef LEVITTE_DEBUG_MEM
265109998Smarkm	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
26659191Skris#endif
26759191Skris	if (malloc_debug_func != NULL)
26859191Skris		malloc_debug_func(ret, num, file, line, 1);
26955714Skris
270109998Smarkm        /* Create a dependency on the value of 'cleanse_ctr' so our memory
271109998Smarkm         * sanitisation function can't be optimised out. NB: We only do
272109998Smarkm         * this for >2Kb so the overhead doesn't bother us. */
273109998Smarkm        if(ret && (num > 2048))
274109998Smarkm		((unsigned char *)ret)[0] = cleanse_ctr;
275109998Smarkm
27659191Skris	return ret;
27755714Skris	}
27855714Skris
27959191Skrisvoid CRYPTO_free_locked(void *str)
28055714Skris	{
28159191Skris	if (free_debug_func != NULL)
28259191Skris		free_debug_func(str, 0);
283109998Smarkm#ifdef LEVITTE_DEBUG_MEM
284109998Smarkm	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
28555714Skris#endif
28659191Skris	free_locked_func(str);
28759191Skris	if (free_debug_func != NULL)
28859191Skris		free_debug_func(NULL, 1);
28955714Skris	}
29055714Skris
29159191Skrisvoid *CRYPTO_malloc(int num, const char *file, int line)
29255714Skris	{
29368651Skris	void *ret = NULL;
294109998Smarkm	extern unsigned char cleanse_ctr;
29555714Skris
296111147Snectar	if (num < 0) return NULL;
297111147Snectar
29859191Skris	allow_customize = 0;
29959191Skris	if (malloc_debug_func != NULL)
30055714Skris		{
30159191Skris		allow_customize_debug = 0;
30259191Skris		malloc_debug_func(NULL, num, file, line, 0);
30355714Skris		}
304109998Smarkm	ret = malloc_ex_func(num,file,line);
305109998Smarkm#ifdef LEVITTE_DEBUG_MEM
306109998Smarkm	fprintf(stderr, "LEVITTE_DEBUG_MEM:         > 0x%p (%d)\n", ret, num);
30759191Skris#endif
30859191Skris	if (malloc_debug_func != NULL)
30959191Skris		malloc_debug_func(ret, num, file, line, 1);
31055714Skris
311109998Smarkm        /* Create a dependency on the value of 'cleanse_ctr' so our memory
312109998Smarkm         * sanitisation function can't be optimised out. NB: We only do
313109998Smarkm         * this for >2Kb so the overhead doesn't bother us. */
314109998Smarkm        if(ret && (num > 2048))
315109998Smarkm                ((unsigned char *)ret)[0] = cleanse_ctr;
316109998Smarkm
31759191Skris	return ret;
31855714Skris	}
31955714Skris
32059191Skrisvoid *CRYPTO_realloc(void *str, int num, const char *file, int line)
32155714Skris	{
32268651Skris	void *ret = NULL;
32355714Skris
324101613Snectar	if (str == NULL)
325101613Snectar		return CRYPTO_malloc(num, file, line);
326111147Snectar
327111147Snectar 	if (num < 0) return NULL;
328111147Snectar
329109998Smarkm	if (realloc_debug_func != NULL)
330109998Smarkm		realloc_debug_func(str, NULL, num, file, line, 0);
331109998Smarkm	ret = realloc_ex_func(str,num,file,line);
332109998Smarkm#ifdef LEVITTE_DEBUG_MEM
333109998Smarkm	fprintf(stderr, "LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n", str, ret, num);
334109998Smarkm#endif
335109998Smarkm	if (realloc_debug_func != NULL)
336109998Smarkm		realloc_debug_func(str, ret, num, file, line, 1);
337101613Snectar
338109998Smarkm	return ret;
339109998Smarkm	}
340109998Smarkm
341109998Smarkmvoid *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
342109998Smarkm			   int line)
343109998Smarkm	{
344109998Smarkm	void *ret = NULL;
345109998Smarkm
346109998Smarkm	if (str == NULL)
347109998Smarkm		return CRYPTO_malloc(num, file, line);
348111147Snectar
349111147Snectar 	if (num < 0) return NULL;
350111147Snectar
35159191Skris	if (realloc_debug_func != NULL)
35259191Skris		realloc_debug_func(str, NULL, num, file, line, 0);
353109998Smarkm	ret=malloc_ex_func(num,file,line);
354109998Smarkm	if(ret)
355109998Smarkm		memcpy(ret,str,old_len);
356109998Smarkm	OPENSSL_cleanse(str,old_len);
357109998Smarkm	free_func(str);
358109998Smarkm#ifdef LEVITTE_DEBUG_MEM
359109998Smarkm	fprintf(stderr, "LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n", str, ret, num);
36055714Skris#endif
36159191Skris	if (realloc_debug_func != NULL)
36259191Skris		realloc_debug_func(str, ret, num, file, line, 1);
36355714Skris
36459191Skris	return ret;
36555714Skris	}
36655714Skris
36759191Skrisvoid CRYPTO_free(void *str)
36855714Skris	{
36959191Skris	if (free_debug_func != NULL)
37059191Skris		free_debug_func(str, 0);
371109998Smarkm#ifdef LEVITTE_DEBUG_MEM
372109998Smarkm	fprintf(stderr, "LEVITTE_DEBUG_MEM:         < 0x%p\n", str);
37355714Skris#endif
37459191Skris	free_func(str);
37559191Skris	if (free_debug_func != NULL)
37659191Skris		free_debug_func(NULL, 1);
37755714Skris	}
37855714Skris
37959191Skrisvoid *CRYPTO_remalloc(void *a, int num, const char *file, int line)
38055714Skris	{
38168651Skris	if (a != NULL) OPENSSL_free(a);
38268651Skris	a=(char *)OPENSSL_malloc(num);
38359191Skris	return(a);
38455714Skris	}
38555714Skris
38659191Skrisvoid CRYPTO_set_mem_debug_options(long bits)
38755714Skris	{
38859191Skris	if (set_debug_options_func != NULL)
38959191Skris		set_debug_options_func(bits);
39055714Skris	}
39155714Skris
39259191Skrislong CRYPTO_get_mem_debug_options(void)
39355714Skris	{
39459191Skris	if (get_debug_options_func != NULL)
39559191Skris		return get_debug_options_func();
39659191Skris	return 0;
39755714Skris	}
398