Commit Graph

719 Commits

Author SHA1 Message Date
Harald Welte 04c1302b68 tlv: Don't require encoder/decoder methods for TLV without value
There are instances where a TLV IE is used as just a flag, i.e.
length zero and no value part.  In those situations, it would require
a lot of boilerplate code to require the TLV_IE class definitions to
have _to_bytes/_from_bytes methods that do nothing.

So instead, add a shortcut: If we want to encode 'None', then return
b'', and if we want to decode b'' return None.

Change-Id: Ie8eb2830e8eefa81e94b8b8b157062c085aeb777
2021-10-21 14:12:13 +02:00
Harald Welte 0bc5326b76 pySim-shell: Use iteration when unregistering command sets
This makes it more easy to extend in the future

Change-Id: I53ecf03d330fe1eb80ee920e6f9c8185f9be48fd
2021-10-21 14:12:13 +02:00
Harald Welte ec95053249 pySim-shell: Add suspend_uicc command
This is an optional command, and it is not supported by e.g.  sysmoISIM-SJA2

Change-Id: Icc726ffd672744e56cc8dd3762891af507942c1e
2021-10-21 14:12:13 +02:00
Harald Welte 2a33ad2c49 pySim-shell: Add support for GSM-R SIM Cards with DF.EIRENE
GSM-R SIM cards have an additional directory (DF.EIRENE) with a number
of files.  This is all specified in the following document:

	UIC Reference P38 T 9001 5.0 "FFFIS for GSM-R SIM Cards"

Change-Id: I4034d09292a08d277d4abcbed9a0ec2808daaacb
2021-10-21 10:05:47 +02:00
Philipp Maier 76667644ea pySim-shell: add bulk provisioning support
There are scenarios where multiple cards need to get the same change.
Lets add a new command that takes a script as parameter and executes the
secript in a loop on multiple cards while prompting the user to change
the card before starting the next cycle.

Change-Id: I9e4926675c5a497a22fc6a4fefdd388fe18a2b2d
Related: SYS#5617
2021-10-20 10:48:44 +02:00
Philipp Maier 57f65eedfc ts_31_10x: add a class for CardApplicationXSIM
In change Id410489841bb9020ddbf74de9114d808b1d5adb6, the RuntimeState
class automatically adds additional files to the CardApplications for
ISIM and USIM. This works only once. The second time an exception will
be thrown because the added files are already in the CardApplication.
Currently there is no way generate new card applications during
initialization because the card applications are just objects that are
created once in ts_31_10x.py. Lets turn them into classes and create the
objects during initialization. This way we get fresh objects when we
re-initialize.

Change-Id: Ibb4f6242e7a92af84a905daa727b1b87016e7819
2021-10-18 14:18:33 +02:00
Harald Welte 9b227de719 pySim-shell: add example script to dump authentication config
you can use this like that:

	./pySim-shell.py -p0 --script ./scripts/sysmoISIM-SJA2/dump-auth-cfg.pysim

Change-Id: I5eac1af63d586f2371f519a160e1005fcbb27bfb
2021-10-16 10:46:05 +02:00
Harald Welte eb45838c47 sysmocom_sja2: Properly decode EF.USIM_SQN freshness
The freshness parameter is not one opaque bytestring, but an array
of 6-byte integer values.

before:
    "freshness": "000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"

after:
    "freshness": [ 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

Change-Id: I53edbcdb40652eb0e2bc4c982cf7b6f7d585cab9
2021-10-16 10:46:05 +02:00
Harald Welte 7a8aa863a6 ts_51_011: Add encoder for EF.SST
We already have those for EF.UST, let's add them for EF.SST, too

Change-Id: Ib51bfffaf8444ec30415aad42e3a0f4f3f7598cb
2021-10-15 18:24:28 +00:00
Harald Welte 611dd783f6 commands: Fix read_binary() for non-zero offset
Similar to the fix in Ie1aeaab29701946233ed73db3331039690d695da
for update_binary(), read_binary() also contained a bug when treating
non-zero offsets.

Change-Id: Ic5c2f0ad1c1ec9c4e9c97e72895382f7b6fa9470
Related: OS#5254
2021-10-15 18:24:28 +00:00
Philipp Maier b52feede25 pySim-shell: add echo command
There is no convinient way to echo strings from scripts.

Change-Id: Iaed1d24eeb7f887e46957971083cd30d8d1bea6c
Related: SYS#5617
2021-10-15 10:36:55 +02:00
Philipp Maier 5d698e575a pySim-shell: allow card insertion at runtime
Currently a card must be present in the reader until the user can enter
pySim-shell. Removing and plugging another card is in theory already
possible, but then the new card will operate on the old card and runtime
state object. It might also be useful to enter pySim-shell before the
card is plugged to execute some other commands for preperation before.

So lets allow to "equip" pySim-shell with a card and rs object at
runtime.

Related: SYS#5617
Change-Id: I9cf532d9da8203065463c7201e7064de6c7ab1b5
2021-10-15 10:36:55 +02:00
Philipp Maier 3c309886e9 ts_51_011: fix select response decoder
The select response decoder is using b2h() wrongly. b2h expects
a bytearray but we call it with an integer. In the following two
lines we try to convert an integer to an integer.

Change-Id: Ib6448d3bd7a0dc7f25e5ee82a42266b3313e2a95
2021-10-15 10:36:55 +02:00
Harald Welte 80901d6d39 commands: fix update_binary() with non-zero offset
In Icc240d5c8c04198640eb118565ea99f10ba27466 we introduced support for
writing files > 255 bytes by splitting the write into multiple chunks.

However, at the same time, that commit broke support for writing data at
non-zero offsets.  Unfortunately, this is used extensively within
pySim-prog e.g. for writing K + OP/OPc data to sysmoISIM-SJA2 and sysmoUSIM-SJS1
cards.

This commit fixes the related problem.

Change-Id: Ie1aeaab29701946233ed73db3331039690d695da
Fixes: Icc240d5c8c04198640eb118565ea99f10ba27466
Closes: OS#5254
2021-10-14 19:13:08 +02:00
Harald Welte 4c1dca04a5 CardModel: Document how this 'magic' works in some comments.
Change-Id: If16ade6e1098a87f749259bad6dea805ddb61ede
2021-10-14 19:10:16 +02:00
Harald Welte f898c28284 51.011: Fix EF_SST decoder for services > table description
Before:
EXCEPTION of type 'KeyError' occurred with message: '60'

After:
    "60": {
        "description": null,
        "allocated": false,
        "activated": false
    }

Change-Id: Ic089f9632a936bdbedd2344442678c5bf9797713
2021-10-14 16:41:57 +02:00
Harald Welte 1aae4a0b88 filsystem: Make NotImplementedError more verbose.
Before:
EXCEPTION of type 'NotImplementedError' occurred with message: ''

After:
EXCEPTION of type 'NotImplementedError' occurred with message: 'EF(EF.SST) encoder not yet implemented. Patches welcome.'

Change-Id: Ie8a10a8847f7c7c6a3332fb9f78de18c9f7f41d0
2021-10-14 16:41:57 +02:00
Harald Welte 7cb94e4193 USIM: Add ENVELOPE and ENVELOPE-SMS shell commands
Change-Id: I4345459d99c0eeb5753217ad4e7188747c51a2a2
2021-10-14 16:41:57 +02:00
Harald Welte 846a898ee0 Add API + shell command for sending TERMINAL PROFILE to card
This allows a very first start to play with PROACTIVE SIM

Change-Id: Id8f23f7cebe0f9efce2c0ce4229509f35cd93d6a
2021-10-14 16:41:57 +02:00
Harald Welte f44256c7df pysim-Shell: Add sysmocom SJA2 card specific bits
Depending on the ATR, we register the various sysmocom SJA2 card
model specific files [or not].

Change-Id: Id410489841bb9020ddbf74de9114d808b1d5adb6
2021-10-14 16:41:57 +02:00
Harald Welte 9edbdb93f7 cat: Fix SMS/ENVELOPE related IE tag definitions
Change-Id: I7ed4d78479b6ff75cde20cb5b0bd1672035806ff
2021-10-14 16:16:23 +02:00
Harald Welte f235954ebb cat: Add more terminal profile bitmask definitions
Change-Id: Ie9934f684956381f6e57ded2140951e473cb09ec
2021-10-14 16:16:23 +02:00
Philipp Maier 6477309107 cards: remove "auto_once" from possible ctype options
The card_detect function in cards.py allows to specify the card type or
use the hints "auto" and "auto_once" to trigger autodetection of the
card. However, "auto_once" has no effect and is not used by any caller,
so lets remove it.

Change-Id: Iea726f51e5ddb43d8a4da2672552fff38e29b006
2021-10-13 16:27:18 +02:00
Harald Welte 913e6166d8 card_handler: clean-up
* introduce type annotations
* introduce + derive implementations from base class
* move shared code to base class

Change-Id: I7168506cbebb1ebb67f47453419b860824912051
2021-10-11 10:56:44 +02:00
Philipp Maier 2bc21316e5 cards: remove unused function card_autodetect()
The function card_autodetect() is not used, lets remove it.

Change-Id: Ic188e1fffb4a40e89ad276941d20f94cf35e1538
2021-10-08 09:33:16 +00:00
Philipp Maier ea95c39cdc pySim-shell: refactor __main__ section
The code in __main__ which initalizes the reader and the card and
runtime state is not so well structured. Lets put the generation of the
card and rs (RuntimeState) object into a separate function. Also do not
wait indefinetly for a card. 3 seconds should be enough. If the card or
reader did not respond until then, then there will be a problem in any
case.

Change-Id: Id2a0f2012b84ce61f5c0c14404df559fca4ddfcd
Related: SYS#5617
2021-10-08 08:31:34 +00:00
Philipp Maier 64b28378bc cards: FairwavesSIM: force SIM APDUs during programming
The FairwavesSIM programming fails when the card is accessed with USIM
APDUs. To keep it working temporarly switch to SIM APDUs during
programming.

Change-Id: I8f02625d2b620ecdf4b2afc27a8750119b707152
2021-10-05 13:58:25 +02:00
Philipp Maier c5501af9a8 pySim-prog: fix typo
Change-Id: I94818e5646ac2beb57052d93b5599f0b877de83b
2021-10-01 18:11:29 +02:00
Philipp Maier f0241451d3 pySim-shell: verify_adm: turn error messages into exceptions
When verify_adm is used with scripts, especially bulk provisioning, then
an exception is far more visible and allows us to spot problems with ADM
verification quicker.

Change-Id: I4162b43754efd061b6b9058b7ff8e1fc985e3538
Related: SYS#5617
2021-10-01 14:05:43 +02:00
Philipp Maier 48e1b90eb8 card_handler: make reader (sl) operations optional.
The constructor gets an sl object on initalization. The card handler
will then carry out the reader operation wait_for_card().

In cases where an mechanically automated card reader is used it may
be useful to go without those operations and let the caller carry out
the appropriate reader operations. So Lets make the sl object
optional for the CardHandlerAuto class. If it is not present, simply
do not carry out the pre programmed reader operation.

Change-Id: I0f793aec51751b7c7b87d55b66326cce9970274e
Related: SYS#5617
2021-09-29 15:41:42 +02:00
Philipp Maier 8bf2125a19 transport/pcsc: make sure reader is disconnected
Make sure that a reader is disconnected before connecting it. This will
efectively prevent resource leakage in the lower PCSC layers when the
reader is connected multiple times during bulk provisioning

Change-Id: I266e56f2330da25c680a76f4c0ca630a38e1f61b
2021-09-22 16:21:16 +02:00
Philipp Maier 7328f10165 transport/init: print exception type if the execption has no string
There may be corner cases where an execption contains no error message.
In this case it might still be helpful to display the type of the
exeption calss to get at least an idea of what kind of error we are
dealing with.

Change-Id: I6e6b3acd17e40934050b9b088960a2f851120b26
2021-09-22 16:11:33 +02:00
Philipp Maier af0f086497 pySim-prog: rename card_handler option to card_handler_config
The option and also the dest variable in the code are currently named
card_handler. This might be confusing since the variable actually refers
to a config file and therefore should be called "card_handler_config"

Change-Id: If93751e815cb46f9ff3f56b54e612d77fe1a6dfd
2021-09-22 16:11:33 +02:00
Philipp Maier a8c9ea9cc7 pySim-shell: move command desc and verify_adm to PySimCommands
Almost all pySim-shell related commands are agrgated in PySimCommands.
There are a few exceptions, so there are some commands in PysimApp.
However, it makes sense to reserve PysimApp exclusively for very basic
commands that do not directly relate to card operations. So lets move
the command verify_adm and desc to PySimCommands.

Change-Id: I4a215c8a3907d69f702a70df9b85988be1ce3dbf
2021-09-22 16:11:33 +02:00
Philipp Maier b18eed072c pySim-prog: rename card_handler to CardHandler
In OOP, we usually use capital letters for class names. The card handler
class should be no execption.

Change-Id: I4b2c06b1c607c993c9aaf0d57ad2352bb6b36e74
2021-09-20 10:02:09 +02:00
Philipp Maier 82511e5218 pySim-prog: rename variable card_handler
The variable card_handler is assigned in the following way:

card_handler = card_handler(sl)

This may cause problems since the class name and the variable name are
the same. Lets rename card_handler to avoid problems here.

Change-Id: I84dafc49862e373ae9f6a56bd2e8d1a02c27430a
2021-09-17 13:39:47 +02:00
Joachim Steiger c3927ec580 update readme detail about cmd2 - make sure people get 1.5 from pip instead of some old debian-pkg
Change-Id: I92a1e4c5a34ca11ce8d8b5f69257fdfedad2f8d6
2021-08-27 17:14:52 +02:00
andrew-ma 465ad32097 Use README.md as long description in package metadata
The normal description can be accessed with `python3 setup.py --description`. The long description can be accessed with `python3 setup.py --long-description`.

Change-Id: I1581a2b9ad7c2b5ed64b77e5e277df792b37990d
2021-08-03 08:38:55 -07:00
andrew-ma 2e6dc03f34 Allow update_binary function to write more than 255 bytes
The T0 protocol (selected in transport/pcsc.py) does not support extended APDU, so 255 bytes is the maximum number of bytes that can be transmitted at a time.  We can divide large data into 255 byte chunks.  The read_binary function already has code to read more than 255 bytes, so we can just adapt it to the update_binary function.

Change-Id: Icc240d5c8c04198640eb118565ea99f10ba27466
2021-07-31 22:29:23 -07:00
Philipp Maier bb73e516cb cards: rename class "Card" to "SimCard"
There are the classes IsimCard and UsimCard, which inheret from Card,
which is the base class for a normal non ISIM/USIM simcard. Card also
has methods in it that are related to simcards, so it is not just any
"Card", it is a SimCard and should be called that way.

Change-Id: I2077ded44bc2297b8d478c5bd1895951b494efcc
2021-06-30 08:17:12 +00:00
Harald Welte f201166999 pySim/commands: Add envelope() method for ENVELOPE command
Change-Id: I2b5b6585ecbe00b54919b197428fe09a220757c6
2021-06-13 22:31:05 +02:00
Harald Welte 14105dce99 implement more files with TLV + construct
This adds encoding/decoding for more files, from 51.011 (SIM)
to 31.102 (USIM) and 31.103 (ISIM)

Change-Id: I6083d2bb0a307f660f09af384803f84e4098a5ed
2021-06-13 22:31:05 +02:00
Harald Welte 592b32ec91 ts_31_102: Fully support USIM EF.AD
The USIM EF.AD has quite some more bits, it should have a separate
implementation and not reuse te DF.GSM/EF.AD implementation.

Change-Id: Iaf195cb63d5d12fc906a7e7cd85e3fd44589a41e
2021-06-13 22:15:45 +02:00
Harald Welte f12979dd58 ts_31_102: Start using pySim.tlv to implement more DF.5GS files
Change-Id: I8ef2d8c24530d13929282e1a1d2138d319b894c6
2021-06-13 22:15:45 +02:00
Harald Welte 1a4e9fd163 cmd2: Constrain version to >= 1.3.0 but < 2.0.0
2.0.0 introduces several incompatible changes, see
https://github.com/python-cmd2/cmd2/blob/master/CHANGELOG.md
as well as https://github.com/python-cmd2/cmd2/issues/1120

As we want to be able to use what distributions ship, let's stay
with 1.x for now.  If piip is used, use 1.5

Change-Id: Iecc953269d5ae9ed9f31b829743c63bdfd29fa61
2021-06-11 23:47:26 +02:00
Harald Welte fb50621570 filesystem: Introduce support for TLV parser
This adds an easy way for files to make use of the pySim.tlv parser.

All a file has to do is to specify a _tlv member which points to
either a TLV_IE or a TLV_IE_Collection instance.

Change-Id: I59f456b4223ec88081e91cee168b654c69bcb5f4
2021-06-05 10:47:17 +02:00
Harald Welte bb3b5df8bf Introduce new object-oriented TLV parser/decoder/encoder
This introduces a new TLV library that heavily builds upon python object
oriented concepts.  Contrary to classic TLV parsers it doesn't focus on
the structure of Tag, Length and binary Value only, but it supports
actual decoding/interpretation of the value part into some kind of JSON
serializable dict.  The latter can be achieved by imperative
encode/decode methods, or by using our existing declarative 'construct'
based approach.

The TLV library supports both BER-TLV and COMPREHENSION-TLV for both
nested and non-nested TLV definitions.

As an example we include TLV definitions for a number of CAT (Card
Application Toolkit) IEs.

Change-Id: I7fc1699443bc9d8a4e7cdd2687af9af7cc03c30e
2021-06-05 10:47:17 +02:00
Harald Welte 8f892fbbe8 ts_102_221: Add construct for contents of EF.UMPC
Change-Id: I7c63ccca90ab34b0d6ac6c990eeb53279ef2cd8d
2021-06-05 10:47:17 +02:00
Harald Welte 07c7b1f592 construct: Recursive normalization of construct parse result
If we want to use construct parse results to generate JSON serializable
dicts, we need to

* apply the filter_dict() operation recursively, and
* simplify the construct Container and ListContainer classes to
  a simple dict and/or list.

We introduce a pySim.construct.parse_construct() helper which is
subsequently used from all pySim.filesystem caller sites.

Change-Id: I319414eb69808ef65895293832bb30519f45949d
2021-06-05 10:47:17 +02:00
Harald Welte 7fca85b42c utils: Make filter_dict() transparently pass non-dict
Change-Id: Ia1802101a62e21f1ce894d80728f939bf3da5a39
2021-06-05 10:47:17 +02:00