move lightwallet pkg to keycard

This commit is contained in:
Andrea Franz 2019-03-11 11:05:28 +01:00
parent 0661f1c128
commit e33b6d138a
No known key found for this signature in database
GPG Key ID: 4F0D2F2D9DE7F29D
10 changed files with 78 additions and 63 deletions

View File

@ -1,4 +1,4 @@
package actions package keycard
import ( import (
"bytes" "bytes"
@ -8,9 +8,9 @@ import (
"fmt" "fmt"
"github.com/status-im/keycard-go/apdu" "github.com/status-im/keycard-go/apdu"
"github.com/status-im/keycard-go/crypto"
"github.com/status-im/keycard-go/globalplatform" "github.com/status-im/keycard-go/globalplatform"
"github.com/status-im/keycard-go/lightwallet" "github.com/status-im/keycard-go/types"
"github.com/status-im/keycard-go/lightwallet/crypto"
) )
var ( var (
@ -19,7 +19,7 @@ var (
ErrApplicationStatusTemplateNotFound = errors.New("application status template not found") ErrApplicationStatusTemplateNotFound = errors.New("application status template not found")
) )
func Select(c globalplatform.Channel, aid []byte) (*lightwallet.ApplicationInfo, error) { func Select(c globalplatform.Channel, aid []byte) (*types.ApplicationInfo, error) {
sel := globalplatform.NewCommandSelect(aid) sel := globalplatform.NewCommandSelect(aid)
resp, err := c.Send(sel) resp, err := c.Send(sel)
if err != nil { if err != nil {
@ -31,13 +31,13 @@ func Select(c globalplatform.Channel, aid []byte) (*lightwallet.ApplicationInfo,
return nil, err return nil, err
} }
info := &lightwallet.ApplicationInfo{} info := &types.ApplicationInfo{}
if resp.Sw == globalplatform.SwFileNotFound { if resp.Sw == globalplatform.SwFileNotFound {
return info, nil return info, nil
} }
info.Installed = true info.Installed = true
if resp.Data[0] == lightwallet.TagSelectResponsePreInitialized { if resp.Data[0] == TagSelectResponsePreInitialized {
info.PublicKey = resp.Data[2:] info.PublicKey = resp.Data[2:]
return info, nil return info, nil
} }
@ -47,8 +47,8 @@ func Select(c globalplatform.Channel, aid []byte) (*lightwallet.ApplicationInfo,
return parseApplicationInfo(resp.Data, info) return parseApplicationInfo(resp.Data, info)
} }
func Init(c globalplatform.Channel, cardPubKey []byte, secrets *lightwallet.Secrets, aid []byte) error { func Init(c globalplatform.Channel, cardPubKey []byte, secrets *Secrets, aid []byte) error {
secureChannel, err := lightwallet.NewSecureChannel(c, cardPubKey) secureChannel, err := NewSecureChannel(c, cardPubKey)
if err != nil { if err != nil {
return err return err
} }
@ -58,19 +58,19 @@ func Init(c globalplatform.Channel, cardPubKey []byte, secrets *lightwallet.Secr
return err return err
} }
init := lightwallet.NewCommandInit(data) init := NewCommandInit(data)
resp, err := c.Send(init) resp, err := c.Send(init)
return checkOKResponse(err, resp) return checkOKResponse(err, resp)
} }
func Pair(c globalplatform.Channel, pairingPass string, pin string) (*lightwallet.PairingInfo, error) { func Pair(c globalplatform.Channel, pairingPass string, pin string) (*types.PairingInfo, error) {
challenge := make([]byte, 32) challenge := make([]byte, 32)
if _, err := rand.Read(challenge); err != nil { if _, err := rand.Read(challenge); err != nil {
return nil, err return nil, err
} }
cmd := lightwallet.NewCommandPairFirstStep(challenge) cmd := NewCommandPairFirstStep(challenge)
resp, err := c.Send(cmd) resp, err := c.Send(cmd)
if err = checkOKResponse(err, resp); err != nil { if err = checkOKResponse(err, resp); err != nil {
return nil, err return nil, err
@ -87,7 +87,7 @@ func Pair(c globalplatform.Channel, pairingPass string, pin string) (*lightwalle
h := sha256.New() h := sha256.New()
h.Write(secretHash[:]) h.Write(secretHash[:])
h.Write(cardChallenge) h.Write(cardChallenge)
cmd = lightwallet.NewCommandPairFinalStep(h.Sum(nil)) cmd = NewCommandPairFinalStep(h.Sum(nil))
resp, err = c.Send(cmd) resp, err = c.Send(cmd)
if err = checkOKResponse(err, resp); err != nil { if err = checkOKResponse(err, resp); err != nil {
return nil, err return nil, err
@ -100,15 +100,15 @@ func Pair(c globalplatform.Channel, pairingPass string, pin string) (*lightwalle
pairingKey := h.Sum(nil) pairingKey := h.Sum(nil)
pairingIndex := resp.Data[0] pairingIndex := resp.Data[0]
return &lightwallet.PairingInfo{ return &types.PairingInfo{
Key: pairingKey, Key: pairingKey,
Index: int(pairingIndex), Index: int(pairingIndex),
}, nil }, nil
} }
func OpenSecureChannel(c globalplatform.Channel, appInfo *lightwallet.ApplicationInfo, pairingIndex uint8, pairingKey []byte) (*lightwallet.SecureChannel, error) { func OpenSecureChannel(c globalplatform.Channel, appInfo *types.ApplicationInfo, pairingIndex uint8, pairingKey []byte) (*SecureChannel, error) {
sc, err := lightwallet.NewSecureChannel(c, appInfo.PublicKey) sc, err := NewSecureChannel(c, appInfo.PublicKey)
cmd := lightwallet.NewCommandOpenSecureChannel(pairingIndex, sc.RawPublicKey()) cmd := NewCommandOpenSecureChannel(pairingIndex, sc.RawPublicKey())
resp, err := c.Send(cmd) resp, err := c.Send(cmd)
if err = checkOKResponse(err, resp); err != nil { if err = checkOKResponse(err, resp); err != nil {
return nil, err return nil, err
@ -125,20 +125,20 @@ func OpenSecureChannel(c globalplatform.Channel, appInfo *lightwallet.Applicatio
return sc, nil return sc, nil
} }
func mutualAuthenticate(sc *lightwallet.SecureChannel) error { func mutualAuthenticate(sc *SecureChannel) error {
data := make([]byte, 32) data := make([]byte, 32)
if _, err := rand.Read(data); err != nil { if _, err := rand.Read(data); err != nil {
return err return err
} }
cmd := lightwallet.NewCommandMutuallyAuthenticate(data) cmd := NewCommandMutuallyAuthenticate(data)
resp, err := sc.Send(cmd) resp, err := sc.Send(cmd)
return checkOKResponse(err, resp) return checkOKResponse(err, resp)
} }
func GetStatusApplication(c globalplatform.Channel) (*lightwallet.ApplicationStatus, error) { func GetStatusApplication(c globalplatform.Channel) (*types.ApplicationStatus, error) {
cmd := lightwallet.NewCommandGetStatusApplication() cmd := NewCommandGetStatusApplication()
resp, err := c.Send(cmd) resp, err := c.Send(cmd)
if err = checkOKResponse(err, resp); err != nil { if err = checkOKResponse(err, resp); err != nil {
return nil, err return nil, err
@ -147,32 +147,32 @@ func GetStatusApplication(c globalplatform.Channel) (*lightwallet.ApplicationSta
return parseApplicationStatus(resp.Data) return parseApplicationStatus(resp.Data)
} }
func parseApplicationInfo(data []byte, info *lightwallet.ApplicationInfo) (*lightwallet.ApplicationInfo, error) { func parseApplicationInfo(data []byte, info *types.ApplicationInfo) (*types.ApplicationInfo, error) {
if data[0] != lightwallet.TagApplicationInfoTemplate { if data[0] != TagApplicationInfoTemplate {
return nil, ErrWrongApplicationInfoTemplate return nil, ErrWrongApplicationInfoTemplate
} }
instanceUID, err := apdu.FindTag(data, lightwallet.TagApplicationInfoTemplate, uint8(0x8F)) instanceUID, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x8F))
if err != nil { if err != nil {
return nil, err return nil, err
} }
pubKey, err := apdu.FindTag(data, lightwallet.TagApplicationInfoTemplate, uint8(0x80)) pubKey, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x80))
if err != nil { if err != nil {
return nil, err return nil, err
} }
appVersion, err := apdu.FindTag(data, lightwallet.TagApplicationInfoTemplate, uint8(0x02)) appVersion, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x02))
if err != nil { if err != nil {
return nil, err return nil, err
} }
availableSlots, err := apdu.FindTagN(data, 1, lightwallet.TagApplicationInfoTemplate, uint8(0x02)) availableSlots, err := apdu.FindTagN(data, 1, TagApplicationInfoTemplate, uint8(0x02))
if err != nil { if err != nil {
return nil, err return nil, err
} }
keyUID, err := apdu.FindTagN(data, 0, lightwallet.TagApplicationInfoTemplate, uint8(0x8E)) keyUID, err := apdu.FindTagN(data, 0, TagApplicationInfoTemplate, uint8(0x8E))
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -186,10 +186,10 @@ func parseApplicationInfo(data []byte, info *lightwallet.ApplicationInfo) (*ligh
return info, nil return info, nil
} }
func parseApplicationStatus(data []byte) (*lightwallet.ApplicationStatus, error) { func parseApplicationStatus(data []byte) (*types.ApplicationStatus, error) {
appStatus := &lightwallet.ApplicationStatus{} appStatus := &types.ApplicationStatus{}
tpl, err := apdu.FindTag(data, lightwallet.TagApplicationStatusTemplate) tpl, err := apdu.FindTag(data, TagApplicationStatusTemplate)
if err != nil { if err != nil {
return nil, ErrApplicationStatusTemplateNotFound return nil, ErrApplicationStatusTemplateNotFound
} }

View File

@ -6,10 +6,11 @@ import (
"fmt" "fmt"
"os" "os"
keycard "github.com/status-im/keycard-go"
"github.com/status-im/keycard-go/apdu" "github.com/status-im/keycard-go/apdu"
"github.com/status-im/keycard-go/globalplatform" "github.com/status-im/keycard-go/globalplatform"
"github.com/status-im/keycard-go/lightwallet" "github.com/status-im/keycard-go/identifiers"
"github.com/status-im/keycard-go/lightwallet/actions" "github.com/status-im/keycard-go/types"
) )
var ( var (
@ -34,7 +35,7 @@ func NewInitializer(t globalplatform.Transmitter) *Initializer {
// Install installs the applet from the specified capFile. // Install installs the applet from the specified capFile.
func (i *Initializer) Install(capFile *os.File, overwriteApplet bool) error { func (i *Initializer) Install(capFile *os.File, overwriteApplet bool) error {
info, err := actions.Select(i.c, lightwallet.WalletAID) info, err := keycard.Select(i.c, identifiers.KeycardAID)
if err != nil { if err != nil {
return err return err
} }
@ -43,12 +44,17 @@ func (i *Initializer) Install(capFile *os.File, overwriteApplet bool) error {
return errors.New("applet already installed") return errors.New("applet already installed")
} }
err = i.initGPSecureChannel(lightwallet.CardManagerAID) err = i.initGPSecureChannel(keycard.CardManagerAID)
if err != nil { if err != nil {
return err return err
} }
err = i.deleteAID(lightwallet.NdefInstanceAID, lightwallet.WalletInstanceAID, lightwallet.AppletPkgAID) instanceAID, err := identifiers.KeycardInstanceAID(1)
if err != nil {
return err
}
err = i.deleteAID(identifiers.NdefInstanceAID, instanceAID, identifiers.PackageAID)
if err != nil { if err != nil {
return err return err
} }
@ -61,13 +67,13 @@ func (i *Initializer) Install(capFile *os.File, overwriteApplet bool) error {
return err return err
} }
func (i *Initializer) Init() (*lightwallet.Secrets, error) { func (i *Initializer) Init() (*keycard.Secrets, error) {
secrets, err := lightwallet.NewSecrets() secrets, err := keycard.NewSecrets()
if err != nil { if err != nil {
return nil, err return nil, err
} }
info, err := actions.Select(i.c, lightwallet.WalletAID) info, err := keycard.Select(i.c, identifiers.KeycardAID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -80,7 +86,7 @@ func (i *Initializer) Init() (*lightwallet.Secrets, error) {
return nil, errCardAlreadyInitialized return nil, errCardAlreadyInitialized
} }
err = actions.Init(i.c, info.PublicKey, secrets, lightwallet.WalletAID) err = keycard.Init(i.c, info.PublicKey, secrets, identifiers.KeycardAID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -88,8 +94,8 @@ func (i *Initializer) Init() (*lightwallet.Secrets, error) {
return secrets, nil return secrets, nil
} }
func (i *Initializer) Pair(pairingPass, pin string) (*lightwallet.PairingInfo, error) { func (i *Initializer) Pair(pairingPass, pin string) (*types.PairingInfo, error) {
appInfo, err := actions.Select(i.c, lightwallet.WalletAID) appInfo, err := keycard.Select(i.c, identifiers.KeycardAID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -98,17 +104,17 @@ func (i *Initializer) Pair(pairingPass, pin string) (*lightwallet.PairingInfo, e
return nil, ErrNotInitialized return nil, ErrNotInitialized
} }
return actions.Pair(i.c, pairingPass, pin) return keycard.Pair(i.c, pairingPass, pin)
} }
// Info returns a lightwallet.ApplicationInfo struct with info about the card. // Info returns a types.ApplicationInfo struct with info about the card.
func (i *Initializer) Info() (*lightwallet.ApplicationInfo, error) { func (i *Initializer) Info() (*types.ApplicationInfo, error) {
return actions.Select(i.c, lightwallet.WalletAID) return keycard.Select(i.c, identifiers.KeycardAID)
} }
// Status returns // Status returns
func (i *Initializer) Status(index uint8, key []byte) (*lightwallet.ApplicationStatus, error) { func (i *Initializer) Status(index uint8, key []byte) (*types.ApplicationStatus, error) {
info, err := actions.Select(i.c, lightwallet.WalletAID) info, err := keycard.Select(i.c, identifiers.KeycardAID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -121,22 +127,26 @@ func (i *Initializer) Status(index uint8, key []byte) (*lightwallet.ApplicationS
return nil, errCardNotInitialized return nil, errCardNotInitialized
} }
sc, err := actions.OpenSecureChannel(i.c, info, index, key) sc, err := keycard.OpenSecureChannel(i.c, info, index, key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return actions.GetStatusApplication(sc) return keycard.GetStatusApplication(sc)
} }
// Delete deletes the applet and related package from the card. // Delete deletes the applet and related package from the card.
func (i *Initializer) Delete() error { func (i *Initializer) Delete() error {
err := i.initGPSecureChannel(lightwallet.CardManagerAID) err := i.initGPSecureChannel(keycard.CardManagerAID)
if err != nil { if err != nil {
return err return err
} }
return i.deleteAID(lightwallet.NdefInstanceAID, lightwallet.WalletInstanceAID, lightwallet.AppletPkgAID) instanceAID, err := identifiers.KeycardInstanceAID(1)
if err != nil {
return err
}
return i.deleteAID(identifiers.NdefInstanceAID, instanceAID, identifiers.PackageAID)
} }
func (i *Initializer) initGPSecureChannel(sdaid []byte) error { func (i *Initializer) initGPSecureChannel(sdaid []byte) error {
@ -159,7 +169,7 @@ func (i *Initializer) initGPSecureChannel(sdaid []byte) error {
} }
func (i *Initializer) selectAID(aid []byte) error { func (i *Initializer) selectAID(aid []byte) error {
sel := globalplatform.NewCommandSelect(lightwallet.CardManagerAID) sel := globalplatform.NewCommandSelect(keycard.CardManagerAID)
_, err := i.send("select", sel) _, err := i.send("select", sel)
return err return err
@ -178,7 +188,7 @@ func (i *Initializer) initializeUpdate() (*globalplatform.Session, error) {
} }
// verify cryptogram and initialize session keys // verify cryptogram and initialize session keys
keys := globalplatform.NewSCP02Keys(lightwallet.CardTestKey, lightwallet.CardTestKey) keys := globalplatform.NewSCP02Keys(identifiers.CardTestKey, identifiers.CardTestKey)
session, err := globalplatform.NewSession(keys, resp, hostChallenge) session, err := globalplatform.NewSession(keys, resp, hostChallenge)
return session, err return session, err
@ -210,7 +220,7 @@ func (i *Initializer) deleteAID(aids ...[]byte) error {
func (i *Initializer) installApplets(capFile *os.File) error { func (i *Initializer) installApplets(capFile *os.File) error {
// install for load // install for load
preLoad := globalplatform.NewCommandInstallForLoad(lightwallet.AppletPkgAID, lightwallet.CardManagerAID) preLoad := globalplatform.NewCommandInstallForLoad(identifiers.PackageAID, keycard.CardManagerAID)
_, err := i.send("install for load", preLoad) _, err := i.send("install for load", preLoad)
if err != nil { if err != nil {
return err return err
@ -230,13 +240,18 @@ func (i *Initializer) installApplets(capFile *os.File) error {
} }
} }
installNdef := globalplatform.NewCommandInstallForInstall(lightwallet.AppletPkgAID, lightwallet.NdefAppletAID, lightwallet.NdefInstanceAID, []byte{}) installNdef := globalplatform.NewCommandInstallForInstall(identifiers.PackageAID, identifiers.NdefAID, identifiers.NdefInstanceAID, []byte{})
_, err = i.send("install for install (ndef)", installNdef) _, err = i.send("install for install (ndef)", installNdef)
if err != nil { if err != nil {
return err return err
} }
installWallet := globalplatform.NewCommandInstallForInstall(lightwallet.AppletPkgAID, lightwallet.WalletAID, lightwallet.WalletInstanceAID, []byte{}) instanceAID, err := identifiers.KeycardInstanceAID(1)
if err != nil {
return err
}
installWallet := globalplatform.NewCommandInstallForInstall(identifiers.PackageAID, identifiers.KeycardAID, instanceAID, []byte{})
_, err = i.send("install for install (wallet)", installWallet) _, err = i.send("install for install (wallet)", installWallet)
return err return err

View File

@ -1,4 +1,4 @@
package lightwallet package keycard
import ( import (
"github.com/status-im/keycard-go/apdu" "github.com/status-im/keycard-go/apdu"

View File

@ -1,4 +1,4 @@
package lightwallet package keycard
import "github.com/ethereum/go-ethereum/log" import "github.com/ethereum/go-ethereum/log"

View File

@ -1,4 +1,4 @@
package lightwallet package keycard
import ( import (
"crypto/rand" "crypto/rand"

View File

@ -1,4 +1,4 @@
package lightwallet package keycard
import ( import (
"bytes" "bytes"
@ -7,8 +7,8 @@ import (
ethcrypto "github.com/ethereum/go-ethereum/crypto" ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/status-im/keycard-go/apdu" "github.com/status-im/keycard-go/apdu"
"github.com/status-im/keycard-go/crypto"
"github.com/status-im/keycard-go/globalplatform" "github.com/status-im/keycard-go/globalplatform"
"github.com/status-im/keycard-go/lightwallet/crypto"
) )
var ErrInvalidResponseMAC = errors.New("invalid response MAC") var ErrInvalidResponseMAC = errors.New("invalid response MAC")

View File

@ -1,4 +1,4 @@
package lightwallet package keycard
import ( import (
"errors" "errors"

View File

@ -1,4 +1,4 @@
package lightwallet package types
type ApplicationInfo struct { type ApplicationInfo struct {
Installed bool Installed bool