Change in pysim[master]: Add support for ADF_USIM/EF_EHPLMN

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Wed Jun 3 15:36:20 UTC 2020


laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/18649 )


Change subject: Add support for ADF_USIM/EF_EHPLMN
......................................................................

Add support for ADF_USIM/EF_EHPLMN

If the EF.EHPLMN exists, it contains the "Equivalent Home PLMN List".
The odd part of that list is that it is not just a list of additional
PLMN identities, but if the first digits of the IMSI are *not* listed
in EF.EHPLMN, then the MCC/MNC of the IMSI prefix is suddently no
longer considered the home network, but the subscriber is roaming.

See TS 23.122: "If the HPLMN code derived from the IMSI is not present
in the EHPLMN list, then it shall be treated as a Visited PLMN for PLMN
selection purposes."

Change-Id: I22d96ab4a424ec5bc1fb02f5e80165c646a748d3
---
M pySim-read.py
M pySim/cards.py
M pySim/utils.py
3 files changed, 51 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/49/18649/1

diff --git a/pySim-read.py b/pySim-read.py
index b23e6ee..92fbd7b 100755
--- a/pySim-read.py
+++ b/pySim-read.py
@@ -241,6 +241,15 @@
 	# Check whether we have th AID of USIM, if so select it by its AID
 	# EF.UST - File Id in ADF USIM : 6f38
 	if '9000' == card.select_adf_by_aid():
+		# EF.EHPLMN
+		try:
+			(res, sw) = card.read_ehplmn()
+			if sw == '9000':
+				print("EHPLMN:\n%s" % (res))
+			else:
+				print("EHPLMN: Can't read, response code = %s" % (sw,))
+		except Exception as e:
+			print("EHPLMN: Can't read file -- " + str(e))
 		# EF.UST
 		(res, sw) = card.read_binary(EF_USIM_ADF_map['UST'])
 		if sw == '9000':
diff --git a/pySim/cards.py b/pySim/cards.py
index 808fde1..302c886 100644
--- a/pySim/cards.py
+++ b/pySim/cards.py
@@ -24,6 +24,7 @@
 #
 
 from pySim.ts_51_011 import EF, DF
+from pySim.ts_31_102 import EF_USIM_ADF_map
 from pySim.utils import *
 from smartcard.util import toBytes
 
@@ -77,6 +78,20 @@
 		else:
 			return (None, sw)
 
+	def read_ehplmn(self):
+		(res, sw) = self._scc.read_binary(EF_USIM_ADF_map['EHPLMN'])
+		if sw == '9000':
+			return (format_xplmn(res), sw)
+		else:
+			return (None, sw)
+
+	def update_ehplmn(self, mcc, mnc):
+		data = self._scc.read_binary(EF_USIM_ADF_map['EHPLMN'], length=None, offset=0)
+		size = len(data[0]) // 2
+		ehplmn = enc_plmn(mcc, mnc)
+		data, sw = self._scc.update_binary(EF_USIM_ADF_map['EHPLMN'], ehplmn)
+		return sw
+
 	def update_hplmn_act(self, mcc, mnc, access_tech='FFFF'):
 		"""
 		Update Home PLMN with access technology bit-field
@@ -1144,16 +1159,20 @@
 			if p.get('opc'):
 				self._scc.update_binary('af20', p['opc'], 17)
 
-		# update EF-USIM_AUTH_KEY in ADF.USIM
 		self._scc.select_file(['3f00'])
 		aid = self.read_aid()
 		if (aid):
+			# update EF-USIM_AUTH_KEY in ADF.USIM
 			self._scc.select_adf(aid)
 			if p.get('ki'):
 				self._scc.update_binary('af20', p['ki'], 1)
 			if p.get('opc'):
 				self._scc.update_binary('af20', p['opc'], 17)
 
+			# update EF.EHPLMN in ADF.USIM
+			if p.get('mcc') and p.get('mnc'):
+				sw = self.update_ehplmn(p['mcc'], p['mnc'])
+				# igoring any error as the file may not be present depending on the profile.
 		return
 
 
diff --git a/pySim/utils.py b/pySim/utils.py
index 496b918..d7ff617 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -125,6 +125,9 @@
 def hexstr_to_fivebytearr(s):
 	return [s[i:i+10] for i in range(0, len(s), 10) ]
 
+def hexstr_to_threebytearr(s):
+	return [s[i:i+6] for i in range(0, len(s), 6) ]
+
 # Accepts hex string representing three bytes
 def dec_mcc_from_plmn(plmn):
 	ia = h2i(plmn)
@@ -183,6 +186,25 @@
 		s += "\t%s # %s\n" % (rec_data, rec_str)
 	return s
 
+def dec_xplmn(threehexbytes):
+	res = {'mcc': 0, 'mnc': 0, 'act': []}
+	plmn_chars = 6
+	plmn_str = threehexbytes[:plmn_chars]				# first three bytes (six ascii hex chars)
+	res['mcc'] = dec_mcc_from_plmn(plmn_str)
+	res['mnc'] = dec_mnc_from_plmn(plmn_str)
+	return res
+
+def format_xplmn(hexstr):
+	s = ""
+	for rec_data in hexstr_to_threebytearr(hexstr):
+		rec_info = dec_xplmn(rec_data)
+		if rec_info['mcc'] == 0xFFF and rec_info['mnc'] == 0xFFF:
+			rec_str = "unused"
+		else:
+			rec_str = "MCC: %03d MNC: %03d" % (rec_info['mcc'], rec_info['mnc'])
+		s += "\t%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

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I22d96ab4a424ec5bc1fb02f5e80165c646a748d3
Gerrit-Change-Number: 18649
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20200603/f3ed1e69/attachment.htm>


More information about the gerrit-log mailing list