代码拉取完成,页面将自动刷新
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")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。