Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 36698e6

Browse files
committed
[FABG-717] fix - Unhandled Panic in TLSCertHash
Change-Id: If2237797fff517bc84ee91738fd2cb78c7c4c39f Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
1 parent 0bc1d75 commit 36698e6

File tree

6 files changed

+51
-22
lines changed

6 files changed

+51
-22
lines changed

pkg/core/config/comm/comm.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ import (
1111

1212
"crypto/x509"
1313

14-
cutil "github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/util"
1514
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
15+
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
16+
"github.com/pkg/errors"
1617
)
1718

1819
// TLSConfig returns the appropriate config for TLS including the root CAs,
@@ -31,17 +32,25 @@ func TLSConfig(cert *x509.Certificate, serverName string, config fab.EndpointCon
3132
}
3233

3334
// TLSCertHash is a utility method to calculate the SHA256 hash of the configured certificate (for usage in channel headers)
34-
func TLSCertHash(config fab.EndpointConfig) []byte {
35+
func TLSCertHash(config fab.EndpointConfig) ([]byte, error) {
3536
certs := config.TLSClientCerts()
3637
if len(certs) == 0 {
37-
return nil
38+
return computeHash([]byte(""))
3839
}
3940

4041
cert := certs[0]
4142
if len(cert.Certificate) == 0 {
42-
return nil
43+
return computeHash([]byte(""))
4344
}
4445

45-
h := cutil.ComputeSHA256(cert.Certificate[0])
46-
return h
46+
return computeHash(cert.Certificate[0])
47+
}
48+
49+
//computeHash computes hash for given bytes using underlying cryptosuite default
50+
func computeHash(msg []byte) ([]byte, error) {
51+
h, err := cryptosuite.GetDefault().Hash(msg, cryptosuite.GetSHA256Opts())
52+
if err != nil {
53+
return nil, errors.WithMessage(err, "failed to compute tls cert hash")
54+
}
55+
return h, err
4756
}

pkg/core/config/comm/comm_test.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/golang/mock/gomock"
2424
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/test/mockfab"
25+
"github.com/stretchr/testify/assert"
2526
)
2627

2728
func TestTLSConfigErrorAddingCertificate(t *testing.T) {
@@ -110,11 +111,9 @@ func TestNoTlsCertHash(t *testing.T) {
110111

111112
config.EXPECT().TLSClientCerts().Return([]tls.Certificate{})
112113

113-
tlsCertHash := TLSCertHash(config)
114-
115-
if len(tlsCertHash) != 0 {
116-
t.Fatal("Unexpected non-empty cert hash")
117-
}
114+
tlsCertHash, err := TLSCertHash(config)
115+
assert.NotNil(t, tlsCertHash)
116+
assert.Nil(t, err)
118117
}
119118

120119
func TestEmptyTlsCertHash(t *testing.T) {
@@ -125,11 +124,9 @@ func TestEmptyTlsCertHash(t *testing.T) {
125124
emptyCert := tls.Certificate{}
126125
config.EXPECT().TLSClientCerts().Return([]tls.Certificate{emptyCert})
127126

128-
tlsCertHash := TLSCertHash(config)
129-
130-
if len(tlsCertHash) != 0 {
131-
t.Fatal("Unexpected non-empty cert hash")
132-
}
127+
tlsCertHash, err := TLSCertHash(config)
128+
assert.NotNil(t, tlsCertHash)
129+
assert.Nil(t, err)
133130
}
134131

135132
func TestTlsCertHash(t *testing.T) {
@@ -143,8 +140,9 @@ func TestTlsCertHash(t *testing.T) {
143140
}
144141

145142
config.EXPECT().TLSClientCerts().Return([]tls.Certificate{cert})
146-
tlsCertHash := TLSCertHash(config)
147-
143+
tlsCertHash, err := TLSCertHash(config)
144+
assert.NotNil(t, tlsCertHash)
145+
assert.Nil(t, err)
148146
// openssl x509 -fingerprint -sha256 -in testdata/server.crt
149147
// SHA256 Fingerprint=0D:D5:90:B8:A5:0E:A6:04:3E:A8:75:16:BF:77:A8:FE:E7:C5:62:2D:4C:B3:CB:99:12:74:72:2A:D8:BA:B8:92
150148
expectedHash, err := hex.DecodeString("0DD590B8A50EA6043EA87516BF77A8FEE7C5622D4CB3CB991274722AD8BAB892")

pkg/fab/comm/connection.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,16 @@ func NewConnection(ctx fabcontext.Client, url string, opts ...options.Opt) (*GRP
6868
return nil, errors.Wrapf(err, "could not connect to %s", url)
6969
}
7070

71+
hash, err := comm.TLSCertHash(ctx.EndpointConfig())
72+
if err != nil {
73+
return nil, errors.Wrapf(err, "failed to get tls cert hash")
74+
}
75+
7176
return &GRPCConnection{
7277
context: ctx,
7378
commManager: commManager,
7479
conn: grpcconn,
75-
tlsCertHash: comm.TLSCertHash(ctx.EndpointConfig()),
80+
tlsCertHash: hash,
7681
}, nil
7782
}
7883

pkg/fab/discovery/discovery.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ func newAuthInfo(ctx fabcontext.Client) (*discovery.AuthInfo, error) {
124124
return nil, err
125125
}
126126

127+
hash, err := corecomm.TLSCertHash(ctx.EndpointConfig())
128+
if err != nil {
129+
return nil, errors.Wrapf(err, "failed to get tls cert hash")
130+
}
131+
127132
return &discovery.AuthInfo{
128133
ClientIdentity: identity,
129-
ClientTlsCertHash: corecomm.TLSCertHash(ctx.EndpointConfig()),
134+
ClientTlsCertHash: hash,
130135
}, nil
131136
}

pkg/fab/resource/block.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ func retrieveBlock(reqCtx reqContext.Context, orderers []fab.Orderer, channel st
3232
return nil, errors.Wrap(err, "generating TX ID failed")
3333
}
3434

35+
hash, err := ccomm.TLSCertHash(ctx.EndpointConfig())
36+
if err != nil {
37+
return nil, errors.Wrapf(err, "failed to get tls cert hash")
38+
}
39+
3540
channelHeaderOpts := txn.ChannelHeaderOpts{
3641
TxnHeader: th,
37-
TLSCertHash: ccomm.TLSCertHash(ctx.EndpointConfig()),
42+
TLSCertHash: hash,
3843
}
44+
3945
seekInfoHeader, err := txn.CreateChannelHeader(common.HeaderType_DELIVER_SEEK_INFO, channelHeaderOpts)
4046
if err != nil {
4147
return nil, errors.Wrap(err, "CreateChannelHeader failed")

pkg/fab/resource/resource.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,15 @@ func createOrUpdateChannel(reqCtx reqContext.Context, txh *txn.TransactionHeader
240240
if !ok {
241241
return errors.New("failed get client context from reqContext for Creating ChannelHeader")
242242
}
243+
244+
hash, err := ccomm.TLSCertHash(ctx.EndpointConfig())
245+
if err != nil {
246+
return errors.WithMessage(err, "failed to get tls cert hash")
247+
}
248+
243249
channelHeaderOpts := txn.ChannelHeaderOpts{
244250
TxnHeader: txh,
245-
TLSCertHash: ccomm.TLSCertHash(ctx.EndpointConfig()),
251+
TLSCertHash: hash,
246252
}
247253
channelHeader, err := txn.CreateChannelHeader(common.HeaderType_CONFIG_UPDATE, channelHeaderOpts)
248254
if err != nil {

0 commit comments

Comments
 (0)