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

Add timestamp-based expiration to cached SafeFreeCredentials #66334

Merged
merged 3 commits into from
Mar 24, 2022

Conversation

rzikm
Copy link
Member

@rzikm rzikm commented Mar 8, 2022

Fixes #43879

This PR adds timestamp-based invalidation of SafeFreeCredentials in SslSessionCache. The expiration timestamp is calculated based on NotAfter fields of the certificates in the SslAuthenticationOptions.CertificateContext (both the actual certificates and the intermediate certs in the chain).

Since this PR does not add any X509Chain.Build() calls to the hot path, I believe there should not be a significant perf hit from this change. I can run benchmarks next week once I have access to my desktop machine.

Also to consider: does it make sense to try to create a test for this? It would probably go to OuterLoop since it would run around 2 minutes, but since the repro requires admin privileges (at least on Windows), I am not sure how viable it is. Maybe running the test on Linux would be sufficient.

@ghost
Copy link

ghost commented Mar 8, 2022

Tagging subscribers to this area: @dotnet/ncl, @vcsjones
See info in area-owners.md if you want to be subscribed.

Issue Details

Fixes Issue #43879

This PR adds timestamp-based invalidation of SafeFreeCredentials in SslSessionCache. The expiration timestamp is calculated based on NotAfter fields of the certificates in the SslAuthenticationOptions.CertificateContext (both the actual certificates and the intermediate certs in the chain).

Author: rzikm
Assignees: rzikm
Labels:

area-System.Net.Security

Milestone: -

@rzikm rzikm changed the title 43879-sslcache-time Add timestamp-based expiration to cached SafeFreeCredentials Mar 8, 2022
@rzikm rzikm marked this pull request as ready for review March 8, 2022 15:28
@rzikm rzikm requested a review from wfurt March 8, 2022 15:31
@@ -18,8 +19,13 @@ internal abstract class SafeFreeCredentials : DebugSafeHandle
internal abstract class SafeFreeCredentials : SafeHandle
{
#endif
internal DateTime _expiry;
Copy link
Member

Choose a reason for hiding this comment

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

I know this is tricky but AFAIK Unix does not have the same issue as Windows. I'm wondering if we should (need to?) replicate the logic.

Copy link
Member Author

Choose a reason for hiding this comment

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

I wasn't sure how to do this so that the code compiles for both platforms, since the code for SslSessionsCache where I access the Expiry property is not platform specific. However, it would be possible to always leave MaxValue on non-windows platforms and set the expiry in SslStreamPal.Windows.cs

cred._expiry = GetExpiryTimestamp(certificateContext);
}

static DateTime GetExpiryTimestamp(SslStreamCertificateContext certificateContext)
Copy link
Member

Choose a reason for hiding this comment

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

We can possibly also add it to SslStreamCertificateContext itself. It may be used multiple times but I guess the difference would be probably very small.

Copy link
Member Author

Choose a reason for hiding this comment

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

I considered it, but I couldn't think of other uses elsewhere. We can always move it later when we need it.

@rzikm rzikm requested a review from wfurt March 22, 2022 19:29
Copy link
Member

@wfurt wfurt left a comment

Choose a reason for hiding this comment

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

LGTM

@rzikm rzikm merged commit e97af55 into dotnet:main Mar 24, 2022
radekdoulik pushed a commit to radekdoulik/runtime that referenced this pull request Mar 30, 2022
…66334)

* Add Expiry timestamp on SafeFreeCredentials handle

* Recalculate expiration timestamp based on CertificateContext

* Fix case when user provides CertificateContext
@karelz karelz added this to the 7.0.0 milestone Apr 8, 2022
@AndyAyersMS
Copy link
Member

AndyAyersMS commented Apr 27, 2022

More possible regressions here: dotnet/perf-autofiling-issues#4279
newplot - 2022-04-27T161454 920

@wfurt
Copy link
Member

wfurt commented Apr 27, 2022

that should be tracked by #68408 @AndyAyersMS. Basically all expired certificates will be more expensive now regardless of key size or negotiated protocol.

@ghost ghost locked as resolved and limited conversation to collaborators May 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Caching of SCHANNEL_CRED may lead to SslStream using expired certificate chain
4 participants