-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent: learn to cache userauths for TPM keys
Signed-off-by: Morten Linderud <[email protected]>
- Loading branch information
Showing
4 changed files
with
69 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,41 @@ | ||
package signer | ||
|
||
import ( | ||
"crypto" | ||
"errors" | ||
"io" | ||
"log/slog" | ||
|
||
"github.com/google/go-tpm/tpm2" | ||
"github.com/google/go-tpm/tpm2/transport" | ||
|
||
keyfile "github.com/foxboron/go-tpm-keyfiles" | ||
"github.com/foxboron/ssh-tpm-agent/key" | ||
) | ||
|
||
// Shim for keyfile.TPMKeySigner | ||
// We need access to the SSHTPMKey to change the userauth for caching | ||
type SSHKeySigner struct { | ||
*keyfile.TPMKeySigner | ||
key *key.SSHTPMKey | ||
} | ||
|
||
// func (t *SSHKeySigner) Public() crypto.PublicKey { | ||
// return t.TPMKeySigner.Public() | ||
// } | ||
|
||
func (t *SSHKeySigner) Sign(r io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { | ||
b, err := t.TPMKeySigner.Sign(r, digest, opts) | ||
if errors.Is(err, tpm2.TPMRCAuthFail) { | ||
slog.Debug("removed cached userauth for key", slog.Any("err", err), slog.String("desc", t.key.Description)) | ||
t.key.Userauth = []byte(nil) | ||
} | ||
return b, err | ||
} | ||
|
||
func NewSSHKeySigner(k *key.SSHTPMKey, ownerAuth func() ([]byte, error), tpm func() transport.TPMCloser, auth func(*keyfile.TPMKey) ([]byte, error)) *SSHKeySigner { | ||
return &SSHKeySigner{ | ||
TPMKeySigner: keyfile.NewTPMKeySigner(k.TPMKey, ownerAuth, tpm, auth), | ||
key: k, | ||
} | ||
} |