Skip to content

Commit 45e3488

Browse files
committed
Cover OcspClientBouncyCastle methods with tests
DEVSIX-4944 Autoported commit. Original commit hash: [f1732a245]
1 parent 57ca93f commit 45e3488

File tree

4 files changed

+154
-5
lines changed

4 files changed

+154
-5
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU Affero General Public License version 3
8+
as published by the Free Software Foundation with the addition of the
9+
following permission added to Section 15 as permitted in Section 7(a):
10+
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY
11+
ITEXT GROUP. ITEXT GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT
12+
OF THIRD PARTY RIGHTS
13+
14+
This program is distributed in the hope that it will be useful, but
15+
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16+
or FITNESS FOR A PARTICULAR PURPOSE.
17+
See the GNU Affero General Public License for more details.
18+
You should have received a copy of the GNU Affero General Public License
19+
along with this program; if not, see http://www.gnu.org/licenses or write to
20+
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21+
Boston, MA, 02110-1301 USA, or download the license from the following URL:
22+
http://itextpdf.com/terms-of-use/
23+
24+
The interactive user interfaces in modified source and object code versions
25+
of this program must display Appropriate Legal Notices, as required under
26+
Section 5 of the GNU Affero General Public License.
27+
28+
In accordance with Section 7(b) of the GNU Affero General Public License,
29+
a covered work must retain the producer line in every PDF that is created
30+
or manipulated using iText.
31+
32+
You can be released from the requirements of the license by purchasing
33+
a commercial license. Buying such a license is mandatory as soon as you
34+
develop commercial activities involving the iText software without
35+
disclosing the source code of your own applications.
36+
These activities include: offering paid services to customers as an ASP,
37+
serving PDFs on the fly in a web application, shipping iText with a closed
38+
source product.
39+
40+
For more information, please contact iText Software Corp. at this
41+
42+
*/
43+
using System;
44+
using Org.BouncyCastle.Crypto;
45+
using Org.BouncyCastle.Ocsp;
46+
using Org.BouncyCastle.X509;
47+
using iText.Signatures.Testutils;
48+
using iText.Signatures.Testutils.Builder;
49+
using iText.Test;
50+
using iText.Test.Attributes;
51+
using iText.Test.Signutils;
52+
53+
namespace iText.Signatures {
54+
public class OcspClientBouncyCastleTest : ExtendedITextTest {
55+
private static readonly String certsSrc = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
56+
.CurrentContext.TestDirectory) + "/resources/itext/signatures/certs/";
57+
58+
private static readonly char[] password = "testpass".ToCharArray();
59+
60+
private static readonly String caCertFileName = certsSrc + "rootRsa.p12";
61+
62+
private static X509Certificate checkCert;
63+
64+
private static X509Certificate rootCert;
65+
66+
private static TestOcspResponseBuilder builder;
67+
68+
[NUnit.Framework.OneTimeSetUp]
69+
public static void Before() {
70+
}
71+
72+
[NUnit.Framework.SetUp]
73+
public virtual void SetUp() {
74+
X509Certificate caCert = (X509Certificate)Pkcs12FileHelper.ReadFirstChain(caCertFileName, password)[0];
75+
ICipherParameters caPrivateKey = Pkcs12FileHelper.ReadFirstKey(caCertFileName, password, password);
76+
builder = new TestOcspResponseBuilder(caCert, caPrivateKey);
77+
checkCert = (X509Certificate)Pkcs12FileHelper.ReadFirstChain(certsSrc + "signCertRsa01.p12", password)[0];
78+
rootCert = builder.GetIssuerCert();
79+
}
80+
81+
[NUnit.Framework.Test]
82+
public virtual void GetBasicOCSPRespTest() {
83+
OcspClientBouncyCastle ocspClientBouncyCastle = CreateOcspClient();
84+
BasicOcspResp basicOCSPResp = ocspClientBouncyCastle.GetBasicOCSPResp(checkCert, rootCert, null);
85+
NUnit.Framework.Assert.IsNotNull(basicOCSPResp);
86+
NUnit.Framework.Assert.IsTrue(basicOCSPResp.Responses.Length > 0);
87+
}
88+
89+
[NUnit.Framework.Test]
90+
public virtual void GetBasicOCSPRespNullTest() {
91+
OCSPVerifier ocspVerifier = new OCSPVerifier(null, null);
92+
OcspClientBouncyCastle ocspClientBouncyCastle = new OcspClientBouncyCastle(ocspVerifier);
93+
BasicOcspResp basicOCSPResp = ocspClientBouncyCastle.GetBasicOCSPResp(checkCert, null, null);
94+
NUnit.Framework.Assert.IsNull(basicOCSPResp);
95+
}
96+
97+
[NUnit.Framework.Test]
98+
[LogMessage("OCSP response could not be verified")]
99+
public virtual void GetBasicOCSPRespLogMessageTest() {
100+
OcspClientBouncyCastle ocspClientBouncyCastle = CreateOcspClient();
101+
BasicOcspResp basicOCSPResp = ocspClientBouncyCastle.GetBasicOCSPResp(null, null, null);
102+
NUnit.Framework.Assert.IsNull(basicOCSPResp);
103+
}
104+
105+
[NUnit.Framework.Test]
106+
public virtual void GetEncodedTest() {
107+
OcspClientBouncyCastle ocspClientBouncyCastle = CreateOcspClient();
108+
byte[] encoded = ocspClientBouncyCastle.GetEncoded(checkCert, rootCert, null);
109+
NUnit.Framework.Assert.IsNotNull(encoded);
110+
NUnit.Framework.Assert.IsTrue(encoded.Length > 0);
111+
}
112+
113+
private static OcspClientBouncyCastle CreateOcspClient() {
114+
OCSPVerifier ocspVerifier = new OCSPVerifier(null, null);
115+
return new OcspClientBouncyCastleTest.TestOcspClientBouncyCastle(ocspVerifier);
116+
}
117+
118+
private sealed class TestOcspClientBouncyCastle : OcspClientBouncyCastle {
119+
public TestOcspClientBouncyCastle(OCSPVerifier verifier)
120+
: base(verifier) {
121+
}
122+
123+
internal override OcspResp GetOcspResponse(X509Certificate chCert, X509Certificate rCert, String url) {
124+
try {
125+
CertificateID id = SignTestPortUtil.GenerateCertificateId(rootCert, checkCert.SerialNumber, Org.BouncyCastle.Ocsp.CertificateID.HashSha1
126+
);
127+
BasicOcspResp basicOCSPResp = builder.MakeOcspResponseObject(SignTestPortUtil.GenerateOcspRequestWithNonce
128+
(id).GetEncoded());
129+
return new OCSPRespGenerator().Generate(Org.BouncyCastle.Asn1.Ocsp.OcspResponseStatus.Successful, basicOCSPResp
130+
);
131+
}
132+
catch (Exception e) {
133+
throw new OcspException(e.Message);
134+
}
135+
}
136+
}
137+
}
138+
}

