Use of Belgian ID Card (Windows OS) #100
-
I'm trying to make a Proof of Concept on for signing with the bEID-Card. During the signing process, I get errors when I try to connect to a timestamper, but I think this is secondary, as without it, I'm not able to sign the document either ... On one hand it looks required (in the Metadata), but when signing with Acrobat, a TSA server isn't used either) When testing the signed document with the pyHanko library itself, I get the error; When adding TSA during signing, it looks like additional certificates are needed. The basics of my tests (although there isn't much more, I added the full code in attachment): sess = open_beid_session(useDLL) #Validation Context #Timestamp client -- Doesn't seem to work yet ... which certicates to add? filenameUnsigned = r'c:\python\PyHanko\Document_Unsigned.pdf' with open(filenameUnsigned, 'rb') as docUnsigned:
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Hi @AlainCNpt That particular error likely arises because you're trying to decode malformed ASN.1 data somewhere (constructed/primitive are ASN.1 terminology). I'd need to see some sample data or at least a stack trace from Regardless, there are a couple things that stand out in your code:
All that being said, I don't know for sure what the root cause of the ASN.1 parse error would be. Final question: how recent is your eID? Do you have one of the newer 1.8 cards (the ones that use ECDSA)? It shouldn't matter, but not having access to one of those, I never actually tried using them with pyHanko, so you never know... Anyway, hopefully that already helps somewhat. |
Beta Was this translation helpful? Give feedback.
-
HI Matthias, Thanks for the reply. I tried to get any further, but I wasn't very successful. I exported the Belgium Root CA, and the Citizen CA from the card, but adding them - one at the time - in the ValidationContext didn't make improvements. I also tried to make some changes with the Signature-metadata, but I didn't notice anything that looked/worked better. The fieldname was changed as well, but the period didn't bother Acrobat Reader (but it still isn't best practice to work this way). My card was issued in 2017, so not that recent. The rest of the week I can't make priorities on this anymore, if you feel like verifying if you get better results, I add my testdata. I made a version with my normal way of adding signature fields, and one based on your library, to make sure the field isn't broken at the first place... Thanks, |
Beta Was this translation helpful? Give feedback.
-
Didn't find that much time yet, but tried to extract the root certificate from the card ... It doesn't raise errors this way (when signing), but still no luck getting a valid signature (not even an invalid one when using a pyHanko-reader) The part to read all/one certificates: certRoot = sess.get_key(object_class=ObjectClass.CERTIFICATE, label='Root') ##Validation Context |
Beta Was this translation helpful? Give feedback.
-
Hi @AlainCNpt, I finally found the time to look at your actual files (thanks for sending those along!) and the problem is now obvious---sorry I didn't spot it earlier... The signature container in your signed file is zeroed out. The reason why that is the case is because you're overwriting the content of the signed file with the contents of the incremental updater in its state prior to the signature being produced. In other words, these lines are the problem: out = signers.sign_pdf(w, signature_meta, signer=beid) # , timestamper=tsa_client)
docSigned = open(filenameSigned, 'wb')
w.write(docSigned)
docSigned.close() In actuality, the output you want is in with open(filenameSigned, 'wb') as outf:
signers.sign_pdf(w, signature_meta, signer=beid, output=outf) or alternatively out = signers.sign_pdf(w, signature_meta, signer=beid)
with open(filenameSigned, 'wb') as outf:
buf = out.getbuffer()
outf.write(buf)
buf.release() Sorry, I should really have noticed that mistake the first time around. For future reference: the rule of thumb is that the content of a PDF writer is no longer relevant after you pass it through |
Beta Was this translation helpful? Give feedback.
Hi @AlainCNpt, I finally found the time to look at your actual files (thanks for sending those along!) and the problem is now obvious---sorry I didn't spot it earlier...
The signature container in your signed file is zeroed out. The reason why that is the case is because you're overwriting the content of the signed file with the contents of the incremental updater in its state prior to the signature being produced.
In other words, these lines are the problem:
In actuality, the output you want is in
out
, not in the file pointed to by