virtualsmartcard: fixed more python3 compatibility

This commit is contained in:
Frank Morgner
2022-02-01 14:21:48 +01:00
parent d16653d377
commit 98bd7a0c8e
8 changed files with 82 additions and 82 deletions

View File

@@ -35,14 +35,14 @@ from virtualsmartcard.SmartcardSAM import SAM
# pgp directory # pgp directory
# self.mf.append(DF(parent=self.mf, # self.mf.append(DF(parent=self.mf,
# fid=4, dfname='\xd2\x76\x00\x01\x24\x01', bertlv_data=[])) # fid=4, dfname=b'\xd2\x76\x00\x01\x24\x01', bertlv_data=[]))
# pkcs-15 directories # pkcs-15 directories
# self.mf.append(DF(parent=self.mf, # self.mf.append(DF(parent=self.mf,
# fid=1, dfname='\xa0\x00\x00\x00\x01')) # fid=1, dfname=b'\xa0\x00\x00\x00\x01'))
# self.mf.append(DF(parent=self.mf, # self.mf.append(DF(parent=self.mf,
# fid=2, dfname='\xa0\x00\x00\x03\x08\x00\x00\x10\x00')) # fid=2, dfname=b'\xa0\x00\x00\x03\x08\x00\x00\x10\x00'))
# self.mf.append(DF(parent=self.mf, # self.mf.append(DF(parent=self.mf,
# fid=3, dfname='\xa0\x00\x00\x03\x08\x00\x00\x10\x00\x01\x00')) # fid=3, dfname=b'\xa0\x00\x00\x03\x08\x00\x00\x10\x00\x01\x00'))
class CardGenerator(object): class CardGenerator(object):
@@ -107,7 +107,7 @@ class CardGenerator(object):
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', df = DF(parent=mf, fid=4, dfname=b'\xa0\x00\x00\x02G\x10\x01',
bertlv_data=[]) bertlv_data=[])
# EF.COM # EF.COM
@@ -137,13 +137,13 @@ class CardGenerator(object):
DG2 = pack([(0x02, 1, b"\x01"), (0x7F60, len(DG2), DG2)]) DG2 = pack([(0x02, 1, b"\x01"), (0x7F60, len(DG2), DG2)])
DG2 = pack([(0x7F61, len(DG2), DG2)]) DG2 = pack([(0x7F61, len(DG2), DG2)])
else: else:
DG2 = "" DG2 = b""
df.append(TransparentStructureEF(parent=df, fid=0x0102, df.append(TransparentStructureEF(parent=df, fid=0x0102,
filedescriptor=0, data=DG2)) filedescriptor=0, data=DG2))
# EF.SOD # EF.SOD
df.append(TransparentStructureEF(parent=df, fid=0x010D, df.append(TransparentStructureEF(parent=df, fid=0x010D,
filedescriptor=0, data="")) filedescriptor=0, data=b""))
mf.append(df) mf.append(df)
@@ -392,95 +392,95 @@ class CardGenerator(object):
# Nationality, BirthName, ResidencePermit1 and ResidencePermit2, refer # Nationality, BirthName, ResidencePermit1 and ResidencePermit2, refer
# to BSI TR-03127 # to BSI TR-03127
if (DocumentType.rstrip() != "<NotOnChip>"): if (DocumentType.rstrip() != "<NotOnChip>"):
dg1 = pack([(0x61, 0, [(0x13, 0, DocumentType)])], True) dg1 = pack([(0x61, 0, [(0x13, 0, bytes(DocumentType, "ascii"))])], True)
else: else:
dg1 = None dg1 = None
if (IssuingState.rstrip() != "<NotOnChip>"): if (IssuingState.rstrip() != "<NotOnChip>"):
dg2 = pack([(0x62, 0, [(0x13, 0, IssuingState)])], True) dg2 = pack([(0x62, 0, [(0x13, 0, bytes(IssuingState, "ascii"))])], True)
else: else:
dg2 = None dg2 = None
if (DateOfExpiry.rstrip() != "<NotOnChip>"): if (DateOfExpiry.rstrip() != "<NotOnChip>"):
dg3 = pack([(0x63, 0, [(0x12, 0, DateOfExpiry)])], True) dg3 = pack([(0x63, 0, [(0x12, 0, bytes(DateOfExpiry, "ascii"))])], True)
else: else:
dg3 = None dg3 = None
if (GivenNames.rstrip() != "<NotOnChip>"): if (GivenNames.rstrip() != "<NotOnChip>"):
dg4 = pack([(0x64, 0, [(0x0C, 0, GivenNames)])], True) dg4 = pack([(0x64, 0, [(0x0C, 0, bytes(GivenNames, "ascii"))])], True)
else: else:
dg4 = None dg4 = None
if (FamilyNames.rstrip() != "<NotOnChip>"): if (FamilyNames.rstrip() != "<NotOnChip>"):
dg5 = pack([(0x65, 0, [(0x0C, 0, FamilyNames)])], True) dg5 = pack([(0x65, 0, [(0x0C, 0, bytes(FamilyNames, "ascii"))])], True)
else: else:
dg5 = None dg5 = None
if (ReligiousArtisticName.rstrip() != "<NotOnChip>"): if (ReligiousArtisticName.rstrip() != "<NotOnChip>"):
dg6 = pack([(0x66, 0, [(0x0C, 0, ReligiousArtisticName)])], True) dg6 = pack([(0x66, 0, [(0x0C, 0, bytes(ReligiousArtisticName, "ascii"))])], True)
else: else:
dg6 = None dg6 = None
if (AcademicTitle.rstrip() != "<NotOnChip>"): if (AcademicTitle.rstrip() != "<NotOnChip>"):
dg7 = pack([(0x67, 0, [(0x0C, 0, AcademicTitle)])], True) dg7 = pack([(0x67, 0, [(0x0C, 0, bytes(AcademicTitle, "ascii"))])], True)
else: else:
dg7 = None dg7 = None
if (DateOfBirth.rstrip() != "<NotOnChip>"): if (DateOfBirth.rstrip() != "<NotOnChip>"):
dg8 = pack([(0x68, 0, [(0x12, 0, DateOfBirth)])], True) dg8 = pack([(0x68, 0, [(0x12, 0, bytes(DateOfBirth, "ascii"))])], True)
else: else:
dg8 = None dg8 = None
if (PlaceOfBirth.rstrip() != "<NotOnChip>"): if (PlaceOfBirth.rstrip() != "<NotOnChip>"):
dg9 = pack([(0x69, 0, [(0xA1, 0, [(0x0C, 0, PlaceOfBirth)])])], dg9 = pack([(0x69, 0, [(0xA1, 0, [(0x0C, 0, bytes(PlaceOfBirth, "ascii"))])])],
True) True)
else: else:
dg9 = None dg9 = None
if (Nationality.rstrip() != "<NotOnChip>"): if (Nationality.rstrip() != "<NotOnChip>"):
dg10 = pack([(0x6A, 0, [(0x13, 0, Nationality)])], True) dg10 = pack([(0x6A, 0, [(0x13, 0, bytes(Nationality, "ascii"))])], True)
else: else:
dg10 = None dg10 = None
if (Sex.rstrip() != "<NotOnChip>"): if (Sex.rstrip() != "<NotOnChip>"):
dg11 = pack([(0x6B, 0, [(0x13, 0, Sex)])], True) dg11 = pack([(0x6B, 0, [(0x13, 0, bytes(Sex, "ascii"))])], True)
else: else:
dg11 = None dg11 = None
if (dg12_param.rstrip() != "<NotOnChip>"): if (dg12_param.rstrip() != "<NotOnChip>"):
dg12 = dg12_param dg12 = bytes(dg12_param, "ascii")
else: else:
dg12 = None dg12 = None
if (BirthName.rstrip() != "<NotOnChip>"): if (BirthName.rstrip() != "<NotOnChip>"):
dg13 = pack([(0x6D, 0, [(0x0C, 0, BirthName)])], True) dg13 = pack([(0x6D, 0, [(0x0C, 0, bytes(BirthName, "ascii"))])], True)
else: else:
dg13 = None dg13 = None
if (dg14_param.rstrip() != "<NotOnChip>"): if (dg14_param.rstrip() != "<NotOnChip>"):
dg14 = dg14_param dg14 = bytes(dg14_param, "ascii")
else: else:
dg14 = None dg14 = None
if (dg15_param.rstrip() != "<NotOnChip>"): if (dg15_param.rstrip() != "<NotOnChip>"):
dg15 = dg15_param dg15 = bytes(dg15_param, "ascii")
else: else:
dg15 = None dg15 = None
if (dg16_param.rstrip() != "<NotOnChip>"): if (dg16_param.rstrip() != "<NotOnChip>"):
dg16 = dg16_param dg16 = bytes(dg16_param, "ascii")
else: else:
dg16 = None dg16 = None
if (PlaceOfResidence.rstrip() != "<NotOnChip>"): if (PlaceOfResidence.rstrip() != "<NotOnChip>"):
dg17 = pack([(0x71, 0, [(0x30, 0, [ dg17 = pack([(0x71, 0, [(0x30, 0, [
(0xAA, 0, [(0x0C, 0, Street)]), (0xAA, 0, [(0x0C, 0, bytes(Street, "ascii"))]),
(0xAB, 0, [(0x0C, 0, City)]), (0xAB, 0, [(0x0C, 0, bytes(City, "ascii"))]),
(0xAD, 0, [(0x13, 0, Country)]), (0xAD, 0, [(0x13, 0, bytes(Country, "ascii"))]),
(0xAE, 0, [(0x13, 0, ZIP)]) (0xAE, 0, [(0x13, 0, bytes(ZIP, "ascii"))])
])])], True) ])])], True)
else: else:
dg17 = None dg17 = None
if (CommunityID.rstrip() != "<NotOnChip>"): if (CommunityID.rstrip() != "<NotOnChip>"):
dg18 = pack([(0x72, 0, [(0x04, 0, CommunityID_Binary)])], True) dg18 = pack([(0x72, 0, [(0x04, 0, bytes(CommunityID_Binary, "ascii"))])], True)
else: else:
dg18 = None dg18 = None
if (ResidencePermit1.rstrip() != "<NotOnChip>"): if (ResidencePermit1.rstrip() != "<NotOnChip>"):
dg19 = pack([(0x73, 0, [(0xA1, 0, dg19 = pack([(0x73, 0, [(0xA1, 0,
[(0x0C, 0, ResidencePermit1)])])], True) [(0x0C, 0, bytes(ResidencePermit1, "ascii"))])])], True)
else: else:
dg19 = None dg19 = None
if (ResidencePermit1.rstrip() != "<NotOnChip>"): if (ResidencePermit1.rstrip() != "<NotOnChip>"):
dg20 = pack([(0x74, 0, [(0xA1, 0, dg20 = pack([(0x74, 0, [(0xA1, 0,
[(0x0C, 0, ResidencePermit2)])])], True) [(0x0C, 0, bytes(ResidencePermit2, "ascii"))])])], True)
else: else:
dg20 = None dg20 = None
if (dg21_param.rstrip() != "<NotOnChip>"): if (dg21_param.rstrip() != "<NotOnChip>"):
dg21 = dg21_param dg21 = bytes(dg21_param, "ascii")
else: else:
dg21 = None dg21 = None

