neels has uploaded this change for review. (
https://gerrit.osmocom.org/c/pysim/+/39743?usp=email )
Change subject: [3/7] personalization: refactor Puk
......................................................................
[3/7] personalization: refactor Puk
Implement abstract DecimalHexParam, and use it to refactor Puk1 and Puk2
to the new ConfigurableParameter implementation style.
DecimalHexParam will also be used for Pin and Adm soon.
Change-Id: I271e6c030c890778ab7af9ab3bc7997e22018f6a
---
M pySim/esim/saip/personalization.py
1 file changed, 33 insertions(+), 21 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/43/39743/1
diff --git a/pySim/esim/saip/personalization.py b/pySim/esim/saip/personalization.py
index c2d71d1..abcca61 100644
--- a/pySim/esim/saip/personalization.py
+++ b/pySim/esim/saip/personalization.py
@@ -326,32 +326,44 @@
filtered = list(filter(lambda x: x.type == wanted_type, l))
return filtered[0]
-class Puk(ConfigurableParameter):
- """Configurable PUK (Pin Unblock Code). String ASCII-encoded
digits."""
- keyReference = None
- def validate(self):
- if isinstance(self.input_value, int):
- self.value = '%08d' % self.input_value
- else:
- self.value = self.input_value
- # FIXME: valid length?
- if not self.value.isdecimal():
- raise ValueError('PUK must only contain decimal digits')
+class DecimalHexParam(DecimalParam):
+ rpad = None
+ rpad_char = None
- def apply(self, pes: ProfileElementSequence):
- puk = ''.join(['%02x' % (ord(x)) for x in self.value])
- padded_puk = rpad(puk, 16)
+ @classmethod
+ def validate_val(cls, val):
+ val = super().validate_val(val)
+ val = ''.join('%02x' % ord(x) for x in val)
+ if cls.rpad is not None:
+ c = cls.rpad_char or 'f'
+ val = rpad(val, cls.rpad, c)
+ # a DecimalHexParam subclass expects the apply_val() input to be a bytes instance
ready for the pes
+ return h2b(val)
+
+class Puk(DecimalHexParam):
+ """Configurable PUK (Pin Unblock Code). String ASCII-encoded
digits."""
+ allow_len = 8
+ rpad = 16
+ keyReference = None
+
+ @classmethod
+ def apply_val(cls, pes: ProfileElementSequence, val):
+ val_bytes = val
mf_pes = pes.pes_by_naa['mf'][0]
pukCodes = obtain_singleton_pe_from_pelist(mf_pes, 'pukCodes')
for pukCode in pukCodes.decoded['pukCodes']:
- if pukCode['keyReference'] == self.keyReference:
- pukCode['pukValue'] = h2b(padded_puk)
+ if pukCode['keyReference'] == cls.keyReference:
+ pukCode['pukValue'] = val_bytes
return
- raise ValueError('cannot find pukCode')
-class Puk1(Puk, keyReference=0x01):
- pass
-class Puk2(Puk, keyReference=0x81):
- pass
+ raise ValueError('input template UPP has unexpected structure:'
+ + f' cannot find pukCode with keyReference={cls.keyReference}')
+
+class Puk1(Puk):
+ keyReference = 0x01
+
+class Puk2(Puk):
+ keyReference = 0x81
+
class Pin(ConfigurableParameter):
"""Configurable PIN (Personal Identification Number). String of
digits."""
--
To view, visit
https://gerrit.osmocom.org/c/pysim/+/39743?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: newchange
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I271e6c030c890778ab7af9ab3bc7997e22018f6a
Gerrit-Change-Number: 39743
Gerrit-PatchSet: 1
Gerrit-Owner: neels <nhofmeyr(a)sysmocom.de>