Skip to content

Commit f23d48d

Browse files
Eugene BochiloiText-CI
authored andcommitted
Cover CertificateVerification#verifyOcspCertificates with tests
DEVSIX-6099 Autoported commit. Original commit hash: [fce60d8fa]
1 parent 5c7f832 commit f23d48d

File tree

8 files changed

+108
-10
lines changed

8 files changed

+108
-10
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ source product.
5656

5757
namespace iText.Signatures.Verify {
5858
public class CertificateVerificationClassTest : ExtendedITextTest {
59+
// Such messageTemplate is equal to any log message. This is required for porting reasons.
60+
private const String ANY_LOG_MESSAGE = "{0}";
61+
5962
private static readonly String certsSrc = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
6063
.CurrentContext.TestDirectory) + "/resources/itext/signatures/certs/";
6164

@@ -97,7 +100,7 @@ public virtual void TimestampCertificateAndKeyStoreDoNotCorrespondTest() {
97100
}
98101

99102
[NUnit.Framework.Test]
100-
[LogMessage("Unexpected exception was thrown during keystore processing")]
103+
[LogMessage(ANY_LOG_MESSAGE)]
101104
public virtual void KeyStoreWithoutCertificatesTest() {
102105
String tsaCertFileName = certsSrc + "tsCertRsa.p12";
103106
NUnit.Framework.Assert.IsFalse(VerifyTimestampCertificates(tsaCertFileName, null));
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using Org.BouncyCastle.Asn1;
3+
using Org.BouncyCastle.Asn1.Ocsp;
4+
using Org.BouncyCastle.Crypto;
5+
using Org.BouncyCastle.Ocsp;
6+
using Org.BouncyCastle.X509;
7+
using iText.Signatures;
8+
using iText.Signatures.Testutils.Client;
9+
using iText.Test;
10+
using iText.Test.Attributes;
11+
using iText.Test.Signutils;
12+
13+
namespace iText.Signatures.Verify {
14+
public class OcspCertificateVerificationTest : ExtendedITextTest {
15+
// Such messageTemplate is equal to any log message. This is required for porting reasons.
16+
private const String ANY_LOG_MESSAGE = "{0}";
17+
18+
private static readonly String ocspCertsSrc = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
19+
.CurrentContext.TestDirectory) + "/resources/itext/signatures/verify/OcspCertificateVerificationTest/";
20+
21+
private static readonly String rootOcspCert = ocspCertsSrc + "ocspRootRsa.p12";
22+
23+
private static readonly String signOcspCert = ocspCertsSrc + "ocspSignRsa.p12";
24+
25+
private static readonly String notOcspAndOcspCert = ocspCertsSrc + "notOcspAndOcspCertificates.p12";
26+
27+
private static readonly char[] password = "testpass".ToCharArray();
28+
29+
private const String ocspServiceUrl = "http://localhost:9000/demo/ocsp/ocsp-service";
30+
31+
private static X509Certificate checkCert;
32+
33+
private static X509Certificate rootCert;
34+
35+
[NUnit.Framework.OneTimeSetUp]
36+
public static void Before() {
37+
checkCert = (X509Certificate)Pkcs12FileHelper.ReadFirstChain(signOcspCert, password)[0];
38+
rootCert = (X509Certificate)Pkcs12FileHelper.ReadFirstChain(rootOcspCert, password)[0];
39+
}
40+
41+
[NUnit.Framework.Test]
42+
public virtual void KeyStoreWithRootOcspCertificateTest() {
43+
BasicOcspResp response = GetOcspResponse();
44+
NUnit.Framework.Assert.IsTrue(CertificateVerification.VerifyOcspCertificates(response, Pkcs12FileHelper.InitStore
45+
(rootOcspCert, password)));
46+
}
47+
48+
[NUnit.Framework.Test]
49+
public virtual void KeyStoreWithSignOcspCertificateTest() {
50+
BasicOcspResp response = GetOcspResponse();
51+
NUnit.Framework.Assert.IsFalse(CertificateVerification.VerifyOcspCertificates(response, Pkcs12FileHelper.InitStore
52+
(signOcspCert, password)));
53+
}
54+
55+
[NUnit.Framework.Test]
56+
public virtual void KeyStoreWithNotOcspAndOcspCertificatesTest() {
57+
BasicOcspResp response = GetOcspResponse();
58+
NUnit.Framework.Assert.IsTrue(CertificateVerification.VerifyOcspCertificates(response, Pkcs12FileHelper.InitStore
59+
(notOcspAndOcspCert, password)));
60+
}
61+
62+
[NUnit.Framework.Test]
63+
[LogMessage(ANY_LOG_MESSAGE)]
64+
public virtual void KeyStoreWithNotOcspCertificateTest() {
65+
NUnit.Framework.Assert.IsFalse(CertificateVerification.VerifyOcspCertificates(null, Pkcs12FileHelper.InitStore
66+
(signOcspCert, password)));
67+
}
68+
69+
private static BasicOcspResp GetOcspResponse() {
70+
TestOcspClient testClient = new TestOcspClient();
71+
ICipherParameters key = Pkcs12FileHelper.ReadFirstKey(rootOcspCert, password, password);
72+
testClient.AddBuilderForCertIssuer(rootCert, key);
73+
byte[] ocspResponseBytes = testClient.GetEncoded(checkCert, rootCert, ocspServiceUrl);
74+
Asn1Object var2 = Asn1Object.FromByteArray(ocspResponseBytes);
75+
return new BasicOcspResp(BasicOcspResponse.GetInstance(var2));
76+
}
77+
}
78+
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ source product.
4949
using Org.BouncyCastle.X509;
5050
using iText.Commons;
5151
using iText.Commons.Utils;
52+
using iText.Signatures.Logs;
5253

5354
namespace iText.Signatures {
5455
/// <summary>This class consists of some methods that allow you to verify certificates.</summary>
5556
public class CertificateVerification {
5657
/// <summary>The Logger instance.</summary>
57-
private static readonly ILogger LOGGER = ITextLogManager.GetLogger(typeof(CrlClientOnline));
58+
private static readonly ILogger LOGGER = ITextLogManager.GetLogger(typeof(CertificateVerification));
5859

5960
/// <summary>Verifies a single certificate for the current date.</summary>
6061
/// <param name="cert">the certificate to verify</param>
@@ -209,7 +210,9 @@ public static bool VerifyOcspCertificates(BasicOcspResp ocsp, List<X509Certifica
209210
try {
210211
foreach (X509Certificate certStoreX509 in SignUtils.GetCertificates(keystore)) {
211212
try {
212-
return SignUtils.IsSignatureValid(ocsp, certStoreX509);
213+
if (SignUtils.IsSignatureValid(ocsp, certStoreX509)) {
214+
return true;
215+
}
213216
}
214217
catch (Exception ex) {
215218
exceptionsThrown.Add(ex);
@@ -219,9 +222,7 @@ public static bool VerifyOcspCertificates(BasicOcspResp ocsp, List<X509Certifica
219222
catch (Exception e) {
220223
exceptionsThrown.Add(e);
221224
}
222-
foreach (Exception ex in exceptionsThrown) {
223-
LOGGER.LogError(ex, ex.Message);
224-
}
225+
LogExceptionMessages(exceptionsThrown);
225226
return false;
226227
}
227228

@@ -243,12 +244,16 @@ public static bool VerifyTimestampCertificates(TimeStampToken ts, List<X509Certi
243244
}
244245
}
245246
catch (Exception e) {
246-
LOGGER.LogError(e, "Unexpected exception was thrown during keystore processing");
247+
exceptionsThrown.Add(e);
247248
}
249+
LogExceptionMessages(exceptionsThrown);
250+
return false;
251+
}
252+
253+
private static void LogExceptionMessages(IList<Exception> exceptionsThrown) {
248254
foreach (Exception ex in exceptionsThrown) {
249-
LOGGER.LogError(ex, ex.Message);
255+
LOGGER.LogError(ex, ex.Message == null ? SignLogMessageConstant.EXCEPTION_WITHOUT_MESSAGE : ex.Message);
250256
}
251-
return false;
252257
}
253258
}
254259
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
3+
namespace iText.Signatures.Logs {
4+
/// <summary>Class which contains constants to be used in logging inside sign module.</summary>
5+
public sealed class SignLogMessageConstant {
6+
public const String EXCEPTION_WITHOUT_MESSAGE = "Unexpected exception without message was thrown during keystore processing";
7+
8+
private SignLogMessageConstant() {
9+
}
10+
// Private constructor will prevent the instantiation of this class directly
11+
}
12+
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3578748626f7e3c0b910f50baebb96f5783e3c02
1+
fce60d8fad64657b947cd4f245cd63e232cbce3d

0 commit comments

Comments
 (0)