Skip to content

Commit 56238d8

Browse files
shibuya-rclaude
andauthored
Always send the configured client certificate to HTTP stores (#444)
* Always send the configured client certificate to HTTP stores Go's default client certificate selection sends no certificate when the server's CertificateRequest doesn't list the issuing CA. Set GetClientCertificate so the configured certificate is always presented, as curl and OpenSSL-based clients do. Fixes #443 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Check the response body close error in the TLS test golangci-lint's errcheck only exempts deferred Close calls. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5ad88e9 commit 56238d8

2 files changed

Lines changed: 106 additions & 0 deletions

File tree

‎store.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ func (o StoreOptions) tlsClientConfig() (*tls.Config, error) {
206206
return nil, fmt.Errorf("failed to load client certificate from %s: %w", o.ClientCert, err)
207207
}
208208
tlsConfig.Certificates = []tls.Certificate{certificate}
209+
// Always present the configured certificate, regardless of the CAs the
210+
// server lists in its CertificateRequest. Go's default selection sends
211+
// no certificate when the list doesn't include the issuer, which breaks
212+
// TLS terminators that advertise a fixed set of public CAs and forward
213+
// the client certificate to the backend for verification.
214+
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
215+
return &certificate, nil
216+
}
209217
}
210218

211219
// Load custom CA set if provided

‎store_tls_test.go‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package desync
2+
3+
import (
4+
"crypto/ecdsa"
5+
"crypto/elliptic"
6+
"crypto/rand"
7+
"crypto/tls"
8+
"crypto/x509"
9+
"crypto/x509/pkix"
10+
"encoding/pem"
11+
"math/big"
12+
"net/http"
13+
"net/http/httptest"
14+
"os"
15+
"path/filepath"
16+
"testing"
17+
"time"
18+
19+
"github.com/stretchr/testify/require"
20+
)
21+
22+
// newTestCA returns a self-signed CA certificate and its key.
23+
func newTestCA(t *testing.T, cn string) (*x509.Certificate, *ecdsa.PrivateKey) {
24+
t.Helper()
25+
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
26+
require.NoError(t, err)
27+
tmpl := &x509.Certificate{
28+
SerialNumber: big.NewInt(1),
29+
Subject: pkix.Name{CommonName: cn},
30+
NotBefore: time.Now().Add(-time.Hour),
31+
NotAfter: time.Now().Add(time.Hour),
32+
IsCA: true,
33+
BasicConstraintsValid: true,
34+
KeyUsage: x509.KeyUsageCertSign,
35+
}
36+
der, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &key.PublicKey, key)
37+
require.NoError(t, err)
38+
cert, err := x509.ParseCertificate(der)
39+
require.NoError(t, err)
40+
return cert, key
41+
}
42+
43+
// TestTLSClientCertSentRegardlessOfAcceptableCAs checks that the configured
44+
// client certificate is sent even when the server's CertificateRequest lists
45+
// CAs that did not issue it. Some TLS terminators (for example managed
46+
// ingresses that forward the client certificate to the backend for
47+
// verification) advertise a fixed list of public CAs, and Go's default
48+
// selection would then send no certificate at all.
49+
func TestTLSClientCertSentRegardlessOfAcceptableCAs(t *testing.T) {
50+
clientCA, clientCAKey := newTestCA(t, "private client CA")
51+
unrelatedCA, _ := newTestCA(t, "unrelated public CA")
52+
53+
// Client certificate issued by the private CA.
54+
clientKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
55+
require.NoError(t, err)
56+
clientTmpl := &x509.Certificate{
57+
SerialNumber: big.NewInt(2),
58+
Subject: pkix.Name{CommonName: "client"},
59+
NotBefore: time.Now().Add(-time.Hour),
60+
NotAfter: time.Now().Add(time.Hour),
61+
KeyUsage: x509.KeyUsageDigitalSignature,
62+
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
63+
}
64+
clientDER, err := x509.CreateCertificate(rand.Reader, clientTmpl, clientCA, &clientKey.PublicKey, clientCAKey)
65+
require.NoError(t, err)
66+
67+
dir := t.TempDir()
68+
certFile := filepath.Join(dir, "client.crt")
69+
keyFile := filepath.Join(dir, "client.key")
70+
keyDER, err := x509.MarshalECPrivateKey(clientKey)
71+
require.NoError(t, err)
72+
require.NoError(t, os.WriteFile(certFile, pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: clientDER}), 0o600))
73+
require.NoError(t, os.WriteFile(keyFile, pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: keyDER}), 0o600))
74+
75+
// The server advertises only the unrelated CA in its CertificateRequest
76+
// and requires a certificate without verifying it, like an ingress that
77+
// leaves verification to the backend.
78+
advertised := x509.NewCertPool()
79+
advertised.AddCert(unrelatedCA)
80+
var gotCN string
81+
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
82+
if len(r.TLS.PeerCertificates) > 0 {
83+
gotCN = r.TLS.PeerCertificates[0].Subject.CommonName
84+
}
85+
}))
86+
srv.TLS = &tls.Config{ClientAuth: tls.RequireAnyClientCert, ClientCAs: advertised}
87+
srv.StartTLS()
88+
defer srv.Close()
89+
90+
opt := StoreOptions{ClientCert: certFile, ClientKey: keyFile, TrustInsecure: true}
91+
tlsConfig, err := opt.tlsClientConfig()
92+
require.NoError(t, err)
93+
client := &http.Client{Transport: &http.Transport{TLSClientConfig: tlsConfig}}
94+
resp, err := client.Get(srv.URL)
95+
require.NoError(t, err, "client certificate was likely not sent")
96+
require.NoError(t, resp.Body.Close())
97+
require.Equal(t, "client", gotCN)
98+
}

0 commit comments

Comments
 (0)
⌂ 换网址