diff options
author | Harald Welte <laforge@osmocom.org> | 2023-06-26 10:52:19 +0200 |
---|---|---|
committer | Harald Welte <laforge@osmocom.org> | 2023-06-27 09:29:37 +0200 |
commit | 579ac3ec0eb122c83260d7509bc4e6d0cb2fe3b3 (patch) | |
tree | 1661d4e6ea00a77b46c3374c714745bd14eef58c | |
parent | 0ec01504ab894bce8dd0e2a882f21b0b253f1ee6 (diff) |
tlv: Fix IE.from_dict() method
The existing IE.from_dict() method *supposedly* accepts a dict as
input value, but it actually expects the raw decoded value, unless it is
a nested IE. This is inconsistent in various ways, and results in a bug
visible at a higher layer, such as files like EF.{DOMAIN,IMPI,IMPU},
which are transparent files containing a single BER-TLV IE.
Decoding such files worked, but re-encoding them did not, due to the
fact that we'd pass a dict to the from_dict method, which then gets
assigned to self.decoded and further passed along to any later actual
encoder function like to_bytes or to_tlv. In that instance, the dict
might be handed to a self._construct which has no idea how to process
the dict, as it expects the raw decoded value.
Change-Id: I3dd5204510e5c32ef1c4a999258d87cb3f1df8c8
Closes: OS#6073
Related: OS#6072
-rw-r--r-- | pySim/tlv.py | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/pySim/tlv.py b/pySim/tlv.py index a5baa23..bd95505 100644 --- a/pySim/tlv.py +++ b/pySim/tlv.py @@ -161,7 +161,10 @@ class IE(Transcodable, metaclass=TlvMeta): self.children = self.nested_collection.from_dict(decoded) else: self.children = [] - self.decoded = decoded + expected_key_name = camel_to_snake(type(self).__name__) + if not expected_key_name in decoded: + raise ValueError("Dict %s doesn't contain expected key %s" % (decoded, expected_key_name)) + self.decoded = decoded[expected_key_name] def is_constructed(self): """Is this IE constructed by further nested IEs?""" @@ -388,7 +391,7 @@ class TLV_IE_Collection(metaclass=TlvCollectionMeta): if k in self.members_by_name: cls = self.members_by_name[k] inst = cls() - inst.from_dict(i[k]) + inst.from_dict({k: i[k]}) res.append(inst) else: raise ValueError('%s: Unknown TLV Class %s in %s; expected %s' % |