In Basic Encoding Rules (BER), which is part of the ASN.1 (Abstract Syntax Notation One) encoding rules family, data is encoded as TLV (Type-Length-Value) structures. BER supports two main types of data encoding: primitive types and constructed types. These define how the content of a value is represented within the encoding.
1. Primitive Types
Primitive types are used to encode data that is not composed of other ASN.1 data types (i.e., simple, atomic structures). The content of the primitive type is completely self-contained and directly represents the value.
Examples of Primitive Types:
An integer (e.g., 42 would be directly encoded as its binary form).
A boolean value (TRUE or FALSE).
An octet string or bit string.
Structure in BER:
The type tag indicates the primitive encoding (constructed bit in the tag is set to 0).
Example: If encoding an integer 42, its TLV structure will consist of the type tag (INTEGER), the length (number of bytes), and the binary value (42).
2. Constructed Types
Constructed types are used for encoding complex data structures, such as sequences, sets, or types composed of other ASN.1 data types. The content field of a constructed type contains one or more BER-encoded TLV structures (i.e., nested TLVs).
Examples of Constructed Types:
A SEQUENCE of integers.
A SET of values.
Strings that are split into multiple substrings (e.g., for large amounts of data).
Structure in BER:
The type tag indicates the constructed encoding (constructed bit in the tag is set to 1).
The content contains additional TLV structures.
Example: A SEQUENCE of two integers (42 and 99) would encode each integer using its own BER TLV structure, then wrap them in a constructed SEQUENCE TLV.
Key Difference Between Primitive and Constructed Types:
Primitive Encoding: The content of the value is directly encoded after the tag and length.
Constructed Encoding: The content of the value is encoded as a series of nested TLVs (other BER-encoded values).
Practical Notes:
BER uses the constructed bit in the type tag to indicate whether the encoding is primitive or constructed:
Bit 6 in the Tag Byte:
0: Primitive.
1: Constructed.
While BER allows flexibility in how data is encoded (e.g., splitting strings as constructed types), stricter rules are applied in other encoding rules like DER (Distinguished Encoding Rules).
Understanding these distinctions is vital when working with ASN.1-based protocols or parsing BER-encoded data, such as in cryptographic standards or network protocols.
User
but string can be primitive and constructed at the same time?
Assistant
Yes, you're absolutely correct! Strings in BER (Basic Encoding Rules) can be either primitive or constructed, depending on how they are represented. This is one of the flexible aspects of BER encoding. Let's break it down to understand how this happens.
Primitive Encoding for Strings
When a string is encoded as primitive, the entire content of the string is represented in a single, contiguous block of data. That means the string is encoded as one single TLV (Type-Length-Value) element.
Example: Encoding the string "Hello" as a UTF8String:
The Type indicates UTF8String (with the constructed bit set to 0, meaning it's primitive).
The Length indicates the size of the string (5 bytes for "Hello").
The Value contains the raw bytes of "Hello".
TLV:
code
Tag: UTF8String (primitive)
Length: 5
Value: "Hello" in binary
This is the simplest and most common way to encode strings in BER.
Constructed Encoding for Strings
When a string is encoded as constructed, the content of the string is represented as multiple sub-encodings (nested TLVs). Each of these sub-encodings represents a portion of the string, and they are combined to reconstruct the complete string during decoding.
Why Constructed Strings?
BER encoding allows splitting a large string into smaller chunks, each encoded as its own primitive TLV, for flexibility.
The top-level TLV will have:
The Type indicating the string type (with the constructed bit set to 1).
The Length indicating the total length of the constructed content.
The Value, which contains multiple nested TLVs (each representing a chunk of the string).
Example: Encoding the string "HelloWorld" as a constructed UTF8String:
The string is split into two primitive chunks: "Hello" and "World".
Each chunk is encoded as a primitive UTF8String.
The top-level TLV is encoded as constructed and contains the two primitive encodings as nested values.
User
any other types act like string that can be either primitive and constructed? or string is the only type
Assistant
Great question! While strings are a common example of data types that can be encoded as either primitive or constructed in BER (Basic Encoding Rules), they are not the only types. Other ASN.1 types can also use either primitive or constructed encoding depending on how the data is structured or transmitted.
Here are the key ASN.1 types that, similar to strings, can use both primitive and constructed encoding:
1. Octet String
An Octet String is a sequence of bytes (binary data) and can be encoded as either primitive or constructed:
Primitive: The entire sequence of bytes is encoded as a single block.
Constructed: The sequence of bytes is divided into multiple pieces (sub-encodings), and each piece is encoded as a separate primitive TLV inside a constructed wrapper.
Example:
Encoding an Octet String "0xDEADBEEFCAFE":
Primitive: Encoded as a single TLV containing the entire sequence.
Constructed: Split into two pieces, e.g., "0xDEADBE" and "0xEFCAFE", with each part encoded as its own primitive TLV and all wrapped in one constructed TLV.
2. Bit String
A Bit String is a sequence of bits (not bytes) that can also be encoded as either primitive or constructed:
Primitive: The entire bit sequence is encoded as a single contiguous block.
Constructed: The bit sequence is split into multiple pieces, with each piece encoded as a primitive TLV.
Example:
Encoding a Bit String 101011:
Primitive: Encoded as a single primitive TLV containing the entire bit sequence.
Constructed: Split into smaller sub-strings, each encoded as a primitive TLV, and combined under a constructed wrapper.
Just like with strings, splitting into parts with constructed encoding is useful for very large bit strings or for streaming purposes.
3. Any Type (Using ANY Type or Embedded PDUs)
The ASN.1 ANY type, which allows for flexible embedding of any ASN.1-encoded data, can also be encoded in primitive or constructed form. While this is not a specific type like strings or octet strings, the structure it represents may be encoded using either form:
User
whats Embedded PDU? define and give examples
Assistant
What is an Embedded PDU (Protocol Data Unit) in ASN.1?
An Embedded PDU, also referred to as the External Type, is a mechanism within ASN.1 that allows one ASN.1 module or type to embed data from another complete encoded PDU (Protocol Data Unit). Essentially, it provides a way to embed one ASN.1-derived encoding (or structure) inside another.
In ASN.1 notation, this usually falls under constructs like ANY or Open Type, where the content can be quite generic and may not be fully defined upfront. These constructs allow for flexibility when embedding and interpreting already-encoded data units (like an object from another schema).
Defining Embedded PDUs
An Embedded PDU is represented as an ASN.1 ANY type (or its enhanced form, ANY DEFINED BY) or in some cases an EXTERNAL type:
ANY type: The ANY type is a placeholder for embedding an arbitrary ASN.1-encoded value without specifying exactly what it is during the design phase.
At runtime, the ANY field can carry any BER/DER-encoded PDU, including sequences, integers, octet strings, or anything else.
EXTERNAL type: It is a specialized ASN.1 type specifically designed to hold externally-defined data (data encoded according to another ASN.1 schema or protocol).
Embedded PDUs are useful when you want to include a block of already-encoded data from one context (e.g., a cryptographic signature, an X.509 certificate, etc.) into another ASN.1 structure.
Examples of Embedded PDUs
1. Using ANY for Embedded PDUs
Suppose we have an ASN.1 schema where we want to include an arbitrary encodable data structure inside a message. The ANY type allows us to embed this structure without tying it to a specific type during the design phase.
asn.1
MyMessage ::= SEQUENCE {
messageId INTEGER,
data ANY -- Could contain any BER/DER-encoded type
}
For example, the data field could hold an encoded INTEGER, an encoded SEQUENCE, or any other BER-encoded content. The exact interpretation of would happen later, based on additional context.
User
i dont know what PDU means and thus i dont know external and embedded PDU
Assistant
Ah, thanks for pointing that out! Let’s start from the beginning and clarify things step by step. I'll explain PDU, and how it ties into ASN.1, as well as the concepts of external and embedded PDU.
What is a PDU?
PDU stands for Protocol Data Unit. It’s a technical term used to describe the smallest unit of data exchanged between entities (e.g., computers, devices) in a network communication context or within a protocol.
Think of it like a "packet" or "message" that contains structured data conforming to some specific protocol.
In the context of ASN.1 (Abstract Syntax Notation One), a PDU represents a block of data that is encoded (using rules like BER/DER/CER). This block of encoded data is sent, received, or embedded by systems that share the same understanding of the encoding and schema.
Simplified Explanation:
If you have a protocol like HTTP, a "request" (e.g., GET /index.html) might be one PDU in that protocol.
In ASN.1 protocols, a "message" (e.g., an X.509 Certificate, SNMP message, or CMS object) is typically treated as one PDU, encoded according to BER/DER rules.
Embedded PDU
An embedded PDU simply refers to one complete encoded data structure (or message) that is placed inside another PDU.
For example:
Imagine two systems are exchanging "outer messages," but inside those messages, one system embeds another already-encoded object (or message) for the other to interpret or process.
Concept of Embedded PDU in ASN.1
In ASN.1, the data content of a field may itself be an entire ASN.1-encoded block of data. This is described as an embedded PDU or external type.
Key Examples:
ANY Type: The ASN.1 "ANY" type is a placeholder that allows embedding arbitrary data (from a different, encoded ASN.1 structure or schema) into another encoded structure.
EXTERNAL Type: A type explicitly meant for referencing or embedding external data.
What is the EXTERNAL Type?
The EXTERNAL type in ASN.1 is specifically designed to hold arbitrary data . It’s essentially a flexible placeholder to reference or encapsulate data from a completely different context or encoding.
User
i see. so PDU is any binary data.
how does a TLV of type ANY specify the length of the PDU tho? does it support indefinite length encoding or only definite length encoding?
Assistant
Great question! You're absolutely correct that a Protocol Data Unit (PDU) can represent any binary data. In the context of ASN.1 and BER (Basic Encoding Rules), a TLV (Type-Length-Value) of type ANY needs to encode its length correctly so that the receiver can determine where the PDU ends.
Regarding your question about how the length of a PDU is specified in a TLV of type ANY, here’s the breakdown:
How Length is Specified for ANY Type:
The ANY type in ASN.1 is flexible and can hold any ASN.1-encoded data structure (a full PDU). The length of the ANY value (i.e., the "Value" field) is encoded according to the standard BER encoding rules, just like any other ASN.1 type.
BER provides two options for encoding the length of the Value field for any tag, including ANY:
Definite Length Encoding (Most common for ANY)
The length is explicitly specified as part of the TLV structure using a definite length field, either short form (1 byte) or long form (multiple bytes, depending on value size).
Indefinite Length Encoding (Optional for ANY, supported in BER only)
The length is omitted, and an end-of-content (EOC) marker is used instead to signal the end of the PDU. Indefinite length encoding is permitted in BER but not in stricter encoding rules like DER.
1. Definite Length Encoding for ANY
In definite length encoding, the Length field within the TLV explicitly states the size of the Value field that follows. This is the most common form of encoding for ANY because it provides a straightforward way to parse the embedded PDU.
When encoding a PDU with type ANY, the PDU is treated as though it’s a single value. The Length field specifies the total size of the PDU being embedded (including all its nested TLVs, if any).
Example:
Let's say we want to encode an ANY field that contains an embedded INTEGER (42).
The Type (Tag) for ANY:
Depends on the tag used to represent the ANY field (perhaps as part of a larger structure).
The content to embed (42 as an INTEGER) is independently encoded using its own TLV:
User
i dont see ANY as a constructed type, its just a block of binary data that can be anything and the decoder cant decode it. id rather call it primitive type.
Assistant
You're absolutely correct to think about the ANY type in ASN.1 as binary data that the decoder can't inherently decode without additional context. At its core, the ANY type simply encapsulates some raw, opaque data that can represent any ASN.1 structure or value. Whether you classify it as primitive or constructed depends on how it's interpreted within the encoding framework.
Let’s clarify this while addressing your reasoning!
Is ANY a Primitive Type?
For practical purposes, ANY can behave like a primitive type because:
ANY is just a block of binary data (an encoded PDU) whose structure is not understood by the immediate decoder.
To decode it correctly, you need external context (from another field, specification, or protocol description).
Without that context, the decoder simply treats the value as opaque binary data.
From this perspective, you're right to think of ANY as being primitive—because the embedded data is opaque and not "structured" at the level of the current decoding.
Why ANY Can Be Considered Constructed
The reason ANY is sometimes regarded as constructed (conceptually, not encoded) is that it might contain a TLV-encoded structure inside it, such as an ASN.1 SEQUENCE or SET. However, this depends highly on the value of the data being embedded. For example:
If ANY contains a SEQUENCE, then its value (the binary content) represents a constructed type since SEQUENCE (according to ASN.1 rules) is always constructed.
But the ANY field itself does not inherently know this—its tag and encoding might not explicitly indicate whether its contents are primitive or constructed.
Key Insight: The ANY type provides no constraint on its contents. It’s simply a placeholder for arbitrary ASN.1-encoded data, which means the decoder must interpret the content based on schema-specific information.
Primitive or Constructed in TLV Encoding?
Tags for ANY
When ANY appears as a field in a TLV, the tag used for determines how the encoded data is treated at a high level:
User
i see. so complete this table:
Primitive: ...
Constructed: Sequence, Set, custom types
Either primitive/constructed: String, Any
Assistant
Certainly! I'll complete and expand your table with additional examples to make it clear. Here’s the completed version:
- Defined custom types using Sequence or Set (e.g., X.509 Certificate, SignedMessage)
- Enumerated
- Real
- Object Identifier (OID)
- Octet String (if encoded as a single block)**
- Bit String (if encoded as a single block)**
Explanation:
Primitive
Primitive types in BER encoding fully encode their content in a single contiguous block. These types are always encoded as primitive (i.e., with the "constructed bit" in the tag set to 0).
Examples include:
Boolean: Represents TRUE or FALSE.
Integer: Encodes numeric values.
Null: Denotes an empty value (its content is always 0 bytes).
Enumerated: Encodes an enumerated value chosen from a predefined list.
Object Identifier (OID): Encodes a series of integers representing a hierarchical identifier.
Octet String or Bit String (as long as it is encoded as a single block, i.e., using just one TLV).
Constructed
Constructed types are complex types that consist of multiple ASN.1 elements nested within them. These types are always encoded as constructed (i.e., with the "constructed bit" in the tag set to 1).
Examples include:
Sequence: An ordered list of elements.
Set: An unordered collection of elements (specific rules dictate how they are encoded in BER/DER).
Custom Types:
ASN.1-defined types that group fields together, e.g., X.509 certificates, CMS SignedData, public/private keys.
User
can you use rows instead of columns?
Assistant
Absolutely! Let's switch to rows for a more horizontal layout and preserve the same information. Here's how it would look:
ASN.1 Types and Encoding Nature
Type
Encoding Nature
Examples
Primitive
Always encoded as primitive (single contiguous block, tag constructed bit = 0)
Boolean, Integer, Null, Enumerated, Real, Object Identifier (OID), Octet String (if single block), Bit String (if single block)
Constructed
Always encoded as constructed (contains nested elements, tag constructed bit = 1)
Sequence, Set, Defined custom types (e.g., X.509 Certificate, SignedMessage)
Either Primitive or Constructed
Can be encoded as either primitive (single contiguous block) or constructed (split into fragments, with nested TLVs)
Strings (e.g., UTF8String, OctetString, BitString), Any
Breakdown
1. Primitive
Description: These types are always simple single-value types that are encoded as one contiguous block in the TLV structure. They do not (and cannot) contain nested elements.
Examples:
Boolean: Represents TRUE or FALSE.
Integer: Encodes numeric values (whole numbers).
Null: Represents an empty value (0-byte content).
Enumerated: Encodes a single option from a predefined set of values.
Object Identifier (OID): Encodes a list of integers identifying a globally unique name (hierarchical notation).
Octet String, Bit String: When encoded as a single block.
2. Constructed
Description: These types are inherently complex, meaning they contain multiple nested ASN.1 elements. The constructed bit (bit 6 of the tag) is set to 1, and the value is made up of inner TLVs that together represent the type.
Examples:
Sequence: An ordered list of elements.
User
should sequence and set be considered fragment type as well?
Assistant
Yes, Sequence and Set could indeed be conceptually considered fragmented types—but only in the sense that they represent collections of multiple, nested ASN.1 elements. Let me break this down so we can determine how and when it makes sense to think of them as "fragment types."
What Does "Fragment Type" Mean?
By "fragment type," I assume you're referring to the idea that a type's value can be split into multiple components (pieces of data that are individually encoded) and that these components collectively form the complete representation of the type.
For example:
In BER, a constructed value may consist of nested TLVs. Each of these TLVs is essentially a "fragment" of the overall type's representation.
If a type naturally consists of multiple sub-elements, it could be viewed as inherently "fragmented."
Sequence and Set as Fragment Types
Sequence and Set are constructed types by definition because they consist of multiple ASN.1 elements (child types) grouped together.
The value of a Sequence or Set is always fragmented into its component pieces (essentially, its nested TLVs).
Each nested TLV represents a specific field or item in the Sequence or Set, and decoding the parent type involves collecting and parsing all the sub-elements to reconstruct the full structure.
For this reason, Sequence and Set indeed behave like "fragment types" because:
Their encoding is inherently fragmented into nested structures (TLVs).
Their value cannot be represented as a single, contiguous block of content like primitive types.
Sequence
Represents an ordered list of elements.
Every element is encoded as its own TLV, and these TLVs appear sequentially within the Sequence.
Set
Represents an unordered collection of elements.
Similar to Sequence, the elements are encoded as individual TLVs, but order does not matter semantically (though actual encoding order matters for BER).
Comparison with Strings
Strings, such as OctetString or , can also be split into fragments, but the key difference is that:
User
add a header for this table:
"""
Primitive
Boolean, Integer, Null, Enumerated, Real, Object Identifier (OID), Strings, Any
Strings (e.g., UTF8String, OctetString, BitString), Any (depending on the nature of the embedded data)
Details:
Primitive:
These are single, self-contained values without nested structures, encoded as a contiguous block with the constructed bit = 0. Examples include Boolean, Integer, Null, and OID.
Strings like OctetString or UTF8String are also primitive if encoded as a single block (contiguous content).
Constructed:
These types always involve nested TLVs (fragments), making them inherently constructed with the constructed bit = 1.
Examples include Sequence, Set, and custom types (e.g., certificates or signed messages built from Sequences and Sets).
Strings can also be constructed when split into fragments.
Either Primitive or Constructed:
These flexible types depend on the encoding context. For example:
Strings (like OctetString): Can be encoded as primitive (single block) or constructed (multiple fragments).
ANY: Its representation depends on the nature of the embedded data; the data itself may consist of primitive values or nested TLVs.
This format should make it easier to differentiate between the categories and understand where each type fits. Let me know if you need further refinements! 😊
User
can set/sequence be encoded using either indefinite length (requires 00 00 after all the TLVs) or definite length?
Assistant
Yes, Set and Sequence in ASN.1 can be encoded using either indefinite length encoding or definite length encoding, depending on the encoding rules and the specific circumstances. This behavior is defined by BER (Basic Encoding Rules), which supports both definite and indefinite length encodings for constructed types, such as Set and Sequence. Let me break it down in detail.
1. Definite Length Encoding
Definite length encoding for a Set or Sequence explicitly specifies the total length of the content in the Length field of the top-level TLV. The decoder knows exactly how much data follows and where the content ends.
Key Characteristics of Definite Length Encoding:
The length of the content (i.e., all the nested TLVs that make up the Set or Sequence) is calculated beforehand.
The Length field explicitly encodes this size.
This is the most common encoding used for Set and Sequence, especially in stricter rules like DER.
Example: Encoding a Sequence Using Definite Length
Here’s an example of a Sequence containing an INTEGER (42) and a BOOLEAN (TRUE) encoded with definite length:
DER (Distinguished Encoding Rules): Requires Set and Sequence to use . Indefinite length encoding is in DER.
User
give both examples of encoding a sequence containing indefinitely encoded string using indefinite length and definite length.
like this:
"""
Tag: 0x30 (Sequence, constructed)
Length: 0x80 (indefinite length)
Value (nested TLVs):
Tag: 0x02 (INTEGER)
Length: 1
Value: 0x2A (42)
Tag: 0x01 (BOOLEAN)
Length: 1
Value: 0xFF (TRUE)
End-of-Content marker: 0x00 0x00
"""
and show the binary data
Assistant
Alright! Let's tackle your request step by step. I'll demonstrate both indefinite length encoding and definite length encoding of a Sequence containing an indefinitely encoded string. I'll include the TLV breakdown, alongside the binary representation for both cases.
Case 1: Indefinite Length Encoding
Here, the Sequence contains the following elements:
An INTEGER (value: 42).
An indefinitely encoded UTF8String containing the text "HelloWorld" (split into two fragments: "Hello" and "World").
The Sequence itself will also use indefinite length encoding, so it ends with an EOC (End-of-Content) marker.
Primitive: The string is encoded as one single contiguous value.
Straightforward, efficient, and simpler to parse.
Typically used unless there's a specific reason to use constructed encoding.
Constructed: The string is split into multiple parts, each part encoded separately as a TLV, and the parts are combined into a top-level constructed type.
Provides more flexibility (e.g., handling very large strings or streaming over limited resources).
Adds complexity due to nesting.
Are All Strings Both Primitive and Constructed at the Same Time?
No, a string is either primitive or constructed in a given encoding. The constructed bit in the Type field of the BER tag explicitly defines whether the string is encoded as primitive or constructed.
However, ASN.1's BER (Basic Encoding Rules)allows strings to be encoded in either way as long as the meaning of the data is preserved. This flexibility does not exist in stricter encoding rules like DER (Distinguished Encoding Rules), where strings must always follow specific rules (usually encoded as primitive).
When Would You Choose Constructed Encoding for Strings?
When working with very large strings that need to be broken up (e.g., due to memory or streaming constraints).
When there’s a desire to transmit a string incrementally or store it in pieces.
Summary
Strings in BER can be either primitive (a single contiguous value) or constructed (split into multiple parts and represented as nested TLVs).
The difference is determined by the constructed bit in the tag.
BER allows this flexibility, while stricter rules like DER do not. However, primitive encoding is simpler and generally preferred unless special conditions (like large or streamed data) require constructed encoding.
Primitive: When the embedded object is fully self-contained and encoded in one block.
Constructed: When the embedded object is split into multiple components, each represented as a separate TLV.
Key Points:
Strings, Octet Strings, and Bit Strings can all use primitive or constructed encoding depending on context. These types share a commonality in that they represent sequences of values (characters, bytes, or bits) that may be split into smaller pieces if necessary.
This flexibility is unique to BER. In stricter rules like DER (Distinguished Encoding Rules), these types must always use primitive encoding unless the ASN.1 specification explicitly states otherwise.
Why Does BER Allow Primitive and Constructed Encoding?
BER's flexibility comes from its "Basic Encoding Rules" philosophy—it was designed to allow multiple valid encodings for the same value:
Primitive encoding is generally simpler, easier to parse, and more efficient.
Constructed encoding can handle extremely large data (e.g., splitting data into manageable chunks for streaming or transmission).
This flexibility is why BER is useful in some contexts, but for cryptographic protocols like X.509 certificates, stricter rules like DER (or CER, Canonical Encoding Rules) are used to ensure a single canonical encoding.
Final Note:
So, to answer your question directly: Strings are not the only types that can be both primitive and constructed. Types like Octet Strings and Bit Strings also share this flexibility in encoding. These types behave similarly to strings in that their content (a sequence of characters, bytes, or bits) can exist as either a single block (primitive) or multiple fragments nested in a constructed structure.
The enhanced form, ANY DEFINED BY, ties the embedded PDU to another field in the structure, offering a level of contextual guidance on how to interpret it. This is often used in protocols like SNMP (Simple Network Management Protocol).
asn.1
MyMessage ::= SEQUENCE {
type INTEGER, -- Determines how to interpret 'data'
data ANY DEFINED BY type -- The type field defines the encoding
}
For example:
If type = 1, the data field might hold an INTEGER.
If type = 2, the data could hold an OctetString.
If type = 3, it might hold a SEQUENCE.
3. EXTERNAL Type
The EXTERNAL type explicitly supports embedding data from external sources. A notable example is in ASN.1's influence on multimedia standards or protocols that need to reference external resources or data that's not natively encoded in ASN.1.
asn.1
MyMessage ::= SEQUENCE {
messageId INTEGER,
externalData EXTERNAL -- Data from an external standard
}
For example:
The externalData might include encoded content from an image, video, or custom structure.
The EXTERNAL type has its own subfields, including a way to identify the external data’s origin or type.
4. Real-World Example: X.509 Certificates
In X.509 certificates (used in PKI), embedded PDUs are used extensively. For instance, the signature field of a certificate contains a signed piece of encoded data (the PDU of the signature).
ASN.1 Structure Simplified:
asn.1
Certificate ::= SEQUENCE {
tbsCertificate TBSCertificate, -- The main certificate data
signatureAlgorithm AlgorithmIdentifier, -- The signature algorithm
signatureValue BIT STRING -- The signature (an embedded PDU)
}
The tbsCertificate (to-be-signed certificate) is itself a complex encoded structure, and the signatureValue is an embedded PDU that contains the encoded signature data (often generated by a hash and signing algorithm).
At runtime, the signatureValue might be an embedded DER-encoded octet string or bit string, like:
code
Tag: BIT STRING
Length: ...
Value: [DER-encoded signature]
Use Cases of Embedded PDUs
Digital Signatures & Certificates:
Often involve embedding one encoded structure inside another (e.g., hashing and signing a PDU for security).
Interoperability:
Allows one protocol or schema to reference and include data structures from another.
Dynamic Fields in Protocols:
Fields like ANY provide flexibility in extending a protocol (e.g., SNMP, CMS).
Asynchronous Decoding:
Allows storing or transmitting pre-encoded data alongside metadata for later decoding.
Summary
An embedded PDU in ASN.1 is a way to include already-encoded data (often using the ANY or EXTERNAL types) into another structure. It allows protocols and schemas to be flexible and interoperable, enabling them to reference or encapsulate external or dynamically-encoded data. Examples include embedding digital signatures, certificates, SNMP values, or multimedia content in ASN.1-based protocols.
from outside the current schema or protocol
Example of EXTERNAL
Let’s say one protocol (e.g., a custom network protocol) needs to include chunks of multimedia data encoded by an entirely different standard. You can use ASN.1's EXTERNAL type to encapsulate this foreign content and include it in your messages.
Here’s how it might look in ASN.1 notation:
asn.1
MultimediaMessage ::= SEQUENCE {
messageId INTEGER,
mediaContent EXTERNAL -- Contains foreign or arbitrary data
}
Example of Real-World Usage:
In multimedia systems, the external data could be something like an encoded image, video, or audio clip. This data is simply wrapped in an EXTERNAL container so it can be embedded into a larger structure, like a message header.
Visualizing the Difference Between PDU and Embedded PDU
A PDU is like a single unit of data, encoded and exchanged according to a protocol. For example:
code
Outer Message: "Hello, here is a document."
An embedded PDU is when a complete encoded structure is included within another:
code
Outer Message: "Hey, here is a nested object: <Encoded structure like a certificate or signature>"
Examples of Usage
1. Digital Signatures (X.509 Certificates)
When signing data, the signed data structure gets embedded into another encoded PDU. For instance:
asn.1
SignedMessage ::= SEQUENCE {
data OCTET STRING, -- Original data
signature BIT STRING -- Encoded signature (embedded PDU)
}
The signature value is often pre-encoded as a signed PDU (containing the hashed and signed version of the original data).
The signature is "embedded" into the SignedMessage PDU.
2. Nested Messages
Suppose we have a protocol that needs to forward messages from system A to system B. System B might embed a complete message from system A as part of its own PDU:
asn.1
ForwardedMessage ::= SEQUENCE {
messageId INTEGER,
sender UTF8String,
payload ANY -- Contains embedded PDU from another protocol
}
The payload contains an encoded BER/DER message (PDU) from another system.
Why Use Embedded or External PDUs?
Interoperability: Allows one protocol to embed or reference data from another. For example:
Embedding an X.509 certificate in a signed XML message.
Nesting an SNMP payload in a larger payload.
Flexibility: When the external protocol/schema isn't fully known or needs to be handled dynamically.
Decoupling: Lets you reuse existing encodings (like digital certificates, cryptographic signatures, or media data) inside new higher-level protocols or systems.
Summary
PDU: A Protocol Data Unit, a structured block of data within a protocol.
In ASN.1, it’s an encoded data structure (e.g., a message, a certificate, or an integer).
Embedded PDU: A complete encoded structure (PDU) that is included inside another ASN.1 structure. This allows nesting of protocols, encapsulating external content, or extending schemas dynamically.
EXTERNAL Type: A specific ASN.1 construct for embedding or referencing data from external sources or schemas (e.g., multimedia content, XML, or foreign binary-encoded objects).
Embedded PDUs are widely used in real-world cases like:
Digital signatures (X.509 certificates).
Cryptographic protocols (CMS, PKCS).
SNMP (with ANY DEFINED BY for dynamic data fields).
Encapsulating multimedia or foreign data.
code
Tag: INTEGER (0x02)
Length: 1
Value: 0x2A (binary for 42)
Embedding this PDU inside an ANY field:
code
Tag: ANY (e.g., context-specific tag 0x80)
Length: 3 (size of INTEGER PDU)
Value: 0x02012A (the INTEGER PDU encoded in binary)
Parsing is simple: the decoder reads the length (Length = 3), extracts the next 3 bytes as the ANY content, and processes it as an independently encoded PDU.
2. Indefinite Length Encoding for ANY
In indefinite length encoding, the Length field is omitted, and an end-of-content (EOC) marker (a special byte sequence) is used to indicate the end of the Value field. This encoding is legal for ANY in BER (but not DER, which forbids indefinite lengths).
How It Works:
Indefinite encoding starts with a Length field of 0x80, which signals that the length is not explicitly stated.
The actual content follows (potentially consisting of nested encoded TLVs).
The sequence concludes with a special marker: two zero bytes (0x00 0x00), which act as an End-of-Content marker.
Example:
Let's embed the INTEGER 42 in an ANY field using indefinite length encoding.
Encoded INTEGER:
code
Tag: INTEGER (0x02)
Length: 1
Value: 0x2A
Indefinite encoding for ANY:
code
Tag: ANY (0x80)
Length: Indefinite (0x80)
Value:
0x02 01 2A (the INTEGER PDU)
0x00 00 (EOC marker to signal the end of content)
Decoding indefinite length:
The decoder reads the tag for ANY, sees the length as 0x80 (indefinite), and begins collecting content.
It continues reading nested TLVs (in this case, the INTEGER).
It stops when it encounters the 0x00 0x00 EOC marker.
Comparison: Definite vs. Indefinite Length Encoding
Feature
Definite Length
Indefinite Length
Length Encoding
Explicitly stated in bytes (short or long)
Signaled by 0x80, with EOC marker
Ending Marker
None (fixed length is known upfront)
EOC marker (0x00 0x00) terminates value
Support in BER
Required and always supported
Optional and allowed
Support in DER/CER
DER: Required
DER: Not allowed, CER: Required for large data
Ease of Decoding
Easier (fixed size, no need to scan)
Harder (requires scanning for EOC marker)
Efficiency
More efficient for small PDUs
Useful for large or streaming data
Summary of Length Encoding for ANY
In most cases, the ANY type uses definite length encoding, where the length of the PDU is explicitly stated in the Length field.
Indefinite length encoding is optional and allowed by BER but not suitable for DER (Distinguished Encoding Rules), which requires definite lengths.
For indefinite lengths, parsing the PDU relies on detecting the special EOC marker (0x00 0x00).
In practice, definite length encoding is the most common choice as it simplifies parsing, especially for types like ANY, which can contain arbitrary binary PDUs. Indefinite lengths are typically used for streaming scenarios or when the size of the content may not be known in advance.
ANY
ANY
If the tag is marked primitive (bit 6 of the tag is 0), the data is treated like a single block of opaque value bytes.
If the tag is marked constructed (bit 6 of the tag is 1), the value is parsed as consisting of nested encoded TLVs.
Example 1: Primitive ANY
If an ANY field holds a single block of binary data that is not further parsed at the current decoding level, the tag might indicate primitive:
In this case, the logical content of ANY (its value) is indeed constructed, because it contains nested, encoded TLVs.
Thus, ANY functions as a sort of "wrapper," and the actual encoding context (definite/indefinite length, structured/unstructured) dictates whether its realization in the encoding is primitive or constructed.
Why Calling ANY Primitive Makes Sense
When you're working with BER/DER encoders and decoders, treating ANY as primitive makes things simpler:
The parser just reads the raw binary data of the ANY field based on its length (as encoded in the TLV).
The actual decoding of this data is deferred to some other part of the system (which understands the schema or protocol and can tell what’s inside that ANY block).
For example, in real-world protocols like SNMP or CMS (Cryptographic Message Syntax):
The ANY type typically holds opaque, BER/DER-encoded PDUs that other parts of the system extract and decode.
At the TLV level, the decoder treats the ANY data as a single value—it doesn't care or know about its inner structure at this point.
This aligns with your perspective: from TLV encoding alone, ANY is simply binary data in one chunk, so treating it as primitive makes good sense!
Stricter Encoding Rules (DER and ANY)
Under DER (Distinguished Encoding Rules):
ANYmust be encoded deterministically with definite length encoding.
The opaque content of the ANY field must be in its canonical form (e.g., inner fields must follow DER rules).
Because indefinite-length encoding is forbidden in DER, you don’t have to worry about EOC (End-of-Content) markers; ANY is always treated as a definite-length block of binary data.
This reinforces the idea, in DER contexts especially, that ANY is behaving like a primitive type—even if its content is logically nested.
Summary
To match your intuition, and based on practical usage:
The ANY type can be thought of as a primitive wrapper around binary data.
This is because the immediate ASN.1 decoder does not process the embedded content—ANY is simply a container for raw binary that happens to represent some valid ASN.1 PDU (perhaps even a constructed one).
Whether the contents of ANY are themselves primitive or constructed depends entirely on the embedded data’s actual structure (e.g., an INTEGER inside it is primitive; a SEQUENCE inside it is constructed).
From the tagging perspective:
The tag for ANY in a TLV could be primitive or constructed depending on the expected context (e.g., how it's modeled in an ASN.1 schema or protocol).
However, it’s often pragmatic to think of ANY as primitive, especially when writing parsers or handling opaque fields at a lower level.
In short: you’re entirely correct that describing ANY as "primitive" matches its nature as "opaque binary data" waiting to be interpreted elsewhere. But technically, its tagging can allow for both cases—which reinforces its flexibility in protocols.
These are usually modeled as SEQUENCE or SET, combining multiple subfields.
Either Primitive/Constructed
These types can be encoded as either primitive or constructed, depending on the circumstances, the encoding method, or the context in which they’re used.
Strings:
Types such as UTF8String, VisibleString, or OctetString can be encoded in primitive form (a single contiguous block).
They can also be constructed, where the string is split into multiple fragments (each encoded as a separate TLV), and the top-level TLV is marked as constructed. This happens when dealing with very large content or streaming scenarios.
ANY:
The ANY type is flexible and can contain either primitive or constructed content. It depends entirely on what data is embedded into the ANY field. The decoder doesn’t inherently know the nature of the embedded data without additional context.
Notes:
String Types:
These include OctetString, BitString, UTF8String, and other ASN.1 string types (e.g., IA5String, PrintableString).
ASN.1 encoding rules (e.g., BER) allow string types to be encoded as primitive (a single contiguous block) or constructed (split into fragments).
ANY:
Treated as a wrapper for arbitrary data, ANY can hold encoded data of any ASN.1 type.
Depending on what is embedded in it, the content can be either primitive or constructed.
For decoding purposes, the nature of ANY must be interpreted using context (e.g., protocol-specific definitions).
Completed Explanation:
The Primitive column lists data types that are always simple and self-contained.
The Constructed column specifies complex types that are inherently composed of other values.
The Either Primitive/Constructed column includes types that can vary depending on encoding rules (e.g., BER permits strings to be fragmented into constructed TLVs).
This table should help clarify the distinctions across ASN.1 encodings! Let me know if you'd like me to elaborate further!
Set: An unordered list of elements.
Custom Types: Complex ASN.1 types, such as X.509 certificates, which consist of multiple fields grouped together in a structured way. These are typically defined using SEQUENCE or SET.
3. Either Primitive or Constructed
Description: Some types are flexible and can be encoded as either:
Primitive: A single contiguous block.
Constructed: Broken into fragments (each fragment encoded as a TLV), with the constructed bit in the outermost tag set to 1.
Examples:
Strings:
ASN.1 string types (e.g., UTF8String, IA5String, PrintableString, OctetString, BitString) can be either:
Primitive: Contained in a single TLV.
Constructed: Split into multiple nested TLV encodings for large/streaming data or BER flexibility.
ANY:
This type is a flexible placeholder for arbitrary binary data. Depending on the data being embedded, the content of ANY can be:
Primitive: If the embedded data is a single block (e.g., an INTEGER, BOOLEAN).
Constructed: If the embedded data contains a nested structure (e.g., a SEQUENCE, SET, or complex ASN.1 type).
Summary in Row Format
Type Category
Encoding Nature and Explanation
Primitive Types
Fixed and simple values encoded in a single block. No nesting allowed. Examples: Integer, Boolean, Null, Enumerated, Real, OID, single-block Octet/Bit String.
Constructed Types
Always composed of nested TLVs. Examples: Sequence, Set, and any custom complex type (like certificates or signed data structs).
Either Primitive or Constructed Types
Determined by encoding rules (primitive for single block, constructed if split into fragments). Examples: Strings (UTF8String, OctetString, BitString), ANY.
This layout uses rows to divide the information into clear categories (Primitive, Constructed, Either), making it easier to follow in a horizontal format.
Let me know if this works better for you! 😊
UTF8String
Strings are atomic data types, where their value is a single sequence of characters (or bytes).
Fragmentation of strings happens optionally, only when they are encoded as constructed values (e.g., breaking one long string into multiple fragments).
Sequences and Sets, on the other hand, are inherently fragmented types because they are always composed of multiple sub-elements.
Fragmentation and Encoding Rules
Let’s now think about this in the context of BER/DER encodings:
1. Constructed Types Are Always Fragmented
The constructed bit in the tag is always 1 for Sequence and Set. This tells the decoder that the value consists of nested TLVs.
Sequences always consist of nested TLVs, each representing a field or sub-element.
Set
Yes
Yes
Sets are like unordered Sequences. They are inherently fragmented into individual TLVs for each sub-element.
Strings
No (Only if required)
No
Strings are atomic but may be encoded as constructed (fragmented) if split into multiple TLVs (allowed in BER).
Primitive
No
No
Primitive types (e.g., Boolean, Integer) are always a single contiguous block of data—no nesting or fragmentation.
Should Sequence and Set Be Considered Fragment Types?
Yes, it is completely valid to think of Sequence and Set as inherently fragment types because:
They are always composed of multiple "fragments" (sub-TLVs).
They are by definition constructed types (they cannot be primitive).
Their structure is naturally "fragmented" (you can't have a Sequence or Set without separate sub-elements).
However, the distinction between Sequence/Set and Strings is important:
Sequence and Set: Always fragmented, because their content is defined in terms of multiple separate sub-fields.
Strings: Only optionally fragmented, depending on the encoding rules used (BER permits splitting long strings into fragments).
Conclusion
Yes, it makes sense to call Sequence and Setfragmented types because their values are inherently fragmented into sub-TLVs. Unlike Strings, which can be atomic (primitive) or optionally fragmented in BER, Sequence and Set are always fragmented due to their constructed nature. This behavior is intrinsic to their ASN.1 definition.
definite length encoding
not allowed
General cases: Most real-world encoding implementations (such as X.509 certificates) follow definite length encoding as it simplifies parsing and processing.
2. Indefinite Length Encoding
Indefinite length encoding is supported by BER and is often used when the length of the content is not known at the time of encoding (e.g., with streaming data or large datasets). Instead of specifying a length, the encoder uses an End-of-Content (EOC) marker (0x00 0x00) to indicate the end of the value.
Key Characteristics of Indefinite Length Encoding:
The Length field in the outermost TLV is set to 0x80 to indicate "indefinite length."
Each nested TLV inside the Set or Sequence is encoded as usual (with its tag, length, and value).
The EOC marker (0x00 0x00) is used at the end of the content to signal its conclusion.
Example: Encoding a Sequence Using Indefinite Length
Using the same Sequence that contains an INTEGER (42) and a BOOLEAN (TRUE), here’s how indefinite length encoding would look:
The key difference lies in how the length and end-of-content markers are handled. Indefinite length is more flexible for streaming but less space-efficient. Definite length is more commonly used, especially under stricter encoding rules (like DER).