Bug hunting and beautifying with pylint

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@449 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
oepen
2011-07-29 15:33:39 +00:00
parent dc040c7ab9
commit 0aae975fef

View File

@@ -16,13 +16,14 @@
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>. # virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
# #
import virtualsmartcard.SmartcardFilesystem, TLVutils
import random, re, struct#, Crypto import random, struct, hashlib
import hashlib
from virtualsmartcard.SWutils import SwError, SW, SW_MESSAGES
from pickle import dumps, loads from pickle import dumps, loads
from virtualsmartcard.utils import inttostring, stringtoint, hexdump, C_APDU, R_APDU
#from Crypto.Cipher import DES3 import SmartcardFilesystem, TLVutils
import virtualsmartcard.CryptoUtils
from virtualsmartcard.SWutils import SwError, SW
from virtualsmartcard.utils import inttostring, stringtoint, hexdump, C_APDU
from virtualsmartcard.ConstantDefinitions import * from virtualsmartcard.ConstantDefinitions import *
from virtualsmartcard.SEutils import ControlReferenceTemplate as CRT from virtualsmartcard.SEutils import ControlReferenceTemplate as CRT
@@ -60,7 +61,7 @@ class CardContainer:
def __init__(self, PIN=None, cardNumber=None, cardSecret=None): def __init__(self, PIN=None, cardNumber=None, cardSecret=None):
from os import urandom from os import urandom
import virtualsmartcard.CryptoUtils from virtualsmartcard.CryptoUtils import get_cipher_keylen, cipher
self.cardNumber = cardNumber self.cardNumber = cardNumber
self.PIN = PIN self.PIN = PIN
@@ -71,7 +72,7 @@ class CardContainer:
self.salt = None self.salt = None
self.asym_key = None self.asym_key = None
keylen = virtualsmartcard.CryptoUtils.get_cipher_keylen(get_referenced_cipher(self.cipher)) keylen = get_cipher_keylen(get_referenced_cipher(self.cipher))
if cardSecret is None: #Generate a random card secret if cardSecret is None: #Generate a random card secret
self.cardSecret = urandom(keylen) self.cardSecret = urandom(keylen)
else: else:
@@ -96,7 +97,7 @@ class CardContainer:
""" """
Encrypt key and store it in the SAM Encrypt key and store it in the SAM
""" """
crypted_key = virtualsmartcard.CryptoUtils.cipher(True,self.cipher,self.master_password,key) crypted_key = cipher(True, self.cipher, self.master_password, key)
if self.FSkeys.has_key(path): if self.FSkeys.has_key(path):
print "Overwriting key for path %s" % path print "Overwriting key for path %s" % path
self.FSkeys[path] = crypted_key self.FSkeys[path] = crypted_key
@@ -116,7 +117,7 @@ class CardContainer:
@return: key if the key is in the container, otherwise None @return: key if the key is in the container, otherwise None
""" """
if(self.FSkeys.has_key(path)): if(self.FSkeys.has_key(path)):
plain_key = virtualsmartcard.CryptoUtils.cipher(False,self.cipher, self.master_password,self.FSkeys[path]) #TODO: Error checking: plain_key = cipher(False, self.cipher, self.master_password, self.FSkeys[path]) #TODO: Error checking:
return plain_key return plain_key
else: else:
return None return None
@@ -368,7 +369,7 @@ class SAM(object):
key = None key = None
if key == None: #Try to read the key from the Key Container if key == None: #Try to read the key from the Key Container
key = self.CardContainer.getKey(reference_data) key = self.CardContainer.getKey(p2)
if key != None: if key != None:
return key return key
@@ -560,17 +561,15 @@ class Security_Environment(object):
structure = TLVutils.unpack(config) structure = TLVutils.unpack(config)
for tag, length, value in structure: for tag, length, value in structure:
if tag == TEMPLATE_AT: if tag == TEMPLATE_AT:
self.at.parse_config(config) self.at.parse_SE_config(config)
elif tag == TEMPLATE_CRT:
self.crt.parse_config(config)
elif tag == TEMPLATE_CCT: elif tag == TEMPLATE_CCT:
self.cct.parse_config(config) self.cct.parse_SE_config(config)
elif tag == TEMPLATE_KAT: elif tag == TEMPLATE_KAT:
self.kat.parse_config(config) self.kat.parse_SE_config(config)
elif tag == TEMPLATE_CT: elif tag == TEMPLATE_CT:
self.ct.parse_config(config) self.ct.parse_SE_config(config)
elif tag == TEMPLATE_DST: elif tag == TEMPLATE_DST:
self.dst.parse_config(config) self.dst.parse_SE_config(config)
else: else:
raise ValueError raise ValueError
@@ -670,7 +669,7 @@ class Secure_Messaging(object):
""" """
SEstr = self.SAM.get_key(SEID) SEstr = self.SAM.get_key(SEID)
SE = loads(SEstr) SE = loads(SEstr)
if isinstance(SE, SecurityEnvironment): if isinstance(SE, Security_Environment):
self.current_SE = SE self.current_SE = SE
else: else:
raise SwError(SW["ERR_REFNOTUSABLE"]) raise SwError(SW["ERR_REFNOTUSABLE"])
@@ -773,19 +772,16 @@ class Secure_Messaging(object):
auth = virtualsmartcard.CryptoUtils.append_padding("DES-CBC", to_authenticate) auth = virtualsmartcard.CryptoUtils.append_padding("DES-CBC", to_authenticate)
sw, checksum = self.compute_cryptographic_checksum(0x8E, 0x80, auth) sw, checksum = self.compute_cryptographic_checksum(0x8E, 0x80, auth)
if checksum != value: if checksum != value:
print "Failed to verify checksum!"
raise SwError(SW["ERR_SECMESSOBJECTSINCORRECT"]) raise SwError(SW["ERR_SECMESSOBJECTSINCORRECT"])
elif tag == SM_Class["DIGITAL_SIGNATURE"]: elif tag == SM_Class["DIGITAL_SIGNATURE"]:
auth = to_authenticate #FIXME: Need padding? auth = to_authenticate #FIXME: Need padding?
sw, signature = self.compute_digital_signature(0x9E, 0x9A, auth) sw, signature = self.compute_digital_signature(0x9E, 0x9A, auth)
if signature != value: if signature != value:
print "Failed to verify signature!" raise SwError(SW["ERR_SECMESSOBJECTSINCORRECT"])
raise SwEroor(SW["ERR_SECMESSOBJECTSINCORRECT"])
elif tag in (SM_Class["HASH_CODE"],SM_Class["HASH_CODE_ODD"]): elif tag in (SM_Class["HASH_CODE"],SM_Class["HASH_CODE_ODD"]):
sw, hash = self.hash(p1, p2, to_authenticate) sw, hash = self.hash(p1, p2, to_authenticate)
if hash != value: if hash != value:
print "Failed to verify hash!" raise SwError(SW["ERR_SECMESSOBJECTSINCORRECT"])
raise SwEroor(SW["ERR_SECMESSOBJECTSINCORRECT"])
#Check if we just parsed a expected SM Object: #Check if we just parsed a expected SM Object:
pos = 0 pos = 0
@@ -897,7 +893,7 @@ class Secure_Messaging(object):
if p1 != 0x8E or p2 != 0x80: if p1 != 0x8E or p2 != 0x80:
raise SwError(SW["ERR_INCORRECTP1P2"]) raise SwError(SW["ERR_INCORRECTP1P2"])
if self.current_SE.cct.key == None: if self.current_SE.cct.key == None:
raise SwError(SE["ERR_CONDITIONNOTSATISFIED"]) raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
checksum = virtualsmartcard.CryptoUtils.crypto_checksum(self.current_SE.cct.algorithm, checksum = virtualsmartcard.CryptoUtils.crypto_checksum(self.current_SE.cct.algorithm,
self.current_SE.cct.key, self.current_SE.cct.key,
@@ -917,7 +913,7 @@ class Secure_Messaging(object):
if p1 != 0x9E or not p2 in (0x9A,0xAC,0xBC): if p1 != 0x9E or not p2 in (0x9A,0xAC,0xBC):
raise SwError(SW["ERR_INCORRECTP1P2"]) raise SwError(SW["ERR_INCORRECTP1P2"])
if self.current_SE.dst.key == None: if self.current_SE.dst.key == None:
raise SwError(SE["ERR_CONDITIONNOTSATISFIED"]) raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
if p2 == 0x9A: #Data to be signed if p2 == 0x9A: #Data to be signed
to_sign = data to_sign = data
@@ -964,7 +960,7 @@ class Secure_Messaging(object):
if algo == None or key == None: if algo == None or key == None:
raise SwError(SW["ERR_CONDITIONNOTSATISFIED"]) raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
structure = TLVutils.unpack(config) structure = TLVutils.unpack(data)
for tag, length, value in structure: for tag, length, value in structure:
if tag == 0x80: if tag == 0x80:
plain = value plain = value
@@ -991,7 +987,7 @@ class Secure_Messaging(object):
if key == None: if key == None:
raise SwError(SW["ERR_CONDITIONNOTSATISFIED"]) raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
structure = TLVutils.unpack(config) structure = TLVutils.unpack(data)
for tag, length, value in structure: for tag, length, value in structure:
if tag == 0x9E: if tag == 0x9E:
signature = value signature = value