DER

20211201李柏林發表於2024-03-31

一、實驗要求

  1. 參考附件中圖書p120 中7.1的實驗指導,完成DER編碼
  2. Name例項中,countryName改為“CN”,organization Name-"你的學號" commoaName="你的姓名拼音"
  3. 用echo -n -e "編碼" > 你的學號.der中,用OpenSSL asn1parse 分析編碼的正確性
  4. 提交編碼過程文件(推薦markdown格式)

二、DER編碼

0.檢視CN、姓名、學號的16進位制ASCII碼

echo -n "CN" | od -tc -tx1
echo -n "libolin" | od -tc -tx1
echo -n "20211201" | od -tc -tx1

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。
  具體編碼過程如表所示。

AttributeTypeOID定義標識串長度串內容串
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碼組成。
  具體編碼過程如表所示。

AttributeValue標識串長度串內容串
"US" 13 02 55 53
"Example rganization" 13 14 45 78 61 6D 70 6C 65 20 4F 7267 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
"20211201" 13 08 32 30 32 31 31 32 30 31
"libolin" 13 07 6c 69 62 6f 6c 69 6e

3.AttributeValueAssertion編碼

AttributeValueAssertion為SEQUENCE結構型別,編碼規則採用結構型別定長模式。
  對於標識串,採用低標識編碼方式,只需1個位元組。SEQUENCE的tag為OxlO:class選擇universal,則位8和位7為0,SEQUENCE為結構型別,則位6為1。因此,標識串0x30.
  對於長度串,採用短型編碼方式,只需1個位元組。
  對於內容串,由AttributeType和AttributeValue的DER編碼值組成。
  具體編碼過程如表所示。

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="20211201" 30 1B 06 03 55 04 0A 13 08 32 30 32 31 31 32 30 31
commonName="libolin" 30 12 06 03 55 04 03 13 06 6c 69 62 6f 6c 69 6e

4.RelativeDistinguishedName編碼

RelativeDistinguishedName為SET OF結構型別,編碼規則採用結構型別定長模式。
  對於標識串,採用低標識編碼方式,只需1個位元組。SET OF的tag為0xl1;class選擇universal,則位8和位7為0,SET OF為結構型別,則位6為1。因此,標識串=0x31。
  對於長度串,採用短型編碼方式,只需1個位元組。
  對於內容串,由AttributeValueAssertion的DER編碼值組成。
  具體編碼過程如表所示。

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="20211201" 31 11 30 0F 06 03 55 04 0A 13 08 32 30 32 31 31 32 30 31
commonName="libolin" 31 15 30 0D 06 03 55 04 03 13 06 6c 69 62 6f 6c 69 6e

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 5331 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 6E31 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 4E31 11 30 0F  06 03 55 04 0A  13 08 32 30 32 30 31 32 31 3231 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
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
31 14
 30 12
  06 03 55 04 03
  13 0B
   54 65 73 74 20 55 73 65 72 20 31
attributeType=country
NameattributeValue="US"

attributeType=organizationName
attributeValue="Example Organization"

attributeType=commonName
attributeValue="Test User 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 32 31 31 32 30 31
31 0F
 30 0D
  06 03 55 04 03
  13 06
   6c 69 62 6f 6c 69 6e
attributeType=countryNameattributeValue="CN"
attributeValue="CN"

attributeType=organizationName
attributeValue="20211201"

attributeType=commonName
attributeValue="libolin"

三、DER編碼驗證

0.驗證

openssl asn1parse -inform der -in ./20211201.der

1.countryName="CN"

echo -n -e "\x31\x0B\x30\x09\x06\x03\x55\x04\x06\x13\x02\x43\x4E" > 20211201.der

2.organization Name="20211201"

echo -n -e "\x31\x11\x30\x0F\x06\x03\x55\x04\x0A\x13\x08\x32\x30\x32\x30\x31\x33\x32\x34" >> 20211201.der

3.commonName="libolin"

echo -n -e "\x31\x0F\x30\x0D\x06\x03\x55\x04\x03\x13\x06\x78\x75\x79\x75\x61\x6E" >> 20201324.der