Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0-staging] use also SslCertificateTrust when constructing CertificateContext #104541

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace System.Net.Security
public partial class SslStreamCertificateContext
{
private const bool TrimRootCertificate = true;
private const bool ChainBuildNeedsTrustedRoot = true;

private SslStreamCertificateContext(X509Certificate2 target, ReadOnlyCollection<X509Certificate2> intermediates, SslCertificateTrust? trust)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace System.Net.Security
public partial class SslStreamCertificateContext
{
private const bool TrimRootCertificate = true;
private const bool ChainBuildNeedsTrustedRoot = false;
internal readonly ConcurrentDictionary<SslProtocols, SafeSslContextHandle> SslContexts;
internal readonly SafeX509Handle CertificateHandle;
internal readonly SafeEvpPKeyHandle KeyHandle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public partial class SslStreamCertificateContext
{
// No leaf, no root.
private const bool TrimRootCertificate = true;
private const bool ChainBuildNeedsTrustedRoot = false;

private SslStreamCertificateContext(X509Certificate2 target, ReadOnlyCollection<X509Certificate2> intermediates, SslCertificateTrust? trust)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public partial class SslStreamCertificateContext
{
// No leaf, include root.
private const bool TrimRootCertificate = false;
private const bool ChainBuildNeedsTrustedRoot = false;

internal static SslStreamCertificateContext Create(X509Certificate2 target)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,24 @@ internal static SslStreamCertificateContext Create(
{
if (additionalCertificates != null)
{
foreach (X509Certificate cert in additionalCertificates)
chain.ChainPolicy.ExtraStore.AddRange(additionalCertificates);
}

if (trust != null)
{
if (trust._store != null)
{
chain.ChainPolicy.CustomTrustStore.AddRange(trust._store.Certificates);
}

if (trust._trustList != null)
{
chain.ChainPolicy.CustomTrustStore.AddRange(trust._trustList);
}

if (chain.ChainPolicy.CustomTrustStore.Count > 0)
{
chain.ChainPolicy.ExtraStore.Add(cert);
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
}
}

Expand All @@ -67,6 +82,20 @@ internal static SslStreamCertificateContext Create(
NetEventSource.Error(null, $"Failed to build chain for {target.Subject}");
}

if (!chainStatus && ChainBuildNeedsTrustedRoot && additionalCertificates?.Count > 0)
{
// Some platforms like Android may not be able to build the chain unless the chain root is trusted.
// We can try to rebuild the chain with making all extra certificates trused.
// We do not try to evaluate trust here, we jsut need to construct the chain so it should not matter.
chain.ChainPolicy.CustomTrustStore.AddRange(additionalCertificates);
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
chainStatus = chain.Build(target);
if (!chainStatus && NetEventSource.Log.IsEnabled())
{
NetEventSource.Error(null, $"Failed to build chain for {target.Subject} while trusting additional certificates");
}
}

int count = chain.ChainElements.Count - 1;

// Some platforms (e.g. Android) can't ignore all verification and will return zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,19 +914,28 @@ public async Task SslStream_ClientCertificate_SendsChain()
}
}

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/68206", TestPlatforms.Android)]
public async Task SslStream_ClientCertificateContext_SendsChain()
[Theory]
[InlineData(true)]
[InlineData(false)]
public async Task SslStream_ClientCertificateContext_SendsChain(bool useTrust)
{
(X509Certificate2 clientCertificate, X509Certificate2Collection clientChain) = TestHelper.GenerateCertificates(nameof(SslStream_ClientCertificateContext_SendsChain), serverCertificate: false);
TestHelper.CleanupCertificates(nameof(SslStream_ClientCertificateContext_SendsChain));

SslCertificateTrust? trust = null;
if (useTrust)
{
// This is simplification. We make all the intermediates trusted,
// normally just the root would go here.
trust = SslCertificateTrust.CreateForX509Collection(clientChain, false);
}

var clientOptions = new SslClientAuthenticationOptions()
{
TargetHost = "localhost",
};
clientOptions.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
clientOptions.ClientCertificateContext = SslStreamCertificateContext.Create(clientCertificate, clientChain);
clientOptions.ClientCertificateContext = SslStreamCertificateContext.Create(clientCertificate, useTrust ? null : clientChain, offline:true, trust);

await SslStream_ClientSendsChain_Core(clientOptions, clientChain);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
Link="ProductionCode\System\Net\Security\ReadWriteAdapter.cs" />
<Compile Include="..\..\src\System\Net\SslStreamContext.cs"
Link="ProductionCode\System\Net\SslStreamContext.cs" />
<Compile Include="..\..\src\System\Net\Security\SslCertificateTrust.cs"
Link="ProductionCode\System\Net\Security\SslCertificateTrust.cs" />
<Compile Include="$(CommonPath)System\Net\SecurityProtocol.cs"
Link="ProductionCode\Common\System\Net\SecurityProtocol.cs" />
<Compile Include="..\..\src\System\Net\Security\TlsAlertType.cs"
Expand Down
Loading