1 /*
2  * EAP peer method: EAP-SIM (RFC 4186)
3  * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "pcsc_funcs.h"
13 #include "crypto/milenage.h"
14 #include "crypto/random.h"
15 #include "eap_peer/eap_i.h"
16 #include "eap_config.h"
17 #include "eap_common/eap_sim_common.h"
18 
19 
20 struct eap_sim_data {
21 	u8 *ver_list;
22 	size_t ver_list_len;
23 	int selected_version;
24 	size_t min_num_chal, num_chal;
25 
26 	u8 kc[3][EAP_SIM_KC_LEN];
27 	u8 sres[3][EAP_SIM_SRES_LEN];
28 	u8 nonce_mt[EAP_SIM_NONCE_MT_LEN], nonce_s[EAP_SIM_NONCE_S_LEN];
29 	u8 mk[EAP_SIM_MK_LEN];
30 	u8 k_aut[EAP_SIM_K_AUT_LEN];
31 	u8 k_encr[EAP_SIM_K_ENCR_LEN];
32 	u8 msk[EAP_SIM_KEYING_DATA_LEN];
33 	u8 emsk[EAP_EMSK_LEN];
34 	u8 rand[3][GSM_RAND_LEN];
35 	u8 reauth_mac[EAP_SIM_MAC_LEN];
36 
37 	int num_id_req, num_notification;
38 	u8 *pseudonym;
39 	size_t pseudonym_len;
40 	u8 *reauth_id;
41 	size_t reauth_id_len;
42 	int reauth;
43 	unsigned int counter, counter_too_small;
44 	u8 *last_eap_identity;
45 	size_t last_eap_identity_len;
46 	enum {
47 		CONTINUE, START_DONE, RESULT_SUCCESS, SUCCESS, FAILURE
48 	} state;
49 	int result_ind, use_result_ind;
50 	int use_pseudonym;
51 	int error_code;
52 };
53 
54 
55 #ifndef CONFIG_NO_STDOUT_DEBUG
eap_sim_state_txt(int state)56 static const char * eap_sim_state_txt(int state)
57 {
58 	switch (state) {
59 	case CONTINUE:
60 		return "CONTINUE";
61 	case START_DONE:
62 		return "START_DONE";
63 	case RESULT_SUCCESS:
64 		return "RESULT_SUCCESS";
65 	case SUCCESS:
66 		return "SUCCESS";
67 	case FAILURE:
68 		return "FAILURE";
69 	default:
70 		return "?";
71 	}
72 }
73 #endif /* CONFIG_NO_STDOUT_DEBUG */
74 
75 
eap_sim_state(struct eap_sim_data * data,int state)76 static void eap_sim_state(struct eap_sim_data *data, int state)
77 {
78 	wpa_printf(MSG_DEBUG, "EAP-SIM: %s -> %s",
79 		   eap_sim_state_txt(data->state),
80 		   eap_sim_state_txt(state));
81 	data->state = state;
82 }
83 
84 
eap_sim_init(struct eap_sm * sm)85 static void * eap_sim_init(struct eap_sm *sm)
86 {
87 	struct eap_sim_data *data;
88 	struct eap_peer_config *config = eap_get_config(sm);
89 
90 	data = os_zalloc(sizeof(*data));
91 	if (data == NULL)
92 		return NULL;
93 
94 	if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
95 		wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
96 			   "for NONCE_MT");
97 		os_free(data);
98 		return NULL;
99 	}
100 
101 	/* Zero is a valid error code, so we need to initialize */
102 	data->error_code = NO_EAP_METHOD_ERROR;
103 
104 	data->min_num_chal = 2;
105 	if (config && config->phase1) {
106 		char *pos = os_strstr(config->phase1, "sim_min_num_chal=");
107 		if (pos) {
108 			data->min_num_chal = atoi(pos + 17);
109 			if (data->min_num_chal < 2 || data->min_num_chal > 3) {
110 				wpa_printf(MSG_WARNING, "EAP-SIM: Invalid "
111 					   "sim_min_num_chal configuration "
112 					   "(%lu, expected 2 or 3)",
113 					   (unsigned long) data->min_num_chal);
114 				os_free(data);
115 				return NULL;
116 			}
117 			wpa_printf(MSG_DEBUG, "EAP-SIM: Set minimum number of "
118 				   "challenges to %lu",
119 				   (unsigned long) data->min_num_chal);
120 		}
121 
122 		data->result_ind = os_strstr(config->phase1, "result_ind=1") !=
123 			NULL;
124 	}
125 
126 	data->use_pseudonym = !sm->init_phase2;
127 	if (config && config->anonymous_identity && data->use_pseudonym) {
128 		data->pseudonym = os_malloc(config->anonymous_identity_len);
129 		if (data->pseudonym) {
130 			os_memcpy(data->pseudonym, config->anonymous_identity,
131 				  config->anonymous_identity_len);
132 			data->pseudonym_len = config->anonymous_identity_len;
133 		}
134 	}
135 
136 	eap_sim_state(data, CONTINUE);
137 
138 	return data;
139 }
140 
141 
eap_sim_clear_keys(struct eap_sim_data * data,int reauth)142 static void eap_sim_clear_keys(struct eap_sim_data *data, int reauth)
143 {
144 	if (!reauth) {
145 		os_memset(data->mk, 0, EAP_SIM_MK_LEN);
146 		os_memset(data->k_aut, 0, EAP_SIM_K_AUT_LEN);
147 		os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN);
148 	}
149 	os_memset(data->kc, 0, 3 * EAP_SIM_KC_LEN);
150 	os_memset(data->sres, 0, 3 * EAP_SIM_SRES_LEN);
151 	os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN);
152 	os_memset(data->emsk, 0, EAP_EMSK_LEN);
153 }
154 
155 
eap_sim_deinit(struct eap_sm * sm,void * priv)156 static void eap_sim_deinit(struct eap_sm *sm, void *priv)
157 {
158 	struct eap_sim_data *data = priv;
159 	if (data) {
160 		os_free(data->ver_list);
161 		os_free(data->pseudonym);
162 		os_free(data->reauth_id);
163 		os_free(data->last_eap_identity);
164 		eap_sim_clear_keys(data, 0);
165 		os_free(data);
166 	}
167 }
168 
169 
eap_sim_ext_sim_req(struct eap_sm * sm,struct eap_sim_data * data)170 static int eap_sim_ext_sim_req(struct eap_sm *sm, struct eap_sim_data *data)
171 {
172 	char req[200], *pos, *end;
173 	size_t i;
174 
175 	wpa_printf(MSG_DEBUG, "EAP-SIM: Use external SIM processing");
176 	pos = req;
177 	end = pos + sizeof(req);
178 	pos += os_snprintf(pos, end - pos, "GSM-AUTH");
179 	for (i = 0; i < data->num_chal; i++) {
180 		pos += os_snprintf(pos, end - pos, ":");
181 		pos += wpa_snprintf_hex(pos, end - pos, data->rand[i],
182 					GSM_RAND_LEN);
183 	}
184 
185 	eap_sm_request_sim(sm, req);
186 	return 1;
187 }
188 
189 
eap_sim_ext_sim_result(struct eap_sm * sm,struct eap_sim_data * data,struct eap_peer_config * conf)190 static int eap_sim_ext_sim_result(struct eap_sm *sm, struct eap_sim_data *data,
191 				  struct eap_peer_config *conf)
192 {
193 	char *resp, *pos;
194 	size_t i;
195 
196 	wpa_printf(MSG_DEBUG,
197 		   "EAP-SIM: Use result from external SIM processing");
198 
199 	resp = conf->external_sim_resp;
200 	conf->external_sim_resp = NULL;
201 
202 	if (os_strncmp(resp, "GSM-AUTH:", 9) != 0) {
203 		wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized external SIM processing response");
204 		os_free(resp);
205 		return -1;
206 	}
207 
208 	pos = resp + 9;
209 	for (i = 0; i < data->num_chal; i++) {
210 		wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
211 			    data->rand[i], GSM_RAND_LEN);
212 
213 		if (hexstr2bin(pos, data->kc[i], EAP_SIM_KC_LEN) < 0)
214 			goto invalid;
215 		wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
216 				data->kc[i], EAP_SIM_KC_LEN);
217 		pos += EAP_SIM_KC_LEN * 2;
218 		if (*pos != ':')
219 			goto invalid;
220 		pos++;
221 
222 		if (hexstr2bin(pos, data->sres[i], EAP_SIM_SRES_LEN) < 0)
223 			goto invalid;
224 		wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
225 				data->sres[i], EAP_SIM_SRES_LEN);
226 		pos += EAP_SIM_SRES_LEN * 2;
227 		if (i + 1 < data->num_chal) {
228 			if (*pos != ':')
229 				goto invalid;
230 			pos++;
231 		}
232 	}
233 
234 	os_free(resp);
235 	return 0;
236 
237 invalid:
238 	wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
239 	os_free(resp);
240 	return -1;
241 }
242 
243 
eap_sim_gsm_auth(struct eap_sm * sm,struct eap_sim_data * data)244 static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data)
245 {
246 	struct eap_peer_config *conf;
247 
248 	wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm");
249 
250 	conf = eap_get_config(sm);
251 	if (conf == NULL)
252 		return -1;
253 
254 	if (sm->external_sim) {
255 		if (conf->external_sim_resp)
256 			return eap_sim_ext_sim_result(sm, data, conf);
257 		else
258 			return eap_sim_ext_sim_req(sm, data);
259 	}
260 
261 #ifdef PCSC_FUNCS
262 	if (conf->pcsc) {
263 		if (scard_gsm_auth(sm->scard_ctx, data->rand[0],
264 				   data->sres[0], data->kc[0]) ||
265 		    scard_gsm_auth(sm->scard_ctx, data->rand[1],
266 				   data->sres[1], data->kc[1]) ||
267 		    (data->num_chal > 2 &&
268 		     scard_gsm_auth(sm->scard_ctx, data->rand[2],
269 				    data->sres[2], data->kc[2]))) {
270 			wpa_printf(MSG_DEBUG, "EAP-SIM: GSM SIM "
271 				   "authentication could not be completed");
272 			return -1;
273 		}
274 		return 0;
275 	}
276 #endif /* PCSC_FUNCS */
277 
278 #ifdef CONFIG_SIM_SIMULATOR
279 	if (conf->password) {
280 		u8 opc[16], k[16];
281 		const char *pos;
282 		size_t i;
283 		wpa_printf(MSG_DEBUG, "EAP-SIM: Use internal GSM-Milenage "
284 			   "implementation for authentication");
285 		if (conf->password_len < 65) {
286 			wpa_printf(MSG_DEBUG, "EAP-SIM: invalid GSM-Milenage "
287 				   "password");
288 			return -1;
289 		}
290 		pos = (const char *) conf->password;
291 		if (hexstr2bin(pos, k, 16))
292 			return -1;
293 		pos += 32;
294 		if (*pos != ':')
295 			return -1;
296 		pos++;
297 
298 		if (hexstr2bin(pos, opc, 16))
299 			return -1;
300 
301 		for (i = 0; i < data->num_chal; i++) {
302 			if (gsm_milenage(opc, k, data->rand[i],
303 					 data->sres[i], data->kc[i])) {
304 				wpa_printf(MSG_DEBUG, "EAP-SIM: "
305 					   "GSM-Milenage authentication "
306 					   "could not be completed");
307 				return -1;
308 			}
309 			wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
310 				    data->rand[i], GSM_RAND_LEN);
311 			wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
312 					data->sres[i], EAP_SIM_SRES_LEN);
313 			wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
314 					data->kc[i], EAP_SIM_KC_LEN);
315 		}
316 		return 0;
317 	}
318 #endif /* CONFIG_SIM_SIMULATOR */
319 
320 #ifdef CONFIG_SIM_HARDCODED
321 	/* These hardcoded Kc and SRES values are used for testing. RAND to
322 	 * KC/SREC mapping is very bogus as far as real authentication is
323 	 * concerned, but it is quite useful for cases where the AS is rotating
324 	 * the order of pre-configured values. */
325 	{
326 		size_t i;
327 
328 		wpa_printf(MSG_DEBUG, "EAP-SIM: Use hardcoded Kc and SRES "
329 			   "values for testing");
330 
331 		for (i = 0; i < data->num_chal; i++) {
332 			if (data->rand[i][0] == 0xaa) {
333 				os_memcpy(data->kc[i],
334 					  "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7",
335 					  EAP_SIM_KC_LEN);
336 				os_memcpy(data->sres[i], "\xd1\xd2\xd3\xd4",
337 					  EAP_SIM_SRES_LEN);
338 			} else if (data->rand[i][0] == 0xbb) {
339 				os_memcpy(data->kc[i],
340 					  "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
341 					  EAP_SIM_KC_LEN);
342 				os_memcpy(data->sres[i], "\xe1\xe2\xe3\xe4",
343 					  EAP_SIM_SRES_LEN);
344 			} else {
345 				os_memcpy(data->kc[i],
346 					  "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
347 					  EAP_SIM_KC_LEN);
348 				os_memcpy(data->sres[i], "\xf1\xf2\xf3\xf4",
349 					  EAP_SIM_SRES_LEN);
350 			}
351 		}
352 	}
353 
354 	return 0;
355 
356 #else /* CONFIG_SIM_HARDCODED */
357 
358 	wpa_printf(MSG_DEBUG, "EAP-SIM: No GSM authentication algorithm "
359 		   "enabled");
360 	return -1;
361 
362 #endif /* CONFIG_SIM_HARDCODED */
363 }
364 
365 
eap_sim_supported_ver(int version)366 static int eap_sim_supported_ver(int version)
367 {
368 	return version == EAP_SIM_VERSION;
369 }
370 
371 
372 #define CLEAR_PSEUDONYM	0x01
373 #define CLEAR_REAUTH_ID	0x02
374 #define CLEAR_EAP_ID	0x04
375 
eap_sim_clear_identities(struct eap_sm * sm,struct eap_sim_data * data,int id)376 static void eap_sim_clear_identities(struct eap_sm *sm,
377 				     struct eap_sim_data *data, int id)
378 {
379 	if ((id & CLEAR_PSEUDONYM) && data->pseudonym) {
380 		wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old pseudonym");
381 		os_free(data->pseudonym);
382 		data->pseudonym = NULL;
383 		data->pseudonym_len = 0;
384 		if (data->use_pseudonym)
385 			eap_set_anon_id(sm, NULL, 0);
386 	}
387 	if ((id & CLEAR_REAUTH_ID) && data->reauth_id) {
388 		wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old reauth_id");
389 		os_free(data->reauth_id);
390 		data->reauth_id = NULL;
391 		data->reauth_id_len = 0;
392 	}
393 	if ((id & CLEAR_EAP_ID) && data->last_eap_identity) {
394 		wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old eap_id");
395 		os_free(data->last_eap_identity);
396 		data->last_eap_identity = NULL;
397 		data->last_eap_identity_len = 0;
398 	}
399 }
400 
401 
eap_sim_learn_ids(struct eap_sm * sm,struct eap_sim_data * data,struct eap_sim_attrs * attr)402 static int eap_sim_learn_ids(struct eap_sm *sm, struct eap_sim_data *data,
403 			     struct eap_sim_attrs *attr)
404 {
405 	if (attr->next_pseudonym) {
406 		const u8 *identity = NULL;
407 		size_t identity_len = 0;
408 		const u8 *realm = NULL;
409 		size_t realm_len = 0;
410 
411 		wpa_hexdump_ascii(MSG_DEBUG,
412 				  "EAP-SIM: (encr) AT_NEXT_PSEUDONYM",
413 				  attr->next_pseudonym,
414 				  attr->next_pseudonym_len);
415 		os_free(data->pseudonym);
416 		/* Look for the realm of the permanent identity */
417 		identity = eap_get_config_identity(sm, &identity_len);
418 		if (identity) {
419 			for (realm = identity, realm_len = identity_len;
420 			     realm_len > 0; realm_len--, realm++) {
421 				if (*realm == '@')
422 					break;
423 			}
424 		}
425 		data->pseudonym = os_malloc(attr->next_pseudonym_len +
426 					    realm_len);
427 		if (data->pseudonym == NULL) {
428 			wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
429 				   "next pseudonym");
430 			data->pseudonym_len = 0;
431 			return -1;
432 		}
433 		os_memcpy(data->pseudonym, attr->next_pseudonym,
434 			  attr->next_pseudonym_len);
435 		if (realm_len) {
436 			os_memcpy(data->pseudonym + attr->next_pseudonym_len,
437 				  realm, realm_len);
438 		}
439 		data->pseudonym_len = attr->next_pseudonym_len + realm_len;
440 		if (data->use_pseudonym)
441 			eap_set_anon_id(sm, data->pseudonym,
442 					data->pseudonym_len);
443 	}
444 
445 	if (attr->next_reauth_id) {
446 		os_free(data->reauth_id);
447 		data->reauth_id = os_memdup(attr->next_reauth_id,
448 					    attr->next_reauth_id_len);
449 		if (data->reauth_id == NULL) {
450 			wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
451 				   "next reauth_id");
452 			data->reauth_id_len = 0;
453 			return -1;
454 		}
455 		data->reauth_id_len = attr->next_reauth_id_len;
456 		wpa_hexdump_ascii(MSG_DEBUG,
457 				  "EAP-SIM: (encr) AT_NEXT_REAUTH_ID",
458 				  data->reauth_id,
459 				  data->reauth_id_len);
460 	}
461 
462 	return 0;
463 }
464 
465 
eap_sim_client_error(struct eap_sim_data * data,u8 id,int err)466 static struct wpabuf * eap_sim_client_error(struct eap_sim_data *data, u8 id,
467 					    int err)
468 {
469 	struct eap_sim_msg *msg;
470 
471 	eap_sim_state(data, FAILURE);
472 	data->num_id_req = 0;
473 	data->num_notification = 0;
474 
475 	wpa_printf(MSG_DEBUG, "EAP-SIM: Send Client-Error (error code %d)",
476 		   err);
477 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
478 			       EAP_SIM_SUBTYPE_CLIENT_ERROR);
479 	eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
480 	return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
481 }
482 
483 
eap_sim_response_start(struct eap_sm * sm,struct eap_sim_data * data,u8 id,enum eap_sim_id_req id_req)484 static struct wpabuf * eap_sim_response_start(struct eap_sm *sm,
485 					      struct eap_sim_data *data, u8 id,
486 					      enum eap_sim_id_req id_req)
487 {
488 	const u8 *identity = NULL;
489 	size_t identity_len = 0;
490 	struct eap_sim_msg *msg;
491 	struct wpabuf *resp;
492 
493 	data->reauth = 0;
494 	if (id_req == ANY_ID && data->reauth_id) {
495 		identity = data->reauth_id;
496 		identity_len = data->reauth_id_len;
497 		data->reauth = 1;
498 	} else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
499 		   data->pseudonym &&
500 		   !eap_sim_anonymous_username(data->pseudonym,
501 					       data->pseudonym_len)) {
502 		identity = data->pseudonym;
503 		identity_len = data->pseudonym_len;
504 		eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID);
505 	} else if (id_req != NO_ID_REQ) {
506 		identity = eap_get_config_identity(sm, &identity_len);
507 		if (identity) {
508 			int ids = CLEAR_PSEUDONYM | CLEAR_REAUTH_ID;
509 
510 			if (data->pseudonym &&
511 			    eap_sim_anonymous_username(data->pseudonym,
512 						       data->pseudonym_len))
513 				ids &= ~CLEAR_PSEUDONYM;
514 			eap_sim_clear_identities(sm, data, ids);
515 		}
516 	}
517 	if (id_req != NO_ID_REQ)
518 		eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
519 
520 	wpa_printf(MSG_DEBUG, "Generating EAP-SIM Start (id=%d)", id);
521 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
522 			       EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START);
523 	if (identity) {
524 		wpa_hexdump_ascii(MSG_DEBUG, "   AT_IDENTITY",
525 				  identity, identity_len);
526 		eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
527 				identity, identity_len);
528 	}
529 	if (!data->reauth) {
530 		wpa_hexdump(MSG_DEBUG, "   AT_NONCE_MT",
531 			    data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
532 		eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_MT, 0,
533 				data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
534 		wpa_printf(MSG_DEBUG, "   AT_SELECTED_VERSION %d",
535 			   data->selected_version);
536 		eap_sim_msg_add(msg, EAP_SIM_AT_SELECTED_VERSION,
537 				data->selected_version, NULL, 0);
538 	}
539 
540 	resp = eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
541 	if (resp)
542 		eap_sim_state(data, START_DONE);
543 	return resp;
544 }
545 
546 
eap_sim_response_challenge(struct eap_sim_data * data,u8 id)547 static struct wpabuf * eap_sim_response_challenge(struct eap_sim_data *data,
548 						  u8 id)
549 {
550 	struct eap_sim_msg *msg;
551 
552 	wpa_printf(MSG_DEBUG, "Generating EAP-SIM Challenge (id=%d)", id);
553 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
554 			       EAP_SIM_SUBTYPE_CHALLENGE);
555 	if (data->use_result_ind) {
556 		wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
557 		eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
558 	}
559 	wpa_printf(MSG_DEBUG, "   AT_MAC");
560 	eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
561 	return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut,
562 				  (u8 *) data->sres,
563 				  data->num_chal * EAP_SIM_SRES_LEN);
564 }
565 
566 
eap_sim_response_reauth(struct eap_sim_data * data,u8 id,int counter_too_small,const u8 * nonce_s)567 static struct wpabuf * eap_sim_response_reauth(struct eap_sim_data *data,
568 					       u8 id, int counter_too_small,
569 					       const u8 *nonce_s)
570 {
571 	struct eap_sim_msg *msg;
572 	unsigned int counter;
573 
574 	wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)",
575 		   id);
576 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
577 			       EAP_SIM_SUBTYPE_REAUTHENTICATION);
578 	wpa_printf(MSG_DEBUG, "   AT_IV");
579 	wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
580 	eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
581 
582 	if (counter_too_small) {
583 		wpa_printf(MSG_DEBUG, "   *AT_COUNTER_TOO_SMALL");
584 		eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
585 		counter = data->counter_too_small;
586 	} else
587 		counter = data->counter;
588 
589 	wpa_printf(MSG_DEBUG, "   *AT_COUNTER %d", counter);
590 	eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
591 
592 	if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
593 		wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
594 			   "AT_ENCR_DATA");
595 		eap_sim_msg_free(msg);
596 		return NULL;
597 	}
598 	if (data->use_result_ind) {
599 		wpa_printf(MSG_DEBUG, "   AT_RESULT_IND");
600 		eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
601 	}
602 	wpa_printf(MSG_DEBUG, "   AT_MAC");
603 	eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
604 	return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, nonce_s,
605 				  EAP_SIM_NONCE_S_LEN);
606 }
607 
608 
eap_sim_response_notification(struct eap_sim_data * data,u8 id,u16 notification)609 static struct wpabuf * eap_sim_response_notification(struct eap_sim_data *data,
610 						     u8 id, u16 notification)
611 {
612 	struct eap_sim_msg *msg;
613 	u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
614 
615 	wpa_printf(MSG_DEBUG, "Generating EAP-SIM Notification (id=%d)", id);
616 	msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
617 			       EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION);
618 	if (k_aut && data->reauth) {
619 		wpa_printf(MSG_DEBUG, "   AT_IV");
620 		wpa_printf(MSG_DEBUG, "   AT_ENCR_DATA");
621 		eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
622 					   EAP_SIM_AT_ENCR_DATA);
623 		wpa_printf(MSG_DEBUG, "   *AT_COUNTER %d", data->counter);
624 		eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
625 				NULL, 0);
626 		if (eap_sim_msg_add_encr_end(msg, data->k_encr,
627 					     EAP_SIM_AT_PADDING)) {
628 			wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
629 				   "AT_ENCR_DATA");
630 			eap_sim_msg_free(msg);
631 			return NULL;
632 		}
633 	}
634 	if (k_aut) {
635 		wpa_printf(MSG_DEBUG, "   AT_MAC");
636 		eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
637 	}
638 	return eap_sim_msg_finish(msg, EAP_TYPE_SIM, k_aut, (u8 *) "", 0);
639 }
640 
641 
eap_sim_process_start(struct eap_sm * sm,struct eap_sim_data * data,u8 id,struct eap_sim_attrs * attr)642 static struct wpabuf * eap_sim_process_start(struct eap_sm *sm,
643 					     struct eap_sim_data *data, u8 id,
644 					     struct eap_sim_attrs *attr)
645 {
646 	int selected_version = -1, id_error;
647 	size_t i;
648 	u8 *pos;
649 
650 	wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Start");
651 	if (attr->version_list == NULL) {
652 		wpa_printf(MSG_INFO, "EAP-SIM: No AT_VERSION_LIST in "
653 			   "SIM/Start");
654 		return eap_sim_client_error(data, id,
655 					    EAP_SIM_UNSUPPORTED_VERSION);
656 	}
657 
658 	os_free(data->ver_list);
659 	data->ver_list = os_memdup(attr->version_list, attr->version_list_len);
660 	if (data->ver_list == NULL) {
661 		wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to allocate "
662 			   "memory for version list");
663 		return eap_sim_client_error(data, id,
664 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
665 	}
666 	data->ver_list_len = attr->version_list_len;
667 	pos = data->ver_list;
668 	for (i = 0; i < data->ver_list_len / 2; i++) {
669 		int ver = pos[0] * 256 + pos[1];
670 		pos += 2;
671 		if (eap_sim_supported_ver(ver)) {
672 			selected_version = ver;
673 			break;
674 		}
675 	}
676 	if (selected_version < 0) {
677 		wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported "
678 			   "version");
679 		return eap_sim_client_error(data, id,
680 					    EAP_SIM_UNSUPPORTED_VERSION);
681 	}
682 	wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d",
683 		   selected_version);
684 	data->selected_version = selected_version;
685 
686 	id_error = 0;
687 	switch (attr->id_req) {
688 	case NO_ID_REQ:
689 		break;
690 	case ANY_ID:
691 		if (data->num_id_req > 0)
692 			id_error++;
693 		data->num_id_req++;
694 		break;
695 	case FULLAUTH_ID:
696 		if (data->num_id_req > 1)
697 			id_error++;
698 		data->num_id_req++;
699 		break;
700 	case PERMANENT_ID:
701 		if (data->num_id_req > 2)
702 			id_error++;
703 		data->num_id_req++;
704 		break;
705 	}
706 	if (id_error) {
707 		wpa_printf(MSG_INFO, "EAP-SIM: Too many ID requests "
708 			   "used within one authentication");
709 		return eap_sim_client_error(data, id,
710 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
711 	}
712 
713 	return eap_sim_response_start(sm, data, id, attr->id_req);
714 }
715 
716 
eap_sim_process_challenge(struct eap_sm * sm,struct eap_sim_data * data,u8 id,const struct wpabuf * reqData,struct eap_sim_attrs * attr)717 static struct wpabuf * eap_sim_process_challenge(struct eap_sm *sm,
718 						 struct eap_sim_data *data,
719 						 u8 id,
720 						 const struct wpabuf *reqData,
721 						 struct eap_sim_attrs *attr)
722 {
723 	const u8 *identity;
724 	size_t identity_len;
725 	struct eap_sim_attrs eattr;
726 	int res;
727 
728 	wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge");
729 	if (data->state != START_DONE) {
730 		wpa_printf(MSG_DEBUG,
731 			   "EAP-SIM: Unexpected Challenge in state %s",
732 			   eap_sim_state_txt(data->state));
733 		return eap_sim_client_error(data, id,
734 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
735 	}
736 	data->reauth = 0;
737 	if (!attr->mac || !attr->rand) {
738 		wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
739 			   "did not include%s%s",
740 			   !attr->mac ? " AT_MAC" : "",
741 			   !attr->rand ? " AT_RAND" : "");
742 		return eap_sim_client_error(data, id,
743 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
744 	}
745 
746 	wpa_printf(MSG_DEBUG, "EAP-SIM: %lu challenges",
747 		   (unsigned long) attr->num_chal);
748 	if (attr->num_chal < data->min_num_chal) {
749 		wpa_printf(MSG_INFO, "EAP-SIM: Insufficient number of "
750 			   "challenges (%lu)", (unsigned long) attr->num_chal);
751 		return eap_sim_client_error(data, id,
752 					    EAP_SIM_INSUFFICIENT_NUM_OF_CHAL);
753 	}
754 	if (attr->num_chal > 3) {
755 		wpa_printf(MSG_INFO, "EAP-SIM: Too many challenges "
756 			   "(%lu)", (unsigned long) attr->num_chal);
757 		return eap_sim_client_error(data, id,
758 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
759 	}
760 
761 	/* Verify that RANDs are different */
762 	if (os_memcmp(attr->rand, attr->rand + GSM_RAND_LEN,
763 		   GSM_RAND_LEN) == 0 ||
764 	    (attr->num_chal > 2 &&
765 	     (os_memcmp(attr->rand, attr->rand + 2 * GSM_RAND_LEN,
766 			GSM_RAND_LEN) == 0 ||
767 	      os_memcmp(attr->rand + GSM_RAND_LEN,
768 			attr->rand + 2 * GSM_RAND_LEN,
769 			GSM_RAND_LEN) == 0))) {
770 		wpa_printf(MSG_INFO, "EAP-SIM: Same RAND used multiple times");
771 		return eap_sim_client_error(data, id,
772 					    EAP_SIM_RAND_NOT_FRESH);
773 	}
774 
775 	os_memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN);
776 	data->num_chal = attr->num_chal;
777 
778 	res = eap_sim_gsm_auth(sm, data);
779 	if (res > 0) {
780 		wpa_printf(MSG_DEBUG, "EAP-SIM: Wait for external SIM processing");
781 		return NULL;
782 	}
783 	if (res) {
784 		wpa_printf(MSG_WARNING, "EAP-SIM: GSM authentication failed");
785 		return eap_sim_client_error(data, id,
786 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
787 	}
788 	if (data->last_eap_identity) {
789 		identity = data->last_eap_identity;
790 		identity_len = data->last_eap_identity_len;
791 	} else if (data->pseudonym &&
792 		   !eap_sim_anonymous_username(data->pseudonym,
793 					       data->pseudonym_len)) {
794 		identity = data->pseudonym;
795 		identity_len = data->pseudonym_len;
796 	} else {
797 		struct eap_peer_config *config;
798 
799 		config = eap_get_config(sm);
800 		if (config && config->imsi_identity) {
801 			identity = config->imsi_identity;
802 			identity_len = config->imsi_identity_len;
803 		} else {
804 			identity = eap_get_config_identity(sm, &identity_len);
805 		}
806 	}
807 	wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Selected identity for MK "
808 			  "derivation", identity, identity_len);
809 	eap_sim_derive_mk(identity, identity_len, data->nonce_mt,
810 			  data->selected_version, data->ver_list,
811 			  data->ver_list_len, data->num_chal,
812 			  (const u8 *) data->kc, data->mk);
813 	eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
814 			    data->emsk);
815 	if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, data->nonce_mt,
816 			       EAP_SIM_NONCE_MT_LEN)) {
817 		wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
818 			   "used invalid AT_MAC");
819 #ifdef TEST_FUZZ
820 		wpa_printf(MSG_INFO,
821 			   "TEST: Ignore AT_MAC mismatch for fuzz testing");
822 #else /* TEST_FUZZ */
823 		return eap_sim_client_error(data, id,
824 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
825 #endif /* TEST_FUZZ */
826 	}
827 
828 	/* Old reauthentication identity must not be used anymore. In
829 	 * other words, if no new reauth identity is received, full
830 	 * authentication will be used on next reauthentication (using
831 	 * pseudonym identity or permanent identity). */
832 	eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
833 
834 	if (attr->encr_data) {
835 		u8 *decrypted;
836 		decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
837 					       attr->encr_data_len, attr->iv,
838 					       &eattr, 0);
839 		if (decrypted == NULL) {
840 			return eap_sim_client_error(
841 				data, id, EAP_SIM_UNABLE_TO_PROCESS_PACKET);
842 		}
843 		eap_sim_learn_ids(sm, data, &eattr);
844 		os_free(decrypted);
845 	}
846 
847 	if (data->result_ind && attr->result_ind)
848 		data->use_result_ind = 1;
849 
850 	if (data->state != FAILURE) {
851 		eap_sim_state(data, data->use_result_ind ?
852 			      RESULT_SUCCESS : SUCCESS);
853 	}
854 
855 	data->num_id_req = 0;
856 	data->num_notification = 0;
857 	/* RFC 4186 specifies that counter is initialized to one after
858 	 * fullauth, but initializing it to zero makes it easier to implement
859 	 * reauth verification. */
860 	data->counter = 0;
861 	return eap_sim_response_challenge(data, id);
862 }
863 
864 
eap_sim_process_notification_reauth(struct eap_sim_data * data,struct eap_sim_attrs * attr)865 static int eap_sim_process_notification_reauth(struct eap_sim_data *data,
866 					       struct eap_sim_attrs *attr)
867 {
868 	struct eap_sim_attrs eattr;
869 	u8 *decrypted;
870 
871 	if (attr->encr_data == NULL || attr->iv == NULL) {
872 		wpa_printf(MSG_WARNING, "EAP-SIM: Notification message after "
873 			   "reauth did not include encrypted data");
874 		return -1;
875 	}
876 
877 	decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
878 				       attr->encr_data_len, attr->iv, &eattr,
879 				       0);
880 	if (decrypted == NULL) {
881 		wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
882 			   "data from notification message");
883 		return -1;
884 	}
885 
886 	if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
887 		wpa_printf(MSG_WARNING, "EAP-SIM: Counter in notification "
888 			   "message does not match with counter in reauth "
889 			   "message");
890 		os_free(decrypted);
891 		return -1;
892 	}
893 
894 	os_free(decrypted);
895 	return 0;
896 }
897 
898 
eap_sim_process_notification_auth(struct eap_sim_data * data,const struct wpabuf * reqData,struct eap_sim_attrs * attr)899 static int eap_sim_process_notification_auth(struct eap_sim_data *data,
900 					     const struct wpabuf *reqData,
901 					     struct eap_sim_attrs *attr)
902 {
903 	if (attr->mac == NULL) {
904 		wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth "
905 			   "Notification message");
906 		return -1;
907 	}
908 
909 	if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
910 	{
911 		wpa_printf(MSG_WARNING, "EAP-SIM: Notification message "
912 			   "used invalid AT_MAC");
913 		return -1;
914 	}
915 
916 	if (data->reauth &&
917 	    eap_sim_process_notification_reauth(data, attr)) {
918 		wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification "
919 			   "message after reauth");
920 		return -1;
921 	}
922 
923 	return 0;
924 }
925 
926 
eap_sim_process_notification(struct eap_sm * sm,struct eap_sim_data * data,u8 id,const struct wpabuf * reqData,struct eap_sim_attrs * attr)927 static struct wpabuf * eap_sim_process_notification(
928 	struct eap_sm *sm, struct eap_sim_data *data, u8 id,
929 	const struct wpabuf *reqData, struct eap_sim_attrs *attr)
930 {
931 	wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Notification");
932 	if (data->num_notification > 0) {
933 		wpa_printf(MSG_INFO, "EAP-SIM: too many notification "
934 			   "rounds (only one allowed)");
935 		return eap_sim_client_error(data, id,
936 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
937 	}
938 	data->num_notification++;
939 	if (attr->notification == -1) {
940 		wpa_printf(MSG_INFO, "EAP-SIM: no AT_NOTIFICATION in "
941 			   "Notification message");
942 		return eap_sim_client_error(data, id,
943 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
944 	}
945 
946 	if ((attr->notification & 0x4000) == 0 &&
947 	    eap_sim_process_notification_auth(data, reqData, attr)) {
948 		return eap_sim_client_error(data, id,
949 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
950 	}
951 
952 	eap_sim_report_notification(sm->msg_ctx, attr->notification, 0);
953 	if (attr->notification >= 0 && attr->notification < 32768) {
954 		data->error_code = attr->notification;
955 		eap_sim_state(data, FAILURE);
956 	} else if (attr->notification == EAP_SIM_SUCCESS &&
957 		   data->state == RESULT_SUCCESS)
958 		eap_sim_state(data, SUCCESS);
959 	return eap_sim_response_notification(data, id, attr->notification);
960 }
961 
962 
eap_sim_process_reauthentication(struct eap_sm * sm,struct eap_sim_data * data,u8 id,const struct wpabuf * reqData,struct eap_sim_attrs * attr)963 static struct wpabuf * eap_sim_process_reauthentication(
964 	struct eap_sm *sm, struct eap_sim_data *data, u8 id,
965 	const struct wpabuf *reqData, struct eap_sim_attrs *attr)
966 {
967 	struct eap_sim_attrs eattr;
968 	u8 *decrypted;
969 
970 	wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication");
971 
972 	if (data->reauth_id == NULL) {
973 		wpa_printf(MSG_WARNING, "EAP-SIM: Server is trying "
974 			   "reauthentication, but no reauth_id available");
975 		return eap_sim_client_error(data, id,
976 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
977 	}
978 
979 	data->reauth = 1;
980 	if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
981 	{
982 		wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
983 			   "did not have valid AT_MAC");
984 #ifdef TEST_FUZZ
985 		wpa_printf(MSG_INFO,
986 			   "TEST: Ignore AT_MAC mismatch for fuzz testing");
987 #else /* TEST_FUZZ */
988 		return eap_sim_client_error(data, id,
989 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
990 #endif /* TEST_FUZZ */
991 	}
992 
993 	/* At this stage the received MAC has been verified. Use this MAC for
994 	 * reauth Session-Id calculation if all other checks pass.
995 	 * The peer does not use the local MAC but the received MAC in deriving
996 	 * Session-Id. */
997 #ifdef TEST_FUZZ
998 	if (attr->mac)
999 		os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN);
1000 	else
1001 		os_memset(data->reauth_mac, 0x12, EAP_SIM_MAC_LEN);
1002 #else /* TEST_FUZZ */
1003 	os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN);
1004 #endif /* TEST_FUZZ */
1005 	wpa_hexdump(MSG_DEBUG, "EAP-SIM: Server MAC",
1006 		    data->reauth_mac, EAP_SIM_MAC_LEN);
1007 
1008 	if (attr->encr_data == NULL || attr->iv == NULL) {
1009 		wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
1010 			   "message did not include encrypted data");
1011 		return eap_sim_client_error(data, id,
1012 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1013 	}
1014 
1015 	decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
1016 				       attr->encr_data_len, attr->iv, &eattr,
1017 				       0);
1018 	if (decrypted == NULL) {
1019 		wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
1020 			   "data from reauthentication message");
1021 		return eap_sim_client_error(data, id,
1022 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1023 	}
1024 
1025 	if (eattr.nonce_s == NULL || eattr.counter < 0) {
1026 		wpa_printf(MSG_INFO, "EAP-SIM: (encr) No%s%s in reauth packet",
1027 			   !eattr.nonce_s ? " AT_NONCE_S" : "",
1028 			   eattr.counter < 0 ? " AT_COUNTER" : "");
1029 		os_free(decrypted);
1030 		return eap_sim_client_error(data, id,
1031 					    EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1032 	}
1033 
1034 	if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
1035 		struct wpabuf *res;
1036 		wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid counter "
1037 			   "(%d <= %d)", eattr.counter, data->counter);
1038 		data->counter_too_small = eattr.counter;
1039 
1040 		/* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
1041 		 * reauth_id must not be used to start a new reauthentication.
1042 		 * However, since it was used in the last EAP-Response-Identity
1043 		 * packet, it has to saved for the following fullauth to be
1044 		 * used in MK derivation. */
1045 		os_free(data->last_eap_identity);
1046 		data->last_eap_identity = data->reauth_id;
1047 		data->last_eap_identity_len = data->reauth_id_len;
1048 		data->reauth_id = NULL;
1049 		data->reauth_id_len = 0;
1050 
1051 		res = eap_sim_response_reauth(data, id, 1, eattr.nonce_s);
1052 		os_free(decrypted);
1053 
1054 		return res;
1055 	}
1056 	data->counter = eattr.counter;
1057 
1058 	os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
1059 	wpa_hexdump(MSG_DEBUG, "EAP-SIM: (encr) AT_NONCE_S",
1060 		    data->nonce_s, EAP_SIM_NONCE_S_LEN);
1061 
1062 	eap_sim_derive_keys_reauth(data->counter,
1063 				   data->reauth_id, data->reauth_id_len,
1064 				   data->nonce_s, data->mk, data->msk,
1065 				   data->emsk);
1066 	eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1067 	eap_sim_learn_ids(sm, data, &eattr);
1068 
1069 	if (data->result_ind && attr->result_ind)
1070 		data->use_result_ind = 1;
1071 
1072 	if (data->state != FAILURE) {
1073 		eap_sim_state(data, data->use_result_ind ?
1074 			      RESULT_SUCCESS : SUCCESS);
1075 	}
1076 
1077 	data->num_id_req = 0;
1078 	data->num_notification = 0;
1079 	if (data->counter > EAP_SIM_MAX_FAST_REAUTHS) {
1080 		wpa_printf(MSG_DEBUG, "EAP-SIM: Maximum number of "
1081 			   "fast reauths performed - force fullauth");
1082 		eap_sim_clear_identities(sm, data,
1083 					 CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1084 	}
1085 	os_free(decrypted);
1086 	return eap_sim_response_reauth(data, id, 0, data->nonce_s);
1087 }
1088 
1089 
eap_sim_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)1090 static struct wpabuf * eap_sim_process(struct eap_sm *sm, void *priv,
1091 				       struct eap_method_ret *ret,
1092 				       const struct wpabuf *reqData)
1093 {
1094 	struct eap_sim_data *data = priv;
1095 	const struct eap_hdr *req;
1096 	u8 subtype, id;
1097 	struct wpabuf *res;
1098 	const u8 *pos;
1099 	struct eap_sim_attrs attr;
1100 	size_t len;
1101 
1102 	wpa_hexdump_buf(MSG_DEBUG, "EAP-SIM: EAP data", reqData);
1103 	if (eap_get_config_identity(sm, &len) == NULL) {
1104 		wpa_printf(MSG_INFO, "EAP-SIM: Identity not configured");
1105 		eap_sm_request_identity(sm);
1106 		ret->ignore = true;
1107 		return NULL;
1108 	}
1109 
1110 	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, reqData, &len);
1111 	if (pos == NULL || len < 3) {
1112 		ret->ignore = true;
1113 		return NULL;
1114 	}
1115 	req = wpabuf_head(reqData);
1116 	id = req->identifier;
1117 	len = be_to_host16(req->length);
1118 
1119 	ret->ignore = false;
1120 	ret->methodState = METHOD_MAY_CONT;
1121 	ret->decision = DECISION_FAIL;
1122 	ret->allowNotifications = true;
1123 
1124 	subtype = *pos++;
1125 	wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype);
1126 	pos += 2; /* Reserved */
1127 
1128 	if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 0,
1129 			       0)) {
1130 		res = eap_sim_client_error(data, id,
1131 					   EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1132 		goto done;
1133 	}
1134 
1135 	switch (subtype) {
1136 	case EAP_SIM_SUBTYPE_START:
1137 		res = eap_sim_process_start(sm, data, id, &attr);
1138 		break;
1139 	case EAP_SIM_SUBTYPE_CHALLENGE:
1140 		res = eap_sim_process_challenge(sm, data, id, reqData, &attr);
1141 		break;
1142 	case EAP_SIM_SUBTYPE_NOTIFICATION:
1143 		res = eap_sim_process_notification(sm, data, id, reqData,
1144 						   &attr);
1145 		break;
1146 	case EAP_SIM_SUBTYPE_REAUTHENTICATION:
1147 		res = eap_sim_process_reauthentication(sm, data, id, reqData,
1148 						       &attr);
1149 		break;
1150 	case EAP_SIM_SUBTYPE_CLIENT_ERROR:
1151 		wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Client-Error");
1152 		res = eap_sim_client_error(data, id,
1153 					   EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1154 		break;
1155 	default:
1156 		wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown subtype=%d", subtype);
1157 		res = eap_sim_client_error(data, id,
1158 					   EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1159 		break;
1160 	}
1161 
1162 done:
1163 	if (data->state == FAILURE) {
1164 		ret->decision = DECISION_FAIL;
1165 		ret->methodState = METHOD_DONE;
1166 	} else if (data->state == SUCCESS) {
1167 		ret->decision = data->use_result_ind ?
1168 			DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
1169 		ret->methodState = data->use_result_ind ?
1170 			METHOD_DONE : METHOD_MAY_CONT;
1171 	} else if (data->state == RESULT_SUCCESS)
1172 		ret->methodState = METHOD_CONT;
1173 
1174 	if (ret->methodState == METHOD_DONE) {
1175 		ret->allowNotifications = false;
1176 	}
1177 
1178 	return res;
1179 }
1180 
1181 
eap_sim_has_reauth_data(struct eap_sm * sm,void * priv)1182 static bool eap_sim_has_reauth_data(struct eap_sm *sm, void *priv)
1183 {
1184 	struct eap_sim_data *data = priv;
1185 	return data->pseudonym || data->reauth_id;
1186 }
1187 
1188 
eap_sim_deinit_for_reauth(struct eap_sm * sm,void * priv)1189 static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv)
1190 {
1191 	struct eap_sim_data *data = priv;
1192 	eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
1193 	data->use_result_ind = 0;
1194 	eap_sim_clear_keys(data, 1);
1195 }
1196 
1197 
eap_sim_init_for_reauth(struct eap_sm * sm,void * priv)1198 static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv)
1199 {
1200 	struct eap_sim_data *data = priv;
1201 	if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
1202 		wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
1203 			   "for NONCE_MT");
1204 		eap_sim_deinit(sm, data);
1205 		return NULL;
1206 	}
1207 	data->num_id_req = 0;
1208 	data->num_notification = 0;
1209 	eap_sim_state(data, CONTINUE);
1210 	return priv;
1211 }
1212 
1213 
eap_sim_get_identity(struct eap_sm * sm,void * priv,size_t * len)1214 static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv,
1215 				       size_t *len)
1216 {
1217 	struct eap_sim_data *data = priv;
1218 
1219 	if (data->reauth_id) {
1220 		*len = data->reauth_id_len;
1221 		return data->reauth_id;
1222 	}
1223 
1224 	if (data->pseudonym) {
1225 		*len = data->pseudonym_len;
1226 		return data->pseudonym;
1227 	}
1228 
1229 	return NULL;
1230 }
1231 
1232 
eap_sim_isKeyAvailable(struct eap_sm * sm,void * priv)1233 static bool eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv)
1234 {
1235 	struct eap_sim_data *data = priv;
1236 	return data->state == SUCCESS;
1237 }
1238 
1239 
eap_sim_getKey(struct eap_sm * sm,void * priv,size_t * len)1240 static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
1241 {
1242 	struct eap_sim_data *data = priv;
1243 	u8 *key;
1244 
1245 	if (data->state != SUCCESS)
1246 		return NULL;
1247 
1248 	key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN);
1249 	if (key == NULL)
1250 		return NULL;
1251 
1252 	*len = EAP_SIM_KEYING_DATA_LEN;
1253 
1254 	return key;
1255 }
1256 
1257 
eap_sim_get_session_id(struct eap_sm * sm,void * priv,size_t * len)1258 static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1259 {
1260 	struct eap_sim_data *data = priv;
1261 	u8 *id;
1262 
1263 	if (data->state != SUCCESS)
1264 		return NULL;
1265 
1266 	if (!data->reauth)
1267 		*len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN;
1268 	else
1269 		*len = 1 + EAP_SIM_NONCE_S_LEN + EAP_SIM_MAC_LEN;
1270 	id = os_malloc(*len);
1271 	if (id == NULL)
1272 		return NULL;
1273 
1274 	id[0] = EAP_TYPE_SIM;
1275 	if (!data->reauth) {
1276 		os_memcpy(id + 1, data->rand, data->num_chal * GSM_RAND_LEN);
1277 		os_memcpy(id + 1 + data->num_chal * GSM_RAND_LEN,
1278 			  data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
1279 	} else {
1280 		os_memcpy(id + 1, data->nonce_s, EAP_SIM_NONCE_S_LEN);
1281 		os_memcpy(id + 1 + EAP_SIM_NONCE_S_LEN, data->reauth_mac,
1282 			  EAP_SIM_MAC_LEN);
1283 	}
1284 	wpa_hexdump(MSG_DEBUG, "EAP-SIM: Derived Session-Id", id, *len);
1285 
1286 	return id;
1287 }
1288 
1289 
eap_sim_get_emsk(struct eap_sm * sm,void * priv,size_t * len)1290 static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1291 {
1292 	struct eap_sim_data *data = priv;
1293 	u8 *key;
1294 
1295 	if (data->state != SUCCESS)
1296 		return NULL;
1297 
1298 	key = os_memdup(data->emsk, EAP_EMSK_LEN);
1299 	if (key == NULL)
1300 		return NULL;
1301 
1302 	*len = EAP_EMSK_LEN;
1303 
1304 	return key;
1305 }
1306 
1307 
eap_sim_get_error_code(void * priv)1308 static int eap_sim_get_error_code(void *priv)
1309 {
1310 	struct eap_sim_data *data = priv;
1311 	int current_data_error;
1312 
1313 	if (!data)
1314 		return NO_EAP_METHOD_ERROR;
1315 
1316 	current_data_error = data->error_code;
1317 
1318 	/* Now reset for next transaction */
1319 	data->error_code = NO_EAP_METHOD_ERROR;
1320 
1321 	return current_data_error;
1322 }
1323 
1324 
eap_peer_sim_register(void)1325 int eap_peer_sim_register(void)
1326 {
1327 	struct eap_method *eap;
1328 
1329 	eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1330 				    EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
1331 	if (eap == NULL)
1332 		return -1;
1333 
1334 	eap->init = eap_sim_init;
1335 	eap->deinit = eap_sim_deinit;
1336 	eap->process = eap_sim_process;
1337 	eap->isKeyAvailable = eap_sim_isKeyAvailable;
1338 	eap->getKey = eap_sim_getKey;
1339 	eap->getSessionId = eap_sim_get_session_id;
1340 	eap->has_reauth_data = eap_sim_has_reauth_data;
1341 	eap->deinit_for_reauth = eap_sim_deinit_for_reauth;
1342 	eap->init_for_reauth = eap_sim_init_for_reauth;
1343 	eap->get_identity = eap_sim_get_identity;
1344 	eap->get_emsk = eap_sim_get_emsk;
1345 	eap->get_error_code = eap_sim_get_error_code;
1346 
1347 	return eap_peer_method_register(eap);
1348 }
1349