-
Notifications
You must be signed in to change notification settings - Fork 189
/
pki.go
271 lines (231 loc) · 6.35 KB
/
pki.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
package tls
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"math/big"
"time"
)
// Implementation based on https://github.com/rancher-sandbox/opni-opensearch-operator/blob/main/pkg/resources/opensearch/certs/certs.go
// and https://github.com/rancher-sandbox/opni-opensearch-operator/blob/main/pkg/pki/pki.go
type PKI interface {
GenerateCA(name string) (ca Cert, err error)
CAFromSecret(data map[string][]byte) Cert
}
type Cert interface {
SecretDataCA() map[string][]byte
SecretData(ca Cert) map[string][]byte
KeyData() []byte
CertData() []byte
CreateAndSignCertificate(commonName string, orgUnit string, dnsnames []string) (cert Cert, err error)
}
type CertValidater interface {
IsExpiringSoon() bool
IsSignedByCA(ca Cert) (bool, error)
}
// Dummy struct so that PKI interface can be implemented for easier mocking in tests
type PkiImpl struct {
}
func NewPKI() PKI {
return &PkiImpl{}
}
// Represents a certificate with key
type PEMCert struct {
certBytes []byte
keyBytes []byte
}
func (pki *PkiImpl) GenerateCA(name string) (ca Cert, err error) {
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
return
}
caCertTemplate := &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
CommonName: name,
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
IsCA: true,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
BasicConstraintsValid: true,
}
caPrivateKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return
}
caBytes, err := x509.CreateCertificate(rand.Reader, caCertTemplate, caCertTemplate, &caPrivateKey.PublicKey, caPrivateKey)
if err != nil {
return
}
caPEM := new(bytes.Buffer)
err = pem.Encode(caPEM, &pem.Block{
Type: "CERTIFICATE",
Bytes: caBytes,
})
if err != nil {
return
}
caKeyPEM := new(bytes.Buffer)
err = pem.Encode(caKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(caPrivateKey),
})
if err != nil {
return
}
return &PEMCert{certBytes: caPEM.Bytes(), keyBytes: caKeyPEM.Bytes()}, nil
}
func (cert *PEMCert) cert() (tls.Certificate, error) {
return tls.X509KeyPair(cert.certBytes, cert.keyBytes)
}
func (ca *PEMCert) SecretDataCA() map[string][]byte {
data := make(map[string][]byte)
data["ca.crt"] = ca.certBytes
data["ca.key"] = ca.keyBytes
return data
}
func (cert *PEMCert) SecretData(ca Cert) map[string][]byte {
data := make(map[string][]byte)
data["tls.crt"] = cert.certBytes
data["tls.key"] = cert.keyBytes
data["ca.crt"] = ca.CertData()
return data
}
func (cert *PEMCert) KeyData() []byte {
return cert.keyBytes
}
func (cert *PEMCert) CertData() []byte {
return cert.certBytes
}
func (ca *PEMCert) CreateAndSignCertificate(commonName string, orgUnit string, dnsnames []string) (cert Cert, err error) {
tlscacert, err := ca.cert()
if err != nil {
return
}
cacert, err := x509.ParseCertificate(tlscacert.Certificate[0])
if err != nil {
return
}
keypair, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return
}
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
return
}
x509cert := &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
CommonName: commonName,
OrganizationalUnit: []string{orgUnit},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(1, 0, 0),
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
}
if len(dnsnames) > 0 {
san, err := calculateExtension(commonName, dnsnames)
if err != nil {
return cert, err
}
x509cert.ExtraExtensions = []pkix.Extension{san}
}
signed, err := x509.CreateCertificate(rand.Reader, x509cert, cacert, &keypair.PublicKey, tlscacert.PrivateKey)
if err != nil {
return
}
certPEMBuffer := new(bytes.Buffer)
err = pem.Encode(certPEMBuffer, &pem.Block{
Type: "CERTIFICATE",
Bytes: signed,
})
if err != nil {
return
}
certBytes := certPEMBuffer.Bytes()
pkcs8key, err := x509.MarshalPKCS8PrivateKey(keypair)
if err != nil {
return
}
keyPEM := new(bytes.Buffer)
err = pem.Encode(keyPEM, &pem.Block{
Type: "PRIVATE KEY",
Bytes: pkcs8key,
})
if err != nil {
return
}
keyBytes := keyPEM.Bytes()
return &PEMCert{keyBytes: keyBytes, certBytes: certBytes}, nil
}
func (pki *PkiImpl) CAFromSecret(data map[string][]byte) Cert {
return &PEMCert{certBytes: data["ca.crt"], keyBytes: data["ca.key"]}
}
func calculateExtension(commonName string, dnsNames []string) (pkix.Extension, error) {
rawValues := []asn1.RawValue{
{FullBytes: []byte{0x88, 0x05, 0x2A, 0x03, 0x04, 0x05, 0x05}},
}
for _, name := range dnsNames {
rawValues = append(rawValues, asn1.RawValue{Tag: 2, Class: 2, Bytes: []byte(name)})
}
rawByte, err := asn1.Marshal(rawValues)
if err != nil {
return pkix.Extension{}, err
}
san := pkix.Extension{
Id: asn1.ObjectIdentifier{2, 5, 29, 17},
Critical: true,
Value: rawByte,
}
return san, nil
}
type implCertValidater struct {
implCertValidaterOptions
cert *x509.Certificate
}
type implCertValidaterOptions struct {
expiryThreshold time.Duration
}
type ImplCertValidaterOption func(*implCertValidaterOptions)
func (o *implCertValidaterOptions) apply(opts ...ImplCertValidaterOption) {
for _, opt := range opts {
opt(o)
}
}
func WithExpiryThreshold(expiryThreshold time.Duration) ImplCertValidaterOption {
return func(o *implCertValidaterOptions) {
o.expiryThreshold = expiryThreshold
}
}
func NewCertValidater(pemData []byte, opts ...ImplCertValidaterOption) (CertValidater, error) {
var o implCertValidaterOptions
o.apply(opts...)
block, _ := pem.Decode(pemData)
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
return &implCertValidater{
implCertValidaterOptions: o,
cert: cert,
}, nil
}
func (i *implCertValidater) IsExpiringSoon() bool {
return time.Now().After(i.cert.NotAfter.Add(i.expiryThreshold * -1))
}
func (i *implCertValidater) IsSignedByCA(ca Cert) (bool, error) {
block, _ := pem.Decode(ca.CertData())
caCert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return false, err
}
return bytes.Equal(i.cert.RawIssuer, caCert.RawSubject), nil
}