|
| 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 | +} |
0 commit comments