pySim library

pySim filesystem abstraction

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.

class pySim.filesystem.BerTlvEF(fid: str, sfid: Optional[str] = None, name: Optional[str] = None, desc: Optional[str] = None, parent: Optional[CardDF] = None, size={1, None}, **kwargs)

BER-TLV EF (Entry File) in the smart card filesystem. A BER-TLV EF is a binary file with a BER (Basic Encoding Rules) TLV structure

NOTE: We currently don’t really support those, this class is simply a wrapper around TransparentEF as a place-holder, so we can already define EFs of BER-TLV type without fully supporting them.

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • size – tuple of (minimum_size, recommended_size)

class ShellCommands

Shell commands specific for BER-TLV EFs.

do_delete_data(opts)

Delete data for a given tag in a BER-TLV EF

do_retrieve_data(opts)

Retrieve (Read) data from a BER-TLV EF

do_retrieve_tags(opts)

List tags available in a given BER-TLV EF

do_set_data(opts)

Set (Write) data for a given tag in a BER-TLV EF

class pySim.filesystem.CardADF(aid: str, **kwargs)

ADF (Application Dedicated File) in the smart card filesystem

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • profile – Card profile that this file should be part of

  • service – Service (SST/UST/IST) associated with the file

class pySim.filesystem.CardApplication(name, adf: Optional[CardADF] = None, aid: Optional[str] = None, sw: Optional[dict] = None)

A card application is represented by an ADF (with contained hierarchy) and optionally some SW definitions.

Parameters
  • adf – ADF name

  • sw – Dict of status word conversions

interpret_sw(sw)

Interpret a given status word within the application.

Parameters

sw – Status word as string of 4 hex digits

Returns

Tuple of two strings

class pySim.filesystem.CardDF(**kwargs)

DF (Dedicated File) in the smart card filesystem. Those are basically sub-directories.

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • profile – Card profile that this file should be part of

  • service – Service (SST/UST/IST) associated with the file

class ShellCommands
add_file(child: CardFile, ignore_existing: bool = False)

Add a child (DF/EF) to this DF. :param child: The new DF/EF to be added :param ignore_existing: Ignore, if file with given FID already exists. Old one will be kept.

add_files(children: Iterable[CardFile], ignore_existing: bool = False)

Add a list of child (DF/EF) to this DF

Parameters
  • 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.

get_selectables(flags=[]) dict

Return a dict of {‘identifier’: File} that is selectable from the current DF.

Parameters

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.

lookup_file_by_fid(fid: str) Optional[CardFile]

Find a file with given file ID within current DF.

lookup_file_by_name(name: Optional[str]) Optional[CardFile]

Find a file with given name within current DF.

lookup_file_by_sfid(sfid: Optional[str]) Optional[CardFile]

Find a file with given short file ID within current DF.

class pySim.filesystem.CardEF(*, fid, **kwargs)

EF (Entry File) in the smart card filesystem

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • profile – Card profile that this file should be part of

  • service – Service (SST/UST/IST) associated with the file

get_selectables(flags=[]) dict

Return a dict of {‘identifier’: File} that is selectable from the current DF.

Parameters

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.

class pySim.filesystem.CardFile(fid: str = None, sfid: str = None, name: str = None, desc: str = None, parent: Optional[CardDF] = None, profile: Optional[CardProfile] = None, service: Optional[Union[int, List[int], Tuple[int, ...]]] = None)

Base class for all objects in the smart card filesystem. Serve as a common ancestor to all other file types; rarely used directly.

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • profile – Card profile that this file should be part of

  • service – Service (SST/UST/IST) associated with the file

build_select_path_to(target: CardFile) Optional[List[CardFile]]

Build the relative sequence of files we need to traverse to get from us to ‘target’.

decode_select_response(data_hex: str)

Decode the response to a SELECT command.

Parameters

data_hex – Hex string of the select response

fully_qualified_path(prefer_name: bool = True) List[str]

Return fully qualified path to file as list of FID or name strings.

Parameters

prefer_name – Preferably build path of names; fall-back to FIDs as required

fully_qualified_path_fobj() List[CardFile]

Return fully qualified path to file as list of CardFile instance references.

get_mf() Optional[CardMF]

Return the MF (root) of the file system.

get_profile()

Get the profile associated with this file. If this file does not have any profile assigned, try to find a file above (usually the MF) in the filesystem hirarchy that has a profile assigned

get_selectable_names(flags=[]) List[str]

Return a dict of {‘identifier’: File} that is selectable from the current file.

Parameters

flags – Specify which selectables to return ‘FIDS’ and/or ‘NAMES’; If not specified, all selectables will be returned.

Returns

list containing all selectable names.

get_selectables(flags=[]) Dict[str, CardFile]

Return a dict of {‘identifier’: File} that is selectable from the current file.

Parameters

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.

should_exist_for_services(services: List[int])

Assuming the provided list of activated services, should this file exist and be activated?.

class pySim.filesystem.CardMF(**kwargs)

MF (Master File) in the smart card filesystem

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • profile – Card profile that this file should be part of

  • service – Service (SST/UST/IST) associated with the file

add_application_df(app: CardADF)

Add an Application to the MF

decode_select_response(data_hex: Optional[str]) object

Decode the response to a SELECT command.

This is the fall-back method which automatically defers to the standard decoding method defined by the card profile. When no profile is set, then no decoding is performed. Specific derived classes (usually ADF) can overload this method to install specific decoding.

get_app_names()

Get list of completions (AID names)

get_app_selectables(flags=[]) dict

Get applications by AID + name

get_selectables(flags=[]) dict

