Skip to content

X509Certificate2Collection.ImportFromPem silently drops a certificate preceded by -----BEGIN  #135845

Closed

Description

@pawlos

Description

Since 10.0.13 and 11.0 RC 2, X509Certificate2Collection.ImportFromPem leaves out a certificate when the text -----BEGIN comes directly before it on the same line. The other certificates are imported and no exception is thrown. Earlier versions of .NET imported every certificate from the same input.

Reproduction Steps

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

string bundle = "";
foreach (string name in new[] { "CN=cert1", "CN=cert2", "CN=cert3" })
{
    using var key = ECDsa.Create(ECCurve.NamedCurves.nistP256);
    using var cert = new CertificateRequest(name, key, HashAlgorithmName.SHA256)
        .CreateSelfSigned(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(1));
    bundle += (name == "CN=cert2" ? "-----BEGIN " : "") + cert.ExportCertificatePem() + "\n";
}

var collection = new X509Certificate2Collection();
collection.ImportFromPem(bundle);
Console.WriteLine($"{collection.Count}: {string.Join(", ", collection.Select(c => c.Subject))}");

Expected behavior

All 3 certificates are imported as it was done in previous releases.

Actual behavior

Certificate that is preceded by an stray -----BEGIN is skipped.

dotnet exec --fx-version 10.0.13 bin/release/net10.0/repro.dll
2: CN=cert1, CN=cert3

Regression?

Yes.

Running it under:
.NET 10.0.12

dotnet exec --fx-version 10.0.12 bin/release/net10.0/repro.dll
3: CN=cert1, CN=cert2, CN=cert3

.NET 10.0.13

dotnet exec --fx-version 10.0.13 bin/release/net10.0/repro.dll
2: CN=cert1, CN=cert3

.NET 11 RC1

dotnet exec --fx-version 11.0.0-rc.1.26425.128 bin/release/net10.0/repro.dll
3: CN=cert1, CN=cert2, CN=cert3

.NET 11 RC2

dotnet exec --fx-version 11.0.0-rc.2.26504.105 bin/release/net10.0/repro.dll
2: CN=cert1, CN=cert3

Known Workarounds

Insert a line break before every -----BEGIN before importing.

bundle.Replace("-----BEGIN ", "\n-----BEGIN ")

Configuration

  • .NET 10.0.13/. NET 11.0 RC 2
  • linux x64.
  • No configuration specific

Other information

It appears that issue is inside PemEncoding.TryFind, which now skips a PEM that follows -----BEGIN that was done in Pr #133137

I've tried to check if this behavioral change is documented but could not find it.

Activity

  1. added
    untriaged
    on Oct 15, 2026
  2. dotnet-policy-service commented on Oct 15, 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.

  3. bartonjs commented on Oct 15, 2026

    @bartonjs
    Member

    All of the PEM tests have "-----BEGIN" either as the first characters in the payload, or preceded by a newline. I just scoured RFC 7468 looking for that being a requirement, but it isn't (it's a bit implied by Textual encoding begins with a line comprising "-----BEGIN ", a label, and "-----", suggesting that's the entirety of the line, but the rest of the prose is then a bit more loosey-goosey).

    I'm guessing the fix is just

                    if (!pemData.Slice(nextHyphen).StartsWith(PostEBPrefix))
                    {
    -                   preebEndIndex = nextHyphen;
    +                   preebEndIndex = nextHyphen - 1;
                        goto NextAfterLabel;
                    }

    (and a test), but I'm not currently in a place where I can try that.

  4. added this to the 10.0.x milestone on Oct 15, 2026
  5. removed
    untriaged
    on Oct 15, 2026
  6. locked and limited conversation to collaborators on Nov 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions