From 83760e2c2272b8deb570cf02ea1f9c5e1b7802b2 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Thu, 21 Mar 2019 17:05:42 +0100 Subject: [PATCH] Support programming of the MNC length value (EF.AD) The file EF.AD contains the length of the MNC, this allows to phone to distinguish where MCC/MNC end and where the MSIN number of the IMSI begins. All cards are shipped with an MNC length of 2, which is fine for the most situations, but when a MNC with 3 digits is needed the MNC length value has to be updated. Otherwise the phone will recognize the third digit of the MNC as part of the MSIN. Related: OS#3850 --- simcard.py | 1 + sysmo-usim-tool.sjs1.py | 22 ++++++- sysmo_usimsjs1.py | 33 ++++++++++ tests/09_mnclen.out | 132 ++++++++++++++++++++++++++++++++++++++++ tests/09_mnclen.sh | 15 +++++ tests/run-tests | 2 +- 6 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 tests/09_mnclen.out create mode 100755 tests/09_mnclen.sh diff --git a/simcard.py b/simcard.py index a1adb73..d1c6298 100644 --- a/simcard.py +++ b/simcard.py @@ -33,6 +33,7 @@ GSM_SIM_DF_TELECOM = [0x7F, 0x10] GSM_SIM_DF_GSM = [0x7F, 0x20] GSM_SIM_EF_ADN = [0x6f,0x3A] GSM_SIM_EF_IMSI = [0x6F, 0x07] +GSM_SIM_EF_AD = [0x6f, 0xAD] GSM_SIM_EF_ICCID = [0x2F, 0xE2] GSM_USIM_EF_DIR = [0x2F, 0x00] # See also: 3GPP TS 31.102 Table 105 diff --git a/sysmo-usim-tool.sjs1.py b/sysmo-usim-tool.sjs1.py index 16d44f5..9c0a2fa 100755 --- a/sysmo-usim-tool.sjs1.py +++ b/sysmo-usim-tool.sjs1.py @@ -55,6 +55,8 @@ def helptext(): print " -S --reset-seq-parameters...... Reset MILENAGE SEQ/SQN parameters to default" print " -I, --set-iccid ................ Set ICCID value" print " -J, --set-imsi ................. Set IMSI value" + print " -n, --mnclen ................... Show MNC length value" + print " -N, --set-mnclen ............... Set MNC length value" print "" @@ -78,17 +80,19 @@ def main(argv): getopt_seq_par = False getopt_reset_seq_par = False getopt_write_imsi = None + getopt_show_mnclen = None + getopt_write_mnclen = None # Analyze commandline options try: opts, args = getopt.getopt(argv, - "ha:ucmtT:lL:oO:C:kK:fiI:sSJ:", + "ha:ucmtT:lL:oO:C:kK:fiI:sSJ:nN:", ["help","adm1=","usim","classic", "mode","auth","set-auth=","milenage", "set-milenage","opc","set-op=","set-opc=", "ki","set-ki=","force","iccid","set-iccid=", "seq-parameters", "reset-seq-parameters", - "set-imsi"]) + "set-imsi", "set-mnclen", "mnclen"]) except getopt.GetoptError: print " * Error: Invalid commandline options" sys.exit(2) @@ -138,6 +142,10 @@ def main(argv): getopt_reset_seq_par = True elif opt in ("-J", "--set-imsi"): getopt_write_imsi = asciihex_to_list(pad_asciihex(arg, True, '9')) + elif opt in ("-n", "--mnclen"): + getopt_show_mnclen = True + elif opt in ("-N", "--set-mnclen"): + getopt_write_mnclen = asciihex_to_list(arg) if not getopt_adm1: @@ -244,6 +252,16 @@ def main(argv): sysmo_usim_write_imsi(sim, getopt_write_imsi) print("") + if getopt_show_mnclen: + print "Reading MNCLEN value..." + sysmo_usim_show_mnclen(sim) + print("") + + if getopt_write_mnclen: + print "Writing MNCLEN value..." + sysmo_usim_write_mnclen(sim, getopt_write_mnclen) + print("") + print "Done!" diff --git a/sysmo_usimsjs1.py b/sysmo_usimsjs1.py index e85bdf0..03bec90 100644 --- a/sysmo_usimsjs1.py +++ b/sysmo_usimsjs1.py @@ -591,3 +591,36 @@ def sysmo_usim_write_imsi(sim, imsi): print " * Programming..." sim.update_binary(imsi) + + +# Show current KI value +def sysmo_usim_show_mnclen(sim): + sysmo_usim_init(sim) + + print " * Reading..." + sim.select(GSM_SIM_DF_GSM) + sim.select(GSM_SIM_EF_AD) + res = sim.read_binary(4) + + print " * Current MNCLEN setting:" + print " MNCLEN: " + "0x%02x" % res.apdu[3] + + +# Program new MNCLEN value +def sysmo_usim_write_mnclen(sim, mnclen): + print " * New MNCLEN setting:" + print " MNCLEN: " + "0x" + hexdump(mnclen) + + if len(mnclen) != 1: + print " * Error: mnclen value must consist of a single byte!" + return + + sysmo_usim_init(sim) + sim.select(GSM_SIM_DF_GSM) + sim.select(GSM_SIM_EF_AD) + + res = sim.read_binary(4) + new_ad = res.apdu[0:3] + mnclen + + print " * Programming..." + sim.update_binary(new_ad) diff --git a/tests/09_mnclen.out b/tests/09_mnclen.out new file mode 100644 index 0000000..2bf371e --- /dev/null +++ b/tests/09_mnclen.out @@ -0,0 +1,132 @@ +sysmoUSIM-SJS1 parameterization tool +Copyright (c)2017 Sysmocom s.f.m.c. GmbH + +Initializing smartcard terminal... + +Detected Card ICCID: 8988211320300000028 + +Authenticating... + * Remaining attempts: 3 + * Authenticating... + * Authentication successful + * Remaining attempts: 3 + +Detected Card IMSI: 262423203000002 + +Writing MNCLEN value... + * New MNCLEN setting: + MNCLEN: 0x02 + * Initalizing... + * Programming... + +Done! +sysmoUSIM-SJS1 parameterization tool +Copyright (c)2017 Sysmocom s.f.m.c. GmbH + +Initializing smartcard terminal... + +Detected Card ICCID: 8988211320300000028 + +Authenticating... + * Remaining attempts: 3 + * Authenticating... + * Authentication successful + * Remaining attempts: 3 + +Detected Card IMSI: 262423203000002 + +Reading MNCLEN value... + * Initalizing... + * Reading... + * Current MNCLEN setting: + MNCLEN: 0x02 + +Done! +sysmoUSIM-SJS1 parameterization tool +Copyright (c)2017 Sysmocom s.f.m.c. GmbH + +Initializing smartcard terminal... + +Detected Card ICCID: 8988211320300000028 + +Authenticating... + * Remaining attempts: 3 + * Authenticating... + * Authentication successful + * Remaining attempts: 3 + +Detected Card IMSI: 262423203000002 + +Writing MNCLEN value... + * New MNCLEN setting: + MNCLEN: 0x03 + * Initalizing... + * Programming... + +Done! +sysmoUSIM-SJS1 parameterization tool +Copyright (c)2017 Sysmocom s.f.m.c. GmbH + +Initializing smartcard terminal... + +Detected Card ICCID: 8988211320300000028 + +Authenticating... + * Remaining attempts: 3 + * Authenticating... + * Authentication successful + * Remaining attempts: 3 + +Detected Card IMSI: 262423203000002 + +Reading MNCLEN value... + * Initalizing... + * Reading... + * Current MNCLEN setting: + MNCLEN: 0x03 + +Done! +sysmoUSIM-SJS1 parameterization tool +Copyright (c)2017 Sysmocom s.f.m.c. GmbH + +Initializing smartcard terminal... + +Detected Card ICCID: 8988211320300000028 + +Authenticating... + * Remaining attempts: 3 + * Authenticating... + * Authentication successful + * Remaining attempts: 3 + +Detected Card IMSI: 262423203000002 + +Writing MNCLEN value... + * New MNCLEN setting: + MNCLEN: 0x02 + * Initalizing... + * Programming... + +Done! +sysmoUSIM-SJS1 parameterization tool +Copyright (c)2017 Sysmocom s.f.m.c. GmbH + +Initializing smartcard terminal... + +Detected Card ICCID: 8988211320300000028 + +Authenticating... + * Remaining attempts: 3 + * Authenticating... + * Authentication successful + * Remaining attempts: 3 + +Detected Card IMSI: 262423203000002 + +Reading MNCLEN value... + * Initalizing... + * Reading... + * Current MNCLEN setting: + MNCLEN: 0x02 + +Done! diff --git a/tests/09_mnclen.sh b/tests/09_mnclen.sh new file mode 100755 index 0000000..19580d7 --- /dev/null +++ b/tests/09_mnclen.sh @@ -0,0 +1,15 @@ +#!/bin/sh +. ./test-data + +# set to 2 (default) + read back +$TOOL -a $ADMPIN -N 02 +$TOOL -a $ADMPIN -n + +# set to 3 + read back +$TOOL -a $ADMPIN -N 03 +$TOOL -a $ADMPIN -n + +# set to 2 (default) + read back +$TOOL -a $ADMPIN -N 02 +$TOOL -a $ADMPIN -n + diff --git a/tests/run-tests b/tests/run-tests index 1078b87..d424093 100755 --- a/tests/run-tests +++ b/tests/run-tests @@ -1,7 +1,7 @@ #!/bin/sh # default: execute all tests -TESTS="01_auth.sh 02_mode_read.sh 03_mode_write.sh 04_algo.sh 05_milenage_par.sh 06_op_opc.sh 07_ki.sh 08_seq.sh" +TESTS="01_auth.sh 02_mode_read.sh 03_mode_write.sh 04_algo.sh 05_milenage_par.sh 06_op_opc.sh 07_ki.sh 08_seq.sh 09_mnclen.sh" # if command line specifies some specific tests, execute only those if [ $# -ge 1 ]; then