63 Star 183 Fork 3

Gitee 极速下载/hyperledger-fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
克隆/下载
creds.go 3.94 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"context"
"crypto/tls"
"errors"
"net"
"github.com/hyperledger/fabric/common/flogging"
"google.golang.org/grpc/credentials"
)
var (
ClientHandshakeNotImplError = errors.New("core/comm: Client handshakes" +
"are not implemented with serverCreds")
OverrrideHostnameNotSupportedError = errors.New(
"core/comm: OverrideServerName is " +
"not supported")
ServerHandshakeNotImplementedError = errors.New("core/comm: server handshakes are not implemented with clientCreds")
MissingServerConfigError = errors.New(
"core/comm: `serverConfig` cannot be nil")
// alpnProtoStr are the specified application level protocols for gRPC.
alpnProtoStr = []string{"h2"}
)
// NewServerTransportCredentials returns a new initialized
// grpc/credentials.TransportCredentials
func NewServerTransportCredentials(
serverConfig *tls.Config,
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.NextProtos = alpnProtoStr
// override TLS version and ensure it is 1.2
serverConfig.MinVersion = tls.VersionTLS12
serverConfig.MaxVersion = tls.VersionTLS12
return &serverCreds{
serverConfig: serverConfig,
logger: logger}
}
// serverCreds is an implementation of grpc/credentials.TransportCredentials.
type serverCreds struct {
serverConfig *tls.Config
logger *flogging.FabricLogger
}
// ClientHandShake is not implemented for `serverCreds`.
func (sc *serverCreds) ClientHandshake(context.Context,
string, net.Conn) (net.Conn, credentials.AuthInfo, error) {
return nil, nil, ClientHandshakeNotImplError
}
// ServerHandshake does the authentication handshake for servers.
func (sc *serverCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
conn := tls.Server(rawConn, sc.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 {
creds := NewServerTransportCredentials(sc.serverConfig, sc.logger)
return creds
}
// OverrideServerName overrides the server name used to verify the hostname
// on the returned certificates from the server.
func (sc *serverCreds) OverrideServerName(string) error {
return OverrrideHostnameNotSupportedError
}
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, ServerHandshakeNotImplementedError
}
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v1.4.6

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385