-
Notifications
You must be signed in to change notification settings - Fork 5
/
certin.go
531 lines (467 loc) · 15.2 KB
/
certin.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
package certin
import (
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"net"
"net/mail"
"net/url"
"time"
)
var (
DefaultKeyType = "rsa-2048"
DefaultDuration = 365 * 24 * time.Hour
)
// Request is a simplified configuration for generating a keypair and certificate
// with the NewCert() func. The most common attributes for a keypair and cert are
// available but if you need more control over the certificate contents you should
// create a x509.Certificate template and use the NewCertFromX509Template() func
// instead.
type Request struct {
// CommonName to use in the certificate Subject
CN string
// Organization(s) to include in the Subject
O []string
// Organizationl Units(s) to include in the Subject
OU []string
// SANs is a list of SubjectAltNames to include in the certificate. DNS, IP, Email, and URIs are
// supported.
SANs []string
// Certiicate duration. Default is 1 year if not specified
Duration time.Duration
// IsCA will create a CA certificate that can be used to sign other certificates.
IsCA bool
// KeyType is the type of private/public key pair to create. Supported keytypes
// are:
// rsa-2048, rsa-3072, rsa-4096
// ecdsa-224, ecdsa-256, ecdsa-384, ecdsa-521
// ed25519
KeyType string
}
// KeyAndCert represents a bundle of private, public keys and an associated Certificate
type KeyAndCert struct {
Certificate *x509.Certificate
PrivateKey crypto.PrivateKey
PublicKey crypto.PublicKey
}
// KeyAndCSR represents a bundle of private, public keys and an associated Certificate Request
type KeyAndCSR struct {
CertificateRequest *x509.CertificateRequest
PrivateKey crypto.PrivateKey
PublicKey crypto.PublicKey
}
// NewCert creates a new keypair and certificate from a Request object. If parent is
// nil it will be a self-signed certificate, otherwise it will be signed by the private
// key and certificate in the parent object.
func NewCert(parent *KeyAndCert, req Request) (*KeyAndCert, error) {
if req.KeyType == "" {
req.KeyType = DefaultKeyType
}
priv, err := GenerateKey(req.KeyType)
if err != nil {
return nil, fmt.Errorf("failed to generate key: %v", err)
}
pub := priv.(crypto.Signer).Public()
skid, err := hashKeyId(pub)
if err != nil {
return nil, err
}
serial, err := randomSerialNumber()
if err != nil {
return nil, err
}
notBefore := time.Now().Add(-30 * time.Second)
notAfter := time.Now().Add(req.Duration)
if req.Duration == 0 {
notAfter = time.Now().Add(DefaultDuration)
}
keyUsage := x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature
if req.IsCA {
keyUsage = x509.KeyUsageCertSign
}
self := &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
Organization: req.O,
OrganizationalUnit: req.OU,
CommonName: req.CN,
},
SubjectKeyId: skid,
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: keyUsage,
BasicConstraintsValid: true,
IsCA: req.IsCA,
MaxPathLenZero: req.IsCA,
}
// Add SANs. Supported types: IPaddr, email, URIs, DNSnames
appendAltNamesToCertificate(self, req.SANs)
// for non-CA certs add the common name to the list of SANs since modern browsers
// are phasing out common name.
// TODO: fix this for the case of non-DNSName in CN such as email addr.
if len(self.DNSNames) == 0 && !req.IsCA {
self.DNSNames = append(self.DNSNames, req.CN)
}
if !req.IsCA {
self.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth}
if len(self.EmailAddresses) > 0 {
self.ExtKeyUsage = append(self.ExtKeyUsage, x509.ExtKeyUsageCodeSigning, x509.ExtKeyUsageEmailProtection)
}
}
// if parent is nil, this will be a self signed cert
signerKey := priv
signerCert := self
if parent != nil {
signerCert = parent.Certificate
signerKey = parent.PrivateKey
akid, err := hashKeyId(parent.PublicKey)
if err != nil {
return nil, err
}
self.AuthorityKeyId = akid
}
// return signKeyAndCert(self, signerCert, pub, signerKey)
certDER, err := x509.CreateCertificate(rand.Reader, self, signerCert, pub, signerKey)
if err != nil {
return nil, fmt.Errorf("failed to sign cert: %v", err)
}
cert, err := x509.ParseCertificate(certDER)
if err != nil {
return nil, fmt.Errorf("failed to parse generated cert: %v", err)
}
bundle := &KeyAndCert{
Certificate: cert,
PrivateKey: priv,
PublicKey: pub,
}
return bundle, nil
}
// TLSCertificate returns a tls.Certificate suitable for use with: tls.Config{Certificates: k.TLSCertificate()}.
func (k *KeyAndCert) TLSCertificate() tls.Certificate {
return tls.Certificate{
Certificate: [][]byte{k.Certificate.Raw},
PrivateKey: k.PrivateKey,
Leaf: k.Certificate,
}
}
// CertPool returns a *x509.CertPool suitable containing the certificate. Use this
// with CA certs to generate a CertPool for use with: tls.Config{RootCAs: k.CertPool()}.
func (k *KeyAndCert) CertPool() *x509.CertPool {
pool := x509.NewCertPool()
pool.AddCert(k.Certificate)
return pool
}
// NewCertFromX509Template creates a new keypair and certificate from an X509.Certificate template.
// If parent is nil, it will be self-signed, otherwise it will be signed by the private key
// and cert from the parent.
func NewCertFromX509Template(parent *KeyAndCert, keyType string, templ *x509.Certificate) (*KeyAndCert, error) {
if keyType == "" {
keyType = DefaultKeyType
}
priv, err := GenerateKey(keyType)
if err != nil {
return nil, fmt.Errorf("failed to generate key: %v", err)
}
pub := priv.(crypto.Signer).Public()
// generate a random serial number if the template did not provide one. Go won't sign a cert without a serial
if templ.SerialNumber == nil {
serial, err := randomSerialNumber()
if err != nil {
return nil, err
}
templ.SerialNumber = serial
}
// if parent is nil, this will be a self signed cert
signerCert := templ
if parent != nil {
signerCert = parent.Certificate
}
signerKey := priv
if parent != nil {
signerKey = parent.PrivateKey
}
certDER, err := x509.CreateCertificate(rand.Reader, templ, signerCert, pub, signerKey)
if err != nil {
return nil, fmt.Errorf("failed to sign cert: %v", err)
}
cert, err := x509.ParseCertificate(certDER)
if err != nil {
return nil, fmt.Errorf("failed to parse generated cert: %v", err)
}
bundle := &KeyAndCert{
Certificate: cert,
PrivateKey: priv,
PublicKey: pub,
}
return bundle, nil
}
// NewCSR creates a new keypair and certificate request from a Request object.
func NewCSR(req Request) (*KeyAndCSR, error) {
if req.KeyType == "" {
req.KeyType = DefaultKeyType
}
priv, err := GenerateKey(req.KeyType)
if err != nil {
return nil, fmt.Errorf("failed to generate key: %v", err)
}
pub := priv.(crypto.Signer).Public()
templ := &x509.CertificateRequest{
Subject: pkix.Name{
Organization: req.O,
OrganizationalUnit: req.OU,
CommonName: req.CN,
},
// TODO: fix this for the case of non-DNSName in CN such as email addr.
DNSNames: []string{req.CN}, // Copy commonname into the SAN
}
// Add SANs. Supported types: IPaddr, email, URIs, DNSnames
appendAltNamesToCertificateRequest(templ, req.SANs)
csrDER, err := x509.CreateCertificateRequest(rand.Reader, templ, priv)
if err != nil {
return nil, fmt.Errorf("failed to generate certificate signing request: %v", err)
}
csr, err := x509.ParseCertificateRequest(csrDER)
if err != nil {
return nil, fmt.Errorf("failed to parse generated certificate signing request: %v", err)
}
bundle := &KeyAndCSR{
CertificateRequest: csr,
PrivateKey: priv,
PublicKey: pub,
}
return bundle, nil
}
// GenerateKey generates a private/public key pair. Valid keytypes are:
// rsa-2048, rsa-3072, rsa-4096
// ecdsa-224, ecdsa-256, ecdsa-384, ecdsa-521
// ed25519
func GenerateKey(keyType string) (crypto.PrivateKey, error) {
switch keyType {
case "rsa-2048":
return rsa.GenerateKey(rand.Reader, 2048)
case "rsa-3072":
return rsa.GenerateKey(rand.Reader, 3072)
case "rsa-4096":
return rsa.GenerateKey(rand.Reader, 4096)
case "ecdsa-224":
return ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
case "ecdsa-256":
return ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
case "ecdsa-384":
return ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
case "ecdsa-521":
return ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
case "ed25519":
_, priv, err := ed25519.GenerateKey(rand.Reader)
return priv, err
}
return nil, fmt.Errorf("unknown keyType %s", keyType)
}
// ExportKeyAndCert saves a private key and certficate from a KeyAndCert to keyFile and certFile.
func ExportKeyAndCert(keyFile, certFile string, cert *KeyAndCert) error {
if err := ExportPrivateKey(keyFile, cert.PrivateKey); err != nil {
return err
}
if err := ExportCert(certFile, cert.Certificate); err != nil {
return err
}
return nil
}
// ExportKeyAndCSR saves a private key and certficate request from a KeyAndCSR to keyFile and csrFile.
func ExportKeyAndCSR(keyFile, csrFile string, csr *KeyAndCSR) error {
if err := ExportPrivateKey(keyFile, csr.PrivateKey); err != nil {
return err
}
if err := ExportCSR(csrFile, csr.CertificateRequest); err != nil {
return err
}
return nil
}
// ExportPrivateKey saves a private key in PEM format from a crypto.PrivateKey to file.
func ExportPrivateKey(file string, priv crypto.PrivateKey) error {
derBytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
return fmt.Errorf("failed to encode private key: %v", err)
}
return ioutil.WriteFile(
file,
pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: derBytes}),
0o600)
}
// ExportPublicKey saves a public key in PEM format from a crypto.PublicKey to file.
func ExportPublicKey(file string, pub crypto.PublicKey) error {
derBytes, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return fmt.Errorf("failed to encode public key: %v", err)
}
return ioutil.WriteFile(
file,
pem.EncodeToMemory(&pem.Block{Type: "PUBLIC KEY", Bytes: derBytes}),
0o600)
}
// ExportCert saves a certificate in PEM format from a *x509.Certificate to file.
func ExportCert(file string, cert *x509.Certificate) error {
return ioutil.WriteFile(
file,
pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}),
0o600)
}
// ExportCSR saves a certificate request in PEM format from a *x509.CertificateRequest to file.
func ExportCSR(file string, csr *x509.CertificateRequest) error {
return ioutil.WriteFile(
file,
pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csr.Raw}),
0o600)
}
// LoadKeyAndCert loads and parses a private key and certificate from keyFile and
// certFile and returns a KeyAndCert.
func LoadKeyAndCert(keyFile string, certFile string) (*KeyAndCert, error) {
key, err := LoadKey(keyFile)
if err != nil {
return nil, err
}
cert, err := LoadCert(certFile)
if err != nil {
return nil, err
}
return &KeyAndCert{
Certificate: cert,
PrivateKey: key,
PublicKey: key.(crypto.Signer).Public(),
}, nil
}
// LoadKeyAndCSR loads and parses a private key and certificate request from keyFile and
// csrFile and returns a KeyAndCSR.
func LoadKeyAndCSR(keyFile, csrFile string) (*KeyAndCSR, error) {
key, err := LoadKey(keyFile)
if err != nil {
return nil, err
}
csr, err := LoadCSR(csrFile)
if err != nil {
return nil, err
}
return &KeyAndCSR{
CertificateRequest: csr,
PrivateKey: key,
PublicKey: key.(crypto.Signer).Public(),
}, nil
}
// LoadCert loads and parses a certificate from a PEM-formatted file and returns a *x509.Certificate.
func LoadCert(file string) (*x509.Certificate, error) {
pemBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
der, _ := pem.Decode(pemBytes)
if der == nil {
return nil, fmt.Errorf("failed to parse certificate: %s", file)
}
return x509.ParseCertificate(der.Bytes)
}
// LoadCSR loads and parses a certificate request from a PEM-formatted file and returns a *x509.Certificate.
func LoadCSR(file string) (*x509.CertificateRequest, error) {
pemBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
der, _ := pem.Decode(pemBytes)
if der == nil {
return nil, fmt.Errorf("failed to parse certificate request: %s", file)
}
return x509.ParseCertificateRequest(der.Bytes)
}
// LoadKey loads and parses a private key from file and returns a crypto.PrivateKey.
func LoadKey(file string) (crypto.PrivateKey, error) {
pemBytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
der, _ := pem.Decode(pemBytes)
if der == nil {
return nil, fmt.Errorf("failed to parse key: %s", file)
}
if key, err := x509.ParsePKCS1PrivateKey(der.Bytes); err == nil {
return key, nil
}
if key, err := x509.ParsePKCS8PrivateKey(der.Bytes); err == nil {
switch key := key.(type) {
case *rsa.PrivateKey, *ecdsa.PrivateKey:
return key, nil
case ed25519.PrivateKey:
return key, nil
default:
return nil, fmt.Errorf("Found unknown private key type in PKCS#8 wrapping")
}
}
if key, err := x509.ParseECPrivateKey(der.Bytes); err == nil {
return key, nil
}
return nil, fmt.Errorf("Failed to parse private key")
}
// appendAltNamesToCertificate parses a slice of SANs and append them to a &x509.Certificate
// Supported types: IPaddr, email, URIs, DNSnames
func appendAltNamesToCertificate(cert *x509.Certificate, sans []string) {
for _, h := range sans {
if ip := net.ParseIP(h); ip != nil {
cert.IPAddresses = append(cert.IPAddresses, ip)
} else if email, err := mail.ParseAddress(h); err == nil && email.Address == h {
cert.EmailAddresses = append(cert.EmailAddresses, h)
} else if uriName, err := url.Parse(h); err == nil && uriName.Scheme != "" && uriName.Host != "" {
cert.URIs = append(cert.URIs, uriName)
} else {
cert.DNSNames = append(cert.DNSNames, h)
}
}
}
// appendAltNamesToCertificateRequest parses a slice of SANs and append them to an &x509.CertificateRequest.
// Supported types: IPaddr, email, URIs, DNSnames
func appendAltNamesToCertificateRequest(csr *x509.CertificateRequest, sans []string) {
for _, h := range sans {
if ip := net.ParseIP(h); ip != nil {
csr.IPAddresses = append(csr.IPAddresses, ip)
} else if email, err := mail.ParseAddress(h); err == nil && email.Address == h {
csr.EmailAddresses = append(csr.EmailAddresses, h)
} else if uriName, err := url.Parse(h); err == nil && uriName.Scheme != "" && uriName.Host != "" {
csr.URIs = append(csr.URIs, uriName)
} else {
csr.DNSNames = append(csr.DNSNames, h)
}
}
}
func hashKeyId(pub crypto.PublicKey) ([]byte, error) {
spkiASN1, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return []byte{}, fmt.Errorf("failed to marshal public key: %v", err)
}
var spki struct {
Algorithm pkix.AlgorithmIdentifier
SubjectPublicKey asn1.BitString
}
_, err = asn1.Unmarshal(spkiASN1, &spki)
if err != nil {
return []byte{}, fmt.Errorf("failed to decode public key: %v", err)
}
skid := sha1.Sum(spki.SubjectPublicKey.Bytes)
return skid[:], nil
}
func randomSerialNumber() (*big.Int, error) {
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
return nil, fmt.Errorf("failed to generate serial number: %v", err)
}
return serialNumber, nil
}