@@ -30,10 +30,20 @@ type TLSSetting struct {
3030 // For a server this verifies client certificates. If empty uses system root CA.
3131 // (optional)
3232 CAFile string `mapstructure:"ca_file"`
33+
3334 // Path to the TLS cert to use for TLS required connections. (optional)
3435 CertFile string `mapstructure:"cert_file"`
36+
3537 // Path to the TLS key to use for TLS required connections. (optional)
3638 KeyFile string `mapstructure:"key_file"`
39+
40+ // MinVersion sets the minimum TLS version that is acceptable.
41+ // If not set, TLS 1.0 is used. (optional)
42+ MinVersion string `mapstructure:"min_version"`
43+
44+ // MaxVersion sets the maximum TLS version that is acceptable.
45+ // If not set, TLS 1.3 is used. (optional)
46+ MaxVersion string `mapstructure:"max_version"`
3747}
3848
3949// TLSClientSetting contains TLS configurations that are specific to client
@@ -96,16 +106,28 @@ func (c TLSSetting) loadTLSConfig() (*tls.Config, error) {
96106
97107 var certificates []tls.Certificate
98108 if c .CertFile != "" && c .KeyFile != "" {
99- tlsCert , err := tls .LoadX509KeyPair (filepath .Clean (c .CertFile ), filepath .Clean (c .KeyFile ))
109+ var tlsCert tls.Certificate
110+ tlsCert , err = tls .LoadX509KeyPair (filepath .Clean (c .CertFile ), filepath .Clean (c .KeyFile ))
100111 if err != nil {
101112 return nil , fmt .Errorf ("failed to load TLS cert and key: %w" , err )
102113 }
103114 certificates = append (certificates , tlsCert )
104115 }
105116
117+ minTLS , err := convertVersion (c .MinVersion )
118+ if err != nil {
119+ return nil , fmt .Errorf ("invalid TLS min_version: %w" , err )
120+ }
121+ maxTLS , err := convertVersion (c .MaxVersion )
122+ if err != nil {
123+ return nil , fmt .Errorf ("invalid TLS max_version: %w" , err )
124+ }
125+
106126 return & tls.Config {
107127 RootCAs : certPool ,
108128 Certificates : certificates ,
129+ MinVersion : minTLS ,
130+ MaxVersion : maxTLS ,
109131 }, nil
110132}
111133
@@ -153,3 +175,21 @@ func (c TLSServerSetting) LoadTLSConfig() (*tls.Config, error) {
153175 }
154176 return tlsCfg , nil
155177}
178+
179+ func convertVersion (v string ) (uint16 , error ) {
180+ if v == "" {
181+ return 0 , nil // default
182+ }
183+ val , ok := tlsVersions [v ]
184+ if ! ok {
185+ return 0 , fmt .Errorf ("unsupported TLS version: %q" , v )
186+ }
187+ return val , nil
188+ }
189+
190+ var tlsVersions = map [string ]uint16 {
191+ "1.0" : tls .VersionTLS10 ,
192+ "1.1" : tls .VersionTLS11 ,
193+ "1.2" : tls .VersionTLS12 ,
194+ "1.3" : tls .VersionTLS13 ,
195+ }
0 commit comments