forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
identity.go
359 lines (310 loc) · 11.9 KB
/
identity.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package identity
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"strings"
"github.com/gravitational/trace"
"github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/utils/keys"
apisshutils "github.com/gravitational/teleport/api/utils/sshutils"
"github.com/gravitational/teleport/lib/tbot/bot"
"github.com/gravitational/teleport/lib/tlsca"
)
const (
// TLSCertKey is the name under which TLS certificates exist in a destination.
TLSCertKey = "tlscert"
// SSHCertKey is the name under which SSH certificates exist in a destination.
SSHCertKey = "key-cert.pub"
// SSHCACertsKey is the name under which SSH CA certificates exist in a destination.
SSHCACertsKey = "sshcacerts"
// TLSCACertsKey is the name under which SSH CA certificates exist in a destination.
TLSCACertsKey = "tlscacerts"
// PrivateKeyKey is the name under which the private key exists in a destination.
// The same private key is used for SSH and TLS certificates.
PrivateKeyKey = "key"
// PublicKeyKey is the ssh public key, required for successful SSH connections.
PublicKeyKey = "key.pub"
// TokenHashKey is the key where a hash of the onboarding token will be stored.
TokenHashKey = "tokenhash"
// WriteTestKey is the key for a file used to check that the destination is
// writable.
WriteTestKey = ".write-test"
)
var log = logrus.WithFields(logrus.Fields{
trace.Component: teleport.ComponentTBot,
})
// Identity is collection of raw key and certificate data as well as the
// parsed equivalents that make up a Teleport identity.
type Identity struct {
// PrivateKeyBytes is a PEM encoded private key
PrivateKeyBytes []byte
// PublicKeyBytes contains bytes of the original SSH public key
PublicKeyBytes []byte
// CertBytes is a PEM encoded SSH host cert
CertBytes []byte
// TLSCertBytes is a PEM encoded TLS x509 client certificate
TLSCertBytes []byte
// TLSCACertBytes is a list of PEM encoded TLS x509 certificate of certificate authority
// associated with auth server services
TLSCACertsBytes [][]byte
// SSHCACertBytes is a list of SSH CAs encoded in the authorized_keys format.
SSHCACertBytes [][]byte
// TokenHashBytes is the hash of the original join token
TokenHashBytes []byte
// Below fields are "computed" by ReadIdentityFromStore - this essentially
// validates the raw data and saves these being continually recomputed.
// KeySigner is an SSH host certificate signer
KeySigner ssh.Signer
// SSHCert is a parsed SSH certificate
SSHCert *ssh.Certificate
// SSHHostCheckers holds the parsed SSH CAs
SSHHostCheckers []ssh.PublicKey
// X509Cert is the parsed X509 client certificate
X509Cert *x509.Certificate
// TLSCAPool is the parsed TLS CAs
TLSCAPool *x509.CertPool
// TLSCert is the parsed TLS client certificate
TLSCert *tls.Certificate
// ClusterName is a name of host's cluster determined from the
// x509 certificate.
ClusterName string
}
// LoadIdentityParams contains parameters beyond proto.Certs needed to load a
// stored identity.
type LoadIdentityParams struct {
PrivateKeyBytes []byte
PublicKeyBytes []byte
TokenHashBytes []byte
}
// Params returns the LoadIdentityParams for this Identity, which are the
// local-only parameters to be carried over to a renewed identity.
func (i *Identity) Params() *LoadIdentityParams {
return &LoadIdentityParams{
PrivateKeyBytes: i.PrivateKeyBytes,
PublicKeyBytes: i.PublicKeyBytes,
TokenHashBytes: i.TokenHashBytes,
}
}
// String returns user-friendly representation of the identity.
func (i *Identity) String() string {
var out []string
if i.X509Cert != nil {
out = append(out, fmt.Sprintf("cert(%v issued by %v:%v)", i.X509Cert.Subject.CommonName, i.X509Cert.Issuer.CommonName, i.X509Cert.Issuer.SerialNumber))
}
for j := range i.TLSCACertsBytes {
cert, err := tlsca.ParseCertificatePEM(i.TLSCACertsBytes[j])
if err != nil {
out = append(out, err.Error())
} else {
out = append(out, fmt.Sprintf("trust root(%v:%v)", cert.Subject.CommonName, cert.Subject.SerialNumber))
}
}
return fmt.Sprintf("Identity(%v)", strings.Join(out, ","))
}
// ReadIdentityFromStore reads stored identity credentials
func ReadIdentityFromStore(params *LoadIdentityParams, certs *proto.Certs) (*Identity, error) {
// Note: in practice we should always expect certificates to have all
// fields set even though destinations do not contain sufficient data to
// load a stored identity. This works in practice because we never read
// destination identities from disk and only read them from the result of
// `generateUserCerts`, which is always fully-formed.
switch {
case len(certs.SSH) == 0:
return nil, trace.BadParameter("identity requires SSH certificates but they are unset")
case len(params.PrivateKeyBytes) == 0:
return nil, trace.BadParameter("missing private key")
case len(certs.TLSCACerts) == 0 || len(certs.TLS) == 0:
return nil, trace.BadParameter("identity requires TLS certificates but they are empty")
}
sshHostCheckers, keySigner, sshCert, err := parseSSHIdentity(
params.PrivateKeyBytes, certs.SSH, certs.SSHCACerts,
)
if err != nil {
return nil, trace.Wrap(err, "parsing ssh identity")
}
clusterName, x509Cert, tlsCert, tlsCAPool, err := ParseTLSIdentity(
params.PrivateKeyBytes, certs.TLS, certs.TLSCACerts,
)
if err != nil {
return nil, trace.Wrap(err, "parsing tls identity")
}
return &Identity{
PublicKeyBytes: params.PublicKeyBytes,
PrivateKeyBytes: params.PrivateKeyBytes,
CertBytes: certs.SSH,
SSHCACertBytes: certs.SSHCACerts,
TLSCertBytes: certs.TLS,
TLSCACertsBytes: certs.TLSCACerts,
TokenHashBytes: params.TokenHashBytes,
// These fields are "computed"
ClusterName: clusterName,
KeySigner: keySigner,
SSHCert: sshCert,
SSHHostCheckers: sshHostCheckers,
X509Cert: x509Cert,
TLSCert: tlsCert,
TLSCAPool: tlsCAPool,
}, nil
}
// ParseTLSIdentity reads TLS identity from key pair
func ParseTLSIdentity(
keyBytes []byte, certBytes []byte, caCertsBytes [][]byte,
) (clusterName string, x509Cert *x509.Certificate, tlsCert *tls.Certificate, certPool *x509.CertPool, err error) {
x509Cert, err = tlsca.ParseCertificatePEM(certBytes)
if err != nil {
return "", nil, nil, nil, trace.Wrap(err, "parsing certificate")
}
if len(x509Cert.Issuer.Organization) == 0 {
return "", nil, nil, nil, trace.BadParameter("certificate missing CA organization")
}
clusterName = x509Cert.Issuer.Organization[0]
if clusterName == "" {
return "", nil, nil, nil, trace.BadParameter("certificate missing cluster name")
}
certPool = x509.NewCertPool()
for j := range caCertsBytes {
parsedCert, err := tlsca.ParseCertificatePEM(caCertsBytes[j])
if err != nil {
return "", nil, nil, nil, trace.Wrap(err, "parsing CA certificate")
}
certPool.AddCert(parsedCert)
}
cert, err := keys.X509KeyPair(certBytes, keyBytes)
if err != nil {
return "", nil, nil, nil, trace.Wrap(err, "parse private key")
}
return clusterName, x509Cert, &cert, certPool, nil
}
// parseSSHIdentity reads identity from initialized keypair
func parseSSHIdentity(
keyBytes, certBytes []byte, caBytes [][]byte,
) (hostCheckers []ssh.PublicKey, certSigner ssh.Signer, cert *ssh.Certificate, err error) {
cert, err = apisshutils.ParseCertificate(certBytes)
if err != nil {
return nil, nil, nil, trace.Wrap(err, "parsing certificate")
}
signer, err := ssh.ParsePrivateKey(keyBytes)
if err != nil {
return nil, nil, nil, trace.Wrap(err, "parsing key")
}
// this signer authenticates using certificate signed by the cert authority
// not only by the public key
certSigner, err = ssh.NewCertSigner(cert, signer)
if err != nil {
return nil, nil, nil, trace.Wrap(err, "creating signer from certificate and key")
}
// check principals on certificate
if len(cert.ValidPrincipals) < 1 {
return nil, nil, nil, trace.BadParameter("valid principals: at least one valid principal is required")
}
for _, validPrincipal := range cert.ValidPrincipals {
if validPrincipal == "" {
return nil, nil, nil, trace.BadParameter("valid principal can not be empty: %q", cert.ValidPrincipals)
}
}
hostCheckers, err = apisshutils.ParseAuthorizedKeys(caBytes)
if err != nil {
return nil, nil, nil, trace.Wrap(err, "parsing ca bytes")
}
return hostCheckers, certSigner, cert, nil
}
// VerifyWrite attempts to write to the .write-test artifact inside the given
// destination. It should be called before attempting a renewal to help ensure
// we won't then fail to save the identity.
func VerifyWrite(ctx context.Context, dest bot.Destination) error {
return trace.Wrap(dest.Write(ctx, WriteTestKey, []byte{}))
}
// ListKeys returns a list of artifact keys that will be written given a list
// of artifacts.
func ListKeys(kinds ...ArtifactKind) []string {
keys := []string{}
for _, artifact := range GetArtifacts() {
if !artifact.Matches(kinds...) {
continue
}
keys = append(keys, artifact.Key)
}
return keys
}
// SaveIdentity saves a bot identity to a destination.
func SaveIdentity(ctx context.Context, id *Identity, d bot.Destination, kinds ...ArtifactKind) error {
for _, artifact := range GetArtifacts() {
// Only store artifacts matching one of the set kinds.
if !artifact.Matches(kinds...) {
continue
}
data := artifact.ToBytes(id)
log.Debugf("Writing %s", artifact.Key)
if err := d.Write(ctx, artifact.Key, data); err != nil {
return trace.Wrap(err, "could not write to %v", artifact.Key)
}
}
return nil
}
// LoadIdentity loads a bot identity from a destination.
func LoadIdentity(ctx context.Context, d bot.Destination, kinds ...ArtifactKind) (*Identity, error) {
var certs proto.Certs
var params LoadIdentityParams
for _, artifact := range GetArtifacts() {
// Only attempt to load artifacts matching one of the specified kinds
if !artifact.Matches(kinds...) {
continue
}
data, err := d.Read(ctx, artifact.Key)
if err != nil {
return nil, trace.Wrap(err, "could not read artifact %q from destination %s", artifact.Key, d)
}
// Attempt to load from an old key if there was no data in the current
// key. This will be in the case as d.Read for the file destination will
// not throw an error if the file does not exist.
// This allows migrations of key names.
if artifact.OldKey != "" && len(data) == 0 {
log.Debugf(
"Unable to load from current key %q, trying to migrate from old key %q",
artifact.Key,
artifact.OldKey,
)
data, err = d.Read(ctx, artifact.OldKey)
if err != nil {
return nil, trace.Wrap(
err,
"could not read artifact %q from destination %q",
artifact.OldKey,
d,
)
}
}
// We generally expect artifacts to exist beforehand regardless of
// whether or not they've actually been written to (due to `tbot init`
// or our lightweight init during `tbot start`). If required artifacts
// are empty, this identity can't be loaded.
if !artifact.Optional && len(data) == 0 {
return nil, trace.NotFound("artifact %q is unexpectedly empty in destination %s", artifact.Key, d)
}
artifact.FromBytes(&certs, ¶ms, data)
}
log.Debugf("Loaded %d SSH CA certs and %d TLS CA certs", len(certs.SSHCACerts), len(certs.TLSCACerts))
return ReadIdentityFromStore(¶ms, &certs)
}