1 // Copyright 2023 The etcd Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package tlsutil 16 17 import ( 18 "crypto/tls" 19 "fmt" 20 ) 21 22 type TLSVersion string 23 24 // Constants for TLS versions. 25 const ( 26 TLSVersionDefault TLSVersion = "" 27 TLSVersion12 TLSVersion = "TLS1.2" 28 TLSVersion13 TLSVersion = "TLS1.3" 29 ) 30 31 // GetTLSVersion returns the corresponding tls.Version or error. 32 func GetTLSVersion(version string) (uint16, error) { 33 var v uint16 34 35 switch version { 36 case string(TLSVersionDefault): 37 v = 0 // 0 means let Go decide. 38 case string(TLSVersion12): 39 v = tls.VersionTLS12 40 case string(TLSVersion13): 41 v = tls.VersionTLS13 42 default: 43 return 0, fmt.Errorf("unexpected TLS version %q (must be one of: TLS1.2, TLS1.3)", version) 44 } 45 46 return v, nil 47 } 48