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