diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py index 737a919..76b8091 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CardGenerator.py @@ -35,14 +35,14 @@ from virtualsmartcard.SmartcardSAM import SAM # pgp directory # 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 # 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, -# 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, -# 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): @@ -107,7 +107,7 @@ class CardGenerator(object): mf = MF() # 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=[]) # EF.COM @@ -137,13 +137,13 @@ class CardGenerator(object): DG2 = pack([(0x02, 1, b"\x01"), (0x7F60, len(DG2), DG2)]) DG2 = pack([(0x7F61, len(DG2), DG2)]) else: - DG2 = "" + DG2 = b"" df.append(TransparentStructureEF(parent=df, fid=0x0102, filedescriptor=0, data=DG2)) # EF.SOD df.append(TransparentStructureEF(parent=df, fid=0x010D, - filedescriptor=0, data="")) + filedescriptor=0, data=b"")) mf.append(df) @@ -392,95 +392,95 @@ class CardGenerator(object): # Nationality, BirthName, ResidencePermit1 and ResidencePermit2, refer # to BSI TR-03127 if (DocumentType.rstrip() != ""): - dg1 = pack([(0x61, 0, [(0x13, 0, DocumentType)])], True) + dg1 = pack([(0x61, 0, [(0x13, 0, bytes(DocumentType, "ascii"))])], True) else: dg1 = None if (IssuingState.rstrip() != ""): - dg2 = pack([(0x62, 0, [(0x13, 0, IssuingState)])], True) + dg2 = pack([(0x62, 0, [(0x13, 0, bytes(IssuingState, "ascii"))])], True) else: dg2 = None if (DateOfExpiry.rstrip() != ""): - dg3 = pack([(0x63, 0, [(0x12, 0, DateOfExpiry)])], True) + dg3 = pack([(0x63, 0, [(0x12, 0, bytes(DateOfExpiry, "ascii"))])], True) else: dg3 = None if (GivenNames.rstrip() != ""): - dg4 = pack([(0x64, 0, [(0x0C, 0, GivenNames)])], True) + dg4 = pack([(0x64, 0, [(0x0C, 0, bytes(GivenNames, "ascii"))])], True) else: dg4 = None if (FamilyNames.rstrip() != ""): - dg5 = pack([(0x65, 0, [(0x0C, 0, FamilyNames)])], True) + dg5 = pack([(0x65, 0, [(0x0C, 0, bytes(FamilyNames, "ascii"))])], True) else: dg5 = None if (ReligiousArtisticName.rstrip() != ""): - dg6 = pack([(0x66, 0, [(0x0C, 0, ReligiousArtisticName)])], True) + dg6 = pack([(0x66, 0, [(0x0C, 0, bytes(ReligiousArtisticName, "ascii"))])], True) else: dg6 = None if (AcademicTitle.rstrip() != ""): - dg7 = pack([(0x67, 0, [(0x0C, 0, AcademicTitle)])], True) + dg7 = pack([(0x67, 0, [(0x0C, 0, bytes(AcademicTitle, "ascii"))])], True) else: dg7 = None if (DateOfBirth.rstrip() != ""): - dg8 = pack([(0x68, 0, [(0x12, 0, DateOfBirth)])], True) + dg8 = pack([(0x68, 0, [(0x12, 0, bytes(DateOfBirth, "ascii"))])], True) else: dg8 = None if (PlaceOfBirth.rstrip() != ""): - dg9 = pack([(0x69, 0, [(0xA1, 0, [(0x0C, 0, PlaceOfBirth)])])], + dg9 = pack([(0x69, 0, [(0xA1, 0, [(0x0C, 0, bytes(PlaceOfBirth, "ascii"))])])], True) else: dg9 = None if (Nationality.rstrip() != ""): - dg10 = pack([(0x6A, 0, [(0x13, 0, Nationality)])], True) + dg10 = pack([(0x6A, 0, [(0x13, 0, bytes(Nationality, "ascii"))])], True) else: dg10 = None if (Sex.rstrip() != ""): - dg11 = pack([(0x6B, 0, [(0x13, 0, Sex)])], True) + dg11 = pack([(0x6B, 0, [(0x13, 0, bytes(Sex, "ascii"))])], True) else: dg11 = None if (dg12_param.rstrip() != ""): - dg12 = dg12_param + dg12 = bytes(dg12_param, "ascii") else: dg12 = None if (BirthName.rstrip() != ""): - dg13 = pack([(0x6D, 0, [(0x0C, 0, BirthName)])], True) + dg13 = pack([(0x6D, 0, [(0x0C, 0, bytes(BirthName, "ascii"))])], True) else: dg13 = None if (dg14_param.rstrip() != ""): - dg14 = dg14_param + dg14 = bytes(dg14_param, "ascii") else: dg14 = None if (dg15_param.rstrip() != ""): - dg15 = dg15_param + dg15 = bytes(dg15_param, "ascii") else: dg15 = None if (dg16_param.rstrip() != ""): - dg16 = dg16_param + dg16 = bytes(dg16_param, "ascii") else: dg16 = None if (PlaceOfResidence.rstrip() != ""): dg17 = pack([(0x71, 0, [(0x30, 0, [ - (0xAA, 0, [(0x0C, 0, Street)]), - (0xAB, 0, [(0x0C, 0, City)]), - (0xAD, 0, [(0x13, 0, Country)]), - (0xAE, 0, [(0x13, 0, ZIP)]) + (0xAA, 0, [(0x0C, 0, bytes(Street, "ascii"))]), + (0xAB, 0, [(0x0C, 0, bytes(City, "ascii"))]), + (0xAD, 0, [(0x13, 0, bytes(Country, "ascii"))]), + (0xAE, 0, [(0x13, 0, bytes(ZIP, "ascii"))]) ])])], True) else: dg17 = None if (CommunityID.rstrip() != ""): - dg18 = pack([(0x72, 0, [(0x04, 0, CommunityID_Binary)])], True) + dg18 = pack([(0x72, 0, [(0x04, 0, bytes(CommunityID_Binary, "ascii"))])], True) else: dg18 = None if (ResidencePermit1.rstrip() != ""): dg19 = pack([(0x73, 0, [(0xA1, 0, - [(0x0C, 0, ResidencePermit1)])])], True) + [(0x0C, 0, bytes(ResidencePermit1, "ascii"))])])], True) else: dg19 = None if (ResidencePermit1.rstrip() != ""): dg20 = pack([(0x74, 0, [(0xA1, 0, - [(0x0C, 0, ResidencePermit2)])])], True) + [(0x0C, 0, bytes(ResidencePermit2, "ascii"))])])], True) else: dg20 = None if (dg21_param.rstrip() != ""): - dg21 = dg21_param + dg21 = bytes(dg21_param, "ascii") else: dg21 = None diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py b/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py index e1d4608..5017c9c 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/CryptoUtils.py @@ -240,5 +240,5 @@ def _makesalt(): 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, "./") diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/SEutils.py b/virtualsmartcard/src/vpicc/virtualsmartcard/SEutils.py index 36deb2d..6183d31 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/SEutils.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/SEutils.py @@ -297,7 +297,7 @@ class Security_Environment(object): to_authenticate = vsCrypto.append_padding(self.cct.blocklength, to_authenticate) else: - to_authenticate = "" + to_authenticate = b"" for tlv in structure: tag, length, value = tlv @@ -407,7 +407,7 @@ class Security_Environment(object): if p2 is None: p2 = CAPDU.p2 # FIXME: - # if expected != "": + # if expected != b"": # raise SwError(SW["ERR_SECMESSOBJECTSMISSING"]) if isinstance(le, bytes): @@ -419,7 +419,7 @@ class Security_Environment(object): le = le_int c = C_APDU(cla=cla, ins=ins, p1=p1, p2=p2, le=le, - data="".join(return_data)) + data=b"".join(return_data)) return c def protect_response(self, sw, result): @@ -429,7 +429,7 @@ class Security_Environment(object): :returns: the protected data and the SW bytes """ - return_data = "" + return_data = b"" # if sw == SW["NORMAL"]: # sw = inttostring(sw) # length = len(sw) @@ -437,9 +437,9 @@ class Security_Environment(object): # tlv_sw = pack([(tag,length,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 = "\x01" + encrypted + encrypted = b"\x01" + encrypted encrypted_tlv = pack([( SM_Class["CRYPTOGRAM_PADDING_INDICATOR_ODD"], len(encrypted), @@ -497,7 +497,7 @@ class Security_Environment(object): response_data = self.decipher(p1, p2, data) if p1 == 0x00: - assert response_data == "" + assert response_data == b"" return SW["NORMAL"], response_data @@ -531,11 +531,11 @@ class Security_Environment(object): if self.dst.key is None: raise SwError(SW["ERR_CONDITIONNOTSATISFIED"]) - to_sign = "" + to_sign = b"" if p2 == 0x9A: # Data to be signed to_sign = data elif p2 == 0xAC: # Data objects, sign values - to_sign = "" + to_sign = b"" structure = unpack(data) for tag, length, value in structure: to_sign += value @@ -570,8 +570,8 @@ class Security_Environment(object): Data field must contain a cryptographic checksum (tag 0x8E) and a plain value (tag 0x80) """ - plain = "" - cct = "" + plain = b"" + cct = b"" algo = self.cct.algorithm key = self.cct.key @@ -585,7 +585,7 @@ class Security_Environment(object): plain = value elif tag == 0x8E: cct = value - if plain == "" or cct == "": + if plain == b"" or cct == b"": raise SwError(SW["ERR_SECMESSOBJECTSMISSING"]) else: my_cct = vsCrypto.crypto_checksum(algo, key, plain, iv) @@ -601,8 +601,8 @@ class Security_Environment(object): (0x9E) """ key = self.dst.key - to_sign = "" - signature = "" + to_sign = b"" + signature = b"" if key is None: raise SwError(SW["ERR_CONDITIONNOTSATISFIED"]) @@ -618,7 +618,7 @@ class Security_Environment(object): elif tag == 0xBC: pass - if to_sign == "" or signature == "": + if to_sign == b"" or signature == b"": raise SwError(SW["ERR_SECMESSOBJECTSMISSING"]) my_signature = key.sign(value) diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/SmartcardFilesystem.py b/virtualsmartcard/src/vpicc/virtualsmartcard/SmartcardFilesystem.py index 3b4b6b5..8bbf118 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/SmartcardFilesystem.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/SmartcardFilesystem.py @@ -89,7 +89,7 @@ def getfile_byrefdataobj(mf, refdataobjs): if length % 2 == 1: raise NotImplementedError else: - if newvalue[:2] == "\x3f\x00": + if newvalue[:2] == b"\x3f\x00": # absolute file = walk(mf, newvalue[2:]) else: @@ -419,7 +419,7 @@ class DF(File): def __init__(self, parent, fid, filedescriptor=FDB["NOTSHAREABLEFILE"] | FDB["DF"], 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. """ @@ -1451,7 +1451,7 @@ class TransparentStructureEF(EF): filedescriptor=FDB["EFSTRUCTURE_TRANSPARENT"], lifecycle=LCB["ACTIVATED"], simpletlv_data=None, bertlv_data=None, - datacoding=DCB["ONETIMEWRITE"], shortfid=0, data=""): + datacoding=DCB["ONETIMEWRITE"], shortfid=0, data=b""): """ See EF for more. """ @@ -1521,7 +1521,7 @@ class Record(object): identifier = make_property("identifier", "integer with 1 <= identifier <=" " 0xfe. The record's identifier.") """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. """ diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/HandlerTest.py b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/HandlerTest.py index 8ae34a7..cea78c7 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/HandlerTest.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/HandlerTest.py @@ -29,51 +29,51 @@ class HandlerTestOS(SmartcardOS): and handler_test(1). """ def __init__(self): - lastCommandOffcut = '' + lastCommandOffcut = b'' def getATR(self): - return '\x3B\xD6\x18\x00\x80\xB1\x80\x6D\x1F\x03\x80\x51\x00\x61' + \ - '\x10\x30\x9E' + return b'\x3B\xD6\x18\x00\x80\xB1\x80\x6D\x1F\x03\x80\x51\x00\x61' + \ + b'\x10\x30\x9E' def __output_from_le(self, msg): le = (ord(msg[2]) << 8) + ord(msg[3]) return ''.join([bytes(num & 0xff) for num in xrange(le)]) def execute(self, msg): - ok = '\x90\x00' - error = '\x6d\x00' - if (msg == '\x00\xA4\x04\x00\x06\xA0\x00\x00\x00\x18\x50' or - msg == '\x00\xA4\x04\x00\x06\xA0\x00\x00\x00\x18\xFF'): + ok = b'\x90\x00' + error = b'\x6d\x00' + if (msg == b'\x00\xA4\x04\x00\x06\xA0\x00\x00\x00\x18\x50' or + msg == b'\x00\xA4\x04\x00\x06\xA0\x00\x00\x00\x18\xFF'): logging.info('Select applet') return ok - elif msg.startswith('\x80\x38\x00'): + elif msg.startswith(b'\x80\x38\x00'): logging.info('Time Request') return ok - elif msg == '\x80\x30\x00\x00': + elif msg == b'\x80\x30\x00\x00': logging.info('Case 1, APDU') return ok - elif msg == '\x80\x30\x00\x00\x00': + elif msg == b'\x80\x30\x00\x00\x00': logging.info('Case 1, TPDU') return ok - elif msg.startswith('\x80\x32\x00\x00'): + elif msg.startswith(b'\x80\x32\x00\x00'): logging.info('Case 3') return ok - elif msg.startswith('\x80\x34'): + elif msg.startswith(b'\x80\x34'): logging.info('Case 2') 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]): logging.info('Case 4, TPDU') self.lastCommandOffcut = self.__output_from_le(msg) if len(self.lastCommandOffcut) > 0xFF: - return '\x61\x00' - return '' + bytes(len(self.lastCommandOffcut) & 0xFF) + return b'\x61\x00' + return b'' + bytes(len(self.lastCommandOffcut) & 0xFF) elif len(msg) == 6+ord(msg[4]): logging.info('Case 4, APDU') return self.__output_from_le(msg) + ok else: return error - elif msg.startswith('\x80\xC0\x00\x00'): + elif msg.startswith(b'\x80\xC0\x00\x00'): logging.info('Get response') out = self.lastCommandOffcut[:ord(msg[4])] self.lastCommandOffcut = self.lastCommandOffcut[ord(msg[4]):] diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/cryptoflex.py b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/cryptoflex.py index 4634424..a9ac94a 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/cryptoflex.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/cryptoflex.py @@ -34,7 +34,7 @@ import logging class CryptoflexOS(Iso7816OS): def __init__(self, mf, sam, ins2handler=None, maxle=MAX_SHORT_LE): 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 notImplemented(*argz, **args): @@ -56,7 +56,7 @@ class CryptoflexOS(Iso7816OS): logging.info(e.message) # traceback.print_exception(*sys.exc_info()) sw = e.sw - result = "" + result = b"" r = self.formatResult(c.ins, c.le, result, sw) return r @@ -179,7 +179,7 @@ class CryptoflexSAM(SAM): def internal_authenticate(self, p1, p2, data): data = data[::-1] # Reverse Byte order sw, data = SAM.internal_authenticate(self, p1, p2, data) - if data != "": + if data != b"": data = data[::-1] return sw, data @@ -189,7 +189,7 @@ class CryptoflexMF(MF): # {{{ @staticmethod def create(p1, p2, data): - if data[0:2] != "\xff\xff": + if data[0:2] != b"\xff\xff": raise SwError(SW["ERR_INCORRECTPARAMETERS"]) args = { @@ -197,11 +197,11 @@ class CryptoflexMF(MF): # {{{ "filedescriptor": 0, "fid": stringtoint(data[4:6]), } - if data[6] == "\x01": + if data[6] == b"\x01": args["data"] = bytes(0)*stringtoint(data[2:4]) args["filedescriptor"] = FDB["EFSTRUCTURE_TRANSPARENT"] new_file = TransparentStructureEF(**args) - elif data[6] == "\x02": + elif data[6] == b"\x02": if len(data) > 16: args["maxrecordsize"] = stringtoint(data[16]) elif p2: @@ -210,15 +210,15 @@ class CryptoflexMF(MF): # {{{ args["filedescriptor"] = FDB["EFSTRUCTURE_LINEAR_FIXED_" "NOFURTHERINFO"] new_file = RecordStructureEF(**args) - elif data[6] == "\x03": + elif data[6] == b"\x03": args["filedescriptor"] = FDB["EFSTRUCTURE_LINEAR_VARIABLE_" "NOFURTHERINFO"] new_file = RecordStructureEF(**args) - elif data[6] == "\x04": + elif data[6] == b"\x04": args["filedescriptor"] = FDB["EFSTRUCTURE_CYCLIC_NOFURTHERINFO"] new_file = RecordStructureEF(**args) - elif data[6] == "\x38": - if data[12] != "\x03": + elif data[6] == b"\x38": + if data[12] != b"\x03": raise SwError(SW["ERR_INCORRECTPARAMETERS"]) new_file = DF(**args) else: diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/nPA.py b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/nPA.py index 6d6a573..3633c7e 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/cards/nPA.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/cards/nPA.py @@ -431,7 +431,7 @@ class nPA_SE(Security_Environment): def __eac_ca(self, data): tlv_data = nPA_SE.__unpack_general_authenticate(data) - pubkey = "" + pubkey = b"" for tag, length, value in tlv_data: if tag == 0x80: pubkey = value diff --git a/virtualsmartcard/src/vpicc/virtualsmartcard/utils.py b/virtualsmartcard/src/vpicc/virtualsmartcard/utils.py index 2c981b7..412c950 100644 --- a/virtualsmartcard/src/vpicc/virtualsmartcard/utils.py +++ b/virtualsmartcard/src/vpicc/virtualsmartcard/utils.py @@ -175,7 +175,7 @@ class APDU(object): def _deldata(self): del self._data - self.data = "" + self.data = b"" data = property(_getdata, _setdata, None, "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.__extended_length = False if len(apdu) == 4: # case 1 - self.data = "" + self.data = b"" elif (len(apdu) >= 7) and (apdu[4] == 0): # extended length apdu self.__extended_length = True if len(apdu) == 7: # case 2 extended length self.Le = (apdu[-2] << 8) + apdu[-1] - self.data = "" + self.data = b"" else: # case 3, 4 extended length self.Lc = (apdu[5] << 8) + apdu[6] if len(apdu) == 7 + self.Lc: # case 3 extended length @@ -254,7 +254,7 @@ class C_APDU(APDU): else: # short apdu if len(apdu) == 5: # case 2 short apdu self.Le = apdu[-1] - self.data = "" + self.data = b"" elif len(apdu) > 5: # case 3, 4 short apdu self.Lc = apdu[4] if len(apdu) == 5 + self.Lc: # case 3