-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
manager.go
322 lines (287 loc) · 10.6 KB
/
manager.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
/*
Copyright 2022 Gravitational, 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 keystore
import (
"bytes"
"context"
"crypto"
"crypto/x509/pkix"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
)
// Manager provides an interface to interact with teleport CA private keys,
// which may be software keys or held in an HSM or other key manager.
type Manager struct {
backend
}
// RSAKeyOptions configure options for RSA key generation.
type RSAKeyOptions struct {
DigestAlgorithm crypto.Hash
}
// RSAKeyOption is a functional option for RSA key generation.
type RSAKeyOption func(*RSAKeyOptions)
func WithDigestAlgorithm(alg crypto.Hash) RSAKeyOption {
return func(opts *RSAKeyOptions) {
opts.DigestAlgorithm = alg
}
}
// backend is an interface that holds private keys and provides signing
// operations.
type backend interface {
// DeleteUnusedKeys deletes all keys from the KeyStore if they are:
// 1. Labeled by this KeyStore when they were created
// 2. Not included in the argument activeKeys
DeleteUnusedKeys(ctx context.Context, activeKeys [][]byte) error
// generateRSA creates a new RSA private key and returns its identifier and
// a crypto.Signer. The returned identifier can be passed to getSigner
// later to get the same crypto.Signer.
generateRSA(context.Context, ...RSAKeyOption) (keyID []byte, signer crypto.Signer, err error)
// getSigner returns a crypto.Signer for the given key identifier, if it is found.
getSigner(ctx context.Context, keyID []byte) (crypto.Signer, error)
// deleteKey deletes the given key from the KeyStore.
deleteKey(ctx context.Context, keyID []byte) error
// canSignWithKey returns true if this KeyStore is able to sign with the
// given key.
canSignWithKey(ctx context.Context, raw []byte, keyType types.PrivateKeyType) (bool, error)
}
// Config holds configuration parameters for the keystore. A software keystore
// will be the default if no other is configured. Only one inner config other
// than Softare should be set. It is okay to always set the Software config even
// when a different keystore is desired because it will only be used if all
// others are empty.
type Config struct {
// Software holds configuration parameters specific to software keystores.
Software SoftwareConfig
// PKCS11 holds configuration parameters specific to PKCS#11 keystores.
PKCS11 PKCS11Config
// GCPKMS holds configuration parameters specific to GCP KMS keystores.
GCPKMS GCPKMSConfig
// Logger is a logger to be used by the keystore.
Logger logrus.FieldLogger
}
func (cfg *Config) CheckAndSetDefaults() error {
// We check for mutual exclusion when parsing the file config.
if (cfg.PKCS11 != PKCS11Config{}) {
return trace.Wrap(cfg.PKCS11.CheckAndSetDefaults())
}
if (cfg.GCPKMS != GCPKMSConfig{}) {
return trace.Wrap(cfg.GCPKMS.CheckAndSetDefaults())
}
return trace.Wrap(cfg.Software.CheckAndSetDefaults())
}
// NewManager returns a new keystore Manager
func NewManager(ctx context.Context, cfg Config) (*Manager, error) {
if err := cfg.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
logger := cfg.Logger
if logger == nil {
logger = logrus.StandardLogger()
}
if (cfg.PKCS11 != PKCS11Config{}) {
backend, err := newPKCS11KeyStore(&cfg.PKCS11, logger)
return &Manager{backend: backend}, trace.Wrap(err)
}
if (cfg.GCPKMS != GCPKMSConfig{}) {
backend, err := newGCPKMSKeyStore(ctx, &cfg.GCPKMS, logger)
return &Manager{backend: backend}, trace.Wrap(err)
}
return &Manager{backend: newSoftwareKeyStore(&cfg.Software, logger)}, nil
}
// GetSSHSigner selects a usable SSH keypair from the given CA ActiveKeys and
// returns an [ssh.Signer].
func (m *Manager) GetSSHSigner(ctx context.Context, ca types.CertAuthority) (ssh.Signer, error) {
signer, err := m.getSSHSigner(ctx, ca.GetActiveKeys())
return signer, trace.Wrap(err)
}
// GetSSHSigner selects a usable SSH keypair from the given CA
// AdditionalTrustedKeys and returns an [ssh.Signer].
func (m *Manager) GetAdditionalTrustedSSHSigner(ctx context.Context, ca types.CertAuthority) (ssh.Signer, error) {
signer, err := m.getSSHSigner(ctx, ca.GetAdditionalTrustedKeys())
return signer, trace.Wrap(err)
}
func (m *Manager) getSSHSigner(ctx context.Context, keySet types.CAKeySet) (ssh.Signer, error) {
for _, keyPair := range keySet.SSH {
canSign, err := m.backend.canSignWithKey(ctx, keyPair.PrivateKey, keyPair.PrivateKeyType)
if err != nil {
return nil, trace.Wrap(err)
}
if !canSign {
continue
}
signer, err := m.backend.getSigner(ctx, keyPair.PrivateKey)
if err != nil {
return nil, trace.Wrap(err)
}
sshSigner, err := ssh.NewSignerFromSigner(signer)
return sshSigner, trace.Wrap(err)
}
return nil, trace.NotFound("no usable SSH key pairs found")
}
// GetTLSCertAndSigner selects a usable TLS keypair from the given CA
// and returns the PEM-encoded TLS certificate and a [crypto.Signer].
func (m *Manager) GetTLSCertAndSigner(ctx context.Context, ca types.CertAuthority) ([]byte, crypto.Signer, error) {
cert, signer, err := m.getTLSCertAndSigner(ctx, ca.GetActiveKeys())
return cert, signer, trace.Wrap(err)
}
// GetAdditionalTrustedTLSCertAndSigner selects a usable TLS keypair from the given CA
// and returns the PEM-encoded TLS certificate and a [crypto.Signer].
func (m *Manager) GetAdditionalTrustedTLSCertAndSigner(ctx context.Context, ca types.CertAuthority) ([]byte, crypto.Signer, error) {
cert, signer, err := m.getTLSCertAndSigner(ctx, ca.GetAdditionalTrustedKeys())
return cert, signer, trace.Wrap(err)
}
func (m *Manager) getTLSCertAndSigner(ctx context.Context, keySet types.CAKeySet) ([]byte, crypto.Signer, error) {
for _, keyPair := range keySet.TLS {
canSign, err := m.backend.canSignWithKey(ctx, keyPair.Key, keyPair.KeyType)
if err != nil {
return nil, nil, trace.Wrap(err)
}
if !canSign {
continue
}
signer, err := m.backend.getSigner(ctx, keyPair.Key)
if err != nil {
return nil, nil, trace.Wrap(err)
}
return keyPair.Cert, signer, nil
}
return nil, nil, trace.NotFound("no usable TLS key pairs found")
}
// GetJWTSigner selects a usable JWT keypair from the given keySet and returns
// a [crypto.Signer].
func (m *Manager) GetJWTSigner(ctx context.Context, ca types.CertAuthority) (crypto.Signer, error) {
for _, keyPair := range ca.GetActiveKeys().JWT {
canSign, err := m.backend.canSignWithKey(ctx, keyPair.PrivateKey, keyPair.PrivateKeyType)
if err != nil {
return nil, trace.Wrap(err)
}
if !canSign {
continue
}
signer, err := m.backend.getSigner(ctx, keyPair.PrivateKey)
return signer, trace.Wrap(err)
}
return nil, trace.NotFound("no usable JWT key pairs found")
}
// NewSSHKeyPair generates a new SSH keypair in the keystore backend and returns it.
func (m *Manager) NewSSHKeyPair(ctx context.Context) (*types.SSHKeyPair, error) {
// The default hash length for SSH signers is 512 bits.
sshKey, cryptoSigner, err := m.backend.generateRSA(ctx, WithDigestAlgorithm(crypto.SHA512))
if err != nil {
return nil, trace.Wrap(err)
}
sshSigner, err := ssh.NewSignerFromSigner(cryptoSigner)
if err != nil {
return nil, trace.Wrap(err)
}
publicKey := ssh.MarshalAuthorizedKey(sshSigner.PublicKey())
return &types.SSHKeyPair{
PublicKey: publicKey,
PrivateKey: sshKey,
PrivateKeyType: keyType(sshKey),
}, nil
}
// NewTLSKeyPair creates a new TLS keypair in the keystore backend and returns it.
func (m *Manager) NewTLSKeyPair(ctx context.Context, clusterName string) (*types.TLSKeyPair, error) {
tlsKey, signer, err := m.backend.generateRSA(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
tlsCert, err := tlsca.GenerateSelfSignedCAWithSigner(
signer,
pkix.Name{
CommonName: clusterName,
Organization: []string{clusterName},
}, nil, defaults.CATTL)
if err != nil {
return nil, trace.Wrap(err)
}
return &types.TLSKeyPair{
Cert: tlsCert,
Key: tlsKey,
KeyType: keyType(tlsKey),
}, nil
}
// New JWTKeyPair create a new JWT keypair in the keystore backend and returns
// it.
func (m *Manager) NewJWTKeyPair(ctx context.Context) (*types.JWTKeyPair, error) {
jwtKey, signer, err := m.backend.generateRSA(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
publicKey, err := utils.MarshalPublicKey(signer)
if err != nil {
return nil, trace.Wrap(err)
}
return &types.JWTKeyPair{
PublicKey: publicKey,
PrivateKey: jwtKey,
PrivateKeyType: keyType(jwtKey),
}, nil
}
// HasUsableActiveKeys returns true if the given CA has any usable active keys.
func (m *Manager) HasUsableActiveKeys(ctx context.Context, ca types.CertAuthority) (bool, error) {
usable, err := m.hasUsableKeys(ctx, ca.GetActiveKeys())
return usable, trace.Wrap(err)
}
// HasUsableActiveKeys returns true if the given CA has any usable additional
// trusted keys.
func (m *Manager) HasUsableAdditionalKeys(ctx context.Context, ca types.CertAuthority) (bool, error) {
usable, err := m.hasUsableKeys(ctx, ca.GetAdditionalTrustedKeys())
return usable, trace.Wrap(err)
}
func (m *Manager) hasUsableKeys(ctx context.Context, keySet types.CAKeySet) (bool, error) {
for _, sshKeyPair := range keySet.SSH {
usable, err := m.backend.canSignWithKey(ctx, sshKeyPair.PrivateKey, sshKeyPair.PrivateKeyType)
if err != nil {
return false, trace.Wrap(err)
}
if usable {
return true, nil
}
}
for _, tlsKeyPair := range keySet.TLS {
usable, err := m.backend.canSignWithKey(ctx, tlsKeyPair.Key, tlsKeyPair.KeyType)
if err != nil {
return false, trace.Wrap(err)
}
if usable {
return true, nil
}
}
for _, jwtKeyPair := range keySet.JWT {
usable, err := m.backend.canSignWithKey(ctx, jwtKeyPair.PrivateKey, jwtKeyPair.PrivateKeyType)
if err != nil {
return false, trace.Wrap(err)
}
if usable {
return true, nil
}
}
return false, nil
}
// keyType returns the type of the given private key.
func keyType(key []byte) types.PrivateKeyType {
if bytes.HasPrefix(key, pkcs11Prefix) {
return types.PrivateKeyType_PKCS11
}
if bytes.HasPrefix(key, []byte(gcpkmsPrefix)) {
return types.PrivateKeyType_GCP_KMS
}
return types.PrivateKeyType_RAW
}