-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
tls.go
283 lines (247 loc) · 8.91 KB
/
tls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package proxy
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"net"
"reflect"
"strings"
"time"
"github.com/grepplabs/kafka-proxy/config"
"github.com/pkg/errors"
)
var (
supportedCurvesMap = map[string]tls.CurveID{
"X25519": tls.X25519,
"P256": tls.CurveP256,
"P384": tls.CurveP384,
"P521": tls.CurveP521,
}
supportedCiphersMap = map[string]uint16{
"TLS_RSA_WITH_RC4_128_SHA": tls.TLS_RSA_WITH_RC4_128_SHA,
"TLS_RSA_WITH_3DES_EDE_CBC_SHA": tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
"TLS_RSA_WITH_AES_128_CBC_SHA": tls.TLS_RSA_WITH_AES_128_CBC_SHA,
"TLS_RSA_WITH_AES_256_CBC_SHA": tls.TLS_RSA_WITH_AES_256_CBC_SHA,
"TLS_RSA_WITH_AES_128_CBC_SHA256": tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
"TLS_RSA_WITH_AES_128_GCM_SHA256": tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
"TLS_RSA_WITH_AES_256_GCM_SHA384": tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
"TLS_ECDHE_ECDSA_WITH_RC4_128_SHA": tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA,
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA": tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA": tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
"TLS_ECDHE_RSA_WITH_RC4_128_SHA": tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA,
"TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
"TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA": tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
"TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256": tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256,
"TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
"TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384": tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
"TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384": tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256": tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256": tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
"TLS_AES_128_GCM_SHA256": tls.TLS_AES_128_GCM_SHA256,
"TLS_AES_256_GCM_SHA384": tls.TLS_AES_256_GCM_SHA384,
"TLS_CHACHA20_POLY1305_SHA256": tls.TLS_CHACHA20_POLY1305_SHA256,
}
zeroTime = time.Time{}
)
func newTLSListenerConfig(conf *config.Config) (*tls.Config, error) {
opts := conf.Proxy.TLS
if opts.ListenerKeyFile == "" || opts.ListenerCertFile == "" {
return nil, errors.New("Listener key and cert files must not be empty")
}
certPEMBlock, err := ioutil.ReadFile(opts.ListenerCertFile)
if err != nil {
return nil, err
}
keyPEMBlock, err := ioutil.ReadFile(opts.ListenerKeyFile)
if err != nil {
return nil, err
}
keyPEMBlock, err = decryptPEM(keyPEMBlock, opts.ListenerKeyPassword)
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return nil, err
}
cipherSuites, err := getCipherSuites(opts.ListenerCipherSuites)
if err != nil {
return nil, err
}
curvePreferences, err := getCurvePreferences(opts.ListenerCurvePreferences)
if err != nil {
return nil, err
}
cfg := &tls.Config{
Certificates: []tls.Certificate{cert},
ClientAuth: tls.NoClientCert,
PreferServerCipherSuites: true,
MinVersion: tls.VersionTLS12,
CurvePreferences: curvePreferences,
CipherSuites: cipherSuites,
}
if opts.CAChainCertFile != "" {
caCertPEMBlock, err := ioutil.ReadFile(opts.CAChainCertFile)
if err != nil {
return nil, err
}
clientCAs := x509.NewCertPool()
if ok := clientCAs.AppendCertsFromPEM(caCertPEMBlock); !ok {
return nil, errors.New("Failed to parse listener root certificate")
}
cfg.ClientCAs = clientCAs
cfg.ClientAuth = tls.RequireAndVerifyClientCert
}
tlsValidateFunc, err := tlsClientCertVerificationFunc(conf)
if err != nil {
return nil, err
}
cfg.VerifyPeerCertificate = tlsValidateFunc
return cfg, nil
}
func getCipherSuites(enabledCipherSuites []string) ([]uint16, error) {
suites := make([]uint16, 0)
for _, suite := range enabledCipherSuites {
cipher, ok := supportedCiphersMap[strings.TrimSpace(suite)]
if !ok {
return nil, errors.Errorf("invalid cipher suite '%s' selected, supported ciphers %v", suite, reflect.ValueOf(supportedCiphersMap).MapKeys())
}
suites = append(suites, cipher)
}
if len(suites) == 0 {
return nil, nil
}
return suites, nil
}
func getCurvePreferences(enabledCurvePreferences []string) ([]tls.CurveID, error) {
curvePreferences := make([]tls.CurveID, 0)
for _, curveID := range enabledCurvePreferences {
curvePreference, ok := supportedCurvesMap[strings.TrimSpace(curveID)]
if !ok {
return nil, errors.Errorf("invalid curveID '%s' selected, supported curveIDs %v", curveID, reflect.ValueOf(supportedCurvesMap).MapKeys())
}
curvePreferences = append(curvePreferences, curvePreference)
}
if len(curvePreferences) == 0 {
return nil, nil
}
return curvePreferences, nil
}
func newTLSClientConfig(conf *config.Config) (*tls.Config, error) {
// https://blog.cloudflare.com/exposing-go-on-the-internet/
opts := conf.Kafka.TLS
cfg := &tls.Config{InsecureSkipVerify: opts.InsecureSkipVerify}
if opts.ClientCertFile != "" && opts.ClientKeyFile != "" {
certPEMBlock, err := ioutil.ReadFile(opts.ClientCertFile)
if err != nil {
return nil, err
}
keyPEMBlock, err := ioutil.ReadFile(opts.ClientKeyFile)
if err != nil {
return nil, err
}
keyPEMBlock, err = decryptPEM(keyPEMBlock, opts.ClientKeyPassword)
if err != nil {
return nil, err
}
cert, err := tls.X509KeyPair(certPEMBlock, keyPEMBlock)
if err != nil {
return nil, err
}
cfg.Certificates = []tls.Certificate{cert}
}
if opts.CAChainCertFile != "" {
caCertPEMBlock, err := ioutil.ReadFile(opts.CAChainCertFile)
if err != nil {
return nil, err
}
rootCAs := x509.NewCertPool()
if ok := rootCAs.AppendCertsFromPEM(caCertPEMBlock); !ok {
return nil, errors.New("Failed to parse client root certificate")
}
cfg.RootCAs = rootCAs
}
return cfg, nil
}
func decryptPEM(pemData []byte, password string) ([]byte, error) {
keyBlock, _ := pem.Decode(pemData)
if keyBlock == nil {
return nil, errors.New("Failed to parse PEM")
}
if x509.IsEncryptedPEMBlock(keyBlock) {
if password == "" {
return nil, errors.New("PEM is encrypted, but password is empty")
}
key, err := x509.DecryptPEMBlock(keyBlock, []byte(password))
if err != nil {
return nil, err
}
block := &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: key,
}
return pem.EncodeToMemory(block), nil
}
return pemData, nil
}
func parseCertificate(certFile string) (*x509.Certificate, error) {
content, readErr := ioutil.ReadFile(certFile)
if readErr != nil {
return nil, errors.Errorf("Failed to read file from location '%s'", certFile)
}
block, _ := pem.Decode(content)
cert, parseErr := x509.ParseCertificate(block.Bytes)
if parseErr != nil {
return nil, errors.Errorf("Failed to parse certificate file from location '%s'", certFile)
}
return cert, nil
}
func handshakeAsTLSAndValidateClientCert(conn net.Conn, expectedCert *x509.Certificate, handshakeTimeout time.Duration) error {
tlsConn, ok := conn.(*tls.Conn)
if !ok {
return errors.New("Unable to cast connection to TLS when validating client cert")
}
err := handshakeTLSConn(tlsConn, handshakeTimeout)
if err != nil {
return err
}
actualClientCert := filterClientCertificate(tlsConn.ConnectionState().PeerCertificates)
result := validateClientCert(actualClientCert, expectedCert)
return result
}
func handshakeTLSConn(tlsConn *tls.Conn, timeout time.Duration) error {
err := tlsConn.SetDeadline(time.Now().Add(timeout))
if err != nil {
return errors.Errorf("Failed to set deadline with handshake timeout in seconds %f on connection: %v", timeout.Seconds(), err)
}
err = tlsConn.Handshake()
if err != nil {
return errors.Errorf("TLS handshake failed when exchanging client certificates: %v", err)
}
err = tlsConn.SetDeadline(zeroTime)
if err != nil {
return errors.Errorf("Failed to reset deadline on connection: %v", err)
}
return err
}
func filterClientCertificate(peerCertificates []*x509.Certificate) *x509.Certificate {
for _, v := range peerCertificates {
if !v.IsCA {
return v
}
}
return nil
}
func validateClientCert(actualClientCert *x509.Certificate, expectedCert *x509.Certificate) error {
if actualClientCert == nil {
return errors.New("Client cert not found in TLS connection")
}
if !actualClientCert.Equal(expectedCert) {
return errors.New("Client cert sent by proxy client does not match brokers client cert (tls-client-cert-file)")
}
return nil
}