139beb93cSSam Leffler /* 2f05cddf9SRui Paulo * EAP peer method: EAP-AKA (RFC 4187) and EAP-AKA' (RFC 5448) 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" 12*a90b9d01SCy Schubert #include "utils/base64.h" 1339beb93cSSam Leffler #include "pcsc_funcs.h" 14e28a4053SRui Paulo #include "crypto/crypto.h" 15e28a4053SRui Paulo #include "crypto/sha1.h" 16e28a4053SRui Paulo #include "crypto/sha256.h" 17e28a4053SRui Paulo #include "crypto/milenage.h" 1839beb93cSSam Leffler #include "eap_common/eap_sim_common.h" 19e28a4053SRui Paulo #include "eap_config.h" 20e28a4053SRui Paulo #include "eap_i.h" 2139beb93cSSam Leffler 2239beb93cSSam Leffler 2339beb93cSSam Leffler struct eap_aka_data { 2439beb93cSSam Leffler u8 ik[EAP_AKA_IK_LEN], ck[EAP_AKA_CK_LEN], res[EAP_AKA_RES_MAX_LEN]; 2539beb93cSSam Leffler size_t res_len; 2639beb93cSSam Leffler u8 nonce_s[EAP_SIM_NONCE_S_LEN]; 2739beb93cSSam Leffler u8 mk[EAP_SIM_MK_LEN]; 2839beb93cSSam Leffler u8 k_aut[EAP_AKA_PRIME_K_AUT_LEN]; 2939beb93cSSam Leffler u8 k_encr[EAP_SIM_K_ENCR_LEN]; 3039beb93cSSam Leffler u8 k_re[EAP_AKA_PRIME_K_RE_LEN]; /* EAP-AKA' only */ 3139beb93cSSam Leffler u8 msk[EAP_SIM_KEYING_DATA_LEN]; 3239beb93cSSam Leffler u8 emsk[EAP_EMSK_LEN]; 3339beb93cSSam Leffler u8 rand[EAP_AKA_RAND_LEN], autn[EAP_AKA_AUTN_LEN]; 3439beb93cSSam Leffler u8 auts[EAP_AKA_AUTS_LEN]; 35206b73d0SCy 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; 44*a90b9d01SCy Schubert u8 *mk_identity; 45*a90b9d01SCy Schubert size_t mk_identity_len; 4639beb93cSSam Leffler enum { 475b9c547cSRui Paulo CONTINUE, RESULT_SUCCESS, SUCCESS, FAILURE 4839beb93cSSam Leffler } state; 4939beb93cSSam Leffler 5039beb93cSSam Leffler struct wpabuf *id_msgs; 5139beb93cSSam Leffler int prev_id; 5239beb93cSSam Leffler int result_ind, use_result_ind; 5385732ac8SCy Schubert int use_pseudonym; 5439beb93cSSam Leffler u8 eap_method; 5539beb93cSSam Leffler u8 *network_name; 5639beb93cSSam Leffler size_t network_name_len; 5739beb93cSSam Leffler u16 kdf; 5839beb93cSSam Leffler int kdf_negotiation; 5985732ac8SCy Schubert u16 last_kdf_attrs[EAP_AKA_PRIME_KDF_MAX]; 6085732ac8SCy Schubert size_t last_kdf_count; 6185732ac8SCy Schubert int error_code; 62*a90b9d01SCy Schubert struct crypto_rsa_key *imsi_privacy_key; 6339beb93cSSam Leffler }; 6439beb93cSSam Leffler 6539beb93cSSam Leffler 6639beb93cSSam Leffler #ifndef CONFIG_NO_STDOUT_DEBUG 6739beb93cSSam Leffler static const char * eap_aka_state_txt(int state) 6839beb93cSSam Leffler { 6939beb93cSSam Leffler switch (state) { 7039beb93cSSam Leffler case CONTINUE: 7139beb93cSSam Leffler return "CONTINUE"; 7239beb93cSSam Leffler case RESULT_SUCCESS: 7339beb93cSSam Leffler return "RESULT_SUCCESS"; 7439beb93cSSam Leffler case SUCCESS: 7539beb93cSSam Leffler return "SUCCESS"; 7639beb93cSSam Leffler case FAILURE: 7739beb93cSSam Leffler return "FAILURE"; 7839beb93cSSam Leffler default: 7939beb93cSSam Leffler return "?"; 8039beb93cSSam Leffler } 8139beb93cSSam Leffler } 8239beb93cSSam Leffler #endif /* CONFIG_NO_STDOUT_DEBUG */ 8339beb93cSSam Leffler 8439beb93cSSam Leffler 8539beb93cSSam Leffler static void eap_aka_state(struct eap_aka_data *data, int state) 8639beb93cSSam Leffler { 8739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: %s -> %s", 8839beb93cSSam Leffler eap_aka_state_txt(data->state), 8939beb93cSSam Leffler eap_aka_state_txt(state)); 9039beb93cSSam Leffler data->state = state; 9139beb93cSSam Leffler } 9239beb93cSSam Leffler 9339beb93cSSam Leffler 9439beb93cSSam Leffler static void * eap_aka_init(struct eap_sm *sm) 9539beb93cSSam Leffler { 9639beb93cSSam Leffler struct eap_aka_data *data; 9739beb93cSSam Leffler const char *phase1 = eap_get_config_phase1(sm); 98f05cddf9SRui Paulo struct eap_peer_config *config = eap_get_config(sm); 9939beb93cSSam Leffler 10039beb93cSSam Leffler data = os_zalloc(sizeof(*data)); 10139beb93cSSam Leffler if (data == NULL) 10239beb93cSSam Leffler return NULL; 10339beb93cSSam Leffler 10439beb93cSSam Leffler data->eap_method = EAP_TYPE_AKA; 10539beb93cSSam Leffler 106*a90b9d01SCy Schubert if (config && config->imsi_privacy_cert) { 107*a90b9d01SCy Schubert #ifdef CRYPTO_RSA_OAEP_SHA256 108*a90b9d01SCy Schubert data->imsi_privacy_key = crypto_rsa_key_read( 109*a90b9d01SCy Schubert config->imsi_privacy_cert, false); 110*a90b9d01SCy Schubert if (!data->imsi_privacy_key) { 111*a90b9d01SCy Schubert wpa_printf(MSG_ERROR, 112*a90b9d01SCy Schubert "EAP-AKA: Failed to read/parse IMSI privacy certificate %s", 113*a90b9d01SCy Schubert config->imsi_privacy_cert); 114*a90b9d01SCy Schubert os_free(data); 115*a90b9d01SCy Schubert return NULL; 116*a90b9d01SCy Schubert } 117*a90b9d01SCy Schubert #else /* CRYPTO_RSA_OAEP_SHA256 */ 118*a90b9d01SCy Schubert wpa_printf(MSG_ERROR, 119*a90b9d01SCy Schubert "EAP-AKA: No support for imsi_privacy_cert in the build"); 120*a90b9d01SCy Schubert os_free(data); 121*a90b9d01SCy Schubert return NULL; 122*a90b9d01SCy Schubert #endif /* CRYPTO_RSA_OAEP_SHA256 */ 123*a90b9d01SCy Schubert } 124*a90b9d01SCy Schubert 12585732ac8SCy Schubert /* Zero is a valid error code, so we need to initialize */ 12685732ac8SCy Schubert data->error_code = NO_EAP_METHOD_ERROR; 12785732ac8SCy Schubert 12839beb93cSSam Leffler eap_aka_state(data, CONTINUE); 12939beb93cSSam Leffler data->prev_id = -1; 13039beb93cSSam Leffler 13139beb93cSSam Leffler data->result_ind = phase1 && os_strstr(phase1, "result_ind=1") != NULL; 13239beb93cSSam Leffler 13385732ac8SCy Schubert data->use_pseudonym = !sm->init_phase2; 13485732ac8SCy Schubert if (config && config->anonymous_identity && data->use_pseudonym) { 135f05cddf9SRui Paulo data->pseudonym = os_malloc(config->anonymous_identity_len); 136f05cddf9SRui Paulo if (data->pseudonym) { 137f05cddf9SRui Paulo os_memcpy(data->pseudonym, config->anonymous_identity, 138f05cddf9SRui Paulo config->anonymous_identity_len); 139f05cddf9SRui Paulo data->pseudonym_len = config->anonymous_identity_len; 140f05cddf9SRui Paulo } 141f05cddf9SRui Paulo } 142f05cddf9SRui Paulo 143*a90b9d01SCy Schubert if (sm->identity) { 144*a90b9d01SCy Schubert /* Use the EAP-Response/Identity in MK derivation if AT_IDENTITY 145*a90b9d01SCy Schubert * is not used. */ 146*a90b9d01SCy Schubert data->mk_identity = os_memdup(sm->identity, sm->identity_len); 147*a90b9d01SCy Schubert data->mk_identity_len = sm->identity_len; 148*a90b9d01SCy Schubert } 149*a90b9d01SCy Schubert 15039beb93cSSam Leffler return data; 15139beb93cSSam Leffler } 15239beb93cSSam Leffler 15339beb93cSSam Leffler 15439beb93cSSam Leffler #ifdef EAP_AKA_PRIME 15539beb93cSSam Leffler static void * eap_aka_prime_init(struct eap_sm *sm) 15639beb93cSSam Leffler { 15739beb93cSSam Leffler struct eap_aka_data *data = eap_aka_init(sm); 15839beb93cSSam Leffler if (data == NULL) 15939beb93cSSam Leffler return NULL; 16039beb93cSSam Leffler data->eap_method = EAP_TYPE_AKA_PRIME; 16139beb93cSSam Leffler return data; 16239beb93cSSam Leffler } 16339beb93cSSam Leffler #endif /* EAP_AKA_PRIME */ 16439beb93cSSam Leffler 16539beb93cSSam Leffler 1665b9c547cSRui Paulo static void eap_aka_clear_keys(struct eap_aka_data *data, int reauth) 1675b9c547cSRui Paulo { 1685b9c547cSRui Paulo if (!reauth) { 1695b9c547cSRui Paulo os_memset(data->mk, 0, EAP_SIM_MK_LEN); 1705b9c547cSRui Paulo os_memset(data->k_aut, 0, EAP_AKA_PRIME_K_AUT_LEN); 1715b9c547cSRui Paulo os_memset(data->k_encr, 0, EAP_SIM_K_ENCR_LEN); 1725b9c547cSRui Paulo os_memset(data->k_re, 0, EAP_AKA_PRIME_K_RE_LEN); 1735b9c547cSRui Paulo } 1745b9c547cSRui Paulo os_memset(data->msk, 0, EAP_SIM_KEYING_DATA_LEN); 1755b9c547cSRui Paulo os_memset(data->emsk, 0, EAP_EMSK_LEN); 1765b9c547cSRui Paulo os_memset(data->autn, 0, EAP_AKA_AUTN_LEN); 1775b9c547cSRui Paulo os_memset(data->auts, 0, EAP_AKA_AUTS_LEN); 1785b9c547cSRui Paulo } 1795b9c547cSRui Paulo 1805b9c547cSRui Paulo 18139beb93cSSam Leffler static void eap_aka_deinit(struct eap_sm *sm, void *priv) 18239beb93cSSam Leffler { 18339beb93cSSam Leffler struct eap_aka_data *data = priv; 18439beb93cSSam Leffler if (data) { 18539beb93cSSam Leffler os_free(data->pseudonym); 18639beb93cSSam Leffler os_free(data->reauth_id); 187*a90b9d01SCy Schubert os_free(data->mk_identity); 18839beb93cSSam Leffler wpabuf_free(data->id_msgs); 18939beb93cSSam Leffler os_free(data->network_name); 1905b9c547cSRui Paulo eap_aka_clear_keys(data, 0); 191*a90b9d01SCy Schubert #ifdef CRYPTO_RSA_OAEP_SHA256 192*a90b9d01SCy Schubert crypto_rsa_key_free(data->imsi_privacy_key); 193*a90b9d01SCy Schubert #endif /* CRYPTO_RSA_OAEP_SHA256 */ 19439beb93cSSam Leffler os_free(data); 19539beb93cSSam Leffler } 19639beb93cSSam Leffler } 19739beb93cSSam Leffler 19839beb93cSSam Leffler 1995b9c547cSRui Paulo static int eap_aka_ext_sim_req(struct eap_sm *sm, struct eap_aka_data *data) 2005b9c547cSRui Paulo { 2015b9c547cSRui Paulo char req[200], *pos, *end; 2025b9c547cSRui Paulo 2035b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-AKA: Use external USIM processing"); 2045b9c547cSRui Paulo pos = req; 2055b9c547cSRui Paulo end = pos + sizeof(req); 2065b9c547cSRui Paulo pos += os_snprintf(pos, end - pos, "UMTS-AUTH"); 2075b9c547cSRui Paulo pos += os_snprintf(pos, end - pos, ":"); 2085b9c547cSRui Paulo pos += wpa_snprintf_hex(pos, end - pos, data->rand, EAP_AKA_RAND_LEN); 2095b9c547cSRui Paulo pos += os_snprintf(pos, end - pos, ":"); 2105b9c547cSRui Paulo wpa_snprintf_hex(pos, end - pos, data->autn, EAP_AKA_AUTN_LEN); 2115b9c547cSRui Paulo 2125b9c547cSRui Paulo eap_sm_request_sim(sm, req); 2135b9c547cSRui Paulo return 1; 2145b9c547cSRui Paulo } 2155b9c547cSRui Paulo 2165b9c547cSRui Paulo 2175b9c547cSRui Paulo static int eap_aka_ext_sim_result(struct eap_sm *sm, struct eap_aka_data *data, 2185b9c547cSRui Paulo struct eap_peer_config *conf) 2195b9c547cSRui Paulo { 2205b9c547cSRui Paulo char *resp, *pos; 2215b9c547cSRui Paulo 2225b9c547cSRui Paulo wpa_printf(MSG_DEBUG, 2235b9c547cSRui Paulo "EAP-AKA: Use result from external USIM processing"); 2245b9c547cSRui Paulo 2255b9c547cSRui Paulo resp = conf->external_sim_resp; 2265b9c547cSRui Paulo conf->external_sim_resp = NULL; 2275b9c547cSRui Paulo 2285b9c547cSRui Paulo if (os_strncmp(resp, "UMTS-AUTS:", 10) == 0) { 2295b9c547cSRui Paulo pos = resp + 10; 2305b9c547cSRui Paulo if (hexstr2bin(pos, data->auts, EAP_AKA_AUTS_LEN) < 0) 2315b9c547cSRui Paulo goto invalid; 2325b9c547cSRui Paulo wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: AUTS", data->auts, 2335b9c547cSRui Paulo EAP_AKA_AUTS_LEN); 2345b9c547cSRui Paulo os_free(resp); 2355b9c547cSRui Paulo return -2; 2365b9c547cSRui Paulo } 2375b9c547cSRui Paulo 2385b9c547cSRui Paulo if (os_strncmp(resp, "UMTS-AUTH:", 10) != 0) { 2395b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-AKA: Unrecognized external USIM processing response"); 2405b9c547cSRui Paulo os_free(resp); 2415b9c547cSRui Paulo return -1; 2425b9c547cSRui Paulo } 2435b9c547cSRui Paulo 2445b9c547cSRui Paulo pos = resp + 10; 2455b9c547cSRui Paulo wpa_hexdump(MSG_DEBUG, "EAP-AKA: RAND", data->rand, EAP_AKA_RAND_LEN); 2465b9c547cSRui Paulo 2475b9c547cSRui Paulo if (hexstr2bin(pos, data->ik, EAP_AKA_IK_LEN) < 0) 2485b9c547cSRui Paulo goto invalid; 2495b9c547cSRui Paulo wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: IK", data->ik, EAP_AKA_IK_LEN); 2505b9c547cSRui Paulo pos += EAP_AKA_IK_LEN * 2; 2515b9c547cSRui Paulo if (*pos != ':') 2525b9c547cSRui Paulo goto invalid; 2535b9c547cSRui Paulo pos++; 2545b9c547cSRui Paulo 2555b9c547cSRui Paulo if (hexstr2bin(pos, data->ck, EAP_AKA_CK_LEN) < 0) 2565b9c547cSRui Paulo goto invalid; 2575b9c547cSRui Paulo wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: CK", data->ck, EAP_AKA_CK_LEN); 2585b9c547cSRui Paulo pos += EAP_AKA_CK_LEN * 2; 2595b9c547cSRui Paulo if (*pos != ':') 2605b9c547cSRui Paulo goto invalid; 2615b9c547cSRui Paulo pos++; 2625b9c547cSRui Paulo 2635b9c547cSRui Paulo data->res_len = os_strlen(pos) / 2; 2645b9c547cSRui Paulo if (data->res_len > EAP_AKA_RES_MAX_LEN) { 2655b9c547cSRui Paulo data->res_len = 0; 2665b9c547cSRui Paulo goto invalid; 2675b9c547cSRui Paulo } 2685b9c547cSRui Paulo if (hexstr2bin(pos, data->res, data->res_len) < 0) 2695b9c547cSRui Paulo goto invalid; 2705b9c547cSRui Paulo wpa_hexdump_key(MSG_DEBUG, "EAP-AKA: RES", data->res, data->res_len); 2715b9c547cSRui Paulo 2725b9c547cSRui Paulo os_free(resp); 2735b9c547cSRui Paulo return 0; 2745b9c547cSRui Paulo 2755b9c547cSRui Paulo invalid: 2765b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-AKA: Invalid external USIM processing UMTS-AUTH response"); 2775b9c547cSRui Paulo os_free(resp); 2785b9c547cSRui Paulo return -1; 2795b9c547cSRui Paulo } 2805b9c547cSRui Paulo 2815b9c547cSRui Paulo 28239beb93cSSam Leffler static int eap_aka_umts_auth(struct eap_sm *sm, struct eap_aka_data *data) 28339beb93cSSam Leffler { 28439beb93cSSam Leffler struct eap_peer_config *conf; 28539beb93cSSam Leffler 28639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: UMTS authentication algorithm"); 28739beb93cSSam Leffler 28839beb93cSSam Leffler conf = eap_get_config(sm); 28939beb93cSSam Leffler if (conf == NULL) 29039beb93cSSam Leffler return -1; 2915b9c547cSRui Paulo 2925b9c547cSRui Paulo if (sm->external_sim) { 2935b9c547cSRui Paulo if (conf->external_sim_resp) 2945b9c547cSRui Paulo return eap_aka_ext_sim_result(sm, data, conf); 2955b9c547cSRui Paulo else 2965b9c547cSRui Paulo return eap_aka_ext_sim_req(sm, data); 2975b9c547cSRui Paulo } 2985b9c547cSRui Paulo 29939beb93cSSam Leffler if (conf->pcsc) { 30039beb93cSSam Leffler return scard_umts_auth(sm->scard_ctx, data->rand, 30139beb93cSSam Leffler data->autn, data->res, &data->res_len, 30239beb93cSSam Leffler data->ik, data->ck, data->auts); 30339beb93cSSam Leffler } 30439beb93cSSam Leffler 30539beb93cSSam Leffler #ifdef CONFIG_USIM_SIMULATOR 30639beb93cSSam Leffler if (conf->password) { 30739beb93cSSam Leffler u8 opc[16], k[16], sqn[6]; 30839beb93cSSam Leffler const char *pos; 30939beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: Use internal Milenage " 31039beb93cSSam Leffler "implementation for UMTS authentication"); 31139beb93cSSam Leffler if (conf->password_len < 78) { 31239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: invalid Milenage " 31339beb93cSSam Leffler "password"); 31439beb93cSSam Leffler return -1; 31539beb93cSSam Leffler } 31639beb93cSSam Leffler pos = (const char *) conf->password; 31739beb93cSSam Leffler if (hexstr2bin(pos, k, 16)) 31839beb93cSSam Leffler return -1; 31939beb93cSSam Leffler pos += 32; 32039beb93cSSam Leffler if (*pos != ':') 32139beb93cSSam Leffler return -1; 32239beb93cSSam Leffler pos++; 32339beb93cSSam Leffler 32439beb93cSSam Leffler if (hexstr2bin(pos, opc, 16)) 32539beb93cSSam Leffler return -1; 32639beb93cSSam Leffler pos += 32; 32739beb93cSSam Leffler if (*pos != ':') 32839beb93cSSam Leffler return -1; 32939beb93cSSam Leffler pos++; 33039beb93cSSam Leffler 33139beb93cSSam Leffler if (hexstr2bin(pos, sqn, 6)) 33239beb93cSSam Leffler return -1; 33339beb93cSSam Leffler 33439beb93cSSam Leffler return milenage_check(opc, k, sqn, data->rand, data->autn, 33539beb93cSSam Leffler data->ik, data->ck, 33639beb93cSSam Leffler data->res, &data->res_len, data->auts); 33739beb93cSSam Leffler } 33839beb93cSSam Leffler #endif /* CONFIG_USIM_SIMULATOR */ 33939beb93cSSam Leffler 34039beb93cSSam Leffler #ifdef CONFIG_USIM_HARDCODED 34139beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: Use hardcoded Kc and SRES values for " 34239beb93cSSam Leffler "testing"); 34339beb93cSSam Leffler 34439beb93cSSam Leffler /* These hardcoded Kc and SRES values are used for testing. 34539beb93cSSam Leffler * Could consider making them configurable. */ 34639beb93cSSam Leffler os_memset(data->res, '2', EAP_AKA_RES_MAX_LEN); 34739beb93cSSam Leffler data->res_len = EAP_AKA_RES_MAX_LEN; 34839beb93cSSam Leffler os_memset(data->ik, '3', EAP_AKA_IK_LEN); 34939beb93cSSam Leffler os_memset(data->ck, '4', EAP_AKA_CK_LEN); 35039beb93cSSam Leffler { 35139beb93cSSam Leffler u8 autn[EAP_AKA_AUTN_LEN]; 35239beb93cSSam Leffler os_memset(autn, '1', EAP_AKA_AUTN_LEN); 3535b9c547cSRui Paulo if (os_memcmp_const(autn, data->autn, EAP_AKA_AUTN_LEN) != 0) { 35439beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: AUTN did not match " 35539beb93cSSam Leffler "with expected value"); 35639beb93cSSam Leffler return -1; 35739beb93cSSam Leffler } 35839beb93cSSam Leffler } 35939beb93cSSam Leffler #if 0 36039beb93cSSam Leffler { 36139beb93cSSam Leffler static int test_resync = 1; 36239beb93cSSam Leffler if (test_resync) { 36339beb93cSSam Leffler /* Test Resynchronization */ 36439beb93cSSam Leffler test_resync = 0; 36539beb93cSSam Leffler return -2; 36639beb93cSSam Leffler } 36739beb93cSSam Leffler } 36839beb93cSSam Leffler #endif 36939beb93cSSam Leffler return 0; 37039beb93cSSam Leffler 37139beb93cSSam Leffler #else /* CONFIG_USIM_HARDCODED */ 37239beb93cSSam Leffler 3735b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-AKA: No UMTS authentication algorithm " 37439beb93cSSam Leffler "enabled"); 37539beb93cSSam Leffler return -1; 37639beb93cSSam Leffler 37739beb93cSSam Leffler #endif /* CONFIG_USIM_HARDCODED */ 37839beb93cSSam Leffler } 37939beb93cSSam Leffler 38039beb93cSSam Leffler 38139beb93cSSam Leffler #define CLEAR_PSEUDONYM 0x01 38239beb93cSSam Leffler #define CLEAR_REAUTH_ID 0x02 38339beb93cSSam Leffler 384f05cddf9SRui Paulo static void eap_aka_clear_identities(struct eap_sm *sm, 385f05cddf9SRui Paulo struct eap_aka_data *data, int id) 38639beb93cSSam Leffler { 387f05cddf9SRui Paulo if ((id & CLEAR_PSEUDONYM) && data->pseudonym) { 388f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old pseudonym"); 38939beb93cSSam Leffler os_free(data->pseudonym); 39039beb93cSSam Leffler data->pseudonym = NULL; 39139beb93cSSam Leffler data->pseudonym_len = 0; 39285732ac8SCy Schubert if (data->use_pseudonym) 393f05cddf9SRui Paulo eap_set_anon_id(sm, NULL, 0); 39439beb93cSSam Leffler } 395f05cddf9SRui Paulo if ((id & CLEAR_REAUTH_ID) && data->reauth_id) { 396f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-AKA: forgetting old reauth_id"); 39739beb93cSSam Leffler os_free(data->reauth_id); 39839beb93cSSam Leffler data->reauth_id = NULL; 39939beb93cSSam Leffler data->reauth_id_len = 0; 40039beb93cSSam Leffler } 40139beb93cSSam Leffler } 40239beb93cSSam Leffler 40339beb93cSSam Leffler 404f05cddf9SRui Paulo static int eap_aka_learn_ids(struct eap_sm *sm, struct eap_aka_data *data, 40539beb93cSSam Leffler struct eap_sim_attrs *attr) 40639beb93cSSam Leffler { 40739beb93cSSam Leffler if (attr->next_pseudonym) { 408f05cddf9SRui Paulo const u8 *identity = NULL; 409f05cddf9SRui Paulo size_t identity_len = 0; 410f05cddf9SRui Paulo const u8 *realm = NULL; 411f05cddf9SRui Paulo size_t realm_len = 0; 412f05cddf9SRui Paulo 413f05cddf9SRui Paulo wpa_hexdump_ascii(MSG_DEBUG, 414f05cddf9SRui Paulo "EAP-AKA: (encr) AT_NEXT_PSEUDONYM", 415f05cddf9SRui Paulo attr->next_pseudonym, 416f05cddf9SRui Paulo attr->next_pseudonym_len); 41739beb93cSSam Leffler os_free(data->pseudonym); 418f05cddf9SRui Paulo /* Look for the realm of the permanent identity */ 419f05cddf9SRui Paulo identity = eap_get_config_identity(sm, &identity_len); 420f05cddf9SRui Paulo if (identity) { 421f05cddf9SRui Paulo for (realm = identity, realm_len = identity_len; 422f05cddf9SRui Paulo realm_len > 0; realm_len--, realm++) { 423f05cddf9SRui Paulo if (*realm == '@') 424f05cddf9SRui Paulo break; 425f05cddf9SRui Paulo } 426f05cddf9SRui Paulo } 427f05cddf9SRui Paulo data->pseudonym = os_malloc(attr->next_pseudonym_len + 428f05cddf9SRui Paulo realm_len); 42939beb93cSSam Leffler if (data->pseudonym == NULL) { 43039beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for " 43139beb93cSSam Leffler "next pseudonym"); 432f05cddf9SRui Paulo data->pseudonym_len = 0; 43339beb93cSSam Leffler return -1; 43439beb93cSSam Leffler } 43539beb93cSSam Leffler os_memcpy(data->pseudonym, attr->next_pseudonym, 43639beb93cSSam Leffler attr->next_pseudonym_len); 437f05cddf9SRui Paulo if (realm_len) { 438f05cddf9SRui Paulo os_memcpy(data->pseudonym + attr->next_pseudonym_len, 439f05cddf9SRui Paulo realm, realm_len); 440f05cddf9SRui Paulo } 441f05cddf9SRui Paulo data->pseudonym_len = attr->next_pseudonym_len + realm_len; 44285732ac8SCy Schubert if (data->use_pseudonym) 44385732ac8SCy Schubert eap_set_anon_id(sm, data->pseudonym, 44485732ac8SCy Schubert data->pseudonym_len); 44539beb93cSSam Leffler } 44639beb93cSSam Leffler 44739beb93cSSam Leffler if (attr->next_reauth_id) { 44839beb93cSSam Leffler os_free(data->reauth_id); 44985732ac8SCy Schubert data->reauth_id = os_memdup(attr->next_reauth_id, 45085732ac8SCy Schubert attr->next_reauth_id_len); 45139beb93cSSam Leffler if (data->reauth_id == NULL) { 45239beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: (encr) No memory for " 45339beb93cSSam Leffler "next reauth_id"); 454f05cddf9SRui Paulo data->reauth_id_len = 0; 45539beb93cSSam Leffler return -1; 45639beb93cSSam Leffler } 45739beb93cSSam Leffler data->reauth_id_len = attr->next_reauth_id_len; 45839beb93cSSam Leffler wpa_hexdump_ascii(MSG_DEBUG, 45939beb93cSSam Leffler "EAP-AKA: (encr) AT_NEXT_REAUTH_ID", 46039beb93cSSam Leffler data->reauth_id, 46139beb93cSSam Leffler data->reauth_id_len); 46239beb93cSSam Leffler } 46339beb93cSSam Leffler 46439beb93cSSam Leffler return 0; 46539beb93cSSam Leffler } 46639beb93cSSam Leffler 46739beb93cSSam Leffler 46839beb93cSSam Leffler static int eap_aka_add_id_msg(struct eap_aka_data *data, 469c1d255d3SCy Schubert const struct wpabuf *msg1, 470c1d255d3SCy Schubert const struct wpabuf *msg2) 47139beb93cSSam Leffler { 472c1d255d3SCy Schubert size_t len; 47339beb93cSSam Leffler 474c1d255d3SCy Schubert if (!msg1) 475c1d255d3SCy Schubert return -1; 476c1d255d3SCy Schubert len = wpabuf_len(msg1); 477c1d255d3SCy Schubert if (msg2) 478c1d255d3SCy Schubert len += wpabuf_len(msg2); 479c1d255d3SCy Schubert 480c1d255d3SCy Schubert if (!data->id_msgs) { 481c1d255d3SCy Schubert data->id_msgs = wpabuf_alloc(len); 482c1d255d3SCy Schubert if (!data->id_msgs) 483c1d255d3SCy Schubert return -1; 484c1d255d3SCy Schubert } else if (wpabuf_resize(&data->id_msgs, len) < 0) { 485c1d255d3SCy Schubert return -1; 48639beb93cSSam Leffler } 48739beb93cSSam Leffler 488c1d255d3SCy Schubert wpabuf_put_buf(data->id_msgs, msg1); 489c1d255d3SCy Schubert if (msg2) 490c1d255d3SCy Schubert wpabuf_put_buf(data->id_msgs, msg2); 49139beb93cSSam Leffler 49239beb93cSSam Leffler return 0; 49339beb93cSSam Leffler } 49439beb93cSSam Leffler 49539beb93cSSam Leffler 49639beb93cSSam Leffler static void eap_aka_add_checkcode(struct eap_aka_data *data, 49739beb93cSSam Leffler struct eap_sim_msg *msg) 49839beb93cSSam Leffler { 49939beb93cSSam Leffler const u8 *addr; 50039beb93cSSam Leffler size_t len; 50139beb93cSSam Leffler u8 hash[SHA256_MAC_LEN]; 50239beb93cSSam Leffler 50339beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_CHECKCODE"); 50439beb93cSSam Leffler 50539beb93cSSam Leffler if (data->id_msgs == NULL) { 50639beb93cSSam Leffler /* 50739beb93cSSam Leffler * No EAP-AKA/Identity packets were exchanged - send empty 50839beb93cSSam Leffler * checkcode. 50939beb93cSSam Leffler */ 51039beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, NULL, 0); 51139beb93cSSam Leffler return; 51239beb93cSSam Leffler } 51339beb93cSSam Leffler 51439beb93cSSam Leffler /* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */ 51539beb93cSSam Leffler addr = wpabuf_head(data->id_msgs); 51639beb93cSSam Leffler len = wpabuf_len(data->id_msgs); 51739beb93cSSam Leffler wpa_hexdump(MSG_MSGDUMP, "EAP-AKA: AT_CHECKCODE data", addr, len); 51839beb93cSSam Leffler #ifdef EAP_AKA_PRIME 51939beb93cSSam Leffler if (data->eap_method == EAP_TYPE_AKA_PRIME) 52039beb93cSSam Leffler sha256_vector(1, &addr, &len, hash); 52139beb93cSSam Leffler else 52239beb93cSSam Leffler #endif /* EAP_AKA_PRIME */ 52339beb93cSSam Leffler sha1_vector(1, &addr, &len, hash); 52439beb93cSSam Leffler 52539beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_CHECKCODE, 0, hash, 52639beb93cSSam Leffler data->eap_method == EAP_TYPE_AKA_PRIME ? 52739beb93cSSam Leffler EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN); 52839beb93cSSam Leffler } 52939beb93cSSam Leffler 53039beb93cSSam Leffler 53139beb93cSSam Leffler static int eap_aka_verify_checkcode(struct eap_aka_data *data, 53239beb93cSSam Leffler const u8 *checkcode, size_t checkcode_len) 53339beb93cSSam Leffler { 53439beb93cSSam Leffler const u8 *addr; 53539beb93cSSam Leffler size_t len; 53639beb93cSSam Leffler u8 hash[SHA256_MAC_LEN]; 53739beb93cSSam Leffler size_t hash_len; 53839beb93cSSam Leffler 53939beb93cSSam Leffler if (checkcode == NULL) 54039beb93cSSam Leffler return -1; 54139beb93cSSam Leffler 54239beb93cSSam Leffler if (data->id_msgs == NULL) { 54339beb93cSSam Leffler if (checkcode_len != 0) { 54439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server " 54539beb93cSSam Leffler "indicates that AKA/Identity messages were " 54639beb93cSSam Leffler "used, but they were not"); 54739beb93cSSam Leffler return -1; 54839beb93cSSam Leffler } 54939beb93cSSam Leffler return 0; 55039beb93cSSam Leffler } 55139beb93cSSam Leffler 55239beb93cSSam Leffler hash_len = data->eap_method == EAP_TYPE_AKA_PRIME ? 55339beb93cSSam Leffler EAP_AKA_PRIME_CHECKCODE_LEN : EAP_AKA_CHECKCODE_LEN; 55439beb93cSSam Leffler 55539beb93cSSam Leffler if (checkcode_len != hash_len) { 55639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: Checkcode from server " 55739beb93cSSam Leffler "indicates that AKA/Identity message were not " 55839beb93cSSam Leffler "used, but they were"); 55939beb93cSSam Leffler return -1; 56039beb93cSSam Leffler } 56139beb93cSSam Leffler 56239beb93cSSam Leffler /* Checkcode is SHA1/SHA256 hash over all EAP-AKA/Identity packets. */ 56339beb93cSSam Leffler addr = wpabuf_head(data->id_msgs); 56439beb93cSSam Leffler len = wpabuf_len(data->id_msgs); 56539beb93cSSam Leffler #ifdef EAP_AKA_PRIME 56639beb93cSSam Leffler if (data->eap_method == EAP_TYPE_AKA_PRIME) 56739beb93cSSam Leffler sha256_vector(1, &addr, &len, hash); 56839beb93cSSam Leffler else 56939beb93cSSam Leffler #endif /* EAP_AKA_PRIME */ 57039beb93cSSam Leffler sha1_vector(1, &addr, &len, hash); 57139beb93cSSam Leffler 5725b9c547cSRui Paulo if (os_memcmp_const(hash, checkcode, hash_len) != 0) { 57339beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: Mismatch in AT_CHECKCODE"); 57439beb93cSSam Leffler return -1; 57539beb93cSSam Leffler } 57639beb93cSSam Leffler 57739beb93cSSam Leffler return 0; 57839beb93cSSam Leffler } 57939beb93cSSam Leffler 58039beb93cSSam Leffler 58139beb93cSSam Leffler static struct wpabuf * eap_aka_client_error(struct eap_aka_data *data, u8 id, 58239beb93cSSam Leffler int err) 58339beb93cSSam Leffler { 58439beb93cSSam Leffler struct eap_sim_msg *msg; 58539beb93cSSam Leffler 58639beb93cSSam Leffler eap_aka_state(data, FAILURE); 58739beb93cSSam Leffler data->num_id_req = 0; 58839beb93cSSam Leffler data->num_notification = 0; 58939beb93cSSam Leffler 590f05cddf9SRui Paulo wpa_printf(MSG_DEBUG, "EAP-AKA: Send Client-Error (error code %d)", 591f05cddf9SRui Paulo err); 59239beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method, 59339beb93cSSam Leffler EAP_AKA_SUBTYPE_CLIENT_ERROR); 59439beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_CLIENT_ERROR_CODE, err, NULL, 0); 5955b9c547cSRui Paulo return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0); 59639beb93cSSam Leffler } 59739beb93cSSam Leffler 59839beb93cSSam Leffler 59939beb93cSSam Leffler static struct wpabuf * eap_aka_authentication_reject(struct eap_aka_data *data, 60039beb93cSSam Leffler u8 id) 60139beb93cSSam Leffler { 60239beb93cSSam Leffler struct eap_sim_msg *msg; 60339beb93cSSam Leffler 60439beb93cSSam Leffler eap_aka_state(data, FAILURE); 60539beb93cSSam Leffler data->num_id_req = 0; 60639beb93cSSam Leffler data->num_notification = 0; 60739beb93cSSam Leffler 60839beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-AKA Authentication-Reject " 60939beb93cSSam Leffler "(id=%d)", id); 61039beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method, 61139beb93cSSam Leffler EAP_AKA_SUBTYPE_AUTHENTICATION_REJECT); 6125b9c547cSRui Paulo return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0); 61339beb93cSSam Leffler } 61439beb93cSSam Leffler 61539beb93cSSam Leffler 61639beb93cSSam Leffler static struct wpabuf * eap_aka_synchronization_failure( 61785732ac8SCy Schubert struct eap_aka_data *data, u8 id, struct eap_sim_attrs *attr) 61839beb93cSSam Leffler { 61939beb93cSSam Leffler struct eap_sim_msg *msg; 62039beb93cSSam Leffler 62139beb93cSSam Leffler data->num_id_req = 0; 62239beb93cSSam Leffler data->num_notification = 0; 62339beb93cSSam Leffler 62439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-AKA Synchronization-Failure " 62539beb93cSSam Leffler "(id=%d)", id); 62639beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method, 62739beb93cSSam Leffler EAP_AKA_SUBTYPE_SYNCHRONIZATION_FAILURE); 62839beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_AUTS"); 62939beb93cSSam Leffler eap_sim_msg_add_full(msg, EAP_SIM_AT_AUTS, data->auts, 63039beb93cSSam Leffler EAP_AKA_AUTS_LEN); 63185732ac8SCy Schubert if (data->eap_method == EAP_TYPE_AKA_PRIME) { 63285732ac8SCy Schubert size_t i; 63385732ac8SCy Schubert 63485732ac8SCy Schubert for (i = 0; i < attr->kdf_count; i++) { 63585732ac8SCy Schubert wpa_printf(MSG_DEBUG, " AT_KDF"); 63685732ac8SCy Schubert eap_sim_msg_add(msg, EAP_SIM_AT_KDF, attr->kdf[i], 63785732ac8SCy Schubert NULL, 0); 63885732ac8SCy Schubert } 63985732ac8SCy Schubert } 6405b9c547cSRui Paulo return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0); 64139beb93cSSam Leffler } 64239beb93cSSam Leffler 64339beb93cSSam Leffler 644*a90b9d01SCy Schubert #ifdef CRYPTO_RSA_OAEP_SHA256 645*a90b9d01SCy Schubert static struct wpabuf * 646*a90b9d01SCy Schubert eap_aka_encrypt_identity(struct crypto_rsa_key *imsi_privacy_key, 647*a90b9d01SCy Schubert const u8 *identity, size_t identity_len, 648*a90b9d01SCy Schubert const char *attr) 649*a90b9d01SCy Schubert { 650*a90b9d01SCy Schubert struct wpabuf *imsi_buf, *enc; 651*a90b9d01SCy Schubert char *b64; 652*a90b9d01SCy Schubert size_t b64_len, len; 653*a90b9d01SCy Schubert 654*a90b9d01SCy Schubert wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Encrypt permanent identity", 655*a90b9d01SCy Schubert identity, identity_len); 656*a90b9d01SCy Schubert 657*a90b9d01SCy Schubert imsi_buf = wpabuf_alloc_copy(identity, identity_len); 658*a90b9d01SCy Schubert if (!imsi_buf) 659*a90b9d01SCy Schubert return NULL; 660*a90b9d01SCy Schubert enc = crypto_rsa_oaep_sha256_encrypt(imsi_privacy_key, imsi_buf); 661*a90b9d01SCy Schubert wpabuf_free(imsi_buf); 662*a90b9d01SCy Schubert if (!enc) 663*a90b9d01SCy Schubert return NULL; 664*a90b9d01SCy Schubert 665*a90b9d01SCy Schubert b64 = base64_encode_no_lf(wpabuf_head(enc), wpabuf_len(enc), &b64_len); 666*a90b9d01SCy Schubert wpabuf_free(enc); 667*a90b9d01SCy Schubert if (!b64) 668*a90b9d01SCy Schubert return NULL; 669*a90b9d01SCy Schubert 670*a90b9d01SCy Schubert len = 1 + b64_len; 671*a90b9d01SCy Schubert if (attr) 672*a90b9d01SCy Schubert len += 1 + os_strlen(attr); 673*a90b9d01SCy Schubert enc = wpabuf_alloc(len); 674*a90b9d01SCy Schubert if (!enc) { 675*a90b9d01SCy Schubert os_free(b64); 676*a90b9d01SCy Schubert return NULL; 677*a90b9d01SCy Schubert } 678*a90b9d01SCy Schubert wpabuf_put_u8(enc, '\0'); 679*a90b9d01SCy Schubert wpabuf_put_data(enc, b64, b64_len); 680*a90b9d01SCy Schubert os_free(b64); 681*a90b9d01SCy Schubert if (attr) { 682*a90b9d01SCy Schubert wpabuf_put_u8(enc, ','); 683*a90b9d01SCy Schubert wpabuf_put_str(enc, attr); 684*a90b9d01SCy Schubert } 685*a90b9d01SCy Schubert wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Encrypted permanent identity", 686*a90b9d01SCy Schubert wpabuf_head(enc), wpabuf_len(enc)); 687*a90b9d01SCy Schubert 688*a90b9d01SCy Schubert return enc; 689*a90b9d01SCy Schubert } 690*a90b9d01SCy Schubert #endif /* CRYPTO_RSA_OAEP_SHA256 */ 691*a90b9d01SCy Schubert 692*a90b9d01SCy Schubert 69339beb93cSSam Leffler static struct wpabuf * eap_aka_response_identity(struct eap_sm *sm, 69439beb93cSSam Leffler struct eap_aka_data *data, 69539beb93cSSam Leffler u8 id, 69639beb93cSSam Leffler enum eap_sim_id_req id_req) 69739beb93cSSam Leffler { 69839beb93cSSam Leffler const u8 *identity = NULL; 69939beb93cSSam Leffler size_t identity_len = 0; 70039beb93cSSam Leffler struct eap_sim_msg *msg; 701*a90b9d01SCy Schubert struct wpabuf *enc_identity = NULL; 702*a90b9d01SCy Schubert struct eap_peer_config *config = NULL; 703*a90b9d01SCy Schubert bool use_imsi_identity = false; 70439beb93cSSam Leffler 70539beb93cSSam Leffler data->reauth = 0; 70639beb93cSSam Leffler if (id_req == ANY_ID && data->reauth_id) { 70739beb93cSSam Leffler identity = data->reauth_id; 70839beb93cSSam Leffler identity_len = data->reauth_id_len; 70939beb93cSSam Leffler data->reauth = 1; 71039beb93cSSam Leffler } else if ((id_req == ANY_ID || id_req == FULLAUTH_ID) && 711206b73d0SCy Schubert data->pseudonym && 712206b73d0SCy Schubert !eap_sim_anonymous_username(data->pseudonym, 713206b73d0SCy Schubert data->pseudonym_len)) { 71439beb93cSSam Leffler identity = data->pseudonym; 71539beb93cSSam Leffler identity_len = data->pseudonym_len; 716f05cddf9SRui Paulo eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID); 71739beb93cSSam Leffler } else if (id_req != NO_ID_REQ) { 71839beb93cSSam Leffler identity = eap_get_config_identity(sm, &identity_len); 71939beb93cSSam Leffler if (identity) { 720206b73d0SCy Schubert int ids = CLEAR_PSEUDONYM | CLEAR_REAUTH_ID; 721206b73d0SCy Schubert 722206b73d0SCy Schubert if (data->pseudonym && 723206b73d0SCy Schubert eap_sim_anonymous_username(data->pseudonym, 724206b73d0SCy Schubert data->pseudonym_len)) 725206b73d0SCy Schubert ids &= ~CLEAR_PSEUDONYM; 726206b73d0SCy Schubert eap_aka_clear_identities(sm, data, ids); 727*a90b9d01SCy Schubert 728*a90b9d01SCy Schubert config = eap_get_config(sm); 729*a90b9d01SCy Schubert if (config && config->imsi_identity) 730*a90b9d01SCy Schubert use_imsi_identity = true; 73139beb93cSSam Leffler } 732*a90b9d01SCy Schubert #ifdef CRYPTO_RSA_OAEP_SHA256 733*a90b9d01SCy Schubert if (identity && data->imsi_privacy_key) { 734*a90b9d01SCy Schubert const char *attr = NULL; 735*a90b9d01SCy Schubert 736*a90b9d01SCy Schubert config = eap_get_config(sm); 737*a90b9d01SCy Schubert if (config) 738*a90b9d01SCy Schubert attr = config->imsi_privacy_attr; 739*a90b9d01SCy Schubert enc_identity = eap_aka_encrypt_identity( 740*a90b9d01SCy Schubert data->imsi_privacy_key, 741*a90b9d01SCy Schubert identity, identity_len, attr); 742*a90b9d01SCy Schubert if (!enc_identity) { 743*a90b9d01SCy Schubert wpa_printf(MSG_INFO, 744*a90b9d01SCy Schubert "EAP-AKA: Failed to encrypt permanent identity"); 745*a90b9d01SCy Schubert return eap_aka_client_error( 746*a90b9d01SCy Schubert data, id, 747*a90b9d01SCy Schubert EAP_AKA_UNABLE_TO_PROCESS_PACKET); 74839beb93cSSam Leffler } 749*a90b9d01SCy Schubert /* Use the real identity, not the encrypted one, in MK 750*a90b9d01SCy Schubert * derivation. */ 751*a90b9d01SCy Schubert os_free(data->mk_identity); 752*a90b9d01SCy Schubert data->mk_identity = os_memdup(identity, identity_len); 753*a90b9d01SCy Schubert data->mk_identity_len = identity_len; 754*a90b9d01SCy Schubert identity = wpabuf_head(enc_identity); 755*a90b9d01SCy Schubert identity_len = wpabuf_len(enc_identity); 756*a90b9d01SCy Schubert } 757*a90b9d01SCy Schubert #endif /* CRYPTO_RSA_OAEP_SHA256 */ 758*a90b9d01SCy Schubert } 75939beb93cSSam Leffler 76039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-AKA Identity (id=%d)", id); 76139beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method, 76239beb93cSSam Leffler EAP_AKA_SUBTYPE_IDENTITY); 76339beb93cSSam Leffler 76439beb93cSSam Leffler if (identity) { 76539beb93cSSam Leffler wpa_hexdump_ascii(MSG_DEBUG, " AT_IDENTITY", 76639beb93cSSam Leffler identity, identity_len); 76739beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_IDENTITY, identity_len, 76839beb93cSSam Leffler identity, identity_len); 769*a90b9d01SCy Schubert if (use_imsi_identity && config && config->imsi_identity) { 770*a90b9d01SCy Schubert /* Use the IMSI identity override, i.e., the not 771*a90b9d01SCy Schubert * encrypted one, in MK derivation, when using 772*a90b9d01SCy Schubert * externally encrypted identity in configuration. */ 773*a90b9d01SCy Schubert os_free(data->mk_identity); 774*a90b9d01SCy Schubert data->mk_identity = os_memdup( 775*a90b9d01SCy Schubert config->imsi_identity, 776*a90b9d01SCy Schubert config->imsi_identity_len); 777*a90b9d01SCy Schubert data->mk_identity_len = config->imsi_identity_len; 778*a90b9d01SCy Schubert } else if (!enc_identity) { 779*a90b9d01SCy Schubert /* Use the last AT_IDENTITY value as the identity in 780*a90b9d01SCy Schubert * MK derivation. */ 781*a90b9d01SCy Schubert os_free(data->mk_identity); 782*a90b9d01SCy Schubert data->mk_identity = os_memdup(identity, identity_len); 783*a90b9d01SCy Schubert data->mk_identity_len = identity_len; 78439beb93cSSam Leffler } 785*a90b9d01SCy Schubert } 786*a90b9d01SCy Schubert wpabuf_free(enc_identity); 78739beb93cSSam Leffler 7885b9c547cSRui Paulo return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0); 78939beb93cSSam Leffler } 79039beb93cSSam Leffler 79139beb93cSSam Leffler 79239beb93cSSam Leffler static struct wpabuf * eap_aka_response_challenge(struct eap_aka_data *data, 79339beb93cSSam Leffler u8 id) 79439beb93cSSam Leffler { 79539beb93cSSam Leffler struct eap_sim_msg *msg; 79639beb93cSSam Leffler 79739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d)", id); 79839beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method, 79939beb93cSSam Leffler EAP_AKA_SUBTYPE_CHALLENGE); 80039beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_RES"); 80139beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_RES, data->res_len * 8, 80239beb93cSSam Leffler data->res, data->res_len); 80339beb93cSSam Leffler eap_aka_add_checkcode(data, msg); 80439beb93cSSam Leffler if (data->use_result_ind) { 80539beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_RESULT_IND"); 80639beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0); 80739beb93cSSam Leffler } 80839beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_MAC"); 80939beb93cSSam Leffler eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC); 8105b9c547cSRui Paulo return eap_sim_msg_finish(msg, data->eap_method, data->k_aut, (u8 *) "", 8115b9c547cSRui Paulo 0); 81239beb93cSSam Leffler } 81339beb93cSSam Leffler 81439beb93cSSam Leffler 81539beb93cSSam Leffler static struct wpabuf * eap_aka_response_reauth(struct eap_aka_data *data, 81639beb93cSSam Leffler u8 id, int counter_too_small, 81739beb93cSSam Leffler const u8 *nonce_s) 81839beb93cSSam Leffler { 81939beb93cSSam Leffler struct eap_sim_msg *msg; 82039beb93cSSam Leffler unsigned int counter; 82139beb93cSSam Leffler 82239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-AKA Reauthentication (id=%d)", 82339beb93cSSam Leffler id); 82439beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method, 82539beb93cSSam Leffler EAP_AKA_SUBTYPE_REAUTHENTICATION); 82639beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_IV"); 82739beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_ENCR_DATA"); 82839beb93cSSam Leffler eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, EAP_SIM_AT_ENCR_DATA); 82939beb93cSSam Leffler 83039beb93cSSam Leffler if (counter_too_small) { 83139beb93cSSam Leffler wpa_printf(MSG_DEBUG, " *AT_COUNTER_TOO_SMALL"); 83239beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER_TOO_SMALL, 0, NULL, 0); 83339beb93cSSam Leffler counter = data->counter_too_small; 83439beb93cSSam Leffler } else 83539beb93cSSam Leffler counter = data->counter; 83639beb93cSSam Leffler 83739beb93cSSam Leffler wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", counter); 83839beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, counter, NULL, 0); 83939beb93cSSam Leffler 84039beb93cSSam Leffler if (eap_sim_msg_add_encr_end(msg, data->k_encr, EAP_SIM_AT_PADDING)) { 84139beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt " 84239beb93cSSam Leffler "AT_ENCR_DATA"); 84339beb93cSSam Leffler eap_sim_msg_free(msg); 84439beb93cSSam Leffler return NULL; 84539beb93cSSam Leffler } 84639beb93cSSam Leffler eap_aka_add_checkcode(data, msg); 84739beb93cSSam Leffler if (data->use_result_ind) { 84839beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_RESULT_IND"); 84939beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_RESULT_IND, 0, NULL, 0); 85039beb93cSSam Leffler } 85139beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_MAC"); 85239beb93cSSam Leffler eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC); 8535b9c547cSRui Paulo return eap_sim_msg_finish(msg, data->eap_method, data->k_aut, nonce_s, 85439beb93cSSam Leffler EAP_SIM_NONCE_S_LEN); 85539beb93cSSam Leffler } 85639beb93cSSam Leffler 85739beb93cSSam Leffler 85839beb93cSSam Leffler static struct wpabuf * eap_aka_response_notification(struct eap_aka_data *data, 85939beb93cSSam Leffler u8 id, u16 notification) 86039beb93cSSam Leffler { 86139beb93cSSam Leffler struct eap_sim_msg *msg; 86239beb93cSSam Leffler u8 *k_aut = (notification & 0x4000) == 0 ? data->k_aut : NULL; 86339beb93cSSam Leffler 86439beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-AKA Notification (id=%d)", id); 86539beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method, 86639beb93cSSam Leffler EAP_AKA_SUBTYPE_NOTIFICATION); 86739beb93cSSam Leffler if (k_aut && data->reauth) { 86839beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_IV"); 86939beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_ENCR_DATA"); 87039beb93cSSam Leffler eap_sim_msg_add_encr_start(msg, EAP_SIM_AT_IV, 87139beb93cSSam Leffler EAP_SIM_AT_ENCR_DATA); 87239beb93cSSam Leffler wpa_printf(MSG_DEBUG, " *AT_COUNTER %d", data->counter); 87339beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_COUNTER, data->counter, 87439beb93cSSam Leffler NULL, 0); 87539beb93cSSam Leffler if (eap_sim_msg_add_encr_end(msg, data->k_encr, 87639beb93cSSam Leffler EAP_SIM_AT_PADDING)) { 87739beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Failed to encrypt " 87839beb93cSSam Leffler "AT_ENCR_DATA"); 87939beb93cSSam Leffler eap_sim_msg_free(msg); 88039beb93cSSam Leffler return NULL; 88139beb93cSSam Leffler } 88239beb93cSSam Leffler } 88339beb93cSSam Leffler if (k_aut) { 88439beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_MAC"); 88539beb93cSSam Leffler eap_sim_msg_add_mac(msg, EAP_SIM_AT_MAC); 88639beb93cSSam Leffler } 8875b9c547cSRui Paulo return eap_sim_msg_finish(msg, data->eap_method, k_aut, (u8 *) "", 0); 88839beb93cSSam Leffler } 88939beb93cSSam Leffler 89039beb93cSSam Leffler 89139beb93cSSam Leffler static struct wpabuf * eap_aka_process_identity(struct eap_sm *sm, 89239beb93cSSam Leffler struct eap_aka_data *data, 89339beb93cSSam Leffler u8 id, 89439beb93cSSam Leffler const struct wpabuf *reqData, 89539beb93cSSam Leffler struct eap_sim_attrs *attr) 89639beb93cSSam Leffler { 89739beb93cSSam Leffler int id_error; 89839beb93cSSam Leffler struct wpabuf *buf; 89939beb93cSSam Leffler 90039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Identity"); 90139beb93cSSam Leffler 90239beb93cSSam Leffler id_error = 0; 90339beb93cSSam Leffler switch (attr->id_req) { 90439beb93cSSam Leffler case NO_ID_REQ: 90539beb93cSSam Leffler break; 90639beb93cSSam Leffler case ANY_ID: 90739beb93cSSam Leffler if (data->num_id_req > 0) 90839beb93cSSam Leffler id_error++; 90939beb93cSSam Leffler data->num_id_req++; 91039beb93cSSam Leffler break; 91139beb93cSSam Leffler case FULLAUTH_ID: 91239beb93cSSam Leffler if (data->num_id_req > 1) 91339beb93cSSam Leffler id_error++; 91439beb93cSSam Leffler data->num_id_req++; 91539beb93cSSam Leffler break; 91639beb93cSSam Leffler case PERMANENT_ID: 91739beb93cSSam Leffler if (data->num_id_req > 2) 91839beb93cSSam Leffler id_error++; 91939beb93cSSam Leffler data->num_id_req++; 92039beb93cSSam Leffler break; 92139beb93cSSam Leffler } 92239beb93cSSam Leffler if (id_error) { 92339beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: Too many ID requests " 92439beb93cSSam Leffler "used within one authentication"); 92539beb93cSSam Leffler return eap_aka_client_error(data, id, 92639beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 92739beb93cSSam Leffler } 92839beb93cSSam Leffler 92939beb93cSSam Leffler buf = eap_aka_response_identity(sm, data, id, attr->id_req); 93039beb93cSSam Leffler 93139beb93cSSam Leffler if (data->prev_id != id) { 932c1d255d3SCy Schubert if (eap_aka_add_id_msg(data, reqData, buf) < 0) { 933c1d255d3SCy Schubert wpa_printf(MSG_INFO, 934c1d255d3SCy Schubert "EAP-AKA: Failed to store ID messages"); 935c1d255d3SCy Schubert wpabuf_free(buf); 936c1d255d3SCy Schubert return eap_aka_client_error( 937c1d255d3SCy Schubert data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET); 938c1d255d3SCy Schubert } 93939beb93cSSam Leffler data->prev_id = id; 94039beb93cSSam Leffler } 94139beb93cSSam Leffler 94239beb93cSSam Leffler return buf; 94339beb93cSSam Leffler } 94439beb93cSSam Leffler 94539beb93cSSam Leffler 94639beb93cSSam Leffler static int eap_aka_verify_mac(struct eap_aka_data *data, 94739beb93cSSam Leffler const struct wpabuf *req, 94839beb93cSSam Leffler const u8 *mac, const u8 *extra, 94939beb93cSSam Leffler size_t extra_len) 95039beb93cSSam Leffler { 95139beb93cSSam Leffler if (data->eap_method == EAP_TYPE_AKA_PRIME) 95239beb93cSSam Leffler return eap_sim_verify_mac_sha256(data->k_aut, req, mac, extra, 95339beb93cSSam Leffler extra_len); 95439beb93cSSam Leffler return eap_sim_verify_mac(data->k_aut, req, mac, extra, extra_len); 95539beb93cSSam Leffler } 95639beb93cSSam Leffler 95739beb93cSSam Leffler 95839beb93cSSam Leffler #ifdef EAP_AKA_PRIME 95939beb93cSSam Leffler static struct wpabuf * eap_aka_prime_kdf_select(struct eap_aka_data *data, 96039beb93cSSam Leffler u8 id, u16 kdf) 96139beb93cSSam Leffler { 96239beb93cSSam Leffler struct eap_sim_msg *msg; 96339beb93cSSam Leffler 96439beb93cSSam Leffler data->kdf_negotiation = 1; 96539beb93cSSam Leffler data->kdf = kdf; 96639beb93cSSam Leffler wpa_printf(MSG_DEBUG, "Generating EAP-AKA Challenge (id=%d) (KDF " 96739beb93cSSam Leffler "select)", id); 96839beb93cSSam Leffler msg = eap_sim_msg_init(EAP_CODE_RESPONSE, id, data->eap_method, 96939beb93cSSam Leffler EAP_AKA_SUBTYPE_CHALLENGE); 97039beb93cSSam Leffler wpa_printf(MSG_DEBUG, " AT_KDF"); 97139beb93cSSam Leffler eap_sim_msg_add(msg, EAP_SIM_AT_KDF, kdf, NULL, 0); 9725b9c547cSRui Paulo return eap_sim_msg_finish(msg, data->eap_method, NULL, NULL, 0); 97339beb93cSSam Leffler } 97439beb93cSSam Leffler 97539beb93cSSam Leffler 97639beb93cSSam Leffler static struct wpabuf * eap_aka_prime_kdf_neg(struct eap_aka_data *data, 97739beb93cSSam Leffler u8 id, struct eap_sim_attrs *attr) 97839beb93cSSam Leffler { 97939beb93cSSam Leffler size_t i; 98039beb93cSSam Leffler 98139beb93cSSam Leffler for (i = 0; i < attr->kdf_count; i++) { 98285732ac8SCy Schubert if (attr->kdf[i] == EAP_AKA_PRIME_KDF) { 98385732ac8SCy Schubert os_memcpy(data->last_kdf_attrs, attr->kdf, 98485732ac8SCy Schubert sizeof(u16) * attr->kdf_count); 98585732ac8SCy Schubert data->last_kdf_count = attr->kdf_count; 98639beb93cSSam Leffler return eap_aka_prime_kdf_select(data, id, 98739beb93cSSam Leffler EAP_AKA_PRIME_KDF); 98839beb93cSSam Leffler } 98985732ac8SCy Schubert } 99039beb93cSSam Leffler 99139beb93cSSam Leffler /* No matching KDF found - fail authentication as if AUTN had been 99239beb93cSSam Leffler * incorrect */ 99339beb93cSSam Leffler return eap_aka_authentication_reject(data, id); 99439beb93cSSam Leffler } 99539beb93cSSam Leffler 99639beb93cSSam Leffler 99739beb93cSSam Leffler static int eap_aka_prime_kdf_valid(struct eap_aka_data *data, 99839beb93cSSam Leffler struct eap_sim_attrs *attr) 99939beb93cSSam Leffler { 100039beb93cSSam Leffler size_t i, j; 100139beb93cSSam Leffler 100239beb93cSSam Leffler if (attr->kdf_count == 0) 100339beb93cSSam Leffler return 0; 100439beb93cSSam Leffler 100539beb93cSSam Leffler /* The only allowed (and required) duplication of a KDF is the addition 100639beb93cSSam Leffler * of the selected KDF into the beginning of the list. */ 100739beb93cSSam Leffler 100839beb93cSSam Leffler if (data->kdf_negotiation) { 100985732ac8SCy Schubert /* When the peer receives the new EAP-Request/AKA'-Challenge 101085732ac8SCy Schubert * message, must check only requested change occurred in the 101185732ac8SCy Schubert * list of AT_KDF attributes. If there are any other changes, 101285732ac8SCy Schubert * the peer must behave like the case that AT_MAC had been 101385732ac8SCy Schubert * incorrect and authentication is failed. These are defined in 101485732ac8SCy Schubert * EAP-AKA' specification RFC 5448, Section 3.2. */ 101539beb93cSSam Leffler if (attr->kdf[0] != data->kdf) { 101639beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA': The server did not " 101739beb93cSSam Leffler "accept the selected KDF"); 101885732ac8SCy Schubert return -1; 101985732ac8SCy Schubert } 102085732ac8SCy Schubert 102185732ac8SCy Schubert if (attr->kdf_count > EAP_AKA_PRIME_KDF_MAX || 102285732ac8SCy Schubert attr->kdf_count != data->last_kdf_count + 1) { 102385732ac8SCy Schubert wpa_printf(MSG_WARNING, 102485732ac8SCy Schubert "EAP-AKA': The length of KDF attributes is wrong"); 102585732ac8SCy Schubert return -1; 102639beb93cSSam Leffler } 102739beb93cSSam Leffler 102839beb93cSSam Leffler for (i = 1; i < attr->kdf_count; i++) { 102985732ac8SCy Schubert if (attr->kdf[i] != data->last_kdf_attrs[i - 1]) { 103085732ac8SCy Schubert wpa_printf(MSG_WARNING, 103185732ac8SCy Schubert "EAP-AKA': The KDF attributes except selected KDF are not same as original one"); 103285732ac8SCy Schubert return -1; 103339beb93cSSam Leffler } 103439beb93cSSam Leffler } 103539beb93cSSam Leffler } 103639beb93cSSam Leffler 103739beb93cSSam Leffler for (i = data->kdf ? 1 : 0; i < attr->kdf_count; i++) { 103839beb93cSSam Leffler for (j = i + 1; j < attr->kdf_count; j++) { 103939beb93cSSam Leffler if (attr->kdf[i] == attr->kdf[j]) { 104039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA': The server " 104139beb93cSSam Leffler "included a duplicated KDF"); 104239beb93cSSam Leffler return 0; 104339beb93cSSam Leffler } 104439beb93cSSam Leffler } 104539beb93cSSam Leffler } 104639beb93cSSam Leffler 104739beb93cSSam Leffler return 1; 104839beb93cSSam Leffler } 104939beb93cSSam Leffler #endif /* EAP_AKA_PRIME */ 105039beb93cSSam Leffler 105139beb93cSSam Leffler 105239beb93cSSam Leffler static struct wpabuf * eap_aka_process_challenge(struct eap_sm *sm, 105339beb93cSSam Leffler struct eap_aka_data *data, 105439beb93cSSam Leffler u8 id, 105539beb93cSSam Leffler const struct wpabuf *reqData, 105639beb93cSSam Leffler struct eap_sim_attrs *attr) 105739beb93cSSam Leffler { 105839beb93cSSam Leffler const u8 *identity; 105939beb93cSSam Leffler size_t identity_len; 106039beb93cSSam Leffler int res; 106139beb93cSSam Leffler struct eap_sim_attrs eattr; 106239beb93cSSam Leffler 106339beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Challenge"); 106439beb93cSSam Leffler 106539beb93cSSam Leffler if (attr->checkcode && 106639beb93cSSam Leffler eap_aka_verify_checkcode(data, attr->checkcode, 106739beb93cSSam Leffler attr->checkcode_len)) { 106839beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the " 106939beb93cSSam Leffler "message"); 1070206b73d0SCy Schubert #ifdef TEST_FUZZ 1071206b73d0SCy Schubert wpa_printf(MSG_INFO, 1072206b73d0SCy Schubert "TEST: Ignore AT_CHECKCODE mismatch for fuzz testing"); 1073206b73d0SCy Schubert #else /* TEST_FUZZ */ 107439beb93cSSam Leffler return eap_aka_client_error(data, id, 107539beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 1076206b73d0SCy Schubert #endif /* TEST_FUZZ */ 107739beb93cSSam Leffler } 107839beb93cSSam Leffler 107939beb93cSSam Leffler #ifdef EAP_AKA_PRIME 108039beb93cSSam Leffler if (data->eap_method == EAP_TYPE_AKA_PRIME) { 108139beb93cSSam Leffler if (!attr->kdf_input || attr->kdf_input_len == 0) { 108239beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA': Challenge message " 108339beb93cSSam Leffler "did not include non-empty AT_KDF_INPUT"); 108439beb93cSSam Leffler /* Fail authentication as if AUTN had been incorrect */ 108539beb93cSSam Leffler return eap_aka_authentication_reject(data, id); 108639beb93cSSam Leffler } 108739beb93cSSam Leffler os_free(data->network_name); 108885732ac8SCy Schubert data->network_name = os_memdup(attr->kdf_input, 108985732ac8SCy Schubert attr->kdf_input_len); 109039beb93cSSam Leffler if (data->network_name == NULL) { 109139beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA': No memory for " 109239beb93cSSam Leffler "storing Network Name"); 109339beb93cSSam Leffler return eap_aka_authentication_reject(data, id); 109439beb93cSSam Leffler } 109539beb93cSSam Leffler data->network_name_len = attr->kdf_input_len; 109639beb93cSSam Leffler wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA': Network Name " 109739beb93cSSam Leffler "(AT_KDF_INPUT)", 109839beb93cSSam Leffler data->network_name, data->network_name_len); 109939beb93cSSam Leffler /* TODO: check Network Name per 3GPP.33.402 */ 110039beb93cSSam Leffler 110185732ac8SCy Schubert res = eap_aka_prime_kdf_valid(data, attr); 110285732ac8SCy Schubert if (res == 0) 110339beb93cSSam Leffler return eap_aka_authentication_reject(data, id); 110485732ac8SCy Schubert else if (res == -1) 110585732ac8SCy Schubert return eap_aka_client_error( 110685732ac8SCy Schubert data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET); 110739beb93cSSam Leffler 110839beb93cSSam Leffler if (attr->kdf[0] != EAP_AKA_PRIME_KDF) 110939beb93cSSam Leffler return eap_aka_prime_kdf_neg(data, id, attr); 111039beb93cSSam Leffler 111139beb93cSSam Leffler data->kdf = EAP_AKA_PRIME_KDF; 111239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA': KDF %d selected", data->kdf); 111339beb93cSSam Leffler } 111439beb93cSSam Leffler 111539beb93cSSam Leffler if (data->eap_method == EAP_TYPE_AKA && attr->bidding) { 111639beb93cSSam Leffler u16 flags = WPA_GET_BE16(attr->bidding); 111739beb93cSSam Leffler if ((flags & EAP_AKA_BIDDING_FLAG_D) && 111839beb93cSSam Leffler eap_allowed_method(sm, EAP_VENDOR_IETF, 111939beb93cSSam Leffler EAP_TYPE_AKA_PRIME)) { 112039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Bidding down from " 112139beb93cSSam Leffler "AKA' to AKA detected"); 112239beb93cSSam Leffler /* Fail authentication as if AUTN had been incorrect */ 112339beb93cSSam Leffler return eap_aka_authentication_reject(data, id); 112439beb93cSSam Leffler } 112539beb93cSSam Leffler } 112639beb93cSSam Leffler #endif /* EAP_AKA_PRIME */ 112739beb93cSSam Leffler 112839beb93cSSam Leffler data->reauth = 0; 112939beb93cSSam Leffler if (!attr->mac || !attr->rand || !attr->autn) { 113039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message " 113139beb93cSSam Leffler "did not include%s%s%s", 113239beb93cSSam Leffler !attr->mac ? " AT_MAC" : "", 113339beb93cSSam Leffler !attr->rand ? " AT_RAND" : "", 113439beb93cSSam Leffler !attr->autn ? " AT_AUTN" : ""); 113539beb93cSSam Leffler return eap_aka_client_error(data, id, 113639beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 113739beb93cSSam Leffler } 113839beb93cSSam Leffler os_memcpy(data->rand, attr->rand, EAP_AKA_RAND_LEN); 113939beb93cSSam Leffler os_memcpy(data->autn, attr->autn, EAP_AKA_AUTN_LEN); 114039beb93cSSam Leffler 114139beb93cSSam Leffler res = eap_aka_umts_auth(sm, data); 114239beb93cSSam Leffler if (res == -1) { 114339beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication " 114439beb93cSSam Leffler "failed (AUTN)"); 114539beb93cSSam Leffler return eap_aka_authentication_reject(data, id); 114639beb93cSSam Leffler } else if (res == -2) { 114739beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication " 114839beb93cSSam Leffler "failed (AUTN seq# -> AUTS)"); 114985732ac8SCy Schubert return eap_aka_synchronization_failure(data, id, attr); 11505b9c547cSRui Paulo } else if (res > 0) { 11515b9c547cSRui Paulo wpa_printf(MSG_DEBUG, "EAP-AKA: Wait for external USIM processing"); 11525b9c547cSRui Paulo return NULL; 115339beb93cSSam Leffler } else if (res) { 115439beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: UMTS authentication failed"); 115539beb93cSSam Leffler return eap_aka_client_error(data, id, 115639beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 115739beb93cSSam Leffler } 115839beb93cSSam Leffler #ifdef EAP_AKA_PRIME 115939beb93cSSam Leffler if (data->eap_method == EAP_TYPE_AKA_PRIME) { 116039beb93cSSam Leffler /* Note: AUTN = (SQN ^ AK) || AMF || MAC which gives us the 116139beb93cSSam Leffler * needed 6-octet SQN ^ AK for CK',IK' derivation */ 116239beb93cSSam Leffler u16 amf = WPA_GET_BE16(data->autn + 6); 116339beb93cSSam Leffler if (!(amf & 0x8000)) { 116439beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA': AMF separation bit " 116539beb93cSSam Leffler "not set (AMF=0x%4x)", amf); 116639beb93cSSam Leffler return eap_aka_authentication_reject(data, id); 116739beb93cSSam Leffler } 116839beb93cSSam Leffler eap_aka_prime_derive_ck_ik_prime(data->ck, data->ik, 116939beb93cSSam Leffler data->autn, 117039beb93cSSam Leffler data->network_name, 117139beb93cSSam Leffler data->network_name_len); 117239beb93cSSam Leffler } 117339beb93cSSam Leffler #endif /* EAP_AKA_PRIME */ 117485732ac8SCy Schubert 1175*a90b9d01SCy Schubert identity = data->mk_identity; 1176*a90b9d01SCy Schubert identity_len = data->mk_identity_len; 117739beb93cSSam Leffler wpa_hexdump_ascii(MSG_DEBUG, "EAP-AKA: Selected identity for MK " 117839beb93cSSam Leffler "derivation", identity, identity_len); 117939beb93cSSam Leffler if (data->eap_method == EAP_TYPE_AKA_PRIME) { 118039beb93cSSam Leffler eap_aka_prime_derive_keys(identity, identity_len, data->ik, 118139beb93cSSam Leffler data->ck, data->k_encr, data->k_aut, 118239beb93cSSam Leffler data->k_re, data->msk, data->emsk); 118339beb93cSSam Leffler } else { 118439beb93cSSam Leffler eap_aka_derive_mk(identity, identity_len, data->ik, data->ck, 118539beb93cSSam Leffler data->mk); 118639beb93cSSam Leffler eap_sim_derive_keys(data->mk, data->k_encr, data->k_aut, 118739beb93cSSam Leffler data->msk, data->emsk); 118839beb93cSSam Leffler } 118939beb93cSSam Leffler if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) { 119039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Challenge message " 119139beb93cSSam Leffler "used invalid AT_MAC"); 1192206b73d0SCy Schubert #ifdef TEST_FUZZ 1193206b73d0SCy Schubert wpa_printf(MSG_INFO, 1194206b73d0SCy Schubert "TEST: Ignore AT_MAC mismatch for fuzz testing"); 1195206b73d0SCy Schubert #else /* TEST_FUZZ */ 119639beb93cSSam Leffler return eap_aka_client_error(data, id, 119739beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 1198206b73d0SCy Schubert #endif /* TEST_FUZZ */ 119939beb93cSSam Leffler } 120039beb93cSSam Leffler 1201f05cddf9SRui Paulo /* Old reauthentication identity must not be used anymore. In 1202f05cddf9SRui Paulo * other words, if no new identities are received, full 1203f05cddf9SRui Paulo * authentication will be used on next reauthentication (using 1204f05cddf9SRui Paulo * pseudonym identity or permanent identity). */ 1205*a90b9d01SCy Schubert eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID); 120639beb93cSSam Leffler 120739beb93cSSam Leffler if (attr->encr_data) { 120839beb93cSSam Leffler u8 *decrypted; 120939beb93cSSam Leffler decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data, 121039beb93cSSam Leffler attr->encr_data_len, attr->iv, 121139beb93cSSam Leffler &eattr, 0); 121239beb93cSSam Leffler if (decrypted == NULL) { 121339beb93cSSam Leffler return eap_aka_client_error( 121439beb93cSSam Leffler data, id, EAP_AKA_UNABLE_TO_PROCESS_PACKET); 121539beb93cSSam Leffler } 1216f05cddf9SRui Paulo eap_aka_learn_ids(sm, data, &eattr); 121739beb93cSSam Leffler os_free(decrypted); 121839beb93cSSam Leffler } 121939beb93cSSam Leffler 122039beb93cSSam Leffler if (data->result_ind && attr->result_ind) 122139beb93cSSam Leffler data->use_result_ind = 1; 122239beb93cSSam Leffler 12235b9c547cSRui Paulo if (data->state != FAILURE) { 122439beb93cSSam Leffler eap_aka_state(data, data->use_result_ind ? 122539beb93cSSam Leffler RESULT_SUCCESS : SUCCESS); 122639beb93cSSam Leffler } 122739beb93cSSam Leffler 122839beb93cSSam Leffler data->num_id_req = 0; 122939beb93cSSam Leffler data->num_notification = 0; 123039beb93cSSam Leffler /* RFC 4187 specifies that counter is initialized to one after 123139beb93cSSam Leffler * fullauth, but initializing it to zero makes it easier to implement 123239beb93cSSam Leffler * reauth verification. */ 123339beb93cSSam Leffler data->counter = 0; 123439beb93cSSam Leffler return eap_aka_response_challenge(data, id); 123539beb93cSSam Leffler } 123639beb93cSSam Leffler 123739beb93cSSam Leffler 123839beb93cSSam Leffler static int eap_aka_process_notification_reauth(struct eap_aka_data *data, 123939beb93cSSam Leffler struct eap_sim_attrs *attr) 124039beb93cSSam Leffler { 124139beb93cSSam Leffler struct eap_sim_attrs eattr; 124239beb93cSSam Leffler u8 *decrypted; 124339beb93cSSam Leffler 124439beb93cSSam Leffler if (attr->encr_data == NULL || attr->iv == NULL) { 124539beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Notification message after " 124639beb93cSSam Leffler "reauth did not include encrypted data"); 124739beb93cSSam Leffler return -1; 124839beb93cSSam Leffler } 124939beb93cSSam Leffler 125039beb93cSSam Leffler decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data, 125139beb93cSSam Leffler attr->encr_data_len, attr->iv, &eattr, 125239beb93cSSam Leffler 0); 125339beb93cSSam Leffler if (decrypted == NULL) { 125439beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted " 125539beb93cSSam Leffler "data from notification message"); 125639beb93cSSam Leffler return -1; 125739beb93cSSam Leffler } 125839beb93cSSam Leffler 125939beb93cSSam Leffler if (eattr.counter < 0 || (size_t) eattr.counter != data->counter) { 126039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Counter in notification " 126139beb93cSSam Leffler "message does not match with counter in reauth " 126239beb93cSSam Leffler "message"); 126339beb93cSSam Leffler os_free(decrypted); 126439beb93cSSam Leffler return -1; 126539beb93cSSam Leffler } 126639beb93cSSam Leffler 126739beb93cSSam Leffler os_free(decrypted); 126839beb93cSSam Leffler return 0; 126939beb93cSSam Leffler } 127039beb93cSSam Leffler 127139beb93cSSam Leffler 127239beb93cSSam Leffler static int eap_aka_process_notification_auth(struct eap_aka_data *data, 127339beb93cSSam Leffler const struct wpabuf *reqData, 127439beb93cSSam Leffler struct eap_sim_attrs *attr) 127539beb93cSSam Leffler { 127639beb93cSSam Leffler if (attr->mac == NULL) { 127739beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: no AT_MAC in after_auth " 127839beb93cSSam Leffler "Notification message"); 127939beb93cSSam Leffler return -1; 128039beb93cSSam Leffler } 128139beb93cSSam Leffler 128239beb93cSSam Leffler if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) { 128339beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Notification message " 128439beb93cSSam Leffler "used invalid AT_MAC"); 128539beb93cSSam Leffler return -1; 128639beb93cSSam Leffler } 128739beb93cSSam Leffler 128839beb93cSSam Leffler if (data->reauth && 128939beb93cSSam Leffler eap_aka_process_notification_reauth(data, attr)) { 129039beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Invalid notification " 129139beb93cSSam Leffler "message after reauth"); 129239beb93cSSam Leffler return -1; 129339beb93cSSam Leffler } 129439beb93cSSam Leffler 129539beb93cSSam Leffler return 0; 129639beb93cSSam Leffler } 129739beb93cSSam Leffler 129839beb93cSSam Leffler 129939beb93cSSam Leffler static struct wpabuf * eap_aka_process_notification( 130039beb93cSSam Leffler struct eap_sm *sm, struct eap_aka_data *data, u8 id, 130139beb93cSSam Leffler const struct wpabuf *reqData, struct eap_sim_attrs *attr) 130239beb93cSSam Leffler { 130339beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Notification"); 130439beb93cSSam Leffler if (data->num_notification > 0) { 130539beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: too many notification " 130639beb93cSSam Leffler "rounds (only one allowed)"); 130739beb93cSSam Leffler return eap_aka_client_error(data, id, 130839beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 130939beb93cSSam Leffler } 131039beb93cSSam Leffler data->num_notification++; 131139beb93cSSam Leffler if (attr->notification == -1) { 131239beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: no AT_NOTIFICATION in " 131339beb93cSSam Leffler "Notification message"); 131439beb93cSSam Leffler return eap_aka_client_error(data, id, 131539beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 131639beb93cSSam Leffler } 131739beb93cSSam Leffler 131839beb93cSSam Leffler if ((attr->notification & 0x4000) == 0 && 131939beb93cSSam Leffler eap_aka_process_notification_auth(data, reqData, attr)) { 132039beb93cSSam Leffler return eap_aka_client_error(data, id, 132139beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 132239beb93cSSam Leffler } 132339beb93cSSam Leffler 132439beb93cSSam Leffler eap_sim_report_notification(sm->msg_ctx, attr->notification, 1); 132539beb93cSSam Leffler if (attr->notification >= 0 && attr->notification < 32768) { 132685732ac8SCy Schubert data->error_code = attr->notification; 132739beb93cSSam Leffler eap_aka_state(data, FAILURE); 132839beb93cSSam Leffler } else if (attr->notification == EAP_SIM_SUCCESS && 132939beb93cSSam Leffler data->state == RESULT_SUCCESS) 133039beb93cSSam Leffler eap_aka_state(data, SUCCESS); 133139beb93cSSam Leffler return eap_aka_response_notification(data, id, attr->notification); 133239beb93cSSam Leffler } 133339beb93cSSam Leffler 133439beb93cSSam Leffler 133539beb93cSSam Leffler static struct wpabuf * eap_aka_process_reauthentication( 133639beb93cSSam Leffler struct eap_sm *sm, struct eap_aka_data *data, u8 id, 133739beb93cSSam Leffler const struct wpabuf *reqData, struct eap_sim_attrs *attr) 133839beb93cSSam Leffler { 133939beb93cSSam Leffler struct eap_sim_attrs eattr; 134039beb93cSSam Leffler u8 *decrypted; 134139beb93cSSam Leffler 134239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Reauthentication"); 134339beb93cSSam Leffler 134439beb93cSSam Leffler if (attr->checkcode && 134539beb93cSSam Leffler eap_aka_verify_checkcode(data, attr->checkcode, 134639beb93cSSam Leffler attr->checkcode_len)) { 1347206b73d0SCy Schubert #ifdef TEST_FUZZ 1348206b73d0SCy Schubert wpa_printf(MSG_INFO, 1349206b73d0SCy Schubert "TEST: Ignore AT_CHECKCODE mismatch for fuzz testing"); 1350206b73d0SCy Schubert #else /* TEST_FUZZ */ 135139beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Invalid AT_CHECKCODE in the " 135239beb93cSSam Leffler "message"); 1353206b73d0SCy Schubert #endif /* TEST_FUZZ */ 135439beb93cSSam Leffler return eap_aka_client_error(data, id, 135539beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 135639beb93cSSam Leffler } 135739beb93cSSam Leffler 135839beb93cSSam Leffler if (data->reauth_id == NULL) { 135939beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Server is trying " 136039beb93cSSam Leffler "reauthentication, but no reauth_id available"); 136139beb93cSSam Leffler return eap_aka_client_error(data, id, 136239beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 136339beb93cSSam Leffler } 136439beb93cSSam Leffler 136539beb93cSSam Leffler data->reauth = 1; 136639beb93cSSam Leffler if (eap_aka_verify_mac(data, reqData, attr->mac, (u8 *) "", 0)) { 136739beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication " 136839beb93cSSam Leffler "did not have valid AT_MAC"); 136939beb93cSSam Leffler return eap_aka_client_error(data, id, 137039beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 137139beb93cSSam Leffler } 137239beb93cSSam Leffler 1373206b73d0SCy Schubert /* At this stage the received MAC has been verified. Use this MAC for 1374206b73d0SCy Schubert * reauth Session-Id calculation if all other checks pass. 1375206b73d0SCy Schubert * The peer does not use the local MAC but the received MAC in deriving 1376206b73d0SCy Schubert * Session-Id. */ 1377206b73d0SCy Schubert os_memcpy(data->reauth_mac, attr->mac, EAP_SIM_MAC_LEN); 1378206b73d0SCy Schubert wpa_hexdump(MSG_DEBUG, "EAP-AKA: Server MAC", 1379206b73d0SCy Schubert data->reauth_mac, EAP_SIM_MAC_LEN); 1380206b73d0SCy Schubert 138139beb93cSSam Leffler if (attr->encr_data == NULL || attr->iv == NULL) { 138239beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Reauthentication " 138339beb93cSSam Leffler "message did not include encrypted data"); 138439beb93cSSam Leffler return eap_aka_client_error(data, id, 138539beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 138639beb93cSSam Leffler } 138739beb93cSSam Leffler 138839beb93cSSam Leffler decrypted = eap_sim_parse_encr(data->k_encr, attr->encr_data, 138939beb93cSSam Leffler attr->encr_data_len, attr->iv, &eattr, 139039beb93cSSam Leffler 0); 139139beb93cSSam Leffler if (decrypted == NULL) { 139239beb93cSSam Leffler wpa_printf(MSG_WARNING, "EAP-AKA: Failed to parse encrypted " 139339beb93cSSam Leffler "data from reauthentication message"); 139439beb93cSSam Leffler return eap_aka_client_error(data, id, 139539beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 139639beb93cSSam Leffler } 139739beb93cSSam Leffler 139839beb93cSSam Leffler if (eattr.nonce_s == NULL || eattr.counter < 0) { 139939beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: (encr) No%s%s in reauth packet", 140039beb93cSSam Leffler !eattr.nonce_s ? " AT_NONCE_S" : "", 140139beb93cSSam Leffler eattr.counter < 0 ? " AT_COUNTER" : ""); 140239beb93cSSam Leffler os_free(decrypted); 140339beb93cSSam Leffler return eap_aka_client_error(data, id, 140439beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 140539beb93cSSam Leffler } 140639beb93cSSam Leffler 140739beb93cSSam Leffler if (eattr.counter < 0 || (size_t) eattr.counter <= data->counter) { 140839beb93cSSam Leffler struct wpabuf *res; 140939beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: (encr) Invalid counter " 141039beb93cSSam Leffler "(%d <= %d)", eattr.counter, data->counter); 141139beb93cSSam Leffler data->counter_too_small = eattr.counter; 141239beb93cSSam Leffler 141339beb93cSSam Leffler /* Reply using Re-auth w/ AT_COUNTER_TOO_SMALL. The current 141439beb93cSSam Leffler * reauth_id must not be used to start a new reauthentication. 1415*a90b9d01SCy Schubert */ 1416*a90b9d01SCy Schubert eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID); 141739beb93cSSam Leffler 141839beb93cSSam Leffler res = eap_aka_response_reauth(data, id, 1, eattr.nonce_s); 141939beb93cSSam Leffler os_free(decrypted); 142039beb93cSSam Leffler 142139beb93cSSam Leffler return res; 142239beb93cSSam Leffler } 142339beb93cSSam Leffler data->counter = eattr.counter; 142439beb93cSSam Leffler 142539beb93cSSam Leffler os_memcpy(data->nonce_s, eattr.nonce_s, EAP_SIM_NONCE_S_LEN); 142639beb93cSSam Leffler wpa_hexdump(MSG_DEBUG, "EAP-AKA: (encr) AT_NONCE_S", 142739beb93cSSam Leffler data->nonce_s, EAP_SIM_NONCE_S_LEN); 142839beb93cSSam Leffler 142939beb93cSSam Leffler if (data->eap_method == EAP_TYPE_AKA_PRIME) { 143039beb93cSSam Leffler eap_aka_prime_derive_keys_reauth(data->k_re, data->counter, 143139beb93cSSam Leffler data->reauth_id, 143239beb93cSSam Leffler data->reauth_id_len, 143339beb93cSSam Leffler data->nonce_s, 143439beb93cSSam Leffler data->msk, data->emsk); 143539beb93cSSam Leffler } else { 143639beb93cSSam Leffler eap_sim_derive_keys_reauth(data->counter, data->reauth_id, 143739beb93cSSam Leffler data->reauth_id_len, 143839beb93cSSam Leffler data->nonce_s, data->mk, 143939beb93cSSam Leffler data->msk, data->emsk); 144039beb93cSSam Leffler } 1441*a90b9d01SCy Schubert eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID); 1442f05cddf9SRui Paulo eap_aka_learn_ids(sm, data, &eattr); 144339beb93cSSam Leffler 144439beb93cSSam Leffler if (data->result_ind && attr->result_ind) 144539beb93cSSam Leffler data->use_result_ind = 1; 144639beb93cSSam Leffler 14475b9c547cSRui Paulo if (data->state != FAILURE) { 144839beb93cSSam Leffler eap_aka_state(data, data->use_result_ind ? 144939beb93cSSam Leffler RESULT_SUCCESS : SUCCESS); 145039beb93cSSam Leffler } 145139beb93cSSam Leffler 145239beb93cSSam Leffler data->num_id_req = 0; 145339beb93cSSam Leffler data->num_notification = 0; 145439beb93cSSam Leffler if (data->counter > EAP_AKA_MAX_FAST_REAUTHS) { 145539beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: Maximum number of " 145639beb93cSSam Leffler "fast reauths performed - force fullauth"); 1457*a90b9d01SCy Schubert eap_aka_clear_identities(sm, data, CLEAR_REAUTH_ID); 145839beb93cSSam Leffler } 145939beb93cSSam Leffler os_free(decrypted); 146039beb93cSSam Leffler return eap_aka_response_reauth(data, id, 0, data->nonce_s); 146139beb93cSSam Leffler } 146239beb93cSSam Leffler 146339beb93cSSam Leffler 146439beb93cSSam Leffler static struct wpabuf * eap_aka_process(struct eap_sm *sm, void *priv, 146539beb93cSSam Leffler struct eap_method_ret *ret, 146639beb93cSSam Leffler const struct wpabuf *reqData) 146739beb93cSSam Leffler { 146839beb93cSSam Leffler struct eap_aka_data *data = priv; 146939beb93cSSam Leffler const struct eap_hdr *req; 147039beb93cSSam Leffler u8 subtype, id; 147139beb93cSSam Leffler struct wpabuf *res; 147239beb93cSSam Leffler const u8 *pos; 147339beb93cSSam Leffler struct eap_sim_attrs attr; 147439beb93cSSam Leffler size_t len; 147539beb93cSSam Leffler 147639beb93cSSam Leffler wpa_hexdump_buf(MSG_DEBUG, "EAP-AKA: EAP data", reqData); 147739beb93cSSam Leffler if (eap_get_config_identity(sm, &len) == NULL) { 147839beb93cSSam Leffler wpa_printf(MSG_INFO, "EAP-AKA: Identity not configured"); 147939beb93cSSam Leffler eap_sm_request_identity(sm); 1480c1d255d3SCy Schubert ret->ignore = true; 148139beb93cSSam Leffler return NULL; 148239beb93cSSam Leffler } 148339beb93cSSam Leffler 148439beb93cSSam Leffler pos = eap_hdr_validate(EAP_VENDOR_IETF, data->eap_method, reqData, 148539beb93cSSam Leffler &len); 1486325151a3SRui Paulo if (pos == NULL || len < 3) { 1487c1d255d3SCy Schubert ret->ignore = true; 148839beb93cSSam Leffler return NULL; 148939beb93cSSam Leffler } 149039beb93cSSam Leffler req = wpabuf_head(reqData); 149139beb93cSSam Leffler id = req->identifier; 149239beb93cSSam Leffler len = be_to_host16(req->length); 149339beb93cSSam Leffler 1494c1d255d3SCy Schubert ret->ignore = false; 149539beb93cSSam Leffler ret->methodState = METHOD_MAY_CONT; 149639beb93cSSam Leffler ret->decision = DECISION_FAIL; 1497c1d255d3SCy Schubert ret->allowNotifications = true; 149839beb93cSSam Leffler 149939beb93cSSam Leffler subtype = *pos++; 150039beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: Subtype=%d", subtype); 150139beb93cSSam Leffler pos += 2; /* Reserved */ 150239beb93cSSam Leffler 150339beb93cSSam Leffler if (eap_sim_parse_attr(pos, wpabuf_head_u8(reqData) + len, &attr, 150439beb93cSSam Leffler data->eap_method == EAP_TYPE_AKA_PRIME ? 2 : 1, 150539beb93cSSam Leffler 0)) { 150639beb93cSSam Leffler res = eap_aka_client_error(data, id, 150739beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 150839beb93cSSam Leffler goto done; 150939beb93cSSam Leffler } 151039beb93cSSam Leffler 151139beb93cSSam Leffler switch (subtype) { 151239beb93cSSam Leffler case EAP_AKA_SUBTYPE_IDENTITY: 151339beb93cSSam Leffler res = eap_aka_process_identity(sm, data, id, reqData, &attr); 151439beb93cSSam Leffler break; 151539beb93cSSam Leffler case EAP_AKA_SUBTYPE_CHALLENGE: 151639beb93cSSam Leffler res = eap_aka_process_challenge(sm, data, id, reqData, &attr); 151739beb93cSSam Leffler break; 151839beb93cSSam Leffler case EAP_AKA_SUBTYPE_NOTIFICATION: 151939beb93cSSam Leffler res = eap_aka_process_notification(sm, data, id, reqData, 152039beb93cSSam Leffler &attr); 152139beb93cSSam Leffler break; 152239beb93cSSam Leffler case EAP_AKA_SUBTYPE_REAUTHENTICATION: 152339beb93cSSam Leffler res = eap_aka_process_reauthentication(sm, data, id, reqData, 152439beb93cSSam Leffler &attr); 152539beb93cSSam Leffler break; 152639beb93cSSam Leffler case EAP_AKA_SUBTYPE_CLIENT_ERROR: 152739beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: subtype Client-Error"); 152839beb93cSSam Leffler res = eap_aka_client_error(data, id, 152939beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 153039beb93cSSam Leffler break; 153139beb93cSSam Leffler default: 153239beb93cSSam Leffler wpa_printf(MSG_DEBUG, "EAP-AKA: Unknown subtype=%d", subtype); 153339beb93cSSam Leffler res = eap_aka_client_error(data, id, 153439beb93cSSam Leffler EAP_AKA_UNABLE_TO_PROCESS_PACKET); 153539beb93cSSam Leffler break; 153639beb93cSSam Leffler } 153739beb93cSSam Leffler 153839beb93cSSam Leffler done: 153939beb93cSSam Leffler if (data->state == FAILURE) { 154039beb93cSSam Leffler ret->decision = DECISION_FAIL; 154139beb93cSSam Leffler ret->methodState = METHOD_DONE; 154239beb93cSSam Leffler } else if (data->state == SUCCESS) { 154339beb93cSSam Leffler ret->decision = data->use_result_ind ? 154439beb93cSSam Leffler DECISION_UNCOND_SUCC : DECISION_COND_SUCC; 154539beb93cSSam Leffler /* 154639beb93cSSam Leffler * It is possible for the server to reply with AKA 154739beb93cSSam Leffler * Notification, so we must allow the method to continue and 154839beb93cSSam Leffler * not only accept EAP-Success at this point. 154939beb93cSSam Leffler */ 155039beb93cSSam Leffler ret->methodState = data->use_result_ind ? 155139beb93cSSam Leffler METHOD_DONE : METHOD_MAY_CONT; 15525b9c547cSRui Paulo } else if (data->state == RESULT_SUCCESS) 155339beb93cSSam Leffler ret->methodState = METHOD_CONT; 155439beb93cSSam Leffler 155539beb93cSSam Leffler if (ret->methodState == METHOD_DONE) { 1556c1d255d3SCy Schubert ret->allowNotifications = false; 155739beb93cSSam Leffler } 155839beb93cSSam Leffler 155939beb93cSSam Leffler return res; 156039beb93cSSam Leffler } 156139beb93cSSam Leffler 156239beb93cSSam Leffler 1563c1d255d3SCy Schubert static bool eap_aka_has_reauth_data(struct eap_sm *sm, void *priv) 156439beb93cSSam Leffler { 156539beb93cSSam Leffler struct eap_aka_data *data = priv; 156639beb93cSSam Leffler return data->pseudonym || data->reauth_id; 156739beb93cSSam Leffler } 156839beb93cSSam Leffler 156939beb93cSSam Leffler 157039beb93cSSam Leffler static void eap_aka_deinit_for_reauth(struct eap_sm *sm, void *priv) 157139beb93cSSam Leffler { 157239beb93cSSam Leffler struct eap_aka_data *data = priv; 1573*a90b9d01SCy Schubert 1574*a90b9d01SCy Schubert os_free(data->mk_identity); 1575*a90b9d01SCy Schubert data->mk_identity = NULL; 1576*a90b9d01SCy Schubert data->mk_identity_len = 0; 157739beb93cSSam Leffler data->prev_id = -1; 157839beb93cSSam Leffler wpabuf_free(data->id_msgs); 157939beb93cSSam Leffler data->id_msgs = NULL; 158039beb93cSSam Leffler data->use_result_ind = 0; 158139beb93cSSam Leffler data->kdf_negotiation = 0; 15825b9c547cSRui Paulo eap_aka_clear_keys(data, 1); 158339beb93cSSam Leffler } 158439beb93cSSam Leffler 158539beb93cSSam Leffler 158639beb93cSSam Leffler static void * eap_aka_init_for_reauth(struct eap_sm *sm, void *priv) 158739beb93cSSam Leffler { 158839beb93cSSam Leffler struct eap_aka_data *data = priv; 1589*a90b9d01SCy Schubert 1590*a90b9d01SCy Schubert if (sm->identity) { 1591*a90b9d01SCy Schubert /* Use the EAP-Response/Identity in MK derivation if AT_IDENTITY 1592*a90b9d01SCy Schubert * is not used. */ 1593*a90b9d01SCy Schubert os_free(data->mk_identity); 1594*a90b9d01SCy Schubert data->mk_identity = os_memdup(sm->identity, sm->identity_len); 1595*a90b9d01SCy Schubert data->mk_identity_len = sm->identity_len; 1596*a90b9d01SCy Schubert } 1597*a90b9d01SCy Schubert 159839beb93cSSam Leffler data->num_id_req = 0; 159939beb93cSSam Leffler data->num_notification = 0; 160039beb93cSSam Leffler eap_aka_state(data, CONTINUE); 160139beb93cSSam Leffler return priv; 160239beb93cSSam Leffler } 160339beb93cSSam Leffler 160439beb93cSSam Leffler 160539beb93cSSam Leffler static const u8 * eap_aka_get_identity(struct eap_sm *sm, void *priv, 160639beb93cSSam Leffler size_t *len) 160739beb93cSSam Leffler { 160839beb93cSSam Leffler struct eap_aka_data *data = priv; 160939beb93cSSam Leffler 161039beb93cSSam Leffler if (data->reauth_id) { 161139beb93cSSam Leffler *len = data->reauth_id_len; 161239beb93cSSam Leffler return data->reauth_id; 161339beb93cSSam Leffler } 161439beb93cSSam Leffler 161539beb93cSSam Leffler if (data->pseudonym) { 161639beb93cSSam Leffler *len = data->pseudonym_len; 161739beb93cSSam Leffler return data->pseudonym; 161839beb93cSSam Leffler } 161939beb93cSSam Leffler 162039beb93cSSam Leffler return NULL; 162139beb93cSSam Leffler } 162239beb93cSSam Leffler 162339beb93cSSam Leffler 1624c1d255d3SCy Schubert static bool eap_aka_isKeyAvailable(struct eap_sm *sm, void *priv) 162539beb93cSSam Leffler { 162639beb93cSSam Leffler struct eap_aka_data *data = priv; 162739beb93cSSam Leffler return data->state == SUCCESS; 162839beb93cSSam Leffler } 162939beb93cSSam Leffler 163039beb93cSSam Leffler 163139beb93cSSam Leffler static u8 * eap_aka_getKey(struct eap_sm *sm, void *priv, size_t *len) 163239beb93cSSam Leffler { 163339beb93cSSam Leffler struct eap_aka_data *data = priv; 163439beb93cSSam Leffler u8 *key; 163539beb93cSSam Leffler 163639beb93cSSam Leffler if (data->state != SUCCESS) 163739beb93cSSam Leffler return NULL; 163839beb93cSSam Leffler 163985732ac8SCy Schubert key = os_memdup(data->msk, EAP_SIM_KEYING_DATA_LEN); 164039beb93cSSam Leffler if (key == NULL) 164139beb93cSSam Leffler return NULL; 164239beb93cSSam Leffler 164339beb93cSSam Leffler *len = EAP_SIM_KEYING_DATA_LEN; 164439beb93cSSam Leffler 164539beb93cSSam Leffler return key; 164639beb93cSSam Leffler } 164739beb93cSSam Leffler 164839beb93cSSam Leffler 16495b9c547cSRui Paulo static u8 * eap_aka_get_session_id(struct eap_sm *sm, void *priv, size_t *len) 16505b9c547cSRui Paulo { 16515b9c547cSRui Paulo struct eap_aka_data *data = priv; 16525b9c547cSRui Paulo u8 *id; 16535b9c547cSRui Paulo 16545b9c547cSRui Paulo if (data->state != SUCCESS) 16555b9c547cSRui Paulo return NULL; 16565b9c547cSRui Paulo 1657206b73d0SCy Schubert if (!data->reauth) 16585b9c547cSRui Paulo *len = 1 + EAP_AKA_RAND_LEN + EAP_AKA_AUTN_LEN; 1659206b73d0SCy Schubert else 1660206b73d0SCy Schubert *len = 1 + EAP_SIM_NONCE_S_LEN + EAP_SIM_MAC_LEN; 16615b9c547cSRui Paulo id = os_malloc(*len); 16625b9c547cSRui Paulo if (id == NULL) 16635b9c547cSRui Paulo return NULL; 16645b9c547cSRui Paulo 16655b9c547cSRui Paulo id[0] = data->eap_method; 1666206b73d0SCy Schubert if (!data->reauth) { 16675b9c547cSRui Paulo os_memcpy(id + 1, data->rand, EAP_AKA_RAND_LEN); 1668206b73d0SCy Schubert os_memcpy(id + 1 + EAP_AKA_RAND_LEN, data->autn, 1669206b73d0SCy Schubert EAP_AKA_AUTN_LEN); 1670206b73d0SCy Schubert } else { 1671206b73d0SCy Schubert os_memcpy(id + 1, data->nonce_s, EAP_SIM_NONCE_S_LEN); 1672206b73d0SCy Schubert os_memcpy(id + 1 + EAP_SIM_NONCE_S_LEN, data->reauth_mac, 1673206b73d0SCy Schubert EAP_SIM_MAC_LEN); 1674206b73d0SCy Schubert } 16755b9c547cSRui Paulo wpa_hexdump(MSG_DEBUG, "EAP-AKA: Derived Session-Id", id, *len); 16765b9c547cSRui Paulo 16775b9c547cSRui Paulo return id; 16785b9c547cSRui Paulo } 16795b9c547cSRui Paulo 16805b9c547cSRui Paulo 168139beb93cSSam Leffler static u8 * eap_aka_get_emsk(struct eap_sm *sm, void *priv, size_t *len) 168239beb93cSSam Leffler { 168339beb93cSSam Leffler struct eap_aka_data *data = priv; 168439beb93cSSam Leffler u8 *key; 168539beb93cSSam Leffler 168639beb93cSSam Leffler if (data->state != SUCCESS) 168739beb93cSSam Leffler return NULL; 168839beb93cSSam Leffler 168985732ac8SCy Schubert key = os_memdup(data->emsk, EAP_EMSK_LEN); 169039beb93cSSam Leffler if (key == NULL) 169139beb93cSSam Leffler return NULL; 169239beb93cSSam Leffler 169339beb93cSSam Leffler *len = EAP_EMSK_LEN; 169439beb93cSSam Leffler 169539beb93cSSam Leffler return key; 169639beb93cSSam Leffler } 169739beb93cSSam Leffler 169839beb93cSSam Leffler 169985732ac8SCy Schubert static int eap_aka_get_error_code(void *priv) 170085732ac8SCy Schubert { 170185732ac8SCy Schubert struct eap_aka_data *data = priv; 170285732ac8SCy Schubert int current_data_error; 170385732ac8SCy Schubert 170485732ac8SCy Schubert if (!data) 170585732ac8SCy Schubert return NO_EAP_METHOD_ERROR; 170685732ac8SCy Schubert 170785732ac8SCy Schubert current_data_error = data->error_code; 170885732ac8SCy Schubert 170985732ac8SCy Schubert /* Now reset for next transaction */ 171085732ac8SCy Schubert data->error_code = NO_EAP_METHOD_ERROR; 171185732ac8SCy Schubert 171285732ac8SCy Schubert return current_data_error; 171385732ac8SCy Schubert } 171485732ac8SCy Schubert 171585732ac8SCy Schubert 171639beb93cSSam Leffler int eap_peer_aka_register(void) 171739beb93cSSam Leffler { 171839beb93cSSam Leffler struct eap_method *eap; 171939beb93cSSam Leffler 172039beb93cSSam Leffler eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION, 172139beb93cSSam Leffler EAP_VENDOR_IETF, EAP_TYPE_AKA, "AKA"); 172239beb93cSSam Leffler if (eap == NULL) 172339beb93cSSam Leffler return -1; 172439beb93cSSam Leffler 172539beb93cSSam Leffler eap->init = eap_aka_init; 172639beb93cSSam Leffler eap->deinit = eap_aka_deinit; 172739beb93cSSam Leffler eap->process = eap_aka_process; 172839beb93cSSam Leffler eap->isKeyAvailable = eap_aka_isKeyAvailable; 172939beb93cSSam Leffler eap->getKey = eap_aka_getKey; 17305b9c547cSRui Paulo eap->getSessionId = eap_aka_get_session_id; 173139beb93cSSam Leffler eap->has_reauth_data = eap_aka_has_reauth_data; 173239beb93cSSam Leffler eap->deinit_for_reauth = eap_aka_deinit_for_reauth; 173339beb93cSSam Leffler eap->init_for_reauth = eap_aka_init_for_reauth; 173439beb93cSSam Leffler eap->get_identity = eap_aka_get_identity; 173539beb93cSSam Leffler eap->get_emsk = eap_aka_get_emsk; 173685732ac8SCy Schubert eap->get_error_code = eap_aka_get_error_code; 173739beb93cSSam Leffler 1738780fb4a2SCy Schubert return eap_peer_method_register(eap); 173939beb93cSSam Leffler } 174039beb93cSSam Leffler 174139beb93cSSam Leffler 174239beb93cSSam Leffler #ifdef EAP_AKA_PRIME 174339beb93cSSam Leffler int eap_peer_aka_prime_register(void) 174439beb93cSSam Leffler { 174539beb93cSSam Leffler struct eap_method *eap; 174639beb93cSSam Leffler 174739beb93cSSam Leffler eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION, 174839beb93cSSam Leffler EAP_VENDOR_IETF, EAP_TYPE_AKA_PRIME, 174939beb93cSSam Leffler "AKA'"); 175039beb93cSSam Leffler if (eap == NULL) 175139beb93cSSam Leffler return -1; 175239beb93cSSam Leffler 175339beb93cSSam Leffler eap->init = eap_aka_prime_init; 175439beb93cSSam Leffler eap->deinit = eap_aka_deinit; 175539beb93cSSam Leffler eap->process = eap_aka_process; 175639beb93cSSam Leffler eap->isKeyAvailable = eap_aka_isKeyAvailable; 175739beb93cSSam Leffler eap->getKey = eap_aka_getKey; 17585b9c547cSRui Paulo eap->getSessionId = eap_aka_get_session_id; 175939beb93cSSam Leffler eap->has_reauth_data = eap_aka_has_reauth_data; 176039beb93cSSam Leffler eap->deinit_for_reauth = eap_aka_deinit_for_reauth; 176139beb93cSSam Leffler eap->init_for_reauth = eap_aka_init_for_reauth; 176239beb93cSSam Leffler eap->get_identity = eap_aka_get_identity; 176339beb93cSSam Leffler eap->get_emsk = eap_aka_get_emsk; 176485732ac8SCy Schubert eap->get_error_code = eap_aka_get_error_code; 176539beb93cSSam Leffler 1766780fb4a2SCy Schubert return eap_peer_method_register(eap); 176739beb93cSSam Leffler } 176839beb93cSSam Leffler #endif /* EAP_AKA_PRIME */ 1769