fixed more Python 3 issues

also fixes the debug output
This commit is contained in:
Frank Morgner
2018-05-03 22:41:25 +02:00
parent 3b7c04c535
commit 04642a3bfc
6 changed files with 77 additions and 70 deletions

View File

@@ -410,7 +410,7 @@ class Security_Environment(object):
# if expected != "": # if expected != "":
# raise SwError(SW["ERR_SECMESSOBJECTSMISSING"]) # raise SwError(SW["ERR_SECMESSOBJECTSMISSING"])
if isinstance(le, str): if isinstance(le, bytes):
# FIXME: C_APDU only handles le with strings of length 1. # FIXME: C_APDU only handles le with strings of length 1.
# Better patch utils.py to support extended length apdus # Better patch utils.py to support extended length apdus
le_int = stringtoint(le) le_int = stringtoint(le)
@@ -702,23 +702,23 @@ class Security_Environment(object):
# Encode keys # Encode keys
if cipher == "RSA": if cipher == "RSA":
# Public key # Public key
n = str(PublicKey.__getstate__()['n']) n = inttostring(PublicKey.__getstate__()['n'])
e = str(PublicKey.__getstate__()['e']) e = inttostring(PublicKey.__getstate__()['e'])
pk = ((0x81, len(n), n), (0x82, len(e), e)) pk = ((0x81, len(n), n), (0x82, len(e), e))
result = bertlv_pack(pk) result = bertlv_pack(pk)
# Private key # Private key
d = PublicKey.__getstate__()['d'] d = PublicKey.__getstate__()['d']
elif cipher == "DSA": elif cipher == "DSA":
# DSAParams # DSAParams
p = str(PublicKey.__getstate__()['p']) p = inttostring(PublicKey.__getstate__()['p'])
q = str(PublicKey.__getstate__()['q']) q = inttostring(PublicKey.__getstate__()['q'])
g = str(PublicKey.__getstate__()['g']) g = inttostring(PublicKey.__getstate__()['g'])
# Public key # Public key
y = str(PublicKey.__getstate__()['y']) y = inttostring(PublicKey.__getstate__()['y'])
pk = ((0x81, len(p), p), (0x82, len(q), q), (0x83, len(g), g), pk = ((0x81, len(p), p), (0x82, len(q), q), (0x83, len(g), g),
(0x84, len(y), y)) (0x84, len(y), y))
# Private key # Private key
x = str(PublicKey.__getstate__()['x']) x = inttostring(PublicKey.__getstate__()['x'])
# Add more algorithms here # Add more algorithms here
# elif cipher = "ECDSA": # elif cipher = "ECDSA":
else: else:

View File

@@ -139,14 +139,14 @@ def write(old, newlist, offsets, datacoding, maxsize=None):
while newindex < writenow: while newindex < writenow:
if datacoding == DCB["WRITEOR"]: if datacoding == DCB["WRITEOR"]:
newpiece = inttostring( newpiece = inttostring(
ord(result[resultindex]) | ord(new[newindex])) result[resultindex] | new[newindex])
elif datacoding == DCB["WRITEAND"]: elif datacoding == DCB["WRITEAND"]:
newpiece = inttostring( newpiece = inttostring(
ord(result[resultindex]) & ord(new[newindex])) result[resultindex] & new[newindex])
elif datacoding == DCB["PROPRIETARY"]: elif datacoding == DCB["PROPRIETARY"]:
# we use it for XOR # we use it for XOR
newpiece = inttostring( newpiece = inttostring(
ord(result[resultindex]) ^ ord(new[newindex])) result[resultindex] ^ new[newindex])
result = (result[0:resultindex] + newpiece + result = (result[0:resultindex] + newpiece +
result[resultindex+1:len(result)]) result[resultindex+1:len(result)])
newindex = newindex + 1 newindex = newindex + 1
@@ -246,7 +246,7 @@ class File(object):
simpletlv_data=None, simpletlv_data=None,
bertlv_data=None, bertlv_data=None,
SAM=None, SAM=None,
extra_fci_data=''): extra_fci_data=b''):
""" """
The constructor is supposed to be involved by creation of a DF or EF. The constructor is supposed to be involved by creation of a DF or EF.
""" """
@@ -642,12 +642,12 @@ class MF(DF):
l += simpletlv_unpack(r.data)[0][1] l += simpletlv_unpack(r.data)[0][1]
else: else:
l += len(r.data) l += len(r.data)
fdm.append("%c\x02%s" % (TAG["BYTES_EXCLUDINGSTRUCTURE"], fdm.append(b"%c\x02%s" % (TAG["BYTES_EXCLUDINGSTRUCTURE"],
inttostring(l, 2, True))) inttostring(l, 2, True)))
fdm.append("%c\x02%s" % (TAG["BYTES_INCLUDINGSTRUCTURE"], fdm.append(b"%c\x02%s" % (TAG["BYTES_INCLUDINGSTRUCTURE"],
inttostring(l, 2, True))) inttostring(l, 2, True)))
l = len(records) l = len(records)
fdm.append("%c\x06%c%c%c%c%s" % (TAG["FILEDISCRIPTORBYTE"], fdm.append(b"%c\x06%c%c%c%c%s" % (TAG["FILEDISCRIPTORBYTE"],
file.filedescriptor, file.datacoding, file.filedescriptor, file.datacoding,
file.maxrecordsize >> 8, file.maxrecordsize >> 8,
file.maxrecordsize & 0x00ff, file.maxrecordsize & 0x00ff,
@@ -655,10 +655,10 @@ class MF(DF):
elif isinstance(file, DF): elif isinstance(file, DF):
# TODO number of files == number of data bytes? # TODO number of files == number of data bytes?
fdm.append("%c\x01%c" % (TAG["FILEDISCRIPTORBYTE"], fdm.append(b"%c\x01%c" % (TAG["FILEDISCRIPTORBYTE"],
file.filedescriptor)) file.filedescriptor))
if hasattr(file, 'dfname'): if hasattr(file, 'dfname'):
fdm.append("%c%c%s" % (TAG["DFNAME"], len(file.dfname), fdm.append(b"%c%c%s" % (TAG["DFNAME"], len(file.dfname),
file.dfname)) file.dfname))
else: else:
@@ -748,6 +748,7 @@ class MF(DF):
data = bertlv_pack([(tag, len(fdm), fdm)]) data = bertlv_pack([(tag, len(fdm), fdm)])
self.current = file self.current = file
logging.info("Selected %s" % file)
return SW["NORMAL"], data return SW["NORMAL"], data
@@ -1309,9 +1310,9 @@ class MF(DF):
if l >= 3: if l >= 3:
args["maxrecordsize"] = stringtoint(value[2:]) args["maxrecordsize"] = stringtoint(value[2:])
if l >= 2: if l >= 2:
args["datacoding"] = ord(value[1]) args["datacoding"] = value[1]
if l >= 1: if l >= 1:
args["filedescriptor"] = ord(value[0]) args["filedescriptor"] = value[0]
def shortfid2args(value, args): def shortfid2args(value, args):
s = stringtoint(value) s = stringtoint(value)
@@ -1323,19 +1324,6 @@ class MF(DF):
def unknown(tag, value): def unknown(tag, value):
logging.debug("unknown tag 0x%x with %r" % (tag, value)) logging.debug("unknown tag 0x%x with %r" % (tag, value))
tag2cmd = {
# TODO support other tags
TAG["FILEDISCRIPTORBYTE"]: 'fdb2args(value, args)',
TAG["FILEIDENTIFIER"]: 'args["fid"] = stringtoint(value)',
TAG["DFNAME"]: 'args["dfname"] = value',
TAG["SHORTFID"]: 'shortfid2args(value, args)',
TAG["LIFECYCLESTATUS"]: 'args["lifecycle"] = ' + \
'stringtoint(value)',
TAG["BYTES_EXCLUDINGSTRUCTURE"]: 'args["data"] = bytes(0) ' + \
'* stringtoint(value)',
TAG["BYTES_INCLUDINGSTRUCTURE"]: 'args["data"] = bytes(0) ' + \
'* stringtoint(value)',
}
fcp_list = tlv_find_tags(bertlv_unpack(data), fcp_list = tlv_find_tags(bertlv_unpack(data),
[TAG["FILECONTROLINFORMATION"], [TAG["FILECONTROLINFORMATION"],
TAG["FILECONTROLPARAMETERS"]]) TAG["FILECONTROLPARAMETERS"]])
@@ -1353,9 +1341,23 @@ class MF(DF):
T != TAG["FILECONTROLINFORMATION"]): T != TAG["FILECONTROLINFORMATION"]):
raise ValueError raise ValueError
for tag, _, value in tlv_data: for tag, _, value in tlv_data:
exec(tag2cmd.get(tag, 'unknown(tag, value)') in locals(), if tag == TAG["FILEDISCRIPTORBYTE"]:
globals()) fdb2args(value, args)
elif tag == TAG["FILEIDENTIFIER"]:
args["fid"] = stringtoint(value)
elif tag == TAG["DFNAME"]:
args["dfname"] = value
elif tag == TAG["SHORTFID"]:
shortfid2args(value, args)
elif tag == TAG["LIFECYCLESTATUS"]:
args["lifecycle"] = stringtoint(value)
elif tag == TAG["BYTES_EXCLUDINGSTRUCTURE"]:
args["data"] = b'\x00' * stringtoint(value)
elif tag == TAG["BYTES_INCLUDINGSTRUCTURE"]:
args["data"] = b'\x00' * stringtoint(value)
else:
unknown(tag, value)
print(args)
if (args["filedescriptor"] & FDB["DF"]) == FDB["DF"]: if (args["filedescriptor"] & FDB["DF"]) == FDB["DF"]:
# FIXME: data for DF # FIXME: data for DF
if "data" in args: if "data" in args:
@@ -1391,6 +1393,7 @@ class MF(DF):
file.parent = df file.parent = df
df.append(file) df.append(file)
self.current = file self.current = file
logging.info("Created %s" % file)
return SW["NORMAL"], b"" return SW["NORMAL"], b""
@@ -1406,6 +1409,7 @@ class MF(DF):
file.parent.content.remove(file) file.parent.content.remove(file)
# FIXME: free memory of file and remove its content from the security # FIXME: free memory of file and remove its content from the security
# device # device
logging.info("Deleted %s" % file)
return SW["NORMAL"], b"" return SW["NORMAL"], b""
@@ -1738,7 +1742,7 @@ class RecordStructureEF(EF):
raise SwError(SW["ERR_INCORRECTPARAMETERS"]) raise SwError(SW["ERR_INCORRECTPARAMETERS"])
if self.hasFixedRecordSize(): if self.hasFixedRecordSize():
data = bytes(0)*(self.maxrecordsize) + data data = b'\x00'*(self.maxrecordsize) + data
records = self.records records = self.records
if self.isCyclic(): if self.isCyclic():

View File

@@ -17,7 +17,7 @@
# virtualsmartcard. If not, see <http://www.gnu.org/licenses/>. # virtualsmartcard. If not, see <http://www.gnu.org/licenses/>.
# #
from virtualsmartcard.utils import stringtoint from virtualsmartcard.utils import stringtoint, inttostring
TAG = {} TAG = {}
TAG["FILECONTROLPARAMETERS"] = 0x62 TAG["FILECONTROLPARAMETERS"] = 0x62
@@ -93,7 +93,7 @@ def tlv_find_tags(tlv_data, tags, num_results=None):
if t in tags: if t in tags:
results.append(d) results.append(d)
else: else:
if isinstance(v, list): if isinstance(v, bytes) and not isinstance(v[0], int):
find_recursive(v) find_recursive(v)
if num_results is not None and len(results) >= num_results: if num_results is not None and len(results) >= num_results:
@@ -119,35 +119,35 @@ def pack(tlv_data, recalculate_length=False):
for data in tlv_data: for data in tlv_data:
tag, length, value = data[:3] tag, length, value = data[:3]
if tag in (0xff, 0x00): if tag in (0xff, 0x00):
result.append(chr(tag)) result.append(inttostring(tag))
continue continue
if not isinstance(value, str): if not isinstance(value, bytes):
value = pack(value, recalculate_length) value = pack(value, recalculate_length)
if recalculate_length: if recalculate_length:
length = len(value) length = len(value)
t = "" t = b""
while tag > 0: while tag > 0:
t = chr(tag & 0xff) + t t = inttostring(tag & 0xff) + t
tag = tag >> 8 tag = tag >> 8
if length < 0x7F: if length < 0x7F:
l = chr(length) l = inttostring(length)
else: else:
l = "" l = b""
while length > 0: while length > 0:
l = chr(length & 0xff) + l l = inttostring(length & 0xff) + l
length = length >> 8 length = length >> 8
assert len(l) < 0x7f assert len(l) < 0x7f
l = chr(0x80 | len(l)) + l l = inttostring(0x80 | len(l)) + l
result.append(t) result.append(t)
result.append(l) result.append(l)
result.append(value) result.append(value)
return "".join(result) return b"".join(result)
def bertlv_pack(data): def bertlv_pack(data):
@@ -158,7 +158,7 @@ def bertlv_pack(data):
def unpack(data, with_marks=None, offset=0, include_filler=False): def unpack(data, with_marks=None, offset=0, include_filler=False):
result = [] result = []
if isinstance(data, str): if isinstance(data, str):
data = map(ord, data) data = list(map(ord, data))
while len(data) > 0: while len(data) > 0:
if data[0] in (0x00, 0xFF): if data[0] in (0x00, 0xFF):
if include_filler: if include_filler:
@@ -202,7 +202,7 @@ def bertlv_unpack(data):
def simpletlv_pack(tlv_data, recalculate_length=False): def simpletlv_pack(tlv_data, recalculate_length=False):
result = "" result = b""
for tag, length, value in tlv_data: for tag, length, value in tlv_data:
if tag >= 0xff or tag <= 0x00: if tag >= 0xff or tag <= 0x00:
@@ -216,10 +216,10 @@ def simpletlv_pack(tlv_data, recalculate_length=False):
continue continue
if length < 0xff: if length < 0xff:
result += chr(tag) + chr(length) + value result += inttostring(tag) + inttostring(length) + value
else: else:
result += chr(tag) + chr(0xff) + chr(length >> 8) + \ result += inttostring(tag) + inttostring(0xff) + inttostring(length >> 8) + \
chr(length & 0xff) + value inttostring(length & 0xff) + value
return result return result
@@ -229,7 +229,7 @@ def simpletlv_unpack(data):
newvalue).""" newvalue)."""
result = [] result = []
if isinstance(data, str): if isinstance(data, str):
data = map(ord, data) data = list(map(ord, data))
rest = data rest = data
while rest: while rest:
tag = rest[0] tag = rest[0]

View File

@@ -23,6 +23,7 @@ import logging
import socket import socket
import struct import struct
import sys import sys
import traceback
from virtualsmartcard.ConstantDefinitions import MAX_EXTENDED_LE, MAX_SHORT_LE from virtualsmartcard.ConstantDefinitions import MAX_EXTENDED_LE, MAX_SHORT_LE
from virtualsmartcard.SWutils import SwError, SW from virtualsmartcard.SWutils import SwError, SW
from virtualsmartcard.SmartcardFilesystem import make_property from virtualsmartcard.SmartcardFilesystem import make_property
@@ -282,15 +283,17 @@ class Iso7816OS(SmartcardOS):
""" """
raise SwError(SW["ERR_INSNOTSUPPORTED"]) raise SwError(SW["ERR_INSNOTSUPPORTED"])
logging.info("Command APDU (%d bytes):\n %s", len(msg),
hexdump(msg, indent=2))
try: try:
c = C_APDU(msg) c = C_APDU(msg)
logging.debug("%s", str(c))
except ValueError as e: except ValueError as e:
logging.warning(str(e)) logging.warning(str(e))
return self.formatResult(False, 0, b"", return self.formatResult(False, 0, b"",
SW["ERR_INCORRECTPARAMETERS"], False) SW["ERR_INCORRECTPARAMETERS"], False)
logging.info("Parsed APDU:\n%s", str(c))
# Handle Class Byte # Handle Class Byte
# {{{ # {{{
class_byte = c.cla class_byte = c.cla
@@ -352,9 +355,8 @@ class Iso7816OS(SmartcardOS):
answer = self.formatResult(Iso7816OS.seekable(c.ins), answer = self.formatResult(Iso7816OS.seekable(c.ins),
c.effective_Le, result, sw, sm) c.effective_Le, result, sw, sm)
except SwError as e: except SwError as e:
logging.debug(traceback.format_exc().rstrip())
logging.info(e.message) logging.info(e.message)
import traceback
traceback.print_exception(*sys.exc_info())
sw = e.sw sw = e.sw
result = b"" result = b""
answer = self.formatResult(False, 0, result, sw, sm) answer = self.formatResult(False, 0, result, sw, sm)
@@ -575,8 +577,8 @@ class VirtualICC(object):
size, len(msg)) size, len(msg))
answer = self.os.execute(msg) answer = self.os.execute(msg)
logging.info("Response APDU (%d Bytes):\n%s\n", len(answer), logging.info("Response APDU (%d bytes):\n %s\n", len(answer),
hexdump(answer)) hexdump(answer, indent=2))
self.__sendToVPICC(answer) self.__sendToVPICC(answer)
def stop(self): def stop(self):

