2018-10-24 11:42:00 +00:00
|
|
|
package actionsets
|
2018-10-02 11:25:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-10-03 14:26:57 +00:00
|
|
|
"os"
|
2018-10-02 11:25:04 +00:00
|
|
|
|
2018-11-06 09:25:54 +00:00
|
|
|
"github.com/status-im/hardware-wallet-go/apdu"
|
|
|
|
"github.com/status-im/hardware-wallet-go/globalplatform"
|
|
|
|
"github.com/status-im/hardware-wallet-go/lightwallet"
|
|
|
|
"github.com/status-im/hardware-wallet-go/lightwallet/actions"
|
2018-10-02 11:25:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-10-04 13:05:12 +00:00
|
|
|
cardManagerAID = []byte{0xa0, 0x00, 0x00, 0x01, 0x51, 0x00, 0x00, 0x00}
|
|
|
|
testKey = []byte{0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f}
|
2018-10-02 14:52:50 +00:00
|
|
|
|
2018-10-19 10:39:11 +00:00
|
|
|
pkgAID = []byte{0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x57, 0x61, 0x6C, 0x6C, 0x65, 0x74}
|
|
|
|
// applet and instance aid
|
|
|
|
walletAID = []byte{0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x57, 0x61, 0x6C, 0x6C, 0x65, 0x74, 0x41, 0x70, 0x70}
|
|
|
|
ndefAppletAID = []byte{0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x57, 0x61, 0x6C, 0x6C, 0x65, 0x74, 0x4E, 0x46, 0x43}
|
|
|
|
ndefInstanceAID = []byte{0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01}
|
2018-10-02 11:25:04 +00:00
|
|
|
)
|
|
|
|
|
2018-10-05 14:40:32 +00:00
|
|
|
// Installer defines a struct with methods to install an applet to a smartcard.
|
2018-10-02 11:25:04 +00:00
|
|
|
type Installer struct {
|
2018-10-05 11:53:35 +00:00
|
|
|
c globalplatform.Channel
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 14:40:32 +00:00
|
|
|
// NewInstaller returns a new Installer that communicates to Transmitter t.
|
2018-10-05 11:53:35 +00:00
|
|
|
func NewInstaller(t globalplatform.Transmitter) *Installer {
|
2018-10-02 11:25:04 +00:00
|
|
|
return &Installer{
|
2018-10-05 11:53:35 +00:00
|
|
|
c: globalplatform.NewNormalChannel(t),
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:40:32 +00:00
|
|
|
// Install installs the applet from the specified capFile.
|
2018-10-22 17:33:53 +00:00
|
|
|
func (i *Installer) Install(capFile *os.File, overwriteApplet bool) error {
|
2018-10-24 16:16:14 +00:00
|
|
|
err := i.initGPSecureChannel(cardManagerAID)
|
2018-10-02 11:25:04 +00:00
|
|
|
if err != nil {
|
2018-10-22 17:33:53 +00:00
|
|
|
return err
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 15:02:10 +00:00
|
|
|
installed, err := i.isAppletInstalled()
|
2018-10-04 14:06:55 +00:00
|
|
|
if err != nil {
|
2018-10-22 17:33:53 +00:00
|
|
|
return err
|
2018-10-04 14:06:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if installed && !overwriteApplet {
|
2018-10-22 17:33:53 +00:00
|
|
|
return errors.New("applet already installed")
|
2018-10-04 14:06:55 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 10:39:11 +00:00
|
|
|
err = i.deleteAID(ndefInstanceAID, walletAID, pkgAID)
|
2018-10-02 11:25:04 +00:00
|
|
|
if err != nil {
|
2018-10-22 17:33:53 +00:00
|
|
|
return err
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:45:35 +00:00
|
|
|
err = i.installApplets(capFile)
|
2018-10-02 11:25:04 +00:00
|
|
|
if err != nil {
|
2018-10-22 17:33:53 +00:00
|
|
|
return err
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-22 17:33:53 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-24 11:42:00 +00:00
|
|
|
func (i *Installer) Init() (*lightwallet.Secrets, error) {
|
|
|
|
secrets, err := lightwallet.NewSecrets()
|
2018-10-22 17:33:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-10-24 16:16:14 +00:00
|
|
|
cardPubKey, err := actions.SelectNotInitialized(i.c, walletAID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = actions.Init(i.c, cardPubKey, secrets, walletAID)
|
2018-10-22 17:33:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return secrets, nil
|
2018-10-04 13:05:12 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 16:16:14 +00:00
|
|
|
func (i *Installer) Pair(pairingPass, pin string) (*lightwallet.PairingInfo, error) {
|
|
|
|
_, err := actions.SelectInitialized(i.c, walletAID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return actions.Pair(i.c, pairingPass, pin)
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:40:32 +00:00
|
|
|
// Info returns if the applet is already installed in the card.
|
2018-10-04 15:02:10 +00:00
|
|
|
func (i *Installer) Info() (bool, error) {
|
2018-10-24 16:16:14 +00:00
|
|
|
err := i.initGPSecureChannel(cardManagerAID)
|
2018-10-04 15:02:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.isAppletInstalled()
|
|
|
|
}
|
|
|
|
|
2018-10-05 14:40:32 +00:00
|
|
|
// Delete deletes the applet and related package from the card.
|
2018-10-04 15:02:10 +00:00
|
|
|
func (i *Installer) Delete() error {
|
2018-10-24 16:16:14 +00:00
|
|
|
err := i.initGPSecureChannel(cardManagerAID)
|
2018-10-04 15:02:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 10:39:11 +00:00
|
|
|
return i.deleteAID(ndefInstanceAID, walletAID, pkgAID)
|
2018-10-04 15:02:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installer) isAppletInstalled() (bool, error) {
|
2018-10-19 10:39:11 +00:00
|
|
|
cmd := globalplatform.NewCommandGetStatus(walletAID, globalplatform.P1GetStatusApplications)
|
2018-10-04 14:06:55 +00:00
|
|
|
resp, err := i.send("get status", cmd, globalplatform.SwOK, globalplatform.SwReferencedDataNotFound)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Sw == globalplatform.SwReferencedDataNotFound {
|
|
|
|
return false, nil
|
|
|
|
}
|
2018-10-04 13:05:12 +00:00
|
|
|
|
2018-10-04 14:06:55 +00:00
|
|
|
return true, nil
|
2018-10-04 13:05:12 +00:00
|
|
|
}
|
|
|
|
|
2018-10-24 16:16:14 +00:00
|
|
|
func (i *Installer) initGPSecureChannel(sdaid []byte) error {
|
2018-10-04 13:05:12 +00:00
|
|
|
// select card manager
|
|
|
|
err := i.selectAID(sdaid)
|
2018-10-02 11:25:04 +00:00
|
|
|
if err != nil {
|
2018-10-04 13:05:12 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize update
|
|
|
|
session, err := i.initializeUpdate()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 11:53:35 +00:00
|
|
|
i.c = globalplatform.NewSecureChannel(session, i.c)
|
2018-10-02 11:25:04 +00:00
|
|
|
|
|
|
|
// external authenticate
|
2018-10-04 13:05:12 +00:00
|
|
|
return i.externalAuthenticate(session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installer) selectAID(aid []byte) error {
|
|
|
|
sel := globalplatform.NewCommandSelect(cardManagerAID)
|
|
|
|
_, err := i.send("select", sel)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installer) initializeUpdate() (*globalplatform.Session, error) {
|
|
|
|
hostChallenge, err := generateHostChallenge()
|
2018-10-02 11:25:04 +00:00
|
|
|
if err != nil {
|
2018-10-04 10:10:19 +00:00
|
|
|
return nil, err
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:05:12 +00:00
|
|
|
init := globalplatform.NewCommandInitializeUpdate(hostChallenge)
|
|
|
|
resp, err := i.send("initialize update", init)
|
2018-10-02 11:25:04 +00:00
|
|
|
if err != nil {
|
2018-10-04 10:10:19 +00:00
|
|
|
return nil, err
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:05:12 +00:00
|
|
|
// verify cryptogram and initialize session keys
|
|
|
|
keys := globalplatform.NewKeyProvider(testKey, testKey)
|
|
|
|
session, err := globalplatform.NewSession(keys, resp, hostChallenge)
|
|
|
|
|
|
|
|
return session, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installer) externalAuthenticate(session *globalplatform.Session) error {
|
|
|
|
encKey := session.KeyProvider().Enc()
|
|
|
|
extAuth, err := globalplatform.NewCommandExternalAuthenticate(encKey, session.CardChallenge(), session.HostChallenge())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-10-02 14:52:50 +00:00
|
|
|
}
|
|
|
|
|
2018-10-04 13:05:12 +00:00
|
|
|
_, err = i.send("external authenticate", extAuth)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installer) deleteAID(aids ...[]byte) error {
|
2018-10-02 14:52:50 +00:00
|
|
|
for _, aid := range aids {
|
|
|
|
del := globalplatform.NewCommandDelete(aid)
|
2018-10-04 13:05:12 +00:00
|
|
|
_, err := i.send("delete", del, globalplatform.SwOK, globalplatform.SwReferencedDataNotFound)
|
2018-10-02 14:52:50 +00:00
|
|
|
if err != nil {
|
2018-10-04 13:05:12 +00:00
|
|
|
return err
|
2018-10-02 14:52:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 13:05:12 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:45:35 +00:00
|
|
|
func (i *Installer) installApplets(capFile *os.File) error {
|
2018-10-03 14:26:57 +00:00
|
|
|
// install for load
|
2018-10-19 10:39:11 +00:00
|
|
|
preLoad := globalplatform.NewCommandInstallForLoad(pkgAID, cardManagerAID)
|
2018-10-04 13:05:12 +00:00
|
|
|
_, err := i.send("install for load", preLoad)
|
2018-10-03 14:26:57 +00:00
|
|
|
if err != nil {
|
2018-10-19 11:45:35 +00:00
|
|
|
return err
|
2018-10-03 14:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// load
|
|
|
|
load, err := globalplatform.NewLoadCommandStream(capFile)
|
|
|
|
if err != nil {
|
2018-10-19 11:45:35 +00:00
|
|
|
return err
|
2018-10-03 14:26:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for load.Next() {
|
|
|
|
cmd := load.GetCommand()
|
2018-10-19 11:45:35 +00:00
|
|
|
_, err = i.send(fmt.Sprintf("load %d of 36", load.Index()), cmd)
|
2018-10-03 14:26:57 +00:00
|
|
|
if err != nil {
|
2018-10-19 11:45:35 +00:00
|
|
|
return err
|
2018-10-03 14:26:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 11:45:35 +00:00
|
|
|
installNdef := globalplatform.NewCommandInstallForInstall(pkgAID, ndefAppletAID, ndefInstanceAID, []byte{})
|
|
|
|
_, err = i.send("install for install (ndef)", installNdef)
|
2018-10-04 10:10:19 +00:00
|
|
|
if err != nil {
|
2018-10-19 11:45:35 +00:00
|
|
|
return err
|
2018-10-04 10:10:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:45:35 +00:00
|
|
|
installWallet := globalplatform.NewCommandInstallForInstall(pkgAID, walletAID, walletAID, []byte{})
|
|
|
|
_, err = i.send("install for install (wallet)", installWallet)
|
2018-10-04 10:10:19 +00:00
|
|
|
|
2018-10-19 11:45:35 +00:00
|
|
|
return err
|
2018-10-02 11:25:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Installer) 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)
|
2018-10-02 11:25:04 +00:00
|
|
|
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)
|
2018-10-02 11:25:04 +00:00
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateHostChallenge() ([]byte, error) {
|
|
|
|
c := make([]byte, 8)
|
|
|
|
_, err := rand.Read(c)
|
|
|
|
return c, err
|
|
|
|
}
|