Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | """ pySim: various utilities |
| 4 | """ |
| 5 | |
Harald Welte | 5e749a7 | 2021-04-10 17:18:17 +0200 | [diff] [blame] | 6 | import json |
Harald Welte | 3de6ca2 | 2021-05-02 20:05:56 +0200 | [diff] [blame] | 7 | import abc |
Philipp Maier | 796ca3d | 2021-11-01 17:13:57 +0100 | [diff] [blame] | 8 | import string |
Harald Welte | 5e749a7 | 2021-04-10 17:18:17 +0200 | [diff] [blame] | 9 | from io import BytesIO |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 10 | from typing import Optional, List, Dict, Any, Tuple |
| 11 | |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 12 | # Copyright (C) 2009-2010 Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | 5e749a7 | 2021-04-10 17:18:17 +0200 | [diff] [blame] | 13 | # Copyright (C) 2021 Harald Welte <laforge@osmocom.org> |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 14 | # |
| 15 | # This program is free software: you can redistribute it and/or modify |
| 16 | # it under the terms of the GNU General Public License as published by |
| 17 | # the Free Software Foundation, either version 2 of the License, or |
| 18 | # (at your option) any later version. |
| 19 | # |
| 20 | # This program is distributed in the hope that it will be useful, |
| 21 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | # GNU General Public License for more details. |
| 24 | # |
| 25 | # You should have received a copy of the GNU General Public License |
| 26 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | # |
| 28 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 29 | # just to differentiate strings of hex nibbles from everything else |
| 30 | Hexstr = str |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 31 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 32 | def h2b(s:Hexstr) -> bytearray: |
Harald Welte | 4f6ca43 | 2021-02-01 17:51:56 +0100 | [diff] [blame] | 33 | """convert from a string of hex nibbles to a sequence of bytes""" |
| 34 | return bytearray.fromhex(s) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 35 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 36 | def b2h(b:bytearray) -> Hexstr: |
Harald Welte | 4f6ca43 | 2021-02-01 17:51:56 +0100 | [diff] [blame] | 37 | """convert from a sequence of bytes to a string of hex nibbles""" |
| 38 | return ''.join(['%02x'%(x) for x in b]) |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 39 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 40 | def h2i(s:Hexstr) -> List[int]: |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 41 | """convert from a string of hex nibbles to a list of integers""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 42 | return [(int(x,16)<<4)+int(y,16) for x,y in zip(s[0::2], s[1::2])] |
| 43 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 44 | def i2h(s:List[int]) -> Hexstr: |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 45 | """convert from a list of integers to a string of hex nibbles""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 46 | return ''.join(['%02x'%(x) for x in s]) |
| 47 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 48 | def h2s(s:Hexstr) -> str: |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 49 | """convert from a string of hex nibbles to an ASCII string""" |
Vadim Yanitskiy | eb06b45 | 2020-05-10 02:32:46 +0700 | [diff] [blame] | 50 | return ''.join([chr((int(x,16)<<4)+int(y,16)) for x,y in zip(s[0::2], s[1::2]) |
| 51 | if int(x + y, 16) != 0xff]) |
Alexander Chemeris | a5f0ea6 | 2017-07-18 16:48:47 +0300 | [diff] [blame] | 52 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 53 | def s2h(s:str) -> Hexstr: |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 54 | """convert from an ASCII string to a string of hex nibbles""" |
Harald Welte | 4f6ca43 | 2021-02-01 17:51:56 +0100 | [diff] [blame] | 55 | b = bytearray() |
| 56 | b.extend(map(ord, s)) |
| 57 | return b2h(b) |
Alexander Chemeris | a5f0ea6 | 2017-07-18 16:48:47 +0300 | [diff] [blame] | 58 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 59 | def i2s(s:List[int]) -> str: |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 60 | """convert from a list of integers to an ASCII string""" |
Supreeth Herle | 7d77d2d | 2020-05-11 09:07:08 +0200 | [diff] [blame] | 61 | return ''.join([chr(x) for x in s]) |
| 62 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 63 | def swap_nibbles(s:Hexstr) -> Hexstr: |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 64 | """swap the nibbles in a hex string""" |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 65 | return ''.join([x+y for x,y in zip(s[1::2], s[0::2])]) |
| 66 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 67 | def rpad(s:str, l:int, c='f') -> str: |
| 68 | """pad string on the right side. |
| 69 | Args: |
| 70 | s : string to pad |
| 71 | l : total length to pad to |
| 72 | c : padding character |
| 73 | Returns: |
| 74 | String 's' padded with as many 'c' as needed to reach total length of 'l' |
| 75 | """ |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 76 | return s + c * (l - len(s)) |
| 77 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 78 | def lpad(s:str, l:int, c='f') -> str: |
| 79 | """pad string on the left side. |
| 80 | Args: |
| 81 | s : string to pad |
| 82 | l : total length to pad to |
| 83 | c : padding character |
| 84 | Returns: |
| 85 | String 's' padded with as many 'c' as needed to reach total length of 'l' |
| 86 | """ |
Sylvain Munaut | 76504e0 | 2010-12-07 00:24:32 +0100 | [diff] [blame] | 87 | return c * (l - len(s)) + s |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 88 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 89 | def half_round_up(n:int) -> int: |
Ben Fox-Moore | 0ec1475 | 2018-09-24 15:47:02 +0200 | [diff] [blame] | 90 | return (n + 1)//2 |
| 91 | |
Philipp Maier | 796ca3d | 2021-11-01 17:13:57 +0100 | [diff] [blame] | 92 | def str_sanitize(s:str) -> str: |
| 93 | """replace all non printable chars, line breaks and whitespaces, with ' ', make sure that |
| 94 | there are no whitespaces at the end and at the beginning of the string. |
| 95 | |
| 96 | Args: |
| 97 | s : string to sanitize |
| 98 | Returns: |
| 99 | filtered result of string 's' |
| 100 | """ |
| 101 | |
| 102 | chars_to_keep = string.digits + string.ascii_letters + string.punctuation |
| 103 | res = ''.join([c if c in chars_to_keep else ' ' for c in s]) |
| 104 | return res.strip() |
| 105 | |
Harald Welte | 917d98c | 2021-04-21 11:51:25 +0200 | [diff] [blame] | 106 | ######################################################################### |
Harald Welte | 9f3b44d | 2021-05-04 18:18:09 +0200 | [diff] [blame] | 107 | # poor man's COMPREHENSION-TLV decoder. |
| 108 | ######################################################################### |
| 109 | |
Harald Welte | 6912b1b | 2021-05-24 23:16:44 +0200 | [diff] [blame] | 110 | def comprehensiontlv_parse_tag_raw(binary:bytes) -> Tuple[int, bytes]: |
| 111 | """Parse a single Tag according to ETSI TS 101 220 Section 7.1.1""" |
| 112 | if binary[0] in [0x00, 0x80, 0xff]: |
| 113 | raise ValueError("Found illegal value 0x%02x in %s" % (binary[0], binary)) |
| 114 | if binary[0] == 0x7f: |
| 115 | # three-byte tag |
| 116 | tag = binary[0] << 16 | binary[1] << 8 | binary[2] |
| 117 | return (tag, binary[3:]) |
| 118 | elif binary[0] == 0xff: |
| 119 | return None, binary |
| 120 | else: |
| 121 | # single byte tag |
| 122 | tag = binary[0] |
| 123 | return (tag, binary[1:]) |
| 124 | |
Harald Welte | 9f3b44d | 2021-05-04 18:18:09 +0200 | [diff] [blame] | 125 | def comprehensiontlv_parse_tag(binary:bytes) -> Tuple[dict, bytes]: |
| 126 | """Parse a single Tag according to ETSI TS 101 220 Section 7.1.1""" |
| 127 | if binary[0] in [0x00, 0x80, 0xff]: |
| 128 | raise ValueError("Found illegal value 0x%02x in %s" % (binary[0], binary)) |
| 129 | if binary[0] == 0x7f: |
| 130 | # three-byte tag |
| 131 | tag = (binary[1] & 0x7f) << 8 |
| 132 | tag |= binary[2] |
| 133 | compr = True if binary[1] & 0x80 else False |
| 134 | return ({'comprehension': compr, 'tag': tag}, binary[3:]) |
| 135 | else: |
| 136 | # single byte tag |
| 137 | tag = binary[0] & 0x7f |
| 138 | compr = True if binary[0] & 0x80 else False |
| 139 | return ({'comprehension': compr, 'tag': tag}, binary[1:]) |
| 140 | |
| 141 | def comprehensiontlv_encode_tag(tag) -> bytes: |
| 142 | """Encode a single Tag according to ETSI TS 101 220 Section 7.1.1""" |
| 143 | # permit caller to specify tag also as integer value |
| 144 | if isinstance(tag, int): |
| 145 | compr = True if tag < 0xff and tag & 0x80 else False |
| 146 | tag = {'tag': tag, 'comprehension': compr} |
| 147 | compr = tag.get('comprehension', False) |
| 148 | if tag['tag'] in [0x00, 0x80, 0xff] or tag['tag'] > 0xff: |
| 149 | # 3-byte format |
Vadim Yanitskiy | dbd5ed6 | 2021-11-05 16:20:52 +0300 | [diff] [blame] | 150 | byte3 = tag['tag'] & 0xff |
Harald Welte | 9f3b44d | 2021-05-04 18:18:09 +0200 | [diff] [blame] | 151 | byte2 = (tag['tag'] >> 8) & 0x7f |
| 152 | if compr: |
| 153 | byte2 |= 0x80 |
| 154 | return b'\x7f' + byte2.to_bytes(1, 'big') + byte3.to_bytes(1, 'big') |
| 155 | else: |
| 156 | # 1-byte format |
| 157 | ret = tag['tag'] |
| 158 | if compr: |
| 159 | ret |= 0x80 |
| 160 | return ret.to_bytes(1, 'big') |
| 161 | |
| 162 | # length value coding is equal to BER-TLV |
| 163 | |
Harald Welte | 6912b1b | 2021-05-24 23:16:44 +0200 | [diff] [blame] | 164 | def comprehensiontlv_parse_one(binary:bytes) -> (dict, int, bytes, bytes): |
| 165 | """Parse a single TLV IE at the start of the given binary data. |
| 166 | Args: |
| 167 | binary : binary input data of BER-TLV length field |
| 168 | Returns: |
| 169 | Tuple of (tag:dict, len:int, remainder:bytes) |
| 170 | """ |
| 171 | (tagdict, remainder) = comprehensiontlv_parse_tag(binary) |
| 172 | (length, remainder) = bertlv_parse_len(remainder) |
| 173 | value = remainder[:length] |
| 174 | remainder = remainder[length:] |
| 175 | return (tagdict, length, value, remainder) |
| 176 | |
| 177 | |
Harald Welte | 9f3b44d | 2021-05-04 18:18:09 +0200 | [diff] [blame] | 178 | |
| 179 | ######################################################################### |
Harald Welte | 917d98c | 2021-04-21 11:51:25 +0200 | [diff] [blame] | 180 | # poor man's BER-TLV decoder. To be a more sophisticated OO library later |
| 181 | ######################################################################### |
| 182 | |
Harald Welte | 6912b1b | 2021-05-24 23:16:44 +0200 | [diff] [blame] | 183 | def bertlv_parse_tag_raw(binary:bytes) -> Tuple[int, bytes]: |
| 184 | """Get a single raw Tag from start of input according to ITU-T X.690 8.1.2 |
| 185 | Args: |
| 186 | binary : binary input data of BER-TLV length field |
| 187 | Returns: |
| 188 | Tuple of (tag:int, remainder:bytes) |
| 189 | """ |
Harald Welte | 9a75410 | 2021-10-21 13:46:59 +0200 | [diff] [blame] | 190 | # check for FF padding at the end, as customary in SIM card files |
| 191 | if binary[0] == 0xff and len(binary) == 1 or binary[0] == 0xff and binary[1] == 0xff: |
Harald Welte | 6912b1b | 2021-05-24 23:16:44 +0200 | [diff] [blame] | 192 | return None, binary |
| 193 | tag = binary[0] & 0x1f |
| 194 | if tag <= 30: |
| 195 | return binary[0], binary[1:] |
| 196 | else: # multi-byte tag |
| 197 | tag = binary[0] |
| 198 | i = 1 |
| 199 | last = False |
| 200 | while not last: |
| 201 | last = False if binary[i] & 0x80 else True |
| 202 | tag <<= 8 |
| 203 | tag |= binary[i] |
| 204 | i += 1 |
| 205 | return tag, binary[i:] |
| 206 | |
Harald Welte | 917d98c | 2021-04-21 11:51:25 +0200 | [diff] [blame] | 207 | def bertlv_parse_tag(binary:bytes) -> Tuple[dict, bytes]: |
| 208 | """Parse a single Tag value according to ITU-T X.690 8.1.2 |
| 209 | Args: |
| 210 | binary : binary input data of BER-TLV length field |
| 211 | Returns: |
| 212 | Tuple of ({class:int, constructed:bool, tag:int}, remainder:bytes) |
| 213 | """ |
| 214 | cls = binary[0] >> 6 |
| 215 | constructed = True if binary[0] & 0x20 else False |
| 216 | tag = binary[0] & 0x1f |
| 217 | if tag <= 30: |
| 218 | return ({'class':cls, 'constructed':constructed, 'tag': tag}, binary[1:]) |
| 219 | else: # multi-byte tag |
| 220 | tag = 0 |
| 221 | i = 1 |
| 222 | last = False |
| 223 | while not last: |
| 224 | last = False if binary[i] & 0x80 else True |
| 225 | tag <<= 7 |
| 226 | tag |= binary[i] & 0x7f |
| 227 | i += 1 |
| 228 | return ({'class':cls, 'constructed':constructed, 'tag':tag}, binary[i:]) |
| 229 | |
Harald Welte | f0885b1 | 2021-05-24 23:18:59 +0200 | [diff] [blame] | 230 | def bertlv_encode_tag(t) -> bytes: |
| 231 | """Encode a single Tag value according to ITU-T X.690 8.1.2 |
| 232 | """ |
| 233 | def get_top7_bits(inp:int) -> Tuple[int, int]: |
| 234 | """Get top 7 bits of integer. Returns those 7 bits as integer and the remaining LSBs.""" |
| 235 | remain_bits = inp.bit_length() |
| 236 | if remain_bits >= 7: |
| 237 | bitcnt = 7 |
| 238 | else: |
| 239 | bitcnt = remain_bits |
| 240 | outp = inp >> (remain_bits - bitcnt) |
| 241 | remainder = inp & ~ (inp << (remain_bits - bitcnt)) |
| 242 | return outp, remainder |
| 243 | |
| 244 | if isinstance(t, int): |
| 245 | # FIXME: multiple byte tags |
| 246 | tag = t & 0x1f |
| 247 | constructed = True if t & 0x20 else False |
| 248 | cls = t >> 6 |
| 249 | else: |
| 250 | tag = t['tag'] |
| 251 | constructed = t['constructed'] |
| 252 | cls = t['class'] |
| 253 | if tag <= 30: |
| 254 | t = tag & 0x1f |
| 255 | if constructed: |
| 256 | t |= 0x20 |
| 257 | t |= (cls & 3) << 6 |
| 258 | return bytes([t]) |
| 259 | else: # multi-byte tag |
Vadim Yanitskiy | dbd5ed6 | 2021-11-05 16:20:52 +0300 | [diff] [blame] | 260 | t = 0x1f |
Harald Welte | f0885b1 | 2021-05-24 23:18:59 +0200 | [diff] [blame] | 261 | if constructed: |
| 262 | t |= 0x20 |
| 263 | t |= (cls & 3) << 6 |
| 264 | tag_bytes = bytes([t]) |
| 265 | remain = tag |
| 266 | while True: |
| 267 | t, remain = get_top7_bits(remain) |
| 268 | if remain: |
| 269 | t |= 0x80 |
| 270 | tag_bytes += bytes([t]) |
| 271 | if not remain: |
| 272 | break |
| 273 | return tag_bytes |
| 274 | |
Harald Welte | 917d98c | 2021-04-21 11:51:25 +0200 | [diff] [blame] | 275 | def bertlv_parse_len(binary:bytes) -> Tuple[int, bytes]: |
| 276 | """Parse a single Length value according to ITU-T X.690 8.1.3; |
| 277 | only the definite form is supported here. |
| 278 | Args: |
| 279 | binary : binary input data of BER-TLV length field |
| 280 | Returns: |
| 281 | Tuple of (length, remainder) |
| 282 | """ |
| 283 | if binary[0] < 0x80: |
| 284 | return (binary[0], binary[1:]) |
| 285 | else: |
| 286 | num_len_oct = binary[0] & 0x7f |
| 287 | length = 0 |
| 288 | for i in range(1, 1+num_len_oct): |
| 289 | length <<= 8 |
| 290 | length |= binary[i] |
Harald Welte | de02718 | 2021-05-04 20:06:45 +0200 | [diff] [blame] | 291 | return (length, binary[1+num_len_oct:]) |
Harald Welte | 917d98c | 2021-04-21 11:51:25 +0200 | [diff] [blame] | 292 | |
| 293 | def bertlv_encode_len(length:int) -> bytes: |
| 294 | """Encode a single Length value according to ITU-T X.690 8.1.3; |
| 295 | only the definite form is supported here. |
| 296 | Args: |
| 297 | length : length value to be encoded |
| 298 | Returns: |
| 299 | binary output data of BER-TLV length field |
| 300 | """ |
| 301 | if length < 0x80: |
| 302 | return length.to_bytes(1, 'big') |
| 303 | elif length <= 0xff: |
| 304 | return b'\x81' + length.to_bytes(1, 'big') |
| 305 | elif length <= 0xffff: |
| 306 | return b'\x82' + length.to_bytes(2, 'big') |
| 307 | elif length <= 0xffffff: |
| 308 | return b'\x83' + length.to_bytes(3, 'big') |
| 309 | elif length <= 0xffffffff: |
| 310 | return b'\x84' + length.to_bytes(4, 'big') |
| 311 | else: |
| 312 | raise ValueError("Length > 32bits not supported") |
| 313 | |
Harald Welte | c147530 | 2021-05-21 21:47:55 +0200 | [diff] [blame] | 314 | def bertlv_parse_one(binary:bytes) -> (dict, int, bytes, bytes): |
Harald Welte | 917d98c | 2021-04-21 11:51:25 +0200 | [diff] [blame] | 315 | """Parse a single TLV IE at the start of the given binary data. |
| 316 | Args: |
| 317 | binary : binary input data of BER-TLV length field |
| 318 | Returns: |
| 319 | Tuple of (tag:dict, len:int, remainder:bytes) |
| 320 | """ |
| 321 | (tagdict, remainder) = bertlv_parse_tag(binary) |
| 322 | (length, remainder) = bertlv_parse_len(remainder) |
Harald Welte | c147530 | 2021-05-21 21:47:55 +0200 | [diff] [blame] | 323 | value = remainder[:length] |
| 324 | remainder = remainder[length:] |
| 325 | return (tagdict, length, value, remainder) |
Harald Welte | 917d98c | 2021-04-21 11:51:25 +0200 | [diff] [blame] | 326 | |
| 327 | |
| 328 | |
Ben Fox-Moore | 0ec1475 | 2018-09-24 15:47:02 +0200 | [diff] [blame] | 329 | # IMSI encoded format: |
| 330 | # For IMSI 0123456789ABCDE: |
| 331 | # |
| 332 | # | byte 1 | 2 upper | 2 lower | 3 upper | 3 lower | ... | 9 upper | 9 lower | |
| 333 | # | length in bytes | 0 | odd/even | 2 | 1 | ... | E | D | |
| 334 | # |
| 335 | # If the IMSI is less than 15 characters, it should be padded with 'f' from the end. |
| 336 | # |
| 337 | # The length is the total number of bytes used to encoded the IMSI. This includes the odd/even |
| 338 | # parity bit. E.g. an IMSI of length 14 is 8 bytes long, not 7, as it uses bytes 2 to 9 to |
| 339 | # encode itself. |
| 340 | # |
| 341 | # Because of this, an odd length IMSI fits exactly into len(imsi) + 1 // 2 bytes, whereas an |
| 342 | # even length IMSI only uses half of the last byte. |
| 343 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 344 | def enc_imsi(imsi:str): |
| 345 | """Converts a string IMSI into the encoded value of the EF""" |
Ben Fox-Moore | 0ec1475 | 2018-09-24 15:47:02 +0200 | [diff] [blame] | 346 | l = half_round_up(len(imsi) + 1) # Required bytes - include space for odd/even indicator |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 347 | oe = len(imsi) & 1 # Odd (1) / Even (0) |
Ben Fox-Moore | 0ec1475 | 2018-09-24 15:47:02 +0200 | [diff] [blame] | 348 | ei = '%02x' % l + swap_nibbles('%01x%s' % ((oe<<3)|1, rpad(imsi, 15))) |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 349 | return ei |
| 350 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 351 | def dec_imsi(ef:Hexstr) -> Optional[str]: |
Harald Welte | c9cdce3 | 2021-04-11 10:28:28 +0200 | [diff] [blame] | 352 | """Converts an EF value to the IMSI string representation""" |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 353 | if len(ef) < 4: |
| 354 | return None |
Pau Espin Pedrol | 665bd22 | 2017-12-29 20:30:35 +0100 | [diff] [blame] | 355 | l = int(ef[0:2], 16) * 2 # Length of the IMSI string |
Ben Fox-Moore | 0ec1475 | 2018-09-24 15:47:02 +0200 | [diff] [blame] | 356 | l = l - 1 # Encoded length byte includes oe nibble |
| 357 | swapped = swap_nibbles(ef[2:]).rstrip('f') |
Philipp Maier | cd3d626 | 2020-05-11 21:41:56 +0200 | [diff] [blame] | 358 | if len(swapped) < 1: |
| 359 | return None |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 360 | oe = (int(swapped[0])>>3) & 1 # Odd (1) / Even (0) |
Ben Fox-Moore | 0ec1475 | 2018-09-24 15:47:02 +0200 | [diff] [blame] | 361 | if not oe: |
| 362 | # if even, only half of last byte was used |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 363 | l = l-1 |
Ben Fox-Moore | 0ec1475 | 2018-09-24 15:47:02 +0200 | [diff] [blame] | 364 | if l != len(swapped) - 1: |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 365 | return None |
Ben Fox-Moore | 0ec1475 | 2018-09-24 15:47:02 +0200 | [diff] [blame] | 366 | imsi = swapped[1:] |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 367 | return imsi |
| 368 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 369 | def dec_iccid(ef:Hexstr) -> str: |
Alexander Chemeris | 5e96c3d | 2013-07-04 17:33:33 +0400 | [diff] [blame] | 370 | return swap_nibbles(ef).strip('f') |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 371 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 372 | def enc_iccid(iccid:str) -> Hexstr: |
Alexander Chemeris | 7be92ff | 2013-07-10 11:18:06 +0400 | [diff] [blame] | 373 | return swap_nibbles(rpad(iccid, 20)) |
| 374 | |
Philipp Maier | e6f8d68 | 2021-04-23 21:14:41 +0200 | [diff] [blame] | 375 | def enc_plmn(mcc:Hexstr, mnc:Hexstr) -> Hexstr: |
Alexander Chemeris | dddbf52 | 2017-07-18 16:49:59 +0300 | [diff] [blame] | 376 | """Converts integer MCC/MNC into 3 bytes for EF""" |
Philipp Maier | 6c5cd80 | 2021-04-23 21:19:36 +0200 | [diff] [blame] | 377 | |
| 378 | # Make sure there are no excess whitespaces in the input |
| 379 | # parameters |
| 380 | mcc = mcc.strip() |
| 381 | mnc = mnc.strip() |
| 382 | |
| 383 | # Make sure that MCC/MNC are correctly padded with leading |
| 384 | # zeros or 'F', depending on the length. |
| 385 | if len(mnc) == 0: |
| 386 | mnc = "FFF" |
| 387 | elif len(mnc) == 1: |
| 388 | mnc = "F0" + mnc |
| 389 | elif len(mnc) == 2: |
| 390 | mnc += "F" |
| 391 | |
| 392 | if len(mcc) == 0: |
| 393 | mcc = "FFF" |
| 394 | elif len(mcc) == 1: |
| 395 | mcc = "00" + mcc |
| 396 | elif len(mcc) == 2: |
| 397 | mcc = "0" + mcc |
| 398 | |
Vadim Yanitskiy | c8458e2 | 2021-03-12 00:34:10 +0100 | [diff] [blame] | 399 | return (mcc[1] + mcc[0]) + (mnc[2] + mcc[2]) + (mnc[1] + mnc[0]) |
Alexander Chemeris | a5f0ea6 | 2017-07-18 16:48:47 +0300 | [diff] [blame] | 400 | |
Harald Welte | d7a7e17 | 2021-04-07 00:30:10 +0200 | [diff] [blame] | 401 | def dec_plmn(threehexbytes:Hexstr) -> dict: |
Philipp Maier | 6c5cd80 | 2021-04-23 21:19:36 +0200 | [diff] [blame] | 402 | res = {'mcc': "0", 'mnc': "0" } |
| 403 | dec_mcc_from_plmn_str(threehexbytes) |
| 404 | res['mcc'] = dec_mcc_from_plmn_str(threehexbytes) |
| 405 | res['mnc'] = dec_mnc_from_plmn_str(threehexbytes) |
Harald Welte | d7a7e17 | 2021-04-07 00:30:10 +0200 | [diff] [blame] | 406 | return res |
| 407 | |
Alexander Chemeris | a5f0ea6 | 2017-07-18 16:48:47 +0300 | [diff] [blame] | 408 | def dec_spn(ef): |
Robert Falkenberg | b07a3e9 | 2021-05-07 15:23:20 +0200 | [diff] [blame] | 409 | """Obsolete, kept for API compatibility""" |
| 410 | from ts_51_011 import EF_SPN |
| 411 | abstract_data = EF_SPN().decode_hex(ef) |
| 412 | show_in_hplmn = abstract_data['show_in_hplmn'] |
| 413 | hide_in_oplmn = abstract_data['hide_in_oplmn'] |
| 414 | name = abstract_data['spn'] |
| 415 | return (name, show_in_hplmn, hide_in_oplmn) |
Alexander Chemeris | a5f0ea6 | 2017-07-18 16:48:47 +0300 | [diff] [blame] | 416 | |
Robert Falkenberg | b07a3e9 | 2021-05-07 15:23:20 +0200 | [diff] [blame] | 417 | def enc_spn(name:str, show_in_hplmn=False, hide_in_oplmn=False): |
| 418 | """Obsolete, kept for API compatibility""" |
| 419 | from ts_51_011 import EF_SPN |
| 420 | abstract_data = { |
| 421 | 'hide_in_oplmn' : hide_in_oplmn, |
| 422 | 'show_in_hplmn' : show_in_hplmn, |
| 423 | 'spn' : name, |
| 424 | } |
| 425 | return EF_SPN().encode_hex(abstract_data) |
Alexander Chemeris | 19fffa1 | 2018-01-11 13:06:43 +0900 | [diff] [blame] | 426 | |
Supreeth Herle | f394853 | 2020-03-24 12:23:51 +0100 | [diff] [blame] | 427 | def hexstr_to_Nbytearr(s, nbytes): |
| 428 | return [s[i:i+(nbytes*2)] for i in range(0, len(s), (nbytes*2)) ] |
| 429 | |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 430 | # Accepts hex string representing three bytes |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 431 | def dec_mcc_from_plmn(plmn:Hexstr) -> int: |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 432 | ia = h2i(plmn) |
| 433 | digit1 = ia[0] & 0x0F # 1st byte, LSB |
| 434 | digit2 = (ia[0] & 0xF0) >> 4 # 1st byte, MSB |
| 435 | digit3 = ia[1] & 0x0F # 2nd byte, LSB |
| 436 | if digit3 == 0xF and digit2 == 0xF and digit1 == 0xF: |
| 437 | return 0xFFF # 4095 |
Supreeth Herle | d24f163 | 2019-11-30 10:37:09 +0100 | [diff] [blame] | 438 | return derive_mcc(digit1, digit2, digit3) |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 439 | |
Philipp Maier | 6c5cd80 | 2021-04-23 21:19:36 +0200 | [diff] [blame] | 440 | def dec_mcc_from_plmn_str(plmn:Hexstr) -> str: |
| 441 | digit1 = plmn[1] # 1st byte, LSB |
| 442 | digit2 = plmn[0] # 1st byte, MSB |
| 443 | digit3 = plmn[3] # 2nd byte, LSB |
| 444 | res = digit1 + digit2 + digit3 |
| 445 | return res.upper().strip("F") |
| 446 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 447 | def dec_mnc_from_plmn(plmn:Hexstr) -> int: |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 448 | ia = h2i(plmn) |
Vadim Yanitskiy | b271be3 | 2021-03-11 23:56:58 +0100 | [diff] [blame] | 449 | digit1 = ia[2] & 0x0F # 3rd byte, LSB |
| 450 | digit2 = (ia[2] & 0xF0) >> 4 # 3rd byte, MSB |
| 451 | digit3 = (ia[1] & 0xF0) >> 4 # 2nd byte, MSB |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 452 | if digit3 == 0xF and digit2 == 0xF and digit1 == 0xF: |
| 453 | return 0xFFF # 4095 |
Supreeth Herle | d24f163 | 2019-11-30 10:37:09 +0100 | [diff] [blame] | 454 | return derive_mnc(digit1, digit2, digit3) |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 455 | |
Philipp Maier | 6c5cd80 | 2021-04-23 21:19:36 +0200 | [diff] [blame] | 456 | def dec_mnc_from_plmn_str(plmn:Hexstr) -> str: |
| 457 | digit1 = plmn[5] # 3rd byte, LSB |
| 458 | digit2 = plmn[4] # 3rd byte, MSB |
| 459 | digit3 = plmn[2] # 2nd byte, MSB |
| 460 | res = digit1 + digit2 + digit3 |
| 461 | return res.upper().strip("F") |
| 462 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 463 | def dec_act(twohexbytes:Hexstr) -> List[str]: |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 464 | act_list = [ |
| 465 | {'bit': 15, 'name': "UTRAN"}, |
| 466 | {'bit': 14, 'name': "E-UTRAN"}, |
Bjoern Riemer | ffee89a | 2022-01-20 22:05:22 +0100 | [diff] [blame^] | 467 | {'bit': 11, 'name': "NG-RAN"}, |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 468 | {'bit': 7, 'name': "GSM"}, |
| 469 | {'bit': 6, 'name': "GSM COMPACT"}, |
| 470 | {'bit': 5, 'name': "cdma2000 HRPD"}, |
| 471 | {'bit': 4, 'name': "cdma2000 1xRTT"}, |
| 472 | ] |
| 473 | ia = h2i(twohexbytes) |
| 474 | u16t = (ia[0] << 8)|ia[1] |
| 475 | sel = [] |
| 476 | for a in act_list: |
| 477 | if u16t & (1 << a['bit']): |
Philipp Maier | e7d4179 | 2021-04-29 16:20:07 +0200 | [diff] [blame] | 478 | if a['name'] == "E-UTRAN": |
| 479 | # The Access technology identifier of E-UTRAN |
| 480 | # allows a more detailed specification: |
| 481 | if u16t & (1 << 13) and u16t & (1 << 12): |
| 482 | sel.append("E-UTRAN WB-S1") |
| 483 | sel.append("E-UTRAN NB-S1") |
| 484 | elif u16t & (1 << 13): |
| 485 | sel.append("E-UTRAN WB-S1") |
| 486 | elif u16t & (1 << 12): |
| 487 | sel.append("E-UTRAN NB-S1") |
| 488 | else: |
| 489 | sel.append("E-UTRAN") |
| 490 | else: |
| 491 | sel.append(a['name']) |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 492 | return sel |
| 493 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 494 | def dec_xplmn_w_act(fivehexbytes:Hexstr) -> Dict[str,Any]: |
Philipp Maier | b919f8b | 2021-04-27 18:28:27 +0200 | [diff] [blame] | 495 | res = {'mcc': "0", 'mnc': "0", 'act': []} |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 496 | plmn_chars = 6 |
| 497 | act_chars = 4 |
| 498 | plmn_str = fivehexbytes[:plmn_chars] # first three bytes (six ascii hex chars) |
| 499 | act_str = fivehexbytes[plmn_chars:plmn_chars + act_chars] # two bytes after first three bytes |
Philipp Maier | b919f8b | 2021-04-27 18:28:27 +0200 | [diff] [blame] | 500 | res['mcc'] = dec_mcc_from_plmn_str(plmn_str) |
| 501 | res['mnc'] = dec_mnc_from_plmn_str(plmn_str) |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 502 | res['act'] = dec_act(act_str) |
| 503 | return res |
| 504 | |
| 505 | def format_xplmn_w_act(hexstr): |
| 506 | s = "" |
herlesupreeth | 45fa604 | 2020-09-18 15:32:20 +0200 | [diff] [blame] | 507 | for rec_data in hexstr_to_Nbytearr(hexstr, 5): |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 508 | rec_info = dec_xplmn_w_act(rec_data) |
Philipp Maier | b919f8b | 2021-04-27 18:28:27 +0200 | [diff] [blame] | 509 | if rec_info['mcc'] == "" and rec_info['mnc'] == "": |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 510 | rec_str = "unused" |
| 511 | else: |
Philipp Maier | b919f8b | 2021-04-27 18:28:27 +0200 | [diff] [blame] | 512 | rec_str = "MCC: %s MNC: %s AcT: %s" % (rec_info['mcc'], rec_info['mnc'], ", ".join(rec_info['act'])) |
Daniel Laszlo Sitzer | 851e9c0 | 2018-12-04 19:40:08 +0100 | [diff] [blame] | 513 | s += "\t%s # %s\n" % (rec_data, rec_str) |
| 514 | return s |
| 515 | |
Sebastian Viviani | e61170c | 2020-06-03 08:57:00 +0100 | [diff] [blame] | 516 | def dec_loci(hexstr): |
| 517 | res = {'tmsi': '', 'mcc': 0, 'mnc': 0, 'lac': '', 'status': 0} |
| 518 | res['tmsi'] = hexstr[:8] |
| 519 | res['mcc'] = dec_mcc_from_plmn(hexstr[8:14]) |
| 520 | res['mnc'] = dec_mnc_from_plmn(hexstr[8:14]) |
| 521 | res['lac'] = hexstr[14:18] |
| 522 | res['status'] = h2i(hexstr[20:22]) |
| 523 | return res |
| 524 | |
| 525 | def dec_psloci(hexstr): |
| 526 | res = {'p-tmsi': '', 'p-tmsi-sig': '', 'mcc': 0, 'mnc': 0, 'lac': '', 'rac': '', 'status': 0} |
| 527 | res['p-tmsi'] = hexstr[:8] |
| 528 | res['p-tmsi-sig'] = hexstr[8:14] |
| 529 | res['mcc'] = dec_mcc_from_plmn(hexstr[14:20]) |
| 530 | res['mnc'] = dec_mnc_from_plmn(hexstr[14:20]) |
| 531 | res['lac'] = hexstr[20:24] |
| 532 | res['rac'] = hexstr[24:26] |
| 533 | res['status'] = h2i(hexstr[26:28]) |
| 534 | return res |
| 535 | |
| 536 | def dec_epsloci(hexstr): |
| 537 | res = {'guti': '', 'mcc': 0, 'mnc': 0, 'tac': '', 'status': 0} |
| 538 | res['guti'] = hexstr[:24] |
| 539 | res['tai'] = hexstr[24:34] |
| 540 | res['mcc'] = dec_mcc_from_plmn(hexstr[24:30]) |
| 541 | res['mnc'] = dec_mnc_from_plmn(hexstr[24:30]) |
| 542 | res['tac'] = hexstr[30:34] |
| 543 | res['status'] = h2i(hexstr[34:36]) |
| 544 | return res |
| 545 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 546 | def dec_xplmn(threehexbytes:Hexstr) -> dict: |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 547 | res = {'mcc': 0, 'mnc': 0, 'act': []} |
| 548 | plmn_chars = 6 |
| 549 | plmn_str = threehexbytes[:plmn_chars] # first three bytes (six ascii hex chars) |
| 550 | res['mcc'] = dec_mcc_from_plmn(plmn_str) |
| 551 | res['mnc'] = dec_mnc_from_plmn(plmn_str) |
| 552 | return res |
| 553 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 554 | def format_xplmn(hexstr:Hexstr) -> str: |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 555 | s = "" |
herlesupreeth | 45fa604 | 2020-09-18 15:32:20 +0200 | [diff] [blame] | 556 | for rec_data in hexstr_to_Nbytearr(hexstr, 3): |
Harald Welte | ca67394 | 2020-06-03 15:19:40 +0200 | [diff] [blame] | 557 | rec_info = dec_xplmn(rec_data) |
| 558 | if rec_info['mcc'] == 0xFFF and rec_info['mnc'] == 0xFFF: |
| 559 | rec_str = "unused" |
| 560 | else: |
| 561 | rec_str = "MCC: %03d MNC: %03d" % (rec_info['mcc'], rec_info['mnc']) |
| 562 | s += "\t%s # %s\n" % (rec_data, rec_str) |
| 563 | return s |
| 564 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 565 | def derive_milenage_opc(ki_hex:Hexstr, op_hex:Hexstr) -> Hexstr: |
Alexander Chemeris | 19fffa1 | 2018-01-11 13:06:43 +0900 | [diff] [blame] | 566 | """ |
| 567 | Run the milenage algorithm to calculate OPC from Ki and OP |
| 568 | """ |
| 569 | from Crypto.Cipher import AES |
| 570 | from Crypto.Util.strxor import strxor |
| 571 | from pySim.utils import b2h |
| 572 | |
| 573 | # We pass in hex string and now need to work on bytes |
Harald Welte | eab8d2a | 2021-03-05 18:30:23 +0100 | [diff] [blame] | 574 | ki_bytes = bytes(h2b(ki_hex)) |
| 575 | op_bytes = bytes(h2b(op_hex)) |
Harald Welte | ab34fa8 | 2021-03-05 18:39:59 +0100 | [diff] [blame] | 576 | aes = AES.new(ki_bytes, AES.MODE_ECB) |
Harald Welte | eab8d2a | 2021-03-05 18:30:23 +0100 | [diff] [blame] | 577 | opc_bytes = aes.encrypt(op_bytes) |
| 578 | return b2h(strxor(opc_bytes, op_bytes)) |
Alexander Chemeris | 19fffa1 | 2018-01-11 13:06:43 +0900 | [diff] [blame] | 579 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 580 | def calculate_luhn(cc) -> int: |
Alexander Chemeris | 19fffa1 | 2018-01-11 13:06:43 +0900 | [diff] [blame] | 581 | """ |
| 582 | Calculate Luhn checksum used in e.g. ICCID and IMEI |
| 583 | """ |
Daniel Willmann | dd014ea | 2020-10-19 10:35:11 +0200 | [diff] [blame] | 584 | num = list(map(int, str(cc))) |
Alexander Chemeris | 19fffa1 | 2018-01-11 13:06:43 +0900 | [diff] [blame] | 585 | check_digit = 10 - sum(num[-2::-2] + [sum(divmod(d * 2, 10)) for d in num[::-2]]) % 10 |
| 586 | return 0 if check_digit == 10 else check_digit |
Philipp Maier | 7592eee | 2019-09-12 13:03:23 +0200 | [diff] [blame] | 587 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 588 | def mcc_from_imsi(imsi:str) -> Optional[str]: |
Philipp Maier | 7592eee | 2019-09-12 13:03:23 +0200 | [diff] [blame] | 589 | """ |
| 590 | Derive the MCC (Mobile Country Code) from the first three digits of an IMSI |
| 591 | """ |
| 592 | if imsi == None: |
| 593 | return None |
| 594 | |
| 595 | if len(imsi) > 3: |
| 596 | return imsi[:3] |
| 597 | else: |
| 598 | return None |
| 599 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 600 | def mnc_from_imsi(imsi:str, long:bool=False) -> Optional[str]: |
Philipp Maier | 7592eee | 2019-09-12 13:03:23 +0200 | [diff] [blame] | 601 | """ |
| 602 | Derive the MNC (Mobile Country Code) from the 4th to 6th digit of an IMSI |
| 603 | """ |
| 604 | if imsi == None: |
| 605 | return None |
| 606 | |
| 607 | if len(imsi) > 3: |
| 608 | if long: |
| 609 | return imsi[3:6] |
| 610 | else: |
| 611 | return imsi[3:5] |
| 612 | else: |
| 613 | return None |
Supreeth Herle | d24f163 | 2019-11-30 10:37:09 +0100 | [diff] [blame] | 614 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 615 | def derive_mcc(digit1:int, digit2:int, digit3:int) -> int: |
Supreeth Herle | d24f163 | 2019-11-30 10:37:09 +0100 | [diff] [blame] | 616 | """ |
| 617 | Derive decimal representation of the MCC (Mobile Country Code) |
| 618 | from three given digits. |
| 619 | """ |
| 620 | |
| 621 | mcc = 0 |
| 622 | |
| 623 | if digit1 != 0x0f: |
| 624 | mcc += digit1 * 100 |
| 625 | if digit2 != 0x0f: |
| 626 | mcc += digit2 * 10 |
| 627 | if digit3 != 0x0f: |
| 628 | mcc += digit3 |
| 629 | |
| 630 | return mcc |
| 631 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 632 | def derive_mnc(digit1:int, digit2:int, digit3:int=0x0f) -> int: |
Supreeth Herle | d24f163 | 2019-11-30 10:37:09 +0100 | [diff] [blame] | 633 | """ |
| 634 | Derive decimal representation of the MNC (Mobile Network Code) |
| 635 | from two or (optionally) three given digits. |
| 636 | """ |
| 637 | |
| 638 | mnc = 0 |
| 639 | |
| 640 | # 3-rd digit is optional for the MNC. If present |
| 641 | # the algorythm is the same as for the MCC. |
| 642 | if digit3 != 0x0f: |
| 643 | return derive_mcc(digit1, digit2, digit3) |
| 644 | |
| 645 | if digit1 != 0x0f: |
| 646 | mnc += digit1 * 10 |
| 647 | if digit2 != 0x0f: |
| 648 | mnc += digit2 |
| 649 | |
| 650 | return mnc |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 651 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 652 | def dec_msisdn(ef_msisdn:Hexstr) -> Optional[Tuple[int,int,Optional[str]]]: |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 653 | """ |
| 654 | Decode MSISDN from EF.MSISDN or EF.ADN (same structure). |
| 655 | See 3GPP TS 31.102, section 4.2.26 and 4.4.2.3. |
| 656 | """ |
| 657 | |
| 658 | # Convert from str to (kind of) 'bytes' |
| 659 | ef_msisdn = h2b(ef_msisdn) |
| 660 | |
| 661 | # Make sure mandatory fields are present |
| 662 | if len(ef_msisdn) < 14: |
| 663 | raise ValueError("EF.MSISDN is too short") |
| 664 | |
| 665 | # Skip optional Alpha Identifier |
| 666 | xlen = len(ef_msisdn) - 14 |
| 667 | msisdn_lhv = ef_msisdn[xlen:] |
| 668 | |
| 669 | # Parse the length (in bytes) of the BCD encoded number |
Harald Welte | 4f6ca43 | 2021-02-01 17:51:56 +0100 | [diff] [blame] | 670 | bcd_len = msisdn_lhv[0] |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 671 | # BCD length = length of dial num (max. 10 bytes) + 1 byte ToN and NPI |
| 672 | if bcd_len == 0xff: |
| 673 | return None |
| 674 | elif bcd_len > 11 or bcd_len < 1: |
| 675 | raise ValueError("Length of MSISDN (%d bytes) is out of range" % bcd_len) |
| 676 | |
| 677 | # Parse ToN / NPI |
Harald Welte | 4f6ca43 | 2021-02-01 17:51:56 +0100 | [diff] [blame] | 678 | ton = (msisdn_lhv[1] >> 4) & 0x07 |
| 679 | npi = msisdn_lhv[1] & 0x0f |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 680 | bcd_len -= 1 |
| 681 | |
| 682 | # No MSISDN? |
| 683 | if not bcd_len: |
| 684 | return (npi, ton, None) |
| 685 | |
| 686 | msisdn = swap_nibbles(b2h(msisdn_lhv[2:][:bcd_len])).rstrip('f') |
| 687 | # International number 10.5.118/3GPP TS 24.008 |
Vadim Yanitskiy | 7ba2428 | 2020-02-27 00:04:13 +0700 | [diff] [blame] | 688 | if ton == 0x01: |
Supreeth Herle | 4b1c763 | 2019-12-22 09:00:59 +0100 | [diff] [blame] | 689 | msisdn = '+' + msisdn |
| 690 | |
| 691 | return (npi, ton, msisdn) |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 692 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 693 | def enc_msisdn(msisdn:str, npi:int=0x01, ton:int=0x03) -> Hexstr: |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 694 | """ |
| 695 | Encode MSISDN as LHV so it can be stored to EF.MSISDN. |
Philipp Maier | b46cb3f | 2021-04-20 22:38:21 +0200 | [diff] [blame] | 696 | See 3GPP TS 31.102, section 4.2.26 and 4.4.2.3. (The result |
| 697 | will not contain the optional Alpha Identifier at the beginning.) |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 698 | |
| 699 | Default NPI / ToN values: |
| 700 | - NPI: ISDN / telephony numbering plan (E.164 / E.163), |
| 701 | - ToN: network specific or international number (if starts with '+'). |
| 702 | """ |
| 703 | |
Philipp Maier | b46cb3f | 2021-04-20 22:38:21 +0200 | [diff] [blame] | 704 | # If no MSISDN is supplied then encode the file contents as all "ff" |
| 705 | if msisdn == "" or msisdn == "+": |
| 706 | return "ff" * 14 |
| 707 | |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 708 | # Leading '+' indicates International Number |
| 709 | if msisdn[0] == '+': |
| 710 | msisdn = msisdn[1:] |
| 711 | ton = 0x01 |
| 712 | |
Philipp Maier | b46cb3f | 2021-04-20 22:38:21 +0200 | [diff] [blame] | 713 | # An MSISDN must not exceed 20 digits |
| 714 | if len(msisdn) > 20: |
| 715 | raise ValueError("msisdn must not be longer than 20 digits") |
| 716 | |
Supreeth Herle | 5a54101 | 2019-12-22 08:59:16 +0100 | [diff] [blame] | 717 | # Append 'f' padding if number of digits is odd |
| 718 | if len(msisdn) % 2 > 0: |
| 719 | msisdn += 'f' |
| 720 | |
| 721 | # BCD length also includes NPI/ToN header |
| 722 | bcd_len = len(msisdn) // 2 + 1 |
| 723 | npi_ton = (npi & 0x0f) | ((ton & 0x07) << 4) | 0x80 |
| 724 | bcd = rpad(swap_nibbles(msisdn), 10 * 2) # pad to 10 octets |
| 725 | |
Philipp Maier | b46cb3f | 2021-04-20 22:38:21 +0200 | [diff] [blame] | 726 | return ('%02x' % bcd_len) + ('%02x' % npi_ton) + bcd + ("ff" * 2) |
| 727 | |
Supreeth Herle | 441c4a7 | 2020-03-24 10:19:15 +0100 | [diff] [blame] | 728 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 729 | def dec_st(st, table="sim") -> str: |
Supreeth Herle | 0c4d82d | 2020-04-20 13:28:31 +0200 | [diff] [blame] | 730 | """ |
| 731 | Parses the EF S/U/IST and prints the list of available services in EF S/U/IST |
| 732 | """ |
| 733 | |
Supreeth Herle | df33037 | 2020-04-20 14:48:55 +0200 | [diff] [blame] | 734 | if table == "isim": |
| 735 | from pySim.ts_31_103 import EF_IST_map |
| 736 | lookup_map = EF_IST_map |
| 737 | elif table == "usim": |
Supreeth Herle | 0c4d82d | 2020-04-20 13:28:31 +0200 | [diff] [blame] | 738 | from pySim.ts_31_102 import EF_UST_map |
| 739 | lookup_map = EF_UST_map |
| 740 | else: |
| 741 | from pySim.ts_51_011 import EF_SST_map |
| 742 | lookup_map = EF_SST_map |
| 743 | |
| 744 | st_bytes = [st[i:i+2] for i in range(0, len(st), 2) ] |
| 745 | |
| 746 | avail_st = "" |
| 747 | # Get each byte and check for available services |
| 748 | for i in range(0, len(st_bytes)): |
| 749 | # Byte i contains info about Services num (8i+1) to num (8i+8) |
| 750 | byte = int(st_bytes[i], 16) |
| 751 | # Services in each byte are in order MSB to LSB |
| 752 | # MSB - Service (8i+8) |
| 753 | # LSB - Service (8i+1) |
| 754 | for j in range(1, 9): |
| 755 | if byte&0x01 == 0x01 and ((8*i) + j in lookup_map): |
| 756 | # Byte X contains info about Services num (8X-7) to num (8X) |
| 757 | # bit = 1: service available |
| 758 | # bit = 0: service not available |
| 759 | avail_st += '\tService %d - %s\n' % ((8*i) + j, lookup_map[(8*i) + j]) |
| 760 | byte = byte >> 1 |
| 761 | return avail_st |
Supreeth Herle | 9837055 | 2020-05-11 09:04:41 +0200 | [diff] [blame] | 762 | |
| 763 | def first_TLV_parser(bytelist): |
| 764 | ''' |
| 765 | first_TLV_parser([0xAA, 0x02, 0xAB, 0xCD, 0xFF, 0x00]) -> (170, 2, [171, 205]) |
| 766 | |
| 767 | parses first TLV format record in a list of bytelist |
| 768 | returns a 3-Tuple: Tag, Length, Value |
| 769 | Value is a list of bytes |
| 770 | parsing of length is ETSI'style 101.220 |
| 771 | ''' |
| 772 | Tag = bytelist[0] |
| 773 | if bytelist[1] == 0xFF: |
| 774 | Len = bytelist[2]*256 + bytelist[3] |
| 775 | Val = bytelist[4:4+Len] |
| 776 | else: |
| 777 | Len = bytelist[1] |
| 778 | Val = bytelist[2:2+Len] |
| 779 | return (Tag, Len, Val) |
| 780 | |
| 781 | def TLV_parser(bytelist): |
| 782 | ''' |
| 783 | TLV_parser([0xAA, ..., 0xFF]) -> [(T, L, [V]), (T, L, [V]), ...] |
| 784 | |
| 785 | loops on the input list of bytes with the "first_TLV_parser()" function |
| 786 | returns a list of 3-Tuples |
| 787 | ''' |
| 788 | ret = [] |
| 789 | while len(bytelist) > 0: |
| 790 | T, L, V = first_TLV_parser(bytelist) |
| 791 | if T == 0xFF: |
| 792 | # padding bytes |
| 793 | break |
| 794 | ret.append( (T, L, V) ) |
| 795 | # need to manage length of L |
| 796 | if L > 0xFE: |
| 797 | bytelist = bytelist[ L+4 : ] |
| 798 | else: |
| 799 | bytelist = bytelist[ L+2 : ] |
| 800 | return ret |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 801 | |
Supreeth Herle | d84daa1 | 2020-03-24 12:20:40 +0100 | [diff] [blame] | 802 | def enc_st(st, service, state=1): |
| 803 | """ |
| 804 | Encodes the EF S/U/IST/EST and returns the updated Service Table |
| 805 | |
| 806 | Parameters: |
| 807 | st - Current value of SIM/USIM/ISIM Service Table |
| 808 | service - Service Number to encode as activated/de-activated |
| 809 | state - 1 mean activate, 0 means de-activate |
| 810 | |
| 811 | Returns: |
| 812 | s - Modified value of SIM/USIM/ISIM Service Table |
| 813 | |
| 814 | Default values: |
| 815 | - state: 1 - Sets the particular Service bit to 1 |
| 816 | """ |
| 817 | st_bytes = [st[i:i+2] for i in range(0, len(st), 2) ] |
| 818 | |
| 819 | s = "" |
| 820 | # Check whether the requested service is present in each byte |
| 821 | for i in range(0, len(st_bytes)): |
| 822 | # Byte i contains info about Services num (8i+1) to num (8i+8) |
| 823 | if service in range((8*i) + 1, (8*i) + 9): |
| 824 | byte = int(st_bytes[i], 16) |
| 825 | # Services in each byte are in order MSB to LSB |
| 826 | # MSB - Service (8i+8) |
| 827 | # LSB - Service (8i+1) |
| 828 | mod_byte = 0x00 |
| 829 | # Copy bit by bit contents of byte to mod_byte with modified bit |
| 830 | # for requested service |
| 831 | for j in range(1, 9): |
| 832 | mod_byte = mod_byte >> 1 |
| 833 | if service == (8*i) + j: |
| 834 | mod_byte = state == 1 and mod_byte|0x80 or mod_byte&0x7f |
| 835 | else: |
| 836 | mod_byte = byte&0x01 == 0x01 and mod_byte|0x80 or mod_byte&0x7f |
| 837 | byte = byte >> 1 |
| 838 | |
| 839 | s += ('%02x' % (mod_byte)) |
| 840 | else: |
| 841 | s += st_bytes[i] |
| 842 | |
| 843 | return s |
| 844 | |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 845 | def dec_addr_tlv(hexstr): |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 846 | """ |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 847 | Decode hex string to get EF.P-CSCF Address or EF.ePDGId or EF.ePDGIdEm. |
| 848 | See 3GPP TS 31.102 version 13.4.0 Release 13, section 4.2.8, 4.2.102 and 4.2.104. |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 849 | """ |
| 850 | |
| 851 | # Convert from hex str to int bytes list |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 852 | addr_tlv_bytes = h2i(hexstr) |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 853 | |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 854 | # Get list of tuples containing parsed TLVs |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 855 | tlvs = TLV_parser(addr_tlv_bytes) |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 856 | |
| 857 | for tlv in tlvs: |
| 858 | # tlv = (T, L, [V]) |
| 859 | # T = Tag |
| 860 | # L = Length |
| 861 | # [V] = List of value |
| 862 | |
| 863 | # Invalid Tag value scenario |
| 864 | if tlv[0] != 0x80: |
| 865 | continue |
| 866 | |
Supreeth Herle | d6a5ec5 | 2020-06-01 12:27:51 +0200 | [diff] [blame] | 867 | # Empty field - Zero length |
| 868 | if tlv[1] == 0: |
| 869 | continue |
| 870 | |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 871 | # First byte in the value has the address type |
| 872 | addr_type = tlv[2][0] |
Supreeth Herle | 43fd03b | 2020-03-25 14:52:46 +0100 | [diff] [blame] | 873 | # TODO: Support parsing of IPv6 |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 874 | # Address Type: 0x00 (FQDN), 0x01 (IPv4), 0x02 (IPv6), other (Reserved) |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 875 | if addr_type == 0x00: #FQDN |
| 876 | # Skip address tye byte i.e. first byte in value list |
| 877 | content = tlv[2][1:] |
Philipp Maier | be18f2a | 2021-04-30 15:00:27 +0200 | [diff] [blame] | 878 | return (i2s(content), '00') |
| 879 | |
Supreeth Herle | 43fd03b | 2020-03-25 14:52:46 +0100 | [diff] [blame] | 880 | elif addr_type == 0x01: #IPv4 |
| 881 | # Skip address tye byte i.e. first byte in value list |
| 882 | # Skip the unused byte in Octect 4 after address type byte as per 3GPP TS 31.102 |
| 883 | ipv4 = tlv[2][2:] |
| 884 | content = '.'.join(str(x) for x in ipv4) |
Philipp Maier | be18f2a | 2021-04-30 15:00:27 +0200 | [diff] [blame] | 885 | return (content, '01') |
| 886 | else: |
| 887 | raise ValueError("Invalid address type") |
Supreeth Herle | d572ede | 2020-03-22 09:55:04 +0100 | [diff] [blame] | 888 | |
Philipp Maier | be18f2a | 2021-04-30 15:00:27 +0200 | [diff] [blame] | 889 | return (None, None) |
Philipp Maier | ff84c23 | 2020-05-12 17:24:18 +0200 | [diff] [blame] | 890 | |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 891 | def enc_addr_tlv(addr, addr_type='00'): |
Supreeth Herle | 3c0bd7a | 2020-03-23 11:59:33 +0100 | [diff] [blame] | 892 | """ |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 893 | Encode address TLV object used in EF.P-CSCF Address, EF.ePDGId and EF.ePDGIdEm. |
| 894 | See 3GPP TS 31.102 version 13.4.0 Release 13, section 4.2.8, 4.2.102 and 4.2.104. |
Supreeth Herle | 3c0bd7a | 2020-03-23 11:59:33 +0100 | [diff] [blame] | 895 | |
| 896 | Default values: |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 897 | - addr_type: 00 - FQDN format of Address |
Supreeth Herle | 3c0bd7a | 2020-03-23 11:59:33 +0100 | [diff] [blame] | 898 | """ |
| 899 | |
| 900 | s = "" |
| 901 | |
Supreeth Herle | 654eca7 | 2020-03-25 14:25:38 +0100 | [diff] [blame] | 902 | # TODO: Encoding of IPv6 address |
| 903 | if addr_type == '00': #FQDN |
Supreeth Herle | 3b342c2 | 2020-03-24 16:15:02 +0100 | [diff] [blame] | 904 | hex_str = s2h(addr) |
Supreeth Herle | 3c0bd7a | 2020-03-23 11:59:33 +0100 | [diff] [blame] | 905 | s += '80' + ('%02x' % ((len(hex_str)//2)+1)) + '00' + hex_str |
Supreeth Herle | 654eca7 | 2020-03-25 14:25:38 +0100 | [diff] [blame] | 906 | elif addr_type == '01': #IPv4 |
| 907 | ipv4_list = addr.split('.') |
| 908 | ipv4_str = "" |
| 909 | for i in ipv4_list: |
| 910 | ipv4_str += ('%02x' % (int(i))) |
| 911 | |
| 912 | # Unused bytes shall be set to 'ff'. i.e 4th Octet after Address Type is not used |
| 913 | # IPv4 Address is in octet 5 to octet 8 of the TLV data object |
| 914 | s += '80' + ('%02x' % ((len(ipv4_str)//2)+2)) + '01' + 'ff' + ipv4_str |
Supreeth Herle | 3c0bd7a | 2020-03-23 11:59:33 +0100 | [diff] [blame] | 915 | |
| 916 | return s |
| 917 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 918 | def is_hex(string:str, minlen:int=2, maxlen:Optional[int]=None) -> bool: |
Philipp Maier | 4723650 | 2021-03-09 21:28:25 +0100 | [diff] [blame] | 919 | """ |
| 920 | Check if a string is a valid hexstring |
| 921 | """ |
| 922 | |
| 923 | # Filter obviously bad strings |
| 924 | if not string: |
| 925 | return False |
| 926 | if len(string) < minlen or minlen < 2: |
| 927 | return False |
| 928 | if len(string) % 2: |
| 929 | return False |
| 930 | if maxlen and len(string) > maxlen: |
| 931 | return False |
| 932 | |
| 933 | # Try actual encoding to be sure |
| 934 | try: |
| 935 | try_encode = h2b(string) |
| 936 | return True |
| 937 | except: |
| 938 | return False |
| 939 | |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 940 | def sanitize_pin_adm(pin_adm, pin_adm_hex = None) -> Hexstr: |
Philipp Maier | e8536c0 | 2020-05-11 21:35:01 +0200 | [diff] [blame] | 941 | """ |
| 942 | The ADM pin can be supplied either in its hexadecimal form or as |
| 943 | ascii string. This function checks the supplied opts parameter and |
Harald Welte | 236a65f | 2021-04-03 10:20:11 +0200 | [diff] [blame] | 944 | returns the pin_adm as hex encoded string, regardless in which form |
Philipp Maier | e8536c0 | 2020-05-11 21:35:01 +0200 | [diff] [blame] | 945 | it was originally supplied by the user |
| 946 | """ |
| 947 | |
Harald Welte | 79b5ba4 | 2021-01-08 21:22:38 +0100 | [diff] [blame] | 948 | if pin_adm is not None: |
| 949 | if len(pin_adm) <= 8: |
| 950 | pin_adm = ''.join(['%02x'%(ord(x)) for x in pin_adm]) |
Philipp Maier | e8536c0 | 2020-05-11 21:35:01 +0200 | [diff] [blame] | 951 | pin_adm = rpad(pin_adm, 16) |
| 952 | |
| 953 | else: |
| 954 | raise ValueError("PIN-ADM needs to be <=8 digits (ascii)") |
| 955 | |
Harald Welte | 79b5ba4 | 2021-01-08 21:22:38 +0100 | [diff] [blame] | 956 | if pin_adm_hex is not None: |
| 957 | if len(pin_adm_hex) == 16: |
| 958 | pin_adm = pin_adm_hex |
Philipp Maier | e8536c0 | 2020-05-11 21:35:01 +0200 | [diff] [blame] | 959 | # Ensure that it's hex-encoded |
| 960 | try: |
| 961 | try_encode = h2b(pin_adm) |
| 962 | except ValueError: |
| 963 | raise ValueError("PIN-ADM needs to be hex encoded using this option") |
| 964 | else: |
| 965 | raise ValueError("PIN-ADM needs to be exactly 16 digits (hex encoded)") |
| 966 | |
| 967 | return pin_adm |
| 968 | |
Supreeth Herle | 95ec772 | 2020-03-24 13:09:03 +0100 | [diff] [blame] | 969 | def enc_ePDGSelection(hexstr, mcc, mnc, epdg_priority='0001', epdg_fqdn_format='00'): |
| 970 | """ |
| 971 | Encode ePDGSelection so it can be stored at EF.ePDGSelection or EF.ePDGSelectionEm. |
| 972 | See 3GPP TS 31.102 version 15.2.0 Release 15, section 4.2.104 and 4.2.106. |
| 973 | |
| 974 | Default values: |
| 975 | - epdg_priority: '0001' - 1st Priority |
| 976 | - epdg_fqdn_format: '00' - Operator Identifier FQDN |
| 977 | """ |
| 978 | |
| 979 | plmn1 = enc_plmn(mcc, mnc) + epdg_priority + epdg_fqdn_format |
| 980 | # TODO: Handle encoding of Length field for length more than 127 Bytes |
| 981 | content = '80' + ('%02x' % (len(plmn1)//2)) + plmn1 |
| 982 | content = rpad(content, len(hexstr)) |
| 983 | return content |
| 984 | |
Supreeth Herle | 95b4e8d | 2020-03-24 12:49:16 +0100 | [diff] [blame] | 985 | def dec_ePDGSelection(sixhexbytes): |
| 986 | """ |
| 987 | Decode ePDGSelection to get EF.ePDGSelection or EF.ePDGSelectionEm. |
| 988 | See 3GPP TS 31.102 version 15.2.0 Release 15, section 4.2.104 and 4.2.106. |
| 989 | """ |
| 990 | |
| 991 | res = {'mcc': 0, 'mnc': 0, 'epdg_priority': 0, 'epdg_fqdn_format': ''} |
| 992 | plmn_chars = 6 |
| 993 | epdg_priority_chars = 4 |
| 994 | epdg_fqdn_format_chars = 2 |
| 995 | # first three bytes (six ascii hex chars) |
| 996 | plmn_str = sixhexbytes[:plmn_chars] |
| 997 | # two bytes after first three bytes |
| 998 | epdg_priority_str = sixhexbytes[plmn_chars:plmn_chars + epdg_priority_chars] |
| 999 | # one byte after first five bytes |
| 1000 | epdg_fqdn_format_str = sixhexbytes[plmn_chars + epdg_priority_chars:plmn_chars + epdg_priority_chars + epdg_fqdn_format_chars] |
| 1001 | res['mcc'] = dec_mcc_from_plmn(plmn_str) |
| 1002 | res['mnc'] = dec_mnc_from_plmn(plmn_str) |
| 1003 | res['epdg_priority'] = epdg_priority_str |
| 1004 | res['epdg_fqdn_format'] = epdg_fqdn_format_str == '00' and 'Operator Identifier FQDN' or 'Location based FQDN' |
| 1005 | return res |
| 1006 | |
| 1007 | def format_ePDGSelection(hexstr): |
| 1008 | ePDGSelection_info_tag_chars = 2 |
| 1009 | ePDGSelection_info_tag_str = hexstr[:2] |
herlesupreeth | 3a261d3 | 2021-01-05 09:20:11 +0100 | [diff] [blame] | 1010 | s = "" |
Supreeth Herle | 95b4e8d | 2020-03-24 12:49:16 +0100 | [diff] [blame] | 1011 | # Minimum length |
| 1012 | len_chars = 2 |
| 1013 | # TODO: Need to determine length properly - definite length support only |
| 1014 | # Inconsistency in spec: 3GPP TS 31.102 version 15.2.0 Release 15, 4.2.104 |
| 1015 | # As per spec, length is 5n, n - number of PLMNs |
| 1016 | # But, each PLMN entry is made of PLMN (3 Bytes) + ePDG Priority (2 Bytes) + ePDG FQDN format (1 Byte) |
| 1017 | # Totalling to 6 Bytes, maybe length should be 6n |
| 1018 | len_str = hexstr[ePDGSelection_info_tag_chars:ePDGSelection_info_tag_chars+len_chars] |
herlesupreeth | 3a261d3 | 2021-01-05 09:20:11 +0100 | [diff] [blame] | 1019 | |
| 1020 | # Not programmed scenario |
| 1021 | if int(len_str, 16) == 255 or int(ePDGSelection_info_tag_str, 16) == 255: |
| 1022 | len_chars = 0 |
| 1023 | ePDGSelection_info_tag_chars = 0 |
Supreeth Herle | 95b4e8d | 2020-03-24 12:49:16 +0100 | [diff] [blame] | 1024 | if len_str[0] == '8': |
| 1025 | # The bits 7 to 1 denotes the number of length octets if length > 127 |
| 1026 | if int(len_str[1]) > 0: |
| 1027 | # Update number of length octets |
| 1028 | len_chars = len_chars * int(len_str[1]) |
| 1029 | len_str = hexstr[ePDGSelection_info_tag_chars:len_chars] |
| 1030 | |
| 1031 | content_str = hexstr[ePDGSelection_info_tag_chars+len_chars:] |
| 1032 | # Right pad to prevent index out of range - multiple of 6 bytes |
| 1033 | content_str = rpad(content_str, len(content_str) + (12 - (len(content_str) % 12))) |
Supreeth Herle | 95b4e8d | 2020-03-24 12:49:16 +0100 | [diff] [blame] | 1034 | for rec_data in hexstr_to_Nbytearr(content_str, 6): |
| 1035 | rec_info = dec_ePDGSelection(rec_data) |
| 1036 | if rec_info['mcc'] == 0xFFF and rec_info['mnc'] == 0xFFF: |
| 1037 | rec_str = "unused" |
| 1038 | else: |
| 1039 | rec_str = "MCC: %03d MNC: %03d ePDG Priority: %s ePDG FQDN format: %s" % \ |
| 1040 | (rec_info['mcc'], rec_info['mnc'], rec_info['epdg_priority'], rec_info['epdg_fqdn_format']) |
| 1041 | s += "\t%s # %s\n" % (rec_data, rec_str) |
| 1042 | return s |
Supreeth Herle | 556b0fe | 2020-03-25 11:26:57 +0100 | [diff] [blame] | 1043 | |
| 1044 | def get_addr_type(addr): |
| 1045 | """ |
| 1046 | Validates the given address and returns it's type (FQDN or IPv4 or IPv6) |
| 1047 | Return: 0x00 (FQDN), 0x01 (IPv4), 0x02 (IPv6), None (Bad address argument given) |
| 1048 | |
| 1049 | TODO: Handle IPv6 |
| 1050 | """ |
| 1051 | |
| 1052 | # Empty address string |
| 1053 | if not len(addr): |
| 1054 | return None |
| 1055 | |
Supreeth Herle | 556b0fe | 2020-03-25 11:26:57 +0100 | [diff] [blame] | 1056 | addr_list = addr.split('.') |
| 1057 | |
| 1058 | # Check for IPv4/IPv6 |
| 1059 | try: |
| 1060 | import ipaddress |
| 1061 | # Throws ValueError if addr is not correct |
Denis 'GNUtoo' Carikli | 79f5b60 | 2020-02-15 04:02:57 +0700 | [diff] [blame] | 1062 | ipa = ipaddress.ip_address(addr) |
Supreeth Herle | 556b0fe | 2020-03-25 11:26:57 +0100 | [diff] [blame] | 1063 | |
| 1064 | if ipa.version == 4: |
| 1065 | return 0x01 |
| 1066 | elif ipa.version == 6: |
| 1067 | return 0x02 |
| 1068 | except Exception as e: |
| 1069 | invalid_ipv4 = True |
| 1070 | for i in addr_list: |
| 1071 | # Invalid IPv4 may qualify for a valid FQDN, so make check here |
| 1072 | # e.g. 172.24.15.300 |
| 1073 | import re |
| 1074 | if not re.match('^[0-9_]+$', i): |
| 1075 | invalid_ipv4 = False |
| 1076 | break |
| 1077 | |
| 1078 | if invalid_ipv4: |
| 1079 | return None |
| 1080 | |
| 1081 | fqdn_flag = True |
| 1082 | for i in addr_list: |
| 1083 | # Only Alpha-numeric characters and hyphen - RFC 1035 |
| 1084 | import re |
| 1085 | if not re.match("^[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)?$", i): |
| 1086 | fqdn_flag = False |
| 1087 | break |
| 1088 | |
| 1089 | # FQDN |
| 1090 | if fqdn_flag: |
| 1091 | return 0x00 |
| 1092 | |
| 1093 | return None |
Harald Welte | 67d551a | 2021-01-21 14:50:01 +0100 | [diff] [blame] | 1094 | |
Harald Welte | 1e45657 | 2021-04-02 17:16:30 +0200 | [diff] [blame] | 1095 | def sw_match(sw:str, pattern:str) -> bool: |
Harald Welte | 67d551a | 2021-01-21 14:50:01 +0100 | [diff] [blame] | 1096 | """Match given SW against given pattern.""" |
| 1097 | # Create a masked version of the returned status word |
| 1098 | sw_lower = sw.lower() |
| 1099 | sw_masked = "" |
| 1100 | for i in range(0, 4): |
Philipp Maier | 78e32f2 | 2021-03-22 20:39:24 +0100 | [diff] [blame] | 1101 | if pattern[i] == '?': |
Harald Welte | 67d551a | 2021-01-21 14:50:01 +0100 | [diff] [blame] | 1102 | sw_masked = sw_masked + '?' |
Philipp Maier | 78e32f2 | 2021-03-22 20:39:24 +0100 | [diff] [blame] | 1103 | elif pattern[i] == 'x': |
Harald Welte | 67d551a | 2021-01-21 14:50:01 +0100 | [diff] [blame] | 1104 | sw_masked = sw_masked + 'x' |
| 1105 | else: |
| 1106 | sw_masked = sw_masked + sw_lower[i] |
Philipp Maier | 78e32f2 | 2021-03-22 20:39:24 +0100 | [diff] [blame] | 1107 | # Compare the masked version against the pattern |
Harald Welte | 67d551a | 2021-01-21 14:50:01 +0100 | [diff] [blame] | 1108 | return sw_masked == pattern |
Philipp Maier | 5d3e259 | 2021-02-22 17:22:16 +0100 | [diff] [blame] | 1109 | |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 1110 | def tabulate_str_list(str_list, width:int = 79, hspace:int = 2, lspace:int = 1, |
Harald Welte | 5225557 | 2021-04-03 09:56:32 +0200 | [diff] [blame] | 1111 | align_left:bool = True) -> str: |
Harald Welte | ee3501f | 2021-04-02 13:00:18 +0200 | [diff] [blame] | 1112 | """Pretty print a list of strings into a tabulated form. |
| 1113 | |
| 1114 | Args: |
| 1115 | width : total width in characters per line |
| 1116 | space : horizontal space between cells |
| 1117 | lspace : number of spaces before row |
| 1118 | align_lef : Align text to the left side |
| 1119 | Returns: |
| 1120 | multi-line string containing formatted table |
| 1121 | """ |
Philipp Maier | 5d3e259 | 2021-02-22 17:22:16 +0100 | [diff] [blame] | 1122 | if str_list == None: |
| 1123 | return "" |
| 1124 | if len(str_list) <= 0: |
| 1125 | return "" |
| 1126 | longest_str = max(str_list, key=len) |
| 1127 | cellwith = len(longest_str) + hspace |
| 1128 | cols = width // cellwith |
| 1129 | rows = (len(str_list) - 1) // cols + 1 |
| 1130 | table = [] |
| 1131 | for i in iter(range(rows)): |
| 1132 | str_list_row = str_list[i::rows] |
| 1133 | if (align_left): |
| 1134 | format_str_cell = '%%-%ds' |
| 1135 | else: |
| 1136 | format_str_cell = '%%%ds' |
| 1137 | format_str_row = (format_str_cell % cellwith) * len(str_list_row) |
| 1138 | format_str_row = (" " * lspace) + format_str_row |
| 1139 | table.append(format_str_row % tuple(str_list_row)) |
| 1140 | return '\n'.join(table) |
Harald Welte | 5e749a7 | 2021-04-10 17:18:17 +0200 | [diff] [blame] | 1141 | |
Harald Welte | 917d98c | 2021-04-21 11:51:25 +0200 | [diff] [blame] | 1142 | def auto_int(x): |
| 1143 | """Helper function for argparse to accept hexadecimal integers.""" |
| 1144 | return int(x, 0) |
| 1145 | |
Harald Welte | 5e749a7 | 2021-04-10 17:18:17 +0200 | [diff] [blame] | 1146 | class JsonEncoder(json.JSONEncoder): |
| 1147 | """Extend the standard library JSONEncoder with support for more types.""" |
| 1148 | def default(self, o): |
| 1149 | if isinstance(o, BytesIO) or isinstance(o, bytes) or isinstance(o, bytearray): |
| 1150 | return b2h(o) |
| 1151 | return json.JSONEncoder.default(self, o) |
Philipp Maier | 80ce71f | 2021-04-19 21:24:23 +0200 | [diff] [blame] | 1152 | |
| 1153 | def boxed_heading_str(heading, width=80): |
| 1154 | """Generate a string that contains a boxed heading.""" |
| 1155 | # Auto-enlarge box if heading exceeds length |
| 1156 | if len(heading) > width - 4: |
| 1157 | width = len(heading) + 4 |
| 1158 | |
| 1159 | res = "#" * width |
| 1160 | fstr = "\n# %-" + str(width - 4) + "s #\n" |
| 1161 | res += fstr % (heading) |
| 1162 | res += "#" * width |
| 1163 | return res |
Harald Welte | 3de6ca2 | 2021-05-02 20:05:56 +0200 | [diff] [blame] | 1164 | |
| 1165 | |
| 1166 | |
| 1167 | class DataObject(abc.ABC): |
| 1168 | """A DataObject (DO) in the sense of ISO 7816-4. Contrary to 'normal' TLVs where one |
| 1169 | simply has any number of different TLVs that may occur in any order at any point, ISO 7816 |
| 1170 | has the habit of specifying TLV data but with very spcific ordering, or specific choices of |
| 1171 | tags at specific points in a stream. This class tries to represent this.""" |
| 1172 | def __init__(self, name, desc = None, tag = None): |
| 1173 | """ |
| 1174 | Args: |
| 1175 | name: A brief, all-lowercase, underscore separated string identifier |
| 1176 | desc: A human-readable description of what this DO represents |
| 1177 | tag : The tag associated with this DO |
| 1178 | """ |
| 1179 | self.name = name |
| 1180 | self.desc = desc |
| 1181 | self.tag = tag |
| 1182 | self.decoded = None |
| 1183 | self.encoded = None |
| 1184 | |
| 1185 | def __str__(self): |
| 1186 | return self.name |
| 1187 | |
| 1188 | def __repr__(self): |
| 1189 | return '%s(%s)' % (self.__class__, self.name) |
| 1190 | |
| 1191 | def __or__(self, other): |
| 1192 | """OR-ing DataObjects together renders a DataObjectChoice.""" |
| 1193 | if isinstance(other, DataObject): |
| 1194 | # DataObject | DataObject = DataObjectChoice |
| 1195 | return DataObjectChoice(None, members=[self, other]) |
| 1196 | else: |
| 1197 | raise TypeError |
| 1198 | |
| 1199 | def __add__(self, other): |
| 1200 | """ADD-ing DataObjects together renders a DataObjectCollection.""" |
| 1201 | if isinstance(other, DataObject): |
| 1202 | # DataObject + DataObject = DataObjectCollectin |
| 1203 | return DataObjectCollection(None, members=[self, other]) |
| 1204 | |
| 1205 | def _compute_tag(self): |
| 1206 | """Compute the tag (sometimes the tag encodes part of the value).""" |
| 1207 | return self.tag |
| 1208 | |
| 1209 | def to_dict(self): |
| 1210 | """Return a dict in form "name: decoded_value" """ |
| 1211 | return {self.name: self.decoded} |
| 1212 | |
| 1213 | @abc.abstractmethod |
| 1214 | def from_bytes(self, do:bytes): |
| 1215 | """Parse the value part of the DO into the internal state of this instance. |
| 1216 | Args: |
| 1217 | do : binary encoded bytes |
| 1218 | """ |
| 1219 | |
| 1220 | @abc.abstractmethod |
| 1221 | def to_bytes(self): |
| 1222 | """Encode the internal state of this instance into the TLV value part. |
| 1223 | Returns: |
| 1224 | binary bytes encoding the internal state |
| 1225 | """ |
| 1226 | |
| 1227 | def from_tlv(self, do:bytes): |
| 1228 | """Parse binary TLV representation into internal state. The resulting decoded |
| 1229 | representation is _not_ returned, but just internalized in the object instance! |
| 1230 | Args: |
| 1231 | do : input bytes containing TLV-encoded representation |
| 1232 | Returns: |
| 1233 | bytes remaining at end of 'do' after parsing one TLV/DO. |
| 1234 | """ |
| 1235 | if do[0] != self.tag: |
| 1236 | raise ValueError('%s: Can only decode tag 0x%02x' % (self, self.tag)) |
| 1237 | length = do[1] |
| 1238 | val = do[2:2+length] |
| 1239 | self.from_bytes(val) |
| 1240 | # return remaining bytes |
| 1241 | return do[2+length:] |
| 1242 | |
| 1243 | def to_tlv(self): |
| 1244 | """Encode internal representation to binary TLV. |
| 1245 | Returns: |
| 1246 | bytes encoded in TLV format. |
| 1247 | """ |
| 1248 | val = self.to_bytes() |
| 1249 | return bytes(self._compute_tag()) + bytes(len(val)) + val |
| 1250 | |
| 1251 | # 'codec' interface |
| 1252 | def decode(self, binary:bytes): |
| 1253 | """Decode a single DOs from the input data. |
| 1254 | Args: |
| 1255 | binary : binary bytes of encoded data |
| 1256 | Returns: |
| 1257 | tuple of (decoded_result, binary_remainder) |
| 1258 | """ |
| 1259 | tag = binary[0] |
| 1260 | if tag != self.tag: |
| 1261 | raise ValueError('%s: Unknown Tag 0x%02x in %s; expected 0x%02x' % |
| 1262 | (self, tag, binary, self.tag)) |
| 1263 | remainder = self.from_tlv(binary) |
| 1264 | return (self.to_dict(), remainder) |
| 1265 | |
| 1266 | # 'codec' interface |
| 1267 | def encode(self): |
| 1268 | return self.to_tlv() |
| 1269 | |
| 1270 | class TL0_DataObject(DataObject): |
| 1271 | """Data Object that has Tag, Len=0 and no Value part.""" |
| 1272 | def __init__(self, name, desc, tag, val=None): |
| 1273 | super().__init__(name, desc, tag) |
| 1274 | self.val = val |
| 1275 | |
| 1276 | def from_bytes(self, binary:bytes): |
| 1277 | if len(binary) != 0: |
| 1278 | raise ValueError |
| 1279 | self.decoded = self.val |
| 1280 | |
| 1281 | def to_bytes(self): |
| 1282 | return b'' |
| 1283 | |
| 1284 | |
| 1285 | class DataObjectCollection: |
| 1286 | """A DataObjectCollection consits of multiple Data Objects identified by their tags. |
| 1287 | A given encoded DO may contain any of them in any order, and may contain multiple instances |
| 1288 | of each DO.""" |
| 1289 | def __init__(self, name, desc = None, members=None): |
| 1290 | self.name = name |
| 1291 | self.desc = desc |
| 1292 | self.members = members or [] |
| 1293 | self.members_by_tag = {} |
| 1294 | self.members_by_name = {} |
| 1295 | self.members_by_tag = { m.tag:m for m in members } |
| 1296 | self.members_by_name = { m.name:m for m in members } |
| 1297 | |
| 1298 | def __str__(self): |
| 1299 | member_strs = [str(x) for x in self.members] |
| 1300 | return '%s(%s)' % (self.name, ','.join(member_strs)) |
| 1301 | |
| 1302 | def __repr__(self): |
| 1303 | member_strs = [repr(x) for x in self.members] |
| 1304 | return '%s(%s)' % (self.__class__, ','.join(member_strs)) |
| 1305 | |
| 1306 | def __add__(self, other): |
| 1307 | """Extending DataCollections with other DataCollections or DataObjects.""" |
| 1308 | if isinstance(other, DataObjectCollection): |
| 1309 | # adding one collection to another |
| 1310 | members = self.members + other.members |
| 1311 | return DataObjectCollection(self.name, self.desc, members) |
| 1312 | elif isinstance(other, DataObject): |
| 1313 | # adding a member to a collection |
| 1314 | return DataObjectCollection(self.name, self.desc, self.members + [other]) |
| 1315 | else: |
| 1316 | raise TypeError |
| 1317 | |
| 1318 | # 'codec' interface |
| 1319 | def decode(self, binary:bytes): |
| 1320 | """Decode any number of DOs from the collection until the end of the input data, |
| 1321 | or uninitialized memory (0xFF) is found. |
| 1322 | Args: |
| 1323 | binary : binary bytes of encoded data |
| 1324 | Returns: |
| 1325 | tuple of (decoded_result, binary_remainder) |
| 1326 | """ |
| 1327 | res = [] |
| 1328 | remainder = binary |
| 1329 | # iterate until no binary trailer is left |
| 1330 | while len(remainder): |
| 1331 | tag = remainder[0] |
| 1332 | if tag == 0xff: # uninitialized memory at the end? |
| 1333 | return (res, remainder) |
| 1334 | if not tag in self.members_by_tag: |
| 1335 | raise ValueError('%s: Unknown Tag 0x%02x in %s; expected %s' % |
| 1336 | (self, tag, remainder, self.members_by_tag.keys())) |
| 1337 | obj = self.members_by_tag[tag] |
| 1338 | # DO from_tlv returns remainder of binary |
| 1339 | remainder = obj.from_tlv(remainder) |
| 1340 | # collect our results |
| 1341 | res.append(obj.to_dict()) |
| 1342 | return (res, remainder) |
| 1343 | |
| 1344 | # 'codec' interface |
| 1345 | def encode(self, decoded): |
| 1346 | res = bytearray() |
| 1347 | for i in decoded: |
| 1348 | obj = self.members_by_name(i[0]) |
| 1349 | res.append(obj.to_tlv()) |
| 1350 | return res |
| 1351 | |
| 1352 | class DataObjectChoice(DataObjectCollection): |
| 1353 | """One Data Object from within a choice, identified by its tag. |
| 1354 | This means that exactly one member of the choice must occur, and which one occurs depends |
| 1355 | on the tag.""" |
| 1356 | def __add__(self, other): |
| 1357 | """We overload the add operator here to avoid inheriting it from DataObjecCollection.""" |
| 1358 | raise TypeError |
| 1359 | |
| 1360 | def __or__(self, other): |
| 1361 | """OR-ing a Choice to another choice extends the choice, as does OR-ing a DataObject.""" |
| 1362 | if isinstance(other, DataObjectChoice): |
| 1363 | # adding one collection to another |
| 1364 | members = self.members + other.members |
| 1365 | return DataObjectChoice(self.name, self.desc, members) |
| 1366 | elif isinstance(other, DataObject): |
| 1367 | # adding a member to a collection |
| 1368 | return DataObjectChoice(self.name, self.desc, self.members + [other]) |
| 1369 | else: |
| 1370 | raise TypeError |
| 1371 | |
| 1372 | # 'codec' interface |
| 1373 | def decode(self, binary:bytes): |
| 1374 | """Decode a single DOs from the choice based on the tag. |
| 1375 | Args: |
| 1376 | binary : binary bytes of encoded data |
| 1377 | Returns: |
| 1378 | tuple of (decoded_result, binary_remainder) |
| 1379 | """ |
| 1380 | tag = binary[0] |
| 1381 | if tag == 0xff: |
| 1382 | return (None, binary) |
| 1383 | if not tag in self.members_by_tag: |
| 1384 | raise ValueError('%s: Unknown Tag 0x%02x in %s; expected %s' % |
| 1385 | (self, tag, binary, self.members_by_tag.keys())) |
| 1386 | obj = self.members_by_tag[tag] |
| 1387 | remainder = obj.from_tlv(binary) |
| 1388 | return (obj.to_dict(), remainder) |
| 1389 | |
| 1390 | # 'codec' interface |
| 1391 | def encode(self, decoded): |
| 1392 | obj = self.members_by_name(decoded[0]) |
| 1393 | return obj.to_tlv() |
| 1394 | |
| 1395 | class DataObjectSequence: |
| 1396 | """A sequence of DataObjects or DataObjectChoices. This allows us to express a certain |
| 1397 | ordered sequence of DOs or choices of DOs that have to appear as per the specification. |
| 1398 | By wrapping them into this formal DataObjectSequence, we can offer convenience methods |
| 1399 | for encoding or decoding an entire sequence.""" |
| 1400 | def __init__(self, name, desc=None, sequence=None): |
| 1401 | self.sequence = sequence or [] |
| 1402 | self.name = name |
| 1403 | self.desc = desc |
| 1404 | |
| 1405 | def __str__(self): |
| 1406 | member_strs = [str(x) for x in self.sequence] |
| 1407 | return '%s(%s)' % (self.name, ','.join(member_strs)) |
| 1408 | |
| 1409 | def __repr__(self): |
| 1410 | member_strs = [repr(x) for x in self.sequence] |
| 1411 | return '%s(%s)' % (self.__class__, ','.join(member_strs)) |
| 1412 | |
| 1413 | def __add__(self, other): |
| 1414 | """Add (append) a DataObject or DataObjectChoice to the sequence.""" |
| 1415 | if isinstance(other, 'DataObject'): |
| 1416 | return DataObjectSequence(self.name, self.desc, self.sequence + [other]) |
| 1417 | elif isinstance(other, 'DataObjectChoice'): |
| 1418 | return DataObjectSequence(self.name, self.desc, self.sequence + [other]) |
| 1419 | elif isinstance(other, 'DataObjectSequence'): |
| 1420 | return DataObjectSequence(self.name, self.desc, self.sequence + other.sequence) |
| 1421 | |
| 1422 | # 'codec' interface |
| 1423 | def decode(self, binary:bytes): |
| 1424 | """Decode a sequence by calling the decoder of each element in the sequence. |
| 1425 | Args: |
| 1426 | binary : binary bytes of encoded data |
| 1427 | Returns: |
| 1428 | tuple of (decoded_result, binary_remainder) |
| 1429 | """ |
| 1430 | remainder = binary |
| 1431 | res = [] |
| 1432 | for e in self.sequence: |
| 1433 | (r, remainder) = e.decode(remainder) |
| 1434 | if r: |
| 1435 | res.append(r) |
| 1436 | return (res, remainder) |
| 1437 | |
| 1438 | # 'codec' interface |
| 1439 | def decode_multi(self, do:bytes): |
| 1440 | """Decode multiple occurrences of the sequence from the binary input data. |
| 1441 | Args: |
| 1442 | do : binary input data to be decoded |
| 1443 | Returns: |
| 1444 | list of results of the decoder of this sequences |
| 1445 | """ |
| 1446 | remainder = do |
| 1447 | res = [] |
| 1448 | while len(remainder): |
| 1449 | (r, remainder2) = self.decode(remainder) |
| 1450 | if r: |
| 1451 | res.append(r) |
| 1452 | if len(remainder2) < len(remainder): |
| 1453 | remainder = remainder2 |
| 1454 | else: |
| 1455 | remainder = remainder2 |
| 1456 | break |
| 1457 | return (res, remainder) |
| 1458 | |
| 1459 | # 'codec' interface |
| 1460 | def encode(self, decoded): |
| 1461 | """Encode a sequence by calling the encoder of each element in the sequence.""" |
| 1462 | encoded = bytearray() |
| 1463 | i = 0 |
| 1464 | for e in self.sequence: |
| 1465 | encoded += e.encode(decoded[i]) |
| 1466 | i += 1 |
| 1467 | return encoded |
Harald Welte | 9044143 | 2021-05-02 21:28:12 +0200 | [diff] [blame] | 1468 | |
| 1469 | class CardCommand: |
| 1470 | """A single card command / instruction.""" |
| 1471 | def __init__(self, name, ins, cla_list=None, desc=None): |
| 1472 | self.name = name |
| 1473 | self.ins = ins |
| 1474 | self.cla_list = cla_list or [] |
| 1475 | self.cla_list = [x.lower() for x in self.cla_list] |
| 1476 | self.desc = desc |
| 1477 | |
| 1478 | def __str__(self): |
| 1479 | return self.name |
| 1480 | |
| 1481 | def __repr__(self): |
| 1482 | return '%s(INS=%02x,CLA=%s)' % (self.name, self.ins, self.cla_list) |
| 1483 | |
| 1484 | def match_cla(self, cla): |
| 1485 | """Does the given CLA match the CLA list of the command?.""" |
| 1486 | if not isinstance(cla, str): |
| 1487 | cla = '%02u' % cla |
| 1488 | cla = cla.lower() |
| 1489 | for cla_match in self.cla_list: |
| 1490 | cla_masked = "" |
| 1491 | for i in range(0, 2): |
| 1492 | if cla_match[i] == 'x': |
| 1493 | cla_masked += 'x' |
| 1494 | else: |
| 1495 | cla_masked += cla[i] |
| 1496 | if cla_masked == cla_match: |
| 1497 | return True |
| 1498 | return False |
| 1499 | |
| 1500 | |
| 1501 | class CardCommandSet: |
| 1502 | """A set of card instructions, typically specified within one spec.""" |
| 1503 | def __init__(self, name, cmds=[]): |
| 1504 | self.name = name |
| 1505 | self.cmds = { c.ins : c for c in cmds } |
| 1506 | |
| 1507 | def __str__(self): |
| 1508 | return self.name |
| 1509 | |
| 1510 | def __getitem__(self, idx): |
| 1511 | return self.cmds[idx] |
| 1512 | |
| 1513 | def __add__(self, other): |
| 1514 | if isinstance(other, CardCommand): |
| 1515 | if other.ins in self.cmds: |
| 1516 | raise ValueError('%s: INS 0x%02x already defined: %s' % |
| 1517 | (self, other.ins, self.cmds[other.ins])) |
| 1518 | self.cmds[other.ins] = other |
| 1519 | elif isinstance(other, CardCommandSet): |
| 1520 | for c in other.cmds.keys(): |
| 1521 | self.cmds[c] = other.cmds[c] |
| 1522 | else: |
| 1523 | raise ValueError('%s: Unsupported type to add operator: %s' % (self, other)) |
| 1524 | |
| 1525 | def lookup(self, ins, cla=None): |
| 1526 | """look-up the command within the CommandSet.""" |
| 1527 | ins = int(ins) |
| 1528 | if not ins in self.cmds: |
| 1529 | return None |
| 1530 | cmd = self.cmds[ins] |
| 1531 | if cla and not cmd.match_cla(cla): |
| 1532 | return None |
| 1533 | return cmd |
Philipp Maier | a028c7d | 2021-11-08 16:12:03 +0100 | [diff] [blame] | 1534 | |
| 1535 | def all_subclasses(cls) -> set: |
| 1536 | """Recursively get all subclasses of a specified class""" |
| 1537 | return set(cls.__subclasses__()).union([s for c in cls.__subclasses__() for s in all_subclasses(c)]) |