Skip to content

Commit

Permalink
fix "CreateCRL will panic using sm2 key" (#31)
Browse files Browse the repository at this point in the history
Signed-off-by: manxiaqu <[email protected]>

Co-authored-by: David Liu <[email protected]>
  • Loading branch information
manxiaqu and David Liu authored Mar 7, 2022
1 parent 3bf2994 commit 66c79ee
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/Hyperledger-TWGC/ccs-gm

go 1.16

require golang.org/x/crypto v0.0.0-20220214200702-86341886e292

require (
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20220214200702-86341886e292
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 h1:f+lwQ+GtmgoY+A2YaQxlSOnDjXcQ7ZRLWOHbC6HtRqE=
golang.org/x/crypto v0.0.0-20220214200702-86341886e292/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
Expand All @@ -8,3 +15,7 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

7 changes: 6 additions & 1 deletion x509/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -2037,7 +2037,12 @@ func (c *Certificate) CreateCRL(rand io.Reader, priv interface{}, revokedCerts [
return
}

h := hashFunc.New()
var h hash.Hash
if hashFunc == 255 {
h = sm3.New()
} else {
h = hashFunc.New()
}
h.Write(tbsCertListContents)
digest := h.Sum(nil)

Expand Down
42 changes: 42 additions & 0 deletions x509/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import (
"strings"
"testing"
"time"

"github.com/Hyperledger-TWGC/ccs-gm/sm2"
"github.com/stretchr/testify/require"
)

func TestParsePKCS1PrivateKey(t *testing.T) {
Expand Down Expand Up @@ -829,6 +832,45 @@ func TestCRLCreation(t *testing.T) {
}
}

func TestSm2CRLCreation(t *testing.T) {
loc := time.FixedZone("Oz/Atlantis", int((2 * time.Hour).Seconds()))
now := time.Unix(1000, 0).In(loc)
nowUTC := now.UTC()
expiry := time.Unix(10000, 0)
revokedCerts := []pkix.RevokedCertificate{
{
SerialNumber: big.NewInt(1),
RevocationTime: nowUTC,
},
{
SerialNumber: big.NewInt(42),
// RevocationTime should be converted to UTC before marshaling.
RevocationTime: now,
},
}
priv, _ := sm2.GenerateKey(rand.Reader)
template := &Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
CommonName: "Σ Acme Co",
},
NotBefore: time.Unix(1000, 0),
NotAfter: time.Unix(100000, 0),

BasicConstraintsValid: true,
IsCA: true,
}
certBytes, err := CreateCertificate(rand.Reader, template, template, &priv.PublicKey, priv)
require.Nil(t, err)
cert, err := ParseCertificate(certBytes)
require.Nil(t, err)

// Panic at this if the sm3 hashFunc not checked
if _, err := cert.CreateCRL(rand.Reader, priv, revokedCerts, now, expiry); err != nil {
t.Errorf("error creating CRL: %s", err)
}
}

func fromBase64(in string) []byte {
out := make([]byte, base64.StdEncoding.DecodedLen(len(in)))
n, err := base64.StdEncoding.Decode(out, []byte(in))
Expand Down

0 comments on commit 66c79ee

Please sign in to comment.