Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

smartcard.Exceptions.CardConnectionException: Unable to connect with protocol: T0 or T1. Reader is unavailable. #559

Open
Torxed opened this issue Jul 12, 2023 · 0 comments

Comments

@Torxed
Copy link
Contributor

Torxed commented Jul 12, 2023

  • YubiKey Manager (ykman) version: 5.1.1
  • How was it installed?: pacman -S ykman
  • Operating system and version: Arch Linux
  • opensc version: 0.23.0-1
  • ccid version: 1.5.2-1
  • YubiKey model and version: NFC 5C
  • Bug description summary: Whenever I run the Python example codes from https://developers.yubico.com/yubikey-manager/Library_Usage.html - the application errors out in one of it's threads (?) when I remove the yubikey from the machine.

Steps to reproduce

Install ykman, run for instance:

from ykman.device import list_all_devices, scan_devices
from yubikit.core.smartcard import SmartCardConnection
from time import sleep

handled_serials = set()  # Keep track of YubiKeys we've already handled.
state = None

while True:  # Run this until we stop the script with Ctrl+C
    pids, new_state = scan_devices()
    if new_state != state:
        state = new_state  # State has changed
        for device, info in list_all_devices():
            if info.serial not in handled_serials:  # Unhandled YubiKey
                print(f"Programming YubiKey with serial: {info.serial}")
                ...  # Do something with the device here.
                handled_serials.add(info.serial)
    else:
        sleep(1.0)  # No change, sleep for 1 second.

Wait a second for it to be detected, then unplug the Yubikey at any point.

Expected result

The code to only print the print() message, nothing else.

Actual results and logs

Right when I unplug the yubikey:

Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/ykman/device.py", line 168, in add
    with dev.open_connection(conn_type) as conn:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/ykman/pcsc/__init__.py", line 88, in open_connection
    return self._open_smartcard_connection()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/ykman/pcsc/__init__.py", line 100, in _open_smartcard_connection
    raise e
  File "/usr/lib/python3.11/site-packages/ykman/pcsc/__init__.py", line 96, in _open_smartcard_connection
    return ScardSmartCardConnection(self.reader.createConnection())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/ykman/pcsc/__init__.py", line 106, in __init__
    connection.connect()
  File "/usr/lib/python3.11/site-packages/smartcard/CardConnectionDecorator.py", line 54, in connect
    self.component.connect(protocol, mode, disposition)
  File "/usr/lib/python3.11/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 122, in connect
    raise CardConnectionException(
smartcard.Exceptions.CardConnectionException: Unable to connect with protocol: T0 or T1. Reader is unavailable.
Failed opening device
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/ykman/device.py", line 168, in add
    with dev.open_connection(conn_type) as conn:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/ykman/pcsc/__init__.py", line 88, in open_connection
    return self._open_smartcard_connection()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/ykman/pcsc/__init__.py", line 100, in _open_smartcard_connection
    raise e
  File "/usr/lib/python3.11/site-packages/ykman/pcsc/__init__.py", line 96, in _open_smartcard_connection
    return ScardSmartCardConnection(self.reader.createConnection())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/ykman/pcsc/__init__.py", line 106, in __init__
    connection.connect()
  File "/usr/lib/python3.11/site-packages/smartcard/CardConnectionDecorator.py", line 54, in connect
    self.component.connect(protocol, mode, disposition)
  File "/usr/lib/python3.11/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 122, in connect
    raise CardConnectionException(
smartcard.Exceptions.CardConnectionException: Unable to connect with protocol: T0 or T1. Reader is unavailable.

Other info

The weird thing for me is that this is triggered "elsewhere"? My code is never referenced in the stack trace. So I have no idea how to mute this with a try/except.

My /etc/opensc.conf file:

app default {
	# debug = 3;
	# debug_file = opensc-debug.txt;
	framework pkcs15 {
		# use_file_caching = public;
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant