-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
306 lines (261 loc) · 7.63 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
package fleet
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"log"
"math/big"
"os"
"time"
"github.com/google/uuid"
"golang.org/x/crypto/sha3"
)
func (a *Agent) getLocalKey() (crypto.Signer, error) {
keyPem, err := a.dbFleetGet("internal_key:key")
if err != nil {
// gen & save a new key
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, fmt.Errorf("failed to generate local key: %w", err)
}
// encode to DER
key_der, err := x509.MarshalPKCS8PrivateKey(key)
if err != nil {
return nil, fmt.Errorf("failed to marshal pkcs8 key: %w", err)
}
// encode to PEM
key_pem := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: key_der})
// store in DB
a.dbSimpleSet([]byte("fleet"), []byte("internal_key:key"), key_pem)
return key, nil
}
// decode PEM key
pdata, _ := pem.Decode(keyPem)
if pdata == nil || pdata.Type != "PRIVATE KEY" {
return nil, errors.New("invalid private key in internal_key:key")
}
// parse pkcs8
keyIntf, err := x509.ParsePKCS8PrivateKey(pdata.Bytes)
if err != nil {
return nil, fmt.Errorf("while decoding pkcs8 key: %w", err)
}
if key, ok := keyIntf.(crypto.Signer); ok {
return key, nil
}
// should not happen
return nil, fmt.Errorf("failed to convert key type %T into crypto.Signer", keyIntf)
}
// KeyShake128 uses PKCS8 private key blob as hash key
func (a *Agent) KeyShake128(N []byte) (sha3.ShakeHash, error) {
keyPem, err := a.dbFleetGet("internal_key:key")
if err != nil {
// call getLocalKey() to generate key
_, err := a.getLocalKey()
if err != nil {
return nil, err
}
keyPem, err = a.dbFleetGet("internal_key:key")
if err != nil {
return nil, err
}
}
// decode PEM key
pdata, _ := pem.Decode(keyPem)
if pdata == nil || pdata.Type != "PRIVATE KEY" {
return nil, errors.New("invalid private key in internal_key:key")
}
v := sha3.NewCShake128(N, pdata.Bytes)
return v, nil
}
// KeySha256 uses PKCS8 private key blob as hash key
func (a *Agent) KeyShake256(N []byte) (sha3.ShakeHash, error) {
keyPem, err := a.dbFleetGet("internal_key:key")
if err != nil {
// call getLocalKey() to generate key
_, err := a.getLocalKey()
if err != nil {
return nil, err
}
keyPem, err = a.dbFleetGet("internal_key:key")
if err != nil {
return nil, err
}
}
// decode PEM key
pdata, _ := pem.Decode(keyPem)
if pdata == nil || pdata.Type != "PRIVATE KEY" {
return nil, errors.New("invalid private key in internal_key:key")
}
v := sha3.NewCShake256(N, pdata.Bytes)
return v, nil
}
func (a *Agent) GenInternalCert() (tls.Certificate, error) {
log.Printf("[tls] Generating new CA & client certificates")
// generate a new CA & certificate
ca_key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return tls.Certificate{}, err
}
// get key
key, err := a.getLocalKey()
if err != nil {
return tls.Certificate{}, err
}
id, err := uuid.NewRandom()
if err != nil {
return tls.Certificate{}, err
}
host, err := os.Hostname()
if err != nil {
// error getting hostname?
host = "localhost"
}
// generate CA cert
// copy value
tplCA := tplCAcrt
tplCA.NotBefore = time.Now()
tplCA.NotAfter = tplCA.NotBefore.Add(10 * 365 * 24 * time.Hour) // +10 years (more or less)
ca_crt_der, err := x509.CreateCertificate(rand.Reader, &tplCA, &tplCA, ca_key.Public(), ca_key)
if err != nil {
return tls.Certificate{}, err
}
ca_crt, err := x509.ParseCertificate(ca_crt_der)
if err != nil {
return tls.Certificate{}, err
}
// generate client cert & sign
tplUsr := tplUsrCrt
tplUsr.SerialNumber = big.NewInt(0).SetBytes(id[:])
tplUsr.Subject.CommonName = host
tplUsr.NotBefore = time.Now()
tplUsr.NotAfter = tplUsr.NotBefore.Add(1 * 365 * 24 * time.Hour) // +10 years (more or less)
crt_der, err := x509.CreateCertificate(rand.Reader, &tplUsr, ca_crt, key.Public(), ca_key)
if err != nil {
return tls.Certificate{}, err
}
// encode keys
ca_key_der, err := x509.MarshalPKCS8PrivateKey(ca_key)
if err != nil {
return tls.Certificate{}, err
}
// store stuff as PEM
ca_crt_pem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: ca_crt_der})
crt_pem := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: crt_der})
ca_key_pem := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: ca_key_der})
// store
a.dbSimpleSet([]byte("fleet"), []byte("internal_key:crt"), crt_pem)
a.dbSimpleSet([]byte("global"), []byte("internal:ca:master"), ca_crt_pem)
a.dbSimpleSet([]byte("fleet"), []byte("ca_key:key"), ca_key_pem)
log.Printf("[tls] New certificate: %s%s%s", ca_crt_pem, ca_key_pem, crt_pem)
return tls.Certificate{Certificate: [][]byte{crt_der}, PrivateKey: key}, nil
}
func (a *Agent) GetInternalCert() (tls.Certificate, error) {
// get internal certificate
crt, err := a.dbFleetGet("internal_key:crt")
if err != nil {
return tls.Certificate{}, err
}
key, err := a.dbFleetGet("internal_key:key")
if err != nil {
return tls.Certificate{}, err
}
return tls.X509KeyPair(crt, key)
}
func (a *Agent) GetDefaultPublicCert() (tls.Certificate, error) {
// get internal certificate
crt, err := a.dbFleetGet("public_key:crt")
if err != nil {
return tls.Certificate{}, err
}
key, err := a.dbFleetGet("public_key:key")
if err != nil {
return tls.Certificate{}, err
}
return tls.X509KeyPair(crt, key)
}
func (a *Agent) GetCA() (*x509.CertPool, error) {
ca := x509.NewCertPool()
// get records
c, err := a.NewDbCursor([]byte("global"))
count := 0
if err == nil {
defer c.Close()
k, v := c.Seek([]byte("internal:ca:"))
for {
if k == nil {
break
}
ca.AppendCertsFromPEM(v)
count++
k, v = c.Next()
}
}
if count == 0 {
// nothing found in db, check for file?
err := a.getFile("internal_ca.pem", func(ca_data []byte) error {
ca.AppendCertsFromPEM(ca_data)
// store in db
err = a.dbSimpleSet([]byte("global"), []byte("internal:ca:legacy_import"), ca_data)
return nil
})
if err != nil {
return nil, err
}
}
return ca, nil
}
// GetTlsConfig returns TLS config suitable for making public facing ssl
// servers.
func (a *Agent) GetTlsConfig() (*tls.Config, error) {
if cert, err := a.GetDefaultPublicCert(); err == nil {
cfg := new(tls.Config)
cfg.Certificates = []tls.Certificate{cert}
a.SeedTlsConfig(cfg)
a.ConfigureTlsServer(cfg)
return cfg, nil
}
if cert, err := a.GetInternalCert(); err == nil {
cfg := new(tls.Config)
cfg.Certificates = []tls.Certificate{cert}
a.SeedTlsConfig(cfg)
a.ConfigureTlsServer(cfg)
return cfg, nil
}
return nil, errors.New("failed to load TLS certificates")
}
func (a *Agent) GetInternalTlsConfig() (*tls.Config, error) {
if cert, err := a.GetInternalCert(); err == nil {
cfg := new(tls.Config)
cfg.Certificates = []tls.Certificate{cert}
a.SeedTlsConfig(cfg)
a.ConfigureTlsServer(cfg)
return cfg, nil
}
return nil, errors.New("failed to load TLS certificates")
}
func (a *Agent) GetClientTlsConfig() (*tls.Config, error) {
if cert, err := a.GetInternalCert(); err == nil {
cfg := new(tls.Config)
cfg.Certificates = []tls.Certificate{cert}
return cfg, nil
}
return nil, errors.New("failed to load TLS certificates")
}
func (a *Agent) ConfigureTlsServer(cfg *tls.Config) {
// perform some basic settings to ensure server is secure
cfg.MinVersion = tls.VersionTLS12
cfg.CurvePreferences = []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}
cfg.CipherSuites = []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
}
}