diff --git a/mrpkey.py b/mrpkey.py index 2ffe33b..f6201a5 100755 --- a/mrpkey.py +++ b/mrpkey.py @@ -1283,6 +1283,7 @@ def help(): if DEBUG or TEST: print 'Auth message: ' + resp print 'Auth MAC: ' + respmac + ' (verified)' + tdes= DES3.new(Kenc,DES.MODE_CBC,passport.DES_IV) decresp= passport.ToHex(tdes.decrypt(passport.ToBinary(resp))) if DEBUG or TEST: print 'Decrypted Auth Response: ' + decresp diff --git a/rfidiot/RFIDIOt.py b/rfidiot/RFIDIOt.py index 23a1f9f..2e00244 100755 --- a/rfidiot/RFIDIOt.py +++ b/rfidiot/RFIDIOt.py @@ -81,6 +81,7 @@ def __init__(self,readernum,reader,port,baud,to,debug,noinit,nfcreader): self.DEBUG= debug self.NoInit= noinit self.NFCReader= nfcreader + self.timeout= to if not self.NoInit: if self.readertype == self.READER_PCSC: try: @@ -507,7 +508,7 @@ def __init__(self,readernum,reader,port,baud,to,debug,noinit,nfcreader): 'N':'No precise diagnosis', 'PC00':'No TAG present!', 'PC01':'PCSC Communications Error', - 'PN00': 'PN531 Communications Error', + 'PN00': 'PN53x Communications Error', 'R':'Block out of range', 'X':'Authentication failed', } @@ -1372,7 +1373,7 @@ def libnfc_mifare_login(self,block,key,keytype): keytype= '%02x' % pynfc.MC_AUTH_A loginblock= '%02x' % block #if self.tagtype == self.ACS_TAG_MIFARE_1K or self.tagtype == self.ACS_TAG_MIFARE_4K: - ret, self.errorcode= self.nfc.sendAPDU([keytype]+[loginblock]+[key]+[self.uid]) + ret, self.errorcode= self.nfc.sendAPDU([keytype]+[loginblock]+[key]+[self.uid], self.timeout) if not ret: self.errorcode= self.ISO_SECURE return False @@ -1383,7 +1384,7 @@ def libnfc_mifare_read_block(self, block): apdu += '%02X' % pynfc.MC_READ # mifare read hexblock= '%02x' % block apdu.append(hexblock) - ret, dat= self.nfc.sendAPDU(apdu) + ret, dat= self.nfc.sendAPDU(apdu, self.timeout) if not ret: self.errorcode= dat return False @@ -1530,10 +1531,13 @@ def send_apdu(self,option,pcb,cid,nad,cla,ins,p1,p2,lc,data,le): if self.readertype == self.READER_LIBNFC: if self.DEBUG: print 'In send_apdu - for libnfc:', cla+ins+p1+p2+lc+data+le - ret, result = self.nfc.sendAPDU(cla+ins+p1+p2+lc+data+le) + ret, result = self.nfc.sendAPDU(cla+ins+p1+p2+lc+data+le, self.timeout) + if not ret: + self.errorcode = 'PN00' + return False self.data = result[0:-4] self.errorcode = result[len(result)-4:len(result)] - if not ret or self.errorcode != self.ISO_OK: + if self.errorcode != self.ISO_OK: return False return True if self.readertype == self.READER_ANDROID: @@ -1708,7 +1712,7 @@ def readblock(self,block): apdu += '%02X' % pynfc.MC_READ # mifare read hexblock= '%04x' % block apdu.append(hexblock) - ret, self.errorcode= self.nfc.sendAPDU(apdu) + ret, self.errorcode= self.nfc.sendAPDU(apdu, self.timeout) if not ret: return False self.errorcode= self.ISO_OK diff --git a/rfidiot/pynfc.py b/rfidiot/pynfc.py index 094b3ec..425e03b 100755 --- a/rfidiot/pynfc.py +++ b/rfidiot/pynfc.py @@ -308,8 +308,9 @@ def initlibnfc(self): self.libnfc.nfc_device_get_name.argtypes = [ctypes.c_void_p] self.libnfc.nfc_open.restype = ctypes.c_void_p self.libnfc.nfc_initiator_init.argtypes = [ctypes.c_void_p] - self.libnfc.nfc_device_set_property_bool.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_bool]; + self.libnfc.nfc_device_set_property_bool.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_bool] self.libnfc.nfc_close.argtypes = [ctypes.c_void_p] + self.libnfc.nfc_perror.argtypes = [ctypes.c_void_p, ctypes.c_char_p] self.libnfc.nfc_initiator_list_passive_targets.argtypes = [ctypes.c_void_p, ctypes.Structure, ctypes.c_void_p, ctypes.c_size_t] self.libnfc.nfc_initiator_transceive_bytes.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t, ctypes.c_void_p, ctypes.c_size_t, ctypes.c_uint32] self.libnfc.nfc_init(ctypes.byref(self.context)) @@ -461,7 +462,7 @@ def configMifare(self): self.libnfc.nfc_device_set_property_bool(self.device, NP_EASY_FRAMING, True) self.selectISO14443A() - def sendAPDU(self, apdu): + def sendAPDU(self, apdu, timeout=None): apdu= "".join([x for x in apdu]) txData = [] for i in range(0, len(apdu), 2): @@ -475,10 +476,11 @@ def sendAPDU(self, apdu): if rfidiotglobals.Debug: self.log.debug("Sending %d byte APDU: %s" % (len(tx),"".join(["%02x" % x for x in tx]))) - rxlen = self.libnfc.nfc_initiator_transceive_bytes(self.device, ctypes.byref(tx), ctypes.c_size_t(len(tx)), ctypes.byref(rx), ctypes.c_size_t(len(rx)), -1) + rxlen = self.libnfc.nfc_initiator_transceive_bytes(self.device, ctypes.byref(tx), ctypes.c_size_t(len(tx)), ctypes.byref(rx), ctypes.c_size_t(len(rx)), int(timeout * 1000) if timeout is not None else -1) if rfidiotglobals.Debug: self.log.debug('APDU rxlen = ' + str(rxlen)) if rxlen < 0: + self.libnfc.nfc_perror(self.device, "nfc_initiator_transceive_bytes") if rfidiotglobals.Debug: self.log.error("Error sending/receiving APDU") return False, rxlen