Skip to content

Commit 8382cf1

Browse files
authored
Don't allocate unnecessary SslCtx (#103720)
1 parent 9e57de2 commit 8382cf1

File tree

1 file changed

+3
-5
lines changed
  • src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native

1 file changed

+3
-5
lines changed

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ internal static SafeSslContextHandle GetOrCreateSslContextHandle(SslAuthenticati
160160
if (sslAuthenticationOptions.IsClient)
161161
{
162162
var key = new SslContextCacheKey(protocols, sslAuthenticationOptions.CertificateContext?.TargetCertificate.GetCertHash(HashAlgorithmName.SHA256));
163-
164163
return s_clientSslContexts.GetOrCreate(key, static (args) =>
165164
{
166165
var (sslAuthOptions, protocols, allowCached) = args;
@@ -173,14 +172,13 @@ internal static SafeSslContextHandle GetOrCreateSslContextHandle(SslAuthenticati
173172

174173
bool hasAlpn = sslAuthenticationOptions.ApplicationProtocols != null && sslAuthenticationOptions.ApplicationProtocols.Count != 0;
175174

176-
SafeSslContextHandle? handle = AllocateSslContext(sslAuthenticationOptions, protocols, allowCached);
177-
178-
if (!sslAuthenticationOptions.CertificateContext!.SslContexts!.TryGetValue(protocols | (hasAlpn ? FakeAlpnSslProtocol : SslProtocols.None), out handle))
175+
SslProtocols serverCacheKey = protocols | (hasAlpn ? FakeAlpnSslProtocol : SslProtocols.None);
176+
if (!sslAuthenticationOptions.CertificateContext!.SslContexts!.TryGetValue(serverCacheKey, out SafeSslContextHandle? handle))
179177
{
180178
// not found in cache, create and insert
181179
handle = AllocateSslContext(sslAuthenticationOptions, protocols, allowCached);
182180

183-
SafeSslContextHandle cached = sslAuthenticationOptions.CertificateContext!.SslContexts!.GetOrAdd(protocols | (hasAlpn ? FakeAlpnSslProtocol : SslProtocols.None), handle);
181+
SafeSslContextHandle cached = sslAuthenticationOptions.CertificateContext!.SslContexts!.GetOrAdd(serverCacheKey, handle);
184182

185183
if (handle != cached)
186184
{

0 commit comments

Comments
 (0)