better PIN/PUK retry counter handling

Signed-off-by: Michele Balistreri <michele@bitgamma.com>
This commit is contained in:
Michele Balistreri 2021-08-25 13:57:09 +03:00
parent 86fcdd47e4
commit eebbfe9346
No known key found for this signature in database
GPG Key ID: E9567DA33A4F791A
7 changed files with 48 additions and 49 deletions

View File

@ -201,4 +201,4 @@
:error-label :t/pin-mismatch))}
(when (zero? pin-retries) (common/frozen-keycard-popup))
(navigation/navigate-to-cofx :enter-pin-settings nil))
(common/show-wrong-keycard-alert true))))))
(common/show-wrong-keycard-alert))))))

View File

@ -284,10 +284,10 @@
{:db (assoc-in db [:keycard :back-button-listener] listener)})
(fx/defn show-wrong-keycard-alert
[_ card-connected?]
(when card-connected?
{:utils/show-popup {:title (i18n/label :t/wrong-card)
:content (i18n/label :t/wrong-card-text)}}))
[_]
(log/debug "show-wrong-keycard-alert")
{:utils/show-popup {:title (i18n/label :t/wrong-card)
:content (i18n/label :t/wrong-card-text)}})
(fx/defn unauthorized-operation
[cofx]
@ -405,7 +405,7 @@
(when (zero? pin-retries-count) (frozen-keycard-popup))
(when (= flow :import)
(navigation/navigate-to-cofx :keycard-recovery-pin nil)))
(show-wrong-keycard-alert true)))))
(show-wrong-keycard-alert)))))
(fx/defn factory-reset
{:events [:keycard/factory-reset]}
@ -475,7 +475,7 @@
(when (contains?
#{last-on-card-connected on-card-connected}
:keycard/prepare-to-sign)
(show-wrong-keycard-alert true))
(show-wrong-keycard-alert))
(when on-card-read
(dispatch-event on-card-read)))))))

View File

@ -259,7 +259,7 @@
(fx/merge cofx
(common/hide-connection-sheet)
(common/show-wrong-keycard-alert true)
(common/show-wrong-keycard-alert)
(clear-on-verify-handlers))))))
(fx/defn unblock-pin

View File

@ -33,7 +33,7 @@
(when (zero? pin-retries) (common/frozen-keycard-popup)))
:else
(fx/merge cofx
(common/show-wrong-keycard-alert true)
(common/show-wrong-keycard-alert)
(common/clear-pin)
(common/hide-connection-sheet)))))

View File

@ -11,12 +11,13 @@
(fx/defn sign
{:events [:keycard/sign]}
[{:keys [db] :as cofx}]
[{:keys [db] :as cofx} hash on-success]
(let [card-connected? (get-in db [:keycard :card-connected?])
key-uid (get-in db [:multiaccount :key-uid])
keycard-key-uid (get-in db [:keycard :application-info :key-uid])
keycard-pin-retries (get-in db [:keycard :application-info :pin-retry-counter])
keycard-match? (= key-uid keycard-key-uid)
hash (get-in db [:keycard :hash])
hash (or hash (get-in db [:keycard :hash]))
data (get-in db [:keycard :data])
typed? (get-in db [:keycard :typed?])
pin (common/vector->string (get-in db [:keycard :pin :sign]))
@ -27,21 +28,25 @@
(reduced path)))
nil
(:multiaccount/accounts db))]
(if (and card-connected?
keycard-match?)
(cond
(not keycard-match?)
(common/show-wrong-keycard-alert cofx)
(not card-connected?)
(fx/merge cofx
{:db (assoc-in db [:signing/sign :keycard-step] :signing)}
(common/set-on-card-connected :keycard/sign))
(pos? keycard-pin-retries) ; if 0, get-application-info will have already closed the connection sheet and opened the frozen card popup
{:db (-> db
(assoc-in [:keycard :card-read-in-progress?] true)
(assoc-in [:keycard :pin :status] :verifying))
:keycard/sign {:hash (ethereum/naked-address hash)
:data data
:typed? typed?
:pin pin
:path path}}
(fx/merge cofx
{:db (assoc-in db [:signing/sign :keycard-step] :signing)}
(common/set-on-card-connected :keycard/sign)
(when-not keycard-match?
(common/show-wrong-keycard-alert card-connected?))))))
:keycard/sign {:hash (ethereum/naked-address hash)
:data data
:typed? typed? ; this parameter is for e2e
:on-success on-success
:pin pin
:path path}})))
(defn normalize-signature [signature]
(-> signature
@ -51,30 +56,12 @@
(fx/defn sign-message
{:events [:keycard/sign-message]}
[{:keys [db] :as cofx} params result]
[cofx params result]
(let [{:keys [result error]} (types/json->clj result)
on-success #(re-frame/dispatch [:keycard/on-sign-message-success params
(normalize-signature %)])
hash (ethereum/naked-address result)
card-connected? (get-in db [:keycard :card-connected?])
key-uid (get-in db [:multiaccount :key-uid])
keycard-key-uid (get-in db [:keycard :application-info :key-uid])
keycard-match? (= key-uid keycard-key-uid)
pin (common/vector->string (get-in db [:keycard :pin :sign]))]
(if (and card-connected?
keycard-match?)
{:db (-> db
(assoc-in [:keycard :card-read-in-progress?] true)
(assoc-in [:keycard :pin :status] :verifying))
:keycard/sign {:hash (ethereum/naked-address hash)
:data (:data params)
:pin pin
:on-success on-success}}
(fx/merge cofx
{:db (assoc-in db [:signing/sign :keycard-step] :signing)}
(common/set-on-card-connected :keycard/sign)
(when-not keycard-match?
(common/show-wrong-keycard-alert card-connected?))))))
hash (ethereum/naked-address result)]
(sign cofx hash on-success)))
(fx/defn on-sign-message-success
{:events [:keycard/on-sign-message-success]}
@ -209,4 +196,4 @@
(fx/merge cofx
(common/hide-connection-sheet)
(common/show-wrong-keycard-alert true))))))
(common/show-wrong-keycard-alert))))))

View File

@ -82,12 +82,18 @@
:message (str "Unexpected error SW, 0x63C" (get-in @state [:application-info :puk-retry-counter]))})
(defn with-pin [pin on-failure on-valid]
(if (= pin (get @state :pin))
(cond
(= (get-in @state [:application-info :pin-retry-counter]) 0)
(later #(on-failure (pin-error)))
(= pin (get @state :pin))
(do
(swap! state update :application-info assoc
:pin-retry-counter 3
:puk-retry-counter 5)
(later on-valid))
:else
(do
(swap! state update-in
[:application-info :pin-retry-counter]
@ -260,13 +266,19 @@
(defn unblock-pin
[{:keys [puk new-pin on-success on-failure]}]
(if (= puk (get @state :puk))
(cond
(= (get-in @state [:application-info :puk-retry-counter]) 0)
(later #(on-failure (puk-error)))
(= puk (get @state :puk))
(do
(swap! state update :application-info assoc
:pin-retry-counter 3
:puk-retry-counter 5)
(swap! state assoc :pin new-pin)
(later #(on-success true)))
:else
(do
(swap! state update-in
[:application-info :puk-retry-counter]

View File

@ -145,7 +145,7 @@
(fx/merge cofx
{:db (assoc-in db [:keycard :pin :status] nil)}
(common/set-on-card-connected :keycard/remove-key-with-unpair))
(common/show-wrong-keycard-alert true)))))
(common/show-wrong-keycard-alert)))))
(fx/defn on-unpair-and-delete-success
{:events [:keycard.callback/on-unpair-and-delete-success]}
@ -162,4 +162,4 @@
(fx/merge cofx
{:db (assoc-in db [:keycard :pin :status] nil)}
(common/set-on-card-connected :keycard/unpair-and-delete))
(common/show-wrong-keycard-alert true)))))
(common/show-wrong-keycard-alert)))))