1e28a4053SRui Paulo /*
2e28a4053SRui Paulo  * hostapd / EAP-SIM (RFC 4186)
3f05cddf9SRui Paulo  * Copyright (c) 2005-2012, Jouni Malinen <j@w1.fi>
4e28a4053SRui Paulo  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
7e28a4053SRui Paulo  */
8e28a4053SRui Paulo 
9e28a4053SRui Paulo #include "includes.h"
10e28a4053SRui Paulo 
11e28a4053SRui Paulo #include "common.h"
12f05cddf9SRui Paulo #include "crypto/random.h"
13e28a4053SRui Paulo #include "eap_server/eap_i.h"
14e28a4053SRui Paulo #include "eap_common/eap_sim_common.h"
15e28a4053SRui Paulo #include "eap_server/eap_sim_db.h"
16e28a4053SRui Paulo 
17e28a4053SRui Paulo 
18e28a4053SRui Paulo struct eap_sim_data {
19e28a4053SRui Paulo 	u8 mk[EAP_SIM_MK_LEN];
20e28a4053SRui Paulo 	u8 nonce_mt[EAP_SIM_NONCE_MT_LEN];
21e28a4053SRui Paulo 	u8 nonce_s[EAP_SIM_NONCE_S_LEN];
22e28a4053SRui Paulo 	u8 k_aut[EAP_SIM_K_AUT_LEN];
23e28a4053SRui Paulo 	u8 k_encr[EAP_SIM_K_ENCR_LEN];
24e28a4053SRui Paulo 	u8 msk[EAP_SIM_KEYING_DATA_LEN];
25e28a4053SRui Paulo 	u8 emsk[EAP_EMSK_LEN];
26e28a4053SRui Paulo 	u8 kc[EAP_SIM_MAX_CHAL][EAP_SIM_KC_LEN];
27e28a4053SRui Paulo 	u8 sres[EAP_SIM_MAX_CHAL][EAP_SIM_SRES_LEN];
28e28a4053SRui Paulo 	u8 rand[EAP_SIM_MAX_CHAL][GSM_RAND_LEN];
29206b73d0SCy Schubert 	u8 reauth_mac[EAP_SIM_MAC_LEN];
30e28a4053SRui Paulo 	int num_chal;
31e28a4053SRui Paulo 	enum {
32e28a4053SRui Paulo 		START, CHALLENGE, REAUTH, NOTIFICATION, SUCCESS, FAILURE
33e28a4053SRui Paulo 	} state;
34e28a4053SRui Paulo 	char *next_pseudonym;
35e28a4053SRui Paulo 	char *next_reauth_id;
36e28a4053SRui Paulo 	u16 counter;
37e28a4053SRui Paulo 	struct eap_sim_reauth *reauth;
38e28a4053SRui Paulo 	u16 notification;
39e28a4053SRui Paulo 	int use_result_ind;
40f05cddf9SRui Paulo 	int start_round;
41f05cddf9SRui Paulo 	char permanent[20]; /* Permanent username */
42e28a4053SRui Paulo };
43e28a4053SRui Paulo 
44e28a4053SRui Paulo 
eap_sim_state_txt(int state)45e28a4053SRui Paulo static const char * eap_sim_state_txt(int state)
46e28a4053SRui Paulo {
47e28a4053SRui Paulo 	switch (state) {
48e28a4053SRui Paulo 	case START:
49e28a4053SRui Paulo 		return "START";
50e28a4053SRui Paulo 	case CHALLENGE:
51e28a4053SRui Paulo 		return "CHALLENGE";
52e28a4053SRui Paulo 	case REAUTH:
53e28a4053SRui Paulo 		return "REAUTH";
54e28a4053SRui Paulo 	case SUCCESS:
55e28a4053SRui Paulo 		return "SUCCESS";
56e28a4053SRui Paulo 	case FAILURE:
57e28a4053SRui Paulo 		return "FAILURE";
58e28a4053SRui Paulo 	case NOTIFICATION:
59e28a4053SRui Paulo 		return "NOTIFICATION";
60e28a4053SRui Paulo 	default:
61e28a4053SRui Paulo 		return "Unknown?!";
62e28a4053SRui Paulo 	}
63e28a4053SRui Paulo }
64e28a4053SRui Paulo 
65e28a4053SRui Paulo 
eap_sim_state(struct eap_sim_data * data,int state)66e28a4053SRui Paulo static void eap_sim_state(struct eap_sim_data *data, int state)
67e28a4053SRui Paulo {
68e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: %s -> %s",
69e28a4053SRui Paulo 		   eap_sim_state_txt(data->state),
70e28a4053SRui Paulo 		   eap_sim_state_txt(state));
71e28a4053SRui Paulo 	data->state = state;
72e28a4053SRui Paulo }
73e28a4053SRui Paulo 
74e28a4053SRui Paulo 
eap_sim_init(struct eap_sm * sm)75e28a4053SRui Paulo static void * eap_sim_init(struct eap_sm *sm)
76e28a4053SRui Paulo {
77e28a4053SRui Paulo 	struct eap_sim_data *data;
78e28a4053SRui Paulo 
79*c1d255d3SCy Schubert 	if (!sm->cfg->eap_sim_db_priv) {
80e28a4053SRui Paulo 		wpa_printf(MSG_WARNING, "EAP-SIM: eap_sim_db not configured");
81e28a4053SRui Paulo 		return NULL;
82e28a4053SRui Paulo 	}
83e28a4053SRui Paulo 
84e28a4053SRui Paulo 	data = os_zalloc(sizeof(*data));
85e28a4053SRui Paulo 	if (data == NULL)
86e28a4053SRui Paulo 		return NULL;
87e28a4053SRui Paulo 	data->state = START;
88e28a4053SRui Paulo 
89e28a4053SRui Paulo 	return data;
90e28a4053SRui Paulo }
91e28a4053SRui Paulo 
92e28a4053SRui Paulo 
eap_sim_reset(struct eap_sm * sm,void * priv)93e28a4053SRui Paulo static void eap_sim_reset(struct eap_sm *sm, void *priv)
94e28a4053SRui Paulo {
95e28a4053SRui Paulo 	struct eap_sim_data *data = priv;
96e28a4053SRui Paulo 	os_free(data->next_pseudonym);
97e28a4053SRui Paulo 	os_free(data->next_reauth_id);
985b9c547cSRui Paulo 	bin_clear_free(data, sizeof(*data));
99e28a4053SRui Paulo }
100e28a4053SRui Paulo 
101e28a4053SRui Paulo 
eap_sim_build_start(struct eap_sm * sm,struct eap_sim_data * data,u8 id)102e28a4053SRui Paulo static struct wpabuf * eap_sim_build_start(struct eap_sm *sm,
103e28a4053SRui Paulo 					   struct eap_sim_data *data, u8 id)
104e28a4053SRui Paulo {
105e28a4053SRui Paulo 	struct eap_sim_msg *msg;
106e28a4053SRui Paulo 	u8 ver[2];
107e28a4053SRui Paulo 
108e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Generating Start");
109e28a4053SRui Paulo 	msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_SIM,
110e28a4053SRui Paulo 			       EAP_SIM_SUBTYPE_START);
111f05cddf9SRui Paulo 	data->start_round++;
112f05cddf9SRui Paulo 	if (data->start_round == 1) {
113e28a4053SRui Paulo 		/*
114e28a4053SRui Paulo 		 * RFC 4186, Chap. 4.2.4 recommends that identity from EAP is
115e28a4053SRui Paulo 		 * ignored and the SIM/Start is used to request the identity.
116e28a4053SRui Paulo 		 */
117e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "   AT_ANY_ID_REQ");
118e28a4053SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_ANY_ID_REQ, 0, NULL, 0);
119f05cddf9SRui Paulo 	} else if (data->start_round > 3) {
120f05cddf9SRui Paulo 		/* Cannot use more than three rounds of Start messages */
121f05cddf9SRui Paulo 		eap_sim_msg_free(msg);
122f05cddf9SRui Paulo 		return NULL;
123f05cddf9SRui Paulo 	} else if (data->start_round == 0) {
124f05cddf9SRui Paulo 		/*
125f05cddf9SRui Paulo 		 * This is a special case that is used to recover from
126f05cddf9SRui Paulo 		 * AT_COUNTER_TOO_SMALL during re-authentication. Since we
127f05cddf9SRui Paulo 		 * already know the identity of the peer, there is no need to
128f05cddf9SRui Paulo 		 * request any identity in this case.
129f05cddf9SRui Paulo 		 */
130f05cddf9SRui Paulo 	} else if (sm->identity && sm->identity_len > 0 &&
131f05cddf9SRui Paulo 		   sm->identity[0] == EAP_SIM_REAUTH_ID_PREFIX) {
132f05cddf9SRui Paulo 		/* Reauth id may have expired - try fullauth */
133f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "   AT_FULLAUTH_ID_REQ");
134f05cddf9SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_FULLAUTH_ID_REQ, 0, NULL, 0);
135f05cddf9SRui Paulo 	} else {
136f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "   AT_PERMANENT_ID_REQ");
137f05cddf9SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_PERMANENT_ID_REQ, 0, NULL, 0);
138e28a4053SRui Paulo 	}
139e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "   AT_VERSION_LIST");
140e28a4053SRui Paulo 	ver[0] = 0;
141e28a4053SRui Paulo 	ver[1] = EAP_SIM_VERSION;
142e28a4053SRui Paulo 	eap_sim_msg_add(msg, EAP_SIM_AT_VERSION_LIST, sizeof(ver),
143e28a4053SRui Paulo 			ver, sizeof(ver));
1445b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
145e28a4053SRui Paulo }
146e28a4053SRui Paulo 
147e28a4053SRui Paulo 
eap_sim_build_encr(struct eap_sm * sm,struct eap_sim_data * data,struct eap_sim_msg * msg,u16 counter,const u8 * nonce_s)148e28a4053SRui Paulo static int eap_sim_build_encr(struct eap_sm *sm, struct eap_sim_data *data,
149e28a4053SRui Paulo 			      struct eap_sim_msg *msg, u16 counter,
150e28a4053SRui Paulo 			      const u8 *nonce_s)
151e28a4053SRui Paulo {
152e28a4053SRui Paulo 	os_free(data->next_pseudonym);
153*c1d255d3SCy Schubert 	if (!(sm->cfg->eap_sim_id & 0x01)) {
154206b73d0SCy Schubert 		/* Use of pseudonyms disabled in configuration */
155206b73d0SCy Schubert 		data->next_pseudonym = NULL;
156206b73d0SCy Schubert 	} else if (!nonce_s) {
157e28a4053SRui Paulo 		data->next_pseudonym =
158*c1d255d3SCy Schubert 			eap_sim_db_get_next_pseudonym(sm->cfg->eap_sim_db_priv,
159f05cddf9SRui Paulo 						      EAP_SIM_DB_SIM);
160f05cddf9SRui Paulo 	} else {
161f05cddf9SRui Paulo 		/* Do not update pseudonym during re-authentication */
162f05cddf9SRui Paulo 		data->next_pseudonym = NULL;
163f05cddf9SRui Paulo 	}
164e28a4053SRui Paulo 	os_free(data->next_reauth_id);
165*c1d255d3SCy Schubert 	if (!(sm->cfg->eap_sim_id & 0x02)) {
166206b73d0SCy Schubert 		/* Use of fast reauth disabled in configuration */
167206b73d0SCy Schubert 		data->next_reauth_id = NULL;
168206b73d0SCy Schubert 	} else if (data->counter <= EAP_SIM_MAX_FAST_REAUTHS) {
169e28a4053SRui Paulo 		data->next_reauth_id =
170*c1d255d3SCy Schubert 			eap_sim_db_get_next_reauth_id(sm->cfg->eap_sim_db_priv,
171f05cddf9SRui Paulo 						      EAP_SIM_DB_SIM);
172e28a4053SRui Paulo 	} else {
173e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Max fast re-authentication "
174e28a4053SRui Paulo 			   "count exceeded - force full authentication");
175e28a4053SRui Paulo 		data->next_reauth_id = NULL;
176e28a4053SRui Paulo 	}
177e28a4053SRui Paulo 
178e28a4053SRui Paulo 	if (data->next_pseudonym == NULL && data->next_reauth_id == NULL &&
179e28a4053SRui Paulo 	    counter == 0 && nonce_s == NULL)
180e28a4053SRui Paulo 		return 0;
181e28a4053SRui Paulo 
182e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "   AT_IV");
183e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
184e28a4053SRui Paulo 	eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
185e28a4053SRui Paulo 
186e28a4053SRui Paulo 	if (counter > 0) {
187e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "   *AT_COUNTER (%u)", counter);
188e28a4053SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
189e28a4053SRui Paulo 	}
190e28a4053SRui Paulo 
191e28a4053SRui Paulo 	if (nonce_s) {
192e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "   *AT_NONCE_S");
193e28a4053SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_S, 0, nonce_s,
194e28a4053SRui Paulo 				EAP_SIM_NONCE_S_LEN);
195e28a4053SRui Paulo 	}
196e28a4053SRui Paulo 
197e28a4053SRui Paulo 	if (data->next_pseudonym) {
198e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "   *AT_NEXT_PSEUDONYM (%s)",
199e28a4053SRui Paulo 			   data->next_pseudonym);
200e28a4053SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_NEXT_PSEUDONYM,
201e28a4053SRui Paulo 				os_strlen(data->next_pseudonym),
202e28a4053SRui Paulo 				(u8 *) data->next_pseudonym,
203e28a4053SRui Paulo 				os_strlen(data->next_pseudonym));
204e28a4053SRui Paulo 	}
205e28a4053SRui Paulo 
206e28a4053SRui Paulo 	if (data->next_reauth_id) {
207e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "   *AT_NEXT_REAUTH_ID (%s)",
208e28a4053SRui Paulo 			   data->next_reauth_id);
209e28a4053SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_NEXT_REAUTH_ID,
210e28a4053SRui Paulo 				os_strlen(data->next_reauth_id),
211e28a4053SRui Paulo 				(u8 *) data->next_reauth_id,
212e28a4053SRui Paulo 				os_strlen(data->next_reauth_id));
213e28a4053SRui Paulo 	}
214e28a4053SRui Paulo 
215e28a4053SRui Paulo 	if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
216e28a4053SRui Paulo 		wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
217e28a4053SRui Paulo 			   "AT_ENCR_DATA");
218e28a4053SRui Paulo 		return -1;
219e28a4053SRui Paulo 	}
220e28a4053SRui Paulo 
221e28a4053SRui Paulo 	return 0;
222e28a4053SRui Paulo }
223e28a4053SRui Paulo 
224e28a4053SRui Paulo 
eap_sim_build_challenge(struct eap_sm * sm,struct eap_sim_data * data,u8 id)225e28a4053SRui Paulo static struct wpabuf * eap_sim_build_challenge(struct eap_sm *sm,
226e28a4053SRui Paulo 					       struct eap_sim_data *data,
227e28a4053SRui Paulo 					       u8 id)
228e28a4053SRui Paulo {
229e28a4053SRui Paulo 	struct eap_sim_msg *msg;
230e28a4053SRui Paulo 
231e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Generating Challenge");
232e28a4053SRui Paulo 	msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_SIM,
233e28a4053SRui Paulo 			       EAP_SIM_SUBTYPE_CHALLENGE);
234e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "   AT_RAND");
235e28a4053SRui Paulo 	eap_sim_msg_add(msg, EAP_SIM_AT_RAND, 0, (u8 *) data->rand,
236e28a4053SRui Paulo 			data->num_chal * GSM_RAND_LEN);
237e28a4053SRui Paulo 
238e28a4053SRui Paulo 	if (eap_sim_build_encr(sm, data, msg, 0, NULL)) {
239e28a4053SRui Paulo 		eap_sim_msg_free(msg);
240e28a4053SRui Paulo 		return NULL;
241e28a4053SRui Paulo 	}
242e28a4053SRui Paulo 
243*c1d255d3SCy Schubert 	if (sm->cfg->eap_sim_aka_result_ind) {
244e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
245e28a4053SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
246e28a4053SRui Paulo 	}
247e28a4053SRui Paulo 
248e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "   AT_MAC");
249e28a4053SRui Paulo 	eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
2505b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut,
2515b9c547cSRui Paulo 				  data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
252e28a4053SRui Paulo }
253e28a4053SRui Paulo 
254e28a4053SRui Paulo 
eap_sim_build_reauth(struct eap_sm * sm,struct eap_sim_data * data,u8 id)255e28a4053SRui Paulo static struct wpabuf * eap_sim_build_reauth(struct eap_sm *sm,
256e28a4053SRui Paulo 					    struct eap_sim_data *data, u8 id)
257e28a4053SRui Paulo {
258e28a4053SRui Paulo 	struct eap_sim_msg *msg;
259206b73d0SCy Schubert 	struct wpabuf *buf;
260e28a4053SRui Paulo 
261e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Generating Re-authentication");
262e28a4053SRui Paulo 
263f05cddf9SRui Paulo 	if (random_get_bytes(data->nonce_s, EAP_SIM_NONCE_S_LEN))
264e28a4053SRui Paulo 		return NULL;
265e28a4053SRui Paulo 	wpa_hexdump_key(MSG_MSGDUMP, "EAP-SIM: NONCE_S",
266e28a4053SRui Paulo 			data->nonce_s, EAP_SIM_NONCE_S_LEN);
267e28a4053SRui Paulo 
268e28a4053SRui Paulo 	eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
269e28a4053SRui Paulo 			    data->emsk);
270e28a4053SRui Paulo 	eap_sim_derive_keys_reauth(data->counter, sm->identity,
271e28a4053SRui Paulo 				   sm->identity_len, data->nonce_s, data->mk,
272e28a4053SRui Paulo 				   data->msk, data->emsk);
273e28a4053SRui Paulo 
274e28a4053SRui Paulo 	msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_SIM,
275e28a4053SRui Paulo 			       EAP_SIM_SUBTYPE_REAUTHENTICATION);
276e28a4053SRui Paulo 
277e28a4053SRui Paulo 	if (eap_sim_build_encr(sm, data, msg, data->counter, data->nonce_s)) {
278e28a4053SRui Paulo 		eap_sim_msg_free(msg);
279e28a4053SRui Paulo 		return NULL;
280e28a4053SRui Paulo 	}
281e28a4053SRui Paulo 
282*c1d255d3SCy Schubert 	if (sm->cfg->eap_sim_aka_result_ind) {
283e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
284e28a4053SRui Paulo 		eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
285e28a4053SRui Paulo 	}
286e28a4053SRui Paulo 
287e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "   AT_MAC");
288e28a4053SRui Paulo 	eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
289206b73d0SCy Schubert 	buf = eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, NULL, 0);
290206b73d0SCy Schubert 
291206b73d0SCy Schubert 	/* Remember this MAC before sending it to the peer. This MAC is used for
292206b73d0SCy Schubert 	 * Session-Id calculation after receiving response from the peer and
293206b73d0SCy Schubert 	 * after all other checks pass. */
294206b73d0SCy Schubert 	os_memcpy(data->reauth_mac,
295206b73d0SCy Schubert 		  wpabuf_head_u8(buf) + wpabuf_len(buf) - EAP_SIM_MAC_LEN,
296206b73d0SCy Schubert 		  EAP_SIM_MAC_LEN);
297206b73d0SCy Schubert 
298206b73d0SCy Schubert 	return buf;
299e28a4053SRui Paulo }
300e28a4053SRui Paulo 
301e28a4053SRui Paulo 
eap_sim_build_notification(struct eap_sm * sm,struct eap_sim_data * data,u8 id)302e28a4053SRui Paulo static struct wpabuf * eap_sim_build_notification(struct eap_sm *sm,
303e28a4053SRui Paulo 						  struct eap_sim_data *data,
304e28a4053SRui Paulo 						  u8 id)
305e28a4053SRui Paulo {
306e28a4053SRui Paulo 	struct eap_sim_msg *msg;
307e28a4053SRui Paulo 
308e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Generating Notification");
309e28a4053SRui Paulo 	msg = eap_sim_msg_init(EAP_CODE_REQUEST, id, EAP_TYPE_SIM,
310e28a4053SRui Paulo 			       EAP_SIM_SUBTYPE_NOTIFICATION);
311e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "   AT_NOTIFICATION (%d)", data->notification);
312e28a4053SRui Paulo 	eap_sim_msg_add(msg, EAP_SIM_AT_NOTIFICATION, data->notification,
313e28a4053SRui Paulo 			NULL, 0);
314e28a4053SRui Paulo 	if (data->use_result_ind) {
315e28a4053SRui Paulo 		if (data->reauth) {
316e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "   AT_IV");
317e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
318e28a4053SRui Paulo 			eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
319e28a4053SRui Paulo 						   EAP_SIM_AT_ENCR_DATA);
320e28a4053SRui Paulo 			wpa_printf(MSG_DEBUG, "   *AT_COUNTER (%u)",
321e28a4053SRui Paulo 				   data->counter);
322e28a4053SRui Paulo 			eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
323e28a4053SRui Paulo 					NULL, 0);
324e28a4053SRui Paulo 
325e28a4053SRui Paulo 			if (eap_sim_msg_add_encr_end(msg, data->k_encr,
326e28a4053SRui Paulo 						     EAP_SIM_AT_PADDING)) {
327e28a4053SRui Paulo 				wpa_printf(MSG_WARNING, "EAP-SIM: Failed to "
328e28a4053SRui Paulo 					   "encrypt AT_ENCR_DATA");
329e28a4053SRui Paulo 				eap_sim_msg_free(msg);
330e28a4053SRui Paulo 				return NULL;
331e28a4053SRui Paulo 			}
332e28a4053SRui Paulo 		}
333e28a4053SRui Paulo 
334e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "   AT_MAC");
335e28a4053SRui Paulo 		eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
336e28a4053SRui Paulo 	}
3375b9c547cSRui Paulo 	return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, NULL, 0);
338e28a4053SRui Paulo }
339e28a4053SRui Paulo 
340e28a4053SRui Paulo 
eap_sim_buildReq(struct eap_sm * sm,void * priv,u8 id)341e28a4053SRui Paulo static struct wpabuf * eap_sim_buildReq(struct eap_sm *sm, void *priv, u8 id)
342e28a4053SRui Paulo {
343e28a4053SRui Paulo 	struct eap_sim_data *data = priv;
344e28a4053SRui Paulo 
345e28a4053SRui Paulo 	switch (data->state) {
346e28a4053SRui Paulo 	case START:
347e28a4053SRui Paulo 		return eap_sim_build_start(sm, data, id);
348e28a4053SRui Paulo 	case CHALLENGE:
349e28a4053SRui Paulo 		return eap_sim_build_challenge(sm, data, id);
350e28a4053SRui Paulo 	case REAUTH:
351e28a4053SRui Paulo 		return eap_sim_build_reauth(sm, data, id);
352e28a4053SRui Paulo 	case NOTIFICATION:
353e28a4053SRui Paulo 		return eap_sim_build_notification(sm, data, id);
354e28a4053SRui Paulo 	default:
355e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown state %d in "
356e28a4053SRui Paulo 			   "buildReq", data->state);
357e28a4053SRui Paulo 		break;
358e28a4053SRui Paulo 	}
359e28a4053SRui Paulo 	return NULL;
360e28a4053SRui Paulo }
361e28a4053SRui Paulo 
362e28a4053SRui Paulo 
eap_sim_check(struct eap_sm * sm,void * priv,struct wpabuf * respData)363*c1d255d3SCy Schubert static bool eap_sim_check(struct eap_sm *sm, void *priv,
364e28a4053SRui Paulo 			  struct wpabuf *respData)
365e28a4053SRui Paulo {
366e28a4053SRui Paulo 	const u8 *pos;
367e28a4053SRui Paulo 	size_t len;
368e28a4053SRui Paulo 
369e28a4053SRui Paulo 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, respData, &len);
370e28a4053SRui Paulo 	if (pos == NULL || len < 3) {
371e28a4053SRui Paulo 		wpa_printf(MSG_INFO, "EAP-SIM: Invalid frame");
372*c1d255d3SCy Schubert 		return true;
373e28a4053SRui Paulo 	}
374e28a4053SRui Paulo 
375*c1d255d3SCy Schubert 	return false;
376f05cddf9SRui Paulo }
377f05cddf9SRui Paulo 
378f05cddf9SRui Paulo 
eap_sim_unexpected_subtype(struct eap_sim_data * data,u8 subtype)379*c1d255d3SCy Schubert static bool eap_sim_unexpected_subtype(struct eap_sim_data *data,
380f05cddf9SRui Paulo 				       u8 subtype)
381f05cddf9SRui Paulo {
382e28a4053SRui Paulo 	if (subtype == EAP_SIM_SUBTYPE_CLIENT_ERROR)
383*c1d255d3SCy Schubert 		return false;
384e28a4053SRui Paulo 
385e28a4053SRui Paulo 	switch (data->state) {
386e28a4053SRui Paulo 	case START:
387e28a4053SRui Paulo 		if (subtype != EAP_SIM_SUBTYPE_START) {
388e28a4053SRui Paulo 			wpa_printf(MSG_INFO, "EAP-SIM: Unexpected response "
389e28a4053SRui Paulo 				   "subtype %d", subtype);
390*c1d255d3SCy Schubert 			return true;
391e28a4053SRui Paulo 		}
392e28a4053SRui Paulo 		break;
393e28a4053SRui Paulo 	case CHALLENGE:
394e28a4053SRui Paulo 		if (subtype != EAP_SIM_SUBTYPE_CHALLENGE) {
395e28a4053SRui Paulo 			wpa_printf(MSG_INFO, "EAP-SIM: Unexpected response "
396e28a4053SRui Paulo 				   "subtype %d", subtype);
397*c1d255d3SCy Schubert 			return true;
398e28a4053SRui Paulo 		}
399e28a4053SRui Paulo 		break;
400e28a4053SRui Paulo 	case REAUTH:
401e28a4053SRui Paulo 		if (subtype != EAP_SIM_SUBTYPE_REAUTHENTICATION) {
402e28a4053SRui Paulo 			wpa_printf(MSG_INFO, "EAP-SIM: Unexpected response "
403e28a4053SRui Paulo 				   "subtype %d", subtype);
404*c1d255d3SCy Schubert 			return true;
405e28a4053SRui Paulo 		}
406e28a4053SRui Paulo 		break;
407e28a4053SRui Paulo 	case NOTIFICATION:
408e28a4053SRui Paulo 		if (subtype != EAP_SIM_SUBTYPE_NOTIFICATION) {
409e28a4053SRui Paulo 			wpa_printf(MSG_INFO, "EAP-SIM: Unexpected response "
410e28a4053SRui Paulo 				   "subtype %d", subtype);
411*c1d255d3SCy Schubert 			return true;
412e28a4053SRui Paulo 		}
413e28a4053SRui Paulo 		break;
414e28a4053SRui Paulo 	default:
415e28a4053SRui Paulo 		wpa_printf(MSG_INFO, "EAP-SIM: Unexpected state (%d) for "
416e28a4053SRui Paulo 			   "processing a response", data->state);
417*c1d255d3SCy Schubert 		return true;
418e28a4053SRui Paulo 	}
419e28a4053SRui Paulo 
420*c1d255d3SCy Schubert 	return false;
421e28a4053SRui Paulo }
422e28a4053SRui Paulo 
423e28a4053SRui Paulo 
eap_sim_supported_ver(struct eap_sim_data * data,int version)424e28a4053SRui Paulo static int eap_sim_supported_ver(struct eap_sim_data *data, int version)
425e28a4053SRui Paulo {
426e28a4053SRui Paulo 	return version == EAP_SIM_VERSION;
427e28a4053SRui Paulo }
428e28a4053SRui Paulo 
429e28a4053SRui Paulo 
eap_sim_process_start(struct eap_sm * sm,struct eap_sim_data * data,struct wpabuf * respData,struct eap_sim_attrs * attr)430e28a4053SRui Paulo static void eap_sim_process_start(struct eap_sm *sm,
431e28a4053SRui Paulo 				  struct eap_sim_data *data,
432e28a4053SRui Paulo 				  struct wpabuf *respData,
433e28a4053SRui Paulo 				  struct eap_sim_attrs *attr)
434e28a4053SRui Paulo {
435e28a4053SRui Paulo 	size_t identity_len;
436e28a4053SRui Paulo 	u8 ver_list[2];
437f05cddf9SRui Paulo 	u8 *new_identity;
438f05cddf9SRui Paulo 	char *username;
439e28a4053SRui Paulo 
440e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Receive start response");
441e28a4053SRui Paulo 
442f05cddf9SRui Paulo 	if (data->start_round == 0) {
443f05cddf9SRui Paulo 		/*
444f05cddf9SRui Paulo 		 * Special case for AT_COUNTER_TOO_SMALL recovery - no identity
445f05cddf9SRui Paulo 		 * was requested since we already know it.
446f05cddf9SRui Paulo 		 */
447f05cddf9SRui Paulo 		goto skip_id_update;
448f05cddf9SRui Paulo 	}
449f05cddf9SRui Paulo 
450f05cddf9SRui Paulo 	/*
451f05cddf9SRui Paulo 	 * We always request identity in SIM/Start, so the peer is required to
452f05cddf9SRui Paulo 	 * have replied with one.
453f05cddf9SRui Paulo 	 */
454f05cddf9SRui Paulo 	if (!attr->identity || attr->identity_len == 0) {
455f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Peer did not provide any "
456f05cddf9SRui Paulo 			   "identity");
457f05cddf9SRui Paulo 		goto failed;
458f05cddf9SRui Paulo 	}
459f05cddf9SRui Paulo 
460f05cddf9SRui Paulo 	new_identity = os_malloc(attr->identity_len);
461f05cddf9SRui Paulo 	if (new_identity == NULL)
462f05cddf9SRui Paulo 		goto failed;
463e28a4053SRui Paulo 	os_free(sm->identity);
464f05cddf9SRui Paulo 	sm->identity = new_identity;
465f05cddf9SRui Paulo 	os_memcpy(sm->identity, attr->identity, attr->identity_len);
466e28a4053SRui Paulo 	sm->identity_len = attr->identity_len;
467e28a4053SRui Paulo 
468e28a4053SRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Identity",
469f05cddf9SRui Paulo 			  sm->identity, sm->identity_len);
470f05cddf9SRui Paulo 	username = sim_get_username(sm->identity, sm->identity_len);
471f05cddf9SRui Paulo 	if (username == NULL)
472f05cddf9SRui Paulo 		goto failed;
473e28a4053SRui Paulo 
474f05cddf9SRui Paulo 	if (username[0] == EAP_SIM_REAUTH_ID_PREFIX) {
475f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Reauth username '%s'",
476f05cddf9SRui Paulo 			   username);
477f05cddf9SRui Paulo 		data->reauth = eap_sim_db_get_reauth_entry(
478*c1d255d3SCy Schubert 			sm->cfg->eap_sim_db_priv, username);
479f05cddf9SRui Paulo 		os_free(username);
480f05cddf9SRui Paulo 		if (data->reauth == NULL) {
481f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown reauth "
482f05cddf9SRui Paulo 				   "identity - request full auth identity");
483f05cddf9SRui Paulo 			/* Remain in START state for another round */
484f05cddf9SRui Paulo 			return;
485f05cddf9SRui Paulo 		}
486f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Using fast re-authentication");
487f05cddf9SRui Paulo 		os_strlcpy(data->permanent, data->reauth->permanent,
488f05cddf9SRui Paulo 			   sizeof(data->permanent));
489f05cddf9SRui Paulo 		data->counter = data->reauth->counter;
490f05cddf9SRui Paulo 		os_memcpy(data->mk, data->reauth->mk, EAP_SIM_MK_LEN);
491e28a4053SRui Paulo 		eap_sim_state(data, REAUTH);
492e28a4053SRui Paulo 		return;
493e28a4053SRui Paulo 	}
494e28a4053SRui Paulo 
495f05cddf9SRui Paulo 	if (username[0] == EAP_SIM_PSEUDONYM_PREFIX) {
496f05cddf9SRui Paulo 		const char *permanent;
497f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Pseudonym username '%s'",
498f05cddf9SRui Paulo 			   username);
499f05cddf9SRui Paulo 		permanent = eap_sim_db_get_permanent(
500*c1d255d3SCy Schubert 			sm->cfg->eap_sim_db_priv, username);
501f05cddf9SRui Paulo 		os_free(username);
502f05cddf9SRui Paulo 		if (permanent == NULL) {
503f05cddf9SRui Paulo 			wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown pseudonym "
504f05cddf9SRui Paulo 				   "identity - request permanent identity");
505f05cddf9SRui Paulo 			/* Remain in START state for another round */
506f05cddf9SRui Paulo 			return;
507f05cddf9SRui Paulo 		}
508f05cddf9SRui Paulo 		os_strlcpy(data->permanent, permanent,
509f05cddf9SRui Paulo 			   sizeof(data->permanent));
510f05cddf9SRui Paulo 	} else if (username[0] == EAP_SIM_PERMANENT_PREFIX) {
511f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Permanent username '%s'",
512f05cddf9SRui Paulo 			   username);
513f05cddf9SRui Paulo 		os_strlcpy(data->permanent, username, sizeof(data->permanent));
514f05cddf9SRui Paulo 		os_free(username);
515f05cddf9SRui Paulo 	} else {
516f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized username '%s'",
517f05cddf9SRui Paulo 			   username);
518f05cddf9SRui Paulo 		os_free(username);
519f05cddf9SRui Paulo 		goto failed;
520f05cddf9SRui Paulo 	}
521f05cddf9SRui Paulo 
522f05cddf9SRui Paulo skip_id_update:
523f05cddf9SRui Paulo 	/* Full authentication */
524f05cddf9SRui Paulo 
525e28a4053SRui Paulo 	if (attr->nonce_mt == NULL || attr->selected_version < 0) {
526e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Start/Response missing "
527e28a4053SRui Paulo 			   "required attributes");
528f05cddf9SRui Paulo 		goto failed;
529e28a4053SRui Paulo 	}
530e28a4053SRui Paulo 
531e28a4053SRui Paulo 	if (!eap_sim_supported_ver(data, attr->selected_version)) {
532e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Peer selected unsupported "
533e28a4053SRui Paulo 			   "version %d", attr->selected_version);
534f05cddf9SRui Paulo 		goto failed;
535e28a4053SRui Paulo 	}
536e28a4053SRui Paulo 
537e28a4053SRui Paulo 	data->counter = 0; /* reset re-auth counter since this is full auth */
538e28a4053SRui Paulo 	data->reauth = NULL;
539e28a4053SRui Paulo 
540e28a4053SRui Paulo 	data->num_chal = eap_sim_db_get_gsm_triplets(
541*c1d255d3SCy Schubert 		sm->cfg->eap_sim_db_priv, data->permanent, EAP_SIM_MAX_CHAL,
542e28a4053SRui Paulo 		(u8 *) data->rand, (u8 *) data->kc, (u8 *) data->sres, sm);
543e28a4053SRui Paulo 	if (data->num_chal == EAP_SIM_DB_PENDING) {
544e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication triplets "
545e28a4053SRui Paulo 			   "not yet available - pending request");
546e28a4053SRui Paulo 		sm->method_pending = METHOD_PENDING_WAIT;
547e28a4053SRui Paulo 		return;
548e28a4053SRui Paulo 	}
549e28a4053SRui Paulo 	if (data->num_chal < 2) {
550e28a4053SRui Paulo 		wpa_printf(MSG_INFO, "EAP-SIM: Failed to get GSM "
551e28a4053SRui Paulo 			   "authentication triplets for the peer");
552f05cddf9SRui Paulo 		goto failed;
553e28a4053SRui Paulo 	}
554e28a4053SRui Paulo 
5554bc52338SCy Schubert 	if (data->permanent[0] == EAP_SIM_PERMANENT_PREFIX)
5564bc52338SCy Schubert 		os_strlcpy(sm->imsi, &data->permanent[1], sizeof(sm->imsi));
5574bc52338SCy Schubert 
558e28a4053SRui Paulo 	identity_len = sm->identity_len;
559e28a4053SRui Paulo 	while (identity_len > 0 && sm->identity[identity_len - 1] == '\0') {
560e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Workaround - drop last null "
561e28a4053SRui Paulo 			   "character from identity");
562e28a4053SRui Paulo 		identity_len--;
563e28a4053SRui Paulo 	}
564e28a4053SRui Paulo 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Identity for MK derivation",
565e28a4053SRui Paulo 			  sm->identity, identity_len);
566e28a4053SRui Paulo 
567e28a4053SRui Paulo 	os_memcpy(data->nonce_mt, attr->nonce_mt, EAP_SIM_NONCE_MT_LEN);
568e28a4053SRui Paulo 	WPA_PUT_BE16(ver_list, EAP_SIM_VERSION);
569e28a4053SRui Paulo 	eap_sim_derive_mk(sm->identity, identity_len, attr->nonce_mt,
570e28a4053SRui Paulo 			  attr->selected_version, ver_list, sizeof(ver_list),
571e28a4053SRui Paulo 			  data->num_chal, (const u8 *) data->kc, data->mk);
572e28a4053SRui Paulo 	eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
573e28a4053SRui Paulo 			    data->emsk);
574e28a4053SRui Paulo 
575e28a4053SRui Paulo 	eap_sim_state(data, CHALLENGE);
576f05cddf9SRui Paulo 	return;
577f05cddf9SRui Paulo 
578f05cddf9SRui Paulo failed:
579f05cddf9SRui Paulo 	data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
580f05cddf9SRui Paulo 	eap_sim_state(data, NOTIFICATION);
581e28a4053SRui Paulo }
582e28a4053SRui Paulo 
583e28a4053SRui Paulo 
eap_sim_process_challenge(struct eap_sm * sm,struct eap_sim_data * data,struct wpabuf * respData,struct eap_sim_attrs * attr)584e28a4053SRui Paulo static void eap_sim_process_challenge(struct eap_sm *sm,
585e28a4053SRui Paulo 				      struct eap_sim_data *data,
586e28a4053SRui Paulo 				      struct wpabuf *respData,
587e28a4053SRui Paulo 				      struct eap_sim_attrs *attr)
588e28a4053SRui Paulo {
589e28a4053SRui Paulo 	if (attr->mac == NULL ||
590e28a4053SRui Paulo 	    eap_sim_verify_mac(data->k_aut, respData, attr->mac,
591e28a4053SRui Paulo 			       (u8 *) data->sres,
592e28a4053SRui Paulo 			       data->num_chal * EAP_SIM_SRES_LEN)) {
593e28a4053SRui Paulo 		wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
594e28a4053SRui Paulo 			   "did not include valid AT_MAC");
595f05cddf9SRui Paulo 		data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
596f05cddf9SRui Paulo 		eap_sim_state(data, NOTIFICATION);
597e28a4053SRui Paulo 		return;
598e28a4053SRui Paulo 	}
599e28a4053SRui Paulo 
600e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Challenge response includes the "
601e28a4053SRui Paulo 		   "correct AT_MAC");
602*c1d255d3SCy Schubert 	if (sm->cfg->eap_sim_aka_result_ind && attr->result_ind) {
603e28a4053SRui Paulo 		data->use_result_ind = 1;
604e28a4053SRui Paulo 		data->notification = EAP_SIM_SUCCESS;
605e28a4053SRui Paulo 		eap_sim_state(data, NOTIFICATION);
606e28a4053SRui Paulo 	} else
607e28a4053SRui Paulo 		eap_sim_state(data, SUCCESS);
608e28a4053SRui Paulo 
609e28a4053SRui Paulo 	if (data->next_pseudonym) {
610*c1d255d3SCy Schubert 		eap_sim_db_add_pseudonym(sm->cfg->eap_sim_db_priv,
611*c1d255d3SCy Schubert 					 data->permanent,
612e28a4053SRui Paulo 					 data->next_pseudonym);
613e28a4053SRui Paulo 		data->next_pseudonym = NULL;
614e28a4053SRui Paulo 	}
615e28a4053SRui Paulo 	if (data->next_reauth_id) {
616*c1d255d3SCy Schubert 		eap_sim_db_add_reauth(sm->cfg->eap_sim_db_priv, data->permanent,
617e28a4053SRui Paulo 				      data->next_reauth_id, data->counter + 1,
618e28a4053SRui Paulo 				      data->mk);
619e28a4053SRui Paulo 		data->next_reauth_id = NULL;
620e28a4053SRui Paulo 	}
621e28a4053SRui Paulo }
622e28a4053SRui Paulo 
623e28a4053SRui Paulo 
eap_sim_process_reauth(struct eap_sm * sm,struct eap_sim_data * data,struct wpabuf * respData,struct eap_sim_attrs * attr)624e28a4053SRui Paulo static void eap_sim_process_reauth(struct eap_sm *sm,
625e28a4053SRui Paulo 				   struct eap_sim_data *data,
626e28a4053SRui Paulo 				   struct wpabuf *respData,
627e28a4053SRui Paulo 				   struct eap_sim_attrs *attr)
628e28a4053SRui Paulo {
629e28a4053SRui Paulo 	struct eap_sim_attrs eattr;
630e28a4053SRui Paulo 	u8 *decrypted = NULL;
631e28a4053SRui Paulo 
632e28a4053SRui Paulo 	if (attr->mac == NULL ||
633e28a4053SRui Paulo 	    eap_sim_verify_mac(data->k_aut, respData, attr->mac, data->nonce_s,
634e28a4053SRui Paulo 			       EAP_SIM_NONCE_S_LEN)) {
635e28a4053SRui Paulo 		wpa_printf(MSG_WARNING, "EAP-SIM: Re-authentication message "
636e28a4053SRui Paulo 			   "did not include valid AT_MAC");
637e28a4053SRui Paulo 		goto fail;
638e28a4053SRui Paulo 	}
639e28a4053SRui Paulo 
640e28a4053SRui Paulo 	if (attr->encr_data == NULL || attr->iv == NULL) {
641e28a4053SRui Paulo 		wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
642e28a4053SRui Paulo 			   "message did not include encrypted data");
643e28a4053SRui Paulo 		goto fail;
644e28a4053SRui Paulo 	}
645e28a4053SRui Paulo 
646e28a4053SRui Paulo 	decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
647e28a4053SRui Paulo 				       attr->encr_data_len, attr->iv, &eattr,
648e28a4053SRui Paulo 				       0);
649e28a4053SRui Paulo 	if (decrypted == NULL) {
650e28a4053SRui Paulo 		wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
651e28a4053SRui Paulo 			   "data from reauthentication message");
652e28a4053SRui Paulo 		goto fail;
653e28a4053SRui Paulo 	}
654e28a4053SRui Paulo 
655e28a4053SRui Paulo 	if (eattr.counter != data->counter) {
656e28a4053SRui Paulo 		wpa_printf(MSG_WARNING, "EAP-SIM: Re-authentication message "
657e28a4053SRui Paulo 			   "used incorrect counter %u, expected %u",
658e28a4053SRui Paulo 			   eattr.counter, data->counter);
659e28a4053SRui Paulo 		goto fail;
660e28a4053SRui Paulo 	}
661e28a4053SRui Paulo 	os_free(decrypted);
662e28a4053SRui Paulo 	decrypted = NULL;
663e28a4053SRui Paulo 
664e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Re-authentication response includes "
665e28a4053SRui Paulo 		   "the correct AT_MAC");
666f05cddf9SRui Paulo 
667f05cddf9SRui Paulo 	if (eattr.counter_too_small) {
668f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-AKA: Re-authentication response "
669f05cddf9SRui Paulo 			   "included AT_COUNTER_TOO_SMALL - starting full "
670f05cddf9SRui Paulo 			   "authentication");
671f05cddf9SRui Paulo 		data->start_round = -1;
672f05cddf9SRui Paulo 		eap_sim_state(data, START);
673f05cddf9SRui Paulo 		return;
674f05cddf9SRui Paulo 	}
675f05cddf9SRui Paulo 
676*c1d255d3SCy Schubert 	if (sm->cfg->eap_sim_aka_result_ind && attr->result_ind) {
677e28a4053SRui Paulo 		data->use_result_ind = 1;
678e28a4053SRui Paulo 		data->notification = EAP_SIM_SUCCESS;
679e28a4053SRui Paulo 		eap_sim_state(data, NOTIFICATION);
680e28a4053SRui Paulo 	} else
681e28a4053SRui Paulo 		eap_sim_state(data, SUCCESS);
682e28a4053SRui Paulo 
683e28a4053SRui Paulo 	if (data->next_reauth_id) {
684*c1d255d3SCy Schubert 		eap_sim_db_add_reauth(sm->cfg->eap_sim_db_priv, data->permanent,
685f05cddf9SRui Paulo 				      data->next_reauth_id,
686e28a4053SRui Paulo 				      data->counter + 1, data->mk);
687e28a4053SRui Paulo 		data->next_reauth_id = NULL;
688e28a4053SRui Paulo 	} else {
689*c1d255d3SCy Schubert 		eap_sim_db_remove_reauth(sm->cfg->eap_sim_db_priv,
690*c1d255d3SCy Schubert 					 data->reauth);
691e28a4053SRui Paulo 		data->reauth = NULL;
692e28a4053SRui Paulo 	}
693e28a4053SRui Paulo 
694e28a4053SRui Paulo 	return;
695e28a4053SRui Paulo 
696e28a4053SRui Paulo fail:
697f05cddf9SRui Paulo 	data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
698f05cddf9SRui Paulo 	eap_sim_state(data, NOTIFICATION);
699*c1d255d3SCy Schubert 	eap_sim_db_remove_reauth(sm->cfg->eap_sim_db_priv, data->reauth);
700e28a4053SRui Paulo 	data->reauth = NULL;
701e28a4053SRui Paulo 	os_free(decrypted);
702e28a4053SRui Paulo }
703e28a4053SRui Paulo 
704e28a4053SRui Paulo 
eap_sim_process_client_error(struct eap_sm * sm,struct eap_sim_data * data,struct wpabuf * respData,struct eap_sim_attrs * attr)705e28a4053SRui Paulo static void eap_sim_process_client_error(struct eap_sm *sm,
706e28a4053SRui Paulo 					 struct eap_sim_data *data,
707e28a4053SRui Paulo 					 struct wpabuf *respData,
708e28a4053SRui Paulo 					 struct eap_sim_attrs *attr)
709e28a4053SRui Paulo {
710e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Client reported error %d",
711e28a4053SRui Paulo 		   attr->client_error_code);
712e28a4053SRui Paulo 	if (data->notification == EAP_SIM_SUCCESS && data->use_result_ind)
713e28a4053SRui Paulo 		eap_sim_state(data, SUCCESS);
714e28a4053SRui Paulo 	else
715e28a4053SRui Paulo 		eap_sim_state(data, FAILURE);
716e28a4053SRui Paulo }
717e28a4053SRui Paulo 
718e28a4053SRui Paulo 
eap_sim_process_notification(struct eap_sm * sm,struct eap_sim_data * data,struct wpabuf * respData,struct eap_sim_attrs * attr)719e28a4053SRui Paulo static void eap_sim_process_notification(struct eap_sm *sm,
720e28a4053SRui Paulo 					 struct eap_sim_data *data,
721e28a4053SRui Paulo 					 struct wpabuf *respData,
722e28a4053SRui Paulo 					 struct eap_sim_attrs *attr)
723e28a4053SRui Paulo {
724e28a4053SRui Paulo 	wpa_printf(MSG_DEBUG, "EAP-SIM: Client replied to notification");
725e28a4053SRui Paulo 	if (data->notification == EAP_SIM_SUCCESS && data->use_result_ind)
726e28a4053SRui Paulo 		eap_sim_state(data, SUCCESS);
727e28a4053SRui Paulo 	else
728e28a4053SRui Paulo 		eap_sim_state(data, FAILURE);
729e28a4053SRui Paulo }
730e28a4053SRui Paulo 
731e28a4053SRui Paulo 
eap_sim_process(struct eap_sm * sm,void * priv,struct wpabuf * respData)732e28a4053SRui Paulo static void eap_sim_process(struct eap_sm *sm, void *priv,
733e28a4053SRui Paulo 			    struct wpabuf *respData)
734e28a4053SRui Paulo {
735e28a4053SRui Paulo 	struct eap_sim_data *data = priv;
736e28a4053SRui Paulo 	const u8 *pos, *end;
737e28a4053SRui Paulo 	u8 subtype;
738e28a4053SRui Paulo 	size_t len;
739e28a4053SRui Paulo 	struct eap_sim_attrs attr;
740e28a4053SRui Paulo 
741e28a4053SRui Paulo 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, respData, &len);
742e28a4053SRui Paulo 	if (pos == NULL || len < 3)
743e28a4053SRui Paulo 		return;
744e28a4053SRui Paulo 
745e28a4053SRui Paulo 	end = pos + len;
746e28a4053SRui Paulo 	subtype = *pos;
747e28a4053SRui Paulo 	pos += 3;
748e28a4053SRui Paulo 
749f05cddf9SRui Paulo 	if (eap_sim_unexpected_subtype(data, subtype)) {
750f05cddf9SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized or unexpected "
751f05cddf9SRui Paulo 			   "EAP-SIM Subtype in EAP Response");
752f05cddf9SRui Paulo 		data->notification = EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
753f05cddf9SRui Paulo 		eap_sim_state(data, NOTIFICATION);
754f05cddf9SRui Paulo 		return;
755f05cddf9SRui Paulo 	}
756f05cddf9SRui Paulo 
757e28a4053SRui Paulo 	if (eap_sim_parse_attr(pos, end, &attr, 0, 0)) {
758e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to parse attributes");
759f05cddf9SRui Paulo 		if (subtype != EAP_SIM_SUBTYPE_CLIENT_ERROR &&
760f05cddf9SRui Paulo 		    (data->state == START || data->state == CHALLENGE ||
761f05cddf9SRui Paulo 		     data->state == REAUTH)) {
762f05cddf9SRui Paulo 			data->notification =
763f05cddf9SRui Paulo 				EAP_SIM_GENERAL_FAILURE_BEFORE_AUTH;
764f05cddf9SRui Paulo 			eap_sim_state(data, NOTIFICATION);
765f05cddf9SRui Paulo 			return;
766f05cddf9SRui Paulo 		}
767e28a4053SRui Paulo 		eap_sim_state(data, FAILURE);
768e28a4053SRui Paulo 		return;
769e28a4053SRui Paulo 	}
770e28a4053SRui Paulo 
771e28a4053SRui Paulo 	if (subtype == EAP_SIM_SUBTYPE_CLIENT_ERROR) {
772e28a4053SRui Paulo 		eap_sim_process_client_error(sm, data, respData, &attr);
773e28a4053SRui Paulo 		return;
774e28a4053SRui Paulo 	}
775e28a4053SRui Paulo 
776e28a4053SRui Paulo 	switch (data->state) {
777e28a4053SRui Paulo 	case START:
778e28a4053SRui Paulo 		eap_sim_process_start(sm, data, respData, &attr);
779e28a4053SRui Paulo 		break;
780e28a4053SRui Paulo 	case CHALLENGE:
781e28a4053SRui Paulo 		eap_sim_process_challenge(sm, data, respData, &attr);
782e28a4053SRui Paulo 		break;
783e28a4053SRui Paulo 	case REAUTH:
784e28a4053SRui Paulo 		eap_sim_process_reauth(sm, data, respData, &attr);
785e28a4053SRui Paulo 		break;
786e28a4053SRui Paulo 	case NOTIFICATION:
787e28a4053SRui Paulo 		eap_sim_process_notification(sm, data, respData, &attr);
788e28a4053SRui Paulo 		break;
789e28a4053SRui Paulo 	default:
790e28a4053SRui Paulo 		wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown state %d in "
791e28a4053SRui Paulo 			   "process", data->state);
792e28a4053SRui Paulo 		break;
793e28a4053SRui Paulo 	}
794e28a4053SRui Paulo }
795e28a4053SRui Paulo 
796e28a4053SRui Paulo 
eap_sim_isDone(struct eap_sm * sm,void * priv)797*c1d255d3SCy Schubert static bool eap_sim_isDone(struct eap_sm *sm, void *priv)
798e28a4053SRui Paulo {
799e28a4053SRui Paulo 	struct eap_sim_data *data = priv;
800e28a4053SRui Paulo 	return data->state == SUCCESS || data->state == FAILURE;
801e28a4053SRui Paulo }
802e28a4053SRui Paulo 
803e28a4053SRui Paulo 
eap_sim_getKey(struct eap_sm * sm,void * priv,size_t * len)804e28a4053SRui Paulo static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
805e28a4053SRui Paulo {
806e28a4053SRui Paulo 	struct eap_sim_data *data = priv;
807e28a4053SRui Paulo 	u8 *key;
808e28a4053SRui Paulo 
809e28a4053SRui Paulo 	if (data->state != SUCCESS)
810e28a4053SRui Paulo 		return NULL;
811e28a4053SRui Paulo 
81285732ac8SCy Schubert 	key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN);
813e28a4053SRui Paulo 	if (key == NULL)
814e28a4053SRui Paulo 		return NULL;
815e28a4053SRui Paulo 	*len = EAP_SIM_KEYING_DATA_LEN;
816e28a4053SRui Paulo 	return key;
817e28a4053SRui Paulo }
818e28a4053SRui Paulo 
819e28a4053SRui Paulo 
eap_sim_get_emsk(struct eap_sm * sm,void * priv,size_t * len)820e28a4053SRui Paulo static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
821e28a4053SRui Paulo {
822e28a4053SRui Paulo 	struct eap_sim_data *data = priv;
823e28a4053SRui Paulo 	u8 *key;
824e28a4053SRui Paulo 
825e28a4053SRui Paulo 	if (data->state != SUCCESS)
826e28a4053SRui Paulo 		return NULL;
827e28a4053SRui Paulo 
82885732ac8SCy Schubert 	key = os_memdup(data->emsk, EAP_EMSK_LEN);
829e28a4053SRui Paulo 	if (key == NULL)
830e28a4053SRui Paulo 		return NULL;
831e28a4053SRui Paulo 	*len = EAP_EMSK_LEN;
832e28a4053SRui Paulo 	return key;
833e28a4053SRui Paulo }
834e28a4053SRui Paulo 
835e28a4053SRui Paulo 
eap_sim_isSuccess(struct eap_sm * sm,void * priv)836*c1d255d3SCy Schubert static bool eap_sim_isSuccess(struct eap_sm *sm, void *priv)
837e28a4053SRui Paulo {
838e28a4053SRui Paulo 	struct eap_sim_data *data = priv;
839e28a4053SRui Paulo 	return data->state == SUCCESS;
840e28a4053SRui Paulo }
841e28a4053SRui Paulo 
842e28a4053SRui Paulo 
eap_sim_get_session_id(struct eap_sm * sm,void * priv,size_t * len)8435b9c547cSRui Paulo static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
8445b9c547cSRui Paulo {
8455b9c547cSRui Paulo 	struct eap_sim_data *data = priv;
8465b9c547cSRui Paulo 	u8 *id;
8475b9c547cSRui Paulo 
8485b9c547cSRui Paulo 	if (data->state != SUCCESS)
8495b9c547cSRui Paulo 		return NULL;
8505b9c547cSRui Paulo 
851206b73d0SCy Schubert 	if (!data->reauth)
8525b9c547cSRui Paulo 		*len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN;
853206b73d0SCy Schubert 	else
854206b73d0SCy Schubert 		*len = 1 + EAP_SIM_NONCE_S_LEN + EAP_SIM_MAC_LEN;
8555b9c547cSRui Paulo 	id = os_malloc(*len);
8565b9c547cSRui Paulo 	if (id == NULL)
8575b9c547cSRui Paulo 		return NULL;
8585b9c547cSRui Paulo 
8595b9c547cSRui Paulo 	id[0] = EAP_TYPE_SIM;
860206b73d0SCy Schubert 	if (!data->reauth) {
8615b9c547cSRui Paulo 		os_memcpy(id + 1, data->rand, data->num_chal * GSM_RAND_LEN);
862206b73d0SCy Schubert 		os_memcpy(id + 1 + data->num_chal * GSM_RAND_LEN,
863206b73d0SCy Schubert 			  data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
864206b73d0SCy Schubert 	} else {
865206b73d0SCy Schubert 		os_memcpy(id + 1, data->nonce_s, EAP_SIM_NONCE_S_LEN);
866206b73d0SCy Schubert 		os_memcpy(id + 1 + EAP_SIM_NONCE_S_LEN, data->reauth_mac,
867206b73d0SCy Schubert 			  EAP_SIM_MAC_LEN);
868206b73d0SCy Schubert 
869206b73d0SCy Schubert 	}
8705b9c547cSRui Paulo 	wpa_hexdump(MSG_DEBUG, "EAP-SIM: Derived Session-Id", id, *len);
8715b9c547cSRui Paulo 
8725b9c547cSRui Paulo 	return id;
8735b9c547cSRui Paulo }
8745b9c547cSRui Paulo 
8755b9c547cSRui Paulo 
eap_server_sim_register(void)876e28a4053SRui Paulo int eap_server_sim_register(void)
877e28a4053SRui Paulo {
878e28a4053SRui Paulo 	struct eap_method *eap;
879e28a4053SRui Paulo 
880e28a4053SRui Paulo 	eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
881e28a4053SRui Paulo 				      EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
882e28a4053SRui Paulo 	if (eap == NULL)
883e28a4053SRui Paulo 		return -1;
884e28a4053SRui Paulo 
885e28a4053SRui Paulo 	eap->init = eap_sim_init;
886e28a4053SRui Paulo 	eap->reset = eap_sim_reset;
887e28a4053SRui Paulo 	eap->buildReq = eap_sim_buildReq;
888e28a4053SRui Paulo 	eap->check = eap_sim_check;
889e28a4053SRui Paulo 	eap->process = eap_sim_process;
890e28a4053SRui Paulo 	eap->isDone = eap_sim_isDone;
891e28a4053SRui Paulo 	eap->getKey = eap_sim_getKey;
892e28a4053SRui Paulo 	eap->isSuccess = eap_sim_isSuccess;
893e28a4053SRui Paulo 	eap->get_emsk = eap_sim_get_emsk;
8945b9c547cSRui Paulo 	eap->getSessionId = eap_sim_get_session_id;
895e28a4053SRui Paulo 
896780fb4a2SCy Schubert 	return eap_server_method_register(eap);
897e28a4053SRui Paulo }
898