-
Notifications
You must be signed in to change notification settings - Fork 0
/
tlsutil.go
124 lines (110 loc) · 3.02 KB
/
tlsutil.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
package tlsutil
import (
"crypto/tls"
"os"
"github.com/pkg/errors"
"golang.org/x/crypto/acme/autocert"
)
type Option func(*tls.Config) error
// Wrap wraps multiple Options into one.
func Wrap(opts ...Option) Option {
return func(cfg *tls.Config) error {
for _, opt := range opts {
if err := opt(cfg); err != nil {
return err
}
}
return nil
}
}
func WithError(err error) Option {
return func(cfg *tls.Config) error {
return err
}
}
// WithKeyPair load a certificate from a certFile, keyFile pair, and append to tls.Config's Certificates
func WithKeyPair(certFile, keyFile string) Option {
return func(cfg *tls.Config) error {
cer, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return errors.Wrap(err, "failed to load keypair")
}
cfg.Certificates = append(cfg.Certificates, cer)
return nil
}
}
type ACMEOption func(*autocert.Manager) error
// AMCEWrap wraps multiple ACMEOptions into one.
func ACMEWrap(opts ...ACMEOption) ACMEOption {
return func(mgr *autocert.Manager) error {
for _, opt := range opts {
if err := opt(mgr); err != nil {
return err
}
}
return nil
}
}
// WithACME configures TLS to use ACME, configure by a ACMEOptions.
func WithACME(opts ...ACMEOption) Option {
return func(cfg *tls.Config) error {
mgr := &autocert.Manager{
Prompt: autocert.AcceptTOS,
}
for _, opt := range opts {
if err := opt(mgr); err != nil {
return err
}
}
cfg.GetCertificate = mgr.GetCertificate
return nil
}
}
// WithACMEHosts adds hosts to the ACME host policy.
func WithACMEHosts(hosts []string) ACMEOption {
return func(mgr *autocert.Manager) error {
if len(hosts) > 0 {
mgr.HostPolicy = autocert.HostWhitelist(hosts...)
}
return nil
}
}
// WithACMEDirCache configures a cache directory for ACME certificates.
func WithACMEDirCache(dir string) ACMEOption {
return func(mgr *autocert.Manager) error {
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
mgr.Cache = autocert.DirCache(dir)
return nil
}
}
// WithTLS12 configures a tls.Config to the intersection of Mozilla's modern compatibility, and go's capability.
// https://wiki.mozilla.org/Security/Server_Side_TLS#Modern_compatibility
// https://blog.gopheracademy.com/advent-2016/exposing-go-on-the-internet/
func WithTLS12() Option {
return func(cfg *tls.Config) error {
cfg.MinVersion = tls.VersionTLS12
cfg.PreferServerCipherSuites = true
cfg.CurvePreferences = []tls.CurveID{tls.X25519, tls.CurveP256}
cfg.CipherSuites = []uint16{
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,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
}
return nil
}
}
// NewTLSConfig returns a new tls.Config with all options applied.
func NewTLSConfig(opts ...Option) (*tls.Config, error) {
cfg := &tls.Config{}
for _, opt := range opts {
if err := opt(cfg); err != nil {
return nil, err
}
}
return cfg, nil
}