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