为了账号安全,请及时绑定邮箱和手机立即绑定

了解 Mutual TLS,使用服务器名的客户端配置

了解 Mutual TLS,使用服务器名的客户端配置

Go
有只小跳蛙 2023-05-08 15:56:00
我试图了解双向 TLS 的工作原理,我有以下示例:我有一个客户端想要连接到服务器“svc1.example.com”但服务器有一个服务器证书的通用名称为“svc1.example.cloud”,SAN 为“svc.example.test.cloud”。现在,当我发出 GET 请求时,我得到以下信息:x509:证书对 svc.example.test.cloud 有效,对 svc1.example.com 无效。所以,我的问题是我应该对 TLS clientConfig 进行更改以包含服务器名吗?或者我应该在 TLS 客户端配置中添加自定义 verifyPeerCertificate 函数,如下所示?请让我知道 Servername 应该是什么以及我应该在 verifyPeerCertificate 函数中检查什么。func customverify(customCName func(*x509.Certificate) bool) func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error {    if customCName == nil {        return nil    }    return func(_ [][]byte, verifiedChains [][]*x509.Certificate) error {        for _, certs := range verifiedChains {            leaf := certs[0]            if customCName(leaf) {                return nil            }        }        return fmt.Errorf("client identity verification failed")    }}func configureClient(certFile, keyFile string) (*http.Client, error) {    certpool, err := addRootCA()    if err != nil {        return nil, err    }cert, err := tls.LoadX509KeyPair(certFile, keyFile)if err != nil {    return nil, err}transport := ytls.NewClientTransport()transport.TLSClientConfig.Certificates = []tls.Certificate{cert}transport.TLSClientConfig.RootCAs = certpool//transport.TLSClientConfig.ServerName = expectedCNametransport.TLSClientConfig.VerifyPeerCertificate = customverify(func(cert *x509.Certificate) bool {    return cert.Subject.CommonName == "svc1.example.cloud"})httpClient := &http.Client{Transport: transport}return httpClient, nil}
查看完整描述

1 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

由于x509: certificate 对 svc.example.test.cloud 有效,所以transport.TLSClientConfig.ServerName = "svc.example.test.cloud"


VerifyPeerCertificate(如果不是 nil)在 TLS 客户端或服务器进行正常证书验证后调用。它
接收对等方提供的原始 ASN.1 证书以及
正常处理发现的任何经过验证的链。如果它返回一个
非零错误,则握手将中止并导致该错误。

如果正常验证失败,则握手将在
考虑此回调之前中止。如果通过设置 InsecureSkipVerify 禁用正常验证
,或者(对于服务器)当 ClientAuth 是RequestClientCert 或 RequireAnyClientCert 时,将 考虑
此回调,但 verifiedChains 参数将始终为 nil。

VerifyPeerCertificate func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) 错误

因此,如果正常验证失败,则VerifyPeerCertificate不会被调用。另外,如果正常验证通过,我认为你不需要这个额外的检查VerifyPeerCertificate



查看完整回答
反对 回复 2023-05-08
  • 1 回答
  • 0 关注
  • 92 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信