LudovicRousseau / pyscard

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

Reader attribute SCARD_ATTR_VENDOR_IFD_SERIAL_NO missing last byte #98

Closed palu3492 closed 4 years ago

palu3492 commented 4 years ago

I'm using an OMNIKEY 5427 CK reader and I'm having problems getting the serial number of the reader when using getAttrib on the reader connection. It seems to be short a byte. When I use the "OMNIKEY Specific Commands" and send an APDU command to the "Reader Information API" for the reader serial number, I get the correct serial number. Here are my code and output.

reader = readers()[0]
connection = reader.createConnection()
connection.connect()
# OMNIKEY Reader Information API
serial_number_apdu = [255, 112, 7, 107, 8, 162, 6, 160, 4, 160, 2, 146, 0, 0]
data, sw1, sw2 = connection.transmit(serial_number_apdu)
sn = toHexString(data[6:], PACK)
print(sn)

sn = connection.getAttrib(SCARD_ATTR_VENDOR_IFD_SERIAL_NO)
sn = hl2bs(sn)
print(sn)

Out:

010100534310112202019E563430630
010100534310112202019E56343063

The OMNIKEY web interface to my reader also shows the serial number with a trailing zero.

LudovicRousseau commented 4 years ago

What operating system do you use?

palu3492 commented 4 years ago

Ubuntu 16.04 virtual machine on Mac.

LudovicRousseau commented 4 years ago

The serial number reported by SCARD_ATTR_VENDOR_IFD_SERIAL_NO comes from the USB descriptor. You should be able to see it using lsusb -v | grep iSerial. What do you get in that case?

I did not know if was also possible to get the device serial number using an APDU.

palu3492 commented 4 years ago
$ lsusb -vd 076b:5427 | grep iSerial
iSerial  3  010100534310112202019E563430630

It indeed shows the device's serial number as having a zero at the end.

LudovicRousseau commented 4 years ago

So that is strange. I will have a look.

LudovicRousseau commented 4 years ago

Can you generate a pcscd log as documented at https://pcsclite.apdu.fr/#support ?

LudovicRousseau commented 4 years ago

Can you also use print(reader) in your code and copy the result?

The serial number should be in the PC/SC reader name. https://ludovicrousseau.blogspot.com/2010/05/what-is-in-pcsc-reader-name.html

palu3492 commented 4 years ago

Sure thing. The output from printing the reader:

HID OMNIKEY 5427 CK (010100534E5634303101122020194630) 00 00

Again, the output from connection.getAttrib(SCARD_ATTR_VENDOR_IFD_SERIAL_NO) as decimal and ascii:

[48, 49, 48, 49, 48, 48, 53, 51, 52, 69, 53, 54, 51, 52, 51, 48, 51, 49, 48, 49, 49, 50, 50, 48, 50, 48, 49, 57, 52, 54, 51]
010100534E563430310112202019463

The pcscd log is attached. log.txt

LudovicRousseau commented 4 years ago

Unfortunately the log file does not contain anything useful for the debug :-( I can't reproduce the problem on my side. Also I have a different behavior:

Broadcom Corp 5880 [Contacted SmartCard] (0123456789ABCD) 00 00
[48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 0]
0123456789ABCD

The serial number is NOT truncated. And I also get the final NUL byte in the Attribute buffer.

I fixed a problem with SCARD_ATTR_VENDOR_IFD_SERIAL_NO in the CCID driver version 1.4.24. https://ludovicrousseau.blogspot.com/2016/05/new-version-of-libccid-1424.html Ubuntu 16.04 provides libccid version 1.4.22-1. Please upgrade this driver and try again.

palu3492 commented 4 years ago

Interesting... I already have 1.4.22-1. That doesn't seem to be the problem. I tweaked your C code from https://ludovicrousseau.blogspot.com/2010/04/pcsc-sample-in-c.html and I was able to use SCardGetAttrib to return the correct serial number. Not sure if that means anything... Here's that truncated code:

LPBYTE pbAttr = NULL;
DWORD cByte = SCARD_AUTOALLOCATE;
DWORD w;
LONG lReturn;
lReturn = SCardGetAttrib(hCard,
                         65795,
                         (LPBYTE)&pbAttr,
                         &cByte);

for (w = 0; w < cByte; w++)
    printf("%c", *(pbAttr+w));

Copied from the example at https://docs.microsoft.com/en-us/windows/win32/api/winscard/nf-winscard-scardgetattrib

LudovicRousseau commented 4 years ago

1.4.22-1 is the problem. Upgrade to 1.4.24 or later.

palu3492 commented 4 years ago

Ah, yes, I read your message incorrectly 🤦‍♂️ That fixed it. Thank you very much.