keycard-cli/initializer.go

137 lines
3.4 KiB
Go
Raw Normal View History

2019-03-01 17:44:07 +00:00
package main
import (
"errors"
2019-03-11 10:05:28 +00:00
keycard "github.com/status-im/keycard-go"
2019-03-01 17:44:07 +00:00
"github.com/status-im/keycard-go/apdu"
"github.com/status-im/keycard-go/globalplatform"
2019-03-11 10:05:28 +00:00
"github.com/status-im/keycard-go/types"
2019-03-01 17:44:07 +00:00
)
var (
errAppletNotInstalled = errors.New("applet not installed")
errCardNotInitialized = errors.New("card not initialized")
errCardAlreadyInitialized = errors.New("card already initialized")
ErrNotInitialized = errors.New("card not initialized")
)
// Initializer defines a struct with methods to install applets and initialize a card.
type Initializer struct {
2019-03-11 10:49:00 +00:00
c types.Channel
2019-03-01 17:44:07 +00:00
}
// NewInitializer returns a new Initializer that communicates to Transmitter t.
func NewInitializer(t globalplatform.Transmitter) *Initializer {
return &Initializer{
c: globalplatform.NewNormalChannel(t),
}
}
2019-03-11 10:05:28 +00:00
func (i *Initializer) Init() (*keycard.Secrets, error) {
2019-03-11 11:50:16 +00:00
logger.Info("initialization started")
cmdSet := keycard.NewCommandSet(i.c)
2019-03-14 15:04:36 +00:00
secrets, err := keycard.GenerateSecrets()
2019-03-01 17:44:07 +00:00
if err != nil {
return nil, err
}
2019-03-11 11:50:16 +00:00
logger.Info("select keycard applet")
err = cmdSet.Select()
2019-03-01 17:44:07 +00:00
if err != nil {
2019-03-11 11:50:16 +00:00
logger.Error("select failed", "error", err)
2019-03-01 17:44:07 +00:00
return nil, err
}
2019-03-11 11:50:16 +00:00
if !cmdSet.ApplicationInfo.Installed {
logger.Error("initialization failed", "error", errAppletNotInstalled)
2019-03-01 17:44:07 +00:00
return nil, errAppletNotInstalled
}
2019-03-11 11:50:16 +00:00
if cmdSet.ApplicationInfo.Initialized {
logger.Error("initialization failed", "error", errCardAlreadyInitialized)
2019-03-01 17:44:07 +00:00
return nil, errCardAlreadyInitialized
}
2019-03-13 12:49:26 +00:00
logger.Info("initializing")
2019-03-11 11:50:16 +00:00
err = cmdSet.Init(secrets)
2019-03-01 17:44:07 +00:00
if err != nil {
return nil, err
}
return secrets, nil
}
2019-03-11 10:05:28 +00:00
// Info returns a types.ApplicationInfo struct with info about the card.
2019-03-13 15:20:51 +00:00
func (i *Initializer) Info() (*types.ApplicationInfo, error) {
2019-03-13 12:49:26 +00:00
logger.Info("info started")
cmdSet := keycard.NewCommandSet(i.c)
2019-03-13 12:49:26 +00:00
logger.Info("select keycard applet")
err := cmdSet.Select()
if e, ok := err.(*apdu.ErrBadResponse); ok && e.Sw == globalplatform.SwFileNotFound {
err = nil
2019-03-13 12:49:26 +00:00
} else {
logger.Error("select failed", "error", err)
}
return cmdSet.ApplicationInfo, err
2019-03-01 17:44:07 +00:00
}
2019-03-13 12:49:26 +00:00
func (i *Initializer) Pair(pairingPass string) (*types.PairingInfo, error) {
logger.Info("pairing started")
cmdSet := keycard.NewCommandSet(i.c)
logger.Info("select keycard applet")
err := cmdSet.Select()
if err != nil {
logger.Error("select failed", "error", err)
return nil, err
}
if !cmdSet.ApplicationInfo.Initialized {
logger.Error("pairing failed", "error", ErrNotInitialized)
return nil, ErrNotInitialized
}
logger.Info("pairing")
err = cmdSet.Pair(pairingPass)
return cmdSet.PairingInfo, err
}
2019-03-13 15:20:51 +00:00
func (i *Initializer) Status(key []byte, index int) (*types.ApplicationStatus, error) {
logger.Info("pairing started")
cmdSet := keycard.NewCommandSet(i.c)
logger.Info("select keycard applet")
err := cmdSet.Select()
if err != nil {
logger.Error("select failed", "error", err)
return nil, err
}
if !cmdSet.ApplicationInfo.Initialized {
logger.Error("pairing failed", "error", ErrNotInitialized)
return nil, ErrNotInitialized
}
logger.Info("open secure channel")
cmdSet.SetPairingInfo(key, index)
err = cmdSet.OpenSecureChannel()
if err != nil {
logger.Error("open secure channel failed", "error", err)
return nil, err
}
logger.Info("get status")
appStatus, err := cmdSet.GetStatus()
if err != nil {
logger.Error("get status failed", "error", err)
return nil, err
}
return appStatus, nil
}