17

We have an application that creates PDFs unsing jasperreports.
It also manipulates said PDFs using iText after they have been created.

We recently started using encryption on some PDF. That means before the app can handle the PDF after its creation, it has to be decrypted. While attempting to do so using iText's PdfReader(String path, byte[] password) I get the following exception:

java.lang.VerifyError: class org.bouncycastle.asn1.ASN1Primitive overrides final method equals.(Ljava/lang/Object;)Z
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at com.simontuffs.onejar.JarClassLoader.defineClass(JarClassLoader.java:561)
    at com.simontuffs.onejar.JarClassLoader.findClass(JarClassLoader.java:475)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at com.itextpdf.text.pdf.PdfEncryption.<init>(PdfEncryption.java:148)
    at com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj(PdfReader.java:914)
    at com.itextpdf.text.pdf.PdfReader.readDocObj(PdfReader.java:1294)
    at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:643)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:187)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:212)
    at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:202)

The project is built as a runnable .jar using Maven and uses the following dependencies:
iText 5.4.2
bouncycastle 1.48

I should mention that jasperreports has its own dependency of iText and bouncycastle:
iText 2.1.7
bouncycastle 1.38

I can't figure out what's going on on and need help.

m00hk00h
  • 507
  • 2
  • 7
  • 21
  • Can you post a small piece of code and associated POM that demonstrates the error? – Duncan Jones Jun 20 '13 at 11:38
  • 1
    You cannot use different BC versions together in the same context. You might want to replace the old iText/BC couple by SpongyCastle and an iText version patched to use SpongyCastle instead of BouncyCastle. – mkl Jun 20 '13 at 12:38

4 Answers4

35

My best guess is that you have ended up with two different versions of Bouncy Castle on your classpath, and it happened so that the classloader has loaded the superclass from one version and is now trying to load the subclass from the other. The versions are different in that one of them defines a final equals method.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • 11
    +1: `ASN1Primitive` is derived from `ASN1Object.` Up to BC 1.46, `ASN1Object.equals` used to be final but since 1.47 it is not final anymore. `ASN1Primitive` is only present in BC since version 1.47 and overrides `equals.` Most likely, therefore, `ASN1Object` in the OP's is loaded from BC 1.38 and `ASN1Primitive` from BC 1.48. – mkl Jun 20 '13 at 12:35
  • Thanks a lot, Marko. mkl, too. I will try to resolve this issue. Will accept your answer if it really was the cause. – m00hk00h Jun 20 '13 at 13:26
  • Thanks again, that really was the source of the problem. I ended up writing a small encrypter/decrypter-adapter-class. It accepts the encrypted PDFs generated by jasperreports, decrypts them using the bouncycastle-dependency of Jasper/iText 2.1.7 and sends the decrypted PDF to the classes that do all the other shenanigans using iText 5.4.2. This way there is no need for the bouncycastle dependency of iText 5.4.2 which I simply removed now. Works flawlessly. – m00hk00h Jun 21 '13 at 08:58
  • 1
    Thanks! In my case, I was using jruby and my jruby bouncycastle version was different from my project library. Both are now running 1.46 and it's working fine! Java Maven:org.bouncycastle,bcprov-jdk15on,1.46; Ruby gems:bouncy-castle-java (1.5.0146.1);jruby-openssl (0.8.2) – tbraun Oct 09 '13 at 11:56
  • @mserioli On my system I had both the bcprov-jdk14 and the bcprov-jdk15 in revision 1.49. To fix the verifyerror I added the bcprov-ext-jdk14 (which contains the ASN classes in the jdk14 version). I guess another way of fixing it would be to make sure only one of the bcprov version were included. – joensson Apr 11 '14 at 17:29
  • it worked for me. thanks. In my case I had diferent versions in my classpath and in [JAVA_HOME]/jre/lib/ext/ – demian Apr 30 '19 at 13:50
4

Had same error, my solution might come in handy. In my case all i was doing was digital signature of pdf documents, using Maven i had both IText(itextpdf.jar/version 5.4.2) & Bouncycastle(bcprov-jdk15on.jar/version 1.55) dependency in my pom.xml. Then i read a part in this iText book Digital Signatures for PDF documents about Bouncycastle-related problems.I removed the Bouncycastle(bcprov-jdk15on.jar/version 1.55) dependency and the error was gone(noob mistake- the bouncycastle dependency was already part of the itext dependency no need to have the two seperately).

NOTE: if you run into any Bouncycastle class file not found errors after removing bouncycastle dependency check this

Community
  • 1
  • 1
ad3bay0
  • 123
  • 1
  • 9
2

I had similiar problem because I had two different artifacts on my classpath:

<groupId>org.bouncycastle</groupId>
<artifactId>bcprov</artifactId>

and

 <groupId>org.bouncycastle</groupId>
 <artifactId>bcprov-jdk15on</artifactId>

I have excluded the bcprov, which solved the issue for me.

fascynacja
  • 1,625
  • 4
  • 17
  • 35
1

Had the same problem, and it got solved in a wierd way. All i had to do is to add bcprov-ext

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-ext-jdk15on</artifactId>
        <version>1.52</version>
    </dependency>
Faouzi
  • 929
  • 2
  • 15
  • 23