diff --git a/common/tls/std_client.go b/common/tls/std_client.go index dd26105a..64a6e302 100644 --- a/common/tls/std_client.go +++ b/common/tls/std_client.go @@ -276,6 +276,9 @@ func verifyConnection(rootCAs *x509.CertPool, timeFunc func() time.Time, serverN } func VerifyPublicKeySHA256(knownHashValues [][]byte, rawCerts [][]byte) error { + if len(rawCerts) == 0 { + return E.New("missing remote certificate") + } leafCertificate, err := x509.ParseCertificate(rawCerts[0]) if err != nil { return E.Cause(err, "failed to parse leaf certificate") diff --git a/common/tls/std_server.go b/common/tls/std_server.go index 171cc4f2..6e51ec4b 100644 --- a/common/tls/std_server.go +++ b/common/tls/std_server.go @@ -473,6 +473,7 @@ func NewSTDServer(ctx context.Context, logger log.ContextLogger, options option. } tlsConfig.ClientCAs = clientCertificateCA } else if len(options.ClientCertificatePublicKeySHA256) > 0 { + verifyIfGiven := tlsConfig.ClientAuth == tls.VerifyClientCertIfGiven switch tlsConfig.ClientAuth { case tls.RequireAndVerifyClientCert: tlsConfig.ClientAuth = tls.RequireAnyClientCert @@ -480,6 +481,9 @@ func NewSTDServer(ctx context.Context, logger log.ContextLogger, options option. tlsConfig.ClientAuth = tls.RequestClientCert } tlsConfig.VerifyPeerCertificate = func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error { + if verifyIfGiven && len(rawCerts) == 0 { + return nil + } return VerifyPublicKeySHA256(options.ClientCertificatePublicKeySHA256, rawCerts) } } else {