Return a dict of {‘identifier’: File} that is selectable from the current DF.

Parameters

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.

class pySim.filesystem.CardModel

A specific card model, typically having some additional vendor-specific files. All you need to do is to define a sub-class with a list of ATRs or an overridden match method.

abstract classmethod add_files(rs: RuntimeState)

Add model specific files to given RuntimeState.

static apply_matching_models(scc: SimCardCommands, rs: RuntimeState)

Check if any of the CardModel sub-classes ‘match’ the currently inserted card (by ATR or overriding the ‘match’ method). If so, call their ‘add_files’ method.

classmethod match(scc: SimCardCommands) bool

Test if given card matches this model.

class pySim.filesystem.CyclicEF(fid: str, sfid: Optional[str] = None, name: Optional[str] = None, desc: Optional[str] = None, parent: Optional[CardDF] = None, rec_len={1, None}, **kwargs)

Cyclic EF (Entry File) in the smart card filesystem

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • rec_len – set of {minimum_length, recommended_length}

class pySim.filesystem.FileData(fdesc)

Represent the runtime, on-card data.

class pySim.filesystem.LinFixedEF(fid: str, sfid: Optional[str] = None, name: Optional[str] = None, desc: Optional[str] = None, parent: Optional[CardDF] = None, rec_len={1, None}, **kwargs)

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.

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • rec_len – set of {minimum_length, recommended_length}

class ShellCommands(**kwargs)

Shell commands specific for Linear Fixed EFs.

do_decode_hex(opts)

Decode command-line provided hex-string as if it was read from the file.

do_edit_record_decoded(opts)

Edit the JSON representation of one record in an editor.

do_read_record(opts)

Read one or multiple records from a record-oriented EF

do_read_record_decoded(opts)

Read + decode a record from a record-oriented EF

do_read_records(opts)

Read all records from a record-oriented EF

do_read_records_decoded(opts)

Read + decode all records from a record-oriented EF

do_update_record(opts)

Update (write) data to a record-oriented EF

do_update_record_decoded(opts)

Encode + Update (write) data to a record-oriented EF

decode_record_bin(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).

Parameters

raw_bin_data – binary encoded data

Returns

abstract_data; dict representing the decoded data

decode_record_hex(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).

Parameters

raw_hex_data – hex-encoded data

Returns

abstract_data; dict representing the decoded data

encode_record_bin(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).

Parameters

abstract_data – dict representing the decoded data

Returns

binary encoded data

encode_record_hex(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).

Parameters

abstract_data – dict representing the decoded data

Returns

hex string encoded data

class pySim.filesystem.RuntimeState(card, profile: CardProfile)

Represent the runtime state of a session with a card.

Parameters
  • card – pysim.cards.Card instance

  • profile – CardProfile instance

activate_file(name: str)

Request ACTIVATE FILE of specified file.

get_application_df() Optional[CardADF]

Obtain the currently selected application DF (if any).

Returns

CardADF() instance or None

get_cwd() CardDF

Obtain the current working directory.

Returns

CardDF instance

interpret_sw(sw: str)

Interpret a given status word relative to the currently selected application or the underlying card profile.

Parameters

sw – Status word as string of 4 hex digits

Returns

Tuple of two strings

probe_file(fid: str, cmd_app=None)

Blindly try to select a file and automatically add a matching file object if the file actually exists.

read_binary(length: Optional[int] = None, offset: int = 0)

Read [part of] a transparent EF binary data.

Parameters
  • 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

read_binary_dec() Tuple[dict, str]

Read [part of] a transparent EF binary data and decode it.

Parameters
  • 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

read_record(rec_nr: int = 0)

Read a record as binary data.

Parameters

rec_nr – Record number to read

Returns

hex string of binary data contained in record

read_record_dec(rec_nr: int = 0) Tuple[dict, str]

Read a record and decode it to abstract data.

Parameters

rec_nr – Record number to read

Returns

abstract data contained in record

reset(cmd_app=None) str

Perform physical card reset and obtain ATR. :param cmd_app: Command Application State (for unregistering old file commands)

retrieve_data(tag: int = 0)

Read a DO/TLV as binary data.

Parameters

tag – Tag of TLV/DO to read

Returns

hex string of full BER-TLV DO including Tag and Length

retrieve_tags()

Retrieve tags available on BER-TLV EF.

Returns

list of integer tags contained in EF

select(name: str, cmd_app=None)

Select a file (EF, DF, ADF, MF, …).

Parameters
  • name – Name of file to select

  • cmd_app – Command Application State (for unregistering old file commands)

select_file(file: CardFile, cmd_app=None)

Select a file (EF, DF, ADF, MF, …).

Parameters
  • file – CardFile [or derived class] instance

  • cmd_app – Command Application State (for unregistering old file commands)

set_data(tag: int, data_hex: str)

Update a TLV/DO with given binary data

Parameters
  • tag – Tag of TLV/DO to be written

  • data_hex – Hex string binary data to be written (value portion)

status()

Request STATUS (current selected file FCP) from card.

unregister_cmds(cmd_app=None)

Unregister all file specific commands.

update_binary(data_hex: str, offset: int = 0)

Update transparent EF binary data.

Parameters
  • data_hex – hex string of data to be written

  • offset – Offset into the file from which to write ‘data_hex’

update_binary_dec(data: dict)

Update transparent EF from abstract data. Encodes the data to binary and then updates the EF with it.

Parameters

data – abstract data which is to be encoded and written

update_record(rec_nr: int, data_hex: str)

Update a record with given binary data

