summaryrefslogtreecommitdiff
path: root/lib/gnutls_buffers.c
blob: 840de05439d7c382470cd6713a3f32b6a679fb93 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
 *      Copyright (C) 2000 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 <defines.h>
#include "gnutls_int.h"
#include "gnutls_errors.h"

int gnutls_insertDataBuffer(ContentType type, GNUTLS_STATE state, char *data, int length)
{
	int old_buffer;

	if (type == GNUTLS_APPLICATION_DATA) {
		old_buffer = state->gnutls_internals.bufferSize;

		state->gnutls_internals.bufferSize += length;
#ifdef HARD_DEBUG
	fprintf(stderr, "Inserted %d bytes of Data(%d) into buffer\n", length, type);
#endif
		state->gnutls_internals.buffer =
		    gnutls_realloc(state->gnutls_internals.buffer,
			   state->gnutls_internals.bufferSize);
		memmove(&state->gnutls_internals.buffer[old_buffer], data, length);
	}
	if (type == GNUTLS_HANDSHAKE) {
		old_buffer = state->gnutls_internals.bufferSize_handshake;

		state->gnutls_internals.bufferSize_handshake += length;
#ifdef HARD_DEBUG
	fprintf(stderr, "Inserted %d bytes of Data(%d) into buffer\n", length, type);
#endif
		state->gnutls_internals.buffer_handshake =
		    gnutls_realloc(state->gnutls_internals.buffer_handshake,
			   state->gnutls_internals.bufferSize_handshake);
		memmove(&state->gnutls_internals.buffer_handshake[old_buffer], data, length);
	}

	return 0;

}

int gnutls_getDataBufferSize(ContentType type, GNUTLS_STATE state)
{
	if (type == GNUTLS_APPLICATION_DATA)
		return state->gnutls_internals.bufferSize;
	if (type == GNUTLS_HANDSHAKE)
		return state->gnutls_internals.bufferSize_handshake;
	return 0;
}

int gnutls_getDataFromBuffer(ContentType type, GNUTLS_STATE state, char *data, int length)
{
	if (type == GNUTLS_APPLICATION_DATA) {
	
		if (length > state->gnutls_internals.bufferSize) {
			length = state->gnutls_internals.bufferSize;
		}
#ifdef HARD_DEBUG
	fprintf(stderr, "Read %d bytes of Data(%d) from buffer\n", length, type);
#endif
		state->gnutls_internals.bufferSize -= length;
		memmove(data, state->gnutls_internals.buffer, length);

		/* overwrite buffer */
		memmove(state->gnutls_internals.buffer,
			&state->gnutls_internals.buffer[length],
			state->gnutls_internals.bufferSize);
		state->gnutls_internals.buffer =
		    gnutls_realloc(state->gnutls_internals.buffer,
				   state->gnutls_internals.bufferSize);
	}
	if (type == GNUTLS_HANDSHAKE) {
		if (length > state->gnutls_internals.bufferSize_handshake) {
			length = state->gnutls_internals.bufferSize_handshake;
		}
#ifdef HARD_DEBUG
	fprintf(stderr, "Read %d bytes of Data(%d) from buffer\n", length, type);
#endif
		state->gnutls_internals.bufferSize_handshake -= length;
		memmove(data, state->gnutls_internals.buffer_handshake, length);

		/* overwrite buffer */
		memmove(state->gnutls_internals.buffer_handshake,
			&state->gnutls_internals.buffer_handshake[length],
			state->gnutls_internals.bufferSize_handshake);
		state->gnutls_internals.buffer_handshake =
		    gnutls_realloc(state->gnutls_internals.buffer_handshake,
				   state->gnutls_internals.bufferSize_handshake);
	}


	return length;
}

ssize_t Read(int fd, void *iptr, size_t sizeOfPtr)
{
	size_t left;
	ssize_t i=0;
	char *ptr = iptr;
#ifdef READ_DEBUG
	int j,x, sum=0;
#endif


	left = sizeOfPtr;
	while (left > 0) {
		i = read(fd, &ptr[i], left);
		if (i < 0) {
			return -1;
		} else {
			if (i == 0)
				break;	/* EOF */
		}

		left -= i;

	}

#ifdef READ_DEBUG
	fprintf(stderr, "read %d bytes from %d\n", (sizeOfPtr-left), fd);
	for (x=0;x<((sizeOfPtr-left)/16)+1;x++) {
		fprintf(stderr, "%.4x - ",x);
		for (j=0;j<16;j++) {
			if (sum<(sizeOfPtr-left)) {
				fprintf(stderr, "%.2x ", ((unsigned char*)ptr)[sum++]);
			}
		}
		fprintf(stderr, "\n");
	
	}
#endif


	return (sizeOfPtr - left);
}


