~ubuntu-branches/ubuntu/feisty/freeradius/feisty-security

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
/*
 * eap_leap.c  EAP LEAP functionality.
 *
 * Version:     $Id: eap_leap.c,v 1.7.4.1 2006/02/06 16:23:54 nbk Exp $
 *
 *   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.
 *
 *   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.
 *
 *   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
 *
 * Copyright 2003 Alan DeKok <aland@freeradius.org>
 */

/*
 *
 *  LEAP Packet Format in EAP Type-Data
 *  --- ------ ------ -- --- ---------
 *    0                   1                   2		        3
 *   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |     Type 0x11 |  Version 0x01 | Unused 0x00   | Count 0x08    |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |               Peer Challenge		                   |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |               Peer Challenge		                   |
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 *  |   User Name .....
 *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-
 *
 *  Count is 8 octets since the Peer challenge is 8 bytes.
 *  Count is 24 for EAP response, with MSCHAP response.
 *  Length is the total number of octets in the EAP-Message.
 *
 *  The LEAP type (0x11) is *not* included in the type data...
 */

#include <stdio.h>
#include <stdlib.h>
#include "eap.h"
#include "eap_leap.h"

/*
 *      Allocate a new LEAP_PACKET
 */
LEAP_PACKET *eapleap_alloc(void)
{
	LEAP_PACKET   *rp;

	if ((rp = malloc(sizeof(LEAP_PACKET))) == NULL) {
		radlog(L_ERR, "rlm_eap_leap: out of memory");
		return NULL;
	}
	memset(rp, 0, sizeof(LEAP_PACKET));
	return rp;
}

/*
 *      Free LEAP_PACKET
 */
void eapleap_free(LEAP_PACKET **leap_packet_ptr)
{
	LEAP_PACKET *leap_packet;

	if (!leap_packet_ptr) return;
	leap_packet = *leap_packet_ptr;
	if (leap_packet == NULL) return;

	if (leap_packet->challenge) free(leap_packet->challenge);
	if (leap_packet->name) free(leap_packet->name);

	free(leap_packet);

	*leap_packet_ptr = NULL;
}

/*
 *   Extract the data from the LEAP packet.
 */
LEAP_PACKET *eapleap_extract(EAP_DS *eap_ds)
{
	leap_packet_t	*data;
	LEAP_PACKET	*packet;
	int		name_len;

	/*
	 *	LEAP can have EAP-Response or EAP-Request (step 5)
	 *	messages sent to it.
	 */
	if (!eap_ds ||
	    !eap_ds->response ||
	    ((eap_ds->response->code != PW_EAP_RESPONSE) &&
	     (eap_ds->response->code != PW_EAP_REQUEST)) ||
	    eap_ds->response->type.type != PW_EAP_LEAP ||
	    !eap_ds->response->type.data ||
	    (eap_ds->response->length < LEAP_HEADER_LEN) ||
	    (eap_ds->response->type.data[0] != 0x01)) {	/* version 1 */
		radlog(L_ERR, "rlm_eap_leap: corrupted data");
		return NULL;
	}

	/*
	 *	Hmm... this cast isn't the best thing to do.
	 */
	data = (leap_packet_t *)eap_ds->response->type.data;

	/*
	 *	Some simple sanity checks on the incoming packet.
	 *
	 *	See 'leap.txt' in this directory for a description
	 *	of the stages.
	 */
	switch (eap_ds->response->code) {
	case PW_EAP_RESPONSE:
		if (data->count != 24) {
			radlog(L_ERR, "rlm_eap_leap: Bad NTChallengeResponse in LEAP stage 3");
			return NULL;
		}
		break;

	case PW_EAP_REQUEST:
		if (data->count != 8) {
			radlog(L_ERR, "rlm_eap_leap: Bad AP Challenge in LEAP stage 5");
			return NULL;
		}
		break;

	default:
		radlog(L_ERR, "rlm_eap_leap: Invalid EAP code %d",
		       eap_ds->response->code);
		return NULL;
		break;
	}

	packet = eapleap_alloc();
	if (!packet) return NULL;

	/*
	 *	Remember code, length, and id.
	 */
	packet->code = eap_ds->response->code;
	packet->id = eap_ds->response->id;

	/*
	 *	The size of the LEAP portion of the packet, not
	 *	counting the EAP header and the type.
	 */
	packet->length = eap_ds->response->length - EAP_HEADER_LEN - 1;

	/*
	 *	Remember the length of the challenge.
	 */
	packet->count = data->count;

	packet->challenge = malloc(packet->count);
	if (packet->challenge == NULL) {
		radlog(L_ERR, "rlm_eap_leap: out of memory");
		eapleap_free(&packet);
		return NULL;
	}
	memcpy(packet->challenge, data->challenge, packet->count);

	/*
	 *	The User-Name comes after the challenge.
	 *
	 *	Length of the EAP-LEAP portion of the packet, minus
	 *	3 octets for data, minus the challenge size, is the
	 *	length of the user name.
	 */
	name_len = packet->length - 3 - packet->count;
	if (name_len > 0) {
		packet->name = malloc(name_len + 1);
		if (!packet->name) {
			radlog(L_ERR, "rlm_eap_leap: out of memory");
			eapleap_free(&packet);
			return NULL;
		}
		memcpy(packet->name, &data->challenge[packet->count],
		       name_len);
		packet->name[name_len] = '\0';
		packet->name_len = name_len;
	}

	return packet;
}

