Skip to content

Commit a85a2f5

Browse files
authored
skip adding leaf certificate to ChainPolicy.ExtraStore (#67279)
1 parent fcc0351 commit a85a2f5

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

src/libraries/Common/src/Interop/Windows/SChannel/UnmanagedCertificateContext.IntPtr.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@ internal static unsafe X509Certificate2Collection GetRemoteCertificatesFromStore
3535
break;
3636
}
3737

38-
var cert = new X509Certificate2(new IntPtr(next));
39-
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(certContext, $"Adding remote certificate:{cert}");
38+
if ((IntPtr)next != certContext)
39+
{
40+
var cert = new X509Certificate2(new IntPtr(next));
41+
if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(certContext, $"Adding remote certificate:{cert}");
42+
43+
result.Add(cert);
44+
}
4045

41-
result.Add(cert);
4246
last = next;
4347
}
4448
}

src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamNetworkStreamTest.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,7 @@ public async Task SslStream_ClientCertificate_SendsChain()
862862
List<SslStream> streams = new List<SslStream>();
863863
TestHelper.CleanupCertificates();
864864
(X509Certificate2 clientCertificate, X509Certificate2Collection clientChain) = TestHelper.GenerateCertificates("SslStream_ClinetCertificate_SendsChain", serverCertificate: false);
865+
865866
using (X509Store store = new X509Store(StoreName.CertificateAuthority, StoreLocation.CurrentUser))
866867
{
867868
// add chain certificate so we can construct chain since there is no way how to pass intermediates directly.
@@ -883,7 +884,7 @@ public async Task SslStream_ClientCertificate_SendsChain()
883884
}
884885
}
885886

886-
var clientOptions = new SslClientAuthenticationOptions() { TargetHost = "localhost", };
887+
var clientOptions = new SslClientAuthenticationOptions() { TargetHost = "localhost" };
887888
clientOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
888889
clientOptions.LocalCertificateSelectionCallback = (sender, target, certificates, remoteCertificate, issuers) => clientCertificate;
889890

@@ -900,7 +901,8 @@ public async Task SslStream_ClientCertificate_SendsChain()
900901
_output.WriteLine("received {0}", c.Subject);
901902
}
902903

903-
Assert.True(chain.ChainPolicy.ExtraStore.Count >= clientChain.Count - 1, "client did not sent expected chain");
904+
Assert.Equal(clientChain.Count - 1, chain.ChainPolicy.ExtraStore.Count);
905+
Assert.Contains(clientChain[0], chain.ChainPolicy.ExtraStore);
904906
return true;
905907
};
906908

@@ -913,7 +915,7 @@ public async Task SslStream_ClientCertificate_SendsChain()
913915

914916
Task t1 = client.AuthenticateAsClientAsync(clientOptions, CancellationToken.None);
915917
Task t2 = server.AuthenticateAsServerAsync(serverOptions, CancellationToken.None);
916-
await Task.WhenAll(t1, t2).WaitAsync(TestConfiguration.PassingTestTimeout);
918+
await TestConfiguration.WhenAllOrAnyFailedWithTimeout(t1, t2);
917919

918920
// hold to the streams so they stay in credential cache
919921
streams.Add(client);

0 commit comments

Comments
 (0)