ssl - Golang : tls. 连接升级后握手挂起

标签 ssl go tls1.2 starttls

我正在为我正在进行的项目编写 TCP 文本协议(protocol)。协议(protocol)中的命令之一是 STARTTLS,它应该将连接升级到 TLS 并继续。我升级连接的代码类似于 this question 中的答案。 .我遇到的问题是当我升级 TLS 连接时,tlsConn.Handshake 将挂起并且永远不会松开。下面有一些代码示例。非常感谢任何帮助。

收到STARTTLS命令后...

// Init a new TLS connection. I need a *tls.Conn type                                                                
// so that I can do the Handshake()                                                                                  
s.Logf("++> Upgrading connection to TLS")
tlsConn := tls.Server(s.Conn, s.Server.TLSConfig)
s.Logf("++> Attempting TLS Handshake")

tlsConn.Handshake()
s.Logf("++> TLS Handshake Successful")

// Here is the trick. Since I do not need to access                                                                  
// any of the TLS functions anymore,                                                                                 
// I can convert tlsConn back in to a net.Conn type                                                                  
s.Conn = net.Conn(tlsConn)

s.Logf("++> Updating read/write buffers")
s.reader = textproto.NewReader(bufio.NewReader(s.Conn))
s.writer = textproto.NewWriter(bufio.NewWriter(s.Conn))

s.Printf("100 SUCCESS")

客户端在发送 STARTTLS 命令后立即升级连接...

c.conn = tls.Client(c.conn, clientTLSConfig)

服务器 *tls.Config 看起来像这样......

// Load the key and certificate - paths are provided in flags.                                                                                           
cert, err := tls.LoadX509KeyPair(flagTLSCert, flagTLSKey)                                                                    
if err != nil {                                                                                                              
    log.Fatal(err)                                                                                                       
}

// Create the TLS config                                                                                                     
tlsConfig := &tls.Config{
    Certificates: []tls.Certificate{cert},
    ClientAuth: tls.VerifyClientCertIfGiven,
    ServerName: fqdn(),
}

客户端 *tls.Config 看起来像这样......

clientTLSConfig := &tls.Config{
    InsecureSkipVerify: true,
}

最佳答案

您是调用 c.conn.Handshake() 还是执行其他操作以在客户端发起 TLS 握手?

如果客户端没有通过发送 TLS Client Hello 来启动握手,服务器将永远等待它。

这是我最好的猜测,因为您没有提供很多客户端代码。此外,使用 tcpdump 检查将有助于缩小问题范围(到服务器或客户端)。

关于ssl - Golang : tls. 连接升级后握手挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38033511/

相关文章:

node.js - 使用 Node/Express 将 http 重定向到 https

node.js - 如何创建一个同时监听两个不同端口的 sails.js 应用程序

c# - 打开 TLS 1.0、TLS 1.1、TLS 1.2 ... Asp.NET IIS 10.0

java - 在 weblogic 10.3.6 中启用 TLS1.2 的效果

python - 如何升级openssl版本并将其链接到python 3.6.5

ssl - 购买 DNSimple SSL 证书以使其与 Heroku ssl 插件一起使用时,我应该在主机名中输入什么?

http - 在网络请求之前执行函数

Cgo:如何将双数组从 C 返回到 Go

templates - Golang索引模板包括

ruby - 如何在 Ruby 中设置 TLS 上下文选项(如 OpenSSL::SSL::SSL_OP_NO_SSLv2)