-
Notifications
You must be signed in to change notification settings - Fork 1
/
certificate.go
450 lines (389 loc) · 13 KB
/
certificate.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
// Copyright certyaml authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package certyaml
import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/ed25519"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
"net"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/tsaarni/x500dn"
)
// Certificate defines the properties for generating a certificate.
//
// Note that struct tags are for certyaml command line command to unmarshal manifest file.
type Certificate struct {
// Subject defines the distinguished name for the certificate.
// Example: CN=Joe.
Subject string `json:"subject"`
// SubjectAltNames defines an optional list of values for x509 Subject Alternative Name extension.
// Examples: DNS:www.example.com, IP:1.2.3.4, URI:https://www.example.com.
SubjectAltNames []string `json:"sans"`
// KeyType defines the certificate key algorithm.
// Default value is KeyTypeEC (elliptic curve) if KeyType is undefined (when value is 0).
KeyType KeyType `json:"-"`
// KeySize defines the key length in bits.
// Default value is 256 (EC) or 2048 (RSA) if KeySize is undefined (when value is 0).
// Examples: For key_type EC: 256, 384, 521. For key_type RSA: 1024, 2048, 4096. For key_type ED25519: 256.
KeySize int `json:"key_size"`
// Expires automatically defines certificate's NotAfter field by adding duration defined in Expires to the current time.
// Default value is 8760h (one year) if Expires is undefined (when value is nil).
// NotAfter takes precedence over Expires.
Expires *time.Duration `json:"-"`
// KeyUsage defines bitmap of values for x509 key usage extension.
// If KeyUsage is undefined (when value is 0),
// CertSign and CRLSign are set for CA certificates,
// KeyEncipherment and DigitalSignature are set for end-entity certificates.
KeyUsage x509.KeyUsage `json:"-"`
// ExtKeyUsage defines a sequence of x509 extended key usages.
// Not set by default.
ExtKeyUsage []x509.ExtKeyUsage `json:"-"`
// Issuer refers to the issuer Certificate.
// Self-signed certificate is generated if Issuer is undefined (when value is nil).
Issuer *Certificate `json:"-" hash:"-"`
// IsCA defines if certificate is / is not CA.
// If IsCA is undefined (when value is nil), true is set by default for self-signed certificates (Issuer is nil).
IsCA *bool `json:"ca"`
// NotBefore defines certificate not to be valid before this time.
// Default value is current time if NotBefore is undefined (when value is nil).
NotBefore *time.Time `json:"not_before"`
// NotAfter defines certificate not to be valid after this time.
// Default value is current time + Expires if NotAfter is undefined (when value is nil)
NotAfter *time.Time `json:"not_after"`
// SerialNumber defines serial number for the certificate.
// If not set, the default value is current time in nanoseconds.
SerialNumber *big.Int `json:"-" hash:"-"`
// CRLDistributionPoint defines the URI for downloading the CRL for this certificate.
// Not set by default.
CRLDistributionPoints []string `json:"crl_distribution_points"`
// GeneratedCert is a pointer to the generated certificate and private key.
// It is automatically set after calling any of the Certificate functions.
GeneratedCert *tls.Certificate `json:"-" hash:"-"`
// lazyInitialize ensures that only single goroutine can run lazy initialization of certificate concurrently.
// Concurrent regeneration of certificate and private key by explicit call to Generate() is not supported.
lazyInitialize sync.Mutex `json:"-" hash:"-"`
}
type KeyType uint
const (
KeyTypeEC = iota
KeyTypeRSA
KeyTypeEd25519
)
// TLSCertificate returns the Certificate as tls.Certificate.
// Complete certificate chain (up to but not including root) is included for end-entity certificates.
// A key pair and certificate will be generated at first call of any Certificate functions.
// Error is not nil if generation fails.
func (c *Certificate) TLSCertificate() (tls.Certificate, error) {
err := c.ensureGenerated()
if err != nil {
return tls.Certificate{}, err
}
return *c.GeneratedCert, nil
}
// X509Certificate returns the Certificate as x509.Certificate.
// A key pair and certificate will be generated at first call of any Certificate functions.
// Error is not nil if generation fails.
func (c *Certificate) X509Certificate() (x509.Certificate, error) {
err := c.ensureGenerated()
if err != nil {
return x509.Certificate{}, err
}
cert, err := x509.ParseCertificate(c.GeneratedCert.Certificate[0])
return *cert, err
}
// PublicKey returns crypto.PublicKey associated to the Certificate.
// A key pair and certificate will be generated at first call of any Certificate functions.
// Error is not nil if generation fails.
func (c *Certificate) PublicKey() (crypto.PublicKey, error) {
err := c.ensureGenerated()
if err != nil {
return nil, err
}
return c.GeneratedCert.PrivateKey.(crypto.Signer).Public(), nil
}
// PrivateKey returns crypto.Signer that represents the PrivateKey associated to the Certificate.
// A key pair and certificate will be generated at first call of any Certificate functions.
// Error is not nil if generation fails.
func (c *Certificate) PrivateKey() (crypto.Signer, error) {
err := c.ensureGenerated()
if err != nil {
return nil, err
}
return c.GeneratedCert.PrivateKey.(crypto.Signer), nil
}
// PEM returns the Certificate as certificate and private key PEM buffers.
// Complete certificate chain (up to but not including root) is included for end-entity certificates.
// A key pair and certificate will be generated at first call of any Certificate functions.
// Error is not nil if generation fails.
func (c *Certificate) PEM() (cert []byte, key []byte, err error) {
err = c.ensureGenerated()
if err != nil {
return
}
cert, err = encodeToPEMBlocks("CERTIFICATE", c.GeneratedCert.Certificate)
if err != nil {
return
}
k, err := x509.MarshalPKCS8PrivateKey(c.GeneratedCert.PrivateKey)
if err != nil {
cert = nil
return
}
key, err = encodeToPEMBlocks("PRIVATE KEY", [][]byte{k})
if err != nil {
cert = nil
return
}
return
}
// CertPEM returns the certificate as a PEM buffer.
// This method is useful in single-value context, for example when populating struct field.
// Unlike the PEM() method, which handles errors, this method will panic if an error occurs.
func (c *Certificate) CertPEM() []byte {
cert, _, err := c.PEM()
if err != nil {
panic(err)
}
return cert
}
// KeyPEM returns the private key as a PEM buffer.
// This method is useful in single-value context, for example when populating struct field.
// Unlike the PEM() method, which handles errors, this method will panic if an error occurs.
func (c *Certificate) KeyPEM() []byte {
_, key, err := c.PEM()
if err != nil {
panic(err)
}
return key
}
// WritePEM writes the Certificate as certificate and private key PEM files.
// Complete certificate chain (up to but not including root) is included for end-entity certificates.
// A key pair and certificate will be generated at first call of any Certificate functions.
// Error is not nil if generation fails.
func (c *Certificate) WritePEM(certFile, keyFile string) error {
err := c.ensureGenerated()
if err != nil {
return err
}
cert, key, err := c.PEM()
if err != nil {
return err
}
err = os.WriteFile(certFile, cert, 0o600)
if err != nil {
return err
}
err = os.WriteFile(keyFile, key, 0o600)
if err != nil {
return err
}
return nil
}
func (c *Certificate) defaults() error {
_, err := x500dn.ParseDN(c.Subject)
if err != nil {
return err
}
if c.KeySize == 0 {
if c.KeyType == KeyTypeEC {
c.KeySize = 256
} else if c.KeyType == KeyTypeRSA {
c.KeySize = 2048
} else if c.KeyType == KeyTypeEd25519 {
c.KeySize = 256
}
}
if c.Expires == nil && c.NotAfter == nil {
year := 8760 * time.Hour
c.Expires = &year
}
if c.IsCA == nil {
noExplicitIssuer := (c.Issuer == nil)
c.IsCA = &noExplicitIssuer
}
if c.KeyUsage == 0 {
if *c.IsCA {
c.KeyUsage = x509.KeyUsageCertSign | x509.KeyUsageCRLSign
} else {
c.KeyUsage = x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature
}
}
if c.SerialNumber == nil {
c.SerialNumber = big.NewInt(time.Now().UnixNano())
}
return nil
}
func (c *Certificate) ensureGenerated() error {
c.lazyInitialize.Lock()
defer c.lazyInitialize.Unlock()
if c.GeneratedCert == nil {
err := c.Generate()
if err != nil {
return err
}
}
return nil
}
// Generate forces re-generation of key pair and certificate according to current state of the Certificate.
// Usually it is automatically called when necessary, e.g. PEM() will internally call Generate().
// It can be called explicitly after changing Certificate fields since certificate was last generated,
// or when a new certificate with same values is needed.
// Error is not nil if generation fails.
func (c *Certificate) Generate() error {
// Traverse the certificate hierarchy recursively to ensure issuing CAs have been generated as well.
if c.Issuer != nil {
err := c.Issuer.ensureGenerated()
if err != nil {
return err
}
}
// Ensure defaults are set correctly.
err := c.defaults()
if err != nil {
return err
}
// Generate key-pair for the certificate.
var key crypto.Signer
if c.KeyType == KeyTypeEC {
var curve elliptic.Curve
switch c.KeySize {
case 256:
curve = elliptic.P256()
case 384:
curve = elliptic.P384()
case 521:
curve = elliptic.P521()
default:
return fmt.Errorf("invalid EC key size: %d (valid: 256, 384, 521)", c.KeySize)
}
key, err = ecdsa.GenerateKey(curve, rand.Reader)
} else if c.KeyType == KeyTypeRSA {
key, err = rsa.GenerateKey(rand.Reader, c.KeySize)
} else if c.KeyType == KeyTypeEd25519 {
if c.KeySize != 256 {
return fmt.Errorf("invalid Ed25519 key size: %d (valid: 256)", c.KeySize)
}
_, key, err = ed25519.GenerateKey(rand.Reader)
}
if err != nil {
return err
}
// Calculate the validity dates according to given values and current time.
var notBefore, notAfter time.Time
if c.NotBefore != nil {
notBefore = *c.NotBefore
} else {
notBefore = time.Now()
}
if c.NotAfter != nil {
notAfter = *c.NotAfter
} else {
notAfter = notBefore.UTC().Add(*c.Expires)
}
// Get subject name as pkix.Name.
// Validity is already ensured by calling default() so it is safe to ignore error.
name, _ := x500dn.ParseDN(c.Subject)
template := &x509.Certificate{
SerialNumber: c.SerialNumber,
Subject: *name,
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: c.KeyUsage,
ExtKeyUsage: c.ExtKeyUsage,
BasicConstraintsValid: *c.IsCA,
IsCA: *c.IsCA,
CRLDistributionPoints: c.CRLDistributionPoints,
}
for _, san := range c.SubjectAltNames {
switch {
case strings.HasPrefix(san, "DNS:"):
template.DNSNames = append(template.DNSNames, strings.TrimPrefix(san, "DNS:"))
case strings.HasPrefix(san, "URI:"):
uri, err := url.Parse(strings.TrimPrefix(san, "URI:"))
if err != nil {
return err
}
template.URIs = append(template.URIs, uri)
case strings.HasPrefix(san, "IP:"):
ip := net.ParseIP(strings.TrimPrefix(san, "IP:"))
if ip == nil {
return fmt.Errorf("invalid IP address: %s", strings.TrimPrefix(san, "IP:"))
}
template.IPAddresses = append(template.IPAddresses, ip)
default:
return fmt.Errorf("unknown san, forgot prefix? (must be one of DNS:|URI:|IP:): %s", san)
}
}
var issuerCert *x509.Certificate
var issuerKey crypto.Signer
var chain [][]byte
if c.Issuer != nil {
issuerCert, err = x509.ParseCertificate(c.Issuer.GeneratedCert.Certificate[0])
if err != nil {
return nil
}
issuerKey = c.Issuer.GeneratedCert.PrivateKey.(crypto.Signer)
// Add certificate chain to end-entity certificates.
if !*c.IsCA {
issuer := c.Issuer
for issuer != nil {
// Add issuer to chain unless it is root certificate.
if issuer.Issuer != nil {
chain = append(chain, issuer.GeneratedCert.Certificate[0])
}
issuer = issuer.Issuer
}
}
} else {
// create self-signed certificate
issuerCert = template
issuerKey = key
}
var cert []byte
cert, err = x509.CreateCertificate(rand.Reader, template, issuerCert, key.Public(), issuerKey)
if err != nil {
return nil
}
c.GeneratedCert = &tls.Certificate{
Certificate: append([][]byte{cert}, chain...),
PrivateKey: key,
}
return nil
}
func encodeToPEMBlocks(blockType string, blocks [][]byte) ([]byte, error) {
var buf bytes.Buffer
for _, b := range blocks {
err := pem.Encode(&buf, &pem.Block{
Type: blockType,
Bytes: b,
})
if err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}