Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
class System.​Formats.​Asn1.​AsnReader
Assembly: System.Formats.Asn1
Inheritance: object → AsnReader
Represents a stateful, forward-only reader for BER-encoded, CER-encoded, or DER-encoded ASN.1 data.
Properties
public
bool
HasData
Gets a value that indicates whether the reader has remaining data available to process.
public
AsnEncodingRules
RuleSet
Gets the encoding rules in use by this reader.
Methods
Returns A clone of the current reader.
Remarks
This does not create a clone of the ASN.1 data, only the reader's state is cloned.
public
ReadOnlyMemory<​byte>
PeekContentBytes​()
Gets a <see cref="T:System.ReadOnlyMemory`1" /> view of the content octets (bytes) of the
next encoded value without advancing the reader.
Returns The bytes of the contents octets of the next encoded value.
public
ReadOnlyMemory<​byte>
PeekEncodedValue​()
Gets a <see cref="T:System.ReadOnlyMemory`1" /> view of the next encoded value without
advancing the reader. For indefinite length encodings, this includes the
End of Contents marker.
Returns The bytes of the next encoded value.
public
Asn1Tag
PeekTag​()
Reads the encoded tag at the next data position, without advancing the reader.
Returns The decoded tag value.
public
bool
ReadBoolean​(Asn1Tag? expectedTag = null)
Reads the next value as a Boolean with a specified tag.
Returns The decoded value.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 1).
public
string
ReadCharacterString​(UniversalTagNumber encodingType,
Asn1Tag? expectedTag = null)
Reads the next value as character string with the specified tag and
encoding type, returning the decoded value as a string.
Returns The decoded value.
encodingType
One of the enumeration values representing the value type to process.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the universal tag that is
appropriate to the requested encoding type.
public
ReadOnlyMemory<​byte>
ReadEncodedValue​()
Get a <see cref="T:System.ReadOnlyMemory`1" /> view of the next encoded value,
and advance the reader past it. For an indefinite length encoding, this includes
the End of Contents marker.
Returns A <see cref="T:System.ReadOnlyMemory`1" /> view of the next encoded value.
public
ReadOnlyMemory<​byte>
ReadEnumeratedBytes​(Asn1Tag? expectedTag = null)
Reads the next value as a Enumerated with a specified tag, returning the contents
as a <see cref="T:System.ReadOnlyMemory`1" /> over the original data.
Returns The bytes of the Enumerated value, in signed big-endian form.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 10).
public
Enum
ReadEnumeratedValue​(Type enumType,
Asn1Tag? expectedTag = null)
Reads the next value as an Enumerated with a specified tag, converting it to the
non-[ <see cref="T:System.FlagsAttribute" /> ] enum specified by <paramref name="enumType" /> .
Returns The Enumerated value converted to a <paramref name="enumType" /> .
enumType
Type object representing the destination type.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 10).
Remarks
This method does not validate that the return value is defined within
<paramref name="enumType" /> .
public
TEnum
ReadEnumeratedValue​(Asn1Tag? expectedTag = null)
Reads the next value as an Enumerated with a specified tag, converting it to the
non-[ <see cref="T:System.FlagsAttribute" /> ] enum specified by <typeparamref name="TEnum" /> .
Returns The Enumerated value converted to a <typeparamref name="TEnum" /> .
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 10).
Remarks
This method does not validate that the return value is defined within
<typeparamref name="TEnum" /> .
public
DateTimeOffset
ReadGeneralizedTime​(Asn1Tag? expectedTag = null)
Reads the next value as a GeneralizedTime with a specified tag.
Returns The decoded value.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 24).
ReadInteger​(Asn1Tag? expectedTag = null)
Reads the next value as an Integer with a specified tag.
Returns The decoded value.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 2).
public
ReadOnlyMemory<​byte>
ReadIntegerBytes​(Asn1Tag? expectedTag = null)
Reads the next value as a Integer with a specified tag, returning the contents
as a <see cref="T:System.ReadOnlyMemory`1" /> over the original data.
Returns The bytes of the Integer value, in signed big-endian form.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 2).
ReadNamedBitList​(Asn1Tag? expectedTag = null)
Reads the next value as a NamedBitList with a specified tag.
Returns The bits from the encoded value.
expectedTag
The tag to check for before reading.
public
Enum
ReadNamedBitListValue​(Type flagsEnumType,
Asn1Tag? expectedTag = null)
Reads the next value as a NamedBitList with a specified tag, converting it to the
[ <see cref="T:System.FlagsAttribute" /> ] enum specified by <paramref name="flagsEnumType" /> .
Returns The NamedBitList value converted to a <paramref name="flagsEnumType" /> .
flagsEnumType
Type object representing the destination type.
expectedTag
The tag to check for before reading.
public
TFlagsEnum
ReadNamedBitListValue​(Asn1Tag? expectedTag = null)
Reads the next value as a NamedBitList with a specified tag, converting it to the
[ <see cref="T:System.FlagsAttribute" /> ] enum specified by <typeparamref name="TFlagsEnum" /> .
Returns The NamedBitList value converted to a <typeparamref name="TFlagsEnum" /> .
expectedTag
The tag to check for before reading.
Remarks
The bit alignment performed by this method is to interpret the most significant bit
in the first byte of the value as the least significant bit in <typeparamref name="TFlagsEnum" /> ,
with bits increasing in value until the least significant bit of the first byte, proceeding
with the most significant bit of the second byte, and so on. Under this scheme, the following
ASN.1 type declaration and C# enumeration can be used together:
<code>
KeyUsage ::= BIT STRING {
digitalSignature (0),
nonRepudiation (1),
keyEncipherment (2),
dataEncipherment (3),
keyAgreement (4),
keyCertSign (5),
cRLSign (6),
encipherOnly (7),
decipherOnly (8) }
</code> <code>
[Flags]
enum KeyUsage
{
None = 0,
DigitalSignature = 1 << (0),
NonRepudiation = 1 << (1),
KeyEncipherment = 1 << (2),
DataEncipherment = 1 << (3),
KeyAgreement = 1 << (4),
KeyCertSign = 1 << (5),
CrlSign = 1 << (6),
EncipherOnly = 1 << (7),
DecipherOnly = 1 << (8),
}
</code>
While the example here uses the KeyUsage NamedBitList from
<a href="https://tools.ietf.org/html/rfc3280#section-4.2.1.3">RFC 3280 (4.2.1.3)</a> ,
the example enum uses values that are different from
System.Security.Cryptography.X509Certificates.X509KeyUsageFlags.
public
void
ReadNull​(Asn1Tag? expectedTag = null)
Reads the next value as a NULL with a specified tag.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 5).
public
string
ReadObjectIdentifier​(Asn1Tag? expectedTag = null)
Reads the next value as an OBJECT IDENTIFIER with a specified tag, returning
the value in a dotted decimal format string.
Returns The decoded object identifier in the dotted-decimal notation.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 6).
public
byte[]
ReadOctetString​(Asn1Tag? expectedTag = null)
Reads the next value as an OCTET STRING with tag UNIVERSAL 4, returning the value
in a byte array.
Returns A copy of the value in a newly allocated, precisely sized, array.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 4).
public
AsnReader
ReadSequence​(Asn1Tag? expectedTag = null)
Reads the next value as a SEQUENCE or SEQUENCE-OF with the specified tag
and returns the result as a new reader positioned at the first
value in the sequence (or with <see cref="P:System.Formats.Asn1.AsnReader.HasData" /> == <see langword="false" /> ).
Returns A new reader positioned at the first
value in the sequence (or with <see cref="P:System.Formats.Asn1.AsnReader.HasData" /> == <see langword="false" /> ).
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 16).
Remarks
The nested content is not evaluated by this method, and might contain data
that's not valid under the current encoding rules.
public
AsnReader
ReadSetOf​(bool skipSortOrderValidation,
Asn1Tag? expectedTag = null)
Reads the next value as a SET-OF with the specified tag
and returns the result as a new reader positioned at the first
value in the set-of (or with <see cref="P:System.Formats.Asn1.AsnReader.HasData" /> == <see langword="false" /> ).
Returns A new reader positioned at the first
value in the set-of (or with <see cref="P:System.Formats.Asn1.AsnReader.HasData" /> == <see langword="false" /> ).
skipSortOrderValidation
<see langword="true" /> to always accept the data in the order it is presented,
<see langword="false" /> to verify that the data is sorted correctly when the
encoding rules say sorting was required (CER and DER).
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 17).
Remarks
The nested content is not evaluated by this method (aside from sort order, when
required) and might contain data that's not valid under the current encoding rules.
public
AsnReader
ReadSetOf​(Asn1Tag? expectedTag = null)
Reads the next value as a SET-OF with the specified tag
and returns the result as a new reader positioned at the first
value in the set-of (or with <see cref="P:System.Formats.Asn1.AsnReader.HasData" /> == <see langword="false" /> ),
using the <see cref="P:System.Formats.Asn1.AsnReaderOptions.SkipSetSortOrderVerification" /> value
from the constructor (default <see langword="false" /> ).
Returns A new reader positioned at the first
value in the set-of (or with <see cref="P:System.Formats.Asn1.AsnReader.HasData" /> == <see langword="false" /> ).
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 17).
Remarks
The nested content is not evaluated by this method (aside from sort order, when
required) and might contain data that's not valid under the current encoding rules.
public
DateTimeOffset
ReadUtcTime​(int twoDigitYearMax,
Asn1Tag? expectedTag = null)
Reads the next value as a UTCTime with a specified tag.
Returns The decoded value.
twoDigitYearMax
The largest year to represent with this value.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 23).
public
DateTimeOffset
ReadUtcTime​(Asn1Tag? expectedTag = null)
Reads the next value as a UTCTime with a specified tag using the
<see cref="P:System.Formats.Asn1.AsnReaderOptions.UtcTimeTwoDigitYearMax" /> value from options passed to
the constructor (with a default of 2049).
Returns The decoded value.
expectedTag
The tag to check for before reading, or <see langword="null" /> for the default tag (Universal 23).
public
void
ThrowIfNotEmpty​()
Throws a standardized <see cref="T:System.Formats.Asn1.AsnContentException" /> if the reader has remaining
data, or performs no function if <see cref="P:System.Formats.Asn1.AsnReader.HasData" /> returns <see langword="false" /> .
Remarks
This method provides a standardized target and standardized exception for reading a
"closed" structure, such as the nested content for an explicitly tagged value.
public
bool
TryReadBitString​(Span<​byte> destination,
Int32& unusedBitCount,
Int32& bytesWritten,
Asn1Tag? expectedTag = null)
public
bool
TryReadCharacterString​(Span<​char> destination,
UniversalTagNumber encodingType,
Int32& charsWritten,
Asn1Tag? expectedTag = null)
public
bool
TryReadCharacterStringBytes​(Span<​byte> destination,
Asn1Tag expectedTag,
Int32& bytesWritten)
public
bool
TryReadOctetString​(Span<​byte> destination,
Int32& bytesWritten,
Asn1Tag? expectedTag = null)
public
bool
TryReadPrimitiveBitString​(Int32& unusedBitCount,
ReadOnlyMemory`1& value,
Asn1Tag? expectedTag = null)
public
bool
TryReadPrimitiveCharacterStringBytes​(Asn1Tag expectedTag,
ReadOnlyMemory`1& contents)
public
bool
TryReadPrimitiveOctetString​(ReadOnlyMemory`1& contents,
Asn1Tag? expectedTag = null)
public
bool
Equals​(object obj)
Inherited from object
protected
void
Finalize​()
Inherited from object
public
int
GetHashCode​()
Inherited from object
protected
object
MemberwiseClone​()
Inherited from object
public
string
ToString​()
Inherited from object