Sign PDF using an external service and iText

后端 未结 2 1024
长情又很酷
长情又很酷 2020-12-11 19:28

I have this scenario.

I have an application that generates a PDF, and that needs to be signed.

We have not the certificates to sign the document, because the

相关标签:
2条回答
  • 2020-12-11 19:53

    If the web service returned a mere signed hash

    In this point, we have the hash code of the document. Then we send the hash to the webservice, and we get the signed hash code.

    Finally, we put the signed hash to the PDF:

    If the webservice merely returns a signed hash, then your PDF signature is incorrect: You set the signature SubFilter to adbe.pkcs7.detached. This implies that the signature Contents have to contain a full-blown PKCS#7 signature container, not merely a signed hash.

    You might want to download Digital Signatures for PDF documents, A White Paper by Bruno Lowagie (iText Software) on creating and verifying digital PDF signatures using iText. It especially contains a section "4.3 Client/server architectures for signing" which should encompass your use cases.

    But the web service returns a full-fledged CMS signature container

    Following to the explanation above, the OP started using code from section 4.3.3 of the above-mentioned white paper which is intended for signing using externally generated signed hashes. As this also resulted in a signed document Adobe Reader was not happy with, he provided a sample document created with this new code.

    Analysis of the sample showed that the CMS signature container embedded in the document contained another CMS signature container where there should have been the signature bytes (the signed hash) for the signed attributes:

    2417   13:           SEQUENCE {
    2419    9:             OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
    2430    0:             NULL
             :             }
    2432 5387:           OCTET STRING, encapsulates {
    2436 NDEF:             SEQUENCE {
    2438    9:               OBJECT IDENTIFIER signedData (1 2 840 113549 1 7 2)
    2449 NDEF:               [0] {
    2451 NDEF:                 SEQUENCE {
    

    (The OCTET STRING following the signature algorithm should contain the signature bytes and not embed another SignedData structure.)

    This indicates that the web service indeed already returns a full-fledged CMS container.

    For such a scenario the original code looked quite ok. The issue might be due to a detail like the use of a wrong hashing algorithm (the original code hashed using SHA1).

    A possible issue: BER encoding

    The CMS signature container from the web service embedded in the CMS container generated by iText from the first sample provided by the OP hints at a possible issue: Looking at the ASN.1 dump above the sizes of the outer structures in the embedded CMS container are often NDEF.

    This indicates that these outer structures are created using the less strict BER (Basic encoding Rules), not the more strict DER (Distinguished Encoding Rules) because the BER option to start a structure without stating its size is forbidden in DER.

    The CMS specification (RFC 3852) referenced from the PDF specification does allow any BER encoding for the outer structures of the container, the PDF specification on the other hand requires:

    the value of Contents shall be a DER-encoded PKCS#7 binary data object containing the signature. The PKCS#7 object shall conform to RFC3852 Cryptographic Message Syntax.

    Strictly speaking, therefore, signature containers embedded in PDFs are required to be DER encoded all over.

    As far as I know no PDF signature validator rejects such signatures as long as the signature container DER-encodes certain pivotal elements. Concerning future tools such signatures are a possible point of failure, though.

    0 讨论(0)
  • 2020-12-11 20:09

    After much debugging, we finally found the problem.

    For some mysterious reason, the method that generates the hash of the document, was executed twice, invalidating the first hash (which we use to send to the service).

    After a refactoring work of the code, the original code worked correctly.

    Very thanks to all people that help me, especially mkl.

    0 讨论(0)
提交回复
热议问题