MatthiasValvekens / pyHanko

pyHanko: sign and stamp PDF files
MIT License
494 stars 71 forks source link

hardware token pkcs11.exceptions.NoSuchKey after upgrading to 0.23.0 #394

Closed C-monC closed 6 months ago

C-monC commented 6 months ago

Describe the bug Using a hardware token stopped working in the recent release of PyHanko.

lib = pkcs11.lib("/usr/lib/libeTPkcs11.so")
token = lib.get_token(token_label="my_token")
with token.open(user_pin="*****") as session:
    for obj in session.get_objects({
        Attribute.CLASS: ObjectClass.PRIVATE_KEY,
    }):
        key_id = obj[Attribute.ID]
        pkcs11_conf = PKCS11SignatureConfig(
            module_path="/usr/lib/libeTPkcs11.so",
            cert_label="tq-a43cf071******1b10724",
            key_id=key_id,
            user_pin="******",
            prompt_pin=PKCS11PinEntryMode.PROMPT,
            token_criteria=TokenCriteria(
                label="my_token", serial=b"027**db"
            ),
        )
with PKCS11SigningContext(pkcs11_conf) as signer:
                    pdf_signer = signers.PdfSigner(  <--------------------------------------- Exception on this line 
                        signature_meta,
                        timestamper=timestamper,
                        signer=signer,  # type: ignore
                        stamp_style=stamp.StaticStampStyle.from_pdf_file(
                            stamp_pdf,
                            border_width=0,
                        ),
                    )

The above code works with pyHanko==0.21.0 and not with pyHanko==0.23.0. I haven't worked through the recent changes yet but I'll get on it soon.

Exception thrown: pkcs11.exceptions.NoSuchKey: No key matching {<Attribute.CLASS>: <ObjectClass.PRIVATE_KEY>, <Attribute.LABEL>: 'tq-a43cf071-2ad1b10724', <Attribute.ID>: b'\x99xa3'}

MatthiasValvekens commented 6 months ago

Hi @C-monC, this was a regression introduced in 0.22.0. I've committed a fix, will do a patch release soon.

MatthiasValvekens commented 6 months ago

Released yesterday as 0.23.1. Thanks for the report, by the way!

C-monC commented 6 months ago

Just confirming it is fixed. Thanks for the speedy patch :)