-
Notifications
You must be signed in to change notification settings - Fork 5.3k
PoC TLS resume on Linux client #64369
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
Changes from 1 commit
e52b12f
1811028
f9a2eab
9b87b4e
fd5635d
99240d4
342d916
acd3b46
51ec560
dc453d4
2170ec4
929e5c7
c2c8580
d2ab19b
576d4d5
876b5a0
434ab42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,6 @@ | |
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Net.Security; | ||
|
|
@@ -34,7 +33,7 @@ internal static partial class Ssl | |
| internal static unsafe partial void SslCtxSetAlpnSelectCb(SafeSslContextHandle ctx, delegate* unmanaged<IntPtr, byte**, byte*, byte*, uint, IntPtr, int> callback, IntPtr arg); | ||
|
|
||
| [GeneratedDllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_SslCtxSetCaching")] | ||
| internal static unsafe partial void SslCtxSetCaching(SafeSslContextHandle ctx, int mode, delegate* unmanaged<IntPtr, IntPtr, int> neewSessionCallback, delegate* unmanaged<IntPtr, IntPtr, void> removeSessionCallback); | ||
| internal static unsafe partial int SslCtxSetCaching(SafeSslContextHandle ctx, int mode, delegate* unmanaged<IntPtr, IntPtr, int> neewSessionCallback, delegate* unmanaged<IntPtr, IntPtr, void> removeSessionCallback); | ||
|
|
||
| internal static bool AddExtraChainCertificates(SafeSslContextHandle ctx, X509Certificate2[] chain) | ||
| { | ||
|
|
@@ -61,7 +60,8 @@ namespace Microsoft.Win32.SafeHandles | |
| { | ||
| internal sealed class SafeSslContextHandle : SafeHandle | ||
| { | ||
| private ConcurrentDictionary<string, IntPtr>? _sslSessions; | ||
| // This is session cache keyed by SNI e.g. TargetHost | ||
| private Dictionary<string, IntPtr>? _sslSessions; | ||
| private GCHandle _gch; | ||
|
|
||
| public SafeSslContextHandle() | ||
|
|
@@ -81,60 +81,74 @@ public override bool IsInvalid | |
|
|
||
| protected override bool ReleaseHandle() | ||
| { | ||
| Interop.Ssl.SslCtxDestroy(handle); | ||
| SetHandle(IntPtr.Zero); | ||
| if (_gch.IsAllocated) | ||
| { | ||
| //Interop.Ssl.SslCtxSetData(this, (IntPtr)_gch); | ||
| _gch.Free(); | ||
| } | ||
|
|
||
| if (_sslSessions != null) | ||
| { | ||
| // The SSL_CTX is ref counted and may not immediately die when we call SslCtxDestroy() | ||
| // Since there is no relation between SafeSslContextHandle and SafeSslHandle `this` can be release | ||
| // while we still have SSL session using it. | ||
| Interop.Ssl.SslCtxSetData(handle, IntPtr.Zero); | ||
|
|
||
| lock (_sslSessions) | ||
| { | ||
| foreach (string name in _sslSessions.Keys) | ||
| foreach (IntPtr session in _sslSessions.Values) | ||
| { | ||
| _sslSessions.Remove(name, out IntPtr session); | ||
| Interop.Ssl.SessionFree(session); | ||
| } | ||
|
|
||
| _sslSessions.Clear(); | ||
| } | ||
|
|
||
| Debug.Assert(_gch.IsAllocated); | ||
| _gch.Free(); | ||
| } | ||
|
|
||
| Interop.Ssl.SslCtxDestroy(handle); | ||
| SetHandle(IntPtr.Zero); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public void EnableSessionCache() | ||
| internal void EnableSessionCache() | ||
| { | ||
| _sslSessions = new ConcurrentDictionary<string, IntPtr>(); | ||
| Debug.Assert(_sslSessions == null); | ||
|
|
||
| _sslSessions = new Dictionary<string, IntPtr>(); | ||
| _gch = GCHandle.Alloc(this); | ||
wfurt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // This is needed so we can find the handle from session remove callback. | ||
| Debug.Assert(_gch.IsAllocated); | ||
| // This is needed so we can find the handle from session in SessionRemove callback. | ||
| Interop.Ssl.SslCtxSetData(this, (IntPtr)_gch); | ||
| } | ||
|
|
||
| public bool TryAddSession(IntPtr serverName, IntPtr session) | ||
| internal bool TryAddSession(IntPtr namePtr, IntPtr session) | ||
| { | ||
| Debug.Assert(_sslSessions != null && session != IntPtr.Zero); | ||
|
|
||
| if (_sslSessions == null || serverName == IntPtr.Zero) | ||
| if (_sslSessions == null || namePtr == IntPtr.Zero) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| string? name = Marshal.PtrToStringAnsi(serverName); | ||
| if (!string.IsNullOrEmpty(name)) | ||
| string? targetName = Marshal.PtrToStringAnsi(namePtr); | ||
| Debug.Assert(targetName != null); | ||
|
|
||
| if (!string.IsNullOrEmpty(targetName)) | ||
| { | ||
| Interop.Ssl.SessionSetHostname(session, serverName); | ||
| // We do this only for lookup in RemoveSession. | ||
| // Since this is part of chache manipulation and no function impact it is done here. | ||
| // This will use strdup() so it is safe to pass in raw pointer. | ||
| Interop.Ssl.SessionSetHostname(session, namePtr); | ||
|
|
||
| lock (_sslSessions) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The locking around _sslSessions makes sense, since you're manipulating state depending on how the dictionary performed. But, since you're already locking it, it feels like you want a non-Concurrent dictionary.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed. I was also thinking about grabbing extra reference on the session. That would allow me to use ConcurrentDictionary without locking as the session would never be released in the middle.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Something like ? (Upref inside has a race condition with the cleanup in ReleaseHandle) That would get a little weird since in the cleanup you'd need to call free twice, I think? The fact that we wrote ConcurrentDictionary suggests that it gives better perf (on average) than manual locking... but if the code to interact with it is doing memory/lifetime management and it becomes unreadable with the gymnastics... then locking is better for maintainability. (If it's clean code and more performant, than by all means use upref+ConcurrentDictionary)
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes. and then call free twice. I'm inclined to good with the lock and better maintainability as the perf does not depend on this. This happens one in while - not even for each SSL session. I started with ConcurrentDictionary but you are right - we don't need it at this point. |
||
| { | ||
| IntPtr oldSession = _sslSessions.GetOrAdd(name, session); | ||
| if (oldSession != session) | ||
| if (!_sslSessions.TryAdd(targetName, session)) | ||
| { | ||
| _sslSessions.Remove(name, out oldSession); | ||
| Interop.Ssl.SessionFree(oldSession); | ||
| oldSession = _sslSessions.GetOrAdd(name, session); | ||
| Debug.Assert(oldSession == session); | ||
| if (_sslSessions.Remove(targetName, out IntPtr oldSession)) | ||
| { | ||
| Interop.Ssl.SessionFree(oldSession); | ||
| } | ||
|
|
||
| bool added = _sslSessions.TryAdd(targetName, session); | ||
| Debug.Assert(added); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -144,21 +158,33 @@ public bool TryAddSession(IntPtr serverName, IntPtr session) | |
| return false; | ||
| } | ||
|
|
||
| public void Remove(string name, IntPtr session) | ||
| internal void RemoveSession(IntPtr namePtr, IntPtr session) | ||
| { | ||
| if (_sslSessions != null) | ||
| Debug.Assert(_sslSessions != null); | ||
|
|
||
| string? targetName = Marshal.PtrToStringAnsi(namePtr); | ||
| Debug.Assert(targetName != null); | ||
|
|
||
| if (_sslSessions != null && targetName != null) | ||
| { | ||
| IntPtr oldSession; | ||
| bool removed; | ||
| lock (_sslSessions) | ||
| { | ||
| if (!_sslSessions.Remove(name, out IntPtr oldSession)) | ||
| { | ||
| Interop.Ssl.SessionFree(oldSession); | ||
| } | ||
| removed = _sslSessions.Remove(targetName, out oldSession); | ||
| } | ||
|
|
||
| if (removed) | ||
| { | ||
| // It seems like we may be called more than once. Since we grabbed only one refference | ||
| // when added to Dictionary, we will also drop exactly one when removed. | ||
| Interop.Ssl.SessionFree(oldSession); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| public bool TrySetSession(SafeSslHandle sslHandle, string name) | ||
| internal bool TrySetSession(SafeSslHandle sslHandle, string name) | ||
| { | ||
| Debug.Assert(_sslSessions != null); | ||
|
|
||
|
|
@@ -169,6 +195,7 @@ public bool TrySetSession(SafeSslHandle sslHandle, string name) | |
|
|
||
| // even if we don't have matching session, we can get new one and we need | ||
| // way how to link SSL back to `this`. | ||
| Debug.Assert(Interop.Ssl.SslGetData(sslHandle) == IntPtr.Zero); | ||
| Interop.Ssl.SslSetData(sslHandle, (IntPtr)_gch); | ||
wfurt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| lock (_sslSessions) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.