|
| 1 | +package com.itextpdf.signatures.verify.pdfinsecurity; |
| 2 | + |
| 3 | +import com.itextpdf.io.LogMessageConstant; |
| 4 | +import com.itextpdf.kernel.pdf.PdfDocument; |
| 5 | +import com.itextpdf.kernel.pdf.PdfReader; |
| 6 | +import com.itextpdf.kernel.pdf.canvas.parser.PdfTextExtractor; |
| 7 | +import com.itextpdf.signatures.PdfPKCS7; |
| 8 | +import com.itextpdf.signatures.SignatureUtil; |
| 9 | +import com.itextpdf.signatures.testutils.SignTestPortUtil; |
| 10 | +import com.itextpdf.test.ExtendedITextTest; |
| 11 | +import com.itextpdf.test.annotations.LogMessage; |
| 12 | +import com.itextpdf.test.annotations.LogMessages; |
| 13 | +import com.itextpdf.test.annotations.type.IntegrationTest; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStream; |
| 16 | +import java.security.GeneralSecurityException; |
| 17 | +import java.security.Security; |
| 18 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.BeforeClass; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.experimental.categories.Category; |
| 23 | + |
| 24 | +@Category(IntegrationTest.class) |
| 25 | +public class IncrementalSavingAttackTest extends ExtendedITextTest { |
| 26 | + private static final String sourceFolder = "./src/test/resources/com/itextpdf/signatures/verify/pdfinsecurity/IncrementalSavingAttackTest/"; |
| 27 | + |
| 28 | + @BeforeClass |
| 29 | + public static void before() { |
| 30 | + Security.addProvider(new BouncyCastleProvider()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + @LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.XREF_ERROR)) |
| 35 | + public void testISA03() throws IOException, GeneralSecurityException { |
| 36 | + String filePath = sourceFolder + "isa-3.pdf"; |
| 37 | + String signatureName = "Signature1"; |
| 38 | + |
| 39 | + PdfDocument document = new PdfDocument(new PdfReader(filePath)); |
| 40 | + SignatureUtil sigUtil = new SignatureUtil(document); |
| 41 | + PdfPKCS7 pdfPKCS7 = sigUtil.verifySignature(signatureName); |
| 42 | + Assert.assertTrue(pdfPKCS7.verify()); |
| 43 | + Assert.assertFalse(sigUtil.signatureCoversWholeDocument(signatureName)); |
| 44 | + document.close(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testISAValidPdf() throws IOException, GeneralSecurityException { |
| 49 | + String filePath = sourceFolder + "isaValidPdf.pdf"; |
| 50 | + String signatureName = "Signature1"; |
| 51 | + |
| 52 | + PdfDocument document = new PdfDocument(new PdfReader(filePath)); |
| 53 | + SignatureUtil sigUtil = new SignatureUtil(document); |
| 54 | + PdfPKCS7 pdfPKCS7 = sigUtil.verifySignature(signatureName); |
| 55 | + Assert.assertTrue(pdfPKCS7.verify()); |
| 56 | + Assert.assertFalse(sigUtil.signatureCoversWholeDocument(signatureName)); |
| 57 | + |
| 58 | + String textFromPage = PdfTextExtractor.getTextFromPage(document.getPage(1)); |
| 59 | + // We are working with the latest revision of the document, that's why we should get amended page text. |
| 60 | + // However Signature shall be marked as not covering the complete document, indicating its invalidity |
| 61 | + // for the current revision. |
| 62 | + Assert.assertEquals("This is manipulated malicious text, ha-ha!", textFromPage); |
| 63 | + |
| 64 | + Assert.assertEquals(2, sigUtil.getTotalRevisions()); |
| 65 | + Assert.assertEquals(1, sigUtil.getRevision(signatureName)); |
| 66 | + |
| 67 | + InputStream sigInputStream = sigUtil.extractRevision(signatureName); |
| 68 | + PdfDocument sigRevDocument = new PdfDocument(new PdfReader(sigInputStream)); |
| 69 | + |
| 70 | + SignatureUtil sigRevUtil = new SignatureUtil(sigRevDocument); |
| 71 | + PdfPKCS7 sigRevSignatureData = sigRevUtil.verifySignature(signatureName); |
| 72 | + Assert.assertTrue(sigRevSignatureData.verify()); |
| 73 | + Assert.assertTrue(sigRevUtil.signatureCoversWholeDocument(signatureName)); |
| 74 | + |
| 75 | + sigRevDocument.close(); |
| 76 | + document.close(); |
| 77 | + } |
| 78 | +} |
0 commit comments