Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ssl 了解相互TLS、客户端配置和服务器名_Ssl_Go_Tls1.2 - Fatal编程技术网

Ssl 了解相互TLS、客户端配置和服务器名

Ssl 了解相互TLS、客户端配置和服务器名,ssl,go,tls1.2,Ssl,Go,Tls1.2,我试图了解双方TLS的工作原理,我有以下示例: 我有一个客户端想要连接到服务器“svc1.example.com” 但是服务器有一个 公用名为“svc1.example.cloud”和SAN的服务器证书 如“svc.example.test.cloud” 现在,当我发出GET请求时,我得到以下信息: x509:证书对svc.example.test.cloud有效,而不是svc1.example.com。 func customverify(customCName func(*x509.Cert

我试图了解双方TLS的工作原理,我有以下示例:

我有一个客户端想要连接到服务器“svc1.example.com”

但是服务器有一个

公用名为“svc1.example.cloud”和SAN的服务器证书 如“svc.example.test.cloud”

现在,当我发出GET请求时,我得到以下信息:

x509:证书对svc.example.test.cloud有效,而不是svc1.example.com。

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 = expectedCName
transport.TLSClientConfig.VerifyPeerCertificate = customverify(func(cert *x509.Certificate) bool {
    return cert.Subject.CommonName == "svc1.example.cloud"
})

httpClient := &http.Client{Transport: transport}
return httpClient, nil
所以,我的问题是,我是否应该对TLS clientConfig更改进行修改,以包括服务器名?或者我应该在TLS客户端配置中添加一个自定义verifyPeerCertificate函数,如下所示

请告诉我,服务器名应该是什么,我应该在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 = expectedCName
transport.TLSClientConfig.VerifyPeerCertificate = customverify(func(cert *x509.Certificate) bool {
    return cert.Subject.CommonName == "svc1.example.cloud"
})

httpClient := &http.Client{Transport: transport}
return httpClient, nil

}由于x509:证书对svc.example.test.cloud有效,因此
transport.TLSClientConfig.ServerName=“svc.example.test.cloud”

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

如果正常验证失败,则握手将在
考虑到这次回调。如果
设置unsecureskipverify,或(对于服务器)当ClientAuth为
RequestClientCert或RequireOnClientCert,则此回调将
将被考虑,但verifiedChains参数将始终为零

VerifyPeerCertificate func(rawCerts[][]字节,verifiedChains[][]*x509.证书)错误

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