aboutsummaryrefslogtreecommitdiffstats
path: root/src/libpttls/pt_tls_server.c
blob: a1c645319bc7c17f990aee34ccb132439241998a (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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
 * Copyright (C) 2012 Martin Willi
 * Copyright (C) 2012 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 "pt_tls_server.h"

#include <sasl/sasl_mechanism.h>

#include <utils/debug.h>

typedef struct private_pt_tls_server_t private_pt_tls_server_t;

/**
 * Private data of an pt_tls_server_t object.
 */
struct private_pt_tls_server_t {

	/**
	 * Public pt_tls_server_t interface.
	 */
	pt_tls_server_t public;

	/**
	 * TLS protected socket
	 */
	tls_socket_t *tls;

	/**
	 * Client authentication requirements
	 */
	pt_tls_auth_t auth;

	enum {
		/* expecting version negotiation */
		PT_TLS_SERVER_VERSION,
		/* expecting an SASL exchange */
		PT_TLS_SERVER_AUTH,
		/* expecting TNCCS exchange */
		PT_TLS_SERVER_TNCCS,
		/* terminating state */
		PT_TLS_SERVER_END,
	} state;

	/**
	 * Message Identifier
	 */
	uint32_t identifier;

	/**
	 * TNCCS protocol handler, implemented as tls_t
	 */
	tls_t *tnccs;

};

/**
 * Negotiate PT-TLS version
 */
static bool negotiate_version(private_pt_tls_server_t *this)
{
	bio_reader_t *reader;
	bio_writer_t *writer;
	uint32_t vendor, type, identifier;
	uint8_t reserved, vmin, vmax, vpref;
	bool res;

	reader = pt_tls_read(this->tls, &vendor, &type, &identifier);
	if (!reader)
	{
		return FALSE;
	}
	if (vendor != 0 || type != PT_TLS_VERSION_REQUEST ||
		!reader->read_uint8(reader, &reserved) ||
		!reader->read_uint8(reader, &vmin) ||
		!reader->read_uint8(reader, &vmax) ||
		!reader->read_uint8(reader, &vpref))
	{
		DBG1(DBG_TNC, "PT-TLS version negotiation failed");
		reader->destroy(reader);
		return FALSE;
	}
	reader->destroy(reader);

	if (vmin > PT_TLS_VERSION || vmax < PT_TLS_VERSION)
	{
		/* TODO: send error */
		return FALSE;
	}

	writer = bio_writer_create(4);
	writer->write_uint24(writer, 0);
	writer->write_uint8(writer, PT_TLS_VERSION);
	res = pt_tls_write(this->tls, PT_TLS_VERSION_RESPONSE,
					   this->identifier++, writer->get_buf(writer));
	writer->destroy(writer);
	return res;
}

/**
 * Process SASL data, send result
 */
static status_t process_sasl(private_pt_tls_server_t *this,
							 sasl_mechanism_t *sasl, chunk_t data)
{
	bio_writer_t *writer;
	identification_t *client;
	tnccs_t *tnccs;
	bool res;

	switch (sasl->process(sasl, data))
	{
		case NEED_MORE:
			return NEED_MORE;
		case SUCCESS:
			DBG1(DBG_TNC, "SASL %s authentication successful",
				 sasl->get_name(sasl));
			client = sasl->get_client(sasl);
			if (client)
			{
				DBG1(DBG_TNC, "SASL client identity is '%Y'", client);
				this->tnccs->set_peer_id(this->tnccs, client);
				if (streq(sasl->get_name(sasl), "PLAIN"))
				{
					tnccs = (tnccs_t*)this->tnccs;
					tnccs->set_auth_type(tnccs, TNC_AUTH_PASSWORD);
				}
			}
			writer = bio_writer_create(1);
			writer->write_uint8(writer, PT_TLS_SASL_RESULT_SUCCESS);
			res = pt_tls_write(this->tls, PT_TLS_SASL_RESULT,
							   this->identifier++, writer->get_buf(writer));
			writer->destroy(writer);
			return res ? SUCCESS : FAILED;
		case FAILED:
		default:
			DBG1(DBG_TNC, "SASL %s authentication failed",
				 sasl->get_name(sasl));
			writer = bio_writer_create(1);
			/* sending abort does not allow the client to retry */
			writer->write_uint8(writer, PT_TLS_SASL_RESULT_ABORT);
			pt_tls_write(this->tls, PT_TLS_SASL_RESULT,
						 this->identifier++, writer->get_buf(writer));
			return FAILED;
	}
}

/**
 * Read a SASL message and process it
 */
static status_t read_sasl(private_pt_tls_server_t *this,
						  sasl_mechanism_t *sasl)
{
	uint32_t vendor, type, identifier;
	bio_reader_t *reader;
	status_t status;
	chunk_t data;

	reader = pt_tls_read(this->tls, &vendor, &type, &identifier);
	if (!reader)
	{
		return FAILED;
	}
	if (vendor != 0 || type != PT_TLS_SASL_AUTH_DATA ||
		!reader->read_data(reader, reader->remaining(reader), &data))
	{
		reader->destroy(reader);
		return FAILED;
	}
	status = process_sasl(this, sasl, data);
	reader->destroy(reader);
	return status;
}

/**
 * Build and write SASL message, or result message
 */
static status_t write_sasl(private_pt_tls_server_t *this,
						   sasl_mechanism_t *sasl)
{
	bio_writer_t *writer;
	chunk_t chunk;
	bool res;

	switch (sasl->build(sasl, &chunk))
	{
		case NEED_MORE:
			res = pt_tls_write(this->tls, PT_TLS_SASL_AUTH_DATA,
							   this->identifier++, chunk);
			free(chunk.ptr);
			return res ? NEED_MORE : FAILED;
		case SUCCESS:
			DBG1(DBG_TNC, "SASL %s authentication successful",
				 sasl->get_name(sasl));
			writer = bio_writer_create(1 + chunk.len);
			writer->write_uint8(writer, PT_TLS_SASL_RESULT_SUCCESS);
			writer->write_data(writer, chunk);
			free(chunk.ptr);
			res = pt_tls_write(this->tls, PT_TLS_SASL_RESULT,
							   this->identifier++, writer->get_buf(writer));
			writer->destroy(writer);
			return res ? SUCCESS : FAILED;
		case FAILED:
		default:
			DBG1(DBG_TNC, "SASL %s authentication failed",
				 sasl->get_name(sasl));
			/* sending abort does not allow the client to retry */
			chunk = chunk_from_chars(PT_TLS_SASL_RESULT_ABORT);
			pt_tls_write(this->tls, PT_TLS_SASL_RESULT,
						 this->identifier++, chunk);
			return FAILED;
	}
}

/**
 * Send the list of supported SASL mechanisms
 */
static bool send_sasl_mechs(private_pt_tls_server_t *this)
{
	enumerator_t *enumerator;
	bio_writer_t *writer = NULL;
	char *name;
	bool res;

	enumerator = sasl_mechanism_create_enumerator(TRUE);
	while (enumerator->enumerate(enumerator, &name))
	{
		if (!writer)
		{
			writer = bio_writer_create(32);
		}
		DBG1(DBG_TNC, "offering SASL %s", name);
		writer->write_data8(writer, chunk_from_str(name));
	}
	enumerator->destroy(enumerator);

	if (!writer)
	{	/* no mechanisms available? */
		return FALSE;
	}
	res = pt_tls_write(this->tls, PT_TLS_SASL_MECHS,
					   this->identifier++, writer->get_buf(writer));
	writer->destroy(writer);
	return res;
}

/**
 * Read the selected SASL mechanism, and process piggybacked data
 */
static status_t read_sasl_mech_selection(private_pt_tls_server_t *this,
										 sasl_mechanism_t **out)
{
	uint32_t vendor, type, identifier;
	sasl_mechanism_t *sasl;
	bio_reader_t *reader;
	chunk_t chunk;
	uint8_t len;
	char buf[21];

	reader = pt_tls_read(this->tls, &vendor, &type, &identifier);
	if (!reader)
	{
		return FAILED;
	}
	if (vendor != 0 || type != PT_TLS_SASL_MECH_SELECTION ||
		!reader->read_uint8(reader, &len) ||
		!reader->read_data(reader, len & 0x1F, &chunk))
	{
		reader->destroy(reader);
		return FAILED;
	}
	snprintf(buf, sizeof(buf), "%.*s", (int)chunk.len, chunk.ptr);

	DBG1(DBG_TNC, "client starts SASL %s authentication", buf);

	sasl = sasl_mechanism_create(buf, NULL);
	if (!sasl)
	{
		reader->destroy(reader);
		return FAILED;
	}
	/* initial SASL data piggybacked? */
	if (reader->remaining(reader))
	{
		switch (process_sasl(this, sasl, reader->peek(reader)))
		{
			case NEED_MORE:
				break;
			case SUCCESS:
				reader->destroy(reader);
				*out = sasl;
				return SUCCESS;
			case FAILED:
			default:
				reader->destroy(reader);
				sasl->destroy(sasl);
				return FAILED;
		}
	}
	reader->destroy(reader);
	*out = sasl;
	return NEED_MORE;
}

/**
 * Do a single SASL exchange
 */
static bool do_sasl(private_pt_tls_server_t *this)
{
	sasl_mechanism_t *sasl;
	identification_t *client_id;
	tnccs_t *tnccs;
	status_t status;

	client_id = this->tls->get_peer_id(this->tls);
	tnccs = (tnccs_t*)this->tnccs;

	switch (this->auth)
	{
		case PT_TLS_AUTH_NONE:
			return TRUE;
		case PT_TLS_AUTH_TLS:
			if (client_id)
			{
				this->tnccs->set_peer_id(this->tnccs, client_id);
				tnccs->set_auth_type(tnccs, TNC_AUTH_X509_CERT);
				return TRUE;
			}
			DBG1(DBG_TNC, "requiring TLS certificate-based "
						  "client authentication");
			return FALSE;
		case PT_TLS_AUTH_SASL:
			break;
		case PT_TLS_AUTH_TLS_OR_SASL:
			if (client_id)
			{
				this->tnccs->set_peer_id(this->tnccs, client_id);
				tnccs->set_auth_type(tnccs, TNC_AUTH_X509_CERT);
				DBG1(DBG_TNC, "skipping SASL, client already authenticated by "
							  "TLS certificate");
				return TRUE;
			}
			break;
		case PT_TLS_AUTH_TLS_AND_SASL:
		default:
			if (!client_id)
			{
				DBG1(DBG_TNC, "requiring TLS certificate-based "
							  "client authentication");
				return FALSE;
			}
			break;
	}

	if (!send_sasl_mechs(this))
	{
		return FALSE;
	}
	status = read_sasl_mech_selection(this, &sasl);
	if (status == FAILED)
	{
		return FALSE;
	}
	while (status == NEED_MORE)
	{
		status = write_sasl(this, sasl);
		if (status == NEED_MORE)
		{
			status = read_sasl(this, sasl);
		}
	}
	sasl->destroy(sasl);
	return status == SUCCESS;
}

/**
 * Authenticated PT-TLS session with a single SASL method
 */
static bool authenticate(private_pt_tls_server_t *this)
{
	if (do_sasl(this))
	{
		/* complete SASL with emtpy mechanism list */
		return pt_tls_write(this->tls, PT_TLS_SASL_MECHS, this->identifier++,
							chunk_empty);
	}
	return FALSE;
}

/**
 * Perform assessment
 */
static status_t assess(private_pt_tls_server_t *this, tls_t *tnccs)
{
	size_t msglen;
	size_t buflen = PT_TLS_MAX_MESSAGE_LEN;
	char buf[buflen];
	bio_reader_t *reader;
	uint32_t vendor, type, identifier;
	chunk_t data;
	status_t status;

	reader = pt_tls_read(this->tls, &vendor, &type, &identifier);
	if (!reader)
	{
		return FAILED;
	}
	if (vendor == 0)
	{
		if (type == PT_TLS_ERROR)
		{
			DBG1(DBG_TNC, "received PT-TLS error");
			reader->destroy(reader);
			return FAILED;
		}
		if (type != PT_TLS_PB_TNC_BATCH)
		{
			DBG1(DBG_TNC, "unexpected PT-TLS message: %d", type);
			reader->destroy(reader);
			return FAILED;
		}
		data = reader->peek(reader);
		switch (tnccs->process(tnccs, data.ptr, data.len))
		{
			case SUCCESS:
				reader->destroy(reader);
				return tnccs->is_complete(tnccs) ? SUCCESS : FAILED;
			case FAILED:
			default:
				reader->destroy(reader);
				return FALSE;
			case NEED_MORE:
				break;
		}
	}
	else
	{
		DBG1(DBG_TNC, "ignoring vendor specific PT-TLS message");
	}
	reader->destroy(reader);

	status = tnccs->build(tnccs, buf, &buflen, &msglen);
	if (status == ALREADY_DONE)
	{
		data = chunk_create(buf, buflen);
		if (!pt_tls_write(this->tls, PT_TLS_PB_TNC_BATCH,
						  this->identifier++, data))
		{
			return FAILED;
		}
	}
	return status;
}

METHOD(pt_tls_server_t, handle, status_t,
	private_pt_tls_server_t *this)
{
	switch (this->state)
	{
		case PT_TLS_SERVER_VERSION:
			DBG1(DBG_TNC, "entering PT-TLS negotiation phase");
			if (!negotiate_version(this))
			{
				return FAILED;
			}
			DBG1(DBG_TNC, "negotiated PT-TLS version %d", PT_TLS_VERSION);
			this->state = PT_TLS_SERVER_AUTH;
			/* fall through to next state */
		case PT_TLS_SERVER_AUTH:
			DBG1(DBG_TNC, "doing SASL client authentication");
			if (!authenticate(this))
			{
				return FAILED;
			}
			this->state = PT_TLS_SERVER_TNCCS;
			DBG1(DBG_TNC, "entering PT-TLS data transport phase");
			break;
		case PT_TLS_SERVER_TNCCS:
			switch (assess(this, (tls_t*)this->tnccs))
			{
				case SUCCESS:
					this->state = PT_TLS_SERVER_END;
					return SUCCESS;
				case FAILED:
					return FAILED;
				default:
					break;
			}
			break;
		default:
			return FAILED;
	}
	return NEED_MORE;
}

METHOD(pt_tls_server_t, get_fd, int,
	private_pt_tls_server_t *this)
{
	return this->tls->get_fd(this->tls);
}

METHOD(pt_tls_server_t, destroy, void,
	private_pt_tls_server_t *this)
{
	this->tnccs->destroy(this->tnccs);
	this->tls->destroy(this->tls);
	free(this);
}

/**
 * See header
 */
pt_tls_server_t *pt_tls_server_create(identification_t *server, int fd,
									  pt_tls_auth_t auth, tnccs_t *tnccs)
{
	private_pt_tls_server_t *this;

	INIT(this,
		.public = {
			.handle = _handle,
			.get_fd = _get_fd,
			.destroy = _destroy,
		},
		.state = PT_TLS_SERVER_VERSION,
		.tls = tls_socket_create(TRUE, server, NULL, fd, NULL, TLS_1_2, FALSE),
		.tnccs = (tls_t*)tnccs,
		.auth = auth,
	);

	if (!this->tls)
	{
		this->tnccs->destroy(this->tnccs);
		free(this);
		return NULL;
	}

	return &this->public;
}