Confused about BER (Basic Encoding Rules)

拈花ヽ惹草 提交于 2019-12-10 21:27:59

问题


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"


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/18453894/confused-about-ber-basic-encoding-rules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!