@@ -44,6 +44,9 @@ This file is part of the iText (R) project.
44
44
45
45
import com .itextpdf .signatures .CertificateVerification ;
46
46
import com .itextpdf .signatures .VerificationException ;
47
+ import com .itextpdf .signatures .testutils .client .TestTsaClient ;
48
+ import com .itextpdf .test .annotations .LogMessage ;
49
+ import com .itextpdf .test .annotations .LogMessages ;
47
50
import com .itextpdf .test .signutils .Pkcs12FileHelper ;
48
51
import com .itextpdf .test .ExtendedITextTest ;
49
52
import com .itextpdf .test .ITextTest ;
@@ -53,12 +56,17 @@ This file is part of the iText (R) project.
53
56
import java .security .KeyStoreException ;
54
57
import java .security .NoSuchAlgorithmException ;
55
58
import java .security .NoSuchProviderException ;
59
+ import java .security .PrivateKey ;
56
60
import java .security .Security ;
57
- import java .security .UnrecoverableKeyException ;
58
61
import java .security .cert .Certificate ;
59
62
import java .security .cert .CertificateException ;
63
+ import java .util .Arrays ;
60
64
import java .util .List ;
65
+
66
+ import org .bouncycastle .asn1 .ASN1Sequence ;
67
+ import org .bouncycastle .asn1 .cms .ContentInfo ;
61
68
import org .bouncycastle .jce .provider .BouncyCastleProvider ;
69
+ import org .bouncycastle .tsp .TimeStampToken ;
62
70
import org .junit .AfterClass ;
63
71
import org .junit .Assert ;
64
72
import org .junit .BeforeClass ;
@@ -92,4 +100,45 @@ public void validCertificateChain01() throws CertificateException, NoSuchAlgorit
92
100
93
101
Assert .assertTrue (verificationExceptions .isEmpty ());
94
102
}
103
+
104
+ @ Test
105
+ public void timestampCertificateAndKeyStoreCorrespondTest () throws Exception {
106
+ String tsaCertFileName = certsSrc + "tsCertRsa.p12" ;
107
+
108
+ KeyStore caKeyStore = Pkcs12FileHelper .initStore (tsaCertFileName , password );
109
+
110
+ Assert .assertTrue (verifyTimestampCertificates (tsaCertFileName , caKeyStore ));
111
+ }
112
+
113
+ @ Test
114
+ @ LogMessages (messages = @ LogMessage (messageTemplate = "certificate hash does not match certID hash." ))
115
+ public void timestampCertificateAndKeyStoreDoNotCorrespondTest () throws Exception {
116
+ String tsaCertFileName = certsSrc + "tsCertRsa.p12" ;
117
+ String notTsaCertFileName = certsSrc + "rootRsa.p12" ;
118
+
119
+ KeyStore caKeyStore = Pkcs12FileHelper .initStore (notTsaCertFileName , password );
120
+
121
+ Assert .assertFalse (verifyTimestampCertificates (tsaCertFileName , caKeyStore ));
122
+ }
123
+
124
+ @ Test
125
+ @ LogMessages (messages = @ LogMessage (messageTemplate = "Unexpected exception was thrown during keystore processing" ))
126
+ public void keyStoreWithoutCertificatesTest () throws Exception {
127
+ String tsaCertFileName = certsSrc + "tsCertRsa.p12" ;
128
+
129
+ Assert .assertFalse (verifyTimestampCertificates (tsaCertFileName , null ));
130
+ }
131
+
132
+ private static boolean verifyTimestampCertificates (String tsaClientCertificate , KeyStore caKeyStore ) throws Exception {
133
+ Certificate [] tsaChain = Pkcs12FileHelper .readFirstChain (tsaClientCertificate , password );
134
+ PrivateKey tsaPrivateKey = Pkcs12FileHelper .readFirstKey (tsaClientCertificate , password , password );
135
+
136
+ TestTsaClient testTsaClient = new TestTsaClient (Arrays .asList (tsaChain ), tsaPrivateKey );
137
+
138
+ byte [] tsaCertificateBytes = testTsaClient .getTimeStampToken (testTsaClient .getMessageDigest ().digest ());
139
+ TimeStampToken timeStampToken = new TimeStampToken (
140
+ ContentInfo .getInstance (ASN1Sequence .getInstance (tsaCertificateBytes )));
141
+
142
+ return CertificateVerification .verifyTimestampCertificates (timeStampToken , caKeyStore , null );
143
+ }
95
144
}
0 commit comments