Parameters
  • rec_nr – Record number to read

  • data_hex – Hex string binary data to be written

update_record_dec(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.

Parameters
  • rec_nr – Record number to read

  • data_hex – Abstract data to be written

class pySim.filesystem.TransRecEF(fid: str, rec_len: int, sfid: Optional[str] = None, name: Optional[str] = None, desc: Optional[str] = None, parent: Optional[CardDF] = None, size={1, None}, **kwargs)

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.

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, like EF_ICCID

  • desc – Description 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)

decode_record_bin(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).

Parameters

raw_bin_data – binary encoded data

Returns

abstract_data; dict representing the decoded data

decode_record_hex(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).

Parameters

raw_hex_data – hex-encoded data

Returns

abstract_data; dict representing the decoded data

encode_record_bin(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).

Parameters

abstract_data – dict representing the decoded data

Returns

binary encoded data

encode_record_hex(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).

Parameters

abstract_data – dict representing the decoded data

Returns

hex string encoded data

class pySim.filesystem.TransparentEF(fid: str, sfid: Optional[str] = None, name: Optional[str] = None, desc: Optional[str] = None, parent: Optional[CardDF] = None, size={1, None}, **kwargs)

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.

Parameters
  • fid – File Identifier (4 hex digits)

  • sfid – Short File Identifier (2 hex digits, optional)

  • name – Brief name of the file, lik EF_ICCID

  • desc – Description of the file

  • parent – Parent CardFile object within filesystem hierarchy

  • size – tuple of (minimum_size, recommended_size)

class ShellCommands

Shell commands specific for transparent EFs.

do_decode_hex(opts)

Decode command-line provided hex-string as if it was read from the file.

do_edit_binary_decoded(opts)

Edit the JSON representation of the EF contents in an editor.

do_read_binary(opts)

Read binary data from a transparent EF

do_read_binary_decoded(opts)

Read + decode data from a transparent EF

do_update_binary(opts)

Update (Write) data of a transparent EF

do_update_binary_decoded(opts)

Encode + Update (Write) data of a transparent EF

decode_bin(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).

Parameters

raw_bin_data – binary encoded data

Returns

abstract_data; dict representing the decoded data

decode_hex(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).

Parameters

raw_hex_data – hex-encoded data

Returns

abstract_data; dict representing the decoded data

encode_bin(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).

Parameters

abstract_data – dict representing the decoded data

Returns

binary encoded data

encode_hex(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).

Parameters

abstract_data – dict representing the decoded data

Returns

hex string encoded data

pySim.filesystem.interpret_sw(sw_data: dict, sw: str)

Interpret a given status word.

Parameters
  • 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)

pySim commands abstraction

pySim: SIM Card commands according to ISO 7816-4 and TS 11.11

pySim Transport

The pySim.transport classes implement specific ways how to communicate with a SIM card. A “transport” provides ways to transceive APDUs with the card.

The most commonly used transport uses the PC/SC interface to utilize a variety of smart card interfaces (“readers”).

Transport base class

pySim: PCSC reader transport link base

class pySim.transport.LinkBase(sw_interpreter=None, apdu_tracer=None)

Base class for link/transport to card.

abstract connect()

Connect to a card immediately

abstract disconnect()

Disconnect from card

abstract reset_card()

Resets the card (power down/up)

send_apdu(pdu)

Sends an APDU and auto fetch response data

Parameters

pdu – string of hexadecimal characters (ex. “A0A40000023F00”)

Returns

tuple(data, sw), where

data : string (in hex) of returned data (ex. “074F4EFFFF”) sw : string (in hex) of status word (ex. “9000”)

send_apdu_checksw(pdu, sw='9000')

Sends an APDU and check returned SW

Parameters
  • pdu – string of hexadecimal characters (ex. “A0A40000023F00”)

  • sw – string of 4 hexadecimal characters (ex. “9000”). The user may mask out certain digits using a ‘?’ to add some ambiguity if needed.

Returns

tuple(data, sw), where

data : string (in hex) of returned data (ex. “074F4EFFFF”) sw : string (in hex) of status word (ex. “9000”)

send_apdu_constr(cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr)

Build and sends an APDU using a ‘construct’ definition; parses response.

Parameters
  • cla – string (in hex) ISO 7816 class byte

  • ins – string (in hex) ISO 7816 instruction byte

  • p1 – string (in hex) ISO 7116 Parameter 1 byte

  • p2 – string (in hex) ISO 7116 Parameter 2 byte

  • cmd_cosntr – defining how to generate binary APDU command data

  • cmd_data – command data passed to cmd_constr

  • resp_cosntr – defining how to decode binary APDU response data

Returns

Tuple of (decoded_data, sw)

send_apdu_constr_checksw(cla, ins, p1, p2, cmd_constr, cmd_data, resp_constr, sw_exp='9000')

Build and sends an APDU using a ‘construct’ definition; parses response.

Parameters
  • cla – string (in hex) ISO 7816 class byte

  • ins – string (in hex) ISO 7816 instruction byte

  • p1 – string (in hex) ISO 7116 Parameter 1 byte

  • p2 – string (in hex) ISO 7116 Parameter 2 byte

  • cmd_cosntr – defining how to generate binary APDU command data

  • cmd_data – command data passed to cmd_constr

  • resp_cosntr – defining how to decode binary APDU response data

  • exp_sw – string (in hex) of status word (ex. “9000”)

Returns

Tuple of (decoded_data, sw)

send_apdu_raw(pdu: str)

Sends an APDU with minimal processing

Parameters

pdu – string of hexadecimal characters (ex. “A0A40000023F00”)

Returns

