|
| 1 | +package com.itextpdf.signatures.verify; |
| 2 | + |
| 3 | +import com.itextpdf.signatures.CertificateVerification; |
| 4 | +import com.itextpdf.signatures.testutils.client.TestOcspClient; |
| 5 | +import com.itextpdf.test.ExtendedITextTest; |
| 6 | +import com.itextpdf.test.annotations.LogMessage; |
| 7 | +import com.itextpdf.test.annotations.LogMessages; |
| 8 | +import com.itextpdf.test.annotations.type.UnitTest; |
| 9 | +import com.itextpdf.test.signutils.Pkcs12FileHelper; |
| 10 | +import org.bouncycastle.asn1.ASN1Primitive; |
| 11 | +import org.bouncycastle.asn1.ocsp.BasicOCSPResponse; |
| 12 | +import org.bouncycastle.cert.ocsp.BasicOCSPResp; |
| 13 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
| 14 | +import org.junit.Assert; |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.experimental.categories.Category; |
| 18 | + |
| 19 | +import java.security.PrivateKey; |
| 20 | +import java.security.Security; |
| 21 | +import java.security.cert.X509Certificate; |
| 22 | + |
| 23 | +@Category(UnitTest.class) |
| 24 | +public class OcspCertificateVerificationTest extends ExtendedITextTest { |
| 25 | + |
| 26 | + // Such messageTemplate is equal to any log message. This is required for porting reasons. |
| 27 | + private static final String ANY_LOG_MESSAGE = "{0}"; |
| 28 | + |
| 29 | + private static final String ocspCertsSrc = "./src/test/resources/com/itextpdf/signatures/verify/OcspCertificateVerificationTest/"; |
| 30 | + |
| 31 | + private static final String rootOcspCert = ocspCertsSrc + "ocspRootRsa.p12"; |
| 32 | + private static final String signOcspCert = ocspCertsSrc + "ocspSignRsa.p12"; |
| 33 | + private static final String notOcspAndOcspCert = ocspCertsSrc + "notOcspAndOcspCertificates.p12"; |
| 34 | + |
| 35 | + private static final char[] password = "testpass".toCharArray(); |
| 36 | + private static final String ocspServiceUrl = "http://localhost:9000/demo/ocsp/ocsp-service"; |
| 37 | + |
| 38 | + private static X509Certificate checkCert; |
| 39 | + private static X509Certificate rootCert; |
| 40 | + |
| 41 | + @BeforeClass |
| 42 | + public static void before() throws Exception { |
| 43 | + Security.addProvider(new BouncyCastleProvider()); |
| 44 | + checkCert = (X509Certificate) Pkcs12FileHelper.readFirstChain(signOcspCert, password)[0]; |
| 45 | + rootCert = (X509Certificate) Pkcs12FileHelper.readFirstChain(rootOcspCert, password)[0]; |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void keyStoreWithRootOcspCertificateTest() throws Exception { |
| 50 | + BasicOCSPResp response = getOcspResponse(); |
| 51 | + |
| 52 | + Assert.assertTrue(CertificateVerification.verifyOcspCertificates( |
| 53 | + response, Pkcs12FileHelper.initStore(rootOcspCert, password), null)); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void keyStoreWithSignOcspCertificateTest() throws Exception { |
| 58 | + BasicOCSPResp response = getOcspResponse(); |
| 59 | + |
| 60 | + Assert.assertFalse(CertificateVerification.verifyOcspCertificates( |
| 61 | + response, Pkcs12FileHelper.initStore(signOcspCert, password), null)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void keyStoreWithNotOcspAndOcspCertificatesTest() throws Exception { |
| 66 | + BasicOCSPResp response = getOcspResponse(); |
| 67 | + |
| 68 | + Assert.assertTrue(CertificateVerification.verifyOcspCertificates( |
| 69 | + response, Pkcs12FileHelper.initStore(notOcspAndOcspCert, password), null)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @LogMessages(messages = @LogMessage(messageTemplate = ANY_LOG_MESSAGE)) |
| 74 | + public void keyStoreWithNotOcspCertificateTest() throws Exception { |
| 75 | + Assert.assertFalse(CertificateVerification.verifyOcspCertificates( |
| 76 | + null, Pkcs12FileHelper.initStore(signOcspCert, password), null)); |
| 77 | + } |
| 78 | + |
| 79 | + private static BasicOCSPResp getOcspResponse() throws Exception { |
| 80 | + TestOcspClient testClient = new TestOcspClient(); |
| 81 | + PrivateKey key = Pkcs12FileHelper.readFirstKey(rootOcspCert, password, password); |
| 82 | + testClient.addBuilderForCertIssuer(rootCert, key); |
| 83 | + byte[] ocspResponseBytes = testClient.getEncoded(checkCert, rootCert, ocspServiceUrl); |
| 84 | + ASN1Primitive var2 = ASN1Primitive.fromByteArray(ocspResponseBytes); |
| 85 | + return new BasicOCSPResp(BasicOCSPResponse.getInstance(var2)); |
| 86 | + } |
| 87 | +} |
0 commit comments