From 3eb525563a815a2c86740ba21ff9938ff30d22e3 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Tue, 6 Oct 2020 16:19:27 +0100 Subject: [PATCH] Fix issue reading MNC length in USIM. --- srsue/hdr/stack/upper/pcsc_usim.h | 3 +++ srsue/hdr/stack/upper/usim.h | 5 ++++- srsue/hdr/stack/upper/usim_base.h | 5 ++++- srsue/src/stack/upper/pcsc_usim.cc | 17 +++++++++++++---- srsue/src/stack/upper/usim.cc | 19 +++++++++++++++++++ srsue/src/stack/upper/usim_base.cc | 25 ++++++++++++++----------- 6 files changed, 57 insertions(+), 17 deletions(-) diff --git a/srsue/hdr/stack/upper/pcsc_usim.h b/srsue/hdr/stack/upper/pcsc_usim.h index d0dd9f1e8..3694bf446 100644 --- a/srsue/hdr/stack/upper/pcsc_usim.h +++ b/srsue/hdr/stack/upper/pcsc_usim.h @@ -56,6 +56,9 @@ public: int* res_len, uint8_t* k_asme); + // Helpers + virtual std::string get_mnc_str(const uint8_t* imsi_vec, std::string mcc_str) final; + private: // Smartcard sub-class which is a port of the PC/SC smartcard implementation // of WPA Supplicant written by Jouni Malinen and licensed under BSD diff --git a/srsue/hdr/stack/upper/usim.h b/srsue/hdr/stack/upper/usim.h index 72a6d81be..519559a91 100644 --- a/srsue/hdr/stack/upper/usim.h +++ b/srsue/hdr/stack/upper/usim.h @@ -62,7 +62,10 @@ private: uint8_t* res, int* res_len, uint8_t* k_asme); - void str_to_hex(std::string str, uint8_t* hex); + + // Helpers + virtual std::string get_mnc_str(const uint8_t* imsi_vec, std::string mcc_str) final; + void str_to_hex(std::string str, uint8_t* hex); // User data auth_algo_t auth_algo = auth_algo_milenage; diff --git a/srsue/hdr/stack/upper/usim_base.h b/srsue/hdr/stack/upper/usim_base.h index 9a2360ba1..3349ef372 100644 --- a/srsue/hdr/stack/upper/usim_base.h +++ b/srsue/hdr/stack/upper/usim_base.h @@ -101,6 +101,10 @@ public: void store_keys_before_ho(const srslte::as_security_config_t& as_ctx) final; void restore_keys_from_failed_ho(srslte::as_security_config_t* as_ctx) final; + // Helpers + std::string get_mcc_str(const uint8_t* imsi_vec); + virtual std::string get_mnc_str(const uint8_t* imsi_vec, std::string mcc_str) = 0; + protected: bool initiated = false; @@ -113,7 +117,6 @@ protected: uint64_t imei = 0; std::string imsi_str; std::string imei_str; - uint32_t mnc_length = 0; // Security variables uint8_t ck[CK_LEN] = {}; diff --git a/srsue/src/stack/upper/pcsc_usim.cc b/srsue/src/stack/upper/pcsc_usim.cc index b5eb2af81..304d54eed 100644 --- a/srsue/src/stack/upper/pcsc_usim.cc +++ b/srsue/src/stack/upper/pcsc_usim.cc @@ -91,10 +91,6 @@ int pcsc_usim::init(usim_args_t* args) return ret; } - // Get MNC length - mnc_length = sc.get_mnc_len(); - log->debug("MNC length %d\n", mnc_length); - initiated = true; ret = SRSLTE_SUCCESS; @@ -163,6 +159,19 @@ auth_result_t pcsc_usim::generate_authentication_response(uint8_t* rand, return ret; } +std::string pcsc_usim::get_mnc_str(const uint8_t* imsi_vec, std::string mcc_str) +{ + uint32_t mcc_len = 3; + uint32_t mnc_len = sc.get_mnc_len(); + std::ostringstream mnc_oss; + + for (uint32_t i = mcc_len; i < mcc_len + mnc_len; i++) { + mnc_oss << (int)imsi_vec[i]; + } + + return mnc_oss.str(); +} + /********************************* * PC/SC class ********************************/ diff --git a/srsue/src/stack/upper/usim.cc b/srsue/src/stack/upper/usim.cc index 496b744f7..3895f876b 100644 --- a/srsue/src/stack/upper/usim.cc +++ b/srsue/src/stack/upper/usim.cc @@ -248,6 +248,25 @@ auth_result_t usim::gen_auth_res_xor(uint8_t* rand, return result; } +std::string usim::get_mnc_str(const uint8_t* imsi_vec, std::string mcc_str) +{ + uint32_t mcc_len = 3; + uint32_t mnc_len = 2; + std::ostringstream mnc_oss; + + // US MCC uses 3 MNC digits + if (!mcc_str.compare("310") || !mcc_str.compare("311") || !mcc_str.compare("312") || !mcc_str.compare("313") || + !mcc_str.compare("316")) { + mnc_len = 3; + } + + for (uint32_t i = mcc_len; i < mcc_len + mnc_len; i++) { + mnc_oss << (int)imsi_vec[i]; + } + + return mnc_oss.str(); +} + void usim::str_to_hex(std::string str, uint8_t* hex) { uint32_t i; diff --git a/srsue/src/stack/upper/usim_base.cc b/srsue/src/stack/upper/usim_base.cc index a2ab22c30..b50cff94e 100644 --- a/srsue/src/stack/upper/usim_base.cc +++ b/srsue/src/stack/upper/usim_base.cc @@ -96,6 +96,16 @@ bool usim_base::get_imei_vec(uint8_t* imei_, uint32_t n) return true; } +std::string usim_base::get_mcc_str(const uint8_t* imsi_vec) +{ + std::ostringstream mcc_oss; + uint32_t mcc_len = 3; + for (uint32_t i = 0; i < mcc_len; i++) { + mcc_oss << (int)imsi_vec[i]; + } + return mcc_oss.str(); +} + bool usim_base::get_home_plmn_id(srslte::plmn_id_t* home_plmn_id) { if (!initiated) { @@ -106,18 +116,11 @@ bool usim_base::get_home_plmn_id(srslte::plmn_id_t* home_plmn_id) uint8_t imsi_vec[15]; get_imsi_vec(imsi_vec, 15); - std::ostringstream plmn_str; - - uint mcc_len = 3; - for (uint i = 0; i < mcc_len; i++) { - plmn_str << (int)imsi_vec[i]; - } - - for (uint i = mcc_len; i < mcc_len + mnc_length; i++) { - plmn_str << (int)imsi_vec[i]; - } + std::string mcc_str = get_mcc_str(imsi_vec); + std::string mnc_str = get_mnc_str(imsi_vec, mcc_str); - if (home_plmn_id->from_string(plmn_str.str())) { + std::string plmn_str = mcc_str + mnc_str; + if (home_plmn_id->from_string(plmn_str)) { log->error("Error reading home PLMN from SIM.\n"); return false; }