-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtls.go
284 lines (259 loc) · 7.83 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
284
package ovirtclient
import (
"crypto/tls"
"crypto/x509"
"os"
"path/filepath"
"regexp"
"sync"
)
// TLSProvider creates a TLS configuration for use by the oVirt client.
type TLSProvider interface {
// CreateTLSConfig returns a working TLS configuration for the client, or an error if the configuration cannot be
// created.
CreateTLSConfig() (*tls.Config, error)
}
// BuildableTLSProvider is a TLSProvider that allows adding configuration options.
type BuildableTLSProvider interface {
TLSProvider
// Insecure disables CA certificate verification. This cannot be chained as any further option doesn't make any
// sense.
Insecure() TLSProvider
// CACertsFromMemory adds one or more CA certificate from an in-memory byte slice containing PEM-encoded
// certificates. This function can be called multiple times to add multiple certificates. The function will fail if
// no certificates have been added.
CACertsFromMemory(caCert []byte) BuildableTLSProvider
// CACertsFromFile adds certificates from a single file. The certificate must be in PEM format. This function can
// be called multiple times to add multiple files.
CACertsFromFile(file string) BuildableTLSProvider
// CACertsFromDir adds all PEM-encoded certificates from a directory. If one or more patterns are passed, the
// files will only be added if the files match at least one of the patterns. The certificate will fail if one or
// more matching files don't contain a valid certificate.
CACertsFromDir(dir string, patterns ...*regexp.Regexp) BuildableTLSProvider
// CACertsFromSystem adds the system certificate store. This may fail because the certificate store is not available
// or not supported on the platform.
CACertsFromSystem() BuildableTLSProvider
// CACertsFromCertPool sets a certificate pool to use as a source for certificates. This is incompatible with the
// CACertsFromSystem call as both create a certificate pool. This function must not be called twice.
CACertsFromCertPool(*x509.CertPool) BuildableTLSProvider
}
// TLS creates a BuildableTLSProvider that can be used to easily add trusted CA certificates and generally follows best
// practices in terms of TLS settings.
func TLS() BuildableTLSProvider {
return &standardTLSProvider{
lock: &sync.Mutex{},
}
}
type standardTLSProvider struct {
lock *sync.Mutex
insecure bool
caCerts [][]byte
files []string
directories []standardTLSProviderDirectory
certPool *x509.CertPool
system bool
configured bool
}
type standardTLSProviderDirectory struct {
dir string
patterns []*regexp.Regexp
}
func (s *standardTLSProvider) Insecure() TLSProvider {
s.lock.Lock()
defer s.lock.Unlock()
s.configured = true
s.insecure = true
return s
}
func (s *standardTLSProvider) CACertsFromCertPool(certPool *x509.CertPool) BuildableTLSProvider {
s.lock.Lock()
defer s.lock.Unlock()
if s.certPool != nil {
panic(newError(EConflict, "the CACertsFromCertPool function has been called twice"))
}
s.configured = true
s.certPool = certPool
return s
}
func (s *standardTLSProvider) CACertsFromMemory(caCert []byte) BuildableTLSProvider {
s.lock.Lock()
defer s.lock.Unlock()
s.configured = true
s.caCerts = append(s.caCerts, caCert)
return s
}
func (s *standardTLSProvider) CACertsFromFile(file string) BuildableTLSProvider {
s.lock.Lock()
defer s.lock.Unlock()
s.configured = true
s.files = append(s.files, file)
return s
}
func (s *standardTLSProvider) CACertsFromDir(dir string, patterns ...*regexp.Regexp) BuildableTLSProvider {
s.lock.Lock()
defer s.lock.Unlock()
s.configured = true
s.directories = append(s.directories, standardTLSProviderDirectory{
dir,
patterns,
})
return s
}
func (s *standardTLSProvider) CACertsFromSystem() BuildableTLSProvider {
s.lock.Lock()
defer s.lock.Unlock()
s.configured = true
s.system = true
return s
}
func (s *standardTLSProvider) CreateTLSConfig() (*tls.Config, error) {
s.lock.Lock()
defer s.lock.Unlock()
if !s.configured {
return nil, newError(
ETLSError,
"TLS not configured (Did you forget to call certificate configuration options on the TLS provider?)",
)
}
if s.insecure {
return &tls.Config{
InsecureSkipVerify: true, //nolint:gosec
}, nil
}
tlsConfig := &tls.Config{
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
},
PreferServerCipherSuites: true,
SessionTicketsDisabled: false,
SessionTicketKey: [32]byte{},
ClientSessionCache: nil,
// Based on Mozilla intermediate compatibility:
// https://wiki.mozilla.org/Security/Server_Side_TLS#Intermediate_compatibility_.28recommended.29
MinVersion: tls.VersionTLS12,
MaxVersion: 0,
CurvePreferences: []tls.CurveID{
tls.CurveP256, tls.CurveP384,
},
DynamicRecordSizingDisabled: false,
Renegotiation: 0,
KeyLogWriter: nil,
}
certPool := s.certPool
if certPool == nil {
var err error
if certPool, err = s.createCertPool(); err != nil {
return nil, err
}
}
if err := s.addCertsFromMemory(certPool); err != nil {
return nil, err
}
if err := s.addCertsFromFile(certPool); err != nil {
return nil, err
}
if err := s.addCertsFromDir(certPool); err != nil {
return nil, err
}
tlsConfig.RootCAs = certPool
return tlsConfig, nil
}
func (s *standardTLSProvider) addCertsFromDir(certPool *x509.CertPool) error {
for _, dir := range s.directories {
files, err := os.ReadDir(dir.dir)
if err != nil {
return wrap(
err,
ELocalIO,
"failed to list contents of %s directory",
dir.dir,
)
}
for _, info := range files {
if info.IsDir() {
continue
}
if len(dir.patterns) > 0 {
matches := false
for _, pattern := range dir.patterns {
if pattern.MatchString(info.Name()) {
matches = true
break
}
}
if !matches {
continue
}
}
fullPath := filepath.Join(dir.dir, info.Name())
data, err := os.ReadFile(fullPath) //nolint:gosec
if err != nil {
return wrap(
err,
EFileReadFailed,
"failed to read certificate file: %s (%w)",
fullPath,
)
}
if !certPool.AppendCertsFromPEM(data) {
return newError(
ETLSError,
"failed to add certificate from file: %s (certificate not in PEM format?)",
fullPath,
)
}
}
}
return nil
}
func (s *standardTLSProvider) addCertsFromFile(certPool *x509.CertPool) error {
for _, file := range s.files {
pemData, err := os.ReadFile(file) //nolint:gosec
if err != nil {
return wrap(err, EFileReadFailed, "failed to read CA certificate from file %s", file)
}
if ok := certPool.AppendCertsFromPEM(pemData); !ok {
return newError(
ETLSError,
"the provided CA certificate is not a valid certificate in PEM format in file %s",
file,
)
}
}
return nil
}
func (s *standardTLSProvider) addCertsFromMemory(certPool *x509.CertPool) error {
for i, caCert := range s.caCerts {
if ok := certPool.AppendCertsFromPEM(caCert); !ok {
return newError(
EBadArgument,
"the provided CA certificate number #%d is not a valid certificate in PEM format",
i,
)
}
}
return nil
}
func (s *standardTLSProvider) createCertPool() (*x509.CertPool, error) {
if s.system && s.certPool != nil {
return nil, newError(ETLSError, "both system and cert pool are specified, these options are incompatible")
}
if s.certPool != nil {
return s.certPool, nil
}
if !s.system {
return x509.NewCertPool(), nil
}
certPool, err := x509.SystemCertPool()
if err != nil {
// This is the case on Windows before go 1.18 where the system certificate pool is not available.
// See https://github.com/golang/go/issues/16736
return nil, wrap(err, ETLSError, "system cert pool not available")
}
return certPool, nil
}