Code style improvements

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@467 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
oepen
2011-07-29 20:40:38 +00:00
parent be448f9001
commit 28972ee5be

View File

@@ -17,7 +17,7 @@
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>. # virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
# #
import sys, getpass, anydbm, readline, os, dircache import sys, getpass, anydbm, readline, dircache
from pickle import loads, dumps from pickle import loads, dumps
from TLVutils import pack from TLVutils import pack
from virtualsmartcard.utils import inttostring from virtualsmartcard.utils import inttostring
@@ -36,10 +36,10 @@ from virtualsmartcard.SmartcardFilesystem import *
class CardGenerator(object): class CardGenerator(object):
def __init__(self, type=None, sam=None, mf=None): def __init__(self, card_type=None, sam=None, mf=None):
self.type = type self.type = card_type
self.mf = None self.mf = mf
self.sam = None self.sam = sam
def __generate_iso_card(self): def __generate_iso_card(self):
from virtualsmartcard.SmartcardSAM import SAM from virtualsmartcard.SmartcardSAM import SAM
@@ -47,80 +47,90 @@ class CardGenerator(object):
print "Using default SAM. Insecure!!!" print "Using default SAM. Insecure!!!"
self.sam = SAM("1234", "1234567890") #FIXME: Use user provided data self.sam = SAM("1234", "1234567890") #FIXME: Use user provided data
self.mf = MF(filedescriptor=FDB["DF"], lifecycle=LCB["ACTIVATED"], dfname=None) self.mf = MF(filedescriptor=FDB["DF"])
self.sam.set_MF(self.mf) self.sam.set_MF(self.mf)
def __generate_ePass(self): def __generate_ePass(self):
from PIL import Image from PIL import Image
from virtualsmartcard.SmartcardSAM import PassportSAM from virtualsmartcard.SmartcardSAM import PassportSAM
MRZ = raw_input("Please enter the MRZ as one string: ") #TODO: Sanity checks MRZ = raw_input("Please enter the MRZ as one string: ") #TODO: Sanity checks
readline.set_completer_delims("") readline.set_completer_delims("")
readline.parse_and_bind("tab: complete") readline.parse_and_bind("tab: complete")
picturepath = raw_input("Please enter the path to an image: ") picturepath = raw_input("Please enter the path to an image: ")
picturepath = picturepath.rstrip() #FIXME picturepath = picturepath.rstrip() #FIXME
#MRZ1 = "P<UTOERIKSSON<<ANNA<MARIX<<<<<<<<<<<<<<<<<<<" #MRZ1 = "P<UTOERIKSSON<<ANNA<MARIX<<<<<<<<<<<<<<<<<<<"
#MRZ2 = "L898902C<3UTO6908061F9406236ZE184226B<<<<<14" #MRZ2 = "L898902C<3UTO6908061F9406236ZE184226B<<<<<14"
#MRZ = MRZ1 + MRZ2 #MRZ = MRZ1 + MRZ2
try: try:
im = Image.open(picturepath) im = Image.open(picturepath)
pic_width, pic_height = im.size pic_width, pic_height = im.size
fd = open(picturepath,"rb") fd = open(picturepath,"rb")
picture = fd.read() picture = fd.read()
fd.close() fd.close()
except IOError: except IOError:
print "Failed to open file: " + picturepath print "Failed to open file: " + picturepath
pic_width = 0 pic_width = 0
pic_height = 0 pic_height = 0
picture = None picture = None
mf = MF() mf = MF()
#We need a MF with Application DF \xa0\x00\x00\x02G\x10\x01 #We need a MF with Application DF \xa0\x00\x00\x02G\x10\x01
df = DF(parent=mf, fid=4, dfname='\xa0\x00\x00\x02G\x10\x01', bertlv_data=[]) df = DF(parent=mf, fid=4, dfname='\xa0\x00\x00\x02G\x10\x01',
bertlv_data=[])
#EF.COM #EF.COM
COM = pack([(0x5F01,4,"0107"),(0x5F36,6,"040000"),(0x5C,2,"6175")]) COM = pack([(0x5F01, 4, "0107"), (0x5F36, 6, "040000"),
COM = pack(((0x60,len(COM),COM),)) (0x5C, 2, "6175")])
df.append(TransparentStructureEF(parent=df, fid=0x011E, filedescriptor=0, data=COM)) COM = pack(((0x60, len(COM), COM),))
df.append(TransparentStructureEF(parent=df, fid=0x011E,
filedescriptor=0, data=COM))
#EF.DG1 #EF.DG1
DG1 = pack([(0x5F1F,len(MRZ),MRZ)]) DG1 = pack([(0x5F1F, len(MRZ), MRZ)])
DG1 = pack([(0x61,len(DG1),DG1)]) DG1 = pack([(0x61, len(DG1), DG1)])
df.append(TransparentStructureEF(parent=df, fid=0x0101, filedescriptor=0, data=DG1)) df.append(TransparentStructureEF(parent=df, fid=0x0101,
filedescriptor=0, data=DG1))
#EF.DG2 #EF.DG2
if picture != None: if picture != None:
IIB = "\x00\x01" + inttostring(pic_width,2) + inttostring(pic_height,2) + 6 * "\x00" IIB = "\x00\x01" + inttostring(pic_width, 2) +\
length = 32 + len(picture) #32 is the length of IIB + FIB inttostring(pic_height, 2) + 6 * "\x00"
FIB = inttostring(length,4) + 16 * "\x00" length = 32 + len(picture) #32 is the length of IIB + FIB
FRH = "FAC" + "\x00" + "010" + "\x00" + inttostring(14+length,4) + inttostring(1,2) FIB = inttostring(length, 4) + 16 * "\x00"
picture = FRH + FIB + IIB + picture FRH = "FAC" + "\x00" + "010" + "\x00" +\
DG2 = pack([(0xA1,8,"\x87\x02\x01\x01\x88\x02\x05\x01"),(0x5F2E,len(picture),picture)]) inttostring(14 + length, 4) + inttostring(1, 2)
DG2 = pack([(0x02,1,"\x01"),(0x7F60,len(DG2),DG2)]) picture = FRH + FIB + IIB + picture
DG2 = pack([(0x7F61,len(DG2),DG2)]) DG2 = pack([(0xA1, 8, "\x87\x02\x01\x01\x88\x02\x05\x01"),
else: (0x5F2E, len(picture), picture)])
DG2="" DG2 = pack([(0x02, 1, "\x01"), (0x7F60, len(DG2), DG2)])
df.append(TransparentStructureEF(parent=df, fid=0x0102, filedescriptor=0, data=DG2)) DG2 = pack([(0x7F61, len(DG2), DG2)])
else:
DG2 = ""
df.append(TransparentStructureEF(parent=df, fid=0x0102,
filedescriptor=0, data=DG2))
#EF.SOD #EF.SOD
df.append(TransparentStructureEF(parent=df, fid=0x010D, filedescriptor=0, data="")) df.append(TransparentStructureEF(parent=df, fid=0x010D,
filedescriptor=0, data=""))
mf.append(df) mf.append(df)
self.mf = mf self.mf = mf
self.sam = PassportSAM(self.mf) self.sam = PassportSAM(self.mf)
def __generate_cryptoflex(self): def __generate_cryptoflex(self):
from virtualsmartcard.SmartcardSAM import CryptoflexSAM from virtualsmartcard.SmartcardSAM import CryptoflexSAM
self.mf = CryptoflexMF() self.mf = CryptoflexMF()
self.mf.append(TransparentStructureEF(parent=self.mf, fid=0x0002, filedescriptor=0x01, self.mf.append(TransparentStructureEF(parent=self.mf, fid=0x0002,
data="\x00\x00\x00\x01\x00\x01\x00\x00")) #EF.ICCSN filedescriptor=0x01,
data="\x00\x00\x00\x01\x00\x01\x00\x00")) #EF.ICCSN
self.sam = CryptoflexSAM(self.mf) self.sam = CryptoflexSAM(self.mf)
def generateCard(self): def generateCard(self):