Skip to content

Commit 395b327

Browse files
committed
Add a define WE_NO_OPENSSL_MALLOC that allows the user to disable the usage of
OpenSSL memory management functions in wolfEngine. Defining this will cause wolfSSL's memory management functions to be used instead (e.g. XMALLOC, XFREE, etc.).
1 parent ff4bde3 commit 395b327

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

include/wolfengine/we_internal.h

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,35 @@
7777
#include <wolfssl/wolfcrypt/pwdbased.h>
7878

7979
#include <wolfengine/we_openssl_bc.h>
80-
8180
#include <wolfengine/we_logging.h>
8281
#include <wolfengine/we_fips.h>
8382

83+
/* Defining WE_NO_OPENSSL_MALLOC will cause wolfEngine to not use the OpenSSL
84+
* memory management functions (e.g. OPENSSL_malloc, OPENSSL_free, etc.).
85+
* Instead, the corresponding wolfSSL functions will be used (e.g. XMALLOC,
86+
* XFREE, etc.). */
87+
#ifdef WE_NO_OPENSSL_MALLOC
88+
#undef OPENSSL_malloc
89+
#define OPENSSL_malloc(num) XMALLOC((num), NULL, DYNAMIC_TYPE_TMP_BUFFER)
90+
#undef OPENSSL_free
91+
#define OPENSSL_free(ptr) XFREE((ptr), NULL, DYNAMIC_TYPE_TMP_BUFFER)
92+
#undef OPENSSL_realloc
93+
#define OPENSSL_realloc(ptr, num) XREALLOC((ptr), (num), NULL, DYNAMIC_TYPE_TMP_BUFFER)
94+
95+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
96+
void *we_zalloc(size_t num);
97+
void we_clear_free(void *str, size_t num);
98+
void *we_memdup(const void *data, size_t siz);
99+
100+
#undef OPENSSL_zalloc
101+
#define OPENSSL_zalloc we_zalloc
102+
#undef OPENSSL_clear_free
103+
#define OPENSSL_clear_free we_clear_free
104+
#undef OPENSSL_memdup
105+
#define OPENSSL_memdup we_memdup
106+
#endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
107+
#endif /* WE_NO_OPENSSL_MALLOC */
108+
84109
#if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
85110
/* Function is a printf style function. Pretend parameter is string literal.
86111
*

src/we_internal.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,42 @@
2222
#include <wolfengine/we_wolfengine.h>
2323
#include <wolfengine/we_internal.h>
2424

25+
#ifdef WE_NO_OPENSSL_MALLOC
26+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
27+
void *we_zalloc(size_t num)
28+
{
29+
void *ret = XMALLOC(num, NULL, DYNAMIC_TYPE_TMP_BUFFER);
30+
if (ret != NULL) {
31+
XMEMSET(ret, 0, num);
32+
}
33+
return ret;
34+
}
35+
36+
void we_clear_free(void *str, size_t num)
37+
{
38+
if (str == NULL)
39+
return;
40+
if (num)
41+
OPENSSL_cleanse(str, num);
42+
XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
43+
}
44+
45+
void *we_memdup(const void *data, size_t siz)
46+
{
47+
void *ret;
48+
49+
if (data == NULL || siz >= INT_MAX)
50+
return NULL;
51+
52+
ret = XMALLOC(siz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
53+
if (ret == NULL) {
54+
return NULL;
55+
}
56+
return XMEMCPY(ret, data, siz);
57+
}
58+
#endif /* OPENSSL_VERSION_NUMBER >= 0x10100000L */
59+
#endif /* WE_NO_OPENSSL_MALLOC */
60+
2561
/** Engine bound to. */
2662
static ENGINE *bound = NULL;
2763

0 commit comments

Comments
 (0)