tuple(data, sw), where

data : string (in hex) of returned data (ex. “074F4EFFFF”) sw : string (in hex) of status word (ex. “9000”)

set_sw_interpreter(interp)

Set an (optional) status word interpreter.

abstract wait_for_card(timeout: Optional[int] = None, newcardonly: bool = False)

Wait for a card and connect to it

Parameters
  • timeout – Maximum wait time in seconds (None=no timeout)

  • newcardonly – Should we wait for a new card, or an already inserted one ?

pySim.transport.argparse_add_reader_args(arg_parser)

Add all reader related arguments to the given argparse.Argumentparser instance.

pySim.transport.init_reader(opts, **kwargs) Optional[LinkBase]

Init card reader driver

calypso / OsmocomBB transport

This allows the use of the SIM slot of an OsmocomBB compatible phone with the TI Calypso chipset, using the L1CTL interface to talk to the layer1.bin firmware on the phone.

Transport Link for Calypso based phones.

connect()

Connect to a card immediately

disconnect()

Disconnect from card

reset_card()

Resets the card (power down/up)

wait_for_card(timeout=None, newcardonly=False)

Wait for a card and connect to it

Parameters
  • timeout – Maximum wait time in seconds (None=no timeout)

  • newcardonly – Should we wait for a new card, or an already inserted one ?

AT-command Modem transport

This transport uses AT commands of a cellular modem in order to get access to the SIM card inserted in such a modem.

Transport Link for 3GPP TS 27.007 compliant modems.

connect()

Connect to a card immediately

disconnect()

Disconnect from card

reset_card()

Resets the card (power down/up)

wait_for_card(timeout=None, newcardonly=False)

Wait for a card and connect to it

Parameters
  • timeout – Maximum wait time in seconds (None=no timeout)

  • newcardonly – Should we wait for a new card, or an already inserted one ?

PC/SC transport

PC/SC is the standard API for accessing smart card interfaces on all major operating systems, including the MS Windows Family, OS X as well as Linux / Unix OSs.

pySim: PCSC reader transport link.

connect()

Connect to a card immediately

disconnect()

Disconnect from card

reset_card()

Resets the card (power down/up)

wait_for_card(timeout: Optional[int] = None, newcardonly: bool = False)

Wait for a card and connect to it

Parameters
  • timeout – Maximum wait time in seconds (None=no timeout)

  • newcardonly – Should we wait for a new card, or an already inserted one ?

Serial/UART transport

This transport implements interfacing smart cards via very simplistic UART readers. These readers basically wire together the Rx+Tx pins of a RS232 UART, provide a fixed crystal oscillator for clock, and operate the UART at 9600 bps. These readers are sometimes called Phoenix.

pySim: Transport Link for serial (RS232) based readers included with simcard

connect()

Connect to a card immediately

disconnect()

Disconnect from card

reset_card()

Resets the card (power down/up)

wait_for_card(timeout=None, newcardonly=False)

Wait for a card and connect to it

Parameters
  • timeout – Maximum wait time in seconds (None=no timeout)

  • newcardonly – Should we wait for a new card, or an already inserted one ?

pySim construct utilities

class pySim.construct.BcdAdapter(subcon)

convert a bytes() type to a string of BCD nibbles.

pySim.construct.BitsRFU(n=1)

Field that packs Reserved for Future Use (RFU) bit(s) as defined in TS 31.101 Sec. “3.4 Coding Conventions”

Use this for (currently) unused/reserved bits whose contents should be initialized automatically but should not be cleared in the future or when restoring read data (unlike padding).

Parameters

n (Integer) – Number of bits (default: 1)

pySim.construct.BytesRFU(n=1)

Field that packs Reserved for Future Use (RFU) byte(s) as defined in TS 31.101 Sec. “3.4 Coding Conventions”

Use this for (currently) unused/reserved bytes whose contents should be initialized automatically but should not be cleared in the future or when restoring read data (unlike padding).

Parameters

n (Integer) – Number of bytes (default: 1)

class pySim.construct.GreedyInteger(signed=False, swapped=False, minlen=0)

A variable-length integer implementation, think of combining GrredyBytes with BytesInteger.

pySim.construct.GsmString(n)

GSM 03.38 encoded byte string of fixed length n. Encoder appends padding bytes (b’xff’) to maintain length. Decoder removes those trailing bytes.

Exceptions are raised for invalid characters and length excess.

Parameters

n (Integer) – Fixed length of the encoded byte string

class pySim.construct.GsmStringAdapter(subcon, codec='gsm03.38', err='strict')

Convert GSM 03.38 encoded bytes to a string.

class pySim.construct.HexAdapter(subcon)

convert a bytes() type to a string of hex nibbles.

class pySim.construct.InvertAdapter(subcon)

inverse logic (false->true, true->false).

class pySim.construct.Rpad(subcon, pattern=b'\xff')

Encoder appends padding bytes (b’xff’) up to target size. Decoder removes trailing padding bytes.

Parameters
  • subcon – Subconstruct as defined by construct library

  • pattern – set padding pattern (default: b’xff’)

pySim.construct.filter_dict(d, exclude_prefix='_')

filter the input dict to ensure no keys starting with ‘exclude_prefix’ remain.

pySim.construct.normalize_construct(c)

Convert a construct specific type to a related base type, mostly useful so we can serialize it.

pySim.construct.parse_construct(c, raw_bin_data: bytes, length: Optional[int] = None, exclude_prefix: str = '_')

Helper function to wrap around normalize_construct() and filter_dict().

pySim TLV utilities

object-oriented TLV parser/encoder library.

