Description
Starting in .NET 11 Preview 6, the System.Security.Cryptography.AsnEncodedData's RawData property has had it's set marked obsolete and should no longer be used.
Version
.NET 11 Preview 6
Previous behavior
AsnEncodedData.RawData's set could be used without a compilation warning.
New behavior
AsnEncodedData.RawData's set is now marked as obsolete. Using this property setter now generates a compiler warning, SYSLIB0065.
Type of breaking change
Reason for change
AsnEncodedData is a type that represents an ASN.1 encoded object and contains the underlying ASN.1 representation of the encoded object. Many derived types exist from this type, such as X509BasicConstraintsExtension. These types accept the ASN.1 encoding as constructor arguments and decode it so that the derived type can provide information about the encoded data. These types will cache the decoded representation of that ASN.1 so that accessing properties does not repeatedly decode the ASN.1.
However, because RawData has a set, this can cause a discrepancy between the decoded representation and the data it contains. Consider this example:
using System;
using System.Security.Cryptography.X509Certificates;
X509BasicConstraintsExtension extension = new(
certificateAuthority: true,
hasPathLengthConstraint: true,
pathLengthConstraint: 3,
critical: true);
X509BasicConstraintsExtension decoded = new();
decoded.RawData = extension.RawData;
Console.WriteLine(decoded.CertificateAuthority);
Console.WriteLine(decoded.HasPathLengthConstraint);
Console.WriteLine(decoded.PathLengthConstraint);
This will unexpectedly print
This happens because RawData is not virtual, so the derived type cannot properly react to the underlying RawData changing.
In most circumstances, any type that is derived from AsnEncodedData will not be able to correctly invalidate its decoded state when the property is set.
Recommended action
Using the constructor of AsnEncodedData, or its derived types, to decode data is the best way to ensure coherency between the decoded state and the data it represents. Instances should best be treated as read only - instead of reusing instances, construct a new instance of the type.
If mutable behavior is required, use AsnEncodedData.CopyFrom. CopyFrom is virtual, so derived types can invalidate their decoded state in the presence of decoded data.
Feature area
Core .NET libraries
Affected APIs
- P:System.Security.Cryptography.AsnEncodedData.RawData
set only
Description
Starting in .NET 11 Preview 6, the
System.Security.Cryptography.AsnEncodedData'sRawDataproperty has had it'ssetmarked obsolete and should no longer be used.Version
.NET 11 Preview 6
Previous behavior
AsnEncodedData.RawData'ssetcould be used without a compilation warning.New behavior
AsnEncodedData.RawData'ssetis now marked as obsolete. Using this property setter now generates a compiler warning,SYSLIB0065.Type of breaking change
Reason for change
AsnEncodedDatais a type that represents an ASN.1 encoded object and contains the underlying ASN.1 representation of the encoded object. Many derived types exist from this type, such asX509BasicConstraintsExtension. These types accept the ASN.1 encoding as constructor arguments and decode it so that the derived type can provide information about the encoded data. These types will cache the decoded representation of that ASN.1 so that accessing properties does not repeatedly decode the ASN.1.However, because
RawDatahas aset, this can cause a discrepancy between the decoded representation and the data it contains. Consider this example:This will unexpectedly print
This happens because
RawDatais not virtual, so the derived type cannot properly react to the underlyingRawDatachanging.In most circumstances, any type that is derived from
AsnEncodedDatawill not be able to correctly invalidate its decoded state when the property is set.Recommended action
Using the constructor of
AsnEncodedData, or its derived types, to decode data is the best way to ensure coherency between the decoded state and the data it represents. Instances should best be treated as read only - instead of reusing instances, construct a new instance of the type.If mutable behavior is required, use
AsnEncodedData.CopyFrom.CopyFromisvirtual, so derived types can invalidate their decoded state in the presence of decoded data.Feature area
Core .NET libraries
Affected APIs
setonly