pySim.apdu: Get rid of HexAdapter

In the past, we always wrapped a HexAdapter around bytes-like data in
order to make sure it's printed as hex-digits.  However, now that we are
doing JSON output it's much easier to let the pySim.utils.JsonEncoder
take care of this in a generic way.

We should do a similar migration all over pySim (pySim-shell,
filesystem, etc.) - but for now only do it in the low-hanging fruit of
pySim-trace aka pySim.apdu

Change-Id: I0cde40b2db08b4db9c10c1ece9ca6fdd42aa9154
This commit is contained in:
Harald Welte
2024-07-19 18:46:30 +02:00
committed by laforge
parent c800f2a716
commit 87b4f99a90
4 changed files with 33 additions and 33 deletions

View File

@@ -150,8 +150,8 @@ class ApduCommand(Apdu, metaclass=ApduCommandMeta):
# fall-back constructs if the derived class provides no override # fall-back constructs if the derived class provides no override
_construct_p1 = Byte _construct_p1 = Byte
_construct_p2 = Byte _construct_p2 = Byte
_construct = HexAdapter(GreedyBytes) _construct = GreedyBytes
_construct_rsp = HexAdapter(GreedyBytes) _construct_rsp = GreedyBytes
_tlv = None _tlv = None
_tlv_rsp = None _tlv_rsp = None

View File

@@ -99,7 +99,7 @@ class UiccSelect(ApduCommand, n='SELECT', ins=0xA4, cla=['0X', '4X', '6X']):
logger.warning('SELECT UNKNOWN FID %s', file_hex) logger.warning('SELECT UNKNOWN FID %s', file_hex)
elif mode == 'df_name': elif mode == 'df_name':
# Select by AID (can be sub-string!) # Select by AID (can be sub-string!)
aid = self.cmd_dict['body'] aid = b2h(self.cmd_dict['body'])
sels = lchan.rs.mf.get_app_selectables(['AIDS']) sels = lchan.rs.mf.get_app_selectables(['AIDS'])
adf = self._find_aid_substr(sels, aid) adf = self._find_aid_substr(sels, aid)
if adf: if adf:
@@ -115,7 +115,7 @@ class UiccSelect(ApduCommand, n='SELECT', ins=0xA4, cla=['0X', '4X', '6X']):
self.file = lchan.selected_file self.file = lchan.selected_file
if 'body' in self.rsp_dict: if 'body' in self.rsp_dict:
# not every SELECT is asking for the FCP in response... # not every SELECT is asking for the FCP in response...
return lchan.selected_file.decode_select_response(self.rsp_dict['body']) return lchan.selected_file.decode_select_response(b2h(self.rsp_dict['body']))
return None return None
@@ -128,7 +128,7 @@ class UiccStatus(ApduCommand, n='STATUS', ins=0xF2, cla=['8X', 'CX', 'EX']):
def process_on_lchan(self, lchan): def process_on_lchan(self, lchan):
if self.cmd_dict['p2'] == 'response_like_select': if self.cmd_dict['p2'] == 'response_like_select':
return lchan.selected_file.decode_select_response(self.rsp_dict['body']) return lchan.selected_file.decode_select_response(b2h(self.rsp_dict['body']))
def _decode_binary_p1p2(p1, p2) -> Dict: def _decode_binary_p1p2(p1, p2) -> Dict:
ret = {} ret = {}

View File

@@ -42,28 +42,28 @@ class UsimAuthenticateEven(ApduCommand, n='AUTHENTICATE', ins=0x88, cla=['0X', '
BitsInteger(4), BitsInteger(4),
'authentication_context'/Enum(BitsInteger(3), gsm=0, umts=1, 'authentication_context'/Enum(BitsInteger(3), gsm=0, umts=1,
vgcs_vbs=2, gba=4)) vgcs_vbs=2, gba=4))
_cs_cmd_gsm_3g = Struct('_rand_len'/Int8ub, 'rand'/HexAdapter(Bytes(this._rand_len)), _cs_cmd_gsm_3g = Struct('_rand_len'/Int8ub, 'rand'/Bytes(this._rand_len),
'_autn_len'/COptional(Int8ub), 'autn'/If(this._autn_len, HexAdapter(Bytes(this._autn_len)))) '_autn_len'/COptional(Int8ub), 'autn'/If(this._autn_len, Bytes(this._autn_len)))
_cs_cmd_vgcs = Struct('_vsid_len'/Int8ub, 'vservice_id'/HexAdapter(Bytes(this._vsid_len)), _cs_cmd_vgcs = Struct('_vsid_len'/Int8ub, 'vservice_id'/Bytes(this._vsid_len),
'_vkid_len'/Int8ub, 'vk_id'/HexAdapter(Bytes(this._vkid_len)), '_vkid_len'/Int8ub, 'vk_id'/Bytes(this._vkid_len),
'_vstk_rand_len'/Int8ub, 'vstk_rand'/HexAdapter(Bytes(this._vstk_rand_len))) '_vstk_rand_len'/Int8ub, 'vstk_rand'/Bytes(this._vstk_rand_len))
_cmd_gba_bs = Struct('_rand_len'/Int8ub, 'rand'/HexAdapter(Bytes(this._rand_len)), _cmd_gba_bs = Struct('_rand_len'/Int8ub, 'rand'/Bytes(this._rand_len),
'_autn_len'/Int8ub, 'autn'/HexAdapter(Bytes(this._autn_len))) '_autn_len'/Int8ub, 'autn'/Bytes(this._autn_len))
_cmd_gba_naf = Struct('_naf_id_len'/Int8ub, 'naf_id'/HexAdapter(Bytes(this._naf_id_len)), _cmd_gba_naf = Struct('_naf_id_len'/Int8ub, 'naf_id'/Bytes(this._naf_id_len),
'_impi_len'/Int8ub, 'impi'/HexAdapter(Bytes(this._impi_len))) '_impi_len'/Int8ub, 'impi'/Bytes(this._impi_len))
_cs_cmd_gba = Struct('tag'/Int8ub, 'body'/Switch(this.tag, { 0xDD: 'bootstrap'/_cmd_gba_bs, _cs_cmd_gba = Struct('tag'/Int8ub, 'body'/Switch(this.tag, { 0xDD: 'bootstrap'/_cmd_gba_bs,
0xDE: 'naf_derivation'/_cmd_gba_naf })) 0xDE: 'naf_derivation'/_cmd_gba_naf }))
_cs_rsp_gsm = Struct('_len_sres'/Int8ub, 'sres'/HexAdapter(Bytes(this._len_sres)), _cs_rsp_gsm = Struct('_len_sres'/Int8ub, 'sres'/Bytes(this._len_sres),
'_len_kc'/Int8ub, 'kc'/HexAdapter(Bytes(this._len_kc))) '_len_kc'/Int8ub, 'kc'/Bytes(this._len_kc))
_rsp_3g_ok = Struct('_len_res'/Int8ub, 'res'/HexAdapter(Bytes(this._len_res)), _rsp_3g_ok = Struct('_len_res'/Int8ub, 'res'/Bytes(this._len_res),
'_len_ck'/Int8ub, 'ck'/HexAdapter(Bytes(this._len_ck)), '_len_ck'/Int8ub, 'ck'/Bytes(this._len_ck),
'_len_ik'/Int8ub, 'ik'/HexAdapter(Bytes(this._len_ik)), '_len_ik'/Int8ub, 'ik'/Bytes(this._len_ik),
'_len_kc'/COptional(Int8ub), 'kc'/If(this._len_kc, HexAdapter(Bytes(this._len_kc)))) '_len_kc'/COptional(Int8ub), 'kc'/If(this._len_kc, Bytes(this._len_kc)))
_rsp_3g_sync = Struct('_len_auts'/Int8ub, 'auts'/HexAdapter(Bytes(this._len_auts))) _rsp_3g_sync = Struct('_len_auts'/Int8ub, 'auts'/Bytes(this._len_auts))
_cs_rsp_3g = Struct('tag'/Int8ub, 'body'/Switch(this.tag, { 0xDB: 'success'/_rsp_3g_ok, _cs_rsp_3g = Struct('tag'/Int8ub, 'body'/Switch(this.tag, { 0xDB: 'success'/_rsp_3g_ok,
0xDC: 'sync_fail'/_rsp_3g_sync})) 0xDC: 'sync_fail'/_rsp_3g_sync}))
_cs_rsp_vgcs = Struct(Const(b'\xDB'), '_vstk_len'/Int8ub, 'vstk'/HexAdapter(Bytes(this._vstk_len))) _cs_rsp_vgcs = Struct(Const(b'\xDB'), '_vstk_len'/Int8ub, 'vstk'/Bytes(this._vstk_len))
_cs_rsp_gba_naf = Struct(Const(b'\xDB'), '_ks_ext_naf_len'/Int8ub, 'ks_ext_naf'/HexAdapter(Bytes(this._ks_ext_naf_len))) _cs_rsp_gba_naf = Struct(Const(b'\xDB'), '_ks_ext_naf_len'/Int8ub, 'ks_ext_naf'/Bytes(this._ks_ext_naf_len))
def _decode_cmd(self) -> Dict: def _decode_cmd(self) -> Dict:
r = {} r = {}
r['p1'] = parse_construct(self._construct_p1, self.p1.to_bytes(1, 'big')) r['p1'] = parse_construct(self._construct_p1, self.p1.to_bytes(1, 'big'))

