Validate certificate chain with custom trust root throws ArgumentOutOfRangeException

Open
#116,977 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp

Research direction

Begin with the supplied net8.0-windows10.0.22621.0 console reproduction and inspect Windows.Security.Cryptography.Certificates ChainBuildingParameters, ExclusiveTrustRoots, and BuildChainAsync behavior. Done means the custom trust root can be used without ArgumentOutOfRangeException and the resulting chain validates successfully.

Written by the indexing model from the issue text.

Description

area-System.Security tracking-external-issue
Describe the bug

When validating a certificate, where the issuer is known to the application, but not installed in the Trusted Root certificate store, I believe the correct approach is to add the known issuer certificate to the ExclusiveTrustRoots collection and then build the chain.

This works using the X509Certificate2 and X509Chain in .NET.
However, using the classes in namespace Windows.Security.Cryptography.Certificates does not work as expected.

I've tried all possible combinations of calls, but they all fail when building the chain.

The use case is validation of certificates using the Windows.Web.HttpClient and HttpServerCustomValidationRequestedEventArgs which contains a server certificate to be validated.

To Reproduce

Create a Console application with TargetFramework:
net8.0-windows10.0.22621.0

using System.Diagnostics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Security.Cryptography.Certificates;

internal class Program
{
    private static async Task Main(string[] args)
    {
        // Issuer cert (TestRoot) is a self signed certificate expiring 2029-12-31
        var testRootBlob = Convert.FromBase64String("MIIFLDCCAxSgAwIBAgIQG6GgIwJQhZ1OnoPKkKzHzzANBgkqhkiG9w0BAQsFADBBMREwDwYDVQQDEwhUZXN0Um9vdDEfMB0GA1UEChMWRGlzdHJpYnV0ZWQgTWVkaWNhbCBBQjELMAkGA1UEBhMCU0UwHhcNMjUwNTEyMDY1NzA4WhcNMjkxMjMxMDAwMDAwWjBBMREwDwYDVQQDEwhUZXN0Um9vdDEfMB0GA1UEChMWRGlzdHJpYnV0ZWQgTWVkaWNhbCBBQjELMAkGA1UEBhMCU0UwggIiMA0GCSqGSIb3DQEBAQUA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qGrPLaXtMPVw2lWj8DsL3acRWNwSu5JaI8yReb8wnLwmZMQTDq6SqX2qLWBurBp5s1glkA/M7q6Wpd6Z2yVwfNLkfy+s1PdUBobl6IYaXemebvbVquFHvSbsJzUC8RFhtnf1gq0N2x4lfUG8X+aqXOovAhaN9Rt7EONGnWeHgqiQy0yrvhHMsPUlZABEcFOxCp23LTIEnPampsmH3qPhrC+/ffOpvUEyaBvq1Ek7PkCcPj2/sVyfePCR+krvOaPxLCO1ZGHisA7A6fqn3hL+yRLb0jBLIqm9ZxTDa9L29Zesmeur2HPA0Z3cch7uiik1FQ7oGXWVU63wt4QaDfC+K7DLCImHLuwBbXv7UqGA7xLupEV6BIy92cRrLZO/g=");

        // Child cert (TestChild) is issued by TestRoot
        var testChildBlob = Convert.FromBase64String("MIIFOjCCAyKgAwIBAgIgUcoa9naHZLQHtY9VfAx+yD66yBFArQGbW4cOig+nmX4wDQYJKoZIhvcNAQELBQAwQTERMA8GA1UEAxMIVGVzdFJvb3QxHzAdBgNVBAoTFkRpc3RyaWJ1dGVkIE1lZGljYWwgQUIxCzAJBgNVBAYTAlNFMB4XDTI1MDUxMTIyMDAwMFoXDTI5MTIzMDIzMDAwMFowQjELMAkGA1UEBhMCU0UxHzAdBgNVBAoTFkRpc3RyaWJ1dGVkIE1lZGljYWwgQUIxEjAQBgNVBAMTCVRlc3RDaGlsZDCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAOl3X4p59+GvYVI9aM6L61bLtOA6NLSW0TRVsx6v/n2z8qyDHCAu+IisvBjBb0nakrCVCNs97h7AJbBifOfii58hRSFtTDjiy0rz/BxZKEdyJIix1BhphEXsPVbaz4HmNG27bYocVIKOGUiFxt5VC9plq5Tco5K9IuH4sQeBtWWRR4w3ZwhYa9q2zC4QadiQqCdhgsAa0YH0D3brbbEmPgPvj2LEPLpi0ax0ZWPWXSYuxceLhDVG3eMS9vlB82UU5cAp/3YIf04rzt5+C3Uewrk2Fgw7d4efoMxDLovU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JxOpiZ0n6QpbuoJbeWkY3pRJd0ZyfCQD+OB3YbqpL+ISRxxC8JvUcXKE1ICOKT/3dU/Cxg==");

        var testRootCert = new Windows.Security.Cryptography.Certificates.Certificate(testRootBlob.AsBuffer());
        var testChildCert = new Windows.Security.Cryptography.Certificates.Certificate(testChildBlob.AsBuffer());

        var builderParams = new Windows.Security.Cryptography.Certificates.ChainBuildingParameters()
        {
            CurrentTimeValidationEnabled = true,
            NetworkRetrievalEnabled = false,
            RevocationCheckEnabled = false,
        };

        // TestRoot is not trusted by the local system
        // but this is expected to do the trick
        builderParams.ExclusiveTrustRoots.Add(testRootCert);

        // None of these works!

        var certs = new List<Certificate>();
        certs.Add(testChildCert); // Only TestChild
        try { await testRootCert.BuildChainAsync(certs, builderParams); } catch { Debug.Assert(false); }
        try { await testChildCert.BuildChainAsync(certs, builderParams); } catch { Debug.Assert(false); }

        certs.Clear();
        certs.Add(testRootCert); // Only TestRoot
        try { await testRootCert.BuildChainAsync(certs, builderParams); } catch { Debug.Assert(false); }
        try { await testChildCert.BuildChainAsync(certs, builderParams); } catch { Debug.Assert(false); }

        certs.Clear();
        certs.Add(testRootCert); // Both TestRoot and TestChild
        certs.Add(testChildCert);
        try { await testRootCert.BuildChainAsync(certs, builderParams); } catch { Debug.Assert(false); }
        try { await testChildCert.BuildChainAsync(certs, builderParams); } catch { Debug.Assert(false); }

        certs.Clear();
        certs.Add(testChildCert); // Both TestRoot and TestChild (different order)
        certs.Add(testRootCert); 
        try { await testRootCert.BuildChainAsync(certs, builderParams); } catch { Debug.Assert(false); }
        try { await testChildCert.BuildChainAsync(certs, builderParams); } catch { Debug.Assert(false); }

        // Clearing the trusted roots will work...
        builderParams.ExclusiveTrustRoots.Clear();

        var chain = await testChildCert.BuildChainAsync(certs, builderParams);
        var result = chain.Validate();

        // ... but the result is now Untrusted (which of course is expected)

        Debug.Assert(result == Windows.Security.Cryptography.Certificates.ChainValidationResult.Success);
    }
}

Exceptions (if any)

{"Specified argument was out of the range of valid values."} | System.ArgumentOutOfRangeException

Further technical details

C:\Program Files\Microsoft Visual Studio\2022\Professional>dotnet --info
.NET SDK:
Version: 9.0.203
Commit: dc7acfa194
Workload version: 9.0.200-manifests.12d79ccf
MSBuild version: 17.13.20+a4ef1e90f

Runtime Environment:
OS Name: Windows
OS Version: 10.0.26100
OS Platform: Windows
RID: win-x64
Base Path: C:\Program Files\dotnet\sdk\9.0.203\

.NET workloads installed:
There are no installed workloads to display.
Configured to use loose manifests when installing new manifests.

Host:
Version: 9.0.4
Architecture: x64
Commit: f57e6dc747

.NET SDKs installed:
9.0.203 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 9.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 9.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.36 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 7.0.20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 8.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 9.0.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
x86 [C:\Program Files (x86)\dotnet]
registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
Not set

global.json file:
Not found

Learn more:
https://aka.ms/dotnet/info

Download .NET:
https://aka.ms/dotnet/download

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.