pySim.esim.saip: Refactor from_der() method to have class_for_petype()

Change-Id: I2e70dddb0b3adb41781e4db76de60bff2ae4fdb7
This commit is contained in:
Harald Welte
2024-07-28 20:48:20 +02:00
parent 6b1c6a986c
commit 4f9ee0fa75

View File

@@ -274,26 +274,48 @@ class ProfileElement:
return self.decoded.get('templateID', None) return self.decoded.get('templateID', None)
@classmethod @classmethod
def from_der(cls, der: bytes) -> 'ProfileElement': def class_for_petype(cls, pe_type: str) -> Optional['ProfileElement']:
"""Return the subclass implementing the given pe-type string."""
class4petype = { class4petype = {
'securityDomain': ProfileElementSD, # use same order as ASN.1 source definition of "ProfileElement ::= CHOICE {"
'mf': ProfileElementMF, 'header': ProfileElementHeader,
'pukCodes': ProfileElementPuk, 'genericFileManagement': ProfileElementGFM,
'pinCodes': ProfileElementPin, 'pinCodes': ProfileElementPin,
'pukCodes': ProfileElementPuk,
'akaParameter': ProfileElementAKA,
# TODO: cdmaParameter
'securityDomain': ProfileElementSD,
# TODO: rfm
# TODO: application
# TODO: nonStandard
'end': ProfileElementEnd,
'mf': ProfileElementMF,
# TODO: cd
'telecom': ProfileElementTelecom, 'telecom': ProfileElementTelecom,
'usim': ProfileElementUSIM, 'usim': ProfileElementUSIM,
'opt-usim': ProfileElementOptUSIM, 'opt-usim': ProfileElementOptUSIM,
'isim': ProfileElementISIM, 'isim': ProfileElementISIM,
'opt-isim': ProfileElementOptISIM, 'opt-isim': ProfileElementOptISIM,
'akaParameter': ProfileElementAKA, # TODO: phonebook
'header': ProfileElementHeader, # TODO: gsm-access
'genericFileManagement': ProfileElementGFM, # TODO: csim
'end': ProfileElementEnd, # TODO: opt-csim
# TODO: eap
# TODO: df-5gs
# TODO: df-saip
} }
if pe_type in class4petype:
return class4petype[pe_type]
else:
return None
@classmethod
def from_der(cls, der: bytes) -> 'ProfileElement':
"""Construct an instance from given raw, DER encoded bytes.""" """Construct an instance from given raw, DER encoded bytes."""
pe_type, decoded = asn1.decode('ProfileElement', der) pe_type, decoded = asn1.decode('ProfileElement', der)
if pe_type in class4petype: pe_cls = cls.class_for_petype(pe_type)
inst = class4petype[pe_type](decoded) if pe_cls:
inst = pe_cls(decoded)
else: else:
inst = ProfileElement(decoded) inst = ProfileElement(decoded)
inst.type = pe_type inst.type = pe_type