Ssl Go TLS连接

Ssl Go TLS连接,ssl,go,tcp,Ssl,Go,Tcp,我通过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.

我通过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参数?

openssl s_client
只是一个连接的测试工具,但它并不关心证书是否对连接有效。相反,Go关心证书是否可以验证,因此您会得到证书无效的信息,因为名称不匹配

我做错了什么


根据错误消息,您确实使用错误的主机名访问了主机。或者您的服务器配置不正确,因此发送了错误的证书。

openssl s_client
只是一个连接的测试工具,但它并不关心证书是否对连接有效。相反,Go关心证书是否可以验证,因此您会得到证书无效的信息,因为名称不匹配

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()
我做错了什么


根据错误消息,您确实使用错误的主机名访问了主机。或者您的服务器配置不正确,因此它发送了错误的证书。

我不需要检查服务器的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()

所以这段代码在我的情况下是有效的。

我不需要检查服务器的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()

所以这段代码在我的情况下是有效的。

“证书对otherhost.com有效,而不是somehost.com”似乎非常清楚。服务器的名称是“otherhost.com”(或者至少是证书中的名称)。不,主机正是somehost.com,而不是otherhost.com。我知道。但我也尝试连接到otherhost.com,但此主机未知。无论DNS名称是什么,服务器使用的证书中的名称都是“otherhost.com”。“证书对otherhost.com有效,而不是somehost.com”似乎非常清楚。服务器的名称是“otherhost.com”(或者至少是证书中的名称)。不,主机正是somehost.com,而不是otherhost.com。我知道。但我也尝试连接到otherhost.com,但此主机未知。无论DNS名称是什么,服务器使用的证书中的名称都是“otherhost.com”。