LudovicRousseau / pyscard

pyscard smartcard library for python
http://pyscard.sourceforge.net/
GNU Lesser General Public License v2.1
396 stars 113 forks source link

PCSC error codes use wrong sign on ARM/M1 Mac #115

Closed dainnilsson closed 3 years ago

dainnilsson commented 3 years ago

Your system information

Please describe your issue in as much detail as possible:

I noticed that I was getting different Exceptions thrown for the same code in different situations if there was no card on the NFC reader (NoCardException or CardConnectionException). I did some digging and tracked it down to this line: https://github.com/LudovicRousseau/pyscard/blob/master/smartcard/pcsc/PCSCCardConnection.py#L119

I realized that when running an ARM build of Python (from Homebrew) the if-statement wasn't returning true, and realized that the SCARD_E_NO_SMARTCARD constant was different from when running it on a AMD64 build:

ARM python:

>>> from smartcard.scard import SCARD_E_NO_SMARTCARD
>>> SCARD_E_NO_SMARTCARD
2148532236

AMD64 python:

>>> from smartcard.scard import SCARD_E_NO_SMARTCARD
>>> SCARD_E_NO_SMARTCARD
-2146435060

In both cases, the actual result of hresult seemed to be -2146435060, which explains why ARM was giving a different Exception.

I'm not sure where the original value for the constant comes from, but it looks like ARM is treating this as unsigned where it looks like it should be signed.

Steps for reproducing this issue:

Script to reproduce (run with a reader connected, but with no card):

from smartcard import System

for r in System.readers():
    c = r.createConnection()
    c.connect()

Throw when running an ARM64 build of python (from python.org): smartcard.Exceptions.NoCardException: Unable to connect: No smart card inserted. (0x-7FEFFFF4)

Thrown when running ARM build of python (from Homebrew): smartcard.Exceptions.CardConnectionException: Unable to connect with protocol: T0 or T1. No smart card inserted.

LudovicRousseau commented 3 years ago

It is not a problem with M1 vs Intel, but with Big Sur. Thanks for the bug report.