Recursive AI

Verify certificate trust for a CMS signature in C#

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

Check a detached signature and its signer’s chain. The download includes release.p7s, release.txt, and root.pem.

Terminal
dotnet add package RecursiveAI.OneCrypto --prerelease

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

C#
using RecursiveAI.OneCrypto;
using RecursiveAI.OneCrypto.Validation;
 
using var root = Crypto.Load("root.pem");
var anchor = TrustAnchor.FromCertificate(
    root.Certificates[0]);
var cms = SignedData.Parse(
    File.ReadAllBytes("release.p7s"));
var data = File.ReadAllBytes("release.txt");
var chain = new ValidationOptions
{
    TrustAnchors = [anchor],
    ReferenceTime = DateTimeOffset.Parse(
        "2026-12-01T00:00:00Z")
};
var options = new SignedDataVerifyOptions
    { Chain = chain };
Console.WriteLine(
    cms.Verify(data, options).IsValid);

Unzip the example, then dotnet run.

Checks the file and the signer’s chain against the demo root:

Text
True

The full program

The sample is offline. release.p7s is a binary DER file containing a detached CMS signature and the demo signer certificate. The signer was issued directly by the demo root in root.pem. Only public material is needed to verify. The example uses a fixed date within both certificates’ validity periods and does not check revocation.

release.txt contains this line followed by a newline:

Text
release 1.4.2

root.pem is the separate trust input, a PEM certificate named Cookbook Demo Root. The complete root.pem is in the example download (.zip):

Text
-----BEGIN CERTIFICATE-----
…
-----END CERTIFICATE-----
C#
using RecursiveAI.OneCrypto;
using RecursiveAI.OneCrypto.Validation;
 
using var roots = Crypto.Load("root.pem");
var cms = SignedData.Parse(File.ReadAllBytes("release.p7s"));
byte[] data = File.ReadAllBytes("release.txt");
var chain = new ValidationOptions
{
    TrustAnchors = [TrustAnchor.FromCertificate(roots.Certificates[0])],
    ReferenceTime = new DateTimeOffset(2026, 12, 1, 0, 0, 0, TimeSpan.Zero)
};
 
Console.WriteLine($"Signature only: {cms.Verify(data).IsValid}");
var trusted = cms.Verify(data, new SignedDataVerifyOptions { Chain = chain });
Console.WriteLine($"With demo root: {trusted.IsValid}");
var untrusted = cms.Verify(data, new SignedDataVerifyOptions
{
    Chain = new ValidationOptions { ReferenceTime = chain.ReferenceTime }
});
Console.WriteLine($"Without demo root: {untrusted.IsValid}");
Text
Signature only: True
With demo root: True
Without demo root: False

If the signature is sound but the signer has no path to a configured anchor, the chain check fails; inspect SignerReport.Chain for the validator’s report. The path validator is a preview and this example is for testing with a demo root, not making production trust decisions.

Variations

Inspect the chain check. Add this after trusted is assigned in the full program:

C#
foreach (var check in trusted.Signers[0].Checks)
    if (check.Rule == "chain")
        Console.WriteLine(check.Status);
Text
Passed

The short answer

Pass SignedDataVerifyOptions.Chain when you call Verify with the original file bytes. Set TrustAnchors to roots supplied by your application and ReferenceTime to the time at which you want to validate the path. Without Chain, IsValid doesn’t tell you whether the signer chains to a trusted root.

How do I verify a digital signature certificate in C#?

A CMS (Cryptographic Message Syntax) signature can include the signer’s certificate. OneCrypto can use that certificate’s public key to verify the signature, but a certificate carried by the message is not trusted just because it’s present. The CMS verifier adds certificates from the message to the untrusted pool of possible issuers. The path must end at an anchor you chose separately. Don’t copy a root out of an incoming signature into your trust list merely to make verification pass.

The example does three checks against the same signature and exact file bytes. The first verifies without a chain. The second supplies the expected root independently. The third still requests path validation but omits that root. The False in the third check is a trust failure, not evidence that the file changed. A successful chain still doesn’t authorize a signer for a particular action; your application decides which signers may publish the files it accepts.

Does verifying a certificate chain check revocation?

Not in this offline example: it supplies neither a certificate revocation list (CRL) nor an Online Certificate Status Protocol (OCSP) response. When your policy requires revocation data, the path validator can report that it could not decide if usable data is missing. A warning for unknown revocation status can still leave the CMS result valid, so inspect the chain report instead of treating IsValid as proof that revocation was checked.

The verifier uses the reference time you give it, not the signing time written by the signer. To validate a historical signature, use an independently proven time, such as one from a verified timestamp, and supply the revocation data your policy requires for that time.

All onecrypto recipes