View File

@@ -92,8 +92,8 @@ class NPAOS(Iso7816OS):
try: try:
sw, result = self.SAM.protect_result(sw, result) sw, result = self.SAM.protect_result(sw, result)
except SwError as e: except SwError as e:
logging.debug(traceback.format_exc().rstrip())
logging.info(e.message) logging.info(e.message)
traceback.print_exception(*sys.exc_info())
sw = e.sw sw = e.sw
result = b"" result = b""
answer = self.formatResult(False, 0, result, sw, False) answer = self.formatResult(False, 0, result, sw, False)

View File

@@ -22,10 +22,14 @@ import struct
from virtualsmartcard.ConstantDefinitions import MAX_SHORT_LE, MAX_EXTENDED_LE from virtualsmartcard.ConstantDefinitions import MAX_SHORT_LE, MAX_EXTENDED_LE
def stringtoint(str): def stringtoint(data):
if str: i = 0
return int(str.encode('hex'), 16) if data:
return 0 if isinstance(data, str):
data = list(map(ord, data))
for byte in data:
i = (i << 8) + byte
return i
def inttostring(i, length=None, len_extendable=False): def inttostring(i, length=None, len_extendable=False):
@@ -61,7 +65,7 @@ def hexdump(data, indent=0, short=False, linelen=16, offset=0):
def hexable(data): def hexable(data):
if isinstance(data, str): if isinstance(data, str):
data = map(ord, data) data = list(map(ord, data))
return " ".join(map("{0:0>2X}".format, data)) return " ".join(map("{0:0>2X}".format, data))
def printable(data): def printable(data):
@@ -71,7 +75,7 @@ def hexdump(data, indent=0, short=False, linelen=16, offset=0):
return "%s (%s)" % (hexable(data), printable(data)) return "%s (%s)" % (hexable(data), printable(data))
if isinstance(data, str): if isinstance(data, str):
data = map(ord, data) data = list(map(ord, data))
FORMATSTRING = "%04x: %-" + str(linelen*3) + "s %-" + str(linelen) + "s" FORMATSTRING = "%04x: %-" + str(linelen*3) + "s %-" + str(linelen) + "s"
result = "" result = ""
(head, tail) = (data[:linelen], data[linelen:]) (head, tail) = (data[:linelen], data[linelen:])
@@ -199,10 +203,7 @@ class APDU(object):
", ".join(self._format_fields())) ", ".join(self._format_fields()))
if len(self.data) > 0: if len(self.data) > 0:
result = result + " with %i (0x%02x) bytes of data" % ( return result + ":\n " + hexdump(self.data, indent=2)
len(self.data), len(self.data)
)
return result + ":\n" + hexdump(self.data)
else: else:
return result return result