Skip to content

agent: never serve an expired certificate - #150

Closed
andsens wants to merge 2 commits into
Foxboron:masterfrom
orbit-online:drop-expired-certs
Closed

andsens wants to merge 2 commits into
Foxboron:masterfrom
orbit-online:drop-expired-certs

Conversation

@andsens

@andsens andsens commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Renewing a short-lived certificate adds the new one next to the old: the agent only replaces an identical key, and for a key with a certificate that is the whole certificate blob. Unless the client removes the old certificate itself, expired ones pile up, ssh offers them in order, and the server cuts the connection at MaxAuthTries before it gets to the valid one.

Drop keys whose certificate is past its ValidBefore before listing them or looking up a signer, so expired certificates are never offered and never sign. Keys without a certificate, and certificates without an expiry, are untouched.

The test certificates of keytest had ValidBefore 0, which is expired; they are now valid forever.

p.s.: Built with AI help.

@Foxboron

Copy link
Copy Markdown
Owner

Whats the ssh-agent behaviour around this? Is it this PR consistent with that behaviour?

Comment thread agent/agent.go Outdated
}

// certificate returns the certificate k carries, or nil.
func certificate(k key.SSHTPMKeys) *ssh.Certificate {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key.SSHTPMKeys has a pointer Certificate field. Why are we parsing the k.AgentKey().Blob for the *ssh.Certificate struct?

ssh-tpm-agent parsed the lifetime constraint of SSH_AGENTC_ADD_ID_CONSTRAINED
and of tpm-add-key and dropped it. Keep it as the expiry of the key and,
like the reaper of ssh-agent(1), delete the keys whose lifetime has run
out before listing, signing or removing.
Renewing a short-lived certificate adds the new one next to the old, and
the agent keeps the old one until it is removed: ssh offers them in
order, and the server cuts the connection at MaxAuthTries before it gets
to a valid one.

ssh-add(1) avoids that client-side: it adds a certificate with a
lifetime that ends CERT_EXPIRY_GRACE (five minutes) after its
ValidBefore, so the agent deletes it, and ignores one already past
that. Do the same, with -N to add certificates without a lifetime, as
ssh-add -N does.
@andsens

andsens commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Whats the ssh-agent behaviour around this? Is it this PR consistent with that behaviour?

No, actually. ssh-agent has a reaper that drops certs whose lifetime has expired. It refuses adding expired certs and otherwise sets a lifetime of valid_before − now + 5 min.

So if we were to follow what ssh-agent does we'd need to introduce the whole concept of key lifetime, which might make sense, but is a bit of a bigger change.

@andsens

andsens commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Then again, it is on your TODO :)

// LifetimeSecs is not yet supported by ssh-tpm-agent
k.ConfirmBeforeUse = confirmBeforeUse
return nil

@andsens
andsens marked this pull request as draft November 26, 2026 11:05
@andsens andsens closed this Nov 27, 2026
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 this pull request may close these issues.

2 participants