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"