Skip to content

Commit fe8e672

Browse files
Eugene BochiloiText-CI
authored andcommitted
Cover CertificateVerification#verifyTimestampCertificates with tests
DEVSIX-6100 Autoported commit. Original commit hash: [22758a9a1]
1 parent 02e0c8a commit fe8e672

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

itext.tests/itext.sign.tests/itext/signatures/verify/CertificateVerificationClassTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ source product.
4242
*/
4343
using System;
4444
using System.Collections.Generic;
45+
using Org.BouncyCastle.Asn1;
46+
using Org.BouncyCastle.Asn1.Cms;
47+
using Org.BouncyCastle.Crypto;
48+
using Org.BouncyCastle.Tsp;
4549
using Org.BouncyCastle.X509;
50+
using iText.Commons.Utils;
4651
using iText.Signatures;
52+
using iText.Signatures.Testutils.Client;
4753
using iText.Test;
54+
using iText.Test.Attributes;
4855
using iText.Test.Signutils;
4956

5057
namespace iText.Signatures.Verify {
@@ -72,5 +79,39 @@ public virtual void ValidCertificateChain01() {
7279
, caKeyStore);
7380
NUnit.Framework.Assert.IsTrue(verificationExceptions.IsEmpty());
7481
}
82+
83+
[NUnit.Framework.Test]
84+
public virtual void TimestampCertificateAndKeyStoreCorrespondTest() {
85+
String tsaCertFileName = certsSrc + "tsCertRsa.p12";
86+
List<X509Certificate> caKeyStore = Pkcs12FileHelper.InitStore(tsaCertFileName, password);
87+
NUnit.Framework.Assert.IsTrue(VerifyTimestampCertificates(tsaCertFileName, caKeyStore));
88+
}
89+
90+
[NUnit.Framework.Test]
91+
[LogMessage("certificate hash does not match certID hash.")]
92+
public virtual void TimestampCertificateAndKeyStoreDoNotCorrespondTest() {
93+
String tsaCertFileName = certsSrc + "tsCertRsa.p12";
94+
String notTsaCertFileName = certsSrc + "rootRsa.p12";
95+
List<X509Certificate> caKeyStore = Pkcs12FileHelper.InitStore(notTsaCertFileName, password);
96+
NUnit.Framework.Assert.IsFalse(VerifyTimestampCertificates(tsaCertFileName, caKeyStore));
97+
}
98+
99+
[NUnit.Framework.Test]
100+
[LogMessage("Unexpected exception was thrown during keystore processing")]
101+
public virtual void KeyStoreWithoutCertificatesTest() {
102+
String tsaCertFileName = certsSrc + "tsCertRsa.p12";
103+
NUnit.Framework.Assert.IsFalse(VerifyTimestampCertificates(tsaCertFileName, null));
104+
}
105+
106+
private static bool VerifyTimestampCertificates(String tsaClientCertificate, List<X509Certificate> caKeyStore
107+
) {
108+
X509Certificate[] tsaChain = Pkcs12FileHelper.ReadFirstChain(tsaClientCertificate, password);
109+
ICipherParameters tsaPrivateKey = Pkcs12FileHelper.ReadFirstKey(tsaClientCertificate, password, password);
110+
TestTsaClient testTsaClient = new TestTsaClient(JavaUtil.ArraysAsList(tsaChain), tsaPrivateKey);
111+
byte[] tsaCertificateBytes = testTsaClient.GetTimeStampToken(testTsaClient.GetMessageDigest().Digest());
112+
TimeStampToken timeStampToken = new TimeStampToken(ContentInfo.GetInstance(Asn1Sequence.GetInstance(tsaCertificateBytes
113+
)));
114+
return CertificateVerification.VerifyTimestampCertificates(timeStampToken, caKeyStore);
115+
}
75116
}
76117
}

itext/itext.sign/itext/signatures/CertificateVerification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ public static bool VerifyTimestampCertificates(TimeStampToken ts, List<X509Certi
243243
}
244244
}
245245
catch (Exception e) {
246-
exceptionsThrown.Add(e);
246+
LOGGER.LogError(e, "Unexpected exception was thrown during keystore processing");
247247
}
248248
foreach (Exception ex in exceptionsThrown) {
249249
LOGGER.LogError(ex, ex.Message);

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c09528c66d2bb0e1b95bed9b284d9aeba778faaf
1+
22758a9a14a188fe166c90befcc99fdbde473415

0 commit comments

Comments
 (0)