LudovicRousseau / pyscard

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

Errors when running pysim with Gemalto IDBridge K3000 #106

Closed nkakouros closed 3 years ago

nkakouros commented 3 years ago

Your system information

Please describe your issue in as much detail as possible:

I am trying to use pySim to read a card with the Gemalto IDBrdige K3000 device. I know pysim is another project, but python fails at two pyscard modules. When I run pysim, it fails at this:

> ./pySim-read.py -p 0                                                                          
Using PC/SC reader interface
Traceback (most recent call last):
  File "/tmp/pysim/./pySim-read.py", line 90, in <module>
    sl.wait_for_card()
  File "/tmp/pysim/pySim/transport/pcsc.py", line 49, in wait_for_card
    cr.waitforcard()
  File "/usr/lib/python3.9/site-packages/smartcard/CardRequest.py", line 69, in waitforcard
    return self.pcsccardrequest.waitforcard()
  File "/usr/lib/python3.9/site-packages/smartcard/pcsc/PCSCCardRequest.py", line 137, in waitforcard
    hresult, newstates = SCardGetStatusChange(
  File "/usr/lib/python3.9/site-packages/smartcard/scard/scard.py", line 614, in SCardGetStatusChange
    return _scard.SCardGetStatusChange(hcontext, dwTimeout, readerstatelist)
UnicodeEncodeError: 'ascii' codec can't encode character '\uff20' in position 39: ordinal not in range(128)

If I print the readerstatelist as it is passed to the SCardGetStatusChange function, I get:

[('Gemalto IDBridge K3000 [IDBridge K3000 @5F001E52] (C840357100001E52) 00 00', 0)]

where the character prints as a square with a question mark.

I then edit the /usr/lib/python3.9/site-packages/smartcard/scard/scard.py file and replace this unicode character:

    readerstatelist = [(readerstatelist[0][0].replace('\uff20', ''), readerstatelist[0][1])]

Then, execution proceeds and fails a few lines down:

Using PC/SC reader interface
Traceback (most recent call last):
  File "/tmp/pysim/./pySim-read.py", line 90, in <module>
    sl.wait_for_card()
  File "/tmp/pysim/pySim/transport/pcsc.py", line 49, in wait_for_card
    cr.waitforcard()
  File "/usr/lib/python3.9/site-packages/smartcard/CardRequest.py", line 69, in waitforcard
    return self.pcsccardrequest.waitforcard()
  File "/usr/lib/python3.9/site-packages/smartcard/pcsc/PCSCCardRequest.py", line 161, in waitforcard
    state[1] = state[1] & (0xFFFFFFFF ^ SCARD_STATE_CHANGED)
TypeError: 'tuple' object does not support item assignment

I then convert the tuples into lists:

        for state in enumerate(newstates):
            newstates[state[0]] = list(state[1])

It does not fail, but nothing happens, the code does not return from the waitforcard method.

I have also tried uninstalling version 1.4.34 of the ccid driver and install it again from the master branch of its git repo, but it didn't change anything.

Steps for reproducing this issue:

  1. Download pysim and install dependencies with pip
  2. Plug in Gemalto IDBridge K3000
  3. Run ./pySim-read.py -p 0
LudovicRousseau commented 3 years ago

The text between [] should be the reader interface name. Can you run the command:

lsusb -d 08E6:8141 -v > dump.bin

And attach the dump.bin file.

nkakouros commented 3 years ago

Thank you for your reply! Here is the dump.txt file (.bin is not supported github says).

dump.txt

nkakouros commented 3 years ago

Some more info that may or may not be relevant.

edit:

LudovicRousseau commented 3 years ago

Please apply the pcsc-lite patch, rebuild pcsc-lite and try again. patch.txt

You do not need to reinstall pcsc-lite. Just run the pcscd from the newly build sources. see https://pcsclite.apdu.fr/#support for pcscd arguments.

nkakouros commented 3 years ago

I tried it and it works! Thank you so much for the fix!

LudovicRousseau commented 3 years ago

The problem TypeError: 'tuple' object does not support item assignment is also fixed?

nkakouros commented 3 years ago

Yes, both pySim-read and pySim-prog worked without issues.

LudovicRousseau commented 3 years ago

Fixed in https://github.com/LudovicRousseau/PCSC/commit/b924630b1c3fdb8cbf484fb567728a03f19828ef