DER编码

X.501 Name类型用ASN.1描述如下:
Name ::=CHOICE{ RDNSequence }
RDNSequence ::= SEQUENCE OF RelativeDistinguishedNameRelativeDistinguishedName ::= SET OF AttributeValueAssertionAttribute ValueAssertion ::= SEQUENCE{
AttributeType,
AttributeValue }
AttributeType ::= OBJECT IDENTIFIERAttribute Value ::= ANY

Name类型定义为CHOICE类型,目前只有1个选项RDNSequence。RDNSequence定义为SEQUENCEOF类型,由0个或多个RelativeDistinguishedName组成。RelativeDistinguished-Name定义为SET OF类型,由0个或多个AttributeValueAssertion组成。AttributeValueAssertion定义为SEQUENCE类型,由2个成分组成:1个为AttributeType类型和1个AttributeValue类型。AttributeType定义为OBJECT IDENTIFIER类型。AttributeValue定义为ANY类型,具体内容由AttributeType决定。
事实上,Name类型可理解为分层或树形结构,即X.500目录树结构。

2.Name实例

对于用户Test User 1,其对应的Name类型采用分层结构描述为:
(root)
countryName = "CN"
organization Name= "20181217"
commonName = "chenshaoqing"

其中,每层对应一个RelativeDistinguishedName;每个RelativeDistinguishedName 由1个AttributeValueAssertion组成。等号前内容为AttributeType,等号后内容为AttributeValue。
用户Test User 1包含3个AttributeType: countryName. organizationName,commonName,其OID定义如下:

attributeType OBJECT IDENTIFIER ::= { joint-iso-ccitt(2) ds(5)4}countryName OBJECT IDENTIFIER:= { attributeType 6 }
organizationName OBJECT IDENTIFIER::= { attributeType 10 }commonName OBJECT IDENTIFIER::= { attributeType 3 }

3.DER编码过程

1. AttributeType编码
AttributeType为OBJECT IDENTIFIER基本类型,编码规则采用基本类型定长模式。对于标识串,采用低标识编码方式,只需1个字节。OBJECT IDENTIFIER的tag 为Ox06;class选择universal,则位8和位7为0,OBJECT IDENTIFIER为基本类型,则位6为0。因此,标识串=Ox06。
对于长度串,采用短型编码方式,只需1个字节。
对于内容串,由3个字节组成。2.5.4.6编码为550406,2.5.4.10编码为5504 0A,2.5.4.3编码为55 04 03。
具体编码过程如表7-1所示。

 

 

2. AttributeValue编码
AttributeValue为 PrintableString 基本类型,编码规则采用基本类型定长模式。
对于标识串,采用低标识编码方式,只需1个字节。PrintableString 的tag 为0x13;class选择universal,则位8和位7为0,OBJECT IDENTIFIER为基本类型,则位6为0。因此,标识串=Ox13。
对于长度串,采用短型编码方式,只需1个字节。对于内容串,由其ASCII码组成。
具体编码过程如表7-2所示。

Attributevalue

标识串 长度串 内容串
“CN” 13 02 43 4E
“20181217” 13 08 32 30 31 38 31 32 31 37
“chenshaoqing” 13 0C 63 68 65 6E 73 68 61 6F 71 69 6E 67

3. AttributeValueAssertion编码
AttributeValueAssertion为 SEQUENCE结构类型,编码规则采用结构类型定长模式。对于标识串,采用低标识编码方式,只需1个字节。SEQUENCE的 tag 为0x10; class

选择universal,则位8和位7为0,SEQUENCE为结构类型,则位6为1。因此,标识串=Ox30。
对于长度串,采用短型编码方式,只需1个字节。
对于内容串,由AttributeType和 AttributeValue的 DER编码值组成。具体编码过程如表7-3所示。

AttributeValueAssertion

标识串 长度串 内容串

countryName="CN"

30 09

06 03 55 04 06 

13 02 43 4E

organizationName="20181217”

30 0F

06 03 55 04 0A

13 08 32 30 31 38 31 32 31 37

commonName ="chenshaoqing"

30 13

06 03 55 04 03

13 0C 63 68 65 6E 73 68 61 6F 71 69 6E 67

4. RelativeDistinguishedName编码
RelativeDistinguishedName为SET OF结构类型,编码规则采用结构类型定长模式。对于标识串,采用低标识编码方式,只需1个字节。SET OF的tag 为0x11; class选择universal,则位8和位7为0,SET OF 为结构类型,则位6为1。因此,标识串=Ox31。
对于长度串,采用短型编码方式,只需1个字节。
对于内容串,由Attribute ValueAssertion的 DER编码值组成。具体编码过程如表7-4所示。

RelativeDistinguishedName

标识串 长度串 内容串
countryName="CN"  31  0B

30 09

    06 03 55 04 06 

    13 02 43 4E

organizationName="20181217”  31  11

 30 0F

    06 03 55 04 0A

    13 08 32 30 31 38 31 32 31 37

commonName ="chenshaoqing"  31  15

 30 13

    06 03 55 04 03

    13 0C 63 68 65 6E 73 68 61 6F 71 69 6E 67

5. RDNSequence编码
RDNSequence为SEQUENCE-OF结构类型,编码规则采用结构类型定长模式。对于标识串,采用低标识编码方式,只需1个字节。SEQUENCE OF的 tag 为0x10;class选择universal,则位8和位7为0,SEQUENCE OF为结构类型,则位6为1。因此,标识串=Ox30。
对于长度串,采用短型编码方式,只需1个字节。
对于内容串,由3个RelativeDistinguishedName的 DER编码值组成。具体编码过程如表7-5所示。

RDNSequence

标识串 长度串 内容串

countryName ="CN"

30  37

31 0B

   30 09

    06 03 55 04 06 

    13 02 43 4E

31 11

   30 0F

    06 03 55 04 0A

    13 08 32 30 31 38 31 32 31 37

31 15

   30 13

    06 03 55 04 03

    13 0C 63 68 65 6E 73 68 61 6F 71 69 6E 67

6.Name编码
Name为CHOICE类型,其DER编码值与RDNSequence相同。用户Test User 1最终DER编码值如表7-6所示。

DER编码值

ASN.1描述

30 37

   31 0B

     30 09

        06 03 55 04 06 

        13 02 43 4E

   31 11

      30 0F

         06 03 55 04 0A

         13 08 32 30 31 38 31 32 31 37

   31 15

      30 13

         06 03 55 04 03

         13 0C 63 68 65 6E 73 68 61 6F 71 69 6E 67

  

 

attributeType = countryName

attributeValue = "CN"
attributeType = organizationName
attributeValue = "20181217"
attributeType = commonName
attribute Value = "chenshaoqing"

echo -n -e "\x30\x37\x31\x0B\x30\x09\x06\x03\x55\x04\x06\x13\x02\x43\x4E\x31\x11\x30\x0F\x06\x03\x55\x04\x0A\x13\x08\x32\x30\x31\x38\x31\x32\x31\x37\x31\x15\x30\x13\x06\x03\x55\x04\x03\x13\x0C\x63\x68\x65\x6E\x73\x68\x61\x6F\x71\x69\x6E\x67" > 20181217.der

 

 

posted @ 2021-06-19 19:38  20181217Cindy  阅读(527)  评论(0编辑  收藏  举报