aboutsummaryrefslogtreecommitdiffstats
path: root/certServiceClient/src/test/java/org/onap/aaf/certservice/client/certification/conversion/PemConverterTest.java
blob: e3a58c3f8435f09dfda72082f2de49ad62b3dd21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*============LICENSE_START=======================================================
 * aaf-certservice-client
 * ================================================================================
 * 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.client.certification.conversion;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.onap.aaf.certservice.client.certification.EncryptionAlgorithmConstants;
import org.onap.aaf.certservice.client.certification.exception.PemConversionException;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class PemConverterTest {

    private static final String RESOURCES_PATH = "src/test/resources";
    private static final String CERT1_PATH = RESOURCES_PATH + "/cert1.pem";
    private static final String CERT2_PATH = RESOURCES_PATH + "/cert2.pem";
    private static final String KEY_PATH = RESOURCES_PATH + "/privateKey";
    private static final String EXPECTED_KEYSTORE_PATH = RESOURCES_PATH + "/expectedKeystore.jks";
    private static final String EXPECTED_TRUSTSTORE_PATH = RESOURCES_PATH + "/expectedTruststore.jks";
    private static final String PKCS12 = "PKCS12";
    private static final String PKCS8 = "PKCS#8";
    private static final String JKS = "JKS";
    private static final String KEY_ERROR_MSG = "java.security.KeyStoreException: Key protection  algorithm not found: java.lang.NullPointerException";
    private static final String CERTIFICATES_ERROR_MSG = "The certificate couldn't be parsed correctly. certificate1";
    private static final String PASSWORD_ERROR_MSG = "Password should be min. 16 chars long and should contain only alphanumeric characters and special characters like Underscore (_), Dollar ($) and Pound (#)";
    private static byte[] key;
    private PrivateKey privateKey = mock(PrivateKey.class);

    @BeforeAll
    static void setUpForAll() throws IOException {
        key = Files.readAllBytes(Path.of(KEY_PATH));
    }

    @ParameterizedTest
    @ValueSource(strings = {PKCS12, JKS})
    void convertKeystoreShouldReturnKeystoreWithGivenPrivateKeyAndCertificateChain(String conversionTarget)
            throws IOException, CertificateException, NoSuchAlgorithmException, KeyStoreException, UnrecoverableKeyException, PemConversionException {
        // given
        final String alias = "keystore-entry";
        final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0");
        final List<String> certificateChain = getCertificates();
        final PemConverter converter = new PemConverter(conversionTarget);
        final KeyStore expectedKeyStore = KeyStore.getInstance(conversionTarget);
        expectedKeyStore.load(new ByteArrayInputStream(Files.readAllBytes(Path.of(EXPECTED_KEYSTORE_PATH))),
                password.toCharArray());
        final Certificate[] expectedChain = expectedKeyStore.getCertificateChain(alias);
        privateKeyMockSetup();

        // when
        final byte[] result = converter.convertKeystore(certificateChain, password, alias, privateKey);

        // then
        final KeyStore actualKeyStore = KeyStore.getInstance(conversionTarget);
        actualKeyStore.load(new ByteArrayInputStream(result), password.toCharArray());
        final Certificate[] actualChain = actualKeyStore.getCertificateChain(alias);

        assertArrayEquals(key, actualKeyStore.getKey(alias, password.toCharArray()).getEncoded());
        assertEquals(2, expectedChain.length);
        assertArrayEquals(expectedChain, actualChain);
    }

    @ParameterizedTest
    @ValueSource(strings = {PKCS12, JKS})
    void convertKeystoreShouldThrowPemConverterExceptionBecauseOfWrongPassword(String conversionTarget) throws IOException {
        // given
        final String alias = "keystore-entry";
        final Password password = new Password("apple");
        final List<String> certificateChain = getCertificates();
        final PemConverter converter = new PemConverter(conversionTarget);
        privateKeyMockSetup();

        // when
        Exception exception = assertThrows(PemConversionException.class, () ->
                converter.convertKeystore(certificateChain, password, alias, privateKey)
        );

        // then
        assertEquals(PASSWORD_ERROR_MSG, exception.getMessage());
    }

    @ParameterizedTest
    @ValueSource(strings = {PKCS12, JKS})
    void convertTruststoreShouldReturnTruststoreWithGivenCertificatesArray(String conversionTarget)
            throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, PemConversionException {

        // given
        final PemConverter converter = new PemConverter(conversionTarget);
        final String alias = "trusted-certificate-";
        final String alias1 = alias + 1;
        final String alias2 = alias + 2;
        final Password password = new Password("9z6oFx1epRSCuBWU4Er8i_0y");
        final List<String> trustedCertificates = getCertificates();
        final KeyStore expectedTrustStore = KeyStore.getInstance(conversionTarget);
        expectedTrustStore.load(new ByteArrayInputStream(Files.readAllBytes(Path.of(EXPECTED_TRUSTSTORE_PATH))),
                password.toCharArray());

        // when
        final byte[] result = converter.convertTruststore(trustedCertificates, password, alias);

        // then
        final KeyStore actualKeyStore = KeyStore.getInstance(conversionTarget);
        actualKeyStore.load(new ByteArrayInputStream(result), password.toCharArray());

        assertTrue(actualKeyStore.containsAlias(alias1));
        assertTrue(actualKeyStore.containsAlias(alias2));
        assertEquals(expectedTrustStore.getCertificate(alias1), actualKeyStore.getCertificate(alias1));
        assertEquals(expectedTrustStore.getCertificate(alias2), actualKeyStore.getCertificate(alias2));
    }

    @ParameterizedTest
    @ValueSource(strings = {PKCS12, JKS})
    void convertTruststoreShouldThrowPemConverterExceptionBecauseOfWrongPassword(String conversionTarget) throws IOException {
        // given
        final String alias = "trusted-certificate-";
        final Password password = new Password("nokia");
        final List<String> trustedCertificates = getCertificates();
        final PemConverter converter = new PemConverter(conversionTarget);

        // when then
        assertThatThrownBy(() ->
                converter.convertTruststore(trustedCertificates, password, alias))
                .isInstanceOf(PemConversionException.class).hasMessage(PASSWORD_ERROR_MSG);
    }

    @Test
    void convertKeystoreShouldThrowPemConverterExceptionBecauseOfWrongPrivateKey() throws IOException {
        // given
        final String alias = "keystore-entry";
        final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0");
        final List<String> certificateChain = getCertificates();
        final PemConverter converter = new PemConverter(PKCS12);

        // when then
        assertThatThrownBy(() -> converter.convertKeystore(certificateChain, password, alias, privateKey))
                .isInstanceOf(PemConversionException.class).hasMessage(KEY_ERROR_MSG);
    }

    @ParameterizedTest
    @ValueSource(strings = {PKCS12, JKS})
    void convertKeystoreShouldThrowPemConverterExceptionBecauseOfWrongCertificates(String conversionTarget) {
        // given
        final String alias = "keystore-entry";
        final Password password = new Password("d9D_u8LooYaXH4G48DtN#vw0");
        final List<String> certificateChain = List.of("certificate1", "certificate2");
        final PemConverter converter = new PemConverter(conversionTarget);
        privateKeyMockSetup();

        // when then
        assertThatThrownBy(() -> converter.convertKeystore(certificateChain, password, alias, privateKey))
                .isInstanceOf(PemConversionException.class).hasMessage(CERTIFICATES_ERROR_MSG);
    }

    private void privateKeyMockSetup() {
        when(privateKey.getEncoded()).thenReturn(key);
        when(privateKey.getAlgorithm()).thenReturn(EncryptionAlgorithmConstants.RSA_ENCRYPTION_ALGORITHM);
        when(privateKey.getFormat()).thenReturn(PKCS8);
    }

    private List<String> getCertificates() throws IOException {
        return List.of(
                Files.readString(
                        Path.of(CERT1_PATH), StandardCharsets.UTF_8),
                Files.readString(
                        Path.of(CERT2_PATH), StandardCharsets.UTF_8)
        );
    }
}