keycard-go/cmd/keycard/initializer.go

191 lines
4.6 KiB
Go
Raw Normal View History

2018-11-07 16:38:53 +00:00
package main
import (
"crypto/rand"
"errors"
"fmt"
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/identifiers"
"github.com/status-im/keycard-go/types"
)
var (
2018-11-06 17:38:13 +00:00
errAppletNotInstalled = errors.New("applet not installed")
errCardNotInitialized = errors.New("card not initialized")
errCardAlreadyInitialized = errors.New("card already initialized")
2018-11-28 11:42:20 +00:00
ErrNotInitialized = errors.New("card not initialized")
)
2018-11-08 14:08:03 +00:00
// 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
}
2018-11-08 14:08:03 +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-11 10:05:28 +00:00
secrets, err := keycard.NewSecrets()
2018-10-22 17:33:53 +00:00
if err != nil {
return nil, err
}
2019-03-11 11:50:16 +00:00
logger.Info("select keycard applet")
err = cmdSet.Select()
2018-10-24 16:16:14 +00:00
if err != nil {
2019-03-11 11:50:16 +00:00
logger.Error("select failed", "error", err)
2018-10-24 16:16:14 +00:00
return nil, err
}
2019-03-11 11:50:16 +00:00
if !cmdSet.ApplicationInfo.Installed {
logger.Error("initialization failed", "error", errAppletNotInstalled)
2018-11-06 17:38:13 +00:00
return nil, errAppletNotInstalled
}
2019-03-11 11:50:16 +00:00
if cmdSet.ApplicationInfo.Initialized {
logger.Error("initialization failed", "error", errCardAlreadyInitialized)
2018-11-06 17:38:13 +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)
2018-10-22 17:33:53 +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.
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-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
}
2018-11-08 14:08:03 +00:00
func (i *Initializer) initGPSecureChannel(sdaid []byte) error {
// select card manager
err := i.selectAID(sdaid)
if err != nil {
return err
}
// initialize update
session, err := i.initializeUpdate()
if err != nil {
return err
}
i.c = globalplatform.NewSecureChannel(session, i.c)
// external authenticate
return i.externalAuthenticate(session)
}
2018-11-08 14:08:03 +00:00
func (i *Initializer) selectAID(aid []byte) error {
2019-03-11 10:49:00 +00:00
sel := globalplatform.NewCommandSelect(identifiers.CardManagerAID)
_, err := i.send("select", sel)
return err
}
2018-11-08 14:08:03 +00:00
func (i *Initializer) initializeUpdate() (*globalplatform.Session, error) {
hostChallenge, err := generateHostChallenge()
if err != nil {
2018-10-04 10:10:19 +00:00
return nil, err
}
init := globalplatform.NewCommandInitializeUpdate(hostChallenge)
resp, err := i.send("initialize update", init)
if err != nil {
2018-10-04 10:10:19 +00:00
return nil, err
}
// verify cryptogram and initialize session keys
2019-03-11 10:05:28 +00:00
keys := globalplatform.NewSCP02Keys(identifiers.CardTestKey, identifiers.CardTestKey)
session, err := globalplatform.NewSession(keys, resp, hostChallenge)
return session, err
}
2018-11-08 14:08:03 +00:00
func (i *Initializer) externalAuthenticate(session *globalplatform.Session) error {
2019-03-06 09:43:37 +00:00
encKey := session.Keys().Enc()
extAuth, err := globalplatform.NewCommandExternalAuthenticate(encKey, session.CardChallenge(), session.HostChallenge())
if err != nil {
return err
}
_, err = i.send("external authenticate", extAuth)
return err
}
2018-11-08 14:08:03 +00:00
func (i *Initializer) send(description string, cmd *apdu.Command, allowedResponses ...uint16) (*apdu.Response, error) {
2018-10-05 09:35:56 +00:00
logger.Debug("sending apdu command", "name", description)
resp, err := i.c.Send(cmd)
if err != nil {
return nil, err
}
if len(allowedResponses) == 0 {
allowedResponses = []uint16{apdu.SwOK}
}
for _, code := range allowedResponses {
if code == resp.Sw {
return resp, nil
}
}
2018-10-05 14:40:32 +00:00
err = fmt.Errorf("unexpected response from command %s: %x", description, resp.Sw)
return nil, err
}
func generateHostChallenge() ([]byte, error) {
c := make([]byte, 8)
_, err := rand.Read(c)
return c, err
}