From 9a75410a88de667232d9194352e7ab1572322ed6 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Thu, 21 Oct 2021 13:46:59 +0200 Subject: [PATCH] utils: Fix BER-TLV tag decode for multi-byte tags We cannot simply skip anything that has 0xFF as first byte to detect the padding after the end of a TLV object: 0xFF may very well be a valid first octet of a multi-octet TAG: Tags of private class (11) with constructed (1) payload will have 0xFF as first octet. So let's expand the check to only detect padding in case of either only a single byte FF being left, or two FF following each other [with whatever suffix]. Change-Id: I5d64ce9ef1d973804daabae0b15c2e2349e6fab9 --- pySim/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pySim/utils.py b/pySim/utils.py index 777638c2..def88f03 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -173,7 +173,8 @@ def bertlv_parse_tag_raw(binary:bytes) -> Tuple[int, bytes]: Returns: Tuple of (tag:int, remainder:bytes) """ - if binary[0] == 0xff: + # check for FF padding at the end, as customary in SIM card files + if binary[0] == 0xff and len(binary) == 1 or binary[0] == 0xff and binary[1] == 0xff: return None, binary tag = binary[0] & 0x1f if tag <= 30: