1

I pulled this example from the following link. I am having problems because I can't get the compiler to find the CMSProcessableInputStream class.

Does anyone have any suggestions?

https://apache.googlesource.com/pdfbox/+/5f032354760374773f7339bbad2678d3281e90ee/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateVisibleSignature.java

This is a snippet of my pom.xml:

        <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>1.8.9</version>
    </dependency>
   <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.6</version>
   </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpg-jdk16</artifactId>
        <version>1.46</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk16</artifactId>
        <version>1.46</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcmail-jdk16</artifactId>
        <version>1.46</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-ext-jdk16</artifactId>
        <version>1.46</version>
    </dependency>

This is the code:

    @Override
public byte[] sign(InputStream content) throws SignatureException,
        IOException {
    CMSProcessableInputStream input = new CMSProcessableInputStream(content);
    CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
    // CertificateChain
    List<Certificate> certList = Arrays.asList(cert);
    CertStore certStore = null;
    try {
        certStore = CertStore.getInstance("Collection",
                new CollectionCertStoreParameters(certList), provider);
        gen.addSigner(privKey, (X509Certificate) certList.get(0),
                CMSSignedGenerator.DIGEST_SHA256);
        gen.addCertificatesAndCRLs(certStore);
        CMSSignedData signedData = gen.generate(input, false, provider);
        return signedData.getEncoded();
    } catch (Exception e) {
        // should be handled
        System.err.println("Error while creating pkcs7 signature.");
        e.printStackTrace();
    }
    throw new RuntimeException("Problem while preparing signature");
}
DenisMP
  • 973
  • 2
  • 16
  • 31

3 Answers3

2

1. The CMSProcessableInputStream class is part of the CreateSignature class (without the word "visible") which is in the same package, which you can find in the source download of PDFBox. Get it here: https://pdfbox.apache.org/downloads.html#recent and click on "pdfbox-1.8.9-src.zip", and then look for pdfbox-1.8.9\examples\src\main\java\org\apache\pdfbox\examples\signature\CreateSignature.java in the zip file.

2. The 1.8.9 version of PDFBox uses bc version 1.44, as shown on the official website:

<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcprov-jdk15</artifactId>
  <version>1.44</version>
</dependency>
<dependency>
  <groupId>org.bouncycastle</groupId>
  <artifactId>bcmail-jdk15</artifactId>
  <version>1.44</version>
</dependency>

Another solution is to use pdfbox-app, which has bc within.

A general hint: use source code that you find with google at your own risk. You don't know what version it is, or if it is correct. Try looking at the official website first.

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • Hi Tilman, I just tried it, but I am still getting same compilation error. – DenisMP Jul 19 '15 at 17:44
  • @DenisMP Sorry, I realize my answer was worthless. I've done some more research, hopefully this will help. – Tilman Hausherr Jul 19 '15 at 17:51
  • Triple thumbs up for your *general hint*. – mkl Jul 19 '15 at 18:20
  • I apologize for being dense, but I copied the code from the [link](https://pdfbox.apache.org/downloads.html#recent) as you suggested and I made my pom look like what you suggested, but I am getting "CMSProcessableInputStream cannot be resolved to a type". Would you have any other suggestions? – DenisMP Jul 19 '15 at 22:02
  • @DenisMP but CMSProcessableInputStream is defined right in that file?! A possible cause for the new error is explained here: https://stackoverflow.com/questions/5579558/why-i-got-cannot-be-resolved-to-a-type-error – Tilman Hausherr Jul 19 '15 at 22:21
  • No it is not defined in the file. Is it supposed to be? – DenisMP Jul 20 '15 at 09:47
  • I am talking about the pdfbox-1.8.9\examples\src\main\java\org\apache\pdfbox\examples\signature\CreateSignature.java in the pdfbox-1.8.9-src.zip file download at https://pdfbox.apache.org/download.cgi. It is at line 294 and has the text "class CMSProcessableInputStream implements CMSProcessable". The file has 321 lines. – Tilman Hausherr Jul 20 '15 at 09:51
  • Ah-ha! Now I see the confusion. I was talking about CreateVisibleSignature. Once I added the CreateSignature class, it now compiles. Thank you some much for your help Tilman and thank you for your patience. Now to see if it works. – DenisMP Jul 20 '15 at 10:24
  • Yeah I see I was also confused. I thought the whole time that you had some incorrect version of the source. I've updated my answer in the hope that it will help other people. – Tilman Hausherr Jul 20 '15 at 10:30
1

Add the following 2 dependencies:

 <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.60</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcmail-jdk15 -->
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcmail-jdk15</artifactId>
        <version>1.46</version>
    </dependency>
AshishGalagali
  • 526
  • 5
  • 4
  • 2
    Having bouncy castle artifacts with different versions in one's project is likely to eventually cause problems. – mkl May 14 '19 at 11:22
0

I copied the necessary classes to my project from here (including CMSProcessableInputStream and CreateSignatureBase): https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/signature/

Broken_Window
  • 2,037
  • 3
  • 21
  • 47