3 回答
TA贡献1898条经验 获得超8个赞
必须调用crypto/tls/Conn.Handshake。然后你可以读取对等证书:tlsconn.ConnectionState().PeerCertificates[0].Subject.CommonName
TA贡献1804条经验 获得超8个赞
使用 crypto/tls 时,您可以查询 ConnectionState 的任何 Conn 对象:
func (c *Conn) ConnectionState() ConnectionState
ConnectionState 结构包含有关客户端证书的信息:
type ConnectionState struct {
PeerCertificates []*x509.Certificate // certificate chain presented by remote peer
}
该x509.Certificate应该相当简单的与工作。
在服务器请求客户端身份验证之前,您必须使用服务器证书、客户端 CA(否则您必须手动验证信任链,您真的不想要)和 tls.RequireAndVerifyClientCert 配置连接。例如:
// Load my SSL key and certificate
cert, err := tls.LoadX509KeyPair(settings.MyCertificateFile, settings.MyKeyFile)
checkError(err, "LoadX509KeyPair")
// Load the CA certificate for client certificate validation
capool := x509.NewCertPool()
cacert, err := ioutil.ReadFile(settings.CAKeyFile)
checkError(err, "loadCACert")
capool.AppendCertsFromPEM(cacert)
// Prepare server configuration
config := tls.Config{Certificates: []tls.Certificate{cert}, ClientCAs: capool, ClientAuth: tls.RequireAndVerifyClientCert}
config.NextProtos = []string{"http/1.1"}
config.Rand = rand.Reader
- 3 回答
- 0 关注
- 437 浏览
添加回答
举报