Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASN.1 encodings BER, PER etc. in C#

Tags:

c#

encoding

asn.1

I have been searching on this topic for a while now, without finding any relevant answers. So thought of taking it on 'Stackoverflow' ...

We are trying to encode a string in order to pass it over a TCP/IP connection. Since ASN.1 is the most popular one to do it, so we are trying the various rules BER,DER,PER etc. to find out which one we can use. Our application is a .net based application and I was looking for freely available library which does this.

Strangely i could not find any free libraries.So, i started looking in the .Net framework itself. I found the there is only a 'BERConverter'. So, i did a small example with it. Taking an example string

string str = "The BER format specifies a self-describing and self-delimiting format for encoding ASN.1 data structures. Each data element is encoded as a type identifier, a length description, the actual data elements, and, where necessary, an end-of-content marker. These types of encodings are commonly called type-length-value or TLV encodings. This format allows a receiver to decode the ASN.1 information from an incomplete stream, without requiring any pre-knowledge of the size, content, or semantic meaning of the data" 

In UTF-8 or ASCII it show as 512 bytes. I use the following code to encode it using BER

public static byte[] BerConvert(byte[] inputbytes)
    {
        byte[] output = BerConverter.Encode("{o}", inputbytes);
        return output;
    }    

I get a byte array with size 522. In some of the other cases I find that the byte size increases compared to the original text. I thought encoding will decrease the size. Why is it happening like this ?

Apart from BER, are there other encoding rules like PER or DER which can be used to reduce the encoding size ? Are there any examples, libraries, or support which will help is implementing the these encoding styles?

like image 289
aviKnight Avatar asked Oct 31 '22 09:10

aviKnight


2 Answers

When looking for ASN.1 Tools (free and commercial), a good place to start is the ITU-T web page http://www.itu.int/en/ITU-T/asn1/Pages/Tools.aspx that lists several. There are commercial tools listed there that support C#, but I do not see a free C# tool.

As for reduction of size of encodings, this depends significantly on the nature of your ASN.1 specification and the encoding rules used. If you are primarily sending text strings, BER and DER will not result in a reduction of the size of your message, while PER can significantly reduce the size of the message if you are able to produce a "permitted alphabet" constraint indicating a smaller set of characters permitted in the text you are sending.

You can try various encodings rules and different constraints to see the effects of your changes at the free online ASN.1 encoder decoder at http://asn1-playground.oss.com.

like image 94
Paul Thorpe Avatar answered Nov 11 '22 14:11

Paul Thorpe


If you are beginning work on a new protocol, you may want to reevaluate your needs a bit.

As you probably know by now, ASN.1 comes with a bit of overhead—not just in the messaging, but in the engineering. A typical workflow involves writing a specification that describes the protocol, feeding it into a CASE tool that generates source code for an API, and then integrating the generated components into your application.

That said, some prefer a more ad-hoc approach. Microsoft has a BER converter class that you could try to use with C#: it may be suitable for your needs.

If compression is important, you may want to look into PER, as Paul said. But it's hard to produce valid PER encodings by hand because they rely on the specification to perform compression. (The permitted alphabet constraint is written into the specification and used to enumerate valid characters for shrinking the encoding.)

For more information on ASN.1 there are a number of tutorials online; you can also look at ITU-T standards X.680-X.695, which specify both the syntax notation and various encoding rules.

like image 37
Ethan Avatar answered Nov 11 '22 14:11

Ethan