pkcs7: follow GB/T 35275-2017 9.1 strictly

This commit is contained in:
Sun Yimin 2024-06-20 11:49:10 +08:00 committed by GitHub
parent 0445d16e97
commit dc66ca673e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 4 deletions

View File

@ -33,17 +33,22 @@
关于```EncryptSM / EncryptCFCA```的区别,请参考**CFCA互操作性指南**。
带PSKPre-shared key后缀的方法其对称加密密钥由调用者提供而非随机生成。
### 加密数据Encrypted Data
加密:对应本项目的```pkcs7.EncryptUsingPSK```和```pkcs7.EncryptSMUsingPSK```方法。
解密:对应本项目的```pkcs7.DecryptUsingPSK```方法(当然要先调用```pkcs7.Parse```)。
### 签名数据Signed Data
签名数据,使用证书对应的私钥进行签名,理论上支持多个签名者,但通常使用场景都是单签。和数字信封数据类似,也分国密和非国密。
#### 创建签名数据
是否国密是指OID也使用国密体系
| 是否国密 | 方法 |
| :--- | :--- |
| 否 | ```NewSignedData``` |
| 是 | ```NewSMSignedData``` |
| 是否国密 | 方法 | 默认签名算法 |
| :--- | :--- | :--- |
| 否 | ```NewSignedData``` | SHA1 |
| 是 | ```NewSMSignedData``` | SM3 |
可选步骤:调用```SetDigestAlgorithm```设置想要的签名算法,通常国密不需要修改。
接着调用```AddSigner```或```AddSignerChain```方法,进行签名;可以通过```SignerInfoConfig.SkipCertificates```指定忽略证书项(最终签名数据中不包含证书项);
如果进行Detach签名则调用```Detach```方法;
最后调用```Finish```方法,序列化输出结果。

View File

@ -112,6 +112,7 @@ func encrypt(cipher pkcs.Cipher, content []byte, recipients []*smx509.Certificat
}
if isSM {
envelope.Version = 1 // follow GB/T 35275-2017 9.1
envelope.EncryptedContentInfo.ContentType = SM2OIDData
}
@ -141,6 +142,9 @@ func encrypt(cipher pkcs.Cipher, content []byte, recipients []*smx509.Certificat
},
EncryptedKey: encrypted,
}
if isSM {
info.Version = 1 // follow GB/T 35275-2017 9.1
}
recipientInfos[i] = info
}
@ -199,6 +203,7 @@ func encryptUsingPSK(isSM bool, cipher pkcs.Cipher, content []byte, key []byte)
},
}
if isSM {
ed.Version = 1 // follow GB/T 35275-2017 9.1
ed.EncryptedContentInfo.ContentType = SM2OIDData
}