[API Proposal]: Content-validating AsnDecoder.ReadEncodedValue #66622
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-System.Formats.Asn1
Milestone
Background and motivation
Currently, the System.Formats.Asn1 package does not have a straightforward way to say, “Given some
ReadOnlySpan<byte>
, do all of the contents conform to a set of encoding rules?”, such as DER.There is useful where we want to build APIs that accept ASN.1 and follows the encoding rules, but also don’t have context as to what the contents are actually supposed to be.
Developers that wish for this functionality on their own would need to use
AsnDecoder
(orAsnReader
) and peek each tag, read, and advance the reader.We’ve loosely built this ourselves, though not complete:
runtime/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/Helpers.cs
Line 271 in c6829b9
Another example where this would be useful was the
AddEncoded
API that was proposed in #44738.API Proposal
A new overload to
ReadEncodedValue
that accepts abool validateInnerContents
.API Usage
The API usage is similar to
ReadEncodedValue
, with a new parameter that indicates if inner-content validation is performed. Whenfalse
, it behaves exactly asReadEncodedValue
. Whentrue
, it performs document validation according to the encoding rules.Alternative Designs
We could make this an entirely separate API:
After thinking about it, I decided not to make this my initial proposal because I would envision still using
ReadEncodedValue
followed by an immediate call toValidateContents
. Rather than have something that does some work twice (reading the initial tag and such), I went with the overload.Risks
No response
The text was updated successfully, but these errors were encountered: