diff options
Diffstat (limited to 'certService/src/test/java/org/onap/aaf/certservice/cmpv2client')
3 files changed, 0 insertions, 993 deletions
diff --git a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java b/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java deleted file mode 100644 index 05bda54b..00000000 --- a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java +++ /dev/null @@ -1,288 +0,0 @@ -/* - * Copyright (C) 2019 Ericsson Software Technology AB. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License - */ - -package org.onap.aaf.certservice.cmpv2client; - -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.spy; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.initMocks; - -import java.io.BufferedInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.security.KeyFactory; -import java.security.KeyPair; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.PrivateKey; -import java.security.PublicKey; -import java.security.Security; -import java.security.cert.X509Certificate; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; -import java.security.spec.X509EncodedKeySpec; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Collections; -import java.util.Date; -import java.util.List; - -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpEntity; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.impl.client.CloseableHttpClient; -import org.bouncycastle.asn1.x500.X500Name; -import org.bouncycastle.asn1.x500.X500NameBuilder; -import org.bouncycastle.asn1.x500.style.BCStyle; -import org.bouncycastle.jce.provider.BouncyCastleProvider; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.onap.aaf.certservice.certification.configuration.model.Authentication; -import org.onap.aaf.certservice.certification.configuration.model.Cmpv2Server; -import org.onap.aaf.certservice.certification.model.CsrModel; -import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException; -import org.onap.aaf.certservice.cmpv2client.impl.CmpClientImpl; -import org.onap.aaf.certservice.cmpv2client.model.Cmpv2CertificationModel; - -class Cmpv2ClientTest { - - static { - Security.addProvider(new BouncyCastleProvider()); - } - - private CsrModel csrModel; - private Cmpv2Server server; - private Date notBefore; - private Date notAfter; - private X500Name dn; - - @Mock - X509Certificate cert; - - @Mock - CloseableHttpClient httpClient; - - @Mock - CloseableHttpResponse httpResponse; - - @Mock - HttpEntity httpEntity; - - private static KeyPair keyPair; - - @BeforeEach - void setUp() - throws NoSuchProviderException, NoSuchAlgorithmException, IOException, - InvalidKeySpecException { - keyPair = loadKeyPair(); - dn = new X500NameBuilder() - .addRDN(BCStyle.O, "TestOrganization") - .build(); - initMocks(this); - } - - public KeyPair loadKeyPair() - throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, - NoSuchProviderException { - - final InputStream privateInputStream = this.getClass().getResourceAsStream("/privateKey"); - final InputStream publicInputStream = this.getClass().getResourceAsStream("/publicKey"); - BufferedInputStream bis = new BufferedInputStream(privateInputStream); - byte[] privateBytes = IOUtils.toByteArray(bis); - bis = new BufferedInputStream(publicInputStream); - byte[] publicBytes = IOUtils.toByteArray(bis); - - KeyFactory keyFactory = KeyFactory.getInstance("RSA", BouncyCastleProvider.PROVIDER_NAME); - X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicBytes); - PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); - - PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateBytes); - PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); - - return new KeyPair(publicKey, privateKey); - } - - @Test - void shouldReturnValidPkiMessageWhenCreateCertificateRequestMessageMethodCalledWithValidCsr() - throws Exception { - // given - Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); - Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - setCsrModelAndServerValues( - "mypassword", - "senderKID", - "http://127.0.0.1/ejbca/publicweb/cmp/cmp", - beforeDate, - afterDate); - when(httpClient.execute(any())).thenReturn(httpResponse); - when(httpResponse.getEntity()).thenReturn(httpEntity); - - try (final InputStream is = - this.getClass().getResourceAsStream("/ReturnedSuccessPKIMessageWithCertificateFile"); - BufferedInputStream bis = new BufferedInputStream(is)) { - - byte[] ba = IOUtils.toByteArray(bis); - doAnswer( - invocation -> { - OutputStream os = (ByteArrayOutputStream) invocation.getArguments()[0]; - os.write(ba); - return null; - }) - .when(httpEntity) - .writeTo(any(OutputStream.class)); - } - CmpClientImpl cmpClient = spy(new CmpClientImpl(httpClient)); - // when - Cmpv2CertificationModel cmpClientResult = - cmpClient.createCertificate(csrModel, server, notBefore, notAfter); - // then - assertNotNull(cmpClientResult); - } - - @Test - void - shouldThrowCmpClientExceptionWhenCreateCertificateRequestMessageMethodCalledWithWrongProtectedBytesInResponse() - throws Exception { - // given - Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); - Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - setCsrModelAndServerValues( - "password", - "senderKID", - "http://127.0.0.1/ejbca/publicweb/cmp/cmp", - beforeDate, - afterDate); - when(httpClient.execute(any())).thenReturn(httpResponse); - when(httpResponse.getEntity()).thenReturn(httpEntity); - - try (final InputStream is = - this.getClass().getResourceAsStream("/ReturnedSuccessPKIMessageWithCertificateFile"); - BufferedInputStream bis = new BufferedInputStream(is)) { - - byte[] ba = IOUtils.toByteArray(bis); - doAnswer( - invocation -> { - OutputStream os = (ByteArrayOutputStream) invocation.getArguments()[0]; - os.write(ba); - return null; - }) - .when(httpEntity) - .writeTo(any(OutputStream.class)); - } - CmpClientImpl cmpClient = spy(new CmpClientImpl(httpClient)); - // then - Assertions.assertThrows( - CmpClientException.class, - () -> cmpClient.createCertificate(csrModel, server, notBefore, notAfter)); - } - - @Test - void shouldThrowCmpClientExceptionWithPkiErrorExceptionWhenCmpClientCalledWithBadPassword() - throws Exception { - // given - Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); - Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - setCsrModelAndServerValues( - "password", - "senderKID", - "http://127.0.0.1/ejbca/publicweb/cmp/cmp", - beforeDate, - afterDate); - when(httpClient.execute(any())).thenReturn(httpResponse); - when(httpResponse.getEntity()).thenReturn(httpEntity); - - try (final InputStream is = - this.getClass().getResourceAsStream("/ReturnedFailurePKIMessageBadPassword"); - BufferedInputStream bis = new BufferedInputStream(is)) { - - byte[] ba = IOUtils.toByteArray(bis); - doAnswer( - invocation -> { - OutputStream os = (ByteArrayOutputStream) invocation.getArguments()[0]; - os.write(ba); - return null; - }) - .when(httpEntity) - .writeTo(any(OutputStream.class)); - } - CmpClientImpl cmpClient = spy(new CmpClientImpl(httpClient)); - - // then - Assertions.assertThrows( - CmpClientException.class, - () -> cmpClient.createCertificate(csrModel, server, notBefore, notAfter)); - } - - @Test - void shouldThrowIllegalArgumentExceptionWhencreateCertificateCalledWithInvalidCsr() - throws ParseException { - // given - Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); - setCsrModelAndServerValues( - "password", - "senderKID", - "http://127.0.0.1/ejbca/publicweb/cmp/cmp", - beforeDate, - afterDate); - CmpClientImpl cmpClient = new CmpClientImpl(httpClient); - // then - Assertions.assertThrows( - IllegalArgumentException.class, - () -> cmpClient.createCertificate(csrModel, server, notBefore, notAfter)); - } - - @Test - void shouldThrowIoExceptionWhenCreateCertificateCalledWithNoServerAvailable() - throws IOException, ParseException { - // given - Date beforeDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2019/11/11 12:00:00"); - Date afterDate = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").parse("2020/11/11 12:00:00"); - setCsrModelAndServerValues( - "myPassword", - "sender", - "http://127.0.0.1/ejbca/publicweb/cmp/cmpTest", - beforeDate, - afterDate); - when(httpClient.execute(any())).thenThrow(IOException.class); - CmpClientImpl cmpClient = spy(new CmpClientImpl(httpClient)); - // then - Assertions.assertThrows( - CmpClientException.class, - () -> cmpClient.createCertificate(csrModel, server, notBefore, notAfter)); - } - - private void setCsrModelAndServerValues(String iak, String rv, String externalCaUrl, Date notBefore, Date notAfter) { - csrModel = new CsrModel(null, dn, keyPair.getPrivate(), keyPair.getPublic(), Collections.emptyList()); - - Authentication authentication = new Authentication(); - authentication.setIak(iak); - authentication.setRv(rv); - server = new Cmpv2Server(); - server.setAuthentication(authentication); - server.setUrl(externalCaUrl); - server.setIssuerDN(dn); - this.notBefore = notBefore; - this.notAfter = notAfter; - } -} diff --git a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/external/RdnTest.java b/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/external/RdnTest.java deleted file mode 100644 index 6e42afe8..00000000 --- a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/external/RdnTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * PROJECT - * ================================================================================ - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.aaf.certservice.cmpv2client.external; - -import org.bouncycastle.asn1.ASN1ObjectIdentifier; -import org.bouncycastle.asn1.x500.style.BCStyle; -import org.bouncycastle.cert.CertException; -import org.junit.jupiter.api.Test; - -import java.util.Arrays; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -class RdnTest { - - @Test - void shouldCreateCorrectRdn() throws CertException { - // given - final String expectedValue = "ManagmentCA"; - final ASN1ObjectIdentifier expectedAoi = BCStyle.CN; - - //when - Rdn rdn1 = new Rdn("CN=ManagmentCA"); - Rdn rdn2 = new Rdn("CN = ManagmentCA "); - Rdn rdn3 = new Rdn("CN", "ManagmentCA"); - - //then - assertEquals(expectedValue, rdn1.getValue()); - assertEquals(expectedValue, rdn2.getValue()); - assertEquals(expectedValue, rdn3.getValue()); - assertEquals(expectedAoi, rdn1.getAoi()); - assertEquals(expectedAoi, rdn2.getAoi()); - assertEquals(expectedAoi, rdn3.getAoi()); - } - - @Test - void shouldCorrectlySplitAndTrimString() { - //given - String value1 = " T = Test"; - List<String> expected1 = Arrays.asList("T", "Test"); - - String value2 = "This 123 is 99 tested 12345 string"; - List<String> expected2 = Arrays.asList("This", "is 99 tested", "string"); - - //when - List<String> actual1 = Rdn.parseRdn("=", value1); - List<String> actual2 = Rdn.parseRdn("[0-9]{3,}", value2); - - //then - assertEquals(expected1, actual1); - assertEquals(expected2, actual2); - } - - @Test - void shouldConvertAoiStringToEnum() throws CertException { - Rdn rdn = new Rdn("CN", "ManagmentCA"); - - assertEquals(BCStyle.CN, rdn.getAoi("CN")); - assertEquals(BCStyle.C, rdn.getAoi("C")); - assertEquals(BCStyle.ST, rdn.getAoi("ST")); - assertEquals(BCStyle.L, rdn.getAoi("L")); - assertEquals(BCStyle.O, rdn.getAoi("O")); - assertEquals(BCStyle.OU, rdn.getAoi("OU")); - assertEquals(BCStyle.DC, rdn.getAoi("DC")); - assertEquals(BCStyle.GIVENNAME, rdn.getAoi("GN")); - assertEquals(BCStyle.SN, rdn.getAoi("SN")); - assertEquals(BCStyle.E, rdn.getAoi("E")); - assertEquals(BCStyle.E, rdn.getAoi("EMAIL")); - assertEquals(BCStyle.E, rdn.getAoi("EMAILADDRESS")); - assertEquals(BCStyle.INITIALS, rdn.getAoi("INITIALS")); - assertEquals(BCStyle.PSEUDONYM, rdn.getAoi("PSEUDONYM")); - assertEquals(BCStyle.GENERATION, rdn.getAoi("GENERATIONQUALIFIER")); - assertEquals(BCStyle.SERIALNUMBER, rdn.getAoi("SERIALNUMBER")); - assertThrows(CertException.class, () -> rdn.getAoi("INVALIDTAG")); - } -} diff --git a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/impl/CmpResponseHelperTest.java b/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/impl/CmpResponseHelperTest.java deleted file mode 100644 index c41d6364..00000000 --- a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/impl/CmpResponseHelperTest.java +++ /dev/null @@ -1,609 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2020 Nokia. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.aaf.certservice.cmpv2client.impl; - -import org.bouncycastle.asn1.cmp.CMPCertificate; -import org.bouncycastle.asn1.cmp.CertRepMessage; -import org.bouncycastle.asn1.cmp.PKIMessage; -import org.bouncycastle.util.io.pem.PemObject; -import org.bouncycastle.util.io.pem.PemReader; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException; -import org.onap.aaf.certservice.cmpv2client.model.Cmpv2CertificationModel; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.StringReader; -import java.security.NoSuchProviderException; -import java.security.Security; -import java.security.cert.Certificate; -import java.security.cert.CertificateEncodingException; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; - -import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -class CmpResponseHelperTest { - - - private static final String EXPECTED_ERROR_MESSAGE = "Something was wrong with the supplied certificate"; - - private static final String TEST_1LAYER_ENTITY_CERT = "" - + "-----BEGIN CERTIFICATE-----\n" - + "MIIEqDCCAxCgAwIBAgIUFioEkVJsxfZGGDMEyCA8Rin3uhQwDQYJKoZIhvcNAQEL\n" - + "BQAwYTEjMCEGCgmSJomT8ixkAQEME2MtMDM1ZDk4NTAwYzhiN2JiMjIxFTATBgNV\n" - + "BAMMDE1hbmFnZW1lbnRDQTEjMCEGA1UECgwaRUpCQ0EgQ29udGFpbmVyIFF1aWNr\n" - + "c3RhcnQwHhcNMjAwMzI0MTEzNTU0WhcNMjIwMzI0MTEzNTU0WjCBljEgMB4GCSqG\n" - + "SIb3DQEJARYRQ29tbW9uTmFtZUBjbi5jb20xDjAMBgNVBAMMBUNsMTIzMQ0wCwYD\n" - + "VQQLDARPTkFQMRkwFwYDVQQKDBBMaW51eC1Gb3VuZGF0aW9uMRYwFAYDVQQHDA1T\n" - + "YW4tRnJhbmNpc2NvMRMwEQY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Jj+cEYl3pjbugnuHfgBdSJokXFGBo6N\n" - + "8PTd1CnMGWcWiMyhbTwNm2UiSr5KhQbjABjiUzDp4C7jFhIzmu/4/tm2uA+y0xPN\n" - + "342uEZC0ZSZmpCIbQMhPaBNjSHeHj8NaLHjnt5jppLkMxScayRqMvSW07eNew2+k\n" - + "VYJD6z6gfy4y+Y5MSLfvddq1JdPDU86TFprtD1ydcUBS5tduYQG2+1bLgpE=" - + "\n-----END CERTIFICATE-----\n"; - - private static final String TEST_1LAYER_CA_CERT = "" - + "-----BEGIN CERTIFICATE-----\n" - + "MIIEszCCAxugAwIBAgIUEhkh+zJtXZN3K3kzQYcbp2smyIkwDQYJKoZIhvcNAQEL\n" - + "BQAwYTEjMCEGCgmSJomT8ixkAQEME2MtMDM1ZDk4NTAwYzhiN2JiMjIxFTATBgNV\n" - + "BAMMDE1hbmFnZW1lbnRDQTEjMCEGA1UECgwaRUpCQ0EgQ29udGFpbmVyIFF1aWNr\n" - + "c3RhcnQwHhcNMjAwMzI0MTAyODQyWhcNMzAwMzI0MTAyODQyWjBhMSMwIQYKCZIm\n" - + "iZPyLGQBAQwTYy0wMzVkOTg1MDBjOGI3YmIyMjEVMBMGA1UEAwwMTWFuYWdlbWVu\n" - + "dENBMSMwIQYDVQQKDBpFSkJDQSBDb250YWluZXIgUXVpY2tzdGFydDCCAaIwDQYJ\n" - + "KoZIhvcNAQEBBQADggGPADCCAYoCggGBAJyKZyKIRyW6cbga/I1YFJGCEEgs9JVU\n" - + "sV7MD5/yF4SIkJlZqFjJ9kfw8D5thg68zAx2vEWIpNTMroqb1eptIn/XsFoyM//6\n" - + "HzKrY3UUYWHx9sQMDZPenTL8LTRx+4szSen7rzrozH2pJat7kfX4EODEtQ6q7RQ2\n" - + "hmXoo7heeSgiHoeHsPGZixPGzcB27WBaY00Z/sP/n+f0CFaE04MKLw8WeQmq/RkC\n" - + "pj628+eBK0lGtEmUcT7z4CBy4x3hbhn9XHOb0+RlDk7rqFbsc09vHoZK2BfQ/r6e\n" - + "HguZjBQ5Ebqf6PiLF3HqkSW73toIdIy/olvQ2dLbOEyI4OnlObc+8xs/1AC7l9xX\n" - + "FkXY+NBv24KG1C2POXx14+ufHhWY0k2nIRUUlkUIJ7WGMWbuiNUXc1wSE1VrmY/c\n" - + "iXlhsJERqFc6bL/STlhOGuwmkdAD1/K8WS+o/QmIIX6cXlOR0U9bHMbD40F9fur6\n" - + "PV8wSKcQQNd0VHRLhmFwo4kkhZpDpuUp4QIDAQABo2MwYTAPBgNVHRMBAf8EBTAD\n" - + "AQH/MB8GA1UdIwQYMBaAFDPaBc+EX/hCLe5cd+oZIxcQZ1tHMB0GA1UdDgQWBBQz\n" - + "2gXPhF/4Qi3uXHfqGSMXEGdbRzAOBgNVHQ8BAf8EBAMCAYYwDQYJKoZIhvcNAQEL\n" - + "BQADggGBAFGsyu5nWycdk8iva+uY98QnPQe/M6uaUGUis0vGn9UYxoz5ddtpF3Z+\n" - + "MsHgbS51BH9iRYn4ZkQoRoukIjt1iO86d6sgpUS5AStCXsylL4DwAY5G/K5i/Qw5\n" - + "x0lP/tRYwqh2tUhmnx1xZLOWbRFZ63A0YHdguj3CqaXQ/cxafYZe0zcNhX3iH3gf\n" - + "5kHH8E682RT0x4ibb1JtPioQ48+pweyfMlOJkJ7WmZEfiVQitQSSNOnw1hRORiUz\n" - + "oFb0MlYHqe/9lIb9nmzD8QQ9q0H8J6RBCFsntx/Z6oUM8GHr80zAvNjqFfR14lOo\n" - + "jp05w2mr7wxIHFpM6h1HGY1QaeGp6W/fi+N7+gSL3nu1LzXVCYNCTcGkBDeasovB\n" - + "ma70KHGO4ZyRcEMKFCxxE8y4GZnw/EhMhDDevXAVsHEzr6XsBCJkC8e2l3iW5IKH\n" - + "4N/f/k06d4kS5pL290dJ450zx/mBxYGJm+pPHZfDszqVeKn1m1ZhGT80150OePGQ\n" - + "Cc2ir84HwQ==" - + "\n-----END CERTIFICATE-----\n"; - - private static final String TEST_2LAYER_ENTITY_CERT =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+mPQSe+yo\n" - + "ULrZSQ/z48YHPueE/BNO/nT4aaVBEhPLR5aVwC7uQVX8H+m1V1UGT8lk9vdI9rej\n" - + "CI9l524sLCpdE4dFXiWK2XHEZ0Vfylk221u3IYEogVVA+UMX7BFPSsOnI2vtYK/i\n" - + "lwZtlri8LtTusNe4oiTkYyq+RSyDhtAswg8ANgvfHolhCHoLFj6w1IkG88UCmbwN\n" - + "d7BoGMy06y5MJxyXEZG0vR7eNeLey0TIh+rAszAFPsIQvrOHW+HuA+WLQAj1mhnm\n" - + "-----END CERTIFICATE-----"; - - private static final String TEST_2LAYER_INTERMEDIATE_CERT = "" - + "-----BEGIN CERTIFICATE-----\n" - + "MIIDqTCCApGgAwIBAgICEAAwDQYJKoZIhvcNAQELBQAwgZcxCzAJBgNVBAYTAlVT\n" - + "MRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1TYW4tRnJhbmNpc2NvMRkw\n" - + "FwYDVQQKDBBMaW51eC1Gb3VuZGF0aW9uMQ0wCwYDVQQLDARPTkFQMREwDwYDVQQD\n" - + "DAhvbmFwLm9yZzEeMBwGCSqGSIb3DQEJARYPdGVzdGVyQG9uYXAub3JnMB4XDTIw\n" - + "MDIxMjA5NDAxMloXDTIyMTEwODA5NDAxMlowgYQxCzAJBgNVBAYTAlVTMRMwEQYD\n" - + "VQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1TYW4tRnJhbmNpc2NvMRkwFwYDVQQK\n" - + "DBBMaW51eC1Gb3VuZGF0aW9uMQ0wCwYDVQQLDARPTkFQMR4wHAYDVQQDDBVpbnRl\n" - + "cm1lZGlhdGUub25hcC5vcmcwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIB\n" - + "AQC1oOYMZ6G+2DGDAizYnzdCNiogivlht1s4oqgem7fM1XFPxD2p31ATIibOdqr/\n" - + "gv1qemO9Q4r1xn6w1Ufq7T1K7PjnMzdSeTqZefurE2JM/HHx2QvW4TjMlz2ILgaD\n" - + "L1LN60kmMQSOi5VxKJpsrCQxbOsxhvefd212gny5AZMcjJe23kUd9OxUrtvpdLEv\n" - + "wI3vFEvT7oRUnEUg/XNz7qeg33vf1C39yMR+6O4s6oevgsEebVKjb+yOoS6zzGtz\n" - + "72wZjm07C54ZlO+4Uy+QAlMjRiU3mgWkKbkOy+4CvwehjhpTikdBs2DX39ZLGHhn\n" - + "L/0a2NYtGulp9XEqmTvRoI+PAgMBAAGjEDAOMAwGA1UdEwQFMAMBAf8wDQYJKoZI\n" - + "hvcNAQELBQADggEBADcitdJ6YswiV8jAD9GK0gf3+zqcGegt4kt+79JXlXYbb1sY\n" - + "q3o6prcB7nSUoClgF2xUPCslFGpM0Er9FCSFElQM/ru0l/KVmJS6kSpwEHvsYIH3\n" - + "q5anta+Pyk8JSQWAAw+qrind0uBQMnhR8Tn13tgV+Kjvg/xlH/nZIEdN5YtLB1cA\n" - + "beVsZRyRfVL9DeZU8s/MZ5wC3kgcEp5A4m5lg7HyBxBdqhzFcDr6xiy6OGqW8Yep\n" - + "xrwfc8Fw8a/lOv4U+tBeGNKPQDYaL9hh+oM+qMkNXsHXDqdJsuEGJtU4i3Wcwzoc\n" - + "XGN5NWV//4bP+NFmwgcn7AYCdRvz04A8GU/0Cwg=\n" - + "-----END CERTIFICATE-----"; - - private static final String TEST_2LAYER_CA_CERT = "" - + "-----BEGIN CERTIFICATE-----\n" - + "MIIDtzCCAp8CFAwqQddh4/iyGfP8UZ3dpXlxfAN8MA0GCSqGSIb3DQEBCwUAMIGX\n" - + "MQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQGA1UEBwwNU2Fu\n" - + "LUZyYW5jaXNjbzEZMBcGA1UECgwQTGludXgtRm91bmRhdGlvbjENMAsGA1UECwwE\n" - + "T05BUDERMA8GA1UEAwwIb25hcC5vcmcxHjAcBgkqhkiG9w0BCQEWD3Rlc3RlckBv\n" - + "bmFwLm9yZzAeFw0yMDAyMTIwOTM0Mj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CPBUCpX/AYYkN0Ow==\n" - + "-----END CERTIFICATE-----"; - - - @BeforeAll - static void setUpSecurity() { - Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); - } - - - @Test - void returnListOfCertificationWhenGivenCaCertInCaPubsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - PKIMessage respPkiMessage = mockExtraCerts(null); - - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT); - CMPCertificate[] cmpCertificates = {caCmpCertificate}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_1LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityCertificate(certs, TEST_1LAYER_ENTITY_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList(certs, caCmpCertificate); - } - - @Test - void returnListOfCertificationWhenGivenCaCertInExtraCertsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT); - CMPCertificate[] extraCmpCertificates = {caCmpCertificate}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - - CertRepMessage certRepMessage = mockCaPubs(null); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_1LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityCertificate(certs, TEST_1LAYER_ENTITY_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList(certs, caCmpCertificate); - } - - @Test - void returnListOfCertificationWhenGivenCaCertInExtraCertsAndExtraTrustAnchorInCaPubsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT); - CMPCertificate[] extraCmpCertificates = {caCmpCertificate}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - - CMPCertificate extraTrustAnchor = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate[] cmpCertificates = {extraTrustAnchor}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_1LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityCertificate(certs, TEST_1LAYER_ENTITY_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList( - certs, - caCmpCertificate, extraTrustAnchor - ); - } - - @Test - void returnListOfCertificationWhenGivenCaCertInExtraCertsAndExtraTrustAnchorInExtraCertsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate trustedCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT); - CMPCertificate[] extraCmpCertificates = {caCmpCertificate, trustedCmpCertificate}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - - CertRepMessage certRepMessage = mockCaPubs(null); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_1LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityCertificate(certs, TEST_1LAYER_ENTITY_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList( - certs, - caCmpCertificate, trustedCmpCertificate - ); - } - - @Test - void returnListOfCertificationWhenGivenCaCertAndIntermediateCertInExtraCertsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate intermediateCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_INTERMEDIATE_CERT); - CMPCertificate[] extraCmpCertificates = {caCmpCertificate, intermediateCmpCertificate}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - - CertRepMessage certRepMessage = mockCaPubs(null); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_2LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityAndIntermediateCertificate(certs, TEST_2LAYER_ENTITY_CERT, TEST_2LAYER_INTERMEDIATE_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList( - certs, - caCmpCertificate - ); - } - - @Test - void returnListOfCertificationWhenGivenCaCertAndIntermediateCertInCmpCertificatesAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - PKIMessage respPkiMessage = mockExtraCerts(null); - - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate intermediateCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_INTERMEDIATE_CERT); - CMPCertificate[] cmpCertificates = {caCmpCertificate, intermediateCmpCertificate}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_2LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityAndIntermediateCertificate(certs, TEST_2LAYER_ENTITY_CERT, TEST_2LAYER_INTERMEDIATE_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList( - certs, - caCmpCertificate - ); - } - - @Test - void returnListOfCertificationWhenGivenCaCertInCaPubsAndIntermediateCertInExtraCertsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate intermediateCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_INTERMEDIATE_CERT); - CMPCertificate[] extraCmpCertificates = {intermediateCmpCertificate}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate[] cmpCertificates = {caCmpCertificate}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_2LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityAndIntermediateCertificate(certs, TEST_2LAYER_ENTITY_CERT, TEST_2LAYER_INTERMEDIATE_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList( - certs, - caCmpCertificate - ); - } - - @Test - void returnListOfCertificationWhenGivenCaCertInCaPubsAndExtraCertsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT); - CMPCertificate[] extraCmpCertificates = {caCmpCertificate}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - CMPCertificate[] cmpCertificates = {mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT)}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_1LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityCertificate(certs, TEST_1LAYER_ENTITY_CERT); - assertThatRootCaAndTrustedCaAreInSecondList(certs, mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT)); - - } - - @Test - void returnListOfCertificationWhenGivenCaCertAndIntermediateCertInExtraCertsAndIntermediateCertInCaPubsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate intermediateCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_INTERMEDIATE_CERT); - CMPCertificate[] extraCmpCertificates = {caCmpCertificate, intermediateCmpCertificate}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - CMPCertificate[] cmpCertificates = {intermediateCmpCertificate}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_2LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityAndIntermediateCertificate(certs, TEST_2LAYER_ENTITY_CERT, TEST_2LAYER_INTERMEDIATE_CERT); - assertThatRootCaAndTrustedCaAreInSecondList( - certs, - caCmpCertificate - ); - } - - @Test - void returnListOfCertificationWhenGivenCaCertAndExtraTrustAnchorInCaPubsAndIntermediateCertInExtraCertsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate intermediateCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_INTERMEDIATE_CERT); - CMPCertificate[] extraCmpCertificates = {intermediateCmpCertificate}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate extraTrustAnchor = mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT); - CMPCertificate[] cmpCertificates = {caCmpCertificate, extraTrustAnchor}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_2LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityAndIntermediateCertificate(certs, TEST_2LAYER_ENTITY_CERT, TEST_2LAYER_INTERMEDIATE_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList( - certs, - caCmpCertificate, extraTrustAnchor - ); - } - - @Test - void returnListOfCertificationWhenGivenCaCertAndFirstExtraTrustAnchorInCaPubsAndIntermediateCertAndSecondExtraTrustAnchorInExtraCertsAndEntityCertInLeafCertificate() - throws CertificateException, CmpClientException, IOException, NoSuchProviderException { - // given - CMPCertificate intermediateCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_INTERMEDIATE_CERT); - CMPCertificate extraTrustAnchor01 = mockCmpCertificateFromPem(TEST_1LAYER_ENTITY_CERT); - CMPCertificate[] extraCmpCertificates = {intermediateCmpCertificate, extraTrustAnchor01}; - PKIMessage respPkiMessage = mockExtraCerts(extraCmpCertificates); - - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate extraTrustAnchor02 = mockCmpCertificateFromPem(TEST_1LAYER_CA_CERT); - CMPCertificate[] cmpCertificates = {caCmpCertificate, extraTrustAnchor02}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_2LAYER_ENTITY_CERT); - - // when - Cmpv2CertificationModel certs = CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate); - - // then - assertThatChainContainsEntityAndIntermediateCertificate(certs, TEST_2LAYER_ENTITY_CERT, TEST_2LAYER_INTERMEDIATE_CERT); - - assertThatRootCaAndTrustedCaAreInSecondList( - certs, - caCmpCertificate, extraTrustAnchor01, extraTrustAnchor02 - ); - } - - @Test - void throwsExceptionWhenNoCaCertForEntityCertIsGivenAndOnlyExtraTrustAnchorIsReturned() - throws CertificateException, IOException, NoSuchProviderException { - // given - - PKIMessage respPkiMessage = mockExtraCerts(null); - - CMPCertificate trustedCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate[] cmpCertificates = {trustedCmpCertificate}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_1LAYER_ENTITY_CERT); - - // when - Exception exception = assertThrows( - CmpClientException.class, - () -> CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate - ) - ); - - String actualMessage = exception.getMessage(); - - // then - assertThat(actualMessage).isEqualTo(EXPECTED_ERROR_MESSAGE); - } - - @Test - void throwsExceptionWhenBothExtraCertsAndCaPubsAreEmpty() - throws CertificateException, IOException, NoSuchProviderException { - // given - - PKIMessage respPkiMessage = mockExtraCerts(null); - CertRepMessage certRepMessage = mockCaPubs(null); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_1LAYER_ENTITY_CERT); - - // when - Exception exception = assertThrows( - CmpClientException.class, - () -> CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate - ) - ); - - String actualMessage = exception.getMessage(); - - // then - assertThat(actualMessage).isEqualTo(EXPECTED_ERROR_MESSAGE); - } - - @Test - void throwsExceptionWhenNoIntermediateCertForEntityCertIsGiven() - throws CertificateException, IOException, NoSuchProviderException { - // given - - PKIMessage respPkiMessage = mockExtraCerts(null); - - CMPCertificate caCmpCertificate = mockCmpCertificateFromPem(TEST_2LAYER_CA_CERT); - CMPCertificate[] cmpCertificates = {caCmpCertificate}; - CertRepMessage certRepMessage = mockCaPubs(cmpCertificates); - - X509Certificate leafCertificate = getX509CertificateFromPem(TEST_2LAYER_ENTITY_CERT); - - // when - Exception exception = assertThrows( - CmpClientException.class, - () -> CmpResponseHelper.verifyAndReturnCertChainAndTrustSTore( - respPkiMessage, certRepMessage, leafCertificate - ) - ); - - String actualMessage = exception.getMessage(); - - // then - assertThat(actualMessage).isEqualTo(EXPECTED_ERROR_MESSAGE); - } - - - private void assertThatRootCaAndTrustedCaAreInSecondList( - Cmpv2CertificationModel certs, CMPCertificate... rootAndTrustedCerts - ) throws IOException { - assertThat(certs.getTrustedCertificates().size()).isEqualTo(rootAndTrustedCerts.length); - for (CMPCertificate certificate : rootAndTrustedCerts) { - assertThat(certs.getTrustedCertificates()) - .extracting(Certificate::getEncoded) - .contains(certificate.getEncoded()); - } - } - - private void assertThatChainContainsEntityCertificate( - Cmpv2CertificationModel certs, String entityCertificate - ) throws CertificateEncodingException, IOException { - assertThat(certs.getCertificateChain().size()).isEqualTo(1); - assertThat(certs.getCertificateChain().get(0).getEncoded()).isEqualTo(createPemObject(entityCertificate).getContent()); - } - - private void assertThatChainContainsEntityAndIntermediateCertificate( - Cmpv2CertificationModel certs, String entityCertificate, String intermediateCertificate - ) throws CertificateEncodingException, IOException { - assertThat(certs.getCertificateChain().size()).isEqualTo(2); - assertThat(certs.getCertificateChain().get(0).getEncoded()).isEqualTo(createPemObject(entityCertificate).getContent()); - assertThat(certs.getCertificateChain().get(1).getEncoded()).isEqualTo(createPemObject(intermediateCertificate).getContent()); - } - - private X509Certificate getX509CertificateFromPem(String pem) throws CertificateException, NoSuchProviderException, IOException { - return (X509Certificate) - CertificateFactory.getInstance("X.509", "BC").generateCertificate( - new ByteArrayInputStream(createPemObject(pem).getContent()) - ); - } - - private PKIMessage mockExtraCerts(CMPCertificate[] cmpCertificates) { - PKIMessage respPkiMessage = mock(PKIMessage.class); - when(respPkiMessage.getExtraCerts()).thenReturn(cmpCertificates); - return respPkiMessage; - } - - private CertRepMessage mockCaPubs(CMPCertificate[] cmpCertificates) { - CertRepMessage certRepMessage = mock(CertRepMessage.class); - when(certRepMessage.getCaPubs()).thenReturn(cmpCertificates); - return certRepMessage; - } - - private CMPCertificate mockCmpCertificateFromPem(String pem) throws IOException { - return mockCmpCertificate(createPemObject(pem).getContent()); - } - - private CMPCertificate mockCmpCertificate(byte[] encodedCertificate) throws IOException { - CMPCertificate cmpCertificate01 = mock(CMPCertificate.class); - when(cmpCertificate01.getEncoded()).thenReturn(encodedCertificate); - return cmpCertificate01; - } - - private PemObject createPemObject(String pem) throws IOException { - try (StringReader stringReader = new StringReader(pem); - PemReader pemReader = new PemReader(stringReader)) { - return pemReader.readPemObject(); - } - } -} |