xref: /freebsd/contrib/wpa/src/eap_peer/eap_aka.c (revision 85732ac8bccbc0adcf5a261ea1ffec8ca7b3a92d)
139beb93cSSam Leffler /*
2f05cddf9SRui Paulo  * EAP peer method: EAP-AKA (RFC 4187) and EAP-AKA' (RFC 5448)
3f05cddf9SRui Paulo  * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
439beb93cSSam Leffler  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
739beb93cSSam Leffler  */
839beb93cSSam Leffler 
939beb93cSSam Leffler #include "includes.h"
1039beb93cSSam Leffler 
1139beb93cSSam Leffler #include "common.h"
1239beb93cSSam Leffler #include "pcsc_funcs.h"
13e28a4053SRui Paulo #include "crypto/crypto.h"
14e28a4053SRui Paulo #include "crypto/sha1.h"
15e28a4053SRui Paulo #include "crypto/sha256.h"
16e28a4053SRui Paulo #include "crypto/milenage.h"
1739beb93cSSam Leffler #include "eap_common/eap_sim_common.h"
18e28a4053SRui Paulo #include "eap_config.h"
19e28a4053SRui Paulo #include "eap_i.h"
2039beb93cSSam Leffler 
2139beb93cSSam Leffler 
2239beb93cSSam Leffler struct eap_aka_data {
2339beb93cSSam Leffler 	u8 ik[EAP_AKA_IK_LEN], ck[EAP_AKA_CK_LEN], res[EAP_AKA_RES_MAX_LEN];
2439beb93cSSam Leffler 	size_t res_len;
2539beb93cSSam Leffler 	u8 nonce_s[EAP_SIM_NONCE_S_LEN];
2639beb93cSSam Leffler 	u8 mk[EAP_SIM_MK_LEN];
2739beb93cSSam Leffler 	u8 k_aut[EAP_AKA_PRIME_K_AUT_LEN];
2839beb93cSSam Leffler 	u8 k_encr[EAP_SIM_K_ENCR_LEN];
2939beb93cSSam Leffler 	u8 k_re[EAP_AKA_PRIME_K_RE_LEN]; /* EAP-AKA' only */
3039beb93cSSam Leffler 	u8 msk[EAP_SIM_KEYING_DATA_LEN];
3139beb93cSSam Leffler 	u8 emsk[EAP_EMSK_LEN];
3239beb93cSSam Leffler 	u8 rand[EAP_AKA_RAND_LEN], autn[EAP_AKA_AUTN_LEN];
3339beb93cSSam Leffler 	u8 auts[EAP_AKA_AUTS_LEN];
3439beb93cSSam Leffler 
3539beb93cSSam Leffler 	int num_id_req, num_notification;
3639beb93cSSam Leffler 	u8 *pseudonym;
3739beb93cSSam Leffler 	size_t pseudonym_len;
3839beb93cSSam Leffler 	u8 *reauth_id;
3939beb93cSSam Leffler 	size_t reauth_id_len;
4039beb93cSSam Leffler 	int reauth;
4139beb93cSSam Leffler 	unsigned int counter, counter_too_small;
4239beb93cSSam Leffler 	u8 *last_eap_identity;
4339beb93cSSam Leffler 	size_t last_eap_identity_len;
4439beb93cSSam Leffler 	enum {
455b9c547cSRui Paulo 		CONTINUE, RESULT_SUCCESS, SUCCESS, FAILURE
4639beb93cSSam Leffler 	} state;
4739beb93cSSam Leffler 
4839beb93cSSam Leffler 	struct wpabuf *id_msgs;
4939beb93cSSam Leffler 	int prev_id;
5039beb93cSSam Leffler 	int result_ind, use_result_ind;
51*85732ac8SCy Schubert 	int use_pseudonym;
5239beb93cSSam Leffler 	u8 eap_method;
5339beb93cSSam Leffler 	u8 *network_name;
5439beb93cSSam Leffler 	size_t network_name_len;
5539beb93cSSam Leffler 	u16 kdf;
5639beb93cSSam Leffler 	int kdf_negotiation;
57*85732ac8SCy Schubert 	u16 last_kdf_attrs[EAP_AKA_PRIME_KDF_MAX];
58*85732ac8SCy Schubert 	size_t last_kdf_count;
59*85732ac8SCy Schubert 	int error_code;
6039beb93cSSam Leffler };
6139beb93cSSam Leffler 
6239beb93cSSam Leffler 
6339beb93cSSam Leffler #ifndef CONFIG_NO_STDOUT_DEBUG
6439beb93cSSam Leffler static const char * eap_aka_state_txt(int state)
6539beb93cSSam Leffler {
6639beb93cSSam Leffler 	switch (state) {
6739beb93cSSam Leffler 	case CONTINUE:
6839beb93cSSam Leffler 		return "CONTINUE";
6939beb93cSSam Leffler 	case RESULT_SUCCESS:
7039beb93cSSam Leffler 		return "RESULT_SUCCESS";
7139beb93cSSam Leffler 	case SUCCESS:
7239beb93cSSam Leffler 		return "SUCCESS";
7339beb93cSSam Leffler 	case FAILURE:
7439beb93cSSam Leffler 		return "FAILURE";
7539beb93cSSam Leffler 	default:
7639beb93cSSam Leffler 		return "?";
7739beb93cSSam Leffler 	}
7839beb93cSSam Leffler }
7939beb93cSSam Leffler #endif /* CONFIG_NO_STDOUT_DEBUG */
8039beb93cSSam Leffler 
8139beb93cSSam Leffler 
8239beb93cSSam Leffler static void eap_aka_state(struct eap_aka_data *data, int state)
8339beb93cSSam Leffler {
8439beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "EAP-AKA: %s -> %s",
8539beb93cSSam Leffler 		   eap_aka_state_txt(data->state),
8639beb93cSSam Leffler 		   eap_aka_state_txt(state));
8739beb93cSSam Leffler 	data->state = state;
8839beb93cSSam Leffler }
8939beb93cSSam Leffler 
9039beb93cSSam Leffler 
9139beb93cSSam Leffler static void * eap_aka_init(struct eap_sm *sm)
9239beb93cSSam Leffler {
9339beb93cSSam Leffler 	struct eap_aka_data *data;
9439beb93cSSam Leffler 	const char *phase1 = eap_get_config_phase1(sm);
95f05cddf9SRui Paulo 	struct eap_peer_config *config = eap_get_config(sm);
9639beb93cSSam Leffler 
9739beb93cSSam Leffler 	data = os_zalloc(sizeof(*data));
9839beb93cSSam Leffler 	if (data == NULL)
9939beb93cSSam Leffler 		return NULL;
10039beb93cSSam Leffler 
10139beb93cSSam Leffler 	data->eap_method = EAP_TYPE_AKA;
10239beb93cSSam Leffler 
103*85732ac8SCy Schubert 	/* Zero is a valid error code, so we need to initialize */
104*85732ac8SCy Schubert 	data->error_code = NO_EAP_METHOD_ERROR;
105*85732ac8SCy Schubert 
10639beb93cSSam Leffler 	eap_aka_state(data, CONTINUE);
10739beb93cSSam Leffler 	data->prev_id = -1;
10839beb93cSSam Leffler 
10939beb93cSSam Leffler 	data->result_ind = phase1 && os_strstr(phase1, "result_ind=1") != NULL;
11039beb93cSSam Leffler 
111*85732ac8SCy Schubert 	data->use_pseudonym = !sm->init_phase2;
112*85732ac8SCy Schubert 	if (config && config->anonymous_identity && data->use_pseudonym) {
113f05cddf9SRui Paulo 		data->pseudonym = os_malloc(config->anonymous_identity_len);
114f05cddf9SRui Paulo 		if (data->pseudonym) {
115f05cddf9SRui Paulo 			os_memcpy(data->pseudonym, config->anonymous_identity,
116f05cddf9SRui Paulo 				  config->anonymous_identity_len);
117f05cddf9SRui Paulo 			data->pseudonym_len = config->anonymous_identity_len;
118f05cddf9SRui Paulo 		}
119f05cddf9SRui Paulo 	}
120f05cddf9SRui Paulo 
12139beb93cSSam Leffler 	return data;
12239beb93cSSam Leffler }
12339beb93cSSam Leffler 
12439beb93cSSam Leffler 
12539beb93cSSam Leffler #ifdef EAP_AKA_PRIME
12639beb93cSSam Leffler static void * eap_aka_prime_init(struct eap_sm *sm)
12739beb93cSSam Leffler {
12839beb93cSSam Leffler 	struct eap_aka_data *data = eap_aka_init(sm);
12939beb93cSSam Leffler 	if (data == NULL)
13039beb93cSSam Leffler 		return NULL;
13139beb93cSSam Leffler 	data->eap_method = EAP_TYPE_AKA_PRIME;
13239beb93cSSam Leffler 	return data;
13339beb93cSSam Leffler }
13439beb93cSSam Leffler #endif /* EAP_AKA_PRIME */
13539beb93cSSam Leffler 
13639beb93cSSam Leffler 
1375b9c547cSRui Paulo static void eap_aka_clear_keys(struct eap_aka_data *data, int reauth)
1385b9c547cSRui Paulo {
1395b9c547cSRui Paulo 	if (!reauth) {
1405b9c547cSRui Paulo 		os_memset(data->mk, 0, EAP_SIM_MK_LEN);
1415b9c547cSRui Paulo 		os_memset(data->k_aut, 0, EAP_AKA_PRIME_K_AUT_LEN);
1425b9c547cSRui Paulo 		os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN);
1435b9c547cSRui Paulo 		os_memset(data->k_re, 0, EAP_AKA_PRIME_K_RE_LEN);
1445b9c547cSRui Paulo 	}
1455b9c547cSRui Paulo 	os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN);
1465b9c547cSRui Paulo 	os_memset(data->emsk, 0, EAP_EMSK_LEN);
1475b9c547cSRui Paulo 	os_memset(data->autn, 0, EAP_AKA_AUTN_LEN);
1485b9c547cSRui Paulo 	os_memset(data->auts, 0, EAP_AKA_AUTS_LEN);
1495b9c547cSRui Paulo }
1505b9c547cSRui Paulo 
1515b9c547cSRui Paulo 
15239beb93cSSam Leffler static void eap_aka_deinit(struct eap_sm *sm, void *priv)
15339beb93cSSam Leffler {
15439beb93cSSam Leffler 	struct eap_aka_data *data = priv;
15539beb93cSSam Leffler 	if (data) {
15639beb93cSSam Leffler 		os_free(data->pseudonym);
15739beb93cSSam Leffler 		os_free(data->reauth_id);
15839beb93cSSam Leffler 		os_free(data->last_eap_identity);
15939beb93cSSam Leffler 		wpabuf_free(data->id_msgs);
16039beb93cSSam Leffler 		os_free(data->network_name);
1615b9c547cSRui Paulo 		eap_aka_clear_keys(data, 0);
16239beb93cSSam Leffler 		os_free(data);
16339beb93cSSam Leffler 	}
16439beb93cSSam Leffler }
16539beb93cSSam Leffler 
16639beb93cSSam Leffler 
1675b9c547cSRui Paulo static int eap_aka_ext_sim_req(struct eap_sm *sm, struct eap_aka_data *data)
1685b9c547cSRui Paulo {
1695b9c547cSRui Paulo 	char req[200], *pos, *end;
1705b9c547cSRui Paulo 
1715b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-AKA: Use external USIM processing");
1725b9c547cSRui Paulo 	pos = req;
1735b9c547cSRui Paulo 	end = pos + sizeof(req);
1745b9c547cSRui Paulo 	pos += os_snprintf(pos, end - pos, "UMTS-AUTH");
1755b9c547cSRui Paulo 	pos += os_snprintf(pos, end - pos, ":");
1765b9c547cSRui Paulo 	pos += wpa_snprintf_hex(pos, end - pos, data->rand, EAP_AKA_RAND_LEN);
1775b9c547cSRui Paulo 	pos += os_snprintf(pos, end - pos, ":");
1785b9c547cSRui Paulo 	wpa_snprintf_hex(pos, end - pos, data->autn, EAP_AKA_AUTN_LEN);
1795b9c547cSRui Paulo 
1805b9c547cSRui Paulo 	eap_sm_request_sim(sm, req);
1815b9c547cSRui Paulo 	return 1;
1825b9c547cSRui Paulo }
1835b9c547cSRui Paulo 
1845b9c547cSRui Paulo 
1855b9c547cSRui Paulo static int eap_aka_ext_sim_result(struct eap_sm *sm, struct eap_aka_data *data,
1865b9c547cSRui Paulo 				  struct eap_peer_config *conf)
1875b9c547cSRui Paulo {
1885b9c547cSRui Paulo 	char *resp, *pos;
1895b9c547cSRui Paulo 
1905b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG,
1915b9c547cSRui Paulo 		   "EAP-AKA: Use result from external USIM processing");
1925b9c547cSRui Paulo 
1935b9c547cSRui Paulo 	resp = conf->external_sim_resp;
1945b9c547cSRui Paulo 	conf->external_sim_resp = NULL;
1955b9c547cSRui Paulo 
1965b9c547cSRui Paulo 	if (os_strncmp(resp, "UMTS-AUTS:", 10) == 0) {
1975b9c547cSRui Paulo 		pos = resp + 10;
1985b9c547cSRui Paulo 		if (hexstr2bin(pos, data->auts, EAP_AKA_AUTS_LEN) < 0)
1995b9c547cSRui Paulo 			goto invalid;
2005b9c547cSRui Paulo 		wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: AUTS", data->auts,
2015b9c547cSRui Paulo 				EAP_AKA_AUTS_LEN);
2025b9c547cSRui Paulo 		os_free(resp);
2035b9c547cSRui Paulo 		return -2;
2045b9c547cSRui Paulo 	}
2055b9c547cSRui Paulo 
2065b9c547cSRui Paulo 	if (os_strncmp(resp, "UMTS-AUTH:", 10) != 0) {
2075b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-AKA: Unrecognized external USIM processing response");
2085b9c547cSRui Paulo 		os_free(resp);
2095b9c547cSRui Paulo 		return -1;
2105b9c547cSRui Paulo 	}
2115b9c547cSRui Paulo 
2125b9c547cSRui Paulo 	pos = resp + 10;
2135b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "EAP-AKA: RAND", data->rand, EAP_AKA_RAND_LEN);
2145b9c547cSRui Paulo 
2155b9c547cSRui Paulo 	if (hexstr2bin(pos, data->ik, EAP_AKA_IK_LEN) < 0)
2165b9c547cSRui Paulo 		goto invalid;
2175b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: IK", data->ik, EAP_AKA_IK_LEN);
2185b9c547cSRui Paulo 	pos += EAP_AKA_IK_LEN * 2;
2195b9c547cSRui Paulo 	if (*pos != ':')
2205b9c547cSRui Paulo 		goto invalid;
2215b9c547cSRui Paulo 	pos++;
2225b9c547cSRui Paulo 
2235b9c547cSRui Paulo 	if (hexstr2bin(pos, data->ck, EAP_AKA_CK_LEN) < 0)
2245b9c547cSRui Paulo 		goto invalid;
2255b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: CK", data->ck, EAP_AKA_CK_LEN);
2265b9c547cSRui Paulo 	pos += EAP_AKA_CK_LEN * 2;
2275b9c547cSRui Paulo 	if (*pos != ':')
2285b9c547cSRui Paulo 		goto invalid;
2295b9c547cSRui Paulo 	pos++;
2305b9c547cSRui Paulo 
2315b9c547cSRui Paulo 	data->res_len = os_strlen(pos) / 2;
2325b9c547cSRui Paulo 	if (data->res_len > EAP_AKA_RES_MAX_LEN) {
2335b9c547cSRui Paulo 		data->res_len = 0;
2345b9c547cSRui Paulo 		goto invalid;
2355b9c547cSRui Paulo 	}
2365b9c547cSRui Paulo 	if (hexstr2bin(pos, data->res, data->res_len) < 0)
2375b9c547cSRui Paulo 		goto invalid;
2385b9c547cSRui Paulo 	wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: RES", data->res, data->res_len);
2395b9c547cSRui Paulo 
2405b9c547cSRui Paulo 	os_free(resp);
2415b9c547cSRui Paulo 	return 0;
2425b9c547cSRui Paulo 
2435b9c547cSRui Paulo invalid:
2445b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-AKA: Invalid external USIM processing UMTS-AUTH response");
2455b9c547cSRui Paulo 	os_free(resp);
2465b9c547cSRui Paulo 	return -1;
2475b9c547cSRui Paulo }
2485b9c547cSRui Paulo 
2495b9c547cSRui Paulo 
25039beb93cSSam Leffler static int eap_aka_umts_auth(struct eap_sm *sm, struct eap_aka_data *data)
25139beb93cSSam Leffler {
25239beb93cSSam Leffler 	struct eap_peer_config *conf;
25339beb93cSSam Leffler 
25439beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "EAP-AKA: UMTS authentication algorithm");
25539beb93cSSam Leffler 
25639beb93cSSam Leffler 	conf = eap_get_config(sm);
25739beb93cSSam Leffler 	if (conf == NULL)
25839beb93cSSam Leffler 		return -1;
2595b9c547cSRui Paulo 
2605b9c547cSRui Paulo 	if (sm->external_sim) {
2615b9c547cSRui Paulo 		if (conf->external_sim_resp)
2625b9c547cSRui Paulo 			return eap_aka_ext_sim_result(sm, data, conf);
2635b9c547cSRui Paulo 		else
2645b9c547cSRui Paulo 			return eap_aka_ext_sim_req(sm, data);
2655b9c547cSRui Paulo 	}
2665b9c547cSRui Paulo 
26739beb93cSSam Leffler 	if (conf->pcsc) {
26839beb93cSSam Leffler 		return scard_umts_auth(sm->scard_ctx, data->rand,
26939beb93cSSam Leffler 				       data->autn, data->res, &data->res_len,
27039beb93cSSam Leffler 				       data->ik, data->ck, data->auts);
27139beb93cSSam Leffler 	}
27239beb93cSSam Leffler 
27339beb93cSSam Leffler #ifdef CONFIG_USIM_SIMULATOR
27439beb93cSSam Leffler 	if (conf->password) {
27539beb93cSSam Leffler 		u8 opc[16], k[16], sqn[6];
27639beb93cSSam Leffler 		const char *pos;
27739beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "EAP-AKA: Use internal Milenage "
27839beb93cSSam Leffler 			   "implementation for UMTS authentication");
27939beb93cSSam Leffler 		if (conf->password_len < 78) {
28039beb93cSSam Leffler 			wpa_printf(MSG_DEBUG, "EAP-AKA: invalid Milenage "
28139beb93cSSam Leffler 				   "password");
28239beb93cSSam Leffler 			return -1;
28339beb93cSSam Leffler 		}
28439beb93cSSam Leffler 		pos = (const char *) conf->password;
28539beb93cSSam Leffler 		if (hexstr2bin(pos, k, 16))
28639beb93cSSam Leffler 			return -1;
28739beb93cSSam Leffler 		pos += 32;
28839beb93cSSam Leffler 		if (*pos != ':')
28939beb93cSSam Leffler 			return -1;
29039beb93cSSam Leffler 		pos++;
29139beb93cSSam Leffler 
29239beb93cSSam Leffler 		if (hexstr2bin(pos, opc, 16))
29339beb93cSSam Leffler 			return -1;
29439beb93cSSam Leffler 		pos += 32;
29539beb93cSSam Leffler 		if (*pos != ':')
29639beb93cSSam Leffler 			return -1;
29739beb93cSSam Leffler 		pos++;
29839beb93cSSam Leffler 
29939beb93cSSam Leffler 		if (hexstr2bin(pos, sqn, 6))
30039beb93cSSam Leffler 			return -1;
30139beb93cSSam Leffler 
30239beb93cSSam Leffler 		return milenage_check(opc, k, sqn, data->rand, data->autn,
30339beb93cSSam Leffler 				      data->ik, data->ck,
30439beb93cSSam Leffler 				      data->res, &data->res_len, data->auts);
30539beb93cSSam Leffler 	}
30639beb93cSSam Leffler #endif /* CONFIG_USIM_SIMULATOR */
30739beb93cSSam Leffler 
30839beb93cSSam Leffler #ifdef CONFIG_USIM_HARDCODED
30939beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "EAP-AKA: Use hardcoded Kc and SRES values for "
31039beb93cSSam Leffler 		   "testing");
31139beb93cSSam Leffler 
31239beb93cSSam Leffler 	/* These hardcoded Kc and SRES values are used for testing.
31339beb93cSSam Leffler 	 * Could consider making them configurable. */
31439beb93cSSam Leffler 	os_memset(data->res, '2', EAP_AKA_RES_MAX_LEN);
31539beb93cSSam Leffler 	data->res_len = EAP_AKA_RES_MAX_LEN;
31639beb93cSSam Leffler 	os_memset(data->ik, '3', EAP_AKA_IK_LEN);
31739beb93cSSam Leffler 	os_memset(data->ck, '4', EAP_AKA_CK_LEN);
31839beb93cSSam Leffler 	{
31939beb93cSSam Leffler 		u8 autn[EAP_AKA_AUTN_LEN];
32039beb93cSSam Leffler 		os_memset(autn, '1', EAP_AKA_AUTN_LEN);
3215b9c547cSRui Paulo 		if (os_memcmp_const(autn, data->autn, EAP_AKA_AUTN_LEN) != 0) {
32239beb93cSSam Leffler 			wpa_printf(MSG_WARNING, "EAP-AKA: AUTN did not match "
32339beb93cSSam Leffler 				   "with expected value");
32439beb93cSSam Leffler 			return -1;
32539beb93cSSam Leffler 		}
32639beb93cSSam Leffler 	}
32739beb93cSSam Leffler #if 0
32839beb93cSSam Leffler 	{
32939beb93cSSam Leffler 		static int test_resync = 1;
33039beb93cSSam Leffler 		if (test_resync) {
33139beb93cSSam Leffler 			/* Test Resynchronization */
33239beb93cSSam Leffler 			test_resync = 0;
33339beb93cSSam Leffler 			return -2;
33439beb93cSSam Leffler 		}
33539beb93cSSam Leffler 	}
33639beb93cSSam Leffler #endif
33739beb93cSSam Leffler 	return 0;
33839beb93cSSam Leffler 
33939beb93cSSam Leffler #else /* CONFIG_USIM_HARDCODED */
34039beb93cSSam Leffler 
3415b9c547cSRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-AKA: No UMTS authentication algorithm "
34239beb93cSSam Leffler 		   "enabled");
34339beb93cSSam Leffler 	return -1;
34439beb93cSSam Leffler 
34539beb93cSSam Leffler #endif /* CONFIG_USIM_HARDCODED */
34639beb93cSSam Leffler }
34739beb93cSSam Leffler 
34839beb93cSSam Leffler 
34939beb93cSSam Leffler #define CLEAR_PSEUDONYM	0x01
35039beb93cSSam Leffler #define CLEAR_REAUTH_ID	0x02
35139beb93cSSam Leffler #define CLEAR_EAP_ID	0x04
35239beb93cSSam Leffler 
353f05cddf9SRui Paulo static void eap_aka_clear_identities(struct eap_sm *sm,
354f05cddf9SRui Paulo 				     struct eap_aka_data *data, int id)
35539beb93cSSam Leffler {
356f05cddf9SRui Paulo 	if ((id & CLEAR_PSEUDONYM) && data->pseudonym) {
357f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old pseudonym");
35839beb93cSSam Leffler 		os_free(data->pseudonym);
35939beb93cSSam Leffler 		data->pseudonym = NULL;
36039beb93cSSam Leffler 		data->pseudonym_len = 0;
361*85732ac8SCy Schubert 		if (data->use_pseudonym)
362f05cddf9SRui Paulo 			eap_set_anon_id(sm, NULL, 0);
36339beb93cSSam Leffler 	}
364f05cddf9SRui Paulo 	if ((id & CLEAR_REAUTH_ID) && data->reauth_id) {
365f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old reauth_id");
36639beb93cSSam Leffler 		os_free(data->reauth_id);
36739beb93cSSam Leffler 		data->reauth_id = NULL;
36839beb93cSSam Leffler 		data->reauth_id_len = 0;
36939beb93cSSam Leffler 	}
370f05cddf9SRui Paulo 	if ((id & CLEAR_EAP_ID) && data->last_eap_identity) {
371f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old eap_id");
37239beb93cSSam Leffler 		os_free(data->last_eap_identity);
37339beb93cSSam Leffler 		data->last_eap_identity = NULL;
37439beb93cSSam Leffler 		data->last_eap_identity_len = 0;
37539beb93cSSam Leffler 	}
37639beb93cSSam Leffler }
37739beb93cSSam Leffler 
37839beb93cSSam Leffler 
379f05cddf9SRui Paulo static int eap_aka_learn_ids(struct eap_sm *sm, struct eap_aka_data *data,
38039beb93cSSam Leffler 			     struct eap_sim_attrs *attr)
38139beb93cSSam Leffler {
38239beb93cSSam Leffler 	if (attr->next_pseudonym) {
383f05cddf9SRui Paulo 		const u8 *identity = NULL;
384f05cddf9SRui Paulo 		size_t identity_len = 0;
385f05cddf9SRui Paulo 		const u8 *realm = NULL;
386f05cddf9SRui Paulo 		size_t realm_len = 0;
387f05cddf9SRui Paulo 
388f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG,
389f05cddf9SRui Paulo 				  "EAP-AKA: (encr) AT_NEXT_PSEUDONYM",
390f05cddf9SRui Paulo 				  attr->next_pseudonym,
391f05cddf9SRui Paulo 				  attr->next_pseudonym_len);
39239beb93cSSam Leffler 		os_free(data->pseudonym);
393f05cddf9SRui Paulo 		/* Look for the realm of the permanent identity */
394f05cddf9SRui Paulo 		identity = eap_get_config_identity(sm, &identity_len);
395f05cddf9SRui Paulo 		if (identity) {
396f05cddf9SRui Paulo 			for (realm = identity, realm_len = identity_len;
397f05cddf9SRui Paulo 			     realm_len > 0; realm_len--, realm++) {
398f05cddf9SRui Paulo 				if (*realm == '@')
399f05cddf9SRui Paulo 					break;
400f05cddf9SRui Paulo 			}
401f05cddf9SRui Paulo 		}
402f05cddf9SRui Paulo 		data->pseudonym = os_malloc(attr->next_pseudonym_len +
403f05cddf9SRui Paulo 					    realm_len);
40439beb93cSSam Leffler 		if (data->pseudonym == NULL) {
40539beb93cSSam Leffler 			wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for "
40639beb93cSSam Leffler 				   "next pseudonym");
407f05cddf9SRui Paulo 			data->pseudonym_len = 0;
40839beb93cSSam Leffler 			return -1;
40939beb93cSSam Leffler 		}
41039beb93cSSam Leffler 		os_memcpy(data->pseudonym, attr->next_pseudonym,
41139beb93cSSam Leffler 			  attr->next_pseudonym_len);
412f05cddf9SRui Paulo 		if (realm_len) {
413f05cddf9SRui Paulo 			os_memcpy(data->pseudonym + attr->next_pseudonym_len,
414f05cddf9SRui Paulo 				  realm, realm_len);
415f05cddf9SRui Paulo 		}
416f05cddf9SRui Paulo 		data->pseudonym_len = attr->next_pseudonym_len + realm_len;
417*85732ac8SCy Schubert 		if (data->use_pseudonym)
418*85732ac8SCy Schubert 			eap_set_anon_id(sm, data->pseudonym,
419*85732ac8SCy Schubert 					data->pseudonym_len);
42039beb93cSSam Leffler 	}
42139beb93cSSam Leffler 
42239beb93cSSam Leffler 	if (attr->next_reauth_id) {
42339beb93cSSam Leffler 		os_free(data->reauth_id);
424*85732ac8SCy Schubert 		data->reauth_id = os_memdup(attr->next_reauth_id,
425*85732ac8SCy Schubert 					    attr->next_reauth_id_len);
42639beb93cSSam Leffler 		if (data->reauth_id == NULL) {
42739beb93cSSam Leffler 			wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for "
42839beb93cSSam Leffler 				   "next reauth_id");
429f05cddf9SRui Paulo 			data->reauth_id_len = 0;
43039beb93cSSam Leffler 			return -1;
43139beb93cSSam Leffler 		}
43239beb93cSSam Leffler 		data->reauth_id_len = attr->next_reauth_id_len;
43339beb93cSSam Leffler 		wpa_hexdump_ascii(MSG_DEBUG,
43439beb93cSSam Leffler 				  "EAP-AKA: (encr) AT_NEXT_REAUTH_ID",
43539beb93cSSam Leffler 				  data->reauth_id,
43639beb93cSSam Leffler 				  data->reauth_id_len);
43739beb93cSSam Leffler 	}
43839beb93cSSam Leffler 
43939beb93cSSam Leffler 	return 0;
44039beb93cSSam Leffler }
44139beb93cSSam Leffler 
44239beb93cSSam Leffler 
44339beb93cSSam Leffler static int eap_aka_add_id_msg(struct eap_aka_data *data,
44439beb93cSSam Leffler 			      const struct wpabuf *msg)
44539beb93cSSam Leffler {
44639beb93cSSam Leffler 	if (msg == NULL)
44739beb93cSSam Leffler 		return -1;
44839beb93cSSam Leffler 
44939beb93cSSam Leffler 	if (data->id_msgs == NULL) {
45039beb93cSSam Leffler 		data->id_msgs = wpabuf_dup(msg);
45139beb93cSSam Leffler 		return data->id_msgs == NULL ? -1 : 0;
45239beb93cSSam Leffler 	}
45339beb93cSSam Leffler 
45439beb93cSSam Leffler 	if (wpabuf_resize(&data->id_msgs, wpabuf_len(msg)) < 0)
45539beb93cSSam Leffler 		return -1;
45639beb93cSSam Leffler 	wpabuf_put_buf(data->id_msgs, msg);
45739beb93cSSam Leffler 
45839beb93cSSam Leffler 	return 0;
45939beb93cSSam Leffler }
46039beb93cSSam Leffler 
46139beb93cSSam Leffler 
46239beb93cSSam Leffler static void eap_aka_add_checkcode(struct eap_aka_data *data,
46339beb93cSSam Leffler 				  struct eap_sim_msg *msg)
46439beb93cSSam Leffler {
46539beb93cSSam Leffler 	const u8 *addr;
46639beb93cSSam Leffler 	size_t len;
46739beb93cSSam Leffler 	u8 hash[SHA256_MAC_LEN];
46839beb93cSSam Leffler 
46939beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   AT_CHECKCODE");
47039beb93cSSam Leffler 
47139beb93cSSam Leffler 	if (data->id_msgs == NULL) {
47239beb93cSSam Leffler 		/*
47339beb93cSSam Leffler 		 * No EAP-AKA/Identity packets were exchanged - send empty
47439beb93cSSam Leffler 		 * checkcode.
47539beb93cSSam Leffler 		 */
47639beb93cSSam Leffler 		eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, NULL, 0);
47739beb93cSSam Leffler 		return;
47839beb93cSSam Leffler 	}
47939beb93cSSam Leffler 
48039beb93cSSam Leffler 	/* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */
48139beb93cSSam Leffler 	addr = wpabuf_head(data->id_msgs);
48239beb93cSSam Leffler 	len = wpabuf_len(data->id_msgs);
48339beb93cSSam Leffler 	wpa_hexdump(MSG_MSGDUMP, "EAP-AKA: AT_CHECKCODE data", addr, len);
48439beb93cSSam Leffler #ifdef EAP_AKA_PRIME
48539beb93cSSam Leffler 	if (data->eap_method == EAP_TYPE_AKA_PRIME)
48639beb93cSSam Leffler 		sha256_vector(1, &addr, &len, hash);
48739beb93cSSam Leffler 	else
48839beb93cSSam Leffler #endif /* EAP_AKA_PRIME */
48939beb93cSSam Leffler 		sha1_vector(1, &addr, &len, hash);
49039beb93cSSam Leffler 
49139beb93cSSam Leffler 	eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, hash,
49239beb93cSSam Leffler 			data->eap_method == EAP_TYPE_AKA_PRIME ?
49339beb93cSSam Leffler 			EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN);
49439beb93cSSam Leffler }
49539beb93cSSam Leffler 
49639beb93cSSam Leffler 
49739beb93cSSam Leffler static int eap_aka_verify_checkcode(struct eap_aka_data *data,
49839beb93cSSam Leffler 				    const u8 *checkcode, size_t checkcode_len)
49939beb93cSSam Leffler {
50039beb93cSSam Leffler 	const u8 *addr;
50139beb93cSSam Leffler 	size_t len;
50239beb93cSSam Leffler 	u8 hash[SHA256_MAC_LEN];
50339beb93cSSam Leffler 	size_t hash_len;
50439beb93cSSam Leffler 
50539beb93cSSam Leffler 	if (checkcode == NULL)
50639beb93cSSam Leffler 		return -1;
50739beb93cSSam Leffler 
50839beb93cSSam Leffler 	if (data->id_msgs == NULL) {
50939beb93cSSam Leffler 		if (checkcode_len != 0) {
51039beb93cSSam Leffler 			wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server "
51139beb93cSSam Leffler 				   "indicates that AKA/Identity messages were "
51239beb93cSSam Leffler 				   "used, but they were not");
51339beb93cSSam Leffler 			return -1;
51439beb93cSSam Leffler 		}
51539beb93cSSam Leffler 		return 0;
51639beb93cSSam Leffler 	}
51739beb93cSSam Leffler 
51839beb93cSSam Leffler 	hash_len = data->eap_method == EAP_TYPE_AKA_PRIME ?
51939beb93cSSam Leffler 		EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN;
52039beb93cSSam Leffler 
52139beb93cSSam Leffler 	if (checkcode_len != hash_len) {
52239beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server "
52339beb93cSSam Leffler 			   "indicates that AKA/Identity message were not "
52439beb93cSSam Leffler 			   "used, but they were");
52539beb93cSSam Leffler 		return -1;
52639beb93cSSam Leffler 	}
52739beb93cSSam Leffler 
52839beb93cSSam Leffler 	/* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */
52939beb93cSSam Leffler 	addr = wpabuf_head(data->id_msgs);
53039beb93cSSam Leffler 	len = wpabuf_len(data->id_msgs);
53139beb93cSSam Leffler #ifdef EAP_AKA_PRIME
53239beb93cSSam Leffler 	if (data->eap_method == EAP_TYPE_AKA_PRIME)
53339beb93cSSam Leffler 		sha256_vector(1, &addr, &len, hash);
53439beb93cSSam Leffler 	else
53539beb93cSSam Leffler #endif /* EAP_AKA_PRIME */
53639beb93cSSam Leffler 		sha1_vector(1, &addr, &len, hash);
53739beb93cSSam Leffler 
5385b9c547cSRui Paulo 	if (os_memcmp_const(hash, checkcode, hash_len) != 0) {
53939beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "EAP-AKA: Mismatch in AT_CHECKCODE");
54039beb93cSSam Leffler 		return -1;
54139beb93cSSam Leffler 	}
54239beb93cSSam Leffler 
54339beb93cSSam Leffler 	return 0;
54439beb93cSSam Leffler }
54539beb93cSSam Leffler 
54639beb93cSSam Leffler 
54739beb93cSSam Leffler static struct wpabuf * eap_aka_client_error(struct eap_aka_data *data, u8 id,
54839beb93cSSam Leffler 					    int err)
54939beb93cSSam Leffler {
55039beb93cSSam Leffler 	struct eap_sim_msg *msg;
55139beb93cSSam Leffler 
55239beb93cSSam Leffler 	eap_aka_state(data, FAILURE);
55339beb93cSSam Leffler 	data->num_id_req = 0;
55439beb93cSSam Leffler 	data->num_notification = 0;
55539beb93cSSam Leffler 
556f05cddf9SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-AKA: Send Client-Error (error code %d)",
557f05cddf9SRui Paulo 		   err);
55839beb93cSSam Leffler 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
55939beb93cSSam Leffler 			       EAP_AKA_SUBTYPE_CLIENT_ERROR);
56039beb93cSSam Leffler 	eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
5615b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
56239beb93cSSam Leffler }
56339beb93cSSam Leffler 
56439beb93cSSam Leffler 
56539beb93cSSam Leffler static struct wpabuf * eap_aka_authentication_reject(struct eap_aka_data *data,
56639beb93cSSam Leffler 						     u8 id)
56739beb93cSSam Leffler {
56839beb93cSSam Leffler 	struct eap_sim_msg *msg;
56939beb93cSSam Leffler 
57039beb93cSSam Leffler 	eap_aka_state(data, FAILURE);
57139beb93cSSam Leffler 	data->num_id_req = 0;
57239beb93cSSam Leffler 	data->num_notification = 0;
57339beb93cSSam Leffler 
57439beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Authentication-Reject "
57539beb93cSSam Leffler 		   "(id=%d)", id);
57639beb93cSSam Leffler 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
57739beb93cSSam Leffler 			       EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT);
5785b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
57939beb93cSSam Leffler }
58039beb93cSSam Leffler 
58139beb93cSSam Leffler 
58239beb93cSSam Leffler static struct wpabuf * eap_aka_synchronization_failure(
583*85732ac8SCy Schubert 	struct eap_aka_data *data, u8 id, struct eap_sim_attrs *attr)
58439beb93cSSam Leffler {
58539beb93cSSam Leffler 	struct eap_sim_msg *msg;
58639beb93cSSam Leffler 
58739beb93cSSam Leffler 	data->num_id_req = 0;
58839beb93cSSam Leffler 	data->num_notification = 0;
58939beb93cSSam Leffler 
59039beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Synchronization-Failure "
59139beb93cSSam Leffler 		   "(id=%d)", id);
59239beb93cSSam Leffler 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
59339beb93cSSam Leffler 			       EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE);
59439beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   AT_AUTS");
59539beb93cSSam Leffler 	eap_sim_msg_add_full(msg, EAP_SIM_AT_AUTS, data->auts,
59639beb93cSSam Leffler 			     EAP_AKA_AUTS_LEN);
597*85732ac8SCy Schubert 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
598*85732ac8SCy Schubert 		size_t i;
599*85732ac8SCy Schubert 
600*85732ac8SCy Schubert 		for (i = 0; i < attr->kdf_count; i++) {
601*85732ac8SCy Schubert 			wpa_printf(MSG_DEBUG, "   AT_KDF");
602*85732ac8SCy Schubert 			eap_sim_msg_add(msg, EAP_SIM_AT_KDF, attr->kdf[i],
603*85732ac8SCy Schubert 					NULL, 0);
604*85732ac8SCy Schubert 		}
605*85732ac8SCy Schubert 	}
6065b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
60739beb93cSSam Leffler }
60839beb93cSSam Leffler 
60939beb93cSSam Leffler 
61039beb93cSSam Leffler static struct wpabuf * eap_aka_response_identity(struct eap_sm *sm,
61139beb93cSSam Leffler 						 struct eap_aka_data *data,
61239beb93cSSam Leffler 						 u8 id,
61339beb93cSSam Leffler 						 enum eap_sim_id_req id_req)
61439beb93cSSam Leffler {
61539beb93cSSam Leffler 	const u8 *identity = NULL;
61639beb93cSSam Leffler 	size_t identity_len = 0;
61739beb93cSSam Leffler 	struct eap_sim_msg *msg;
61839beb93cSSam Leffler 
61939beb93cSSam Leffler 	data->reauth = 0;
62039beb93cSSam Leffler 	if (id_req == ANY_ID && data->reauth_id) {
62139beb93cSSam Leffler 		identity = data->reauth_id;
62239beb93cSSam Leffler 		identity_len = data->reauth_id_len;
62339beb93cSSam Leffler 		data->reauth = 1;
62439beb93cSSam Leffler 	} else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
62539beb93cSSam Leffler 		   data->pseudonym) {
62639beb93cSSam Leffler 		identity = data->pseudonym;
62739beb93cSSam Leffler 		identity_len = data->pseudonym_len;
628f05cddf9SRui Paulo 		eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID);
62939beb93cSSam Leffler 	} else if (id_req != NO_ID_REQ) {
63039beb93cSSam Leffler 		identity = eap_get_config_identity(sm, &identity_len);
63139beb93cSSam Leffler 		if (identity) {
632f05cddf9SRui Paulo 			eap_aka_clear_identities(sm, data, CLEAR_PSEUDONYM |
63339beb93cSSam Leffler 						 CLEAR_REAUTH_ID);
63439beb93cSSam Leffler 		}
63539beb93cSSam Leffler 	}
63639beb93cSSam Leffler 	if (id_req != NO_ID_REQ)
637f05cddf9SRui Paulo 		eap_aka_clear_identities(sm, data, CLEAR_EAP_ID);
63839beb93cSSam Leffler 
63939beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Identity (id=%d)", id);
64039beb93cSSam Leffler 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
64139beb93cSSam Leffler 			       EAP_AKA_SUBTYPE_IDENTITY);
64239beb93cSSam Leffler 
64339beb93cSSam Leffler 	if (identity) {
64439beb93cSSam Leffler 		wpa_hexdump_ascii(MSG_DEBUG, "   AT_IDENTITY",
64539beb93cSSam Leffler 				  identity, identity_len);
64639beb93cSSam Leffler 		eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
64739beb93cSSam Leffler 				identity, identity_len);
64839beb93cSSam Leffler 	}
64939beb93cSSam Leffler 
6505b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
65139beb93cSSam Leffler }
65239beb93cSSam Leffler 
65339beb93cSSam Leffler 
65439beb93cSSam Leffler static struct wpabuf * eap_aka_response_challenge(struct eap_aka_data *data,
65539beb93cSSam Leffler 						  u8 id)
65639beb93cSSam Leffler {
65739beb93cSSam Leffler 	struct eap_sim_msg *msg;
65839beb93cSSam Leffler 
65939beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d)", id);
66039beb93cSSam Leffler 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
66139beb93cSSam Leffler 			       EAP_AKA_SUBTYPE_CHALLENGE);
66239beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   AT_RES");
66339beb93cSSam Leffler 	eap_sim_msg_add(msg, EAP_SIM_AT_RES, data->res_len * 8,
66439beb93cSSam Leffler 			data->res, data->res_len);
66539beb93cSSam Leffler 	eap_aka_add_checkcode(data, msg);
66639beb93cSSam Leffler 	if (data->use_result_ind) {
66739beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
66839beb93cSSam Leffler 		eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
66939beb93cSSam Leffler 	}
67039beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   AT_MAC");
67139beb93cSSam Leffler 	eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
6725b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, data->eap_method, data->k_aut, (u8 *) "",
6735b9c547cSRui Paulo 				  0);
67439beb93cSSam Leffler }
67539beb93cSSam Leffler 
67639beb93cSSam Leffler 
67739beb93cSSam Leffler static struct wpabuf * eap_aka_response_reauth(struct eap_aka_data *data,
67839beb93cSSam Leffler 					       u8 id, int counter_too_small,
67939beb93cSSam Leffler 					       const u8 *nonce_s)
68039beb93cSSam Leffler {
68139beb93cSSam Leffler 	struct eap_sim_msg *msg;
68239beb93cSSam Leffler 	unsigned int counter;
68339beb93cSSam Leffler 
68439beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Reauthentication (id=%d)",
68539beb93cSSam Leffler 		   id);
68639beb93cSSam Leffler 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
68739beb93cSSam Leffler 			       EAP_AKA_SUBTYPE_REAUTHENTICATION);
68839beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   AT_IV");
68939beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
69039beb93cSSam Leffler 	eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
69139beb93cSSam Leffler 
69239beb93cSSam Leffler 	if (counter_too_small) {
69339beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   *AT_COUNTER_TOO_SMALL");
69439beb93cSSam Leffler 		eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
69539beb93cSSam Leffler 		counter = data->counter_too_small;
69639beb93cSSam Leffler 	} else
69739beb93cSSam Leffler 		counter = data->counter;
69839beb93cSSam Leffler 
69939beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   *AT_COUNTER %d", counter);
70039beb93cSSam Leffler 	eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
70139beb93cSSam Leffler 
70239beb93cSSam Leffler 	if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
70339beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
70439beb93cSSam Leffler 			   "AT_ENCR_DATA");
70539beb93cSSam Leffler 		eap_sim_msg_free(msg);
70639beb93cSSam Leffler 		return NULL;
70739beb93cSSam Leffler 	}
70839beb93cSSam Leffler 	eap_aka_add_checkcode(data, msg);
70939beb93cSSam Leffler 	if (data->use_result_ind) {
71039beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
71139beb93cSSam Leffler 		eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
71239beb93cSSam Leffler 	}
71339beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   AT_MAC");
71439beb93cSSam Leffler 	eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
7155b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, data->eap_method, data->k_aut, nonce_s,
71639beb93cSSam Leffler 				  EAP_SIM_NONCE_S_LEN);
71739beb93cSSam Leffler }
71839beb93cSSam Leffler 
71939beb93cSSam Leffler 
72039beb93cSSam Leffler static struct wpabuf * eap_aka_response_notification(struct eap_aka_data *data,
72139beb93cSSam Leffler 						     u8 id, u16 notification)
72239beb93cSSam Leffler {
72339beb93cSSam Leffler 	struct eap_sim_msg *msg;
72439beb93cSSam Leffler 	u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
72539beb93cSSam Leffler 
72639beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Notification (id=%d)", id);
72739beb93cSSam Leffler 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
72839beb93cSSam Leffler 			       EAP_AKA_SUBTYPE_NOTIFICATION);
72939beb93cSSam Leffler 	if (k_aut && data->reauth) {
73039beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   AT_IV");
73139beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
73239beb93cSSam Leffler 		eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
73339beb93cSSam Leffler 					   EAP_SIM_AT_ENCR_DATA);
73439beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   *AT_COUNTER %d", data->counter);
73539beb93cSSam Leffler 		eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
73639beb93cSSam Leffler 				NULL, 0);
73739beb93cSSam Leffler 		if (eap_sim_msg_add_encr_end(msg, data->k_encr,
73839beb93cSSam Leffler 					     EAP_SIM_AT_PADDING)) {
73939beb93cSSam Leffler 			wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt "
74039beb93cSSam Leffler 				   "AT_ENCR_DATA");
74139beb93cSSam Leffler 			eap_sim_msg_free(msg);
74239beb93cSSam Leffler 			return NULL;
74339beb93cSSam Leffler 		}
74439beb93cSSam Leffler 	}
74539beb93cSSam Leffler 	if (k_aut) {
74639beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "   AT_MAC");
74739beb93cSSam Leffler 		eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
74839beb93cSSam Leffler 	}
7495b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, data->eap_method, k_aut, (u8 *) "", 0);
75039beb93cSSam Leffler }
75139beb93cSSam Leffler 
75239beb93cSSam Leffler 
75339beb93cSSam Leffler static struct wpabuf * eap_aka_process_identity(struct eap_sm *sm,
75439beb93cSSam Leffler 						struct eap_aka_data *data,
75539beb93cSSam Leffler 						u8 id,
75639beb93cSSam Leffler 						const struct wpabuf *reqData,
75739beb93cSSam Leffler 						struct eap_sim_attrs *attr)
75839beb93cSSam Leffler {
75939beb93cSSam Leffler 	int id_error;
76039beb93cSSam Leffler 	struct wpabuf *buf;
76139beb93cSSam Leffler 
76239beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Identity");
76339beb93cSSam Leffler 
76439beb93cSSam Leffler 	id_error = 0;
76539beb93cSSam Leffler 	switch (attr->id_req) {
76639beb93cSSam Leffler 	case NO_ID_REQ:
76739beb93cSSam Leffler 		break;
76839beb93cSSam Leffler 	case ANY_ID:
76939beb93cSSam Leffler 		if (data->num_id_req > 0)
77039beb93cSSam Leffler 			id_error++;
77139beb93cSSam Leffler 		data->num_id_req++;
77239beb93cSSam Leffler 		break;
77339beb93cSSam Leffler 	case FULLAUTH_ID:
77439beb93cSSam Leffler 		if (data->num_id_req > 1)
77539beb93cSSam Leffler 			id_error++;
77639beb93cSSam Leffler 		data->num_id_req++;
77739beb93cSSam Leffler 		break;
77839beb93cSSam Leffler 	case PERMANENT_ID:
77939beb93cSSam Leffler 		if (data->num_id_req > 2)
78039beb93cSSam Leffler 			id_error++;
78139beb93cSSam Leffler 		data->num_id_req++;
78239beb93cSSam Leffler 		break;
78339beb93cSSam Leffler 	}
78439beb93cSSam Leffler 	if (id_error) {
78539beb93cSSam Leffler 		wpa_printf(MSG_INFO, "EAP-AKA: Too many ID requests "
78639beb93cSSam Leffler 			   "used within one authentication");
78739beb93cSSam Leffler 		return eap_aka_client_error(data, id,
78839beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
78939beb93cSSam Leffler 	}
79039beb93cSSam Leffler 
79139beb93cSSam Leffler 	buf = eap_aka_response_identity(sm, data, id, attr->id_req);
79239beb93cSSam Leffler 
79339beb93cSSam Leffler 	if (data->prev_id != id) {
79439beb93cSSam Leffler 		eap_aka_add_id_msg(data, reqData);
79539beb93cSSam Leffler 		eap_aka_add_id_msg(data, buf);
79639beb93cSSam Leffler 		data->prev_id = id;
79739beb93cSSam Leffler 	}
79839beb93cSSam Leffler 
79939beb93cSSam Leffler 	return buf;
80039beb93cSSam Leffler }
80139beb93cSSam Leffler 
80239beb93cSSam Leffler 
80339beb93cSSam Leffler static int eap_aka_verify_mac(struct eap_aka_data *data,
80439beb93cSSam Leffler 			      const struct wpabuf *req,
80539beb93cSSam Leffler 			      const u8 *mac, const u8 *extra,
80639beb93cSSam Leffler 			      size_t extra_len)
80739beb93cSSam Leffler {
80839beb93cSSam Leffler 	if (data->eap_method == EAP_TYPE_AKA_PRIME)
80939beb93cSSam Leffler 		return eap_sim_verify_mac_sha256(data->k_aut, req, mac, extra,
81039beb93cSSam Leffler 						 extra_len);
81139beb93cSSam Leffler 	return eap_sim_verify_mac(data->k_aut, req, mac, extra, extra_len);
81239beb93cSSam Leffler }
81339beb93cSSam Leffler 
81439beb93cSSam Leffler 
81539beb93cSSam Leffler #ifdef EAP_AKA_PRIME
81639beb93cSSam Leffler static struct wpabuf * eap_aka_prime_kdf_select(struct eap_aka_data *data,
81739beb93cSSam Leffler 						u8 id, u16 kdf)
81839beb93cSSam Leffler {
81939beb93cSSam Leffler 	struct eap_sim_msg *msg;
82039beb93cSSam Leffler 
82139beb93cSSam Leffler 	data->kdf_negotiation = 1;
82239beb93cSSam Leffler 	data->kdf = kdf;
82339beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d) (KDF "
82439beb93cSSam Leffler 		   "select)", id);
82539beb93cSSam Leffler 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method,
82639beb93cSSam Leffler 			       EAP_AKA_SUBTYPE_CHALLENGE);
82739beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "   AT_KDF");
82839beb93cSSam Leffler 	eap_sim_msg_add(msg, EAP_SIM_AT_KDF, kdf, NULL, 0);
8295b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0);
83039beb93cSSam Leffler }
83139beb93cSSam Leffler 
83239beb93cSSam Leffler 
83339beb93cSSam Leffler static struct wpabuf * eap_aka_prime_kdf_neg(struct eap_aka_data *data,
83439beb93cSSam Leffler 					     u8 id, struct eap_sim_attrs *attr)
83539beb93cSSam Leffler {
83639beb93cSSam Leffler 	size_t i;
83739beb93cSSam Leffler 
83839beb93cSSam Leffler 	for (i = 0; i < attr->kdf_count; i++) {
839*85732ac8SCy Schubert 		if (attr->kdf[i] == EAP_AKA_PRIME_KDF) {
840*85732ac8SCy Schubert 			os_memcpy(data->last_kdf_attrs, attr->kdf,
841*85732ac8SCy Schubert 				  sizeof(u16) * attr->kdf_count);
842*85732ac8SCy Schubert 			data->last_kdf_count = attr->kdf_count;
84339beb93cSSam Leffler 			return eap_aka_prime_kdf_select(data, id,
84439beb93cSSam Leffler 							EAP_AKA_PRIME_KDF);
84539beb93cSSam Leffler 		}
846*85732ac8SCy Schubert 	}
84739beb93cSSam Leffler 
84839beb93cSSam Leffler 	/* No matching KDF found - fail authentication as if AUTN had been
84939beb93cSSam Leffler 	 * incorrect */
85039beb93cSSam Leffler 	return eap_aka_authentication_reject(data, id);
85139beb93cSSam Leffler }
85239beb93cSSam Leffler 
85339beb93cSSam Leffler 
85439beb93cSSam Leffler static int eap_aka_prime_kdf_valid(struct eap_aka_data *data,
85539beb93cSSam Leffler 				   struct eap_sim_attrs *attr)
85639beb93cSSam Leffler {
85739beb93cSSam Leffler 	size_t i, j;
85839beb93cSSam Leffler 
85939beb93cSSam Leffler 	if (attr->kdf_count == 0)
86039beb93cSSam Leffler 		return 0;
86139beb93cSSam Leffler 
86239beb93cSSam Leffler 	/* The only allowed (and required) duplication of a KDF is the addition
86339beb93cSSam Leffler 	 * of the selected KDF into the beginning of the list. */
86439beb93cSSam Leffler 
86539beb93cSSam Leffler 	if (data->kdf_negotiation) {
866*85732ac8SCy Schubert 		/* When the peer receives the new EAP-Request/AKA'-Challenge
867*85732ac8SCy Schubert 		 * message, must check only requested change occurred in the
868*85732ac8SCy Schubert 		 * list of AT_KDF attributes. If there are any other changes,
869*85732ac8SCy Schubert 		 * the peer must behave like the case that AT_MAC had been
870*85732ac8SCy Schubert 		 * incorrect and authentication is failed. These are defined in
871*85732ac8SCy Schubert 		 * EAP-AKA' specification RFC 5448, Section 3.2. */
87239beb93cSSam Leffler 		if (attr->kdf[0] != data->kdf) {
87339beb93cSSam Leffler 			wpa_printf(MSG_WARNING, "EAP-AKA': The server did not "
87439beb93cSSam Leffler 				   "accept the selected KDF");
875*85732ac8SCy Schubert 			return -1;
876*85732ac8SCy Schubert 		}
877*85732ac8SCy Schubert 
878*85732ac8SCy Schubert 		if (attr->kdf_count > EAP_AKA_PRIME_KDF_MAX ||
879*85732ac8SCy Schubert 		    attr->kdf_count != data->last_kdf_count + 1) {
880*85732ac8SCy Schubert 			wpa_printf(MSG_WARNING,
881*85732ac8SCy Schubert 				   "EAP-AKA': The length of KDF attributes is wrong");
882*85732ac8SCy Schubert 			return -1;
88339beb93cSSam Leffler 		}
88439beb93cSSam Leffler 
88539beb93cSSam Leffler 		for (i = 1; i < attr->kdf_count; i++) {
886*85732ac8SCy Schubert 			if (attr->kdf[i] != data->last_kdf_attrs[i - 1]) {
887*85732ac8SCy Schubert 				wpa_printf(MSG_WARNING,
888*85732ac8SCy Schubert 					   "EAP-AKA': The KDF attributes except selected KDF are not same as original one");
889*85732ac8SCy Schubert 				return -1;
89039beb93cSSam Leffler 			}
89139beb93cSSam Leffler 		}
89239beb93cSSam Leffler 	}
89339beb93cSSam Leffler 
89439beb93cSSam Leffler 	for (i = data->kdf ? 1 : 0; i < attr->kdf_count; i++) {
89539beb93cSSam Leffler 		for (j = i + 1; j < attr->kdf_count; j++) {
89639beb93cSSam Leffler 			if (attr->kdf[i] == attr->kdf[j]) {
89739beb93cSSam Leffler 				wpa_printf(MSG_WARNING, "EAP-AKA': The server "
89839beb93cSSam Leffler 					   "included a duplicated KDF");
89939beb93cSSam Leffler 				return 0;
90039beb93cSSam Leffler 			}
90139beb93cSSam Leffler 		}
90239beb93cSSam Leffler 	}
90339beb93cSSam Leffler 
90439beb93cSSam Leffler 	return 1;
90539beb93cSSam Leffler }
90639beb93cSSam Leffler #endif /* EAP_AKA_PRIME */
90739beb93cSSam Leffler 
90839beb93cSSam Leffler 
90939beb93cSSam Leffler static struct wpabuf * eap_aka_process_challenge(struct eap_sm *sm,
91039beb93cSSam Leffler 						 struct eap_aka_data *data,
91139beb93cSSam Leffler 						 u8 id,
91239beb93cSSam Leffler 						 const struct wpabuf *reqData,
91339beb93cSSam Leffler 						 struct eap_sim_attrs *attr)
91439beb93cSSam Leffler {
91539beb93cSSam Leffler 	const u8 *identity;
91639beb93cSSam Leffler 	size_t identity_len;
91739beb93cSSam Leffler 	int res;
91839beb93cSSam Leffler 	struct eap_sim_attrs eattr;
91939beb93cSSam Leffler 
92039beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Challenge");
92139beb93cSSam Leffler 
92239beb93cSSam Leffler 	if (attr->checkcode &&
92339beb93cSSam Leffler 	    eap_aka_verify_checkcode(data, attr->checkcode,
92439beb93cSSam Leffler 				     attr->checkcode_len)) {
92539beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the "
92639beb93cSSam Leffler 			   "message");
92739beb93cSSam Leffler 		return eap_aka_client_error(data, id,
92839beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
92939beb93cSSam Leffler 	}
93039beb93cSSam Leffler 
93139beb93cSSam Leffler #ifdef EAP_AKA_PRIME
93239beb93cSSam Leffler 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
93339beb93cSSam Leffler 		if (!attr->kdf_input || attr->kdf_input_len == 0) {
93439beb93cSSam Leffler 			wpa_printf(MSG_WARNING, "EAP-AKA': Challenge message "
93539beb93cSSam Leffler 				   "did not include non-empty AT_KDF_INPUT");
93639beb93cSSam Leffler 			/* Fail authentication as if AUTN had been incorrect */
93739beb93cSSam Leffler 			return eap_aka_authentication_reject(data, id);
93839beb93cSSam Leffler 		}
93939beb93cSSam Leffler 		os_free(data->network_name);
940*85732ac8SCy Schubert 		data->network_name = os_memdup(attr->kdf_input,
941*85732ac8SCy Schubert 					       attr->kdf_input_len);
94239beb93cSSam Leffler 		if (data->network_name == NULL) {
94339beb93cSSam Leffler 			wpa_printf(MSG_WARNING, "EAP-AKA': No memory for "
94439beb93cSSam Leffler 				   "storing Network Name");
94539beb93cSSam Leffler 			return eap_aka_authentication_reject(data, id);
94639beb93cSSam Leffler 		}
94739beb93cSSam Leffler 		data->network_name_len = attr->kdf_input_len;
94839beb93cSSam Leffler 		wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA': Network Name "
94939beb93cSSam Leffler 				  "(AT_KDF_INPUT)",
95039beb93cSSam Leffler 				  data->network_name, data->network_name_len);
95139beb93cSSam Leffler 		/* TODO: check Network Name per 3GPP.33.402 */
95239beb93cSSam Leffler 
953*85732ac8SCy Schubert 		res = eap_aka_prime_kdf_valid(data, attr);
954*85732ac8SCy Schubert 		if (res == 0)
95539beb93cSSam Leffler 			return eap_aka_authentication_reject(data, id);
956*85732ac8SCy Schubert 		else if (res == -1)
957*85732ac8SCy Schubert 			return eap_aka_client_error(
958*85732ac8SCy Schubert 				data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET);
95939beb93cSSam Leffler 
96039beb93cSSam Leffler 		if (attr->kdf[0] != EAP_AKA_PRIME_KDF)
96139beb93cSSam Leffler 			return eap_aka_prime_kdf_neg(data, id, attr);
96239beb93cSSam Leffler 
96339beb93cSSam Leffler 		data->kdf = EAP_AKA_PRIME_KDF;
96439beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "EAP-AKA': KDF %d selected", data->kdf);
96539beb93cSSam Leffler 	}
96639beb93cSSam Leffler 
96739beb93cSSam Leffler 	if (data->eap_method == EAP_TYPE_AKA && attr->bidding) {
96839beb93cSSam Leffler 		u16 flags = WPA_GET_BE16(attr->bidding);
96939beb93cSSam Leffler 		if ((flags & EAP_AKA_BIDDING_FLAG_D) &&
97039beb93cSSam Leffler 		    eap_allowed_method(sm, EAP_VENDOR_IETF,
97139beb93cSSam Leffler 				       EAP_TYPE_AKA_PRIME)) {
97239beb93cSSam Leffler 			wpa_printf(MSG_WARNING, "EAP-AKA: Bidding down from "
97339beb93cSSam Leffler 				   "AKA' to AKA detected");
97439beb93cSSam Leffler 			/* Fail authentication as if AUTN had been incorrect */
97539beb93cSSam Leffler 			return eap_aka_authentication_reject(data, id);
97639beb93cSSam Leffler 		}
97739beb93cSSam Leffler 	}
97839beb93cSSam Leffler #endif /* EAP_AKA_PRIME */
97939beb93cSSam Leffler 
98039beb93cSSam Leffler 	data->reauth = 0;
98139beb93cSSam Leffler 	if (!attr->mac || !attr->rand || !attr->autn) {
98239beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
98339beb93cSSam Leffler 			   "did not include%s%s%s",
98439beb93cSSam Leffler 			   !attr->mac ? " AT_MAC" : "",
98539beb93cSSam Leffler 			   !attr->rand ? " AT_RAND" : "",
98639beb93cSSam Leffler 			   !attr->autn ? " AT_AUTN" : "");
98739beb93cSSam Leffler 		return eap_aka_client_error(data, id,
98839beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
98939beb93cSSam Leffler 	}
99039beb93cSSam Leffler 	os_memcpy(data->rand, attr->rand, EAP_AKA_RAND_LEN);
99139beb93cSSam Leffler 	os_memcpy(data->autn, attr->autn, EAP_AKA_AUTN_LEN);
99239beb93cSSam Leffler 
99339beb93cSSam Leffler 	res = eap_aka_umts_auth(sm, data);
99439beb93cSSam Leffler 	if (res == -1) {
99539beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication "
99639beb93cSSam Leffler 			   "failed (AUTN)");
99739beb93cSSam Leffler 		return eap_aka_authentication_reject(data, id);
99839beb93cSSam Leffler 	} else if (res == -2) {
99939beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication "
100039beb93cSSam Leffler 			   "failed (AUTN seq# -> AUTS)");
1001*85732ac8SCy Schubert 		return eap_aka_synchronization_failure(data, id, attr);
10025b9c547cSRui Paulo 	} else if (res > 0) {
10035b9c547cSRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-AKA: Wait for external USIM processing");
10045b9c547cSRui Paulo 		return NULL;
100539beb93cSSam Leffler 	} else if (res) {
100639beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication failed");
100739beb93cSSam Leffler 		return eap_aka_client_error(data, id,
100839beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
100939beb93cSSam Leffler 	}
101039beb93cSSam Leffler #ifdef EAP_AKA_PRIME
101139beb93cSSam Leffler 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
101239beb93cSSam Leffler 		/* Note: AUTN = (SQN ^ AK) || AMF || MAC which gives us the
101339beb93cSSam Leffler 		 * needed 6-octet SQN ^ AK for CK',IK' derivation */
101439beb93cSSam Leffler 		u16 amf = WPA_GET_BE16(data->autn + 6);
101539beb93cSSam Leffler 		if (!(amf & 0x8000)) {
101639beb93cSSam Leffler 			wpa_printf(MSG_WARNING, "EAP-AKA': AMF separation bit "
101739beb93cSSam Leffler 				   "not set (AMF=0x%4x)", amf);
101839beb93cSSam Leffler 			return eap_aka_authentication_reject(data, id);
101939beb93cSSam Leffler 		}
102039beb93cSSam Leffler 		eap_aka_prime_derive_ck_ik_prime(data->ck, data->ik,
102139beb93cSSam Leffler 						 data->autn,
102239beb93cSSam Leffler 						 data->network_name,
102339beb93cSSam Leffler 						 data->network_name_len);
102439beb93cSSam Leffler 	}
102539beb93cSSam Leffler #endif /* EAP_AKA_PRIME */
102639beb93cSSam Leffler 	if (data->last_eap_identity) {
102739beb93cSSam Leffler 		identity = data->last_eap_identity;
102839beb93cSSam Leffler 		identity_len = data->last_eap_identity_len;
102939beb93cSSam Leffler 	} else if (data->pseudonym) {
103039beb93cSSam Leffler 		identity = data->pseudonym;
103139beb93cSSam Leffler 		identity_len = data->pseudonym_len;
1032*85732ac8SCy Schubert 	} else {
1033*85732ac8SCy Schubert 		struct eap_peer_config *config;
1034*85732ac8SCy Schubert 
1035*85732ac8SCy Schubert 		config = eap_get_config(sm);
1036*85732ac8SCy Schubert 		if (config && config->imsi_identity) {
1037*85732ac8SCy Schubert 			identity = config->imsi_identity;
1038*85732ac8SCy Schubert 			identity_len = config->imsi_identity_len;
1039*85732ac8SCy Schubert 		} else {
104039beb93cSSam Leffler 			identity = eap_get_config_identity(sm, &identity_len);
1041*85732ac8SCy Schubert 		}
1042*85732ac8SCy Schubert 	}
104339beb93cSSam Leffler 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Selected identity for MK "
104439beb93cSSam Leffler 			  "derivation", identity, identity_len);
104539beb93cSSam Leffler 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
104639beb93cSSam Leffler 		eap_aka_prime_derive_keys(identity, identity_len, data->ik,
104739beb93cSSam Leffler 					  data->ck, data->k_encr, data->k_aut,
104839beb93cSSam Leffler 					  data->k_re, data->msk, data->emsk);
104939beb93cSSam Leffler 	} else {
105039beb93cSSam Leffler 		eap_aka_derive_mk(identity, identity_len, data->ik, data->ck,
105139beb93cSSam Leffler 				  data->mk);
105239beb93cSSam Leffler 		eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut,
105339beb93cSSam Leffler 				    data->msk, data->emsk);
105439beb93cSSam Leffler 	}
105539beb93cSSam Leffler 	if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
105639beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message "
105739beb93cSSam Leffler 			   "used invalid AT_MAC");
105839beb93cSSam Leffler 		return eap_aka_client_error(data, id,
105939beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
106039beb93cSSam Leffler 	}
106139beb93cSSam Leffler 
1062f05cddf9SRui Paulo 	/* Old reauthentication identity must not be used anymore. In
1063f05cddf9SRui Paulo 	 * other words, if no new identities are received, full
1064f05cddf9SRui Paulo 	 * authentication will be used on next reauthentication (using
1065f05cddf9SRui Paulo 	 * pseudonym identity or permanent identity). */
1066f05cddf9SRui Paulo 	eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
106739beb93cSSam Leffler 
106839beb93cSSam Leffler 	if (attr->encr_data) {
106939beb93cSSam Leffler 		u8 *decrypted;
107039beb93cSSam Leffler 		decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
107139beb93cSSam Leffler 					       attr->encr_data_len, attr->iv,
107239beb93cSSam Leffler 					       &eattr, 0);
107339beb93cSSam Leffler 		if (decrypted == NULL) {
107439beb93cSSam Leffler 			return eap_aka_client_error(
107539beb93cSSam Leffler 				data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET);
107639beb93cSSam Leffler 		}
1077f05cddf9SRui Paulo 		eap_aka_learn_ids(sm, data, &eattr);
107839beb93cSSam Leffler 		os_free(decrypted);
107939beb93cSSam Leffler 	}
108039beb93cSSam Leffler 
108139beb93cSSam Leffler 	if (data->result_ind && attr->result_ind)
108239beb93cSSam Leffler 		data->use_result_ind = 1;
108339beb93cSSam Leffler 
10845b9c547cSRui Paulo 	if (data->state != FAILURE) {
108539beb93cSSam Leffler 		eap_aka_state(data, data->use_result_ind ?
108639beb93cSSam Leffler 			      RESULT_SUCCESS : SUCCESS);
108739beb93cSSam Leffler 	}
108839beb93cSSam Leffler 
108939beb93cSSam Leffler 	data->num_id_req = 0;
109039beb93cSSam Leffler 	data->num_notification = 0;
109139beb93cSSam Leffler 	/* RFC 4187 specifies that counter is initialized to one after
109239beb93cSSam Leffler 	 * fullauth, but initializing it to zero makes it easier to implement
109339beb93cSSam Leffler 	 * reauth verification. */
109439beb93cSSam Leffler 	data->counter = 0;
109539beb93cSSam Leffler 	return eap_aka_response_challenge(data, id);
109639beb93cSSam Leffler }
109739beb93cSSam Leffler 
109839beb93cSSam Leffler 
109939beb93cSSam Leffler static int eap_aka_process_notification_reauth(struct eap_aka_data *data,
110039beb93cSSam Leffler 					       struct eap_sim_attrs *attr)
110139beb93cSSam Leffler {
110239beb93cSSam Leffler 	struct eap_sim_attrs eattr;
110339beb93cSSam Leffler 	u8 *decrypted;
110439beb93cSSam Leffler 
110539beb93cSSam Leffler 	if (attr->encr_data == NULL || attr->iv == NULL) {
110639beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Notification message after "
110739beb93cSSam Leffler 			   "reauth did not include encrypted data");
110839beb93cSSam Leffler 		return -1;
110939beb93cSSam Leffler 	}
111039beb93cSSam Leffler 
111139beb93cSSam Leffler 	decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
111239beb93cSSam Leffler 				       attr->encr_data_len, attr->iv, &eattr,
111339beb93cSSam Leffler 				       0);
111439beb93cSSam Leffler 	if (decrypted == NULL) {
111539beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
111639beb93cSSam Leffler 			   "data from notification message");
111739beb93cSSam Leffler 		return -1;
111839beb93cSSam Leffler 	}
111939beb93cSSam Leffler 
112039beb93cSSam Leffler 	if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
112139beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Counter in notification "
112239beb93cSSam Leffler 			   "message does not match with counter in reauth "
112339beb93cSSam Leffler 			   "message");
112439beb93cSSam Leffler 		os_free(decrypted);
112539beb93cSSam Leffler 		return -1;
112639beb93cSSam Leffler 	}
112739beb93cSSam Leffler 
112839beb93cSSam Leffler 	os_free(decrypted);
112939beb93cSSam Leffler 	return 0;
113039beb93cSSam Leffler }
113139beb93cSSam Leffler 
113239beb93cSSam Leffler 
113339beb93cSSam Leffler static int eap_aka_process_notification_auth(struct eap_aka_data *data,
113439beb93cSSam Leffler 					     const struct wpabuf *reqData,
113539beb93cSSam Leffler 					     struct eap_sim_attrs *attr)
113639beb93cSSam Leffler {
113739beb93cSSam Leffler 	if (attr->mac == NULL) {
113839beb93cSSam Leffler 		wpa_printf(MSG_INFO, "EAP-AKA: no AT_MAC in after_auth "
113939beb93cSSam Leffler 			   "Notification message");
114039beb93cSSam Leffler 		return -1;
114139beb93cSSam Leffler 	}
114239beb93cSSam Leffler 
114339beb93cSSam Leffler 	if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
114439beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Notification message "
114539beb93cSSam Leffler 			   "used invalid AT_MAC");
114639beb93cSSam Leffler 		return -1;
114739beb93cSSam Leffler 	}
114839beb93cSSam Leffler 
114939beb93cSSam Leffler 	if (data->reauth &&
115039beb93cSSam Leffler 	    eap_aka_process_notification_reauth(data, attr)) {
115139beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Invalid notification "
115239beb93cSSam Leffler 			   "message after reauth");
115339beb93cSSam Leffler 		return -1;
115439beb93cSSam Leffler 	}
115539beb93cSSam Leffler 
115639beb93cSSam Leffler 	return 0;
115739beb93cSSam Leffler }
115839beb93cSSam Leffler 
115939beb93cSSam Leffler 
116039beb93cSSam Leffler static struct wpabuf * eap_aka_process_notification(
116139beb93cSSam Leffler 	struct eap_sm *sm, struct eap_aka_data *data, u8 id,
116239beb93cSSam Leffler 	const struct wpabuf *reqData, struct eap_sim_attrs *attr)
116339beb93cSSam Leffler {
116439beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Notification");
116539beb93cSSam Leffler 	if (data->num_notification > 0) {
116639beb93cSSam Leffler 		wpa_printf(MSG_INFO, "EAP-AKA: too many notification "
116739beb93cSSam Leffler 			   "rounds (only one allowed)");
116839beb93cSSam Leffler 		return eap_aka_client_error(data, id,
116939beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
117039beb93cSSam Leffler 	}
117139beb93cSSam Leffler 	data->num_notification++;
117239beb93cSSam Leffler 	if (attr->notification == -1) {
117339beb93cSSam Leffler 		wpa_printf(MSG_INFO, "EAP-AKA: no AT_NOTIFICATION in "
117439beb93cSSam Leffler 			   "Notification message");
117539beb93cSSam Leffler 		return eap_aka_client_error(data, id,
117639beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
117739beb93cSSam Leffler 	}
117839beb93cSSam Leffler 
117939beb93cSSam Leffler 	if ((attr->notification & 0x4000) == 0 &&
118039beb93cSSam Leffler 	    eap_aka_process_notification_auth(data, reqData, attr)) {
118139beb93cSSam Leffler 		return eap_aka_client_error(data, id,
118239beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
118339beb93cSSam Leffler 	}
118439beb93cSSam Leffler 
118539beb93cSSam Leffler 	eap_sim_report_notification(sm->msg_ctx, attr->notification, 1);
118639beb93cSSam Leffler 	if (attr->notification >= 0 && attr->notification < 32768) {
1187*85732ac8SCy Schubert 		data->error_code = attr->notification;
118839beb93cSSam Leffler 		eap_aka_state(data, FAILURE);
118939beb93cSSam Leffler 	} else if (attr->notification == EAP_SIM_SUCCESS &&
119039beb93cSSam Leffler 		   data->state == RESULT_SUCCESS)
119139beb93cSSam Leffler 		eap_aka_state(data, SUCCESS);
119239beb93cSSam Leffler 	return eap_aka_response_notification(data, id, attr->notification);
119339beb93cSSam Leffler }
119439beb93cSSam Leffler 
119539beb93cSSam Leffler 
119639beb93cSSam Leffler static struct wpabuf * eap_aka_process_reauthentication(
119739beb93cSSam Leffler 	struct eap_sm *sm, struct eap_aka_data *data, u8 id,
119839beb93cSSam Leffler 	const struct wpabuf *reqData, struct eap_sim_attrs *attr)
119939beb93cSSam Leffler {
120039beb93cSSam Leffler 	struct eap_sim_attrs eattr;
120139beb93cSSam Leffler 	u8 *decrypted;
120239beb93cSSam Leffler 
120339beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Reauthentication");
120439beb93cSSam Leffler 
120539beb93cSSam Leffler 	if (attr->checkcode &&
120639beb93cSSam Leffler 	    eap_aka_verify_checkcode(data, attr->checkcode,
120739beb93cSSam Leffler 				     attr->checkcode_len)) {
120839beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the "
120939beb93cSSam Leffler 			   "message");
121039beb93cSSam Leffler 		return eap_aka_client_error(data, id,
121139beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
121239beb93cSSam Leffler 	}
121339beb93cSSam Leffler 
121439beb93cSSam Leffler 	if (data->reauth_id == NULL) {
121539beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Server is trying "
121639beb93cSSam Leffler 			   "reauthentication, but no reauth_id available");
121739beb93cSSam Leffler 		return eap_aka_client_error(data, id,
121839beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
121939beb93cSSam Leffler 	}
122039beb93cSSam Leffler 
122139beb93cSSam Leffler 	data->reauth = 1;
122239beb93cSSam Leffler 	if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) {
122339beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
122439beb93cSSam Leffler 			   "did not have valid AT_MAC");
122539beb93cSSam Leffler 		return eap_aka_client_error(data, id,
122639beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
122739beb93cSSam Leffler 	}
122839beb93cSSam Leffler 
122939beb93cSSam Leffler 	if (attr->encr_data == NULL || attr->iv == NULL) {
123039beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication "
123139beb93cSSam Leffler 			   "message did not include encrypted data");
123239beb93cSSam Leffler 		return eap_aka_client_error(data, id,
123339beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
123439beb93cSSam Leffler 	}
123539beb93cSSam Leffler 
123639beb93cSSam Leffler 	decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
123739beb93cSSam Leffler 				       attr->encr_data_len, attr->iv, &eattr,
123839beb93cSSam Leffler 				       0);
123939beb93cSSam Leffler 	if (decrypted == NULL) {
124039beb93cSSam Leffler 		wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted "
124139beb93cSSam Leffler 			   "data from reauthentication message");
124239beb93cSSam Leffler 		return eap_aka_client_error(data, id,
124339beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
124439beb93cSSam Leffler 	}
124539beb93cSSam Leffler 
124639beb93cSSam Leffler 	if (eattr.nonce_s == NULL || eattr.counter < 0) {
124739beb93cSSam Leffler 		wpa_printf(MSG_INFO, "EAP-AKA: (encr) No%s%s in reauth packet",
124839beb93cSSam Leffler 			   !eattr.nonce_s ? " AT_NONCE_S" : "",
124939beb93cSSam Leffler 			   eattr.counter < 0 ? " AT_COUNTER" : "");
125039beb93cSSam Leffler 		os_free(decrypted);
125139beb93cSSam Leffler 		return eap_aka_client_error(data, id,
125239beb93cSSam Leffler 					    EAP_AKA_UNABLE_TO_PROCESS_PACKET);
125339beb93cSSam Leffler 	}
125439beb93cSSam Leffler 
125539beb93cSSam Leffler 	if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
125639beb93cSSam Leffler 		struct wpabuf *res;
125739beb93cSSam Leffler 		wpa_printf(MSG_INFO, "EAP-AKA: (encr) Invalid counter "
125839beb93cSSam Leffler 			   "(%d <= %d)", eattr.counter, data->counter);
125939beb93cSSam Leffler 		data->counter_too_small = eattr.counter;
126039beb93cSSam Leffler 
126139beb93cSSam Leffler 		/* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
126239beb93cSSam Leffler 		 * reauth_id must not be used to start a new reauthentication.
126339beb93cSSam Leffler 		 * However, since it was used in the last EAP-Response-Identity
126439beb93cSSam Leffler 		 * packet, it has to saved for the following fullauth to be
126539beb93cSSam Leffler 		 * used in MK derivation. */
126639beb93cSSam Leffler 		os_free(data->last_eap_identity);
126739beb93cSSam Leffler 		data->last_eap_identity = data->reauth_id;
126839beb93cSSam Leffler 		data->last_eap_identity_len = data->reauth_id_len;
126939beb93cSSam Leffler 		data->reauth_id = NULL;
127039beb93cSSam Leffler 		data->reauth_id_len = 0;
127139beb93cSSam Leffler 
127239beb93cSSam Leffler 		res = eap_aka_response_reauth(data, id, 1, eattr.nonce_s);
127339beb93cSSam Leffler 		os_free(decrypted);
127439beb93cSSam Leffler 
127539beb93cSSam Leffler 		return res;
127639beb93cSSam Leffler 	}
127739beb93cSSam Leffler 	data->counter = eattr.counter;
127839beb93cSSam Leffler 
127939beb93cSSam Leffler 	os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
128039beb93cSSam Leffler 	wpa_hexdump(MSG_DEBUG, "EAP-AKA: (encr) AT_NONCE_S",
128139beb93cSSam Leffler 		    data->nonce_s, EAP_SIM_NONCE_S_LEN);
128239beb93cSSam Leffler 
128339beb93cSSam Leffler 	if (data->eap_method == EAP_TYPE_AKA_PRIME) {
128439beb93cSSam Leffler 		eap_aka_prime_derive_keys_reauth(data->k_re, data->counter,
128539beb93cSSam Leffler 						 data->reauth_id,
128639beb93cSSam Leffler 						 data->reauth_id_len,
128739beb93cSSam Leffler 						 data->nonce_s,
128839beb93cSSam Leffler 						 data->msk, data->emsk);
128939beb93cSSam Leffler 	} else {
129039beb93cSSam Leffler 		eap_sim_derive_keys_reauth(data->counter, data->reauth_id,
129139beb93cSSam Leffler 					   data->reauth_id_len,
129239beb93cSSam Leffler 					   data->nonce_s, data->mk,
129339beb93cSSam Leffler 					   data->msk, data->emsk);
129439beb93cSSam Leffler 	}
1295f05cddf9SRui Paulo 	eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1296f05cddf9SRui Paulo 	eap_aka_learn_ids(sm, data, &eattr);
129739beb93cSSam Leffler 
129839beb93cSSam Leffler 	if (data->result_ind && attr->result_ind)
129939beb93cSSam Leffler 		data->use_result_ind = 1;
130039beb93cSSam Leffler 
13015b9c547cSRui Paulo 	if (data->state != FAILURE) {
130239beb93cSSam Leffler 		eap_aka_state(data, data->use_result_ind ?
130339beb93cSSam Leffler 			      RESULT_SUCCESS : SUCCESS);
130439beb93cSSam Leffler 	}
130539beb93cSSam Leffler 
130639beb93cSSam Leffler 	data->num_id_req = 0;
130739beb93cSSam Leffler 	data->num_notification = 0;
130839beb93cSSam Leffler 	if (data->counter > EAP_AKA_MAX_FAST_REAUTHS) {
130939beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "EAP-AKA: Maximum number of "
131039beb93cSSam Leffler 			   "fast reauths performed - force fullauth");
1311f05cddf9SRui Paulo 		eap_aka_clear_identities(sm, data,
1312f05cddf9SRui Paulo 					 CLEAR_REAUTH_ID | CLEAR_EAP_ID);
131339beb93cSSam Leffler 	}
131439beb93cSSam Leffler 	os_free(decrypted);
131539beb93cSSam Leffler 	return eap_aka_response_reauth(data, id, 0, data->nonce_s);
131639beb93cSSam Leffler }
131739beb93cSSam Leffler 
131839beb93cSSam Leffler 
131939beb93cSSam Leffler static struct wpabuf * eap_aka_process(struct eap_sm *sm, void *priv,
132039beb93cSSam Leffler 				       struct eap_method_ret *ret,
132139beb93cSSam Leffler 				       const struct wpabuf *reqData)
132239beb93cSSam Leffler {
132339beb93cSSam Leffler 	struct eap_aka_data *data = priv;
132439beb93cSSam Leffler 	const struct eap_hdr *req;
132539beb93cSSam Leffler 	u8 subtype, id;
132639beb93cSSam Leffler 	struct wpabuf *res;
132739beb93cSSam Leffler 	const u8 *pos;
132839beb93cSSam Leffler 	struct eap_sim_attrs attr;
132939beb93cSSam Leffler 	size_t len;
133039beb93cSSam Leffler 
133139beb93cSSam Leffler 	wpa_hexdump_buf(MSG_DEBUG, "EAP-AKA: EAP data", reqData);
133239beb93cSSam Leffler 	if (eap_get_config_identity(sm, &len) == NULL) {
133339beb93cSSam Leffler 		wpa_printf(MSG_INFO, "EAP-AKA: Identity not configured");
133439beb93cSSam Leffler 		eap_sm_request_identity(sm);
133539beb93cSSam Leffler 		ret->ignore = TRUE;
133639beb93cSSam Leffler 		return NULL;
133739beb93cSSam Leffler 	}
133839beb93cSSam Leffler 
133939beb93cSSam Leffler 	pos = eap_hdr_validate(EAP_VENDOR_IETF, data->eap_method, reqData,
134039beb93cSSam Leffler 			       &len);
1341325151a3SRui Paulo 	if (pos == NULL || len < 3) {
134239beb93cSSam Leffler 		ret->ignore = TRUE;
134339beb93cSSam Leffler 		return NULL;
134439beb93cSSam Leffler 	}
134539beb93cSSam Leffler 	req = wpabuf_head(reqData);
134639beb93cSSam Leffler 	id = req->identifier;
134739beb93cSSam Leffler 	len = be_to_host16(req->length);
134839beb93cSSam Leffler 
134939beb93cSSam Leffler 	ret->ignore = FALSE;
135039beb93cSSam Leffler 	ret->methodState = METHOD_MAY_CONT;
135139beb93cSSam Leffler 	ret->decision = DECISION_FAIL;
135239beb93cSSam Leffler 	ret->allowNotifications = TRUE;
135339beb93cSSam Leffler 
135439beb93cSSam Leffler 	subtype = *pos++;
135539beb93cSSam Leffler 	wpa_printf(MSG_DEBUG, "EAP-AKA: Subtype=%d", subtype);
135639beb93cSSam Leffler 	pos += 2; /* Reserved */
135739beb93cSSam Leffler 
135839beb93cSSam Leffler 	if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr,
135939beb93cSSam Leffler 			       data->eap_method == EAP_TYPE_AKA_PRIME ? 2 : 1,
136039beb93cSSam Leffler 			       0)) {
136139beb93cSSam Leffler 		res = eap_aka_client_error(data, id,
136239beb93cSSam Leffler 					   EAP_AKA_UNABLE_TO_PROCESS_PACKET);
136339beb93cSSam Leffler 		goto done;
136439beb93cSSam Leffler 	}
136539beb93cSSam Leffler 
136639beb93cSSam Leffler 	switch (subtype) {
136739beb93cSSam Leffler 	case EAP_AKA_SUBTYPE_IDENTITY:
136839beb93cSSam Leffler 		res = eap_aka_process_identity(sm, data, id, reqData, &attr);
136939beb93cSSam Leffler 		break;
137039beb93cSSam Leffler 	case EAP_AKA_SUBTYPE_CHALLENGE:
137139beb93cSSam Leffler 		res = eap_aka_process_challenge(sm, data, id, reqData, &attr);
137239beb93cSSam Leffler 		break;
137339beb93cSSam Leffler 	case EAP_AKA_SUBTYPE_NOTIFICATION:
137439beb93cSSam Leffler 		res = eap_aka_process_notification(sm, data, id, reqData,
137539beb93cSSam Leffler 						   &attr);
137639beb93cSSam Leffler 		break;
137739beb93cSSam Leffler 	case EAP_AKA_SUBTYPE_REAUTHENTICATION:
137839beb93cSSam Leffler 		res = eap_aka_process_reauthentication(sm, data, id, reqData,
137939beb93cSSam Leffler 						       &attr);
138039beb93cSSam Leffler 		break;
138139beb93cSSam Leffler 	case EAP_AKA_SUBTYPE_CLIENT_ERROR:
138239beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Client-Error");
138339beb93cSSam Leffler 		res = eap_aka_client_error(data, id,
138439beb93cSSam Leffler 					   EAP_AKA_UNABLE_TO_PROCESS_PACKET);
138539beb93cSSam Leffler 		break;
138639beb93cSSam Leffler 	default:
138739beb93cSSam Leffler 		wpa_printf(MSG_DEBUG, "EAP-AKA: Unknown subtype=%d", subtype);
138839beb93cSSam Leffler 		res = eap_aka_client_error(data, id,
138939beb93cSSam Leffler 					   EAP_AKA_UNABLE_TO_PROCESS_PACKET);
139039beb93cSSam Leffler 		break;
139139beb93cSSam Leffler 	}
139239beb93cSSam Leffler 
139339beb93cSSam Leffler done:
139439beb93cSSam Leffler 	if (data->state == FAILURE) {
139539beb93cSSam Leffler 		ret->decision = DECISION_FAIL;
139639beb93cSSam Leffler 		ret->methodState = METHOD_DONE;
139739beb93cSSam Leffler 	} else if (data->state == SUCCESS) {
139839beb93cSSam Leffler 		ret->decision = data->use_result_ind ?
139939beb93cSSam Leffler 			DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
140039beb93cSSam Leffler 		/*
140139beb93cSSam Leffler 		 * It is possible for the server to reply with AKA
140239beb93cSSam Leffler 		 * Notification, so we must allow the method to continue and
140339beb93cSSam Leffler 		 * not only accept EAP-Success at this point.
140439beb93cSSam Leffler 		 */
140539beb93cSSam Leffler 		ret->methodState = data->use_result_ind ?
140639beb93cSSam Leffler 			METHOD_DONE : METHOD_MAY_CONT;
14075b9c547cSRui Paulo 	} else if (data->state == RESULT_SUCCESS)
140839beb93cSSam Leffler 		ret->methodState = METHOD_CONT;
140939beb93cSSam Leffler 
141039beb93cSSam Leffler 	if (ret->methodState == METHOD_DONE) {
141139beb93cSSam Leffler 		ret->allowNotifications = FALSE;
141239beb93cSSam Leffler 	}
141339beb93cSSam Leffler 
141439beb93cSSam Leffler 	return res;
141539beb93cSSam Leffler }
141639beb93cSSam Leffler 
141739beb93cSSam Leffler 
141839beb93cSSam Leffler static Boolean eap_aka_has_reauth_data(struct eap_sm *sm, void *priv)
141939beb93cSSam Leffler {
142039beb93cSSam Leffler 	struct eap_aka_data *data = priv;
142139beb93cSSam Leffler 	return data->pseudonym || data->reauth_id;
142239beb93cSSam Leffler }
142339beb93cSSam Leffler 
142439beb93cSSam Leffler 
142539beb93cSSam Leffler static void eap_aka_deinit_for_reauth(struct eap_sm *sm, void *priv)
142639beb93cSSam Leffler {
142739beb93cSSam Leffler 	struct eap_aka_data *data = priv;
1428f05cddf9SRui Paulo 	eap_aka_clear_identities(sm, data, CLEAR_EAP_ID);
142939beb93cSSam Leffler 	data->prev_id = -1;
143039beb93cSSam Leffler 	wpabuf_free(data->id_msgs);
143139beb93cSSam Leffler 	data->id_msgs = NULL;
143239beb93cSSam Leffler 	data->use_result_ind = 0;
143339beb93cSSam Leffler 	data->kdf_negotiation = 0;
14345b9c547cSRui Paulo 	eap_aka_clear_keys(data, 1);
143539beb93cSSam Leffler }
143639beb93cSSam Leffler 
143739beb93cSSam Leffler 
143839beb93cSSam Leffler static void * eap_aka_init_for_reauth(struct eap_sm *sm, void *priv)
143939beb93cSSam Leffler {
144039beb93cSSam Leffler 	struct eap_aka_data *data = priv;
144139beb93cSSam Leffler 	data->num_id_req = 0;
144239beb93cSSam Leffler 	data->num_notification = 0;
144339beb93cSSam Leffler 	eap_aka_state(data, CONTINUE);
144439beb93cSSam Leffler 	return priv;
144539beb93cSSam Leffler }
144639beb93cSSam Leffler 
144739beb93cSSam Leffler 
144839beb93cSSam Leffler static const u8 * eap_aka_get_identity(struct eap_sm *sm, void *priv,
144939beb93cSSam Leffler 				       size_t *len)
145039beb93cSSam Leffler {
145139beb93cSSam Leffler 	struct eap_aka_data *data = priv;
145239beb93cSSam Leffler 
145339beb93cSSam Leffler 	if (data->reauth_id) {
145439beb93cSSam Leffler 		*len = data->reauth_id_len;
145539beb93cSSam Leffler 		return data->reauth_id;
145639beb93cSSam Leffler 	}
145739beb93cSSam Leffler 
145839beb93cSSam Leffler 	if (data->pseudonym) {
145939beb93cSSam Leffler 		*len = data->pseudonym_len;
146039beb93cSSam Leffler 		return data->pseudonym;
146139beb93cSSam Leffler 	}
146239beb93cSSam Leffler 
146339beb93cSSam Leffler 	return NULL;
146439beb93cSSam Leffler }
146539beb93cSSam Leffler 
146639beb93cSSam Leffler 
146739beb93cSSam Leffler static Boolean eap_aka_isKeyAvailable(struct eap_sm *sm, void *priv)
146839beb93cSSam Leffler {
146939beb93cSSam Leffler 	struct eap_aka_data *data = priv;
147039beb93cSSam Leffler 	return data->state == SUCCESS;
147139beb93cSSam Leffler }
147239beb93cSSam Leffler 
147339beb93cSSam Leffler 
147439beb93cSSam Leffler static u8 * eap_aka_getKey(struct eap_sm *sm, void *priv, size_t *len)
147539beb93cSSam Leffler {
147639beb93cSSam Leffler 	struct eap_aka_data *data = priv;
147739beb93cSSam Leffler 	u8 *key;
147839beb93cSSam Leffler 
147939beb93cSSam Leffler 	if (data->state != SUCCESS)
148039beb93cSSam Leffler 		return NULL;
148139beb93cSSam Leffler 
1482*85732ac8SCy Schubert 	key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN);
148339beb93cSSam Leffler 	if (key == NULL)
148439beb93cSSam Leffler 		return NULL;
148539beb93cSSam Leffler 
148639beb93cSSam Leffler 	*len = EAP_SIM_KEYING_DATA_LEN;
148739beb93cSSam Leffler 
148839beb93cSSam Leffler 	return key;
148939beb93cSSam Leffler }
149039beb93cSSam Leffler 
149139beb93cSSam Leffler 
14925b9c547cSRui Paulo static u8 * eap_aka_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
14935b9c547cSRui Paulo {
14945b9c547cSRui Paulo 	struct eap_aka_data *data = priv;
14955b9c547cSRui Paulo 	u8 *id;
14965b9c547cSRui Paulo 
14975b9c547cSRui Paulo 	if (data->state != SUCCESS)
14985b9c547cSRui Paulo 		return NULL;
14995b9c547cSRui Paulo 
15005b9c547cSRui Paulo 	*len = 1 + EAP_AKA_RAND_LEN + EAP_AKA_AUTN_LEN;
15015b9c547cSRui Paulo 	id = os_malloc(*len);
15025b9c547cSRui Paulo 	if (id == NULL)
15035b9c547cSRui Paulo 		return NULL;
15045b9c547cSRui Paulo 
15055b9c547cSRui Paulo 	id[0] = data->eap_method;
15065b9c547cSRui Paulo 	os_memcpy(id + 1, data->rand, EAP_AKA_RAND_LEN);
15075b9c547cSRui Paulo 	os_memcpy(id + 1 + EAP_AKA_RAND_LEN, data->autn, EAP_AKA_AUTN_LEN);
15085b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "EAP-AKA: Derived Session-Id", id, *len);
15095b9c547cSRui Paulo 
15105b9c547cSRui Paulo 	return id;
15115b9c547cSRui Paulo }
15125b9c547cSRui Paulo 
15135b9c547cSRui Paulo 
151439beb93cSSam Leffler static u8 * eap_aka_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
151539beb93cSSam Leffler {
151639beb93cSSam Leffler 	struct eap_aka_data *data = priv;
151739beb93cSSam Leffler 	u8 *key;
151839beb93cSSam Leffler 
151939beb93cSSam Leffler 	if (data->state != SUCCESS)
152039beb93cSSam Leffler 		return NULL;
152139beb93cSSam Leffler 
1522*85732ac8SCy Schubert 	key = os_memdup(data->emsk, EAP_EMSK_LEN);
152339beb93cSSam Leffler 	if (key == NULL)
152439beb93cSSam Leffler 		return NULL;
152539beb93cSSam Leffler 
152639beb93cSSam Leffler 	*len = EAP_EMSK_LEN;
152739beb93cSSam Leffler 
152839beb93cSSam Leffler 	return key;
152939beb93cSSam Leffler }
153039beb93cSSam Leffler 
153139beb93cSSam Leffler 
1532*85732ac8SCy Schubert static int eap_aka_get_error_code(void *priv)
1533*85732ac8SCy Schubert {
1534*85732ac8SCy Schubert 	struct eap_aka_data *data = priv;
1535*85732ac8SCy Schubert 	int current_data_error;
1536*85732ac8SCy Schubert 
1537*85732ac8SCy Schubert 	if (!data)
1538*85732ac8SCy Schubert 		return NO_EAP_METHOD_ERROR;
1539*85732ac8SCy Schubert 
1540*85732ac8SCy Schubert 	current_data_error = data->error_code;
1541*85732ac8SCy Schubert 
1542*85732ac8SCy Schubert 	/* Now reset for next transaction */
1543*85732ac8SCy Schubert 	data->error_code = NO_EAP_METHOD_ERROR;
1544*85732ac8SCy Schubert 
1545*85732ac8SCy Schubert 	return current_data_error;
1546*85732ac8SCy Schubert }
1547*85732ac8SCy Schubert 
1548*85732ac8SCy Schubert 
154939beb93cSSam Leffler int eap_peer_aka_register(void)
155039beb93cSSam Leffler {
155139beb93cSSam Leffler 	struct eap_method *eap;
155239beb93cSSam Leffler 
155339beb93cSSam Leffler 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
155439beb93cSSam Leffler 				    EAP_VENDOR_IETF, EAP_TYPE_AKA, "AKA");
155539beb93cSSam Leffler 	if (eap == NULL)
155639beb93cSSam Leffler 		return -1;
155739beb93cSSam Leffler 
155839beb93cSSam Leffler 	eap->init = eap_aka_init;
155939beb93cSSam Leffler 	eap->deinit = eap_aka_deinit;
156039beb93cSSam Leffler 	eap->process = eap_aka_process;
156139beb93cSSam Leffler 	eap->isKeyAvailable = eap_aka_isKeyAvailable;
156239beb93cSSam Leffler 	eap->getKey = eap_aka_getKey;
15635b9c547cSRui Paulo 	eap->getSessionId = eap_aka_get_session_id;
156439beb93cSSam Leffler 	eap->has_reauth_data = eap_aka_has_reauth_data;
156539beb93cSSam Leffler 	eap->deinit_for_reauth = eap_aka_deinit_for_reauth;
156639beb93cSSam Leffler 	eap->init_for_reauth = eap_aka_init_for_reauth;
156739beb93cSSam Leffler 	eap->get_identity = eap_aka_get_identity;
156839beb93cSSam Leffler 	eap->get_emsk = eap_aka_get_emsk;
1569*85732ac8SCy Schubert 	eap->get_error_code = eap_aka_get_error_code;
157039beb93cSSam Leffler 
1571780fb4a2SCy Schubert 	return eap_peer_method_register(eap);
157239beb93cSSam Leffler }
157339beb93cSSam Leffler 
157439beb93cSSam Leffler 
157539beb93cSSam Leffler #ifdef EAP_AKA_PRIME
157639beb93cSSam Leffler int eap_peer_aka_prime_register(void)
157739beb93cSSam Leffler {
157839beb93cSSam Leffler 	struct eap_method *eap;
157939beb93cSSam Leffler 
158039beb93cSSam Leffler 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
158139beb93cSSam Leffler 				    EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME,
158239beb93cSSam Leffler 				    "AKA'");
158339beb93cSSam Leffler 	if (eap == NULL)
158439beb93cSSam Leffler 		return -1;
158539beb93cSSam Leffler 
158639beb93cSSam Leffler 	eap->init = eap_aka_prime_init;
158739beb93cSSam Leffler 	eap->deinit = eap_aka_deinit;
158839beb93cSSam Leffler 	eap->process = eap_aka_process;
158939beb93cSSam Leffler 	eap->isKeyAvailable = eap_aka_isKeyAvailable;
159039beb93cSSam Leffler 	eap->getKey = eap_aka_getKey;
15915b9c547cSRui Paulo 	eap->getSessionId = eap_aka_get_session_id;
159239beb93cSSam Leffler 	eap->has_reauth_data = eap_aka_has_reauth_data;
159339beb93cSSam Leffler 	eap->deinit_for_reauth = eap_aka_deinit_for_reauth;
159439beb93cSSam Leffler 	eap->init_for_reauth = eap_aka_init_for_reauth;
159539beb93cSSam Leffler 	eap->get_identity = eap_aka_get_identity;
159639beb93cSSam Leffler 	eap->get_emsk = eap_aka_get_emsk;
1597*85732ac8SCy Schubert 	eap->get_error_code = eap_aka_get_error_code;
159839beb93cSSam Leffler 
1599780fb4a2SCy Schubert 	return eap_peer_method_register(eap);
160039beb93cSSam Leffler }
160139beb93cSSam Leffler #endif /* EAP_AKA_PRIME */
1602