From 1e7492a8068610c26dd7390ea3592a35724bbc6f Mon Sep 17 00:00:00 2001 From: Dominik Date: Sun, 21 Dec 2014 16:06:32 +0100 Subject: [PATCH 1/5] Remove superfluous whitespaces --- .../vpicc/virtualsmartcard/CardGenerator.py | 79 ++++++++----------- 1 file changed, 33 insertions(+), 46 deletions(-) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py index 025f348..5ff1841 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py @@ -1,18 +1,18 @@ # # Copyright (C) 2011 Dominik Oepen -# +# # This file is part of virtualsmartcard. -# +# # virtualsmartcard is free software: you can redistribute it and/or modify it # under the terms of the GNU General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) any # later version. -# +# # virtualsmartcard is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. -# +# # You should have received a copy of the GNU General Public License along with # virtualsmartcard. If not, see . # @@ -39,52 +39,52 @@ except ImportError: #self.mf.append(DF(parent=self.mf, #fid=2, dfname='\xa0\x00\x00\x03\x08\x00\x00\x10\x00')) #self.mf.append(DF(parent=self.mf, - #fid=3, dfname='\xa0\x00\x00\x03\x08\x00\x00\x10\x00\x01\x00')) + #fid=3, dfname='\xa0\x00\x00\x03\x08\x00\x00\x10\x00\x01\x00')) class CardGenerator(object): - """This class is used to generate the SAM and filesystem for the + """This class is used to generate the SAM and filesystem for the different supported card types. It is also able used for persistent storage (in encrypted form) of the card on disks. """ - + def __init__(self, card_type=None, sam=None, mf=None): self.type = card_type self.mf = mf self.sam = sam self.password = None self.datagroups = {} - + def __generate_iso_card(self): default_pin = "1234" default_cardno = "1234567890" - + logging.warning("Using default SAM parameters. PIN=%s, Card Nr=%s" % (default_pin, default_cardno)) #TODO: Use user provided data self.sam = SAM(default_pin, default_cardno) - + self.mf = MF(filedescriptor=FDB["DF"]) self.sam.set_MF(self.mf) - + def __generate_ePass(self): - """Generate the MF and SAM of an ICAO passport. This method is + """Generate the MF and SAM of an ICAO passport. This method is responsible for generating the filesystem and filling it with content. Therefore it must interact with the user by prompting for the MRZ and optionally for the path to a photo.""" from PIL import Image from virtualsmartcard.cards.ePass import PassportSAM - + #TODO: Sanity checks MRZ = raw_input("Please enter the MRZ as one string: ") readline.set_completer_delims("") readline.parse_and_bind("tab: complete") - + picturepath = raw_input("Please enter the path to an image: ") picturepath = picturepath.strip() - + #MRZ1 = "P Date: Sun, 21 Dec 2014 16:09:26 +0100 Subject: [PATCH 2/5] Minor improvements to CardGenerator and the corresponding unit test Make sure that the file gets closed when writing a card to/reading a card from disk. Also add a unit test for the error condition when trying to import a card from a nonexistent file. --- .../vpicc/virtualsmartcard/CardGenerator.py | 25 +++++++++++-------- .../tests/CardGenerator_test.py | 14 +++++++---- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py index 5ff1841..b810af5 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py @@ -382,16 +382,19 @@ class CardGenerator(object): def loadCard(self, filename): """Load a card from disk""" - db = anydbm.open(filename, 'r') - if self.password is None: self.password = getpass.getpass("Please enter your password:") - serializedMF = read_protected_string(db["mf"], self.password) - serializedSAM = read_protected_string(db["sam"], self.password) + db = anydbm.open(filename, 'r') + try: + serializedMF = read_protected_string(db["mf"], self.password) + serializedSAM = read_protected_string(db["sam"], self.password) + self.type = db["type"] + finally: + db.close() + self.sam = loads(serializedSAM) self.mf = loads(serializedMF) - self.type = db["type"] def saveCard(self, filename): """Save the currently running card to disk""" @@ -413,11 +416,13 @@ class CardGenerator(object): protectedSAM = protect_string(sam_string, self.password) db = anydbm.open(filename, 'c') - db["mf"] = protectedMF - db["sam"] = protectedSAM - db["type"] = self.type - db["version"] = "0.1" - db.close() + try: + db["mf"] = protectedMF + db["sam"] = protectedSAM + db["type"] = self.type + db["version"] = "0.1" + finally: + db.close() if __name__ == "__main__": from optparse import OptionParser diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/tests/CardGenerator_test.py b/virtualsmartcard/src/vpicc/virtualsmartcard/tests/CardGenerator_test.py index b9bf39f..ecb8be2 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/tests/CardGenerator_test.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/tests/CardGenerator_test.py @@ -17,9 +17,10 @@ # virtualsmartcard. If not, see . # -import unittest -import tempfile +import anydbm import os +import tempfile +import unittest from virtualsmartcard.CardGenerator import CardGenerator class TestNPACardGenerator(unittest.TestCase): @@ -29,15 +30,13 @@ class TestNPACardGenerator(unittest.TestCase): self.nPA_generator = CardGenerator('nPA') self.nPA_generator.password = "TestPassword" - def tearDown(self): - os.unlink(self.filename) - def test_nPA_creation(self): self.nPA_generator.generateCard() self.nPA_generator.saveCard(self.filename) mf, sam = self.nPA_generator.getCard() self.assertIsNotNone(mf) self.assertIsNotNone(sam) + os.unlink(self.filename) def test_load_nPA_from_file_nPA_from_file(self): self.nPA_generator.generateCard() @@ -48,6 +47,11 @@ class TestNPACardGenerator(unittest.TestCase): mf, sam = local_generator.getCard() self.assertIsNotNone(mf) self.assertIsNotNone(sam) + os.unlink(self.filename) + + def test_load_nonexistent_file(self): + with self.assertRaises(anydbm.error): + self.nPA_generator.loadCard(self.filename) if __name__ == '__main__': unittest.main() From 5cb1c6ad584db33c17237c77e2d84d6f1dc24313 Mon Sep 17 00:00:00 2001 From: Dominik Date: Tue, 23 Dec 2014 11:16:58 +0100 Subject: [PATCH 3/5] Restore readDatagroups function accidently deleted in 1e7492a8068610c26dd7390ea3592a35724bbc6f --- .../src/vpicc/virtualsmartcard/CardGenerator.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py index b810af5..2487b96 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py @@ -424,6 +424,18 @@ class CardGenerator(object): finally: db.close() + def readDatagroups(self, datasetfile): + """Read Datagroups from file""" + with open(datasetfile, 'r') as f: + for line in f: + if (not line.startswith("#")) and (not len(line.strip()) == 0): + # spaces after equal sign are allowed to get strings with leading spaces! + line=line.replace(" =", "=") + splitLine = line.split("=") + # we don't want to have the newline char from dataset file as part of the value!! + self.datagroups[splitLine[0]] = splitLine[1].rstrip("\n\r") + f.close() + if __name__ == "__main__": from optparse import OptionParser parser = OptionParser() From 8ea1c74e2f0f707bf8f80947dab5a23495f9607a Mon Sep 17 00:00:00 2001 From: Dominik Date: Tue, 23 Dec 2014 11:36:29 +0100 Subject: [PATCH 4/5] Fix indention --- .../vpicc/virtualsmartcard/CardGenerator.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py index 2487b96..0987314 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py @@ -408,7 +408,7 @@ class CardGenerator(object): if self.mf == None or self.sam == None: raise ValueError("Card Generator wasn't set up properly" +\ - "(missing MF or SAM).") + "(missing MF or SAM).") mf_string = dumps(self.mf) sam_string = dumps(self.sam) @@ -427,14 +427,14 @@ class CardGenerator(object): def readDatagroups(self, datasetfile): """Read Datagroups from file""" with open(datasetfile, 'r') as f: - for line in f: - if (not line.startswith("#")) and (not len(line.strip()) == 0): - # spaces after equal sign are allowed to get strings with leading spaces! - line=line.replace(" =", "=") - splitLine = line.split("=") - # we don't want to have the newline char from dataset file as part of the value!! - self.datagroups[splitLine[0]] = splitLine[1].rstrip("\n\r") - f.close() + for line in f: + if (not line.startswith("#")) and (not len(line.strip()) == 0): + # spaces after equal sign are allowed to get strings with leading spaces! + line=line.replace(" =", "=") + splitLine = line.split("=") + # we don't want to have the newline char from dataset file as part of the value!! + self.datagroups[splitLine[0]] = splitLine[1].rstrip("\n\r") + f.close() if __name__ == "__main__": from optparse import OptionParser From d90914edb079be393aba87ad432d390beafb0b2d Mon Sep 17 00:00:00 2001 From: Dominik Date: Tue, 23 Dec 2014 12:10:07 +0100 Subject: [PATCH 5/5] Fix incorrect indention of close statement Commit 8ea1c74e2f0f707bf8f80947dab5a23495f9607a moved the f.close() statement to the wrong indention level, thereby closing the file prematurely when reading Datagroups from a file. Since files are closed automatically due to the with statement, we just remove the explicit call to close(). --- virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py | 1 - 1 file changed, 1 deletion(-) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py index 0987314..e7a4eba 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py @@ -434,7 +434,6 @@ class CardGenerator(object): splitLine = line.split("=") # we don't want to have the newline char from dataset file as part of the value!! self.datagroups[splitLine[0]] = splitLine[1].rstrip("\n\r") - f.close() if __name__ == "__main__": from optparse import OptionParser