Skip to content

[API Proposal]: Obsolete AsnEncodedData RawData setter #129544

Closed

Description

@vcsjones

Background and motivation

AsnEncodedData.RawData has a set on the property. Unfortunately for the majority of things derived from AsnEncodedData, it's broken and can't easily be fixed. This was highlighted in #81844.

For example:

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

X509BasicConstraintsExtension extension = new()
{
    RawData = new byte[] { 0x30, 0x03, 0x01, 0x01, 0xFF }
};

Console.WriteLine(extension.CertificateAuthority);

Will print False. This is wrong - the ASN.1 encoded data indicates that the basicConstraints should be a CA.

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

X509BasicConstraintsExtension extension = new(new AsnEncodedData([0x30, 0x03, 0x01, 0x01, 0xFF]), true);
Console.WriteLine(extension.CertificateAuthority);

Or consider:

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

X509BasicConstraintsExtension ext = new(new AsnEncodedData([0x30, 0x03, 0x01, 0x01, 0xFF]), true);
Console.WriteLine(ext.CertificateAuthority);
ext.RawData = [0x30, 0x03, 0x01, 0x01, 0x00];
Console.WriteLine(ext.CertificateAuthority);

This prints True, True, but it should print True, False.

The core of the issue here is that most extension types memoize their decoded representation. They cannot react to RawData changing because RawData's setter is not virtual, so they cannot invalidate their cache easily.

It's not just X509BasicConstraintsExtension that has this problem.

•  X500DistinguishedName 
•  X509BasicConstraintsExtension 
•  X509EnhancedKeyUsageExtension 
•  X509KeyUsageExtension 
•  X509SubjectKeyIdentifierExtension 
•  X509SubjectAlternativeNameExtension 
•  X509AuthorityKeyIdentifierExtension 
•  X509AuthorityInformationAccessExtension 
•  Pkcs9ContentType 
•  Pkcs9DocumentDescription 
•  Pkcs9DocumentName 
•  Pkcs9MessageDigest 
•  Pkcs9SigningTime 
•  Pkcs9LocalKeyId 

All have this problem and there is no easy way for this to fix this, other than to not memoize anything at all.


CopyFrom can be used to mutate the instance if someone really wants that behavior. It does require passing in an AsnEncodedData to wrap the bytes, but this is virtual, so derived types can react to it appropriately.

API Proposal

namespace System.Security.Cryptography

public partial class AsnEncodedData {
    public byte[] RawData {
        get;
+       [Obsolete("Setting RawData is obsolete. Use CopyFrom instead.", DiagnosticId = "SYSLIB1234", ...)]
        set;
    }
}

API Usage

No usage other than "don't use it"

Alternative Designs

Make RawData virtual. This is, technically, a breaking change

Adding virtual to a member

While this change would often work without breaking too many scenarios because C# compiler tends to emit callvirt IL instructions to call non-virtual methods (callvirt performs a null check, while a normal call won't), we can't rely on it. C# is not the only language we target and the C# compiler increasingly tries to optimize callvirt to a normal call whenever the target method is non-virtual and the this is provably not null

Risks

The usual concerns about obsoleting a member.

Activity

  1. added
    api-suggestion
    on Jun 17, 2026
  2. added
    untriaged
    on Jun 17, 2026
  3. dotnet-policy-service commented on Jun 17, 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.

  4. self-assigned this
    on Jun 17, 2026
  5. bartonjs commented on Jun 19, 2026

    @bartonjs
    Member

    It looks like most types in the hierarchy don't even set their RawData value, they just build them in a ctor call and call base with the right answer a priori.

    So, yeah, makes sense.

  6. removed
    api-suggestion
    untriaged
    on Jun 19, 2026
  7. added this to the 11.0.0 milestone on Jun 19, 2026
  8. added
    api-ready-for-review
    on Jun 19, 2026
  9. vcsjones commented on Jun 22, 2026

    @vcsjones
    MemberAuthor

    For reference, nuget use of the setter basically rounds to zero.

    Image
  10. bartonjs commented on Jun 23, 2026

    @bartonjs
    Member

    Video

    • Obsoleting it seems appropriate.
    • Use the next available SYSLIB value
    namespace System.Security.Cryptography
    
    public partial class AsnEncodedData {
        public byte[] RawData {
            get;
    +       [Obsolete("Setting RawData is obsolete. Use CopyFrom instead.", DiagnosticId = "SYSLIBxxxx", ...)]
            set;
        }
    }
  11. added
    api-approved
    and removed
    api-ready-for-review
    on Jun 23, 2026
  12. ladeak commented on Jun 23, 2026

    @ladeak
    Contributor

    Given the motivation, have you consider marking the complete RawData property as obsolete and adding a new property, something like public ReadOnlySpan<byte> RawDataSpan { get; } to address the modification concern that could happen through the current getter (because it does not make a copy)?

  13. vcsjones commented on Jun 23, 2026

    @vcsjones
    MemberAuthor

    My 2c: I don't think it's worth the noise that will create. There are unfortunately a good number of APIs that do not always return defensive copies, and it is slightly more justifiable that you shouldn't mutate the array (mutating ASN.1 data in-place is already a somewhat odd scenario).

  14. locked and limited conversation to collaborators on Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Security

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions