Skip to content

Commit d3e134b

Browse files
author
Dmitry Radchuk
committed
Add cades verification for pdfa documents
DEVSIX-7745
1 parent b80b691 commit d3e134b

File tree

13 files changed

+124
-1
lines changed

13 files changed

+124
-1
lines changed

kernel/src/main/java/com/itextpdf/kernel/pdf/IsoKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ public enum IsoKey {
3838
FONT_GLYPHS,
3939
XREF_TABLE,
4040
SIGNATURE,
41+
SIGNATURE_TYPE,
4142
CRYPTO
4243
}

kernel/src/main/java/com/itextpdf/kernel/utils/PdfMergerProperties.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ public PdfMergerProperties setMergeOutlines(boolean mergeOutlines) {
115115
}
116116

117117
/**
118-
* merge documents ECMA scripts
118+
* merge documents ECMA scripts,
119+
* if AA or OpenAction or Names dictionaries contained in both documents than nothing will be merged
119120
*
120121
* @param mergeNames true to merge, false otherwise
121122
*

pdfa/src/main/java/com/itextpdf/pdfa/PdfADocument.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ public void checkIsoConformance(Object obj, IsoKey key, PdfResources resources,
211211
case SIGNATURE:
212212
checker.checkSignature((PdfDictionary) obj);
213213
break;
214+
case SIGNATURE_TYPE:
215+
checker.checkSignatureType(((Boolean) obj).booleanValue());
216+
break;
214217
case CRYPTO:
215218
checker.checkCrypto((PdfObject) obj);
216219
break;

pdfa/src/main/java/com/itextpdf/pdfa/checker/PdfA1Checker.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ public void checkCrypto(PdfObject crypto) {
320320
}
321321
}
322322

323+
/**
324+
* {@inheritDoc}
325+
*/
326+
@Override
327+
public void checkSignatureType(boolean isCAdES) {
328+
//nothing to do
329+
}
330+
323331
@Override
324332
protected void checkPageTransparency(PdfDictionary pageDict, PdfDictionary pageResources) {
325333
// This check is irrelevant for the PdfA1 checker, so the body of the method is empty

pdfa/src/main/java/com/itextpdf/pdfa/checker/PdfA4Checker.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,16 @@ public void checkCanvasStack(char stackOperation) {
293293

294294
}
295295

296+
/**
297+
* {@inheritDoc}
298+
*/
299+
@Override
300+
public void checkSignatureType(boolean isCAdES) {
301+
if (!isCAdES) {
302+
throw new PdfAConformanceException(PdfaExceptionMessageConstant.SIGNATURE_SHALL_CONFORM_TO_ONE_OF_THE_PADES_PROFILE);
303+
}
304+
}
305+
296306
//There is no limit for String length in pdf-a/4
297307
/**
298308
* {@inheritDoc}

pdfa/src/main/java/com/itextpdf/pdfa/checker/PdfAChecker.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,18 @@ public void checkSignature(PdfDictionary signatureDict) {
273273
checkedObjects.add(signatureDict);
274274
}
275275

276+
/**
277+
* This method checks compliance of the signature type
278+
*
279+
* @param isCAdES true is CAdES sig type is used, false otherwise.
280+
*
281+
* @deprecated Will become an abstract in the next major release.
282+
*/
283+
@Deprecated
284+
public void checkSignatureType(boolean isCAdES) {
285+
286+
}
287+
276288
/**
277289
* This method checks compliance with the graphics state architectural
278290
* limitation, explained by {@link PdfAChecker#maxGsStackDepth}.

pdfa/src/main/java/com/itextpdf/pdfa/exceptions/PdfaExceptionMessageConstant.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,8 @@ public final class PdfaExceptionMessageConstant {
180180
public static final String XMP_METADATA_HISTORY_ENTRY_SHALL_CONTAIN_KEY = "XMP metadata history entry shall"
181181
+ " contain key {0}";
182182

183+
public static final String SIGNATURE_SHALL_CONFORM_TO_ONE_OF_THE_PADES_PROFILE =
184+
"Signature shall conform to one of the PAdES profiles from either ISO 32000-2 or ISO 14533-3.";
185+
183186
private PdfaExceptionMessageConstant(){}
184187
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.itextpdf.pdfa;
2+
3+
import com.itextpdf.kernel.pdf.IsoKey;
4+
import com.itextpdf.kernel.pdf.PdfAConformanceLevel;
5+
import com.itextpdf.kernel.pdf.PdfOutputIntent;
6+
import com.itextpdf.kernel.pdf.PdfVersion;
7+
import com.itextpdf.kernel.pdf.PdfWriter;
8+
import com.itextpdf.kernel.pdf.WriterProperties;
9+
import com.itextpdf.pdfa.exceptions.PdfAConformanceException;
10+
import com.itextpdf.pdfa.exceptions.PdfaExceptionMessageConstant;
11+
import com.itextpdf.test.ExtendedITextTest;
12+
import com.itextpdf.test.annotations.type.UnitTest;
13+
import org.junit.Assert;
14+
import org.junit.Test;
15+
import org.junit.experimental.categories.Category;
16+
17+
import java.io.ByteArrayOutputStream;
18+
import java.io.FileInputStream;
19+
import java.io.FileNotFoundException;
20+
import java.io.InputStream;
21+
22+
@Category(UnitTest.class)
23+
public class PdfADocumentTest extends ExtendedITextTest {
24+
public static final String SOURCE_FOLDER = "./src/test/resources/com/itextpdf/pdfa/";
25+
26+
@Test
27+
public void checkCadesSignatureTypeIsoConformance() throws FileNotFoundException {
28+
PdfWriter writer = new PdfWriter(new ByteArrayOutputStream(), new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0));
29+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
30+
PdfADocument document = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is));
31+
document.checkIsoConformance(true, IsoKey.SIGNATURE_TYPE, null, null);
32+
}
33+
34+
@Test
35+
public void checkCMSSignatureTypeIsoConformance() throws FileNotFoundException {
36+
PdfWriter writer = new PdfWriter(new ByteArrayOutputStream(), new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0));
37+
InputStream is = new FileInputStream(SOURCE_FOLDER + "sRGB Color Space Profile.icm");
38+
PdfADocument document = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_4, new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is));
39+
Exception e = Assert.assertThrows(PdfAConformanceException.class, () -> document.checkIsoConformance(false, IsoKey.SIGNATURE_TYPE, null, null));
40+
Assert.assertEquals(PdfaExceptionMessageConstant.SIGNATURE_SHALL_CONFORM_TO_ONE_OF_THE_PADES_PROFILE, e.getMessage());
41+
}
42+
}

pdfa/src/test/java/com/itextpdf/pdfa/checker/PdfA1CheckerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public void checkSignatureTest() {
103103
Assert.assertTrue(pdfA1Checker.objectIsChecked(dict));
104104
}
105105

106+
@Test
107+
public void checkSignatureTypeTest() {
108+
pdfA1Checker.checkSignatureType(true);
109+
//nothing to check, only for coverage
110+
}
111+
106112
@Test
107113
public void checkLZWDecodeInInlineImage() {
108114
PdfStream stream = new PdfStream();

sign/src/main/java/com/itextpdf/signatures/PdfSigner.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,8 @@ public void signDetached(IExternalDigest externalDigest, IExternalSignature exte
556556
}
557557
}
558558

559+
document.checkIsoConformance(sigtype == CryptoStandard.CADES, IsoKey.SIGNATURE_TYPE);
560+
559561
Collection<byte[]> crlBytes = null;
560562
int i = 0;
561563
while (crlBytes == null && i < chain.length) {

0 commit comments

Comments
 (0)