相关参考
OFD电子印章应用流程
OFD电子印章数据封装代码
OFD电子签章,验章规范流程
PKCS#7中规定了6种数据内容:明文数据(Data)、签名数据(Signed-data),数字信封数据(Enveloped-data),带签名的数字信封数据(Signed-and-enveloped-data),摘要数据(Digested-data),加密数据(Encrypted-data)。
国密局参考p7规范,对这6种类型进行了定义(具体见GM/T 0010规范)。
其实区别不是很大,都是将数据封装到pkcs7结构中(国际标准的消息数据封装通过openssl中的pkcs7模块实现,而国密规范的消息数据封装是通过gmssl中的pkcs7模块实现,GmSSL (http://gmssl.org) 是支持国密算法和标准的OpenSSL分支)。那么接下来我们细细聊一下GM/T 0010规范中的6中类型及与RFC中的区别。
1、类型标识符定义
RFC中各类型标识符定义如下:
对象标识符OID 对象标识符定义 GmSSL/OpenSSL中NID
1.2.840.113549.1.7.1 数据类型data NID_pkcs7_data
1.2.840.113549.1.7.2 签名数据类型signedData NID_pkcs7_signed
1.2.840.113549.1.7.3 数字信封数据类型envelopedData NID_pkcs7_enveloped
1.2.840.113549.1.7.4 签名及数字信封数据类型signedAndEnvelopedData NID_pkcs7_signedAndEnveloped
1.2.840.113549.1.7.5 摘要数据类型digestData NID_pkcs7_digest
1.2.840.113549.1.7.6 加密数据类型encryptedData NID_pkcs7_encrypted
GM/T 0010各类型标识符定义,详见下表:
对象标识符OID 对象标识符定义 GmSSL/OpenSSL中NID
1.2.156.10197.6.1.4.2 SM2密码算法加密签名消息语法规范 无
1.2.156.10197.6.1.4.2.1 数据类型data 无
1.2.156.10197.6.1.4.2.2 签名数据类型signedData 无
1.2.156.10197.6.1.4.2.3 数字信封数据类型envelopedData 无
1.2.156.10197.6.1.4.2.4 签名及数据信封数据类型signedAndEnvelopedData 无
1.2.156.10197.6.1.4.2.5 加密数据类型encrypedData 无
1.2.156.10197.6.1.4.2.6 密钥协商类型keyAgreementInfo
无
1、基本类型定义
Version
Version类型标明语法版本号。
Version ::= INTEGER
ContentEncryptionAlgorithmIdentifier
ContentEncryptionAlgorithmIdentifier类型标明一个数据加密算法。其OID见GM/T0006标准。
ContentEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
例如,gmssl库中已定义的SM4算法相关OID如下:
NID_sms4_ecb, /* OBJ_sms4_ecb 1 2 156 10197 1 104 1 /
NID_sms4_cbc, / OBJ_sms4_cbc 1 2 156 10197 1 104 2 /
NID_sms4_ofb128, / OBJ_sms4_ofb128 1 2 156 10197 1 104 3 /
NID_sms4_cfb128, / OBJ_sms4_cfb128 1 2 156 10197 1 104 4 /
NID_sms4_cfb1, / OBJ_sms4_cfb1 1 2 156 10197 1 104 5 /
NID_sms4_cfb8, / OBJ_sms4_cfb8 1 2 156 10197 1 104 6 /
NID_sms4_ctr, / OBJ_sms4_ctr 1 2 156 10197 1 104 7 /
NID_sms4_gcm, / OBJ_sms4_gcm 1 2 156 10197 1 104 8 /
NID_sms4_ccm, / OBJ_sms4_ccm 1 2 156 10197 1 104 9 /
NID_sms4_xts, / OBJ_sms4_xts 1 2 156 10197 1 104 10 /
NID_sms4_wrap, / OBJ_sms4_wrap 1 2 156 10197 1 104 11 /
NID_sms4_wrap_pad, / OBJ_sms4_wrap_pad 1 2 156 10197 1 104 12 /
NID_sms4_ocb, / OBJ_sms4_ocb 1 2 156 10197 1 104 100 */
DigestAlgorithmIdentifier
DigestAlgorithmIdentifier类型标识消息摘要算法,在GM/T0010标准中为SM3算法,其OID为:1.2.156.10197.1.401
DigestAlgorithmIdentifier ::= AlgorithmIdentifier
DigestEncryptionAlgorithmIdentifier
DigestEncryptionAlgorithmIdentifier类型标明一个签名算法,在GM/T0010标准中为SM2-1数字签名算法,其OID为:1.2.156.10197.1.301.1
DigestEncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
ExtendedCertificatesAndCertificates
ExtendedCertificatesAndCertificate类型指定一个PKCS#6扩展证书或者一个X.509证书。这一类型见PKCS#6第6节推荐的语法:
“
ExtendedCertificateOrCertificate ::= CHOICE {
certificate Certificate, -- X.509
extendedCertificate [0] IMPLICIT ExtendedCertificate
}
IssuerAndSerialNumber
IssuerAndSerialNumber类型标明一个证书颁发者可识别名和颁发者的证书序列号,可据此确定一份证书和与此证书对应的实体及公钥。
IssuerAndSerialNumber ::= SEQUENCE {
issuer Name,
serialNumber CertificateSerialNumber
}
ContentInfo
ContentInfo类型标明了实体之间交换内容的通用语法结构,定义如下:
ContentInfo ::= SEQUENCE {
contentType ContentType,
content[0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
}
ContentType ::= OBJECT IDENTIFIER
其中:
ContentType 内容类型。它是一个对象标识符,这意味着它是由定义内容类型的权威分配的唯一整数字符串。也就是六种内容类型:data、signedData、envelopedData、signedAndEnvelopedData、digestddata和encryptedData对应的对象标识符。(对于SM2算法的消息语法规范即为GM/T 0010中定义的各类型标识符OID)
content 内容,可选
CertificateRevocationLists
CertificateRevocationLists类型标明一个证书撤销列表的集合。它的目的是让集合包含足够的信息来确定与该集合相关联的证书是否仍有效。
CertificateRevocationLists ::= SET OF CertificateRevocationList
2、明文数据类型 data
data数据类型结构定义如下:
Data ::= OCTET STRING
Data数据类型标识任意的字符串,比如ASCII文本文件。也就是说这样的字符串不需要有任何内部结构(当然也可以有),它们甚至可能是DER编码的数据。该类型与rfc2315中定义是一样一样的
3、签名数据类型 signedData
signedData数据类型有任意类型的数和至少一个签名者的签名值组成。任意类型的数据能够同时被任意数量的签名者签名。signedData数据类型结构定义如下:
SignedData ::= SEQUENCE {
version Version,
digestAlgorithms DigestAlgorithmIdentifiers,
contentInfo ContentInfo,
certificates[0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
crls[1] IMPLICIT CertificateRevocationLists OPTIONAL,
signerInfos SignerInfos
}
DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmIdentifier
SignerInfos ::= SET OF SignerInfo
3.1、SignerInfo类型
SignerInfo结构定义如下:
SignerInfo ::= SEQUENCE {
version Version,
issuerAndSerialNumber IssuerAndSerialNumber,
digestAlgorithm DigestAlgorithmIdentifier,
authenticatedAttributes[0] IMPLICIT Attributes OPTIONAL,
digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
encryptedDigest EncryptedDigest,
unauthenticatedAttributes[1] IMPLICIT Attributes OPTIONAL
}
EncryptedDigest ::= OCTET STRING
4、数字信封数据类型envelopedData
数字信封envelopedData数据类型有加密数据和至少一个接收者的数据加密密钥的密文组成。其中加密数据是用数据加密密钥加密的,数据加密密钥是有接收者的公钥加密的。
该类型用于为接收者的data、digestData或signedData三种类型的数据做数字信封。
envelopedData数据类型结构定义如下:
EnvelopedData ::= SEQUENCE {
version Version,
recipientInfos RecipientInfos,
encryptedContentInfo EncryptedContentInfo
}
RecipientInfos ::= SET OF RecipientInfo
EncryptedContentInfo ::= SEQUENCE {
contentType ContentType,
contentEncryptionAlgorithm ContentEncryptionAlgorithmIdentifier,
encryptedContent[0] IMPLICIT EncryptedContent OPTIONAL,
sharedInfo[1] IMPLICIT OCTET STRING OPTIONAL,
sharedInfo[2] IMPLICIT OCTET STRING OPTIONAL
}
EncryptedContent ::= OCTET STRING
4.1、RecipientInfo类型
每个接收者信息用RecipientInfo类型表示,其类型结构定义如下:
RecipientInfo ::= SEQUENCE {
version Version,
issuerAndSerialNumber IssuerAndSerialNumber,
keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier,
encryptedKey EncryptedKey
}
EncryptedKey ::= OCTET STRING
5、签名及数字信封数据类型signedAndEnvelpedData
signedAndEnvelopedData数据类型有任意类型的加密数据、至少一个接收者的数据加密密钥和至少一个签名者的签名组成。其结构定义如下:
SignedAndEnvelopedData ::= SEQUENCE {
version Version,
recipientInfos RecipientInfos,
digestAlgorithms DigestAlgorithmIdentifiers,
encryptedContentInfo EncryptedContentInfo,
certificates[0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
crls[1] IMPLICIT CertificateRevocationLists OPTIONAL,
signerInfos SignerInfos
}
6、加密数据类型encryptedData
encryptedData数据类型有任意类型的已加密的数据组成,数据类型既没有接收者也没有加密的数据加密密钥。其结构定义如下:
EncryptedData ::= SEQUENCE {
version Version,
encryptedContentInfo EncryptedContentInfo
}
7、密钥协商类型keyAgreementInfo
KeyAgreementInfo ::= SEQUENCE {
version Version(1),
tempPublicKeyR SM2PublicKey,
userCertificate Certificate,
userID OCTET STRING
}