/*
* Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* https://www.openssl.org/source/license.html
*/
-/*
- * Copyright 2004-2014, Akamai Technologies. All Rights Reserved.
- * This file is distributed under the terms of the OpenSSL license.
- */
-
/*
* This file is in two halves. The first half implements the public API
* to be used by external consumers, and to be used by OpenSSL to store
* "SECURE HEAP IMPLEMENTATION").
*/
#include <openssl/crypto.h>
-#include <e_os.h>
+#include "e_os.h"
#include <string.h>
sec_malloc_lock = CRYPTO_THREAD_lock_new();
if (sec_malloc_lock == NULL)
return 0;
- ret = sh_init(size, minsize);
- secure_mem_initialized = 1;
+ if ((ret = sh_init(size, minsize)) != 0) {
+ secure_mem_initialized = 1;
+ } else {
+ CRYPTO_THREAD_lock_free(sec_malloc_lock);
+ sec_malloc_lock = NULL;
+ }
}
return ret;
sh_done();
secure_mem_initialized = 0;
CRYPTO_THREAD_lock_free(sec_malloc_lock);
+ sec_malloc_lock = NULL;
return 1;
}
#endif /* IMPLEMENTED */
#endif /* IMPLEMENTED */
}
+void CRYPTO_secure_clear_free(void *ptr, size_t num,
+ const char *file, int line)
+{
+#ifdef IMPLEMENTED
+ size_t actual_size;
+
+ if (ptr == NULL)
+ return;
+ if (!CRYPTO_secure_allocated(ptr)) {
+ OPENSSL_cleanse(ptr, num);
+ CRYPTO_free(ptr, file, line);
+ return;
+ }
+ CRYPTO_THREAD_write_lock(sec_malloc_lock);
+ actual_size = sh_actual_size(ptr);
+ CLEAR(ptr, actual_size);
+ secure_mem_used -= actual_size;
+ sh_free(ptr);
+ CRYPTO_THREAD_unlock(sec_malloc_lock);
+#else
+ if (ptr == NULL)
+ return;
+ OPENSSL_cleanse(ptr, num);
+ CRYPTO_free(ptr, file, line);
+#endif /* IMPLEMENTED */
+}
+
int CRYPTO_secure_allocated(const void *ptr)
{
#ifdef IMPLEMENTED
static int sh_init(size_t size, int minsize)
{
- int i, ret;
+ int ret;
+ size_t i;
size_t pgsize;
size_t aligned;
close(fd);
}
}
- OPENSSL_assert(sh.map_result != MAP_FAILED);
if (sh.map_result == MAP_FAILED)
goto err;
sh.arena = (char *)(sh.map_result + pgsize);
size_t i;
char *chunk;
+ if (size > sh.arena_size)
+ return NULL;
+
list = sh.freelist_size - 1;
for (i = sh.minsize; i < size; i <<= 1)
list--;