60121b033823e5b72a084b7fbf6ee48cb9eebcb9
[oom/platform/cert-service.git] /
1 /*============LICENSE_START=======================================================
2  * aaf-certservice-client
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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=========================================================
18  */
19
20 package org.onap.aaf.certservice.client.certification.conversion;
21
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import org.onap.aaf.certservice.client.certification.exception.PemToPKCS12ConverterException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 class PKCS12FilesCreator {
29
30     private static final String KEYSTORE_JKS = "keystore.jks";
31     private static final String KEYSTORE_PASS = "keystore.pass";
32     private static final String TRUSTSTORE_JKS = "truststore.jks";
33     private static final String TRUSTSTORE_PASS = "truststore.pass";
34     private final String keystoreJksPath;
35     private final String keystorePassPath;
36     private final String truststoreJksPath;
37     private final String truststorePassPath;
38     private final Logger LOGGER = LoggerFactory.getLogger(PKCS12FilesCreator.class);
39
40
41     PKCS12FilesCreator(String path) {
42         keystoreJksPath = path + KEYSTORE_JKS;
43         keystorePassPath = path + KEYSTORE_PASS;
44         truststoreJksPath = path + TRUSTSTORE_JKS;
45         truststorePassPath = path + TRUSTSTORE_PASS;
46     }
47
48     void saveKeystoreData(byte[] keystoreData, String keystorePassword) throws PemToPKCS12ConverterException {
49         LOGGER.debug("Creating PKCS12 keystore files and saving data. Keystore path: {}", keystoreJksPath);
50
51         saveDataToLocation(keystoreData, keystoreJksPath);
52         saveDataToLocation(keystorePassword.getBytes(), keystorePassPath);
53     }
54
55     void saveTruststoreData(byte[] truststoreData, String truststorePassword)
56         throws PemToPKCS12ConverterException {
57         LOGGER.debug("Creating PKCS12 truststore files and saving data. Truststore path: {}", truststoreJksPath);
58
59         saveDataToLocation(truststoreData, truststoreJksPath);
60         saveDataToLocation(truststorePassword.getBytes(), truststorePassPath);
61     }
62
63     private void saveDataToLocation(byte[] data, String path) throws PemToPKCS12ConverterException {
64         try (FileOutputStream fos = new FileOutputStream(path)) {
65             fos.write(data);
66         } catch (IOException e) {
67             LOGGER.error("PKCS12 files creation failed", e);
68             throw new PemToPKCS12ConverterException(e);
69         }
70     }
71 }