Foxboron / ssh-tpm-agent

:computer: :key: ssh-agent for TPMs
MIT License
354 stars 20 forks source link

unable to use sealed key (TPM_RC_AUTH_FAIL (session 1): the authorization HMAC check failed and DA counter incremented") #59

Closed FLX-0x00 closed 3 months ago

FLX-0x00 commented 3 months ago

I have not been able to use my sealed key for a few weeks now. I tried to generate a new key with ssh-tpm-keygen, but it does not work either (different error message - something with hmac, not very sure about it). The whole setup worked flawlessly until 4-5 weeks ago. Is there any help in troubleshooting this problem? I have absolutely no idea where to start.

Things I tried:

After try to use the key the ssh-tpm-agent.service logs level=INFO msg="agent 13: failed getting handle: TPM_RC_INTEGRITY (parameter 1): integrity check failed"

my current working kernel is 6.10.3-arch1-2 (Arch Linux) keytype is ecdsa-sha2-nistp256

Hoping for some input on this =)

Foxboron commented 3 months ago

Could you write out the commands from your terminal?

FLX-0x00 commented 3 months ago

With a complete new key:

ム ssh-add -D
All identities removed.
ム ssh-add -L
The agent has no identities.
ム ssh-tpm-keygen
Generating a sealed public/private ecdsa key pair.
Enter file in which to save the key (/home/xxx/.ssh/id_ecdsa): 
/home/xxx/.ssh/id_ecdsa.tpm already exists.
Overwrite (y/n)? y
/home/xxx/.ssh/id_ecdsa.pub already exists.
Overwrite (y/n)? y
Enter pin (empty for no pin): 
Confirm pin: 
Your identification has been saved in /home/xxx/.ssh/id_ecdsa.tpm
Your public key has been saved in /home/xxx/.ssh/id_ecdsa.pub
The key fingerprint is:
SHA256:q8ZLwQvwx7ne2C/xxxxxxxxxxxx
The key's randomart image is the color of television, tuned to a dead channel.
ム ssh-add -L
The agent has no identities.
ム systemctl --user restart ssh-tpm-agent.service
ム ssh-add -L
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBG1kcvbUpwS5vpn/xxxxxxxxxxxx
ム git pull
sign_and_send_pubkey: signing failed for ECDSA "/home/xxx/.ssh/id_ecdsa" from agent: agent refused operation
ム systemctl --user status ssh-tpm-agent.service
● ssh-tpm-agent.service - ssh-tpm-agent service
     Loaded: loaded (/home/xxx/.config/systemd/user/ssh-tpm-agent.service; indirect; preset: enabled)
     Active: active (running) since Thu 2024-08-08 00:22:36 CEST; 1min 46s ago
 Invocation: 37c20871df2147e79f878f837e6f593b
TriggeredBy: ● ssh-tpm-agent.socket
       Docs: man:ssh-agent(1)
             man:ssh-add(1)
             man:ssh(1)
   Main PID: 18718 (ssh-tpm-agent)
      Tasks: 7 (limit: 115482)
     Memory: 4.8M (peak: 6M)
        CPU: 179ms
     CGroup: /user.slice/user-1000.slice/user@1000.service/app.slice/ssh-tpm-agent.service
             └─18718 /usr/bin/ssh-tpm-agent -A /run/user/1000/ssh-agent.socket

Aug 08 00:22:36 xxx systemd[1641]: Started ssh-tpm-agent service.
Aug 08 00:22:36 xxx ssh-tpm-agent[18718]: time=2024-08-08T00:22:36.263+02:00 level=INFO msg="Activated agent by socket"
Aug 08 00:24:19 xxx ssh-tpm-agent[18718]: time=2024-08-08T00:24:19.566+02:00 level=INFO msg="agent 13: failed to sign: TPM_RC_AUTH_FAIL (session 1): the authorization HMAC check failed and DA counter incremented"

Edit 00:48: I will go to bed =) checking things in the next morning

Foxboron commented 3 months ago

I can't reproduce this.

Are you sure you are using the build from the git checkout for all your commands?

Foxboron commented 3 months ago

It's also worth pointing out that the error from the terminal example is different from the initial error you reported

FLX-0x00 commented 3 months ago

