diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/mem.c | 443 | 
1 files changed, 240 insertions, 203 deletions
| diff --git a/src/lib/libcrypto/mem.c b/src/lib/libcrypto/mem.c index 55e829dc92..86e5ff1c8e 100644 --- a/src/lib/libcrypto/mem.c +++ b/src/lib/libcrypto/mem.c | |||
| @@ -62,206 +62,230 @@ | |||
| 62 | #include "cryptlib.h" | 62 | #include "cryptlib.h" | 
| 63 | 63 | ||
| 64 | 64 | ||
| 65 | static int allow_customize = 1; /* we provide flexible functions for */ | 65 | static int allow_customize = 1; | 
| 66 | static int allow_customize_debug = 1;/* exchanging memory-related functions at | 66 | /* we provide flexible functions for */ | 
| 67 | * run-time, but this must be done | 67 | static int allow_customize_debug = 1;/* exchanging memory - related functions at | 
| 68 | * before any blocks are actually | 68 | * run - time, but this must be done | 
| 69 | * allocated; or we'll run into huge | 69 | * before any blocks are actually | 
| 70 | * problems when malloc/free pairs | 70 | * allocated; or we'll run into huge | 
| 71 | * don't match etc. */ | 71 | * problems when malloc/free pairs | 
| 72 | 72 | * don't match etc. */ | |
| 73 | |||
| 74 | 73 | ||
| 75 | /* the following pointers may be changed as long as 'allow_customize' is set */ | 74 | /* the following pointers may be changed as long as 'allow_customize' is set */ | 
| 76 | 75 | ||
| 77 | static void *(*malloc_func)(size_t) = malloc; | 76 | static void *(*malloc_func)(size_t) = malloc; | 
| 78 | static void *default_malloc_ex(size_t num, const char *file, int line) | 77 | static void | 
| 79 | { return malloc_func(num); } | 78 | *default_malloc_ex(size_t num, const char *file, int line) | 
| 80 | static void *(*malloc_ex_func)(size_t, const char *file, int line) | 79 | { | 
| 81 | = default_malloc_ex; | 80 | return malloc_func(num); | 
| 82 | 81 | } | |
| 83 | static void *(*realloc_func)(void *, size_t)= realloc; | 82 | static void *(*malloc_ex_func)(size_t, const char *file, int line) = | 
| 84 | static void *default_realloc_ex(void *str, size_t num, | 83 | default_malloc_ex; | 
| 85 | const char *file, int line) | 84 | |
| 86 | { return realloc_func(str,num); } | 85 | static void *(*realloc_func)(void *, size_t) = realloc; | 
| 87 | static void *(*realloc_ex_func)(void *, size_t, const char *file, int line) | 86 | static void | 
| 88 | = default_realloc_ex; | 87 | *default_realloc_ex(void *str, size_t num, const char *file, int line) | 
| 89 | 88 | { | |
| 90 | static void (*free_func)(void *) = free; | 89 | return realloc_func(str, num); | 
| 91 | 90 | } | |
| 92 | static void *(*malloc_locked_func)(size_t) = malloc; | 91 | static void *(*realloc_ex_func)(void *, size_t, const char *file, int line) = | 
| 93 | static void *default_malloc_locked_ex(size_t num, const char *file, int line) | 92 | default_realloc_ex; | 
| 94 | { return malloc_locked_func(num); } | 93 | |
| 95 | static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) | 94 | static void (*free_func)(void *) = free; | 
| 96 | = default_malloc_locked_ex; | 95 | |
| 97 | 96 | static void *(*malloc_locked_func)(size_t) = malloc; | |
| 98 | static void (*free_locked_func)(void *) = free; | 97 | static void | 
| 99 | 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; | ||
| 100 | 106 | ||
| 101 | 107 | ||
| 102 | /* may be changed as long as 'allow_customize_debug' is set */ | 108 | /* may be changed as long as 'allow_customize_debug' is set */ | 
| 103 | /* XXX use correct function pointer types */ | 109 | /* XXX use correct function pointer types */ | 
| 104 | #ifdef CRYPTO_MDEBUG | 110 | #ifdef CRYPTO_MDEBUG | 
| 105 | /* use default functions from mem_dbg.c */ | 111 | /* use default functions from mem_dbg.c */ | 
| 106 | static void (*malloc_debug_func)(void *,int,const char *,int,int) | 112 | static void (*malloc_debug_func)(void *, int, const char *, int, int) = | 
| 107 | = CRYPTO_dbg_malloc; | 113 | CRYPTO_dbg_malloc; | 
| 108 | static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) | 114 | static void (*realloc_debug_func)(void *, void *, int, const char *, int, int) = | 
| 109 | = CRYPTO_dbg_realloc; | 115 | CRYPTO_dbg_realloc; | 
| 110 | static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free; | 116 | static void (*free_debug_func)(void *, int) = CRYPTO_dbg_free; | 
| 111 | static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; | 117 | static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; | 
| 112 | static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; | 118 | static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; | 
| 113 | #else | 119 | #else | 
| 114 | /* applications can use CRYPTO_malloc_debug_init() to select above case | 120 | /* applications can use CRYPTO_malloc_debug_init() to select above case | 
| 115 | * at run-time */ | 121 | * at run-time */ | 
| 116 | static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL; | 122 | static void (*malloc_debug_func)(void *, int, const char *, int, int) = NULL; | 
| 117 | static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) | 123 | static void (*realloc_debug_func)(void *, void *, int, const char *, int, int) = | 
| 118 | = NULL; | 124 | NULL; | 
| 119 | static void (*free_debug_func)(void *,int) = NULL; | 125 | static void (*free_debug_func)(void *, int) = NULL; | 
| 120 | static void (*set_debug_options_func)(long) = NULL; | 126 | static void (*set_debug_options_func)(long) = NULL; | 
| 121 | static long (*get_debug_options_func)(void) = NULL; | 127 | static long (*get_debug_options_func)(void) = NULL; | 
| 122 | #endif | 128 | #endif | 
| 123 | 129 | ||
| 124 | int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), | 130 | int | 
| 125 | void (*f)(void *)) | 131 | CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), | 
| 126 | { | 132 | void (*f)(void *)) | 
| 133 | { | ||
| 127 | /* Dummy call just to ensure OPENSSL_init() gets linked in */ | 134 | /* Dummy call just to ensure OPENSSL_init() gets linked in */ | 
| 128 | OPENSSL_init(); | 135 | OPENSSL_init(); | 
| 129 | if (!allow_customize) | 136 | if (!allow_customize) | 
| 130 | return 0; | 137 | return 0; | 
| 131 | if ((m == 0) || (r == 0) || (f == 0)) | 138 | if ((m == 0) || (r == 0) || (f == 0)) | 
| 132 | return 0; | 139 | return 0; | 
| 133 | malloc_func=m; malloc_ex_func=default_malloc_ex; | 140 | malloc_func = m; | 
| 134 | realloc_func=r; realloc_ex_func=default_realloc_ex; | 141 | malloc_ex_func = default_malloc_ex; | 
| 135 | free_func=f; | 142 | realloc_func = r; | 
| 136 | malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex; | 143 | realloc_ex_func = default_realloc_ex; | 
| 137 | free_locked_func=f; | 144 | free_func = f; | 
| 145 | malloc_locked_func = m; | ||
| 146 | malloc_locked_ex_func = default_malloc_locked_ex; | ||
| 147 | free_locked_func = f; | ||
| 138 | return 1; | 148 | return 1; | 
| 139 | } | 149 | } | 
| 140 | 150 | ||
| 141 | int CRYPTO_set_mem_ex_functions( | 151 | int | 
| 142 | void *(*m)(size_t,const char *,int), | 152 | CRYPTO_set_mem_ex_functions(void *(*m)(size_t, const char *, int), | 
| 143 | void *(*r)(void *, size_t,const char *,int), | 153 | void *(*r)(void *, size_t, const char *, int), void (*f)(void *)) | 
| 144 | void (*f)(void *)) | 154 | { | 
| 145 | { | ||
| 146 | if (!allow_customize) | 155 | if (!allow_customize) | 
| 147 | return 0; | 156 | return 0; | 
| 148 | if ((m == 0) || (r == 0) || (f == 0)) | 157 | if ((m == 0) || (r == 0) || (f == 0)) | 
| 149 | return 0; | 158 | return 0; | 
| 150 | malloc_func=0; malloc_ex_func=m; | 159 | malloc_func = 0; | 
| 151 | realloc_func=0; realloc_ex_func=r; | 160 | malloc_ex_func = m; | 
| 152 | free_func=f; | 161 | realloc_func = 0; | 
| 153 | malloc_locked_func=0; malloc_locked_ex_func=m; | 162 | realloc_ex_func = r; | 
| 154 | free_locked_func=f; | 163 | free_func = f; | 
| 164 | malloc_locked_func = 0; | ||
| 165 | malloc_locked_ex_func = m; | ||
| 166 | free_locked_func = f; | ||
| 155 | return 1; | 167 | return 1; | 
| 156 | } | 168 | } | 
| 157 | 169 | ||
| 158 | int CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) | 170 | int | 
| 159 | { | 171 | CRYPTO_set_locked_mem_functions(void *(*m)(size_t), void (*f)(void *)) | 
| 172 | { | ||
| 160 | if (!allow_customize) | 173 | if (!allow_customize) | 
| 161 | return 0; | 174 | return 0; | 
| 162 | if ((m == NULL) || (f == NULL)) | 175 | if ((m == NULL) || (f == NULL)) | 
| 163 | return 0; | 176 | return 0; | 
| 164 | malloc_locked_func=m; malloc_locked_ex_func=default_malloc_locked_ex; | 177 | malloc_locked_func = m; | 
| 165 | free_locked_func=f; | 178 | malloc_locked_ex_func = default_malloc_locked_ex; | 
| 179 | free_locked_func = f; | ||
| 166 | return 1; | 180 | return 1; | 
| 167 | } | 181 | } | 
| 168 | 182 | ||
| 169 | int CRYPTO_set_locked_mem_ex_functions( | 183 | int CRYPTO_set_locked_mem_ex_functions(void *(*m)(size_t, const char *, int), | 
| 170 | void *(*m)(size_t,const char *,int), | 184 | void (*f)(void *)) | 
| 171 | void (*f)(void *)) | 185 | { | 
| 172 | { | ||
| 173 | if (!allow_customize) | 186 | if (!allow_customize) | 
| 174 | return 0; | 187 | return 0; | 
| 175 | if ((m == NULL) || (f == NULL)) | 188 | if ((m == NULL) || (f == NULL)) | 
| 176 | return 0; | 189 | return 0; | 
| 177 | malloc_locked_func=0; malloc_locked_ex_func=m; | 190 | malloc_locked_func = 0; | 
| 178 | free_func=f; | 191 | malloc_locked_ex_func = m; | 
| 192 | free_func = f; | ||
| 179 | return 1; | 193 | return 1; | 
| 180 | } | 194 | } | 
| 181 | 195 | ||
| 182 | int CRYPTO_set_mem_debug_functions(void (*m)(void *,int,const char *,int,int), | 196 | int | 
| 183 | void (*r)(void *,void *,int,const char *,int,int), | 197 | CRYPTO_set_mem_debug_functions(void (*m)(void *, int, const char *, int, int), | 
| 184 | void (*f)(void *,int), | 198 | void (*r)(void *, void *, int, const char *, int, int), | 
| 185 | void (*so)(long), | 199 | void (*f)(void *, int), void (*so)(long), long (*go)(void)) | 
| 186 | long (*go)(void)) | 200 | { | 
| 187 | { | ||
| 188 | if (!allow_customize_debug) | 201 | if (!allow_customize_debug) | 
| 189 | return 0; | 202 | return 0; | 
| 190 | OPENSSL_init(); | 203 | OPENSSL_init(); | 
| 191 | malloc_debug_func=m; | 204 | malloc_debug_func = m; | 
| 192 | realloc_debug_func=r; | 205 | realloc_debug_func = r; | 
| 193 | free_debug_func=f; | 206 | free_debug_func = f; | 
| 194 | set_debug_options_func=so; | 207 | set_debug_options_func = so; | 
| 195 | get_debug_options_func=go; | 208 | get_debug_options_func = go; | 
| 196 | return 1; | 209 | return 1; | 
| 197 | } | 210 | } | 
| 198 | 211 | ||
| 199 | 212 | ||
| 200 | void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), | 213 | void | 
| 201 | void (**f)(void *)) | 214 | CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), | 
| 202 | { | 215 | void (**f)(void *)) | 
| 203 | if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? | 216 | { | 
| 204 | malloc_func : 0; | 217 | if (m != NULL) | 
| 205 | if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? | 218 | *m = (malloc_ex_func == default_malloc_ex) ? malloc_func : 0; | 
| 206 | realloc_func : 0; | 219 | if (r != NULL) | 
| 207 | if (f != NULL) *f=free_func; | 220 | *r = (realloc_ex_func == default_realloc_ex) ? realloc_func : 0; | 
| 208 | } | 221 | if (f != NULL) | 
| 209 | 222 | *f = free_func; | |
| 210 | void CRYPTO_get_mem_ex_functions( | 223 | } | 
| 211 | void *(**m)(size_t,const char *,int), | 224 | |
| 212 | void *(**r)(void *, size_t,const char *,int), | 225 | void CRYPTO_get_mem_ex_functions(void *(**m)(size_t, const char *, int), | 
| 213 | void (**f)(void *)) | 226 | void *(**r)(void *, size_t, const char *, int), void (**f)(void *)) | 
| 214 | { | 227 | { | 
| 215 | if (m != NULL) *m = (malloc_ex_func != default_malloc_ex) ? | 228 | if (m != NULL) | 
| 216 | malloc_ex_func : 0; | 229 | *m = (malloc_ex_func != default_malloc_ex) ? malloc_ex_func : 0; | 
| 217 | if (r != NULL) *r = (realloc_ex_func != default_realloc_ex) ? | 230 | if (r != NULL) | 
| 218 | realloc_ex_func : 0; | 231 | *r = (realloc_ex_func != default_realloc_ex) ? | 
| 219 | if (f != NULL) *f=free_func; | 232 | realloc_ex_func : 0; | 
| 220 | } | 233 | if (f != NULL) | 
| 221 | 234 | *f = free_func; | |
| 222 | void CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) | 235 | } | 
| 223 | { | 236 | |
| 224 | if (m != NULL) *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? | 237 | void | 
| 225 | malloc_locked_func : 0; | 238 | CRYPTO_get_locked_mem_functions(void *(**m)(size_t), void (**f)(void *)) | 
| 226 | if (f != NULL) *f=free_locked_func; | 239 | { | 
| 227 | } | 240 | if (m != NULL) | 
| 228 | 241 | *m = (malloc_locked_ex_func == default_malloc_locked_ex) ? | |
| 229 | void CRYPTO_get_locked_mem_ex_functions( | 242 | malloc_locked_func : 0; | 
| 230 | void *(**m)(size_t,const char *,int), | 243 | if (f != NULL) | 
| 231 | void (**f)(void *)) | 244 | *f = free_locked_func; | 
| 232 | { | 245 | } | 
| 233 | if (m != NULL) *m = (malloc_locked_ex_func != default_malloc_locked_ex) ? | 246 | |
| 234 | malloc_locked_ex_func : 0; | 247 | void CRYPTO_get_locked_mem_ex_functions(void *(**m)(size_t, const char *, int), | 
| 235 | if (f != NULL) *f=free_locked_func; | 248 | void (**f)(void *)) | 
| 236 | } | 249 | { | 
| 237 | 250 | if (m != NULL) | |
| 238 | void CRYPTO_get_mem_debug_functions(void (**m)(void *,int,const char *,int,int), | 251 | *m = (malloc_locked_ex_func != default_malloc_locked_ex) ? | 
| 239 | void (**r)(void *,void *,int,const char *,int,int), | 252 | malloc_locked_ex_func : 0; | 
| 240 | void (**f)(void *,int), | 253 | if (f != NULL) | 
| 241 | void (**so)(long), | 254 | *f = free_locked_func; | 
| 242 | long (**go)(void)) | 255 | } | 
| 243 | { | 256 | |
| 244 | if (m != NULL) *m=malloc_debug_func; | 257 | void | 
| 245 | if (r != NULL) *r=realloc_debug_func; | 258 | CRYPTO_get_mem_debug_functions(void (**m)(void *, int, const char *, int, int), | 
| 246 | if (f != NULL) *f=free_debug_func; | 259 | void (**r)(void *, void *, int, const char *, int, int), | 
| 247 | if (so != NULL) *so=set_debug_options_func; | 260 | void (**f)(void *, int), void (**so)(long), long (**go)(void)) | 
| 248 | if (go != NULL) *go=get_debug_options_func; | 261 | { | 
| 249 | } | 262 | if (m != NULL) | 
| 250 | 263 | *m = malloc_debug_func; | |
| 251 | 264 | if (r != NULL) | |
| 252 | void *CRYPTO_malloc_locked(int num, const char *file, int line) | 265 | *r = realloc_debug_func; | 
| 253 | { | 266 | if (f != NULL) | 
| 267 | *f = free_debug_func; | ||
| 268 | if (so != NULL) | ||
| 269 | *so = set_debug_options_func; | ||
| 270 | if (go != NULL) | ||
| 271 | *go = get_debug_options_func; | ||
| 272 | } | ||
| 273 | |||
| 274 | |||
| 275 | void | ||
| 276 | *CRYPTO_malloc_locked(int num, const char *file, int line) | ||
| 277 | { | ||
| 254 | void *ret = NULL; | 278 | void *ret = NULL; | 
| 255 | 279 | ||
| 256 | if (num <= 0) return NULL; | 280 | if (num <= 0) | 
| 281 | return NULL; | ||
| 257 | 282 | ||
| 258 | allow_customize = 0; | 283 | allow_customize = 0; | 
| 259 | if (malloc_debug_func != NULL) | 284 | if (malloc_debug_func != NULL) { | 
| 260 | { | ||
| 261 | allow_customize_debug = 0; | 285 | allow_customize_debug = 0; | 
| 262 | malloc_debug_func(NULL, num, file, line, 0); | 286 | malloc_debug_func(NULL, num, file, line, 0); | 
| 263 | } | 287 | } | 
| 264 | ret = malloc_locked_ex_func(num,file,line); | 288 | ret = malloc_locked_ex_func(num, file, line); | 
| 265 | #ifdef LEVITTE_DEBUG_MEM | 289 | #ifdef LEVITTE_DEBUG_MEM | 
| 266 | fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); | 290 | fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); | 
| 267 | #endif | 291 | #endif | 
| @@ -272,17 +296,18 @@ void *CRYPTO_malloc_locked(int num, const char *file, int line) | |||
| 272 | /* Create a dependency on the value of 'cleanse_ctr' so our memory | 296 | /* Create a dependency on the value of 'cleanse_ctr' so our memory | 
| 273 | * sanitisation function can't be optimised out. NB: We only do | 297 | * sanitisation function can't be optimised out. NB: We only do | 
| 274 | * this for >2Kb so the overhead doesn't bother us. */ | 298 | * this for >2Kb so the overhead doesn't bother us. */ | 
| 275 | if(ret && (num > 2048)) | 299 | if (ret && (num > 2048)) { | 
| 276 | { extern unsigned char cleanse_ctr; | 300 | extern unsigned char cleanse_ctr; | 
| 277 | ((unsigned char *)ret)[0] = cleanse_ctr; | 301 | ((unsigned char *)ret)[0] = cleanse_ctr; | 
| 278 | } | 302 | } | 
| 279 | #endif | 303 | #endif | 
| 280 | 304 | ||
| 281 | return ret; | 305 | return ret; | 
| 282 | } | 306 | } | 
| 283 | 307 | ||
| 284 | void CRYPTO_free_locked(void *str) | 308 | void | 
| 285 | { | 309 | CRYPTO_free_locked(void *str) | 
| 310 | { | ||
| 286 | if (free_debug_func != NULL) | 311 | if (free_debug_func != NULL) | 
| 287 | free_debug_func(str, 0); | 312 | free_debug_func(str, 0); | 
| 288 | #ifdef LEVITTE_DEBUG_MEM | 313 | #ifdef LEVITTE_DEBUG_MEM | 
| @@ -291,21 +316,22 @@ void CRYPTO_free_locked(void *str) | |||
| 291 | free_locked_func(str); | 316 | free_locked_func(str); | 
| 292 | if (free_debug_func != NULL) | 317 | if (free_debug_func != NULL) | 
| 293 | free_debug_func(NULL, 1); | 318 | free_debug_func(NULL, 1); | 
| 294 | } | 319 | } | 
| 295 | 320 | ||
| 296 | void *CRYPTO_malloc(int num, const char *file, int line) | 321 | void | 
| 297 | { | 322 | *CRYPTO_malloc(int num, const char *file, int line) | 
| 323 | { | ||
| 298 | void *ret = NULL; | 324 | void *ret = NULL; | 
| 299 | 325 | ||
| 300 | if (num <= 0) return NULL; | 326 | if (num <= 0) | 
| 327 | return NULL; | ||
| 301 | 328 | ||
| 302 | allow_customize = 0; | 329 | allow_customize = 0; | 
| 303 | if (malloc_debug_func != NULL) | 330 | if (malloc_debug_func != NULL) { | 
| 304 | { | ||
| 305 | allow_customize_debug = 0; | 331 | allow_customize_debug = 0; | 
| 306 | malloc_debug_func(NULL, num, file, line, 0); | 332 | malloc_debug_func(NULL, num, file, line, 0); | 
| 307 | } | 333 | } | 
| 308 | ret = malloc_ex_func(num,file,line); | 334 | ret = malloc_ex_func(num, file, line); | 
| 309 | #ifdef LEVITTE_DEBUG_MEM | 335 | #ifdef LEVITTE_DEBUG_MEM | 
| 310 | fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); | 336 | fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); | 
| 311 | #endif | 337 | #endif | 
| @@ -316,35 +342,39 @@ void *CRYPTO_malloc(int num, const char *file, int line) | |||
| 316 | /* Create a dependency on the value of 'cleanse_ctr' so our memory | 342 | /* Create a dependency on the value of 'cleanse_ctr' so our memory | 
| 317 | * sanitisation function can't be optimised out. NB: We only do | 343 | * sanitisation function can't be optimised out. NB: We only do | 
| 318 | * this for >2Kb so the overhead doesn't bother us. */ | 344 | * this for >2Kb so the overhead doesn't bother us. */ | 
| 319 | if(ret && (num > 2048)) | 345 | if (ret && (num > 2048)) { | 
| 320 | { extern unsigned char cleanse_ctr; | 346 | extern unsigned char cleanse_ctr; | 
| 321 | ((unsigned char *)ret)[0] = cleanse_ctr; | 347 | ((unsigned char *)ret)[0] = cleanse_ctr; | 
| 322 | } | 348 | } | 
| 323 | #endif | 349 | #endif | 
| 324 | 350 | ||
| 325 | return ret; | 351 | return ret; | 
| 326 | } | 352 | } | 
| 327 | char *CRYPTO_strdup(const char *str, const char *file, int line) | 353 | |
| 328 | { | 354 | char | 
| 329 | size_t len = strlen(str)+1; | 355 | *CRYPTO_strdup(const char *str, const char *file, int line) | 
| 356 | { | ||
| 357 | size_t len = strlen(str) + 1; | ||
| 330 | char *ret = CRYPTO_malloc(len, file, line); | 358 | char *ret = CRYPTO_malloc(len, file, line); | 
| 331 | 359 | ||
| 332 | memcpy(ret, str, len); | 360 | memcpy(ret, str, len); | 
| 333 | return ret; | 361 | return ret; | 
| 334 | } | 362 | } | 
| 335 | 363 | ||
| 336 | void *CRYPTO_realloc(void *str, int num, const char *file, int line) | 364 | void | 
| 337 | { | 365 | *CRYPTO_realloc(void *str, int num, const char *file, int line) | 
| 366 | { | ||
| 338 | void *ret = NULL; | 367 | void *ret = NULL; | 
| 339 | 368 | ||
| 340 | if (str == NULL) | 369 | if (str == NULL) | 
| 341 | return CRYPTO_malloc(num, file, line); | 370 | return CRYPTO_malloc(num, file, line); | 
| 342 | 371 | ||
| 343 | if (num <= 0) return NULL; | 372 | if (num <= 0) | 
| 373 | return NULL; | ||
| 344 | 374 | ||
| 345 | if (realloc_debug_func != NULL) | 375 | if (realloc_debug_func != NULL) | 
| 346 | realloc_debug_func(str, NULL, num, file, line, 0); | 376 | realloc_debug_func(str, NULL, num, file, line, 0); | 
| 347 | ret = realloc_ex_func(str,num,file,line); | 377 | ret = realloc_ex_func(str, num, file, line); | 
| 348 | #ifdef LEVITTE_DEBUG_MEM | 378 | #ifdef LEVITTE_DEBUG_MEM | 
| 349 | fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num); | 379 | fprintf(stderr, "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", str, ret, num); | 
| 350 | #endif | 380 | #endif | 
| @@ -352,44 +382,47 @@ void *CRYPTO_realloc(void *str, int num, const char *file, int line) | |||
| 352 | realloc_debug_func(str, ret, num, file, line, 1); | 382 | realloc_debug_func(str, ret, num, file, line, 1); | 
| 353 | 383 | ||
| 354 | return ret; | 384 | return ret; | 
| 355 | } | 385 | } | 
| 356 | 386 | ||
| 357 | void *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file, | 387 | void | 
| 358 | int line) | 388 | *CRYPTO_realloc_clean(void *str, int old_len, int num, const char *file, | 
| 359 | { | 389 | int line) | 
| 390 | { | ||
| 360 | void *ret = NULL; | 391 | void *ret = NULL; | 
| 361 | 392 | ||
| 362 | if (str == NULL) | 393 | if (str == NULL) | 
| 363 | return CRYPTO_malloc(num, file, line); | 394 | return CRYPTO_malloc(num, file, line); | 
| 364 | 395 | ||
| 365 | if (num <= 0) return NULL; | 396 | if (num <= 0) | 
| 397 | return NULL; | ||
| 366 | 398 | ||
| 367 | /* We don't support shrinking the buffer. Note the memcpy that copies | 399 | /* We don't support shrinking the buffer. Note the memcpy that copies | 
| 368 | * |old_len| bytes to the new buffer, below. */ | 400 | * |old_len| bytes to the new buffer, below. */ | 
| 369 | if (num < old_len) return NULL; | 401 | if (num < old_len) | 
| 402 | return NULL; | ||
| 370 | 403 | ||
| 371 | if (realloc_debug_func != NULL) | 404 | if (realloc_debug_func != NULL) | 
| 372 | realloc_debug_func(str, NULL, num, file, line, 0); | 405 | realloc_debug_func(str, NULL, num, file, line, 0); | 
| 373 | ret=malloc_ex_func(num,file,line); | 406 | ret = malloc_ex_func(num, file, line); | 
| 374 | if(ret) | 407 | if (ret) { | 
| 375 | { | 408 | memcpy(ret, str, old_len); | 
| 376 | memcpy(ret,str,old_len); | 409 | OPENSSL_cleanse(str, old_len); | 
| 377 | OPENSSL_cleanse(str,old_len); | ||
| 378 | free_func(str); | 410 | free_func(str); | 
| 379 | } | 411 | } | 
| 380 | #ifdef LEVITTE_DEBUG_MEM | 412 | #ifdef LEVITTE_DEBUG_MEM | 
| 381 | fprintf(stderr, | 413 | fprintf(stderr, | 
| 382 | "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", | 414 | "LEVITTE_DEBUG_MEM: | 0x%p -> 0x%p (%d)\n", | 
| 383 | str, ret, num); | 415 | str, ret, num); | 
| 384 | #endif | 416 | #endif | 
| 385 | if (realloc_debug_func != NULL) | 417 | if (realloc_debug_func != NULL) | 
| 386 | realloc_debug_func(str, ret, num, file, line, 1); | 418 | realloc_debug_func(str, ret, num, file, line, 1); | 
| 387 | 419 | ||
| 388 | return ret; | 420 | return ret; | 
| 389 | } | 421 | } | 
| 390 | 422 | ||
| 391 | void CRYPTO_free(void *str) | 423 | void | 
| 392 | { | 424 | CRYPTO_free(void *str) | 
| 425 | { | ||
| 393 | if (free_debug_func != NULL) | 426 | if (free_debug_func != NULL) | 
| 394 | free_debug_func(str, 0); | 427 | free_debug_func(str, 0); | 
| 395 | #ifdef LEVITTE_DEBUG_MEM | 428 | #ifdef LEVITTE_DEBUG_MEM | 
| @@ -398,24 +431,28 @@ void CRYPTO_free(void *str) | |||
| 398 | free_func(str); | 431 | free_func(str); | 
| 399 | if (free_debug_func != NULL) | 432 | if (free_debug_func != NULL) | 
| 400 | free_debug_func(NULL, 1); | 433 | free_debug_func(NULL, 1); | 
| 401 | } | 434 | } | 
| 402 | 435 | ||
| 403 | void *CRYPTO_remalloc(void *a, int num, const char *file, int line) | 436 | void | 
| 404 | { | 437 | *CRYPTO_remalloc(void *a, int num, const char *file, int line) | 
| 405 | if (a != NULL) OPENSSL_free(a); | 438 | { | 
| 406 | a=(char *)OPENSSL_malloc(num); | 439 | if (a != NULL) | 
| 407 | return(a); | 440 | OPENSSL_free(a); | 
| 408 | } | 441 | a = (char *)OPENSSL_malloc(num); | 
| 409 | 442 | return (a); | |
| 410 | void CRYPTO_set_mem_debug_options(long bits) | 443 | } | 
| 411 | { | 444 | |
| 445 | void | ||
| 446 | CRYPTO_set_mem_debug_options(long bits) | ||
| 447 | { | ||
| 412 | if (set_debug_options_func != NULL) | 448 | if (set_debug_options_func != NULL) | 
| 413 | set_debug_options_func(bits); | 449 | set_debug_options_func(bits); | 
| 414 | } | 450 | } | 
| 415 | 451 | ||
| 416 | long CRYPTO_get_mem_debug_options(void) | 452 | long | 
| 417 | { | 453 | CRYPTO_get_mem_debug_options(void) | 
| 454 | { | ||
| 418 | if (get_debug_options_func != NULL) | 455 | if (get_debug_options_func != NULL) | 
| 419 | return get_debug_options_func(); | 456 | return get_debug_options_func(); | 
| 420 | return 0; | 457 | return 0; | 
| 421 | } | 458 | } | 
