diff options
author | Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com> | 2020-03-25 14:08:49 +0100 |
---|---|---|
committer | Zebek Bogumil <bogumil.zebek@nokia.com> | 2020-03-27 11:47:14 +0100 |
commit | a999a364ac1d9557decfd3b0157cbe46181006f3 (patch) | |
tree | 99596cdb2981f53324bd4a526961a7c25460a749 /certService/src/test | |
parent | 9600d1361dd6e662248fe0704d1436ae56d446a7 (diff) |
Refactor CmpResponseHelper to support intermediate certificate
Issue-ID: AAF-1107
Signed-off-by: Bartosz Gardziejewski <bartosz.gardziejewski@nokia.com>
Change-Id: Ia2e2f9ba1fbcf0482121ffb5f451c408774481ba
Diffstat (limited to 'certService/src/test')
3 files changed, 615 insertions, 5 deletions
diff --git a/certService/src/test/java/org/onap/aaf/certservice/certification/CertificationProviderTest.java b/certService/src/test/java/org/onap/aaf/certservice/certification/CertificationProviderTest.java index cf3c7233..a590c5ea 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/certification/CertificationProviderTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/certification/CertificationProviderTest.java @@ -31,6 +31,7 @@ import org.onap.aaf.certservice.certification.model.CertificationModel; import org.onap.aaf.certservice.certification.model.CsrModel; import org.onap.aaf.certservice.cmpv2client.api.CmpClient; import org.onap.aaf.certservice.cmpv2client.exceptions.CmpClientException; +import org.onap.aaf.certservice.cmpv2client.model.Cmpv2CertificationModel; import java.io.IOException; import java.io.InputStream; @@ -38,9 +39,7 @@ import java.nio.charset.StandardCharsets; import java.security.NoSuchProviderException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; -import java.util.Arrays; import java.util.Collections; -import java.util.List; import java.util.Objects; import static org.assertj.core.api.Assertions.assertThat; @@ -115,14 +114,15 @@ class CertificationProviderTest { assertThat(exception.getMessage()).isEqualTo(expectedErrorMessage); } - private List<List<X509Certificate>> createCorrectClientResponse() + private Cmpv2CertificationModel createCorrectClientResponse() throws CertificateException, NoSuchProviderException { InputStream certificateChain = getClass().getClassLoader().getResourceAsStream("certificateChain.first"); InputStream trustedCertificate = getClass().getClassLoader().getResourceAsStream("trustedCertificates.first"); X509Certificate x509Certificate = new CertificateFactoryProvider().generateCertificate(certificateChain); X509Certificate x509TrustedCertificate = new CertificateFactoryProvider().generateCertificate(trustedCertificate); - return Arrays.asList(Collections.singletonList(x509Certificate), + return new Cmpv2CertificationModel( + Collections.singletonList(x509Certificate), Collections.singletonList(x509TrustedCertificate)); } 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 index 06eeecce..05bda54b 100644 --- a/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java +++ b/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/Cmpv2ClientTest.java @@ -62,6 +62,7 @@ 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 { @@ -152,7 +153,7 @@ class Cmpv2ClientTest { } CmpClientImpl cmpClient = spy(new CmpClientImpl(httpClient)); // when - List<List<X509Certificate>> cmpClientResult = + Cmpv2CertificationModel cmpClientResult = cmpClient.createCertificate(csrModel, server, notBefore, notAfter); // then assertNotNull(cmpClientResult); 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 new file mode 100644 index 00000000..c41d6364 --- /dev/null +++ b/certService/src/test/java/org/onap/aaf/certservice/cmpv2client/impl/CmpResponseHelperTest.java @@ -0,0 +1,609 @@ +/*- + * ============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" + + "BAMMDE1hbmFnZW1lbnRDQTEjMCEGA1UECgwaRUpCQ0Eg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TC8IpW5CaENt\n" + + "zTR7IGJr06ccwLgsigR7FxJKnEkxJiBxzkE3zFOEel3KAnV2b7KvOP7cJAzsCdcS\n" + + "iZU475XHOw4Ox3k8fHzhTJJa0Tzw5EjQ3GO99HTiUClGrjJuYDLfen1q7IQSNuTY\n" + + "FzxJZjyqzi34pkKeCNSPRj8Z8Q5aZiWqlmzSJmZRT83xzzeW/pQ1JwvIrWwrbEjR\n" + + "FPXBlUa1n2HztkDgeBQfRyMAj5ixFV+s1Jj+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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mr7wxIHFpM6h1HGY1QaeGp6W/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 = "" + + "-----BEGIN CERTIFICATE-----\n" + + "MIIDjDCCAnSgAwIBAgICEAIwDQYJKoZIhvcNAQELBQAwgYQxCzAJBgNVBAYTAlVT\n" + + "MRMwEQYDVQQIDApDYWxpZm9ybmlhMRYwFAYDVQQHDA1TYW4tRnJhbmNpc2NvMRkw\n" + + "FwYDVQQKDBBMaW51eC1Gb3VuZGF0aW9uMQ0wCwYDVQQLDARPTkFQMR4wHAYDVQQD\n" + + "DBVpbnRlcm1lZGlhdGUub25hcC5vcmcwHhcNMjAwMjEyMDk1MTI2WhcNMjIxMTA4\n" + + "MDk1MTI2WjB7MQswCQYDVQQGEwJVUzETMBEGA1UECAwKQ2FsaWZvcm5pYTEWMBQG\n" + + "A1UEBwwNU2FuLUZyYW5jaXNjbzEZMBcGA1UECgwQTGludXgtRm91bmRhdGlvbjEN\n" + + "MAsGA1UECwwET05BUDEVMBMGA1UEAwwMdmlkLm9uYXAub3JnMIIBIjANBgkqhkiG\n" + + "9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw+GIRzJzUOh0gtc+wzFJEdTnn+q5F10L0Yhr\n" + + "G1xKdjPieHIFGsoiXwcuCU8arNSqlz7ocx62KQRkcA8y6edlOAsYtdOEJvqEI9vc\n" + + "eyTB/HYsbzw3URPGch4AmibrQkKU9QvGwouHtHn4R2Ft2Y0tfEqv9hxj9v4njq4A\n" + + "EiDLAFLl5FmVyCZu/MtKngSgu1smcaFKTYySPMxytgJZexoa/ALZyyE0gRhsvwHm\n" + + "NLGCPt1bmE/PEGZybsCqliyTO0S56ncD55The7+D/UDS4kE1Wg0svlWon/YsE6QW\n" + + "B3oeJDX7Kr8ebDTIAErevIAD7Sm4ee5se2zxYrsYlj0MzHZtvwIDAQABoxAwDjAM\n" + + "BgNVHRMEBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQCvQ1pTvjON6vSlcJRKSY4r\n" + + "8q7L4/9ZaVXWJAjzEYJtPIqsgGiPWz0vGfgklowU6tZxp9zRZFXfMil+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/iyGfP8UZ3dpXlxfAN8MA0GC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TFhd+5dX\n" + + "vxq5y7MaqxHAY9J7+Qzq33+COVFQnaF7ddel2NbyUVb2b9ZINNsaZkkPXui6DtQ7\n" + + "/Fb/1tmAGWd3hMp75G2thBSzs816JMKKa9WD+4VGATEs6OSll4sv2fOZEn+0mAD3\n" + + "9q9c+WtLGIudOwcHwzPb2njtNntQSCK/tVOqbY+vzhMY3JW+p9oSrLDSdGC+pAKK\n" + + "m/wB+2VPIYcsPMtIhHC4tgoSaiCqjXYptaOh4b8ye8CPBUCpX/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(); + } + } +} |