summaryrefslogtreecommitdiff
path: root/lib/gnutls_mem.c
blob: 96e6b1e03d789174cb359342a648cdbfa59123a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 *      Copyright (C) 2001,2002 Nikos Mavroyanopoulos
 *
 * This file is part of GNUTLS.
 *
 * GNUTLS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GNUTLS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <gnutls_int.h>
#include <gnutls_errors.h>
#include <gnutls_num.h>

#ifdef USE_LIBCALLOC

int _gnutls_is_secure_memory(const void *ign)
{
	return 0;
}

#else

/* #define MALLOC_DEBUG */
# define EXTRA_SIZE sizeof(size_t)+1

int _gnutls_is_secure_memory(const svoid * mem)
{
	if (mem==NULL) return 0;
	return *((opaque *) mem - 1);
}

void *gnutls_malloc(size_t size)
{
	opaque *ret;
	if (size == 0)
		return NULL;

	ret = malloc(size + EXTRA_SIZE);
	if (ret == NULL)
		return ret;

	*((int *) ret) = size;
	ret[sizeof(size_t)] = 0;	/* not secure */

	ret += EXTRA_SIZE;

#ifdef MALLOC_DEBUG
	_gnutls_log("Allocated: %x with %d bytes\n", ret,
		    _gnutls_malloc_ptr_size(ret));
#endif

	return ret;

}

void *gnutls_calloc(size_t nmemb, size_t size)
{
	void *ret;
	ret = gnutls_malloc(size);
	if (ret == NULL)
		return ret;

	memset(ret, 0, size);

	return ret;
}

size_t _gnutls_malloc_ptr_size(void *_ptr)
{
	opaque *ptr = _ptr;

	if (_ptr == NULL)
		return 0;

	return *((int *) ((opaque *) ptr - sizeof(size_t) - 1));
}

void *gnutls_realloc(void *_ptr, size_t size)
{
	opaque *ret;
	opaque* ptr = _ptr;
	
	if (ptr!=NULL)
		ptr -= EXTRA_SIZE;

	ret = realloc(ptr, size + EXTRA_SIZE);
	if (ret == NULL)
		return ret;

	*((int *) ret) = size;
	ret[sizeof(size_t)] = 0;	/* not secure */

	ret += EXTRA_SIZE;

	return ret;
}

/* This realloc only returns a new pointer if you
 * request more data than the data into the pointer.
 */
void *gnutls_realloc_fast(void *ptr, size_t size)
{

	if (ptr != NULL && size <= _gnutls_malloc_ptr_size(ptr)) {
		/* do nothing, just return the pointer.
		   * It's much faster.
		 */
		return ptr;
	}
	return gnutls_realloc(ptr, size);
}

inline
static void _gnutls_free(void *_ptr)
{
opaque *ptr = _ptr;


	ptr -= EXTRA_SIZE;

#ifdef MALLOC_DEBUG
	_gnutls_log("Freed: %x with %d bytes\n", _ptr,
	    _gnutls_malloc_ptr_size(_ptr));
#endif
	free(ptr);
}

void gnutls_free(void *_ptr)
{
	if (_ptr == NULL)
		return;

	if ( _gnutls_is_secure_memory( _ptr) != 0) {
		return gnutls_secure_free( _ptr);
	} else {
		_gnutls_free( _ptr);
	}
}



svoid *gnutls_secure_malloc(size_t size)
{
	opaque *ret;
	ret = gnutls_malloc(size);
	if (ret == NULL)
		return ret;

	*((opaque *) ret - 1) = 1;	/* secure mem */

	return ret;

}

svoid *gnutls_secure_calloc(size_t nmemb, size_t size)
{
	svoid *ret;
	ret = gnutls_secure_malloc(size);
	if (ret == NULL)
		return ret;

	memset(ret, 0, size);

	return ret;
}

size_t _gnutls_secure_ptr_size(svoid * ptr)
{
	return _gnutls_malloc_ptr_size(ptr);
}

svoid *gnutls_secure_realloc(svoid * ptr, size_t size)
{
	svoid *ret;
	if (ptr != NULL && size <= _gnutls_secure_ptr_size(ptr)) {
		/* do not do realloc.
		 * return the previous pointer.
		 */
		return ptr;
	}
	ret = gnutls_secure_malloc(size);
	if (ret == NULL)
		return ret;

	if (ptr != NULL) {
		memcpy(ret, ptr, GMIN(_gnutls_secure_ptr_size(ptr), size));
		gnutls_secure_free(ptr);
	}

	return ret;
}

void gnutls_secure_free(svoid * ptr)
{
opaque* _ptr = ptr;

	memset(ptr, 0, _gnutls_secure_ptr_size(ptr));
	*((opaque *) _ptr - 1) = 0;	/* not secure mem */

	_gnutls_free(ptr);
}

char *gnutls_strdup(const char *s)
{
	int size = strlen(s);
	char *ret;

	ret = gnutls_malloc(size + 1);	/* hold null */
	if (ret == NULL)
		return ret;

	strcpy(ret, s); /* Flawfinder: ignore */

	return ret;
}
#endif	/* USE_LIBCALLOC */