I am using itext7 version 7.1.5 in my application. My scenario is as following: - Take the hash of the document - Sign the hash from external signing server and get Pkcs7 - Embed the signed pkcs7 into the PDF document using itext7
After embedding adobe fails to validate my signature in the document. When I check the signature structure, the signature structure is wrong.
Here is the step wise code:
1: Document Hashing:
string hashAlgorithm = "SHA256";
Stream documentStream = new MemoryStream(_latestDocumentBytes);
PdfSigner _pdfSigner = new PdfSigner(pdfReader, outputStream, new StampingProperties());
_pdfSigner.SetFieldName("Signature1");
ImageData imageData = ImageDataFactory.Create(imageBytes);
PdfSignatureAppearance sigAppearance = _pdfSigner.GetSignatureAppearance();
sigAppearance.SetContact("ContactInfo");
sigAppearance.SetLocation("Location");
sigAppearance.SetPageNumber(1);
sigAppearance.SetReason("SigningReason");
sigAppearance.SetSignatureGraphic(imageData);
sigAppearance.SetRenderingMode(sigAppearance.RenderingMode.GRAPHIC)
sigAppearance.SetSignatureCreator("Malik");
PdfSignature signature = new PdfSignature(PdfName.Adobe_PPKLite, PdfName.Adbe_pkcs7_detached);
signature.SetContact(sigAppearance.GetContact());
signature.SetDate(new PdfDate(DateTime.Now));
signature.SetLocation(sigAppearance.GetLocation());
signature.SetReason(sigAppearance.GetReason());
signature.SetSignatureCreator(sigAppearance.GetSignatureCreator());
signature.MakeIndirect(_pdfDocument);
documentHash = DigestAlgorithms.Digest(documentStream, DigestAlgorithms.SHA256);
SHA256 sha256 = new SHA256Managed();
byte[] documentHash = sha256.ComputeHash(documentHash);
2: Get signing hash from document server
3: Embedding signature to PDF
Stream readerStream = new MemoryStream(_latestDocumentBytes);
PdfPKCS7 pdfPKCS7 = new PdfPKCS7(pdfSignatureBytes, PdfName.Adbe_pkcs7_detached);
PdfSignatureAppearance signatureAppearance = _pdfSigner.GetSignatureAppearance();
signatureAppearance.SetCertificate(pdfPKCS7.GetSigningCertificate());
signature.SetContents(pdfSignatureBytes);
IExternalSignatureContainer externalSignatureContainer = new ExternalBlankSignatureContainer(PdfName.Adobe_PPKLite, PdfName.Adbe_pkcs7_detached);
_pdfSigner.SignExternalContainer(externalSignatureContainer, 8192);
_latestDocumentBytes = ((MemoryStream)outputStream).ToArray();
My signature structure difference is as follows:
Correct signature structure:
<</Type/Sig/Reason(I have approved ad signed the document)/Contents ><[CONTENT]>/Prop_Build<</App<</Name/Malik>>>>/ByteRange [0 10857 522859 2584 >] >/SubFilter/adbe.pkcs7.detached/Filter/Adobe.PPKLite/M(D:20190719103520+00'00>>')/ContactInfo(923399999999)/Name(John Clark)/Location(Pakistan)>>
InCorrect (Itext Implementation):
<</ByteRange [0 157 16543 260086 ] [Large Space] >/ContactInfo([email protected])/Contents ><[CONTENT]>/Filter/Adobe.PPKLite/Location(Pakistan)/M(D:20190719154813+05'00'>)/Prop_Build<</App<</Name/Malik>>>>/Reason(Test Signing >Reason)/SubFilter/adbe.pkcs7.detached/Type/Sig>>
While opening the signed PDF in Adobe, an error displayed while validating the signature that ERROR ENCOUNTERED WHILE BER DECODING
Original document is here: https://www.dropbox.com/s/ajscg8j74opuwxe/SigFieldDoc%20-%20Original.pdf?dl=0
Signed document is here: https://www.dropbox.com/s/h72u360rl5iy6fq/SigFieldDoc%20-%20AfterSign.pdf?dl=0
Any help in this regard will be highly appreciated.
ExternalBlankSignatureContainer
contains the word Blank for a reason... - mkl