1 /*
2  * EAP peer method: EAP-AKA (RFC 4187) and EAP-AKA' (RFC 5448)
3  * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "pcsc_funcs.h"
13 #include "crypto/crypto.h"
14 #include "crypto/sha1.h"
15 #include "crypto/sha256.h"
16 #include "crypto/milenage.h"
17 #include "eap_common/eap_sim_common.h"
18 #include "eap_config.h"
19 #include "eap_i.h"
20 
21 
22 struct eap_aka_data {
23 	u8 ik[EAP_AKA_IK_LEN], ck[EAP_AKA_CK_LEN], res[EAP_AKA_RES_MAX_LEN];
24 	size_t res_len;
25 	u8 nonce_s[EAP_SIM_NONCE_S_LEN];
26 	u8 mk[EAP_SIM_MK_LEN];
27 	u8 k_aut[EAP_AKA_PRIME_K_AUT_LEN];
28 	u8 k_encr[EAP_SIM_K_ENCR_LEN];
29 	u8 k_re[EAP_AKA_PRIME_K_RE_LEN]; /* EAP-AKA' only */
30 	u8 msk[EAP_SIM_KEYING_DATA_LEN];
31 	u8 emsk[EAP_EMSK_LEN];
32 	u8 rand[EAP_AKA_RAND_LEN], autn[EAP_AKA_AUTN_LEN];
33 	u8 auts[EAP_AKA_AUTS_LEN];
34 
35 	int num_id_req, num_notification;
36 	u8 *pseudonym;
37 	size_t pseudonym_len;
38 	u8 *reauth_id;
39 	size_t reauth_id_len;
40 	int reauth;
41 	unsigned int counter, counter_too_small;
42 	u8 *last_eap_identity;
43 	size_t last_eap_identity_len;
44 	enum {
45 		CONTINUE, RESULT_SUCCESS, SUCCESS, FAILURE
46 	} state;
47 
48 	struct wpabuf *id_msgs;
49 	int prev_id;
50 	int result_ind, use_result_ind;
51 	int use_pseudonym;
52 	u8 eap_method;
53 	u8 *network_name;
54 	size_t network_name_len;
55 	u16 kdf;
56 	int kdf_negotiation;
57 	u16 last_kdf_attrs[EAP_AKA_PRIME_KDF_MAX];
58 	size_t last_kdf_count;
59 	int error_code;
60 	int anonymous_flag;
61 };
62 
63 
64 #ifndef CONFIG_NO_STDOUT_DEBUG
eap_aka_state_txt(int state)65 static const char * eap_aka_state_txt(int state)
66 {
67 	switch (state) {
68 	case CONTINUE:
69 		return "CONTINUE";
70 	case RESULT_SUCCESS:
71 		return "RESULT_SUCCESS";
72 	case SUCCESS:
73 		return "SUCCESS";
74 	case FAILURE:
75 		return "FAILURE";
76 	default:
77 		return "?";
78 	}
79 }
80 #endif /* CONFIG_NO_STDOUT_DEBUG */
81 
82 
eap_aka_state(struct eap_aka_data * data,int state)83 static void eap_aka_state(struct eap_aka_data *data, int state)
84 {
85 	wpa_printf(MSG_DEBUG, "EAP-AKA: %s -> %s",
86 		   eap_aka_state_txt(data->state),
87 		   eap_aka_state_txt(state));
88 	data->state = state;
89 }
90 
91 
eap_aka_init(struct eap_sm * sm)92 static void * eap_aka_init(struct eap_sm *sm)
93 {
94 	struct eap_aka_data *data;
95 	const char *phase1 = eap_get_config_phase1(sm);
96 	struct eap_peer_config *config = eap_get_config(sm);
97 	static const char *anonymous_id_prefix = "anonymous@";
98 
99 	data = os_zalloc(sizeof(*data));
100 	if (data == NULL)
101 		return NULL;
102 
103 	data->eap_method = EAP_TYPE_AKA;
104 
105 	/* Zero is a valid error code, so we need to initialize */
106 	data->error_code = NO_EAP_METHOD_ERROR;
107 
108 	eap_aka_state(data, CONTINUE);
109 	data->prev_id = -1;
110 
111 	data->result_ind = phase1 && os_strstr(phase1, "result_ind=1") != NULL;
112 	data->anonymous_flag = 0;
113 
114 	data->use_pseudonym = !sm->init_phase2;
115 	if (config && config->anonymous_identity && data->use_pseudonym) {
116 		data->pseudonym = os_malloc(config->anonymous_identity_len);
117 		if (data->pseudonym) {
118 			os_memcpy(data->pseudonym, config->anonymous_identity,
119 				  config->anonymous_identity_len);
120 			data->pseudonym_len = config->anonymous_identity_len;
121 			if (data->pseudonym_len > os_strlen(anonymous_id_prefix) &&
122 					!os_memcmp(data->pseudonym, anonymous_id_prefix,
123 					os_strlen(anonymous_id_prefix))) {
124 				data->anonymous_flag = 1;
125 				wpa_printf(MSG_DEBUG,
126 					   "EAP-AKA: Setting anonymous@realm flag");
127 			}
128 		}
129 	}
130 
131 	return data;
132 }
133 
134 
135 #ifdef EAP_AKA_PRIME
eap_aka_prime_init(struct eap_sm * sm)136 static void * eap_aka_prime_init(struct eap_sm *sm)
137 {
138 	struct eap_aka_data *data = eap_aka_init(sm);
139 	if (data == NULL)
140 		return NULL;
141 	data->eap_method = EAP_TYPE_AKA_PRIME;
142 	return data;
143 }
144 #endif /* EAP_AKA_PRIME */
145 
146 
eap_aka_clear_keys(struct eap_aka_data * data,int reauth)147 static void eap_aka_clear_keys(struct eap_aka_data *data, int reauth)
148 {
149 	if (!reauth) {
150 		os_memset(data->mk, 0, EAP_SIM_MK_LEN);
151 		os_memset(data->k_aut, 0, EAP_AKA_PRIME_K_AUT_LEN);
152 		os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN);
153 		os_memset(data->k_re, 0, EAP_AKA_PRIME_K_RE_LEN);
154 	}
155 	os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN);
156 	os_memset(data->emsk, 0, EAP_EMSK_LEN);
157 	os_memset(data->autn, 0, EAP_AKA_AUTN_LEN);
158 	os_memset(data->auts, 0, EAP_AKA_AUTS_LEN);
159 }
160 
161 
eap_aka_deinit(struct eap_sm * sm,void * priv)162 static void eap_aka_deinit(struct eap_sm *sm, void *priv)
163 {
164 	struct eap_aka_data *data = priv;
165 	if (data) {
166 		os_free(data->pseudonym);
167 		os_free(data->reauth_id);
168 		os_free(data->last_eap_identity);
169 		wpabuf_free(data->id_msgs);
170 		os_free(data->network_name);
171 		eap_aka_clear_keys(data, 0);
172 		os_free(data);
173 	}
174 }
175 
176 
eap_aka_ext_sim_req(struct eap_sm * sm,struct eap_aka_data * data)177 static int eap_aka_ext_sim_req(struct eap_sm *sm, struct eap_aka_data *data)
178 {
179 	char req[200], *pos, *end;
180 
181 	wpa_printf(MSG_DEBUG, "EAP-AKA: Use external USIM processing");
182 	pos = req;
183 	end = pos + sizeof(req);
184 	pos += os_snprintf(pos, end - pos, "UMTS-AUTH");
185 	pos += os_snprintf(pos, end - pos, ":");
186 	pos += wpa_snprintf_hex(pos, end - pos, data->rand, EAP_AKA_RAND_LEN);
187 	pos += os_snprintf(pos, end - pos, ":");
188 	wpa_snprintf_hex(pos, end - pos, data->autn, EAP_AKA_AUTN_LEN);
189 
190 	eap_sm_request_sim(sm, req);
191 	return 1;
192 }
193 
194 
eap_aka_ext_sim_result(struct eap_sm * sm,struct eap_aka_data * data,struct eap_peer_config * conf)195 static int eap_aka_ext_sim_result(struct eap_sm *sm, struct eap_aka_data *data,
196 				  struct eap_peer_config *conf)
197 {
198 	char *resp, *pos;
199 
200 	wpa_printf(MSG_DEBUG,
201 		   "EAP-AKA: Use result from external USIM processing");
202 
203 	resp = conf->external_sim_resp;
204 	conf->external_sim_resp = NULL;
205 
206 	if (os_strncmp(resp, "UMTS-AUTS:", 10) == 0) {
207 		pos = resp + 10;
208 		if (hexstr2bin(pos, data->auts, EAP_AKA_AUTS_LEN) < 0)
209 			goto invalid;
210 		wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: AUTS", data->auts,
211 				EAP_AKA_AUTS_LEN);
212 		os_free(resp);
213 		return -2;
214 	}
215 
216 	if (os_strncmp(resp, "UMTS-AUTH:", 10) != 0) {
217 		wpa_printf(MSG_DEBUG, "EAP-AKA: Unrecognized external USIM processing response");
218 		os_free(resp);
219 		return -1;
220 	}
221 
222 	pos = resp + 10;
223 	wpa_hexdump(MSG_DEBUG, "EAP-AKA: RAND", data->rand, EAP_AKA_RAND_LEN);
224 
225 	if (hexstr2bin(pos, data->ik, EAP_AKA_IK_LEN) < 0)
226 		goto invalid;
227 	wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: IK", data->ik, EAP_AKA_IK_LEN);
228 	pos += EAP_AKA_IK_LEN * 2;
229 	if (*pos != ':')
230 		goto invalid;
231 	pos++;
232 
233 	if (hexstr2bin(pos, data->ck, EAP_AKA_CK_LEN) < 0)
234 		goto invalid;
235 	wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: CK", data->ck, EAP_AKA_CK_LEN);
236 	pos += EAP_AKA_CK_LEN * 2;
237 	if (*pos != ':')
238 		goto invalid;
239 	pos++;
240 
241 	data->res_len = os_strlen(pos) / 2;
242 	if (data->res_len > EAP_AKA_RES_MAX_LEN) {
243 		data->res_len = 0;
244 		goto invalid;
245 	}
246 	if (hexstr2bin(pos, data->res, data->res_len) < 0)
247 		goto invalid;
248 	wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: RES", data->res, data->res_len);
249 
250 	os_free(resp);
251 	return 0;
252 
253 invalid:
254 	wpa_printf(MSG_DEBUG, "EAP-AKA: Invalid external USIM processing UMTS-AUTH response");
255 	os_free(resp);
256 	return -1;
257 }
258 
259 
eap_aka_umts_auth(struct eap_sm * sm,struct eap_aka_data * data)260 static int eap_aka_umts_auth(struct eap_sm *sm, struct eap_aka_data *data)
261 {
262 	struct eap_peer_config *conf;
263 
264 	wpa_printf(MSG_DEBUG, "EAP-AKA: UMTS authentication algorithm");
265 
266 	conf = eap_get_config(sm);
267 	if (conf == NULL)
268 		return -1;
269 
270 	if (sm->external_sim) {
271 		if (conf->external_sim_resp)
272 			return eap_aka_ext_sim_result(sm, data, conf);
273 		else
274 			return eap_aka_ext_sim_req(sm, data);
275 	}
276 
277 	if (conf->pcsc) {
278 		return scard_umts_auth(sm->scard_ctx, data->rand,
279 				       data->autn, data->res, &data->res_len,
280 				       data->ik, data->ck, data->auts);
281 	}
282 
283 #ifdef CONFIG_USIM_SIMULATOR
284 	if (conf->password) {
285 		u8 opc[16], k[16], sqn[6];
286 		const char *pos;
287 		wpa_printf(MSG_DEBUG, "EAP-AKA: Use internal Milenage "
288 			   "implementation for UMTS authentication");
289 		if (conf->password_len < 78) {
290 			wpa_printf(MSG_DEBUG, "EAP-AKA: invalid Milenage "
291 				   "password");
292 			return -1;
293 		}
294 		pos = (const char *) conf->password;
295 		if (hexstr2bin(pos, k, 16))
296 			return -1;
297 		pos += 32;
298 		if (*pos != ':')
299 			return -1;
300 		pos++;
301 
302 		if (hexstr2bin(pos, opc, 16))
303 			return -1;
304 		pos += 32;
305 		if (*pos != ':')
306 			return -1;
307 		pos++;
308 
309 		if (hexstr2bin(pos, sqn, 6))
310 			return -1;
311 
312 		return milenage_check(opc, k, sqn, data->rand, data->autn,
313 				      data->ik, data->ck,
314 				      data->res, &data->res_len, data->auts);
315 	}
316 #endif /* CONFIG_USIM_SIMULATOR */
317 
318 #ifdef CONFIG_USIM_HARDCODED
319 	wpa_printf(MSG_DEBUG, "EAP-AKA: Use hardcoded Kc and SRES values for "
320 		   "testing");
321 
322 	/* These hardcoded Kc and SRES values are used for testing.
323 	 * Could consider making them configurable. */
324 	os_memset(data->res, '2', EAP_AKA_RES_MAX_LEN);
325 	data->res_len = EAP_AKA_RES_MAX_LEN;
326 	os_memset(data->ik, '3', EAP_AKA_IK_LEN);
327 	os_memset(data->ck, '4', EAP_AKA_CK_LEN);
328 	{
329 		u8 autn[EAP_AKA_AUTN_LEN];
330 		os_memset(autn, '1', EAP_AKA_AUTN_LEN);
331 		if (os_memcmp_const(autn, data->autn, EAP_AKA_AUTN_LEN) != 0) {
332 			wpa_printf(MSG_WARNING, "EAP-AKA: AUTN did not match "
333 				   "with expected value");
334 			return -1;
335 		}
336 	}
337 #if 0
338 	{
339 		static int test_resync = 1;
340 		if (test_resync) {
341 			/* Test Resynchronization */
342 			test_resync = 0;
343 			return -2;
344 		}
345 	}
346 #endif
347 	return 0;
348 
349 #else /* CONFIG_USIM_HARDCODED */
350 
351 	wpa_printf(MSG_DEBUG, "EAP-AKA: No UMTS authentication algorithm "
352 		   "enabled");
353 	return -1;
354 
355 #endif /* CONFIG_USIM_HARDCODED */
356 }
357 
358 
359 #define CLEAR_PSEUDONYM	0x01
360 #define CLEAR_REAUTH_ID	0x02
361 #define CLEAR_EAP_ID	0x04
362 
eap_aka_clear_identities(struct eap_sm * sm,struct eap_aka_data * data,int id)363 static void eap_aka_clear_identities(struct eap_sm *sm,
364 				     struct eap_aka_data *data, int id)
365 {
366 	if ((id & CLEAR_PSEUDONYM) && data->pseudonym) {
367 		wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old pseudonym");
368 		os_free(data->pseudonym);
369 		data->pseudonym = NULL;
370 		data->pseudonym_len = 0;
371 		if (data->use_pseudonym)
372 			eap_set_anon_id(sm, NULL, 0);
373 	}
374 	if ((id & CLEAR_REAUTH_ID) && data->reauth_id) {
375 		wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old reauth_id");
376 		os_free(data->reauth_id);
377 		data->reauth_id = NULL;
378 		data->reauth_id_len = 0;
379 	}
380 	if ((id & CLEAR_EAP_ID) && data->last_eap_identity) {
381 		wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old eap_id");
382 		os_free(data->last_eap_identity);
383 		data->last_eap_identity = NULL;
384 		data->last_eap_identity_len = 0;
385 	}
386 }
387 
388 
eap_aka_learn_ids(struct eap_sm * sm,struct eap_aka_data * data,struct eap_sim_attrs * attr)389 static int eap_aka_learn_ids(struct eap_sm *sm, struct eap_aka_data *data,
390 			     struct eap_sim_attrs *attr)
391 {
392 	if (attr->next_pseudonym) {
393 		const u8 *identity = NULL;
394 		size_t identity_len = 0;
395 		const u8 *realm = NULL;
396 		size_t realm_len = 0;
397 
398 		wpa_hexdump_ascii(MSG_DEBUG,
399 				  "EAP-AKA: (encr) AT_NEXT_PSEUDONYM",
400 				  attr->next_pseudonym,
401 				  attr->next_pseudonym_len);
402 		os_free(data->pseudonym);
403 		/* Look for the realm of the permanent identity */
404 		identity = eap_get_config_identity(sm, &identity_len);
405 		if (identity) {
406 			for (realm = identity, realm_len = identity_len;
407 			     realm_len > 0; realm_len--, realm++) {
408 				if (*realm == '@')
409 					break;
410 			}
411 		}
412 		data->pseudonym = os_malloc(attr->next_pseudonym_len +
413 					    realm_len);
414 		if (data->pseudonym == NULL) {
415 			wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for "
416 				   "next pseudonym");
417 			data->pseudonym_len = 0;
418 			return -1;
419 		}
420 		os_memcpy(data->pseudonym, attr->next_pseudonym,
421 			  attr->next_pseudonym_len);
422 		if (realm_len) {
423 			os_memcpy(data->pseudonym + attr->next_pseudonym_len,
424 				  realm, realm_len);
425 		}
426 		data->pseudonym_len = attr->next_pseudonym_len + realm_len;
427 		if (data->use_pseudonym)
428 			eap_set_anon_id(sm, data->pseudonym,
429 					data->pseudonym_len);
430 		data->anonymous_flag = 0;
431 	}
432 
433 	if (attr->next_reauth_id) {
434 		os_free(data->reauth_id);
435 		data->reauth_id = os_memdup(attr->next_reauth_id,
436 					    attr->next_reauth_id_len);
437 		if (data->reauth_id == NULL) {
438 			wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for "
439 				   "next reauth_id");
440 			data->reauth_id_len = 0;
441 			return -1;
442 		}
443 		data->reauth_id_len = attr->next_reauth_id_len;
444 		wpa_hexdump_ascii(MSG_DEBUG,
445 				  "EAP-AKA: (encr) AT_NEXT_REAUTH_ID",
446 				  data->reauth_id,
447 				  data->reauth_id_len);
448 	}
449 
450 	return 0;
451 }
452 
453 
eap_aka_add_id_msg(struct eap_aka_data * data,const struct wpabuf * msg)454 static int eap_aka_add_id_msg(struct eap_aka_data *data,
455 			      const struct wpabuf *msg)
456 {
457 	if (msg == NULL)
458 		return -1;
459 
460 	if (data->id_msgs == NULL) {
461 		data->id_msgs = wpabuf_dup(msg);
462 		return data->id_msgs == NULL ? -1 : 0;
463 	}
464 
465 	if (wpabuf_resize(&data->id_msgs, wpabuf_len(msg)) < 0)
466 		return -1;
467 	wpabuf_put_buf(data->id_msgs, msg);
468 
469 	return 0;
470 }
471 
472 
eap_aka_add_checkcode(struct eap_aka_data * data,struct eap_sim_msg * msg)473 static void eap_aka_add_checkcode(struct eap_aka_data *data,
474 				  struct eap_sim_msg *msg)
475 {
476 	const u8 *addr;
477 	size_t len;
478 	u8 hash[SHA256_MAC_LEN];
479 
480 	wpa_printf(MSG_DEBUG, "   AT_CHECKCODE");
481 
482 	if (data->id_msgs == NULL) {
483 		/*
484 		 * No EAP-AKA/Identity packets were exchanged - send empty
485 		 * checkcode.
486 		 */
487 		eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, NULL, 0);
488 		return;
489 	}
490 
491 	/* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */
492 	addr = wpabuf_head(data->id_msgs);
493 	len = wpabuf_len(data->id_msgs);
494 	wpa_hexdump(MSG_MSGDUMP, "EAP-AKA: AT_CHECKCODE data", addr, len);
495 #ifdef EAP_AKA_PRIME
496 	if (data->eap_method == EAP_TYPE_AKA_PRIME)
497 		sha256_vector(1, &addr, &len, hash);
498 	else
499 #endif /* EAP_AKA_PRIME */
500 		sha1_vector(1, &addr, &len, hash);
501 
502 	eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, hash,
503 			data->eap_method == EAP_TYPE_AKA_PRIME ?
504 			EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN);
505 }
506 
507 
eap_aka_verify_checkcode(struct eap_aka_data * data,const u8 * checkcode,size_t checkcode_len)508 static int eap_aka_verify_checkcode(struct eap_aka_data *data,
509 				    const u8 *checkcode, size_t checkcode_len)
510 {
511 	const u8 *addr;
512 	size_t len;
513 	u8 hash[SHA256_MAC_LEN];
514 	size_t hash_len;
515 
516 	if (checkcode == NULL)
517 		return -1;
518 
519 	if (data->id_msgs == NULL) {
520 		if (checkcode_len != 0) {
521 			wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server "
522 				   "indicates that AKA/Identity messages were "
523 				   "used, but they were not");
524 			return -1;
525 		}
526 		return 0;
527 	}
528 
529 	hash_len = data->eap_method == EAP_TYPE_AKA_PRIME ?
530 		EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN;
531 
532 	if (checkcode_len != hash_len) {
533 		wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server "
534 			   "indicates that AKA/Identity message were not "
535 			   "used, but they were");
536 		return -1;
537 	}
538 
539 	/* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */
540 	addr = wpabuf_head(data->id_msgs);
541 	len = wpabuf_len(data->id_msgs);
542 #ifdef EAP_AKA_PRIME
543 	if (data->eap_method == EAP_TYPE_AKA_PRIME)
544 		sha256_vector(1, &addr, &len, hash);
545 	else
546 #endif /* EAP_AKA_PRIME */
547 		sha1_vector(1, &addr, &len, hash);
548 
549 	if (os_memcmp_const(hash, checkcode, hash_len) != 0) {
550 		wpa_printf(MSG_DEBUG, "EAP-AKA: Mismatch in AT_CHECKCODE");
551 		return -1;
552 	}
553 
554 	return 0;
555 }
556 
557 
eap_aka_client_error(struct eap_aka_data * data,u8 id,int err)558 static struct wpabuf * eap_aka_client_error(struct eap_aka_data *data, u8 id,
559 					    int err)
560 {
561 	struct eap_sim_msg *msg;
562 
563 	eap_aka_state(data, FAILURE);
564 	data->num_id_req = 0;
565 	data->num_notification = 0;
566 
567 	wpa_printf(MSG_DEBUG, "EAP-AKA: Send Client-Error (error code %d)",
568 		   err);
569 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
570 			       EAP_AKA_SUBTYPE_CLIENT_ERROR);
571 	eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
572 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
573 }
574 
575 
eap_aka_authentication_reject(struct eap_aka_data * data,u8 id)576 static struct wpabuf * eap_aka_authentication_reject(struct eap_aka_data *data,
577 						     u8 id)
578 {
579 	struct eap_sim_msg *msg;
580 
581 	eap_aka_state(data, FAILURE);
582 	data->num_id_req = 0;
583 	data->num_notification = 0;
584 
585 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Authentication-Reject "
586 		   "(id=%d)", id);
587 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
588 			       EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT);
589 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
590 }
591 
592 
eap_aka_synchronization_failure(struct eap_aka_data * data,u8 id,struct eap_sim_attrs * attr)593 static struct wpabuf * eap_aka_synchronization_failure(
594 	struct eap_aka_data *data, u8 id, struct eap_sim_attrs *attr)
595 {
596 	struct eap_sim_msg *msg;
597 
598 	data->num_id_req = 0;
599 	data->num_notification = 0;
600 
601 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Synchronization-Failure "
602 		   "(id=%d)", id);
603 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
604 			       EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE);
605 	wpa_printf(MSG_DEBUG, "   AT_AUTS");
606 	eap_sim_msg_add_full(msg, EAP_SIM_AT_AUTS, data->auts,
607 			     EAP_AKA_AUTS_LEN);
608 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
609 		size_t i;
610 
611 		for (i = 0; i < attr->kdf_count; i++) {
612 			wpa_printf(MSG_DEBUG, "   AT_KDF");
613 			eap_sim_msg_add(msg, EAP_SIM_AT_KDF, attr->kdf[i],
614 					NULL, 0);
615 		}
616 	}
617 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
618 }
619 
620 
eap_aka_response_identity(struct eap_sm * sm,struct eap_aka_data * data,u8 id,enum eap_sim_id_req id_req)621 static struct wpabuf * eap_aka_response_identity(struct eap_sm *sm,
622 						 struct eap_aka_data *data,
623 						 u8 id,
624 						 enum eap_sim_id_req id_req)
625 {
626 	const u8 *identity = NULL;
627 	size_t identity_len = 0;
628 	struct eap_sim_msg *msg;
629 
630 	data->reauth = 0;
631 	if (id_req == ANY_ID && data->reauth_id) {
632 		identity = data->reauth_id;
633 		identity_len = data->reauth_id_len;
634 		data->reauth = 1;
635 	} else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
636 		   data->pseudonym && !data->anonymous_flag) {
637 		identity = data->pseudonym;
638 		identity_len = data->pseudonym_len;
639 		eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID);
640 	} else if (id_req != NO_ID_REQ) {
641 		identity = eap_get_config_identity(sm, &identity_len);
642 		if (identity) {
643 			eap_aka_clear_identities(sm, data, CLEAR_PSEUDONYM |
644 						 CLEAR_REAUTH_ID);
645 		}
646 	}
647 	if (id_req != NO_ID_REQ)
648 		eap_aka_clear_identities(sm, data, CLEAR_EAP_ID);
649 
650 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Identity (id=%d)", id);
651 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
652 			       EAP_AKA_SUBTYPE_IDENTITY);
653 
654 	if (identity) {
655 		wpa_hexdump_ascii(MSG_DEBUG, "   AT_IDENTITY",
656 				  identity, identity_len);
657 		eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
658 				identity, identity_len);
659 	}
660 
661 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
662 }
663 
664 
eap_aka_response_challenge(struct eap_aka_data * data,u8 id)665 static struct wpabuf * eap_aka_response_challenge(struct eap_aka_data *data,
666 						  u8 id)
667 {
668 	struct eap_sim_msg *msg;
669 
670 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d)", id);
671 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
672 			       EAP_AKA_SUBTYPE_CHALLENGE);
673 	wpa_printf(MSG_DEBUG, "   AT_RES");
674 	eap_sim_msg_add(msg, EAP_SIM_AT_RES, data->res_len * 8,
675 			data->res, data->res_len);
676 	eap_aka_add_checkcode(data, msg);
677 	if (data->use_result_ind) {
678 		wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
679 		eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
680 	}
681 	wpa_printf(MSG_DEBUG, "   AT_MAC");
682 	eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
683 	return eap_sim_msg_finish(msg, data->eap_method, data->k_aut, (u8 *) "",
684 				  0);
685 }
686 
687 
eap_aka_response_reauth(struct eap_aka_data * data,u8 id,int counter_too_small,const u8 * nonce_s)688 static struct wpabuf * eap_aka_response_reauth(struct eap_aka_data *data,
689 					       u8 id, int counter_too_small,
690 					       const u8 *nonce_s)
691 {
692 	struct eap_sim_msg *msg;
693 	unsigned int counter;
694 
695 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Reauthentication (id=%d)",
696 		   id);
697 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
698 			       EAP_AKA_SUBTYPE_REAUTHENTICATION);
699 	wpa_printf(MSG_DEBUG, "   AT_IV");
700 	wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
701 	eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
702 
703 	if (counter_too_small) {
704 		wpa_printf(MSG_DEBUG, "   *AT_COUNTER_TOO_SMALL");
705 		eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
706 		counter = data->counter_too_small;
707 	} else
708 		counter = data->counter;
709 
710 	wpa_printf(MSG_DEBUG, "   *AT_COUNTER %d", counter);
711 	eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
712 
713 	if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
714 		wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
715 			   "AT_ENCR_DATA");
716 		eap_sim_msg_free(msg);
717 		return NULL;
718 	}
719 	eap_aka_add_checkcode(data, msg);
720 	if (data->use_result_ind) {
721 		wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
722 		eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
723 	}
724 	wpa_printf(MSG_DEBUG, "   AT_MAC");
725 	eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
726 	return eap_sim_msg_finish(msg, data->eap_method, data->k_aut, nonce_s,
727 				  EAP_SIM_NONCE_S_LEN);
728 }
729 
730 
eap_aka_response_notification(struct eap_aka_data * data,u8 id,u16 notification)731 static struct wpabuf * eap_aka_response_notification(struct eap_aka_data *data,
732 						     u8 id, u16 notification)
733 {
734 	struct eap_sim_msg *msg;
735 	u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
736 
737 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Notification (id=%d)", id);
738 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
739 			       EAP_AKA_SUBTYPE_NOTIFICATION);
740 	if (k_aut && data->reauth) {
741 		wpa_printf(MSG_DEBUG, "   AT_IV");
742 		wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
743 		eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
744 					   EAP_SIM_AT_ENCR_DATA);
745 		wpa_printf(MSG_DEBUG, "   *AT_COUNTER %d", data->counter);
746 		eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
747 				NULL, 0);
748 		if (eap_sim_msg_add_encr_end(msg, data->k_encr,
749 					     EAP_SIM_AT_PADDING)) {
750 			wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
751 				   "AT_ENCR_DATA");
752 			eap_sim_msg_free(msg);
753 			return NULL;
754 		}
755 	}
756 	if (k_aut) {
757 		wpa_printf(MSG_DEBUG, "   AT_MAC");
758 		eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
759 	}
760 	return eap_sim_msg_finish(msg, data->eap_method, k_aut, (u8 *) "", 0);
761 }
762 
763 
eap_aka_process_identity(struct eap_sm * sm,struct eap_aka_data * data,u8 id,const struct wpabuf * reqData,struct eap_sim_attrs * attr)764 static struct wpabuf * eap_aka_process_identity(struct eap_sm *sm,
765 						struct eap_aka_data *data,
766 						u8 id,
767 						const struct wpabuf *reqData,
768 						struct eap_sim_attrs *attr)
769 {
770 	int id_error;
771 	struct wpabuf *buf;
772 
773 	wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Identity");
774 
775 	id_error = 0;
776 	switch (attr->id_req) {
777 	case NO_ID_REQ:
778 		break;
779 	case ANY_ID:
780 		if (data->num_id_req > 0)
781 			id_error++;
782 		data->num_id_req++;
783 		break;
784 	case FULLAUTH_ID:
785 		if (data->num_id_req > 1)
786 			id_error++;
787 		data->num_id_req++;
788 		break;
789 	case PERMANENT_ID:
790 		if (data->num_id_req > 2)
791 			id_error++;
792 		data->num_id_req++;
793 		break;
794 	}
795 	if (id_error) {
796 		wpa_printf(MSG_INFO, "EAP-AKA: Too many ID requests "
797 			   "used within one authentication");
798 		return eap_aka_client_error(data, id,
799 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
800 	}
801 
802 	buf = eap_aka_response_identity(sm, data, id, attr->id_req);
803 
804 	if (data->prev_id != id) {
805 		eap_aka_add_id_msg(data, reqData);
806 		eap_aka_add_id_msg(data, buf);
807 		data->prev_id = id;
808 	}
809 
810 	return buf;
811 }
812 
813 
eap_aka_verify_mac(struct eap_aka_data * data,const struct wpabuf * req,const u8 * mac,const u8 * extra,size_t extra_len)814 static int eap_aka_verify_mac(struct eap_aka_data *data,
815 			      const struct wpabuf *req,
816 			      const u8 *mac, const u8 *extra,
817 			      size_t extra_len)
818 {
819 	if (data->eap_method == EAP_TYPE_AKA_PRIME)
820 		return eap_sim_verify_mac_sha256(data->k_aut, req, mac, extra,
821 						 extra_len);
822 	return eap_sim_verify_mac(data->k_aut, req, mac, extra, extra_len);
823 }
824 
825 
826 #ifdef EAP_AKA_PRIME
eap_aka_prime_kdf_select(struct eap_aka_data * data,u8 id,u16 kdf)827 static struct wpabuf * eap_aka_prime_kdf_select(struct eap_aka_data *data,
828 						u8 id, u16 kdf)
829 {
830 	struct eap_sim_msg *msg;
831 
832 	data->kdf_negotiation = 1;
833 	data->kdf = kdf;
834 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d) (KDF "
835 		   "select)", id);
836 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
837 			       EAP_AKA_SUBTYPE_CHALLENGE);
838 	wpa_printf(MSG_DEBUG, "   AT_KDF");
839 	eap_sim_msg_add(msg, EAP_SIM_AT_KDF, kdf, NULL, 0);
840 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
841 }
842 
843 
eap_aka_prime_kdf_neg(struct eap_aka_data * data,u8 id,struct eap_sim_attrs * attr)844 static struct wpabuf * eap_aka_prime_kdf_neg(struct eap_aka_data *data,
845 					     u8 id, struct eap_sim_attrs *attr)
846 {
847 	size_t i;
848 
849 	for (i = 0; i < attr->kdf_count; i++) {
850 		if (attr->kdf[i] == EAP_AKA_PRIME_KDF) {
851 			os_memcpy(data->last_kdf_attrs, attr->kdf,
852 				  sizeof(u16) * attr->kdf_count);
853 			data->last_kdf_count = attr->kdf_count;
854 			return eap_aka_prime_kdf_select(data, id,
855 							EAP_AKA_PRIME_KDF);
856 		}
857 	}
858 
859 	/* No matching KDF found - fail authentication as if AUTN had been
860 	 * incorrect */
861 	return eap_aka_authentication_reject(data, id);
862 }
863 
864 
eap_aka_prime_kdf_valid(struct eap_aka_data * data,struct eap_sim_attrs * attr)865 static int eap_aka_prime_kdf_valid(struct eap_aka_data *data,
866 				   struct eap_sim_attrs *attr)
867 {
868 	size_t i, j;
869 
870 	if (attr->kdf_count == 0)
871 		return 0;
872 
873 	/* The only allowed (and required) duplication of a KDF is the addition
874 	 * of the selected KDF into the beginning of the list. */
875 
876 	if (data->kdf_negotiation) {
877 		/* When the peer receives the new EAP-Request/AKA'-Challenge
878 		 * message, must check only requested change occurred in the
879 		 * list of AT_KDF attributes. If there are any other changes,
880 		 * the peer must behave like the case that AT_MAC had been
881 		 * incorrect and authentication is failed. These are defined in
882 		 * EAP-AKA' specification RFC 5448, Section 3.2. */
883 		if (attr->kdf[0] != data->kdf) {
884 			wpa_printf(MSG_WARNING, "EAP-AKA': The server did not "
885 				   "accept the selected KDF");
886 			return -1;
887 		}
888 
889 		if (attr->kdf_count > EAP_AKA_PRIME_KDF_MAX ||
890 		    attr->kdf_count != data->last_kdf_count + 1) {
891 			wpa_printf(MSG_WARNING,
892 				   "EAP-AKA': The length of KDF attributes is wrong");
893 			return -1;
894 		}
895 
896 		for (i = 1; i < attr->kdf_count; i++) {
897 			if (attr->kdf[i] != data->last_kdf_attrs[i - 1]) {
898 				wpa_printf(MSG_WARNING,
899 					   "EAP-AKA': The KDF attributes except selected KDF are not same as original one");
900 				return -1;
901 			}
902 		}
903 	}
904 
905 	for (i = data->kdf ? 1 : 0; i < attr->kdf_count; i++) {
906 		for (j = i + 1; j < attr->kdf_count; j++) {
907 			if (attr->kdf[i] == attr->kdf[j]) {
908 				wpa_printf(MSG_WARNING, "EAP-AKA': The server "
909 					   "included a duplicated KDF");
910 				return 0;
911 			}
912 		}
913 	}
914 
915 	return 1;
916 }
917 #endif /* EAP_AKA_PRIME */
918 
919 
eap_aka_process_challenge(struct eap_sm * sm,struct eap_aka_data * data,u8 id,const struct wpabuf * reqData,struct eap_sim_attrs * attr)920 static struct wpabuf * eap_aka_process_challenge(struct eap_sm *sm,
921 						 struct eap_aka_data *data,
922 						 u8 id,
923 						 const struct wpabuf *reqData,
924 						 struct eap_sim_attrs *attr)
925 {
926 	const u8 *identity;
927 	size_t identity_len;
928 	int res;
929 	struct eap_sim_attrs eattr;
930 
931 	wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Challenge");
932 
933 	if (attr->checkcode &&
934 	    eap_aka_verify_checkcode(data, attr->checkcode,
935 				     attr->checkcode_len)) {
936 		wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the "
937 			   "message");
938 		return eap_aka_client_error(data, id,
939 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
940 	}
941 
942 #ifdef EAP_AKA_PRIME
943 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
944 		if (!attr->kdf_input || attr->kdf_input_len == 0) {
945 			wpa_printf(MSG_WARNING, "EAP-AKA': Challenge message "
946 				   "did not include non-empty AT_KDF_INPUT");
947 			/* Fail authentication as if AUTN had been incorrect */
948 			return eap_aka_authentication_reject(data, id);
949 		}
950 		os_free(data->network_name);
951 		data->network_name = os_memdup(attr->kdf_input,
952 					       attr->kdf_input_len);
953 		if (data->network_name == NULL) {
954 			wpa_printf(MSG_WARNING, "EAP-AKA': No memory for "
955 				   "storing Network Name");
956 			return eap_aka_authentication_reject(data, id);
957 		}
958 		data->network_name_len = attr->kdf_input_len;
959 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA': Network Name "
960 				  "(AT_KDF_INPUT)",
961 				  data->network_name, data->network_name_len);
962 		/* TODO: check Network Name per 3GPP.33.402 */
963 
964 		res = eap_aka_prime_kdf_valid(data, attr);
965 		if (res == 0)
966 			return eap_aka_authentication_reject(data, id);
967 		else if (res == -1)
968 			return eap_aka_client_error(
969 				data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET);
970 
971 		if (attr->kdf[0] != EAP_AKA_PRIME_KDF)
972 			return eap_aka_prime_kdf_neg(data, id, attr);
973 
974 		data->kdf = EAP_AKA_PRIME_KDF;
975 		wpa_printf(MSG_DEBUG, "EAP-AKA': KDF %d selected", data->kdf);
976 	}
977 
978 	if (data->eap_method == EAP_TYPE_AKA && attr->bidding) {
979 		u16 flags = WPA_GET_BE16(attr->bidding);
980 		if ((flags & EAP_AKA_BIDDING_FLAG_D) &&
981 		    eap_allowed_method(sm, EAP_VENDOR_IETF,
982 				       EAP_TYPE_AKA_PRIME)) {
983 			wpa_printf(MSG_WARNING, "EAP-AKA: Bidding down from "
984 				   "AKA' to AKA detected");
985 			/* Fail authentication as if AUTN had been incorrect */
986 			return eap_aka_authentication_reject(data, id);
987 		}
988 	}
989 #endif /* EAP_AKA_PRIME */
990 
991 	data->reauth = 0;
992 	if (!attr->mac || !attr->rand || !attr->autn) {
993 		wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
994 			   "did not include%s%s%s",
995 			   !attr->mac ? " AT_MAC" : "",
996 			   !attr->rand ? " AT_RAND" : "",
997 			   !attr->autn ? " AT_AUTN" : "");
998 		return eap_aka_client_error(data, id,
999 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1000 	}
1001 	os_memcpy(data->rand, attr->rand, EAP_AKA_RAND_LEN);
1002 	os_memcpy(data->autn, attr->autn, EAP_AKA_AUTN_LEN);
1003 
1004 	res = eap_aka_umts_auth(sm, data);
1005 	if (res == -1) {
1006 		wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication "
1007 			   "failed (AUTN)");
1008 		return eap_aka_authentication_reject(data, id);
1009 	} else if (res == -2) {
1010 		wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication "
1011 			   "failed (AUTN seq# -> AUTS)");
1012 		return eap_aka_synchronization_failure(data, id, attr);
1013 	} else if (res > 0) {
1014 		wpa_printf(MSG_DEBUG, "EAP-AKA: Wait for external USIM processing");
1015 		return NULL;
1016 	} else if (res) {
1017 		wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication failed");
1018 		return eap_aka_client_error(data, id,
1019 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1020 	}
1021 #ifdef EAP_AKA_PRIME
1022 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
1023 		/* Note: AUTN = (SQN ^ AK) || AMF || MAC which gives us the
1024 		 * needed 6-octet SQN ^ AK for CK',IK' derivation */
1025 		u16 amf = WPA_GET_BE16(data->autn + 6);
1026 		if (!(amf & 0x8000)) {
1027 			wpa_printf(MSG_WARNING, "EAP-AKA': AMF separation bit "
1028 				   "not set (AMF=0x%4x)", amf);
1029 			return eap_aka_authentication_reject(data, id);
1030 		}
1031 		eap_aka_prime_derive_ck_ik_prime(data->ck, data->ik,
1032 						 data->autn,
1033 						 data->network_name,
1034 						 data->network_name_len);
1035 	}
1036 #endif /* EAP_AKA_PRIME */
1037 	if (data->last_eap_identity) {
1038 		identity = data->last_eap_identity;
1039 		identity_len = data->last_eap_identity_len;
1040 	} else if (data->pseudonym && !data->anonymous_flag) {
1041 		identity = data->pseudonym;
1042 		identity_len = data->pseudonym_len;
1043 	} else {
1044 		struct eap_peer_config *config;
1045 
1046 		config = eap_get_config(sm);
1047 		if (config && config->imsi_identity) {
1048 			identity = config->imsi_identity;
1049 			identity_len = config->imsi_identity_len;
1050 		} else {
1051 			identity = eap_get_config_identity(sm, &identity_len);
1052 		}
1053 	}
1054 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Selected identity for MK "
1055 			  "derivation", identity, identity_len);
1056 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
1057 		eap_aka_prime_derive_keys(identity, identity_len, data->ik,
1058 					  data->ck, data->k_encr, data->k_aut,
1059 					  data->k_re, data->msk, data->emsk);
1060 	} else {
1061 		eap_aka_derive_mk(identity, identity_len, data->ik, data->ck,
1062 				  data->mk);
1063 		eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut,
1064 				    data->msk, data->emsk);
1065 	}
1066 	if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
1067 		wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
1068 			   "used invalid AT_MAC");
1069 		return eap_aka_client_error(data, id,
1070 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1071 	}
1072 
1073 	/* Old reauthentication identity must not be used anymore. In
1074 	 * other words, if no new identities are received, full
1075 	 * authentication will be used on next reauthentication (using
1076 	 * pseudonym identity or permanent identity). */
1077 	eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1078 
1079 	if (attr->encr_data) {
1080 		u8 *decrypted;
1081 		decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
1082 					       attr->encr_data_len, attr->iv,
1083 					       &eattr, 0);
1084 		if (decrypted == NULL) {
1085 			return eap_aka_client_error(
1086 				data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1087 		}
1088 		eap_aka_learn_ids(sm, data, &eattr);
1089 		os_free(decrypted);
1090 	}
1091 
1092 	if (data->result_ind && attr->result_ind)
1093 		data->use_result_ind = 1;
1094 
1095 	if (data->state != FAILURE) {
1096 		eap_aka_state(data, data->use_result_ind ?
1097 			      RESULT_SUCCESS : SUCCESS);
1098 	}
1099 
1100 	data->num_id_req = 0;
1101 	data->num_notification = 0;
1102 	/* RFC 4187 specifies that counter is initialized to one after
1103 	 * fullauth, but initializing it to zero makes it easier to implement
1104 	 * reauth verification. */
1105 	data->counter = 0;
1106 	return eap_aka_response_challenge(data, id);
1107 }
1108 
1109 
eap_aka_process_notification_reauth(struct eap_aka_data * data,struct eap_sim_attrs * attr)1110 static int eap_aka_process_notification_reauth(struct eap_aka_data *data,
1111 					       struct eap_sim_attrs *attr)
1112 {
1113 	struct eap_sim_attrs eattr;
1114 	u8 *decrypted;
1115 
1116 	if (attr->encr_data == NULL || attr->iv == NULL) {
1117 		wpa_printf(MSG_WARNING, "EAP-AKA: Notification message after "
1118 			   "reauth did not include encrypted data");
1119 		return -1;
1120 	}
1121 
1122 	decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
1123 				       attr->encr_data_len, attr->iv, &eattr,
1124 				       0);
1125 	if (decrypted == NULL) {
1126 		wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
1127 			   "data from notification message");
1128 		return -1;
1129 	}
1130 
1131 	if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
1132 		wpa_printf(MSG_WARNING, "EAP-AKA: Counter in notification "
1133 			   "message does not match with counter in reauth "
1134 			   "message");
1135 		os_free(decrypted);
1136 		return -1;
1137 	}
1138 
1139 	os_free(decrypted);
1140 	return 0;
1141 }
1142 
1143 
eap_aka_process_notification_auth(struct eap_aka_data * data,const struct wpabuf * reqData,struct eap_sim_attrs * attr)1144 static int eap_aka_process_notification_auth(struct eap_aka_data *data,
1145 					     const struct wpabuf *reqData,
1146 					     struct eap_sim_attrs *attr)
1147 {
1148 	if (attr->mac == NULL) {
1149 		wpa_printf(MSG_INFO, "EAP-AKA: no AT_MAC in after_auth "
1150 			   "Notification message");
1151 		return -1;
1152 	}
1153 
1154 	if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
1155 		wpa_printf(MSG_WARNING, "EAP-AKA: Notification message "
1156 			   "used invalid AT_MAC");
1157 		return -1;
1158 	}
1159 
1160 	if (data->reauth &&
1161 	    eap_aka_process_notification_reauth(data, attr)) {
1162 		wpa_printf(MSG_WARNING, "EAP-AKA: Invalid notification "
1163 			   "message after reauth");
1164 		return -1;
1165 	}
1166 
1167 	return 0;
1168 }
1169 
1170 
eap_aka_process_notification(struct eap_sm * sm,struct eap_aka_data * data,u8 id,const struct wpabuf * reqData,struct eap_sim_attrs * attr)1171 static struct wpabuf * eap_aka_process_notification(
1172 	struct eap_sm *sm, struct eap_aka_data *data, u8 id,
1173 	const struct wpabuf *reqData, struct eap_sim_attrs *attr)
1174 {
1175 	wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Notification");
1176 	if (data->num_notification > 0) {
1177 		wpa_printf(MSG_INFO, "EAP-AKA: too many notification "
1178 			   "rounds (only one allowed)");
1179 		return eap_aka_client_error(data, id,
1180 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1181 	}
1182 	data->num_notification++;
1183 	if (attr->notification == -1) {
1184 		wpa_printf(MSG_INFO, "EAP-AKA: no AT_NOTIFICATION in "
1185 			   "Notification message");
1186 		return eap_aka_client_error(data, id,
1187 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1188 	}
1189 
1190 	if ((attr->notification & 0x4000) == 0 &&
1191 	    eap_aka_process_notification_auth(data, reqData, attr)) {
1192 		return eap_aka_client_error(data, id,
1193 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1194 	}
1195 
1196 	eap_sim_report_notification(sm->msg_ctx, attr->notification, 1);
1197 	if (attr->notification >= 0 && attr->notification < 32768) {
1198 		data->error_code = attr->notification;
1199 		eap_aka_state(data, FAILURE);
1200 	} else if (attr->notification == EAP_SIM_SUCCESS &&
1201 		   data->state == RESULT_SUCCESS)
1202 		eap_aka_state(data, SUCCESS);
1203 	return eap_aka_response_notification(data, id, attr->notification);
1204 }
1205 
1206 
eap_aka_process_reauthentication(struct eap_sm * sm,struct eap_aka_data * data,u8 id,const struct wpabuf * reqData,struct eap_sim_attrs * attr)1207 static struct wpabuf * eap_aka_process_reauthentication(
1208 	struct eap_sm *sm, struct eap_aka_data *data, u8 id,
1209 	const struct wpabuf *reqData, struct eap_sim_attrs *attr)
1210 {
1211 	struct eap_sim_attrs eattr;
1212 	u8 *decrypted;
1213 
1214 	wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Reauthentication");
1215 
1216 	if (attr->checkcode &&
1217 	    eap_aka_verify_checkcode(data, attr->checkcode,
1218 				     attr->checkcode_len)) {
1219 		wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the "
1220 			   "message");
1221 		return eap_aka_client_error(data, id,
1222 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1223 	}
1224 
1225 	if (data->reauth_id == NULL) {
1226 		wpa_printf(MSG_WARNING, "EAP-AKA: Server is trying "
1227 			   "reauthentication, but no reauth_id available");
1228 		return eap_aka_client_error(data, id,
1229 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1230 	}
1231 
1232 	data->reauth = 1;
1233 	if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
1234 		wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
1235 			   "did not have valid AT_MAC");
1236 		return eap_aka_client_error(data, id,
1237 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1238 	}
1239 
1240 	if (attr->encr_data == NULL || attr->iv == NULL) {
1241 		wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
1242 			   "message did not include encrypted data");
1243 		return eap_aka_client_error(data, id,
1244 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1245 	}
1246 
1247 	decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
1248 				       attr->encr_data_len, attr->iv, &eattr,
1249 				       0);
1250 	if (decrypted == NULL) {
1251 		wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
1252 			   "data from reauthentication message");
1253 		return eap_aka_client_error(data, id,
1254 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1255 	}
1256 
1257 	if (eattr.nonce_s == NULL || eattr.counter < 0) {
1258 		wpa_printf(MSG_INFO, "EAP-AKA: (encr) No%s%s in reauth packet",
1259 			   !eattr.nonce_s ? " AT_NONCE_S" : "",
1260 			   eattr.counter < 0 ? " AT_COUNTER" : "");
1261 		os_free(decrypted);
1262 		return eap_aka_client_error(data, id,
1263 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1264 	}
1265 
1266 	if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
1267 		struct wpabuf *res;
1268 		wpa_printf(MSG_INFO, "EAP-AKA: (encr) Invalid counter "
1269 			   "(%d <= %d)", eattr.counter, data->counter);
1270 		data->counter_too_small = eattr.counter;
1271 
1272 		/* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
1273 		 * reauth_id must not be used to start a new reauthentication.
1274 		 * However, since it was used in the last EAP-Response-Identity
1275 		 * packet, it has to saved for the following fullauth to be
1276 		 * used in MK derivation. */
1277 		os_free(data->last_eap_identity);
1278 		data->last_eap_identity = data->reauth_id;
1279 		data->last_eap_identity_len = data->reauth_id_len;
1280 		data->reauth_id = NULL;
1281 		data->reauth_id_len = 0;
1282 
1283 		res = eap_aka_response_reauth(data, id, 1, eattr.nonce_s);
1284 		os_free(decrypted);
1285 
1286 		return res;
1287 	}
1288 	data->counter = eattr.counter;
1289 
1290 	os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
1291 	wpa_hexdump(MSG_DEBUG, "EAP-AKA: (encr) AT_NONCE_S",
1292 		    data->nonce_s, EAP_SIM_NONCE_S_LEN);
1293 
1294 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
1295 		eap_aka_prime_derive_keys_reauth(data->k_re, data->counter,
1296 						 data->reauth_id,
1297 						 data->reauth_id_len,
1298 						 data->nonce_s,
1299 						 data->msk, data->emsk);
1300 	} else {
1301 		eap_sim_derive_keys_reauth(data->counter, data->reauth_id,
1302 					   data->reauth_id_len,
1303 					   data->nonce_s, data->mk,
1304 					   data->msk, data->emsk);
1305 	}
1306 	eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1307 	eap_aka_learn_ids(sm, data, &eattr);
1308 
1309 	if (data->result_ind && attr->result_ind)
1310 		data->use_result_ind = 1;
1311 
1312 	if (data->state != FAILURE) {
1313 		eap_aka_state(data, data->use_result_ind ?
1314 			      RESULT_SUCCESS : SUCCESS);
1315 	}
1316 
1317 	data->num_id_req = 0;
1318 	data->num_notification = 0;
1319 	if (data->counter > EAP_AKA_MAX_FAST_REAUTHS) {
1320 		wpa_printf(MSG_DEBUG, "EAP-AKA: Maximum number of "
1321 			   "fast reauths performed - force fullauth");
1322 		eap_aka_clear_identities(sm, data,
1323 					 CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1324 	}
1325 	os_free(decrypted);
1326 	return eap_aka_response_reauth(data, id, 0, data->nonce_s);
1327 }
1328 
1329 
eap_aka_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)1330 static struct wpabuf * eap_aka_process(struct eap_sm *sm, void *priv,
1331 				       struct eap_method_ret *ret,
1332 				       const struct wpabuf *reqData)
1333 {
1334 	struct eap_aka_data *data = priv;
1335 	const struct eap_hdr *req;
1336 	u8 subtype, id;
1337 	struct wpabuf *res;
1338 	const u8 *pos;
1339 	struct eap_sim_attrs attr;
1340 	size_t len;
1341 
1342 	wpa_hexdump_buf(MSG_DEBUG, "EAP-AKA: EAP data", reqData);
1343 	if (eap_get_config_identity(sm, &len) == NULL) {
1344 		wpa_printf(MSG_INFO, "EAP-AKA: Identity not configured");
1345 		eap_sm_request_identity(sm);
1346 		ret->ignore = TRUE;
1347 		return NULL;
1348 	}
1349 
1350 	pos = eap_hdr_validate(EAP_VENDOR_IETF, data->eap_method, reqData,
1351 			       &len);
1352 	if (pos == NULL || len < 3) {
1353 		ret->ignore = TRUE;
1354 		return NULL;
1355 	}
1356 	req = wpabuf_head(reqData);
1357 	id = req->identifier;
1358 	len = be_to_host16(req->length);
1359 
1360 	ret->ignore = FALSE;
1361 	ret->methodState = METHOD_MAY_CONT;
1362 	ret->decision = DECISION_FAIL;
1363 	ret->allowNotifications = TRUE;
1364 
1365 	subtype = *pos++;
1366 	wpa_printf(MSG_DEBUG, "EAP-AKA: Subtype=%d", subtype);
1367 	pos += 2; /* Reserved */
1368 
1369 	if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr,
1370 			       data->eap_method == EAP_TYPE_AKA_PRIME ? 2 : 1,
1371 			       0)) {
1372 		res = eap_aka_client_error(data, id,
1373 					   EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1374 		goto done;
1375 	}
1376 
1377 	switch (subtype) {
1378 	case EAP_AKA_SUBTYPE_IDENTITY:
1379 		res = eap_aka_process_identity(sm, data, id, reqData, &attr);
1380 		break;
1381 	case EAP_AKA_SUBTYPE_CHALLENGE:
1382 		res = eap_aka_process_challenge(sm, data, id, reqData, &attr);
1383 		break;
1384 	case EAP_AKA_SUBTYPE_NOTIFICATION:
1385 		res = eap_aka_process_notification(sm, data, id, reqData,
1386 						   &attr);
1387 		break;
1388 	case EAP_AKA_SUBTYPE_REAUTHENTICATION:
1389 		res = eap_aka_process_reauthentication(sm, data, id, reqData,
1390 						       &attr);
1391 		break;
1392 	case EAP_AKA_SUBTYPE_CLIENT_ERROR:
1393 		wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Client-Error");
1394 		res = eap_aka_client_error(data, id,
1395 					   EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1396 		break;
1397 	default:
1398 		wpa_printf(MSG_DEBUG, "EAP-AKA: Unknown subtype=%d", subtype);
1399 		res = eap_aka_client_error(data, id,
1400 					   EAP_AKA_UNABLE_TO_PROCESS_PACKET);
1401 		break;
1402 	}
1403 
1404 done:
1405 	if (data->state == FAILURE) {
1406 		ret->decision = DECISION_FAIL;
1407 		ret->methodState = METHOD_DONE;
1408 	} else if (data->state == SUCCESS) {
1409 		ret->decision = data->use_result_ind ?
1410 			DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
1411 		/*
1412 		 * It is possible for the server to reply with AKA
1413 		 * Notification, so we must allow the method to continue and
1414 		 * not only accept EAP-Success at this point.
1415 		 */
1416 		ret->methodState = data->use_result_ind ?
1417 			METHOD_DONE : METHOD_MAY_CONT;
1418 	} else if (data->state == RESULT_SUCCESS)
1419 		ret->methodState = METHOD_CONT;
1420 
1421 	if (ret->methodState == METHOD_DONE) {
1422 		ret->allowNotifications = FALSE;
1423 	}
1424 
1425 	return res;
1426 }
1427 
1428 
eap_aka_has_reauth_data(struct eap_sm * sm,void * priv)1429 static Boolean eap_aka_has_reauth_data(struct eap_sm *sm, void *priv)
1430 {
1431 	struct eap_aka_data *data = priv;
1432 	return data->pseudonym || data->reauth_id;
1433 }
1434 
1435 
eap_aka_deinit_for_reauth(struct eap_sm * sm,void * priv)1436 static void eap_aka_deinit_for_reauth(struct eap_sm *sm, void *priv)
1437 {
1438 	struct eap_aka_data *data = priv;
1439 	eap_aka_clear_identities(sm, data, CLEAR_EAP_ID);
1440 	data->prev_id = -1;
1441 	wpabuf_free(data->id_msgs);
1442 	data->id_msgs = NULL;
1443 	data->use_result_ind = 0;
1444 	data->kdf_negotiation = 0;
1445 	eap_aka_clear_keys(data, 1);
1446 }
1447 
1448 
eap_aka_init_for_reauth(struct eap_sm * sm,void * priv)1449 static void * eap_aka_init_for_reauth(struct eap_sm *sm, void *priv)
1450 {
1451 	struct eap_aka_data *data = priv;
1452 	data->num_id_req = 0;
1453 	data->num_notification = 0;
1454 	eap_aka_state(data, CONTINUE);
1455 	return priv;
1456 }
1457 
1458 
eap_aka_get_identity(struct eap_sm * sm,void * priv,size_t * len)1459 static const u8 * eap_aka_get_identity(struct eap_sm *sm, void *priv,
1460 				       size_t *len)
1461 {
1462 	struct eap_aka_data *data = priv;
1463 
1464 	if (data->reauth_id) {
1465 		*len = data->reauth_id_len;
1466 		return data->reauth_id;
1467 	}
1468 
1469 	if (data->pseudonym) {
1470 		*len = data->pseudonym_len;
1471 		return data->pseudonym;
1472 	}
1473 
1474 	return NULL;
1475 }
1476 
1477 
eap_aka_isKeyAvailable(struct eap_sm * sm,void * priv)1478 static Boolean eap_aka_isKeyAvailable(struct eap_sm *sm, void *priv)
1479 {
1480 	struct eap_aka_data *data = priv;
1481 	return data->state == SUCCESS;
1482 }
1483 
1484 
eap_aka_getKey(struct eap_sm * sm,void * priv,size_t * len)1485 static u8 * eap_aka_getKey(struct eap_sm *sm, void *priv, size_t *len)
1486 {
1487 	struct eap_aka_data *data = priv;
1488 	u8 *key;
1489 
1490 	if (data->state != SUCCESS)
1491 		return NULL;
1492 
1493 	key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN);
1494 	if (key == NULL)
1495 		return NULL;
1496 
1497 	*len = EAP_SIM_KEYING_DATA_LEN;
1498 
1499 	return key;
1500 }
1501 
1502 
eap_aka_get_session_id(struct eap_sm * sm,void * priv,size_t * len)1503 static u8 * eap_aka_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1504 {
1505 	struct eap_aka_data *data = priv;
1506 	u8 *id;
1507 
1508 	if (data->state != SUCCESS)
1509 		return NULL;
1510 
1511 	*len = 1 + EAP_AKA_RAND_LEN + EAP_AKA_AUTN_LEN;
1512 	id = os_malloc(*len);
1513 	if (id == NULL)
1514 		return NULL;
1515 
1516 	id[0] = data->eap_method;
1517 	os_memcpy(id + 1, data->rand, EAP_AKA_RAND_LEN);
1518 	os_memcpy(id + 1 + EAP_AKA_RAND_LEN, data->autn, EAP_AKA_AUTN_LEN);
1519 	wpa_hexdump(MSG_DEBUG, "EAP-AKA: Derived Session-Id", id, *len);
1520 
1521 	return id;
1522 }
1523 
1524 
eap_aka_get_emsk(struct eap_sm * sm,void * priv,size_t * len)1525 static u8 * eap_aka_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1526 {
1527 	struct eap_aka_data *data = priv;
1528 	u8 *key;
1529 
1530 	if (data->state != SUCCESS)
1531 		return NULL;
1532 
1533 	key = os_memdup(data->emsk, EAP_EMSK_LEN);
1534 	if (key == NULL)
1535 		return NULL;
1536 
1537 	*len = EAP_EMSK_LEN;
1538 
1539 	return key;
1540 }
1541 
1542 
eap_aka_get_error_code(void * priv)1543 static int eap_aka_get_error_code(void *priv)
1544 {
1545 	struct eap_aka_data *data = priv;
1546 	int current_data_error;
1547 
1548 	if (!data)
1549 		return NO_EAP_METHOD_ERROR;
1550 
1551 	current_data_error = data->error_code;
1552 
1553 	/* Now reset for next transaction */
1554 	data->error_code = NO_EAP_METHOD_ERROR;
1555 
1556 	return current_data_error;
1557 }
1558 
1559 
eap_peer_aka_register(void)1560 int eap_peer_aka_register(void)
1561 {
1562 	struct eap_method *eap;
1563 
1564 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1565 				    EAP_VENDOR_IETF, EAP_TYPE_AKA, "AKA");
1566 	if (eap == NULL)
1567 		return -1;
1568 
1569 	eap->init = eap_aka_init;
1570 	eap->deinit = eap_aka_deinit;
1571 	eap->process = eap_aka_process;
1572 	eap->isKeyAvailable = eap_aka_isKeyAvailable;
1573 	eap->getKey = eap_aka_getKey;
1574 	eap->getSessionId = eap_aka_get_session_id;
1575 	eap->has_reauth_data = eap_aka_has_reauth_data;
1576 	eap->deinit_for_reauth = eap_aka_deinit_for_reauth;
1577 	eap->init_for_reauth = eap_aka_init_for_reauth;
1578 	eap->get_identity = eap_aka_get_identity;
1579 	eap->get_emsk = eap_aka_get_emsk;
1580 	eap->get_error_code = eap_aka_get_error_code;
1581 
1582 	return eap_peer_method_register(eap);
1583 }
1584 
1585 
1586 #ifdef EAP_AKA_PRIME
eap_peer_aka_prime_register(void)1587 int eap_peer_aka_prime_register(void)
1588 {
1589 	struct eap_method *eap;
1590 
1591 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1592 				    EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME,
1593 				    "AKA'");
1594 	if (eap == NULL)
1595 		return -1;
1596 
1597 	eap->init = eap_aka_prime_init;
1598 	eap->deinit = eap_aka_deinit;
1599 	eap->process = eap_aka_process;
1600 	eap->isKeyAvailable = eap_aka_isKeyAvailable;
1601 	eap->getKey = eap_aka_getKey;
1602 	eap->getSessionId = eap_aka_get_session_id;
1603 	eap->has_reauth_data = eap_aka_has_reauth_data;
1604 	eap->deinit_for_reauth = eap_aka_deinit_for_reauth;
1605 	eap->init_for_reauth = eap_aka_init_for_reauth;
1606 	eap->get_identity = eap_aka_get_identity;
1607 	eap->get_emsk = eap_aka_get_emsk;
1608 	eap->get_error_code = eap_aka_get_error_code;
1609 
1610 	return eap_peer_method_register(eap);
1611 }
1612 #endif /* EAP_AKA_PRIME */
1613