forked from gravitational/teleport
/
interfaces.go
597 lines (528 loc) · 18.2 KB
/
interfaces.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
Copyright 2015-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 client
import (
"bytes"
"crypto/subtle"
"crypto/tls"
"crypto/x509"
"fmt"
"time"
"github.com/gravitational/trace"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
"github.com/zmb3/teleport"
"github.com/zmb3/teleport/api/constants"
"github.com/zmb3/teleport/api/identityfile"
apiutils "github.com/zmb3/teleport/api/utils"
"github.com/zmb3/teleport/api/utils/keys"
"github.com/zmb3/teleport/api/utils/sshutils"
"github.com/zmb3/teleport/lib/auth"
"github.com/zmb3/teleport/lib/auth/native"
"github.com/zmb3/teleport/lib/services"
"github.com/zmb3/teleport/lib/tlsca"
"github.com/zmb3/teleport/lib/utils"
)
// KeyIndex helps to identify a key in the store.
type KeyIndex struct {
// ProxyHost is the root proxy hostname that a key is associated with.
ProxyHost string
// Username is the username that a key is associated with.
Username string
// ClusterName is the cluster name that a key is associated with.
ClusterName string
}
// Check verifies the KeyIndex is fully specified.
func (idx KeyIndex) Check() error {
missingField := "key index field %s is not set"
if idx.ProxyHost == "" {
return trace.BadParameter(missingField, "ProxyHost")
}
if idx.Username == "" {
return trace.BadParameter(missingField, "Username")
}
if idx.ClusterName == "" {
return trace.BadParameter(missingField, "ClusterName")
}
return nil
}
// Key describes a complete (signed) client key
type Key struct {
KeyIndex
// PrivateKey is a private key used for cryptographical operations.
*keys.PrivateKey
// Cert is an SSH client certificate
Cert []byte `json:"Cert,omitempty"`
// TLSCert is a PEM encoded client TLS x509 certificate.
// It's used to authenticate to the Teleport APIs.
TLSCert []byte `json:"TLSCert,omitempty"`
// KubeTLSCerts are TLS certificates (PEM-encoded) for individual
// kubernetes clusters. Map key is a kubernetes cluster name.
KubeTLSCerts map[string][]byte `json:"KubeCerts,omitempty"`
// DBTLSCerts are PEM-encoded TLS certificates for database access.
// Map key is the database service name.
DBTLSCerts map[string][]byte `json:"DBCerts,omitempty"`
// AppTLSCerts are TLS certificates for application access.
// Map key is the application name.
AppTLSCerts map[string][]byte `json:"AppCerts,omitempty"`
// WindowsDesktopCerts are TLS certificates for Windows Desktop access.
// Map key is the desktop server name.
WindowsDesktopCerts map[string][]byte `json:"WindowsDesktopCerts,omitempty"`
// TrustedCA is a list of trusted certificate authorities
TrustedCA []auth.TrustedCerts
}
// GenerateRSAKey generates a new unsigned key.
func GenerateRSAKey() (*Key, error) {
priv, err := native.GeneratePrivateKey()
if err != nil {
return nil, trace.Wrap(err)
}
return NewKey(priv), nil
}
// NewKey creates a new Key for the given private key.
func NewKey(priv *keys.PrivateKey) *Key {
return &Key{
PrivateKey: priv,
KubeTLSCerts: make(map[string][]byte),
DBTLSCerts: make(map[string][]byte),
AppTLSCerts: make(map[string][]byte),
WindowsDesktopCerts: make(map[string][]byte),
}
}
// extractIdentityFromCert parses a tlsca.Identity from raw PEM cert bytes.
func extractIdentityFromCert(certBytes []byte) (*tlsca.Identity, error) {
cert, err := tlsca.ParseCertificatePEM(certBytes)
if err != nil {
return nil, trace.Wrap(err, "failed to parse TLS certificate")
}
parsed, err := tlsca.FromSubject(cert.Subject, cert.NotAfter)
if err != nil {
return nil, trace.Wrap(err)
}
return parsed, nil
}
// KeyFromIdentityFile loads the private key + certificate
// from an identity file into a Key.
func KeyFromIdentityFile(path string) (*Key, error) {
ident, err := identityfile.ReadFile(path)
if err != nil {
return nil, trace.Wrap(err, "failed to parse identity file")
}
priv, err := keys.ParsePrivateKey(ident.PrivateKey)
if err != nil {
return nil, trace.Wrap(err)
}
dbTLSCerts := make(map[string][]byte)
appCerts := make(map[string][]byte)
// validate TLS Cert (if present):
if len(ident.Certs.TLS) > 0 {
if _, err := priv.TLSCertificate(ident.Certs.TLS); err != nil {
return nil, trace.Wrap(err)
}
parsedIdent, err := extractIdentityFromCert(ident.Certs.TLS)
if err != nil {
return nil, trace.Wrap(err)
}
// If this identity file has any database certs, copy it into the DBTLSCerts map.
if parsedIdent.RouteToDatabase.ServiceName != "" {
dbTLSCerts[parsedIdent.RouteToDatabase.ServiceName] = ident.Certs.TLS
}
// Similarly, if this identity has any app certs, copy them in.
if parsedIdent.RouteToApp.Name != "" {
appCerts[parsedIdent.RouteToApp.Name] = ident.Certs.TLS
}
}
// Validate TLS CA certs (if present).
var trustedCA []auth.TrustedCerts
if len(ident.CACerts.TLS) > 0 || len(ident.CACerts.SSH) > 0 {
trustedCA = []auth.TrustedCerts{{
TLSCertificates: ident.CACerts.TLS,
HostCertificates: ident.CACerts.SSH,
}}
pool := x509.NewCertPool()
for i, certPEM := range ident.CACerts.TLS {
if !pool.AppendCertsFromPEM(certPEM) {
return nil, trace.BadParameter("identity file contains invalid TLS CA cert (#%v)", i+1)
}
}
for _, caCert := range ident.CACerts.SSH {
if _, _, _, _, _, err := ssh.ParseKnownHosts(caCert); err != nil {
return nil, trace.BadParameter("CA cert parsing error: %v; make sure this identity file was generated by 'tsh login -o' or 'tctl auth sign --format=file' or try generating it again", err.Error())
}
}
}
return &Key{
PrivateKey: priv,
Cert: ident.Certs.SSH,
TLSCert: ident.Certs.TLS,
TrustedCA: trustedCA,
DBTLSCerts: dbTLSCerts,
AppTLSCerts: appCerts,
}, nil
}
// RootClusterCAs returns root cluster CAs.
func (k *Key) RootClusterCAs() ([][]byte, error) {
rootClusterName, err := k.RootClusterName()
if err != nil {
return nil, trace.Wrap(err)
}
var out [][]byte
for _, cas := range k.TrustedCA {
for _, v := range cas.TLSCertificates {
cert, err := tlsca.ParseCertificatePEM(v)
if err != nil {
return nil, trace.Wrap(err)
}
if cert.Subject.CommonName == rootClusterName {
out = append(out, v)
}
}
}
if len(out) > 0 {
return out, nil
}
return nil, trace.NotFound("failed to find TLS CA for %q root cluster", rootClusterName)
}
// TLSCAs returns all TLS CA certificates from this key
func (k *Key) TLSCAs() (result [][]byte) {
for _, ca := range k.TrustedCA {
result = append(result, ca.TLSCertificates...)
}
return result
}
func (k *Key) KubeClientTLSConfig(cipherSuites []uint16, kubeClusterName string) (*tls.Config, error) {
rootCluster, err := k.RootClusterName()
if err != nil {
return nil, trace.Wrap(err)
}
tlsCert, ok := k.KubeTLSCerts[kubeClusterName]
if !ok {
return nil, trace.NotFound("TLS certificate for kubernetes cluster %q not found", kubeClusterName)
}
tlsConfig, err := k.clientTLSConfig(cipherSuites, tlsCert, []string{rootCluster})
if err != nil {
return nil, trace.Wrap(err)
}
tlsConfig.ServerName = fmt.Sprintf("%s%s", constants.KubeSNIPrefix, constants.APIDomain)
return tlsConfig, nil
}
// SSHCAs returns all SSH CA certificates from this key
func (k *Key) SSHCAs() (result [][]byte) {
for _, ca := range k.TrustedCA {
result = append(result, ca.HostCertificates...)
}
return result
}
// SSHCAsForClusters returns SSH CA for particular clusters.
func (k *Key) SSHCAsForClusters(clusters []string) (result [][]byte, err error) {
for _, ca := range k.TrustedCA {
for _, hc := range ca.HostCertificates {
_, hosts, _, _, _, err := ssh.ParseKnownHosts(hc)
if err != nil {
return nil, trace.Wrap(err)
}
for _, h := range hosts {
for _, c := range clusters {
if h == c {
result = append(result, hc)
}
}
}
}
}
return result, nil
}
// GetClusterNames gets the names of clusters this key has CAs for.
func (k *Key) GetClusterNames() ([]string, error) {
var clusters []string
for _, ca := range k.TrustedCA {
for _, hc := range ca.HostCertificates {
_, hosts, _, _, _, err := ssh.ParseKnownHosts(hc)
if err != nil {
return nil, trace.Wrap(err)
}
clusters = append(clusters, hosts...)
}
}
return apiutils.Deduplicate(clusters), nil
}
// TeleportClientTLSConfig returns client TLS configuration used
// to authenticate against API servers.
func (k *Key) TeleportClientTLSConfig(cipherSuites []uint16, clusters []string) (*tls.Config, error) {
return k.clientTLSConfig(cipherSuites, k.TLSCert, clusters)
}
func (k *Key) clientTLSConfig(cipherSuites []uint16, tlsCertRaw []byte, clusters []string) (*tls.Config, error) {
tlsCert, err := k.TLSCertificate(tlsCertRaw)
if err != nil {
return nil, trace.Wrap(err)
}
pool, err := k.clientCertPool(clusters...)
if err != nil {
return nil, trace.Wrap(err)
}
tlsConfig := utils.TLSConfig(cipherSuites)
tlsConfig.RootCAs = pool
tlsConfig.Certificates = append(tlsConfig.Certificates, tlsCert)
// Use Issuer CN from the certificate to populate the correct SNI in
// requests.
leaf, err := x509.ParseCertificate(tlsCert.Certificate[0])
if err != nil {
return nil, trace.Wrap(err, "failed to parse TLS cert")
}
tlsConfig.ServerName = apiutils.EncodeClusterName(leaf.Issuer.CommonName)
return tlsConfig, nil
}
// ClientCertPool returns x509.CertPool containing trusted CA.
func (k *Key) clientCertPool(clusters ...string) (*x509.CertPool, error) {
pool := x509.NewCertPool()
for _, caPEM := range k.TLSCAs() {
cert, err := tlsca.ParseCertificatePEM(caPEM)
if err != nil {
return nil, trace.Wrap(err)
}
for _, k := range clusters {
if cert.Subject.CommonName == k {
if !pool.AppendCertsFromPEM(caPEM) {
return nil, trace.BadParameter("failed to parse TLS CA certificate")
}
}
}
}
return pool, nil
}
// ProxyClientSSHConfig returns an ssh.ClientConfig with SSH credentials from this
// Key and HostKeyCallback matching SSH CAs in the Key.
//
// The config is set up to authenticate to proxy with the first available principal
// and ( if keyStore != nil ) trust local SSH CAs without asking for public keys.
func (k *Key) ProxyClientSSHConfig(keyStore sshKnowHostGetter, host string) (*ssh.ClientConfig, error) {
sshCert, err := k.SSHCert()
if err != nil {
return nil, trace.Wrap(err, "failed to extract username from SSH certificate")
}
sshConfig, err := sshutils.ProxyClientSSHConfig(sshCert, k)
if err != nil {
return nil, trace.Wrap(err)
}
if keyStore != nil {
sshConfig.HostKeyCallback = NewKeyStoreCertChecker(keyStore, host)
}
return sshConfig, nil
}
// CertUsername returns the name of the Teleport user encoded in the SSH certificate.
func (k *Key) CertUsername() (string, error) {
cert, err := k.SSHCert()
if err != nil {
return "", trace.Wrap(err)
}
return cert.KeyId, nil
}
// CertPrincipals returns the principals listed on the SSH certificate.
func (k *Key) CertPrincipals() ([]string, error) {
cert, err := k.SSHCert()
if err != nil {
return nil, trace.Wrap(err)
}
return cert.ValidPrincipals, nil
}
func (k *Key) CertRoles() ([]string, error) {
cert, err := k.SSHCert()
if err != nil {
return nil, trace.Wrap(err)
}
// Extract roles from certificate. Note, if the certificate is in old format,
// this will be empty.
var roles []string
rawRoles, ok := cert.Extensions[teleport.CertExtensionTeleportRoles]
if ok {
roles, err = services.UnmarshalCertRoles(rawRoles)
if err != nil {
return nil, trace.Wrap(err)
}
}
return roles, nil
}
// AsAgentKeys converts client.Key struct to a []*agent.AddedKey. All elements
// of the []*agent.AddedKey slice need to be loaded into the agent!
func (k *Key) AsAgentKey() (agent.AddedKey, error) {
sshCert, err := k.SSHCert()
if err != nil {
return agent.AddedKey{}, trace.Wrap(err)
}
return k.PrivateKey.AsAgentKey(sshCert)
}
// TeleportTLSCertificate returns the parsed x509 certificate for
// authentication against Teleport APIs.
func (k *Key) TeleportTLSCertificate() (*x509.Certificate, error) {
return tlsca.ParseCertificatePEM(k.TLSCert)
}
// KubeTLSCertificate returns the parsed x509 certificate for
// authentication against a named kubernetes cluster.
func (k *Key) KubeTLSCertificate(kubeClusterName string) (*x509.Certificate, error) {
tlsCert, ok := k.KubeTLSCerts[kubeClusterName]
if !ok {
return nil, trace.NotFound("TLS certificate for kubernetes cluster %q not found", kubeClusterName)
}
return tlsca.ParseCertificatePEM(tlsCert)
}
// DBTLSCertificates returns all parsed x509 database access certificates.
func (k *Key) DBTLSCertificates() (certs []x509.Certificate, err error) {
for _, bytes := range k.DBTLSCerts {
cert, err := tlsca.ParseCertificatePEM(bytes)
if err != nil {
return nil, trace.Wrap(err)
}
certs = append(certs, *cert)
}
return certs, nil
}
// AppTLSCertificates returns all parsed x509 app access certificates.
func (k *Key) AppTLSCertificates() (certs []x509.Certificate, err error) {
for _, bytes := range k.AppTLSCerts {
cert, err := tlsca.ParseCertificatePEM(bytes)
if err != nil {
return nil, trace.Wrap(err)
}
certs = append(certs, *cert)
}
return certs, nil
}
// TeleportTLSCertValidBefore returns the time of the TLS cert expiration
func (k *Key) TeleportTLSCertValidBefore() (t time.Time, err error) {
cert, err := k.TeleportTLSCertificate()
if err != nil {
return t, trace.Wrap(err)
}
return cert.NotAfter, nil
}
// CertValidBefore returns the time of the cert expiration
func (k *Key) CertValidBefore() (t time.Time, err error) {
cert, err := k.SSHCert()
if err != nil {
return t, trace.Wrap(err)
}
return time.Unix(int64(cert.ValidBefore), 0), nil
}
// AsAuthMethod returns an "auth method" interface, a common abstraction
// used by Golang SSH library. This is how you actually use a Key to feed
// it into the SSH lib.
func (k *Key) AsAuthMethod() (ssh.AuthMethod, error) {
cert, err := k.SSHCert()
if err != nil {
return nil, trace.Wrap(err)
}
return sshutils.AsAuthMethod(cert, k)
}
// SSHSigner returns an ssh.Signer using the SSH certificate in this key.
func (k *Key) SSHSigner() (ssh.Signer, error) {
cert, err := k.SSHCert()
if err != nil {
return nil, trace.Wrap(err)
}
return sshutils.SSHSigner(cert, k)
}
// SSHCert returns parsed SSH certificate
func (k *Key) SSHCert() (*ssh.Certificate, error) {
if k.Cert == nil {
return nil, trace.NotFound("SSH cert not available")
}
return sshutils.ParseCertificate(k.Cert)
}
// ActiveRequests gets the active requests associated with this key.
func (k *Key) ActiveRequests() (services.RequestIDs, error) {
var activeRequests services.RequestIDs
sshCert, err := k.SSHCert()
if err != nil {
return activeRequests, trace.Wrap(err)
}
rawRequests, ok := sshCert.Extensions[teleport.CertExtensionTeleportActiveRequests]
if ok {
if err := activeRequests.Unmarshal([]byte(rawRequests)); err != nil {
return activeRequests, trace.Wrap(err)
}
}
return activeRequests, nil
}
// CheckCert makes sure the key's SSH certificate is valid.
func (k *Key) CheckCert() error {
cert, err := k.SSHCert()
if err != nil {
return trace.Wrap(err)
}
if err := k.checkCert(cert); err != nil {
return trace.Wrap(err)
}
return nil
}
// checkCert makes sure the given SSH certificate is valid.
func (k *Key) checkCert(sshCert *ssh.Certificate) error {
// Check that the certificate was for the current public key. If not, the
// public/private key pair may have been rotated.
if !sshutils.KeysEqual(sshCert.Key, k.SSHPublicKey()) {
return trace.CompareFailed("public key in profile does not match the public key in SSH certificate")
}
// A valid principal is always passed in because the principals are not being
// checked here, but rather the validity period, signature, and algorithms.
certChecker := sshutils.CertChecker{
FIPS: isFIPS(),
}
if err := certChecker.CheckCert(sshCert.ValidPrincipals[0], sshCert); err != nil {
return trace.Wrap(err)
}
return nil
}
// HostKeyCallback returns an ssh.HostKeyCallback that validates host
// keys/certs against SSH CAs in the Key.
//
// If not CAs are present in the Key, the returned ssh.HostKeyCallback is nil.
// This causes golang.org/x/crypto/ssh to prompt the user to verify host key
// fingerprint (same as OpenSSH does for an unknown host).
func (k *Key) HostKeyCallback(withHostKeyFallback bool) (ssh.HostKeyCallback, error) {
return sshutils.HostKeyCallback(k.SSHCAs(), withHostKeyFallback)
}
// HostKeyCallbackForClusters returns an ssh.HostKeyCallback that validates host
// keys/certs against SSH clusters CAs.
//
// If not CAs are present in the Key, the returned ssh.HostKeyCallback is nil.
// This causes golang.org/x/crypto/ssh to prompt the user to verify host key
// fingerprint (same as OpenSSH does for an unknown host).
func (k *Key) HostKeyCallbackForClusters(withHostKeyFallback bool, clusters []string) (ssh.HostKeyCallback, error) {
sshCA, err := k.SSHCAsForClusters(clusters)
if err != nil {
return nil, trace.Wrap(err)
}
return sshutils.HostKeyCallback(sshCA, withHostKeyFallback)
}
// RootClusterName extracts the root cluster name from the issuer
// of the Teleport TLS certificate.
func (k *Key) RootClusterName() (string, error) {
cert, err := k.TeleportTLSCertificate()
if err != nil {
return "", trace.Wrap(err)
}
clusterName := cert.Issuer.CommonName
if clusterName == "" {
return "", trace.NotFound("failed to extract root cluster name from Teleport TLS cert")
}
return clusterName, nil
}
// EqualPrivateKey returns whether this key and the given key have the same PrivateKey.
func (k *Key) EqualPrivateKey(other *Key) bool {
// Compare both private and public key PEM, since hardware keys
// may not be uniquely identifiable by their private key PEM alone.
// For example, for PIV keys, the private key PEM only uniquely
// identifies a PIV slot, so we can use the public key to verify
// that the private key on the slot hasn't changed.
return subtle.ConstantTimeCompare(k.PrivateKeyPEM(), other.PrivateKeyPEM()) == 1 &&
bytes.Equal(k.MarshalSSHPublicKey(), other.MarshalSSHPublicKey())
}