Teslemetry / python-tesla-fleet-api

Python library for Tesla Fleet API and Teslemetry
https://teslemetry.com/docs
Apache License 2.0
11 stars 3 forks source link

Add support for Command signing #5

Open tadly opened 3 weeks ago

tadly commented 3 weeks ago

Discussions aren't enabled and there's no issue for this yet so here I go...

Home-Assistant just added a bunch of features to the tesla integration which is based on this module. For my m3 highland, command signing seems to be required.

Are there plans on adding support for this?

I don't know how difficult this would be so excuse me if I sound ignorant.

Bre77 commented 3 weeks ago

Yes there are plans to do this, but it is very difficult, and it will take time. I will also need a way to install a private key on vehicles, which is likely going to require a bluetooth implementation, which I have also started for Home Assistant.

tadly commented 2 weeks ago

Did a quick google to get a better understanding on the whole key topic. We would need to install the public-key part (not private) on the vehicle. This medium articel outlines it quite well.

For protocol, the relevant part of the article:

Step 1 :: Generate the private key
openssl ecparam -genkey -name prime256v1 -noout > private.pem

Step 2 :: Generate the public key
openssl ec -in private.pem -pubout > public.pem

Step 3 :: Add the public key with the car
./tesla-control -vin {VIN} -ble add-key-request {path_to_the_public_key_along_with_the_public_key_file_name} {ROLE} {FORM_FACTOR}

VIN — Vehicle Identification Number
ROLE — One of: owner, driver
FORM_FACTOR — One of: nfc_card, ios_device, android_device, cloud_key

A sample command to add a BLE key looks like the below -
./tesla-control -vin 7ABCGDEE123ABC555 -ble add-key-request public.pem owner cloud_key

When the command is issued, a successful request will return the following response.
Sent add-key request to 7ABCGDEE123ABC555. Confirm by tapping NFC card on center console.
Bre77 commented 2 weeks ago

Did a quick google to get a better understanding on the whole key topic. We would need to install the public-key part (not private) on the vehicle. This medium articel outlines it quite well.

Yes, but that's written in Go and the only Python implementation hasn't been updated for ~2 years and no longer works. I've spent most of the day today working on it and while I can get the public key installed that's about as far as I have it working.

I may just give up on the Bluetooth implementation and resort to giving people instructions to use Tesla's tools.

tadly commented 2 weeks ago

Oh I see. You wanted it to be part of this project (makes sense). For a first iteration, just to get command signing to work, relying on Tesla's tool wouldn't be that bad IMO. Key-generation and installation needs to be done only once so... :)

Bre77 commented 2 weeks ago

Well the bluetooth side will live in https://github.com/Teslemetry/python_tesla_bluetooth but it creates the private key that this library could then use.

So really now the biggest blocker is that the only command signing implementation ive seen that works is either in C# or Go, and I need it in Python. It'll be the next thing I try attempt.

tadly commented 2 weeks ago

Mind sharing the go project? Translating the logic to python shouldn't be that hard I think. Maybe I can help :)

Bre77 commented 2 weeks ago

https://github.com/teslamotors/vehicle-command/blob/main/cmd/tesla-http-proxy/main.go

I have started the library side implementation here: https://github.com/Teslemetry/python-tesla-fleet-api/blob/main/tesla_fleet_api/vehiclesigned.py

Bre77 commented 2 weeks ago

I got pretty close to a working implementation on flash_lights tonight. Just the HMAC signature to go, so it's realistic that I could get this in for 2024.11

Bre77 commented 2 weeks ago

IT WORKS https://github.com/Teslemetry/python-tesla-fleet-api/commit/a74041e549f28359a31a8e70b68026ad94cda356

tadly commented 2 weeks ago

I love how you went from

...but it is very difficult, and it will take time

to

IT WORKS

in just 3 days. Thank you so much for this. Gets me really excited to finally automate some stuff with my tesla

Bre77 commented 2 weeks ago

I suffer from shiny object syndrome. I started making progress and felt like It was actually acheivable. I have working:

Just the matter of writting all the protobuf defintions for the other Infotainment commands and then a lot of exception handling.

tadly commented 2 weeks ago

I suffer from shiny object syndrome.

Sounds to me like you got into some kind of flow. I love when that happens :)

Given the commit history, you've been chipping away. Congrats on getting all that figured out and thanks again for doing so. Really appreciate it man :)