From patchwork Tue Dec 4 08:47:05 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Laszlo Sitzer X-Patchwork-Id: 1007487 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=lists.osmocom.org (client-ip=144.76.43.76; helo=lists.osmocom.org; envelope-from=openbsc-bounces@lists.osmocom.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sumup.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sumup.com header.i=@sumup.com header.b="owqEMVDm"; dkim-atps=neutral Received: from lists.osmocom.org (lists.osmocom.org [144.76.43.76]) by ozlabs.org (Postfix) with ESMTP id 438FqP3TsLz9s55 for ; Tue, 4 Dec 2018 19:47:28 +1100 (AEDT) Received: from lists.osmocom.org (lists.osmocom.org [144.76.43.76]) by lists.osmocom.org (Postfix) with ESMTP id D06B795835; Tue, 4 Dec 2018 08:47:22 +0000 (UTC) Authentication-Results: lists.osmocom.org; dmarc=none header.from=sumup.com Authentication-Results: lists.osmocom.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=sumup.com header.i=@sumup.com header.b=owqEMVDm X-Original-To: openbsc@lists.osmocom.org Delivered-To: openbsc@lists.osmocom.org Received-SPF: Permerror (SPF Permanent Error: More than 10 MX records returned) identity=mailfrom; client-ip=2607:f8b0:4864:20::431; helo=mail-pf1-x431.google.com; envelope-from=laszlo.sitzer@sumup.com; receiver=openbsc@lists.osmocom.org Authentication-Results: lists.osmocom.org; dmarc=none header.from=sumup.com Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lists.osmocom.org (Postfix) with ESMTP id 0638A95820 for ; Tue, 4 Dec 2018 08:47:18 +0000 (UTC) Received: by mail-pf1-x431.google.com with SMTP id c123so7878504pfb.0 for ; Tue, 04 Dec 2018 00:47:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sumup.com; s=google; h=mime-version:from:date:message-id:subject:to; bh=6MrRDM6v+c5SrgLeD+w1Q9RN+zgCuhTkbflSGoQT1wE=; b=owqEMVDmj0Q3AA1KX8fUnh18rRoPkD3Tc+oLuLaQcsPyJ9eO+Tkgge3kfI+bnkxMGo biuan5vFpZ0bqfR2UdsPAqFzqCJ7lAyAdMMgZcw/KDziK2TPS3NkjfPYsFJH3MOqvfC4 O1S6kycubs8gqda2tlQQLuGMJf2zF5GXjEXH0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=6MrRDM6v+c5SrgLeD+w1Q9RN+zgCuhTkbflSGoQT1wE=; b=uEzzYK6LqjdaT71BnOyW1zvvDaeL0aTPpJbkhRxifEhNeuH4U7njTjUwv16VC6MbnH ujPPEwQq2milK2tHar+v+KkJY7q6xYZ44ytcBfc0bEDUxsNPLIiObSOkDJSqucvzmGR7 SwGhj4I6lUwYQPe7H4qhi0xTnPZAlT/hQQf2OjY7vqmZc2zkcqCERZ3jbnWL7YgiBOH0 odVDOuRwWDXbBEbsCez77WUQuTeazmqcaH4Mx8+2/3QKWNc1QkZddiTggSgY/oPWPVuY x55b5I0P62LXx6eRjs6TVsuBm5wcQKRA8il3RBfSMrtUPMut6SO8ZYUelbTa2FDF4BVV ABsA== X-Gm-Message-State: AA+aEWbi1QZyHM0aDAE9n+zqu0nR87LkCYZj1JzGda9J1M/VebThS+d6 A6B4dp8fs6wP/qBlbz0qBxolFxJzwSNAvTMzVe1Xhj+YAQW6B6So X-Google-Smtp-Source: AFSGD/XbZQujEm+/RRf6LnIvzmww09rMWOov7KOUO170yF2dPDm7AshSt2rKl88Tl7zIhDrSNrcVNx60vbc9OuUWVNs= X-Received: by 2002:a62:938f:: with SMTP id r15mr19106226pfk.27.1543913237171; Tue, 04 Dec 2018 00:47:17 -0800 (PST) MIME-Version: 1.0 From: Daniel Laszlo Sitzer Date: Tue, 4 Dec 2018 09:47:05 +0100 Message-ID: Subject: pySim patch - decode EF_[O|H]PLMNwAcT To: openbsc@lists.osmocom.org X-BeenThere: openbsc@lists.osmocom.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Development of OsmoBTS, OpenBSC, OsmoBSC, OsmoNITB, OsmoMSC, OsmoMGW, OsmoSIPconnector" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openbsc-bounces@lists.osmocom.org Sender: "OpenBSC" Hello everyone, I implemented decoding of the [O|H]PLMNwAcT elementary file in pySim. Here https://github.com/lazlo/pysim-dec-plmn you will also find unit tests for the code I wrote. Let me know if I should further refactor the code (maybe to use more of the existing conversions functions in pySim/util.py). I'm aware it is not the most elegant code but it does its job. Best, Lazlo From d75d3462ffb67d860bf53568b0e1155e1ea31616 Mon Sep 17 00:00:00 2001 From: Laszlo Sitzer Date: Mon, 3 Dec 2018 17:40:20 +0100 Subject: [PATCH] WIP: Decode xPLMNwAcT. --- pySim-read.py | 8 +++--- pySim/utils.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/pySim-read.py b/pySim-read.py index 4356453..6341fd0 100755 --- a/pySim-read.py +++ b/pySim-read.py @@ -37,7 +37,7 @@ except ImportError: import simplejson as json from pySim.commands import SimCardCommands -from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid +from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, format_xplmn_w_act def parse_options(): @@ -129,7 +129,7 @@ if __name__ == '__main__': try: (res, sw) = scc.read_binary(EF['PLMNwAcT']) if sw == '9000': - print("PLMNwAcT: %s" % (res)) + print("PLMNwAcT:\n%s" % (format_xplmn_w_act(res))) else: print("PLMNwAcT: Can't read, response code = %s" % (sw,)) except Exception as e: @@ -139,7 +139,7 @@ if __name__ == '__main__': try: (res, sw) = scc.read_binary(EF['OPLMNwAcT']) if sw == '9000': - print("OPLMNwAcT: %s" % (res)) + print("OPLMNwAcT:\n%s" % (format_xplmn_w_act(res))) else: print("OPLMNwAcT: Can't read, response code = %s" % (sw,)) except Exception as e: @@ -149,7 +149,7 @@ if __name__ == '__main__': try: (res, sw) = scc.read_binary(EF['HPLMNAcT']) if sw == '9000': - print("HPLMNAcT: %s" % (res)) + print("HPLMNAcT:\n%s" % (format_xplmn_w_act(res))) else: print("HPLMNAcT: Can't read, response code = %s" % (sw,)) except Exception as e: diff --git a/pySim/utils.py b/pySim/utils.py index ba94702..afb0d28 100644 --- a/pySim/utils.py +++ b/pySim/utils.py @@ -113,6 +113,79 @@ def enc_spn(name, hplmn_disp=False, oplmn_disp=False): if oplmn_disp: byte1 = byte1|0x02 return i2h([byte1])+s2h(name) +def hexstr_to_fivebytearr(input_str): + return [s[i:i+10] for i in range(0, len(s), 10) ] + +# Accepts hex string representing three bytes +def dec_mcc_from_plmn(plmn): + ia = h2i(plmn) + digit1 = ia[0] & 0x0F # 1st byte, LSB + digit2 = (ia[0] & 0xF0) >> 4 # 1st byte, MSB + digit3 = ia[1] & 0x0F # 2nd byte, LSB + if digit3 == 0xF and digit2 == 0xF and digit1 == 0xF: + return 0xFFF # 4095 + mcc = digit1 * 100 + mcc += digit2 * 10 + mcc += digit3 + return mcc + +def dec_mnc_from_plmn(plmn): + ia = h2i(plmn) + digit1 = ia[2] & 0x0F # 3rd byte, LSB + digit2 = (ia[2] & 0xF0) >> 4 # 3rd byte, MSB + digit3 = (ia[1] & 0xF0) >> 4 # 2nd byte, MSB + if digit3 == 0xF and digit2 == 0xF and digit1 == 0xF: + return 0xFFF # 4095 + mnc = 0 + # signifies two digit MNC + if digit3 == 0xF: + mnc += digit1 * 10 + mnc += digit2 + else: + mnc += digit1 * 100 + mnc += digit2 * 10 + mnc += digit3 + return mnc + +def dec_act(twohexbytes): + act_list = [ + {'bit': 15, 'name': "UTRAN"}, + {'bit': 14, 'name': "E-UTRAN"}, + {'bit': 7, 'name': "GSM"}, + {'bit': 6, 'name': "GSM COMPACT"}, + {'bit': 5, 'name': "cdma2000 HRPD"}, + {'bit': 4, 'name': "cdma2000 1xRTT"}, + ] + ia = h2i(twohexbytes) + u16t = (ia[0] << 8)|ia[1] + sel = [] + for a in act_list: + if u16t & (1 << a['bit']): + sel.append(a['name']) + return sel + +def dec_xplmn_w_act(fivehexbytes): + res = {'mcc': 0, 'mnc': 0, 'act': []} + plmn_chars = 6 + act_chars = 4 + plmn_str = fivehexbytes[:plmn_chars] # first three bytes (six ascii hex chars) + act_str = fivehexbytes[plmn_chars:plmn_chars + act_chars] # two bytes after first three bytes + res['mcc'] = dec_mcc_from_plmn(plmn_str) + res['mnc'] = dec_mnc_from_plmn(plmn_str) + res['act'] = dec_act(act_str) + return res + +def format_xplmn_w_act(hexstr): + s = "" + for rec_data in hexstr_to_fivebytearr(hexstr): + rec_info = dec_xplmn_w_act(rec_data) + if rec_info['mcc'] == 0xFFF and rec_info['mnc'] == 0xFFF: + rec_str = "unused" + else: + rec_str = "MCC: %3s MNC: %3s AcT: %s" % (rec_info['mcc'], rec_info['mnc'], ", ".join(rec_info['act'])) + s += "%s # %s\n" % (rec_data, rec_str) + return s + def derive_milenage_opc(ki_hex, op_hex): """ Run the milenage algorithm to calculate OPC from Ki and OP -- 2.19.2