forked from vulcand/vulcand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
243 lines (205 loc) · 6.34 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
package engine
import (
"crypto/tls"
"fmt"
)
// TLSSettings is a JSON and API friendly version of some of the tls.Config parameters
type TLSSettings struct {
// PreferServerCipherSuites controls whether the server selects the CipherSuites
PreferServerCipherSuites bool
// SkipVerify skips certificate check, very insecure
InsecureSkipVerify bool
// MinVersion is minimal TLS version "VersionTLS10" is default
MinVersion string
// MaxVersion is max supported TLS version, "VersionTLS12" is default
MaxVersion string
// SessionTicketsDisabled disables session ticket resumption support
SessionTicketsDisabled bool
// SessionCache specifies TLS session cache, default it 'LRU' with capacity 1024
SessionCache TLSSessionCache
// Preferred CipherSuites, default is:
//
// TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
// TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
// TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA
// TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
// TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA
// TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA
// TLS_RSA_WITH_AES_256_CBC_SHA
// TLS_RSA_WITH_AES_128_CBC_SHA
CipherSuites []string
}
// TLSSessionCache sets up parameters for TLS session cache
type TLSSessionCache struct {
// Session cache implementation, default is 'LRU' with capacity 1024
Type string
Settings *LRUSessionCacheSettings
}
type LRUSessionCacheSettings struct {
// LRU Capacity default is 1024
Capacity int
}
// NewTLSConfig validates the TLSSettings and returns the tls.Config with the converted parameters
func NewTLSConfig(s *TLSSettings) (*tls.Config, error) {
// Parse min and max TLS versions
var min, max uint16
var err error
if s.MinVersion == "" {
min = tls.VersionTLS10
} else if min, err = ParseTLSVersion(s.MinVersion); err != nil {
return nil, err
}
if s.MaxVersion == "" {
max = tls.VersionTLS12
} else if max, err = ParseTLSVersion(s.MaxVersion); err != nil {
return nil, err
}
var css []uint16
if len(s.CipherSuites) == 0 {
css = []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
}
} else {
css = make([]uint16, len(s.CipherSuites))
for i, suite := range s.CipherSuites {
cs, err := ParseCipherSuite(suite)
if err != nil {
return nil, err
}
css[i] = cs
}
}
var cache tls.ClientSessionCache
if !s.SessionTicketsDisabled {
cache, err = NewTLSSessionCache(&s.SessionCache)
if err != nil {
return nil, err
}
}
return &tls.Config{
MinVersion: min,
MaxVersion: max,
SessionTicketsDisabled: s.SessionTicketsDisabled,
ClientSessionCache: cache,
PreferServerCipherSuites: s.PreferServerCipherSuites,
CipherSuites: css,
InsecureSkipVerify: s.InsecureSkipVerify,
}, nil
}
// NewTLSSessionCache validates parameters and creates a new TLS session cache
func NewTLSSessionCache(s *TLSSessionCache) (tls.ClientSessionCache, error) {
cacheType := s.Type
if cacheType == "" {
cacheType = LRUCacheType
}
if cacheType != LRUCacheType {
return nil, fmt.Errorf("unsupported session cache type: %v", s.Type)
}
var capacity int
if params := s.Settings; params != nil {
if params.Capacity < 0 {
return nil, fmt.Errorf("bad LRU capacity: %v", params.Capacity)
} else if params.Capacity == 0 {
capacity = DefaultLRUCapacity
} else {
capacity = params.Capacity
}
}
return tls.NewLRUClientSessionCache(capacity), nil
}
func ParseCipherSuite(cs string) (uint16, error) {
switch cs {
case "TLS_RSA_WITH_RC4_128_SHA":
return tls.TLS_RSA_WITH_RC4_128_SHA, nil
case "TLS_RSA_WITH_3DES_EDE_CBC_SHA":
return tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA, nil
case "TLS_RSA_WITH_AES_128_CBC_SHA":
return tls.TLS_RSA_WITH_AES_128_CBC_SHA, nil
case "TLS_RSA_WITH_AES_256_CBC_SHA":
return tls.TLS_RSA_WITH_AES_256_CBC_SHA, nil
case "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA":
return tls.TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, nil
case "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA":
return tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, nil
case "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA":
return tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, nil
case "TLS_ECDHE_RSA_WITH_RC4_128_SHA":
return tls.TLS_ECDHE_RSA_WITH_RC4_128_SHA, nil
case "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA":
return tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, nil
case "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA":
return tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, nil
case "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA":
return tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, nil
case "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256":
return tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, nil
case "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256":
return tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, nil
}
return 0, fmt.Errorf("unsupported cipher suite: %v", cs)
}
func ParseTLSVersion(version string) (uint16, error) {
switch version {
case "VersionTLS10":
return tls.VersionTLS10, nil
case "VersionTLS11":
return tls.VersionTLS11, nil
case "VersionTLS12":
return tls.VersionTLS12, nil
}
return 0, fmt.Errorf("unsupported TLS version: %v", version)
}
const DefaultLRUCapacity = 1024
const LRUCacheType = "LRU"
func (s *TLSSettings) Equals(other *TLSSettings) bool {
scfg, err := NewTLSConfig(s)
if err != nil {
return false
}
ocfg, err := NewTLSConfig(other)
if err != nil {
return false
}
if scfg.PreferServerCipherSuites != ocfg.PreferServerCipherSuites ||
scfg.InsecureSkipVerify != ocfg.InsecureSkipVerify ||
scfg.MinVersion != ocfg.MinVersion ||
scfg.MaxVersion != ocfg.MaxVersion ||
scfg.SessionTicketsDisabled != ocfg.SessionTicketsDisabled {
return false
}
if len(scfg.CipherSuites) != len(ocfg.CipherSuites) {
return false
}
for i := range scfg.CipherSuites {
if scfg.CipherSuites[i] != ocfg.CipherSuites[i] {
return false
}
}
if !(&s.SessionCache).Equals(&other.SessionCache) {
return false
}
return true
}
func (c *TLSSessionCache) Equals(o *TLSSessionCache) bool {
if c.Type != o.Type {
return false
}
if c.Settings == nil && o.Settings == nil {
return true
}
cs, os := c.Settings, o.Settings
if (cs == nil && os != nil) || (cs != nil && os == nil) {
return false
}
if cs == nil && os == nil {
return true
}
return cs.Capacity == os.Capacity
}