信息安全技术
SM2密码算法加密签名消息语法规范
GB/T 35275-2017
本标准定义了使用SM2密码算法的加密签名消息语法。
本标准适用于使用SM2密码算法进行加密和签名操作时对操作结果的标准化封装
下列文件对于本文件的应用是必不可少的。凡是注日期的引用文件,仅注日期的版本适用于本文件,凡是不注日期的引用文件,其最新版本(包括所有的修改单)适用于本文件。
GB/T 33560链接
http://c.gb688.cn/bzgk/gb/showGb?type=online&hcno=252CF0F72A7BE339A56DEA7D774E8994
下列术语和定义适用于本文件。
算法标识 algorithmidentifier
用于标明算法机制的数字化信息。
SM2密码算法 SM2cryptographicalgorithm
由GB/T32918(所有部分)定义的一种算法。
SM3密码算法 SM3cryptographic algorithm
由GB/T32905 定义的一种算法。
下列缩略语适用于本文件。
本标准对6个对象 data,signedData、envelopedData,signedAndEnvelopedData,encryptedData 和keyAgreementInfo的标识符进行了定义,详见表1。
表1 对象标识符
对象标识符OID | 对象标识符定义 | 备注 |
---|---|---|
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 | 加密数据类型 | 加密数据类型encryptedData |
1.2.156.10197.6.1.4.2.6 | 密钥协商类型 | 密钥协商类型keyAgreementInfo |
CertificateRevocationLists类型标明一个证书撤销列表的集合
CertificateRevocationlists ::= SET OF CertificateRevocationlist
ContentEncryptionAlgorithmIdentifier 类型标明一个数据加密算法。其 OID见 GB/T 33560。
ContentEncryptionAlgorithmldentifier ::AlgorithmIdentifier
DigestAlgorithmIdentifier类型标明一个消息摘要算法,本标准为SM3算法,其OID见GB/T 33560。
DigestAlgorithmIdentifier ::=AlgorithmIdentifier
DigestEncryptionAlgorithmIdentifier 类型标明一个签名算法,本标准为SM2密码算法,其 OID见GB/T 33560。
DigestEncryptionAlgorithmIdentifier ::=AlgorithmIdentifier
ExtendedCertificateOrCertificate类型指定一个PKCS#6扩展证书或者一个X.509 证书。这一类型见PKCS#6第6节推荐的语法:
ExtendedCertificateOrCertificate ::= CHOICE
certificate Certificate, --X.509
extendedCertificate [0] IMPICIT ExtendedCertificate
}
ExtendedCertificatesAndCertificates类型指定一个扩展证书和X.509 证书的集合。它表示集合足以包含从可识别的“根”或“顶级CA”到所有签名者的证书链。
ExtendedCertificatesAndCertificates ::= SET OF
ExtendedCertificateOrCertificate
IssuerAndSerialNumber类型标明一个证书颁发者可识别名和颁发者确定的证书序列号,可据此确定一份证书和与此证书对应的实体及公钥。
IssuerAndSerialNumber ::= SEQUENC {
Eissuer Name,
serialNumber CertificateSerialNumber
}
KeyEncryptionAlgorithmIdentifier类型标明加密对称密钥的加密算法
KeyEncryptionAlgorithmIdentifier ::=Algorithmldentifier
Version类型标明语法版本号
Version ::=INTEGER(1)
Contentlnfo类型标明内容交换通用语法结构,内容交换的通用语法结构定义如下:
ContentInfo ::= SEQUENCE {
contentType ContentType ,
content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
}
ContentType ::= OBJECT IDENTIFIER
其中:
ContentType内容类型是一个对象标识符,其定义见第5章。 content内容,可选。
这里如果大家对证书不是很了解的就很容易费解,下文和的关系是什么。笔者也是通过RSA的签名反推出来。
先看看RSA数字签名的格式:
然后是国密证书格式:
本文第五章定义的内容除1.2.156.10197.6.1.4.2SM2密码算法加密签名消息语法规范跟对象外,都可以用ContentInfo来表示。ContentType就是通过OID来表示下面描述的内容。想想本标准有六大类内容需要解释,怎么让人知道是那种数据呢?所以下文中从第八章开始,他们都是被包裹在此对象下面的。。
数据类型Data结构定义如下
Data ::= OCTET STRING
数据类型Data表示任意的字节串
不会单独出现,肯定是被包裹在下文某个对象中。
signedData数据类型由任意类型的数据和至少一个签名者的签名值组成。任意类型的数据能够同时被任意数量的签名者签名。
signedData数据类型结构定义如下;
SignedData ::= SEQUENCE(
version Version,
digestAlgorithms DigestAlgorithmIdentifiers,
contentInfo ContentInfo ,
certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
signerlnfos Signerlnfos
}
DigestAlgorithmIdentifiers ::= SET OF DigestAlgorithmldentifier
SignerInfos::= SET OF SignerInfo
结构中各项含义见表2。
表 2signedData数据类型
字段名称 | 数据类型 | 含义 |
---|---|---|
version | Version | 版本号,此处取值为1 |
digestAlgorithms | DigestAlgorithmldentifiers | 消息摘要算法标识符的集合 OID: 1.2.156.10197.1.401 |
contentInfo | Contentlnfo | 数据内容 (签名原文数据 OID:1.2.156.10197.6.1.4.2.1) |
certificates | ExtendedCertificatesandCertificates | PKCS#6扩展证书和X.509证书的集合 |
crls | CertificateReyocationlists | 证书撤销列表的集合 |
signlnfos | SignerInfos | 每个签名者信息的集合 |
本章节描述的对象SignedData是被包裹在ContentInfo对象内的。 下面章节的类型雷同。
SignedData对象内部的contentInfo是数据的内容用1.2.156.10197.6.1.4.2.1的OID来表示
signerInfo类型结构定义如下:
SignerInfo ::= SEQUENCE{
version Version,
issuerAndSerialNumber IssuerAndSerialNumber,
digestAlgorithm DigestAlgorithmldentifier,
authenticatedAttributes [0] IMPLICIT Attributes OPTIONAL,
digestEncryptionAlgorithm DigestEncryptionAlgorithmldentifier.
encryptedDigest EncryptedDigest ,
unauthenticatedAttributes [1] IMPLICIT Attributes OPTIONAL
}
EncryptedDigest ::OCTETSTRING
结构中各项含义见表3。
表 3 SignerInfo 数据类型
字段名称 | 数据类型 | 含义 |
---|---|---|
version | Version | 版本号,此处取值为1 |
issuerAndSerialNumber | IssuerAndSerialNumber | 一个证书颁发者可识别名和颁发者确定的证书序列号,可据此确定一份证书和与此证书对应的实体及公钥 |
digestAlgorithm | DigestAlgorithm | 对内容进行摘要计算的消息摘要算法,本标准采用SM3算法 |
authenticatedAttributes | Attributes | 是经由签名者签名的属性的集合,该域可选。如果该域存在,该域中摘要的计算方法是对原文进行摘要计算结果 |
digestEncryptionAlgorithm | DigestEncryptionAlgorithmldentifier | SM2椭圆曲线数字签名算法标识符 1.2.156.10197.1.301.1 |
encryptedDigest | OCTET STRING | 值是SM2Signature,用签名者私钥进行签名的结果,其定义见GB/T 35276 |
表ADD1 authenticatedAttributes 主要内容
属性 | 属性对象值 | 说明 |
---|---|---|
contentType (OID:1.2.840.113549.1.9.3) | is set to PKCS #7 Data ( 1.2.156.10197.6.1.4.2.1). | 内容类型 |
Signing Time (1.2.840.113549.1.9.5) | is set to the UTC time of timestamp generation time | 签名时间 |
Message Digest (1.2.840.113549.1.9.4) | is set to the hash value of the SignerInfo structure's encryptedDigest value. The hash algorithm that is used to calculate the hash value is the same as that specified in the SignerInfo structure’s digestAlgorithm value of the timestamp. | 对内容进行摘要计算的消息摘要算法,本标准采用SM3算法 |
参考:
https://learn.microsoft.com/zh-cn/windows/win32/api/wincrypt/ns-wincrypt-crypt_algorithm_identifier
https://blog.csdn.net/vevenlcf/article/details/81531014
数字信封envelopedData数据类型由加密数据和至少一个接收者的数据加密密钥的密文组成。其中,加密数据是用数据加密密钥加密的,数据加密密钥是用接收者的公钥加密的。
该类型用于为接收者的 data、digestedData或 signedData 三种类型的数据做数字信封envelopedData数据类型结构定义如下:
EnvelopedData ::= SEQUENCE{
version Version,
recipientInfos Recipientlnfos ,
encryptedContentInfo EncryptedContentInfo
}
RecipientInfos ::= SET OF RecipientInfo
结构中各项含义见表4。
表4 EnvelopedData数据类型
字段名称 | 数据类型 | 含义 |
---|---|---|
version | Version | 版本号,此处取值为1 |
recipientInfos | RecipientInfos | 每个接收者信息的集合,至少要有一个接收者 |
encryptedContentInfo | EncryptedContentInfo | 加了密的内容信息 |
EncryptedContentInfo ::= SEQUENCE {
contentType ContentType,
contentEncryptionAlgorithm ContentEncryptionAlgorithmldentifier.
encryptedContent [0] IMPLICIT EncryptedContent OPTIONAL,
sharedInfo1 [1] IMPLICIT OCTET STRING OPTIONAL,
sharedInfo2 [2] IMPLICIT OCTET STRING OPTIONAL
}
EncryptedContent ::=OCTET STRING
结构中各项含义见表5。
表5 EncryptedContentInfo数据类型
字段名称 | 数据类型 | 含义 |
---|---|---|
contenType | ContentType | 内容的类型 |
contentEncryptionAlgorithm | ContentEncryptionAlgorithmIdentifier | 内容加密算法(和相应的参数) |
encryptedContent | EncryptedContent | 内容加密的结果,可选 |
sharedInfol | OCTET STRING | 协商好的共享信息,可选 |
sharedInfo2 | OCTET STRING | 协商好的共享信息,可选 |
每个接收者信息用recipientlnfo类型表示,
recipientnfo类型结构定义如下:
RecipientInfo ::= SEQUENCE {
version Version,
issuerAndSerialNumber IssuerAndSerialNumber,
keyEncryptionAlgorithm KeyEncryptionAlgorithmIdentifier.
encryptedKey OCTET STRING
}
结构中各项含义见表6。
表6RecipientInfo数据类型
字段名称 | 数据类型 | 含义 |
---|---|---|
version | Version | 版本号,此处取值为1 |
issuerAndSerial-Number | IssuerAndSerialNumber | 颁发者可辨别名和颁发序列号 |
keyEncryptionAlgorithm | KeyEncryptionAlgorithmIdentifier | 用接收者公钥加密数据加密密钥的算法,为SM2椭圆曲线加密算法 |
encryptedKey | OCTET STRING | 数据加密密钥密文SM2cipher,其定义见GB/T35276 |
signedAndEnvelopedData 数据类型由任意类型的加密数据、至少一个接收者的数据加密密钥和至少一个签名者的签名组成。
signedAndEnvelopedData数据类型结构定义如下:
SignedAndEnvelopedData ::=SEQUENCE {
version Version,
recipientInfos RecipientInfos ,
digestAlgorithms DigestAlgorithmIdentifiers.
encryptedContentInfo EncryptedContentInfo,
certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL,
crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
signerInfos SignerInfos
}
结构中各项含义见表7
表7signedAndEnvelopedData数据类型
字段名称 | 数据类型 | 含义 |
---|---|---|
version | Version | 版本号,此处取值为1 |
recipientInfos-Number | Recipientlnfos | 每个接受者信息的集合,至少一个元素 |
digestAlgorithms | DigestAlgorithmldentifiers | 消息摘要算法标识符的集合 |
encryptedContentInfo | EncryptedContentInfo | 加了密的内容,可以是任何定义的数据类型 |
certificates | ExtendedCertificatesAndCertificates | PKCS#6扩展证书和X.509证书的集合,是可选的 |
crls | CertificateRevocationlists | 数据加密密钥密文SM2cipher,其定义见GB/T35276 |
signerInfos | Signerlnfos | 每个签名者的集合,至少要有一个元素 |
encryptedData数据类型由任意类型的加了密的数据组成,数据类型既没有接收者也没有加密的数据加密密钥。
encryptedData数据类型定义如下:
EncryptedData ::= SEQUENCE {
version Version,
encryptedContentInfo EncryptedContentInfo
}
结构中各项含义见表8
表8encryptedData数据类型
字段名称 | 数据类型 | 含义 |
---|---|---|
version | Version | 版本号,此处取值为1 |
encryptedContenInfo-Number | EncryptedContentInfo | 已加密的内容信息 |
密钥协商keyAgreementlnfo数据类型标明两个用户之间建立一个共享秘密密钥的结构,通过这种方式能够确定一个共享秘密密钥的值
该类型用于两个用户为产生共享秘密密钥进行的公共参数交换。
KeyAgreementInfo::= SEQUENCE {
version Version
tempPublicKeyR SM2PublicKey,
userCertificate Certificate,
userID OCTET STRING
}
表 9keyAgreementInfo数据类型
字段名称 | 数据类型 | 含义 |
---|---|---|
version | Version | 版本号,此处取值为1 |
tempPublicKeyR | SM2PublicKey | 临时公钥,结构定义见GB/T33560 |
userCertificate | Certificate | 用户证书 |
userlD | OCTET STRING | 用户标识 |
椭圆曲线参数的表达采用与ANSIX9.62相同的ASN.1定义,其定义如下:
Parameters ::=CHOICE {
ecParameters ECParameters,
namedCurve Obiectldentifier,
implicitlyCA NULL
}
在用于SM2密码算法表达时,只使用namedCurve这一种表达方法,SM2密码算法曲线定义的OID。见GB/T33560。
椭圆曲线公钥的表达采用与X9.62相同的ASN.1定义,其定义如下:
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier {ECPKAlgorithms}),
subjectPublicKey SM2PublicKey
}
其中:
algorithm定义了公钥的类型
subiectPublicKey定义了公钥的实际值
AlgorithmIdentifier是对象标识与参数的绑定,其定义如下:
Algorithmldentifier ::= SEQUENCE{
algorithm OBJECT IDENTIFIER.
parameters ANY DEFINED BY algorithm OPTIONAL
}
对于SM2密码算法,其OID(algorithm)定义见GB/T 33560。
椭圆曲线私钥的表达采用与X9.62相同的ASN.1定义,其定义如下:
ECPrivateKey {CURVES:IOSet} ::= SEQUENCE {
version INTEGER { ecPrivkeyVerl(1)}(ecPrivkeyVerl),
privateKey SM2PrivateKey,
parameters [0] Parameters{(IOSet}}OPTIONAL
publicKey [1] SM2PublicKey
}
其中:
version指定了私钥的版本号,这里使用整数1来表示SM2私钥的版本号。