ssize_t Write(int fd, const void *iptr, size_t n)
{
	size_t left;
#ifdef WRITE_DEBUG
	int j,x, sum=0;
#endif
	ssize_t i = 0;
	const char *ptr = iptr;

#ifdef WRITE_DEBUG
	fprintf(stderr, "wrote %d bytes to %d\n", n, fd);
	for (x=0;x<(n/16)+1;x++) {
		fprintf(stderr, "%.4x - ",x);
		for (j=0;j<16;j++) {
			if (sum<n) {
				fprintf(stderr, "%.2x ", ((unsigned char*)ptr)[sum++]);
			}
		}
		fprintf(stderr, "\n");
	
	}
#endif
	left = n;
	while (left > 0) {
		i = write(fd, &ptr[i], left);
		if (i <= 0) {
			return -1;
		}
		left -= i;
	}

	return n;

}
ssize_t _gnutls_Send_int(int fd, GNUTLS_STATE state, ContentType type, void *iptr, size_t n)
{
	size_t left;
	ssize_t i = 0;
	char *ptr = iptr;

	left = n;
	while (left > 0) {
		i = gnutls_send_int(fd, state, type, &ptr[i], left);
		if (i <= 0) {
			return i;
		}
		left -= i;
	}

	return n;

}

ssize_t _gnutls_Recv_int(int fd, GNUTLS_STATE state, ContentType type, void *iptr, size_t sizeOfPtr)
{
	size_t left;
	ssize_t i=0;
	char *ptr = iptr;

	left = sizeOfPtr;
	while (left > 0) {
		i = gnutls_recv_int(fd, state, type, &ptr[i], left);
		if (i < 0) {
			return i;
		} else {
			if (i == 0)
				break;	/* EOF */
		}

		left -= i;

	}

	return (sizeOfPtr - left);
}

int gnutls_insertHashDataBuffer( int type, GNUTLS_STATE state, char *data, int length)
{
	int old_buffer;
	
	if (type==GNUTLS_SERVER) {
		old_buffer = state->gnutls_internals.server_hash_bufferSize;

		state->gnutls_internals.server_hash_bufferSize += length;
#ifdef HARD_DEBUG
	fprintf(stderr, "Inserted %d bytes of SSL3 Server Hash Data(%d) into buffer\n", length, type);
#endif
		state->gnutls_internals.server_hash_buffer =
			    gnutls_realloc(state->gnutls_internals.server_hash_buffer,
				   state->gnutls_internals.server_hash_bufferSize);
			memmove(&state->gnutls_internals.server_hash_buffer[old_buffer], data, length);

	} else { /* GNUTLS_CLIENT */
		old_buffer = state->gnutls_internals.client_hash_bufferSize;

		state->gnutls_internals.client_hash_bufferSize += length;
#ifdef HARD_DEBUG
	fprintf(stderr, "Inserted %d bytes of SSL3 Client Hash Data(%d) into buffer\n", length, type);
#endif
		state->gnutls_internals.client_hash_buffer =
			    gnutls_realloc(state->gnutls_internals.client_hash_buffer,
				   state->gnutls_internals.client_hash_bufferSize);
			memmove(&state->gnutls_internals.client_hash_buffer[old_buffer], data, length);
	}
	return 0;
}

int gnutls_getHashDataBufferSize( int type, GNUTLS_STATE state)
{
	if (type==GNUTLS_SERVER) {
		return state->gnutls_internals.server_hash_bufferSize;
	} else {
		return state->gnutls_internals.client_hash_bufferSize;
	}
}

int gnutls_getHashDataFromBuffer(int type, GNUTLS_STATE state, char *data, int length)
{
	if (type==GNUTLS_SERVER) {
		if (length > state->gnutls_internals.server_hash_bufferSize) {
			length = state->gnutls_internals.server_hash_bufferSize;
		}
#ifdef HARD_DEBUG
	fprintf(stderr, "Read %d bytes of SSL3 Server Hash Data(%d) from buffer\n", length, type);
#endif
		state->gnutls_internals.server_hash_bufferSize -= length;
		memmove(data, state->gnutls_internals.server_hash_buffer, length);
		/* overwrite buffer */
		memmove(state->gnutls_internals.server_hash_buffer,
			&state->gnutls_internals.server_hash_buffer[length],
			state->gnutls_internals.server_hash_bufferSize);
		state->gnutls_internals.server_hash_buffer =
		    gnutls_realloc(state->gnutls_internals.server_hash_buffer,
				   state->gnutls_internals.server_hash_bufferSize);

		return length;
	} else { /* CLIENT */
		if (length > state->gnutls_internals.client_hash_bufferSize) {
			length = state->gnutls_internals.client_hash_bufferSize;
		}
#ifdef HARD_DEBUG
	fprintf(stderr, "Read %d bytes of SSL3 Client Hash Data(%d) from buffer\n", length, type);
#endif
		state->gnutls_internals.client_hash_bufferSize -= length;
		memmove(data, state->gnutls_internals.client_hash_buffer, length);
		/* overwrite buffer */
		memmove(state->gnutls_internals.client_hash_buffer,
			&state->gnutls_internals.client_hash_buffer[length],
			state->gnutls_internals.client_hash_bufferSize);
		state->gnutls_internals.client_hash_buffer =
		    gnutls_realloc(state->gnutls_internals.client_hash_buffer,
				   state->gnutls_internals.client_hash_bufferSize);

		return length;	
	}
}