Commit Graph

79 Commits

Author SHA1 Message Date
Philipp Maier af4e5bb18c transport: do not catch exceptions in init_reader
We currently catch any exceptions that may occur when the card reader is
initialized. Then we print the exception string or the exception type
when no string is available. However, a failure during the reader
initialization is usually a severe problem, so a traceback would provde
a lot of helpful information to debug the issue. So lets not catch any
exceptions at this level so that we get the full backtrace.

Related: OS#6210
Change-Id: I4c4807576fe63cf71a7d33b243a3f8fea0b7ff23
2023-10-16 14:36:02 +02:00
Harald Welte b314b9be34 ts_31_102, ts_31_103: Move legacy-only code to pySim.legacy
Change-Id: Ifebfbbc00ef0d01cafd6f058a32d243d3696e97e
2023-07-12 22:05:14 +02:00
Harald Welte 57ad38e661 create pySim.legacy.ts_51_011.py and move legacy code there
Those old flat dicts indicating FID to string-name mapping have long
been obsoleted by the pySim.filsystem based classes.

Change-Id: I20ceea3fdb02ee70d8c8889c078b2e5a0f17c83b
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 f422eb1886 Add ".py" suffix to sphinx-argparse generated docs
This is important to produce the right command syntax when generating
command line reference in the user manual.  However, we shouldn't add
this kludge to the individual programs, but only to the documentation
using the :prog: syntax.

Change-Id: I2ec7ab00c63d5d386f187e54755c71ffc2dce429
2023-06-09 11:50:18 +02:00
Matan Perelman 777ee9e54d Add FPLMN read and program
Change-Id: I9ce8c1af691c28ea9ed69e7b5f03f0c02d1f029b
2023-06-07 10:00:07 +00: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
Philipp Maier abc2336571 pySim-read: put try/catch block around select_adf_by_aid()
Selecting an application may fail, especially when the application does
not exist on the card.

Change-Id: Ia904a74d672cf9551fb4ee062dd606b350b64cef
2021-11-23 18:35:34 +01:00
Philipp Maier 055b80aa5c pySim-read: do not select ADF.ISIM again
Before reading EF.IST ADF.ISIM is selected again even though it was
selected before. Lets skip this step since it is unnecessary.

Change-Id: I75be18e3476cb1d093bc99775eeddd0c08b81d78
2021-11-18 10:36:02 +01: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
Robert Falkenberg 5933c3b88d pySim-read: adjust meaning of HPLMN/OPLMN flags in EF_SPN
The updated wording better reflects the actual meaning
of a set or unset flag, especially as OPLMN is inverted.

Change-Id: I65c6f0e9bc1a12a4a74c4274eebb8e612296888f
2021-05-23 10:06:08 +00:00
Philipp Maier e2c59a8b91 pySim_prog: remove unused import for dec_addr_tlv
The function dec_addr_tlv is imported from utils, but not used

Change-Id: I2a962d544f288259f16c1dca92715953d1c24d82
Related: OS#4963
2021-05-21 15:26:52 +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 4210a7001d pySim-read: fix wrong comment
Change-Id: Idcbbc6e964f7932a10d55f7f28646f278c994129
2021-05-05 14:22:23 +02:00
Harald Welte 8fe1d202c7 pySim-read: Migrate over to use shared argparse from transport
Now that we have a shared argparse definition for all reader related
options in the transport module, use that.

Change-Id: I12ca1a484a5d6e84820d9761c9701f8a94381f66
2021-04-22 09:11:23 +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 6e0458dda6 Move init_reader() from utils.py to transport/__init__.py
This avoids a circular dependency when introducing type annotations.

Change-Id: I168597ac14497fb188a15cb632f32452128bc1c6
2021-04-04 10:53:36 +02:00
Philipp Maier cba6dbce9a fileystem: fix ADF selection
When the ADF is selected, then this is done by the AID. At the moment
only the first 7 bytes of the AID are used to select the ADF.
sysmo-isim-sja2 tolerates this, but sysmo-usim-sjs1 does not. The Cards
class already has methods to deal with this problem. The method
select_adf_by_aid takes an ADF name and completes the AID from an
internal list. This can be extended to support partial hexadecimal AIDs
as well.

