Created
June 10, 2021 12:17
-
-
Save antonio-fr/cf45fbab77fe4c2784d9f4d1d7f51cc1 to your computer and use it in GitHub Desktop.
PyScard Windows Issue demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Demonstration of the issue "Smart Card Resource Manager has shut down", | |
# appears when disconnecting all readers before reconnect to a card. | |
from smartcard.System import readers | |
from smartcard.CardType import ATRCardType | |
from smartcard.CardRequest import CardRequest | |
from smartcard.util import toBytes | |
from smartcard.Exceptions import CardRequestTimeoutException | |
YUBICO5_ATR_HEX = toBytes("3BFD1300008131FE158073C021C057597562694B657940") | |
YUBICO_MGMT_APPID = toBytes("00A4040008A000000527471117") | |
def get_yubico5_serial(conn): | |
# For Yubico 5 use the YubiKey Management app | |
GETserialY5 = [0x00, 0x1D, 0x00, 0x00] | |
data, sw1, sw2 = conn.transmit(YUBICO_MGMT_APPID) | |
if sw1 != 0x90 or sw2 != 0x00: | |
return 0 | |
data, sw1, sw2 = conn.transmit(GETserialY5) | |
if sw1 != 0x90 or sw2 != 0x00: | |
return 0 | |
if len(data) >= 15 and data[10] == 0x04: | |
sn = 0 | |
for snd in data[11:15]: | |
sn = (sn << 8) + snd | |
return sn | |
else: | |
return 0 | |
def get_yubico_serial(): | |
piv_card_atr = ATRCardType(YUBICO5_ATR_HEX) | |
# With the following line added (uncommented), it doesn't throw | |
# the "Smart Card Resource Manager has shut down" exception : | |
# readers() | |
# Similarly, with createConnection to get the connection from | |
# a reader, it doesn't fail. | |
try: | |
cardrequest = CardRequest(timeout=2, cardType=piv_card_atr) | |
cardservice = cardrequest.waitforcard() | |
except CardRequestTimeoutException: | |
return 0 | |
cardservice.connection.connect() | |
return get_yubico5_serial(cardservice.connection) | |
def print_yubis(): | |
print("") | |
yubi_SN = get_yubico_serial() | |
if yubi_SN > 0: | |
print("A YubiKey detected :") | |
print(f" - SN {yubi_SN}") | |
else: | |
print("No Yubikey detected") | |
return yubi_SN > 0 | |
input("\nConnect a YubiKey, and press RETURN") | |
yubn_detect = print_yubis() | |
if yubn_detect == 0: | |
print("Connect a YubiKey to perform the test.") | |
else: | |
input("\nDisconnect all the reader, all Yubikeys, and press RETURN") | |
input("\nReconnect a Yubikey, and press RETURN") | |
print_yubis() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment