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
|
/*
* Copyright (C) 2001 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_DMALLOC
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) {
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) {
void* ret;
if ( ptr != NULL && size <= _gnutls_malloc_ptr_size(ptr)) {
/* do nothing, just return the pointer.
* It's much faster.
*/
return ptr;
}
ret = gnutls_malloc( size);
if (ret==NULL) return ret;
if (ptr!=NULL) {
memcpy( ret, ptr, GMIN( _gnutls_malloc_ptr_size(ptr), size));
gnutls_free(ptr);
}
return ret;
}
void gnutls_free( void* _ptr) {
opaque* ptr = _ptr;
if (_ptr==NULL) return;
ptr -= EXTRA_SIZE;
#ifdef MALLOC_DEBUG
_gnutls_log("Freed: %x with %d bytes\n", _ptr, _gnutls_malloc_ptr_size(_ptr));
#endif
free( ptr);
}
svoid* 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* secure_calloc( size_t nmemb, size_t size) {
svoid* ret;
ret = secure_malloc( size);
if (ret==NULL) return ret;
memset( ret, 0, size);
return ret;
}
size_t _secure_ptr_size( svoid* ptr) {
return _gnutls_malloc_ptr_size( ptr);
}
svoid* secure_realloc( svoid* ptr, size_t size) {
svoid* ret;
if ( ptr != NULL && size <= _secure_ptr_size(ptr)) {
/* do not do realloc.
* return the previous pointer.
*/
return ptr;
}
ret = secure_malloc( size);
if (ret==NULL) return ret;
if (ptr!=NULL) {
memcpy( ret, ptr, GMIN( _secure_ptr_size(ptr), size));
secure_free(ptr);
}
return ret;
}
void secure_free( svoid* ptr) {
memset( ptr, 0, _secure_ptr_size( ptr));
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);
return ret;
}
#endif /* USE_DMALLOC */
|