如何保证tlsConfig中VerifyPeerCertificate的顺序?

问题描述 投票:0回答:1

我有一个带有 tlsConfig 的 tls 监听:

  listener, _ := tls.Listen("tcp", ":8080", tlsConfig)

  for {
    fmt.Println("1", time.Now())
    peerMutex.Lock()
    conn, _ := listener.Accept()
    fmt.Println("2", time.Now(), lastRawCert)
    c := NewConnection(conn, lastRawCert)
    go c.HandleConnection()

  }

tlsConfig 有:

  var peerMutex sync.Mutex
  var lastRawCert string 

  tlsConfig := &tls.Config{
    VerifyPeerCertificate: func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {
      defer peerMutex.Unlock()
      fmt.Println("3", time.Now())
      if len(rawCerts) == 0 {
        lastRawCert = ""
        return fmt.Errorf("no client certificate provided")
      }
      asBytes := rawCerts[0]
      lastRawCert = util.Fingerprint(asBytes)
      return nil
    },
  }

在调用

listener.Accept()
后,回调
VerifyPeerCertificate
将触发,但我似乎无法将回调中的
lastRawCert
与 Accept 返回的
conn
相匹配。

peerMutex 不能 100% 工作。匹配这些的正确方法是什么?

go callback synchronization mutex
1个回答
0
投票

我的麻烦是我不知道:

cs := conn.(*tls.Conn).ConnectionState()
asBytes := cs.PeerCertificates[0].Raw

我可以使用它,甚至不需要回调! 致谢:https://stackoverflow.com/users/603316/peter

© www.soinside.com 2019 - 2024. All rights reserved.