Skip to content

Breaking change in .NET 9 behavior for the ability to allow duplicate attributes in a PFX #113726

Open
@ppekrol

Description

@ppekrol

Description

We are aware that .NET 9 brought significant number of API (and behavior) changes to the framework. One issue that we are struggling is that some of the certificates that we are dealing with have duplicate attributes.

There was a discussion about this topic here: #103924

It appears that Pkcs12LoaderLimits has a property called AllowDuplicateAttributes, but it is internal. Shouldn't this be public?

We are aware that there is a 'Pkcs12LoaderLimits.DangerousNoLimits', but we do not want to disable everything, just have an option to change this one limit.

At the moment we are using reflection to set it:

        var allowDuplicateAttributes = new Pkcs12LoaderLimits(Pkcs12LoaderLimits.Defaults);
        var allowDuplicateAttributesProperty = allowDuplicateAttributes.GetType().GetProperty("AllowDuplicateAttributes", BindingFlags.Instance | BindingFlags.NonPublic);
        Debug.Assert(allowDuplicateAttributesProperty != null, "AllowDuplicateAttributes != null");
        allowDuplicateAttributesProperty.SetValue(allowDuplicateAttributes, true);
        var certificate = X509CertificateLoader.LoadPkcs12FromFile(Path.Combine(""), password: null, loaderLimits: allowDuplicateAttributes);

Reproduction Steps

var cert = X509CertificateLoader.LoadPkcs12FromFile("pathToCertFileWithDuplicateAttributes");
System.Security.Cryptography.X509Certificates.Pkcs12LoadLimitExceededException
HResult=0x80131501
Message=The PKCS#12/PFX violated the 'AllowDuplicateAttributes' limit.
Source=System.Security.Cryptography
StackTrace:
at System.Security.Cryptography.X509Certificates.X509CertificateLoader.RejectDuplicateAttributes(AttributeAsn[] bagAttributes, HashSet`1 duplicateAttributeCheck)
at System.Security.Cryptography.X509Certificates.X509CertificateLoader.ProcessSafeContents(ReadOnlyMemory`1 contentData, Pkcs12LoaderLimits loaderLimits, Nullable`1& workRemaining, BagState& bagState)
at System.Security.Cryptography.X509Certificates.X509CertificateLoader.ReadCertsAndKeys(BagState& bagState, ReadOnlyMemory`1 data, ReadOnlySpan`1& password, Pkcs12LoaderLimits loaderLimits)
at System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12(ReadOnlyMemory`1 data, ReadOnlySpan`1 password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits)
at System.Security.Cryptography.X509Certificates.X509CertificateLoader.LoadPkcs12(Byte[] data, String password, X509KeyStorageFlags keyStorageFlags, Pkcs12LoaderLimits loaderLimits)

Expected behavior

Have the ability to override default Pkcs12LoaderLimits and set the AllowDuplicateAttributes to desired value

Actual behavior

Exception is thrown, no ability to override it beside using reflection.

Regression?

Was working in .NET 8

Known Workarounds

        var allowDuplicateAttributes = new Pkcs12LoaderLimits(Pkcs12LoaderLimits.Defaults);
        var allowDuplicateAttributesProperty = allowDuplicateAttributes.GetType().GetProperty("AllowDuplicateAttributes", BindingFlags.Instance | BindingFlags.NonPublic);
        Debug.Assert(allowDuplicateAttributesProperty != null, "AllowDuplicateAttributes != null");
        allowDuplicateAttributesProperty.SetValue(allowDuplicateAttributes, true);
        var certificate = X509CertificateLoader.LoadPkcs12FromFile(Path.Combine("pathToCertWithDuplicateAttributes"), password: null, loaderLimits: allowDuplicateAttributes);

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions