Skip to content

Commit 03f58cb

Browse files
authored
Reduce some allocations in SslStream handshake. (#103814)
* Reduce some allocations in SslStream handshake. * Remove now unnecessary change
1 parent 8169bde commit 03f58cb

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/libraries/System.Net.Security/src/System/Net/Security/SslAuthenticationOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ internal void UpdateOptions(SslServerAuthenticationOptions sslServerAuthenticati
149149

150150
private static SslProtocols FilterOutIncompatibleSslProtocols(SslProtocols protocols)
151151
{
152-
if (protocols.HasFlag(SslProtocols.Tls12) || protocols.HasFlag(SslProtocols.Tls13))
152+
if ((protocols & (SslProtocols.Tls12 | SslProtocols.Tls13)) != SslProtocols.None)
153153
{
154154
#pragma warning disable 0618
155155
// SSL2 is mutually exclusive with >= TLS1.2

src/libraries/System.Net.Security/src/System/Net/Security/SslStreamCertificateContext.Linux.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Security.Cryptography.X509Certificates;
1212
using System.Text;
1313
using System.Net;
14+
using System.Threading;
1415
using System.Threading.Tasks;
1516
using Microsoft.Win32.SafeHandles;
1617

@@ -23,7 +24,22 @@ public partial class SslStreamCertificateContext
2324
internal static TimeSpan RefreshAfterFailureBackOffInterval => TimeSpan.FromSeconds(5);
2425

2526
private const bool TrimRootCertificate = true;
26-
internal readonly ConcurrentDictionary<SslProtocols, SafeSslContextHandle> SslContexts;
27+
internal ConcurrentDictionary<SslProtocols, SafeSslContextHandle> SslContexts
28+
{
29+
get
30+
{
31+
ConcurrentDictionary<SslProtocols, SafeSslContextHandle>? sslContexts = _sslContexts;
32+
if (sslContexts is null)
33+
{
34+
Interlocked.CompareExchange(ref _sslContexts, new(), null);
35+
sslContexts = _sslContexts;
36+
}
37+
38+
return sslContexts;
39+
}
40+
}
41+
42+
private ConcurrentDictionary<SslProtocols, SafeSslContextHandle>? _sslContexts;
2743
internal readonly SafeX509Handle CertificateHandle;
2844
internal readonly SafeEvpPKeyHandle KeyHandle;
2945

@@ -57,7 +73,6 @@ private SslStreamCertificateContext(X509Certificate2 target, ReadOnlyCollection<
5773

5874
TargetCertificate = target;
5975
Trust = trust;
60-
SslContexts = new ConcurrentDictionary<SslProtocols, SafeSslContextHandle>();
6176

6277
using (RSAOpenSsl? rsa = (RSAOpenSsl?)target.GetRSAPrivateKey())
6378
{

0 commit comments

Comments
 (0)