1
+ package com .itextpdf .signatures .sign ;
2
+
3
+ import com .itextpdf .kernel .pdf .PdfArray ;
4
+ import com .itextpdf .kernel .pdf .PdfDictionary ;
5
+ import com .itextpdf .kernel .pdf .PdfDocument ;
6
+ import com .itextpdf .kernel .pdf .PdfName ;
7
+ import com .itextpdf .kernel .pdf .PdfReader ;
8
+ import com .itextpdf .kernel .pdf .PdfWriter ;
9
+ import com .itextpdf .kernel .pdf .StampingProperties ;
10
+ import com .itextpdf .signatures .LtvVerification ;
11
+ import com .itextpdf .signatures .testutils .Pkcs12FileHelper ;
12
+ import com .itextpdf .signatures .testutils .client .TestCrlClient ;
13
+ import com .itextpdf .signatures .testutils .client .TestOcspClient ;
14
+ import com .itextpdf .signatures .testutils .client .TestTsaClient ;
15
+ import com .itextpdf .test .ExtendedITextTest ;
16
+ import com .itextpdf .test .annotations .type .IntegrationTest ;
17
+
18
+ import java .security .GeneralSecurityException ;
19
+ import java .security .PrivateKey ;
20
+ import java .security .Security ;
21
+ import java .security .cert .Certificate ;
22
+ import java .security .cert .X509Certificate ;
23
+ import java .util .Arrays ;
24
+
25
+ import org .junit .Assert ;
26
+ import org .junit .BeforeClass ;
27
+ import org .junit .Test ;
28
+ import org .junit .experimental .categories .Category ;
29
+
30
+ @ Category ( IntegrationTest .class )
31
+ public class LtvWithTwoSignatures extends ExtendedITextTest {
32
+
33
+ private static final String certsSrc = "./src/test/resources/com/itextpdf/signatures/certs/" ;
34
+ private static final String sourceFolder = "./src/test/resources/com/itextpdf/signatures/sign/LtvWithTwoSignaturesTest/" ;
35
+ private static final String destinationFolder = "./target/test/com/itextpdf/signatures/sign/LtvWithTwoSignaturesTest/" ;
36
+
37
+ private static final char [] password = "testpass" .toCharArray ();
38
+
39
+ @ BeforeClass
40
+ public static void before () {
41
+ Security .addProvider (new org .bouncycastle .jce .provider .BouncyCastleProvider ());
42
+ createOrClearDestinationFolder (destinationFolder );
43
+ }
44
+
45
+ @ Test
46
+ public void AddLtvInfo () throws GeneralSecurityException , java .io .IOException {
47
+ String tsaCertFileName = certsSrc + "tsCertRsa.p12" ;
48
+ String caCertFileName = certsSrc + "rootRsa.p12" ;
49
+ String srcFileName = sourceFolder + "signedDoc.pdf" ;
50
+ String ltvFileName = destinationFolder + "ltvEnabledTest01.pdf" ;
51
+ String ltvFileName2 = destinationFolder + "ltvEnabledTest02.pdf" ;
52
+
53
+ Certificate [] tsaChain = Pkcs12FileHelper .readFirstChain (tsaCertFileName , password );
54
+ PrivateKey tsaPrivateKey = Pkcs12FileHelper .readFirstKey (tsaCertFileName , password , password );
55
+ X509Certificate caCert = (X509Certificate ) Pkcs12FileHelper .readFirstChain (caCertFileName , password )[0 ];
56
+ PrivateKey caPrivateKey = Pkcs12FileHelper .readFirstKey (caCertFileName , password , password );
57
+
58
+ TestTsaClient testTsa = new TestTsaClient (Arrays .asList (tsaChain ), tsaPrivateKey );
59
+ TestOcspClient testOcspClient = new TestOcspClient (caCert , caPrivateKey );
60
+ TestCrlClient testCrlClient = new TestCrlClient (caCert , caPrivateKey );
61
+
62
+ AddLtvInfo (srcFileName ,ltvFileName ,"sig" ,testOcspClient ,testCrlClient );
63
+ AddLtvInfo (ltvFileName ,ltvFileName2 ,"sig2" ,testOcspClient ,testCrlClient );
64
+
65
+ PdfReader reader = new PdfReader (ltvFileName2 );
66
+ PdfDocument document = new PdfDocument (reader );
67
+ PdfDictionary catalogDictionary = document .getCatalog ().getPdfObject ();
68
+ PdfDictionary dssDictionary = catalogDictionary .getAsDictionary (PdfName .DSS );
69
+
70
+ PdfDictionary vri = dssDictionary .getAsDictionary (PdfName .VRI );
71
+ Assert .assertNotNull (vri );
72
+ Assert .assertEquals (2 , vri .size ());
73
+
74
+ PdfArray ocsps = dssDictionary .getAsArray (PdfName .OCSPs );
75
+ Assert .assertNotNull (ocsps );
76
+ Assert .assertEquals (2 , ocsps .size ());
77
+
78
+ PdfArray certs = dssDictionary .getAsArray (PdfName .Certs );
79
+ Assert .assertNotNull (certs );
80
+ Assert .assertEquals (2 , certs .size ());
81
+
82
+ PdfArray crls = dssDictionary .getAsArray (PdfName .CRLs );
83
+ Assert .assertNotNull (crls );
84
+ Assert .assertEquals (1 , crls .size ());
85
+ }
86
+
87
+ private void AddLtvInfo (String src , String dest , String sigName , TestOcspClient testOcspClient ,TestCrlClient testCrlClient ) throws java .io .IOException , GeneralSecurityException {
88
+ PdfDocument document = new PdfDocument (new PdfReader (src ), new PdfWriter (dest ), new StampingProperties ().useAppendMode ());
89
+ LtvVerification ltvVerification = new LtvVerification (document , "BC" );
90
+ ltvVerification .addVerification (sigName , testOcspClient , testCrlClient , LtvVerification .CertificateOption .SIGNING_CERTIFICATE , LtvVerification .Level .OCSP_CRL , LtvVerification .CertificateInclusion .YES );
91
+ ltvVerification .merge ();
92
+ document .close ();
93
+ }
94
+ }
0 commit comments