Add a new pySim-shell program

pySim-prog was nice when there were only 5 parameters on a SIM that we
could program, and where the use case was pretty limited.  Today, we
have SIM/USIM/ISIM cards with hundreds of files and even more parameters
to program.  We cannot add a command line argument for each file to
pySim-prog.

Instead, this introduces an interactive command-line shell / REPL,
in which one can navigate the file system of the card, read and update
files both in raw format and in decoded/parsed format.

The idea is primarily inspired by Henryk Ploatz' venerable
cyberflex-shell, but implemented on a more modern basis using
the cmd2 python module.

See https://lists.osmocom.org/pipermail/simtrace/2021-January/000860.html
and https://lists.osmocom.org/pipermail/simtrace/2021-February/000864.html
for some related background.

Most code by Harald Welte. Some bug fixes by Philipp Maier
have been squashed.

Change-Id: Iad117596e922223bdc1e5b956f84844b7c577e02
Related: OS#4963
This commit is contained in:
Harald Welte 2021-01-08 23:29:35 +01:00
parent 4f6ca43e1f
commit b2edd14475
8 changed files with 1800 additions and 2 deletions

View File

@ -13,6 +13,7 @@ virtualenv -p python3 venv --system-site-packages
. venv/bin/activate
pip install pytlv
pip install pyyaml
pip install cmd2
cd pysim-testdata
../tests/pysim-test.sh

213
pySim-shell.py Executable file
View File

@ -0,0 +1,213 @@
#!/usr/bin/env python3
# Interactive shell for working with SIM / UICC / USIM / ISIM cards
#
# (C) 2021 by Harald Welte <laforge@osmocom.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from typing import List
import json
import cmd2
from cmd2 import style, fg, bg
from cmd2 import CommandSet, with_default_category, with_argparser
import argparse
import os
import sys
from optparse import OptionParser
from pySim.ts_51_011 import EF, DF, EF_SST_map, EF_AD_mode_map
from pySim.ts_31_102 import EF_UST_map, EF_USIM_ADF_map
from pySim.ts_31_103 import EF_IST_map, EF_ISIM_ADF_map
from pySim.exceptions import *
from pySim.commands import SimCardCommands
from pySim.cards import card_detect, Card
from pySim.utils import h2b, swap_nibbles, rpad, h2s
from pySim.utils import dec_st, init_reader, sanitize_pin_adm
from pySim.card_handler import card_handler
from pySim.filesystem import CardMF, RuntimeState
from pySim.ts_51_011 import CardProfileSIM, DF_TELECOM, DF_GSM
from pySim.ts_102_221 import CardProfileUICC
from pySim.ts_31_102 import ADF_USIM
from pySim.ts_31_103 import ADF_ISIM
class PysimApp(cmd2.Cmd):
CUSTOM_CATEGORY = 'pySim Commands'
def __init__(self, card, rs):
basic_commands = [Iso7816Commands(), UsimCommands()]
super().__init__(persistent_history_file='~/.pysim_shell_history', allow_cli_args=False,
use_ipython=True, auto_load_commands=False, command_sets=basic_commands)
self.intro = style('Welcome to pySim-shell!', fg=fg.red)
self.default_category = 'pySim-shell built-in commands'
self.card = card
self.rs = rs
self.py_locals = { 'card': self.card, 'rs' : self.rs }
self.card.read_aids()
self.poutput('AIDs on card: %s' % (self.card._aids))
self.numeric_path = False
self.add_settable(cmd2.Settable('numeric_path', bool, 'Print File IDs instead of names',
onchange_cb=self._onchange_numeric_path))
self.update_prompt()
def _onchange_numeric_path(self, param_name, old, new):
self.update_prompt()
def update_prompt(self):
path_list = self.rs.selected_file.fully_qualified_path(not self.numeric_path)
self.prompt = 'pySIM-shell (%s)> ' % ('/'.join(path_list))
@cmd2.with_category(CUSTOM_CATEGORY)
def do_intro(self, _):
"""Display the intro banner"""
self.poutput(self.intro)
@cmd2.with_category(CUSTOM_CATEGORY)
def do_verify_adm(self, arg):
"""VERIFY the ADM1 PIN"""
pin_adm = sanitize_pin_adm(arg)
self.card.verify_adm(h2b(pin_adm))
@with_default_category('ISO7816 Commands')
class Iso7816Commands(CommandSet):
def __init__(self):
super().__init__()
def do_select(self, opts):
"""SELECT a File (ADF/DF/EF)"""
path = opts.arg_list[0]
fcp_dec = self._cmd.rs.select(path, self._cmd)
self._cmd.update_prompt()
self._cmd.poutput(json.dumps(fcp_dec, indent=4))
def complete_select(self, text, line, begidx, endidx) -> List[str]:
"""Command Line tab completion for SELECT"""
index_dict = { 1: self._cmd.rs.selected_file.get_selectable_names() }
return self._cmd.index_based_complete(text, line, begidx, endidx, index_dict=index_dict)
verify_chv_parser = argparse.ArgumentParser()
verify_chv_parser.add_argument('--chv-nr', type=int, default=1, help='CHV Number')
verify_chv_parser.add_argument('code', help='CODE/PIN/PUK')
@cmd2.with_argparser(verify_chv_parser)
def do_verify_chv(self, opts):
"""Verify (authenticate) using specified CHV (PIN)"""
(data, sw) = self._cmd.card._scc.verify_chv(opts.chv_nr, opts.code)
self._cmd.poutput(data)
@with_default_category('USIM Commands')
class UsimCommands(CommandSet):
def __init__(self):
super().__init__()
def do_read_ust(self, _):
"""Read + Display the EF.UST"""
self._cmd.card.select_adf_by_aid(adf="usim")
(res, sw) = self._cmd.card.read_ust()
self._cmd.poutput(res[0])
self._cmd.poutput(res[1])
def do_read_ehplmn(self, _):
"""Read EF.EHPLMN"""
self._cmd.card.select_adf_by_aid(adf="usim")
(res, sw) = self._cmd.card.read_ehplmn()
self._cmd.poutput(res)
def parse_options():
parser = OptionParser(usage="usage: %prog [options]")
parser.add_option("-d", "--device", dest="device", metavar="DEV",
help="Serial Device for SIM access [default: %default]",
default="/dev/ttyUSB0",
)
parser.add_option("-b", "--baud", dest="baudrate", type="int", metavar="BAUD",
help="Baudrate used for SIM access [default: %default]",
default=9600,
)
parser.add_option("-p", "--pcsc-device", dest="pcsc_dev", type='int', metavar="PCSC",
help="Which PC/SC reader number for SIM access",
default=None,
)
parser.add_option("--modem-device", dest="modem_dev", metavar="DEV",
help="Serial port of modem for Generic SIM Access (3GPP TS 27.007)",
default=None,
)
parser.add_option("--modem-baud", dest="modem_baud", type="int", metavar="BAUD",
help="Baudrate used for modem's port [default: %default]",
default=115200,
)
parser.add_option("--osmocon", dest="osmocon_sock", metavar="PATH",
help="Socket path for Calypso (e.g. Motorola C1XX) based reader (via OsmocomBB)",
default=None,
)
parser.add_option("-a", "--pin-adm", dest="pin_adm",
help="ADM PIN used for provisioning (overwrites default)",
)
parser.add_option("-A", "--pin-adm-hex", dest="pin_adm_hex",
help="ADM PIN used for provisioning, as hex string (16 characters long",
)
(options, args) = parser.parse_args()
if args:
parser.error("Extraneous arguments")
return options
if __name__ == '__main__':
# Parse options
opts = parse_options()
# Init card reader driver
sl = init_reader(opts)
if (sl == None):
exit(1)
# Create command layer
scc = SimCardCommands(transport=sl)
sl.wait_for_card();
card_handler = card_handler(sl)
card = card_detect("auto", scc)
if card is None:
print("No card detected!")
sys.exit(2)
profile = CardProfileUICC()
rs = RuntimeState(card, profile)
# FIXME: do this dynamically
rs.mf.add_file(DF_TELECOM())
rs.mf.add_file(DF_GSM())
rs.mf.add_application(ADF_USIM())
rs.mf.add_application(ADF_ISIM())
app = PysimApp(card, rs)
app.cmdloop()

View File

