-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtls.go
291 lines (262 loc) · 7.23 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
package hime
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"net"
"net/http"
"strings"
"time"
)
// Restricted is the tls config for restricted mode
func Restricted() *tls.Config {
return &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{
tls.X25519,
tls.CurveP256,
},
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,
},
}
}
// Modern is the tls config for modern mode
func Modern() *tls.Config {
return &tls.Config{
MinVersion: tls.VersionTLS12,
CurvePreferences: []tls.CurveID{
tls.X25519,
tls.CurveP256,
},
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,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
},
}
}
// Compatible is the tls config for compatible mode
func Compatible() *tls.Config {
return &tls.Config{
MinVersion: tls.VersionTLS10,
CurvePreferences: []tls.CurveID{
tls.X25519,
tls.CurveP256,
},
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,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
},
}
}
// TLS type
type TLS struct {
SelfSign *SelfSign `yaml:"selfSign" json:"selfSign"`
CertFile string `yaml:"certFile" json:"certFile"`
KeyFile string `yaml:"keyFile" json:"keyFile"`
Profile string `yaml:"profile" json:"profile"`
MinVersion string `yaml:"minVersion" json:"minVersion"`
MaxVersion string `yaml:"maxVersion" json:"maxVersion"`
Curves []string `yaml:"curves" json:"curves"`
}
func cloneTLSNextProto(xs map[string]func(*http.Server, *tls.Conn, http.Handler)) map[string]func(*http.Server, *tls.Conn, http.Handler) {
if xs == nil {
return nil
}
rs := make(map[string]func(*http.Server, *tls.Conn, http.Handler))
for k, v := range xs {
rs[k] = v
}
return rs
}
func parseTLSVersion(s string) uint16 {
switch strings.ToLower(s) {
case "":
return 0
case "ssl3.0":
return tls.VersionSSL30
case "tls1.0":
return tls.VersionTLS10
case "tls1.1":
return tls.VersionTLS11
case "tls1.2":
return tls.VersionTLS12
case "tls1.3":
return tls.VersionTLS13
default:
panicf("unknown tls version '%s'", s)
}
panic("unreachable")
}
func (t *TLS) config() *tls.Config {
var tlsConfig *tls.Config
switch strings.ToLower(t.Profile) {
case "restricted":
tlsConfig = Restricted()
case "modern":
tlsConfig = Modern()
case "compatible":
tlsConfig = Compatible()
case "":
tlsConfig = &tls.Config{}
default:
panicf("unknown tls profile '%s'", t.Profile)
}
tlsConfig.MinVersion = parseTLSVersion(t.MinVersion)
tlsConfig.MaxVersion = parseTLSVersion(t.MaxVersion)
if t.Curves != nil {
tlsConfig.CurvePreferences = []tls.CurveID{}
for _, c := range t.Curves {
switch strings.ToLower(c) {
case "p256":
tlsConfig.CurvePreferences = append(tlsConfig.CurvePreferences, tls.CurveP256)
case "p384":
tlsConfig.CurvePreferences = append(tlsConfig.CurvePreferences, tls.CurveP384)
case "p521":
tlsConfig.CurvePreferences = append(tlsConfig.CurvePreferences, tls.CurveP521)
case "x25519":
tlsConfig.CurvePreferences = append(tlsConfig.CurvePreferences, tls.X25519)
default:
panicf("unknown tls curve '%s'", c)
}
}
}
if t.CertFile != "" && t.KeyFile != "" {
err := loadTLSCertKey(tlsConfig, t.CertFile, t.KeyFile)
if err != nil {
panicf("load key pair error; %v", err)
}
} else if t.SelfSign != nil {
t.SelfSign.config(tlsConfig)
}
return tlsConfig
}
// SelfSign type
type SelfSign struct {
Key struct {
Algo string `yaml:"algo" json:"algo"`
Size int `yaml:"size" json:"size"`
} `yaml:"key" json:"key"`
CN string `yaml:"cn" json:"cn"`
Hosts []string `yaml:"host" json:"host"`
}
func loadTLSCertKey(t *tls.Config, certFile, keyFile string) error {
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return err
}
t.Certificates = append(t.Certificates, cert)
return nil
}
func (s *SelfSign) config(t *tls.Config) error {
var priv any
var pub any
switch strings.ToLower(s.Key.Algo) {
case "ecdsa", "":
var curve elliptic.Curve
switch s.Key.Size {
case 224:
curve = elliptic.P224()
case 256, 0:
curve = elliptic.P256()
case 384:
curve = elliptic.P384()
case 521:
curve = elliptic.P521()
default:
return fmt.Errorf("invalid self-sign key size '%d'", s.Key.Size)
}
pri, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
return err
}
priv, pub = pri, &pri.PublicKey
case "rsa":
// TODO: make rsa key size configurable
pri, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return err
}
priv, pub = pri, &pri.PublicKey
default:
return fmt.Errorf("invalid self-sign key algo '%s'", s.Key.Algo)
}
sn, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
return err
}
cert := x509.Certificate{
SerialNumber: sn,
Subject: pkix.Name{
CommonName: s.CN,
Organization: []string{"Acme Co"},
},
NotAfter: time.Now().AddDate(1, 0, 0), // TODO: make not before configurable
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
for _, h := range s.Hosts {
if ip := net.ParseIP(h); ip != nil {
cert.IPAddresses = append(cert.IPAddresses, ip)
} else {
cert.DNSNames = append(cert.DNSNames, h)
}
}
certBytes, err := x509.CreateCertificate(rand.Reader, &cert, &cert, pub, priv)
if err != nil {
return err
}
certPem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certBytes})
var keyPemBlock pem.Block
switch k := priv.(type) {
case *ecdsa.PrivateKey:
b, err := x509.MarshalECPrivateKey(k)
if err != nil {
return err
}
keyPemBlock = pem.Block{Type: "EC PRIVATE KEY", Bytes: b}
case *rsa.PrivateKey:
keyPemBlock = pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)}
}
keyPem := pem.EncodeToMemory(&keyPemBlock)
tlsCert, err := tls.X509KeyPair(certPem, keyPem)
if err != nil {
return err
}
t.Certificates = append(t.Certificates, tlsCert)
return nil
}