Code style improvements

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@465 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
oepen
2011-07-29 20:06:15 +00:00
parent dfe2ecba2a
commit 60d8d5501b

View File

@@ -17,8 +17,9 @@
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>. # virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
# #
import random, struct, hashlib import struct, hashlib
from pickle import dumps, loads from pickle import dumps, loads
from os import urandom
import TLVutils import TLVutils
import virtualsmartcard.CryptoUtils as vsCrypto import virtualsmartcard.CryptoUtils as vsCrypto
@@ -59,10 +60,7 @@ class CardContainer:
to the corresponding container. to the corresponding container.
""" """
def __init__(self, PIN=None, cardNumber=None, cardSecret=None): def __init__(self, PIN=None, cardSecret=None):
from os import urandom
self.cardNumber = cardNumber
self.PIN = PIN self.PIN = PIN
self.cipher = 0x01 self.cipher = 0x01
self.asym_key = None self.asym_key = None
@@ -90,8 +88,7 @@ class SAM(object):
def __init__(self, PIN, cardNumber, mf=None): def __init__(self, PIN, cardNumber, mf=None):
self.CardContainer = CardContainer(PIN, cardNumber) self.CardContainer = CardContainer(PIN)
#self.CardContainer.loadConfiguration(path, password)
self.mf = mf self.mf = mf
self.cardNumber = cardNumber self.cardNumber = cardNumber
@@ -261,13 +258,7 @@ class SAM(object):
raise SwError(SW["ERR_INCORRECTP1P2"]) raise SwError(SW["ERR_INCORRECTP1P2"])
length = 8 #Length of the challenge in Byte length = 8 #Length of the challenge in Byte
max = "0x" + length * "ff" self.last_challenge = urandom(length)
#cipher = get_referenced_cipher(self.CardContainer.cipher)
#block_size = Crypto.Cipher.cipher.block_size
random.seed()
self.last_challenge = random.randint(0, int(max, 16))
print "Generated challenge: %s" % str(self.last_challenge) print "Generated challenge: %s" % str(self.last_challenge)
self.last_challenge = inttostring(self.last_challenge, length) self.last_challenge = inttostring(self.last_challenge, length)
@@ -302,20 +293,17 @@ class SAM(object):
#We treat global and specific reference data alike #We treat global and specific reference data alike
#elif ((p2 >> 7) == 0x01 or (p2 >> 7) == 0x00): #elif ((p2 >> 7) == 0x01 or (p2 >> 7) == 0x00):
else: else:
try:
#Interpret qualifier as an short fid (try to read the key from FS) #Interpret qualifier as an short fid (try to read the key from FS)
if self.mf == None: if self.mf == None:
raise SwError(SW["CONDITIONSNOTSATISFIED"]) raise SwError(SW["ERR_REFNOTUSABLE"])
df = self.mf.currentDF() df = self.mf.currentDF()
fid = df.select("fid", stringtoint(qualifier)) fid = df.select("fid", stringtoint(qualifier))
key = fid.readbinary(keylength) key = fid.readbinary(keylength)
except SwError:
key = None
if key != None: if key != None:
return key return key
else: else:
raise SwError(SW["DATANOTFOUND"]) raise SwError(SW["ERR_REFNOTUSABLE"])
#The following commands define the interface to the Secure Messaging functions #The following commands define the interface to the Secure Messaging functions
def generate_public_key_pair(self, p1, p2, data): def generate_public_key_pair(self, p1, p2, data):
@@ -400,9 +388,7 @@ class PassportSAM(SAM):
#Extraxt keying material from IFD, generate ICC keying material #Extraxt keying material from IFD, generate ICC keying material
Kifd = plain[16:] Kifd = plain[16:]
rnd_ifd = plain[:8] rnd_ifd = plain[:8]
max = "0x" + 16 * "ff" Kicc = inttostring(urandom(16))
random.seed()
Kicc = inttostring(random.randint(0, int(max, 16)))
#Generate Answer #Generate Answer
data = plain[8:16] + plain[:8] + Kicc data = plain[8:16] + plain[:8] + Kicc
Eicc = vsCrypto.encrypt("DES3-CBC", self.KEnc, data) Eicc = vsCrypto.encrypt("DES3-CBC", self.KEnc, data)
@@ -625,7 +611,7 @@ class Secure_Messaging(object):
@return: Unprotected command APDU @return: Unprotected command APDU
""" """
structure = TLVutils.unpack(CAPDU.data) structure = TLVutils.unpack(CAPDU.data)
return_data = ["",]; return_data = ["",]
expected = self.current_SE.sm_objects expected = self.current_SE.sm_objects
cla = None cla = None