add identifiers package
This commit is contained in:
parent
cfdc2c0eed
commit
3c0ece1eec
|
@ -7,22 +7,13 @@ import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/status-im/keycard-go/apdu"
|
"github.com/status-im/keycard-go/apdu"
|
||||||
)
|
"github.com/status-im/keycard-go/identifiers"
|
||||||
|
|
||||||
var (
|
|
||||||
CardManagerAID = []byte{0xa0, 0x00, 0x00, 0x01, 0x51, 0x00, 0x00, 0x00}
|
|
||||||
CardTestKey = []byte{0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f}
|
|
||||||
AppletPkgAID = []byte{0xA0, 0x00, 0x00, 0x08, 0x04, 0x00, 0x01}
|
|
||||||
|
|
||||||
WalletAID = []byte{0xA0, 0x00, 0x00, 0x08, 0x04, 0x00, 0x01, 0x01}
|
|
||||||
WalletInstanceAID = []byte{0xA0, 0x00, 0x00, 0x08, 0x04, 0x00, 0x01, 0x01, 0x01}
|
|
||||||
|
|
||||||
NdefAppletAID = []byte{0xA0, 0x00, 0x00, 0x08, 0x04, 0x00, 0x01, 0x02}
|
|
||||||
NdefInstanceAID = []byte{0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type LoadingCallback = func(loadingBlock, totalBlocks int)
|
type LoadingCallback = func(loadingBlock, totalBlocks int)
|
||||||
|
|
||||||
|
const defaultKeycardInstanceAID = 1
|
||||||
|
|
||||||
type CommandSet struct {
|
type CommandSet struct {
|
||||||
c Channel
|
c Channel
|
||||||
session *Session
|
session *Session
|
||||||
|
@ -40,7 +31,7 @@ func (cs *CommandSet) Select() error {
|
||||||
InsSelect,
|
InsSelect,
|
||||||
uint8(0x04),
|
uint8(0x04),
|
||||||
uint8(0x00),
|
uint8(0x00),
|
||||||
CardManagerAID,
|
identifiers.CardManagerAID,
|
||||||
)
|
)
|
||||||
|
|
||||||
resp, err := cs.c.Send(cmd)
|
resp, err := cs.c.Send(cmd)
|
||||||
|
@ -62,10 +53,15 @@ func (cs *CommandSet) OpenSecureChannel() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CommandSet) DeleteKeycardInstancesAndPackage() error {
|
func (cs *CommandSet) DeleteKeycardInstancesAndPackage() error {
|
||||||
|
instanceAID, err := identifiers.KeycardInstanceAID(defaultKeycardInstanceAID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
ids := [][]byte{
|
ids := [][]byte{
|
||||||
NdefInstanceAID,
|
identifiers.NdefInstanceAID,
|
||||||
WalletInstanceAID,
|
instanceAID,
|
||||||
AppletPkgAID,
|
identifiers.PackageAID,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
|
@ -80,7 +76,7 @@ func (cs *CommandSet) DeleteKeycardInstancesAndPackage() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CommandSet) LoadKeycardPackage(capFile *os.File, callback LoadingCallback) error {
|
func (cs *CommandSet) LoadKeycardPackage(capFile *os.File, callback LoadingCallback) error {
|
||||||
preLoad := NewCommandInstallForLoad(AppletPkgAID, CardManagerAID)
|
preLoad := NewCommandInstallForLoad(identifiers.PackageAID, identifiers.CardManagerAID)
|
||||||
resp, err := cs.c.Send(preLoad)
|
resp, err := cs.c.Send(preLoad)
|
||||||
if err = cs.checkOK(resp, err); err != nil {
|
if err = cs.checkOK(resp, err); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -104,11 +100,24 @@ func (cs *CommandSet) LoadKeycardPackage(capFile *os.File, callback LoadingCallb
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CommandSet) InstallNDEFApplet(ndefRecord []byte) error {
|
func (cs *CommandSet) InstallNDEFApplet(ndefRecord []byte) error {
|
||||||
return cs.installForInstall(AppletPkgAID, NdefAppletAID, NdefInstanceAID, ndefRecord)
|
return cs.installForInstall(
|
||||||
|
identifiers.PackageAID,
|
||||||
|
identifiers.NdefAID,
|
||||||
|
identifiers.NdefInstanceAID,
|
||||||
|
ndefRecord)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CommandSet) InstallKeycardApplet() error {
|
func (cs *CommandSet) InstallKeycardApplet() error {
|
||||||
return cs.installForInstall(AppletPkgAID, WalletAID, WalletInstanceAID, []byte{})
|
instanceAID, err := identifiers.KeycardInstanceAID(defaultKeycardInstanceAID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return cs.installForInstall(
|
||||||
|
identifiers.PackageAID,
|
||||||
|
identifiers.KeycardAID,
|
||||||
|
instanceAID,
|
||||||
|
[]byte{})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cs *CommandSet) installForInstall(packageAID, appletAID, instanceAID, params []byte) error {
|
func (cs *CommandSet) installForInstall(packageAID, appletAID, instanceAID, params []byte) error {
|
||||||
|
@ -125,7 +134,7 @@ func (cs *CommandSet) initializeUpdate(hostChallenge []byte) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify cryptogram and initialize session keys
|
// verify cryptogram and initialize session keys
|
||||||
keys := NewSCP02Keys(CardTestKey, CardTestKey)
|
keys := NewSCP02Keys(identifiers.CardTestKey, identifiers.CardTestKey)
|
||||||
session, err := NewSession(keys, resp, hostChallenge)
|
session, err := NewSession(keys, resp, hostChallenge)
|
||||||
cs.c = NewSecureChannel(session, cs.c)
|
cs.c = NewSecureChannel(session, cs.c)
|
||||||
cs.session = session
|
cs.session = session
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package identifiers
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
|
var (
|
||||||
|
CardManagerAID = []byte{0xa0, 0x00, 0x00, 0x01, 0x51, 0x00, 0x00, 0x00}
|
||||||
|
CardTestKey = []byte{0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f}
|
||||||
|
|
||||||
|
PackageAID = []byte{0xA0, 0x00, 0x00, 0x08, 0x04, 0x00, 0x01}
|
||||||
|
KeycardAID = []byte{0xA0, 0x00, 0x00, 0x08, 0x04, 0x00, 0x01, 0x01}
|
||||||
|
NdefAID = []byte{0xA0, 0x00, 0x00, 0x08, 0x04, 0x00, 0x01, 0x02}
|
||||||
|
NdefInstanceAID = []byte{0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01}
|
||||||
|
|
||||||
|
ErrInvalidInstanceIndex = errors.New("instance index must be between 1 and 255")
|
||||||
|
)
|
||||||
|
|
||||||
|
func KeycardInstanceAID(index int) ([]byte, error) {
|
||||||
|
if index < 0x01 || index > 0xFF {
|
||||||
|
return nil, ErrInvalidInstanceIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
return append(KeycardAID, byte(index)), nil
|
||||||
|
}
|
Loading…
Reference in New Issue