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

Report which fields for selecting token were provided #42

Merged
Merged
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
19 changes: 10 additions & 9 deletions crypto11.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ package crypto11
import (
"crypto"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"time"

"github.com/vitessio/vitess/go/sync2"
Expand All @@ -113,9 +115,6 @@ var errTokenNotFound = errors.New("could not find PKCS#11 token")
// errClosed is returned if a Context is used after a call to Close.
var errClosed = errors.New("cannot used closed Context")

// errAmbiguousToken is returned if the supplied Config specifies more than one way to select the token.
var errAmbiguousToken = errors.New("config must only specify one way to select a token")

// pkcs11Object contains a reference to a loaded PKCS#11 object.
type pkcs11Object struct {
// The PKCS#11 object handle.
Expand Down Expand Up @@ -246,18 +245,20 @@ type Config struct {
// Configure creates a new Context based on the supplied PKCS#11 configuration.
func Configure(config *Config) (*Context, error) {
// Have we been given exactly one way to select a token?
count := 0
var fields []string
if config.SlotNumber != nil {
count++
fields = append(fields, "slot number")
}
if config.TokenLabel != "" {
count++
fields = append(fields, "token label")
}
if config.TokenSerial != "" {
count++
fields = append(fields, "token serial number")
}
if count != 1 {
return nil, errAmbiguousToken
if len(fields) == 0 {
return nil, fmt.Errorf("config must specify exactly one way to select a token: none given")
} else if len(fields) > 1 {
return nil, fmt.Errorf("config must specify exactly one way to select a token: %v given", strings.Join(fields, ", "))
}

if config.MaxSessions == 0 {
Expand Down
35 changes: 27 additions & 8 deletions crypto11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,34 @@ func TestKeyDelete(t *testing.T) {

func TestAmbiguousTokenConfig(t *testing.T) {
slotNum := 1
badConfigs := []*Config{
{TokenSerial: "serial", TokenLabel: "label"},
{TokenSerial: "serial", SlotNumber: &slotNum},
{SlotNumber: &slotNum, TokenLabel: "label"},
tests := []struct {
config *Config
err string
}{
{
config: &Config{TokenSerial: "serial", TokenLabel: "label"},
err: "config must specify exactly one way to select a token: token label, token serial number given",
},
{
config: &Config{TokenSerial: "serial", SlotNumber: &slotNum},
err: "config must specify exactly one way to select a token: slot number, token serial number given",
},
{
config: &Config{SlotNumber: &slotNum, TokenLabel: "label"},
err: "config must specify exactly one way to select a token: slot number, token label given",
},
{
config: &Config{},
err: "config must specify exactly one way to select a token: none given",
},
}

for _, config := range badConfigs {
_, err := Configure(config)
assert.Equal(t, errAmbiguousToken, err)
for i, test := range tests {
t.Run(fmt.Sprintf("test_%d", i), func(t *testing.T) {
_, err := Configure(test.config)
if assert.Error(t, err) {
assert.Equal(t, test.err, err.Error())
}
})
}
}

Expand Down