aboutsummaryrefslogtreecommitdiffstats
path: root/pySim/cards.py
AgeCommit message (Collapse)AuthorFilesLines
2024-02-05pylint: cards.pyHarald Welte1-10/+6
pySim/cards.py:30:0: W0401: Wildcard import pySim.utils (wildcard-import) pySim/cards.py:41:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/cards.py:55:4: R1711: Useless return at end of function or method (useless-return) pySim/cards.py:78:8: R1725: Consider using Python 3 style super() without arguments (super-with-arguments) pySim/cards.py:91:8: R1725: Consider using Python 3 style super() without arguments (super-with-arguments) pySim/cards.py:159:12: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/cards.py:28:0: C0411: standard import "import abc" should be placed before "from pySim.ts_102_221 import EF_DIR" (wrong-import-order) pySim/cards.py:25:0: W0611: Unused Dict imported from typing (unused-import) pySim/cards.py:28:0: W0611: Unused import abc (unused-import) Change-Id: I708da28caffb417ed2f8413f9611526b18b29cd4
2023-10-24prevent SimCardCommands.select_adf_by_aid bypassing lchanHarald Welte1-3/+5
Now that pySim-shell is aware of logical channels and issues almost all of its APDUs on the currently selected channel, we must also make sure that ADF selection by AID (implemented by the CardBase class) issues the SELECT on the respective logical channel. Before this patch, SELECT ADF by AID would always be issued on the primary logical channel (0), irrespective of the currently active RuntimeLchan. Change-Id: Idf05c297e6a2e24ca539408b8912e348c0782bb4 Related: OS#6230
2023-10-21cards.py: Fix type annotationHarald Welte1-6/+5
The CardBaes 'scc' member refers to a SimCardCommands instance, not to a LinkBase. Change-Id: If4c0dfbd8c9a03d1a0bc4129bb3c5d5fa492d4cb
2023-08-21cards: get rid of method read_iccidPhilipp Maier1-9/+1
The method read_iccid in class CardBase should be put back to legacy/cards.py. The reason for this is that it falls in the same category like read_imsi, read_ki, etc. We should not use those old methods in future programs since we have a more modern infrastructure (lchan) now. Also pySim-shell.py is the only caller of this method now. It is not used in any other place. Related: RT#67094 Change-Id: Ied3ae6fd107992abcc1b5ea3edb0eb4bdcd2f892
2023-07-20cards: fix swapped PIN mapping numberPhilipp Maier1-1/+1
The constant for _adm_chv_num is swapped. It should be 0x0A, rather than 0xA0 Change-Id: I5680d2deee855ef316a98058e8c8ff8cf4edbbb2
2023-07-12cards: all UICC should use sel_ctrl="0400" and SIM "0000"Harald Welte1-0/+2
Hence move this from the derived classes into the respective base classes SimCardBase and UiccCardBase Change-Id: Iad197c2b560c5ea05c54a122144361de5742aafd
2023-07-12cards: cosmetic rename, argument name should be scc, not sscHarald Welte1-2/+2
ssc = SimCardCommands Change-Id: I9d690a0a5b9b49ea342728a29b7d4ed10ac31e4e
2023-07-12cards: All derived of SimCardBase use CLA=A0; all UiccCardBase use CLA=00Harald Welte1-1/+6
Change-Id: Id61b549f68410631529349ee62b08a102f609405
2023-07-12pySim/cards: Add type annotationsHarald Welte1-16/+18
Change-Id: Id5752a64b59097584301c860ebf74d858ed3d240
2023-07-12split pySim/legacy/{cards,utils} from pySim/{cards,utils}Harald Welte1-1665/+30
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-12pySim/cards: Split legacy classes away from core SIM + UICCHarald Welte1-68/+81
This introduces an internal split between * the code that is shared between pySim-shell and legacy tools, which is now in the new class hierarchy {Card,SimCard,UiccCard}Base * the code that is only used by legacy tools, which is using the old class names inherited from the *Base above All users still go through the legacy {Sim,Usim,Isim}Card classes, they will be adjusted in subsequent patches. Change-Id: Id36140675def5fc44eedce81fc7b09e0adc527e1
2023-06-07Add FPLMN read and programMatan Perelman1-0/+18
Change-Id: I9ce8c1af691c28ea9ed69e7b5f03f0c02d1f029b
2023-05-27cards: Add support for Gialer SIM cardsMatan Perelman1-1/+64
Change-Id: Icd2021aec630ac018f66ab565e03112047389e17
2023-05-25cards.py: support ATR-based detection of sysmoISIM-SJA5Harald Welte1-1/+28
The cards are 99% software-compatible to the SJA2, so let's just derive the SJA5 class from the SJA2 Change-Id: I706631baaf447c49904277886bc9a3f6ba3f5532
2023-02-10cards: sysmo-isim-sja2: make sure an ADF is present in EF.DIR before ↵Philipp Maier1-7/+18
selecting it sysmo-isim-sja2 may come in different configurations, so some may intentionally lack ADF.USIM or ADF.ISIM. Since select_adf_by_aid() may raise an exception when selecting a non existent file we should make sure that the ADF we intend to select is indeed present. A reliable way to do this is to check if the application is registered in EF.DIR. Change-Id: Icf6f6b36f246398af408ec432d493fe3f22963dd
2023-01-19cards: also program EF.AD under ADF.USIMPhilipp Maier1-3/+15
DF.GSM and ADF.USIM have an EF.AD with nearly the same contents. Usually there is one file physically present and the other is just a link. Apparantly this is not always the case for sysmo-ismi-sja2 cards, so lets program EF.AD in both locations. Change-Id: Ic9dd4acc8d9a72acbb7376ddf3e2128125d4a8f5 Related: OS#5830
2023-01-03cards: fix typoPhilipp Maier1-2/+2
Change-Id: I81a6074776bdf67b7bea359fe7a24f906936f46d
2022-12-20cards: check length of mnc more restrictivelyPhilipp Maier1-4/+5
Since we now ensure that mnc always has a valid length lets make the check in cards.py more strict. Related: OS#5830 Change-Id: Iee8f25416e0cc3be96dff025affb1dc11d919fcd
2022-07-07Py2 -> Py3: do not inherit classes from objectVadim Yanitskiy1-1/+1
https://stackoverflow.com/questions/4015417/why-do-python-classes-inherit-object/45062077 Change-Id: I15003ba591510d68f3235f71526ad5d8a456088e
2022-06-21cards: populate ADM1 key reference memberPhilipp Maier1-0/+3
In class SimCard, we specify the key reference for ADM1 as 0x04. in the UsimCard class, which inherits from SimCard nothing is specified, even though ETSI TS 102 221 specifies 0x0A as key reference. Lets set the member in UsimCard accordingly to be closer to the spec. Note: For the moment this is a cosmetic fix, it does not change the behaviour since all card classes derived from UsimCard set the key reference properly. Change-Id: I96af395b1832f4462a6043cca3bb3812fddac612
2022-04-21SimCard.reset(): fix SyntaxWarning: 'is' with a literalVadim Yanitskiy1-1/+1
Change-Id: I5860179acd1cb330e91dbe5b57cd60cd520f2d9d
2022-02-15Split EF.UST handling from EF.IST and EF.SSTHarald Welte1-0/+17
The existing code had the following serious problems: * when trying to update EF.SST or EF.IST, it would write to EF.UST ! * shell commands were called ust_* even for the EST/IST files Let's introduce the proper separation between what is shared and what is file-specific. Change-Id: Ie55669ca37a4762fac9f71b1db528ca67056e8dd
2022-02-11cosmetic: Switch to consistent four-spaces indent; run autopep8Harald Welte1-1446/+1484
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
2021-11-23cards: Make select_adf_by_aid() use prefix AID selectionPhilipp Maier1-0/+3
There is no need for us to expand a partial AID to the full AID before selecting that ADF. The UICC specifications permit AID selection by prefix only. So we could pass the prefix to the card, and the card would do the prefix matching. In order to avoid problems with cards that fail to do the prefix matching themselves we will still do the AID completion, but in case we cannot complete the AID (AID not listed in EF.DIR), we will try with the AID prefix anyway. From the API user point of view, this allows us to select applications not listed in EF.DIR Change-Id: I0747b4e46ca7e30bd96d76053765080367ac1317
2021-11-22cards: make _get_aid case insensitivePhilipp Maier1-0/+1
There is no need to be case sensitive when the xSIM application name is given as AID. Change-Id: I9944d9180bf1ba35f44f0be2b05bdb725b5b8da9
2021-11-18cards: select_adf_by_aid: split off aid completionPhilipp Maier1-13/+26
The function select_adf_by_aid first searches for the complete AID in the set of AIDs that were read from EF.DIR. Lets put this task into a separate helper method Change-Id: I88447d47bc96d0d4ff5cea694b46e854232cdf86
2021-10-29filesystem: fix reset mechanismPhilipp Maier1-1/+5
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-29cards: add method to modify APDU parameters (cla, sel_ctrl)Philipp Maier1-0/+9
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-29cards: use python style commends to describe methodsPhilipp Maier1-15/+7
Change-Id: Iae862d8f0a191c7015a94f9516ef5804265f7a82
2021-10-13cards: remove "auto_once" from possible ctype optionsPhilipp Maier1-4/+1
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-08cards: remove unused function card_autodetect()Philipp Maier1-8/+0
The function card_autodetect() is not used, lets remove it. Change-Id: Ic188e1fffb4a40e89ad276941d20f94cf35e1538
2021-10-05cards: FairwavesSIM: force SIM APDUs during programmingPhilipp Maier1-1/+18
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-06-30cards: rename class "Card" to "SimCard"Philipp Maier1-8/+8
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-05-23utils: split string formatting from dec_addr_tlvPhilipp Maier1-2/+30
The function dec_addr_tlv() takes an encoded FQDN or IPv4 address and fromats it into a human readable string that contains the human readable form and the encoded hex form. Unfortunately this limits the usecase of dec_addr_tlv. Lets split the string generation into a separate function so that we can use dec_addr_tlv universally Change-Id: Id017b0786089adac4d6c5be688742eaa9699e529 Related: OS#4963
2021-05-21cards: populate name property in Card, UsimCard and IsimCardPhilipp Maier1-0/+8
Even though Card, UsimCard and IsimCard are abstract classes which are normally only used to inherit from mit may make sense to pre-populate the name property with some meaningful value. Change-Id: Id643e1f83718aea073e7200aecbf2db2def8652f
2021-05-10Add codecs for EF_SPN and GSM strings via constructRobert Falkenberg1-10/+17
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-02[pylint] Fix float vs integer division in cards.pyVadim Yanitskiy1-2/+2
Change-Id: Ie4ba72b725a56ba9cfe98cc7bd17dd3653194f36
2021-05-02[pylint] Mark abstract MagicSimBase class as suchVadim Yanitskiy1-3/+4
Change-Id: I315c646d94a1d3282917f5abb0c93efb918b53d7
2021-05-02[pylint] Fix calling non-existing iteritems() of dictVadim Yanitskiy1-2/+2
This method has been removed [1] in Python 3.0: pySim/cards.py:581:14: E1101: Instance of 'dict' has no 'iteritems' member (no-member) pySim/cards.py:591:24: E1101: Instance of 'dict' has no 'iteritems' member (no-member) [1] https://wiki.python.org/moin/Python3.0#Built-In_Changes Change-Id: Iba7ad9ed2a9b197ecedaaed1c6744fe1c721515a
2021-05-02[pylint] Declare some fields in _MagicSimBase classVadim Yanitskiy1-0/+5
Fixes the following pylint's warnings: pySim/cards.py:494:18: E1101: Class '_MagicSimBase' has no '_files' member (no-member) pySim/cards.py:509:6: E1101: Instance of '_MagicSimBase' has no '_files' member (no-member) pySim/cards.py:529:26: E1101: Instance of '_MagicSimBase' has no '_files' member (no-member) pySim/cards.py:537:5: E1101: Instance of '_MagicSimBase' has no '_ki_file' member (no-member) pySim/cards.py:547:5: E1101: Instance of '_MagicSimBase' has no '_ki_file' member (no-member) pySim/cards.py:548:8: E1101: Instance of '_MagicSimBase' has no '_ki_file' member (no-member) pySim/cards.py:559:26: E1101: Instance of '_MagicSimBase' has no '_files' member (no-member) pySim/cards.py:560:11: E1101: Instance of '_MagicSimBase' has no '_files' member (no-member) pySim/cards.py:576:14: E1101: Instance of '_MagicSimBase' has no '_files' member (no-member) Change-Id: I4db9d21258d6e04140962134c540e36631466322
2021-04-23utils: fix encoding of EF.MSISDNPhilipp Maier1-2/+2
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-13Use construct for EF_AD in pySim-{shell, prog, read}.py, cards.pyRobert Falkenberg1-10/+8
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-11fix various typos all over the codeHarald Welte1-3/+3
Change-Id: Ic8392a951bf94f67b51e35bed95d0e856f7a9250
2021-04-11WIP: Add option to set UE operation mode in EF_AD (Administrative Data)Robert Falkenberg1-20/+62
Use ``--opmode=OPMODE`` in cmdline mode or column ``OPMODE`` in csv mode to specify OPMODE as listed below. Details: The ``EF_AD`` field contains administrative data (AD). It consists of four bytes ``B1``, ``B2``, ``B3``, ``B4``, and optionally further bytes for future use. Previous implementation only sets the MNC field appropriately (located in `B4`) and sets all other bits/bytes to 0. However, `B1` also defines the *UE operation mode* (see below). For type approval operations, such as testing with a test uSIM, this value could be set to `0x80` rather than `0x00`(= normal operation). This may unlock some UE capabilities that are restricted in normal operation mode. Excerpt from [ETSI TS 131 102, 4.2.18](https://www.etsi.org/deliver/etsi_ts/131100_131199/131102/04.15.00_60/ts_131102v041500p.pdf): ``` B1 - UE operation mode: Coding: Initial value - '00' normal operation. - '80' type approval operations. - '01' normal operation + specific facilities. - '81' type approval operations + specific facilities. - '02' maintenance (off line). - '04' cell test operation. B2 - Additional information: Coding: Reserved for future use B3 - Additional information: Coding: - B3.b1: OFM setting (Ciphering Indicator) - B3.others: Reserved for future use B4 - Length of MNC in the IMSI: Coding: - B4.b4..B4.b1: length: '0010' (= 2) or '0011' (=3) - B4.others: Reserved for future use ``` **Legend:** Byte X, bit Y: BX.bY Further reading: https://nickvsnetworking.com/usim-basics/ Change-Id: Ie9040c6b127c268878a0845ed73d0918ec6bbb08
2021-04-07Use zero padding for EF['ACC'] fieldRobert Falkenberg1-1/+1
The ``EF_ACC`` field defines the access control class (ACC) for a subscriber. Without this patch, the implementation adds padding 1 towards the most significant bits if the input is shorter than 2 bytes. However, it should be padded with 0, otherwise additional ACCs are allocated to the subscriber. (Probably only a single bit shall be set to 1) Excerpt from [ETSI TS 131 102, 4.2.15](https://www.etsi.org/deliver/etsi_ts/131100_131199/131102/04.15.00_60/ts_131102v041500p.pdf): ``` EF_ACC: Two bytes: B1, B2 B1.b8...B1.b4: high priority users (class 15...11) B1.b3: always 0 B1.b2...B1.b2 and B2.b7...B2.b0: normal priority users (class 9...0) - to be evenly distributed across subscribers ``` **Legend:** Byte X, bit Y: BX.bY Change-Id: I1b8dc01a6c48adad1ed8158de59b12519ed688e9
2021-04-07SysmoISIM-SJA2: Add option to set Service Provider Name (SPN)Robert Falkenberg1-0/+5
Same implementation as for sysmoUSIM-SJS1 Change-Id: I3a9dd2fe85126584758ea4cfa127f9cd14ab0c7d
2021-04-02fileystem: fix ADF selectionPhilipp Maier1-17/+19
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-03-26pySim-shell: add ADF.ISIM / ADF.USIM dynamicallyPhilipp Maier1-0/+3
currently ADF.ISIM and ADF.USIM are always added regardless if there is a matching application on the card or not. Lets check what applications are actually installed and add ADF.ISIM and ADF.USIM dynamically. Change-Id: I42ee23375f98e6322708c1c4db6d65e1425feecd Related: OS#4963
2021-03-23cards: remove unnecessary execptions.Philipp Maier1-10/+2
The _scc.veryif_adm() method already does status word checking internally and also raises an execption should the authentication be unsuccessful, so we do not have to put an additional status word check + execition when we use the method from cards. Change-Id: I785d27e4d49a9cda1a771b56ce5ac9c1f1d1e79a Related: OS#4963
2021-03-03start using python3 bytearray for our b2h/h2b typesHarald Welte1-1/+1
The code was written long ago, when the python3 bytearray type probably didn't exist yet, or was at least not known. Let's stop using string types with binary bytes inside, and instead standardize on two types: * bytearray for binary data * string for hexadecimal nibbles representing that binary data Change-Id: I8aca84b6280f9702b0e2aba2c9759b4f312ab6a9