Skip to content

X509Chain.Build throws "MapOpenSsl30Code reported unhandled error code" on OpenSSL 3.0.0-3.0.5 when the trust anchor is rejected as an issuer #137098

Description

@caraioniurie47

Description

On Linux with OpenSSL 3.0.0 through 3.0.5, which includes Ubuntu 22.04's libssl3, X509Chain.Build throws CryptographicException: OpenSSL mapping function MapOpenSsl30Code reported unhandled error code '1'. (or '30') for the two chains below, whose custom trust anchor OpenSSL rejects as the issuer of the next certificate. With OpenSSL 3.0.7 the same chains build to PartialChain.

Two ways the trust anchor gets rejected, both in the program below:

  • its extended key usage extension doesn't decode: error 1, X509_V_ERR_UNSPECIFIED. The CorruptPoliciesChainTests.CorruptEku*(level: 4) cases fail with Ubuntu 22.04's libssl3.
  • its subject key identifier doesn't match the intermediate's authority key identifier (for example a re-keyed root with the same name): error 30, X509_V_ERR_AKID_SKID_MISMATCH.

In OpenSSL 3.0.0-3.0.5 the default check_issued records why it rejected a candidate issuer as the verify context's error, and when no issuer is accepted, chain building reports that recorded error as final instead of X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY (20). OpenSSL removed this in openssl/openssl@a58afad (PR openssl/openssl#18758, issue openssl/openssl#18691), first tagged in 3.0.6. .NET maps neither code and throws:

OpenSslX509ChainProcessor.cs:1226-1236

        private static X509ChainStatusFlags MapOpenSsl30Code(Interop.Crypto.X509VerifyStatusCode code)
        {
            switch (code.Code30)
            {
                case Interop.Crypto.X509VerifyStatusCode30.X509_V_ERR_INVALID_CA:
                    return X509ChainStatusFlags.InvalidBasicConstraints;
                default:
                    Debug.Fail("Unrecognized X509VerifyStatusCode:" + code.Code30);
                    throw GetUnmappedCodeException(nameof(MapOpenSsl30Code), (int)code.Code30);
            }
        }

Reproduction Steps

A net10.0 console app (ImplicitUsings and Nullable enabled):

using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

// Trust anchor whose extended key usage extension doesn't decode (truncated DER 30 11).
Build("Unparsable EKU on the trust anchor",
    new X509Extension("2.5.29.37", new byte[] { 0x30, 0x11 }, critical: false),
    intermediateExtension: null);

// Trust anchor whose subject key identifier doesn't match the intermediate's authority key identifier.
Build("Key identifier mismatch at the trust anchor",
    new X509Extension("2.5.29.14", Convert.FromHexString("0414C7AC28EFB300F46F9406ED155628A123633E556F"), critical: false),
    new X509Extension("2.5.29.35", Convert.FromHexString("30168014A84A6A63047DDDBAE6D139B7A64565EFF3A8ECA1"), critical: false));

static void Build(string name, X509Extension rootExtension, X509Extension? intermediateExtension)
{
    using RSA rootKey = RSA.Create(2048);
    using RSA intermediateKey = RSA.Create(2048);
    using RSA leafKey = RSA.Create(2048);
    DateTimeOffset now = DateTimeOffset.UtcNow;

    CertificateRequest rootReq = new("CN=Root", rootKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
    rootReq.CertificateExtensions.Add(X509BasicConstraintsExtension.CreateForCertificateAuthority());
    rootReq.CertificateExtensions.Add(rootExtension);
    using X509Certificate2 root = rootReq.CreateSelfSigned(now.AddDays(-1), now.AddDays(30));

    CertificateRequest intermediateReq = new("CN=Intermediate", intermediateKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
    intermediateReq.CertificateExtensions.Add(X509BasicConstraintsExtension.CreateForCertificateAuthority());

    if (intermediateExtension is not null)
    {
        intermediateReq.CertificateExtensions.Add(intermediateExtension);
    }
    using X509Certificate2 intermediate = intermediateReq.Create(root, now.AddDays(-1), now.AddDays(20), [1]);
    using X509Certificate2 intermediateWithKey = intermediate.CopyWithPrivateKey(intermediateKey);

    CertificateRequest leafReq = new("CN=Leaf", leafKey, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
    leafReq.CertificateExtensions.Add(X509BasicConstraintsExtension.CreateForEndEntity());
    using X509Certificate2 leaf = leafReq.Create(intermediateWithKey, now.AddDays(-1), now.AddDays(10), [2]);

    using X509Chain chain = new();
    chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
    chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
    chain.ChainPolicy.CustomTrustStore.Add(root);
    chain.ChainPolicy.ExtraStore.Add(intermediate);

    try
    {
        bool valid = chain.Build(leaf);
        Console.WriteLine($"{name}: Build returned {valid}, {string.Join(", ", chain.ChainStatus.Select(s => s.Status))}");
    }
    catch (CryptographicException e)
    {
        Console.WriteLine($"{name}: {e.GetType().Name}: {e.Message}");
    }
}

To compare with a later OpenSSL, run it with LD_LIBRARY_PATH pointing at an OpenSSL 3.0.7 build's lib directory.

Expected behavior

Both chains build to PartialChain, as with OpenSSL 3.0.7:

Unparsable EKU on the trust anchor: Build returned False, PartialChain
Key identifier mismatch at the trust anchor: Build returned False, PartialChain

Actual behavior

With Ubuntu 22.04's libssl3 3.0.2-0ubuntu1.30, and with OpenSSL 3.0.5:

Unparsable EKU on the trust anchor: CryptographicException: OpenSSL mapping function MapOpenSsl30Code reported unhandled error code '1'.
Key identifier mismatch at the trust anchor: CryptographicException: OpenSSL mapping function MapOpenSsl30Code reported unhandled error code '30'.

With Debug libraries the Debug.Fail above ends the process instead.

Regression?

With the same .NET runtime, changing OpenSSL from 3.0.5 to 3.0.7 changes both results from exceptions to PartialChain; the OpenSSL fix first appears in 3.0.6.

Known Workarounds

An OpenSSL from 3.0.6 on; Ubuntu 22.04's package is 3.0.2.

Configuration

  • .NET 10.0.12, x64.
  • Ubuntu 22.04.5 LTS on WSL 2, libssl3 3.0.2-0ubuntu1.30; also OpenSSL 3.0.5 and 3.0.7 built from the release tarballs and loaded with LD_LIBRARY_PATH.

Other information

Related history: #67304 hit code 30 on Ubuntu 22.04 in DynamicChainTests.MismatchKeyIdentifiers. #67343, which would have ignored code 30, was closed as possibly "fixing a symptom and not a root cause", and #67350 fixed that test by always committing the chain. In MismatchKeyIdentifiers the mismatching certificate is an untrusted intermediate; when it is the trust anchor, as above, the error still reaches the mapping. On Ubuntu 22.04's 3.0.2, MismatchKeyIdentifiers passes while the program's second chain throws.

I have a fix with a test ready and will open a PR for it shortly. Could this be assigned to me?

Note

AI-generated, written at my direction and reviewed by me before posting. The program above ran on .NET 10.0.12, Ubuntu 22.04 x64, with Ubuntu's libssl3 3.0.2-0ubuntu1.30 and with OpenSSL 3.0.0, 3.0.1, 3.0.3, 3.0.4, 3.0.5 and 3.0.7 built from the release tarballs.

Activity

  1. added
    untriaged
    on Nov 6, 2026
  2. dotnet-policy-service commented on Nov 6, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
    See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Securityuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions