Commit Graph

640 Commits

Author SHA1 Message Date
Philipp Maier d454fe7843 filesystem: do not read AIDs when no apps are defined
When the profile does not define any ADFs, then do not try to read any
AIDs. This is the case for old non UICC SIMs for example.

Change-Id: I8cfbee1d23e9f99461fa5f4fbf92c1a0929c50bf
Related: OS#5274
2021-11-10 14:10:11 +01:00
Philipp Maier 5af7bdf5c7 filesystem: fix decode_select_response
There are some problems with the usage of decode_select_response. At the
moment the ADF files overload the related method to provide decoding of
the select responses as per 3gpp TS 102 221. However, this also means
that the decoder is only available under ADF.USIM and ADF.ISIM. DF.GSM
and DF.TELECOM also overload the decoder method, just like an ADF would
do. This decoding method is then implemented as per 3gpp TS 51 011.
Since this a a problem on UICCs, the method detects the magic byte 0x62
that can be found at the beginning on every select response of an UICC
to defer to the TS 102 221 decoding method. TS 51 011 defines the first
two bytes of the select response as RFU. This at least problematic.

To solve this there should be a default method for
decode_select_response in the profile, which can be used if no file
overloads it with a specific decoder. ADFs use specific decoders, but
everything else should use the default decoder. When we deal with an
UICC, we expect the select response to be consistantly conform to TS
102 221, if we deal with a clasic sim we expect responses as per TS 51
011 only.

Since it is still possible to replace the select response decoder we
still have the opportunity to have custom select response in cartain
DFs and ADFs should we need them.

Change-Id: I95e33ec1755727dc9bbbc6016ce2d99a9e66f214
Related: OS#5274
2021-11-10 14:10:11 +01:00
Philipp Maier 9764de202b pySim-shell: print newline on exit with CTRL+D
When pySim-shell is exited using CTRL+D it does not print a newline.
This means that the prompt of the OS shell shows up after the
pySim-shell prompt. This is irretating. Lets print a new line on exit
with CTRL+D so that everything looks straight.

Change-Id: I88e58094b9badeaabd8502006e5e16f35eaa683e
2021-11-08 16:34:53 +01:00
Philipp Maier e087f909b3 commands: return none, when offset exceeds file length
The computed length of the file may be negative, when the offset exceeds
the file length. When this is the case, return none

Change-Id: I2c017c620254fae188022851ef3b670730aab503
2021-11-05 16:55:48 +00:00
Philipp Maier 712251a6e0 commands: complete documentation strings
Some of the methods lack an explaination of the arguments. Lets add that
to be complete

Change-Id: Icda245e2fd5ef4556c7736d73574dfbb48168973
2021-11-05 16:55:27 +00:00
Philipp Maier 1db33115ea utils: cosmetic: remove stray comment
The comment is already covered by the help string, lets remove it.

Change-Id: Ide2080ddb898441b6af70e32511b33ced23d0023
2021-11-05 16:55:01 +00:00
Philipp Maier 796ca3daf9 commands: do not check SW manually, use send_apdu_checksw()
The transport layer provides a method send_apdu_checksw to send APDUs
and to be sure the SW is the expected one. Given that, there is no need
to verify the SW manually. The exception of send_apdu_checksw will catch
the problem and also display the SW in a human readable form.

Change-Id: I9ce556ac0b7bb21c5c5a27170c32af0152255b79
Related: OS#5275
2021-11-05 16:54:43 +00:00
Vadim Yanitskiy fc769e2fdb contrib/jenkins.sh: make pylint warn about unnecessary semicolon
Change-Id: I7793e30501ad109c95b207cbfada50596de17cda
Related: OS#5292
2021-11-05 16:22:06 +03:00
Vadim Yanitskiy dbd5ed64d7 Python is not C: get rid of unnecessary semicolons
See https://www.python.org/dev/peps/pep-0008/.

Change-Id: I9de3bcd324b0a1b98af761678996eaae85f7f790
Related: OS#5292
2021-11-05 16:22:06 +03:00
Harald Welte 23198c4e90 sim-rest-server: Add example systemd service/unit file
the sim-rest-server is a minimal HTTP/RESTful API for performing
UMTS-AKA against a SIM card inserted in a locally reachable PC/SC
reader.  Let's add s systemd service/unit file for people wanting to
run this service from systemd.

Change-Id: I84b390af09d33de2c740898ff3d7d5a90a300588
2021-11-03 12:50:15 +01:00
Harald Welte df3d01bedc sim-rest-client: Add support for 'info' command to get IMSI+ICCID
Change-Id: Ia2a13033b1d3e009a841579184f4ad39101f94d0
2021-11-03 12:34:24 +01:00
Harald Welte 33f8da8a52 sim-rest-server: Add capability to obtain IMSI + ICCID of card
$ curl http://localhost:8000/sim-info-api/v1/slot/0
{
    "imsi": "262011500776110",
    "iccid": "89490240001879910128"
}

Change-Id: I9df8854f6a962e7f86f62b2d44ec7696271c58c8
2021-11-03 12:34:24 +01:00
Harald Welte 7a401a24bb sim-rest-client: Errors are plain text, not JSON
don't try to decode JSON where there is none.

Change-Id: Iafa5d1fc20b2b9ea8d9c828fc3c7e8490d0c3693
2021-11-03 12:34:24 +01:00
Philipp Maier c8387dc031 ts_51_011: implement CardProfileSIM as a class
CardProfileSIM is currenty instantiated directly. However, it should be
implemented as class and then instaniated later like CardProfileUICC

Change-Id: I37d49b11a07ce5a80d1a703fab4620b7d1ecb25b
2021-10-29 18:51:28 +02:00
Philipp Maier 946226a5d1 filesystem: fix reset mechanism
Currently we call the reset_card and get_atr methods directly at the
transport layer via the private _scc and _tp object of the card. This is
a violation. Fix and use the reset methods that are already in the
SimCard object.

Change-Id: I0e9d2a62a42a7387e7ca69d2ae830782a61aed89
2021-10-29 18:51:28 +02:00
Philipp Maier 30b225f3bf cards: add method to modify APDU parameters (cla, sel_ctrl)
There are situations where it is necessary to modify the class byte and
the selection control bytes of a card at runtime. This should not be
done by accessing the properties of the _scc object directly. The
modification of those properties should be done via a set method
instead.

Change-Id: Ifd8aa2660e44a18d28945d070419612eff443e78
2021-10-29 18:51:28 +02:00
Philipp Maier 51e4cb7a8f commands: use python style commends to describe methods
Change-Id: Iccc9f01769ee9274d01036d3fbbc161d8bca7628
2021-10-29 18:51:28 +02:00
Philipp Maier 305e1f8ee4 cards: use python style commends to describe methods
Change-Id: Iae862d8f0a191c7015a94f9516ef5804265f7a82
2021-10-29 18:10:35 +02:00
Harald Welte bd02f84fbd pySim-shell: Improve documentation
A number of new commands were recently introduced without proper
coverage in the documentation (user manual).  This includes equip,
bulk_script and others.

Change-Id: Ide7ba68ad90f6e5c2a41a2e3de22534258ebb7fd
2021-10-21 14:41:43 +02:00
Harald Welte 9a75410a88 utils: Fix BER-TLV tag decode for multi-byte tags
We cannot simply skip anything that has 0xFF as first byte to detect
the padding after the end of a TLV object:

0xFF may very well be a valid first octet of a multi-octet TAG:

Tags of private class (11) with constructed (1) payload will have 0xFF
as first octet.

So let's expand the check to only detect padding in case of either only
a single byte FF being left, or two FF following each other [with
whatever suffix].

Change-Id: I5d64ce9ef1d973804daabae0b15c2e2349e6fab9
2021-10-21 14:23:19 +02:00
Harald Welte 5895380a45 tlv: Fix recursive initialization from_dict()
When calling from_dict() on a hierarchy of nested BER_TLV_IE,
only the first/outer layer of TLV_IE_Collection would get its
'decoded' initialized correctly from the dict.  Subsequent layers
were not, as the 'decoded=' was passed as parameter during instance
initialization.  If we first instantiate the class and then call the
from_dict() method, the recursive initialization down the full hierarchy
works as expected.

Change-Id: I795a33ed8dfa8454dc9079c189ab7b2ba64a3b72
2021-10-21 14:12:13 +02:00
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