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