Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused about BER (Basic Encoding Rules)

I'm trying to study and understand BER (Basic Encoding Rules).

I've been using the website http://asn1-playground.oss.com/ to experiment with different ASN.1 objects and encoding them using BER.

However, even the simplest encodings seem to confuse me.

Let's take a simple ASN.1 schema:

World-Schema DEFINITIONS AUTOMATIC TAGS ::= 
BEGIN
  Human ::= SEQUENCE {
     name UTF8String
  }
END

So basically this is just a SEQUENCE with a single UTF8String type field called name.

An example of a value that matches this sequence would be something like:

{ "Bob" }

So, using http://asn1-playground.oss.com/, I produce the BER encoding of the following data:

some-guy Human ::= 
{  
    name "Bob"
}

I would expect this to produce one sequence object, followed by a single string object.

What I get is:

30 05 80 03 42 6F 62

Now, I understand some of this encoding. The first octet, 30, is the identifier which tells us that a SEQUENCE type is the first object. The 30 is 00110000 in binary, which means that we have a class of 0, a PC (primitive/constructed) bit of 1 (meaning constructed), and a tag number of 10000 (16 in decimal) which means SEQUENCE

So far so good. The next value is the LENGTH in bytes of the SEQUENCE, which is 05.

Okay, still so far so good.

But then... I'm totally confused by the next octet 80. What does that mean??? I would have expected a value of 00001100 (for tag number 12, meaning UTF8String.)

The bytes following the 80 are pretty straightforward: the 03 means Length of 3, and the 42 6F 62 is just the UTF8String value itself, "Bob"

like image 835
Channel72 Avatar asked Aug 26 '13 22:08

Channel72


2 Answers

The 80 is a context-specific tag 0. Please note that "AUTOMATIC TAGS" is used at the beginning of the module. This indicates that all SEQUENCE, SET and CHOICE types will have context specific tags for their components starting with [0], and incrementing by 1 for each subsequent component. This way, you don't have to worry about tag conflicts when creating your messages, especially when dealing with components which are OPTIONAL or have a DEFAULT value. If you change "AUTOMATIC" to "EXPLICIT" (which I would not recommend) you will see the [UNIVERSAL 12] that you were expecting in the encoding.

Please note that AUTOMATIC TAGS applied only to tags on components of SEQUENCE, SET, or CHOICE. It does not apply to the top level components, which is why you saw the [UNIVERSAL 16] for the SEQUENCE rather than seeing a context-specific tag there also.

like image 86
Paul Thorpe Avatar answered Sep 27 '22 19:09

Paul Thorpe


80 indicates context specific class, primitive, tag number 0. This is there because you specified an AUTOMATIC TAGGING environment, which automatically assigned a [0] tag to field name in type Human.

like image 20
Kevin Avatar answered Sep 27 '22 19:09

Kevin