Giter VIP home page Giter VIP logo

apple-home-key-reader's Introduction

Apple Home Key Reader

![Home Key on an iPhone with PN532] ![Home Key on an Apple Watch with PN532] ![Home Key on an Apple Watch with ACR122U]

Overview

This project offers a demonstration of an Apple Home Key Reader built in Python. It includes:

  • Fully functional Apple Home Key NFC authentication;
  • NFC Express mode support;
  • HAP configuration (as a virtual lock);

It's intended for makers and developers interested in building practical and user-friendly applications using this example.
Feel free to give it a try! :)

Requirements

Running this project requires the following:

  • An operating system: Linux or macOS;
  • Python installation version 3.9 or higher (earlier versions might work but haven't been tested);
  • A PN532 module connected to a PC or SBC via UART (not through I2C or SPI);
  • Either Ethernet or Wi-Fi to ensure HAP can be discovered.

When using a PC, connect the PN532 to a UART adapter, and then connect the adapter to the PC as follows:

![Connecting PN532]

Installation & running

Code has been tested using Python 3.9, 3.10, 3.11 on macOS and Linux. Windows support status is unknown.
Other OS + Python version combos were not verified but may still work.

  1. (Optionally). Create and activate a venv:

    python3 -m venv ./venv
    
    source ./venv/bin/activate
    
  2. Install dependencies:

    python3 -m pip install -r requirements.txt
    
  3. Configure the application via the text editor of choice:

    nano configuration.json 
    

    3.1. Find NFC device port:

     # linux
     ls /dev/*
    
     # macOS
     ls /dev/tty.*
    

    3.2. Copy the port without the tty. part, insert it into port field;
    3.3. If you don't use a PN532, set broadcast to false and driver to appropriate value, otherwise leave as is.

  4. Run application:

    python3 main.py
    

Configuration

Configuration is done via a JSON file configuration.json, with the following 4 blocks configurable:

  • logging:
    • level: level to log messages at. All logs related to this codebase use INFO level (20).
  • nfc: configuration of the NFC frontend used:
    • port: path to serial port of the connected NFC module;
    • driver: nfcpy driver to use with your device. Implementation was only tested for PN532, other modules have to be verified separately (if supported by nfcpy);
    • broadcast: configures if to use broadcast frames and ECP. If this parameter is true but used NFC device is not based on PN532, will cause an exception to be raised, set to false only if such problems occur;
  • hap: configuration of the HAP-python library, better left unchanged;
    • port: network port of the virtual accessory;
    • persist: file to store HAP-python pairing data in.
    • default: default state of the virtual lock accessory;
      Possible values: locked unlocked. Value locked is default;
  • homekey:
    • persist: file to save endpoint and issuer configuration data in;
    • express: configures if to trigger express mode on devices that have it enabled. If set to false, bringing a device to the reader will display the key on the screen while asking for biometric authentication. Beware that this doesn't increase security as express mode is disabled on ECP level, so a would-be attacker could always 'excite' the device with express ECP frame and bring it to the reader;
    • finish: color of the home key art to display on your device. Usually, finish of the first NFC lock added to your home defines which color the keys are going to be, even if more locks are added;
      Possible values: black tan gold silver;
    • flow: minimum viable digital key transaction flow to do. By default, reader attempts to do as least actions as possible, with fallback to next level of authentication only happening if the previous one failed. Setting this setting to standard or attestation will force protocol to fall back to those flows even if they're not required for successful auth.
      Possible values: fast standard attestation.

Project structure

Project is split into following primary modules:

  • main.py - initialize and start all primary classes, configure device ports, etc;
  • accessory.py - service definitions for HAP, contains no logic, with it forwarded to service.py;
  • service.py - implements core application functionality, parsing HAP messages and generating responses to them, initiating NFC communication.
  • homekey.py - homekey NFC part of the protocol implementation;

Other modules:

  • repository.py - implements homekey configuration state storage;
  • bfclf.py - implementation of Broadcast frames for pn532;
  • entity.py - entity definitions;
  • util/* - protocol implementations, data structures, cryptography, other utility methods.

Two files will be created as the result of you running the application, assuming no settings were changed:

  • hap.state: contains pairing data needed for HAP-python;
  • homekey.json: contains all lock configuration data formatted in a human-readable form.

Terminology

  • EC: elliptic curve;
  • HAP: Homekit Accessory Protocol, aka Network/Bluetooth part;
  • Issuer: a party that enrolls endpoints. Each issuer is a person with an iCloud account;
  • Endpoint: a device that's enrolled/paired to the lock;
  • Enrollment: payload that contains data that was used to enroll the Endpoint to the device.
    Can be either hap, meaning that the Endpoint was added via HAP, or attestation, meaning that endpoint was enrolled via NFC attestation flow.

Contributing

The project is quite far from ideal since most of the codebase remains unchanged from my original local project, which was primarily utilized during the reverse-engineering phase.

As a result, there are numerous opportunities for enhancement:

  • Fixing potential typos in README and code;
  • Adding tests to insure stability if a refactor is made;
  • Re-writing pieces of service/homekey code to reduce code size, improve readability;
  • Improve logging for better protocol analysis;
  • HAP command implementations:
    • remove_device_credential;
    • get_nfc_access_control_point - no idea what it should do;
    • Re-test and verify validity of other methods;
  • Re-write NFC stack to improve device support.

In case you're planning on tackling one of those, feel free to raise an issue to discuss potential solution and approach.

Codebase is formatted with default black and isort configurations, linted with pylint.
Before making a contribution, verify that they were used for best code diffs and quality;

Notes

  • This code is provided as-is. Considering the sensitive nature of authentication and security, I assume no responsibility for any issues that may arise while using this project;
  • Information is provided here for learning and DIY purposes only, usage in commercial applications is highly discouraged.
  • Refrain from posting raw logs as they may contain sensitive information, such as reader private key, issuer id's, etc.
  • If you find a bug, feel free to raise an issue;

Credits

This project would have been impossible without the contributions of:

  • @kupa22 - for full HAP part analysis, NFC protocol research;
  • @kormax - ECP, NFC protocol research;

Special thanks to:

  • @gm3197 - for finding clues about ISO18013 being used in Home Key protocol;
  • @KhaosT - for creating a demo that caused Home Key to appear in Wallet, which partially inspired me/others to go on this journey;
  • @ikalchev and @bdraco for developing HAP-Python and helping with accepting in changes that were needed for this project;
  • Other people, who gave their input on demo app improvement.

References

apple-home-key-reader's People

Contributors

fussel132 avatar kormax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

apple-home-key-reader's Issues

Unhandled exception in runner homekey.

Hi,

The software works, but after a few opening and closing processes it gets stuck in an error loop.
To solve the problem, I have to completely power down my Pi and thus also the NFC reader and reboot.

Any ideas?

Raspberry Pi 4 with Raspbian OS, 64bit

Best regards
Christoph

Log:

[2023-12-10 17:59:44,546] [   ERROR] service           :178  Connecting to the NFC reader...
NoneType: None
[2023-12-10 17:59:44,546] [    INFO] __init__          :148  searching for reader on path tty:serial0:pn532
[2023-12-10 17:59:44,652] [   ERROR] threads           :19   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/home/christoph/venv/util/threads.py", line 17, in function_
    target(*args, **kwargs)
  File "/home/christoph/venv/service.py", line 181, in run
    self.clf.open(self.clf.path)
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 149, in open
    self.device = device.connect(path)
                  ^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/device.py", line 112, in connect
    device = driver.init(tty)
             ^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/pn532.py", line 411, in init
    if not transport.read(timeout=initial_timeout) == Chipset.ACK:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/transport.py", line 154, in read
    raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out
[2023-12-10 17:59:49,657] [   ERROR] service           :178  Connecting to the NFC reader...
NoneType: None
[2023-12-10 17:59:49,658] [    INFO] __init__          :148  searching for reader on path tty:serial0:pn532
[2023-12-10 17:59:49,763] [   ERROR] threads           :19   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/home/christoph/venv/util/threads.py", line 17, in function_
    target(*args, **kwargs)
  File "/home/christoph/venv/service.py", line 181, in run
    self.clf.open(self.clf.path)
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 149, in open
    self.device = device.connect(path)
                  ^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/device.py", line 112, in connect
    device = driver.init(tty)
             ^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/pn532.py", line 411, in init
    if not transport.read(timeout=initial_timeout) == Chipset.ACK:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/transport.py", line 154, in read
    raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out

After a restart of the software (without power cycle):

[2023-12-10 18:08:04,564] [    INFO] accessory_driver  :483  Loading Accessory state from `hap.state`
[2023-12-10 18:08:04,566] [    INFO] accessory_driver  :320  Starting the event loop
[2023-12-10 18:08:04,566] [   ERROR] service           :178  Connecting to the NFC reader...
NoneType: None
[2023-12-10 18:08:04,567] [    INFO] __init__          :148  searching for reader on path tty:serial0:pn532
[2023-12-10 18:08:04,569] [    INFO] accessory_driver  :372  Starting accessory NFC Lock on addresses ['172.30.160.67'], port 51926.
[2023-12-10 18:08:04,606] [    INFO] hap_protocol      :82   ('172.30.134.147', 64446): Connection made to NFC Lock
[2023-12-10 18:08:04,618] [    INFO] accessory         :138  get_hardware_finish
[2023-12-10 18:08:04,618] [    INFO] service           :284  get_hardware_finish=AQTj4+MA
[2023-12-10 18:08:04,618] [    INFO] accessory         :115  get_lock_current_state
[2023-12-10 18:08:04,618] [    INFO] accessory         :119  get_lock_target_state
[2023-12-10 18:08:04,618] [    INFO] accessory         :129  get_lock_version
[2023-12-10 18:08:04,619] [    INFO] accessory         :158  get_configuration_state
[2023-12-10 18:08:04,619] [    INFO] service           :339  get_configuration_state
[2023-12-10 18:08:04,619] [    INFO] accessory         :148  get_nfc_access_control_point
[2023-12-10 18:08:04,619] [    INFO] service           :297  get_nfc_access_control_point
[2023-12-10 18:08:04,619] [    INFO] accessory         :143  get_nfc_access_supported_configuration
[2023-12-10 18:08:04,620] [    INFO] service           :293  TODO get_nfc_access_supported_configuration=AQEQAgEQ
[2023-12-10 18:08:04,680] [   ERROR] threads           :19   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/home/christoph/venv/util/threads.py", line 17, in function_
    target(*args, **kwargs)
  File "/home/christoph/venv/service.py", line 181, in run
    self.clf.open(self.clf.path)
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 149, in open
    self.device = device.connect(path)
                  ^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/device.py", line 112, in connect
    device = driver.init(tty)
             ^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/pn532.py", line 411, in init
    if not transport.read(timeout=initial_timeout) == Chipset.ACK:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/transport.py", line 154, in read
    raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out
[2023-12-10 18:08:06,924] [    INFO] accessory         :119  get_lock_target_state
[2023-12-10 18:08:06,924] [    INFO] accessory         :115  get_lock_current_state
[2023-12-10 18:08:09,694] [   ERROR] service           :178  Connecting to the NFC reader...
NoneType: None
[2023-12-10 18:08:09,695] [    INFO] __init__          :148  searching for reader on path tty:serial0:pn532
[2023-12-10 18:08:09,803] [   ERROR] threads           :19   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/home/christoph/venv/util/threads.py", line 17, in function_
    target(*args, **kwargs)
  File "/home/christoph/venv/service.py", line 181, in run
    self.clf.open(self.clf.path)
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 149, in open
    self.device = device.connect(path)
                  ^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/device.py", line 112, in connect
    device = driver.init(tty)
             ^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/pn532.py", line 411, in init
    if not transport.read(timeout=initial_timeout) == Chipset.ACK:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/transport.py", line 154, in read
    raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out
[2023-12-10 18:08:14,808] [   ERROR] service           :178  Connecting to the NFC reader...
NoneType: None
[2023-12-10 18:08:14,809] [    INFO] __init__          :148  searching for reader on path tty:serial0:pn532
[2023-12-10 18:08:14,914] [   ERROR] threads           :19   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/home/christoph/venv/util/threads.py", line 17, in function_
    target(*args, **kwargs)
  File "/home/christoph/venv/service.py", line 181, in run
    self.clf.open(self.clf.path)
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 149, in open
    self.device = device.connect(path)
                  ^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/device.py", line 112, in connect
    device = driver.init(tty)
             ^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/pn532.py", line 411, in init
    if not transport.read(timeout=initial_timeout) == Chipset.ACK:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/transport.py", line 154, in read
    raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out
[2023-12-10 18:08:19,918] [   ERROR] service           :178  Connecting to the NFC reader...
NoneType: None
[2023-12-10 18:08:19,919] [    INFO] __init__          :148  searching for reader on path tty:serial0:pn532
[2023-12-10 18:08:20,024] [   ERROR] threads           :19   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/home/christoph/venv/util/threads.py", line 17, in function_
    target(*args, **kwargs)
  File "/home/christoph/venv/service.py", line 181, in run
    self.clf.open(self.clf.path)
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 149, in open
    self.device = device.connect(path)
                  ^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/device.py", line 112, in connect
    device = driver.init(tty)
             ^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/pn532.py", line 411, in init
    if not transport.read(timeout=initial_timeout) == Chipset.ACK:
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/christoph/venv/lib/python3.11/site-packages/nfc/clf/transport.py", line 154, in read
    raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out

RFID Reader Requirements

I'm looking into upgrading my RFID reader to a bigger range to get through a door. I'm looking for the requirements of the reader so it can be compatible with Apple Home Key

Can't share key

Hey first of all, your project works great! Thank you for doing research. I noticed, that I can't share the key with others. Is it, cause its not Apple certifified? Or can I scan the QR-Code on multiple devices or just once?

Sharing key to apple watch

I installed the newest version on a RPi 3 with the pn532. Everything works great on my iPhone 13 pro max.
When I try to share the key to my apple watch series 7 from the watch app "Wallet & Apple pay" I get the following error: "Could Not Add Key - Try again later. If the problem continues, the locks might have to be removed and added again in the Home app."

Am I doing something wrong or is this just apple trying to discourage diy stuff?

Different actions for different states

Hi, I'm struggling a bit to get this to work
I want to make it so if locked it performs one action and if unlocked another.
My current code is below
I would appreciate help if anyone could.

    def on_endpoint_authenticated(self, endpoint):
        self._lock_target_state = 0 if self._lock_current_state else 1
        log.info(
            f"Toggling lock state due to endpoint authentication event {self._lock_target_state} -> {self._lock_current_state} {endpoint}"
        )
        self.lock_target_state.set_value(self._lock_target_state, should_notify=True)
        self._lock_current_state = self._lock_target_state
        self.lock_current_state.set_value(self._lock_current_state, should_notify=True)
        if self._lock_current_state == 0:
            requests.post("http://....")
        if self._lock_current_state == 1:
            requests.post("http://....")

Error While run first time

/Users/sense/Desktop/apple-home-key-reader-main/venv/bin/python /Users/sense/Desktop/apple-home-key-reader-main/main.py
[2024-04-06 23:06:57,166] [ ERROR] repository :42 Could not load Home Key configuration. Assuming that device is not yet configured...
Traceback (most recent call last):
File "/Users/sense/Desktop/apple-home-key-reader-main/repository.py", line 30, in _load_state_from_file
configuration = json.load(open(self.storage_file_path, "r+"))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'homekey.json'
[2024-04-06 23:06:57,172] [ INFO] accessory_driver :483 Loading Accessory state from hap.state
[2024-04-06 23:06:57,173] [ ERROR] service :175 Connecting to the NFC reader...
NoneType: None

I got this At first Run

Last login: Sat Apr 6 22:42:17 on ttys001
sense@senseuui-Macmini ~ % ls /dev/tty.*

/dev/tty.Bluetooth-Incoming-Port /dev/tty.usbserial-0001
/dev/tty.SLAB_USBtoUART
sense@senseuui-Macmini ~ % Íß

anyyone can help me ?

Trouble Setting Up

I'm struggling to set it up :/

I am using a raspberry pi 4 with the PN532 NFC module with GPIO:
GND -> 6
VCC -> 4
SDA -> 3
SCL -> 5

I have followed your steps, but when I run python3 main.py I get back:

(venv) tom@HomeAssistant:~/apple-home-key-reader $ python3 main.py
[2023-11-26 22:54:20,449] [    INFO] __init__          :148  searching for reader on path tty:/dev/i2c-1:pn532
[2023-11-26 22:54:20,449] [   ERROR] __init__          :153  no reader available on path tty:/dev/i2c-1:pn532
Traceback (most recent call last):
  File "/home/tom/apple-home-key-reader/main.py", line 81, in <module>
    main()
  File "/home/tom/apple-home-key-reader/main.py", line 62, in main
    nfc_device = configure_nfc_device(config["nfc"])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tom/apple-home-key-reader/main.py", line 41, in configure_nfc_device
    clf = BroadcastFrameContactlessFrontend(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tom/apple-home-key-reader/util/bfclf.py", line 51, in __init__
    super().__init__(path)
  File "/home/tom/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 76, in __init__
    raise IOError(errno.ENODEV, os.strerror(errno.ENODEV))
OSError: [Errno 19] No such device`

Then I tried sudo python3 main.py I get back

(venv) tom@HomeAssistant:~/apple-home-key-reader $ sudo python3 main.py
Traceback (most recent call last):
  File "/home/tom/apple-home-key-reader/main.py", line 6, in <module>
    from pyhap.accessory_driver import AccessoryDriver
ModuleNotFoundError: No module named 'pyhap

When I run nfc-scan-device -v I get

nfc-scan-device uses libnfc 1.8.0
1 NFC device(s) found:
- PN532 over I2C:
    pn532_i2c:/dev/i2c-1
chip: PN532 v1.6
initator mode modulations: ISO/IEC 14443A (424 kbps, 212 kbps, 106 kbps), FeliCa (424 kbps, 212 kbps), ISO/IEC 14443-4B (106 kbps), ISO/IEC 14443-4B' (106 kbps), ISO/IEC 14443-2B ST SRx (106 kbps), ISO/IEC 14443-2B ASK CTx (106 kbps), ISO/IEC 14443-2B-3B iClass (Picopass) (106 kbps), Innovision Jewel (106 kbps), Thinfilm NFC Barcode (106 kbps), D.E.P. (424 kbps, 212 kbps, 106 kbps)
target mode modulations: ISO/IEC 14443A (424 kbps, 212 kbps, 106 kbps), FeliCa (424 kbps, 212 kbps), D.E.P. (424 kbps, 212 kbps, 106 kbps)

My config file is

{
    "logging": {
        "level": 20
    },

    "nfc": {
        "port": "/dev/i2c-1",
        "driver": "pn532",
        "broadcast": true
    },

    "hap": {
        "port": 51926,
        "persist": "hap.state"
    },

    "homekey": {
        "persist": "homekey.json",
        "finish": "silver",
        "flow": "fast"
    }
}

I wondering is it me doing something wrong (More than likely me I'm doing this late at night - bad idea)

I'm thinking its the pn532 as when I run /dev/i2c-1 i get back

-bash: /dev/i2c-1: Permission denied

Receiving protocol error sometimes when using the HomeKey

Hey guys,

Most of the times everything works fine, however approximately every 1 of 5 attempts throw the following error. I am using standard option in my configuration.json for flow.

[2024-05-04 02:11:04,043] [   ERROR] threads           :20   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/home/rakolcza/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 667, in send_cmd_recv_rsp
    return self.chipset.in_communicate_thru(data, timeout+0.1)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rakolcza/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 421, in in_communicate_thru
    self.chipset_error(data)
  File "/home/rakolcza/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 124, in chipset_error
    raise Chipset.Error(errno, strerr)
nfc.clf.pn53x.Chipset.Error: Error 0x0B: Protocol error during RF communication

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rakolcza/apple-home-key-reader/util/threads.py", line 18, in function_
    target(*args, **kwargs)
  File "/home/rakolcza/apple-home-key-reader/service.py", line 185, in run
    self._read_homekey()
  File "/home/rakolcza/apple-home-key-reader/service.py", line 138, in _read_homekey
    result_flow, new_issuers_state, endpoint = read_homekey(
                                               ^^^^^^^^^^^^^
  File "/home/rakolcza/apple-home-key-reader/homekey.py", line 737, in read_homekey
    result_flow, issuer, endpoint = perform_authentication_flow(
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rakolcza/apple-home-key-reader/homekey.py", line 598, in perform_authentication_flow
    k_persistent, endpoint, secure = standard_auth(
                                     ^^^^^^^^^^^^^^
  File "/home/rakolcza/apple-home-key-reader/homekey.py", line 305, in standard_auth
    response = tag.transceive(command)
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rakolcza/apple-home-key-reader/util/iso7816.py", line 199, in transceive
    self._implementation.transceive(
  File "/home/rakolcza/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/tag/tt4.py", line 437, in transceive
    data = self._dep.exchange(data, timeout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rakolcza/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/tag/tt4.py", line 123, in exchange
    data = self.clf.exchange(data, (data[1] & 0x3F) * self.fwt)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rakolcza/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/__init__.py", line 1055, in exchange
    rcvd_data = exchange(self.target, send_data, timeout)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/rakolcza/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 673, in send_cmd_recv_rsp
    raise nfc.clf.TransmissionError(str(error))
nfc.clf.TransmissionError: Error 0x0B: Protocol error during RF communication

lock status info after restart

Hi first of all congratulations for the project, it works great, my question is when the program is interrupted obviously on HomeKit the lock goes to no response, and this is normal, the problem arises when the program is restarted because the lock status goes to unlocked , in this way having done an automation with my real lock (Nuki) the automation opens the door for me and this is a problem, can the NFC lock start on closed when the program starts? Thank you for the time you dedicate to the project

Integration with HAP-python in Bridge Mode?

Hi there and thanks for sharing this awesome guide.

Though I haven't tried the scipt yet (waiting for my ACR122U to arrive tomorrow), I was wondering about integrating the script into my existing project.

I use HAP-python bridge configuration to expose accessories to homekit as so Driver -> Bridge -> Accessory on my RPi in a django server to quickly generate homekit accessories to control GPIO, and I was wondering if this repo would work in bridge mode as opposed to a single accessory that the sample accessory.py shows.

Appreciate if anyone could provide some insight if tried.

Thanks again for the great work!

NFC driver not freeing /dev/serial0 on authentication failure?

Hi,
first of all: FINALLY. Idk when homekeys were announced but it is very great to see an implementation like this.

I've deployed the project on a Raspberry Pi and it is running fine. However, in some cases the loop service.py/_process_nfc() throws a TimeoutError:

View Log

[2023-12-05 10:48:54,116] [ ERROR] service :156 [Errno 110] Connection timed out
Traceback (most recent call last):
File "/home/pi/apple-home-key-reader/service.py", line 154, in run
self._process_nfc()
File "/home/pi/apple-home-key-reader/service.py", line 95, in _process_nfc
remote_target = self.clf.sense(
File "/home/pi/apple-home-key-reader/util/bfclf.py", line 162, in sense
sense_broadcast(target, options.get("broadcast", None))
File "/home/pi/apple-home-key-reader/util/bfclf.py", line 124, in sense_broadcast
_ = self.device.chipset.in_communicate_thru(broadcast, timeout=0.1)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 416, in in_communicate_thru
data = self.command(0x42, data, timeout)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 202, in command
raise error
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 197, in command
frame = self.read_frame(int(1000 * timeout))
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 249, in read_frame
return self.transport.read(timeout)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/transport.py", line 154, in read
raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out

I've observed this behaviour in three scenarios now:

  1. Running application, then presenting a static NFC tag resulting in an unrecoverable timeout error. After the delay, the terminal gets spammed with the TimeoutError as seen above.
View Log

[2023-12-05 10:57:54,211] [ INFO] service :147 Waiting for next device...
[2023-12-05 10:57:54,242] [ INFO] service :110 Got NFC tag Type2Tag ID=A3031BF7
[2023-12-05 10:57:54,243] [ INFO] homekey :529 SELECT CMD = ISO7816Command(cla=0x00; ins=0xa4; p1=0x04; p2=0x00; lc=0x07(7); data=a0000008580101; le=0x00)
[2023-12-05 10:57:54,581] [ ERROR] service :142 unrecoverable timeout error
Traceback (most recent call last):
File "/home/pi/apple-home-key-reader/service.py", line 112, in _process_nfc
result_flow, new_issuers_state, endpoint = read_homekey(
File "/home/pi/apple-home-key-reader/homekey.py", line 706, in read_homekey
response = select_applet(tag, applet=ISO7816Application.HOME_KEY)
File "/home/pi/apple-home-key-reader/homekey.py", line 530, in select_applet
response = tag.transceive(command)
File "/home/pi/apple-home-key-reader/util/iso7816.py", line 199, in transceive
self._implementation.transceive(
File "/home/pi/.local/lib/python3.9/site-packages/nfc/tag/tt2.py", line 600, in transceive
raise Type2TagCommandError(nfc.tag.TIMEOUT_ERROR)
nfc.tag.tt2.Type2TagCommandError: unrecoverable timeout error
[2023-12-05 10:57:54,586] [ WARNING] service :143 Encountered an exception. Waiting for 5 seconds before continuing...
[2023-12-05 10:57:59,592] [ INFO] service :147 Waiting for next device...
[2023-12-05 10:57:59,723] [ ERROR] service :156 [Errno 110] Connection timed out
Traceback (most recent call last):
File "/home/pi/apple-home-key-reader/service.py", line 154, in run
self._process_nfc()
File "/home/pi/apple-home-key-reader/service.py", line 95, in _process_nfc
remote_target = self.clf.sense(
File "/home/pi/apple-home-key-reader/util/bfclf.py", line 162, in sense
sense_broadcast(target, options.get("broadcast", None))
File "/home/pi/apple-home-key-reader/util/bfclf.py", line 124, in sense_broadcast
_ = self.device.chipset.in_communicate_thru(broadcast, timeout=0.1)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 416, in in_communicate_thru
data = self.command(0x42, data, timeout)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 202, in command
raise error
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 197, in command
frame = self.read_frame(int(1000 * timeout))
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 249, in read_frame
return self.transport.read(timeout)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/transport.py", line 154, in read
raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out
[...and so on...]

  1. Running application (no matter how long or how many authentication attempts of any type), then killing it (ctrl + c). Trying to restart the application (even after several minutes) won't work due to (again) the same TimeoutError killing the app right at the beginning. The last log message looks like this:

[2023-12-05 10:58:03,746] [ ERROR] base_events :1738 Task was destroyed but it is pending!
task: <Task pending name='Task-11' coro=<Zeroconf._async_broadcast_service() done, defined at /home/pi/.local/lib/python3.9/site-packages/zeroconf/_core.py:367> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f8cb87dc0>()]>>

But I don't think thats the task keeping the reader busy as zeroconf is some mDNS stuff I guess.

View Full Log

[...]
^C[2023-12-05 10:58:03,558] [ INFO] main :71 SIGNAL 15
[2023-12-05 10:58:03,680] [ INFO] accessory_driver :432 Stopping accessory NFC Lock on address ['xxx.xxx.xxx.xxx'], port 51926.
[2023-12-05 10:58:03,683] [ INFO] accessory_driver :342 Closed the event loop
[2023-12-05 10:58:03,746] [ ERROR] base_events :1738 Task was destroyed but it is pending!
task: <Task pending name='Task-11' coro=<Zeroconf._async_broadcast_service() done, defined at /home/pi/.local/lib/python3.9/site-packages/zeroconf/_core.py:367> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f8cb87dc0>()]>>
pi@nfctest:~/apple-home-key-reader $ python3 main.py
[2023-12-05 11:07:03,300] [ INFO] init :148 searching for reader on path tty:serial0:pn532
Traceback (most recent call last):
File "/home/pi/apple-home-key-reader/main.py", line 82, in
main()
File "/home/pi/apple-home-key-reader/main.py", line 63, in main
nfc_device = configure_nfc_device(config["nfc"])
File "/home/pi/apple-home-key-reader/main.py", line 41, in configure_nfc_device
clf = BroadcastFrameContactlessFrontend(
File "/home/pi/apple-home-key-reader/util/bfclf.py", line 51, in init
super().init(path)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/init.py", line 75, in init
if path and not self.open(path):
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/init.py", line 149, in open
self.device = device.connect(path)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/device.py", line 112, in connect
device = driver.init(tty)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn532.py", line 411, in init
if not transport.read(timeout=initial_timeout) == Chipset.ACK:
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/transport.py", line 154, in read
raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out
pi@nfctest:~/apple-home-key-reader $

  1. When unplugging the reader while running. It will result in another spam of the terminal. Sadly, it is not recovering from that when you reconnect the reader again :(
View Log

[2023-12-05 11:15:55,207] [ ERROR] pn53x :180 input/output error while waiting for ack
[2023-12-05 11:15:55,208] [ ERROR] service :156 [Errno 5] Input/output error
Traceback (most recent call last):
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 178, in command
frame = self.read_frame(timeout=100)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 249, in read_frame
return self.transport.read(timeout)
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/transport.py", line 154, in read
raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 110] Connection timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/pi/apple-home-key-reader/service.py", line 154, in run
self._process_nfc()
File "/home/pi/apple-home-key-reader/service.py", line 95, in _process_nfc
remote_target = self.clf.sense(
File "/home/pi/apple-home-key-reader/util/bfclf.py", line 142, in sense
self.device.mute() # deactivate the rf field
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 499, in mute
self.chipset.rf_configuration(0x01, bytearray([0b00000010]))
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 348, in rf_configuration
self.command(0x32, bytearray([cfg_item]) + bytearray(cfg_data),
File "/home/pi/.local/lib/python3.9/site-packages/nfc/clf/pn53x.py", line 181, in command
raise IOError(errno.EIO, os.strerror(errno.EIO))
OSError: [Errno 5] Input/output error
[... repeating over and over...]

So in conclusion, it seems that the connection to the reader via UART is never correctly closed. There is no other pyhon process running in the background after killing the code but interpreting those error messages it appears to me that the application cannot access the reader. Because to fix ANY of the above named scenarios: Unplug and replug the reader, then restart

I've digged a little bit into the code but wasn't able to find a quick fix for the two problems (timeout and i/o errors)

Maybe python 3.9.2 is not compatible?

discussion: esp32 firmware?

anyone reckon if this implementation is lightweight enough to be turned into custom firmware for esp32 devices?

Only Read Kreditcard

For Some Reason I can't explain every time I try to you the express Mode by Holding my Phone and Front of the NFC Reader try to use my Kreditcard. I don't know if it do that cause of it not fully Authentic auf der HomeKit System.

IMG_2931.mov

Unhandled exception in runner homekey: Crash after each successful read.

I'm getting this error each time after a successful read. Only tested in Python3.11 and wanted to share here.

[2024-01-05 19:56:11,464] [ INFO] service :156 Transaction took 255.7717920280993 ms
[2024-01-05 19:56:11,464] [ INFO] accessory :29 Toggling lock state due to endpoint authentication event 0 -> 1 Endpoint(last_used_at=1704437771, counter=11, ####)
[2024-01-05 19:56:11,484] [ INFO] service :165 Waiting for device to leave the field...
[2024-01-05 19:56:12,011] [ INFO] service :165 Waiting for device to leave the field...
[2024-01-05 19:56:12,540] [ INFO] service :167 Device left the field. Continuing in 2 seconds...
[2024-01-05 19:56:14,547] [ INFO] service :169 Waiting for next device...
[2024-01-05 19:56:14,695] [ ERROR] threads :20 Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
File "./apple-home-key-reader-main/util/threads.py", line 18, in function_
target(*args, **kwargs)
File "./apple-home-key-reader-main/service.py", line 185, in run
self._read_homekey()
File "./apple-home-key-reader-main/service.py", line 111, in _read_homekey
remote_target = self.clf.sense(
^^^^^^^^^^^^^^^
File "./apple-home-key-reader-main/util/bfclf.py", line 199, in sense
sense_broadcast(target, options.get("broadcast", None))
File "./apple-home-key-reader-main/util/bfclf.py", line 161, in sense_broadcast
_ = self.device.chipset.in_communicate_thru(broadcast, timeout=0.1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "./apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 416, in in_communicate_thru
data = self.command(0x42, data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "./apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 202, in command
raise error
File "./apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 197, in command
frame = self.read_frame(int(1000 * timeout))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "./apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 249, in read_frame
return self.transport.read(timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "./apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/transport.py", line 154, in read
raise IOError(errno.ETIMEDOUT, os.strerror(errno.ETIMEDOUT))
TimeoutError: [Errno 60] Operation timed out
[2024-01-05 19:56:19,702] [ ERROR] service :175 Connecting to the NFC reader...
NoneType: None
[2024-01-05 19:56:19,813] [ INFO] init :148 searching for reader on path tty:usbserial-0001:pn532
[2024-01-05 19:56:20,425] [ INFO] init :151 using PN532v1.6 at /dev/cu.usbserial-0001

Nothing happens when putting my phone next to the NFC reader

Hi,
First of all - kudos for a great project!
I managed to install and run the project on my raspberry zero, and add the "lock" device to my homekit. I can see the home key in my wallet, but nothing happens when I put my phone next to the NFC reader.
I'm using "HiLetgo PN532 NFC NXP RFID Module V3 Kit" ( https://www.amazon.com/dp/B01I1J17LC ).

Would appriciate your help in debugging this issue and attaching the logs from the first run (in which I added the accessory using my phone). At the end, the program doesn't print any output even if I place my phone next to the NFC reader. I've also attached the logs of the second run (which behaves the same - but doesn't go through all the NFC handshake as the key was already set up):

adi@raspberrypi:~/apple-home-key-reader $ python main.py
[ 20:48:30,684] [ ERROR] repository :42 Could not load Home Key configuration. Assuming that device is not yet configured...
Traceback (most recent call last):
File "/", line 30, in _load_state_from_file
configuration = json.load(open(self.storage_file_path, "r+"))
FileNotFoundError: [Errno 2] No such file or directory: 'homekey.json'
[ 20:48:30,715] [ INFO] accessory_driver :483 Loading Accessory state from hap.state
[ 20:48:30,719] [ INFO] accessory_driver :320 Starting the event loop
[ 20:48:30,719] [ ERROR] service :175 Connecting to the NFC reader...
NoneType: None
[ 20:48:30,722] [ INFO] init :148 searching for reader on path tty:AMA0:pn532
[ 20:48:30,725] [ INFO] accessory_driver :372 Starting accessory NFC Lock on addresses ['192.168.1.106'], port 51926.
To use the QR Code feature, use 'pip install HAP-python[QRCode]'
Enter this code in your HomeKit app on your iOS device:
[ 20:48:56,842] [ INFO] hap_protocol :82 ('192.168.1.35', 52665): Connection made to NFC Lock
[ 20:49:19,030] [ INFO] accessory_driver :694 Paired with b'' with permissions b'\x01'.
[ 20:49:19,097] [ INFO] hap_protocol :82 ('192.168.1.35', 52668): Connection made to NFC Lock
[ 20:49:19,175] [ INFO] service :105 Adding issuer Issuer(public_key=, endpoints=[]) based on paired clients
[ 20:49:19,178] [ INFO] accessory :138 get_hardware_finish
[ 20:49:19,178] [ INFO] service :281 get_hardware_finish=
[ 20:49:19,179] [ INFO] accessory :115 get_lock_current_state
[ 20:49:19,179] [ INFO] accessory :119 get_lock_target_state
[ 20:49:19,180] [ INFO] accessory :129 get_lock_version
[ 20:49:19,180] [ INFO] accessory :158 get_configuration_state
[ 20:49:19,180] [ INFO] service :336 get_configuration_state
[ 20:49:19,181] [ INFO] accessory :148 get_nfc_access_control_point
[ 20:49:19,181] [ INFO] service :294 get_nfc_access_control_point
[ 20:49:19,181] [ INFO] accessory :143 get_nfc_access_supported_configuration
[ 20:49:19,182] [ INFO] service :290 TODO get_nfc_access_supported_configuration=
[ 20:49:19,519] [ INFO] hap_protocol :82 ('192.168.1.144', 63762): Connection made to NFC Lock
[ 20:49:19,584] [ ERROR] hap_handler :581 NFC Lock: Client ('192.168.1.144', 63762) with uuid attempted pair verify without being paired first (public_key=, paired clients={UUID(''): ''}).
[ 20:49:19,684] [ INFO] hap_protocol :82 ('192.168.1.144', 63763): Connection made to NFC Lock
[ 20:49:20,036] [ INFO] accessory :138 get_hardware_finish
[ 20:49:20,036] [ INFO] service :281 get_hardware_finish=
[ 20:49:20,037] [ INFO] accessory :115 get_lock_current_state
[ 20:49:20,038] [ INFO] accessory :119 get_lock_target_state
[ 20:49:20,038] [ INFO] accessory :129 get_lock_version
[ 20:49:20,039] [ INFO] accessory :158 get_configuration_state
[ 20:49:20,039] [ INFO] service :336 get_configuration_state
[ 20:49:20,040] [ INFO] accessory :148 get_nfc_access_control_point
[ 20:49:20,040] [ INFO] service :294 get_nfc_access_control_point
[ 20:49:20,041] [ INFO] accessory :143 get_nfc_access_supported_configuration
[ 20:49:20,041] [ INFO] service :290 TODO get_nfc_access_supported_configuration=
[ 20:49:20,125] [ INFO] accessory :153 set_nfc_access_control_point =
[ 20:49:20,125] [ INFO] service :298 <-- (B64) =
[ 20:49:20,126] [ INFO] service :300 <-- (TLV)
[ 20:49:20,128] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=GET(0x01), reader_key_request=ReaderKeyRequest())
[ 20:49:20,128] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(key_identifier=))
[ 20:49:20,129] [ INFO] service :330 --> (TLV)
[ 20:49:20,129] [ INFO] service :332 --> (B64)
[ 20:49:20,145] [ INFO] accessory :153 set_nfc_access_control_point =
[ 20:49:20,145] [ INFO] service :298 <-- (B64) =
[ 20:49:20,146] [ INFO] service :300 <-- (TLV)
[ 20:49:20,147] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=GET(0x01), reader_key_request=ReaderKeyRequest())
[ 20:49:20,147] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(key_identifier=))
[ 20:49:20,148] [ INFO] service :330 --> (TLV)
[ 20:49:20,148] [ INFO] service :332 --> (B64)
[ 20:49:20,188] [ INFO] accessory :138 get_hardware_finish
[ 20:49:20,189] [ INFO] service :281 get_hardware_finish=
[ 20:49:20,208] [ INFO] accessory :153 set_nfc_access_control_point =
[ 20:49:20,208] [ INFO] service :298 <-- (B64) =
[ 20:49:20,209] [ INFO] service :300 <-- (TLV)
[ 20:49:20,210] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=GET(0x01), reader_key_request=ReaderKeyRequest())
[ 20:49:20,211] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(key_identifier=))
[ 20:49:20,211] [ INFO] service :330 --> (TLV)
[ 20:49:20,212] [ INFO] service :332 --> (B64)
[ 20:49:20,234] [ INFO] accessory :153 set_nfc_access_control_point =
[ 20:49:20,235] [ INFO] service :298 <-- (B64) =
[ 20:49:20,235] [ INFO] service :300 <-- (TLV)
[ 20:49:20,236] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=GET(0x01), reader_key_request=ReaderKeyRequest())
[ 20:49:20,237] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(key_identifier=))
[ 20:49:20,237] [ INFO] service :330 --> (TLV)
[ 20:49:20,238] [ INFO] service :332 --> (B64)
[ 20:49:20,247] [ INFO] accessory :153 set_nfc_access_control_point
[ 20:49:20,248] [ INFO] service :298 <-- (B64)
[ 20:49:20,248] [ INFO] service :300 <-- (TLV)
[ 20:49:20,250] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=REMOVE(0x03), reader_key_request=ReaderKeyRequest(key_identifier=))
[ 20:49:20,253] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(status=SUCCESS(0x00)))
[ 20:49:20,254] [ INFO] service :330 --> (TLV)
[ 20:49:20,254] [ INFO] service :332 --> (B64) =
[ 20:49:20,340] [ INFO] accessory :153 set_nfc_access_control_point
[ 20:49:20,340] [ INFO] service :298 <-- (B64)
[ 20:49:20,341] [ INFO] service :300 <-- (TLV)
[ 20:49:20,342] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=REMOVE(0x03), reader_key_request=ReaderKeyRequest(key_identifier=))
[ 20:49:20,345] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(status=SUCCESS(0x00)))
[ 20:49:20,346] [ INFO] service :330 --> (TLV)
[ 20:49:20,347] [ INFO] service :332 --> (B64) =
[ 20:49:20,369] [ INFO] accessory :153 set_nfc_access_control_point
[ 20:49:20,370] [ INFO] service :298 <-- (B64)
[ 20:49:20,370] [ INFO] service :300 <-- (TLV)
[ 20:49:20,372] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=REMOVE(0x03), reader_key_request=ReaderKeyRequest(key_identifier=))
[ 20:49:20,375] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(status=SUCCESS(0x00)))
[ 20:49:20,375] [ INFO] service :330 --> (TLV)
[ 20:49:20,376] [ INFO] service :332 --> (B64) =
[ 20:49:20,403] [ INFO] accessory :153 set_nfc_access_control_point ==
[ 20:49:20,403] [ INFO] service :298 <-- (B64) ==
[ 20:49:20,404] [ INFO] service :300 <-- (TLV)
[ 20:49:20,406] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=ADD(0x02), reader_key_request=ReaderKeyRequest(key_type=SECP256R1(0x02), reader_private_key=, unique_reader_identifier=))
[ 20:49:20,412] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(status=SUCCESS(0x00)))
[ 20:49:20,412] [ INFO] service :330 --> (TLV)
[ 20:49:20,413] [ INFO] service :332 --> (B64) =
[ 20:49:20,444] [ INFO] accessory :153 set_nfc_access_control_point ==
[ 20:49:20,444] [ INFO] service :298 <-- (B64) ==
[ 20:49:20,445] [ INFO] service :300 <-- (TLV)
[ 20:49:20,446] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=ADD(0x02), reader_key_request=ReaderKeyRequest(key_type=SECP256R1(0x02), reader_private_key=, unique_reader_identifier=))
[ 20:49:20,447] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(status=DUPLICATE(0x02)))
[ 20:49:20,448] [ INFO] service :330 --> (TLV)
[ 20:49:20,448] [ INFO] service :332 --> (B64) =
[ 20:49:20,476] [ INFO] accessory :153 set_nfc_access_control_point ==
[ 20:49:20,477] [ INFO] service :298 <-- (B64) ==
[ 20:49:20,477] [ INFO] service :300 <-- (TLV)
[ 20:49:20,478] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=ADD(0x02), reader_key_request=ReaderKeyRequest(key_type=SECP256R1(0x02), reader_private_key=, unique_reader_identifier=))
[ 20:49:20,479] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(status=DUPLICATE(0x02)))
[ 20:49:20,480] [ INFO] service :330 --> (TLV)
[ 20:49:20,480] [ INFO] service :332 --> (B64) =
[ 20:49:20,520] [ INFO] accessory :153 set_nfc_access_control_point
[ 20:49:20,520] [ INFO] service :298 <-- (B64)
[ 20:49:20,521] [ INFO] service :300 <-- (TLV)
[ 20:49:20,522] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=REMOVE(0x03), reader_key_request=ReaderKeyRequest(key_identifier=))
[ 20:49:20,523] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(status=DOES_NOT_EXIST(0x03)))
[ 20:49:20,523] [ INFO] service :330 --> (TLV)
[ 20:49:20,524] [ INFO] service :332 --> (B64) =
[ 20:49:20,536] [ INFO] accessory :153 set_nfc_access_control_point =
[ 20:49:20,536] [ INFO] service :298 <-- (B64) =
[ 20:49:20,537] [ INFO] service :300 <-- (TLV)
[ 20:49:20,538] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=GET(0x01), reader_key_request=ReaderKeyRequest())
[ 20:49:20,539] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(key_identifier=))
[ 20:49:20,539] [ INFO] service :330 --> (TLV)
[ 20:49:20,540] [ INFO] service :332 --> (B64) +
[ 20:49:20,710] [ INFO] accessory :153 set_nfc_access_control_point =
[ 20:49:20,710] [ INFO] service :298 <-- (B64) =
[ 20:49:20,711] [ INFO] service :300 <-- (TLV)
[ 20:49:20,712] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=GET(0x01), reader_key_request=ReaderKeyRequest())
[ 20:49:20,713] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(key_identifier=))
[ 20:49:20,714] [ INFO] service :330 --> (TLV)
[ 20:49:20,714] [ INFO] service :332 --> (B64) +
[ 20:49:23,358] [ INFO] accessory_driver :694 Paired with b'' with permissions b'\x01'.
[ 20:49:24,948] [ INFO] accessory :153 set_nfc_access_control_point =
[ 20:49:24,949] [ INFO] service :298 <-- (B64) =
[ 20:49:24,950] [ INFO] service :300 <-- (TLV)
[ 20:49:24,951] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=GET(0x01), reader_key_request=ReaderKeyRequest())
[ 20:49:24,952] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(key_identifier=))
[ 20:49:24,952] [ INFO] service :330 --> (TLV)
[ 20:49:24,953] [ INFO] service :332 --> (B64) +
[ 20:49:24,961] [ INFO] accessory :153 set_nfc_access_control_point =
[ 20:49:24,962] [ INFO] service :298 <-- (B64) =
[ 20:49:24,962] [ INFO] service :300 <-- (TLV)
[ 20:49:24,963] [ INFO] service :302 <-- (OBJ) ControlPointRequest(operation=GET(0x01), reader_key_request=ReaderKeyRequest())
[ 20:49:24,964] [ INFO] service :328 --> (OBJ) ControlPointResponse(reader_key_response=ReaderKeyResponse(key_identifier=))
[ 20:49:24,965] [ INFO] service :330 --> (TLV)
[ 20:49:24,965] [ INFO] service :332 --> (B64) +
[ 20:49:25,040] [ INFO] accessory_driver :694 Paired with b'' with permissions b'\x01'.
[ 20:49:26,745] [ INFO] accessory_driver :694 Paired with b'' with permissions b'\x01'.
[

Multiple Devices

Hi,
Is it possible to pair multiple devices so that I do not have to buy a HomeKit hub to share the homekey?
Thanks

Connection via UDP

Hello! I am trying to connect the python app to a Serial server (with PN532 connected to server device) hosted with ESPHome (https://github.com/oxan/esphome-stream-server). I have used this approach with Paradox Alarm Panels and it works very well. When I try to configure the apple-home-key-reader to use it, I get "Connection made to NFC Lock", but it does not seem to work with nfc at all. If I connect the same PN532 chip with serial adapter via usb, it works.

Config used (develop branch):

{
    "logging": {
        "level": 20
    },

    "nfc": {
        "path": "udp:10.32.1.116:8887",
        "broadcast": false
    },

    "hap": {
        "port": 51926,
        "persist": "hap.state",
        "default": "locked"
    },

    "homekey": {
        "persist": "homekey.json",
        "express": true,
        "finish": "gold",
        "flow": "fast"
    }
}

Maybe there are additional implementations that need to be made for this to work?

Logs:

[2024-05-30 20:34:46,347] [    INFO] __init__          :148  searching for reader on path udp:10.32.1.116:8887
[2024-05-30 20:34:46,347] [    INFO] accessory_driver  :320  Starting the event loop
[2024-05-30 20:34:46,347] [    INFO] accessory_driver  :372  Starting accessory NFC Lock on addresses ['192.168.1.169'], port 51926.
[2024-05-30 20:34:46,355] [    INFO] __init__          :151  using Darwin IP-Stack UDP at udp:10.32.1.116:8887
[2024-05-30 20:34:46,396] [    INFO] hap_protocol      :82   ('10.21.1.129', 51539): Connection made to NFC Lock
[2024-05-30 20:34:46,405] [    INFO] accessory         :138  get_hardware_finish
[2024-05-30 20:34:46,405] [    INFO] service           :281  get_hardware_finish=AQSq1uwA
[2024-05-30 20:34:46,405] [    INFO] accessory         :115  get_lock_current_state
[2024-05-30 20:34:46,405] [    INFO] accessory         :119  get_lock_target_state
[2024-05-30 20:34:46,406] [    INFO] accessory         :129  get_lock_version
[2024-05-30 20:34:46,406] [    INFO] accessory         :158  get_configuration_state
[2024-05-30 20:34:46,406] [    INFO] service           :336  get_configuration_state
[2024-05-30 20:34:46,406] [    INFO] accessory         :148  get_nfc_access_control_point
[2024-05-30 20:34:46,406] [    INFO] service           :294  get_nfc_access_control_point
[2024-05-30 20:34:46,406] [    INFO] accessory         :143  get_nfc_access_supported_configuration
[2024-05-30 20:34:46,406] [    INFO] service           :290  TODO get_nfc_access_supported_configuration=AQEQAgEQ
[2024-05-30 20:37:23,070] [   ERROR] threads           :17   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/Users/maslyankov/Developer/apple-home-key-reader/util/threads.py", line 15, in function_
    target(*args, **kwargs)
  File "/Users/maslyankov/Developer/apple-home-key-reader/service.py", line 185, in run
    self._read_homekey()
  File "/Users/maslyankov/Developer/apple-home-key-reader/service.py", line 111, in _read_homekey
    remote_target = self.clf.sense(
                    ^^^^^^^^^^^^^^^
  File "/Users/maslyankov/Developer/apple-home-key-reader/util/bfclf.py", line 386, in sense
    self.target = sense_tta(target)
                  ^^^^^^^^^^^^^^^^^
  File "/Users/maslyankov/Developer/apple-home-key-reader/util/bfclf.py", line 290, in sense_tta
    target = self.device.sense_tta(target)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maslyankov/Developer/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/udp.py", line 94, in sense_tta
    brty, sens_res, addr = self._recv_data(1.0, target.brty)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maslyankov/Developer/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/udp.py", line 555, in _recv_data
    data, addr = self.socket.recvfrom(1024)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
ConnectionRefusedError: [Errno 61] Connection refused
[2024-05-30 20:37:28,082] [   ERROR] service           :175  Connecting to the NFC reader...
NoneType: None
[2024-05-30 20:37:28,082] [    INFO] __init__          :148  searching for reader on path udp:10.32.1.116:8887
[2024-05-30 20:37:28,094] [    INFO] __init__          :151  using Darwin IP-Stack UDP at udp:10.32.1.116:8887
[2024-05-30 20:37:51,208] [   ERROR] threads           :17   Unhandled exception in runner homekey. Continuing in 5 seconds
Traceback (most recent call last):
  File "/Users/maslyankov/Developer/apple-home-key-reader/util/threads.py", line 15, in function_
    target(*args, **kwargs)
  File "/Users/maslyankov/Developer/apple-home-key-reader/service.py", line 185, in run
    self._read_homekey()
  File "/Users/maslyankov/Developer/apple-home-key-reader/service.py", line 111, in _read_homekey
    remote_target = self.clf.sense(
                    ^^^^^^^^^^^^^^^
  File "/Users/maslyankov/Developer/apple-home-key-reader/util/bfclf.py", line 386, in sense
    self.target = sense_tta(target)
                  ^^^^^^^^^^^^^^^^^
  File "/Users/maslyankov/Developer/apple-home-key-reader/util/bfclf.py", line 290, in sense_tta
    target = self.device.sense_tta(target)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maslyankov/Developer/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/udp.py", line 94, in sense_tta
    brty, sens_res, addr = self._recv_data(1.0, target.brty)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/maslyankov/Developer/apple-home-key-reader/venv/lib/python3.11/site-packages/nfc/clf/udp.py", line 555, in _recv_data
    data, addr = self.socket.recvfrom(1024)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
ConnectionRefusedError: [Errno 61] Connection refused
[2024-05-30 20:37:56,216] [   ERROR] service           :175  Connecting to the NFC reader...
NoneType: None
[2024-05-30 20:37:56,217] [    INFO] __init__          :148  searching for reader on path udp:10.32.1.116:8887
[2024-05-30 20:37:56,236] [    INFO] __init__          :151  using Darwin IP-Stack UDP at udp:10.32.1.116:8887

Error homekey iPhone

Hi @kormax,

I installed this project on a raspberry pi 3b+, via uart.

it’s working fine when I use my Apple Watch, but my iPhone and my gf iPhone doesn’t work.

I tried flow standard and fast, same problem for both.

Logs: ( 1st success with watch, 2nd iPhone)

[2023-11-30 17:17:06,025] [ INFO] init :148 searching for reader on path tty:ttyAMA0:pn532
[2023-11-30 17:17:06,144] [ INFO] init :151 using PN532v1.6 at /dev/ttyAMA0
[2023-11-30 17:17:06,157] [ INFO] accessory_driver :483 Loading Accessory state from hap.state
[2023-11-30 17:17:06,161] [ INFO] accessory_driver :320 Starting the event loop
[2023-11-30 17:17:06,165] [ INFO] accessory_driver :372 Starting accessory NFC Lock on addresses ['192.168.5.74'], port 51926.
[2023-11-30 17:17:06,464] [ INFO] hap_protocol :82 ('192.168.4.27', 50714): Connection made to NFC Lock
[2023-11-30 17:17:06,507] [ INFO] accessory :138 get_hardware_finish
[2023-11-30 17:17:06,508] [ INFO] service :254 get_hardware_finish=AQTj4+MA
[2023-11-30 17:17:06,509] [ INFO] accessory :115 get_lock_current_state
[2023-11-30 17:17:06,510] [ INFO] accessory :119 get_lock_target_state
[2023-11-30 17:17:06,511] [ INFO] accessory :129 get_lock_version
[2023-11-30 17:17:06,511] [ INFO] accessory :158 get_configuration_state
[2023-11-30 17:17:06,511] [ INFO] service :309 get_configuration_state
[2023-11-30 17:17:06,512] [ INFO] accessory :148 get_nfc_access_control_point
[2023-11-30 17:17:06,512] [ INFO] service :267 get_nfc_access_control_point
[2023-11-30 17:17:06,512] [ INFO] accessory :143 get_nfc_access_supported_configuration
[2023-11-30 17:17:06,513] [ INFO] service :263 TODO get_nfc_access_supported_configuration=AQEQAgEQ
[2023-11-30 17:17:18,599] [ INFO] service :110 Got NFC tag Type4ATag MIU=253 FWT=0.038664
[2023-11-30 17:17:18,606] [ INFO] homekey :529 SELECT CMD = ISO7816Command(cla=0x00; ins=0xa4; p1=0x04; p2=0x00; lc=0x07(7); data=a0000008580101; le=0x00)
[2023-11-30 17:17:18,626] [ INFO] homekey :535 SELECT RES = ISO7816Response(sw1=0x90; sw2=0x00; data=5c0402000100(6))
[2023-11-30 17:17:18,628] [ INFO] homekey :708 reader_identifier.hex()='700ec626a71584e3ca53a7dfb6eb559c'
[2023-11-30 17:17:18,629] [ INFO] homekey :721 Choosing preferred version b'\x02\x00'
[2023-11-30 17:17:18,635] [ INFO] homekey :569 Reader public key: x=df2f764981a8adc2d9b24acf08207938e18e0fd2dba28cdc3ffab4ec6a208242 y=02fa3a1f8e524be9d2aafd3d9bb372a264abcc0b86459b5c345da72ea140dfc0
[2023-11-30 17:17:18,637] [ INFO] homekey :575 protocol_version.hex()='0200'
[2023-11-30 17:17:18,642] [ INFO] homekey :172 AUTH0 CMD = ISO7816Command(cla=0x80; ins=0x80; p1=0x00; p2=0x01; lc=0x6b(107); data=5c0202008741046b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd05b5d58d5e5760972263684a2e7d1ba341a1e3d2b33f44265c5e3b61bab439b744c103f059b89fe06b32cba2567251a8fe0464d10700ec626a71584e3ca53a7dfb6eb559c)
[2023-11-30 17:17:18,718] [ INFO] homekey :176 AUTH0 RES = ISO7816Response(sw1=0x90; sw2=0x00; data=864104d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f3d59c74894e90b24b88d567642287fae9241337de322091a9211dba6545f9385(67))
[2023-11-30 17:17:18,722] [ INFO] homekey :275 endpoint_ephemeral_public_key_x.hex()='d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f' reader_ephemeral_public_key_x.hex()='6b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd0'
[2023-11-30 17:17:18,724] [ INFO] homekey :287 authentication_hash_input.hex()='4d10700ec626a71584e3ca53a7dfb6eb559c8620d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f87206b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd04c103f059b89fe06b32cba2567251a8fe0469304415d9569'
[2023-11-30 17:17:18,727] [ INFO] homekey :292 signature.hex()='3045022006d5a67eb1aa019548c40a1bc333a154a3307fb516cdd5330f5c25d351370a25022100b0ab8df4fd3b06ed012a88deac84ba7c2044175a32cee7e7fa6e13213e3d2723' (0x47)
[2023-11-30 17:17:18,728] [ INFO] homekey :295 signature_point_form.hex()='06d5a67eb1aa019548c40a1bc333a154a3307fb516cdd5330f5c25d351370a25b0ab8df4fd3b06ed012a88deac84ba7c2044175a32cee7e7fa6e13213e3d2723' (0x40)
[2023-11-30 17:17:18,730] [ INFO] homekey :300 AUTH1 COMMAND ISO7816Command(cla=0x80; ins=0x81; p1=0x00; p2=0x00; lc=0x42(66); data=9e4006d5a67eb1aa019548c40a1bc333a154a3307fb516cdd5330f5c25d351370a25b0ab8df4fd3b06ed012a88deac84ba7c2044175a32cee7e7fa6e13213e3d2723)
[2023-11-30 17:17:18,837] [ INFO] homekey :302 AUTH1 RESPONSE: ISO7816Response(sw1=0x90; sw2=0x00; data=d08136d37d5e087bf45e3af6317606fdfcd2842e504bb9699a2f679cad95133f3825437086393490f49ce75a623810a256ed88fa222f7b3847b5969b38de9b43e72f2d3309344a53a66b81eb136da812aaedbb96dd97732a(88))
[2023-11-30 17:17:18,842] [ INFO] homekey :102 shared_key.hex()='19328297d181b3d4e666ef41506a16397da0d062f11c087c21139b161855af38'
[2023-11-30 17:17:18,844] [ INFO] homekey :109 derived_key.hex()='04a672a819a5215c6cae0a9dd901190bc756aa72e5f287498e4cdcb44c8a531c'
[2023-11-30 17:17:18,846] [ INFO] homekey :124 info.hex()='6b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd0d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f3f059b89fe06b32cba2567251a8fe0465e000150657273697374656e745c0202005c0402000100'
[2023-11-30 17:17:18,848] [ INFO] homekey :317 k_persistent.hex()='dbf920c1bfa87fb1999d9c0dbc9a38d6af45a89443d172cd5829388b70c21476'
[2023-11-30 17:17:18,850] [ INFO] homekey :124 info.hex()='6b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd0d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f3f059b89fe06b32cba2567251a8fe0465e0001566f6c6174696c655c0202005c0402000100'
[2023-11-30 17:17:18,853] [ INFO] homekey :320 hkdf.hex()='bf2592fcefbbf7fc368da0bc10d6c67601a14dd31e1eb40f8148088b08264e4290661305f487d92b066b5151b6f86350'
[2023-11-30 17:17:18,853] [ INFO] homekey :324 kenc.hex()='bf2592fcefbbf7fc368da0bc10d6c676' kmac.hex()='01a14dd31e1eb40f8148088b08264e42' krmac.hex()='90661305f487d92b066b5151b6f86350'
[2023-11-30 17:17:18,857] [ INFO] homekey :334 AUTH1 DECRYPTED RESPONSE: ISO7816Response(sw1=0x90; sw2=0x00; data=4e066c95af82b0739e402ce07313e81257c19819fca821ac34b7c7340fb719afc3e1b3a6ae0142ee591dd739be9975cc1cc216a63f844845a3f251f82a81ff592671ba9575ba35812266(74))
[2023-11-30 17:17:18,859] [ INFO] homekey :346 device_identifier.hex()='6c95af82b073'
[2023-11-30 17:17:18,861] [ INFO] homekey :357 signature.hex()='2ce07313e81257c19819fca821ac34b7c7340fb719afc3e1b3a6ae0142ee591dd739be9975cc1cc216a63f844845a3f251f82a81ff592671ba9575ba35812266'
[2023-11-30 17:17:18,862] [ INFO] homekey :370 verification_hash_input.hex()='4d10700ec626a71584e3ca53a7dfb6eb559c8620d26772e1bb627b55fc5fb2e412222159431f6b3316f9a2ee6c47cb0a04de5f0f87206b263a435845dbd50c49b3c98713c6ddcd21316eb4506958b38caac0f9148bd04c103f059b89fe06b32cba2567251a8fe04693044e887b4c'
[2023-11-30 17:17:18,865] [ INFO] homekey :541 OP_CONTROL_FLOW CMD = ISO7816Command(cla=0x80; ins=0x3c; p1=0x01; p2=0x00)
[2023-11-30 17:17:18,889] [ INFO] homekey :543 OP_CONTROL_FLOW RES = ISO7816Response(sw1=0x90; sw2=0x00)
[2023-11-30 17:17:18,898] [ INFO] service :127 Authenticated endpoint via <DigitalKeyFlow.STANDARD: 1>: Endpoint(last_used_at=1701364638, counter=3, key_type=SECP256R1(0x02), public_key=044ba24aac55330184b95e237de272ef2890c9d2bc5ab1a8720320ec607250f724ba2190b0f9a7fc7f42aea69e13769aec2bdfe6e40a32790ec4e5f7923493c453; persistent_key=dbf920c1bfa87fb1999d9c0dbc9a38d6af45a89443d172cd5829388b70c21476, enrollments=Enrollments(hap, ))
[2023-11-30 17:17:18,899] [ INFO] service :130 Transaction took 343.12944600014816 ms
[2023-11-30 17:17:18,900] [ INFO] accessory :29 Toggling lock state due to endpoint authentication event 1 -> 0 Endpoint(last_used_at=1701364638, counter=3, key_type=SECP256R1(0x02), public_key=044ba24aac55330184b95e237de272ef2890c9d2bc5ab1a8720320ec607250f724ba2190b0f9a7fc7f42aea69e13769aec2bdfe6e40a32790ec4e5f7923493c453; persistent_key=dbf920c1bfa87fb1999d9c0dbc9a38d6af45a89443d172cd5829388b70c21476, enrollments=Enrollments(hap, ))
[2023-11-30 17:17:18,911] [ INFO] service :137 Waiting for device to leave the field...
[2023-11-30 17:17:19,474] [ INFO] service :139 Device left the field. Continuing in 2 seconds...
[2023-11-30 17:17:21,475] [ INFO] service :147 Waiting for next device...
[2023-11-30 17:17:28,700] [ INFO] accessory :119 get_lock_target_state
[2023-11-30 17:17:28,700] [ INFO] accessory :115 get_lock_current_state
[2023-11-30 17:17:31,879] [ INFO] accessory :115 get_lock_current_state
[2023-11-30 17:17:38,773] [ INFO] service :110 Got NFC tag Type4ATag MIU=253 FWT=0.038664
[2023-11-30 17:17:38,776] [ INFO] homekey :529 SELECT CMD = ISO7816Command(cla=0x00; ins=0xa4; p1=0x04; p2=0x00; lc=0x07(7); data=a0000008580101; le=0x00)
[2023-11-30 17:17:38,798] [ INFO] homekey :535 SELECT RES = ISO7816Response(sw1=0x90; sw2=0x00; data=5c0402000100(6))
[2023-11-30 17:17:38,799] [ INFO] homekey :708 reader_identifier.hex()='700ec626a71584e3ca53a7dfb6eb559c'
[2023-11-30 17:17:38,799] [ INFO] homekey :721 Choosing preferred version b'\x02\x00'
[2023-11-30 17:17:38,803] [ INFO] homekey :569 Reader public key: x=df2f764981a8adc2d9b24acf08207938e18e0fd2dba28cdc3ffab4ec6a208242 y=02fa3a1f8e524be9d2aafd3d9bb372a264abcc0b86459b5c345da72ea140dfc0
[2023-11-30 17:17:38,805] [ INFO] homekey :575 protocol_version.hex()='0200'
[2023-11-30 17:17:38,807] [ INFO] homekey :172 AUTH0 CMD = ISO7816Command(cla=0x80; ins=0x80; p1=0x00; p2=0x01; lc=0x6b(107); data=5c020200874104c20cc6b5d72d020d5b2d3d1c022db0fa57c5a2e78f3deb886abb5d8fac126a82c64137a8c6e27dd676ddefc38de9ed0f7bf99392fc49b8aeaa077c189d330e854c10fe3dec88ed35bf69b43cf8712ed619974d10700ec626a71584e3ca53a7dfb6eb559c)
[2023-11-30 17:17:38,884] [ INFO] homekey :176 AUTH0 RES = ISO7816Response(sw1=0x90; sw2=0x00; data=864104ff99f76dde0aa59e3d95ef32ebc124e69baa4fe2f319dc598f642e7df3fd42367695625a8a9125e744648504055155cce6683e15d33fe771b65351933754bf5d(67))
[2023-11-30 17:17:38,887] [ INFO] homekey :275 endpoint_ephemeral_public_key_x.hex()='ff99f76dde0aa59e3d95ef32ebc124e69baa4fe2f319dc598f642e7df3fd4236' reader_ephemeral_public_key_x.hex()='c20cc6b5d72d020d5b2d3d1c022db0fa57c5a2e78f3deb886abb5d8fac126a82'
[2023-11-30 17:17:38,888] [ INFO] homekey :287 authentication_hash_input.hex()='4d10700ec626a71584e3ca53a7dfb6eb559c8620ff99f76dde0aa59e3d95ef32ebc124e69baa4fe2f319dc598f642e7df3fd42368720c20cc6b5d72d020d5b2d3d1c022db0fa57c5a2e78f3deb886abb5d8fac126a824c10fe3dec88ed35bf69b43cf8712ed619979304415d9569'
[2023-11-30 17:17:38,889] [ INFO] homekey :292 signature.hex()='3045022012cd625588cc1619af195d58fb30701c7144e0e023f11f8da4cd874e075adb7a022100fabc06ce0b5e7510343f9e0c9dee3fd8834b232db372ce0f050f4fe7b11806ab' (0x47)
[2023-11-30 17:17:38,890] [ INFO] homekey :295 signature_point_form.hex()='12cd625588cc1619af195d58fb30701c7144e0e023f11f8da4cd874e075adb7afabc06ce0b5e7510343f9e0c9dee3fd8834b232db372ce0f050f4fe7b11806ab' (0x40)
[2023-11-30 17:17:38,891] [ INFO] homekey :300 AUTH1 COMMAND ISO7816Command(cla=0x80; ins=0x81; p1=0x00; p2=0x00; lc=0x42(66); data=9e4012cd625588cc1619af195d58fb30701c7144e0e023f11f8da4cd874e075adb7afabc06ce0b5e7510343f9e0c9dee3fd8834b232db372ce0f050f4fe7b11806ab)
[2023-11-30 17:17:38,941] [ WARNING] tt4 :105 ISO-DEP transmission error (#1)
[2023-11-30 17:17:38,951] [ WARNING] tt4 :105 ISO-DEP transmission error (#2)
[2023-11-30 17:17:38,961] [ WARNING] tt4 :105 ISO-DEP transmission error (#3)
[2023-11-30 17:17:38,971] [ WARNING] tt4 :105 ISO-DEP transmission error (#4)
[2023-11-30 17:17:38,981] [ WARNING] tt4 :105 ISO-DEP transmission error (#5)
[2023-11-30 17:17:38,992] [ ERROR] tt4 :108 ISO-DEP unrecoverable transmission error
[2023-11-30 17:17:38,992] [ ERROR] service :142 unrecoverable transmission error
Traceback (most recent call last):
File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 667, in send_cmd_recv_rsp
return self.chipset.in_communicate_thru(data, timeout+0.1)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 421, in in_communicate_thru
self.chipset_error(data)
File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 124, in chipset_error
raise Chipset.Error(errno, strerr)
nfc.clf.pn53x.Chipset.Error: Error 0x0B: Protocol error during RF communication

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/tag/tt4.py", line 95, in exchange
data = self.clf.exchange(data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/init.py", line 1055, in exchange
rcvd_data = exchange(self.target, send_data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/clf/pn53x.py", line 673, in send_cmd_recv_rsp
raise nfc.clf.TransmissionError(str(error))
nfc.clf.TransmissionError: Error 0x0B: Protocol error during RF communication

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/juaurodri/apple-home-key-reader-main/service.py", line 112, in _process_nfc
result_flow, new_issuers_state, endpoint = read_homekey(
^^^^^^^^^^^^^
File "/home/juaurodri/apple-home-key-reader-main/homekey.py", line 733, in read_homekey
result_flow, issuer, endpoint = perform_authentication_flow(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/juaurodri/apple-home-key-reader-main/homekey.py", line 594, in perform_authentication_flow
k_persistent, endpoint, secure = standard_auth(
^^^^^^^^^^^^^^
File "/home/juaurodri/apple-home-key-reader-main/homekey.py", line 301, in standard_auth
response = tag.transceive(command)
^^^^^^^^^^^^^^^^^^^^^^^
File "/home/juaurodri/apple-home-key-reader-main/util/iso7816.py", line 199, in transceive
self._implementation.transceive(
File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/tag/tt4.py", line 437, in transceive
data = self._dep.exchange(data, timeout)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/juaurodri/apple-home-key-reader-main/venv/lib/python3.11/site-packages/nfc/tag/tt4.py", line 109, in exchange
raise Type4TagCommandError(nfc.tag.RECEIVE_ERROR)
nfc.tag.tt4.Type4TagCommandError: unrecoverable transmission error
[2023-11-30 17:17:39,017] [ WARNING] service :143 Encountered an exception. Waiting for 5 seconds before continuing...
[2023-11-30 17:17:44,018] [ INFO] service :147 Waiting for next device...

Not an issue - but a question

Hi
first of all: GREAT and congrats - i'll love this and it's the first time i have an Homekey generated myself :-)
Currently i'll have an PN532 reader connected to an Arduino to read simple NFC Tags. Homekey is a dream since then.

I'll understand the current implementation is require an python - which means a raspberry or similar. Is there an implementation on an ESP32 or similar planned?
Or is there any functional implantation based in a Pi0? Should work, isn't?

And last but not least: how to notify that a key is successful authenticated? Maybe via MQTT, webhooks, ,..?

Thank you in advance
Kind regards
Thorsten

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.