Better exception handling

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@471 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
oepen
2011-07-30 13:13:46 +00:00
parent c908fa9436
commit ae54cf5e18
2 changed files with 18 additions and 19 deletions

View File

@@ -110,7 +110,7 @@ class SAM(object):
@param keytype: Type of the public key (e.g. RSA, DSA) @param keytype: Type of the public key (e.g. RSA, DSA)
""" """
if not keytype in range(0x07, 0x08): if not keytype in range(0x07, 0x08):
raise ValueError, "Illegal Parameter" raise SwError(SW["ERR_INCORRECTP1P2"])
else: else:
self.cipher = type self.cipher = type
self.asym_key = cipher self.asym_key = cipher
@@ -134,9 +134,7 @@ class SAM(object):
return SW["NORMAL"], "" return SW["NORMAL"], ""
else: else:
self.counter -= 1 self.counter -= 1
print self.PIN() + " != " + PIN
raise SwError(SW["WARN_NOINFO63"]) raise SwError(SW["WARN_NOINFO63"])
#raise SwError(0X63C0 + self.counter) #Be verbose
else: else:
raise SwError(SW["ERR_AUTHBLOCKED"]) raise SwError(SW["ERR_AUTHBLOCKED"])
@@ -472,7 +470,7 @@ class Security_Environment(object):
elif tag == CRT_TEMPLATE["DST"]: elif tag == CRT_TEMPLATE["DST"]:
self.dst.parse_SE_config(config) self.dst.parse_SE_config(config)
else: else:
raise ValueError raise SwError(SW["ERR_REFNOTUSABLE"])
class Secure_Messaging(object): class Secure_Messaging(object):
@@ -614,9 +612,11 @@ class Secure_Messaging(object):
for tlv in structure: for tlv in structure:
tag, length, value = tlv tag, length, value = tlv
#TODO: Sanity checking
#if not SM_Class.has_key(tag): #Sanity checking
# raise ValueError, "unknown Secure messaging tag %#X" % tag if not SM_Class.has_key(tag):
raise SwError(SW["ERR_SECMESSOBJECTSINCORRECT"])
if tag % 2 == 1: #Include object in checksum calculation if tag % 2 == 1: #Include object in checksum calculation
to_authenticate += inttostring(tag) + inttostring(length) + value to_authenticate += inttostring(tag) + inttostring(length) + value
@@ -624,21 +624,20 @@ class Secure_Messaging(object):
if tag in (SM_Class["PLAIN_VALUE_NO_TLV"], if tag in (SM_Class["PLAIN_VALUE_NO_TLV"],
SM_Class["PLAIN_VALUE_NO_TLV_ODD"]): SM_Class["PLAIN_VALUE_NO_TLV_ODD"]):
return_data.append(value) #FIXME: Need TLV coding? return_data.append(value) #FIXME: Need TLV coding?
elif tag in (SM_Class["PLAIN_VALUE_TLV_INCULDING_SM"],
SM_Class["PLAIN_VALUE_TLV_INCULDING_SM_ODD"]):
#Encapsulated SM objects. Parse them #Encapsulated SM objects. Parse them
#FIXME: Need to pack value into a dummy CAPDU #FIXME: Need to pack value into a dummy CAPDU
elif tag in (SM_Class["PLAIN_VALUE_TLV_INCULDING_SM"],
SM_Class["PLAIN_VALUE_TLV_INCULDING_SM_ODD"]):
return_data.append(self.parse_SM_CAPDU(value, header_authentication)) return_data.append(self.parse_SM_CAPDU(value, header_authentication))
#Encapsulated plaintext BER-TLV objects
elif tag in (SM_Class["PLAIN_VALUE_TLV_NO_SM"], elif tag in (SM_Class["PLAIN_VALUE_TLV_NO_SM"],
SM_Class["PLAIN_VALUE_TLV_NO_SM_ODD"]): SM_Class["PLAIN_VALUE_TLV_NO_SM_ODD"]):
#Encapsulated plaintext BER-TLV objects
return_data.append(value) return_data.append(value)
elif tag in (SM_Class["Ne"], SM_Class["Ne_ODD"]): elif tag in (SM_Class["Ne"], SM_Class["Ne_ODD"]):
le = value le = value
elif tag == SM_Class["PLAIN_COMMAND_HEADER"]: elif tag == SM_Class["PLAIN_COMMAND_HEADER"]:
if len(value) != 8: if len(value) != 8:
raise ValueError, "Plain Command Header expected, but" +\ raise SwError(SW["ERR_SECMESSOBJECTSINCORRECT"])
"received insufficient data"
else: else:
cla = value[:2] cla = value[:2]
ins = value[2:4] ins = value[2:4]
@@ -688,8 +687,6 @@ class Secure_Messaging(object):
""" """
padding_indicator = stringtoint(value[0]) padding_indicator = stringtoint(value[0])
sw, plain = self.decipher(tag, 0x80, value[1:]) sw, plain = self.decipher(tag, 0x80, value[1:])
if sw != 0x9000:
raise ValueError
plain = vsCrypto.strip_padding(self.current_SE.ct.algorithm, plain = vsCrypto.strip_padding(self.current_SE.ct.algorithm,
plain, plain,
padding_indicator) padding_indicator)
@@ -877,8 +874,8 @@ class Secure_Messaging(object):
raise SwError(SW["ERR_CONDITIONNOTSATISFIED"]) raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
try: try:
hash = vsCrypto.hash(algo, data) hash = vsCrypto.hash(algo, data)
except ValueError: #FIXME: Type of error except ValueError:
raise SwError(SW["ERR_SECMESSNOTSUPPORTED"]) raise SwError(SW["ERR_EXECUTION"])
return SW["NORMAL"], hash return SW["NORMAL"], hash
@@ -975,7 +972,7 @@ class Secure_Messaging(object):
algo = self.current_SE.ct.algorithm algo = self.current_SE.ct.algorithm
key = self.current_SE.ct.key key = self.current_SE.ct.key
if key == None or algo == None: if key == None or algo == None:
return SW["ERR_CONDITIONNOTSATISFIED"], "" raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
else: else:
plain = vsCrypto.decrypt(algo, key, data, self.current_SE.ct.iv) plain = vsCrypto.decrypt(algo, key, data, self.current_SE.ct.iv)
return SW["NORMAL"], plain return SW["NORMAL"], plain

View File

@@ -504,7 +504,9 @@ class VirtualICC(object): # {{{
signal.signal(signal.SIGINT, self.signalHandler) signal.signal(signal.SIGINT, self.signalHandler)
atexit.register(self.stop) atexit.register(self.stop)
def signalHandler(self, signal=None, frame=None): def signalHandler(self, sig=None, frame=None):
"""Basically this signal handler just surpresses a traceback from being
printed when the user presses crtl-c"""
sys.exit() sys.exit()
@staticmethod @staticmethod