LudovicRousseau / pyscard

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

SLE4442 card unresponsive after write #137

Closed mvzlb closed 1 year ago

mvzlb commented 1 year ago

Your system information

Please describe your issue in as much detail as possible:

I'm expecting to write and retrieve binary data to/from to a blank SLE4442 memory card using a Cherry ST-1144 terminal via the "Update Binary" command as documented in https://www.hidglobal.de/sites/default/files/resource_files/plt-03099_a.4_-_omnikey_sw_dev_guide.pdf (8.1.2)

I have a few blank memory card here that I can read using pyscard, but become completely unresponsive after writing data. This happened for two cards now.

Steps for reproducing this issue:

Reading blank card:

#!/usr/bin/env python3
from smartcard.CardRequest import CardRequest
from smartcard.util import toHexString

# request = CardRequest(timeout=None, newcardonly=True)
request = CardRequest(timeout=None, newcardonly=False)
service = request.waitforcard()
service.connection.connect()
print("ATR:", toHexString(service.connection.getATR()))
print("Data:", service.connection.transmit([0xff, 0xb0, 0x00, 0x00, 0x00]))
ATR: 3B 04 A2 13 10 91
Data: ([162, 19, 16, 145, 255, 255, 129, 21, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 210, 118, 0, 0, 4, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255], 144, 0)

Write some data:

#!/usr/bin/env python3
from smartcard.CardRequest import CardRequest
from smartcard.util import toHexString
import subprocess

data = list(subprocess.check_output("date").strip())

# request = CardRequest(timeout=None, newcardonly=True)
request = CardRequest(timeout=None, newcardonly=False)
service = request.waitforcard()
service.connection.connect()
print("ATR:", toHexString(service.connection.getATR()))
print("PIN:", service.connection.transmit([0xff, 0x20, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff]))
print(service.connection.transmit([0xff, 0xd6, 0x00, 0x00, len(data)] + data))
ATR: 3B 04 A2 13 10 91
PIN: ([], 144, 0)
([], 144, 0)

Now the card is unresponsive. Neither reinserting the card or replugging the terminal helps, the card seems to be dead:

Traceback (most recent call last):
  File "/home/**/./read.py", line 8, in <module>
    service.connection.connect()
  File "/usr/lib/python3/dist-packages/smartcard/CardConnectionDecorator.py", line 54, in connect
    self.component.connect(protocol, mode, disposition)
  File "/usr/lib/python3/dist-packages/smartcard/pcsc/PCSCCardConnection.py", line 122, in connect
    raise CardConnectionException(
smartcard.Exceptions.CardConnectionException: Unable to connect with protocol: T0 or T1. Card is unpowered.

pcscd log

Checking the documentation again, I just noticed that I forgot the Le field in the "Update Binary" command. But before I waste another card I'd like to ask:

Thanks in advance!

LudovicRousseau commented 1 year ago

SLE4442 are memory cards, not microprocessor cards.

I do not know the SLE4442 cards. If the documentation says Le is mandatory then just add it. But I note that the card (or reader) answered with 90 00 to the write command. So it looks like the command worked.

Maybe you wrote something in the wrong place that killed the card.

It is not a problem with PySCard. See https://ludovicrousseau.blogspot.com/2020/11/how-to-get-smart-card-support.html

mvzlb commented 1 year ago

Maybe you wrote something in the wrong place that killed the card.

That seems to be the case. If I change the write address from 0x00 to 0x80 everything works as expected (even without Le)!