Change-Id: If99b143ae5ff42a889c52e8023084692e709e1b1
Related: OS#4963
2021-04-02 16:21:14 +02:00
Philipp Maier c8caec2933 utils: catch exceptions during reader initalitation
Failed reader initializations happen frome time to time, mostly because
of messed up commandline arguments. This results in ugly crashes very
often. Lets control this a bit by catching the exception string and
print it.

Change-Id: I313017355da01bbef7c3d3f1899555aadb509319
2021-02-25 15:16:07 +01:00
herlesupreeth cebf8b198b pySim-read.py: Add support for reading USIM+ISIM data from third-party UICC
Change-Id: Id8b95630e90cb5833482da2690e423e7adefb95b
2021-01-21 06:13:03 +01:00
Supreeth Herle be3b64167a pySim-read.py: Enable reading of UICC IARI from ISIM
Change-Id: Iba222421f2fcc2b9b12605608bf640f7627904d0
2021-01-05 15:59:38 +01:00
Supreeth Herle 0c02d8a57b pySim-read.py: Enable reading of EF.IMPU from ISIM
IMS public user identity (IMPU)
As per TS1.103, this EF contains one or more records, with each record able
to hold a public SIP Identity (SIP URI) of the user. EF.IMPI consist of URI TLV data
object values see IETF RFC 3261. The URI shall be encoded to an octet string according
to UTF-8 encoding rules as specified in IETF RFC 3629

Reading of EF.IMPU is achieved by first selecting the ISIM application using its AID.
This is followed by selecting EF.IMPU with File ID - 6f04 in ADF.ISIM

Change-Id: Icf78a564aeaf4254658d3b018ff57dfc4b987e6f
2021-01-05 11:46:41 +01:00
Supreeth Herle 3f67f9c1d3 pySim-read.py: Enable reading of EF.IMPI from ISIM
IMS private user identity (IMPI)
As per TS1.103 version 14.2.0, this EF contains the private user identity of the user.
EF.IMPI consist of NAI TLV Data object.The NAI shall be encoded to an octet string
according to UTF-8 encoding rules as specified in IETF RFC 3629.

Reading of EF.IMPI is achieved by first selecting the ISIM application using its AID.
This is followed by selecting EF.IMPI with File ID - 6f02 in ADF.ISIM

Change-Id: I8d8e76e3f6b9ca7a0be262fee99cd5a397edbefa
2021-01-05 11:46:41 +01:00
Supreeth Herle 05b2807168 pySim-read.py: Enable reading of Home Network Domain Name from ISIM
As per TS1.103 version 14.2.0, this EF contains the home operator's network domain name.
The Home Network Domain Name, i.e. FQDN shall be encoded to an octet string
according to UTF-8 encoding rules as specified in IETF RFC 3629

Change-Id: Ia3c68c717d105e10d52a8e9d170480da2ad7d65a
2021-01-05 11:46:40 +01:00
Supreeth Herle 5ad9aec98f pySim-read.py: Enabled reading P-CSCF address from ISIM
As per 3GPP TS 31.103 version 14.2.0 Release 14, this EF can found under ADF.ISIM at File Id 6f09.

This EF contains one or more Proxy Call Session Control Function addresses.
The first record in the EF shall be considered to be of the highest priority
If ISIM service n°1 and/or service n°5 is available, this file shall be present.

Change-Id: I7a701212c84d3dc5d4c8ccbcf638c97ceda33654
2021-01-05 10:06:19 +01:00
Supreeth Herle 3b342c2f14 Re-purpose helper method to be used for parsing Address TLV Object in general
The Address TLV object is 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.

Address TLV Object format
Tag (1 Byte) - '80'
Length (1 Byte)
Address Type (1 Byte) - '00' (FQDN), '01' (IPv4), '02' (IPv6)
Address (Address Length Bytes)

