Skip to content

Commit 8a3e603

Browse files
[Android] Fix SslStreamCertificateContext empty custom trust store exception (#104016)
* Check if certificate collections are not empty before changing trust mode to custom root trust * Enable SslStream_ClientCertificateContext_SendsChain test on Android * Apply suggestions from reviews * Avoid unnecessary allocations
1 parent 117c4ab commit 8a3e603

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,20 @@ internal static SslStreamCertificateContext Create(
5656

5757
if (trust != null)
5858
{
59-
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
6059
if (trust._store != null)
6160
{
6261
chain.ChainPolicy.CustomTrustStore.AddRange(trust._store.Certificates);
6362
}
63+
6464
if (trust._trustList != null)
6565
{
6666
chain.ChainPolicy.CustomTrustStore.AddRange(trust._trustList);
6767
}
68+
69+
if (chain.ChainPolicy.CustomTrustStore.Count > 0)
70+
{
71+
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
72+
}
6873
}
6974

7075
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllFlags;
@@ -77,7 +82,7 @@ internal static SslStreamCertificateContext Create(
7782
NetEventSource.Error(null, $"Failed to build chain for {target.Subject}");
7883
}
7984

80-
if (!chainStatus && ChainBuildNeedsTrustedRoot && additionalCertificates != null)
85+
if (!chainStatus && ChainBuildNeedsTrustedRoot && additionalCertificates?.Count > 0)
8186
{
8287
// Some platforms like Android may not be able to build the chain unless the chain root is trusted.
8388
// We can try to rebuild the chain with making all extra certificates trused.

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

-1
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,6 @@ public async Task SslStream_ClientCertificate_SendsChain()
917917
[Theory]
918918
[InlineData(true)]
919919
[InlineData(false)]
920-
[ActiveIssue("https://github.com/dotnet/runtime/issues/68206", TestPlatforms.Android)]
921920
public async Task SslStream_ClientCertificateContext_SendsChain(bool useTrust)
922921
{
923922
(X509Certificate2 clientCertificate, X509Certificate2Collection clientChain) = Configuration.Certificates.GenerateCertificates(nameof(SslStream_ClientCertificateContext_SendsChain), serverCertificate: false);

0 commit comments

Comments
 (0)