forked from ThienLK1/fabric-v2.2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
creds.go
162 lines (132 loc) · 4.56 KB
/
creds.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"net"
"sync"
"github.com/hyperledger/fabric/common/flogging"
"google.golang.org/grpc/credentials"
)
var (
ErrClientHandshakeNotImplemented = errors.New("core/comm: client handshakes are not implemented with serverCreds")
ErrServerHandshakeNotImplemented = errors.New("core/comm: server handshakes are not implemented with clientCreds")
ErrOverrideHostnameNotSupported = errors.New("core/comm: OverrideServerName is not supported")
// alpnProtoStr are the specified application level protocols for gRPC.
alpnProtoStr = []string{"h2"}
)
// NewServerTransportCredentials returns a new initialized
// grpc/credentials.TransportCredentials
func NewServerTransportCredentials(
serverConfig *TLSConfig,
logger *flogging.FabricLogger) credentials.TransportCredentials {
// NOTE: unlike the default grpc/credentials implementation, we do not
// clone the tls.Config which allows us to update it dynamically
serverConfig.config.NextProtos = alpnProtoStr
// override TLS version and ensure it is 1.2
serverConfig.config.MinVersion = tls.VersionTLS12
serverConfig.config.MaxVersion = tls.VersionTLS12
return &serverCreds{
serverConfig: serverConfig,
logger: logger}
}
// serverCreds is an implementation of grpc/credentials.TransportCredentials.
type serverCreds struct {
serverConfig *TLSConfig
logger *flogging.FabricLogger
}
type TLSConfig struct {
config *tls.Config
lock sync.RWMutex
}
func NewTLSConfig(config *tls.Config) *TLSConfig {
return &TLSConfig{
config: config,
}
}
func (t *TLSConfig) Config() tls.Config {
t.lock.RLock()
defer t.lock.RUnlock()
if t.config != nil {
return *t.config.Clone()
}
return tls.Config{}
}
func (t *TLSConfig) AddClientRootCA(cert *x509.Certificate) {
t.lock.Lock()
defer t.lock.Unlock()
t.config.ClientCAs.AddCert(cert)
}
func (t *TLSConfig) SetClientCAs(certPool *x509.CertPool) {
t.lock.Lock()
defer t.lock.Unlock()
t.config.ClientCAs = certPool
}
// ClientHandShake is not implemented for `serverCreds`.
func (sc *serverCreds) ClientHandshake(context.Context,
string, net.Conn) (net.Conn, credentials.AuthInfo, error) {
return nil, nil, ErrClientHandshakeNotImplemented
}
// ServerHandshake does the authentication handshake for servers.
func (sc *serverCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
serverConfig := sc.serverConfig.Config()
conn := tls.Server(rawConn, &serverConfig)
if err := conn.Handshake(); err != nil {
if sc.logger != nil {
sc.logger.With("remote address",
conn.RemoteAddr().String()).Errorf("TLS handshake failed with error %s", err)
}
return nil, nil, err
}
return conn, credentials.TLSInfo{State: conn.ConnectionState()}, nil
}
// Info provides the ProtocolInfo of this TransportCredentials.
func (sc *serverCreds) Info() credentials.ProtocolInfo {
return credentials.ProtocolInfo{
SecurityProtocol: "tls",
SecurityVersion: "1.2",
}
}
// Clone makes a copy of this TransportCredentials.
func (sc *serverCreds) Clone() credentials.TransportCredentials {
config := sc.serverConfig.Config()
serverConfig := NewTLSConfig(&config)
return NewServerTransportCredentials(serverConfig, sc.logger)
}
// OverrideServerName overrides the server name used to verify the hostname
// on the returned certificates from the server.
func (sc *serverCreds) OverrideServerName(string) error {
return ErrOverrideHostnameNotSupported
}
type DynamicClientCredentials struct {
TLSConfig *tls.Config
TLSOptions []TLSOption
}
func (dtc *DynamicClientCredentials) latestConfig() *tls.Config {
tlsConfigCopy := dtc.TLSConfig.Clone()
for _, tlsOption := range dtc.TLSOptions {
tlsOption(tlsConfigCopy)
}
return tlsConfigCopy
}
func (dtc *DynamicClientCredentials) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
return credentials.NewTLS(dtc.latestConfig()).ClientHandshake(ctx, authority, rawConn)
}
func (dtc *DynamicClientCredentials) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
return nil, nil, ErrServerHandshakeNotImplemented
}
func (dtc *DynamicClientCredentials) Info() credentials.ProtocolInfo {
return credentials.NewTLS(dtc.latestConfig()).Info()
}
func (dtc *DynamicClientCredentials) Clone() credentials.TransportCredentials {
return credentials.NewTLS(dtc.latestConfig())
}
func (dtc *DynamicClientCredentials) OverrideServerName(name string) error {
dtc.TLSConfig.ServerName = name
return nil
}