Skip to content

Commit 45f4275

Browse files
committed
crypto/tls: advertise correct ciphers in TLS 1.3 only mode
This change updates the makeClientHello logic to only advertise TLS 1.3 ciphers when tls.Config.MinVersion is set to tls.VersionTLS13 (i.e the client only supports TLS 1.3). Previously, TLS 1.2 ciphers would be included in the client hello message. Signed-off-by: Monis Khan <[email protected]>
1 parent 0202ad0 commit 45f4275

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

src/crypto/tls/handshake_client.go

+4
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func (c *Conn) makeClientHello() (*clientHelloMsg, *ecdh.PrivateKey, error) {
127127

128128
var key *ecdh.PrivateKey
129129
if hello.supportedVersions[0] == VersionTLS13 {
130+
// Reset the list of ciphers when the client only supports TLS 1.3.
131+
if len(hello.supportedVersions) == 1 {
132+
hello.cipherSuites = nil
133+
}
130134
if hasAESGCMHardwareSupport {
131135
hello.cipherSuites = append(hello.cipherSuites, defaultCipherSuitesTLS13...)
132136
} else {

src/crypto/tls/handshake_client_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -2595,3 +2595,66 @@ func TestClientHandshakeContextCancellation(t *testing.T) {
25952595
t.Error("Client connection was not closed when the context was canceled")
25962596
}
25972597
}
2598+
2599+
// TestTLS13OnlyClientHelloCipherSuite tests that when a client states that
2600+
// it only supports TLS 1.3, it correctly advertises only TLS 1.3 ciphers.
2601+
func TestTLS13OnlyClientHelloCipherSuite(t *testing.T) {
2602+
tls13Tests := []struct {
2603+
name string
2604+
ciphers []uint16
2605+
}{
2606+
{
2607+
name: "nil",
2608+
ciphers: nil,
2609+
},
2610+
{
2611+
name: "empty",
2612+
ciphers: []uint16{},
2613+
},
2614+
{
2615+
name: "some TLS 1.2 cipher",
2616+
ciphers: []uint16{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
2617+
},
2618+
{
2619+
name: "some TLS 1.3 cipher",
2620+
ciphers: []uint16{TLS_AES_128_GCM_SHA256},
2621+
},
2622+
{
2623+
name: "some TLS 1.2 and 1.3 ciphers",
2624+
ciphers: []uint16{TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_AES_256_GCM_SHA384},
2625+
},
2626+
}
2627+
for _, tt := range tls13Tests {
2628+
tt := tt
2629+
t.Run(tt.name, func(t *testing.T) {
2630+
t.Parallel()
2631+
testTLS13OnlyClientHelloCipherSuite(t, tt.ciphers)
2632+
})
2633+
}
2634+
}
2635+
2636+
func testTLS13OnlyClientHelloCipherSuite(t *testing.T, ciphers []uint16) {
2637+
serverConfig := &Config{
2638+
Certificates: testConfig.Certificates,
2639+
GetConfigForClient: func(chi *ClientHelloInfo) (*Config, error) {
2640+
if len(chi.CipherSuites) != len(defaultCipherSuitesTLS13NoAES) {
2641+
t.Errorf("only TLS 1.3 suites should be advertised, got=%x", chi.CipherSuites)
2642+
} else {
2643+
for i := range defaultCipherSuitesTLS13NoAES {
2644+
if want, got := defaultCipherSuitesTLS13NoAES[i], chi.CipherSuites[i]; want != got {
2645+
t.Errorf("cipher at index %d does not match, want=%x, got=%x", i, want, got)
2646+
}
2647+
}
2648+
}
2649+
return nil, nil
2650+
},
2651+
}
2652+
clientConfig := &Config{
2653+
MinVersion: VersionTLS13, // client only supports TLS 1.3
2654+
CipherSuites: ciphers,
2655+
InsecureSkipVerify: true,
2656+
}
2657+
if _, _, err := testHandshake(t, clientConfig, serverConfig); err != nil {
2658+
t.Fatalf("handshake failed: %s", err)
2659+
}
2660+
}

0 commit comments

Comments
 (0)