我通过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参数?
发布于 2016-01-30 11:25:52
我不需要检查服务器的ssl证书。它是某个域名注册中心的演示服务器。所以我需要服务器来检查我的证书。
const certPEM = `-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
`
const certKey = `-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----`
cert, err := tls.X509KeyPair([]byte(certPEM), []byte(certKey))
if err != nil {
t.Error("server: loadkeys: %s", err)
}
cfg := tls.Config{
InsecureSkipVerify: true,
ServerName: "somehost.com",
Certificates: []tls.Certificate{cert},
}
conn, err := tls.Dial("tcp", "somehost.com:700", &cfg)
if err != nil {
t.Error("failed to connect: " + err.Error())
}
defer conn.Close()所以这段代码适用于我的情况。
发布于 2016-01-25 14:55:10
openssl s_client只是一个连接的测试工具,但是它并不关心证书对连接是否有效。相反,Go关心是否可以验证证书,因此您可以获得证书无效的信息,因为该证书的名称不匹配。
我做错什么了?
根据错误消息,您确实以错误的主机名访问了主机。或者您的服务器配置得很糟糕,所以它发送了错误的证书。
https://stackoverflow.com/questions/34995238
复制相似问题