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

2019-09-04 Thread laforge
laforge has submitted this change and it was merged. ( 
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 pin_adm in the file
Also require iccid as identifier for the SIM card
Set defaults for optional card parameters.

Change-Id: I0d317ea51d0cf582b82157eec6cdec074001a236
---
A csv-format
M pySim-prog.py
2 files changed, 44 insertions(+), 15 deletions(-)

Approvals:
  Jenkins Builder: Verified
  dexter: Looks good to me, but someone else must approve; Verified
  laforge: Looks good to me, approved



diff --git a/csv-format b/csv-format
new file mode 100644
index 000..808003a
--- /dev/null
+++ b/csv-format
@@ -0,0 +1,16 @@
+This file aims to describe the format of the CSV file pySim uses.
+
+The first line contains the fieldnames which will be used by pySim. This
+avoids having a specific order.
+
+The field names are the following:
+
+iccid: ICCID of the card. Used to identify the cards (with --read-iccid)
+imsi: IMSI of the card
+mcc: Mobile Country Code (optional)
+mnc: Mobile Network Code (optional)
+smsp: MSISDN of the SMSC (optional)
+ki: Ki
+opc: OPc
+acc: Access class of the SIM (optional)
+pin_adm: Admin PIN of the SIM. Needed to reprogram various files
diff --git a/pySim-prog.py b/pySim-prog.py
index 7edadd7..e00c2d0 100755
--- a/pySim-prog.py
+++ b/pySim-prog.py
@@ -405,17 +405,20 @@

 def print_parameters(params):

-   print """Generated card parameters :
- > Name : %(name)s
- > SMSP : %(smsp)s
- > ICCID: %(iccid)s
- > MCC/MNC  : %(mcc)d/%(mnc)d
- > IMSI : %(imsi)s
- > Ki   : %(ki)s
- > OPC  : %(opc)s
- > ACC  : %(acc)s
- > ADM1(hex): %(pin_adm)s
-"""% params
+   s = ["Generated card parameters :"]
+   if 'name' in params:
+   s.append(" > Name : %(name)s")
+   if 'smsp' in params:
+   s.append(" > SMSP : %(smsp)s")
+   s.append(" > ICCID: %(iccid)s")
+   s.append(" > MCC/MNC  : %(mcc)d/%(mnc)d")
+   s.append(" > IMSI : %(imsi)s")
+   s.append(" > Ki   : %(ki)s")
+   s.append(" > OPC  : %(opc)s")
+   if 'acc' in params:
+   s.append(" > ACC  : %(acc)s")
+   s.append(" > ADM1(hex): %(pin_adm)s")
+   print("\n".join(s) % params)


 def write_params_csv(opts, params):
@@ -430,10 +433,11 @@

 def _read_params_csv(opts, imsi):
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:
@@ -450,8 +454,17 @@
 def read_params_csv(opts, imsi):
row = _read_params_csv(opts, imsi)
if row is not None:
-   row['mcc'] = int(row['mcc'])
-   row['mnc'] = int(row['mnc'])
+   row['mcc'] = int(row.get('mcc', row['imsi'][0:3]))
+   row['mnc'] = int(row.get('mnc', row['imsi'][3:5]))
+   pin_adm = None
+   # We need to escape the pin_adm we get from the csv
+   if 'pin_adm' in row:
+   pin_adm = ''.join(['%02x'%(ord(x)) for x in 
row['pin_adm']])
+   # Stay compatible to the odoo csv format
+   elif 'adm1' in row:
+   pin_adm = ''.join(['%02x'%(ord(x)) for x in 
row['adm1']])
+   if pin_adm:
+   row['pin_adm'] = rpad(pin_adm, 16)
return row



--
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: 3
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: lynxis lazus 
Gerrit-CC: daniel 
Gerrit-CC: pespin 
Gerrit-MessageType: merged


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

2019-09-04 Thread laforge
laforge has posted comments on this change. ( 
https://gerrit.osmocom.org/c/pysim/+/15313 )

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


Patch Set 3: Code-Review+2


--
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: 3
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter 
Gerrit-Reviewer: laforge 
Gerrit-Reviewer: lynxis lazus 
Gerrit-CC: daniel 
Gerrit-CC: pespin 
Gerrit-Comment-Date: Wed, 04 Sep 2019 18:50:08 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


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

2019-09-04 Thread dexter
dexter has posted comments on this change. ( 
https://gerrit.osmocom.org/c/pysim/+/15313 )

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


Patch Set 3: Verified+1 Code-Review+1

> Patch Set 3:
>
> I refactored the code and split it up into different commits as well. There 
> were quite a few corner cases we weren't handling before, and there should be 
> less now.

I have tested it with a CSV file, seems to work fine for me.


--
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: 3
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter 
Gerrit-CC: daniel 
Gerrit-CC: pespin 
Gerrit-Comment-Date: Wed, 04 Sep 2019 09:31:57 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


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

2019-09-03 Thread daniel
daniel has posted comments on this change. ( 
https://gerrit.osmocom.org/c/pysim/+/15313 )

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


Patch Set 3:

I refactored the code and split it up into different commits as well. There 
were quite a few corner cases we weren't handling before, and there should be 
less now.


--
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: 3
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter 
Gerrit-CC: daniel 
Gerrit-CC: pespin 
Gerrit-Comment-Date: Tue, 03 Sep 2019 18:19:21 +
Gerrit-HasComments: No
Gerrit-Has-Labels: No
Gerrit-MessageType: comment


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

2019-09-03 Thread daniel
daniel has uploaded a new patch set (#3) to the change originally created by 
laforge. ( 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 pin_adm in the file
Also require iccid as identifier for the SIM card
Set defaults for optional card parameters.

Change-Id: I0d317ea51d0cf582b82157eec6cdec074001a236
---
A csv-format
M pySim-prog.py
2 files changed, 44 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/13/15313/3
--
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: 3
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter 
Gerrit-CC: pespin 
Gerrit-MessageType: newpatchset


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

2019-09-03 Thread dexter
dexter has posted comments on this change. ( 
https://gerrit.osmocom.org/c/pysim/+/15313 )

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


Patch Set 2: Code-Review+1

I fixed the problem. Now it passes the tests.


--
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: 2
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter 
Gerrit-CC: pespin 
Gerrit-Comment-Date: Tue, 03 Sep 2019 09:52:43 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


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

2019-09-03 Thread dexter
dexter has uploaded a new patch set (#2) to the change originally created by 
laforge. ( 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/2
--
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: 2
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter 
Gerrit-CC: pespin 
Gerrit-MessageType: newpatchset


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

2019-09-02 Thread dexter
dexter has posted comments on this change. ( 
https://gerrit.osmocom.org/c/pysim/+/15313 )

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


Patch Set 1:

(1 comment)

I think I found whats wrong. (See comment)

https://gerrit.osmocom.org/#/c/15313/1/pySim/cards.py
File pySim/cards.py:

https://gerrit.osmocom.org/#/c/15313/1/pySim/cards.py@572
PS1, Line 572:  self._scc.verify_chv(0x0A, p['adm1'])
I think this should be: self._scc.verify_chv(0x0A, h2b(p['adm1']))

This also explains the problems with sysmo-usim-tool. The authentication here 
fails, and then the tests for sysmo-usim-tool also block because 
sysmo-usim-tool detects that only 2 or less authentication attempts are left. 
PySim won't complain, after the 3rd run of the test the card will lock down.



--
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 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter 
Gerrit-CC: pespin 
Gerrit-Comment-Date: Mon, 02 Sep 2019 11:47:57 +
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment


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

2019-08-28 Thread laforge
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(""))

# Authenticate using ADM PIN 5
-   if p['pin_adm']:
-   pin = h2b(p['pin_adm'])
+   if p['adm1']:
+   pin = h2b(p['adm1'])
else:
pin = h2b("")
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("")

@@ -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
Gerr