Skip to content
Advertisement

Message digest in a base64 encoded signed attributes DER structure

I have the following ASN1 ASN.1 dump

JavaScript

and I understand that the OCTET STRING is the messageDigest(hash sha-256) of what I am trying to sign. Which in this case is a PDF document using PDFBOX the code I’m using to sign is the following

JavaScript

I have also calculated the sha-256 of the document I am trying to sign and the result is the following

JavaScript

So my question is, is the message digest from the ANS1 the same as the one I calculated? And if so how do I reach that result as when I’m going through the ASN1 structure with the following code I have not been able to get the same result

JavaScript

and the using the following code to convert the bytes to hex

JavaScript

I get the following result

JavaScript

Which is the OCTET STRING of the ASN1 dump but its not the hash of the document. And that Octet String is always changing so I can assume its actually not a regular message digest. So what exactly is it and am I able to get the sha-256 of the content I’m sending to sign

Advertisement

Answer

In Short

The document hash is not calculated from the original PDF you want to sign. That PDF first is prepared for signing by applying certain changes, and then the hash is calculated from this prepared PDF except a placeholder gap in it prepared to later house the signature container.

In Detail

To create an integrated PDF signature, certain changes have to be applied to the PDF:

  • The holder of the to-be-integrated signature is an AcroForm form field in the PDF. If the PDF does not contain an empty, unused signature field (or no existing field shall be used), a new signature field has to be added to the PDF.
  • A signature form field may have a visualization, a widget annotation, which represents the signature on some page of the document itself. If such a visualization is desired, a matching annotation has to be added to the PDF.
  • Information describing the mode and other details of signing have to be added to the PDF. Thus, the value of the chosen signature field has to be set to a new dictionary object in the PDF with these signature details; there are two special entries here, the ByteRange and the Contents. Both are set to blank values of appropriate size for starters.
  • A marker is added to the PDF root AcroForm object indicating that the PDF is signed.

With these additions the PDF is stored. Thereafter the position of the Contents value in the file is fixed and the blank value of the ByteRange value is patched to an array of four integers, the start offset and size of the file segment before the Contents value and the start offset and size of the file segment thereafter.

Then the bytes of these segments of the file are hashed and a CMS signature container signing this document hash is generated which in turn is injected into the Contents value.


In your case the hash you find in the to-be-signed attributes,

JavaScript

is the hash over those two segments of the prepared file which almost always will differ from the hash over the original PDF, like in your case where that is

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement