diff --git a/server.go b/server.go index 9189da6069..73683bf8a2 100644 --- a/server.go +++ b/server.go @@ -1677,14 +1677,13 @@ func (s *Server) ListenAndServeTLSEmbed(addr string, certData, keyData []byte) e // the function will use previously added TLS configuration. func (s *Server) ServeTLS(ln net.Listener, certFile, keyFile string) error { s.mu.Lock() - err := s.AppendCert(certFile, keyFile) - if err != nil && err != errNoCertOrKeyProvided { - s.mu.Unlock() - return err - } - if s.TLSConfig == nil { - s.mu.Unlock() - return errNoCertOrKeyProvided + s.configTLS() + configHasCert := len(s.TLSConfig.Certificates) > 0 || s.TLSConfig.GetCertificate != nil + if !configHasCert || certFile != "" || keyFile != "" { + if err := s.AppendCert(certFile, keyFile); err != nil { + s.mu.Unlock() + return err + } } // BuildNameToCertificate has been deprecated since 1.14. @@ -1706,15 +1705,13 @@ func (s *Server) ServeTLS(ln net.Listener, certFile, keyFile string) error { // the function will use previously added TLS configuration. func (s *Server) ServeTLSEmbed(ln net.Listener, certData, keyData []byte) error { s.mu.Lock() - - err := s.AppendCertEmbed(certData, keyData) - if err != nil && err != errNoCertOrKeyProvided { - s.mu.Unlock() - return err - } - if s.TLSConfig == nil { - s.mu.Unlock() - return errNoCertOrKeyProvided + s.configTLS() + configHasCert := len(s.TLSConfig.Certificates) > 0 || s.TLSConfig.GetCertificate != nil + if !configHasCert || len(certData) != 0 || len(keyData) != 0 { + if err := s.AppendCertEmbed(certData, keyData); err != nil { + s.mu.Unlock() + return err + } } // BuildNameToCertificate has been deprecated since 1.14.