javax.crypto.BadPaddingException when trying to license Aspose.PDF

Hi there,

we’re getting an exception when settting the license for Aspose PDF. Mails, Cells, Slides and Words works fine with the same license (we have Aspose Total for Java licensed). We checked with version 18.9 and 18.9.1.

This is the code we’re using:

public void checkLicense() throws RenditionException {
    // only check if one license is not set
    if (slidesLicense == null || wordsLicense == null || cellsLicense == null || pdfLicense == null) {
        // load license
        InputStream in = RenditionServiceImpl.class.getResourceAsStream(ASPOSE_LICENSE);

        try {
            // throw exception if license has not been found
            if (in == null) {
                throw RenditionException.licenseNotAvailable(ASPOSE_LICENSE);
            }

            byte[] license = IOUtils.toByteArray(in);
            if (slidesLicense == null) {
                slidesLicense = new com.aspose.slides.License();
                slidesLicense.setLicense(new ByteArrayInputStream(license));
            }
            if (wordsLicense == null) {
                wordsLicense = new com.aspose.words.License();
                wordsLicense.setLicense(new ByteArrayInputStream(license));
            }
            if (cellsLicense == null) {
                cellsLicense = new com.aspose.cells.License();
                cellsLicense.setLicense(new ByteArrayInputStream(license));
            }
            if (pdfLicense == null) {
                pdfLicense = new com.aspose.pdf.License();
                pdfLicense.setLicense(new ByteArrayInputStream(license));
            }
            if (mailLicense == null) {
                mailLicense = new com.aspose.email.License();
                mailLicense.setLicense(new ByteArrayInputStream(license));
            }
        } catch (Exception ex) {
            throw RenditionException.invalidLicense(ex);
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
}

We’re getting the following exception when licensing the PDF module:

java.security.SignatureException: Signature decryption error: javax.crypto.BadPaddingException: Invalid padding!

Java Version:

[root@ima /]# java -version
java version “1.8.0_181”
Java™ SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot™ 64-Bit Server VM (build 25.181-b13, mixed mode)

OS: CentOS 7

aspose pdf license exception stacktrace.png (52.2 KB) (sorry, only have a screenshot)

@tim.weller

Thank you for contacting support.

Would you please narrow down the problem and check if it occurs when the license is set for Aspose.PDF API only. In case you still face the issue, then please share a sample application reproducing this issue so that we may try to reproduce and investigate it in our environment. Please also share your license file with me via private message. You may click on my user name and select message option.

Hi @Farhan.Raza,

it literally occurs when trying to set the license, so the sample would be the above code. I’m not sure how to narrow this down more. I will send you the license via PM. We’re running into the exception at this line: pdfLicense.setLicense(new ByteArrayInputStream(license));

Kind regards,
Tim

@tim.weller

Thank you for sharing requested data.

We have modified your code snippet a little to check if the problem occurs when we set license for Aspose.PDF API only. Neither this approach, nor setting the license for multiple APIs in parallel, reproduce any issue. Can you please check if the same error occurs when license is set for Aspose.PDF only, so that we can have narrowed down information about the problem.

static com.aspose.cells.License cellsLicense = null;
static com.aspose.pdf.License pdfLicense = null;
static com.aspose.words.License wordsLicense = null;

static public void checkLicense() throws FileNotFoundException {
// only check if one license is not set
if (cellsLicense == null || pdfLicense == null || wordsLicense == null) {
    // load license
    FileInputStream in = new java.io.FileInputStream("D:\\Aspose.Total.Java.lic");
    try {
        // throw exception if license has not been found
        if (in == null) {
        }
        byte[] license = IOUtils.toByteArray(in);
        if (wordsLicense == null) {
            wordsLicense = new com.aspose.words.License();
            wordsLicense.setLicense(new ByteArrayInputStream(license));
        }
        if (wordsLicense.isLicensed()) {
        System.out.println("Words License is Set!");
        }
        if (cellsLicense == null) {
            cellsLicense = new com.aspose.cells.License();
            cellsLicense.setLicense(new ByteArrayInputStream(license));
        }
        if (cellsLicense.isLicenseSet()) {
        System.out.println("Cells License is Set!");
        }
        if (pdfLicense == null) {
            pdfLicense = new com.aspose.pdf.License();
            pdfLicense.setLicense(new ByteArrayInputStream(license));
        }
        if (com.aspose.pdf.Document.isLicensed()) {
        System.out.println("PDF License is Set!");
        }
    } catch (Exception ex) {
        //throw RenditionException.invalidLicense(ex);
    } finally {
        IOUtils.closeQuietly(in);
    }
}
}

Please share you kind feedback with us if you still face the problem. We will be more than happy to assist you further.

@Farhan.Raza

We managed to narrow down the issue. We’re using IAIK (https://jce.iaik.tugraz.at/) internally as our cryptographic provider. There seems to be an issue with the first attempt to license Aspose.PDF, the following attempts are working fine.

The following sample reproduces it everytime:

public class Main {
	private static final String ASPOSE_LICENSE = "/Aspose.Total.Java.lic";

	public static void main(String[] args) throws Exception {
		IAIK.addAsProvider();

		int attempts = 5;
		for (int i = 0; i < attempts; i++) {
			setLicense(i);
		}
	}

	private static void setLicense(int i) throws Exception {
		System.out.println("attempt: " + (i+1));
		try (InputStream in = Main.class.getResourceAsStream(ASPOSE_LICENSE)) {
			new com.aspose.pdf.License().setLicense(in);
		} catch (SignatureException e) {
			System.out.println("SignatureException occurred!");
			e.printStackTrace(System.out);
		}
		System.out.println("attempt finished: " + (i+1));
	}
}

You need Aspose.PDF jar, the above test jar and the IAIK jar (I cannot provide that) on the classpath for it to work, the output will be:

[root@ima]# java -cp “asposelicensetest.jar:lib/*” com.tsystems.ima.asposelicensetest.Main
attempt: 1
SignatureException occurred!
java.security.SignatureException: Signature decryption error: javax.crypto.BadPaddingException: Invalid padding!
at iaik.security.rsa.RSASignature.engineVerify(Unknown Source)
at java.security.Signature$Delegate.engineVerify(Signature.java:1222)
at java.security.Signature.verify(Signature.java:655)
at com.aspose.pdf.I23I.lif(Unknown Source)
at com.aspose.pdf.I23I.l0I(Unknown Source)
at com.aspose.pdf.I23I.lif(Unknown Source)
at com.aspose.pdf.License.setLicense(Unknown Source)
at com.tsystems.ima.asposelicensetest.Main.setLicense(Main.java:23)
at com.tsystems.ima.asposelicensetest.Main.main(Main.java:16)
attempt finished: 1
attempt: 2
attempt finished: 2
attempt: 3
attempt finished: 3
attempt: 4
attempt finished: 4
attempt: 5
attempt finished: 5

The problem seems to be introduced with Aspose.PDF 18.3, versions before that work fine.

Just fyi, this guy seems to have the same issue: Aspose PDF Java 18.3 on AIX error: Signature decryption error

I know that this might be an issue of either IAIK or Aspose - not sure since I have no sources for both, I just wanted to share our findings with you.

Kind regards,
Tim

asposelicensetest.zip (2.3 KB)

@tim.weller

Thank you for elaborating it further.

Would you please share a sample application along with all necessary resources so that we may try to reproduce and investigate it to sort out this problem.

@Farhan.Raza I attached the jar to my previous post. The call is there as well. We also found out the following:

  • It looks like multiple RSA checks are done in the first attempt
  • In the second attempt there are less RSA checks

The BadPaddingException returns false in case of the default implementation: https://github.com/frohoff/jdk8u-jdk/blob/master/src/share/classes/sun/security/rsa/RSASignature.java#L199
IAIK does not have this handling and throws the aforementioned SignatureException.

@tim.weller

We have executed the .jar file with command prompt but it ends up as ClassNotFoundException with following details:

Exception in thread "main" java.lang.NoClassDefFoundError: iaik/security/provider/IAIK
    at com.tsystems.ima.asposelicensetest.Main.main(Main.java:12)

Kindly share a sample console application so that we may efficiently reproduce and investigate it in our environment.

@Farhan.Raza I cannot provide that jar (it’s proprietary). The link is above where you could get it for evaluation and or bugfixing.

I only see that Words, Slides, Mail and Cells works fine even with IAIK. Something must be different with PDF since 18.3.

You decide if it’s in your interest that using a well known 3rd party cryptographic provides still enables your customers to use your product. A simple workaround would be just calling setLicense of the PDF module twice.

Kind regards,
Tim

@tim.weller

Please specify the version you are working with, in your environment. There are different versions with up-to three levels hierarchy as in attached screenshot IAIK_SS.jpg We will then be able to proceed and sort it out.

We’re using IAIK JCE 5.51. I planned to check if the issue appears with version 5.52 (latest) as well.

@Farhan.Raza Ok, a colleague of mine tested the latest version as well, so both 5.51 and 5.52 of IAIK JCE show the same behaviour.

Kind regards,
Tim

@tim.weller

Thank you for further details.

We have managed to run the code snippet shared by you. However, we are noticing NullPointerException on each of five iterations when license is set. Whereas, this exception disappears if we pass the path to license file as under, and license is being set successfully on each iteration.

new com.aspose.pdf.License().setLicense("D:\\Test\\Aspose.Total.Java.lic");

Can you please create a sample console application using the evaluation JAR and share it with us so that we may proceed to help you out.

@Farhan.Raza
I will send you the jar files (without Aspose.PDF 18.9) in a private message.

You need the following in a folder:

  • aspose-license-test.jar (PM)
  • iaik_jce_full_v5.51_eval.jar (PM)
  • aspose-pdf-18.9.jar (not via PM; you should have that already)

And then start it with:

java -jar aspose-license-test.jar

Example output:

C:\develop\workspaces\IntelliJ\aspose-license-test\out\artifacts\aspose_license_test_jar>java -jar aspose-license-test.jar



*** Welcome to the IAIK JCE Library ***


*** This version of IAIK-JCE is licensed for evaluation, education, ***
*** research, and use in open-source projects only. ***
*** Commercial use of this software is prohibited. ***
*** For details please see http://jce.iaik.tugraz.at/sales/. ***
*** This message does not appear in the registered commercial version. ***



attempt: 1
SignatureException occurred!
java.security.SignatureException: Signature decryption error: javax.crypto.BadPaddingException: Invalid padding!
at iaik.security.rsa.RSASignature.engineVerify(Unknown Source)
at java.security.Signature$Delegate.engineVerify(Unknown Source)
at java.security.Signature.verify(Unknown Source)
at com.aspose.pdf.I23I.lif(Unknown Source)
at com.aspose.pdf.I23I.l0I(Unknown Source)
at com.aspose.pdf.I23I.lif(Unknown Source)
at com.aspose.pdf.License.setLicense(Unknown Source)
at com.tsystems.ima.asposelicensetest.Main.setLicense(Main.java:23)
at com.tsystems.ima.asposelicensetest.Main.main(Main.java:16)
attempt finished: 1
attempt: 2
attempt finished: 2
attempt: 3
attempt finished: 3
attempt: 4
attempt finished: 4
attempt: 5
attempt finished: 5

Here is the source code again:

package com.tsystems.ima.asposelicensetest;

import iaik.security.provider.IAIK;

import java.io.InputStream;
import java.security.SignatureException;

public class Main {
    private static final String ASPOSE_LICENSE = "/Aspose.Total.Java.lic";

    public static void main(String[] args) throws Exception {
        IAIK.addAsProvider();

        int attempts = 5;
        for (int i = 0; i < attempts; i++) {
            setLicense(i);
        }
    }

    private static void setLicense(int i) throws Exception {
        System.out.println("attempt: " + (i+1));
        try (InputStream in = Main.class.getResourceAsStream(ASPOSE_LICENSE)) {
            new com.aspose.pdf.License().setLicense(in);
        } catch (SignatureException e) {
            System.out.println("SignatureException occurred!");
            e.printStackTrace(System.out);
        }
        System.out.println("attempt finished: " + (i+1));
    }
  }

@tim.weller

We have worked with the data shared by you and have been able to reproduce the issue in our environment. A ticket with ID PDFJAVA-38107 has been logged in our issue management system for further investigation and resolution. The ticket ID has been linked with this thread so that you will receive notification as soon as the ticket is resolved.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFJAVA-38107) have been fixed in Aspose.PDF for Java 19.6.

@Farhan.Raza The exact same bug came back with Aspose PDF 20.3. Is it possible to reopen/create a new ticket?

@tim.weller

We have logged another regression issue as PDFJAVA-39284 in our issue tracking system for this. We will surely inform you as soon as we have some definite updates regarding its resolution. Please spare us some time.

We are sorry for the inconvenience.

@tim.weller

Regarding PDFJAVA-39284, Internal security FIPS provider for Aspose.PDF license check should be enabled manually. In most of cases should be used as default security provider.
Use the following code before licence initialization:

com.aspose.pdf.License.setInternalFIPSSecurity(true);