in BER, a string can be constructed, which it can contain primitive string TLV fragments.
can it contain constructed string TLV fragments too?
Assistant
Yes, in Basic Encoding Rules (BER), a "string" can indeed contain constructed string TLV (Tag, Length, Value) fragments. In BER encoding, data elements can be either primitive or constructed.
Primitive elements are the simplest form of encoding for a value (e.g., a single string or integer).
Constructed elements are those that contain other TLV elements within them, effectively grouping multiple values or pieces of data.
For a "constructed string," the constructed TLV would essentially be an outer TLV that contains multiple inner TLV elements (which could themselves be either primitive or constructed). BER explicitly supports constructed formats to represent composite structures.
Tag Form in BER
Each TLV includes a tag that specifies whether it's primitive or constructed:
Primitive: The tag's first bit (in the tag byte) is set to 0, indicating the value is encoded as a single entity.
Constructed: The tag's first bit is set to 1, indicating that the value is composed of nested TLV elements.
Usage of Constructed Strings
This design makes it possible for BER to represent constructed string TLV fragments. For instance, you could have a TLV where:
The "constructed" string (outer TLV) groups multiple primitive or constructed inner string segments.
Example:
Assume you want to encode a TLV structure that contains multiple string values within a composite structure:
04 ; Tag: Octet String, Primitive
05 ; Fixed Length of 5 bytes
48 65 6C 6C 6F ; Value
Assistant
To encode your requested example in BER (Basic Encoding Rules) using the given TLV (Tag, Length, Value) format, let's break down the structure step by step and construct it in detail:
Structure Recap:
We want to represent an Octet String (Constructed) that contains:
An Inner Octet String (Primitive).
An Inner Octet String (Constructed), which itself contains:
Two Sub-inner Octet Strings (Primitive).
Example Explanation
Outer Octet String (Constructed):
Tag: 24 (24 in hexadecimal indicates "Constructed Octet String" — the first bit of the tag is set to 1 to mark it as constructed, and the base Octet String tag is 04).
Length: This needs to be calculated from the sum of the inner TLVs.
Value: Contains the encoded inner TLVs.
Inner Octet String (Primitive):
Tag: 04 (Primitive Octet String).
Length: Length of the content (e.g., 5 bytes).
Value: The actual content (e.g., "Hello" → 48 65 6C 6C 6F in ASCII).
Inner Octet String (Constructed):
Tag: 24 (Constructed Octet String).
Length: This needs to be calculated from the sum of the Sub-inner TLVs.
Value: Contains the sub-inner TLVs.
Sub-inner Octet Strings (Primitive) (two in total):
Each is a primitive Octet String with its own tag, length, and value.
Let’s encode the two structures step-by-step, following BER (Basic Encoding Rules) conventions.
Key Notes
Sequence:
Encoded using universal tag 30 (tag for a "Sequence").
Constructed by definition (since it contains other TLVs inside).
Integer:
Encoded with tag 02 (tag for an "Integer").
Primitive by definition.
The value 42 is encoded as 2A in hexadecimal.
String ("foo"):
Encoded with tag 04 (tag for an "Octet String").
In case of definite length, the length is explicitly stated.
In case of indefinite length, it is terminated with an End-of-Content (EOC) marker (00 00).
1. Sequence with Indefinite Length
Definition:
Outer Sequence is of indefinite length, terminated by an EOC marker (00 00).
The Integer (42) is encoded as a primitive TLV with a definite length.
The String ("foo") is encoded as a primitive TLV but with an indefinite length, also terminated by an EOC marker (00 00).
Encoding Steps:
Integer (42):
Tag: 02 (Primitive Integer).
Length: 01 (1 byte).
Value: 2A (hex for 42).
Encoded: 02 01 2A
String ("foo") with Indefinite Length:
Tag: 04 (Primitive Octet String).
Length: Indefinite (80 is the special length indicator for indefinite length).
Value: 66 6F 6F (ASCII for "foo"), terminated with EOC (00 00).
Encoded: 04 80 66 6F 6F 00 00
Sequence (Outer) with Indefinite Length:
Tag: 30 (Constructed Sequence).
Length: Indefinite (80 is the special length indicator).
Value: Contains the integer and octet string, terminated with an EOC ().
User
what is eoc
Assistant
EOC stands for End-of-Content.
In Basic Encoding Rules (BER), an End-of-Content marker (00 00 in hexadecimal) is used to denote the end of data when an element has an indefinite length. The indefinite length encoding allows a flexible way to encode data when the size of the content is not known in advance or when streaming data dynamically. The EOC marker signals the conclusion of the TLV (Tag, Length, Value).
Characteristics of EOC
The EOC marker consists of two zero bytes: 00 00.
The first 00 represents the tag, which is a reserved "End-of-Content" tag.
The second 00 represents the length, which is always zero for the EOC marker.
It is only used with elements that have an indefinite length (80 is used as the length field for such elements).
Where is EOC Used?
The EOC marker is used in two main places:
Indefinite-Length Elements:
When an element (like a Sequence, Octet String, etc.) is encoded with a length indicator of 80, it means the length of the content is not explicitly stated.
The EOC marker signals the end of the content.
Example:
plaintext
04 80 ; Tag: Octet String, Length: Indefinite
66 6F 6F ; Value: "foo" (ASCII for "foo")
00 00 ; EOC (End-of-Content for the Octet String)
Nesting of Indefinitely-Length Encodings:
If a constructed element (like a Sequence or Set) contains sub-elements, and the outer element has an indefinite length, an EOC marker is also used to terminate the outer element.
0x00 0x00 cannot be within value as it conflicts with end marker for indefinite length types
"""
the statement is valid for string and any in primitive form using indefinite length
but for constructed type, its ok to have 00 00 in value by having child indefinite-length TLVs
Assistant
0x00 0x00 cannot appear within the value of a primitive indefinite-length type, as it conflicts with the end-of-content marker. However, for constructed types, 0x00 0x00 is allowed within the value if it appears as part of child indefinite-length TLVs.
User
"""
- Constructed types: Sequence, Set, String types, Any, Custom types
"""
are these all constructed types?
Assistant
Not all the types listed in your statement are necessarily constructed types by default, as the distinction between constructed and primitive types in ASN.1 encoding (e.g., BER/DER) depends on the encoding rules and specific use cases. Let’s review the types you’ve listed individually:
1. Sequence:
Always constructed.
A Sequence contains zero or more components, which are themselves TLVs (e.g., other types, either primitive or constructed).
Usage Example: A list of ordered items.
2. Set:
Always constructed.
A Set is similar to a Sequence, with the primary difference being that the elements are unordered.
Usage Example: A set of unordered values.
3. String Types:
Depends on the encoding (can be constructed or primitive):
Most commonly, string types like UTF8String, IA5String, PrintableString, etc., are primitive by default (containing a simple string value).
However, string types can also be encoded as constructed if the value is represented as a concatenation of multiple string fragments (e.g., in indefinite-length encoding with BER, or explicitly dividing long strings into segments).
4. Any:
Not inherently a specific type but a placeholder for any valid ASN.1 type (either primitive or constructed).
Whether it is primitive or constructed depends on the actual value encoded.
Usage Example: OpenType in ASN.1, where the exact type is determined dynamically.
5. Custom Types:
Depends on the definition:
Custom types defined in an ASN.1 schema can be constructed if they represent a structure (e.g., Sequence or Set).
If custom types represent a single value (like an integer or a simple string), they can be primitive.
Refined Classification
Based on the above analysis, here’s a clearer and accurate statement:
Always Constructed Types:
Sequence, .
User
No. im asking are all constructed types covered in the statement?
Assistant
Ah, I see now. You're asking whether all constructed types are covered in this list:
No, not all constructed types are covered in this statement. Let me clarify and expand on this.
Key Points:
ASN.1 constructed types include all composite structures where the encoding uses a constructed approach (i.e., an outer header with encapsulated inner TLVs).
The statement you provided does mention some of the key constructed types but does not explicitly cover all possible ones, especially:
Types like SetOf and SequenceOf, which are specific constructed types.
Certain implicitly constructed types based on schema definitions.
Some overlooked universal constructed types in ASN.1.
Comprehensive List of Constructed ASN.1 Types:
1. Universal Constructed Types (Defined in ASN.1):
Specific string types when encoded as constructed, such as UTF8String, IA5String, PrintableString, etc., especially under BER indefinite-length encoding.
2. Application-Specific or Custom Types:
Explicitly Defined Constructed Types:
Custom types in an ASN.1 schema can be constructed (e.g., a custom data structure defined as a composite of multiple fields).
Tagged Types:
Types with EXPLICIT tagging are treated as constructed, even if they wrap a primitive or constructed inner value.
3. "Any" Type:
As mentioned, ANY can be constructed or primitive, depending on the actual encoded content.
Types Not Covered in Your Statement:
Here is what your original statement misses:
User
can you break these into multiple bullets and make them less wordy while conveying same idea:
"""
0x00 0x00 cannot appear within the value of a primitive indefinite-length type, as it conflicts with the end-of-content marker. However, for constructed types, 0x00 0x00 is allowed within the value if it appears as part of child indefinite-length TLVs.
"""
Assistant
0x00 0x00 cannot appear in the value of a primitive indefinite-length type.
It conflicts with the end-of-content marker.
For constructed types, 0x00 0x00 is allowed if it is part of child indefinite-length TLVs.
User
improve the readability while keeping as condensed:
"""
0x00 0x00 cannot be within value of a primitive indefinite-length type
it conflicts with end marker
Alternatives:
Use definite-length encoding (short/long form)
Escape 0x00 0x00 into other values
For constructed types, 0x00 0x00 is allowed if it is part of child indefinite-length TLVs
"""
Assistant
0x00 0x00 cannot appear in the value of a primitive indefinite-length type:
It conflicts with the end marker.
Alternatives:
Use definite-length encoding (short/long form).
Escape 0x00 0x00 to other values.
For constructed types, 0x00 0x00 is allowed if part of child indefinite-length TLVs.
User
"For constructed types, 0x00 0x00 is allowed if part of child indefinite-length TLVs"
improve this
Assistant
In constructed types, 0x00 0x00 is allowed when included in child indefinite-length TLVs.
User
this is better:
"""
For constructed types, 0x00 0x00 is allowed as it can be part of child type's value (length-definite) or EoC marker (length-indefinite)
"""
can you improve it more
Assistant
For constructed types, 0x00 0x00 is allowed, as it may appear as part of a child type's value (definite length) or as an EoC marker (indefinite length).
User
help make it shorter
Assistant
For constructed types, 0x00 0x00 is allowed as part of a child value or EoC marker.
Dataset releases
Not yet included in a published release.
62 61 72
Final encoding: 04 03 62 61 72
Inner Octet String (Constructed):
This Inner Octet String is constructed, and its value contains the two Sub-inner Octet Strings (Primitive) we just encoded.
If elements have a definite length (i.e., the length is encoded as the number of bytes in the element), the BER encoding does not require an EOC marker because the length of the content is already clearly specified.
In this case, the lengths of the Sequence and Octet String are explicitly specified, so there is no need for EOC markers.
Summary
EOC (00 00) is used to terminate elements with indefinite length.
It signals the end of content for both simple (e.g., Octet String) and constructed (e.g., Sequence) elements.
For elements with definite length, EOC is not needed since the length is explicitly encoded.
Set
Conditionally Constructed Types:
String types (can be primitive or constructed depending on encoding rules).
Any (depends on the actual value, primitive or constructed).
Custom types (depends on the schema definition, primitive or constructed).
Revised Statement:
To make your statement more precise, you could rephrase it as:
"Constructed types typically include Sequence, Set, and any custom types defined as constructed. String types (UTF8String, PrintableString, etc.) and Any can be either primitive or constructed, depending on the encoding rules and schema definition."
This ensures clarity across different use cases.
SequenceOf and SetOf:
These are considered constructed by definition (representing collections of ordered or unordered elements).
They are distinct types in ASN.1 and not automatically implied by "Sequence" or "Set".
Explicitly Tagged Types:
Any EXPLICIT tagging in ASN.1 introduces a level of construction, even if the underlying value is primitive. For example:
asn.1
[APPLICATION 5] EXPLICIT INTEGER
The above is a constructed type because of the explicit tagging.
Some Special String Encodings:
There are edge cases where a string type (e.g., a GraphicString, GeneralString, or other less commonly used encodings) might appear as constructed, typically due to fragmentation in BER's indefinite-length encoding.
External and Embedded PDV:
ASN.1 defines certain universal types, like EXTERNAL and EMBEDDED-PDV, which are inherently constructed but are often omitted in generic lists.
Refined and Complete Statement:
If you want to cover all constructed types explicitly, you could revise your statement as follows:
"Constructed types include Sequence, Set, SequenceOf, SetOf, explicitly tagged types, constructed string types, Any (when constructed), and custom types. Other universal constructed types include EXTERNAL and EMBEDDED-PDV."
TL;DR:
Your original list covers most of the common constructed types but misses some key ones, like SequenceOf, SetOf, explicitly tagged types, and special rarely used types (EXTERNAL, EMBEDDED-PDV).