improve getstatus and unpair

This commit is contained in:
Michele Balistreri 2021-10-22 09:32:07 +03:00
parent ff1230bb9c
commit e299c38600
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
4 changed files with 72 additions and 11 deletions

View File

@ -93,5 +93,6 @@ func main() {
testFlow(skg.GetAppInfo, skg.FlowParams{})
testFlow(skg.RecoverAccount, skg.FlowParams{skg.PIN: "234567"})
testFlow(skg.Login, skg.FlowParams{})
testFlow(skg.GetAppInfo, skg.FlowParams{})
testFlow(skg.UnpairThis, skg.FlowParams{skg.PIN: correctPIN})
}

49
flow.go
View File

@ -219,15 +219,38 @@ func (f *KeycardFlow) connectedFlow() (FlowStatus, error) {
return f.exportKeysFlow(kc, true)
case Login:
return f.exportKeysFlow(kc, false)
case LoadAccount:
return f.loadKeysFlow(kc)
case Sign:
return f.signFlow(kc)
case ChangeCredentials:
return f.changeCredentialsFlow(kc)
case UnpairThis:
return f.unpairThisFlow(kc)
case UnpairOthers:
return f.unpairOthersFlow(kc)
case DeleteAccountAndUnpair:
return f.deleteUnpairFlow(kc)
default:
return nil, errors.New(ErrorUnknownFlow)
}
}
func (f *KeycardFlow) getAppInfoFlow(kc *keycardContext) (FlowStatus, error) {
return FlowStatus{ErrorKey: ErrorOK, AppInfo: toAppInfo(kc.cmdSet.ApplicationInfo)}, nil
res := FlowStatus{ErrorKey: ErrorOK, AppInfo: toAppInfo(kc.cmdSet.ApplicationInfo)}
err := f.openSCAndAuthenticate(kc, true)
if err == nil {
res[Paired] = true
res[PINRetries] = f.cardInfo.pinRetries
res[PUKRetries] = f.cardInfo.pukRetries
} else if _, ok := err.(*giveupError); ok {
res[Paired] = false
} else {
return nil, err
}
return res, nil
}
func (f *KeycardFlow) exportKeysFlow(kc *keycardContext, recover bool) (FlowStatus, error) {
@ -237,7 +260,7 @@ func (f *KeycardFlow) exportKeysFlow(kc *keycardContext, recover bool) (FlowStat
return nil, err
}
err = f.openSCAndAuthenticate(kc)
err = f.openSCAndAuthenticate(kc, false)
if err != nil {
return nil, err
@ -286,8 +309,20 @@ func (f *KeycardFlow) exportKeysFlow(kc *keycardContext, recover bool) (FlowStat
return result, nil
}
func (f *KeycardFlow) loadKeysFlow(kc *keycardContext) (FlowStatus, error) {
return nil, errors.New("not implemented yet")
}
func (f *KeycardFlow) signFlow(kc *keycardContext) (FlowStatus, error) {
return nil, errors.New("not implemented yet")
}
func (f *KeycardFlow) changeCredentialsFlow(kc *keycardContext) (FlowStatus, error) {
return nil, errors.New("not implemented yet")
}
func (f *KeycardFlow) unpairThisFlow(kc *keycardContext) (FlowStatus, error) {
err := f.openSCAndAuthenticate(kc)
err := f.openSCAndAuthenticate(kc, true)
if err != nil {
return nil, err
@ -302,3 +337,11 @@ func (f *KeycardFlow) unpairThisFlow(kc *keycardContext) (FlowStatus, error) {
f.cardInfo.freeSlots++
return FlowStatus{InstanceUID: f.cardInfo.instanceUID, FreeSlots: f.cardInfo.freeSlots}, err
}
func (f *KeycardFlow) unpairOthersFlow(kc *keycardContext) (FlowStatus, error) {
return nil, errors.New("not implemented yet")
}
func (f *KeycardFlow) deleteUnpairFlow(kc *keycardContext) (FlowStatus, error) {
return nil, errors.New("not implemented yet")
}

View File

@ -70,12 +70,14 @@ func (f *KeycardFlow) initCard(kc *keycardContext) error {
return errors.New("not implemented")
}
func (f *KeycardFlow) openSC(kc *keycardContext) error {
if !kc.cmdSet.ApplicationInfo.Initialized {
return f.initCard(kc)
}
func (f *KeycardFlow) openSC(kc *keycardContext, giveup bool) error {
var pairing *PairingInfo
pairing := f.pairings.get(f.cardInfo.instanceUID)
if !kc.cmdSet.ApplicationInfo.Initialized && !giveup {
return f.initCard(kc)
} else {
pairing = f.pairings.get(f.cardInfo.instanceUID)
}
if pairing != nil {
err := kc.openSecureChannel(pairing.Index, pairing.Key)
@ -99,13 +101,17 @@ func (f *KeycardFlow) openSC(kc *keycardContext) error {
f.pairings.delete(f.cardInfo.instanceUID)
}
if giveup {
return giveupErr()
}
err := f.pair(kc)
if err != nil {
return err
}
return f.openSC(kc)
return f.openSC(kc, giveup)
}
func (f *KeycardFlow) unblockPIN(kc *keycardContext) error {
@ -184,8 +190,8 @@ func (f *KeycardFlow) authenticate(kc *keycardContext) error {
return f.authenticate(kc)
}
func (f *KeycardFlow) openSCAndAuthenticate(kc *keycardContext) error {
err := f.openSC(kc)
func (f *KeycardFlow) openSCAndAuthenticate(kc *keycardContext, giveup bool) error {
err := f.openSC(kc, giveup)
if err != nil {
return err

View File

@ -6,6 +6,7 @@ type FlowStatus map[string]interface{}
type runState int
type restartError struct{}
type giveupError struct{}
func restartErr() (e *restartError) {
return &restartError{}
@ -15,6 +16,14 @@ func (e *restartError) Error() string {
return "restart"
}
func giveupErr() (e *giveupError) {
return &giveupError{}
}
func (e *giveupError) Error() string {
return "giveup"
}
const (
GetAppInfo FlowType = iota
RecoverAccount
@ -67,6 +76,7 @@ const (
PINRetries = "pin-retries"
PUKRetries = "puk-retries"
PairingPass = "pairing-pass"
Paired = "paired"
NewPairing = "new- pairing-pass"
PIN = "pin"
NewPIN = "new-pin"
@ -78,6 +88,7 @@ const (
EIP1581Key = "eip1581-key"
WhisperKey = "whisper-key"
EncKey = "encryption-key"
Mnemonic = "mnemonic"
)
const (