Skip to content

Commit 23f21e4

Browse files
rzikmcampersau
andcommitted
[release/6.0] Avoid rooting X509Certificate2 in SslSessionCache (dotnet#101120)
* Avoid rooting X509Certificate2 in SslSessionCache * Update src/libraries/System.Net.Security/src/System/Net/Security/SslStreamPal.Windows.cs Co-authored-by: campersau <[email protected]> --------- Co-authored-by: campersau <[email protected]>
1 parent 000cc30 commit 23f21e4

File tree

3 files changed

+9
-11
lines changed

3 files changed

+9
-11
lines changed

src/libraries/Common/src/Interop/Windows/SspiCli/SecuritySafeHandles.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,13 @@ public static unsafe int AcquireCredentialsHandle(
322322
internal sealed class SafeFreeCredential_SECURITY : SafeFreeCredentials
323323
{
324324
#pragma warning disable 0649
325-
// This is used only by SslStream but it is included elsewhere
326-
public X509Certificate? LocalCertificate;
327-
#pragma warning restore 0649
325+
// This is used only by SslStream but it is included elsewhere
326+
public bool HasLocalCertificate;
327+
#pragma warning restore 0649
328328
public SafeFreeCredential_SECURITY() : base() { }
329329

330330
protected override bool ReleaseHandle()
331331
{
332-
LocalCertificate?.Dispose();
333332
return Interop.SspiCli.FreeCredentialsHandle(ref _handle) == 0;
334333
}
335334
}

src/libraries/System.Net.Security/src/System/Net/CertificateValidationPal.Windows.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ internal static bool IsLocalCertificateUsed(SafeFreeCredentials? _credentialsHan
101101
// This is TLS Resumed session. Windows can fail to query the local cert bellow.
102102
// Instead, we will determine the usage form used credentials.
103103
SafeFreeCredential_SECURITY creds = (SafeFreeCredential_SECURITY)_credentialsHandle!;
104-
return creds.LocalCertificate != null;
104+
return creds.HasLocalCertificate;
105105
}
106106

107107
SafeFreeCertContext? localContext = null;

src/libraries/System.Net.Security/src/System/Net/Security/SslStreamPal.Windows.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ public static SecurityStatusPal InitializeSecurityContext(ref SafeFreeCredential
112112
return SecurityStatusAdapterPal.GetSecurityStatusPalFromNativeInt(errorCode);
113113
}
114114

115-
public static SecurityStatusPal Renegotiate(ref SafeFreeCredentials? credentialsHandle, ref SafeDeleteSslContext? context, SslAuthenticationOptions sslAuthenticationOptions, out byte[]? outputBuffer )
115+
public static SecurityStatusPal Renegotiate(ref SafeFreeCredentials? credentialsHandle, ref SafeDeleteSslContext? context, SslAuthenticationOptions sslAuthenticationOptions, out byte[]? outputBuffer)
116116
{
117117
byte[]? output = Array.Empty<byte>();
118-
SecurityStatusPal status = AcceptSecurityContext(ref credentialsHandle, ref context, Span<byte>.Empty, ref output, sslAuthenticationOptions);
118+
SecurityStatusPal status = AcceptSecurityContext(ref credentialsHandle, ref context, Span<byte>.Empty, ref output, sslAuthenticationOptions);
119119
outputBuffer = output;
120120
return status;
121121
}
@@ -139,8 +139,7 @@ public static SafeFreeCredentials AcquireCredentialsHandle(SslStreamCertificateC
139139
if (newCredentialsRequested && certificateContext != null)
140140
{
141141
SafeFreeCredential_SECURITY handle = (SafeFreeCredential_SECURITY)cred;
142-
// We need to create copy to avoid Disposal issue.
143-
handle.LocalCertificate = new X509Certificate2(certificateContext.Certificate);
142+
handle.HasLocalCertificate = true;
144143
}
145144

146145
return cred;
@@ -270,11 +269,11 @@ public static unsafe SafeFreeCredentials AcquireCredentialsHandleSchCredentials(
270269
Interop.SspiCli.SCH_CREDENTIALS credential = default;
271270
credential.dwVersion = Interop.SspiCli.SCH_CREDENTIALS.CurrentVersion;
272271
credential.dwFlags = flags;
273-
Interop.Crypt32.CERT_CONTEXT *certificateHandle = null;
272+
Interop.Crypt32.CERT_CONTEXT* certificateHandle = null;
274273
if (certificate != null)
275274
{
276275
credential.cCreds = 1;
277-
certificateHandle = (Interop.Crypt32.CERT_CONTEXT *)certificate.Handle;
276+
certificateHandle = (Interop.Crypt32.CERT_CONTEXT*)certificate.Handle;
278277
credential.paCred = &certificateHandle;
279278
}
280279

0 commit comments

Comments
 (0)