Implement the CRYPTO_secure_clear_free function
authorBernd Edlinger <bernd.edlinger@hotmail.de>
Fri, 28 Jul 2017 19:24:02 +0000 (21:24 +0200)
committerBernd Edlinger <bernd.edlinger@hotmail.de>
Sat, 29 Jul 2017 17:26:06 +0000 (19:26 +0200)
Reviewed-by: Rich Salz <rsalz@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/4044)

crypto/mem_sec.c
include/openssl/crypto.h
util/libcrypto.num
util/private.num

index 11e95c42b891b2c63e971d934d6d44ddebed0cdd..04aa94eae168d23895123b3aa72c92a9feb2ad25 100644 (file)
@@ -157,6 +157,33 @@ void CRYPTO_secure_free(void *ptr, const char *file, int line)
 #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
index f0bc98f3200a9f3382fda88e0b566994c5cf8175..cebde9734b8bba89ab0cb0cba54b10dc952c526f 100644 (file)
@@ -144,6 +144,8 @@ int CRYPTO_mem_ctrl(int mode);
         CRYPTO_secure_zalloc(num, OPENSSL_FILE, OPENSSL_LINE)
 # define OPENSSL_secure_free(addr) \
         CRYPTO_secure_free(addr, OPENSSL_FILE, OPENSSL_LINE)
+# define OPENSSL_secure_clear_free(addr, num) \
+        CRYPTO_secure_clear_free(addr, num, OPENSSL_FILE, OPENSSL_LINE)
 # define OPENSSL_secure_actual_size(ptr) \
         CRYPTO_secure_actual_size(ptr)
 
@@ -283,6 +285,8 @@ int CRYPTO_secure_malloc_done(void);
 void *CRYPTO_secure_malloc(size_t num, const char *file, int line);
 void *CRYPTO_secure_zalloc(size_t num, const char *file, int line);
 void CRYPTO_secure_free(void *ptr, const char *file, int line);
+void CRYPTO_secure_clear_free(void *ptr, size_t num,
+                              const char *file, int line);
 int CRYPTO_secure_allocated(const void *ptr);
 int CRYPTO_secure_malloc_initialized(void);
 size_t CRYPTO_secure_actual_size(void *ptr);
index 1c754b47a67652f014913d6ba0639f532be4d5b5..4b00b00dbb66ae45470e8ee0cbac74ab25ed8ad1 100644 (file)
@@ -4370,3 +4370,4 @@ i2d_SCRYPT_PARAMS                       4312      1_1_1   EXIST::FUNCTION:SCRYPT
 d2i_SCRYPT_PARAMS                       4313   1_1_1   EXIST::FUNCTION:SCRYPT
 SCRYPT_PARAMS_it                        4314   1_1_1   EXIST:!EXPORT_VAR_AS_FUNCTION:VARIABLE:SCRYPT
 SCRYPT_PARAMS_it                        4314   1_1_1   EXIST:EXPORT_VAR_AS_FUNCTION:FUNCTION:SCRYPT
+CRYPTO_secure_clear_free                4315   1_1_0g  EXIST::FUNCTION:
index f82a8f6db6694ce825f31c0969fc5f16267ea97a..a586a9bb1d2a4870e21bb0a1335bfc0e80166111 100644 (file)
@@ -202,6 +202,7 @@ OPENSSL_memdup                          define
 OPENSSL_no_config                       define deprecated 1.1.0
 OPENSSL_realloc                         define
 OPENSSL_secure_actual_size              define
+OPENSSL_secure_clear_free               define
 OPENSSL_secure_free                     define
 OPENSSL_secure_malloc                   define
 OPENSSL_secure_zalloc                   define