Change in pysim[master]: pySim-read.py: fix reading and parsing of EF.MSISDN

2020-02-15 Thread laforge
laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/16947 
)

Change subject: pySim-read.py: fix reading and parsing of EF.MSISDN
..

pySim-read.py: fix reading and parsing of EF.MSISDN

This change implements parsing of EF.MSISDN (and thus EF.ADN)
as per 3GPP TS 31.102, sections 4.2.26 and 4.4.2.3.

Example (commercial SIM card from 401/02):

  EF.MSISDN: 07917787028982f7
  Decoded (NPI=1 ToN=1): +77782098287

Note that sysmoUSIM-SJS1 in the test setup has malformed
EF.MSISDN, so that's why the test output is changed.

Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
---
M pySim-read.py
M pySim/utils.py
M pysim-testdata/sysmoISIM-SJA2.ok
M pysim-testdata/sysmoUSIM-SJS1.ok
4 files changed, 48 insertions(+), 5 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/pySim-read.py b/pySim-read.py
index d753c8b..ce5f8a5 100755
--- a/pySim-read.py
+++ b/pySim-read.py
@@ -37,7 +37,7 @@
import simplejson as json

 from pySim.commands import SimCardCommands
-from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, 
format_xplmn_w_act, dec_spn
+from pySim.utils import h2b, swap_nibbles, rpad, dec_imsi, dec_iccid, 
dec_msisdn, format_xplmn_w_act, dec_spn


 def parse_options():
@@ -186,8 +186,10 @@
#   print(scc.record_size(['3f00', '7f10', '6f40']))
(res, sw) = scc.read_record(['3f00', '7f10', '6f40'], 1)
if sw == '9000':
-   if res[1] != 'f':
-   print("MSISDN: %s" % (res,))
+   res_dec = dec_msisdn(res)
+   if res_dec is not None:
+   # (npi, ton, msisdn) = res_dec
+   print("MSISDN (NPI=%d ToN=%d): %s" % res_dec)
else:
print("MSISDN: Not available")
else:
diff --git a/pySim/utils.py b/pySim/utils.py
index af4a491..12f66b9 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -258,3 +258,44 @@
mnc += digit2

return mnc
+
+def dec_msisdn(ef_msisdn):
+   """
+   Decode MSISDN from EF.MSISDN or EF.ADN (same structure).
+   See 3GPP TS 31.102, section 4.2.26 and 4.4.2.3.
+   """
+
+   # Convert from str to (kind of) 'bytes'
+   ef_msisdn = h2b(ef_msisdn)
+
+   # Make sure mandatory fields are present
+   if len(ef_msisdn) < 14:
+   raise ValueError("EF.MSISDN is too short")
+
+   # Skip optional Alpha Identifier
+   xlen = len(ef_msisdn) - 14
+   msisdn_lhv = ef_msisdn[xlen:]
+
+   # Parse the length (in bytes) of the BCD encoded number
+   bcd_len = ord(msisdn_lhv[0])
+   # BCD length = length of dial num (max. 10 bytes) + 1 byte ToN and NPI
+   if bcd_len == 0xff:
+   return None
+   elif bcd_len > 11 or bcd_len < 1:
+   raise ValueError("Length of MSISDN (%d bytes) is out of range" 
% bcd_len)
+
+   # Parse ToN / NPI
+   ton = (ord(msisdn_lhv[1]) >> 4) & 0x07
+   npi = ord(msisdn_lhv[1]) & 0x0f
+   bcd_len -= 1
+
+   # No MSISDN?
+   if not bcd_len:
+   return (npi, ton, None)
+
+   msisdn = swap_nibbles(b2h(msisdn_lhv[2:][:bcd_len])).rstrip('f')
+   # International number 10.5.118/3GPP TS 24.008
+   if (ton & 0x01) == 0x01:
+   msisdn = '+' + msisdn
+
+   return (npi, ton, msisdn)
diff --git a/pysim-testdata/sysmoISIM-SJA2.ok b/pysim-testdata/sysmoISIM-SJA2.ok
index a64aa8b..bfef882 100644
--- a/pysim-testdata/sysmoISIM-SJA2.ok
+++ b/pysim-testdata/sysmoISIM-SJA2.ok
@@ -50,7 +50,7 @@
ff # unused

 ACC: 0001
