1

I am using iText 5.5.5 for Java and I would like to create signed PDF with external signature as follows:

Take the PDF document that should be signed and create PDF with empty signature and provide BASE64 encoded bytes to be signed by external signature mechanism:

PdfReader reader = new PdfReader(src);
FileOutputStream os = new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createSignature(reader, os, '\0');
PdfSignatureAppearance appearance = stamper.getSignatureAppearance();
appearance.setVisibleSignature(new Rectangle(36, 748, 144, 780), 1, "test");
appearance.setCertificate(chain[1]);
ExternalSignatureContainer external = new ExternalBlankSignatureContainer(PdfName.ADOBE_PPKLITE, PdfName.ADBE_PKCS7_DETACHED);
MakeSignature.signExternalContainer(appearance, external, 8192);

InputStream is = appearance.getRangeStream();
byte[] toSign = getBytes(is);
this.b64String = new String(Base64.encode(toSign));

Sign b64String with external signature mechanism providing signature as PKCS#7 signed data in BASE64.

Create ExternalSignatureContainer to have just the PKCS#7 signed data from external signing mechanism:

public class MyExternalSignatureContainer implements ExternalSignatureContainer {
    protected byte[] sig;

    public MyExternalSignatureContainer(byte[] sig) {
        this.sig = sig;
    }

    @Override
    public void modifySigningDictionary(PdfDictionary arg0) {
    }

    @Override
    public byte[] sign(InputStream arg0) throws GeneralSecurityException {
        return sig;
    }
}

Create signed PDF document with MyExternalSignatureContainer:

PdfReader reader = new PdfReader(dest);
FileOutputStream os = new FileOutputStream(signedpdf);
ExternalSignatureContainer external = new MyExternalSignatureContainer(signedData);
MakeSignature.signDeferred(reader, "test", os, external);

But I get on the last line MakeSignature.signDeferred(reader, "test", os, external); the following exception:

com.itextpdf.text.DocumentException: Not enough space

Where is the problem and how to resolve it?

user1563721
  • 1,373
  • 3
  • 28
  • 46
  • Check the link and I hope it will helps you. http://stackoverflow.com/questions/17149053/cant-add-ltv-to-pdf-document-error – Viswanath Donthi Mar 30 '15 at 11:44
  • Hey, Can you help me with this, I have a PKCS7 String (got as a response from a api ) "MIILwwYJKoZIhvcNAQcCoIILtDCCC7ACAQExDzANBglghkgBZQMEAgEFADALBgkqhkiG9w0BBwGg.........." – Mudit Jul 15 '16 at 05:32
  • And I need to sign a pdf using it . I went through your code but I am not able to understand how to apply it. Please help – Mudit Jul 15 '16 at 05:32
  • @user1563721 Please guide me – Mudit Jul 15 '16 at 11:04

2 Answers2

2

You have made an estimation that the signature will fit into 8192 bytes. However, the number of bytes of signature byte[] exceeds 8192, hence the exception Not enough space. For instance: your external signature container returns a signature that measures 10000 bytes. iText tells you that 10000 is bigger than 8192 and that you are asking something that is impossible.

How to fix this: make a better estimate when you create the PDF with the empty signature.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • OK, so I see that the PKCS#7 signed data bytes has 54916 bytes. So I made change in estimation to following: `MakeSignature.signExternalContainer(appearance, external, 55000);`. Then there is no exception, but the file saved in `signedpdf` location has 0 bytes. What is wrong? – user1563721 Mar 30 '15 at 11:44
  • 1
    I don't have a crystal ball. I don't know why something that works for me (and many other people), doesn't work for you. – Bruno Lowagie Mar 30 '15 at 11:50
  • I have figured out that the signed file was saved after stopping the application server (don't know why it was not saved right after calling `MakeSignature`). It seems that this problem is not related to itext library but is somewhere in application server WildFly 8. The above mentioned resolves the exception `Not enough space`. – user1563721 Mar 30 '15 at 16:19
1

Say to your container the size is estimated:

MakeSignature.signExternalContainer(appearance, external, 0);

According to MakeSignature API

estimatedSize - the reserved size for the signature. It will be estimated if 0

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109