os/ossrv/ssl/libcrypto/src/crypto/mem.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    60  */
    61 
    62 
    63 #include <stdio.h>
    64 #include <stdlib.h>
    65 #include <openssl/crypto.h>
    66 #include "cryptlib.h"
    67 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    68 #include "libcrypto_wsd_macros.h"
    69 #include "libcrypto_wsd.h"
    70 #endif
    71 
    72 #ifndef EMULATOR
    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
    79                                       * don't match etc. */
    80 #else
    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)())
    85 #endif
    86 
    87 
    88 /* the following pointers may be changed as long as 'allow_customize' is set */
    89 #ifndef EMULATOR
    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)
    94 	{ 
    95 		if(malloc_func)
    96 			return malloc_func(num); 
    97 		else
    98 			return malloc_func_def(num);
    99 	}
   100 #else
   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); }
   104 #endif
   105 #ifndef EMULATOR
   106 static void *(*malloc_ex_func)(size_t, const char *file, int line)
   107         = default_malloc_ex;
   108 #else
   109 #define malloc_ex_func libcrypto_ImpurePtr()->malloc_ex_func        
   110 #endif        
   111 
   112 #ifndef EMULATOR
   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)
   118 	{ 
   119 		if(realloc_func)
   120 			return realloc_func(str,num); 
   121 		else
   122 			return realloc_func_def(str,num);		 
   123 	}
   124 #else
   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); }
   129 #endif
   130 
   131 
   132 #ifndef EMULATOR	
   133 static void *(*realloc_ex_func)(void *, size_t, const char *file, int line)
   134         = default_realloc_ex;
   135 #else
   136 #define realloc_ex_func libcrypto_ImpurePtr()->realloc_ex_func
   137 #endif        
   138 
   139 #ifndef EMULATOR
   140 typedef void (*free_func_t)(void *);
   141 static void (*free_func)(void *)            = NULL;
   142 static free_func_t const free_func_def = free;
   143 #else
   144 #define free_func_openssl libcrypto_ImpurePtr()->free_func_openssl
   145 #endif
   146 
   147 #ifndef EMULATOR
   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;
   151 #else
   152 #define  malloc_locked_func libcrypto_ImpurePtr()->malloc_locked_func
   153 #endif
   154 
   155 #ifndef EMULATOR	
   156 static void *default_malloc_locked_ex(size_t num, const char *file, int line)
   157 	{ 
   158 		if(malloc_locked_func)
   159 			return malloc_locked_func(num); 
   160 		else
   161 			return malloc_locked_func_def(num);
   162 		}
   163 
   164 static void *(*malloc_locked_ex_func)(size_t, const char *file, int line)
   165         = default_malloc_locked_ex;
   166     
   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;
   170 #else
   171 void *default_malloc_locked_ex(size_t num, const char *file, int line)
   172 	{ return malloc_locked_func(num); }
   173 
   174 #define malloc_locked_ex_func libcrypto_ImpurePtr()->malloc_locked_ex_func
   175 #define free_locked_func libcrypto_ImpurePtr()->free_locked_func
   176 #endif
   177 
   178 
   179 
   180 
   181 /* may be changed as long as 'allow_customize_debug' is set */
   182 /* XXX use correct function pointer types */
   183 #ifdef CRYPTO_MDEBUG
   184 /* use default functions from mem_dbg.c */
   185 #ifndef EMULATOR
   186 static void (*malloc_debug_func)(void *,int,const char *,int,int)
   187 	= CRYPTO_dbg_malloc;
   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;
   193 #else
   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
   199 
   200 #endif
   201 #else
   202 /* applications can use CRYPTO_malloc_debug_init() to select above case
   203  * at run-time */
   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)
   206 	= NULL;
   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;
   210 #endif
   211 
   212 
   213 EXPORT_C int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t),
   214 	void (*f)(void *))
   215 	{
   216 	if (!allow_customize)
   217 		return 0;
   218 	if ((m == 0) || (r == 0) || (f == 0))
   219 		return 0;
   220 	malloc_func=m; malloc_ex_func=default_malloc_ex;
   221 	realloc_func=r; realloc_ex_func=default_realloc_ex;
   222 #ifndef EMULATOR
   223     free_func=f;
   224 #else	
   225 	free_func_openssl=f;
   226 #endif	
   227 	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
   228 	free_locked_func=f;
   229 	return 1;
   230 	}
   231 
   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),
   235 	void (*f)(void *))
   236 	{
   237 	if (!allow_customize)
   238 		return 0;
   239 	if ((m == 0) || (r == 0) || (f == 0))
   240 		return 0;
   241 	malloc_func=0; malloc_ex_func=m;
   242 	realloc_func=0; realloc_ex_func=r;
   243 #ifndef EMULATOR
   244     free_func=f;
   245 #else	
   246 	free_func_openssl=f;
   247 #endif	
   248 	malloc_locked_func=0; malloc_locked_ex_func=m;
   249 	free_locked_func=f;
   250 	return 1;
   251 	}
   252 
   253 EXPORT_C int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *))
   254 	{
   255 	if (!allow_customize)
   256 		return 0;
   257 	if ((m == NULL) || (f == NULL))
   258 		return 0;
   259 	malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex;
   260 	free_locked_func=f;
   261 	return 1;
   262 	}
   263 
   264 EXPORT_C int CRYPTO_set_locked_mem_ex_functions(
   265         void *(*m)(size_t,const char *,int),
   266         void (*f)(void *))
   267 	{
   268 	if (!allow_customize)
   269 		return 0;
   270 	if ((m == NULL) || (f == NULL))
   271 		return 0;
   272 	malloc_locked_func=0; malloc_locked_ex_func=m;
   273 #ifndef EMULATOR	
   274     free_func=f;
   275 #else
   276 	free_func_openssl=f;
   277 #endif	
   278 	return 1;
   279 	}
   280 
   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),
   284 				   void (*so)(long),
   285 				   long (*go)(void))
   286 	{
   287 	if (!allow_customize_debug)
   288 		return 0;
   289 	malloc_debug_func=m;
   290 	realloc_debug_func=r;
   291 	free_debug_func=f;
   292 	set_debug_options_func=so;
   293 	get_debug_options_func=go;
   294 	return 1;
   295 	}
   296 
   297 
   298 EXPORT_C void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t),
   299 	void (**f)(void *))
   300 	{
   301 	if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? 
   302 #ifndef	EMULATOR	
   303 	                     (malloc_func!=NULL?malloc_func:malloc_func_def) : 0;
   304 #else
   305 											 malloc_func : 0;
   306 #endif
   307 	                     
   308 	if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? 
   309 #ifndef	EMULATOR	
   310 	                     (realloc_func!=NULL?realloc_func:realloc_func_def) : 0;
   311 #else
   312 											 realloc_func : 0;
   313 #endif
   314 	                     
   315 #ifndef EMULATOR	
   316    if (f != NULL) *f=(free_func?free_func:free_func_def);
   317 #else
   318 	if (f != NULL) *f=free_func_openssl;
   319 #endif	
   320 	}
   321 
   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),
   325 	void (**f)(void *))
   326 	{
   327 	if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ?
   328 	                    malloc_ex_func : 0;
   329 	if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ?
   330 	                    realloc_ex_func : 0;
   331 	#ifndef EMULATOR	
   332 	if (f != NULL) *f=(free_func?free_func:free_func_def);
   333 	#else
   334 	if (f != NULL) *f=free_func_openssl;
   335 	#endif
   336 	}
   337 
   338 EXPORT_C void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *))
   339 	{
   340 	if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? 
   341 #ifndef	EMULATOR
   342 	                     (malloc_locked_func?malloc_locked_func:malloc_locked_func_def) : 0;
   343 #else
   344 												malloc_locked_func : 0;
   345 #endif	                     
   346 #ifndef	EMULATOR
   347 	if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def);
   348 #else
   349 	if (f != NULL) *f=free_locked_func;
   350 #endif	
   351 	}
   352 
   353 EXPORT_C void CRYPTO_get_locked_mem_ex_functions(
   354         void *(**m)(size_t,const char *,int),
   355         void (**f)(void *))
   356 	{
   357 	if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ?
   358 	                    malloc_locked_ex_func : 0;
   359 #ifndef	EMULATOR	                    
   360 	if (f != NULL) *f=(free_locked_func?free_locked_func:free_locked_func_def);
   361 #else
   362 	if (f != NULL) *f= free_locked_func;
   363 #endif	
   364 	}
   365 
   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),
   369 				    void (**so)(long),
   370 				    long (**go)(void))
   371 	{
   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;
   377 	}
   378 
   379 #ifdef EMULATOR
   380 
   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)())
   383 #endif
   384 
   385 EXPORT_C void *CRYPTO_malloc_locked(int num, const char *file, int line)
   386 	{
   387 	void *ret = NULL;
   388 	extern unsigned char cleanse_ctr;
   389 
   390 	if (num <= 0) return NULL;
   391 
   392 	allow_customize = 0;
   393 	if (malloc_debug_func != NULL)
   394 		{
   395 		allow_customize_debug = 0;
   396 		malloc_debug_func(NULL, num, file, line, 0);
   397 		}
   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);
   401 #endif
   402 	if (malloc_debug_func != NULL)
   403 		malloc_debug_func(ret, num, file, line, 1);
   404 
   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;
   410 
   411 	return ret;
   412 	}
   413 
   414 EXPORT_C void CRYPTO_free_locked(void *str)
   415 	{
   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);
   420 #endif
   421 #ifndef	EMULATOR
   422 	free_locked_func?free_locked_func(str):free_locked_func_def(str);
   423 #else
   424 	free_locked_func(str);
   425 #endif	
   426 	if (free_debug_func != NULL)
   427 		free_debug_func(NULL, 1);
   428 	}
   429 
   430 
   431 
   432 EXPORT_C void *CRYPTO_malloc(int num, const char *file, int line)
   433 	{
   434 	void *ret = NULL;
   435 	extern unsigned char cleanse_ctr;
   436 
   437 	if (num <= 0) return NULL;
   438 
   439 	allow_customize = 0;
   440 	if (malloc_debug_func != NULL)
   441 		{
   442 		allow_customize_debug = 0;
   443 		malloc_debug_func(NULL, num, file, line, 0);
   444 		}
   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);
   448 #endif
   449 	if (malloc_debug_func != NULL)
   450 		malloc_debug_func(ret, num, file, line, 1);
   451 
   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;
   457 
   458 	return ret;
   459 	}
   460 
   461 EXPORT_C void *CRYPTO_realloc(void *str, int num, const char *file, int line)
   462 	{
   463 	void *ret = NULL;
   464 
   465 	if (str == NULL)
   466 		return CRYPTO_malloc(num, file, line);
   467 
   468 	if (num <= 0) return NULL;
   469 
   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);
   475 #endif
   476 	if (realloc_debug_func != NULL)
   477 		realloc_debug_func(str, ret, num, file, line, 1);
   478 
   479 	return ret;
   480 	}
   481 
   482 EXPORT_C void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file,
   483 			   int line)
   484 	{
   485 	void *ret = NULL;
   486 
   487 	if (str == NULL)
   488 		return CRYPTO_malloc(num, file, line);
   489 
   490 	if (num <= 0) return NULL;
   491 
   492 	if (realloc_debug_func != NULL)
   493 		realloc_debug_func(str, NULL, num, file, line, 0);
   494 	ret=malloc_ex_func(num,file,line);
   495 	if(ret)
   496 		{
   497 		memcpy(ret,str,old_len);
   498 		OPENSSL_cleanse(str,old_len);
   499 		#ifndef EMULATOR
   500 		free_func?free_func(str):free_func_def(str);
   501 		#else
   502 		free_func_openssl(str);
   503 		#endif
   504 		}
   505 #ifdef LEVITTE_DEBUG_MEM
   506 	fprintf(stderr,
   507 		"LEVITTE_DEBUG_MEM:         | 0x%p -> 0x%p (%d)\n",
   508 		str, ret, num);
   509 #endif
   510 	if (realloc_debug_func != NULL)
   511 		realloc_debug_func(str, ret, num, file, line, 1);
   512 
   513 	return ret;
   514 	}
   515 	
   516 
   517 EXPORT_C void CRYPTO_free(void *str)
   518 	{
   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);
   523 #endif
   524 #ifndef EMULATOR
   525 	free_func?free_func(str):free_func_def(str);
   526 #else
   527 	free_func_openssl(str);
   528 #endif	
   529 	if (free_debug_func != NULL)
   530 		free_debug_func(NULL, 1);
   531 	}
   532 
   533 
   534 EXPORT_C void *CRYPTO_remalloc(void *a, int num, const char *file, int line)
   535 	{
   536 	if (a != NULL) OPENSSL_free(a);
   537 	a=(char *)OPENSSL_malloc(num);
   538 	return(a);
   539 	}
   540 
   541 EXPORT_C void CRYPTO_set_mem_debug_options(long bits)
   542 	{
   543 	if (set_debug_options_func != NULL)
   544 		set_debug_options_func(bits);
   545 	}
   546 
   547 EXPORT_C long CRYPTO_get_mem_debug_options(void)
   548 	{
   549 	if (get_debug_options_func != NULL)
   550 		return get_debug_options_func();
   551 	return 0;
   552 	}
   553 
   554