@ -41,8 +41,13 @@ class ReaderError(Exception):
class SwMatchError(Exception):
"""Raised when an operation specifies an expected SW but the actual SW from
the card doesn't match."""
def __init__(self, sw_actual, sw_expected):
def __init__(self, sw_actual, sw_expected, rs=None):
self.sw_actual = sw_actual
self.sw_expected = sw_expected
self.rs = rs
def __str__(self):
if self.rs:
r = self.rs.interpret_sw(sw_actual)
if r:
return "SW match failed! Expected %s and got %s: %s - %s" % (self.sw_expected, self.sw_actual, r[0], r[1])
return "SW match failed! Expected %s and got %s." % (self.sw_expected, self.sw_actual)

715
pySim/filesystem.py Normal file
View File

@ -0,0 +1,715 @@
# coding=utf-8
"""Representation of the ISO7816-4 filesystem model.
The File (and its derived classes) represent the structure / hierarchy
of the ISO7816-4 smart card file system with the MF, DF, EF and ADF
entries, further sub-divided into the EF sub-types Transparent, Linear Fixed, etc.
The classes are intended to represent the *specification* of the filesystem,
not the actual contents / runtime state of interacting with a given smart card.
(C) 2021 by Harald Welte <laforge@osmocom.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import code
import json
import cmd2
from cmd2 import CommandSet, with_default_category, with_argparser
import argparse
from pySim.utils import sw_match, h2b, b2h
from pySim.exceptions import *
class CardFile(object):
"""Base class for all objects in the smart card filesystem.
Serve as a common ancestor to all other file types; rarely used directly.
"""
RESERVED_NAMES = ['..', '.', '/', 'MF']
RESERVED_FIDS = ['3f00']
def __init__(self, fid=None, sfid=None, name=None, desc=None, parent=None):
if not isinstance(self, CardADF) and fid == None:
raise ValueError("fid is mandatory")
if fid:
fid = fid.lower()
self.fid = fid # file identifier
self.sfid = sfid # short file identifier
self.name = name # human readable name
self.desc = desc # human readable description
self.parent = parent
if self.parent and self.parent != self and self.fid:
self.parent.add_file(self)
self.shell_commands = []
def __str__(self):
if self.name:
return self.name
else:
return self.fid
def _path_element(self, prefer_name):
if prefer_name and self.name:
return self.name
else:
return self.fid
def fully_qualified_path(self, prefer_name=True):
"""Return fully qualified path to file as list of FID or name strings."""
if self.parent != self:
ret = self.parent.fully_qualified_path(prefer_name)
else:
ret = []
ret.append(self._path_element(prefer_name))
return ret
def get_mf(self):
"""Return the MF (root) of the file system."""
if self.parent == None:
return None
# iterate towards the top. MF has parent == self
node = self
while node.parent != node:
node = node.parent
return node
def _get_self_selectables(self, alias=None):
"""Return a dict of {'identifier': self} tuples"""
sels = {}
if alias:
sels.update({alias: self})
if self.fid:
sels.update({self.fid: self})
if self.name:
sels.update({self.name: self})
return sels
def get_selectables(self):
"""Return a dict of {'identifier': File} that is selectable from the current file."""
# we can always select ourself
sels = self._get_self_selectables('.')
# we can always select our parent
sels = self.parent._get_self_selectables('..')
# if we have a MF, we can always select its applications
mf = self.get_mf()
if mf:
sels.update(mf._get_self_selectables())
sels.update(mf.get_app_selectables())
return sels
def get_selectable_names(self):
"""Return a list of strings for all identifiers that are selectable from the current file."""
sels = self.get_selectables()
return sels.keys()
def decode_select_response(self, data_hex):
"""Decode the response to a SELECT command."""
return self.parent.decode_select_response(data_hex)
class CardDF(CardFile):
"""DF (Dedicated File) in the smart card filesystem. Those are basically sub-directories."""
def __init__(self, **kwargs):
if not isinstance(self, CardADF):
if not 'fid' in kwargs:
raise TypeError('fid is mandatory for all DF')
super().__init__(**kwargs)
self.children = dict()
def __str__(self):
return "DF(%s)" % (super().__str__())
def add_file(self, child, ignore_existing=False):
"""Add a child (DF/EF) to this DF"""
if not isinstance(child, CardFile):
raise TypeError("Expected a File instance")
if child.name in CardFile.RESERVED_NAMES:
raise ValueError("File name %s is a reserved name" % (child.name))
if child.fid in CardFile.RESERVED_FIDS:
raise ValueError("File fid %s is a reserved name" % (child.fid))
if child.fid in self.children:
if ignore_existing:
return
raise ValueError("File with given fid %s already exists" % (child.fid))
if self.lookup_file_by_sfid(child.sfid):
raise ValueError("File with given sfid %s already exists" % (child.sfid))
if self.lookup_file_by_name(child.name):
if ignore_existing:
return
raise ValueError("File with given name %s already exists" % (child.name))
self.children[child.fid] = child
child.parent = self
def add_files(self, children, ignore_existing=False):
"""Add a list of child (DF/EF) to this DF"""
for child in children:
self.add_file(child, ignore_existing)
def get_selectables(self):
"""Get selectable (DF/EF names) from current DF"""
# global selectables + our children
sels = super().get_selectables()
sels.update({x.fid: x for x in self.children.values() if x.fid})
sels.update({x.name: x for x in self.children.values() if x.name})
return sels
def lookup_file_by_name(self, name):
if name == None:
return None
for i in self.children.values():
if i.name and i.name == name:
return i
return None
def lookup_file_by_sfid(self, sfid):
if sfid == None:
return None
for i in self.children.values():
if i.sfid == int(sfid):
return i
return None
def lookup_file_by_fid(self, fid):
if fid in self.children:
return self.children[fid]
return None
class CardMF(CardDF):
"""MF (Master File) in the smart card filesystem"""
def __init__(self, **kwargs):
# can be overridden; use setdefault
kwargs.setdefault('fid', '3f00')
kwargs.setdefault('name', 'MF')
kwargs.setdefault('desc', 'Master File (directory root)')
# cannot be overridden; use assignment
kwargs['parent'] = self
super().__init__(**kwargs)
self.applications = dict()
def __str__(self):
return "MF(%s)" % (self.fid)
def add_application(self, app):
"""Add an ADF (Application Dedicated File) to the MF"""
if not isinstance(app, CardADF):
raise TypeError("Expected an ADF instance")
if app.aid in self.applications:
raise ValueError("AID %s already exists" % (app.aid))
self.applications[app.aid] = app
app.parent=self
def get_app_names(self):
"""Get list of completions (AID names)"""
return [x.name for x in self.applications]
def get_selectables(self):
"""Get list of completions (DF/EF/ADF names) from current DF"""
sels = super().get_selectables()
sels.update(self.get_app_selectables())
return sels
def get_app_selectables(self):
# applications by AID + name
sels = {x.aid: x for x in self.applications.values()}
sels.update({x.name: x for x in self.applications.values() if x.name})
return sels
def decode_select_response(self, data_hex):
"""Decode the response to a SELECT command."""
return data_hex
class CardADF(CardDF):
"""ADF (Application Dedicated File) in the smart card filesystem"""
def __init__(self, aid, **kwargs):
super().__init__(**kwargs)
self.aid = aid # Application Identifier
if self.parent:
self.parent.add_application(self)
def __str__(self):
return "ADF(%s)" % (self.aid)
def _path_element(self, prefer_name):
if self.name and prefer_name:
return self.name
else:
return self.aid
class CardEF(CardFile):
"""EF (Entry File) in the smart card filesystem"""
def __init__(self, *, fid, **kwargs):
kwargs['fid'] = fid
super().__init__(**kwargs)
def __str__(self):
return "EF(%s)" % (super().__str__())
def get_selectables(self):
"""Get list of completions (EF names) from current DF"""
#global selectable names + those of the parent DF
sels = super().get_selectables()
sels.update({x.name:x for x in self.parent.children.values() if x != self})
return sels
class TransparentEF(CardEF):
"""Transparent EF (Entry File) in the smart card filesystem"""
@with_default_category('Transparent EF Commands')
class ShellCommands(CommandSet):
def __init__(self):
super().__init__()
read_bin_parser = argparse.ArgumentParser()
read_bin_parser.add_argument('--offset', type=int, default=0, help='Byte offset for start of read')
read_bin_parser.add_argument('--length', type=int, help='Number of bytes to read')
@cmd2.with_argparser(read_bin_parser)
def do_read_binary(self, opts):
"""Read binary data from a transparent EF"""
(data, sw) = self._cmd.rs.read_binary(opts.length, opts.offset)
self._cmd.poutput(data)
def do_read_binary_decoded(self, opts):
"""Read + decode data from a transparent EF"""
(data, sw) = self._cmd.rs.read_binary_dec()
self._cmd.poutput(json.dumps(data, indent=4))
upd_bin_parser = argparse.ArgumentParser()
upd_bin_parser.add_argument('--offset', type=int, default=0, help='Byte offset for start of read')
upd_bin_parser.add_argument('data', help='Data bytes (hex format) to write')
@cmd2.with_argparser(upd_bin_parser)
def do_update_binary(self, opts):
"""Update (Write) data of a transparent EF"""
(data, sw) = self._cmd.rs.update_binary(opts.data, opts.offset)
self._cmd.poutput(data)
upd_bin_dec_parser = argparse.ArgumentParser()
upd_bin_dec_parser.add_argument('data', help='Abstract data (JSON format) to write')
@cmd2.with_argparser(upd_bin_dec_parser)
def do_update_binary_decoded(self, opts):
"""Encode + Update (Write) data of a transparent EF"""
data_json = json.loads(opts.data)
(data, sw) = self._cmd.rs.update_binary_dec(data_json)
self._cmd.poutput(json.dumps(data, indent=4))
def __init__(self, fid, sfid=None, name=None, desc=None, parent=None, size={1,None}):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, parent=parent)
self.size = size
self.shell_commands = [self.ShellCommands()]
def decode_bin(self, raw_bin_data):
"""Decode raw (binary) data into abstract representation. Overloaded by specific classes."""
method = getattr(self, '_decode_bin', None)
if callable(method):
return method(raw_bin_data)
method = getattr(self, '_decode_hex', None)
if callable(method):
return method(b2h(raw_bin_data))
return {'raw': raw_bin_data.hex()}
def decode_hex(self, raw_hex_data):
"""Decode raw (hex string) data into abstract representation. Overloaded by specific classes."""
method = getattr(self, '_decode_hex', None)
if callable(method):
return method(raw_hex_data)
raw_bin_data = h2b(raw_hex_data)
method = getattr(self, '_decode_bin', None)
if callable(method):
return method(raw_bin_data)
return {'raw': raw_bin_data.hex()}
def encode_bin(self, abstract_data):
"""Encode abstract representation into raw (binary) data. Overloaded by specific classes."""
method = getattr(self, '_encode_bin', None)
if callable(method):
return method(abstract_data)
method = getattr(self, '_encode_hex', None)
if callable(method):
return h2b(method(abstract_data))
raise NotImplementedError
def encode_hex(self, abstract_data):
"""Encode abstract representation into raw (hex string) data. Overloaded by specific classes."""
method = getattr(self, '_encode_hex', None)
if callable(method):
return method(abstract_data)
method = getattr(self, '_encode_bin', None)
if callable(method):
raw_bin_data = method(abstract_data)
return b2h(raw_bin_data)
raise NotImplementedError
class LinFixedEF(CardEF):
"""Linear Fixed EF (Entry File) in the smart card filesystem"""
@with_default_category('Linear Fixed EF Commands')
class ShellCommands(CommandSet):
def __init__(self):
super().__init__()
read_rec_parser = argparse.ArgumentParser()
read_rec_parser.add_argument('record_nr', type=int, help='Number of record to be read')
@cmd2.with_argparser(read_rec_parser)
def do_read_record(self, opts):
"""Read a record from a record-oriented EF"""
(data, sw) = self._cmd.rs.read_record(opts.record_nr)
self._cmd.poutput(data)
read_rec_dec_parser = argparse.ArgumentParser()
read_rec_dec_parser.add_argument('record_nr', type=int, help='Number of record to be read')
@cmd2.with_argparser(read_rec_dec_parser)
def do_read_record_decoded(self, opts):
"""Read + decode a record from a record-oriented EF"""
(data, sw) = self._cmd.rs.read_record_dec(opts.record_nr)
self._cmd.poutput(json.dumps(data, indent=4))
upd_rec_parser = argparse.ArgumentParser()
upd_rec_parser.add_argument('record_nr', type=int, help='Number of record to be read')
upd_rec_parser.add_argument('data', help='Data bytes (hex format) to write')
@cmd2.with_argparser(upd_rec_parser)
def do_update_record(self, opts):
"""Update (write) data to a record-oriented EF"""
(data, sw) = self._cmd.rs.update_record(opts.record_nr, opts.data)
self._cmd.poutput(data)
upd_rec_dec_parser = argparse.ArgumentParser()
upd_rec_dec_parser.add_argument('record_nr', type=int, help='Number of record to be read')
upd_rec_dec_parser.add_argument('data', help='Data bytes (hex format) to write')
@cmd2.with_argparser(upd_rec_dec_parser)
def do_update_record_decoded(self, opts):
"""Encode + Update (write) data to a record-oriented EF"""
(data, sw) = self._cmd.rs.update_record_dec(opts.record_nr, opts.data)
self._cmd.poutput(data)
def __init__(self, fid, sfid=None, name=None, desc=None, parent=None, rec_len={1,None}):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, parent=parent)
self.rec_len = rec_len
self.shell_commands = [self.ShellCommands()]
def decode_record_hex(self, raw_hex_data):
"""Decode raw (hex string) data into abstract representation. Overloaded by specific classes."""
method = getattr(self, '_decode_record_hex', None)
if callable(method):
return method(raw_hex_data)
raw_bin_data = h2b(raw_hex_data)
method = getattr(self, '_decode_record_bin', None)
if callable(method):
return method(raw_bin_data)
return {'raw': raw_bin_data.hex()}
def decode_record_bin(self, raw_bin_data):
"""Decode raw (binary) data into abstract representation. Overloaded by specific classes."""
method = getattr(self, '_decode_record_bin', None)
if callable(method):
return method(raw_bin_data)
raw_hex_data = b2h(raw_bin_data)
method = getattr(self, '_decode_record_hex', None)
if callable(method):
return method(raw_hex_data)
return {'raw': raw_hex_data}
def encode_record_hex(self, abstract_data):
"""Encode abstract representation into raw (hex string) data. Overloaded by specific classes."""
method = getattr(self, '_encode_record_hex', None)
if callable(method):
return method(abstract_data)
method = getattr(self, '_encode_record_bin', None)
if callable(method):
raw_bin_data = method(abstract_data)
return b2h(raww_bin_data)
raise NotImplementedError
def encode_record_bin(self, abstract_data):
"""Encode abstract representation into raw (binary) data. Overloaded by specific classes."""
method = getattr(self, '_encode_record_bin', None)
if callable(method):
return method(abstract_data)
method = getattr(self, '_encode_record_hex', None)
if callable(method):
return b2h(method(abstract_data))
raise NotImplementedError
class CyclicEF(LinFixedEF):
"""Cyclic EF (Entry File) in the smart card filesystem"""
# we don't really have any special support for those; just recycling LinFixedEF here
def __init__(self, fid, sfid=None, name=None, desc=None, parent=None, rec_len={1,None}):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, parent=parent, rec_len=rec_len)
class TransRecEF(TransparentEF):
"""Transparent EF (Entry File) containing fixed-size records.
These are the real odd-balls and mostly look like mistakes in the specification:
Specified as 'transparent' EF, but actually containing several fixed-length records
inside.
We add a special class for those, so the user only has to provide encoder/decoder functions
for a record, while this class takes care of split / merge of records.
"""
def __init__(self, fid, sfid=None, name=None, desc=None, parent=None, rec_len=None, size={1,None}):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, parent=parent, size=size)
self.rec_len = rec_len
def decode_record_hex(self, raw_hex_data):
"""Decode raw (hex string) data into abstract representation. Overloaded by specific classes."""
method = getattr(self, '_decode_record_hex', None)
if callable(method):
return method(raw_hex_data)
method = getattr(self, '_decode_record_bin', None)
if callable(method):
raw_bin_data = h2b(raw_hex_data)
return method(raw_bin_data)
return {'raw': raw_hex_data}
def decode_record_bin(self, raw_bin_data):
"""Decode raw (hex string) data into abstract representation. Overloaded by specific classes."""
method = getattr(self, '_decode_record_bin', None)
if callable(method):
return method(raw_bin_data)
raw_hex_data = b2h(raw_bin_data)
method = getattr(self, '_decode_record_hex', None)
if callable(method):
return method(raw_hex_data)
return {'raw': raw_hex_data}
def encode_record_hex(self, abstract_data):
"""Encode abstract representation into raw (hex string) data. Overloaded by specific classes."""
method = getattr(self, '_encode_record_hex', None)
if callable(method):
return method(abstract_data)
method = getattr(self, '_encode_record_bin', None)
if callable(method):
return h2b(method(abstract_data))
raise NotImplementedError
def encode_record_bin(self, abstract_data):
"""Encode abstract representation into raw (binary) data. Overloaded by specific classes."""
method = getattr(self, '_encode_record_bin', None)
if callable(method):
return method(abstract_data)
method = getattr(self, '_encode_record_hex', None)
if callable(method):
return h2b(method(abstract_data))
raise NotImplementedError
def _decode_bin(self, raw_bin_data):
chunks = [raw_bin_data[i:i+self.rec_len] for i in range(0, len(raw_bin_data), self.rec_len)]
return [self.decode_record_bin(x) for x in chunks]
def _encode_bin(self, abstract_data):
chunks = [self.encode_record_bin(x) for x in abstract_data]
# FIXME: pad to file size
return b''.join(chunks)
class RuntimeState(object):
"""Represent the runtime state of a session with a card."""
def __init__(self, card, profile):
self.mf = CardMF()
self.card = card
self.selected_file = self.mf
self.profile = profile
# add applications + MF-files from profile
for a in self.profile.applications:
self.mf.add_application(a)
for f in self.profile.files_in_mf:
self.mf.add_file(f)
def get_cwd(self):
"""Obtain the current working directory."""
if isinstance(self.selected_file, CardDF):
return self.selected_file
else:
return self.selected_file.parent
def get_application(self):
"""Obtain the currently selected application (if any)."""
# iterate upwards from selected file; check if any is an ADF
node = self.selected_file
while node.parent != node:
if isinstance(node, CardADF):
return node
node = node.parent
return None
def interpret_sw(self, sw):
"""Interpret the given SW relative to the currently selected Application
or the underlying profile."""
app = self.get_application()
if app:
# The application either comes with its own interpret_sw
# method or we will use the interpret_sw method from the
# card profile.
if hasattr(app, "interpret_sw"):
return app.interpret_sw(sw)
else:
return self.profile.interpret_sw(sw)
return app.interpret_sw(sw)
else:
return self.profile.interpret_sw(sw)
def select(self, name, cmd_app=None):
"""Change current directory"""
sels = self.selected_file.get_selectables()
if name in sels:
f = sels[name]
# unregister commands of old file
if cmd_app and self.selected_file.shell_commands:
for c in self.selected_file.shell_commands:
cmd_app.unregister_command_set(c)
try:
if isinstance(f, CardADF):
(data, sw) = self.card._scc.select_adf(f.aid)
else:
(data, sw) = self.card._scc.select_file(f.fid)
self.selected_file = f
except SwMatchError as swm:
k = self.interpret_sw(swm.sw_actual)
if not k:
raise(swm)
raise RuntimeError("%s: %s - %s" % (swm.sw_actual, k[0], k[1]))
# register commands of new file
if cmd_app and self.selected_file.shell_commands:
for c in self.selected_file.shell_commands:
cmd_app.register_command_set(c)
return f.decode_select_response(data)
#elif looks_like_fid(name):
else:
raise ValueError("Cannot select unknown %s" % (name))
def read_binary(self, length=None, offset=0):
if not isinstance(self.selected_file, TransparentEF):
raise TypeError("Only works with TransparentEF")
return self.card._scc.read_binary(self.selected_file.fid, length, offset)
def read_binary_dec(self):
(data, sw) = self.read_binary()
dec_data = self.selected_file.decode_hex(data)
print("%s: %s -> %s" % (sw, data, dec_data))
return (dec_data, sw)
def update_binary(self, data_hex, offset=0):
if not isinstance(self.selected_file, TransparentEF):
raise TypeError("Only works with TransparentEF")
return self.card._scc.update_binary(self.selected_file.fid, data_hex, offset)
def update_binary_dec(self, data):
data_hex = self.selected_file.encode_hex(data)
print("%s -> %s" % (data, data_hex))
return self.update_binary(data_hex)
def read_record(self, rec_nr=0):
if not isinstance(self.selected_file, LinFixedEF):
raise TypeError("Only works with Linear Fixed EF")
# returns a string of hex nibbles
return self.card._scc.read_record(self.selected_file.fid, rec_nr)
def read_record_dec(self, rec_nr=0):
(data, sw) = self.read_record(rec_nr)
return (self.selected_file.decode_record_hex(data), sw)
def update_record(self, rec_nr, data_hex):
if not isinstance(self.selected_file, LinFixedEF):
raise TypeError("Only works with Linear Fixed EF")
return self.card._scc.update_record(self.selected_file.fid, rec_nr, data_hex)
def update_record_dec(self, rec_nr, data):
hex_data = self.selected_file.encode_record_hex(data)
return self.update_record(self, rec_nr, data_hex)
class FileData(object):
"""Represent the runtime, on-card data."""
def __init__(self, fdesc):
self.desc = fdesc
self.fcp = None
def interpret_sw(sw_data, sw):
"""Interpret a given status word within the profile. Returns tuple of
two strings"""
for class_str, swdict in sw_data.items():
# first try direct match
if sw in swdict:
return (class_str, swdict[sw])
# next try wildcard matches
for pattern, descr in swdict.items():
if sw_match(sw, pattern):
return (class_str, descr)
return None
class CardApplication(object):
"""A card application is represented by an ADF (with contained hierarchy) and optionally
some SW definitions."""
def __init__(self, name, adf=None, sw={}):
self.name = name
self.adf = adf
self.sw = sw
def __str__(self):
return "APP(%s)" % (self.name)
def interpret_sw(self, sw):
"""Interpret a given status word within the application. Returns tuple of
two strings"""
return interpret_sw(self.sw, sw)
class CardProfile(object):
"""A Card Profile describes a card, it's filessystem hierarchy, an [initial] list of
applications as well as profile-specific SW and shell commands. Every card has
one card profile, but there may be multiple applications within that profile."""
def __init__(self, name, desc=None, files_in_mf=[], sw=[], applications=[], shell_cmdsets=[]):
self.name = name
self.desc = desc
self.files_in_mf = files_in_mf
self.sw = sw
self.applications = applications
self.shell_cmdsets = shell_cmdsets
def __str__(self):
return self.name
def add_application(self, app):
self.applications.add(app)
def interpret_sw(self, sw):
"""Interpret a given status word within the profile. Returns tuple of
two strings"""
return interpret_sw(self.sw, sw)
######################################################################
if __name__ == '__main__':
mf = CardMF()
adf_usim = ADF('a0000000871002', name='ADF_USIM')
mf.add_application(adf_usim)
df_pb = CardDF('5f3a', name='DF.PHONEBOOK')
adf_usim.add_file(df_pb)
adf_usim.add_file(TransparentEF('6f05', name='EF.LI', size={2,16}))
adf_usim.add_file(TransparentEF('6f07', name='EF.IMSI', size={9,9}))
rss = RuntimeState(mf, None)
interp = code.InteractiveConsole(locals={'mf':mf, 'rss':rss})
interp.interact()

297
pySim/ts_102_221.py Normal file
View File

@ -0,0 +1,297 @@
# coding=utf-8
"""Utilities / Functions related to ETSI TS 102 221, the core UICC spec.
(C) 2021 by Harald Welte <laforge@osmocom.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from pytlv.TLV import *
from struct import pack, unpack
from pySim.utils import *
from pySim.filesystem import *
FCP_TLV_MAP = {
'82': 'file_descriptor',
'83': 'file_identifier',
'84': 'df_name',
'A5': 'proprietary_info',
'8A': 'life_cycle_status_int',
'8B': 'security_attrib_ref_expanded',
'8C': 'security_attrib_compact',
'AB': 'security_attrib_espanded',
'C6': 'pin_status_template_do',
'80': 'file_size',
'81': 'total_file_size',
'88': 'short_file_id',
}
# ETSI TS 102 221 11.1.1.4.6
FCP_Proprietary_TLV_MAP = {
'80': 'uicc_characteristics',
'81': 'application_power_consumption',
'82': 'minimum_app_clock_freq',
'83': 'available_memory',
'84': 'file_details',
'85': 'reserved_file_size',
'86': 'maximum_file_size',
'87': 'suported_system_commands',
'88': 'specific_uicc_env_cond',
'89': 'p2p_cat_secured_apdu',
# Additional private TLV objects (bits b7 and b8 of the first byte of the tag set to '1')
}
# ETSI TS 102 221 11.1.1.4.3
def interpret_file_descriptor(in_hex):
in_bin = h2b(in_hex)
out = {}
ft_dict = {
0: 'working_ef',
1: 'internal_ef',
7: 'df'
}
fs_dict = {
0: 'no_info_given',
1: 'transparent',
2: 'linear_fixed',
6: 'cyclic',
}
fdb = in_bin[0]
ftype = (fdb >> 3) & 7
fstruct = fdb & 7
out['shareable'] = True if fdb & 0x40 else False
out['file_type'] = ft_dict[ftype] if ftype in ft_dict else ftype
out['structure'] = fs_dict[fstruct] if fstruct in fs_dict else fstruct
if len(in_bin) >= 5:
out['record_len'] = int.from_bytes(in_bin[2:4], 'big')
out['num_of_rec'] = int.from_bytes(in_bin[4:5], 'big')
return out
# ETSI TS 102 221 11.1.1.4.9
def interpret_life_cycle_sts_int(in_hex):
lcsi = int(in_hex, 16)
if lcsi == 0x00:
return 'no_information'
elif lcsi == 0x01:
return 'creation'
elif lcsi == 0x03:
return 'initialization'
elif lcsi & 0x05 == 0x05:
return 'operational_activated'
elif lcsi & 0x05 == 0x04:
return 'operational_deactivated'
elif lcsi & 0xc0 == 0xc0:
return 'termination'
else:
return in_hex
# ETSI TS 102 221 11.1.1.4.10
FCP_Pin_Status_TLV_MAP = {
'90': 'ps_do',
'95': 'usage_qualifier',
'83': 'key_reference',
}
def interpret_ps_templ_do(in_hex):
# cannot use the 'TLV' parser due to repeating tags
#psdo_tlv = TLV(FCP_Pin_Status_TLV_MAP)
#return psdo_tlv.parse(in_hex)
return in_hex
# 'interpreter' functions for each tag
FCP_interpreter_map = {
'80': lambda x: int(x, 16),
'82': interpret_file_descriptor,
'8A': interpret_life_cycle_sts_int,
'C6': interpret_ps_templ_do,
}
FCP_prorietary_interpreter_map = {
'83': lambda x: int(x, 16),
}
# pytlv unfortunately doesn't have a setting using which we can make it
# accept unknown tags. It also doesn't raise a specific exception type but
# just the generic ValueError, so we cannot ignore those either. Instead,
# we insert a dict entry for every possible proprietary tag permitted
def fixup_fcp_proprietary_tlv_map(tlv_map):
if 'D0' in tlv_map:
return
for i in range(0xd0, 0xff):
i_hex = i2h([i]).upper()
tlv_map[i_hex] = 'proprietary_' + i_hex
def tlv_key_replace(inmap, indata):
def newkey(inmap, key):
if key in inmap:
return inmap[key]
else:
return key
return {newkey(inmap, d[0]): d[1] for d in indata.items()}
def tlv_val_interpret(inmap, indata):
def newval(inmap, key, val):
if key in inmap:
return inmap[key](val)
else:
return val
return {d[0]: newval(inmap, d[0], d[1]) for d in indata.items()}
# ETSI TS 102 221 Section 11.1.1.3
def decode_select_response(resp_hex):
fixup_fcp_proprietary_tlv_map(FCP_Proprietary_TLV_MAP)
resp_hex = resp_hex.upper()
# outer layer
fcp_base_tlv = TLV(['62'])
fcp_base = fcp_base_tlv.parse(resp_hex)
# actual FCP
fcp_tlv = TLV(FCP_TLV_MAP)
fcp = fcp_tlv.parse(fcp_base['62'])
# further decode the proprietary information
if fcp['A5']:
prop_tlv = TLV(FCP_Proprietary_TLV_MAP)
prop = prop_tlv.parse(fcp['A5'])
fcp['A5'] = tlv_val_interpret(FCP_prorietary_interpreter_map, prop)
fcp['A5'] = tlv_key_replace(FCP_Proprietary_TLV_MAP, fcp['A5'])
# finally make sure we get human-readable keys in the output dict
r = tlv_val_interpret(FCP_interpreter_map, fcp)
return tlv_key_replace(FCP_TLV_MAP, r)
# TS 102 221 Section 13.1
class EF_DIR(LinFixedEF):
def __init__(self, fid='2f00', sfid=0x1e, name='EF.DIR', desc='Application Directory'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={5,54})
def _decode_record_hex(self, raw_hex_data):
raw_hex_data = raw_hex_data.upper()
atempl_base_tlv = TLV(['61'])
atempl_base = atempl_base_tlv.parse(raw_hex_data)
atempl_TLV_MAP = {'4F': 'aid_value', 50:'label'}
atempl_tlv = TLV(atempl_TLV_MAP)
atempl = atempl_tlv.parse(atempl_base['61'])
# FIXME: "All other Dos are according to ISO/IEC 7816-4"
return tlv_key_replace(atempl_TLV_MAP, atempl)
# TS 102 221 Section 13.2
class EF_ICCID(TransparentEF):
def __init__(self, fid='2fe2', sfid=0x02, name='EF.ICCID', desc='ICC Identification'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size={10,10})
def _decode_hex(self, raw_hex):
return {'iccid': dec_iccid(raw_hex)}
def _encode_hex(self, abstract):
return enc_iccid(abstract['iccid'])
# TS 102 221 Section 13.3
class EF_PL(TransRecEF):
def __init__(self, fid='2f05', sfid=0x05, name='EF.PL', desc='Preferred Languages'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=2, size={2,None})
# TS 102 221 Section 13.4
class EF_ARR(LinFixedEF):
def __init__(self, fid='2f06', sfid=0x06, name='EF.ARR', desc='Access Rule Reference'):
super().__init__(fid, sfid=sfid, name=name, desc=desc)
# TS 102 221 Section 13.6
class EF_UMPC(TransparentEF):
def __init__(self, fid='2f08', sfid=0x08, name='EF.UMPC', desc='UICC Maximum Power Consumption'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size={5,5})
class CardProfileUICC(CardProfile):
def __init__(self):
files = [
EF_DIR(),
EF_ICCID(),
EF_PL(),
EF_ARR(),
# FIXME: DF.CD
EF_UMPC(),
]
sw = {
'Normal': {
'9000': 'Normal ending of the command',
'91xx': 'Normal ending of the command, with extra information from the proactive UICC containing a command for the terminal',
'92xx': 'Normal ending of the command, with extra information concerning an ongoing data transfer session',
},
'Postponed processing': {
'9300': 'SIM Application Toolkit is busy. Command cannot be executed at present, further normal commands are allowed',
},
'Warnings': {
'6200': 'No information given, state of non-volatile memory unchanged',
'6281': 'Part of returned data may be corrupted',
'6282': 'End of file/record reached before reading Le bytes or unsuccessful search',
'6283': 'Selected file invalidated',
'6284': 'Selected file in termination state',
'62f1': 'More data available',
'62f2': 'More data available and proactive command pending',
'62f3': 'Response data available',
'63f1': 'More data expected',
'63f2': 'More data expected and proactive command pending',
'63cx': 'Command successful but after using an internal update retry routine X times',
},
'Execution errors': {
'6400': 'No information given, state of non-volatile memory unchanged',
'6500': 'No information given, state of non-volatile memory changed',
'6581': 'Memory problem',
},
'Checking errors': {
'6700': 'Wrong length',
'67xx': 'The interpretation of this status word is command dependent',
'6b00': 'Wrong parameter(s) P1-P2',
'6d00': 'Instruction code not supported or invalid',
'6e00': 'Class not supported',
'6f00': 'Technical problem, no precise diagnosis',
'6fxx': 'The interpretation of this status word is command dependent',
},
'Functions in CLA not supported': {
'6800': 'No information given',
'6881': 'Logical channel not supported',
'6882': 'Secure messaging not supported',
},
'Command not allowed': {
'6900': 'No information given',
'6981': 'Command incompatible with file structure',
'6982': 'Security status not satisfied',
'6983': 'Authentication/PIN method blocked',
'6984': 'Referenced data invalidated',
'6985': 'Conditions of use not satisfied',
'6986': 'Command not allowed (no EF selected)',
'6989': 'Command not allowed - secure channel - security not satisfied',
},
'Wrong parameters': {
'6a80': 'Incorrect parameters in the data field',
'6a81': 'Function not supported',
'6a82': 'File not found',
'6a83': 'Record not found',
'6a84': 'Not enough memory space',
'6a86': 'Incorrect parameters P1 to P2',
'6a87': 'Lc inconsistent with P1 to P2',
'6a88': 'Referenced data not found',
},
'Application errors': {
'9850': 'INCREASE cannot be performed, max value reached',
'9862': 'Authentication error, application specific',
'9863': 'Security session or association expired',
'9864': 'Minimum UICC suspension time is too long',
},
}
super().__init__('UICC', 'ETSI TS 102 221', files, sw)