class pySim.tlv.BER_TLV_IE(**kwargs)

TLV_IE formatted as ASN.1 BER described in ITU-T X.690 8.1.2.

class pySim.tlv.COMPR_TLV_IE(**kwargs)

TLV_IE formated as COMPREHENSION-TLV as described in ETSI TS 101 220.

class pySim.tlv.IE(**kwargs)

Base class for various Information Elements. We understand the notion of a hierarchy of IEs on top of the Transcodable class.

from_bytes(do: bytes)

Parse _the value part_ from binary bytes to internal representation.

from_dict(decoded: dict)

Set the IE internal decoded representation to data from the argument. If this is a nested IE, the child IE instance list is re-created.

is_constructed()

Is this IE constructed by further nested IEs?

to_bytes() bytes

Convert the internal representation _of the value part_ to binary bytes.

to_dict()

Return a JSON-serializable dict representing the [nested] IE data.

abstract to_ie() bytes

Convert the internal representation to entire IE including IE header.

class pySim.tlv.TLV_IE(**kwargs)

Abstract base class for various TLV type Information Elements.

to_ie()

Convert the internal representation to entire IE including IE header.

to_tlv()

Convert the internal representation to binary TLV bytes.

class pySim.tlv.TLV_IE_Collection(desc=None, **kwargs)

A TLV_IE_Collection consists of multiple TLV_IE classes identified by their tags. A given encoded DO may contain any of them in any order, and may contain multiple instances of each DO.

from_bytes(binary: bytes) List[TLV_IE]

Create a list of TLV_IEs from the collection based on binary input data. :param binary: binary bytes of encoded data

Returns

list of instances of TLV_IE sub-classes containing parsed data

from_dict(decoded: List[dict]) List[TLV_IE]

Create a list of TLV_IE instances from the collection based on an array of dicts, where they key indicates the name of the TLV_IE subclass to use.

class pySim.tlv.TlvCollectionMeta(name, bases, namespace, **kwargs)

Metaclass which we use to set some class variables at the time of defining a subclass. This allows us to create subclasses for each Collection type, where the class represents fixed parameters like the nested IE classes and instances of it represent the actual TLV data.

class pySim.tlv.TlvMeta(name, bases, namespace, **kwargs)

Metaclass which we use to set some class variables at the time of defining a subclass. This allows us to create subclasses for each TLV/IE type, where the class represents fixed parameters like the tag/type and instances of it represent the actual TLV data.

class pySim.tlv.Transcodable
from_bytes(do: bytes)

Convert from binary bytes to internal representation. Store the decoded result in the internal state and return it.

to_bytes() bytes

Convert from internal representation to binary bytes. Store the binary result in the internal state and return it.

pySim.tlv.flatten_dict_lists(inp)

hierarchically flatten each list-of-dicts into a single dict. This is useful to make the output of hierarchical TLV decoder structures flatter and more easy to read.

pySim utility functions

pySim: various utilities

class pySim.utils.CardCommand(name, ins, cla_list=None, desc=None)

A single card command / instruction.

match_cla(cla)

Does the given CLA match the CLA list of the command?.

class pySim.utils.CardCommandSet(name, cmds=[])

A set of card instructions, typically specified within one spec.

lookup(ins, cla=None)

look-up the command within the CommandSet.

class pySim.utils.DataObject(name: str, desc: Optional[str] = None, tag: Optional[int] = None)

A DataObject (DO) in the sense of ISO 7816-4. Contrary to ‘normal’ TLVs where one simply has any number of different TLVs that may occur in any order at any point, ISO 7816 has the habit of specifying TLV data but with very spcific ordering, or specific choices of tags at specific points in a stream. This class tries to represent this.

Parameters
  • name – A brief, all-lowercase, underscore separated string identifier

  • desc – A human-readable description of what this DO represents

  • tag – The tag associated with this DO

decode(binary: bytes) Tuple[dict, bytes]

Decode a single DOs from the input data. :param binary: binary bytes of encoded data

Returns

tuple of (decoded_result, binary_remainder)

abstract from_bytes(do: bytes)

Parse the value part of the DO into the internal state of this instance. :param do: binary encoded bytes

from_tlv(do: bytes) bytes

Parse binary TLV representation into internal state. The resulting decoded representation is _not_ returned, but just internalized in the object instance! :param do: input bytes containing TLV-encoded representation

Returns

bytes remaining at end of ‘do’ after parsing one TLV/DO.

abstract to_bytes() bytes

Encode the internal state of this instance into the TLV value part. :returns: binary bytes encoding the internal state

to_dict() dict

Return a dict in form “name: decoded_value”

to_tlv() bytes

Encode internal representation to binary TLV. :returns: bytes encoded in TLV format.

class pySim.utils.DataObjectChoice(name: str, desc: Optional[str] = None, members=None)

One Data Object from within a choice, identified by its tag. This means that exactly one member of the choice must occur, and which one occurs depends on the tag.

decode(binary: bytes) Tuple[dict, bytes]

Decode a single DOs from the choice based on the tag. :param binary: binary bytes of encoded data

Returns

tuple of (decoded_result, binary_remainder)

class pySim.utils.DataObjectCollection(name: str, desc: Optional[str] = None, members=None)

A DataObjectCollection consits of multiple Data Objects identified by their tags. A given encoded DO may contain any of them in any order, and may contain multiple instances of each DO.

decode(binary: bytes) Tuple[List, bytes]

Decode any number of DOs from the collection until the end of the input data, or uninitialized memory (0xFF) is found. :param binary: binary bytes of encoded data

Returns

