2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.oom.certservice.certification.model;
23 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
24 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
25 import org.bouncycastle.util.io.pem.PemObject;
26 import org.junit.jupiter.api.Test;
27 import org.onap.oom.certservice.certification.Pkcs10CertificationRequestFactory;
28 import org.onap.oom.certservice.certification.PemObjectFactory;
29 import org.onap.oom.certservice.certification.exception.CsrDecryptionException;
30 import org.onap.oom.certservice.certification.exception.DecryptionException;
31 import org.onap.oom.certservice.certification.exception.KeyDecryptionException;
33 import java.io.IOException;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.junit.jupiter.api.Assertions.assertThrows;
37 import static org.junit.jupiter.api.Assertions.assertTrue;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.when;
40 import static org.onap.oom.certservice.certification.TestData.TEST_CSR;
41 import static org.onap.oom.certservice.certification.TestData.TEST_PEM;
42 import static org.onap.oom.certservice.certification.TestData.TEST_PK;
47 private final Pkcs10CertificationRequestFactory certificationRequestFactory
48 = new Pkcs10CertificationRequestFactory();
49 private final PemObjectFactory pemObjectFactory
50 = new PemObjectFactory();
53 void shouldByConstructedAndReturnProperFields() throws DecryptionException, IOException {
55 PemObject testPrivateKey = getPemPrivateKey();
56 PemObject testPublicKey = generateTestPublicKey();
57 PKCS10CertificationRequest testCsr = generateTestCertificationRequest();
60 CsrModel csrModel = generateTestCsrModel(testCsr);
63 assertThat(csrModel.getCsr())
65 assertThat(csrModel.getPrivateKey().getEncoded())
66 .contains(testPrivateKey.getContent());
67 assertThat(csrModel.getPublicKey().getEncoded())
68 .contains(testPublicKey.getContent());
69 assertThat(csrModel.getSans())
71 "gerrit.onap.org", "test.onap.org", "onap.com");
72 assertThat(csrModel.getSubjectData().toString())
74 "C=US,ST=California,L=San-Francisco,O=Linux-Foundation,OU=ONAP,CN=onap.org,E=tester@onap.org");
78 void shouldThrowExceptionWhenPublicKeyIsNotCorrect() throws DecryptionException, IOException {
80 PemObject testPrivateKey = getPemPrivateKey();
81 PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
82 SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
83 when(testCsr.getSubjectPublicKeyInfo())
84 .thenReturn(wrongKryInfo);
85 when(wrongKryInfo.getEncoded())
86 .thenThrow(new IOException());
89 Exception exception = assertThrows(
90 CsrDecryptionException.class,
91 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
94 String expectedMessage = "Reading Public Key from CSR failed";
95 String actualMessage = exception.getMessage();
98 assertTrue(actualMessage.contains(expectedMessage));
102 void shouldThrowExceptionWhenPrivateKeyPemIsNotProperPrivateKey() throws KeyDecryptionException, IOException {
104 PemObject testPrivateKey = getPemWrongKey();
105 PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
106 SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
107 when(testCsr.getSubjectPublicKeyInfo())
108 .thenReturn(wrongKryInfo);
109 when(wrongKryInfo.getEncoded())
110 .thenThrow(new IOException());
113 Exception exception = assertThrows(
114 KeyDecryptionException.class,
115 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
118 String expectedMessage = "Converting Private Key failed";
119 String actualMessage = exception.getMessage();
122 assertTrue(actualMessage.contains(expectedMessage));
126 void shouldThrowExceptionWhenPublicKeyPemIsNotProperPublicKey() throws KeyDecryptionException, IOException {
128 PemObject testPrivateKey = getPemPrivateKey();
129 PemObject testPublicKey = getPemWrongKey();
130 PKCS10CertificationRequest testCsr = mock(PKCS10CertificationRequest.class);
131 SubjectPublicKeyInfo wrongKryInfo = mock(SubjectPublicKeyInfo.class);
132 when(testCsr.getSubjectPublicKeyInfo())
133 .thenReturn(wrongKryInfo);
134 when(wrongKryInfo.getEncoded())
135 .thenReturn(testPublicKey.getContent());
138 Exception exception = assertThrows(
139 KeyDecryptionException.class,
140 () -> new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build()
143 String expectedMessage = "Converting Public Key from CSR failed";
144 String actualMessage = exception.getMessage();
147 assertTrue(actualMessage.contains(expectedMessage));
150 private PemObject getPemPrivateKey() throws KeyDecryptionException {
151 PemObjectFactory pemObjectFactory = new PemObjectFactory();
152 return pemObjectFactory.createPemObject(TEST_PK).orElseThrow(
153 () -> new KeyDecryptionException("Private key decoding fail")
157 private PemObject getPemWrongKey() throws KeyDecryptionException {
158 PemObjectFactory pemObjectFactory = new PemObjectFactory();
159 return pemObjectFactory.createPemObject(TEST_PEM).orElseThrow(
160 () -> new KeyDecryptionException("Private key decoding fail")
164 private CsrModel generateTestCsrModel(PKCS10CertificationRequest testCsr) throws DecryptionException {
165 PemObject testPrivateKey = pemObjectFactory.createPemObject(TEST_PK).orElseThrow(
166 () -> new DecryptionException("Incorrect Private Key, decryption failed")
168 return new CsrModel.CsrModelBuilder(testCsr, testPrivateKey).build();
171 private PemObject generateTestPublicKey() throws DecryptionException, IOException {
172 PKCS10CertificationRequest testCsr = generateTestCertificationRequest();
173 return new PemObject("PUBLIC KEY", testCsr.getSubjectPublicKeyInfo().getEncoded());
176 private PKCS10CertificationRequest generateTestCertificationRequest() throws DecryptionException {
177 return pemObjectFactory.createPemObject(TEST_CSR)
179 certificationRequestFactory::createPkcs10CertificationRequest
181 () -> new DecryptionException("Incorrect CSR, decryption failed")