From a672362b56293ec1a2d117ffb128f40d6516c4d1 Mon Sep 17 00:00:00 2001 From: awss Date: Mon, 19 Oct 2026 07:22:16 +0100 Subject: [PATCH] caddytls: skip client cert verifiers when no certificate is presented (#8251) (cherry picked from commit 872c864c200b22dc5161942816be6409eafcb295) --- modules/caddytls/connpolicy.go | 9 +- .../connpolicy_verifyconnection_test.go | 161 +++++++++++++++++- 2 files changed, 168 insertions(+), 2 deletions(-) diff --git a/modules/caddytls/connpolicy.go b/modules/caddytls/connpolicy.go index 79a708116..0dff51af8 100644 --- a/modules/caddytls/connpolicy.go +++ b/modules/caddytls/connpolicy.go @@ -918,6 +918,12 @@ func (clientauth *ClientAuthentication) verifyConnection(cs tls.ConnectionState) return err } } + + // crypto/tls has already enforced whether the mode requires a + // certificate, so the verifiers only check one that was presented + if len(rawCerts) == 0 { + return nil + } for _, verifier := range clientauth.verifiers { if err := verifier.VerifyClientCertificate(rawCerts, cs.VerifiedChains); err != nil { return err @@ -1082,7 +1088,8 @@ type LeafCertificateLoader interface { } // ClientCertificateVerifier is a type which verifies client certificates. -// It is called during verifyPeerCertificate in the TLS handshake. +// It is called during verifyConnection in the TLS handshake, only when +// the client presented a certificate. type ClientCertificateVerifier interface { VerifyClientCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error } diff --git a/modules/caddytls/connpolicy_verifyconnection_test.go b/modules/caddytls/connpolicy_verifyconnection_test.go index 8b5511a3b..8069b9377 100644 --- a/modules/caddytls/connpolicy_verifyconnection_test.go +++ b/modules/caddytls/connpolicy_verifyconnection_test.go @@ -1,20 +1,30 @@ package caddytls import ( + "crypto" + "crypto/ecdsa" + "crypto/elliptic" + "crypto/rand" "crypto/tls" "crypto/x509" + "crypto/x509/pkix" "errors" + "math/big" + "net" "reflect" "testing" + "time" ) type testClientCertificateVerifier struct { + called bool rawCerts [][]byte verifiedChains [][]*x509.Certificate err error } func (v *testClientCertificateVerifier) VerifyClientCertificate(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error { + v.called = true v.rawCerts = rawCerts v.verifiedChains = verifiedChains return v.err @@ -52,8 +62,157 @@ func TestClientAuthenticationVerifyConnectionReturnsVerifierError(t *testing.T) verifiers: []ClientCertificateVerifier{verifier}, } - err := clientauth.verifyConnection(tls.ConnectionState{}) + peerCert := &x509.Certificate{Raw: []byte("peer-cert-raw")} + err := clientauth.verifyConnection(tls.ConnectionState{ + PeerCertificates: []*x509.Certificate{peerCert}, + }) if !errors.Is(err, wantErr) { t.Fatalf("expected error %v, got %v", wantErr, err) } } + +func TestClientAuthenticationVerifyConnectionSkipsVerifiersWithoutPeerCert(t *testing.T) { + verifier := &testClientCertificateVerifier{err: errors.New("verify failed")} + clientauth := &ClientAuthentication{ + verifiers: []ClientCertificateVerifier{verifier}, + } + + if err := clientauth.verifyConnection(tls.ConnectionState{}); err != nil { + t.Fatalf("verifyConnection failed: %v", err) + } + if verifier.called { + t.Fatal("verifier was called without a peer certificate") + } +} + +func TestClientAuthenticationModeWithLeafVerifier(t *testing.T) { + ca := issueTestCert(t, nil, 0) + serverCert := issueTestCert(t, &ca, x509.ExtKeyUsageServerAuth) + trusted := issueTestCert(t, &ca, x509.ExtKeyUsageClientAuth) + untrusted := issueTestCert(t, &ca, x509.ExtKeyUsageClientAuth) + + caPool := x509.NewCertPool() + caPool.AddCert(ca.Leaf) + + for _, tc := range []struct { + mode string + cert *tls.Certificate + wantError bool + }{ + {mode: "request", cert: nil, wantError: false}, + {mode: "request", cert: &trusted, wantError: false}, + {mode: "request", cert: &untrusted, wantError: true}, + {mode: "require", cert: nil, wantError: true}, + {mode: "require", cert: &trusted, wantError: false}, + {mode: "require", cert: &untrusted, wantError: true}, + {mode: "verify_if_given", cert: nil, wantError: false}, + {mode: "verify_if_given", cert: &trusted, wantError: false}, + {mode: "verify_if_given", cert: &untrusted, wantError: true}, + {mode: "require_and_verify", cert: nil, wantError: true}, + {mode: "require_and_verify", cert: &trusted, wantError: false}, + {mode: "require_and_verify", cert: &untrusted, wantError: true}, + } { + for _, version := range []uint16{tls.VersionTLS12, tls.VersionTLS13} { + clientauth := &ClientAuthentication{ + Mode: tc.mode, + ca: InlineCAPool{pool: caPool}, + verifiers: []ClientCertificateVerifier{LeafCertClientAuth{trustedLeafCerts: []*x509.Certificate{trusted.Leaf}}}, + } + serverConfig := &tls.Config{Certificates: []tls.Certificate{serverCert}} + if err := clientauth.ConfigureTLSConfig(serverConfig); err != nil { + t.Fatalf("configuring TLS: %v", err) + } + clientConfig := &tls.Config{ + InsecureSkipVerify: true, + MinVersion: version, + MaxVersion: version, + } + if tc.cert != nil { + clientConfig.Certificates = []tls.Certificate{*tc.cert} + } + + err := serverHandshake(t, serverConfig, clientConfig) + certName := "no certificate" + if tc.cert != nil { + certName = tc.cert.Leaf.Subject.CommonName + } + if tc.wantError && err == nil { + t.Errorf("mode %s, %s, %s: expected handshake error but got none", tc.mode, tls.VersionName(version), certName) + } + if !tc.wantError && err != nil { + t.Errorf("mode %s, %s, %s: unexpected handshake error: %v", tc.mode, tls.VersionName(version), certName, err) + } + } + } +} + +// serverHandshake runs one TLS handshake over loopback TCP and returns +// the server's handshake error. +func serverHandshake(t *testing.T, serverConfig, clientConfig *tls.Config) error { + t.Helper() + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + defer ln.Close() + + errCh := make(chan error, 1) + go func() { + conn, err := ln.Accept() + if err != nil { + errCh <- err + return + } + tlsConn := tls.Server(conn, serverConfig) + errCh <- tlsConn.Handshake() + tlsConn.Close() + }() + + conn, err := tls.Dial("tcp", ln.Addr().String(), clientConfig) + if err == nil { + // wait for the server to finish before closing + _, _ = conn.Read(make([]byte, 1)) + conn.Close() + } + return <-errCh +} + +// issueTestCert returns a certificate signed by parent, or a self-signed +// CA certificate if parent is nil. +func issueTestCert(t *testing.T, parent *tls.Certificate, extKeyUsage x509.ExtKeyUsage) tls.Certificate { + t.Helper() + key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) + if err != nil { + t.Fatal(err) + } + serial, err := rand.Int(rand.Reader, big.NewInt(1<<62)) + if err != nil { + t.Fatal(err) + } + template := &x509.Certificate{ + SerialNumber: serial, + Subject: pkix.Name{CommonName: "test " + serial.String()}, + DNSNames: []string{"localhost"}, + NotBefore: time.Now().Add(-time.Hour), + NotAfter: time.Now().Add(time.Hour), + KeyUsage: x509.KeyUsageDigitalSignature, + } + signer, signerKey := template, crypto.Signer(key) + if parent == nil { + template.IsCA = true + template.BasicConstraintsValid = true + template.KeyUsage |= x509.KeyUsageCertSign + } else { + template.ExtKeyUsage = []x509.ExtKeyUsage{extKeyUsage} + signer, signerKey = parent.Leaf, parent.PrivateKey.(crypto.Signer) + } + der, err := x509.CreateCertificate(rand.Reader, template, signer, &key.PublicKey, signerKey) + if err != nil { + t.Fatal(err) + } + leaf, err := x509.ParseCertificate(der) + if err != nil { + t.Fatal(err) + } + return tls.Certificate{Certificate: [][]byte{der}, PrivateKey: key, Leaf: leaf} +}