-
Notifications
You must be signed in to change notification settings - Fork 0
/
tls.go
416 lines (369 loc) · 11.1 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
/*
Copyright © 2023 ITRS Group
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package config
import (
"crypto"
"crypto/ecdh"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"strings"
"time"
"github.com/awnumar/memguard"
"github.com/rs/zerolog/log"
"github.com/itrs-group/cordial/pkg/host"
)
const DefaultKeyType = "ecdh"
// ParseCertificate reads a PEM encoded cert from path on host h, return
// the first found as a parsed certificate. The returned certificate is
// not verified or validated beyond that of the underlying Go x509
// package parsing functions.
func ParseCertificate(h host.Host, certfile string) (cert *x509.Certificate, err error) {
pembytes, err := h.ReadFile(certfile)
if err != nil {
return
}
for {
p, rest := pem.Decode(pembytes)
if p == nil {
return nil, fmt.Errorf("cannot locate certificate in %s", certfile)
}
if p.Type == "CERTIFICATE" {
return x509.ParseCertificate(p.Bytes)
}
pembytes = rest
}
}
// ParseCertificates reads a PEM encoded file from host h and returns
// all the certificates found (using the same rules as
// x509.ParseCertificates). The returned certificates are not verified
// or validated beyond that of the underlying Go x509 package parsing
// functions.
func ParseCertificates(h host.Host, p string) (certs []*x509.Certificate, err error) {
pembytes, err := h.ReadFile(p)
if err != nil {
return
}
return x509.ParseCertificates(pembytes)
}
// ParseKey tries to parse the DER encoded private key enclave, first as
// PKCS#8 and then as a PKCS#1 and finally as SEC1 (EC) if that fails.
// It returns the private and public keys or an error
func ParseKey(der *memguard.Enclave) (privateKey any, publickey crypto.PublicKey, err error) {
k, err := der.Open()
if err != nil {
return
}
defer k.Destroy()
if privateKey, err = x509.ParsePKCS8PrivateKey(k.Bytes()); err != nil {
if privateKey, err = x509.ParsePKCS1PrivateKey(k.Bytes()); err != nil {
if privateKey, err = x509.ParseECPrivateKey(k.Bytes()); err != nil {
return
}
}
}
if k, ok := privateKey.(crypto.Signer); ok {
publickey = k.Public()
}
return
}
// PublicKey parses the DER encoded private key enclave and returns the
// public key if successful. It will first try as PKCS#8 and then PKCS#1
// if that fails. Using this over the more general ParseKey() ensures
// the decoded private key is not returned to the caller when not
// required.
func PublicKey(der *memguard.Enclave) (publickey crypto.PublicKey, err error) {
var pkey any
k, err := der.Open()
if err != nil {
return
}
defer k.Destroy()
if pkey, err = x509.ParsePKCS8PrivateKey(k.Bytes()); err != nil {
if pkey, err = x509.ParsePKCS1PrivateKey(k.Bytes()); err != nil {
if pkey, err = x509.ParseECPrivateKey(k.Bytes()); err != nil {
return
}
}
}
if k, ok := pkey.(crypto.Signer); ok {
publickey = k.Public()
}
return
}
// WriteCert writes cert as PEM to file p on host h
func WriteCert(h host.Host, p string, cert *x509.Certificate) (err error) {
log.Debug().Msgf("write cert to %s", p)
pembytes := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
})
return h.WriteFile(p, pembytes, 0644)
}
// WriteCertChain concatenate certs and writes to path on host h
func WriteCertChain(h host.Host, p string, certs ...*x509.Certificate) (err error) {
log.Debug().Msgf("write certs to %s", p)
var pembytes []byte
for _, cert := range certs {
if cert == nil {
continue
}
p := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE",
Bytes: cert.Raw,
})
pembytes = append(pembytes, p...)
}
return h.WriteFile(p, pembytes, 0644)
}
// ReadPrivateKey reads a unencrypted, PEM-encoded private key and saves
// the der format key in a memguard.Enclave
func ReadPrivateKey(h host.Host, pt string) (key *memguard.Enclave, err error) {
pembytes, err := h.ReadFile(pt)
if err != nil {
return
}
for {
p, rest := pem.Decode(pembytes)
if p == nil {
return nil, fmt.Errorf("cannot locate private key in %s", pt)
}
if strings.HasSuffix(p.Type, "PRIVATE KEY") {
key = memguard.NewEnclave(p.Bytes)
return
}
pembytes = rest
}
}
// WritePrivateKey writes a DER encoded private key as a PKCS#8 encoded
// PEM file to path on host h. sets file permissions to 0600 (before
// umask)
func WritePrivateKey(h host.Host, pt string, key *memguard.Enclave) (err error) {
l, _ := key.Open()
defer l.Destroy()
pembytes := pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Bytes: l.Bytes(),
})
return h.WriteFile(pt, pembytes, 0600)
}
// CreateCertificateAndKey is a wrapper to create a new certificate
// given the signing cert and key and an optional private key to (re)use
// for the certificate creation. Returns a certificate and private key.
// Keys are usually PKCS#8 encoded and so need parsing after unsealing.
func CreateCertificateAndKey(template, parent *x509.Certificate, signingKeyDER, existingKeyDER *memguard.Enclave) (cert *x509.Certificate, certKeyDER *memguard.Enclave, err error) {
var certBytes []byte
// var certKey *rsa.PrivateKey
if template != parent && signingKeyDER == nil {
err = errors.New("parent key empty but not self-signing")
return
}
certKeyDER = existingKeyDER
if certKeyDER == nil {
keytype := PrivateKeyType(signingKeyDER)
if keytype == "" {
keytype = DefaultKeyType
}
certKeyDER, err = NewPrivateKey(keytype)
if err != nil {
panic(err)
}
}
// default the signingKey to the certKey (for self-signed root)
signingKey, certPubKey, err := ParseKey(certKeyDER)
if err != nil {
return
}
if signingKeyDER != nil {
signingKey, _, err = ParseKey(signingKeyDER)
if err != nil {
certKeyDER = nil
return
}
}
if certBytes, err = x509.CreateCertificate(rand.Reader, template, parent, certPubKey, signingKey); err != nil {
certKeyDER = nil
return
}
if cert, err = x509.ParseCertificate(certBytes); err != nil {
certKeyDER = nil
return
}
return
}
// PrivateKeyType returns the type of the DER encoded private key,
// suitable for use to NewPrivateKey
func PrivateKeyType(der *memguard.Enclave) (keytype string) {
if der == nil {
return
}
key, _, err := ParseKey(der)
if err != nil {
return
}
switch key.(type) {
case *rsa.PrivateKey:
return "rsa"
case *ecdsa.PrivateKey:
return "ecdsa"
case *ecdh.PrivateKey:
return "ecdh"
case ed25519.PrivateKey: // not a pointer
return "ed2559"
default:
return ""
}
}
// NewPrivateKey returns a PKCS#8 DER encoded private key as an enclave.
func NewPrivateKey(keytype string) (der *memguard.Enclave, err error) {
var privateKey any
switch keytype {
case "rsa":
privateKey, err = rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
log.Fatal().Err(err).Msg("")
}
case "ecdsa":
privateKey, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatal().Err(err).Msg("")
}
case "ed25519":
_, privateKey, err = ed25519.GenerateKey(nil)
if err != nil {
log.Fatal().Err(err).Msg("")
}
case "ecdh":
ecdsaKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatal().Err(err).Msg("")
}
privateKey, err = ecdsaKey.ECDH()
if err != nil {
log.Fatal().Err(err).Msg("")
}
default:
log.Fatal().Msgf("unsupported key type %s", keytype)
}
key, err := x509.MarshalPKCS8PrivateKey(privateKey)
if err != nil {
log.Fatal().Err(err).Msg("")
}
der = memguard.NewEnclave(key)
return
}
// CreateRootCert creates a new root certificate and private key and
// saves it with dir and file basefilepath with .pem and .key extensions. If
// overwrite is true then any existing certificate and key is
// overwritten.
func CreateRootCert(h host.Host, basefilepath string, cn string, overwrite bool, keytype string) (err error) {
// create rootCA.pem / rootCA.key
var cert *x509.Certificate
if !overwrite {
if _, err = ParseCertificate(h, basefilepath+".pem"); err == nil {
return host.ErrExists
}
}
serial, err := rand.Prime(rand.Reader, 64)
if err != nil {
return
}
template := &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
CommonName: cn,
},
NotBefore: time.Now().Add(-60 * time.Second),
NotAfter: time.Now().AddDate(10, 0, 0).Truncate(24 * time.Hour),
IsCA: true,
BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
MaxPathLen: -1,
}
privateKeyPEM, err := NewPrivateKey(keytype)
if err != nil {
return
}
cert, key, err := CreateCertificateAndKey(template, template, privateKeyPEM, nil)
if err != nil {
return
}
if err = WriteCert(h, basefilepath+".pem", cert); err != nil {
return
}
if err = WritePrivateKey(h, basefilepath+".key", key); err != nil {
return
}
return
}
// CreateSigningCert creates a new signing certificate and private key
// with the path and file bane name basefilepath. You must provide a
// valid root certificate and key in rootbasefilepath. If overwrite is
// true than any existing cert and key are overwritten.
func CreateSigningCert(h host.Host, basefilepath string, rootbasefilepath string, cn string, overwrite bool) (err error) {
var cert *x509.Certificate
if !overwrite {
if _, err = ParseCertificate(h, basefilepath+".pem"); err == nil {
return host.ErrExists
}
}
serial, err := rand.Prime(rand.Reader, 64)
if err != nil {
return
}
template := x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
CommonName: cn,
},
NotBefore: time.Now().Add(-60 * time.Second),
NotAfter: time.Now().AddDate(10, 0, 0).Truncate(24 * time.Hour),
IsCA: true,
BasicConstraintsValid: true,
KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
MaxPathLen: 0,
MaxPathLenZero: true,
}
rootCert, err := ParseCertificate(h, rootbasefilepath+".pem")
if err != nil {
return
}
rootKey, err := ReadPrivateKey(h, rootbasefilepath+".key")
if err != nil {
return
}
cert, key, err := CreateCertificateAndKey(&template, rootCert, rootKey, nil)
if err != nil {
return
}
if err = WriteCert(h, basefilepath+".pem", cert); err != nil {
return
}
if err = WritePrivateKey(h, basefilepath+".key", key); err != nil {
return
}
return
}