From e33b6d138a428a8e048b4d931af76f3972c24f86 Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Mon, 11 Mar 2019 11:05:28 +0100 Subject: [PATCH] move lightwallet pkg to keycard --- lightwallet/actions/actions.go => actions.go | 60 ++++++++--------- cmd/keycard/initializer.go | 67 ++++++++++++------- lightwallet/commands.go => commands.go | 2 +- {lightwallet/crypto => crypto}/crypto.go | 0 {lightwallet/crypto => crypto}/crypto_test.go | 0 lightwallet/lightwallet.go => keycard.go | 2 +- lightwallet/secrets.go => secrets.go | 2 +- .../secure_channel.go => secure_channel.go | 4 +- ..._channel_test.go => secure_channel_test.go | 2 +- {lightwallet => types}/types.go | 2 +- 10 files changed, 78 insertions(+), 63 deletions(-) rename lightwallet/actions/actions.go => actions.go (66%) rename lightwallet/commands.go => commands.go (98%) rename {lightwallet/crypto => crypto}/crypto.go (100%) rename {lightwallet/crypto => crypto}/crypto_test.go (100%) rename lightwallet/lightwallet.go => keycard.go (97%) rename lightwallet/secrets.go => secrets.go (98%) rename lightwallet/secure_channel.go => secure_channel.go (97%) rename lightwallet/secure_channel_test.go => secure_channel_test.go (98%) rename {lightwallet => types}/types.go (95%) diff --git a/lightwallet/actions/actions.go b/actions.go similarity index 66% rename from lightwallet/actions/actions.go rename to actions.go index 4cc7ef0..13c7cb0 100644 --- a/lightwallet/actions/actions.go +++ b/actions.go @@ -1,4 +1,4 @@ -package actions +package keycard import ( "bytes" @@ -8,9 +8,9 @@ import ( "fmt" "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/lightwallet" - "github.com/status-im/keycard-go/lightwallet/crypto" + "github.com/status-im/keycard-go/types" ) var ( @@ -19,7 +19,7 @@ var ( 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) resp, err := c.Send(sel) if err != nil { @@ -31,13 +31,13 @@ func Select(c globalplatform.Channel, aid []byte) (*lightwallet.ApplicationInfo, return nil, err } - info := &lightwallet.ApplicationInfo{} + info := &types.ApplicationInfo{} if resp.Sw == globalplatform.SwFileNotFound { return info, nil } info.Installed = true - if resp.Data[0] == lightwallet.TagSelectResponsePreInitialized { + if resp.Data[0] == TagSelectResponsePreInitialized { info.PublicKey = resp.Data[2:] return info, nil } @@ -47,8 +47,8 @@ func Select(c globalplatform.Channel, aid []byte) (*lightwallet.ApplicationInfo, return parseApplicationInfo(resp.Data, info) } -func Init(c globalplatform.Channel, cardPubKey []byte, secrets *lightwallet.Secrets, aid []byte) error { - secureChannel, err := lightwallet.NewSecureChannel(c, cardPubKey) +func Init(c globalplatform.Channel, cardPubKey []byte, secrets *Secrets, aid []byte) error { + secureChannel, err := NewSecureChannel(c, cardPubKey) if err != nil { return err } @@ -58,19 +58,19 @@ func Init(c globalplatform.Channel, cardPubKey []byte, secrets *lightwallet.Secr return err } - init := lightwallet.NewCommandInit(data) + init := NewCommandInit(data) resp, err := c.Send(init) 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) if _, err := rand.Read(challenge); err != nil { return nil, err } - cmd := lightwallet.NewCommandPairFirstStep(challenge) + cmd := NewCommandPairFirstStep(challenge) resp, err := c.Send(cmd) if err = checkOKResponse(err, resp); err != nil { return nil, err @@ -87,7 +87,7 @@ func Pair(c globalplatform.Channel, pairingPass string, pin string) (*lightwalle h := sha256.New() h.Write(secretHash[:]) h.Write(cardChallenge) - cmd = lightwallet.NewCommandPairFinalStep(h.Sum(nil)) + cmd = NewCommandPairFinalStep(h.Sum(nil)) resp, err = c.Send(cmd) if err = checkOKResponse(err, resp); err != nil { return nil, err @@ -100,15 +100,15 @@ func Pair(c globalplatform.Channel, pairingPass string, pin string) (*lightwalle pairingKey := h.Sum(nil) pairingIndex := resp.Data[0] - return &lightwallet.PairingInfo{ + return &types.PairingInfo{ Key: pairingKey, Index: int(pairingIndex), }, nil } -func OpenSecureChannel(c globalplatform.Channel, appInfo *lightwallet.ApplicationInfo, pairingIndex uint8, pairingKey []byte) (*lightwallet.SecureChannel, error) { - sc, err := lightwallet.NewSecureChannel(c, appInfo.PublicKey) - cmd := lightwallet.NewCommandOpenSecureChannel(pairingIndex, sc.RawPublicKey()) +func OpenSecureChannel(c globalplatform.Channel, appInfo *types.ApplicationInfo, pairingIndex uint8, pairingKey []byte) (*SecureChannel, error) { + sc, err := NewSecureChannel(c, appInfo.PublicKey) + cmd := NewCommandOpenSecureChannel(pairingIndex, sc.RawPublicKey()) resp, err := c.Send(cmd) if err = checkOKResponse(err, resp); err != nil { return nil, err @@ -125,20 +125,20 @@ func OpenSecureChannel(c globalplatform.Channel, appInfo *lightwallet.Applicatio return sc, nil } -func mutualAuthenticate(sc *lightwallet.SecureChannel) error { +func mutualAuthenticate(sc *SecureChannel) error { data := make([]byte, 32) if _, err := rand.Read(data); err != nil { return err } - cmd := lightwallet.NewCommandMutuallyAuthenticate(data) + cmd := NewCommandMutuallyAuthenticate(data) resp, err := sc.Send(cmd) return checkOKResponse(err, resp) } -func GetStatusApplication(c globalplatform.Channel) (*lightwallet.ApplicationStatus, error) { - cmd := lightwallet.NewCommandGetStatusApplication() +func GetStatusApplication(c globalplatform.Channel) (*types.ApplicationStatus, error) { + cmd := NewCommandGetStatusApplication() resp, err := c.Send(cmd) if err = checkOKResponse(err, resp); err != nil { return nil, err @@ -147,32 +147,32 @@ func GetStatusApplication(c globalplatform.Channel) (*lightwallet.ApplicationSta return parseApplicationStatus(resp.Data) } -func parseApplicationInfo(data []byte, info *lightwallet.ApplicationInfo) (*lightwallet.ApplicationInfo, error) { - if data[0] != lightwallet.TagApplicationInfoTemplate { +func parseApplicationInfo(data []byte, info *types.ApplicationInfo) (*types.ApplicationInfo, error) { + if data[0] != TagApplicationInfoTemplate { return nil, ErrWrongApplicationInfoTemplate } - instanceUID, err := apdu.FindTag(data, lightwallet.TagApplicationInfoTemplate, uint8(0x8F)) + instanceUID, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x8F)) if err != nil { return nil, err } - pubKey, err := apdu.FindTag(data, lightwallet.TagApplicationInfoTemplate, uint8(0x80)) + pubKey, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x80)) if err != nil { return nil, err } - appVersion, err := apdu.FindTag(data, lightwallet.TagApplicationInfoTemplate, uint8(0x02)) + appVersion, err := apdu.FindTag(data, TagApplicationInfoTemplate, uint8(0x02)) if err != nil { 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 { 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 { return nil, err } @@ -186,10 +186,10 @@ func parseApplicationInfo(data []byte, info *lightwallet.ApplicationInfo) (*ligh return info, nil } -func parseApplicationStatus(data []byte) (*lightwallet.ApplicationStatus, error) { - appStatus := &lightwallet.ApplicationStatus{} +func parseApplicationStatus(data []byte) (*types.ApplicationStatus, error) { + appStatus := &types.ApplicationStatus{} - tpl, err := apdu.FindTag(data, lightwallet.TagApplicationStatusTemplate) + tpl, err := apdu.FindTag(data, TagApplicationStatusTemplate) if err != nil { return nil, ErrApplicationStatusTemplateNotFound } diff --git a/cmd/keycard/initializer.go b/cmd/keycard/initializer.go index 788ddae..a3c1fc1 100644 --- a/cmd/keycard/initializer.go +++ b/cmd/keycard/initializer.go @@ -6,10 +6,11 @@ import ( "fmt" "os" + keycard "github.com/status-im/keycard-go" "github.com/status-im/keycard-go/apdu" "github.com/status-im/keycard-go/globalplatform" - "github.com/status-im/keycard-go/lightwallet" - "github.com/status-im/keycard-go/lightwallet/actions" + "github.com/status-im/keycard-go/identifiers" + "github.com/status-im/keycard-go/types" ) var ( @@ -34,7 +35,7 @@ func NewInitializer(t globalplatform.Transmitter) *Initializer { // Install installs the applet from the specified capFile. 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 { return err } @@ -43,12 +44,17 @@ func (i *Initializer) Install(capFile *os.File, overwriteApplet bool) error { return errors.New("applet already installed") } - err = i.initGPSecureChannel(lightwallet.CardManagerAID) + err = i.initGPSecureChannel(keycard.CardManagerAID) if err != nil { 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 { return err } @@ -61,13 +67,13 @@ func (i *Initializer) Install(capFile *os.File, overwriteApplet bool) error { return err } -func (i *Initializer) Init() (*lightwallet.Secrets, error) { - secrets, err := lightwallet.NewSecrets() +func (i *Initializer) Init() (*keycard.Secrets, error) { + secrets, err := keycard.NewSecrets() if err != nil { return nil, err } - info, err := actions.Select(i.c, lightwallet.WalletAID) + info, err := keycard.Select(i.c, identifiers.KeycardAID) if err != nil { return nil, err } @@ -80,7 +86,7 @@ func (i *Initializer) Init() (*lightwallet.Secrets, error) { 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 { return nil, err } @@ -88,8 +94,8 @@ func (i *Initializer) Init() (*lightwallet.Secrets, error) { return secrets, nil } -func (i *Initializer) Pair(pairingPass, pin string) (*lightwallet.PairingInfo, error) { - appInfo, err := actions.Select(i.c, lightwallet.WalletAID) +func (i *Initializer) Pair(pairingPass, pin string) (*types.PairingInfo, error) { + appInfo, err := keycard.Select(i.c, identifiers.KeycardAID) if err != nil { return nil, err } @@ -98,17 +104,17 @@ func (i *Initializer) Pair(pairingPass, pin string) (*lightwallet.PairingInfo, e 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. -func (i *Initializer) Info() (*lightwallet.ApplicationInfo, error) { - return actions.Select(i.c, lightwallet.WalletAID) +// Info returns a types.ApplicationInfo struct with info about the card. +func (i *Initializer) Info() (*types.ApplicationInfo, error) { + return keycard.Select(i.c, identifiers.KeycardAID) } // Status returns -func (i *Initializer) Status(index uint8, key []byte) (*lightwallet.ApplicationStatus, error) { - info, err := actions.Select(i.c, lightwallet.WalletAID) +func (i *Initializer) Status(index uint8, key []byte) (*types.ApplicationStatus, error) { + info, err := keycard.Select(i.c, identifiers.KeycardAID) if err != nil { return nil, err } @@ -121,22 +127,26 @@ func (i *Initializer) Status(index uint8, key []byte) (*lightwallet.ApplicationS return nil, errCardNotInitialized } - sc, err := actions.OpenSecureChannel(i.c, info, index, key) + sc, err := keycard.OpenSecureChannel(i.c, info, index, key) if err != nil { return nil, err } - return actions.GetStatusApplication(sc) + return keycard.GetStatusApplication(sc) } // Delete deletes the applet and related package from the card. func (i *Initializer) Delete() error { - err := i.initGPSecureChannel(lightwallet.CardManagerAID) + err := i.initGPSecureChannel(keycard.CardManagerAID) if err != nil { 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 { @@ -159,7 +169,7 @@ func (i *Initializer) initGPSecureChannel(sdaid []byte) error { } func (i *Initializer) selectAID(aid []byte) error { - sel := globalplatform.NewCommandSelect(lightwallet.CardManagerAID) + sel := globalplatform.NewCommandSelect(keycard.CardManagerAID) _, err := i.send("select", sel) return err @@ -178,7 +188,7 @@ func (i *Initializer) initializeUpdate() (*globalplatform.Session, error) { } // 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) return session, err @@ -210,7 +220,7 @@ func (i *Initializer) deleteAID(aids ...[]byte) error { func (i *Initializer) installApplets(capFile *os.File) error { // install for load - preLoad := globalplatform.NewCommandInstallForLoad(lightwallet.AppletPkgAID, lightwallet.CardManagerAID) + preLoad := globalplatform.NewCommandInstallForLoad(identifiers.PackageAID, keycard.CardManagerAID) _, err := i.send("install for load", preLoad) if err != nil { 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) if err != nil { 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) return err diff --git a/lightwallet/commands.go b/commands.go similarity index 98% rename from lightwallet/commands.go rename to commands.go index da7e36d..039a62e 100644 --- a/lightwallet/commands.go +++ b/commands.go @@ -1,4 +1,4 @@ -package lightwallet +package keycard import ( "github.com/status-im/keycard-go/apdu" diff --git a/lightwallet/crypto/crypto.go b/crypto/crypto.go similarity index 100% rename from lightwallet/crypto/crypto.go rename to crypto/crypto.go diff --git a/lightwallet/crypto/crypto_test.go b/crypto/crypto_test.go similarity index 100% rename from lightwallet/crypto/crypto_test.go rename to crypto/crypto_test.go diff --git a/lightwallet/lightwallet.go b/keycard.go similarity index 97% rename from lightwallet/lightwallet.go rename to keycard.go index cd5c943..6911900 100644 --- a/lightwallet/lightwallet.go +++ b/keycard.go @@ -1,4 +1,4 @@ -package lightwallet +package keycard import "github.com/ethereum/go-ethereum/log" diff --git a/lightwallet/secrets.go b/secrets.go similarity index 98% rename from lightwallet/secrets.go rename to secrets.go index 6336477..85e06b4 100644 --- a/lightwallet/secrets.go +++ b/secrets.go @@ -1,4 +1,4 @@ -package lightwallet +package keycard import ( "crypto/rand" diff --git a/lightwallet/secure_channel.go b/secure_channel.go similarity index 97% rename from lightwallet/secure_channel.go rename to secure_channel.go index 97c91e9..17eec66 100644 --- a/lightwallet/secure_channel.go +++ b/secure_channel.go @@ -1,4 +1,4 @@ -package lightwallet +package keycard import ( "bytes" @@ -7,8 +7,8 @@ import ( ethcrypto "github.com/ethereum/go-ethereum/crypto" "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/lightwallet/crypto" ) var ErrInvalidResponseMAC = errors.New("invalid response MAC") diff --git a/lightwallet/secure_channel_test.go b/secure_channel_test.go similarity index 98% rename from lightwallet/secure_channel_test.go rename to secure_channel_test.go index 010f9c4..2408ec7 100644 --- a/lightwallet/secure_channel_test.go +++ b/secure_channel_test.go @@ -1,4 +1,4 @@ -package lightwallet +package keycard import ( "errors" diff --git a/lightwallet/types.go b/types/types.go similarity index 95% rename from lightwallet/types.go rename to types/types.go index f271b00..5cbb52f 100644 --- a/lightwallet/types.go +++ b/types/types.go @@ -1,4 +1,4 @@ -package lightwallet +package types type ApplicationInfo struct { Installed bool