1 /*============LICENSE_START=======================================================
2 * oom-truststore-merger
3 * ================================================================================
4 * Copyright (C) 2020 Nokia. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
20 package org.onap.oom.truststoremerger.certification.file;
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Path;
26 import java.nio.file.Paths;
27 import java.nio.file.StandardCopyOption;
28 import org.onap.oom.truststoremerger.certification.file.exception.KeystoreInstanceException;
29 import org.onap.oom.truststoremerger.certification.file.exception.LoadTruststoreException;
30 import org.onap.oom.truststoremerger.certification.file.model.JavaTruststore;
31 import org.onap.oom.truststoremerger.certification.file.model.PemTruststore;
32 import org.onap.oom.truststoremerger.certification.file.provider.JavaCertificateStoreController;
33 import org.onap.oom.truststoremerger.certification.file.provider.CertificateStoreControllerFactory;
34 import org.onap.oom.truststoremerger.certification.file.provider.PemCertificateController;
36 public class TestCertificateProvider {
38 public static final String SAMPLE_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-p12.p12";
39 public static final String SAMPLE_P12_TRUSTSTORE_PASSWORD = "88y9v5D8H3SG6bZWRVHDfOAo";
40 public static final String TMP_P12_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-p12.p12";
42 public static final String SAMPLE_P12_KEYSTORE_FILE_PATH = "src/test/resources/keystore.p12";
43 public static final String SAMPLE_P12_KEYSTORE_PASSWORD = "Foh49MJNYI7S_pEzE9gvUDSu";
45 public static final String SAMPLE_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore-jks.jks";
46 public static final String SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH = "src/test/resources/truststore-jks-uniq.jks";
47 public static final String SAMPLE_JKS_TRUSTSTORE_PASSWORD = "EOyuFbuYDyq_EhpboM72RHua";
48 public static final String TMP_JKS_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore-jks.jks";
50 public static final String SAMPLE_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/truststore.pem";
51 public static final String EMPTY_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/empty-truststore.pem";
52 public static final String TMP_PEM_TRUSTSTORE_FILE_PATH = "src/test/resources/tmp-truststore.pem";
53 public static final String SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH = "src/test/resources/truststore-with-private-key.pem";
55 private static final CertificateStoreControllerFactory certificateStoreControllerFactory = new CertificateStoreControllerFactory();
57 public static JavaTruststore getSampleP12Truststore() throws LoadTruststoreException, KeystoreInstanceException {
58 return createP12TruststoreInstance(SAMPLE_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD);
61 public static JavaTruststore getSampleP12Keystore() throws LoadTruststoreException, KeystoreInstanceException {
62 return createP12TruststoreInstance(SAMPLE_P12_KEYSTORE_FILE_PATH, SAMPLE_P12_KEYSTORE_PASSWORD);
65 public static JavaTruststore createTmpP12TruststoreFile()
66 throws IOException, LoadTruststoreException, KeystoreInstanceException {
67 copyFile(SAMPLE_P12_TRUSTSTORE_FILE_PATH, TMP_P12_TRUSTSTORE_FILE_PATH);
68 return createP12TruststoreInstance(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD);
71 public static JavaTruststore getTmpP12TruststoreFile() throws LoadTruststoreException, KeystoreInstanceException {
72 return createP12TruststoreInstance(TMP_P12_TRUSTSTORE_FILE_PATH, SAMPLE_P12_TRUSTSTORE_PASSWORD);
75 private static JavaTruststore createP12TruststoreInstance(String filePath, String password)
76 throws LoadTruststoreException, KeystoreInstanceException {
77 File certFile = getFile(filePath);
78 JavaCertificateStoreController storeController = certificateStoreControllerFactory
79 .createLoadedPkcs12CertificateStoreController(certFile, password);
80 return new JavaTruststore(certFile, storeController);
83 public static PemTruststore getSamplePemTruststoreFile() {
84 return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
87 public static PemTruststore getEmptyPemTruststoreFile() {
88 return getPemTruststoreInstance(EMPTY_PEM_TRUSTSTORE_FILE_PATH);
91 public static PemTruststore createEmptyTmpPemTruststoreFile() throws IOException {
92 copyFile(EMPTY_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH);
93 return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
96 public static PemTruststore createTmpPemTruststoreFile() throws IOException {
97 copyFile(SAMPLE_PEM_TRUSTSTORE_FILE_PATH, TMP_PEM_TRUSTSTORE_FILE_PATH);
98 return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
101 public static PemTruststore getTmpPemTruststoreFile() {
102 return getPemTruststoreInstance(TMP_PEM_TRUSTSTORE_FILE_PATH);
105 public static PemTruststore getPemWithPrivateKeyTruststoreFile() {
106 return getPemTruststoreInstance(SAMPLE_PEM_TRUSTSTORE_WITH_PRIVATE_KEY_FILE_PATH);
109 public static String getExpectedPemCertificateAsString() throws IOException {
110 Path samplePemFilePath = Paths.get(SAMPLE_PEM_TRUSTSTORE_FILE_PATH);
111 return Files.readString(samplePemFilePath);
114 public static JavaTruststore getSampleJksTruststoreFile()
115 throws LoadTruststoreException, KeystoreInstanceException {
116 return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD);
119 public static JavaTruststore getSampleJksTruststoreFileWithUniqueAlias()
120 throws LoadTruststoreException, KeystoreInstanceException {
121 return createJksTruststoreInstance(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH,
122 SAMPLE_JKS_TRUSTSTORE_PASSWORD);
125 public static JavaTruststore createTmpJksTruststoreFileWithUniqAlias()
126 throws IOException, LoadTruststoreException, KeystoreInstanceException {
127 copyFile(SAMPLE_JKS_TRUSTSTORE_UNIQUE_ALIAS_FILE_PATH, TMP_JKS_TRUSTSTORE_FILE_PATH);
128 return createJksTruststoreInstance(TMP_JKS_TRUSTSTORE_FILE_PATH, SAMPLE_JKS_TRUSTSTORE_PASSWORD);
131 public static void removeTemporaryFiles() throws IOException {
132 Files.deleteIfExists(Paths.get(TMP_PEM_TRUSTSTORE_FILE_PATH));
133 Files.deleteIfExists(Paths.get(TMP_JKS_TRUSTSTORE_FILE_PATH));
134 Files.deleteIfExists(Paths.get(TMP_P12_TRUSTSTORE_FILE_PATH));
137 private static JavaTruststore createJksTruststoreInstance(String filePath, String password)
138 throws LoadTruststoreException, KeystoreInstanceException {
139 File certFile = getFile(filePath);
140 JavaCertificateStoreController storeController = certificateStoreControllerFactory
141 .createLoadedJksCertificateStoreController(certFile, password);
142 return new JavaTruststore(certFile, storeController);
145 private static PemTruststore getPemTruststoreInstance(String tmpPemTruststoreFilePath) {
146 File file = getFile(tmpPemTruststoreFilePath);
147 return new PemTruststore(file, new PemCertificateController(file));
150 private static void copyFile(String sourcePath, String destPath) throws IOException {
151 Files.copy(Paths.get(sourcePath), Paths.get(destPath), StandardCopyOption.REPLACE_EXISTING);
154 private static File getFile(String path) {
155 return new File(path);