Browse Source

Fix many python3 compatibility problems

It's 2020, and I'm actually rather shocked to find that this code
was not python3 compatbile a long time ago...
master
Harald Welte 1 year ago
parent
commit
76c74c93d5
  1. 4
      card/utils.py
  2. 8
      common.py
  3. 4
      simcard.py
  4. 56
      sysmo_isim_sja2.py
  5. 8
      sysmo_usim.py
  6. 8
      sysmo_usim_sjs1.py
  7. 2
      utils.py

4
card/utils.py

@ -217,9 +217,9 @@ def decode_BCD(data=[]):
# 2nd digit (4 MSB), can be padding (e.g. 0xF)
if (B>>4) < 10: string += str(B>>4)
if len(string) <= 0:
return None
return None
else:
return string
return string
def compute_luhn(digit_str=''):
'''

8
common.py

@ -67,7 +67,7 @@ class Common():
opts, args = getopt.getopt(argv, COMMON_GETOPTS + getopts,
COMMON_GETOPTS_LONG + getopts_long)
except getopt.GetoptError:
print " * Error: Invalid commandline options"
print(" * Error: Invalid commandline options")
sys.exit(2)
# Set flags for common options
@ -114,8 +114,8 @@ class Common():
# Check for ADM1 key
if not self.adm1:
print " * Error: adm1 parameter missing -- exiting..."
print ""
print(" * Error: adm1 parameter missing -- exiting...")
print("")
sys.exit(1)
# Set flags for specific options
@ -213,4 +213,4 @@ class Common():
if self.show_aid:
self.sim.show_aid()
print "Done!"
print("Done!")

4
simcard.py

@ -63,7 +63,7 @@ class Card_res_apdu():
# convert Benoit Michau style result to sysmocom style result
def from_mich(self, mich):
self.apdu = mich[3]
self.sw = [ mich[2][0], mich[2][1] ]
self.sw = [ mich[2][0], mich[2][1] ]
def __str__(self):
dump = ""
@ -204,4 +204,4 @@ class Simcard():
def update_record(self, data, rec_no = 0):
res = Card_res_apdu()
res.from_mich(self.card.UPDATE_RECORD(rec_no, GSM_SIM_INS_UPDATE_RECORD_ABS, data))
return res
return res

56
sysmo_isim_sja2.py

@ -347,7 +347,7 @@ class SYSMO_ISIMSJA2_FILE_EF_USIM_SQN:
# we automatically set the value to the correct length
ind_size_bits_calculated = int(math.log((len(content) - 14) / 6, 2))
if ind_size_bits_calculated != self.ind_size_bits:
print " Warning: SQN Parameter ind_size_bits is set to " + str(self.ind_size_bits) + ", resetting it to " + str(ind_size_bits_calculated) + "!"
print(" Warning: SQN Parameter ind_size_bits is set to " + str(self.ind_size_bits) + ", resetting it to " + str(ind_size_bits_calculated) + "!")
self.ind_size_bits = ind_size_bits_calculated
self.reset() #ensure freshness data is correctly reset
@ -452,11 +452,11 @@ class Sysmo_isim_sja2(Sysmo_usim):
# Try card model #1
try:
atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 30 34 05 4B A9"
print "Trying to find card with ATR: " + atr
print("Trying to find card with ATR: " + atr)
Sysmo_usim.__init__(self, atr)
card_detected = True
except:
print " * Card not detected!"
print(" * Card not detected!")
if card_detected == True:
return
@ -465,11 +465,11 @@ class Sysmo_isim_sja2(Sysmo_usim):
# Try card model #2
try:
atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 75 31 33 02 51 B2"
print "Trying to find card with ATR: " + atr
print("Trying to find card with ATR: " + atr)
Sysmo_usim.__init__(self, atr)
card_detected = True
except:
print " * Card not detected!"
print(" * Card not detected!")
if card_detected == True:
return
@ -477,11 +477,11 @@ class Sysmo_isim_sja2(Sysmo_usim):
# Try card model #3 (sysmoTSIM)
try:
atr = "3B 9F 96 80 1F 87 80 31 E0 73 FE 21 1B 67 4A 4C 52 75 31 04 51 D5"
print "Trying to find card with ATR: " + atr
print("Trying to find card with ATR: " + atr)
Sysmo_usim.__init__(self, atr)
card_detected = True
except:
print " * Card not detected!"
print(" * Card not detected!")
if card_detected == True:
return
@ -522,7 +522,7 @@ class Sysmo_isim_sja2(Sysmo_usim):
print(" * New Milenage Parameters for (EF.MILENAGE_CFG):")
ef_milenage_cfg = SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(params_swapped)
print str(ef_milenage_cfg)
print(str(ef_milenage_cfg))
print(" * Programming...")
# Note: The milenage configuration file in ADF_USIM and
@ -573,42 +573,42 @@ class Sysmo_isim_sja2(Sysmo_usim):
# DF_SYSTEM/EF_SIM_AUTH_KEY:
self.__select_ef_sim_auth_key()
res = self._read_binary(self.sim.filelen)
print " * DF_SYSTEM/EF_SIM_AUTH_KEY:"
print SYSMO_ISIMSJA2_FILE_EF_SIM_AUTH_KEY(res.apdu)
print(" * DF_SYSTEM/EF_SIM_AUTH_KEY:")
print(SYSMO_ISIMSJA2_FILE_EF_SIM_AUTH_KEY(res.apdu))
# ADF_USIM/EF_USIM_AUTH_KEY_2G:
self.__select_xsim_auth_key(isim = False, _2G = True)
res = self._read_binary(self.sim.filelen)
print " * ADF_USIM/EF_USIM_AUTH_KEY_2G:"
print SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY_2G(res.apdu)
print(" * ADF_USIM/EF_USIM_AUTH_KEY_2G:")
print(SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY_2G(res.apdu))
if self.sim.has_isim:
# ADF_ISIM/EF_ISIM_AUTH_KEY_2G:
self.__select_xsim_auth_key(isim = True, _2G = True)
res = self._read_binary(self.sim.filelen)
print " * ADF_ISIM/EF_ISIM_AUTH_KEY_2G:"
print SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY_2G(res.apdu)
print(" * ADF_ISIM/EF_ISIM_AUTH_KEY_2G:")
print(SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY_2G(res.apdu))
# ADF_USIM/EF_USIM_AUTH_KEY:
self.__select_xsim_auth_key(isim = False, _2G = False)
res = self._read_binary(self.sim.filelen)
print " * ADF_USIM/EF_USIM_AUTH_KEY:"
print SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY(res.apdu)
print(" * ADF_USIM/EF_USIM_AUTH_KEY:")
print(SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY(res.apdu))
if self.sim.has_isim:
# ADF_ISIM/EF_ISIM_AUTH_KEY:
self.__select_xsim_auth_key(isim = True, _2G = False)
res = self._read_binary(self.sim.filelen)
print " * ADF_ISIM/EF_ISIM_AUTH_KEY:"
print SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY(res.apdu)
print(" * ADF_ISIM/EF_ISIM_AUTH_KEY:")
print(SYSMO_ISIMSJA2_FILE_EF_USIM_AUTH_KEY(res.apdu))
# ADF_USIM/EF_MILENAGE_CFG:
self.sim.select(GSM_SIM_MF)
self.sim.card.SELECT_ADF_USIM()
self.sim.select(SYSMO_ISIMSJA2_EF_MILENAGE_CFG)
res = self._read_binary(self.sim.filelen)
print " * ADF_USIM/EF_MILENAGE_CFG:"
print SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(res.apdu)
print(" * ADF_USIM/EF_MILENAGE_CFG:")
print(SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(res.apdu))
if self.sim.has_isim:
# ADF_ISIM/EF_MILENAGE_CFG:
@ -616,16 +616,16 @@ class Sysmo_isim_sja2(Sysmo_usim):
self.sim.card.SELECT_ADF_ISIM()
self.sim.select(SYSMO_ISIMSJA2_EF_MILENAGE_CFG)
res = self._read_binary(self.sim.filelen)
print " * ADF_ISIM/EF_MILENAGE_CFG:"
print SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(res.apdu)
print(" * ADF_ISIM/EF_MILENAGE_CFG:")
print(SYSMO_ISIMSJA2_FILE_EF_MILENAGE_CFG(res.apdu))
# ADF_USIM/EF_USIM_SQN:
self.sim.select(GSM_SIM_MF)
self.sim.card.SELECT_ADF_USIM()
self.sim.select(SYSMO_ISIMSJA2_EF_USIM_SQN)
res = self._read_binary(self.sim.filelen)
print " * ADF_USIM/EF_USIM_SQN:"
print SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu)
print(" * ADF_USIM/EF_USIM_SQN:")
print(SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu))
if self.sim.has_isim:
# ADF_USIM/EF_ISIM_SQN:
@ -633,8 +633,8 @@ class Sysmo_isim_sja2(Sysmo_usim):
self.sim.card.SELECT_ADF_ISIM()
self.sim.select(SYSMO_ISIMSJA2_EF_USIM_SQN)
res = self._read_binary(self.sim.filelen)
print " * ADF_ISIM/EF_ISIM_SQN:"
print SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu)
print(" * ADF_ISIM/EF_ISIM_SQN:")
print(SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu))
# Show current KI value
@ -822,7 +822,7 @@ class Sysmo_isim_sja2(Sysmo_usim):
self.sim.card.SELECT_ADF_USIM()
self.sim.select(SYSMO_ISIMSJA2_EF_USIM_SQN)
res = self._read_binary(self.sim.filelen)
print SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu)
print(SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu))
if self.sim.has_isim:
print(" * Current SQN Configuration for ADF_ISIM:")
@ -830,7 +830,7 @@ class Sysmo_isim_sja2(Sysmo_usim):
self.sim.card.SELECT_ADF_ISIM()
self.sim.select(SYSMO_ISIMSJA2_EF_USIM_SQN)
res = self._read_binary(self.sim.filelen)
print SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu)
print(SYSMO_ISIMSJA2_FILE_EF_USIM_SQN(res.apdu))
print("")

8
sysmo_usim.py

@ -60,7 +60,7 @@ class Sysmo_usim:
# Initalize card (select master file)
def _init(self):
print " * Initalizing..."
print(" * Initalizing...")
self.sim.select(GSM_SIM_MF)
@ -204,12 +204,12 @@ class Sysmo_usim:
self._init()
self.sim.card.get_AID()
AID = self.sim.card.AID
for a in AID:
for a in AID:
if a[0:7] == [0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x02]:
appstr = "USIM"
elif a[0:7] == [0xA0, 0x00, 0x00, 0x00, 0x87, 0x10, 0x04]:
appstr = "ISIM"
else:
appstr = "(unknown)"
print " AID: " + hexdump(a[0:5]) + " " + hexdump(a[5:7]) + " " + hexdump(a[7:]) + " ==> " + appstr
print ""
print(" AID: " + hexdump(a[0:5]) + " " + hexdump(a[5:7]) + " " + hexdump(a[7:]) + " ==> " + appstr)
print("")

8
sysmo_usim_sjs1.py

@ -340,7 +340,7 @@ class Sysmo_usim_sjs1(Sysmo_usim):
ef_mlngc = SYSMO_USIMSJS1_FILE_EF_MLNGC(res.apdu)
print(" * Current Milenage Parameters in (EF.MLNGC):")
print str(ef_mlngc)
print(str(ef_mlngc))
print("")
@ -351,7 +351,7 @@ class Sysmo_usim_sjs1(Sysmo_usim):
print(" * New Milenage Parameters for (EF.MLNGC):")
ef_mlngc = SYSMO_USIMSJS1_FILE_EF_MLNGC(params)
print str(ef_mlngc)
print(str(ef_mlngc))
self.sim.select(SYSMO_USIMSJS1_DF_AUTH)
self.sim.select(SYSMO_USIMSJS1_EF_MLNGC)
@ -400,7 +400,7 @@ class Sysmo_usim_sjs1(Sysmo_usim):
res = self._read_binary(15, offset = 0)
ef_sqnc = SYSMO_USIMSJS1_FILE_EF_SQNC(res.apdu)
print(" * Current SQN Configuration:")
print str(ef_sqnc)
print(str(ef_sqnc))
# SQN Array
ind_pow = 2**ef_sqnc.ind_size_bits
@ -408,7 +408,7 @@ class Sysmo_usim_sjs1(Sysmo_usim):
res = self._read_binary(ind_pow*6, offset=0)
ef_sqna = SYSMO_USIMSJS1_FILE_EF_SQNA(res.apdu)
print(" * Current SQN Array:")
print str(ef_sqna)
print(str(ef_sqna))
auth_ctr = self.__get_auth_counter()
print("* Authentication Counter: %s" % auth_ctr)

2
utils.py

@ -56,7 +56,7 @@ def asciihex_to_list(string):
try:
return map(ord, string.decode("hex"))
except:
print "Warning: Invalid hex string -- ignored!"
print("Warning: Invalid hex string -- ignored!")
return []

Loading…
Cancel
Save