-
Notifications
You must be signed in to change notification settings - Fork 14
/
tls.go
642 lines (532 loc) · 17.7 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
package config
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"io/ioutil"
"path/filepath"
"reflect"
"runtime"
"sort"
"strings"
pkcs12 "github.com/anz-bank/go-pkcs12"
"github.com/anz-bank/sysl-go/log"
)
var cipherSuites = 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": tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305": tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
"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,
"TLS_FALLBACK_SCSV": tls.TLS_FALLBACK_SCSV,
}
var tlsVersions = map[string]uint16{
"1.0": tls.VersionTLS10,
"1.1": tls.VersionTLS11,
"1.2": tls.VersionTLS12,
"1.3": tls.VersionTLS13,
}
var clientAuthTypes = map[string]tls.ClientAuthType{
"NoClientCert": tls.NoClientCert,
"RequestClientCert": tls.RequestClientCert,
"RequireAnyClientCert": tls.RequireAnyClientCert,
"VerifyClientCertIfGiven": tls.VerifyClientCertIfGiven,
"RequireAndVerifyClientCert": tls.RequireAndVerifyClientCert,
}
// Downward compatibility for low version TLS.
var renegotiationSupportTypes = map[string]tls.RenegotiationSupport{
"renegotiatenever": tls.RenegotiateNever,
"renegotiateonceasclient": tls.RenegotiateOnceAsClient,
"renegotiatefreelyasclient": tls.RenegotiateFreelyAsClient,
}
type TLSConfig struct {
MinVersion *string `yaml:"min" mapstructure:"min"`
MaxVersion *string `yaml:"max" mapstructure:"max"`
ClientAuth *string `yaml:"clientAuth" mapstructure:"clientAuth"`
Ciphers []string `yaml:"ciphers" mapstructure:"ciphers"`
ServerIdentities []*ServerIdentityConfig `yaml:"serverIdentities" mapstructure:"serverIdentities"` // One server needs more than 1 identities in some cases.
TrustedCertPool *TrustedCertPoolConfig `yaml:"trustedCertPool" mapstructure:"trustedCertPool"`
InsecureSkipVerify bool `yaml:"insecureSkipVerify" mapstructure:"insecureSkipVerify"`
SelfSigned bool `yaml:"selfSigned" mapstructure:"selfSigned"`
Renegotiation *string `yaml:"renegotiation" mapstructure:"renegotiation"` // Downward compatibility for low version TLS.
}
type TrustedCertPoolConfig struct {
Mode *string `yaml:"mode" mapstructure:"mode"`
Encoding *string `yaml:"encoding" mapstructure:"encoding"`
Path *string `yaml:"path" mapstructure:"path"`
Password *SensitiveString `yaml:"password" mapstructure:"password"`
}
type ServerIdentityConfig struct {
CertKeyPair *CertKeyPair `yaml:"certKeyPair" mapstructure:"certKeyPair"`
// Add Pkcs12Store to store cert and key as it is protected by password
PKCS12Store *Pkcs12Store `yaml:"p12Store" mapstructure:"p12Store"`
}
type CertKeyPair struct {
CertPath *string `yaml:"certPath" mapstructure:"certPath"`
KeyPath *string `yaml:"keyPath" mapstructure:"keyPath"`
}
type Pkcs12Store struct {
Path *string `yaml:"path" mapstructure:"path"`
Password *SensitiveString `yaml:"password" mapstructure:"password"`
}
// Cert path modes.
const (
DIRMODE = "directory"
FILEMODE = "file"
SYSMODE = "system"
)
// Cert encoding types.
const (
PEM = "pem"
PKCS12 = "pkcs12"
)
var CertPoolEncodingTypes = map[string]func(ctx context.Context, cfg *TrustedCertPoolConfig) (pool *x509.CertPool, err error){
PEM: buildPoolFromPEM,
PKCS12: buildPoolFromPKCS12,
}
func TLSVersions(cfg *TLSConfig) (min, max uint16, err error) {
if cfg.MinVersion != nil {
var has bool
if min, has = tlsVersions[*cfg.MinVersion]; !has {
return 0, 0, fmt.Errorf("invalid TLSMin config: %s", *cfg.MinVersion)
}
}
if cfg.MaxVersion != nil {
var has bool
if max, has = tlsVersions[*cfg.MaxVersion]; !has {
return 0, 0, fmt.Errorf("invalid TLSMax config: %s", *cfg.MaxVersion)
}
}
if min > max {
return 0, 0, fmt.Errorf("invalid TLS version config")
}
return min, max, nil
}
func TLSCiphers(cfg *TLSConfig) (ciphers []uint16, err error) {
tlsCipherCount := len(cipherSuites)
cfgCipherCount := len(cfg.Ciphers)
if cfgCipherCount > tlsCipherCount {
return nil, fmt.Errorf("TLS cipher suite configuration contains more ciphers than the number of known ciphers")
}
ciphers = make([]uint16, cfgCipherCount)
for i, cipher := range cfg.Ciphers {
c, ok := cipherSuites[cipher]
if !ok {
return nil, fmt.Errorf("unknown TLS cipher suite: %s", cipher)
}
ciphers[i] = c
}
return ciphers, nil
}
func TLSClientAuth(cfg *TLSConfig) (*tls.ClientAuthType, error) {
policy, ok := clientAuthTypes[*cfg.ClientAuth]
if !ok {
return nil, fmt.Errorf("invalid client authentication policy: %s", *cfg.ClientAuth)
}
return &policy, nil
}
func TLSRenegotiationSupport(cfg *TLSConfig) (*tls.RenegotiationSupport, error) {
if policy, ok := renegotiationSupportTypes[strings.ToLower(*cfg.Renegotiation)]; ok {
return &policy, nil
}
// Get allowed values, array has better string format than slice, so use array here.
keys := []string{}
for key := range renegotiationSupportTypes {
keys = append(keys, key)
}
sort.Strings(keys)
return nil, fmt.Errorf("renegotiation policy is invalid, expected value is one item in %s, but got: %s", keys, *cfg.Renegotiation)
}
func OurIdentityCertificates(cfg *TLSConfig) ([]tls.Certificate, error) {
if len(cfg.ServerIdentities) == 0 {
return nil, nil
}
cs := make([]tls.Certificate, 0)
for _, identity := range cfg.ServerIdentities {
if identity == nil {
continue
}
if identity.CertKeyPair != nil {
pair := identity.CertKeyPair
cert, err := tls.LoadX509KeyPair(*pair.CertPath, *pair.KeyPath)
if err != nil {
return nil, err
}
cs = append(cs, cert)
} else if identity.PKCS12Store != nil {
passBytes, err := base64.StdEncoding.DecodeString((identity.PKCS12Store.Password).Value())
if err != nil {
return nil, err
}
pass := string(passBytes)
tlsCert := tls.Certificate{}
p12bytes, err := ioutil.ReadFile(*identity.PKCS12Store.Path)
if err != nil {
return nil, err
}
privKey, cert, caCerts, err := pkcs12.DecodeChain(p12bytes, pass)
if err != nil {
return nil, err
}
tlsCert.PrivateKey = privKey
tlsCert.Certificate = append(tlsCert.Certificate, cert.Raw)
tlsCert.Leaf = cert
for _, caCert := range caCerts {
tlsCert.Certificate = append(tlsCert.Certificate, caCert.Raw)
}
cs = append(cs, tlsCert)
}
}
return cs, nil
}
func findCertsFromPath(cfg *TrustedCertPoolConfig) ([]string, error) {
var files []string
switch strings.ToLower(*cfg.Mode) {
case DIRMODE:
var err error
fileInfo, err := ioutil.ReadDir(*cfg.Path)
if err != nil {
return nil, err
}
for _, file := range fileInfo {
if file.IsDir() {
continue
}
files = append(files, filepath.Join(*cfg.Path, file.Name()))
}
case FILEMODE:
files = append(files, *cfg.Path)
default:
return nil, fmt.Errorf("unknown or missing mode: %v. Valid modes are %s & %s", *cfg.Mode, DIRMODE, FILEMODE)
}
return files, nil
}
func buildPoolFromPEM(ctx context.Context, cfg *TrustedCertPoolConfig) (*x509.CertPool, error) {
pool := x509.NewCertPool()
files, err := findCertsFromPath(cfg)
if err != nil {
return nil, err
}
var failedCerts []string
addedCerts := false
for _, file := range files {
cert, err := ioutil.ReadFile(file)
if err != nil {
failedCerts = append(failedCerts, file)
continue
}
if ok := pool.AppendCertsFromPEM(cert); !ok {
failedCerts = append(failedCerts, file)
continue
}
addedCerts = true
}
if failedCerts != nil {
if !addedCerts {
return nil, fmt.Errorf("failed to append any certificates to the RootCA. The following certs failed: %v", failedCerts)
}
log.Infof(ctx, "failed to append the following certs to RootCAs: %v", failedCerts)
}
return pool, nil
}
func buildPoolFromPKCS12(ctx context.Context, cfg *TrustedCertPoolConfig) (*x509.CertPool, error) {
pool := x509.NewCertPool()
passBytes, err := base64.StdEncoding.DecodeString((*cfg.Password).Value())
if err != nil {
return nil, err
}
pass := string(passBytes)
files, err := findCertsFromPath(cfg)
if err != nil {
return nil, err
}
var failedCerts []string
addedCerts := false
for _, file := range files {
p12bytes, err := ioutil.ReadFile(file)
if err != nil {
failedCerts = append(failedCerts, file)
continue
}
_, cert, caCerts, err := pkcs12.DecodeChain(p12bytes, pass)
if err != nil {
log.Error(ctx, err, "failed to decode PKCS12 file")
failedCerts = append(failedCerts, file)
continue
}
pool.AddCert(cert)
for _, cert := range caCerts {
pool.AddCert(cert)
}
addedCerts = true
}
if failedCerts != nil {
if !addedCerts {
return nil, fmt.Errorf("failed to append any certificates to the RootCA. The following certs failed: %v", failedCerts)
}
log.Infof(ctx, "failed to append the following certs to RootCAs: %v", failedCerts)
}
return pool, nil
}
func buildPool(ctx context.Context, cfg *TrustedCertPoolConfig) (*x509.CertPool, error) {
var pool *x509.CertPool
buildPoolFn, ok := CertPoolEncodingTypes[strings.ToLower(*cfg.Encoding)]
if !ok {
keys := make([]string, len(CertPoolEncodingTypes))
for key := range CertPoolEncodingTypes {
keys = append(keys, key)
}
return nil, fmt.Errorf("unrecognised certificate encoding: %s. Valid encodings are: %v in either upper or lower case", *cfg.Encoding, keys)
}
pool, err := buildPoolFn(ctx, cfg)
if err != nil {
return nil, err
}
return pool, nil
}
func GetTrustedCAs(ctx context.Context, cfg *TLSConfig) (*x509.CertPool, error) {
// certificates exchanged are self signed mostly applicable for dev env
// skip setting for rootcas, clientcas and cipher suites
if cfg.TrustedCertPool != nil {
if *cfg.TrustedCertPool.Mode != SYSMODE {
pool, err := buildPool(ctx, cfg.TrustedCertPool)
if err != nil {
return nil, err
}
return pool, nil
}
}
if runtime.GOOS == "windows" {
// crypto/x509: system root pool is not available on Windows
// hack: try returning nil and see what happens by default.
return nil, nil
}
return x509.SystemCertPool()
}
func makeSelfSignedTLSConfig(cfg *TLSConfig) (*tls.Config, error) {
tlsMin, tlsMax, err := TLSVersions(cfg)
if err != nil {
return nil, err
}
ourIdentityCertificates, err := OurIdentityCertificates(cfg)
if err != nil {
return nil, err
}
return &tls.Config{
MinVersion: tlsMin,
MaxVersion: tlsMax,
Certificates: ourIdentityCertificates,
ClientAuth: tls.NoClientCert,
}, nil
}
//nolint:funlen
func MakeTLSConfig(ctx context.Context, cfg *TLSConfig) (*tls.Config, error) {
if cfg == nil {
return nil, nil
}
if cfg.InsecureSkipVerify {
//nolint:gosec // This is configured by the user
log.Info(ctx, "It is insecure due to skipping server certificate verification")
return &tls.Config{InsecureSkipVerify: true}, nil
}
if cfg.SelfSigned {
return makeSelfSignedTLSConfig(cfg)
}
trustedCAs, err := GetTrustedCAs(ctx, cfg)
if err != nil {
return nil, err
}
tlsMin, tlsMax, err := TLSVersions(cfg)
if err != nil {
return nil, err
}
ciphers, err := TLSCiphers(cfg)
if err != nil {
return nil, err
}
policy, err := TLSClientAuth(cfg)
if err != nil {
return nil, err
}
ourIdentityCertificates, err := OurIdentityCertificates(cfg)
if err != nil {
return nil, err
}
renegotiation, err := TLSRenegotiationSupport(cfg)
if err != nil {
return nil, err
}
settings := &tls.Config{
MinVersion: tlsMin,
MaxVersion: tlsMax,
CipherSuites: ciphers,
PreferServerCipherSuites: true,
// Certificates must contain one or more certificate chains to present to the
// other side of the connection, to establish our server's identity.
// This is intended for use both when we are serving TLS and when we are the
// client establishing TLS connections with other servers.
Certificates: ourIdentityCertificates,
// Upstream (End-user) configuration
// Certificate authorities to trust when receiving certs from end users (i.e. acting as server)
ClientCAs: trustedCAs,
ClientAuth: *policy,
// Certificate authorities to trust when receiving certs from other servers (making requests, i.e acting as client)
RootCAs: trustedCAs,
InsecureSkipVerify: cfg.InsecureSkipVerify,
Renegotiation: *renegotiation,
}
return settings, nil
}
//nolint:funlen // TODO: Break this into smaller functions
func (t *TLSConfig) Validate() error {
if t == nil {
return nil
}
emptyCfg := &TLSConfig{}
if reflect.DeepEqual(t, emptyCfg) {
return fmt.Errorf("config missing")
}
if t.ClientAuth == nil {
return fmt.Errorf("clientAuth config missing")
}
_, ok := clientAuthTypes[*t.ClientAuth]
if !ok {
return fmt.Errorf("clientAuth: client authentication policy must be set if TLS is in use")
}
if t.MinVersion == nil {
return fmt.Errorf("min config missing")
}
_, ok = tlsVersions[*t.MinVersion]
if !ok {
return fmt.Errorf("min: TLS version not recognized")
}
if t.MaxVersion == nil {
return fmt.Errorf("max config missing")
}
_, ok = tlsVersions[*t.MaxVersion]
if !ok {
return fmt.Errorf("max: TLS version not recognized")
}
var failedCiphers []string
for _, cs := range t.Ciphers {
_, ok := cipherSuites[cs]
if !ok {
failedCiphers = append(failedCiphers, cs)
}
}
if len(failedCiphers) > 0 {
return fmt.Errorf("ciphers: %v are not valid", failedCiphers)
}
if t.Renegotiation == nil {
return fmt.Errorf("renegotiation config missing")
}
if _, ok := renegotiationSupportTypes[strings.ToLower(*t.Renegotiation)]; !ok {
return fmt.Errorf("renegotiation policy is invalid, expected policy is `RenegotiateNever`, `RenegotiateOnceAsClient` or `RenegotiateFreelyAsClient`, but got: %s", *t.Renegotiation)
}
if len(t.ServerIdentities) == 0 {
return fmt.Errorf("serverIdentities config missing")
}
for i, identity := range t.ServerIdentities {
if identity == nil {
return fmt.Errorf("serverIdentities[%d] %s", i, "config missing")
}
if err := identity.validate(); err != nil {
return fmt.Errorf("serverIdentity[%d].%w", i, err)
}
}
if t.TrustedCertPool == nil {
return fmt.Errorf("trustedCertPool config missing")
}
if err := t.TrustedCertPool.validate(); err != nil {
return fmt.Errorf("trustedCertPool.%v", err)
}
return nil
}
func (b *ServerIdentityConfig) validate() error {
if (b.PKCS12Store != nil) == (b.CertKeyPair != nil) {
return fmt.Errorf("p12Store/certKeyPair: only one may be configured")
}
if b.CertKeyPair != nil {
if err := b.CertKeyPair.validate(); err != nil {
return fmt.Errorf("certKeyPair.%v", err)
}
}
if b.PKCS12Store != nil {
if err := b.PKCS12Store.validate(); err != nil {
return fmt.Errorf("p12Store.%v", err)
}
}
return nil
}
func (c *CertKeyPair) validate() error {
if c.KeyPath == nil {
return fmt.Errorf("keyPath must be set")
}
if c.CertPath == nil {
return fmt.Errorf("certPath must be set")
}
return nil
}
func (p *Pkcs12Store) validate() error {
if p.Path == nil {
return fmt.Errorf("path must be set")
}
if p.Password == nil {
return fmt.Errorf("password must be set")
}
return nil
}
func (t *TrustedCertPoolConfig) validate() error {
if t.Mode == nil {
return fmt.Errorf("mode config missing")
}
lowMode := strings.ToLower(*t.Mode)
t.Mode = &lowMode
if *t.Mode != SYSMODE {
if t.Path == nil {
return fmt.Errorf("path config missing")
}
if t.Encoding != nil {
lowEncoding := strings.ToLower(*t.Encoding)
t.Encoding = &lowEncoding
if *t.Encoding == PKCS12 {
if t.Password == nil {
return fmt.Errorf("password config missing. Must be provided if the encoding config provided is type of %s", PKCS12)
}
}
} else {
return fmt.Errorf("encoding missing")
}
var validEncodings []string
for encoding := range CertPoolEncodingTypes {
validEncodings = append(validEncodings, encoding)
}
_, ok := CertPoolEncodingTypes[*t.Encoding]
if !ok {
return fmt.Errorf("encoding \"%s\" is not of valid encoding types: %v", *t.Encoding, validEncodings)
}
}
return nil
}