Skip to content

Commit 6943335

Browse files
richsalzbernd-edlinger
authored andcommitted
Make secure-memory be a config option
Adding support for "no-secure-memory" was simple, a one-liner. Fixing all the "ifdef OPENSSL_SECURE_MEMORY" to be "ifndef NO_xxx" was a bit more work. My original goof, for not following the OpenSSL pattern "ifndef NO_" used everywhere else. Reviewed-by: Richard Levitte <[email protected]> Reviewed-by: Bernd Edlinger <[email protected]> (Merged from openssl#11023)
1 parent cdb1663 commit 6943335

File tree

4 files changed

+36
-31
lines changed

4 files changed

+36
-31
lines changed

Configure

+1
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ my @disablables = (
412412
"rmd160",
413413
"scrypt",
414414
"sctp",
415+
"secure-memory",
415416
"seed",
416417
"shared",
417418
"siphash",

crypto/mem_sec.c

+22-23
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020

2121
#include <string.h>
2222

23-
/* e_os.h defines OPENSSL_SECURE_MEMORY if secure memory can be implemented */
24-
#ifdef OPENSSL_SECURE_MEMORY
23+
#ifndef OPENSSL_NO_SECURE_MEMORY
2524
# include <stdlib.h>
2625
# include <assert.h>
2726
# include <unistd.h>
@@ -47,7 +46,7 @@
4746
# define MAP_ANON MAP_ANONYMOUS
4847
#endif
4948

50-
#ifdef OPENSSL_SECURE_MEMORY
49+
#ifndef OPENSSL_NO_SECURE_MEMORY
5150
static size_t secure_mem_used;
5251

5352
static int secure_mem_initialized;
@@ -67,7 +66,7 @@ static int sh_allocated(const char *ptr);
6766

6867
int CRYPTO_secure_malloc_init(size_t size, size_t minsize)
6968
{
70-
#ifdef OPENSSL_SECURE_MEMORY
69+
#ifndef OPENSSL_NO_SECURE_MEMORY
7170
int ret = 0;
7271

7372
if (!secure_mem_initialized) {
@@ -85,35 +84,35 @@ int CRYPTO_secure_malloc_init(size_t size, size_t minsize)
8584
return ret;
8685
#else
8786
return 0;
88-
#endif /* OPENSSL_SECURE_MEMORY */
87+
#endif /* OPENSSL_NO_SECURE_MEMORY */
8988
}
9089

9190
int CRYPTO_secure_malloc_done(void)
9291
{
93-
#ifdef OPENSSL_SECURE_MEMORY
92+
#ifndef OPENSSL_NO_SECURE_MEMORY
9493
if (secure_mem_used == 0) {
9594
sh_done();
9695
secure_mem_initialized = 0;
9796
CRYPTO_THREAD_lock_free(sec_malloc_lock);
9897
sec_malloc_lock = NULL;
9998
return 1;
10099
}
101-
#endif /* OPENSSL_SECURE_MEMORY */
100+
#endif /* OPENSSL_NO_SECURE_MEMORY */
102101
return 0;
103102
}
104103

105104
int CRYPTO_secure_malloc_initialized(void)
106105
{
107-
#ifdef OPENSSL_SECURE_MEMORY
106+
#ifndef OPENSSL_NO_SECURE_MEMORY
108107
return secure_mem_initialized;
109108
#else
110109
return 0;
111-
#endif /* OPENSSL_SECURE_MEMORY */
110+
#endif /* OPENSSL_NO_SECURE_MEMORY */
112111
}
113112

114113
void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
115114
{
116-
#ifdef OPENSSL_SECURE_MEMORY
115+
#ifndef OPENSSL_NO_SECURE_MEMORY
117116
void *ret;
118117
size_t actual_size;
119118

@@ -128,12 +127,12 @@ void *CRYPTO_secure_malloc(size_t num, const char *file, int line)
128127
return ret;
129128
#else
130129
return CRYPTO_malloc(num, file, line);
131-
#endif /* OPENSSL_SECURE_MEMORY */
130+
#endif /* OPENSSL_NO_SECURE_MEMORY */
132131
}
133132

134133
void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
135134
{
136-
#ifdef OPENSSL_SECURE_MEMORY
135+
#ifndef OPENSSL_NO_SECURE_MEMORY
137136
if (secure_mem_initialized)
138137
/* CRYPTO_secure_malloc() zeroes allocations when it is implemented */
139138
return CRYPTO_secure_malloc(num, file, line);
@@ -143,7 +142,7 @@ void *CRYPTO_secure_zalloc(size_t num, const char *file, int line)
143142

144143
void CRYPTO_secure_free(void *ptr, const char *file, int line)
145144
{
146-
#ifdef OPENSSL_SECURE_MEMORY
145+
#ifndef OPENSSL_NO_SECURE_MEMORY
147146
size_t actual_size;
148147

149148
if (ptr == NULL)
@@ -160,13 +159,13 @@ void CRYPTO_secure_free(void *ptr, const char *file, int line)
160159
CRYPTO_THREAD_unlock(sec_malloc_lock);
161160
#else
162161
CRYPTO_free(ptr, file, line);
163-
#endif /* OPENSSL_SECURE_MEMORY */
162+
#endif /* OPENSSL_NO_SECURE_MEMORY */
164163
}
165164

166165
void CRYPTO_secure_clear_free(void *ptr, size_t num,
167166
const char *file, int line)
168167
{
169-
#ifdef OPENSSL_SECURE_MEMORY
168+
#ifndef OPENSSL_NO_SECURE_MEMORY
170169
size_t actual_size;
171170

172171
if (ptr == NULL)
@@ -187,12 +186,12 @@ void CRYPTO_secure_clear_free(void *ptr, size_t num,
187186
return;
188187
OPENSSL_cleanse(ptr, num);
189188
CRYPTO_free(ptr, file, line);
190-
#endif /* OPENSSL_SECURE_MEMORY */
189+
#endif /* OPENSSL_NO_SECURE_MEMORY */
191190
}
192191

193192
int CRYPTO_secure_allocated(const void *ptr)
194193
{
195-
#ifdef OPENSSL_SECURE_MEMORY
194+
#ifndef OPENSSL_NO_SECURE_MEMORY
196195
int ret;
197196

198197
if (!secure_mem_initialized)
@@ -203,21 +202,21 @@ int CRYPTO_secure_allocated(const void *ptr)
203202
return ret;
204203
#else
205204
return 0;
206-
#endif /* OPENSSL_SECURE_MEMORY */
205+
#endif /* OPENSSL_NO_SECURE_MEMORY */
207206
}
208207

209208
size_t CRYPTO_secure_used(void)
210209
{
211-
#ifdef OPENSSL_SECURE_MEMORY
210+
#ifndef OPENSSL_NO_SECURE_MEMORY
212211
return secure_mem_used;
213212
#else
214213
return 0;
215-
#endif /* OPENSSL_SECURE_MEMORY */
214+
#endif /* OPENSSL_NO_SECURE_MEMORY */
216215
}
217216

218217
size_t CRYPTO_secure_actual_size(void *ptr)
219218
{
220-
#ifdef OPENSSL_SECURE_MEMORY
219+
#ifndef OPENSSL_NO_SECURE_MEMORY
221220
size_t actual_size;
222221

223222
CRYPTO_THREAD_write_lock(sec_malloc_lock);
@@ -235,7 +234,7 @@ size_t CRYPTO_secure_actual_size(void *ptr)
235234
/*
236235
* SECURE HEAP IMPLEMENTATION
237236
*/
238-
#ifdef OPENSSL_SECURE_MEMORY
237+
#ifndef OPENSSL_NO_SECURE_MEMORY
239238

240239

241240
/*
@@ -642,4 +641,4 @@ static size_t sh_actual_size(char *ptr)
642641
OPENSSL_assert(sh_testbit(ptr, list, sh.bittable));
643642
return sh.arena_size / (ONE << list);
644643
}
645-
#endif /* OPENSSL_SECURE_MEMORY */
644+
#endif /* OPENSSL_NO_SECURE_MEMORY */

e_os.h

+11-6
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,16 @@ struct servent *getservbyname(const char *name, const char *proto);
299299
# define CRYPTO_memcmp memcmp
300300
# endif
301301

302-
/* unistd.h defines _POSIX_VERSION */
303-
# if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
304-
&& ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
305-
|| defined(__sun) || defined(__hpux) || defined(__sgi) \
306-
|| defined(__osf__) )
307-
# define OPENSSL_SECURE_MEMORY /* secure memory is implemented */
302+
# ifndef OPENSSL_NO_SECURE_MEMORY
303+
/* unistd.h defines _POSIX_VERSION */
304+
# if defined(OPENSSL_SYS_UNIX) \
305+
&& ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
306+
|| defined(__sun) || defined(__hpux) || defined(__sgi) \
307+
|| defined(__osf__) )
308+
/* secure memory is implemented */
309+
# else
310+
# define OPENSSL_NO_SECURE_MEMORY
311+
# endif
308312
# endif
313+
309314
#endif

test/secmemtest.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
static int test_sec_mem(void)
1616
{
17-
#ifdef OPENSSL_SECURE_MEMORY
17+
#ifndef OPENSSL_NO_SECURE_MEMORY
1818
int testresult = 0;
1919
char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
2020

@@ -135,7 +135,7 @@ static int test_sec_mem(void)
135135

136136
static int test_sec_mem_clear(void)
137137
{
138-
#ifdef OPENSSL_SECURE_MEMORY
138+
#ifndef OPENSSL_NO_SECURE_MEMORY
139139
const int size = 64;
140140
unsigned char *p = NULL;
141141
int i, res = 0;

0 commit comments

Comments
 (0)