def run(self): """ Waits for a card to get into reader field. Reads its UID and compares to database of authorized users. Unlocks lock if authorized. """ self.nfc = NFCDevice() self.hmacAuthenticator = YubikeyHMACAuthenthicator( config.authDbFilename, self.nfc) self.desfireAuthenticator = DesfireEd25519Authenthicator( config.authDbFilename, self.nfc, config.desfirePubkey.decode("hex")) #self.nfc.pollNr = 0xFF #poll indefinitely while True: try: uid_hex = hexlify(self.nfc.scanUID()) logging.debug("Got UID %s", uid_hex) if len(uid_hex) > 0: self.actOnUid(uid_hex) else: #prevent busy loop if reader goes awry e = threading.Event() e.wait(timeout=0.3) except NFCError, e: #this exception happens also when scanUID finds no cards logging.debug( "Failed to find RFID cards in reader's field: %s", e) e = threading.Event() e.wait(timeout=0.2) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") self.nfc.close() self.nfc.unload() self.unlocker.lock() sys.exit(2)
class NFCScanner(object): """Thread reading data from NFC reader""" def __init__(self, config, msgQueue, ircThread): """Create worker reading UIDs from PN53x reader. """ self.authenticator = UidAuthenticator(config.authDbFilename) self.hmacAuthenticator = None self.desfireAuthenticator = None self.unknownUidTimeoutSecs = config.unknownUidTimeoutSecs self.lockOpenedSecs = config.lockOpenedSecs self.msgQueue = msgQueue self.ircThread = ircThread unlockerClassName = config.unlocker unlockerClass = getattr(unlocker, unlockerClassName) self.unlocker = unlockerClass(config) def run(self): """ Waits for a card to get into reader field. Reads its UID and compares to database of authorized users. Unlocks lock if authorized. """ self.nfc = NFCDevice() self.hmacAuthenticator = YubikeyHMACAuthenthicator( config.authDbFilename, self.nfc ) self.desfireAuthenticator = DesfireEd25519Authenthicator( config.authDbFilename, self.nfc, config.desfirePubkey.decode("hex") ) #self.nfc.pollNr = 0xFF #poll indefinitely while True: try: uid_hex = hexlify(self.nfc.scanUID()) logging.debug("Got UID %s", uid_hex) if len(uid_hex) > 0: self.actOnUid(uid_hex) else: #prevent busy loop if reader goes awry e = threading.Event() e.wait(timeout=0.3) except NFCError, e: #this exception happens also when scanUID finds no cards logging.debug("Failed to find RFID cards in reader's field: %s", e) e = threading.Event() e.wait(timeout=0.2) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") self.nfc.close() self.nfc.unload() self.unlocker.lock() sys.exit(2) except Exception: logging.exception("Exception in main unlock thread")
class NFCScanner(object): """Thread reading data from NFC reader""" def __init__(self, config, msgQueue, ircThread): """Create worker reading UIDs from PN53x reader. """ self.authenticator = UidAuthenticator(config.authDbFilename) self.hmacAuthenticator = None self.desfireAuthenticator = None self.unknownUidTimeoutSecs = config.unknownUidTimeoutSecs self.lockOpenedSecs = config.lockOpenedSecs self.msgQueue = msgQueue self.ircThread = ircThread unlockerClassName = config.unlocker unlockerClass = getattr(unlocker, unlockerClassName) self.unlocker = unlockerClass(config) def run(self): """ Waits for a card to get into reader field. Reads its UID and compares to database of authorized users. Unlocks lock if authorized. """ self.nfc = NFCDevice() self.hmacAuthenticator = YubikeyHMACAuthenthicator( config.authDbFilename, self.nfc) self.desfireAuthenticator = DesfireEd25519Authenthicator( config.authDbFilename, self.nfc, config.desfirePubkey.decode("hex")) #self.nfc.pollNr = 0xFF #poll indefinitely while True: try: uid_hex = hexlify(self.nfc.scanUID()) logging.debug("Got UID %s", uid_hex) if len(uid_hex) > 0: self.actOnUid(uid_hex) else: #prevent busy loop if reader goes awry e = threading.Event() e.wait(timeout=0.3) except NFCError, e: #this exception happens also when scanUID finds no cards logging.debug( "Failed to find RFID cards in reader's field: %s", e) e = threading.Event() e.wait(timeout=0.2) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") self.nfc.close() self.nfc.unload() self.unlocker.lock() sys.exit(2) except Exception: logging.exception("Exception in main unlock thread")
def run(self): """ Waits for a card to get into reader field. Reads its UID and compares to database of authorized users. Unlocks lock if authorized. """ self.nfc = NFCDevice() self.hmacAuthenticator = YubikeyHMACAuthenthicator( config.authDbFilename, self.nfc ) #self.nfc.pollNr = 0xFF #poll indefinitely while True: try: uid_hex = hexlify(self.nfc.scanUID()) logging.debug("Got UID %s", uid_hex) if len(uid_hex) > 0: self.actOnUid(uid_hex) else: #prevent busy loop if reader goes awry time.sleep(0.3) except NFCError, e: #this exception happens also when scanUID times out logging.debug("Failed to wait for RFID card: %s", e) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") self.nfc.close() self.nfc.unload() self.unlocker.lock() sys.exit(2)
def run(self): """ Waits for a card to get into reader field. Reads its UID and compares to database of authorized users. Unlocks lock if authorized. """ self.nfc = NFCDevice() self.hmacAuthenticator = YubikeyHMACAuthenthicator( config.authDbFilename, self.nfc ) self.desfireAuthenticator = DesfireEd25519Authenthicator( config.authDbFilename, self.nfc, config.desfirePubkey.decode("hex") ) #self.nfc.pollNr = 0xFF #poll indefinitely while True: try: uid_hex = hexlify(self.nfc.scanUID()) logging.debug("Got UID %s", uid_hex) if len(uid_hex) > 0: self.actOnUid(uid_hex) else: #prevent busy loop if reader goes awry e = threading.Event() e.wait(timeout=0.3) except NFCError, e: #this exception happens also when scanUID finds no cards logging.debug("Failed to find RFID cards in reader's field: %s", e) e = threading.Event() e.wait(timeout=0.2) except KeyboardInterrupt: logging.info("Exiting on keyboard interrupt") self.nfc.close() self.nfc.unload() self.unlocker.lock() sys.exit(2)
if len(sys.argv) > 1: apdu_test = sys.argv[1] print "Available tests: %s" % ", ".join( sorted(tests.keys() + ["desfire-ndef4"])) #desfire-ndef4 has a bit postprocessing print "Selected test: %s" % apdu_test # select apdus according to test name if apdu_test in tests: hex_apdus = tests[apdu_test] apdus = [hex_apdu.replace(" ", "").decode("hex") for hex_apdu in hex_apdus] try: nfc = NFCDevice() uid = nfc.scanUID() print "UID", hexlify(uid) #nfc.close() #nfc.open() print "Now trying to send ISO14443-4 APDUs" try: #nfc.selectPassiveTarget() for apdu in apdus: print "Command APDU:", formatAPDU(apdu) rapdu = nfc.sendAPDU(apdu) print "Response APDU valid: %s, SW %04x, data %s" % ( rapdu.valid(), rapdu.sw(), hexlify(rapdu.data())) except NFCError, e: print "Failed to transmit APDU:", e.what()
from binascii import hexlify from nfc_smartcard import NFCDevice, NFCError from sign_uid import signUid if len(sys.argv) < 2: print "Usage: write_signed_ndef_on_desfire.py private_key_in_hex" sys.exit(3) tempFd = None tempFname = None try: print "Opening NFC reader" nfc = NFCDevice() nfc.pollNr = 0xFF #poll indefinitely print "Waiting for Desfire card to appear in the reader" uid_hex = hexlify(nfc.scanUID()) key = sys.argv[1].decode("hex") print "Got UID %s" % uid_hex signature = signUid(key, uid_hex.decode("hex")) (tempFd, tempFname) = tempfile.mkstemp(dir="/tmp") signatureJson = '{"brmdoorSignature": "%s"}' % signature.encode("hex") print "Writing signature JSON:", signatureJson os.write(tempFd, signatureJson) os.close(tempFd) print "Wrote signature into %s" % tempFname except NFCError, e: #this exception happens also when scanUID times out
# default test if not selected otherwise in sys.argv[1] apdu_test = "ndef4" if len(sys.argv) > 1: apdu_test = sys.argv[1] print "Available tests: %s" % ", ".join(sorted(tests.keys())) print "Selected test: %s" % apdu_test # select apdus according to test name hex_apdus = tests[apdu_test] apdus = [hex_apdu.replace(" ","").decode("hex") for hex_apdu in hex_apdus] try: nfc = NFCDevice() uid = nfc.scanUID() print "UID", hexlify(uid) #nfc.close() #nfc.open() print "Now trying to send ISO14443-4 APDUs" try: #nfc.selectPassiveTarget() for apdu in apdus: print "Command APDU:", formatAPDU(apdu) rapdu = nfc.sendAPDU(apdu) print "Response APDU valid: %s, SW %04x, data %s" % (rapdu.valid(), rapdu.sw(), hexlify(rapdu.data())) except NFCError, e: print "Failed to transmit APDU:", e.what()
# default test if not selected otherwise in sys.argv[1] apdu_test = "desfire-ndef4" if len(sys.argv) > 1: apdu_test = sys.argv[1] print "Available tests: %s" % ", ".join(sorted(tests.keys() + ["desfire-ndef4"])) #desfire-ndef4 has a bit postprocessing print "Selected test: %s" % apdu_test # select apdus according to test name if apdu_test in tests: hex_apdus = tests[apdu_test] apdus = [hex_apdu.replace(" ","").decode("hex") for hex_apdu in hex_apdus] try: nfc = NFCDevice() uid = nfc.scanUID() print "UID", hexlify(uid) #nfc.close() #nfc.open() print "Now trying to send ISO14443-4 APDUs" try: #nfc.selectPassiveTarget() for apdu in apdus: print "Command APDU:", formatAPDU(apdu) rapdu = nfc.sendAPDU(apdu) print "Response APDU valid: %s, SW %04x, data %s" % (rapdu.valid(), rapdu.sw(), hexlify(rapdu.data())) except NFCError, e: print "Failed to transmit APDU:", e.what()