@@ -6,74 +6,48 @@ import (
66 "encoding/pem"
77 "io/ioutil"
88 "net"
9+ "reflect"
910 "strings"
1011 "time"
1112
1213 "github.com/grepplabs/kafka-proxy/config"
13- "github.com/klauspost/cpuid"
1414 "github.com/pkg/errors"
1515)
1616
17- type clientCertSubjectField string
18-
19- const (
20- clientCertSubjectCommonName = "CN"
21- clientCertSubjectCountry = "C"
22- clientCertSubjectProvince = "S"
23- clientCertSubjectLocality = "L"
24- clientCertSubjectOrganization = "O"
25- clientCertSubjectOrganizationalUnit = "OU"
26- )
27-
2817var (
29- defaultCurvePreferences = []tls.CurveID {
30- tls .CurveP256 ,
31- tls .X25519 ,
32- }
33-
3418 supportedCurvesMap = map [string ]tls.CurveID {
3519 "X25519" : tls .X25519 ,
3620 "P256" : tls .CurveP256 ,
3721 "P384" : tls .CurveP384 ,
3822 "P521" : tls .CurveP521 ,
3923 }
40-
41- defaultCiphers = []uint16 {
42- tls .TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 ,
43- tls .TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 ,
44- tls .TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 ,
45- tls .TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ,
46- tls .TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 ,
47- tls .TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 ,
48- }
49-
50- defaultCiphersNonAESNI = []uint16 {
51- tls .TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 ,
52- tls .TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 ,
53- tls .TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 ,
54- tls .TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 ,
55- tls .TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 ,
56- tls .TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ,
57- }
58- // https://github.com/mholt/caddy/blob/master/caddytls/config.go
5924 supportedCiphersMap = map [string ]uint16 {
60- "ECDHE-ECDSA-AES256-GCM-SHA384" : tls .TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 ,
61- "ECDHE-RSA-AES256-GCM-SHA384" : tls .TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 ,
62- "ECDHE-ECDSA-AES128-GCM-SHA256" : tls .TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 ,
63- "ECDHE-RSA-AES128-GCM-SHA256" : tls .TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ,
64- "ECDHE-ECDSA-WITH-CHACHA20-POLY1305" : tls .TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 ,
65- "ECDHE-RSA-WITH-CHACHA20-POLY1305" : tls .TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 ,
66- "ECDHE-RSA-AES256-CBC-SHA" : tls .TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA ,
67- "ECDHE-RSA-AES128-CBC-SHA" : tls .TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA ,
68- "ECDHE-RSA-AES128-CBC-SHA256" : tls .TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 ,
69- "ECDHE-ECDSA-AES256-CBC-SHA" : tls .TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA ,
70- "ECDHE-ECDSA-AES128-CBC-SHA" : tls .TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA ,
71- "RSA-AES256-CBC-SHA" : tls .TLS_RSA_WITH_AES_256_CBC_SHA ,
72- "RSA-AES128-CBC-SHA" : tls .TLS_RSA_WITH_AES_128_CBC_SHA ,
73- "ECDHE-RSA-3DES-EDE-CBC-SHA" : tls .TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA ,
74- "RSA-3DES-EDE-CBC-SHA" : tls .TLS_RSA_WITH_3DES_EDE_CBC_SHA ,
25+ "TLS_RSA_WITH_RC4_128_SHA" : tls .TLS_RSA_WITH_RC4_128_SHA ,
26+ "TLS_RSA_WITH_3DES_EDE_CBC_SHA" : tls .TLS_RSA_WITH_3DES_EDE_CBC_SHA ,
27+ "TLS_RSA_WITH_AES_128_CBC_SHA" : tls .TLS_RSA_WITH_AES_128_CBC_SHA ,
28+ "TLS_RSA_WITH_AES_256_CBC_SHA" : tls .TLS_RSA_WITH_AES_256_CBC_SHA ,
29+ "TLS_RSA_WITH_AES_128_CBC_SHA256" : tls .TLS_RSA_WITH_AES_128_CBC_SHA256 ,
30+ "TLS_RSA_WITH_AES_128_GCM_SHA256" : tls .TLS_RSA_WITH_AES_128_GCM_SHA256 ,
31+ "TLS_RSA_WITH_AES_256_GCM_SHA384" : tls .TLS_RSA_WITH_AES_256_GCM_SHA384 ,
32+ "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA" : tls .TLS_ECDHE_ECDSA_WITH_RC4_128_SHA ,
33+ "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA" : tls .TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA ,
34+ "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA" : tls .TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA ,
35+ "TLS_ECDHE_RSA_WITH_RC4_128_SHA" : tls .TLS_ECDHE_RSA_WITH_RC4_128_SHA ,
36+ "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA" : tls .TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA ,
37+ "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA" : tls .TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA ,
38+ "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA" : tls .TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA ,
39+ "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256" : tls .TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 ,
40+ "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256" : tls .TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 ,
41+ "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" : tls .TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ,
42+ "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" : tls .TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 ,
43+ "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" : tls .TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 ,
44+ "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" : tls .TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 ,
45+ "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256" : tls .TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 ,
46+ "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256" : tls .TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 ,
47+ "TLS_AES_128_GCM_SHA256" : tls .TLS_AES_128_GCM_SHA256 ,
48+ "TLS_AES_256_GCM_SHA384" : tls .TLS_AES_256_GCM_SHA384 ,
49+ "TLS_CHACHA20_POLY1305_SHA256" : tls .TLS_CHACHA20_POLY1305_SHA256 ,
7550 }
76-
7751 zeroTime = time.Time {}
7852)
7953
@@ -103,10 +77,6 @@ func newTLSListenerConfig(conf *config.Config) (*tls.Config, error) {
10377 if err != nil {
10478 return nil , err
10579 }
106- // for security, ensure TLS_FALLBACK_SCSV is always included first
107- if len (cipherSuites ) == 0 || cipherSuites [0 ] != tls .TLS_FALLBACK_SCSV {
108- cipherSuites = append ([]uint16 {tls .TLS_FALLBACK_SCSV }, cipherSuites ... )
109- }
11080 curvePreferences , err := getCurvePreferences (opts .ListenerCurvePreferences )
11181 if err != nil {
11282 return nil , err
@@ -142,56 +112,32 @@ func newTLSListenerConfig(conf *config.Config) (*tls.Config, error) {
142112 return cfg , nil
143113}
144114
145- func removeEmptyStrings (input []string ) []string {
146- output := []string {}
147- for _ , value := range input {
148- if value == "" {
149- continue
150- }
151- output = append (output , value )
152- }
153- return output
154- }
155-
156115func getCipherSuites (enabledCipherSuites []string ) ([]uint16 , error ) {
157116 suites := make ([]uint16 , 0 )
158117 for _ , suite := range enabledCipherSuites {
159118 cipher , ok := supportedCiphersMap [strings .TrimSpace (suite )]
160119 if ! ok {
161- return nil , errors .Errorf ("invalid cipher suite '%s' selected" , suite )
120+ return nil , errors .Errorf ("invalid cipher suite '%s' selected, supported ciphers %v " , suite , reflect . ValueOf ( supportedCiphersMap ). MapKeys () )
162121 }
163122 suites = append (suites , cipher )
164123 }
165124 if len (suites ) == 0 {
166- return getPreferredDefaultCiphers () , nil
125+ return nil , nil
167126 }
168127 return suites , nil
169128}
170129
171- // getPreferredDefaultCiphers returns an appropriate cipher suite to use, depending on
172- // the hardware support available for AES-NI.
173- //
174- // See https://github.com/mholt/caddy/issues/1674
175- func getPreferredDefaultCiphers () []uint16 {
176- if cpuid .CPU .AesNi () {
177- return defaultCiphers
178- }
179-
180- // Return a cipher suite that prefers ChaCha20
181- return defaultCiphersNonAESNI
182- }
183-
184130func getCurvePreferences (enabledCurvePreferences []string ) ([]tls.CurveID , error ) {
185131 curvePreferences := make ([]tls.CurveID , 0 )
186132 for _ , curveID := range enabledCurvePreferences {
187133 curvePreference , ok := supportedCurvesMap [strings .TrimSpace (curveID )]
188134 if ! ok {
189- return nil , errors .Errorf ("invalid curveID '%s' selected" , curveID )
135+ return nil , errors .Errorf ("invalid curveID '%s' selected, supported curveIDs %v " , curveID , reflect . ValueOf ( supportedCurvesMap ). MapKeys () )
190136 }
191137 curvePreferences = append (curvePreferences , curvePreference )
192138 }
193139 if len (curvePreferences ) == 0 {
194- return defaultCurvePreferences , nil
140+ return nil , nil
195141 }
196142 return curvePreferences , nil
197143}
0 commit comments