View File

@ -263,3 +263,134 @@ EF_USIM_ADF_map = {
'ePDGIdEm': '6FF5',
'ePDGSelectionEm': '6FF6',
}
######################################################################
# ADF.USIM
######################################################################
from pySim.filesystem import *
from pySim.ts_51_011 import EF_IMSI, EF_xPLMNwAcT, EF_SPN, EF_CBMI, EF_ACC, EF_PLMNsel, EF_AD
from pySim.ts_51_011 import EF_CBMID, EF_ECC, EF_CBMIR
import pySim.ts_102_221
class EF_LI(TransRecEF):
def __init__(self, fid='6f05', sfid=None, name='EF.LI', size={2,None}, rec_len=2,
desc='Language Indication'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
def _decode_record_bin(self, in_bin):
if in_bin == b'\xff\xff':
return None
else:
# officially this is 7-bit GSM alphabet with one padding bit in each byte
return in_bin.decode('ascii')
def _encode_record_bin(self, in_json):
if in_json == None:
return b'\xff\xff'
else:
# officially this is 7-bit GSM alphabet with one padding bit in each byte
return in_json.encode('ascii')
class EF_Keys(TransparentEF):
def __init__(self, fid='6f08', sfid=0x08, name='EF.Keys', size={33,33},
desc='Ciphering and Integrity Keys'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
def _decode_bin(self, in_bin):
return {'ksi': in_bin[0],
'ck': b2h(in_bin[1:17]),
'ik': b2h(in_bin[17:33])}
def _encode_bin(self, in_json):
return h2b(in_json['ksi']) + h2b(in_json['ck']) + h2b(in_json['ik'])
# TS 31.103 Section 4.2.7
class EF_UST(TransparentEF):
def __init__(self, fid='6f38', sfid=0x04, name='EF.UST', desc='USIM Service Table'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size={1,17})
# add those commands to the general commands of a TransparentEF
self.shell_commands += [self.AddlShellCommands()]
def _decode_bin(self, in_bin):
ret = []
for i in range (0, len(in_bin)):
byte = in_bin[i]
for bitno in range(0,7):
if byte & (1 << bitno):
ret.append(i * 8 + bitno + 1)
return ret
def _encode_bin(self, in_json):
# FIXME: size this to length of file
ret = bytearray(20)
for srv in in_json:
print("srv=%d"%srv)
srv = srv-1
byte_nr = srv // 8
# FIXME: detect if service out of range was selected
bit_nr = srv % 8
ret[byte_nr] |= (1 << bit_nr)
return ret
@with_default_category('File-Specific Commands')
class AddlShellCommands(CommandSet):
def __init__(self):
super().__init__()
def do_ust_service_activate(self, arg):
"""Activate a service within EF.UST"""
self._cmd.card.update_ust(int(arg), 1)
def do_ust_service_deactivate(self, arg):
"""Deactivate a service within EF.UST"""
self._cmd.card.update_ust(int(arg), 0)
class ADF_USIM(CardADF):
def __init__(self, aid='a0000000871002', name='ADF.USIM', fid=None, sfid=None,
desc='USIM Application'):
super().__init__(aid=aid, fid=fid, sfid=sfid, name=name, desc=desc)
self.shell_commands = [self.ShellCommands()]
files = [
EF_LI(sfid=0x02),
EF_IMSI(sfid=0x07),
EF_Keys(),
EF_Keys('6f09', 0x09, 'EF.KeysPS', desc='Ciphering and Integrity Keys for PS domain'),
EF_xPLMNwAcT('6f60', 0x0a, 'EF.PLMNwAcT',
'User controlled PLMN Selector with Access Technology'),
TransparentEF('6f31', 0x12, 'EF.HPPLMN', 'Higher Priority PLMN search period'),
# EF.ACMmax
EF_UST(),
CyclicEF('6f39', None, 'EF.ACM', 'Accumulated call meter', rec_len={3,3}),
TransparentEF('6f3e', None, 'EF.GID1', 'Group Identifier Level 1'),
TransparentEF('6f3f', None, 'EF.GID2', 'Group Identifier Level 2'),
EF_SPN(),
TransparentEF('6f41', None, 'EF.PUCT', 'Price per unit and currency table', size={5,5}),
EF_CBMI(),
EF_ACC(sfid=0x06),
EF_PLMNsel('6f7b', 0x0d, 'EF.FPLMN', 'Forbidden PLMNs', size={12,None}),
TransparentEF('6f7e', 0x0b, 'EF.LOCI', 'Locationn information', size={11,11}),
EF_AD(sfid=0x03),
EF_CBMID(sfid=0x0e),
EF_ECC(sfid=0x01),
EF_CBMIR(),
]
self.add_files(files)
def decode_select_response(self, data_hex):
return pySim.ts_102_221.decode_select_response(data_hex)
@with_default_category('File-Specific Commands')
class ShellCommands(CommandSet):
def __init__(self):
super().__init__()
# TS 31.102 Section 7.3
sw_usim = {
'Security management': {
'9862': 'Authentication error, incorrect MAC',
'9864': 'Authentication error, security context not supported',
'9865': 'Key freshness failure',
'9866': 'Authentication error, no memory space available',
'9867': 'Authentication error, no memory space available in EF MUK',
}
}
CardApplicationUSIM = CardApplication('USIM', adf=ADF_USIM(), sw=sw_usim)

View File

@ -6,6 +6,7 @@ Various constants from ETSI TS 131 103 V14.2.0
#
# Copyright (C) 2020 Supreeth Herle <herlesupreeth@gmail.com>
# Copyright (C) 2021 Harald Welte <laforge@osmocom.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -21,6 +22,11 @@ Various constants from ETSI TS 131 103 V14.2.0
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from pySim.filesystem import *
from pySim.utils import *
from pySim.ts_51_011 import EF_AD
import pySim.ts_102_221
# Mapping between ISIM Service Number and its description
EF_IST_map = {
1: 'P-CSCF address',
@ -66,3 +72,130 @@ EF_ISIM_ADF_map = {
'XCAPConfigData': '6FFC',
'WebRTCURI': '6FFA'
}
# TS 31.103 Section 4.2.2
class EF_IMPI(TransparentEF):
def __init__(self, fid='6f02', sfid=0x02, name='EF.IMPI', desc='IMS private user identity'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.3
class EF_DOMAIN(TransparentEF):
def __init__(self, fid='6f05', sfid=0x05, name='EF.DOMAIN', desc='Home Network Domain Name'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.4
class EF_IMPU(LinFixedEF):
def __init__(self, fid='6f04', sfid=0x04, name='EF.IMPU', desc='IMS public user identity'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.6
class EF_ARR(LinFixedEF):
def __init__(self, fid='6f06', sfid=0x06, name='EF.ARR', desc='Access Rule Reference'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.7
class EF_IST(TransparentEF):
def __init__(self, fid='6f07', sfid=0x07, name='EF.IST', desc='ISIM Service Table'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, size={1,4})
# add those commands to the general commands of a TransparentEF
self.shell_commands += [self.AddlShellCommands()]
@with_default_category('File-Specific Commands')
class AddlShellCommands(CommandSet):
def __init__(self):
super().__init__()
def do_ist_service_activate(self, arg):
"""Activate a service within EF.IST"""
self._cmd.card.update_ist(int(arg), 1)
def do_ist_service_deactivate(self, arg):
"""Deactivate a service within EF.IST"""
self._cmd.card.update_ist(int(arg), 0)
# TS 31.103 Section 4.2.8
class EF_PCSCF(LinFixedEF):
def __init__(self, fid='6f09', sfid=None, name='EF.P-CSCF', desc='P-CSCF Address'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
def _decode_record_hex(self, raw_hex):
# FIXME: this doesn't do JSON output
return dec_addr_tlv(raw_hex)
def _encode_record_hex(self, json_in):
return enc_addr_tlv(json_in)
# TS 31.103 Section 4.2.9
class EF_GBABP(LinFixedEF):
def __init__(self, fid='6fd5', sfid=None, name='EF.GBABP', desc='GBA Bootstrappng'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.10
class EF_GBANL(LinFixedEF):
def __init__(self, fid='6fd7', sfid=None, name='EF.GBANL', desc='GBA NAF List'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.11
class EF_NAFKCA(LinFixedEF):
def __init__(self, fid='6fdd', sfid=None, name='EF.NAFKCA', desc='NAF Key Centre Address'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.16
class EF_UICCIARI(LinFixedEF):
def __init__(self, fid='6fe7', sfid=None, name='EF.UICCIARI', desc='UICC IARI'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.18
class EF_IMSConfigData(TransparentEF):
def __init__(self, fid='6ff8', sfid=None, name='EF.IMSConfigData', desc='IMS Configuration Data'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.19
class EF_XCAPConfigData(TransparentEF):
def __init__(self, fid='6ffc', sfid=None, name='EF.XCAPConfigData', desc='XCAP Configuration Data'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
# TS 31.103 Section 4.2.20
class EF_WebRTCURI(TransparentEF):
def __init__(self, fid='6ffa', sfid=None, name='EF.WebRTCURI', desc='WebRTC URI'):
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc)
class ADF_ISIM(CardADF):
def __init__(self, aid='a0000000871004', name='ADF.ISIM', fid=None, sfid=None,
desc='ISIM Application'):
super().__init__(aid=aid, fid=fid, sfid=sfid, name=name, desc=desc)
files = [
EF_IMPI(),
EF_DOMAIN(),
EF_IMPU(),
EF_AD(),
EF_ARR(),
EF_IST(),
EF_PCSCF(),
EF_GBABP(),
EF_GBANL(),
EF_NAFKCA(),
# SMS
# SMSS
# SMSR
#EF_SMSP(),
EF_UICCIARI(),
# FromPreferred
EF_IMSConfigData(),
EF_XCAPConfigData(),
EF_WebRTCURI(),
]
self.add_files(files)
def decode_select_response(self, data_hex):
return pySim.ts_102_221.decode_select_response(data_hex)
# TS 31.103 Section 7.1
sw_isim = {
'Security management': {
'9862': 'Authentication error, incorrect MAC',
'9864': 'Authentication error, security context not supported',
}
}
CardApplicationISIM = CardApplication('ISIM', adf=ADF_ISIM(), sw=sw_isim)

View File

@ -1,10 +1,15 @@
# -*- coding: utf-8 -*-
""" Various constants from ETSI TS 151.011
""" Various constants from ETSI TS 151.011 +
Representation of the GSM SIM/USIM/ISIM filesystem hierarchy.
The File (and its derived classes) uses the classes of pySim.filesystem in
order to describe the files specified in the relevant ETSI + 3GPP specifications.
"""
#
# Copyright (C) 2017 Alexander.Chemeris <Alexander.Chemeris@gmail.com>
# Copyright (C) 2021 Harald Welte <laforge@osmocom.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@ -323,3 +328,301 @@ EF_AD_mode_map = {
'02' : 'maintenance (off line)',
'04' : 'cell test operation',
}
from pySim.utils import *
from struct import pack, unpack
from pySim.filesystem import *
import pySim.ts_102_221
######################################################################
# DF.TELECOM
######################################################################
# TS 51.011 Section 10.5.1
class EF_ADN(LinFixedEF):
def __init__(self, fid='6f3a', sfid=None, name='EF.ADN', desc='Abbreviated Dialing Numbers'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={14, 30})
def _decode_record_bin(self, raw_bin_data):
alpha_id_len = len(raw_bin_data) - 14
alpha_id = raw_bin_data[:alpha_id_len]
u = unpack('!BB10sBB', raw_bin_data[-14:])
return {'alpha_id': alpha_id, 'len_of_bcd': u[0], 'ton_npi': u[1],
'dialing_nr': u[2], 'cap_conf_id': u[3], 'ext1_record_id': u[4]}
# TS 51.011 Section 10.5.5
class EF_MSISDN(LinFixedEF):
def __init__(self, fid='6f4f', sfid=None, name='EF.MSISDN', desc='MSISDN'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={15, None})
def _decode_record_hex(self, raw_hex_data):
return {'msisdn': dec_msisdn(raw_hex_data)}
def _encode_record_hex(self, abstract):
return enc_msisdn(abstract['msisdn'])
# TS 51.011 Section 10.5.6
class EF_SMSP(LinFixedEF):
def __init__(self, fid='6f42', sfid=None, name='EF.SMSP', desc='Short message service parameters'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={28, None})
class DF_TELECOM(CardDF):
def __init__(self, fid='7f10', name='DF.TELECOM', desc=None):
super().__init__(fid=fid, name=name, desc=desc)
files = [
EF_ADN(),
# FDN, SMS, CCP, ECCP
EF_MSISDN(),
EF_SMSP(),
# SMSS, LND, SDN, EXT1, EXT2, EXT3, BDN, EXT4, SMSR, CMI
]
self.add_files(files)
def decode_select_response(self, data_hex):
return decode_select_response(data_hex)
######################################################################
# DF.GSM
######################################################################
# TS 51.011 Section 10.3.1
class EF_LP(TransRecEF):
def __init__(self, fid='6f05', sfid=None, name='EF.LP', size={1,None}, rec_len=1,
desc='Language Preference'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
def _decode_record_bin(self, in_bin):
return b2h(in_bin)
def _encode_record_bin(self, in_json):
return h2b(in_json)
# TS 51.011 Section 10.3.2
class EF_IMSI(TransparentEF):
def __init__(self, fid='6f07', sfid=None, name='EF.IMSI', desc='IMSI', size={9,9}):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
def _decode_hex(self, raw_hex):
return {'imsi': dec_imsi(raw_hex)}
def _encode_hex(self, abstract):
return enc_imsi(abstract['imsi'])
# TS 51.011 Section 10.3.4
class EF_PLMNsel(TransRecEF):
def __init__(self, fid='6f30', sfid=None, name='EF.PLMNsel', desc='PLMN selector',
size={24,None}, rec_len=3):
super().__init__(fid, name=name, sfid=sfid, desc=desc, size=size, rec_len=rec_len)
def _decode_record_hex(self, in_hex):
if in_hex[:6] == "ffffff":
return None
else:
return dec_plmn(in_hex)
def _encode_record_hex(self, in_json):
if in_json == None:
return "ffffff"
else:
return enc_plmn(in_json['mcc'], in_json['mnc'])
# TS 51.011 Section 10.3.7
class EF_ServiceTable(TransparentEF):
def __init__(self, fid, sfid, name, desc, size, table):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
self.table = table
def _decode_bin(self, raw_bin):
ret = {}
for i in range(0, len(raw_bin)*4):
service_nr = i+1
byte = int(raw_bin[i//4])
bit_offset = (i % 4) * 2
bits = (byte >> bit_offset) & 3
ret[service_nr] = {
'description': self.table[service_nr] or None,
'allocated': True if bits & 1 else False,
'activated': True if bits & 2 else False,
}
return ret
# TODO: encoder
# TS 51.011 Section 10.3.11
class EF_SPN(TransparentEF):
def __init__(self, fid='6f46', sfid=None, name='EF.SPN', desc='Service Provider Name', size={17,17}):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
def _decode_hex(self, raw_hex):
return {'spn': dec_spn(raw_hex)}
def _encode_hex(self, abstract):
return enc_spn(abstract['spn'])
# TS 51.011 Section 10.3.13
class EF_CBMI(TransRecEF):
def __init__(self, fid='6f45', sfid=None, name='EF.CBMI', size={2,None}, rec_len=2,
desc='Cell Broadcast message identifier selection'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
# TS 51.011 Section 10.3.15
class EF_ACC(TransparentEF):
def __init__(self, fid='6f78', sfid=None, name='EF.ACC', desc='Access Control Class', size={2,2}):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
def _decode_bin(self, raw_bin):
return {'acc': unpack('!H', raw_bin)[0]}
def _encode_bin(self, abstract):
return pack('!H', abstract['acc'])
# TS 51.011 Section 10.3.18
class EF_AD(TransparentEF):
OP_MODE = {
0x00: 'normal operation',
0x80: 'type approval operations',
0x01: 'normal operation + specific facilities',
0x81: 'type approval + specific facilities',
0x02: 'maintenance (off line)',
0x04: 'cell test operation',
}
def __init__(self, fid='6fad', sfid=None, name='EF.AD', desc='Administrative Data', size={3,4}):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size)
def _decode_bin(self, raw_bin):
u = unpack('!BH', raw_bin[:3])
# TS 51.011 Section 10.3.13
class EF_CBMID(EF_CBMI):
def __init__(self, fid='6f48', sfid=None, name='EF.CBMID', size={2,None}, rec_len=2,
desc='Cell Broadcast Message Identifier for Data Download'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
# TS 51.011 Section 10.3.26
class EF_ECC(LinFixedEF):
def __init__(self, fid='6fb7', sfid=None, name='EF.ECC', desc='Emergency Call Codes'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len={4, 20})
# TS 51.011 Section 10.3.28
class EF_CBMIR(TransRecEF):
def __init__(self, fid='6f50', sfid=None, name='EF.CBMIR', size={4,None}, rec_len=4,
desc='Cell Broadcast message identifier range selection'):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
# TS 51.011 Section 10.3.35..37
class EF_xPLMNwAcT(TransRecEF):
def __init__(self, fid, sfid=None, name=None, desc=None, size={40,None}, rec_len=5):
super().__init__(fid, sfid=sfid, name=name, desc=desc, size=size, rec_len=rec_len)
def _decode_record_hex(self, in_hex):
if in_hex[:6] == "ffffff":
return None
else:
return dec_xplmn_w_act(in_hex)
def _encode_record_hex(self, in_json):
if in_json == None:
return "ffffff0000"
else:
hplmn = enc_plmn(in_json['mcc'], in_json['mnc'])
act = self.enc_act(in_json['act'])
return hplmn + act
@staticmethod
def enc_act(in_list):
u16 = 0
# first the simple ones
if 'UTRAN' in in_list:
u16 |= 0x8000
if 'NG-RAN' in in_list:
u16 |= 0x0800
if 'GSM COMPACT' in in_list:
u16 |= 0x0040
if 'cdma2000 HRPD' in in_list:
u16 |= 0x0020
if 'cdma2000 1xRTT' in in_list:
u16 |= 0x0010
# E-UTRAN
if 'E-UTRAN WB-S1' and 'E-UTRAN NB-S1' in in_list:
u16 |= 0x7000 # WB-S1 and NB-S1
elif 'E-UTRAN NB-S1' in in_list:
u16 |= 0x6000 # only WB-S1
elif 'E-UTRAN NB-S1' in in_list:
u16 |= 0x5000 # only NB-S1
# GSM mess
if 'GSM' in in_list and 'EC-GSM-IoT' in in_list:
u16 |= 0x008C
elif 'GSM' in in_list:
u16 |= 0x0084
elif 'EC-GSM-IuT' in in_list:
u16 |= 0x0088
return '%04X'%(u16)
class DF_GSM(CardDF):
def __init__(self, fid='7f20', name='DF.GSM', desc='GSM Network related files'):
super().__init__(fid=fid, name=name, desc=desc)
files = [
EF_LP(),
EF_IMSI(),
TransparentEF('5f20', None, 'EF.Kc', 'Ciphering key Kc'),
EF_PLMNsel(),
TransparentEF('6f31', None, 'EF.HPPLMN', 'Higher Priority PLMN search period'),
# ACMmax
EF_ServiceTable('6f37', None, 'EF.SST', 'SIM service table', table=EF_SST_map, size={2,16}),
CyclicEF('6f39', None, 'EF.ACM', 'Accumulated call meter', rec_len={4,3}),
TransparentEF('6f3e', None, 'EF.GID1', 'Group Identifier Level 1'),
TransparentEF('6f3f', None, 'EF.GID2', 'Group Identifier Level 2'),
EF_SPN(),
TransparentEF('6f41', None, 'EF.PUCT', 'Price per unit and currency table', size={5,5}),
EF_CBMI(),
TransparentEF('6f7f', None, 'EF.BCCH', 'Broadcast control channels', size={16,16}),
EF_ACC(),
EF_PLMNsel('6f7b', None, 'EF.FPLMN', 'Forbidden PLMNs', size={12,12}),
TransparentEF('6f7e', None, 'EF.LOCI', 'Locationn information', size={11,11}),
EF_AD(),
TransparentEF('6fa3', None, 'EF.Phase', 'Phase identification', size={1,1}),
# TODO EF.VGCS VGCSS, VBS, VBSS, eMLPP, AAeM
EF_CBMID(),
EF_ECC(),
EF_CBMIR(),
# DCK, CNL, NIA, KcGRS, LOCIGPRS, SUME
EF_xPLMNwAcT('6f60', None, 'EF.PLMNwAcT',
'User controlled PLMN Selector with Access Technology'),
EF_xPLMNwAcT('6f61', None, 'EF.OPLMNwAcT',
'Operator controlled PLMN Selector with Access Technology'),
EF_xPLMNwAcT('6f62', None, 'EF.HPLMNwAcT', 'HPLMN Selector with Access Technology'),
# CPBCCH, InvScan, PNN, OPL, MBDN, MBI, MWIS, CFIS, EXT5, EXT6, EXT7, SPDI, MMSN, EXT8
# MMSICP, MMSUP, MMSUCP
]
self.add_files(files)
def decode_select_response(self, data_hex):
return decode_select_response(data_hex)
def decode_select_response(resp_hex):
resp_bin = h2b(resp_hex)
if resp_bin[0] == 0x62:
return pySim.ts_102_221.decode_select_response(resp_hex)
struct_of_file_map = {
0: 'transparent',
1: 'linear_fixed',
3: 'cyclic'
}
type_of_file_map = {
1: 'mf',
2: 'df',
4: 'working_ef'
}
ret = {
'file_descriptor': {},
'proprietary_info': {},
}
ret['file_id'] = b2h(resp_bin[4:6])
ret['proprietary_info']['available_memory'] = int.from_bytes(resp_bin[2:4], 'big')
file_type = type_of_file_map[resp_bin[6]] if resp_bin[6] in type_of_file_map else resp_bin[6]
ret['file_descriptor']['file_type'] = file_type
if file_type in ['mf', 'df']:
ret['file_characteristics'] = b2h(resp_bin[13])
ret['num_direct_child_df'] = int(resp_bin[14], 16)
ret['num_direct_child_ef'] = int(resp_bin[15], 16)
ret['num_chv_unbkock_adm_codes'] = int(resp_bin[16])
# CHV / UNBLOCK CHV stats
elif file_type in ['working_ef']:
file_struct = struct_of_file_map[resp_bin[13]] if resp_bin[13] in struct_of_file_map else resp_bin[13]
ret['file_descriptor']['structure'] = file_struct
ret['access_conditions'] = b2h(resp_bin[8:10])
if resp_bin[11] & 0x01 == 0:
ret['life_cycle_status_int'] = 'operational_activated'
elif resp_bin[11] & 0x04:
ret['life_cycle_status_int'] = 'operational_deactivated'
else:
ret['life_cycle_status_int'] = 'terminated'
return ret
CardProfileSIM = CardProfile('SIM', desc='GSM SIM Card', files_in_mf=[DF_TELECOM(), DF_GSM()])