Created
January 6, 2022 12:16
-
-
Save antonio-fr/56e84dd019302d5bbebe6456b053a43a to your computer and use it in GitHub Desktop.
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
# Check about the pyscard issue "Smart Card Resource Manager has shut down" #123, | |
from importlib.metadata import version | |
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) | |
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 | |
print("Testing pyscard Windows") | |
print("pyscard version", version('pyscard')) | |
input("\nConnect a YubiKey, and press RETURN") | |
yubn_detect = print_yubis() | |
input("\nDisconnect all the reader, all Yubikeys, and press RETURN") | |
input("\nReconnect a Yubikey, and press RETURN") | |
try: | |
print_yubis() | |
except Exception as exc: | |
print("Error !!!") | |
print(exc) | |
print("\nNow with readers list") | |
readers_list = readers() | |
if len(readers_list) > 0: | |
for r in readers_list: | |
if str(r).startswith("Yubico"): | |
connection = r.createConnection() | |
connection.connect() | |
yubSN = get_yubico5_serial(connection) | |
print("A YubiKey detected :") | |
print(f" - SN {yubSN}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment