mtrudel / hap

A HomeKit Accessory Protocol (HAP) Implementation for Elixir
MIT License
90 stars 9 forks source link

Pairing Accessory is failing #97

Closed arjunbajaj closed 9 months ago

arjunbajaj commented 9 months ago

Hi,

I am trying to use this library, but I am unable to pair with the Apple Home app on the iPhone. I followed the demo code. First, if I use the latest hex.pm package (0.4.8), ThousandIsland refuses to start because HAP.HAPSessionTransport.sockname/1 is undefined or private. I noticed this was fixed in the recent commits, so I used the git repo in mix.exs instead.

Now, when I try to pair, Apple Home does request details (as visible in the console), but the HAP.PairSetup GenServer crashes due to bad IV length.

Here are the logs:

12:42:54.688 [info] POST /pair-setup

12:42:54.698 [info] Sent 200 in 9ms

12:42:54.751 [info] POST /pair-setup

12:42:54.752 [info] Sent 200 in 1ms

12:42:54.758 [info] POST /pair-setup

12:42:54.761 [error] GenServer HAP.PairSetup terminating
** (stop) {:badarg, {~c"aead.c", 111}, ~c"Bad IV length"}
    (crypto 5.2) crypto.erl:1023: :crypto.crypto_one_time_aead(:chacha20_poly1305, <<140, 134, 89, 123, 121, 178, 138, 33, 27, 220, 164, 31, 200, 248, 154, 52, 178, 117, 38, 128, 158, 119, 53, 41, 228, 79, 0, 123, 2, 90, 19, 142>>, "PS-Msg05", <<142, 52, 148, 112, 87, 220, 56, 54, 225, 232, 91, 209, 194, 240, 6, 138, 18, 142, 101, 140, 36, 61, 108, 156, 5, 170, 90, 213, 64, 203, 101, 216, 34, 224, 19, 185, 58, 129, 84, 154, 58, 171, 121, 190, 205, 111, 10, 141, 32, 5, ...>>, "", <<151, 111, 233, 169, 209, 103, 60, 53, 218, 80, 101, 129, 96, 93, 141, 149>>, false)
    (hap 0.4.8) lib/hap/crypto/cha_cha_20.ex:24: HAP.Crypto.ChaCha20.decrypt_and_verify/4
    (hap 0.4.8) lib/hap/pair_setup.ex:97: HAP.PairSetup.handle_call/3
    (stdlib 5.0.2) gen_server.erl:1113: :gen_server.try_handle_call/4
    (stdlib 5.0.2) gen_server.erl:1142: :gen_server.handle_msg/6
    (stdlib 5.0.2) proc_lib.erl:241: :proc_lib.init_p_do_apply/3

I tried experimenting with the :crypto.crypto_one_time_aead function, but every variation I try gives me the same Bad IV Length error, even though the lengths described in the Erlang manual are what I'm using.

I am using a Mac with Elixir installed from brew. I also tried it in a ARM Alpine Linux VM, and it gave the same error.

Erlang/OTP 26 [erts-14.0.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit] [dtrace]

Elixir 1.15.7 (compiled with Erlang/OTP 26)
OpenSSL 3.1.4 24 Oct 2023 (Library: OpenSSL 3.1.4 24 Oct 2023)

I'm guessing I'm doing something wrong, or there's some recent bug?

Also, slightly off-topic, but I cannot find the HomeKit Accessory Protocol Specification PDF anywhere. Did Apple remove it? Their help pages say it's available for hobbyists, but I can't find it anywhere on their website.

mtrudel commented 9 months ago

These are all 'known issues', though some of them are pretty new and not fixed yet:

I'll endeavour to get the OpenSSL error characterized and fixed later this week, and one that's done i'll cut a new 0.4.9 release. Thanks for reminding me!