fix IMSI reading from SIM

master
Andre Puschmann 7 years ago
parent 0a6a72832a
commit d7d6bd1a78

@ -47,23 +47,25 @@ pcsc_usim::~pcsc_usim()
int pcsc_usim::init(usim_args_t *args, srslte::log *log_) int pcsc_usim::init(usim_args_t *args, srslte::log *log_)
{ {
int ret = SRSLTE_ERROR;
log = log_; log = log_;
if (sc.init(args, log_) != SRSLTE_SUCCESS) { if (sc.init(args, log_) != SRSLTE_SUCCESS) {
return SRSLTE_ERROR; return ret;
} }
// Read IMSI from SIM card // Read IMSI from SIM card
char tmp[100]; char tmp[15];
size_t tmp_len; size_t tmp_len = 15; // set to max IMSI length
if (sc.get_imsi(tmp, &tmp_len)) { if (sc.get_imsi(tmp, &tmp_len)) {
log->error("Error reading IMSI from SIM\n"); log->error("Error reading IMSI from SIM.\n");
return ret;
} }
imsi_str = tmp; imsi_str = tmp;
// Check extracted IMSI and convert // Check extracted IMSI and convert
const char *imsi_c = imsi_str.c_str(); if(15 == imsi_str.length()) {
if(15 == args->imsi.length()) { const char *imsi_c = imsi_str.c_str();
imsi = 0; imsi = 0;
for(int i = 0; i < 15; i++) for(int i = 0; i < 15; i++)
{ {
@ -71,13 +73,14 @@ int pcsc_usim::init(usim_args_t *args, srslte::log *log_)
imsi += imsi_c[i] - '0'; imsi += imsi_c[i] - '0';
} }
} else { } else {
log->error("Invalid length for ISMI: %zu should be %d\n", imsi_str.length(), 15); log->error("Invalid length for IMSI: %zu should be %d\n", imsi_str.length(), 15);
log->console("Invalid length for IMSI: %zu should be %d\n", imsi_str.length(), 15); log->console("Invalid length for IMSI: %zu should be %d\n", imsi_str.length(), 15);
return ret;
} }
// Check IMEI // Check IMEI
const char *imei_c = args->imei.c_str();
if(15 == args->imei.length()) { if(15 == args->imei.length()) {
const char *imei_c = args->imei.c_str();
imei = 0; imei = 0;
for(int i = 0; i < 15; i++) for(int i = 0; i < 15; i++)
{ {
@ -87,6 +90,7 @@ int pcsc_usim::init(usim_args_t *args, srslte::log *log_)
} else { } else {
log->error("Invalid length for IMEI: %zu should be %d\n", args->imei.length(), 15); log->error("Invalid length for IMEI: %zu should be %d\n", args->imei.length(), 15);
log->console("Invalid length for IMEI: %zu should be %d\n", args->imei.length(), 15); log->console("Invalid length for IMEI: %zu should be %d\n", args->imei.length(), 15);
return ret;
} }
// Get MNC length // Get MNC length
@ -94,8 +98,9 @@ int pcsc_usim::init(usim_args_t *args, srslte::log *log_)
log->debug("MNC length %d\n", mnc_length); log->debug("MNC length %d\n", mnc_length);
initiated = true; initiated = true;
ret = SRSLTE_SUCCESS;
return SRSLTE_SUCCESS; return ret;
} }
void pcsc_usim::stop() void pcsc_usim::stop()
@ -415,7 +420,7 @@ int pcsc_usim::scard::init(usim_args_t *args, srslte::log *log_)
ret = SCardListReaders(scard_context, NULL, readers, &len); ret = SCardListReaders(scard_context, NULL, readers, &len);
if (ret != SCARD_S_SUCCESS) { if (ret != SCARD_S_SUCCESS) {
log->error("%s\n", pcsc_stringify_error(ret)); log->error("SCardListReaders() 2: %s\n", pcsc_stringify_error(ret));
goto clean_exit; goto clean_exit;
} }
if (len < 3) { if (len < 3) {
@ -510,6 +515,12 @@ int pcsc_usim::scard::init(usim_args_t *args, srslte::log *log_)
pin1_needed = false; pin1_needed = false;
} }
// stop before pin retry counter reaches zero
if (pin1_needed && get_pin_retry_counter() <= 1) {
log->error("PIN1 needed for SIM access (retry counter=%d), emergency stop.\n", get_pin_retry_counter());
goto clean_exit;
}
// Set pin // Set pin
if (pin1_needed) { if (pin1_needed) {
// verify PIN // verify PIN

@ -53,9 +53,14 @@ int main(int argc, char **argv)
args.imei = "353490069873319"; args.imei = "353490069873319";
srsue::pcsc_usim usim; srsue::pcsc_usim usim;
usim.init(&args, &usim_log); if (usim.init(&args, &usim_log)) {
printf("Error initializing PC/SC USIM.\n");
return SRSLTE_ERROR;
};
std::string imsi = usim.get_imsi_str(); std::string imsi = usim.get_imsi_str();
cout << "IMSI: " << imsi << endl; cout << "IMSI: " << imsi << endl;
auth_result_t result = usim.generate_authentication_response(rand_enb, autn_enb, mcc, mnc, res, &res_len, k_asme); auth_result_t result = usim.generate_authentication_response(rand_enb, autn_enb, mcc, mnc, res, &res_len, k_asme);
return SRSLTE_SUCCESS;
} }

Loading…
Cancel
Save