Introduction

ASN.1 is an interface description language for cross-platform data serialization. Many people may not have heard of ASN.1, but I believe that students who have cross-platform programming experience may have heard of protocol buffers and Apache Thrift. Although ASN.1 is not so famous compared to the above two languages, ASN.1 appeared much earlier than them, as early as 1984 ASN.1 appeared.

Compared with them, ASN.1 does not provide a single open source implementation, but is implemented as a specification for third-party vendors. ASN.1 is mainly used to define various basic protocols, such as commonly used LDAP, PKCS, GSM, X.500, etc.

ASN.1 is a platform and language-independent description language. Many ASN.1 translation tools can be used to translate ASN.1 into C, C++, Java and other codes.

Example of ASN.1

Since ASN.1 is a description language, let's look at an intuitive example first. The basis of ASN.1 is module, let's take a look at the example of module in ASN.1:

 StudentCards DEFINITIONS AUTOMATIC TAGS ::= BEGIN

StudentCard ::= SEQUENCE {
dateOfBirthday DATE,
student    StudentInfo
}

StudentInfo ::= SEQUENCE {
studentName    VisibleString (SIZE (3..50)),
homeAddress Address,
contactPhone   NumericString (SIZE (7..12))
}

Address::= SEQUENCE {
street  VisibleString (SIZE (5 .. 50)) OPTIONAL,
city    VisibleString (SIZE (2..30)),
state   VisibleString (SIZE(2) ^ FROM ("A".."Z")),
zipCode NumericString (SIZE(5 | 9))
}

END

In the above example, we use ASN.1 to define a StudentCard, and the outermost layer surrounded by BEGIN and END is the module. StudentCards is the name of the module, the first letter must be capitalized.

where ::= is an assignment symbol.

There can be multiple types in a module, and the name of the type must also be capitalized, such as StudentCard, StudentInfo, etc. above.

Each type defines its components. The first letter of the component name must be lowercase. The names of these components are also called identifiers.

The DATE followed by dateOfBirthday above is a built-in type in ASN.1. The StudentInfo behind the student is a custom type and is also included in the module.

studentName in StudentInfo is a VisibleString, and the limit of this String is between 3 and 50 in size.

When we defined module above, we added AUTOMATIC TAGS after module. What does this mean?

In ASN.1, tags are the internal identifiers of each component in ASN.1 messages. Taking Address as an example, we want to assign an internal identifier to each attribute in Address, as shown below:

 Address::= SEQUENCE {
street  [0] VisibleString (SIZE (5 .. 50)) OPTIONAL,
city    [1] VisibleString (SIZE (2..30)),
state   [2] VisibleString (SIZE(2) ^ FROM ("A".."Z")),
zipCode [3] NumericString (SIZE(5 | 9))
}

The [0] [1] is the identifier. Of course, we can manually specify these tags when defining the module, but if we use AUTOMATIC TAGS , these identifiers will be created automatically, thus avoiding the need for Possible problems with manually creating identifiers.

Built-in types in ASN.1

Through the above explanation, we have a basic concept of ASN.1. If we want to dig deeper into ASN.1, we first need to know the built-in types in ASN.1.

Generally speaking, ASN.1 has the following data types:

  • BOOLEAN

BOOLEAN is consistent with boolean values in programming languages, and it has two possible values: TRUE and FALSE. The following is the specific usage:

 removed BOOLEAN ::= TRUE
  • INTEGER

INTEGER represents an integer, as shown below, represents a year range from 0 to 100, and the final value is 18:

 age INTEGER (0..100) ::= 18
  • BIT STRING

The bit representation method of a byte, you can set a value for each bit in a byte:

 Status ::= BIT STRING {
married(0),
handsome(1),
kind(2)
}
myStatus Status ::= {handsome, kind}

In the above example, we set Status and assign it to a variable myStatus using Status.

  • OCTET STRING

String in octal representation:

 octetExample ::= OCTET STRING
  • DATE

Represents a date in the format "YYYY-MM-DD":

 birthday DATE ::= "1990-11-18"
  • TIME-OF-DAY

Represents the time in the date in the format "HH:MM:SS":

 startTime TIME-OF-DAY ::= "09:30:00"
  • DATE-TIME

The format of the time plus date, which is in the format "YYYY-MM-DDTHH:MM:SS", as follows:

 endTime DATE-TIME ::= "2022-01-10T18:30:23"
  • REAL

REAL represents a floating-point number, which can be represented as follows:

 Amount ::= REAL
  • ENUMERATED

ENUMERATED represents an enumeration, which can be represented as follows:

 Colors ::= ENUMERATED {black, red, white}
myColor Colors ::= white
  • SEQUENCE

SEQUENCE represents a collection of sequences of items, as follows:

 StudentInfo ::= SEQUENCE {
name VisibleString,
phone NumericString
}
max StudentInfo ::= {name "J.Max", phone "18888888888"}
  • SEQUENCE OF

SEQUENCE OF represents a list:

 breakTimes SEQUENCE OF TIME-OF-DAY ::= {"10:00:00", "12:00:00", "14:45:00"}
  • CHOICE

CHOICE means to choose one of many items:

 Identity ::= CHOICE {
name VisibleString,
phone VisibleString,
idCard VisibleString
}
jack Identity ::= name: "jack"
  • IA5String

IA5String represents ASCII characters and contains control characters.

 SampleString ::= IA5String
  • VisibleString

VisibleString represents ASCII characters, which do not contain control characters.

 SampleString ::= VisibleString
  • NumericString

NumericString represents numbers and spaces.

 SomeNumber ::= NumericString
  • UTF8String

UTF8String represents Unicode characters

 UnicodeString ::= UTF8String
  • NULL

is a null value, used as a placeholder.

Restriction Syntax in ASN.1

Many fields can be defined in ASN.1, and some fields may have some restrictions, such as mobile phone numbers can only use numbers, and names have length restrictions.

These restrictions are called Constraints in ASN.1, and generally have the following restrictions:

  • FROM

FROM provides a read range of data values as follows:

 PermittedChars ::= IA5String (FROM("ABCDEFG1244"))

PermittedChars only allow selection from "ABCDEFG1244".

  • PATTERN

PATTERN represents a regular expression, as follows:

 phoneNumber ::= IA5String (PATTERN "1[0-9]#10")

Listed above is a simple regular expression for mobile phone numbers.

  • SIZE

SIZE can represent the length of a string or the length of an array:

 Name ::= IA5String (SIZE (4..7))
       NameList ::= SEQUENCE SIZE (1..25) OF Name
  • RANGE

Using .. can represent a range:

 Age ::= INTEGER (0..100)
  • single value

Pick one from the list of provided values:

 Colors ::= UTF8String ("Blue" | "White")

Summarize

The above is the basic introduction of ASN.1 data structure description language. With these foundations, we can easily understand the data structure described by ASN.1.

For more information, please refer to http://www.flydean.com/46-asn-1/

The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!

Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!


flydean
890 声望433 粉丝

欢迎访问我的个人网站:www.flydean.com