我通过 openssl 连接到某个服务器:
openssl s_client -crlf -connect somehost.com:700 -cert key.pem
它有效。连接成功。
但是当我尝试从 Go 代码(文档中的示例)中做同样的事情时,它对我不起作用:
import (
"crypto/tls"
"crypto/x509"
)
func main() {
// Connecting with a custom root-certificate set.
const rootPEM = `
-----BEGIN CERTIFICATE-----
my key text
-----END CERTIFICATE-----`
// First, create the set of root certificates. For this example we only
// have one. It's also possible to omit this in order to use the
// default root set of the current operating system.
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(rootPEM))
if !ok {
panic("failed to parse root certificate")
}
conn, err := tls.Dial("tcp", "somehost.com:700", &tls.Config{
RootCAs: roots,
})
if err != nil {
panic("failed to connect: " + err.Error())
}
conn.Close()
}
我的文字错误是:
panic: failed to connect: x509: certificate is valid for otherhost.com, not somehost.com [recovered]
问题:我做错了什么?也许我没有添加一些 tls.Config 参数?
幕布斯6054654
慕婉清6462132
相关分类