391 lines
7.4 KiB
Go
Raw Normal View History

package statuskeycardgo
2021-10-14 13:14:43 +03:00
2021-10-15 10:35:01 +03:00
import (
"errors"
"github.com/status-im/status-keycard-go/signal"
)
2021-10-14 13:14:43 +03:00
2021-10-20 11:44:57 +03:00
type cardStatus struct {
instanceUID string
keyUID string
freeSlots int
pinRetries int
pukRetries int
}
2021-10-18 14:50:56 +03:00
type KeycardFlow struct {
flowType FlowType
state runState
wakeUp chan (struct{})
2021-10-19 12:19:02 +03:00
pairings *pairingStore
params FlowParams
2021-10-20 11:44:57 +03:00
cardInfo cardStatus
2021-10-18 14:50:56 +03:00
}
func NewFlow(storageDir string) (*KeycardFlow, error) {
2021-10-19 12:19:02 +03:00
p, err := newPairingStore(storageDir)
if err != nil {
return nil, err
}
2021-10-18 14:50:56 +03:00
flow := &KeycardFlow{
2021-10-19 12:19:02 +03:00
wakeUp: make(chan (struct{})),
pairings: p,
2021-10-14 13:14:43 +03:00
}
return flow, nil
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) Start(flowType FlowType, params FlowParams) error {
2021-10-15 10:35:01 +03:00
if f.state != Idle {
2021-10-14 13:14:43 +03:00
return errors.New("already running")
}
f.flowType = flowType
f.params = params
2021-10-15 10:35:01 +03:00
f.state = Running
2021-10-14 13:14:43 +03:00
go f.runFlow()
return nil
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) Resume(params FlowParams) error {
2021-10-15 10:35:01 +03:00
if f.state != Paused {
2021-10-14 13:14:43 +03:00
return errors.New("only paused flows can be resumed")
}
for k, v := range params {
f.params[k] = v
}
2021-10-15 10:35:01 +03:00
f.state = Resuming
2021-10-14 13:14:43 +03:00
f.wakeUp <- struct{}{}
return nil
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) Cancel() error {
2021-10-14 13:14:43 +03:00
prevState := f.state
2021-10-15 10:35:01 +03:00
if prevState != Idle {
2021-10-14 13:14:43 +03:00
return errors.New("cannot cancel idle flow")
}
2021-10-15 10:35:01 +03:00
f.state = Cancelling
if prevState == Paused {
2021-10-14 13:14:43 +03:00
f.wakeUp <- struct{}{}
}
return nil
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) runFlow() {
2021-10-18 10:19:09 +03:00
var result FlowStatus
2021-10-18 16:25:20 +03:00
var err error
for {
2021-10-20 11:44:57 +03:00
f.cardInfo = cardStatus{freeSlots: -1, pinRetries: -1, pukRetries: -1}
2021-10-18 16:25:20 +03:00
result, err = f.connectedFlow()
2021-10-15 10:35:01 +03:00
2021-10-18 16:25:20 +03:00
if _, ok := err.(*restartError); !ok {
if result == nil {
result = FlowStatus{ErrorKey: err.Error()}
}
break
}
2021-10-15 10:35:01 +03:00
}
if f.state != Cancelling {
signal.SendEvent(FlowResult, result)
}
f.state = Idle
}
2021-10-20 11:44:57 +03:00
func (f *KeycardFlow) pause(action string, errMsg string) {
status := FlowParams{}
2021-10-22 09:55:00 +03:00
status[ErrorKey] = errMsg
2021-10-20 11:44:57 +03:00
if f.cardInfo.freeSlots != -1 {
status[InstanceUID] = f.cardInfo.instanceUID
status[KeyUID] = f.cardInfo.keyUID
status[FreeSlots] = f.cardInfo.freeSlots
}
if f.cardInfo.pinRetries != -1 {
status[PINRetries] = f.cardInfo.pinRetries
status[PUKRetries] = f.cardInfo.pukRetries
}
2021-10-15 10:35:01 +03:00
signal.SendEvent(action, status)
f.state = Paused
}
2021-10-20 11:44:57 +03:00
func (f *KeycardFlow) pauseAndWait(action string, errMsg string) error {
2021-10-20 12:01:38 +03:00
if f.state == Cancelling {
2021-10-22 09:55:00 +03:00
return giveupErr()
2021-10-20 12:01:38 +03:00
}
2021-10-20 11:44:57 +03:00
f.pause(action, errMsg)
2021-10-15 10:35:01 +03:00
<-f.wakeUp
2021-10-15 12:38:06 +03:00
if f.state == Resuming {
f.state = Running
2021-10-18 16:29:55 +03:00
return nil
2021-10-15 12:38:06 +03:00
} else {
2021-10-22 09:55:00 +03:00
return giveupErr()
2021-10-15 12:38:06 +03:00
}
2021-10-15 10:35:01 +03:00
}
2021-10-20 11:44:57 +03:00
func (f *KeycardFlow) pauseAndRestart(action string, errMsg string) error {
err := f.pauseAndWait(action, errMsg)
2021-10-18 16:34:15 +03:00
if err != nil {
return err
}
return restartErr()
}
2021-10-21 09:09:20 +03:00
func (f *KeycardFlow) requireKeys() error {
if f.cardInfo.keyUID != "" {
return nil
}
return f.pauseAndRestart(SwapCard, ErrorNoKeys)
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) closeKeycard(kc *keycardContext) {
2021-10-15 10:35:01 +03:00
if kc != nil {
kc.stop()
}
}
2021-10-18 14:50:56 +03:00
func (f *KeycardFlow) connect() *keycardContext {
2021-10-15 10:35:01 +03:00
kc, err := startKeycardContext()
if err != nil {
return nil
}
2021-10-22 09:55:00 +03:00
f.pause(InsertCard, ErrorConnection)
2021-10-15 10:35:01 +03:00
select {
case <-f.wakeUp:
if f.state != Cancelling {
panic("Resuming is not expected during connection")
}
return nil
case <-kc.connected:
if kc.runErr != nil {
return nil
}
2021-10-18 14:50:56 +03:00
signal.SendEvent(CardInserted, FlowStatus{})
2021-10-15 10:35:01 +03:00
return kc
}
}
2021-10-20 12:01:38 +03:00
func (f *KeycardFlow) connectedFlow() (FlowStatus, error) {
kc := f.connect()
defer f.closeKeycard(kc)
2021-10-15 12:38:06 +03:00
2021-10-20 12:01:38 +03:00
if kc == nil {
return nil, errors.New(ErrorConnection)
2021-10-18 14:50:56 +03:00
}
2021-10-20 12:01:38 +03:00
if factoryReset, ok := f.params[FactoryReset]; ok && factoryReset.(bool) {
err := f.factoryReset(kc)
2021-10-15 12:38:06 +03:00
2021-10-20 12:01:38 +03:00
if err != nil {
return nil, err
2021-10-15 12:38:06 +03:00
}
}
2021-10-14 13:14:43 +03:00
2021-10-20 12:01:38 +03:00
err := f.selectKeycard(kc)
2021-10-19 12:19:02 +03:00
if err != nil {
2021-10-20 12:01:38 +03:00
return nil, err
2021-10-19 12:19:02 +03:00
}
2021-10-20 12:01:38 +03:00
switch f.flowType {
case GetAppInfo:
return f.getAppInfoFlow(kc)
case RecoverAccount:
2021-10-21 09:31:54 +03:00
return f.exportKeysFlow(kc, true)
case Login:
return f.exportKeysFlow(kc, false)
2021-10-22 11:36:25 +03:00
case ExportPublic:
2021-10-22 11:49:55 +03:00
return f.exportPublicFlow(kc)
2021-10-22 09:32:07 +03:00
case LoadAccount:
return f.loadKeysFlow(kc)
case Sign:
return f.signFlow(kc)
2021-10-22 11:36:25 +03:00
case ChangePIN:
return f.changePINFlow(kc)
case ChangePUK:
return f.changePUKFlow(kc)
case ChangePairing:
return f.changePairingFlow(kc)
2021-10-20 12:01:38 +03:00
case UnpairThis:
return f.unpairThisFlow(kc)
2021-10-22 09:32:07 +03:00
case UnpairOthers:
return f.unpairOthersFlow(kc)
case DeleteAccountAndUnpair:
return f.deleteUnpairFlow(kc)
2021-10-20 12:01:38 +03:00
default:
return nil, errors.New(ErrorUnknownFlow)
2021-10-19 12:19:02 +03:00
}
2021-10-18 16:25:20 +03:00
}
2021-10-20 12:01:38 +03:00
func (f *KeycardFlow) getAppInfoFlow(kc *keycardContext) (FlowStatus, error) {
2021-10-22 09:32:07 +03:00
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
2021-10-20 11:44:57 +03:00
}
2021-10-21 09:31:54 +03:00
func (f *KeycardFlow) exportKeysFlow(kc *keycardContext, recover bool) (FlowStatus, error) {
2021-10-21 09:09:20 +03:00
err := f.requireKeys()
if err != nil {
return nil, err
}
2021-10-22 09:32:07 +03:00
err = f.openSCAndAuthenticate(kc, false)
2021-10-21 09:09:20 +03:00
if err != nil {
return nil, err
}
2021-10-21 09:31:54 +03:00
result := FlowStatus{KeyUID: f.cardInfo.keyUID}
2021-10-21 09:09:20 +03:00
key, err := f.exportKey(kc, encryptionPath, false)
if err != nil {
return nil, err
}
result[EncKey] = key
key, err = f.exportKey(kc, whisperPath, false)
if err != nil {
return nil, err
}
result[WhisperKey] = key
2021-10-21 09:31:54 +03:00
if recover {
key, err = f.exportKey(kc, eip1581Path, true)
if err != nil {
return nil, err
}
result[EIP1581Key] = key
2021-10-21 09:09:20 +03:00
2021-10-21 09:31:54 +03:00
key, err = f.exportKey(kc, walletRoothPath, true)
if err != nil {
return nil, err
}
result[WalleRootKey] = key
2021-10-19 12:19:02 +03:00
2021-10-21 09:31:54 +03:00
key, err = f.exportKey(kc, walletPath, true)
if err != nil {
return nil, err
}
result[WalletKey] = key
key, err = f.exportKey(kc, masterPath, true)
if err != nil {
return nil, err
}
result[MasterKey] = key
2021-10-19 12:19:02 +03:00
}
2021-10-21 09:09:20 +03:00
return result, nil
2021-10-18 16:25:20 +03:00
}
2021-10-22 11:49:55 +03:00
func (f *KeycardFlow) exportPublicFlow(kc *keycardContext) (FlowStatus, error) {
err := f.requireKeys()
if err != nil {
return nil, err
}
err = f.openSCAndAuthenticate(kc, false)
if err != nil {
return nil, err
}
path, ok := f.params[BIP44Path]
if !ok {
err := f.pauseAndWait(EnterPath, ErrorExporting)
if err != nil {
return nil, err
}
}
key, err := f.exportKey(kc, path.(string), true)
if err != nil {
return nil, err
}
return FlowStatus{KeyUID: f.cardInfo.keyUID, ExportedKey: key}, nil
2021-10-22 11:36:25 +03:00
}
2021-10-22 09:32:07 +03:00
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")
}
2021-10-22 11:36:25 +03:00
func (f *KeycardFlow) changePINFlow(kc *keycardContext) (FlowStatus, error) {
return nil, errors.New("not implemented yet")
}
func (f *KeycardFlow) changePUKFlow(kc *keycardContext) (FlowStatus, error) {
return nil, errors.New("not implemented yet")
}
func (f *KeycardFlow) changePairingFlow(kc *keycardContext) (FlowStatus, error) {
2021-10-22 09:32:07 +03:00
return nil, errors.New("not implemented yet")
}
2021-10-20 12:01:38 +03:00
func (f *KeycardFlow) unpairThisFlow(kc *keycardContext) (FlowStatus, error) {
2021-10-22 09:32:07 +03:00
err := f.openSCAndAuthenticate(kc, true)
2021-10-15 12:38:06 +03:00
if err != nil {
2021-10-18 16:25:20 +03:00
return nil, err
2021-10-15 12:38:06 +03:00
}
2021-10-21 08:48:51 +03:00
err = f.unpairCurrent(kc)
2021-10-21 08:19:25 +03:00
2021-10-21 08:48:51 +03:00
if err != nil {
2021-10-21 08:19:25 +03:00
return nil, err
}
f.cardInfo.freeSlots++
2021-10-22 11:49:55 +03:00
return FlowStatus{InstanceUID: f.cardInfo.instanceUID, FreeSlots: f.cardInfo.freeSlots}, nil
2021-10-18 15:47:27 +03:00
}
2021-10-22 09:32:07 +03:00
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")
}