1 Star 0 Fork 0

yzsunjianguo / sponge

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.go 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
yzsunjianguo 提交于 2024-02-08 14:53 . init
// Package gtls provides grpc secure connectivity, supporting both server-only authentication and client-server authentication.
package gtls
import (
"crypto/tls"
"crypto/x509"
"errors"
"os"
"google.golang.org/grpc/credentials"
)
// GetServerTLSCredentialsByCA two-way authentication via CA-issued root certificate
func GetServerTLSCredentialsByCA(caFile string, certFile string, keyFile string) (credentials.TransportCredentials, error) {
//read and parse the information from the certificate file to obtain the certificate public key, key pair
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}
// create an empty CertPool
certPool := x509.NewCertPool()
ca, err := os.ReadFile(caFile)
if err != nil {
return nil, err
}
//attempts to parse the incoming PEM-encoded certificate. If the parsing is successful it will be added to the CertPool for later use
if ok := certPool.AppendCertsFromPEM(ca); !ok {
return nil, errors.New("certPool.AppendCertsFromPEM err")
}
//building TLS-based TransportCredentials options
c := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cert}, // set up a certificate chain that allows the inclusion of one or more
ClientAuth: tls.RequireAndVerifyClientCert, // requirement to verify the client's certificate
ClientCAs: certPool, // set the set of root certificates and use the mode set in ClientAuth for verification
})
return c, err
}
// GetServerTLSCredentials server-side authentication
func GetServerTLSCredentials(certFile string, keyFile string) (credentials.TransportCredentials, error) {
c, err := credentials.NewServerTLSFromFile(certFile, keyFile)
if err != nil {
return nil, err
}
return c, err
}
Go
1
https://gitee.com/yzsunjianguo/sponge.git
git@gitee.com:yzsunjianguo/sponge.git
yzsunjianguo
sponge
sponge
v1.0.3

搜索帮助