2021-01-08 22:29:35 +00:00
|
|
|
# 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.
|
|
|
|
"""
|
|
|
|
|
2021-04-02 14:05:26 +00:00
|
|
|
# (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/>.
|
|
|
|
|
2021-01-08 22:29:35 +00:00
|
|
|
import code
|
|
|
|
import json
|
|
|
|
|
|
|
|
import cmd2
|
|
|
|
from cmd2 import CommandSet, with_default_category, with_argparser
|
|
|
|
import argparse
|
|
|
|
|
2021-04-02 15:16:30 +00:00
|
|
|
from typing import cast, Optional, Iterable, List, Any, Dict, Tuple
|
2021-04-02 11:00:18 +00:00
|
|
|
|
2021-03-09 20:49:01 +00:00
|
|
|
from pySim.utils import sw_match, h2b, b2h, is_hex
|
2021-01-08 22:29:35 +00:00
|
|
|
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']
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def __init__(self, fid:str=None, sfid:str=None, name:str=None, desc:str=None,
|
|
|
|
parent:Optional['CardDF']=None):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
fid : File Identifier (4 hex digits)
|
|
|
|
sfid : Short File Identifier (2 hex digits, optional)
|
|
|
|
name : Brief name of the file, lik EF_ICCID
|
|
|
|
desc : Descriptoin of the file
|
|
|
|
parent : Parent CardFile object within filesystem hierarchy
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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)
|
2021-04-02 11:00:18 +00:00
|
|
|
self.shell_commands: List[CommandSet] = []
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-03-09 20:57:57 +00:00
|
|
|
# Note: the basic properties (fid, name, ect.) are verified when
|
|
|
|
# the file is attached to a parent file. See method add_file() in
|
|
|
|
# class Card DF
|
|
|
|
|
2021-01-08 22:29:35 +00:00
|
|
|
def __str__(self):
|
|
|
|
if self.name:
|
|
|
|
return self.name
|
|
|
|
else:
|
|
|
|
return self.fid
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def _path_element(self, prefer_name:bool) -> Optional[str]:
|
2021-01-08 22:29:35 +00:00
|
|
|
if prefer_name and self.name:
|
|
|
|
return self.name
|
|
|
|
else:
|
|
|
|
return self.fid
|
|
|
|
|
2021-04-02 15:16:30 +00:00
|
|
|
def fully_qualified_path(self, prefer_name:bool=True) -> List[str]:
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Return fully qualified path to file as list of FID or name strings.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
prefer_name : Preferably build path of names; fall-back to FIDs as required
|
|
|
|
"""
|
2021-04-02 15:16:30 +00:00
|
|
|
if self.parent and self.parent != self:
|
2021-01-08 22:29:35 +00:00
|
|
|
ret = self.parent.fully_qualified_path(prefer_name)
|
|
|
|
else:
|
|
|
|
ret = []
|
2021-04-02 15:16:30 +00:00
|
|
|
elem = self._path_element(prefer_name)
|
|
|
|
if elem:
|
|
|
|
ret.append(elem)
|
2021-01-08 22:29:35 +00:00
|
|
|
return ret
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def get_mf(self) -> Optional['CardMF']:
|
2021-01-08 22:29:35 +00:00
|
|
|
"""Return the MF (root) of the file system."""
|
|
|
|
if self.parent == None:
|
|
|
|
return None
|
|
|
|
# iterate towards the top. MF has parent == self
|
|
|
|
node = self
|
2021-04-02 15:16:30 +00:00
|
|
|
while node.parent and node.parent != node:
|
2021-01-08 22:29:35 +00:00
|
|
|
node = node.parent
|
2021-04-02 15:16:30 +00:00
|
|
|
return cast(CardMF, node)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 15:16:30 +00:00
|
|
|
def _get_self_selectables(self, alias:str=None, flags = []) -> Dict[str, 'CardFile']:
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Return a dict of {'identifier': self} tuples.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
alias : Add an alias with given name to 'self'
|
|
|
|
flags : Specify which selectables to return 'FIDS' and/or 'NAMES';
|
|
|
|
If not specified, all selectables will be returned.
|
|
|
|
Returns:
|
|
|
|
dict containing reference to 'self' for all identifiers.
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
sels = {}
|
|
|
|
if alias:
|
|
|
|
sels.update({alias: self})
|
2021-02-25 15:48:10 +00:00
|
|
|
if self.fid and (flags == [] or 'FIDS' in flags):
|
2021-01-08 22:29:35 +00:00
|
|
|
sels.update({self.fid: self})
|
2021-03-18 16:09:33 +00:00
|
|
|
if self.name and (flags == [] or 'FNAMES' in flags):
|
2021-01-08 22:29:35 +00:00
|
|
|
sels.update({self.name: self})
|
|
|
|
return sels
|
|
|
|
|
2021-04-02 15:16:30 +00:00
|
|
|
def get_selectables(self, flags = []) -> Dict[str, 'CardFile']:
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Return a dict of {'identifier': File} that is selectable from the current file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
flags : Specify which selectables to return 'FIDS' and/or 'NAMES';
|
|
|
|
If not specified, all selectables will be returned.
|
|
|
|
Returns:
|
|
|
|
dict containing all selectable items. Key is identifier (string), value
|
|
|
|
a reference to a CardFile (or derived class) instance.
|
|
|
|
"""
|
2021-02-25 15:48:10 +00:00
|
|
|
sels = {}
|
2021-01-08 22:29:35 +00:00
|
|
|
# we can always select ourself
|
2021-02-25 15:48:10 +00:00
|
|
|
if flags == [] or 'SELF' in flags:
|
|
|
|
sels = self._get_self_selectables('.', flags)
|
2021-01-08 22:29:35 +00:00
|
|
|
# we can always select our parent
|
2021-02-25 15:48:10 +00:00
|
|
|
if flags == [] or 'PARENT' in flags:
|
2021-04-02 15:16:30 +00:00
|
|
|
if self.parent:
|
|
|
|
sels = self.parent._get_self_selectables('..', flags)
|
2021-01-08 22:29:35 +00:00
|
|
|
# if we have a MF, we can always select its applications
|
2021-02-25 15:48:10 +00:00
|
|
|
if flags == [] or 'MF' in flags:
|
|
|
|
mf = self.get_mf()
|
|
|
|
if mf:
|
|
|
|
sels.update(mf._get_self_selectables(flags = flags))
|
2021-03-18 16:09:33 +00:00
|
|
|
sels.update(mf.get_app_selectables(flags = flags))
|
2021-01-08 22:29:35 +00:00
|
|
|
return sels
|
|
|
|
|
2021-04-02 15:16:30 +00:00
|
|
|
def get_selectable_names(self, flags = []) -> List[str]:
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Return a dict of {'identifier': File} that is selectable from the current file.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
flags : Specify which selectables to return 'FIDS' and/or 'NAMES';
|
|
|
|
If not specified, all selectables will be returned.
|
|
|
|
Returns:
|
2021-04-02 15:16:30 +00:00
|
|
|
list containing all selectable names.
|
2021-04-02 11:00:18 +00:00
|
|
|
"""
|
2021-02-25 15:48:10 +00:00
|
|
|
sels = self.get_selectables(flags)
|
2021-04-02 15:16:30 +00:00
|
|
|
return list(sels.keys())
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def decode_select_response(self, data_hex:str):
|
2021-01-08 22:29:35 +00:00
|
|
|
"""Decode the response to a SELECT command."""
|
2021-04-02 15:16:30 +00:00
|
|
|
if self.parent:
|
|
|
|
return self.parent.decode_select_response(data_hex)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CardDF(CardFile):
|
|
|
|
"""DF (Dedicated File) in the smart card filesystem. Those are basically sub-directories."""
|
2021-03-09 21:42:47 +00:00
|
|
|
|
|
|
|
@with_default_category('DF/ADF Commands')
|
|
|
|
class ShellCommands(CommandSet):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
2021-01-08 22:29:35 +00:00
|
|
|
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()
|
2021-03-09 21:42:47 +00:00
|
|
|
self.shell_commands = [self.ShellCommands()]
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "DF(%s)" % (super().__str__())
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def add_file(self, child:CardFile, ignore_existing:bool=False):
|
|
|
|
"""Add a child (DF/EF) to this DF.
|
|
|
|
Args:
|
|
|
|
child: The new DF/EF to be added
|
|
|
|
ignore_existing: Ignore, if file with given FID already exists. Old one will be kept.
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
if not isinstance(child, CardFile):
|
|
|
|
raise TypeError("Expected a File instance")
|
2021-03-09 20:49:01 +00:00
|
|
|
if not is_hex(child.fid, minlen = 4, maxlen = 4):
|
|
|
|
raise ValueError("File name %s is not a valid fid" % (child.fid))
|
2021-01-08 22:29:35 +00:00
|
|
|
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:
|
2021-03-09 19:33:41 +00:00
|
|
|
raise ValueError("File fid %s is a reserved fid" % (child.fid))
|
2021-01-08 22:29:35 +00:00
|
|
|
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
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def add_files(self, children:Iterable[CardFile], ignore_existing:bool=False):
|
|
|
|
"""Add a list of child (DF/EF) to this DF
|
|
|
|
|
|
|
|
Args:
|
|
|
|
children: List of new DF/EFs to be added
|
|
|
|
ignore_existing: Ignore, if file[s] with given FID already exists. Old one[s] will be kept.
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
for child in children:
|
|
|
|
self.add_file(child, ignore_existing)
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def get_selectables(self, flags = []) -> dict:
|
|
|
|
"""Return a dict of {'identifier': File} that is selectable from the current DF.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
flags : Specify which selectables to return 'FIDS' and/or 'NAMES';
|
|
|
|
If not specified, all selectables will be returned.
|
|
|
|
Returns:
|
|
|
|
dict containing all selectable items. Key is identifier (string), value
|
|
|
|
a reference to a CardFile (or derived class) instance.
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
# global selectables + our children
|
2021-02-25 15:48:10 +00:00
|
|
|
sels = super().get_selectables(flags)
|
|
|
|
if flags == [] or 'FIDS' in flags:
|
|
|
|
sels.update({x.fid: x for x in self.children.values() if x.fid})
|
2021-03-18 16:09:33 +00:00
|
|
|
if flags == [] or 'FNAMES' in flags:
|
2021-02-25 15:48:10 +00:00
|
|
|
sels.update({x.name: x for x in self.children.values() if x.name})
|
2021-01-08 22:29:35 +00:00
|
|
|
return sels
|
|
|
|
|
2021-04-02 15:16:30 +00:00
|
|
|
def lookup_file_by_name(self, name:Optional[str]) -> Optional[CardFile]:
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Find a file with given name within current DF."""
|
2021-01-08 22:29:35 +00:00
|
|
|
if name == None:
|
|
|
|
return None
|
|
|
|
for i in self.children.values():
|
|
|
|
if i.name and i.name == name:
|
|
|
|
return i
|
|
|
|
return None
|
|
|
|
|
2021-04-02 15:16:30 +00:00
|
|
|
def lookup_file_by_sfid(self, sfid:Optional[str]) -> Optional[CardFile]:
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Find a file with given short file ID within current DF."""
|
2021-01-08 22:29:35 +00:00
|
|
|
if sfid == None:
|
|
|
|
return None
|
|
|
|
for i in self.children.values():
|
2021-04-02 15:16:30 +00:00
|
|
|
if i.sfid == int(str(sfid)):
|
2021-01-08 22:29:35 +00:00
|
|
|
return i
|
|
|
|
return None
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def lookup_file_by_fid(self, fid:str) -> Optional[CardFile]:
|
|
|
|
"""Find a file with given file ID within current DF."""
|
2021-01-08 22:29:35 +00:00
|
|
|
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)
|
|
|
|
|
2021-04-02 18:27:05 +00:00
|
|
|
def add_application_df(self, app:'CardADF'):
|
|
|
|
"""Add an Application to the MF"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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]
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def get_selectables(self, flags = []) -> dict:
|
|
|
|
"""Return a dict of {'identifier': File} that is selectable from the current DF.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
flags : Specify which selectables to return 'FIDS' and/or 'NAMES';
|
|
|
|
If not specified, all selectables will be returned.
|
|
|
|
Returns:
|
|
|
|
dict containing all selectable items. Key is identifier (string), value
|
|
|
|
a reference to a CardFile (or derived class) instance.
|
|
|
|
"""
|
2021-02-25 15:48:10 +00:00
|
|
|
sels = super().get_selectables(flags)
|
2021-03-18 16:09:33 +00:00
|
|
|
sels.update(self.get_app_selectables(flags))
|
2021-01-08 22:29:35 +00:00
|
|
|
return sels
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def get_app_selectables(self, flags = []) -> dict:
|
2021-02-25 15:48:10 +00:00
|
|
|
"""Get applications by AID + name"""
|
|
|
|
sels = {}
|
2021-03-18 16:09:33 +00:00
|
|
|
if flags == [] or 'AIDS' in flags:
|
2021-02-25 15:48:10 +00:00
|
|
|
sels.update({x.aid: x for x in self.applications.values()})
|
2021-03-18 16:09:33 +00:00
|
|
|
if flags == [] or 'ANAMES' in flags:
|
2021-02-25 15:48:10 +00:00
|
|
|
sels.update({x.name: x for x in self.applications.values() if x.name})
|
2021-01-08 22:29:35 +00:00
|
|
|
return sels
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def decode_select_response(self, data_hex:str) -> Any:
|
|
|
|
"""Decode the response to a SELECT command.
|
|
|
|
|
|
|
|
This is the fall-back method which doesn't perform any decoding. It mostly
|
|
|
|
exists so specific derived classes can overload it for actual decoding.
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
return data_hex
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CardADF(CardDF):
|
|
|
|
"""ADF (Application Dedicated File) in the smart card filesystem"""
|
2021-04-02 11:00:18 +00:00
|
|
|
def __init__(self, aid:str, **kwargs):
|
2021-01-08 22:29:35 +00:00
|
|
|
super().__init__(**kwargs)
|
2021-04-02 18:27:05 +00:00
|
|
|
# reference to CardApplication may be set from CardApplication constructor
|
|
|
|
self.application:Optional[CardApplication] = None
|
2021-01-08 22:29:35 +00:00
|
|
|
self.aid = aid # Application Identifier
|
2021-04-02 15:16:30 +00:00
|
|
|
mf = self.get_mf()
|
|
|
|
if mf:
|
2021-04-02 18:27:05 +00:00
|
|
|
mf.add_application_df(self)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "ADF(%s)" % (self.aid)
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def _path_element(self, prefer_name:bool):
|
2021-01-08 22:29:35 +00:00
|
|
|
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__())
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def get_selectables(self, flags = []) -> dict:
|
|
|
|
"""Return a dict of {'identifier': File} that is selectable from the current DF.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
flags : Specify which selectables to return 'FIDS' and/or 'NAMES';
|
|
|
|
If not specified, all selectables will be returned.
|
|
|
|
Returns:
|
|
|
|
dict containing all selectable items. Key is identifier (string), value
|
|
|
|
a reference to a CardFile (or derived class) instance.
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
#global selectable names + those of the parent DF
|
2021-02-25 15:48:10 +00:00
|
|
|
sels = super().get_selectables(flags)
|
2021-01-08 22:29:35 +00:00
|
|
|
sels.update({x.name:x for x in self.parent.children.values() if x != self})
|
|
|
|
return sels
|
|
|
|
|
|
|
|
|
|
|
|
class TransparentEF(CardEF):
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Transparent EF (Entry File) in the smart card filesystem.
|
|
|
|
|
|
|
|
A Transparent EF is a binary file with no formal structure. This is contrary to
|
|
|
|
Record based EFs which have [fixed size] records that can be individually read/updated."""
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
@with_default_category('Transparent EF Commands')
|
|
|
|
class ShellCommands(CommandSet):
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Shell commands specific for Trransparent EFs."""
|
2021-01-08 22:29:35 +00:00
|
|
|
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)
|
2021-03-11 16:13:46 +00:00
|
|
|
if data:
|
|
|
|
self._cmd.poutput(data)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
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)
|
2021-03-11 16:13:46 +00:00
|
|
|
if data:
|
|
|
|
self._cmd.poutput(json.dumps(data, indent=4))
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def __init__(self, fid:str, sfid:str=None, name:str=None, desc:str=None, parent:CardDF=None,
|
|
|
|
size={1,None}):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
fid : File Identifier (4 hex digits)
|
|
|
|
sfid : Short File Identifier (2 hex digits, optional)
|
|
|
|
name : Brief name of the file, lik EF_ICCID
|
|
|
|
desc : Descriptoin of the file
|
|
|
|
parent : Parent CardFile object within filesystem hierarchy
|
|
|
|
size : tuple of (minimum_size, recommended_size)
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, parent=parent)
|
|
|
|
self.size = size
|
|
|
|
self.shell_commands = [self.ShellCommands()]
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def decode_bin(self, raw_bin_data:bytearray) -> dict:
|
|
|
|
"""Decode raw (binary) data into abstract representation.
|
|
|
|
|
|
|
|
A derived class would typically provide a _decode_bin() or _decode_hex() method
|
|
|
|
for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
raw_bin_data : binary encoded data
|
|
|
|
Returns:
|
|
|
|
abstract_data; dict representing the decoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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()}
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def decode_hex(self, raw_hex_data:str) -> dict:
|
|
|
|
"""Decode raw (hex string) data into abstract representation.
|
|
|
|
|
|
|
|
A derived class would typically provide a _decode_bin() or _decode_hex() method
|
|
|
|
for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
raw_hex_data : hex-encoded data
|
|
|
|
Returns:
|
|
|
|
abstract_data; dict representing the decoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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()}
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def encode_bin(self, abstract_data:dict) -> bytearray:
|
|
|
|
"""Encode abstract representation into raw (binary) data.
|
|
|
|
|
|
|
|
A derived class would typically provide an _encode_bin() or _encode_hex() method
|
|
|
|
for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
abstract_data : dict representing the decoded data
|
|
|
|
Returns:
|
|
|
|
binary encoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def encode_hex(self, abstract_data:dict) -> str:
|
|
|
|
"""Encode abstract representation into raw (hex string) data.
|
|
|
|
|
|
|
|
A derived class would typically provide an _encode_bin() or _encode_hex() method
|
|
|
|
for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
abstract_data : dict representing the decoded data
|
|
|
|
Returns:
|
|
|
|
hex string encoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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):
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Linear Fixed EF (Entry File) in the smart card filesystem.
|
|
|
|
|
|
|
|
Linear Fixed EFs are record oriented files. They consist of a number of fixed-size
|
|
|
|
records. The records can be individually read/updated."""
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
@with_default_category('Linear Fixed EF Commands')
|
|
|
|
class ShellCommands(CommandSet):
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Shell commands specific for Linear Fixed EFs."""
|
2021-01-08 22:29:35 +00:00
|
|
|
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')
|
2021-02-25 15:52:08 +00:00
|
|
|
read_rec_parser.add_argument('--count', type=int, default=1, help='Number of records to be read, beginning at record_nr')
|
2021-01-08 22:29:35 +00:00
|
|
|
@cmd2.with_argparser(read_rec_parser)
|
|
|
|
def do_read_record(self, opts):
|
2021-02-25 15:52:08 +00:00
|
|
|
"""Read one or multiple records from a record-oriented EF"""
|
|
|
|
for r in range(opts.count):
|
|
|
|
recnr = opts.record_nr + r
|
|
|
|
(data, sw) = self._cmd.rs.read_record(recnr)
|
|
|
|
if (len(data) > 0):
|
|
|
|
recstr = str(data)
|
|
|
|
else:
|
|
|
|
recstr = "(empty)"
|
|
|
|
self._cmd.poutput("%03d %s" % (recnr, recstr))
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
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)
|
2021-03-11 16:13:46 +00:00
|
|
|
if data:
|
|
|
|
self._cmd.poutput(data)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
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)
|
2021-03-11 16:13:46 +00:00
|
|
|
if data:
|
|
|
|
self._cmd.poutput(data)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def __init__(self, fid:str, sfid:str=None, name:str=None, desc:str=None,
|
|
|
|
parent:Optional[CardDF]=None, rec_len={1,None}):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
fid : File Identifier (4 hex digits)
|
|
|
|
sfid : Short File Identifier (2 hex digits, optional)
|
|
|
|
name : Brief name of the file, lik EF_ICCID
|
|
|
|
desc : Descriptoin of the file
|
|
|
|
parent : Parent CardFile object within filesystem hierarchy
|
|
|
|
rec_len : tuple of (minimum_length, recommended_length)
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, parent=parent)
|
|
|
|
self.rec_len = rec_len
|
|
|
|
self.shell_commands = [self.ShellCommands()]
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def decode_record_hex(self, raw_hex_data:str) -> dict:
|
|
|
|
"""Decode raw (hex string) data into abstract representation.
|
|
|
|
|
|
|
|
A derived class would typically provide a _decode_record_bin() or _decode_record_hex()
|
|
|
|
method for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
raw_hex_data : hex-encoded data
|
|
|
|
Returns:
|
|
|
|
abstract_data; dict representing the decoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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()}
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def decode_record_bin(self, raw_bin_data:bytearray) -> dict:
|
|
|
|
"""Decode raw (binary) data into abstract representation.
|
|
|
|
|
|
|
|
A derived class would typically provide a _decode_record_bin() or _decode_record_hex()
|
|
|
|
method for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
raw_bin_data : binary encoded data
|
|
|
|
Returns:
|
|
|
|
abstract_data; dict representing the decoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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}
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def encode_record_hex(self, abstract_data:dict) -> str:
|
|
|
|
"""Encode abstract representation into raw (hex string) data.
|
|
|
|
|
|
|
|
A derived class would typically provide an _encode_record_bin() or _encode_record_hex()
|
|
|
|
method for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
abstract_data : dict representing the decoded data
|
|
|
|
Returns:
|
|
|
|
hex string encoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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)
|
2021-04-02 15:16:30 +00:00
|
|
|
return b2h(raw_bin_data)
|
2021-01-08 22:29:35 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def encode_record_bin(self, abstract_data:dict) -> bytearray:
|
|
|
|
"""Encode abstract representation into raw (binary) data.
|
|
|
|
|
|
|
|
A derived class would typically provide an _encode_record_bin() or _encode_record_hex()
|
|
|
|
method for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
abstract_data : dict representing the decoded data
|
|
|
|
Returns:
|
|
|
|
binary encoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
method = getattr(self, '_encode_record_bin', None)
|
|
|
|
if callable(method):
|
|
|
|
return method(abstract_data)
|
|
|
|
method = getattr(self, '_encode_record_hex', None)
|
|
|
|
if callable(method):
|
2021-04-02 11:00:18 +00:00
|
|
|
return h2b(method(abstract_data))
|
2021-01-08 22:29:35 +00:00
|
|
|
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
|
2021-04-02 11:00:18 +00:00
|
|
|
def __init__(self, fid:str, sfid:str=None, name:str=None, desc:str=None, parent:CardDF=None,
|
|
|
|
rec_len={1,None}):
|
2021-01-08 22:29:35 +00:00
|
|
|
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.
|
2021-04-02 11:00:18 +00:00
|
|
|
|
2021-01-08 22:29:35 +00:00
|
|
|
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.
|
|
|
|
"""
|
2021-04-02 15:16:30 +00:00
|
|
|
def __init__(self, fid:str, rec_len:int, sfid:str=None, name:str=None, desc:str=None,
|
|
|
|
parent:Optional[CardDF]=None, size={1,None}):
|
2021-04-02 11:00:18 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
fid : File Identifier (4 hex digits)
|
|
|
|
sfid : Short File Identifier (2 hex digits, optional)
|
|
|
|
name : Brief name of the file, lik EF_ICCID
|
|
|
|
desc : Descriptoin of the file
|
|
|
|
parent : Parent CardFile object within filesystem hierarchy
|
|
|
|
rec_len : Length of the fixed-length records within transparent EF
|
|
|
|
size : tuple of (minimum_size, recommended_size)
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
super().__init__(fid=fid, sfid=sfid, name=name, desc=desc, parent=parent, size=size)
|
|
|
|
self.rec_len = rec_len
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def decode_record_hex(self, raw_hex_data:str) -> dict:
|
|
|
|
"""Decode raw (hex string) data into abstract representation.
|
|
|
|
|
|
|
|
A derived class would typically provide a _decode_record_bin() or _decode_record_hex()
|
|
|
|
method for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
raw_hex_data : hex-encoded data
|
|
|
|
Returns:
|
|
|
|
abstract_data; dict representing the decoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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}
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def decode_record_bin(self, raw_bin_data:bytearray) -> dict:
|
|
|
|
"""Decode raw (binary) data into abstract representation.
|
|
|
|
|
|
|
|
A derived class would typically provide a _decode_record_bin() or _decode_record_hex()
|
|
|
|
method for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
raw_bin_data : binary encoded data
|
|
|
|
Returns:
|
|
|
|
abstract_data; dict representing the decoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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}
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def encode_record_hex(self, abstract_data:dict) -> str:
|
|
|
|
"""Encode abstract representation into raw (hex string) data.
|
|
|
|
|
|
|
|
A derived class would typically provide an _encode_record_bin() or _encode_record_hex()
|
|
|
|
method for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
abstract_data : dict representing the decoded data
|
|
|
|
Returns:
|
|
|
|
hex string encoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
method = getattr(self, '_encode_record_hex', None)
|
|
|
|
if callable(method):
|
|
|
|
return method(abstract_data)
|
|
|
|
method = getattr(self, '_encode_record_bin', None)
|
|
|
|
if callable(method):
|
2021-04-02 15:16:30 +00:00
|
|
|
return b2h(method(abstract_data))
|
2021-01-08 22:29:35 +00:00
|
|
|
raise NotImplementedError
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def encode_record_bin(self, abstract_data:dict) -> bytearray:
|
|
|
|
"""Encode abstract representation into raw (binary) data.
|
|
|
|
|
|
|
|
A derived class would typically provide an _encode_record_bin() or _encode_record_hex()
|
|
|
|
method for implementing this specifically for the given file. This function checks which
|
|
|
|
of the method exists, add calls them (with conversion, as needed).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
abstract_data : dict representing the decoded data
|
|
|
|
Returns:
|
|
|
|
binary encoded data
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def _decode_bin(self, raw_bin_data:bytearray):
|
2021-01-08 22:29:35 +00:00
|
|
|
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]
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def _encode_bin(self, abstract_data) -> bytes:
|
2021-01-08 22:29:35 +00:00
|
|
|
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."""
|
2021-04-02 11:00:18 +00:00
|
|
|
def __init__(self, card, profile:'CardProfile'):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
card : pysim.cards.Card instance
|
|
|
|
profile : CardProfile instance
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
self.mf = CardMF()
|
|
|
|
self.card = card
|
2021-04-02 18:27:05 +00:00
|
|
|
self.selected_file:CardDF = self.mf
|
2021-01-08 22:29:35 +00:00
|
|
|
self.profile = profile
|
2021-04-02 18:27:05 +00:00
|
|
|
# add application ADFs + MF-files from profile
|
2021-03-10 16:02:53 +00:00
|
|
|
apps = self._match_applications()
|
|
|
|
for a in apps:
|
2021-04-02 18:27:05 +00:00
|
|
|
if a.adf:
|
|
|
|
self.mf.add_application_df(a.adf)
|
2021-01-08 22:29:35 +00:00
|
|
|
for f in self.profile.files_in_mf:
|
|
|
|
self.mf.add_file(f)
|
2021-03-17 16:19:52 +00:00
|
|
|
self.conserve_write = True
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-03-10 16:02:53 +00:00
|
|
|
def _match_applications(self):
|
|
|
|
"""match the applications from the profile with applications on the card"""
|
|
|
|
apps_profile = self.profile.applications
|
|
|
|
aids_card = self.card.read_aids()
|
|
|
|
apps_taken = []
|
|
|
|
if aids_card:
|
|
|
|
aids_taken = []
|
|
|
|
print("AIDs on card:")
|
|
|
|
for a in aids_card:
|
|
|
|
for f in apps_profile:
|
|
|
|
if f.aid in a:
|
|
|
|
print(" %s: %s" % (f.name, a))
|
|
|
|
aids_taken.append(a)
|
|
|
|
apps_taken.append(f)
|
|
|
|
aids_unknown = set(aids_card) - set(aids_taken)
|
|
|
|
for a in aids_unknown:
|
|
|
|
print(" unknown: %s" % a)
|
|
|
|
else:
|
|
|
|
print("error: could not determine card applications")
|
|
|
|
return apps_taken
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def get_cwd(self) -> CardDF:
|
|
|
|
"""Obtain the current working directory.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
CardDF instance
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
if isinstance(self.selected_file, CardDF):
|
|
|
|
return self.selected_file
|
|
|
|
else:
|
|
|
|
return self.selected_file.parent
|
|
|
|
|
2021-04-02 18:27:05 +00:00
|
|
|
def get_application_df(self) -> Optional[CardADF]:
|
|
|
|
"""Obtain the currently selected application DF (if any).
|
2021-04-02 11:00:18 +00:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
CardADF() instance or None"""
|
2021-01-08 22:29:35 +00:00
|
|
|
# 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
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def interpret_sw(self, sw:str):
|
|
|
|
"""Interpret a given status word relative to the currently selected application
|
|
|
|
or the underlying card profile.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sw : Status word as string of 4 hexd digits
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple of two strings
|
|
|
|
"""
|
2021-04-02 20:13:09 +00:00
|
|
|
res = None
|
2021-04-02 18:27:05 +00:00
|
|
|
adf = self.get_application_df()
|
|
|
|
if adf:
|
|
|
|
app = adf.application
|
2021-01-08 22:29:35 +00:00
|
|
|
# The application either comes with its own interpret_sw
|
|
|
|
# method or we will use the interpret_sw method from the
|
|
|
|
# card profile.
|
2021-04-02 18:27:05 +00:00
|
|
|
if app and hasattr(app, "interpret_sw"):
|
2021-04-02 20:13:09 +00:00
|
|
|
res = app.interpret_sw(sw)
|
|
|
|
return res or self.profile.interpret_sw(sw)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def probe_file(self, fid:str, cmd_app=None):
|
|
|
|
"""Blindly try to select a file and automatically add a matching file
|
|
|
|
object if the file actually exists."""
|
2021-03-09 21:42:47 +00:00
|
|
|
if not is_hex(fid, 4, 4):
|
|
|
|
raise ValueError("Cannot select unknown file by name %s, only hexadecimal 4 digit FID is allowed" % fid)
|
|
|
|
|
|
|
|
try:
|
|
|
|
(data, sw) = self.card._scc.select_file(fid)
|
|
|
|
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]))
|
|
|
|
|
|
|
|
select_resp = self.selected_file.decode_select_response(data)
|
|
|
|
if (select_resp['file_descriptor']['file_type'] == 'df'):
|
|
|
|
f = CardDF(fid=fid, sfid=None, name="DF." + str(fid).upper(), desc="dedicated file, manually added at runtime")
|
|
|
|
else:
|
|
|
|
if (select_resp['file_descriptor']['structure'] == 'transparent'):
|
|
|
|
f = TransparentEF(fid=fid, sfid=None, name="EF." + str(fid).upper(), desc="elementry file, manually added at runtime")
|
|
|
|
else:
|
|
|
|
f = LinFixedEF(fid=fid, sfid=None, name="EF." + str(fid).upper(), desc="elementry file, manually added at runtime")
|
|
|
|
|
|
|
|
self.selected_file.add_files([f])
|
|
|
|
self.selected_file = f
|
|
|
|
return select_resp
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def select(self, name:str, cmd_app=None):
|
|
|
|
"""Select a file (EF, DF, ADF, MF, ...).
|
|
|
|
|
|
|
|
Args:
|
|
|
|
name : Name of file to select
|
|
|
|
cmd_app : Command Application State (for unregistering old file commands)
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
sels = self.selected_file.get_selectables()
|
2021-03-11 13:29:37 +00:00
|
|
|
if is_hex(name):
|
|
|
|
name = name.lower()
|
2021-03-09 21:42:47 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2021-01-08 22:29:35 +00:00
|
|
|
if name in sels:
|
|
|
|
f = sels[name]
|
|
|
|
try:
|
|
|
|
if isinstance(f, CardADF):
|
2021-03-11 12:03:18 +00:00
|
|
|
(data, sw) = self.card.select_adf_by_aid(f.aid)
|
2021-01-08 22:29:35 +00:00
|
|
|
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]))
|
2021-03-09 21:42:47 +00:00
|
|
|
select_resp = f.decode_select_response(data)
|
2021-01-08 22:29:35 +00:00
|
|
|
else:
|
2021-03-09 21:42:47 +00:00
|
|
|
select_resp = self.probe_file(name, cmd_app)
|
|
|
|
|
|
|
|
# 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 select_resp
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def read_binary(self, length:int=None, offset:int=0):
|
|
|
|
"""Read [part of] a transparent EF binary data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
length : Amount of data to read (None: as much as possible)
|
|
|
|
offset : Offset into the file from which to read 'length' bytes
|
|
|
|
Returns:
|
|
|
|
binary data read from the file
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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)
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def read_binary_dec(self) -> dict:
|
|
|
|
"""Read [part of] a transparent EF binary data and decode it.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
length : Amount of data to read (None: as much as possible)
|
|
|
|
offset : Offset into the file from which to read 'length' bytes
|
|
|
|
Returns:
|
|
|
|
abstract decode data read from the file
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
(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)
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def update_binary(self, data_hex:str, offset:int=0):
|
|
|
|
"""Update transparent EF binary data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data_hex : hex string of data to be written
|
|
|
|
offset : Offset into the file from which to write 'data_hex'
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
if not isinstance(self.selected_file, TransparentEF):
|
|
|
|
raise TypeError("Only works with TransparentEF")
|
2021-03-17 16:19:52 +00:00
|
|
|
return self.card._scc.update_binary(self.selected_file.fid, data_hex, offset, conserve=self.conserve_write)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def update_binary_dec(self, data:dict):
|
|
|
|
"""Update transparent EF from abstract data. Encodes the data to binary and
|
|
|
|
then updates the EF with it.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
data : abstract data which is to be encoded and written
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
data_hex = self.selected_file.encode_hex(data)
|
|
|
|
print("%s -> %s" % (data, data_hex))
|
|
|
|
return self.update_binary(data_hex)
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def read_record(self, rec_nr:int=0):
|
|
|
|
"""Read a record as binary data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
rec_nr : Record number to read
|
|
|
|
Returns:
|
|
|
|
hex string of binary data contained in record
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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)
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def read_record_dec(self, rec_nr:int=0) -> Tuple[dict, str]:
|
|
|
|
"""Read a record and decode it to abstract data.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
rec_nr : Record number to read
|
|
|
|
Returns:
|
|
|
|
abstract data contained in record
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
(data, sw) = self.read_record(rec_nr)
|
|
|
|
return (self.selected_file.decode_record_hex(data), sw)
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def update_record(self, rec_nr:int, data_hex:str):
|
|
|
|
"""Update a record with given binary data
|
|
|
|
|
|
|
|
Args:
|
|
|
|
rec_nr : Record number to read
|
|
|
|
data_hex : Hex string binary data to be written
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
if not isinstance(self.selected_file, LinFixedEF):
|
|
|
|
raise TypeError("Only works with Linear Fixed EF")
|
2021-03-17 16:19:52 +00:00
|
|
|
return self.card._scc.update_record(self.selected_file.fid, rec_nr, data_hex, conserve=self.conserve_write)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def update_record_dec(self, rec_nr:int, data:dict):
|
|
|
|
"""Update a record with given abstract data. Will encode abstract to binary data
|
|
|
|
and then write it to the given record on the card.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
rec_nr : Record number to read
|
|
|
|
data_hex : Abstract data to be written
|
|
|
|
"""
|
2021-04-02 15:16:30 +00:00
|
|
|
data_hex = self.selected_file.encode_record_hex(data)
|
|
|
|
return self.update_record(rec_nr, data_hex)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FileData(object):
|
|
|
|
"""Represent the runtime, on-card data."""
|
|
|
|
def __init__(self, fdesc):
|
|
|
|
self.desc = fdesc
|
|
|
|
self.fcp = None
|
|
|
|
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def interpret_sw(sw_data:dict, sw:str):
|
|
|
|
"""Interpret a given status word.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sw_data : Hierarchical dict of status word matches
|
|
|
|
sw : status word to match (string of 4 hex digits)
|
|
|
|
Returns:
|
|
|
|
tuple of two strings (class_string, description)
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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."""
|
2021-04-02 18:27:05 +00:00
|
|
|
def __init__(self, name, adf:Optional[CardADF]=None, aid:str=None, sw:dict=None):
|
2021-04-02 11:00:18 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
adf : ADF name
|
|
|
|
sw : Dict of status word conversions
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
self.name = name
|
|
|
|
self.adf = adf
|
2021-03-27 00:25:46 +00:00
|
|
|
self.sw = sw or dict()
|
2021-04-02 18:27:05 +00:00
|
|
|
# back-reference from ADF to Applicaiton
|
|
|
|
if self.adf:
|
|
|
|
self.aid = aid or self.adf.aid
|
|
|
|
self.adf.application = self
|
|
|
|
else:
|
|
|
|
self.aid = aid
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "APP(%s)" % (self.name)
|
|
|
|
|
|
|
|
def interpret_sw(self, sw):
|
2021-04-02 11:00:18 +00:00
|
|
|
"""Interpret a given status word within the application.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sw : Status word as string of 4 hexd digits
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple of two strings
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
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."""
|
2021-03-27 00:25:46 +00:00
|
|
|
def __init__(self, name, **kw):
|
2021-04-02 11:00:18 +00:00
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
desc (str) : Description
|
|
|
|
files_in_mf : List of CardEF instances present in MF
|
|
|
|
applications : List of CardApplications present on card
|
|
|
|
sw : List of status word definitions
|
|
|
|
shell_cmdsets : List of cmd2 shell command sets of profile-specific commands
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
self.name = name
|
2021-03-27 00:25:46 +00:00
|
|
|
self.desc = kw.get("desc", None)
|
|
|
|
self.files_in_mf = kw.get("files_in_mf", [])
|
|
|
|
self.sw = kw.get("sw", [])
|
|
|
|
self.applications = kw.get("applications", [])
|
|
|
|
self.shell_cmdsets = kw.get("shell_cmdsets", [])
|
2021-01-08 22:29:35 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def add_application(self, app:CardApplication):
|
|
|
|
"""Add an application to a card profile.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
app : CardApplication instance to be added to profile
|
|
|
|
"""
|
2021-03-26 20:29:57 +00:00
|
|
|
self.applications.append(app)
|
2021-01-08 22:29:35 +00:00
|
|
|
|
2021-04-02 11:00:18 +00:00
|
|
|
def interpret_sw(self, sw:str):
|
|
|
|
"""Interpret a given status word within the profile.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
sw : Status word as string of 4 hexd digits
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple of two strings
|
|
|
|
"""
|
2021-01-08 22:29:35 +00:00
|
|
|
return interpret_sw(self.sw, sw)
|