in BER tag,
"""
Bits 8-7: Tag class identifier
Bit 6: Primitive (0) or Constructed (1) - this indicates whether the value is primitive or constructed (i.e., contains other TLV elements)
Bits 5-1: Tag number (0-30) or 31 for extended form
"""
can you elaborate in details on tag class identifier
Assistant
In BER (Basic Encoding Rules) tags, the "Tag Class Identifier" refers to the first two bits (bits 8 and 7) of the tag byte. These bits are used to classify the context or purpose of the tag, providing information about the type or category of the data that is being encoded. The tag class identifier specifies the semantic scope of the tag. There are four possible values for these two bits, each representing a different class:
Class Identifiers (Bits 8-7):
Bits 8-7
Value
Class
Description
00
0
Universal
The tag is a standard, globally defined data type from the Abstract Syntax Notation One (ASN.1) specification. Universal tags are used for general-purpose data types, such as integers, strings, or structures (e.g., SEQUENCE).
01
1
Application
The tag is specific to a particular application or user-defined context outside the Universal class. This scope is narrower and specific to a particular application domain.
10
2
Context-Specific
The tag has meaning only within a particular context, often determined by its position within a structure or in relation to other data. Context-specific tags are often used in more complex data structures where the tag meaning depends on the enclosing context (such as within a SEQUENCE or SET).
11
3
Private
The tag is defined for private or proprietary use. It is application-specific but does not follow any formal structure or global standard. This is often used in internal systems where standardization is not necessary.
Detailed Explanation of Each Class:
1. Universal Class (00 / Bits 8-7 = 0):
Tags with this class are globally defined types that are part of the ASN.1 standard.
These types are recognized universally, regardless of application or context.
Examples of universal tag types include:
INTEGER (2)
BOOLEAN (1)
SEQUENCE (16)
OCTET STRING (4)
Universal tags are primarily used for representing basic data structures.
2. Application Class (01 / Bits 8-7 = 1):
Tags in this class are specific to an application or domain.
Unlike universal tags, their meanings are not the same across all applications; instead, these tags are defined within the scope of a specific application context.
Example: In a banking protocol, an APPLICATION tag might correspond to an account number or transaction ID.
3. Context-Specific Class (10 / Bits 8-7 = 2):
Tags in this class are defined in the context of a specific data structure or protocol and are meaningful only in that context.
These are commonly used to represent optional or alternative fields in protocols.
The actual meaning of the tag is often determined by its position in the sequence or structure, rather than by a globally recognized ID.
For example: In a nested SEQUENCE (a complex data structure), a context-specific tag might indicate an optional field, like an email address or phone number.
4. Private Class (11 / Bits 8-7 = 3):
Tags in this class are reserved for private or custom use.
These tags are defined by specific organizations or applications and are not part of any standardized specification.
They are useful for proprietary implementations that require specific tags for internal usage, where compatibility with other systems is not a concern.
For example: In a company-specific protocol, a PRIVATE tag might be used to represent an employee ID or internal metadata.
Example of Tag Class Identifier:
Let’s say we have a tag byte 10010110:
Bits 8-7 (Tag Class Identifier) = 10 → Context-Specific class
Bit 6 (Constructed/Primitive) = 0 → Primitive (the data is directly encoded in the tag)
Bits 5-1 (Tag Number) = 22 → Tag number 22 within the context-specific class
This tag represents a context-specific primitive value, where the tag number is 22.
Summary
The Tag Class Identifier provides essential context about what kind of data the encoded tag represents, specifically whether it is universal (general-purpose) or application, context-specific, or private (domain-specific or proprietary). This classification helps parsers interpret the data correctly based on its intended scope and usage.
User
saying "Bits 8-7" is inconclusive because tag can be multibyte when we have huge tag number
Assistant
You're absolutely correct—when dealing with multi-byte BER (Basic Encoding Rules) tags, it is necessary to be more precise about the interpretation of the class, because the tag can span multiple bytes, especially when the tag number exceeds 30 (i.e., when an extended form of tag encoding is used). Let me provide a detailed and more robust explanation:
Elaboration on BER Tag Structure for Multi-Byte Tags
A BER tag consists of multiple fields, and the relationship between the class, primitive/constructed (P/C) flag, and the tag number depends on whether the tag is single-byte or multi-byte (extended) format.
Single-Byte Tag Identifier
When the tag number is ≤ 30, the tag is encoded in a single byte, where the structure is as follows:
Bits
Field
Description
Bits 8-7
Class
Indicates the class of the tag (Universal, Application, Context-Specific, Private).
Bit 6
P/C Flag
Specifies whether the tag is Primitive (0) or Constructed (1).
Bits 5-1
Tag Number
Encodes the tag number directly if ≤ 30.
Multi-Byte Tag Identifier
When the tag number is greater than 30 (i.e., large tag numbers), the tag encoding switches to a multi-byte format. Specifically:
Bits 5-1 are set to 11111 (which is 31 in decimal) to indicate that the extended form is used.
The subsequent bytes encode the tag number in a variable-length format.
Structure of the First Byte in Multi-Byte Tags:
Bits
Field
Description
Bits 8-7
Class
Still identifies the class of the tag (Universal, Application, Context-Specific, Private).
Bit 6
P/C Flag
Specifies whether the tag is Primitive (0) or Constructed (1).
Bits 5-1
Fixed (11111)
Indicates that this tag uses the extended (multi-byte) form.
User
can you give a name to bit 6 like we call bits8,7 tag class idnetifier?
Assistant
Certainly! Bit 6 in the BER tag is typically referred to as the P/C Flag or the Primitive/Constructed Flag. This name reflects its primary purpose: to indicate whether the associated value is Primitive or Constructed. Similar to how bits 8-7 are called the "Tag Class Identifier," bit 6 can be explicitly labeled for clarity.
Suggested Name for Bit 6:
P/C Indicator (Primitive/Constructed Indicator)
Type Flag
Constructed Bit (commonly used in ASN.1 documentation)
Value Type Indicator
Summary of What Bit 6 Does:
Bit 6 (P/C Flag): This bit identifies the encoding type of the tag's associated content.
0 → Primitive: The value is directly encoded (e.g., an INTEGER or OCTET STRING).
1 → Constructed: The value is a composition of other TLV (Tag-Length-Value) elements, such as a SEQUENCE or SET.
So, here’s how the BER Tag Structure could be rephrased for clarity:
Bits
Field
Description
Bits 8-7
Tag Class Identifier
Indicates the class of the tag (Universal, Application, Context-Specific, or Private).
Bit 6
P/C Flag
Indicates whether the value is Primitive (0) or Constructed (1).
Bits 5-1
Tag Number
Encodes the tag number directly (0-30), or uses 11111 (31) to switch to the extended multi-byte form.
Thus, calling Bit 6 the P/C Flag or Primitive/Constructed Indicator is most accurate and common in the context of BER tagging.
User
write a table for p/c flag too
Assistant
Certainly! Below is a concise table that explains the P/C Flag (Bit 6) in detail, along with its meaning and implications.
P/C Flag (Bit 6) Table
Bit 6 (P/C Flag)
Value
Type
Description
Examples
0
0
Primitive
The data value is directly included in the tag (a single, non-nested value).
INTEGER, BOOLEAN, OCTET STRING, NULL
1
1
Constructed
The value is composed of one or more nested TLV (Tag-Length-Value) elements, like a container.
SEQUENCE, SET, Context-Specific structure, or nested elements
Details and Interpretation
Primitive (P/C Flag = 0):
Indicates that the value is a single, direct data element.
The encoded content can be directly interpreted without requiring further dissection of nested TLV structures.
Common use cases include basic data types like:
INTEGER (e.g., encoded value = 02 01 05, representing an integer value of 5)
OCTET STRING (e.g., encoded value = 04 03 41 42 43, representing the string ABC)
BOOLEAN, NULL, etc.
Constructed (P/C Flag = 1):
Indicates that the value contains other nested TLVs, forming a structured or composite type.
"Constructed" types act as containers, where the content itself includes multiple levels of encoded elements.
Examples of constructed types:
SEQUENCE (e.g., a list of values—encoded recursively as nested TLVs).
SET (e.g., an unordered collection of values).
Context-Specific Constructed (e.g., optional/extended data fields).
Comparison of Primitive and Constructed
User
so 9F 82 01 encodes tag number 257?
Assistant
Yes, absolutely! The tag 9F 82 01 indeed encodes a tag number of 257. Let me break it down so that you can clearly follow the reasoning.
Understanding the Components of 9F 82 01
First Byte: 9F (10011111 in binary)
Bits 8-7 (Tag Class Identifier):10
This means it belongs to the Context-Specific class.
Bit 6 (Primitive/Constructed):1
This indicates the tag is Constructed.
Bits 5-1 (Tag Number Field):11111
When this value is 11111 (31 in decimal), it signifies that the extended form is used to encode the tag number.
Thus, the first byte (9F) tells us to expect a multi-byte tag number.
Second Byte: 82 (10000010 in binary)
Bit 8 (Continuation Flag):1
The most significant bit (MSB) is 1, signaling that more bytes follow to encode the tag number.
Bits 7-1 (Tag Number Fragment):0000010
These 7 bits contribute 0x02 (decimal 2) to the tag number.
Third Byte: 01 (00000001 in binary)
Bit 8 (Continuation Flag):0
The most significant bit (MSB) is 0, marking this as the final byte of the tag number.
Bits 7-1 (Tag Number Fragment):0000001
These 7 bits contribute 0x01 (decimal 1) to the tag number.
Putting It All Together (Tag Number Calculation)
The tag number is stored in Base 128, with each byte contributing 7 bits (ignoring the MSB). To calculate the tag number:
Take the 7-bit contributions from all extended bytes:
Second byte: 0000010 = 2 in decimal.
Third byte: 0000001 = 1 in decimal.
Concatenate these values into a single binary number:
code
0000010 0000001 (Base 2) = 257 (Decimal)
The result is a tag number of 257.
Interpreted Information for 9F 82 01
Class (Bits 8-7):10 → Context-Specific.
P/C Flag (Bit 6):1 → Constructed.
Tag Number:257.
Full Explanation of the Encoding
The tag 9F 82 01 encodes a Context-Specific Constructed tag with tag number 257. This is how large tag numbers (≥31) are encoded in BER/DER/BER-like formats.
User
write concise steps in bullet form to encode 420 as tag number
Assistant
To encode the tag number 420 in BER (Basic Encoding Rules), follow these steps:
Steps to Encode Tag Number 420
Determine the Class and Primitive/Constructed Bit (First Byte):
Assign Class (Bits 8–7): Choose 00 (Universal), 01 (Application), 10 (Context-Specific), or 11 (Private).
Set P/C Flag (Bit 6): 0 for Primitive or 1 for Constructed.
Set Tag Number Field (Bits 5–1):
If the tag number > 30, set Bits 5–1 as 11111 (31) to indicate extended form.
For 420, this will be the case, as 420 > 30.
Convert Tag Number to Base-128 Representation:
Encode the tag number 420 in binary: 110100100.
Split the binary into 7-bit groups from right to left:
First group: 1000100 (least significant 7 bits).
Second group: 0000011 (remaining bits).
Add a continuation bit (1) to all groups except the last one:
Second group becomes 10000011 (MSB set to 1).
First group becomes 00010010 (MSB set to 0).
Construct the Encoded Bytes:
First byte: Combine Class, P/C, and 11111 for extended form.
The encoded tag for tag number 420 is BF 83 04 in hex.
User
refine the wordings and complete this (by refine i dont mean adding extra words, i mean improve existing words while being concise)
"""
Multi-byte tag (extended-form)
First byte
code
<Tag Class Identifier><Primitive/Constructed Flag><Fixed value 11111>
Bits
Field
Description
Bits 8-7
Class
One of Universal, Application, Context-Specific, Private
Bit 6
P/C Flag
Primitive (0) or Constructed (1)
Bits 5-1
Fixed (11111)
Indicates this tag uses extended (multi-byte) form
Continuation bytes
code
<Continuation flag><Tag Number>
Bits
Field
Description
Bit 8
Continuation flag
Bits 7-1
Significant Tag Number Parts
"""
Assistant
Here’s the refined and completed version of your text:
Multi-byte Tag (Extended Form)
First Byte
code
<Tag Class Identifier><Primitive/Constructed Flag><Fixed Value 11111>
Bits
Field
Description
Bits 8-7
Class
Specifies one of Universal, Application, Context-Specific, or Private
Bit 6
P/C Flag
Indicates Primitive (0) or Constructed (1)
Bits 5-1
Fixed (11111)
Marks the start of an extended (multi-byte) tag number
Continuation Bytes
code
<Continuation Flag><Tag Number Fragment>
Bits
Field
Description
Bit 8
Continuation Flag
1 if more bytes follow, 0 for the final byte of the tag number
Bits 7-1
Tag Number Fragment
Encodes the tag number in 7-bit chunks (Base 128)
This version retains your concise format but improves clarity and fills in the missing descriptions. Let me know if there's anything else you'd like adjusted!
User
give an example in ascii art style on sequence's tag
Assistant
Certainly! Let's create an ASCII-art style example to visualize the encoding of the BER tag for a SEQUENCE in TLV (Tag-Length-Value) format, including the tag's breakdown.
Example: Encoding a SEQUENCE Tag
Suppose we are working with a SEQUENCE in BER encoding. By definition:
Class: Universal (00 from Bits 8-7).
P/C Flag (Bit 6): Constructed (1 because SEQUENCEs are containers for other TLVs).
Tag Number: 16 (SEQUENCE is a standard ASN.1 tag, assigned as 16 in the Universal class).
Here's how the tag would be encoded:
code
+-----+------+----------+
| 00 | 1 | 10000 |
+-----+------+----------+
^ ^ ^
| | +-- Tag Number = 16 (binary: 10000)
| +--------- P/C Flag = Constructed (1)
+--------------- Class = Universal (00)
Encoded Tag Byte
The individual fields combine to form the 8-bit tag byte:
code
Binary: 00110000
Decimal: 48
Hex: 0x30
Thus, the SEQUENCE Tag is encoded as 0x30.
Complete Example with Length and Value (TLV):
Consider a SEQUENCE containing two integers: 5 and 10.
Tag (T): SEQUENCE (0x30).
Length (L): Full length of the contained data. Here, the total length is 6 (each INTEGER is 3 bytes: 1 byte for tag, 1 byte for length, and 1 byte for the value).
Here’s the complete TLV encoding for the SEQUENCE:
code
Tag (T) Length (L) Value (V)
0x30 0x06 0x02 0x01 0x05 0x02 0x01 0x0A
User
thats great one. now on encoding 420 being Private and primitive
Assistant
Great! Let's break down the process of encoding the tag number 420 in BER format with the following properties:
Class: Private (11 from Bits 8-7).
P/C Flag (Primitive/Constructed): Primitive (0 for Primitive value).
Tag Number: 420 (since tag numbers > 30 use the extended multi-byte format).
Step-by-Step Encoding of 420
1. First Byte: The Leading Tag Byte
The leading tag byte encodes the Class, the Primitive/Constructed Flag, and Tag Number (or indicates that extended tag encoding is used if the tag number > 30):
code
< Tag Class >< P/C Flag >< Tag Number (or 11111 for extended) >
Class (Bits 8-7): 11 → Private.
P/C Flag (Bit 6): 0 → Primitive.
Tag Number Field (Bits 5-1): Since 420 > 30, we use the extended form, so this field is set to 11111 (31 in decimal).
Resulting First Byte: 11111111 (binary) → 0xFF (hex).
2. Continuation Bytes: Encode 420 in Base 128
In extended multi-byte format, the tag number is encoded in Base 128 (7 bits per byte), using continuation bits (the MSB of each byte) to indicate whether more bytes follow:
Binary Representation of 420: 110100100 (9 bits).
Split the binary into 7-bit chunks (from right to left):
First chunk (least significant): 100100 → 0100100 (7 bits).
Second chunk: 11 → 0000011.
Add the Continuation Flag:
For all but the last byte, set the MSB (Bit 8) to 1 to indicate continuation.
User
cant you have art close to this?
code
+-----+------+----------+
| 00 | 1 | 10000 |
+-----+------+----------+
^ ^ ^
| | +-- Tag Number = 16 (binary: 10000)
| +--------- P/C Flag = Constructed (1)
+--------------- Class = Universal (00)
Assistant
Certainly! Let me reformat the explanation and use a similar ASCII art-style visualization for encoding the tag number 420 in Private class with Primitive type.
Encoding 420 in Private Class, Primitive
First Byte
code
+-----+------+----------+
| 11 | 0 | 11111 |
+-----+------+----------+
^ ^ ^
| | +-- Tag Number = 31 (indicates extended/multi-byte tag)
| +--------- P/C Flag = Primitive (0)
+--------------- Class = Private (11)
Class (Bits 8-7):11 → Private.
P/C Flag (Bit 6):0 → Primitive.
Tag Number Field (Bits 5-1):11111 → Indicates extended (multi-byte) tag.
Thus, the first byte of the tag = 11111111 (binary) → 0xFF.
Continuation Bytes (for Tag Number 420)
Convert tag number 420 into binary:
code
420 = 110100100 (binary)
Split into 7-bit chunks (Base 128 representation):
no you are laying them out vertically in the art. can you put the bytes horizontally
Assistant
Oh, I see what you're asking! You're requesting an ASCII art representation where multiple bytes are laid out side by side horizontally instead of vertically. Got it! Let me reformat everything accordingly.
Encoding Tag Number 420: Private Class, Primitive
Here’s how the encoded tag FF 83 24 looks when represented horizontally.
Certainly! Let me expand the ASCII art to cover more details about the value 420 and how it is encoded. I'll break it down into the class, P/C flag, and the detailed encoding of the tag number (420), but all laid out horizontally in a clean and readable manner.
Full Horizontal Representation for Tag Number 420 (Private, Primitive)
code
+--------+------+-----------+-------------+-------------+-------------------------+
| 11 | 0 | 11111 | 1 000011 | 0 100100 | Value = 420 |
+--------+------+-----------+-------------+-------------+-------------------------+
| Private| P/C | Extended | Continuation| Final Byte | Encodes Tag Number 420 |
| Class | Flag | Tag Start | (Part 1) | (Part 2) | (Base 128 representation)|
+--------+------+-----------+-------------+-------------+-------------------------+
FF 83 (Continuation) 24
Detailing Each Section Horizontally
1. Class and P/C Flag (First Byte)
The first byte (0xFF) is broken down as follows:
code
+--------+------+-----------+
| 11 | 0 | 11111 |
+--------+------+-----------+
| Private| P/C | Extended |
| Class | Flag | Tag Start |
+--------+------+-----------+
Class (Bits 8–7):11 → Private class.
P/C Flag (Bit 6):0 → Primitive value.
Tag Number Indicator (Bits 5–1):11111 → Signals that this is an extended tag (multi-byte tag number follows).
Encoded as: FF
2. Continuation Byte(s)
The tag number 420 is encoded in extended form using and split into 7-bit chunks:
User
no
"""
+--------+------+-----------+-------------+-------------+
| 11 | 0 | 11111 | 1 000011 | 0 100100 |
+--------+------+-----------+-------------+-------------+
| Private| P/C | Extended | Continuation| Final Byte |
| Class | Flag | Tag Start | (Part 1) | (Part 2) |
+--------+------+-----------+-------------+-------------+
"""
to new reader its not apparent where 420 is in this art
Assistant
Ah, I understand your point now! You want the ASCII art representation to make it explicitly clear where 420 is encoded in the tag. I'll address this by explicitly showing the binary breakdown of 420 (from Base 128 encoding) and how it maps into the continuation and final bytes. Here's an improved, more descriptive representation:
Encoding for Tag Number 420 (Private, Primitive)
code
+--------+------+-----------+-------------+-------------+
| 11 | 0 | 11111 | 1 000011 | 0 100100 |
+--------+------+-----------+-------------+-------------+
| Private| P/C | Extended | Continuation| Final Byte |
| Class | Flag | Tag Start | (Part 1) | (Part 2) |
+--------+------+-----------+-------------+-------------+
^ ^ ^ ^
| | | |
| | | +-- Last 7 bits of 420 (binary: 100100)
| | +--------------- First 7 bits of 420 (binary: 000011)
| +------------------------------- Encodes extended form (11111)
+--------------------------------------------- Indicates Private class, Primitive
Detailed Breakdown of 420 in the Tag
Tag Start (First Byte: FF):
Class (Bits 8–7):11 → Private.
P/C Flag (Bit 6):0 → Primitive.
Tag Number Field (Bits 5–1):11111 → Indicates extended tagging (multi-byte).
Continuation Byte(s) (Base-128 Encoding of 420):
Binary Representation of 420:110100100
Split into 7-bit chunks (Base 128):
User
this one is good but the continuation bit is not known to new reader so they have no clue what the leading extra is for:
"""
+--------+------+-----------+-------------+-------------+
| 11 | 0 | 11111 | 1 000011 | 0 100100 |
+--------+------+-----------+-------------+-------------+
| Private| P/C | Extended | Continuation| Final Byte |
| Class | Flag | Tag Start | (Part 1) | (Part 2) |
+--------+------+-----------+-------------+-------------+
^ ^ ^ ^
| | | |
| | | +-- Last 7 bits of 420 (binary: 100100)
| | +--------------- First 7 bits of 420 (binary: 000011)
| +------------------------------- Encodes extended form (11111)
+--------------------------------------------- Indicates Private class, Primitive
"""
Assistant
Ah! You're right—if the continuation bit (the “leading extra bit”) isn’t explained, a new reader might not understand why it’s there. Let me clarify that in the diagram and provide an additional explanation for the continuation flag in Base 128 encoding. Here's the improved version:
Revised Horizontal ASCII Art Representation (With Continuation Flag Explanation)
code
+--------+------+-----------+----------------+----------------+
| 11 | 0 | 11111 | 1 | 000011 | 0 | 100100 |
+--------+------+-----------+----------------+----------------+
| Private| P/C | Extended | Continuation | Final Byte |
| Class | Flag | Tag Start | Tag Part (3) | Tag Part (36) |
+--------+------+-----------+----------------+----------------+
^ ^ ^ ^
| | | |
| | | +--- Last 7 bits of 420 (binary: 100100)
| | +---------------------- First 7 bits of 420 (binary: 000011)
| +---------------------------------------- Encodes extended form (11111)
+----------------------------------------------------- Private class, Primitive
Clear Explanation of Each Part
Why the Continuation Flag?
The Base 128 encoding used for multi-byte tag numbers assigns:
Bit 8 in each byte (MSB) as the Continuation Flag.
1 = Continuation: More bytes will follow to complete the tag number.
0 = Final: This byte is the last byte for the extended tag number.
The remaining 7 bits in each byte encode part of the tag number.
Dataset releases
Not yet included in a published release.
Structure of Subsequent Bytes Encoding the Tag Number:
The tag number is encoded in Base 128 (7 bits per byte).
Each byte contributes 7 bits to the tag number.
The most significant bit (MSB) of each byte is a continuation flag:
If MSB = 1, this means that additional bytes are part of the tag number encoding.
If MSB = 0, this is the last byte of the tag number encoding.
Interpreting Class Information in Multi-Byte Tags
Even when the tag is multi-byte, the class identifier always resides in the first two bits (bits 8-7) of the first byte. The tag class remains unambiguous, regardless of how many bytes are used to encode the full tag. This means:
Bits 8-7 of the first byte define the tag class, regardless of the tag number's length.
The interpretation of the class bits is the same as in the single-byte tag:
Bits 8-7
Value
Class
Description
00
0
Universal
Tag is globally defined by the ASN.1 standard.
01
1
Application
Tag is application-specific (defined for use in a specific application).
10
2
Context-Specific
Tag has a meaning defined within the context of a specific structure or schema.
11
3
Private
Tag is defined for private or proprietary use (not subject to ASN.1 standards).
Examples: Multi-Byte Extended Tag Formats
Example 1: A Single-Byte Tag
Suppose the tag byte is 0x62 (01100010 in binary):
Bits 8-7:01 → Application class.
Bit 6:1 → Constructed tag.
Bits 5-1:2 → The tag number is 2 (single-byte tag).
Example 2: A Tag with Extended Numbering (Multi-Byte Tag)
Suppose the multi-byte tag consists of 9F 82 01:
First byte: 9F (10011111 in binary)
Bits 8-7:10 → Context-Specific class.
Bit 6:1 → Constructed tag.
Bits 5-1:11111 → Extended tag numbering is used.
Second byte: 82 (10000010 in binary)
Bit 8 (MSB):1 → Continuation flag (indicates more bytes follow).
Bits 7-1:0000010 → Encodes part of the tag number.
Third byte: 01 (00000001 in binary)
Bit 8 (MSB):0 → Last byte of the tag number.
Bits 7-1:0000001 → Encodes the final part of the tag number.
Tag class interpretation:
The class (Bits 8-7 = 10) of the first byte still denotes Context-Specific, regardless of the multi-byte tag format.
Tag number calculation:
Combining the 7-bit pieces from the second and third bytes, the tag number is (0x02 << 7) | 0x01 = 257.
Thus, the tag 9F 82 01 represents a Context-Specific, Constructed tag, with a tag number of 257.
Key Takeaways
Tag class information is always encoded in the first two bits (bits 8-7) of the first byte, regardless of whether the tag is single- or multi-byte.
The multi-byte tag format is used for large tag numbers (greater than 30) and encodes the tag number using Base 128 with continuation flags (MSB).
The class portion of the tag is unaffected by extended tag numbering and indicates whether the tag is Universal, Application, Context-Specific, or Private.
Once the class and P/C bit are decoded, you can proceed to interpret the tag number, regardless of its length.
This ensures that the "class" of the tag is always easy to interpret and consistent, even for multi-byte tags.
Aspect
Primitive (0)
Constructed (1)
Data Type
Single, atomic value
Composite, containing other TLVs
Size
Typically smaller (directly encodes the value)
Can be larger (nested TLVs add overhead)
Example Encodings
02 01 05 (INTEGER 5)
30 0A 02 01 05 04 03 41 42 43 (SEQUENCE)
Usage
Simple data types
Complex, hierarchical data structures
Practical Encoded Example
Example 1: Primitive Encoding
plaintext
02 01 05
Tag Byte (02): Universal, Primitive, INTEGER (tag number = 2), P/C = 0.
Encoded Value: The number 5 directly encoded.
Example 2: Constructed Encoding
plaintext
30 0A 02 01 05 04 03 41 42 43
Tag Byte (30): Universal, Constructed, SEQUENCE (tag number = 16), P/C = 1.
Encoded Value: Nested TLVs (02 01 05 for an INTEGER and 04 03 41 42 43 for an OCTET STRING ABC).
In the second example, the Constructed flag indicates the presence of nested TLV elements, requiring additional parsing.
This table and explanation should make the P/C Flag (Bit 6) interpretation intuitive and clear!
Length (0x06) specifies that the total size of the content is 6 bytes.
Value contains two nested INTEGER TLVs:
02 01 05 → INTEGER 5
02 01 0A → INTEGER 10
This ASCII-style breakdown helps to map the tag's construction and placement within the larger TLV representation. Let me know if you'd like another example!
For the last byte, set the MSB (Bit 8) to 0 to indicate this is the final byte.
Encoding the chunks with continuation bits:
First byte: 10000011 (0x83) → Continuation (more bytes follow).
Second byte: 00010010 (0x24) → Final byte.
3. Combine All Bytes
Combine the first byte (class, P/C flag, and extended tag marker) with the continuation bytes to get the full encoded tag:
code
0xFF 0x83 0x24
Final Encoded Tag for 420 (Private, Primitive)
FF 83 24
Visual Breakdown (ASCII Art)
code
+----------------+--------------------+---------------------+
| First Byte | Continuation Byte | Final Continuation |
+----------------+--------------------+---------------------+
| Tag Class=11 | Base-128 (part 1) | Base-128 (final) |
| P/C Flag = 0 | Continuation=1 | Continuation=0 |
| Tag Number=11111| Tag Number=0000011| Tag Number=100100 |
+----------------+--------------------+---------------------+
Binary Rundown:
+-------+--------+--------+
| 11111111 | 10000011 | 00100100 |
| 0xFF | 0x83 | 0x24 |
+-------+--------+--------+
Explanation of the Result
0xFF
Class: Private (11 = Class).
Primitive: (0 = P/C Flag is Primitive).
Tag Number: 11111 indicates extended tag encoding.
Base-128 Continuation Bytes (0x83 0x24)
Encoded 420 in Base-128 with continuation bits:
0x83 (10000011): Indicates continuation of the tag number.
0x24 (00100100): Last byte, completing the tag number encoding.
So the encoded tag 0xFF 0x83 0x24 represents:
Private class.
Primitive type.
Tag Number = 420.
Let me know if you'd like to decode a different number or see how this applies in TLV! 😊
420
Combine the bytes:
code
FF 83 24
Final Visual Representation with Art
code
+-----+------+----------+
| 11 | 0 | 11111 |
+-----+------+----------+
^ ^ ^
| | +-- Tag Number = 31 (extended multi-byte mode)
| +--------- P/C Flag = Primitive (0)
+--------------- Class = Private (11)
+-------+----------+
Continuation | 1 | 000011 |
+-------+----------+
^ ^
| +-- Tag Number fragment (first 7 bits)
+---------- Continuation Flag = 1 (more bytes follow)
+-------+----------+
Final Byte | 0 | 100100 |
+-------+----------+
^ ^
| +-- Tag Number fragment (last 7 bits)
+---------- Continuation Flag = 0 (last byte)
Interpretation of Encoded Bytes
First Byte (FF):
Class = Private (11).
Primitive = 0 (not Constructed).
Tag Number = 31 (11111 → Indicates extended mode).
Continuation Bytes (83 24):
Encodes 420 in Base 128:
83 → Continuation flag + Tag Number bits (000011).
24 → Final byte + Tag Number bits (100100).
Summary
The tag for 420 in Private Class, Primitive Type is:
code
FF 83 24
Let me know if you'd like to refine this further! 😊
00010010
0x24
Complete Encoded Tag
Combine all the parts:
code
FF 83 24
Horizontal ASCII Art Representation
Here’s the full tag byte-by-byte, laid out horizontally:
+--------+------+-----------+
| 11 | 0 | 11111 |
+--------+------+-----------+
^ ^ ^
| | +-- Tag Number = 31 (indicates extended form)
| +--------- P/C Flag = Primitive (0)
+----------------- Class = Private (11)
Second Byte (Continuation): 83
code
+-------+------------+
| 1 | 000011 |
+-------+------------+
^ ^
| +-- Tag Number fragment (first 7 bits)
+---------- Continuation Flag = 1 (more bytes follow)
Third Byte (Final Byte): 24
code
+-------+------------+
| 0 | 100100 |
+-------+------------+
^ ^
| +-- Tag Number fragment (last 7 bits)
+---------- Continuation Flag = 0 (end of tag number)
Summary
The tag FF 83 24 represents:
Private class (11).
Primitive type (0).
Tag Number = 420 (encoded in extended form using Base 128).
Let me know if you'd like to explore another example! 😊
code
+-------+----------+
| 0 | 100100 |
+-------+----------+
| Final | Part 2 |
+-------+----------+
Most Significant Bit (MSB):0 → Indicates this is the last byte of the tag number.
Bits 7-1:100100 → Final fragment of the tag number.
+--------+------+-----------+-------------+-------------+-------------------------+
| 11 | 0 | 11111 | 1 000011 | 0 100100 | Value = 420 |
+--------+------+-----------+-------------+-------------+-------------------------+
| Private| P/C | Extended | Continuation| Final Byte | Encodes Tag Number 420 |
| Class | Flag | Tag Start | (Part 1) | (Part 2) | (Base 128 representation)|
+--------+------+-----------+-------------+-------------+-------------------------+
FF 83 (Continuation) 24
Overall Tag Breakdown
Now, let's recap what every byte means:
First Byte (FF):
Encodes the Private class (11), Primitive type (0), and signals extended tagging with a reserved 11111.
Function: Indicates that the full tag number requires additional bytes.
Second Byte (83):
Encodes the first 7 bits of the tag number (000011) with a 1 as the MSB (continuation flag set).
Function: Indicates more bytes are part of the tag number.
Third Byte (24):
Encodes the remaining bits of the tag number (100100) with a 0 as the MSB (final flag set).
Function: Marks the completion of the tag number encoding.
Summary
The tag number 420, encoded as Private class and Primitive type, is:
code
FF 83 24
This encoding employs multi-byte tagging (extended form) because the tag number exceeds 30. The tag number is split across continuation bytes, leveraging Base 128 encoding with MSB flags to signal continuation and the end.
Enhanced Horizontal Tag View with Continuation Flag Highlighted
code
+--------+------+-----------+----------------+----------------+
| 11 | 0 | 11111 | 1 | 000011 | 0 | 100100 |
+--------+------+-----------+----------------+----------------+
| Private| P/C | Extended | Continuation | Final Byte |
| Class | Flag | Tag Start | Tag Part (3) | Tag Part (36) |
+--------+------+-----------+----------------+----------------+
Key Takeaways
To explain to a new reader:
First Byte (FF):
Sets up the tag as Private (11), Primitive (0), and an extended tag (11111).
Second Byte (83):
Encodes the first part of 420 (3 in decimal = 000011 in binary).
Uses a continuation flag (1) to indicate more bytes will follow.
Third Byte (24):
Encodes the final part of 420 (36 in decimal = 100100 in binary).
Sets the final flag (0) to indicate no further bytes remain.
By splitting the tag number into Base-128 fragments and using MSB to indicate continuation, this approach efficiently encodes large tag numbers like 420.