-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
pkcs11.go
321 lines (291 loc) · 9.39 KB
/
pkcs11.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
/*
Copyright 2021 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 (
"context"
"crypto"
"crypto/rsa"
"encoding/json"
"github.com/ThalesIgnite/crypto11"
"github.com/google/uuid"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/types"
)
var pkcs11Prefix = []byte("pkcs11:")
// PKCS11Config is used to pass PKCS11 HSM client configuration parameters.
type PKCS11Config struct {
// Path is the path to the PKCS11 module.
Path string
// SlotNumber is the PKCS11 slot to use.
SlotNumber *int
// TokenLabel is the label of the PKCS11 token to use.
TokenLabel string
// Pin is the PKCS11 pin for the given token.
Pin string
// HostUUID is the UUID of the local auth server this HSM is connected to.
HostUUID string
}
func (cfg *PKCS11Config) CheckAndSetDefaults() error {
if cfg.SlotNumber == nil && cfg.TokenLabel == "" {
return trace.BadParameter("must provide one of SlotNumber or TokenLabel")
}
if cfg.HostUUID == "" {
return trace.BadParameter("must provide HostUUID")
}
return nil
}
type pkcs11KeyStore struct {
ctx *crypto11.Context
hostUUID string
log logrus.FieldLogger
}
func newPKCS11KeyStore(config *PKCS11Config, logger logrus.FieldLogger) (*pkcs11KeyStore, error) {
cryptoConfig := &crypto11.Config{
Path: config.Path,
TokenLabel: config.TokenLabel,
SlotNumber: config.SlotNumber,
Pin: config.Pin,
}
ctx, err := crypto11.Configure(cryptoConfig)
if err != nil {
return nil, trace.Wrap(err)
}
logger = logger.WithFields(logrus.Fields{trace.Component: "PKCS11KeyStore"})
return &pkcs11KeyStore{
ctx: ctx,
hostUUID: config.HostUUID,
log: logger,
}, nil
}
func (p *pkcs11KeyStore) findUnusedID() (uuid.UUID, error) {
var id uuid.UUID
var err error
// Some HSMs (like YubiHSM2) will silently truncate the passed ID to as few
// as 2 bytes. There's not a great way to detect this and I don't want to
// limit the ID to 2 bytes on all systems, so for now we will generate a
// few random IDs and hope to avoid a collision. Ideally Teleport should be
// the only thing creating keys for this token and there should only be 10
// keys per HSM at a given time:
// 2(rotation phases) * (4(SSH and TLS for User and Host CA) + 1(JWT CA))
maxIterations := 16
iterations := 0
for ; iterations < maxIterations; iterations++ {
id, err = uuid.NewRandom()
if err != nil {
return id, trace.Wrap(err)
}
existingSigner, err := p.ctx.FindKeyPair(id[:], []byte(p.hostUUID))
if err != nil {
return id, trace.Wrap(err)
}
if existingSigner == nil {
// failed to find an existing keypair, so this ID is unique
break
} else {
p.log.Warn("Found CKA_ID collision while creating keypair, retrying with new ID")
}
}
if iterations == maxIterations {
return id, trace.AlreadyExists("failed to find unused CKA_ID for HSM")
}
return id, nil
}
// 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.
func (p *pkcs11KeyStore) generateRSA(ctx context.Context, options ...RSAKeyOption) ([]byte, crypto.Signer, error) {
p.log.Debug("Creating new HSM keypair")
id, err := p.findUnusedID()
if err != nil {
return nil, nil, trace.Wrap(err)
}
signer, err := p.ctx.GenerateRSAKeyPairWithLabel(id[:], []byte(p.hostUUID), constants.RSAKeySize)
if err != nil {
return nil, nil, trace.Wrap(err)
}
key := keyID{
HostID: p.hostUUID,
KeyID: id.String(),
}
keyID, err := key.marshal()
if err != nil {
return nil, nil, trace.Wrap(err)
}
return keyID, signer, nil
}
// getSigner returns a crypto.Signer for the given key identifier, if it is found.
func (p *pkcs11KeyStore) getSigner(ctx context.Context, rawKey []byte) (crypto.Signer, error) {
if t := keyType(rawKey); t != types.PrivateKeyType_PKCS11 {
return nil, trace.BadParameter("pkcs11KeyStore cannot get signer for key type %s", t.String())
}
keyID, err := parseKeyID(rawKey)
if err != nil {
return nil, trace.Wrap(err)
}
if keyID.HostID != p.hostUUID {
return nil, trace.NotFound("given pkcs11 key is for host: %q, but this host is: %q", keyID.HostID, p.hostUUID)
}
pkcs11ID, err := keyID.pkcs11Key()
if err != nil {
return nil, trace.Wrap(err)
}
signer, err := p.ctx.FindKeyPair(pkcs11ID, []byte(p.hostUUID))
if err != nil {
return nil, trace.Wrap(err)
}
if signer == nil {
return nil, trace.NotFound("failed to find keypair for given id")
}
return signer, nil
}
// canSignWithKey returns true if the given key is PKCS11 and was created by
// this host. If the HSM is disconnected or the key material has been deleted
// the error will not be detected here but when the first signature is
// attempted.
func (p *pkcs11KeyStore) canSignWithKey(ctx context.Context, raw []byte, keyType types.PrivateKeyType) (bool, error) {
if keyType != types.PrivateKeyType_PKCS11 {
return false, nil
}
keyID, err := parseKeyID(raw)
if err != nil {
return false, trace.Wrap(err)
}
return keyID.HostID == p.hostUUID, nil
}
// deleteKey deletes the given key from the HSM
func (p *pkcs11KeyStore) deleteKey(_ context.Context, rawKey []byte) error {
keyID, err := parseKeyID(rawKey)
if err != nil {
return trace.Wrap(err)
}
if keyID.HostID != p.hostUUID {
return trace.NotFound("pkcs11 key is for different host")
}
pkcs11ID, err := keyID.pkcs11Key()
if err != nil {
return trace.Wrap(err)
}
signer, err := p.ctx.FindKeyPair(pkcs11ID, []byte(p.hostUUID))
if err != nil {
return trace.Wrap(err)
}
if signer == nil {
return trace.NotFound("failed to find keypair for given id")
}
return trace.Wrap(signer.Delete())
}
// DeleteUnusedKeys deletes all keys from the KeyStore if they are:
// 1. Labeled with the local HostUUID when they were created
// 2. Not included in the argument activeKeys
// This is meant to delete unused keys after they have been rotated out by a CA
// rotation.
func (p *pkcs11KeyStore) DeleteUnusedKeys(ctx context.Context, activeKeys [][]byte) error {
p.log.Debug("Deleting unused keys from HSM")
// It's necessary to fetch all PublicKeys for the known activeKeys in order to
// compare with the signers returned by FindKeyPairs below. We have no way
// to find the CKA_ID of an unused key if it is not known.
var activePublicKeys []*rsa.PublicKey
for _, activeKey := range activeKeys {
if keyType(activeKey) != types.PrivateKeyType_PKCS11 {
continue
}
keyID, err := parseKeyID(activeKey)
if err != nil {
return trace.Wrap(err)
}
if keyID.HostID != p.hostUUID {
// This key was labeled with a foreign host UUID, it is likely not
// present on the attached HSM and definitely will not be returned
// by FindKeyPairs below which queries by host UUID.
continue
}
signer, err := p.getSigner(ctx, activeKey)
if trace.IsNotFound(err) {
// Failed to find a currently active key owned by this host.
// The cluster is in a bad state, refuse to delete any keys.
return trace.NotFound(
"cannot find currently active CA key %q in HSM, aborting attempt to delete unused keys",
keyID.KeyID)
}
if err != nil {
return trace.Wrap(err)
}
rsaPublicKey, ok := signer.Public().(*rsa.PublicKey)
if !ok {
return trace.BadParameter("unknown public key type: %T", signer.Public())
}
activePublicKeys = append(activePublicKeys, rsaPublicKey)
}
keyIsActive := func(signer crypto.Signer) bool {
rsaPublicKey, ok := signer.Public().(*rsa.PublicKey)
if !ok {
// unknown key type... we don't know what this is, so don't delete it
return true
}
for _, k := range activePublicKeys {
if rsaPublicKey.Equal(k) {
return true
}
}
return false
}
signers, err := p.ctx.FindKeyPairs(nil, []byte(p.hostUUID))
if err != nil {
return trace.Wrap(err)
}
for _, signer := range signers {
if keyIsActive(signer) {
continue
}
if err := signer.Delete(); err != nil {
// Key deletion is best-effort, log a warning on errors, and
// continue trying to delete other keys. Errors have been observed
// when FindKeyPairs returns duplicate keys.
p.log.Warnf("Failed deleting unused key from HSM: %v", err)
}
}
return nil
}
type keyID struct {
HostID string `json:"host_id"`
KeyID string `json:"key_id"`
}
func (k keyID) marshal() ([]byte, error) {
buf, err := json.Marshal(k)
if err != nil {
return nil, trace.Wrap(err)
}
buf = append(append([]byte{}, pkcs11Prefix...), buf...)
return buf, nil
}
func (k keyID) pkcs11Key() ([]byte, error) {
id, err := uuid.Parse(k.KeyID)
if err != nil {
return nil, trace.Wrap(err)
}
return id[:], nil
}
func parseKeyID(key []byte) (keyID, error) {
var keyID keyID
if keyType(key) != types.PrivateKeyType_PKCS11 {
return keyID, trace.BadParameter("unable to parse invalid pkcs11 key")
}
// strip pkcs11: prefix
key = key[len(pkcs11Prefix):]
if err := json.Unmarshal(key, &keyID); err != nil {
return keyID, trace.Wrap(err)
}
return keyID, nil
}