forked from hyperledger/fabric-sdk-go
/
comm.go
56 lines (44 loc) · 1.55 KB
/
comm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"crypto/tls"
"crypto/x509"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/core/cryptosuite"
"github.com/pkg/errors"
)
// TLSConfig returns the appropriate config for TLS including the root CAs,
// certs for mutual TLS, and server host override. Works with certs loaded either from a path or embedded pem.
func TLSConfig(cert *x509.Certificate, serverName string, config fab.EndpointConfig) (*tls.Config, error) {
if cert != nil {
config.TLSCACertPool().Add(cert)
}
certPool, err := config.TLSCACertPool().Get()
if err != nil {
return nil, err
}
return &tls.Config{RootCAs: certPool, Certificates: config.TLSClientCerts(), ServerName: serverName}, nil
}
// TLSCertHash is a utility method to calculate the SHA256 hash of the configured certificate (for usage in channel headers)
func TLSCertHash(config fab.EndpointConfig) ([]byte, error) {
certs := config.TLSClientCerts()
if len(certs) == 0 {
return computeHash([]byte(""))
}
cert := certs[0]
if len(cert.Certificate) == 0 {
return computeHash([]byte(""))
}
return computeHash(cert.Certificate[0])
}
//computeHash computes hash for given bytes using underlying cryptosuite default
func computeHash(msg []byte) ([]byte, error) {
h, err := cryptosuite.GetDefault().Hash(msg, cryptosuite.GetSHA256Opts())
if err != nil {
return nil, errors.WithMessage(err, "failed to compute tls cert hash")
}
return h, err
}