Skip to content

Commit

Permalink
Merge pull request #33 from doegox/fixes
Browse files Browse the repository at this point in the history
Excellent, thanks!!
  • Loading branch information
AdamLaurie authored Jan 8, 2019
2 parents 06a75f2 + 839ced8 commit 4eaf194
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
1 change: 1 addition & 0 deletions mrpkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions rfidiot/RFIDIOt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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',
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions rfidiot/pynfc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down

0 comments on commit 4eaf194

Please sign in to comment.