aboutsummaryrefslogtreecommitdiffstats
path: root/pySim/commands.py
AgeCommit message (Collapse)AuthorFilesLines
2024-02-15Dynamically determine maximum CMD data length depending on SCPHarald Welte1-4/+12
If we're using a Secure Channel Protocol, this will add overhead in terms of the C-MAC appended to the C-APDU. This means in turn that the useable length of the data field shrinks by a certain number of bytes. Let's make sure the SCP instances expose an 'overhead' property of how much overhead they add - and that other commands use this to determine the maximum command data field length. Change-Id: I0a081a23efe20c77557600e62b52ba90a401058d
2024-02-05pylint: commands.pyHarald Welte1-27/+25
pySim/commands.py:443:0: C0325: Unnecessary parens after 'if' keyword (superfluous-parens) pySim/commands.py:446:0: C0325: Unnecessary parens after 'elif' keyword (superfluous-parens) pySim/commands.py:669:0: C0325: Unnecessary parens after 'elif' keyword (superfluous-parens) pySim/commands.py:27:0: W0622: Redefining built-in 'BlockingIOError' (redefined-builtin) pySim/commands.py:27:0: W0401: Wildcard import construct (wildcard-import) pySim/commands.py:30:0: W0404: Reimport 'Hexstr' (imported line 29) (reimported) pySim/commands.py:42:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/commands.py:48:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/commands.py:98:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/commands.py:114:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/commands.py:131:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/commands.py:223:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/commands.py:234:8: R1705: Unnecessary "else" after "return", remove the "else" and de-indent the code inside it (no-else-return) pySim/commands.py:252:11: C0123: Use isinstance() rather than type() for a typecheck. (unidiomatic-typecheck) pySim/commands.py:271:11: C0123: Use isinstance() rather than type() for a typecheck. (unidiomatic-typecheck) pySim/commands.py:274:18: W0612: Unused variable 'sw' (unused-variable) pySim/commands.py:326:16: W0707: Consider explicitly re-raising using 'raise ValueError('%s, failed to read (offset %d)' % (str_sanitize(str(e)), offset)) from e' (raise-missing-from) pySim/commands.py:386:16: W0707: Consider explicitly re-raising using 'raise ValueError('%s, failed to write chunk (chunk_offset %d, chunk_len %d)' % (str_sanitize(str(e)), chunk_offset, chunk_len)) from e' (raise-missing-from) pySim/commands.py:443:12: R1720: Unnecessary "elif" after "raise", remove the leading "el" from "elif" (no-else-raise) pySim/commands.py:521:14: R1714: Consider merging these comparisons with 'in' by using 'sw in ('62f1', '62f2')'. Use a set instead if elements are hashable. (consider-using-in) pySim/commands.py:532:11: R1701: Consider merging these isinstance calls to isinstance(data, (bytearray, bytes)) (consider-merging-isinstance) pySim/commands.py:666:8: R1720: Unnecessary "elif" after "raise", remove the leading "el" from "elif" (no-else-raise) pySim/commands.py:762:12: R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return) pySim/commands.py:776:12: R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return) Change-Id: Idfcd6f799d5de9ecacd2c3d1e0d1f7d932f2b8db
2024-02-04Introduce GlobalPlatform SCP02 implementationHarald Welte1-2/+9
This implementation of GlobalPlatform SCP02 currently only supports C-MAC and C-ENC, but no R-MAC or R-ENC yet. The patch also introduces the notion of having a SCP instance associated with a SimCardCommands instance. It also adds the establish_scp0w and release_scp shell commands to all GlobalPlatform Security Domains. Change-Id: I56020382b9dfe8ba0f7c1c9f71eb1a9746bc5a27
2024-02-01commands.py: Wrap the transport send_apdu* methodsHarald Welte1-43/+111
Let's not have higher level code directly call the transports send_apdu* methods. We do this as a precursor to introducing secure channel support, where the secure channel driver would add MAC and/or encrypt APDUs before they are sent to the transport. Change-Id: I1b870140959aa8241cda2246e74576390123cb2d
2023-12-29commands: Ignore exceptions during READ while UPDATEHarald Welte1-7/+21
If we are reading a file to check if we can skip the write to conserve writes, don't treat exceptions as fatal. The file may well have the access mode in a way that permits us to UPDATE but not to READ. Simply fall-back to unconditional UPDATE in this case. Change-Id: I7bffdaa7596e63c8f0ab04a3cb3ebe12f137d3a8
2023-10-24pySim-shell: Create + use per-RuntimeLchan SimCardCommandsHarald Welte1-0/+1
This new approach will "fork" separate SimCardCommands instances for each RuntimeLchan. Higher-layer code should now always use the RuntimeLchan.scc rather than the RuntimeState.card._scc in order to make sure commands use the correct logical channel. Change-Id: I13e2e871f2afc2460d9fd1cd566de42267c7d389 Related: OS#6230
2023-10-24commands.py: Add support for multiple logical channels.Harald Welte1-9/+68
Historically we always only had one instance of SimCardCommands, but with this patch we can now have multiple instances, one for each lchan. The SimCardCommands class is aware of the logical channel it runs on and will patch the CLA byte accordingly. Change-Id: Ibe5650dedc0f7681acf82018a86f83377ba81d30 Related: OS#6230
2023-09-07filesystem: add attribute "leftpad" to class LinFixedEFPhilipp Maier1-3/+7
In some cases, the specs do not specify an absolute record length. Instead there may be only a minimum record length specified. The card vendor may then chose to use larger record length at will. This usually is no problem since the data is usually written from the left and the remaining bytes are padded at the end (right side) of the data. However in some rare cases (EF.MSISDN, see also 3GPP TS 51.011, section 10.5.5) the data must be written right-aligned towards the physical record length. This means that the data is padded from the left in this case. To fix this: Let's add a "leftpad" flag to LinFixedEF, which we set to true in those corner cases. The code that updates the record in commands.py must then check this flag and padd the data accordingly. Change-Id: I241d9fd656f9064a3ebb4e8e01a52b6b030f9923 Related: OS#5714
2023-09-07commands: make method verify_binary and verify_record privatePhilipp Maier1-28/+28
The methods verify_binary and verify_record are only used internally in class SimCardCommands, they can be both private methods. Also lets move them above the method that uses them. Related: OS#5714 Change-Id: I57c9af3d6ff45caa4378c400643b4ae1fa42ecac
2023-07-11pySim/commands.py: Better type annotationsHarald Welte1-45/+54
Change-Id: I68081b5472188f80a964ca48d5ec1f03adc70c4a
2023-06-07ts_31_102: Add shell command for GET IDENTITYHarald Welte1-0/+5
GET IDENTITY is used in the "SUCI computation on USIM" feature. Change-Id: I619d397900dbd6565f8f46acdabcee511903830c
2023-06-07ts_102221: Add "resume_uicc" commandHarald Welte1-0/+7
We've had a "suspend_uicc" command since commit ec95053249bc7f9308ca1f659d8ef6ac97a6e393 in 2021, but didn't yet have the corresponding "resume" pair. Note that you cannot really execute this in a reasonable way from within pySim, as it is required to power-cycle the card between SUSPEND and RESUME, see TS 102 221 Section 11.1.22.3.2 Change-Id: I3322fde74f680e77954e1d3e18a32ef5662759f2
2023-06-06Fix result parsing of "suspend_uicc"Harald Welte1-1/+1
prior to this patch, the suspend_uicc command would always cause a python exception as a list of integers was returned by decode_duration rather than a single integer (that can be used with %u format string). Change-Id: I981e9d46607193176b28cb574564e6da546501ba
2023-03-22SimCardCommands.run_gsm(): use send_apdu_checksw()Vadim Yanitskiy1-1/+1
Change-Id: Ib713cf8154a3aba72bc5776a8d99ec47631ade28
2023-03-22fix SimCardCommands.run_gsm(): always use CLA=0xa0Vadim Yanitskiy1-1/+1
Depending on the card type (SIM or USIM/ISUM), self.cla_byte may be either 0xa0 or 0x00. Sending RUN GSM ALGORITHM with CLA=0x00 fails with SW=6985 (Command not allowed), so let's make sure that we always use CLA=0xa0 regardless of the card type. Change-Id: Ia0abba136dbd4cdea8dbbc3c4d6abe12c2863680
2023-03-09ts_102_222: Implement support for RESIZE FILE for an EFHarald Welte1-0/+4
This adds pySim-shell support for the RESIZE FILE command in order to change the size of linear fixed or transparent EF. Change-Id: I03fbb683e26231c75f345330ac5f914ac88bbe7a
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-03commands: add ".." notation to expand hexstringsPhilipp Maier1-5/+10
When updating files and records there are sometimes huge portions that are just 0xff. Mostly this is at the end of a file or record that is not completely used. Lets add a notation to tell PySim-shell how to fill those sections. Change-Id: Iedd7887bf7d706878f4a3beca8dbea456404610b
2022-03-01Initial support for GlobalPlatformHarald Welte1-0/+4
One can now select the Issuer Security Domain (hard-coded to a000000003000000) and issue get_data requests. FCI and other TLV objects are dcoded, e.g. pySIM-shell (MF)> select ADF.ISD { "application_id": "a000000003000000", "proprietary_data": { "maximum_length_of_data_field_in_command_message": 255 } } pySIM-shell (MF/ADF.ISD)> get_data CardData { "card_data": [ { "card_recognition_data": [ { "object_identifier": "2a864886fc6b01" }, { "card_management_type_and_version": [ { "object_identifier": "2a864886fc6b02020101" } ] }, { "card_identification_scheme": [ { "object_identifier": "2a864886fc6b03" } ] }, { "secure_channel_protocol_of_isd": [ { "object_identifier": "2a864886fc6b040215" } ] } ] } ] } Change-Id: If11267d45ab7aa371eea8c143abd9320c32b54d0
2022-02-15pySim-shell: support TS 102 222 administrative commandsHarald Welte1-0/+20
This adds support for creating/deleting and terminating files, as well as support for permanent card termination. Change-Id: I5b1ffb1334afa18d62beb642268066a30deb7ea6
2022-02-14commands: Add method to select parent DF ("cd ..")Harald Welte1-0/+4
This is useful when walking around the filesystem tree. Change-Id: Ib256c1b7319f2b5f9a06200fb96854ecb2b7f6bb
2022-02-11cosmetic: Switch to consistent four-spaces indent; run autopep8Harald Welte1-550/+570
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-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-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-05commands: do not check SW manually, use send_apdu_checksw()Philipp Maier1-16/+15
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-10-29commands: use python style commends to describe methodsPhilipp Maier1-2/+1
Change-Id: Iccc9f01769ee9274d01036d3fbbc161d8bca7628
2021-10-21pySim-shell: Add suspend_uicc commandHarald Welte1-1/+37
This is an optional command, and it is not supported by e.g. sysmoISIM-SJA2 Change-Id: Icc726ffd672744e56cc8dd3762891af507942c1e
2021-10-15commands: Fix read_binary() for non-zero offsetHarald Welte1-4/+5
Similar to the fix in Ie1aeaab29701946233ed73db3331039690d695da for update_binary(), read_binary() also contained a bug when treating non-zero offsets. Change-Id: Ic5c2f0ad1c1ec9c4e9c97e72895382f7b6fa9470 Related: OS#5254
2021-10-14commands: fix update_binary() with non-zero offsetHarald Welte1-2/+2
In Icc240d5c8c04198640eb118565ea99f10ba27466 we introduced support for writing files > 255 bytes by splitting the write into multiple chunks. However, at the same time, that commit broke support for writing data at non-zero offsets. Unfortunately, this is used extensively within pySim-prog e.g. for writing K + OP/OPc data to sysmoISIM-SJA2 and sysmoUSIM-SJS1 cards. This commit fixes the related problem. Change-Id: Ie1aeaab29701946233ed73db3331039690d695da Fixes: Icc240d5c8c04198640eb118565ea99f10ba27466 Closes: OS#5254
2021-10-14Add API + shell command for sending TERMINAL PROFILE to cardHarald Welte1-0/+6
This allows a very first start to play with PROACTIVE SIM Change-Id: Id8f23f7cebe0f9efce2c0ce4229509f35cd93d6a
2021-07-31Allow update_binary function to write more than 255 bytesandrew-ma1-3/+15
The T0 protocol (selected in transport/pcsc.py) does not support extended APDU, so 255 bytes is the maximum number of bytes that can be transmitted at a time. We can divide large data into 255 byte chunks. The read_binary function already has code to read more than 255 bytes, so we can just adapt it to the update_binary function. Change-Id: Icc240d5c8c04198640eb118565ea99f10ba27466
2021-06-13pySim/commands: Add envelope() method for ENVELOPE commandHarald Welte1-0/+4
Change-Id: I2b5b6585ecbe00b54919b197428fe09a220757c6
2021-05-25shell: Fix activate_file + deactivate_file commandsHarald Welte1-2/+2
We cannot re-activate a deactivated file after we have selected somethng else, as SELECT will fail on the deactivated file. Hence, the deactivate_file command needs to be used with a file name as argument. Change-Id: Ief4d2bf8ea90497a8f25d1986aeea935c615f9bb
2021-05-25shell: Add 'status' command to issue STATUS APDUHarald Welte1-0/+4
This can be used to get the FCP of the currently selected file. Change-Id: I65c97adadd831ca2daa5a0dbb52a37999f8514fd
2021-05-23commands: remove superfluous getter/setter for cla_byte propertyHarald Welte1-8/+1
There's little point in having a getter+setter for a property if all it does is assigning a value to an attribute of self. That works without any property methods Change-Id: Id214cc83a29e8aa88f4e1413e07b419285c1b7ff
2021-05-23commands: resolve inconsistency on sel_ctrlHarald Welte1-7/+0
The code uses self.sel_ctrl everywhere except in the two @property methods, where the _sel_ctrl variable is used. Let's just abandon those property methods and make sure all users directly use the [public] sel_ctrl member variable. Change-Id: I10362300c1cf7b493d89bf71bbd3a10c80ef9a49
2021-05-23commands: pad short data input in update_record()Philipp Maier1-6/+13
The method update_record as a "force_len" parameter, which is somewhat irretatating. Some explainatory comments and a reformat of the if statement will help to make it more understandable to the api user. In the non force_len case the method determines the record length from the select response and throws an exception if the data input does not match that length. This makes sense if the data input exceeds the record length of the file but if the data input is less then the record length the situation is fixable by padding the input with 0xff. This also a quite common case because in some situation it is not guaranteed that the data will fill the entire record. Change-Id: I9a5df0e46c3dd2e87d447c5c01cf15844b0eed07 Related: OS#4963
2021-05-22commands: check for status word in USIM authenticate commandHarald Welte1-1/+1
Change-Id: I4c7e7261dd597cef0825826b36d50a144efa90d9
2021-05-04BER-TLV EF support (command, filesystem, shell)Harald Welte1-2/+72
This adds support for a new EF file type: BER-TLV files. They are different from transparent and linear fixed EFs in that they neither operate on a byte stream nor fixed-sized records, but on BER-TLV encoded objects. One can specify a tag value, and the card will return the entire TLV for that tag. As indicated in the spec, the magic tag value 0x5C (92) will return a list of tags existing in the file. Change-Id: Ibfcce757dcd477fd0d6857f64fbb4346d6d62e63
2021-04-11pySim-shell: Add open_channel + close_channel commandsHarald Welte1-0/+9
Change-Id: I53d9d7f7720eb5f10956bff74ea7ba9fd3b3bd19
2021-04-11pySim-shell: Adds support for DEACTIVATE FILE + ACTIVATE FILEHarald Welte1-0/+8
Change-Id: I22207dde20f991b0a22dea8f5dd695a0ec99da33
2021-04-11pySim-shell: Authenticate (3g) supportHarald Welte1-0/+22
This adds support for AUTHENTICATE to the USIM and ISIM application, based on the newly-introduced 'construct' encoder/decoder support. Change-Id: Id5697463e29c3dceff98bcf80f5400f7f2bcaa6c
2021-04-02Add more documentation to the classes/methodsHarald Welte1-19/+82
* add type annotations in-line with PEP484 * convert existing documentation to follow the "Google Python Style Guide" format understood by the sphinx.ext.napoleon' extension * add much more documentation all over the code base Change-Id: I6ac88e0662cf3c56ae32d86d50b18a8b4150571a
2021-04-02pySim-shell: complete CHV/PIN management toolsPhilipp Maier1-6/+37
At the moment we only have a basic version of a verify_chv commnad, but in order to handle any CHV/PIN related situation we also need commands to enable, disable, change and unblock CHV. - fix verify_chv commnad: more distinct parameter names, better help strings, correct pin code encoding and add external source lookup - Add unblock_chv, change_chv, enable_chv and disable_chv commands - add/fix related functions in commands.py Change-Id: Ic89446e6bd2021095e579fb6b20458df48ba6413 Related: OS#4963
2021-04-02commands: conserve write cyclesPhilipp Maier1-3/+19
When a record or a binary file is written the card goes throth a full flash/eeprom write cycle at this location, even when the data does not change. This can be optimized by reading before writing in order to compere if the data we are about to write is actually different. Change-Id: Ifd1b80d3ede15a7caa29077a37ac7cf58c9053f1 Related: OS#4963
2021-03-23commands: better exception string for authentication failuresPhilipp Maier1-1/+4
At the moment we use the send_apdu_checksw() method to send the APDU for ADM authentication. This method only checks if the command returns with sw = 9000. If not it raises an exception that the sw is not as expected. The user may think that this is a problem with thr reader, pcscd or pySim in the first place and may try multiple times until the card is permanently locked. A better execption string that also displays the tries which are left may be helpful. Change-Id: Icf428831094f8c1045eefaa8cb2b92e6a36b0c13 Related: OS#4963
2021-03-02commands.py: Introduce a real select_file() methodHarald Welte1-1/+4
This method, like select_adf(), only selects a single file ID and unlike select_path() returns the actual status words returned by the card. Change-Id: I8bc86654c6d79f2428e196cc8a401e12d93a676b
2021-03-02commands.py: rename select_file() to select_path()Harald Welte1-10/+10
In reality, the function is not a simple avstraction around the SELECT command, but it iterates over a list/path and selects at each element. Change-Id: I63e01155de4ae47aeed8500708c0eb6580c7b8d1
2021-03-01Remove Python shebang from files where it's not neededVadim Yanitskiy1-1/+0
Change-Id: I1d08544c37f50416acf8dc30139c572c029790d0