|
| 1 | +package tlsprofiles |
| 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" |
| 13 | + "testing" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +// generateSelfSignedCert generates a self-signed ECDSA P-256 certificate for use in tests. |
| 20 | +func generateSelfSignedCert(t *testing.T) tls.Certificate { |
| 21 | + t.Helper() |
| 22 | + |
| 23 | + priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 24 | + require.NoError(t, err) |
| 25 | + |
| 26 | + template := x509.Certificate{ |
| 27 | + SerialNumber: big.NewInt(1), |
| 28 | + Subject: pkix.Name{Organization: []string{"Test"}}, |
| 29 | + NotBefore: time.Now().Add(-time.Hour), |
| 30 | + NotAfter: time.Now().Add(time.Hour), |
| 31 | + KeyUsage: x509.KeyUsageDigitalSignature, |
| 32 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 33 | + IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}, |
| 34 | + } |
| 35 | + |
| 36 | + certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv) |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER}) |
| 40 | + privDER, err := x509.MarshalECPrivateKey(priv) |
| 41 | + require.NoError(t, err) |
| 42 | + keyPEM := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: privDER}) |
| 43 | + |
| 44 | + cert, err := tls.X509KeyPair(certPEM, keyPEM) |
| 45 | + require.NoError(t, err) |
| 46 | + return cert |
| 47 | +} |
| 48 | + |
| 49 | +// startTLSServer starts a TLS listener with cfgFn applied and serves connections in |
| 50 | +// the background. The listener is closed when the test completes. |
| 51 | +func startTLSServer(t *testing.T, cfgFn func(*tls.Config)) string { |
| 52 | + t.Helper() |
| 53 | + |
| 54 | + serverCfg := &tls.Config{ |
| 55 | + Certificates: []tls.Certificate{generateSelfSignedCert(t)}, |
| 56 | + MinVersion: tls.VersionTLS12, // baseline; cfgFn will raise this if the profile requires it |
| 57 | + } |
| 58 | + cfgFn(serverCfg) |
| 59 | + |
| 60 | + ln, err := tls.Listen("tcp", "127.0.0.1:0", serverCfg) |
| 61 | + require.NoError(t, err) |
| 62 | + t.Cleanup(func() { ln.Close() }) |
| 63 | + |
| 64 | + go func() { |
| 65 | + for { |
| 66 | + conn, err := ln.Accept() |
| 67 | + if err != nil { |
| 68 | + return // listener closed |
| 69 | + } |
| 70 | + go func() { |
| 71 | + defer conn.Close() |
| 72 | + _ = conn.(*tls.Conn).Handshake() |
| 73 | + }() |
| 74 | + } |
| 75 | + }() |
| 76 | + |
| 77 | + return ln.Addr().String() |
| 78 | +} |
| 79 | + |
| 80 | +// dialTLS connects to addr with the given config and returns the negotiated |
| 81 | +// ConnectionState. The caller must check err before using the state. |
| 82 | +func dialTLS(addr string, clientCfg *tls.Config) (tls.ConnectionState, error) { |
| 83 | + conn, err := tls.Dial("tcp", addr, clientCfg) |
| 84 | + if err != nil { |
| 85 | + return tls.ConnectionState{}, err |
| 86 | + } |
| 87 | + defer conn.Close() |
| 88 | + return conn.ConnectionState(), nil |
| 89 | +} |
| 90 | + |
| 91 | +// setCustomProfile configures the package-level custom TLS profile for the duration |
| 92 | +// of the test and restores the original state via t.Cleanup. |
| 93 | +func setCustomProfile(t *testing.T, cipherNames []string, curveNames []string, minVersion string) { |
| 94 | + t.Helper() |
| 95 | + |
| 96 | + origProfile := configuredProfile |
| 97 | + origCustom := customTLSProfile |
| 98 | + t.Cleanup(func() { |
| 99 | + configuredProfile = origProfile |
| 100 | + customTLSProfile = origCustom |
| 101 | + }) |
| 102 | + |
| 103 | + configuredProfile = "custom" |
| 104 | + customTLSProfile = tlsProfile{ |
| 105 | + ciphers: cipherSlice{}, |
| 106 | + curves: curveSlice{}, |
| 107 | + } |
| 108 | + |
| 109 | + for _, name := range cipherNames { |
| 110 | + require.NoError(t, customTLSProfile.ciphers.Append(name)) |
| 111 | + } |
| 112 | + for _, name := range curveNames { |
| 113 | + require.NoError(t, customTLSProfile.curves.Append(name)) |
| 114 | + } |
| 115 | + if minVersion != "" { |
| 116 | + require.NoError(t, customTLSProfile.minTLSVersion.Set(minVersion)) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +// TestCustomTLSProfileCipherNegotiation verifies that when a custom profile |
| 121 | +// specifies a single cipher suite, that cipher is actually negotiated. |
| 122 | +func TestCustomTLSProfileCipherNegotiation(t *testing.T) { |
| 123 | + const cipher = "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" |
| 124 | + cipherID := cipherSuiteId(cipher) |
| 125 | + require.NotZero(t, cipherID) |
| 126 | + |
| 127 | + setCustomProfile(t, []string{cipher}, []string{"prime256v1"}, "TLSv1.2") |
| 128 | + |
| 129 | + cfgFn, err := GetTLSConfigFunc() |
| 130 | + require.NoError(t, err) |
| 131 | + |
| 132 | + addr := startTLSServer(t, cfgFn) |
| 133 | + |
| 134 | + // Client is restricted to TLS 1.2 with the same single cipher. |
| 135 | + clientCfg := &tls.Config{ |
| 136 | + InsecureSkipVerify: true, //nolint:gosec // self-signed cert used only in tests |
| 137 | + MaxVersion: tls.VersionTLS12, |
| 138 | + CipherSuites: []uint16{cipherID}, |
| 139 | + } |
| 140 | + |
| 141 | + state, err := dialTLS(addr, clientCfg) |
| 142 | + require.NoError(t, err) |
| 143 | + require.Equal(t, cipherID, state.CipherSuite, "expected cipher %s to be negotiated", cipher) |
| 144 | +} |
| 145 | + |
| 146 | +// TestCustomTLSProfileCipherRejection verifies that the server rejects a |
| 147 | +// connection when the client offers only a cipher not in the custom profile. |
| 148 | +func TestCustomTLSProfileCipherRejection(t *testing.T) { |
| 149 | + // Server is configured with AES-256 only. |
| 150 | + setCustomProfile(t, |
| 151 | + []string{"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384"}, |
| 152 | + []string{"prime256v1"}, |
| 153 | + "TLSv1.2", |
| 154 | + ) |
| 155 | + |
| 156 | + cfgFn, err := GetTLSConfigFunc() |
| 157 | + require.NoError(t, err) |
| 158 | + |
| 159 | + addr := startTLSServer(t, cfgFn) |
| 160 | + |
| 161 | + // Client offers only AES-128, which the server does not allow. |
| 162 | + cipherID := cipherSuiteId("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") |
| 163 | + require.NotZero(t, cipherID, "cipher suite must be available on this platform") |
| 164 | + clientCfg := &tls.Config{ |
| 165 | + InsecureSkipVerify: true, //nolint:gosec // self-signed cert used only in tests |
| 166 | + MaxVersion: tls.VersionTLS12, |
| 167 | + CipherSuites: []uint16{cipherID}, |
| 168 | + } |
| 169 | + |
| 170 | + _, err = dialTLS(addr, clientCfg) |
| 171 | + require.Error(t, err, "connection should fail when client offers no cipher from the custom profile") |
| 172 | +} |
| 173 | + |
| 174 | +// TestCustomTLSProfileMinVersionEnforcement verifies that a custom profile |
| 175 | +// configured with a TLS 1.3 minimum rejects TLS 1.2-only clients. |
| 176 | +func TestCustomTLSProfileMinVersionEnforcement(t *testing.T) { |
| 177 | + setCustomProfile(t, |
| 178 | + []string{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"}, |
| 179 | + []string{"prime256v1"}, |
| 180 | + "TLSv1.3", |
| 181 | + ) |
| 182 | + |
| 183 | + cfgFn, err := GetTLSConfigFunc() |
| 184 | + require.NoError(t, err) |
| 185 | + |
| 186 | + addr := startTLSServer(t, cfgFn) |
| 187 | + |
| 188 | + // Client advertises TLS 1.2 as its maximum; server requires TLS 1.3. |
| 189 | + clientCfg := &tls.Config{ |
| 190 | + InsecureSkipVerify: true, //nolint:gosec // self-signed cert used only in tests |
| 191 | + MaxVersion: tls.VersionTLS12, |
| 192 | + } |
| 193 | + |
| 194 | + _, err = dialTLS(addr, clientCfg) |
| 195 | + require.Error(t, err, "connection should fail when server requires TLS 1.3 and client only supports TLS 1.2") |
| 196 | +} |
| 197 | + |
| 198 | +// TestCustomTLSProfileCurveNegotiation verifies that a connection succeeds when |
| 199 | +// the client's curve preferences overlap with the custom profile's curve list. |
| 200 | +func TestCustomTLSProfileCurveNegotiation(t *testing.T) { |
| 201 | + // Server allows only prime256v1 (P-256). |
| 202 | + setCustomProfile(t, |
| 203 | + []string{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"}, |
| 204 | + []string{"prime256v1"}, |
| 205 | + "TLSv1.2", |
| 206 | + ) |
| 207 | + |
| 208 | + cfgFn, err := GetTLSConfigFunc() |
| 209 | + require.NoError(t, err) |
| 210 | + |
| 211 | + addr := startTLSServer(t, cfgFn) |
| 212 | + |
| 213 | + // Client also only uses prime256v1 — there is an overlap. |
| 214 | + cipherID := cipherSuiteId("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") |
| 215 | + require.NotZero(t, cipherID, "cipher suite must be available on this platform") |
| 216 | + clientCfg := &tls.Config{ |
| 217 | + InsecureSkipVerify: true, //nolint:gosec // self-signed cert used only in tests |
| 218 | + MaxVersion: tls.VersionTLS12, |
| 219 | + CurvePreferences: []tls.CurveID{tls.CurveP256}, |
| 220 | + CipherSuites: []uint16{cipherID}, |
| 221 | + } |
| 222 | + |
| 223 | + _, err = dialTLS(addr, clientCfg) |
| 224 | + require.NoError(t, err) |
| 225 | +} |
| 226 | + |
| 227 | +// TestCustomTLSProfileCurveRejection verifies that a connection fails when the |
| 228 | +// client's supported curves do not overlap with the custom profile's curve list. |
| 229 | +// TLS 1.2 is used because the curve negotiation failure is deterministic there; |
| 230 | +// TLS 1.3 can fall back via HelloRetryRequest. |
| 231 | +func TestCustomTLSProfileCurveRejection(t *testing.T) { |
| 232 | + // Server allows only prime256v1 (P-256). |
| 233 | + setCustomProfile(t, |
| 234 | + []string{"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"}, |
| 235 | + []string{"prime256v1"}, |
| 236 | + "TLSv1.2", |
| 237 | + ) |
| 238 | + |
| 239 | + cfgFn, err := GetTLSConfigFunc() |
| 240 | + require.NoError(t, err) |
| 241 | + |
| 242 | + addr := startTLSServer(t, cfgFn) |
| 243 | + |
| 244 | + // Client only supports X25519, which is not in the server's curve list. |
| 245 | + cipherID := cipherSuiteId("TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256") |
| 246 | + require.NotZero(t, cipherID, "cipher suite must be available on this platform") |
| 247 | + clientCfg := &tls.Config{ |
| 248 | + InsecureSkipVerify: true, //nolint:gosec // self-signed cert used only in tests |
| 249 | + MaxVersion: tls.VersionTLS12, |
| 250 | + CurvePreferences: []tls.CurveID{tls.X25519}, |
| 251 | + CipherSuites: []uint16{cipherID}, |
| 252 | + } |
| 253 | + |
| 254 | + _, err = dialTLS(addr, clientCfg) |
| 255 | + require.Error(t, err, "connection should fail when client and server share no common curve") |
| 256 | +} |
0 commit comments