From f41c2e62ca449e00d572817ce4cd38726ee2cfa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 24 Nov 2026 14:23:57 +0800 Subject: [PATCH] Fix public key verification with empty certificate chain --- common/tls/std_client.go | 3 +++ common/tls/std_server.go | 4 ++++ 2 files changed, 7 insertions(+) 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 {