doubaoxue5788 2016-06-25 22:29
浏览 139

Golang:tls。连接升级后握手挂起

I am writing a TCP text protocol for a project that I am working on. One of the commands in the protocol is STARTTLS which should upgrade the connection to TLS and continue on. My code to upgrade the connection is similar to the answer in this question. The problem I am having is when I am upgrading the TLS connection, the tlsConn.Handshake will hang and never let go. There are some code samples below. Any help is greatly appreciated.

After the STARTTLS command is received...

// 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")

The client is currently upgrading the connection right after it sends the STARTTLS command like this...

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

The server *tls.Config looks like this...

// 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(),
}

The client *tls.Config looks like this...

clientTLSConfig := &tls.Config{
    InsecureSkipVerify: true,
}
  • 写回答

1条回答 默认 最新

  • dtyqflrr775518 2016-06-27 22:13
    关注

    Do you call c.conn.Handshake() or do something else to initiate the TLS handshake on the client side?

    If the client does not initiate the handshake by sending TLS Client Hello, the server will sit forever waiting for it.

    This is my best guess as you did not provide much of the client side code. Also checking with tcpdump would help to narrow the problem down (to server or client side).

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度