Commit Graph

21 Commits

Author SHA1 Message Date
Philipp Maier
852eff54df pySim/transport add support for T=1 protocol and fix APDU/TPDU layer conflicts
ETSI TS 102 221, section 7.3 specifies that UICCs (and eUICCs) may support two
different transport protocols: T=0 or T=1 or both. The spec also says that the
terminal must support both protocols.

This patch adds the necessary functionality to support the T=1 protocol
alongside the T=0 protocol. However, this also means that we have to sharpen
the lines between APDUs and TPDUs.

As this patch also touches the low level interface to readers it was also
manually tested with a classic serial reader. Calypso and AT command readers
were not tested.

Change-Id: I8b56d7804a2b4c392f43f8540e0b6e70001a8970
Related: OS#6367
2024-11-19 10:56:26 +01:00
Harald Welte
2fe9b6a3e9 pySim.transport: Also trace card reset events in ApduTracer
Change-Id: Ia46b65124520eb2b8015dfa3f0a135b497668b92
2024-09-10 20:37:56 +02:00
Harald Welte
a3962b2076 Migrate over to using pyosmocom
We're creating a 'pyosmocom' pypi module which contains a number of core
Osmocom libraries / interfaces that are not specific to SIM card stuff
contained here.

The main modules moved in this initial step are pySim.tlv, pySim.utils
and pySim.construct. utils is split, not all of the contents is
unrelated to SIM Cards.  The other two are moved completely.

Change-Id: I4b63e45bcb0c9ba2424dacf85e0222aee735f411
2024-09-03 21:57:47 +02:00
Harald Welte
c800f2a716 pySim-trace: display decoded result as JSON, not as python dict
This means users can copy+paste or otherwise post-process the data in a
standard format.

Change-Id: I3135f2f52b8d61684a71b836915b43da5c48422b
2024-07-24 10:37:05 +02:00
Harald Welte
16749075f9 pySim-trace: Add support for the TCA Loader log file format
The "TCA Loader" is a freeware utility program published by the
Trusted Connectivity Alliance for testing SCP80, SCP81, SCP02 and SCP03
in UICCs.  It can generate text log files of the APDUs it exchanges;
let's add this file format to pySim-trace

Change-Id: Ie76d36bb18c6bd8968d2a5b74ec1b8c5ccaaa409
2024-06-08 18:38:22 +02:00
Harald Welte
568d8cf5db pySim-trace.py: Resolve possible variable use before assignment
pySim-trace.py:198:27: E0606: Possibly using variable 's' before assignment (possibly-used-before-assignment)

Change-Id: I28c137a20143b2cd6ea9a0d5461ab61fcd6fe935
2024-05-22 18:03:59 +02:00
Harald Welte
73a5c74114 pySim-trace: Support decoding of eUICC traces
Let's register the ISD-R and ECASD applications so we avoid the warnings
printed when processing an eUICC protocol trace:

WARNING  pySim.apdu.ts_102_221: SELECT UNKNOWN AID a0000005591010ffffffff8900000100

Change-Id: I362a1a7f12d979ff0b7971d5300db9ed56bb1ee5
2024-05-10 20:30:58 +02:00
Harald Welte
cfa62cb95b Allow logger to do lazy evaluation of format strings
Change-Id: I39d26cdd5b85a61a06fd8c7a9d0a046e398819bd
2024-01-08 12:10:11 +00:00
Philipp Maier
6bfa8a8533 pySim-shell: print device info in case an exception occurs
When an exception occurs while initializing or handling the card we
print a traceback, but we do not print any info that allows us to
identify the device that was involved when the exception occurred. Let's
include the device path or number in the error message before we print
the traceback.

In order to make it easier to print the device information, let's add a
__str__() method to all of our devices. This method shall return the
device number or path.

Related: OS#6210
Change-Id: I200463e692245da40ea6d5b609bfc0ca02d15bdb
2023-10-10 11:51:08 +02:00
Philipp Maier
8dc2ca2d37 pySim-trace: catch StopIteration exception on trace file end
When the trace file end is reaced, pyShark raises a StopIteration
exception. Let's catch this exception and exit gracefully.

Related: OS#6094
Change-Id: I6ab5689b909333531d08bf46e5dfea59b161a79e
2023-07-28 10:36:52 +02:00
Philipp Maier
162ba3af3e pySim-trace: mark card reset in the trace
The trace log currently does not contain any information about card
resets. This makes the trace difficult to follow. Let's use the
CardReset object to display the ATR in the trace.