tuple of (decoded_result, binary_remainder)

class pySim.utils.DataObjectSequence(name: str, desc: Optional[str] = None, sequence=None)

A sequence of DataObjects or DataObjectChoices. This allows us to express a certain ordered sequence of DOs or choices of DOs that have to appear as per the specification. By wrapping them into this formal DataObjectSequence, we can offer convenience methods for encoding or decoding an entire sequence.

decode(binary: bytes) Tuple[list, bytes]

Decode a sequence by calling the decoder of each element in the sequence. :param binary: binary bytes of encoded data

Returns

tuple of (decoded_result, binary_remainder)

decode_multi(do: bytes) Tuple[list, bytes]

Decode multiple occurrences of the sequence from the binary input data. :param do: binary input data to be decoded

Returns

list of results of the decoder of this sequences

encode(decoded) bytes

Encode a sequence by calling the encoder of each element in the sequence.

encode_multi(decoded) bytes

Encode multiple occurrences of the sequence from the decoded input data. :param decoded: list of json-serializable input data; one sequence per list item

Returns

binary encoded output data

class pySim.utils.JsonEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Extend the standard library JSONEncoder with support for more types.

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (‘, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

default(o)

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
class pySim.utils.TL0_DataObject(name: str, desc: str, tag: int, val=None)

Data Object that has Tag, Len=0 and no Value part.

Parameters
  • name – A brief, all-lowercase, underscore separated string identifier

  • desc – A human-readable description of what this DO represents

  • tag – The tag associated with this DO

from_bytes(binary: bytes)

Parse the value part of the DO into the internal state of this instance. :param do: binary encoded bytes

to_bytes() bytes

Encode the internal state of this instance into the TLV value part. :returns: binary bytes encoding the internal state

pySim.utils.TLV_parser([0xAA, ..., 0xFF]) [T, L, [V], T, L, [V], ...]

loops on the input list of bytes with the “first_TLV_parser()” function returns a list of 3-Tuples

pySim.utils.all_subclasses(cls) set

Recursively get all subclasses of a specified class

pySim.utils.auto_int(x)

Helper function for argparse to accept hexadecimal integers.

pySim.utils.b2h(b: bytearray) str

convert from a sequence of bytes to a string of hex nibbles

pySim.utils.bertlv_encode_len(length: int) bytes

Encode a single Length value according to ITU-T X.690 8.1.3; only the definite form is supported here. :param length: length value to be encoded

Returns

binary output data of BER-TLV length field

pySim.utils.bertlv_encode_tag(t) bytes

Encode a single Tag value according to ITU-T X.690 8.1.2

pySim.utils.bertlv_parse_len(binary: bytes) Tuple[int, bytes]

Parse a single Length value according to ITU-T X.690 8.1.3; only the definite form is supported here. :param binary: binary input data of BER-TLV length field

Returns

Tuple of (length, remainder)

pySim.utils.bertlv_parse_one(binary: bytes) Tuple[dict, int, bytes, bytes]

Parse a single TLV IE at the start of the given binary data. :param binary: binary input data of BER-TLV length field

Returns

dict, len:int, remainder:bytes)

Return type

Tuple of (tag

pySim.utils.bertlv_parse_tag(binary: bytes) Tuple[dict, bytes]

Parse a single Tag value according to ITU-T X.690 8.1.2 :param binary: binary input data of BER-TLV length field

Returns

int, constructed:bool, tag:int}, remainder:bytes)

Return type