/*
 *  Get the NT-Password hash.
 */
static int eapleap_ntpwdhash(unsigned char *ntpwdhash, VALUE_PAIR *password)
{
	if (password->attribute == PW_PASSWORD) {
		int i;
		unsigned char unicode[512];

		/*
		 *	Convert the password to NT's weird Unicode format.
		 */
		memset(unicode, 0, sizeof(unicode));
		for (i = 0; i < password->length; i++) {
			/*
			 *  Yes, the *even* bytes have the values,
			 *  and the *odd* bytes are zero.
			 */
			unicode[(i << 1)] = password->strvalue[i];
		}

		/*
		 *  Get the NT Password hash.
		 */
		md4_calc(ntpwdhash, unicode, password->length * 2);

	} else {		/* MUST be NT-Password */
		if (password->length == 32) {
			password->length = lrad_hex2bin(password->strvalue,
							password->strvalue,
							16);
		}
		if (password->length != 16) {
			radlog(L_ERR, "rlm_eap_leap: Bad NT-Password");
			return 0;
		}

		memcpy(ntpwdhash, password->strvalue, 16);
	}
	return 1;
}


/*
 *	Verify the MS-CHAP response from the user.
 */
int eapleap_stage4(LEAP_PACKET *packet, VALUE_PAIR* password,
		   leap_session_t *session)
{
	unsigned char ntpwdhash[16];
	unsigned char response[24];


	/*
	 *	No password or previous packet.  Die.
	 */
	if ((password == NULL) || (session == NULL)) {
		return 0;
	}

	if (!eapleap_ntpwdhash(ntpwdhash, password)) {
		return 0;
	}

	/*
	 *	Calculate and verify the CHAP challenge.
	 */
	eapleap_mschap(ntpwdhash, session->peer_challenge, response);
	if (memcmp(response, packet->challenge, 24) == 0) {
		DEBUG2("  rlm_eap_leap: NtChallengeResponse from AP is valid");
		memcpy(session->peer_response, response, sizeof(response));
		return 1;
	}

	DEBUG2("  rlm_eap_leap: FAILED incorrect NtChallengeResponse from AP");
	return 0;
}

/*
 *	Verify ourselves to the AP
 */
LEAP_PACKET *eapleap_stage6(LEAP_PACKET *packet, REQUEST *request,
			    VALUE_PAIR *user_name, VALUE_PAIR* password,
			    leap_session_t *session, VALUE_PAIR **reply_vps)
{
	int i;
	unsigned char ntpwdhash[16], ntpwdhashhash[16];
	unsigned char buffer[256];
	LEAP_PACKET *reply;
	char *p;
	VALUE_PAIR *vp;

	/*
	 *	No password or previous packet.  Die.
	 */
	if ((password == NULL) || (session == NULL)) {
		return NULL;
	}

	reply = eapleap_alloc();
	if (!reply) return NULL;

	reply->code = PW_EAP_RESPONSE;
	reply->length = LEAP_HEADER_LEN + 24 + user_name->length;
	reply->count = 24;

	reply->challenge = malloc(reply->count);
	if (reply->challenge == NULL) {
		radlog(L_ERR, "rlm_eap_leap: out of memory");
		eapleap_free(&reply);
		return NULL;
	}

	/*
	 *	The LEAP packet also contains the user name.
	 */
	reply->name = malloc(user_name->length + 1);
	if (reply->name == NULL) {
		radlog(L_ERR, "rlm_eap_leap: out of memory");
		eapleap_free(&reply);
		return NULL;
	}

	/*
	 *	Copy the name over, and ensure it's NUL terminated.
	 */
	memcpy(reply->name, user_name->strvalue, user_name->length);
	reply->name[user_name->length] = '\0';
	reply->name_len = user_name->length;

	/*
	 *  MPPE hash = ntpwdhash(ntpwdhash(unicode(pw)))
	 */
	if (!eapleap_ntpwdhash(ntpwdhash, password)) {
		eapleap_free(&reply);
		return NULL;
	}
	md4_calc(ntpwdhashhash, ntpwdhash, 16);

	/*
	 *	Calculate our response, to authenticate ourselves
	 *	to the AP.
	 */
	eapleap_mschap(ntpwdhashhash, packet->challenge, reply->challenge);

	/*
	 *  Calculate the leap:session-key attribute
	 */
	vp = pairmake("Cisco-AVPair", "leap:session-key=", T_OP_ADD);
	if (!vp) {
		radlog(L_ERR, "rlm_eap_leap: Failed to create Cisco-AVPair attribute.  LEAP cancelled.");
		eapleap_free(&reply);
		return NULL;
	}

	/*
	 *	And calculate the MPPE session key.
	 */
	p = buffer;
	memcpy(p, ntpwdhashhash, 16); /* MPPEHASH */
	p += 16;
	memcpy(p, packet->challenge, 8); /* APC */
	p += 8;
	memcpy(p, reply->challenge, 24); /* APR */
	p += 24;
	memcpy(p, session->peer_challenge, 8); /* PC */
	p += 8;
	memcpy(p, session->peer_response, 24); /* PR */
	p += 24;

	/*
	 *	These 16 bytes are the session key to use.
	 */
	librad_md5_calc(ntpwdhash, buffer, 16 + 8 + 24 + 8 + 24);

	memcpy(vp->strvalue + vp->length, ntpwdhash, 16);
	memset(vp->strvalue + vp->length + 16, 0,
	       sizeof(vp->strvalue) - (vp->length + 16));

	i = 16;
	rad_tunnel_pwencode(vp->strvalue + vp->length, &i,
			    request->secret, request->packet->vector);
	vp->length += i;
	pairadd(reply_vps, vp);

	return reply;
}