Related: OS#6094
Change-Id: Ia550a8bd2f45d2ad622cb2ac2a2905397db76bce
2023-07-28 10:14:19 +02:00
Philipp Maier
784b947b11 pySim-trace: remove stray debug print
Related: OS#6094
Change-Id: I5f030a8552a84f721bd12ab4751933fc6eeae256
2023-07-28 10:14:19 +02:00
Philipp Maier
407c95520f pySim-trace: add commandline option --show-raw-apdu
The trace log currently only shows the parsed APDU. However, depending
on the problem to investigate it may be required to see the raw APDU
string as well. Let's add an option for this.

Related: OS#6094
Change-Id: I1a3bc54c459e45ed3154479759ceecdc26db9d37
2023-07-28 10:07:35 +02:00
Harald Welte
531894d386 move Runtime{State,Lchan} from pySim.filesystem to new pySim.runtime
Those two are really separate concepts, so let's keep them in separate
source code files.

Change-Id: I9ec54304dd8f4a4cba9487054a8eb8d265c2d340
2023-07-12 22:05:14 +02:00
Harald Welte
323a35043f Introduce concept of CardProfileAddon
We have a strict "one CardProfile per card" rule.  For a modern UICC
without legacy SIM support, that works great, as all applications
have AID and ADF and can hence be enumerated/detected that way.

However, in reality there are mostly UICC that have legacy SIM, GSM-R
or even CDMA support, all of which are not proper UICC applications
for historical reasons.

So instead of having hard-coded hacks in various places, let's introduce
the new concept of a CardProfileAddon.  Every profile can have any
number of those.  When building up the RuntimeState, we iterate over the
CardProfile addons, and probe which of those are actually on the card.
For those discovered, we add their files to the filesystem hierarchy.

Change-Id: I5866590b6d48f85eb889c9b1b8ab27936d2378b9
2023-07-12 22:05:14 +02:00
Harald Welte
f8d2e2ba08 split pySim/legacy/{cards,utils} from pySim/{cards,utils}
There are some functions / classes which are only needed by the legacy
tools pySim-{read,prog}, bypassing our modern per-file transcoder
classes.  Let's move this code to the pySim/legacy sub-directory,
rendering pySim.legacy.* module names.

The long-term goal is to get rid of those and have all code use the
modern pySim/filesystem classes for reading/decoding/encoding/writing
any kind of data on cards.

Change-Id: Ia8cf831929730c48f90679a83d69049475cc5077
2023-07-12 22:03:59 +02:00
Harald Welte
7e55569f3a docs: Add section on pySim-trace to user manual
Change-Id: I5edb222818f00e36ed5b067e0f8d5786f39ae887
2023-06-13 15:10:25 +00:00
Harald Welte
c95f6e2124 pySim-trace: Add support for reading GSMTAP from pcap files
So far we supported
* GSMTAP live traces via a UDP socket
* RSPRO traces from pcap files (or live)

We were lacking support for reading GSMTAP stored in pcap, which
is what this patch implements.

Change-Id: I46d42774b39a2735500ff5804206ddcfa545568c
2023-01-24 13:50:51 +01:00
Harald Welte
86d698d310 pySim-trace: Don't print argparse object at start-up
Change-Id: I881471d026457d8ffcfdbd412c7aae0d0bff9344
2022-07-24 10:23:50 +02:00
Harald Welte
72c5b2d796 pySim-trace: Fix --no-suppress-{select.status} command line arguments
The Tracer implemented those options and the argparser handled it,
but we didn't ever connect the two.

Change-Id: I7d7d5fc475a8d09efdb63d3d6f1cc1de1996687b
2022-07-24 10:23:50 +02:00
Harald Welte
21caf32e3d Introduce APDU/TPDU trace decoder
This introduces a new pySim.apdu module hierarchy, which contains
classes that represent TPDU/APDUs as exchanged between
SIM/UICC/USIM/ISIM card and UE.

It contains instruction level decoders for SELECT, READ BINARY and
friends, and then uses the pySim.filesystem.Runtime{Lchan,State} classes
to keep track of the currently selected EF/DF/ADF for each logical
channel, and uses the file-specific decoder classes of pySim to decode
the actual file content that is being read or written.

This provides a much more meaningful decode of protocol traces than
wireshark will ever be able to give us.

Furthermore, there's the new pySim.apdu_source set of classes which
provides "input plugins" for obtaining APDU traces in a variety of
formats.  So far, GSMTAP UDP live capture and pyshark based RSPRO
live and pcap file reading are imlpemented.

Change-Id: I862d93163d495a294364168f7818641e47b18c0a
Closes: OS#5126
2022-07-23 12:18:57 +02:00