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

View File

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

View File

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

View File

@ -11,12 +11,13 @@
(fx/defn sign (fx/defn sign
{:events [:keycard/sign]} {:events [:keycard/sign]}
[{:keys [db] :as cofx}] [{:keys [db] :as cofx} hash on-success]
(let [card-connected? (get-in db [:keycard :card-connected?]) (let [card-connected? (get-in db [:keycard :card-connected?])
key-uid (get-in db [:multiaccount :key-uid]) key-uid (get-in db [:multiaccount :key-uid])
keycard-key-uid (get-in db [:keycard :application-info :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) 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]) data (get-in db [:keycard :data])
typed? (get-in db [:keycard :typed?]) typed? (get-in db [:keycard :typed?])
pin (common/vector->string (get-in db [:keycard :pin :sign])) pin (common/vector->string (get-in db [:keycard :pin :sign]))
@ -27,21 +28,25 @@
(reduced path))) (reduced path)))
nil nil
(:multiaccount/accounts db))] (:multiaccount/accounts db))]
(if (and card-connected? (cond
keycard-match?) (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 {:db (-> db
(assoc-in [:keycard :card-read-in-progress?] true) (assoc-in [:keycard :card-read-in-progress?] true)
(assoc-in [:keycard :pin :status] :verifying)) (assoc-in [:keycard :pin :status] :verifying))
:keycard/sign {:hash (ethereum/naked-address hash) :keycard/sign {:hash (ethereum/naked-address hash)
:data data :data data
:typed? typed? :typed? typed? ; this parameter is for e2e
:pin pin :on-success on-success
:path path}} :pin pin
(fx/merge cofx :path path}})))
{: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?))))))
(defn normalize-signature [signature] (defn normalize-signature [signature]
(-> signature (-> signature
@ -51,30 +56,12 @@
(fx/defn sign-message (fx/defn sign-message
{:events [:keycard/sign-message]} {:events [:keycard/sign-message]}
[{:keys [db] :as cofx} params result] [cofx params result]
(let [{:keys [result error]} (types/json->clj result) (let [{:keys [result error]} (types/json->clj result)
on-success #(re-frame/dispatch [:keycard/on-sign-message-success params on-success #(re-frame/dispatch [:keycard/on-sign-message-success params
(normalize-signature %)]) (normalize-signature %)])
hash (ethereum/naked-address result) hash (ethereum/naked-address result)]
card-connected? (get-in db [:keycard :card-connected?]) (sign cofx hash on-success)))
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?))))))
(fx/defn on-sign-message-success (fx/defn on-sign-message-success
{:events [:keycard/on-sign-message-success]} {:events [:keycard/on-sign-message-success]}
@ -209,4 +196,4 @@
(fx/merge cofx (fx/merge cofx
(common/hide-connection-sheet) (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]))}) :message (str "Unexpected error SW, 0x63C" (get-in @state [:application-info :puk-retry-counter]))})
(defn with-pin [pin on-failure on-valid] (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 (do
(swap! state update :application-info assoc (swap! state update :application-info assoc
:pin-retry-counter 3 :pin-retry-counter 3
:puk-retry-counter 5) :puk-retry-counter 5)
(later on-valid)) (later on-valid))
:else
(do (do
(swap! state update-in (swap! state update-in
[:application-info :pin-retry-counter] [:application-info :pin-retry-counter]
@ -260,13 +266,19 @@
(defn unblock-pin (defn unblock-pin
[{:keys [puk new-pin on-success on-failure]}] [{: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 (do
(swap! state update :application-info assoc (swap! state update :application-info assoc
:pin-retry-counter 3 :pin-retry-counter 3
:puk-retry-counter 5) :puk-retry-counter 5)
(swap! state assoc :pin new-pin) (swap! state assoc :pin new-pin)
(later #(on-success true))) (later #(on-success true)))
:else
(do (do
(swap! state update-in (swap! state update-in
[:application-info :puk-retry-counter] [:application-info :puk-retry-counter]

View File

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