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
|
/*
* Copyright (C) 2010 Martin Willi, revosec AG
* Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
*
* 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 "eap_ttls.h"
#include "eap_ttls_peer.h"
#include <tls.h>
#include <daemon.h>
#include <library.h>
typedef struct private_eap_ttls_t private_eap_ttls_t;
/**
* Private data of an eap_ttls_t object.
*/
struct private_eap_ttls_t {
/**
* Public interface.
*/
eap_ttls_t public;
/**
* Number of EAP-TLS messages processed so far
*/
int processed;
/**
* Is this method instance acting as server?
*/
bool is_server;
/**
* TLS layers
*/
tls_t *tls;
/**
* Allocated input buffer
*/
chunk_t input;
/**
* Number of bytes read in input buffer
*/
size_t inpos;
/**
* Allocated ouput buffer
*/
chunk_t output;
/**
* Number of bytes sent from output buffer
*/
size_t outpos;
};
/** Size limit for a single TLS message */
#define MAX_TLS_MESSAGE_LEN 16384
/** Size of a EAP-TLS fragment */
#define EAP_TTLS_FRAGMENT_LEN 1014
/** Maximum number of EAP-TLS messages/fragments allowed */
#define MAX_EAP_TTLS_MESSAGE_COUNT 16
/**
* Flags of an EAP-TLS message
*/
typedef enum {
EAP_TTLS_LENGTH = (1<<7),
EAP_TTLS_MORE_FRAGS = (1<<6),
EAP_TTLS_START = (1<<5),
EAP_TTLS_VERSION = 0x07
} eap_ttls_flags_t;
/**
* EAP-TLS packet format
*/
typedef struct __attribute__((packed)) {
u_int8_t code;
u_int8_t identifier;
u_int16_t length;
u_int8_t type;
u_int8_t flags;
} eap_ttls_packet_t;
/**
* TLS record
*/
typedef struct __attribute__((packed)) {
u_int8_t type;
u_int16_t version;
u_int16_t length;
char data[];
} tls_record_t;
METHOD(eap_method_t, initiate, status_t,
private_eap_ttls_t *this, eap_payload_t **out)
{
if (this->is_server)
{
eap_ttls_packet_t pkt = {
.type = EAP_TTLS,
.code = EAP_REQUEST,
.flags = EAP_TTLS_START,
};
htoun16(&pkt.length, sizeof(eap_ttls_packet_t));
/* start with non-zero random identifier */
do {
pkt.identifier = random();
} while (!pkt.identifier);
DBG2(DBG_IKE, "sending EAP-TLS start packet");
*out = eap_payload_create_data(chunk_from_thing(pkt));
return NEED_MORE;
}
return FAILED;
}
/**
* Write received TLS data to the input buffer
*/
static bool write_buf(private_eap_ttls_t *this, eap_ttls_packet_t *pkt)
{
u_int32_t msg_len;
u_int16_t pkt_len;
chunk_t data;
pkt_len = untoh16(&pkt->length);
if (pkt->flags & EAP_TTLS_LENGTH)
{
if (pkt_len < sizeof(eap_ttls_packet_t) + sizeof(msg_len))
{
DBG1(DBG_IKE, "EAP-TLS packet too short");
return FALSE;
}
msg_len = untoh32(pkt + 1);
if (msg_len < pkt_len - sizeof(eap_ttls_packet_t) - sizeof(msg_len) ||
msg_len > MAX_TLS_MESSAGE_LEN)
{
DBG1(DBG_IKE, "invalid EAP-TLS packet length");
return FALSE;
}
if (this->input.ptr)
{
if (msg_len != this->input.len)
{
DBG1(DBG_IKE, "received unexpected TLS message length");
return FALSE;
}
}
else
{
this->input = chunk_alloc(msg_len);
this->inpos = 0;
}
data = chunk_create((char*)(pkt + 1) + sizeof(msg_len),
pkt_len - sizeof(eap_ttls_packet_t) - sizeof(msg_len));
}
else
{
data = chunk_create((char*)(pkt + 1),
pkt_len - sizeof(eap_ttls_packet_t));
}
if (data.len > this->input.len - this->inpos)
{
DBG1(DBG_IKE, "EAP-TLS fragment exceeds TLS message length");
return FALSE;
}
memcpy(this->input.ptr + this->inpos, data.ptr, data.len);
this->inpos += data.len;
return TRUE;
}
/**
* Send an ack to request next fragment
*/
static eap_payload_t *create_ack(private_eap_ttls_t *this, u_int8_t identifier)
{
eap_ttls_packet_t pkt = {
.code = this->is_server ? EAP_REQUEST : EAP_RESPONSE,
.identifier = this->is_server ? identifier + 1 : identifier,
.type = EAP_TTLS,
};
htoun16(&pkt.length, sizeof(pkt));
DBG2(DBG_IKE, "sending EAP-TLS acknowledgement packet");
return eap_payload_create_data(chunk_from_thing(pkt));
}
/**
* Create a eap response from data in the TLS output buffer
*/
static eap_payload_t *read_buf(private_eap_ttls_t *this, u_int8_t identifier)
{
char buf[EAP_TTLS_FRAGMENT_LEN + sizeof(eap_ttls_packet_t) + 4], *start;
eap_ttls_packet_t *pkt = (eap_ttls_packet_t*)buf;
u_int16_t pkt_len = sizeof(eap_ttls_packet_t);
pkt->code = this->is_server ? EAP_REQUEST : EAP_RESPONSE;
pkt->identifier = this->is_server ? identifier + 1 : identifier;
pkt->type = EAP_TTLS;
pkt->flags = 0;
if (this->output.len)
{
start = (char*)(pkt + 1);
if (this->outpos == 0)
{ /* first fragment */
pkt->flags = EAP_TTLS_LENGTH;
pkt_len += 4;
start += 4;
htoun32(pkt + 1, this->output.len);
}
if (this->output.len - this->outpos > EAP_TTLS_FRAGMENT_LEN)
{
pkt->flags |= EAP_TTLS_MORE_FRAGS;
pkt_len += EAP_TTLS_FRAGMENT_LEN;
memcpy(start, this->output.ptr + this->outpos, EAP_TTLS_FRAGMENT_LEN);
this->outpos += EAP_TTLS_FRAGMENT_LEN;
DBG2(DBG_IKE, "sending EAP-TLS packet fragment");
}
else
{
pkt_len += this->output.len - this->outpos;
memcpy(start, this->output.ptr + this->outpos,
this->output.len - this->outpos);
chunk_free(&this->output);
this->outpos = 0;
DBG2(DBG_IKE, "sending EAP-TLS packet");
}
}
else
{
DBG2(DBG_IKE, "sending EAP-TLS acknowledgement packet");
}
htoun16(&pkt->length, pkt_len);
return eap_payload_create_data(chunk_create(buf, pkt_len));
}
/**
* Pass data in input buffer to upper layers, write result to output buffer
*/
static status_t process_buf(private_eap_ttls_t *this)
{
tls_record_t *in, out;
chunk_t data;
u_int16_t len;
status_t status;
/* pass input buffer to upper layer, record for record */
data = this->input;
while (data.len > sizeof(tls_record_t))
{
in = (tls_record_t*)data.ptr;
len = untoh16(&in->length);
if (len > data.len - sizeof(tls_record_t))
{
DBG1(DBG_IKE, "TLS record length invalid");
return FAILED;
}
if (untoh16(&in->version) < TLS_1_0)
{
DBG1(DBG_IKE, "%N invalid with EAP-TLS",
tls_version_names, untoh16(&in->version));
return FAILED;
}
status = this->tls->process(this->tls, in->type,
chunk_create(in->data, len));
if (status != NEED_MORE)
{
return status;
}
data = chunk_skip(data, len + sizeof(tls_record_t));
}
chunk_free(&this->input);
this->inpos = 0;
/* read in records from upper layer, append to output buffer */
chunk_free(&this->output);
while (TRUE)
{
tls_content_type_t type;
chunk_t header = chunk_from_thing(out);
status = this->tls->build(this->tls, &type, &data);
switch (status)
{
case NEED_MORE:
break;
case INVALID_STATE:
/* invalid state means we need more input from peer first */
return NEED_MORE;
case SUCCESS:
return SUCCESS;
case FAILED:
default:
return FAILED;
}
out.type = type;
htoun16(&out.version, this->tls->get_version(this->tls));
htoun16(&out.length, data.len);
this->output = chunk_cat("mcm", this->output, header, data);
}
}
METHOD(eap_method_t, process, status_t,
private_eap_ttls_t *this, eap_payload_t *in, eap_payload_t **out)
{
eap_ttls_packet_t *pkt;
chunk_t data;
status_t status;
if (++this->processed > MAX_EAP_TTLS_MESSAGE_COUNT)
{
DBG1(DBG_IKE, "EAP-TTLS packet count exceeded");
return FAILED;
}
data = in->get_data(in);
pkt = (eap_ttls_packet_t*)data.ptr;
if (data.len < sizeof(eap_ttls_packet_t) ||
untoh16(&pkt->length) != data.len)
{
DBG1(DBG_IKE, "invalid EAP-TLS packet length");
return FAILED;
}
if (pkt->flags & EAP_TTLS_START)
{
DBG1(DBG_IKE, "EAP-TTLS version is v%u",
pkt->flags & EAP_TTLS_VERSION);
}
else
{
if (data.len == sizeof(eap_ttls_packet_t))
{
if (this->output.len)
{ /* ACK to our fragment, send next */
*out = read_buf(this, pkt->identifier);
return NEED_MORE;
}
if (this->tls->is_complete(this->tls))
{
return SUCCESS;
}
return FAILED;
}
if (!write_buf(this, pkt))
{
return FAILED;
}
if (pkt->flags & EAP_TTLS_MORE_FRAGS)
{ /* more fragments follow */
*out = create_ack(this, pkt->identifier);
return NEED_MORE;
}
else if (this->input.len != this->inpos)
{
DBG1(DBG_IKE, "defragmented TLS message has invalid length");
return FAILED;
}
}
status = process_buf(this);
if (status == NEED_MORE)
{
*out = read_buf(this, pkt->identifier);
}
return status;
}
METHOD(eap_method_t, get_type, eap_type_t,
private_eap_ttls_t *this, u_int32_t *vendor)
{
*vendor = 0;
return EAP_TTLS;
}
METHOD(eap_method_t, get_msk, status_t,
private_eap_ttls_t *this, chunk_t *msk)
{
*msk = this->tls->get_eap_msk(this->tls);
if (msk->len)
{
return SUCCESS;
}
return FAILED;
}
METHOD(eap_method_t, is_mutual, bool,
private_eap_ttls_t *this)
{
return TRUE;
}
METHOD(eap_method_t, destroy, void,
private_eap_ttls_t *this)
{
free(this->input.ptr);
free(this->output.ptr);
this->tls->destroy(this->tls);
free(this);
}
/**
* Generic private constructor
*/
static eap_ttls_t *eap_ttls_create(identification_t *server,
identification_t *peer, bool is_server,
tls_application_t *application)
{
private_eap_ttls_t *this;
INIT(this,
.public.eap_method = {
.initiate = _initiate,
.process = _process,
.get_type = _get_type,
.is_mutual = _is_mutual,
.get_msk = _get_msk,
.destroy = _destroy,
},
.is_server = is_server,
);
/* MSK PRF ASCII constant label according to EAP-TTLS RFC 5281 */
this->tls = tls_create(is_server, server, peer, "ttls keying material",
application);
return &this->public;
}
eap_ttls_t *eap_ttls_create_server(identification_t *server,
identification_t *peer)
{
return eap_ttls_create(server, peer, TRUE, NULL);
}
eap_ttls_t *eap_ttls_create_peer(identification_t *server,
identification_t *peer)
{
return eap_ttls_create(server, peer, FALSE,
&eap_ttls_peer_create(peer)->application);
}
|