View File

@@ -30,8 +30,8 @@ class TestUsimAuth(unittest.TestCase):
'04a0a1a2a308b0b1b2b3b4b5b6b79000') '04a0a1a2a308b0b1b2b3b4b5b6b79000')
res = { res = {
'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'gsm'}, 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'gsm'},
'body': {'rand': '0001020304050607', 'autn': None}}, 'body': {'rand': h2b('0001020304050607'), 'autn': None}},
'rsp': {'body': {'sres': 'a0a1a2a3', 'kc': 'b0b1b2b3b4b5b6b7'}} 'rsp': {'body': {'sres': h2b('a0a1a2a3'), 'kc': h2b('b0b1b2b3b4b5b6b7')}}
} }
u = UsimAuthenticateEven(apdu[0], apdu[1]) u = UsimAuthenticateEven(apdu[0], apdu[1])
d = filter_dict(u.to_dict()) d = filter_dict(u.to_dict())
@@ -44,12 +44,12 @@ class TestUsimAuth(unittest.TestCase):
'10' + 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + '9000') '10' + 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf' + '9000')
res = { res = {
'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'umts'}, 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'umts'},
'body': {'rand': '0001020304050607', 'autn': '1011121314151617'}}, 'body': {'rand': h2b('0001020304050607'), 'autn': h2b('1011121314151617')}},
'rsp': {'body': {'tag': 219, 'rsp': {'body': {'tag': 219,
'body': { 'body': {
'res': 'a0a1a2a3a4a5a6a7', 'res': h2b('a0a1a2a3a4a5a6a7'),
'ck': 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf', 'ck': h2b('b0b1b2b3b4b5b6b7b8b9babbbcbdbebf'),
'ik': 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf', 'ik': h2b('c0c1c2c3c4c5c6c7c8c9cacbcccdcecf'),
'kc': None 'kc': None
} }
} }
@@ -64,8 +64,8 @@ class TestUsimAuth(unittest.TestCase):
'DC' + '08' + 'a0a1a2a3a4a5a6a7' + '9000') 'DC' + '08' + 'a0a1a2a3a4a5a6a7' + '9000')
res = { res = {
'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'umts'}, 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'umts'},
'body': {'rand': '0001020304050607', 'autn': '1011121314151617'}}, 'body': {'rand': h2b('0001020304050607'), 'autn': h2b('1011121314151617')}},
'rsp': {'body': {'tag': 220, 'body': {'auts': 'a0a1a2a3a4a5a6a7' }}} 'rsp': {'body': {'tag': 220, 'body': {'auts': h2b('a0a1a2a3a4a5a6a7') }}}
} }
u = UsimAuthenticateEven(apdu[0], apdu[1]) u = UsimAuthenticateEven(apdu[0], apdu[1])
d = filter_dict(u.to_dict()) d = filter_dict(u.to_dict())
@@ -78,8 +78,8 @@ class TestUsimAuth(unittest.TestCase):
'DB' + '10' + 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + '9000') 'DB' + '10' + 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf' + '9000')
res = { res = {
'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'vgcs_vbs'}, 'cmd': {'p1': 0, 'p2': {'scope': 'df_adf_specific', 'authentication_context': 'vgcs_vbs'},
'body': { 'vk_id': '10', 'vservice_id': '00010203', 'vstk_rand': '2021222324252627'}}, 'body': { 'vk_id': h2b('10'), 'vservice_id': h2b('00010203'), 'vstk_rand': h2b('2021222324252627')}},
'rsp': {'body': {'vstk': 'b0b1b2b3b4b5b6b7b8b9babbbcbdbebf'}} 'rsp': {'body': {'vstk': h2b('b0b1b2b3b4b5b6b7b8b9babbbcbdbebf')}}
} }
u = UsimAuthenticateEven(apdu[0], apdu[1]) u = UsimAuthenticateEven(apdu[0], apdu[1])
d = filter_dict(u.to_dict()) d = filter_dict(u.to_dict())