aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/plugins/eap_aka/eap_aka_server.c
blob: 9f6e71ad7833f6a1b57e8ee18714e1d77c6ead45 (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
/*
 * Copyright (C) 2006-2009 Martin Willi
 * 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_aka_server.h"

#include <daemon.h>
#include <library.h>

#include <simaka_message.h>
#include <simaka_crypto.h>

typedef struct private_eap_aka_server_t private_eap_aka_server_t;

/**
 * Private data of an eap_aka_server_t object.
 */
struct private_eap_aka_server_t {

	/**
	 * Public authenticator_t interface.
	 */
	eap_aka_server_t public;

	/**
	 * EAP-AKA crypto helper
	 */
	simaka_crypto_t *crypto;

	/**
	 * ID of the peer
	 */
	identification_t *peer;

	/**
	 * EAP identifier value
	 */
	u_int8_t identifier;

	/**
	 * MSK
	 */
	chunk_t msk;

	/**
	 * Expected Result XRES
	 */
	chunk_t xres;

	/**
	 * Random value RAND
	 */
	chunk_t rand;

	/**
	 * EAP-AKA message we have initiated
	 */
	simaka_subtype_t pending;

	/**
	 * Did the client send a synchronize request?
	 */
	bool synchronized;
};

/**
 * Check if an unknown attribute is skippable
 */
static bool attribute_skippable(simaka_attribute_t attribute)
{
	if (attribute >= 0 && attribute <= 127)
	{
		DBG1(DBG_IKE, "ignoring skippable attribute %N",
			 simaka_attribute_names, attribute);
		return TRUE;
	}
	return FALSE;
}

/**
 * Implementation of eap_method_t.initiate
 */
static status_t initiate(private_eap_aka_server_t *this, eap_payload_t **out)
{
	simaka_message_t *message;
	enumerator_t *enumerator;
	sim_provider_t *provider;
	char rand[AKA_RAND_LEN], xres[AKA_RES_LEN];
	char ck[AKA_CK_LEN], ik[AKA_IK_LEN], autn[AKA_AUTN_LEN];
	chunk_t data, mk;
	bool found = FALSE;

	enumerator = charon->sim->create_provider_enumerator(charon->sim);
	while (enumerator->enumerate(enumerator, &provider))
	{
		if (provider->get_quintuplet(provider, this->peer,
									 rand, xres, ck, ik, autn))
		{
			found = TRUE;
			break;
		}
	}
	enumerator->destroy(enumerator);
	if (!found)
	{
		DBG1(DBG_IKE, "no AKA provider found with quintuplets for '%Y'",
			 this->peer);
		return FAILED;
	}

	data = chunk_cata("cc", chunk_create(ik, AKA_IK_LEN),
					  chunk_create(ck, AKA_CK_LEN));
	free(this->msk.ptr);
	this->msk = this->crypto->derive_keys_full(this->crypto, this->peer,
											   data, &mk);
	free(mk.ptr);
	this->rand = chunk_clone(chunk_create(rand, AKA_RAND_LEN));
	this->xres = chunk_clone(chunk_create(xres, AKA_RES_LEN));

	message = simaka_message_create(TRUE, this->identifier++, EAP_AKA,
									AKA_CHALLENGE, this->crypto);
	message->add_attribute(message, AT_RAND, this->rand);
	message->add_attribute(message, AT_AUTN, chunk_create(autn, AKA_AUTN_LEN));
	*out = message->generate(message, chunk_empty);
	message->destroy(message);

	this->pending = AKA_CHALLENGE;
	return NEED_MORE;
}

/**
 * Process EAP-AKA/Response/Challenge message
 */
static status_t process_challenge(private_eap_aka_server_t *this,
								  simaka_message_t *in)
{
	enumerator_t *enumerator;
	simaka_attribute_t type;
	chunk_t data, res = chunk_empty;

	if (this->pending != AKA_CHALLENGE)
	{
		DBG1(DBG_IKE, "received %N, but not expected",
			 simaka_subtype_names, AKA_CHALLENGE);
		return FAILED;
	}
	enumerator = in->create_attribute_enumerator(in);
	while (enumerator->enumerate(enumerator, &type, &data))
	{
		switch (type)
		{
			case AT_RES:
				res = data;
				break;
			default:
				if (!attribute_skippable(type))
				{
					enumerator->destroy(enumerator);
					DBG1(DBG_IKE, "found non skippable attribute %N",
						 simaka_attribute_names, type);
					return FAILED;
				}
				break;
		}
	}
	enumerator->destroy(enumerator);

	/* verify MAC of EAP message, AT_MAC */
	if (!in->verify(in, chunk_empty))
	{
		DBG1(DBG_IKE, "AT_MAC verification failed");
		return FAILED;
	}
	/* compare received RES against stored XRES */
	if (!chunk_equals(res, this->xres))
	{
		DBG1(DBG_IKE, "received RES does not match XRES");
		return FAILED;
	}
	return SUCCESS;
}

/**
 * Process EAP-AKA/Response/SynchronizationFailure message
 */
static status_t process_synchronize(private_eap_aka_server_t *this,
									simaka_message_t *in, eap_payload_t **out)
{
	sim_provider_t *provider;
	enumerator_t *enumerator;
	simaka_attribute_t type;
	chunk_t data, auts = chunk_empty;
	bool found = FALSE;

	if (this->synchronized)
	{
		DBG1(DBG_IKE, "received %N, but peer did already resynchronize",
			 simaka_subtype_names, AKA_SYNCHRONIZATION_FAILURE);
		return FAILED;
	}

	DBG1(DBG_IKE, "received synchronization request, retrying...");

	enumerator = in->create_attribute_enumerator(in);
	while (enumerator->enumerate(enumerator, &type, &data))
	{
		switch (type)
		{
			case AT_AUTS:
				auts = data;
				break;
			default:
				if (!attribute_skippable(type))
				{
					enumerator->destroy(enumerator);
					DBG1(DBG_IKE, "found non skippable attribute %N",
						 simaka_attribute_names, type);
					return FAILED;
				}
				break;
		}
	}
	enumerator->destroy(enumerator);

	if (!auts.len)
	{
		DBG1(DBG_IKE, "synchronization request didn't contain usable AUTS");
		return FAILED;
	}

	enumerator = charon->sim->create_provider_enumerator(charon->sim);
	while (enumerator->enumerate(enumerator, &provider))
	{
		if (provider->resync(provider, this->peer, this->rand.ptr, auts.ptr))
		{
			found = TRUE;
			break;
		}
	}
	enumerator->destroy(enumerator);

	if (!found)
	{
		DBG1(DBG_IKE, "no AKA provider found supporting "
			 "resynchronization for '%Y'", this->peer);
		return FAILED;
	}
	this->synchronized = TRUE;
	return initiate(this, out);
}

/**
 * Process EAP-AKA/Response/ClientErrorCode message
 */
static status_t process_client_error(private_eap_aka_server_t *this,
									 simaka_message_t *in)
{
	enumerator_t *enumerator;
	simaka_attribute_t type;
	chunk_t data;

	enumerator = in->create_attribute_enumerator(in);
	while (enumerator->enumerate(enumerator, &type, &data))
	{
		if (type == AT_CLIENT_ERROR_CODE)
		{
			u_int16_t code;

			memcpy(&code, data.ptr, sizeof(code));
			DBG1(DBG_IKE, "received EAP-AKA client error '%N'",
				 simaka_client_error_names, ntohs(code));
		}
		else if (!simaka_attribute_skippable(type))
		{
			break;
		}
	}
	enumerator->destroy(enumerator);
	return FAILED;
}

/**
 * Process EAP-AKA/Response/AuthenticationReject message
 */
static status_t process_authentication_reject(private_eap_aka_server_t *this,
											  simaka_message_t *in)
{
	DBG1(DBG_IKE, "received %N, authentication failed",
		 simaka_subtype_names, in->get_subtype(in));
	return FAILED;
}

/**
 * Implementation of eap_method_t.process
 */
static status_t process(private_eap_aka_server_t *this,
						eap_payload_t *in, eap_payload_t **out)
{
	simaka_message_t *message;
	status_t status;

	message = simaka_message_create_from_payload(in, this->crypto);
	if (!message)
	{
		return FAILED;
	}
	if (!message->parse(message))
	{
		message->destroy(message);
		return FAILED;
	}
	switch (message->get_subtype(message))
	{
		case AKA_CHALLENGE:
			status = process_challenge(this, message);
			break;
		case AKA_SYNCHRONIZATION_FAILURE:
			status = process_synchronize(this, message, out);
			break;
		case AKA_CLIENT_ERROR:
			status = process_client_error(this, message);
			break;
		case AKA_AUTHENTICATION_REJECT:
			status = process_authentication_reject(this, message);
			break;
		default:
			DBG1(DBG_IKE, "unable to process EAP-AKA subtype %N",
				 simaka_subtype_names, message->get_subtype(message));
			status = FAILED;
			break;
	}
	message->destroy(message);
	return status;
}

/**
 * Implementation of eap_method_t.get_type.
 */
static eap_type_t get_type(private_eap_aka_server_t *this, u_int32_t *vendor)
{
	*vendor = 0;
	return EAP_AKA;
}

/**
 * Implementation of eap_method_t.get_msk.
 */
static status_t get_msk(private_eap_aka_server_t *this, chunk_t *msk)
{
	if (this->msk.ptr)
	{
		*msk = this->msk;
		return SUCCESS;
	}
	return FAILED;
}

/**
 * Implementation of eap_method_t.is_mutual.
 */
static bool is_mutual(private_eap_aka_server_t *this)
{
	return TRUE;
}

/**
 * Implementation of eap_method_t.destroy.
 */
static void destroy(private_eap_aka_server_t *this)
{
	this->crypto->destroy(this->crypto);
	this->peer->destroy(this->peer);
	free(this->msk.ptr);
	free(this->xres.ptr);
	free(this->rand.ptr);
	free(this);
}

/*
 * Described in header.
 */
eap_aka_server_t *eap_aka_server_create(identification_t *server,
										identification_t *peer)
{
	private_eap_aka_server_t *this = malloc_thing(private_eap_aka_server_t);

	this->public.interface.initiate = (status_t(*)(eap_method_t*,eap_payload_t**))initiate;
	this->public.interface.process = (status_t(*)(eap_method_t*,eap_payload_t*,eap_payload_t**))process;
	this->public.interface.get_type = (eap_type_t(*)(eap_method_t*,u_int32_t*))get_type;
	this->public.interface.is_mutual = (bool(*)(eap_method_t*))is_mutual;
	this->public.interface.get_msk = (status_t(*)(eap_method_t*,chunk_t*))get_msk;
	this->public.interface.destroy = (void(*)(eap_method_t*))destroy;

	this->crypto = simaka_crypto_create();
	if (!this->crypto)
	{
		free(this);
		return NULL;
	}
	this->peer = peer->clone(peer);
	this->msk = chunk_empty;
	this->xres = chunk_empty;
	this->rand = chunk_empty;
	this->pending = 0;
	this->synchronized = FALSE;
	/* generate a non-zero identifier */
	do {
		this->identifier = random();
	} while (!this->identifier);

	return &this->public;
}