Jep. The error message is not the same. Error message in the title displays only when using my original key and not a new one. I am using the AUR git package. Not sure how to investigate further into this.

FLX-0x00 commented 3 months ago

Anyway, thanks for your time and effort. I will set up a new arch linux environment and try to get this to work. If you cannot reproduce it - its definitely my system or config. What is confusing is the fact that the agent and key worked fine until last month.

Foxboron commented 3 months ago

If you could tell me which version the old key was created with i can try to bisect it.

FLX-0x00 commented 3 months ago

The old key was created with ssh-keygen from arch linux on a air gapped system because I want to backup the private key. Then I imported / sealed it with ssh-tpm-keygen --import

Foxboron commented 3 months ago

Ah, lol.

I forgot I deprecated the format from the previous release :)

https://github.com/Foxboron/ssh-tpm-agent/releases/tag/v0.5.0

If the old key is made with the previous version please recreate it. If the issue is with the key you have ran --import on i can continue to look at it.

FLX-0x00 commented 3 months ago

I dont think so - I have used the linux build in command to generate my own key - but maybe this is the point? I have read the notes with the breaking changes and thought, they do not effect me because I have not generated my key with ssh-tpm-keygen but if the import is also affected... I will git clone the repo and compile ssh-tpm-keygen for myself to be sure.

Foxboron commented 3 months ago

--import wraps the ssh key with the TSS keyfile format, so it's affected.

Foxboron commented 3 months ago

I'll be more clear in future announcements if you feel the previous one was unclear :)

FLX-0x00 commented 3 months ago

Sorry for keeping you up on this, but I went all in as I saw this repo and used only the TPM sealed key for anything because the Yubikey GPG agent is quite pain in the ass ;)

So with the new binaries the old error is gone but the key does not work. The new error message appears now: time=2024-08-08T23:21:55.061+02:00 level=INFO msg="agent 13: failed to sign: TPM_RC_AUTH_FAIL (session 1): the authorization HMAC check failed and DA counter incremented"

FLX-0x00 commented 3 months ago

What about the public key?

The tool asks:

Sealing an existing public/private key pair.
id_ecdsa.pub already exists.
Overwrite (y/n)? y

but the pub key is not modified. I the public key is removed the ssh-tpm-keygen will not continue. Maybe its totally confusing but thats only my observations.

But I have tested with new keys so this should not matter.

Foxboron commented 3 months ago

but the pub key is not modified. I the public key is removed the ssh-tpm-keygen will not continue. Maybe its totally confusing but thats only my observations.

I'm just mimicking ssh-keygen and don't check the content of the file as we have not created the key yet. It should not matter.

Foxboron commented 3 months ago

Sorry for keeping you up on this, but I went all in as I saw this repo and used only the TPM sealed key for anything because the Yubikey GPG agent is quite pain in the ass ;)

No worries!

FLX-0x00 commented 3 months ago

Even with a complete new key I can not get this to work. Error message stays the same :/

Is there any chance to get deeper? jounalctl and systemd status has not much info for me. Encrypting and decrypting using gpg works fine btw. from TPM.

Foxboron commented 3 months ago

I suspect there is a regression from when I introduced wrapped keys. I don't have any test coverage for the key creation stuff so it's a bit hard to write proper tests for.

https://github.com/Foxboron/ssh-tpm-agent/commit/5334976e9a09040609b294de73a50b1ea97bdbc4

FLX-0x00 commented 3 months ago

Sounds reasonable. Is there anything that I can help you?

Foxboron commented 3 months ago

I've tried two sets of P256 keys with no password, with a password, and imported them all to my ssh-tpm-agent and sshed to my NAS. I'm still unable to recreate the error you are seeeing.

λ ssh-tpm-agent master Ɇ » ssh-keygen -t ecdsa -b 256 -f ./testkey
λ ssh-tpm-agent master Ɇ » ./bin/ssh-tpm-keygen --import ./testkey
λ ssh-tpm-agent master Ɇ » ./bin/ssh-tpm-add ./testkey.tpm

Am I missing something?

Foxboron commented 3 months ago

Please make sure you do make and run the binaries from ./bin.

ssh-tpm-agent can be ran with debug mode enabled.

