diff --git a/src/crypto/tls/tls.go b/src/crypto/tls/tls.go index b30f0b8fe4f..f3089f0ed68 100644 --- a/src/crypto/tls/tls.go +++ b/src/crypto/tls/tls.go @@ -87,6 +87,7 @@ func NewListener(inner net.Listener, config *Config) net.Listener { // The configuration config must be non-nil and must include // at least one certificate or else set GetCertificate. func Listen(network, laddr string, config *Config) (net.Listener, error) { + // If this condition changes, consider updating http.Server.ServeTLS too. if config == nil || len(config.Certificates) == 0 && config.GetCertificate == nil && config.GetConfigForClient == nil { return nil, errors.New("tls: neither Certificates, GetCertificate, nor GetConfigForClient set in Config") diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go index 34b7d57f401..06bf5089d8f 100644 --- a/src/net/http/serve_test.go +++ b/src/net/http/serve_test.go @@ -1748,6 +1748,24 @@ func TestAutomaticHTTP2_ListenAndServe_GetCertificate(t *testing.T) { }) } +func TestAutomaticHTTP2_ListenAndServe_GetConfigForClient(t *testing.T) { + cert, err := tls.X509KeyPair(testcert.LocalhostCert, testcert.LocalhostKey) + if err != nil { + t.Fatal(err) + } + conf := &tls.Config{ + // GetConfigForClient requires specifying a full tls.Config so we must set + // NextProtos ourselves. + NextProtos: []string{"h2"}, + Certificates: []tls.Certificate{cert}, + } + testAutomaticHTTP2_ListenAndServe(t, &tls.Config{ + GetConfigForClient: func(clientHello *tls.ClientHelloInfo) (*tls.Config, error) { + return conf, nil + }, + }) +} + func testAutomaticHTTP2_ListenAndServe(t *testing.T, tlsConf *tls.Config) { CondSkipHTTP2(t) // Not parallel: uses global test hooks. diff --git a/src/net/http/server.go b/src/net/http/server.go index b9a6edd7ad2..190f5650137 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -3370,7 +3370,8 @@ func (srv *Server) Serve(l net.Listener) error { // // Files containing a certificate and matching private key for the // server must be provided if neither the [Server]'s -// TLSConfig.Certificates nor TLSConfig.GetCertificate are populated. +// TLSConfig.Certificates, TLSConfig.GetCertificate nor +// config.GetConfigForClient are populated. // If the certificate is signed by a certificate authority, the // certFile should be the concatenation of the server's certificate, // any intermediates, and the CA's certificate. @@ -3389,7 +3390,7 @@ func (srv *Server) ServeTLS(l net.Listener, certFile, keyFile string) error { config.NextProtos = append(config.NextProtos, "http/1.1") } - configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil + configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil || config.GetConfigForClient != nil if !configHasCert || certFile != "" || keyFile != "" { var err error config.Certificates = make([]tls.Certificate, 1)