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
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.aaf.certservice.client.certification.conversion;
22 import java.security.PrivateKey;
23 import java.util.List;
24 import org.onap.aaf.certservice.client.certification.exception.CertFileWriterException;
25 import org.onap.aaf.certservice.client.certification.exception.PemConversionException;
26 import org.onap.aaf.certservice.client.certification.writer.CertFileWriter;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class ConvertedArtifactsCreator implements ArtifactsCreator {
32 private static final Logger LOGGER = LoggerFactory.getLogger(ConvertedArtifactsCreator.class);
33 private static final String CERTIFICATE_ALIAS = "certificate";
34 private static final String TRUSTED_CERTIFICATE_ALIAS = "trusted-certificate-";
35 private static final int PASSWORD_LENGTH = 24;
36 private static final String PASS_EXT = "pass";
37 private static final String KEYSTORE = "keystore";
38 private static final String TRUSTSTORE = "truststore";
40 private final String fileExtension;
41 private final RandomPasswordGenerator passwordGenerator;
42 private final PemConverter converter;
43 private final CertFileWriter fileWriter;
45 public ConvertedArtifactsCreator(CertFileWriter fileWriter, RandomPasswordGenerator passwordGenerator,
46 PemConverter converter, String fileExtension) {
47 this.passwordGenerator = passwordGenerator;
48 this.converter = converter;
49 this.fileWriter = fileWriter;
50 this.fileExtension = fileExtension;
54 public void create(List<String> keystoreData, List<String> truststoreData, PrivateKey privateKey)
55 throws PemConversionException, CertFileWriterException {
56 createKeystore(keystoreData,privateKey);
57 createTruststore(truststoreData);
60 private void createKeystore(List<String> data, PrivateKey privateKey)
61 throws PemConversionException, CertFileWriterException {
62 Password password = passwordGenerator.generate(PASSWORD_LENGTH);
63 String keystoreArtifactName = String.format("%s.%s", KEYSTORE, fileExtension);
64 String keystorePass = String.format("%s.%s", KEYSTORE, PASS_EXT);
66 LOGGER.debug("Attempt to create keystore files and saving data. File names: {}, {}", keystoreArtifactName, keystorePass);
68 fileWriter.saveData(converter.convertKeystore(data, password, CERTIFICATE_ALIAS, privateKey), keystoreArtifactName);
69 fileWriter.saveData(getPasswordAsBytes(password), keystorePass);
72 private void createTruststore(List<String> data)
73 throws PemConversionException, CertFileWriterException {
74 Password password = passwordGenerator.generate(PASSWORD_LENGTH);
75 String truststoreArtifactName = String.format("%s.%s", TRUSTSTORE, fileExtension);
76 String truststorePass = String.format("%s.%s", TRUSTSTORE, PASS_EXT);
78 LOGGER.debug("Attempt to create truststore files and saving data. File names: {}, {}", truststoreArtifactName, truststorePass);
80 fileWriter.saveData(converter.convertTruststore(data, password, TRUSTED_CERTIFICATE_ALIAS), truststoreArtifactName);
81 fileWriter.saveData(getPasswordAsBytes(password), truststorePass);
84 private byte[] getPasswordAsBytes(Password password) {
85 return password.getCurrentPassword().getBytes();