-
Notifications
You must be signed in to change notification settings - Fork 341
/
ca.go
196 lines (169 loc) · 5.33 KB
/
ca.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
/*
Copyright 2018 Heptio Inc.
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 ca
import (
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"net"
"sync"
"time"
"github.com/pkg/errors"
)
const (
rsaBits = 2048
validFor = 48 * time.Hour
caName = "sonobuoy-ca"
)
var (
pkixName = pkix.Name{
Organization: []string{"VMware"},
OrganizationalUnit: []string{"Sonobuoy"},
Country: []string{"USA"},
Locality: []string{"Palo Alto, CA"},
}
randReader = rand.Reader
)
// Authority represents a root certificate authority that can issues
// certificates to be used for Client certs.
// Sonobuoy issues every worker a client certificate
type Authority struct {
sync.Mutex
privKey *ecdsa.PrivateKey
cert *x509.Certificate
lastSerial *big.Int
}
// NewAuthority creates a new certificate authority. A new private key and root certificate will
// be generated but not returned.
func NewAuthority() (*Authority, error) {
privKey, err := ecdsa.GenerateKey(elliptic.P256(), randReader)
if err != nil {
return nil, errors.Wrap(err, "couldn't generate private key")
}
auth := &Authority{
privKey: privKey,
}
cert, err := auth.makeCert(privKey.Public(), func(cert *x509.Certificate) {
cert.IsCA = true
cert.KeyUsage = x509.KeyUsageCertSign
cert.Subject.CommonName = caName
})
if err != nil {
return nil, errors.Wrap(err, "couldn't create certificate authority root certificate")
}
auth.cert = cert
return auth, nil
}
// makeCert takes a public key and a function to mutate the certificate template with updated parameters
func (a *Authority) makeCert(pub crypto.PublicKey, mut func(*x509.Certificate)) (*x509.Certificate, error) {
serialNumber := a.nextSerial()
validFrom := time.Now()
tmpl := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkixName,
NotBefore: validFrom,
NotAfter: validFrom.Add(validFor),
KeyUsage: 0,
ExtKeyUsage: []x509.ExtKeyUsage{},
BasicConstraintsValid: true,
}
mut(&tmpl)
parent := a.cert
// NewAuthority case
if a.cert == nil {
parent = &tmpl
}
newDERCert, err := x509.CreateCertificate(randReader, &tmpl, parent, pub, a.privKey)
if err != nil {
return nil, errors.Wrap(err, "couldn't make new certificate")
}
cert, err := x509.ParseCertificate(newDERCert)
return cert, errors.Wrap(err, "couldn't re-parse created certificate")
}
func (a *Authority) makeLeafCert(mut func(*x509.Certificate)) (*tls.Certificate, error) {
privKey, err := ecdsa.GenerateKey(elliptic.P256(), randReader)
if err != nil {
return nil, errors.Wrap(err, "couldn't generate private key")
}
cert, err := a.makeCert(privKey.Public(), mut)
return &tls.Certificate{
Certificate: [][]byte{cert.Raw, a.cert.Raw},
PrivateKey: privKey,
Leaf: cert,
}, errors.Wrap(err, "couldn't make leaf cert")
}
func (a *Authority) nextSerial() *big.Int {
a.Lock()
defer a.Unlock()
if a.lastSerial == nil {
num := big.NewInt(1)
a.lastSerial = num
return num
}
// Make a copy
return a.lastSerial.Add(a.lastSerial, big.NewInt(1))
}
// CACert is the root certificate of the CA.
func (a *Authority) CACert() *x509.Certificate {
return a.cert
}
// CACertPool returns a CertPool prepopulated with the authority's certificate
func (a *Authority) CACertPool() *x509.CertPool {
pool := x509.NewCertPool()
pool.AddCert(a.CACert())
return pool
}
// ServerKeyPair makes a TLS server cert signed by our root CA. The returned certificate
// has a chain including the root CA cert.
func (a *Authority) ServerKeyPair(name string) (*tls.Certificate, error) {
cert, err := a.makeLeafCert(func(cert *x509.Certificate) {
cert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}
ip := net.ParseIP(name)
if ip != nil {
cert.IPAddresses = []net.IP{ip}
} else {
cert.DNSNames = []string{name}
}
})
return cert, errors.Wrap(err, "couldn't make server certificate")
}
// MakeServerConfig makes a new server certificate, then returns a TLS config that uses it
// and will verify peer certificates
func (a *Authority) MakeServerConfig(name string) (*tls.Config, error) {
cert, err := a.ServerKeyPair(name)
if err != nil {
return nil, err
}
pool := x509.NewCertPool()
pool.AddCert(a.cert)
return &tls.Config{
Certificates: []tls.Certificate{*cert},
ServerName: name,
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: pool,
}, nil
}
// ClientKeyPair makes a client cert signed by our root CA. The returned certificate
// has a chain including the root CA
func (a *Authority) ClientKeyPair(name string) (*tls.Certificate, error) {
cert, err := a.makeLeafCert(func(cert *x509.Certificate) {
cert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
cert.Subject.CommonName = name
})
return cert, errors.Wrap(err, "couldn't make client certificate")
}