Skip to content

Commit

Permalink
修复 X509 的 SM2 测试证书
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Mar 6, 2024
1 parent d95c945 commit 87cdebd
Showing 1 changed file with 44 additions and 22 deletions.
66 changes: 44 additions & 22 deletions x509/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1167,33 +1167,39 @@ func Test_P12_Gost_222(t *testing.T) {
}
}

var testSM2Cert = `
var testSM2RootCaCert = `
-----BEGIN CERTIFICATE-----
MIICTTCCAfOgAwIBAgIJAOWoGwJCndt2MAoGCCqBHM9VAYN1MGcxCzAJBgNVBAYT
AkNOMRAwDgYDVQQIDAdCZWlqaW5nMRAwDgYDVQQHDAdIYWlEaWFuMRMwEQYDVQQK
DApHTUNlcnQub3JnMR8wHQYDVQQDDBZHTUNlcnQgR00gUm9vdCBDQSAtIDAxMB4X
DTI0MDEyNDA5MDAwMFoXDTI1MDEyMzA5MDAwMFowYjELMAkGA1UEBhMCNjYxCzAJ
BgNVBAgMAjU1MQswCQYDVQQHDAI0NDELMAkGA1UEBwwCMzMxCzAJBgNVBAoMAjIy
MQswCQYDVQQKDAIxMTESMBAGA1UEAwwJdGVzdF9OVUxMMFkwEwYHKoZIzj0CAQYI
KoEcz1UBgi0DQgAEnA3ttWiJUt1tF0sEPALgYhevNXYSUa6lzJ4ZbGM8NQiHRlva
bVrlmcLIG1l1byDo/trnduv5q0dQdFmfYihh2aOBjDCBiTAMBgNVHRMBAf8EAjAA
MAsGA1UdDwQEAwIHgDAsBglghkgBhvhCAQ0EHxYdR01DZXJ0Lm9yZyBTaWduZWQg
Q2VydGlmaWNhdGUwHQYDVR0OBBYEFLgpNzezd7Hs307jtoQXYGW3oQH3MB8GA1Ud
IwQYMBaAFH9aXjsAhFkqD5i+oQ5vOZVDEE0HMAoGCCqBHM9VAYN1A0gAMEUCIQDh
KwUL+a/JnoDf8cXaUkrty+t9f7H/Ob0ElI5ETcZHdAIgYHmgM/n3M5a04b+4wrna
EUPf68vuiWz0E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QYDVQQLDAJERDEQMA4GA1UEAwwHcm9v
dCBjYTAgFw0yMzAyMjIwMjMwMTNaGA8yMTIzMDEyOTAyMzAxM1owRTELMAkGA1UE
BhMCQUExCzAJBgNVBAgMAkJCMQswCQYDVQQKDAJDQzELMAkGA1UECwwCREQxDzAN
BgNVBAMMBnN1YiBjYTBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABH0feWwae0S0
w4QQA5cBGYwaQPaxZFcLzIqph+I6BQQCGXaIAabqpO0zjAyf1twYmoM3ZRLJgbZz
HE/2rRMPBiajZjBkMB0GA1UdDgQWBBSsYesigGJZCD6WyNF/znRcAq88mTAfBgNV
HSMEGDAWgBTG7uYyZVoHe+rslGLxQQUdzFUVjjASBgNVHRMBAf8ECDAGAQH/AgEA
MA4GA1UdDwEB/wQEAwIBhjAKBggqgRzPVQGDdQNJADBGAiEApoHDue1bzGukE97O
BqQbboU1d3jqNg4gAgpMe5fFIosCIQDwndSp7Tc3DZ0QCifXKNqgykjepsWTPZ3R
NrMzM0rflg==
-----END CERTIFICATE-----
`

func Test_P12_SM2(t *testing.T) {
certpem := decodePEM(testSM2Cert)
certpem := decodePEM(testSM2RootCaCert)

cert, err := ParseCertificate(certpem)
if err != nil {
Expand All @@ -1217,6 +1223,22 @@ func Test_P12_SM2(t *testing.T) {

err = cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature)
if err != nil {
// t.Fatal(err)
t.Fatal(err)
}

// ==========

subCertpem := decodePEM(testSM2SubCaCert)

subCert, err := ParseCertificate(subCertpem)
if err != nil {
t.Fatal(err)
}

// use root ca PublicKey to Check Signature
subCert.PublicKey = pubKey
err = subCert.CheckSignature(subCert.SignatureAlgorithm, subCert.RawTBSCertificate, subCert.Signature)
if err != nil {
t.Fatal(err)
}
}

0 comments on commit 87cdebd

Please sign in to comment.