mirror of
https://gitea.osmocom.org/sim-card/pysim.git
synced 2026-03-29 19:00:44 +03:00
pylint: global_platform/__init__.py
pySim/global_platform/__init__.py:468:4: W0221: Number of parameters was 2 in 'CardFile.decode_select_response' and is now 1 in overriding 'ADF_SD.decode_select_response' method (arguments-differ) pySim/global_platform/__init__.py:473:8: W0246: Useless parent or super() delegation in method '__init__' (useless-parent-delegation) pySim/global_platform/__init__.py:491:19: W0612: Unused variable 'sw' (unused-variable) pySim/global_platform/__init__.py:528:22: W0612: Unused variable 'sw' (unused-variable) pySim/global_platform/__init__.py:559:12: C0200: Consider using enumerate instead of iterating with range and len (consider-using-enumerate) pySim/global_platform/__init__.py:587:18: W0612: Unused variable 'sw' (unused-variable) pySim/global_platform/__init__.py:617:20: W0612: Unused variable 'dec' (unused-variable) pySim/global_platform/__init__.py:645:12: W0612: Unused variable 'data' (unused-variable) pySim/global_platform/__init__.py:645:18: W0612: Unused variable 'sw' (unused-variable) pySim/global_platform/__init__.py:746:15: C0121: Comparison 'opts.key_id == None' should be 'opts.key_id is None' (singleton-comparison) pySim/global_platform/__init__.py:746:39: C0121: Comparison 'opts.key_ver == None' should be 'opts.key_ver is None' (singleton-comparison) pySim/global_platform/__init__.py:750:15: C0121: Comparison 'opts.key_id != None' should be 'opts.key_id is not None' (singleton-comparison) pySim/global_platform/__init__.py:752:15: C0121: Comparison 'opts.key_ver != None' should be 'opts.key_ver is not None' (singleton-comparison) pySim/global_platform/__init__.py:787:16: W0612: Unused variable 'rsp_hex' (unused-variable) pySim/global_platform/__init__.py:787:25: W0612: Unused variable 'sw' (unused-variable) pySim/global_platform/__init__.py:836:30: W0612: Unused variable 'sw' (unused-variable) pySim/global_platform/__init__.py:839:12: W0612: Unused variable 'ext_auth_resp' (unused-variable) pySim/global_platform/__init__.py:846:33: W0613: Unused argument 'opts' (unused-argument) pySim/global_platform/__init__.py:878:15: R1716: Simplify chained comparison between the operands (chained-comparison) pySim/global_platform/__init__.py:886:29: W0613: Unused argument 'kvn' (unused-argument) pySim/global_platform/__init__.py:893:0: C0413: Import "from Cryptodome.Cipher import DES, DES3, AES" should be placed at the top of the module (wrong-import-position) pySim/global_platform/__init__.py:23:0: C0411: standard import "from typing import Optional, List, Dict, Tuple" should be placed before "from construct import Optional as COptional" (wrong-import-order) pySim/global_platform/__init__.py:24:0: C0411: standard import "from copy import deepcopy" should be placed before "from construct import Optional as COptional" (wrong-import-order) pySim/global_platform/__init__.py:893:0: C0411: third party import "from Cryptodome.Cipher import DES, DES3, AES" should be placed before "from pySim.global_platform.scp import SCP02, SCP03" (wrong-import-order) pySim/global_platform/__init__.py:893:0: C0412: Imports from package Cryptodome are not grouped (ungrouped-imports) Change-Id: Iea6afb5e72e035637e761bb25535f48fd4bc99f4
This commit is contained in:
@@ -17,12 +17,12 @@ You should have received a copy of the GNU General Public License
|
|||||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from copy import deepcopy
|
||||||
|
from typing import Optional, List, Dict, Tuple
|
||||||
from construct import Optional as COptional
|
from construct import Optional as COptional
|
||||||
from construct import Struct, GreedyRange, FlagsEnum, Int16ub, Int24ub, Padding, Bit, Const
|
from construct import Struct, GreedyRange, FlagsEnum, Int16ub, Int24ub, Padding, Bit, Const
|
||||||
from typing import Optional, List, Dict, Tuple
|
|
||||||
from copy import deepcopy
|
|
||||||
from bidict import bidict
|
|
||||||
from Cryptodome.Random import get_random_bytes
|
from Cryptodome.Random import get_random_bytes
|
||||||
|
from Cryptodome.Cipher import DES, DES3, AES
|
||||||
from pySim.global_platform.scp import SCP02, SCP03
|
from pySim.global_platform.scp import SCP02, SCP03
|
||||||
from pySim.construct import *
|
from pySim.construct import *
|
||||||
from pySim.utils import *
|
from pySim.utils import *
|
||||||
@@ -446,15 +446,11 @@ class ADF_SD(CardADF):
|
|||||||
super().__init__(aid=aid, fid=None, sfid=None, name=name, desc=desc)
|
super().__init__(aid=aid, fid=None, sfid=None, name=name, desc=desc)
|
||||||
self.shell_commands += [self.AddlShellCommands()]
|
self.shell_commands += [self.AddlShellCommands()]
|
||||||
|
|
||||||
@staticmethod
|
def decode_select_response(self, data_hex: str) -> object:
|
||||||
def decode_select_response(res_hex: str) -> object:
|
return decode_select_response(data_hex)
|
||||||
return decode_select_response(res_hex)
|
|
||||||
|
|
||||||
@with_default_category('Application-Specific Commands')
|
@with_default_category('Application-Specific Commands')
|
||||||
class AddlShellCommands(CommandSet):
|
class AddlShellCommands(CommandSet):
|
||||||
def __init__(self):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
get_data_parser = argparse.ArgumentParser()
|
get_data_parser = argparse.ArgumentParser()
|
||||||
get_data_parser.add_argument('data_object_name', type=str,
|
get_data_parser.add_argument('data_object_name', type=str,
|
||||||
help='Name of the data object to be retrieved from the card')
|
help='Name of the data object to be retrieved from the card')
|
||||||
@@ -470,7 +466,7 @@ class ADF_SD(CardADF):
|
|||||||
self._cmd.poutput('Unknown data object "%s", available options: %s' % (tlv_cls_name,
|
self._cmd.poutput('Unknown data object "%s", available options: %s' % (tlv_cls_name,
|
||||||
do_names))
|
do_names))
|
||||||
return
|
return
|
||||||
(data, sw) = self._cmd.lchan.scc.get_data(cla=0x80, tag=tlv_cls.tag)
|
(data, _sw) = self._cmd.lchan.scc.get_data(cla=0x80, tag=tlv_cls.tag)
|
||||||
ie = tlv_cls()
|
ie = tlv_cls()
|
||||||
ie.from_tlv(h2b(data))
|
ie.from_tlv(h2b(data))
|
||||||
self._cmd.poutput_json(ie.to_dict())
|
self._cmd.poutput_json(ie.to_dict())
|
||||||
@@ -507,7 +503,7 @@ class ADF_SD(CardADF):
|
|||||||
{'last_block': len(remainder) == 0, 'encryption': encryption,
|
{'last_block': len(remainder) == 0, 'encryption': encryption,
|
||||||
'structure': structure, 'response': response_permitted})
|
'structure': structure, 'response': response_permitted})
|
||||||
hdr = "80E2%02x%02x%02x" % (p1b[0], block_nr, len(chunk))
|
hdr = "80E2%02x%02x%02x" % (p1b[0], block_nr, len(chunk))
|
||||||
data, sw = self._cmd.lchan.scc.send_apdu_checksw(hdr + b2h(chunk))
|
data, _sw = self._cmd.lchan.scc.send_apdu_checksw(hdr + b2h(chunk))
|
||||||
block_nr += 1
|
block_nr += 1
|
||||||
response += data
|
response += data
|
||||||
return data
|
return data
|
||||||
@@ -566,7 +562,7 @@ class ADF_SD(CardADF):
|
|||||||
See GlobalPlatform CardSpecification v2.3 Section 11.8 for details."""
|
See GlobalPlatform CardSpecification v2.3 Section 11.8 for details."""
|
||||||
key_data = kvn.to_bytes(1, 'big') + build_construct(ADF_SD.AddlShellCommands.KeyDataBasic, key_dict)
|
key_data = kvn.to_bytes(1, 'big') + build_construct(ADF_SD.AddlShellCommands.KeyDataBasic, key_dict)
|
||||||
hdr = "80D8%02x%02x%02x" % (old_kvn, kid, len(key_data))
|
hdr = "80D8%02x%02x%02x" % (old_kvn, kid, len(key_data))
|
||||||
data, sw = self._cmd.lchan.scc.send_apdu_checksw(hdr + b2h(key_data))
|
data, _sw = self._cmd.lchan.scc.send_apdu_checksw(hdr + b2h(key_data))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
get_status_parser = argparse.ArgumentParser()
|
get_status_parser = argparse.ArgumentParser()
|
||||||
@@ -596,7 +592,7 @@ class ADF_SD(CardADF):
|
|||||||
while len(remainder):
|
while len(remainder):
|
||||||
# tlv sequence, each element is one GpRegistryRelatedData()
|
# tlv sequence, each element is one GpRegistryRelatedData()
|
||||||
grd = GpRegistryRelatedData()
|
grd = GpRegistryRelatedData()
|
||||||
dec, remainder = grd.from_tlv(remainder)
|
_dec, remainder = grd.from_tlv(remainder)
|
||||||
grd_list.append(grd)
|
grd_list.append(grd)
|
||||||
if sw != '6310':
|
if sw != '6310':
|
||||||
return grd_list
|
return grd_list
|
||||||
@@ -624,7 +620,7 @@ class ADF_SD(CardADF):
|
|||||||
'scope'/SetStatusScope, 'status'/CLifeCycleState,
|
'scope'/SetStatusScope, 'status'/CLifeCycleState,
|
||||||
'aid'/HexAdapter(Prefixed(Int8ub, COptional(GreedyBytes))))
|
'aid'/HexAdapter(Prefixed(Int8ub, COptional(GreedyBytes))))
|
||||||
apdu = build_construct(SetStatus, {'scope':scope, 'status':status, 'aid':aid})
|
apdu = build_construct(SetStatus, {'scope':scope, 'status':status, 'aid':aid})
|
||||||
data, sw = self._cmd.lchan.scc.send_apdu_checksw(b2h(apdu))
|
_data, _sw = self._cmd.lchan.scc.send_apdu_checksw(b2h(apdu))
|
||||||
|
|
||||||
inst_perso_parser = argparse.ArgumentParser()
|
inst_perso_parser = argparse.ArgumentParser()
|
||||||
inst_perso_parser.add_argument('application-aid', type=is_hexstr, help='Application AID')
|
inst_perso_parser.add_argument('application-aid', type=is_hexstr, help='Application AID')
|
||||||
@@ -700,13 +696,13 @@ class ADF_SD(CardADF):
|
|||||||
"""Perform GlobalPlaform DELETE (Key) command.
|
"""Perform GlobalPlaform DELETE (Key) command.
|
||||||
If both KID and KVN are specified, exactly one key is deleted. If only either of the two is
|
If both KID and KVN are specified, exactly one key is deleted. If only either of the two is
|
||||||
specified, multiple matching keys may be deleted."""
|
specified, multiple matching keys may be deleted."""
|
||||||
if opts.key_id == None and opts.key_ver == None:
|
if opts.key_id is None and opts.key_ver is None:
|
||||||
raise ValueError('At least one of KID or KVN must be specified')
|
raise ValueError('At least one of KID or KVN must be specified')
|
||||||
p2 = 0x80 if opts.delete_related_objects else 0x00
|
p2 = 0x80 if opts.delete_related_objects else 0x00
|
||||||
cmd = ""
|
cmd = ""
|
||||||
if opts.key_id != None:
|
if opts.key_id is not None:
|
||||||
cmd += "d001%02x" % opts.key_id
|
cmd += "d001%02x" % opts.key_id
|
||||||
if opts.key_ver != None:
|
if opts.key_ver is not None:
|
||||||
cmd += "d201%02x" % opts.key_ver
|
cmd += "d201%02x" % opts.key_ver
|
||||||
self.delete(0x00, p2, cmd)
|
self.delete(0x00, p2, cmd)
|
||||||
|
|
||||||
@@ -759,17 +755,17 @@ class ADF_SD(CardADF):
|
|||||||
def _establish_scp(self, scp, host_challenge, security_level):
|
def _establish_scp(self, scp, host_challenge, security_level):
|
||||||
# perform the common functionality shared by SCP02 and SCP03 establishment
|
# perform the common functionality shared by SCP02 and SCP03 establishment
|
||||||
init_update_apdu = scp.gen_init_update_apdu(host_challenge=host_challenge)
|
init_update_apdu = scp.gen_init_update_apdu(host_challenge=host_challenge)
|
||||||
init_update_resp, sw = self._cmd.lchan.scc.send_apdu_checksw(b2h(init_update_apdu))
|
init_update_resp, _sw = self._cmd.lchan.scc.send_apdu_checksw(b2h(init_update_apdu))
|
||||||
scp.parse_init_update_resp(h2b(init_update_resp))
|
scp.parse_init_update_resp(h2b(init_update_resp))
|
||||||
ext_auth_apdu = scp.gen_ext_auth_apdu(security_level)
|
ext_auth_apdu = scp.gen_ext_auth_apdu(security_level)
|
||||||
ext_auth_resp, sw = self._cmd.lchan.scc.send_apdu_checksw(b2h(ext_auth_apdu))
|
_ext_auth_resp, _sw = self._cmd.lchan.scc.send_apdu_checksw(b2h(ext_auth_apdu))
|
||||||
self._cmd.poutput("Successfully established a %s secure channel" % str(scp))
|
self._cmd.poutput("Successfully established a %s secure channel" % str(scp))
|
||||||
# store a reference to the SCP instance
|
# store a reference to the SCP instance
|
||||||
self._cmd.lchan.scc.scp = scp
|
self._cmd.lchan.scc.scp = scp
|
||||||
self._cmd.update_prompt()
|
self._cmd.update_prompt()
|
||||||
|
|
||||||
|
|
||||||
def do_release_scp(self, opts):
|
def do_release_scp(self, _opts):
|
||||||
"""Release a previously establiehed secure channel."""
|
"""Release a previously establiehed secure channel."""
|
||||||
if not self._cmd.lchan.scc.scp:
|
if not self._cmd.lchan.scc.scp:
|
||||||
self._cmd.poutput("Cannot release SCP as none is established")
|
self._cmd.poutput("Cannot release SCP as none is established")
|
||||||
@@ -801,7 +797,7 @@ class CardApplicationISD(CardApplicationSD):
|
|||||||
class GpCardKeyset:
|
class GpCardKeyset:
|
||||||
"""A single set of GlobalPlatform card keys and the associated KVN."""
|
"""A single set of GlobalPlatform card keys and the associated KVN."""
|
||||||
def __init__(self, kvn: int, enc: bytes, mac: bytes, dek: bytes):
|
def __init__(self, kvn: int, enc: bytes, mac: bytes, dek: bytes):
|
||||||
assert kvn >= 0 and kvn < 256
|
assert 0 < kvn < 256
|
||||||
assert len(enc) == len(mac) == len(dek)
|
assert len(enc) == len(mac) == len(dek)
|
||||||
self.kvn = kvn
|
self.kvn = kvn
|
||||||
self.enc = enc
|
self.enc = enc
|
||||||
@@ -810,13 +806,12 @@ class GpCardKeyset:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_single_key(cls, kvn: int, base_key: bytes) -> 'GpCardKeyset':
|
def from_single_key(cls, kvn: int, base_key: bytes) -> 'GpCardKeyset':
|
||||||
return cls(int, base_key, base_key, base_key)
|
return cls(kvn, base_key, base_key, base_key)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "%s(KVN=%u, ENC=%s, MAC=%s, DEK=%s)" % (self.__class__.__name__,
|
return "%s(KVN=%u, ENC=%s, MAC=%s, DEK=%s)" % (self.__class__.__name__,
|
||||||
self.kvn, b2h(self.enc), b2h(self.mac), b2h(self.dek))
|
self.kvn, b2h(self.enc), b2h(self.mac), b2h(self.dek))
|
||||||
|
|
||||||
from Cryptodome.Cipher import DES, DES3, AES
|
|
||||||
|
|
||||||
def compute_kcv_des(key:bytes) -> bytes:
|
def compute_kcv_des(key:bytes) -> bytes:
|
||||||
# GP Card Spec B.6: For a DES key, the key check value is computed by encrypting 8 bytes, each with
|
# GP Card Spec B.6: For a DES key, the key check value is computed by encrypting 8 bytes, each with
|
||||||
|
|||||||
Reference in New Issue
Block a user