pyauth / python-pkcs11

PKCS#11/Cryptoki support for Python
MIT License
148 stars 70 forks source link

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

Open asergios opened 5 years ago

asergios commented 5 years ago

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.

danni commented 5 years ago

Hi, what HSM are you using?

asergios commented 5 years ago

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 commented 5 years ago

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

danni commented 5 years ago

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.