@@ -26,6 +26,7 @@ This file is part of the iText (R) project.
26
26
import com .itextpdf .commons .bouncycastle .IBouncyCastleFactory ;
27
27
import com .itextpdf .commons .bouncycastle .operator .AbstractOperatorCreationException ;
28
28
import com .itextpdf .commons .bouncycastle .pkcs .AbstractPKCSException ;
29
+ import com .itextpdf .commons .utils .FileUtil ;
29
30
import com .itextpdf .kernel .geom .Rectangle ;
30
31
import com .itextpdf .kernel .pdf .PdfReader ;
31
32
import com .itextpdf .kernel .pdf .StampingProperties ;
@@ -42,9 +43,7 @@ This file is part of the iText (R) project.
42
43
import com .itextpdf .signatures .testutils .client .TestTsaClient ;
43
44
import com .itextpdf .test .ExtendedITextTest ;
44
45
import com .itextpdf .test .annotations .type .BouncyCastleIntegrationTest ;
45
- import com .itextpdf .test .annotations .type .IntegrationTest ;
46
46
47
- import java .io .FileOutputStream ;
48
47
import java .io .IOException ;
49
48
import java .security .GeneralSecurityException ;
50
49
import java .security .PrivateKey ;
@@ -65,6 +64,8 @@ public class PdfPadesSignerLevelsTest extends ExtendedITextTest {
65
64
66
65
private static final IBouncyCastleFactory FACTORY = BouncyCastleFactoryCreator .getFactory ();
67
66
67
+ private static final boolean FIPS_MODE = "BCFIPS" .equals (FACTORY .getProviderName ());
68
+
68
69
private static final String certsSrc = "./src/test/resources/com/itextpdf/signatures/certs/" ;
69
70
private static final String sourceFolder = "./src/test/resources/com/itextpdf/signatures/sign/PdfPadesSignerLevelsTest/" ;
70
71
private static final String destinationFolder = "./target/test/com/itextpdf/signatures/sign/PdfPadesSignerLevelsTest/" ;
@@ -87,7 +88,7 @@ public PdfPadesSignerLevelsTest(Object useTempFolder, Object useSignature, Objec
87
88
this .comparisonPdfId = (Integer ) comparisonPdfId ;
88
89
}
89
90
90
- @ Parameterized .Parameters (name = "{index }: folder path: {0}; pass whole signature: {1}" )
91
+ @ Parameterized .Parameters (name = "{2 }: folder path: {0}; pass whole signature: {1}" )
91
92
public static Iterable <Object []> createParameters () {
92
93
return Arrays .asList (new Object [] {true , true , 1 },
93
94
new Object [] {false , true , 2 },
@@ -105,16 +106,20 @@ public void padesSignatureLevelBTest()
105
106
106
107
Certificate [] signRsaChain = PemFileHelper .readFirstChain (signCertFileName );
107
108
PrivateKey signRsaPrivateKey = PemFileHelper .readFirstKey (signCertFileName , password );
108
- IExternalSignature pks =
109
- new PrivateKeySignature (signRsaPrivateKey , DigestAlgorithms .SHA256 , FACTORY .getProviderName ());
110
109
111
110
PdfSigner signer = createPdfSigner (srcFileName , outFileName );
112
111
113
- PdfPadesSigner padesSigner = createPdfPadesSigner ( signer , pks , signRsaPrivateKey );
112
+ PdfPadesSigner padesSigner = new PdfPadesSigner ( );
114
113
if ((boolean ) useTempFolder ) {
115
114
padesSigner .setTemporaryDirectoryPath (destinationFolder );
116
115
}
117
- padesSigner .signWithBaselineBProfile (signRsaChain );
116
+ if ((boolean ) useSignature ) {
117
+ IExternalSignature pks =
118
+ new PrivateKeySignature (signRsaPrivateKey , DigestAlgorithms .SHA256 , FACTORY .getProviderName ());
119
+ padesSigner .signWithBaselineBProfile (signer , signRsaChain , pks );
120
+ } else {
121
+ padesSigner .signWithBaselineBProfile (signer , signRsaChain , signRsaPrivateKey );
122
+ }
118
123
119
124
PadesSigTest .basicCheckSignedDoc (outFileName , "Signature1" );
120
125
@@ -133,21 +138,24 @@ public void padesSignatureLevelTTest()
133
138
134
139
Certificate [] signRsaChain = PemFileHelper .readFirstChain (signCertFileName );
135
140
PrivateKey signRsaPrivateKey = PemFileHelper .readFirstKey (signCertFileName , password );
136
- IExternalSignature pks =
137
- new PrivateKeySignature (signRsaPrivateKey , DigestAlgorithms .SHA256 , FACTORY .getProviderName ());
138
141
Certificate [] tsaChain = PemFileHelper .readFirstChain (tsaCertFileName );
139
142
PrivateKey tsaPrivateKey = PemFileHelper .readFirstKey (tsaCertFileName , password );
140
143
141
144
PdfSigner signer = createPdfSigner (srcFileName , outFileName );
142
145
143
146
TestTsaClient testTsa = new TestTsaClient (Arrays .asList (tsaChain ), tsaPrivateKey );
144
147
145
- PdfPadesSigner padesSigner = createPdfPadesSigner ( signer , pks , signRsaPrivateKey );
148
+ PdfPadesSigner padesSigner = new PdfPadesSigner ( );
146
149
if ((boolean ) useTempFolder ) {
147
150
padesSigner .setTemporaryDirectoryPath (destinationFolder );
148
151
}
149
- padesSigner .setTsaClient (testTsa );
150
- padesSigner .signWithBaselineTProfile (signRsaChain );
152
+ if ((boolean ) useSignature ) {
153
+ IExternalSignature pks =
154
+ new PrivateKeySignature (signRsaPrivateKey , DigestAlgorithms .SHA256 , FACTORY .getProviderName ());
155
+ padesSigner .signWithBaselineTProfile (signer , signRsaChain , pks , testTsa );
156
+ } else {
157
+ padesSigner .signWithBaselineTProfile (signer , signRsaChain , signRsaPrivateKey , testTsa );
158
+ }
151
159
152
160
PadesSigTest .basicCheckSignedDoc (outFileName , "Signature1" );
153
161
@@ -167,8 +175,6 @@ public void padesSignatureLevelLTTest()
167
175
168
176
Certificate [] signRsaChain = PemFileHelper .readFirstChain (signCertFileName );
169
177
PrivateKey signRsaPrivateKey = PemFileHelper .readFirstKey (signCertFileName , password );
170
- IExternalSignature pks =
171
- new PrivateKeySignature (signRsaPrivateKey , DigestAlgorithms .SHA256 , FACTORY .getProviderName ());
172
178
Certificate [] tsaChain = PemFileHelper .readFirstChain (tsaCertFileName );
173
179
PrivateKey tsaPrivateKey = PemFileHelper .readFirstKey (tsaCertFileName , password );
174
180
X509Certificate caCert = (X509Certificate ) PemFileHelper .readFirstChain (caCertFileName )[0 ];
@@ -180,12 +186,18 @@ public void padesSignatureLevelLTTest()
180
186
ICrlClient crlClient = new TestCrlClient ().addBuilderForCertIssuer (caCert , caPrivateKey );
181
187
TestOcspClient ocspClient = new TestOcspClient ().addBuilderForCertIssuer (caCert , caPrivateKey );
182
188
183
- PdfPadesSigner padesSigner = createPdfPadesSigner ( signer , pks , signRsaPrivateKey );
189
+ PdfPadesSigner padesSigner = new PdfPadesSigner ( );
184
190
if ((boolean ) useTempFolder ) {
185
191
padesSigner .setTemporaryDirectoryPath (destinationFolder );
186
192
}
187
- padesSigner .setTsaClient (testTsa ).setOcspClient (ocspClient ).setCrlClient (crlClient );
188
- padesSigner .signWithBaselineLTProfile (signRsaChain );
193
+ padesSigner .setOcspClient (ocspClient ).setCrlClient (crlClient );
194
+ if ((boolean ) useSignature ) {
195
+ IExternalSignature pks =
196
+ new PrivateKeySignature (signRsaPrivateKey , DigestAlgorithms .SHA256 , FACTORY .getProviderName ());
197
+ padesSigner .signWithBaselineLTProfile (signer , signRsaChain , pks , testTsa );
198
+ } else {
199
+ padesSigner .signWithBaselineLTProfile (signer , signRsaChain , signRsaPrivateKey , testTsa );
200
+ }
189
201
190
202
PadesSigTest .basicCheckSignedDoc (outFileName , "Signature1" );
191
203
@@ -205,8 +217,6 @@ public void padesSignatureLevelLTATest()
205
217
206
218
Certificate [] signRsaChain = PemFileHelper .readFirstChain (signCertFileName );
207
219
PrivateKey signRsaPrivateKey = PemFileHelper .readFirstKey (signCertFileName , password );
208
- IExternalSignature pks =
209
- new PrivateKeySignature (signRsaPrivateKey , DigestAlgorithms .SHA256 , FACTORY .getProviderName ());
210
220
Certificate [] tsaChain = PemFileHelper .readFirstChain (tsaCertFileName );
211
221
PrivateKey tsaPrivateKey = PemFileHelper .readFirstKey (tsaCertFileName , password );
212
222
X509Certificate caCert = (X509Certificate ) PemFileHelper .readFirstChain (caCertFileName )[0 ];
@@ -218,29 +228,64 @@ public void padesSignatureLevelLTATest()
218
228
ICrlClient crlClient = new TestCrlClient ().addBuilderForCertIssuer (caCert , caPrivateKey );
219
229
TestOcspClient ocspClient = new TestOcspClient ().addBuilderForCertIssuer (caCert , caPrivateKey );
220
230
221
- PdfPadesSigner padesSigner = createPdfPadesSigner ( signer , pks , signRsaPrivateKey );
231
+ PdfPadesSigner padesSigner = new PdfPadesSigner ( );
222
232
if ((boolean ) useTempFolder ) {
223
233
padesSigner .setTemporaryDirectoryPath (destinationFolder );
224
234
}
225
- padesSigner .setTsaClient ( testTsa ). setOcspClient (ocspClient ).setCrlClient (crlClient )
235
+ padesSigner .setOcspClient (ocspClient ).setCrlClient (crlClient )
226
236
.setTimestampSignatureName ("timestampSig1" );
227
- padesSigner .signWithBaselineLTAProfile (signRsaChain );
237
+ if ((boolean ) useSignature ) {
238
+ IExternalSignature pks =
239
+ new PrivateKeySignature (signRsaPrivateKey , DigestAlgorithms .SHA256 , FACTORY .getProviderName ());
240
+ padesSigner .signWithBaselineLTAProfile (signer , signRsaChain , pks , testTsa );
241
+ } else {
242
+ padesSigner .signWithBaselineLTAProfile (signer , signRsaChain , signRsaPrivateKey , testTsa );
243
+ }
228
244
229
245
PadesSigTest .basicCheckSignedDoc (outFileName , "Signature1" );
230
246
231
247
Assert .assertNull (SignaturesCompareTool .compareSignatures (outFileName , cmpFileName ));
232
248
}
233
249
234
- private PdfPadesSigner createPdfPadesSigner (PdfSigner signer , IExternalSignature externalSignature ,
235
- PrivateKey privateKey ) {
250
+ @ Test
251
+ public void prolongDocumentSignaturesTest ()
252
+ throws GeneralSecurityException , IOException , AbstractOperatorCreationException , AbstractPKCSException {
253
+ String fileName = "prolongDocumentSignaturesTest" + comparisonPdfId + (FIPS_MODE ? "_FIPS.pdf" : ".pdf" );
254
+ String outFileName = destinationFolder + fileName ;
255
+ String cmpFileName = sourceFolder + "cmp_" + fileName ;
256
+ String srcFileName = sourceFolder + "padesSignatureLevelLTA.pdf" ;
257
+ String tsaCertFileName = certsSrc + "tsCertRsa.pem" ;
258
+ String caCertFileName = certsSrc + "rootRsa.pem" ;
259
+
260
+ Certificate [] tsaChain = PemFileHelper .readFirstChain (tsaCertFileName );
261
+ PrivateKey tsaPrivateKey = PemFileHelper .readFirstKey (tsaCertFileName , password );
262
+ X509Certificate caCert = (X509Certificate ) PemFileHelper .readFirstChain (caCertFileName )[0 ];
263
+ PrivateKey caPrivateKey = PemFileHelper .readFirstKey (caCertFileName , password );
264
+
265
+ TestTsaClient testTsa = new TestTsaClient (Arrays .asList (tsaChain ), tsaPrivateKey );
266
+ ICrlClient crlClient = new TestCrlClient ().addBuilderForCertIssuer (caCert , caPrivateKey );
267
+ TestOcspClient ocspClient = new TestOcspClient ().addBuilderForCertIssuer (caCert , caPrivateKey );
268
+
269
+ PdfPadesSigner padesSigner = new PdfPadesSigner ();
270
+ if ((boolean ) useTempFolder ) {
271
+ padesSigner .setTemporaryDirectoryPath (destinationFolder );
272
+ }
273
+ padesSigner .setOcspClient (ocspClient ).setCrlClient (crlClient );
236
274
if ((boolean ) useSignature ) {
237
- return new PdfPadesSigner (signer , externalSignature );
275
+ padesSigner .prolongSignatures (new PdfReader (FileUtil .getInputStreamForFile (srcFileName )),
276
+ FileUtil .getFileOutputStream (outFileName ), testTsa );
277
+ } else {
278
+ padesSigner .prolongSignatures (new PdfReader (FileUtil .getInputStreamForFile (srcFileName )),
279
+ FileUtil .getFileOutputStream (outFileName ));
238
280
}
239
- return new PdfPadesSigner (signer , privateKey );
281
+
282
+ PadesSigTest .basicCheckSignedDoc (outFileName , "Signature1" );
283
+ Assert .assertNull (SignaturesCompareTool .compareSignatures (outFileName , cmpFileName ));
240
284
}
241
285
242
286
private PdfSigner createPdfSigner (String srcFileName , String outFileName ) throws IOException {
243
- PdfSigner signer = new PdfSigner (new PdfReader (srcFileName ), new FileOutputStream (outFileName ), new StampingProperties ());
287
+ PdfSigner signer = new PdfSigner (new PdfReader (srcFileName ), FileUtil .getFileOutputStream (outFileName ),
288
+ new StampingProperties ());
244
289
signer .setFieldName ("Signature1" );
245
290
signer .getSignatureAppearance ()
246
291
.setPageRect (new Rectangle (50 , 650 , 200 , 100 ))
0 commit comments