Change-Id: Ifd8a240f6b5c7736e58a8151295c30ec5b32ed5f
2021-01-05 10:06:19 +01:00
Supreeth Herle 99d55552d5 pySim-read.py: Enable parsing of EF.ePDGSelection in USIM
As per TS 31.102, this EF can found under ADF.USIM at File Id 6ff4.
Also, if service n°106 and service n°107 are available, this file shall be present.

Change-Id: I98916e6f5c9791aff63c18a3b16bdfb8ae9b2d36
2020-10-21 08:32:55 +02:00
Daniel Willmann de07b95f84 Fix invocation python2 -> python3
Change-Id: Ic78da9c03e99f59d142c93394051bbc2751f0205
2020-10-19 10:32:34 +02:00
herlesupreeth 4a3580b4c1 Move reading of USIM service table to generic USIM class
Change-Id: I537547f3bd01a547310358f8a8fceddcb4c79f37
2020-10-03 07:37:06 +00:00
herlesupreeth f8232db327 Move reading of ePDG Id to generic USIM class
Change-Id: I716acb994430db3d4e56fea072f8dc2cebeaba84
2020-10-03 07:36:58 +00: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
Harald Welte ca6739458e Add support for ADF_USIM/EF_EHPLMN
If the EF.EHPLMN exists, it contains the "Equivalent Home PLMN List".
The odd part of that list is that it is not just a list of additional
PLMN identities, but if the first digits of the IMSI are *not* listed
in EF.EHPLMN, then the MCC/MNC of the IMSI prefix is suddently no
longer considered the home network, but the subscriber is roaming.

See TS 23.122: "If the HPLMN code derived from the IMSI is not present
in the EHPLMN list, then it shall be treated as a Visited PLMN for PLMN
selection purposes."

Change-Id: I22d96ab4a424ec5bc1fb02f5e80165c646a748d3
2020-08-28 08:32:55 +00:00
Supreeth Herle b1634db0b3 pySim-read.py: Enable parsing of Home ePDG Identifier in USIM
As per TS 31.102, version 13.4.0 Release 13, this EF can found under ADF.USIM at File Id 6ff3.
Also, if service n°106 and service n°107 are available, this file shall be present.

Change-Id: I68114d328d1af5682a5bc1fa7642882e80b5de4d
2020-06-23 13:54:20 +02: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
Vadim Yanitskiy 29ca8049d6 Implement Generic SIM Access interface as per 3GPP TS 27.007
According to 3GPP TS 27.007, sections 8.17 and 8.18, the modem
may *optionally* provide Generic and/or Restricted SIM Access
to the TE (Terminal Equipment) by means of the AT commands.
This basically means that a modem can act as a card reader.

Generic SIM Access allows the TE to send raw PDUs in the format
as described in 3GPP TS 51.011 directly to the SIM card, while
Restricted SIM Access is more limited, and thus is not really
interesting to us.

This change implements a new transport called ModemATCommandLink,
so using it a SIM card can be read and/or programmed without the
need to remove it from the modem's socket. A downside of this
approach is relatively slow I/O speed compared to PC/SC readers.

Tested with Quectel EC20:

  $ ./pySim-read.py --modem-dev /dev/ttyUSB2

Change-Id: I20bc00315e2c7c298f46283852865c1416047bc6
Signed-off-by: Vadim Yanitskiy <axilirator@gmail.com>
2020-06-02 21:51:07 +07:00
Philipp Maier ff84c23839 pySim-prog, pySim-read, do not echo reader id
pySim-prog and pySim-read currently echo back the pcsc reader id (or
baudrate/socket, depending on the interface used). This makes the output
unecessarly undeterministic, which becomes a problem when verifying the
putput in tests. Lets not echo those variable, user supplied parameters
back. Also lets move the code that does the initalization to utils, so
that it can be used from pySim-prog and from pySim-read (code dup).

Change-Id: I243cc332f075d007b1c111292effcc610e874eb3
Related: OS#4503
2020-05-12 18:11:38 +00:00
Supreeth Herle ee15c77185 Enable parsing of ISIM Service table (IST)
As per TS 31.103, This EF indicates which ISIM services are available.
If a service is not indicated as available in the ISIM, the ME shall not select this service.

