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SsVPfhr+gnDASMj5Syt+hxs
amHygPecjCLbcdFQQgIgSXC4musF5Fnj/CpNTqvk9+56FuINkATGS8xRh7kzKBE=
有没有对国密比较了解的,请问一下如何通过 golang 从这个 sm2 文件提取私钥?
或者 gmssl/openssl 有办法直接提取吗?
附上一个简单的测试用例,方便其它有需要的朋友.
注: 使用了 github.com/tjfoc/gmsm 这个库
type sm4Cipher struct {
Version int
Seq struct {
Obj1 asn1.ObjectIdentifier
Obj2 asn1.ObjectIdentifier
PrivateKey []byte
}
}
func TestSm2(t *testing.T) {
data := `MIIDSQIBATBHBgoqgRzPVQYBBAIBBgcqgRzPVQFoBDDkLvKllj9ZWhaKU6MSnxBBV5yaF3tEcOk1
vQniWyVzyaQA4F3j/YvDJwEoE8gOF/swggL5BgoqgRzPVQYBBAIBBIIC6TCCAuUwggKJoAMCAQIC
BRBAmQgJMAwGCCqBHM9VAYN1BQAwXDELMAkGA1UEBhMCQ04xMDAuBgNVBAoMJ0NoaW5hIEZpbmFu
Y2lhbCBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTEbMBkGA1UEAwwSQ0ZDQSBURVNUIFNNMiBPQ0Ex
MB4XDTIwMTExOTA4MzExOFoXDTI1MTExOTA4MzExOFowgYkxCzAJBgNVBAYTAkNOMRcwFQYDVQQK
DA5DRkNBIFRFU1QgT0NBMTENMAsGA1UECwwEUFNCQzEZMBcGA1UECwwQT3JnYW5pemF0aW9uYWwt
MjE3MDUGA1UEAwwuMDUxQOmCruWCqOe6v+S4iuaUtuWNleWVhuaIt0BONTEwMTEzMDAwMTg4NzhA
MTBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABJVRC63OKfcL4H324rDOdb4SSlbAjoJDXnK0qmwX
Z59FWmiSqt3ipreljKew4QynjTgR/yfp9yjNgNU8G5pkYdujggEGMIIBAjAfBgNVHSMEGDAWgBRr
/hjaj0I6prhtsy6Igzo0osEw4TAMBgNVHRMBAf8EAjAAMEgGA1UdIARBMD8wPQYIYIEchu8qAQEw
MTAvBggrBgEFBQcCARYjaHR0cDovL3d3dy5jZmNhLmNvbS5jbi91cy91cy0xNC5odG0wOQYDVR0f
BDIwMDAuoCygKoYoaHR0cDovL3VjcmwuY2ZjYS5jb20uY24vU00yL2NybDE0MzU2LmNybDAOBgNV
HQ8BAf8EBAMCBsAwHQYDVR0OBBYEFPiGPZT0oTuRXvkyGoOgviNEWnc1MB0GA1UdJQQWMBQGCCsG
AQUFBwMCBggrBgEFBQcDBDAMBggqgRzPVQGDdQUAA0gAMEUCIQCJDSsVPfhr+gnDASMj5Syt+hxs
amHygPecjCLbcdFQQgIgSXC4musF5Fnj/CpNTqvk9+56FuINkATGS8xRh7kzKBE=`
der, _ := base64.StdEncoding.DecodeString(data)
var pk sm4Cipher
_, e := asn1.Unmarshal(der, &pk)
if e != nil {
t.Fatal(e)
}
sm3Hash := sm3.Sm3Sum([]byte{'1', '2', '3', '4', '5', '6', 0, 0, 0, 1})
_ = sm4.SetIV(sm3Hash[:16])
out, e := sm4.Sm4Cbc(sm3Hash[16:], pk.Seq.PrivateKey, false)
sm2private, e := x509.ReadPrivateKeyFromHex(hex.EncodeToString(out))
privateHex := x509.WritePrivateKeyToHex(sm2private)
if privateHex != "d3f24d61bb2816882b8474b778dd7c3166d665f9455dc9d551c989c161e76ab0" {
t.Fail()
}
}
1
hxndg 2022-05-09 12:04:40 +08:00
这个看起来就是 pem 文件掐头去尾,应该可以直接用 openssl 看的
|
2
mengyx 2022-05-09 12:20:32 +08:00
这个是 SM2 的证书吧,不包含私钥
|
3
chenall OP @hxndg
不知要用什么命令? 我有试了像下面的命令不行 gmssl x509 -in test.sm2 -passin pass:123456 @mengyx java 的读取代码如下,可以读取到公钥和私钥. ``` import cfca.sadk.util.KeyUtil; public class HelloWorld { public static void main(String[] args) { PrivateKey priKey = KeyUtil.getPrivateKeyFromSM2("test.sm2", "123456"); System.out.println(priKey.toString()); System.out.println(args.length); } } ``` |
4
mengyx 2022-05-09 13:53:39 +08:00 1
|
5
Pinattsu 2022-05-09 14:35:17 +08:00
提取用户证书:
openssl pkcs12 -in 1.p12 -clcerts -nokeys -out cert.pem //pem 格式 openssl pkcs12 -in 1.p12 -clcerts -nokeys -out cert.crt //crt 格式 提取公钥 openssl pkcs12 -in 1.p12 -clcerts -out public_key.pem //pem 格式 openssl pkcs12 -in 1.p12 -clcerts -out cert.crt //crt 格式 提取私钥: openssl pkcs12 -in 1.p12 -nodes -out private_key.pem |
6
Pinattsu 2022-05-09 14:38:29 +08:00 1
golang 可以试试这个 https://github.com/tjfoc/gmsm
我用过这个库解析 p12 文件 |
7
chenall OP @Pinattsu
试了,会报错 wrong tag. 用 gmsl 提示 tags don't match (16 vs {class:0 tag:6 length:10 isCompound:false}) |
8
mengyx 2022-05-09 15:33:04 +08:00
@chenall #3 反编译看了一下你提到的 SDK ,感觉这个 key/iv 派生方式,不是标准的 PKCS#12 ,可能是自定义的
既然有 SDK 能够转换为标准格式,你就先转换导出成 PEM/DER ,再在 Go 里面读取好了 |
10
chenall OP @mengyx @Pinattsu @hxndg
感谢各位的回复,目前好像只能先用 sdk 转换提取再使用了. 另外我刚有找到了这篇文章. https://www.thunes.com/2021/12/29/digital-signature-usage-of-chinese-cryptography-standards/ 太概就是 asn.1 第一段 的那 48 个字节就是 经过 sm4 加密的 私钥. 我有空再研究一下,应该可以通过 sm4 来解密. |
11
mengyx 2022-05-09 17:36:50 +08:00
@chenall #10 看代码是
1. 先将 password 和 \0x00\0x00\0x00\0x00 拼接,然后用 SM3 散列得到 32byte 2. 取前 16B 作为 IV ,后 16B 作为 Key ,使用 SM4(CBC, PKCS7 Padding), 解密得到私钥 |
12
chenall OP @mengyx
感谢,测试了一下确实可以, sm3 散列是 password 和 \0x00\0x00\0x00\0x01 拼接的( #8 图的内容). 先用 asn.1 解析得到 密文,然后解密就可以得到私钥了. sm4data, _ := hex.DecodeString("E42EF2A5963F595A168A53A3129F1041579C9A177B4470E935BD09E25B2573C9A400E05DE3FD8BC327012813C80E17FB") sm3Hash := sm3.Sm3Sum([]byte{'1', '2', '3', '4', '5', '6', 0, 0, 0, 1}) _ = sm4.SetIV(sm3Hash[:16]) out, e := sm4.Sm4Cbc(sm3Hash[16:], sm4data, false) |