Daniel Willmann | de07b95 | 2020-10-19 10:32:34 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 2 | |
| 3 | # |
| 4 | # Utility to deal with sim cards and program the 'magic' ones easily |
| 5 | # |
| 6 | # |
| 7 | # Part of the sim link code of inspired by pySimReader-Serial-src-v2 |
| 8 | # |
| 9 | # |
| 10 | # Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com> |
| 11 | # Copyright (C) 2010 Harald Welte <laforge@gnumonks.org> |
| 12 | # |
| 13 | # This program is free software: you can redistribute it and/or modify |
| 14 | # it under the terms of the GNU General Public License as published by |
| 15 | # the Free Software Foundation, either version 2 of the License, or |
| 16 | # (at your option) any later version. |
| 17 | # |
| 18 | # This program is distributed in the hope that it will be useful, |
| 19 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | # GNU General Public License for more details. |
| 22 | # |
| 23 | # You should have received a copy of the GNU General Public License |
| 24 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | # |
| 26 | |
| 27 | import hashlib |
| 28 | from optparse import OptionParser |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 29 | import os |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 30 | import random |
| 31 | import re |
| 32 | import sys |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 33 | import traceback |
Denis 'GNUtoo' Carikli | 79f5b60 | 2020-02-15 04:02:57 +0700 | [diff] [blame] | 34 | import json |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 35 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 36 | from pySim.commands import SimCardCommands |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 37 | from pySim.transport import init_reader |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 38 | from pySim.cards import _cards_classes, card_detect |
Harald Welte | 6e0458d | 2021-04-03 11:52:37 +0200 | [diff] [blame] | 39 | from pySim.utils import h2b, swap_nibbles, rpad, derive_milenage_opc, calculate_luhn, dec_iccid |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 40 | from pySim.ts_51_011 import EF, EF_AD |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 41 | from pySim.card_handler import * |
Philipp Maier | 7592eee | 2019-09-12 13:03:23 +0200 | [diff] [blame] | 42 | from pySim.utils import * |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 43 | |
| 44 | def parse_options(): |
| 45 | |
| 46 | parser = OptionParser(usage="usage: %prog [options]") |
| 47 | |
| 48 | parser.add_option("-d", "--device", dest="device", metavar="DEV", |
| 49 | help="Serial Device for SIM access [default: %default]", |
| 50 | default="/dev/ttyUSB0", |
| 51 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 52 | parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD", |
| 53 | help="Baudrate used for SIM access [default: %default]", |
| 54 | default=9600, |
| 55 | ) |
Sylvain Munaut | 9c8729a | 2010-12-08 23:20:27 +0100 | [diff] [blame] | 56 | parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC", |
Sylvain Munaut | e9fdecb | 2010-12-08 22:33:19 +0100 | [diff] [blame] | 57 | help="Which PC/SC reader number for SIM access", |
| 58 | default=None, |
| 59 | ) |
Vadim Yanitskiy | 29ca804 | 2020-05-09 21:23:37 +0700 | [diff] [blame] | 60 | parser.add_option("--modem-device", dest="modem_dev", metavar="DEV", |
| 61 | help="Serial port of modem for Generic SIM Access (3GPP TS 27.007)", |
| 62 | default=None, |
| 63 | ) |
| 64 | parser.add_option("--modem-baud", dest="modem_baud", type="int", metavar="BAUD", |
| 65 | help="Baudrate used for modem's port [default: %default]", |
| 66 | default=115200, |
| 67 | ) |
Vadim Yanitskiy | 9f9f5a6 | 2018-10-27 02:10:34 +0700 | [diff] [blame] | 68 | parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH", |
| 69 | help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)", |
| 70 | default=None, |
| 71 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 72 | parser.add_option("-t", "--type", dest="type", |
| 73 | help="Card type (user -t list to view) [default: %default]", |
| 74 | default="auto", |
| 75 | ) |
Philipp Maier | ac9dde6 | 2018-07-04 11:05:14 +0200 | [diff] [blame] | 76 | parser.add_option("-T", "--probe", dest="probe", |
| 77 | help="Determine card type", |
| 78 | default=False, action="store_true" |
| 79 | ) |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 80 | parser.add_option("-a", "--pin-adm", dest="pin_adm", |
| 81 | help="ADM PIN used for provisioning (overwrites default)", |
| 82 | ) |
Daniel Willmann | f432b2b | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 83 | parser.add_option("-A", "--pin-adm-hex", dest="pin_adm_hex", |
| 84 | help="ADM PIN used for provisioning, as hex string (16 characters long", |
| 85 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 86 | parser.add_option("-e", "--erase", dest="erase", action='store_true', |
| 87 | help="Erase beforehand [default: %default]", |
| 88 | default=False, |
| 89 | ) |
| 90 | |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 91 | parser.add_option("-S", "--source", dest="source", |
| 92 | help="Data Source[default: %default]", |
| 93 | default="cmdline", |
| 94 | ) |
| 95 | |
| 96 | # if mode is "cmdline" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 97 | parser.add_option("-n", "--name", dest="name", |
| 98 | help="Operator name [default: %default]", |
| 99 | default="Magic", |
| 100 | ) |
| 101 | parser.add_option("-c", "--country", dest="country", type="int", metavar="CC", |
| 102 | help="Country code [default: %default]", |
| 103 | default=1, |
| 104 | ) |
Harald Welte | 7f1d3c4 | 2020-05-12 21:12:44 +0200 | [diff] [blame] | 105 | parser.add_option("-x", "--mcc", dest="mcc", type="string", |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 106 | help="Mobile Country Code [default: %default]", |
Harald Welte | 7f1d3c4 | 2020-05-12 21:12:44 +0200 | [diff] [blame] | 107 | default="901", |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 108 | ) |
Harald Welte | 7f1d3c4 | 2020-05-12 21:12:44 +0200 | [diff] [blame] | 109 | parser.add_option("-y", "--mnc", dest="mnc", type="string", |
Sylvain Munaut | 1771603 | 2010-12-08 22:33:51 +0100 | [diff] [blame] | 110 | help="Mobile Network Code [default: %default]", |
Harald Welte | 7f1d3c4 | 2020-05-12 21:12:44 +0200 | [diff] [blame] | 111 | default="55", |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 112 | ) |
Supreeth Herle | fc83e43 | 2020-05-06 11:48:46 +0200 | [diff] [blame] | 113 | parser.add_option("--mnclen", dest="mnclen", type="choice", |
| 114 | help="Length of Mobile Network Code [default: %default]", |
| 115 | default=2, |
| 116 | choices=[2, 3], |
| 117 | ) |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 118 | parser.add_option("-m", "--smsc", dest="smsc", |
Daniel Willmann | 4fa8f1c | 2018-10-02 18:10:21 +0200 | [diff] [blame] | 119 | help="SMSC number (Start with + for international no.) [default: '00 + country code + 5555']", |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 120 | ) |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 121 | parser.add_option("-M", "--smsp", dest="smsp", |
| 122 | help="Raw SMSP content in hex [default: auto from SMSC]", |
| 123 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 124 | |
| 125 | parser.add_option("-s", "--iccid", dest="iccid", metavar="ID", |
| 126 | help="Integrated Circuit Card ID", |
| 127 | ) |
| 128 | parser.add_option("-i", "--imsi", dest="imsi", |
| 129 | help="International Mobile Subscriber Identity", |
| 130 | ) |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 131 | parser.add_option("--msisdn", dest="msisdn", |
| 132 | help="Mobile Subscriber Integrated Services Digital Number", |
| 133 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 134 | parser.add_option("-k", "--ki", dest="ki", |
| 135 | help="Ki (default is to randomize)", |
| 136 | ) |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 137 | parser.add_option("-o", "--opc", dest="opc", |
| 138 | help="OPC (default is to randomize)", |
| 139 | ) |
Holger Hans Peter Freyther | cca4179 | 2012-03-22 15:23:14 +0100 | [diff] [blame] | 140 | parser.add_option("--op", dest="op", |
| 141 | help="Set OP to derive OPC from OP and KI", |
| 142 | ) |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 143 | parser.add_option("--acc", dest="acc", |
| 144 | help="Set ACC bits (Access Control Code). not all card types are supported", |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 145 | ) |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 146 | parser.add_option("--opmode", dest="opmode", type="choice", |
| 147 | help="Set UE Operation Mode in EF.AD (Administrative Data)", |
| 148 | default=None, |
| 149 | choices=['{:02X}'.format(m) for m in list(EF_AD.OP_MODE.keys())], |
| 150 | ) |
Supreeth Herle | 8e0fccd | 2020-03-23 12:10:56 +0100 | [diff] [blame] | 151 | parser.add_option("--epdgid", dest="epdgid", |
| 152 | help="Set Home Evolved Packet Data Gateway (ePDG) Identifier. (Only FQDN format supported)", |
| 153 | ) |
Supreeth Herle | f964df4 | 2020-03-24 13:15:37 +0100 | [diff] [blame] | 154 | parser.add_option("--epdgSelection", dest="epdgSelection", |
| 155 | help="Set PLMN for ePDG Selection Information. (Only Operator Identifier FQDN format supported)", |
| 156 | ) |
Supreeth Herle | cf727f2 | 2020-03-24 17:32:21 +0100 | [diff] [blame] | 157 | parser.add_option("--pcscf", dest="pcscf", |
| 158 | help="Set Proxy Call Session Control Function (P-CSCF) Address. (Only FQDN format supported)", |
| 159 | ) |
Supreeth Herle | 79f43dd | 2020-03-25 11:43:19 +0100 | [diff] [blame] | 160 | parser.add_option("--ims-hdomain", dest="ims_hdomain", |
| 161 | help="Set IMS Home Network Domain Name in FQDN format", |
| 162 | ) |
Supreeth Herle | a5bd968 | 2020-03-26 09:16:14 +0100 | [diff] [blame] | 163 | parser.add_option("--impi", dest="impi", |
| 164 | help="Set IMS private user identity", |
| 165 | ) |
Supreeth Herle | be7007e | 2020-03-26 09:27:45 +0100 | [diff] [blame] | 166 | parser.add_option("--impu", dest="impu", |
| 167 | help="Set IMS public user identity", |
| 168 | ) |
Holger Hans Peter Freyther | 4e82468 | 2012-08-15 15:56:05 +0200 | [diff] [blame] | 169 | parser.add_option("--read-imsi", dest="read_imsi", action="store_true", |
| 170 | help="Read the IMSI from the CARD", default=False |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 171 | ) |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 172 | parser.add_option("--read-iccid", dest="read_iccid", action="store_true", |
| 173 | help="Read the ICCID from the CARD", default=False |
| 174 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 175 | parser.add_option("-z", "--secret", dest="secret", metavar="STR", |
| 176 | help="Secret used for ICCID/IMSI autogen", |
| 177 | ) |
| 178 | parser.add_option("-j", "--num", dest="num", type=int, |
| 179 | help="Card # used for ICCID/IMSI autogen", |
| 180 | ) |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 181 | parser.add_option("--batch", dest="batch_mode", |
| 182 | help="Enable batch mode [default: %default]", |
| 183 | default=False, action='store_true', |
| 184 | ) |
| 185 | parser.add_option("--batch-state", dest="batch_state", metavar="FILE", |
| 186 | help="Optional batch state file", |
| 187 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 188 | |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 189 | # if mode is "csv" |
| 190 | parser.add_option("--read-csv", dest="read_csv", metavar="FILE", |
| 191 | help="Read parameters from CSV file rather than command line") |
| 192 | |
| 193 | |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 194 | parser.add_option("--write-csv", dest="write_csv", metavar="FILE", |
| 195 | help="Append generated parameters in CSV file", |
| 196 | ) |
| 197 | parser.add_option("--write-hlr", dest="write_hlr", metavar="FILE", |
| 198 | help="Append generated parameters to OpenBSC HLR sqlite3", |
| 199 | ) |
Harald Welte | e9e5ecb | 2012-08-15 15:26:30 +0200 | [diff] [blame] | 200 | parser.add_option("--dry-run", dest="dry_run", |
| 201 | help="Perform a 'dry run', don't actually program the card", |
| 202 | default=False, action="store_true") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 203 | parser.add_option("--card_handler", dest="card_handler", metavar="FILE", |
| 204 | help="Use automatic card handling machine") |
| 205 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 206 | (options, args) = parser.parse_args() |
| 207 | |
| 208 | if options.type == 'list': |
| 209 | for kls in _cards_classes: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 210 | print(kls.name) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 211 | sys.exit(0) |
| 212 | |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 213 | if options.probe: |
| 214 | return options |
Philipp Maier | ac9dde6 | 2018-07-04 11:05:14 +0200 | [diff] [blame] | 215 | |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 216 | if options.source == 'csv': |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 217 | if (options.imsi is None) and (options.batch_mode is False) and (options.read_imsi is False) and (options.read_iccid is False): |
| 218 | parser.error("CSV mode needs either an IMSI, --read-imsi, --read-iccid or batch mode") |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 219 | if options.read_csv is None: |
| 220 | parser.error("CSV mode requires a CSV input file") |
| 221 | elif options.source == 'cmdline': |
| 222 | if ((options.imsi is None) or (options.iccid is None)) and (options.num is None): |
| 223 | parser.error("If either IMSI or ICCID isn't specified, num is required") |
| 224 | else: |
| 225 | parser.error("Only `cmdline' and `csv' sources supported") |
| 226 | |
| 227 | if (options.read_csv is not None) and (options.source != 'csv'): |
| 228 | parser.error("You cannot specify a CSV input file in source != csv") |
| 229 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 230 | if (options.batch_mode) and (options.num is None): |
| 231 | options.num = 0 |
| 232 | |
Sylvain Munaut | 98d2b85 | 2010-12-23 20:27:25 +0100 | [diff] [blame] | 233 | if (options.batch_mode): |
| 234 | if (options.imsi is not None) or (options.iccid is not None): |
| 235 | parser.error("Can't give ICCID/IMSI for batch mode, need to use automatic parameters ! see --num and --secret for more informations") |
| 236 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 237 | if args: |
| 238 | parser.error("Extraneous arguments") |
| 239 | |
| 240 | return options |
| 241 | |
| 242 | |
| 243 | def _digits(secret, usage, len, num): |
Jeremy Herbert | 3b00dbf | 2020-10-25 20:56:05 +1000 | [diff] [blame] | 244 | seed = secret + usage + '%d' % num |
| 245 | s = hashlib.sha1(seed.encode()) |
| 246 | d = ''.join(['%02d' % x for x in s.digest()]) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 247 | return d[0:len] |
| 248 | |
| 249 | def _mcc_mnc_digits(mcc, mnc): |
Harald Welte | 7f1d3c4 | 2020-05-12 21:12:44 +0200 | [diff] [blame] | 250 | return '%s%s' % (mcc, mnc) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 251 | |
| 252 | def _cc_digits(cc): |
| 253 | return ('%03d' if cc > 100 else '%02d') % cc |
| 254 | |
| 255 | def _isnum(s, l=-1): |
| 256 | return s.isdigit() and ((l== -1) or (len(s) == l)) |
| 257 | |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 258 | def _ishex(s, l=-1): |
| 259 | hc = '0123456789abcdef' |
| 260 | return all([x in hc for x in s.lower()]) and ((l== -1) or (len(s) == l)) |
| 261 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 262 | |
Sylvain Munaut | 9f120e0 | 2010-12-23 20:28:24 +0100 | [diff] [blame] | 263 | def _dbi_binary_quote(s): |
| 264 | # Count usage of each char |
| 265 | cnt = {} |
| 266 | for c in s: |
| 267 | cnt[c] = cnt.get(c, 0) + 1 |
| 268 | |
| 269 | # Find best offset |
| 270 | e = 0 |
| 271 | m = len(s) |
| 272 | for i in range(1, 256): |
| 273 | if i == 39: |
| 274 | continue |
| 275 | sum_ = cnt.get(i, 0) + cnt.get((i+1)&0xff, 0) + cnt.get((i+39)&0xff, 0) |
| 276 | if sum_ < m: |
| 277 | m = sum_ |
| 278 | e = i |
| 279 | if m == 0: # No overhead ? use this ! |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 280 | break |
Sylvain Munaut | 1a91443 | 2011-12-08 20:08:26 +0100 | [diff] [blame] | 281 | |
Sylvain Munaut | 9f120e0 | 2010-12-23 20:28:24 +0100 | [diff] [blame] | 282 | # Generate output |
| 283 | out = [] |
| 284 | out.append( chr(e) ) # Offset |
| 285 | for c in s: |
| 286 | x = (256 + ord(c) - e) % 256 |
| 287 | if x in (0, 1, 39): |
| 288 | out.append('\x01') |
| 289 | out.append(chr(x+1)) |
| 290 | else: |
| 291 | out.append(chr(x)) |
| 292 | |
| 293 | return ''.join(out) |
| 294 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 295 | def gen_parameters(opts): |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 296 | """Generates Name, ICCID, MCC, MNC, IMSI, SMSP, Ki, PIN-ADM from the |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 297 | options given by the user""" |
| 298 | |
| 299 | # MCC/MNC |
| 300 | mcc = opts.mcc |
| 301 | mnc = opts.mnc |
| 302 | |
Harald Welte | 7f1d3c4 | 2020-05-12 21:12:44 +0200 | [diff] [blame] | 303 | if not mcc.isdigit() or not mnc.isdigit(): |
| 304 | raise ValueError('mcc & mnc must only contain decimal digits') |
| 305 | if len(mcc) < 1 or len(mcc) > 3: |
| 306 | raise ValueError('mcc must be between 1 .. 3 digits') |
| 307 | if len(mnc) < 1 or len(mnc) > 3: |
| 308 | raise ValueError('mnc must be between 1 .. 3 digits') |
| 309 | |
| 310 | # MCC always has 3 digits |
| 311 | mcc = lpad(mcc, 3, "0") |
| 312 | # MNC must be at least 2 digits |
| 313 | mnc = lpad(mnc, 2, "0") |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 314 | |
| 315 | # Digitize country code (2 or 3 digits) |
| 316 | cc_digits = _cc_digits(opts.country) |
| 317 | |
| 318 | # Digitize MCC/MNC (5 or 6 digits) |
| 319 | plmn_digits = _mcc_mnc_digits(mcc, mnc) |
| 320 | |
Supreeth Herle | 840a9e2 | 2020-01-21 13:32:46 +0100 | [diff] [blame] | 321 | if opts.name is not None: |
| 322 | if len(opts.name) > 16: |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 323 | raise ValueError('Service Provider Name must max 16 characters!') |
Supreeth Herle | 840a9e2 | 2020-01-21 13:32:46 +0100 | [diff] [blame] | 324 | |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 325 | if opts.msisdn is not None: |
| 326 | msisdn = opts.msisdn |
| 327 | if msisdn[0] == '+': |
| 328 | msisdn = msisdn[1:] |
| 329 | if not msisdn.isdigit(): |
| 330 | raise ValueError('MSISDN must be digits only! ' |
| 331 | 'Start with \'+\' for international numbers.') |
| 332 | if len(msisdn) > 10 * 2: |
| 333 | # TODO: Support MSISDN of length > 20 (10 Bytes) |
| 334 | raise ValueError('MSISDNs longer than 20 digits are not (yet) supported.') |
| 335 | |
Harald Welte | 2c0ff3a | 2011-12-07 12:34:13 +0100 | [diff] [blame] | 336 | # ICCID (19 digits, E.118), though some phase1 vendors use 20 :( |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 337 | if opts.iccid is not None: |
| 338 | iccid = opts.iccid |
Todd Neal | 9eeadfc | 2018-04-25 15:36:29 -0500 | [diff] [blame] | 339 | if not _isnum(iccid, 19) and not _isnum(iccid, 20): |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 340 | raise ValueError('ICCID must be 19 or 20 digits !') |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 341 | |
| 342 | else: |
| 343 | if opts.num is None: |
| 344 | raise ValueError('Neither ICCID nor card number specified !') |
| 345 | |
| 346 | iccid = ( |
| 347 | '89' + # Common prefix (telecom) |
| 348 | cc_digits + # Country Code on 2/3 digits |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 349 | plmn_digits # MCC/MNC on 5/6 digits |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 350 | ) |
| 351 | |
Harald Welte | 2c0ff3a | 2011-12-07 12:34:13 +0100 | [diff] [blame] | 352 | ml = 18 - len(iccid) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 353 | |
| 354 | if opts.secret is None: |
| 355 | # The raw number |
| 356 | iccid += ('%%0%dd' % ml) % opts.num |
| 357 | else: |
| 358 | # Randomized digits |
| 359 | iccid += _digits(opts.secret, 'ccid', ml, opts.num) |
| 360 | |
Harald Welte | 2c0ff3a | 2011-12-07 12:34:13 +0100 | [diff] [blame] | 361 | # Add checksum digit |
| 362 | iccid += ('%1d' % calculate_luhn(iccid)) |
| 363 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 364 | # IMSI (15 digits usually) |
| 365 | if opts.imsi is not None: |
| 366 | imsi = opts.imsi |
| 367 | if not _isnum(imsi): |
| 368 | raise ValueError('IMSI must be digits only !') |
| 369 | |
| 370 | else: |
| 371 | if opts.num is None: |
| 372 | raise ValueError('Neither IMSI nor card number specified !') |
| 373 | |
| 374 | ml = 15 - len(plmn_digits) |
| 375 | |
| 376 | if opts.secret is None: |
| 377 | # The raw number |
| 378 | msin = ('%%0%dd' % ml) % opts.num |
| 379 | else: |
| 380 | # Randomized digits |
| 381 | msin = _digits(opts.secret, 'imsi', ml, opts.num) |
| 382 | |
| 383 | imsi = ( |
| 384 | plmn_digits + # MCC/MNC on 5/6 digits |
| 385 | msin # MSIN |
| 386 | ) |
| 387 | |
| 388 | # SMSP |
| 389 | if opts.smsp is not None: |
| 390 | smsp = opts.smsp |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 391 | if not _ishex(smsp): |
| 392 | raise ValueError('SMSP must be hex digits only !') |
| 393 | if len(smsp) < 28*2: |
| 394 | raise ValueError('SMSP must be at least 28 bytes') |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 395 | |
| 396 | else: |
Daniel Willmann | 4fa8f1c | 2018-10-02 18:10:21 +0200 | [diff] [blame] | 397 | ton = "81" |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 398 | if opts.smsc is not None: |
| 399 | smsc = opts.smsc |
Daniel Willmann | 4fa8f1c | 2018-10-02 18:10:21 +0200 | [diff] [blame] | 400 | if smsc[0] == '+': |
| 401 | ton = "91" |
| 402 | smsc = smsc[1:] |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 403 | if not _isnum(smsc): |
Daniel Willmann | 4fa8f1c | 2018-10-02 18:10:21 +0200 | [diff] [blame] | 404 | raise ValueError('SMSC must be digits only!\n \ |
| 405 | Start with \'+\' for international numbers') |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 406 | else: |
| 407 | smsc = '00%d' % opts.country + '5555' # Hack ... |
| 408 | |
Daniel Willmann | 4fa8f1c | 2018-10-02 18:10:21 +0200 | [diff] [blame] | 409 | smsc = '%02d' % ((len(smsc) + 3)//2,) + ton + swap_nibbles(rpad(smsc, 20)) |
Sylvain Munaut | 607ce2a | 2011-12-08 20:16:43 +0100 | [diff] [blame] | 410 | |
| 411 | smsp = ( |
| 412 | 'e1' + # Parameters indicator |
| 413 | 'ff' * 12 + # TP-Destination address |
| 414 | smsc + # TP-Service Centre Address |
| 415 | '00' + # TP-Protocol identifier |
| 416 | '00' + # TP-Data coding scheme |
| 417 | '00' # TP-Validity period |
| 418 | ) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 419 | |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 420 | # ACC |
| 421 | if opts.acc is not None: |
| 422 | acc = opts.acc |
| 423 | if not _ishex(acc): |
| 424 | raise ValueError('ACC must be hex digits only !') |
| 425 | if len(acc) != 2*2: |
| 426 | raise ValueError('ACC must be exactly 2 bytes') |
| 427 | |
| 428 | else: |
| 429 | acc = None |
| 430 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 431 | # Ki (random) |
| 432 | if opts.ki is not None: |
| 433 | ki = opts.ki |
| 434 | if not re.match('^[0-9a-fA-F]{32}$', ki): |
| 435 | raise ValueError('Ki needs to be 128 bits, in hex format') |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 436 | else: |
| 437 | ki = ''.join(['%02x' % random.randrange(0,256) for i in range(16)]) |
| 438 | |
Alexander Chemeris | d17ca3d | 2017-07-18 16:40:58 +0300 | [diff] [blame] | 439 | # OPC (random) |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 440 | if opts.opc is not None: |
| 441 | opc = opts.opc |
| 442 | if not re.match('^[0-9a-fA-F]{32}$', opc): |
| 443 | raise ValueError('OPC needs to be 128 bits, in hex format') |
| 444 | |
Holger Hans Peter Freyther | cca4179 | 2012-03-22 15:23:14 +0100 | [diff] [blame] | 445 | elif opts.op is not None: |
| 446 | opc = derive_milenage_opc(ki, opts.op) |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 447 | else: |
| 448 | opc = ''.join(['%02x' % random.randrange(0,256) for i in range(16)]) |
| 449 | |
Harald Welte | 79b5ba4 | 2021-01-08 21:22:38 +0100 | [diff] [blame] | 450 | pin_adm = sanitize_pin_adm(opts.pin_adm, opts.pin_adm_hex) |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 451 | |
Supreeth Herle | f964df4 | 2020-03-24 13:15:37 +0100 | [diff] [blame] | 452 | # ePDG Selection Information |
| 453 | if opts.epdgSelection: |
| 454 | if len(opts.epdgSelection) < 5 or len(opts.epdgSelection) > 6: |
| 455 | raise ValueError('ePDG Selection Information is not valid') |
| 456 | epdg_mcc = opts.epdgSelection[:3] |
| 457 | epdg_mnc = opts.epdgSelection[3:] |
| 458 | if not epdg_mcc.isdigit() or not epdg_mnc.isdigit(): |
| 459 | raise ValueError('PLMN for ePDG Selection must only contain decimal digits') |
| 460 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 461 | # Return that |
| 462 | return { |
| 463 | 'name' : opts.name, |
| 464 | 'iccid' : iccid, |
| 465 | 'mcc' : mcc, |
| 466 | 'mnc' : mnc, |
| 467 | 'imsi' : imsi, |
| 468 | 'smsp' : smsp, |
| 469 | 'ki' : ki, |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 470 | 'opc' : opc, |
Alexander Chemeris | 2188524 | 2013-07-02 16:56:55 +0400 | [diff] [blame] | 471 | 'acc' : acc, |
Jan Balke | c3ebd33 | 2015-01-26 12:22:55 +0100 | [diff] [blame] | 472 | 'pin_adm' : pin_adm, |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 473 | 'msisdn' : opts.msisdn, |
Supreeth Herle | 8e0fccd | 2020-03-23 12:10:56 +0100 | [diff] [blame] | 474 | 'epdgid' : opts.epdgid, |
Supreeth Herle | f964df4 | 2020-03-24 13:15:37 +0100 | [diff] [blame] | 475 | 'epdgSelection' : opts.epdgSelection, |
Supreeth Herle | cf727f2 | 2020-03-24 17:32:21 +0100 | [diff] [blame] | 476 | 'pcscf' : opts.pcscf, |
Supreeth Herle | 79f43dd | 2020-03-25 11:43:19 +0100 | [diff] [blame] | 477 | 'ims_hdomain': opts.ims_hdomain, |
Supreeth Herle | be7007e | 2020-03-26 09:27:45 +0100 | [diff] [blame] | 478 | 'impi' : opts.impi, |
| 479 | 'impu' : opts.impu, |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 480 | 'opmode': opts.opmode, |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | |
| 484 | def print_parameters(params): |
| 485 | |
Daniel Willmann | c46a4eb | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 486 | s = ["Generated card parameters :"] |
| 487 | if 'name' in params: |
| 488 | s.append(" > Name : %(name)s") |
| 489 | if 'smsp' in params: |
| 490 | s.append(" > SMSP : %(smsp)s") |
| 491 | s.append(" > ICCID : %(iccid)s") |
Philipp Maier | be069e2 | 2019-09-12 12:52:43 +0200 | [diff] [blame] | 492 | s.append(" > MCC/MNC : %(mcc)s/%(mnc)s") |
Daniel Willmann | c46a4eb | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 493 | s.append(" > IMSI : %(imsi)s") |
| 494 | s.append(" > Ki : %(ki)s") |
| 495 | s.append(" > OPC : %(opc)s") |
| 496 | if 'acc' in params: |
| 497 | s.append(" > ACC : %(acc)s") |
| 498 | s.append(" > ADM1(hex): %(pin_adm)s") |
Robert Falkenberg | d0505bd | 2021-02-24 14:06:18 +0100 | [diff] [blame] | 499 | if 'opmode' in params: |
| 500 | s.append(" > OPMODE : %(opmode)s") |
Daniel Willmann | c46a4eb | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 501 | print("\n".join(s) % params) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 502 | |
| 503 | |
Harald Welte | 130524b | 2012-08-13 15:53:43 +0200 | [diff] [blame] | 504 | def write_params_csv(opts, params): |
| 505 | # csv |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 506 | if opts.write_csv: |
| 507 | import csv |
Harald Welte | 93b38cd | 2012-03-22 14:31:36 +0100 | [diff] [blame] | 508 | row = ['name', 'iccid', 'mcc', 'mnc', 'imsi', 'smsp', 'ki', 'opc'] |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 509 | f = open(opts.write_csv, 'a') |
| 510 | cw = csv.writer(f) |
| 511 | cw.writerow([params[x] for x in row]) |
| 512 | f.close() |
| 513 | |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 514 | def _read_params_csv(opts, iccid=None, imsi=None): |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 515 | import csv |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 516 | f = open(opts.read_csv, 'r') |
Daniel Willmann | c46a4eb | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 517 | cr = csv.DictReader(f) |
Philipp Maier | 120a000 | 2019-09-12 13:11:45 +0200 | [diff] [blame] | 518 | |
| 519 | # Lower-case fieldnames |
| 520 | cr.fieldnames = [ field.lower() for field in cr.fieldnames ] |
| 521 | |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 522 | i = 0 |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 523 | if not 'iccid' in cr.fieldnames: |
| 524 | raise Exception("CSV file in wrong format!") |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 525 | for row in cr: |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 526 | if opts.num is not None and opts.read_iccid is False and opts.read_imsi is False: |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 527 | if opts.num == i: |
Harald Welte | c26b829 | 2012-08-15 15:25:51 +0200 | [diff] [blame] | 528 | f.close() |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 529 | return row |
Harald Welte | c26b829 | 2012-08-15 15:25:51 +0200 | [diff] [blame] | 530 | i += 1 |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 531 | if row['iccid'] == iccid: |
| 532 | f.close() |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 533 | return row |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 534 | |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 535 | if row['imsi'] == imsi: |
Harald Welte | c26b829 | 2012-08-15 15:25:51 +0200 | [diff] [blame] | 536 | f.close() |
Daniel Willmann | 677d41b | 2020-10-19 10:34:31 +0200 | [diff] [blame] | 537 | return row |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 538 | |
| 539 | f.close() |
Harald Welte | c26b829 | 2012-08-15 15:25:51 +0200 | [diff] [blame] | 540 | return None |
| 541 | |
Daniel Willmann | 164b963 | 2019-09-03 19:13:51 +0200 | [diff] [blame] | 542 | def read_params_csv(opts, imsi=None, iccid=None): |
| 543 | row = _read_params_csv(opts, iccid=iccid, imsi=imsi) |
Harald Welte | c26b829 | 2012-08-15 15:25:51 +0200 | [diff] [blame] | 544 | if row is not None: |
Philipp Maier | 7592eee | 2019-09-12 13:03:23 +0200 | [diff] [blame] | 545 | row['mcc'] = row.get('mcc', mcc_from_imsi(row.get('imsi'))) |
| 546 | row['mnc'] = row.get('mnc', mnc_from_imsi(row.get('imsi'))) |
| 547 | |
Daniel Willmann | c46a4eb | 2018-06-15 07:31:50 +0200 | [diff] [blame] | 548 | pin_adm = None |
| 549 | # We need to escape the pin_adm we get from the csv |
| 550 | if 'pin_adm' in row: |
| 551 | pin_adm = ''.join(['%02x'%(ord(x)) for x in row['pin_adm']]) |
| 552 | # Stay compatible to the odoo csv format |
| 553 | elif 'adm1' in row: |
| 554 | pin_adm = ''.join(['%02x'%(ord(x)) for x in row['adm1']]) |
| 555 | if pin_adm: |
| 556 | row['pin_adm'] = rpad(pin_adm, 16) |
Philipp Maier | e053da5 | 2019-09-05 13:08:36 +0200 | [diff] [blame] | 557 | |
| 558 | # If the CSV-File defines a pin_adm_hex field use this field to |
| 559 | # generate pin_adm from that. |
| 560 | pin_adm_hex = row.get('pin_adm_hex') |
| 561 | if pin_adm_hex: |
| 562 | if len(pin_adm_hex) == 16: |
| 563 | row['pin_adm'] = pin_adm_hex |
| 564 | # Ensure that it's hex-encoded |
| 565 | try: |
| 566 | try_encode = h2b(pin_adm) |
| 567 | except ValueError: |
| 568 | raise ValueError("pin_adm_hex needs to be hex encoded using this option") |
| 569 | else: |
| 570 | raise ValueError("pin_adm_hex needs to be exactly 16 digits (hex encoded)") |
| 571 | |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 572 | return row |
| 573 | |
Harald Welte | c26b829 | 2012-08-15 15:25:51 +0200 | [diff] [blame] | 574 | |
Harald Welte | 130524b | 2012-08-13 15:53:43 +0200 | [diff] [blame] | 575 | def write_params_hlr(opts, params): |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 576 | # SQLite3 OpenBSC HLR |
| 577 | if opts.write_hlr: |
| 578 | import sqlite3 |
| 579 | conn = sqlite3.connect(opts.write_hlr) |
| 580 | |
| 581 | c = conn.execute( |
| 582 | 'INSERT INTO Subscriber ' + |
| 583 | '(imsi, name, extension, authorized, created, updated) ' + |
| 584 | 'VALUES ' + |
| 585 | '(?,?,?,1,datetime(\'now\'),datetime(\'now\'));', |
| 586 | [ |
| 587 | params['imsi'], |
| 588 | params['name'], |
Harald Welte | e9e5ecb | 2012-08-15 15:26:30 +0200 | [diff] [blame] | 589 | '9' + params['iccid'][-5:-1] |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 590 | ], |
| 591 | ) |
| 592 | sub_id = c.lastrowid |
| 593 | c.close() |
| 594 | |
| 595 | c = conn.execute( |
| 596 | 'INSERT INTO AuthKeys ' + |
| 597 | '(subscriber_id, algorithm_id, a3a8_ki)' + |
| 598 | 'VALUES ' + |
| 599 | '(?,?,?)', |
Sylvain Munaut | 9f120e0 | 2010-12-23 20:28:24 +0100 | [diff] [blame] | 600 | [ sub_id, 2, sqlite3.Binary(_dbi_binary_quote(h2b(params['ki']))) ], |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 601 | ) |
| 602 | |
| 603 | conn.commit() |
| 604 | conn.close() |
| 605 | |
Harald Welte | 130524b | 2012-08-13 15:53:43 +0200 | [diff] [blame] | 606 | def write_parameters(opts, params): |
| 607 | write_params_csv(opts, params) |
Harald Welte | 7f62cec | 2012-08-13 20:07:41 +0200 | [diff] [blame] | 608 | write_params_hlr(opts, params) |
Harald Welte | 130524b | 2012-08-13 15:53:43 +0200 | [diff] [blame] | 609 | |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 610 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 611 | BATCH_STATE = [ 'name', 'country', 'mcc', 'mnc', 'smsp', 'secret', 'num' ] |
| 612 | BATCH_INCOMPATIBLE = ['iccid', 'imsi', 'ki'] |
Sylvain Munaut | 143e99d | 2010-12-08 22:35:04 +0100 | [diff] [blame] | 613 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 614 | def init_batch(opts): |
| 615 | # Need to do something ? |
| 616 | if not opts.batch_mode: |
| 617 | return |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 618 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 619 | for k in BATCH_INCOMPATIBLE: |
| 620 | if getattr(opts, k): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 621 | print("Incompatible option with batch_state: %s" % (k,)) |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 622 | sys.exit(-1) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 623 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 624 | # Don't load state if there is none ... |
| 625 | if not opts.batch_state: |
| 626 | return |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 627 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 628 | if not os.path.isfile(opts.batch_state): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 629 | print("No state file yet") |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 630 | return |
| 631 | |
| 632 | # Get stored data |
| 633 | fh = open(opts.batch_state) |
| 634 | d = json.loads(fh.read()) |
| 635 | fh.close() |
| 636 | |
| 637 | for k,v in d.iteritems(): |
| 638 | setattr(opts, k, v) |
| 639 | |
| 640 | |
| 641 | def save_batch(opts): |
| 642 | # Need to do something ? |
| 643 | if not opts.batch_mode or not opts.batch_state: |
| 644 | return |
| 645 | |
| 646 | d = json.dumps(dict([(k,getattr(opts,k)) for k in BATCH_STATE])) |
| 647 | fh = open(opts.batch_state, 'w') |
| 648 | fh.write(d) |
| 649 | fh.close() |
| 650 | |
| 651 | |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 652 | def process_card(opts, first, card_handler): |
| 653 | |
| 654 | if opts.dry_run is False: |
| 655 | # Connect transport |
| 656 | card_handler.get(first) |
| 657 | |
| 658 | if opts.dry_run is False: |
| 659 | # Get card |
Supreeth Herle | 4c306ab | 2020-03-18 11:38:00 +0100 | [diff] [blame] | 660 | card = card_detect(opts.type, scc) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 661 | if card is None: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 662 | print("No card detected!") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 663 | return -1 |
| 664 | |
| 665 | # Probe only |
| 666 | if opts.probe: |
| 667 | return 0 |
| 668 | |
| 669 | # Erase if requested |
| 670 | if opts.erase: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 671 | print("Formatting ...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 672 | card.erase() |
| 673 | card.reset() |
| 674 | |
| 675 | # Generate parameters |
| 676 | if opts.source == 'cmdline': |
| 677 | cp = gen_parameters(opts) |
| 678 | elif opts.source == 'csv': |
| 679 | imsi = None |
| 680 | iccid = None |
| 681 | if opts.read_iccid: |
| 682 | if opts.dry_run: |
| 683 | # Connect transport |
Daniel Willmann | dd014ea | 2020-10-19 10:35:11 +0200 | [diff] [blame] | 684 | card_handler.get(False) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 685 | (res,_) = scc.read_binary(['3f00', '2fe2'], length=10) |
| 686 | iccid = dec_iccid(res) |
| 687 | elif opts.read_imsi: |
| 688 | if opts.dry_run: |
| 689 | # Connect transport |
Daniel Willmann | dd014ea | 2020-10-19 10:35:11 +0200 | [diff] [blame] | 690 | card_handler.get(False) |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 691 | (res,_) = scc.read_binary(EF['IMSI']) |
| 692 | imsi = swap_nibbles(res)[3:] |
| 693 | else: |
| 694 | imsi = opts.imsi |
| 695 | cp = read_params_csv(opts, imsi=imsi, iccid=iccid) |
| 696 | if cp is None: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 697 | print("Error reading parameters from CSV file!\n") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 698 | return 2 |
| 699 | print_parameters(cp) |
| 700 | |
| 701 | if opts.dry_run is False: |
| 702 | # Program the card |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 703 | print("Programming ...") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 704 | card.program(cp) |
| 705 | else: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 706 | print("Dry Run: NOT PROGRAMMING!") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 707 | |
| 708 | # Write parameters permanently |
| 709 | write_parameters(opts, cp) |
| 710 | |
| 711 | # Batch mode state update and save |
| 712 | if opts.num is not None: |
| 713 | opts.num += 1 |
| 714 | save_batch(opts) |
| 715 | |
| 716 | card_handler.done() |
| 717 | return 0 |
| 718 | |
| 719 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 720 | if __name__ == '__main__': |
| 721 | |
| 722 | # Parse options |
| 723 | opts = parse_options() |
| 724 | |
Vadim Yanitskiy | 588f3ac | 2018-10-27 06:30:33 +0700 | [diff] [blame] | 725 | # Init card reader driver |
Philipp Maier | ff84c23 | 2020-05-12 17:24:18 +0200 | [diff] [blame] | 726 | sl = init_reader(opts) |
Philipp Maier | c8caec2 | 2021-02-22 16:07:53 +0100 | [diff] [blame] | 727 | if sl is None: |
| 728 | exit(1) |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 729 | |
| 730 | # Create command layer |
| 731 | scc = SimCardCommands(transport=sl) |
| 732 | |
Philipp Maier | 196b08c | 2019-09-12 11:49:44 +0200 | [diff] [blame] | 733 | # If we use a CSV file as data input, check if the CSV file exists. |
| 734 | if opts.source == 'csv': |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 735 | print("Using CSV file as data input: " + str(opts.read_csv)) |
Philipp Maier | 196b08c | 2019-09-12 11:49:44 +0200 | [diff] [blame] | 736 | if not os.path.isfile(opts.read_csv): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 737 | print("CSV file not found!") |
Philipp Maier | 196b08c | 2019-09-12 11:49:44 +0200 | [diff] [blame] | 738 | sys.exit(1) |
| 739 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 740 | # Batch mode init |
| 741 | init_batch(opts) |
| 742 | |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 743 | if opts.card_handler: |
| 744 | card_handler = card_handler_auto(sl, opts.card_handler) |
Denis 'GNUtoo' Carikli | 84d2cb3 | 2019-09-12 01:46:25 +0200 | [diff] [blame] | 745 | else: |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 746 | card_handler = card_handler(sl) |
| 747 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 748 | # Iterate |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 749 | first = True |
| 750 | card = None |
Sylvain Munaut | 1a91443 | 2011-12-08 20:08:26 +0100 | [diff] [blame] | 751 | |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 752 | while 1: |
| 753 | try: |
| 754 | rc = process_card(opts, first, card_handler) |
| 755 | except (KeyboardInterrupt): |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 756 | print("") |
| 757 | print("Terminated by user!") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 758 | sys.exit(0) |
| 759 | except (SystemExit): |
| 760 | raise |
| 761 | except: |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 762 | print("") |
| 763 | print("Card programming failed with an execption:") |
| 764 | print("---------------------8<---------------------") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 765 | traceback.print_exc() |
Vadim Yanitskiy | 6727f0c | 2020-01-22 23:38:24 +0700 | [diff] [blame] | 766 | print("---------------------8<---------------------") |
| 767 | print("") |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 768 | rc = -1 |
Harald Welte | e9e5ecb | 2012-08-15 15:26:30 +0200 | [diff] [blame] | 769 | |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 770 | # Something did not work as well as expected, however, lets |
| 771 | # make sure the card is pulled from the reader. |
| 772 | if rc != 0: |
| 773 | card_handler.error() |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 774 | |
Philipp Maier | c5b422e | 2019-08-30 11:41:02 +0200 | [diff] [blame] | 775 | # If we are not in batch mode we are done in any case, so lets |
| 776 | # exit here. |
| 777 | if not opts.batch_mode: |
| 778 | sys.exit(rc) |
| 779 | |
Sylvain Munaut | 8f7d3ba | 2010-12-09 13:32:48 +0100 | [diff] [blame] | 780 | first = False |