我正在尝试设置一个http.ListenAndServeTLS()
带有自签名证书的 https 服务器 (
<date> <time> http: TLS handshake error from <IP>: remote error: tls: bad certificate
我已经寻找解决方案并尝试了几个(在 中设置InsecureSkipVerify
为 true TLSConfig
,将我的证书添加到Certificates
配置字段或使用自签名证书签署另一个证书并将后者附加到RootCAs
),但没有解决问题(最后一个解决方案不出所料地将错误更改为unknown certificate authority
)。
正如我所提到的,我很确定问题与我正在尝试收听的服务无关,所以在 Go 中设置服务器时一定有一些非常简单的东西我错过了。我的代码看起来像这样:
rootCAs, _ := x509.SystemCertPool()
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
certs, _ := ioutil.ReadFile(CA_PATH)
_ = rootCAs.AppendCertsFromPEM(certs);
server := http.Server{
Addr: "",
Handler: nil,
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
Certificates: []tls.Certificate{ my_cert },
RootCAs: rootCAs,
},
}
fmt.Println(server.ListenAnsServeTLS(PEM_PATH, KEY_PATH))