-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add server-side tpm joining implementation
#40512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8071b7f
Add clientside elements of TPM joining
strideynet e5b4bd0
Update lib/auth/register.go
strideynet 338caa3
Update api/client/joinservice.go
strideynet 71b55b1
Update lib/auth/register.go
strideynet e999ea9
Tidy up RegisterUsingTPMMethod method
strideynet 29eb188
Add default case
strideynet 101f07c
Rename CheckAndSetDefaults to validate
strideynet 55834c9
Add basic success test for JoinServiceClient_RegisterUsingTPMMethod
strideynet 3eec5a1
Add final touches to client joinservice test
strideynet 656354b
Add license header to joinservice_test.go
strideynet 6911756
Add server-side elements of TPM joining
strideynet 93adbd1
Turn SAN extension code into helper func
strideynet c4560ed
Add `ok` check to provision token casting
strideynet 64a7fee
Improve test name
strideynet a0392f2
Update lib/auth/join_tpm.go
strideynet f0a111b
Update lib/auth/join_tpm.go
strideynet a2dfa3e
Unexported registerUsingTPMMethod
strideynet bad75f7
Refactor enterprise error
strideynet 3e181e1
tidy up test
strideynet 487d367
Update lib/tpm/validate.go
strideynet 1774b1b
Update lib/auth/join_tpm.go
strideynet 20afee1
Update lib/auth/join_tpm_test.go
strideynet ea25e88
Fix StripSANExtensionOIDs and add test
strideynet d9b7b73
Improve joinserver.go, use simpler proto getter methods and use slog
strideynet c74da87
Tidy up join_tpm_test.go
strideynet 39ad9bf
Tidy up joinserver_test
strideynet 3bd65c2
Merge remote-tracking branch 'origin/master' into strideynet/tpm-join…
strideynet 5a10a17
Add join failure audit event
strideynet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /* | ||
| * Teleport | ||
| * Copyright (C) 2024 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 auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/x509" | ||
| "log/slog" | ||
|
|
||
| "github.com/google/go-attestation/attest" | ||
| "github.com/gravitational/trace" | ||
|
|
||
| "github.com/gravitational/teleport/api/client" | ||
| "github.com/gravitational/teleport/api/client/proto" | ||
| "github.com/gravitational/teleport/api/types" | ||
| "github.com/gravitational/teleport/lib/modules" | ||
| "github.com/gravitational/teleport/lib/tpm" | ||
| ) | ||
|
|
||
| func (a *Server) registerUsingTPMMethod( | ||
| ctx context.Context, | ||
| initReq *proto.RegisterUsingTPMMethodInitialRequest, | ||
| solveChallenge client.RegisterTPMChallengeResponseFunc, | ||
| ) (_ *proto.Certs, err error) { | ||
| var provisionToken types.ProvisionToken | ||
| var attributeSrc joinAttributeSourcer | ||
| defer func() { | ||
| // Emit a log message and audit event on join failure. | ||
| if err != nil { | ||
| a.handleJoinFailure( | ||
| err, provisionToken, attributeSrc, initReq.JoinRequest, | ||
| ) | ||
| } | ||
| }() | ||
|
|
||
| // First, check the specified token exists, and is a TPM-type join token. | ||
| if err := initReq.JoinRequest.CheckAndSetDefaults(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| provisionToken, err = a.checkTokenJoinRequestCommon(ctx, initReq.JoinRequest) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| ptv2, ok := provisionToken.(*types.ProvisionTokenV2) | ||
| if !ok { | ||
| return nil, trace.BadParameter("expected *types.ProvisionTokenV2, got %T", provisionToken) | ||
| } | ||
| if ptv2.Spec.JoinMethod != types.JoinMethodTPM { | ||
| return nil, trace.BadParameter("specified join token is not for `tpm` method") | ||
| } | ||
|
|
||
| if modules.GetModules().BuildType() != modules.BuildEnterprise { | ||
| return nil, trace.Wrap( | ||
| ErrRequiresEnterprise, | ||
| "tpm joining", | ||
| ) | ||
| } | ||
|
|
||
| // Convert configured CAs to a CAPool | ||
| var certPool *x509.CertPool | ||
| if len(ptv2.Spec.TPM.EKCertAllowedCAs) > 0 { | ||
| certPool = x509.NewCertPool() | ||
| for i, ca := range ptv2.Spec.TPM.EKCertAllowedCAs { | ||
| if ok := certPool.AppendCertsFromPEM([]byte(ca)); !ok { | ||
| return nil, trace.BadParameter( | ||
| "ekcert_allowed_cas[%d] has an invalid or malformed PEM", i, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TODO(noah): Use logger from TeleportProcess. | ||
| validatedEK, err := a.tpmValidator(ctx, slog.Default(), tpm.ValidateParams{ | ||
| EKCert: initReq.GetEkCert(), | ||
| EKKey: initReq.GetEkKey(), | ||
| AttestParams: tpm.AttestationParametersFromProto(initReq.AttestationParams), | ||
| AllowedCAs: certPool, | ||
| Solve: func(ec *attest.EncryptedCredential) ([]byte, error) { | ||
| solution, err := solveChallenge(tpm.EncryptedCredentialToProto(ec)) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| return solution.Solution, nil | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err, "validating TPM EK") | ||
| } | ||
| attributeSrc = validatedEK | ||
|
|
||
| if err := checkTPMAllowRules(validatedEK, ptv2.Spec.TPM.Allow); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| if initReq.JoinRequest.Role == types.RoleBot { | ||
| certs, err := a.generateCertsBot( | ||
| ctx, ptv2, initReq.JoinRequest, validatedEK, | ||
| ) | ||
| return certs, trace.Wrap(err, "generating certs for bot") | ||
| } | ||
| certs, err := a.generateCerts( | ||
| ctx, ptv2, initReq.JoinRequest, validatedEK, | ||
| ) | ||
| return certs, trace.Wrap(err, "generating certs for host") | ||
| } | ||
|
|
||
| func checkTPMAllowRules(tpm *tpm.ValidatedTPM, rules []*types.ProvisionTokenSpecV2TPM_Rule) error { | ||
| // If a single rule passes, accept the TPM | ||
| for _, rule := range rules { | ||
| if rule.EKPublicHash != "" && tpm.EKPubHash != rule.EKPublicHash { | ||
| continue | ||
| } | ||
| if rule.EKCertificateSerial != "" && tpm.EKCertSerial != rule.EKCertificateSerial { | ||
| continue | ||
| } | ||
|
|
||
| // All rules met. | ||
| return nil | ||
| } | ||
| return trace.AccessDenied("validated tpm attributes did not match any allow rules") | ||
|
codingllama marked this conversation as resolved.
Outdated
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.