Commit Graph

38 Commits

Author SHA1 Message Date
Harald Welte a630a3cd28 cosmetic: Remove extraneous empty lines between spec-section-comment and class
This is an artefact of the recent autopep8 re-formatting.

Change-Id: I8b0e7781719d69e18856ada2f482de2c5396bcc3
2022-02-14 00:51:27 +01:00
Harald Welte 6169c72f82 USIM + ISIM: Specify the services associated with each file
This allows us [in a future patch] to perform consistency checking,
whether files exist for services not activated in EF.{UST,IST} or
vice-versa: Services are activated by files are not present or
deactivated.

Change-Id: I94bd1c3f9e977767553000077dd003423ed6dbd1
2022-02-14 00:51:27 +01:00
Harald Welte c91085e744 cosmetic: Switch to consistent four-spaces indent; run autopep8
We had a mixture of tab and 4space based indenting, which is a bad
idea.  4space is the standard in python, so convert all our code to
that.  The result unfortuantely still shoed even more inconsistencies,
so I've decided to run autopep8 on the entire code base.

Change-Id: I4a4b1b444a2f43fab05fc5d2c8a7dd6ddecb5f07
2022-02-11 13:32:58 +01:00
Harald Welte aaf5931b60 ts_51_011: Fix type annotation for Tuple[int, int]
Thanks to Vadim for pointing this out.

Change-Id: I7ee1309331902bafab3c9fc6bc33ca713f8c7832
2022-02-10 14:53:56 +01:00
Harald Welte 6113fe9929 ts_51_011: Fix typo in EF_MMSUP
The TLV decoder class must be in self._tlv, not self.tlv

Change-Id: Ide6f6c823d5a16e375c324ba9bfa92e02c3b3c89
2022-02-09 20:23:27 +00:00
Bjoern Riemer e91405e04e implement shell command to update PLMN in IMSI
Add file specific command `update_imsi_plmn` to EF_IMSI to replace
the mcc and mnc part of the imsi for use in bulk_script(s)

Change-Id: I9662ff074acf9dc974ae4c78edac44db286e98fc
2022-01-31 11:55:12 +00:00
Harald Welte 2a701eea60 cosmetic: Use EF.FDN instead of EF_FDN in ts_51_011.py
All the files have '.' as separator in their names so far, let's avoid
any inconsistencies

Change-Id: Icabb892408a40ea37c7ebeb7db545b383aa01d99
2022-01-22 12:59:02 +00:00
Philipp Maier 9e42e7ffce profile: decode_select_response use object instead 'Any'
the return type of decode_select_response is 'Any', lets be more
specific and use 'object'

Change-Id: Ic5c7ace234bc94ab1381d87e091369ade8011cab
2021-11-19 13:21:32 +01:00
Philipp Maier 5998a3a8b3 profile: decode_select_response can be a static method
The method decode_select_response does not access any property of the
object. This means the method can be static.

Change-Id: Idd7aaebcf1ab0099cd40a88b8938604e84d8a88b
2021-11-19 13:21:32 +01:00
Philipp Maier 4ab971c62e ts_51_011: move _decode_select_response into profile class
The method decode_select_response just calls the function
_decode_select_response. But the function _decode_select_response
is not called from any other location, so we can move it into the
profile class.

Change-Id: Icf0143f64ca7d1c1ebf60ba06585f7afc1ac0d11
2021-11-19 13:21:32 +01:00
Philipp Maier a028c7d7aa pySim-shell: add method to match card profile to card
UICC and old SIM cards can be difficult to tell apart without prior
knowledge of the card. The ATR won't tell if the card is UICC or not.
The only remaining option is to try out if the card is able to handle
UICC APDUs. The same is true for 2G SIM cards. It is not guranteed that
every UICC card will have 2G functionality.

Lets add functionality to match a profile to the currently plugged card
by actively probing it.

Lets also add another profile to distinguish between UICC-only cards and
UICC cards that include SIM functionality.

Change-Id: If090d32551145f75c644657b90085a3ef5bfa691
Related: OS#5274
2021-11-19 13:21:32 +01:00
Philipp Maier a4df942fe6 ts_51_011: add status word definition
There is no status word definition given in the SIM profile. Lets add
one to be complete

Change-Id: I01f2643a93e4a9b2ce2f95134aa5d773179d9b1c
2021-11-11 08:34:18 +00:00
Philipp Maier 51cad0d234 filesystem: define class byte and select control bytes in profile
The class byte and the select control bytes are different for SIM cards
and UICC cards. Lets define those parameters in the card profile, so
that we always get the correct parameters depending on which profile we
use.

Change-Id: I2d175e28bd748a4871b1373273b3a9be9ae8c4d0
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 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
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
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 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 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
Robert Falkenberg b07a3e9c87 Add codecs for EF_SPN and GSM strings via construct
This will replace the hand-crafted codec for EF_SPN
by a struct definition using the construct library.
Old encoders are updated and kept for API compatibility
but are not used internally anymore.

