...

Source file src/go.etcd.io/etcd/client/pkg/v3/tlsutil/cipher_suites.go

Documentation: go.etcd.io/etcd/client/pkg/v3/tlsutil

     1  // Copyright 2018 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  // GetCipherSuite returns the corresponding cipher suite,
    23  // and boolean value if it is supported.
    24  func GetCipherSuite(s string) (uint16, bool) {
    25  	for _, c := range tls.CipherSuites() {
    26  		if s == c.Name {
    27  			return c.ID, true
    28  		}
    29  	}
    30  	for _, c := range tls.InsecureCipherSuites() {
    31  		if s == c.Name {
    32  			return c.ID, true
    33  		}
    34  	}
    35  	switch s {
    36  	case "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305":
    37  		return tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, true
    38  	case "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305":
    39  		return tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, true
    40  	}
    41  	return 0, false
    42  }
    43  
    44  // GetCipherSuites returns list of corresponding cipher suite IDs.
    45  func GetCipherSuites(ss []string) ([]uint16, error) {
    46  	cs := make([]uint16, len(ss))
    47  	for i, s := range ss {
    48  		var ok bool
    49  		cs[i], ok = GetCipherSuite(s)
    50  		if !ok {
    51  			return nil, fmt.Errorf("unexpected TLS cipher suite %q", s)
    52  		}
    53  	}
    54  
    55  	return cs, nil
    56  }
    57  

View as plain text