Skip to content

Commit

Permalink
Merge PR #53.
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Sep 20, 2023
2 parents af94b07 + 71620fa commit fa0682a
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions yubihsm/backends/usb.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,28 @@ def __init__(self, serial: Optional[int] = None, timeout: Optional[int] = None):
:param serial: (optional) The serial number of the YubiHSM to connect to.
:param timeout: (optional) A read/write timeout in seconds.
"""
err = None
for device in usb.core.find(
find_all=True, idVendor=YUBIHSM_VID, idProduct=YUBIHSM_PID
):
try:
cfg = device.get_active_configuration()
except usb.core.USBError:
cfg = None

if cfg is None or cfg.bConfigurationValue != 0x01:
try:
device.set_configuration(0x01)
except usb.core.USBError as e:
err = YubiHsmConnectionError(e)
continue

if serial is None or int(device.serial_number) == serial:
break
else:
raise YubiHsmConnectionError("No YubiHSM found.")

try:
cfg = device.get_active_configuration()
except usb.core.USBError:
cfg = None

if cfg is None or cfg.bConfigurationValue != 0x01:
try:
device.set_configuration(0x01)
except usb.core.USBError as e:
raise YubiHsmConnectionError(e)
usb.util.dispose_resources(device)
else:
raise err or YubiHsmConnectionError("No YubiHSM found.")

# Flush any data waiting to be read
try:
Expand Down

0 comments on commit fa0682a

Please sign in to comment.