Search code examples
pythonpdfpython-cryptographyasn1crypto

Verifying a pdf signature with Endesive raises an error when accessing SignerInfo native


I am trying to compare a signature with a certificate for a pdf file in python. I found this very nice package called endesive.

I followed the example for verifying a pdf signature and I have something like this:

pdf_file_path = "/workspaces/test.pdf"
data = open(pdf_file_path, 'rb').read()
certificates = (
    open("/workspaces/certificates/pki.pem", 'rt').read(),
    open("/workspaces/certificates/pki-chain.pem", 'rt').read()
    )

(hashok, signatureok, certok) = pdf.verify(data, certificates)
print('signature ok?', signatureok)
print('hash ok?', hashok)
print('cert ok?', certok)

This should be pretty straight forward. I read the pdf, I open the certificates and then I 'pdf.verify' to see that everything is in order.

pdf.verify, at one point calls this: signed_data = cms.ContentInfo.load(bcontents)['content'].native which makes ans1crypto raise this error File "/home/vscode/.local/lib/python3.9/site-packages/asn1crypto/core.py", line 4060, in native raise e repeatedly until it gets to

    ValueError: Unknown element - context class, constructed method, tag 0
    while parsing asn1crypto.core.Sequence
    while parsing asn1crypto.cms.SetOfAny
    while parsing asn1crypto.cms.CMSAttribute
    while parsing asn1crypto.cms.CMSAttributes
    while parsing asn1crypto.cms.SignerInfo

What could go wrong here?


Solution

  • Instead of addressing signer data info like this:

    signature = signed_data['signer_infos'][0].native['signature']

    It should have been addressed like this:

    signature = signed_data['signer_infos'][0]['signature'].native

    This has been addressed here.