New data structures:
* Rpad(Adapter): Right-padded bytestring (0xff, adjustable)
* GsmStringAdapter(Adapter): Codec for "SMS default 7-bit
	coded alphabet as defined int TS 23.038" using
	the gsm0338 library.
* GsmString(n): Convenient wrapper of both above

Adjustments:
* utils: update+deprecate old dec_spn(), enc_spn()
* remove refs to deprecated functions

Change-Id: Ia1d3a3835933bac0002b7c52511481dd8094b994
2021-05-10 06:15:39 +02:00
Philipp Maier c957ce8adc ts_51_011: fix encoder of EF_SPN:
The encoder for EF_SPN is passing the 'spn' parameter (which is a list)
directly to enc_spn without taking it apart first.

Change-Id: I0a405793c8909d4279e634b93dcb76e5cb2963f3
Related: OS#4963
2021-05-07 21:52:30 +00:00
Philipp Maier e7d417955d ts_51_011, utils: fix Access Technology Identifier coding
When the Access Technology Identifier encoder sets the bits for E-UTRAN
it does not respect that bit "100" is also a valid bit combination that
encodes E-UTRAN WB-S1 and E-UTRAN NB-S1. Lets encode this bit
combination if the user is just specifying "E-UTRAN" without further
spefication of WB or NB.

The decoder only looks at bit 14 and decodes "1xx" always to "E-UTRAN".
This is not specific enough. Lets make sure that the decoder is
complementary to the encoder.

Change-Id: Ibfe8883a05f9ad6988d8e212cb9a598229954296
Related: OS#4963
2021-05-03 17:08:37 +02:00
Vadim Yanitskiy 1a95d2be61 [pylint] fix non-existing 'res' in EF_CNL._encode_record_hex()
pySim/ts_51_011.py:648:45: E0602: Undefined variable 'res' (undefined-variable)
pySim/ts_51_011.py:648:68: E0602: Undefined variable 'res' (undefined-variable)
pySim/ts_51_011.py:649:24: E0602: Undefined variable 'res' (undefined-variable)

Change-Id: I039e2b271dd3ab44f437108c5e8def43e97098a3
2021-05-02 20:22:15 +00:00
Philipp Maier f9cbe090e4 ts_51_011: fix encoding of EF.MSISDN
The json input that is used with EF.MSISDN seems to be somewhat
ambigious. The original code accepts {"msisdn": "+4916012345678"}
only while the output is {"msisdn": [1, 1, "+4916012345678"]}. Lets
add a check and also accept the latter version.

Change-Id: I8f8dd68aac25d3fa3bc1aab06b855f8ec6640258
Related: OS#4963
2021-04-30 17:04:52 +02:00
Philipp Maier b46cb3ffa2 utils: fix encoding of EF.MSISDN
The encoding of EF.MSISDN is a bit unstrutured. The encoder function
does not return a valid result since it lacks the parameters
Capability/Configuration2 Record Identifier and Extension5 Record
Identifier, which are mandatory but can be set to 0xFF. Also the
encoder gets its input from pySim-shell, so it should have some
more input validation, especially when the user encodes an empty
string. The encoder and decoder function also do not have unit-tests.

Since the encoder now adds the missing two bytes by isself this does
not have to be done manually anymore, so cards.py needs to be
re-aligned.

For pySim-shell.py the encoder is used from ts_51_011.py. Unfortunately
it is used wrongly there. The optional Alpha Identifier is required
here as well.

Related: OS#4963
Change-Id: Iee5369b3e3ba7fa1155facc8fa824bc60e33b55b
2021-04-23 15:52:10 +02:00
Robert Falkenberg 9d16fbca4a Use construct for EF_AD in pySim-{shell, prog, read}.py, cards.py
Also serves as example for RFU (reserved for future use) fields
which should not always be reset to zero in case they have been
set on the uSIM for some reason.
See pySim/ts_51_011.py, class EF_AD.

* Add definitions for RFU {Flag, Bits, Byte, Bytes}
* Use IntEnum for OP_MODE (convenient auto completion)
* Remove obsolete definitions and imports
* Update test results for all SIMs (opmode strings are shortened)

Change-Id: I65e0a426f80a619fec38856a30e590f0e726b554
2021-04-13 11:27:37 +00:00
Harald Welte c9cdce3e02 fix various typos all over the code
Change-Id: Ic8392a951bf94f67b51e35bed95d0e856f7a9250
2021-04-11 12:20:29 +02:00
Harald Welte 790b2709bd lots of file definitions for classic SIM and USIM
Change-Id: I91475df4a5eaca423473aaebba8c69c54c9a0c1a
2021-04-11 12:20:29 +02:00
Harald Welte c9baa4d915 ts_51_011: Full encoder/decoder for EF.AD
The EF.AD class only had a partial decoder and no encoder before this
patch.

