如何在 golang 中创建没有证书的 TLS 连接?

Net*_*ave 3 ssl go

从源头看:

// Listen creates a TLS listener accepting connections on the
// given network address using net.Listen.
// The configuration config must be non-nil and must include
// at least one certificate or else set GetCertificate.
func Listen(network, laddr string, config *Config) (net.Listener, error) {
    if config == nil || (len(config.Certificates) == 0 && config.GetCertificate == nil) {
        return nil, errors.New("tls: neither Certificates nor GetCertificate set in Config")
    }
    l, err := net.Listen(network, laddr)
    if err != nil {
        return nil, err
    }
    return NewListener(l, config), nil
}
Run Code Online (Sandbox Code Playgroud)

问题是证书不能为零:

// The configuration config must be non-nil and must include
// at least one certificate or else set GetCertificate.
Run Code Online (Sandbox Code Playgroud)

如何tls在没有证书的情况下使用连接监听?我需要的是tls 加密不是身份验证

我尝试使用空证书制作 tls.Config,如下所示:

&tls.Config{
            Certificates: []tls.Certificate{tls.Certificate{}},
}
Run Code Online (Sandbox Code Playgroud)

但连接失败tls: handshake failure。这可能吗?

Ste*_*ich 6

没有证书的 TLS 需要支持不使用证书的密码套件。

查看源代码可以在crypto/tls/cipher_suites.gocrypto/tls中找到支持的密码套件。可以看到,仅支持使用 RSA 或 ECDSA 身份验证的密码套件,这意味着您需要拥有带有 RSA 或 ECC 密钥的证书。

要支持没有证书的 TLS,需要有 PSK、SRP .. 或不需要证书的类似身份验证方法的密码或 NULL 身份验证(匿名,即无需身份验证)。但这些都不支持。

我需要的是 tls 加密而不是身份验证。

在大多数情况下,这种要求从一开始就是有缺陷的。没有身份验证的 TLS 意味着主动且无法检测的中间人攻击通常很容易发生,这基本上会使 TLS 提供的所有加密变得毫无意义。仅当客户端能够在建立 TLS 连接之后和传输任何应用程序有效负载之前安全地(即抵抗 MITM 攻击)对服务器进行身份验证时,无需身份验证的 TLS 才有意义。