From 3016fd4ec190725e93768164b45ad8f0b5720352 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Mon, 4 Oct 2021 17:57:01 +0300 Subject: [PATCH] make pairing work --- src/app/keycard/core.nim | 55 +---------------------------- src/app/keycard/pairing.nim | 10 +++--- src/app/keycard/view.nim | 70 +++++++++++++++++++++++++++++++------ 3 files changed, 67 insertions(+), 68 deletions(-) diff --git a/src/app/keycard/core.nim b/src/app/keycard/core.nim index ffb92058c1..96e6cc42af 100644 --- a/src/app/keycard/core.nim +++ b/src/app/keycard/core.nim @@ -24,60 +24,7 @@ proc delete*(self: KeycardController) = proc reset*(self: KeycardController) = discard -proc attemptOpenSecureChannel(self: KeycardController) : bool = - let pairing = self.view.pairings.getPairing(self.view.appInfo.instanceUID) - - if pairing == "": - return false - - discard """let err = self.status.keycard.openSecureChannel(pairing) - - if err == Ok: - return true - - self.view.pairings.removePairing(self.view.appInfo.instanceUID) - - """" - return false - -proc getCardState(self: KeycardController) = - var appInfo: KeycardApplicationInfo - - try: - appInfo = self.status.keycard.select() - except KeycardSelectException as ex: - self.view.cardUnhandledError(ex.error) - return - - self.view.appInfo = appInfo - - if not appInfo.installed: - self.view.cardState = NotKeycard - self.view.cardNotKeycard() - elif not appInfo.initialized: - self.view.cardState = PreInit - self.view.cardPreInit() - elif self.attemptOpenSecureChannel(): - discard """ - self.view.appStatus = self.status.keycard.getStatusApplication() - if self.view.appStatus.pukRetryCounter == 0: - self.view.cardState = Blocked - self.view.cardBlocked() - elif self.view.appStatus.pinRetryCounter == 0: - self.view.cardState = Frozen - self.view.cardFrozen() - else: - """ - self.view.cardState = Paired - self.view.cardPaired() - elif appInfo.availableSlots > 0: - self.view.cardState = Unpaired - self.view.cardUnpaired() - else: - self.view.cardState = NoFreeSlots - self.view.cardNoFreeSlots() - proc init*(self: KeycardController) = self.status.events.on(SignalType.KeycardConnected.event) do(e:Args): - self.getCardState() + self.view.getCardState() self.view.cardConnected() \ No newline at end of file diff --git a/src/app/keycard/pairing.nim b/src/app/keycard/pairing.nim index d94c98f483..bcc8dbc9b9 100644 --- a/src/app/keycard/pairing.nim +++ b/src/app/keycard/pairing.nim @@ -1,4 +1,5 @@ -import json, os, std/wrapnils +import json, os +import types/keycard import ../../constants @@ -17,7 +18,7 @@ proc newPairingController*(): KeycardPairingController = proc save(self: KeycardPairingController) = writeFile(PAIRINGSTORE, $self.store) -proc addPairing*(self: KeycardPairingController, instanceUID: string, pairing: string) = +proc addPairing*(self: KeycardPairingController, instanceUID: string, pairing: KeycardPairingInfo) = self.store[instanceUID] = %* pairing self.save() @@ -25,6 +26,7 @@ proc removePairing*(self: KeycardPairingController, instanceUID: string) = self.store.delete(instanceUID) self.save() -proc getPairing*(self: KeycardPairingController, instanceUID: string): string = +proc getPairing*(self: KeycardPairingController, instanceUID: string): KeycardPairingInfo = let node = self.store{instanceUID} - return ?.node.getStr() + if node != nil: + result = to(node, KeycardPairingInfo) diff --git a/src/app/keycard/view.nim b/src/app/keycard/view.nim index 6ed805eab3..8125ba4ee5 100644 --- a/src/app/keycard/view.nim +++ b/src/app/keycard/view.nim @@ -55,7 +55,6 @@ QtObject: except KeycardStartException as ex: self.cardUnhandledError(ex.error) - proc stopConnection*(self: KeycardView) {.slot.} = self.cardState = Disconnected try: @@ -63,17 +62,42 @@ QtObject: except KeycardStopException as ex: self.cardUnhandledError(ex.error) - proc pair*(self: KeycardView, password: string) {.slot.} = + proc attemptOpenSecureChannel(self: KeycardView): bool = + let pairing = self.pairings.getPairing(self.appInfo.instanceUID) + + if pairing == nil: + return false + + try: + self.status.keycard.openSecureChannel(int(pairing.index), pairing.key) + except KeycardOpenSecureChannelException: + self.pairings.removePairing(self.appInfo.instanceUID) + return false + + return true + + proc onSecureChannelOpened(self: KeycardView) = discard """ - let pairing = self.status.keycard.pair(password) - - if pairing == "": - error - - self.pairings.addPairing(self.appInfo.instanceUID, pairing) + self.appStatus = self.status.keycard.getStatusApplication() + if self.appStatus.pukRetryCounter == 0: + self.cardState = Blocked + self.cardBlocked() + elif self.appStatus.pinRetryCounter == 0: + self.cardState = Frozen + self.cardFrozen() + else: + """ self.cardState = Paired self.cardPaired() - """ + + proc pair*(self: KeycardView, password: string) {.slot.} = + try: + let pairing = self.status.keycard.pair(password) + self.pairings.addPairing(self.appInfo.instanceUID, pairing) + if self.attemptOpenSecureChannel(): + self.onSecureChannelOpened() + except KeycardPairException: + discard #display wrong pairing password message proc authenticate*(self: KeycardView, pin: string) {.slot.} = discard """ @@ -90,4 +114,30 @@ QtObject: proc recoverAccount*(self: KeycardView) {.slot.} = discard """ - """ \ No newline at end of file + """ + + proc getCardState*(self: KeycardView) = + var appInfo: KeycardApplicationInfo + + try: + appInfo = self.status.keycard.select() + except KeycardSelectException as ex: + self.cardUnhandledError(ex.error) + return + + self.appInfo = appInfo + + if not appInfo.installed: + self.cardState = NotKeycard + self.cardNotKeycard() + elif not appInfo.initialized: + self.cardState = PreInit + self.cardPreInit() + elif self.attemptOpenSecureChannel(): + self.onSecureChannelOpened() + elif appInfo.availableSlots > 0: + self.cardState = Unpaired + self.cardUnpaired() + else: + self.cardState = NoFreeSlots + self.cardNoFreeSlots() \ No newline at end of file