summaryrefslogtreecommitdiff
path: root/src/libtls/tls_fragmentation.c
blob: c76376b432bb2c45dff9fc683359e3923a7dbdf7 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * Copyright (C) 2010 Martin Willi
 * Copyright (C) 2010 revosec AG
 *
 * This program 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.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program 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.
 */

#include "tls_fragmentation.h"

#include <bio/bio_reader.h>
#include <utils/debug.h>

/**
 * Maximum size of a TLS handshake message we accept
 */
#define TLS_MAX_HANDSHAKE_LEN	65536

typedef struct private_tls_fragmentation_t private_tls_fragmentation_t;

/**
 * Alert state
 */
typedef enum {
	/* no alert received/sent */
	ALERT_NONE,
	/* currently sending an alert */
	ALERT_SENDING,
	/* alert sent and out */
	ALERT_SENT,
} alert_state_t;

/**
 * Private data of an tls_fragmentation_t object.
 */
struct private_tls_fragmentation_t {

	/**
	 * Public tls_fragmentation_t interface.
	 */
	tls_fragmentation_t public;

	/**
	 * Upper layer handshake protocol
	 */
	tls_handshake_t *handshake;

	/**
	 * TLS alert handler
	 */
	tls_alert_t *alert;

	/**
	 * State of alert handling
	 */
	alert_state_t state;

	/**
	 * Did the application layer complete successfully?
	 */
	bool application_finished;

	/**
	 * Handshake input buffer
	 */
	chunk_t input;

	/**
	 * Position in input buffer
	 */
	size_t inpos;

	/**
	 * Currently processed handshake message type
	 */
	tls_handshake_type_t type;

	/**
	 * Handshake output buffer
	 */
	chunk_t output;

	/**
	 * Type of data in output buffer
	 */
	tls_content_type_t output_type;

	/**
	 * Upper layer application data protocol
	 */
	tls_application_t *application;
};

/**
 * Process a TLS alert
 */
static status_t process_alert(private_tls_fragmentation_t *this,
							  bio_reader_t *reader)
{
	u_int8_t level, description;

	if (!reader->read_uint8(reader, &level) ||
		!reader->read_uint8(reader, &description))
	{
		this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
		return NEED_MORE;
	}
	return this->alert->process(this->alert, level, description);
}

/**
 * Process TLS handshake protocol data
 */
static status_t process_handshake(private_tls_fragmentation_t *this,
								  bio_reader_t *reader)
{
	while (reader->remaining(reader))
	{
		bio_reader_t *msg;
		u_int8_t type;
		u_int32_t len;
		status_t status;
		chunk_t data;

		if (reader->remaining(reader) > TLS_MAX_FRAGMENT_LEN)
		{
			DBG1(DBG_TLS, "TLS fragment has invalid length");
			this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
			return NEED_MORE;
		}

		if (this->input.len == 0)
		{	/* new handshake message */
			if (!reader->read_uint8(reader, &type) ||
				!reader->read_uint24(reader, &len))
			{
				DBG1(DBG_TLS, "TLS handshake header invalid");
				this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
				return NEED_MORE;
			}
			this->type = type;
			if (len > TLS_MAX_HANDSHAKE_LEN)
			{
				DBG1(DBG_TLS, "TLS handshake exceeds maximum length");
				this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
				return NEED_MORE;
			}
			chunk_free(&this->input);
			this->inpos = 0;
			if (len)
			{
				this->input = chunk_alloc(len);
			}
		}

		len = min(this->input.len - this->inpos, reader->remaining(reader));
		if (!reader->read_data(reader, len, &data))
		{
			DBG1(DBG_TLS, "TLS fragment has invalid length");
			this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
			return NEED_MORE;
		}
		memcpy(this->input.ptr + this->inpos, data.ptr, len);
		this->inpos += len;

		if (this->input.len == this->inpos)
		{	/* message completely defragmented, process */
			msg = bio_reader_create(this->input);
			DBG2(DBG_TLS, "received TLS %N handshake (%u bytes)",
				 tls_handshake_type_names, this->type, this->input.len);
			status = this->handshake->process(this->handshake, this->type, msg);
			msg->destroy(msg);
			chunk_free(&this->input);
			if (status != NEED_MORE)
			{
				return status;
			}
		}
		if (this->alert->fatal(this->alert))
		{
			break;
		}
	}
	return NEED_MORE;
}

/**
 * Process TLS application data
 */
static status_t process_application(private_tls_fragmentation_t *this,
									bio_reader_t *reader)
{
	if (!this->handshake->finished(this->handshake))
	{
		DBG1(DBG_TLS, "received TLS application data, "
			 "but handshake not finished");
		return FAILED;
	}
	while (reader->remaining(reader))
	{
		status_t status;
		chunk_t data;

		if (reader->remaining(reader) > TLS_MAX_FRAGMENT_LEN)
		{
			DBG1(DBG_TLS, "TLS fragment has invalid length");
			this->alert->add(this->alert, TLS_FATAL, TLS_DECODE_ERROR);
			return NEED_MORE;
		}
		data = reader->peek(reader);
		DBG3(DBG_TLS, "%B", &data);
		status = this->application->process(this->application, reader);
		switch (status)
		{
			case NEED_MORE:
				continue;
			case SUCCESS:
				this->application_finished = TRUE;
				return SUCCESS;
			case FAILED:
			default:
				this->alert->add(this->alert, TLS_FATAL, TLS_CLOSE_NOTIFY);
				return NEED_MORE;
		}
	}
	return NEED_MORE;
}

METHOD(tls_fragmentation_t, process, status_t,
	private_tls_fragmentation_t *this, tls_content_type_t type, chunk_t data)
{
	bio_reader_t *reader;
	status_t status;

	switch (this->state)
	{
		case ALERT_SENDING:
		case ALERT_SENT:
			/* don't accept more input, fatal error occurred */
			return NEED_MORE;
		case ALERT_NONE:
			break;
	}
	reader = bio_reader_create(data);
	switch (type)
	{
		case TLS_CHANGE_CIPHER_SPEC:
			if (this->handshake->cipherspec_changed(this->handshake, TRUE))
			{
				this->handshake->change_cipherspec(this->handshake, TRUE);
				status = NEED_MORE;
				break;
			}
			status = FAILED;
			break;
		case TLS_ALERT:
			status = process_alert(this, reader);
			break;
		case TLS_HANDSHAKE:
			status = process_handshake(this, reader);
			break;
		case TLS_APPLICATION_DATA:
			status = process_application(this, reader);
			break;
		default:
			DBG1(DBG_TLS, "received unknown TLS content type %d, ignored", type);
			status = NEED_MORE;
			break;
	}
	reader->destroy(reader);
	return status;
}

/**
 * Check if alerts are pending
 */
static bool check_alerts(private_tls_fragmentation_t *this, chunk_t *data)
{
	tls_alert_level_t level;
	tls_alert_desc_t desc;
	bio_writer_t *writer;

	if (this->alert->get(this->alert, &level, &desc))
	{
		writer = bio_writer_create(2);

		writer->write_uint8(writer, level);
		writer->write_uint8(writer, desc);

		*data = chunk_clone(writer->get_buf(writer));
		writer->destroy(writer);
		return TRUE;
	}
	return FALSE;
}

/**
 * Build hanshake message
 */
static status_t build_handshake(private_tls_fragmentation_t *this)
{
	bio_writer_t *hs, *msg;
	tls_handshake_type_t type;
	status_t status;

	msg = bio_writer_create(64);
	while (TRUE)
	{
		hs = bio_writer_create(64);
		status = this->handshake->build(this->handshake, &type, hs);
		switch (status)
		{
			case NEED_MORE:
				if (this->alert->fatal(this->alert))
				{
					break;
				}
				msg->write_uint8(msg, type);
				msg->write_data24(msg, hs->get_buf(hs));
				DBG2(DBG_TLS, "sending TLS %N handshake (%u bytes)",
					 tls_handshake_type_names, type, hs->get_buf(hs).len);
				if (!this->handshake->cipherspec_changed(this->handshake, FALSE))
				{
					hs->destroy(hs);
					continue;
				}
				/* FALL */
			case INVALID_STATE:
				this->output_type = TLS_HANDSHAKE;
				this->output = chunk_clone(msg->get_buf(msg));
				break;
			default:
				break;
		}
		hs->destroy(hs);
		break;
	}
	msg->destroy(msg);
	return status;
}

/**
 * Build TLS application data
 */
static status_t build_application(private_tls_fragmentation_t *this)
{
	bio_writer_t *msg;
	status_t status;

	msg = bio_writer_create(64);
	while (TRUE)
	{
		status = this->application->build(this->application, msg);
		switch (status)
		{
			case NEED_MORE:
				continue;
			case INVALID_STATE:
				this->output_type = TLS_APPLICATION_DATA;
				this->output = chunk_clone(msg->get_buf(msg));
				break;
			case SUCCESS:
				this->application_finished = TRUE;
				break;
			case FAILED:
			default:
				this->alert->add(this->alert, TLS_FATAL, TLS_CLOSE_NOTIFY);
				break;
		}
		break;
	}
	msg->destroy(msg);
	return status;
}

METHOD(tls_fragmentation_t, build, status_t,
	private_tls_fragmentation_t *this, tls_content_type_t *type, chunk_t *data)
{
	status_t status = INVALID_STATE;

	switch (this->state)
	{
		case ALERT_SENDING:
			this->state = ALERT_SENT;
			return INVALID_STATE;
		case ALERT_SENT:
			return FAILED;
		case ALERT_NONE:
			break;
	}
	if (check_alerts(this, data))
	{
		this->state = ALERT_SENDING;
		*type = TLS_ALERT;
		return NEED_MORE;
	}
	if (!this->output.len)
	{
		if (this->handshake->cipherspec_changed(this->handshake, FALSE))
		{
			this->handshake->change_cipherspec(this->handshake, FALSE);
			*type = TLS_CHANGE_CIPHER_SPEC;
			*data = chunk_clone(chunk_from_chars(0x01));
			return NEED_MORE;
		}
		if (!this->handshake->finished(this->handshake))
		{
			status = build_handshake(this);
		}
		else if (this->application)
		{
			status = build_application(this);
		}
		if (check_alerts(this, data))
		{
			this->state = ALERT_SENDING;
			*type = TLS_ALERT;
			return NEED_MORE;
		}
	}
	if (this->output.len)
	{
		*type = this->output_type;
		if (this->output.len <= TLS_MAX_FRAGMENT_LEN)
		{
			*data = this->output;
			this->output = chunk_empty;
			return NEED_MORE;
		}
		*data = chunk_create(this->output.ptr, TLS_MAX_FRAGMENT_LEN);
		this->output = chunk_clone(chunk_skip(this->output, TLS_MAX_FRAGMENT_LEN));
		return NEED_MORE;
	}
	return status;
}

METHOD(tls_fragmentation_t, application_finished, bool,
	private_tls_fragmentation_t *this)
{
	return this->application_finished;
}

METHOD(tls_fragmentation_t, destroy, void,
	private_tls_fragmentation_t *this)
{
	free(this->input.ptr);
	free(this->output.ptr);
	free(this);
}

/**
 * See header
 */
tls_fragmentation_t *tls_fragmentation_create(tls_handshake_t *handshake,
							tls_alert_t *alert, tls_application_t *application)
{
	private_tls_fragmentation_t *this;

	INIT(this,
		.public = {
			.process = _process,
			.build = _build,
			.application_finished = _application_finished,
			.destroy = _destroy,
		},
		.handshake = handshake,
		.alert = alert,
		.state = ALERT_NONE,
		.application = application,
	);

	return &this->public;
}