Skip to content
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

Alias sentinel errors for ErrNotFound and ErrExists #562

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions tpm/ak.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (ak *AK) MarshalJSON() ([]byte, error) {

// CreateAK creates and stores a new AK identified by `name`.
// If no name is provided, a random 10 character name is generated.
// If an AK with the same name exists, `ErrExists` is returned.
// If an AK with the same name exists, [ErrExists] is returned.
func (t *TPM) CreateAK(ctx context.Context, name string) (ak *AK, err error) {
if err = t.open(ctx); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
Expand All @@ -149,7 +149,7 @@ func (t *TPM) CreateAK(ctx context.Context, name string) (ak *AK, err error) {
case err == nil:
return nil, fmt.Errorf("failed creating AK %q: %w", name, ErrExists)
case errors.Is(err, storage.ErrNoStorageConfigured):
return nil, fmt.Errorf("failed creating key %q: %w", name, err)
return nil, fmt.Errorf("failed creating AK %q: %w", name, err)
}

akConfig := attest.AKConfig{
Expand Down Expand Up @@ -184,7 +184,7 @@ func (t *TPM) CreateAK(ctx context.Context, name string) (ak *AK, err error) {
return ak, nil
}

// GetAK returns the AK identified by `name`. It returns `ErrNotfound`
// GetAK returns the AK identified by `name`. It returns [ErrNotfound]
// if it doesn't exist.
func (t *TPM) GetAK(ctx context.Context, name string) (ak *AK, err error) {
if err = t.open(ctx); err != nil {
Expand All @@ -194,9 +194,6 @@ func (t *TPM) GetAK(ctx context.Context, name string) (ak *AK, err error) {

sak, err := t.store.GetAK(name)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, fmt.Errorf("failed getting AK %q: %w", name, ErrNotFound)
}
return nil, fmt.Errorf("failed getting AK %q: %w", name, err)
}

Expand All @@ -209,7 +206,7 @@ var (

// GetAKByPermanentIdentifier returns an AK for which a certificate
// exists with `permanentIdentifier` as one of the Subject Alternative
// Names. It returns `ErrNotFound` if it doesn't exist.
// Names. It returns [ErrNotFound] if it doesn't exist.
func (t *TPM) GetAKByPermanentIdentifier(ctx context.Context, permanentIdentifier string) (ak *AK, err error) {
if err = t.open(ctx); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
Expand Down Expand Up @@ -256,7 +253,7 @@ func (t *TPM) ListAKs(ctx context.Context) (aks []*AK, err error) {
return
}

// DeleteAK removes the AK identified by `name`. It returns `ErrNotfound`
// DeleteAK removes the AK identified by `name`. It returns [ErrNotfound]
// if it doesn't exist. Keys that were attested by the AK have to be removed
// before removing the AK, otherwise an error will be returned.
func (t *TPM) DeleteAK(ctx context.Context, name string) (err error) {
Expand All @@ -267,9 +264,6 @@ func (t *TPM) DeleteAK(ctx context.Context, name string) (err error) {

ak, err := t.store.GetAK(name)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("failed getting AK %q: %w", name, ErrNotFound)
}
return fmt.Errorf("failed getting AK %q: %w", name, err)
}

Expand Down
6 changes: 2 additions & 4 deletions tpm/errors.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package tpm

import (
"errors"

"go.step.sm/crypto/tpm/storage"
)

// ErrNotFound is returned when a Key or AK cannot be found
var ErrNotFound = errors.New("not found")
var ErrNotFound = storage.ErrNotFound

// ErrExists is returned when a Key or AK already exists
var ErrExists = errors.New("already exists")
var ErrExists = storage.ErrExists

// ErrNoStorageConfigured is returned when a TPM operation is
// performed that requires a storage to have been configured
Expand Down
17 changes: 4 additions & 13 deletions tpm/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type AttestKeyConfig struct {

// CreateKey creates a new Key identified by `name`. If no name is provided,
// a random 10 character name is generated. If a Key with the same name exists,
// `ErrExists` is returned. The Key won't be attested by an AK.
// [ErrExists] is returned. The Key won't be attested by an AK.
func (t *TPM) CreateKey(ctx context.Context, name string, config CreateKeyConfig) (key *Key, err error) {
if err = t.open(goTPMCall(ctx)); err != nil {
return nil, fmt.Errorf("failed opening TPM: %w", err)
Expand Down Expand Up @@ -211,7 +211,7 @@ func (w attestValidationWrapper) Validate() error {

// AttestKey creates a new Key identified by `name` and attested by the AK
// identified by `akName`. If no name is provided, a random 10 character
// name is generated. If a Key with the same name exists, `ErrExists` is
// name is generated. If a Key with the same name exists, [ErrExists] is
// returned.
func (t *TPM) AttestKey(ctx context.Context, akName, name string, config AttestKeyConfig) (key *Key, err error) {
if err = t.open(ctx); err != nil {
Expand All @@ -234,9 +234,6 @@ func (t *TPM) AttestKey(ctx context.Context, akName, name string, config AttestK

ak, err := t.store.GetAK(akName)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, fmt.Errorf("failed getting AK %q: %w", akName, ErrNotFound)
}
return nil, fmt.Errorf("failed getting AK %q: %w", akName, err)
}

Expand Down Expand Up @@ -285,7 +282,7 @@ func (t *TPM) AttestKey(ctx context.Context, akName, name string, config AttestK
return
}

// GetKey returns the Key identified by `name`. It returns `ErrNotfound`
// GetKey returns the Key identified by `name`. It returns [ErrNotfound]
// if it doesn't exist.
func (t *TPM) GetKey(ctx context.Context, name string) (key *Key, err error) {
if err = t.open(ctx); err != nil {
Expand All @@ -295,9 +292,6 @@ func (t *TPM) GetKey(ctx context.Context, name string) (key *Key, err error) {

skey, err := t.store.GetKey(name)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, fmt.Errorf("failed getting key %q: %w", name, ErrNotFound)
}
return nil, fmt.Errorf("failed getting key %q: %w", name, err)
}

Expand Down Expand Up @@ -348,7 +342,7 @@ func (t *TPM) GetKeysAttestedBy(ctx context.Context, akName string) (keys []*Key
return
}

// DeleteKey removes the Key identified by `name`. It returns `ErrNotfound`
// DeleteKey removes the Key identified by `name`. It returns [ErrNotfound]
// if it doesn't exist.
func (t *TPM) DeleteKey(ctx context.Context, name string) (err error) {
if err := t.open(ctx); err != nil {
Expand All @@ -358,9 +352,6 @@ func (t *TPM) DeleteKey(ctx context.Context, name string) (err error) {

key, err := t.store.GetKey(name)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return fmt.Errorf("failed getting key %q: %w", name, ErrNotFound)
}
return fmt.Errorf("failed getting key %q: %w", name, err)
}

Expand Down
5 changes: 0 additions & 5 deletions tpm/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ package tpm
import (
"context"
"crypto"
"errors"
"fmt"
"io"

"go.step.sm/crypto/tpm/storage"
"go.step.sm/crypto/tpm/tss2"
)

Expand Down Expand Up @@ -62,9 +60,6 @@ func (t *TPM) GetSigner(ctx context.Context, name string) (csigner crypto.Signer

key, err := t.store.GetKey(name)
if err != nil {
if errors.Is(err, storage.ErrNotFound) {
return nil, fmt.Errorf("failed getting signer for key %q: %w", name, ErrNotFound)
}
return nil, fmt.Errorf("failed getting signer for key %q: %w", name, err)
}

Expand Down
3 changes: 2 additions & 1 deletion tpm/tpm_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"strings"
"testing"

"github.com/smallstep/go-attestation/attest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smallstep/go-attestation/attest"

"go.step.sm/crypto/keyutil"
"go.step.sm/crypto/minica"
"go.step.sm/crypto/tpm/simulator"
Expand Down