/*
 *	If an EAP LEAP request needs to be initiated then
 *	create such a packet.
 */
LEAP_PACKET *eapleap_initiate(UNUSED EAP_DS *eap_ds, VALUE_PAIR *user_name)
{
	int i;
	LEAP_PACKET 	*reply;

	reply = eapleap_alloc();
	if (reply == NULL)  {
		radlog(L_ERR, "rlm_eap_leap: out of memory");
		return NULL;
	}

	reply->code = PW_EAP_REQUEST;
	reply->length = LEAP_HEADER_LEN + 8 + user_name->length;
	reply->count = 8;	/* random challenge */

	reply->challenge = malloc(reply->count);
	if (reply->challenge == NULL) {
		radlog(L_ERR, "rlm_eap_leap: out of memory");
		eapleap_free(&reply);
		return NULL;
	}

	/*
	 *	Fill the challenge with random bytes.
	 */
	for (i = 0; i < reply->count; i++) {
		reply->challenge[i] = lrad_rand();
	}

	DEBUG2("  rlm_eap_leap: Issuing AP Challenge");

	/*
	 *	The LEAP packet also contains the user name.
	 */
	reply->name = malloc(user_name->length + 1);
	if (reply->name == NULL) {
		radlog(L_ERR, "rlm_eap_leap: out of memory");
		eapleap_free(&reply);
		return NULL;
	}

	/*
	 *	Copy the name over, and ensure it's NUL terminated.
	 */
	memcpy(reply->name, user_name->strvalue, user_name->length);
	reply->name[user_name->length] = '\0';
	reply->name_len = user_name->length;

	return reply;
}

/*
 * compose the LEAP reply packet in the EAP reply typedata
 */
int eapleap_compose(EAP_DS *eap_ds, LEAP_PACKET *reply)
{
	leap_packet_t *data;

	/*
	 *  We need the name and the challenge.
	 */
	switch (reply->code) {
	case PW_EAP_REQUEST:
	case PW_EAP_RESPONSE:
		eap_ds->request->type.type = PW_EAP_LEAP;
		eap_ds->request->type.length = reply->length;

		eap_ds->request->type.data = malloc(reply->length);
		if (eap_ds->request->type.data == NULL) {
			radlog(L_ERR, "rlm_eap_leap: out of memory");
			return 0;
		}
		data = (leap_packet_t *) eap_ds->request->type.data;
		data->version = 0x01;
		data->unused = 0;
		data->count = reply->count;

		/*
		 *	N bytes of the challenge, followed by the user name.
		 */
		memcpy(&data->challenge[0], reply->challenge, reply->count);
		memcpy(&data->challenge[reply->count],
		       reply->name, reply->name_len);
		break;

		/*
		 *	EAP-Success packets don't contain any data
		 *	other than the header.
		 */
	case PW_EAP_SUCCESS:
		eap_ds->request->type.length = 0;
		break;

	default:
		radlog(L_ERR, "rlm_eap_leap: Internal sanity check failed");
		return 0;
		break;
	}

	/*
	 *	Set the EAP code.
	 */
	eap_ds->request->code = reply->code;

	return 1;
}