|
| 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