View File

@@ -240,5 +240,5 @@ def _makesalt():
This function is not suitable for generating cryptographic secrets. This function is not suitable for generating cryptographic secrets.
""" """
binarysalt = "".join([pack("@H", randint(0, 0xffff)) for i in range(3)]) binarysalt = b"".join([pack("@H", randint(0, 0xffff)) for i in range(3)])
return b64encode(binarysalt, "./") return b64encode(binarysalt, "./")

View File

@@ -297,7 +297,7 @@ class Security_Environment(object):
to_authenticate = vsCrypto.append_padding(self.cct.blocklength, to_authenticate = vsCrypto.append_padding(self.cct.blocklength,
to_authenticate) to_authenticate)
else: else:
to_authenticate = "" to_authenticate = b""
for tlv in structure: for tlv in structure:
tag, length, value = tlv tag, length, value = tlv
@@ -407,7 +407,7 @@ class Security_Environment(object):
if p2 is None: if p2 is None:
p2 = CAPDU.p2 p2 = CAPDU.p2
# FIXME: # FIXME:
# if expected != "": # if expected != b"":
# raise SwError(SW["ERR_SECMESSOBJECTSMISSING"]) # raise SwError(SW["ERR_SECMESSOBJECTSMISSING"])
if isinstance(le, bytes): if isinstance(le, bytes):
@@ -419,7 +419,7 @@ class Security_Environment(object):
le = le_int le = le_int
c = C_APDU(cla=cla, ins=ins, p1=p1, p2=p2, le=le, c = C_APDU(cla=cla, ins=ins, p1=p1, p2=p2, le=le,
data="".join(return_data)) data=b"".join(return_data))
return c return c
def protect_response(self, sw, result): def protect_response(self, sw, result):
@@ -429,7 +429,7 @@ class Security_Environment(object):
:returns: the protected data and the SW bytes :returns: the protected data and the SW bytes
""" """
return_data = "" return_data = b""
# if sw == SW["NORMAL"]: # if sw == SW["NORMAL"]:
# sw = inttostring(sw) # sw = inttostring(sw)
# length = len(sw) # length = len(sw)
@@ -437,9 +437,9 @@ class Security_Environment(object):
# tlv_sw = pack([(tag,length,sw)]) # tlv_sw = pack([(tag,length,sw)])
# return_data += tlv_sw # return_data += tlv_sw
if result != "": # Encrypt the data included in the RAPDU if result != b"": # Encrypt the data included in the RAPDU
encrypted = self.encipher(0x82, 0x80, result) encrypted = self.encipher(0x82, 0x80, result)
encrypted = "\x01" + encrypted encrypted = b"\x01" + encrypted
encrypted_tlv = pack([( encrypted_tlv = pack([(
SM_Class["CRYPTOGRAM_PADDING_INDICATOR_ODD"], SM_Class["CRYPTOGRAM_PADDING_INDICATOR_ODD"],
len(encrypted), len(encrypted),
@@ -497,7 +497,7 @@ class Security_Environment(object):
response_data = self.decipher(p1, p2, data) response_data = self.decipher(p1, p2, data)
if p1 == 0x00: if p1 == 0x00:
assert response_data == "" assert response_data == b""
return SW["NORMAL"], response_data return SW["NORMAL"], response_data
@@ -531,11 +531,11 @@ class Security_Environment(object):
if self.dst.key is None: if self.dst.key is None:
raise SwError(SW["ERR_CONDITIONNOTSATISFIED"]) raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
to_sign = "" to_sign = b""
if p2 == 0x9A: # Data to be signed if p2 == 0x9A: # Data to be signed
to_sign = data to_sign = data
elif p2 == 0xAC: # Data objects, sign values elif p2 == 0xAC: # Data objects, sign values
to_sign = "" to_sign = b""
structure = unpack(data) structure = unpack(data)
for tag, length, value in structure: for tag, length, value in structure:
to_sign += value to_sign += value
@@ -570,8 +570,8 @@ class Security_Environment(object):
Data field must contain a cryptographic checksum (tag 0x8E) and a plain Data field must contain a cryptographic checksum (tag 0x8E) and a plain
value (tag 0x80) value (tag 0x80)
""" """
plain = "" plain = b""
cct = "" cct = b""
algo = self.cct.algorithm algo = self.cct.algorithm
key = self.cct.key key = self.cct.key
@@ -585,7 +585,7 @@ class Security_Environment(object):
plain = value plain = value
elif tag == 0x8E: elif tag == 0x8E:
cct = value cct = value
if plain == "" or cct == "": if plain == b"" or cct == b"":
raise SwError(SW["ERR_SECMESSOBJECTSMISSING"]) raise SwError(SW["ERR_SECMESSOBJECTSMISSING"])
else: else:
my_cct = vsCrypto.crypto_checksum(algo, key, plain, iv) my_cct = vsCrypto.crypto_checksum(algo, key, plain, iv)
@@ -601,8 +601,8 @@ class Security_Environment(object):
(0x9E) (0x9E)
""" """
key = self.dst.key key = self.dst.key
to_sign = "" to_sign = b""
signature = "" signature = b""
if key is None: if key is None:
raise SwError(SW["ERR_CONDITIONNOTSATISFIED"]) raise SwError(SW["ERR_CONDITIONNOTSATISFIED"])
@@ -618,7 +618,7 @@ class Security_Environment(object):
elif tag == 0xBC: elif tag == 0xBC:
pass pass
if to_sign == "" or signature == "": if to_sign == b"" or signature == b"":
raise SwError(SW["ERR_SECMESSOBJECTSMISSING"]) raise SwError(SW["ERR_SECMESSOBJECTSMISSING"])
my_signature = key.sign(value) my_signature = key.sign(value)

View File

@@ -89,7 +89,7 @@ def getfile_byrefdataobj(mf, refdataobjs):
if length % 2 == 1: if length % 2 == 1:
raise NotImplementedError raise NotImplementedError
else: else:
if newvalue[:2] == "\x3f\x00": if newvalue[:2] == b"\x3f\x00":
# absolute # absolute
file = walk(mf, newvalue[2:]) file = walk(mf, newvalue[2:])
else: else:
@@ -419,7 +419,7 @@ class DF(File):
def __init__(self, parent, fid, def __init__(self, parent, fid,
filedescriptor=FDB["NOTSHAREABLEFILE"] | FDB["DF"], filedescriptor=FDB["NOTSHAREABLEFILE"] | FDB["DF"],
lifecycle=LCB["ACTIVATED"], lifecycle=LCB["ACTIVATED"],
simpletlv_data=None, bertlv_data=None, dfname=None, data=""): simpletlv_data=None, bertlv_data=None, dfname=None, data=b""):
""" """
See File for more. See File for more.
""" """
@@ -1451,7 +1451,7 @@ class TransparentStructureEF(EF):
filedescriptor=FDB["EFSTRUCTURE_TRANSPARENT"], filedescriptor=FDB["EFSTRUCTURE_TRANSPARENT"],
lifecycle=LCB["ACTIVATED"], lifecycle=LCB["ACTIVATED"],
simpletlv_data=None, bertlv_data=None, simpletlv_data=None, bertlv_data=None,
datacoding=DCB["ONETIMEWRITE"], shortfid=0, data=""): datacoding=DCB["ONETIMEWRITE"], shortfid=0, data=b""):
""" """
See EF for more. See EF for more.
""" """
@@ -1521,7 +1521,7 @@ class Record(object):
identifier = make_property("identifier", "integer with 1 <= identifier <=" identifier = make_property("identifier", "integer with 1 <= identifier <="
" 0xfe. The record's identifier.") " 0xfe. The record's identifier.")
"""Class for a Record of an elementary of record structure""" """Class for a Record of an elementary of record structure"""
def __init__(self, identifier=None, data=""): def __init__(self, identifier=None, data=b""):
""" """
The constructor is supposed to be involved by EF.appendrecord. The constructor is supposed to be involved by EF.appendrecord.
""" """

View File

@@ -29,51 +29,51 @@ class HandlerTestOS(SmartcardOS):
and handler_test(1). and handler_test(1).
""" """
def __init__(self): def __init__(self):
lastCommandOffcut = '' lastCommandOffcut = b''
def getATR(self): def getATR(self):
return '\x3B\xD6\x18\x00\x80\xB1\x80\x6D\x1F\x03\x80\x51\x00\x61' + \ return b'\x3B\xD6\x18\x00\x80\xB1\x80\x6D\x1F\x03\x80\x51\x00\x61' + \
'\x10\x30\x9E' b'\x10\x30\x9E'
def __output_from_le(self, msg): def __output_from_le(self, msg):
le = (ord(msg[2]) << 8) + ord(msg[3]) le = (ord(msg[2]) << 8) + ord(msg[3])
return ''.join([bytes(num & 0xff) for num in xrange(le)]) return ''.join([bytes(num & 0xff) for num in xrange(le)])
def execute(self, msg): def execute(self, msg):
ok = '\x90\x00' ok = b'\x90\x00'
error = '\x6d\x00' error = b'\x6d\x00'
if (msg == '\x00\xA4\x04\x00\x06\xA0\x00\x00\x00\x18\x50' or if (msg == b'\x00\xA4\x04\x00\x06\xA0\x00\x00\x00\x18\x50' or
msg == '\x00\xA4\x04\x00\x06\xA0\x00\x00\x00\x18\xFF'): msg == b'\x00\xA4\x04\x00\x06\xA0\x00\x00\x00\x18\xFF'):
logging.info('Select applet') logging.info('Select applet')
return ok return ok
elif msg.startswith('\x80\x38\x00'): elif msg.startswith(b'\x80\x38\x00'):
logging.info('Time Request') logging.info('Time Request')
return ok return ok
elif msg == '\x80\x30\x00\x00': elif msg == b'\x80\x30\x00\x00':
logging.info('Case 1, APDU') logging.info('Case 1, APDU')
return ok return ok
elif msg == '\x80\x30\x00\x00\x00': elif msg == b'\x80\x30\x00\x00\x00':
logging.info('Case 1, TPDU') logging.info('Case 1, TPDU')
return ok return ok
elif msg.startswith('\x80\x32\x00\x00'): elif msg.startswith(b'\x80\x32\x00\x00'):
logging.info('Case 3') logging.info('Case 3')
return ok return ok
elif msg.startswith('\x80\x34'): elif msg.startswith(b'\x80\x34'):
logging.info('Case 2') logging.info('Case 2')
return self.__output_from_le(msg) + ok return self.__output_from_le(msg) + ok
elif msg.startswith('\x80\x36'): elif msg.startswith(b'\x80\x36'):
if len(msg) == 5+ord(msg[4]): if len(msg) == 5+ord(msg[4]):
logging.info('Case 4, TPDU') logging.info('Case 4, TPDU')
self.lastCommandOffcut = self.__output_from_le(msg) self.lastCommandOffcut = self.__output_from_le(msg)
if len(self.lastCommandOffcut) > 0xFF: if len(self.lastCommandOffcut) > 0xFF:
return '\x61\x00' return b'\x61\x00'
return '' + bytes(len(self.lastCommandOffcut) & 0xFF) return b'' + bytes(len(self.lastCommandOffcut) & 0xFF)
elif len(msg) == 6+ord(msg[4]): elif len(msg) == 6+ord(msg[4]):
logging.info('Case 4, APDU') logging.info('Case 4, APDU')
return self.__output_from_le(msg) + ok return self.__output_from_le(msg) + ok
else: else:
return error return error
elif msg.startswith('\x80\xC0\x00\x00'): elif msg.startswith(b'\x80\xC0\x00\x00'):
logging.info('Get response') logging.info('Get response')
out = self.lastCommandOffcut[:ord(msg[4])] out = self.lastCommandOffcut[:ord(msg[4])]
self.lastCommandOffcut = self.lastCommandOffcut[ord(msg[4]):] self.lastCommandOffcut = self.lastCommandOffcut[ord(msg[4]):]

View File

@@ -34,7 +34,7 @@ import logging
class CryptoflexOS(Iso7816OS): class CryptoflexOS(Iso7816OS):
def __init__(self, mf, sam, ins2handler=None, maxle=MAX_SHORT_LE): def __init__(self, mf, sam, ins2handler=None, maxle=MAX_SHORT_LE):
Iso7816OS.__init__(self, mf, sam, ins2handler, maxle) Iso7816OS.__init__(self, mf, sam, ins2handler, maxle)
self.atr = '\x3B\xE2\x00\x00\x40\x20\x49\x06' self.atr = b'\x3B\xE2\x00\x00\x40\x20\x49\x06'
def execute(self, msg): def execute(self, msg):
def notImplemented(*argz, **args): def notImplemented(*argz, **args):
@@ -56,7 +56,7 @@ class CryptoflexOS(Iso7816OS):
logging.info(e.message) logging.info(e.message)
# traceback.print_exception(*sys.exc_info()) # traceback.print_exception(*sys.exc_info())
sw = e.sw sw = e.sw
result = "" result = b""
r = self.formatResult(c.ins, c.le, result, sw) r = self.formatResult(c.ins, c.le, result, sw)
return r return r
@@ -179,7 +179,7 @@ class CryptoflexSAM(SAM):
def internal_authenticate(self, p1, p2, data): def internal_authenticate(self, p1, p2, data):
data = data[::-1] # Reverse Byte order data = data[::-1] # Reverse Byte order
sw, data = SAM.internal_authenticate(self, p1, p2, data) sw, data = SAM.internal_authenticate(self, p1, p2, data)
if data != "": if data != b"":
data = data[::-1] data = data[::-1]
return sw, data return sw, data
@@ -189,7 +189,7 @@ class CryptoflexMF(MF): # {{{
@staticmethod @staticmethod
def create(p1, p2, data): def create(p1, p2, data):
if data[0:2] != "\xff\xff": if data[0:2] != b"\xff\xff":
raise SwError(SW["ERR_INCORRECTPARAMETERS"]) raise SwError(SW["ERR_INCORRECTPARAMETERS"])
args = { args = {
@@ -197,11 +197,11 @@ class CryptoflexMF(MF): # {{{
"filedescriptor": 0, "filedescriptor": 0,
"fid": stringtoint(data[4:6]), "fid": stringtoint(data[4:6]),
} }
if data[6] == "\x01": if data[6] == b"\x01":
args["data"] = bytes(0)*stringtoint(data[2:4]) args["data"] = bytes(0)*stringtoint(data[2:4])
args["filedescriptor"] = FDB["EFSTRUCTURE_TRANSPARENT"] args["filedescriptor"] = FDB["EFSTRUCTURE_TRANSPARENT"]
new_file = TransparentStructureEF(**args) new_file = TransparentStructureEF(**args)
elif data[6] == "\x02": elif data[6] == b"\x02":
if len(data) > 16: if len(data) > 16:
args["maxrecordsize"] = stringtoint(data[16]) args["maxrecordsize"] = stringtoint(data[16])
elif p2: elif p2:
@@ -210,15 +210,15 @@ class CryptoflexMF(MF): # {{{
args["filedescriptor"] = FDB["EFSTRUCTURE_LINEAR_FIXED_" args["filedescriptor"] = FDB["EFSTRUCTURE_LINEAR_FIXED_"
"NOFURTHERINFO"] "NOFURTHERINFO"]
new_file = RecordStructureEF(**args) new_file = RecordStructureEF(**args)
elif data[6] == "\x03": elif data[6] == b"\x03":
args["filedescriptor"] = FDB["EFSTRUCTURE_LINEAR_VARIABLE_" args["filedescriptor"] = FDB["EFSTRUCTURE_LINEAR_VARIABLE_"
"NOFURTHERINFO"] "NOFURTHERINFO"]
new_file = RecordStructureEF(**args) new_file = RecordStructureEF(**args)
elif data[6] == "\x04": elif data[6] == b"\x04":
args["filedescriptor"] = FDB["EFSTRUCTURE_CYCLIC_NOFURTHERINFO"] args["filedescriptor"] = FDB["EFSTRUCTURE_CYCLIC_NOFURTHERINFO"]
new_file = RecordStructureEF(**args) new_file = RecordStructureEF(**args)
elif data[6] == "\x38": elif data[6] == b"\x38":
if data[12] != "\x03": if data[12] != b"\x03":
raise SwError(SW["ERR_INCORRECTPARAMETERS"]) raise SwError(SW["ERR_INCORRECTPARAMETERS"])
new_file = DF(**args) new_file = DF(**args)
else: else:

View File

@@ -431,7 +431,7 @@ class nPA_SE(Security_Environment):
def __eac_ca(self, data): def __eac_ca(self, data):
tlv_data = nPA_SE.__unpack_general_authenticate(data) tlv_data = nPA_SE.__unpack_general_authenticate(data)
pubkey = "" pubkey = b""
for tag, length, value in tlv_data: for tag, length, value in tlv_data:
if tag == 0x80: if tag == 0x80:
pubkey = value pubkey = value

View File

@@ -175,7 +175,7 @@ class APDU(object):
def _deldata(self): def _deldata(self):
del self._data del self._data
self.data = "" self.data = b""
data = property(_getdata, _setdata, None, data = property(_getdata, _setdata, None,
"The data contents of this APDU") "The data contents of this APDU")
@@ -229,12 +229,12 @@ class C_APDU(APDU):
self.CLA, self.INS, self.P1, self.P2 = apdu[:4] # case 1, 2, 3, 4 self.CLA, self.INS, self.P1, self.P2 = apdu[:4] # case 1, 2, 3, 4
self.__extended_length = False self.__extended_length = False
if len(apdu) == 4: # case 1 if len(apdu) == 4: # case 1
self.data = "" self.data = b""
elif (len(apdu) >= 7) and (apdu[4] == 0): # extended length apdu elif (len(apdu) >= 7) and (apdu[4] == 0): # extended length apdu
self.__extended_length = True self.__extended_length = True
if len(apdu) == 7: # case 2 extended length if len(apdu) == 7: # case 2 extended length
self.Le = (apdu[-2] << 8) + apdu[-1] self.Le = (apdu[-2] << 8) + apdu[-1]
self.data = "" self.data = b""
else: # case 3, 4 extended length else: # case 3, 4 extended length
self.Lc = (apdu[5] << 8) + apdu[6] self.Lc = (apdu[5] << 8) + apdu[6]
if len(apdu) == 7 + self.Lc: # case 3 extended length if len(apdu) == 7 + self.Lc: # case 3 extended length
@@ -254,7 +254,7 @@ class C_APDU(APDU):
else: # short apdu else: # short apdu
if len(apdu) == 5: # case 2 short apdu if len(apdu) == 5: # case 2 short apdu
self.Le = apdu[-1] self.Le = apdu[-1]
self.data = "" self.data = b""
elif len(apdu) > 5: # case 3, 4 short apdu elif len(apdu) > 5: # case 3, 4 short apdu
self.Lc = apdu[4] self.Lc = apdu[4]
if len(apdu) == 5 + self.Lc: # case 3 if len(apdu) == 5 + self.Lc: # case 3