DER编码

一、任务详情
参考附件中图书p120 中7.1的实验指导,完成DER编码
Name实例中,countryName改为"CN",organization Name="你的学号" commonName="你的姓名拼音"
用echo -n -e "编码" > 你的学号.der中,用OpenSSL asn1parse 分析编码的正确性
提交编码过程文档(推荐markdown格式)
附件:PKI.CA与数字证书技术大全.pdf
二、查看CN、姓名、学号的16进制ASCII码

echo -n "CN" | od -tc -tx1
echo -n "Guo Tao" | od -tc -tx1
echo -n "20201205" | od -tc -tx1

43 4e
47 75 6f 20 54 61 6f
32 30 32 30 31 32 30 35

三、DER编码示例:X.501Name类型
(一)ASN.1描述与实例
1.ASN.1描述
  X.501Name类型用ASN.1描述如下:

Name::=CHOICE{ RDNSequence }
RDNSequence ::=SEQUENCE OF RelativeDistinguishedName
RelativeDistinguishedName ::=SET OF AttributeValueAssertion
Attribute ValueAssertion ::=SEQUENCE{
  AttributeType,
  AttributeValue}
AttributeType ::=OBJECT IDENTIFIER
Attribute Value ::=ANY

Name类型定义为CHOICE类型,目前只有1个选项RDNSequence。RDNSequence定义为SEQUENCE OF类型,由O个或多个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 User1,其对应的Name类型采用分层结构描述为:
image

其中,每层对应一个RelativeDistinguishedName;每个RelativeDistinguishedName由l个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 ::=

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

AttributeType OID定义 标识串 长度串 内容串
countryName 2.5.4.6 06 03 55 04 06
organizationName 2.5.4.10 06 03 55 04 0A
commonName 2.5.4.3 06 03 55 04 03

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

AttributeValue 标识串 长度串 内容串
"US" 13 02 55 53
"Example rganization" 13 14 45 78 61 6D 70 6C 65 20 4F 72 67 61 6E 69 7A 61 74 69 6F 6E
"Test User 1" 13 0B 54 65 73 74 20 55 73 65 72 20 31
"CN" 13 02 43 4E
"20201205" 13 08 32 30 32 30 31 32 30 35
"Guo Tao" 13 0C 47 75 6f 20 54 61 6f

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

AttributeValueAssertion 标识串 长度串 内容串
countryName="US" 30 09 06 03 55 04 06 13 02 55 53
organizationName="Example rganization" 30 1B 06 03 55 04 0A 13 14 45 78 61 6D 70 6C 65 20 4F 72 67 61 6E 69 7A 61 74 69 6F 6E
commonName="Test User 1" 30 12 06 03 55 04 03 13 0B 54 65 73 74 20 55 73 65 72 20 31
countryName="CN" 30 09 06 03 55 04 06 13 02 43 4E
organizationName="20201205" 30 1B 06 03 55 04 0A 13 08 32 30 32 30 31 32 30 35
commonName="Guo Tao" 30 12 06 03 55 04 03 13 0C 47 75 6f 20 54 61 6f

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

RelativeDistinguishedName 标识串 长度串 内容串
countryName="US" 31 0B 30 09 06 03 55 04 06 13 02 55 53
organizationName="Example rganization" 31 1D 30 1B 06 03 55 04 0A 13 14 45 78 61 6D 70 6C 65 20 4F 72 67 61 6E 69 7A 61 74 69 6F 6E
commonName="Test User 1" 31 14 30 12 06 03 55 04 03 13 0B 54 65 73 74 20 55 73 65 72 20 31
countryName="CN" 31 0B 30 09 06 03 55 04 06 13 02 43 4E
organizationName="20201205" 31 11 30 0F 06 03 55 04 0A 13 08 32 30 32 30 31 32 30 35
commonName="Guo Tao" 31 15 30 13 06 03 55 04 03 13 0C 47 75 6f 20 54 61 6f

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

RDNSequence 标识串 长度串 内容串
countryName="US" 30 42 31 0B 30 09 06 03 55 04 06 13 02 55 53 31 1D 30 1B 06 03 55 04 0A 13 14 45 78 61 6D 70 6C 65 20 4F 72 67 61 6E 69 7A 61 74 69 6F 6E 31 14 30 12 06 03 55 04 03 13 0B 54 65 73 74 20 55 73 65 72 20 31
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 32 30 31 32 31 32 31 15 30 13 06 03 55 04 03 13 0C 59 61 6e 67 20 43 68 65 6e 67 79 75

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

DER编码值 ASN.1描述
30 42 31 0B 30 09 06 03 55 04 06 13 02 55 53 attributeType=countryName attributeValue="US"
31 1D 30 1B 06 03 55 04 0A 13 14 45 78 61 6D 70 6C 65 20 4F 72 67 67 61 6E 69 7A 61 74 69 6F 6E attributeType=organizationName attributeValue="Example Organization"
31 14 30 12 06 03 55 04 03 13 0B 54 65 73 74 20 55 73 65 72 20 31 attributeType=commonName attributeValue="Test User 1"
30 37 31 0B 30 09 06 03 55 04 06 13 02 43 4E attributeType=countryName attributeValue="CN"
31 11 30 0F 06 03 55 04 0A 13 08 32 30 32 30 31 32 30 35 attributeType=organizationName attributeValue="20201205"
31 15 30 13 06 03 55 04 03 13 0C 47 75 6f 20 54 61 6f attributeType=commonName
attributeValue="GuoTao"
四、DER编码过程
验证:openssl asn1parse -inform der -in ./20201205.der

1.countryName="CN"
echo -n -e "\x31\x0B\x30\x09\x06\x03\x55\x04\x06\x13\x02\x43\x4E" > 20201205.der

2.organization Name="20201205"
echo -n -e "\x31\x11\x30\x0F\x06\x03\x55\x04\x0A\x13\x08\x32\x30\x32\x30\x31\x32\x30\x35" >> 20201205.der

3.commonName="Guo Tao"
echo -n -e "\x31\x15\x30\x13\x06\x03\x55\x04\x03\x13\x0C\x47\x75\x6f\x20\x54\x61\x6f" >> 20201205.der

posted @ 2023-05-04 11:22  西宁西  阅读(20)  评论(0编辑  收藏  举报