Tool to (re)configure the sysmoUSIM and sysmoISIM cards
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.3 KiB

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. User interface parts that are common for all tools
  5. (C) 2017 by Sysmocom s.f.m.c. GmbH
  6. All Rights Reserved
  7. Author: Philipp Maier
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. """
  19. from utils import *
  20. import sys, getopt
  21. COMMON_GETOPTS = "hfa:J:nN:lL:kK:tT:oO:C:sSip"
  22. COMMON_GETOPTS_LONG = ["help", "force", "adm1=", "set-imsi", "mnclen",
  23. "set-mnclen", "milenage", "set-milenage", "ki",
  24. "set-ki=", "auth", "set-auth=", "opc", "set-op=",
  25. "set-opc=", "seq-parameters", "reset-seq-parameters"
  26. "iccid", "aid"]
  27. # Parse common commandline options and keep them as flags
  28. class Common():
  29. sim = None
  30. show_helptext = None
  31. force = False
  32. adm1 = None
  33. write_imsi = None
  34. write_mnclen = None
  35. show_mnclen = None
  36. show_milenage = False
  37. write_milenage = None
  38. show_ki = None
  39. write_ki = None
  40. show_auth = False
  41. write_auth = None
  42. show_opc = False
  43. write_op = None
  44. write_opc = None
  45. show_seq_par = False
  46. reset_seq_par = False
  47. show_iccid = False
  48. show_aid = False
  49. def __init__(self, argv, getopts, getopts_long):
  50. self._banner()
  51. # Analyze commandline options
  52. try:
  53. opts, args = getopt.getopt(argv, COMMON_GETOPTS + getopts,
  54. COMMON_GETOPTS_LONG + getopts_long)
  55. except getopt.GetoptError:
  56. print(" * Error: Invalid commandline options")
  57. sys.exit(2)
  58. # Set flags for common options
  59. for opt, arg in opts:
  60. if opt in ("-h", "--help"):
  61. self.__common_helptext()
  62. sys.exit(0)
  63. elif opt in ("-f", "--force"):
  64. self.force = True
  65. elif opt in ("-a", "--adm1"):
  66. self.adm1 = ascii_to_list(arg)
  67. elif opt in ("-J", "--set-imsi"):
  68. self.write_imsi = asciihex_to_list(pad_asciihex(arg, True, '9'))
  69. elif opt in ("-n", "--mnclen"):
  70. self.show_mnclen = True
  71. elif opt in ("-N", "--set-mnclen"):
  72. self.write_mnclen = asciihex_to_list(arg)
  73. elif opt in ("-l", "--milenage"):
  74. self.show_milenage = True
  75. elif opt in ("-L", "--set-milenage"):
  76. self.write_milenage = asciihex_to_list(arg)
  77. elif opt in ("-k", "--ki"):
  78. self.show_ki = True
  79. elif opt in ("-K", "--set-ki"):
  80. self.write_ki = asciihex_to_list(arg)
  81. elif opt in ("-t", "--auth"):
  82. self.show_auth = True
  83. elif opt in ("-T", "--set-auth"):
  84. self.write_auth = arg.split(':',1)
  85. elif opt in ("-o", "--opc"):
  86. self.show_opc = True
  87. elif opt in ("-O", "--set-op"):
  88. self.write_op = asciihex_to_list(arg)
  89. elif opt in ("-C", "--set-opc"):
  90. self.write_opc = asciihex_to_list(arg)
  91. elif opt in ("-s", "--sqe-parameters"):
  92. self.show_seq_par = True
  93. elif opt in ("-S", "--reset-sqe-parameters"):
  94. self.reset_seq_par = True
  95. elif opt in ("-i", "--iccid"):
  96. self.show_iccid = True
  97. elif opt in ("-p", "--aid"):
  98. self.show_aid = True
  99. # Check for ADM1 key
  100. if not self.adm1:
  101. print(" * Error: adm1 parameter missing -- exiting...")
  102. print("")
  103. sys.exit(1)
  104. # Set flags for specific options
  105. self._options(opts)
  106. # Initialize
  107. self._init()
  108. # Execute tasks
  109. self.__common_execute()
  110. # Print the part of the helptext that is common for all tools
  111. def __common_helptext(self):
  112. print(" * Commandline options:")
  113. print(" -h, --help ..................... Show this screen")
  114. print(" -f, --force .................... Enforce authentication after failure")
  115. print(" -a, --adm1 CHV ................. Administrator PIN (e.g 55538407)")
  116. print(" -J, --set-imsi ................. Set IMSI value")
  117. print(" -n, --mnclen ................... Show MNC length value")
  118. print(" -N, --set-mnclen ............... Set MNC length value")
  119. print(" -l, --milenage ................. Show milenage parameters")
  120. print(" -L, --set-milenage HEXSTRING ... Set milenage parameters")
  121. print(" -k, --ki ....................... Show KI value")
  122. print(" -K, --set-ki ................... Set KI value")
  123. print(" -t, --auth ..................... Show Authentication algorithms")
  124. print(" -T, --set-auth 2G:3G ........... Set 2G/3G Auth algo (e.g. COMP128v1:COMP128v1)")
  125. print(" -o, --opc ...................... Show OP/c configuration")
  126. print(" -O, --set-op HEXSTRING ......... Set OP value")
  127. print(" -C, --set-opc HEXSTRING ........ Set OPc value")
  128. print(" -s --seq-parameters ........... Show MILENAGE SEQ/SQN parameters")
  129. print(" -S --reset-seq-parameters ..... Reset MILENAGE SEQ/SQN parameters to default")
  130. print(" -i --iccid .................... Show ICCID")
  131. print(" -p --aid ...................... Show AID list (installed applications)")
  132. self._helptext()
  133. # Execute common tasks
  134. def __common_execute(self):
  135. # Autnetnication is a primary task that must always run before
  136. # any other task is carried out
  137. if self.sim.admin_auth(self.adm1, self.force) == False:
  138. exit(1)
  139. # First run the card specific tasks
  140. self._execute()
  141. # And then the common tasks
  142. if self.write_imsi:
  143. self.sim.write_imsi(self.write_imsi)
  144. if self.show_mnclen:
  145. self.sim.show_mnclen()
  146. if self.write_mnclen:
  147. self.sim.write_mnclen(self.write_mnclen)
  148. if self.write_milenage:
  149. self.sim.write_milenage_params(self.write_milenage)
  150. if self.show_milenage:
  151. self.sim.show_milenage_params()
  152. if self.write_ki:
  153. self.sim.write_ki_params(self.write_ki)
  154. if self.show_ki:
  155. self.sim.show_ki_params()
  156. if self.show_auth:
  157. self.sim.show_auth_params()
  158. if self.write_auth:
  159. self.sim.write_auth_params(self.write_auth[0], self.write_auth[1])
  160. if self.show_opc:
  161. self.sim.show_opc_params()
  162. if self.write_op:
  163. self.sim.write_opc_params(0, self.write_op)
  164. if self.write_opc:
  165. self.sim.write_opc_params(1, self.write_opc)
  166. if self.show_seq_par:
  167. self.sim.show_milenage_sqn_params()
  168. if self.reset_seq_par:
  169. self.sim.reset_milenage_sqn_params()
  170. if self.show_iccid:
  171. self.sim.show_iccid()
  172. if self.show_aid:
  173. self.sim.show_aid()
  174. print("Done!")