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