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