For more information, see ASN.1/BER/DER encoding subset getting started (1)
2. Abstract Syntax tag (Abstract Syntax Notation One)
Abstract Syntax Notation One (Abstract Syntax Notation One) is a mark describing Abstract types and values, abbreviated as ASN.1.
In ASN.1, a type is a set of values. Some types have limited values, while others have unlimited values. The value of a given ASN.1 type is an element in this type set. ASN.1 has four types: simple type, which is equivalent to atoms and has no lower-layer components; structure type, which has components; label type, which is generated by other types; other types, including the CHOICE and ANY types. You can use the ASN.1 distributor (: =) to specify names for the type and value. These names can be used to define other types or values.
In addition to the CHOICE and ANY types, each ASN.1 type has a tag consisting of a class and a number of non-negative tags. Tag values can be unique in ASN.1. That is to say, the name of the ASN.1 type does not affect its abstract meaning. Only the tag value can play this role. There are four types of labels:
● Universal: the meaning of this type is the same in all applications. This type is only defined in X.208.
● Application: the meaning of this type is determined by the application, for example, the X.500 Directory Service. The types in two different applications can have the same application-specific tag but can have different meanings.
● Private: the meaning of this type varies with the given enterprise.
● Context-specific: the meaning of this type varies according to the given structure type. Context-specific labels are used to differentiate component types that use the same underlying labels in a given structure type Context. In two different structure types, the component type can have the same tag but different meanings.
The type with the universal tag is defined in X.208, and X.208 also provides the type of the universal tag value. The types of other labels are defined in many places, usually through the implicit or explicit tag. Table 1 lists some ASN.1 types and their universal-class labels.
(For clarity, the fields in the table are separated by underscores)
===== Type Tag ====================== number _ (decimal) ======== Tag_number _ (hexadecimal)
________ Integer____________________2_____________________________02 ________________
________ BIT_STRING_________________3_________________________03 ________________
________ Octet_string_________________4_________________________04 ________________
________ Null_____________________________5_________________________05 ________________
________ OBJECT identifier_____________________________________________06 ________________
________ SEQUENCE and SEQUENCE of1__16________________________10 ________________
________ SET and SET of____________17____________________________11 _________________
________ PrintableString ____________ 19________________________13 ________________
________ T61String __________________ 20________________________14 ________________
________ IA5String __________________ 22________________________16 ________________
________ UTCTime ____________________ 23________________________17 ________________
The ASN.1 type and value use a flexible Symbol Representation similar to a programming language. The rules are as follows:
Layering (line feed) has no special meaning; multiple spaces and multiple blank rows are equivalent to one space.
A comment starts with a pair of hyphens (--), or a pair of hyphens (-) and an empty line.
The identifier (value or field name) and type index (type name) consist of uppercase/lowercase letters, numbers, hyphens, and spaces. The identifier starts with a lowercase letter, A type index starts with an uppercase letter.
The following four subsections describe the simple type, structure type, implicit and explicit label type, and other types. Section 5th defines more details about types.
2.1 Simple type (Simple types)
A simple type has no components and is an atomic level. ASN.1 defines several simple types. The types related to the pkcs standard are as follows:
● Bit string: Any BIT stream composed of 0 and 1
● IA5String: a batch stream composed of any IA5 (ASCII) characters
● INTEGER: an arbitrary INTEGER.
● NULL: null Value
● Object identifier: an object identifier consisting of a column of integers used to determine an OBJECT, such as an algorithm or attribute type.
● Octet string: Any octet (8 bit value) stream
● PrintableString: any printable audio stream
● T61String: any stream of T.61 (8bit) characters
● UTCTime: "coordinated universal time" or Greenwich Mean time (GMT) value.
There are two simple types: string and non-string. Bit string, IA5String, octet string, PrintableString, T61String, and UTCTime are string types.
Considering the encoding, the String type can be regarded as composed of components, and the component is substring. In this way, even if you do not know the length of the value, you can use a structured, uncertain length encoding method (for example, the octet string value input from a file stream ).
You can specify the size limit for the String type to limit the length of the value.
2.2 Structured type (Structured types)
The structure type is composed of components. ASN.1 defines four types, all related to the PKCS standard:
● SEQUENCE: one or more types of Ordered Sets
● Sequence of: an ordered set OF 0 or a given type that appears multiple times
● SET: one or more unordered sets.
● Set of: 0 or a disordered SET that appears multiple times for a given type
The structure type can contain optional components. Optional components may have default values.
2.3 implicit and explicit tag types (Implicitly and explicitly tagged types)
In an application, tagging is very useful for distinguishing types. tagging is usually used to differentiate component types in a structure type. For example, optional components of the SET or SEQUENCE type generally have different context-specific labels to avoid confusion.
There are two ways to mark a type: Implicit (implicitly) and explicit (explicit ).
The implicit tag type is generated by changing the tag of its lower-level type based on other types. IMPLICIT tags are represented by the ASN.1 keyword [class number] IMPLICIT (see section 5.1.
An explicit tag is generated by adding an outer tag to its lower-level tag based on other types. In terms of effect, the explicit tag type is the structure type of a component, that is, the lower-layer type. The EXPLICIT tag is represented by the [class number] EXPLICIT keyword ASN.1 (see section 5.2.
Only the keyword [class number] is the same as the explicit tag, unless the ASN.1 type of the module is defined as an implicit tag by default. (The "module" is an advanced feature and is not included in the scope described in this document)
From the encoding perspective, the implicit tag type can be considered to be the same as the lower-layer type, unless the tag is different. The explicit tag type can be regarded as the structure type of a component. The component is of the lower type. Implicit labels can make the encoding shorter, but if the lower-layer type is uncertain, explicit labels must avoid ambiguity (for example, the lower-layer type is CHOICE or ANY ).
Other Types in ASN.1 include CHOICE and ANY. The CHOICE type indicates a consortium, which has one or more alternative options; the ANY type indicates ANY value of ANY type, any type may be defined in the registration using object identifiers or integer values.
(To be continued)