ts_51_011: replace encoding of EF.MSISDN with construct model

The encoding of EF.MSISDN is currently done with enc_msisdn and
dec_msisdn from utils.py. Let's replace this with a construct
based model, similar to the one we already use with EF.ADN

Related: OS#5714
Change-Id: I647f5c63f7f87902a86c0c5d8e92fdc7f4350a5a
This commit is contained in:
Philipp Maier
2024-09-12 10:24:46 +02:00
parent 4045146f62
commit 39552464d8
2 changed files with 48 additions and 15 deletions

View File

@@ -40,7 +40,6 @@ from osmocom.utils import *
from osmocom.construct import *
from pySim.utils import dec_iccid, enc_iccid, dec_imsi, enc_imsi, dec_plmn, enc_plmn, dec_xplmn_w_act
from pySim.utils import dec_msisdn, enc_msisdn
from pySim.profile import CardProfile, CardProfileAddon
from pySim.filesystem import *
from pySim.ts_31_102_telecom import DF_PHONEBOOK, DF_MULTIMEDIA, DF_MCS, DF_V2X
@@ -189,20 +188,54 @@ class EF_SMS(LinFixedEF):
# TS 51.011 Section 10.5.5
class EF_MSISDN(LinFixedEF):
_test_de_encode = [
( 'ffffffffffffffffffffffffffffffffffffffff04b12143f5ffffffffffffffffff',
{"alpha_id": "", "len_of_bcd": 4, "ton_npi": {"ext": True, "type_of_number": "network_specific",
"numbering_plan_id": "isdn_e164"},
"dialing_nr": "12345f"}),
( '456967656e65205275666e756d6d6572ffffffff0891947172199181f3ffffffffff',
{"alpha_id": "Eigene Rufnummer", "len_of_bcd": 8, "ton_npi": {"ext": True, "type_of_number": "international",
"numbering_plan_id": "isdn_e164"},
"dialing_nr": "4917279119183f"}),
]
# Ensure deprecated representations still work
_test_encode = [
( 'ffffffffffffffffffffffffffffffffffffffff05b1716662f6ffffffffffffffff',
{"msisdn": [ 1, 3, "1766266"]}),
( 'ffffffffffffffffffffffffffffffffffffffff06b121436587f9ffffffffffffff',
{"msisdn": "123456789"}),
]
_test_no_pad = True
def __init__(self, fid='6f40', sfid=None, name='EF.MSISDN', desc='MSISDN', **kwargs):
super().__init__(fid, sfid=sfid, name=name, desc=desc, rec_len=(15, 34), leftpad=True, **kwargs)
self._construct = Struct('alpha_id'/COptional(GsmOrUcs2Adapter(Rpad(Bytes(this._.total_len-14)))),
'len_of_bcd'/Int8ub,
'ton_npi'/TonNpi,
'dialing_nr'/ExtendedBcdAdapter(BcdAdapter(Rpad(Bytes(10)))),
Padding(2, pattern=b'\xff'))
def _decode_record_hex(self, raw_hex_data, **kwargs):
return {'msisdn': dec_msisdn(raw_hex_data)}
def _encode_record_hex(self, abstract, **kwargs):
msisdn = abstract['msisdn']
if type(msisdn) == str:
encoded_msisdn = enc_msisdn(msisdn)
else:
encoded_msisdn = enc_msisdn(msisdn[2], msisdn[0], msisdn[1])
alpha_identifier = (list(self.rec_len)[0] - len(encoded_msisdn) // 2) * "ff"
return alpha_identifier + encoded_msisdn
# Maintain compatibility with deprecated representations
def encode_record_hex(self, abstract_data: dict, record_nr: int, total_len: int = None) -> str:
if 'msisdn' in abstract_data:
msisdn = abstract_data['msisdn']
if type(msisdn) == str:
npi = 'isdn_e164'
ton = 'network_specific'
dialing_nr = msisdn + len(msisdn) % 2 * "f"
else:
npi = msisdn[0]
ton = msisdn[1]
dialing_nr = msisdn[2] + len(msisdn[2]) % 2 * "f"
abstract_data = {'alpha_id' : "",
'len_of_bcd' : len(dialing_nr) // 2 + 1,
'ton_npi' : {'ext' : True,
'type_of_number' : ton,
'numbering_plan_id' : npi},
'dialing_nr' : dialing_nr}
return super().encode_record_hex(abstract_data, record_nr, total_len)
# TS 51.011 Section 10.5.6
class EF_SMSP(LinFixedEF):

View File

@@ -125,7 +125,7 @@ CardReset(3b9f96801f878031e073fe211b674a4c753034054ba9)
===============================
00 READ RECORD MF/DF.TELECOM/DF.PHONEBOOK/EF.PBR 01 9000 {"raw": "a81ec0034f3a01c1034f3202c3034f5414c5034f0904c6034f5212c9034f2109a90ac4034f1108ca034f500daa14c2034f4a03c7034f4b06c8034f5313cb034f4f16ffffff"}
===============================
00 READ RECORD MF/ADF.USIM/EF.MSISDN 01 9000 {"msisdn": null}
00 READ RECORD MF/ADF.USIM/EF.MSISDN 01 9000 {"alpha_id": null, "len_of_bcd": 255, "ton_npi": {"ext": false, "type_of_number": "unknown", "numbering_plan_id": 7}, "dialing_nr": "19491234567890"}
===============================
00 UPDATE RECORD MF/ADF.USIM/EF.EPSNSC 01 9000 {"eps_nas__security__context": [{"ksi_asme": 7}, {"k_asme": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, {"uplink_nas_count": 4294967295}, {"downlink_nas_count": 4294967295}, {"i_dof_nas_algorithms": "00"}]}
===============================
@@ -203,7 +203,7 @@ CardReset(3b9f96801f878031e073fe211b674a4c753034054ba9)
===============================
00 READ RECORD MF/DF.TELECOM/EF.FDN 01 9000 {"alpha_id": "", "len_of_bcd": 255, "ton_npi": {"ext": true, "type_of_number": "reserved_for_extension", "numbering_plan_id": "reserved_for_extension"}, "dialing_nr": "", "cap_conf_id": 255, "ext2_record_id": 255}
===============================
00 READ RECORD MF/ADF.USIM/EF.MSISDN 01 9000 {"msisdn": null}
00 READ RECORD MF/ADF.USIM/EF.MSISDN 01 9000 {"alpha_id": null, "len_of_bcd": 255, "ton_npi": {"ext": false, "type_of_number": "unknown", "numbering_plan_id": 7}, "dialing_nr": "19491234567890"}
===============================
00 SEARCH RECORD MF/DF.TELECOM/EF.SDN 01 9000 {"cmd": {"file": "currently_selected_ef", "mode": "forward_search", "record_number": 1, "search_string": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, "rsp": {"body": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], "sw": "9000"}}
===============================
@@ -357,7 +357,7 @@ CardReset(3b9f96801f878031e073fe211b674a4c753034054ba9)
===============================
00 MANAGE CHANNEL 02 9000 {"mode": "open_channel", "created_channel": 2}
===============================
00 READ RECORD MF/ADF.USIM/EF.MSISDN 01 9000 {"msisdn": null}
00 READ RECORD MF/ADF.USIM/EF.MSISDN 01 9000 {"alpha_id": null, "len_of_bcd": 255, "ton_npi": {"ext": false, "type_of_number": "unknown", "numbering_plan_id": 7}, "dialing_nr": "19491234567890"}
===============================
01 READ RECORD MF/ADF.ISIM/EF.IMPU 03 9000 {"impu": null}
===============================