ASN.1 coding study Note 1

Source: Internet
Author: User
Tags modulus

1. What is ASN.1 (Abstract Syntax 1 )?

ASN.1 is a standard set of ITU-T used to encode and represent common data types, which have printable string values, octal string values, bit string values, integer and other types of sequence values that are combined by shift. in short, ASN.1 specifies the method used to encode non-trivial data types so that any other platform or third-party tool can interpret the content. for example, the letter A is in some platforms with ASCII Encoding As the decimal value 97, while on other non-ASCII platforms, it may be another encoding. ASN.1 specifies a encoding method. on any platform, the letter A is encoded in a unified manner.

 

 

2. ASN.1 syntax.

ASN.1 syntax follows the traditional BNF style of the bucos paradigm. the most basic expression is name: = type. defines an element named name. Its type is type. for example, myname: = ia5string. defines an element or variable named myname. Its type is ASN.1 type ia5string (similar to ASCII string ).

 

2.1 ASN.1 explicit value (explict value ).

In some cases, we need to define an ASN.1 type. Its subset element contains a predefined value. name: = type (explict value ). explicit value (explict value ). it must be a value allowed by the ASN.1 type, and must also be a value allowed by the element. for example, myname ::= ia5string (Tom) indicates that myname is the ia5string encoding of the string Tom. for example, myname ::= ia5string (Tom | Joe) indicates that the string value can be either Tom or Joe.

This syntax is used to extend the fixed decoder. For example:

Publickey: = SEQUENCE {

Keytype Boolean (0 ),

Modulus integer,

Pubexponent integer

}

 

Privatekey ::= SEQUENCE {

Keytype Boolean (1)

Modulus integer,

Pubexponent integer,

Privateexponent integer

}

 

2.2 ASN.1 container (container)

A container is a value that contains other data types of the same or different types of elements (such as sequence value sequence or set value set type ). the purpose is to combine some complex data type sets. the ASN.1 Specification defines four container types: sequence, single sequence of, set ). although they have different meanings, the syntax is the same.

Name: = container {name type [name type...]} The content in square brackets and the number of elements in the container are optional. nested definitions can also be performed.

Example:

Userrecord: = SEQUENCE {

Name SEQUENCE {

First ia5string,

Last ia5string

},

DOB utctime

}

The structure in the C language is roughly translated as follows:

Struct userrecord {

Struct name {

Char * first,

Char * Last

};

Time_t DOB;

}

It is roughly translated into the following records in the Object Pascal Language (Object Pascal does not support nested records ):

Type

Name = record

First: string;

Last: string;

End;

Userrecord = record

Aname: Name;

DOB: datetime;

End;

 

2.3asn.1 Modifier

ASN.1 defines various modifiers, such as optional (optional), default, and choice ). they can change the expression declaration. it is typically used to define a type that requires flexible encoding and is not cumbersome to define.

<1>. Optional (optional ). As the name suggests, it indicates changing an element so that its type is optional During encoding. that is, the encoder can ignore this element. The decoder cannot assume that it will appear. however, when the two adjacent elements have the same type, the decoder may encounter some problems.

Definition: Name: = type optional

For example:

Float ::= SEQUENCE {

Exponent integer optional,

Mantissa integer,

Sign Boolean

}

When the decoder reads this structure, the first integer may be exponent or mantissa. We do not recommend this method to define the structure.

<2>. default ). default modifier allows the container to include default values. if the data value to be encoded is equivalent to its default value, it will be ignored in the sent data stream. for example:

Command: = SEQUENCE {

Token ia5string (NOP) default,

Parameter integer

}

If the encoder regards the token as a string NOP, the sequence is encoded as follows:

Command: = SEQUENCE {

Parameter integer

}

<3>. select (choice ). the Select modifier allows an element to have multiple possible values in a given instance. essentially, the decoder will try all the desired Decoding Algorithms until there is a type that fits. the selector is useful when a complex container contains other containers. for example:

Userkey: = SEQUENCE {

Name ia5string,

Startdate utctime,

Expire utctime,

Keydata CHOICE {

Ecckey ecckeytype,

Rsakey rsakeytype

}

}

In the preceding example, ECC is allowed to also allow public key certificates of RSA keys.

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.