-MSISDN: Not available
+MSISDN (NPI=1 ToN=1): +1234
 AD: 0002
 Done !

diff --git a/pysim-testdata/sysmoUSIM-SJS1.ok b/pysim-testdata/sysmoUSIM-SJS1.ok
index 8def4e3..6d85f38 100644
--- a/pysim-testdata/sysmoUSIM-SJS1.ok
+++ b/pysim-testdata/sysmoUSIM-SJS1.ok
@@ -50,7 +50,7 @@
ff # unused

 ACC: 0008
-MSISDN: Not available
+MSISDN: Can't read file -- Length of MSISDN (136 bytes) is out of range
 AD: 0002
 Done !


--
To view, visit https://gerrit.osmocom.org/c/pysim/+/16947
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
Gerrit-Change-Number: 16947
Gerrit-PatchSet: 19
Gerrit-Owner: herlesupreeth 
Gerrit-Assignee: dexter 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria 
Gerrit-Reviewer: herlesupreeth 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: neels 
Gerrit-CC: dexter 
Gerrit-MessageType: merged


Change in pysim[master]: pySim-read.py: fix reading and parsing of EF.MSISDN

2020-02-15 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/pysim/+/16947 )

Change subject: pySim-read.py: fix reading and parsing of EF.MSISDN
..


Patch Set 19: Code-Review+2


--
To view, visit https://gerrit.osmocom.org/c/pysim/+/16947
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
Gerrit-Change-Number: 16947
Gerrit-PatchSet: 19
Gerrit-Owner: herlesupreeth 
Gerrit-Assignee: dexter 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria 
Gerrit-Reviewer: herlesupreeth 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: neels 
Gerrit-CC: dexter 
Gerrit-Comment-Date: Sat, 15 Feb 2020 19:11:31 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in pysim[master]: pySim-read.py: fix reading and parsing of EF.MSISDN

2020-02-15 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/pysim/+/16947 )

Change subject: pySim-read.py: fix reading and parsing of EF.MSISDN
..


Patch Set 19: Code-Review+1


--
To view, visit https://gerrit.osmocom.org/c/pysim/+/16947
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
Gerrit-Change-Number: 16947
Gerrit-PatchSet: 19
Gerrit-Owner: herlesupreeth 
Gerrit-Assignee: dexter 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria 
Gerrit-Reviewer: herlesupreeth 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: neels 
Gerrit-CC: dexter 
Gerrit-Comment-Date: Sat, 15 Feb 2020 09:30:03 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in pysim[master]: pySim-read.py: fix reading and parsing of EF.MSISDN

2020-02-14 Thread fixeria
fixeria has uploaded a new patch set (#19) to the change originally created by 
herlesupreeth. ( https://gerrit.osmocom.org/c/pysim/+/16947 )

Change subject: pySim-read.py: fix reading and parsing of EF.MSISDN
..

pySim-read.py: fix reading and parsing of EF.MSISDN

This change implements parsing of EF.MSISDN (and thus EF.ADN)
as per 3GPP TS 31.102, sections 4.2.26 and 4.4.2.3.

Example (commercial SIM card from 401/02):

  EF.MSISDN: 07917787028982f7
  Decoded (NPI=1 ToN=1): +77782098287

Note that sysmoUSIM-SJS1 in the test setup has malformed
EF.MSISDN, so that's why the test output is changed.

Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
---
M pySim-read.py
M pySim/utils.py
M pysim-testdata/sysmoISIM-SJA2.ok
M pysim-testdata/sysmoUSIM-SJS1.ok
4 files changed, 48 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/47/16947/19
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/16947
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
Gerrit-Change-Number: 16947
Gerrit-PatchSet: 19
Gerrit-Owner: herlesupreeth 
Gerrit-Assignee: dexter 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria 
Gerrit-Reviewer: herlesupreeth 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: neels 
Gerrit-CC: dexter 
Gerrit-MessageType: newpatchset


Change in pysim[master]: pySim-read.py: fix reading and parsing of EF.MSISDN

2020-02-14 Thread fixeria
fixeria has uploaded a new patch set (#18) to the change originally created by 
herlesupreeth. ( https://gerrit.osmocom.org/c/pysim/+/16947 )

Change subject: pySim-read.py: fix reading and parsing of EF.MSISDN
..

pySim-read.py: fix reading and parsing of EF.MSISDN

This change implements parsing of EF.MSISDN (and thus EF.ADN)
as per 3GPP TS 31.102, sections 4.2.26 and 4.4.2.3.

Example (commercial SIM card from 401/02):

  EF.MSISDN: 07917787028982f7
  Decoded (NPI=1 ToN=1): +77782098287

Note that sysmoUSIM-SJS1 in the test setup has malformed
EF.MSISDN, so that's why the test output is changed.

Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
---
M pySim-read.py
M pySim/utils.py
M pysim-testdata/sysmoUSIM-SJS1.ok
3 files changed, 47 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/47/16947/18
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/16947
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
Gerrit-Change-Number: 16947
Gerrit-PatchSet: 18
Gerrit-Owner: herlesupreeth 
Gerrit-Assignee: dexter 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria 
Gerrit-Reviewer: herlesupreeth 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: neels 
Gerrit-CC: dexter 
Gerrit-MessageType: newpatchset


Change in pysim[master]: pySim-read.py: fix reading and parsing of EF.MSISDN

2020-02-14 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/pysim/+/16947 )

Change subject: pySim-read.py: fix reading and parsing of EF.MSISDN
..


Patch Set 17: Code-Review+1


--
To view, visit https://gerrit.osmocom.org/c/pysim/+/16947
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
Gerrit-Change-Number: 16947
Gerrit-PatchSet: 17
Gerrit-Owner: herlesupreeth 
Gerrit-Assignee: dexter 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria 
Gerrit-Reviewer: herlesupreeth 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: neels 
Gerrit-CC: dexter 
Gerrit-Comment-Date: Fri, 14 Feb 2020 19:33:51 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in pysim[master]: pySim-read.py: fix reading and parsing of EF.MSISDN

2020-02-14 Thread fixeria
fixeria has uploaded a new patch set (#17) to the change originally created by 
herlesupreeth. ( https://gerrit.osmocom.org/c/pysim/+/16947 )

Change subject: pySim-read.py: fix reading and parsing of EF.MSISDN
..

pySim-read.py: fix reading and parsing of EF.MSISDN

This change implements parsing of EF.MSISDN (and thus EF.ADN)
as per 3GPP TS 31.102, sections 4.2.26 and 4.4.2.3.

Example (commercial SIM card from 401/02):

  EF.MSISDN: 07917787028982f7
  Decoded: (ToN=1 NPI=1) +77782098287

Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
---
M pySim-read.py
M pySim/utils.py
2 files changed, 46 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/47/16947/17
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/16947
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
Gerrit-Change-Number: 16947
Gerrit-PatchSet: 17
Gerrit-Owner: herlesupreeth 
Gerrit-Assignee: dexter 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria 
Gerrit-Reviewer: herlesupreeth 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: neels 
Gerrit-CC: dexter 
Gerrit-MessageType: newpatchset


Change in pysim[master]: pySim-read.py: fix reading and parsing of EF.MSISDN

2020-02-14 Thread fixeria
fixeria has uploaded a new patch set (#16) to the change originally created by 
herlesupreeth. ( https://gerrit.osmocom.org/c/pysim/+/16947 )

Change subject: pySim-read.py: fix reading and parsing of EF.MSISDN
..

pySim-read.py: fix reading and parsing of EF.MSISDN

This change implements parsing of EF.MSISDN (and thus EF.ADN)
as per 3GPP TS 31.102, sections 4.2.26 and 4.4.2.3.

Example (commercial SIM card from 401/02):

  EF.MSISDN: 07917787028982f7
  Decoded: (ToN=1 NPI=1) +77782098287

Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
---
M pySim-read.py
M pySim/utils.py
2 files changed, 46 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/47/16947/16
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/16947
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ie914ae83d787e3f1a90f9f305bffd45053b8c863
Gerrit-Change-Number: 16947
Gerrit-PatchSet: 16
Gerrit-Owner: herlesupreeth 
Gerrit-Assignee: dexter 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria 
Gerrit-Reviewer: herlesupreeth 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: neels 
Gerrit-CC: dexter 
Gerrit-MessageType: newpatchset