Change in ...pysim[master]: pySim-prog: Use CSV format with headers

laforge gerrit-no-reply at lists.osmocom.org
Wed Aug 28 21:22:26 UTC 2019


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


Change subject: pySim-prog: Use CSV format with headers
......................................................................

pySim-prog: Use CSV format with headers

This way we can have optional fields like adm1 in the file
Also require iccid as identifier for the SIM card

Change-Id: I0d317ea51d0cf582b82157eec6cdec074001a236
---
M pySim-prog.py
M pySim/cards.py
2 files changed, 22 insertions(+), 14 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/13/15313/1

diff --git a/pySim-prog.py b/pySim-prog.py
index 2638eef..dde9194 100755
--- a/pySim-prog.py
+++ b/pySim-prog.py
@@ -399,7 +399,7 @@
 		'ki'	: ki,
 		'opc'	: opc,
 		'acc'	: acc,
-		'pin_adm' : pin_adm,
+		'adm1' : pin_adm,
 	}
 
 
@@ -414,6 +414,7 @@
  > Ki      : %(ki)s
  > OPC     : %(opc)s
  > ACC     : %(acc)s
+ > ADM1    : %(adm1)s
 """	% params
 
 
@@ -427,18 +428,23 @@
 		cw.writerow([params[x] for x in row])
 		f.close()
 
-def _read_params_csv(opts, imsi):
+def _read_params_csv(opts, iccid=None, imsi=None):
 	import csv
-	row = ['name', 'iccid', 'mcc', 'mnc', 'imsi', 'smsp', 'ki', 'opc']
 	f = open(opts.read_csv, 'r')
-	cr = csv.DictReader(f, row)
+	cr = csv.DictReader(f)
 	i = 0
+        if not 'iccid' in cr.fieldnames:
+            raise Exception("CSV file in wrong format!")
 	for row in cr:
 		if opts.num is not None and opts.read_imsi is False:
 			if opts.num == i:
 				f.close()
 				return row;
 			i += 1
+		if row['iccid'] == iccid:
+			f.close()
+			return row;
+
 		if row['imsi'] == imsi:
 			f.close()
 			return row;
@@ -446,8 +452,8 @@
 	f.close()
 	return None
 
-def read_params_csv(opts, imsi):
-	row = _read_params_csv(opts, imsi)
+def read_params_csv(opts, imsi=None, iccid=None):
+	row = _read_params_csv(opts, iccid=iccid, imsi=imsi)
 	if row is not None:
 		row['mcc'] = int(row['mcc'])
 		row['mnc'] = int(row['mnc'])
@@ -628,7 +634,9 @@
 		if opts.source == 'cmdline':
 			cp = gen_parameters(opts)
 		elif opts.source == 'csv':
-			if opts.read_imsi:
+                        imsi = None
+                        iccid = None
+                        if opts.read_imsi:
 				if opts.dry_run:
 					# Connect transport
 					print "Insert card now (or CTRL-C to cancel)"
@@ -637,7 +645,7 @@
 				imsi = swap_nibbles(res)[3:]
 			else:
 				imsi = opts.imsi
-			cp = read_params_csv(opts, imsi)
+			cp = read_params_csv(opts, imsi=imsi, iccid=iccid)
 		if cp is None:
 			print "Error reading parameters\n"
 			sys.exit(2)
diff --git a/pySim/cards.py b/pySim/cards.py
index 55282aa..a850e5a 100644
--- a/pySim/cards.py
+++ b/pySim/cards.py
@@ -375,8 +375,8 @@
 		#self._scc.verify_chv(4, h2b("4444444444444444"))
 
 		# Authenticate using ADM PIN 5
-		if p['pin_adm']:
-			pin = h2b(p['pin_adm'])
+		if p['adm1']:
+			pin = h2b(p['adm1'])
 		else:
 			pin = h2b("4444444444444444")
 		self._scc.verify_chv(5, pin)
@@ -495,8 +495,8 @@
 		# P1: 3A for PIN, 3B for PUK
 		# P2: CHV number, as in VERIFY CHV for PIN, and as in UNBLOCK CHV for PUK
 		# P3: 08, CHV length (curiously the PUK is also 08 length, instead of 10)
-		if p['pin_adm']:
-			pin = p['pin_adm']
+		if p['adm1']:
+			pin = p['adm1']
 		else:
 			pin = h2b("4444444444444444")
 
@@ -567,9 +567,9 @@
 	def program(self, p):
 
 		# authenticate as ADM using default key (written on the card..)
-		if not p['pin_adm']:
+		if not p['adm1']:
 			raise ValueError("Please provide a PIN-ADM as there is no default one")
-		self._scc.verify_chv(0x0A, h2b(p['pin_adm']))
+		self._scc.verify_chv(0x0A, p['adm1'])
 
 		# select MF
 		r = self._scc.select_file(['3f00'])

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

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I0d317ea51d0cf582b82157eec6cdec074001a236
Gerrit-Change-Number: 15313
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge at gnumonks.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20190828/de217e06/attachment.html>


More information about the gerrit-log mailing list