Infineon / BlockchainSecurity2Go-Python-Library

Python library for the Blockchain Security 2Go starter kit
https://infineon.com/blockchain
MIT License
16 stars 8 forks source link

blocksec2go always looks for the wrong interface instead of using contactless #26

Closed nicexe closed 4 years ago

nicexe commented 4 years ago

First, I'm listing the readers

$ blocksec2go --loglevel info list_readers
Identiv Identiv uTrust 4701 F Dual Interface Reader

Then I try to get the card info

$ blocksec2go --loglevel info --reader "Identiv Identiv uTrust 4701 F Dual Interface Reader" get_card_info
Traceback (most recent call last):
  File "/usr/local/bin/blocksec2go", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/site-packages/blocksec2go/cli/main.py", line 60, in main
    raise e
  File "/usr/local/lib/python3.8/site-packages/blocksec2go/cli/main.py", line 47, in main
    args.func(args)
  File "/usr/local/lib/python3.8/site-packages/blocksec2go/cli/get_card_info.py", line 9, in _get_card_info
    reader = open_pyscard(args.reader)
  File "/usr/local/lib/python3.8/site-packages/blocksec2go/comm/pyscard.py", line 45, in open_pyscard
    return PySCardReader(PCSCReader(name).createConnection())
  File "/usr/local/lib/python3.8/site-packages/blocksec2go/comm/pyscard.py", line 16, in __init__
    self.connection.connect()
  File "/usr/local/lib/python3.8/site-packages/smartcard/CardConnectionDecorator.py", line 54, in connect
    self.component.connect(protocol, mode, disposition)
  File "/usr/local/lib/python3.8/site-packages/smartcard/pcsc/PCSCCardConnection.py", line 121, in connect
    raise NoCardException('Unable to connect', hresult=hresult)
smartcard.Exceptions.NoCardException: Unable to connect: No smart card inserted. (0x-7FEFFFF4)

The same happens when the card either on or off the reader. And this happens when I enter a payment card in the reader just as a test

$ blocksec2go --loglevel info --reader "Identiv Identiv uTrust 4701 F Dual Interface Reader" get_card_info
Card indicated failure: 0x6a82 Security status not satisfied - counter exceeded

Is there a way to use the contactless interface of my Identiv uTrust 4701 F?

nicexe commented 4 years ago

Just to clarify, the payment card that I used as a test was in the card slot of the 4701 F, this isn't the contactless interface. If I place the payment card on top of the reader I'm getting the same error message (Unable to connect: No smart card inserted. (0x-7FEFFFF4))

DhruvKhemani commented 4 years ago

Hey, so from what I see, this issue isn't really related to the blocksec2go library itself, but more your card reader and the underlying smartcard library that blocksec2go uses.

Let me try to explain this in a bit more detail... I have not tested this with your reader (Identiv Identiv uTrust 4701 F Dual Interface Reader), but with mine (Identiv uTrust 3700 F CL Reader) it works as intended (I am on Windows, but this should not really matter...):

C:\>blocksec2go list_readers
Identiv uTrust 3700 F CL Reader 0

When no card is on the reader i get the following:

C:\>blocksec2go --reader "Identiv uTrust 3700 F CL Reader 0" get_card_info
Traceback (most recent call last):
  File "c:\users\*****\appdata\local\programs\python\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\*****\appdata\local\programs\python\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\*****\AppData\Local\Programs\Python\Python37\Scripts\blocksec2go.exe\__main__.py", line 9, in <module>
  File "c:\users\*****\appdata\local\programs\python\python37\lib\site-packages\blocksec2go\cli\main.py", line 60, in main
    raise e
  File "c:\users\*****\appdata\local\programs\python\python37\lib\site-packages\blocksec2go\cli\main.py", line 47, in main
    args.func(args)
  File "c:\users\*****\appdata\local\programs\python\python37\lib\site-packages\blocksec2go\cli\get_card_info.py", line 9, in _get_card_info
    reader = open_pyscard(args.reader)
  File "c:\users\*****\appdata\local\programs\python\python37\lib\site-packages\blocksec2go\comm\pyscard.py", line 45, in open_pyscard
    return PySCardReader(PCSCReader(name).createConnection())
  File "c:\users\*****\appdata\local\programs\python\python37\lib\site-packages\blocksec2go\comm\pyscard.py", line 16, in __init__
    self.connection.connect()
  File "c:\users\*****\appdata\local\programs\python\python37\lib\site-packages\smartcard\CardConnectionDecorator.py", line 54, in connect
    self.component.connect(protocol, mode, disposition)
  File "c:\users\*****\appdata\local\programs\python\python37\lib\site-packages\smartcard\pcsc\PCSCCardConnection.py", line 121, in connect
    raise NoCardException('Unable to connect', hresult=hresult)
smartcard.Exceptions.NoCardException: Unable to connect: The smart card has been removed, so that further communication is not possible.  (0x-7FEFFF97)

Note that the exact reason for why it was not able to connect (in your case No smart card inserted. (0x-7FEFFFF4), in mine The smart card has been removed, so that further communication is not possible. (0x-7FEFFF97)) does not really matter. You can see that the NoCardException is raised in the same line for both of us. This all simply means, that the smartcard library didn't detect a card on the reader at that specific time and because of this, the python smartcard lib/blocksec2go lib was not able to communicate with the card.

If a card was recognized by the reader, and by the smartcard library, then the blocksec2go lib checks if that smartcard is a Blockchain Security 2Go card:

C:\>blocksec2go --reader "Identiv uTrust 3700 F CL Reader 0" get_card_info
PIN is: ENABLED
Card ID (hex): *string of hexadecimal numbers*
Version: v1.0

If a card is recognized, but you use a different smartcard (you said above that you used a "payment card"), it looks like this:

C:\>blocksec2go --reader "Identiv uTrust 3700 F CL Reader 0" get_card_info
Card indicated failure: 0x6a82 Security status not satisfied - counter exceeded

The main problem that you are probably facing is that the smartcard library is having issues detecting a contactless card with your Dual Interface reader. This may be the fault of the reader or the smartcard library. I vaguely remember that some time ago I had tested something similar (a reader with both contact and contactless options) and it worked just fine. Please make sure that when you use the contactless option on your card reader, the card reader recognizes the smartcard (this can easily be seen via the status LED on the reader). If the reader finds a card, but the program still doesn't, try updating the drivers for the card reader. If that still does not fix this problem, then try opening an issue on the pyscard library Github. Maybe there you will be able to find better help with this issue.

nicexe commented 4 years ago

It turns out. I had to restart after installing the drivers for both interfaces to be accessible.