aboutsummaryrefslogtreecommitdiffstats
path: root/pySim
AgeCommit message (Collapse)AuthorFilesLines
2022-02-13transport/bt_rsap.py: fix unknown variable in reset_card()fixeria/btsapVadim Yanitskiy1-1/+1
Change-Id: I50f0f8d9ad30994c4d9693157dfa1a0c52753178
2022-02-13transport/bt_rsap.py: properly implement get_atr() methodVadim Yanitskiy1-2/+2
Change-Id: Ib40f59e3dd026aaeca8c51f7d0de3db78d12fb3e
2022-02-13transport: add Bluetooth (SIM Access Profile) based transportGabriel K. Gegenhuber2-0/+562
Change-Id: I2e8b202ac5cddf7c8533115d53dd0d64da6ca9b9
2022-02-12tlv: Convert CamelCase class name to snake_case in jsonHarald Welte1-1/+5
Our hand-written JSON so far is using snake_case identifiers, while the JSON generated by the pySim.tlv classes use the class names as keys, which LooksQuiteDifferent. So let's auto-convert the CamelCase into something that reflects our existing notion. Change-Id: Id55929ef03dc48cb668e6ba7e99b6b291680a42f
2022-02-11tlv: Function for flattening the list-of-dict output of TLV decoderHarald Welte1-0/+27
Before: { "FcpTemplate": [ { "FileDescriptor": { "shareable": true, "file_type": "df", "structure": "no_info_given" } }, { "FileIdentifier": "3f00" }, { "ProprietaryInformation": [ { "UiccCharacteristics": "71" }, { "AvailableMemory": 123052 } ] }, { "LifeCycleStatusInteger": "operational_activated" }, { "SecurityAttribReferenced": { "ef_arr_file_id": "2f06", "ef_arr_record_nr": 2 } }, { "PinStatusTemplate_DO": [ { "PS_DO": "40" }, { "KeyReference": 1 }, { "KeyReference": 129 } ] }, { "TotalFileSize": 187809 } ] } After: { "FcpTemplate": { "FileDescriptor": { "shareable": true, "file_type": "df", "structure": "no_info_given" }, "FileIdentifier": "3f00", "ProprietaryInformation": { "UiccCharacteristics": "71", "AvailableMemory": 123052 }, "LifeCycleStatusInteger": "operational_activated", "SecurityAttribReferenced": { "ef_arr_file_id": "2f06", "ef_arr_record_nr": 2 }, "PinStatusTemplate_DO": { "PS_DO": "40", "KeyReference": 129 }, "TotalFileSize": 187809 } } Change-Id: Ia5ad8f1d3b0d47ebdb1856b0feaba120bad3eef9
2022-02-11utils: Fix missing Optional[] in type annotationsHarald Welte1-3/+3
Thanks to Vadim for pointing this out Change-Id: I6e7d3725f28410d66580e88f2271d2b240d1f98e
2022-02-11cosmetic: Switch to consistent four-spaces indent; run autopep8Harald Welte26-5486/+6346
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-11ts_102_221: Implement proper parsing of EF.DIRHarald Welte2-10/+78
EF.DIR can not only contain the AID + Label of TS 102 221, but can also contain any of the DOs specified in ISO7816-4. Let's imoplement this based on the modern pySim.tlv parser Change-Id: I875eb49e1f0370428c2eae69af84f5483bd5b1fc Closes: OS#5410
2022-02-10filesystem.py: Accept both a class or an instance as TLV._tlv memberHarald Welte1-12/+12
As we've seen in recent patches, this has been a source of bugs, so let's be tolerant and deal with both. Change-Id: I0a5ec2a860104ffe4524c647105a42505ac394d6
2022-02-10ts_31_102: TLV._tlv must point to the class, not an instanceHarald Welte1-2/+2
In Change-Id I6d7c1bf49a8eaf3d8e50fb12888bf3d5b46b6c55 we fixed the filesystem code to assume the self._tlv memper is a reference to a class, and not an instance (as this is what the majority of the code did). However, it seems thre wer two instances where we actually had _tlv reference an instance. Change that to class so it's the same all over the code base. Change-Id: Ie4878ad6a92feafe47e375c4f5f3f198921e1e95
2022-02-10filesystem: Fix TLV decode/encodeHarald Welte1-24/+36
We cannot call a method of a class without instantiating it Change-Id: I6d7c1bf49a8eaf3d8e50fb12888bf3d5b46b6c55
2022-02-10Better decode of EF.UST, EF.EST and EF.ISTHarald Welte2-39/+46
So far, we only returned an array of service numbers like [ 2, 4, 5, 9 ] which is not very friendly to the human reader. In EF.SST we already had more verbose decoding including a description of each service. Let's add the same principle to EF.UST, EST and IST The same output above now looks like this: { "1": { "description": "Local Phone Book", "activated": false }, "2": { "description": "Fixed Dialling Numbers (FDN)", "activated": true }, "3": { "description": "Extension 2", "activated": false }, "4": { "description": "Service Dialling Numbers (SDN)", "activated": true }, "5": { "description": "Extension3", "activated": true }, "6": { "description": "Barred Dialling Numbers (BDN)", "activated": false }, "7": { "description": "Extension4", "activated": false }, "9": { "description": "Incoming Call Information (ICI and ICT)", "activated": true } } Change-Id: I34f64d1043698dc385619b2fdda23cb541675f76
2022-02-10utils.py: Fix some tuple type annotationsHarald Welte1-2/+2
Change-Id: I869b0268383f6babd9b51d0ddfce448a1d2dda1e
2022-02-10utils.py: type annotations for DataObject related methodsHarald Welte1-29/+31
Change-Id: I291a429e9fe9f1a3fd95dcba3020b0e982154c97
2022-02-10ts_102_221: Handle nested security condition data objectsHarald Welte1-1/+28
ISO 7816-4 Section 5.4.3.2 "Expanded Format" permits for nesting of security conditions using boolean operators OR, AND, NOT. Let's implement decoding and encoding of these. An example decoded looks like: pySIM-shell (MF/EF.ARR)> read_record_decoded 1 [ [ { "access_mode": [ "activate_file_or_record", "deactivate_file_or_record", "update_erase" ] }, { "or": [ { "control_reference_template": "ADM1" }, { "control_reference_template": "ADM2" } ] } ], [ { "access_mode": [ "read_search_compare" ] }, { "always": null } ] ] Prior to this patch, pySim would raise "ValueError: Unknown Tag 0xa0 in bytearray" Change-Id: Icb09cf3a90303a86fc77406b8b0806b5c926f1be Closes: OS#5411
2022-02-10ts_51_011: Fix type annotation for Tuple[int, int]Harald Welte1-1/+2
Thanks to Vadim for pointing this out. Change-Id: I7ee1309331902bafab3c9fc6bc33ca713f8c7832
2022-02-09ts_51_011: Fix typo in EF_MMSUPHarald Welte1-1/+1
The TLV decoder class must be in self._tlv, not self.tlv Change-Id: Ide6f6c823d5a16e375c324ba9bfa92e02c3b3c89
2022-02-09ts_31_102: EF.PNN encoding is identical to that of DF.GSMHarald Welte1-2/+2
so let's use the DF.GSM/EF.PNN decoder Change-Id: If2ce52fccfca3d8bb2c9801b9812912922600377
2022-02-09avoid pylint E0611: No name 'strxor' in module 'Crypto.Util.strxor'Harald Welte1-0/+1
At least on Debian 10 and unstable, I'm getting this error for pylint: ************* Module pySim.utils pySim/utils.py:570:1: E0611: No name 'strxor' in module 'Crypto.Util.strxor' (no-name-in-module) despite it clearly existing: >>> import Crypto.Util.strxor >>> Crypto.Util.strxor.strxor <built-in function strxor> So let's suppress the related pylint error. Change-Id: Iea89e758782a569be953d19892028f083a92c2f1
2022-01-31implement shell command to update PLMN in IMSIBjoern Riemer1-0/+24
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-25transport/serial: fix for Python 3Steve Markgraf1-3/+3
Change-Id: I21e5a7ad4f623ed30681dce1ff819679b8714c5b
2022-01-22ts_102_221: decode/encode EF.PLHarald Welte1-0/+11
pySIM-shell (MF/EF.PL)> read_binary_decoded [ "en", null, null, null, null ] Change-Id: I4e879ef34acee461adb8137a6315d064370b1b10
2022-01-22pySim-shell: alphabetically sort name of files in 'dir' commandHarald Welte1-1/+3
Change-Id: Id136909884d3c0eaa2416c6c488a6c4b7ed48119
2022-01-22cosmetic: Use EF.FDN instead of EF_FDN in ts_51_011.pyHarald Welte1-1/+1
All the files have '.' as separator in their names so far, let's avoid any inconsistencies Change-Id: Icabb892408a40ea37c7ebeb7db545b383aa01d99
2022-01-22ts_31_102: Add support for EF.ECC (emergency call codes)Harald Welte1-0/+26
decoded output will look like this: [ { "call_code": "911", "service_category": { "police": false, "ambulance": false, "fire_brigade": false, "marine_guard": false, "mountain_rescue": false, "manual_ecall": false, "automatic_ecall": false }, "alpha_id": "911" }, { "call_code": "112", "service_category": { "police": false, "ambulance": false, "fire_brigade": false, "marine_guard": false, "mountain_rescue": false, "manual_ecall": false, "automatic_ecall": false }, "alpha_id": "112" }, null, null, null ] Change-Id: If8b4972af4f5be1707446d335cfc6e729c973abb
2022-01-22add missing bit definition for NG-RAN in xAcTBjoern Riemer1-0/+1
when encoding the AcT value bit 11 is correctly set when NG-RAN is present in the string representation, however the decoding of bit 11 was missing. Adds tests for the decoder as well. Change-Id: I910df28c4c59ec94cce9603377786325f6d8c1a3
2022-01-20catch and ignore SwMatchError on probing for AID'sBjoern Riemer1-5/+7
When probing applications on a card by running select_adf_by_aid() SwMatchError exceptions indicating the non exsistance of that application on the card should be ignored. Change-Id: I3aa7deaf46bdf352a201c3089b3714405a06f281
2022-01-04filesystem: use correct AID for applications found by probingPhilipp Maier1-1/+1
When printing applications found by probing for a specific AID, then the wrong variable is used to print the AID. Change-Id: I3d5ec28e46fe00c0d793a1d9ef0a0e0900649a4d
2021-12-01filesystem: actively probe applicationsPhilipp Maier1-3/+12
A profile can cover lots of different applications. Those applications may not exist on all card models. To exclude applications that are not installed on the particular card EF.DIR is evaluated. However, there may be applications that are not registered in EF.DIR but supported by the profile. To cover those as well, lets try to select the applications we do not see in EF.DIR. If selecting works we know that the application exists on the card and we can include them in the RuntimeState. Change-Id: I3fa77a68664fe50d690a18adfb1ae1a88a189827
2021-11-25fix: Decoder may raise KeyErrorLennart Rosam1-1/+1
This fixes an issue where a KeyError may be raised when 'A5' is not present in `fcp` Change-Id: I5bb6131bd76c7bae2a70034c429cae2b380d164f
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-19profile: decode_select_response use object instead 'Any'Philipp Maier4-6/+5
the return type of decode_select_response is 'Any', lets be more specific and use 'object' Change-Id: Ic5c7ace234bc94ab1381d87e091369ade8011cab
2021-11-19profile: decode_select_response can be a static methodPhilipp Maier6-29/+27
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-19commands: use send_apdu_checksw() in method read_recordPhilipp Maier1-1/+1
At the moment the non checking send_apdu() method is used when records are read. Lets use read_record_checksw so that we get an exception in case there is a problem to read the specified record. Change-Id: I9fc411e1b12e8d9fd89b9964209808c0706011bd
2021-11-19ts_51_011: move _decode_select_response into profile classPhilipp Maier1-44/+38
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-19pySim-shell: add method to match card profile to cardPhilipp Maier5-62/+193
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-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-11-11ARA-M related command supportHarald Welte1-0/+364
This introduces support for talking to the ARA-M application on a card, as specified in the GlobalPlatform "Secure Element Access Control" specification v1.1. Change-Id: Ia9107a4629c3d68320f32bbd4dd26e1f430717da
2021-11-11ts_51_011: add status word definitionPhilipp Maier1-1/+38
There is no status word definition given in the SIM profile. Lets add one to be complete Change-Id: I01f2643a93e4a9b2ce2f95134aa5d773179d9b1c
2021-11-11filesystem: CardProfile: initialize empty sw table as empty dictPhilipp Maier1-1/+1
The table that holds the status word descriptions is initialized as an empty list '[]'. This is not correct since the interpret_sw method processes this data as dictionary, so lets initialize the sw member with an empty dict '{}' when not status word description is given. Change-Id: I3cae83f0f6ab274546991ecd14425f094b2816b2 Related: OS#5274
2021-11-10filesystem: define class byte and select control bytes in profilePhilipp Maier3-3/+11
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-10filesystem: make sure the card is in a defined statePhilipp Maier1-0/+4
When the runtime state is created there is already some interaction with the card. Lets make sure that the card is in a defined state when we leave the constructor of the RuntimeState. Change-Id: I986204964903069bcce781afdbf3c5d26682b749 Related: OS#5274
2021-11-10filesystem: do not read AIDs when no apps are definedPhilipp Maier1-0/+7
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-10filesystem: fix decode_select_responsePhilipp Maier4-18/+60
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-05commands: return none, when offset exceeds file lengthPhilipp Maier1-0/+3
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-05commands: complete documentation stringsPhilipp Maier1-15/+107
Some of the methods lack an explaination of the arguments. Lets add that to be complete Change-Id: Icda245e2fd5ef4556c7736d73574dfbb48168973
2021-11-05utils: cosmetic: remove stray commentPhilipp Maier1-1/+0
The comment is already covered by the help string, lets remove it. Change-Id: Ide2080ddb898441b6af70e32511b33ced23d0023
2021-11-05commands: do not check SW manually, use send_apdu_checksw()Philipp Maier2-16/+30
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-05Python is not C: get rid of unnecessary semicolonsVadim Yanitskiy1-2/+2
See https://www.python.org/dev/peps/pep-0008/. Change-Id: I9de3bcd324b0a1b98af761678996eaae85f7f790 Related: OS#5292