You can now do things like

pySIM-shell (MF/ADF.USIM/EF.AD)> read_binary_decoded
{
    "ms_operation_mode": "normal_and_specific_facilities",
    "specific_facilities": {
        "ofm": false
    },
    "len_of_mnc_in_imsi": 2
}
pySIM-shell (MF/ADF.USIM/EF.AD)> update_binary_decoded '{"ms_operation_mode": "normal_and_specific_facilities", "specific_facilities": {"ofm": false}, "len_of_mnc_in_imsi": 3}'

not quite all that elegant yet, but working at all.

Change-Id: Id2cb66cb26b6bd08befe9f8468b0b0773da842b1
2021-04-06 20:20:57 +02:00
Harald Welte ec7d0daf6e 51.011: Define some more files within DF_TELECOM
Add some more minimal definitions for various DF_TELECOM files

Change-Id: I155729b4d62969cde2af00fc9fb9901299fe5c25
2021-04-02 22:05:28 +02:00
Harald Welte 89e5954773 fix various file definitions
As we can notice during 'export': Some files had been defined
as LinFixed but are Transparent - and vice versa.  Let's fix those
an bring our definitions in sync with the specs.

Change-Id: I365ece7b82a1c79b3af87a79ff964d7989362789
2021-04-02 22:05:28 +02:00
Vadim Yanitskiy 5452d64120 ts_51_011: fix bitmask compositing in EF_xPLMNwAcT.enc_act()
This commit fixes two problems (found by semgrep):

  * "'foo' and 'bar' in list" is incorrect, because it's interpreted
    as "'foo' and ('bar' in list)".  Strings with a non-zero length
    evaluate to True, thus it's True if at least 'bar' is present.

  * Copy-pasted 'E-UTRAN NB-S1' checked two times.

The first condition is redundant, and the whole block can be
re-implemented using two independent 'if' statements.

Change-Id: Iceb66160cfb571db8879d3810c55d252c763d320
2021-03-07 21:52:13 +01:00
Harald Welte b2edd14475 Add a new pySim-shell program
pySim-prog was nice when there were only 5 parameters on a SIM that we
could program, and where the use case was pretty limited.  Today, we
have SIM/USIM/ISIM cards with hundreds of files and even more parameters
to program.  We cannot add a command line argument for each file to
pySim-prog.

Instead, this introduces an interactive command-line shell / REPL,
in which one can navigate the file system of the card, read and update
files both in raw format and in decoded/parsed format.

The idea is primarily inspired by Henryk Ploatz' venerable
cyberflex-shell, but implemented on a more modern basis using
the cmd2 python module.

See https://lists.osmocom.org/pipermail/simtrace/2021-January/000860.html
and https://lists.osmocom.org/pipermail/simtrace/2021-February/000864.html
for some related background.

Most code by Harald Welte. Some bug fixes by Philipp Maier
have been squashed.

Change-Id: Iad117596e922223bdc1e5b956f84844b7c577e02
Related: OS#4963
2021-03-03 08:43:38 +01:00
Vadim Yanitskiy 6d5e0c9272 Remove Python shebang from files where it's not needed
Change-Id: I1d08544c37f50416acf8dc30139c572c029790d0
2021-03-01 17:33:46 +01:00
Vadim Yanitskiy dfe3dbb117 pySim-read.py: decode contents of EF.AD (Administrative data)
Change-Id: I938667bdf99d238eefac205d6dd70db1d714d842
2020-09-20 06:58:36 +00:00
Sebastian Viviani 0dc8f69217 ts_31_102.py: add EF_ADF_map
the EF files in the USIM ADF are different to the ones int the GSM dir
so added the dictionary to avoid conflicts and hardcoded values

the 'DIR' one was added in ts_51_011.py, not sure if it should be there
as it's not in that standard, but did it for simplicity

Change-Id: I458380bf46b2986662ecdede2551c22cd9be92ba
2020-06-03 06:30:37 +00:00
Supreeth Herle bf5d602588 Define mapping between SIM Service Number and its description
This commit introduces a lookup table which maps SIM Service Number to its description.
The mapping is defined in 3GPP TS 51.011 version 4.15.0 Release 4, 10.3.7 EF.SST (SIM Service Table)

Change-Id: I4a416bd8bff563ae08b1b3c053d2047da91667b4
2020-04-01 11:06:59 +02:00
Alexander Chemeris 067f69cade ts_51_011: A file with MF/DF/EF constants from TS 51 011
pySim has been using magic numbers to access various files which makes it hard
to read, maintain and extend. With this file in place we can start replacing all
those magic numbers with human readable names lile EF['IMSI'] instead of
['3F00', '7F20', '6F07'].

Change-Id: I38f5d36d16b41b5d516a6a3e2ec1d09637883932
2018-01-10 14:04:08 +09:00