python-pkcs11 icon indicating copy to clipboard operation
python-pkcs11 copied to clipboard

struct.error: unpack requires a buffer of 1 bytes at printing objects from SmartCard

Open asergios opened this issue 7 years ago • 4 comments

When I try to print every object available in my Smart Card with: for i in session.get_objects(): print(i)

I always get this error on the second object

Traceback (most recent call last): File "cc_test.py", line 59, in cc.scan() File "cc_test.py", line 50, in scan for i in self.session.get_objects(): File "pkcs11/_pkcs11.pyx", line 271, in pkcs11._pkcs11.SearchIter.next File "pkcs11/_pkcs11.pyx", line 578, in pkcs11._pkcs11.Object._make File "pkcs11/_pkcs11.pyx", line 605, in pkcs11._pkcs11.Object.getitem File "pkcs11/_utils.pyx", line 35, in pkcs11._pkcs11._unpack_attributes File "/lib/python3.7/site-packages/pkcs11/defaults.py", line 113, in _bool = (Struct('?').pack, lambda v: Struct('?').unpack(v)[0]) struct.error: unpack requires a buffer of 1 bytes

Note that this also happens when I try to use get_key() for some keys.

asergios avatar Oct 31 '18 17:10 asergios

Hi, what HSM are you using?

danni avatar Nov 01 '18 02:11 danni

Hi,

I am using the Portuguese Citizen Card. I can't find much information about it in English. https://en.wikipedia.org/wiki/Citizen_Card_(Portugal)

The package overall works good with the card, that's the only error I had so far.

asergios avatar Nov 01 '18 18:11 asergios

It seems like it happens when I try to get the Public Key from my Citizen Card

obj = next(self.session.get_objects({Attribute.CLASS: ObjectClass.PUBLIC_KEY}))

If there is something I can do to help you solve this problem tell me.

The Private Keys work perfectly, the problem seems to be really on the Public Keys

asergios avatar Nov 01 '18 21:11 asergios

It looks like it's failing to read attributes from the device. It's possible those attributes don't exist/return empty.

If you're willing to make a PR, you should be able to add exception handling to _unpack_attributes to handle the error.

danni avatar Nov 01 '18 22:11 danni