itext.tests/itext.sign.tests/itext/signatures/testutils/builder/TestOcspResponseBuilder.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public virtual void SetNextUpdate(DateTime nextUpdate) {
9393
}
9494

9595
public virtual byte[] MakeOcspResponse(byte[] requestBytes) {
96+
BasicOcspResp ocspResponse = MakeOcspResponseObject(requestBytes);
97+
return ocspResponse.GetEncoded();
98+
}
99+
100+
public virtual BasicOcspResp MakeOcspResponseObject(byte[] requestBytes) {
96101
OcspReq ocspRequest = new OcspReq(requestBytes);
97102
Req[] requestList = ocspRequest.GetRequestList();
98103

@@ -107,9 +112,7 @@ public virtual byte[] MakeOcspResponse(byte[] requestBytes) {
107112
responseBuilder.AddResponse(req.GetCertID(), certificateStatus, thisUpdate.ToUniversalTime(), nextUpdate.ToUniversalTime(), null);
108113
}
109114
DateTime time = DateTimeUtil.GetCurrentUtcTime();
110-
BasicOcspResp ocspResponse = responseBuilder.Generate(new Asn1SignatureFactory(SIGN_ALG, (AsymmetricKeyParameter)issuerPrivateKey), new X509Certificate[] { issuerCert }, time);
111-
// return new OCSPRespBuilder().build(ocspResult, ocspResponse).getEncoded();
112-
return ocspResponse.GetEncoded();
115+
return responseBuilder.Generate(new Asn1SignatureFactory(SIGN_ALG, (AsymmetricKeyParameter)issuerPrivateKey), new X509Certificate[] { issuerCert }, time);
113116
}
114117
}
115118
}

itext/itext.sign/itext/signatures/OcspClientBouncyCastle.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,15 @@ private static OcspReq GenerateOCSPRequest(X509Certificate issuerCert, BigIntege
150150
return SignUtils.GenerateOcspRequestWithNonce(id);
151151
}
152152

153-
private OcspResp GetOcspResponse(X509Certificate checkCert, X509Certificate rootCert, String url) {
153+
/// <summary>Gets an OCSP response object using BouncyCastle.</summary>
154+
/// <param name="checkCert">to certificate to check</param>
155+
/// <param name="rootCert">the parent certificate</param>
156+
/// <param name="url">
157+
/// to get the verification. It it's null it will be taken
158+
/// from the check cert or from other implementation specific source
159+
/// </param>
160+
/// <returns>an OCSP response</returns>
161+
internal virtual OcspResp GetOcspResponse(X509Certificate checkCert, X509Certificate rootCert, String url) {
154162
if (checkCert == null || rootCert == null) {
155163
return null;
156164
}

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a717943d40f439f04863f96873b5dd79dac77351
1+
f1732a245a993f40926832877c8639bbd0ff93b2

0 commit comments

Comments
 (0)