Skip to content

Commit b5e4be0

Browse files
committed
Check for supported feature if decryption has failed
Also fix reference files in encryption tests, add certificate file in a format to be imported in Acrobat. DEVSIX-7519
1 parent e2f29e5 commit b5e4be0

20 files changed

+32
-21
lines changed

kernel/src/main/java/com/itextpdf/kernel/crypto/securityhandler/EncryptionUtils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ static byte[] fetchEnvelopedData(Key certificateKey, Certificate certificate, St
126126
}
127127
}
128128
} catch (Exception f) {
129+
// First check if the feature is supported, it will throw if not
130+
// Exact algorithm doesn't matter currently
131+
BouncyCastleFactoryCreator.getFactory().isEncryptionFeatureSupported(0, true);
132+
// Throw the original exception if the feature is supported
129133
throw new PdfException(KernelExceptionMessageConstant.PDF_DECRYPTION, f);
130134
}
131135
}

kernel/src/test/java/com/itextpdf/kernel/crypto/PdfEncryptionManuallyPortedTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ This file is part of the iText (R) project.
2727
import com.itextpdf.commons.bouncycastle.operator.AbstractOperatorCreationException;
2828
import com.itextpdf.commons.bouncycastle.pkcs.AbstractPKCSException;
2929
import com.itextpdf.io.font.constants.StandardFonts;
30+
import com.itextpdf.kernel.exceptions.KernelExceptionMessageConstant;
31+
import com.itextpdf.kernel.exceptions.PdfException;
3032
import com.itextpdf.kernel.font.PdfFontFactory;
3133
import com.itextpdf.kernel.logs.KernelLogMessageConstant;
3234
import com.itextpdf.kernel.pdf.CompressionConstants;
@@ -82,6 +84,8 @@ public class PdfEncryptionManuallyPortedTest extends ExtendedITextTest {
8284
public static final String sourceFolder = "./src/test/resources/com/itextpdf/kernel/crypto/PdfEncryptionManuallyPortedTest/";
8385

8486
public static final char[] PRIVATE_KEY_PASS = "testpassphrase".toCharArray();
87+
// There is also test.pfx to add to Acrobat to be able to open result pdf files. Password for it is also
88+
// testpassphrase
8589
public static final String CERT = sourceFolder + "test.cer";
8690
public static final String PRIVATE_KEY = sourceFolder + "test.pem";
8791

@@ -180,6 +184,27 @@ public void encryptWithCertificateAes256NoCompression() throws IOException, Inte
180184
encryptWithCertificate(filename, encryptionType, CompressionConstants.NO_COMPRESSION);
181185
}
182186

187+
@Test
188+
@LogMessages(messages = @LogMessage(messageTemplate = KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT,
189+
ignore = true))
190+
public void openEncryptedDocWithWrongPrivateKey()
191+
throws IOException, GeneralSecurityException, AbstractPKCSException, AbstractOperatorCreationException {
192+
try (PdfReader reader = new PdfReader(sourceFolder + "encryptedWithCertificateAes128.pdf",
193+
new ReaderProperties()
194+
.setPublicKeySecurityParams(
195+
getPublicCertificate(CERT),
196+
PemFileHelper.readPrivateKeyFromPemFile(
197+
new FileInputStream(sourceFolder + "wrong.pem"), PRIVATE_KEY_PASS),
198+
FACTORY.getProviderName(),
199+
null))) {
200+
201+
Exception e = Assert.assertThrows(PdfException.class,
202+
() -> new PdfDocument(reader)
203+
);
204+
Assert.assertEquals(KernelExceptionMessageConstant.PDF_DECRYPTION, e.getMessage());
205+
}
206+
}
207+
183208
public void encryptWithCertificate(String filename, int encryptionType, int compression) throws IOException,
184209
InterruptedException, GeneralSecurityException, AbstractPKCSException, AbstractOperatorCreationException {
185210
ITextTest.removeCryptographyRestrictions();

kernel/src/test/java/com/itextpdf/kernel/crypto/PdfEncryptionTest.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -269,27 +269,6 @@ public void openEncryptedDocWithWrongCertificate()
269269
}
270270
}
271271

272-
@Test
273-
@LogMessages(messages = @LogMessage(messageTemplate = KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT,
274-
ignore = true))
275-
public void openEncryptedDocWithWrongPrivateKey()
276-
throws IOException, GeneralSecurityException, AbstractPKCSException, AbstractOperatorCreationException {
277-
try (PdfReader reader = new PdfReader(sourceFolder + "encryptedWithCertificateAes128.pdf",
278-
new ReaderProperties()
279-
.setPublicKeySecurityParams(
280-
getPublicCertificate(CERT),
281-
PemFileHelper.readPrivateKeyFromPemFile(
282-
new FileInputStream(sourceFolder + "wrong.pem"), PRIVATE_KEY_PASS),
283-
FACTORY.getProviderName(),
284-
null))) {
285-
286-
Exception e = Assert.assertThrows(PdfException.class,
287-
() -> new PdfDocument(reader)
288-
);
289-
Assert.assertEquals(KernelExceptionMessageConstant.PDF_DECRYPTION, e.getMessage());
290-
}
291-
}
292-
293272
@Test
294273
@LogMessages(messages = @LogMessage(messageTemplate = KernelLogMessageConstant.MD5_IS_NOT_FIPS_COMPLIANT,
295274
ignore = true))
@@ -330,6 +309,9 @@ public void metadataReadingInEncryptedDoc() throws IOException, XMPException {
330309
ignore = true))
331310
public void copyEncryptedDocument() throws GeneralSecurityException, IOException, InterruptedException,
332311
AbstractPKCSException, AbstractOperatorCreationException {
312+
// I don't know how this source doc was created. Currently it's not opening by Acrobat and Foxit.
313+
// If I recreate it using iText, decrypting it in bc-fips on dotnet will start failing. But we probably still
314+
// want this test.
333315
PdfDocument srcDoc = new PdfDocument(new PdfReader(sourceFolder + "encryptedWithCertificateAes128.pdf",
334316
new ReaderProperties().
335317
setPublicKeySecurityParams(getPublicCertificate(CERT), getPrivateKey(),

0 commit comments

Comments
 (0)