Skip to content

[BUG] PFX generated via bouncycastle Pkcs12Store are not loading in .Net 9 #605

@hiddenshadow21

Description

@hiddenshadow21

Describe the Bug

Client certificate generated when creating whole chain (CA, server, and client certificates) is not loading. It fails
AllowDuplicates check added in .Net 9 (check discussion about it here).

As a last step of generating the client pfx we are attaching issuer certificate to have correct chain using bouncyCastle Pkcs12Store as shown below:

        Pkcs12Store store = new Pkcs12StoreBuilder().Build();
        var serverCert = DotNetUtilities.FromX509Certificate(certificateHolder.Certificate);

        store.Load(new MemoryStream(certBytes), Array.Empty<char>());
        store.SetCertificateEntry(serverCert.SubjectDN.ToString(), new X509CertificateEntry(serverCert));

        var memoryStream = new MemoryStream();
        store.Save(memoryStream, Array.Empty<char>(), GetSeededSecureRandom());

While debugging we found that loading our client.pfx fails duplicate check for OID 2.16.840.1.113894.746875.1.1. This one is added automatically, under the hood, when saving PKCS12 store with certificate that does not have private key attached and has an EKU (see source code here).

Are we misusing the BouncyCastle API in this scenario, or is there a recommended way to prevent this OID duplication?
Is bouncycastle not compatible with .Net 9?

To Reproduce

Steps to reproduce the behavior:

  1. Download uploaded app
  2. Run scenario 3.

Expected Behavior

The PFX should load successfully without triggering the duplicate attribute check.

Screenshots and Logs

Image

Desktop

  • OS: Windows 11

Additional Context

There is issue in .Net 9 regarding this problem - dotnet/runtime#113726

In .NET, a workaround was introduced via the internal property Pkcs12LoaderLimits.AllowDuplicateAttributes, but it is not publicly accessible.

Sample .Net 9 console app:
PfxLimitsNet9.zip

There are 3 scenarios in the app:

  1. Self-signed certificates - working correctly
  2. Creating CA and server certificate (without including any in the chain) - loading these certificates is working correctly as well.
  3. Creating whole chain with client cert containing server cert in the pfx chain - not working

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions