First public contribution.
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
59 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
65 #include <openssl/crypto.h>
67 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
68 #include "libcrypto_wsd_macros.h"
69 #include "libcrypto_wsd.h"
73 static int allow_customize = 1; /* we provide flexible functions for */
74 static int allow_customize_debug = 1;/* exchanging memory-related functions at
75 * run-time, but this must be done
76 * before any blocks are actually
77 * allocated; or we'll run into huge
78 * problems when malloc/free pairs
81 GET_STATIC_VAR_FROM_TLS(allow_customize,mem,int)
82 #define allow_customize (*GET_WSD_VAR_NAME(allow_customize,mem,s)())
83 GET_STATIC_VAR_FROM_TLS(allow_customize_debug,mem,int)
84 #define allow_customize_debug (*GET_WSD_VAR_NAME(allow_customize_debug,mem,s)())
88 /* the following pointers may be changed as long as 'allow_customize' is set */
90 typedef void *(*malloc_func_t)(size_t);
91 static void *(*malloc_func)(size_t) = NULL;
92 static malloc_func_t const malloc_func_def = malloc;
93 static void *default_malloc_ex(size_t num, const char *file, int line)
96 return malloc_func(num);
98 return malloc_func_def(num);
101 #define malloc_func libcrypto_ImpurePtr()->malloc_func
102 void *default_malloc_ex(size_t num, const char *file, int line)
103 { return malloc_func(num); }
106 static void *(*malloc_ex_func)(size_t, const char *file, int line)
109 #define malloc_ex_func libcrypto_ImpurePtr()->malloc_ex_func
113 typedef void *(*realloc_func_t)(void *, size_t);
114 static void *(*realloc_func)(void *, size_t)= NULL;
115 static realloc_func_t const realloc_func_def = realloc ;
116 static void *default_realloc_ex(void *str, size_t num,
117 const char *file, int line)
120 return realloc_func(str,num);
122 return realloc_func_def(str,num);
125 #define realloc_func libcrypto_ImpurePtr()->realloc_func
126 void *default_realloc_ex(void *str, size_t num,
127 const char *file, int line)
128 { return realloc_func(str,num); }
133 static void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
134 = default_realloc_ex;
136 #define realloc_ex_func libcrypto_ImpurePtr()->realloc_ex_func
140 typedef void (*free_func_t)(void *);
141 static void (*free_func)(void *) = NULL;
142 static free_func_t const free_func_def = free;
144 #define free_func_openssl libcrypto_ImpurePtr()->free_func_openssl
148 typedef void *(*malloc_locked_func_t)(size_t);
149 static void *(*malloc_locked_func)(size_t) = NULL;
150 static malloc_locked_func_t const malloc_locked_func_def = malloc;
152 #define malloc_locked_func libcrypto_ImpurePtr()->malloc_locked_func
156 static void *default_malloc_locked_ex(size_t num, const char *file, int line)
158 if(malloc_locked_func)
159 return malloc_locked_func(num);
161 return malloc_locked_func_def(num);
164 static void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
165 = default_malloc_locked_ex;
167 typedef void (*free_locked_func_t)(void *);
168 static void (*free_locked_func)(void *) = NULL;
169 static free_locked_func_t const free_locked_func_def = free;
171 void *default_malloc_locked_ex(size_t num, const char *file, int line)
172 { return malloc_locked_func(num); }
174 #define malloc_locked_ex_func libcrypto_ImpurePtr()->malloc_locked_ex_func
175 #define free_locked_func libcrypto_ImpurePtr()->free_locked_func
181 /* may be changed as long as 'allow_customize_debug' is set */
182 /* XXX use correct function pointer types */
184 /* use default functions from mem_dbg.c */
186 static void (*malloc_debug_func)(void *,int,const char *,int,int)
188 static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
189 = CRYPTO_dbg_realloc;
190 static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free;
191 static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options;
192 static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options;
194 #define malloc_debug_func libcrypto_ImpurePtr()->malloc_debug_func
195 #define realloc_debug_func libcrypto_ImpurePtr()->realloc_debug_func
196 #define free_debug_func libcrypto_ImpurePtr()->free_debug_func
197 #define set_debug_options_func libcrypto_ImpurePtr()->set_debug_options_func
198 #define get_debug_options_func libcrypto_ImpurePtr()->get_debug_options_func
202 /* applications can use CRYPTO_malloc_debug_init() to select above case
204 static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL;
205 static void (*realloc_debug_func)(void *,void *,int,const char *,int,int)
207 static void (*free_debug_func)(void *,int) = NULL;
208 static void (*set_debug_options_func)(long) = NULL;
209 static long (*get_debug_options_func)(void) = NULL;
213 EXPORT_C int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
216 if (!allow_customize)
218 if ((m == 0) || (r == 0) || (f == 0))
220 malloc_func=m; malloc_ex_func=default_malloc_ex;
221 realloc_func=r; realloc_ex_func=default_realloc_ex;
227 malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
232 EXPORT_C int CRYPTO_set_mem_ex_functions(
233 void *(*m)(size_t,const char *,int),
234 void *(*r)(void *, size_t,const char *,int),
237 if (!allow_customize)
239 if ((m == 0) || (r == 0) || (f == 0))
241 malloc_func=0; malloc_ex_func=m;
242 realloc_func=0; realloc_ex_func=r;
248 malloc_locked_func=0; malloc_locked_ex_func=m;
253 EXPORT_C int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
255 if (!allow_customize)
257 if ((m == NULL) || (f == NULL))
259 malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
264 EXPORT_C int CRYPTO_set_locked_mem_ex_functions(
265 void *(*m)(size_t,const char *,int),
268 if (!allow_customize)
270 if ((m == NULL) || (f == NULL))
272 malloc_locked_func=0; malloc_locked_ex_func=m;
281 EXPORT_C int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int),
282 void (*r)(void *,void *,int,const char *,int,int),
283 void (*f)(void *,int),
287 if (!allow_customize_debug)
290 realloc_debug_func=r;
292 set_debug_options_func=so;
293 get_debug_options_func=go;
298 EXPORT_C void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
301 if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ?
303 (malloc_func!=NULL?malloc_func:malloc_func_def) : 0;
308 if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ?
310 (realloc_func!=NULL?realloc_func:realloc_func_def) : 0;
316 if (f != NULL) *f=(free_func?free_func:free_func_def);
318 if (f != NULL) *f=free_func_openssl;
322 EXPORT_C void CRYPTO_get_mem_ex_functions(
323 void *(**m)(size_t,const char *,int),
324 void *(**r)(void *, size_t,const char *,int),
327 if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ?
329 if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ?
332 if (f != NULL) *f=(free_func?free_func:free_func_def);
334 if (f != NULL) *f=free_func_openssl;
338 EXPORT_C void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
340 if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ?
342 (malloc_locked_func?malloc_locked_func:malloc_locked_func_def) : 0;
344 malloc_locked_func : 0;
347 if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def);
349 if (f != NULL) *f=free_locked_func;
353 EXPORT_C void CRYPTO_get_locked_mem_ex_functions(
354 void *(**m)(size_t,const char *,int),
357 if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
358 malloc_locked_ex_func : 0;
360 if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def);
362 if (f != NULL) *f= free_locked_func;
366 EXPORT_C void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int),
367 void (**r)(void *,void *,int,const char *,int,int),
368 void (**f)(void *,int),
372 if (m != NULL) *m=malloc_debug_func;
373 if (r != NULL) *r=realloc_debug_func;
374 if (f != NULL) *f=free_debug_func;
375 if (so != NULL) *so=set_debug_options_func;
376 if (go != NULL) *go=get_debug_options_func;
381 unsigned char GET_WSD_VAR_NAME(cleanse_ctr,mem_clr, g)();
382 #define cleanse_ctr (*GET_WSD_VAR_NAME(cleanse_ctr,mem_clr, g)())
385 EXPORT_C void *CRYPTO_malloc_locked(int num, const char *file, int line)
388 extern unsigned char cleanse_ctr;
390 if (num <= 0) return NULL;
393 if (malloc_debug_func != NULL)
395 allow_customize_debug = 0;
396 malloc_debug_func(NULL, num, file, line, 0);
398 ret = malloc_locked_ex_func(num,file,line);
399 #ifdef LEVITTE_DEBUG_MEM
400 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
402 if (malloc_debug_func != NULL)
403 malloc_debug_func(ret, num, file, line, 1);
405 /* Create a dependency on the value of 'cleanse_ctr' so our memory
406 * sanitisation function can't be optimised out. NB: We only do
407 * this for >2Kb so the overhead doesn't bother us. */
408 if(ret && (num > 2048))
409 ((unsigned char *)ret)[0] = cleanse_ctr;
414 EXPORT_C void CRYPTO_free_locked(void *str)
416 if (free_debug_func != NULL)
417 free_debug_func(str, 0);
418 #ifdef LEVITTE_DEBUG_MEM
419 fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str);
422 free_locked_func?free_locked_func(str):free_locked_func_def(str);
424 free_locked_func(str);
426 if (free_debug_func != NULL)
427 free_debug_func(NULL, 1);
432 EXPORT_C void *CRYPTO_malloc(int num, const char *file, int line)
435 extern unsigned char cleanse_ctr;
437 if (num <= 0) return NULL;
440 if (malloc_debug_func != NULL)
442 allow_customize_debug = 0;
443 malloc_debug_func(NULL, num, file, line, 0);
445 ret = malloc_ex_func(num,file,line);
446 #ifdef LEVITTE_DEBUG_MEM
447 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num);
449 if (malloc_debug_func != NULL)
450 malloc_debug_func(ret, num, file, line, 1);
452 /* Create a dependency on the value of 'cleanse_ctr' so our memory
453 * sanitisation function can't be optimised out. NB: We only do
454 * this for >2Kb so the overhead doesn't bother us. */
455 if(ret && (num > 2048))
456 ((unsigned char *)ret)[0] = cleanse_ctr;
461 EXPORT_C void *CRYPTO_realloc(void *str, int num, const char *file, int line)
466 return CRYPTO_malloc(num, file, line);
468 if (num <= 0) return NULL;
470 if (realloc_debug_func != NULL)
471 realloc_debug_func(str, NULL, num, file, line, 0);
472 ret = realloc_ex_func(str,num,file,line);
473 #ifdef LEVITTE_DEBUG_MEM
474 fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num);
476 if (realloc_debug_func != NULL)
477 realloc_debug_func(str, ret, num, file, line, 1);
482 EXPORT_C void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
488 return CRYPTO_malloc(num, file, line);
490 if (num <= 0) return NULL;
492 if (realloc_debug_func != NULL)
493 realloc_debug_func(str, NULL, num, file, line, 0);
494 ret=malloc_ex_func(num,file,line);
497 memcpy(ret,str,old_len);
498 OPENSSL_cleanse(str,old_len);
500 free_func?free_func(str):free_func_def(str);
502 free_func_openssl(str);
505 #ifdef LEVITTE_DEBUG_MEM
507 "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n",
510 if (realloc_debug_func != NULL)
511 realloc_debug_func(str, ret, num, file, line, 1);
517 EXPORT_C void CRYPTO_free(void *str)
519 if (free_debug_func != NULL)
520 free_debug_func(str, 0);
521 #ifdef LEVITTE_DEBUG_MEM
522 fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str);
525 free_func?free_func(str):free_func_def(str);
527 free_func_openssl(str);
529 if (free_debug_func != NULL)
530 free_debug_func(NULL, 1);
534 EXPORT_C void *CRYPTO_remalloc(void *a, int num, const char *file, int line)
536 if (a != NULL) OPENSSL_free(a);
537 a=(char *)OPENSSL_malloc(num);
541 EXPORT_C void CRYPTO_set_mem_debug_options(long bits)
543 if (set_debug_options_func != NULL)
544 set_debug_options_func(bits);
547 EXPORT_C long CRYPTO_get_mem_debug_options(void)
549 if (get_debug_options_func != NULL)
550 return get_debug_options_func();