|
| 1 | +package com.itextpdf.signatures.sign; |
| 2 | + |
| 3 | +import com.itextpdf.kernel.font.PdfFont; |
| 4 | +import com.itextpdf.kernel.font.PdfFontFactory; |
| 5 | +import com.itextpdf.kernel.geom.Rectangle; |
| 6 | +import com.itextpdf.kernel.pdf.PdfReader; |
| 7 | +import com.itextpdf.kernel.utils.CompareTool; |
| 8 | +import com.itextpdf.signatures.BouncyCastleDigest; |
| 9 | +import com.itextpdf.signatures.DigestAlgorithms; |
| 10 | +import com.itextpdf.signatures.IExternalSignature; |
| 11 | +import com.itextpdf.signatures.PdfSignatureAppearance; |
| 12 | +import com.itextpdf.signatures.PdfSigner; |
| 13 | +import com.itextpdf.signatures.PrivateKeySignature; |
| 14 | +import com.itextpdf.signatures.testutils.Pkcs12FileHelper; |
| 15 | +import com.itextpdf.test.ExtendedITextTest; |
| 16 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 17 | +import org.junit.Assert; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Rule; |
| 21 | +import org.junit.Test; |
| 22 | +import org.junit.rules.ExpectedException; |
| 23 | + |
| 24 | +import java.io.FileOutputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.security.GeneralSecurityException; |
| 27 | +import java.security.KeyStoreException; |
| 28 | +import java.security.NoSuchAlgorithmException; |
| 29 | +import java.security.PrivateKey; |
| 30 | +import java.security.Security; |
| 31 | +import java.security.UnrecoverableKeyException; |
| 32 | +import java.security.cert.Certificate; |
| 33 | +import java.security.cert.CertificateException; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +public class PdfASigningTest extends ExtendedITextTest { |
| 40 | + |
| 41 | + public static final String sourceFolder = "./src/test/resources/com/itextpdf/signatures/sign/PdfASigningTest/"; |
| 42 | + public static final String destinationFolder = "./target/test/com/itextpdf/signatures/sign/PdfASigningTest/"; |
| 43 | + public static final String keystorePath = "./src/test/resources/com/itextpdf/signatures/certs/signCertRsa01.p12"; |
| 44 | + public static final char[] password = "testpass".toCharArray(); |
| 45 | + public static final String FONT = "./src/test/resources/com/itextpdf/signatures/font/FreeSans.ttf"; |
| 46 | + |
| 47 | + private Certificate[] chain; |
| 48 | + private PrivateKey pk; |
| 49 | + |
| 50 | + @Rule |
| 51 | + public ExpectedException junitExpectedException = ExpectedException.none(); |
| 52 | + |
| 53 | + @BeforeClass |
| 54 | + public static void before() { |
| 55 | + Security.addProvider(new BouncyCastleProvider()); |
| 56 | + createOrClearDestinationFolder(destinationFolder); |
| 57 | + } |
| 58 | + |
| 59 | + @Before |
| 60 | + public void init() throws KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException { |
| 61 | + pk = Pkcs12FileHelper.readFirstKey(keystorePath, password, password); |
| 62 | + chain = Pkcs12FileHelper.readFirstChain(keystorePath, password); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void simpleSigningTest() throws GeneralSecurityException, IOException, InterruptedException { |
| 67 | + String src = sourceFolder + "simplePdfADocument.pdf"; |
| 68 | + String fileName = "simpleSignature.pdf"; |
| 69 | + String dest = destinationFolder + fileName; |
| 70 | + |
| 71 | + int x = 36; |
| 72 | + int y = 548; |
| 73 | + int w = 200; |
| 74 | + int h = 100; |
| 75 | + Rectangle rect = new Rectangle(x, y, w, h); |
| 76 | + |
| 77 | + String fieldName = "Signature1"; |
| 78 | + sign(src, fieldName, dest, chain, pk, |
| 79 | + DigestAlgorithms.SHA256, PdfSigner.CryptoStandard.CADES, "Test 1", "TestCity", rect, false, false, PdfSigner.NOT_CERTIFIED, 12f); |
| 80 | + |
| 81 | + Assert.assertNull(new CompareTool().compareVisually(dest, sourceFolder + "cmp_" + fileName, destinationFolder, |
| 82 | + "diff_", getTestMap(new Rectangle(67, 575, 155, 15)))); |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + protected void sign(String src, String name, String dest, |
| 87 | + Certificate[] chain, PrivateKey pk, |
| 88 | + String digestAlgorithm, PdfSigner.CryptoStandard subfilter, |
| 89 | + String reason, String location, Rectangle rectangleForNewField, boolean setReuseAppearance, boolean isAppendMode) throws GeneralSecurityException, IOException { |
| 90 | + sign(src, name, dest, chain, pk, digestAlgorithm, subfilter, reason, location, rectangleForNewField, setReuseAppearance, isAppendMode, PdfSigner.NOT_CERTIFIED, null); |
| 91 | + } |
| 92 | + |
| 93 | + protected void sign(String src, String name, String dest, |
| 94 | + Certificate[] chain, PrivateKey pk, |
| 95 | + String digestAlgorithm, PdfSigner.CryptoStandard subfilter, |
| 96 | + String reason, String location, Rectangle rectangleForNewField, boolean setReuseAppearance, boolean isAppendMode, int certificationLevel, Float fontSize) |
| 97 | + throws GeneralSecurityException, IOException { |
| 98 | + |
| 99 | + PdfReader reader = new PdfReader(src); |
| 100 | + PdfSigner signer = new PdfSigner(reader, new FileOutputStream(dest), isAppendMode); |
| 101 | + |
| 102 | + signer.setCertificationLevel(certificationLevel); |
| 103 | + |
| 104 | + PdfFont font = PdfFontFactory.createFont(FONT, "WinAnsi", true); |
| 105 | + |
| 106 | + // Creating the appearance |
| 107 | + PdfSignatureAppearance appearance = signer.getSignatureAppearance() |
| 108 | + .setReason(reason) |
| 109 | + .setLocation(location) |
| 110 | + .setLayer2Font(font) |
| 111 | + .setReuseAppearance(setReuseAppearance); |
| 112 | + |
| 113 | + if (rectangleForNewField != null) { |
| 114 | + appearance.setPageRect(rectangleForNewField); |
| 115 | + } |
| 116 | + if (fontSize != null) { |
| 117 | + appearance.setLayer2FontSize((float) fontSize); |
| 118 | + } |
| 119 | + |
| 120 | + signer.setFieldName(name); |
| 121 | + // Creating the signature |
| 122 | + IExternalSignature pks = new PrivateKeySignature(pk, digestAlgorithm, BouncyCastleProvider.PROVIDER_NAME); |
| 123 | + signer.signDetached(new BouncyCastleDigest(), pks, chain, null, null, null, 0, subfilter); |
| 124 | + } |
| 125 | + |
| 126 | + private static Map<Integer, List<Rectangle>> getTestMap(Rectangle ignoredArea) { |
| 127 | + Map<Integer, List<Rectangle>> result = new HashMap<Integer, List<Rectangle>>(); |
| 128 | + result.put(1, Arrays.asList(ignoredArea)); |
| 129 | + return result; |
| 130 | + } |
| 131 | + |
| 132 | +} |
0 commit comments