Repository navigation
Description
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
dotnet-policy-service commentedon Jun 17, 2026
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security
See info in area-owners.md if you want to be subscribed.
bartonjs commentedon Jun 19, 2026
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.
vcsjones commentedon Jun 22, 2026
For reference, nuget use of the setter basically rounds to zero.
bartonjs commentedon Jun 23, 2026
Video
namespace System.Security.Cryptography public partial class AsnEncodedData { public byte[] RawData { get; + [Obsolete("Setting RawData is obsolete. Use CopyFrom instead.", DiagnosticId = "SYSLIBxxxx", ...)] set; } }ladeak commentedon Jun 23, 2026
Given the motivation, have you consider marking the complete
RawDataproperty as obsolete and adding a new property, something likepublic ReadOnlySpan<byte> RawDataSpan { get; }to address the modification concern that could happen through the current getter (because it does not make a copy)?vcsjones commentedon Jun 23, 2026
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).