From e453bda217ea61eb678cdbbf4653123c93c41f2f Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Thu, 22 Aug 2024 14:09:22 +0200 Subject: [PATCH] CTAP: Handle broken HID connection --- helper/helper/device.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/helper/helper/device.py b/helper/helper/device.py index 755911e3..5ebd96a5 100644 --- a/helper/helper/device.py +++ b/helper/helper/device.py @@ -45,6 +45,7 @@ from ykman.pcsc import list_devices, YK_READER_NAME from smartcard.Exceptions import SmartcardException, NoCardException from smartcard.pcsc.PCSCExceptions import EstablishContextException from smartcard.CardMonitoring import CardObserver, CardMonitor +from fido2.ctap import CtapError from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey from hashlib import sha256 from dataclasses import asdict @@ -349,6 +350,11 @@ class UsbDeviceNode(AbstractDeviceNode): except (ValueError, OSError) as e: logger.warning("Error opening connection", exc_info=True) raise ConnectionException(self._device.fingerprint, "fido", e) + except Exception as e: # TODO: Replace with ConnectionError once added + if "Wrong" in str(e): + logger.warning("Error opening connection", exc_info=True) + raise ConnectionException(self._device.fingerprint, "fido", e) + raise class _ReaderObserver(CardObserver): @@ -445,6 +451,14 @@ class ConnectionNode(RpcNode): if e.sw == SW.INVALID_INSTRUCTION: raise ChildResetException(f"SW: {e.sw}") raise e + except CtapError as e: + if e.code == CtapError.ERR.CHANNEL_BUSY: + raise ChildResetException(str(e)) + raise + except Exception as e: # TODO: Replace with ConnectionError once added + if "Wrong" in str(e): + raise ChildResetException(str(e)) + raise @property def capabilities(self):