Skip to content

Commit 22758a9

Browse files
author
Eugene Bochilo
committed
Cover CertificateVerification#verifyTimestampCertificates with tests
DEVSIX-6100
1 parent c09528c commit 22758a9

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ This file is part of the iText (R) project.
6464
*/
6565
public class CertificateVerification {
6666

67-
6867
/**
6968
* The Logger instance.
7069
*/
@@ -244,16 +243,14 @@ public static boolean verifyTimestampCertificates(TimeStampToken ts, KeyStore ke
244243
try {
245244
for (X509Certificate certStoreX509 : SignUtils.getCertificates(keystore)) {
246245
try {
247-
248246
SignUtils.isSignatureValid(ts, certStoreX509, provider);
249247
return true;
250248
} catch (Exception ex) {
251249
exceptionsThrown.add(ex);
252-
253250
}
254251
}
255252
} catch (Exception e) {
256-
exceptionsThrown.add(e);
253+
LOGGER.error("Unexpected exception was thrown during keystore processing", e);
257254
}
258255

259256
for (Exception ex : exceptionsThrown) {

sign/src/test/java/com/itextpdf/signatures/verify/CertificateVerificationClassTest.java

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ This file is part of the iText (R) project.
4444

4545
import com.itextpdf.signatures.CertificateVerification;
4646
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;
4750
import com.itextpdf.test.signutils.Pkcs12FileHelper;
4851
import com.itextpdf.test.ExtendedITextTest;
4952
import com.itextpdf.test.ITextTest;
@@ -53,12 +56,17 @@ This file is part of the iText (R) project.
5356
import java.security.KeyStoreException;
5457
import java.security.NoSuchAlgorithmException;
5558
import java.security.NoSuchProviderException;
59+
import java.security.PrivateKey;
5660
import java.security.Security;
57-
import java.security.UnrecoverableKeyException;
5861
import java.security.cert.Certificate;
5962
import java.security.cert.CertificateException;
63+
import java.util.Arrays;
6064
import java.util.List;
65+
66+
import org.bouncycastle.asn1.ASN1Sequence;
67+
import org.bouncycastle.asn1.cms.ContentInfo;
6168
import org.bouncycastle.jce.provider.BouncyCastleProvider;
69+
import org.bouncycastle.tsp.TimeStampToken;
6270
import org.junit.AfterClass;
6371
import org.junit.Assert;
6472
import org.junit.BeforeClass;
@@ -92,4 +100,45 @@ public void validCertificateChain01() throws CertificateException, NoSuchAlgorit
92100

93101
Assert.assertTrue(verificationExceptions.isEmpty());
94102
}
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+
}
95144
}

0 commit comments

Comments
 (0)