-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ca.go
1223 lines (1112 loc) · 40.9 KB
/
ca.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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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 tlsca
import (
"crypto"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/pem"
"fmt"
"math/big"
"net"
"strconv"
"time"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/sirupsen/logrus"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/api/types/wrappers"
"github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/api/utils/keys"
)
var log = logrus.WithFields(logrus.Fields{
trace.Component: teleport.ComponentAuthority,
})
// FromCertAndSigner returns a CertAuthority with the given raw certificate and signer.
func FromCertAndSigner(certPEM []byte, signer crypto.Signer) (*CertAuthority, error) {
cert, err := ParseCertificatePEM(certPEM)
if err != nil {
return nil, trace.Wrap(err)
}
return &CertAuthority{
Cert: cert,
Signer: signer,
}, nil
}
// FromKeys returns new CA from PEM encoded certificate and private
// key. Private Key is optional, if omitted CA won't be able to
// issue new certificates, only verify them
func FromKeys(certPEM, keyPEM []byte) (*CertAuthority, error) {
ca := &CertAuthority{}
var err error
ca.Cert, err = ParseCertificatePEM(certPEM)
if err != nil {
return nil, trace.Wrap(err)
}
if len(keyPEM) != 0 {
ca.Signer, err = ParsePrivateKeyPEM(keyPEM)
if err != nil {
return nil, trace.Wrap(err)
}
}
return ca, nil
}
// FromTLSCertificate returns a CertAuthority with the given TLS certificate.
func FromTLSCertificate(ca tls.Certificate) (*CertAuthority, error) {
if len(ca.Certificate) == 0 {
return nil, trace.BadParameter("invalid certificate length")
}
cert, err := x509.ParseCertificate(ca.Certificate[0])
if err != nil {
return nil, trace.Wrap(err)
}
signer, ok := ca.PrivateKey.(crypto.Signer)
if !ok {
return nil, trace.BadParameter("failed to convert private key to signer")
}
return &CertAuthority{
Cert: cert,
Signer: signer,
}, nil
}
// CertAuthority is X.509 certificate authority
type CertAuthority struct {
// Cert is a CA certificate
Cert *x509.Certificate
// Signer is a private key based signer
Signer crypto.Signer
}
// Identity is an identity of the user or service, e.g. Proxy or Node
type Identity struct {
// Username is a username or name of the node connection
Username string
// Impersonator is a username of a user impersonating this user
Impersonator string
// Groups is a list of groups (Teleport roles) encoded in the identity
Groups []string
// SystemRoles is a list of system roles (e.g. auth, proxy, node, etc) used
// in "multi-role" certificates. Single-role certificates encode the system role
// in `Groups` for back-compat reasons.
SystemRoles []string
// Usage is a list of usage restrictions encoded in the identity
Usage []string
// Principals is a list of Unix logins allowed.
Principals []string
// KubernetesGroups is a list of Kubernetes groups allowed
KubernetesGroups []string
// KubernetesUsers is a list of Kubernetes users allowed
KubernetesUsers []string
// Expires specifies whenever the session will expire
Expires time.Time
// RouteToCluster specifies the target cluster
// if present in the session
RouteToCluster string
// KubernetesCluster specifies the target kubernetes cluster for TLS
// identities. This can be empty on older Teleport clients.
KubernetesCluster string
// Traits hold claim data used to populate a role at runtime.
Traits wrappers.Traits
// RouteToApp holds routing information for applications. Routing metadata
// allows Teleport web proxy to route HTTP requests to the appropriate
// cluster and Teleport application proxy within the cluster.
RouteToApp RouteToApp
// TeleportCluster is the name of the teleport cluster that this identity
// originated from. For TLS certs this may not be the same as cert issuer,
// in case of multi-hop requests that originate from a remote cluster.
TeleportCluster string
// RouteToDatabase contains routing information for databases.
RouteToDatabase RouteToDatabase
// DatabaseNames is a list of allowed database names.
DatabaseNames []string
// DatabaseUsers is a list of allowed database users.
DatabaseUsers []string
// MFAVerified is the UUID of an MFA device when this Identity was
// confirmed immediately after an MFA check.
MFAVerified string
// PreviousIdentityExpires is the expiry time of the identity/cert that this
// identity/cert was derived from. It is used to determine a session's hard
// deadline in cases where both require_session_mfa and disconnect_expired_cert
// are enabled. See https://github.com/gravitational/teleport/issues/18544.
PreviousIdentityExpires time.Time
// LoginIP is an observed IP of the client that this Identity represents.
LoginIP string
// PinnedIP is an IP the certificate is pinned to.
PinnedIP string
// AWSRoleARNs is a list of allowed AWS role ARNs user can assume.
AWSRoleARNs []string
// AzureIdentities is a list of allowed Azure identities user can assume.
AzureIdentities []string
// GCPServiceAccounts is a list of allowed GCP service accounts that the user can assume.
GCPServiceAccounts []string
// ActiveRequests is a list of UUIDs of active requests for this Identity.
ActiveRequests []string
// DisallowReissue is a flag that, if set, instructs the auth server to
// deny any attempts to reissue new certificates while authenticated with
// this certificate.
DisallowReissue bool
// Renewable indicates that this identity is allowed to renew it's
// own credentials. This is only enabled for certificate renewal bots.
Renewable bool
// Generation counts the number of times this certificate has been renewed.
Generation uint64
// BotName indicates the name of the Machine ID bot this identity was issued
// to, if any.
BotName string
// AllowedResourceIDs lists the resources the identity should be allowed to
// access.
AllowedResourceIDs []types.ResourceID
// PrivateKeyPolicy is the private key policy supported by this identity.
PrivateKeyPolicy keys.PrivateKeyPolicy
// ConnectionDiagnosticID is used to add connection diagnostic messages when Testing a Connection.
ConnectionDiagnosticID string
// DeviceExtensions holds device-aware extensions for the identity.
DeviceExtensions DeviceExtensions
// UserType indicates if the User was created by an SSO Provider or locally.
UserType types.UserType
}
// RouteToApp holds routing information for applications.
type RouteToApp struct {
// SessionID is a UUIDv4 used to identify application sessions created by
// this certificate. The reason a UUID was used instead of a hash of the
// SubjectPublicKeyInfo like the CA pin is for UX consistency. For example,
// the SessionID is emitted in the audit log, using a UUID matches how SSH
// sessions are identified.
SessionID string
// PublicAddr (and ClusterName) are used to route requests issued with this
// certificate to the appropriate application proxy/cluster.
PublicAddr string
// ClusterName (and PublicAddr) are used to route requests issued with this
// certificate to the appropriate application proxy/cluster.
ClusterName string
// Name is the app name.
Name string
// AWSRoleARN is the AWS role to assume when accessing AWS console.
AWSRoleARN string
// AzureIdentity is the Azure identity to assume when accessing Azure API.
AzureIdentity string
// GCPServiceAccount is the GCP service account to assume when accessing GCP API.
GCPServiceAccount string
}
// RouteToDatabase contains routing information for databases.
type RouteToDatabase struct {
// ServiceName is the name of the Teleport database proxy service
// to route requests to.
ServiceName string
// Protocol is the database protocol.
//
// It is embedded in identity so clients can understand what type
// of database this is without contacting server.
Protocol string
// Username is an optional database username to serve as a default
// username to connect as.
Username string
// Database is an optional database name to serve as a default
// database to connect to.
Database string
// Roles is an optional list of database roles to use for a database
// session.
// This list should be a subset of allowed database roles. If not
// specified, Database Service will use all allowed database roles for this
// database.
Roles []string
}
// String returns string representation of the database routing struct.
func (r RouteToDatabase) String() string {
return fmt.Sprintf("Database(Service=%v, Protocol=%v, Username=%v, Database=%v, Roles=%v)",
r.ServiceName, r.Protocol, r.Username, r.Database, r.Roles)
}
// Empty returns true if RouteToDatabase is empty.
func (r RouteToDatabase) Empty() bool {
return r.ServiceName == "" &&
r.Protocol == "" &&
r.Username == "" &&
r.Database == "" &&
len(r.Roles) == 0
}
// DeviceExtensions holds device-aware extensions for the identity.
type DeviceExtensions struct {
// DeviceID is the trusted device identifier.
DeviceID string
// AssetTag is the device inventory identifier.
AssetTag string
// CredentialID is the identifier for the credential used by the device to
// authenticate itself.
CredentialID string
}
// GetRouteToApp returns application routing data. If missing, returns an error.
func (id *Identity) GetRouteToApp() (RouteToApp, error) {
if id.RouteToApp.SessionID == "" ||
id.RouteToApp.PublicAddr == "" ||
id.RouteToApp.ClusterName == "" {
return RouteToApp{}, trace.BadParameter("identity is missing application routing metadata")
}
return id.RouteToApp, nil
}
func (id *Identity) GetEventIdentity() events.Identity {
// leave a nil instead of a zero struct so the field doesn't appear when
// serialized as json
var routeToApp *events.RouteToApp
if id.RouteToApp != (RouteToApp{}) {
routeToApp = &events.RouteToApp{
Name: id.RouteToApp.Name,
SessionID: id.RouteToApp.SessionID,
PublicAddr: id.RouteToApp.PublicAddr,
ClusterName: id.RouteToApp.ClusterName,
AWSRoleARN: id.RouteToApp.AWSRoleARN,
AzureIdentity: id.RouteToApp.AzureIdentity,
GCPServiceAccount: id.RouteToApp.GCPServiceAccount,
}
}
var routeToDatabase *events.RouteToDatabase
if !id.RouteToDatabase.Empty() {
routeToDatabase = &events.RouteToDatabase{
ServiceName: id.RouteToDatabase.ServiceName,
Protocol: id.RouteToDatabase.Protocol,
Username: id.RouteToDatabase.Username,
Database: id.RouteToDatabase.Database,
Roles: id.RouteToDatabase.Roles,
}
}
return events.Identity{
User: id.Username,
Impersonator: id.Impersonator,
Roles: id.Groups,
Usage: id.Usage,
Logins: id.Principals,
KubernetesGroups: id.KubernetesGroups,
KubernetesUsers: id.KubernetesUsers,
Expires: id.Expires,
RouteToCluster: id.RouteToCluster,
KubernetesCluster: id.KubernetesCluster,
Traits: id.Traits,
RouteToApp: routeToApp,
TeleportCluster: id.TeleportCluster,
RouteToDatabase: routeToDatabase,
DatabaseNames: id.DatabaseNames,
DatabaseUsers: id.DatabaseUsers,
MFADeviceUUID: id.MFAVerified,
PreviousIdentityExpires: id.PreviousIdentityExpires,
ClientIP: id.LoginIP,
AWSRoleARNs: id.AWSRoleARNs,
AzureIdentities: id.AzureIdentities,
GCPServiceAccounts: id.GCPServiceAccounts,
AccessRequests: id.ActiveRequests,
DisallowReissue: id.DisallowReissue,
AllowedResourceIDs: events.ResourceIDs(id.AllowedResourceIDs),
PrivateKeyPolicy: string(id.PrivateKeyPolicy),
}
}
// CheckAndSetDefaults checks and sets default values
func (id *Identity) CheckAndSetDefaults() error {
if id.Username == "" {
return trace.BadParameter("missing identity username")
}
if len(id.Groups) == 0 {
return trace.BadParameter("missing identity groups")
}
return nil
}
// Custom ranges are taken from this article
//
// https://serverfault.com/questions/551477/is-there-reserved-oid-space-for-internal-enterprise-cas
//
// http://oid-info.com/get/1.3.9999
var (
// KubeUsersASN1ExtensionOID is an extension ID used when encoding/decoding
// license payload into certificates
KubeUsersASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 1}
// KubeGroupsASN1ExtensionOID is an extension ID used when encoding/decoding
// license payload into certificates
KubeGroupsASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 2}
// KubeClusterASN1ExtensionOID is an extension ID used when encoding/decoding
// target kubernetes cluster name into certificates.
KubeClusterASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 3}
// AppSessionIDASN1ExtensionOID is an extension ID used to encode the application
// session ID into a certificate.
AppSessionIDASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 4}
// AppClusterNameASN1ExtensionOID is an extension ID used to encode the application
// cluster name into a certificate.
AppClusterNameASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 5}
// AppPublicAddrASN1ExtensionOID is an extension ID used to encode the application
// public address into a certificate.
AppPublicAddrASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 6}
// TeleportClusterASN1ExtensionOID is an extension ID used when encoding/decoding
// origin teleport cluster name into certificates.
TeleportClusterASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 7}
// MFAVerifiedASN1ExtensionOID is an extension ID used when encoding/decoding
// the MFAVerified flag into certificates.
MFAVerifiedASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 8}
// LoginIPASN1ExtensionOID is an extension ID used when encoding/decoding
// the client's login IP into certificates.
LoginIPASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 9}
// AppNameASN1ExtensionOID is an extension ID used when encoding/decoding
// application name into a certificate.
AppNameASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 10}
// AppAWSRoleARNASN1ExtensionOID is an extension ID used when encoding/decoding
// AWS role ARN into a certificate.
AppAWSRoleARNASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 11}
// AWSRoleARNsASN1ExtensionOID is an extension ID used when encoding/decoding
// allowed AWS role ARNs into a certificate.
AWSRoleARNsASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 12}
// RenewableCertificateASN1ExtensionOID is an extension ID used to indicate
// that a certificate may be renewed by a certificate renewal bot.
RenewableCertificateASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 13}
// GenerationASN1ExtensionOID is an extension OID used to count the number
// of times this certificate has been renewed.
GenerationASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 14}
// PrivateKeyPolicyASN1ExtensionOID is an extension ID used to determine the
// private key policy supported by the certificate.
PrivateKeyPolicyASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 15}
// AppAzureIdentityASN1ExtensionOID is an extension ID used when encoding/decoding
// Azure identity into a certificate.
AppAzureIdentityASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 16}
// AzureIdentityASN1ExtensionOID is an extension ID used when encoding/decoding
// allowed Azure identity into a certificate.
AzureIdentityASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 17}
// AppGCPServiceAccountASN1ExtensionOID is an extension ID used when encoding/decoding
// the chosen GCP service account into a certificate.
AppGCPServiceAccountASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 18}
// GCPServiceAccountsASN1ExtensionOID is an extension ID used when encoding/decoding
// the list of allowed GCP service accounts into a certificate.
GCPServiceAccountsASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 1, 19}
// DatabaseServiceNameASN1ExtensionOID is an extension ID used when encoding/decoding
// database service name into certificates.
DatabaseServiceNameASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 1}
// DatabaseProtocolASN1ExtensionOID is an extension ID used when encoding/decoding
// database protocol into certificates.
DatabaseProtocolASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 2}
// DatabaseUsernameASN1ExtensionOID is an extension ID used when encoding/decoding
// database username into certificates.
DatabaseUsernameASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 3}
// DatabaseNameASN1ExtensionOID is an extension ID used when encoding/decoding
// database name into certificates.
DatabaseNameASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 4}
// DatabaseNamesASN1ExtensionOID is an extension OID used when encoding/decoding
// allowed database names into certificates.
DatabaseNamesASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 5}
// DatabaseUsersASN1ExtensionOID is an extension OID used when encoding/decoding
// allowed database users into certificates.
DatabaseUsersASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 6}
// ImpersonatorASN1ExtensionOID is an extension OID used when encoding/decoding
// impersonator user
ImpersonatorASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 7}
// ActiveRequestsASN1ExtensionOID is an extension OID used when encoding/decoding
// active access requests into certificates.
ActiveRequestsASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 8}
// DisallowReissueASN1ExtensionOID is an extension OID used to flag that a
// requests to generate new certificates using this certificate should be
// denied.
DisallowReissueASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 9}
// AllowedResourcesASN1ExtensionOID is an extension OID used to list the
// resources which the certificate should be able to grant access to
AllowedResourcesASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 10}
// SystemRolesASN1ExtensionOID is an extension OID used to indicate system roles
// (auth, proxy, node, etc). Note that some certs correspond to a single specific
// system role, and use `pkix.Name.Organization` to encode this value. This extension
// is specifically used for "multi-role" certs.
SystemRolesASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 11}
// PreviousIdentityExpiresASN1ExtensionOID is the RFC3339 timestamp representing the hard
// deadline of the session on a certificates issued after an MFA check.
// See https://github.com/gravitational/teleport/issues/18544.
PreviousIdentityExpiresASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 12}
// ConnectionDiagnosticIDASN1ExtensionOID is an extension OID used to indicate the Connection Diagnostic ID.
// When using the Test Connection feature, there's propagation of the ConnectionDiagnosticID.
// Each service (ex DB Agent) uses that to add checkpoints describing if it was a success or a failure.
ConnectionDiagnosticIDASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 13}
// LicenseOID is an extension OID signaling the license type of Teleport build.
// It should take values "oss" or "ent" (the values returned by modules.GetModules().BuildType())
LicenseOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 14}
// PinnedIPASN1ExtensionOID is an extension ID used when encoding/decoding
// the IP the certificate is pinned to.
PinnedIPASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 15}
// CreateWindowsUserOID is an extension OID used to indicate that the user should be created.
CreateWindowsUserOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 16}
// DesktopsLimitExceededOID is an extension OID used indicate if number of non-AD desktops exceeds the limit for OSS distribution.
DesktopsLimitExceededOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 17}
// BotASN1ExtensionOID is an extension OID used to indicate an identity is associated with a Machine ID bot.
BotASN1ExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 18}
// RequestedDatabaseRolesExtensionOID is an extension OID used when
// encoding/decoding requested database roles.
RequestedDatabaseRolesExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 2, 19}
)
// Device Trust OIDs.
// Namespace 1.3.9999.3.x.
var (
// DeviceIDExtensionOID is a string extension that identifies the trusted
// device.
DeviceIDExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 3, 1}
// DeviceAssetTagExtensionOID is a string extension containing the device
// inventory identifier.
DeviceAssetTagExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 3, 2}
// DeviceCredentialIDExtensionOID is a string extension that identifies the
// credential used to authenticate the device.
DeviceCredentialIDExtensionOID = asn1.ObjectIdentifier{1, 3, 9999, 3, 3}
)
// Subject converts identity to X.509 subject name
func (id *Identity) Subject() (pkix.Name, error) {
rawTraits, err := wrappers.MarshalTraits(&id.Traits)
if err != nil {
return pkix.Name{}, trace.Wrap(err)
}
subject := pkix.Name{
CommonName: id.Username,
Organization: append([]string{}, id.Groups...),
OrganizationalUnit: append([]string{}, id.Usage...),
Locality: append([]string{}, id.Principals...),
// TODO: create ASN.1 extensions for traits and RouteToCluster
// and move away from using StreetAddress and PostalCode
StreetAddress: []string{id.RouteToCluster},
PostalCode: []string{string(rawTraits)},
}
for i := range id.SystemRoles {
systemRole := id.SystemRoles[i]
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: SystemRolesASN1ExtensionOID,
Value: systemRole,
})
}
for i := range id.KubernetesUsers {
kubeUser := id.KubernetesUsers[i]
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: KubeUsersASN1ExtensionOID,
Value: kubeUser,
})
}
for i := range id.KubernetesGroups {
kubeGroup := id.KubernetesGroups[i]
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: KubeGroupsASN1ExtensionOID,
Value: kubeGroup,
})
}
if id.KubernetesCluster != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: KubeClusterASN1ExtensionOID,
Value: id.KubernetesCluster,
})
}
// Encode application routing metadata if provided.
if id.RouteToApp.SessionID != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AppSessionIDASN1ExtensionOID,
Value: id.RouteToApp.SessionID,
})
}
if id.RouteToApp.PublicAddr != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AppPublicAddrASN1ExtensionOID,
Value: id.RouteToApp.PublicAddr,
})
}
if id.RouteToApp.ClusterName != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AppClusterNameASN1ExtensionOID,
Value: id.RouteToApp.ClusterName,
})
}
if id.RouteToApp.Name != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AppNameASN1ExtensionOID,
Value: id.RouteToApp.Name,
})
}
if id.RouteToApp.AWSRoleARN != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AppAWSRoleARNASN1ExtensionOID,
Value: id.RouteToApp.AWSRoleARN,
})
}
for i := range id.AWSRoleARNs {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AWSRoleARNsASN1ExtensionOID,
Value: id.AWSRoleARNs[i],
})
}
if id.RouteToApp.AzureIdentity != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AppAzureIdentityASN1ExtensionOID,
Value: id.RouteToApp.AzureIdentity,
})
}
for i := range id.AzureIdentities {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AzureIdentityASN1ExtensionOID,
Value: id.AzureIdentities[i],
})
}
if id.RouteToApp.GCPServiceAccount != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AppGCPServiceAccountASN1ExtensionOID,
Value: id.RouteToApp.GCPServiceAccount,
})
}
for i := range id.GCPServiceAccounts {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: GCPServiceAccountsASN1ExtensionOID,
Value: id.GCPServiceAccounts[i],
})
}
if id.Renewable {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: RenewableCertificateASN1ExtensionOID,
Value: types.True,
})
}
if id.TeleportCluster != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: TeleportClusterASN1ExtensionOID,
Value: id.TeleportCluster,
})
}
if id.MFAVerified != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: MFAVerifiedASN1ExtensionOID,
Value: id.MFAVerified,
})
}
if !id.PreviousIdentityExpires.IsZero() {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: PreviousIdentityExpiresASN1ExtensionOID,
Value: id.PreviousIdentityExpires.Format(time.RFC3339),
})
}
if id.LoginIP != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: LoginIPASN1ExtensionOID,
Value: id.LoginIP,
})
}
if id.PinnedIP != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: PinnedIPASN1ExtensionOID,
Value: id.PinnedIP,
})
}
// Encode routing metadata for databases.
if id.RouteToDatabase.ServiceName != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: DatabaseServiceNameASN1ExtensionOID,
Value: id.RouteToDatabase.ServiceName,
})
}
if id.RouteToDatabase.Protocol != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: DatabaseProtocolASN1ExtensionOID,
Value: id.RouteToDatabase.Protocol,
})
}
if id.RouteToDatabase.Username != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: DatabaseUsernameASN1ExtensionOID,
Value: id.RouteToDatabase.Username,
})
}
if id.RouteToDatabase.Database != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: DatabaseNameASN1ExtensionOID,
Value: id.RouteToDatabase.Database,
})
}
for i := range id.RouteToDatabase.Roles {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: RequestedDatabaseRolesExtensionOID,
Value: id.RouteToDatabase.Roles[i],
})
}
// Encode allowed database names/users used when passing them
// to remote clusters as user traits.
for i := range id.DatabaseNames {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: DatabaseNamesASN1ExtensionOID,
Value: id.DatabaseNames[i],
})
}
for i := range id.DatabaseUsers {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: DatabaseUsersASN1ExtensionOID,
Value: id.DatabaseUsers[i],
})
}
if id.Impersonator != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: ImpersonatorASN1ExtensionOID,
Value: id.Impersonator,
})
}
for _, activeRequest := range id.ActiveRequests {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: ActiveRequestsASN1ExtensionOID,
Value: activeRequest,
})
}
if id.DisallowReissue {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: DisallowReissueASN1ExtensionOID,
Value: types.True,
},
)
}
if id.Generation > 0 {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: GenerationASN1ExtensionOID,
Value: fmt.Sprint(id.Generation),
},
)
}
if id.BotName != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: BotASN1ExtensionOID,
Value: id.BotName,
})
}
if len(id.AllowedResourceIDs) > 0 {
allowedResourcesStr, err := types.ResourceIDsToString(id.AllowedResourceIDs)
if err != nil {
return pkix.Name{}, trace.Wrap(err)
}
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: AllowedResourcesASN1ExtensionOID,
Value: allowedResourcesStr,
},
)
}
if id.PrivateKeyPolicy != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: PrivateKeyPolicyASN1ExtensionOID,
Value: id.PrivateKeyPolicy,
},
)
}
if id.ConnectionDiagnosticID != "" {
subject.ExtraNames = append(subject.ExtraNames,
pkix.AttributeTypeAndValue{
Type: ConnectionDiagnosticIDASN1ExtensionOID,
Value: id.ConnectionDiagnosticID,
},
)
}
// Device extensions.
if devID := id.DeviceExtensions.DeviceID; devID != "" {
subject.ExtraNames = append(subject.ExtraNames, pkix.AttributeTypeAndValue{
Type: DeviceIDExtensionOID,
Value: devID,
})
}
if devTag := id.DeviceExtensions.AssetTag; devTag != "" {
subject.ExtraNames = append(subject.ExtraNames, pkix.AttributeTypeAndValue{
Type: DeviceAssetTagExtensionOID,
Value: devTag,
})
}
if devCred := id.DeviceExtensions.CredentialID; devCred != "" {
subject.ExtraNames = append(subject.ExtraNames, pkix.AttributeTypeAndValue{
Type: DeviceCredentialIDExtensionOID,
Value: devCred,
})
}
return subject, nil
}
// FromSubject returns identity from subject name
func FromSubject(subject pkix.Name, expires time.Time) (*Identity, error) {
id := &Identity{
Username: subject.CommonName,
Groups: subject.Organization,
Usage: subject.OrganizationalUnit,
Principals: subject.Locality,
Expires: expires,
}
if len(subject.StreetAddress) > 0 {
id.RouteToCluster = subject.StreetAddress[0]
}
if len(subject.PostalCode) > 0 {
err := wrappers.UnmarshalTraits([]byte(subject.PostalCode[0]), &id.Traits)
if err != nil {
return nil, trace.Wrap(err)
}
}
for _, attr := range subject.Names {
switch {
case attr.Type.Equal(SystemRolesASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.SystemRoles = append(id.SystemRoles, val)
}
case attr.Type.Equal(KubeUsersASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.KubernetesUsers = append(id.KubernetesUsers, val)
}
case attr.Type.Equal(KubeGroupsASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.KubernetesGroups = append(id.KubernetesGroups, val)
}
case attr.Type.Equal(KubeClusterASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.KubernetesCluster = val
}
case attr.Type.Equal(AppSessionIDASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToApp.SessionID = val
}
case attr.Type.Equal(AppPublicAddrASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToApp.PublicAddr = val
}
case attr.Type.Equal(AppClusterNameASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToApp.ClusterName = val
}
case attr.Type.Equal(AppNameASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToApp.Name = val
}
case attr.Type.Equal(AppAWSRoleARNASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToApp.AWSRoleARN = val
}
case attr.Type.Equal(AWSRoleARNsASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.AWSRoleARNs = append(id.AWSRoleARNs, val)
}
case attr.Type.Equal(AppAzureIdentityASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToApp.AzureIdentity = val
}
case attr.Type.Equal(AzureIdentityASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.AzureIdentities = append(id.AzureIdentities, val)
}
case attr.Type.Equal(AppGCPServiceAccountASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToApp.GCPServiceAccount = val
}
case attr.Type.Equal(GCPServiceAccountsASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.GCPServiceAccounts = append(id.GCPServiceAccounts, val)
}
case attr.Type.Equal(RenewableCertificateASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.Renewable = val == types.True
}
case attr.Type.Equal(TeleportClusterASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.TeleportCluster = val
}
case attr.Type.Equal(MFAVerifiedASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.MFAVerified = val
}
case attr.Type.Equal(PreviousIdentityExpiresASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
asTime, err := time.Parse(time.RFC3339, val)
if err != nil {
return nil, trace.Wrap(err)
}
id.PreviousIdentityExpires = asTime
}
case attr.Type.Equal(LoginIPASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.LoginIP = val
}
case attr.Type.Equal(DatabaseServiceNameASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToDatabase.ServiceName = val
}
case attr.Type.Equal(DatabaseProtocolASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToDatabase.Protocol = val
}
case attr.Type.Equal(DatabaseUsernameASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToDatabase.Username = val
}
case attr.Type.Equal(DatabaseNameASN1ExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToDatabase.Database = val
}
case attr.Type.Equal(RequestedDatabaseRolesExtensionOID):
val, ok := attr.Value.(string)
if ok {
id.RouteToDatabase.Roles = append(id.RouteToDatabase.Roles, val)