/
keycertbundle.go
339 lines (300 loc) · 10.5 KB
/
keycertbundle.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
// Copyright Istio 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.
// Provides utility methods to generate X.509 certificates with different
// options. This implementation is Largely inspired from
// https://golang.org/src/crypto/tls/generate_cert.go.
package util
import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"os"
"sync"
"time"
)
// KeyCertBundle stores the cert, private key, cert chain and root cert for an entity. It is thread safe.
// The cert and privKey should be a public/private key pair.
// The cert should be verifiable from the rootCert through the certChain.
// cert and priveKey are pointers to the cert/key parsed from certBytes/privKeyBytes.
type KeyCertBundle struct {
certBytes []byte
cert *x509.Certificate
privKeyBytes []byte
privKey *crypto.PrivateKey
certChainBytes []byte
rootCertBytes []byte
// mutex protects the R/W to all keys and certs.
mutex sync.RWMutex
}
// NewKeyCertBundleFromPem returns a new KeyCertBundle, regardless of whether or not the key can be correctly parsed.
func NewKeyCertBundleFromPem(certBytes, privKeyBytes, certChainBytes, rootCertBytes []byte) *KeyCertBundle {
bundle := &KeyCertBundle{}
bundle.setAllFromPem(certBytes, privKeyBytes, certChainBytes, rootCertBytes)
return bundle
}
// NewVerifiedKeyCertBundleFromPem returns a new KeyCertBundle, or error if the provided certs failed the
// verification.
func NewVerifiedKeyCertBundleFromPem(certBytes, privKeyBytes, certChainBytes, rootCertBytes []byte) (
*KeyCertBundle, error,
) {
bundle := &KeyCertBundle{}
if err := bundle.VerifyAndSetAll(certBytes, privKeyBytes, certChainBytes, rootCertBytes); err != nil {
return nil, err
}
return bundle, nil
}
// NewVerifiedKeyCertBundleFromFile returns a new KeyCertBundle, or error if the provided certs failed the
// verification.
func NewVerifiedKeyCertBundleFromFile(certFile string, privKeyFile string, certChainFiles []string, rootCertFile string) (
*KeyCertBundle, error,
) {
certBytes, err := os.ReadFile(certFile)
if err != nil {
return nil, err
}
privKeyBytes, err := os.ReadFile(privKeyFile)
if err != nil {
return nil, err
}
var certChainBytes []byte
if len(certChainFiles) > 0 {
for _, f := range certChainFiles {
var b []byte
if b, err = os.ReadFile(f); err != nil {
return nil, err
}
certChainBytes = append(certChainBytes, b...)
}
}
rootCertBytes, err := os.ReadFile(rootCertFile)
if err != nil {
return nil, err
}
return NewVerifiedKeyCertBundleFromPem(certBytes, privKeyBytes, certChainBytes, rootCertBytes)
}
// NewKeyCertBundleWithRootCertFromFile returns a new KeyCertBundle with the root cert without verification.
func NewKeyCertBundleWithRootCertFromFile(rootCertFile string) (*KeyCertBundle, error) {
var rootCertBytes []byte
var err error
if rootCertFile == "" {
rootCertBytes = []byte{}
} else {
rootCertBytes, err = os.ReadFile(rootCertFile)
if err != nil {
return nil, err
}
}
return &KeyCertBundle{
certBytes: []byte{},
cert: nil,
privKeyBytes: []byte{},
privKey: nil,
certChainBytes: []byte{},
rootCertBytes: rootCertBytes,
}, nil
}
// GetAllPem returns all key/cert PEMs in KeyCertBundle together. Getting all values together avoids inconsistency.
func (b *KeyCertBundle) GetAllPem() (certBytes, privKeyBytes, certChainBytes, rootCertBytes []byte) {
b.mutex.RLock()
certBytes = copyBytes(b.certBytes)
privKeyBytes = copyBytes(b.privKeyBytes)
certChainBytes = copyBytes(b.certChainBytes)
rootCertBytes = copyBytes(b.rootCertBytes)
b.mutex.RUnlock()
return
}
// GetAll returns all key/cert in KeyCertBundle together. Getting all values together avoids inconsistency.
// NOTE: Callers should not modify the content of cert and privKey.
func (b *KeyCertBundle) GetAll() (cert *x509.Certificate, privKey *crypto.PrivateKey, certChainBytes,
rootCertBytes []byte,
) {
b.mutex.RLock()
cert = b.cert
privKey = b.privKey
certChainBytes = copyBytes(b.certChainBytes)
rootCertBytes = copyBytes(b.rootCertBytes)
b.mutex.RUnlock()
return
}
// GetCertChainPem returns the certificate chain PEM.
func (b *KeyCertBundle) GetCertChainPem() []byte {
b.mutex.RLock()
defer b.mutex.RUnlock()
return copyBytes(b.certChainBytes)
}
// GetRootCertPem returns the root certificate PEM.
func (b *KeyCertBundle) GetRootCertPem() []byte {
b.mutex.RLock()
defer b.mutex.RUnlock()
return copyBytes(b.rootCertBytes)
}
// VerifyAndSetAll verifies the key/certs, and sets all key/certs in KeyCertBundle together.
// Setting all values together avoids inconsistency.
func (b *KeyCertBundle) VerifyAndSetAll(certBytes, privKeyBytes, certChainBytes, rootCertBytes []byte) error {
if err := Verify(certBytes, privKeyBytes, certChainBytes, rootCertBytes); err != nil {
return err
}
b.setAllFromPem(certBytes, privKeyBytes, certChainBytes, rootCertBytes)
return nil
}
// Setting all values together avoids inconsistency.
func (b *KeyCertBundle) setAllFromPem(certBytes, privKeyBytes, certChainBytes, rootCertBytes []byte) {
b.mutex.Lock()
b.certBytes = copyBytes(certBytes)
b.privKeyBytes = copyBytes(privKeyBytes)
b.certChainBytes = copyBytes(certChainBytes)
b.rootCertBytes = copyBytes(rootCertBytes)
// cert and privKey are always reset to point to new addresses. This avoids modifying the pointed structs that
// could be still used outside of the class.
b.cert, _ = ParsePemEncodedCertificate(certBytes)
privKey, _ := ParsePemEncodedKey(privKeyBytes)
b.privKey = &privKey
b.mutex.Unlock()
}
// CertOptions returns the certificate config based on currently stored cert.
func (b *KeyCertBundle) CertOptions() (*CertOptions, error) {
b.mutex.RLock()
defer b.mutex.RUnlock()
ids, err := ExtractIDs(b.cert.Extensions)
if err != nil {
return nil, fmt.Errorf("failed to extract id %v", err)
}
if len(ids) != 1 {
return nil, fmt.Errorf("expect single id from the cert, found %v", ids)
}
opts := &CertOptions{
Host: ids[0],
Org: b.cert.Issuer.Organization[0],
IsCA: b.cert.IsCA,
TTL: b.cert.NotAfter.Sub(b.cert.NotBefore),
IsDualUse: ids[0] == b.cert.Subject.CommonName,
}
switch (*b.privKey).(type) {
case *rsa.PrivateKey:
size, err := GetRSAKeySize(*b.privKey)
if err != nil {
return nil, fmt.Errorf("failed to get RSA key size: %v", err)
}
opts.RSAKeySize = size
case *ecdsa.PrivateKey:
opts.ECSigAlg = EcdsaSigAlg
default:
return nil, errors.New("unknown private key type")
}
return opts, nil
}
// UpdateVerifiedKeyCertBundleFromFile Verifies and updates KeyCertBundle with new certs
func (b *KeyCertBundle) UpdateVerifiedKeyCertBundleFromFile(certFile string, privKeyFile string, certChainFiles []string, rootCertFile string) error {
certBytes, err := os.ReadFile(certFile)
if err != nil {
return err
}
privKeyBytes, err := os.ReadFile(privKeyFile)
if err != nil {
return err
}
certChainBytes := []byte{}
if len(certChainFiles) != 0 {
for _, f := range certChainFiles {
var b []byte
if b, err = os.ReadFile(f); err != nil {
return err
}
certChainBytes = append(certChainBytes, b...)
}
}
rootCertBytes, err := os.ReadFile(rootCertFile)
if err != nil {
return err
}
err = b.VerifyAndSetAll(certBytes, privKeyBytes, certChainBytes, rootCertBytes)
if err != nil {
return err
}
return nil
}
// ExtractRootCertExpiryTimestamp returns the unix timestamp when the root becomes expires.
func (b *KeyCertBundle) ExtractRootCertExpiryTimestamp() (float64, error) {
return extractCertExpiryTimestamp("root cert", b.GetRootCertPem())
}
// ExtractCACertExpiryTimestamp returns the unix timestamp when the cert chain becomes expires.
func (b *KeyCertBundle) ExtractCACertExpiryTimestamp() (float64, error) {
return extractCertExpiryTimestamp("CA cert", b.GetCertChainPem())
}
// TimeBeforeCertExpires returns the time duration before the cert gets expired.
// It returns an error if it failed to extract the cert expiration timestamp.
// The returned time duration could be a negative value indicating the cert has already been expired.
func TimeBeforeCertExpires(certBytes []byte, now time.Time) (time.Duration, error) {
if len(certBytes) == 0 {
return 0, fmt.Errorf("no certificate found")
}
certExpiryTimestamp, err := extractCertExpiryTimestamp("cert", certBytes)
if err != nil {
return 0, fmt.Errorf("failed to extract cert expiration timestamp: %v", err)
}
certExpiry := time.Duration(certExpiryTimestamp-float64(now.Unix())) * time.Second
return certExpiry, nil
}
// Verify that the cert chain, root cert and key/cert match.
func Verify(certBytes, privKeyBytes, certChainBytes, rootCertBytes []byte) error {
// Verify the cert can be verified from the root cert through the cert chain.
rcp := x509.NewCertPool()
rcp.AppendCertsFromPEM(rootCertBytes)
icp := x509.NewCertPool()
icp.AppendCertsFromPEM(certChainBytes)
opts := x509.VerifyOptions{
Intermediates: icp,
Roots: rcp,
}
cert, err := ParsePemEncodedCertificate(certBytes)
if err != nil {
return fmt.Errorf("failed to parse cert PEM: %v", err)
}
chains, err := cert.Verify(opts)
if len(chains) == 0 || err != nil {
return fmt.Errorf(
"cannot verify the cert with the provided root chain and cert "+
"pool with error: %v", err)
}
// Verify that the key can be correctly parsed.
if _, err = ParsePemEncodedKey(privKeyBytes); err != nil {
return fmt.Errorf("failed to parse private key PEM: %v", err)
}
// Verify the cert and key match.
if _, err := tls.X509KeyPair(certBytes, privKeyBytes); err != nil {
return fmt.Errorf("the cert does not match the key: %v", err)
}
return nil
}
func extractCertExpiryTimestamp(certType string, certPem []byte) (float64, error) {
cert, err := ParsePemEncodedCertificate(certPem)
if err != nil {
return -1, fmt.Errorf("failed to parse the %s: %v", certType, err)
}
end := cert.NotAfter
expiryTimestamp := float64(end.Unix())
if end.Before(time.Now()) {
return expiryTimestamp, fmt.Errorf("expired %s found, x509.NotAfter %v, please transit your %s", certType, end, certType)
}
return expiryTimestamp, nil
}
func copyBytes(src []byte) []byte {
bs := make([]byte, len(src))
copy(bs, src)
return bs
}