Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting the private key out of macOS login keychain non-trivial #193

Closed
rkobylinski opened this issue Feb 22, 2024 · 2 comments · Fixed by #259
Closed

Getting the private key out of macOS login keychain non-trivial #193

rkobylinski opened this issue Feb 22, 2024 · 2 comments · Fixed by #259

Comments

@rkobylinski
Copy link

rkobylinski commented Feb 22, 2024

After a full restore from a Time Machine backup the private key generated by tesla-keygen seemed gone. While I was able to recover a copy of the login keychain from an older backup manually, I figured it would make sense to get it out of the macOS keychain. In pem format ideally, so you could use it with tesla-keygen migrate if needed. Well this proved possible but not trivial, the following seems to work for me:

% echo 30310201010420 `security find-generic-password -a vehicleCommandKey.$TESLA_KEY_NAME -w` a00a06082a8648ce3d030107 | xxd -r -p | openssl ec -inform d > private_key.pem

Puh, took me a while to figure that out. Or am I missing an obvious easier way?

[Edit: if there is not, maybe this could be added to the documentation / FAQ]

@andig
Copy link

andig commented Mar 13, 2024

@rkobylinski same issue here. Could you kindly explain the magic constants in your approach?

@rkobylinski
Copy link
Author

@rkobylinski same issue here. Could you kindly explain the magic constants in your approach?

My understanding is that these are requirements of the DER / ASN.1 binary format. I found the following post and the included links useful: https://stackoverflow.com/questions/48101258/how-to-convert-an-ecdsa-key-to-pem-format

Note that Tesla uses secp256r1 curve (prime256v1), so the initial example in the answer won't work. A later addition to the answer explains how to adapt pre_string and the mid_string though. Because we are not including the public key here, you have to change the length in the second byte of the pre_string to the length of the following sequence: decimal 49 or hex 31.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants