数字证书格式编码
数字证书格式编码
ASN.1描述与实例
1.TBSCertificate 的 ASN.1描述与实例
TBSCertificate格式用ASN.1描述如下:
TBSCertificate :=SEQUENCE {
version
[0] EXPLICIT Version DEFAULT v1,
serialNumber
CertificateSerialNumber,
signature
Algorithmldentifier,
issuer
Name,
validity
Validity,
subject
Name,
subjectPublicKeyInfo SubjectPublicKeyInfo,
issuerUniqueID[1] IMPLICIT Uniqueldentifier OPTIONAL,
-- If present, version MUST be v2 or v3
subjectUniqueID [2] IMPLICIT Uniqueldentifier OPTIONAL,
-- If present, version MUST be v2 or v3
extensions[3]EXPLICIT Extensions OPTIONAL
-- If present, version MUST be v3
}
Extensions ::=SEQUENCE SIZE (1..MAX) OF ExtensionTBSCertificate 中各项内容具体值如表13-3所示。
TBSCertificate | 值 |
---|---|
version | 02 |
serialNumber | 04 96 |
signature | sha 1 WithRSAEncryption ( 1.2.840.113549.1.1.5) |
issuer | “CN=Virtual CA,C=CN” |
validity | notBefore=20200222000000、notAfter=20220222000000 |
subject | “CN=chenshaoqing,OU=Person,C=CN” |
subjectPublicKeyInfo | 空 |
issuerUniquelD subjectUniquelD | 空 |
extensions | 包含6个扩展项(Extension ): basicContraints、subjectKeyldentifier、keyUsage、extKeyUsage,netscapeCertType、authorityKeyIdentifier |
-
Extension的ASN.1描述与实例Extension格式用ASN.1描述如下:
Extension :=SEQUENCE {
extnID
OBJECT IDENTIFIER,
critical
BOOLEAN DEFAULT FALSE,
extnValue
OCTET STRING }
Extension各扩展项值如表13-4所示。
-
Certificate 的ASN.1描述与实例Certificate格式用ASN.1描述如下:
Certificate ::=SEQUENCE {
tbsCertificate
TBSCertificate,
signatureAlgorithm
AlgorithmIdentifier,
signature Value
BIT STRING }
Certificate中各项内容的具体值如表13-5所示。
DER编码过程
1.对Extension进行DER编码
各扩展项具体内容用ASN.1描述如下:
BasicConstraints ::=SEQUENCE{
cA
BOOLEAN DEFAULT FALSE,
pathLenConstraint
INTEGER (O..MAX)OPTIONAL}
SubjectKeyIdentifier ::= Keyldentifier
(KeyIdentifier ::= OCTET STRING)KeyUsage ::= BIT STRING
ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX)OF KeyPurposeld
(KeyPurposeld ::= OBJECT IDENTIFIER)NetscapeCertType ::= BIT STRING
AuthorityKeyldentifier ::=SEQUENCE{
keyIdentifier
[O] KeyIdentifier
OPTIONAL,
authorityCertIssuer
[1]GeneralNames
OPTIONAL,
authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }(KeyIdentifier ::= OCTET STRING)
Extension为 SEQUENCE结构类型,不同扩展项DER编码值包含在OCTET STRING类型extnValue中,编码规则采用结构类型定长模式。各扩展项DER编码值用括号分隔。其中,对于BIT STRING类型,编码后第1个字节表示填充位数或未使用位数。
2.对TBSCertificate进行DER编码
TBSCertificate内容编码规则采用结构类型定长模式,具体编码过程如表13-7所示。
1、序列号=1174(0x0496)
echo -n -e "\xA0\x03\x02\x01\x02\x02\x02\x04\x96\x30\x0D\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x01\x05\x05\x00" > 20191227.der
2、证书签发者 DN="CN=Virtual CA
echo -n -e "\x30\x22\x31\x0B\x30\x09\x06\x03\x55\x04\x06\x13\x02\x43\x4E\x31\x13\x30\x11\x06\x03\x55\x04\x03\x13\x0A\x56\x69\x72\x74\x75\x61\x6C\x20\x43\x41" >> 20191227.der
3、证书有效期=20200222000000-20220222000000
echo -n -e "\x17\x0D\x32\x30\x32\x30\x30\x32\x32\x32\x30\x30\x30\x30\x5A\x17\x0D\x32\x30\x32\x32\x30\x32\x32\x32\x30\x30\x30\x30\x5A" >> 20191227.der
4、证书持有者DN=CN=你的名字拼音, OU=Person
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\x39\x31\x32\x32\x37\x31\x15\x30\x13\x06\x03\x55\x04\x03\x13\x0c\x47\x61\x6e\x4e\x69\x6e\x67\x79\x75\x20\x20\x20\x20" >> 20191227.der