λ ssh-tpm-agent master Ɇ » export SSH_AUTH_SOCK="/run/user/$(id -u)/ssh-tpm-agent.sock"
λ ssh-tpm-agent master Ɇ » ./bin/ssh-tpm-agent -d

Client

λ ssh-tpm-agent master Ɇ » export SSH_AUTH_SOCK="/run/user/$(id -u)/ssh-tpm-agent.sock"
λ ssh-tpm-agent master Ɇ » ssh-keygen -t ecdsa -b 256 -f ./testkey
λ ssh-tpm-agent master Ɇ » ./bin/ssh-tpm-keygen --import ./testkey
λ ssh-tpm-agent master Ɇ » ./bin/ssh-tpm-add ./testkey.tpm
FLX-0x00 commented 3 months ago

Hm. Maybe the ssh-tpm-add is the thing...for adding I always restarted the service. the ssh-tpm-add do not work in zsh or bash for my system O.o first parameter is not recognized

image

Foxboron commented 3 months ago

Lol, sorry. I pushed a commit now fixing it :) The CA stuff had faulty logic.

EDIT: Even more funny I had the fix locally while writing. I just forgot it would affect you as well while debugging this!

FLX-0x00 commented 3 months ago

hehe :D nice - love the bleeding edge!

even with debugging agent (service and socket stopped)

time=2024-08-09T00:08:14.956+02:00 level=DEBUG msg="called extensions"
time=2024-08-09T00:08:14.956+02:00 level=DEBUG msg="called list"
time=2024-08-09T00:08:14.967+02:00 level=DEBUG msg="called signwithflags"
time=2024-08-09T00:08:15.709+02:00 level=INFO msg="agent 13: failed to sign: TPM_RC_AUTH_FAIL (session 1): the authorization HMAC check failed and DA counter incremented"
time=2024-08-09T00:08:15.721+02:00 level=DEBUG msg="called signwithflags"
time=2024-08-09T00:08:16.465+02:00 level=INFO msg="agent 13: failed to sign: TPM_RC_AUTH_FAIL (session 1): the authorization HMAC check failed and DA counter incremented"
Foxboron commented 3 months ago

Is this with or without a password?

FLX-0x00 commented 3 months ago

with pin. will try without a pin

Foxboron commented 3 months ago

with pin.

Do you have an ssh askpass package installed :)?

Foxboron commented 3 months ago

I'm betting you don't.

I have for some reason implemented this in a way where we never actually check if an askpass binary is available before we continue. So your pin is never added to the TPM object which fails the AUTH check.

https://github.com/Foxboron/ssh-tpm-agent/blob/master/cmd/ssh-tpm-agent/main.go#L220

I'll try and rework this tomorrow and provide a clear error, but in the meantime you can install x11-ssh-askpass.

FLX-0x00 commented 3 months ago

I just bury myself

Foxboron commented 3 months ago

I just bury myself

Nah, it's my fault. There should be a clear error somewhere instead of silently continuing.

FLX-0x00 commented 3 months ago

Yes. Before this requirement it was using the casual arch linux default dialog that works for gpg

FLX-0x00 commented 3 months ago

I never looked into new dependancies...now it worked but the interface for the pin looks like Windows 95 or earlier :D

Foxboron commented 3 months ago

Pinentry depends on gnupg so it's a bit awkward to have with an ssh-agent. I'll fix this tomorrow and sorry for the issue :)

I never looked into new dependancies...now it worked but the interface for the pin looks like Windows 95 or earlier :D

Yes, there are some kde versions and stuff. I was contemplating writing some GUI thing for this and bundle it with this agent but havent had time to look at it.

FLX-0x00 commented 3 months ago

Oh man thanks for this incredible debug session :D what a damn issue. Yeah I am on XFCE so that was never a thing before

FLX-0x00 commented 3 months ago

My ssh directory totally messed up. Time to check btrfs snapshots :D

Thanks for taking time into this and the whole project! Awesome

Foxboron commented 3 months ago

No worries. Thanks for not loosing faith in me :)

FLX-0x00 commented 3 months ago

Maybe its time for a new release. I was using the official arch package and as well the git package. I am not sure if the binaries were up to date, especially now with the ./ssh-tpm-add fixes and maybe the pinentry workaround.

Foxboron commented 3 months ago

I'll do a release after fixing the askpass error stuff.