I need to encode and decode BER data. .NET has the class System.DirectoryServices.Protocols.BerConverter
The static method requires me to enter a string in the first parameter as shown below
byte[] oid = { 0x30, 0xD, 0x6, 0x9, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0xD, 0x1, 0x1, 0x1, 0x5, 0x0 }; // Object ID for RSA
var result2 = System.DirectoryServices.Protocols.BerConverter.Decoding("?what goes here?", oid);
BER encoding is used in LDAP, Certificates, and is commonplace in many other formats.
I'll be happy with information telling me how to Encode or Decode on this class. There is nothing on Stack Overflow or the first few pages of Google (or Bing) regarding this.
Question
How do I convert the byte array above to the corresponding OID using BER decoding?
How can I parse (or attempt to parse) SubjectPublicKeyInfo ASN.1 data in DER or BER format?
It seems the DER encoding\decoding classes are internal to the .NET framework. If so, where are they? (I'd like to ask connect.microsoft.com to make these members public)
After you have extracted the OID byte array, you can convert it to an OID string using
OidByteArrayToString()
. I have included the code below, since I couldn't find a similar function in the .NET libraries.I was not able to find a TLV parser in the .NET SDK either. Below is an implementation of a BER TLV parser,
BerTlv
. Since DER is a subset of BER, parsing will work the same way. Given a BER-TLVbyte[]
array, it will return a list ofBerTlv
objects that support access of sub TLVs.Maybe somebody else can answer this question.
Summary
Here is an example of how you can use the code provided below. I have used the public key data you provided in your previous post. The BerTlv should probably be augmented to support querying like
BerTlv.getValue(rootTlvs, '/30/30/06');
.Output:
OID Encode/Decode
BER-TLV Parser
Here is a utility class that contains some functions used in the class above. It is included for the sake of clarity how it works.