Skip to content

Commit

Permalink
[FAB-17632] Exclude unnecessary code from ecdsa.go (#60)
Browse files Browse the repository at this point in the history
* Clean GenSKI() method

Signed-off-by: vadiminshakov <vadiminshakov@gmail.com>
Signed-off-by: Vadim Inshakov <vadiminshakov@gmail.com>
  • Loading branch information
vadiminshakov committed Mar 23, 2020
1 parent e71412f commit e2b6c73
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
6 changes: 5 additions & 1 deletion scripts/_go/src/pkcs11helper/go.mod
Expand Up @@ -4,6 +4,10 @@

module pkcs11helper

require github.com/miekg/pkcs11 v0.0.0-20190329070431-55f3fac3af27
require (
github.com/miekg/pkcs11 v0.0.0-20190329070431-55f3fac3af27
github.com/olekukonko/tablewriter v0.0.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
)

go 1.13
7 changes: 7 additions & 0 deletions scripts/_go/src/pkcs11helper/go.sum
@@ -1 +1,8 @@
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/miekg/pkcs11 v0.0.0-20190329070431-55f3fac3af27 h1:XA/VH+SzpYyukhgh7v2mTp8rZoKKITXR/x3FIizVEXs=
github.com/miekg/pkcs11 v0.0.0-20190329070431-55f3fac3af27/go.mod h1:WCBAbTOdfhHhz7YXujeZMF7owC4tPb1naKFsgfUISjo=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
17 changes: 12 additions & 5 deletions scripts/_go/src/pkcs11helper/pkg/pkcs11wrapper/ecdsa.go
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/hex"
"encoding/pem"
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"math/big"
)
Expand All @@ -35,26 +36,32 @@ type SubjectKeyIdentifier struct {
}

// SKI returns the subject key identifier of this key.
func (k *EcdsaKey) GenSKI() (ski []byte) {
func (k *EcdsaKey) GenSKI() error {
if k.PubKey == nil {
return nil
return errors.New("PubKey is empty")
}

// Marshall the public key
raw := elliptic.Marshal(k.PubKey.Curve, k.PubKey.X, k.PubKey.Y)

// Hash it
hash := sha256.New()
hash.Write(raw)
_, err := hash.Write(raw)
if err != nil {
return errors.Wrap(err, "Failed to write hash")
}
k.SKI.Sha256Bytes = hash.Sum(nil)
k.SKI.Sha256 = hex.EncodeToString(k.SKI.Sha256Bytes)

hash = sha1.New()
hash.Write(raw)
_, err = hash.Write(raw)
if err != nil {
return errors.Wrap(err, "Failed to write hash")
}
k.SKI.Sha1Bytes = hash.Sum(nil)
k.SKI.Sha1 = hex.EncodeToString(k.SKI.Sha1Bytes)

return
return nil
}

func (k *EcdsaKey) Generate(namedCurve string) (err error) {
Expand Down
8 changes: 6 additions & 2 deletions scripts/_go/src/pkcs11helper/pkg/pkcs11wrapper/pkcs11.go
Expand Up @@ -9,8 +9,8 @@ import (
"crypto/elliptic"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"github.com/pkg/errors"
"os"

"github.com/miekg/pkcs11"
Expand Down Expand Up @@ -236,7 +236,11 @@ func (p11w *Pkcs11Wrapper) ImportECKey(ec EcdsaKey) (err error) {
return
}

ec.GenSKI()
err = ec.GenSKI()
if err != nil {
err = errors.Wrap(err, "failed to generate SKI")
return
}

marshaledOID, err := GetECParamMarshaled(ec.PrivKey.Params().Name)
if err != nil {
Expand Down

0 comments on commit e2b6c73

Please sign in to comment.