Hoernchen has uploaded this change for review.

View Change

euicc: fix profile reset DER encoding

ResetOptions is an ASN.1 BIT STRING, DER encodes it with the unused
bit count first and trailing zero bits stripped, so
deleteOperationalProfiles alone is 07 80, not plain 80 produced by
FlagsEnum(Byte).

Why does this look so fancy? Well, construct has no DER BIT STRING
primitive, and the flat decoded dict ({'deleteOperationalProfiles': True, ...})
that the shell euicc_memory_reset passes has to survive, so something must
flatten the unused bit prefix.

Change-Id: I318ea7b08bad9e0b34b14929ef750151f4c5fa4e
---
M pySim/euicc.py
M tests/unittests/test_euicc.py
2 files changed, 59 insertions(+), 4 deletions(-)

git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/25/43725/1
diff --git a/pySim/euicc.py b/pySim/euicc.py
index 0a6c054..fdcb9e8 100644
--- a/pySim/euicc.py
+++ b/pySim/euicc.py
@@ -25,7 +25,8 @@

import argparse

-from construct import Array, Struct, FlagsEnum, GreedyRange
+from construct import Array, Struct, FlagsEnum, GreedyRange, Adapter, Byte, Int8ub
+from construct import Optional as COptional
from cmd2 import cmd2, CommandSet, with_default_category
from osmocom.utils import Hexstr
from osmocom.tlv import *
@@ -288,11 +289,32 @@
pass

# SGP.22 Section 5.7.19: EuiccMemoryReset
+class DerNamedBitString(Adapter):
+ """ASN.1 DER named BIT STRING over a FlagsEnum(Byte): the count of unused bits comes first,
+ trailing zero bits are stripped, all bits clear is the single octet 00."""
+ def __init__(self, flags: FlagsEnum):
+ super().__init__(Struct('unused'/Int8ub, 'value'/COptional(Byte)))
+ self.flags = flags
+
+ def _decode(self, obj, context, path):
+ if obj.value is None:
+ return self.flags.parse(b'\x00')
+ if obj.unused > 7 or obj.value & ((1 << obj.unused) - 1):
+ raise ValueError('invalid DER BIT STRING padding')
+ return self.flags.parse(bytes([obj.value]))
+
+ def _encode(self, obj, context, path):
+ value = self.flags.build(obj)[0]
+ if not value:
+ return {'unused': 0, 'value': None}
+ return {'unused': (value & -value).bit_length() - 1, 'value': value}
+
+ResetOptionsFlags = FlagsEnum(Byte, deleteOperationalProfiles=0x80, deleteFieldLoadedTestProfiles=0x40,
+ resetDefaultSmdpAddress=0x20)
class ResetOptions(BER_TLV_IE, tag=0x82):
- _construct = FlagsEnum(Byte, deleteOperationalProfiles=0x80, deleteFieldLoadedTestProfiles=0x40,
- resetDefaultSmdpAddress=0x20)
+ _construct = DerNamedBitString(ResetOptionsFlags)
class ResetResult(BER_TLV_IE, tag=0x80):
- _construct = Enum(Int8ub, ok=0, nothingToDelete=1, undefinedError=127)
+ _construct = Enum(Int8ub, ok=0, nothingToDelete=1, catBusy=5, undefinedError=127)
class EuiccMemoryResetReq(BER_TLV_IE, tag=0xbf34, nested=[ResetOptions]):
pass
class EuiccMemoryResetResp(BER_TLV_IE, tag=0xbf34, nested=[ResetResult]):
diff --git a/tests/unittests/test_euicc.py b/tests/unittests/test_euicc.py
index 29c66ea..918d888 100755
--- a/tests/unittests/test_euicc.py
+++ b/tests/unittests/test_euicc.py
@@ -27,5 +27,38 @@
self.assertEqual(compute_eid_checksum('89049032123451234512345678901299'), '89049032123451234512345678901235')
self.assertEqual(compute_eid_checksum(89049032123451234512345678901299), '89049032123451234512345678901235')

+class TestResetOptions(unittest.TestCase):
+
+ def test_der_named_bit_string_encodings(self):
+ cases = (
+ ({}, 'bf3403820100'),
+ ({'deleteOperationalProfiles': True}, 'bf340482020780'),
+ ({'deleteFieldLoadedTestProfiles': True}, 'bf340482020640'),
+ ({'resetDefaultSmdpAddress': True}, 'bf340482020520'),
+ ({'deleteOperationalProfiles': True,
+ 'deleteFieldLoadedTestProfiles': True}, 'bf3404820206c0'),
+ ({'deleteOperationalProfiles': True,
+ 'resetDefaultSmdpAddress': True}, 'bf3404820205a0'),
+ ({'deleteFieldLoadedTestProfiles': True,
+ 'resetDefaultSmdpAddress': True}, 'bf340482020560'),
+ ({'deleteOperationalProfiles': True,
+ 'deleteFieldLoadedTestProfiles': True,
+ 'resetDefaultSmdpAddress': True}, 'bf3404820205e0'),
+ )
+ for decoded, expected in cases:
+ with self.subTest(decoded=decoded):
+ encoded = EuiccMemoryResetReq(children=[ResetOptions(decoded=decoded)]).to_tlv()
+ self.assertEqual(encoded.hex(), expected)
+ parsed = EuiccMemoryResetReq()
+ parsed.from_tlv(encoded)
+ actual = parsed.children[0].decoded
+ for name in ResetOptionsFlags.flags:
+ self.assertEqual(actual[name], decoded.get(name, False))
+
+ def test_cat_busy_result(self):
+ reset = EuiccMemoryResetResp()
+ reset.from_tlv(bytes.fromhex('bf3403800105'))
+ self.assertEqual(reset.children[0].decoded, 'catBusy')
+
if __name__ == "__main__":
unittest.main()

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

Gerrit-MessageType: newchange
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I318ea7b08bad9e0b34b14929ef750151f4c5fa4e
Gerrit-Change-Number: 43725
Gerrit-PatchSet: 1
Gerrit-Owner: Hoernchen <ewild@sysmocom.de>