Parsing of IST is achieved by first selecting the ISIM application using its AID.
This is followed by selecting EF.IST with File ID - 6f07 in ADF.ISIM

Change-Id: I3f0a7227360b72a707dc1bcc4cc9c8a4ec7ad2b2
2020-04-27 12:29:00 +02:00
Supreeth Herle 9641299f9a Enable parsing of USIM Service table (UST)
As per TS.31.102, This EF indicates which USIM services are available.
If a service is not indicated as available in the USIM, the ME shall not select this service.

Parsing of UST is achieved by first selecting the USIM application using its AID.
This is followed by selecting EF.UST with File ID - 6f38 in ADF.USIM

Change-Id: I54dbbd40bd3d22cee81f7c32e58cd946f8564257
2020-04-27 12:29:00 +02:00
Supreeth Herle d3b13d0c85 Use helper method to print available service in EF.SST
Change-Id: I375475e9f7210dae4e8da7258d6824dc2d54cf4c
2020-04-27 12:29:00 +02:00
Supreeth Herle 3e6f16d8f6 pySim-read.py: Add ability to read Cards with correct CLA, P1 and P2 bytes
Initially the Card is read assuming a UICC SIM, but in case its not, an
error 6e00 will be thrown indicating CLA not supported and Card has just Classic SIM application.

Ref: https://web.archive.org/web/20090630004017/http://cheef.ru/docs/HowTo/APDU.info

The above link provides the bytes to use for CLA, P1, P2 in APDU

Change-Id: Ifea328eff3a381d7b82118e22d2bc0ec5f8a87e4
2020-04-27 08:41:49 +00:00
Supreeth Herle f9f3e5e0c1 cards.py: Added method to select ADF by its full AID
If AID of the desired ADF is in the list of AIDs of the Card/Card subclass object
then ADF is selected or else None is returned

Change-Id: Ie5f29eec14f099add1d0978e3e7d4ed3c9130854
2020-04-16 07:55:15 +02:00
Supreeth Herle 3bf43639ce Populate AIDs present on the UICC
Change-Id: I4d0d8f5f1e8cb252be55a2995b730927cfa7004d
2020-04-16 07:55:13 +02:00
Supreeth Herle e26331ef72 Add ability to parse SIM Service Table (EF.SST)
As per TS.51.011, This EF indicates which services in the SIM are allocated, and whether, if allocated, the service is activated
.If a service is not indicated as available in the SIM, the ME shall not select this service.

Change-Id: Id28a35727adbaaa9df19b1adc621a0c51ad0e51b
2020-04-16 07:38:27 +02:00
Supreeth Herle 52ef675c31 Use the generic method read_binary of card class to read AD
Change-Id: Ie7f62913caed95f482445962954857bb69b58078
2020-04-01 11:06:25 +02:00
Supreeth Herle 6d66af653f Move parsing of MSISDN to generic Card class
Change-Id: I5b726bc0dc8c8e5eb42f209b1fe0f35a46ac91be
2020-04-01 11:05:26 +02:00
Supreeth Herle d1fb6fc359 Use the generic method read_binary of card class to read ACC
Change-Id: I92a02c74d64b3120055163548fc128ed6e0650a4
2020-04-01 11:04:43 +02:00
Supreeth Herle a850a47981 Move parsing of HPLMNAcT to generic Card class
Change-Id: I46c863c118dcbef89455c34289ed25e5a342f35b
2020-04-01 09:35:52 +02:00
Supreeth Herle 1757b263bf Move parsing of OPLMNwAcT to generic Card class
Change-Id: I8050bd103a7085b2631ddc4e567d15e05f9428f2
2020-04-01 09:35:52 +02:00
Supreeth Herle 140844052a Move parsing of PLMNwAcT to generic Card class
Change-Id: I14d7c2dc51fac6d5cf4a708a77ad23d252ba6094
2020-04-01 09:35:52 +02:00
Supreeth Herle 9efd8ef812 Use the generic method read_binary of card class to read PLMNsel
Change-Id: I0a683c479cd41ccc6a93c23434c73793cb5dc186
2020-04-01 09:34:57 +02:00