Tuple of ({class

pySim.utils.bertlv_parse_tag_raw(binary: bytes) Tuple[int, bytes]

Get a single raw Tag from start of input according to ITU-T X.690 8.1.2 :param binary: binary input data of BER-TLV length field

Returns: Tuple of (tag:int, remainder:bytes)

pySim.utils.boxed_heading_str(heading, width=80)

Generate a string that contains a boxed heading.

pySim.utils.calculate_luhn(cc) int

Calculate Luhn checksum used in e.g. ICCID and IMEI

pySim.utils.comprehensiontlv_encode_tag(tag) bytes

Encode a single Tag according to ETSI TS 101 220 Section 7.1.1

pySim.utils.comprehensiontlv_parse_one(binary: bytes) Tuple[dict, int, bytes, bytes]

Parse a single TLV IE at the start of the given binary data. :param binary: binary input data of BER-TLV length field

Returns

dict, len:int, remainder:bytes)

Return type

Tuple of (tag

pySim.utils.comprehensiontlv_parse_tag(binary: bytes) Tuple[dict, bytes]

Parse a single Tag according to ETSI TS 101 220 Section 7.1.1

pySim.utils.comprehensiontlv_parse_tag_raw(binary: bytes) Tuple[int, bytes]

Parse a single Tag according to ETSI TS 101 220 Section 7.1.1

pySim.utils.dec_addr_tlv(hexstr)

Decode hex string to get EF.P-CSCF Address or EF.ePDGId or EF.ePDGIdEm. See 3GPP TS 31.102 version 13.4.0 Release 13, section 4.2.8, 4.2.102 and 4.2.104.

pySim.utils.dec_ePDGSelection(sixhexbytes)

Decode ePDGSelection to get EF.ePDGSelection or EF.ePDGSelectionEm. See 3GPP TS 31.102 version 15.2.0 Release 15, section 4.2.104 and 4.2.106.

pySim.utils.dec_imsi(ef: str) Optional[str]

Converts an EF value to the IMSI string representation

pySim.utils.dec_msisdn(ef_msisdn: str) Optional[Tuple[int, int, Optional[str]]]

Decode MSISDN from EF.MSISDN or EF.ADN (same structure). See 3GPP TS 31.102, section 4.2.26 and 4.4.2.3.

pySim.utils.dec_spn(ef)

Obsolete, kept for API compatibility

pySim.utils.dec_st(st, table='sim') str

Parses the EF S/U/IST and prints the list of available services in EF S/U/IST

pySim.utils.derive_mcc(digit1: int, digit2: int, digit3: int) int

Derive decimal representation of the MCC (Mobile Country Code) from three given digits.

pySim.utils.derive_milenage_opc(ki_hex: str, op_hex: str) str

Run the milenage algorithm to calculate OPC from Ki and OP

pySim.utils.derive_mnc(digit1: int, digit2: int, digit3: int = 15) int

Derive decimal representation of the MNC (Mobile Network Code) from two or (optionally) three given digits.

pySim.utils.enc_addr_tlv(addr, addr_type='00')

Encode address TLV object used in EF.P-CSCF Address, EF.ePDGId and EF.ePDGIdEm. See 3GPP TS 31.102 version 13.4.0 Release 13, section 4.2.8, 4.2.102 and 4.2.104.

Default values:
  • addr_type: 00 - FQDN format of Address

pySim.utils.enc_ePDGSelection(hexstr, mcc, mnc, epdg_priority='0001', epdg_fqdn_format='00')

Encode ePDGSelection so it can be stored at EF.ePDGSelection or EF.ePDGSelectionEm. See 3GPP TS 31.102 version 15.2.0 Release 15, section 4.2.104 and 4.2.106.

Default values:
  • epdg_priority: ‘0001’ - 1st Priority

  • epdg_fqdn_format: ‘00’ - Operator Identifier FQDN

pySim.utils.enc_imsi(imsi: str)

Converts a string IMSI into the encoded value of the EF

pySim.utils.enc_msisdn(msisdn: str, npi: int = 1, ton: int = 3) str

Encode MSISDN as LHV so it can be stored to EF.MSISDN. See 3GPP TS 31.102, section 4.2.26 and 4.4.2.3. (The result will not contain the optional Alpha Identifier at the beginning.)

Default NPI / ToN values:
  • NPI: ISDN / telephony numbering plan (E.164 / E.163),

  • ToN: network specific or international number (if starts with ‘+’).

pySim.utils.enc_plmn(mcc: str, mnc: str) str

Converts integer MCC/MNC into 3 bytes for EF

pySim.utils.enc_spn(name: str, show_in_hplmn=False, hide_in_oplmn=False)

Obsolete, kept for API compatibility

pySim.utils.enc_st(st, service, state=1)

Encodes the EF S/U/IST/EST and returns the updated Service Table

Parameters
  • Table (st - Current value of SIM/USIM/ISIM Service) –

  • activated/de-activated (service - Service Number to encode as) –

  • activate (state - 1 mean) –

  • de-activate (0 means) –

Returns

s - Modified value of SIM/USIM/ISIM Service Table

Default values:
  • state: 1 - Sets the particular Service bit to 1

pySim.utils.expand_hex(hexstring, length)
Expand a given hexstring to a specified length by replacing “.” or “..”

with a filler that is derived from the neighboring nibbles respective bytes. Usually this will be the nibble respective byte before “.” or “..”, execpt when the string begins with “.” or “..”, then the nibble respective byte after “.” or “..” is used.”. In case the string cannot be expanded for some reason, the input string is returned unmodified.

Parameters
  • hexstring – hexstring to expand

  • length – desired length of the resulting hexstring.

Returns

expanded hexstring

pySim.utils.first_TLV_parser([0xAA, 0x02, 0xAB, 0xCD, 0xFF, 0x00]) -> (170, 2, [171, 205])

parses first TLV format record in a list of bytelist returns a 3-Tuple: Tag, Length, Value Value is a list of bytes parsing of length is ETSI’style 101.220

pySim.utils.get_addr_type(addr)

Validates the given address and returns it’s type (FQDN or IPv4 or IPv6) Return: 0x00 (FQDN), 0x01 (IPv4), 0x02 (IPv6), None (Bad address argument given)

TODO: Handle IPv6

pySim.utils.h2b(s: str) bytearray

convert from a string of hex nibbles to a sequence of bytes

pySim.utils.h2i(s: str) List[int]

convert from a string of hex nibbles to a list of integers

pySim.utils.h2s(s: str) str

convert from a string of hex nibbles to an ASCII string

pySim.utils.i2h(s: List[int]) str

convert from a list of integers to a string of hex nibbles

pySim.utils.i2s(s: List[int]) str

convert from a list of integers to an ASCII string

pySim.utils.is_hex(string: str, minlen: int = 2, maxlen: Optional[int] = None) bool

Check if a string is a valid hexstring

pySim.utils.lpad(s: str, l: int, c='f') str

pad string on the left side. :param s: string to pad :param l: total length to pad to :param c: padding character

Returns

String ‘s’ padded with as many ‘c’ as needed to reach total length of ‘l’

pySim.utils.mcc_from_imsi(imsi: str) Optional[str]

Derive the MCC (Mobile Country Code) from the first three digits of an IMSI

pySim.utils.mnc_from_imsi(imsi: str, long: bool = False) Optional[str]

Derive the MNC (Mobile Country Code) from the 4th to 6th digit of an IMSI

pySim.utils.rpad(s: str, l: int, c='f') str

pad string on the right side. :param s: string to pad :param l: total length to pad to :param c: padding character

Returns

String ‘s’ padded with as many ‘c’ as needed to reach total length of ‘l’

pySim.utils.s2h(s: str) str

convert from an ASCII string to a string of hex nibbles

pySim.utils.sanitize_pin_adm(pin_adm, pin_adm_hex=None) str

The ADM pin can be supplied either in its hexadecimal form or as ascii string. This function checks the supplied opts parameter and returns the pin_adm as hex encoded string, regardless in which form it was originally supplied by the user

pySim.utils.str_sanitize(s: str) str

replace all non printable chars, line breaks and whitespaces, with ‘ ‘, make sure that there are no whitespaces at the end and at the beginning of the string.

Parameters

s – string to sanitize

Returns

filtered result of string ‘s’

pySim.utils.sw_match(sw: str, pattern: str) bool

Match given SW against given pattern.

pySim.utils.swap_nibbles(s: str) str

swap the nibbles in a hex string

pySim.utils.tabulate_str_list(str_list, width: int = 79, hspace: int = 2, lspace: int = 1, align_left: bool = True) str

Pretty print a list of strings into a tabulated form.

Parameters
  • width – total width in characters per line

  • space – horizontal space between cells

  • lspace – number of spaces before row

  • align_lef – Align text to the left side

Returns

multi-line string containing formatted table

pySim exceptions

pySim: Exceptions

exception pySim.exceptions.NoCardError

No card was found in the reader.

exception pySim.exceptions.ProtocolError

Some kind of protocol level error interfacing with the card.

exception pySim.exceptions.ReaderError

Some kind of general error with the card reader.

exception pySim.exceptions.SwMatchError(sw_actual: str, sw_expected: str, rs=None)

Raised when an operation specifies an expected SW but the actual SW from the card doesn’t match.

Parameters
  • sw_actual – the SW we actually received from the card (4 hex digits)

  • sw_expected – the SW we expected to receive from the card (4 hex digits)

  • rs – interpreter class to convert SW to string

pySim card_handler

pySim: card handler utilities. A ‘card handler’ is some method by which cards can be inserted/removed into the card reader. For normal smart card readers, this has to be done manually. However, there are also automatic card feeders.

class pySim.card_handler.CardHandler(sl: LinkBase)

Manual card handler: User is prompted to insert/remove card from the reader.

class pySim.card_handler.CardHandlerAuto(sl: LinkBase, config_file: str)

Automatic card handler: A machine is used to handle the cards.

class pySim.card_handler.CardHandlerBase(sl: LinkBase)

Abstract base class representing a mechanism for card insertion/removal.

done()

Method called when pySim failed to program a card. Move card to ‘good’ batch.

error()

Method called when pySim failed to program a card. Move card to ‘bad’ batch.

get(first: bool = False)

Method called when pySim needs a new card to be inserted.

Parameters

first – set to true when the get method is called the first time. This is required to prevent blocking when a card is already inserted into the reader. The reader API would not recognize that card as “new card” until it would be removed and re-inserted again.

pySim card_key_provider

Obtaining card parameters (mostly key data) from external source.

This module contains a base class and a concrete implementation of obtaining card key material (or other card-individual parameters) from an external data source.

This is used e.g. to keep PIN/PUK data in some file on disk, avoiding the need of manually entering the related card-individual data on every operation with pySim-shell.

class pySim.card_key_provider.CardKeyProvider

Base class, not containing any concrete implementation.

abstract get(fields: List[str], key: str, value: str) Dict[str, str]

Get multiple card-individual fields for identified card.

Parameters
  • fields – list of valid field names such as ‘ADM1’, ‘PIN1’, … which are to be obtained

  • key – look-up key to identify card data, such as ‘ICCID’

  • value – value for look-up key to identify card data

Returns

dictionary of {field, value} strings for each requested field from ‘fields’

get_field(field: str, key: str = 'ICCID', value: str = '') Optional[str]

get a single field from CSV file using a specified key/value pair

class pySim.card_key_provider.CardKeyProviderCsv(filename: str)

Card key provider implementation that allows to query against a specified CSV file

Parameters

filename – file name (path) of CSV file containing card-individual key/data

get(fields: List[str], key: str, value: str) Dict[str, str]

Get multiple card-individual fields for identified card.

Parameters
  • fields – list of valid field names such as ‘ADM1’, ‘PIN1’, … which are to be obtained

  • key – look-up key to identify card data, such as ‘ICCID’

  • value – value for look-up key to identify card data

Returns

dictionary of {field, value} strings for each requested field from ‘fields’

pySim.card_key_provider.card_key_provider_get(fields, key: str, value: str, provider_list=[]) Dict[str, str]

Query all registered card data providers for card-individual [key] data.

Parameters
  • fields – list of valid field names such as ‘ADM1’, ‘PIN1’, … which are to be obtained

  • key – look-up key to identify card data, such as ‘ICCID’

  • value – value for look-up key to identify card data

  • provider_list – override the list of providers from the global default

Returns

dictionary of {field, value} strings for each requested field from ‘fields’

pySim.card_key_provider.card_key_provider_get_field(field: str, key: str, value: str, provider_list=[]) Optional[str]

Query all registered card data providers for a single field.

Parameters
  • field – name valid field such as ‘ADM1’, ‘PIN1’, … which is to be obtained

  • key – look-up key to identify card data, such as ‘ICCID’

  • value – value for look-up key to identify card data

  • provider_list – override the list of providers from the global default

Returns

dictionary of {field, value} strings for the requested field

pySim.card_key_provider.card_key_provider_register(provider: CardKeyProvider, provider_list=[])

Register a new card key provider.

Parameters
  • provider – the to-be-registered provider

  • provider_list – override the list of providers from the global default