Recursive AI

Validate an X.509 certificate chain in C#

OneCrypto is a free, open-source cryptography library for .NET from Recursive AI. MIT or Apache-2.0, preview, no native dependencies.

A certificate chain connects a certificate to a root you explicitly trust. This example uses local test certificates and a fixed validation time.

Terminal
dotnet add package RecursiveAI.OneCrypto

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

leaf.der and root.der are test certificates in the download.

C#
using RecursiveAI.OneCrypto;
using RecursiveAI.OneCrypto.X509;
using RecursiveAI.OneCrypto.Validation;
 
var leaf = Certificate.Parse(
    File.ReadAllBytes("leaf.der").AsMemory());
var root = Certificate.Parse(
    File.ReadAllBytes("root.der").AsMemory());
var time = DateTimeOffset.Parse(
    "2026-06-01T12:00:00Z");
var report = PathValidator.Validate(leaf,
    new ValidationOptions
{
    TrustAnchors =
        [TrustAnchor.FromCertificate(root)],
    ReferenceTime = time
});
Console.WriteLine(
    $"With anchor: {report.Verdict}");
Text
With anchor: Valid

Unzip the example, then dotnet run.

The full program

The download contains a code-signing leaf named CN=Cookbook test leaf, issued directly by CN=Cookbook test root. There are no intermediate certificates. The leaf is valid from May 31 through June 30, 2026; the reference time is June 1, 2026 at noon UTC. These dates let the example run after the test certificate expires.

C#
using RecursiveAI.OneCrypto;
using RecursiveAI.OneCrypto.X509;
using RecursiveAI.OneCrypto.Validation;
 
var leaf = Certificate.Parse(File.ReadAllBytes("leaf.der").AsMemory());
var root = Certificate.Parse(File.ReadAllBytes("root.der").AsMemory());
var time = new DateTimeOffset(2026, 6, 1, 12, 0, 0, TimeSpan.Zero);
var report = PathValidator.Validate(leaf, new ValidationOptions
{
    TrustAnchors = [TrustAnchor.FromCertificate(root)],
    ReferenceTime = time
});
Console.WriteLine($"With anchor: {report.Verdict}");
var untrusted = PathValidator.Validate(leaf, new ValidationOptions
{
    ReferenceTime = time
});
Console.WriteLine($"Without anchor: {untrusted.Verdict}");
Text
With anchor: Valid
Without anchor: Invalid

A missing trust path produces an Invalid report, not an exception; a work limit can instead produce Indeterminate.

Variations

After the full program, move the reference time past the leaf’s validity:

C#
var expired = PathValidator.Validate(leaf, new ValidationOptions
{
    TrustAnchors = [TrustAnchor.FromCertificate(root)],
    ReferenceTime = time.AddDays(60)
});
Console.WriteLine($"After expiry: {expired.Verdict}");
Text
After expiry: Invalid

Require revocation information for the leaf. No CRL or OCSP response is supplied here:

C#
var revocation = PathValidator.Validate(leaf, new ValidationOptions
{
    TrustAnchors = [TrustAnchor.FromCertificate(root)],
    ReferenceTime = time,
    RevocationRequired = RevocationScope.EndEntity
});
Console.WriteLine($"Revocation required: {revocation.Verdict}");
Text
Revocation required: Indeterminate

For a leaf issued through an intermediate CA, replace Program.cs with this example. The download includes chain-leaf.der (CN=Cookbook chain leaf), intermediate.der (CN=Cookbook intermediate) and chain-root.der (CN=Cookbook chain root), all valid at the reference time. Only the root is trusted; the intermediate is a path-building input.

C#
using RecursiveAI.OneCrypto;
using RecursiveAI.OneCrypto.X509;
using RecursiveAI.OneCrypto.Validation;
 
var leaf = Certificate.Parse(
    File.ReadAllBytes("chain-leaf.der").AsMemory());
var intermediate = Certificate.Parse(
    File.ReadAllBytes("intermediate.der").AsMemory());
var root = Certificate.Parse(
    File.ReadAllBytes("chain-root.der").AsMemory());
var report = PathValidator.Validate(leaf,
    new ValidationOptions
    {
        TrustAnchors = [TrustAnchor.FromCertificate(root)],
        Intermediates = [intermediate],
        ReferenceTime = DateTimeOffset.Parse(
            "2026-06-01T12:00:00Z")
    });
Console.WriteLine($"With intermediate: {report.Verdict}");
Text
With intermediate: Valid

The short answer

Call PathValidator.Validate with the target certificate, an explicit TrustAnchors collection, and a ReferenceTime. Supply intermediate certificates separately through Intermediates if the issuer is not the trusted root itself. The result is a report with a verdict and rule findings, not merely a signature-check Boolean.

The path-validation documentation defines the operation as a pure function of its inputs. The synchronous call neither fetches missing certificates nor consults the operating system’s chain engine. This makes the recipe suitable for an application that needs to decide exactly which roots, certificates, revocation data and date take part in validation.

.NET’s X509Chain.Build builds a chain using its X509ChainPolicy; OneCrypto’s PathValidator.Validate builds and validates a path using explicit inputs and a reference time, without consulting the operating system’s chain engine.

How do I check if my certificate chain is valid?

Start with the leaf you intend to accept, not an arbitrary certificate from the received bundle. Decide which root certificates your application trusts, then give the builder any untrusted intermediate certificates needed to connect that leaf to a root. A certificate in Intermediates can help build a path but cannot confer trust.

Choose the reference time deliberately. The sample uses a fixed date because its local certificates are short-lived demonstration inputs. A live acceptance decision needs a time appropriate to that decision, not the demonstration date copied from this page. The validator applies validity and algorithm-policy checks at the reference time.

Inspect Verdict, and use the report’s rule findings to explain a failure. A result of Indeterminate is not a successful check: it means the inputs or permitted work did not establish the answer. For example, asking for leaf revocation status without supplying revocation evidence produces Indeterminate in this sample. Leaving revocation unrequired yields Valid for the anchored chain, but does not establish that its leaf has not been revoked.

How are X509 certificates validated?

Path building first finds candidate issuers by name and signature. The validator then applies the certificate-path rules, including signature verification, validity, CA basic constraints, key usage, name constraints and certificate policies. The default cryptographic policy also evaluates certificate-signature algorithms. These checks are described individually in the path-validation docs, including how the results identify a rule, outcome, reason and standards clause.

The trust anchor is an input to this process. A valid self-signature on a root is not a reason for an application to trust it. Nor does checking only the leaf’s signature establish that every issuer in a longer chain is allowed to issue it.

This recipe checks certificate paths, not a complete HTTPS connection. Application-specific acceptance rules still matter. If your application needs a particular extended key usage, set that requirement rather than accepting a chain merely because it can be built. The sample leaf is for code signing; it is not a demonstration of matching a web server’s hostname.

How do you fix a self-signed certificate in a chain?

First determine whether the self-signed certificate is a root your application is supposed to trust. If so, configure it as an explicit trust anchor through a trusted distribution path. Do not automatically trust a root just because a peer sent it alongside a leaf.

The full program demonstrates the distinction without changing the certificate bytes. With the test root in TrustAnchors, the result is Valid; without it, the result is Invalid. Adding a root only to an untrusted certificate pool would not make it an anchor.

If the root is already trusted, inspect the failure rather than disabling validation. An expired leaf, a missing intermediate or an invalid signature is a different problem. Suppressing all checks to make a self-signed-chain error disappear removes the very decision the chain validator is meant to make.

All onecrypto recipes