From 6883296620cecc18fcb2ef3ec2a07a8a386ee6db Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Mon, 25 Oct 2021 09:10:43 +0300 Subject: [PATCH] implement change credentials flows --- flow.go | 43 ++++++++++++++++++++++++++++--- flow_commands.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ flow_types.go | 1 + 3 files changed, 106 insertions(+), 4 deletions(-) diff --git a/flow.go b/flow.go index 1ec469d..1aced01 100644 --- a/flow.go +++ b/flow.go @@ -353,15 +353,51 @@ func (f *KeycardFlow) signFlow(kc *keycardContext) (FlowStatus, error) { } func (f *KeycardFlow) changePINFlow(kc *keycardContext) (FlowStatus, error) { - return nil, errors.New("not implemented yet") + err := f.openSCAndAuthenticate(kc, false) + + if err != nil { + return nil, err + } + + err = f.changePIN(kc) + + if err != nil { + return nil, err + } + + return FlowStatus{InstanceUID: f.cardInfo.instanceUID}, nil } func (f *KeycardFlow) changePUKFlow(kc *keycardContext) (FlowStatus, error) { - return nil, errors.New("not implemented yet") + err := f.openSCAndAuthenticate(kc, false) + + if err != nil { + return nil, err + } + + err = f.changePUK(kc) + + if err != nil { + return nil, err + } + + return FlowStatus{InstanceUID: f.cardInfo.instanceUID}, nil } func (f *KeycardFlow) changePairingFlow(kc *keycardContext) (FlowStatus, error) { - return nil, errors.New("not implemented yet") + err := f.openSCAndAuthenticate(kc, false) + + if err != nil { + return nil, err + } + + err = f.changePairing(kc) + + if err != nil { + return nil, err + } + + return FlowStatus{InstanceUID: f.cardInfo.instanceUID}, nil } func (f *KeycardFlow) unpairThisFlow(kc *keycardContext) (FlowStatus, error) { @@ -399,7 +435,6 @@ func (f *KeycardFlow) unpairOthersFlow(kc *keycardContext) (FlowStatus, error) { return nil, err } - f.cardInfo.freeSlots++ } return FlowStatus{InstanceUID: f.cardInfo.instanceUID, FreeSlots: f.cardInfo.freeSlots}, nil diff --git a/flow_commands.go b/flow_commands.go index c7233ec..38debc4 100644 --- a/flow_commands.go +++ b/flow_commands.go @@ -279,3 +279,69 @@ func (f *KeycardFlow) exportKey(kc *keycardContext, path string, onlyPublic bool return keyPair, err } + +func (f *KeycardFlow) changePIN(kc *keycardContext) error { + if newPIN, ok := f.params[NewPIN]; ok { + err := kc.changePin(newPIN.(string)) + + if isSCardError(err) { + return restartErr() + } else if err != nil { + return err + } + + return nil + } + + err := f.pauseAndWait(EnterNewPIN, ErrorChanging) + + if err != nil { + return err + } + + return f.changePIN(kc) +} + +func (f *KeycardFlow) changePUK(kc *keycardContext) error { + if newPUK, ok := f.params[NewPUK]; ok { + err := kc.changePuk(newPUK.(string)) + + if isSCardError(err) { + return restartErr() + } else if err != nil { + return err + } + + return nil + } + + err := f.pauseAndWait(EnterNewPUK, ErrorChanging) + + if err != nil { + return err + } + + return f.changePUK(kc) +} + +func (f *KeycardFlow) changePairing(kc *keycardContext) error { + if newPairing, ok := f.params[NewPairing]; ok { + err := kc.changePairingPassword(newPairing.(string)) + + if isSCardError(err) { + return restartErr() + } else if err != nil { + return err + } + + return nil + } + + err := f.pauseAndWait(EnterNewPair, ErrorChanging) + + if err != nil { + return err + } + + return f.changePairing(kc) +} diff --git a/flow_types.go b/flow_types.go index 877b7dd..6f5c8aa 100644 --- a/flow_types.go +++ b/flow_types.go @@ -75,6 +75,7 @@ const ( ErrorUnblocking = "unblocking" ErrorSigning = "signing" ErrorExporting = "exporting" + ErrorChanging = "changing-credentials" ) const (