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, 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";
68 #endif /* CONFIG_NO_STDOUT_DEBUG */
71 static void eap_sim_state(struct eap_sim_data *data, int state)
73 wpa_printf(MSG_DEBUG, "EAP-SIM: %s -> %s",
74 eap_sim_state_txt(data->state),
75 eap_sim_state_txt(state));
80 static void * eap_sim_init(struct eap_sm *sm)
82 struct eap_sim_data *data;
83 struct eap_peer_config *config = eap_get_config(sm);
85 data = os_zalloc(sizeof(*data));
89 if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
90 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
96 data->min_num_chal = 2;
97 if (config && config->phase1) {
98 char *pos = os_strstr(config->phase1, "sim_min_num_chal=");
100 data->min_num_chal = atoi(pos + 17);
101 if (data->min_num_chal < 2 || data->min_num_chal > 3) {
102 wpa_printf(MSG_WARNING, "EAP-SIM: Invalid "
103 "sim_min_num_chal configuration "
104 "(%lu, expected 2 or 3)",
105 (unsigned long) data->min_num_chal);
109 wpa_printf(MSG_DEBUG, "EAP-SIM: Set minimum number of "
111 (unsigned long) data->min_num_chal);
114 data->result_ind = os_strstr(config->phase1, "result_ind=1") !=
118 if (config && config->anonymous_identity) {
119 data->pseudonym = os_malloc(config->anonymous_identity_len);
120 if (data->pseudonym) {
121 os_memcpy(data->pseudonym, config->anonymous_identity,
122 config->anonymous_identity_len);
123 data->pseudonym_len = config->anonymous_identity_len;
127 eap_sim_state(data, CONTINUE);
133 static void eap_sim_clear_keys(struct eap_sim_data *data, int reauth)
136 os_memset(data->mk, 0, EAP_SIM_MK_LEN);
137 os_memset(data->k_aut, 0, EAP_SIM_K_AUT_LEN);
138 os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN);
140 os_memset(data->kc, 0, 3 * EAP_SIM_KC_LEN);
141 os_memset(data->sres, 0, 3 * EAP_SIM_SRES_LEN);
142 os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN);
143 os_memset(data->emsk, 0, EAP_EMSK_LEN);
147 static void eap_sim_deinit(struct eap_sm *sm, void *priv)
149 struct eap_sim_data *data = priv;
151 os_free(data->ver_list);
152 os_free(data->pseudonym);
153 os_free(data->reauth_id);
154 os_free(data->last_eap_identity);
155 eap_sim_clear_keys(data, 0);
161 static int eap_sim_ext_sim_req(struct eap_sm *sm, struct eap_sim_data *data)
163 char req[200], *pos, *end;
166 wpa_printf(MSG_DEBUG, "EAP-SIM: Use external SIM processing");
168 end = pos + sizeof(req);
169 pos += os_snprintf(pos, end - pos, "GSM-AUTH");
170 for (i = 0; i < data->num_chal; i++) {
171 pos += os_snprintf(pos, end - pos, ":");
172 pos += wpa_snprintf_hex(pos, end - pos, data->rand[i],
176 eap_sm_request_sim(sm, req);
181 static int eap_sim_ext_sim_result(struct eap_sm *sm, struct eap_sim_data *data,
182 struct eap_peer_config *conf)
187 wpa_printf(MSG_DEBUG,
188 "EAP-SIM: Use result from external SIM processing");
190 resp = conf->external_sim_resp;
191 conf->external_sim_resp = NULL;
193 if (os_strncmp(resp, "GSM-AUTH:", 9) != 0) {
194 wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized external SIM processing response");
200 for (i = 0; i < data->num_chal; i++) {
201 wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
202 data->rand[i], GSM_RAND_LEN);
204 if (hexstr2bin(pos, data->kc[i], EAP_SIM_KC_LEN) < 0)
206 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
207 data->kc[i], EAP_SIM_KC_LEN);
208 pos += EAP_SIM_KC_LEN * 2;
213 if (hexstr2bin(pos, data->sres[i], EAP_SIM_SRES_LEN) < 0)
215 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
216 data->sres[i], EAP_SIM_SRES_LEN);
217 pos += EAP_SIM_SRES_LEN * 2;
218 if (i + 1 < data->num_chal) {
229 wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response");
235 static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data)
237 struct eap_peer_config *conf;
239 wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm");
241 conf = eap_get_config(sm);
245 if (sm->external_sim) {
246 if (conf->external_sim_resp)
247 return eap_sim_ext_sim_result(sm, data, conf);
249 return eap_sim_ext_sim_req(sm, data);
253 if (scard_gsm_auth(sm->scard_ctx, data->rand[0],
254 data->sres[0], data->kc[0]) ||
255 scard_gsm_auth(sm->scard_ctx, data->rand[1],
256 data->sres[1], data->kc[1]) ||
257 (data->num_chal > 2 &&
258 scard_gsm_auth(sm->scard_ctx, data->rand[2],
259 data->sres[2], data->kc[2]))) {
260 wpa_printf(MSG_DEBUG, "EAP-SIM: GSM SIM "
261 "authentication could not be completed");
267 #ifdef CONFIG_SIM_SIMULATOR
268 if (conf->password) {
272 wpa_printf(MSG_DEBUG, "EAP-SIM: Use internal GSM-Milenage "
273 "implementation for authentication");
274 if (conf->password_len < 65) {
275 wpa_printf(MSG_DEBUG, "EAP-SIM: invalid GSM-Milenage "
279 pos = (const char *) conf->password;
280 if (hexstr2bin(pos, k, 16))
287 if (hexstr2bin(pos, opc, 16))
290 for (i = 0; i < data->num_chal; i++) {
291 if (gsm_milenage(opc, k, data->rand[i],
292 data->sres[i], data->kc[i])) {
293 wpa_printf(MSG_DEBUG, "EAP-SIM: "
294 "GSM-Milenage authentication "
295 "could not be completed");
298 wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND",
299 data->rand[i], GSM_RAND_LEN);
300 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES",
301 data->sres[i], EAP_SIM_SRES_LEN);
302 wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc",
303 data->kc[i], EAP_SIM_KC_LEN);
307 #endif /* CONFIG_SIM_SIMULATOR */
309 #ifdef CONFIG_SIM_HARDCODED
310 /* These hardcoded Kc and SRES values are used for testing. RAND to
311 * KC/SREC mapping is very bogus as far as real authentication is
312 * concerned, but it is quite useful for cases where the AS is rotating
313 * the order of pre-configured values. */
317 wpa_printf(MSG_DEBUG, "EAP-SIM: Use hardcoded Kc and SRES "
318 "values for testing");
320 for (i = 0; i < data->num_chal; i++) {
321 if (data->rand[i][0] == 0xaa) {
322 os_memcpy(data->kc[i],
323 "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7",
325 os_memcpy(data->sres[i], "\xd1\xd2\xd3\xd4",
327 } else if (data->rand[i][0] == 0xbb) {
328 os_memcpy(data->kc[i],
329 "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7",
331 os_memcpy(data->sres[i], "\xe1\xe2\xe3\xe4",
334 os_memcpy(data->kc[i],
335 "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7",
337 os_memcpy(data->sres[i], "\xf1\xf2\xf3\xf4",
345 #else /* CONFIG_SIM_HARDCODED */
347 wpa_printf(MSG_DEBUG, "EAP-SIM: No GSM authentication algorithm "
351 #endif /* CONFIG_SIM_HARDCODED */
355 static int eap_sim_supported_ver(int version)
357 return version == EAP_SIM_VERSION;
361 #define CLEAR_PSEUDONYM 0x01
362 #define CLEAR_REAUTH_ID 0x02
363 #define CLEAR_EAP_ID 0x04
365 static void eap_sim_clear_identities(struct eap_sm *sm,
366 struct eap_sim_data *data, int id)
368 if ((id & CLEAR_PSEUDONYM) && data->pseudonym) {
369 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old pseudonym");
370 os_free(data->pseudonym);
371 data->pseudonym = NULL;
372 data->pseudonym_len = 0;
373 eap_set_anon_id(sm, NULL, 0);
375 if ((id & CLEAR_REAUTH_ID) && data->reauth_id) {
376 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old reauth_id");
377 os_free(data->reauth_id);
378 data->reauth_id = NULL;
379 data->reauth_id_len = 0;
381 if ((id & CLEAR_EAP_ID) && data->last_eap_identity) {
382 wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old eap_id");
383 os_free(data->last_eap_identity);
384 data->last_eap_identity = NULL;
385 data->last_eap_identity_len = 0;
390 static int eap_sim_learn_ids(struct eap_sm *sm, struct eap_sim_data *data,
391 struct eap_sim_attrs *attr)
393 if (attr->next_pseudonym) {
394 const u8 *identity = NULL;
395 size_t identity_len = 0;
396 const u8 *realm = NULL;
397 size_t realm_len = 0;
399 wpa_hexdump_ascii(MSG_DEBUG,
400 "EAP-SIM: (encr) AT_NEXT_PSEUDONYM",
401 attr->next_pseudonym,
402 attr->next_pseudonym_len);
403 os_free(data->pseudonym);
404 /* Look for the realm of the permanent identity */
405 identity = eap_get_config_identity(sm, &identity_len);
407 for (realm = identity, realm_len = identity_len;
408 realm_len > 0; realm_len--, realm++) {
413 data->pseudonym = os_malloc(attr->next_pseudonym_len +
415 if (data->pseudonym == NULL) {
416 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
418 data->pseudonym_len = 0;
421 os_memcpy(data->pseudonym, attr->next_pseudonym,
422 attr->next_pseudonym_len);
424 os_memcpy(data->pseudonym + attr->next_pseudonym_len,
427 data->pseudonym_len = attr->next_pseudonym_len + realm_len;
428 eap_set_anon_id(sm, data->pseudonym, data->pseudonym_len);
431 if (attr->next_reauth_id) {
432 os_free(data->reauth_id);
433 data->reauth_id = os_malloc(attr->next_reauth_id_len);
434 if (data->reauth_id == NULL) {
435 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for "
437 data->reauth_id_len = 0;
440 os_memcpy(data->reauth_id, attr->next_reauth_id,
441 attr->next_reauth_id_len);
442 data->reauth_id_len = attr->next_reauth_id_len;
443 wpa_hexdump_ascii(MSG_DEBUG,
444 "EAP-SIM: (encr) AT_NEXT_REAUTH_ID",
446 data->reauth_id_len);
453 static struct wpabuf * eap_sim_client_error(struct eap_sim_data *data, u8 id,
456 struct eap_sim_msg *msg;
458 eap_sim_state(data, FAILURE);
459 data->num_id_req = 0;
460 data->num_notification = 0;
462 wpa_printf(MSG_DEBUG, "EAP-SIM: Send Client-Error (error code %d)",
464 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
465 EAP_SIM_SUBTYPE_CLIENT_ERROR);
466 eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0);
467 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
471 static struct wpabuf * eap_sim_response_start(struct eap_sm *sm,
472 struct eap_sim_data *data, u8 id,
473 enum eap_sim_id_req id_req)
475 const u8 *identity = NULL;
476 size_t identity_len = 0;
477 struct eap_sim_msg *msg;
480 if (id_req == ANY_ID && data->reauth_id) {
481 identity = data->reauth_id;
482 identity_len = data->reauth_id_len;
484 } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) &&
486 identity = data->pseudonym;
487 identity_len = data->pseudonym_len;
488 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID);
489 } else if (id_req != NO_ID_REQ) {
490 identity = eap_get_config_identity(sm, &identity_len);
492 eap_sim_clear_identities(sm, data, CLEAR_PSEUDONYM |
496 if (id_req != NO_ID_REQ)
497 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
499 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Start (id=%d)", id);
500 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
501 EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START);
503 wpa_hexdump(MSG_DEBUG, " AT_NONCE_MT",
504 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
505 eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_MT, 0,
506 data->nonce_mt, EAP_SIM_NONCE_MT_LEN);
507 wpa_printf(MSG_DEBUG, " AT_SELECTED_VERSION %d",
508 data->selected_version);
509 eap_sim_msg_add(msg, EAP_SIM_AT_SELECTED_VERSION,
510 data->selected_version, NULL, 0);
514 wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY",
515 identity, identity_len);
516 eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len,
517 identity, identity_len);
520 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0);
524 static struct wpabuf * eap_sim_response_challenge(struct eap_sim_data *data,
527 struct eap_sim_msg *msg;
529 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Challenge (id=%d)", id);
530 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
531 EAP_SIM_SUBTYPE_CHALLENGE);
532 if (data->use_result_ind) {
533 wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
534 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
536 wpa_printf(MSG_DEBUG, " AT_MAC");
537 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
538 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut,
540 data->num_chal * EAP_SIM_SRES_LEN);
544 static struct wpabuf * eap_sim_response_reauth(struct eap_sim_data *data,
545 u8 id, int counter_too_small,
548 struct eap_sim_msg *msg;
549 unsigned int counter;
551 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)",
553 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM,
554 EAP_SIM_SUBTYPE_REAUTHENTICATION);
555 wpa_printf(MSG_DEBUG, " AT_IV");
556 wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
557 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA);
559 if (counter_too_small) {
560 wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL");
561 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0);
562 counter = data->counter_too_small;
564 counter = data->counter;
566 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter);
567 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0);
569 if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) {
570 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
572 eap_sim_msg_free(msg);
575 if (data->use_result_ind) {
576 wpa_printf(MSG_DEBUG, " AT_RESULT_IND");
577 eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0);
579 wpa_printf(MSG_DEBUG, " AT_MAC");
580 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
581 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, nonce_s,
582 EAP_SIM_NONCE_S_LEN);
586 static struct wpabuf * eap_sim_response_notification(struct eap_sim_data *data,
587 u8 id, u16 notification)
589 struct eap_sim_msg *msg;
590 u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL;
592 wpa_printf(MSG_DEBUG, "Generating EAP-SIM Notification (id=%d)", id);
593 msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id,
594 EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION);
595 if (k_aut && data->reauth) {
596 wpa_printf(MSG_DEBUG, " AT_IV");
597 wpa_printf(MSG_DEBUG, " AT_ENCR_DATA");
598 eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV,
599 EAP_SIM_AT_ENCR_DATA);
600 wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter);
601 eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter,
603 if (eap_sim_msg_add_encr_end(msg, data->k_encr,
604 EAP_SIM_AT_PADDING)) {
605 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt "
607 eap_sim_msg_free(msg);
612 wpa_printf(MSG_DEBUG, " AT_MAC");
613 eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC);
615 return eap_sim_msg_finish(msg, EAP_TYPE_SIM, k_aut, (u8 *) "", 0);
619 static struct wpabuf * eap_sim_process_start(struct eap_sm *sm,
620 struct eap_sim_data *data, u8 id,
621 struct eap_sim_attrs *attr)
623 int selected_version = -1, id_error;
627 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Start");
628 if (attr->version_list == NULL) {
629 wpa_printf(MSG_INFO, "EAP-SIM: No AT_VERSION_LIST in "
631 return eap_sim_client_error(data, id,
632 EAP_SIM_UNSUPPORTED_VERSION);
635 os_free(data->ver_list);
636 data->ver_list = os_malloc(attr->version_list_len);
637 if (data->ver_list == NULL) {
638 wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to allocate "
639 "memory for version list");
640 return eap_sim_client_error(data, id,
641 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
643 os_memcpy(data->ver_list, attr->version_list, attr->version_list_len);
644 data->ver_list_len = attr->version_list_len;
645 pos = data->ver_list;
646 for (i = 0; i < data->ver_list_len / 2; i++) {
647 int ver = pos[0] * 256 + pos[1];
649 if (eap_sim_supported_ver(ver)) {
650 selected_version = ver;
654 if (selected_version < 0) {
655 wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported "
657 return eap_sim_client_error(data, id,
658 EAP_SIM_UNSUPPORTED_VERSION);
660 wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d",
662 data->selected_version = selected_version;
665 switch (attr->id_req) {
669 if (data->num_id_req > 0)
674 if (data->num_id_req > 1)
679 if (data->num_id_req > 2)
685 wpa_printf(MSG_INFO, "EAP-SIM: Too many ID requests "
686 "used within one authentication");
687 return eap_sim_client_error(data, id,
688 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
691 return eap_sim_response_start(sm, data, id, attr->id_req);
695 static struct wpabuf * eap_sim_process_challenge(struct eap_sm *sm,
696 struct eap_sim_data *data,
698 const struct wpabuf *reqData,
699 struct eap_sim_attrs *attr)
703 struct eap_sim_attrs eattr;
706 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge");
708 if (!attr->mac || !attr->rand) {
709 wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
710 "did not include%s%s",
711 !attr->mac ? " AT_MAC" : "",
712 !attr->rand ? " AT_RAND" : "");
713 return eap_sim_client_error(data, id,
714 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
717 wpa_printf(MSG_DEBUG, "EAP-SIM: %lu challenges",
718 (unsigned long) attr->num_chal);
719 if (attr->num_chal < data->min_num_chal) {
720 wpa_printf(MSG_INFO, "EAP-SIM: Insufficient number of "
721 "challenges (%lu)", (unsigned long) attr->num_chal);
722 return eap_sim_client_error(data, id,
723 EAP_SIM_INSUFFICIENT_NUM_OF_CHAL);
725 if (attr->num_chal > 3) {
726 wpa_printf(MSG_INFO, "EAP-SIM: Too many challenges "
727 "(%lu)", (unsigned long) attr->num_chal);
728 return eap_sim_client_error(data, id,
729 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
732 /* Verify that RANDs are different */
733 if (os_memcmp(attr->rand, attr->rand + GSM_RAND_LEN,
734 GSM_RAND_LEN) == 0 ||
735 (attr->num_chal > 2 &&
736 (os_memcmp(attr->rand, attr->rand + 2 * GSM_RAND_LEN,
737 GSM_RAND_LEN) == 0 ||
738 os_memcmp(attr->rand + GSM_RAND_LEN,
739 attr->rand + 2 * GSM_RAND_LEN,
740 GSM_RAND_LEN) == 0))) {
741 wpa_printf(MSG_INFO, "EAP-SIM: Same RAND used multiple times");
742 return eap_sim_client_error(data, id,
743 EAP_SIM_RAND_NOT_FRESH);
746 os_memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN);
747 data->num_chal = attr->num_chal;
749 res = eap_sim_gsm_auth(sm, data);
751 wpa_printf(MSG_DEBUG, "EAP-SIM: Wait for external SIM processing");
755 wpa_printf(MSG_WARNING, "EAP-SIM: GSM authentication failed");
756 return eap_sim_client_error(data, id,
757 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
759 if (data->last_eap_identity) {
760 identity = data->last_eap_identity;
761 identity_len = data->last_eap_identity_len;
762 } else if (data->pseudonym) {
763 identity = data->pseudonym;
764 identity_len = data->pseudonym_len;
766 identity = eap_get_config_identity(sm, &identity_len);
767 wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Selected identity for MK "
768 "derivation", identity, identity_len);
769 eap_sim_derive_mk(identity, identity_len, data->nonce_mt,
770 data->selected_version, data->ver_list,
771 data->ver_list_len, data->num_chal,
772 (const u8 *) data->kc, data->mk);
773 eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk,
775 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, data->nonce_mt,
776 EAP_SIM_NONCE_MT_LEN)) {
777 wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message "
778 "used invalid AT_MAC");
779 return eap_sim_client_error(data, id,
780 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
783 /* Old reauthentication identity must not be used anymore. In
784 * other words, if no new reauth identity is received, full
785 * authentication will be used on next reauthentication (using
786 * pseudonym identity or permanent identity). */
787 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
789 if (attr->encr_data) {
791 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
792 attr->encr_data_len, attr->iv,
794 if (decrypted == NULL) {
795 return eap_sim_client_error(
796 data, id, EAP_SIM_UNABLE_TO_PROCESS_PACKET);
798 eap_sim_learn_ids(sm, data, &eattr);
802 if (data->result_ind && attr->result_ind)
803 data->use_result_ind = 1;
805 if (data->state != FAILURE) {
806 eap_sim_state(data, data->use_result_ind ?
807 RESULT_SUCCESS : SUCCESS);
810 data->num_id_req = 0;
811 data->num_notification = 0;
812 /* RFC 4186 specifies that counter is initialized to one after
813 * fullauth, but initializing it to zero makes it easier to implement
814 * reauth verification. */
816 return eap_sim_response_challenge(data, id);
820 static int eap_sim_process_notification_reauth(struct eap_sim_data *data,
821 struct eap_sim_attrs *attr)
823 struct eap_sim_attrs eattr;
826 if (attr->encr_data == NULL || attr->iv == NULL) {
827 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message after "
828 "reauth did not include encrypted data");
832 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
833 attr->encr_data_len, attr->iv, &eattr,
835 if (decrypted == NULL) {
836 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
837 "data from notification message");
841 if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) {
842 wpa_printf(MSG_WARNING, "EAP-SIM: Counter in notification "
843 "message does not match with counter in reauth "
854 static int eap_sim_process_notification_auth(struct eap_sim_data *data,
855 const struct wpabuf *reqData,
856 struct eap_sim_attrs *attr)
858 if (attr->mac == NULL) {
859 wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth "
860 "Notification message");
864 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
866 wpa_printf(MSG_WARNING, "EAP-SIM: Notification message "
867 "used invalid AT_MAC");
872 eap_sim_process_notification_reauth(data, attr)) {
873 wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification "
874 "message after reauth");
882 static struct wpabuf * eap_sim_process_notification(
883 struct eap_sm *sm, struct eap_sim_data *data, u8 id,
884 const struct wpabuf *reqData, struct eap_sim_attrs *attr)
886 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Notification");
887 if (data->num_notification > 0) {
888 wpa_printf(MSG_INFO, "EAP-SIM: too many notification "
889 "rounds (only one allowed)");
890 return eap_sim_client_error(data, id,
891 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
893 data->num_notification++;
894 if (attr->notification == -1) {
895 wpa_printf(MSG_INFO, "EAP-SIM: no AT_NOTIFICATION in "
896 "Notification message");
897 return eap_sim_client_error(data, id,
898 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
901 if ((attr->notification & 0x4000) == 0 &&
902 eap_sim_process_notification_auth(data, reqData, attr)) {
903 return eap_sim_client_error(data, id,
904 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
907 eap_sim_report_notification(sm->msg_ctx, attr->notification, 0);
908 if (attr->notification >= 0 && attr->notification < 32768) {
909 eap_sim_state(data, FAILURE);
910 } else if (attr->notification == EAP_SIM_SUCCESS &&
911 data->state == RESULT_SUCCESS)
912 eap_sim_state(data, SUCCESS);
913 return eap_sim_response_notification(data, id, attr->notification);
917 static struct wpabuf * eap_sim_process_reauthentication(
918 struct eap_sm *sm, struct eap_sim_data *data, u8 id,
919 const struct wpabuf *reqData, struct eap_sim_attrs *attr)
921 struct eap_sim_attrs eattr;
924 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication");
926 if (data->reauth_id == NULL) {
927 wpa_printf(MSG_WARNING, "EAP-SIM: Server is trying "
928 "reauthentication, but no reauth_id available");
929 return eap_sim_client_error(data, id,
930 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
934 if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0))
936 wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
937 "did not have valid AT_MAC");
938 return eap_sim_client_error(data, id,
939 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
942 if (attr->encr_data == NULL || attr->iv == NULL) {
943 wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication "
944 "message did not include encrypted data");
945 return eap_sim_client_error(data, id,
946 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
949 decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data,
950 attr->encr_data_len, attr->iv, &eattr,
952 if (decrypted == NULL) {
953 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted "
954 "data from reauthentication message");
955 return eap_sim_client_error(data, id,
956 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
959 if (eattr.nonce_s == NULL || eattr.counter < 0) {
960 wpa_printf(MSG_INFO, "EAP-SIM: (encr) No%s%s in reauth packet",
961 !eattr.nonce_s ? " AT_NONCE_S" : "",
962 eattr.counter < 0 ? " AT_COUNTER" : "");
964 return eap_sim_client_error(data, id,
965 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
968 if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) {
970 wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid counter "
971 "(%d <= %d)", eattr.counter, data->counter);
972 data->counter_too_small = eattr.counter;
974 /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current
975 * reauth_id must not be used to start a new reauthentication.
976 * However, since it was used in the last EAP-Response-Identity
977 * packet, it has to saved for the following fullauth to be
978 * used in MK derivation. */
979 os_free(data->last_eap_identity);
980 data->last_eap_identity = data->reauth_id;
981 data->last_eap_identity_len = data->reauth_id_len;
982 data->reauth_id = NULL;
983 data->reauth_id_len = 0;
985 res = eap_sim_response_reauth(data, id, 1, eattr.nonce_s);
990 data->counter = eattr.counter;
992 os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN);
993 wpa_hexdump(MSG_DEBUG, "EAP-SIM: (encr) AT_NONCE_S",
994 data->nonce_s, EAP_SIM_NONCE_S_LEN);
996 eap_sim_derive_keys_reauth(data->counter,
997 data->reauth_id, data->reauth_id_len,
998 data->nonce_s, data->mk, data->msk,
1000 eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1001 eap_sim_learn_ids(sm, data, &eattr);
1003 if (data->result_ind && attr->result_ind)
1004 data->use_result_ind = 1;
1006 if (data->state != FAILURE) {
1007 eap_sim_state(data, data->use_result_ind ?
1008 RESULT_SUCCESS : SUCCESS);
1011 data->num_id_req = 0;
1012 data->num_notification = 0;
1013 if (data->counter > EAP_SIM_MAX_FAST_REAUTHS) {
1014 wpa_printf(MSG_DEBUG, "EAP-SIM: Maximum number of "
1015 "fast reauths performed - force fullauth");
1016 eap_sim_clear_identities(sm, data,
1017 CLEAR_REAUTH_ID | CLEAR_EAP_ID);
1020 return eap_sim_response_reauth(data, id, 0, data->nonce_s);
1024 static struct wpabuf * eap_sim_process(struct eap_sm *sm, void *priv,
1025 struct eap_method_ret *ret,
1026 const struct wpabuf *reqData)
1028 struct eap_sim_data *data = priv;
1029 const struct eap_hdr *req;
1033 struct eap_sim_attrs attr;
1036 wpa_hexdump_buf(MSG_DEBUG, "EAP-SIM: EAP data", reqData);
1037 if (eap_get_config_identity(sm, &len) == NULL) {
1038 wpa_printf(MSG_INFO, "EAP-SIM: Identity not configured");
1039 eap_sm_request_identity(sm);
1044 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, reqData, &len);
1045 if (pos == NULL || len < 3) {
1049 req = wpabuf_head(reqData);
1050 id = req->identifier;
1051 len = be_to_host16(req->length);
1053 ret->ignore = FALSE;
1054 ret->methodState = METHOD_MAY_CONT;
1055 ret->decision = DECISION_FAIL;
1056 ret->allowNotifications = TRUE;
1059 wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype);
1060 pos += 2; /* Reserved */
1062 if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 0,
1064 res = eap_sim_client_error(data, id,
1065 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1070 case EAP_SIM_SUBTYPE_START:
1071 res = eap_sim_process_start(sm, data, id, &attr);
1073 case EAP_SIM_SUBTYPE_CHALLENGE:
1074 res = eap_sim_process_challenge(sm, data, id, reqData, &attr);
1076 case EAP_SIM_SUBTYPE_NOTIFICATION:
1077 res = eap_sim_process_notification(sm, data, id, reqData,
1080 case EAP_SIM_SUBTYPE_REAUTHENTICATION:
1081 res = eap_sim_process_reauthentication(sm, data, id, reqData,
1084 case EAP_SIM_SUBTYPE_CLIENT_ERROR:
1085 wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Client-Error");
1086 res = eap_sim_client_error(data, id,
1087 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1090 wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown subtype=%d", subtype);
1091 res = eap_sim_client_error(data, id,
1092 EAP_SIM_UNABLE_TO_PROCESS_PACKET);
1097 if (data->state == FAILURE) {
1098 ret->decision = DECISION_FAIL;
1099 ret->methodState = METHOD_DONE;
1100 } else if (data->state == SUCCESS) {
1101 ret->decision = data->use_result_ind ?
1102 DECISION_UNCOND_SUCC : DECISION_COND_SUCC;
1103 ret->methodState = data->use_result_ind ?
1104 METHOD_DONE : METHOD_MAY_CONT;
1105 } else if (data->state == RESULT_SUCCESS)
1106 ret->methodState = METHOD_CONT;
1108 if (ret->methodState == METHOD_DONE) {
1109 ret->allowNotifications = FALSE;
1116 static Boolean eap_sim_has_reauth_data(struct eap_sm *sm, void *priv)
1118 struct eap_sim_data *data = priv;
1119 return data->pseudonym || data->reauth_id;
1123 static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv)
1125 struct eap_sim_data *data = priv;
1126 eap_sim_clear_identities(sm, data, CLEAR_EAP_ID);
1127 data->use_result_ind = 0;
1128 eap_sim_clear_keys(data, 1);
1132 static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv)
1134 struct eap_sim_data *data = priv;
1135 if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) {
1136 wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data "
1141 data->num_id_req = 0;
1142 data->num_notification = 0;
1143 eap_sim_state(data, CONTINUE);
1148 static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv,
1151 struct eap_sim_data *data = priv;
1153 if (data->reauth_id) {
1154 *len = data->reauth_id_len;
1155 return data->reauth_id;
1158 if (data->pseudonym) {
1159 *len = data->pseudonym_len;
1160 return data->pseudonym;
1167 static Boolean eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv)
1169 struct eap_sim_data *data = priv;
1170 return data->state == SUCCESS;
1174 static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len)
1176 struct eap_sim_data *data = priv;
1179 if (data->state != SUCCESS)
1182 key = os_malloc(EAP_SIM_KEYING_DATA_LEN);
1186 *len = EAP_SIM_KEYING_DATA_LEN;
1187 os_memcpy(key, data->msk, EAP_SIM_KEYING_DATA_LEN);
1193 static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
1195 struct eap_sim_data *data = priv;
1198 if (data->state != SUCCESS)
1201 *len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN;
1202 id = os_malloc(*len);
1206 id[0] = EAP_TYPE_SIM;
1207 os_memcpy(id + 1, data->rand, data->num_chal * GSM_RAND_LEN);
1208 os_memcpy(id + 1 + data->num_chal * GSM_RAND_LEN, data->nonce_mt,
1209 EAP_SIM_NONCE_MT_LEN);
1210 wpa_hexdump(MSG_DEBUG, "EAP-SIM: Derived Session-Id", id, *len);
1216 static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
1218 struct eap_sim_data *data = priv;
1221 if (data->state != SUCCESS)
1224 key = os_malloc(EAP_EMSK_LEN);
1228 *len = EAP_EMSK_LEN;
1229 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
1235 int eap_peer_sim_register(void)
1237 struct eap_method *eap;
1240 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1241 EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM");
1245 eap->init = eap_sim_init;
1246 eap->deinit = eap_sim_deinit;
1247 eap->process = eap_sim_process;
1248 eap->isKeyAvailable = eap_sim_isKeyAvailable;
1249 eap->getKey = eap_sim_getKey;
1250 eap->getSessionId = eap_sim_get_session_id;
1251 eap->has_reauth_data = eap_sim_has_reauth_data;
1252 eap->deinit_for_reauth = eap_sim_deinit_for_reauth;
1253 eap->init_for_reauth = eap_sim_init_for_reauth;
1254 eap->get_identity = eap_sim_get_identity;
1255 eap->get_emsk = eap_sim_get_emsk;
1257 ret = eap_peer_method_register(eap);
1259 eap_peer_method_free(eap);