Skip to content

Commit

Permalink
[FABG-1024] Put valid PublicKey in client PrivateKey (#157)
Browse files Browse the repository at this point in the history
Signed-off-by: David Mazary <david@corsha.com>
  • Loading branch information
DavidCorsha committed Dec 24, 2020
1 parent 2dcfaa9 commit 88e5e47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/core/config/cryptoutil/cryptoutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func X509KeyPair(certPEMBlock []byte, pk core.Key, cs core.CryptoSuite) (tls.Cer

switch x509Cert.PublicKey.(type) {
case *ecdsa.PublicKey:
cert.PrivateKey = &PrivateKey{cs, pk, &ecdsa.PublicKey{}}
cert.PrivateKey = &PrivateKey{cs, pk, x509Cert.PublicKey}
default:
return fail(errors.New("tls: unknown public key algorithm"))
}
Expand Down
17 changes: 16 additions & 1 deletion pkg/core/config/cryptoutil/cryptoutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package cryptoutil

import (
"crypto/ecdsa"
"testing"

fabricCaUtil "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric-ca/sdkinternal/pkg/util"
Expand Down Expand Up @@ -66,11 +67,25 @@ func TestX509KeyPair(t *testing.T) {
}

// ECSDA Cert
_, err = X509KeyPair([]byte(ecdsaCert), nil, cs)
cert, err := X509KeyPair([]byte(ecdsaCert), nil, cs)
if err != nil {
t.Fatalf("Failed to load key pair: %s", err)
}

key, ok := cert.PrivateKey.(*PrivateKey)
if !ok {
t.Fatal("Should have loaded private key as cryptoutils.PrivateKey")
}

pubKey, ok := key.Public().(*ecdsa.PublicKey)
if !ok {
t.Fatal("Should have loaded public key as ECDSA")
}

// Valid public key in private key
if pubKey.Curve == nil {
t.Fatal("Should have loaded private key with valid public key")
}
}

func TestPrivateKey(t *testing.T) {
Expand Down

0 comments on commit 88e5e47

Please sign in to comment.