1 Star 0 Fork 0

h79/goutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
tls.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-11-24 23:56 . db tls
package util
import (
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"gitee.com/h79/goutils/common/result"
"gitee.com/h79/goutils/dao/config"
"os"
)
func GetCertificate(t *config.Tls) (tls.Certificate, *x509.CertPool, error) {
if t.Key == "" || t.CaFile == "" || t.CertFile == "" || t.KeyFile == "" {
return tls.Certificate{}, nil, result.RErrParam
}
cert, err := tls.LoadX509KeyPair(t.CertFile, t.KeyFile)
if err != nil {
return tls.Certificate{}, nil, err
}
certPool := x509.NewCertPool()
ca, err := os.ReadFile(t.CaFile)
if err != nil {
return tls.Certificate{}, nil, err
}
if ok := certPool.AppendCertsFromPEM(ca); !ok {
return tls.Certificate{}, nil, err
}
return cert, certPool, nil
}
func GetServerPubKey(pk *config.ServerPubKey) (*rsa.PublicKey, error) {
if pk.Key == "" || pk.PemFile == "" {
return nil, result.RErrParam
}
data, err := os.ReadFile(pk.PemFile)
if err != nil {
return nil, err
}
block, _ := pem.Decode(data)
if block == nil || block.Type != "PUBLIC KEY" {
return nil, errors.New("failed to decode PEM block containing public key")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
if rsaPubKey, ok := pub.(*rsa.PublicKey); ok {
return rsaPubKey, nil
}
return nil, errors.New("failure")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/h79/goutils.git
git@gitee.com:h79/goutils.git
h79
goutils
goutils
v1.20.19

搜索帮助