dexter has uploaded this change for review.

View Change

ts_51_011/EF.SMSP: fix handling of 'alpha_id' field

The field 'alpha_id' is technically not an optional field, even though
the specification describes it as optional. Once the card manufacturer
decides that the field should be present, it must be always present and
vice versa.

(see code comment for a more detailed description)

Related: SYS#7765
Change-Id: I0ec99b2648b22c56f9145345e4cd8776f9217701
---
M pySim/ts_51_011.py
1 file changed, 20 insertions(+), 1 deletion(-)

git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/62/42662/1
diff --git a/pySim/ts_51_011.py b/pySim/ts_51_011.py
index 0dbf6eb..26e2d18 100644
--- a/pySim/ts_51_011.py
+++ b/pySim/ts_51_011.py
@@ -331,7 +331,7 @@
'ton_npi'/TonNpi, 'call_number'/PaddedBcdAdapter(Rpad(Bytes(10))))
DestAddr = Struct('length'/Rebuild(Int8ub, lambda ctx: EF_SMSP.dest_addr_len(ctx)),
'ton_npi'/TonNpi, 'call_number'/PaddedBcdAdapter(Rpad(Bytes(10))))
- self._construct = Struct('alpha_id'/COptional(GsmOrUcs2Adapter(Rpad(Bytes(this._.total_len-28)))),
+ self._construct = Struct('alpha_id'/GsmOrUcs2Adapter(Rpad(Bytes(this._.total_len-28))),
'parameter_indicators'/InvertAdapter(BitStruct(
Const(7, BitsInteger(3)),
'tp_vp'/Flag,
@@ -345,6 +345,25 @@
'tp_dcs'/Bytes(1),
'tp_vp_minutes'/EF_SMSP.ValidityPeriodAdapter(Byte))

+ # Ensure 'alpha_id' is always present
+ def encode_record_hex(self, abstract_data: dict, record_nr: int, total_len: int = None) -> str:
+ # Problem: TS 51.011 Section 10.5.6 describes the 'alpha_id' field as optional. However, this is only true
+ # at the time when the record length of the file is set up in the file system. A card manufacturer may decide
+ # to remove the field by setting the record length to 28. Likewise, the card manaufacturer may also decide to
+ # set the field to a distinct length by setting the record length to a value greater than 28 (e.g. 14 bytes
+ # 'alpha_id' + 28 bytes). Due to the fixed nature of the record length, this eventually means that in practice
+ # 'alpha_id' is a mandatory field with a fixed length.
+ #
+ # Due to the problematic specification of 'alpha_id' as a pseudo-optional field at the beginning of a
+ # fixed-size memory, the construct definition in self._construct has been incorrectly implemented and the field
+ # has been marked as COptional. We may correct the problem by removing COptional. But to maintain compatibility,
+ # we then have to ensure that in case the field is not provided (None), it is set to an empty string ('').
+ #
+ # See also ts_31_102.py, class EF_OCI for a correct example.
+ if abstract_data['alpha_id'] is None:
+ abstract_data['alpha_id'] = ''
+ return super().encode_record_hex(abstract_data, record_nr, total_len)
+
# TS 51.011 Section 10.5.7
class EF_SMSS(TransparentEF):
class MemCapAdapter(Adapter):

To view, visit change 42662. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I0ec99b2648b22c56f9145345e4cd8776f9217701
Gerrit-Change-Number: 42662
Gerrit-PatchSet: 1
Gerrit-Owner: dexter <pmaier@sysmocom.de>