Description
Currently AsnWriter
offers couple of methods for writing integers represented using ReadOnlySpan<byte>
:
public void WriteInteger(ReadOnlySpan<byte> value);
public void WriteIntegerUnsigned(ReadOnlySpan<byte> value);
public void WriteInteger(Asn1Tag tag, ReadOnlySpan<byte> value);
public void WriteIntegerUnsigned(Asn1Tag tag, ReadOnlySpan<byte> value);
These methods throw CryptographicException
when the value
is not properly padded, ie. the integer starts with a zero, followed by a byte < 0x80.
Nearly all of the usages of these methods use extra code to strip this padding. Examples include:
I found no usages of WriteInteger[Unsigned]
that rely on the padding check except for the unit tests that check that specific behavior.
I propose that these WriteInteger[Unsigned]
overloads should strip the padding instead of throwing an exception on it. Alternatively, if there's a compelling reason to keep the current API with the checks, offer a different API that does the normalization.
Furthermore, the WriteIntegerUnsigned
overloads above are not exposed from AsnSerializer
. If the normalization is enabled in some way it should be available for the serializer as well.
Of the three normalization code snippets above two of them had bugs, which were spotted only after careful review (one of them landed in master, the other was spotted during pull request review). This suggests that it's a bit of code that's prone to be written incorrectly and a good API should ideally prevent the users from writing incorrect code.
/cc @bartonjs