From 8188c434c2a5b6fdcade71a21b4f3bdee4564af7 Mon Sep 17 00:00:00 2001 From: YanNaingWinn Date: Tue, 31 Mar 2026 04:17:06 +0700 Subject: [PATCH 1/2] Enforce double confirmation when changing PIN or PUK --- cryptnox_cli/command/change_pin.py | 2 +- cryptnox_cli/command/change_puk.py | 2 +- cryptnox_cli/command/helper/security.py | 22 ++++++++++++++++++++++ cryptnox_cli/command/unlock_pin.py | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/cryptnox_cli/command/change_pin.py b/cryptnox_cli/command/change_pin.py index 0cd2a98..d48c8dd 100644 --- a/cryptnox_cli/command/change_pin.py +++ b/cryptnox_cli/command/change_pin.py @@ -36,7 +36,7 @@ def _execute(self, card: cryptnox_sdk_py.Card) -> int: print("Card is in easy mode. Setting same PIN code") pin_code = security.EASY_MODE_PIN else: - pin_code = security.get_pin_code(card, f"Set new PIN code ({card.pin_rule}): ") + pin_code = security.confirm_pin_code(card, f"Set new PIN code ({card.pin_rule}): ") card.change_pin(pin_code) diff --git a/cryptnox_cli/command/change_puk.py b/cryptnox_cli/command/change_puk.py index 13e464c..e3f736b 100644 --- a/cryptnox_cli/command/change_puk.py +++ b/cryptnox_cli/command/change_puk.py @@ -37,7 +37,7 @@ def _execute(self, card: cryptnox_sdk_py.Card) -> int: break else: puk_code = security.get_puk_code(card) - new_puk_code = security.get_puk_code(card, f"Set new PUK code ({card.puk_rule}): ") + new_puk_code = security.confirm_puk_code(card, f"Set new PUK code ({card.puk_rule}): ") try: card.change_puk(puk_code, new_puk_code) diff --git a/cryptnox_cli/command/helper/security.py b/cryptnox_cli/command/helper/security.py index b36eb33..02ddc4c 100644 --- a/cryptnox_cli/command/helper/security.py +++ b/cryptnox_cli/command/helper/security.py @@ -277,6 +277,28 @@ def get_puk_code(card: cryptnox_sdk_py.Card, text: str = "", allowed_values: Lis return _get_code(card.valid_puk, text, allowed_values) +def confirm_pin_code(card: cryptnox_sdk_py.Card, text: str, confirm_text: str = "") -> str: + """Prompt for new PIN twice and return it only if both entries match.""" + confirm_text = confirm_text or f"Confirm new PIN code ({card.pin_rule}): " + while True: + value = get_pin_code(card, text) + confirm_value = get_pin_code(card, confirm_text) + if value == confirm_value: + return value + print("The values do not match. Please try again.") + + +def confirm_puk_code(card: cryptnox_sdk_py.Card, text: str, confirm_text: str = "") -> str: + """Prompt for new PUK twice and return it only if both entries match.""" + confirm_text = confirm_text or f"Confirm new PUK code ({card.puk_rule}): " + while True: + value = get_puk_code(card, text) + confirm_value = get_puk_code(card, confirm_text) + if value == confirm_value: + return value + print("The values do not match. Please try again.") + + def _get_code( validation_method: typing.Callable, text: str = "", diff --git a/cryptnox_cli/command/unlock_pin.py b/cryptnox_cli/command/unlock_pin.py index b33986e..326cc85 100644 --- a/cryptnox_cli/command/unlock_pin.py +++ b/cryptnox_cli/command/unlock_pin.py @@ -33,7 +33,7 @@ def unblock_pin(card, puk): if is_easy_mode: pin_code = security.EASY_MODE_PIN else: - pin_code = security.get_pin_code(card, f"Set card PIN code ({card.pin_rule}): ") + pin_code = security.confirm_pin_code(card, f"Set card PIN code ({card.pin_rule}): ") card.unblock_pin(puk, pin_code) From 7696f5321c8216c5985a9893f19ad1237a6dafed Mon Sep 17 00:00:00 2001 From: YanNaingWinn Date: Thu, 2 Apr 2026 03:10:40 +0700 Subject: [PATCH 2/2] Fix Ctrl+C handling in confirm_pin_code and confirm_puk_code --- cryptnox_cli/command/helper/security.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cryptnox_cli/command/helper/security.py b/cryptnox_cli/command/helper/security.py index 02ddc4c..8a1a4a3 100644 --- a/cryptnox_cli/command/helper/security.py +++ b/cryptnox_cli/command/helper/security.py @@ -281,8 +281,11 @@ def confirm_pin_code(card: cryptnox_sdk_py.Card, text: str, confirm_text: str = """Prompt for new PIN twice and return it only if both entries match.""" confirm_text = confirm_text or f"Confirm new PIN code ({card.pin_rule}): " while True: - value = get_pin_code(card, text) - confirm_value = get_pin_code(card, confirm_text) + try: + value = get_pin_code(card, text) + confirm_value = get_pin_code(card, confirm_text) + except KeyboardInterrupt: + raise ExitException("Aborted.") if value == confirm_value: return value print("The values do not match. Please try again.") @@ -292,8 +295,11 @@ def confirm_puk_code(card: cryptnox_sdk_py.Card, text: str, confirm_text: str = """Prompt for new PUK twice and return it only if both entries match.""" confirm_text = confirm_text or f"Confirm new PUK code ({card.puk_rule}): " while True: - value = get_puk_code(card, text) - confirm_value = get_puk_code(card, confirm_text) + try: + value = get_puk_code(card, text) + confirm_value = get_puk_code(card, confirm_text) + except KeyboardInterrupt: + raise ExitException("Aborted.") if value == confirm_value: return value print("The values do not match. Please try again.")