From 04ef6e120deb590f942d2efdb04c08bde37061d8 Mon Sep 17 00:00:00 2001 From: David Rupprecht Date: Fri, 9 Jul 2021 13:08:06 +0200 Subject: [PATCH] Added new SIM functions to get MCC, MNC and MSIN --- .../srsran/interfaces/ue_usim_interfaces.h | 6 ++ srsue/hdr/stack/upper/usim_base.h | 3 + srsue/src/stack/upper/nas_5g.cc | 12 +-- srsue/src/stack/upper/test/usim_test.cc | 58 +++++++++++-- srsue/src/stack/upper/usim_base.cc | 83 +++++++++++++++++++ 5 files changed, 152 insertions(+), 10 deletions(-) diff --git a/lib/include/srsran/interfaces/ue_usim_interfaces.h b/lib/include/srsran/interfaces/ue_usim_interfaces.h index ae1112b44..34d15a5f3 100644 --- a/lib/include/srsran/interfaces/ue_usim_interfaces.h +++ b/lib/include/srsran/interfaces/ue_usim_interfaces.h @@ -29,6 +29,12 @@ public: virtual bool get_imsi_vec(uint8_t* imsi_, uint32_t n) = 0; virtual bool get_imei_vec(uint8_t* imei_, uint32_t n) = 0; virtual bool get_home_plmn_id(srsran::plmn_id_t* home_plmn_id) = 0; + // Get the home mcc as bytes array + virtual bool get_home_mcc_bytes(uint8_t* mcc_, uint32_t n) = 0; + // Get the home mnc as byte array + virtual bool get_home_mnc_bytes(uint8_t* mnc_, uint32_t n) = 0; + // Get the home msin in bytes array encoded as bcd + virtual bool get_home_msin_bcd(uint8_t* msin_, uint32_t n) = 0; virtual auth_result_t generate_authentication_response(uint8_t* rand, uint8_t* autn_enb, uint16_t mcc, diff --git a/srsue/hdr/stack/upper/usim_base.h b/srsue/hdr/stack/upper/usim_base.h index 54f7175a0..fcda0c6aa 100644 --- a/srsue/hdr/stack/upper/usim_base.h +++ b/srsue/hdr/stack/upper/usim_base.h @@ -70,6 +70,9 @@ public: std::string get_imei_str() final; bool get_imsi_vec(uint8_t* imsi_, uint32_t n) final; + bool get_home_mcc_bytes(uint8_t* mcc_, uint32_t n) final; + bool get_home_mnc_bytes(uint8_t* mnc_, uint32_t n) final; + bool get_home_msin_bcd(uint8_t* msin_, uint32_t n) final; bool get_imei_vec(uint8_t* imei_, uint32_t n) final; bool get_home_plmn_id(srsran::plmn_id_t* home_plmn_id) final; diff --git a/srsue/src/stack/upper/nas_5g.cc b/srsue/src/stack/upper/nas_5g.cc index a210af22f..e35484b47 100644 --- a/srsue/src/stack/upper/nas_5g.cc +++ b/srsue/src/stack/upper/nas_5g.cc @@ -147,6 +147,8 @@ int nas_5g::send_registration_request() logger.info("Generating registration request"); nas_5gs_msg nas_msg; + nas_msg.hdr.extended_protocol_discriminator = + nas_5gs_hdr::extended_protocol_discriminator_opts::extended_protocol_discriminator_5gmm; registration_request_t& reg_req = nas_msg.set_registration_request(); reg_req.registration_type_5gs.follow_on_request_bit = @@ -155,11 +157,11 @@ int nas_5g::send_registration_request() registration_type_5gs_t::registration_type_type_::options::initial_registration; mobile_identity_5gs_t::suci_s& suci = reg_req.mobile_identity_5gs.set_suci(); suci.supi_format = mobile_identity_5gs_t::suci_s::supi_format_type_::options::imsi; - mcc_to_bytes(0x0, suci.mcc.data()); - uint8_t mnc_len; - mnc_to_bytes(0x0, suci.mnc.data(), &mnc_len); - suci.scheme_output.resize(15); - usim->get_imsi_vec(suci.scheme_output.data(), 15); + usim->get_home_mcc_bytes(suci.mcc.data(), suci.mcc.size()); + usim->get_home_mcc_bytes(suci.mnc.data(), suci.mnc.size()); + + suci.scheme_output.resize(5); + usim->get_home_msin_bcd(suci.scheme_output.data(), 5); logger.info("Requesting IMSI attach (IMSI=%s)", usim->get_imsi_str().c_str()); if (nas_msg.pack(pdu) != SRSASN_SUCCESS) { diff --git a/srsue/src/stack/upper/test/usim_test.cc b/srsue/src/stack/upper/test/usim_test.cc index 7ca21ef8d..ed4300fb7 100644 --- a/srsue/src/stack/upper/test/usim_test.cc +++ b/srsue/src/stack/upper/test/usim_test.cc @@ -51,12 +51,8 @@ static uint8_t autn_enb[] = static constexpr uint16_t mcc = 208; static constexpr uint16_t mnc = 93; -int main(int argc, char** argv) +int gen_auth_response_test() { - auto& logger = srslog::fetch_basic_logger("USIM", false); - // Start the log backend. - srslog::init(); - uint8_t res[16]; int res_len; uint8_t k_asme[32]; @@ -69,8 +65,60 @@ int main(int argc, char** argv) args.using_op = true; args.op = "11111111111111111111111111111111"; + auto& logger = srslog::fetch_basic_logger("USIM", false); srsue::usim usim(logger); usim.init(&args); TESTASSERT(usim.generate_authentication_response(rand_enb, autn_enb, mcc, mnc, res, &res_len, k_asme) == AUTH_OK); + return SRSRAN_SUCCESS; +} + +int mcc_mnc_msin_test() +{ + usim_args_t args; + args.algo = "milenage"; + args.imei = "356092040793011"; + args.imsi = "208930000000001"; + args.k = "8BAF473F2F8FD09487CCCBD7097C6862"; + args.using_op = true; + args.op = "11111111111111111111111111111111"; + + auto& logger = srslog::fetch_basic_logger("USIM", false); + srsue::usim usim(logger); + usim.init(&args); + + uint8_t mcc_correct[] = {0x2, 0x0, 0x8}; + uint8_t mnc_correct[] = {0x9, 0x3, 0xf}; + uint8_t msin_correct_bcd[] = {0x00, 0x00, 0x00, 0x00, 0x10}; + + uint8_t mcc_test[3]; + usim.get_home_mcc_bytes(mcc_test, 3); + + TESTASSERT(mcc_correct[0] == mcc_test[0]); + TESTASSERT(mcc_correct[1] == mcc_test[1]); + TESTASSERT(mcc_correct[2] == mcc_test[2]); + + uint8_t mnc_test[3]; + usim.get_home_mnc_bytes(mnc_test, 3); + TESTASSERT(mnc_correct[0] == mnc_test[0]); + TESTASSERT(mnc_correct[1] == mnc_test[1]); + TESTASSERT(mnc_correct[2] == mnc_test[2]); + + uint8_t msin_test[5]; + usim.get_home_msin_bcd(msin_test, 5); + + TESTASSERT(msin_correct_bcd[0] == msin_test[0]); + TESTASSERT(msin_correct_bcd[1] == msin_test[1]); + TESTASSERT(msin_correct_bcd[2] == msin_test[2]); + TESTASSERT(msin_correct_bcd[3] == msin_test[3]); + TESTASSERT(msin_correct_bcd[4] == msin_test[4]); + return SRSRAN_SUCCESS; +} + +int main(int argc, char** argv) +{ + // Start the log backend. + srslog::init(); + TESTASSERT(gen_auth_response_test() == SRSRAN_SUCCESS); + TESTASSERT(mcc_mnc_msin_test() == SRSRAN_SUCCESS); } diff --git a/srsue/src/stack/upper/usim_base.cc b/srsue/src/stack/upper/usim_base.cc index 9d9c7dd92..0c4b23674 100644 --- a/srsue/src/stack/upper/usim_base.cc +++ b/srsue/src/stack/upper/usim_base.cc @@ -11,6 +11,7 @@ */ #include "srsue/hdr/stack/upper/usim_base.h" +#include "srsran/common/bcd_helpers.h" #include "srsue/hdr/stack/upper/usim.h" #ifdef HAVE_PCSC @@ -121,6 +122,88 @@ bool usim_base::get_home_plmn_id(srsran::plmn_id_t* home_plmn_id) return true; } +bool usim_base::get_home_mcc_bytes(uint8_t* mcc_, uint32_t n) +{ + if (!initiated) { + logger.error("USIM not initiated!"); + return false; + } + + if (NULL == mcc_ || n < 3) { + logger.error("Invalid parameters to get_home_mcc_bytes"); + return false; + } + + uint8_t imsi_vec[15]; + get_imsi_vec(imsi_vec, 15); + + std::string mcc_str = get_mcc_str(imsi_vec); + uint16_t mcc_bcd = 0; + + srsran::string_to_mcc(mcc_str, &mcc_bcd); + srsran::mcc_to_bytes(mcc_bcd, mcc_); + + return true; +} + +bool usim_base::get_home_mnc_bytes(uint8_t* mnc_, uint32_t n) +{ + if (!initiated) { + logger.error("USIM not initiated!"); + return false; + } + + if (NULL == mnc_ || n < 3) { + logger.error("Invalid parameters to get_home_mcc_bytes"); + return false; + } + + uint8_t imsi_vec[15]; + get_imsi_vec(imsi_vec, 15); + + std::string mcc_str = get_mcc_str(imsi_vec); + std::string mnc_str = get_mnc_str(imsi_vec, mcc_str); + + uint16_t mnc_bcd = 0; + uint8_t len = 0; + + srsran::string_to_mnc(mnc_str, &mnc_bcd); + srsran::mnc_to_bytes(mnc_bcd, mnc_, &len); + + if (len == 2) { + mnc_[2] = 0xf; + } + + return true; +} + +bool usim_base::get_home_msin_bcd(uint8_t* msin_, uint32_t n) +{ + if (!initiated) { + logger.error("USIM not initiated!"); + return false; + } + + if (NULL == msin_ || n < 5) { + logger.error("Invalid parameters to get_home_mcc_bytes"); + return false; + } + + srsran::plmn_id_t tmp_plmn; + get_home_plmn_id(&tmp_plmn); + + uint8_t imsi_vec[15]; + get_imsi_vec(imsi_vec, 15); + + int total_msin_len = (tmp_plmn.nof_mnc_digits + 3) / 2; + int j = 0; + for (int i = tmp_plmn.nof_mnc_digits + 3; i < 15; i += 2) { + msin_[j] = (imsi_vec[i]) | (imsi_vec[i + 1] << 4); + j++; + } + return true; +} + void usim_base::generate_nas_keys(uint8_t* k_asme, uint8_t* k_nas_enc, uint8_t* k_nas_int,