Exception has occurred: ValueError not enough values to unpack (expected 1, got 0) #42
-
Hi: I am trying to use pyhanko to perform a Pades-LTV signature using the spanish eID card. First I was trying with the CLI: pyhanko sign addsig pkcs11 --lib /usr/lib64/pkcs11/opensc-pkcs11.so --token-label 'DNI electrónico (PIN1)' --cert-label CertFirmaDigital lorem_field.pdf lorem-f.pdf and get the following error, which I suppose is related to not being able to get the right certificate from the card: Traceback (most recent call last): Then, I tried to do it using a python code which it is as follows:
And get the same error in "out=signers.sign_pdf (....)" whic is "Exception has occurred: ValueError But using:
I am getting the signature certificate. Any help will be appreciated |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments
-
From the stack trace, it appears that pyHanko isn't able to find the private key handle (although the error message is definitely less than enlightening — I'll do something about that). Probably, the PKCS#11 label of the private key is not the same as that of the certificate object. Can you try to list the labels of the keys on the card using a tool like Let me know if that helps. Incidentally, you might also want to pass in "CertFirmaDigital" as the PS: The ECDSA signing commands for PKCS#11 are almost completely untested, since SoftHSMv2 doesn't (didn't?) support ECDSA properly when I wrote the tests for it. So far, there's no indication that points towards that being the issue here, though. PPS: By the way, validating against the validation contexts from the test suite won't help you, I'm afraid. Those have been set up to only accept the trust roots from my testing CAs. You'll want to set up a validation context with at least one trust root that covers your ID cert. If there's such a trust root in the system trust store, initialising your validation context without any arguments might already be sufficient. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply: List of label of the keys using the pkcs11-tool are: As you can see, there is public labelled "KpuFirmaDigital". I have tried to pass it as the key_label parameter in the PKCS11Signer with no luck. Same error. I have also used your improvement in the code to get an error message on a missing key handle, but have the same error on "signers.sign.pdf(....)" on the "test_wrong_key_label(buk_fetch)" function. This also happens when the CLI is used where you do not specify the name of the cert to use, so is it possible that this is not related with the key label management? Thanks again |
Beta Was this translation helpful? Give feedback.
-
Just to confirm: I'm not seeing any private keys in the Anyway, the certificates and public key objects won't help you when signing things, so you'll need to find a way to talk to the private keys :/ |
Beta Was this translation helpful? Give feedback.
-
For comparison: this is what I see on my Belgian eID. Note in particular that there are two objects of type "Private Key" (labelled "Authentication" and "Signature", respectively).
|
Beta Was this translation helpful? Give feedback.
-
Hi Mathias, you were right: After logging in my eID card, I got the label of the private key which was "KprivFirmaDigital". So it seems there is not error if I pass this as key_value parameter to the PKCS11Signer constructor. Now I am struggling how to save this in a pdf after the sign to check if this has worked. Anyway, thanks for your help and congratulation for your code. Fernando |
Beta Was this translation helpful? Give feedback.
-
Haha, good to hear that that part got solved ;) As for saving the output to a file: there you have multiple options. Assuming that you're calling
PyHanko should take care of the buffer management behind the scenes. If the input buffer is writable as well, you can also pass For what it's worth, the PKCS#11 signer is also available directly from the CLI (with I'll keep this issue open for now; let me know if that solves your use case. |
Beta Was this translation helpful? Give feedback.
-
Well, that works like a charm and now I am able to get a valid pades-b signature. Thank you very much.
Obviously, I am not managing / understanding the "SigSeedValueSpec" in a proper way. Last code I made to get the subject, issuer and serial number was:
Thanks Matthias |
Beta Was this translation helpful? Give feedback.
-
A couple things:
To put in a custom appearance, you'll have to work directly with the (slightly more detailed) API on which
Granted, it's a bit complicated, but the APIs are there. :) I've made a note to add a proper example to the documentation when I find the time, but I can't promise when that'll happen. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your help Matthias. All working now. And once more, congratulations for your code. Best regards |
Beta Was this translation helpful? Give feedback.
-
Great! And thank you for making good use of it :) I'll go ahead and close the issue now, if you don't mind. |
Beta Was this translation helpful? Give feedback.
A couple things:
To put in a custom appearance, you'll have to work directly with the (slightly more detailed) API on which
signers.sign_pdf()
is built…