Skip to content

Commit 6a208ef

Browse files
committed
net/http: make ListenAndServeTLS treat GetCertificate as a set cert too
ListenAndServeTLS doesn't require cert and key file names if the server's TLSConfig has a cert configured. This code was never updated when the GetCertificate hook was added to *tls.Config, however. Fixes #14268 Change-Id: Ib282ebb05697edd37ed8ff105972cbd1176d900b Reviewed-on: https://go-review.googlesource.com/19381 Reviewed-by: Russ Cox <rsc@golang.org>
1 parent 41191e1 commit 6a208ef

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

src/net/http/serve_test.go

+22-6
Original file line numberDiff line numberDiff line change
@@ -1039,12 +1039,30 @@ func TestAutomaticHTTP2_Serve(t *testing.T) {
10391039
}
10401040

10411041
func TestAutomaticHTTP2_ListenAndServe(t *testing.T) {
1042-
defer afterTest(t)
1043-
defer SetTestHookServerServe(nil)
10441042
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
10451043
if err != nil {
10461044
t.Fatal(err)
10471045
}
1046+
testAutomaticHTTP2_ListenAndServe(t, &tls.Config{
1047+
Certificates: []tls.Certificate{cert},
1048+
})
1049+
}
1050+
1051+
func TestAutomaticHTTP2_ListenAndServe_GetCertificate(t *testing.T) {
1052+
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
1053+
if err != nil {
1054+
t.Fatal(err)
1055+
}
1056+
testAutomaticHTTP2_ListenAndServe(t, &tls.Config{
1057+
GetCertificate: func(clientHello *tls.ClientHelloInfo) (*tls.Certificate, error) {
1058+
return &cert, nil
1059+
},
1060+
})
1061+
}
1062+
1063+
func testAutomaticHTTP2_ListenAndServe(t *testing.T, tlsConf *tls.Config) {
1064+
defer afterTest(t)
1065+
defer SetTestHookServerServe(nil)
10481066
var ok bool
10491067
var s *Server
10501068
const maxTries = 5
@@ -1060,10 +1078,8 @@ Try:
10601078
lnc <- ln
10611079
})
10621080
s = &Server{
1063-
Addr: addr,
1064-
TLSConfig: &tls.Config{
1065-
Certificates: []tls.Certificate{cert},
1066-
},
1081+
Addr: addr,
1082+
TLSConfig: tlsConf,
10671083
}
10681084
errc := make(chan error, 1)
10691085
go func() { errc <- s.ListenAndServeTLS("", "") }()

src/net/http/server.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -2233,10 +2233,11 @@ func ListenAndServeTLS(addr, certFile, keyFile string, handler Handler) error {
22332233
// Accepted connections are configured to enable TCP keep-alives.
22342234
//
22352235
// Filenames containing a certificate and matching private key for the
2236-
// server must be provided if the Server's TLSConfig.Certificates is
2237-
// not populated. If the certificate is signed by a certificate
2238-
// authority, the certFile should be the concatenation of the server's
2239-
// certificate, any intermediates, and the CA's certificate.
2236+
// server must be provided if neither the Server's TLSConfig.Certificates
2237+
// nor TLSConfig.GetCertificate are populated. If the certificate is
2238+
// signed by a certificate authority, the certFile should be the
2239+
// concatenation of the server's certificate, any intermediates, and
2240+
// the CA's certificate.
22402241
//
22412242
// If srv.Addr is blank, ":https" is used.
22422243
//
@@ -2258,7 +2259,8 @@ func (srv *Server) ListenAndServeTLS(certFile, keyFile string) error {
22582259
config.NextProtos = append(config.NextProtos, "http/1.1")
22592260
}
22602261

2261-
if len(config.Certificates) == 0 || certFile != "" || keyFile != "" {
2262+
configHasCert := len(config.Certificates) > 0 || config.GetCertificate != nil
2263+
if !configHasCert || certFile != "" || keyFile != "" {
22622264
var err error
22632265
config.Certificates = make([]tls.Certificate, 1)
22642266
config.Certificates[0], err = tls.LoadX509KeyPair(certFile, keyFile)

0 commit comments

Comments
 (0)