139beb93cSSam Leffler /* 239beb93cSSam Leffler * EAP peer method: EAP-SIM (RFC 4186) 3f05cddf9SRui Paulo * Copyright (c) 2004-2012, Jouni Malinen <j@w1.fi> 439beb93cSSam Leffler * 5f05cddf9SRui Paulo * This software may be distributed under the terms of the BSD license. 6f05cddf9SRui Paulo * See README for more details. 739beb93cSSam Leffler */ 839beb93cSSam Leffler 939beb93cSSam Leffler #include "includes.h" 1039beb93cSSam Leffler 1139beb93cSSam Leffler #include "common.h" 12e28a4053SRui Paulo #include "pcsc_funcs.h" 13e28a4053SRui Paulo #include "crypto/milenage.h" 14f05cddf9SRui Paulo #include "crypto/random.h" 1539beb93cSSam Leffler #include "eap_peer/eap_i.h" 1639beb93cSSam Leffler #include "eap_config.h" 1739beb93cSSam Leffler #include "eap_common/eap_sim_common.h" 1839beb93cSSam Leffler 1939beb93cSSam Leffler 2039beb93cSSam Leffler struct eap_sim_data { 2139beb93cSSam Leffler u8 *ver_list; 2239beb93cSSam Leffler size_t ver_list_len; 2339beb93cSSam Leffler int selected_version; 2439beb93cSSam Leffler size_t min_num_chal, num_chal; 2539beb93cSSam Leffler 2639beb93cSSam Leffler u8 kc[3][EAP_SIM_KC_LEN]; 2739beb93cSSam Leffler u8 sres[3][EAP_SIM_SRES_LEN]; 2839beb93cSSam Leffler u8 nonce_mt[EAP_SIM_NONCE_MT_LEN], nonce_s[EAP_SIM_NONCE_S_LEN]; 2939beb93cSSam Leffler u8 mk[EAP_SIM_MK_LEN]; 3039beb93cSSam Leffler u8 k_aut[EAP_SIM_K_AUT_LEN]; 3139beb93cSSam Leffler u8 k_encr[EAP_SIM_K_ENCR_LEN]; 3239beb93cSSam Leffler u8 msk[EAP_SIM_KEYING_DATA_LEN]; 3339beb93cSSam Leffler u8 emsk[EAP_EMSK_LEN]; 3439beb93cSSam Leffler u8 rand[3][GSM_RAND_LEN]; 35*206b73d0SCy Schubert u8 reauth_mac[EAP_SIM_MAC_LEN]; 3639beb93cSSam Leffler 3739beb93cSSam Leffler int num_id_req, num_notification; 3839beb93cSSam Leffler u8 *pseudonym; 3939beb93cSSam Leffler size_t pseudonym_len; 4039beb93cSSam Leffler u8 *reauth_id; 4139beb93cSSam Leffler size_t reauth_id_len; 4239beb93cSSam Leffler int reauth; 4339beb93cSSam Leffler unsigned int counter, counter_too_small; 4439beb93cSSam Leffler u8 *last_eap_identity; 4539beb93cSSam Leffler size_t last_eap_identity_len; 4639beb93cSSam Leffler enum { 475b9c547cSRui Paulo CONTINUE, RESULT_SUCCESS, SUCCESS, FAILURE 4839beb93cSSam Leffler } state; 4939beb93cSSam Leffler int result_ind, use_result_ind; 5085732ac8SCy Schubert int use_pseudonym; 5185732ac8SCy Schubert int error_code; 5239beb93cSSam Leffler }; 5339beb93cSSam Leffler 5439beb93cSSam Leffler 5539beb93cSSam Leffler #ifndef CONFIG_NO_STDOUT_DEBUG 5639beb93cSSam Leffler static const char * eap_sim_state_txt(int state) 5739beb93cSSam Leffler { 5839beb93cSSam Leffler switch (state) { 5939beb93cSSam Leffler case CONTINUE: 6039beb93cSSam Leffler return "CONTINUE"; 6139beb93cSSam Leffler case RESULT_SUCCESS: 6239beb93cSSam Leffler return "RESULT_SUCCESS"; 6339beb93cSSam Leffler case SUCCESS: 6439beb93cSSam Leffler return "SUCCESS"; 6539beb93cSSam Leffler case FAILURE: 6639beb93cSSam Leffler return "FAILURE"; 6739beb93cSSam Leffler default: 6839beb93cSSam Leffler return "?"; 6939beb93cSSam Leffler } 7039beb93cSSam Leffler } 7139beb93cSSam Leffler #endif /* CONFIG_NO_STDOUT_DEBUG */ 7239beb93cSSam Leffler 7339beb93cSSam Leffler 7439beb93cSSam Leffler static void eap_sim_state(struct eap_sim_data *data, int state) 7539beb93cSSam Leffler { 7639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: %s -> %s", 7739beb93cSSam Leffler eap_sim_state_txt(data->state), 7839beb93cSSam Leffler eap_sim_state_txt(state)); 7939beb93cSSam Leffler data->state = state; 8039beb93cSSam Leffler } 8139beb93cSSam Leffler 8239beb93cSSam Leffler 8339beb93cSSam Leffler static void * eap_sim_init(struct eap_sm *sm) 8439beb93cSSam Leffler { 8539beb93cSSam Leffler struct eap_sim_data *data; 8639beb93cSSam Leffler struct eap_peer_config *config = eap_get_config(sm); 8739beb93cSSam Leffler 8839beb93cSSam Leffler data = os_zalloc(sizeof(*data)); 8939beb93cSSam Leffler if (data == NULL) 9039beb93cSSam Leffler return NULL; 9139beb93cSSam Leffler 92f05cddf9SRui Paulo if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) { 9339beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data " 9439beb93cSSam Leffler "for NONCE_MT"); 9539beb93cSSam Leffler os_free(data); 9639beb93cSSam Leffler return NULL; 9739beb93cSSam Leffler } 9839beb93cSSam Leffler 9985732ac8SCy Schubert /* Zero is a valid error code, so we need to initialize */ 10085732ac8SCy Schubert data->error_code = NO_EAP_METHOD_ERROR; 10185732ac8SCy Schubert 10239beb93cSSam Leffler data->min_num_chal = 2; 10339beb93cSSam Leffler if (config && config->phase1) { 10439beb93cSSam Leffler char *pos = os_strstr(config->phase1, "sim_min_num_chal="); 10539beb93cSSam Leffler if (pos) { 10639beb93cSSam Leffler data->min_num_chal = atoi(pos + 17); 10739beb93cSSam Leffler if (data->min_num_chal < 2 || data->min_num_chal > 3) { 10839beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Invalid " 10939beb93cSSam Leffler "sim_min_num_chal configuration " 11039beb93cSSam Leffler "(%lu, expected 2 or 3)", 11139beb93cSSam Leffler (unsigned long) data->min_num_chal); 11239beb93cSSam Leffler os_free(data); 11339beb93cSSam Leffler return NULL; 11439beb93cSSam Leffler } 11539beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: Set minimum number of " 11639beb93cSSam Leffler "challenges to %lu", 11739beb93cSSam Leffler (unsigned long) data->min_num_chal); 11839beb93cSSam Leffler } 11939beb93cSSam Leffler 12039beb93cSSam Leffler data->result_ind = os_strstr(config->phase1, "result_ind=1") != 12139beb93cSSam Leffler NULL; 12239beb93cSSam Leffler } 12339beb93cSSam Leffler 12485732ac8SCy Schubert data->use_pseudonym = !sm->init_phase2; 12585732ac8SCy Schubert if (config && config->anonymous_identity && data->use_pseudonym) { 126f05cddf9SRui Paulo data->pseudonym = os_malloc(config->anonymous_identity_len); 127f05cddf9SRui Paulo if (data->pseudonym) { 128f05cddf9SRui Paulo os_memcpy(data->pseudonym, config->anonymous_identity, 129f05cddf9SRui Paulo config->anonymous_identity_len); 130f05cddf9SRui Paulo data->pseudonym_len = config->anonymous_identity_len; 131f05cddf9SRui Paulo } 132f05cddf9SRui Paulo } 133f05cddf9SRui Paulo 13439beb93cSSam Leffler eap_sim_state(data, CONTINUE); 13539beb93cSSam Leffler 13639beb93cSSam Leffler return data; 13739beb93cSSam Leffler } 13839beb93cSSam Leffler 13939beb93cSSam Leffler 1405b9c547cSRui Paulo static void eap_sim_clear_keys(struct eap_sim_data *data, int reauth) 1415b9c547cSRui Paulo { 1425b9c547cSRui Paulo if (!reauth) { 1435b9c547cSRui Paulo os_memset(data->mk, 0, EAP_SIM_MK_LEN); 1445b9c547cSRui Paulo os_memset(data->k_aut, 0, EAP_SIM_K_AUT_LEN); 1455b9c547cSRui Paulo os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN); 1465b9c547cSRui Paulo } 1475b9c547cSRui Paulo os_memset(data->kc, 0, 3 * EAP_SIM_KC_LEN); 1485b9c547cSRui Paulo os_memset(data->sres, 0, 3 * EAP_SIM_SRES_LEN); 1495b9c547cSRui Paulo os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN); 1505b9c547cSRui Paulo os_memset(data->emsk, 0, EAP_EMSK_LEN); 1515b9c547cSRui Paulo } 1525b9c547cSRui Paulo 1535b9c547cSRui Paulo 15439beb93cSSam Leffler static void eap_sim_deinit(struct eap_sm *sm, void *priv) 15539beb93cSSam Leffler { 15639beb93cSSam Leffler struct eap_sim_data *data = priv; 15739beb93cSSam Leffler if (data) { 15839beb93cSSam Leffler os_free(data->ver_list); 15939beb93cSSam Leffler os_free(data->pseudonym); 16039beb93cSSam Leffler os_free(data->reauth_id); 16139beb93cSSam Leffler os_free(data->last_eap_identity); 1625b9c547cSRui Paulo eap_sim_clear_keys(data, 0); 16339beb93cSSam Leffler os_free(data); 16439beb93cSSam Leffler } 16539beb93cSSam Leffler } 16639beb93cSSam Leffler 16739beb93cSSam Leffler 1685b9c547cSRui Paulo static int eap_sim_ext_sim_req(struct eap_sm *sm, struct eap_sim_data *data) 1695b9c547cSRui Paulo { 1705b9c547cSRui Paulo char req[200], *pos, *end; 1715b9c547cSRui Paulo size_t i; 1725b9c547cSRui Paulo 1735b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-SIM: Use external SIM processing"); 1745b9c547cSRui Paulo pos = req; 1755b9c547cSRui Paulo end = pos + sizeof(req); 1765b9c547cSRui Paulo pos += os_snprintf(pos, end - pos, "GSM-AUTH"); 1775b9c547cSRui Paulo for (i = 0; i < data->num_chal; i++) { 1785b9c547cSRui Paulo pos += os_snprintf(pos, end - pos, ":"); 1795b9c547cSRui Paulo pos += wpa_snprintf_hex(pos, end - pos, data->rand[i], 1805b9c547cSRui Paulo GSM_RAND_LEN); 1815b9c547cSRui Paulo } 1825b9c547cSRui Paulo 1835b9c547cSRui Paulo eap_sm_request_sim(sm, req); 1845b9c547cSRui Paulo return 1; 1855b9c547cSRui Paulo } 1865b9c547cSRui Paulo 1875b9c547cSRui Paulo 1885b9c547cSRui Paulo static int eap_sim_ext_sim_result(struct eap_sm *sm, struct eap_sim_data *data, 1895b9c547cSRui Paulo struct eap_peer_config *conf) 1905b9c547cSRui Paulo { 1915b9c547cSRui Paulo char *resp, *pos; 1925b9c547cSRui Paulo size_t i; 1935b9c547cSRui Paulo 1945b9c547cSRui Paulo wpa_printf(MSG_DEBUG, 1955b9c547cSRui Paulo "EAP-SIM: Use result from external SIM processing"); 1965b9c547cSRui Paulo 1975b9c547cSRui Paulo resp = conf->external_sim_resp; 1985b9c547cSRui Paulo conf->external_sim_resp = NULL; 1995b9c547cSRui Paulo 2005b9c547cSRui Paulo if (os_strncmp(resp, "GSM-AUTH:", 9) != 0) { 2015b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-SIM: Unrecognized external SIM processing response"); 2025b9c547cSRui Paulo os_free(resp); 2035b9c547cSRui Paulo return -1; 2045b9c547cSRui Paulo } 2055b9c547cSRui Paulo 2065b9c547cSRui Paulo pos = resp + 9; 2075b9c547cSRui Paulo for (i = 0; i < data->num_chal; i++) { 2085b9c547cSRui Paulo wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND", 2095b9c547cSRui Paulo data->rand[i], GSM_RAND_LEN); 2105b9c547cSRui Paulo 2115b9c547cSRui Paulo if (hexstr2bin(pos, data->kc[i], EAP_SIM_KC_LEN) < 0) 2125b9c547cSRui Paulo goto invalid; 2135b9c547cSRui Paulo wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc", 2145b9c547cSRui Paulo data->kc[i], EAP_SIM_KC_LEN); 2155b9c547cSRui Paulo pos += EAP_SIM_KC_LEN * 2; 2165b9c547cSRui Paulo if (*pos != ':') 2175b9c547cSRui Paulo goto invalid; 2185b9c547cSRui Paulo pos++; 2195b9c547cSRui Paulo 2205b9c547cSRui Paulo if (hexstr2bin(pos, data->sres[i], EAP_SIM_SRES_LEN) < 0) 2215b9c547cSRui Paulo goto invalid; 2225b9c547cSRui Paulo wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES", 2235b9c547cSRui Paulo data->sres[i], EAP_SIM_SRES_LEN); 2245b9c547cSRui Paulo pos += EAP_SIM_SRES_LEN * 2; 2255b9c547cSRui Paulo if (i + 1 < data->num_chal) { 2265b9c547cSRui Paulo if (*pos != ':') 2275b9c547cSRui Paulo goto invalid; 2285b9c547cSRui Paulo pos++; 2295b9c547cSRui Paulo } 2305b9c547cSRui Paulo } 2315b9c547cSRui Paulo 2325b9c547cSRui Paulo os_free(resp); 2335b9c547cSRui Paulo return 0; 2345b9c547cSRui Paulo 2355b9c547cSRui Paulo invalid: 2365b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-SIM: Invalid external SIM processing GSM-AUTH response"); 2375b9c547cSRui Paulo os_free(resp); 2385b9c547cSRui Paulo return -1; 2395b9c547cSRui Paulo } 2405b9c547cSRui Paulo 2415b9c547cSRui Paulo 24239beb93cSSam Leffler static int eap_sim_gsm_auth(struct eap_sm *sm, struct eap_sim_data *data) 24339beb93cSSam Leffler { 24439beb93cSSam Leffler struct eap_peer_config *conf; 24539beb93cSSam Leffler 24639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: GSM authentication algorithm"); 24739beb93cSSam Leffler 24839beb93cSSam Leffler conf = eap_get_config(sm); 24939beb93cSSam Leffler if (conf == NULL) 25039beb93cSSam Leffler return -1; 2515b9c547cSRui Paulo 2525b9c547cSRui Paulo if (sm->external_sim) { 2535b9c547cSRui Paulo if (conf->external_sim_resp) 2545b9c547cSRui Paulo return eap_sim_ext_sim_result(sm, data, conf); 2555b9c547cSRui Paulo else 2565b9c547cSRui Paulo return eap_sim_ext_sim_req(sm, data); 2575b9c547cSRui Paulo } 2585b9c547cSRui Paulo 259780fb4a2SCy Schubert #ifdef PCSC_FUNCS 26039beb93cSSam Leffler if (conf->pcsc) { 26139beb93cSSam Leffler if (scard_gsm_auth(sm->scard_ctx, data->rand[0], 26239beb93cSSam Leffler data->sres[0], data->kc[0]) || 26339beb93cSSam Leffler scard_gsm_auth(sm->scard_ctx, data->rand[1], 26439beb93cSSam Leffler data->sres[1], data->kc[1]) || 26539beb93cSSam Leffler (data->num_chal > 2 && 26639beb93cSSam Leffler scard_gsm_auth(sm->scard_ctx, data->rand[2], 26739beb93cSSam Leffler data->sres[2], data->kc[2]))) { 26839beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: GSM SIM " 26939beb93cSSam Leffler "authentication could not be completed"); 27039beb93cSSam Leffler return -1; 27139beb93cSSam Leffler } 27239beb93cSSam Leffler return 0; 27339beb93cSSam Leffler } 274780fb4a2SCy Schubert #endif /* PCSC_FUNCS */ 27539beb93cSSam Leffler 27639beb93cSSam Leffler #ifdef CONFIG_SIM_SIMULATOR 27739beb93cSSam Leffler if (conf->password) { 27839beb93cSSam Leffler u8 opc[16], k[16]; 27939beb93cSSam Leffler const char *pos; 28039beb93cSSam Leffler size_t i; 28139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: Use internal GSM-Milenage " 28239beb93cSSam Leffler "implementation for authentication"); 28339beb93cSSam Leffler if (conf->password_len < 65) { 28439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: invalid GSM-Milenage " 28539beb93cSSam Leffler "password"); 28639beb93cSSam Leffler return -1; 28739beb93cSSam Leffler } 28839beb93cSSam Leffler pos = (const char *) conf->password; 28939beb93cSSam Leffler if (hexstr2bin(pos, k, 16)) 29039beb93cSSam Leffler return -1; 29139beb93cSSam Leffler pos += 32; 29239beb93cSSam Leffler if (*pos != ':') 29339beb93cSSam Leffler return -1; 29439beb93cSSam Leffler pos++; 29539beb93cSSam Leffler 29639beb93cSSam Leffler if (hexstr2bin(pos, opc, 16)) 29739beb93cSSam Leffler return -1; 29839beb93cSSam Leffler 29939beb93cSSam Leffler for (i = 0; i < data->num_chal; i++) { 30039beb93cSSam Leffler if (gsm_milenage(opc, k, data->rand[i], 30139beb93cSSam Leffler data->sres[i], data->kc[i])) { 30239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: " 30339beb93cSSam Leffler "GSM-Milenage authentication " 30439beb93cSSam Leffler "could not be completed"); 30539beb93cSSam Leffler return -1; 30639beb93cSSam Leffler } 30739beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "EAP-SIM: RAND", 30839beb93cSSam Leffler data->rand[i], GSM_RAND_LEN); 30939beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: SRES", 31039beb93cSSam Leffler data->sres[i], EAP_SIM_SRES_LEN); 31139beb93cSSam Leffler wpa_hexdump_key(MSG_DEBUG, "EAP-SIM: Kc", 31239beb93cSSam Leffler data->kc[i], EAP_SIM_KC_LEN); 31339beb93cSSam Leffler } 31439beb93cSSam Leffler return 0; 31539beb93cSSam Leffler } 31639beb93cSSam Leffler #endif /* CONFIG_SIM_SIMULATOR */ 31739beb93cSSam Leffler 31839beb93cSSam Leffler #ifdef CONFIG_SIM_HARDCODED 31939beb93cSSam Leffler /* These hardcoded Kc and SRES values are used for testing. RAND to 32039beb93cSSam Leffler * KC/SREC mapping is very bogus as far as real authentication is 32139beb93cSSam Leffler * concerned, but it is quite useful for cases where the AS is rotating 32239beb93cSSam Leffler * the order of pre-configured values. */ 32339beb93cSSam Leffler { 32439beb93cSSam Leffler size_t i; 32539beb93cSSam Leffler 32639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: Use hardcoded Kc and SRES " 32739beb93cSSam Leffler "values for testing"); 32839beb93cSSam Leffler 32939beb93cSSam Leffler for (i = 0; i < data->num_chal; i++) { 33039beb93cSSam Leffler if (data->rand[i][0] == 0xaa) { 33139beb93cSSam Leffler os_memcpy(data->kc[i], 33239beb93cSSam Leffler "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7", 33339beb93cSSam Leffler EAP_SIM_KC_LEN); 33439beb93cSSam Leffler os_memcpy(data->sres[i], "\xd1\xd2\xd3\xd4", 33539beb93cSSam Leffler EAP_SIM_SRES_LEN); 33639beb93cSSam Leffler } else if (data->rand[i][0] == 0xbb) { 33739beb93cSSam Leffler os_memcpy(data->kc[i], 33839beb93cSSam Leffler "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7", 33939beb93cSSam Leffler EAP_SIM_KC_LEN); 34039beb93cSSam Leffler os_memcpy(data->sres[i], "\xe1\xe2\xe3\xe4", 34139beb93cSSam Leffler EAP_SIM_SRES_LEN); 34239beb93cSSam Leffler } else { 34339beb93cSSam Leffler os_memcpy(data->kc[i], 34439beb93cSSam Leffler "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7", 34539beb93cSSam Leffler EAP_SIM_KC_LEN); 34639beb93cSSam Leffler os_memcpy(data->sres[i], "\xf1\xf2\xf3\xf4", 34739beb93cSSam Leffler EAP_SIM_SRES_LEN); 34839beb93cSSam Leffler } 34939beb93cSSam Leffler } 35039beb93cSSam Leffler } 35139beb93cSSam Leffler 35239beb93cSSam Leffler return 0; 35339beb93cSSam Leffler 35439beb93cSSam Leffler #else /* CONFIG_SIM_HARDCODED */ 35539beb93cSSam Leffler 35639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: No GSM authentication algorithm " 35739beb93cSSam Leffler "enabled"); 35839beb93cSSam Leffler return -1; 35939beb93cSSam Leffler 36039beb93cSSam Leffler #endif /* CONFIG_SIM_HARDCODED */ 36139beb93cSSam Leffler } 36239beb93cSSam Leffler 36339beb93cSSam Leffler 36439beb93cSSam Leffler static int eap_sim_supported_ver(int version) 36539beb93cSSam Leffler { 36639beb93cSSam Leffler return version == EAP_SIM_VERSION; 36739beb93cSSam Leffler } 36839beb93cSSam Leffler 36939beb93cSSam Leffler 37039beb93cSSam Leffler #define CLEAR_PSEUDONYM 0x01 37139beb93cSSam Leffler #define CLEAR_REAUTH_ID 0x02 37239beb93cSSam Leffler #define CLEAR_EAP_ID 0x04 37339beb93cSSam Leffler 374f05cddf9SRui Paulo static void eap_sim_clear_identities(struct eap_sm *sm, 375f05cddf9SRui Paulo struct eap_sim_data *data, int id) 37639beb93cSSam Leffler { 377f05cddf9SRui Paulo if ((id & CLEAR_PSEUDONYM) && data->pseudonym) { 378f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old pseudonym"); 37939beb93cSSam Leffler os_free(data->pseudonym); 38039beb93cSSam Leffler data->pseudonym = NULL; 38139beb93cSSam Leffler data->pseudonym_len = 0; 38285732ac8SCy Schubert if (data->use_pseudonym) 383f05cddf9SRui Paulo eap_set_anon_id(sm, NULL, 0); 38439beb93cSSam Leffler } 385f05cddf9SRui Paulo if ((id & CLEAR_REAUTH_ID) && data->reauth_id) { 386f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old reauth_id"); 38739beb93cSSam Leffler os_free(data->reauth_id); 38839beb93cSSam Leffler data->reauth_id = NULL; 38939beb93cSSam Leffler data->reauth_id_len = 0; 39039beb93cSSam Leffler } 391f05cddf9SRui Paulo if ((id & CLEAR_EAP_ID) && data->last_eap_identity) { 392f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-SIM: forgetting old eap_id"); 39339beb93cSSam Leffler os_free(data->last_eap_identity); 39439beb93cSSam Leffler data->last_eap_identity = NULL; 39539beb93cSSam Leffler data->last_eap_identity_len = 0; 39639beb93cSSam Leffler } 39739beb93cSSam Leffler } 39839beb93cSSam Leffler 39939beb93cSSam Leffler 400f05cddf9SRui Paulo static int eap_sim_learn_ids(struct eap_sm *sm, struct eap_sim_data *data, 40139beb93cSSam Leffler struct eap_sim_attrs *attr) 40239beb93cSSam Leffler { 40339beb93cSSam Leffler if (attr->next_pseudonym) { 404f05cddf9SRui Paulo const u8 *identity = NULL; 405f05cddf9SRui Paulo size_t identity_len = 0; 406f05cddf9SRui Paulo const u8 *realm = NULL; 407f05cddf9SRui Paulo size_t realm_len = 0; 408f05cddf9SRui Paulo 409f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, 410f05cddf9SRui Paulo "EAP-SIM: (encr) AT_NEXT_PSEUDONYM", 411f05cddf9SRui Paulo attr->next_pseudonym, 412f05cddf9SRui Paulo attr->next_pseudonym_len); 41339beb93cSSam Leffler os_free(data->pseudonym); 414f05cddf9SRui Paulo /* Look for the realm of the permanent identity */ 415f05cddf9SRui Paulo identity = eap_get_config_identity(sm, &identity_len); 416f05cddf9SRui Paulo if (identity) { 417f05cddf9SRui Paulo for (realm = identity, realm_len = identity_len; 418f05cddf9SRui Paulo realm_len > 0; realm_len--, realm++) { 419f05cddf9SRui Paulo if (*realm == '@') 420f05cddf9SRui Paulo break; 421f05cddf9SRui Paulo } 422f05cddf9SRui Paulo } 423f05cddf9SRui Paulo data->pseudonym = os_malloc(attr->next_pseudonym_len + 424f05cddf9SRui Paulo realm_len); 42539beb93cSSam Leffler if (data->pseudonym == NULL) { 42639beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for " 42739beb93cSSam Leffler "next pseudonym"); 428f05cddf9SRui Paulo data->pseudonym_len = 0; 42939beb93cSSam Leffler return -1; 43039beb93cSSam Leffler } 43139beb93cSSam Leffler os_memcpy(data->pseudonym, attr->next_pseudonym, 43239beb93cSSam Leffler attr->next_pseudonym_len); 433f05cddf9SRui Paulo if (realm_len) { 434f05cddf9SRui Paulo os_memcpy(data->pseudonym + attr->next_pseudonym_len, 435f05cddf9SRui Paulo realm, realm_len); 436f05cddf9SRui Paulo } 437f05cddf9SRui Paulo data->pseudonym_len = attr->next_pseudonym_len + realm_len; 43885732ac8SCy Schubert if (data->use_pseudonym) 43985732ac8SCy Schubert eap_set_anon_id(sm, data->pseudonym, 44085732ac8SCy Schubert data->pseudonym_len); 44139beb93cSSam Leffler } 44239beb93cSSam Leffler 44339beb93cSSam Leffler if (attr->next_reauth_id) { 44439beb93cSSam Leffler os_free(data->reauth_id); 44585732ac8SCy Schubert data->reauth_id = os_memdup(attr->next_reauth_id, 44685732ac8SCy Schubert attr->next_reauth_id_len); 44739beb93cSSam Leffler if (data->reauth_id == NULL) { 44839beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: (encr) No memory for " 44939beb93cSSam Leffler "next reauth_id"); 450f05cddf9SRui Paulo data->reauth_id_len = 0; 45139beb93cSSam Leffler return -1; 45239beb93cSSam Leffler } 45339beb93cSSam Leffler data->reauth_id_len = attr->next_reauth_id_len; 45439beb93cSSam Leffler wpa_hexdump_ascii(MSG_DEBUG, 45539beb93cSSam Leffler "EAP-SIM: (encr) AT_NEXT_REAUTH_ID", 45639beb93cSSam Leffler data->reauth_id, 45739beb93cSSam Leffler data->reauth_id_len); 45839beb93cSSam Leffler } 45939beb93cSSam Leffler 46039beb93cSSam Leffler return 0; 46139beb93cSSam Leffler } 46239beb93cSSam Leffler 46339beb93cSSam Leffler 46439beb93cSSam Leffler static struct wpabuf * eap_sim_client_error(struct eap_sim_data *data, u8 id, 46539beb93cSSam Leffler int err) 46639beb93cSSam Leffler { 46739beb93cSSam Leffler struct eap_sim_msg *msg; 46839beb93cSSam Leffler 46939beb93cSSam Leffler eap_sim_state(data, FAILURE); 47039beb93cSSam Leffler data->num_id_req = 0; 47139beb93cSSam Leffler data->num_notification = 0; 47239beb93cSSam Leffler 473f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-SIM: Send Client-Error (error code %d)", 474f05cddf9SRui Paulo err); 47539beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM, 47639beb93cSSam Leffler EAP_SIM_SUBTYPE_CLIENT_ERROR); 47739beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0); 4785b9c547cSRui Paulo return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0); 47939beb93cSSam Leffler } 48039beb93cSSam Leffler 48139beb93cSSam Leffler 48239beb93cSSam Leffler static struct wpabuf * eap_sim_response_start(struct eap_sm *sm, 48339beb93cSSam Leffler struct eap_sim_data *data, u8 id, 48439beb93cSSam Leffler enum eap_sim_id_req id_req) 48539beb93cSSam Leffler { 48639beb93cSSam Leffler const u8 *identity = NULL; 48739beb93cSSam Leffler size_t identity_len = 0; 48839beb93cSSam Leffler struct eap_sim_msg *msg; 48939beb93cSSam Leffler 49039beb93cSSam Leffler data->reauth = 0; 49139beb93cSSam Leffler if (id_req == ANY_ID && data->reauth_id) { 49239beb93cSSam Leffler identity = data->reauth_id; 49339beb93cSSam Leffler identity_len = data->reauth_id_len; 49439beb93cSSam Leffler data->reauth = 1; 49539beb93cSSam Leffler } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) && 496*206b73d0SCy Schubert data->pseudonym && 497*206b73d0SCy Schubert !eap_sim_anonymous_username(data->pseudonym, 498*206b73d0SCy Schubert data->pseudonym_len)) { 49939beb93cSSam Leffler identity = data->pseudonym; 50039beb93cSSam Leffler identity_len = data->pseudonym_len; 501f05cddf9SRui Paulo eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID); 50239beb93cSSam Leffler } else if (id_req != NO_ID_REQ) { 50339beb93cSSam Leffler identity = eap_get_config_identity(sm, &identity_len); 50439beb93cSSam Leffler if (identity) { 505*206b73d0SCy Schubert int ids = CLEAR_PSEUDONYM | CLEAR_REAUTH_ID; 506*206b73d0SCy Schubert 507*206b73d0SCy Schubert if (data->pseudonym && 508*206b73d0SCy Schubert eap_sim_anonymous_username(data->pseudonym, 509*206b73d0SCy Schubert data->pseudonym_len)) 510*206b73d0SCy Schubert ids &= ~CLEAR_PSEUDONYM; 511*206b73d0SCy Schubert eap_sim_clear_identities(sm, data, ids); 51239beb93cSSam Leffler } 51339beb93cSSam Leffler } 51439beb93cSSam Leffler if (id_req != NO_ID_REQ) 515f05cddf9SRui Paulo eap_sim_clear_identities(sm, data, CLEAR_EAP_ID); 51639beb93cSSam Leffler 51739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-SIM Start (id=%d)", id); 51839beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, 51939beb93cSSam Leffler EAP_TYPE_SIM, EAP_SIM_SUBTYPE_START); 52039beb93cSSam Leffler if (!data->reauth) { 52139beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, " AT_NONCE_MT", 52239beb93cSSam Leffler data->nonce_mt, EAP_SIM_NONCE_MT_LEN); 52339beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_NONCE_MT, 0, 52439beb93cSSam Leffler data->nonce_mt, EAP_SIM_NONCE_MT_LEN); 52539beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_SELECTED_VERSION %d", 52639beb93cSSam Leffler data->selected_version); 52739beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_SELECTED_VERSION, 52839beb93cSSam Leffler data->selected_version, NULL, 0); 52939beb93cSSam Leffler } 53039beb93cSSam Leffler 53139beb93cSSam Leffler if (identity) { 53239beb93cSSam Leffler wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY", 53339beb93cSSam Leffler identity, identity_len); 53439beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len, 53539beb93cSSam Leffler identity, identity_len); 53639beb93cSSam Leffler } 53739beb93cSSam Leffler 5385b9c547cSRui Paulo return eap_sim_msg_finish(msg, EAP_TYPE_SIM, NULL, NULL, 0); 53939beb93cSSam Leffler } 54039beb93cSSam Leffler 54139beb93cSSam Leffler 54239beb93cSSam Leffler static struct wpabuf * eap_sim_response_challenge(struct eap_sim_data *data, 54339beb93cSSam Leffler u8 id) 54439beb93cSSam Leffler { 54539beb93cSSam Leffler struct eap_sim_msg *msg; 54639beb93cSSam Leffler 54739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-SIM Challenge (id=%d)", id); 54839beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM, 54939beb93cSSam Leffler EAP_SIM_SUBTYPE_CHALLENGE); 55039beb93cSSam Leffler if (data->use_result_ind) { 55139beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_RESULT_IND"); 55239beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0); 55339beb93cSSam Leffler } 55439beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_MAC"); 55539beb93cSSam Leffler eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC); 5565b9c547cSRui Paulo return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, 5575b9c547cSRui Paulo (u8 *) data->sres, 55839beb93cSSam Leffler data->num_chal * EAP_SIM_SRES_LEN); 55939beb93cSSam Leffler } 56039beb93cSSam Leffler 56139beb93cSSam Leffler 56239beb93cSSam Leffler static struct wpabuf * eap_sim_response_reauth(struct eap_sim_data *data, 563f05cddf9SRui Paulo u8 id, int counter_too_small, 564f05cddf9SRui Paulo const u8 *nonce_s) 56539beb93cSSam Leffler { 56639beb93cSSam Leffler struct eap_sim_msg *msg; 56739beb93cSSam Leffler unsigned int counter; 56839beb93cSSam Leffler 56939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-SIM Reauthentication (id=%d)", 57039beb93cSSam Leffler id); 57139beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, EAP_TYPE_SIM, 57239beb93cSSam Leffler EAP_SIM_SUBTYPE_REAUTHENTICATION); 57339beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_IV"); 57439beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_ENCR_DATA"); 57539beb93cSSam Leffler eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA); 57639beb93cSSam Leffler 57739beb93cSSam Leffler if (counter_too_small) { 57839beb93cSSam Leffler wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL"); 57939beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0); 58039beb93cSSam Leffler counter = data->counter_too_small; 58139beb93cSSam Leffler } else 58239beb93cSSam Leffler counter = data->counter; 58339beb93cSSam Leffler 58439beb93cSSam Leffler wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter); 58539beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0); 58639beb93cSSam Leffler 58739beb93cSSam Leffler if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) { 58839beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt " 58939beb93cSSam Leffler "AT_ENCR_DATA"); 59039beb93cSSam Leffler eap_sim_msg_free(msg); 59139beb93cSSam Leffler return NULL; 59239beb93cSSam Leffler } 59339beb93cSSam Leffler if (data->use_result_ind) { 59439beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_RESULT_IND"); 59539beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0); 59639beb93cSSam Leffler } 59739beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_MAC"); 59839beb93cSSam Leffler eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC); 5995b9c547cSRui Paulo return eap_sim_msg_finish(msg, EAP_TYPE_SIM, data->k_aut, nonce_s, 60039beb93cSSam Leffler EAP_SIM_NONCE_S_LEN); 60139beb93cSSam Leffler } 60239beb93cSSam Leffler 60339beb93cSSam Leffler 60439beb93cSSam Leffler static struct wpabuf * eap_sim_response_notification(struct eap_sim_data *data, 60539beb93cSSam Leffler u8 id, u16 notification) 60639beb93cSSam Leffler { 60739beb93cSSam Leffler struct eap_sim_msg *msg; 60839beb93cSSam Leffler u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL; 60939beb93cSSam Leffler 61039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-SIM Notification (id=%d)", id); 61139beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, 61239beb93cSSam Leffler EAP_TYPE_SIM, EAP_SIM_SUBTYPE_NOTIFICATION); 61339beb93cSSam Leffler if (k_aut && data->reauth) { 61439beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_IV"); 61539beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_ENCR_DATA"); 61639beb93cSSam Leffler eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, 61739beb93cSSam Leffler EAP_SIM_AT_ENCR_DATA); 61839beb93cSSam Leffler wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter); 61939beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter, 62039beb93cSSam Leffler NULL, 0); 62139beb93cSSam Leffler if (eap_sim_msg_add_encr_end(msg, data->k_encr, 62239beb93cSSam Leffler EAP_SIM_AT_PADDING)) { 62339beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Failed to encrypt " 62439beb93cSSam Leffler "AT_ENCR_DATA"); 62539beb93cSSam Leffler eap_sim_msg_free(msg); 62639beb93cSSam Leffler return NULL; 62739beb93cSSam Leffler } 62839beb93cSSam Leffler } 62939beb93cSSam Leffler if (k_aut) { 63039beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_MAC"); 63139beb93cSSam Leffler eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC); 63239beb93cSSam Leffler } 6335b9c547cSRui Paulo return eap_sim_msg_finish(msg, EAP_TYPE_SIM, k_aut, (u8 *) "", 0); 63439beb93cSSam Leffler } 63539beb93cSSam Leffler 63639beb93cSSam Leffler 63739beb93cSSam Leffler static struct wpabuf * eap_sim_process_start(struct eap_sm *sm, 63839beb93cSSam Leffler struct eap_sim_data *data, u8 id, 63939beb93cSSam Leffler struct eap_sim_attrs *attr) 64039beb93cSSam Leffler { 64139beb93cSSam Leffler int selected_version = -1, id_error; 64239beb93cSSam Leffler size_t i; 64339beb93cSSam Leffler u8 *pos; 64439beb93cSSam Leffler 64539beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Start"); 64639beb93cSSam Leffler if (attr->version_list == NULL) { 64739beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: No AT_VERSION_LIST in " 64839beb93cSSam Leffler "SIM/Start"); 64939beb93cSSam Leffler return eap_sim_client_error(data, id, 65039beb93cSSam Leffler EAP_SIM_UNSUPPORTED_VERSION); 65139beb93cSSam Leffler } 65239beb93cSSam Leffler 65339beb93cSSam Leffler os_free(data->ver_list); 65485732ac8SCy Schubert data->ver_list = os_memdup(attr->version_list, attr->version_list_len); 65539beb93cSSam Leffler if (data->ver_list == NULL) { 65639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: Failed to allocate " 65739beb93cSSam Leffler "memory for version list"); 65839beb93cSSam Leffler return eap_sim_client_error(data, id, 65939beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 66039beb93cSSam Leffler } 66139beb93cSSam Leffler data->ver_list_len = attr->version_list_len; 66239beb93cSSam Leffler pos = data->ver_list; 66339beb93cSSam Leffler for (i = 0; i < data->ver_list_len / 2; i++) { 66439beb93cSSam Leffler int ver = pos[0] * 256 + pos[1]; 66539beb93cSSam Leffler pos += 2; 66639beb93cSSam Leffler if (eap_sim_supported_ver(ver)) { 66739beb93cSSam Leffler selected_version = ver; 66839beb93cSSam Leffler break; 66939beb93cSSam Leffler } 67039beb93cSSam Leffler } 67139beb93cSSam Leffler if (selected_version < 0) { 67239beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: Could not find a supported " 67339beb93cSSam Leffler "version"); 67439beb93cSSam Leffler return eap_sim_client_error(data, id, 67539beb93cSSam Leffler EAP_SIM_UNSUPPORTED_VERSION); 67639beb93cSSam Leffler } 67739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: Selected Version %d", 67839beb93cSSam Leffler selected_version); 67939beb93cSSam Leffler data->selected_version = selected_version; 68039beb93cSSam Leffler 68139beb93cSSam Leffler id_error = 0; 68239beb93cSSam Leffler switch (attr->id_req) { 68339beb93cSSam Leffler case NO_ID_REQ: 68439beb93cSSam Leffler break; 68539beb93cSSam Leffler case ANY_ID: 68639beb93cSSam Leffler if (data->num_id_req > 0) 68739beb93cSSam Leffler id_error++; 68839beb93cSSam Leffler data->num_id_req++; 68939beb93cSSam Leffler break; 69039beb93cSSam Leffler case FULLAUTH_ID: 69139beb93cSSam Leffler if (data->num_id_req > 1) 69239beb93cSSam Leffler id_error++; 69339beb93cSSam Leffler data->num_id_req++; 69439beb93cSSam Leffler break; 69539beb93cSSam Leffler case PERMANENT_ID: 69639beb93cSSam Leffler if (data->num_id_req > 2) 69739beb93cSSam Leffler id_error++; 69839beb93cSSam Leffler data->num_id_req++; 69939beb93cSSam Leffler break; 70039beb93cSSam Leffler } 70139beb93cSSam Leffler if (id_error) { 70239beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: Too many ID requests " 70339beb93cSSam Leffler "used within one authentication"); 70439beb93cSSam Leffler return eap_sim_client_error(data, id, 70539beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 70639beb93cSSam Leffler } 70739beb93cSSam Leffler 70839beb93cSSam Leffler return eap_sim_response_start(sm, data, id, attr->id_req); 70939beb93cSSam Leffler } 71039beb93cSSam Leffler 71139beb93cSSam Leffler 71239beb93cSSam Leffler static struct wpabuf * eap_sim_process_challenge(struct eap_sm *sm, 71339beb93cSSam Leffler struct eap_sim_data *data, 71439beb93cSSam Leffler u8 id, 71539beb93cSSam Leffler const struct wpabuf *reqData, 71639beb93cSSam Leffler struct eap_sim_attrs *attr) 71739beb93cSSam Leffler { 71839beb93cSSam Leffler const u8 *identity; 71939beb93cSSam Leffler size_t identity_len; 72039beb93cSSam Leffler struct eap_sim_attrs eattr; 7215b9c547cSRui Paulo int res; 72239beb93cSSam Leffler 72339beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Challenge"); 72439beb93cSSam Leffler data->reauth = 0; 72539beb93cSSam Leffler if (!attr->mac || !attr->rand) { 72639beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message " 72739beb93cSSam Leffler "did not include%s%s", 72839beb93cSSam Leffler !attr->mac ? " AT_MAC" : "", 72939beb93cSSam Leffler !attr->rand ? " AT_RAND" : ""); 73039beb93cSSam Leffler return eap_sim_client_error(data, id, 73139beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 73239beb93cSSam Leffler } 73339beb93cSSam Leffler 73439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: %lu challenges", 73539beb93cSSam Leffler (unsigned long) attr->num_chal); 73639beb93cSSam Leffler if (attr->num_chal < data->min_num_chal) { 73739beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: Insufficient number of " 73839beb93cSSam Leffler "challenges (%lu)", (unsigned long) attr->num_chal); 73939beb93cSSam Leffler return eap_sim_client_error(data, id, 74039beb93cSSam Leffler EAP_SIM_INSUFFICIENT_NUM_OF_CHAL); 74139beb93cSSam Leffler } 74239beb93cSSam Leffler if (attr->num_chal > 3) { 74339beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: Too many challenges " 74439beb93cSSam Leffler "(%lu)", (unsigned long) attr->num_chal); 74539beb93cSSam Leffler return eap_sim_client_error(data, id, 74639beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 74739beb93cSSam Leffler } 74839beb93cSSam Leffler 74939beb93cSSam Leffler /* Verify that RANDs are different */ 75039beb93cSSam Leffler if (os_memcmp(attr->rand, attr->rand + GSM_RAND_LEN, 75139beb93cSSam Leffler GSM_RAND_LEN) == 0 || 75239beb93cSSam Leffler (attr->num_chal > 2 && 75339beb93cSSam Leffler (os_memcmp(attr->rand, attr->rand + 2 * GSM_RAND_LEN, 75439beb93cSSam Leffler GSM_RAND_LEN) == 0 || 75539beb93cSSam Leffler os_memcmp(attr->rand + GSM_RAND_LEN, 75639beb93cSSam Leffler attr->rand + 2 * GSM_RAND_LEN, 75739beb93cSSam Leffler GSM_RAND_LEN) == 0))) { 75839beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: Same RAND used multiple times"); 75939beb93cSSam Leffler return eap_sim_client_error(data, id, 76039beb93cSSam Leffler EAP_SIM_RAND_NOT_FRESH); 76139beb93cSSam Leffler } 76239beb93cSSam Leffler 76339beb93cSSam Leffler os_memcpy(data->rand, attr->rand, attr->num_chal * GSM_RAND_LEN); 76439beb93cSSam Leffler data->num_chal = attr->num_chal; 76539beb93cSSam Leffler 7665b9c547cSRui Paulo res = eap_sim_gsm_auth(sm, data); 7675b9c547cSRui Paulo if (res > 0) { 7685b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-SIM: Wait for external SIM processing"); 7695b9c547cSRui Paulo return NULL; 7705b9c547cSRui Paulo } 7715b9c547cSRui Paulo if (res) { 77239beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: GSM authentication failed"); 77339beb93cSSam Leffler return eap_sim_client_error(data, id, 77439beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 77539beb93cSSam Leffler } 77639beb93cSSam Leffler if (data->last_eap_identity) { 77739beb93cSSam Leffler identity = data->last_eap_identity; 77839beb93cSSam Leffler identity_len = data->last_eap_identity_len; 779*206b73d0SCy Schubert } else if (data->pseudonym && 780*206b73d0SCy Schubert !eap_sim_anonymous_username(data->pseudonym, 781*206b73d0SCy Schubert data->pseudonym_len)) { 78239beb93cSSam Leffler identity = data->pseudonym; 78339beb93cSSam Leffler identity_len = data->pseudonym_len; 78485732ac8SCy Schubert } else { 78585732ac8SCy Schubert struct eap_peer_config *config; 78685732ac8SCy Schubert 78785732ac8SCy Schubert config = eap_get_config(sm); 78885732ac8SCy Schubert if (config && config->imsi_identity) { 78985732ac8SCy Schubert identity = config->imsi_identity; 79085732ac8SCy Schubert identity_len = config->imsi_identity_len; 79185732ac8SCy Schubert } else { 79239beb93cSSam Leffler identity = eap_get_config_identity(sm, &identity_len); 79385732ac8SCy Schubert } 79485732ac8SCy Schubert } 79539beb93cSSam Leffler wpa_hexdump_ascii(MSG_DEBUG, "EAP-SIM: Selected identity for MK " 79639beb93cSSam Leffler "derivation", identity, identity_len); 79739beb93cSSam Leffler eap_sim_derive_mk(identity, identity_len, data->nonce_mt, 79839beb93cSSam Leffler data->selected_version, data->ver_list, 79939beb93cSSam Leffler data->ver_list_len, data->num_chal, 80039beb93cSSam Leffler (const u8 *) data->kc, data->mk); 80139beb93cSSam Leffler eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, data->msk, 80239beb93cSSam Leffler data->emsk); 80339beb93cSSam Leffler if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, data->nonce_mt, 80439beb93cSSam Leffler EAP_SIM_NONCE_MT_LEN)) { 80539beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Challenge message " 80639beb93cSSam Leffler "used invalid AT_MAC"); 807*206b73d0SCy Schubert #ifdef TEST_FUZZ 808*206b73d0SCy Schubert wpa_printf(MSG_INFO, 809*206b73d0SCy Schubert "TEST: Ignore AT_MAC mismatch for fuzz testing"); 810*206b73d0SCy Schubert #else /* TEST_FUZZ */ 81139beb93cSSam Leffler return eap_sim_client_error(data, id, 81239beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 813*206b73d0SCy Schubert #endif /* TEST_FUZZ */ 81439beb93cSSam Leffler } 81539beb93cSSam Leffler 816f05cddf9SRui Paulo /* Old reauthentication identity must not be used anymore. In 817f05cddf9SRui Paulo * other words, if no new reauth identity is received, full 818f05cddf9SRui Paulo * authentication will be used on next reauthentication (using 819f05cddf9SRui Paulo * pseudonym identity or permanent identity). */ 820f05cddf9SRui Paulo eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID); 82139beb93cSSam Leffler 82239beb93cSSam Leffler if (attr->encr_data) { 82339beb93cSSam Leffler u8 *decrypted; 82439beb93cSSam Leffler decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data, 82539beb93cSSam Leffler attr->encr_data_len, attr->iv, 82639beb93cSSam Leffler &eattr, 0); 82739beb93cSSam Leffler if (decrypted == NULL) { 82839beb93cSSam Leffler return eap_sim_client_error( 82939beb93cSSam Leffler data, id, EAP_SIM_UNABLE_TO_PROCESS_PACKET); 83039beb93cSSam Leffler } 831f05cddf9SRui Paulo eap_sim_learn_ids(sm, data, &eattr); 83239beb93cSSam Leffler os_free(decrypted); 83339beb93cSSam Leffler } 83439beb93cSSam Leffler 83539beb93cSSam Leffler if (data->result_ind && attr->result_ind) 83639beb93cSSam Leffler data->use_result_ind = 1; 83739beb93cSSam Leffler 8385b9c547cSRui Paulo if (data->state != FAILURE) { 83939beb93cSSam Leffler eap_sim_state(data, data->use_result_ind ? 84039beb93cSSam Leffler RESULT_SUCCESS : SUCCESS); 84139beb93cSSam Leffler } 84239beb93cSSam Leffler 84339beb93cSSam Leffler data->num_id_req = 0; 84439beb93cSSam Leffler data->num_notification = 0; 84539beb93cSSam Leffler /* RFC 4186 specifies that counter is initialized to one after 84639beb93cSSam Leffler * fullauth, but initializing it to zero makes it easier to implement 84739beb93cSSam Leffler * reauth verification. */ 84839beb93cSSam Leffler data->counter = 0; 84939beb93cSSam Leffler return eap_sim_response_challenge(data, id); 85039beb93cSSam Leffler } 85139beb93cSSam Leffler 85239beb93cSSam Leffler 85339beb93cSSam Leffler static int eap_sim_process_notification_reauth(struct eap_sim_data *data, 85439beb93cSSam Leffler struct eap_sim_attrs *attr) 85539beb93cSSam Leffler { 85639beb93cSSam Leffler struct eap_sim_attrs eattr; 85739beb93cSSam Leffler u8 *decrypted; 85839beb93cSSam Leffler 85939beb93cSSam Leffler if (attr->encr_data == NULL || attr->iv == NULL) { 86039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Notification message after " 86139beb93cSSam Leffler "reauth did not include encrypted data"); 86239beb93cSSam Leffler return -1; 86339beb93cSSam Leffler } 86439beb93cSSam Leffler 86539beb93cSSam Leffler decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data, 86639beb93cSSam Leffler attr->encr_data_len, attr->iv, &eattr, 86739beb93cSSam Leffler 0); 86839beb93cSSam Leffler if (decrypted == NULL) { 86939beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted " 87039beb93cSSam Leffler "data from notification message"); 87139beb93cSSam Leffler return -1; 87239beb93cSSam Leffler } 87339beb93cSSam Leffler 87439beb93cSSam Leffler if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) { 87539beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Counter in notification " 87639beb93cSSam Leffler "message does not match with counter in reauth " 87739beb93cSSam Leffler "message"); 87839beb93cSSam Leffler os_free(decrypted); 87939beb93cSSam Leffler return -1; 88039beb93cSSam Leffler } 88139beb93cSSam Leffler 88239beb93cSSam Leffler os_free(decrypted); 88339beb93cSSam Leffler return 0; 88439beb93cSSam Leffler } 88539beb93cSSam Leffler 88639beb93cSSam Leffler 88739beb93cSSam Leffler static int eap_sim_process_notification_auth(struct eap_sim_data *data, 88839beb93cSSam Leffler const struct wpabuf *reqData, 88939beb93cSSam Leffler struct eap_sim_attrs *attr) 89039beb93cSSam Leffler { 89139beb93cSSam Leffler if (attr->mac == NULL) { 89239beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: no AT_MAC in after_auth " 89339beb93cSSam Leffler "Notification message"); 89439beb93cSSam Leffler return -1; 89539beb93cSSam Leffler } 89639beb93cSSam Leffler 89739beb93cSSam Leffler if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0)) 89839beb93cSSam Leffler { 89939beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Notification message " 90039beb93cSSam Leffler "used invalid AT_MAC"); 90139beb93cSSam Leffler return -1; 90239beb93cSSam Leffler } 90339beb93cSSam Leffler 90439beb93cSSam Leffler if (data->reauth && 90539beb93cSSam Leffler eap_sim_process_notification_reauth(data, attr)) { 90639beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Invalid notification " 90739beb93cSSam Leffler "message after reauth"); 90839beb93cSSam Leffler return -1; 90939beb93cSSam Leffler } 91039beb93cSSam Leffler 91139beb93cSSam Leffler return 0; 91239beb93cSSam Leffler } 91339beb93cSSam Leffler 91439beb93cSSam Leffler 91539beb93cSSam Leffler static struct wpabuf * eap_sim_process_notification( 91639beb93cSSam Leffler struct eap_sm *sm, struct eap_sim_data *data, u8 id, 91739beb93cSSam Leffler const struct wpabuf *reqData, struct eap_sim_attrs *attr) 91839beb93cSSam Leffler { 91939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Notification"); 92039beb93cSSam Leffler if (data->num_notification > 0) { 92139beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: too many notification " 92239beb93cSSam Leffler "rounds (only one allowed)"); 92339beb93cSSam Leffler return eap_sim_client_error(data, id, 92439beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 92539beb93cSSam Leffler } 92639beb93cSSam Leffler data->num_notification++; 92739beb93cSSam Leffler if (attr->notification == -1) { 92839beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: no AT_NOTIFICATION in " 92939beb93cSSam Leffler "Notification message"); 93039beb93cSSam Leffler return eap_sim_client_error(data, id, 93139beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 93239beb93cSSam Leffler } 93339beb93cSSam Leffler 93439beb93cSSam Leffler if ((attr->notification & 0x4000) == 0 && 93539beb93cSSam Leffler eap_sim_process_notification_auth(data, reqData, attr)) { 93639beb93cSSam Leffler return eap_sim_client_error(data, id, 93739beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 93839beb93cSSam Leffler } 93939beb93cSSam Leffler 94039beb93cSSam Leffler eap_sim_report_notification(sm->msg_ctx, attr->notification, 0); 94139beb93cSSam Leffler if (attr->notification >= 0 && attr->notification < 32768) { 94285732ac8SCy Schubert data->error_code = attr->notification; 94339beb93cSSam Leffler eap_sim_state(data, FAILURE); 94439beb93cSSam Leffler } else if (attr->notification == EAP_SIM_SUCCESS && 94539beb93cSSam Leffler data->state == RESULT_SUCCESS) 94639beb93cSSam Leffler eap_sim_state(data, SUCCESS); 94739beb93cSSam Leffler return eap_sim_response_notification(data, id, attr->notification); 94839beb93cSSam Leffler } 94939beb93cSSam Leffler 95039beb93cSSam Leffler 95139beb93cSSam Leffler static struct wpabuf * eap_sim_process_reauthentication( 95239beb93cSSam Leffler struct eap_sm *sm, struct eap_sim_data *data, u8 id, 95339beb93cSSam Leffler const struct wpabuf *reqData, struct eap_sim_attrs *attr) 95439beb93cSSam Leffler { 95539beb93cSSam Leffler struct eap_sim_attrs eattr; 95639beb93cSSam Leffler u8 *decrypted; 95739beb93cSSam Leffler 95839beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Reauthentication"); 95939beb93cSSam Leffler 96039beb93cSSam Leffler if (data->reauth_id == NULL) { 96139beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Server is trying " 96239beb93cSSam Leffler "reauthentication, but no reauth_id available"); 96339beb93cSSam Leffler return eap_sim_client_error(data, id, 96439beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 96539beb93cSSam Leffler } 96639beb93cSSam Leffler 96739beb93cSSam Leffler data->reauth = 1; 96839beb93cSSam Leffler if (eap_sim_verify_mac(data->k_aut, reqData, attr->mac, (u8 *) "", 0)) 96939beb93cSSam Leffler { 97039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication " 97139beb93cSSam Leffler "did not have valid AT_MAC"); 972*206b73d0SCy Schubert #ifdef TEST_FUZZ 973*206b73d0SCy Schubert wpa_printf(MSG_INFO, 974*206b73d0SCy Schubert "TEST: Ignore AT_MAC mismatch for fuzz testing"); 975*206b73d0SCy Schubert #else /* TEST_FUZZ */ 97639beb93cSSam Leffler return eap_sim_client_error(data, id, 97739beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 978*206b73d0SCy Schubert #endif /* TEST_FUZZ */ 97939beb93cSSam Leffler } 98039beb93cSSam Leffler 981*206b73d0SCy Schubert /* At this stage the received MAC has been verified. Use this MAC for 982*206b73d0SCy Schubert * reauth Session-Id calculation if all other checks pass. 983*206b73d0SCy Schubert * The peer does not use the local MAC but the received MAC in deriving 984*206b73d0SCy Schubert * Session-Id. */ 985*206b73d0SCy Schubert #ifdef TEST_FUZZ 986*206b73d0SCy Schubert if (attr->mac) 987*206b73d0SCy Schubert os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN); 988*206b73d0SCy Schubert else 989*206b73d0SCy Schubert os_memset(data->reauth_mac, 0x12, EAP_SIM_MAC_LEN); 990*206b73d0SCy Schubert #else /* TEST_FUZZ */ 991*206b73d0SCy Schubert os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN); 992*206b73d0SCy Schubert #endif /* TEST_FUZZ */ 993*206b73d0SCy Schubert wpa_hexdump(MSG_DEBUG, "EAP-SIM: Server MAC", 994*206b73d0SCy Schubert data->reauth_mac, EAP_SIM_MAC_LEN); 995*206b73d0SCy Schubert 99639beb93cSSam Leffler if (attr->encr_data == NULL || attr->iv == NULL) { 99739beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Reauthentication " 99839beb93cSSam Leffler "message did not include encrypted data"); 99939beb93cSSam Leffler return eap_sim_client_error(data, id, 100039beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 100139beb93cSSam Leffler } 100239beb93cSSam Leffler 100339beb93cSSam Leffler decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data, 100439beb93cSSam Leffler attr->encr_data_len, attr->iv, &eattr, 100539beb93cSSam Leffler 0); 100639beb93cSSam Leffler if (decrypted == NULL) { 100739beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Failed to parse encrypted " 100839beb93cSSam Leffler "data from reauthentication message"); 100939beb93cSSam Leffler return eap_sim_client_error(data, id, 101039beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 101139beb93cSSam Leffler } 101239beb93cSSam Leffler 101339beb93cSSam Leffler if (eattr.nonce_s == NULL || eattr.counter < 0) { 101439beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: (encr) No%s%s in reauth packet", 101539beb93cSSam Leffler !eattr.nonce_s ? " AT_NONCE_S" : "", 101639beb93cSSam Leffler eattr.counter < 0 ? " AT_COUNTER" : ""); 101739beb93cSSam Leffler os_free(decrypted); 101839beb93cSSam Leffler return eap_sim_client_error(data, id, 101939beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 102039beb93cSSam Leffler } 102139beb93cSSam Leffler 102239beb93cSSam Leffler if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) { 10235b9c547cSRui Paulo struct wpabuf *res; 102439beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: (encr) Invalid counter " 102539beb93cSSam Leffler "(%d <= %d)", eattr.counter, data->counter); 102639beb93cSSam Leffler data->counter_too_small = eattr.counter; 10275b9c547cSRui Paulo 102839beb93cSSam Leffler /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current 102939beb93cSSam Leffler * reauth_id must not be used to start a new reauthentication. 103039beb93cSSam Leffler * However, since it was used in the last EAP-Response-Identity 103139beb93cSSam Leffler * packet, it has to saved for the following fullauth to be 103239beb93cSSam Leffler * used in MK derivation. */ 103339beb93cSSam Leffler os_free(data->last_eap_identity); 103439beb93cSSam Leffler data->last_eap_identity = data->reauth_id; 103539beb93cSSam Leffler data->last_eap_identity_len = data->reauth_id_len; 103639beb93cSSam Leffler data->reauth_id = NULL; 103739beb93cSSam Leffler data->reauth_id_len = 0; 10385b9c547cSRui Paulo 10395b9c547cSRui Paulo res = eap_sim_response_reauth(data, id, 1, eattr.nonce_s); 104039beb93cSSam Leffler os_free(decrypted); 10415b9c547cSRui Paulo 10425b9c547cSRui Paulo return res; 104339beb93cSSam Leffler } 104439beb93cSSam Leffler data->counter = eattr.counter; 104539beb93cSSam Leffler 104639beb93cSSam Leffler os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN); 104739beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "EAP-SIM: (encr) AT_NONCE_S", 104839beb93cSSam Leffler data->nonce_s, EAP_SIM_NONCE_S_LEN); 104939beb93cSSam Leffler 105039beb93cSSam Leffler eap_sim_derive_keys_reauth(data->counter, 105139beb93cSSam Leffler data->reauth_id, data->reauth_id_len, 105239beb93cSSam Leffler data->nonce_s, data->mk, data->msk, 105339beb93cSSam Leffler data->emsk); 1054f05cddf9SRui Paulo eap_sim_clear_identities(sm, data, CLEAR_REAUTH_ID | CLEAR_EAP_ID); 1055f05cddf9SRui Paulo eap_sim_learn_ids(sm, data, &eattr); 105639beb93cSSam Leffler 105739beb93cSSam Leffler if (data->result_ind && attr->result_ind) 105839beb93cSSam Leffler data->use_result_ind = 1; 105939beb93cSSam Leffler 10605b9c547cSRui Paulo if (data->state != FAILURE) { 106139beb93cSSam Leffler eap_sim_state(data, data->use_result_ind ? 106239beb93cSSam Leffler RESULT_SUCCESS : SUCCESS); 106339beb93cSSam Leffler } 106439beb93cSSam Leffler 106539beb93cSSam Leffler data->num_id_req = 0; 106639beb93cSSam Leffler data->num_notification = 0; 106739beb93cSSam Leffler if (data->counter > EAP_SIM_MAX_FAST_REAUTHS) { 106839beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: Maximum number of " 106939beb93cSSam Leffler "fast reauths performed - force fullauth"); 1070f05cddf9SRui Paulo eap_sim_clear_identities(sm, data, 1071f05cddf9SRui Paulo CLEAR_REAUTH_ID | CLEAR_EAP_ID); 107239beb93cSSam Leffler } 107339beb93cSSam Leffler os_free(decrypted); 1074f05cddf9SRui Paulo return eap_sim_response_reauth(data, id, 0, data->nonce_s); 107539beb93cSSam Leffler } 107639beb93cSSam Leffler 107739beb93cSSam Leffler 107839beb93cSSam Leffler static struct wpabuf * eap_sim_process(struct eap_sm *sm, void *priv, 107939beb93cSSam Leffler struct eap_method_ret *ret, 108039beb93cSSam Leffler const struct wpabuf *reqData) 108139beb93cSSam Leffler { 108239beb93cSSam Leffler struct eap_sim_data *data = priv; 108339beb93cSSam Leffler const struct eap_hdr *req; 108439beb93cSSam Leffler u8 subtype, id; 108539beb93cSSam Leffler struct wpabuf *res; 108639beb93cSSam Leffler const u8 *pos; 108739beb93cSSam Leffler struct eap_sim_attrs attr; 108839beb93cSSam Leffler size_t len; 108939beb93cSSam Leffler 109039beb93cSSam Leffler wpa_hexdump_buf(MSG_DEBUG, "EAP-SIM: EAP data", reqData); 109139beb93cSSam Leffler if (eap_get_config_identity(sm, &len) == NULL) { 109239beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-SIM: Identity not configured"); 109339beb93cSSam Leffler eap_sm_request_identity(sm); 109439beb93cSSam Leffler ret->ignore = TRUE; 109539beb93cSSam Leffler return NULL; 109639beb93cSSam Leffler } 109739beb93cSSam Leffler 109839beb93cSSam Leffler pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SIM, reqData, &len); 1099325151a3SRui Paulo if (pos == NULL || len < 3) { 110039beb93cSSam Leffler ret->ignore = TRUE; 110139beb93cSSam Leffler return NULL; 110239beb93cSSam Leffler } 110339beb93cSSam Leffler req = wpabuf_head(reqData); 110439beb93cSSam Leffler id = req->identifier; 110539beb93cSSam Leffler len = be_to_host16(req->length); 110639beb93cSSam Leffler 110739beb93cSSam Leffler ret->ignore = FALSE; 110839beb93cSSam Leffler ret->methodState = METHOD_MAY_CONT; 110939beb93cSSam Leffler ret->decision = DECISION_FAIL; 111039beb93cSSam Leffler ret->allowNotifications = TRUE; 111139beb93cSSam Leffler 111239beb93cSSam Leffler subtype = *pos++; 111339beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: Subtype=%d", subtype); 111439beb93cSSam Leffler pos += 2; /* Reserved */ 111539beb93cSSam Leffler 111639beb93cSSam Leffler if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 0, 111739beb93cSSam Leffler 0)) { 111839beb93cSSam Leffler res = eap_sim_client_error(data, id, 111939beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 112039beb93cSSam Leffler goto done; 112139beb93cSSam Leffler } 112239beb93cSSam Leffler 112339beb93cSSam Leffler switch (subtype) { 112439beb93cSSam Leffler case EAP_SIM_SUBTYPE_START: 112539beb93cSSam Leffler res = eap_sim_process_start(sm, data, id, &attr); 112639beb93cSSam Leffler break; 112739beb93cSSam Leffler case EAP_SIM_SUBTYPE_CHALLENGE: 112839beb93cSSam Leffler res = eap_sim_process_challenge(sm, data, id, reqData, &attr); 112939beb93cSSam Leffler break; 113039beb93cSSam Leffler case EAP_SIM_SUBTYPE_NOTIFICATION: 113139beb93cSSam Leffler res = eap_sim_process_notification(sm, data, id, reqData, 113239beb93cSSam Leffler &attr); 113339beb93cSSam Leffler break; 113439beb93cSSam Leffler case EAP_SIM_SUBTYPE_REAUTHENTICATION: 113539beb93cSSam Leffler res = eap_sim_process_reauthentication(sm, data, id, reqData, 113639beb93cSSam Leffler &attr); 113739beb93cSSam Leffler break; 113839beb93cSSam Leffler case EAP_SIM_SUBTYPE_CLIENT_ERROR: 113939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: subtype Client-Error"); 114039beb93cSSam Leffler res = eap_sim_client_error(data, id, 114139beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 114239beb93cSSam Leffler break; 114339beb93cSSam Leffler default: 114439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-SIM: Unknown subtype=%d", subtype); 114539beb93cSSam Leffler res = eap_sim_client_error(data, id, 114639beb93cSSam Leffler EAP_SIM_UNABLE_TO_PROCESS_PACKET); 114739beb93cSSam Leffler break; 114839beb93cSSam Leffler } 114939beb93cSSam Leffler 115039beb93cSSam Leffler done: 115139beb93cSSam Leffler if (data->state == FAILURE) { 115239beb93cSSam Leffler ret->decision = DECISION_FAIL; 115339beb93cSSam Leffler ret->methodState = METHOD_DONE; 115439beb93cSSam Leffler } else if (data->state == SUCCESS) { 115539beb93cSSam Leffler ret->decision = data->use_result_ind ? 115639beb93cSSam Leffler DECISION_UNCOND_SUCC : DECISION_COND_SUCC; 115739beb93cSSam Leffler ret->methodState = data->use_result_ind ? 115839beb93cSSam Leffler METHOD_DONE : METHOD_MAY_CONT; 11595b9c547cSRui Paulo } else if (data->state == RESULT_SUCCESS) 116039beb93cSSam Leffler ret->methodState = METHOD_CONT; 116139beb93cSSam Leffler 116239beb93cSSam Leffler if (ret->methodState == METHOD_DONE) { 116339beb93cSSam Leffler ret->allowNotifications = FALSE; 116439beb93cSSam Leffler } 116539beb93cSSam Leffler 116639beb93cSSam Leffler return res; 116739beb93cSSam Leffler } 116839beb93cSSam Leffler 116939beb93cSSam Leffler 117039beb93cSSam Leffler static Boolean eap_sim_has_reauth_data(struct eap_sm *sm, void *priv) 117139beb93cSSam Leffler { 117239beb93cSSam Leffler struct eap_sim_data *data = priv; 117339beb93cSSam Leffler return data->pseudonym || data->reauth_id; 117439beb93cSSam Leffler } 117539beb93cSSam Leffler 117639beb93cSSam Leffler 117739beb93cSSam Leffler static void eap_sim_deinit_for_reauth(struct eap_sm *sm, void *priv) 117839beb93cSSam Leffler { 117939beb93cSSam Leffler struct eap_sim_data *data = priv; 1180f05cddf9SRui Paulo eap_sim_clear_identities(sm, data, CLEAR_EAP_ID); 118139beb93cSSam Leffler data->use_result_ind = 0; 11825b9c547cSRui Paulo eap_sim_clear_keys(data, 1); 118339beb93cSSam Leffler } 118439beb93cSSam Leffler 118539beb93cSSam Leffler 118639beb93cSSam Leffler static void * eap_sim_init_for_reauth(struct eap_sm *sm, void *priv) 118739beb93cSSam Leffler { 118839beb93cSSam Leffler struct eap_sim_data *data = priv; 1189f05cddf9SRui Paulo if (random_get_bytes(data->nonce_mt, EAP_SIM_NONCE_MT_LEN)) { 119039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-SIM: Failed to get random data " 119139beb93cSSam Leffler "for NONCE_MT"); 1192780fb4a2SCy Schubert eap_sim_deinit(sm, data); 119339beb93cSSam Leffler return NULL; 119439beb93cSSam Leffler } 119539beb93cSSam Leffler data->num_id_req = 0; 119639beb93cSSam Leffler data->num_notification = 0; 119739beb93cSSam Leffler eap_sim_state(data, CONTINUE); 119839beb93cSSam Leffler return priv; 119939beb93cSSam Leffler } 120039beb93cSSam Leffler 120139beb93cSSam Leffler 120239beb93cSSam Leffler static const u8 * eap_sim_get_identity(struct eap_sm *sm, void *priv, 120339beb93cSSam Leffler size_t *len) 120439beb93cSSam Leffler { 120539beb93cSSam Leffler struct eap_sim_data *data = priv; 120639beb93cSSam Leffler 120739beb93cSSam Leffler if (data->reauth_id) { 120839beb93cSSam Leffler *len = data->reauth_id_len; 120939beb93cSSam Leffler return data->reauth_id; 121039beb93cSSam Leffler } 121139beb93cSSam Leffler 121239beb93cSSam Leffler if (data->pseudonym) { 121339beb93cSSam Leffler *len = data->pseudonym_len; 121439beb93cSSam Leffler return data->pseudonym; 121539beb93cSSam Leffler } 121639beb93cSSam Leffler 121739beb93cSSam Leffler return NULL; 121839beb93cSSam Leffler } 121939beb93cSSam Leffler 122039beb93cSSam Leffler 122139beb93cSSam Leffler static Boolean eap_sim_isKeyAvailable(struct eap_sm *sm, void *priv) 122239beb93cSSam Leffler { 122339beb93cSSam Leffler struct eap_sim_data *data = priv; 122439beb93cSSam Leffler return data->state == SUCCESS; 122539beb93cSSam Leffler } 122639beb93cSSam Leffler 122739beb93cSSam Leffler 122839beb93cSSam Leffler static u8 * eap_sim_getKey(struct eap_sm *sm, void *priv, size_t *len) 122939beb93cSSam Leffler { 123039beb93cSSam Leffler struct eap_sim_data *data = priv; 123139beb93cSSam Leffler u8 *key; 123239beb93cSSam Leffler 123339beb93cSSam Leffler if (data->state != SUCCESS) 123439beb93cSSam Leffler return NULL; 123539beb93cSSam Leffler 123685732ac8SCy Schubert key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN); 123739beb93cSSam Leffler if (key == NULL) 123839beb93cSSam Leffler return NULL; 123939beb93cSSam Leffler 124039beb93cSSam Leffler *len = EAP_SIM_KEYING_DATA_LEN; 124139beb93cSSam Leffler 124239beb93cSSam Leffler return key; 124339beb93cSSam Leffler } 124439beb93cSSam Leffler 124539beb93cSSam Leffler 12465b9c547cSRui Paulo static u8 * eap_sim_get_session_id(struct eap_sm *sm, void *priv, size_t *len) 12475b9c547cSRui Paulo { 12485b9c547cSRui Paulo struct eap_sim_data *data = priv; 12495b9c547cSRui Paulo u8 *id; 12505b9c547cSRui Paulo 12515b9c547cSRui Paulo if (data->state != SUCCESS) 12525b9c547cSRui Paulo return NULL; 12535b9c547cSRui Paulo 1254*206b73d0SCy Schubert if (!data->reauth) 12555b9c547cSRui Paulo *len = 1 + data->num_chal * GSM_RAND_LEN + EAP_SIM_NONCE_MT_LEN; 1256*206b73d0SCy Schubert else 1257*206b73d0SCy Schubert *len = 1 + EAP_SIM_NONCE_S_LEN + EAP_SIM_MAC_LEN; 12585b9c547cSRui Paulo id = os_malloc(*len); 12595b9c547cSRui Paulo if (id == NULL) 12605b9c547cSRui Paulo return NULL; 12615b9c547cSRui Paulo 12625b9c547cSRui Paulo id[0] = EAP_TYPE_SIM; 1263*206b73d0SCy Schubert if (!data->reauth) { 12645b9c547cSRui Paulo os_memcpy(id + 1, data->rand, data->num_chal * GSM_RAND_LEN); 1265*206b73d0SCy Schubert os_memcpy(id + 1 + data->num_chal * GSM_RAND_LEN, 1266*206b73d0SCy Schubert data->nonce_mt, EAP_SIM_NONCE_MT_LEN); 1267*206b73d0SCy Schubert } else { 1268*206b73d0SCy Schubert os_memcpy(id + 1, data->nonce_s, EAP_SIM_NONCE_S_LEN); 1269*206b73d0SCy Schubert os_memcpy(id + 1 + EAP_SIM_NONCE_S_LEN, data->reauth_mac, 1270*206b73d0SCy Schubert EAP_SIM_MAC_LEN); 1271*206b73d0SCy Schubert } 12725b9c547cSRui Paulo wpa_hexdump(MSG_DEBUG, "EAP-SIM: Derived Session-Id", id, *len); 12735b9c547cSRui Paulo 12745b9c547cSRui Paulo return id; 12755b9c547cSRui Paulo } 12765b9c547cSRui Paulo 12775b9c547cSRui Paulo 127839beb93cSSam Leffler static u8 * eap_sim_get_emsk(struct eap_sm *sm, void *priv, size_t *len) 127939beb93cSSam Leffler { 128039beb93cSSam Leffler struct eap_sim_data *data = priv; 128139beb93cSSam Leffler u8 *key; 128239beb93cSSam Leffler 128339beb93cSSam Leffler if (data->state != SUCCESS) 128439beb93cSSam Leffler return NULL; 128539beb93cSSam Leffler 128685732ac8SCy Schubert key = os_memdup(data->emsk, EAP_EMSK_LEN); 128739beb93cSSam Leffler if (key == NULL) 128839beb93cSSam Leffler return NULL; 128939beb93cSSam Leffler 129039beb93cSSam Leffler *len = EAP_EMSK_LEN; 129139beb93cSSam Leffler 129239beb93cSSam Leffler return key; 129339beb93cSSam Leffler } 129439beb93cSSam Leffler 129539beb93cSSam Leffler 129685732ac8SCy Schubert static int eap_sim_get_error_code(void *priv) 129785732ac8SCy Schubert { 129885732ac8SCy Schubert struct eap_sim_data *data = priv; 129985732ac8SCy Schubert int current_data_error; 130085732ac8SCy Schubert 130185732ac8SCy Schubert if (!data) 130285732ac8SCy Schubert return NO_EAP_METHOD_ERROR; 130385732ac8SCy Schubert 130485732ac8SCy Schubert current_data_error = data->error_code; 130585732ac8SCy Schubert 130685732ac8SCy Schubert /* Now reset for next transaction */ 130785732ac8SCy Schubert data->error_code = NO_EAP_METHOD_ERROR; 130885732ac8SCy Schubert 130985732ac8SCy Schubert return current_data_error; 131085732ac8SCy Schubert } 131185732ac8SCy Schubert 131285732ac8SCy Schubert 131339beb93cSSam Leffler int eap_peer_sim_register(void) 131439beb93cSSam Leffler { 131539beb93cSSam Leffler struct eap_method *eap; 131639beb93cSSam Leffler 131739beb93cSSam Leffler eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION, 131839beb93cSSam Leffler EAP_VENDOR_IETF, EAP_TYPE_SIM, "SIM"); 131939beb93cSSam Leffler if (eap == NULL) 132039beb93cSSam Leffler return -1; 132139beb93cSSam Leffler 132239beb93cSSam Leffler eap->init = eap_sim_init; 132339beb93cSSam Leffler eap->deinit = eap_sim_deinit; 132439beb93cSSam Leffler eap->process = eap_sim_process; 132539beb93cSSam Leffler eap->isKeyAvailable = eap_sim_isKeyAvailable; 132639beb93cSSam Leffler eap->getKey = eap_sim_getKey; 13275b9c547cSRui Paulo eap->getSessionId = eap_sim_get_session_id; 132839beb93cSSam Leffler eap->has_reauth_data = eap_sim_has_reauth_data; 132939beb93cSSam Leffler eap->deinit_for_reauth = eap_sim_deinit_for_reauth; 133039beb93cSSam Leffler eap->init_for_reauth = eap_sim_init_for_reauth; 133139beb93cSSam Leffler eap->get_identity = eap_sim_get_identity; 133239beb93cSSam Leffler eap->get_emsk = eap_sim_get_emsk; 133385732ac8SCy Schubert eap->get_error_code = eap_sim_get_error_code; 133439beb93cSSam Leffler 1335780fb4a2SCy Schubert return eap_peer_method_register(eap); 133639beb93cSSam Leffler } 1337