Skip to content

Include Root Certificates in Custom Trust Store #5624

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

Merged
merged 2 commits into from
Apr 8, 2025
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 @@ -53,6 +53,10 @@ public bool TryGetCustomRemoteCertificateValidationCallback(
return false;
}

// Do this outside of the callback so that we aren't opening the root store every request.
using var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine, OpenFlags.ReadOnly);
var rootCertificates = store.Certificates;

// Ref: https://github.com/dotnet/runtime/issues/39835#issuecomment-663020581
callback = (certificate, chain, errors) =>
{
Expand All @@ -62,6 +66,10 @@ public bool TryGetCustomRemoteCertificateValidationCallback(
}

chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;

// We want our additional certificates to be in addition to the machines root store.
chain.ChainPolicy.CustomTrustStore.AddRange(rootCertificates);

foreach (var additionalCertificate in AdditionalCustomTrustCertificates)
{
chain.ChainPolicy.CustomTrustStore.Add(additionalCertificate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,41 @@ public async Task CallHttpWithSelfSignedCert_SelfSignedCertificateConfigured_Wit
Assert.Equal("Hi", response);
}

[Fact]
public async Task CallHttp_ReachingOutToServerTrustedThroughSystemCA()
{
var services = CreateServices((gs, environment, config) => { }, services =>
{
services.Configure<X509ChainOptions>(options =>
{
options.AdditionalCustomTrustCertificates = [];
});
});

var httpClient = services.GetRequiredService<IHttpClientFactory>().CreateClient();

var response = await httpClient.GetAsync("https://example.com");
response.EnsureSuccessStatusCode();
}

[Fact]
public async Task CallHttpWithCustomTrustForSelfSigned_ReachingOutToServerTrustedThroughSystemCA()
{
var selfSignedCertificate = CreateSelfSignedCert("localhost");
var services = CreateServices((gs, environment, config) => { }, services =>
{
services.Configure<X509ChainOptions>(options =>
{
options.AdditionalCustomTrustCertificates = [selfSignedCertificate];
});
});

var httpClient = services.GetRequiredService<IHttpClientFactory>().CreateClient();

var response = await httpClient.GetAsync("https://example.com");
response.EnsureSuccessStatusCode();
}

private static async Task<IAsyncDisposable> CreateServerAsync(int port, Action<HttpsConnectionAdapterOptions> configure)
{
var builder = WebApplication.CreateEmptyBuilder(new WebApplicationOptions());
Expand Down
Loading