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];
36 int num_id_req, num_notification;
42 unsigned int counter, counter_too_small;
43 u8 *last_eap_identity;
44 size_t last_eap_identity_len;
46 CONTINUE, RESULT_SUCCESS, RESULT_FAILURE, SUCCESS, FAILURE
48 int result_ind, use_result_ind;
52 #ifndef CONFIG_NO_STDOUT_DEBUG
53 static const char * eap_sim_state_txt(int state)
59 return "RESULT_SUCCESS";
61 return "RESULT_FAILURE";
70 #endif /* CONFIG_NO_STDOUT_DEBUG */
73 static void eap_sim_state(struct eap_sim_data *data, int state)
75 wpa_printf(MSG_DEBUG, "EAP-SIM: %s -> %s",
76 eap_sim_state_txt(data->state),
77 eap_sim_state_txt(state));
82 static void * eap_sim_init(struct eap_sm *sm)
84 struct eap_sim_data *data;
85 struct eap_peer_config *config = eap_get_config(sm);
87 data = os_zalloc(sizeof(*data));
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 "
98 data->min_num_chal = 2;
99 if (config && config->phase1) {
100 char *pos = os_strstr(config->phase1, "sim_min_num_chal=");
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);
111 wpa_printf(MSG_DEBUG, "EAP-SIM: Set minimum number of "
113 (unsigned long) data->min_num_chal);
116 data->result_ind = os_strstr(config->phase1, "result_ind=1") !=
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;
129 eap_sim_state(data, CONTINUE);
135 static void eap_sim_deinit(struct eap_sm *sm, void *priv)
137 struct eap_sim_data *data = priv;
139 os_free(data->ver_list);
140 os_free(data->pseudonym);
141 os_free(data->reauth_id);
142 os_free(data->last_eap_identity);
148 static int eap_sim_ext_sim_req(struct eap_sm *sm, struct eap_sim_data *data)
150 char req[200], *pos, *end;
153 wpa_printf(MSG_DEBUG, "EAP-SIM: Use external SIM processing");
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],
163 eap_sm_request_sim(sm, req);
168 static int eap_sim_ext_sim_result(struct eap_sm *sm, struct eap_sim_data *data,
169 struct eap_peer_config *conf)
174 wpa_printf(MSG_DEBUG,
175 "EAP-SIM: Use result from external SIM processing");
177 resp = conf->external_sim_resp;
178 conf->external_sim_resp = NULL;
180 if (os_strncmp(resp, "GSM-AUTH:", 9) != 0) {
181 wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized external SIM processing response");
187 for (i = 0; i < data->num_chal; i++) {
188 wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
189 data->rand[i], GSM_RAND_LEN);
191 if (hexstr2bin(pos, data->kc[i], EAP_SIM_KC_LEN) < 0)
193 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
194 data->kc[i], EAP_SIM_KC_LEN);
195 pos += EAP_SIM_KC_LEN * 2;
200 if (hexstr2bin(pos, data->sres[i], EAP_SIM_SRES_LEN) < 0)
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) {
216 wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
222 static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data)
224 struct eap_peer_config *conf;
226 wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm");
228 conf = eap_get_config(sm);
232 if (sm->external_sim) {
233 if (conf->external_sim_resp)
234 return eap_sim_ext_sim_result(sm, data, conf);
236 return eap_sim_ext_sim_req(sm, data);
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");
254 #ifdef CONFIG_SIM_SIMULATOR
255 if (conf->password) {
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 "
266 pos = (const char *) conf->password;
267 if (hexstr2bin(pos, k, 16))
274 if (hexstr2bin(pos, opc, 16))
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");
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);
294 #endif /* CONFIG_SIM_SIMULATOR */
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. */
304 wpa_printf(MSG_DEBUG, "EAP-SIM: Use hardcoded Kc and SRES "
305 "values for testing");
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",
312 os_memcpy(data->sres[i], "\xd1\xd2\xd3\xd4",
314 } else if (data->rand[i][0] == 0xbb) {
315 os_memcpy(data->kc[i],
316 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
318 os_memcpy(data->sres[i], "\xe1\xe2\xe3\xe4",
321 os_memcpy(data->kc[i],
322 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
324 os_memcpy(data->sres[i], "\xf1\xf2\xf3\xf4",
332 #else /* CONFIG_SIM_HARDCODED */
334 wpa_printf(MSG_DEBUG, "EAP-SIM: No GSM authentication algorithm "
338 #endif /* CONFIG_SIM_HARDCODED */
342 static int eap_sim_supported_ver(int version)
344 return version == EAP_SIM_VERSION;
348 #define CLEAR_PSEUDONYM 0x01
349 #define CLEAR_REAUTH_ID 0x02
350 #define CLEAR_EAP_ID 0x04
352 static void eap_sim_clear_identities(struct eap_sm *sm,
353 struct eap_sim_data *data, int id)
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);
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;
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;
377 static int eap_sim_learn_ids(struct eap_sm *sm, struct eap_sim_data *data,
378 struct eap_sim_attrs *attr)
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;
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);
394 for (realm = identity, realm_len = identity_len;
395 realm_len > 0; realm_len--, realm++) {
400 data->pseudonym = os_malloc(attr->next_pseudonym_len +
402 if (data->pseudonym == NULL) {
403 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
405 data->pseudonym_len = 0;
408 os_memcpy(data->pseudonym, attr->next_pseudonym,
409 attr->next_pseudonym_len);
411 os_memcpy(data->pseudonym + attr->next_pseudonym_len,
414 data->pseudonym_len = attr->next_pseudonym_len + realm_len;
415 eap_set_anon_id(sm, data->pseudonym, data->pseudonym_len);
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 "
424 data->reauth_id_len = 0;
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",
433 data->reauth_id_len);
440 static struct wpabuf * eap_sim_client_error(struct eap_sim_data *data, u8 id,
443 struct eap_sim_msg *msg;
445 eap_sim_state(data, FAILURE);
446 data->num_id_req = 0;
447 data->num_notification = 0;
449 wpa_printf(MSG_DEBUG, "EAP-SIM: Send Client-Error (error code %d)",
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);
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)
462 const u8 *identity = NULL;
463 size_t identity_len = 0;
464 struct eap_sim_msg *msg;
467 if (id_req == ANY_ID && data->reauth_id) {
468 identity = data->reauth_id;
469 identity_len = data->reauth_id_len;
471 } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
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);
479 eap_sim_clear_identities(sm, data, CLEAR_PSEUDONYM |
483 if (id_req != NO_ID_REQ)
484 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
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);
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);
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);
507 return eap_sim_msg_finish(msg, NULL, NULL, 0);
511 static struct wpabuf * eap_sim_response_challenge(struct eap_sim_data *data,
514 struct eap_sim_msg *msg;
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);
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);
530 static struct wpabuf * eap_sim_response_reauth(struct eap_sim_data *data,
531 u8 id, int counter_too_small,
534 struct eap_sim_msg *msg;
535 unsigned int counter;
537 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)",
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);
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;
550 counter = data->counter;
552 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter);
553 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
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 "
558 eap_sim_msg_free(msg);
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);
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);
572 static struct wpabuf * eap_sim_response_notification(struct eap_sim_data *data,
573 u8 id, u16 notification)
575 struct eap_sim_msg *msg;
576 u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
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,
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 "
593 eap_sim_msg_free(msg);
598 wpa_printf(MSG_DEBUG, " AT_MAC");
599 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
601 return eap_sim_msg_finish(msg, k_aut, (u8 *) "", 0);
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)
609 int selected_version = -1, id_error;
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 "
617 return eap_sim_client_error(data, id,
618 EAP_SIM_UNSUPPORTED_VERSION);
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);
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];
635 if (eap_sim_supported_ver(ver)) {
636 selected_version = ver;
640 if (selected_version < 0) {
641 wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported "
643 return eap_sim_client_error(data, id,
644 EAP_SIM_UNSUPPORTED_VERSION);
646 wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d",
648 data->selected_version = selected_version;
651 switch (attr->id_req) {
655 if (data->num_id_req > 0)
660 if (data->num_id_req > 1)
665 if (data->num_id_req > 2)
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);
677 return eap_sim_response_start(sm, data, id, attr->id_req);
681 static struct wpabuf * eap_sim_process_challenge(struct eap_sm *sm,
682 struct eap_sim_data *data,
684 const struct wpabuf *reqData,
685 struct eap_sim_attrs *attr)
689 struct eap_sim_attrs eattr;
692 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge");
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);
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);
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);
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);
732 os_memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN);
733 data->num_chal = attr->num_chal;
735 res = eap_sim_gsm_auth(sm, data);
737 wpa_printf(MSG_DEBUG, "EAP-SIM: Wait for external SIM processing");
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);
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;
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,
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);
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);
775 if (attr->encr_data) {
777 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
778 attr->encr_data_len, attr->iv,
780 if (decrypted == NULL) {
781 return eap_sim_client_error(
782 data, id, EAP_SIM_UNABLE_TO_PROCESS_PACKET);
784 eap_sim_learn_ids(sm, data, &eattr);
788 if (data->result_ind && attr->result_ind)
789 data->use_result_ind = 1;
791 if (data->state != FAILURE && data->state != RESULT_FAILURE) {
792 eap_sim_state(data, data->use_result_ind ?
793 RESULT_SUCCESS : SUCCESS);
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. */
802 return eap_sim_response_challenge(data, id);
806 static int eap_sim_process_notification_reauth(struct eap_sim_data *data,
807 struct eap_sim_attrs *attr)
809 struct eap_sim_attrs eattr;
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");
818 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
819 attr->encr_data_len, attr->iv, &eattr,
821 if (decrypted == NULL) {
822 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
823 "data from notification message");
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 "
840 static int eap_sim_process_notification_auth(struct eap_sim_data *data,
841 const struct wpabuf *reqData,
842 struct eap_sim_attrs *attr)
844 if (attr->mac == NULL) {
845 wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth "
846 "Notification message");
850 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
852 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message "
853 "used invalid AT_MAC");
858 eap_sim_process_notification_reauth(data, attr)) {
859 wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification "
860 "message after reauth");
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)
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);
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);
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);
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);
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)
907 struct eap_sim_attrs eattr;
910 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication");
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);
920 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
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);
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);
935 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
936 attr->encr_data_len, attr->iv, &eattr,
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);
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" : "");
950 return eap_sim_client_error(data, id,
951 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
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;
969 return eap_sim_response_reauth(data, id, 1, eattr.nonce_s);
971 data->counter = eattr.counter;
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);
977 eap_sim_derive_keys_reauth(data->counter,
978 data->reauth_id, data->reauth_id_len,
979 data->nonce_s, data->mk, data->msk,
981 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
982 eap_sim_learn_ids(sm, data, &eattr);
984 if (data->result_ind && attr->result_ind)
985 data->use_result_ind = 1;
987 if (data->state != FAILURE && data->state != RESULT_FAILURE) {
988 eap_sim_state(data, data->use_result_ind ?
989 RESULT_SUCCESS : SUCCESS);
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);
1001 return eap_sim_response_reauth(data, id, 0, data->nonce_s);
1005 static struct wpabuf * eap_sim_process(struct eap_sm *sm, void *priv,
1006 struct eap_method_ret *ret,
1007 const struct wpabuf *reqData)
1009 struct eap_sim_data *data = priv;
1010 const struct eap_hdr *req;
1014 struct eap_sim_attrs attr;
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);
1025 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, reqData, &len);
1026 if (pos == NULL || len < 1) {
1030 req = wpabuf_head(reqData);
1031 id = req->identifier;
1032 len = be_to_host16(req->length);
1034 ret->ignore = FALSE;
1035 ret->methodState = METHOD_MAY_CONT;
1036 ret->decision = DECISION_FAIL;
1037 ret->allowNotifications = TRUE;
1040 wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype);
1041 pos += 2; /* Reserved */
1043 if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 0,
1045 res = eap_sim_client_error(data, id,
1046 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1051 case EAP_SIM_SUBTYPE_START:
1052 res = eap_sim_process_start(sm, data, id, &attr);
1054 case EAP_SIM_SUBTYPE_CHALLENGE:
1055 res = eap_sim_process_challenge(sm, data, id, reqData, &attr);
1057 case EAP_SIM_SUBTYPE_NOTIFICATION:
1058 res = eap_sim_process_notification(sm, data, id, reqData,
1061 case EAP_SIM_SUBTYPE_REAUTHENTICATION:
1062 res = eap_sim_process_reauthentication(sm, data, id, reqData,
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);
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);
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;
1091 if (ret->methodState == METHOD_DONE) {
1092 ret->allowNotifications = FALSE;
1099 static Boolean eap_sim_has_reauth_data(struct eap_sm *sm, void *priv)
1101 struct eap_sim_data *data = priv;
1102 return data->pseudonym || data->reauth_id;
1106 static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv)
1108 struct eap_sim_data *data = priv;
1109 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
1110 data->use_result_ind = 0;
1114 static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv)
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 "
1123 data->num_id_req = 0;
1124 data->num_notification = 0;
1125 eap_sim_state(data, CONTINUE);
1130 static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv,
1133 struct eap_sim_data *data = priv;
1135 if (data->reauth_id) {
1136 *len = data->reauth_id_len;
1137 return data->reauth_id;
1140 if (data->pseudonym) {
1141 *len = data->pseudonym_len;
1142 return data->pseudonym;
1149 static Boolean eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv)
1151 struct eap_sim_data *data = priv;
1152 return data->state == SUCCESS;
1156 static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
1158 struct eap_sim_data *data = priv;
1161 if (data->state != SUCCESS)
1164 key = os_malloc(EAP_SIM_KEYING_DATA_LEN);
1168 *len = EAP_SIM_KEYING_DATA_LEN;
1169 os_memcpy(key, data->msk, EAP_SIM_KEYING_DATA_LEN);
1175 static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1177 struct eap_sim_data *data = priv;
1180 if (data->state != SUCCESS)
1183 *len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN;
1184 id = os_malloc(*len);
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);
1198 static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1200 struct eap_sim_data *data = priv;
1203 if (data->state != SUCCESS)
1206 key = os_malloc(EAP_EMSK_LEN);
1210 *len = EAP_EMSK_LEN;
1211 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1217 int eap_peer_sim_register(void)
1219 struct eap_method *eap;
1222 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1223 EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
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;
1239 ret = eap_peer_method_register(eap);
1241 eap_peer_method_free(eap);