使用 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