mirror of
https://github.com/status-im/keycard-go.git
synced 2025-01-31 06:07:03 +00:00
remove old commands
This commit is contained in:
parent
a0138cd86d
commit
04b3d48a80
108
actions.go
108
actions.go
@ -1,108 +0,0 @@
|
||||
package keycard
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/status-im/keycard-go/apdu"
|
||||
"github.com/status-im/keycard-go/crypto"
|
||||
"github.com/status-im/keycard-go/types"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrAlreadyInitialized = errors.New("card already initialized")
|
||||
ErrWrongApplicationInfoTemplate = errors.New("wrong application info template")
|
||||
ErrApplicationStatusTemplateNotFound = errors.New("application status template not found")
|
||||
)
|
||||
|
||||
func OpenSecureChannel(c types.Channel, appInfo *types.ApplicationInfo, pairingIndex uint8, pairingKey []byte) (*SecureChannel, error) {
|
||||
sc := NewSecureChannel(c)
|
||||
cmd := NewCommandOpenSecureChannel(pairingIndex, sc.RawPublicKey())
|
||||
resp, err := c.Send(cmd)
|
||||
if err = checkOKResponse(err, resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
encKey, macKey, iv := crypto.DeriveSessionKeys(sc.Secret(), pairingKey, resp.Data)
|
||||
sc.Init(iv, encKey, macKey)
|
||||
|
||||
err = mutualAuthenticate(sc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
func mutualAuthenticate(sc *SecureChannel) error {
|
||||
data := make([]byte, 32)
|
||||
if _, err := rand.Read(data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cmd := NewCommandMutuallyAuthenticate(data)
|
||||
resp, err := sc.Send(cmd)
|
||||
|
||||
return checkOKResponse(err, resp)
|
||||
}
|
||||
|
||||
func GetStatusApplication(c types.Channel) (*types.ApplicationStatus, error) {
|
||||
cmd := NewCommandGetStatusApplication()
|
||||
resp, err := c.Send(cmd)
|
||||
if err = checkOKResponse(err, resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return parseApplicationStatus(resp.Data)
|
||||
}
|
||||
|
||||
func parseApplicationStatus(data []byte) (*types.ApplicationStatus, error) {
|
||||
appStatus := &types.ApplicationStatus{}
|
||||
|
||||
tpl, err := apdu.FindTag(data, TagApplicationStatusTemplate)
|
||||
if err != nil {
|
||||
return nil, ErrApplicationStatusTemplateNotFound
|
||||
}
|
||||
|
||||
if pinRetryCount, err := apdu.FindTag(tpl, uint8(0x02)); err == nil && len(pinRetryCount) == 1 {
|
||||
appStatus.PinRetryCount = int(pinRetryCount[0])
|
||||
}
|
||||
|
||||
if pukRetryCount, err := apdu.FindTagN(tpl, 1, uint8(0x02)); err == nil && len(pukRetryCount) == 1 {
|
||||
appStatus.PUKRetryCount = int(pukRetryCount[0])
|
||||
}
|
||||
|
||||
if keyInitialized, err := apdu.FindTag(tpl, uint8(0x01)); err == nil {
|
||||
if bytes.Equal(keyInitialized, []byte{0xFF}) {
|
||||
appStatus.KeyInitialized = true
|
||||
}
|
||||
}
|
||||
|
||||
if keyDerivationSupported, err := apdu.FindTagN(tpl, 1, uint8(0x01)); err == nil {
|
||||
if bytes.Equal(keyDerivationSupported, []byte{0xFF}) {
|
||||
appStatus.PubKeyDerivation = true
|
||||
}
|
||||
}
|
||||
|
||||
return appStatus, nil
|
||||
}
|
||||
|
||||
func checkOKResponse(err error, resp *apdu.Response) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return checkResponse(resp, apdu.SwOK)
|
||||
}
|
||||
|
||||
func checkResponse(resp *apdu.Response, allowedResponses ...uint16) error {
|
||||
for _, code := range allowedResponses {
|
||||
if code == resp.Sw {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("unexpected response: %x", resp.Sw)
|
||||
}
|
@ -1,14 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
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/identifiers"
|
||||
"github.com/status-im/keycard-go/types"
|
||||
)
|
||||
|
||||
@ -138,88 +135,3 @@ func (i *Initializer) Status(key []byte, index int) (*types.ApplicationStatus, e
|
||||
|
||||
return appStatus, nil
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (i *Initializer) selectAID(aid []byte) error {
|
||||
sel := globalplatform.NewCommandSelect(identifiers.CardManagerAID)
|
||||
_, err := i.send("select", sel)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (i *Initializer) initializeUpdate() (*globalplatform.Session, error) {
|
||||
hostChallenge, err := generateHostChallenge()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
init := globalplatform.NewCommandInitializeUpdate(hostChallenge)
|
||||
resp, err := i.send("initialize update", init)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// verify cryptogram and initialize session keys
|
||||
keys := globalplatform.NewSCP02Keys(identifiers.CardTestKey, identifiers.CardTestKey)
|
||||
session, err := globalplatform.NewSession(keys, resp, hostChallenge)
|
||||
|
||||
return session, err
|
||||
}
|
||||
|
||||
func (i *Initializer) externalAuthenticate(session *globalplatform.Session) error {
|
||||
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
|
||||
}
|
||||
|
||||
func (i *Initializer) send(description string, cmd *apdu.Command, allowedResponses ...uint16) (*apdu.Response, error) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
@ -3,8 +3,7 @@ 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}
|
||||
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}
|
||||
|
Loading…
x
Reference in New Issue
Block a user