2 * EAP peer method: EAP-SIM (RFC 4186)
3 * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
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"
24 size_t min_num_chal, num_chal;
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];
37 int num_id_req, num_notification;
43 unsigned int counter, counter_too_small;
44 u8 *last_eap_identity;
45 size_t last_eap_identity_len;
47 CONTINUE, RESULT_SUCCESS, SUCCESS, FAILURE
49 int result_ind, use_result_ind;
55 #ifndef CONFIG_NO_STDOUT_DEBUG
56 static const char * eap_sim_state_txt(int state)
62 return "RESULT_SUCCESS";
71 #endif /* CONFIG_NO_STDOUT_DEBUG */
74 static void eap_sim_state(struct eap_sim_data *data, int state)
76 wpa_printf(MSG_DEBUG, "EAP-SIM: %s -> %s",
77 eap_sim_state_txt(data->state),
78 eap_sim_state_txt(state));
83 static void * eap_sim_init(struct eap_sm *sm)
85 struct eap_sim_data *data;
86 struct eap_peer_config *config = eap_get_config(sm);
88 data = os_zalloc(sizeof(*data));
92 if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
93 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
99 /* Zero is a valid error code, so we need to initialize */
100 data->error_code = NO_EAP_METHOD_ERROR;
102 data->min_num_chal = 2;
103 if (config && config->phase1) {
104 char *pos = os_strstr(config->phase1, "sim_min_num_chal=");
106 data->min_num_chal = atoi(pos + 17);
107 if (data->min_num_chal < 2 || data->min_num_chal > 3) {
108 wpa_printf(MSG_WARNING, "EAP-SIM: Invalid "
109 "sim_min_num_chal configuration "
110 "(%lu, expected 2 or 3)",
111 (unsigned long) data->min_num_chal);
115 wpa_printf(MSG_DEBUG, "EAP-SIM: Set minimum number of "
117 (unsigned long) data->min_num_chal);
120 data->result_ind = os_strstr(config->phase1, "result_ind=1") !=
124 data->use_pseudonym = !sm->init_phase2;
125 if (config && config->anonymous_identity && data->use_pseudonym) {
126 data->pseudonym = os_malloc(config->anonymous_identity_len);
127 if (data->pseudonym) {
128 os_memcpy(data->pseudonym, config->anonymous_identity,
129 config->anonymous_identity_len);
130 data->pseudonym_len = config->anonymous_identity_len;
134 eap_sim_state(data, CONTINUE);
140 static void eap_sim_clear_keys(struct eap_sim_data *data, int reauth)
143 os_memset(data->mk, 0, EAP_SIM_MK_LEN);
144 os_memset(data->k_aut, 0, EAP_SIM_K_AUT_LEN);
145 os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN);
147 os_memset(data->kc, 0, 3 * EAP_SIM_KC_LEN);
148 os_memset(data->sres, 0, 3 * EAP_SIM_SRES_LEN);
149 os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN);
150 os_memset(data->emsk, 0, EAP_EMSK_LEN);
154 static void eap_sim_deinit(struct eap_sm *sm, void *priv)
156 struct eap_sim_data *data = priv;
158 os_free(data->ver_list);
159 os_free(data->pseudonym);
160 os_free(data->reauth_id);
161 os_free(data->last_eap_identity);
162 eap_sim_clear_keys(data, 0);
168 static int eap_sim_ext_sim_req(struct eap_sm *sm, struct eap_sim_data *data)
170 char req[200], *pos, *end;
173 wpa_printf(MSG_DEBUG, "EAP-SIM: Use external SIM processing");
175 end = pos + sizeof(req);
176 pos += os_snprintf(pos, end - pos, "GSM-AUTH");
177 for (i = 0; i < data->num_chal; i++) {
178 pos += os_snprintf(pos, end - pos, ":");
179 pos += wpa_snprintf_hex(pos, end - pos, data->rand[i],
183 eap_sm_request_sim(sm, req);
188 static int eap_sim_ext_sim_result(struct eap_sm *sm, struct eap_sim_data *data,
189 struct eap_peer_config *conf)
194 wpa_printf(MSG_DEBUG,
195 "EAP-SIM: Use result from external SIM processing");
197 resp = conf->external_sim_resp;
198 conf->external_sim_resp = NULL;
200 if (os_strncmp(resp, "GSM-AUTH:", 9) != 0) {
201 wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized external SIM processing response");
207 for (i = 0; i < data->num_chal; i++) {
208 wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
209 data->rand[i], GSM_RAND_LEN);
211 if (hexstr2bin(pos, data->kc[i], EAP_SIM_KC_LEN) < 0)
213 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
214 data->kc[i], EAP_SIM_KC_LEN);
215 pos += EAP_SIM_KC_LEN * 2;
220 if (hexstr2bin(pos, data->sres[i], EAP_SIM_SRES_LEN) < 0)
222 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
223 data->sres[i], EAP_SIM_SRES_LEN);
224 pos += EAP_SIM_SRES_LEN * 2;
225 if (i + 1 < data->num_chal) {
236 wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
242 static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data)
244 struct eap_peer_config *conf;
246 wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm");
248 conf = eap_get_config(sm);
252 if (sm->external_sim) {
253 if (conf->external_sim_resp)
254 return eap_sim_ext_sim_result(sm, data, conf);
256 return eap_sim_ext_sim_req(sm, data);
261 if (scard_gsm_auth(sm->scard_ctx, data->rand[0],
262 data->sres[0], data->kc[0]) ||
263 scard_gsm_auth(sm->scard_ctx, data->rand[1],
264 data->sres[1], data->kc[1]) ||
265 (data->num_chal > 2 &&
266 scard_gsm_auth(sm->scard_ctx, data->rand[2],
267 data->sres[2], data->kc[2]))) {
268 wpa_printf(MSG_DEBUG, "EAP-SIM: GSM SIM "
269 "authentication could not be completed");
274 #endif /* PCSC_FUNCS */
276 #ifdef CONFIG_SIM_SIMULATOR
277 if (conf->password) {
281 wpa_printf(MSG_DEBUG, "EAP-SIM: Use internal GSM-Milenage "
282 "implementation for authentication");
283 if (conf->password_len < 65) {
284 wpa_printf(MSG_DEBUG, "EAP-SIM: invalid GSM-Milenage "
288 pos = (const char *) conf->password;
289 if (hexstr2bin(pos, k, 16))
296 if (hexstr2bin(pos, opc, 16))
299 for (i = 0; i < data->num_chal; i++) {
300 if (gsm_milenage(opc, k, data->rand[i],
301 data->sres[i], data->kc[i])) {
302 wpa_printf(MSG_DEBUG, "EAP-SIM: "
303 "GSM-Milenage authentication "
304 "could not be completed");
307 wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
308 data->rand[i], GSM_RAND_LEN);
309 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
310 data->sres[i], EAP_SIM_SRES_LEN);
311 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
312 data->kc[i], EAP_SIM_KC_LEN);
316 #endif /* CONFIG_SIM_SIMULATOR */
318 #ifdef CONFIG_SIM_HARDCODED
319 /* These hardcoded Kc and SRES values are used for testing. RAND to
320 * KC/SREC mapping is very bogus as far as real authentication is
321 * concerned, but it is quite useful for cases where the AS is rotating
322 * the order of pre-configured values. */
326 wpa_printf(MSG_DEBUG, "EAP-SIM: Use hardcoded Kc and SRES "
327 "values for testing");
329 for (i = 0; i < data->num_chal; i++) {
330 if (data->rand[i][0] == 0xaa) {
331 os_memcpy(data->kc[i],
332 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7",
334 os_memcpy(data->sres[i], "\xd1\xd2\xd3\xd4",
336 } else if (data->rand[i][0] == 0xbb) {
337 os_memcpy(data->kc[i],
338 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
340 os_memcpy(data->sres[i], "\xe1\xe2\xe3\xe4",
343 os_memcpy(data->kc[i],
344 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
346 os_memcpy(data->sres[i], "\xf1\xf2\xf3\xf4",
354 #else /* CONFIG_SIM_HARDCODED */
356 wpa_printf(MSG_DEBUG, "EAP-SIM: No GSM authentication algorithm "
360 #endif /* CONFIG_SIM_HARDCODED */
364 static int eap_sim_supported_ver(int version)
366 return version == EAP_SIM_VERSION;
370 #define CLEAR_PSEUDONYM 0x01
371 #define CLEAR_REAUTH_ID 0x02
372 #define CLEAR_EAP_ID 0x04
374 static void eap_sim_clear_identities(struct eap_sm *sm,
375 struct eap_sim_data *data, int id)
377 if ((id & CLEAR_PSEUDONYM) && data->pseudonym) {
378 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old pseudonym");
379 os_free(data->pseudonym);
380 data->pseudonym = NULL;
381 data->pseudonym_len = 0;
382 if (data->use_pseudonym)
383 eap_set_anon_id(sm, NULL, 0);
385 if ((id & CLEAR_REAUTH_ID) && data->reauth_id) {
386 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old reauth_id");
387 os_free(data->reauth_id);
388 data->reauth_id = NULL;
389 data->reauth_id_len = 0;
391 if ((id & CLEAR_EAP_ID) && data->last_eap_identity) {
392 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old eap_id");
393 os_free(data->last_eap_identity);
394 data->last_eap_identity = NULL;
395 data->last_eap_identity_len = 0;
400 static int eap_sim_learn_ids(struct eap_sm *sm, struct eap_sim_data *data,
401 struct eap_sim_attrs *attr)
403 if (attr->next_pseudonym) {
404 const u8 *identity = NULL;
405 size_t identity_len = 0;
406 const u8 *realm = NULL;
407 size_t realm_len = 0;
409 wpa_hexdump_ascii(MSG_DEBUG,
410 "EAP-SIM: (encr) AT_NEXT_PSEUDONYM",
411 attr->next_pseudonym,
412 attr->next_pseudonym_len);
413 os_free(data->pseudonym);
414 /* Look for the realm of the permanent identity */
415 identity = eap_get_config_identity(sm, &identity_len);
417 for (realm = identity, realm_len = identity_len;
418 realm_len > 0; realm_len--, realm++) {
423 data->pseudonym = os_malloc(attr->next_pseudonym_len +
425 if (data->pseudonym == NULL) {
426 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
428 data->pseudonym_len = 0;
431 os_memcpy(data->pseudonym, attr->next_pseudonym,
432 attr->next_pseudonym_len);
434 os_memcpy(data->pseudonym + attr->next_pseudonym_len,
437 data->pseudonym_len = attr->next_pseudonym_len + realm_len;
438 if (data->use_pseudonym)
439 eap_set_anon_id(sm, data->pseudonym,
440 data->pseudonym_len);
443 if (attr->next_reauth_id) {
444 os_free(data->reauth_id);
445 data->reauth_id = os_memdup(attr->next_reauth_id,
446 attr->next_reauth_id_len);
447 if (data->reauth_id == NULL) {
448 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
450 data->reauth_id_len = 0;
453 data->reauth_id_len = attr->next_reauth_id_len;
454 wpa_hexdump_ascii(MSG_DEBUG,
455 "EAP-SIM: (encr) AT_NEXT_REAUTH_ID",
457 data->reauth_id_len);
464 static struct wpabuf * eap_sim_client_error(struct eap_sim_data *data, u8 id,
467 struct eap_sim_msg *msg;
469 eap_sim_state(data, FAILURE);
470 data->num_id_req = 0;
471 data->num_notification = 0;
473 wpa_printf(MSG_DEBUG, "EAP-SIM: Send Client-Error (error code %d)",
475 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
476 EAP_SIM_SUBTYPE_CLIENT_ERROR);
477 eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
478 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
482 static struct wpabuf * eap_sim_response_start(struct eap_sm *sm,
483 struct eap_sim_data *data, u8 id,
484 enum eap_sim_id_req id_req)
486 const u8 *identity = NULL;
487 size_t identity_len = 0;
488 struct eap_sim_msg *msg;
491 if (id_req == ANY_ID && data->reauth_id) {
492 identity = data->reauth_id;
493 identity_len = data->reauth_id_len;
495 } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
497 !eap_sim_anonymous_username(data->pseudonym,
498 data->pseudonym_len)) {
499 identity = data->pseudonym;
500 identity_len = data->pseudonym_len;
501 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID);
502 } else if (id_req != NO_ID_REQ) {
503 identity = eap_get_config_identity(sm, &identity_len);
505 int ids = CLEAR_PSEUDONYM | CLEAR_REAUTH_ID;
507 if (data->pseudonym &&
508 eap_sim_anonymous_username(data->pseudonym,
509 data->pseudonym_len))
510 ids &= ~CLEAR_PSEUDONYM;
511 eap_sim_clear_identities(sm, data, ids);
514 if (id_req != NO_ID_REQ)
515 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
517 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Start (id=%d)", id);
518 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
519 EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START);
521 wpa_hexdump(MSG_DEBUG, " AT_NONCE_MT",
522 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
523 eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_MT, 0,
524 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
525 wpa_printf(MSG_DEBUG, " AT_SELECTED_VERSION %d",
526 data->selected_version);
527 eap_sim_msg_add(msg, EAP_SIM_AT_SELECTED_VERSION,
528 data->selected_version, NULL, 0);
532 wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY",
533 identity, identity_len);
534 eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
535 identity, identity_len);
538 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
542 static struct wpabuf * eap_sim_response_challenge(struct eap_sim_data *data,
545 struct eap_sim_msg *msg;
547 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Challenge (id=%d)", id);
548 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
549 EAP_SIM_SUBTYPE_CHALLENGE);
550 if (data->use_result_ind) {
551 wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
552 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
554 wpa_printf(MSG_DEBUG, " AT_MAC");
555 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
556 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut,
558 data->num_chal * EAP_SIM_SRES_LEN);
562 static struct wpabuf * eap_sim_response_reauth(struct eap_sim_data *data,
563 u8 id, int counter_too_small,
566 struct eap_sim_msg *msg;
567 unsigned int counter;
569 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)",
571 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
572 EAP_SIM_SUBTYPE_REAUTHENTICATION);
573 wpa_printf(MSG_DEBUG, " AT_IV");
574 wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
575 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
577 if (counter_too_small) {
578 wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL");
579 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
580 counter = data->counter_too_small;
582 counter = data->counter;
584 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter);
585 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
587 if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
588 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
590 eap_sim_msg_free(msg);
593 if (data->use_result_ind) {
594 wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
595 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
597 wpa_printf(MSG_DEBUG, " AT_MAC");
598 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
599 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, nonce_s,
600 EAP_SIM_NONCE_S_LEN);
604 static struct wpabuf * eap_sim_response_notification(struct eap_sim_data *data,
605 u8 id, u16 notification)
607 struct eap_sim_msg *msg;
608 u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
610 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Notification (id=%d)", id);
611 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
612 EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION);
613 if (k_aut && data->reauth) {
614 wpa_printf(MSG_DEBUG, " AT_IV");
615 wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
616 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
617 EAP_SIM_AT_ENCR_DATA);
618 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter);
619 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
621 if (eap_sim_msg_add_encr_end(msg, data->k_encr,
622 EAP_SIM_AT_PADDING)) {
623 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
625 eap_sim_msg_free(msg);
630 wpa_printf(MSG_DEBUG, " AT_MAC");
631 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
633 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, k_aut, (u8 *) "", 0);
637 static struct wpabuf * eap_sim_process_start(struct eap_sm *sm,
638 struct eap_sim_data *data, u8 id,
639 struct eap_sim_attrs *attr)
641 int selected_version = -1, id_error;
645 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Start");
646 if (attr->version_list == NULL) {
647 wpa_printf(MSG_INFO, "EAP-SIM: No AT_VERSION_LIST in "
649 return eap_sim_client_error(data, id,
650 EAP_SIM_UNSUPPORTED_VERSION);
653 os_free(data->ver_list);
654 data->ver_list = os_memdup(attr->version_list, attr->version_list_len);
655 if (data->ver_list == NULL) {
656 wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to allocate "
657 "memory for version list");
658 return eap_sim_client_error(data, id,
659 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
661 data->ver_list_len = attr->version_list_len;
662 pos = data->ver_list;
663 for (i = 0; i < data->ver_list_len / 2; i++) {
664 int ver = pos[0] * 256 + pos[1];
666 if (eap_sim_supported_ver(ver)) {
667 selected_version = ver;
671 if (selected_version < 0) {
672 wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported "
674 return eap_sim_client_error(data, id,
675 EAP_SIM_UNSUPPORTED_VERSION);
677 wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d",
679 data->selected_version = selected_version;
682 switch (attr->id_req) {
686 if (data->num_id_req > 0)
691 if (data->num_id_req > 1)
696 if (data->num_id_req > 2)
702 wpa_printf(MSG_INFO, "EAP-SIM: Too many ID requests "
703 "used within one authentication");
704 return eap_sim_client_error(data, id,
705 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
708 return eap_sim_response_start(sm, data, id, attr->id_req);
712 static struct wpabuf * eap_sim_process_challenge(struct eap_sm *sm,
713 struct eap_sim_data *data,
715 const struct wpabuf *reqData,
716 struct eap_sim_attrs *attr)
720 struct eap_sim_attrs eattr;
723 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge");
725 if (!attr->mac || !attr->rand) {
726 wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
727 "did not include%s%s",
728 !attr->mac ? " AT_MAC" : "",
729 !attr->rand ? " AT_RAND" : "");
730 return eap_sim_client_error(data, id,
731 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
734 wpa_printf(MSG_DEBUG, "EAP-SIM: %lu challenges",
735 (unsigned long) attr->num_chal);
736 if (attr->num_chal < data->min_num_chal) {
737 wpa_printf(MSG_INFO, "EAP-SIM: Insufficient number of "
738 "challenges (%lu)", (unsigned long) attr->num_chal);
739 return eap_sim_client_error(data, id,
740 EAP_SIM_INSUFFICIENT_NUM_OF_CHAL);
742 if (attr->num_chal > 3) {
743 wpa_printf(MSG_INFO, "EAP-SIM: Too many challenges "
744 "(%lu)", (unsigned long) attr->num_chal);
745 return eap_sim_client_error(data, id,
746 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
749 /* Verify that RANDs are different */
750 if (os_memcmp(attr->rand, attr->rand + GSM_RAND_LEN,
751 GSM_RAND_LEN) == 0 ||
752 (attr->num_chal > 2 &&
753 (os_memcmp(attr->rand, attr->rand + 2 * GSM_RAND_LEN,
754 GSM_RAND_LEN) == 0 ||
755 os_memcmp(attr->rand + GSM_RAND_LEN,
756 attr->rand + 2 * GSM_RAND_LEN,
757 GSM_RAND_LEN) == 0))) {
758 wpa_printf(MSG_INFO, "EAP-SIM: Same RAND used multiple times");
759 return eap_sim_client_error(data, id,
760 EAP_SIM_RAND_NOT_FRESH);
763 os_memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN);
764 data->num_chal = attr->num_chal;
766 res = eap_sim_gsm_auth(sm, data);
768 wpa_printf(MSG_DEBUG, "EAP-SIM: Wait for external SIM processing");
772 wpa_printf(MSG_WARNING, "EAP-SIM: GSM authentication failed");
773 return eap_sim_client_error(data, id,
774 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
776 if (data->last_eap_identity) {
777 identity = data->last_eap_identity;
778 identity_len = data->last_eap_identity_len;
779 } else if (data->pseudonym &&
780 !eap_sim_anonymous_username(data->pseudonym,
781 data->pseudonym_len)) {
782 identity = data->pseudonym;
783 identity_len = data->pseudonym_len;
785 struct eap_peer_config *config;
787 config = eap_get_config(sm);
788 if (config && config->imsi_identity) {
789 identity = config->imsi_identity;
790 identity_len = config->imsi_identity_len;
792 identity = eap_get_config_identity(sm, &identity_len);
795 wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Selected identity for MK "
796 "derivation", identity, identity_len);
797 eap_sim_derive_mk(identity, identity_len, data->nonce_mt,
798 data->selected_version, data->ver_list,
799 data->ver_list_len, data->num_chal,
800 (const u8 *) data->kc, data->mk);
801 eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
803 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, data->nonce_mt,
804 EAP_SIM_NONCE_MT_LEN)) {
805 wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
806 "used invalid AT_MAC");
809 "TEST: Ignore AT_MAC mismatch for fuzz testing");
810 #else /* TEST_FUZZ */
811 return eap_sim_client_error(data, id,
812 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
813 #endif /* TEST_FUZZ */
816 /* Old reauthentication identity must not be used anymore. In
817 * other words, if no new reauth identity is received, full
818 * authentication will be used on next reauthentication (using
819 * pseudonym identity or permanent identity). */
820 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
822 if (attr->encr_data) {
824 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
825 attr->encr_data_len, attr->iv,
827 if (decrypted == NULL) {
828 return eap_sim_client_error(
829 data, id, EAP_SIM_UNABLE_TO_PROCESS_PACKET);
831 eap_sim_learn_ids(sm, data, &eattr);
835 if (data->result_ind && attr->result_ind)
836 data->use_result_ind = 1;
838 if (data->state != FAILURE) {
839 eap_sim_state(data, data->use_result_ind ?
840 RESULT_SUCCESS : SUCCESS);
843 data->num_id_req = 0;
844 data->num_notification = 0;
845 /* RFC 4186 specifies that counter is initialized to one after
846 * fullauth, but initializing it to zero makes it easier to implement
847 * reauth verification. */
849 return eap_sim_response_challenge(data, id);
853 static int eap_sim_process_notification_reauth(struct eap_sim_data *data,
854 struct eap_sim_attrs *attr)
856 struct eap_sim_attrs eattr;
859 if (attr->encr_data == NULL || attr->iv == NULL) {
860 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message after "
861 "reauth did not include encrypted data");
865 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
866 attr->encr_data_len, attr->iv, &eattr,
868 if (decrypted == NULL) {
869 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
870 "data from notification message");
874 if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
875 wpa_printf(MSG_WARNING, "EAP-SIM: Counter in notification "
876 "message does not match with counter in reauth "
887 static int eap_sim_process_notification_auth(struct eap_sim_data *data,
888 const struct wpabuf *reqData,
889 struct eap_sim_attrs *attr)
891 if (attr->mac == NULL) {
892 wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth "
893 "Notification message");
897 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
899 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message "
900 "used invalid AT_MAC");
905 eap_sim_process_notification_reauth(data, attr)) {
906 wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification "
907 "message after reauth");
915 static struct wpabuf * eap_sim_process_notification(
916 struct eap_sm *sm, struct eap_sim_data *data, u8 id,
917 const struct wpabuf *reqData, struct eap_sim_attrs *attr)
919 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Notification");
920 if (data->num_notification > 0) {
921 wpa_printf(MSG_INFO, "EAP-SIM: too many notification "
922 "rounds (only one allowed)");
923 return eap_sim_client_error(data, id,
924 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
926 data->num_notification++;
927 if (attr->notification == -1) {
928 wpa_printf(MSG_INFO, "EAP-SIM: no AT_NOTIFICATION in "
929 "Notification message");
930 return eap_sim_client_error(data, id,
931 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
934 if ((attr->notification & 0x4000) == 0 &&
935 eap_sim_process_notification_auth(data, reqData, attr)) {
936 return eap_sim_client_error(data, id,
937 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
940 eap_sim_report_notification(sm->msg_ctx, attr->notification, 0);
941 if (attr->notification >= 0 && attr->notification < 32768) {
942 data->error_code = attr->notification;
943 eap_sim_state(data, FAILURE);
944 } else if (attr->notification == EAP_SIM_SUCCESS &&
945 data->state == RESULT_SUCCESS)
946 eap_sim_state(data, SUCCESS);
947 return eap_sim_response_notification(data, id, attr->notification);
951 static struct wpabuf * eap_sim_process_reauthentication(
952 struct eap_sm *sm, struct eap_sim_data *data, u8 id,
953 const struct wpabuf *reqData, struct eap_sim_attrs *attr)
955 struct eap_sim_attrs eattr;
958 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication");
960 if (data->reauth_id == NULL) {
961 wpa_printf(MSG_WARNING, "EAP-SIM: Server is trying "
962 "reauthentication, but no reauth_id available");
963 return eap_sim_client_error(data, id,
964 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
968 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
970 wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
971 "did not have valid AT_MAC");
974 "TEST: Ignore AT_MAC mismatch for fuzz testing");
975 #else /* TEST_FUZZ */
976 return eap_sim_client_error(data, id,
977 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
978 #endif /* TEST_FUZZ */
981 /* At this stage the received MAC has been verified. Use this MAC for
982 * reauth Session-Id calculation if all other checks pass.
983 * The peer does not use the local MAC but the received MAC in deriving
987 os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN);
989 os_memset(data->reauth_mac, 0x12, EAP_SIM_MAC_LEN);
990 #else /* TEST_FUZZ */
991 os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN);
992 #endif /* TEST_FUZZ */
993 wpa_hexdump(MSG_DEBUG, "EAP-SIM: Server MAC",
994 data->reauth_mac, EAP_SIM_MAC_LEN);
996 if (attr->encr_data == NULL || attr->iv == NULL) {
997 wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
998 "message did not include encrypted data");
999 return eap_sim_client_error(data, id,
1000 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1003 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
1004 attr->encr_data_len, attr->iv, &eattr,
1006 if (decrypted == NULL) {
1007 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
1008 "data from reauthentication message");
1009 return eap_sim_client_error(data, id,
1010 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1013 if (eattr.nonce_s == NULL || eattr.counter < 0) {
1014 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No%s%s in reauth packet",
1015 !eattr.nonce_s ? " AT_NONCE_S" : "",
1016 eattr.counter < 0 ? " AT_COUNTER" : "");
1018 return eap_sim_client_error(data, id,
1019 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1022 if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
1024 wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid counter "
1025 "(%d <= %d)", eattr.counter, data->counter);
1026 data->counter_too_small = eattr.counter;
1028 /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
1029 * reauth_id must not be used to start a new reauthentication.
1030 * However, since it was used in the last EAP-Response-Identity
1031 * packet, it has to saved for the following fullauth to be
1032 * used in MK derivation. */
1033 os_free(data->last_eap_identity);
1034 data->last_eap_identity = data->reauth_id;
1035 data->last_eap_identity_len = data->reauth_id_len;
1036 data->reauth_id = NULL;
1037 data->reauth_id_len = 0;
1039 res = eap_sim_response_reauth(data, id, 1, eattr.nonce_s);
1044 data->counter = eattr.counter;
1046 os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
1047 wpa_hexdump(MSG_DEBUG, "EAP-SIM: (encr) AT_NONCE_S",
1048 data->nonce_s, EAP_SIM_NONCE_S_LEN);
1050 eap_sim_derive_keys_reauth(data->counter,
1051 data->reauth_id, data->reauth_id_len,
1052 data->nonce_s, data->mk, data->msk,
1054 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1055 eap_sim_learn_ids(sm, data, &eattr);
1057 if (data->result_ind && attr->result_ind)
1058 data->use_result_ind = 1;
1060 if (data->state != FAILURE) {
1061 eap_sim_state(data, data->use_result_ind ?
1062 RESULT_SUCCESS : SUCCESS);
1065 data->num_id_req = 0;
1066 data->num_notification = 0;
1067 if (data->counter > EAP_SIM_MAX_FAST_REAUTHS) {
1068 wpa_printf(MSG_DEBUG, "EAP-SIM: Maximum number of "
1069 "fast reauths performed - force fullauth");
1070 eap_sim_clear_identities(sm, data,
1071 CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1074 return eap_sim_response_reauth(data, id, 0, data->nonce_s);
1078 static struct wpabuf * eap_sim_process(struct eap_sm *sm, void *priv,
1079 struct eap_method_ret *ret,
1080 const struct wpabuf *reqData)
1082 struct eap_sim_data *data = priv;
1083 const struct eap_hdr *req;
1087 struct eap_sim_attrs attr;
1090 wpa_hexdump_buf(MSG_DEBUG, "EAP-SIM: EAP data", reqData);
1091 if (eap_get_config_identity(sm, &len) == NULL) {
1092 wpa_printf(MSG_INFO, "EAP-SIM: Identity not configured");
1093 eap_sm_request_identity(sm);
1098 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, reqData, &len);
1099 if (pos == NULL || len < 3) {
1103 req = wpabuf_head(reqData);
1104 id = req->identifier;
1105 len = be_to_host16(req->length);
1107 ret->ignore = FALSE;
1108 ret->methodState = METHOD_MAY_CONT;
1109 ret->decision = DECISION_FAIL;
1110 ret->allowNotifications = TRUE;
1113 wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype);
1114 pos += 2; /* Reserved */
1116 if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 0,
1118 res = eap_sim_client_error(data, id,
1119 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1124 case EAP_SIM_SUBTYPE_START:
1125 res = eap_sim_process_start(sm, data, id, &attr);
1127 case EAP_SIM_SUBTYPE_CHALLENGE:
1128 res = eap_sim_process_challenge(sm, data, id, reqData, &attr);
1130 case EAP_SIM_SUBTYPE_NOTIFICATION:
1131 res = eap_sim_process_notification(sm, data, id, reqData,
1134 case EAP_SIM_SUBTYPE_REAUTHENTICATION:
1135 res = eap_sim_process_reauthentication(sm, data, id, reqData,
1138 case EAP_SIM_SUBTYPE_CLIENT_ERROR:
1139 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Client-Error");
1140 res = eap_sim_client_error(data, id,
1141 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1144 wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown subtype=%d", subtype);
1145 res = eap_sim_client_error(data, id,
1146 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1151 if (data->state == FAILURE) {
1152 ret->decision = DECISION_FAIL;
1153 ret->methodState = METHOD_DONE;
1154 } else if (data->state == SUCCESS) {
1155 ret->decision = data->use_result_ind ?
1156 DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
1157 ret->methodState = data->use_result_ind ?
1158 METHOD_DONE : METHOD_MAY_CONT;
1159 } else if (data->state == RESULT_SUCCESS)
1160 ret->methodState = METHOD_CONT;
1162 if (ret->methodState == METHOD_DONE) {
1163 ret->allowNotifications = FALSE;
1170 static Boolean eap_sim_has_reauth_data(struct eap_sm *sm, void *priv)
1172 struct eap_sim_data *data = priv;
1173 return data->pseudonym || data->reauth_id;
1177 static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv)
1179 struct eap_sim_data *data = priv;
1180 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
1181 data->use_result_ind = 0;
1182 eap_sim_clear_keys(data, 1);
1186 static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv)
1188 struct eap_sim_data *data = priv;
1189 if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
1190 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
1192 eap_sim_deinit(sm, data);
1195 data->num_id_req = 0;
1196 data->num_notification = 0;
1197 eap_sim_state(data, CONTINUE);
1202 static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv,
1205 struct eap_sim_data *data = priv;
1207 if (data->reauth_id) {
1208 *len = data->reauth_id_len;
1209 return data->reauth_id;
1212 if (data->pseudonym) {
1213 *len = data->pseudonym_len;
1214 return data->pseudonym;
1221 static Boolean eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv)
1223 struct eap_sim_data *data = priv;
1224 return data->state == SUCCESS;
1228 static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
1230 struct eap_sim_data *data = priv;
1233 if (data->state != SUCCESS)
1236 key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN);
1240 *len = EAP_SIM_KEYING_DATA_LEN;
1246 static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1248 struct eap_sim_data *data = priv;
1251 if (data->state != SUCCESS)
1255 *len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN;
1257 *len = 1 + EAP_SIM_NONCE_S_LEN + EAP_SIM_MAC_LEN;
1258 id = os_malloc(*len);
1262 id[0] = EAP_TYPE_SIM;
1263 if (!data->reauth) {
1264 os_memcpy(id + 1, data->rand, data->num_chal * GSM_RAND_LEN);
1265 os_memcpy(id + 1 + data->num_chal * GSM_RAND_LEN,
1266 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
1268 os_memcpy(id + 1, data->nonce_s, EAP_SIM_NONCE_S_LEN);
1269 os_memcpy(id + 1 + EAP_SIM_NONCE_S_LEN, data->reauth_mac,
1272 wpa_hexdump(MSG_DEBUG, "EAP-SIM: Derived Session-Id", id, *len);
1278 static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1280 struct eap_sim_data *data = priv;
1283 if (data->state != SUCCESS)
1286 key = os_memdup(data->emsk, EAP_EMSK_LEN);
1290 *len = EAP_EMSK_LEN;
1296 static int eap_sim_get_error_code(void *priv)
1298 struct eap_sim_data *data = priv;
1299 int current_data_error;
1302 return NO_EAP_METHOD_ERROR;
1304 current_data_error = data->error_code;
1306 /* Now reset for next transaction */
1307 data->error_code = NO_EAP_METHOD_ERROR;
1309 return current_data_error;
1313 int eap_peer_sim_register(void)
1315 struct eap_method *eap;
1317 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1318 EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
1322 eap->init = eap_sim_init;
1323 eap->deinit = eap_sim_deinit;
1324 eap->process = eap_sim_process;
1325 eap->isKeyAvailable = eap_sim_isKeyAvailable;
1326 eap->getKey = eap_sim_getKey;
1327 eap->getSessionId = eap_sim_get_session_id;
1328 eap->has_reauth_data = eap_sim_has_reauth_data;
1329 eap->deinit_for_reauth = eap_sim_deinit_for_reauth;
1330 eap->init_for_reauth = eap_sim_init_for_reauth;
1331 eap->get_identity = eap_sim_get_identity;
1332 eap->get_emsk = eap_sim_get_emsk;
1333 eap->get_error_code = eap_sim_get_error_code;
1335 return eap_peer_method_register(eap);