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